Skip to content

Commit

Permalink
Honor calls to track.stop() during an ongoing restart attempt (#1131)
Browse files Browse the repository at this point in the history
* Honor calls to track.stop() during an ongoing restart attempt

* Create eight-buses-design.md

* emit restarted before stopping
  • Loading branch information
lukasIO authored May 12, 2024
1 parent 0ec6825 commit c38d133
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/eight-buses-design.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"livekit-client": patch
---

Honor calls to track.stop() during an ongoing restart attempt
12 changes: 11 additions & 1 deletion src/room/track/LocalTrack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ export default abstract class LocalTrack<

protected audioContext?: AudioContext;

protected manuallyStopped: boolean = false;

private restartLock: Mutex;

/**
Expand Down Expand Up @@ -259,6 +261,7 @@ export default abstract class LocalTrack<
}

protected async restart(constraints?: MediaTrackConstraints) {
this.manuallyStopped = false;
const unlock = await this.restartLock.lock();
try {
if (!constraints) {
Expand Down Expand Up @@ -296,8 +299,14 @@ export default abstract class LocalTrack<

await this.setMediaStreamTrack(newTrack);
this._constraints = constraints;

this.emit(TrackEvent.Restarted, this);
if (this.manuallyStopped) {
this.log.warn(
'track was stopped during a restart, stopping restarted track',
this.logContext,
);
this.stop();
}
return this;
} finally {
unlock();
Expand Down Expand Up @@ -361,6 +370,7 @@ export default abstract class LocalTrack<
};

stop() {
this.manuallyStopped = true;
super.stop();

this._mediaStreamTrack.removeEventListener('ended', this.handleEnded);
Expand Down

0 comments on commit c38d133

Please sign in to comment.