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

Add HDR capture support #86

Merged
merged 4 commits into from
Aug 12, 2024
Merged
Changes from 3 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 @@ -21,13 +21,15 @@ import android.content.ContentValues
import android.content.Context
import android.os.Build
import android.provider.MediaStore
import android.util.Log
import android.view.Display
import android.widget.Toast
import androidx.annotation.RequiresPermission
import androidx.camera.core.AspectRatio
import androidx.camera.core.Camera
import androidx.camera.core.CameraSelector
import androidx.camera.core.DisplayOrientedMeteringPointFactory
import androidx.camera.core.DynamicRange
import androidx.camera.core.FocusMeteringAction
import androidx.camera.core.ImageCapture
import androidx.camera.core.ImageCaptureException
Expand Down Expand Up @@ -60,6 +62,8 @@ import javax.inject.Inject
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.launch

private const val TAG = "CameraViewModel"

@HiltViewModel
class CameraViewModel @Inject constructor(
@ApplicationContext private val application: Context,
Expand All @@ -69,6 +73,7 @@ class CameraViewModel @Inject constructor(
) : ViewModel() {
private lateinit var camera: Camera
private lateinit var extensionsManager: ExtensionsManager
private lateinit var videoCaptureUseCase: VideoCapture<Recorder>

val chatId: Long? = savedStateHandle.get("chatId")
var viewFinderState = MutableStateFlow(ViewFinderState())
Expand All @@ -93,12 +98,42 @@ class CameraViewModel @Inject constructor(
.setQualitySelector(QualitySelector.from(Quality.HIGHEST))
.build()

private val videoCaptureUseCase = VideoCapture.Builder(recorder)
.build()

private var currentRecording: Recording? = null
private lateinit var recordingState: VideoRecordEvent

init {
val videoCaptureBuilder = VideoCapture.Builder(recorder)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: any reason for the space here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think just habit for personal preference for code readability, but removed

viewModelScope.launch {
val hdrCameraInfo = getHdrCameraInfo()

if (hdrCameraInfo != null) {
Log.i(TAG, "Capturing HDR video")
videoCaptureBuilder.setDynamicRange(hdrCameraInfo)
}

videoCaptureUseCase = videoCaptureBuilder.build()
}
}

private suspend fun getHdrCameraInfo(): DynamicRange? {
var supportedHdrEncoding: DynamicRange? = null

cameraProviderManager.getCameraProvider().availableCameraInfos
.first { cameraInfo ->
val videoCapabilities = Recorder.getVideoCapabilities(cameraInfo)
val supportedDynamicRanges =
videoCapabilities.supportedDynamicRanges

supportedHdrEncoding = supportedDynamicRanges.firstOrNull {
it != DynamicRange.SDR // Ensure an HDR encoding is found

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why don't you directly check HDR, not SDR?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 to this, can we just check if it is HDR instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'm not sure, i copied the code snippet from https://android-developers.googleblog.com/2023/06/camerax-13-is-now-in-beta.html which has the snippet:

supportedHdrEncoding = supportedDynamicRanges.firstOrNull {
            it != DynamicRange.SDR  // Ensure an HDR encoding is found
        }

@donovanfm any idea why we are checking for NOT SDR instead of checking for HDR directly?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we actually make this more specific as well?

There seem to be multiple different dynamic range profiles and to make it consistent, we should choose one we know they all support, which would be HLG 10-bit

https://developer.android.com/reference/androidx/camera/core/DynamicRange

Any device that supports HDR should support HLG 10-bit at a minimum

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done!

}
return@first true
}

return supportedHdrEncoding
}

fun setChatId(chatId: Long) {
savedStateHandle.set("chatId", chatId)
}
Expand Down
Loading