Skip to content

Commit c21fb20

Browse files
authored
Pre-connect improvements (#699)
1 parent 23ab704 commit c21fb20

File tree

2 files changed

+16
-11
lines changed

2 files changed

+16
-11
lines changed

Sources/LiveKit/Core/PreConnectAudioBuffer.swift

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -146,25 +146,30 @@ public final class PreConnectAudioBuffer: NSObject, Sendable, Loggable {
146146
throw LiveKitError(.invalidState, message: "Audio stream is nil")
147147
}
148148

149-
let audioData = try await audioStream.collect()
150-
guard audioData.count > 1024 else {
151-
throw LiveKitError(.unknown, message: "Audio data size too small, nothing to send")
152-
}
153-
154149
let streamOptions = StreamByteOptions(
155150
topic: topic,
156151
attributes: [
157152
"sampleRate": "\(recorder.sampleRate)",
158153
"channels": "\(recorder.channels)",
159154
"trackId": recorder.track.sid?.stringValue ?? "",
160155
],
161-
destinationIdentities: agents,
162-
totalSize: audioData.count
156+
destinationIdentities: agents
163157
)
164158
let writer = try await room.localParticipant.streamBytes(options: streamOptions)
165-
try await writer.write(audioData)
159+
160+
var sentSize = 0
161+
for await chunk in audioStream {
162+
do {
163+
try await writer.write(chunk)
164+
} catch {
165+
try await writer.close(reason: error.localizedDescription)
166+
throw error
167+
}
168+
sentSize += chunk.count
169+
}
166170
try await writer.close()
167-
log("Sent \(recorder.duration(audioData.count))s = \(audioData.count / 1024)KB of audio data to \(agents.count) agent(s) \(agents)", .info)
171+
172+
log("Sent \(recorder.duration(sentSize))s = \(sentSize / 1024)KB of audio data to \(agents.count) agent(s) \(agents)", .info)
168173
}
169174
}
170175

Sources/LiveKit/Core/Room+PreConnect.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public extension Room {
4545
/// - See: ``PreConnectAudioBuffer``
4646
/// - Important: Call ``AudioManager/setRecordingAlwaysPreparedMode(_:)`` during app launch sequence to request microphone permissions early.
4747
///
48-
func withPreConnectAudio<T>(timeout: TimeInterval = 10,
48+
func withPreConnectAudio<T>(timeout: TimeInterval = PreConnectAudioBuffer.Constants.timeout,
4949
_ operation: @Sendable @escaping () async throws -> T,
5050
onError: PreConnectAudioBuffer.OnError? = nil) async throws -> T
5151
{
@@ -61,7 +61,7 @@ public extension Room {
6161
}
6262

6363
@available(*, deprecated, message: "Use withPreConnectAudio instead")
64-
func startCapturingBeforeConnecting(timeout: TimeInterval = 10) async throws {
64+
func startCapturingBeforeConnecting(timeout: TimeInterval = PreConnectAudioBuffer.Constants.timeout) async throws {
6565
try await preConnectBuffer.startRecording(timeout: timeout)
6666
}
6767
}

0 commit comments

Comments
 (0)