This commit is contained in:
yumoqing 2024-09-11 12:01:41 +08:00
parent ebc27fc306
commit 14cd252502

View File

@ -5,13 +5,25 @@ from aiortc import VideoStreamTrack, AudioStreamTrack
class MyMediaPlayer(MediaPlayer):
pass
class MyAudioStreamTrack(AudioStreamTrack):
class MyTrackBasea:
def __init__(self, source=None):
super().__init__()
self.source = source
self.track = self.set_track()
self.track.on('ended', self.reload)
def set_track(self):
if self.kind == 'audio':
self.track = self.source.audio
else:
self.track = self.source.video
def self.reload(self):
print(f'reload the source----{self.source._file_path}')
self.set_source(MyMediaPlayer(self.source._file_path))
def set_source(self, source):
self.source = source
self.set_track()
async def recv(self):
if self.source is None:
@ -23,20 +35,13 @@ class MyAudioStreamTrack(AudioStreamTrack):
f = await self.source.audio.recv()
return f
class MyVideoStreamTrack(VideoStreamTrack):
class MyAudioStreamTrack(AudioStreamTrack, MyTrackBasea):
def __init__(self, source=None):
super().__init__()
self.source = source
AudioStreamTrack.__init__(self)
MyTrackBase.__init__(self, source=source)
def set_source(self, source):
self.source = source
async def recv(self):
if self.source is None:
return None
f = await self.source.video.recv()
while f is None:
print('MyVideoTrack::recv(): will reload source')
self.set_source(MyMediaPlayer(self.source._file_path))
f = await self.source.video.recv()
return f
class MyVideoStreamTrack(VideoStreamTrack, MyTrackBasea):
def __init__(self, source=None):
VideoStreamTrack.__init__(self)
MyTrackBase.__init__(self, source=source)