Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Add wrapper for MediaDevices
MediaDevicesManager stores the id of the audio and video input devices
to be used when requesting media. If no id is set then it behaves like
before, that is, letting the browser decide which device to use.

Signed-off-by: Daniel Calviño Sánchez <[email protected]>
  • Loading branch information
danxuliu committed Aug 5, 2020
commit 8d1219ce9fee652e0d57d0afce36efbacc743e62
87 changes: 87 additions & 0 deletions src/utils/webrtc/MediaDevicesManager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/**
*
* @copyright Copyright (c) 2020, Daniel Calviño Sánchez ([email protected])
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

/**
* Wrapper for MediaDevices to simplify its use.
*
* "attributes.audioInputId" and "attributes.videoInputId" define the devices
* that will be used when calling "getUserMedia(constraints)".
*/
export default function MediaDevicesManager() {
this.attributes = {
audioInputId: undefined,
videoInputId: undefined,
}
}
MediaDevicesManager.prototype = {

/**
* Returns whether getting user media and enumerating media devices is
* supported or not.
*
* Note that even if false is returned the MediaDevices interface could be
* technically supported by the browser but not available due to the page
* being loaded in an insecure context.
*
* @returns {boolean} true if MediaDevices interface is supported, false
* otherwise.
*/
isSupported: function() {
return navigator && navigator.mediaDevices && navigator.mediaDevices.getUserMedia && navigator.mediaDevices.enumerateDevices
},

/**
* Wrapper for MediaDevices.getUserMedia to use the selected audio and video
* input devices.
*
* The selected audio and video input devices are used only if the
* constraints do not specify a device already. Otherwise the devices in the
* constraints are respected.
*
* @param {MediaStreamConstraints} constraints the constraints specifying
* the media to request
* @returns {Promise} resolved with a MediaStream object when successful, or
* rejected with a DOMException in case of error
*/
getUserMedia: function(constraints) {
if (!this.isSupported()) {
return new Promise((resolve, reject) => {
reject(new DOMException('MediaDevicesManager is not supported', 'NotSupportedError'))
})
}

if (constraints.audio && !constraints.audio.deviceId && this.attributes.audioInputId) {
if (!(constraints.audio instanceof Object)) {
constraints.audio = {}
}
constraints.audio.deviceId = this.attributes.audioInputId
}

if (constraints.video && !constraints.video.deviceId && this.attributes.videoInputId) {
if (!(constraints.video instanceof Object)) {
constraints.video = {}
}
constraints.video.deviceId = this.attributes.videoInputId
}

return navigator.mediaDevices.getUserMedia(constraints)
},
}
4 changes: 4 additions & 0 deletions src/utils/webrtc/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import CallAnalyzer from './analyzers/CallAnalyzer'
import CallParticipantCollection from './models/CallParticipantCollection'
import LocalCallParticipantModel from './models/LocalCallParticipantModel'
import LocalMediaModel from './models/LocalMediaModel'
import MediaDevicesManager from './MediaDevicesManager'
import SentVideoQualityThrottler from './SentVideoQualityThrottler'
import { PARTICIPANT } from '../../constants'
import { fetchSignalingSettings } from '../../services/signalingService'
Expand All @@ -35,6 +36,7 @@ let webRtc = null
const callParticipantCollection = new CallParticipantCollection()
const localCallParticipantModel = new LocalCallParticipantModel()
const localMediaModel = new LocalMediaModel()
const mediaDevicesManager = new MediaDevicesManager()
let callAnalyzer = null
let sentVideoQualityThrottler = null

Expand Down Expand Up @@ -225,6 +227,8 @@ export {
localCallParticipantModel,
localMediaModel,

mediaDevicesManager,

callAnalyzer,

signalingJoinConversation,
Expand Down
9 changes: 6 additions & 3 deletions src/utils/webrtc/simplewebrtc/localmedia.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ const hark = require('hark')
const getScreenMedia = require('./getscreenmedia')
const WildEmitter = require('wildemitter')
const mockconsole = require('mockconsole')
// Only mediaDevicesManager is used, but it can not be assigned here due to not
// being initialized yet.
const webrtcIndex = require('../index.js')

function isAllTracksEnded(stream) {
let isAllTracksEnded = true
Expand Down Expand Up @@ -43,7 +46,7 @@ function LocalMedia(opts) {
this._audioMonitorStreams = []
this.localScreens = []

if (!navigator.mediaDevices || !navigator.mediaDevices.getUserMedia) {
if (!webrtcIndex.mediaDevicesManager.isSupported()) {
this._logerror('Your browser does not support local media capture.')
}

Expand Down Expand Up @@ -86,7 +89,7 @@ LocalMedia.prototype.start = function(mediaConstraints, cb, context) {
const self = this
const constraints = mediaConstraints || this.config.media

if (!navigator || !navigator.mediaDevices || !navigator.mediaDevices.getUserMedia) {
if (!webrtcIndex.mediaDevicesManager.isSupported()) {
const error = new Error('MediaStreamError')
error.name = 'NotSupportedError'

Expand All @@ -99,7 +102,7 @@ LocalMedia.prototype.start = function(mediaConstraints, cb, context) {

this.emit('localStreamRequested', constraints, context)

navigator.mediaDevices.getUserMedia(constraints).then(function(stream) {
webrtcIndex.mediaDevicesManager.getUserMedia(constraints).then(function(stream) {
// Although the promise should be resolved only if all the constraints
// are met Edge resolves it if both audio and video are requested but
// only audio is available.
Expand Down