Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set CloudFront Cookies in Exoplayer Android library for HLS streaming?

Where and how do we set cloudfront cookies for HLS streaming in Exoplayer Android library. Is it with DEFAULT COOKIES MANAGER? What is exact way to set cookies. Till now no exact solutions from any links

like image 339
Rahul Sood Avatar asked Oct 20 '25 05:10

Rahul Sood


2 Answers

Try to do set cookies in DefaultHttpDataSourceFactory.

https://github.com/google/ExoPlayer/issues/4870#issuecomment-425795374

like image 69
manish joshi Avatar answered Oct 22 '25 20:10

manish joshi


Recent versions of exoplayer is little different from the above mentioned code.here I have posted a simple code that uses a cloudfront signed HLS m3u8 as media source.

---------- Gradle -----------

def exoplayer_version = "2.17.1"
implementation "com.google.android.exoplayer:exoplayer-hls:$exoplayer_version"
implementation "com.google.android.exoplayer:exoplayer-core:$exoplayer_version"
implementation "com.google.android.exoplayer:exoplayer-ui:$exoplayer_version"

----------- XML [define a ui.PlayerView] -----------

[....]
    <com.google.android.exoplayer2.ui.PlayerView
         android:id="@+id/ep_video_view"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         app:layout_constraintBottom_toBottomOf="parent"
         app:layout_constraintEnd_toEndOf="parent"
         app:layout_constraintStart_toStartOf="parent"
         app:layout_constraintTop_toTopOf="parent" />
[....]

------------ Fragment ------------


    private var exoPlayer: ExoPlayer? = null

[...] 

        val sourceURL= "https://a.cloudfront.net/b.m3u8?Policy=eyJT..._&Signature=eZ6....__&Key-Pair-Id=KRSDW..."
// extract Policy, Signature and KeyPairIds
        val uri: Uri = Uri.parse(sourceURL)
        val policyString: String? = uri.getQueryParameter("Policy")
        val signatureString: String? = uri.getQueryParameter("Signature")
        val keyPairIdString: String? = uri.getQueryParameter("Key-Pair-Id")

// Create a cookie list to send

        var cookieValue = ""
        cookieValue += "CloudFront-Policy=$policyString;"
        cookieValue += "CloudFront-Signature=$signatureString;"
        cookieValue += "CloudFront-Key-Pair-Id=$keyPairIdString;"

// httpDataSourceFactory

        val httpDataSourceFactory: HttpDataSource.Factory =
            DefaultHttpDataSource.Factory()
                .setDefaultRequestProperties(mapOf("Cookie" to cookieValue))
                .setAllowCrossProtocolRedirects(true)

        val dataSourceFactory = DefaultDataSource.Factory(requireContext(), 
                                httpDataSourceFactory)

        val hlsMediaSource: HlsMediaSource = 
             HlsMediaSource.Factory(dataSourceFactory)
            .createMediaSource(MediaItem.fromUri(sourceURL))

        exoPlayer = ExoPlayer.Builder(requireContext()).build()

        exoPlayer.also { exoPlayer ->
            binding.epVideoView.player = exoPlayer
            exoPlayer?.addMediaSource(hlsMediaSource)
            exoPlayer?.prepare()
            exoPlayer?.playWhenReady = true
        }

[...]

Incase if you get any 403 from exoplayer, try changing the userAgent in httpDataSourceFactory

like image 21
Sandaru Avatar answered Oct 22 '25 19:10

Sandaru



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!