aboutsummaryrefslogtreecommitdiffstats
path: root/packages/meshbay-android/app/src/main/kotlin/org/meshbay/client/cast/CastRelay.kt
blob: 21328dbf134e214d3fbbebee3d9743e74b27c5e5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
package org.meshbay.client.cast

import android.util.Log
import org.json.JSONObject
import java.io.BufferedReader
import java.io.File
import java.io.InputStreamReader
import java.io.RandomAccessFile
import java.nio.ByteBuffer
import java.nio.channels.FileChannel
import java.io.OutputStream
import java.net.InetAddress
import java.net.InetSocketAddress
import java.net.ServerSocket
import java.net.Socket
import java.net.SocketException
import java.security.SecureRandom
import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.atomic.AtomicLong

/**
 * Local HTTP relay for LAN casting — a port of meshbay-client/src/cast-relay.js,
 * whose comments are the specification.
 *
 * The page decrypts the fMP4 stream from the node and pushes it here; a
 * receiver on the same Wi-Fi plays it from the URL. Same mitigations:
 *
 *  - bound to the LAN interface (the caller supplies it), never 0.0.0.0;
 *  - fixed port range 19550–19553, open only while casting;
 *  - an unguessable token in the URL (32 hex chars);
 *  - CORS on both served paths, both behind the token;
 *  - `Cache-Control: no-store` on every response;
 *  - the server closed when playback stops — zero residual surface.
 *
 * What a receiver has not read yet waits in a file, not in memory. The page
 * runs ahead of the television by its whole read-ahead — tens of megabytes in
 * the first seconds — and a fragment is a segment, megabytes at a film's
 * bitrate (5 to 10 MB measured). Held in memory under the desktop's 8 MB
 * bound, fragments were dropped and the picture froze for their length; held
 * in memory without a bound, the heap went. A spool file per receiver holds
 * the lead at no cost to either, and is deleted when the receiver goes.
 *
 * `java.net` and `java.nio` only, so the JVM tests run the real thing.
 */
class CastRelay(private val lanAddress: () -> InetAddress?, private val spoolDir: File) {

    /**
     * One receiver: the fragments it has been given, appended to its spool
     * file, and how far it has read. Positional reads and writes on one
     * channel, so the pusher and the reader never share a file pointer.
     */
    private class Client(val socket: Socket, val out: OutputStream, val file: File) {
        val channel: FileChannel = RandomAccessFile(file, "rw").channel
        val lock = Object()
        var written = 0L      // guarded by lock
        var read = 0L         // guarded by lock
        var ended = false     // guarded by lock
        @Volatile var closed = false
        val sent = AtomicLong()
        val dropped = AtomicLong()
        @Volatile var lastReport = System.currentTimeMillis()

        fun waiting() = synchronized(lock) { written - read }

        fun append(data: ByteArray) {
            var at = synchronized(lock) { written }
            val buf = ByteBuffer.wrap(data)
            while (buf.hasRemaining()) at += channel.write(buf, at)
            synchronized(lock) { written = at; lock.notifyAll() }
        }

        fun end() = synchronized(lock) { ended = true; lock.notifyAll() }

        fun close() {
            closed = true
            synchronized(lock) { lock.notifyAll() }
            try { channel.close() } catch (e: Exception) {}
            file.delete()
        }
    }

    private val spoolSeq = AtomicLong()

    private class Subtitle(val vtt: ByteArray, val language: String, val label: String)

    @Volatile private var server: ServerSocket? = null
    @Volatile private var port = 0
    @Volatile private var token: String? = null
    @Volatile private var host: String? = null
    @Volatile private var initSegment: ByteArray? = null
    private val headerLock = Object()
    @Volatile private var headerReady = false
    @Volatile private var subtitle: Subtitle? = null
    @Volatile private var subtitleVersion = 0
    /** How many times a receiver asked for the stream since the relay started. */
    @Volatile var streamRequests = 0
        private set
    private val ring = ArrayDeque<ByteArray>()
    private var ringBytes = 0L
    private val clients = ConcurrentHashMap.newKeySet<Client>()
    private var accum = BoxAccumulator()

    val active get() = server != null
    /** For the tests: what a receiver joining now would be sent before live fragments. */
    fun backlogBytes() = synchronized(ring) { ringBytes }

    val url get() = if (server == null) null else "http://${hostPart()}:$port/stream.mp4?t=$token"

    private fun hostPart() = host?.let { if (it.contains(':')) "[$it]" else it }

    /** Versioned: a receiver caches a side-loaded track by its address. */
    val subtitleUrl get() =
        if (server == null || subtitle == null) null
        else "http://${hostPart()}:$port/subs.vtt?t=$token&v=$subtitleVersion"

    fun subtitleInfo(): JSONObject? = subtitle?.let {
        JSONObject().put("url", subtitleUrl).put("language", it.language).put("label", it.label)
    }

    /** Cues already on the stream's timeline — the page shifts them; the relay serves bytes. */
    fun setSubtitle(sub: JSONObject?): JSONObject? {
        val vtt = sub?.optString("vtt", "") ?: ""
        subtitle = if (vtt.isEmpty()) null
                   else Subtitle(vtt.toByteArray(Charsets.UTF_8), sub!!.optString("language", ""), sub.optString("label", ""))
        subtitleVersion++
        return subtitleInfo()
    }

    @Synchronized
    fun start(init: ByteArray?, sub: JSONObject?): JSONObject {
        if (server != null) stop()
        val address = lanAddress() ?: throw IllegalStateException("Casting needs this device on Wi-Fi")
        token = randomToken()
        streamRequests = 0
        pushes = 0
        fragments = 0
        host = address.hostAddress
        initSegment = init?.let(::headerOnly)
        // Nothing to wait for without a first chunk: no header is coming.
        headerReady = init == null
        synchronized(ring) { ring.clear(); ringBytes = 0 }
        clients.clear()
        accum = BoxAccumulator().also { a -> a.onPreamble = ::preamble }
        // What a killed process left behind.
        spoolDir.mkdirs()
        spoolDir.listFiles()?.forEach { it.delete() }
        subtitle = null
        setSubtitle(sub)

        var bound: ServerSocket? = null
        for (i in 0 until PORT_COUNT) {
            val s = ServerSocket()
            try {
                // As Node's server does (and so the desktop relay): a port just
                // closed sits in TIME_WAIT, and every seek restarts the relay —
                // without this, four quick seeks used all four ports. It does
                // not let two live listeners share a port.
                s.reuseAddress = true
                s.bind(InetSocketAddress(address, PORT_BASE + i))
                bound = s; port = PORT_BASE + i
                break
            } catch (e: java.net.BindException) {
                s.close()
            }
        }
        server = bound ?: throw IllegalStateException("All cast relay ports are in use")
        Thread({ acceptLoop(bound) }, "cast-relay-accept").apply { isDaemon = true }.start()
        log("started on ${hostPart()}:$port, init ${init?.size ?: 0} bytes, header ${initSegment?.size ?: 0} bytes")
        return JSONObject().put("url", url).put("port", port).put("token", token)
            .put("subtitle", subtitleInfo() ?: JSONObject.NULL)
    }

    /**
     * The header is everything the page pushed before the first moof. The
     * `init` the page hands to start() is only its first chunk, which may be
     * the ftyp without the moov: served as the header, the receiver got no
     * track description and gave up — the "fails the first time" of a fresh
     * start, every time. The pushed stream carries the whole of it; `init` is
     * the fallback when nothing came before the first moof.
     */
    private fun preamble(bytes: ByteArray) {
        val header = headerOnly(bytes)
        synchronized(headerLock) {
            if (header.size >= 8 && BoxAccumulator.u32(header, 4) == FTYP) initSegment = header
            headerReady = true
            headerLock.notifyAll()
        }
        log("header complete: ${initSegment?.size ?: 0} bytes")
    }

    /** A receiver that connects before the first moof waits for the whole header, not a piece of it. */
    private fun awaitHeader(): ByteArray? {
        val deadline = System.currentTimeMillis() + HEADER_WAIT_MS
        synchronized(headerLock) {
            while (!headerReady) {
                val left = deadline - System.currentTimeMillis()
                if (left <= 0) break
                headerLock.wait(left)
            }
        }
        return initSegment
    }

    @Volatile private var pushes = 0
    @Volatile private var fragments = 0

    fun push(data: ByteArray, offset: Int = 0, length: Int = data.size - offset) {
        if (server == null) return
        pushes++
        if (pushes <= 3 || pushes % 100 == 0) log("push #$pushes: $length bytes, $fragments fragments so far")
        for (frag in accum.push(data, offset, length)) {
            fragments++
            // The backlog and the clients under one lock: a receiver joining
            // gets each fragment exactly once, from the backlog or from here.
            synchronized(ring) {
                ring.addLast(frag); ringBytes += frag.size
                // Bounded in fragments, as the desktop is, and in bytes too: a
                // fragment is a whole segment, megabytes at a film's bitrate,
                // and 64 of them outgrew a phone's heap — the app died of it.
                while (ring.size > RING_CAP || (ringBytes > RING_MAX_BYTES && ring.size > 1)) {
                    ringBytes -= ring.removeFirst().size
                }
                for (c in clients) {
                    if (c.waiting() > spoolLimit()) {
                        // Only with the disk bound reached: the picture on the
                        // receiver freezes until the next fragment it gets.
                        val n = c.dropped.incrementAndGet()
                        if (n <= 3 || n % 20 == 0L) {
                            log("DROPPED fragment for ${c.socket.inetAddress?.hostAddress}: ${frag.size} bytes, " +
                                "${c.waiting() / 1048576} MiB already waiting, $n dropped so far")
                        }
                        continue
                    }
                    try { c.append(frag) } catch (e: Exception) { log("spool write failed: ${e.message}") }
                }
            }
        }
    }

    /** End of film: every client's response is ended, the server stays until stop. */
    fun finish() {
        for (c in clients) c.end()
    }

    /** Half the free space, at most 2 GiB: a receiver this far behind is not coming back. */
    private fun spoolLimit(): Long = minOf(SPOOL_MAX_BYTES, spoolDir.usableSpace / 2)

    @Synchronized
    fun stop() {
        for (c in clients) { c.close(); try { c.socket.close() } catch (e: Exception) {} }
        clients.clear()
        server?.let { try { it.close() } catch (e: Exception) {} }
        server = null
        port = 0; token = null; initSegment = null; subtitle = null
        synchronized(ring) { ring.clear(); ringBytes = 0 }
        accum.reset()
    }

    // ── HTTP ────────────────────────────────────────────────────────────────

    private fun acceptLoop(s: ServerSocket) {
        while (!s.isClosed) {
            val socket = try { s.accept() } catch (e: SocketException) { return }
            Thread({ serve(socket) }, "cast-relay-client").apply { isDaemon = true }.start()
        }
    }

    private fun serve(socket: Socket) {
        try {
            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 }
            socket.soTimeout = 0
            val parts = requestLine.split(' ')
            val method = parts.getOrElse(0) { "" }
            val target = parts.getOrElse(1) { "" }
            val out = socket.getOutputStream()

            if (method != "GET" && method != "OPTIONS") return respond(out, socket, 405, emptyMap())
            // The preflight is answered before the token is examined: a
            // receiver sends it without credentials, and refusing it would
            // read on the receiver as a network failure, not a refusal.
            if (method == "OPTIONS") return respond(out, socket, 204, CORS_HEADERS)

            val path = target.substringBefore('?')
            log("$method $path from ${socket.inetAddress?.hostAddress}")
            val query = target.substringAfter('?', "").split('&').associate {
                it.substringBefore('=') to it.substringAfter('=', "")
            }
            if (token == null || query["t"] != token) return respond(out, socket, 403, emptyMap())
            when (path) {
                "/subs.vtt" -> serveSubtitle(out, socket)
                "/stream.mp4" -> { streamRequests++; serveStream(out, socket) }
                else -> respond(out, socket, 404, emptyMap())
            }
        } catch (e: Exception) {
            try { socket.close() } catch (x: Exception) {}
        }
    }

    private fun serveSubtitle(out: OutputStream, socket: Socket) {
        val sub = subtitle ?: return respond(out, socket, 404, CORS_HEADERS)
        respond(out, socket, 200, CORS_HEADERS + mapOf(
            "Content-Type" to "text/vtt; charset=utf-8",
            "Content-Length" to sub.vtt.size.toString(),
            "Cache-Control" to "no-store",
        ), sub.vtt)
    }

    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.
        head(out, 200, CORS_HEADERS + mapOf(
            "Content-Type" to "video/mp4",
            "Cache-Control" to "no-store",
            "Accept-Ranges" to "none",
            "Connection" to "keep-alive",
            "Transfer-Encoding" to "chunked",
        ))
        awaitHeader()?.let { chunk(out, it) }
        out.flush()
        val client = Client(socket, out, File(spoolDir, "client-${spoolSeq.incrementAndGet()}.spool"))
        val backlog = synchronized(ring) {
            for (frag in ring) client.append(frag)
            clients.add(client)
            ring.size
        }
        log("client ${socket.inetAddress?.hostAddress} served init + $backlog fragments")
        val block = ByteBuffer.allocate(BLOCK)
        try {
            while (!client.closed) {
                val at = synchronized(client.lock) {
                    while (client.read == client.written && !client.ended && !client.closed) client.lock.wait()
                    if (client.read == client.written) -1L else client.read
                }
                if (at < 0) {
                    if (!client.closed) { out.write("0\r\n\r\n".toByteArray()); out.flush() }
                    break
                }
                block.clear()
                val n = client.channel.read(block, at)
                if (n <= 0) continue
                val t0 = System.currentTimeMillis()
                chunk(out, block.array(), n)
                out.flush()
                val took = System.currentTimeMillis() - t0
                synchronized(client.lock) { client.read += n }
                client.sent.addAndGet(n.toLong())
                // A write that blocks is the receiver not reading (or the
                // Wi-Fi not carrying): the one place a stall downstream shows.
                if (took > 5000) log("slow write to ${socket.inetAddress?.hostAddress}: $n bytes took $took ms")
                val now = System.currentTimeMillis()
                if (now - client.lastReport >= 10_000) {
                    client.lastReport = now
                    log("client ${socket.inetAddress?.hostAddress}: sent ${client.sent.get() / 1048576} MiB, " +
                        "${client.waiting() / 1048576} MiB waiting in the spool, ${client.dropped.get()} dropped")
                }
            }
        } catch (e: Exception) {
            // The receiver went away; nothing to tell anyone.
        } finally {
            log("client ${socket.inetAddress?.hostAddress} gone")
            synchronized(ring) { clients.remove(client) }
            client.close()
            try { socket.close() } catch (e: Exception) {}
        }
    }

    companion object {
        /**
         * The init segment is what comes before the first moof, and only that.
         *
         * The page hands over the first chunk it decrypted, which is a slice of
         * the stream and not a box: on a real film it was 65536 bytes — ftyp,
         * moov, then the first moof and the start of its mdat. Served whole,
         * the receiver read that partial fragment, then the same fragment again
         * from the accumulator (the page pushes that chunk too), and the box
         * structure was broken from the first fragment: the receiver gave up
         * within three seconds, on the television's idle screen. Whether the
         * first chunk carries film depends on when the node read ffmpeg's
         * output, so the same film can work once and not the next time.
         */
        fun headerOnly(init: ByteArray): ByteArray {
            var at = 0
            while (at + 8 <= init.size) {
                if (BoxAccumulator.u32(init, at + 4) == BoxAccumulator.MOOF) return init.copyOfRange(0, at)
                val size = BoxAccumulator.u32(init, at)
                if (size < 8) break
                at += size.toInt()
            }
            return init
        }

        const val RING_CAP = 64
        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
        private const val FTYP = 0x66747970L
        private const val BLOCK = 256 * 1024
        const val PORT_BASE = 19550
        const val PORT_COUNT = 4

        // Never the URL: it carries the token.
        private fun log(m: String) = try { Log.i("MeshBayCast", "[relay] $m") } catch (e: RuntimeException) { /* JVM tests */ }

        // A receiver reads a side-loaded subtitle with XHR from its own
        // origin, so the headers it sends are allowed by name — Range included.
        val CORS_HEADERS = mapOf(
            "Access-Control-Allow-Origin" to "*",
            "Access-Control-Allow-Methods" to "GET, OPTIONS",
            "Access-Control-Allow-Headers" to "Content-Type, Accept-Encoding, Range",
            "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 fun randomToken(): String {
            val b = ByteArray(16).also { SecureRandom().nextBytes(it) }
            return b.joinToString("") { "%02x".format(it) }
        }

        private fun head(out: OutputStream, status: Int, headers: Map<String, String>) {
            val sb = StringBuilder("HTTP/1.1 $status ${REASONS[status] ?: ""}\r\n")
            for ((k, v) in headers) sb.append(k).append(": ").append(v).append("\r\n")
            sb.append("\r\n")
            out.write(sb.toString().toByteArray(Charsets.ISO_8859_1))
        }

        private fun respond(out: OutputStream, socket: Socket, status: Int, headers: Map<String, String>,
                            body: ByteArray = ByteArray(0)) {
            val h = if (headers.containsKey("Content-Length")) headers else headers + ("Content-Length" to body.size.toString())
            head(out, status, h + ("Connection" to "close"))
            out.write(body)
            out.flush()
            socket.close()
        }

        private fun chunk(out: OutputStream, data: ByteArray, length: Int = data.size) {
            out.write("${Integer.toHexString(length)}\r\n".toByteArray())
            out.write(data, 0, length)
            out.write("\r\n".toByteArray())
        }
    }
}