fix(settings): Always show mic audio levels

This commit is contained in:
Vlad Piersec 2020-06-25 13:31:14 +03:00 committed by Jaya Allamsetty
parent 4b1743bb2f
commit 99e7d636b7
2 changed files with 74 additions and 69 deletions

View File

@ -4,7 +4,8 @@ import React, { Component } from 'react';
import { translate } from '../../../../base/i18n';
import { IconMicrophoneEmpty, IconVolumeEmpty } from '../../../../base/icons';
import { createLocalAudioTrack } from '../../../functions';
import { equals } from '../../../../base/redux';
import { createLocalAudioTracks } from '../../../functions';
import AudioSettingsHeader from './AudioSettingsHeader';
import MicrophoneEntry from './MicrophoneEntry';
@ -53,10 +54,10 @@ export type Props = {
type State = {
/**
* An object containing the jitsiTrack and the error (if the case)
* for the microphone that is in use.
* An list of objects, each containing the microphone label, audio track, device id
* and track error if the case.
*/
currentMicData: Object
audioTracks: Object[]
}
/**
@ -81,10 +82,14 @@ class AudioSettingsContent extends Component<Props, State> {
this._onSpeakerEntryClick = this._onSpeakerEntryClick.bind(this);
this.state = {
currentMicData: {
error: false,
jitsiTrack: null
}
audioTracks: props.microphoneDevices.map(({ deviceId, label }) => {
return {
deviceId,
hasError: false,
jitsiTrack: null,
label
};
})
};
}
@ -115,20 +120,13 @@ class AudioSettingsContent extends Component<Props, State> {
/**
* Renders a single microphone entry.
*
* @param {Object} data - An object with the deviceId and label of the microphone.
* @param {Object} data - An object with the deviceId, jitsiTrack & label of the microphone.
* @param {number} index - The index of the element, used for creating a key.
* @returns {React$Node}
*/
_renderMicrophoneEntry(data, index) {
const { deviceId, label } = data;
const key = `me-${index}`;
const { deviceId, label, jitsiTrack, hasError } = data;
const isSelected = deviceId === this.props.currentMicDeviceId;
let jitsiTrack = null;
let hasError = false;
if (isSelected) {
({ jitsiTrack, hasError } = this.state.currentMicData);
}
return (
<MicrophoneEntry
@ -136,7 +134,7 @@ class AudioSettingsContent extends Component<Props, State> {
hasError = { hasError }
isSelected = { isSelected }
jitsiTrack = { jitsiTrack }
key = { key }
key = { `me-${index}` }
onClick = { this._onMicrophoneEntryClick }>
{label}
</MicrophoneEntry>
@ -166,50 +164,36 @@ class AudioSettingsContent extends Component<Props, State> {
}
/**
* Disposes the audio track for a given micData object.
*
* @param {Object} micData - The object holding the track.
* @returns {Promise<void>}
*/
_disposeTrack(micData) {
const { jitsiTrack } = micData;
return jitsiTrack ? jitsiTrack.dispose() : Promise.resolve();
}
/**
* Updates the current microphone data.
* Disposes previously created track and creates a new one.
* Creates and updates the audio tracks.
*
* @returns {void}
*/
async _updateCurrentMicData() {
await this._disposeTrack(this.state.currentMicData);
async _setTracks() {
this._disposeTracks(this.state.audioTracks);
const currentMicData = await createLocalAudioTrack(
this.props.currentMicDeviceId,
const audioTracks = await createLocalAudioTracks(
this.props.microphoneDevices
);
// In case the component gets unmounted before the track is created
// avoid a leak by not setting the state
if (this._componentWasUnmounted) {
this._disposeTrack(currentMicData);
this._disposeTracks(audioTracks);
} else {
this.setState({
currentMicData
audioTracks
});
}
}
/**
* Implements React's {@link Component#componentDidUpdate}.
* Disposes the audio tracks.
*
* @inheritdoc
* @param {Object} audioTracks - The object holding the audio tracks.
* @returns {void}
*/
componentDidUpdate(prevProps) {
if (prevProps.currentMicDeviceId !== this.props.currentMicDeviceId) {
this._updateCurrentMicData();
}
_disposeTracks(audioTracks) {
audioTracks.forEach(({ jitsiTrack }) => {
jitsiTrack && jitsiTrack.dispose();
});
}
/**
@ -218,7 +202,7 @@ class AudioSettingsContent extends Component<Props, State> {
* @inheritdoc
*/
componentDidMount() {
this._updateCurrentMicData();
this._setTracks();
}
/**
@ -228,16 +212,28 @@ class AudioSettingsContent extends Component<Props, State> {
*/
componentWillUnmount() {
this._componentWasUnmounted = true;
this._disposeTrack(this.state.currentMicData);
this._disposeTracks(this.state.audioTracks);
}
/**
* Implements React's {@link Component#componentDidUpdate}.
*
* @inheritdoc
*/
componentDidUpdate(prevProps) {
if (!equals(this.props.microphoneDevices, prevProps.microphoneDevices)) {
this._setTracks();
}
}
/**
* Implements React's {@link Component#render}.
*
* @inheritdoc
*/
render() {
const { microphoneDevices, outputDevices, t } = this.props;
const { outputDevices, t } = this.props;
return (
<div>
@ -245,7 +241,7 @@ class AudioSettingsContent extends Component<Props, State> {
<AudioSettingsHeader
IconComponent = { IconMicrophoneEmpty }
text = { t('settings.microphones') } />
{microphoneDevices.map((data, i) =>
{this.state.audioTracks.map((data, i) =>
this._renderMicrophoneEntry(data, i),
)}
<AudioSettingsHeader

View File

@ -176,27 +176,36 @@ export function createLocalVideoTracks(ids: string[]) {
/**
* Returns a promise which resolves with an object containing the corresponding
* the audio jitsiTrack/error.
* Returns a promise which resolves with a list of objects containing
* the audio track and the corresponding audio device information.
*
* @param {string} deviceId - The deviceId for the current microphone.
*
* @returns {Promise<Object>}
* @param {Object[]} devices - A list of microphone devices.
* @returns {Promise<{
* deviceId: string,
* hasError: boolean,
* jitsiTrack: Object,
* label: string
* }[]>}
*/
export function createLocalAudioTrack(deviceId: string) {
return createLocalTrack('audio', deviceId)
.then(jitsiTrack => {
return {
hasError: false,
jitsiTrack
};
})
.catch(() => {
return {
hasError: true,
jitsiTrack: null
};
});
export function createLocalAudioTracks(devices: Object[]) {
return Promise.all(
devices.map(async ({ deviceId, label }) => {
let jitsiTrack = null;
let hasError = false;
try {
jitsiTrack = await createLocalTrack('audio', deviceId);
} catch (err) {
hasError = true;
}
return {
deviceId,
hasError,
jitsiTrack,
label
};
}));
}
/**