From f8a676da3ca82d798016d71a4bb936fb08b33791 Mon Sep 17 00:00:00 2001 From: Marks Polakovs Date: Thu, 2 Apr 2020 20:26:58 +0200 Subject: [PATCH] Python is a good language --- server.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/server.py b/server.py index bd8b7bf..d358b53 100644 --- a/server.py +++ b/server.py @@ -5,7 +5,7 @@ import uuid import av import struct from aiortc import MediaStreamTrack, RTCPeerConnection, RTCSessionDescription -from aiortc.contrib.media import MediaBlackhole, MediaPlayer, MediaRecorder +from aiortc.contrib.media import MediaBlackhole, MediaPlayer import jack as Jack @Jack.set_error_function @@ -40,9 +40,15 @@ class JackSender(object): def __init__(self, track): self.track = track self.resampler = None + self.ended = False + + def end(): + self.ended = True async def process(self): while True: + if self.ended: + break frame = await self.track.recv() # Right, depending on the format, we may need to do some fuckery. # Jack expects all audio to be 32 bit floating point @@ -59,7 +65,14 @@ class JackSender(object): current_session = None class Session(object): + def __init__(self): + self.websocket = None + self.sender = None + self.pc = None + async def end(): + if self.sender is not None: + self.sender.end() await self.pc.close() init_buffers() await self.websocket.send(json.dumps({ "kind": "REPLACED" })) @@ -78,8 +91,6 @@ class Session(object): self.pc = RTCPeerConnection() - self.recorder = MediaRecorder("/home/marks/test.opus") - @self.pc.on("signalingstatechange") async def on_signalingstatechange(): print(connection_id, "Signaling state is {}".format(self.pc.signalingState))