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

refactor(android): migrate VideoDecoderPropertiesModule to Kotlin #3954

Merged
merged 2 commits into from
Jun 30, 2024
Merged
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

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package com.brentvatne.react

import android.media.MediaCodecList
import android.media.MediaDrm
import android.media.MediaFormat
import android.media.UnsupportedSchemeException
import android.os.Build
import com.facebook.react.bridge.Promise
import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.bridge.ReactContextBaseJavaModule
import com.facebook.react.bridge.ReactMethod
import java.util.UUID

class VideoDecoderPropertiesModule(reactContext: ReactApplicationContext?) : ReactContextBaseJavaModule(reactContext) {
override fun getName(): String = REACT_CLASS

@ReactMethod
fun getWidevineLevel(p: Promise) {
var widevineLevel = 0
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) {
p.resolve(widevineLevel)
return
}
try {
val mediaDrm = MediaDrm(WIDEVINE_UUID)
val securityProperty = mediaDrm.getPropertyString(SECURITY_LEVEL_PROPERTY)
widevineLevel = when (securityProperty) {
"L1" -> 1
"L2" -> 2
"L3" -> 3
else -> 0
}
} catch (e: UnsupportedSchemeException) {
e.printStackTrace()
}
p.resolve(widevineLevel)
}

@ReactMethod
fun isCodecSupported(mimeType: String?, width: Int, height: Int, p: Promise) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
p.resolve("unsupported")
return
}
val mRegularCodecs = MediaCodecList(MediaCodecList.REGULAR_CODECS)
val format = MediaFormat.createVideoFormat(mimeType!!, width, height)
val codecName = mRegularCodecs.findDecoderForFormat(format)
if (codecName == null) {
p.resolve("unsupported")
return
}

// Fallback for android < 10
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
p.resolve("software")
return
}
val isHardwareAccelerated = mRegularCodecs.codecInfos.any {
it.name.equals(codecName, ignoreCase = true) && it.isHardwareAccelerated
}
p.resolve(if (isHardwareAccelerated) "software" else "hardware")
}

@ReactMethod
fun isHEVCSupported(p: Promise) = isCodecSupported("video/hevc", 1920, 1080, p)

companion object {
private val WIDEVINE_UUID = UUID(-0x121074568629b532L, -0x5c37d8232ae2de13L)
private const val SECURITY_LEVEL_PROPERTY = "securityLevel"
private const val REACT_CLASS = "VideoManager"
}
}
Loading