feat(player): add mpv cache and buffer configuration

This commit is contained in:
Fredrik Burmester
2026-01-26 20:46:42 +01:00
parent 21f2ceefc3
commit 1cbb46f0ca
10 changed files with 299 additions and 9 deletions

View File

@@ -300,7 +300,11 @@ final class MPVLayerRenderer {
startPosition: Double? = nil,
externalSubtitles: [String]? = nil,
initialSubtitleId: Int? = nil,
initialAudioId: Int? = nil
initialAudioId: Int? = nil,
cacheEnabled: String? = nil,
cacheSeconds: Int? = nil,
demuxerMaxBytes: Int? = nil,
demuxerMaxBackBytes: Int? = nil
) {
currentPreset = preset
currentURL = url
@@ -323,6 +327,21 @@ final class MPVLayerRenderer {
// Stop previous playback before loading new file
self.command(handle, ["stop"])
self.updateHTTPHeaders(headers)
// Apply cache/buffer settings
if let cacheMode = cacheEnabled {
self.setProperty(name: "cache", value: cacheMode)
}
if let cacheSecs = cacheSeconds {
self.setProperty(name: "cache-secs", value: String(cacheSecs))
}
if let maxBytes = demuxerMaxBytes {
self.setProperty(name: "demuxer-max-bytes", value: "\(maxBytes)MiB")
}
if let maxBackBytes = demuxerMaxBackBytes {
self.setProperty(name: "demuxer-max-back-bytes", value: "\(maxBackBytes)MiB")
}
// Set start position
if let startPos = startPosition, startPos > 0 {
self.setProperty(name: "start", value: String(format: "%.2f", startPos))

View File

@@ -29,7 +29,10 @@ public class MpvPlayerModule: Module {
guard let source = source,
let urlString = source["url"] as? String,
let videoURL = URL(string: urlString) else { return }
// Parse cache config if provided
let cacheConfig = source["cacheConfig"] as? [String: Any]
let config = VideoLoadConfig(
url: videoURL,
headers: source["headers"] as? [String: String],
@@ -37,9 +40,13 @@ public class MpvPlayerModule: Module {
startPosition: source["startPosition"] as? Double,
autoplay: (source["autoplay"] as? Bool) ?? true,
initialSubtitleId: source["initialSubtitleId"] as? Int,
initialAudioId: source["initialAudioId"] as? Int
initialAudioId: source["initialAudioId"] as? Int,
cacheEnabled: cacheConfig?["enabled"] as? String,
cacheSeconds: cacheConfig?["cacheSeconds"] as? Int,
demuxerMaxBytes: cacheConfig?["maxBytes"] as? Int,
demuxerMaxBackBytes: cacheConfig?["maxBackBytes"] as? Int
)
view.loadVideo(config: config)
}

View File

@@ -15,7 +15,12 @@ struct VideoLoadConfig {
var initialSubtitleId: Int?
/// MPV audio track ID to select on start (1-based, nil to use default)
var initialAudioId: Int?
/// Cache/buffer settings
var cacheEnabled: String? // "auto", "yes", or "no"
var cacheSeconds: Int? // Seconds of video to buffer
var demuxerMaxBytes: Int? // Max cache size in MB
var demuxerMaxBackBytes: Int? // Max backward cache size in MB
init(
url: URL,
headers: [String: String]? = nil,
@@ -23,7 +28,11 @@ struct VideoLoadConfig {
startPosition: Double? = nil,
autoplay: Bool = true,
initialSubtitleId: Int? = nil,
initialAudioId: Int? = nil
initialAudioId: Int? = nil,
cacheEnabled: String? = nil,
cacheSeconds: Int? = nil,
demuxerMaxBytes: Int? = nil,
demuxerMaxBackBytes: Int? = nil
) {
self.url = url
self.headers = headers
@@ -32,6 +41,10 @@ struct VideoLoadConfig {
self.autoplay = autoplay
self.initialSubtitleId = initialSubtitleId
self.initialAudioId = initialAudioId
self.cacheEnabled = cacheEnabled
self.cacheSeconds = cacheSeconds
self.demuxerMaxBytes = demuxerMaxBytes
self.demuxerMaxBackBytes = demuxerMaxBackBytes
}
}
@@ -151,13 +164,17 @@ class MpvPlayerView: ExpoView {
startPosition: config.startPosition,
externalSubtitles: config.externalSubtitles,
initialSubtitleId: config.initialSubtitleId,
initialAudioId: config.initialAudioId
initialAudioId: config.initialAudioId,
cacheEnabled: config.cacheEnabled,
cacheSeconds: config.cacheSeconds,
demuxerMaxBytes: config.demuxerMaxBytes,
demuxerMaxBackBytes: config.demuxerMaxBackBytes
)
if config.autoplay {
play()
}
onLoad(["url": config.url.absoluteString])
}

View File

@@ -43,6 +43,17 @@ export type VideoSource = {
initialSubtitleId?: number;
/** MPV audio track ID to select on start (1-based) */
initialAudioId?: number;
/** MPV cache/buffer configuration */
cacheConfig?: {
/** Whether caching is enabled: "auto" (default), "yes", or "no" */
enabled?: "auto" | "yes" | "no";
/** Seconds of video to buffer (default: 10, range: 5-120) */
cacheSeconds?: number;
/** Maximum cache size in MB (default: 150, range: 50-500) */
maxBytes?: number;
/** Maximum backward cache size in MB (default: 50, range: 25-200) */
maxBackBytes?: number;
};
};
export type MpvPlayerViewProps = {