Skip to content

Commit

Permalink
fix bugs with screenshare not sending sometimes (hopefully xd)
Browse files Browse the repository at this point in the history
  • Loading branch information
SupertigerDev committed Oct 15, 2024
1 parent 127c0af commit 97efbc7
Showing 1 changed file with 48 additions and 5 deletions.
53 changes: 48 additions & 5 deletions src/chat-api/store/useVoiceUsers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ export type VoiceUser = RawVoice & {
vad?: VADInstance;
voiceActivity: boolean;
audio?: HTMLAudioElement

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

// voiceUsers[channelId][userId] = VoiceUser
Expand Down Expand Up @@ -144,11 +147,15 @@ async function addPeer(this: VoiceUser, signal: SimplePeer.SignalData) {
onStream(this, stream);
});

peer.on("data", (chunk: Uint8Array) => {
onData(this, Uint8ArrayToJson(chunk));

});
peer.on("connect", () => {
console.log("connect");
});
peer.on("end", () => {
user.username, console.log("end");
console.log(user.username + " peer removed");
});
peer.on("error", (err) => {
console.log(err);
Expand Down Expand Up @@ -246,7 +253,9 @@ export async function createPeer(voiceUser: VoiceUser | RawVoice) {
console.log("stream");
onStream(voiceUser, stream);
});

peer.on("data", (chunk: Uint8Array) => {
onData(voiceUser, Uint8ArrayToJson(chunk));
});
peer.on("connect", () => {
console.log("connect");
});
Expand Down Expand Up @@ -305,9 +314,24 @@ function setVAD(stream: MediaStream, voiceUser: RawVoice) {
setVoiceUsers(voiceUser.channelId, voiceUser.userId, { vad: vadInstance });
}

const onStream = (voiceUser: VoiceUser | RawVoice, stream: MediaStream) => {
const videoTracks = stream.getVideoTracks();
const streamType = videoTracks.length ? "videoStream" : "audioStream";
const onData = (rawVoice: RawVoice, data?: { type: "video" | "audio", streamId: string }) => {
if (!data?.type || !data?.streamId) return;
const voiceUser = getVoiceUser(rawVoice.channelId, rawVoice.userId);
if (!voiceUser) return;

setVoiceUsers(voiceUser.channelId, voiceUser.userId, {
waitingForStreamId: data.streamId,
waitingForStreamType: data.type,
});
};

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

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

stream.onremovetrack = () => {
setVoiceUsers(voiceUser.channelId, voiceUser.userId, {
Expand All @@ -331,6 +355,8 @@ const onStream = (voiceUser: VoiceUser | RawVoice, stream: MediaStream) => {
setVoiceUsers(voiceUser.channelId, voiceUser.userId, {
[streamType]: stream,
...(streamType === "audioStream" ? { audio: mic } : {}),
waitingForStreamId: undefined,
waitingForStreamType: undefined,
});
};

Expand Down Expand Up @@ -404,6 +430,10 @@ const sendStreamToPeer = (stream: MediaStream, type: "audio" | "video") => {
const voiceUsers = getVoiceUsers(currentVoiceChannelId()!);
for (let i = 0; i < voiceUsers.length; i++) {
const voiceUser = voiceUsers[i];
voiceUser?.peer?.write(jsonToUint8Array({
type,
streamId: stream.id,
}));
voiceUser?.peer?.addStream(stream);
}
};
Expand Down Expand Up @@ -487,3 +517,16 @@ export default function useVoiceUsers() {
localStreams,
};
}

function jsonToUint8Array<T extends object>(json: T) {
return new TextEncoder().encode(JSON.stringify(json));
}

function Uint8ArrayToJson(array: Uint8Array) {
try {
return JSON.parse(new TextDecoder().decode(array));
}
catch {
return null;
}
}

0 comments on commit 97efbc7

Please sign in to comment.