Skip to content

Commit 744d0d5

Browse files
authored
Add support for react-native preconnect audio (livekit#1598)
1 parent 25f8269 commit 744d0d5

4 files changed

Lines changed: 31 additions & 4 deletions

File tree

.changeset/breezy-jobs-yawn.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'livekit-client': patch
3+
---
4+
5+
Add support for react-native preconnect audio

src/room/participant/LocalParticipant.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -558,10 +558,14 @@ export default class LocalParticipant extends Participant {
558558
}
559559

560560
for (const localTrack of localTracks) {
561+
const opts: TrackPublishOptions = {
562+
...this.roomOptions.publishDefaults,
563+
...options,
564+
};
561565
if (
562566
source === Track.Source.Microphone &&
563567
isAudioTrack(localTrack) &&
564-
publishOptions?.preConnectBuffer
568+
opts.preConnectBuffer
565569
) {
566570
this.log.info('starting preconnect buffer for microphone', {
567571
...this.logContext,
@@ -1302,6 +1306,7 @@ export default class LocalParticipant extends Participant {
13021306
ti.audioFeatures.includes(AudioTrackFeature.TF_PRECONNECT_BUFFER)
13031307
) {
13041308
const stream = track.getPreConnectBuffer();
1309+
const mimeType = track.getPreConnectBufferMimeType();
13051310
// TODO: we're registering the listener after negotiation, so there might be a race
13061311
this.on(ParticipantEvent.LocalTrackSubscribed, (pub) => {
13071312
if (pub.trackSid === ti.sid) {
@@ -1335,7 +1340,7 @@ export default class LocalParticipant extends Participant {
13351340
});
13361341
const writer = await this.streamBytes({
13371342
name: 'preconnect-buffer',
1338-
mimeType: 'audio/opus',
1343+
mimeType,
13391344
topic: 'lk.agent.pre-connect-audio-buffer',
13401345
destinationIdentities: [agent.identity],
13411346
attributes: {

src/room/track/LocalTrack.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -649,5 +649,9 @@ export default abstract class LocalTrack<
649649
return this.localTrackRecorder?.byteStream;
650650
}
651651

652+
getPreConnectBufferMimeType() {
653+
return this.localTrackRecorder?.mimeType;
654+
}
655+
652656
protected abstract monitorSender(): void;
653657
}

src/room/track/record.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,24 @@ export class LocalTrackRecorder<T extends LocalTrack> extends RecorderBase {
5151
start: (controller) => {
5252
streamController = controller;
5353
dataListener = async (event: BlobEvent) => {
54-
const arrayBuffer = await event.data.arrayBuffer();
54+
let data: Uint8Array;
55+
56+
if (event.data.arrayBuffer) {
57+
const arrayBuffer = await event.data.arrayBuffer();
58+
data = new Uint8Array(arrayBuffer);
59+
60+
// @ts-expect-error react-native passes over Uint8Arrays directly
61+
} else if (event.data.byteArray) {
62+
// @ts-expect-error
63+
data = event.data.byteArray as Uint8Array;
64+
} else {
65+
throw new Error('no data available!');
66+
}
67+
5568
if (isClosed()) {
5669
return;
5770
}
58-
controller.enqueue(new Uint8Array(arrayBuffer));
71+
controller.enqueue(data);
5972
};
6073
this.addEventListener('dataavailable', dataListener);
6174
},

0 commit comments

Comments
 (0)