/** * Retrieves data for a given DRM scheme, specified by its UUID. * * @deprecated Use {@link #get(int)} and {@link SchemeData#matches(UUID)} instead. * @param uuid The DRM scheme's UUID. * @return The initialization data for the scheme, or null if the scheme is not supported. */ @Deprecated public SchemeData get(UUID uuid) { for (SchemeData schemeData : schemeDatas) { if (schemeData.matches(uuid)) { return schemeData; } } return null; }
@Override public boolean canAcquireSession(@NonNull DrmInitData drmInitData) { if (offlineLicenseKeySetId != null) { // An offline license can be restored so a session can always be acquired. return true; } List<SchemeData> schemeDatas = getSchemeDatas(drmInitData, uuid, true); if (schemeDatas.isEmpty()) { if (drmInitData.schemeDataCount == 1 && drmInitData.get(0).matches(C.COMMON_PSSH_UUID)) { // Assume scheme specific data will be added before the session is opened. Log.w( TAG, "DrmInitData only contains common PSSH SchemeData. Assuming support for: " + uuid); } else { // No data for this manager's scheme. return false; } } String schemeType = drmInitData.schemeType; if (schemeType == null || C.CENC_TYPE_cenc.equals(schemeType)) { // If there is no scheme information, assume patternless AES-CTR. return true; } else if (C.CENC_TYPE_cbc1.equals(schemeType) || C.CENC_TYPE_cbcs.equals(schemeType) || C.CENC_TYPE_cens.equals(schemeType)) { // API support for AES-CBC and pattern encryption was added in API 24. However, the // implementation was not stable until API 25. return Util.SDK_INT >= 25; } // Unknown schemes, assume one of them is supported. return true; }
/** * Extracts {@link SchemeData} instances suitable for the given DRM scheme {@link UUID}. * * @param drmInitData The {@link DrmInitData} from which to extract the {@link SchemeData}. * @param uuid The UUID. * @param allowMissingData Whether a {@link SchemeData} with null {@link SchemeData#data} may be * returned. * @return The extracted {@link SchemeData} instances, or an empty list if no suitable data is * present. */ private static List<SchemeData> getSchemeDatas( DrmInitData drmInitData, UUID uuid, boolean allowMissingData) { // Look for matching scheme data (matching the Common PSSH box for ClearKey). List<SchemeData> matchingSchemeDatas = new ArrayList<>(drmInitData.schemeDataCount); for (int i = 0; i < drmInitData.schemeDataCount; i++) { SchemeData schemeData = drmInitData.get(i); boolean uuidMatches = schemeData.matches(uuid) || (C.CLEARKEY_UUID.equals(uuid) && schemeData.matches(C.COMMON_PSSH_UUID)); if (uuidMatches && (schemeData.data != null || allowMissingData)) { matchingSchemeDatas.add(schemeData); } } return matchingSchemeDatas; }
assertThat(playlist.protectionSchemes.get(0).matches(C.PLAYREADY_UUID)).isTrue(); assertThat(playlist.protectionSchemes.get(0).hasData()).isFalse(); assertThat(playlist.protectionSchemes.get(1).matches(C.WIDEVINE_UUID)).isTrue(); assertThat(playlist.protectionSchemes.get(1).hasData()).isFalse(); assertThat(playlist.segments.get(1).drmInitData.get(0).matches(C.PLAYREADY_UUID)).isTrue(); assertThat(playlist.segments.get(1).drmInitData.get(0).hasData()).isTrue(); assertThat(playlist.segments.get(1).drmInitData.get(1).matches(C.WIDEVINE_UUID)).isTrue(); assertThat(playlist.segments.get(1).drmInitData.get(1).hasData()).isTrue(); assertThat(playlist.segments.get(2).drmInitData) .isNotEqualTo(playlist.segments.get(3).drmInitData); assertThat(playlist.segments.get(3).drmInitData.get(0).matches(C.PLAYREADY_UUID)).isTrue(); assertThat(playlist.segments.get(3).drmInitData.get(0).hasData()).isTrue(); assertThat(playlist.segments.get(3).drmInitData.get(1).matches(C.WIDEVINE_UUID)).isTrue(); assertThat(playlist.segments.get(3).drmInitData.get(1).hasData()).isTrue();
@Test public void testParseSampleAesCtrMethod() throws Exception { Uri playlistUri = Uri.parse("https://example.com/test.m3u8"); String playlistString = "#EXTM3U\n" + "#EXT-X-MEDIA-SEQUENCE:0\n" + "#EXTINF:8,\n" + "https://priv.example.com/1.ts\n" + "\n" + "#EXT-X-KEY:METHOD=SAMPLE-AES-CTR,URI=" + "\"data:text/plain;base64,VGhpcyBpcyBhbiBlYXN0ZXIgZWdn\"," + "IV=0x9358382AEB449EE23C3D809DA0B9CCD3,KEYFORMATVERSIONS=\"1\"," + "KEYFORMAT=\"com.widevine\",IV=0x1566B\n" + "#EXTINF:8,\n" + "https://priv.example.com/2.ts\n" + "#EXT-X-ENDLIST\n"; InputStream inputStream = new ByteArrayInputStream(Util.getUtf8Bytes(playlistString)); HlsMediaPlaylist playlist = (HlsMediaPlaylist) new HlsPlaylistParser().parse(playlistUri, inputStream); assertThat(playlist.protectionSchemes.schemeType).isEqualTo(C.CENC_TYPE_cenc); assertThat(playlist.protectionSchemes.get(0).matches(C.WIDEVINE_UUID)).isTrue(); assertThat(playlist.protectionSchemes.get(0).hasData()).isFalse(); }
@Test public void testParseSampleAesCencMethod() throws Exception { Uri playlistUri = Uri.parse("https://example.com/test.m3u8"); String playlistString = "#EXTM3U\n" + "#EXT-X-MEDIA-SEQUENCE:0\n" + "#EXTINF:8,\n" + "https://priv.example.com/1.ts\n" + "\n" + "#EXT-X-KEY:URI=\"data:text/plain;base64,VGhpcyBpcyBhbiBlYXN0ZXIgZWdn\"," + "IV=0x9358382AEB449EE23C3D809DA0B9CCD3,KEYFORMATVERSIONS=\"1\"," + "KEYFORMAT=\"urn:uuid:edef8ba9-79d6-4ace-a3c8-27dcd51d21ed\"," + "IV=0x1566B,METHOD=SAMPLE-AES-CENC \n" + "#EXTINF:8,\n" + "https://priv.example.com/2.ts\n" + "#EXT-X-ENDLIST\n"; InputStream inputStream = new ByteArrayInputStream(Util.getUtf8Bytes(playlistString)); HlsMediaPlaylist playlist = (HlsMediaPlaylist) new HlsPlaylistParser().parse(playlistUri, inputStream); assertThat(playlist.protectionSchemes.schemeType).isEqualTo(C.CENC_TYPE_cenc); assertThat(playlist.protectionSchemes.get(0).matches(C.WIDEVINE_UUID)).isTrue(); assertThat(playlist.protectionSchemes.get(0).hasData()).isFalse(); }
@Test public void testParseSampleAesMethod() throws Exception { Uri playlistUri = Uri.parse("https://example.com/test.m3u8"); String playlistString = "#EXTM3U\n" + "#EXT-X-MEDIA-SEQUENCE:0\n" + "#EXTINF:8,\n" + "https://priv.example.com/1.ts\n" + "\n" + "#EXT-X-KEY:METHOD=SAMPLE-AES,URI=" + "\"data:text/plain;base64,VGhpcyBpcyBhbiBlYXN0ZXIgZWdn\"," + "IV=0x9358382AEB449EE23C3D809DA0B9CCD3,KEYFORMATVERSIONS=\"1\"," + "KEYFORMAT=\"com.widevine\",IV=0x1566B\n" + "#EXTINF:8,\n" + "https://priv.example.com/2.ts\n" + "#EXT-X-ENDLIST\n"; InputStream inputStream = new ByteArrayInputStream(Util.getUtf8Bytes(playlistString)); HlsMediaPlaylist playlist = (HlsMediaPlaylist) new HlsPlaylistParser().parse(playlistUri, inputStream); assertThat(playlist.protectionSchemes.schemeType).isEqualTo(C.CENC_TYPE_cbcs); assertThat(playlist.protectionSchemes.get(0).matches(C.WIDEVINE_UUID)).isTrue(); assertThat(playlist.protectionSchemes.get(0).hasData()).isFalse(); assertThat(playlist.segments.get(0).drmInitData).isNull(); assertThat(playlist.segments.get(1).drmInitData.get(0).matches(C.WIDEVINE_UUID)).isTrue(); assertThat(playlist.segments.get(1).drmInitData.get(0).hasData()).isTrue(); }
/** * Returns whether this {@link SchemeData} can be used to replace {@code other}. * * @param other A {@link SchemeData}. * @return Whether this {@link SchemeData} can be used to replace {@code other}. */ public boolean canReplace(SchemeData other) { return hasData() && !other.hasData() && matches(other.uuid); }
@Test public void testSchemeDataMatches() { assertThat(DATA_1.matches(WIDEVINE_UUID)).isTrue(); assertThat(DATA_1.matches(PLAYREADY_UUID)).isFalse(); assertThat(DATA_2.matches(UUID_NIL)).isFalse(); assertThat(DATA_2.matches(WIDEVINE_UUID)).isFalse(); assertThat(DATA_2.matches(PLAYREADY_UUID)).isTrue(); assertThat(DATA_2.matches(UUID_NIL)).isFalse(); assertThat(DATA_UNIVERSAL.matches(WIDEVINE_UUID)).isTrue(); assertThat(DATA_UNIVERSAL.matches(PLAYREADY_UUID)).isTrue(); assertThat(DATA_UNIVERSAL.matches(UUID_NIL)).isTrue(); }