PlayerModuleDefault.java
Overview
`PlayerModuleDefault.java` is a **Dagger 2 module** responsible for providing default, scoped dependencies related to the media player system within the Tubitv media framework. It defines how core playback components, such as the **finite state machine (FSM) player**, UI controllers, ad retrieval mechanisms, playback monitors, and VPAID clients, are instantiated and injected into the application.
This module is designed for **default or demo environments**, supplying mostly stub or no-op implementations that enable the player system to function without requiring live networking or complex UI integrations like WebView. It serves as a simple, self-contained provider for essential playback services scoped to an activity lifecycle, supporting modularity, testability, and lifecycle management.
Classes and Methods
Class: PlayerModuleDefault
A Dagger module annotated with `@Module` that provides scoped instances of player-related components.
Constructor
public PlayerModuleDefault()
Default no-argument constructor.
Initializes the module (no state or parameters needed).
Provided Dependencies (Methods)
Each method is annotated with `@Provides` and `@ActicityScope` (a custom scope controlling lifecycle within an activity). These methods define how a particular dependency is created.
1. provideStateFactory()
@ActicityScope
@Provides
StateFactory provideStateFactory()
Purpose: Creates and provides a new instance of
StateFactory.Returns: A new
StateFactoryobject.Usage:
StateFactoryis used by the FSM player to instantiate concrete playback states.
**Example:**
StateFactory factory = playerModuleDefault.provideStateFactory();
2. provideFsmPlayer(StateFactory factory)
@ActicityScope
@Provides
FsmPlayer provideFsmPlayer(StateFactory factory)
Parameters:
factory: aStateFactoryinstance used to create FSM states.
Returns: An instance of
FsmPlayerImperial(a concrete FSM player) initialized with the given factory.Implementation Detail:
The returnedFsmPlayerImperialoverrides theinitializeState()method to start the FSM in theFetchCuePointStateclass.Usage: Used to manage the media playback state machine lifecycle.
**Example:**
FsmPlayer player = new FsmPlayerImperial(factory) {
@Override
public Class initializeState() {
return FetchCuePointState.class;
}
};
3. provideController()
@ActicityScope
@Provides
PlayerUIController provideController()
Purpose: Provides a default instance of
PlayerUIController.Returns: A new
PlayerUIController.Usage: Responsible for managing UI-related playback controls.
4. provideComponentController()
@ActicityScope
@Provides
PlayerAdLogicController provideComponentController()
Purpose: Provides a default instance of
PlayerAdLogicController.Returns: A new
PlayerAdLogicController.Usage: Manages ad-specific playback logic.
5. provideAdRetriever()
@ActicityScope
@Provides
AdRetriever provideAdRetriever()
Purpose: Provides a new instance of
AdRetriever.Returns: A new
AdRetriever.Usage: Used to fetch ad media data.
6. provideCuePointsRetriever()
@ActicityScope
@Provides
CuePointsRetriever provideCuePointsRetriever()
Purpose: Provides a new instance of
CuePointsRetriever.Returns: A new
CuePointsRetriever.Usage: Used to fetch cue points for ad insertion timing.
7. provideAdPlayingMonitor(FsmPlayer player)
@ActicityScope
@Provides
AdPlayingMonitor provideAdPlayingMonitor(FsmPlayer player)
Parameters:
player: The FSM player instance to monitor.
Returns: A new
AdPlayingMonitormonitoring the provided FSM player.Usage: Observes ad playback progress and notifies FSM state changes.
8. provideCuePointMonitor(FsmPlayer fsmPlayer)
@ActicityScope
@Provides
CuePointMonitor provideCuePointMonitor(FsmPlayer fsmPlayer)
Parameters:
fsmPlayer: The FSM player instance to monitor for cue points.
Returns: An instance of
CuePointMonitorwith overridden methodnetworkingAhead()returning5000(milliseconds).Usage: Monitors cue points during playback, signaling FSM when approaching ad breaks.
9. provideAdMediaModel()
@ActicityScope
@Provides
AdMediaModel provideAdMediaModel()
Purpose: Provides a stub
AdMediaModelcontaining a single hardcoded ad.Implementation Details:
Creates a
MediaModelad with a sample Apple ad URL and a fallback URL.Adds the ad to a list.
Returns an
AdMediaModelthat overridesnextAD()to return the first ad in the list ornull.
Usage: Supplies ad media data for playback in the demo environment.
10. provideAdInterfaceNoPreroll()
@ActicityScope
@Provides
AdInterface provideAdInterfaceNoPreroll()
Purpose: Provides a stub implementation of
AdInterface.Implementation Details:
fetchAd(...): Immediately calls back with the stubbed ad media model fromprovideAdMediaModel().fetchQuePoint(...): Immediately calls back withnullcue points.
Usage: Simulates ad and cue point fetching without real network calls or preroll ads.
11. provideVpaidClient()
@ActicityScope
@Provides
VpaidClient provideVpaidClient()
Purpose: Provides a no-op stub implementation of
VpaidClient.Implementation Details:
All methods (init,notifyAdError,notifyVideoEnd,getVastXml) are empty or returnnull.Usage: Placeholder for VPAID client functionality in demo or simplified environments.
Important Implementation Details
Activity-Scoped Singleton Instances: All provided dependencies share the same lifecycle within an activity, managed by the custom
@ActicityScopeannotation.Stub Implementations: The module avoids network calls or complex UI dependencies, instead returning immediate or no-op responses suitable for testing or offline/demo scenarios.
FSM Player Initialization: The FSM player always starts in the
FetchCuePointStateto simulate cue point retrieval before playback.Hardcoded Ad Media: A single advertisement with a fixed URL is used for demonstration, simplifying ad playback without external dependencies.
Cue Point Monitoring: The
CuePointMonitorsignals to fetch cue points 5 seconds before reaching them, facilitating smooth ad insertion timing.
Interaction with Other Parts of the System
Consumed by:
Activities or components that require playback dependencies injected, typically via a Dagger component that includesPlayerModuleDefault.Provides Dependencies To:
FsmPlayerImperialand its playback states.UI controllers managing playback and ads.
Ad and cue point retrieval logic (stubbed).
Monitoring classes observing playback progress.
VPAID client interface (stubbed).
Dependency Injection Role:
This module centralizes creation and lifecycle management of player-related services, enabling decoupled architecture where consumers receive fully configured instances without direct instantiation.Complementary Modules:
In the same system, other modules likeFSMModuleRealprovide real implementations with WebView integration and asynchronous network calls.PlayerModuleDefaultserves as the simpler counterpart.
Usage Example
Assuming a Dagger component includes `PlayerModuleDefault`:
@Component(modules = PlayerModuleDefault.class)
@ActicityScope
public interface PlayerComponent {
void inject(MyPlayerActivity activity);
}
In an injected activity:
@Inject
FsmPlayer fsmPlayer;
@Inject
PlayerUIController uiController;
@Inject
AdInterface adInterface;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
playerComponent.inject(this);
// FSM player starts in FetchCuePointState
fsmPlayer.start();
// Use adInterface to fetch ads (returns stub immediately)
adInterface.fetchAd(new AdRetriever(), new RetrieveAdCallback() {
@Override
public void onReceiveAd(AdMediaModel adMedia) {
// Handle received ad media
}
});
}
Mermaid Class Diagram
classDiagram
class PlayerModuleDefault {
+PlayerModuleDefault()
+provideStateFactory() StateFactory
+provideFsmPlayer(factory: StateFactory) FsmPlayer
+provideController() PlayerUIController
+provideComponentController() PlayerAdLogicController
+provideAdRetriever() AdRetriever
+provideCuePointsRetriever() CuePointsRetriever
+provideAdPlayingMonitor(player: FsmPlayer) AdPlayingMonitor
+provideCuePointMonitor(fsmPlayer: FsmPlayer) CuePointMonitor
+provideAdMediaModel() AdMediaModel
+provideAdInterfaceNoPreroll() AdInterface
+provideVpaidClient() VpaidClient
}
The diagram highlights the main provider methods and their return types.
Each method is scoped to the activity lifecycle.
This class acts as a factory for player-related service instances.
Summary
`PlayerModuleDefault.java` is a fundamental Dagger module in the Tubitv media player framework responsible for:
Providing default, scoped singleton instances of FSM player, controllers, retrievers, monitors, and VPAID clients.
Supplying stub implementations suitable for demo, offline, or test environments.
Simplifying dependency management by centralizing object creation behind injectable providers.
Enabling seamless integration and lifecycle management of playback components within an activity scope.
This module plays a vital role in the modular architecture of the media playback system, allowing different runtime configurations and easing testing and development efforts.