fixed header issues

This commit is contained in:
tapframe 2025-12-23 21:31:10 +05:30
parent 811701ebae
commit 6855a89792
3 changed files with 37 additions and 19 deletions

View file

@ -15,6 +15,7 @@ import com.facebook.react.defaults.DefaultReactNativeHost
import expo.modules.ApplicationLifecycleDispatcher import expo.modules.ApplicationLifecycleDispatcher
import expo.modules.ReactNativeHostWrapper import expo.modules.ReactNativeHostWrapper
import com.nuvio.app.mpv.MpvPackage
class MainApplication : Application(), ReactApplication { class MainApplication : Application(), ReactApplication {

View file

@ -51,8 +51,8 @@ class MPVView @JvmOverloads constructor(
isMpvInitialized = true isMpvInitialized = true
// If a data source was set before surface was ready, load it now // If a data source was set before surface was ready, load it now
// Headers are already applied in initOptions() before init()
pendingDataSource?.let { url -> pendingDataSource?.let { url ->
applyHttpHeaders()
loadFile(url) loadFile(url)
pendingDataSource = null pendingDataSource = null
} }
@ -93,9 +93,11 @@ class MPVView @JvmOverloads constructor(
MPVLib.setOptionString("gpu-context", "android") MPVLib.setOptionString("gpu-context", "android")
MPVLib.setOptionString("opengl-es", "yes") MPVLib.setOptionString("opengl-es", "yes")
// Hardware decoding - use mediacodec-copy to allow subtitle overlay // Hardware decoding configuration
// 'mediacodec-copy' copies frames to CPU memory which enables subtitle blending // NOTE: On emulator, mediacodec can cause freezes due to slow GPU translation
MPVLib.setOptionString("hwdec", "auto") // Using 'no' for software decoding which is more reliable on emulator
// For real devices, use 'mediacodec-copy' for hardware acceleration
MPVLib.setOptionString("hwdec", "no")
MPVLib.setOptionString("hwdec-codecs", "all") MPVLib.setOptionString("hwdec-codecs", "all")
// Audio output // Audio output
@ -110,6 +112,20 @@ class MPVView @JvmOverloads constructor(
// Network options // Network options
MPVLib.setOptionString("network-timeout", "60") // 60 second timeout MPVLib.setOptionString("network-timeout", "60") // 60 second timeout
// CRITICAL: Disable youtube-dl/yt-dlp hook
// The ytdl_hook incorrectly tries to parse HLS/direct URLs through youtube-dl
// which fails on Android since yt-dlp is not available, causing playback failure
MPVLib.setOptionString("ytdl", "no")
// CRITICAL: HTTP headers MUST be set as options before init()
// Apply headers if they were set before surface initialization
applyHttpHeadersAsOptions()
// FFmpeg HTTP protocol options for better compatibility
MPVLib.setOptionString("tls-verify", "no") // Disable TLS cert verification
MPVLib.setOptionString("http-reconnect", "yes") // Auto-reconnect on network issues
MPVLib.setOptionString("stream-reconnect", "yes") // Reconnect if stream drops
// Subtitle configuration - CRITICAL for Android // Subtitle configuration - CRITICAL for Android
MPVLib.setOptionString("sub-auto", "fuzzy") // Auto-load subtitles MPVLib.setOptionString("sub-auto", "fuzzy") // Auto-load subtitles
MPVLib.setOptionString("sub-visibility", "yes") // Make subtitles visible by default MPVLib.setOptionString("sub-visibility", "yes") // Make subtitles visible by default
@ -184,8 +200,7 @@ class MPVView @JvmOverloads constructor(
fun setDataSource(url: String) { fun setDataSource(url: String) {
if (isMpvInitialized) { if (isMpvInitialized) {
// Apply headers before loading the file // Headers were already set during initialization in initOptions()
applyHttpHeaders()
loadFile(url) loadFile(url)
} else { } else {
pendingDataSource = url pendingDataSource = url
@ -197,13 +212,22 @@ class MPVView @JvmOverloads constructor(
Log.d(TAG, "Headers set: $headers") Log.d(TAG, "Headers set: $headers")
} }
private fun applyHttpHeaders() { private fun applyHttpHeadersAsOptions() {
// Always set user-agent (this works reliably)
val userAgent = httpHeaders?.get("User-Agent")
?: "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
Log.d(TAG, "Setting User-Agent: $userAgent")
MPVLib.setOptionString("user-agent", userAgent)
// Additionally, set other headers via http-header-fields if present
// This is needed for streams that require Referer, Origin, Cookie, etc.
httpHeaders?.let { headers -> httpHeaders?.let { headers ->
if (headers.isNotEmpty()) { val otherHeaders = headers.filterKeys { it != "User-Agent" }
// Format headers for MPV: comma-separated "Key: Value" pairs if (otherHeaders.isNotEmpty()) {
val headerList = headers.map { (key, value) -> "$key: $value" } // Format as comma-separated "Key: Value" pairs
val headerString = headerList.joinToString(",") val headerString = otherHeaders.map { (key, value) -> "$key: $value" }.joinToString(",")
Log.d(TAG, "Applying HTTP headers: $headerString") Log.d(TAG, "Setting additional headers: $headerString")
MPVLib.setOptionString("http-header-fields", headerString) MPVLib.setOptionString("http-header-fields", headerString)
} }
} }

View file

@ -83,13 +83,6 @@
"username": "nayifleo" "username": "nayifleo"
} }
], ],
[
"expo-libvlc-player",
{
"localNetworkPermission": "Allow $(PRODUCT_NAME) to access your local network",
"supportsBackgroundPlayback": true
}
],
"react-native-bottom-tabs", "react-native-bottom-tabs",
[ [
"react-native-google-cast", "react-native-google-cast",