-
Notifications
You must be signed in to change notification settings - Fork 268
Add local data track flush method #1925
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
base: main
Are you sure you want to change the base?
Changes from all commits
58474ae
0914a18
d7c2041
35ad89c
8c3b58e
3b8f21e
da85f11
725d1f3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| 'livekit-client': patch | ||
| --- | ||
|
|
||
| Add local data track flush method |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| import log, { LoggerNames, type StructuredLogger, getLogger } from '../../logger'; | ||
| import { Future } from '../utils'; | ||
| import { type DataTrackFrame, DataTrackFrameInternal } from './frame'; | ||
| import type { DataTrackHandle } from './handle'; | ||
| import type OutgoingDataTrackManager from './outgoing/OutgoingDataTrackManager'; | ||
|
|
@@ -28,14 +29,35 @@ export default class LocalDataTrack implements ILocalTrack, IDataTrack { | |
|
|
||
| protected log: StructuredLogger = log; | ||
|
|
||
| /** Resolves once the data track has sent all pending packets the rtc data channel buffer. */ | ||
| protected flushedFuture = new Future<void, never>(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what should happen to this future if the room disconnects while flushing?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point - I think the two options are rejecting with an error or just resolving it normally. Also I would argue that the better signal might be engine close, not room disconnect, though I think in practice the former should always happen when the latter occurs. Any thoughts on either of these points before I add this?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. engine close also sounds fine to me! no strong opinion on whether it should be rejecting or resolving, but something should happen!
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
| /** @internal */ | ||
| constructor(options: DataTrackOptions, manager: OutgoingDataTrackManager) { | ||
| this.options = options; | ||
| this.manager = manager; | ||
|
|
||
| this.log = getLogger(LoggerNames.DataTracks); | ||
|
|
||
| this.manager.on('packetsFlushed', this.handleManagerPacketsFlushed); | ||
| this.manager.on('reset', this.handleManagerReset); | ||
| } | ||
|
|
||
| private handleManagerReset = () => { | ||
| // When the associated manager resets, mark any in flight flushes as complete | ||
| // There's nothing actionable a user can do to get these to complete so no | ||
| // error is being thrown. | ||
| this.handleManagerPacketsFlushed(); | ||
|
|
||
| this.manager.off('packetsFlushed', this.handleManagerReset); | ||
| this.manager.off('reset', this.handleManagerReset); | ||
| }; | ||
|
|
||
| private handleManagerPacketsFlushed = () => { | ||
| this.flushedFuture.resolve?.(); | ||
| this.flushedFuture = new Future(); | ||
| }; | ||
|
|
||
| /** @internal */ | ||
| static withExplicitHandle( | ||
| options: DataTrackOptions, | ||
|
|
@@ -104,6 +126,35 @@ export default class LocalDataTrack implements ILocalTrack, IDataTrack { | |
| } | ||
| } | ||
|
|
||
| /** | ||
| * When called, waits for all in flight packets to be sent before resolving. | ||
| * | ||
| * Use this to: | ||
| * | ||
| * 1. Send frames exactly in order: | ||
| * ```ts | ||
| * await track.tryPush(/* ... *\/); | ||
| * await track.flush(); | ||
| * await track.tryPush(/* ... *\/); | ||
| * await track.flush(); | ||
| * // ... etc ... | ||
| * ``` | ||
| * | ||
| * 2. Wait for frames to all be delivered before unpublishing a local data track: | ||
| * | ||
| * ```ts | ||
| * await track.tryPush(/* ... *\/); | ||
| * await track.tryPush(/* ... *\/); | ||
| * await track.tryPush(/* ... *\/); | ||
| * // ... etc ... | ||
| * await track.flush(); | ||
| * await track.unpublish(); | ||
| * ``` | ||
| **/ | ||
| async flush(): Promise<void> { | ||
| return this.flushedFuture.promise; | ||
| } | ||
|
|
||
| /** | ||
| * Unpublish the track from the SFU. Once this is called, any further calls to {@link tryPush} | ||
| * will fail. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -66,4 +66,8 @@ export class DataTrackHandleAllocator { | |
| } | ||
| return this.value; | ||
| } | ||
|
|
||
| reset() { | ||
| this.value = 0; | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.