Your Android phone and your Windows PC, working as one. Local-first, end-to-end encrypted, no account.
Play on phone: video files play on the phone itself
Beea800cf8eb8e5
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex d253c22..0e6978d 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -16,7 +16,10 @@ change things that need a step from you, and its notes say so.as it plays, picture and sound, so your browser's ad blocker stillworks on YouTube: the phone shows just the video, even with otherwindows over it on the PC, in Chrome, Edge, Firefox, Zen and other- browsers. Tap the notification on the phone to watch.+ browsers. A video file playing in VLC, Media Player or another desktop+ player plays on the phone itself, read from the PC at its full quality,+ from where the PC was; the PC pauses. Tap the notification on the phone+ to watch.The PC mutes its speakers while you watch and turns them back on after(**Settings › Camera & Audio** to keep them on).Scripts can do the same through the local APIdiff --git a/android/app/src/main/kotlin/app/nectarlink/android/core/Core.kt b/android/app/src/main/kotlin/app/nectarlink/android/core/Core.ktindex bc15326..c689b62 100644--- a/android/app/src/main/kotlin/app/nectarlink/android/core/Core.kt+++ b/android/app/src/main/kotlin/app/nectarlink/android/core/Core.kt@@ -100,13 +100,14 @@ class Core(context: Context, private val scope: CoroutineScope) : EventListeneronMic = { pc ->app.nectarlink.android.mic.MicRequests.show(this.context, pc, _state.value.nameOf(pc).orEmpty())},- onWatch = { pc, player, title ->+ onWatch = { pc, player, title, file ->app.nectarlink.android.media.WatchOffers.show(this.context,pc,_state.value.nameOf(pc).orEmpty(),player,title,+ file,)},onTask = { pc, task ->@@ -2121,6 +2122,15 @@ class Core(context: Context, private val scope: CoroutineScope) : EventListenerrefreshNotificationAccess()}+ /**+ * Play on phone: up to [length] bytes from [offset] of the video file a PC offered.+ * Blocks; call off the main thread.+ */+ fun readWatchFile(pcId: String, offset: Long, length: Long): ByteArray {+ val running = node ?: throw java.io.IOException("NectarLink isn't running")+ return running.watchFileRead(pcId, offset.toULong(), length.toULong()).data+ }+/** Lists files and folders in `path` on a paired PC (`storage.list`). */fun listPcFiles(deviceId: String,diff --git a/android/app/src/main/kotlin/app/nectarlink/android/core/PhonePlatform.kt b/android/app/src/main/kotlin/app/nectarlink/android/core/PhonePlatform.ktindex 1b222e4..6539905 100644--- a/android/app/src/main/kotlin/app/nectarlink/android/core/PhonePlatform.kt+++ b/android/app/src/main/kotlin/app/nectarlink/android/core/PhonePlatform.kt@@ -62,7 +62,7 @@ internal class PhonePlatform(/** Shows a link a PC sent: (PC's ID, link) → shown. */private val onLink: (String, String) -> Boolean,/** Shows a PC's Play on phone offer: (PC's ID, player, title) → shown. */- private val onWatch: (String, String, String?) -> Boolean,+ private val onWatch: (String, String, String?, app.nectarlink.core.WatchFileOffer?) -> Boolean,/** Asks the user to start the microphone for a PC: PC's ID → asked. */private val onMic: (String) -> Boolean,/** Shows or updates a watched task from a PC: (PC's ID, task) → shown. */@@ -107,8 +107,12 @@ internal class PhonePlatform(override fun openLink(fromId: String, url: String): Boolean = onLink(fromId, url)- override fun watchOffered(fromId: String, player: String, title: String?): Boolean =- onWatch(fromId, player, title)+ override fun watchOffered(+ fromId: String,+ player: String,+ title: String?,+ file: app.nectarlink.core.WatchFileOffer?,+ ): Boolean = onWatch(fromId, player, title, file)override fun taskNotify(fromId: String, task: TaskNotify): Boolean = onTask(fromId, task)diff --git a/android/app/src/main/kotlin/app/nectarlink/android/media/PcFileSource.kt b/android/app/src/main/kotlin/app/nectarlink/android/media/PcFileSource.ktnew file mode 100644index 0000000..abef21c--- /dev/null+++ b/android/app/src/main/kotlin/app/nectarlink/android/media/PcFileSource.kt@@ -0,0 +1,105 @@+// SPDX-License-Identifier: GPL-3.0-or-later+package app.nectarlink.android.media++import android.media.MediaDataSource+import java.io.IOException+import java.util.concurrent.ExecutionException+import java.util.concurrent.Executors+import java.util.concurrent.Future+import java.util.concurrent.RejectedExecutionException+import java.util.concurrent.TimeUnit+import java.util.concurrent.TimeoutException++/**+ * Play on phone for a video file: the file a PC offered, read from the PC as the phone's+ * player asks for it (docs/protocol/media.md §4). It reads in pieces of [ChunkedReader.CHUNK] bytes and+ * a few ahead of where the player reads, and keeps the latest of them, so playing+ * on doesn't wait on the network and seeking back is quick.+ *+ * [read] reads `length` bytes from `offset` of the file (fewer at its end); it blocks, and+ * runs on this source's own threads.+ */+class PcFileSource(fileSize: Long, read: (offset: Long, length: Long) -> ByteArray) : MediaDataSource() {+ private val reader = ChunkedReader(fileSize, read)++ override fun readAt(position: Long, buffer: ByteArray, offset: Int, size: Int): Int =+ reader.readAt(position, buffer, offset, size)++ override fun getSize(): Long = reader.fileSize++ override fun close() = reader.close()+}++/** [PcFileSource]'s reading, apart from Android's player so it can be tested on its own. */+class ChunkedReader(+ val fileSize: Long,+ private val read: (offset: Long, length: Long) -> ByteArray,+) {+ private val pool = Executors.newFixedThreadPool(2) { Thread(it, "pc-file").apply { isDaemon = true } }++ /** Pieces read or on their way, by index; the least recently used go first. */+ private val chunks = object : LinkedHashMap<Long, Future<ByteArray>>(KEEP, 0.75f, true) {+ override fun removeEldestEntry(eldest: MutableMap.MutableEntry<Long, Future<ByteArray>>?): Boolean =+ size > KEEP+ }++ @Volatile private var closed = false++ private fun chunk(index: Long): Future<ByteArray> = synchronized(chunks) {+ chunks.getOrPut(index) { pool.submit<ByteArray> { read(index * CHUNK, CHUNK) } }+ }++ /** As [MediaDataSource.readAt]: how many bytes it put in [buffer], or -1 at the end. */+ fun readAt(position: Long, buffer: ByteArray, offset: Int, size: Int): Int {+ if (closed || position >= fileSize) return -1+ if (size <= 0) return 0+ val index = position / CHUNK+ val data = try {+ val wanted = chunk(index)+ for (ahead in 1..AHEAD) {+ if ((index + ahead) * CHUNK < fileSize) chunk(index + ahead)+ }+ try {+ wanted.get(READ_TIMEOUT_S, TimeUnit.SECONDS)+ } catch (e: ExecutionException) {+ // Read ahead may have failed a while ago (the link dropped): once more, now.+ forget(index)+ chunk(index).get(READ_TIMEOUT_S, TimeUnit.SECONDS)+ }+ } catch (e: RejectedExecutionException) {+ return -1+ } catch (e: ExecutionException) {+ forget(index)+ throw IOException("can't read the PC's file", e.cause)+ } catch (e: TimeoutException) {+ forget(index)+ throw IOException("the PC's file didn't come in time", e)+ } catch (e: InterruptedException) {+ Thread.currentThread().interrupt()+ throw IOException("interrupted", e)+ }+ val within = (position - index * CHUNK).toInt()+ val count = minOf(size, data.size - within)+ if (count <= 0) return -1+ System.arraycopy(data, within, buffer, offset, count)+ return count+ }++ /** A failed piece is read again next time. */+ private fun forget(index: Long) {+ synchronized(chunks) { chunks.remove(index) }+ }++ fun close() {+ closed = true+ pool.shutdownNow()+ synchronized(chunks) { chunks.clear() }+ }++ companion object {+ const val CHUNK = 1L shl 20+ private const val AHEAD = 3+ private const val KEEP = 24+ private const val READ_TIMEOUT_S = 30L+ }+}diff --git a/android/app/src/main/kotlin/app/nectarlink/android/media/WatchOffers.kt b/android/app/src/main/kotlin/app/nectarlink/android/media/WatchOffers.ktindex 351589e..b5951ae 100644--- a/android/app/src/main/kotlin/app/nectarlink/android/media/WatchOffers.kt+++ b/android/app/src/main/kotlin/app/nectarlink/android/media/WatchOffers.kt@@ -12,9 +12,11 @@ import androidx.core.content.ContextCompatimport app.nectarlink.android.Rimport app.nectarlink.android.links.LinkNotificationsimport app.nectarlink.android.ui.watch.WatchActivity+import app.nectarlink.core.WatchFileOffer/**- * Play on phone: a PC offers to stream what one of its players plays. Android doesn't let an+ * Play on phone: a PC offers to stream what one of its players plays, or a video file for the+ * phone to play itself. Android doesn't let an* app in the background open a screen, so the offer waits in a notification (on the links* channel, as links from the PC do); with NectarLink in front, the Watch screen opens at once.*/@@ -22,9 +24,16 @@ object WatchOffers {private const val TAG = "watch"/** Shows the offer; false if notifications are off and it couldn't open either. */- fun show(context: Context, pcId: String, pcName: String, player: String, title: String?): Boolean {+ fun show(+ context: Context,+ pcId: String,+ pcName: String,+ player: String,+ title: String?,+ file: WatchFileOffer? = null,+ ): Boolean {val from = pcName.ifEmpty { context.getString(R.string.your_pc) }- val watch = WatchActivity.intent(context, pcId, from, player, title)+ val watch = WatchActivity.intent(context, pcId, from, player, title, file).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)// Allowed only while NectarLink is in front; otherwise Android ignores it.val opened = runCatching { context.startActivity(watch) }.isSuccess@@ -39,7 +48,7 @@ object WatchOffers {watch,PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT,)- val text = title?.takeIf { it.isNotBlank() } ?: context.getString(R.string.watch_offer_untitled)+ val text = title?.takeIf { it.isNotBlank() } ?: file?.name ?: context.getString(R.string.watch_offer_untitled)val notification = NotificationCompat.Builder(context, LinkNotifications.CHANNEL).setSmallIcon(R.drawable.ic_notification).setContentTitle(context.getString(R.string.watch_offer_title, from))diff --git a/android/app/src/main/kotlin/app/nectarlink/android/ui/watch/FileWatchView.kt b/android/app/src/main/kotlin/app/nectarlink/android/ui/watch/FileWatchView.ktnew file mode 100644index 0000000..9c6908d--- /dev/null+++ b/android/app/src/main/kotlin/app/nectarlink/android/ui/watch/FileWatchView.kt@@ -0,0 +1,165 @@+// SPDX-License-Identifier: GPL-3.0-or-later+package app.nectarlink.android.ui.watch++import android.media.AudioAttributes+import android.media.MediaPlayer+import android.util.Log+import android.view.Surface+import android.view.SurfaceHolder+import android.view.SurfaceView+import androidx.compose.foundation.gestures.detectTapGestures+import androidx.compose.foundation.layout.Box+import androidx.compose.foundation.layout.aspectRatio+import androidx.compose.foundation.layout.fillMaxSize+import androidx.compose.foundation.layout.padding+import androidx.compose.material3.MaterialTheme+import androidx.compose.material3.Text+import androidx.compose.runtime.Composable+import androidx.compose.runtime.DisposableEffect+import androidx.compose.runtime.Stable+import androidx.compose.runtime.getValue+import androidx.compose.runtime.mutableStateOf+import androidx.compose.runtime.remember+import androidx.compose.runtime.rememberUpdatedState+import androidx.compose.runtime.setValue+import androidx.compose.ui.Alignment+import androidx.compose.ui.Modifier+import androidx.compose.ui.graphics.Color+import androidx.compose.ui.input.pointer.pointerInput+import androidx.compose.ui.res.stringResource+import androidx.compose.ui.text.style.TextAlign+import androidx.compose.ui.unit.IntSize+import androidx.compose.ui.unit.dp+import androidx.compose.ui.viewinterop.AndroidView+import app.nectarlink.android.R+import app.nectarlink.android.media.PcFileSource+import app.nectarlink.core.WatchFileOffer++/** A video file playing on the phone (Play on phone): whether it plays, and play or pause. */+@Stable+class LocalPlayback {+ var playing by mutableStateOf(false)+ internal set++ internal var player: MediaPlayer? = null++ /** Where to go on from when the picture comes back (after the app was in the background). */+ internal var resumeAtMs: Long? = null++ /** Whether the PC's player was paused for this already. */+ internal var tookOver = false++ fun toggle() {+ val player = player ?: return+ runCatching { if (player.isPlaying) player.pause() else player.start() }+ playing = runCatching { player.isPlaying }.getOrDefault(false)+ }+}++/**+ * Plays [file], read from the PC with [read], full screen and letterboxed, from where the PC+ * had got to. [onStarted] runs once, when it first plays.+ */+@Composable+fun FileWatchView(+ file: WatchFileOffer,+ read: (offset: Long, length: Long) -> ByteArray,+ playback: LocalPlayback,+ onStarted: () -> Unit,+ onTap: () -> Unit,+ modifier: Modifier = Modifier,+) {+ var surface by remember { mutableStateOf<Surface?>(null) }+ var videoSize by remember { mutableStateOf<IntSize?>(null) }+ var failed by remember { mutableStateOf(false) }+ val started by rememberUpdatedState(onStarted)+ val reader by rememberUpdatedState(read)++ // A player for as long as there is a picture to draw on.+ DisposableEffect(surface) {+ val target = surface ?: return@DisposableEffect onDispose {}+ val source = PcFileSource(file.size.toLong()) { offset, length -> reader(offset, length) }+ val player = MediaPlayer()+ player.setAudioAttributes(+ AudioAttributes.Builder()+ .setUsage(AudioAttributes.USAGE_MEDIA)+ .setContentType(AudioAttributes.CONTENT_TYPE_MOVIE)+ .build(),+ )+ player.setSurface(target)+ player.setOnVideoSizeChangedListener { _, width, height ->+ if (width > 0 && height > 0) videoSize = IntSize(width, height)+ }+ player.setOnErrorListener { _, what, extra ->+ Log.w(TAG, "can't play the PC's file: $what/$extra")+ failed = true+ playback.playing = false+ true+ }+ player.setOnCompletionListener { playback.playing = false }+ player.setOnPreparedListener { prepared ->+ val at = playback.resumeAtMs ?: file.positionMs.toLong()+ if (at > 0) prepared.seekTo(at, MediaPlayer.SEEK_CLOSEST)+ prepared.start()+ playback.playing = true+ if (!playback.tookOver) {+ playback.tookOver = true+ started()+ }+ }+ runCatching {+ player.setDataSource(source)+ player.prepareAsync()+ }.onFailure {+ Log.w(TAG, "can't open the PC's file", it)+ failed = true+ }+ playback.player = player+ onDispose {+ playback.resumeAtMs = runCatching { player.currentPosition.toLong() }.getOrNull()?.takeIf { it > 0 }+ ?: playback.resumeAtMs+ playback.player = null+ playback.playing = false+ runCatching { player.stop() }+ player.release()+ source.close()+ }+ }++ Box(modifier, contentAlignment = Alignment.Center) {+ val ratio = videoSize?.let { it.width.toFloat() / it.height }+ AndroidView(+ factory = { context ->+ SurfaceView(context).apply {+ holder.addCallback(+ object : SurfaceHolder.Callback {+ override fun surfaceCreated(holder: SurfaceHolder) {+ surface = holder.surface+ }++ override fun surfaceChanged(holder: SurfaceHolder, format: Int, width: Int, height: Int) {}++ override fun surfaceDestroyed(holder: SurfaceHolder) {+ surface = null+ }+ },+ )+ }+ },+ modifier = if (ratio != null) Modifier.aspectRatio(ratio) else Modifier.fillMaxSize(),+ )+ // Taps over the picture show and hide the controls.+ Box(Modifier.fillMaxSize().pointerInput(Unit) { detectTapGestures { onTap() } })+ if (failed) {+ Text(+ text = stringResource(R.string.watch_file_failed),+ style = MaterialTheme.typography.bodyLarge,+ color = Color.White,+ textAlign = TextAlign.Center,+ modifier = Modifier.padding(24.dp),+ )+ }+ }+}++private const val TAG = "watch-file"diff --git a/android/app/src/main/kotlin/app/nectarlink/android/ui/watch/WatchActivity.kt b/android/app/src/main/kotlin/app/nectarlink/android/ui/watch/WatchActivity.ktindex a5d9458..0b1ffc0 100644--- a/android/app/src/main/kotlin/app/nectarlink/android/ui/watch/WatchActivity.kt+++ b/android/app/src/main/kotlin/app/nectarlink/android/ui/watch/WatchActivity.kt@@ -57,6 +57,7 @@ import app.nectarlink.android.ui.remote.PcMirrorViewimport app.nectarlink.android.ui.theme.LocalReducedMotionimport app.nectarlink.android.ui.theme.NectarlinkThemeimport app.nectarlink.core.MediaAction+import app.nectarlink.core.WatchFileOfferimport kotlinx.coroutines.delay/**@@ -76,13 +77,20 @@ class WatchActivity : ComponentActivity() {}val pcName = intent.getStringExtra(EXTRA_PC_NAME).orEmpty()val title = intent.getStringExtra(EXTRA_TITLE)+ val file = intent.getStringExtra(EXTRA_FILE_NAME)?.let { name ->+ WatchFileOffer(+ name = name,+ size = intent.getLongExtra(EXTRA_FILE_SIZE, 0).toULong(),+ positionMs = intent.getLongExtra(EXTRA_FILE_POSITION, 0).toULong(),+ )+ }WatchOffers.dismiss(this, pcId)val core = (application as NectarlinkApplication).coreval preferences = Preferences(this)setContent {val appearance by preferences.appearance.collectAsStateWithLifecycle()NectarlinkTheme(appearance) {- WatchScreen(core, pcId, pcName, player, title, onClose = ::finish)+ WatchScreen(core, pcId, pcName, player, title, file, onClose = ::finish)}}}@@ -92,13 +100,30 @@ class WatchActivity : ComponentActivity() {private const val EXTRA_PC_NAME = "watch_pc_name"private const val EXTRA_PLAYER = "watch_player"private const val EXTRA_TITLE = "watch_title"+ private const val EXTRA_FILE_NAME = "watch_file_name"+ private const val EXTRA_FILE_SIZE = "watch_file_size"+ private const val EXTRA_FILE_POSITION = "watch_file_position"- fun intent(context: Context, pcId: String, pcName: String, player: String, title: String?): Intent =+ fun intent(+ context: Context,+ pcId: String,+ pcName: String,+ player: String,+ title: String?,+ file: WatchFileOffer? = null,+ ): Intent =Intent(context, WatchActivity::class.java).putExtra(EXTRA_PC, pcId).putExtra(EXTRA_PC_NAME, pcName).putExtra(EXTRA_PLAYER, player).putExtra(EXTRA_TITLE, title)+ .apply {+ if (file != null) {+ putExtra(EXTRA_FILE_NAME, file.name)+ putExtra(EXTRA_FILE_SIZE, file.size.toLong())+ putExtra(EXTRA_FILE_POSITION, file.positionMs.toLong())+ }+ }}}@@ -109,11 +134,14 @@ private fun WatchScreen(pcName: String,player: String,title: String?,+ file: WatchFileOffer?,onClose: () -> Unit,) {val reducedMotion = LocalReducedMotion.currentval playing by core.pcMedia.playing.collectAsStateWithLifecycle()- val isPlaying = playing["$pcId/$player"] ?: true+ // A file plays here; a stream plays on the PC.+ val local = remember(file) { file?.let { LocalPlayback() } }+ val isPlaying = if (local != null) local.playing else playing["$pcId/$player"] ?: truevar controls by remember { mutableStateOf(true) }var shownAt by remember { mutableIntStateOf(0) }@@ -126,19 +154,32 @@ private fun WatchScreen(}Box(Modifier.fillMaxSize().background(Color.Black)) {- PcMirrorView(- pcId = pcId,- pcName = pcName,- core = core,- modifier = Modifier.fillMaxSize(),- isFullScreen = true,- onFullScreenChange = { full -> if (!full) onClose() },- watch = player,- onTap = {- controls = !controls- shownAt++- },- )+ val onTap: () -> Unit = {+ controls = !controls+ shownAt+++ }+ if (file != null && local != null) {+ FileWatchView(+ file = file,+ read = { offset, length -> core.readWatchFile(pcId, offset, length) },+ playback = local,+ // The phone took over: the PC stops, as when a service opens in its app.+ onStarted = { core.pcMedia.command(pcId, player, MediaAction.PAUSE) },+ onTap = onTap,+ modifier = Modifier.fillMaxSize(),+ )+ } else {+ PcMirrorView(+ pcId = pcId,+ pcName = pcName,+ core = core,+ modifier = Modifier.fillMaxSize(),+ isFullScreen = true,+ onFullScreenChange = { full -> if (!full) onClose() },+ watch = player,+ onTap = onTap,+ )+ }AnimatedVisibility(visible = controls,enter = if (reducedMotion) EnterTransition.None else fadeIn(tween(180)),@@ -157,7 +198,8 @@ private fun WatchScreen() {Column(Modifier.weight(1f)) {Text(- text = title?.takeIf { it.isNotBlank() } ?: stringResource(R.string.watch_offer_untitled),+ text = title?.takeIf { it.isNotBlank() } ?: file?.name+ ?: stringResource(R.string.watch_offer_untitled),style = MaterialTheme.typography.titleMedium,color = Color.White,maxLines = 1,@@ -186,7 +228,11 @@ private fun WatchScreen(val label = stringResource(if (isPlaying) R.string.media_pause else R.string.media_play)IconButton(onClick = {- core.pcMedia.command(pcId, player, if (isPlaying) MediaAction.PAUSE else MediaAction.PLAY)+ if (local != null) {+ local.toggle()+ } else {+ core.pcMedia.command(pcId, player, if (isPlaying) MediaAction.PAUSE else MediaAction.PLAY)+ }shownAt++},colors = IconButtonDefaults.iconButtonColors(diff --git a/android/app/src/main/res/values-zh-rCN/strings.xml b/android/app/src/main/res/values-zh-rCN/strings.xmlindex ccc5113..7153d0f 100644--- a/android/app/src/main/res/values-zh-rCN/strings.xml+++ b/android/app/src/main/res/values-zh-rCN/strings.xml@@ -271,6 +271,7 @@<string name="channel_links">来自电脑的链接</string><string name="watch_offer_title">观看 %1$s 上的播放</string><string name="watch_offer_untitled">电脑上正在播放的内容</string>+ <string name="watch_file_failed">无法播放此视频。</string><string name="watch_offer_action">观看</string><string name="watch_from">来自 %1$s</string><string name="watch_close">关闭</string>diff --git a/android/app/src/main/res/values/strings.xml b/android/app/src/main/res/values/strings.xmlindex fc1f6a0..041b058 100644--- a/android/app/src/main/res/values/strings.xml+++ b/android/app/src/main/res/values/strings.xml@@ -280,6 +280,7 @@<!-- Play on phone: what a PC plays, streamed to the phone. --><string name="watch_offer_title">Watch from %1$s</string><string name="watch_offer_untitled">What\'s playing on your PC</string>+ <string name="watch_file_failed">Couldn’t play this video.</string><string name="watch_offer_action">Watch</string><string name="watch_from">From %1$s</string><string name="watch_close">Close</string>diff --git a/android/app/src/test/kotlin/app/nectarlink/android/media/ChunkedReaderTest.kt b/android/app/src/test/kotlin/app/nectarlink/android/media/ChunkedReaderTest.ktnew file mode 100644index 0000000..cf88de2--- /dev/null+++ b/android/app/src/test/kotlin/app/nectarlink/android/media/ChunkedReaderTest.kt@@ -0,0 +1,87 @@+// SPDX-License-Identifier: GPL-3.0-or-later+package app.nectarlink.android.media++import org.junit.Assert.assertArrayEquals+import org.junit.Assert.assertEquals+import org.junit.Assert.assertTrue+import org.junit.Assert.fail+import org.junit.Test+import java.io.IOException+import java.util.Collections++class ChunkedReaderTest {+ private val chunk = ChunkedReader.CHUNK.toInt()+ private val file = ByteArray(chunk * 5 + 1234) { (it % 251).toByte() }++ /** Reads from [file], recording each read's offset. */+ private class Pc(private val file: ByteArray) {+ val reads: MutableList<Long> = Collections.synchronizedList(mutableListOf())+ @Volatile var failing = false++ fun read(offset: Long, length: Long): ByteArray {+ reads += offset+ if (failing) throw IllegalStateException("offline")+ val from = offset.toInt().coerceAtMost(file.size)+ return file.copyOfRange(from, (from + length.toInt()).coerceAtMost(file.size))+ }+ }++ private fun readAll(reader: ChunkedReader, at: Long, size: Int): ByteArray {+ val out = ByteArray(size)+ var done = 0+ while (done < size) {+ val n = reader.readAt(at + done, out, done, size - done)+ if (n < 0) break+ done += n+ }+ return out.copyOf(done)+ }++ @Test+ fun readsComeBackAsTheFileIsAcrossPieces() {+ val pc = Pc(file)+ val reader = ChunkedReader(file.size.toLong(), pc::read)+ // Across a piece's end, and the file's.+ val at = chunk - 100L+ assertArrayEquals(file.copyOfRange(at.toInt(), at.toInt() + 300), readAll(reader, at, 300))+ val tail = file.size - 50L+ assertArrayEquals(file.copyOfRange(tail.toInt(), file.size), readAll(reader, tail, 500))+ assertEquals(-1, reader.readAt(file.size.toLong(), ByteArray(10), 0, 10))+ reader.close()+ }++ @Test+ fun piecesAheadAreReadAndAPieceIsReadOnce() {+ val pc = Pc(file)+ val reader = ChunkedReader(file.size.toLong(), pc::read)+ readAll(reader, 0, 10)+ readAll(reader, 20, 10)+ // Read ahead lands soon after.+ val deadline = System.currentTimeMillis() + 5_000+ while (pc.reads.size < 4 && System.currentTimeMillis() < deadline) Thread.sleep(10)+ assertEquals(listOf(0L, chunk.toLong(), 2L * chunk, 3L * chunk), pc.reads.sorted())+ reader.close()+ }++ @Test+ fun aFailedPieceIsReadAgainNextTime() {+ val pc = Pc(file)+ val reader = ChunkedReader(file.size.toLong(), pc::read)+ pc.failing = true+ try {+ reader.readAt(0, ByteArray(10), 0, 10)+ fail("a failed read says so")+ } catch (_: IOException) {+ }+ pc.failing = false+ assertArrayEquals(file.copyOfRange(0, 10), readAll(reader, 0, 10))+ reader.close()+ }++ @Test+ fun aClosedReaderReadsNothing() {+ val reader = ChunkedReader(file.size.toLong(), Pc(file)::read)+ reader.close()+ assertTrue(reader.readAt(0, ByteArray(10), 0, 10) < 0)+ }+}diff --git a/core/nectarlink-cli/src/main.rs b/core/nectarlink-cli/src/main.rsindex 74c15b0..e07f350 100644--- a/core/nectarlink-cli/src/main.rs+++ b/core/nectarlink-cli/src/main.rs@@ -984,10 +984,11 @@ fn stream_sound(sound: &nectarlink_core::MirrorStream, rate: u32, channels: u8,static MIC_ASKS: std::sync::OnceLock<tokio::sync::mpsc::UnboundedSender<DeviceId>> =std::sync::OnceLock::new();-/// Play on phone offers from PCs: (PC, player, title).-static WATCH_OFFERS: std::sync::OnceLock<- tokio::sync::mpsc::UnboundedSender<(DeviceId, String, Option<String>)>,-> = std::sync::OnceLock::new();+/// A Play on phone offer from a PC: (PC, player, title, a file to play itself).+type WatchOffer = (DeviceId, String, Option<String>, Option<nectarlink_core::WatchFile>);+/// Play on phone offers from PCs.+static WATCH_OFFERS: std::sync::OnceLock<tokio::sync::mpsc::UnboundedSender<WatchOffer>> =+ std::sync::OnceLock::new();static SCREEN_ASKS: std::sync::OnceLock<tokio::sync::mpsc::UnboundedSender<(DeviceId, bool, u32)>> =std::sync::OnceLock::new();@@ -1790,10 +1791,23 @@ impl Platform for TerminalPlatform {from: &DeviceId,player: &str,title: Option<&str>,+ file: Option<&nectarlink_core::WatchFile>,) -> std::result::Result<(), String> {let offers = WATCH_OFFERS.get().ok_or("not watching (run `watch`)")?;- println!("{} offers to stream {player}: {}", from.short(), title.unwrap_or("(untitled)"));- offers.send((*from, player.to_owned(), title.map(str::to_owned))).map_err(|e| e.to_string())+ match file {+ Some(f) => println!(+ "{} offers {} ({} bytes, at {} ms) from {player}: {}",+ from.short(),+ f.name,+ f.size,+ f.position,+ title.unwrap_or("(untitled)")+ ),+ None => println!("{} offers to stream {player}: {}", from.short(), title.unwrap_or("(untitled)")),+ }+ offers+ .send((*from, player.to_owned(), title.map(str::to_owned), file.cloned()))+ .map_err(|e| e.to_string())}fn mirror_sink(&self,@@ -3327,10 +3341,10 @@ async fn run(cli: &Cli, node: &Node) -> Result<()> {let id = resolve(node, device)?;wait_until_online(node, id).await?;println!("Waiting for the PC to offer something to watch (Play on phone on its media card).");- let player = tokio::time::timeout(std::time::Duration::from_secs(*wait), async {- while let Some((from, player, _)) = offers.recv().await {+ let (player, file) = tokio::time::timeout(std::time::Duration::from_secs(*wait), async {+ while let Some((from, player, _, file)) = offers.recv().await {if from == id {- return Some(player);+ return Some((player, file));}}None@@ -3339,6 +3353,33 @@ async fn run(cli: &Cli, node: &Node) -> Result<()> {.ok().flatten().context("no offer came")?;+ // A video file: read it all from the PC, as the phone's player would.+ if let Some(file) = file {+ use std::io::Write;+ drop(recording.file.lock().unwrap().take());+ let mut saved = std::fs::File::create(out).context("can't create the file")?;+ let started = std::time::Instant::now();+ let mut offset = 0u64;+ while offset < file.size {+ let (size, bytes) = node+ .watch_file_read(id, offset, nectarlink_core::MEDIA_WATCH_MAX_READ)+ .await+ .context("the PC didn't send the file")?;+ if bytes.is_empty() || size != file.size {+ bail!("the PC sent {} bytes at {offset} of {size}", bytes.len());+ }+ saved.write_all(&bytes)?;+ offset += bytes.len() as u64;+ }+ let secs = started.elapsed().as_secs_f64().max(0.001);+ println!(+ "Read {} ({offset} bytes) in {secs:.1} s, {:.1} MB/s; it plays from {} ms.",+ file.name,+ offset as f64 / secs / 1e6,+ file.position+ );+ return Ok(());+ }let mut events = node.events();let options = nectarlink_core::MirrorStart {max_size: 1920,diff --git a/core/nectarlink-core/src/config.rs b/core/nectarlink-core/src/config.rsindex 0764854..d641b94 100644--- a/core/nectarlink-core/src/config.rs+++ b/core/nectarlink-core/src/config.rs@@ -322,12 +322,14 @@ pub trait Platform: Send + Sync + 'static {/// Play on phone: a paired PC offers to stream what `player` plays/// (`title`). The phone shows the offer; taking it starts mirroring with- /// `watch` set to `player`.+ /// `watch` set to `player`. With `file`, the phone plays that video file+ /// itself instead, read with [`Node::watch_file_read`](crate::Node::watch_file_read).fn watch_offered(&self,_from: &nectarlink_protocol::DeviceId,_player: &str,_title: Option<&str>,+ _file: Option<&nectarlink_protocol::messages::WatchFile>,) -> Result<(), String> {Err("this device doesn't show a PC's playback".into())}diff --git a/core/nectarlink-core/src/lib.rs b/core/nectarlink-core/src/lib.rsindex ae3afc0..c47a1e7 100644--- a/core/nectarlink-core/src/lib.rs+++ b/core/nectarlink-core/src/lib.rs@@ -91,8 +91,8 @@ pub use nectarlink_protocol::{PhotoAlbum, PhotoItem, PhotoNew as Photo, PhotoThumb, PowerLevel, ScreenCorners, ScreenRect,ScreenShape, SmsAttachment, SmsMessage, SmsPart, SmsThread, StorageChanged, StorageDelete,StorageEntries, StorageEntry, StorageList, StorageMkdir, StorageRead, StorageReadMeta, StorageRename,- StorageWriteAccept, StorageWriteDone, StorageWriteOffer, TaskNotify, TouchAction, WebcamConfig,- WebcamStart,+ StorageWriteAccept, StorageWriteDone, StorageWriteOffer, TaskNotify, TouchAction, WatchFile,+ WebcamConfig, WebcamStart,calls::{CONTROL as CALLS_CONTROL, DIAL as CALLS_DIAL, IN_CALL as CALLS_IN_CALL, LOG as CALLS_LOG,SHOW as CALLS_SHOW, STATE as CALLS_STATE,@@ -104,7 +104,7 @@ pub use nectarlink_protocol::{},format_mac, ipv4_broadcast, is_package_name, is_valid_storage_dir_path, is_valid_storage_id,is_valid_storage_name, is_valid_storage_path, magic_packet,- media_watch::{STREAM as MEDIA_STREAM, WATCH as MEDIA_WATCH},+ media_watch::{MAX_READ as MEDIA_WATCH_MAX_READ, STREAM as MEDIA_STREAM, WATCH as MEDIA_WATCH},mic::{LISTEN as MIC_LISTEN, STREAM as MIC_STREAM},mirror::{AUDIO as MIRROR_AUDIO, AUDIO_PLAYBACK as MIRROR_AUDIO_PLAYBACK, AV1 as MIRROR_AV1,diff --git a/core/nectarlink-core/src/media.rs b/core/nectarlink-core/src/media.rsindex a8311f8..1f589ef 100644--- a/core/nectarlink-core/src/media.rs+++ b/core/nectarlink-core/src/media.rs@@ -12,12 +12,14 @@ use std::{sync::{Arc, Mutex},};+use iroh::endpoint::{RecvStream, SendStream};use nectarlink_protocol::{DeviceId, Envelope, ErrorCode,messages::{- MediaCommand, MediaPlayer, MediaState, MediaWatch, media_actions, media_limits::PLAYERS, media_watch,- types,+ MediaCommand, MediaFileMeta, MediaFileRead, MediaPlayer, MediaState, MediaWatch, StreamHeader,+ WatchFile, media_actions, media_limits::PLAYERS, media_watch, types,},+ read_frame, write_frame,};use crate::{Error, Result, events::NodeEvent, node::Shared, session::Session};@@ -229,16 +231,37 @@ pub(crate) async fn handle(shared: &Arc<Shared>, session: &Arc<Session>, env: &EOk(true)}-/// Play on phone, on the PC: offers `peer` what `player` plays. The offer-/// is what lets that phone start mirroring with `watch` (see-/// [`watch_allowed`]).+/// A Play on phone offer this PC made a phone.+#[derive(Debug, Clone)]+pub(crate) struct WatchOffer {+ player: String,+ /// When it was made, or last taken or left.+ at: std::time::Instant,+ /// The file the phone may read, for a file it plays itself.+ file: Option<std::path::PathBuf>,+}++/// Play on phone, on the PC: offers `peer` what `player` plays, streamed,+/// or with `file` (and where it had got to, `position_ms`), that video+/// file for the phone to play itself. The offer is what lets that phone+/// start mirroring with `watch` (see [`watch_allowed`]), or read the file+/// (see [`serve_file`]).pub(crate) async fn offer_watch(shared: &Arc<Shared>,peer: DeviceId,player: String,title: Option<String>,+ file: Option<(std::path::PathBuf, u64)>,) -> Result<()> {- let watch = MediaWatch { player, title };+ let described = match &file {+ Some((path, position)) => {+ let size = tokio::fs::metadata(path).await.map_err(|e| Error::Internal(e.to_string()))?.len();+ let name = path.file_name().map(|n| n.to_string_lossy().into_owned()).unwrap_or_default();+ Some(WatchFile { name, size, position: *position })+ }+ None => None,+ };+ let watch = MediaWatch { player, title, file: described };if !watch.is_valid() {return Err(Error::Internal("not a player to offer".into()));}@@ -252,12 +275,12 @@ pub(crate) async fn offer_watch(return Err(Error::Denied);}let session = shared.session(&peer).ok_or(Error::Offline)?;- let player = watch.player.clone();- shared- .watch_offers- .lock()- .unwrap_or_else(|e| e.into_inner())- .insert(peer, (player, std::time::Instant::now()));+ let offer = WatchOffer {+ player: watch.player.clone(),+ at: std::time::Instant::now(),+ file: file.map(|(path, _)| path),+ };+ shared.watch_offers.lock().unwrap_or_else(|e| e.into_inner()).insert(peer, offer);let env = Envelope::new(types::MEDIA_WATCH, &watch)?;let answer =session.request(env, crate::session::REQUEST_TIMEOUT).await.and_then(|r| Ok(r.expect(types::OK)?));@@ -272,8 +295,8 @@ pub(crate) async fn offer_watch(pub(crate) fn watch_allowed(shared: &Shared, peer: &DeviceId, player: &str) -> bool {let mut offers = shared.watch_offers.lock().unwrap_or_else(|e| e.into_inner());match offers.get_mut(peer) {- Some((offered, at)) if offered == player && at.elapsed() < WATCH_OFFER => {- *at = std::time::Instant::now();+ Some(offer) if offer.player == player && offer.at.elapsed() < WATCH_OFFER => {+ offer.at = std::time::Instant::now();true}_ => false,@@ -288,14 +311,112 @@ pub(crate) fn watch_open(shared: &Shared, peer: &DeviceId) -> bool {.lock().unwrap_or_else(|e| e.into_inner()).get(peer)- .is_some_and(|(_, at)| at.elapsed() < WATCH_OFFER)+ .is_some_and(|offer| offer.at.elapsed() < WATCH_OFFER)}/// Watching stopped: the offer stays open for a while from now.pub(crate) fn renew_watch(shared: &Shared, peer: &DeviceId) {- if let Some((_, at)) = shared.watch_offers.lock().unwrap_or_else(|e| e.into_inner()).get_mut(peer) {- *at = std::time::Instant::now();+ if let Some(offer) = shared.watch_offers.lock().unwrap_or_else(|e| e.into_inner()).get_mut(peer) {+ offer.at = std::time::Instant::now();+ }+}++/// The file `peer` may read, while its offer is open (reading renews it).+fn offered_file(shared: &Shared, peer: &DeviceId) -> Option<std::path::PathBuf> {+ let mut offers = shared.watch_offers.lock().unwrap_or_else(|e| e.into_inner());+ let offer = offers.get_mut(peer).filter(|offer| offer.at.elapsed() < WATCH_OFFER)?;+ offer.at = std::time::Instant::now();+ offer.file.clone()+}++/// How long a `media/file` read may take to arrive or answer.+const FILE_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(20);++/// The PC's side of a `media/file` stream (its header already read): one+/// read of the offered file, if `peer` has one open.+pub(crate) async fn serve_file(+ shared: Arc<Shared>,+ peer: DeviceId,+ mut send: SendStream,+ mut recv: RecvStream,+) {+ use tokio::io::{AsyncReadExt, AsyncSeekExt};+ let Ok(Ok(Some(bytes))) = tokio::time::timeout(FILE_TIMEOUT, read_frame(&mut recv)).await else {+ return;+ };+ let Ok(env) = Envelope::from_cbor(&bytes) else { return };+ let reply_error = |code, msg: &'static str| Envelope::error(code, msg).reply_to(env.id);+ let read = match env.expect_body::<MediaFileRead>(types::MEDIA_FILE) {+ Ok(read) => read,+ Err(_) => {+ let _ =+ write_frame(&mut send, &reply_error(ErrorCode::BadMessage, "invalid media.file").to_cbor())+ .await;+ let _ = send.finish();+ return;+ }+ };+ let Some(path) = offered_file(&shared, &peer) else {+ let _ = write_frame(&mut send, &reply_error(ErrorCode::Denied, "no file offered").to_cbor()).await;+ let _ = send.finish();+ return;+ };+ let opened = async {+ let mut file = tokio::fs::File::open(&path).await?;+ let size = file.metadata().await?.len();+ let length = read.length.min(media_watch::MAX_READ).min(size.saturating_sub(read.offset));+ file.seek(std::io::SeekFrom::Start(read.offset.min(size))).await?;+ let mut data = vec![0u8; length as usize];+ file.read_exact(&mut data).await?;+ std::io::Result::Ok((size, data))}+ .await;+ let (size, data) = match opened {+ Ok(read) => read,+ Err(e) => {+ tracing::warn!(error = %e, "can't read the offered file");+ let _ =+ write_frame(&mut send, &reply_error(ErrorCode::Internal, "can't read it").to_cbor()).await;+ let _ = send.finish();+ return;+ }+ };+ let meta = MediaFileMeta { size, length: data.len() as u64 };+ let Ok(reply) = Envelope::new(types::MEDIA_FILE_META, &meta).map(|e| e.reply_to(env.id)) else { return };+ if write_frame(&mut send, &reply.to_cbor()).await.is_ok() && send.write_all(&data).await.is_ok() {+ let _ = send.finish();+ }+}++/// The phone's side: reads up to `length` bytes of the file `peer` (a PC)+/// offered, from `offset`. Returns the file's size and the bytes.+pub(crate) async fn read_file(session: &Session, offset: u64, length: u64) -> Result<(u64, Vec<u8>)> {+ let (mut send, mut recv) = session.open_bi().await?;+ let header = StreamHeader {+ svc: media_watch::SERVICE.into(),+ op: media_watch::OP_FILE.into(),+ v: media_watch::VERSION,+ };+ write_frame(&mut send, &Envelope::new(types::STREAM, &header)?.to_cbor()).await?;+ let read = MediaFileRead { offset, length: length.min(media_watch::MAX_READ) };+ write_frame(&mut send, &Envelope::new(types::MEDIA_FILE, &read)?.with_id(1).to_cbor()).await?;+ let _ = send.finish();+ let reply = match tokio::time::timeout(FILE_TIMEOUT, read_frame(&mut recv)).await {+ Ok(Ok(Some(bytes))) => Envelope::from_cbor(&bytes)?,+ Ok(Ok(None)) => return Err(Error::Offline),+ Ok(Err(e)) => return Err(e.into()),+ Err(_) => return Err(Error::Timeout),+ };+ let meta: MediaFileMeta = reply.expect_body(types::MEDIA_FILE_META)?;+ if meta.length > media_watch::MAX_READ {+ return Err(Error::Protocol("media.file sent too much".into()));+ }+ let mut data = vec![0u8; meta.length as usize];+ tokio::time::timeout(FILE_TIMEOUT, recv.read_exact(&mut data))+ .await+ .map_err(|_| Error::Timeout)?+ .map_err(|e| Error::Network(e.to_string()))?;+ Ok((meta.size, data))}/// The phone's side of `media.watch`: show the offer.@@ -311,10 +432,12 @@ async fn watch_offered(shared: &Arc<Shared>, peer: &DeviceId, watch: MediaWatch)}let platform = shared.platform.clone();let peer = *peer;- let MediaWatch { player, title } = watch;- match tokio::task::spawn_blocking(move || platform.watch_offered(&peer, &player, title.as_deref()))- .await- .unwrap_or_else(|e| Err(e.to_string()))+ let MediaWatch { player, title, file } = watch;+ match tokio::task::spawn_blocking(move || {+ platform.watch_offered(&peer, &player, title.as_deref(), file.as_ref())+ })+ .await+ .unwrap_or_else(|e| Err(e.to_string())){Ok(()) => Envelope::empty(types::OK),Err(reason) => {diff --git a/core/nectarlink-core/src/node.rs b/core/nectarlink-core/src/node.rsindex 4ee51cd..4080db8 100644--- a/core/nectarlink-core/src/node.rs+++ b/core/nectarlink-core/src/node.rs@@ -151,7 +151,7 @@ pub(crate) struct Shared {/// Peers granted one-time reverse-mirror consent for the current session.pub(crate) pc_mirror_sessions: Mutex<std::collections::HashSet<DeviceId>>,/// Play on phone offers this PC made, by phone: the player and when.- pub(crate) watch_offers: Mutex<HashMap<DeviceId, (String, std::time::Instant)>>,+ pub(crate) watch_offers: Mutex<HashMap<DeviceId, crate::media::WatchOffer>>,/// Stop signals for phone webcam streams arriving here, by phone.pub(crate) webcam_stops: Mutex<HashMap<DeviceId, Arc<tokio::sync::Notify>>>,/// Stop signals for phone microphone streams arriving here, by phone.@@ -2895,7 +2895,30 @@ impl Node {/// taking it starts mirroring with `watch`, which this PC then allows/// for that phone and player.pub async fn offer_watch(&self, peer: DeviceId, player: String, title: Option<String>) -> Result<()> {- crate::media::offer_watch(&self.shared, peer, player, title).await+ crate::media::offer_watch(&self.shared, peer, player, title, None).await+ }++ /// Play on phone for a video file: offers `peer` the `file` this PC's+ /// `player` plays, at `position_ms`, for the phone to play itself. The+ /// phone reads it with [`Node::watch_file_read`] while the offer is open,+ /// and only that file.+ pub async fn offer_watch_file(+ &self,+ peer: DeviceId,+ player: String,+ title: Option<String>,+ file: std::path::PathBuf,+ position_ms: u64,+ ) -> Result<()> {+ crate::media::offer_watch(&self.shared, peer, player, title, Some((file, position_ms))).await+ }++ /// On the phone: reads up to `length` bytes (at most 4 MiB) from+ /// `offset` of the file the PC `peer` offered. Returns the file's size+ /// and the bytes (fewer at its end).+ pub async fn watch_file_read(&self, peer: DeviceId, offset: u64, length: u64) -> Result<(u64, Vec<u8>)> {+ let session = self.connected(&peer)?;+ crate::media::read_file(&session, offset, length).await}/// Runs a command on a paired device's media player.diff --git a/core/nectarlink-core/src/transfer.rs b/core/nectarlink-core/src/transfer.rsindex 6fc3db2..55ff582 100644--- a/core/nectarlink-core/src/transfer.rs+++ b/core/nectarlink-core/src/transfer.rs@@ -1462,6 +1462,13 @@ pub(crate) async fn accept_stream(Some(h) if h.svc == mirror::SERVICE && h.op == mirror::OP_AUDIO && h.v == mirror::VERSION => {crate::mirror::receive_audio(shared, session.peer, send, recv).await;}+ Some(h)+ if h.svc == nectarlink_protocol::messages::media_watch::SERVICE+ && h.op == nectarlink_protocol::messages::media_watch::OP_FILE+ && h.v == nectarlink_protocol::messages::media_watch::VERSION =>+ {+ crate::media::serve_file(shared, session.peer, send, recv).await;+ }Some(h)if h.svc == nectarlink_protocol::messages::mic::SERVICE&& h.op == nectarlink_protocol::messages::mic::OP_PCMdiff --git a/core/nectarlink-core/tests/two_devices.rs b/core/nectarlink-core/tests/two_devices.rsindex 7f06e5e..2b911ee 100644--- a/core/nectarlink-core/tests/two_devices.rs+++ b/core/nectarlink-core/tests/two_devices.rs@@ -143,8 +143,10 @@ impl Platform for RecordingPlatform {_from: &nectarlink_core::DeviceId,player: &str,title: Option<&str>,+ file: Option<&nectarlink_core::WatchFile>,) -> Result<(), String> {- self.watch_offers.lock().unwrap().push(format!("{player}: {}", title.unwrap_or("")));+ let file = file.map(|f| format!(" [{} {} at {}]", f.name, f.size, f.position)).unwrap_or_default();+ self.watch_offers.lock().unwrap().push(format!("{player}: {}{file}", title.unwrap_or("")));Ok(())}fn mirror_stop_requested(&self, _peer: &nectarlink_core::DeviceId, session: u32) {@@ -5681,6 +5683,61 @@ async fn pc_audio_reaches_the_phone_over_both_paths() {phone.node.shutdown().await;}+/// Play on phone for a video file: the phone reads only the file the PC+/// offered, in ranges, while the offer is open.+#[tokio::test(flavor = "multi_thread")]+async fn play_on_phone_offers_a_video_file_the_phone_reads_in_ranges() {+ use nectarlink_core::{MEDIA_STREAM, MEDIA_WATCH, MEDIA_WATCH_MAX_READ};++ let mut pc = device_with("Desktop", DeviceKind::Desktop, &[MEDIA_STREAM]).await;+ let mut phone =+ device_with("Pixel", DeviceKind::Phone, &[MEDIA_WATCH, nectarlink_core::MIRROR_VIEW]).await;+ pair_qr(&mut pc, &mut phone).await;+ let (pc_id, phone_id) = (pc.node.device_id(), phone.node.device_id());++ // Nothing offered yet: nothing to read.+ let early = phone.node.watch_file_read(pc_id, 0, 1024).await;+ assert!(matches!(early, Err(Error::Denied)), "{early:?}");++ let dir = tempfile::tempdir().unwrap();+ let movie = dir.path().join("Holiday.mp4");+ let bytes: Vec<u8> = (0..(MEDIA_WATCH_MAX_READ as usize + 300_000)).map(|i| (i % 251) as u8).collect();+ std::fs::write(&movie, &bytes).unwrap();+ with_timeout(+ "offer the file",+ pc.node.offer_watch_file(phone_id, "vlc.exe".into(), Some("Holiday".into()), movie.clone(), 61_500),+ )+ .await+ .unwrap();+ let offered = format!("vlc.exe: Holiday [Holiday.mp4 {} at 61500]", bytes.len());+ assert_eq!(*phone.platform.watch_offers.lock().unwrap(), [offered]);++ // Ranges come back as they are on the PC, capped at one read's worth.+ let (size, head) =+ with_timeout("read the start", phone.node.watch_file_read(pc_id, 0, 1000)).await.unwrap();+ assert_eq!((size, head.as_slice()), (bytes.len() as u64, &bytes[..1000]));+ let (_, big) = with_timeout("read a lot", phone.node.watch_file_read(pc_id, 10, u64::MAX)).await.unwrap();+ assert_eq!(big.len() as u64, MEDIA_WATCH_MAX_READ);+ assert_eq!(big.as_slice(), &bytes[10..10 + MEDIA_WATCH_MAX_READ as usize]);+ let tail_at = bytes.len() as u64 - 100;+ let (_, tail) =+ with_timeout("read the end", phone.node.watch_file_read(pc_id, tail_at, 4096)).await.unwrap();+ assert_eq!(tail.as_slice(), &bytes[tail_at as usize..]);+ let (_, past) =+ with_timeout("read past it", phone.node.watch_file_read(pc_id, bytes.len() as u64 + 5, 10))+ .await+ .unwrap();+ assert!(past.is_empty());++ // A streamed offer replaces it: the file can't be read any more.+ with_timeout("offer a stream", pc.node.offer_watch(phone_id, "Chrome".into(), None)).await.unwrap();+ let after = phone.node.watch_file_read(pc_id, 0, 10).await;+ assert!(matches!(after, Err(Error::Denied)), "{after:?}");++ pc.node.shutdown().await;+ phone.node.shutdown().await;+}+#[tokio::test(flavor = "multi_thread")]async fn play_on_phone_lets_the_phone_watch_only_what_the_pc_offered() {use nectarlink_core::{MEDIA_STREAM, MEDIA_WATCH, MIRROR_SCREEN, MirrorStart};diff --git a/core/nectarlink-ffi/src/lib.rs b/core/nectarlink-ffi/src/lib.rsindex 1879829..ebe06db 100644--- a/core/nectarlink-ffi/src/lib.rs+++ b/core/nectarlink-ffi/src/lib.rs@@ -764,6 +764,24 @@ pub enum Event {},}+/// Play on phone: a video file a PC offers for the phone to play itself+/// (`docs/protocol/media.md` §4), read with `watch_file_read`.+#[derive(Debug, Clone, PartialEq, Eq, uniffi::Record)]+pub struct WatchFileOffer {+ /// Its file name, for the player to tell the format by.+ pub name: String,+ pub size: u64,+ /// Where the PC had got to, milliseconds.+ pub position_ms: u64,+}++/// Bytes of an offered file (`watch_file_read`), and the file's size.+#[derive(Debug, Clone, PartialEq, Eq, uniffi::Record)]+pub struct WatchFileChunk {+ pub size: u64,+ pub data: Vec<u8>,+}+/// One file or folder returned by `list_remote_storage` (`storage.list`).#[derive(Debug, Clone, PartialEq, Eq, uniffi::Record)]pub struct RemoteStorageEntry {@@ -2585,8 +2603,15 @@ pub trait Platform: Send + Sync {fn open_link(&self, from_id: String, url: String) -> bool;/// Play on phone: a paired PC offers to stream what its `player` plays/// (`title`). Show the offer; taking it starts PC mirroring with- /// `watch` set to `player`. False if it couldn't be shown.- fn watch_offered(&self, from_id: String, player: String, title: Option<String>) -> bool;+ /// `watch` set to `player`, or with `file`, plays that video file read+ /// with `watch_file_read`. False if it couldn't be shown.+ fn watch_offered(+ &self,+ from_id: String,+ player: String,+ title: Option<String>,+ file: Option<WatchFileOffer>,+ ) -> bool;/// A paired PC sent an ongoing or completed task notification (`task.notify`)./// False if it couldn't be shown.fn task_notify(&self, from_id: String, task: TaskNotify) -> bool;@@ -2793,8 +2818,16 @@ impl core::Platform for PlatformAdapter {fn open_link(&self, from: &DeviceId, url: &str) -> Result<(), String> {if self.0.open_link(from.to_string(), url.to_owned()) { Ok(()) } else { Err("not shown".into()) }}- fn watch_offered(&self, from: &DeviceId, player: &str, title: Option<&str>) -> Result<(), String> {- if self.0.watch_offered(from.to_string(), player.to_owned(), title.map(str::to_owned)) {+ fn watch_offered(+ &self,+ from: &DeviceId,+ player: &str,+ title: Option<&str>,+ file: Option<&core::WatchFile>,+ ) -> Result<(), String> {+ let file =+ file.map(|f| WatchFileOffer { name: f.name.clone(), size: f.size, position_ms: f.position });+ if self.0.watch_offered(from.to_string(), player.to_owned(), title.map(str::to_owned), file) {Ok(())} else {Err("not shown".into())@@ -4162,6 +4195,22 @@ impl NectarlinkNode {self.run(async move { node.network_changed().await }).await;}+ /// Play on phone: reads up to `length` bytes (at most 4 MiB) from+ /// `offset` of the video file the PC `peer` offered. Blocks; call off the+ /// main thread (a player's data source does).+ pub fn watch_file_read(+ &self,+ peer: String,+ offset: u64,+ length: u64,+ ) -> Result<WatchFileChunk, NectarlinkError> {+ let peer_id = parse_id(&peer)?;+ let node = self.node.clone();+ let (size, data) =+ self.block_on_rt(async move { node.watch_file_read(peer_id, offset, length).await })?;+ Ok(WatchFileChunk { size, data })+ }+/// Lists entries in a directory on a connected paired device (`storage.list`)./// `Denied` from a PC: it doesn't share its folders.pub fn list_remote_storage(diff --git a/core/nectarlink-protocol/src/messages.rs b/core/nectarlink-protocol/src/messages.rsindex 091f101..dfd43de 100644--- a/core/nectarlink-protocol/src/messages.rs+++ b/core/nectarlink-protocol/src/messages.rs@@ -39,6 +39,8 @@ pub mod types {pub const MEDIA_SYNC: &str = "media.sync";pub const MEDIA_COMMAND: &str = "media.command";pub const MEDIA_WATCH: &str = "media.watch";+ pub const MEDIA_FILE: &str = "media.file";+ pub const MEDIA_FILE_META: &str = "media.file.meta";pub const PC_POWER: &str = "pc.power";pub const PC_WAKE_INFO: &str = "pc.wake_info";pub const LINK_OPEN: &str = "link.open";@@ -1013,6 +1015,48 @@ pub mod media_watch {pub const WATCH: &str = "media.watch";/// Offered by PCs that stream a player's picture and sound on request.pub const STREAM: &str = "media.stream";+ /// Stream service and operation for reading an offered file.+ pub const SERVICE: &str = "media";+ pub const OP_FILE: &str = "file";+ pub const VERSION: u32 = 1;+ /// The most one `media.file` read returns.+ pub const MAX_READ: u64 = 4 * 1024 * 1024;+}++/// A video file a PC player plays, offered with `media.watch` for the phone+/// to play itself (`docs/protocol/media.md` §4).+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]+pub struct WatchFile {+ /// Its file name, for the phone's player to tell the format by.+ pub name: String,+ /// Its size in bytes.+ pub size: u64,+ /// Where the PC had got to, milliseconds.+ #[serde(default)]+ pub position: u64,+}++impl WatchFile {+ pub fn is_valid(&self) -> bool {+ (1..=media_limits::TEXT_CHARS).contains(&self.name.chars().count())+ && !self.name.contains(['/', '\\'])+ && self.size > 0+ }+}++/// Body of `media.file` on a `media/file` stream: read `length` bytes of+/// the offered file from `offset`.+#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]+pub struct MediaFileRead {+ pub offset: u64,+ pub length: u64,+}++/// Body of `media.file.meta`: the file's size and how many bytes follow.+#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]+pub struct MediaFileMeta {+ pub size: u64,+ pub length: u64,}/// Body of `media.watch`: a PC offers to stream what a player of its own@@ -1024,12 +1068,17 @@ pub struct MediaWatch {/// What it plays, to show with the offer.#[serde(default, skip_serializing_if = "Option::is_none")]pub title: Option<String>,+ /// A video file the phone plays itself, read from the PC, instead of+ /// watching the PC's stream.+ #[serde(default, skip_serializing_if = "Option::is_none")]+ pub file: Option<WatchFile>,}impl MediaWatch {pub fn is_valid(&self) -> bool {(1..=media_limits::ID_BYTES).contains(&self.player.len())&& self.title.as_ref().is_none_or(|t| t.chars().count() <= media_limits::TEXT_CHARS)+ && self.file.as_ref().is_none_or(WatchFile::is_valid)}}@@ -5069,11 +5118,26 @@ mod tests {#[test]fn play_on_phone_messages_validate() {- let offer = MediaWatch { player: "Chrome".into(), title: Some("A video".into()) };+ let offer = MediaWatch { player: "Chrome".into(), title: Some("A video".into()), file: None };assert!(offer.is_valid());- assert!(!MediaWatch { player: String::new(), title: None }.is_valid());- assert!(!MediaWatch { player: "p".repeat(257), title: None }.is_valid());- assert!(!MediaWatch { player: "Chrome".into(), title: Some("t".repeat(257)) }.is_valid());+ assert!(!MediaWatch { player: String::new(), title: None, file: None }.is_valid());+ assert!(!MediaWatch { player: "p".repeat(257), title: None, file: None }.is_valid());+ assert!(!MediaWatch { player: "Chrome".into(), title: Some("t".repeat(257)), file: None }.is_valid());++ // A file offer: a plain name and a size; it goes over the wire as is.+ let file = WatchFile { name: "Holiday.mp4".into(), size: 1_000, position: 61_500 };+ let with_file = MediaWatch { player: "vlc.exe".into(), title: None, file: Some(file.clone()) };+ assert!(with_file.is_valid());+ let env = Envelope::new(types::MEDIA_WATCH, &with_file).unwrap();+ assert_eq!(Envelope::from_cbor(&env.to_cbor()).unwrap().body::<MediaWatch>().unwrap(), with_file);+ for bad in [+ WatchFile { name: String::new(), ..file.clone() },+ WatchFile { name: "a/b.mp4".into(), ..file.clone() },+ WatchFile { name: "a\\b.mp4".into(), ..file.clone() },+ WatchFile { size: 0, ..file.clone() },+ ] {+ assert!(!MediaWatch { file: Some(bad.clone()), ..with_file.clone() }.is_valid(), "{bad:?}");+ }let screen = MirrorStart {max_size: 1280,diff --git a/desktop/app/src/main.rs b/desktop/app/src/main.rsindex 359e9c0..ccbd9a0 100644--- a/desktop/app/src/main.rs+++ b/desktop/app/src/main.rs@@ -28,6 +28,7 @@ mod local_api;mod logging;mod mark;mod media;+mod media_file;mod messages;mod mirror;mod notification_store;diff --git a/desktop/app/src/media_file.rs b/desktop/app/src/media_file.rsnew file mode 100644index 0000000..9868588--- /dev/null+++ b/desktop/app/src/media_file.rs@@ -0,0 +1,127 @@+// SPDX-License-Identifier: GPL-3.0-or-later+//! Play on phone for video files (docs/protocol/media.md §4): which file a+//! desktop player on this PC plays. Players don't say, but Windows keeps a+//! shortcut to every file opened lately (the Recent folder), and the+//! player's title or its window's names the file.++use std::path::{Path, PathBuf};++use crate::win::{browser, shortcut};++/// Formats a phone's own player plays.+const PLAYABLE: &[&str] = &["mp4", "m4v", "mkv", "webm", "mov", "3gp", "ts"];++/// How many of the latest Recent shortcuts to look through.+const RECENT: usize = 400;++/// Whether a phone plays a file of this name itself.+pub fn playable(name: &str) -> bool {+ Path::new(name)+ .extension()+ .and_then(|e| e.to_str())+ .is_some_and(|e| PLAYABLE.iter().any(|p| e.eq_ignore_ascii_case(p)))+}++/// Letters and digits only, lowercase: "Holiday (2024).mp4 - VLC" and+/// "holiday_2024" compare alike.+fn words(text: &str) -> String {+ text.chars().filter(|c| c.is_alphanumeric()).flat_map(char::to_lowercase).collect()+}++/// How well `title` names the file `file_name`: 2 when it is just its+/// name, 1 when one holds the other, 0 when it doesn't name it.+pub(crate) fn names(title: &str, file_name: &str) -> u8 {+ let stem = Path::new(file_name).file_stem().and_then(|s| s.to_str()).unwrap_or(file_name);+ let (title, stem) = (words(title), words(stem));+ if title.chars().count() < 4 || stem.chars().count() < 4 {+ 0+ } else if title == stem {+ 2+ } else if title.contains(&stem) || stem.contains(&title) {+ 1+ } else {+ 0+ }+}++/// The file among `recent` (file names, newest first) that `titles` name+/// best: just its name, else the longest name a title holds; the newest of+/// equals.+pub(crate) fn pick<'a>(titles: &[&str], recent: &'a [String]) -> Option<&'a String> {+ let mut best: Option<((u8, usize), &String)> = None;+ for name in recent.iter().filter(|n| playable(n)) {+ let level = titles.iter().map(|t| names(t, name)).max().unwrap_or(0);+ if level == 0 {+ continue;+ }+ let score = (level, words(name).len());+ if best.is_none_or(|(s, _)| score > s) {+ best = Some((score, name));+ }+ }+ best.map(|(_, name)| name)+}++/// The video file this PC's `player` plays (`title`, as its media session+/// says), if it is one a phone plays and Windows lists it as opened lately.+pub fn file_for(player: &str, title: &str) -> Option<PathBuf> {+ let programs = browser::programs_for(player);+ if programs.iter().any(|p| browser::is_browser(p)) {+ return None;+ }+ let windows: Vec<String> =+ browser::windows().into_iter().filter(|w| programs.contains(&w.program)).map(|w| w.title).collect();+ let mut titles: Vec<&str> = windows.iter().map(String::as_str).collect();+ titles.push(title);++ let dir = shortcut::recent_dir()?;+ let mut links: Vec<(std::time::SystemTime, PathBuf)> = std::fs::read_dir(&dir)+ .ok()?+ .filter_map(Result::ok)+ .filter(|e| e.path().extension().is_some_and(|x| x.eq_ignore_ascii_case("lnk")))+ .filter_map(|e| Some((e.metadata().ok()?.modified().ok()?, e.path())))+ .collect();+ links.sort_by_key(|(modified, _)| std::cmp::Reverse(*modified));+ links.truncate(RECENT);+ // "Holiday.mp4.lnk" is the shortcut to Holiday.mp4.+ let names: Vec<String> =+ links.iter().filter_map(|(_, p)| p.file_stem().and_then(|s| s.to_str()).map(str::to_owned)).collect();+ let name = pick(&titles, &names)?;+ let link = links.iter().find(|(_, p)| p.file_stem().and_then(|s| s.to_str()) == Some(name.as_str()))?;+ let target = shortcut::target(&link.1)?;+ (target.is_file() && target.file_name().and_then(|n| n.to_str()).is_some_and(playable)).then_some(target)+}++#[cfg(test)]+mod tests {+ use super::*;++ #[test]+ fn only_formats_phones_play() {+ assert!(playable("Holiday.MP4") && playable("a.mkv") && playable("clip.webm"));+ assert!(!playable("old.avi") && !playable("song.mp3") && !playable("notes.txt") && !playable("mp4"));+ }++ #[test]+ fn a_title_names_its_file() {+ assert_eq!(names("Holiday 2024", "Holiday_2024.mp4"), 2);+ assert_eq!(names("Holiday_2024.mp4 - VLC media player", "Holiday_2024.mp4"), 1);+ assert_eq!(names("Holiday", "Holiday 2024 (director's cut).mkv"), 1);+ assert_eq!(names("Something else", "Holiday.mp4"), 0);+ assert_eq!(names("abc", "abc.mp4"), 0, "too short to tell");+ }++ #[test]+ fn the_best_named_playable_file_wins_and_newest_breaks_ties() {+ let recent: Vec<String> =+ ["Holiday 2024.mkv", "Holiday 2024 extras.mp4", "Holiday 2024.avi", "Holiday 2024.mp4"]+ .map(String::from)+ .to_vec();+ // Exactly named beats only contained; the .avi doesn't play on a phone.+ assert_eq!(pick(&["Holiday 2024"], &recent).map(String::as_str), Some("Holiday 2024.mkv"));+ assert_eq!(pick(&["Unrelated"], &recent), None);+ // The window's title counts too.+ let titles = ["", "Holiday 2024 extras.mp4 - VLC media player"];+ assert_eq!(pick(&titles, &recent).map(String::as_str), Some("Holiday 2024 extras.mp4"));+ }+}diff --git a/desktop/app/src/play_on_phone.rs b/desktop/app/src/play_on_phone.rsindex bc22235..1c72143 100644--- a/desktop/app/src/play_on_phone.rs+++ b/desktop/app/src/play_on_phone.rs@@ -146,6 +146,8 @@ pub fn play(player: String) {let _ = unsafe { CoInitializeEx(None, COINIT_MULTITHREADED) };let (title, position) = now_playing(&player);let window = browser::window_for(&player, &title);+ // A video file in a desktop player: the phone plays the file itself.+ let file = crate::media_file::file_for(&player, &title);let url = window.as_ref().filter(|w| browser::is_browser(&w.program))@@ -153,6 +155,18 @@ pub fn play(player: String) {let plan = nectarlink_core::play_on_phone::plan(url.as_deref(), position);let Some(node) = core_host::node() else { return };core_host::spawn(async move {+ if let Some(file) = file {+ let shown = (!title.is_empty()).then_some(title);+ let at = u64::from(position.unwrap_or(0)) * 1000;+ match node.offer_watch_file(phone, player, shown, file, at).await {+ Ok(()) => {+ asked();+ show_message(tr!("PlayOnPhone", "Tap the notification on %1 to watch.", name));+ }+ Err(e) => show_message(failed(&e, &name)),+ }+ return;+ }match plan {PlayOnPhone::Open { url, service } => match node.open_link(phone, url).await {Ok(()) => {diff --git a/desktop/app/src/win/shortcut.rs b/desktop/app/src/win/shortcut.rsindex a9f83c6..1aaa88c 100644--- a/desktop/app/src/win/shortcut.rs+++ b/desktop/app/src/win/shortcut.rs@@ -6,7 +6,9 @@ use std::path::{Path, PathBuf};use windows::{Win32::{System::Com::{CLSCTX_INPROC_SERVER, CoCreateInstance, CoTaskMemFree, IPersistFile},- UI::Shell::{FOLDERID_SendTo, IShellLinkW, KF_FLAG_DEFAULT, SHGetKnownFolderPath, ShellLink},+ UI::Shell::{+ FOLDERID_Recent, FOLDERID_SendTo, IShellLinkW, KF_FLAG_DEFAULT, SHGetKnownFolderPath, ShellLink,+ },},core::{HSTRING, Interface},};@@ -57,11 +59,39 @@ pub fn arguments(path: &Path) -> Option<String> {})}+/// What a shortcut points at, or `None` if it can't be read (or points at+/// no file, as a shortcut to a website does).+pub fn target(path: &Path) -> Option<PathBuf> {+ with_com(|| {+ // SAFETY: as in `arguments`; the buffer holds the longest path.+ unsafe {+ let link: IShellLinkW = CoCreateInstance(&ShellLink, None, CLSCTX_INPROC_SERVER).ok()?;+ link.cast::<IPersistFile>()+ .ok()?+ .Load(&HSTRING::from(path.as_os_str()), Default::default())+ .ok()?;+ let mut buffer = [0u16; 32768];+ link.GetPath(&mut buffer, std::ptr::null_mut(), 0).ok()?;+ let len = buffer.iter().position(|&c| c == 0).unwrap_or(buffer.len());+ (len > 0).then(|| PathBuf::from(String::from_utf16_lossy(&buffer[..len])))+ }+ })+}+/// The user's "Send to" folder.pub fn send_to_dir() -> Option<PathBuf> {+ known_folder(&FOLDERID_SendTo)+}++/// The user's Recent folder: a shortcut to each file opened lately.+pub fn recent_dir() -> Option<PathBuf> {+ known_folder(&FOLDERID_Recent)+}++fn known_folder(id: &windows::core::GUID) -> Option<PathBuf> {// SAFETY: the returned string is freed with CoTaskMemFree as documented.unsafe {- let path = SHGetKnownFolderPath(&FOLDERID_SendTo, KF_FLAG_DEFAULT, None).ok()?;+ let path = SHGetKnownFolderPath(id, KF_FLAG_DEFAULT, None).ok()?;let result = path.to_string().ok().map(PathBuf::from);CoTaskMemFree(Some(path.as_ptr().cast()));result@@ -83,6 +113,8 @@ mod tests {};write(&path, &shortcut).unwrap();assert_eq!(arguments(&path).as_deref(), Some(shortcut.arguments.as_str()));+ assert_eq!(target(&path), Some(shortcut.target.clone()));+ assert_eq!(target(&dir.path().join("missing.lnk")), None);// Replacing works too.write(&path, &Shortcut { arguments: "--send-to def".into(), ..shortcut }).unwrap();assert_eq!(arguments(&path).as_deref(), Some("--send-to def"));@@ -94,4 +126,10 @@ mod tests {let dir = send_to_dir().expect("every user has one");assert!(dir.ends_with("SendTo"), "{}", dir.display());}++ #[test]+ fn finds_the_recent_folder() {+ let dir = recent_dir().expect("every user has one");+ assert!(dir.ends_with("Recent"), "{}", dir.display());+ }}diff --git a/docs/protocol/media.md b/docs/protocol/media.mdindex be69cf4..d9b212f 100644--- a/docs/protocol/media.md+++ b/docs/protocol/media.md@@ -88,6 +88,9 @@ what plays, and the PC decides:JioHotstar, Disney+ and the like) come out black when captured. The PCsends the page's address with `link.open` ([actions.md](actions.md)),which Android hands to the service's app, and pauses its own player.+- **A video file** a desktop player plays (VLC, Media Player and the like)+ is offered as a file: the phone reads it from the PC and plays it+ itself, from where the PC had got to, at its full quality.- **Everything else** is streamed from the PC as it plays, picture andsound, so whatever the PC does to it carries over (a browser's adblocker on YouTube, for one). The PC keeps playing.@@ -95,8 +98,10 @@ what plays, and the PC decides:Streaming starts with an offer from the PC:```-t = "media.watch" id = n b = { player: text, title?: text } // PC to phone+t = "media.watch" id = n b = { player: text, title?: text, file?: WatchFile } // PC to phonet = "ok" re = n++WatchFile = { name: text, size: uint, position: uint }````player` is the PC's ID for the player, as in `media.state`; `title` is@@ -118,3 +123,31 @@ stream, and its sound as Phone speaker does. Input from the phone isignored while it watches. Anything else in that `mirror.start` is as forthe PC's screen; without an offer the answer is `DENIED`, and the PCdoesn't ask its user to share the screen.++### Playing a file++With `file`, the phone plays the file itself instead of watching a+stream. `name` is the file's name (1 to 256 characters, no `/` or `\`),+for the phone's player to tell the format by; `size` its size in bytes+(more than 0); `position` where the PC had got to, in milliseconds. A+PC offers a file only in a format phones play (MP4, MKV, WebM, MOV, 3GP,+MPEG-TS), and streams anything else. Once the phone plays it, it pauses+the PC's player with `media.command`.++The phone reads the file on bidirectional streams of their own, one read+each:++```+stream header { svc: "media", op: "file", v: 1 }+t = "media.file" id = 1 b = { offset: uint, length: uint } // phone to PC+t = "media.file.meta" re = 1 b = { size: uint, length: uint } // PC to phone+<length bytes>+```++The PC answers with at most 4 MiB (`length` is cut to that, and to the+end of the file; past the end it is 0), then the bytes, and finishes the+stream. It serves only the file of its open offer to that phone (the+same 10 minutes as for watching, renewed by every read), so a phone can't+read anything else on the PC; without one the answer is `DENIED`.+A newer offer, streamed or not, replaces it.+