This commit is contained in:
Freddy Boulton
2025-04-14 09:57:15 -04:00
committed by GitHub
parent 5835e74377
commit 54d07bc3c8
2 changed files with 15 additions and 4 deletions

View File

@@ -22470,7 +22470,7 @@ function Bp(n, e, t) {
const oe = (N) => { const oe = (N) => {
(N == null ? void 0 : N.type) === "info" || (N == null ? void 0 : N.type) === "warning" || (N == null ? void 0 : N.type) === "error" ? b.dispatch( (N == null ? void 0 : N.type) === "info" || (N == null ? void 0 : N.type) === "warning" || (N == null ? void 0 : N.type) === "error" ? b.dispatch(
(N == null ? void 0 : N.type) === "error" ? "error" : "warning", (N == null ? void 0 : N.type) === "error" ? "error" : "warning",
N.message N.data
) : (N == null ? void 0 : N.type) === "end_stream" ? b.dispatch("warning", N.data) : (N == null ? void 0 : N.type) === "fetch_output" ? b.dispatch("state_change") : (N == null ? void 0 : N.type) === "send_input" ? b.dispatch("tick") : (N == null ? void 0 : N.type) === "connection_timeout" && b.dispatch("warning", "Taking a while to connect. Are you on a VPN?"), N.type === "state_change" && b.dispatch(N === "change" ? "state_change" : "tick"); ) : (N == null ? void 0 : N.type) === "end_stream" ? b.dispatch("warning", N.data) : (N == null ? void 0 : N.type) === "fetch_output" ? b.dispatch("state_change") : (N == null ? void 0 : N.type) === "send_input" ? b.dispatch("tick") : (N == null ? void 0 : N.type) === "connection_timeout" && b.dispatch("warning", "Taking a while to connect. Are you on a VPN?"), N.type === "state_change" && b.dispatch(N === "change" ? "state_change" : "tick");
}, q = (N) => { }, q = (N) => {
var le, qt; var le, qt;

View File

@@ -15,9 +15,11 @@ from .tracks import AsyncStreamHandler, StreamHandlerImpl
from .utils import ( from .utils import (
AdditionalOutputs, AdditionalOutputs,
CloseStream, CloseStream,
Context,
DataChannel, DataChannel,
audio_to_float32, audio_to_float32,
audio_to_int16, audio_to_int16,
current_context,
split_output, split_output,
) )
@@ -141,7 +143,7 @@ class WebSocketHandler:
) )
else: else:
await run_sync( await run_sync(
self.stream_handler.receive, self.receive_with_context,
(self.stream_handler.input_sample_rate, audio_array), (self.stream_handler.input_sample_rate, audio_array),
) )
except Exception as e: except Exception as e:
@@ -156,6 +158,7 @@ class WebSocketHandler:
self.stream_id = cast(str, message["streamSid"]) self.stream_id = cast(str, message["streamSid"])
else: else:
self.stream_id = cast(str, message["websocket_id"]) self.stream_id = cast(str, message["websocket_id"])
current_context.set(Context(webrtc_id=self.stream_id))
self.set_additional_outputs = self.set_additional_outputs_factory( self.set_additional_outputs = self.set_additional_outputs_factory(
self.stream_id self.stream_id
) )
@@ -186,13 +189,21 @@ class WebSocketHandler:
self.clean_up(cast(str, self.stream_id)) self.clean_up(cast(str, self.stream_id))
def emit_with_context(self):
current_context.set(Context(webrtc_id=cast(str, self.stream_id)))
return self.stream_handler.emit()
def receive_with_context(self, frame: tuple[int, np.ndarray]):
current_context.set(Context(webrtc_id=cast(str, self.stream_id)))
return self.stream_handler.receive(frame)
async def _emit_to_queue(self): async def _emit_to_queue(self):
try: try:
while not self.quit.is_set(): while not self.quit.is_set():
if isinstance(self.stream_handler, AsyncStreamHandler): if isinstance(self.stream_handler, AsyncStreamHandler):
output = await self.stream_handler.emit() output = await self.stream_handler.emit()
else: else:
output = await run_sync(self.stream_handler.emit) output = await run_sync(self.emit_with_context)
self.queue.put_nowait(output) self.queue.put_nowait(output)
except asyncio.CancelledError: except asyncio.CancelledError:
logger.debug("Emit loop cancelled") logger.debug("Emit loop cancelled")
@@ -270,7 +281,7 @@ class WebSocketHandler:
audio_payload = base64.b64encode(mulaw_audio).decode("utf-8") audio_payload = base64.b64encode(mulaw_audio).decode("utf-8")
if self.websocket and self.stream_id: if self.websocket and self.stream_id:
sample_rate, audio_array = frame sample_rate, audio_array = frame[:2]
duration = len(audio_array) / sample_rate duration = len(audio_array) / sample_rate
self.playing_durations.append(duration) self.playing_durations.append(duration)