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
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658 | class VideoFrameProcessor:
"""Base class for processing video frames through workflow.
Can be used independently for data-only processing (no video track output)
or as a base for VideoTransformTrackWithLoop when video output is needed.
"""
def __init__(
self,
asyncio_loop: asyncio.AbstractEventLoop,
workflow_configuration: WorkflowConfiguration,
api_key: str,
model_manager: Optional[ModelManager] = None,
data_output: Optional[List[str]] = None,
stream_output: Optional[str] = None,
has_video_track: bool = True,
declared_fps: float = 30,
termination_date: Optional[datetime.datetime] = None,
terminate_event: Optional[asyncio.Event] = None,
heartbeat_callback: Optional[Callable[[], None]] = None,
realtime_processing: bool = True,
is_preview: bool = False,
):
self._file_processing = False
self._loop = asyncio_loop
self._termination_date = termination_date
self._terminate_event = terminate_event
self.track: Optional[MediaStreamTrack] = None
self._track_active: bool = False
self._av_logging_set: bool = False
self._received_frames = 0
self._declared_fps = declared_fps
self._stop_processing = False
self._termination_reason: Optional[str] = None
self._processing_complete_sent = False
self.heartbeat_callback = heartbeat_callback
self.has_video_track = has_video_track
self.stream_output = stream_output
self.data_channel: Optional[RTCDataChannel] = None
# Video file upload support
self.video_upload_handler: Optional[VideoFileUploadHandler] = None
self._track_ready_event: asyncio.Event = asyncio.Event()
self.realtime_processing = realtime_processing
self._rotation_code: Optional[int] = None
# Optional receiver-paced flow control (enabled only after first ACK is received)
self._ack_last: int = 0
# If ack=1 and window=4, server may produce/send up to frame 5.
# Configurable via WEBRTC_DATACHANNEL_ACK_WINDOW env var.
self._ack_window: int = WEBRTC_DATA_CHANNEL_ACK_WINDOW
self._ack_event: asyncio.Event = asyncio.Event()
if data_output is None:
self.data_output = None
self._data_mode = DataOutputMode.NONE
elif isinstance(data_output, list):
self.data_output = [f for f in data_output if f]
if self.data_output == ["*"]:
self._data_mode = DataOutputMode.ALL
elif len(self.data_output) == 0:
self._data_mode = DataOutputMode.NONE
else:
self._data_mode = DataOutputMode.SPECIFIC
else:
raise WebRTCConfigurationError(
f"data_output must be list or None, got {type(data_output).__name__}"
)
self._validate_output_fields(workflow_configuration)
self._inference_pipeline = InferencePipeline.init_with_workflow(
video_reference=VideoFrameProducer,
workflow_specification=workflow_configuration.workflow_specification,
workspace_name=workflow_configuration.workspace_name,
workflow_id=workflow_configuration.workflow_id,
api_key=api_key,
image_input_name=workflow_configuration.image_input_name,
workflows_parameters=workflow_configuration.workflows_parameters,
workflows_thread_pool_workers=workflow_configuration.workflows_thread_pool_workers,
cancel_thread_pool_tasks_on_exit=workflow_configuration.cancel_thread_pool_tasks_on_exit,
video_metadata_input_name=workflow_configuration.video_metadata_input_name,
model_manager=model_manager,
_is_preview=is_preview,
)
def set_track(self, track: MediaStreamTrack, rotation_code: Optional[int] = None):
if not self.track:
self.track = track
self._rotation_code = rotation_code
self._track_ready_event.set()
async def close(self):
self._track_active = False
self._stop_processing = True
# Clean up video upload handler if present
if self.video_upload_handler is not None:
await self.video_upload_handler.cleanup()
def record_ack(self, ack: int) -> None:
"""Record cumulative ACK from the client.
ACK semantics: client has fully handled all frames <= ack.
Backwards compatible: pacing is disabled until we receive the first ACK.
"""
try:
ack_int = int(ack)
except (TypeError, ValueError):
logger.warning("Invalid ACK value: %s", ack)
return
if ack_int < 0:
logger.warning("Invalid ACK value: %s", ack)
return
if ack_int > self._ack_last:
if ack_int % 100 == 1:
logger.info("ACK received: %s", ack_int)
self._ack_last = ack_int
self._ack_event.set()
async def _wait_for_ack_window(self, next_frame_id: int) -> None:
"""Block frame production when too far ahead of client ACKs."""
if self.realtime_processing or self._ack_last == 0:
return
wait_counter = 0
while not self._stop_processing and next_frame_id > (
self._ack_last + self._ack_window
):
if self._check_termination():
return
if self.heartbeat_callback:
self.heartbeat_callback()
self._ack_event.clear()
try:
await asyncio.wait_for(self._ack_event.wait(), timeout=0.2)
except asyncio.TimeoutError:
wait_counter += 1
if wait_counter % 5 == 1:
logger.info(
"Waiting for ACK window (next=%d, ack_last=%d, window=%d)",
next_frame_id,
self._ack_last,
self._ack_window,
)
def _check_termination(self):
"""Check if we should terminate based on timeout.
Does NOT set terminate_event — callers must call _signal_termination()
after sending final data-channel messages to avoid a race with the
cleanup task closing the peer connection.
"""
if self._termination_date and self._termination_date < datetime.datetime.now():
logger.info("Timeout reached, terminating inference pipeline")
self._termination_reason = "timeout_reached"
return True
if self._terminate_event and self._terminate_event.is_set():
logger.info("Terminate event set, terminating inference pipeline")
return True
return False
def _signal_termination(self):
if self._terminate_event:
self._terminate_event.set()
@staticmethod
def serialize_outputs_sync(
fields_to_send: List[str],
workflow_output: Dict[str, Any],
data_output_mode: DataOutputMode,
) -> Tuple[Dict[str, Any], List[str]]:
"""Serialize workflow outputs for WebRTC transmission."""
serialized = {}
serialization_errors = []
for field_name in fields_to_send:
if field_name not in workflow_output:
serialization_errors.append(f"Output '{field_name}' not found")
continue
output_data = workflow_output[field_name]
if data_output_mode == DataOutputMode.ALL and isinstance(
output_data, WorkflowImageData
):
continue
try:
serialized[field_name] = serialize_for_webrtc(output_data)
except Exception as e:
serialization_errors.append(f"{field_name}: {e}")
serialized[field_name] = {"__serialization_error__": str(e)}
logger.error("[SERIALIZE] Error: %s - %s", field_name, e)
return serialized, serialization_errors
async def _send_data_output(
self,
workflow_output: Dict[str, Any],
frame_timestamp: datetime.datetime,
frame: VideoFrame,
errors: List[str],
):
frame_id = self._received_frames
if not self.data_channel or self.data_channel.readyState != "open":
return
video_metadata = WebRTCVideoMetadata(
frame_id=frame_id,
received_at=frame_timestamp.isoformat(),
pts=frame.pts,
time_base=frame.time_base,
declared_fps=self._declared_fps,
height=frame.height,
width=frame.width,
)
webrtc_output = WebRTCOutput(
serialized_output_data=None,
video_metadata=video_metadata,
errors=errors.copy(),
)
if self._data_mode == DataOutputMode.NONE:
json_bytes = await asyncio.to_thread(
lambda: json.dumps(webrtc_output.model_dump()).encode("utf-8")
)
await send_chunked_data(
self.data_channel,
frame_id,
json_bytes,
heartbeat_callback=self.heartbeat_callback,
)
return
if self._data_mode == DataOutputMode.ALL:
fields_to_send = list(workflow_output.keys())
else:
fields_to_send = self.data_output
serialized_outputs, serialization_errors = await asyncio.to_thread(
VideoFrameProcessor.serialize_outputs_sync,
fields_to_send,
workflow_output,
self._data_mode,
)
webrtc_output.errors.extend(serialization_errors)
if serialized_outputs:
webrtc_output.serialized_output_data = serialized_outputs
# TODO: use orjson
json_bytes = await asyncio.to_thread(
lambda: json.dumps(webrtc_output.model_dump(mode="json")).encode("utf-8")
)
if WEBRTC_GZIP_PREVIEW_FRAME_COMPRESSION:
def compress_json():
return gzip.compress(json_bytes, compresslevel=6)
output_bytes = await asyncio.to_thread(compress_json)
else:
output_bytes = json_bytes
success = await send_chunked_data(
self.data_channel,
frame_id,
output_bytes,
heartbeat_callback=self.heartbeat_callback,
)
if not success:
logger.error("[SEND_OUTPUT] Frame %d failed", frame_id)
async def _send_processing_complete(self):
"""Send final message indicating processing is complete.
Also drains the data channel buffer to ensure delivery before the
connection is closed.
"""
if self._processing_complete_sent:
return
if not self.data_channel or self.data_channel.readyState != "open":
return
self._processing_complete_sent = True
completion_output = WebRTCOutput(
processing_complete=True,
termination_reason=self._termination_reason,
video_metadata=WebRTCVideoMetadata(
frame_id=self._received_frames,
received_at=datetime.datetime.now().isoformat(),
),
)
json_bytes = json.dumps(completion_output.model_dump()).encode("utf-8")
await send_chunked_data(
self.data_channel, self._received_frames + 1, json_bytes
)
if not await wait_for_buffer_drain(
self.data_channel, timeout=2.0, low_threshold=0
):
logger.warning(
"Buffer drain timed out, processing_complete may not reach client"
)
async def process_frames_data_only(self):
"""Process frames for data extraction only, without video track output."""
if not self._av_logging_set:
av_logging.set_libav_level(av_logging.ERROR)
self._av_logging_set = True
try:
while not self._stop_processing:
await self._wait_for_ack_window(next_frame_id=self._received_frames + 1)
if self._check_termination():
await self._send_processing_complete()
self._signal_termination()
break
if self.heartbeat_callback:
self.heartbeat_callback()
if not self.track or self.track.readyState == "ended":
break
# Drain queue for realtime RTSP
if (
isinstance(self.track, PlayerStreamTrack)
and self.realtime_processing
):
while self.track._queue.qsize() > 30:
self.track._queue.get_nowait()
frame = await self.track.recv()
self._received_frames += 1
frame_timestamp = datetime.datetime.now()
workflow_output, _, errors = await self._process_frame_async(
frame=frame,
frame_id=self._received_frames,
render_output=False,
include_errors_on_frame=False,
)
await self._send_data_output(
workflow_output, frame_timestamp, frame, errors
)
except asyncio.CancelledError as exc:
# No one will catch this exception as it's executed in a create_task
logger.info("[DATA_ONLY] Processing cancelled: %s", exc)
except MediaStreamError as exc:
logger.info("[DATA_ONLY] Media stream ended: %s", exc)
except Exception as exc:
logger.error(
"[DATA_ONLY] Error at frame %d: %s", self._received_frames, exc
)
finally:
await self._send_processing_complete()
@staticmethod
def _ensure_workflow_specification(
workflow_configuration: WorkflowConfiguration, api_key: str
) -> None:
has_specification = workflow_configuration.workflow_specification is not None
has_workspace_and_workflow_id = (
workflow_configuration.workspace_name is not None
and workflow_configuration.workflow_id is not None
)
if not has_specification and not has_workspace_and_workflow_id:
raise WebRTCConfigurationError(
"Either 'workflow_specification' or both 'workspace_name' and 'workflow_id' must be provided"
)
if not has_specification and has_workspace_and_workflow_id:
try:
workflow_configuration.workflow_specification = (
get_workflow_specification(
api_key=api_key,
workspace_id=workflow_configuration.workspace_name,
workflow_id=workflow_configuration.workflow_id,
)
)
workflow_configuration.workspace_name = None
workflow_configuration.workflow_id = None
except Exception as e:
raise WebRTCConfigurationError(
f"Failed to fetch workflow specification from API: {str(e)}"
)
def _validate_output_fields(
self, workflow_configuration: WorkflowConfiguration
) -> None:
if workflow_configuration.workflow_specification is None:
return
workflow_outputs = workflow_configuration.workflow_specification.get(
"outputs", []
)
available_output_names = [o.get("name") for o in workflow_outputs]
if self._data_mode == DataOutputMode.SPECIFIC:
invalid_fields = [
field
for field in self.data_output
if field not in available_output_names
]
if invalid_fields:
raise WebRTCConfigurationError(
f"Invalid data_output fields: {invalid_fields}. "
f"Available workflow outputs: {available_output_names}"
)
if self.stream_output and self.stream_output not in available_output_names:
raise WebRTCConfigurationError(
f"Invalid stream_output field: '{self.stream_output}'. "
f"Available workflow outputs: {available_output_names}"
)
async def _process_frame_async(
self,
frame: VideoFrame,
frame_id: int,
stream_output: Optional[str] = None,
render_output: bool = True,
include_errors_on_frame: bool = True,
) -> Tuple[Dict[str, Any], Optional[VideoFrame], List[str]]:
"""Async wrapper for process_frame using executor."""
if self._rotation_code is not None:
frame = rotate_video_frame(frame, self._rotation_code)
loop = asyncio.get_running_loop()
return await loop.run_in_executor(
None,
process_frame,
frame,
frame_id,
self._declared_fps,
self._declared_fps, # TODO: measure fps
self._file_processing,
self._inference_pipeline,
stream_output,
render_output,
include_errors_on_frame,
)
|