Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to create an MJPEG streaming service using Drogon ? #2256

Closed
Rz-Rz opened this issue Feb 13, 2025 · 2 comments
Closed

How to create an MJPEG streaming service using Drogon ? #2256

Rz-Rz opened this issue Feb 13, 2025 · 2 comments
Labels
FAQ Frequently Asked Questions

Comments

@Rz-Rz
Copy link

Rz-Rz commented Feb 13, 2025

I'm trying to build a motion jpeg streaming service using Drogon. The feature is quite simple using python here's an example :

@app.route('/streaming/jpeg/<string:name>')
@cross_origin(supports_credentials=True)
def video_stream(name):
    stream_data = request_stream_data_from_external_server(name)
    [Setting up streaming source...]

    def generate(fps):
        try:
            while True:
                frame = video_stream_obj.get_frame()
                if frame is not None:
                    ret, jpeg = cv2.imencode('.jpg', frame, [int(cv2.IMWRITE_JPEG_QUALITY), 80])
                    if ret:
                        yield (b'--frame\r\n'
                               b'Content-Type: image/jpeg\r\n\r\n' + jpeg.tobytes() + b'\r\n')
                else:
                    logger.warning(f"Stream '{name}': No frame available.")
                time.sleep(1 / fps)
        except GeneratorExit:
            logger.info(f"Stream '{name}': Client disconnected.")
        except Exception as e:
            logger.error(f"Stream '{name}': Error in generate function: {e}")
        finally:
            ...cleanup...

    return Response(generate(fps), mimetype='multipart/x-mixed-replace; boundary=frame')

Using multipart, I send a full jpeg frame every x ms. This allows me to display a video in an img html tag easily.

I was wondering if it is possible to do the same using Drogon. I'm having a hard time setting this up as there is no generate function. Also I can't indescriminately call response->write(frame) in my infinite loop that runs in a specific thread.

Can anyone provide some advice or an example on how to achieve that using html img tag ?

Thanks in advance.

@an-tao
Copy link
Member

an-tao commented Feb 13, 2025

You can try using the newAsyncStreamResponse method to create asynchronous data transfer.

@Rz-Rz
Copy link
Author

Rz-Rz commented Feb 13, 2025

Thank you it works and outperforms everything. Mindblowing. almost as good as webrtc.

@Rz-Rz Rz-Rz closed this as completed Feb 13, 2025
@an-tao an-tao added the FAQ Frequently Asked Questions label Feb 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
FAQ Frequently Asked Questions
Projects
None yet
Development

No branches or pull requests

2 participants