Skip to content

Commit

Permalink
Add CMCD logging when requesting initialization chunk for DASH and HLS
Browse files Browse the repository at this point in the history
Additionally, two existing methods to `buildDataSpec` in `DashUtil` have been deprecated, while a new method has been added that allows the inclusion of `httpRequestHeaders`.

Issue: #8699

#minor-release

PiperOrigin-RevId: 540594444
(cherry picked from commit c69b6fe)
  • Loading branch information
rohitjoins authored and tianyif committed Jun 15, 2023
1 parent 593590c commit fd06061
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@
import com.google.android.exoplayer2.upstream.ParsingLoadable;
import com.google.android.exoplayer2.util.Assertions;
import com.google.android.exoplayer2.util.MimeTypes;
import com.google.common.collect.ImmutableMap;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
* Utility methods for DASH streams.
Expand All @@ -58,33 +60,47 @@ public final class DashUtil {
* @param requestUri The {@link RangedUri} of the data to request.
* @param flags Flags to be set on the returned {@link DataSpec}. See {@link
* DataSpec.Builder#setFlags(int)}.
* @param httpRequestHeaders The {@link DataSpec#httpRequestHeaders}.
* @return The {@link DataSpec}.
*/
public static DataSpec buildDataSpec(
Representation representation, String baseUrl, RangedUri requestUri, int flags) {
Representation representation,
String baseUrl,
RangedUri requestUri,
int flags,
Map<String, String> httpRequestHeaders) {
return new DataSpec.Builder()
.setUri(requestUri.resolveUri(baseUrl))
.setPosition(requestUri.start)
.setLength(requestUri.length)
.setKey(resolveCacheKey(representation, requestUri))
.setFlags(flags)
.setHttpRequestHeaders(httpRequestHeaders)
.build();
}

/**
* Builds a {@link DataSpec} for a given {@link RangedUri} belonging to {@link Representation}.
*
* <p>Uses the first base URL of the representation to build the data spec.
*
* @param representation The {@link Representation} to which the request belongs.
* @param requestUri The {@link RangedUri} of the data to request.
* @param flags Flags to be set on the returned {@link DataSpec}. See {@link
* DataSpec.Builder#setFlags(int)}.
* @return The {@link DataSpec}.
* @deprecated Use {@link #buildDataSpec(Representation, String, RangedUri, int, Map)} instead.
*/
@Deprecated
public static DataSpec buildDataSpec(
Representation representation, String baseUrl, RangedUri requestUri, int flags) {
return buildDataSpec(
representation, baseUrl, requestUri, flags, /* httpRequestHeaders= */ ImmutableMap.of());
}

/**
* @deprecated Use {@link #buildDataSpec(Representation, String, RangedUri, int, Map)} instead.
*/
@Deprecated
public static DataSpec buildDataSpec(
Representation representation, RangedUri requestUri, int flags) {
return buildDataSpec(representation, representation.baseUrls.get(0).url, requestUri, flags);
return buildDataSpec(
representation,
representation.baseUrls.get(0).url,
requestUri,
flags,
/* httpRequestHeaders= */ ImmutableMap.of());
}

/**
Expand Down Expand Up @@ -298,7 +314,8 @@ private static void loadInitializationData(
representation,
representation.baseUrls.get(baseUrlIndex).url,
requestUri,
/* flags= */ 0);
/* flags= */ 0,
/* httpRequestHeaders= */ ImmutableMap.of());
InitializationChunk initializationChunk =
new InitializationChunk(
dataSource,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,13 @@ public void getNextChunk(
trackSelection.updateSelectedTrack(
playbackPositionUs, bufferedDurationUs, availableLiveDurationUs, queue, chunkIterators);

@Nullable
CmcdLog cmcdLog =
cmcdConfiguration == null
? null
: CmcdLog.createInstance(
cmcdConfiguration, trackSelection, playbackPositionUs, loadPositionUs);

RepresentationHolder representationHolder =
updateSelectedBaseUrl(trackSelection.getSelectedIndex());
if (representationHolder.chunkExtractor != null) {
Expand All @@ -398,7 +405,8 @@ public void getNextChunk(
trackSelection.getSelectionReason(),
trackSelection.getSelectionData(),
pendingInitializationUri,
pendingIndexUri);
pendingIndexUri,
cmcdLog);
return;
}
}
Expand Down Expand Up @@ -451,13 +459,6 @@ public void getNextChunk(
}
}

@Nullable
CmcdLog cmcdLog =
cmcdConfiguration == null
? null
: CmcdLog.createInstance(
cmcdConfiguration, trackSelection, playbackPositionUs, loadPositionUs);

long seekTimeUs = queue.isEmpty() ? loadPositionUs : C.TIME_UNSET;
out.chunk =
newMediaChunk(
Expand Down Expand Up @@ -645,7 +646,8 @@ protected Chunk newInitializationChunk(
@C.SelectionReason int trackSelectionReason,
@Nullable Object trackSelectionData,
@Nullable RangedUri initializationUri,
@Nullable RangedUri indexUri) {
@Nullable RangedUri indexUri,
@Nullable CmcdLog cmcdLog) {
Representation representation = representationHolder.representation;
@Nullable RangedUri requestUri;
if (initializationUri != null) {
Expand All @@ -659,9 +661,15 @@ protected Chunk newInitializationChunk(
} else {
requestUri = indexUri;
}
ImmutableMap<@CmcdConfiguration.HeaderKey String, String> httpRequestHeaders =
cmcdLog == null ? ImmutableMap.of() : cmcdLog.getHttpRequestHeaders();
DataSpec dataSpec =
DashUtil.buildDataSpec(
representation, representationHolder.selectedBaseUrl.url, requestUri, /* flags= */ 0);
representation,
representationHolder.selectedBaseUrl.url,
requestUri,
/* flags= */ 0,
httpRequestHeaders);
return new InitializationChunk(
dataSource,
dataSpec,
Expand Down Expand Up @@ -697,8 +705,11 @@ protected Chunk newMediaChunk(
: DataSpec.FLAG_MIGHT_NOT_USE_FULL_NETWORK_SPEED;
DataSpec dataSpec =
DashUtil.buildDataSpec(
representation, representationHolder.selectedBaseUrl.url, segmentUri, flags);
dataSpec = dataSpec.buildUpon().setHttpRequestHeaders(httpRequestHeaders).build();
representation,
representationHolder.selectedBaseUrl.url,
segmentUri,
flags,
httpRequestHeaders);
return new SingleSampleMediaChunk(
dataSource,
dataSpec,
Expand Down Expand Up @@ -737,8 +748,11 @@ protected Chunk newMediaChunk(
: DataSpec.FLAG_MIGHT_NOT_USE_FULL_NETWORK_SPEED;
DataSpec dataSpec =
DashUtil.buildDataSpec(
representation, representationHolder.selectedBaseUrl.url, segmentUri, flags);
dataSpec = dataSpec.buildUpon().setHttpRequestHeaders(httpRequestHeaders).build();
representation,
representationHolder.selectedBaseUrl.url,
segmentUri,
flags,
httpRequestHeaders);
long sampleOffsetUs = -representation.presentationTimeOffsetUs;
return new ContainerMediaChunk(
dataSource,
Expand Down Expand Up @@ -809,7 +823,8 @@ public DataSpec getDataSpec() {
representationHolder.representation,
representationHolder.selectedBaseUrl.url,
segmentUri,
flags);
flags,
/* httpRequestHeaders= */ ImmutableMap.of());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import com.google.android.exoplayer2.upstream.cache.CacheDataSource;
import com.google.android.exoplayer2.util.RunnableFutureTask;
import com.google.android.exoplayer2.util.Util;
import com.google.common.collect.ImmutableMap;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -231,7 +232,13 @@ private void addSegmentsForAdaptationSet(

private Segment createSegment(
Representation representation, String baseUrl, long startTimeUs, RangedUri rangedUri) {
DataSpec dataSpec = DashUtil.buildDataSpec(representation, baseUrl, rangedUri, /* flags= */ 0);
DataSpec dataSpec =
DashUtil.buildDataSpec(
representation,
baseUrl,
rangedUri,
/* flags= */ 0,
/* httpRequestHeaders= */ ImmutableMap.of());
return new Segment(startTimeUs, dataSpec);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import com.google.android.exoplayer2.util.UriUtil;
import com.google.android.exoplayer2.util.Util;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterables;
import com.google.common.primitives.Ints;
import java.io.IOException;
Expand Down Expand Up @@ -486,17 +487,24 @@ public void getNextChunk(
seenExpectedPlaylistError = false;
expectedPlaylistUrl = null;

@Nullable
CmcdLog cmcdLog =
cmcdConfiguration == null
? null
: CmcdLog.createInstance(
cmcdConfiguration, trackSelection, playbackPositionUs, loadPositionUs);

// Check if the media segment or its initialization segment are fully encrypted.
@Nullable
Uri initSegmentKeyUri =
getFullEncryptionKeyUri(playlist, segmentBaseHolder.segmentBase.initializationSegment);
out.chunk = maybeCreateEncryptionChunkFor(initSegmentKeyUri, selectedTrackIndex);
out.chunk = maybeCreateEncryptionChunkFor(initSegmentKeyUri, selectedTrackIndex, cmcdLog);
if (out.chunk != null) {
return;
}
@Nullable
Uri mediaSegmentKeyUri = getFullEncryptionKeyUri(playlist, segmentBaseHolder.segmentBase);
out.chunk = maybeCreateEncryptionChunkFor(mediaSegmentKeyUri, selectedTrackIndex);
out.chunk = maybeCreateEncryptionChunkFor(mediaSegmentKeyUri, selectedTrackIndex, cmcdLog);
if (out.chunk != null) {
return;
}
Expand All @@ -512,13 +520,6 @@ public void getNextChunk(
return;
}

@Nullable
CmcdLog cmcdLog =
cmcdConfiguration == null
? null
: CmcdLog.createInstance(
cmcdConfiguration, trackSelection, playbackPositionUs, loadPositionUs);

out.chunk =
HlsMediaChunk.createInstance(
extractorFactory,
Expand Down Expand Up @@ -846,7 +847,8 @@ private void updateLiveEdgeTimeUs(HlsMediaPlaylist mediaPlaylist) {
}

@Nullable
private Chunk maybeCreateEncryptionChunkFor(@Nullable Uri keyUri, int selectedTrackIndex) {
private Chunk maybeCreateEncryptionChunkFor(
@Nullable Uri keyUri, int selectedTrackIndex, @Nullable CmcdLog cmcdLog) {
if (keyUri == null) {
return null;
}
Expand All @@ -859,8 +861,14 @@ private Chunk maybeCreateEncryptionChunkFor(@Nullable Uri keyUri, int selectedTr
keyCache.put(keyUri, encryptionKey);
return null;
}
ImmutableMap<@CmcdConfiguration.HeaderKey String, String> httpRequestHeaders =
cmcdLog == null ? ImmutableMap.of() : cmcdLog.getHttpRequestHeaders();
DataSpec dataSpec =
new DataSpec.Builder().setUri(keyUri).setFlags(DataSpec.FLAG_ALLOW_GZIP).build();
new DataSpec.Builder()
.setUri(keyUri)
.setFlags(DataSpec.FLAG_ALLOW_GZIP)
.setHttpRequestHeaders(httpRequestHeaders)
.build();
return new EncryptionKeyChunk(
encryptionDataSource,
dataSpec,
Expand Down

0 comments on commit fd06061

Please sign in to comment.