fix: stop audio track on mute. (#207)

* fix: stop audio track on mute.

* chore: Let audioBitrate preset to music.

* update.

* update depends.

* update.
This commit is contained in:
CloudWebRTC
2022-11-14 19:13:48 +08:00
committed by GitHub
parent 663fc313c8
commit 27ea869443
12 changed files with 98 additions and 46 deletions
+9 -1
View File
@@ -130,6 +130,9 @@ class AudioPreset {
static const telephone = 12000;
static const speech = 20000;
static const music = 32000;
static const musicStereo = 48000;
static const musicHighQuality = 64000;
static const musicHighQualityStereo = 96000;
}
/// Options used when publishing audio.
@@ -142,9 +145,14 @@ class AudioPublishOptions {
/// max audio bitrate
final int audioBitrate;
/// Turn off the audio track when muted, to avoid the microphone
/// indicator light on.
final bool stopMicTrackOnMute;
const AudioPublishOptions({
this.dtx = true,
this.audioBitrate = AudioPreset.speech,
this.audioBitrate = AudioPreset.music,
this.stopMicTrackOnMute = true,
});
@override
+14
View File
@@ -25,6 +25,7 @@ import 'participant.dart';
/// Represents the current participant in the room. Instance of [LocalParticipant] is automatically
/// created after successfully connecting to a [Room] and will be accessible from [Room.localParticipant].
class LocalParticipant extends Participant<LocalTrackPublication> {
bool stopMicTrackOnMute = false;
@internal
LocalParticipant({
required Room room,
@@ -53,6 +54,8 @@ class LocalParticipant extends Participant<LocalTrackPublication> {
publishOptions =
publishOptions ?? room.roomOptions.defaultAudioPublishOptions;
stopMicTrackOnMute = publishOptions.stopMicTrackOnMute;
final trackInfo = await room.engine.addTrack(
cid: track.getCid(),
name: track.name,
@@ -310,13 +313,24 @@ class LocalParticipant extends Participant<LocalTrackPublication> {
logger.fine('setSourceEnabled(source: $source, enabled: $enabled)');
final publication = getTrackPublicationBySource(source);
if (publication != null) {
var track = publication.track;
var stopOnMute = stopMicTrackOnMute && track is LocalAudioTrack;
if (enabled) {
await publication.unmute();
if (stopOnMute) {
await track.restartTrack();
await track.onTrackStarted();
}
} else {
if (source == TrackSource.screenShareVideo) {
await unpublishTrack(publication.sid);
} else {
await publication.mute();
if (stopOnMute) {
await track.sender?.setTrack(null);
await track.stop();
await track.onTrackStopped();
}
}
}
return publication;
+14
View File
@@ -52,6 +52,20 @@ mixin LocalAudioManagementMixin on LocalTrack, AudioTrack {
}
return didUpdate;
}
Future<void> onTrackStopped() async {
await _trackCounterLock.synchronized(() async {
_localTrackCount--;
await _onAudioTrackCountDidChange();
});
}
Future<void> onTrackStarted() async {
await _trackCounterLock.synchronized(() async {
_localTrackCount++;
await _onAudioTrackCountDidChange();
});
}
}
mixin RemoteAudioManagementMixin on RemoteTrack, AudioTrack {
/// Start playing audio track. On web platform, create an audio element and
+4
View File
@@ -312,6 +312,10 @@ class Utils {
case ConnectivityResult.ethernet:
networkType = 'wired';
break;
case ConnectivityResult.vpn:
//TODO: will livekit-server handle vpn types correctly?
// networkType = 'vpn';
break;
case ConnectivityResult.none:
networkType = 'empty';
break;