MediaHelper.java
Overview
`MediaHelper.java` is a utility class designed to aid media playback components by providing standardized factory methods for constructing ExoPlayer data source factories. These factories are essential for fetching media streams efficiently and reliably over HTTP with integrated bandwidth metering and user-agent identification.
The class primarily focuses on:
Building
DataSource.Factoryinstances that wrap HTTP data sources and optionally incorporate bandwidth metering.Creating HTTP data source factories with custom user agent strings.
While the file previously contained commented-out code related to managing a list of media models and concatenated media sources, its current active functionality is limited to these static factory methods.
This utility class simplifies and centralizes the creation of ExoPlayer-compatible data sources, ensuring consistent network behavior and facilitating adaptive streaming.
Classes and Methods
Class: MediaHelper
A utility class that provides static methods for creating ExoPlayer data source factories required for media playback.
Method: buildDataSourceFactory
public static @NonNull DataSource.Factory buildDataSourceFactory(
@NonNull Context context,
@Nullable DefaultBandwidthMeter bandwidthMeter)
Description
Creates a `DataSource.Factory` that combines local and HTTP data sources wrapped with bandwidth metering support. This factory is used by ExoPlayer to instantiate media sources capable of adaptive streaming.
Parameters
context(android.content.Context): The Android context, used for accessing application resources and system services.bandwidthMeter(DefaultBandwidthMeter, nullable): An optional bandwidth meter that tracks current network bandwidth to support adaptive bitrate streaming.
Returns
A non-null
DataSource.Factoryinstance configured to create data sources with bandwidth metering and HTTP capabilities.
Usage Example
// Create a bandwidth meter instance (optional)
DefaultBandwidthMeter bandwidthMeter = new DefaultBandwidthMeter.Builder(context).build();
// Build the data source factory
DataSource.Factory dataSourceFactory = MediaHelper.buildDataSourceFactory(context, bandwidthMeter);
// Use the factory to create media sources for ExoPlayer
Method: buildHttpDataSourceFactory
public static @NonNull HttpDataSource.Factory buildHttpDataSourceFactory(
@NonNull Context context,
@NonNull DefaultBandwidthMeter bandwidthMeter)
Description
Constructs an HTTP data source factory embedding a user-agent string and bandwidth metering capabilities. This factory is responsible for creating HTTP connections used to fetch media data over the network.
Parameters
context(android.content.Context): The Android context for resource access.bandwidthMeter(DefaultBandwidthMeter): A mandatory bandwidth meter used for tracking network performance during HTTP operations.
Returns
A non-null
HttpDataSource.Factoryinstance configured with a user agent and bandwidth metering.
Important Notes
The user agent string is currently hardcoded as
"TubiExoPlayer". There is a TODO comment suggesting this should be externalized into metadata or XML attributes for easier customization.
Usage Example
DefaultBandwidthMeter bandwidthMeter = new DefaultBandwidthMeter.Builder(context).build();
HttpDataSource.Factory httpDataSourceFactory = MediaHelper.buildHttpDataSourceFactory(context, bandwidthMeter);
Implementation Details and Design Notes
The class imports ExoPlayer’s upstream classes (
DataSource,DefaultBandwidthMeter,DefaultDataSourceFactory,DefaultHttpDataSourceFactory,HttpDataSource) to build layered data source factories.Bandwidth metering is integrated into both data source and HTTP data source factories to enable ExoPlayer’s adaptive streaming features.
The user agent string is generated using ExoPlayer’s utility method
Util.getUserAgent(context, "TubiExoPlayer"), which appends system and app version info.The commented-out code suggests that
MediaHelperonce managed a singleton instance holding a linked list ofMediaModelobjects and created concatenated media sources for playlist playback. This code is currently inactive and does not affect the current behavior.The utility methods are all static, emphasizing statelessness and reusability across the application.
Interaction with Other Components
Media Models: The data source factories built by
MediaHelperare typically used by media model classes (e.g.,MediaModel) to createMediaSourceinstances that ExoPlayer consumes.Playback Components: Player activities and controllers request data source factories from
MediaHelperto prepare media playback pipelines.Bandwidth Meter: The optional
DefaultBandwidthMeterpassed to these factories tracks network conditions and informs adaptive streaming logic within ExoPlayer.User Agent Configuration: Centralizing user agent creation ensures consistent HTTP headers across all media fetch requests, useful for analytics, debugging, or server-side optimizations.
Mermaid Class Diagram
classDiagram
class MediaHelper {
<<utility>>
+buildDataSourceFactory(context: Context, bandwidthMeter: DefaultBandwidthMeter?): DataSource.Factory
+buildHttpDataSourceFactory(context: Context, bandwidthMeter: DefaultBandwidthMeter): HttpDataSource.Factory
}
Summary
`MediaHelper.java` serves as a centralized utility for building ExoPlayer-compatible data source factories with integrated bandwidth metering and standardized user agent strings. Its methods facilitate seamless media stream fetching over HTTP, enabling adaptive streaming and consistent network request behavior in the media playback pipeline. Although previously considered as a media model manager, its current role focuses solely on providing these foundational factories to support other media playback components within the system.
If you are extending or maintaining the media playback system, use `MediaHelper` to obtain properly configured data source factories rather than repeatedly instantiating them manually. Consider externalizing the user agent string as noted in the TODO comment for greater flexibility.