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()),
])
),
})