Skip to content

Commit a5d9620

Browse files
committed
2 parents f3fad79 + 1095e25 commit a5d9620

19 files changed

+159
-19
lines changed

lib/src/extensions.dart

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,25 @@ extension ConnectionQualityExt on lk_models.ConnectionQuality {
102102
}[this] ??
103103
ConnectionQuality.unknown;
104104
}
105+
106+
extension PBTrackSourceExt on lk_models.TrackSource {
107+
TrackSource toLKType() =>
108+
<lk_models.TrackSource, TrackSource>{
109+
lk_models.TrackSource.CAMERA: TrackSource.camera,
110+
lk_models.TrackSource.MICROPHONE: TrackSource.microphone,
111+
lk_models.TrackSource.SCREEN_SHARE: TrackSource.screenShareVideo,
112+
lk_models.TrackSource.SCREEN_SHARE_AUDIO: TrackSource.screenShareAudio,
113+
}[this] ??
114+
TrackSource.unknown;
115+
}
116+
117+
extension LKTrackSourceExt on TrackSource {
118+
lk_models.TrackSource toPBType() =>
119+
<TrackSource, lk_models.TrackSource>{
120+
TrackSource.camera: lk_models.TrackSource.CAMERA,
121+
TrackSource.microphone: lk_models.TrackSource.MICROPHONE,
122+
TrackSource.screenShareVideo: lk_models.TrackSource.SCREEN_SHARE,
123+
TrackSource.screenShareAudio: lk_models.TrackSource.SCREEN_SHARE_AUDIO,
124+
}[this] ??
125+
lk_models.TrackSource.UNKNOWN;
126+
}

lib/src/participant/local_participant.dart

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ class LocalParticipant extends Participant {
5858
cid: track.getCid(),
5959
name: track.name,
6060
kind: track.kind,
61+
source: track.source.toPBType(),
6162
dtx: options?.dtx,
6263
);
6364

@@ -102,6 +103,7 @@ class LocalParticipant extends Participant {
102103
cid: track.getCid(),
103104
name: track.name,
104105
kind: track.kind,
106+
source: track.source.toPBType(),
105107
);
106108

107109
logger.fine('publishVideoTrack addTrack response: ${trackInfo}');
@@ -230,3 +232,37 @@ class LocalParticipant extends Participant {
230232
super.updateFromInfo(info);
231233
}
232234
}
235+
236+
extension LocalParticipantTrackSourceExt on LocalParticipant {
237+
Future<void> setCameraEnabled(bool enabled) async {
238+
return setSourceEnabled(TrackSource.camera, enabled);
239+
}
240+
241+
Future<void> setMicrophoneEnabled(bool enabled) async {
242+
return setSourceEnabled(TrackSource.microphone, enabled);
243+
}
244+
245+
Future<void> setSourceEnabled(TrackSource source, bool enabled) async {
246+
final pub = getTrackPublicationBySource(source);
247+
if (pub != null) {
248+
if (enabled) {
249+
pub.muted = false;
250+
} else {
251+
if (source == TrackSource.screenShareVideo) {
252+
await unpublishTrack(pub.sid);
253+
} else {
254+
pub.muted = true;
255+
}
256+
}
257+
} else if (enabled) {
258+
if (source == TrackSource.camera) {
259+
final track = await LocalVideoTrack.createCameraTrack();
260+
await publishVideoTrack(track);
261+
} else if (source == TrackSource.microphone) {
262+
final track = await LocalAudioTrack.create();
263+
await publishAudioTrack(track);
264+
}
265+
// TODO: Screen share
266+
}
267+
}
268+
}

lib/src/participant/participant.dart

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import 'package:collection/collection.dart';
2+
import 'package:livekit_client/src/track/track.dart';
23
import 'package:meta/meta.dart';
34

45
import '../events.dart';
@@ -187,3 +188,38 @@ extension ParticipantExt on Participant {
187188
.where((e) => e.kind == lk_models.TrackType.AUDIO)
188189
.toList();
189190
}
191+
192+
extension ParticipantTrackSourceExt on Participant {
193+
bool isCameraEnabled() {
194+
return !(getTrackPublicationBySource(TrackSource.camera)?.muted ?? true);
195+
}
196+
197+
bool isMicrophoneEnabled() {
198+
return !(getTrackPublicationBySource(TrackSource.microphone)?.muted ??
199+
true);
200+
}
201+
202+
/// Find a track publication by its [TrackSource]
203+
TrackPublication? getTrackPublicationBySource(TrackSource source) {
204+
if (source == TrackSource.unknown) return null;
205+
// try to find by source
206+
final result =
207+
trackPublications.values.firstWhereOrNull((e) => e.source == source);
208+
if (result != null) return result;
209+
// try to find by compatibility
210+
return trackPublications.values
211+
.where((e) => e.source == TrackSource.unknown)
212+
.firstWhereOrNull((e) =>
213+
(source == TrackSource.microphone &&
214+
e.kind == lk_models.TrackType.AUDIO) ||
215+
(source == TrackSource.camera &&
216+
e.kind == lk_models.TrackType.VIDEO &&
217+
e.name != Track.screenShareName) ||
218+
(source == TrackSource.screenShareVideo &&
219+
e.kind == lk_models.TrackType.VIDEO &&
220+
e.name == Track.screenShareName) ||
221+
(source == TrackSource.screenShareAudio &&
222+
e.kind == lk_models.TrackType.AUDIO &&
223+
e.name == Track.screenShareName));
224+
}
225+
}

lib/src/participant/remote_participant.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
import 'package:flutter_webrtc/flutter_webrtc.dart' as rtc;
2-
import 'package:livekit_client/src/track/remote_audio_track.dart';
3-
import 'package:livekit_client/src/track/remote_video_track.dart';
42
import 'package:meta/meta.dart';
53

64
import '../constants.dart';
@@ -10,7 +8,9 @@ import '../logger.dart';
108
import '../managers/event.dart';
119
import '../proto/livekit_models.pb.dart' as lk_models;
1210
import '../rtc_engine.dart';
11+
import '../track/remote_audio_track.dart';
1312
import '../track/remote_track_publication.dart';
13+
import '../track/remote_video_track.dart';
1414
import '../track/track.dart';
1515
import '../types.dart';
1616
import 'participant.dart';
@@ -91,10 +91,10 @@ class RemoteParticipant extends Participant {
9191
final Track track;
9292
if (pub.kind == lk_models.TrackType.AUDIO) {
9393
// audio track
94-
track = RemoteAudioTrack(pub.name, mediaTrack, stream);
94+
track = RemoteAudioTrack(pub.source, pub.name, mediaTrack, stream);
9595
} else {
9696
// video track
97-
track = RemoteVideoTrack(pub.name, mediaTrack, stream);
97+
track = RemoteVideoTrack(pub.source, pub.name, mediaTrack, stream);
9898
}
9999

100100
await track.start();

lib/src/proto/livekit_models.pbenum.dart

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/src/proto/livekit_models.pbjson.dart

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/src/rtc_engine.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ class RTCEngine extends Disposable with EventsEmittable<EngineEvent> {
145145
required String cid,
146146
required String name,
147147
required lk_models.TrackType kind,
148+
required lk_models.TrackSource source,
148149
TrackDimension? dimension,
149150
bool? dtx,
150151
}) async {
@@ -155,6 +156,7 @@ class RTCEngine extends Disposable with EventsEmittable<EngineEvent> {
155156
cid: cid,
156157
name: name,
157158
type: kind,
159+
source: source,
158160
dimension: dimension,
159161
dtx: dtx,
160162
);

lib/src/signal_client.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,13 +151,15 @@ class SignalClient extends Disposable with EventsEmittable<SignalEvent> {
151151
required String cid,
152152
required String name,
153153
required lk_models.TrackType type,
154+
required lk_models.TrackSource source,
154155
TrackDimension? dimension,
155156
bool? dtx,
156157
}) {
157158
final req = lk_rtc.AddTrackRequest(
158159
cid: cid,
159160
name: name,
160161
type: type,
162+
source: source,
161163
);
162164

163165
if (type == lk_models.TrackType.VIDEO && dimension != null) {

lib/src/track/audio_track.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import 'package:synchronized/synchronized.dart' as sync;
88
import '../logger.dart';
99
import '../proto/livekit_models.pb.dart' as lk_models;
1010
import '../support/native_audio.dart';
11-
11+
import '../types.dart';
1212
import 'local_audio_track.dart';
1313
import 'track.dart';
1414

@@ -35,11 +35,13 @@ abstract class AudioTrack extends Track {
3535
rtc.MediaStream? mediaStream;
3636

3737
AudioTrack(
38+
TrackSource source,
3839
String name,
3940
rtc.MediaStreamTrack track,
4041
this.mediaStream,
4142
) : super(
4243
lk_models.TrackType.AUDIO,
44+
source,
4345
name,
4446
track,
4547
);

lib/src/track/local_audio_track.dart

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,18 @@ import 'package:flutter_webrtc/flutter_webrtc.dart' as rtc;
44

55
import '../exceptions.dart';
66
import '../logger.dart';
7+
import '../types.dart';
78
import 'audio_track.dart';
89
import 'options.dart';
910

1011
class LocalAudioTrack extends AudioTrack {
1112
// private constructor
1213
LocalAudioTrack._(
14+
TrackSource source,
1315
String name,
1416
rtc.MediaStreamTrack track,
1517
rtc.MediaStream stream,
16-
) : super(name, track, stream);
18+
) : super(source, name, track, stream);
1719

1820
/// Creates a new audio track from the default audio input device.
1921
static Future<LocalAudioTrack> create(
@@ -31,7 +33,12 @@ class LocalAudioTrack extends AudioTrack {
3133

3234
if (stream.getAudioTracks().isEmpty) throw TrackCreateException();
3335

34-
return LocalAudioTrack._('', stream.getAudioTracks().first, stream);
36+
return LocalAudioTrack._(
37+
TrackSource.microphone,
38+
'',
39+
stream.getAudioTracks().first,
40+
stream,
41+
);
3542
}
3643

3744
@override

0 commit comments

Comments
 (0)