-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmedia.py
More file actions
856 lines (711 loc) · 28.1 KB
/
Copy pathmedia.py
File metadata and controls
856 lines (711 loc) · 28.1 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
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
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
"""
media.py modifies some components in aiortc.contrib.media to meet the functional requirements of our framework.
Adapted classes and functions have suffix "Delta" and "delta" respectively.
"""
__author__ = "Yihang Wu"
import asyncio
import errno
import fractions
import logging
import threading
import time
import os
from datetime import datetime
from typing import Dict, Optional, Set
import av
from av import AudioFrame, VideoFrame
from av.frame import Frame
from aiortc.mediastreams import AUDIO_PTIME, MediaStreamError, MediaStreamTrack
logger = logging.getLogger(__name__)
REAL_TIME_FORMATS = [
"alsa",
"android_camera",
"avfoundation",
"bktr",
"decklink",
"dshow",
"fbdev",
"gdigrab",
"iec61883",
"jack",
"kmsgrab",
"openal",
"oss",
"pulse",
"sndio",
"rtsp",
"v4l2",
"vfwcap",
"x11grab",
]
async def blackhole_consume(track):
while True:
try:
await track.recv()
except MediaStreamError:
return
class MediaBlackhole:
"""
A media sink that consumes and discards all media.
"""
def __init__(self):
self.__tracks = {}
def addTrack(self, track):
"""
Add a track whose media should be discarded.
:param track: A :class:`aiortc.MediaStreamTrack`.
"""
if track not in self.__tracks:
self.__tracks[track] = None
async def start(self):
"""
Start discarding media.
"""
for track, task in self.__tracks.items():
if task is None:
self.__tracks[track] = asyncio.ensure_future(blackhole_consume(track))
async def stop(self):
"""
Stop discarding media.
"""
for task in self.__tracks.values():
if task is not None:
task.cancel()
self.__tracks = {}
def player_worker(
loop, container, streams, audio_track, video_track, quit_event, throttle_playback
):
audio_fifo = av.AudioFifo()
audio_format_name = "s16"
audio_layout_name = "stereo"
audio_sample_rate = 48000
audio_samples = 0
audio_samples_per_frame = int(audio_sample_rate * AUDIO_PTIME)
audio_resampler = av.AudioResampler(
format=audio_format_name, layout=audio_layout_name, rate=audio_sample_rate
)
video_first_pts = None
frame_time = None
start_time = time.time()
while not quit_event.is_set():
try:
frame = next(container.decode(*streams))
except (av.AVError, StopIteration) as exc:
if isinstance(exc, av.FFmpegError) and exc.errno == errno.EAGAIN:
time.sleep(0.01)
continue
if audio_track:
asyncio.run_coroutine_threadsafe(audio_track._queue.put(None), loop)
if video_track:
asyncio.run_coroutine_threadsafe(video_track._queue.put(None), loop)
break
# read up to 1 second ahead
if throttle_playback:
elapsed_time = time.time() - start_time
if frame_time and frame_time > elapsed_time + 1:
time.sleep(0.1)
if isinstance(frame, AudioFrame) and audio_track:
if (
frame.format.name != audio_format_name
or frame.layout.name != audio_layout_name
or frame.sample_rate != audio_sample_rate
):
frame.pts = None
frame = audio_resampler.resample(frame)
# fix timestamps
frame.pts = audio_samples
frame.time_base = fractions.Fraction(1, audio_sample_rate)
audio_samples += frame.samples
audio_fifo.write(frame)
while True:
frame = audio_fifo.read(audio_samples_per_frame)
if frame:
frame_time = frame.time
asyncio.run_coroutine_threadsafe(
audio_track._queue.put(frame), loop
)
else:
break
elif isinstance(frame, VideoFrame) and video_track:
if frame.pts is None: # pragma: no cover
logger.warning(
"MediaPlayer(%s) Skipping video frame with no pts", container.name
)
continue
# video from a webcam doesn't start at pts 0, cancel out offset
if video_first_pts is None:
video_first_pts = frame.pts
frame.pts -= video_first_pts
frame_time = frame.time
asyncio.run_coroutine_threadsafe(video_track._queue.put(frame), loop)
def player_worker_delta(
loop, container, streams, audio_track, video_track, quit_event, throttle_playback,
frame_height, frame_width, slot, framerate_degradation
):
"""
A worker to play the media stream
Change list:
- the idle time for throttle playback
- accept specified frame size (height, width), which the original frame is resized to
- a slot for placing most-recent original frame and its low-quality version
- reduce framerate in terms of framerate_degradation
"""
audio_fifo = av.AudioFifo()
audio_format_name = "s16"
audio_layout_name = "stereo"
audio_sample_rate = 48000
audio_samples = 0
audio_samples_per_frame = int(audio_sample_rate * AUDIO_PTIME)
audio_resampler = av.AudioResampler(
format=audio_format_name, layout=audio_layout_name, rate=audio_sample_rate
)
video_first_pts = None
frame_time = None
start_time = time.time()
count = -1
while not quit_event.is_set():
try:
frame = next(container.decode(*streams))
count += 1
if count % framerate_degradation != 0:
continue
except (av.AVError, StopIteration) as exc:
if isinstance(exc, av.FFmpegError) and exc.errno == errno.EAGAIN:
time.sleep(0.01)
continue
if audio_track:
asyncio.run_coroutine_threadsafe(audio_track._queue.put(None), loop)
if video_track:
asyncio.run_coroutine_threadsafe(video_track._queue.put(None), loop)
break
# read up to some time ahead to avoid excessive memory usage
if throttle_playback:
elapsed_time = time.time() - start_time
if frame_time and frame_time > elapsed_time + 1:
time.sleep(0.6)
"""
Change from default 0.1.
I think this should adapt to the playout framerate (fps) of the video.
Normally, the thread adds frame in a very quick manner.
If fps > 10, which is normal the case, then 0.1 is ok,
becase the interval between adjacent frames is < 0.1, say it 0.03 for 30fps.
Now, if we find frame_time > elapsed_time + 1, we stop 0.1s,
then we actually consume 3 frames then we add 1 frame, which attain the queue size.
However, if fps == 5, sleep 0.1 wouldn't help.
We can sleep 0.6 at this case.
"""
if isinstance(frame, AudioFrame) and audio_track:
if (
frame.format.name != audio_format_name
or frame.layout.name != audio_layout_name
or frame.sample_rate != audio_sample_rate
):
frame.pts = None
frame = audio_resampler.resample(frame)
# fix timestamps
frame.pts = audio_samples
frame.time_base = fractions.Fraction(1, audio_sample_rate)
audio_samples += frame.samples
audio_fifo.write(frame)
while True:
frame = audio_fifo.read(audio_samples_per_frame)
if frame:
frame_time = frame.time
asyncio.run_coroutine_threadsafe(
audio_track._queue.put(frame), loop
)
else:
break
elif isinstance(frame, VideoFrame) and video_track:
if frame.pts is None: # pragma: no cover
logger.warning(
"MediaPlayer(%s) Skipping video frame with no pts", container.name
)
continue
# video from a webcam doesn't start at pts 0, cancel out offset
if video_first_pts is None:
video_first_pts = frame.pts
frame.pts -= video_first_pts
frame_time = frame.time
# resize (provided frame_height and frame_width are valid integers)
lr_frame = frame.reformat(height=frame_height, width=frame_width, interpolation='BILINEAR')
asyncio.run_coroutine_threadsafe(video_track._queue.put(lr_frame), loop)
# put the frame pair to most recent slot
slot.put([frame, lr_frame])
class PlayerStreamTrack(MediaStreamTrack):
def __init__(self, player, kind):
super().__init__()
self.kind = kind
self._player = player
self._queue = asyncio.Queue()
self._start = None
async def recv(self):
if self.readyState != "live":
raise MediaStreamError
self._player._start(self)
frame = await self._queue.get()
if frame is None:
self.stop()
raise MediaStreamError
frame_time = frame.time
# control playback rate
if (
self._player is not None
and self._player._throttle_playback
and frame_time is not None
):
if self._start is None:
self._start = time.time() - frame_time
else:
wait = self._start + frame_time - time.time()
await asyncio.sleep(wait)
return frame
def stop(self):
super().stop()
if self._player is not None:
self._player._stop(self)
self._player = None
class PlayerStreamTrackDelta(MediaStreamTrack):
"""
A delta adaption for class PlayerStreamTrack.
Change list:
- add frame count
"""
def __init__(self, player, kind):
super().__init__()
self.kind = kind
self._player = player
self._queue = asyncio.Queue()
self._start = None
self._count = 0
async def recv(self):
if self.readyState != "live":
raise MediaStreamError
self._player._start(self)
frame = await self._queue.get()
if frame is None:
self.stop()
raise MediaStreamError
frame_time = frame.time
# control playback rate
if (
self._player is not None
and self._player._throttle_playback
and frame_time is not None
):
if self._start is None:
self._start = time.time() - frame_time
else:
wait = self._start + frame_time - time.time()
await asyncio.sleep(wait)
self._count += 1
return frame
def stop(self):
super().stop()
if self._player is not None:
self._player._stop(self)
self._player = None
logger.info(f'PlayerStreamTrack totally played {self._count} frames')
class MediaPlayer:
"""
A media source that reads audio and/or video from a file.
Examples:
.. code-block:: python
# Open a video file.
player = MediaPlayer('/path/to/some.mp4')
# Open an HTTP stream.
player = MediaPlayer(
'http://download.tsi.telecom-paristech.fr/'
'gpac/dataset/dash/uhd/mux_sources/hevcds_720p30_2M.mp4')
# Open webcam on Linux.
player = MediaPlayer('/dev/video0', format='v4l2', options={
'video_size': '640x480'
})
# Open webcam on OS X.
player = MediaPlayer('default:none', format='avfoundation', options={
'video_size': '640x480'
})
# Open webcam on Windows.
player = MediaPlayer('video=Integrated Camera', format='dshow', options={
'video_size': '640x480'
})
:param file: The path to a file, or a file-like object.
:param format: The format to use, defaults to autodect.
:param options: Additional options to pass to FFmpeg.
"""
def __init__(self, file, format=None, options={}):
self.__container = av.open(file=file, format=format, mode="r", options=options)
self.__thread: Optional[threading.Thread] = None
self.__thread_quit: Optional[threading.Event] = None
# examine streams
self.__started: Set[PlayerStreamTrack] = set()
self.__streams = []
self.__audio: Optional[PlayerStreamTrack] = None
self.__video: Optional[PlayerStreamTrack] = None
for stream in self.__container.streams:
if stream.type == "audio" and not self.__audio:
self.__audio = PlayerStreamTrack(self, kind="audio")
self.__streams.append(stream)
elif stream.type == "video" and not self.__video:
self.__video = PlayerStreamTrack(self, kind="video")
self.__streams.append(stream)
# check whether we need to throttle playback
container_format = set(self.__container.format.name.split(","))
self._throttle_playback = not container_format.intersection(REAL_TIME_FORMATS)
@property
def audio(self) -> MediaStreamTrack:
"""
A :class:`aiortc.MediaStreamTrack` instance if the file contains audio.
"""
return self.__audio
@property
def video(self) -> MediaStreamTrack:
"""
A :class:`aiortc.MediaStreamTrack` instance if the file contains video.
"""
return self.__video
def _start(self, track: PlayerStreamTrack) -> None:
self.__started.add(track)
if self.__thread is None:
self.__log_debug("Starting worker thread")
self.__thread_quit = threading.Event()
self.__thread = threading.Thread(
name="media-player",
target=player_worker,
args=(
asyncio.get_event_loop(),
self.__container,
self.__streams,
self.__audio,
self.__video,
self.__thread_quit,
self._throttle_playback,
),
)
self.__thread.start()
def _stop(self, track: PlayerStreamTrack) -> None:
self.__started.discard(track)
if not self.__started and self.__thread is not None:
self.__log_debug("Stopping worker thread")
self.__thread_quit.set()
self.__thread.join()
self.__thread = None
if not self.__started and self.__container is not None:
self.__container.close()
self.__container = None
def __log_debug(self, msg: str, *args) -> None:
logger.debug(f"MediaPlayer(%s) {msg}", self.__container.name, *args)
class MediaPlayerDelta:
"""
A delta adaption for class MediaPlayer.
Change list:
- use PlayerStreamTrackDelta as internal track
- use player_worker_delta to crop frame and add pair to most-recent slot
- add arguments frame_height, frame_width for cropping frame
- add argument frame_pair_queue for (hr_frame, lr_frame) pairs
- add argument slot for placing the most-recent frame
- add argument framerate_degradation to reduce framerate when necessary
"""
def __init__(self, file, frame_width, frame_height, slot=None, framerate_degradation=1, format=None, options={}):
self.__container = av.open(file=file, format=format, mode="r", options=options)
self.__thread: Optional[threading.Thread] = None
self.__thread_quit: Optional[threading.Event] = None
# examine streams
self.__started: Set[PlayerStreamTrackDelta] = set()
self.__streams = []
self.__audio: Optional[PlayerStreamTrackDelta] = None
self.__video: Optional[PlayerStreamTrackDelta] = None
for stream in self.__container.streams:
if stream.type == "audio" and not self.__audio:
self.__audio = PlayerStreamTrackDelta(self, kind="audio")
self.__streams.append(stream)
elif stream.type == "video" and not self.__video:
self.__video = PlayerStreamTrackDelta(self, kind="video")
self.__streams.append(stream)
# check whether we need to throttle playback
container_format = set(self.__container.format.name.split(","))
self._throttle_playback = not container_format.intersection(REAL_TIME_FORMATS)
self._frame_width = frame_width # desired playout width
self._frame_height = frame_height # desired playout height
self._slot = slot # asyncio.Queue wrapper that stores "most recent" [hr, lr] frame pair
self._framerate_degradation = framerate_degradation
@property
def audio(self) -> MediaStreamTrack:
"""
A :class:`aiortc.MediaStreamTrack` instance if the file contains audio.
"""
return self.__audio
@property
def video(self) -> MediaStreamTrack:
"""
A :class:`aiortc.MediaStreamTrack` instance if the file contains video.
"""
return self.__video
def _start(self, track: PlayerStreamTrackDelta) -> None:
self.__started.add(track)
if self.__thread is None:
self.__log_debug("Starting worker thread")
self.__thread_quit = threading.Event()
self.__thread = threading.Thread(
name="media-player",
target=player_worker_delta,
args=(
asyncio.get_event_loop(),
self.__container,
self.__streams,
self.__audio,
self.__video,
self.__thread_quit,
self._throttle_playback,
self._frame_height,
self._frame_width,
self._slot,
self._framerate_degradation
),
)
self.__thread.start()
def _stop(self, track: PlayerStreamTrackDelta) -> None:
self.__started.discard(track)
if not self.__started and self.__thread is not None:
self.__log_debug("Stopping worker thread")
self.__thread_quit.set()
self.__thread.join()
self.__thread = None
if not self.__started and self.__container is not None:
self.__container.close()
self.__container = None
def __log_debug(self, msg: str, *args) -> None:
logger.debug(f"MediaPlayer(%s) {msg}", self.__container.name, *args)
class MediaRecorderContext:
def __init__(self, stream):
self.stream = stream
self.task = None
class MediaRecorder:
"""
A media sink that writes audio and/or video to a file.
Examples:
.. code-block:: python
# Write to a video file.
player = MediaRecorder('/path/to/file.mp4')
# Write to a set of images.
player = MediaRecorder('/path/to/file-%3d.png')
:param file: The path to a file, or a file-like object.
:param format: The format to use, defaults to autodect.
:param options: Additional options to pass to FFmpeg.
"""
def __init__(self, file, format=None, options={}):
self.__container = av.open(file=file, format=format, mode="w", options=options)
self.__tracks = {}
def addTrack(self, track):
"""
Add a track to be recorded.
:param track: A :class:`aiortc.MediaStreamTrack`.
"""
if track.kind == "audio":
if self.__container.format.name in ("wav", "alsa"):
codec_name = "pcm_s16le"
elif self.__container.format.name == "mp3":
codec_name = "mp3"
else:
codec_name = "aac"
stream = self.__container.add_stream(codec_name)
else:
if self.__container.format.name == "image2":
stream = self.__container.add_stream("png", rate=30)
stream.pix_fmt = "rgb24"
else:
stream = self.__container.add_stream("libx264", rate=30)
stream.pix_fmt = "yuv420p"
self.__tracks[track] = MediaRecorderContext(stream)
async def start(self):
"""
Start recording.
"""
for track, context in self.__tracks.items():
if context.task is None:
context.task = asyncio.ensure_future(self.__run_track(track, context))
async def stop(self):
"""
Stop recording.
"""
if self.__container:
for track, context in self.__tracks.items():
if context.task is not None:
context.task.cancel()
context.task = None
for packet in context.stream.encode(None):
self.__container.mux(packet)
self.__tracks = {}
if self.__container:
self.__container.close()
self.__container = None
async def __run_track(self, track, context):
while True:
try:
frame = await track.recv()
except MediaStreamError:
return
for packet in context.stream.encode(frame):
self.__container.mux(packet)
class MediaRecorderDelta:
"""
A delta adaption for class MediaRecorder
Change list:
- add arguments width, height, fps to constructor, allowing different recording settings
- enable an optional log file to write the timestamp of each frame
- [Not really in used] add method stop_after_finish, which will not stop the recorder until the recording is done
"""
def __init__(self, file, logfile: str = None, width: int = None, height: int = None, fps: int = 30, format=None, options={}):
self._container = av.open(file=file, format=format, mode="w", options=options)
self._tracks = {}
self.width = width
self.height = height
self.fps = fps
self._log = open(logfile if logfile is not None else os.devnull, 'w')
self._count = 0
def addTrack(self, track):
"""
Add a track to be recorded.
:param track: A :class:`aiortc.MediaStreamTrack`.
"""
if track.kind == "audio":
if self._container.format.name in ("wav", "alsa"):
codec_name = "pcm_s16le"
elif self._container.format.name == "mp3":
codec_name = "mp3"
else:
codec_name = "aac"
stream = self._container.add_stream(codec_name)
else:
if self._container.format.name == "image2":
stream = self._container.add_stream("png", rate=30)
stream.pix_fmt = "rgb24"
else:
stream = self._container.add_stream("libx264", rate=self.fps) # adjust framerate
# Define resolution
if self.width is not None and self.height is not None:
stream.width = self.width
stream.height = self.height
stream.pix_fmt = "yuv420p"
self._tracks[track] = MediaRecorderContext(stream)
async def start(self):
"""
Start recording.
"""
for track, context in self._tracks.items():
if context.task is None:
context.task = asyncio.ensure_future(self.__run_track(track, context))
async def stop(self):
"""
Stop recording.
"""
if self._container:
for track, context in self._tracks.items():
if context.task is not None:
context.task.cancel()
context.task = None
for packet in context.stream.encode(None):
self._container.mux(packet)
self._tracks = {}
if self._container:
self._container.close()
self._container = None
self._log.close()
logger.info(f'MediaRecorder totally received {self._count} frames')
async def stop_after_finish(self):
if self._container:
for _, context in self._tracks.items():
if context.task is not None:
await context.task
context.task = None
for packet in context.stream.encode(None):
self._container.mux(packet)
self._tracks = {}
if self._container:
self._container.close()
self._container = None
self._log.close()
logger.info(f'MediaRecorder totally received {self._count} frames')
async def __run_track(self, track, context):
while True:
try:
frame = await track.recv()
self._count += 1
self._log.write(f'{datetime.now().strftime("%H:%M:%S")} {self._count:4d}\n')
self._log.flush()
except MediaStreamError:
return
for packet in context.stream.encode(frame):
self._container.mux(packet)
class RelayStreamTrack(MediaStreamTrack):
def __init__(self, relay, source: MediaStreamTrack) -> None:
super().__init__()
self.kind = source.kind
self._relay = relay
self._queue: asyncio.Queue[Optional[Frame]] = asyncio.Queue()
self._source: Optional[MediaStreamTrack] = source
async def recv(self):
if self.readyState != "live":
raise MediaStreamError
self._relay._start(self)
frame = await self._queue.get()
if frame is None:
self.stop()
raise MediaStreamError
return frame
def stop(self):
super().stop()
if self._relay is not None:
self._relay._stop(self)
self._relay = None
self._source = None
class MediaRelay:
"""
A media source that relays one or more tracks to multiple consumers.
This is especially useful for live tracks such as webcams or media received
over the network.
"""
def __init__(self) -> None:
self.__proxies: Dict[MediaStreamTrack, Set[RelayStreamTrack]] = {}
self.__tasks: Dict[MediaStreamTrack, asyncio.Future[None]] = {}
def subscribe(self, track: MediaStreamTrack) -> MediaStreamTrack:
"""
Create a proxy around the given `track` for a new consumer.
"""
proxy = RelayStreamTrack(self, track)
self.__log_debug("Create proxy %s for source %s", id(proxy), id(track))
if track not in self.__proxies:
self.__proxies[track] = set()
return proxy
def _start(self, proxy: RelayStreamTrack) -> None:
track = proxy._source
if track is not None and track in self.__proxies:
# register proxy
if proxy not in self.__proxies[track]:
self.__log_debug("Start proxy %s", id(proxy))
self.__proxies[track].add(proxy)
# start worker
if track not in self.__tasks:
self.__tasks[track] = asyncio.ensure_future(self.__run_track(track))
def _stop(self, proxy: RelayStreamTrack) -> None:
track = proxy._source
if track is not None and track in self.__proxies:
# unregister proxy
self.__log_debug("Stop proxy %s", id(proxy))
self.__proxies[track].discard(proxy)
def __log_debug(self, msg: str, *args) -> None:
logger.debug(f"MediaRelay(%s) {msg}", id(self), *args)
async def __run_track(self, track: MediaStreamTrack) -> None:
self.__log_debug("Start reading source %s" % id(track))
while True:
try:
frame = await track.recv()
except MediaStreamError:
frame = None
for proxy in self.__proxies[track]:
proxy._queue.put_nowait(frame)
if frame is None:
break
self.__log_debug("Stop reading source %s", id(track))
del self.__proxies[track]
del self.__tasks[track]