@@ -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
0 commit comments