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 @@ @@ -19,44 +19,12 @@
*
*/
/* global Promise */
"use strict";
define(['webrtc.adapter'], function() {
// mediaDevices
return ["$window", function($window) {
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
}
return $window.navigator.mediaDevices;
}];
});

29
static/js/services/mediasources.js

@ -20,13 +20,14 @@ @@ -20,13 +20,14 @@
*/
"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() {
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.video = [];
@ -57,25 +58,29 @@ define(['jquery', 'underscore'], function($, _) { @@ -57,25 +58,29 @@ define(['jquery', 'underscore'], function($, _) {
MediaSources.prototype._refresh = function(cb) {
$window.MediaStreamTrack.getSources(_.bind(function(sources) {
mediaDevices.enumerateDevices().then(_.bind(function(devices) {
var audio = this.audio = [];
var video = this.video = [];
_.each(sources, function(source) {
_.each(devices, function(device) {
var o = {
id: source.id,
facing: source.facing
};
if (source.kind === "audio") {
o.label = source.label ? source.label : "Microphone " + (audio.length + 1);
id: device.deviceId,
}
if (device.kind === "audioinput") {
o.label = device.label ? device.label : "Microphone " + (audio.length + 1);
audio.push(o);
} else if (source.kind === "video") {
o.label = source.label ? source.label : "Camera " + (video.length + 1);
} else if (device.kind === "videoinput") {
o.label = device.label ? device.label : "Camera " + (video.length + 1);
video.push(o);
}
});
if (cb) {
cb(audio, video);
}
}, this), _.bind(function(error) {
console.error("failed to get media devices: " + error);
if (cb) {
cb([], []);
}
}, this));
};

Loading…
Cancel
Save