-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.py
178 lines (145 loc) · 5.86 KB
/
lib.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
import fractions
import av
import threading
from typing import Dict, Optional, Set
from aiortc.mediastreams import AUDIO_PTIME, MediaStreamError, MediaStreamTrack, VIDEO_PTIME
from aiortc.contrib.media import player_worker_decode as player_worker, REAL_TIME_FORMATS, PlayerStreamTrack
import time
import asyncio
from av.audio.frame import AudioFrame
from av.frame import Frame
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.
:param loop: Whether to repeat playback indefinitely (requires a seekable file).
"""
def __init__(self, file, format=None, options={}, loop=False, audio=0):
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
audio_counter = -1
for stream in self.__container.streams:
if stream.type == "audio" and not self.__audio:
audio_counter += 1
if audio_counter != audio:
continue
self.__audio = PlayerStreamTrack(self, kind="audio")
self.__streams.append(stream)
elif stream.type == "video" and not self.__video and not audio:
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)
# check whether the looping is supported
assert (
not loop or self.__container.duration is not None
), "The `loop` argument requires a seekable file"
self._loop_playback = loop
@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._loop_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:
for steam in self.__streams:
steam.close()
self.__container.close()
del self.__container
self.__container = None
def __log_debug(self, msg: str, *args) -> None:
pass
# logger.debug(f"MediaPlayer(%s) {msg}", self.__container.name, *args)
class AudioStreamTrack(MediaStreamTrack):
"""
A dummy audio track which reads silence.
"""
kind = "audio"
_start: float
_timestamp: int
async def recv(self) -> Frame:
"""
Receive the next :class:`~av.audio.frame.AudioFrame`.
The base implementation just reads silence, subclass
:class:`AudioStreamTrack` to provide a useful implementation.
"""
if self.readyState != "live":
raise MediaStreamError
sample_rate = 48000
samples = int(AUDIO_PTIME * sample_rate)
if hasattr(self, "_timestamp"):
self._timestamp += samples
wait = self._start + (self._timestamp / sample_rate) - time.time()
await asyncio.sleep(wait)
else:
self._start = time.time()
self._timestamp = 0
frame = AudioFrame(format="s16", layout="mono", samples=samples)
for p in frame.planes:
p.update(bytes(p.buffer_size))
frame.pts = self._timestamp
frame.sample_rate = sample_rate
frame.time_base = fractions.Fraction(1, sample_rate)
return frame