Skip to content

Commit

Permalink
Merge pull request #143 from SWM-FIRE/FIRE-528-미디어-기기-설정-기능-구현
Browse files Browse the repository at this point in the history
FIRE-528-미디어-기기-설정-기능-구현
  • Loading branch information
071yoon authored Aug 30, 2022
2 parents afc422d + cd8758e commit ade219a
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 9 deletions.
9 changes: 9 additions & 0 deletions src/components/ready/modal/SettingModal.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import React, { useRef, useEffect } from 'react';
import styled from 'styled-components';
import Selectors from './Selectors';
import UserMediaStreamStore from '../../../stores/userMediaStreamStore';
import { useCreateMediaStream } from '../../../hooks/useCreateMediaStream';
import { ReactComponent as X } from '../../../assets/svg/X.svg';

export default function SettingModal({
Expand All @@ -10,6 +12,13 @@ export default function SettingModal({
setSetting: React.Dispatch<React.SetStateAction<boolean>>;
stream: MediaStream;
}) {
const { userAudioInputDevice } = UserMediaStreamStore();
const { replaceAudioStream } = useCreateMediaStream();

useEffect(() => {
replaceAudioStream();
}, [userAudioInputDevice]);

const closeModal = (event: React.MouseEvent<HTMLButtonElement>) => {
event.preventDefault();
setSetting(false);
Expand Down
2 changes: 1 addition & 1 deletion src/components/room/header/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useNavigate, useParams } from 'react-router-dom';
import Timer from './Timer';
import Settings from './Settings';
import { ReactComponent as X } from '../../../assets/svg/X.svg';
import Theme from '../Theme';
import Theme from './Theme';
import roomSocket from '../../../adapters/roomSocket';
import connectedUsersStore from '../../../stores/connectedUsersStore';
import messageStore from '../../../stores/messagesStore';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { useState, useRef, useEffect } from 'react';
import styled from 'styled-components';
import MovingTheme from './screenShare/MovingTheme';
import { ReactComponent as VolumeOn } from '../../assets/svg/VolumeOn.svg';
import { ReactComponent as VolumeOff } from '../../assets/svg/VolumeOff.svg';
import UserMediaStreamStore from '../../stores/userMediaStreamStore';
import ThemeSound from './header/ThemeSound';
import MovingTheme from '../screenShare/MovingTheme';
import { ReactComponent as VolumeOn } from '../../../assets/svg/VolumeOn.svg';
import { ReactComponent as VolumeOff } from '../../../assets/svg/VolumeOff.svg';
import UserMediaStreamStore from '../../../stores/userMediaStreamStore';
import ThemeSound from './ThemeSound';

export default function Theme({ theme }) {
const { userSpeaker, setUserSpeaker } = UserMediaStreamStore();
const { userSpeaker, setUserSpeaker, userAudioOutputDevice } =
UserMediaStreamStore();
const [volume, setVolume] = useState<number>(0.5);
const volumeRef = useRef<HTMLAudioElement>(null);
const setSpeaker = () => {
Expand All @@ -21,7 +22,9 @@ export default function Theme({ theme }) {
if (volumeRef.current) {
volumeRef.current.volume = volume;
}
}, [volume, userSpeaker]);
const newAudio = volumeRef.current;
(newAudio as any).setSinkId(userAudioOutputDevice?.deviceId);
}, [volume, userSpeaker, userAudioOutputDevice]);

return (
<Container>
Expand Down
6 changes: 5 additions & 1 deletion src/components/room/screenShare/SingleScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import MyAvatar from '../../../assets/avatar/MyAvatar';
import controlModal from '../../../stores/controlModal';
import messageStore from '../../../stores/messagesStore';
import userStore from '../../../stores/userStore';
import UserMediaStreamStore from '../../../stores/userMediaStreamStore';

export default function SingleScreen({ connectedUser, stream }) {
const { messages } = messageStore();
Expand All @@ -12,6 +13,7 @@ export default function SingleScreen({ connectedUser, stream }) {
controlModal();
const { uid } = userStore();
const videoRef = useRef<HTMLVideoElement | null>(null);
const { userAudioOutputDevice } = UserMediaStreamStore();

const OpenModal = () => {
setNickname(connectedUser.nickname);
Expand All @@ -25,7 +27,9 @@ export default function SingleScreen({ connectedUser, stream }) {
if (videoRef.current && stream) {
videoRef.current.srcObject = stream;
}
}, [stream, videoRef]);
const newRef = videoRef.current;
(newRef as any).setSinkId(userAudioOutputDevice?.deviceId);
}, [stream, videoRef, userAudioOutputDevice]);

useEffect(() => {
const timer = setInterval(() => {
Expand Down
34 changes: 34 additions & 0 deletions src/hooks/useCreateMediaStream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export const useCreateMediaStream = () => {
setUserMic,
setUserVideo,
userVideo,
userAudioInputDevice,
} = UserMediaStreamStore();
const myStream: { localStream: MediaStream | null } = {
localStream: userMediaStream,
Expand Down Expand Up @@ -79,6 +80,38 @@ export const useCreateMediaStream = () => {
}
};

const replaceAudioStream = async () => {
try {
const audioStream = await navigator.mediaDevices.getUserMedia({
audio: {
deviceId: userAudioInputDevice?.deviceId,
autoGainControl: false,
channelCount: 2,
echoCancellation: true,
latency: 0,
noiseSuppression: false,
sampleRate: 48000,
sampleSize: 16,
},
});
myStream.localStream.removeTrack(
myStream.localStream.getAudioTracks()[0],
);
myStream.localStream.addTrack(audioStream.getAudioTracks()[0]);
setUserMediaStream(myStream.localStream);
Object.keys(pcs).forEach((pc) => {
const sender = pcs[pc]
.getSenders()
.find((s) => s.track.kind === 'audio');
sender.replaceTrack(audioStream.getAudioTracks()[0]);
});
} catch (error) {
console.log('failed to change audio stream', error);
const stream = new MediaStream();
setUserMediaStream(stream);
}
};

const createDisplayStream = async () => {
try {
const videoStream = await navigator.mediaDevices.getDisplayMedia({
Expand Down Expand Up @@ -110,6 +143,7 @@ export const useCreateMediaStream = () => {
};

return {
replaceAudioStream,
toggleMic,
createDisplayStream,
createAudioStream,
Expand Down

0 comments on commit ade219a

Please sign in to comment.