Skip to content
Snippets Groups Projects
Commit a46fd956 authored by Greg Fodor's avatar Greg Fodor
Browse files

Mic flow kind of working, but need to figure out VR mode entry not drawing UI

parent 972d5a4e
No related branches found
No related tags found
No related merge requests found
...@@ -12,11 +12,54 @@ async function grantedMicLabels() { ...@@ -12,11 +12,54 @@ async function grantedMicLabels() {
return mediaDevices.filter(d => d.label && d.kind === "audioinput").map(d => d.label); return mediaDevices.filter(d => d.label && d.kind === "audioinput").map(d => d.label);
} }
async function hasGrantedMicPermissionsFor2D() { async function hasGrantedMicPermissions() {
const micLabels = await grantedMicLabels(); const micLabels = await grantedMicLabels();
return micLabels.length > 0; return micLabels.length > 0;
} }
function stopAllTracks(mediaStream) {
for (const track of mediaStream.getAudioTracks()) {
track.stop();
}
for (const track of mediaStream.getVideoTracks()) {
track.stop();
}
}
async function getMediaStream(shareScreen, ...desiredMicRegexes) {
let mediaStream = null;
let desiredAudioDeviceIds;
const mediaStreamHasDesiredMic = () => {
if (!mediaStream || mediaStream.getAudioTracks().length == 0) return false;
if (desiredMicRegexes.length == 0) return true;
return !!(desiredMicRegexes.find(r => mediaStream.getAudioTracks()[0].label.match(r)));
};
do {
if (mediaStream) {
stopAllTracks(mediaStream);
}
const mediaDevices = await navigator.mediaDevices.enumerateDevices();
desiredAudioDeviceIds = mediaDevices.filter(d => {
return desiredMicRegexes.find(r => d.label.match(r)) && d.kind === "audioinput";
}).map(d => d.deviceId);
const constraints = {
audio: desiredAudioDeviceIds.length > 0 ? { deviceId: { exact: desiredAudioDeviceIds } } : true,
video: shareScreen ? { mediaSource: "screen", height: 720, frameRate: 30 } : false
};
mediaStream = await navigator.mediaDevices.getUserMedia(constraints);
} while (!mediaStreamHasDesiredMic() && desiredAudioDeviceIds.length > 0);
return mediaStream;
}
class UIRoot extends Component { class UIRoot extends Component {
static propTypes = { static propTypes = {
enterScene: PropTypes.func, enterScene: PropTypes.func,
...@@ -29,41 +72,52 @@ class UIRoot extends Component { ...@@ -29,41 +72,52 @@ class UIRoot extends Component {
enterInVR: false enterInVR: false
} }
promptForMedia = async () => { performDirectEntryFlow = async (enterInVR) => {
console.log("HI"); this.setState({ enterInVR });
const constraints = {
audio: true, if (enterInVR) {
video: this.state.shareScreen ? { mediaSource: "screen", height: 720, frameRate: 30 } : false document.querySelector("a-scene").enterVR();
}; }
const mediaStream = await navigator.mediaDevices.getUserMedia(constraints); const hasMic = await hasGrantedMicPermissions();
const audioTracks = mediaStream.getAudioTracks();
console.log(audioTracks);
if (audioTracks.length > 0) { console.log("A");
const selectedMic = audioTracks[0].label; if (hasMic) {
console.log("Mic: " + selectedMic); console.log("B");
// TODO if VR mode is desired here, look at the VR displays to see if we have the right mic await this.getMediaStreamAndEnterScene();
// TODO: this.props.enterScene(mediaStream);
this.setState({ entryStep: ENTRY_STEPS.finished });
} else { } else {
// TODO mic not granted this.setState({ entryStep: ENTRY_STEPS.mic_check });
} }
} }
enter2D = async () => { enter2D = async () => {
const hasMic = await hasGrantedMicPermissionsFor2D(); await this.performDirectEntryFlow(false);
}
if (hasMic) { enterVR = async () => {
await this.promptForMedia(); await this.performDirectEntryFlow(true);
this.setState({ entryStep: ENTRY_STEPS.finished, enter_in_vr: false }); }
} else {
this.setState({ entryStep: ENTRY_STEPS.mic_check, enter_in_vr: false }); getMediaStreamAndEnterScene = async (...desiredMicRegexes) => {
const preStreamAcquisitionTime = new Date();
const mediaStream = await getMediaStream(this.state.shareScreen, ...desiredMicRegexes);
if (mediaStream) {
if (mediaStream.getAudioTracks().length > 0) {
console.log(`Using microphone: ${mediaStream.getAudioTracks()[0].label}`)
}
if (mediaStream.getVideoTracks().length > 0) {
console.log('Screen sharing enabled.')
}
} }
this.setState({ entryStep: ENTRY_STEPS.finished });
this.props.enterScene(mediaStream);
} }
enterVR = () => { onMicActivateButtonClicked = async () => {
this.setState({ entryStep: ENTRY_STEPS.mic_check, enter_in_vr: true }); await this.getMediaStreamAndEnterScene();
} }
componentDidMount = () => { componentDidMount = () => {
...@@ -86,7 +140,7 @@ class UIRoot extends Component { ...@@ -86,7 +140,7 @@ class UIRoot extends Component {
const micPanel = this.state.entryStep === ENTRY_STEPS.mic_check const micPanel = this.state.entryStep === ENTRY_STEPS.mic_check
? ( ? (
<div> <div>
<button onClick={this.promptForMedia}> <button onClick={this.onMicActivateButtonClicked}>
Choose Mic Choose Mic
</button> </button>
</div> </div>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment