Skip to content

Commit

Permalink
fix vc bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
SupertigerDev committed Oct 15, 2024
1 parent 97efbc7 commit 56cfa58
Showing 1 changed file with 21 additions and 13 deletions.
34 changes: 21 additions & 13 deletions src/chat-api/store/useVoiceUsers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ export type VoiceUser = RawVoice & {
voiceActivity: boolean;
audio?: HTMLAudioElement

waitingForStreamId?: string;
waitingForStreamType?: "video" | "audio";
waitingForVideoStreamId?: string;
waitingForAudioStreamId?: string;
};

// voiceUsers[channelId][userId] = VoiceUser
Expand Down Expand Up @@ -133,9 +133,7 @@ async function addPeer(this: VoiceUser, signal: SimplePeer.SignalData) {
},
],
},
streams: [localStreams.audioStream, localStreams.videoStream].filter(
(stream) => stream
) as MediaStream[],
streams: []
});

peer.on("signal", (signal) => {
Expand All @@ -153,6 +151,12 @@ async function addPeer(this: VoiceUser, signal: SimplePeer.SignalData) {
});
peer.on("connect", () => {
console.log("connect");
if (localStreams.audioStream) {
sendStreamToPeer(localStreams.audioStream, "audio");
}
if (localStreams.videoStream) {
sendStreamToPeer(localStreams.videoStream, "video");
}
});
peer.on("end", () => {
console.log(user.username + " peer removed");
Expand Down Expand Up @@ -240,9 +244,7 @@ export async function createPeer(voiceUser: VoiceUser | RawVoice) {
},
],
},
streams: [localStreams.audioStream, localStreams.videoStream].filter(
(stream) => stream
) as MediaStream[],
streams: []
});

peer.on("signal", (signal) => {
Expand All @@ -258,6 +260,12 @@ export async function createPeer(voiceUser: VoiceUser | RawVoice) {
});
peer.on("connect", () => {
console.log("connect");
if (localStreams.audioStream) {
sendStreamToPeer(localStreams.audioStream, "audio");
}
if (localStreams.videoStream) {
sendStreamToPeer(localStreams.videoStream, "video");
}
});
peer.on("end", () => {
console.log(user.username, "end");
Expand Down Expand Up @@ -320,18 +328,18 @@ const onData = (rawVoice: RawVoice, data?: { type: "video" | "audio", streamId:
if (!voiceUser) return;

setVoiceUsers(voiceUser.channelId, voiceUser.userId, {
waitingForStreamId: data.streamId,
waitingForStreamType: data.type,
...(data.type === "audio" ? { waitingForAudioStreamId: data.streamId } : {}),
...(data.type === "video" ? { waitingForVideoStreamId: data.streamId } : {}),
});
};

const onStream = (rawVoiceUser: RawVoice, stream: MediaStream) => {
const voiceUser = getVoiceUser(rawVoiceUser.channelId, rawVoiceUser.userId);
if (!voiceUser) return;
if (!voiceUser.waitingForStreamId) return;
if (!voiceUser.waitingForStreamType) return;
if (!voiceUser.waitingForAudioStreamId && !voiceUser.waitingForVideoStreamId) return;


const streamType = voiceUser.waitingForStreamType === "video" ? "videoStream" : "audioStream";
const streamType = voiceUser.waitingForAudioStreamId === stream.id ? "audioStream" : "videoStream";

stream.onremovetrack = () => {
setVoiceUsers(voiceUser.channelId, voiceUser.userId, {
Expand Down

0 comments on commit 56cfa58

Please sign in to comment.