Switch to new event system (#9)
* `SignalEvents` and `synchronized` mode for `EventsListenable` * cascade syntax * clean up * `RoomEvents` * `ParticipantEvents` * all events implemented * make it compile with M1 macs * dispose logic * cleaner connect logic * ask to publish * fix initial EngineTrackAddedEvent glitch * fix unpublishTrack bug * clean up * better events docs * organize event types * clean up * cleaner wait logic * wait for event instead of using Completer * notifyListeners
This commit is contained in:
@@ -2,15 +2,16 @@ import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter_webrtc/flutter_webrtc.dart' as rtc;
|
||||
|
||||
import '../errors.dart';
|
||||
import '../events.dart';
|
||||
import '../extensions.dart';
|
||||
import '../logger.dart';
|
||||
import '../managers/event.dart';
|
||||
import '../options.dart';
|
||||
import '../proto/livekit_models.pb.dart' as lk_models;
|
||||
import '../rtc_engine.dart';
|
||||
import '../track/local_audio_track.dart';
|
||||
import '../track/local_track_publication.dart';
|
||||
import '../track/local_video_track.dart';
|
||||
import '../track/track.dart';
|
||||
import '../track/track_publication.dart';
|
||||
import '../types.dart';
|
||||
import '../utils.dart';
|
||||
@@ -25,8 +26,13 @@ class LocalParticipant extends Participant {
|
||||
required RTCEngine engine,
|
||||
required lk_models.ParticipantInfo info,
|
||||
this.defaultPublishOptions,
|
||||
required EventsEmitter<RoomEvent> roomEvents,
|
||||
}) : _engine = engine,
|
||||
super(info.sid, info.identity) {
|
||||
super(
|
||||
info.sid,
|
||||
info.identity,
|
||||
roomEvents: roomEvents,
|
||||
) {
|
||||
updateFromInfo(info);
|
||||
}
|
||||
|
||||
@@ -135,21 +141,25 @@ class LocalParticipant extends Participant {
|
||||
}
|
||||
|
||||
/// Unpublish a track that's already published
|
||||
Future<void> unpublishTrack(Track track) async {
|
||||
final existing = tracks.values.where((element) => element.track == track);
|
||||
if (existing.isEmpty) return;
|
||||
@override
|
||||
Future<void> unpublishTrack(String trackSid, {bool notify = false}) async {
|
||||
logger.finer('Unpublish track sid: $trackSid, notify: $notify');
|
||||
final pub = trackPublications.remove(trackSid);
|
||||
if (pub is! LocalTrackPublication) return;
|
||||
|
||||
final pub = existing.first;
|
||||
// final existing = tracks.values.where((element) => element.track == track);
|
||||
// if (existing.isEmpty) return;
|
||||
// final pub = existing.first;
|
||||
final track = pub.track;
|
||||
if (track != null) {
|
||||
await track.stop();
|
||||
|
||||
await track.stop();
|
||||
|
||||
final sender = track.transceiver?.sender;
|
||||
if (sender != null) {
|
||||
await engine.publisher?.pc.removeTrack(sender);
|
||||
await engine.negotiate();
|
||||
final sender = track.transceiver?.sender;
|
||||
if (sender != null) {
|
||||
await engine.publisher?.pc.removeTrack(sender);
|
||||
await engine.negotiate();
|
||||
}
|
||||
}
|
||||
|
||||
tracks.remove(pub.sid);
|
||||
}
|
||||
|
||||
/// Publish a new data payload to the room.
|
||||
|
||||
@@ -1,50 +1,16 @@
|
||||
import 'package:collection/collection.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:meta/meta.dart';
|
||||
|
||||
import '../classes/change_notifier.dart';
|
||||
import '../events.dart';
|
||||
import '../extensions.dart';
|
||||
import '../logger.dart';
|
||||
import '../managers/event.dart';
|
||||
import '../proto/livekit_models.pb.dart' as lk_models;
|
||||
import '../track/remote_track_publication.dart';
|
||||
import '../track/track.dart';
|
||||
import '../track/track_publication.dart';
|
||||
import 'remote_participant.dart';
|
||||
|
||||
/// Callbacks for participant changes
|
||||
mixin ParticipantDelegate {
|
||||
/// The participant's metadata has changed
|
||||
void onMetadataChanged(Participant participant) {}
|
||||
|
||||
/// The participant's isSpeaking property has changed
|
||||
void onSpeakingChanged(Participant participant, bool speaking) {}
|
||||
|
||||
/// This participant has muted one of their tracks
|
||||
void onTrackMuted(Participant participant, TrackPublication publication) {}
|
||||
|
||||
/// This participant has unmuted one of their tracks
|
||||
void onTrackUnmuted(Participant participant, TrackPublication publication) {}
|
||||
|
||||
/// This participant has published a new [Track] to the [Room].
|
||||
void onTrackPublished(RemoteParticipant participant, RemoteTrackPublication publication) {}
|
||||
|
||||
/// This participant has unpublished one of their [Track].
|
||||
void onTrackUnpublished(RemoteParticipant participant, RemoteTrackPublication publication) {}
|
||||
|
||||
/// The [LocalParticipant] has subscribed to a new track published by this
|
||||
/// [RemoteParticipant]
|
||||
void onTrackSubscribed(
|
||||
RemoteParticipant participant, Track track, RemoteTrackPublication publication) {}
|
||||
|
||||
/// The [LocalParticipant] has unsubscribed from a track published by this
|
||||
/// [RemoteParticipant]. This event is fired when the track was unpublished
|
||||
void onTrackUnsubscribed(
|
||||
RemoteParticipant participant, Track track, RemoteTrackPublication publication) {}
|
||||
|
||||
/// Data received from this [RemoteParticipant].
|
||||
void onDataReceived(RemoteParticipant participant, List<int> data) {}
|
||||
|
||||
/// An error has occured during track subscription.
|
||||
void onTrackSubscriptionFailed(RemoteParticipant participant, String sid, String? message) {}
|
||||
}
|
||||
|
||||
/// Represents a Participant in the room, notifies changes via delegates as
|
||||
/// well as ChangeNotifier/providers.
|
||||
/// A change notification is triggered when
|
||||
@@ -52,15 +18,18 @@ mixin ParticipantDelegate {
|
||||
/// - mute status changed
|
||||
/// - added/removed subscribed tracks
|
||||
/// - metadata changed
|
||||
class Participant extends ChangeNotifier {
|
||||
|
||||
/// Base for [RemoteParticipant] and [LocalParticipant],
|
||||
/// can not be instantiated directly.
|
||||
abstract class Participant extends LKChangeNotifier {
|
||||
/// map of track sid => published track
|
||||
Map<String, TrackPublication> tracks = {};
|
||||
final trackPublications = <String, TrackPublication>{};
|
||||
|
||||
/// audio level between 0-1, 1 being the loudest
|
||||
double audioLevel = 0;
|
||||
|
||||
/// server assigned unique id
|
||||
String sid;
|
||||
final String sid;
|
||||
|
||||
/// user-assigned identity
|
||||
String identity;
|
||||
@@ -71,16 +40,12 @@ class Participant extends ChangeNotifier {
|
||||
/// when the participant had last spoken
|
||||
DateTime? lastSpokeAt;
|
||||
|
||||
ParticipantDelegate? roomDelegate;
|
||||
|
||||
/// delegate to receive participant callbacks
|
||||
ParticipantDelegate? delegate;
|
||||
|
||||
lk_models.ParticipantInfo? _participantInfo;
|
||||
bool _isSpeaking = false;
|
||||
|
||||
// suppport for multiple event listeners
|
||||
final events = EventsEmitter<ParticipantEvent>();
|
||||
final EventsEmitter<RoomEvent> roomEvents;
|
||||
|
||||
/// when the participant joined the room
|
||||
DateTime get joinedAt {
|
||||
@@ -95,23 +60,40 @@ class Participant extends ChangeNotifier {
|
||||
bool get isSpeaking => _isSpeaking;
|
||||
|
||||
/// true if participant is publishing an audio track and is muted
|
||||
bool get isMuted {
|
||||
if (audioTracks.isEmpty) return false;
|
||||
return audioTracks.first.muted;
|
||||
}
|
||||
bool get isMuted => audioTracks.firstOrNull?.muted ?? true;
|
||||
|
||||
bool get hasAudio => audioTracks.isNotEmpty;
|
||||
|
||||
bool get hasVideo => videoTracks.isNotEmpty;
|
||||
|
||||
/// tracks that are subscribed to
|
||||
List<TrackPublication> get subscribedTracks => tracks.values.where((e) => e.subscribed).toList();
|
||||
List<TrackPublication> get subscribedTracks =>
|
||||
trackPublications.values.where((e) => e.subscribed).toList();
|
||||
|
||||
/// for internal use
|
||||
/// {@nodoc}
|
||||
@internal
|
||||
bool get hasInfo => _participantInfo != null;
|
||||
|
||||
Participant(this.sid, this.identity);
|
||||
Participant(
|
||||
this.sid,
|
||||
this.identity, {
|
||||
required this.roomEvents,
|
||||
}) {
|
||||
// Any event emitted will trigger ChangeNotifier
|
||||
events.listen((event) {
|
||||
logger.fine('[ParticipantEvent] $event, will notifyListeners()');
|
||||
notifyListeners();
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
@mustCallSuper
|
||||
Future<void> dispose() async {
|
||||
logger.fine('$objectId dispose()');
|
||||
await events.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
/// for internal use
|
||||
/// {@nodoc}
|
||||
@@ -123,26 +105,29 @@ class Participant extends ChangeNotifier {
|
||||
if (speaking) {
|
||||
lastSpokeAt = DateTime.now();
|
||||
}
|
||||
delegate?.onSpeakingChanged(this, speaking);
|
||||
roomDelegate?.onSpeakingChanged(this, speaking);
|
||||
notifyListeners();
|
||||
|
||||
[events, roomEvents].emit(SpeakingChangedEvent(
|
||||
participant: this,
|
||||
speaking: speaking,
|
||||
));
|
||||
}
|
||||
|
||||
void _setMetadata(String md) {
|
||||
final changed = _participantInfo?.metadata != md;
|
||||
metadata = md;
|
||||
if (changed) {
|
||||
delegate?.onMetadataChanged(this);
|
||||
roomDelegate?.onMetadataChanged(this);
|
||||
notifyListeners();
|
||||
[events, roomEvents].emit(ParticipantMetadataUpdatedEvent(
|
||||
participant: this,
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
/// for internal use
|
||||
/// {@nodoc}
|
||||
@internal
|
||||
void updateFromInfo(lk_models.ParticipantInfo info) {
|
||||
identity = info.identity;
|
||||
sid = info.sid;
|
||||
// participantSid = info.sid;
|
||||
if (info.metadata.isNotEmpty) {
|
||||
_setMetadata(info.metadata);
|
||||
}
|
||||
@@ -151,23 +136,36 @@ class Participant extends ChangeNotifier {
|
||||
|
||||
/// for internal use
|
||||
/// {@nodoc}
|
||||
void muteChanged() {
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
/// for internal use
|
||||
/// {@nodoc}
|
||||
@internal
|
||||
void addTrackPublication(TrackPublication pub) {
|
||||
pub.track?.sid = pub.sid;
|
||||
tracks[pub.sid] = pub;
|
||||
trackPublications[pub.sid] = pub;
|
||||
}
|
||||
|
||||
// Must implement
|
||||
Future<void> unpublishTrack(String trackSid, {bool notify = false});
|
||||
|
||||
Future<void> unpublishAllTracks() async {
|
||||
final _ = List<TrackPublication>.from(trackPublications.values);
|
||||
for (final track in _) {
|
||||
await unpublishTrack(track.sid);
|
||||
}
|
||||
}
|
||||
|
||||
// Equality operators
|
||||
// Object is considered equal when sid is equal
|
||||
@override
|
||||
int get hashCode => sid.hashCode;
|
||||
|
||||
@override
|
||||
bool operator ==(Object other) => other is Participant && sid == other.sid;
|
||||
}
|
||||
|
||||
// Convenience extension
|
||||
extension ParticipantExt on Participant {
|
||||
List<TrackPublication> get videoTracks =>
|
||||
tracks.values.where((e) => e.kind == lk_models.TrackType.VIDEO).toList();
|
||||
trackPublications.values.where((e) => e.kind == lk_models.TrackType.VIDEO).toList();
|
||||
|
||||
List<TrackPublication> get audioTracks =>
|
||||
tracks.values.where((e) => e.kind == lk_models.TrackType.AUDIO).toList();
|
||||
trackPublications.values.where((e) => e.kind == lk_models.TrackType.AUDIO).toList();
|
||||
}
|
||||
|
||||
@@ -1,12 +1,18 @@
|
||||
import 'package:flutter_webrtc/flutter_webrtc.dart' as rtc;
|
||||
import 'package:meta/meta.dart';
|
||||
|
||||
import '../constants.dart';
|
||||
import '../events.dart';
|
||||
import '../extensions.dart';
|
||||
import '../logger.dart';
|
||||
import '../managers/event.dart';
|
||||
import '../proto/livekit_models.pb.dart' as lk_models;
|
||||
import '../signal_client.dart';
|
||||
import '../track/audio_track.dart';
|
||||
import '../track/remote_track_publication.dart';
|
||||
import '../track/track.dart';
|
||||
import '../track/video_track.dart';
|
||||
import '../types.dart';
|
||||
import 'participant.dart';
|
||||
|
||||
/// Represents other participant in the [Room].
|
||||
@@ -18,140 +24,154 @@ class RemoteParticipant extends Participant {
|
||||
RemoteParticipant(
|
||||
this._client,
|
||||
String sid,
|
||||
String identity,
|
||||
) : super(sid, identity);
|
||||
String identity, {
|
||||
required EventsEmitter<RoomEvent> roomEvents,
|
||||
}) : super(
|
||||
sid,
|
||||
identity,
|
||||
roomEvents: roomEvents,
|
||||
);
|
||||
|
||||
RemoteParticipant.fromInfo(
|
||||
this._client,
|
||||
lk_models.ParticipantInfo info,
|
||||
) : super(info.sid, info.identity) {
|
||||
lk_models.ParticipantInfo info, {
|
||||
required EventsEmitter<RoomEvent> roomEvents,
|
||||
}) : super(
|
||||
info.sid,
|
||||
info.identity,
|
||||
roomEvents: roomEvents,
|
||||
) {
|
||||
updateFromInfo(info);
|
||||
}
|
||||
|
||||
RemoteTrackPublication? getTrackPublication(String sid) {
|
||||
final pub = tracks[sid];
|
||||
final pub = trackPublications[sid];
|
||||
if (pub is RemoteTrackPublication) return pub;
|
||||
}
|
||||
|
||||
/// for internal use
|
||||
/// {@nodoc}
|
||||
void addSubscribedMediaTrack(
|
||||
@internal
|
||||
Future<void> addSubscribedMediaTrack(
|
||||
rtc.MediaStreamTrack mediaTrack,
|
||||
rtc.MediaStream stream,
|
||||
String? sid,
|
||||
String trackSid,
|
||||
) async {
|
||||
if (sid == null) {
|
||||
const msg = 'addSubscribedMediaTrack received null sid';
|
||||
delegate?.onTrackSubscriptionFailed(this, '', msg);
|
||||
roomDelegate?.onTrackSubscriptionFailed(this, '', msg);
|
||||
return;
|
||||
}
|
||||
logger.fine('addSubscribedMediaTrack()');
|
||||
|
||||
var pub = getTrackPublication(sid);
|
||||
// If publication doesn't exist yet...
|
||||
RemoteTrackPublication? pub = getTrackPublication(trackSid);
|
||||
if (pub == null) {
|
||||
// we may have received the track prior to metadata. wait up to 3s
|
||||
pub = await _waitForTrackPublication(sid, const Duration(seconds: 3));
|
||||
if (pub == null) {
|
||||
const msg = 'no track metadata found';
|
||||
delegate?.onTrackSubscriptionFailed(this, sid, msg);
|
||||
roomDelegate?.onTrackSubscriptionFailed(this, sid, msg);
|
||||
return;
|
||||
}
|
||||
logger.fine('addSubscribedMediaTrack() pub is null, will wait...');
|
||||
// Wait for the metadata to arrive
|
||||
final event = await events.waitFor<TrackPublishedEvent>(
|
||||
filter: (event) => event.participant == this && event.publication.sid == trackSid,
|
||||
duration: Timeouts.publish,
|
||||
onTimeout: () => throw TrackSubscriptionExceptionEvent(
|
||||
participant: this,
|
||||
sid: trackSid,
|
||||
reason: TrackSubscribeFailReason.notTrackMetadataFound,
|
||||
),
|
||||
);
|
||||
pub = event.publication;
|
||||
logger.fine('addSubscribedMediaTrack() did receive pub');
|
||||
}
|
||||
|
||||
Track? track;
|
||||
// Check if track type is supported, throw if not.
|
||||
if (![lk_models.TrackType.AUDIO, lk_models.TrackType.VIDEO].contains(pub.kind)) {
|
||||
throw TrackSubscriptionExceptionEvent(
|
||||
participant: this,
|
||||
sid: trackSid,
|
||||
reason: TrackSubscribeFailReason.unsupportedTrackType,
|
||||
);
|
||||
}
|
||||
|
||||
// create Track
|
||||
final Track track;
|
||||
if (pub.kind == lk_models.TrackType.AUDIO) {
|
||||
// audio track
|
||||
final audioTrack = AudioTrack(pub.name, mediaTrack, stream);
|
||||
audioTrack.start();
|
||||
track = audioTrack;
|
||||
} else if (pub.kind == lk_models.TrackType.VIDEO) {
|
||||
track = VideoTrack(pub.name, mediaTrack, stream);
|
||||
} else {
|
||||
final msg = 'unsupported track type ${pub.kind}';
|
||||
delegate?.onTrackSubscriptionFailed(this, sid, msg);
|
||||
roomDelegate?.onTrackSubscriptionFailed(this, sid, msg);
|
||||
return;
|
||||
// video track
|
||||
track = VideoTrack(pub.name, mediaTrack, stream);
|
||||
}
|
||||
|
||||
pub.track = track;
|
||||
addTrackPublication(pub);
|
||||
|
||||
delegate?.onTrackSubscribed(this, track, pub);
|
||||
roomDelegate?.onTrackSubscribed(this, track, pub);
|
||||
notifyListeners();
|
||||
[events, roomEvents].emit(TrackSubscribedEvent(
|
||||
participant: this,
|
||||
track: track,
|
||||
publication: pub,
|
||||
));
|
||||
}
|
||||
|
||||
/// for internal use
|
||||
/// {@nodoc}
|
||||
@override
|
||||
void updateFromInfo(lk_models.ParticipantInfo info) async {
|
||||
@internal
|
||||
Future<void> updateFromInfo(lk_models.ParticipantInfo info) async {
|
||||
final hadInfo = hasInfo;
|
||||
super.updateFromInfo(info);
|
||||
|
||||
// figuring out deltas between tracks
|
||||
final validPubs = <String, RemoteTrackPublication>{};
|
||||
final newPubs = <String, RemoteTrackPublication>{};
|
||||
|
||||
for (final info in info.tracks) {
|
||||
final sid = info.sid;
|
||||
var pub = getTrackPublication(sid);
|
||||
final newPubs = <RemoteTrackPublication>{};
|
||||
|
||||
for (final trackInfo in info.tracks) {
|
||||
RemoteTrackPublication? pub = getTrackPublication(trackInfo.sid);
|
||||
if (pub == null) {
|
||||
pub = RemoteTrackPublication(info, this);
|
||||
newPubs[sid] = pub;
|
||||
pub = RemoteTrackPublication(trackInfo, this);
|
||||
newPubs.add(pub);
|
||||
addTrackPublication(pub);
|
||||
} else {
|
||||
pub.updateFromInfo(info);
|
||||
pub.updateFromInfo(trackInfo);
|
||||
}
|
||||
|
||||
validPubs[sid] = pub;
|
||||
}
|
||||
|
||||
// notify listeners when it's not a new participant
|
||||
if (hadInfo) {
|
||||
for (final pub in newPubs.values) {
|
||||
delegate?.onTrackPublished(this, pub);
|
||||
roomDelegate?.onTrackPublished(this, pub);
|
||||
for (final pub in newPubs) {
|
||||
final event = TrackPublishedEvent(
|
||||
participant: this,
|
||||
publication: pub,
|
||||
);
|
||||
[events, roomEvents].emit(event);
|
||||
}
|
||||
}
|
||||
|
||||
// remove tracks
|
||||
final removeTrackSids =
|
||||
tracks.values.where((e) => !validPubs.containsKey(e.sid)).map((e) => e.sid).toList();
|
||||
|
||||
for (final sid in removeTrackSids) {
|
||||
await unpublishTrack(sid, true);
|
||||
// unpublish any track that is not in the info
|
||||
final validSids = info.tracks.map((e) => e.sid);
|
||||
final removeSids =
|
||||
trackPublications.values.where((e) => !validSids.contains(e.sid)).map((e) => e.sid);
|
||||
for (final sid in removeSids) {
|
||||
await unpublishTrack(sid, notify: true);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> unpublishTrack(String sid, [bool notify = false]) async {
|
||||
logger.finer('Unpublish track sid: $sid, notify: $notify');
|
||||
final pub = tracks.remove(sid);
|
||||
if (pub == null || pub is! RemoteTrackPublication) return;
|
||||
@override
|
||||
Future<void> unpublishTrack(String trackSid, {bool notify = false}) async {
|
||||
logger.finer('Unpublish track sid: $trackSid, notify: $notify');
|
||||
final pub = trackPublications.remove(trackSid);
|
||||
if (pub is! RemoteTrackPublication) return;
|
||||
|
||||
final track = pub.track;
|
||||
// if has track
|
||||
if (track != null) {
|
||||
await track.stop();
|
||||
delegate?.onTrackUnsubscribed(this, track, pub);
|
||||
roomDelegate?.onTrackUnsubscribed(this, track, pub);
|
||||
notifyListeners();
|
||||
[events, roomEvents].emit(TrackUnsubscribedEvent(
|
||||
participant: this,
|
||||
track: track,
|
||||
publication: pub,
|
||||
));
|
||||
}
|
||||
|
||||
if (notify) {
|
||||
delegate?.onTrackUnpublished(this, pub);
|
||||
roomDelegate?.onTrackUnpublished(this, pub);
|
||||
}
|
||||
}
|
||||
|
||||
Future<RemoteTrackPublication?> _waitForTrackPublication(String sid, Duration delay) async {
|
||||
final endTime = DateTime.now().add(delay);
|
||||
while (DateTime.now().isBefore(endTime)) {
|
||||
final pub =
|
||||
await Future<RemoteTrackPublication?>.delayed(const Duration(milliseconds: 100), () {
|
||||
return getTrackPublication(sid);
|
||||
});
|
||||
|
||||
if (pub != null) return pub;
|
||||
[events, roomEvents].emit(TrackUnpublishedEvent(
|
||||
participant: this,
|
||||
publication: pub,
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user