Exoplayer: Add/Parse additional headers to/from HTTP request HLS

Created on 27 Oct 2016  路  3Comments  路  Source: google/ExoPlayer

Hey I'm very new to ExoPlayer, and I'm trying to play HLS stream from my app.

Here's what I'm trying to do:

The client (with uses ExoPlayer) sends HTTP request server with addition headers or some contents so the server can verify it. (How can I add it?)

The server verifies the request and add additional fields to the HTTP response. (How do I pre-process the response before ExoPlayer processes it?)

I'm using ExoPlayer r2.0.3.
Thanks

question

Most helpful comment

In the latest dev-v2 branch you can set headers directly on the DefaultHttpDataSourceFactory that you're building in your code, like:

```
DefaultHttpDataSourceFactory factory = new DefaultHttpDataSourceFactory(.....);
factory.setDefaultRequestProperty(key, value);
return factory;
````

This feature will be available in 2.2.0 (scheduled for release this week) and above.

All 3 comments

Here's the code I used for preparing the ExoPlayer (got it from the tutorial on the ExoPlayer website):

`

// 1. Create a default TrackSelector
mainHandler = new Handler();
TrackSelection.Factory videoTrackSelectionFactory =
        new AdaptiveVideoTrackSelection.Factory(BANDWIDTH_METER);
trackSelector =
        new DefaultTrackSelector(mainHandler, videoTrackSelectionFactory);

// 2. Create a default LoadControl
LoadControl loadControl = new DefaultLoadControl();

// 3. Create the player
player =
        ExoPlayerFactory.newSimpleInstance(this, trackSelector, loadControl);

mediaDataSourceFactory = buildDataSourceFactory(true);
eventLogger = new EventLogger();

playerControl = new PlayerControl(player);

connectButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        HlsMediaSource hlsMediaSource = new HlsMediaSource(
                Uri.parse("http://" + ipText.getText().toString() + ":8080/output.m3u8"),
                mediaDataSourceFactory,
                mainHandler,
                eventLogger
        );
        player.prepare(hlsMediaSource);
        clientControlTask = new ClientControlTask(ipText.getText().toString(), 8090);
        clientControlTask.execute();
    }
});

private DataSource.Factory buildDataSourceFactory(DefaultBandwidthMeter bandwidthMeter) {
    return new DefaultDataSourceFactory(this, bandwidthMeter,
            buildHttpDataSourceFactory(bandwidthMeter));
}

private HttpDataSource.Factory buildHttpDataSourceFactory(DefaultBandwidthMeter bandwidthMeter) {
    DefaultHttpDataSourceFactory factory = new DefaultHttpDataSourceFactory(userAgent, bandwidthMeter);
    return factory;
}

private DataSource.Factory buildDataSourceFactory(boolean useBandwidthMeter) {
    return buildDataSourceFactory(useBandwidthMeter ? BANDWIDTH_METER : null);
}

`

If you check out the full library project you can make modifications to DefaultHttpDataSource.java.

In the latest dev-v2 branch you can set headers directly on the DefaultHttpDataSourceFactory that you're building in your code, like:

```
DefaultHttpDataSourceFactory factory = new DefaultHttpDataSourceFactory(.....);
factory.setDefaultRequestProperty(key, value);
return factory;
````

This feature will be available in 2.2.0 (scheduled for release this week) and above.

Was this page helpful?
0 / 5 - 0 ratings