Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detect devices #14

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
18 changes: 12 additions & 6 deletions lib/audioPlayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,13 @@ module.exports =
initialized = true;
},

/**
* Play a sound
* @param {String} name - Sound name
* @param {[Float]} relativeVolume - Relative volume (0.0 - 1.0)
*/
play(name, relativeVolume)
/**
* Play a sound
* @param {String} name - Sound name
* @param {String} deviceId - DeviceId to use for audio output
* @param {[Float]} relativeVolume - Relative volume (0.0 - 1.0)
*/
play(name, deviceId, relativeVolume)
{
this.initialize();

Expand All @@ -62,6 +63,11 @@ module.exports =
sound.audio.pause();
sound.audio.currentTime = 0.0;
sound.audio.volume = (sound.volume || 1.0) * relativeVolume;

if (deviceId && sound.audio.setSinkId) {
sound.audio.setSinkId(deviceId);
}

sound.audio.play();
}
catch (error)
Expand Down
23 changes: 23 additions & 0 deletions lib/components/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import Snackbar from 'material-ui/Snackbar';
import Notifier from './Notifier';
import Login from './Login';
import Phone from './Phone';
import DevicesWatcher from './DevicesWatcher';
import clone from 'clone';

const logger = new Logger('App');

Expand Down Expand Up @@ -71,6 +73,12 @@ export default class App extends React.Component
<div data-component='App'>
<Notifier ref='Notifier'/>

<DevicesWatcher
mediaDevices={state.settings.media}
onNotify={this.handleNotify.bind(this)}
onMediaSettingsChange={this.handleMediaSettingsChange.bind(this)}
/>

{component}

<Snackbar
Expand Down Expand Up @@ -151,6 +159,21 @@ export default class App extends React.Component
});
}

handleMediaSettingsChange(key, deviceId)
{
const settings = clone(this.state.settings);

logger.debug('handleMediaSettingsChange() [key:%s] [old:%s] [new:%s]', key, settings.media[key], deviceId);

// Merge media settings without mutating
settings.media[key] = deviceId;
settingsManager.set(settings);

this.setState({
settings: settingsManager.get()
});
}

handlePhoneExit()
{
logger.debug('handlePhoneExit()');
Expand Down
162 changes: 162 additions & 0 deletions lib/components/DevicesWatcher.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
'use strict';

import React from 'react';
import PropTypes from 'prop-types';
import Logger from '../Logger';

const logger = new Logger('DevicesWatcher');

export default class Dialer extends React.Component
{
constructor(props)
{
super(props);
}

reloadDevices() {
navigator.mediaDevices.enumerateDevices({audio:true, video:true})
.then(devices => {
const devicesOld = localStorage.getItem('devices')
? JSON.parse(localStorage.getItem('devices'))
: devices;

devicesOld.map((item) => {
if (!devices.find(x => x.deviceId === item.deviceId)) {
this.deviceRemoved(item, devices);
}
});

devices.map((item) => {
if (!devicesOld.find(x => x.deviceId === item.deviceId)) {
this.deviceAdded(item, devices);
}
});

localStorage.setItem('devices', JSON.stringify(devices));
});
}

deviceAdded(device) {
const {
mediaDevices: {
audioInput,
audioOutput,
videoInput
},
onMediaSettingsChange,
onNotify
} = this.props;

logger.debug('deviceAdded() [device:%o]', device);

onNotify({
level : 'success',
title : 'Device added',
message : device.label
});

if (device.kind === 'audioinput') {
localStorage.setItem('audioInputOld', audioInput);
onMediaSettingsChange('audioInput', device.deviceId);
} else if (device.kind === 'audiooutput') {
localStorage.setItem('audioOutputOld', audioOutput);
onMediaSettingsChange('audioOutput', device.deviceId);
} else if (device.kind === 'videoinput') {
localStorage.setItem('videoInputOld', videoInput);
onMediaSettingsChange('videoInput', device.deviceId);
}
}

deviceRemoved(device, devices) {
const {
mediaDevices: {
audioInput,
audioOutput,
videoInput
},
onMediaSettingsChange,
onNotify
} = this.props;

logger.debug('deviceRemoved() [device:%o]', device);

onNotify({
level : 'error',
title : 'Device removed',
message : device.label
});

// if (device.deviceId === audioInput) {
// if (audioInput && devices.find(x => x.deviceId === audioInput)) {
// onMediaSettingsChange('audioInput', audioInput)
// } else {
// onMediaSettingsChange('audioInput', 'default')
// }
// }
//
// if (device.deviceId === audioOutput) {
// if (audioOutput && devices.find(x => x.deviceId === audioOutput)) {
// onMediaSettingsChange('audioOutput', audioOutput)
// } else {
// onMediaSettingsChange('audioOutput', 'default')
// }
// }
//
// if (device.deviceId === videoInput) {
// const videoInput = localStorage.getItem('videoInput')
// if (videoInput && devices.find(x => x.deviceId === videoInput)) {
// onMediaSettingsChange('videoInput', videoInput)
// } else {
// onMediaSettingsChange('videoInput', null)
// }
// }

if (device.deviceId === audioInput) {
const audioInputOld = localStorage.getItem('audioInputOld');
if (audioInputOld && devices.find(x => x.deviceId === audioInputOld)) {
onMediaSettingsChange('audioInput', audioInputOld);
localStorage.removeItem('audioInputOld');
} else {
onMediaSettingsChange('audioInput', 'default');
}
}

if (device.deviceId === audioOutput) {
const audioOutputOld = localStorage.getItem('audioOutputOld');
if (audioOutputOld && devices.find(x => x.deviceId === audioOutputOld)) {
onMediaSettingsChange('audioOutput', audioOutputOld);
localStorage.removeItem('audioOutputOld');
} else {
onMediaSettingsChange('audioOutput', 'default');
}
}

if (device.deviceId === videoInput) {
const videoInputOld = localStorage.getItem('videoInputOld');
if (videoInputOld && devices.find(x => x.deviceId === videoInputOld)) {
onMediaSettingsChange('videoInput', videoInputOld);
localStorage.removeItem('videoInputOld');
} else {
onMediaSettingsChange('videoInput', null);
}
}
}

componentWillMount() {
this.reloadDevices();
navigator.mediaDevices.ondevicechange = () => this.reloadDevices();
}

render()
{
return null;
}
}

Dialer.propTypes =
{
settings: PropTypes.object.isRequired,
onNotify: PropTypes.func.isRequired,
onMediaSettingsChange: PropTypes.func.isRequired,
mediaDevices: PropTypes.object.isRequired
};
38 changes: 25 additions & 13 deletions lib/components/Phone.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ export default class Phone extends React.Component
session={state.session}
onNotify={props.onNotify}
onHideNotification={props.onHideNotification}
mediaDevices={props.settings.media}
/>
:
null
Expand Down Expand Up @@ -266,7 +267,7 @@ export default class Phone extends React.Component
return;
}

audioPlayer.play('ringing');
audioPlayer.play('ringing', settings.media.audioRinging);
this.setState({ incomingSession: session });

session.on('failed', () =>
Expand Down Expand Up @@ -349,15 +350,27 @@ export default class Phone extends React.Component

handleOutgoingCall(uri)
{
const {
settings: {
media: {
audioInput,
audioOutput,
videoInput
},
pcConfig
},
onNotify
} = this.props;

logger.debug('handleOutgoingCall() [uri:"%s"]', uri);

let session = this._ua.call(uri,
{
pcConfig : this.props.settings.pcConfig || { iceServers: [] },
pcConfig : pcConfig || { iceServers: [] },
mediaConstraints :
{
audio : true,
video : true
audio: audioInput ? {deviceId: {exact: audioInput}} : true,
video : videoInput ? {deviceId: {exact: videoInput}} : true
},
rtcOfferConstraints :
{
Expand All @@ -373,21 +386,20 @@ export default class Phone extends React.Component

session.on('progress', () =>
{
audioPlayer.play('ringback');
audioPlayer.play('ringback', audioOutput);
});

session.on('failed', (data) =>
{
audioPlayer.stop('ringback');
audioPlayer.play('rejected');
audioPlayer.play('rejected', audioOutput);
this.setState({ session: null });

this.props.onNotify(
{
level : 'error',
title : 'Call failed',
message : data.cause
});
onNotify({
level : 'error',
title : 'Call failed',
message : data.cause
});
});

session.on('ended', () =>
Expand All @@ -399,7 +411,7 @@ export default class Phone extends React.Component
session.on('accepted', () =>
{
audioPlayer.stop('ringback');
audioPlayer.play('answered');
audioPlayer.play('answered', audioOutput);
});
}

Expand Down
Loading