Ad and Cue Point Management

This module is responsible for coordinating the fetching, monitoring, and triggering of video advertisements and cue points during media playback. It integrates closely with the finite state machine (FSM) to enable timely ad insertions based on playback progress. By managing ad retrieval requests and cue point triggers, this module ensures a seamless user experience with video ads appearing at designated points without disrupting content playback.


Core Concepts and Purpose

This module exists to solve the challenge of timely and accurate insertion of video ads within content playback. It synchronizes ad calls and ad playback with content progress, preventing redundant calls and missed ad opportunities, while integrating with the FSM to manage state transitions accordingly.


Module Components and Workflows

1. Ad Retrieval Data Model (AdRetriever)

The `AdRetriever` class encapsulates the attributes necessary to request ads from the ad server. It contains:

This object is updated dynamically with the current cue point during playback to request the correct set of ads aligned with the content timeline.

public class AdRetriever {
    private String videoId;
    private String publisherId;
    private long cubPoint;

    // Constructors, getters, and setters omitted for brevity
}

2. Cue Point Retrieval Data Model (CuePointsRetriever)

The `CuePointsRetriever` class holds the identifiers needed to retrieve cue points for a given video:

Cue points indicate the timestamps in the content where ads should be inserted (e.g., preroll, midroll, postroll).

public class CuePointsRetriever {
    private String videoId;
    private String publisherId;

    // Constructors, getters, and setters omitted for brevity
}

3. Playback Monitoring

Two key classes monitor playback to coordinate ad-related actions:

a. AdPlayingMonitor

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

@Override
public void onPlayerError(EventTime eventTime, ExoPlaybackException error) {
    fsmPlayer.removePlayedAdAndTransitToNextState();
}

The monitor ensures ads do not stall playback and handles errors gracefully.

b. CuePointMonitor

Key methods:

public void onMovieProgress(long milliseconds, long durationMillis) {
    if (fsmPlayer.getCurrentState() instanceof AdPlayingState || fsmPlayer.getCurrentState() instanceof VpaidState) {
        return; // Do not trigger during ad playback
    }
    preformAdCallIfNecessary(milliseconds);
    preformShowAdIfNecessary(milliseconds);
}

The monitor thus acts as a bridge between playback progress and FSM-driven ad state transitions, ensuring ads are fetched and displayed at correct times.


Interaction with Other System Parts


Important Concepts and Design Patterns


Visual Diagram: Cue Point and Ad Retrieval Workflow

flowchart TD
    ContentPlayer[Content Playback Progress]
    ContentPlayer -->|Frame by frame| CuePointMonitor
    CuePointMonitor -->|Near Ad Call Point| FSM[Finite State Machine]
    FSM -->|Make Ad Network Call| AdRetriever
    AdRetriever --> AdServer[Ad Server]
    AdServer -->|Ads Data| FSM
    FSM -->|Transition to AdPlayingState| AdPlayer[Ad Playback Player]
    AdPlayer -->|Ad Playback Progress| AdPlayingMonitor
    AdPlayingMonitor -->|Ad Finished or Error| FSM
    FSM -->|Transition to Content Playback| ContentPlayer

This diagram illustrates how the monitoring components observe playback progress, trigger ad retrieval calls, coordinate with the FSM to manage state transitions, and monitor ad playback completion to resume content playback accordingly.