-
Notifications
You must be signed in to change notification settings - Fork 6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make all media source factories create a drm session
PiperOrigin-RevId: 327691347
- Loading branch information
1 parent
7abece9
commit e55b345
Showing
9 changed files
with
318 additions
and
174 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
library/core/src/main/java/com/google/android/exoplayer2/source/MediaSourceDrmHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
/* | ||
* Copyright (C) 2020 The Android Open Source Project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.google.android.exoplayer2.source; | ||
|
||
import static com.google.android.exoplayer2.drm.DefaultDrmSessionManager.MODE_PLAYBACK; | ||
import static com.google.android.exoplayer2.util.Util.castNonNull; | ||
|
||
import android.os.Build; | ||
import androidx.annotation.Nullable; | ||
import com.google.android.exoplayer2.ExoPlayerLibraryInfo; | ||
import com.google.android.exoplayer2.MediaItem; | ||
import com.google.android.exoplayer2.drm.DefaultDrmSessionManager; | ||
import com.google.android.exoplayer2.drm.DrmSessionManager; | ||
import com.google.android.exoplayer2.drm.FrameworkMediaDrm; | ||
import com.google.android.exoplayer2.drm.HttpMediaDrmCallback; | ||
import com.google.android.exoplayer2.upstream.DefaultHttpDataSourceFactory; | ||
import com.google.android.exoplayer2.upstream.HttpDataSource; | ||
import com.google.android.exoplayer2.util.Assertions; | ||
import com.google.android.exoplayer2.util.Util; | ||
import com.google.common.primitives.Ints; | ||
import java.util.Map; | ||
|
||
/** A helper to create a {@link DrmSessionManager} from a {@link MediaItem}. */ | ||
public final class MediaSourceDrmHelper { | ||
|
||
private static final String DEFAULT_USER_AGENT = | ||
ExoPlayerLibraryInfo.VERSION_SLASHY | ||
+ " (Linux;Android " | ||
+ Build.VERSION.RELEASE | ||
+ ") " | ||
+ ExoPlayerLibraryInfo.VERSION_SLASHY; | ||
|
||
@Nullable private HttpDataSource.Factory drmHttpDataSourceFactory; | ||
@Nullable private String userAgent; | ||
|
||
/** | ||
* Sets the {@link HttpDataSource.Factory} to be used for creating {@link HttpMediaDrmCallback | ||
* HttpMediaDrmCallbacks} which executes key and provisioning requests over HTTP. If {@code null} | ||
* is passed the {@link DefaultHttpDataSourceFactory} is used. | ||
* | ||
* @param drmHttpDataSourceFactory The HTTP data source factory or {@code null} to use {@link | ||
* DefaultHttpDataSourceFactory}. | ||
*/ | ||
public void setDrmHttpDataSourceFactory( | ||
@Nullable HttpDataSource.Factory drmHttpDataSourceFactory) { | ||
this.drmHttpDataSourceFactory = drmHttpDataSourceFactory; | ||
} | ||
|
||
/** | ||
* Sets the optional user agent to be used for DRM requests. | ||
* | ||
* <p>In case a factory has been set by {@link | ||
* #setDrmHttpDataSourceFactory(HttpDataSource.Factory)}, this user agent is ignored. | ||
* | ||
* @param userAgent The user agent to be used for DRM requests. | ||
*/ | ||
public void setDrmUserAgent(@Nullable String userAgent) { | ||
this.userAgent = userAgent; | ||
} | ||
|
||
/** Creates a {@link DrmSessionManager} for the given media item. */ | ||
public DrmSessionManager create(MediaItem mediaItem) { | ||
Assertions.checkNotNull(mediaItem.playbackProperties); | ||
@Nullable | ||
MediaItem.DrmConfiguration drmConfiguration = mediaItem.playbackProperties.drmConfiguration; | ||
if (drmConfiguration == null || drmConfiguration.licenseUri == null || Util.SDK_INT < 18) { | ||
return DrmSessionManager.getDummyDrmSessionManager(); | ||
} | ||
HttpDataSource.Factory dataSourceFactory = | ||
drmHttpDataSourceFactory != null | ||
? drmHttpDataSourceFactory | ||
: new DefaultHttpDataSourceFactory(userAgent != null ? userAgent : DEFAULT_USER_AGENT); | ||
HttpMediaDrmCallback httpDrmCallback = | ||
new HttpMediaDrmCallback( | ||
castNonNull(drmConfiguration.licenseUri).toString(), | ||
drmConfiguration.forceDefaultLicenseUri, | ||
dataSourceFactory); | ||
for (Map.Entry<String, String> entry : drmConfiguration.requestHeaders.entrySet()) { | ||
httpDrmCallback.setKeyRequestProperty(entry.getKey(), entry.getValue()); | ||
} | ||
DefaultDrmSessionManager drmSessionManager = | ||
new DefaultDrmSessionManager.Builder() | ||
.setUuidAndExoMediaDrmProvider( | ||
drmConfiguration.uuid, FrameworkMediaDrm.DEFAULT_PROVIDER) | ||
.setMultiSession(drmConfiguration.multiSession) | ||
.setPlayClearSamplesWithoutKeys(drmConfiguration.playClearContentWithoutKey) | ||
.setUseDrmSessionsForClearContent(Ints.toArray(drmConfiguration.sessionForClearTypes)) | ||
.build(httpDrmCallback); | ||
drmSessionManager.setMode(MODE_PLAYBACK, drmConfiguration.getKeySetId()); | ||
return drmSessionManager; | ||
} | ||
} |
Oops, something went wrong.