Skip to content

Commit

Permalink
Support sync stream id (#881)
Browse files Browse the repository at this point in the history
* Handle new format streamId to better sync a/v tracks (#760)

* Handle new format streamId

* changeset

* Add stream to TrackPublishOptions

* Fix track is not ended correctly with new stream id (#843)

* Fix track is not ended correctly with new stream id

* rename

* Supports sync stream id

* Revert version change

* Update src/room/track/options.ts

Co-authored-by: David Zhao <[email protected]>

* remove unused line

* remove changesets

---------

Co-authored-by: David Zhao <[email protected]>
  • Loading branch information
cnderrauber and davidzhao authored Oct 17, 2023
1 parent 01c4d02 commit 34bea39
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/blue-starfishes-bathe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'livekit-client': patch
---

Handle new format streamId to better sync a/v tracks
7 changes: 5 additions & 2 deletions src/room/Room.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1018,8 +1018,11 @@ class Room extends (EventEmitter as new () => TypedEmitter<RoomEventCallbacks>)
}
const parts = unpackStreamId(stream.id);
const participantId = parts[0];
let trackId = parts[1];
if (!trackId || trackId === '') trackId = mediaTrack.id;
let streamId = parts[1];
let trackId = mediaTrack.id;
// firefox will get streamId (pID|trackId) instead of (pID|streamId) as it doesn't support sync tracks by stream
// and generates its own track id instead of infer from sdp track id.
if (streamId && streamId.startsWith('TR')) trackId = streamId;

if (participantId === this.localParticipant.sid) {
log.warn('tried to create RemoteParticipant for local participant');
Expand Down
1 change: 1 addition & 0 deletions src/room/participant/LocalParticipant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,7 @@ export default class LocalParticipant extends Participant {
encryption: this.encryptionType,
stereo: isStereo,
disableRed: this.isE2EEEnabled || !(opts.red ?? true),
stream: opts?.stream,
});

// compute encodings and layers for video
Expand Down
14 changes: 8 additions & 6 deletions src/room/track/RemoteTrack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,16 @@ export default abstract class RemoteTrack extends Track {
/** @internal */
setMediaStream(stream: MediaStream) {
// this is needed to determine when the track is finished
// we send each track down in its own MediaStream, so we can assume the
// current track is the only one that can be removed.
this.mediaStream = stream;
stream.onremovetrack = () => {
this.receiver = undefined;
this._currentBitrate = 0;
this.emit(TrackEvent.Ended, this);
const onRemoveTrack = (event: MediaStreamTrackEvent) => {
if (event.track === this._mediaStreamTrack) {
stream.removeEventListener('removetrack', onRemoveTrack);
this.receiver = undefined;
this._currentBitrate = 0;
this.emit(TrackEvent.Ended, this);
}
};
stream.addEventListener('removetrack', onRemoveTrack);
}

start() {
Expand Down
7 changes: 7 additions & 0 deletions src/room/track/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,13 @@ export interface TrackPublishOptions extends TrackPublishDefaults {
* Source of track, camera, microphone, or screen
*/
source?: Track.Source;

/**
* Set stream name for the track. Audio and video tracks with the same stream name
* will be placed in the same `MediaStream` and offer better synchronization.
* By default, camera and microphone will be placed in a stream; as would screen_share and screen_share_audio
*/
stream?: string;
}

export interface CreateLocalTracksOptions {
Expand Down
2 changes: 1 addition & 1 deletion src/version.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { version as v } from '../package.json';

export const version = v;
export const protocolVersion = 9;
export const protocolVersion = 10;

0 comments on commit 34bea39

Please sign in to comment.