15
15
*/
16
16
package com .google .android .exoplayer2 .source ;
17
17
18
+ import static com .google .android .exoplayer2 .C .BUFFER_FLAG_ENCRYPTED ;
18
19
import static com .google .android .exoplayer2 .C .BUFFER_FLAG_KEY_FRAME ;
19
20
import static com .google .android .exoplayer2 .C .RESULT_BUFFER_READ ;
20
21
import static com .google .android .exoplayer2 .C .RESULT_FORMAT_READ ;
21
22
import static com .google .android .exoplayer2 .C .RESULT_NOTHING_READ ;
22
23
import static com .google .common .truth .Truth .assertThat ;
23
24
import static java .lang .Long .MIN_VALUE ;
24
25
import static java .util .Arrays .copyOfRange ;
26
+ import static org .junit .Assert .assertArrayEquals ;
25
27
import static org .mockito .Mockito .when ;
26
28
27
29
import androidx .annotation .Nullable ;
@@ -114,17 +116,13 @@ public final class SampleQueueTest {
114
116
C .BUFFER_FLAG_KEY_FRAME , C .BUFFER_FLAG_ENCRYPTED , 0 , C .BUFFER_FLAG_ENCRYPTED ,
115
117
};
116
118
private static final long [] ENCRYPTED_SAMPLE_TIMESTAMPS = new long [] {0 , 1000 , 2000 , 3000 };
117
- private static final Format [] ENCRYPTED_SAMPLES_FORMATS =
119
+ private static final Format [] ENCRYPTED_SAMPLE_FORMATS =
118
120
new Format [] {FORMAT_ENCRYPTED , FORMAT_ENCRYPTED , FORMAT_1 , FORMAT_ENCRYPTED };
119
121
/** Encrypted samples require the encryption preamble. */
120
- private static final int [] ENCRYPTED_SAMPLES_SIZES = new int [] {1 , 3 , 1 , 3 };
122
+ private static final int [] ENCRYPTED_SAMPLE_SIZES = new int [] {1 , 3 , 1 , 3 };
121
123
122
- private static final int [] ENCRYPTED_SAMPLES_OFFSETS = new int [] {7 , 4 , 3 , 0 };
123
- private static final byte [] ENCRYPTED_SAMPLES_DATA = new byte [8 ];
124
-
125
- static {
126
- Arrays .fill (ENCRYPTED_SAMPLES_DATA , (byte ) 1 );
127
- }
124
+ private static final int [] ENCRYPTED_SAMPLE_OFFSETS = new int [] {7 , 4 , 3 , 0 };
125
+ private static final byte [] ENCRYPTED_SAMPLE_DATA = new byte [] {1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 };
128
126
129
127
private static final TrackOutput .CryptoData DUMMY_CRYPTO_DATA =
130
128
new TrackOutput .CryptoData (C .CRYPTO_MODE_AES_CTR , new byte [16 ], 0 , 0 );
@@ -461,6 +459,60 @@ public void testAllowPlaceholderSessionPopulatesDrmSession() {
461
459
/* decodeOnlyUntilUs= */ 0 );
462
460
assertThat (result ).isEqualTo (RESULT_FORMAT_READ );
463
461
assertThat (formatHolder .drmSession ).isSameInstanceAs (mockDrmSession );
462
+ assertReadEncryptedSample (/* sampleIndex= */ 3 );
463
+ }
464
+
465
+ @ Test
466
+ @ SuppressWarnings ("unchecked" )
467
+ public void testTrailingCryptoInfoInitializationVectorBytesZeroed () {
468
+ when (mockDrmSession .getState ()).thenReturn (DrmSession .STATE_OPENED_WITH_KEYS );
469
+ DrmSession <ExoMediaCrypto > mockPlaceholderDrmSession =
470
+ (DrmSession <ExoMediaCrypto >) Mockito .mock (DrmSession .class );
471
+ when (mockPlaceholderDrmSession .getState ()).thenReturn (DrmSession .STATE_OPENED_WITH_KEYS );
472
+ when (mockDrmSessionManager .acquirePlaceholderSession (
473
+ ArgumentMatchers .any (), ArgumentMatchers .anyInt ()))
474
+ .thenReturn (mockPlaceholderDrmSession );
475
+
476
+ writeFormat (ENCRYPTED_SAMPLE_FORMATS [0 ]);
477
+ byte [] sampleData = new byte [] {0 , 1 , 2 };
478
+ byte [] initializationVector = new byte [] {7 , 6 , 5 , 4 , 3 , 2 , 1 , 0 };
479
+ byte [] encryptedSampleData =
480
+ TestUtil .joinByteArrays (
481
+ new byte [] {
482
+ 0x08 , // subsampleEncryption = false (1 bit), ivSize = 8 (7 bits).
483
+ },
484
+ initializationVector ,
485
+ sampleData );
486
+ writeSample (
487
+ encryptedSampleData , /* timestampUs= */ 0 , BUFFER_FLAG_KEY_FRAME | BUFFER_FLAG_ENCRYPTED );
488
+
489
+ int result =
490
+ sampleQueue .read (
491
+ formatHolder ,
492
+ inputBuffer ,
493
+ /* formatRequired= */ false ,
494
+ /* loadingFinished= */ false ,
495
+ /* decodeOnlyUntilUs= */ 0 );
496
+ assertThat (result ).isEqualTo (RESULT_FORMAT_READ );
497
+
498
+ // Fill cryptoInfo.iv with non-zero data. When the 8 byte initialization vector is written into
499
+ // it, we expect the trailing 8 bytes to be zeroed.
500
+ inputBuffer .cryptoInfo .iv = new byte [16 ];
501
+ Arrays .fill (inputBuffer .cryptoInfo .iv , (byte ) 1 );
502
+
503
+ result =
504
+ sampleQueue .read (
505
+ formatHolder ,
506
+ inputBuffer ,
507
+ /* formatRequired= */ false ,
508
+ /* loadingFinished= */ false ,
509
+ /* decodeOnlyUntilUs= */ 0 );
510
+ assertThat (result ).isEqualTo (RESULT_BUFFER_READ );
511
+
512
+ // Assert cryptoInfo.iv contains the 8-byte initialization vector and that the trailing 8 bytes
513
+ // have been zeroed.
514
+ byte [] expectedInitializationVector = Arrays .copyOf (initializationVector , 16 );
515
+ assertArrayEquals (expectedInitializationVector , inputBuffer .cryptoInfo .iv );
464
516
}
465
517
466
518
@ Test
@@ -995,11 +1047,11 @@ private void writeTestData() {
995
1047
996
1048
private void writeTestDataWithEncryptedSections () {
997
1049
writeTestData (
998
- ENCRYPTED_SAMPLES_DATA ,
999
- ENCRYPTED_SAMPLES_SIZES ,
1000
- ENCRYPTED_SAMPLES_OFFSETS ,
1050
+ ENCRYPTED_SAMPLE_DATA ,
1051
+ ENCRYPTED_SAMPLE_SIZES ,
1052
+ ENCRYPTED_SAMPLE_OFFSETS ,
1001
1053
ENCRYPTED_SAMPLE_TIMESTAMPS ,
1002
- ENCRYPTED_SAMPLES_FORMATS ,
1054
+ ENCRYPTED_SAMPLE_FORMATS ,
1003
1055
ENCRYPTED_SAMPLES_FLAGS );
1004
1056
}
1005
1057
@@ -1033,7 +1085,12 @@ private void writeFormat(Format format) {
1033
1085
/** Writes a single sample to {@code sampleQueue}. */
1034
1086
private void writeSample (byte [] data , long timestampUs , int sampleFlags ) {
1035
1087
sampleQueue .sampleData (new ParsableByteArray (data ), data .length );
1036
- sampleQueue .sampleMetadata (timestampUs , sampleFlags , data .length , 0 , null );
1088
+ sampleQueue .sampleMetadata (
1089
+ timestampUs ,
1090
+ sampleFlags ,
1091
+ data .length ,
1092
+ /* offset= */ 0 ,
1093
+ (sampleFlags & C .BUFFER_FLAG_ENCRYPTED ) != 0 ? DUMMY_CRYPTO_DATA : null );
1037
1094
}
1038
1095
1039
1096
/**
@@ -1206,7 +1263,7 @@ private void assertReadFormat(boolean formatRequired, Format format) {
1206
1263
}
1207
1264
1208
1265
private void assertReadEncryptedSample (int sampleIndex ) {
1209
- byte [] sampleData = new byte [ENCRYPTED_SAMPLES_SIZES [sampleIndex ]];
1266
+ byte [] sampleData = new byte [ENCRYPTED_SAMPLE_SIZES [sampleIndex ]];
1210
1267
Arrays .fill (sampleData , (byte ) 1 );
1211
1268
boolean isKeyFrame = (ENCRYPTED_SAMPLES_FLAGS [sampleIndex ] & C .BUFFER_FLAG_KEY_FRAME ) != 0 ;
1212
1269
boolean isEncrypted = (ENCRYPTED_SAMPLES_FLAGS [sampleIndex ] & C .BUFFER_FLAG_ENCRYPTED ) != 0 ;
@@ -1216,7 +1273,7 @@ private void assertReadEncryptedSample(int sampleIndex) {
1216
1273
isEncrypted ,
1217
1274
sampleData ,
1218
1275
/* offset= */ 0 ,
1219
- ENCRYPTED_SAMPLES_SIZES [sampleIndex ] - (isEncrypted ? 2 : 0 ));
1276
+ ENCRYPTED_SAMPLE_SIZES [sampleIndex ] - (isEncrypted ? 2 : 0 ));
1220
1277
}
1221
1278
1222
1279
/**
0 commit comments