Browse Source

Use mediaDevices.enumerateDevices to retrieve media input devices.

pull/248/head
Simon Eisenmann 10 years ago
parent
commit
53290cd4c5
  1. 34
      static/js/services/mediadevices.js
  2. 29
      static/js/services/mediasources.js

34
static/js/services/mediadevices.js

@ -19,44 +19,12 @@
* *
*/ */
/* global Promise */
"use strict"; "use strict";
define(['webrtc.adapter'], function() { define(['webrtc.adapter'], function() {
// mediaDevices // mediaDevices
return ["$window", function($window) { return ["$window", function($window) {
return $window.navigator.mediaDevices;
var mediaDevices = $window.navigator.mediaDevices || {};
var getUserMedia = (function() {
// Implement a Promise based wrapper around getUserMedia.
if (mediaDevices.getUserMedia) {
// mediaDevices calls return Promise native.
return mediaDevices.getUserMedia.bind(mediaDevices);
} else {
return function getUserMedia(constraints) {
return new Promise(function(resolve, reject) {
var onSuccess = function(stream) {
resolve(stream)
};
var onError = function(error) {
reject(error);
};
try {
$window.getUserMedia(constraints, onSuccess, onError);
} catch(err) {
onError(err);
}
});
}
}
})();
// Public api.
return {
shim: mediaDevices.getUserMedia ? false : true,
getUserMedia: getUserMedia
}
}]; }];
}); });

29
static/js/services/mediasources.js

@ -20,13 +20,14 @@
*/ */
"use strict"; "use strict";
define(['jquery', 'underscore'], function($, _) { define(['jquery', 'underscore', 'webrtc.adapter'], function($, _, adapter) {
return ["$window", function($window) { return ["$window", "mediaDevices", function($window, mediaDevices) {
var MediaSources = function() { var MediaSources = function() {
this.supported = $window.MediaStreamTrack && $window.MediaStreamTrack.getSources // For now enable media sources only in Chrome until other browsers have some use for it.
this.supported = $window.navigator.mediaDevices.enumerateDevices && adapter.webrtcDetectedBrowser === "chrome";
this.audio = []; this.audio = [];
this.video = []; this.video = [];
@ -57,25 +58,29 @@ define(['jquery', 'underscore'], function($, _) {
MediaSources.prototype._refresh = function(cb) { MediaSources.prototype._refresh = function(cb) {
$window.MediaStreamTrack.getSources(_.bind(function(sources) { mediaDevices.enumerateDevices().then(_.bind(function(devices) {
var audio = this.audio = []; var audio = this.audio = [];
var video = this.video = []; var video = this.video = [];
_.each(sources, function(source) { _.each(devices, function(device) {
var o = { var o = {
id: source.id, id: device.deviceId,
facing: source.facing }
}; if (device.kind === "audioinput") {
if (source.kind === "audio") { o.label = device.label ? device.label : "Microphone " + (audio.length + 1);
o.label = source.label ? source.label : "Microphone " + (audio.length + 1);
audio.push(o); audio.push(o);
} else if (source.kind === "video") { } else if (device.kind === "videoinput") {
o.label = source.label ? source.label : "Camera " + (video.length + 1); o.label = device.label ? device.label : "Camera " + (video.length + 1);
video.push(o); video.push(o);
} }
}); });
if (cb) { if (cb) {
cb(audio, video); cb(audio, video);
} }
}, this), _.bind(function(error) {
console.error("failed to get media devices: " + error);
if (cb) {
cb([], []);
}
}, this)); }, this));
}; };

Loading…
Cancel
Save