Simpler APIs (#28)

* TrackSource

* remove absolute import

* typo

* get track

* set source enabled

* fix symbol collision

* move to participant

* update proto

* screen share audio

* typo

* format

* change screen share name
This commit is contained in:
Hiroshi Horie
2021-11-11 00:20:32 +09:00
committed by GitHub
parent abae7a7ff7
commit 1095e25113
19 changed files with 159 additions and 19 deletions
@@ -58,6 +58,7 @@ class LocalParticipant extends Participant {
cid: track.getCid(),
name: track.name,
kind: track.kind,
source: track.source.toPBType(),
dtx: options?.dtx,
);
@@ -102,6 +103,7 @@ class LocalParticipant extends Participant {
cid: track.getCid(),
name: track.name,
kind: track.kind,
source: track.source.toPBType(),
);
logger.fine('publishVideoTrack addTrack response: ${trackInfo}');
@@ -230,3 +232,37 @@ class LocalParticipant extends Participant {
super.updateFromInfo(info);
}
}
extension LocalParticipantTrackSourceExt on LocalParticipant {
Future<void> setCameraEnabled(bool enabled) async {
return setSourceEnabled(TrackSource.camera, enabled);
}
Future<void> setMicrophoneEnabled(bool enabled) async {
return setSourceEnabled(TrackSource.microphone, enabled);
}
Future<void> setSourceEnabled(TrackSource source, bool enabled) async {
final pub = getTrackPublicationBySource(source);
if (pub != null) {
if (enabled) {
pub.muted = false;
} else {
if (source == TrackSource.screenShareVideo) {
await unpublishTrack(pub.sid);
} else {
pub.muted = true;
}
}
} else if (enabled) {
if (source == TrackSource.camera) {
final track = await LocalVideoTrack.createCameraTrack();
await publishVideoTrack(track);
} else if (source == TrackSource.microphone) {
final track = await LocalAudioTrack.create();
await publishAudioTrack(track);
}
// TODO: Screen share
}
}
}
+36
View File
@@ -1,4 +1,5 @@
import 'package:collection/collection.dart';
import 'package:livekit_client/src/track/track.dart';
import 'package:meta/meta.dart';
import '../events.dart';
@@ -187,3 +188,38 @@ extension ParticipantExt on Participant {
.where((e) => e.kind == lk_models.TrackType.AUDIO)
.toList();
}
extension ParticipantTrackSourceExt on Participant {
bool isCameraEnabled() {
return !(getTrackPublicationBySource(TrackSource.camera)?.muted ?? true);
}
bool isMicrophoneEnabled() {
return !(getTrackPublicationBySource(TrackSource.microphone)?.muted ??
true);
}
/// Find a track publication by its [TrackSource]
TrackPublication? getTrackPublicationBySource(TrackSource source) {
if (source == TrackSource.unknown) return null;
// try to find by source
final result =
trackPublications.values.firstWhereOrNull((e) => e.source == source);
if (result != null) return result;
// try to find by compatibility
return trackPublications.values
.where((e) => e.source == TrackSource.unknown)
.firstWhereOrNull((e) =>
(source == TrackSource.microphone &&
e.kind == lk_models.TrackType.AUDIO) ||
(source == TrackSource.camera &&
e.kind == lk_models.TrackType.VIDEO &&
e.name != Track.screenShareName) ||
(source == TrackSource.screenShareVideo &&
e.kind == lk_models.TrackType.VIDEO &&
e.name == Track.screenShareName) ||
(source == TrackSource.screenShareAudio &&
e.kind == lk_models.TrackType.AUDIO &&
e.name == Track.screenShareName));
}
}
+4 -4
View File
@@ -1,6 +1,4 @@
import 'package:flutter_webrtc/flutter_webrtc.dart' as rtc;
import 'package:livekit_client/src/track/remote_audio_track.dart';
import 'package:livekit_client/src/track/remote_video_track.dart';
import 'package:meta/meta.dart';
import '../constants.dart';
@@ -10,7 +8,9 @@ import '../logger.dart';
import '../managers/event.dart';
import '../proto/livekit_models.pb.dart' as lk_models;
import '../rtc_engine.dart';
import '../track/remote_audio_track.dart';
import '../track/remote_track_publication.dart';
import '../track/remote_video_track.dart';
import '../track/track.dart';
import '../types.dart';
import 'participant.dart';
@@ -91,10 +91,10 @@ class RemoteParticipant extends Participant {
final Track track;
if (pub.kind == lk_models.TrackType.AUDIO) {
// audio track
track = RemoteAudioTrack(pub.name, mediaTrack, stream);
track = RemoteAudioTrack(pub.source, pub.name, mediaTrack, stream);
} else {
// video track
track = RemoteVideoTrack(pub.name, mediaTrack, stream);
track = RemoteVideoTrack(pub.source, pub.name, mediaTrack, stream);
}
await track.start();