Quantcast
Channel: Active questions tagged zeromq - Stack Overflow
Viewing all articles
Browse latest Browse all 193

Load video in flask server python passing the chunks from zmq socket to it

$
0
0

I have the following problem, I am using Hololens glasses, where there is an API: https://IP/api/holographic/stream/live_high.mp4?holo=true&pv=true&mic=true&loopback=true’ that provides a streaming that the user sees. The idea is that the user will have a pair of glasses in his private network, therefore, from the outside it will not be possible to access to make the request directly, since it is a private IP.

Therefore, the idea would be to have two scripts, one that is responsible for making the HTTP GET request and get the video chunks and this, send it through a zmq socket to another script that will be on my server. The second script will be in charge of receiving through the socket all the chunks and load them in the video using a Flask Response.

The problem is that testing both scripts in one and in local it works, but when I split it in two, it doesn't load the video, I think it could be a yield problem because it receives so many data in a row that it doesn't have time to process everything.

Script Request

import argparseimport signalimport threadingimport zmq # type: ignoreimport requestsZMQ_ADDR = "localhost"ZMQ_SUB_PORT = 5756ZMQ_PUSH_PORT = 5755TOPIC_PUSH = "stream"class HololensRequestAgent():    def exec_streaming_agent(self):        try:            with requests.get(self.hololens_url, stream=True, verify=False) as req:                if req.status_code == 200:                    for chunk in req.iter_content(chunk_size=1024):                        self.push_socket.send_multipart([TOPIC_PUSH.encode('utf-8'), chunk])        except Exception as e:            print("Error reading data: {}".format(e))    def start(self):        self.stop_recording = False        self.thread.start()    def stop(self):        self.stop_recording = True        self.thread.join()    def __init__(self, topic, hololens_ip):        print("API Hololens {}".format(hololens_ip))        self.hololens_url = "https://{}/api/holographic/stream/live_high.mp4?holo=true&pv=true&mic=true&loopback=true".format(hololens_ip)        self.thread = threading.Thread(target=self.exec_streaming_agent)        # create ZMQ socket instance         self.context = zmq.Context()        # create SUB socket        self.sub_socket = self.context.socket(zmq.SUB)        print("SUB tcp://{}:{}".format(ZMQ_ADDR, ZMQ_SUB_PORT))        self.sub_socket.connect("tcp://{}:{}".format(ZMQ_ADDR, ZMQ_SUB_PORT))        self.sub_socket.subscribe(topic)        # create PUSH socket        self.push_socket = self.context.socket(zmq.PUSH)        print("PUSH tcp://{}:{}".format(ZMQ_ADDR, ZMQ_PUSH_PORT))        self.push_socket.connect("tcp://{}:{}".format(ZMQ_ADDR, ZMQ_PUSH_PORT)) # esto habrá que cambiarlo al server    def close(self, sig, frame):        print("Signal '{}' received".format(signal.strsignal(sig)))        self.stop()        print("Closing connection...")        # close connection        print("HololensRequest agent closed.")        self.context.destroy()if __name__ == "__main__":    parser = argparse.ArgumentParser(description='Hololens agent')    parser.add_argument('--topic', type=str, default="", help="Topic to subscribe to. (Subscribe to all by default)")    parser.add_argument('--hololens_ip', type=str, default="", help="IP from hololens. (Subscribe to all by default)")    args = parser.parse_args()    print("Starting hololens request agent...")    hololens_agent = HololensRequestAgent(        topic=args.topic,        hololens_ip=args.hololens_ip    )    for signal_code in (signal.SIGINT, signal.SIGTERM):        signal.signal(signal_code, hololens_agent.close)    print("Starting to run hololens")    hololens_agent.start()

Script Receiver

import argparseimport signalimport threadingimport zmq # type: ignorefrom flask import Flask, render_template, Response, request # type: ignoreZMQ_SUB_ADDR = "localhost"ZMQ_SUB_PORT = 5756app = Flask(__name__)class HololensReceiverAgent():    def receive_chunks(self):        try:            while True:                chunk = self.sub_socket.recv_multipart()[1]                print(chunk)                yield chunk        except Exception as e:            print("Error reading data: {}".format(e))                yield b''    def start(self):        self.thread.start()    def stop(self):        self.thread.join()    def __init__(self, host_addr, port, topic):        self.topic = topic        self.thread = threading.Thread(target=self.receive_chunks)        # create ZMQ socket instance        self.context = zmq.Context()        self.sub_socket = self.context.socket(zmq.SUB)        print("tcp://{}:{}".format(host_addr, port))        self.sub_socket.connect("tcp://{}:{}".format(host_addr, port))        self.sub_socket.subscribe(self.topic)    def close(self, sig, frame):        print("Signal '{}' received".format(signal.strsignal(sig)))        self.context.destroy()        shutdown_server()def shutdown_server():    func = request.environ.get('werkzeug.server.shutdown')    if func is None:        raise RuntimeError('Not running with the Werkzeug Server')    func()@app.route('/')def index():    return render_template('index.html')@app.route('/video')def video():       return Response(hololens_agent.receive_chunks(), mimetype='video/mp4')if __name__ == "__main__":    parser = argparse.ArgumentParser(description='Hololens agent')    parser.add_argument('--host_addr', type=str, default=ZMQ_SUB_ADDR, help='Host address (default 127.0.0.1)')    parser.add_argument('--port', type=int, default=ZMQ_SUB_PORT, help='Subscriber port')    parser.add_argument('--topic', type=str, default="", help="Topic to subscribe to. (Subscribe to all by default)")    args = parser.parse_args()    print("Starting hololens receiver agent...")    hololens_agent = HololensReceiverAgent(        host_addr=args.host_addr,        port=args.port,        topic=args.topic    )    for signal_code in (signal.SIGINT, signal.SIGTERM):        signal.signal(signal_code, hololens_agent.close)    print("Starting to run hololens")    hololens_agent.start()    app.run(host='0.0.0.0', port=5002, debug=True)

Index.html

<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><link href="data:image/x-icon;base64,AAABAAEAEBAQAAEABAAoAQAAFgAAACgAAAAQAAAAIAAAAAEABAAAAAAAgAAAAAAAAAAAAAAAEAAAAAAAAAAAAAAAHb7eAEDeHQAdHd4AAF7/AP/dAADmEuIAYwAMAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABBAjUGUyNWBAQQI1BlNWAxQEECNQZWA2AUBBAjUGA2cgFAQQI1A2dDIBQEECNQNnUyAUBBAjUDYFMgFAQQI1A0BTIBQEECNQFAUyAUBBAjUhQFMgFAQQIwIUBTIBQEECIUAgUyAUBBAUAhIFMgFAQUAhQSBTIBQEAhQEEgUyAUAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" rel="icon" type="image/x-icon"><title>Hololens Video</title></head><body><video id="hololens-video" autoplay controls width="100%"></video><script>        const videoElement = document.getElementById('hololens-video');        videoElement.src = '/video';</script></body></html>

I hope the video doesn't stop and I continue with the streaming.


Viewing all articles
Browse latest Browse all 193

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>