Skip to content

Commit

Permalink
feat: 마이크를 켠 스트림만 탐지하도록 개선
Browse files Browse the repository at this point in the history
  • Loading branch information
simeunseo committed Dec 2, 2024
1 parent 3e8d4ce commit a134689
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions apps/web/src/hooks/mediasoup/useAudioLevelDetector.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { useEffect, useRef, useState } from 'react';
import { client } from '@repo/mediasoup';

import { useRemoteStreamState } from '@/contexts/remoteStream/context';

interface AudioLevelData {
socketId: string;
audioLevel: number;
Expand All @@ -9,6 +11,8 @@ interface AudioLevelData {
}

const useAudioLevelDetector = () => {
const { audioStreams } = useRemoteStreamState();

const audioContextRef = useRef<AudioContext | null>(null);
const audioLevelsRef = useRef<AudioLevelData[]>([]);

Expand All @@ -22,12 +26,20 @@ const useAudioLevelDetector = () => {
const SPEECH_END_DELAY = 1000;

const detectAudioLevels = () => {
const audioLevels = audioLevelsRef.current;
const unmutedStreamIds = new Set(
audioStreams.filter((stream) => !stream.paused).map((stream) => stream.socketId)
);

const unmutedAudioLevels = audioLevelsRef.current.filter((data) =>
unmutedStreamIds.has(data.socketId)
);

if (!unmutedAudioLevels.length) return;

let maxLevel = 0;
let maxLevelSocketId = null;

audioLevels.forEach((levelData) => {
unmutedAudioLevels.forEach((levelData) => {
const { analyser, dataArray, socketId } = levelData;
analyser.getFloatTimeDomainData(dataArray);

Expand Down

0 comments on commit a134689

Please sign in to comment.