feat(media,android): ExoPlayer DataSource for sealed files, demo player app
:shade-android-media wraps the Kotlin MediaReader as a Media3 DataSource, so ExoPlayer plays and seeks any container from a sealed file; a tampered segment fails the read instead of reaching the decoder. :shade-media-demo opens a shared watch link and plays it. Verified: Robolectric — byte-exact reads whole and in ranges, tamper refusal, ExoPlayer plays a sealed H.264/AAC clip to the end and reopens mid-file on seek. Kotlin suite 80/0. Not yet run on a device. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
29
android/shade-media-demo/src/main/AndroidManifest.xml
Normal file
29
android/shade-media-demo/src/main/AndroidManifest.xml
Normal file
@@ -0,0 +1,29 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
|
||||
<uses-permission android:name="android.permission.INTERNET" />
|
||||
|
||||
<!-- Cleartext is allowed so the demo can reach a relay on the LAN
|
||||
(`shade media serve` speaks plain HTTP). The media is sealed end to
|
||||
end either way; TLS would hide only which file is fetched. -->
|
||||
<application
|
||||
android:label="Shade Media"
|
||||
android:usesCleartextTraffic="true"
|
||||
android:theme="@android:style/Theme.Material.NoActionBar">
|
||||
<activity
|
||||
android:name=".PlayerActivity"
|
||||
android:exported="true"
|
||||
android:configChanges="orientation|screenSize|screenLayout|keyboardHidden">
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.MAIN" />
|
||||
<category android:name="android.intent.category.LAUNCHER" />
|
||||
</intent-filter>
|
||||
<!-- Share a watch link to the app from any browser or chat. -->
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.SEND" />
|
||||
<category android:name="android.intent.category.DEFAULT" />
|
||||
<data android:mimeType="text/plain" />
|
||||
</intent-filter>
|
||||
</activity>
|
||||
</application>
|
||||
</manifest>
|
||||
@@ -0,0 +1,136 @@
|
||||
package no.zyon.shade.mediademo
|
||||
|
||||
import android.app.Activity
|
||||
import android.content.Intent
|
||||
import android.graphics.Color
|
||||
import android.os.Bundle
|
||||
import android.view.Gravity
|
||||
import android.view.ViewGroup.LayoutParams.MATCH_PARENT
|
||||
import android.view.ViewGroup.LayoutParams.WRAP_CONTENT
|
||||
import android.widget.Button
|
||||
import android.widget.EditText
|
||||
import android.widget.LinearLayout
|
||||
import android.widget.TextView
|
||||
import androidx.annotation.OptIn
|
||||
import androidx.media3.common.PlaybackException
|
||||
import androidx.media3.common.Player
|
||||
import androidx.media3.common.util.UnstableApi
|
||||
import androidx.media3.exoplayer.ExoPlayer
|
||||
import androidx.media3.ui.PlayerView
|
||||
import no.zyon.shade.media.MediaReader
|
||||
import no.zyon.shade.media.android.ShadeMedia
|
||||
import java.util.concurrent.Executors
|
||||
|
||||
/**
|
||||
* Plays a sealed Shade film, video, song or picture-less audio file.
|
||||
*
|
||||
* The link comes from a share (`ACTION_SEND` text), from
|
||||
* `adb shell am start -n no.zyon.shade.mediademo/.PlayerActivity --es link '<link>'`,
|
||||
* or is pasted into the field. Opening the reader fetches the footer and
|
||||
* trailer over the network, so it runs off the main thread.
|
||||
*/
|
||||
@OptIn(UnstableApi::class)
|
||||
class PlayerActivity : Activity() {
|
||||
private val io = Executors.newSingleThreadExecutor()
|
||||
private var player: ExoPlayer? = null
|
||||
private var reader: MediaReader? = null
|
||||
private lateinit var status: TextView
|
||||
private lateinit var view: PlayerView
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
val field = EditText(this).apply {
|
||||
hint = "https://…/watch/#shade=…"
|
||||
setSingleLine()
|
||||
}
|
||||
val open = Button(this).apply {
|
||||
text = "Open"
|
||||
setOnClickListener { play(field.text.toString().trim()) }
|
||||
}
|
||||
status = TextView(this).apply {
|
||||
setTextColor(Color.LTGRAY)
|
||||
setPadding(24, 12, 24, 12)
|
||||
}
|
||||
view = PlayerView(this).apply { setBackgroundColor(Color.BLACK) }
|
||||
val bar = LinearLayout(this).apply {
|
||||
orientation = LinearLayout.HORIZONTAL
|
||||
gravity = Gravity.CENTER_VERTICAL
|
||||
addView(field, LinearLayout.LayoutParams(0, WRAP_CONTENT, 1f))
|
||||
addView(open)
|
||||
}
|
||||
setContentView(
|
||||
LinearLayout(this).apply {
|
||||
orientation = LinearLayout.VERTICAL
|
||||
setBackgroundColor(Color.rgb(15, 17, 21))
|
||||
addView(bar, LinearLayout.LayoutParams(MATCH_PARENT, WRAP_CONTENT))
|
||||
addView(status, LinearLayout.LayoutParams(MATCH_PARENT, WRAP_CONTENT))
|
||||
addView(view, LinearLayout.LayoutParams(MATCH_PARENT, 0, 1f))
|
||||
},
|
||||
)
|
||||
linkFrom(intent)?.let {
|
||||
field.setText(it)
|
||||
play(it)
|
||||
}
|
||||
}
|
||||
|
||||
override fun onNewIntent(intent: Intent) {
|
||||
super.onNewIntent(intent)
|
||||
linkFrom(intent)?.let { play(it) }
|
||||
}
|
||||
|
||||
private fun linkFrom(intent: Intent?): String? = when (intent?.action) {
|
||||
Intent.ACTION_SEND -> intent.getStringExtra(Intent.EXTRA_TEXT)
|
||||
else -> intent?.getStringExtra("link")
|
||||
}?.let { text -> Regex("\\S*#\\S*shade=[A-Za-z0-9_-]+").find(text)?.value }
|
||||
|
||||
private fun play(link: String) {
|
||||
if (link.isEmpty()) return
|
||||
stop()
|
||||
status.text = "Opening…"
|
||||
io.execute {
|
||||
val opened = runCatching { ShadeMedia.open(link) }
|
||||
runOnUiThread {
|
||||
opened.fold(
|
||||
onSuccess = { start(it) },
|
||||
onFailure = { status.text = "Cannot open: ${it.message}" },
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun start(r: MediaReader) {
|
||||
reader = r
|
||||
val p = ExoPlayer.Builder(this).build()
|
||||
player = p
|
||||
view.player = p
|
||||
val name = r.meta.name ?: "sealed file"
|
||||
status.text = "$name · ${r.meta.mime ?: "?"} · ${r.size / 1024} KiB · end-to-end encrypted"
|
||||
p.addListener(object : Player.Listener {
|
||||
override fun onPlayerError(error: PlaybackException) {
|
||||
status.text = "Playback failed: ${error.cause?.message ?: error.message}"
|
||||
}
|
||||
})
|
||||
p.setMediaSource(ShadeMedia.mediaSource(r))
|
||||
p.prepare()
|
||||
p.play()
|
||||
}
|
||||
|
||||
private fun stop() {
|
||||
view.player = null
|
||||
player?.release()
|
||||
player = null
|
||||
reader?.close()
|
||||
reader = null
|
||||
}
|
||||
|
||||
override fun onStop() {
|
||||
super.onStop()
|
||||
player?.pause()
|
||||
}
|
||||
|
||||
override fun onDestroy() {
|
||||
stop()
|
||||
io.shutdown()
|
||||
super.onDestroy()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user