Release Kaleido 0.1.10 with native Original playback and PCM compatibility
All checks were successful
Validate and publish Kaleido / android (push) Successful in 2m45s
All checks were successful
Validate and publish Kaleido / android (push) Successful in 2m45s
This commit is contained in:
parent
784921dba9
commit
2a83fe0db5
23 changed files with 721 additions and 43 deletions
|
|
@ -0,0 +1,28 @@
|
|||
package tv.kaleido.app
|
||||
|
||||
import android.media.AudioFormat
|
||||
import android.media.AudioTrack
|
||||
import androidx.media3.common.C
|
||||
import androidx.media3.common.Format
|
||||
import androidx.media3.common.MimeTypes
|
||||
import org.junit.Assert.*
|
||||
import org.junit.Test
|
||||
|
||||
@androidx.annotation.OptIn(androidx.media3.common.util.UnstableApi::class)
|
||||
class PlaybackCompatibilityTest {
|
||||
@Test fun monoPcmHasAPlayableMaskWithoutChangingSamples() {
|
||||
val source=Format.Builder().setSampleMimeType(MimeTypes.AUDIO_RAW).setChannelCount(1)
|
||||
.setChannelMask(AudioFormat.CHANNEL_OUT_FRONT_CENTER).setSampleRate(96000).setPcmEncoding(C.ENCODING_PCM_24BIT).build()
|
||||
val output=compatiblePcmFormat(source)
|
||||
assertEquals(96000,output.sampleRate);assertEquals(source.pcmEncoding,output.pcmEncoding)
|
||||
assertEquals(1,output.channelCount);assertEquals(AudioFormat.CHANNEL_OUT_MONO,output.channelMask)
|
||||
// The default sink converts integer PCM to 16 bit before opening AudioTrack.
|
||||
assertTrue(AudioTrack.getMinBufferSize(output.sampleRate,output.channelMask,C.ENCODING_PCM_16BIT)>0)
|
||||
}
|
||||
@Test fun multichannelPcmAndBitstreamsKeepTheirLayout() {
|
||||
val pcm=Format.Builder().setSampleMimeType(MimeTypes.AUDIO_RAW).setChannelCount(6)
|
||||
.setChannelMask(AudioFormat.CHANNEL_OUT_5POINT1).setSampleRate(48000).setPcmEncoding(C.ENCODING_PCM_16BIT).build()
|
||||
val eac3=pcm.buildUpon().setSampleMimeType(MimeTypes.AUDIO_E_AC3).build()
|
||||
assertSame(pcm,compatiblePcmFormat(pcm));assertSame(eac3,compatiblePcmFormat(eac3))
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,46 @@
|
|||
package tv.kaleido.app
|
||||
|
||||
import android.content.Context
|
||||
import android.hardware.display.DisplayManager
|
||||
import android.media.AudioManager
|
||||
import android.media.MediaCodecList
|
||||
import android.os.Build
|
||||
import androidx.test.platform.app.InstrumentationRegistry
|
||||
import org.json.JSONArray
|
||||
import org.json.JSONObject
|
||||
import org.junit.Assume.assumeTrue
|
||||
import org.junit.Test
|
||||
|
||||
/** Explicit opt-in capability inventory. No session credentials are exported. */
|
||||
class PlaybackValidationSetupTest {
|
||||
@Test fun privateValidationContext() {
|
||||
assumeTrue(InstrumentationRegistry.getArguments().getString("validationSetup")=="true")
|
||||
val app=InstrumentationRegistry.getInstrumentation().targetContext.applicationContext as KaleidoApplication
|
||||
val codecs=JSONArray()
|
||||
MediaCodecList(MediaCodecList.ALL_CODECS).codecInfos.filterNot{it.isEncoder}.forEach { codec->
|
||||
codec.supportedTypes.forEach { type->
|
||||
runCatching {
|
||||
val caps=codec.getCapabilitiesForType(type)
|
||||
codecs.put(JSONObject().put("name",codec.name).put("mime",type)
|
||||
.put("hardware",if(Build.VERSION.SDK_INT>=29)codec.isHardwareAccelerated else JSONObject.NULL)
|
||||
.put("profiles",JSONArray(caps.profileLevels.map{JSONObject().put("profile",it.profile).put("level",it.level)})))
|
||||
}
|
||||
}
|
||||
}
|
||||
val displays=app.getSystemService(DisplayManager::class.java).displays.map { display->
|
||||
JSONObject().put("id",display.displayId).put("name",display.name)
|
||||
.put("hdrTypes",JSONArray(display.hdrCapabilities.supportedHdrTypes.toList()))
|
||||
.put("mode",display.mode.toString())
|
||||
}
|
||||
val audio=app.getSystemService(AudioManager::class.java)
|
||||
val outputs=audio.getDevices(AudioManager.GET_DEVICES_OUTPUTS).map { device->
|
||||
JSONObject().put("id",device.id).put("type",device.type).put("name",device.productName)
|
||||
.put("encodings",JSONArray(device.encodings.toList())).put("channels",JSONArray(device.channelCounts.toList()))
|
||||
}
|
||||
val report=JSONObject().put("model",Build.MODEL).put("sdk",Build.VERSION.SDK_INT)
|
||||
.put("codecs",codecs).put("displays",JSONArray(displays)).put("audioOutputs",JSONArray(outputs))
|
||||
.put("mediaVolume",audio.getStreamVolume(AudioManager.STREAM_MUSIC))
|
||||
.put("mediaMuted",audio.isStreamMute(AudioManager.STREAM_MUSIC))
|
||||
app.openFileOutput("validation-capabilities.json",Context.MODE_PRIVATE).use{it.write(report.toString(2).toByteArray())}
|
||||
}
|
||||
}
|
||||
5
app/src/debug/AndroidManifest.xml
Normal file
5
app/src/debug/AndroidManifest.xml
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<application>
|
||||
<activity android:name=".HdmiValidationActivity" android:exported="true" />
|
||||
</application>
|
||||
</manifest>
|
||||
54
app/src/debug/java/tv/kaleido/app/HdmiValidationActivity.kt
Normal file
54
app/src/debug/java/tv/kaleido/app/HdmiValidationActivity.kt
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
package tv.kaleido.app
|
||||
|
||||
import android.app.Activity
|
||||
import android.media.tv.TvContract
|
||||
import android.media.tv.TvView
|
||||
import android.os.Bundle
|
||||
import android.view.Gravity
|
||||
import android.widget.FrameLayout
|
||||
import android.widget.TextView
|
||||
import org.json.JSONObject
|
||||
|
||||
/** Temporary, public-TIF HDMI observer. No capture permissions or device settings changes. */
|
||||
class HdmiValidationActivity:Activity() {
|
||||
private lateinit var tv:TvView
|
||||
private lateinit var label:TextView
|
||||
private fun extras(bundle:Bundle?)=bundle?.let{b->b.keySet().joinToString{key->"$key=${b.get(key)}"}}.orEmpty()
|
||||
private fun record(event:String,data:String="") {
|
||||
val line=JSONObject().put("event",event).put("detail",data).put("elapsedMs",android.os.SystemClock.elapsedRealtime()).toString()
|
||||
openFileOutput("hdmi-validation.jsonl",MODE_APPEND).use{it.write((line+"\n").toByteArray())}
|
||||
label.text="HDMI validation · $event"
|
||||
}
|
||||
override fun onCreate(state:Bundle?) {
|
||||
super.onCreate(state)
|
||||
window.addFlags(android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
|
||||
setTurnScreenOn(true)
|
||||
window.decorView.systemUiVisibility=android.view.View.SYSTEM_UI_FLAG_FULLSCREEN or android.view.View.SYSTEM_UI_FLAG_HIDE_NAVIGATION or android.view.View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
|
||||
val input=intent.getStringExtra("inputId") ?: return finish()
|
||||
tv=TvView(this)
|
||||
label=TextView(this).apply{setTextColor(android.graphics.Color.WHITE);setBackgroundColor(0x99000000.toInt());setPadding(18,10,18,10)}
|
||||
val root=FrameLayout(this).apply {
|
||||
addView(tv,FrameLayout.LayoutParams(-1,-1))
|
||||
addView(label,FrameLayout.LayoutParams(-2,-2,Gravity.BOTTOM or Gravity.START))
|
||||
}
|
||||
setContentView(root)
|
||||
openFileOutput("hdmi-validation.jsonl",MODE_PRIVATE).close()
|
||||
tv.setCallback(object:TvView.TvInputCallback() {
|
||||
override fun onVideoAvailable(inputId:String){record("videoAvailable")}
|
||||
override fun onVideoUnavailable(inputId:String,reason:Int){record("videoUnavailable",reason.toString())}
|
||||
override fun onConnectionFailed(inputId:String){record("connectionFailed")}
|
||||
override fun onDisconnected(inputId:String){record("disconnected")}
|
||||
override fun onVideoSizeChanged(inputId:String,width:Int,height:Int){record("size","${width}x$height")}
|
||||
override fun onTracksChanged(inputId:String,tracks:MutableList<android.media.tv.TvTrackInfo>?) {
|
||||
record("tracks",tracks?.joinToString { t->
|
||||
if(t.type==android.media.tv.TvTrackInfo.TYPE_VIDEO)"video ${t.videoWidth}x${t.videoHeight} ${t.videoFrameRate} ${extras(t.extra)}"
|
||||
else "type=${t.type} ${t.extra}"
|
||||
}.orEmpty())
|
||||
}
|
||||
override fun onTvMessage(inputId:String,type:Int,args:Bundle){record("message:$type",args.toString())}
|
||||
})
|
||||
record("tuning",input)
|
||||
runCatching{tv.tune(input,TvContract.buildChannelUriForPassthroughInput(input))}.onFailure{record("tuneFailed",it.javaClass.simpleName)}
|
||||
}
|
||||
override fun onDestroy(){if(::tv.isInitialized)tv.reset();super.onDestroy()}
|
||||
}
|
||||
27
app/src/main/java/tv/kaleido/app/CompatibleAudioSink.kt
Normal file
27
app/src/main/java/tv/kaleido/app/CompatibleAudioSink.kt
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
package tv.kaleido.app
|
||||
|
||||
import android.media.AudioFormat
|
||||
import androidx.media3.common.Format
|
||||
import androidx.media3.common.MimeTypes
|
||||
import androidx.media3.common.util.UnstableApi
|
||||
import androidx.media3.exoplayer.audio.AudioSink
|
||||
import androidx.media3.exoplayer.audio.ForwardingAudioSink
|
||||
|
||||
/** Android rejects a center-only PCM mask; one-channel audio uses its standard mono output. */
|
||||
@androidx.annotation.OptIn(UnstableApi::class)
|
||||
internal class CompatibleAudioSink(sink:AudioSink):ForwardingAudioSink(sink) {
|
||||
@Suppress("DEPRECATION")
|
||||
override fun configure(config:AudioSink.AudioSinkConfig) {
|
||||
val format=compatiblePcmFormat(config.format)
|
||||
super.configure(if(format===config.format)config else AudioSink.AudioSinkConfig.Builder(format)
|
||||
.setPreferredBufferSizeOverride(config.preferredBufferSizeOverride)
|
||||
.setOutputChannelMapping(config.outputChannelMapping)
|
||||
.setTimeline(config.timeline).setMediaPeriodId(config.mediaPeriodId).build())
|
||||
}
|
||||
}
|
||||
|
||||
@androidx.annotation.OptIn(UnstableApi::class)
|
||||
internal fun compatiblePcmFormat(format:Format):Format =
|
||||
if(format.sampleMimeType==MimeTypes.AUDIO_RAW&&format.channelCount==1&&format.channelMask!=AudioFormat.CHANNEL_OUT_MONO)
|
||||
format.buildUpon().setChannelMask(AudioFormat.CHANNEL_OUT_MONO).build()
|
||||
else format
|
||||
|
|
@ -69,6 +69,8 @@ class KaleidoViewModel(app: Application): AndroidViewModel(app) {
|
|||
var streamQuality by mutableStateOf(savedStreamQuality()); private set
|
||||
/** Debug test intents force a delivery mode (0 file, 1 direct stream, 2 convert); never set in release. */
|
||||
var debugMode by mutableStateOf<Int?>(null); private set
|
||||
var debugValidation=false; private set
|
||||
var debugValidationFile:File?=null; private set
|
||||
var watchedPercent by mutableStateOf(preferences.getInt("watchedPercent",80).coerceIn(50,100)); private set
|
||||
var navigationSounds by mutableStateOf(preferences.getBoolean("navigationSounds",true)); private set
|
||||
var episodeQueue by mutableStateOf(emptyList<Title>());private set
|
||||
|
|
@ -277,6 +279,7 @@ class KaleidoViewModel(app: Application): AndroidViewModel(app) {
|
|||
sectionCache.clear()
|
||||
}
|
||||
fun play(item: Title,fromStart: Boolean=false)=launchLoad {
|
||||
debugMode=null;debugValidation=false;debugValidationFile=null
|
||||
val playable=if(item.type=="show" || item.type=="season") {
|
||||
val episodes=api.allEpisodes(item.id);episodes.firstOrNull {it.offset>0&&!it.watched} ?: episodes.firstOrNull {!it.watched} ?: episodes.firstOrNull() ?: throw PlexException("No playable episodes are available.")
|
||||
} else item
|
||||
|
|
@ -335,5 +338,17 @@ class KaleidoViewModel(app: Application): AndroidViewModel(app) {
|
|||
fun clearDownload(){downloadedApk=null}
|
||||
/** Back from the system installer without updating: the verified APK can be installed again. */
|
||||
fun installerReturned(){if(readyApk!=null&&!downloading)updateStatus="Update downloaded. Select Install ${update?.version.orEmpty()} to finish."}
|
||||
fun debugPlay(id: String,offset: Long,mode: Int){if(!BuildConfig.DEBUG)return;launchLoad {playing=null;delay(150);debugMode=mode;playing=api.details(id).copy(offset=offset);startFromZero=false}}
|
||||
fun debugPlay(id: String,offset: Long,mode: Int,validation:Boolean=false){if(!BuildConfig.DEBUG)return;launchLoad {playing=null;delay(150);debugValidationFile=null;debugMode=mode.takeIf{it>=0};debugValidation=validation;playing=api.details(id).copy(offset=offset);startFromZero=false}}
|
||||
/** Private, opt-in test media uses the same controller and SurfaceView as Plex playback. */
|
||||
fun debugPlayFixture(name:String){
|
||||
if(!BuildConfig.DEBUG||!name.matches(Regex("[a-z0-9-]+")))return
|
||||
launchLoad {
|
||||
val folder=File(application.filesDir,"validation").canonicalFile
|
||||
val fixture=Title.parse(JSONObject(File(folder,"$name.json").readText()))
|
||||
val file=File(folder,fixture.media.first().parts.first().key).canonicalFile
|
||||
require(file.parentFile==folder&&file.isFile)
|
||||
playing=null;delay(150);debugMode=0;debugValidation=true;debugValidationFile=file
|
||||
playing=fixture;startFromZero=true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -42,6 +42,9 @@ class MainActivity: ComponentActivity() {
|
|||
debugPlayback(intent)
|
||||
}
|
||||
override fun onNewIntent(intent: Intent) {super.onNewIntent(intent);setIntent(intent);debugPlayback(intent)}
|
||||
private fun debugPlayback(intent: Intent) {if(BuildConfig.DEBUG)intent.getStringExtra("testTitleId")?.let{model.debugPlay(it,intent.getLongExtra("testOffset",90000),intent.getIntExtra("testMode",0))}}
|
||||
private fun debugPlayback(intent: Intent) {if(BuildConfig.DEBUG){
|
||||
intent.getStringExtra("testFixture")?.let{model.debugPlayFixture(it);return}
|
||||
intent.getStringExtra("testTitleId")?.let{model.debugPlay(it,intent.getLongExtra("testOffset",90000),intent.getIntExtra("testMode",0),intent.getBooleanExtra("testValidation",false))}
|
||||
}}
|
||||
override fun onStart() { super.onStart(); model.checkUpdate(false) }
|
||||
}
|
||||
|
|
|
|||
|
|
@ -40,6 +40,8 @@ import android.util.Log
|
|||
import kotlinx.coroutines.*
|
||||
import kotlinx.coroutines.channels.Channel
|
||||
import java.util.UUID
|
||||
import kotlinx.coroutines.sync.Mutex
|
||||
import kotlinx.coroutines.sync.withLock
|
||||
|
||||
data class TimelineReport(val position: Long,val state: String,val duration: Long,val final: Boolean=false)
|
||||
@androidx.annotation.OptIn(androidx.media3.common.util.UnstableApi::class)
|
||||
|
|
@ -49,15 +51,31 @@ class PlaybackController(private val vm: KaleidoViewModel,val item: Title) {
|
|||
val scope=CoroutineScope(SupervisorJob()+Dispatchers.Main.immediate)
|
||||
val selector=DefaultTrackSelector(app)
|
||||
private val sourceFactory=DefaultMediaSourceFactory(DefaultDataSource.Factory(app,OkHttpDataSource.Factory(api.http).setDefaultRequestProperties(api.headers().filterKeys{it!="Accept"})))
|
||||
val player=ExoPlayer.Builder(app,DefaultRenderersFactory(app).setEnableDecoderFallback(true).forceDisableMediaCodecAsynchronousQueueing()).setTrackSelector(selector).setMediaSourceFactory(sourceFactory).build().apply {
|
||||
private val renderers=object:DefaultRenderersFactory(app) {
|
||||
override fun buildAudioSink(context:android.content.Context,enableFloatOutput:Boolean,enableAudioOutputPlaybackParams:Boolean):androidx.media3.exoplayer.audio.AudioSink =
|
||||
CompatibleAudioSink(androidx.media3.exoplayer.audio.DefaultAudioSink.Builder(context)
|
||||
.setEnableFloatOutput(enableFloatOutput)
|
||||
.setEnableAudioOutputPlaybackParameters(enableAudioOutputPlaybackParams)
|
||||
.build())
|
||||
}.setEnableDecoderFallback(true).forceDisableMediaCodecAsynchronousQueueing()
|
||||
val player=ExoPlayer.Builder(app,renderers).setTrackSelector(selector).setMediaSourceFactory(sourceFactory).build().apply {
|
||||
setAudioAttributes(AudioAttributes.Builder().setUsage(C.USAGE_MEDIA).setContentType(C.AUDIO_CONTENT_TYPE_MOVIE).build(),true)
|
||||
setHandleAudioBecomingNoisy(true)
|
||||
}
|
||||
val mediaSession=MediaSession.Builder(app,player).setId("kaleido-${UUID.randomUUID()}").build()
|
||||
// Original quality direct-streams the source video (remuxed, never re-encoded) and falls back
|
||||
// to conversion if the TV can't play it; a resolution always asks Plex to convert.
|
||||
var mode by mutableIntStateOf(vm.debugMode ?: if(vm.streamQuality==StreamQuality.Original)1 else 2);private set
|
||||
val qualityLabel get()=if(mode<2)StreamQuality.Original.label else "Converting · "+vm.streamQuality.conversion.label
|
||||
private val validation=BuildConfig.DEBUG&&vm.debugValidation
|
||||
private val localValidationFile=if(validation)vm.debugValidationFile else null
|
||||
private val audit=if(validation)PlaybackValidationLog(app,item,player)else null
|
||||
// Preserve the actual file first; Plex's generic HLS profile can convert even with directStream=1.
|
||||
var mode by mutableIntStateOf(vm.debugMode ?: if(vm.streamQuality==StreamQuality.Original)0 else 2);private set
|
||||
private var serverConverting by mutableStateOf(false)
|
||||
private var serverAudioConverting by mutableStateOf(false)
|
||||
val qualityLabel get()=when {
|
||||
mode==2->"Converting · "+vm.streamQuality.conversion.label
|
||||
serverConverting->if(serverAudioConverting)"Converted video and audio"else "Converted video"
|
||||
serverAudioConverting->"Original video · Converted audio"
|
||||
else->StreamQuality.Original.label
|
||||
}
|
||||
var loading by mutableStateOf(true);private set
|
||||
var error by mutableStateOf<String?>(null);private set
|
||||
var position by mutableLongStateOf(if(vm.startFromZero)0 else item.offset);private set
|
||||
|
|
@ -75,17 +93,23 @@ class PlaybackController(private val vm: KaleidoViewModel,val item: Title) {
|
|||
private var streamStart=0L
|
||||
private var preparing=false
|
||||
private var closed=false
|
||||
private val prepareMutex=Mutex()
|
||||
private var fallbackPending=false
|
||||
private var nativeSelectionApplied=false
|
||||
private val reports=Channel<TimelineReport>(Channel.UNLIMITED)
|
||||
private val reportJob=app.scope.launch {
|
||||
val watched=WatchedProgress{api.watched(item,true);withContext(Dispatchers.Main){vm.refreshWatched(item)}}
|
||||
for(r in reports) {
|
||||
runCatching {api.timeline(item,r.position,r.state,r.duration,streamSession)}.onFailure {Log.w("KaleidoPlayback","Timeline sync unavailable")}
|
||||
runCatching{watched.report(r.position,r.duration,vm.watchedPercent)}
|
||||
.onFailure{Log.w("KaleidoPlayback","Watched status sync unavailable")}
|
||||
if(!validation) {
|
||||
runCatching {api.timeline(item,r.position,r.state,r.duration,streamSession)}.onFailure {Log.w("KaleidoPlayback","Timeline sync unavailable")}
|
||||
runCatching{watched.report(r.position,r.duration,vm.watchedPercent)}
|
||||
.onFailure{Log.w("KaleidoPlayback","Watched status sync unavailable")}
|
||||
}
|
||||
if(r.final){if(mode>0)runCatching{api.stopTranscode(streamSession)};break}
|
||||
}
|
||||
}
|
||||
init {
|
||||
audit?.let{player.addAnalyticsListener(it)}
|
||||
player.addListener(object: Player.Listener {
|
||||
override fun onPlaybackStateChanged(state: Int) {
|
||||
loading=state==Player.STATE_BUFFERING;ended=state==Player.STATE_ENDED
|
||||
|
|
@ -93,45 +117,91 @@ class PlaybackController(private val vm: KaleidoViewModel,val item: Title) {
|
|||
if(ended) {position=duration;report("stopped");Log.i("KaleidoPlayback","ENDED")}
|
||||
}
|
||||
override fun onIsPlayingChanged(value: Boolean){isPlaying=value;if(!preparing&&!closed)report(if(value)"playing"else "paused")}
|
||||
override fun onTracksChanged(value: Tracks){tracks=value;Log.i("KaleidoPlayback","TRACKS audio=${value.groups.count{it.type==C.TRACK_TYPE_AUDIO}} text=${value.groups.count{it.type==C.TRACK_TYPE_TEXT}}")}
|
||||
override fun onTracksChanged(value: Tracks){
|
||||
tracks=value
|
||||
Log.i("KaleidoPlayback","TRACKS audio=${value.groups.count{it.type==C.TRACK_TYPE_AUDIO}} text=${value.groups.count{it.type==C.TRACK_TYPE_TEXT}}")
|
||||
// Some extractors expose only audio for an unsupported video codec (e.g. VC-1).
|
||||
if(value.groups.isNotEmpty()&&item.media.first().videoCodec.isNotBlank()&&
|
||||
value.groups.none{it.type==C.TRACK_TYPE_VIDEO&&it.isSupported(true)}) {
|
||||
fallback("unsupportedVideo");return
|
||||
}
|
||||
if(mode==0&&!nativeSelectionApplied&&value.groups.isNotEmpty()) {
|
||||
nativeSelectionApplied=true
|
||||
applyNativeSelection(C.TRACK_TYPE_AUDIO,activeAudio)
|
||||
applyNativeSelection(C.TRACK_TYPE_TEXT,activeSubtitle)
|
||||
}
|
||||
}
|
||||
override fun onPlayerError(e: PlaybackException) {
|
||||
Log.w("KaleidoPlayback","ERROR code=${e.errorCodeName} mode=$mode")
|
||||
if(mode<2){scope.launch{prepare(mode+1,position)}}else{loading=false;error="Playback failed (${e.errorCodeName}). Check the server transcoder, network connection, and media availability."}
|
||||
if(mode<2&&localValidationFile==null)fallback(e.errorCodeName)else{loading=false;error="Playback failed (${e.errorCodeName}). Check the server transcoder, network connection, and media availability."}
|
||||
}
|
||||
})
|
||||
scope.launch {prepare(mode,position)}
|
||||
if(audit!=null)scope.launch{while(isActive){delay(1000);audit.sample(mode)}}
|
||||
scope.launch {var ticks=0;while(isActive){delay(250);if(!preparing){position=currentPosition();bufferedPosition=player.bufferedPosition.coerceIn(0,duration);val d=player.duration;if(d>0)duration=if(mode>0)item.duration else d};if(++ticks%40==0&&!preparing&&!ended)report(if(isPlaying)"playing"else "paused")}}
|
||||
}
|
||||
private fun currentPosition() = player.currentPosition.coerceIn(0,duration.coerceAtLeast(0))
|
||||
private fun report(state: String,final: Boolean=false){reports.trySend(TimelineReport(position,state,duration,final))}
|
||||
suspend fun prepare(newMode: Int,offset: Long,resume:Boolean=true) {
|
||||
private fun fallback(reason:String) {
|
||||
if(closed||mode>=2||localValidationFile!=null||fallbackPending)return
|
||||
fallbackPending=true
|
||||
val session=streamSession;val offset=if(preparing)position else currentPosition();val resume=player.playWhenReady
|
||||
scope.launch {
|
||||
try {prepareMutex.withLock {
|
||||
if(!closed&&session==streamSession) {
|
||||
audit?.event("fallback",org.json.JSONObject().put("reason",reason).put("fromMode",mode).put("offset",offset))
|
||||
prepareInternal(mode+1,offset,resume)
|
||||
}
|
||||
}}finally{fallbackPending=false}
|
||||
}
|
||||
}
|
||||
suspend fun prepare(newMode: Int,offset: Long,resume:Boolean=true) = prepareMutex.withLock{prepareInternal(newMode,offset,resume)}
|
||||
private suspend fun prepareInternal(newMode: Int,offset: Long,resume:Boolean) {
|
||||
if(closed)return
|
||||
preparing=true;loading=true;error=null;ended=false;player.stop()
|
||||
preparing=true;loading=true;error=null;ended=false;nativeSelectionApplied=false
|
||||
serverConverting=false;serverAudioConverting=false
|
||||
// Empty track events from stop must not be mistaken for a missing video track.
|
||||
player.stop()
|
||||
try {
|
||||
if(mode>0)runCatching{api.stopTranscode(streamSession)}
|
||||
mode=newMode;streamSession=UUID.randomUUID().toString();streamStart=if(mode>0)offset else 0L
|
||||
audit?.event("prepare",org.json.JSONObject().put("mode",mode).put("offset",offset))
|
||||
val media=item.media.first();val part=media.parts.first()
|
||||
val builder=MediaItem.Builder().setMediaId(item.id).setMediaMetadata(MediaMetadata.Builder().setTitle(item.title).setArtist(item.grandparentTitle).build())
|
||||
if(mode==0) {
|
||||
builder.setUri(api.url(part.key))
|
||||
builder.setUri(if(localValidationFile!=null)android.net.Uri.fromFile(localValidationFile).toString()else api.url(part.key))
|
||||
builder.setSubtitleConfigurations(part.streams.filter{it.type==3&&it.key.isNotBlank()&&it.codec in listOf("srt","webvtt","vtt","ass","ssa")}.map {s ->
|
||||
MediaItem.SubtitleConfiguration.Builder(android.net.Uri.parse(api.url(s.key))).setId(s.id).setLabel(s.title).setLanguage(s.language).setMimeType(when(s.codec){"srt"->MimeTypes.APPLICATION_SUBRIP;"ass","ssa"->MimeTypes.TEXT_SSA;else->MimeTypes.TEXT_VTT}).setSelectionFlags(if(s.id==activeSubtitle)C.SELECTION_FLAG_DEFAULT else 0).build()
|
||||
})
|
||||
} else {
|
||||
val p=api.transcodeParams(item,streamSession,mode,offset,vm.streamQuality.conversion)
|
||||
val decision=api.container("/video/:/transcode/universal/decision",p)
|
||||
val streams=decision.optJSONArray("Metadata").objects().flatMap{it.optJSONArray("Media").objects()}
|
||||
.flatMap{it.optJSONArray("Part").objects()}.flatMap{it.optJSONArray("Stream").objects()}
|
||||
serverConverting=streams.any{it.optInt("streamType")==1&&it.optString("decision")=="transcode"}
|
||||
serverAudioConverting=streams.any{it.optInt("streamType")==2&&it.optString("decision")=="transcode"}
|
||||
audit?.event("decision",org.json.JSONObject().apply{
|
||||
listOf("generalDecisionCode","generalDecisionText","directPlayDecisionCode","directPlayDecisionText","transcodeDecisionCode","transcodeDecisionText").forEach{k->if(decision.has(k))put(k,decision.get(k))}
|
||||
}
|
||||
.put("streams",org.json.JSONArray(decision.optJSONArray("Metadata").objects().flatMap{it.optJSONArray("Media").objects()}
|
||||
.flatMap{it.optJSONArray("Part").objects()}.flatMap{it.optJSONArray("Stream").objects()}.map{s->
|
||||
org.json.JSONObject().apply{listOf("streamType","codec","decision","profile","bitDepth","colorTrc","DOVIProfile").forEach{k->if(s.has(k))put(k,s.get(k))}}
|
||||
})))
|
||||
if(decision.optInt("generalDecisionCode")>=2000)throw PlexException(decision.optString("generalDecisionText","Server cannot convert this media."))
|
||||
builder.setUri(api.url("/video/:/transcode/universal/start.m3u8",p)).setMimeType(MimeTypes.APPLICATION_M3U8)
|
||||
}
|
||||
// Plex HLS exposes the original absolute timeline, including a seek-start offset.
|
||||
player.setMediaItem(builder.build(),offset);player.prepare();player.playWhenReady=resume;player.setPlaybackSpeed(playbackRate)
|
||||
Log.i("KaleidoPlayback","PREPARE item=${item.id} mode=$mode offset=$offset")
|
||||
}catch(e:CancellationException){throw e}catch(e:Exception){if(newMode<2)prepare(newMode+1,offset)else{preparing=false;loading=false;error=e.message ?: "The server could not start playback."}}
|
||||
}catch(e:CancellationException){throw e}catch(e:Exception){
|
||||
audit?.event("prepareFailure",org.json.JSONObject().put("mode",newMode).put("kind",e.javaClass.simpleName).put("httpStatus",(e as? PlexException)?.status))
|
||||
if(newMode<2&&localValidationFile==null)prepareInternal(newMode+1,offset,resume)else{preparing=false;loading=false;error=e.message ?: "The server could not start playback."}
|
||||
}
|
||||
}
|
||||
fun seekTo(target:Long){val clamped=target.coerceIn(0,duration);if(mode>0&&clamped<streamStart){val resume=player.playWhenReady;scope.launch{prepare(mode,clamped,resume)}}else{player.seekTo(clamped);position=clamped;report(if(isPlaying)"playing"else "paused")}}
|
||||
fun toggle(){if(player.isPlaying)player.pause()else{if(ended)seekTo(0);player.play()}}
|
||||
fun retry(){scope.launch{prepare(mode,position)}}
|
||||
fun changeQuality(value:StreamQuality){val resume=player.playWhenReady;vm.chooseStreamQuality(value);scope.launch{prepare(if(value==StreamQuality.Original)1 else 2,position,resume)}}
|
||||
fun changeQuality(value:StreamQuality){val resume=player.playWhenReady;vm.chooseStreamQuality(value);scope.launch{prepare(if(value==StreamQuality.Original)0 else 2,position,resume)}}
|
||||
fun changeSpeed(value:Float){playbackRate=value;player.setPlaybackSpeed(value)}
|
||||
/** Moves the pending seek target; repeated presses accumulate into one seek after [settle] ms. */
|
||||
fun scrubBy(delta:Long,settle:Long=650){scrubPosition=((scrubPosition ?: position)+delta).coerceIn(0,duration);seekJob?.cancel();seekJob=scope.launch{delay(settle);commitScrub()}}
|
||||
|
|
@ -140,6 +210,21 @@ class PlaybackController(private val vm: KaleidoViewModel,val item: Title) {
|
|||
fun select(type:Int,groupIndex:Int,trackIndex:Int) {
|
||||
val group=tracks.groups[groupIndex];selector.parameters=selector.buildUponParameters().setTrackTypeDisabled(type,false).setOverrideForType(TrackSelectionOverride(group.mediaTrackGroup,listOf(trackIndex))).build()
|
||||
}
|
||||
private fun nativeTrack(type:Int,stream:Stream):Pair<Int,Int>? {
|
||||
val candidates=tracks.groups.withIndex().filter{it.value.type==type}.flatMap{g->(0 until g.value.length).map{g.index to it}}
|
||||
if(stream.key.isNotBlank())return candidates.firstOrNull{(g,t)->tracks.groups[g].getTrackFormat(t).id==stream.id}
|
||||
val ordinal=item.media.first().parts.first().streams.filter{it.type==stream.type&&it.key.isBlank()}.indexOfFirst{it.id==stream.id}
|
||||
return candidates.filter{(g,t)->tracks.groups[g].getTrackFormat(t).id !in item.media.first().parts.first().streams.filter{it.key.isNotBlank()}.map{it.id}}.getOrNull(ordinal)
|
||||
}
|
||||
private fun applyNativeSelection(type:Int,id:String) {
|
||||
if(type==C.TRACK_TYPE_TEXT&&id=="0") {
|
||||
selector.parameters=selector.buildUponParameters().setTrackTypeDisabled(type,true).build();return
|
||||
}
|
||||
val stream=item.media.first().parts.first().streams.firstOrNull{it.id==id} ?: return
|
||||
val match=nativeTrack(type,stream) ?: return
|
||||
if(tracks.groups[match.first].isTrackSupported(match.second,true))select(type,match.first,match.second)
|
||||
else fallback("unsupportedSelectedTrack")
|
||||
}
|
||||
fun selectServerStream(stream:Stream?,type:Int) {
|
||||
scope.launch {
|
||||
try {
|
||||
|
|
@ -147,18 +232,13 @@ class PlaybackController(private val vm: KaleidoViewModel,val item: Title) {
|
|||
if(type==C.TRACK_TYPE_TEXT){activeSubtitle=stream?.id ?: "0";api.selectStreams(part,subtitle=activeSubtitle)}else{activeAudio=stream?.id.orEmpty();api.selectStreams(part,audio=activeAudio)}
|
||||
if(mode>0){prepare(mode,position,player.playWhenReady);return@launch}
|
||||
if(stream==null){selector.parameters=selector.buildUponParameters().setTrackTypeDisabled(type,true).build();return@launch}
|
||||
val groups=tracks.groups.withIndex().filter{it.value.type==type}
|
||||
val matching=groups.flatMap{g->(0 until g.value.length).map{g.index to it}}.firstOrNull{(g,t)->tracks.groups[g].getTrackFormat(t).id==stream.id}
|
||||
val matchingExternal=if(type==C.TRACK_TYPE_TEXT&&stream.key.isNotBlank())matching else null
|
||||
val streamIndex=part.streams.filter{it.type==(if(type==C.TRACK_TYPE_AUDIO)2 else 3)&&it.key.isBlank()}.indexOfFirst{it.id==stream.id}
|
||||
val candidates=groups.flatMap{g->(0 until g.value.length).map{g.index to it}}
|
||||
val match=matchingExternal ?: matching ?: candidates.getOrNull(streamIndex)
|
||||
if(match!=null)select(type,match.first,match.second)
|
||||
val match=nativeTrack(type,stream)
|
||||
if(match!=null&&tracks.groups[match.first].isTrackSupported(match.second,true))select(type,match.first,match.second)
|
||||
else prepare(2,position) // Image subtitles and unsupported embedded tracks are burned by Plex.
|
||||
}catch(e:Exception){error=e.message ?: "Could not change the selected track."}
|
||||
}
|
||||
}
|
||||
fun close(){if(closed)return;position=currentPosition();closed=true;scope.cancel();report("stopped",true);reports.close();mediaSession.release();player.release()}
|
||||
fun close(){if(closed)return;position=currentPosition();closed=true;scope.cancel();audit?.finish(mode);report("stopped",true);reports.close();mediaSession.release();player.release()}
|
||||
}
|
||||
/** The single playback quality preference: the original stream, then conversions from highest to lowest resolution. */
|
||||
enum class StreamQuality(val label:String,val width:Int,val height:Int,val kbps:Int) {
|
||||
|
|
|
|||
71
app/src/main/java/tv/kaleido/app/PlaybackValidationLog.kt
Normal file
71
app/src/main/java/tv/kaleido/app/PlaybackValidationLog.kt
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
package tv.kaleido.app
|
||||
|
||||
import android.content.Context
|
||||
import android.util.Base64
|
||||
import androidx.media3.common.*
|
||||
import androidx.media3.common.util.UnstableApi
|
||||
import androidx.media3.exoplayer.*
|
||||
import androidx.media3.exoplayer.analytics.AnalyticsListener
|
||||
import androidx.media3.exoplayer.audio.AudioSink
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.channels.Channel
|
||||
import kotlinx.coroutines.launch
|
||||
import org.json.JSONArray
|
||||
import org.json.JSONObject
|
||||
|
||||
/** Opt-in debug evidence only: never serialize media URLs, headers, or session credentials. */
|
||||
@androidx.annotation.OptIn(UnstableApi::class)
|
||||
internal class PlaybackValidationLog(app:KaleidoApplication,item:Title,private val player:ExoPlayer):AnalyticsListener {
|
||||
private val lines=Channel<String>(Channel.UNLIMITED)
|
||||
init {
|
||||
app.scope.launch(Dispatchers.IO) {
|
||||
app.openFileOutput("playback-validation.jsonl",Context.MODE_PRIVATE).bufferedWriter().use { out->
|
||||
for(line in lines){out.write(line);out.newLine();out.flush()}
|
||||
}
|
||||
}
|
||||
event("source",JSONObject().put("id",item.id).put("title",item.title).put("type",item.type)
|
||||
.put("media",JSONArray(item.media.map{JSONObject().put("videoCodec",it.videoCodec).put("audioCodec",it.audioCodec)
|
||||
.put("width",it.width).put("height",it.height).put("container",it.container).put("bitrate",it.bitrate)})))
|
||||
}
|
||||
fun event(name:String,data:JSONObject=JSONObject()) {
|
||||
data.put("event",name).put("elapsedMs",android.os.SystemClock.elapsedRealtime())
|
||||
lines.trySend(data.toString())
|
||||
}
|
||||
private fun format(f:Format):JSONObject=JSONObject().put("mime",f.sampleMimeType).put("codecs",f.codecs)
|
||||
.put("width",f.width).put("height",f.height).put("fps",f.frameRate).put("channels",f.channelCount).put("channelMask",f.channelMask).put("sampleRate",f.sampleRate).put("pcmEncoding",f.pcmEncoding)
|
||||
.apply { f.colorInfo?.let { c->
|
||||
put("colorSpace",c.colorSpace).put("colorRange",c.colorRange).put("colorTransfer",c.colorTransfer)
|
||||
put("hdrStaticInfo",c.hdrStaticInfo?.let{Base64.encodeToString(it,Base64.NO_WRAP)} ?: JSONObject.NULL)
|
||||
}}
|
||||
fun sample(mode:Int) {
|
||||
event("sample",JSONObject().put("mode",mode).put("state",player.playbackState).put("playing",player.isPlaying)
|
||||
.put("position",player.currentPosition).put("buffered",player.bufferedPosition).put("duration",player.duration)
|
||||
.put("video",player.videoFormat?.let(::format)).put("audio",player.audioFormat?.let(::format))
|
||||
.put("videoCounters",player.videoDecoderCounters?.let(::counters)).put("audioCounters",player.audioDecoderCounters?.let(::counters)))
|
||||
}
|
||||
private fun counters(c:DecoderCounters):JSONObject {
|
||||
c.ensureUpdated()
|
||||
return JSONObject().put("queued",c.queuedInputBufferCount).put("rendered",c.renderedOutputBufferCount)
|
||||
.put("dropped",c.droppedBufferCount).put("skipped",c.skippedOutputBufferCount)
|
||||
.put("maxConsecutiveDropped",c.maxConsecutiveDroppedBufferCount)
|
||||
}
|
||||
override fun onVideoDecoderInitialized(t:AnalyticsListener.EventTime,name:String,a:Long,b:Long){event("videoDecoder",JSONObject().put("name",name))}
|
||||
override fun onAudioDecoderInitialized(t:AnalyticsListener.EventTime,name:String,a:Long,b:Long){event("audioDecoder",JSONObject().put("name",name))}
|
||||
override fun onVideoInputFormatChanged(t:AnalyticsListener.EventTime,f:Format,reuse:DecoderReuseEvaluation?){event("videoFormat",format(f))}
|
||||
override fun onAudioInputFormatChanged(t:AnalyticsListener.EventTime,f:Format,reuse:DecoderReuseEvaluation?){event("audioFormat",format(f))}
|
||||
override fun onRenderedFirstFrame(t:AnalyticsListener.EventTime,output:Any,renderTimeMs:Long){event("firstFrame")}
|
||||
override fun onAudioPositionAdvancing(t:AnalyticsListener.EventTime,startTimeMs:Long){event("audioAdvancing")}
|
||||
override fun onAudioTrackInitialized(t:AnalyticsListener.EventTime,c:AudioSink.AudioTrackConfig){
|
||||
event("audioTrack",JSONObject().put("encoding",c.encoding).put("sampleRate",c.sampleRate)
|
||||
.put("channelMask",c.channelConfig).put("offload",c.offload).put("tunneling",c.tunneling))
|
||||
}
|
||||
override fun onAudioUnderrun(t:AnalyticsListener.EventTime,size:Int,duration:Long,elapsed:Long){event("audioUnderrun",JSONObject().put("elapsedMsSinceFeed",elapsed))}
|
||||
override fun onPlayerError(t:AnalyticsListener.EventTime,e:PlaybackException){
|
||||
val http=generateSequence<Throwable>(e){it.cause}.filterIsInstance<androidx.media3.datasource.HttpDataSource.InvalidResponseCodeException>().firstOrNull()
|
||||
event("error",JSONObject().put("code",e.errorCodeName).put("httpStatus",http?.responseCode)
|
||||
.put("causes",JSONArray(generateSequence<Throwable>(e){it.cause}.map{cause->
|
||||
JSONObject().put("type",cause.javaClass.simpleName).put("frames",JSONArray(cause.stackTrace.take(4).map{"${it.className}.${it.methodName}:${it.lineNumber}"}))
|
||||
}.toList())))
|
||||
}
|
||||
fun finish(mode:Int){sample(mode);event("closed");lines.close()}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue