From 3eccf9b56c0536d265d98c30a1bd7742cea12e68 Mon Sep 17 00:00:00 2001 From: cloudwebrtc Date: Mon, 23 Oct 2023 11:29:05 +0800 Subject: [PATCH 1/9] fix: video rendering flickers for safari. --- example/lib/pages/room.dart | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/example/lib/pages/room.dart b/example/lib/pages/room.dart index 95e7cec7a..9823267b1 100644 --- a/example/lib/pages/room.dart +++ b/example/lib/pages/room.dart @@ -68,16 +68,13 @@ class _RoomPageState extends State { WidgetsBindingCompatible.instance ?.addPostFrameCallback((timeStamp) => Navigator.pop(context)); }) - ..on((event) { - print('Participant event'); - // sort participants on many track events as noted in documentation linked above - _sortParticipants(); - }) ..on((event) { context.showRecordingStatusChangedDialog(event.activeRecording); }) ..on((_) => _sortParticipants()) ..on((_) => _sortParticipants()) + ..on((_) => _sortParticipants()) + ..on((_) => _sortParticipants()) ..on(_onE2EEStateEvent) ..on((event) { print( From 7dc614f6cc4897c509f2814df20561bf84577446 Mon Sep 17 00:00:00 2001 From: cloudwebrtc Date: Mon, 23 Oct 2023 15:58:56 +0800 Subject: [PATCH 2/9] update. --- example/lib/pages/room.dart | 7 ------- 1 file changed, 7 deletions(-) diff --git a/example/lib/pages/room.dart b/example/lib/pages/room.dart index 9823267b1..3aaa6e958 100644 --- a/example/lib/pages/room.dart +++ b/example/lib/pages/room.dart @@ -32,8 +32,6 @@ class _RoomPageState extends State { @override void initState() { super.initState(); - // add callback for a `RoomEvent` as opposed to a `ParticipantEvent` - widget.room.addListener(_onRoomDidUpdate); // add callbacks for finer grained events _setUpListeners(); _sortParticipants(); @@ -52,7 +50,6 @@ class _RoomPageState extends State { void dispose() { // always dispose listener (() async { - widget.room.removeListener(_onRoomDidUpdate); await _listener.dispose(); await widget.room.dispose(); })(); @@ -117,10 +114,6 @@ class _RoomPageState extends State { } } - void _onRoomDidUpdate() { - _sortParticipants(); - } - void _onE2EEStateEvent(TrackE2EEStateEvent e2eeState) { print('e2ee state: $e2eeState'); } From d8d677d565634a963f691a5a709fed1f2994742b Mon Sep 17 00:00:00 2001 From: cloudwebrtc Date: Wed, 25 Oct 2023 09:32:57 +0800 Subject: [PATCH 3/9] fix: unpublish screen audio when stop screen share. --- lib/src/participant/local.dart | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/src/participant/local.dart b/lib/src/participant/local.dart index 23a379893..0570fa5e1 100644 --- a/lib/src/participant/local.dart +++ b/lib/src/participant/local.dart @@ -471,6 +471,11 @@ class LocalParticipant extends Participant { } else { if (source == TrackSource.screenShareVideo) { await unpublishTrack(publication.sid); + final screenAudio = + getTrackPublicationBySource(TrackSource.screenShareAudio); + if (screenAudio != null) { + await unpublishTrack(screenAudio.sid); + } } else { await publication.mute(); } From 96fe986913a5c06a1812a8a2c917df798c043800 Mon Sep 17 00:00:00 2001 From: cloudwebrtc Date: Wed, 25 Oct 2023 09:35:30 +0800 Subject: [PATCH 4/9] revert changes. --- example/lib/pages/room.dart | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/example/lib/pages/room.dart b/example/lib/pages/room.dart index 3aaa6e958..95e7cec7a 100644 --- a/example/lib/pages/room.dart +++ b/example/lib/pages/room.dart @@ -32,6 +32,8 @@ class _RoomPageState extends State { @override void initState() { super.initState(); + // add callback for a `RoomEvent` as opposed to a `ParticipantEvent` + widget.room.addListener(_onRoomDidUpdate); // add callbacks for finer grained events _setUpListeners(); _sortParticipants(); @@ -50,6 +52,7 @@ class _RoomPageState extends State { void dispose() { // always dispose listener (() async { + widget.room.removeListener(_onRoomDidUpdate); await _listener.dispose(); await widget.room.dispose(); })(); @@ -65,13 +68,16 @@ class _RoomPageState extends State { WidgetsBindingCompatible.instance ?.addPostFrameCallback((timeStamp) => Navigator.pop(context)); }) + ..on((event) { + print('Participant event'); + // sort participants on many track events as noted in documentation linked above + _sortParticipants(); + }) ..on((event) { context.showRecordingStatusChangedDialog(event.activeRecording); }) ..on((_) => _sortParticipants()) ..on((_) => _sortParticipants()) - ..on((_) => _sortParticipants()) - ..on((_) => _sortParticipants()) ..on(_onE2EEStateEvent) ..on((event) { print( @@ -114,6 +120,10 @@ class _RoomPageState extends State { } } + void _onRoomDidUpdate() { + _sortParticipants(); + } + void _onE2EEStateEvent(TrackE2EEStateEvent e2eeState) { print('e2ee state: $e2eeState'); } From 982454b672abec80e0bf3250a6239682072fd8e5 Mon Sep 17 00:00:00 2001 From: cloudwebrtc Date: Wed, 25 Oct 2023 10:04:49 +0800 Subject: [PATCH 5/9] Listen to the MediaStreamTrack.onEnded to auto unpublishTrack. --- example/lib/widgets/controls.dart | 2 +- lib/src/events.dart | 14 ++++++++++++++ lib/src/participant/local.dart | 12 ++++++++++++ lib/src/track/local/local.dart | 7 ++++++- 4 files changed, 33 insertions(+), 2 deletions(-) diff --git a/example/lib/widgets/controls.dart b/example/lib/widgets/controls.dart index 421b1f265..1488cc31c 100644 --- a/example/lib/widgets/controls.dart +++ b/example/lib/widgets/controls.dart @@ -203,7 +203,7 @@ class _ControlsWidgetState extends State { void _disableScreenShare() async { await participant.setScreenShareEnabled(false); - if (Platform.isAndroid) { + if (lkPlatformIs(PlatformType.android)) { // Android specific try { // await FlutterBackground.disableBackgroundExecution(); diff --git a/lib/src/events.dart b/lib/src/events.dart index 01237d665..04cf7fb54 100644 --- a/lib/src/events.dart +++ b/lib/src/events.dart @@ -12,6 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +import 'package:meta/meta.dart'; + import 'core/engine.dart'; import 'core/room.dart'; import 'core/signal_client.dart'; @@ -504,3 +506,15 @@ class VideoReceiverStatsEvent with TrackEvent { String toString() => '${runtimeType}' 'stats: ${stats})'; } + +@internal +class TrackEndedEvent with TrackEvent { + final Track track; + const TrackEndedEvent({ + required this.track, + }); + + @override + String toString() => '${runtimeType}' + '(track: ${track})'; +} diff --git a/lib/src/participant/local.dart b/lib/src/participant/local.dart index 0570fa5e1..2bf049df8 100644 --- a/lib/src/participant/local.dart +++ b/lib/src/participant/local.dart @@ -108,6 +108,12 @@ class LocalParticipant extends Participant { await track.onPublish(); await room.applyAudioSpeakerSettings(); + var listener = track.createListener(); + listener.on((TrackEndedEvent event) { + logger.fine('TrackEndedEvent: ${event.track}'); + unpublishTrack(pub.sid); + }); + [events, room.events].emit(LocalTrackPublishedEvent( participant: this, publication: pub, @@ -300,6 +306,12 @@ class LocalParticipant extends Participant { // did publish await track.onPublish(); + var listener = track.createListener(); + listener.on((TrackEndedEvent event) { + logger.fine('TrackEndedEvent: ${event.track}'); + unpublishTrack(pub.sid); + }); + [events, room.events].emit(LocalTrackPublishedEvent( participant: this, publication: pub, diff --git a/lib/src/track/local/local.dart b/lib/src/track/local/local.dart index f8df8625d..00fbea0af 100644 --- a/lib/src/track/local/local.dart +++ b/lib/src/track/local/local.dart @@ -80,7 +80,12 @@ abstract class LocalTrack extends Track { source, mediaStream, mediaStreamTrack, - ); + ) { + mediaStreamTrack.onEnded = () { + logger.fine('MediaStreamTrack.onEnded()'); + events.emit(TrackEndedEvent(track: this)); + }; + } /// Mutes this [LocalTrack]. This will stop the sending of track data /// and notify the [RemoteParticipant] with [TrackMutedEvent]. From 064ac5023f53bc8722a9e807c358d4f5fa0bca7b Mon Sep 17 00:00:00 2001 From: cloudwebrtc Date: Wed, 25 Oct 2023 10:11:39 +0800 Subject: [PATCH 6/9] move event to internal. --- lib/src/events.dart | 14 -------------- lib/src/internal/events.dart | 12 ++++++++++++ lib/src/participant/local.dart | 1 + 3 files changed, 13 insertions(+), 14 deletions(-) diff --git a/lib/src/events.dart b/lib/src/events.dart index 04cf7fb54..01237d665 100644 --- a/lib/src/events.dart +++ b/lib/src/events.dart @@ -12,8 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -import 'package:meta/meta.dart'; - import 'core/engine.dart'; import 'core/room.dart'; import 'core/signal_client.dart'; @@ -506,15 +504,3 @@ class VideoReceiverStatsEvent with TrackEvent { String toString() => '${runtimeType}' 'stats: ${stats})'; } - -@internal -class TrackEndedEvent with TrackEvent { - final Track track; - const TrackEndedEvent({ - required this.track, - }); - - @override - String toString() => '${runtimeType}' - '(track: ${track})'; -} diff --git a/lib/src/internal/events.dart b/lib/src/internal/events.dart index 359d5d98b..f4fe608f9 100644 --- a/lib/src/internal/events.dart +++ b/lib/src/internal/events.dart @@ -408,3 +408,15 @@ class SubscriberDataChannelStateUpdatedEvent state: state, ); } + +@internal +class TrackEndedEvent with TrackEvent { + final Track track; + const TrackEndedEvent({ + required this.track, + }); + + @override + String toString() => '${runtimeType}' + '(track: ${track})'; +} diff --git a/lib/src/participant/local.dart b/lib/src/participant/local.dart index 2bf049df8..f1490ae52 100644 --- a/lib/src/participant/local.dart +++ b/lib/src/participant/local.dart @@ -24,6 +24,7 @@ import '../core/transport.dart'; import '../events.dart'; import '../exceptions.dart'; import '../extensions.dart'; +import '../internal/events.dart'; import '../logger.dart'; import '../options.dart'; import '../proto/livekit_models.pb.dart' as lk_models; From 2c38bacbaf29708520eb6e6a61add7b376136136 Mon Sep 17 00:00:00 2001 From: cloudwebrtc Date: Wed, 25 Oct 2023 10:15:09 +0800 Subject: [PATCH 7/9] remove unused import. --- example/lib/widgets/controls.dart | 1 - 1 file changed, 1 deletion(-) diff --git a/example/lib/widgets/controls.dart b/example/lib/widgets/controls.dart index 1488cc31c..8998888e8 100644 --- a/example/lib/widgets/controls.dart +++ b/example/lib/widgets/controls.dart @@ -1,6 +1,5 @@ import 'dart:async'; import 'dart:convert'; -import 'dart:io'; import 'package:collection/collection.dart'; import 'package:flutter/foundation.dart'; From 1fb39355777db08ee44bb9404522e4e6fb50779e Mon Sep 17 00:00:00 2001 From: cloudwebrtc Date: Wed, 25 Oct 2023 10:31:47 +0800 Subject: [PATCH 8/9] no mirror for screen share. --- lib/src/widgets/video_track_renderer.dart | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/src/widgets/video_track_renderer.dart b/lib/src/widgets/video_track_renderer.dart index cf1b281f5..301521667 100644 --- a/lib/src/widgets/video_track_renderer.dart +++ b/lib/src/widgets/video_track_renderer.dart @@ -24,6 +24,7 @@ import '../support/platform.dart'; import '../track/local/local.dart'; import '../track/local/video.dart'; import '../track/options.dart'; +import '../types/other.dart'; enum VideoViewMirrorMode { auto, @@ -129,6 +130,8 @@ class _VideoTrackRendererState extends State { ); bool _shouldMirror() { + // off for screen share + if (widget.track.source == TrackSource.screenShareVideo) return false; // on if (widget.mirrorMode == VideoViewMirrorMode.mirror) return true; // auto From 971a5b47bb6c5dbe8e5e5feb289841bdef5ee7b0 Mon Sep 17 00:00:00 2001 From: cloudwebrtc Date: Wed, 25 Oct 2023 22:19:17 +0800 Subject: [PATCH 9/9] bump version for flutter-webrtc. --- pubspec.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pubspec.yaml b/pubspec.yaml index 15b022359..a1058db08 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -38,7 +38,7 @@ dependencies: uuid: '>=3.0.6' synchronized: ^3.0.0+3 protobuf: ^3.0.0 - flutter_webrtc: 0.9.45 + flutter_webrtc: 0.9.46 flutter_window_close: ^0.2.2 device_info_plus: '>=8.0.0' js: ^0.6.4