How SmartTube Manages Video Quality Presets: A Complete Technical Guide
SmartTube manages video quality presets through a static array of VideoPreset objects defined in AppDataSourceManager, filters them by device capability using Utils.isPresetSupported(), renders them via AppDialogUtil.createVideoPresetsCategory(), and applies them to ExoPlayer by parsing spec strings into track selection parameters.
SmartTube, the popular open-source YouTube client for Android TV developed by yuliskov, implements a sophisticated video quality preset system that allows users to force specific resolutions, frame rates, and codecs. The architecture cleanly separates preset definition, hardware validation, UI presentation, and playback integration to ensure users only see compatible options while maintaining full control over the streaming experience.
How SmartTube Defines Video Quality Presets
The VideoPreset Data Structure
At the core of the system is the VideoPreset class, an inner class of FormatItem that acts as a lightweight container for display names and technical specifications. Each preset holds a human-readable label and a machine-parseable format string.
// From FormatItem.java - VideoPreset inner class
public VideoPreset(String presetName, String presetSpec) {
this.name = presetName;
this.format = presetSpec;
}
The format spec uses a comma-delimited structure: width,height,fps,codec[.hdr]. For example, "1280,720,60,av01.hdr" represents 720p resolution at 60 frames per second using the AV1 codec with HDR enabled.
Static Preset Definitions in AppDataSourceManager
The complete catalog of available presets lives in AppDataSourceManager.java (lines 82-142), where a static array defines every supported combination from 144p 30fps AVC to 4K 60fps AV1 HDR.
// AppDataSourceManager.java - lines 82-142
public VideoPreset[] getVideoPresets() {
return new VideoPreset[] {
new VideoPreset("720p 60fps av01+hdr", "1280,720,60,av01.hdr"),
new VideoPreset("1080p 60fps av01+hdr", "1920,1080,60,av01.hdr"),
new VideoPreset("4K 30fps av01+hdr", "3840,2160,30,av01.hdr"),
new VideoPreset("4K 60fps av01+hdr", "3840,2160,60,av01.hdr"),
// ... additional presets
};
}
This centralized definition makes it straightforward to add new formats as hardware capabilities evolve or YouTube expands codec support.
Filtering Presets by Device Capability
Before displaying presets to users, SmartTube validates hardware compatibility to prevent selection of unsupported formats. The Utils.isPresetSupported() method (lines 833-838 in Utils.java) checks whether the device can decode VP9 or AV1 at the requested resolution.
// Utils.java - lines 833-838
public static boolean isPresetSupported(VideoPreset preset) {
// Validates VP9/AV1 codec support and resolution limits
// Returns false if device cannot render the specified format
return isCodecSupported(preset.format) &&
isResolutionSupported(preset.format);
}
This filtering occurs during UI generation, ensuring that budget Android TV devices only see AVC (H.264) options while high-end streamers get access to AV1 and VP9 selections.
Rendering the Preset Selection UI
Building the Settings Dialog
The AppDialogUtil class provides the bridge between data and presentation. The createVideoPresetsCategory() method (lines 261-289 in AppDialogUtil.java) constructs a settings category that integrates with the app's preference system.
// AppDialogUtil.java - lines 261-289
public static OptionCategory createVideoPresetsCategory(...) {
List<OptionItem> options = fromPresets(presets, playerData, onSelected);
return OptionCategory.from(PRESETS_ID, R.string.video_presets, options);
}
Creating Selectable Options
The helper method fromPresets() iterates over the filtered preset array and generates OptionItem instances with selection callbacks. Each item triggers the format application when tapped.
// Inside fromPresets() - AppDialogUtil.java
OptionItem item = UiOptionItem.from(
preset.name,
option -> setFormat(preset.format, playerData, onFormatSelected),
isSelected
);
Both PlayerSettingsPresenter and HQDialogController utilize this utility to inject preset selection into the Settings screen and the "High-Quality" quick dialog respectively.
Applying a Preset to ExoPlayer
Parsing the Spec String
When a user selects a preset, the system converts the format string into an ExoFormatItem through the fromVideoSpec() factory method. This parses the comma-separated values into structured width, height, frame rate, codec, and HDR parameters.
// Example: Selecting "720p 60fps av01 HDR"
String presetSpec = "1280,720,60,av01.hdr";
ExoFormatItem format = ExoFormatItem.fromVideoSpec(presetSpec, true);
Forcing the Track Selection
The parsed format overrides the ExoPlayer track selector parameters, forcing the renderer to choose streams matching the preset criteria rather than adaptive bitrate selection.
// Inside setFormat() callback - AppDialogUtil.java line 293
player.getTrackSelector().setParameters(
player.getTrackSelector().buildUponParameters()
.setPreferredVideoFormat(format.toExoFormat())
);
This mechanism works identically whether applied through user interaction or automatically during app startup.
Persisting User Selection
SmartTube stores the selected preset index using the VIDEO_PRESET_INDEX preference key. On application initialization, the player reads this saved index, retrieves the corresponding preset from AppDataSourceManager, and executes the same setFormat() path to restore the user's preferred quality settings without requiring manual reselection.
Summary
- Centralized Definitions: All video quality presets are defined as static
VideoPresetobjects inAppDataSourceManager.java(lines 82-142) using comma-separated spec strings. - Hardware Validation: The
Utils.isPresetSupported()method filters presets by checking VP9/AV1 decoding capabilities at specific resolutions. - UI Generation:
AppDialogUtil.createVideoPresetsCategory()generates selectable options that appear in bothPlayerSettingsPresenterandHQDialogController. - Format Application: Preset strings are parsed by
ExoFormatItem.fromVideoSpec()and applied to ExoPlayer's track selector to force specific video formats. - State Persistence: Selected presets are saved via the
VIDEO_PRESET_INDEXpreference and automatically applied on app startup.
Frequently Asked Questions
How does SmartTube prevent users from selecting unsupported 4K or AV1 presets?
SmartTube validates device capabilities through Utils.isPresetSupported() (lines 833-838 in Utils.java) before displaying options. This method checks whether the Android TV hardware can decode VP9 or AV1 codecs at the requested resolution, filtering out impossible formats such as 4K AV1 on devices with limited graphics pipelines.
What happens when I select a video quality preset in the settings?
When selected, the preset string (e.g., "3840,2160,60,av01.hdr") is parsed by ExoFormatItem.fromVideoSpec() into a structured format object. This is passed to ExoPlayer's DefaultTrackSelector via setPreferredVideoFormat(), which forces the player to select video tracks matching your specified resolution, frame rate, codec, and HDR settings rather than using adaptive streaming.
Where are the video quality presets defined in the source code?
All presets are statically defined in AppDataSourceManager.java between lines 82 and 142 as an array of VideoPreset objects. Each entry combines a display name (like "4K 60fps av01+hdr") with a technical specification string that ExoPlayer parses to configure the video renderer.
Can I add custom video quality presets to SmartTube?
While the preset array is static in AppDataSourceManager.java, developers can extend the getVideoPresets() method to include additional VideoPreset entries following the width,height,fps,codec[.hdr] format. However, unsupported combinations will be filtered out by Utils.isPresetSupported() unless the device hardware capabilities are also satisfied.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →