Skip to content
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

[DO NOT MERGE] Feature combinations experiments CL #563

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
@@ -0,0 +1,34 @@
package com.example.android.camera2.video

import android.util.Range
import android.util.Size

class FeatureCombination (resolution: Size, dynamicRangeProfile: Long, fpsRange: Range<Int>, videoStabilization: Int) {
private val resolution = resolution;
private val dynamicRangeProfile = dynamicRangeProfile;
private val fpsRange = fpsRange;
private val videoStabilization = videoStabilization;

public fun getResolution() : Size {
return resolution;
}

public fun getDynamicRangeProfile() : Long {
return dynamicRangeProfile;
}

public fun getFpsRange() : Range<Int> {
return fpsRange;
}

public fun getVideoStabilization() : Int {
return videoStabilization;
}

override fun toString(): String {
return "Combination(resolution=${resolution.toString()}, " +
"dynamicRangeProfile=${dynamicRangeProfile.toString()}, " +
"fpsRange=${fpsRange.toString()}, videoStabilization=${videoStabilization.toString()})"
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import android.content.Intent
import android.content.pm.ActivityInfo
import android.graphics.Color
import android.graphics.ColorSpace
import android.graphics.SurfaceTexture
import android.hardware.camera2.CameraCaptureSession
import android.hardware.camera2.CameraCharacteristics
import android.hardware.camera2.CameraDevice
Expand All @@ -43,6 +44,7 @@ import android.os.Handler
import android.os.HandlerThread
import android.util.Log
import android.util.Range
import android.util.Size
import android.view.LayoutInflater
import android.view.MotionEvent
import android.view.Surface
Expand All @@ -62,6 +64,7 @@ import com.example.android.camera.utils.OrientationLiveData
import com.example.android.camera.utils.getPreviewOutputSize
import com.example.android.camera2.video.BuildConfig
import com.example.android.camera2.video.CameraActivity
import com.example.android.camera2.video.FeatureCombination
import com.example.android.camera2.video.R
import com.example.android.camera2.video.databinding.FragmentPreviewBinding
import kotlinx.coroutines.Dispatchers
Expand Down Expand Up @@ -173,6 +176,9 @@ class PreviewFragment : Fragment() {
/** The [CameraDevice] that will be opened in this fragment */
private lateinit var camera: CameraDevice

/** The [FeatureCombination] that will be used to initialize camera */
private lateinit var validFeatureCombination: FeatureCombination

/** Requests used for preview only in the [CameraCaptureSession] */
private val previewRequest: CaptureRequest? by lazy {
pipeline.createPreviewRequest(session, args.previewStabilization)
Expand Down Expand Up @@ -233,7 +239,7 @@ class PreviewFragment : Fragment() {
height: Int) = Unit

override fun surfaceCreated(holder: SurfaceHolder) {

setValidFeatureCombination()
// Selects appropriate preview size and configures view finder
val previewSize = getPreviewOutputSize(
fragmentBinding.viewFinder.display, characteristics, SurfaceHolder::class.java)
Expand All @@ -246,6 +252,7 @@ class PreviewFragment : Fragment() {
// To ensure that size is set, initialize camera in the view's thread
fragmentBinding.viewFinder.post {
pipeline.createResources(holder.surface)
// Camera is already opened here
initializeCamera()
}
}
Expand Down Expand Up @@ -281,10 +288,6 @@ class PreviewFragment : Fragment() {
*/
@SuppressLint("ClickableViewAccessibility")
private fun initializeCamera() = lifecycleScope.launch(Dispatchers.Main) {

// Open the selected camera
camera = openCamera(cameraManager, args.cameraId, cameraHandler)

// Creates list of Surfaces where the camera will output frames
val previewTargets = pipeline.getPreviewTargets()

Expand Down Expand Up @@ -435,6 +438,76 @@ class PreviewFragment : Fragment() {
}, handler)
}

/**
* Testing some static feature combinations
*/
private fun setValidFeatureCombination() = lifecycleScope.launch(Dispatchers.Main) {
camera = openCamera(cameraManager, args.cameraId, cameraHandler)
val emptyStateCallback = object: CameraCaptureSession.StateCallback() {
override fun onConfigured(session: CameraCaptureSession) {}
override fun onConfigureFailed(session: CameraCaptureSession) {}
override fun onReady(session: CameraCaptureSession) {}
}

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.TIRAMISU) {
val resolution4K = Size(3840, 2160);
val resolution1080p = Size(1920, 1080);

val hlg = DynamicRangeProfiles.HLG10;
val hdr = DynamicRangeProfiles.HDR10;
val standardDynamicRange = DynamicRangeProfiles.STANDARD;

val fps60 = Range(60, 60);
val fps30 = Range(30, 60);

val previewStabilization = CaptureRequest.CONTROL_VIDEO_STABILIZATION_MODE_PREVIEW_STABILIZATION;
val standardStabilization = CaptureRequest.CONTROL_VIDEO_STABILIZATION_MODE_ON;
val noStabilization = CaptureRequest.CONTROL_VIDEO_STABILIZATION_MODE_OFF;

val acceptableCombinations = arrayOf(
FeatureCombination(resolution4K, hlg, fps60, previewStabilization),
// FeatureCombination(resolution4K, hdr, fps60, previewStabilization),
// FeatureCombination(resolution4K, hdr, fps30, previewStabilization),
FeatureCombination(resolution4K, hlg, fps30, previewStabilization),
// FeatureCombination(resolution1080p, hdr, fps30, previewStabilization),
FeatureCombination(resolution1080p, hlg, fps30, standardStabilization),
FeatureCombination(resolution1080p, standardDynamicRange, fps30, noStabilization),
)

for (combination: FeatureCombination in acceptableCombinations) {
try {
val outputConfigs = mutableListOf<OutputConfiguration>()
val outputConfig = OutputConfiguration(combination.getResolution(), SurfaceTexture::class.java)
outputConfig.setDynamicRangeProfile(combination.getDynamicRangeProfile())
outputConfigs.add(outputConfig)

sessionConfig = SessionConfiguration(SessionConfiguration.SESSION_REGULAR,
outputConfigs, HandlerExecutor(cameraHandler), emptyStateCallback)

val builder: CaptureRequest.Builder = device.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE)
builder.set(CaptureRequest.CONTROL_AE_TARGET_FPS_RANGE, combination.getFpsRange())
builder.set(CaptureRequest.CONTROL_VIDEO_STABILIZATION_MODE, combination.getVideoStabilization())
val request = builder.build()

sessionConfig.setSessionParameters(request)

Log.i(TAG, "====== TESTING combination ======")
Log.i(TAG, combination.toString())
val isSupported: Boolean = device.isSessionConfigurationSupported(sessionConfig);
Log.i(TAG, "supported: $isSupported")
if (isSupported) {
// combination works
validFeatureCombination = combination
break
}
} catch (exception: Exception) {
Log.i(TAG, "The given combination does not work because of the following exception:")
Log.i(TAG, exception.toString());
}
}
}
}

/**
* Creates a [CameraCaptureSession] with the dynamic range profile set.
*/
Expand Down Expand Up @@ -498,7 +571,24 @@ class PreviewFragment : Fragment() {
}
}

setupSessionWithDynamicRangeProfile(device, targets, handler, stateCallback)
val outputConfigs = mutableListOf<OutputConfiguration>()
for (target in targets) {
val outputConfig = OutputConfiguration(target)
outputConfig.setDynamicRangeProfile(validFeatureCombination.getDynamicRangeProfile())
outputConfigs.add(outputConfig)
}

val sessionConfig = SessionConfiguration(SessionConfiguration.SESSION_REGULAR,
outputConfigs, HandlerExecutor(handler), stateCallback)

val builder: CaptureRequest.Builder = camera.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE)
builder.set(CaptureRequest.CONTROL_AE_TARGET_FPS_RANGE, validFeatureCombination.getFpsRange())
builder.set(CaptureRequest.CONTROL_VIDEO_STABILIZATION_MODE, validFeatureCombination.getVideoStabilization())
val request = builder.build()
sessionConfig.setSessionParameters(request)

device.createCaptureSession(sessionConfig)
// setupSessionWithDynamicRangeProfile(device, targets, handler, stateCallback)
}

override fun onStop() {
Expand Down