-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbroadcast.py
46 lines (41 loc) · 1.62 KB
/
broadcast.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# Origin: https://github.com/Onixaz/picamera-h264-web-streaming
# Modified by: Mikail Yoelek
from threading import Thread, Event
class BroadcastThread(Thread):
"""
A Class that inherits from Thread and broadcasts camera frames to a
websocket server.
"""
def __init__(self, camera, output, websocket_server):
"""
Constructor: params are the camera object, streamoutput and a websocket
server object
"""
super(BroadcastThread, self).__init__()
self.camera = camera
self.output = output
self.websocket_server = websocket_server
self.stop_event = Event()
def run(self):
"""
This function starts the camera recording and broadcasts the frames to the
websocket server. It uses the baseline h264 profile, which fits perfectly
for low cost applications like low delay video streams.
"""
try:
self.camera.start_recording(self.output, 'h264', profile="baseline")
while not self.stop_event.is_set():
with self.output.condition:
self.output.condition.wait()
self.websocket_server.manager.broadcast(self.output.frame,
binary=True)
except:
raise Exception
def stop_thread(self, timeout=5):
"""
This function stops the thread within a specific timeout.
"""
self.stop_event.set()
self.join(timeout)
if self.is_alive():
self._stop()