State Management

Purpose

This component addresses the complex challenge of controlling media playback and advertisement integration through a clear, modular finite state machine (FSM) design. It encapsulates distinct playback scenarios—such as content playing, ad retrieval, ad playback (including VPAID ads), and user interactions—into well-defined states. This approach ensures robust handling of asynchronous events, smooth transitions between content and ads, and consistent UI updates, which collectively provide a seamless viewing experience.

Functionality

State Management defines specific playback states that govern the behavior of the media player at any given point. Each state encapsulates the logic for responding to inputs (events) and performing relevant actions. Key functionalities include:

Input-Driven Transitions

Each state defines a `transformToState` method that determines the next state based on input events. For example:

@Override
public State transformToState(Input input, StateFactory factory) {
    switch (input) {
        case MAKE_AD_CALL:
            return factory.createState(MakingAdCallState.class);
        case MOVIE_FINISH:
            return factory.createState(FinishState.class);
        // other cases...
    }
    return null;
}

Work Execution and UI Updates

States implement `performWorkAndUpdatePlayerUI` to carry out actions like fetching ads, preparing players, pausing/resuming playback, and updating UI visibility. For instance, `AdPlayingState` prepares the ad player and hides subtitles:

SimpleExoPlayer adPlayer = controller.getAdPlayer();
adPlayer.prepare(adMedia.getMediaSource(), !haveResumePosition, true);
adPlayer.setPlayWhenReady(true);

((TubiExoPlayerView) controller.getExoPlayerView()).getSubtitleView().setVisibility(View.INVISIBLE);

Integration

State Management is the core operational layer within the broader Finite State Machine Playback system. It directly implements the playback logic abstracted by the FSM framework, bridging asynchronous ad and cue point fetching with synchronous playback control.

This modular state structure allows the FSM player to react dynamically to playback events, ad server responses, and user interactions without mixing concerns, promoting maintainability and extensibility.

Diagram

flowchart LR
    Start[Start Playback] --> FetchCuePoints[Fetch Cue Points]
    FetchCuePoints -->|Has Preroll| MakePrerollAdCall[MakingPrerollAdCallState]
    FetchCuePoints -->|No Preroll| MoviePlaying[MoviePlayingState]
    MakePrerollAdCall --> ReceiveAd[ReceiveAdState]
    ReceiveAd --> AdPlaying[AdPlayingState]
    AdPlaying -->|VPAID Ad Detected| Vpaid[VpaidState]
    AdPlaying --> MoviePlaying
    Vpaid --> MoviePlaying
    MoviePlaying -->|Midroll Cue Point| MakingAdCall[MakingAdCallState]
    MakingAdCall --> ReceiveAd
    MoviePlaying -->|Playback Finished| Finish[FinishState]

This flowchart illustrates the primary states and transitions managed by State Management, highlighting the core playback and ad integration process, including preroll, midroll, standard ads, and VPAID ads.