Apply Flutter best practices [Non-Breaking] (#2)
* Add flutter_lints package * Use key in widget constructors lint: use_key_in_widget_constructors * Unnecessary new keyword lint: unnecessary_new * Prefer const with constant constructors * Exclude protobuf files from analyzer * Private field could be final * Annotate overridden members * Use initializing formals when possible * Don't access members with `this` unless avoiding shadowing * Prefer is! operator * Use isEmpty instead of length * Use collection literals when possible * Revert comment changes * Cleaner null testing * Misc fixes * Avoid using `forEach` with a function literal * Prefer `final` over `var` where applicable * Enforce stricter type-checking * Ignore VS Code files * Flutter format * Prefer using lowerCamelCase for constant names * flutter format -l 100 * Disable `avoid_print` for examples * Slight improvements to example Should solve lint error: no_logic_in_create_state
This commit is contained in:
@@ -13,7 +13,7 @@ import 'participant.dart';
|
||||
|
||||
/// Represents the current participant in the room.
|
||||
class LocalParticipant extends Participant {
|
||||
RTCEngine _engine;
|
||||
final RTCEngine _engine;
|
||||
|
||||
LocalParticipant({
|
||||
required RTCEngine engine,
|
||||
@@ -29,15 +29,14 @@ class LocalParticipant extends Participant {
|
||||
|
||||
/// publish an audio track to the room
|
||||
Future<TrackPublication> publishAudioTrack(LocalAudioTrack track) async {
|
||||
if (audioTracks.values.any(
|
||||
(element) => element.track?.mediaTrack.id == track.mediaTrack.id)) {
|
||||
if (audioTracks.values.any((element) => element.track?.mediaTrack.id == track.mediaTrack.id)) {
|
||||
return Future.error(TrackPublishError('track already exists'));
|
||||
}
|
||||
|
||||
try {
|
||||
var trackInfo = await _engine.addTrack(
|
||||
cid: track.getCid(), name: track.name, kind: track.kind);
|
||||
var transceiverInit = new RTCRtpTransceiverInit(
|
||||
final trackInfo =
|
||||
await _engine.addTrack(cid: track.getCid(), name: track.name, kind: track.kind);
|
||||
final transceiverInit = RTCRtpTransceiverInit(
|
||||
direction: TransceiverDirection.SendOnly,
|
||||
);
|
||||
// addTransceiver cannot pass in a kind parameter due to a bug in flutter-webrtc (web)
|
||||
@@ -46,7 +45,7 @@ class LocalParticipant extends Participant {
|
||||
init: transceiverInit,
|
||||
);
|
||||
|
||||
var pub = new LocalTrackPublication(trackInfo, track, this);
|
||||
final pub = LocalTrackPublication(trackInfo, track, this);
|
||||
addTrackPublication(pub);
|
||||
notifyListeners();
|
||||
|
||||
@@ -58,15 +57,14 @@ class LocalParticipant extends Participant {
|
||||
|
||||
/// Publish a video track to the room
|
||||
Future<TrackPublication> publishVideoTrack(LocalVideoTrack track) async {
|
||||
if (videoTracks.values.any(
|
||||
(element) => element.track?.mediaTrack.id == track.mediaTrack.id)) {
|
||||
if (videoTracks.values.any((element) => element.track?.mediaTrack.id == track.mediaTrack.id)) {
|
||||
return Future.error(TrackPublishError('track already exists'));
|
||||
}
|
||||
|
||||
try {
|
||||
var trackInfo = await _engine.addTrack(
|
||||
cid: track.getCid(), name: track.name, kind: track.kind);
|
||||
var transceiverInit = new RTCRtpTransceiverInit(
|
||||
final trackInfo =
|
||||
await _engine.addTrack(cid: track.getCid(), name: track.name, kind: track.kind);
|
||||
final transceiverInit = RTCRtpTransceiverInit(
|
||||
direction: TransceiverDirection.SendOnly,
|
||||
);
|
||||
// TODO: video encodings and simulcasts
|
||||
@@ -76,7 +74,7 @@ class LocalParticipant extends Participant {
|
||||
init: transceiverInit,
|
||||
);
|
||||
|
||||
var pub = new LocalTrackPublication(trackInfo, track, this);
|
||||
final pub = LocalTrackPublication(trackInfo, track, this);
|
||||
addTrackPublication(pub);
|
||||
notifyListeners();
|
||||
|
||||
@@ -87,15 +85,15 @@ class LocalParticipant extends Participant {
|
||||
}
|
||||
|
||||
/// Unpublish a track that's already published
|
||||
unpublishTrack(Track track) {
|
||||
var existing = tracks.values.where((element) => element.track == track);
|
||||
void unpublishTrack(Track track) {
|
||||
final existing = tracks.values.where((element) => element.track == track);
|
||||
if (existing.isEmpty) {
|
||||
return;
|
||||
}
|
||||
var pub = existing.first;
|
||||
final pub = existing.first;
|
||||
|
||||
track.stop();
|
||||
var sender = track.transceiver?.sender;
|
||||
final sender = track.transceiver?.sender;
|
||||
if (sender != null) {
|
||||
engine.publisher?.pc.removeTrack(sender);
|
||||
}
|
||||
@@ -108,13 +106,14 @@ class LocalParticipant extends Participant {
|
||||
case TrackType.VIDEO:
|
||||
videoTracks.remove(pub.sid);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/// Publish a new data payload to the room.
|
||||
/// @param destinationSids When empty, data will be forwarded to each participant in the room.
|
||||
publishData(List<int> data, DataPacket_Kind reliability,
|
||||
{List<String>? destinationSids}) {
|
||||
void publishData(List<int> data, DataPacket_Kind reliability, {List<String>? destinationSids}) {
|
||||
RTCDataChannel? channel;
|
||||
switch (reliability) {
|
||||
case DataPacket_Kind.RELIABLE:
|
||||
@@ -128,23 +127,23 @@ class LocalParticipant extends Participant {
|
||||
return;
|
||||
}
|
||||
|
||||
var packet = new DataPacket(
|
||||
final packet = DataPacket(
|
||||
kind: reliability,
|
||||
user: new UserPacket(
|
||||
user: UserPacket(
|
||||
payload: data,
|
||||
participantSid: sid,
|
||||
destinationSids: destinationSids,
|
||||
),
|
||||
);
|
||||
|
||||
var buffer = packet.writeToBuffer();
|
||||
final buffer = packet.writeToBuffer();
|
||||
channel.send(RTCDataChannelMessage.fromBinary(buffer));
|
||||
}
|
||||
|
||||
/// for internal use
|
||||
/// {@nodoc}
|
||||
@override
|
||||
updateFromInfo(ParticipantInfo info) {
|
||||
void updateFromInfo(ParticipantInfo info) {
|
||||
super.updateFromInfo(info);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,29 +21,26 @@ mixin ParticipantDelegate {
|
||||
void onTrackUnmuted(Participant participant, TrackPublication publication) {}
|
||||
|
||||
/// This participant has published a new [Track] to the [Room].
|
||||
void onTrackPublished(
|
||||
RemoteParticipant participant, RemoteTrackPublication publication) {}
|
||||
void onTrackPublished(RemoteParticipant participant, RemoteTrackPublication publication) {}
|
||||
|
||||
/// This participant has unpublished one of their [Track].
|
||||
void onTrackUnpublished(
|
||||
RemoteParticipant participant, RemoteTrackPublication publication) {}
|
||||
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) {}
|
||||
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) {}
|
||||
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) {}
|
||||
void onTrackSubscriptionFailed(RemoteParticipant participant, String sid, String? message) {}
|
||||
}
|
||||
|
||||
/// Represents a Participant in the room, notifies changes via delegates as
|
||||
@@ -85,10 +82,9 @@ class Participant extends ChangeNotifier {
|
||||
|
||||
/// when the participant joined the room
|
||||
DateTime get joinedAt {
|
||||
var pi = _participantInfo;
|
||||
final pi = _participantInfo;
|
||||
if (pi != null) {
|
||||
return DateTime.fromMillisecondsSinceEpoch(pi.joinedAt.toInt() * 1000,
|
||||
isUtc: true);
|
||||
return DateTime.fromMillisecondsSinceEpoch(pi.joinedAt.toInt() * 1000, isUtc: true);
|
||||
}
|
||||
return DateTime.now();
|
||||
}
|
||||
@@ -111,7 +107,7 @@ class Participant extends ChangeNotifier {
|
||||
/// tracks that are subscribed to
|
||||
List<TrackPublication> get subscribedTracks {
|
||||
List<TrackPublication> result = [];
|
||||
for (var track in tracks.values) {
|
||||
for (final track in tracks.values) {
|
||||
if (track.subscribed) {
|
||||
result.add(track);
|
||||
}
|
||||
@@ -140,9 +136,9 @@ class Participant extends ChangeNotifier {
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
_setMetadata(String md) {
|
||||
var changed = this._participantInfo?.metadata != md;
|
||||
this.metadata = md;
|
||||
void _setMetadata(String md) {
|
||||
final changed = _participantInfo?.metadata != md;
|
||||
metadata = md;
|
||||
if (changed) {
|
||||
delegate?.onMetadataChanged(this);
|
||||
roomDelegate?.onMetadataChanged(this);
|
||||
@@ -152,24 +148,24 @@ class Participant extends ChangeNotifier {
|
||||
|
||||
/// for internal use
|
||||
/// {@nodoc}
|
||||
updateFromInfo(ParticipantInfo info) {
|
||||
this.identity = info.identity;
|
||||
this.sid = info.sid;
|
||||
void updateFromInfo(ParticipantInfo info) {
|
||||
identity = info.identity;
|
||||
sid = info.sid;
|
||||
if (info.metadata.isNotEmpty) {
|
||||
_setMetadata(info.metadata);
|
||||
}
|
||||
this._participantInfo = info;
|
||||
_participantInfo = info;
|
||||
}
|
||||
|
||||
/// for internal use
|
||||
/// {@nodoc}
|
||||
muteChanged() {
|
||||
void muteChanged() {
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
/// for internal use
|
||||
/// {@nodoc}
|
||||
addTrackPublication(TrackPublication pub) {
|
||||
void addTrackPublication(TrackPublication pub) {
|
||||
pub.track?.sid = pub.sid;
|
||||
tracks[pub.sid] = pub;
|
||||
switch (pub.kind) {
|
||||
|
||||
@@ -9,20 +9,18 @@ import 'participant.dart';
|
||||
|
||||
/// Represents other participant in the [Room].
|
||||
class RemoteParticipant extends Participant {
|
||||
SignalClient _client;
|
||||
final SignalClient _client;
|
||||
|
||||
SignalClient get client => _client;
|
||||
|
||||
RemoteParticipant(this._client, String sid, String identity)
|
||||
: super(sid, identity);
|
||||
RemoteParticipant(this._client, String sid, String identity) : super(sid, identity);
|
||||
|
||||
RemoteParticipant.fromInfo(this._client, ParticipantInfo info)
|
||||
: super(info.sid, info.identity) {
|
||||
RemoteParticipant.fromInfo(this._client, ParticipantInfo info) : super(info.sid, info.identity) {
|
||||
updateFromInfo(info);
|
||||
}
|
||||
|
||||
RemoteTrackPublication? getTrackPublication(String sid) {
|
||||
var pub = tracks[sid];
|
||||
final pub = tracks[sid];
|
||||
if (pub is RemoteTrackPublication) {
|
||||
return pub;
|
||||
}
|
||||
@@ -30,10 +28,9 @@ class RemoteParticipant extends Participant {
|
||||
|
||||
/// for internal use
|
||||
/// {@nodoc}
|
||||
addSubscribedMediaTrack(
|
||||
MediaStreamTrack mediaTrack, MediaStream stream, String? sid) async {
|
||||
void addSubscribedMediaTrack(MediaStreamTrack mediaTrack, MediaStream stream, String? sid) async {
|
||||
if (sid == null) {
|
||||
var msg = 'addSubscribedMediaTrack received null sid';
|
||||
const msg = 'addSubscribedMediaTrack received null sid';
|
||||
delegate?.onTrackSubscriptionFailed(this, '', msg);
|
||||
roomDelegate?.onTrackSubscriptionFailed(this, '', msg);
|
||||
return;
|
||||
@@ -42,9 +39,9 @@ class RemoteParticipant extends Participant {
|
||||
var pub = getTrackPublication(sid);
|
||||
if (pub == null) {
|
||||
// we may have received the track prior to metadata. wait up to 3s
|
||||
pub = await _waitForTrackPublication(sid, Duration(seconds: 3));
|
||||
pub = await _waitForTrackPublication(sid, const Duration(seconds: 3));
|
||||
if (pub == null) {
|
||||
var msg = 'no track metadata found';
|
||||
const msg = 'no track metadata found';
|
||||
delegate?.onTrackSubscriptionFailed(this, sid, msg);
|
||||
roomDelegate?.onTrackSubscriptionFailed(this, sid, msg);
|
||||
return;
|
||||
@@ -53,13 +50,13 @@ class RemoteParticipant extends Participant {
|
||||
|
||||
Track? track;
|
||||
if (pub.kind == TrackType.AUDIO) {
|
||||
var audioTrack = new AudioTrack(pub.name, mediaTrack, stream);
|
||||
final audioTrack = AudioTrack(pub.name, mediaTrack, stream);
|
||||
audioTrack.start();
|
||||
track = audioTrack;
|
||||
} else if (pub.kind == TrackType.VIDEO) {
|
||||
track = new VideoTrack(pub.name, mediaTrack, stream);
|
||||
track = VideoTrack(pub.name, mediaTrack, stream);
|
||||
} else {
|
||||
var msg = 'unsupported track type ${pub.kind}';
|
||||
final msg = 'unsupported track type ${pub.kind}';
|
||||
delegate?.onTrackSubscriptionFailed(this, sid, msg);
|
||||
roomDelegate?.onTrackSubscriptionFailed(this, sid, msg);
|
||||
return;
|
||||
@@ -77,15 +74,15 @@ class RemoteParticipant extends Participant {
|
||||
/// {@nodoc}
|
||||
@override
|
||||
void updateFromInfo(ParticipantInfo info) {
|
||||
var hadInfo = hasInfo;
|
||||
final hadInfo = hasInfo;
|
||||
super.updateFromInfo(info);
|
||||
|
||||
// figuring out deltas between tracks
|
||||
var validPubs = Map<String, RemoteTrackPublication>();
|
||||
var newPubs = Map<String, RemoteTrackPublication>();
|
||||
final validPubs = <String, RemoteTrackPublication>{};
|
||||
final newPubs = <String, RemoteTrackPublication>{};
|
||||
|
||||
for (var info in info.tracks) {
|
||||
var sid = info.sid;
|
||||
for (final info in info.tracks) {
|
||||
final sid = info.sid;
|
||||
var pub = getTrackPublication(sid);
|
||||
|
||||
if (pub == null) {
|
||||
@@ -101,30 +98,30 @@ class RemoteParticipant extends Participant {
|
||||
|
||||
// notify listeners when it's not a new participant
|
||||
if (hadInfo) {
|
||||
for (var pub in newPubs.values) {
|
||||
for (final pub in newPubs.values) {
|
||||
delegate?.onTrackPublished(this, pub);
|
||||
roomDelegate?.onTrackPublished(this, pub);
|
||||
}
|
||||
}
|
||||
|
||||
// remove tracks
|
||||
for (var pub in tracks.values) {
|
||||
for (final pub in tracks.values) {
|
||||
if (!validPubs.containsKey(pub.sid)) {
|
||||
unpublishTrack(sid, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
unpublishTrack(String sid, [bool sendUnpublish = false]) {
|
||||
var pub = tracks.remove(sid);
|
||||
if (pub == null || !(pub is RemoteTrackPublication)) {
|
||||
void unpublishTrack(String sid, [bool sendUnpublish = false]) {
|
||||
final pub = tracks.remove(sid);
|
||||
if (pub == null || pub is! RemoteTrackPublication) {
|
||||
return;
|
||||
}
|
||||
|
||||
audioTracks.remove(sid);
|
||||
videoTracks.remove(sid);
|
||||
|
||||
var track = pub.track;
|
||||
final track = pub.track;
|
||||
if (track != null) {
|
||||
track.stop();
|
||||
delegate?.onTrackUnsubscribed(this, track, pub);
|
||||
@@ -137,12 +134,11 @@ class RemoteParticipant extends Participant {
|
||||
}
|
||||
}
|
||||
|
||||
Future<RemoteTrackPublication?> _waitForTrackPublication(
|
||||
String sid, Duration delay) async {
|
||||
var endTime = DateTime.now().add(delay);
|
||||
Future<RemoteTrackPublication?> _waitForTrackPublication(String sid, Duration delay) async {
|
||||
final endTime = DateTime.now().add(delay);
|
||||
while (DateTime.now().isBefore(endTime)) {
|
||||
var pub = await Future<RemoteTrackPublication?>.delayed(
|
||||
Duration(milliseconds: 100), () {
|
||||
final pub =
|
||||
await Future<RemoteTrackPublication?>.delayed(const Duration(milliseconds: 100), () {
|
||||
return getTrackPublication(sid);
|
||||
});
|
||||
if (pub != null) {
|
||||
|
||||
Reference in New Issue
Block a user