Hello, for context, I'm doing a project on an Air Duct Inspection robot, and right now im working on streaming a HD video to browser. We're using a WiFi Mesh and we put the routers and different rooms. The code manage to stream h264 video to browser live, but when we're testing the range (Taking the raspberry pi camera further from the monitor displaying the video), the stream will buffer and then disconnect after I walked out of the room even though the router is in the next room I'm already in. Im confused as the Raspberry Pi should not lose connection as the routers are relatively close to one another as one of each is placed in 1 room. We used the code from a github that allows h264 streaming for PiCamera2 library.
Link: https://github.com/chradev/pi-h264-to-browser-streamer
The code is below:
I'd really hope it's the code issue that the Raspberry Pi cant connect because the WiFi mesh seems to be working when we tested it.
Link: https://github.com/chradev/pi-h264-to-browser-streamer
The code is below:
Code:
import ioimport osimport socketfrom string import Templatefrom threading import Conditionimport tornado.ioloopimport tornado.webimport tornado.websocketfrom picamera2 import Picamera2from picamera2.encoders import H264Encoderfrom picamera2.outputs import Output# start configurationserverPort = 8000wsURL = "ws://192.168.0.243:8000/ws/"framerate = 60picam2 = Picamera2()picam2.configure(picam2.create_video_configuration(main={"size": (1280, 720)}))abspath = os.path.abspath(__file__)dname = os.path.dirname(abspath)os.chdir(dname)def getFile(filePath): file = open(filePath, 'r') content = file.read() file.close() return contentdef templatize(content, replacements): tmpl = Template(content) return tmpl.substitute(replacements)indexHtml = templatize(getFile('index.html'), {'wsurl': wsURL, 'fps': framerate})jmuxerJs = getFile('jmuxer.min.js')class StreamingOutput(Output): def __init__(self): super().__init__() self.loop = None self.buffer = io.BytesIO() def setLoop(self, loop): self.loop = loop def outputframe(self, frame, keyframe=True, timestamp=None): self.buffer.write(frame) if self.loop is not None and wsHandler.hasConnections(): self.loop.add_callback(callback=wsHandler.broadcast, message=self.buffer.getvalue()) self.buffer.seek(0) self.buffer.truncate()class wsHandler(tornado.websocket.WebSocketHandler): connections = [] def open(self): self.connections.append(self) def on_close(self): self.connections.remove(self) def on_message(self, message): pass @classmethod def hasConnections(cl): if len(cl.connections) == 0: return False return True @classmethod async def broadcast(cl, message): for connection in cl.connections: try: await connection.write_message(message, True) except tornado.websocket.WebSocketClosedError: pass except tornado.iostream.StreamClosedError: pass def check_origin(self, origin): return Trueclass indexHandler(tornado.web.RequestHandler): def get(self): self.write(indexHtml)class jmuxerHandler(tornado.web.RequestHandler): def get(self): self.set_header('Content-Type', 'text/javascript') self.write(jmuxerJs)requestHandlers = [ (r"/ws/", wsHandler), (r"/", indexHandler), (r"/index.html", indexHandler), (r"/jmuxer.min.js", jmuxerHandler)]try: output = StreamingOutput() encoder = H264Encoder(repeat=True, framerate=framerate, qp=20) encoder.output = output picam2.start_recording(encoder, output) application = tornado.web.Application(requestHandlers) application.listen(serverPort) loop = tornado.ioloop.IOLoop.current() output.setLoop(loop) loop.start()except KeyboardInterrupt: picam2.stop_recording() loop.stop()
Statistics: Posted by CrabbyPatty — Mon Nov 11, 2024 7:31 am