Skip to content

Commit 6e3be78

Browse files
authored
use bytestream instead of bytearray (#6847)
1 parent f97833e commit 6e3be78

File tree

1 file changed

+7
-8
lines changed
  • firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/type

1 file changed

+7
-8
lines changed

firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/type/LiveSession.kt

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import io.ktor.client.plugins.websocket.ClientWebSocketSession
2424
import io.ktor.websocket.Frame
2525
import io.ktor.websocket.close
2626
import io.ktor.websocket.readBytes
27+
import java.io.ByteArrayOutputStream
2728
import java.util.concurrent.ConcurrentLinkedQueue
2829
import kotlin.coroutines.CoroutineContext
2930
import kotlinx.coroutines.CoroutineScope
@@ -141,16 +142,14 @@ internal constructor(
141142
}
142143

143144
private suspend fun sendAudioDataToServer() {
144-
var offset = 0
145-
val audioBuffer = ByteArray(MIN_BUFFER_SIZE * 2)
145+
146+
val audioBufferStream = ByteArrayOutputStream()
146147
while (isRecording) {
147148
val receivedAudio = audioQueue.poll() ?: continue
148-
receivedAudio.copyInto(audioBuffer, offset)
149-
offset += receivedAudio.size
150-
if (offset >= MIN_BUFFER_SIZE) {
151-
sendMediaStream(listOf(MediaData(audioBuffer, "audio/pcm")))
152-
audioBuffer.fill(0)
153-
offset = 0
149+
audioBufferStream.write(receivedAudio)
150+
if (audioBufferStream.size() >= MIN_BUFFER_SIZE) {
151+
sendMediaStream(listOf(MediaData(audioBufferStream.toByteArray(), "audio/pcm")))
152+
audioBufferStream.reset()
154153
}
155154
}
156155
}

0 commit comments

Comments
 (0)