aboutsummaryrefslogtreecommitdiffstats
path: root/packages/meshbay-android/app/src/main
diff options
context:
space:
mode:
Diffstat (limited to 'packages/meshbay-android/app/src/main')
-rw-r--r--packages/meshbay-android/app/src/main/assets/bridge/meshbay-bridge.js18
-rw-r--r--packages/meshbay-android/app/src/main/kotlin/org/meshbay/client/MainActivity.kt23
-rw-r--r--packages/meshbay-android/app/src/main/kotlin/org/meshbay/client/bridge/Channels.kt3
-rw-r--r--packages/meshbay-android/app/src/main/kotlin/org/meshbay/client/cast/CastChannels.kt59
-rw-r--r--packages/meshbay-android/app/src/main/kotlin/org/meshbay/client/cast/CastControl.kt47
-rw-r--r--packages/meshbay-android/app/src/main/kotlin/org/meshbay/client/cast/CastRelay.kt156
-rw-r--r--packages/meshbay-android/app/src/main/kotlin/org/meshbay/client/keys/Kdf.kt2
-rw-r--r--packages/meshbay-android/app/src/main/kotlin/org/meshbay/client/keys/Keyring.kt2
-rw-r--r--packages/meshbay-android/app/src/main/kotlin/org/meshbay/client/keys/Transcripts.kt2
-rw-r--r--packages/meshbay-android/app/src/main/kotlin/org/meshbay/client/save/SaveNames.kt1
10 files changed, 292 insertions, 21 deletions
diff --git a/packages/meshbay-android/app/src/main/assets/bridge/meshbay-bridge.js b/packages/meshbay-android/app/src/main/assets/bridge/meshbay-bridge.js
index 7ace33b..b71b5e8 100644
--- a/packages/meshbay-android/app/src/main/assets/bridge/meshbay-bridge.js
+++ b/packages/meshbay-android/app/src/main/assets/bridge/meshbay-bridge.js
@@ -50,6 +50,10 @@
// per 48 KB chunk). Without ArrayBuffer messages, base64 over JSON.
const SAVE_WRITE = 1;
const CAST_PUSH = 2;
+ const CAST_FILE = 3;
+ // A file crosses in pieces this size: big enough to be few, small enough
+ // that no one message is a burden to the WebView or the bridge.
+ const FILE_PIECE = 1024 * 1024;
const bytesOf = (chunk) => (chunk instanceof Uint8Array ? chunk
: ArrayBuffer.isView(chunk) ? new Uint8Array(chunk.buffer, chunk.byteOffset, chunk.byteLength)
: new Uint8Array(chunk));
@@ -151,6 +155,19 @@
push: (data) => pushSegment(data),
stop: () => call('cast:stop'),
subtitle: (sub) => call('cast:subtitle', sub),
+ // A whole file — a photo, a track — in pieces: one message the size of a
+ // lossless track would be the WebView's to refuse.
+ file: async (f) => {
+ const bytes = bytesOf(f.bytes);
+ const cover = f.cover ? { bytes: base64Of(bytesOf(f.cover.bytes)), type: f.cover.type } : null;
+ await call('cast:file:begin', { type: f.type, size: bytes.length, cover,
+ title: f.title, artist: f.artist, album: f.album });
+ for (let at = 0; at < bytes.length; at += FILE_PIECE) {
+ const piece = bytes.subarray(at, at + FILE_PIECE);
+ await (BINARY ? sendBinary(CAST_FILE, 0, piece) : call('cast:file:write', base64Of(piece)));
+ }
+ return call('cast:file:end');
+ },
finish: () => call('cast:finish'),
status: () => call('cast:status'),
scan: () => call('cast:scan'),
@@ -160,6 +177,7 @@
chromecastDisconnect: () => call('cast:chromecast:disconnect'),
chromecastPause: () => call('cast:chromecast:pause'),
chromecastPlay: () => call('cast:chromecast:play'),
+ chromecastSeek: (seconds) => call('cast:chromecast:seek', seconds),
} } : {}),
// Music is playing: keep the process, the Wi-Fi and the page awake with
diff --git a/packages/meshbay-android/app/src/main/kotlin/org/meshbay/client/MainActivity.kt b/packages/meshbay-android/app/src/main/kotlin/org/meshbay/client/MainActivity.kt
index fc446b9..0fa63d6 100644
--- a/packages/meshbay-android/app/src/main/kotlin/org/meshbay/client/MainActivity.kt
+++ b/packages/meshbay-android/app/src/main/kotlin/org/meshbay/client/MainActivity.kt
@@ -6,9 +6,11 @@ import android.content.Intent
import android.net.Uri
import android.os.Build
import android.os.Bundle
+import android.os.SystemClock
import android.util.Log
import android.view.View
import android.view.ViewGroup
+import android.view.ViewTreeObserver
import android.view.WindowInsets
import android.webkit.ConsoleMessage
import android.webkit.PermissionRequest
@@ -66,6 +68,7 @@ class MainActivity : Activity() {
root = FrameLayout(this)
setContentView(root)
applyInsets(root)
+ if (savedInstanceState == null && Build.VERSION.SDK_INT >= 31) holdSplash()
// A WebView too old for the page's crypto would fail at the first
// handshake; say so before loading anything.
@@ -255,6 +258,22 @@ class MainActivity : Activity() {
catch (e: android.content.ActivityNotFoundException) { Log.w(Bridge.TAG, "no browser for $url") }
}
+ /**
+ * Android 12+ shows the launcher icon until the first frame is drawn, which
+ * on a warm process is a flash. Holding that frame keeps it up long enough
+ * to be seen; the WebView loads underneath in the meantime.
+ */
+ private fun holdSplash() {
+ val until = SystemClock.uptimeMillis() + SPLASH_MS
+ root.viewTreeObserver.addOnPreDrawListener(object : ViewTreeObserver.OnPreDrawListener {
+ override fun onPreDraw(): Boolean {
+ if (SystemClock.uptimeMillis() < until) return false
+ root.viewTreeObserver.removeOnPreDrawListener(this)
+ return true
+ }
+ })
+ }
+
/** Edge-to-edge is enforced from Android 15: keep the page clear of the bars and the keyboard. */
private fun applyInsets(view: View) {
view.setOnApplyWindowInsetsListener { v, insets ->
@@ -296,4 +315,8 @@ class MainActivity : Activity() {
if (::web.isInitialized) { root.removeView(web); web.destroy() }
super.onDestroy()
}
+
+ companion object {
+ private const val SPLASH_MS = 500L
+ }
}
diff --git a/packages/meshbay-android/app/src/main/kotlin/org/meshbay/client/bridge/Channels.kt b/packages/meshbay-android/app/src/main/kotlin/org/meshbay/client/bridge/Channels.kt
index 7542fa1..f7094a0 100644
--- a/packages/meshbay-android/app/src/main/kotlin/org/meshbay/client/bridge/Channels.kt
+++ b/packages/meshbay-android/app/src/main/kotlin/org/meshbay/client/bridge/Channels.kt
@@ -63,6 +63,7 @@ class Channels(
fun binary(frame: BinaryFrame): Any? = when (frame.channel) {
BinaryFrame.SAVE_WRITE -> saves().write(frame.handle, frame.bytes, frame.offset, frame.length)
BinaryFrame.CAST_PUSH -> (cast ?: throw Refused("Refused: no such channel")).push(frame.bytes, frame.offset, frame.length)
+ BinaryFrame.CAST_FILE -> (cast ?: throw Refused("Refused: no such channel")).writeFile(frame.bytes, frame.offset, frame.length)
else -> throw Refused("Refused: no such channel")
}
@@ -72,7 +73,7 @@ class Channels(
* they could overtake one another; these run on one thread, in arrival order.
*/
fun ordered(channel: String) = cast?.ordered(channel) == true
- fun ordered(frame: BinaryFrame) = frame.channel == BinaryFrame.CAST_PUSH
+ fun ordered(frame: BinaryFrame) = frame.channel == BinaryFrame.CAST_PUSH || frame.channel == BinaryFrame.CAST_FILE
private fun setLocale(code: String): String {
// A code, never text, and only one the package has a catalogue for.
diff --git a/packages/meshbay-android/app/src/main/kotlin/org/meshbay/client/cast/CastChannels.kt b/packages/meshbay-android/app/src/main/kotlin/org/meshbay/client/cast/CastChannels.kt
index 74a086b..edaec67 100644
--- a/packages/meshbay-android/app/src/main/kotlin/org/meshbay/client/cast/CastChannels.kt
+++ b/packages/meshbay-android/app/src/main/kotlin/org/meshbay/client/cast/CastChannels.kt
@@ -26,8 +26,9 @@ class CastChannels(
fun handles(channel: String) = channel.startsWith("cast:")
- /** What must reach the relay in the order the page sent it: start, pushes, subtitle, finish, stop. */
- fun ordered(channel: String) = channel in setOf("cast:start", "cast:push", "cast:subtitle", "cast:finish", "cast:stop")
+ /** What must reach the relay in the order the page sent it: start, pushes, subtitle, a file's pieces, finish, stop. */
+ fun ordered(channel: String) = channel in setOf("cast:start", "cast:push", "cast:subtitle",
+ "cast:file:begin", "cast:file:write", "cast:file:end", "cast:finish", "cast:stop")
fun call(channel: String, a: JSONArray): Any? = when (channel) {
"cast:start" -> {
@@ -38,6 +39,20 @@ class CastChannels(
started
}
"cast:subtitle" -> relay.setSubtitle(a.optJSONObject(0))
+ // A whole file — a photo, a track — in pieces. No foreground service
+ // for one: a photo needs nothing once loaded, and music keeps the page
+ // alive through its own playback hold.
+ "cast:file:begin" -> {
+ val o = a.optJSONObject(0) ?: JSONObject()
+ val cover = o.optJSONObject("cover")
+ relay.beginFile(o.optString("type", ""), o.optLong("size", -1),
+ cover?.optString("bytes", "")?.takeIf { it.isNotEmpty() }?.let { Base64.getDecoder().decode(it) },
+ cover?.optString("type", ""), o)
+ }
+ "cast:file:write" -> { // the base64 path, for a WebView without ArrayBuffer messages
+ relay.writeFile(Base64.getDecoder().decode(a.optString(0, "")))
+ }
+ "cast:file:end" -> relay.endFile()
"cast:push" -> { // the base64 path, for a WebView without ArrayBuffer messages
relay.push(Base64.getDecoder().decode(a.optString(0, ""))); true
}
@@ -50,14 +65,17 @@ class CastChannels(
"cast:devices" -> control.devices()
"cast:chromecast:connect" -> {
val o = a.optJSONObject(0) ?: JSONObject()
- reported { control.connect(o.optString("deviceId", ""), relayUrl(o), subtitleOf(o)) }
+ val url = relayUrl(o)
+ reported { control.connect(o.optString("deviceId", ""), url, subtitleFor(url, o), fileOf(url), startOf(url, o)) }
}
"cast:chromecast:reload" -> {
val o = a.optJSONObject(0) ?: JSONObject()
- reported { control.reload(relayUrl(o), subtitleOf(o)) }
+ val url = relayUrl(o)
+ reported { control.reload(url, subtitleFor(url, o), fileOf(url), startOf(url, o)) }
}
"cast:chromecast:pause" -> control.pause()
"cast:chromecast:play" -> control.play()
+ "cast:chromecast:seek" -> control.seek(a.optDouble(0, 0.0))
"cast:chromecast:disconnect" -> control.disconnect()
else -> throw Refused("Refused: no such channel")
}
@@ -77,21 +95,40 @@ class CastChannels(
fun push(bytes: ByteArray, offset: Int, length: Int): Boolean { relay.push(bytes, offset, length); return true }
+ fun writeFile(bytes: ByteArray, offset: Int, length: Int): Boolean = relay.writeFile(bytes, offset, length)
+
/**
* The receiver is only ever pointed at this relay. A URL the page names is
- * accepted when it is the relay's own, and refused otherwise — a page
- * cannot use this application to make a television fetch anything else.
+ * accepted when it is the relay's own stream or file, and refused
+ * otherwise — a page cannot use this application to make a television
+ * fetch anything else.
*/
private fun relayUrl(o: JSONObject): String {
val asked = o.optString("mediaUrl", "")
val ours = relay.url ?: throw Refused("The cast relay is not running")
- if (asked != ours) throw Refused("Refused: not this relay's stream")
- return ours
+ if (asked != ours && asked != relay.fileUrl) throw Refused("Refused: not this relay's stream")
+ return asked
}
- /** As main.js: no subtitle named means the relay's current one. */
- private fun subtitleOf(o: JSONObject): JSONObject? =
- if (o.has("subtitle") && o.get("subtitle") != JSONObject.NULL) o.optJSONObject("subtitle") else relay.subtitleInfo()
+ /**
+ * The relay's file is loaded as what the relay says it is — its type, and
+ * the title, artist, album and cover it was given — and anything else as
+ * the stream (null here); the page does not say which.
+ */
+ private fun fileOf(url: String): JSONObject? =
+ if (url != relay.fileUrl) null
+ else JSONObject().put("contentType", relay.fileType).put("meta", relay.fileMeta() ?: JSONObject())
+
+ /** Where in a track to begin; a film starts where its relay does. */
+ private fun startOf(url: String, o: JSONObject): Double =
+ if (url == relay.fileUrl) maxOf(0.0, o.optDouble("startAt", 0.0)) else 0.0
+
+ /** As main.js: no subtitle named means the relay's current one; a file has none. */
+ private fun subtitleFor(url: String, o: JSONObject): JSONObject? = when {
+ url == relay.fileUrl -> null
+ o.has("subtitle") && o.get("subtitle") != JSONObject.NULL -> o.optJSONObject("subtitle")
+ else -> relay.subtitleInfo()
+ }
/**
* The Wi-Fi (or Ethernet) address, never the mobile network's: a TV cannot
diff --git a/packages/meshbay-android/app/src/main/kotlin/org/meshbay/client/cast/CastControl.kt b/packages/meshbay-android/app/src/main/kotlin/org/meshbay/client/cast/CastControl.kt
index f574c89..0d18a90 100644
--- a/packages/meshbay-android/app/src/main/kotlin/org/meshbay/client/cast/CastControl.kt
+++ b/packages/meshbay-android/app/src/main/kotlin/org/meshbay/client/cast/CastControl.kt
@@ -2,6 +2,7 @@ package org.meshbay.client.cast
import android.content.Context
import android.graphics.Color
+import android.net.Uri
import android.os.Handler
import android.os.Looper
import android.util.Log
@@ -10,6 +11,9 @@ import androidx.mediarouter.media.MediaRouter
import com.google.android.gms.cast.CastMediaControlIntent
import com.google.android.gms.cast.MediaInfo
import com.google.android.gms.cast.MediaLoadRequestData
+import com.google.android.gms.cast.MediaMetadata
+import com.google.android.gms.cast.MediaSeekOptions
+import com.google.android.gms.common.images.WebImage
import com.google.android.gms.cast.MediaStatus
import com.google.android.gms.cast.MediaTrack
import com.google.android.gms.cast.TextTrackStyle
@@ -111,7 +115,7 @@ class CastControl(private val context: Context) {
}
/** Connect, launch the default receiver, load; answers once the receiver has. */
- fun connect(deviceId: String, mediaUrl: String, subtitle: JSONObject?): JSONObject {
+ fun connect(deviceId: String, mediaUrl: String, subtitle: JSONObject?, file: JSONObject? = null, startAt: Double = 0.0): JSONObject {
need()
val session = onMain {
val cast = CastContext.getSharedInstance(context)
@@ -167,7 +171,7 @@ class CastControl(private val context: Context) {
// device's name included: read it there, never from this worker.
val name = deviceName ?: onMain { session.castDevice?.friendlyName }
Log.i(TAG, "session up on '$name', loading the relay stream")
- val state = load(session, mediaUrl, subtitle)
+ val state = load(session, mediaUrl, subtitle, file, startAt)
watch(session)
return JSONObject().put("deviceName", name ?: JSONObject.NULL).put("playerState", state)
}
@@ -200,18 +204,42 @@ class CastControl(private val context: Context) {
watched = c
}
- fun reload(mediaUrl: String, subtitle: JSONObject?): JSONObject {
+ fun reload(mediaUrl: String, subtitle: JSONObject?, file: JSONObject? = null, startAt: Double = 0.0): JSONObject {
need()
val session = onMain { CastContext.getSharedInstance(context).sessionManager.currentCastSession }
?: throw Refused("Not connected")
- val state = load(session, mediaUrl, subtitle)
+ val state = load(session, mediaUrl, subtitle, file, startAt)
watch(session)
return JSONObject().put("playerState", state)
}
- private fun load(session: CastSession, mediaUrl: String, subtitle: JSONObject?): String {
+ /**
+ * `file` is the relay's description of its file (type and what to show
+ * with it), or null for the stream. A photo is not a stream at all; a
+ * music track is an ordinary buffered file the receiver seeks in itself.
+ */
+ private fun load(session: CastSession, mediaUrl: String, subtitle: JSONObject?,
+ file: JSONObject?, startAt: Double): String {
+ val contentType = file?.optString("contentType", "") ?: "video/mp4"
+ val photo = file != null && contentType.startsWith("image/")
val subUrl = subtitle?.optString("url", "")?.takeIf { it.isNotEmpty() }
- val info = MediaInfo.Builder(mediaUrl)
+ val info = if (photo) {
+ // A picture, not a stream: nothing to seek, nothing to buffer.
+ MediaInfo.Builder(mediaUrl).setContentType(contentType)
+ .setStreamType(MediaInfo.STREAM_TYPE_NONE)
+ .setMetadata(MediaMetadata(MediaMetadata.MEDIA_TYPE_PHOTO)).build()
+ } else if (file != null) {
+ val meta = file.optJSONObject("meta") ?: JSONObject()
+ MediaInfo.Builder(mediaUrl).setContentType(contentType)
+ .setStreamType(MediaInfo.STREAM_TYPE_BUFFERED)
+ .setMetadata(MediaMetadata(MediaMetadata.MEDIA_TYPE_MUSIC_TRACK).apply {
+ putString(MediaMetadata.KEY_TITLE, meta.optString("title", ""))
+ putString(MediaMetadata.KEY_ARTIST, meta.optString("artist", ""))
+ putString(MediaMetadata.KEY_ALBUM_TITLE, meta.optString("album", ""))
+ meta.optString("cover", "").takeIf { it.isNotEmpty() && it != "null" }
+ ?.let { addImage(WebImage(Uri.parse(it))) }
+ }).build()
+ } else MediaInfo.Builder(mediaUrl)
.setContentType("video/mp4")
// LIVE: the relay has no beginning to seek back to — the film's own
// timeline lives on this side, and a seek restarts the relay.
@@ -236,6 +264,7 @@ class CastControl(private val context: Context) {
}
}.build()
val request = MediaLoadRequestData.Builder().setMediaInfo(info).setAutoplay(true)
+ .apply { if (startAt > 0) setCurrentTime((startAt * 1000).toLong()) }
.apply { if (subUrl != null) setActiveTrackIds(longArrayOf(TEXT_TRACK_ID)) }.build()
val loaded = CountDownLatch(1)
var ok = false
@@ -258,6 +287,8 @@ class CastControl(private val context: Context) {
throw IllegalStateException("The receiver did not load the stream")
}
if (!ok) throw IllegalStateException("The receiver refused the stream ($reason)")
+ // A photo has no playing state to wait for: loaded is shown.
+ if (photo) return onMain { stateName(session.remoteMediaClient?.playerState ?: MediaStatus.PLAYER_STATE_UNKNOWN) }
// A load the receiver accepted is not a film on the screen: it answers
// IDLE, then fetches the stream, and only then plays — or gives up.
// So the answer waits for what the receiver actually did (measured
@@ -286,6 +317,10 @@ class CastControl(private val context: Context) {
/** The player as a remote: pause and play the receiver, answered once it has. */
fun pause(): JSONObject = command { it.pause() }
fun play(): JSONObject = command { it.play() }
+ /** Where in a track the receiver should be; a film seeks by restarting the relay instead. */
+ fun seek(seconds: Double): JSONObject = command {
+ it.seek(MediaSeekOptions.Builder().setPosition((maxOf(0.0, seconds) * 1000).toLong()).build())
+ }
private fun command(send: (com.google.android.gms.cast.framework.media.RemoteMediaClient) ->
com.google.android.gms.common.api.PendingResult<com.google.android.gms.cast.framework.media.RemoteMediaClient.MediaChannelResult>): JSONObject {
diff --git a/packages/meshbay-android/app/src/main/kotlin/org/meshbay/client/cast/CastRelay.kt b/packages/meshbay-android/app/src/main/kotlin/org/meshbay/client/cast/CastRelay.kt
index 21328db..5b76f60 100644
--- a/packages/meshbay-android/app/src/main/kotlin/org/meshbay/client/cast/CastRelay.kt
+++ b/packages/meshbay-android/app/src/main/kotlin/org/meshbay/client/cast/CastRelay.kt
@@ -82,6 +82,13 @@ class CastRelay(private val lanAddress: () -> InetAddress?, private val spoolDir
private val spoolSeq = AtomicLong()
private class Subtitle(val vtt: ByteArray, val language: String, val label: String)
+ private class Cover(val bytes: ByteArray, val type: String)
+ /** A whole file on disk, served at `/file`: a photo, or a track with its cover. */
+ private class FileSlot(val path: File, val size: Long, val type: String, val cover: Cover?, val meta: JSONObject)
+ private class Incoming(val slot: FileSlot) {
+ val out = java.io.FileOutputStream(slot.path)
+ var received = 0L
+ }
@Volatile private var server: ServerSocket? = null
@Volatile private var port = 0
@@ -92,6 +99,10 @@ class CastRelay(private val lanAddress: () -> InetAddress?, private val spoolDir
@Volatile private var headerReady = false
@Volatile private var subtitle: Subtitle? = null
@Volatile private var subtitleVersion = 0
+ @Volatile private var file: FileSlot? = null
+ @Volatile private var fileVersion = 0
+ private var incoming: Incoming? = null // guarded by this
+ private val fileSeq = AtomicLong()
/** How many times a receiver asked for the stream since the relay started. */
@Volatile var streamRequests = 0
private set
@@ -113,6 +124,77 @@ class CastRelay(private val lanAddress: () -> InetAddress?, private val spoolDir
if (server == null || subtitle == null) null
else "http://${hostPart()}:$port/subs.vtt?t=$token&v=$subtitleVersion"
+ /** Versioned like the subtitle: a receiver handed the same address twice shows what it has. */
+ val fileUrl get() =
+ if (server == null || file == null) null
+ else "http://${hostPart()}:$port/file?t=$token&v=$fileVersion"
+
+ val fileType get() = file?.type
+
+ /** What the receiver is told the file is: title, artist, album, and the cover's address. */
+ fun fileMeta(): JSONObject? = file?.let { f ->
+ JSONObject(f.meta.toString()).put("cover",
+ if (f.cover == null) JSONObject.NULL else "http://${hostPart()}:$port/cover?t=$token&v=$fileVersion")
+ }
+
+ /**
+ * A file arrives in three steps, so that a large one never crosses the
+ * bridge as one message: `beginFile` names it, `writeFile` appends, and
+ * `endFile` puts it up and answers its address. It is written to the spool
+ * directory rather than held: a long lossless track is a few hundred
+ * megabytes. The relay starts for it when nothing else has.
+ */
+ @Synchronized
+ fun beginFile(type: String, size: Long, cover: ByteArray?, coverType: String?, meta: JSONObject): Boolean {
+ if (type !in FILE_TYPES) throw IllegalArgumentException("Not something a receiver shows: $type")
+ if (size <= 0 || size > FILE_MAX_BYTES) throw IllegalArgumentException("Bad file size: $size")
+ if (cover != null && (coverType == null || !coverType.startsWith("image/") || coverType !in FILE_TYPES)) {
+ throw IllegalArgumentException("Bad cover type: $coverType")
+ }
+ if (server == null) start(null, null)
+ dropIncoming()
+ // At most the file being shown and the one arriving: anything older
+ // has been replaced on the television already.
+ spoolDir.listFiles()?.filter { it.name.startsWith("file-") && it != file?.path }?.forEach { it.delete() }
+ val text = { k: String -> meta.optString(k, "").take(500) }
+ val slot = FileSlot(File(spoolDir, "file-${fileSeq.incrementAndGet()}.bin"), size, type,
+ cover?.let { Cover(it, coverType!!) },
+ JSONObject().put("title", text("title")).put("artist", text("artist")).put("album", text("album")))
+ incoming = Incoming(slot)
+ return true
+ }
+
+ @Synchronized
+ fun writeFile(data: ByteArray, offset: Int = 0, length: Int = data.size - offset): Boolean {
+ val f = incoming ?: throw IllegalStateException("No file is being sent")
+ if (f.received + length > f.slot.size) { dropIncoming(); throw IllegalStateException("More bytes than announced") }
+ f.out.write(data, offset, length)
+ f.received += length
+ return true
+ }
+
+ @Synchronized
+ fun endFile(): JSONObject {
+ val f = incoming ?: throw IllegalStateException("No file is being sent")
+ incoming = null
+ f.out.close()
+ if (f.received != f.slot.size) {
+ f.slot.path.delete()
+ throw IllegalStateException("File cut short: ${f.received} of ${f.slot.size} bytes")
+ }
+ // The previous file stays on disk until the next one begins: a receiver
+ // may still be reading it.
+ file = f.slot
+ fileVersion++
+ log("file set: ${f.slot.size} bytes, ${f.slot.type}, v$fileVersion")
+ return JSONObject().put("url", fileUrl)
+ }
+
+ private fun dropIncoming() {
+ incoming?.let { try { it.out.close() } catch (e: Exception) {}; it.slot.path.delete() }
+ incoming = null
+ }
+
fun subtitleInfo(): JSONObject? = subtitle?.let {
JSONObject().put("url", subtitleUrl).put("language", it.language).put("label", it.label)
}
@@ -145,6 +227,8 @@ class CastRelay(private val lanAddress: () -> InetAddress?, private val spoolDir
spoolDir.mkdirs()
spoolDir.listFiles()?.forEach { it.delete() }
subtitle = null
+ dropIncoming()
+ file = null
setSubtitle(sub)
var bound: ServerSocket? = null
@@ -252,6 +336,9 @@ class CastRelay(private val lanAddress: () -> InetAddress?, private val spoolDir
server?.let { try { it.close() } catch (e: Exception) {} }
server = null
port = 0; token = null; initSegment = null; subtitle = null
+ dropIncoming()
+ file = null
+ spoolDir.listFiles()?.filter { it.name.startsWith("file-") }?.forEach { it.delete() }
synchronized(ring) { ring.clear(); ringBytes = 0 }
accum.reset()
}
@@ -270,7 +357,12 @@ class CastRelay(private val lanAddress: () -> InetAddress?, private val spoolDir
socket.soTimeout = 15000
val reader = BufferedReader(InputStreamReader(socket.getInputStream(), Charsets.ISO_8859_1))
val requestLine = reader.readLine() ?: return socket.close()
- while (true) { val line = reader.readLine() ?: break; if (line.isEmpty()) break }
+ var range: String? = null
+ while (true) {
+ val line = reader.readLine() ?: break
+ if (line.isEmpty()) break
+ if (line.startsWith("range:", ignoreCase = true)) range = line.substringAfter(':').trim()
+ }
socket.soTimeout = 0
val parts = requestLine.split(' ')
val method = parts.getOrElse(0) { "" }
@@ -291,6 +383,8 @@ class CastRelay(private val lanAddress: () -> InetAddress?, private val spoolDir
if (token == null || query["t"] != token) return respond(out, socket, 403, emptyMap())
when (path) {
"/subs.vtt" -> serveSubtitle(out, socket)
+ "/file" -> serveFile(out, socket, range)
+ "/cover" -> serveCover(out, socket)
"/stream.mp4" -> { streamRequests++; serveStream(out, socket) }
else -> respond(out, socket, 404, emptyMap())
}
@@ -308,6 +402,54 @@ class CastRelay(private val lanAddress: () -> InetAddress?, private val spoolDir
), sub.vtt)
}
+ /** The whole file, or the one range asked for: a receiver seeks in a track that way. */
+ private fun serveFile(out: OutputStream, socket: Socket, range: String?) {
+ val f = file ?: return respond(out, socket, 404, CORS_HEADERS)
+ val total = f.size
+ val base = CORS_HEADERS + mapOf("Content-Type" to f.type, "Cache-Control" to "no-store",
+ "Accept-Ranges" to "bytes", "Connection" to "close")
+ var from = 0L
+ var to = total - 1
+ var status = 200
+ val m = range?.let { Regex("^bytes=(\\d*)-(\\d*)$").matchEntire(it) }
+ if (m != null && (m.groupValues[1].isNotEmpty() || m.groupValues[2].isNotEmpty())) {
+ val a = m.groupValues[1]; val b = m.groupValues[2]
+ from = if (a.isEmpty()) total - b.toLong() else a.toLong()
+ to = if (a.isEmpty() || b.isEmpty()) total - 1 else minOf(b.toLong(), total - 1)
+ from = maxOf(0L, from)
+ if (from > to || from >= total) {
+ head(out, 416, base + ("Content-Range" to "bytes */$total") + ("Content-Length" to "0"))
+ out.flush(); socket.close(); return
+ }
+ status = 206
+ }
+ val length = to - from + 1
+ head(out, status, base + ("Content-Length" to length.toString()) +
+ (if (status == 206) mapOf("Content-Range" to "bytes $from-$to/$total") else emptyMap()))
+ RandomAccessFile(f.path, "r").use { raf ->
+ raf.seek(from)
+ val block = ByteArray(BLOCK)
+ var left = length
+ while (left > 0) {
+ val n = raf.read(block, 0, minOf(BLOCK.toLong(), left).toInt())
+ if (n <= 0) break
+ out.write(block, 0, n)
+ left -= n
+ }
+ }
+ out.flush()
+ socket.close()
+ }
+
+ private fun serveCover(out: OutputStream, socket: Socket) {
+ val c = file?.cover ?: return respond(out, socket, 404, CORS_HEADERS)
+ respond(out, socket, 200, CORS_HEADERS + mapOf(
+ "Content-Type" to c.type,
+ "Content-Length" to c.bytes.size.toString(),
+ "Cache-Control" to "no-store",
+ ), c.bytes)
+ }
+
private fun serveStream(out: OutputStream, socket: Socket) {
// Same headers as the subtitle: a receiver given a side-loaded track
// reads the media through the same CORS-checked path.
@@ -393,6 +535,13 @@ class CastRelay(private val lanAddress: () -> InetAddress?, private val spoolDir
}
const val RING_CAP = 64
+ // What a receiver is ever handed as a file: the page re-encodes every
+ // photo to JPEG, and plays a track in the format it is stored in.
+ val FILE_TYPES = setOf("image/jpeg", "image/png", "image/webp",
+ "audio/mpeg", "audio/mp4", "audio/aac", "audio/flac", "audio/ogg", "audio/opus",
+ "audio/wav", "audio/webm")
+ // Only stops a page from filling the phone: a long lossless track is a few hundred MB.
+ const val FILE_MAX_BYTES = 1024L * 1024 * 1024
const val RING_MAX_BYTES = 32L * 1024 * 1024
const val SPOOL_MAX_BYTES = 2L * 1024 * 1024 * 1024
private const val HEADER_WAIT_MS = 10_000L
@@ -413,8 +562,9 @@ class CastRelay(private val lanAddress: () -> InetAddress?, private val spoolDir
"Access-Control-Expose-Headers" to "Content-Length, Content-Range",
)
- private val REASONS = mapOf(200 to "OK", 204 to "No Content", 403 to "Forbidden",
- 404 to "Not Found", 405 to "Method Not Allowed")
+ private val REASONS = mapOf(200 to "OK", 204 to "No Content", 206 to "Partial Content",
+ 403 to "Forbidden", 404 to "Not Found", 405 to "Method Not Allowed",
+ 416 to "Range Not Satisfiable")
private fun randomToken(): String {
val b = ByteArray(16).also { SecureRandom().nextBytes(it) }
diff --git a/packages/meshbay-android/app/src/main/kotlin/org/meshbay/client/keys/Kdf.kt b/packages/meshbay-android/app/src/main/kotlin/org/meshbay/client/keys/Kdf.kt
index 30f094e..404efcb 100644
--- a/packages/meshbay-android/app/src/main/kotlin/org/meshbay/client/keys/Kdf.kt
+++ b/packages/meshbay-android/app/src/main/kotlin/org/meshbay/client/keys/Kdf.kt
@@ -1,3 +1,5 @@
+// SPDX-License-Identifier: LGPL-3.0-or-later
+// Part of MeshBay's protocol layer, under the LGPL so that any client may use it.
package org.meshbay.client.keys
import org.bouncycastle.crypto.digests.SHA256Digest
diff --git a/packages/meshbay-android/app/src/main/kotlin/org/meshbay/client/keys/Keyring.kt b/packages/meshbay-android/app/src/main/kotlin/org/meshbay/client/keys/Keyring.kt
index fa8ff71..9a7dd9f 100644
--- a/packages/meshbay-android/app/src/main/kotlin/org/meshbay/client/keys/Keyring.kt
+++ b/packages/meshbay-android/app/src/main/kotlin/org/meshbay/client/keys/Keyring.kt
@@ -1,3 +1,5 @@
+// SPDX-License-Identifier: LGPL-3.0-or-later
+// Part of MeshBay's protocol layer, under the LGPL so that any client may use it.
package org.meshbay.client.keys
import org.bouncycastle.crypto.agreement.X25519Agreement
diff --git a/packages/meshbay-android/app/src/main/kotlin/org/meshbay/client/keys/Transcripts.kt b/packages/meshbay-android/app/src/main/kotlin/org/meshbay/client/keys/Transcripts.kt
index 4d183f7..cf20a0b 100644
--- a/packages/meshbay-android/app/src/main/kotlin/org/meshbay/client/keys/Transcripts.kt
+++ b/packages/meshbay-android/app/src/main/kotlin/org/meshbay/client/keys/Transcripts.kt
@@ -1,3 +1,5 @@
+// SPDX-License-Identifier: LGPL-3.0-or-later
+// Part of MeshBay's protocol layer, under the LGPL so that any client may use it.
package org.meshbay.client.keys
import org.json.JSONObject
diff --git a/packages/meshbay-android/app/src/main/kotlin/org/meshbay/client/save/SaveNames.kt b/packages/meshbay-android/app/src/main/kotlin/org/meshbay/client/save/SaveNames.kt
index 2414730..40250c7 100644
--- a/packages/meshbay-android/app/src/main/kotlin/org/meshbay/client/save/SaveNames.kt
+++ b/packages/meshbay-android/app/src/main/kotlin/org/meshbay/client/save/SaveNames.kt
@@ -59,6 +59,7 @@ class BinaryFrame(val id: Long, val channel: Int, val handle: Long, val bytes: B
const val HEADER = 16
const val SAVE_WRITE = 1
const val CAST_PUSH = 2
+ const val CAST_FILE = 3
private val MAGIC = byteArrayOf('M'.code.toByte(), 'B'.code.toByte(), 'B'.code.toByte(), '1'.code.toByte())
private fun u32(b: ByteArray, at: Int) =