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

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 argparse
import signal
import threading
import zmq # type: ignore
import requests

ZMQ_ADDR = "localhost"
ZMQ_SUB_PORT = 5756
ZMQ_PUSH_PORT = 5755
TOPIC_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 argparse
import signal
import threading
import zmq # type: ignore
from flask import Flask, render_template, Response, request # type: ignore

ZMQ_SUB_ADDR = "localhost"
ZMQ_SUB_PORT = 5756

app = 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)

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

New contributor

Iván Zamora is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật