Skip to content

Commit

Permalink
revert voice changes
Browse files Browse the repository at this point in the history
  • Loading branch information
SupertigerDev committed Oct 15, 2024
1 parent 11eefb7 commit b0ffb64
Showing 1 changed file with 41 additions and 30 deletions.
71 changes: 41 additions & 30 deletions src/chat-api/store/useVoiceUsers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export type VoiceUser = RawVoice & {
videoStream?: MediaStream;
vad?: VADInstance;
voiceActivity: boolean;
audio?: HTMLAudioElement
audio?: HTMLAudioElement;

waitingForVideoStreamId?: string;
waitingForAudioStreamId?: string;
Expand Down Expand Up @@ -98,7 +98,6 @@ async function addPeer(this: VoiceUser, signal: SimplePeer.SignalData) {
const { default: LazySimplePeer } = await import("@thaunknown/simple-peer");
const turnServer = await postGenerateCredential();


const peer = new LazySimplePeer({
initiator: false,
trickle: true,
Expand Down Expand Up @@ -133,7 +132,9 @@ async function addPeer(this: VoiceUser, signal: SimplePeer.SignalData) {
},
],
},
streams: []
streams: [localStreams.audioStream, localStreams.videoStream].filter(
(stream) => stream
) as MediaStream[],
});

peer.on("signal", (signal) => {
Expand All @@ -147,7 +148,6 @@ async function addPeer(this: VoiceUser, signal: SimplePeer.SignalData) {

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

});
peer.on("connect", () => {
console.log("connect");
Expand Down Expand Up @@ -209,7 +209,6 @@ export async function createPeer(voiceUser: VoiceUser | RawVoice) {
const { default: LazySimplePeer } = await import("@thaunknown/simple-peer");
const turnServer = await postGenerateCredential();


const peer = new LazySimplePeer({
initiator: true,
trickle: true,
Expand Down Expand Up @@ -244,7 +243,9 @@ export async function createPeer(voiceUser: VoiceUser | RawVoice) {
},
],
},
streams: []
streams: [localStreams.audioStream, localStreams.videoStream].filter(
(stream) => stream
) as MediaStream[],
});

peer.on("signal", (signal) => {
Expand All @@ -260,12 +261,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");
}
// 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 @@ -322,29 +323,40 @@ function setVAD(stream: MediaStream, voiceUser: RawVoice) {
setVoiceUsers(voiceUser.channelId, voiceUser.userId, { vad: vadInstance });
}

const onData = (rawVoice: RawVoice, data?: { type: "video" | "audio", streamId: string }) => {
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, {
...(data.type === "audio" ? { waitingForAudioStreamId: data.streamId } : {}),
...(data.type === "video" ? { waitingForVideoStreamId: data.streamId } : {}),
...(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.waitingForAudioStreamId && !voiceUser.waitingForVideoStreamId) return;
const onStream = (voiceUser: RawVoice, stream: MediaStream) => {
// const voiceUser = getVoiceUser(rawVoiceUser.channelId, rawVoiceUser.userId);
// if (!voiceUser) return;
// if (!voiceUser.waitingForAudioStreamId && !voiceUser.waitingForVideoStreamId) return;

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

const streamType = voiceUser.waitingForAudioStreamId === stream.id ? "audioStream" : "videoStream";
const videoTracks = stream.getVideoTracks();
const streamType = videoTracks.length ? "videoStream" : "audioStream";

stream.onremovetrack = () => {
setVoiceUsers(voiceUser.channelId, voiceUser.userId, {
[streamType]: null,
...(streamType === "audioStream" ? { audio: mic, voiceActivity: false, } : {}),
...(streamType === "audioStream"
? { audio: mic, voiceActivity: false }
: {}),
});
stream.onremovetrack = null;
};
Expand All @@ -363,8 +375,6 @@ const onStream = (rawVoiceUser: RawVoice, stream: MediaStream) => {
setVoiceUsers(voiceUser.channelId, voiceUser.userId, {
[streamType]: stream,
...(streamType === "audioStream" ? { audio: mic } : {}),
waitingForStreamId: undefined,
waitingForStreamType: undefined,
});
};

Expand Down Expand Up @@ -438,10 +448,12 @@ 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?.write(
// jsonToUint8Array({
// type,
// streamId: stream.id,
// })
// );
voiceUser?.peer?.addStream(stream);
}
};
Expand Down Expand Up @@ -533,8 +545,7 @@ function jsonToUint8Array<T extends object>(json: T) {
function Uint8ArrayToJson(array: Uint8Array) {
try {
return JSON.parse(new TextDecoder().decode(array));
}
catch {
} catch {
return null;
}
}
}

0 comments on commit b0ffb64

Please sign in to comment.