-
Notifications
You must be signed in to change notification settings - Fork 23
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
fix(client): receive audioLevels when joined the call with mic muted #1202
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
4cbe21f
fix(react-native): receive audioLevels when joined the call with mic …
khushal87 14ae3c0
fix(react-native): styling fix
khushal87 a11dbc9
refactor: muteStream and unmuteStream to muteOrDestrouStream and unmu…
khushal87 b5b1ecd
fix: tests for microphone manager and inputdevicemanager
khushal87 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -8,6 +8,13 @@ import { getLogger } from '../logger'; | |||||
import { TrackType } from '../gen/video/sfu/models/models'; | ||||||
import { deviceIds$ } from './devices'; | ||||||
|
||||||
export type UnmuteOrCreateStreamSettings = { | ||||||
/** | ||||||
* Create a stream that is muted. Useful for React Native SDK audioLevel Stats. | ||||||
*/ | ||||||
createMutedStream?: boolean; | ||||||
}; | ||||||
|
||||||
export abstract class InputMediaDeviceManager< | ||||||
T extends InputMediaDeviceManagerState<C>, | ||||||
C = MediaTrackConstraints, | ||||||
|
@@ -55,7 +62,7 @@ export abstract class InputMediaDeviceManager< | |||||
*/ | ||||||
async enable() { | ||||||
if (this.state.status === 'enabled') return; | ||||||
this.enablePromise = this.unmuteStream(); | ||||||
this.enablePromise = this.unmuteOrCreateStream(); | ||||||
try { | ||||||
await this.enablePromise; | ||||||
this.state.setStatus('enabled'); | ||||||
|
@@ -73,7 +80,7 @@ export abstract class InputMediaDeviceManager< | |||||
this.state.prevStatus = this.state.status; | ||||||
if (this.state.status === 'disabled') return; | ||||||
const stopTracks = this.state.disableMode === 'stop-tracks'; | ||||||
this.disablePromise = this.muteStream(stopTracks); | ||||||
this.disablePromise = this.muteOrDestroyStream(stopTracks); | ||||||
try { | ||||||
await this.disablePromise; | ||||||
this.state.setStatus('disabled'); | ||||||
|
@@ -140,44 +147,26 @@ export abstract class InputMediaDeviceManager< | |||||
|
||||||
protected async applySettingsToStream() { | ||||||
if (this.state.status === 'enabled') { | ||||||
await this.muteStream(); | ||||||
await this.unmuteStream(); | ||||||
await this.muteOrDestroyStream(); | ||||||
await this.unmuteOrCreateStream(); | ||||||
} | ||||||
} | ||||||
|
||||||
protected abstract getDevices(): Observable<MediaDeviceInfo[] | undefined>; | ||||||
|
||||||
protected abstract getStream(constraints: C): Promise<MediaStream>; | ||||||
|
||||||
protected abstract publishStream(stream: MediaStream): Promise<void>; | ||||||
protected abstract publishStream( | ||||||
stream: MediaStream, | ||||||
disableTrackWhilePublish?: boolean, | ||||||
): Promise<void>; | ||||||
|
||||||
protected abstract stopPublishStream(stopTracks: boolean): Promise<void>; | ||||||
|
||||||
protected getTracks(): MediaStreamTrack[] { | ||||||
return this.state.mediaStream?.getTracks() ?? []; | ||||||
} | ||||||
|
||||||
protected async muteStream(stopTracks: boolean = true) { | ||||||
if (!this.state.mediaStream) return; | ||||||
this.logger('debug', `${stopTracks ? 'Stopping' : 'Disabling'} stream`); | ||||||
if (this.call.state.callingState === CallingState.JOINED) { | ||||||
await this.stopPublishStream(stopTracks); | ||||||
} | ||||||
this.muteLocalStream(stopTracks); | ||||||
const allEnded = this.getTracks().every((t) => t.readyState === 'ended'); | ||||||
if (allEnded) { | ||||||
if ( | ||||||
this.state.mediaStream && | ||||||
// @ts-expect-error release() is present in react-native-webrtc | ||||||
typeof this.state.mediaStream.release === 'function' | ||||||
) { | ||||||
// @ts-expect-error called to dispose the stream in RN | ||||||
this.state.mediaStream.release(); | ||||||
} | ||||||
this.state.setMediaStream(undefined); | ||||||
} | ||||||
} | ||||||
|
||||||
private muteTracks() { | ||||||
this.getTracks().forEach((track) => { | ||||||
if (track.enabled) track.enabled = false; | ||||||
|
@@ -207,25 +196,60 @@ export abstract class InputMediaDeviceManager< | |||||
} | ||||||
} | ||||||
|
||||||
protected async unmuteStream() { | ||||||
protected async muteOrDestroyStream(stopTracks: boolean = true) { | ||||||
if (!this.state.mediaStream) return; | ||||||
this.logger('debug', `${stopTracks ? 'Stopping' : 'Disabling'} stream`); | ||||||
if (this.call.state.callingState === CallingState.JOINED) { | ||||||
await this.stopPublishStream(stopTracks); | ||||||
} | ||||||
this.muteLocalStream(stopTracks); | ||||||
const allEnded = this.getTracks().every((t) => t.readyState === 'ended'); | ||||||
if (allEnded) { | ||||||
if ( | ||||||
this.state.mediaStream && | ||||||
// @ts-expect-error release() is present in react-native-webrtc | ||||||
typeof this.state.mediaStream.release === 'function' | ||||||
) { | ||||||
// @ts-expect-error called to dispose the stream in RN | ||||||
this.state.mediaStream.release(); | ||||||
} | ||||||
this.state.setMediaStream(undefined); | ||||||
} | ||||||
} | ||||||
|
||||||
protected async unmuteOrCreateStream( | ||||||
settings?: UnmuteOrCreateStreamSettings, | ||||||
) { | ||||||
this.logger('debug', 'Starting stream'); | ||||||
let stream: MediaStream; | ||||||
if ( | ||||||
this.state.mediaStream && | ||||||
this.getTracks().every((t) => t.readyState === 'live') | ||||||
) { | ||||||
stream = this.state.mediaStream; | ||||||
this.unmuteTracks(); | ||||||
} else { | ||||||
if (!this.state.mediaStream && settings && settings.createMutedStream) { | ||||||
const defaultConstraints = this.state.defaultConstraints; | ||||||
const constraints: MediaTrackConstraints = { | ||||||
...defaultConstraints, | ||||||
deviceId: this.state.selectedDevice, | ||||||
}; | ||||||
stream = await this.getStream(constraints as C); | ||||||
this.state.setMediaStream(stream); | ||||||
await this.muteOrDestroyStream(this.state.disableMode === 'stop-tracks'); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
We always want to just mute the stream here, if we call it with |
||||||
} else { | ||||||
if ( | ||||||
this.state.mediaStream && | ||||||
this.getTracks().every((t) => t.readyState === 'live') | ||||||
) { | ||||||
stream = this.state.mediaStream; | ||||||
this.unmuteTracks(); | ||||||
} else { | ||||||
const defaultConstraints = this.state.defaultConstraints; | ||||||
const constraints: MediaTrackConstraints = { | ||||||
...defaultConstraints, | ||||||
deviceId: this.state.selectedDevice, | ||||||
}; | ||||||
stream = await this.getStream(constraints as C); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Check here if we need to mute
|
||||||
} | ||||||
} | ||||||
|
||||||
if (this.call.state.callingState === CallingState.JOINED) { | ||||||
await this.publishStream(stream); | ||||||
await this.publishStream(stream, settings?.createMutedStream); | ||||||
} | ||||||
if (this.state.mediaStream !== stream) { | ||||||
this.state.setMediaStream(stream); | ||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can do this with less copy-paste, and we can remove this whole if statement, see below