-
Notifications
You must be signed in to change notification settings - Fork 7.4k
[ALF] Add LiveAPI video sample #2724
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
Open
rlazo
wants to merge
20
commits into
master
Choose a base branch
from
rl.bidi.video
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
a388739
[ALF] Add LiveAPI video sample
rlazo b797bed
Actually runs this time
rlazo cb09306
Point to the right screen
rlazo a64723b
feat: Implement camera frame capture and logging
rlazo d4a71ff
Additional changes not captured by the previous commit
rlazo 6c59bf4
Capture audio
rlazo 1d0a1ca
Reorg code
rlazo dba04b3
Permissions correctly requested
rlazo af5b172
Format new files using ktfmt
rlazo 1fae483
Better naming
rlazo a613fda
Suppress unnecessary lints
rlazo ea0577a
bump versions
rlazo 904fcd9
additional lint fixes
rlazo 1db6a38
Add entries to top level libs.versions.toml
rlazo 5d53fdd
Merge branch 'master' into rl.bidi.video
rlazo d9c46f5
Update libs.versions.toml
rlazo bdadb69
Update firebaseBom and firebase-ai version references
rlazo 9747284
Update firebase-ai/app/build.gradle.kts
rlazo 9bdf3a2
Fix manifest
rlazo db4e94d
Merge branch 'master' into rl.bidi.video
rlazo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
firebase-ai/app/src/main/java/com/google/firebase/quickstart/ai/feature/live/CameraView.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| package com.google.firebase.quickstart.ai.feature.live | ||
|
|
||
| import android.annotation.SuppressLint | ||
| import android.graphics.Bitmap | ||
| import androidx.camera.core.CameraSelector | ||
| import androidx.camera.core.ImageAnalysis | ||
| import androidx.camera.core.ImageProxy | ||
| import androidx.camera.core.Preview | ||
| import androidx.camera.lifecycle.ProcessCameraProvider | ||
| import androidx.camera.view.PreviewView | ||
| import androidx.compose.runtime.Composable | ||
| import androidx.compose.runtime.remember | ||
| import androidx.compose.ui.Modifier | ||
| import androidx.compose.ui.platform.LocalContext | ||
| import androidx.compose.ui.platform.LocalLifecycleOwner | ||
| import androidx.compose.ui.viewinterop.AndroidView | ||
| import androidx.core.content.ContextCompat | ||
| import androidx.lifecycle.LifecycleOwner | ||
| import kotlin.time.Duration.Companion.seconds | ||
|
|
||
| @Composable | ||
| fun CameraView( | ||
| modifier: Modifier = Modifier, | ||
| cameraSelector: CameraSelector = CameraSelector.DEFAULT_BACK_CAMERA, | ||
| onFrameCaptured: (Bitmap) -> Unit, | ||
| ) { | ||
| val context = LocalContext.current | ||
| val lifecycleOwner = LocalLifecycleOwner.current | ||
| val cameraProviderFuture = remember { ProcessCameraProvider.getInstance(context) } | ||
|
|
||
| AndroidView( | ||
| factory = { ctx -> | ||
| val previewView = PreviewView(ctx) | ||
| val executor = ContextCompat.getMainExecutor(ctx) | ||
| cameraProviderFuture.addListener( | ||
| { | ||
| val cameraProvider = cameraProviderFuture.get() | ||
| bindPreview( | ||
| lifecycleOwner, | ||
| previewView, | ||
| cameraProvider, | ||
| cameraSelector, | ||
| onFrameCaptured, | ||
| ) | ||
| }, | ||
| executor, | ||
| ) | ||
| previewView | ||
| }, | ||
| modifier = modifier, | ||
| ) | ||
| } | ||
|
|
||
| private fun bindPreview( | ||
| lifecycleOwner: LifecycleOwner, | ||
| previewView: PreviewView, | ||
| cameraProvider: ProcessCameraProvider, | ||
| cameraSelector: CameraSelector, | ||
| onFrameCaptured: (Bitmap) -> Unit, | ||
| ) { | ||
| val preview = | ||
| Preview.Builder().build().also { it.setSurfaceProvider(previewView.surfaceProvider) } | ||
|
|
||
| val imageAnalysis = | ||
| ImageAnalysis.Builder() | ||
| .setBackpressureStrategy(ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST) | ||
| .build() | ||
| .also { | ||
| it.setAnalyzer( | ||
| ContextCompat.getMainExecutor(previewView.context), | ||
| SnapshotFrameAnalyzer(onFrameCaptured), | ||
| ) | ||
| } | ||
|
|
||
| cameraProvider.unbindAll() | ||
| cameraProvider.bindToLifecycle(lifecycleOwner, cameraSelector, preview, imageAnalysis) | ||
| } | ||
|
|
||
| // Calls the [onFrameCaptured] callback with the captured frame every second. | ||
| private class SnapshotFrameAnalyzer(private val onFrameCaptured: (Bitmap) -> Unit) : | ||
| ImageAnalysis.Analyzer { | ||
| private var lastFrameTimestamp = 0L | ||
| private val interval = 1.seconds // 1 second | ||
|
|
||
| @SuppressLint("UnsafeOptInUsageError") | ||
| override fun analyze(image: ImageProxy) { | ||
| val currentTimestamp = System.currentTimeMillis() | ||
| if (lastFrameTimestamp == 0L) { | ||
| lastFrameTimestamp = currentTimestamp | ||
| } | ||
|
|
||
| if (currentTimestamp - lastFrameTimestamp >= interval.inWholeMilliseconds) { | ||
| onFrameCaptured(image.toBitmap()) | ||
| lastFrameTimestamp = currentTimestamp | ||
| } | ||
| image.close() | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.