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,6 +392,9 @@ const MediaAgent = ({setPeers}) => {
* attach it to an <audio> or <video> tag if they give us access. */ * attach it to an <audio> or <video> tag if they give us access. */
console.log("media-agent - Requesting access to local audio / video inputs"); 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.getUserMedia = (navigator.getUserMedia ||
navigator.webkitGetUserMedia || navigator.webkitGetUserMedia ||
navigator.mozGetUserMedia || navigator.mozGetUserMedia ||
@ -399,15 +402,39 @@ const MediaAgent = ({setPeers}) => {
return navigator.mediaDevices return navigator.mediaDevices
.getUserMedia({audio: true, video: true}) .getUserMedia({audio: true, video: true})
.then((media) => {
return {
media: media,
audio: true,
video: true
};
})
.catch((error) => { .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.`); `failed. Trying just audio.`);
return navigator.mediaDevices return navigator.mediaDevices
.getUserMedia({ audio: true, video: false }) .getUserMedia({ audio: true, video: false })
.then((media) => {
return {
media: media,
audio: true,
video: false
};
}) })
.then((media) => { /* user accepted access to a/v */ .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"); console.log("media-agent - Access granted to audio/video");
media.getVideoTracks().forEach((track) => { context.media.getVideoTracks().forEach((track) => {
track.applyConstraints({ track.applyConstraints({
"video": { "video": {
"width": { "width": {
@ -421,8 +448,46 @@ const MediaAgent = ({setPeers}) => {
} }
}); });
}); });
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()]);
}); });
}; };