aboutsummaryrefslogtreecommitdiffstats
path: root/packages/meshbay-android/app/src
diff options
context:
space:
mode:
Diffstat (limited to 'packages/meshbay-android/app/src')
-rw-r--r--packages/meshbay-android/app/src/main/assets/bridge/meshbay-bridge.js19
-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.kt50
-rw-r--r--packages/meshbay-android/app/src/main/kotlin/org/meshbay/client/cast/CastControl.kt38
-rw-r--r--packages/meshbay-android/app/src/main/kotlin/org/meshbay/client/cast/CastRelay.kt169
-rw-r--r--packages/meshbay-android/app/src/main/kotlin/org/meshbay/client/save/SaveNames.kt1
-rw-r--r--packages/meshbay-android/app/src/test/kotlin/org/meshbay/client/CastRelayTest.kt68
7 files changed, 289 insertions, 59 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 21d27df..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,7 +155,19 @@
push: (data) => pushSegment(data),
stop: () => call('cast:stop'),
subtitle: (sub) => call('cast:subtitle', sub),
- image: (img) => call('cast:image', { bytes: base64Of(bytesOf(img.bytes)), type: img.type }),
+ // 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'),
@@ -161,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/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 6be5d72..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, photo, finish, stop. */
- fun ordered(channel: String) = channel in setOf("cast:start", "cast:push", "cast:subtitle", "cast:image", "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,11 +39,20 @@ class CastChannels(
started
}
"cast:subtitle" -> relay.setSubtitle(a.optJSONObject(0))
- // No foreground service for a photo: the receiver has it once loaded.
- "cast:image" -> {
+ // 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()
- relay.showImage(Base64.getDecoder().decode(o.optString("bytes", "")), o.optString("type", ""))
+ 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
}
@@ -56,15 +66,16 @@ class CastChannels(
"cast:chromecast:connect" -> {
val o = a.optJSONObject(0) ?: JSONObject()
val url = relayUrl(o)
- reported { control.connect(o.optString("deviceId", ""), url, subtitleFor(url, o), typeOf(url)) }
+ reported { control.connect(o.optString("deviceId", ""), url, subtitleFor(url, o), fileOf(url), startOf(url, o)) }
}
"cast:chromecast:reload" -> {
val o = a.optJSONObject(0) ?: JSONObject()
val url = relayUrl(o)
- reported { control.reload(url, subtitleFor(url, o), typeOf(url)) }
+ 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")
}
@@ -84,26 +95,37 @@ 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 stream or photo, and refused
+ * 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 && asked != relay.imageUrl) throw Refused("Refused: not this relay's stream")
+ if (asked != ours && asked != relay.fileUrl) throw Refused("Refused: not this relay's stream")
return asked
}
- /** The relay's photo is loaded as a picture, anything else as the stream; the page does not say which. */
- private fun typeOf(url: String): String =
- if (url == relay.imageUrl) relay.imageType ?: "image/jpeg" else "video/mp4"
+ /**
+ * 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 photo has none. */
+ /** 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.imageUrl -> null
+ url == relay.fileUrl -> null
o.has("subtitle") && o.get("subtitle") != JSONObject.NULL -> o.optJSONObject("subtitle")
else -> relay.subtitleInfo()
}
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 56a1f5e..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
@@ -11,6 +12,8 @@ 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
@@ -112,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?, contentType: String = "video/mp4"): 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)
@@ -168,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, contentType)
+ val state = load(session, mediaUrl, subtitle, file, startAt)
watch(session)
return JSONObject().put("deviceName", name ?: JSONObject.NULL).put("playerState", state)
}
@@ -201,23 +204,41 @@ class CastControl(private val context: Context) {
watched = c
}
- fun reload(mediaUrl: String, subtitle: JSONObject?, contentType: String = "video/mp4"): 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, contentType)
+ val state = load(session, mediaUrl, subtitle, file, startAt)
watch(session)
return JSONObject().put("playerState", state)
}
- private fun load(session: CastSession, mediaUrl: String, subtitle: JSONObject?, contentType: String): String {
- val photo = contentType.startsWith("image/")
+ /**
+ * `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 = 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
@@ -243,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
@@ -295,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 41bf4bd..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,7 +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 Image(val bytes: ByteArray, val type: 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
@@ -93,8 +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 image: Image? = null
- @Volatile private var imageVersion = 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
@@ -117,24 +125,74 @@ class CastRelay(private val lanAddress: () -> InetAddress?, private val spoolDir
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 imageUrl get() =
- if (server == null || image == null) null
- else "http://${hostPart()}:$port/image?t=$token&v=$imageVersion"
+ val fileUrl get() =
+ if (server == null || file == null) null
+ else "http://${hostPart()}:$port/file?t=$token&v=$fileVersion"
+
+ val fileType get() = file?.type
- val imageType get() = image?.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")
+ }
/**
- * One photo, scaled and re-encoded by the page, served at `/image` until
- * the next one. The relay starts for it when nothing else has.
+ * 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 showImage(bytes: ByteArray, type: String): JSONObject {
- if (type !in IMAGE_TYPES) throw IllegalArgumentException("Not a picture a receiver shows: $type")
+ 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)
- image = Image(bytes, type)
- imageVersion++
- log("image set: ${bytes.size} bytes, $type, v$imageVersion")
- return JSONObject().put("url", imageUrl)
+ 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 {
@@ -169,7 +227,8 @@ class CastRelay(private val lanAddress: () -> InetAddress?, private val spoolDir
spoolDir.mkdirs()
spoolDir.listFiles()?.forEach { it.delete() }
subtitle = null
- image = null
+ dropIncoming()
+ file = null
setSubtitle(sub)
var bound: ServerSocket? = null
@@ -276,7 +335,10 @@ class CastRelay(private val lanAddress: () -> InetAddress?, private val spoolDir
clients.clear()
server?.let { try { it.close() } catch (e: Exception) {} }
server = null
- port = 0; token = null; initSegment = null; subtitle = null; image = 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()
}
@@ -295,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) { "" }
@@ -316,7 +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)
- "/image" -> serveImage(out, socket)
+ "/file" -> serveFile(out, socket, range)
+ "/cover" -> serveCover(out, socket)
"/stream.mp4" -> { streamRequests++; serveStream(out, socket) }
else -> respond(out, socket, 404, emptyMap())
}
@@ -334,13 +402,52 @@ class CastRelay(private val lanAddress: () -> InetAddress?, private val spoolDir
), sub.vtt)
}
- private fun serveImage(out: OutputStream, socket: Socket) {
- val img = image ?: return respond(out, socket, 404, CORS_HEADERS)
+ /** 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 img.type,
- "Content-Length" to img.bytes.size.toString(),
+ "Content-Type" to c.type,
+ "Content-Length" to c.bytes.size.toString(),
"Cache-Control" to "no-store",
- ), img.bytes)
+ ), c.bytes)
}
private fun serveStream(out: OutputStream, socket: Socket) {
@@ -428,8 +535,13 @@ class CastRelay(private val lanAddress: () -> InetAddress?, private val spoolDir
}
const val RING_CAP = 64
- // The page re-encodes every photo to JPEG: a guard, not a menu.
- val IMAGE_TYPES = setOf("image/jpeg", "image/png", "image/webp")
+ // 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
@@ -450,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/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) =
diff --git a/packages/meshbay-android/app/src/test/kotlin/org/meshbay/client/CastRelayTest.kt b/packages/meshbay-android/app/src/test/kotlin/org/meshbay/client/CastRelayTest.kt
index 8013e29..14f49cf 100644
--- a/packages/meshbay-android/app/src/test/kotlin/org/meshbay/client/CastRelayTest.kt
+++ b/packages/meshbay-android/app/src/test/kotlin/org/meshbay/client/CastRelayTest.kt
@@ -158,9 +158,38 @@ class CastRelayTest {
assertEquals(200, get(r.getString("url"), readBytes = 0).status)
}
- @Test fun `a photo starts the relay and is served behind the token, re-addressed when it changes`() {
+ private fun send(bytes: ByteArray, type: String, meta: JSONObject = JSONObject(),
+ cover: ByteArray? = null): String {
+ relay.beginFile(type, bytes.size.toLong(), cover, cover?.let { "image/jpeg" }, meta)
+ var at = 0
+ while (at < bytes.size) { val n = minOf(3, bytes.size - at); relay.writeFile(bytes, at, n); at += n }
+ return relay.endFile().getString("url")
+ }
+
+ private fun getRange(url: String, range: String): Reply {
+ val u = java.net.URI(url)
+ Socket(u.host, u.port).use { s ->
+ s.soTimeout = 5000
+ s.getOutputStream().write("GET ${u.rawPath}?${u.rawQuery} HTTP/1.1\r\nHost: x\r\nRange: $range\r\n\r\n".toByteArray())
+ val input = DataInputStream(s.getInputStream())
+ val lines = ArrayList<String>()
+ val line = StringBuilder()
+ while (true) {
+ val c = input.read()
+ if (c == -1) break
+ if (c == '\n'.code) { val l = line.toString().trimEnd('\r'); if (l.isEmpty()) break; lines.add(l); line.clear() }
+ else line.append(c.toChar())
+ }
+ val headers = lines.drop(1).associate { it.substringBefore(':').lowercase() to it.substringAfter(':').trim() }
+ val body = ByteArray(headers["content-length"]?.toInt() ?: 0)
+ input.readFully(body)
+ return Reply(lines[0].split(' ')[1].toInt(), headers, body)
+ }
+ }
+
+ @Test fun `a file starts the relay and is served behind the token, re-addressed when it changes`() {
val jpeg = byteArrayOf(0xFF.toByte(), 0xD8.toByte(), 1, 2, 3, 0xFF.toByte(), 0xD9.toByte())
- val first = relay.showImage(jpeg, "image/jpeg").getString("url")
+ val first = send(jpeg, "image/jpeg")
assertTrue(relay.active)
val reply = get(first)
assertEquals(200, reply.status)
@@ -169,21 +198,42 @@ class CastRelayTest {
assertArrayEquals(jpeg, reply.body)
assertEquals(403, get(first.replace(Regex("t=[0-9a-f]+"), "t=x")).status)
// A receiver handed the same address twice shows what it already has.
- val second = relay.showImage(byteArrayOf(9), "image/jpeg").getString("url")
+ val second = send(byteArrayOf(9), "image/jpeg")
assertNotEquals(first, second)
assertArrayEquals(byteArrayOf(9), get(second).body)
}
- @Test fun `only a picture is served as one`() {
- val refused = try { relay.showImage(byteArrayOf(1), "text/html"); false } catch (e: IllegalArgumentException) { true }
+ @Test fun `a track is served with its cover, and by range`() {
+ val track = ByteArray(20) { it.toByte() }
+ val cover = byteArrayOf(0xFF.toByte(), 0xD8.toByte(), 7)
+ val url = send(track, "audio/flac", JSONObject().put("title", "A Song").put("artist", "Some Band"), cover)
+ assertEquals("audio/flac", relay.fileType)
+ val meta = relay.fileMeta()!!
+ assertEquals("A Song", meta.getString("title"))
+ assertArrayEquals(cover, get(meta.getString("cover")).body)
+ val part = getRange(url, "bytes=5-9")
+ assertEquals(206, part.status)
+ assertEquals("bytes 5-9/20", part.headers["content-range"])
+ assertArrayEquals(track.copyOfRange(5, 10), part.body)
+ assertArrayEquals(track.copyOfRange(15, 20), getRange(url, "bytes=15-").body)
+ assertEquals(416, getRange(url, "bytes=50-60").status)
+ }
+
+ @Test fun `only a whole picture or sound is served as one`() {
+ val refused = try { relay.beginFile("text/html", 1, null, null, JSONObject()); false } catch (e: IllegalArgumentException) { true }
assertTrue(refused)
- assertNull(relay.imageUrl)
+ assertNull(relay.fileUrl)
+ relay.beginFile("audio/mpeg", 10, null, null, JSONObject())
+ relay.writeFile(byteArrayOf(1, 2, 3))
+ val short = try { relay.endFile(); false } catch (e: IllegalStateException) { true }
+ assertTrue(short)
+ assertNull(relay.fileUrl)
}
- @Test fun `a film started over a photo takes the photo down`() {
- relay.showImage(byteArrayOf(1), "image/jpeg")
+ @Test fun `a film started over a file takes the file down`() {
+ send(byteArrayOf(1), "image/jpeg")
relay.start(null, null)
- assertNull(relay.imageUrl)
+ assertNull(relay.fileUrl)
}
@Test fun `the preflight is answered before the token is checked`() {