|
|
|
@ -25,7 +25,86 @@ define(['jquery', 'underscore', 'audiocontext', 'mediastream/dummystream', 'webr
@@ -25,7 +25,86 @@ define(['jquery', 'underscore', 'audiocontext', 'mediastream/dummystream', 'webr
|
|
|
|
|
// Create AudioContext singleton, if supported.
|
|
|
|
|
var context = AudioContext ? new AudioContext() : null; |
|
|
|
|
|
|
|
|
|
var getUserMedia = window.getUserMedia; |
|
|
|
|
// Converter helpers to convert media constraints to new API.
|
|
|
|
|
var mergeConstraints = function(constraints, k, v, mandatory) { |
|
|
|
|
var prefix = k.substring(0, 3); |
|
|
|
|
switch (prefix) { |
|
|
|
|
case "min": |
|
|
|
|
case "max": |
|
|
|
|
var suffix = k[3].toLowerCase()+k.substring(4); |
|
|
|
|
if (!constraints.hasOwnProperty(suffix)) { |
|
|
|
|
constraints[suffix]={}; |
|
|
|
|
} |
|
|
|
|
if (mandatory && prefix === "min" && constraints[suffix].hasOwnProperty(prefix)) { |
|
|
|
|
// Use existing min constraint as ideal.
|
|
|
|
|
constraints[suffix].ideal = constraints[suffix].min; |
|
|
|
|
} |
|
|
|
|
constraints[suffix][prefix]=v; |
|
|
|
|
break; |
|
|
|
|
default: |
|
|
|
|
constraints[k] = v; |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
var convertConstraints = function(constraints) { |
|
|
|
|
if (!constraints) { |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
if (!constraints.hasOwnProperty("optional") && !constraints.hasOwnProperty("mandatory")) { |
|
|
|
|
// No old style members.
|
|
|
|
|
return constraints; |
|
|
|
|
} |
|
|
|
|
var c = {}; |
|
|
|
|
// Process optional constraints.
|
|
|
|
|
if (constraints.optional) { |
|
|
|
|
_.each(constraints.optional, function(o) { |
|
|
|
|
_.each(o, function(v, k) { |
|
|
|
|
mergeConstraints(c, k, v); |
|
|
|
|
}) |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
// Process mandatory constraints.
|
|
|
|
|
if (constraints.mandatory) { |
|
|
|
|
_.each(constraints.mandatory, function(v, k) { |
|
|
|
|
mergeConstraints(c, k, v, true); |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
// Fastpath.
|
|
|
|
|
if (_.isEmpty(c)) { |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
// Use ideal if there is only one value set.
|
|
|
|
|
_.each(c, function(v, k) { |
|
|
|
|
if (_.isObject(v)) { |
|
|
|
|
var values = _.values(v); |
|
|
|
|
if (values.length === 1) { |
|
|
|
|
// Use as ideal value if only one given.
|
|
|
|
|
c[k] = {ideal: values[0]}; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
return c; |
|
|
|
|
}; |
|
|
|
|
// Adapter to support navigator.mediaDevices API.
|
|
|
|
|
// http://w3c.github.io/mediacapture-main/getusermedia.html#mediadevices
|
|
|
|
|
var getUserMedia = (function() { |
|
|
|
|
if (window.webrtcDetectedBrowser === "firefox"&& window.webrtcDetectedVersion >= 38) { |
|
|
|
|
console.info("Enabled mediaDevices adapter ..."); |
|
|
|
|
return function(constraints, success, error) { |
|
|
|
|
// Full constraints syntax with plain values and ideal-algorithm supported in FF38+.
|
|
|
|
|
// Note on FF32-37: Plain values and ideal are not supported.
|
|
|
|
|
// See https://wiki.mozilla.org/Media/getUserMedia for details.
|
|
|
|
|
// Examples here: https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices/getUserMedia
|
|
|
|
|
var c = {audio: convertConstraints(constraints.audio), video: convertConstraints(constraints.video)}; |
|
|
|
|
// mediaDevices API returns a promise.
|
|
|
|
|
console.log("Constraints for mediaDevices", c); |
|
|
|
|
window.navigator.mediaDevices.getUserMedia(c).then(success).catch(error); |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
// Use existing adapter.
|
|
|
|
|
return window.getUserMedia; |
|
|
|
|
} |
|
|
|
|
})(); |
|
|
|
|
|
|
|
|
|
var stopUserMediaStream = (function() { |
|
|
|
|
return function(stream) { |
|
|
|
|