FsmPlayer.java


Overview

`FsmPlayer` is an **abstract class** that implements a Finite State Machine (FSM) to manage media playback within a video player system, particularly focusing on orchestrating **content playback**, **ad playback**, and **state transitions** in response to playback events and ad retrieval results.

This class serves as the **core controller** that:

`FsmPlayer` acts as the backbone for playback logic, offering a robust, extensible framework to handle complex playback workflows in a modular, maintainable way.


Detailed Description of Classes and Interfaces

Class: FsmPlayer

Package

`com.tubitv.media.fsm.state_machine`

Implements

Key Responsibilities


Fields and Properties

Field

Type

Description

`playerComponentController`

`PlayerAdLogicController`

Wrapper for player logic related components (e.g., ad player, content player logic).

`controller`

`PlayerUIController`

Wrapper for UI components managing video playback UI and ExoPlayer instances.

`adServerInterface`

`AdInterface`

Interface to interact with ad network servers for fetching ads.

`adRetriever`

`AdRetriever`

Contains information to retrieve ads from the server.

`cuePointsRetriever`

`CuePointsRetriever`

Contains information to retrieve cue points from the server, which trigger midroll ads.

`movieMedia`

`MediaModel`

The main content media being played.

`adMedia`

`AdMediaModel`

The current ad media being played.

`currentState`

`State`

The current FSM playback state representing the player's status.

`factory`

`StateFactory`

Factory class to create and cache state instances for transitions.

`VPAID_END_POINT`

`String`

URL endpoint for VPAID ads (default is example URL).

`isInitialized`

`boolean`

Flag indicating whether the FSM player has been initialized.

`mLifecycle`

`Lifecycle`

Android lifecycle to avoid transitions when the UI is inactive (e.g., stopped).


Constructor

public FsmPlayer(StateFactory factory)

Public Methods

static void updateMovieResumePosition(PlayerUIController controller)


boolean isInitialized()


Getters and Setters for Media and Controllers


boolean hasAdToPlay()


MediaModel getNextAdd()


void updateCuePointForRetriever(long cuepoint)


FSM Interface Methods

State getCurrentState()

void restart()

void transit(Input input)

fsmPlayer.transit(Input.INITIALIZE);

Triggers FSM initialization and state transition.


Ad Playback Control Methods (FsmAdController)

void removePlayedAdAndTransitToNextState()

void adPlayerError()


Callback Methods (RetrieveAdCallback)

void onReceiveAd(AdMediaModel mediaModels)

void onError()

void onEmptyAdReceived()


Other Methods

void updateSelf()


Important Implementation Details and Algorithms


Interaction with Other System Components


Usage Example

// Initialize FSM Player with a StateFactory
StateFactory factory = new StateFactory();
FsmPlayer fsmPlayer = new ConcreteFsmPlayer(factory);

// Set controllers
fsmPlayer.setController(playerUIController);
fsmPlayer.setPlayerComponentController(playerAdLogicController);

// Set media and ad retrieval info
fsmPlayer.setMovieMedia(movieMediaModel);
fsmPlayer.setAdRetriever(adRetriever);
fsmPlayer.setAdServerInterface(adInterface);

// Start playback FSM
fsmPlayer.transit(Input.INITIALIZE);

// Later, on receiving ads:
fsmPlayer.onReceiveAd(adMediaModel);

// On ad playback completion:
fsmPlayer.removePlayedAdAndTransitToNextState();

Visual Diagram: Class Structure of FsmPlayer.java

classDiagram
    class FsmPlayer {
        -PlayerAdLogicController playerComponentController
        -PlayerUIController controller
        -AdInterface adServerInterface
        -AdRetriever adRetriever
        -CuePointsRetriever cuePointsRetriever
        -MediaModel movieMedia
        -AdMediaModel adMedia
        -State currentState
        -StateFactory factory
        -String VPAID_END_POINT
        -boolean isInitialized
        -Lifecycle mLifecycle
        +FsmPlayer(StateFactory factory)
        +static void updateMovieResumePosition(PlayerUIController controller)
        +boolean isInitialized()
        +MediaModel getMovieMedia()
        +void setMovieMedia(MediaModel)
        +AdMediaModel getAdMedia()
        +void setAdMedia(AdMediaModel)
        +AdInterface getAdServerInterface()
        +void setAdServerInterface(AdInterface)
        +AdRetriever getAdRetriever()
        +void setAdRetriever(AdRetriever)
        +Lifecycle getLifecycle()
        +void setLifecycle(Lifecycle)
        +boolean hasAdToPlay()
        +String getVPAID_END_POINT()
        +void setVPAID_END_POINT(String)
        +MediaModel getNextAdd()
        +PlayerUIController getController()
        +void setController(PlayerUIController)
        +PlayerAdLogicController getPlayerComponentController()
        +void setPlayerComponentController(PlayerAdLogicController)
        +CuePointsRetriever getCuePointsRetriever()
        +void setCuePointsRetriever(CuePointsRetriever)
        +void updateCuePointForRetriever(long)
        +State getCurrentState()
        +void restart()
        +void transit(Input)
        +void removePlayedAdAndTransitToNextState()
        +void adPlayerError()
        +void updateSelf()
        +void onReceiveAd(AdMediaModel)
        +void onError()
        +void onEmptyAdReceived()
    }
    FsmPlayer ..|> Fsm
    FsmPlayer ..|> RetrieveAdCallback
    FsmPlayer ..|> FsmAdController

Summary

`FsmPlayer` is an abstract finite state machine controller for a media player that elegantly manages the complex interplay between content playback and ad insertion. It coordinates playback states, handles asynchronous ad fetching callbacks, manages lifecycle-aware transitions, and supports special ad formats like VPAID. Its design promotes modularity, extensibility, and robustness, making it a foundational component of the media playback system.


End of Documentation for FsmPlayer.java