1
0

Added dummy audio/video track if no media access

Signed-off-by: James Ketrenos <james_eikona@ketrenos.com>
This commit is contained in:
James Ketrenos 2022-03-19 10:24:39 -07:00
parent 3bbff22ec2
commit eea8b1acce

View File

@ -392,37 +392,102 @@ const MediaAgent = ({setPeers}) => {
* attach it to an <audio> or <video> tag if they give us access. */
console.log("media-agent - Requesting access to local audio / video inputs");
/* See Dummy Tracks for more ideas...
https://blog.mozilla.org/webrtc/warm-up-with-replacetrack/
*/
navigator.getUserMedia = (navigator.getUserMedia ||
navigator.webkitGetUserMedia ||
navigator.mozGetUserMedia ||
navigator.msGetUserMedia);
return navigator.mediaDevices
.getUserMedia({audio: true, video: true})
.getUserMedia({audio: true, video: true})
.then((media) => {
return {
media: media,
audio: true,
video: true
};
})
.catch((error) => {
console.log(`media-agent - Access granted to audio and video ` +
console.log(`media-agent - Access to audio and video ` +
`failed. Trying just audio.`);
return navigator.mediaDevices
.getUserMedia({ audio: true, video: false })
})
.then((media) => { /* user accepted access to a/v */
console.log("media-agent - Access granted to audio/video");
media.getVideoTracks().forEach((track) => {
track.applyConstraints({
"video": {
"width": {
"min": 160,
"max": 320
},
"height": {
"min": 120,
"max": 240
}
}
});
.getUserMedia({ audio: true, video: false })
.then((media) => {
return {
media: media,
audio: true,
video: false
};
})
.catch((error) => {
console.log(`media-agent - Access to audio ` +
`failed.`);
return {
media: undefined,
audio: false,
video: false
};
});
})
.then((context) => { /* user accepted access to a/v */
if (context.video) {
console.log("media-agent - Access granted to audio/video");
context.media.getVideoTracks().forEach((track) => {
track.applyConstraints({
"video": {
"width": {
"min": 160,
"max": 320
},
"height": {
"min": 120,
"max": 240
}
}
});
});
return context.media;
}
return media;
const black = ({ width = 640, height = 480 } = {}) => {
const canvas = Object.assign(document.createElement("canvas"), {
width, height
});
canvas.getContext('2d').fillRect(0, 0, width, height);
const stream = canvas.captureStream();
return Object.assign(stream.getVideoTracks()[0], {
enabled: true
});
}
const silence = () => {
const ctx = new AudioContext(), oscillator = ctx.createOscillator();
const dst = oscillator.connect(ctx.createMediaStreamDestination());
oscillator.start();
return Object.assign(dst.stream.getAudioTracks()[0], {
enabled: true
});
}
if (context.audio) {
console.log("media-agent - Access granted to audio");
let black = ({ width = 640, height = 480 } = {}) => {
let canvas = Object.assign(document.createElement("canvas"), {
width, height
});
canvas.getContext('2d').fillRect(0, 0, width, height);
let stream = canvas.captureStream();
return Object.assign(stream.getVideoTracks()[0], {
enabled: true
});
}
return new MediaStream([context.media, black()]);
}
return new MediaStream([black(), silence()]);
});
};
@ -431,7 +496,7 @@ const MediaAgent = ({setPeers}) => {
if (debug) console.log(`media-agent - WebSocket open request. Attempting to create local media.`);
setup_local_media().then((media) => {
/* once the user has given us access to their
* microphone/camcorder, join the channel and start peering up */
* microphone/camcorder, join the channel and start peering up */
if (abort) {
console.log(`media-agent - aborting setting local media`);
} else {