Skip to content

Commit

Permalink
fix: moved dispatch of mediastream and added clean-up
Browse files Browse the repository at this point in the history
  • Loading branch information
malmen237 committed Apr 10, 2024
1 parent 471d909 commit 2cc43ad
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions src/components/production-line/use-rtc-connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,15 @@ type TEstablishConnection = {
type TAttachAudioStream = {
inputAudioStream: MediaStream;
rtcPeerConnection: RTCPeerConnection;
dispatch: Dispatch<TGlobalStateAction>;
};

const attachInputAudioToPeerConnection = ({
inputAudioStream,
rtcPeerConnection,
dispatch,
}: TAttachAudioStream) => {
inputAudioStream.getTracks().forEach((track) => {
rtcPeerConnection.addTrack(track);
});
dispatch({
type: "CONNECTED_MEDIASTREAM",
payload: inputAudioStream,
});
};

const establishConnection = ({
Expand Down Expand Up @@ -167,14 +161,28 @@ export const useRtcConnection = ({
// Teardown
useEffect(
() => () => {
if (inputAudioStream !== "no-device") {
// TODO remove check-log
console.log("Dispatch of mediastream");
dispatch({
type: "CONNECTED_MEDIASTREAM",
payload: inputAudioStream,
});
}
audioElements.forEach((el) => {
console.log("Tearing down audio element");
el.pause();
// eslint-disable-next-line no-param-reassign
el.srcObject = null;
// TODO remove check-log
console.log("Remove mediastream");
dispatch({
type: "CONNECTED_MEDIASTREAM",
payload: null,
});
});
},
[audioElements]
[audioElements, dispatch, inputAudioStream]
);

useEffect(() => {
Expand Down Expand Up @@ -204,7 +212,6 @@ export const useRtcConnection = ({
attachInputAudioToPeerConnection({
rtcPeerConnection,
inputAudioStream,
dispatch,
});
}

Expand Down

0 comments on commit 2cc43ad

Please sign in to comment.