From 7f7a13416d84c23687800efeb055adbedbb8e862 Mon Sep 17 00:00:00 2001 From: Jose B Date: Thu, 17 Aug 2023 14:35:36 -0500 Subject: [PATCH] useAudioDevice hook + permission mngmnt --- www/app/transcripts/new/page.js | 71 +++++++++++++++++++++------ www/app/transcripts/recorder.js | 43 ++++++---------- www/app/transcripts/useAudioDevice.js | 67 +++++++++++++++++++++++++ 3 files changed, 137 insertions(+), 44 deletions(-) create mode 100644 www/app/transcripts/useAudioDevice.js diff --git a/www/app/transcripts/new/page.js b/www/app/transcripts/new/page.js index 60f4642b..65ebcb90 100644 --- a/www/app/transcripts/new/page.js +++ b/www/app/transcripts/new/page.js @@ -5,6 +5,7 @@ import { Dashboard } from "../dashboard"; import useWebRTC from "../useWebRTC"; import useTranscript from "../useTranscript"; import { useWebSockets } from "../useWebSockets"; +import useAudioDevice from "../useAudioDevice"; import "../../styles/button.css"; const App = () => { @@ -24,6 +25,13 @@ const App = () => { const transcript = useTranscript(); const webRTC = useWebRTC(stream, transcript.response?.id); const webSockets = useWebSockets(transcript.response?.id); + const { + loading, + permissionOk, + audioDevices, + requestPermission, + getAudioStream, + } = useAudioDevice(); return (
@@ -32,23 +40,54 @@ const App = () => {

Capture The Signal, Not The Noise

- { - webRTC?.peer?.send(JSON.stringify({ cmd: "STOP" })); - setStream(null); - }} - /> + {permissionOk ? ( + <> + { + webRTC?.peer?.send(JSON.stringify({ cmd: "STOP" })); + setStream(null); + }} + getAudioStream={getAudioStream} + audioDevices={audioDevices} + /> -
- - + + + ) : ( + <> +
+

+ Audio Permissions +

+ {loading ? ( +

+ Checking permission... +

+ ) : ( + <> +

+ Reflector needs access to your microphone to work. +
+ Please grant permission to continue. +

+ + + )} +
+ + )} ); }; diff --git a/www/app/transcripts/recorder.js b/www/app/transcripts/recorder.js index 41284236..134373bb 100644 --- a/www/app/transcripts/recorder.js +++ b/www/app/transcripts/recorder.js @@ -15,24 +15,11 @@ const AudioInputsDropdown = (props) => { const [ddOptions, setDdOptions] = useState([]); useEffect(() => { - const init = async () => { - // Request permission to use audio inputs - await navigator.mediaDevices - .getUserMedia({ audio: true }) - .then((stream) => stream.getTracks().forEach((t) => t.stop())); - - const devices = await navigator.mediaDevices.enumerateDevices(); - const audioDevices = devices - .filter((d) => d.kind === "audioinput" && d.deviceId != "") - .map((d) => ({ value: d.deviceId, label: d.label })); - - if (audioDevices.length < 1) return console.log("no audio input devices"); - - setDdOptions(audioDevices); - props.setDeviceId(audioDevices[0].value); - }; - init(); - }, []); + setDdOptions(props.audioDevices); + props.setDeviceId( + props.audioDevices.length > 0 ? props.audioDevices[0].value : null, + ); + }, [props.audioDevices]); const handleDropdownChange = (e) => { props.setDeviceId(e.value); @@ -131,16 +118,12 @@ export default function Recorder(props) { setIsRecording(false); document.getElementById("play-btn").disabled = false; } else { - const stream = await navigator.mediaDevices.getUserMedia({ - audio: { - deviceId, - noiseSuppression: false, - echoCancellation: false, - }, - }); - await record.startRecording(stream); + const stream = await props.getAudioStream(deviceId); props.setStream(stream); - setIsRecording(true); + if (stream) { + await record.startRecording(stream); + setIsRecording(true); + } } }; @@ -158,7 +141,11 @@ export default function Recorder(props) { return (
- +