1

Was trying to wrap RSTP stream in StreamingHttpResponse and same is working completely fine with WSGI server in Django but i need to implement this with ASGI app.

Below code for reference.

In ASGI it continuously loading due to while True loop but with WSGI it works fine. I am not sure it is happening because of WSGI or ASGI.

def gen(camera):
    while True:
        frame = camera.get_frame()
        yield (b'--frame\r\n'
                b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n\r\n')

def livecam_feed(request):
    return StreamingHttpResponse(gen(LiveWebCam()),
                    content_type='multipart/x-mixed-replace; boundary=frame')

class LiveWebCam(object):
    def __init__(self):
        self.url = cv2.VideoCapture("<RTSP link here>")

def __del__(self):
    cv2.destroyAllWindows()

def get_frame(self):
    success,imgNp = self.url.read()
    resize = cv2.resize(imgNp, (640, 480), interpolation = cv2.INTER_LINEAR) 
    ret, jpeg = cv2.imencode('.jpg', resize)
    return jpeg.tobytes()

asgi.py

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'BMS_host.settings')

# application = get_asgi_application()
django.setup()

application = ProtocolTypeRouter({
    "http": get_asgi_application(),
    "websocket": AuthMiddlewareStack(
        URLRouter([
            url(r'^ws/', BmsConsumer.as_asgi()),
            url(r'^dash/', DashConsumer.as_asgi()),
            url(r'^component/', ComponentConsumer.as_asgi()),
        ])
    ),
})
  • I'm facing the same issue, but didn't find any answer – Mubashar Javed Apr 29 '21 at 08:36
  • As of now i have created an another project in which i have created this API and calling this same in my working project. I know this is not good but till time i got resolution i have to use this. Also with this stream is lagging and latency with it. – Keyur Tailor Apr 30 '21 at 04:39
  • Have done an another work around for streaming rtsp have used FFMPEG to get stream from rtsp and it will create .m3u8 file as playlist and then HLS player will play that video in web browser. This is an another option if anyone else goes through the same situation. – Keyur Tailor May 06 '21 at 17:52

0 Answers0