Skip to content

fix emulator listening to itself #6823

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Mar 31, 2025
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.google.firebase.vertexai

import android.util.Log
import com.google.firebase.FirebaseApp
import com.google.firebase.annotations.concurrent.Background
import com.google.firebase.appcheck.interop.InteropAppCheckTokenProvider
Expand Down Expand Up @@ -97,10 +98,10 @@ internal constructor(
modelName,
config?.toInternal(),
tools?.map { it.toInternal() },
systemInstruction?.toInternal()
)
systemInstruction?.toInternal())
.toInternal()
val data: String = Json.encodeToString(clientMessage)
Log.w(TAG, data)
try {
val webSession = controller.getWebSocketSession(location)
webSession.send(Frame.Text(data))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,17 @@ internal class AudioHelper {
}
}

fun stopRecording() {
if(::audioRecord.isInitialized && audioRecord.recordingState == AudioRecord.RECORDSTATE_RECORDING) {
audioRecord.stop()
}
}

fun start() {
if (::audioRecord.isInitialized && audioRecord.recordingState != AudioRecord.RECORDSTATE_RECORDING) {
audioRecord.startRecording()
}
}
@RequiresPermission(Manifest.permission.RECORD_AUDIO)
fun startRecording(): Flow<ByteArray> {

Expand Down Expand Up @@ -110,10 +121,16 @@ internal class AudioHelper {
return flow {
val buffer = ByteArray(bufferSize)
while (!stopRecording) {
val bytesRead = audioRecord.read(buffer, 0, buffer.size)
if (bytesRead > 0) {
emit(buffer.copyOf(bytesRead))
if(audioRecord.recordingState != AudioRecord.RECORDSTATE_RECORDING) {
buffer.fill(0x00)
continue
}
try {
val bytesRead = audioRecord.read(buffer, 0, buffer.size)
if (bytesRead > 0) {
emit(buffer.copyOf(bytesRead))
}
} catch (_: Exception) {}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.google.firebase.vertexai.type

import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@OptIn(ExperimentalSerializationApi::class)
Expand All @@ -37,8 +38,9 @@ internal class BidiGenerateContentClientMessage(
val tools: List<Tool.Internal>?,
val systemInstruction: Content.Internal?
)
}

}
fun toInternal() =
Internal(Internal.BidiGenerateContentSetup(model, generationConfig, tools, systemInstruction))
}

Original file line number Diff line number Diff line change
Expand Up @@ -164,14 +164,14 @@ internal constructor(
cancel()
}
when (it.status) {
LiveContentResponse.Status.INTERRUPTED ->
while (!playBackQueue.isEmpty()) playBackQueue.poll()
LiveContentResponse.Status.INTERRUPTED -> while(!playBackQueue.isEmpty()) playBackQueue.poll()
LiveContentResponse.Status.NORMAL ->
if (!it.functionCalls.isNullOrEmpty() && functionCallsHandler != null) {
sendFunctionResponse(it.functionCalls.map(functionCallsHandler).toList())
} else {
val audioData = it.data?.parts?.get(0)?.asInlineDataPartOrNull()?.inlineData
if (audioData != null) {
Log.w(TAG, "Received response from server ${audioData.size}")
playBackQueue.add(audioData)
}
}
Expand All @@ -183,7 +183,12 @@ internal constructor(
private fun playServerResponseAudio() {
CoroutineScope(backgroundDispatcher).launch {
while (isRecording) {
val x = playBackQueue.poll() ?: continue
val x = playBackQueue.poll()
if(x == null) {
audioHelper?.start()
continue
}
audioHelper?.stopRecording()
audioHelper?.playAudio(x)
}
}
Expand Down
Loading