-
Notifications
You must be signed in to change notification settings - Fork 110
Expand file tree
/
Copy pathaudio_source.ts
More file actions
157 lines (133 loc) · 3.91 KB
/
audio_source.ts
File metadata and controls
157 lines (133 loc) · 3.91 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// SPDX-FileCopyrightText: 2024 LiveKit, Inc.
//
// SPDX-License-Identifier: Apache-2.0
import type {
AudioSourceInfo,
CaptureAudioFrameCallback,
CaptureAudioFrameResponse,
ClearAudioBufferResponse,
NewAudioSourceResponse,
} from '@livekit/rtc-ffi-bindings';
import {
AudioSourceType,
CaptureAudioFrameRequest,
ClearAudioBufferRequest,
FfiHandle,
NewAudioSourceRequest,
} from '@livekit/rtc-ffi-bindings';
import type { AudioFrame } from './audio_frame.js';
import { FfiClient } from './ffi_client.js';
export class AudioSource {
/** @internal */
info: AudioSourceInfo;
/** @internal */
ffiHandle: FfiHandle;
/** @internal */
lastCapture: number;
/** @internal */
currentQueueSize: number;
/** @internal */
release = () => {};
promise = this.newPromise();
/** @internal */
timeout?: ReturnType<typeof setTimeout> = undefined;
/** @internal */
closed = false;
sampleRate: number;
numChannels: number;
queueSize: number;
constructor(sampleRate: number, numChannels: number, queueSize = 1000) {
this.sampleRate = sampleRate;
this.numChannels = numChannels;
this.queueSize = queueSize;
this.lastCapture = 0;
this.currentQueueSize = 0;
const req = new NewAudioSourceRequest({
type: AudioSourceType.AUDIO_SOURCE_NATIVE,
sampleRate: sampleRate,
numChannels: numChannels,
queueSizeMs: queueSize,
});
const res = FfiClient.instance.request<NewAudioSourceResponse>({
message: {
case: 'newAudioSource',
value: req,
},
});
this.info = res.source!.info!;
this.ffiHandle = new FfiHandle(res.source!.handle!.id!);
}
get queuedDuration(): number {
return Math.max(
this.currentQueueSize - Number(process.hrtime.bigint() / BigInt(1000000)) + this.lastCapture,
0,
);
}
clearQueue() {
const req = new ClearAudioBufferRequest({
sourceHandle: this.ffiHandle.handle,
});
FfiClient.instance.request<ClearAudioBufferResponse>({
message: {
case: 'clearAudioBuffer',
value: req,
},
});
this.currentQueueSize = 0;
this.release();
}
/** @internal */
async newPromise() {
return new Promise<void>((resolve) => {
this.release = resolve;
});
}
async waitForPlayout() {
return this.promise.then(() => {
this.lastCapture = 0;
this.currentQueueSize = 0;
this.promise = this.newPromise();
this.timeout = undefined;
});
}
async captureFrame(frame: AudioFrame) {
if (this.closed) {
throw new Error('AudioSource is closed');
}
if (frame.samplesPerChannel === 0) {
return;
}
const now = Number(process.hrtime.bigint() / BigInt(1000000));
const elapsed = this.lastCapture === 0 ? 0 : now - this.lastCapture;
const frameDurationMs = (frame.samplesPerChannel / frame.sampleRate) * 1000;
this.currentQueueSize = Math.max(this.currentQueueSize - elapsed, 0) + frameDurationMs;
this.lastCapture = now;
if (this.timeout) {
clearTimeout(this.timeout);
}
this.timeout = setTimeout(this.release, this.currentQueueSize);
const req = new CaptureAudioFrameRequest({
sourceHandle: this.ffiHandle.handle,
buffer: frame.protoInfo(),
});
const res = FfiClient.instance.request<CaptureAudioFrameResponse>({
message: { case: 'captureAudioFrame', value: req },
});
const cb = await FfiClient.instance.waitFor<CaptureAudioFrameCallback>((ev) => {
return ev.message.case == 'captureAudioFrame' && ev.message.value.asyncId == res.asyncId;
});
if (cb.error) {
throw new Error(cb.error);
}
}
async close() {
// Clear any pending playout timeout so its callback doesn't fire after
// the handle is disposed, which would reference freed native state.
if (this.timeout) {
clearTimeout(this.timeout);
this.timeout = undefined;
}
this.ffiHandle.dispose();
this.closed = true;
}
}