From c6262d62a63fee8aad8546141e5898c400f73284 Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Fri, 28 Apr 2017 18:21:22 +0100 Subject: [PATCH] webrtc config electron init on LoggedInView mounting configurable via UserSettings new class: CallMediaHandler Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> --- src/CallMediaHandler.js | 63 ++++++++++++++++++ src/components/structures/LoggedInView.js | 5 ++ src/components/structures/UserSettings.js | 78 ++++++++++++++++++++++- 3 files changed, 145 insertions(+), 1 deletion(-) create mode 100644 src/CallMediaHandler.js diff --git a/src/CallMediaHandler.js b/src/CallMediaHandler.js new file mode 100644 index 0000000000..9133a6548d --- /dev/null +++ b/src/CallMediaHandler.js @@ -0,0 +1,63 @@ +/* + Copyright 2017 Michael Telatynski <7t3chguy@gmail.com> + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +import UserSettingsStore from './UserSettingsStore'; +import * as Matrix from 'matrix-js-sdk'; +import q from 'q'; + +export default { + getDevices: function() { + // Only needed for Electron atm, though should work in modern browsers + // once permission has been granted to the webapp + return navigator.mediaDevices.enumerateDevices().then(function(devices) { + const audioIn = {}; + const videoIn = {}; + + devices.forEach((device) => { + switch (device.kind) { + case 'audioinput': audioIn[device.deviceId] = device.label; break; + case 'videoinput': videoIn[device.deviceId] = device.label; break; + } + }); + + // console.log("Loaded WebRTC Devices", mediaDevices); + return { + audioinput: audioIn, + videoinput: videoIn, + }; + }, (error) => { console.log('Unable to refresh WebRTC Devices: ', error); }); + }, + + loadDevices: function() { + // this.getDevices().then((devices) => { + const localSettings = UserSettingsStore.getLocalSettings(); + // // if deviceId is not found, automatic fallback is in spec + // // recall previously stored inputs if any + Matrix.setMatrixCallAudioInput(localSettings['webrtc_audioinput']); + Matrix.setMatrixCallVideoInput(localSettings['webrtc_videoinput']); + // }); + }, + + setAudioInput: function(deviceId) { + UserSettingsStore.setLocalSetting('webrtc_audioinput', deviceId); + Matrix.setMatrixCallAudioInput(deviceId); + }, + + setVideoInput: function(deviceId) { + UserSettingsStore.setLocalSetting('webrtc_videoinput', deviceId); + Matrix.setMatrixCallVideoInput(deviceId); + }, +}; diff --git a/src/components/structures/LoggedInView.js b/src/components/structures/LoggedInView.js index 9f01b0082b..8d18e92a0d 100644 --- a/src/components/structures/LoggedInView.js +++ b/src/components/structures/LoggedInView.js @@ -21,6 +21,7 @@ import React from 'react'; import KeyCode from '../../KeyCode'; import Notifier from '../../Notifier'; import PageTypes from '../../PageTypes'; +import CallMediaHandler from '../../CallMediaHandler'; import sdk from '../../index'; import dis from '../../dispatcher'; @@ -71,6 +72,10 @@ export default React.createClass({ // RoomView.getScrollState() this._scrollStateMap = {}; + // Only run these in electron, at least until a better mechanism for perms exists + // https://w3c.github.io/permissions/#dom-permissionname-device-info + if (window && window.process && window.process.type) CallMediaHandler.loadDevices(); + document.addEventListener('keydown', this._onKeyDown); }, diff --git a/src/components/structures/UserSettings.js b/src/components/structures/UserSettings.js index ba5d5780b4..05410e866f 100644 --- a/src/components/structures/UserSettings.js +++ b/src/components/structures/UserSettings.js @@ -24,6 +24,7 @@ var dis = require("../../dispatcher"); var q = require('q'); var package_json = require('../../../package.json'); var UserSettingsStore = require('../../UserSettingsStore'); +var CallMediaHandler = require('../../CallMediaHandler'); var GeminiScrollbar = require('react-gemini-scrollbar'); var Email = require('../../email'); var AddThreepid = require('../../AddThreepid'); @@ -109,7 +110,6 @@ const THEMES = [ } ]; - module.exports = React.createClass({ displayName: 'UserSettings', @@ -147,6 +147,7 @@ module.exports = React.createClass({ email_add_pending: false, vectorVersion: null, rejectingInvites: false, + mediaDevices: null, }; }, @@ -167,6 +168,18 @@ module.exports = React.createClass({ }); } + q().then(() => { + return CallMediaHandler.getDevices(); + }).then((mediaDevices) => { + console.log("got mediaDevices", mediaDevices, this._unmounted); + if (this._unmounted) return; + this.setState({ + mediaDevices, + activeAudioInput: this._localSettings['webrtc_audioinput'] || 'default', + activeVideoInput: this._localSettings['webrtc_videoinput'] || 'default', + }); + }); + // Bulk rejecting invites: // /sync won't have had time to return when UserSettings re-renders from state changes, so getRooms() // will still return rooms with invites. To get around this, add a listener for @@ -187,6 +200,8 @@ module.exports = React.createClass({ this._syncedSettings = syncedSettings; this._localSettings = UserSettingsStore.getLocalSettings(); + this._setAudioInput = this._setAudioInput.bind(this); + this._setVideoInput = this._setVideoInput.bind(this); }, componentDidMount: function() { @@ -775,6 +790,66 @@ module.exports = React.createClass({ ; }, + _mapWebRtcDevicesToSpans: function(devices) { + return Object.keys(devices).map( + (deviceId) => {devices[deviceId]} + ); + }, + + _setAudioInput: function(deviceId) { + this.setState({activeAudioInput: deviceId}); + CallMediaHandler.setAudioInput(deviceId); + }, + + _setVideoInput: function(deviceId) { + this.setState({activeVideoInput: deviceId}); + CallMediaHandler.setVideoInput(deviceId); + }, + + _renderWebRtcSettings: function() { + if (!(window && window.process && window.process.type) + || !this.state.mediaDevices) return; + + const Dropdown = sdk.getComponent('elements.Dropdown'); + + let microphoneDropdown =
No Microphones detected
; + let webcamDropdown =
No Webcams detected
; + + const audioInputs = this.state.mediaDevices.audioinput; + if ('default' in audioInputs) { + microphoneDropdown =
+

Microphone

+ + {this._mapWebRtcDevicesToSpans(audioInputs)} + +
; + } + + const videoInputs = this.state.mediaDevices.videoinput; + if ('default' in videoInputs) { + webcamDropdown =
+

Cameras

+ + {this._mapWebRtcDevicesToSpans(videoInputs)} + +
; + } + + return
+

WebRTC

+
+ {microphoneDropdown} + {webcamDropdown} +
+
; + }, + _showSpoiler: function(event) { const target = event.target; const hidden = target.getAttribute('data-spoiler'); @@ -973,6 +1048,7 @@ module.exports = React.createClass({ {this._renderUserInterfaceSettings()} {this._renderLabs()} + {this._renderWebRtcSettings()} {this._renderDevicesPanel()} {this._renderCryptoInfo()} {this._renderBulkOptions()}