-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstreamer_utils.py
More file actions
584 lines (522 loc) · 17.3 KB
/
streamer_utils.py
File metadata and controls
584 lines (522 loc) · 17.3 KB
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
import ipaddress
import time
import os
import gi
import argparse
import subprocess
import multiprocessing
import queue as queue_module
from typing import List
gi.require_version('Gst', '1.0')
from gi.repository import Gst # noqa: E402
from common_utils import ( # noqa: E402
int_in_range,
apply_required_external_defaults,
VALID_PORT_MIN,
VALID_PORT_MAX,
get_logger,
CAMERA_FRAME_WIDTH,
CAMERA_FRAME_HEIGHT,
)
logger = get_logger(__name__)
# Initialize GStreamer
Gst.init(None)
CAMERA_RESTART_BASE_SECONDS = 0.5
CAMERA_RESTART_MAX_SECONDS = 10
STARTUP_STAGGER_SECONDS = 1
VIDEOTEST_PATTERNS = [
"smpte",
"snow",
"black",
"white",
"red",
"green",
"blue",
"checkers-1",
"checkers-2",
"checkers-4",
"checkers-8",
"circular",
"blink",
"smpte75",
"zone-plate",
"gamut",
"chroma-zone-plate",
"solid-color",
"ball",
"smpte100",
"bar",
"pinwheel",
"spokes",
"gradient",
"colors",
"smpte-rp-219",
]
def resolve_local_ip() -> str:
env_ip = os.environ.get("WRECORDER_STREAMER_IP")
if env_ip:
return env_ip
result = subprocess.run(["hostname", "-I"], capture_output=True, text=True)
if result.stdout:
try:
# Return the first non-loopback IP address
for ip_str in result.stdout.strip().split():
ip = ipaddress.ip_address(ip_str)
if not ip.is_loopback:
return ip_str
except Exception as e:
logger.error(f"Error parsing local IP addresses: {e}")
return "127.0.0.1"
def _is_gstreamer_element_available(element_name: str) -> bool:
try:
result = subprocess.run(["gst-inspect-1.0", element_name], capture_output=True)
return result.returncode == 0
except FileNotFoundError:
return False
def _configure_camera_v4l2(camera_id: int, fps: int, width: int, height: int) -> bool:
"""Configure V4L2 camera properties using v4l2-ctl."""
device = f"/dev/video{camera_id}"
try:
# Set resolution
subprocess.run(
["v4l2-ctl", "-d", device, "-v", f"width={width},height={height}"],
capture_output=True,
timeout=2.0
)
# Set FPS
subprocess.run(
["v4l2-ctl", "-d", device, "-p", str(fps)],
capture_output=True,
timeout=2.0
)
return True
except Exception as e:
logger.warning(f"Failed to configure camera {camera_id} with v4l2-ctl: {e}")
return False
def _videotestsrc_props_for_camera(camera_id: int) -> str:
pattern = VIDEOTEST_PATTERNS[camera_id % len(VIDEOTEST_PATTERNS)]
seed = camera_id + 1
foreground = 0xFF000000 | ((seed * 73) % 256) << 16 | ((seed * 151) % 256) << 8 | ((seed * 199) % 256)
background = 0xFF000000 | ((seed * 41) % 256) << 16 | ((seed * 97) % 256) << 8 | ((seed * 157) % 256)
motion = ["wavy", "sweep", "hsweep"][camera_id % 3]
horizontal_speed = ((camera_id % 5) + 1) * 2
animation_mode = ["frames", "wall-time", "running-time"][camera_id % 3]
return (
f'pattern={pattern} '
f'foreground-color={foreground} '
f'background-color={background} '
f'motion={motion} '
f'horizontal-speed={horizontal_speed} '
f'animation-mode={animation_mode}'
)
def _h264_level_for_frame_rate(width: int, height: int, fps: int) -> str:
macroblocks_w = (width + 15) // 16
macroblocks_h = (height + 15) // 16
macroblocks_per_frame = macroblocks_w * macroblocks_h
macroblocks_per_second = macroblocks_per_frame * fps
level_limits = [
("1", 99, 1485),
("1b", 99, 1485),
("1.1", 396, 3000),
("1.2", 396, 6000),
("1.3", 396, 11880),
("2", 396, 11880),
("2.1", 792, 19800),
("2.2", 1620, 20250),
("3", 1620, 40500),
("3.1", 3600, 108000),
("3.2", 5120, 216000),
("4", 8192, 245760),
("4.1", 8192, 245760),
("4.2", 8704, 522240),
]
for level, max_macroblocks_per_frame, max_macroblocks_per_second in level_limits:
if (
macroblocks_per_frame <= max_macroblocks_per_frame
and macroblocks_per_second <= max_macroblocks_per_second
):
return level
return "4.2"
def _build_encoder_pipeline(
source_element: str,
port: int,
bitrate: int,
target_fps: int,
multicast_ip: str,
force_software: bool = False,
output_width: int = CAMERA_FRAME_WIDTH,
output_height: int = CAMERA_FRAME_HEIGHT,
) -> str:
video_chain = [source_element, "videoconvert", "video/x-raw,format=I420"]
use_v4l2 = (not force_software) and _is_gstreamer_element_available("v4l2h264enc")
if use_v4l2:
logger.info(f"[stream-{port}] Using hardware encoder v4l2h264enc")
level = _h264_level_for_frame_rate(output_width, output_height, target_fps)
encoder_chain = (
f'v4l2h264enc extra-controls="encode,video_bitrate={bitrate},video_gop_size={int(target_fps)}" ! '
f'h264parse ! video/x-h264,level=(string){level}'
)
else:
if force_software:
logger.info(f"[stream-{port}] Forcing software encoder x264enc (fallback)")
else:
logger.info(f"[stream-{port}] v4l2h264enc not found, falling back to software x264enc")
kbps = max(1, bitrate // 1000)
encoder_chain = (
f'x264enc tune=zerolatency bitrate={kbps} speed-preset=ultrafast key-int-max={int(target_fps)} ! '
"h264parse"
)
return (
" ! ".join(video_chain)
+ " ! "
+ encoder_chain
+ f" ! rtph264pay config-interval=1 pt=96 ! udpsink host={multicast_ip} port={port} auto-multicast=true sync=false"
)
class StreamerConfig:
def __init__(
self,
port: int,
camera_id: int,
bitrate: int,
target_fps: int,
multicast_ip: str,
simulation: bool = False,
force_software: bool = False,
):
self.port = port
self.camera_id = camera_id
self.bitrate = bitrate
self.target_fps = target_fps
self.multicast_ip = multicast_ip
self.simulation = simulation
self.force_software = force_software
class StreamPipeline:
def __init__(self, config: StreamerConfig):
self.config = config
self.pipeline = None
self.bus = None
def _build_pipeline(self) -> str:
if self.config.simulation:
source = (
"videotestsrc is-live=true "
+ _videotestsrc_props_for_camera(self.config.camera_id)
+ f" ! video/x-raw,width={CAMERA_FRAME_WIDTH},height={CAMERA_FRAME_HEIGHT},framerate={self.config.target_fps}/1"
)
else:
device = f"/dev/video{self.config.camera_id}"
_configure_camera_v4l2(
self.config.camera_id,
self.config.target_fps,
CAMERA_FRAME_WIDTH,
CAMERA_FRAME_HEIGHT,
)
source = f"v4l2src device={device}"
return _build_encoder_pipeline(
source,
self.config.port,
self.config.bitrate,
self.config.target_fps,
self.config.multicast_ip,
force_software=getattr(self.config, "force_software", False),
output_width=CAMERA_FRAME_WIDTH,
output_height=CAMERA_FRAME_HEIGHT,
)
def start(self) -> bool:
pipeline_str = self._build_pipeline()
logger.info(f"[stream-{self.config.port}] Initializing GStreamer pipeline for {self.config.multicast_ip}:{self.config.port}")
logger.info(f"[stream-{self.config.port}] Pipeline: {pipeline_str}")
# Try to start pipeline; if starting fails and v4l2 was used, retry with software encoder
try:
self.pipeline = Gst.parse_launch(pipeline_str)
self.bus = self.pipeline.get_bus()
ret = self.pipeline.set_state(Gst.State.PLAYING)
if ret == Gst.StateChangeReturn.FAILURE:
raise RuntimeError("failed to set pipeline to PLAYING state")
logger.info(f"[stream-{self.config.port}] GStreamer pipeline initialized")
return True
except Exception as e:
logger.error(f"[stream-{self.config.port}] Failed to create GStreamer pipeline: {e}")
# If we haven't already forced software encoding, try that as a fallback
if not getattr(self.config, "force_software", False):
logger.info(f"[stream-{self.config.port}] Retrying with software encoder (fallback)")
setattr(self.config, "force_software", True)
try:
pipeline_str = self._build_pipeline()
self.pipeline = Gst.parse_launch(pipeline_str)
self.bus = self.pipeline.get_bus()
ret = self.pipeline.set_state(Gst.State.PLAYING)
if ret == Gst.StateChangeReturn.FAILURE:
raise RuntimeError("failed to set pipeline to PLAYING state (software)")
logger.info(f"[stream-{self.config.port}] GStreamer pipeline initialized (software encoder)")
return True
except Exception as e2:
logger.error(f"[stream-{self.config.port}] Fallback to software encoder failed: {e2}")
# give up
self.stop()
return False
def run_until_stopped(self, stop_event: multiprocessing.Event) -> bool:
try:
while not stop_event.is_set():
if self.bus is not None:
message = self.bus.timed_pop_filtered(
100 * Gst.MSECOND,
Gst.MessageType.ERROR | Gst.MessageType.EOS,
)
if message is not None:
if message.type == Gst.MessageType.ERROR:
err, debug = message.parse_error()
logger.error(f"[stream-{self.config.port}] GStreamer error: {err.message}; debug={debug}")
# If this looks like a v4l2 encoder frame handling failure, try runtime fallback to software encoder
err_text = (err.message or "").lower()
debug_text = (debug or "").lower()
if ("gst_v4l2_video_enc_handle_frame" in debug_text or "v4l2" in err_text or "v4l2" in debug_text) and not getattr(self.config, "force_software", False):
logger.info(f"[stream-{self.config.port}] Detected v4l2 encoder failure; attempting runtime fallback to software encoder")
# Attempt to restart with software encoder
self.stop()
setattr(self.config, "force_software", True)
try:
pipeline_str = self._build_pipeline()
logger.info(f"[stream-{self.config.port}] Restarting pipeline with software encoder: {pipeline_str}")
self.pipeline = Gst.parse_launch(pipeline_str)
self.bus = self.pipeline.get_bus()
ret = self.pipeline.set_state(Gst.State.PLAYING)
if ret == Gst.StateChangeReturn.FAILURE:
raise RuntimeError("failed to set pipeline to PLAYING state (runtime software)")
logger.info(f"[stream-{self.config.port}] Pipeline restarted with software encoder")
except Exception as e3:
logger.error(f"[stream-{self.config.port}] Runtime fallback failed: {e3}")
break
else:
logger.info(f"[stream-{self.config.port}] GStreamer EOS received")
break
time.sleep(0.1)
return stop_event.is_set()
finally:
self.stop()
def stop(self):
if self.pipeline is not None:
try:
self.pipeline.set_state(Gst.State.NULL)
except Exception as e:
logger.error(f"[stream-{self.config.port}] error stopping pipeline: {e}")
finally:
self.pipeline = None
self.bus = None
def _publish_stream_status(
status_queue: multiprocessing.Queue,
port: int,
state: str,
reason: str,
):
try:
status_queue.put_nowait(
{
"port": port,
"state": state,
"reason": reason,
}
)
except Exception:
pass
def handle_arguments():
parser = argparse.ArgumentParser(
prog="camera_streamer",
description="Streams one or more cameras using OpenCV and GStreamer UDP Multicast",
)
parser.add_argument(
"--base-port",
type=int_in_range("base-port", VALID_PORT_MIN, VALID_PORT_MAX),
help="Starting port for the first camera. Subsequent cameras use base-port+index",
)
parser.add_argument(
"--camera-ids",
type=int,
nargs="+",
help="List of camera IDs to stream (example: --camera-ids 0 2 4)",
)
parser.add_argument(
"--auto-find-cameras",
type=str,
help="Automatically find available camera IDs (on or off, overrides --camera-ids)",
choices=["on", "off"],
)
parser.add_argument(
"--bitrate",
type=int_in_range("bitrate", 1000, 100000000),
help="Target video bitrate for hardware h.264 encoding",
)
parser.add_argument(
"--target-fps",
type=int_in_range("target-fps", 1),
help="Target frames per second for streaming",
)
parser.add_argument(
"--simulate-cameras",
type=int_in_range("simulate-cameras", 1),
help="Simulate N cameras instead of real cameras (for testing)",
)
parser.add_argument(
"--streamer-name",
type=str,
help="Name announced during discovery (used by receiver filter)",
)
parser.add_argument(
"--announce-discovery",
type=str,
choices=["on", "off"],
help="Broadcast stream metadata for receiver auto-configuration",
)
parser.add_argument(
"--discovery-port",
type=int_in_range("discovery-port", VALID_PORT_MIN, VALID_PORT_MAX),
help="UDP port used for discovery announcements",
)
parser.add_argument(
"--discovery-interval",
type=float,
help="Seconds between discovery announcements",
)
parser.add_argument(
"--never-give-up",
type=str,
choices=["on", "off"],
help="Keep restarting cameras even if all streams have failed",
)
try:
apply_required_external_defaults(parser, "streamer-only")
except RuntimeError as exc:
logger.error(f"[defaults] {exc}")
exit(2)
return parser.parse_args()
class SingleStreamer:
def __init__(self, config: StreamerConfig):
self.config = config
def start(self, stop_event: multiprocessing.Event, status_queue: multiprocessing.Queue):
restart_count = 0
while not stop_event.is_set():
stream_pipeline = StreamPipeline(self.config)
if not stream_pipeline.start():
_publish_stream_status(
status_queue,
self.config.port,
"failed",
"pipeline_start_failed",
)
restart_count += 1
delay = min(CAMERA_RESTART_BASE_SECONDS * (2 ** (restart_count - 1)), CAMERA_RESTART_MAX_SECONDS)
logger.warning(
f"[stream-{self.config.port}] pipeline failed to start; retrying in {delay:.1f}s (attempt #{restart_count})"
)
time.sleep(delay)
continue
_publish_stream_status(status_queue, self.config.port, "healthy", "pipeline_started")
logger.info(f"[stream-{self.config.port}] running GStreamer pipeline")
stopped_by_request = stream_pipeline.run_until_stopped(stop_event)
logger.info(f"[stream-{self.config.port}] cleaning up pipeline")
_publish_stream_status(
status_queue,
self.config.port,
"failed" if not stop_event.is_set() else "starting",
"pipeline_stopped" if not stop_event.is_set() else "shutdown_requested",
)
if stopped_by_request or stop_event.is_set():
break
restart_count += 1
delay = min(CAMERA_RESTART_BASE_SECONDS * (2 ** (restart_count - 1)), CAMERA_RESTART_MAX_SECONDS)
logger.warning(
f"[stream-{self.config.port}] pipeline stopped unexpectedly; restarting in {delay:.1f}s (attempt #{restart_count})"
)
time.sleep(delay)
def _spawn_streamer_process(
streamer: "SingleStreamer",
stop_event: multiprocessing.Event,
status_queue: multiprocessing.Queue,
) -> multiprocessing.Process:
p = multiprocessing.Process(target=streamer.start, args=(stop_event, status_queue), daemon=True)
p.start()
return p
class MultiStreamer:
def __init__(
self,
base_port: int,
camera_ids: List[int],
bitrate: int,
target_fps: int,
multicast_ip: str,
simulation: bool = False,
never_give_up: bool = False,
):
self.streamers = [
SingleStreamer(
StreamerConfig(
port=base_port + idx,
camera_id=cam_id,
bitrate=bitrate,
target_fps=target_fps,
multicast_ip=multicast_ip,
simulation=simulation,
)
)
for idx, cam_id in enumerate(camera_ids)
]
self.stop_event = multiprocessing.Event()
self.never_give_up = never_give_up
self.status_queue: multiprocessing.Queue = multiprocessing.Queue()
self.stream_health = {sub_streamer.config.port: "starting" for sub_streamer in self.streamers}
self.processes: List[multiprocessing.Process] = []
def start(self):
for sub_streamer in self.streamers:
p = _spawn_streamer_process(sub_streamer, self.stop_event, self.status_queue)
self.processes.append(p)
logger.info(
f"Attempting to start camera {sub_streamer.config.camera_id} on port {sub_streamer.config.port}"
)
time.sleep(STARTUP_STAGGER_SECONDS) # stagger camera startups to reduce contention
def supervise(self):
while not self.stop_event.is_set():
while True:
try:
status = self.status_queue.get_nowait()
except queue_module.Empty:
break
port = status.get("port")
if port in self.stream_health:
self.stream_health[port] = str(status.get("state", "failed"))
reason = status.get("reason", "unknown")
logger.info(f"[stream-{port}] status update: {self.stream_health[port]} ({reason})")
alive_count = sum(1 for p in self.processes if p.is_alive())
if alive_count == 0:
if self.never_give_up:
logger.warning("All camera processes stopped; restarting every stream...")
self.processes = [
_spawn_streamer_process(sub_streamer, self.stop_event, self.status_queue)
for sub_streamer in self.streamers
]
for sub_streamer in self.streamers:
self.stream_health[sub_streamer.config.port] = "starting"
for sub_streamer in self.streamers:
logger.info(
f"Attempting to restart camera {sub_streamer.config.camera_id} on port {sub_streamer.config.port}"
)
for _ in self.streamers:
time.sleep(STARTUP_STAGGER_SECONDS)
continue
logger.error("All camera processes stopped. Exiting streamer.")
self.stop_event.set()
break
for idx, p in enumerate(self.processes):
if p.is_alive():
continue
p.join(timeout=0)
sub_streamer = self.streamers[idx]
logger.warning(
f"[stream-{sub_streamer.config.port}] process exited unexpectedly (code={p.exitcode}); restarting"
)
self.stream_health[sub_streamer.config.port] = "starting"
self.processes[idx] = _spawn_streamer_process(sub_streamer, self.stop_event, self.status_queue)
time.sleep(STARTUP_STAGGER_SECONDS)
if not self.never_give_up and self.stream_health and all(state == "failed" for state in self.stream_health.values()):
logger.error("All cameras are failing. Exiting streamer.")
self.stop_event.set()
break
time.sleep(0.1)