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

VideoRenderProvider Fixes & RenderProvider Improvements #802

Merged
merged 1 commit into from
Nov 25, 2023
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.devbrackets.android.exomedia.core.renderer.provider

import androidx.annotation.OptIn
import androidx.media3.common.util.UnstableApi
import androidx.media3.exoplayer.Renderer
import com.devbrackets.android.exomedia.core.renderer.RendererType
import kotlin.reflect.KClass

@OptIn(UnstableApi::class)
abstract class AbstractRenderProvider(
private val type: RendererType
): RenderProvider {
private var latestRenderers: List<Renderer> = emptyList()

override fun type() = type
override fun getLatestRenderers() = latestRenderers

override fun rendererClasses(): List<String> {
return emptyList()
}

protected fun buildRenderers(
vararg initialRenderers: Renderer,
action: (className: String) -> Renderer?
): List<Renderer> {
val renderers = mutableListOf(*initialRenderers)

// Adds any registered classes
rendererClasses().forEach { className ->
try {
action(className)?.let { renderer ->
renderers.add(renderer)
}
} catch (e: Exception) {
// Purposefully left blank
}
}

return renderers.also {
latestRenderers = it
}
}

protected fun <T> Class<T>.newInstance(vararg params: Pair<KClass<*>, Any>): T {
val types = params.map { it.first.java }.toTypedArray()
val values = params.map { it.second }.toTypedArray()

return getConstructor(*types).newInstance(*values)
}
}
Original file line number Diff line number Diff line change
@@ -1,72 +1,62 @@
package com.devbrackets.android.exomedia.core.renderer.provider

import android.content.Context
import java.lang.reflect.Array
import android.os.Handler
import android.util.Log
import androidx.annotation.OptIn
import androidx.media3.common.audio.AudioProcessor
import androidx.media3.common.util.UnstableApi
import androidx.media3.exoplayer.Renderer
import androidx.media3.exoplayer.audio.AudioCapabilities
import androidx.media3.exoplayer.audio.AudioRendererEventListener
import androidx.media3.exoplayer.audio.DecoderAudioRenderer
import androidx.media3.exoplayer.audio.MediaCodecAudioRenderer
import androidx.media3.exoplayer.mediacodec.MediaCodecSelector
import com.devbrackets.android.exomedia.core.renderer.RendererType

@OptIn(UnstableApi::class)
open class AudioRenderProvider : RenderProvider {
private var latestRenderers: List<Renderer> = emptyList()

override fun type() = RendererType.AUDIO

open class AudioRenderProvider : AbstractRenderProvider(RendererType.AUDIO) {
override fun rendererClasses(): List<String> {
return listOf(
"androidx.media3.decoder.opus.LibopusAudioRenderer",
"androidx.media3.decoder.flac.LibflacAudioRenderer",
"androidx.media3.decoder.ffmpeg.FfmpegAudioRenderer",
"androidx.media3.decoder.flac.LibflacAudioRenderer",
"androidx.media3.decoder.opus.LibopusAudioRenderer"
)
}

override fun getLatestRenderers(): List<Renderer> {
return latestRenderers
}

fun buildRenderers(context: Context, handler: Handler, listener: AudioRendererEventListener): List<Renderer> {
val renderers = mutableListOf<Renderer>()

val audioCapabilities = AudioCapabilities.getCapabilities(context)
renderers.add(MediaCodecAudioRenderer(context, MediaCodecSelector.DEFAULT, handler, listener, audioCapabilities))

// Adds any registered classes
rendererClasses().forEach { className ->
try {
val renderer = buildRenderer(className, handler, listener)
renderers.add(renderer)
} catch (e: Exception) {
// Purposefully left blank
}
}
fun buildRenderers(
context: Context,
handler: Handler,
listener: AudioRendererEventListener
): List<Renderer> {
val initialRenderer = MediaCodecAudioRenderer(
context,
MediaCodecSelector.DEFAULT,
handler,
listener,
AudioCapabilities.getCapabilities(context)
)

return renderers.also {
latestRenderers = it
return buildRenderers(initialRenderer) { className ->
buildRenderer(className, handler, listener)
}
}

private fun buildRenderer(
className: String,
handler: Handler,
listener: AudioRendererEventListener
): Renderer {
val rendererClass = Class.forName(className)

val audioProcessorVarArg = Array.newInstance(AudioProcessor::class.java, 0)

val constructor = rendererClass.getConstructor(
Handler::class.java,
AudioRendererEventListener::class.java,
audioProcessorVarArg::class.java
)

return constructor.newInstance(handler, listener, audioProcessorVarArg) as Renderer
): Renderer? {
return when (val rendererClass = Class.forName(className)) {
DecoderAudioRenderer::class.java -> rendererClass.newInstance(
Handler::class to handler,
AudioRendererEventListener::class to listener,
arrayOf<AudioProcessor>()::class to arrayOf<AudioProcessor>()
)
else -> {
Log.w("AudioRenderProvider", "Unsupported audio Renderer class $rendererClass")
null
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.devbrackets.android.exomedia.core.renderer.provider

import android.os.Handler
import android.util.Log
import androidx.annotation.OptIn
import androidx.media3.common.util.UnstableApi
import androidx.media3.exoplayer.Renderer
Expand All @@ -9,24 +10,11 @@ import androidx.media3.exoplayer.text.TextRenderer
import com.devbrackets.android.exomedia.core.renderer.RendererType

@OptIn(UnstableApi::class)
class CaptionRenderProvider: RenderProvider {
private var latestRenderers: List<Renderer> = emptyList()

override fun type() = RendererType.CLOSED_CAPTION

override fun rendererClasses(): List<String> {
return emptyList()
}

override fun getLatestRenderers(): List<Renderer> {
return latestRenderers
}

class CaptionRenderProvider: AbstractRenderProvider(RendererType.CLOSED_CAPTION) {
fun buildRenderers(handler: Handler, captionListener: TextOutput): List<Renderer> {
return listOf(
TextRenderer(captionListener, handler.looper)
).also {
latestRenderers = it
return buildRenderers(TextRenderer(captionListener, handler.looper)) {
Log.w("CaptionRenderProvider", "Adding additional Caption renderers not supported")
null
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.devbrackets.android.exomedia.core.renderer.provider

import android.os.Handler
import android.util.Log
import androidx.annotation.OptIn
import androidx.media3.common.util.UnstableApi
import androidx.media3.exoplayer.Renderer
Expand All @@ -10,24 +11,11 @@ import androidx.media3.exoplayer.metadata.MetadataRenderer
import com.devbrackets.android.exomedia.core.renderer.RendererType

@OptIn(UnstableApi::class)
class MetadataRenderProvider: RenderProvider {
private var latestRenderers: List<Renderer> = emptyList()

override fun type() = RendererType.METADATA

override fun rendererClasses(): List<String> {
return emptyList()
}

override fun getLatestRenderers(): List<Renderer> {
return latestRenderers
}

class MetadataRenderProvider: AbstractRenderProvider(RendererType.METADATA) {
fun buildRenderers(handler: Handler, metadataListener: MetadataOutput): List<Renderer> {
return listOf(
MetadataRenderer(metadataListener, handler.looper, MetadataDecoderFactory.DEFAULT)
).also {
latestRenderers = it
return buildRenderers(MetadataRenderer(metadataListener, handler.looper, MetadataDecoderFactory.DEFAULT)) {
Log.w("MetadataRenderProvider", "Adding additional Metadata renderers not supported")
null
}
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package com.devbrackets.android.exomedia.core.renderer.provider

import com.devbrackets.android.exomedia.core.renderer.RendererType
import androidx.annotation.OptIn
import androidx.media3.common.util.UnstableApi
import androidx.media3.exoplayer.Renderer
import com.devbrackets.android.exomedia.core.renderer.RendererType

/**
* Provides the capabilities for building renderers
*/
@OptIn(UnstableApi::class)
interface RenderProvider {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package com.devbrackets.android.exomedia.core.renderer.provider

import android.content.Context
import android.os.Handler
import android.util.Log
import androidx.annotation.OptIn
import androidx.media3.common.util.UnstableApi
import androidx.media3.exoplayer.Renderer
import androidx.media3.exoplayer.mediacodec.MediaCodecSelector
import androidx.media3.exoplayer.video.DecoderVideoRenderer
import androidx.media3.exoplayer.video.MediaCodecVideoRenderer
import androidx.media3.exoplayer.video.VideoRendererEventListener
import com.devbrackets.android.exomedia.core.renderer.RendererType
Expand All @@ -14,65 +16,49 @@ import com.devbrackets.android.exomedia.core.renderer.RendererType
class VideoRenderProvider(
private val droppedFrameNotificationAmount: Int = 50,
private val videoJoiningTimeMs: Long = 5_000L
) : RenderProvider {
private var latestRenderers: List<Renderer> = emptyList()

override fun type() = RendererType.VIDEO

) : AbstractRenderProvider(RendererType.VIDEO) {
override fun rendererClasses(): List<String> {
return listOf(
"androidx.media3.exoplayer.ext.vp9.LibvpxVideoRenderer"
"androidx.media3.decoder.av1.Libgav1VideoRenderer",
"androidx.media3.decoder.vp9.LibvpxVideoRenderer"
)
}

override fun getLatestRenderers(): List<Renderer> {
return latestRenderers
}

fun buildRenderers(
context: Context,
handler: Handler,
listener: VideoRendererEventListener
): List<Renderer> {
val renderers = mutableListOf<Renderer>()
renderers.add(MediaCodecVideoRenderer(context, MediaCodecSelector.DEFAULT, videoJoiningTimeMs, handler, listener, droppedFrameNotificationAmount))

// Adds any registered classes
rendererClasses().forEach { className ->
try {
val renderer = buildRenderer(className, handler, listener)
renderers.add(renderer)
} catch (e: Exception) {
// Purposefully left blank
}
}
val initialRenderer = MediaCodecVideoRenderer(
context,
MediaCodecSelector.DEFAULT,
videoJoiningTimeMs,
handler,
listener,
droppedFrameNotificationAmount
)

return renderers.also {
latestRenderers = it
return buildRenderers(initialRenderer) { className ->
buildRenderer(className, handler, listener)
}
}

private fun buildRenderer(
className: String,
handler: Handler,
listener: VideoRendererEventListener
): Renderer {
val rendererClass = Class.forName(className)

val constructor = rendererClass.getConstructor(
Boolean::class.javaPrimitiveType,
Long::class.javaPrimitiveType,
Handler::class.java,
VideoRendererEventListener::class.java,
Int::class.javaPrimitiveType
)

return constructor.newInstance(
true,
videoJoiningTimeMs,
handler,
listener,
droppedFrameNotificationAmount
) as Renderer
): Renderer? {
return when (val rendererClass = Class.forName(className)) {
DecoderVideoRenderer::class.java -> rendererClass.newInstance(
Long::class to videoJoiningTimeMs,
Handler::class to handler,
VideoRendererEventListener::class to listener,
Int::class to droppedFrameNotificationAmount
)
else -> {
Log.w("VideoRenderProvider", "Unsupported video Renderer class $rendererClass")
null
}
}
}
}
Loading