Adding support for renegotiation.
This commit is contained in:
@@ -156,17 +156,17 @@ class SmallWebRTCClient:
|
||||
self._pipecat_resampler = AudioResampler("s16", "mono", 16000)
|
||||
|
||||
@self._webrtcConnection.on("connected")
|
||||
async def on_connected():
|
||||
async def on_connected(connection: SmallWebRTCConnection):
|
||||
logger.info("Peer connection established.")
|
||||
await self._handle_client_connected()
|
||||
|
||||
@self._webrtcConnection.on("disconnected")
|
||||
async def on_disconnected():
|
||||
async def on_disconnected(connection: SmallWebRTCConnection):
|
||||
logger.info("Peer connection lost.")
|
||||
await self._handle_client_disconnected()
|
||||
|
||||
@self._webrtcConnection.on("closed")
|
||||
async def on_closed():
|
||||
async def on_closed(connection: SmallWebRTCConnection):
|
||||
logger.info("Client connection closed.")
|
||||
await self._handle_client_closed()
|
||||
|
||||
@@ -188,9 +188,8 @@ class SmallWebRTCClient:
|
||||
frame = await asyncio.wait_for(self._video_input_track.recv(), timeout=1.0)
|
||||
except asyncio.TimeoutError:
|
||||
logger.warning("Timeout: No video frame received within the specified time.")
|
||||
# TODO maybe we should ask to renegotiate in this case. Need to test.
|
||||
# self._webrtcConnection.renegotiate()
|
||||
frame = None
|
||||
self._webrtcConnection.ask_to_renegotiate()
|
||||
|
||||
if frame is None or not isinstance(frame, VideoFrame):
|
||||
# If no valid frame, sleep for a bit
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import asyncio
|
||||
import json
|
||||
import uuid
|
||||
from enum import Enum
|
||||
@@ -24,6 +25,7 @@ class SmallWebRTCConnection(EventEmitter):
|
||||
self._setup_listeners()
|
||||
self._tracks = set()
|
||||
self._data_channel = None
|
||||
self._renegotiation_in_progress = False
|
||||
|
||||
def _setup_listeners(self):
|
||||
@self.pc.on("datachannel")
|
||||
@@ -41,7 +43,7 @@ class SmallWebRTCConnection(EventEmitter):
|
||||
@self.pc.on("connectionstatechange")
|
||||
async def on_connectionstatechange():
|
||||
logger.info(f"Connection state is {self.pc.connectionState}")
|
||||
await self.emit(self.pc.connectionState)
|
||||
await self.emit(self.pc.connectionState, self)
|
||||
if self.pc.connectionState == "failed":
|
||||
await self.close()
|
||||
|
||||
@@ -57,7 +59,7 @@ class SmallWebRTCConnection(EventEmitter):
|
||||
self._tracks.discard(track)
|
||||
await self.emit("track-ended", track)
|
||||
|
||||
async def initialize(self, sdp: str, type: str):
|
||||
async def _create_answer(self, sdp: str, type: str):
|
||||
offer = RTCSessionDescription(sdp=sdp, type=type)
|
||||
await self.pc.setRemoteDescription(offer)
|
||||
|
||||
@@ -67,11 +69,26 @@ class SmallWebRTCConnection(EventEmitter):
|
||||
|
||||
self.answer = await self.pc.createAnswer()
|
||||
|
||||
return self.pc
|
||||
async def initialize(self, sdp: str, type: str):
|
||||
await self._create_answer(sdp, type)
|
||||
|
||||
async def connect(self):
|
||||
await self.pc.setLocalDescription(self.answer)
|
||||
|
||||
async def renegotiate(self, sdp: str, type: str):
|
||||
logger.info(f"Renegotiating {self.pc_id}")
|
||||
await self._create_answer(sdp, type)
|
||||
await self.pc.setLocalDescription(self.answer)
|
||||
|
||||
# TODO maybe we should refactor to receive a message from the client side when the renegotiation is completed.
|
||||
# or look at the peer connection listeners
|
||||
# but this is good enough for now for testing.
|
||||
async def delayed_task():
|
||||
await asyncio.sleep(2)
|
||||
self._renegotiation_in_progress = False
|
||||
|
||||
asyncio.create_task(delayed_task())
|
||||
|
||||
def force_transceivers_to_send_recv(self):
|
||||
for transceiver in self.pc.getTransceivers():
|
||||
transceiver.direction = "sendrecv"
|
||||
@@ -149,7 +166,11 @@ class SmallWebRTCConnection(EventEmitter):
|
||||
json_message = json.dumps(message)
|
||||
self._data_channel.send(json_message)
|
||||
|
||||
def renegotiate(self):
|
||||
def ask_to_renegotiate(self):
|
||||
if self._renegotiation_in_progress:
|
||||
return
|
||||
|
||||
self._renegotiation_in_progress = True
|
||||
self.send_app_message(
|
||||
{"type": SIGNALLING_TYPE, "message": SignallingMessage.RENEGOTIATE.value}
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user