Skip to content

Commit

Permalink
fix InvalidStateError on appendBuffer
Browse files Browse the repository at this point in the history
  • Loading branch information
sainak committed Aug 30, 2024
1 parent 7a74418 commit fd9e4c6
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions src/Components/CameraFeed/useMSEplayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,7 @@ export const useMSEMediaPlayer = ({ videoEl }: UseMSEMediaPlayerOption) => {
try {
if (!videoEl || !url) return;

if (wsRef.current) {
wsRef.current.close();
}

//https://developer.mozilla.org/en-US/docs/Web/API/MediaSource
let mse: MediaSource;
if (typeof ManagedMediaSource !== "undefined") {
mse = new ManagedMediaSource();
Expand All @@ -71,10 +68,10 @@ export const useMSEMediaPlayer = ({ videoEl }: UseMSEMediaPlayerOption) => {
mse = new MediaSource();
videoEl.src = URL.createObjectURL(mse);
}

let ws: WebSocket | null = null;
mse.onsourceopen = function () {
wsRef.current = new WebSocket(url);
const ws = wsRef.current;
ws = new WebSocket(url);
wsRef.current = ws;
ws.binaryType = "arraybuffer";
ws.onopen = (_) => onSuccess && onSuccess(undefined);
ws.onerror = (event) => onError && onError(event);
Expand All @@ -83,7 +80,7 @@ export const useMSEMediaPlayer = ({ videoEl }: UseMSEMediaPlayerOption) => {
if (+data[0] === 9) {
const mimeCodec = new TextDecoder("utf-8").decode(data.slice(1));
try {
//https://developer.mozilla.org/en-US/docs/Web/API/MediaSource
//https://developer.mozilla.org/en-US/docs/Web/API/SourceBuffer
mseSourceBuffer = mse.addSourceBuffer(
`video/mp4; codecs="${mimeCodec}"`,
);
Expand All @@ -96,12 +93,14 @@ export const useMSEMediaPlayer = ({ videoEl }: UseMSEMediaPlayerOption) => {
mseSourceBuffer.onupdateend = pushPacket;
}
// switch to readPacket after creating SourceBuffer
ws.onmessage = readPacket;
ws!.onmessage = readPacket;
} else {
readPacket(event);
}
};
};
mse.onsourceended = () => ws?.close();
mse.onsourceclose = () => ws?.close();
} catch (e) {
onError && onError(e);
}
Expand Down

0 comments on commit fd9e4c6

Please sign in to comment.