Skip to content

Commit

Permalink
Add response headers to LoadEventInfo.
Browse files Browse the repository at this point in the history
The response headers of the last load are available from the loading source
when creating media source events and can be easily forwarded.

Issue:#4361
Issue:#4615

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=209900693
  • Loading branch information
tonihei authored and ojw28 committed Aug 24, 2018
1 parent 4bf5e49 commit 74e2384
Show file tree
Hide file tree
Showing 13 changed files with 88 additions and 20 deletions.
4 changes: 4 additions & 0 deletions RELEASENOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@
* Add uri field to `LoadEventInfo` in `MediaSourceEventListener` or
`AnalyticsListener` callbacks. This uri is the redirected uri if redirection
occurred ([#2054](https://github.com/google/ExoPlayer/issues/2054)).
* Add response headers field to `LoadEventInfo` in `MediaSourceEventListener` or
`AnalyticsListener` callbacks
([#4361](https://github.com/google/ExoPlayer/issues/4361) and
[#4615](https://github.com/google/ExoPlayer/issues/4615)).
* Allow `MediaCodecSelector`s to return multiple compatible decoders for
`MediaCodecRenderer`, and provide an (optional) `MediaCodecSelector` that
falls back to less preferred decoders like `MediaCodec.createDecoderByType`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,7 @@ public void onLoadCompleted(ExtractingLoadable loadable, long elapsedRealtimeMs,
eventDispatcher.loadCompleted(
loadable.dataSpec,
loadable.dataSource.getLastOpenedUri(),
loadable.dataSource.getLastResponseHeaders(),
C.DATA_TYPE_MEDIA,
C.TRACK_TYPE_UNKNOWN,
/* trackFormat= */ null,
Expand All @@ -524,6 +525,7 @@ public void onLoadCanceled(ExtractingLoadable loadable, long elapsedRealtimeMs,
eventDispatcher.loadCanceled(
loadable.dataSpec,
loadable.dataSource.getLastOpenedUri(),
loadable.dataSource.getLastResponseHeaders(),
C.DATA_TYPE_MEDIA,
C.TRACK_TYPE_UNKNOWN,
/* trackFormat= */ null,
Expand Down Expand Up @@ -570,6 +572,7 @@ public LoadErrorAction onLoadError(
eventDispatcher.loadError(
loadable.dataSpec,
loadable.dataSource.getLastOpenedUri(),
loadable.dataSource.getLastResponseHeaders(),
C.DATA_TYPE_MEDIA,
C.TRACK_TYPE_UNKNOWN,
/* trackFormat= */ null,
Expand Down Expand Up @@ -697,7 +700,6 @@ private void startLoading() {
loadable, this, loadErrorHandlingPolicy.getMinimumLoadableRetryCount(dataType));
eventDispatcher.loadStarted(
loadable.dataSpec,
loadable.dataSpec.uri,
C.DATA_TYPE_MEDIA,
C.TRACK_TYPE_UNKNOWN,
/* trackFormat= */ null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
import com.google.android.exoplayer2.upstream.DataSpec;
import com.google.android.exoplayer2.util.Assertions;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;

/** Interface for callbacks to be notified of {@link MediaSource} events. */
Expand All @@ -44,6 +47,8 @@ final class LoadEventInfo {
* after redirection.
*/
public final Uri uri;
/** The response headers associated with the load, or an empty map if unavailable. */
public final Map<String, List<String>> responseHeaders;
/** The value of {@link SystemClock#elapsedRealtime} at the time of the load event. */
public final long elapsedRealtimeMs;
/** The duration of the load up to the event time. */
Expand All @@ -58,16 +63,24 @@ final class LoadEventInfo {
* @param uri The {@link Uri} from which data is being read. The uri must be identical to the
* one in {@code dataSpec.uri} unless redirection has occurred. If redirection has occurred,
* this is the uri after redirection.
* @param responseHeaders The response headers associated with the load, or an empty map if
* unavailable.
* @param elapsedRealtimeMs The value of {@link SystemClock#elapsedRealtime} at the time of the
* load event.
* @param loadDurationMs The duration of the load up to the event time.
* @param bytesLoaded The number of bytes that were loaded up to the event time. For compressed
* network responses, this is the decompressed size.
*/
public LoadEventInfo(
DataSpec dataSpec, Uri uri, long elapsedRealtimeMs, long loadDurationMs, long bytesLoaded) {
DataSpec dataSpec,
Uri uri,
Map<String, List<String>> responseHeaders,
long elapsedRealtimeMs,
long loadDurationMs,
long bytesLoaded) {
this.dataSpec = dataSpec;
this.uri = uri;
this.responseHeaders = responseHeaders;
this.elapsedRealtimeMs = elapsedRealtimeMs;
this.loadDurationMs = loadDurationMs;
this.bytesLoaded = bytesLoaded;
Expand Down Expand Up @@ -168,7 +181,8 @@ public MediaLoadData(
* @param mediaPeriodId The {@link MediaPeriodId} this load belongs to. Null if the load does not
* belong to a specific media period.
* @param loadEventInfo The {@link LoadEventInfo} corresponding to the event. The value of {@link
* LoadEventInfo#uri} won't reflect potential redirection yet.
* LoadEventInfo#uri} won't reflect potential redirection yet and {@link
* LoadEventInfo#responseHeaders} will be empty.
* @param mediaLoadData The {@link MediaLoadData} defining the data being loaded.
*/
void onLoadStarted(
Expand Down Expand Up @@ -370,10 +384,9 @@ public void mediaPeriodReleased() {
}

/** Dispatches {@link #onLoadStarted(int, MediaPeriodId, LoadEventInfo, MediaLoadData)}. */
public void loadStarted(DataSpec dataSpec, Uri uri, int dataType, long elapsedRealtimeMs) {
public void loadStarted(DataSpec dataSpec, int dataType, long elapsedRealtimeMs) {
loadStarted(
dataSpec,
uri,
dataType,
C.TRACK_TYPE_UNKNOWN,
null,
Expand All @@ -387,7 +400,6 @@ public void loadStarted(DataSpec dataSpec, Uri uri, int dataType, long elapsedRe
/** Dispatches {@link #onLoadStarted(int, MediaPeriodId, LoadEventInfo, MediaLoadData)}. */
public void loadStarted(
DataSpec dataSpec,
Uri uri,
int dataType,
int trackType,
@Nullable Format trackFormat,
Expand All @@ -398,7 +410,12 @@ public void loadStarted(
long elapsedRealtimeMs) {
loadStarted(
new LoadEventInfo(
dataSpec, uri, elapsedRealtimeMs, /* loadDurationMs= */ 0, /* bytesLoaded= */ 0),
dataSpec,
dataSpec.uri,
/* responseHeaders= */ Collections.emptyMap(),
elapsedRealtimeMs,
/* loadDurationMs= */ 0,
/* bytesLoaded= */ 0),
new MediaLoadData(
dataType,
trackType,
Expand All @@ -423,13 +440,15 @@ public void loadStarted(LoadEventInfo loadEventInfo, MediaLoadData mediaLoadData
public void loadCompleted(
DataSpec dataSpec,
Uri uri,
Map<String, List<String>> responseHeaders,
int dataType,
long elapsedRealtimeMs,
long loadDurationMs,
long bytesLoaded) {
loadCompleted(
dataSpec,
uri,
responseHeaders,
dataType,
C.TRACK_TYPE_UNKNOWN,
null,
Expand All @@ -446,6 +465,7 @@ public void loadCompleted(
public void loadCompleted(
DataSpec dataSpec,
Uri uri,
Map<String, List<String>> responseHeaders,
int dataType,
int trackType,
@Nullable Format trackFormat,
Expand All @@ -457,7 +477,8 @@ public void loadCompleted(
long loadDurationMs,
long bytesLoaded) {
loadCompleted(
new LoadEventInfo(dataSpec, uri, elapsedRealtimeMs, loadDurationMs, bytesLoaded),
new LoadEventInfo(
dataSpec, uri, responseHeaders, elapsedRealtimeMs, loadDurationMs, bytesLoaded),
new MediaLoadData(
dataType,
trackType,
Expand All @@ -483,13 +504,15 @@ public void loadCompleted(LoadEventInfo loadEventInfo, MediaLoadData mediaLoadDa
public void loadCanceled(
DataSpec dataSpec,
Uri uri,
Map<String, List<String>> responseHeaders,
int dataType,
long elapsedRealtimeMs,
long loadDurationMs,
long bytesLoaded) {
loadCanceled(
dataSpec,
uri,
responseHeaders,
dataType,
C.TRACK_TYPE_UNKNOWN,
null,
Expand All @@ -506,6 +529,7 @@ public void loadCanceled(
public void loadCanceled(
DataSpec dataSpec,
Uri uri,
Map<String, List<String>> responseHeaders,
int dataType,
int trackType,
@Nullable Format trackFormat,
Expand All @@ -517,7 +541,8 @@ public void loadCanceled(
long loadDurationMs,
long bytesLoaded) {
loadCanceled(
new LoadEventInfo(dataSpec, uri, elapsedRealtimeMs, loadDurationMs, bytesLoaded),
new LoadEventInfo(
dataSpec, uri, responseHeaders, elapsedRealtimeMs, loadDurationMs, bytesLoaded),
new MediaLoadData(
dataType,
trackType,
Expand Down Expand Up @@ -546,6 +571,7 @@ public void loadCanceled(LoadEventInfo loadEventInfo, MediaLoadData mediaLoadDat
public void loadError(
DataSpec dataSpec,
Uri uri,
Map<String, List<String>> responseHeaders,
int dataType,
long elapsedRealtimeMs,
long loadDurationMs,
Expand All @@ -555,6 +581,7 @@ public void loadError(
loadError(
dataSpec,
uri,
responseHeaders,
dataType,
C.TRACK_TYPE_UNKNOWN,
null,
Expand All @@ -576,6 +603,7 @@ public void loadError(
public void loadError(
DataSpec dataSpec,
Uri uri,
Map<String, List<String>> responseHeaders,
int dataType,
int trackType,
@Nullable Format trackFormat,
Expand All @@ -589,7 +617,8 @@ public void loadError(
IOException error,
boolean wasCanceled) {
loadError(
new LoadEventInfo(dataSpec, uri, elapsedRealtimeMs, loadDurationMs, bytesLoaded),
new LoadEventInfo(
dataSpec, uri, responseHeaders, elapsedRealtimeMs, loadDurationMs, bytesLoaded),
new MediaLoadData(
dataType,
trackType,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,6 @@ public boolean continueLoading(long positionUs) {
loadErrorHandlingPolicy.getMinimumLoadableRetryCount(C.DATA_TYPE_MEDIA));
eventDispatcher.loadStarted(
dataSpec,
dataSpec.uri,
C.DATA_TYPE_MEDIA,
C.TRACK_TYPE_UNKNOWN,
format,
Expand Down Expand Up @@ -211,6 +210,7 @@ public void onLoadCompleted(SourceLoadable loadable, long elapsedRealtimeMs,
eventDispatcher.loadCompleted(
loadable.dataSpec,
loadable.dataSource.getLastOpenedUri(),
loadable.dataSource.getLastResponseHeaders(),
C.DATA_TYPE_MEDIA,
C.TRACK_TYPE_UNKNOWN,
format,
Expand All @@ -229,6 +229,7 @@ public void onLoadCanceled(SourceLoadable loadable, long elapsedRealtimeMs, long
eventDispatcher.loadCanceled(
loadable.dataSpec,
loadable.dataSource.getLastOpenedUri(),
loadable.dataSource.getLastResponseHeaders(),
C.DATA_TYPE_MEDIA,
C.TRACK_TYPE_UNKNOWN,
/* trackFormat= */ null,
Expand Down Expand Up @@ -269,6 +270,7 @@ public LoadErrorAction onLoadError(
eventDispatcher.loadError(
loadable.dataSpec,
loadable.dataSource.getLastOpenedUri(),
loadable.dataSource.getLastResponseHeaders(),
C.DATA_TYPE_MEDIA,
C.TRACK_TYPE_UNKNOWN,
format,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import java.lang.annotation.RetentionPolicy;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -556,6 +557,7 @@ public void onAdLoadError(final AdLoadException error, DataSpec dataSpec) {
.loadError(
dataSpec,
dataSpec.uri,
/* responseHeaders= */ Collections.emptyMap(),
C.DATA_TYPE_AD,
C.TRACK_TYPE_UNKNOWN,
/* loadDurationMs= */ 0,
Expand Down Expand Up @@ -595,6 +597,7 @@ public void onPrepareError(MediaPeriodId mediaPeriodId, final IOException except
.loadError(
new DataSpec(adUri),
adUri,
/* responseHeaders= */ Collections.emptyMap(),
C.DATA_TYPE_AD,
C.TRACK_TYPE_UNKNOWN,
/* loadDurationMs= */ 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,7 @@ public void onLoadCompleted(Chunk loadable, long elapsedRealtimeMs, long loadDur
eventDispatcher.loadCompleted(
loadable.dataSpec,
loadable.getUri(),
loadable.getResponseHeaders(),
loadable.type,
primaryTrackType,
loadable.trackFormat,
Expand All @@ -451,6 +452,7 @@ public void onLoadCanceled(Chunk loadable, long elapsedRealtimeMs, long loadDura
eventDispatcher.loadCanceled(
loadable.dataSpec,
loadable.getUri(),
loadable.getResponseHeaders(),
loadable.type,
primaryTrackType,
loadable.trackFormat,
Expand Down Expand Up @@ -518,6 +520,7 @@ public LoadErrorAction onLoadError(
eventDispatcher.loadError(
loadable.dataSpec,
loadable.getUri(),
loadable.getResponseHeaders(),
loadable.type,
primaryTrackType,
loadable.trackFormat,
Expand Down Expand Up @@ -585,7 +588,6 @@ public boolean continueLoading(long positionUs) {
loadable, this, loadErrorHandlingPolicy.getMinimumLoadableRetryCount(loadable.type));
eventDispatcher.loadStarted(
loadable.dataSpec,
loadable.dataSpec.uri,
loadable.type,
primaryTrackType,
loadable.trackFormat,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import com.google.android.exoplayer2.util.Util;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

/**
* A {@link Loadable} for objects that can be parsed from binary data using a {@link Parser}.
Expand Down Expand Up @@ -132,6 +134,14 @@ public Uri getUri() {
return dataSource.getLastOpenedUri();
}

/**
* Returns the response headers associated with the load. Must only be called after the load
* completed, failed, or was canceled.
*/
public Map<String, List<String>> getResponseHeaders() {
return dataSource.getLastResponseHeaders();
}

@Override
public final void cancelLoad() {
// Do nothing.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -709,6 +709,7 @@ public void releaseSourceInternal() {
manifestEventDispatcher.loadCompleted(
loadable.dataSpec,
loadable.getUri(),
loadable.getResponseHeaders(),
loadable.type,
elapsedRealtimeMs,
loadDurationMs,
Expand Down Expand Up @@ -800,6 +801,7 @@ public void releaseSourceInternal() {
manifestEventDispatcher.loadError(
loadable.dataSpec,
loadable.getUri(),
loadable.getResponseHeaders(),
loadable.type,
elapsedRealtimeMs,
loadDurationMs,
Expand All @@ -814,6 +816,7 @@ public void releaseSourceInternal() {
manifestEventDispatcher.loadCompleted(
loadable.dataSpec,
loadable.getUri(),
loadable.getResponseHeaders(),
loadable.type,
elapsedRealtimeMs,
loadDurationMs,
Expand All @@ -829,6 +832,7 @@ public void releaseSourceInternal() {
manifestEventDispatcher.loadError(
loadable.dataSpec,
loadable.getUri(),
loadable.getResponseHeaders(),
loadable.type,
elapsedRealtimeMs,
loadDurationMs,
Expand All @@ -844,6 +848,7 @@ public void releaseSourceInternal() {
manifestEventDispatcher.loadCanceled(
loadable.dataSpec,
loadable.getUri(),
loadable.getResponseHeaders(),
loadable.type,
elapsedRealtimeMs,
loadDurationMs,
Expand Down Expand Up @@ -1031,8 +1036,7 @@ private long getManifestLoadRetryDelayMillis() {
private <T> void startLoading(ParsingLoadable<T> loadable,
Loader.Callback<ParsingLoadable<T>> callback, int minRetryCount) {
long elapsedRealtimeMs = loader.startLoading(loadable, callback, minRetryCount);
manifestEventDispatcher.loadStarted(
loadable.dataSpec, loadable.dataSpec.uri, loadable.type, elapsedRealtimeMs);
manifestEventDispatcher.loadStarted(loadable.dataSpec, loadable.type, elapsedRealtimeMs);
}

private long getNowUnixTimeUs() {
Expand Down
Loading

0 comments on commit 74e2384

Please sign in to comment.