Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Emit TimesyncUpdates for local tracks #1191

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Emit TimesyncUpdates for local tracks
  • Loading branch information
lukasIO committed Jul 15, 2024
commit 98b54439d415053ce9fc803a5d949b8fa6fc0abf
2 changes: 2 additions & 0 deletions src/room/track/LocalAudioTrack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ export default class LocalAudioTrack extends LocalTrack<Track.Kind.Audio> {
this.monitorInterval = setInterval(() => {
this.monitorSender();
}, monitorFrequency);

this.registerTimeSyncUpdate();
}

protected monitorSender = async () => {
Expand Down
20 changes: 20 additions & 0 deletions src/room/track/LocalTrack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ export default abstract class LocalTrack<

protected manuallyStopped: boolean = false;

protected lastTimeSyncUpdate = 0;

private restartLock: Mutex;

/**
Expand Down Expand Up @@ -541,5 +543,23 @@ export default abstract class LocalTrack<
this.emit(TrackEvent.TrackProcessorUpdate);
}

protected registerTimeSyncUpdate() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does this work for RN?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the browser APIs requestAnimationFrame, and performance.x should be available in RN. Haven't done any tests yet to confirm though.
@davidliu had some concerns whether the contributing sources used for remote tracks would work in RN out of the box though

const loop = () => {
this.timeSyncHandle = requestAnimationFrame(() => loop());
// for local tracks we don't have access to appropriate timestamps on the sender reports so we generate them manually by following
// how the spec computes timestamps as part of "contributing sources" https://w3c.github.io/webrtc-pc/#dom-rtcrtpcontributingsource-timestamp
const timestamp = performance.timeOrigin + performance.now();

if (!this.isMuted && timestamp - this.lastTimeSyncUpdate > 100) {
this.emit(TrackEvent.TimeSyncUpdate, {
timestamp,
rtpTimestamp: 0,
});
this.lastTimeSyncUpdate = timestamp;
}
};
loop();
}

protected abstract monitorSender(): void;
}
2 changes: 2 additions & 0 deletions src/room/track/LocalVideoTrack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ export default class LocalVideoTrack extends LocalTrack<Track.Kind.Video> {
this.monitorInterval = setInterval(() => {
this.monitorSender();
}, monitorFrequency);

this.registerTimeSyncUpdate();
}

stop() {
Expand Down
Loading