Playback Monitoring

Purpose

Playback Monitoring addresses the critical need to observe media playback progress and ad playback events in real time, to ensure timely and accurate triggering of ad-related actions within the media player. Specifically, it monitors the video playback timeline to detect when cue points are reached to initiate ad calls and ad insertions, and it observes ad playback completion or errors to transition the player state machine appropriately. This ensures seamless integration of advertisements into the content stream without disrupting the user experience.

Functionality

Playback Monitoring is realized through two specialized monitors: **CuePointMonitor** and **AdPlayingMonitor**.

Key Code Snippets

**Triggering ad call and ad show in CuePointMonitor:**

if (isProgressActionable(adCallPoints, milliseconds) && safeCheckForAdcall) {
    safeCheckForAdcall = false;
    fsmPlayer.transit(Input.MAKE_AD_CALL);
}

if (isProgressActionable(cuePoints, milliseconds) && safeCheckForCue) {
    safeCheckForCue = false;
    fsmPlayer.transit(Input.SHOW_ADS);
}

**Ad playback completion handling in AdPlayingMonitor:**

@Override
public void onPlayerStateChanged(EventTime eventTime, boolean playWhenReady, int playbackState) {
    if (playbackState == Player.STATE_ENDED && playWhenReady) {
        fsmPlayer.removePlayedAdAndTransitToNextState();
    }
}

Integration

Playback Monitoring tightly integrates with the parent topic — **Ad and Cue Point Management** — by acting as the runtime observer that bridges media playback progress with ad insertion logic. While Ad Retrieval and Cue Point Retrieval focus on fetching ad metadata and cue timing data, Playback Monitoring ensures that these data points translate into timely FSM transitions.

This monitoring introduces a proactive and reactive mechanism not covered in other subtopics — it acts as the real-time engine that triggers state changes based on playback progress and ad lifecycle events.

Diagram

flowchart TD
    ContentPlayer[Content Playback (ExoPlayer)]
    CuePointMonitor -->|Checks playback position| ContentPlayer
    ContentPlayer -->|Playback Progress| CuePointMonitor
    CuePointMonitor -->|Trigger MAKE_AD_CALL| FSM[FsmPlayer]
    CuePointMonitor -->|Trigger SHOW_ADS| FSM
    AdPlayer[Ad Playback (ExoPlayer)]
    AdPlayingMonitor -->|Monitors ad playback state| AdPlayer
    AdPlayer -->|Playback events| AdPlayingMonitor
    AdPlayingMonitor -->|Notify ad ended or error| FSM
    FSM -->|Controls playback| ContentPlayer
    FSM -->|Controls playback| AdPlayer

This flowchart highlights the core monitoring interactions: `CuePointMonitor` observes content playback progress to trigger ad calls and ad playback start, while `AdPlayingMonitor` observes ad playback events to notify FSM transitions. Both monitors communicate with the FSM player, which in turn controls the underlying media players.