Asked for directly: Emby's admin "now playing" dashboard should see
this app's streams the same way it sees a real web/mobile/TV client's.
This is exactly the follow-up "Sync watch-party progress back to Emby"
already anticipated and deliberately deferred at the time ("wiring
clean start/stop into every transition... is real complexity for a
secondary payoff") - that section also explains why this app abandoned
Emby's session-shaped PlayingItems endpoints entirely in favor of
direct UserData writes. Given that history (two separate rounds of a
wrong-but-schema-plausible request shape for that other, now-abandoned
endpoint family), this one was built checking the real API first
rather than assuming.
Confirmed against the real server's own /openapi.json that the actual
session-lifecycle family is POST /Sessions/Playing (start),
/Sessions/Playing/Progress, and /Sessions/Playing/Stopped - distinct
from PlayingItems. None of the three schemas declare a required list
at all, which - given the neighboring history - was read as "don't
trust that": ItemId/MediaSourceId/PlaySessionId/PositionTicks are sent
on every call anyway, matching what a real client obviously would,
rather than risking a repeat of "technically matches the schema, still
400s".
One real Emby session per PARTY MEMBER, not one per party - a faithful
copy of how a real client behaves, and the actual point of "let admin
see who's watching what" would be lost by collapsing everyone onto one
shared entry. New server/src/services/EmbySessionService.ts mirrors
PlaystateService's own best-effort shape (every failure caught and
logged, never thrown) but stays its own class - PlaystateService's own
opening comment is specifically about why it doesn't use a
session-shaped endpoint, so folding this into it would muddy that
exact distinction.
Getting a MediaSourceId/PlaySessionId per member needed a new
CatalogService.negotiatePlaybackSession - a fresh PlaybackInfo
negotiation under that specific member's own account, since Emby scopes
PlaybackInfo per user and PartyStreamService's existing shared
negotiation is never guaranteed valid for reporting under everyone
else's account too. Returns null on any failure rather than throwing -
this only ever feeds an admin-dashboard nicety, never something that
should disrupt real playback.
Driven entirely from index.ts's existing periodic sweep rather than new
hooks scattered across every queue-advance/track-skip call site -
that sweep already visits every connected member on every ~20s tick,
already positioned to notice a member's current item no longer matches
their cached session and react (stop the old one if any, negotiate
fresh ids, start the new one; same item as last tick just sends
Progress with the cached ids). PartyMember gained the nullable
embySession field this tracks. Explicit stop calls also happen at
PartyService.leave() (alongside the existing CastTokenService
.revokeForMember) and removeIdleParties (a backstop for a disconnect
that never triggered a clean leave).
Verified: full typecheck/test/build (134/134 server tests - new
coverage for negotiatePlaybackSession's null-on-any-failure contract,
every EmbySessionService method's exact request shape, that a non-ok
response/thrown error is swallowed not propagated, and that leave()
calls stop with a member's real cached session info but only when they
actually had one open). Not verified: an actual live write against the
real server - deliberately not attempted, since that would mean using
a real account's Emby token for a genuine state-changing call, beyond
the read-only diagnostic use this session's shared API key was ever
meant for. So while the request shape is checked against the real
schema and the per-member logic is unit-tested in full, whether Emby's
dashboard actually displays these sessions the way a native client's
would - correct device/client labeling in particular, since this app
never sends the X-Emby-Authorization Device/Client/Version header a
real client would, only X-Emby-Token - is genuinely unconfirmed. Worth
watching the real dashboard the first time a party runs after this ships.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
104 lines
5.1 KiB
TypeScript
104 lines
5.1 KiB
TypeScript
'use strict';
|
|
|
|
import { EmbyClient } from '../emby/EmbyClient';
|
|
import { Logger } from '../Logger';
|
|
|
|
/**
|
|
* Reports a party member's own playback to Emby's real Sessions API
|
|
* (`POST /Sessions/Playing`, `/Sessions/Playing/Progress`,
|
|
* `/Sessions/Playing/Stopped`) - the piece of session lifecycle
|
|
* docs/architecture.md's "Sync watch-party progress back to Emby"
|
|
* section deliberately deferred at the time ("wiring clean start/stop
|
|
* into every transition... is real complexity for a secondary payoff").
|
|
* Asked for directly since: without this, this app's own streams never
|
|
* showed up in Emby's admin "now playing" dashboard the way a real web/
|
|
* mobile/TV client's would - only the resume-position/watched-history
|
|
* UserData (PlaystateService) was ever visible anywhere, and only by
|
|
* opening the item itself, not from the dashboard's live session list.
|
|
*
|
|
* One real Emby session per PARTY MEMBER, not one per party - a
|
|
* faithful copy of how a real client behaves (every device/account gets
|
|
* its own), and the actual point of the request ("let admin see who's
|
|
* watching what") would be lost by collapsing every member onto one
|
|
* shared entry. Every call needs the member's own `embyToken` (the same
|
|
* source every other per-member Emby write in this app already uses -
|
|
* see `PartyMember.embyToken`/`PlaystateService`) - Emby's Sessions API
|
|
* is inherently per-account, there's no equivalent of "the party's" own
|
|
* session to report against instead.
|
|
*
|
|
* `MediaSourceId`/`PlaySessionId` (see `CatalogService
|
|
* .negotiatePlaybackSession`) are required in practice, not just
|
|
* present in the schema - confirmed by reading the real server's own
|
|
* `/openapi.json` rather than assumed, after this exact neighboring
|
|
* area (`PlaystateService`'s own history) already got bitten twice by
|
|
* an Emby endpoint accepting a request shape that looked complete but
|
|
* wasn't. `index.ts`'s periodic sweep is what actually drives the
|
|
* start/progress/stop transitions - see its own comment for why that
|
|
* one place could cover this instead of scattering hooks across every
|
|
* queue-advance/track-skip call site the way `PlaystateService
|
|
* .markPlayed` needed to.
|
|
*
|
|
* Best-effort throughout, exactly like `PlaystateService`: any failure
|
|
* is caught and logged here, never thrown - none of this may ever
|
|
* disrupt real playback over an admin-dashboard nicety.
|
|
*/
|
|
class EmbySessionServiceClass {
|
|
/** Called once per member when they start watching/listening to
|
|
* `itemId` - a fresh join, or the party moving to a new item while
|
|
* they're already a member (see index.ts's sweep, which detects that
|
|
* by comparing against `PartyMember.embySession.itemId`). */
|
|
async start(embyToken: string, itemId: string, mediaSourceId: string, playSessionId: string, positionTicks: number, isPaused: boolean): Promise<void> {
|
|
await this.report('/Sessions/Playing', embyToken, {
|
|
ItemId: itemId,
|
|
MediaSourceId: mediaSourceId,
|
|
PlaySessionId: playSessionId,
|
|
PositionTicks: Math.round(positionTicks),
|
|
IsPaused: isPaused,
|
|
PlayMethod: 'Transcode',
|
|
CanSeek: true
|
|
});
|
|
}
|
|
|
|
/** Called periodically (~20s, alongside `PlaystateService
|
|
* .updatePosition` - see index.ts's sweep) for every member with a
|
|
* live session already open for their current item. */
|
|
async progress(embyToken: string, itemId: string, mediaSourceId: string, playSessionId: string, positionTicks: number, isPaused: boolean): Promise<void> {
|
|
await this.report('/Sessions/Playing/Progress', embyToken, {
|
|
ItemId: itemId,
|
|
MediaSourceId: mediaSourceId,
|
|
PlaySessionId: playSessionId,
|
|
PositionTicks: Math.round(positionTicks),
|
|
IsPaused: isPaused,
|
|
PlayMethod: 'Transcode'
|
|
});
|
|
}
|
|
|
|
/** Called once per member when they stop watching/listening to
|
|
* `itemId` - leaving the party (`PartyService.leave`), the item
|
|
* changing (index.ts's sweep), or the party being destroyed for
|
|
* inactivity (`PartyService.removeIdleParties`). */
|
|
async stop(embyToken: string, itemId: string, mediaSourceId: string, playSessionId: string, positionTicks: number): Promise<void> {
|
|
await this.report('/Sessions/Playing/Stopped', embyToken, {
|
|
ItemId: itemId,
|
|
MediaSourceId: mediaSourceId,
|
|
PlaySessionId: playSessionId,
|
|
PositionTicks: Math.round(positionTicks)
|
|
});
|
|
}
|
|
|
|
/** Every call logs its own outgoing request + outcome - see
|
|
* Logger.playstate's own comment for why this needs to be enough on
|
|
* its own, greppable, to answer "is this actually reaching Emby, and
|
|
* with what" for this service too, not just PlaystateService. */
|
|
private async report(path: string, embyToken: string, body: Record<string, unknown>): Promise<void> {
|
|
Logger.playstate(`-> POST ${path} itemId=${body.ItemId}`);
|
|
try {
|
|
const res = await EmbyClient.fetch(path, embyToken, { method: 'POST', body });
|
|
Logger.playstate(`<- ${res.status} ${res.ok ? 'ok' : 'FAILED'} ${path} itemId=${body.ItemId}`);
|
|
} catch (err) {
|
|
Logger.playstate(`<- threw ${path} itemId=${body.ItemId}: ${(err as Error).message}`);
|
|
}
|
|
}
|
|
}
|
|
|
|
export const EmbySessionService = new EmbySessionServiceClass();
|