aboutsummaryrefslogtreecommitdiffstats
path: root/packages/meshbay-hub
diff options
context:
space:
mode:
Diffstat (limited to 'packages/meshbay-hub')
-rw-r--r--packages/meshbay-hub/src/meshbay_hub/static/cast-session.js59
-rw-r--r--packages/meshbay-hub/src/meshbay_hub/static/music-app.js3
-rw-r--r--packages/meshbay-hub/src/meshbay_hub/static/music-player.js212
-rw-r--r--packages/meshbay-hub/src/meshbay_hub/static/platform.js25
-rw-r--r--packages/meshbay-hub/src/meshbay_hub/static/style.css2
-rw-r--r--packages/meshbay-hub/src/meshbay_hub/static/video-player.js1
-rw-r--r--packages/meshbay-hub/tests/harness/cast_session_probe.py166
-rw-r--r--packages/meshbay-hub/tests/test_android_cast.py8
-rw-r--r--packages/meshbay-hub/tests/test_cast_files.py372
-rw-r--r--packages/meshbay-hub/tests/test_cast_photos.py240
10 files changed, 794 insertions, 294 deletions
diff --git a/packages/meshbay-hub/src/meshbay_hub/static/cast-session.js b/packages/meshbay-hub/src/meshbay_hub/static/cast-session.js
index 007722c..9f93919 100644
--- a/packages/meshbay-hub/src/meshbay_hub/static/cast-session.js
+++ b/packages/meshbay-hub/src/meshbay_hub/static/cast-session.js
@@ -11,10 +11,14 @@ import * as platform from './platform.js';
// in the lightbox is shown there. In memory only: a television chosen
// yesterday may be off today, and choosing it again is one tap.
//
-// The receiver is the default media receiver, which shows a film or a
-// picture and nothing else; the interface itself stays on this device.
+// The receiver is the default media receiver, which shows one film, one
+// picture or one track at a time and nothing else; the interface itself stays
+// on this device. `owner` says which of the three the television is showing
+// now ('video', 'photo', 'music'), so that the music bar stops treating the
+// receiver as its own once a film or a photo has taken it.
let current = null;
+let owner = null;
const listeners = new Set();
function notify() {
@@ -23,7 +27,15 @@ function notify() {
export const castSession = {
get device() { return current; },
+ get owner() { return owner; },
get supported() { return Boolean(platform.capabilities.lanCast && platform.cast.available); },
+ /** Whether a whole file (a photo, a track) can be sent: an older shell cannot. */
+ get canSendFiles() { return this.supported && platform.cast.canSendFiles; },
+ /** The television now shows what `who` sends it. */
+ claim(who) {
+ owner = who;
+ notify();
+ },
choose(device) {
current = device ? { id: device.id, name: device.name } : null;
notify();
@@ -32,6 +44,7 @@ export const castSession = {
forget() { this.choose(null); },
/** Stop casting: the television goes back to its idle screen. */
async end() {
+ owner = null;
this.choose(null);
photoGen++;
await platform.cast.chromecastDisconnect().catch(() => {});
@@ -49,6 +62,27 @@ export function useCastDevice() {
return device;
}
+// ── A file on the television ─────────────────────────────────────────────────
+//
+// A photo or a music track, whole, handed to the relay and loaded on the
+// session's television — connected if it is not yet, reloaded if it is. The
+// relay, not the page, tells the receiver what the file is; the page only
+// names it (title, artist, album, cover) and says where in it to begin.
+
+export async function loadFile({ owner: who, startAt = 0, ...file }) {
+ const device = current;
+ if (!device || !castSession.canSendFiles) throw new Error('No television to cast to');
+ castSession.claim(who);
+ const sent = await platform.cast.file(file);
+ if (!sent) throw new Error('The cast relay did not take the file');
+ const status = await platform.cast.status();
+ if (status && status.chromecast && status.chromecast.connected) {
+ await platform.cast.chromecastReload({ mediaUrl: sent.url, startAt });
+ } else {
+ await platform.cast.chromecastConnect({ deviceId: device.id, mediaUrl: sent.url, startAt });
+ }
+}
+
// ── A photo on the television ────────────────────────────────────────────────
//
// Scaled to fit 1920×1080 and re-encoded as JPEG before it leaves: a camera's
@@ -87,18 +121,10 @@ let photoChain = Promise.resolve();
export function showPhoto(blob) {
const gen = ++photoGen;
photoChain = photoChain.then(async () => {
- const device = current;
- if (gen !== photoGen || !device || !platform.cast.canShowImages) return;
+ if (gen !== photoGen || !current || !castSession.canSendFiles) return;
const bytes = await scaledForTv(blob);
if (gen !== photoGen) return;
- const shown = await platform.cast.image({ bytes, type: 'image/jpeg' });
- if (!shown || gen !== photoGen) return;
- const status = await platform.cast.status();
- if (status && status.chromecast && status.chromecast.connected) {
- await platform.cast.chromecastReload({ mediaUrl: shown.url });
- } else {
- await platform.cast.chromecastConnect({ deviceId: device.id, mediaUrl: shown.url });
- }
+ await loadFile({ owner: 'photo', bytes, type: 'image/jpeg' });
}).catch((err) => console.warn('[cast] photo not shown:', err));
return photoChain;
}
@@ -111,9 +137,12 @@ export function showPhoto(blob) {
// toolbar. `cls` and `iconCls` are the button's and its icon's own classes,
// so it matches its neighbours.
// `onChosen` runs after a television is picked, for the view that has
-// something to show on it straight away.
+// something to show on it straight away. `up` opens the menu above the
+// button, for one at the bottom of the window.
-export function CastButton({ variant = 'page', cls = 'tb-btn tb-btn-icon', iconCls = '', onChosen }) {
+export function CastButton({
+ variant = 'page', cls = 'tb-btn tb-btn-icon', iconCls = '', onChosen, up = false,
+}) {
const device = useCastDevice();
const [open, setOpen] = useState(false);
const [devices, setDevices] = useState([]);
@@ -143,7 +172,7 @@ export function CastButton({ variant = 'page', cls = 'tb-btn tb-btn-icon', iconC
if (!castSession.supported) return null;
const overlay = variant === 'overlay';
- const menuCls = overlay ? 'cast-picker' : 'ctx-menu cast-menu';
+ const menuCls = `${overlay ? 'cast-picker' : 'ctx-menu cast-menu'}${up ? ' cast-menu-up' : ''}`;
const itemCls = overlay ? 'cast-picker-item' : 'ctx-menu-item';
const title = device ? `${t('cast.casting_to')} ${device.name}` : t('cast.start');
diff --git a/packages/meshbay-hub/src/meshbay_hub/static/music-app.js b/packages/meshbay-hub/src/meshbay_hub/static/music-app.js
index 67be344..f2c4374 100644
--- a/packages/meshbay-hub/src/meshbay_hub/static/music-app.js
+++ b/packages/meshbay-hub/src/meshbay_hub/static/music-app.js
@@ -10,6 +10,7 @@ import { usePager, Pager, pageSizeFrom } from './pager.js';
import { Menu, MenuDots, useMenu } from './menu.js';
import { PlaylistMenuButton, NameModal, usePlaylists } from './playlist-menu.js';
import * as P from './playlists.js';
+import { CastButton } from './cast-session.js';
// -- Music --------------------------------------------------------------------
//
@@ -747,6 +748,8 @@ function MusicApp({
lists=${playlists} reload=${reloadPlaylists}
onPlayQueue=${onPlayQueue} onSync=${syncNow} />`}
<${Pager} pager=${pager} />
+ ${/* The television the music bar plays to (cast-session.js). */''}
+ <${CastButton} />
${!hideFilter && html`<div class="tb-search">
<${Icon} name="search" />
<input type="text" placeholder="${t('group.filter')}"
diff --git a/packages/meshbay-hub/src/meshbay_hub/static/music-player.js b/packages/meshbay-hub/src/meshbay_hub/static/music-player.js
index e97ce00..4587990 100644
--- a/packages/meshbay-hub/src/meshbay_hub/static/music-player.js
+++ b/packages/meshbay-hub/src/meshbay_hub/static/music-player.js
@@ -6,7 +6,8 @@ import { t } from './i18n.js';
import { Icon } from './icon.js';
import { useStickyBand } from './sticky.js';
import { CHUNK_SIZE, pipelinedDownload } from './file-utils.js';
-import { playback } from './platform.js';
+import { playback, cast as castBridge } from './platform.js';
+import { castSession, useCastDevice, loadFile, CastButton } from './cast-session.js';
/**
* The Music app's persistent player bar (docs/MESHBAY_DESIGN.md §9.8).
@@ -30,6 +31,14 @@ import { playback } from './platform.js';
* through `transport.requestAudioTranscode` first — a node-side,
* cached-after-once AAC/M4A conversion — before the same download/blob path
* runs.
+ *
+ * With a television chosen (cast-session.js), the same decrypted track is
+ * handed to the cast relay instead of the <audio> element, with its cover,
+ * and the bar becomes the receiver's remote: its play, pause, seek, previous
+ * and next drive the television, its clock is the television's, and the end
+ * of a track there moves the queue on as `ended` does here (docs/MESHBAY_DESIGN.md
+ * §11.4). The element keeps the track's source for its duration, and never
+ * plays while casting.
*/
const MIME_BY_EXT = {
@@ -351,6 +360,64 @@ function MusicPlayerBar({ getConnection, queue, onClose, userPrefs, onSaveQueue
}
}, []);
+ // ── Casting ──────────────────────────────────────────────────────────────
+ const castDevice = useCastDevice();
+ const remote = Boolean(castDevice) && castSession.canSendFiles;
+ const remoteRef = useRef(remote);
+ // Which track the television was last given, where it was when last
+ // polled, and which track's end has already moved the queue on: the
+ // receiver keeps reporting FINISHED until it is given something else.
+ const remoteTrackRef = useRef(null);
+ const remotePosRef = useRef(null);
+ const finishedRef = useRef(null);
+ // True while a track is on its way to the television: the receiver's
+ // status still describes the previous one until it lands.
+ const sendingRef = useRef(false);
+
+ // The track's cover, for the television to show beside it — found as the
+ // album card finds it (music-app.js): this track's own, else another queued
+ // track of the same album that carries one, else the cover the node found
+ // for the album. The node answers that last question from its cache, and
+ // asks MusicBrainz only where the group has it on and the track is tagged.
+ // A cover that cannot be had is no reason not to play the track.
+ const fetchCover = useCallback(async (entry) => {
+ try {
+ const { transport, gek } = await getConnection(entry.groupId);
+ if (!transport || !transport.connected) return null;
+ const sibling = entry.album && tracks.find((tr) => tr.thumb_hash
+ && tr.groupId === entry.groupId && tr.album === entry.album && tr.artist === entry.artist);
+ let hash = entry.thumb_hash || (sibling && sibling.thumb_hash) || null;
+ if (!hash && transport.fetchMusicMeta) {
+ const meta = await transport.fetchMusicMeta(entry.id).catch(() => null);
+ hash = (meta && meta.cover_thumb_hash) || null;
+ }
+ if (!hash) return null;
+ const chunks = await pipelinedDownload(transport, gek, hash, 1);
+ return { bytes: new Uint8Array(await new Blob(chunks).arrayBuffer()), type: 'image/jpeg' };
+ } catch {
+ return null;
+ }
+ }, [getConnection, tracks]);
+
+ const sendToTv = useCallback(async (entry, startAt = 0) => {
+ const cached = blobCacheRef.current.get(entry.id);
+ if (!cached) throw new Error(t('music.err_playback'));
+ sendingRef.current = true;
+ try {
+ const cover = await fetchCover(entry);
+ remoteTrackRef.current = entry.id;
+ remotePosRef.current = startAt;
+ finishedRef.current = null;
+ await loadFile({
+ owner: 'music', startAt,
+ bytes: new Uint8Array(await cached.blob.arrayBuffer()), type: cached.mime, cover,
+ title: entry.display_title || entry.name, artist: entry.artist || '', album: entry.album || '',
+ });
+ } finally {
+ sendingRef.current = false;
+ }
+ }, [fetchCover]);
+
const fetchTrackBlob = useCallback(async (entry) => {
const cached = blobCacheRef.current.get(entry.id);
if (cached) return cached.url;
@@ -395,7 +462,8 @@ function MusicPlayerBar({ getConnection, queue, onClose, userPrefs, onSaveQueue
const chunks = await pipelinedDownload(transport, gek, downloadId, totalChunks);
const blob = new Blob(chunks, { type: mime });
const url = URL.createObjectURL(blob);
- blobCacheRef.current.set(entry.id, { url, order: blobInsertRef.current++ });
+ // The blob itself too: a cast hands its bytes to the relay.
+ blobCacheRef.current.set(entry.id, { url, blob, mime, order: blobInsertRef.current++ });
evictOldBlobs();
return url;
}, [getConnection, evictOldBlobs]);
@@ -520,6 +588,22 @@ function MusicPlayerBar({ getConnection, queue, onClose, userPrefs, onSaveQueue
if (!audio) return;
audio.src = url;
audio.currentTime = 0;
+ if (remoteRef.current) {
+ // To the television; the element keeps the source for its duration.
+ audio.pause();
+ setCurrentTime(0);
+ try {
+ await sendToTv(currentTrack, 0);
+ } catch (castErr) {
+ castErr.isCast = true;
+ throw castErr;
+ }
+ if (loadTokenRef.current !== myToken) return;
+ consecutiveFailuresRef.current = 0;
+ setPlaying(true);
+ prefetchNext(pos);
+ return;
+ }
await audio.play();
consecutiveFailuresRef.current = 0;
setPlaying(true);
@@ -529,6 +613,9 @@ function MusicPlayerBar({ getConnection, queue, onClose, userPrefs, onSaveQueue
// Autoplay refused (no recent tap): the track is loaded and fine, it
// waits for the play button. Not an error, and no reason to skip it.
if (err && err.name === 'NotAllowedError') { setPlaying(false); prefetchNext(pos); return; }
+ // The television did not take it: that says nothing about the track,
+ // so the queue stays where it is, with the reason on screen.
+ if (err && err.isCast) { setError(err.message || String(err)); setPlaying(false); return; }
if (err && err.isGroupDown && asleepOrJustWoke()) {
// Our own connection is what went, not the group: keep the spinner
// and this track, and try it again once we are back.
@@ -557,6 +644,9 @@ function MusicPlayerBar({ getConnection, queue, onClose, userPrefs, onSaveQueue
// stream). Both paths converge on the same bounded advance, so whichever
// fires first, the queue moves on exactly once.
const handleMediaError = useCallback(() => {
+ // Casting, the element only holds the source for its duration: whether it
+ // could decode it says nothing about what the television is playing.
+ if (remoteRef.current) return;
const audio = audioRef.current;
const mediaError = audio && audio.error;
setError((mediaError && mediaError.message) || t('music.err_playback'));
@@ -583,27 +673,111 @@ function MusicPlayerBar({ getConnection, queue, onClose, userPrefs, onSaveQueue
if (order.length === 0) return;
// A few seconds in: restart the current track, the way most players do,
// rather than always jumping to the previous one.
+ if (remoteRef.current && castSession.owner === 'music' && (remotePosRef.current || 0) > 3) {
+ castBridge.chromecastSeek(0).catch(() => {});
+ remotePosRef.current = 0;
+ setCurrentTime(0);
+ return;
+ }
const audio = audioRef.current;
- if (audio && audio.currentTime > 3) { audio.currentTime = 0; return; }
+ if (!remoteRef.current && audio && audio.currentTime > 3) { audio.currentTime = 0; return; }
if (pos > 0) { skipTo(pos - 1); return; }
if (repeat === 'all') { skipTo(order.length - 1); }
}, [pos, order.length, repeat, skipTo]);
const onEnded = useCallback(() => {
if (repeat === 'one') {
+ if (remoteRef.current && currentTrack) {
+ sendToTv(currentTrack, 0).catch((e) => { setError(e.message || String(e)); setPlaying(false); });
+ return;
+ }
const audio = audioRef.current;
if (audio) { audio.currentTime = 0; audio.play().catch(() => {}); }
return;
}
skipNext();
- }, [repeat, skipNext]);
+ }, [repeat, skipNext, currentTrack, sendToTv]);
+ const onEndedRef = useRef(onEnded);
+ onEndedRef.current = onEnded;
const togglePlaying = useCallback(() => {
+ if (remoteRef.current && currentTrack) {
+ // A film or a photo has the television: playing again gives it back
+ // the track, from where it was.
+ if (castSession.owner !== 'music') {
+ sendToTv(currentTrack, remotePosRef.current || 0)
+ .then(() => { setError(''); setPlaying(true); })
+ .catch((e) => { setError(e.message || String(e)); setPlaying(false); });
+ return;
+ }
+ (playing ? castBridge.chromecastPause() : castBridge.chromecastPlay())
+ .then(() => setPlaying(!playing)).catch(() => {});
+ return;
+ }
const audio = audioRef.current;
if (!audio) return;
if (playing) { audio.pause(); setPlaying(false); }
else { audio.play().then(() => { setError(''); setPlaying(true); }).catch(() => {}); }
- }, [playing]);
+ }, [playing, currentTrack, sendToTv]);
+
+ // A television chosen, or let go, in the middle of a track: the track goes
+ // on from where it was, on the other side.
+ useEffect(() => {
+ const was = remoteRef.current;
+ remoteRef.current = remote;
+ if (was === remote || !currentTrack) return;
+ const audio = audioRef.current;
+ if (remote) {
+ const at = audio ? audio.currentTime : 0;
+ const wasPlaying = playing;
+ if (audio) audio.pause();
+ // Still loading: the load effect sends it once it has it.
+ if (!blobCacheRef.current.has(currentTrack.id)) return;
+ sendToTv(currentTrack, at)
+ .then(async () => {
+ if (!wasPlaying) await castBridge.chromecastPause().catch(() => {});
+ setPlaying(wasPlaying);
+ })
+ .catch((e) => { setError(e.message || String(e)); setPlaying(false); });
+ } else {
+ const at = remotePosRef.current;
+ remoteTrackRef.current = null;
+ if (audio && at != null && isFinite(at)) audio.currentTime = at;
+ if (playing && audio) audio.play().catch(() => setPlaying(false));
+ }
+ // eslint-disable-next-line
+ }, [remote]);
+
+ // The television's clock and state, once a second, for as long as the
+ // track there is ours.
+ useEffect(() => {
+ if (!remote) return undefined;
+ let stopped = false;
+ const tick = async () => {
+ if (sendingRef.current || castSession.owner !== 'music' || !remoteTrackRef.current) return;
+ const s = await castBridge.status().catch(() => null);
+ const c = s && s.chromecast;
+ if (stopped || sendingRef.current || castSession.owner !== 'music' || !c) return;
+ if (!c.connected) { setPlaying(false); return; }
+ if (c.position != null) { remotePosRef.current = c.position; setCurrentTime(c.position); }
+ const state = c.playerState;
+ if (state === 'PLAYING' || state === 'BUFFERING') setPlaying(true);
+ else if (state === 'PAUSED') setPlaying(false);
+ else if (state === 'IDLE' && String(c.idleReason || '').toLowerCase() === 'finished'
+ && finishedRef.current !== remoteTrackRef.current) {
+ finishedRef.current = remoteTrackRef.current;
+ onEndedRef.current();
+ }
+ };
+ const id = setInterval(tick, 1000);
+ tick();
+ // A film or a photo took the television: the track there has stopped.
+ // Not when casting stops altogether — the track then carries on here.
+ const unsubscribe = castSession.subscribe(() => {
+ if (castSession.device && castSession.owner !== 'music') setPlaying(false);
+ });
+ return () => { stopped = true; clearInterval(id); unsubscribe(); };
+ }, [remote]);
// Reshuffling keeps the currently playing track in place — turning shuffle
// on mid-album must not interrupt what's already playing. Which track that
@@ -624,19 +798,36 @@ function MusicPlayerBar({ getConnection, queue, onClose, userPrefs, onSaveQueue
}, []);
const seek = useCallback((e) => {
+ const to = parseFloat(e.target.value);
+ if (remoteRef.current) {
+ if (castSession.owner !== 'music') return;
+ castBridge.chromecastSeek(to).catch(() => {});
+ remotePosRef.current = to;
+ setCurrentTime(to);
+ return;
+ }
const audio = audioRef.current;
- if (audio && isFinite(audio.duration)) audio.currentTime = parseFloat(e.target.value);
+ if (audio && isFinite(audio.duration)) audio.currentTime = to;
}, []);
const handleClose = useCallback(() => {
const audio = audioRef.current;
if (audio) audio.pause();
+ // The television stops with the bar; it stays chosen for what comes next.
+ if (remoteRef.current && castSession.owner === 'music') {
+ castSession.claim(null);
+ castBridge.chromecastDisconnect().catch(() => {});
+ castBridge.stop().catch(() => {});
+ }
if (onClose) onClose();
}, [onClose]);
if (!currentTrack) return null;
const title = currentTrack.display_title || currentTrack.name;
+ // The element's, once it has read the file; the index's until then — and
+ // while casting, a file this device cannot decode still has a length.
+ const shownDuration = (isFinite(duration) && duration > 0) ? duration : (currentTrack.duration || 0);
const repeatLabel = repeat === 'off' ? t('music.player_repeat_off')
: repeat === 'all' ? t('music.player_repeat_all') : t('music.player_repeat_one');
@@ -678,17 +869,18 @@ function MusicPlayerBar({ getConnection, queue, onClose, userPrefs, onSaveQueue
</div>
<div class="music-player-seek">
<span class="music-player-time">${formatTime(currentTime)}</span>
- <input type="range" min="0" max=${duration || 0} step="1" value=${currentTime}
- disabled=${!duration} onInput=${seek} />
- <span class="music-player-time">${formatTime(duration)}</span>
+ <input type="range" min="0" max=${shownDuration} step="1" value=${currentTime}
+ disabled=${!shownDuration} onInput=${seek} />
+ <span class="music-player-time">${formatTime(shownDuration)}</span>
</div>
<div class="music-player-volume">
<${Icon} name="volume" />
<input type="range" min="0" max="1" step="0.05" value=${volume}
- title=${t('music.player_volume')}
+ title=${t('music.player_volume')} disabled=${remote}
onInput=${(e) => setVolume(parseFloat(e.target.value))} />
</div>
<div class="music-player-extra">
+ <${CastButton} cls="music-player-btn" up />
<button class="music-player-btn" title=${t('music.player_queue')} onClick=${() => setShowQueue(true)}>
<${Icon} name="menu" />
</button>
diff --git a/packages/meshbay-hub/src/meshbay_hub/static/platform.js b/packages/meshbay-hub/src/meshbay_hub/static/platform.js
index 3a0c628..28817c7 100644
--- a/packages/meshbay-hub/src/meshbay_hub/static/platform.js
+++ b/packages/meshbay-hub/src/meshbay_hub/static/platform.js
@@ -460,14 +460,16 @@ export const cast = {
if (!bridge || !bridge.cast || !bridge.cast.subtitle) return null;
return bridge.cast.subtitle(sub);
},
- // A photo for the receiver: `{ bytes, type }`, already scaled to the
- // television. Absent from an older shell, so callers check `canShowImages`.
- get canShowImages() {
- return Boolean(bridge && bridge.cast && bridge.cast.image);
+ // A whole file for the receiver — a photo already scaled to the television,
+ // a music track — as `{ bytes, type, cover?: { bytes, type }, title?,
+ // artist?, album? }`; answers `{ url }` to load. Absent from an older
+ // shell, so callers check `canSendFiles`.
+ get canSendFiles() {
+ return Boolean(bridge && bridge.cast && bridge.cast.file);
},
- async image(img) {
- if (!this.canShowImages) return null;
- return bridge.cast.image(img);
+ async file(f) {
+ if (!this.canSendFiles) return null;
+ return bridge.cast.file(f);
},
async finish() {
if (!bridge || !bridge.cast) return false;
@@ -529,6 +531,15 @@ export const cast = {
if (!this.canControl) return null;
return bridge.cast.chromecastPlay();
},
+ // Seeking inside a file the receiver plays (a track); a film seeks by
+ // restarting the relay instead.
+ get canSeek() {
+ return Boolean(bridge && bridge.cast && bridge.cast.chromecastSeek);
+ },
+ async chromecastSeek(seconds) {
+ if (!this.canSeek) return null;
+ return bridge.cast.chromecastSeek(seconds);
+ },
};
/**
diff --git a/packages/meshbay-hub/src/meshbay_hub/static/style.css b/packages/meshbay-hub/src/meshbay_hub/static/style.css
index 9be414f..9383043 100644
--- a/packages/meshbay-hub/src/meshbay_hub/static/style.css
+++ b/packages/meshbay-hub/src/meshbay_hub/static/style.css
@@ -2054,6 +2054,8 @@ button:disabled { opacity: 0.5; cursor: not-allowed; }
any other menu there, hung under the button rather than at a point. */
.cast-btn.casting { color: var(--accent); border-color: var(--accent); }
.ctx-menu.cast-menu { position: absolute; top: calc(100% + 4px); right: 0; z-index: 150; }
+/* From the music bar, at the bottom of the window: the menu opens upward. */
+.cast-menu-up { top: auto !important; bottom: calc(100% + 4px); }
.ctx-menu-item.cast-menu-current { color: var(--text-dim); cursor: default; }
.ctx-menu-item.cast-menu-current:hover { background: none; }
.ctx-menu-item svg { width: 16px; height: 16px; flex-shrink: 0; }
diff --git a/packages/meshbay-hub/src/meshbay_hub/static/video-player.js b/packages/meshbay-hub/src/meshbay_hub/static/video-player.js
index ed1f18c..37ea727 100644
--- a/packages/meshbay-hub/src/meshbay_hub/static/video-player.js
+++ b/packages/meshbay-hub/src/meshbay_hub/static/video-player.js
@@ -1316,6 +1316,7 @@ function VideoPlayer({ entry, transportRef, gekRef, onClose, onDownload }) {
}).then(async (result) => {
if (castRestartGenRef.current !== gen) return;
if (!result) return;
+ if (device || castSession.device) castSession.claim('video');
setCastUrl(result.url);
const status = await platform.cast.status();
if (status && status.chromecast && status.chromecast.connected) {
diff --git a/packages/meshbay-hub/tests/harness/cast_session_probe.py b/packages/meshbay-hub/tests/harness/cast_session_probe.py
index 2904860..573dd95 100644
--- a/packages/meshbay-hub/tests/harness/cast_session_probe.py
+++ b/packages/meshbay-hub/tests/harness/cast_session_probe.py
@@ -40,9 +40,13 @@ FRAME = r"""<!doctype html><html><head><meta charset=utf-8>
<script>
// A module that fails to load never reaches its own catch: say so.
const report = (error) => parent.postMessage({ error }, '*');
-addEventListener('error', (e) => report(
- `${e.message || 'load error'} ${e.filename || (e.target && e.target.src) || ''}`
- + `:${e.lineno || ''}`), true);
+// Scripts only: the music case's stub track is three bytes no <audio>
+// element can decode, which is expected and is not the page failing.
+addEventListener('error', (e) => {
+ if (!(e instanceof ErrorEvent) && !(e.target && e.target.tagName === 'SCRIPT')) return;
+ report(`${e.message || 'load error'} ${e.filename || (e.target && e.target.src) || ''}`
+ + `:${e.lineno || ''}`);
+}, true);
addEventListener('unhandledrejection',
(e) => report(String((e.reason && e.reason.stack) || e.reason)));
const WITH_BRIDGE = !location.search.includes('nobridge');
@@ -111,6 +115,8 @@ window.MeshBayTransport = function () {
// The native side, as the Electron preload and the Android shim present it,
// recording every cast call.
window.CALLS = [];
+// What the stub receiver reports: the music case moves it along.
+window.TV = { state: 'IDLE', position: 0, idle: null };
if (WITH_BRIDGE) {
let connected = false, v = 0;
const rec = (name, arg) => { CALLS.push({ name, arg }); };
@@ -127,13 +133,19 @@ if (WITH_BRIDGE) {
stop: async () => { rec('stop'); return true; },
subtitle: async () => null,
finish: async () => true,
- image: async (img) => {
- const bmp = await createImageBitmap(new Blob([img.bytes], { type: img.type }));
- rec('image', { type: img.type, width: bmp.width, height: bmp.height,
- jpeg: img.bytes[0] === 0xff && img.bytes[1] === 0xd8 });
- return { url: 'http://tv/image?t=x&v=' + (++v) };
+ file: async (f) => {
+ let info = { type: f.type, size: f.bytes.length, title: f.title || null,
+ artist: f.artist || null, album: f.album || null, cover: !!f.cover };
+ if (f.type.startsWith('image/')) {
+ const bmp = await createImageBitmap(new Blob([f.bytes], { type: f.type }));
+ info = { ...info, width: bmp.width, height: bmp.height,
+ jpeg: f.bytes[0] === 0xff && f.bytes[1] === 0xd8 };
+ }
+ rec('file', info);
+ return { url: 'http://tv/file?t=x&v=' + (++v) };
},
- status: async () => ({ active: true, url: null, chromecast: { connected } }),
+ status: async () => ({ active: true, url: null, chromecast: {
+ connected, playerState: TV.state, position: TV.position, idleReason: TV.idle } }),
scan: async () => { rec('scan'); return true; },
devices: async () => ({ devices: [{ id: 'tv1', name: 'Salon' }], scanning: false }),
chromecastConnect: async (o) => {
@@ -142,8 +154,9 @@ if (WITH_BRIDGE) {
},
chromecastReload: async (o) => { rec('reload', o); return { playerState: 'IDLE' }; },
chromecastDisconnect: async () => { rec('disconnect'); connected = false; return true; },
- chromecastPause: async () => ({}),
- chromecastPlay: async () => ({}),
+ chromecastPause: async () => { rec('pause'); TV.state = 'PAUSED'; return {}; },
+ chromecastPlay: async () => { rec('play'); TV.state = 'PLAYING'; return {}; },
+ chromecastSeek: async (t) => { rec('seek', t); TV.position = t; return {}; },
},
};
}
@@ -161,12 +174,91 @@ async function until(pred, what, tries = 200) {
throw new Error('timed out waiting for ' + what);
}
const calls = (name) => CALLS.filter((c) => c.name === name);
+const files = () => calls('file').map((c) => c.arg);
// Files is always first; then the enabled apps in their registry order.
const videosTab = () => $$('.group-tab')[1];
const count = () => (($('.photo-lightbox-count') || {}).textContent || '').trim();
+// The music bar, with a television already chosen: two tracks of one album.
+async function music() {
+ const { MusicPlayerBar } = await import('/music-player.js');
+ const { castSession } = await import('/cast-session.js');
+ const out = {};
+ castSession.choose({ id: 'tv1', name: 'Salon' });
+ // Which hashes were fetched, so a cover can be traced to where it came from.
+ window.FETCHED = [];
+ const transport = { connected: true, async waitForReconnect() {},
+ async fetchChunk(id) {
+ FETCHED.push(id);
+ return { nonce: 'n', ct: new Uint8Array([1, 2, 3]) };
+ },
+ // The node's MusicBrainz answer: a cover for the other album only.
+ async fetchMusicMeta(id) {
+ CALLS.push({ name: 'musicMeta', arg: id });
+ return id === 't3' ? { confidence: 1, cover_thumb_hash: 'mb-cover' }
+ : { confidence: 0 };
+ } };
+ const getConnection = async () => ({ transport, gek: 'gek' });
+ // One has no cover of its own but Two, of the same album, has; Three is of
+ // another album whose only cover is the one the node found.
+ const track = (n, title, album, hash) => ({ id: 't' + n, name: title + '.mp3', size: 3,
+ groupId: 'g1', display_title: title, artist: 'Some Band', album, duration: 200,
+ thumb_hash: hash });
+ const queue = { tracks: [track(1, 'One', 'An Album', null), track(2, 'Two', 'An Album', 'cover2'),
+ track(3, 'Three', 'Another Album', null)], startIndex: 0, nonce: 1 };
+ render(html`<${MusicPlayerBar} getConnection=${getConnection} queue=${queue}
+ userPrefs=${{}} onClose=${() => {}} />`, document.getElementById('root'));
+
+ await until(() => calls('connect').length === 1, 'the first track on the television');
+ out.first = { file: files()[0], connect: calls('connect')[0].arg,
+ coverFetched: FETCHED.includes('cover2') };
+ TV.state = 'PLAYING'; TV.position = 12;
+ await until(() => ($$('.music-player-time')[0] || {}).textContent === '0:12',
+ "the television's clock in the bar");
+ out.barButton = !!$('.music-player-bar .cast-btn.casting');
+ out.volumeDisabled = $('.music-player-volume input').disabled;
+
+ $('.music-player-play').click();
+ await until(() => calls('pause').length === 1, 'pause on the television');
+ const slider = $('.music-player-seek input');
+ slider.value = '30';
+ slider.dispatchEvent(new Event('input', { bubbles: true }));
+ await until(() => calls('seek').length === 1, 'a seek on the television');
+ out.seek = calls('seek')[0].arg;
+
+ // The television finishes the track: the next one goes, once.
+ TV.state = 'IDLE'; TV.idle = 'FINISHED';
+ await until(() => calls('reload').length === 1, 'the next track on the television');
+ out.second = { file: files()[1], reload: calls('reload')[0].arg,
+ title: $('.music-player-title').textContent.trim() };
+ TV.state = 'PLAYING'; TV.idle = null; TV.position = 1;
+ await sleep(2500);
+ out.filesAfterSettling = files().length;
+
+ // Three: the cover comes from the node's answer for its album.
+ TV.state = 'IDLE'; TV.idle = 'FINISHED';
+ await until(() => calls('reload').length === 2, 'the third track on the television');
+ out.third = { file: files()[2], mbCover: FETCHED.includes('mb-cover'),
+ metaAsked: calls('musicMeta').map((c) => c.arg) };
+ TV.state = 'PLAYING'; TV.idle = null;
+
+ // Stopping the cast lets the television go.
+ await castSession.end();
+ await sleep(200);
+ out.afterStop = { disconnects: calls('disconnect').length,
+ barCasting: !!$('.music-player-bar .cast-btn.casting') };
+ parent.postMessage(out, '*');
+}
+
(async () => {
const out = { bridge: !!window.meshbay };
+ if (location.search.includes('music')) {
+ try { await initLocale(); await music(); }
+ catch (err) {
+ parent.postMessage({ calls: CALLS, error: String((err && err.stack) || err) }, '*');
+ }
+ return;
+ }
try {
await initLocale();
render(html`<${GroupPage} groupId="g1" token="t" username="me" userId="u1"
@@ -195,13 +287,13 @@ const count = () => (($('.photo-lightbox-count') || {}).textContent || '').trim(
$$('.cast-menu .ctx-menu-item').find((b) => b.textContent.includes('Salon')).click();
await until(() => $('.photo-lightbox'), 'the lightbox to open on choosing');
await until(() => calls('connect').length === 1, 'the first photo to reach the television');
- out.first = { count: count(), image: calls('image')[0], connect: calls('connect')[0].arg,
+ out.first = { count: count(), image: files()[0], connect: calls('connect')[0].arg,
lightboxCasting: !!$('.photo-lightbox .cast-btn.casting') };
// Next photo: the receiver is connected, so it is reloaded.
$('.photo-nav-next').click();
await until(() => calls('reload').length === 1, 'the second photo');
- out.second = { count: count(), reload: calls('reload')[0].arg, images: calls('image').length };
+ out.second = { count: count(), reload: calls('reload')[0].arg, images: files().length };
// Slideshow: on by itself to the last photo, then off.
const play = $$('.photo-lightbox .video-top-bar .video-close')
@@ -209,7 +301,7 @@ const count = () => (($('.photo-lightbox-count') || {}).textContent || '').trim(
play.click();
await until(() => count() === '3 / 3', 'the slideshow to reach the last photo', 160);
await until(() => play.getAttribute('aria-pressed') === 'false', 'the slideshow to end', 160);
- out.slideshow = { count: count(), images: calls('image').length,
+ out.slideshow = { count: count(), images: files().length,
reloads: calls('reload').length };
// Close the lightbox; Videos shows the same television; a film opens as its remote.
@@ -341,16 +433,17 @@ def check(engine: str, cast: dict, plain: dict) -> list[str]:
first = cast["first"]
if first["count"] != "1 / 3":
bad.append(f"choosing a television did not open the first photo: {first['count']}")
- img = first["image"]["arg"]
+ img = first["image"]
if not (img["type"] == "image/jpeg" and img["jpeg"] and img["width"] == 1440
and img["height"] == 1080):
bad.append(f"the photo did not leave as a 1440x1080 JPEG: {img}")
- if first["connect"] != {"deviceId": "tv1", "mediaUrl": "http://tv/image?t=x&v=1"}:
+ if first["connect"] != {"deviceId": "tv1", "mediaUrl": "http://tv/file?t=x&v=1",
+ "startAt": 0}:
bad.append(f"connect: {first['connect']}")
if not first["lightboxCasting"]:
bad.append("the lightbox's cast button does not say it is casting")
if cast["second"]["count"] != "2 / 3" or cast["second"]["reload"] != {
- "mediaUrl": "http://tv/image?t=x&v=2"}:
+ "mediaUrl": "http://tv/file?t=x&v=2", "startAt": 0}:
bad.append(f"next photo: {cast['second']}")
if cast["slideshow"]["count"] != "3 / 3" or cast["slideshow"]["images"] != 3:
bad.append(f"slideshow: {cast['slideshow']}")
@@ -367,6 +460,39 @@ def check(engine: str, cast: dict, plain: dict) -> list[str]:
return [f"{engine}: {b}" for b in bad]
+def check_music(engine: str, m: dict) -> list[str]:
+ if "error" in m:
+ return [f"{engine}: music: {m['error']}"]
+ bad = []
+ f = m["first"]["file"]
+ if not (f["type"] == "audio/mpeg" and f["title"] == "One" and f["artist"] == "Some Band"
+ and f["album"] == "An Album" and f["cover"] and m["first"]["coverFetched"]):
+ bad.append(f"the first track left without its album's cover: {m['first']}")
+ third = m["third"]
+ if not (third["file"]["title"] == "Three" and third["file"]["cover"] and third["mbCover"]):
+ bad.append(f"the node's cover did not reach the television: {third}")
+ if "t1" in third["metaAsked"] or "t2" in third["metaAsked"]:
+ bad.append(f"the node was asked for a cover a track already had: {third['metaAsked']}")
+ if m["first"]["connect"] != {"deviceId": "tv1", "mediaUrl": "http://tv/file?t=x&v=1",
+ "startAt": 0}:
+ bad.append(f"connect: {m['first']['connect']}")
+ if not m["barButton"]:
+ bad.append("the music bar's cast button does not say it is casting")
+ if not m["volumeDisabled"]:
+ bad.append("the volume slider still acts while casting")
+ if m["seek"] != 30:
+ bad.append(f"seek: {m['seek']}")
+ s = m["second"]
+ if s["file"]["title"] != "Two" or s["title"] != "Two" or s["reload"] != {
+ "mediaUrl": "http://tv/file?t=x&v=2", "startAt": 0}:
+ bad.append(f"the end of a track did not move the queue on: {s}")
+ if m["filesAfterSettling"] != 2:
+ bad.append(f"a finished track moved the queue on more than once: {m['filesAfterSettling']}")
+ if m["afterStop"]["disconnects"] < 1 or m["afterStop"]["barCasting"]:
+ bad.append(f"stopping: {m['afterStop']}")
+ return [f"{engine}: music: {b}" for b in bad]
+
+
def main() -> int:
failures = []
with socketserver.ThreadingTCPServer(("127.0.0.1", PORT), H) as srv:
@@ -377,11 +503,13 @@ def main() -> int:
continue
cast = browse(engine, "")
plain = browse(engine, "?nobridge")
- if cast is None or plain is None:
+ music = browse(engine, "?music")
+ if cast is None or plain is None or music is None:
failures.append(f"{engine}: the page never reported")
continue
- print(f"{engine}:", json.dumps(cast, indent=1))
+ print(f"{engine}:", json.dumps({"photos-videos": cast, "music": music}, indent=1))
failures += check(engine, cast, plain)
+ failures += check_music(engine, music)
for f in failures:
print("FAIL", f, file=sys.stderr)
if not failures:
diff --git a/packages/meshbay-hub/tests/test_android_cast.py b/packages/meshbay-hub/tests/test_android_cast.py
index b537df7..ebcdec4 100644
--- a/packages/meshbay-hub/tests/test_android_cast.py
+++ b/packages/meshbay-hub/tests/test_android_cast.py
@@ -103,12 +103,14 @@ def test_the_receiver_is_pointed_at_the_relay_and_nothing_else():
def test_relay_calls_keep_their_order():
"""The page does not await each push; on a pool they could overtake."""
channels = _read(CAST / "CastChannels.kt")
- assert '"cast:start", "cast:push", "cast:subtitle", "cast:image", "cast:finish", "cast:stop"' in channels
+ assert '"cast:start", "cast:push", "cast:subtitle",' in channels
+ assert ('"cast:file:begin", "cast:file:write", "cast:file:end", "cast:finish", "cast:stop"'
+ in channels)
bridge = _read(SRC / "bridge" / "Bridge.kt")
assert "Executors.newSingleThreadExecutor()" in bridge
assert "(if (ordered) serial else work).execute" in bridge
- assert "fun ordered(frame: BinaryFrame) = frame.channel == BinaryFrame.CAST_PUSH" in _read(
- SRC / "bridge" / "Channels.kt")
+ assert ("fun ordered(frame: BinaryFrame) = frame.channel == BinaryFrame.CAST_PUSH"
+ " || frame.channel == BinaryFrame.CAST_FILE") in _read(SRC / "bridge" / "Channels.kt")
def test_screen_off_survival_is_switched_on_only_while_casting_or_playing():
diff --git a/packages/meshbay-hub/tests/test_cast_files.py b/packages/meshbay-hub/tests/test_cast_files.py
new file mode 100644
index 0000000..8f51d80
--- /dev/null
+++ b/packages/meshbay-hub/tests/test_cast_files.py
@@ -0,0 +1,372 @@
+"""
+Whole files on a cast receiver — photos and music tracks — and the television
+a session casts to.
+
+A file takes a different road to the television than a film: no stream, one
+file at a time, handed to the relay in pieces (begin, writes, end), served at
+`/file` with its cover at `/cover`, and loaded by the default media receiver
+as a picture or as a music track. The page scales and re-encodes a photo
+first (cast-session.js); a track goes as it is stored.
+
+The first half runs the desktop relay itself, as test_cast_subtitles.py does:
+it is plain Node with no dependencies. The file is behind the same token as
+the stream, its address changes with every file — a receiver handed the same
+URL twice shows what it already has — ranges are honoured, because that is how
+a receiver seeks in a track, and only a picture or a sound is served as one.
+
+The second half holds the wiring: what the receiver loads is the shell's to
+say, never the page's; Android accepts no URL but the relay's own; a
+television chosen anywhere is the one a film opens on, with the player as its
+remote from the start; and the music bar drives the television once one is
+chosen.
+"""
+
+import json
+import re
+import shutil
+import subprocess
+from pathlib import Path
+
+import pytest
+
+PACKAGES = Path(__file__).resolve().parents[2]
+STATIC = PACKAGES / "meshbay-hub" / "src" / "meshbay_hub" / "static"
+DESKTOP = PACKAGES / "meshbay-client" / "src"
+RELAY = DESKTOP / "cast-relay.js"
+CHROMECAST = DESKTOP / "cast-chromecast.js"
+ANDROID_MAIN = PACKAGES / "meshbay-android" / "app" / "src" / "main"
+ANDROID_SRC = ANDROID_MAIN / "kotlin"
+CLIENT = ANDROID_SRC / "org" / "meshbay" / "client"
+CAST = CLIENT / "cast"
+SHIM = ANDROID_MAIN / "assets" / "bridge" / "meshbay-bridge.js"
+
+
+def _read(path: Path) -> str:
+ return path.read_text(encoding="utf-8")
+
+
+RELAY_SCRIPT = r"""
+const http = require('node:http');
+const CastRelay = require(process.argv[2]);
+const get = (url, headers = {}) => new Promise((resolve, reject) => {
+ http.get(url, { headers }, (res) => {
+ const parts = [];
+ res.on('data', (c) => parts.push(c));
+ res.on('end', () => resolve({ status: res.statusCode, headers: res.headers,
+ body: Buffer.concat(parts).toString('hex') }));
+ }).on('error', reject);
+});
+const refused = async (f) => { try { await f(); return false; } catch { return true; } };
+const send = async (relay, bytes, opts) => {
+ await relay.beginFile({ size: bytes.length, ...opts });
+ for (let at = 0; at < bytes.length; at += 3) relay.writeFile(bytes.subarray(at, at + 3));
+ return relay.endFile();
+};
+(async () => {
+ const relay = new CastRelay();
+ const out = {};
+ out.activeBefore = relay.active;
+ out.htmlRefused = await refused(() => relay.beginFile({ type: 'text/html', size: 3 }));
+ out.activeAfterRefusal = relay.active;
+
+ const jpeg = Buffer.from([0xff, 0xd8, 1, 2, 0xff, 0xd9]);
+ const first = await send(relay, jpeg, { type: 'image/jpeg' });
+ out.activeAfterPhoto = relay.active;
+ out.firstUrl = first.url;
+ out.photo = await get(first.url);
+ out.badToken = (await get(first.url.replace(/t=[0-9a-f]+/, 't=x'))).status;
+ out.photoMeta = relay.fileMeta;
+
+ // A track, its cover and what to show with it; read back whole and by range.
+ const track = Buffer.from([...Array(20).keys()]);
+ const second = await send(relay, track, {
+ type: 'audio/flac', title: 'A Song', artist: 'Some Band', album: 'An Album',
+ cover: { bytes: Buffer.from([0xff, 0xd8, 7, 0xff, 0xd9]), type: 'image/jpeg' } });
+ out.secondUrl = second.url;
+ out.trackType = relay.fileType;
+ out.trackMeta = relay.fileMeta;
+ out.whole = await get(second.url);
+ out.range = await get(second.url, { Range: 'bytes=5-9' });
+ out.tail = await get(second.url, { Range: 'bytes=15-' });
+ out.beyond = (await get(second.url, { Range: 'bytes=50-60' })).status;
+ out.cover = await get(relay.fileMeta.cover);
+
+ // Fewer bytes than announced is not a file; more is refused as they come.
+ await relay.beginFile({ type: 'audio/mpeg', size: 10 });
+ relay.writeFile(Buffer.from([1, 2, 3]));
+ out.shortRefused = await refused(() => relay.endFile());
+ await relay.beginFile({ type: 'audio/mpeg', size: 2 });
+ out.longRefused = await refused(() => relay.writeFile(Buffer.from([1, 2, 3])));
+ out.stillServed = (await get(second.url)).status;
+
+ await relay.start({});
+ out.fileAfterFilm = relay.fileUrl;
+ await send(relay, jpeg, { type: 'image/jpeg' });
+ await relay.stop();
+ out.fileAfterStop = relay.fileUrl;
+ console.log(JSON.stringify(out));
+})().catch((e) => { console.error(e); process.exit(1); });
+"""
+
+
+@pytest.fixture(scope="module")
+def relay_run(tmp_path_factory):
+ if shutil.which("node") is None or not RELAY.exists():
+ pytest.skip("node or the desktop client sources are not available")
+ script = tmp_path_factory.mktemp("cf") / "relay.cjs"
+ script.write_text(RELAY_SCRIPT, encoding="utf-8")
+ proc = subprocess.run(["node", str(script), str(RELAY)],
+ capture_output=True, text=True, encoding="utf-8", timeout=30)
+ assert proc.returncode == 0, proc.stderr
+ return json.loads(proc.stdout)
+
+
+def test_a_file_starts_the_relay_and_is_served_behind_its_token(relay_run):
+ assert relay_run["activeBefore"] is False
+ assert relay_run["activeAfterPhoto"] is True
+ photo = relay_run["photo"]
+ assert photo["status"] == 200
+ assert photo["headers"]["content-type"] == "image/jpeg"
+ assert photo["headers"]["cache-control"] == "no-store"
+ assert photo["body"] == "ffd80102ffd9"
+ assert relay_run["badToken"] == 403
+ assert relay_run["photoMeta"]["cover"] is None
+
+
+def test_every_file_has_its_own_address(relay_run):
+ assert re.search(r"/file\?t=[0-9a-f]{32}&v=\d+$", relay_run["firstUrl"])
+ assert relay_run["secondUrl"] != relay_run["firstUrl"]
+
+
+def test_a_track_is_served_with_its_cover_and_what_it_is(relay_run):
+ assert relay_run["trackType"] == "audio/flac"
+ meta = relay_run["trackMeta"]
+ assert (meta["title"], meta["artist"], meta["album"]) == ("A Song", "Some Band", "An Album")
+ assert re.search(r"/cover\?t=[0-9a-f]{32}&v=\d+$", meta["cover"])
+ assert relay_run["cover"]["status"] == 200
+ assert relay_run["cover"]["body"] == "ffd807ffd9"
+ assert relay_run["whole"]["body"] == bytes(range(20)).hex()
+ assert relay_run["whole"]["headers"]["accept-ranges"] == "bytes"
+
+
+def test_a_receiver_seeks_in_a_track_by_range(relay_run):
+ r = relay_run["range"]
+ assert r["status"] == 206
+ assert r["headers"]["content-range"] == "bytes 5-9/20"
+ assert r["body"] == bytes(range(5, 10)).hex()
+ tail = relay_run["tail"]
+ assert tail["status"] == 206 and tail["body"] == bytes(range(15, 20)).hex()
+ assert relay_run["beyond"] == 416
+
+
+def test_only_a_whole_picture_or_sound_is_served(relay_run):
+ assert relay_run["htmlRefused"] is True
+ assert relay_run["activeAfterRefusal"] is False, "a refused file starts nothing"
+ assert relay_run["shortRefused"] is True
+ assert relay_run["longRefused"] is True
+ assert relay_run["stillServed"] == 200, "a failed send leaves the shown file alone"
+
+
+def test_a_film_or_a_stop_takes_the_file_down(relay_run):
+ assert relay_run["fileAfterFilm"] is None
+ assert relay_run["fileAfterStop"] is None
+
+
+def test_the_receiver_loads_a_photo_as_a_picture_and_a_track_as_music():
+ if shutil.which("node") is None or not CHROMECAST.exists():
+ pytest.skip("node or the desktop client sources are not available")
+ script = (
+ "const Module = require('node:module');"
+ "const real = Module._load;"
+ "Module._load = function (r, ...a) {"
+ " if (r === 'bonjour-service') return { Bonjour: class {} };"
+ " if (r === 'castv2-client') return { Client: class {}, DefaultMediaReceiver: {} };"
+ " return real.call(this, r, ...a); };"
+ f"const {{ mediaFor, loadOptionsFor }} = require({json.dumps(str(CHROMECAST))});"
+ "const meta = { title: 'A Song', artist: 'Some Band', album: 'An Album',"
+ " cover: 'http://x/cover?t=a&v=2' };"
+ "console.log(JSON.stringify({"
+ " photo: mediaFor('http://x/file?t=a&v=1', null,"
+ " { contentType: 'image/jpeg', meta: { cover: null } }),"
+ " track: mediaFor('http://x/file?t=a&v=2', null, { contentType: 'audio/flac', meta }),"
+ " from: loadOptionsFor(null, 42.5),"
+ " film: mediaFor('http://x/stream.mp4?t=a', null, null),"
+ " filmOptions: loadOptionsFor(null, 0),"
+ " legacy: mediaFor('http://x/stream.mp4?t=a', null) }));")
+ proc = subprocess.run(["node", "-e", script], capture_output=True, text=True,
+ encoding="utf-8", timeout=30)
+ assert proc.returncode == 0, proc.stderr
+ out = json.loads(proc.stdout)
+ assert out["photo"]["contentType"] == "image/jpeg"
+ assert out["photo"]["streamType"] == "NONE"
+ assert "tracks" not in out["photo"]
+ track = out["track"]
+ assert track["contentType"] == "audio/flac" and track["streamType"] == "BUFFERED"
+ assert track["metadata"] == {"metadataType": 3, "title": "A Song", "artist": "Some Band",
+ "albumName": "An Album",
+ "images": [{"url": "http://x/cover?t=a&v=2"}]}
+ assert out["from"]["currentTime"] == 42.5
+ assert "currentTime" not in out["filmOptions"]
+ assert out["film"] == out["legacy"], "a film is loaded exactly as before"
+ assert out["film"]["streamType"] == "LIVE" and out["film"]["contentType"] == "video/mp4"
+
+
+def test_the_shell_not_the_page_says_what_the_receiver_loads():
+ """The page names a URL; what it is, is the relay's to say. A page that
+ could name the content type could have a television load the stream as
+ anything at all."""
+ main = _read(DESKTOP / "main.js")
+ load_of = main.split("const loadOf = ", 1)[1].split("\n\n", 1)[0]
+ assert "mediaUrl === castRelay.fileUrl" in load_of
+ assert "contentType: castRelay.fileType, meta: castRelay.fileMeta" in load_of
+ assert "subtitle: null" in load_of, "a file carries no subtitle"
+ assert main.count("loadOf(mediaUrl, subtitle)") == 2, "connect and reload both"
+ # A start position means something for a file only; a film starts where
+ # its relay does.
+ assert main.count("l.file ? startAt : 0") == 2
+
+
+def test_android_accepts_the_relays_file_and_nothing_else():
+ if not CAST.exists():
+ pytest.skip("android sources not present")
+ channels = _read(CAST / "CastChannels.kt")
+ check = channels.split("private fun relayUrl(", 1)[1].split("\n }", 1)[0]
+ assert "asked != ours && asked != relay.fileUrl" in check
+ assert 'put("contentType", relay.fileType).put("meta", relay.fileMeta()' in channels
+ assert "url == relay.fileUrl -> null" in channels, "a file carries no subtitle"
+ assert 'if (url == relay.fileUrl) maxOf(0.0, o.optDouble("startAt", 0.0)) else 0.0' in channels
+ # A file is not a cast that has to survive the screen going off.
+ begin = channels.split('"cast:file:begin" ->', 1)[1].split('"cast:file:end"', 1)[0]
+ assert "onCasting" not in begin
+ relay = _read(CAST / "CastRelay.kt")
+ assert '"/file" -> serveFile(out, socket, range)' in relay
+ assert '"/cover" -> serveCover(out, socket)' in relay
+ serve = relay.split("private fun serve(", 1)[1]
+ assert serve.index('query["t"] != token') < serve.index('"/file"'), "behind the token"
+ # On disk, never held: a long lossless track is a few hundred megabytes.
+ assert 'File(spoolDir, "file-${fileSeq.incrementAndGet()}.bin")' in relay
+ assert "java.io.FileOutputStream(slot.path)" in relay
+ control = _read(CAST / "CastControl.kt")
+ assert "MediaInfo.STREAM_TYPE_NONE" in control
+ assert "MediaMetadata.MEDIA_TYPE_PHOTO" in control
+ assert "MediaMetadata.MEDIA_TYPE_MUSIC_TRACK" in control
+ assert "MediaInfo.STREAM_TYPE_BUFFERED" in control
+ assert "setCurrentTime((startAt * 1000).toLong())" in control
+ assert "MediaSeekOptions.Builder().setPosition(" in control
+ # Loaded is shown: a photo never reaches PLAYING, and waiting for it
+ # would hold every photo for the full fifteen seconds.
+ load = control.split("private fun load(", 1)[1]
+ assert load.index("if (photo) return") < load.index("PLAY_WAIT_MS")
+
+
+def test_a_file_crosses_the_android_bridge_in_binary_pieces():
+ """One message the size of a lossless track would be the WebView's to
+ refuse; a file goes in pieces of a megabyte, as binary frames where the
+ WebView has them."""
+ if not SHIM.exists():
+ pytest.skip("android sources not present")
+ shim = _read(SHIM)
+ assert "const CAST_FILE = 3;" in shim
+ assert "const FILE_PIECE = 1024 * 1024;" in shim
+ assert ("BINARY ? sendBinary(CAST_FILE, 0, piece) : call('cast:file:write', base64Of(piece))"
+ in shim)
+ assert "const val CAST_FILE = 3" in _read(CLIENT / "save" / "SaveNames.kt")
+ channels = _read(CLIENT / "bridge" / "Channels.kt")
+ assert "BinaryFrame.CAST_FILE -> " in channels and ".writeFile(frame.bytes" in channels
+
+
+def test_a_television_chosen_anywhere_takes_the_next_film():
+ """The player opens as the remote of a television chosen before it, the
+ cast pending until the first segment exactly as a restart is; choosing one
+ in the player keeps it for the session, and stopping there forgets it."""
+ player = _read(STATIC / "video-player.js")
+ assert ("const [autoCast] = useState(() => "
+ "(castSession.supported ? castSession.device : null));") in player
+ assert "useState(Boolean(autoCast))" in player
+ assert "castActiveRef = useRef(Boolean(autoCast))" in player
+ assert "castRestartPendingRef = useRef(Boolean(autoCast))" in player
+ assert "castDeviceRef = useRef(autoCast)" in player
+ assert "useState(autoCast ? autoCast.name : null)" in player
+ stop = player.split("const stopCast = async () => {", 1)[1].split("};", 1)[0]
+ assert "castSession.forget()" in stop
+ assert "castSession.choose(d);" in player
+ assert "castSession.claim('video')" in player
+
+
+def test_the_cast_button_is_where_the_media_are():
+ """Videos' toolbar, the Photos landing, a photo album's bar and Music's
+ toolbar are drawn by the group page and the search page alike, so one
+ button in each covers both. On the Photos landing the button keeps a
+ toolbar Search would otherwise hide. The music bar has its own, opening
+ upward from the bottom of the window."""
+ videos = _read(STATIC / "video-app.js")
+ assert "<${CastButton} />" in videos.split('<div class="video-toolbar">', 1)[1]
+ photos = _read(STATIC / "photos-app.js")
+ landing = photos.split("function PhotosApp(", 1)[1]
+ assert "(!hideFilter || castSession.supported) && html`" in landing
+ assert "<${CastButton} />" in landing.split('<div class="photo-toolbar">', 1)[1]
+ album = photos.split("function AlbumView(", 1)[1]
+ assert "<${CastButton}" in album
+ lightbox = photos.split("function Lightbox(", 1)[1].split("function AlbumView(", 1)[0]
+ assert '<${CastButton} variant="overlay"' in lightbox
+ assert "if (castDevice && blobUrl && blob) showPhoto(blob);" in lightbox
+ music = _read(STATIC / "music-app.js")
+ assert "<${CastButton} />" in music.split('<div class="video-toolbar">', 1)[1]
+ bar = _read(STATIC / "music-player.js")
+ assert '<${CastButton} cls="music-player-btn" up />' in bar
+ # The group page draws them through the app registry, the search page
+ # imports them: the same components either way.
+ registry = _read(STATIC / "apps.js")
+ search = _read(STATIC / "search-page.js")
+ for page in (registry, search):
+ for app in ("VideoApp", "PhotosApp", "MusicApp"):
+ assert app in page
+
+
+def test_the_music_bar_drives_the_television():
+ """With a television chosen, the decrypted track goes to the relay instead
+ of the element, and the bar's controls and clock are the receiver's. The
+ receiver keeps saying FINISHED until it is given something else, so the
+ end of a track moves the queue on once; and a television a film or a
+ photo has taken is not the bar's to read."""
+ bar = _read(STATIC / "music-player.js")
+ assert "const remote = Boolean(castDevice) && castSession.canSendFiles;" in bar
+ send = bar.split("const sendToTv = useCallback(", 1)[1].split("}, [fetchCover]);", 1)[0]
+ assert "owner: 'music', startAt," in send
+ assert "type: cached.mime, cover," in send
+ assert "sendingRef.current = true;" in send
+ load = bar.split("if (remoteRef.current) {\n // To the television", 1)[1]
+ assert "audio.pause();" in load.split("await audio.play();", 1)[0]
+ tick = bar.split("const tick = async () => {", 1)[1].split("const id = setInterval", 1)[0]
+ assert "castSession.owner !== 'music'" in tick
+ assert "sendingRef.current" in tick
+ assert "finishedRef.current !== remoteTrackRef.current" in tick
+ assert "String(c.idleReason || '').toLowerCase() === 'finished'" in tick
+ # The cover is found as the album card finds it, not only on the track.
+ cover = bar.split("const fetchCover = useCallback(", 1)[1]
+ cover = cover.split("}, [getConnection, tracks]);", 1)[0]
+ assert "entry.thumb_hash || (sibling && sibling.thumb_hash)" in cover
+ assert "transport.fetchMusicMeta(entry.id)" in cover
+ assert "meta.cover_thumb_hash" in cover
+ assert "castBridge.chromecastSeek(to)" in bar
+ assert "castBridge.chromecastPause() : castBridge.chromecastPlay()" in bar
+ # Stopping the cast carries the track on here; a film taking the
+ # television pauses the bar.
+ assert "if (castSession.device && castSession.owner !== 'music') setPlaying(false);" in bar
+
+
+def test_a_photo_leaves_scaled_and_upright():
+ session = _read(STATIC / "cast-session.js")
+ assert "imageOrientation: 'from-image'" in session
+ assert "const TV_WIDTH = 1920;" in session and "const TV_HEIGHT = 1080;" in session
+ assert "Math.min(1, TV_WIDTH / bitmap.width, TV_HEIGHT / bitmap.height)" in session
+ assert "await loadFile({ owner: 'photo', bytes, type: 'image/jpeg' });" in session
+ # Only the newest photo asked for is sent.
+ assert session.count("gen !== photoGen") >= 2
+
+
+def test_every_locale_names_the_slideshow():
+ for f in sorted((STATIC / "locales").glob("*.js")):
+ text = f.read_text(encoding="utf-8")
+ for key in ("photo.slideshow", "photo.slideshow_stop"):
+ assert f"'{key}':" in text, f"{f.name} lacks {key}"
diff --git a/packages/meshbay-hub/tests/test_cast_photos.py b/packages/meshbay-hub/tests/test_cast_photos.py
deleted file mode 100644
index 699e663..0000000
--- a/packages/meshbay-hub/tests/test_cast_photos.py
+++ /dev/null
@@ -1,240 +0,0 @@
-"""
-Photos on a cast receiver, and the television a session casts to.
-
-A photo takes a different road to the television than a film: no stream, one
-picture at a time, served by the relay at `/image` and loaded as a picture by
-the default media receiver. The page scales and re-encodes it first
-(cast-session.js), so what reaches the relay is a JPEG the receiver can show.
-
-The first half runs the desktop relay itself, as test_cast_subtitles.py does:
-it is plain Node with no dependencies. The photo is behind the same token as
-the stream, its address changes with every photo — a receiver handed the same
-URL twice shows what it already has — and only a picture is served as one.
-
-The second half holds the wiring: the receiver is told a photo is a photo by
-the shell, never by the page; Android accepts no URL but the relay's own; and
-a television chosen anywhere is the one a film opens on, with the player as
-its remote from the start.
-"""
-
-import json
-import re
-import shutil
-import subprocess
-from pathlib import Path
-
-import pytest
-
-PACKAGES = Path(__file__).resolve().parents[2]
-STATIC = PACKAGES / "meshbay-hub" / "src" / "meshbay_hub" / "static"
-DESKTOP = PACKAGES / "meshbay-client" / "src"
-RELAY = DESKTOP / "cast-relay.js"
-CHROMECAST = DESKTOP / "cast-chromecast.js"
-ANDROID_SRC = PACKAGES / "meshbay-android" / "app" / "src" / "main" / "kotlin"
-CAST = ANDROID_SRC / "org" / "meshbay" / "client" / "cast"
-
-
-def _read(path: Path) -> str:
- return path.read_text(encoding="utf-8")
-
-
-RELAY_SCRIPT = r"""
-const http = require('node:http');
-const CastRelay = require(process.argv[2]);
-const get = (url) => new Promise((resolve, reject) => {
- http.get(url, (res) => {
- const parts = [];
- res.on('data', (c) => parts.push(c));
- res.on('end', () => resolve({ status: res.statusCode, headers: res.headers,
- body: Buffer.concat(parts).toString('hex') }));
- }).on('error', reject);
-});
-(async () => {
- const relay = new CastRelay();
- const out = {};
- out.activeBefore = relay.active;
- try {
- await relay.showImage({ bytes: Buffer.from('<p>'), type: 'text/html' });
- out.htmlRefused = false;
- } catch { out.htmlRefused = true; }
- out.activeAfterRefusal = relay.active;
-
- const jpeg = Buffer.from([0xff, 0xd8, 1, 2, 0xff, 0xd9]);
- const first = await relay.showImage({ bytes: jpeg, type: 'image/jpeg' });
- out.activeAfterPhoto = relay.active;
- out.firstUrl = first.url;
- out.imageType = relay.imageType;
- out.photo = await get(first.url);
- out.badToken = (await get(first.url.replace(/t=[0-9a-f]+/, 't=x'))).status;
- const second = await relay.showImage({ bytes: Buffer.from([9]), type: 'image/jpeg' });
- out.secondUrl = second.url;
- out.second = await get(second.url);
-
- await relay.start({});
- out.imageAfterFilm = relay.imageUrl;
- await relay.stop();
- out.imageAfterStop = relay.imageUrl;
- console.log(JSON.stringify(out));
-})().catch((e) => { console.error(e); process.exit(1); });
-"""
-
-
-@pytest.fixture(scope="module")
-def relay_run(tmp_path_factory):
- if shutil.which("node") is None or not RELAY.exists():
- pytest.skip("node or the desktop client sources are not available")
- script = tmp_path_factory.mktemp("cp") / "relay.cjs"
- script.write_text(RELAY_SCRIPT, encoding="utf-8")
- proc = subprocess.run(["node", str(script), str(RELAY)],
- capture_output=True, text=True, encoding="utf-8", timeout=30)
- assert proc.returncode == 0, proc.stderr
- return json.loads(proc.stdout)
-
-
-def test_a_photo_starts_the_relay_and_is_served_behind_its_token(relay_run):
- assert relay_run["activeBefore"] is False
- assert relay_run["activeAfterPhoto"] is True
- photo = relay_run["photo"]
- assert photo["status"] == 200
- assert photo["headers"]["content-type"] == "image/jpeg"
- assert photo["headers"]["cache-control"] == "no-store"
- assert photo["body"] == "ffd80102ffd9"
- assert relay_run["badToken"] == 403
- assert relay_run["imageType"] == "image/jpeg"
-
-
-def test_every_photo_has_its_own_address(relay_run):
- assert re.search(r"/image\?t=[0-9a-f]{32}&v=\d+$", relay_run["firstUrl"])
- assert relay_run["secondUrl"] != relay_run["firstUrl"]
- assert relay_run["second"]["body"] == "09"
-
-
-def test_only_a_picture_is_served_as_one(relay_run):
- assert relay_run["htmlRefused"] is True
- assert relay_run["activeAfterRefusal"] is False, "a refused photo starts nothing"
-
-
-def test_a_film_or_a_stop_takes_the_photo_down(relay_run):
- assert relay_run["imageAfterFilm"] is None
- assert relay_run["imageAfterStop"] is None
-
-
-def test_the_receiver_loads_a_photo_as_a_picture():
- if shutil.which("node") is None or not CHROMECAST.exists():
- pytest.skip("node or the desktop client sources are not available")
- script = (
- "const Module = require('node:module');"
- "const real = Module._load;"
- "Module._load = function (r, ...a) {"
- " if (r === 'bonjour-service') return { Bonjour: class {} };"
- " if (r === 'castv2-client') return { Client: class {}, DefaultMediaReceiver: {} };"
- " return real.call(this, r, ...a); };"
- f"const {{ mediaFor }} = require({json.dumps(str(CHROMECAST))});"
- "console.log(JSON.stringify({"
- " photo: mediaFor('http://x/image?t=a&v=1', null, 'image/jpeg'),"
- " film: mediaFor('http://x/stream.mp4?t=a', null, 'video/mp4'),"
- " legacy: mediaFor('http://x/stream.mp4?t=a', null) }));")
- proc = subprocess.run(["node", "-e", script], capture_output=True, text=True,
- encoding="utf-8", timeout=30)
- assert proc.returncode == 0, proc.stderr
- out = json.loads(proc.stdout)
- assert out["photo"]["contentType"] == "image/jpeg"
- assert out["photo"]["streamType"] == "NONE"
- assert "tracks" not in out["photo"]
- assert out["film"] == out["legacy"], "a film is loaded exactly as before"
- assert out["film"]["streamType"] == "LIVE" and out["film"]["contentType"] == "video/mp4"
-
-
-def test_the_shell_not_the_page_says_what_the_receiver_loads():
- """The page names a URL; whether it is a photo is the relay's to say. A
- page that could name the content type could have a television load the
- stream URL as anything at all."""
- main = _read(DESKTOP / "main.js")
- load_of = main.split("const loadOf = ", 1)[1].split("\n\n", 1)[0]
- assert "mediaUrl === castRelay.imageUrl" in load_of
- assert "contentType: castRelay.imageType" in load_of
- assert "contentType: 'video/mp4'" in load_of
- assert "subtitle: null" in load_of, "a photo carries no subtitle"
- assert main.count("loadOf(mediaUrl, subtitle)") == 2, "connect and reload both"
-
-
-def test_android_accepts_the_relays_photo_and_nothing_else():
- if not CAST.exists():
- pytest.skip("android sources not present")
- channels = _read(CAST / "CastChannels.kt")
- check = channels.split("private fun relayUrl(", 1)[1].split("\n }", 1)[0]
- assert "asked != ours && asked != relay.imageUrl" in check
- assert "if (url == relay.imageUrl) relay.imageType" in channels
- assert "url == relay.imageUrl -> null" in channels, "a photo carries no subtitle"
- # A photo is not a cast that has to survive the screen going off.
- image = channels.split('"cast:image" ->', 1)[1].split('\n "', 1)[0]
- assert "onCasting" not in image
- relay = _read(CAST / "CastRelay.kt")
- assert '"/image" -> serveImage(out, socket)' in relay
- serve = relay.split("private fun serve(", 1)[1]
- assert serve.index('query["t"] != token') < serve.index('"/image"'), "behind the token"
- control = _read(CAST / "CastControl.kt")
- assert "MediaInfo.STREAM_TYPE_NONE" in control
- assert "MediaMetadata.MEDIA_TYPE_PHOTO" in control
- # Loaded is shown: a photo never reaches PLAYING, and waiting for it
- # would hold every photo for the full fifteen seconds.
- load = control.split("private fun load(", 1)[1]
- assert load.index("if (photo) return") < load.index("PLAY_WAIT_MS")
-
-
-def test_a_television_chosen_anywhere_takes_the_next_film():
- """The player opens as the remote of a television chosen before it, the
- cast pending until the first segment exactly as a restart is; choosing one
- in the player keeps it for the session, and stopping there forgets it."""
- player = _read(STATIC / "video-player.js")
- assert ("const [autoCast] = useState(() => "
- "(castSession.supported ? castSession.device : null));") in player
- assert "useState(Boolean(autoCast))" in player
- assert "castActiveRef = useRef(Boolean(autoCast))" in player
- assert "castRestartPendingRef = useRef(Boolean(autoCast))" in player
- assert "castDeviceRef = useRef(autoCast)" in player
- assert "useState(autoCast ? autoCast.name : null)" in player
- stop = player.split("const stopCast = async () => {", 1)[1].split("};", 1)[0]
- assert "castSession.forget()" in stop
- assert "castSession.choose(d);" in player
-
-
-def test_the_cast_button_is_where_the_media_are():
- """Videos' toolbar, the Photos landing and a photo album's bar are drawn by
- the group page and the search page alike, so one button in each covers
- both. On the landing the button keeps a toolbar Search would otherwise
- hide."""
- videos = _read(STATIC / "video-app.js")
- assert "<${CastButton} />" in videos.split('<div class="video-toolbar">', 1)[1]
- photos = _read(STATIC / "photos-app.js")
- landing = photos.split("function PhotosApp(", 1)[1]
- assert "(!hideFilter || castSession.supported) && html`" in landing
- assert "<${CastButton} />" in landing.split('<div class="photo-toolbar">', 1)[1]
- album = photos.split("function AlbumView(", 1)[1]
- assert "<${CastButton}" in album
- lightbox = photos.split("function Lightbox(", 1)[1].split("function AlbumView(", 1)[0]
- assert '<${CastButton} variant="overlay"' in lightbox
- assert "if (castDevice && blobUrl && blob) showPhoto(blob);" in lightbox
- # The group page draws them through the app registry, the search page
- # imports them: the same components either way.
- registry = _read(STATIC / "apps.js")
- search = _read(STATIC / "search-page.js")
- for page in (registry, search):
- assert "VideoApp" in page and "PhotosApp" in page
-
-
-def test_a_photo_leaves_scaled_and_upright():
- session = _read(STATIC / "cast-session.js")
- assert "imageOrientation: 'from-image'" in session
- assert "const TV_WIDTH = 1920;" in session and "const TV_HEIGHT = 1080;" in session
- assert "Math.min(1, TV_WIDTH / bitmap.width, TV_HEIGHT / bitmap.height)" in session
- assert "'image/jpeg'" in session
- # Only the newest photo asked for is sent.
- assert session.count("gen !== photoGen") >= 2
-
-
-def test_every_locale_names_the_slideshow():
- for f in sorted((STATIC / "locales").glob("*.js")):
- text = f.read_text(encoding="utf-8")
- for key in ("photo.slideshow", "photo.slideshow_stop"):
- assert f"'{key}':" in text, f"{f.name} lacks {key}"