Skip to content

Commit

Permalink
Merge pull request #86 from android/caren/hdr_capture
Browse files Browse the repository at this point in the history
Add HDR capture support
  • Loading branch information
calren authored Aug 12, 2024
2 parents 490acda + 10a3074 commit 85d11e8
Showing 1 changed file with 39 additions and 3 deletions.
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,43 @@ 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)
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 {
// To ensure consistency between the multiple dynamic range profiles, chose
// HLG 10-bit, which is supported by all devices that supports HDR.
it == DynamicRange.HLG_10_BIT
}
return@first true
}

return supportedHdrEncoding
}

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

0 comments on commit 85d11e8

Please sign in to comment.