Skip to content

Commit

Permalink
add enableVisualizer to RoomOptions.
Browse files Browse the repository at this point in the history
  • Loading branch information
cloudwebrtc committed Nov 30, 2024
1 parent 195cfdf commit a401197
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 15 deletions.
9 changes: 6 additions & 3 deletions example/lib/pages/prejoin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,12 @@ class _PreJoinPageState extends State<PreJoinPage> {
}

if (_selectedAudioDevice != null) {
_audioTrack = await LocalAudioTrack.create(AudioCaptureOptions(
deviceId: _selectedAudioDevice!.deviceId,
));
_audioTrack = await LocalAudioTrack.create(
AudioCaptureOptions(
deviceId: _selectedAudioDevice!.deviceId,
),
true, // enableVisualizer
);
await _audioTrack!.start();
}
}
Expand Down
1 change: 1 addition & 0 deletions lib/src/core/room.dart
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,7 @@ class Room extends DisposableChangeNotifier with EventsEmittable<RoomEvent> {
trackSid,
receiver: event.receiver,
audioOutputOptions: roomOptions.defaultAudioOutputOptions,
enableVisualizer: roomOptions.enableVisualizer,
);
} on TrackSubscriptionExceptionEvent catch (event) {
logger.severe('addSubscribedMediaTrack() throwed ${event}');
Expand Down
4 changes: 4 additions & 0 deletions lib/src/options.dart
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ class RoomOptions {
/// Options for end-to-end encryption.
final E2EEOptions? e2eeOptions;

/// Enable visualizer for audio tracks. Defaults to true.
final bool enableVisualizer;

const RoomOptions({
this.defaultCameraCaptureOptions = const CameraCaptureOptions(),
this.defaultScreenShareCaptureOptions = const ScreenShareCaptureOptions(),
Expand All @@ -126,6 +129,7 @@ class RoomOptions {
this.dynacast = false,
this.stopLocalTrackOnUnpublish = true,
this.e2eeOptions,
this.enableVisualizer = true,
});

RoomOptions copyWith({
Expand Down
3 changes: 2 additions & 1 deletion lib/src/participant/local.dart
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,8 @@ class LocalParticipant extends Participant<LocalTrackPublication> {
} else if (source == TrackSource.microphone) {
AudioCaptureOptions captureOptions =
audioCaptureOptions ?? room.roomOptions.defaultAudioCaptureOptions;
final track = await LocalAudioTrack.create(captureOptions);
final track = await LocalAudioTrack.create(
captureOptions, room.roomOptions.enableVisualizer);
return await publishAudioTrack(track);
} else if (source == TrackSource.screenShareVideo) {
ScreenShareCaptureOptions captureOptions = screenShareCaptureOptions ??
Expand Down
5 changes: 3 additions & 2 deletions lib/src/participant/remote.dart
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ class RemoteParticipant extends Participant<RemoteTrackPublication> {
String trackSid, {
rtc.RTCRtpReceiver? receiver,
AudioOutputOptions audioOutputOptions = const AudioOutputOptions(),
bool? enableVisualizer,
}) async {
logger.fine('addSubscribedMediaTrack()');

Expand Down Expand Up @@ -153,8 +154,8 @@ class RemoteParticipant extends Participant<RemoteTrackPublication> {
RemoteVideoTrack(pub.source, stream, mediaTrack, receiver: receiver);
} else if (pub.kind == TrackType.AUDIO) {
// audio track
track =
RemoteAudioTrack(pub.source, stream, mediaTrack, receiver: receiver);
track = RemoteAudioTrack(pub.source, stream, mediaTrack,
receiver: receiver, enableVisualizer: enableVisualizer);

var listener = track.createListener();
listener.on<AudioPlaybackStarted>((event) {
Expand Down
8 changes: 6 additions & 2 deletions lib/src/track/local/audio.dart
Original file line number Diff line number Diff line change
Expand Up @@ -118,17 +118,20 @@ class LocalAudioTrack extends LocalTrack
TrackSource source,
rtc.MediaStream stream,
rtc.MediaStreamTrack track,
this.currentOptions,
) : super(
this.currentOptions, {
bool? enableVisualizer,
}) : super(
TrackType.AUDIO,
source,
stream,
track,
enableVisualizer: enableVisualizer,
);

/// Creates a new audio track from the default audio input device.
static Future<LocalAudioTrack> create([
AudioCaptureOptions? options,
bool? enableVisualizer,
]) async {
options ??= const AudioCaptureOptions();
final stream = await LocalTrack.createStream(options);
Expand All @@ -138,6 +141,7 @@ class LocalAudioTrack extends LocalTrack
stream,
stream.getAudioTracks().first,
options,
enableVisualizer: enableVisualizer,
);
}
}
18 changes: 14 additions & 4 deletions lib/src/track/local/local.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,18 @@ mixin AudioTrack on Track {
StreamSubscription? _streamSubscription;

@override
Future<void> onStarted() => startVisualizer();
Future<void> onStarted() async {
if (enableVisualizer == true) {
await startVisualizer();
}
}

@override
Future<void> onStopped() => stopVisualizer();
Future<void> onStopped() async {
if (enableVisualizer == true) {
await stopVisualizer();
}
}

Future<void> startVisualizer() async {
if (_eventChannel != null) {
Expand Down Expand Up @@ -115,12 +123,14 @@ abstract class LocalTrack extends Track {
TrackType kind,
TrackSource source,
rtc.MediaStream mediaStream,
rtc.MediaStreamTrack mediaStreamTrack,
) : super(
rtc.MediaStreamTrack mediaStreamTrack, {
bool? enableVisualizer,
}) : super(
kind,
source,
mediaStream,
mediaStreamTrack,
enableVisualizer: enableVisualizer,
) {
mediaStreamTrack.onEnded = () {
logger.fine('MediaStreamTrack.onEnded()');
Expand Down
3 changes: 2 additions & 1 deletion lib/src/track/remote/audio.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,14 @@ class RemoteAudioTrack extends RemoteTrack
String? _deviceId;
RemoteAudioTrack(
TrackSource source, rtc.MediaStream stream, rtc.MediaStreamTrack track,
{rtc.RTCRtpReceiver? receiver})
{rtc.RTCRtpReceiver? receiver, bool? enableVisualizer})
: super(
TrackType.AUDIO,
source,
stream,
track,
receiver: receiver,
enableVisualizer: enableVisualizer,
);

@override
Expand Down
3 changes: 2 additions & 1 deletion lib/src/track/remote/remote.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@ import '../track.dart';
abstract class RemoteTrack extends Track {
RemoteTrack(TrackType kind, TrackSource source, rtc.MediaStream stream,
rtc.MediaStreamTrack track,
{rtc.RTCRtpReceiver? receiver})
{rtc.RTCRtpReceiver? receiver, bool? enableVisualizer})
: super(
kind,
source,
stream,
track,
receiver: receiver,
enableVisualizer: enableVisualizer,
);

@override
Expand Down
4 changes: 3 additions & 1 deletion lib/src/track/track.dart
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,10 @@ abstract class Track extends DisposableChangeNotifier

rtc.RTCRtpReceiver? receiver;

final bool? enableVisualizer;

Track(this.kind, this.source, this._mediaStream, this._mediaStreamTrack,
{this.receiver}) {
{this.receiver, this.enableVisualizer}) {
// Any event emitted will trigger ChangeNotifier
events.listen((event) {
logger.finer('[TrackEvent] $event, will notifyListeners()');
Expand Down

0 comments on commit a401197

Please sign in to comment.