Close Stream from Backend (#222)

* Close from backend

* Add code
This commit is contained in:
Freddy Boulton
2025-03-28 20:47:34 -04:00
committed by GitHub
parent 71743acb64
commit 8ed27fba78
10 changed files with 2336 additions and 2237 deletions

View File

@@ -51,12 +51,16 @@
}
});
let _on_change_cb = (msg: "change" | "tick" | "stopword") => {
let _on_change_cb = (msg: "change" | "tick" | "stopword" | any) => {
if (msg === "stopword") {
stopword_recognized = true;
setTimeout(() => {
stopword_recognized = false;
}, 3000);
} else if (msg.type === "end_stream") {
stream_state = "closed";
stop(pc);
on_change_cb(msg);
} else {
console.debug("calling on_change_cb with msg", msg);
on_change_cb(msg);

View File

@@ -29,6 +29,17 @@
let pc: RTCPeerConnection;
let _webrtc_id = Math.random().toString(36).substring(2);
let _on_change_cb = (msg: "change" | "tick" | "stopword" | any) => {
if (msg.type === "end_stream") {
on_change_cb(msg);
stream_state = "closed";
stop(pc);
} else {
console.debug("calling on_change_cb with msg", msg);
on_change_cb(msg);
}
};
const dispatch = createEventDispatcher<{
tick: undefined;
error: string;
@@ -75,7 +86,7 @@
server.offer,
_webrtc_id,
"audio",
on_change_cb,
_on_change_cb,
)
.then((connection) => {
clearTimeout(timeoutId);

View File

@@ -25,6 +25,17 @@
tick: undefined;
}>();
let _on_change_cb = (msg: "change" | "tick" | "stopword" | any) => {
if (msg.type === "end_stream") {
on_change_cb(msg);
stream_state = "closed";
stop(pc);
} else {
console.debug("calling on_change_cb with msg", msg);
on_change_cb(msg);
}
};
let stream_state = "closed";
$: if (value === "start_webrtc_stream") {
@@ -62,7 +73,7 @@
server.offer,
_webrtc_id,
"video",
on_change_cb,
_on_change_cb,
)
.then((connection) => {
clearTimeout(timeoutId);

View File

@@ -124,6 +124,18 @@
}
}
let _on_change_cb = (msg: "change" | "tick" | "stopword" | any) => {
if (msg.type === "end_stream") {
on_change_cb(msg);
stream_state = "closed";
stop(pc);
access_webcam();
} else {
console.debug("calling on_change_cb with msg", msg);
on_change_cb(msg);
}
};
let recording = false;
let stream: MediaStream;
@@ -171,7 +183,7 @@
server.offer,
webrtc_id,
"video",
on_change_cb,
_on_change_cb,
rtp_params,
undefined,
reject_cb,

View File

@@ -80,7 +80,8 @@ export async function start(
event_json?.type === "error" ||
event_json?.type === "send_input" ||
event_json?.type === "fetch_output" ||
event_json?.type === "stopword"
event_json?.type === "stopword" ||
event_json?.type === "end_stream"
) {
on_change_cb(event_json ?? event.data);
}