Skip to content

Commit eb56f62

Browse files
committed
fix: enumerate devices working fine now
1 parent 1b836e5 commit eb56f62

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

packages/frontend/utils/hooks.js renamed to packages/frontend/hooks/useGetDevices.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,22 @@ export default function useGetDevices() {
1212
return;
1313
}
1414
try {
15+
const cameraPermission = await navigator.permissions.query({ name: "camera" });
16+
const micPermission = await navigator.permissions.query({ name: "microphone" });
17+
//toggleLoader()
18+
if (cameraPermission.state !== "granted" || micPermission.state !== "granted") {
19+
const stream = await navigator.mediaDevices.getUserMedia({
20+
video: true,
21+
audio: true,
22+
});
23+
if (!stream) {
24+
return;
25+
}
26+
stream.getVideoTracks().forEach((x) => x.stop());
27+
stream.getAudioTracks().forEach((x) => x.stop());
28+
}
1529
const devices = await navigator.mediaDevices.enumerateDevices();
30+
//toggleLoader()
1631
defaultDevices = devices.reduce((acc, device) => {
1732
if (device.kind === "videoinput") {
1833
acc.video.push(device);

packages/frontend/pages/index.jsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import usePageVisibility from "../hooks/pageVisibility";
66
import { classNames } from "../utils/classNames";
77
import { text } from "../utils/constants";
88
import { capitalize, getBrowserName } from "../utils/helpers";
9-
import useGetDevices from "../utils/hooks";
9+
import useGetDevices from "../hooks/useGetDevices";
1010
export async function getStaticProps() {
1111
return {
1212
props: {},
@@ -137,7 +137,7 @@ export default function Vlog() {
137137
<form className="w-full mx-auto md:w-1/4" onSubmit={handleFormSubmit}>
138138
{!isInitialized && (
139139
<>
140-
{devices.audio.length && (
140+
{devices.audio.length ? (
141141
<div className="flex flex-col">
142142
<label className="w-full font-semibold" htmlFor="audioDevice">
143143
Select audio input
@@ -168,8 +168,8 @@ export default function Vlog() {
168168
</select>
169169
</div>
170170
</div>
171-
)}
172-
{devices.video.length && (
171+
) : null}
172+
{devices.video.length ? (
173173
<div className="flex flex-col">
174174
<label className="w-full font-semibold" htmlFor="videoDevice">
175175
Select video input
@@ -200,7 +200,7 @@ export default function Vlog() {
200200
</select>
201201
</div>
202202
</div>
203-
)}
203+
) : null}
204204
<button
205205
type="submit"
206206
className="flex-grow-0 w-full px-4 py-2 mt-5 font-bold text-white transition bg-green-500 rounded hover:bg-green-700"

0 commit comments

Comments
 (0)