Playback reporting
Status: works on phone, TV and Cast; no open bugs known. As of 2026-11-23 (main at 26e62f27a).
Start here
Section titled “Start here”- The local player’s reports come from
PlaybackReporter, a service ofapplication(application/.../application/playback/service/, step 13 of #970), whichPlayerViewModelgets from Hilt (PlaybackReporterModuleinplayer/local/.../di/) and tells what happened. It takes each stream as aReportedStreamof its own, which the view model makes from thePlayerItem(toReportedStream,player/local/.../presentation/ReportedStreams.kt), and logs throughAppLog, the log port ofapplication(Timber incore,TimberAppLog).PlaybackStartGate(a rule indomain/.../domain/playback/rule/, ADR 0003) holds a start until the item has loaded;playbackStopReport, in the same package since step 10, decides which stop is owed. What the server was told plays is aPlayingSnapshot(domain/.../domain/playback/model/), whose duration isnulluntil the player knows it. The service keeps it with the start report’sJobas aReportedItem: the job is the service’s state, not a fact ofdomain. - The chain:
PlaybackReportertoPlaybackReports.postPlaybackStart/Progress/Stop(MediaRepositoryImpl) toPlaybackPort.reportStart/Progress/Stoppedwith aPlaybackReporttoJellyfinPlayback, which builds the SDK’sPlaybackStartInfo,PlaybackProgressInfoandPlaybackStopInfo. - What plays per item is
PlaySessions(domain/.../repository/PlaySessions.kt): the play session and the version as onePlayingStream(#909), and the play method (Transcode with the transcode’s session, else DirectPlay without one). The way through the layers: thePlayerItem→ReportedStream→PlaybackReporter.onStreamQueued→PlaybackReports.setPlaySession→PlaySessions→MediaRepositoryImpl.postPlayback*builds thePlaybackReport→JellyfinPlayback. - Cast decides its own reports in
CastPlaybackReporter(proprietary flavour): its own gate (the receiver plays the current item, no load pending), its own interval, and the stop’s percentage fromplaybackStopReportwithclampPercentage = true.CastPlayerControllerImplsends what it decided through the samePlaybackReporter(sendDecidedStart,sendDecidedProgress,sendDecidedStop, step 14 of #970). TV’sTvProgressReportsis only a loop that asks the view model for a progress report. - One sender (R25): only
PlaybackReportercallspostPlaybackStart,postPlaybackProgressandpostPlaybackStop, and onlyMediaRepositoryImpl(and the adapter) callsPlaybackPort.reportStart/Progress/Stopped. The sync of a downloaded item’s stop (SyncWorker,UserDataSyncPort.reportStopped, #584) is a port of its own and outside the rule. - Rules: R1, R2 (no SDK outside the adapter), R12 (
Ticks), R14 (PlayStopandPlayState), R6 (no engine package), R25 (one sender). - Test fast:
./gradlew :application:test --tests '*PlaybackReporter*'(no Android) and./gradlew :domain:test --tests '*PlaybackStartGate*' --tests '*PlaybackStopReport*'; request bodies:./gradlew :server:jellyfin:testDebugUnitTest --tests '*PlaybackReportRequestTest'.
What it does
Section titled “What it does”While an item plays, the app tells the server three things, so its dashboard, “now playing”, resume position and played state follow.
- Start (
POST /Sessions/Playing): once per item, after the player has loaded it (#601), with the position (#868). - Progress (
.../Progress): every 5 s while playing, and at once on pause and resume (#674). One at a time; a newer one replaces a waiting one. - Stop (
.../Stopped): when the item is left (next, previous, end, close). It carries the play session, so the server ends a transcode at once.
The reports keep two orders. Between items: the previous item’s stop must reach the server before the next item’s start, or it can clear the session’s now-playing item; stopJob?.join() in PlayerViewModel.onMediaItemTransition holds the start until then. Within one item: a stop waits up to 5 s for its own start (START_REPORT_WAIT_MS), and progress waits for the start.
Progress and stop also write user data through reportUserData: a downloaded item gets its position and a toBeSynced mark (user-data). The start writes nothing locally.
With the server away, PlaybackReporter catches and logs each failure; playback goes on. A download’s progress and stop are kept in its row and sent by SyncWorker, which also ends a play session the server still shows for this device (#584). A streamed item has no row: a lost report is lost.
Where the code is
Section titled “Where the code is”| Path | What |
|---|---|
application/.../playback/service/PlaybackReporter.kt |
onItemTransition, onTransitionHandled, onStreamReplaced, sendPlaybackStart, reportStopped, reportProgress; ReportedItem, ItemTransition; Cast’s sendDecidedStart, sendDecidedProgress, sendDecidedStop |
application/.../playback/service/ReportedStream.kt, player/local/.../presentation/ReportedStreams.kt |
The stream the service takes, and the mapping from PlayerItem |
player/local/.../di/PlaybackReporterModule.kt |
One service per player, with the application’s scope, Dispatchers.IO and the log |
domain/.../playback/rule/PlaybackStartGate.kt, domain/.../playback/rule/PlaybackStopReport.kt |
The start gate; which stop is owed, with Cast’s clamp of the percentage as an input |
player/local/.../presentation/PlayerViewModel.kt |
onMediaItemTransition, swapInQueue (the reload once a switch of StreamSwitches, in application since step 18 of #970, has its streams; on mpv it tells PlaybackReporter.onStreamReplaced), updatePlaybackProgress, reportedPlayer |
application/.../repository/PlaybackReports.kt |
postPlaybackStart, postPlaybackProgress, postPlaybackStop, setPlaySession |
application/.../repository/MediaRepositoryImpl.kt |
Builds each PlaybackReport; progress and stop through reportUserData |
domain/.../repository/PlaySessions.kt |
The transcode session per item; playReport |
domain/.../domain/ports/PlaybackPort.kt |
PlaybackReport and the three report calls |
server/jellyfin/.../JellyfinPlayback.kt |
The request bodies; playMethod() |
application/.../repository/PlaybackEvents.kt |
Tells screens an item was reported, so they reload |
app/tv/.../ce/presentation/player/TvProgressReports.kt |
TV: the 5 s loop with a 1 s guard, used in TvPlayerScreen.kt |
player/cast/src/proprietary/.../CastPlaybackReporter.kt |
Cast: which reports a receiver’s state owes, its stop from playbackStopReport; CastPlayerControllerImpl.kt sends them through PlaybackReporter. The “item ran out, position 0 for a moment” rule is CastPositions.ranOut in src/main, which the segment skip reads too (#851) |
On the phone the progress loop is in PlayerActivity.kt and a report on pause in BasePlayerActivity.kt. Both call PlayerViewModel.updatePlaybackProgress, as TV does.
- R1, R2: SDK types stay in
server/jellyfin; everything else usesPlaybackReport. - R12: positions go through
Ticks. R14: what a stop writes locally isPlayStopandPlayState. R6:PlaybackReporterreads the player throughReportedPlayer. R19: it is inapplication, so it names no media3 type, noPlayerItemand no Timber. R26: it takes its dispatcher; its waits (START_REPORT_WAIT_MS) arewithTimeoutOrNull, so the tests run them in virtual time. R10: ids areItemIdandMediaSourceId. R25: onlyPlaybackReportercalls the three report operations ofPlaybackReports, and onlyMediaRepositoryImplthose ofPlaybackPort; a new sender goes through the service (ReportSenderTest). - A report runs in
reportScope(the application’s), not the ViewModel’s: the stop must outlive the player. - The previous item’s stop goes before the next item’s start.
PlaybackReporter.sendPlaybackStartwaits for the stops still in flight (stopsInFlight) before it records and sends a start, whichever way the start comes: a transition, a ready state, a quality switch (a second transition on ExoPlayer,onStreamReplacedon mpv). A transition that a newer one overtook while it waited sends nothing (ItemTransition). Before #890 the order was kept only bystopJob?.join()inPlayerViewModel.onMediaItemTransition, and a switch went around it. PlayerInvariantsTestreads the source ofonMediaItemTransitionwith a regex and expectsplaybackReporter.onTransitionHandled(in it: change that signature or call and the test fails; look there first. It also readsPlaybackReporter.ktinapplication(moduleSource), for the gate,onItemReadyand the progress report’s checks ofplayingItem?.startJob.
How to test it
Section titled “How to test it”./gradlew :application:test --tests '*PlaybackReporter*'./gradlew :domain:test --tests '*PlaybackStopReport*'./gradlew :domain:test --tests '*PlaybackStartGate*' --tests '*ReportedPause*'./gradlew :server:jellyfin:testDebugUnitTest --tests '*PlaybackReportRequestTest'./gradlew :domain:test :application:test :architecture:test # PlaySessionsTest, R1, R2, R12, R14./gradlew :app:tv:testLibreDebugUnitTest --tests '*TvProgressReports*'./gradlew :player:cast:testProprietaryDebugUnitTest --tests '*CastPlaybackReporterTest*' --tests '*CastReportSendingTest*'Which test pins what: PlaybackReportRequestTest (server/jellyfin, Robolectric with a stand-in server) the JSON bodies; PlaybackReporterTest the order and timing, in virtual time; PlaySessionsTest what is stored per item; PlayerInvariantsTest the calls in onMediaItemTransition; CastPlaybackReporterTest which reports Cast owes, and CastReportSendingTest what the server gets for each of them.
On a device: the test server (test-server skill, docs/dev-environment.md). What the server was told is in GET /Sessions: NowPlayingItem, PlayState.MediaSourceId, PlayState.PlayMethod, PlayState.PositionTicks. Call it with a DeviceId of your own in the Authorization header (tools/test-server/jf.py has the login and call helpers and its own DeviceId); a DeviceId the app uses signs the app out. The dashboard’s activity log says only that something is playing: no version, no method.
- Versions Movie: pick a version, play, read the session (#909).
- Plain Movie: start, pause, resume, leave: one start, progress, one stop at the right position.
- A quality switch on both ExoPlayer and mpv: one new start after each switch (#870).
- Next while the server holds the stop (#890). The steps: cap the quality, play an episode, press Next (the proxy now holds that episode’s stop), switch the next episode to Original quality while the stop is held, and let the stop through. The next episode must then have exactly one start, after the stop, with the switched stream’s
PlayMethod(DirectPlay). In a unit test: a fake whosepostPlaybackStopsuspends until the test releases it (PlaybackReporterTest), switch during the wait, count the starts and their play method. On a device: a proxy that holdsPOST /Sessions/Playing/Stopped:tools/test-server/forward.py <port> 8096 --hold POST /Sessions/Playing/Stopped --log(press Enter or create the--release-fileto let it through;--lognumbers each request and logs when the forwarder passed it on (close to, not proof of, the server’s order across connections) and the first 1000 bytes of its body), with the app’s address pointed at it (in the app’s database, as root:update serverAddresses set address='<proxy>' where address='<old>', and set it back after the test; without thewhereevery saved server moves). Two traps for a proxy of your own on Windows:SO_REUSEADDRbinds silently to a port another process already uses (forward.pyusesSO_EXCLUSIVEADDRUSEthere and refuses a busy port); and a hard close of the client socket at the server’s EOF gives mpv “Connection reset by peer” onmaster.m3u8(ExoPlayer copes): half-close instead (shutdown(SHUT_WR), whichforward.pydoes since #941). Have the proxy log every/Sessions/Playingrequest in order with its body, and count the starts and read each one’sPlayMethodthere:GET /Sessionsshows only the current state, which the next progress report changes. - A quality cap set during a test stays in the preferences (
pref_player_max_bitrate): the next test then starts transcoded. Reset it to Auto first, or set a cap on purpose before a #890 test, so that the switch to Original changes the play method. - Leave while the item loads: no start, progress or stop for it.
Before a PR
Section titled “Before a PR”./gradlew ktfmtCheck./gradlew :domain:test :application:test :architecture:test./gradlew :player:local:testDebugUnitTest :server:jellyfin:testDebugUnitTest :data:testDebugUnitTest # PlayerInvariantsTestAnything under player/local runs docs/mpv-checklist.md on both backends (player). A change to PlaybackReport changes the pinned bodies in PlaybackReportRequestTest. A change in player/cast compiles in both flavours and needs a real receiver for anything about a receiver.
Decisions and history
Section titled “Decisions and history”- #461, #601: a stop is owed once the start was sent; the start waits for the item to load.
- #599, #728: Transcode only with its session; the session follows the stream that plays.
- #674: pause and resume at once, in order. #868: the start carries the position.
- #757, #765:
PlaybackPort;PlaybackReporterout ofPlayerViewModel. - #970, step 13 of ADR 0003:
PlaybackReportera service ofapplication, withReportedStreamandAppLog; the start’sJobleftPlayingSnapshotfor the service’sReportedItem. Cast keepsCastPlaybackReporterand its own three report calls until step 14. - #970, step 14: Cast’s stop takes its percentage from
playbackStopReport(the clamp to 0..100 an input), and its three report calls go throughPlaybackReporter; Cast keeps its own gate. Rule R25 since. - #870 / #889: a quality switch on mpv reports its new stream (
onStreamReplaced). - #846: leaving without a stop still lets screens reload.
Pitfalls
Section titled “Pitfalls”- An item’s stop does not release its queued download (#1143).
PlaybackEvents.onStoppedannounces the report, whileonPlayingtracks the current item. The local player also accumulates download ids inqueuedDownloadIdsuntilonPlaying(null)on release, so a stop triggered by Previous cannot let Smart Downloads remove Next. Queue replacements keep outgoing files protected even if engine installation fails; see downloads. - The reports name the version (#909).
setPlaySessionrecords the version of the stream that is loaded with its play session, as onePlayingStreamper item inPlaySessions(domain/.../repository/PlaySessions.kt), and every report sends it asMediaSourceId. A report reads the session and the version from that one value. Without it the server shows the item’s first version.PlaySessionsis keyed by item id: the last stream loaded for an item is the one named. - A start goes out after the previous item’s stop, once (#890).
onMediaItemTransitiontakes the item before it waits for the previous item’s stop. A quality or audio switch in that wait (ExoPlayer: a second transition; mpv:onStreamReplaced) used to send the new stream’s start ahead of the stop, and the waiting transition a second start after it. NowPlaybackReporternumbers the transitions (ItemTransition): one handled after a newer one sends nothing. AndsendPlaybackStartwaits for the stops still being sent (stopsInFlight), whichever way the start comes. A new way intosendPlaybackStartkeeps both. - A start before the stream is fetched says DirectPlay;
PlaySessions.playReportsends Transcode only with a known session, and the first progress corrects it. - A quality switch leaves the old transcode to the server’s timer: there is no call to end it.
- No start, no progress.
reportProgressreturns whileplaying?.startJobis null: the server marks an item without a runtime played at its first position report. PlaybackReporteris not thread-safe: the player’s listener and the main thread only.
Source: docs/areas/playback-reporting.md · raw Markdown