-
Notifications
You must be signed in to change notification settings - Fork 110
Expand file tree
/
Copy pathlocal.ts
More file actions
99 lines (91 loc) · 2.77 KB
/
local.ts
File metadata and controls
99 lines (91 loc) · 2.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// SPDX-FileCopyrightText: 2026 LiveKit, Inc.
//
// SPDX-License-Identifier: Apache-2.0
import type {
LocalDataTrackIsPublishedResponse,
LocalDataTrackTryPushResponse,
OwnedLocalDataTrack,
} from '@livekit/rtc-ffi-bindings';
import {
LocalDataTrackIsPublishedRequest,
LocalDataTrackTryPushRequest,
LocalDataTrackUnpublishRequest,
DataTrackFrame as ProtoDataTrackFrame,
} from '@livekit/rtc-ffi-bindings';
import { FfiClient, FfiHandle } from '../ffi_client.js';
import type { DataTrackFrame, DataTrackInfo } from './types.js';
import { DataTrackPushFrameError } from './types.js';
/** Data track published by the local participant. */
export class LocalDataTrack {
private _info: DataTrackInfo;
private ffiHandle: FfiHandle;
/** @internal */
constructor(ownedTrack: OwnedLocalDataTrack) {
this._info = {
sid: ownedTrack.info!.sid!,
name: ownedTrack.info!.name!,
usesE2ee: ownedTrack.info!.usesE2ee!,
};
this.ffiHandle = new FfiHandle(ownedTrack.handle!.id!);
}
/** Information about the data track. */
get info(): DataTrackInfo {
return this._info;
}
/** Whether or not the track is still published. */
isPublished(): boolean {
const res = FfiClient.instance.request<LocalDataTrackIsPublishedResponse>({
message: {
case: 'localDataTrackIsPublished',
value: new LocalDataTrackIsPublishedRequest({
trackHandle: this.ffiHandle.handle,
}),
},
});
return res.isPublished!;
}
/**
* Try pushing a frame to subscribers of the track.
*
* See {@link DataTrackFrame} for how to construct a frame and attach metadata.
*
* Pushing a frame can fail for several reasons:
*
* - The track has been unpublished by the local participant or SFU
* - The room is no longer connected
*
* @throws {@link DataTrackPushFrameError} If the push fails.
*/
tryPush(frame: DataTrackFrame): void {
const protoFrame = new ProtoDataTrackFrame({
payload: frame.payload,
userTimestamp: frame.userTimestamp,
});
const res = FfiClient.instance.request<LocalDataTrackTryPushResponse>({
message: {
case: 'localDataTrackTryPush',
value: new LocalDataTrackTryPushRequest({
trackHandle: this.ffiHandle.handle,
frame: protoFrame,
}),
},
});
if (res.error) {
throw new DataTrackPushFrameError(res.error.message!);
}
}
/**
* Unpublish the track from the SFU. Once this is called, any further calls to
* {@link tryPush} will fail.
*/
async unpublish(): Promise<void> {
FfiClient.instance.request({
message: {
case: 'localDataTrackUnpublish',
value: new LocalDataTrackUnpublishRequest({
trackHandle: this.ffiHandle.handle,
}),
},
});
}
}