Skip to content

Add HLS playback stats reporting - #1917

Merged
aleksandar-apostolov merged 67 commits into
developfrom
pratimmallick/vid-1546-hls-stats
Nov 27, 2026
Merged

aleksandar-apostolov merged 67 commits into
developfrom
pratimmallick/vid-1546-hls-stats

Conversation

@PratimMallick

@PratimMallick PratimMallick commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Stacked on #1909. Merge after parent.

Goal

Refs VID-1546 — report HLS viewers' playback to POST /video/call/{type}/{id}/hls_stats, so they appear in the dashboard's call stats with their watch time, stalls and errors.

Reports are sent when the join response returns playback.stats_token, unless stats_options.enable_hls_stats is false or the app set StreamVideoBuilder(enableStatsReporting = false).

Implementation

  • API: reportHLSStats endpoint and its models. PlaybackResponse.statsToken, StatsOptions.enableHlsStats / reportingHlsIntervalMs, and JoinCallRequest.unifiedSessionId are added as trailing nullable fields. The join now sends the call's existing unifiedSessionId.
  • HlsPlayer gets two optional hooks with default bodies, so existing players still compile: setPlaybackListener (state and error code) and readPlaybackMetrics. New public types: HlsPlaybackListener, HlsPlaybackState, HlsPlaybackMetrics.
  • HlsStatsCollector applies the stall definition shared across SDKs. A stall is Playing followed by Buffering; the initial load, seeks and pauses are not stalls. Watch time counts only time spent Playing. It buffers stall_start, stall_end (with duration_ms) and error events until a report carrying them is handled.
  • HlsStatsReporter sends one started on first play, then stats every reporting_hls_interval_ms (falling back to reporting_interval_ms, then 10 s), and ended on leave. ended uses end_reason left, or error with error_code after a playback error. All reports share one user_session_id. Delivery rules:
    • 2xx: buffered events are dropped.
    • 400: events are dropped and nothing is retried.
    • 403: reporting stops for the session, and no ended is sent.
    • 429, 5xx or no response: events are kept for the next report. started and ended are retried with backoff.
  • If playback fails before the first frame, a single ended is sent with end_reason: "error", the error code and the error event. No started or periodic stats are sent for a session that never played.
  • When the player reaches the end of a stopped broadcast, the session sends ended (end_reason: "left") with a stream_ended event instead of reporting frozen stats. The event shows up as hls.stream_ended in the timeline.
  • When the broadcast restarts (call.hls_broadcasting_started), playback switches to the new playlist from the event: a new player and a new stats session. The old session ends with ended.
  • CallState.hlsPlaybackState and hlsPlaybackError (experimental) expose the player's state and last fatal error code, so the app can decide what to do when playback ends or fails. The SDK does not rejoin on its own.
  • Wiring: CallJoinCoordinator starts the reporter after the player is installed. If the reporter fails to start, the HLS join still succeeds. CallLifecycleManager stops it before releasing the player. ended is sent on the client scope, so it outlives the call scope.
  • ExoHlsPlayer maps ExoPlayer state to HlsPlaybackState, reports PlaybackException.errorCodeName, and reads bitrate, buffered duration, live offset, dropped frames and resolution.
  • The payload omits user_id (the backend takes it from the auth token) and the browser fields. It sends sdk = stream-android, the SDK version, os = Android and the OS version. Logs never include the stats token or the playlist URL.
  • API dump: the constructor and copy of StatsOptions change shape (the usual effect of regenerating a generated model). JoinCallRequest and PlaybackResponse already changed in Join livestreams over HLS when the backend grants HLS playback #1908.

Testing

  • Unit tests:
    • Collector: stall rules, watch time, event acknowledgement, the buffer cap, stream_ended when the stream ends.
    • Reporter: report order, the interval, keeping or dropping events per response status, 403 stopping reports, ended reasons and retries, no reports before first play, a single ended for an error before the first frame, ended when the stream ends.
    • Coordinator: switching to the new playlist on a broadcast restart, with a new stats session.
    • Config gating and the status-code mapping.
    • Coordinator and lifecycle wiring, and unified_session_id on the join.
    • Serialization of the new fields.
    • Mapping ExoPlayer state to HlsPlaybackState.
  • Ran ./gradlew spotlessCheck apiCheck :stream-video-android-core:testDebugUnitTest :stream-video-android-hls:testDebugUnitTest :demo-app:compileDevelopmentDebugKotlin: all passed. The repo has no detekt task.
  • Manual testing needs a backend that serves the hls_stats endpoint. Join a livestream that is served as HLS and watch for a few intervals. Turning on airplane mode should produce a stall and then an error. Leave, then check that the call stats show the viewer with a started, periodic stats, and one ended.

PratimMallick and others added 23 commits November 2, 2026 13:59
…nection

RtcSession becomes open and gains an internal initializeRtc flag. When the
flag is false, the session never creates the SFU connection or any peer
connection and leaves the shared peer connection factory untouched.
EmptyRtcSession uses it and turns every public member into a no-op.

cleanup() no longer assumes the SFU connection module exists.

Prepares for joining a call without WebRTC media (VID-1543). No behaviour
change for existing sessions.

Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
Stock LivestreamPlayer shows the playlist when the backend grants HLS. Custom screens opt in by composing HlsVideoRenderer, and VideoRenderer stays WebRTC-only.
…1543-hls-join-sdk

# Conflicts:
#	stream-video-android-core/api/stream-video-android-core.api
…sdk' into pratimmallick/vid-1543-hls-renderer
Media3 adds about 2 MB, and few apps need HLS, so the player moves to a
separate artifact. Core keeps the contract: HlsPlayer and HlsPlayerFactory,
registered through StreamVideoBuilder.hlsPlayerFactory.

The first join only declares HLS support when a factory is registered, so
an app without one is never served HLS. An HLS join creates the call's
player next to the playback session; leave releases it. A player that
cannot be created fails the join and installs nothing.
…sdk' into pratimmallick/vid-1543-hls-renderer
Adds stream-video-android-hls with ExoHlsPlayerFactory and the Compose
HlsVideoRenderer. Core and ui-compose no longer depend on Media3, and the
process-wide playback store is gone: the renderer attaches to the call's
player and detaches (pauses) on dispose, while the SDK releases it on leave.

The demo app registers the factory and shows a minimal HLS viewer screen
when a call joins over HLS.
Send started, periodic stats and ended reports to hls_stats while a call
plays over HLS, when the join response enables HLS stats. The player
reports its state, errors and readings through new optional HlsPlayer
hooks; ExoHlsPlayer implements them.
@PratimMallick PratimMallick added the pr:new-feature Adds new functionality label Nov 20, 2026
@PratimMallick

Copy link
Copy Markdown
Contributor Author

@CodeRabbit review

@github-actions

github-actions Bot commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

PR checklist ✅

All required conditions are satisfied:

  • Title length is OK (or ignored by label).
  • At least one pr: label exists.
  • Sections ### Goal, ### Implementation, and ### Testing are filled, or the PR is bot-authored.
  • An issue is linked (Linear ticket or GitHub issue), or the PR is bot-authored.

🎉 Great job! This PR is ready for review.

@github-actions

github-actions Bot commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

SDK Size Comparison 📏

SDK Before After Difference Status
stream-video-android-core 12.38 MB 12.41 MB 0.03 MB 🟢
stream-video-android-ui-xml 5.66 MB 5.65 MB -0.02 MB 🚀
stream-video-android-ui-compose 6.19 MB 6.17 MB -0.02 MB 🚀

@PratimMallick
PratimMallick marked this pull request as ready for review November 20, 2026 15:09
ExoPlayer stretches each frame over the SurfaceView buffer, which kept its own size, so in portrait the 16:9 box showed a stretched crop. The buffer now takes the video's size and the compositor scales it into the aspect-ratio box.
A fatal error idles the player, which reports no state, so watch time
and an open stall kept growing until the viewer left.
The default policy failed playback after a few seconds offline and the
player stayed idle. Client errors (4xx) still fail at once.
Integrations decide how to recover when HLS playback ends or fails. The
SDK now listens to the player with or without stats reporting.
…rer' into pratimmallick/vid-1546-hls-stats

# Conflicts:
#	stream-video-android-hls/src/main/kotlin/io/getstream/video/android/hls/ExoHlsPlayer.kt
The broadcast playlist keeps every segment, so after a long stall the
player resumed minutes behind live and never fell out of the window.
The broadcast playlist has no program date times, so the live offset is
always unknown and the jump to the live edge never happened.
Playback can be ready again before the refresh that moves the live
position arrives, so checking only on ready missed the drift.
LivestreamPlayer showed "host video not available" for HLS viewers,
since an HLS call has no participants. The new hlsContent slot renders
the broadcast instead, under the regular overlay. ui-compose does not
depend on the HLS module, so integrators pass HlsVideoRenderer in.
A load error before the first frame (for example an empty media
playlist) cannot recover and previously sent no report at all, so the
backend never learned about the failed viewer. Send a single `ended`
with the error instead, and send `started` before `ended` when a leave
right after the first frame cancelled it.
A restarted HLS broadcast writes a new playlist, so the viewer's player
stayed ended on the old one. On `call.hls_broadcasting_started` while
playing HLS, switch to the playlist from the event: build a new player,
start a new stats session for it, and release the old player.
Once the player reaches the end of a stopped broadcast, send `ended`
instead of reporting frozen stats until the viewer leaves. A restart
starts a new session.
The backend only accepts `left` and `error` as end reasons, so a stopped
broadcast showed up as the viewer leaving. Add a `stream_ended` player
event when playback reaches the end of the stream; it rides on the
`ended` report and appears as `hls.stream_ended` in the timeline.
Base automatically changed from pratimmallick/vid-1543-hls-renderer to develop November 27, 2026 14:49
@aleksandar-apostolov
aleksandar-apostolov added this pull request to the merge queue Nov 27, 2026
Merged via the queue into develop with commit 63b204f Nov 27, 2026
25 checks passed
@aleksandar-apostolov
aleksandar-apostolov deleted the pratimmallick/vid-1546-hls-stats branch November 27, 2026 15:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr:new-feature Adds new functionality

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants