Skip to content

Commit

Permalink
[mirotalkbro] - #10 fix
Browse files Browse the repository at this point in the history
  • Loading branch information
miroslavpejic85 committed Jan 20, 2025
1 parent ba8bc08 commit 3cb5642
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 44 deletions.
74 changes: 39 additions & 35 deletions public/js/broadcast.js
Original file line number Diff line number Diff line change
Expand Up @@ -1023,48 +1023,52 @@ videoSelect.onchange = getStream;
getStream().then(getDevices).then(gotDevices);

function getStream() {
videoOff.style.visibility = 'hidden';

videoQualitySelect.selectedIndex = localStorage.videoQualitySelectedIndex
? localStorage.videoQualitySelectedIndex
: 0;
videoFpsSelect.selectedIndex = localStorage.videoFpsSelectedIndex ? localStorage.videoFpsSelectedIndex : 0;
videoBtn.style.color = getMode === 'dark' ? 'white' : 'black';
try {
videoOff.style.visibility = 'hidden';

videoQualitySelect.selectedIndex = localStorage.videoQualitySelectedIndex
? localStorage.videoQualitySelectedIndex
: 0;
videoFpsSelect.selectedIndex = localStorage.videoFpsSelectedIndex ? localStorage.videoFpsSelectedIndex : 0;
videoBtn.style.color = getMode === 'dark' ? 'white' : 'black';

const audioSource = audioSelect.value;
const videoSource = videoSelect.value;

const screenConstraints = { audio: true, video: true };
const cameraConstraints = {
audio: { deviceId: audioSource ? { exact: audioSource } : undefined },
video: { deviceId: videoSource ? { exact: videoSource } : undefined },
};
const constraints = screenShareEnabled ? screenConstraints : cameraConstraints;

if (screenShareEnabled) {
stopVideoTrack(broadcastStream);

video.classList.remove('mirror');
isVideoMirrored = false;
return navigator.mediaDevices
.getDisplayMedia(constraints)
.then(gotScreenStream)
.then(applyVideoConstraints)
.catch(handleMediaDeviceError);
}

const audioSource = audioSelect.value;
const videoSource = videoSelect.value;
if (isDesktopDevice && !isVideoMirrored) {
video.className = 'mirror';
isVideoMirrored = true;
}

const screenConstraints = { audio: true, video: true };
const cameraConstraints = {
audio: { deviceId: audioSource ? { exact: audioSource } : undefined },
video: { deviceId: videoSource ? { exact: videoSource } : undefined },
};
const constraints = screenShareEnabled ? screenConstraints : cameraConstraints;
stopTracks(broadcastStream);

if (screenShareEnabled) {
stopVideoTrack(broadcastStream);

video.classList.remove('mirror');
isVideoMirrored = false;
return navigator.mediaDevices
.getDisplayMedia(constraints)
.then(gotScreenStream)
.getUserMedia(constraints)
.then(gotStream)
.then(applyVideoConstraints)
.catch(handleMediaDeviceError);
} catch (error) {
handleMediaDeviceError(error);
}

if (isDesktopDevice && !isVideoMirrored) {
video.className = 'mirror';
isVideoMirrored = true;
}

stopTracks(broadcastStream);

return navigator.mediaDevices
.getUserMedia(constraints)
.then(gotStream)
.then(applyVideoConstraints)
.catch(handleMediaDeviceError);
}

function gotStream(stream) {
Expand Down
2 changes: 1 addition & 1 deletion public/js/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ function handleMediaStreamError(mediaStreamType, error) {
<li>Media type: ${mediaStreamType}</li>
<li>Error name: ${error.name}</li>
<li>
<p>Error message:</p>
Error message:
<p style="color: red">${errorMessage}</p>
</li>`;

Expand Down
17 changes: 9 additions & 8 deletions public/js/viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -395,17 +395,18 @@ function attachStream(stream) {
}

async function getStream() {
return navigator.mediaDevices
.getUserMedia({
try {
const stream = await navigator.mediaDevices.getUserMedia({
video: viewerSettings.buttons.video,
audio: viewerSettings.buttons.audio,
})
.catch((error) => {
console.error('Error accessing media devices', error.message);
handleMediaStreamError('Audio/Video', error);
hideVideoAudioButtons();
return null;
});
return stream;
} catch (error) {
console.error('Failed to access media devices:', error.message);
handleMediaStreamError('Audio/Video', error);
hideVideoAudioButtons();
return null;
}
}

function hideVideoAudioButtons() {
Expand Down

0 comments on commit 3cb5642

Please sign in to comment.