Files
baji-vc/tests/avatar/CodecMedia.kt
T

70 lines
3.7 KiB
Kotlin

package android.media
import android.view.Surface
import java.nio.ByteBuffer
/** Deterministic API doubles exercise production codec control; no Android decoder claim. */
object CodecFixture {
var frames=20; var fps=20; var gop=4; var firstPtsUs=0L
var timestampOffsetUs=0L; var outputEnabled=true; var configureFails=false
var seeks=mutableListOf<Long>(); var releases=0; var flushes=0
var rendered=mutableListOf<Long>(); var onRender: ((Long) -> Unit)? = null
var emitNativeSurface=false
val timelines=mutableMapOf<String,List<Long>>()
val audioPaths=mutableSetOf<String>()
fun reset() {
frames=20; fps=20; gop=4; firstPtsUs=0; timestampOffsetUs=0; outputEnabled=true; configureFails=false
seeks.clear(); releases=0; flushes=0; rendered.clear(); onRender=null
emitNativeSurface=false;timelines.clear();audioPaths.clear()
}
}
class MediaFormat(private val mime: String="video/avc") {
companion object { const val KEY_MIME="mime"; const val KEY_WIDTH="width"; const val KEY_HEIGHT="height" }
fun getString(key: String): String? = mime
fun getInteger(key: String): Int = if (key == KEY_WIDTH) 480 else 848
}
class MediaExtractor {
private var av=false
val trackCount get()=if(av) 2 else 1
private var frame=0
private var timeline: List<Long>?=null
val sampleTime: Long get() = timeline?.getOrNull(frame) ?: if(frame >= (timeline?.size ?: CodecFixture.frames)) -1L else CodecFixture.firstPtsUs+frame*1_000_000L/CodecFixture.fps
fun setDataSource(fd: java.io.FileDescriptor,offset: Long,length: Long) { val path=android.content.AssetFixture.paths[fd];timeline=CodecFixture.timelines[path];av=path in CodecFixture.audioPaths }
fun getTrackFormat(index: Int) = MediaFormat(if(av && index==1) "audio/mp4a-latm" else "video/avc")
fun selectTrack(index: Int) {}
fun seekTo(time: Long,mode: Int) { CodecFixture.seeks += time; frame=(time*CodecFixture.fps/1_000_000).toInt()/CodecFixture.gop*CodecFixture.gop }
fun readSampleData(buffer: ByteBuffer,offset: Int): Int = if (frame >= (timeline?.size ?: CodecFixture.frames)) -1 else 1
fun advance() { frame++ }
fun release() {}
companion object { const val SEEK_TO_PREVIOUS_SYNC=0 }
}
class MediaCodec {
class BufferInfo { var presentationTimeUs=0L; var flags=0; var size=0 }
private data class Output(val pts: Long,val flags: Int,val size: Int)
private val queue=java.util.ArrayDeque<Output>(); private var current: Output? = null
private var target: Surface?=null
companion object {
const val BUFFER_FLAG_END_OF_STREAM=4
fun createDecoderByType(mime: String) = MediaCodec()
}
fun configure(format: MediaFormat,surface: Surface,crypto: Any?,flags: Int) { check(!CodecFixture.configureFails);target=surface }
fun start() {}
fun dequeueInputBuffer(timeout: Long) = 0
fun getInputBuffer(index: Int): ByteBuffer = ByteBuffer.allocate(32)
fun queueInputBuffer(index: Int,offset: Int,size: Int,pts: Long,flags: Int) { queue.add(Output(pts+CodecFixture.timestampOffsetUs,flags,size)) }
fun dequeueOutputBuffer(info: BufferInfo,timeout: Long): Int {
if (!CodecFixture.outputEnabled || queue.isEmpty()) return -1
val out=queue.removeFirst(); current=out
info.presentationTimeUs=out.pts; info.flags=out.flags; info.size=out.size
return 0
}
fun releaseOutputBuffer(index: Int,render: Boolean) {
if (render) { val pts=checkNotNull(current).pts; CodecFixture.rendered += pts; CodecFixture.onRender?.invoke(pts)
if(CodecFixture.emitNativeSurface) target?.texture?.emit(pts*1000)
}
}
fun flush() { queue.clear(); CodecFixture.flushes++ }
fun stop() {}
fun release() { CodecFixture.releases++ }
}