Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit b2c1100

Browse files
committedNov 28, 2023
Add nullness check
1 parent ff515d3 commit b2c1100

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed
 

‎libraries/extractor/src/main/java/androidx/media3/extractor/ts/AdtsReader.java

+14-8
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package androidx.media3.extractor.ts;
1717

18+
import static androidx.media3.common.util.Assertions.checkNotNull;
1819
import static java.lang.Math.min;
1920

2021
import androidx.annotation.Nullable;
@@ -33,6 +34,7 @@
3334
import androidx.media3.extractor.ExtractorOutput;
3435
import androidx.media3.extractor.TrackOutput;
3536
import androidx.media3.extractor.ts.TsPayloadReader.TrackIdGenerator;
37+
import com.google.common.collect.ImmutableList;
3638
import java.util.Arrays;
3739
import java.util.Collections;
3840
import org.checkerframework.checker.nullness.qual.EnsuresNonNull;
@@ -191,6 +193,7 @@ public void consume(ParsableByteArray data) throws ParserException {
191193
}
192194
break;
193195
case STATE_READING_AAC_PCE:
196+
checkNotNull(pceBuffer);
194197
if (continueRead(data, pceBuffer.data, pceBuffer.data.length)) {
195198
readAacProgramConfigElement();
196199
}
@@ -571,8 +574,12 @@ private void parseAdtsHeader() throws ParserException {
571574
}
572575
}
573576

574-
@RequiresNonNull({"pendingOutputFormat", "pceBuffer"})
577+
@RequiresNonNull("currentOutput")
575578
void readAacProgramConfigElement() {
579+
checkNotNull(pendingOutputFormat);
580+
checkNotNull(pceBuffer);
581+
582+
576583
// See ISO 13818-7 Advanced Audio Coding (2006) Table 36 for PCE tag encoding.
577584
if (pceBuffer.readBits(3) == 5 /* PCE tag */) {
578585
// See ISO 13818-7 Advanced Audio Coding (2006) Table 25 for syntax of a PCE.
@@ -624,16 +631,15 @@ void readAacProgramConfigElement() {
624631

625632
int configSize = oldConfig.length;
626633
configSize += (numPceBits + 7) / 8 + 1; // Byte align and add a zero length comment.
627-
byte[] newConfig = new byte[configSize];
634+
byte[] newConfig = Arrays.copyOf(oldConfig, configSize);
628635

629-
System.arraycopy(oldConfig, 0, newConfig, 0, oldConfig.length);
630636
pceBuffer.setPosition(3 /* PCE tag */);
631637
pceBuffer.readBits(newConfig, oldConfig.length, numPceBits);
632638

633639
pendingOutputFormat =
634640
pendingOutputFormat
635641
.buildUpon()
636-
.setInitializationData(Collections.singletonList(newConfig))
642+
.setInitializationData(ImmutableList.of(newConfig))
637643
.build();
638644

639645
// Submit PCE-appended output format.
@@ -643,13 +649,13 @@ void readAacProgramConfigElement() {
643649
}
644650
}
645651

646-
pendingOutputFormat = null;
647-
648652
// Pass through all accumulated data as sample data.
649653
ParsableByteArray data = new ParsableByteArray(pceBuffer.data);
650-
pceBuffer = null;
651654
setReadingSampleState(currentOutput, currentSampleDuration, 0, sampleSize);
652655
readSample(data);
656+
657+
pendingOutputFormat = null;
658+
pceBuffer = null;
653659
}
654660

655661
/** Reads the rest of the sample */
@@ -669,7 +675,7 @@ private void readSample(ParsableByteArray data) {
669675

670676
@EnsuresNonNull({"output", "currentOutput", "id3Output"})
671677
private void assertTracksCreated() {
672-
Assertions.checkNotNull(output);
678+
checkNotNull(output);
673679
Util.castNonNull(currentOutput);
674680
Util.castNonNull(id3Output);
675681
}

‎libraries/extractor/src/test/java/androidx/media3/extractor/ts/AdtsReaderTest.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import static androidx.media3.extractor.ts.TsPayloadReader.FLAG_DATA_ALIGNMENT_INDICATOR;
1919
import static java.lang.Math.min;
20+
import static org.junit.Assert.assertThrows;
2021

2122
import androidx.media3.common.C;
2223
import androidx.media3.common.ParserException;
@@ -208,7 +209,7 @@ public void aacPceData() throws ParserException {
208209
adtsOutput.assertSample(0, AAC_PCE_ADTS_CONTENT, 0, C.BUFFER_FLAG_KEY_FRAME, null);
209210
}
210211

211-
@Test(expected = IllegalStateException.class)
212+
@Test
212213
public void aacPceDataFail() throws ParserException {
213214
data = new ParsableByteArray(Arrays.copyOf(AAC_PCE_TEST_DATA, AAC_PCE_TEST_DATA.length));
214215
byte[] bytes = data.getData();
@@ -218,7 +219,7 @@ public void aacPceDataFail() throws ParserException {
218219
bytes[AAC_PCE_ADTS_HEADER.length] |= 0x20;
219220

220221
// Should throw as FakeTrackOutput expects a format before sampleMetadata.
221-
feed();
222+
assertThrows(IllegalStateException.class, this::feed);
222223
}
223224

224225
private void feedLimited(int limit) throws ParserException {

0 commit comments

Comments
 (0)
Please sign in to comment.