Skip to content

Commit

Permalink
Fix memory leak in HlsMediaChunk's
Browse files Browse the repository at this point in the history
Issue:#2319

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=145089668
  • Loading branch information
AquilesCanta authored and ojw28 committed Jan 20, 2017
1 parent 26b303a commit 6360449
Showing 1 changed file with 19 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,10 @@
private final boolean isEncrypted;
private final boolean isMasterTimestampSource;
private final TimestampAdjuster timestampAdjuster;
private final HlsMediaChunk previousChunk;
private final String lastPathSegment;
private final Extractor previousExtractor;
private final boolean shouldSpliceIn;
private final boolean needNewExtractor;

private final boolean isPackedAudio;
private final Id3Decoder id3Decoder;
Expand Down Expand Up @@ -123,21 +125,26 @@ public HlsMediaChunk(DataSource dataSource, DataSpec dataSpec, DataSpec initData
this.isMasterTimestampSource = isMasterTimestampSource;
this.timestampAdjuster = timestampAdjuster;
this.discontinuitySequenceNumber = discontinuitySequenceNumber;
this.previousChunk = previousChunk;
// Note: this.dataSource and dataSource may be different.
this.isEncrypted = this.dataSource instanceof Aes128DataSource;
lastPathSegment = dataSpec.uri.getLastPathSegment();
isPackedAudio = lastPathSegment.endsWith(AAC_FILE_EXTENSION)
|| lastPathSegment.endsWith(AC3_FILE_EXTENSION)
|| lastPathSegment.endsWith(EC3_FILE_EXTENSION)
|| lastPathSegment.endsWith(MP3_FILE_EXTENSION);
if (isPackedAudio) {
id3Decoder = previousChunk != null ? previousChunk.id3Decoder : new Id3Decoder();
id3Data = previousChunk != null ? previousChunk.id3Data
: new ParsableByteArray(Id3Decoder.ID3_HEADER_LENGTH);
if (previousChunk != null) {
id3Decoder = previousChunk.id3Decoder;
id3Data = previousChunk.id3Data;
previousExtractor = previousChunk.extractor;
shouldSpliceIn = previousChunk.hlsUrl != hlsUrl;
needNewExtractor = previousChunk.discontinuitySequenceNumber != discontinuitySequenceNumber
|| shouldSpliceIn;
} else {
id3Decoder = null;
id3Data = null;
id3Decoder = isPackedAudio ? new Id3Decoder() : null;
id3Data = isPackedAudio ? new ParsableByteArray(Id3Decoder.ID3_HEADER_LENGTH) : null;
previousExtractor = null;
shouldSpliceIn = false;
needNewExtractor = true;
}
initDataSource = dataSource;
uid = UID_SOURCE.getAndIncrement();
Expand All @@ -151,7 +158,7 @@ public HlsMediaChunk(DataSource dataSource, DataSpec dataSpec, DataSpec initData
*/
public void init(HlsSampleStreamWrapper output) {
extractorOutput = output;
output.init(uid, previousChunk != null && previousChunk.hlsUrl != hlsUrl);
output.init(uid, shouldSpliceIn);
}

@Override
Expand Down Expand Up @@ -191,8 +198,8 @@ public void load() throws IOException, InterruptedException {
// Internal loading methods.

private void maybeLoadInitData() throws IOException, InterruptedException {
if ((previousChunk != null && previousChunk.extractor == extractor) || initLoadCompleted
|| initDataSpec == null) {
if (previousExtractor == extractor || initLoadCompleted || initDataSpec == null) {
// According to spec, for packed audio, initDataSpec is expected to be null.
return;
}
DataSpec initSegmentDataSpec = Util.getRemainderDataSpec(initDataSpec, initSegmentBytesLoaded);
Expand Down Expand Up @@ -325,17 +332,14 @@ private static DataSource buildDataSource(DataSource dataSource, byte[] encrypti
private Extractor buildExtractorByExtension() {
// Set the extractor that will read the chunk.
Extractor extractor;
boolean needNewExtractor = previousChunk == null
|| previousChunk.discontinuitySequenceNumber != discontinuitySequenceNumber
|| trackFormat != previousChunk.trackFormat;
boolean usingNewExtractor = true;
if (lastPathSegment.endsWith(WEBVTT_FILE_EXTENSION)
|| lastPathSegment.endsWith(VTT_FILE_EXTENSION)) {
extractor = new WebvttExtractor(trackFormat.language, timestampAdjuster);
} else if (!needNewExtractor) {
// Only reuse TS and fMP4 extractors.
usingNewExtractor = false;
extractor = previousChunk.extractor;
extractor = previousExtractor;
} else if (lastPathSegment.endsWith(MP4_FILE_EXTENSION)) {
extractor = new FragmentedMp4Extractor(0, timestampAdjuster);
} else {
Expand Down

0 comments on commit 6360449

Please sign in to comment.