-
Notifications
You must be signed in to change notification settings - Fork 32
/
stream.py
361 lines (296 loc) · 11.3 KB
/
stream.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
#!/bin/env/python3
"""
RTSP client for getting video stream from SIYI cameras, e.g. ZR10, A8 Mini
ZR10 webpage: http://en.siyi.biz/en/Gimbal%20Camera/ZR10/overview/
A8 mini page: https://shop.siyi.biz/products/siyi-a8-mini?VariantsId=10611
Author : Mohamed Abdelkader
Email: [email protected]
Copyright 2022
Required:
- OpenCV
(sudo apt-get install python3-opencv -y)
- imutils
pip install imutils
- Gstreamer (https://gstreamer.freedesktop.org/documentation/installing/index.html?gi-language=c)
(Ubuntu: sudo apt-get install libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev libgstreamer-plugins-bad1.0-dev gstreamer1.0-plugins-base gstreamer1.0-plugins-good gstreamer1.0-plugins-bad gstreamer1.0-plugins-ugly gstreamer1.0-libav gstreamer1.0-doc gstreamer1.0-tools gstreamer1.0-x gstreamer1.0-alsa gstreamer1.0-gl gstreamer1.0-gtk3 gstreamer1.0-qt5 gstreamer1.0-pulseaudio -y)
- Deepstream (only for Nvidia Jetson boards)
(https://docs.nvidia.com/metropolis/deepstream/dev-guide/text/DS_Quickstart.html#jetson-setup)
- For RTMP streaming
sudo apt install ffmpeg -y
pip install ffmpeg-python
"""
import cv2
import logging
from time import time, sleep
import threading
import platform
class SIYIRTSP:
def __init__(self, rtsp_url="rtsp://192.168.144.25:8554/main.264", cam_name="ZR10", debug=False, use_udp=True) -> None:
'''
Receives video stream from SIYI cameras
Params
--
- rtsp_url [str] RTSP url
- cam_name [str] camera name (optional)
- debug [bool] print debug messages
- use_udp [bool] use UDP instead of TCP for RTSP transport
'''
self._original_rtsp_url = rtsp_url # Keep the original URL intact
self._rtsp_url = self._update_url_for_udp(rtsp_url, use_udp)
self._cam_name = cam_name
self._use_udp = use_udp # Track whether we are trying UDP or TCP
# Desired image width/height
self._width = 640 # Lower resolution to reduce data size
self._height = 480
# Stored image frame
self._frame = None
# Configure logging
self._debug = debug
self._logger = logging.getLogger(self.__class__.__name__)
console_handler = logging.StreamHandler()
console_handler.setFormatter(logging.Formatter('[%(levelname)s] %(asctime)s [SIYIRTSP::%(funcName)s]: %(message)s'))
self._logger.addHandler(console_handler)
self._logger.setLevel(logging.DEBUG if self._debug else logging.INFO)
# Flag to stop frame grabbing loop and close windows
self._stopped = False
self._recv_thread = None # Initialize thread variable
# Show grabbed frame in a window (mainly for debugging)
self._show_window = False
self._last_image_time = time()
# Timeout (seconds) before closing everything
self._connection_timeout = 2.0
# Start stream
self.start()
def setShowWindow(self, f: bool):
self._show_window = f
def getFrame(self):
"""
Returns current image frame
"""
return self._frame
def start(self):
"""
Start receiving thread
"""
try:
self._logger.info("Connecting to %s using %s...", self._cam_name, "UDP" if self._use_udp else "TCP")
# Initialize the FFmpeg-based VideoCapture
self._stream = cv2.VideoCapture(self._rtsp_url, cv2.CAP_FFMPEG)
# Reduce buffer size for lower latency
self._stream.set(cv2.CAP_PROP_BUFFERSIZE, 1)
# Set lower resolution and frame rate to reduce latency
self._stream.set(cv2.CAP_PROP_FRAME_WIDTH, self._width)
self._stream.set(cv2.CAP_PROP_FRAME_HEIGHT, self._height)
self._stream.set(cv2.CAP_PROP_FPS, 15) # Lower FPS to reduce processing load
if not self._stream.isOpened():
raise Exception(f"Failed to open RTSP stream {self._rtsp_url}")
# Start the receiving loop thread
self._recv_thread = threading.Thread(target=self.loop)
self._recv_thread.start()
self._stopped = False
except Exception as e:
self._logger.error("Could not receive stream from %s. Error: %s", self._cam_name, e)
# Retry with the original URL if appending ?rtsp_transport=udp failed
if self._use_udp:
self._logger.info("Retrying with the default RTSP URL (no ?rtsp_transport=udp)...")
self._use_udp = False
self._rtsp_url = self._original_rtsp_url # Reset to the original URL
self.start() # Retry with the default RTSP URL
else:
self.close()
def close(self):
self._logger.info("Closing stream of %s...", self._cam_name)
cv2.destroyAllWindows()
if self._stream:
self._stream.release()
self._stopped = True
if self._recv_thread and self._recv_thread.is_alive():
self._recv_thread.join()
def loop(self):
self._last_image_time = time()
while not self._stopped:
ret, self._frame = self._stream.read()
if not ret:
if (time() - self._last_image_time) > self._connection_timeout:
self._logger.warning("Connection timeout. Exiting")
self.close()
break
continue
self._last_image_time = time()
# Log the timestamp
timestamp = self._stream.get(cv2.CAP_PROP_POS_MSEC)
if timestamp == 0:
timestamp = time() * 1000 # Convert seconds to milliseconds for consistency
self._logger.debug(f"Frame timestamp: {timestamp} ms")
if self._show_window:
cv2.imshow('{} Stream'.format(self._cam_name), self._frame)
key = cv2.waitKey(1) & 0xFF
if key == ord('q'):
self.close()
break
# Optimized delay to avoid overwhelming CPU while reducing latency
sleep(0.001)
self._logger.warning("RTSP receiving loop is done")
return
def _update_url_for_udp(self, rtsp_url, use_udp):
"""
Modify the RTSP URL to use UDP transport if specified
"""
if use_udp:
if "rtsp_transport" not in rtsp_url:
# Add UDP transport to the URL
if '?' in rtsp_url:
return f"{rtsp_url}&rtsp_transport=udp"
else:
return f"{rtsp_url}?rtsp_transport=udp"
return rtsp_url
class RTMPSender:
'''
Streams image frames to an RTMP server
'''
def __init__(self, rtmp_url="rtmp://127.0.0.1:1935/live/webcam", debug=False) -> None:
'''
Params
--
- rtmp_url [str] RTMP server URL
- debug [bool] Printing debug message
'''
self._rtmp_url=rtmp_url
# Desired frequency of streaming to rtmp server
self._fps =30
self._last_send_time = time()
self._current_send_time = time()
# Frame to send
self._frame = None
# Desired image height
self._height = 480
# Desired image width
self._width = 640
self._toGray=False
if self._toGray:
self._pix_fmt="gray"
else:
self._pix_fmt="bgr24"
# Flag to stop streaming loop
self._stopped = False
self._debug= debug # print debug messages
if self._debug:
d_level = logging.DEBUG
else:
d_level = logging.INFO
LOG_FORMAT=' [%(levelname)s] %(asctime)s [RTMPSender::%(funcName)s] :\t%(message)s'
logging.basicConfig(format=LOG_FORMAT, level=d_level)
self._logger = logging.getLogger(self.__class__.__name__)
# Streaming thread
self._st_thread = threading.Thread(target=self.loop)
def setImageSize(self, w=640, h=480):
self._width=w
self._height=h
def setFPS(self, fps=20):
self._fps=fps
def setGrayFrame(self, b: bool):
'''
Params
--
- b [bool] True: sends grayscale image. Otherwise sends color image
'''
self._toGray = b
if self._toGray:
self._pix_fmt="gray"
else:
self._pix_fmt="bgr24"
def setFrame(self, frame):
self._frame=frame
def start(self):
command = ["ffmpeg",
"-y",
"-f", "rawvideo",
"-vcodec", "rawvideo",
"-pix_fmt", self._pix_fmt,
"-s", "{}x{}".format(self._width, self._height),
"-r", str(self._fps),
"-i", "-",
"-c:v", "libx264",
'-pix_fmt', 'yuv420p',
"-preset", "ultrafast",
"-f", "flv",
"-tune", "zerolatency",
self._rtmp_url]
# using subprocess and pipe to fetch frame data
try:
self._p = subprocess.Popen(command, stdin=subprocess.PIPE)
except Exception as e:
self._logger.error("Could not create ffmpeg pipeline. Error %s", e)
exit(1)
try:
self._st_thread.start()
except Exception as e:
self._logger.error("Could not start RTMP streaming thread")
exit(1)
def stop(self):
"""
Stops streaming thread
"""
self._logger.warning("RTMP streaming is stopped.")
self._stopped=True
self._p.kill()
def sendFrame(self) -> bool:
'''
Sends current image frame stored in self._frame
Returns
--
True if all is fine. False otherwise
'''
if self._frame is None:
return False
try:
val = self._frame.shape
rows = val[0]
cols=val[1]
# Resize the image, if needed
if rows != self._height or cols != self._width:
self._frame = cv2.resize(self._frame, (self._width,self._height), interpolation = cv2.INTER_AREA)
if self._toGray and (len(self._frame.shape) > 2):
self._frame = cv2.cvtColor(self._frame, cv2.COLOR_BGR2GRAY)
self._p.stdin.write(self._frame.tobytes())
return(True)
except Exception as e:
self._logger.error(" Error in sending: %s", e)
return(False)
def loop(self):
while(not self._stopped):
dt = time() - self._last_send_time
start_t=time()
self.sendFrame()
end_t=time()
dt=end_t-start_t
duration = 1/self._fps
if dt < duration:
sleep(duration-dt)
self._logger.warning("RTMP streaming loop is done")
return
def test():
# rtsp = SIYIRTSP(debug=False)
# rtsp.setShowWindow(True)
# Webcam
try:
wc = VideoStream(src=0).start()
except Exception as e:
print("Error in opening webcam")
exit(1)
rtmp = RTMPSender(rtmp_url="rtmp://127.0.0.1:1935/live/webcam")
rtmp.start()
try:
while(True):
# frame=stream.getFrame()
frame=wc.read()
rtmp.setFrame(frame)
except KeyboardInterrupt:
# rtsp.close()
rtmp.stop()
wc.stop()
cv2.destroyAllWindows()
# quit
exit(0)
if __name__=="__main__":
test()