Added dummy audio/video track if no media access
Signed-off-by: James Ketrenos <james_eikona@ketrenos.com>
This commit is contained in:
parent
3bbff22ec2
commit
eea8b1acce
@ -392,37 +392,102 @@ 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 ||
|
||||||
navigator.msGetUserMedia);
|
navigator.msGetUserMedia);
|
||||||
|
|
||||||
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) => {
|
||||||
.then((media) => { /* user accepted access to a/v */
|
return {
|
||||||
console.log("media-agent - Access granted to audio/video");
|
media: media,
|
||||||
media.getVideoTracks().forEach((track) => {
|
audio: true,
|
||||||
track.applyConstraints({
|
video: false
|
||||||
"video": {
|
};
|
||||||
"width": {
|
})
|
||||||
"min": 160,
|
.catch((error) => {
|
||||||
"max": 320
|
console.log(`media-agent - Access to audio ` +
|
||||||
},
|
`failed.`);
|
||||||
"height": {
|
return {
|
||||||
"min": 120,
|
media: undefined,
|
||||||
"max": 240
|
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.`);
|
if (debug) console.log(`media-agent - WebSocket open request. Attempting to create local media.`);
|
||||||
setup_local_media().then((media) => {
|
setup_local_media().then((media) => {
|
||||||
/* once the user has given us access to their
|
/* 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) {
|
if (abort) {
|
||||||
console.log(`media-agent - aborting setting local media`);
|
console.log(`media-agent - aborting setting local media`);
|
||||||
} else {
|
} else {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user