Skip to content

Commit

Permalink
Listen to the MediaStreamTrack.onEnded to auto unpublishTrack.
Browse files Browse the repository at this point in the history
  • Loading branch information
cloudwebrtc committed Oct 25, 2023
1 parent 96fe986 commit 982454b
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 2 deletions.
2 changes: 1 addition & 1 deletion example/lib/widgets/controls.dart
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ class _ControlsWidgetState extends State<ControlsWidget> {

void _disableScreenShare() async {
await participant.setScreenShareEnabled(false);
if (Platform.isAndroid) {
if (lkPlatformIs(PlatformType.android)) {
// Android specific
try {
// await FlutterBackground.disableBackgroundExecution();
Expand Down
14 changes: 14 additions & 0 deletions lib/src/events.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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})';
}
12 changes: 12 additions & 0 deletions lib/src/participant/local.dart
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,12 @@ class LocalParticipant extends Participant<LocalTrackPublication> {
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,
Expand Down Expand Up @@ -300,6 +306,12 @@ class LocalParticipant extends Participant<LocalTrackPublication> {
// 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,
Expand Down
7 changes: 6 additions & 1 deletion lib/src/track/local/local.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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].
Expand Down

0 comments on commit 982454b

Please sign in to comment.