Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes for #2146 #2147

Merged
merged 1 commit into from
Aug 29, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 40 additions & 9 deletions lib/addons/p5.dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -967,10 +967,33 @@
p5.prototype.VIDEO = 'video';
p5.prototype.AUDIO = 'audio';

navigator.getUserMedia = navigator.getUserMedia ||
navigator.webkitGetUserMedia ||
navigator.mozGetUserMedia ||
navigator.msGetUserMedia;
// from: https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices/getUserMedia
// Older browsers might not implement mediaDevices at all, so we set an empty object first
if (navigator.mediaDevices === undefined) {
navigator.mediaDevices = {};
}

// Some browsers partially implement mediaDevices. We can't just assign an object
// with getUserMedia as it would overwrite existing properties.
// Here, we will just add the getUserMedia property if it's missing.
if (navigator.mediaDevices.getUserMedia === undefined) {
navigator.mediaDevices.getUserMedia = function(constraints) {

// First get ahold of the legacy getUserMedia, if present
var getUserMedia = navigator.webkitGetUserMedia || navigator.mozGetUserMedia;

// Some browsers just don't implement it - return a rejected promise with an error
// to keep a consistent interface
if (!getUserMedia) {
return Promise.reject(new Error('getUserMedia is not implemented in this browser'));
}

// Otherwise, wrap the call to the old navigator.getUserMedia with a Promise
return new Promise(function(resolve, reject) {
getUserMedia.call(navigator, constraints, resolve, reject);
});
};
}

/**
* <p>Creates a new &lt;video&gt; element that contains the audio/video feed
Expand Down Expand Up @@ -1043,19 +1066,27 @@
cb = arguments[i];
}
}

if (navigator.getUserMedia) {
var elt = document.createElement('video');

if (!constraints) {
constraints = {video: useVideo, audio: useAudio};
}

navigator.getUserMedia(constraints, function(stream) {
elt.src = window.URL.createObjectURL(stream);
if (cb) {
cb(stream);
navigator.mediaDevices.getUserMedia(constraints)
.then( function(stream) {
try {
if("srcObject" in elt) {
elt.srcObject = stream;
} else {
elt.src = window.URL.createObjectURL(stream);
}
} catch (err) {
elt.src = stream;
}
if (cb) {
cb(stream);
}
}, function(e) { console.log(e); });
} else {
throw 'getUserMedia not supported in this browser';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<head>
<script language="javascript" type="text/javascript" src="../../../../../lib/p5.js"></script>
<script language="javascript" type="text/javascript" src="../../../../../lib/addons/p5.dom.js"></script>
<script language="javascript" type="text/javascript" src="sketch.js"></script>
<script language="javascript" type="text/javascript" src="sketch.js"></script>
</head>

<body>
Expand Down