arut/nginx-rtmp-module

Rename HLS stream directory in on_publish

style77 opened this issue · 1 comments

Hey,
I know this topic been here several times, but i checked all of them, and still can't get my server to work. I''ve got django endpoint

def post(self, request):
        data = request.data

        if data.get("addr") == "127.0.0.1":  # I also tried without these two lines
            return Response({"message": "Stream authorized"}, status=200)

        call = data.get("call", None)

        if not call or call != "publish":
            return Response({"message": "Stream authorization failed"}, status=403)

        stream_key = data.get("name")
        if not stream_key:
            return Response({"message": "Stream authorization failed"}, status=403)

        try:
            user = User.objects.get(stream_key=stream_key)
            if not user:
                return Response({"message": "Stream authorization failed"}, status=403)
        except User.DoesNotExist:
            return Response({"message": "Stream authorization failed"}, status=403)

        # Add rtmp to allowed schemes
        # https://stackoverflow.com/questions/34465617/disallowedredirect-unsafe-redirect-to-url-with-protocol-django
        HttpResponseRedirect.allowed_schemes.append("rtmp")

        return HttpResponseRedirect(
            redirect_to=f"rtmp://127.0.0.1/live/{user.username}",
            status=302,
        )

That redirects stream to streamer username instead of secret key, however it doesn't work, the only way i can access stream is through going to localhost/hls/secret_key/index.m3u8.

Nginx.conf

rtmp {
    server {
        listen 1935;
        chunk_size 4000;

        application live {
            live on;
            hls on;
            hls_nested on;
            hls_fragment 5s;
            hls_playlist_length 36000s;
            deny play all;
            hls_path /tmp/hls;
            on_publish http://127.0.0.1:8000/api/stream/auth/;
            notify_method post;
            # on_publish_done http://127.0.0.1:8080/api/stream/done/;
        }
    }
}

http {
    server {
        listen 80;

        # location / {
        #     include uwsgi_params;
        #     uwsgi_pass unix:/tmp/stream.sock;
        # }


        location /hls {
            types {
                application/vnd.apple.mpegurl m3u8;
                video/mp2t ts;
            }
            root /tmp;
            add_header Cache-Control no-cache;
            add_header 'Access-Control-Allow-Origin' '*';
            add_header 'Access-Control-Allow-Credentials' 'true';
            add_header 'Access-Control-Allow-Headers' 'User-Agent,Keep-Alive,Content-Type';
            add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
        }

    }
}

events { }

I use Nginx gryphon 1.7.11.3

error.log

2023/09/14 21:07:30 [error] 10548#5280: *11 notify: push '987c927662fb49f4' to 'rtmp://127.0.0.1/hls/kozako123', client: 127.0.0.1, server: 0.0.0.0:1935
2023/09/14 21:08:13 [error] 10548#5280: *15 CreateFile() "C:/tmp/hls/kozako123/index.m3u8" failed (3: The system cannot find the path specified), client: 127.0.0.1, server: , request: "GET /hls/kozako123/index.m3u8 HTTP/1.1", host: "localhost"
2023/09/14 21:11:40 [error] 10548#5280: *17 notify: push '987c927662fb49f4' to 'rtmp://127.0.0.1/live/kozako123', client: 127.0.0.1, server: 0.0.0.0:1935

Of course i tried many ways to redirect, none of them worked.

image

Thank you.

My bad! That first if condition were allowing every request to go through, I added check fir semvars as well