Skip to content

Commit 6df1112

Browse files
committed
add everything
1 parent 1acaefe commit 6df1112

File tree

4 files changed

+22
-12
lines changed

4 files changed

+22
-12
lines changed

firebase-ai/src/main/kotlin/com/google/firebase/ai/LiveGenerativeModel.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,13 +113,17 @@ internal constructor(
113113
val receivedJson = JSON.parseToJsonElement(receivedJsonStr)
114114

115115
return if (receivedJson is JsonObject && "setupComplete" in receivedJson) {
116-
LiveSession(session = webSession, blockingDispatcher = blockingDispatcher)
116+
LiveSession(
117+
session = webSession,
118+
blockingDispatcher = blockingDispatcher,
119+
firebaseApp = controller.firebaseApp
120+
)
117121
} else {
118122
webSession.close()
119123
throw ServiceConnectionHandshakeFailedException("Unable to connect to the server")
120124
}
121125
} catch (e: ClosedReceiveChannelException) {
122-
throw ServiceConnectionHandshakeFailedException("Channel was closed by the server", e)
126+
throw ServiceConnectionHandshakeFailedException("Error: Too many concurrent live requests", e)
123127
}
124128
}
125129

firebase-ai/src/main/kotlin/com/google/firebase/ai/common/APIController.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ internal constructor(
9797
private val requestOptions: RequestOptions,
9898
httpEngine: HttpClientEngine,
9999
private val apiClient: String,
100-
private val firebaseApp: FirebaseApp,
100+
internal val firebaseApp: FirebaseApp,
101101
private val appVersion: Int = 0,
102102
private val googleAppId: String,
103103
private val headerProvider: HeaderProvider?,

firebase-ai/src/main/kotlin/com/google/firebase/ai/java/LiveSessionFutures.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ public abstract class LiveSessionFutures internal constructor() {
6868
@RequiresPermission(RECORD_AUDIO)
6969
public abstract fun stopAudioConversation(): ListenableFuture<Unit>
7070

71-
7271
/**
7372
* Stops receiving from the model.
7473
*

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

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,13 @@
1717
package com.google.firebase.ai.type
1818

1919
import android.Manifest.permission.RECORD_AUDIO
20+
import android.content.pm.PackageManager
2021
import android.media.AudioFormat
2122
import android.media.AudioTrack
2223
import android.util.Log
2324
import androidx.annotation.RequiresPermission
25+
import androidx.core.content.ContextCompat
26+
import com.google.firebase.FirebaseApp
2427
import com.google.firebase.ai.common.JSON
2528
import com.google.firebase.ai.common.util.CancelledCoroutineScope
2629
import com.google.firebase.ai.common.util.accumulateUntil
@@ -58,7 +61,8 @@ public class LiveSession
5861
internal constructor(
5962
private val session: ClientWebSocketSession,
6063
@Blocking private val blockingDispatcher: CoroutineContext,
61-
private var audioHelper: AudioHelper? = null
64+
private var audioHelper: AudioHelper? = null,
65+
private val firebaseApp: FirebaseApp,
6266
) {
6367
/**
6468
* Coroutine scope that we batch data on for [startAudioConversation].
@@ -93,6 +97,14 @@ internal constructor(
9397
public suspend fun startAudioConversation(
9498
functionCallHandler: ((FunctionCallPart) -> FunctionResponsePart)? = null
9599
) {
100+
101+
val context = firebaseApp.applicationContext
102+
if (
103+
ContextCompat.checkSelfPermission(context, RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED
104+
) {
105+
throw PermissionMissingException("Audio access not provided by the user")
106+
}
107+
96108
FirebaseAIException.catchAsync {
97109
if (scope.isActive) {
98110
Log.w(
@@ -131,15 +143,10 @@ internal constructor(
131143
}
132144
}
133145

134-
135-
/**
136-
* Indicates whether the underlying websocket connection is active.
137-
*/
146+
/** Indicates whether the underlying websocket connection is active. */
138147
public fun isActive(): Boolean = session.isActive
139148

140-
/**
141-
* Indicates whether an audio conversation is being used for this session object.
142-
*/
149+
/** Indicates whether an audio conversation is being used for this session object. */
143150
public fun isAudioConversationRunning(): Boolean = (audioHelper == null)
144151

145152
/**

0 commit comments

Comments
 (0)