mirror of
https://github.com/HumanAIGC-Engineering/gradio-webrtc.git
synced 2026-02-04 17:39:23 +08:00
Add code (#276)
This commit is contained in:
@@ -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;
|
||||||
|
|||||||
@@ -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)
|
||||||
|
|||||||
Reference in New Issue
Block a user