Skip to content

Commit

Permalink
+ online case working with keyhelper. Created custom datasource and f…
Browse files Browse the repository at this point in the history
…actory.
  • Loading branch information
hishamMuneer committed Oct 12, 2018
1 parent 6f0053d commit 00e1b15
Show file tree
Hide file tree
Showing 9 changed files with 369 additions and 142 deletions.
14 changes: 13 additions & 1 deletion demos/main/src/main/assets/media.exolist.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,23 @@
},
{
"name": "Encrypted Custom Small",
"uri":"http://54.152.186.92:60801/drm/static/sample/playlist.m3u8"
"uri": "https://voca2hosting.firebaseapp.com/small_files/encrypted_without_key/index.m3u8"
},
{
"name": "Inayat's Sample, Not Encrypted",
"uri":"http://54.152.186.92:60801/drm/static/sample/playlist.m3u8"
},
{
"name": "Video from server in video list",
"uri":"http://54.152.186.92:60801/static/sample/playlist.m3u8"
},
{
"name": "Video from server in video list DRM With key inside it",
"uri":"http://54.152.186.92:60801/drm/static/tutorial/playlistc.m3u8"
},
{
"name": "Video from server in video list",
"uri":"http://54.152.186.92:60801/drm/static/tutorial/playlist.m3u8"
}
]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import com.google.android.exoplayer2.source.smoothstreaming.offline.SsDownloadAction;
import com.google.android.exoplayer2.upstream.DataSource;
import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory;
import com.google.android.exoplayer2.upstream.DefaultHttpDataSourceFactory;
import com.google.android.exoplayer2.upstream.FileDataSourceFactory;
import com.google.android.exoplayer2.upstream.HttpDataSource;
import com.google.android.exoplayer2.upstream.TransferListener;
Expand All @@ -34,7 +33,9 @@
import com.google.android.exoplayer2.upstream.cache.CacheDataSourceFactory;
import com.google.android.exoplayer2.upstream.cache.NoOpCacheEvictor;
import com.google.android.exoplayer2.upstream.cache.SimpleCache;
import com.google.android.exoplayer2.upstream.vocabimate_stream.CustomDataSourceFactory;
import com.google.android.exoplayer2.util.Util;
import com.google.android.exoplayer2.vocab.KeyHelperModel;
import java.io.File;

/**
Expand Down Expand Up @@ -68,16 +69,16 @@ public void onCreate() {
}

/** Returns a {@link DataSource.Factory}. */
public DataSource.Factory buildDataSourceFactory(TransferListener<? super DataSource> listener) {
public DataSource.Factory buildDataSourceFactory(TransferListener<? super DataSource> listener, KeyHelperModel keyHelper) {
DefaultDataSourceFactory upstreamFactory =
new DefaultDataSourceFactory(this, listener, buildHttpDataSourceFactory(listener));
new DefaultDataSourceFactory(this, listener, buildHttpDataSourceFactory(listener, keyHelper));
return buildReadOnlyCacheDataSource(upstreamFactory, getDownloadCache());
}

/** Returns a {@link HttpDataSource.Factory}. */
public HttpDataSource.Factory buildHttpDataSourceFactory(
TransferListener<? super DataSource> listener) {
return new DefaultHttpDataSourceFactory(userAgent, listener);
public HttpDataSource.Factory buildHttpDataSourceFactory(TransferListener<? super DataSource> listener,
KeyHelperModel keyHelper) {
return new CustomDataSourceFactory(userAgent, listener, keyHelper);
}

/** Returns whether extension renderers should be used. */
Expand All @@ -86,20 +87,26 @@ public boolean useExtensionRenderers() {
}

public DownloadManager getDownloadManager() {
initDownloadManager();
initDownloadManager(null);
return downloadManager;
}

public DownloadTracker getDownloadTracker() {
initDownloadManager();
initDownloadManager(null);
return downloadTracker;
}

private synchronized void initDownloadManager() {
public DownloadTracker getDownloadTracker(KeyHelperModel keyHelperModel){
initDownloadManager(keyHelperModel);
return downloadTracker;
}

private synchronized void initDownloadManager(
KeyHelperModel keyHelper) {
if (downloadManager == null) {
DownloaderConstructorHelper downloaderConstructorHelper =
new DownloaderConstructorHelper(
getDownloadCache(), buildHttpDataSourceFactory(/* listener= */ null));
getDownloadCache(), buildHttpDataSourceFactory(/* listener= */ null, keyHelper)); // todo - should not be null
downloadManager =
new DownloadManager(
downloaderConstructorHelper,
Expand All @@ -110,7 +117,7 @@ private synchronized void initDownloadManager() {
downloadTracker =
new DownloadTracker(
/* context= */ this,
buildDataSourceFactory(/* listener= */ null),
buildDataSourceFactory(/* listener= */ null, keyHelper), // todo - should not be null,
new File(getDownloadDirectory(), DOWNLOAD_TRACKER_ACTION_FILE),
DOWNLOAD_DESERIALIZERS);
downloadManager.addListener(downloadTracker);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@
import com.google.android.exoplayer2.util.ErrorMessageProvider;
import com.google.android.exoplayer2.util.EventLogger;
import com.google.android.exoplayer2.util.Util;
import com.google.android.exoplayer2.vocab.KeyHelperModel;
import java.io.Serializable;
import java.lang.reflect.Constructor;
import java.net.CookieHandler;
import java.net.CookieManager;
Expand Down Expand Up @@ -152,13 +154,21 @@ public class PlayerActivity extends Activity
private AdsLoader adsLoader;
private Uri loadedAdTagUri;
private ViewGroup adUiViewGroup;
// private KeyHelperModel keyHelperModel;

// Activity lifecycle

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mediaDataSourceFactory = buildDataSourceFactory(true);

Intent intent = getIntent();
if(intent.hasExtra("keyHelperModel")){
KeyHelperModel keyHelperModel = (KeyHelperModel) intent.getSerializableExtra("keyHelperModel");
mediaDataSourceFactory = buildDataSourceFactory(true, keyHelperModel);
} else {
throw new NullPointerException("keyHelperModel not found inside intent, forgot to call: intent.putExtra(\"keyHelperModel\", keyHelperModel);");
}
if (CookieHandler.getDefault() != DEFAULT_COOKIE_MANAGER) {
CookieHandler.setDefault(DEFAULT_COOKIE_MANAGER);
}
Expand Down Expand Up @@ -442,22 +452,22 @@ private MediaSource buildMediaSource(Uri uri) {
private MediaSource buildMediaSource(Uri uri, @Nullable String overrideExtension) {
@ContentType int type = Util.inferContentType(uri, overrideExtension);
switch (type) {
case C.TYPE_DASH:
return new DashMediaSource.Factory(
new DefaultDashChunkSource.Factory(mediaDataSourceFactory),
buildDataSourceFactory(false))
.setManifestParser(
new FilteringManifestParser<>(
new DashManifestParser(), (List<RepresentationKey>) getOfflineStreamKeys(uri)))
.createMediaSource(uri);
case C.TYPE_SS:
return new SsMediaSource.Factory(
new DefaultSsChunkSource.Factory(mediaDataSourceFactory),
buildDataSourceFactory(false))
.setManifestParser(
new FilteringManifestParser<>(
new SsManifestParser(), (List<StreamKey>) getOfflineStreamKeys(uri)))
.createMediaSource(uri);
// case C.TYPE_DASH:
// return new DashMediaSource.Factory(
// new DefaultDashChunkSource.Factory(mediaDataSourceFactory),
// buildDataSourceFactory(false))
// .setManifestParser(
// new FilteringManifestParser<>(
// new DashManifestParser(), (List<RepresentationKey>) getOfflineStreamKeys(uri)))
// .createMediaSource(uri);
// case C.TYPE_SS:
// return new SsMediaSource.Factory(
// new DefaultSsChunkSource.Factory(mediaDataSourceFactory),
// buildDataSourceFactory(false))
// .setManifestParser(
// new FilteringManifestParser<>(
// new SsManifestParser(), (List<StreamKey>) getOfflineStreamKeys(uri)))
// .createMediaSource(uri);
case C.TYPE_HLS:
return new HlsMediaSource.Factory(mediaDataSourceFactory)
.setPlaylistParser(
Expand All @@ -480,7 +490,7 @@ private DefaultDrmSessionManager<FrameworkMediaCrypto> buildDrmSessionManagerV18
UUID uuid, String licenseUrl, String[] keyRequestPropertiesArray, boolean multiSession)
throws UnsupportedDrmException {
HttpDataSource.Factory licenseDataSourceFactory =
((DemoApplication) getApplication()).buildHttpDataSourceFactory(/* listener= */ null);
((DemoApplication) getApplication()).buildHttpDataSourceFactory(/* listener= */ null, null); // voca - drm not using as of now
HttpMediaDrmCallback drmCallback =
new HttpMediaDrmCallback(licenseUrl, licenseDataSourceFactory);
if (keyRequestPropertiesArray != null) {
Expand Down Expand Up @@ -549,11 +559,12 @@ private void clearStartPosition() {
*
* @param useBandwidthMeter Whether to set {@link #BANDWIDTH_METER} as a listener to the new
* DataSource factory.
* @param keyHelperModel
* @return A new DataSource factory.
*/
private DataSource.Factory buildDataSourceFactory(boolean useBandwidthMeter) {
private DataSource.Factory buildDataSourceFactory(boolean useBandwidthMeter, KeyHelperModel keyHelperModel) {
return ((DemoApplication) getApplication())
.buildDataSourceFactory(useBandwidthMeter ? BANDWIDTH_METER : null);
.buildDataSourceFactory(useBandwidthMeter ? BANDWIDTH_METER : null, keyHelperModel);
}

/** Returns an ads media source, reusing the ads loader if one exists. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,10 @@
import com.google.android.exoplayer2.upstream.DataSourceInputStream;
import com.google.android.exoplayer2.upstream.DataSpec;
import com.google.android.exoplayer2.upstream.DefaultDataSource;
import com.google.android.exoplayer2.upstream.TransferListener;
import com.google.android.exoplayer2.util.Assertions;
import com.google.android.exoplayer2.util.Util;
import com.google.android.exoplayer2.vocab.CredUpdateSingleton;
import com.google.android.exoplayer2.vocab.KeyHelperModel;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
Expand Down Expand Up @@ -133,21 +134,38 @@ private void onSampleGroups(final List<SampleGroup> groups, boolean sawError) {
}

@Override
public boolean onChildClick(
ExpandableListView parent, View view, int groupPosition, int childPosition, long id) {
CredUpdateSingleton.getInstance().setVideoId("5");
public boolean onChildClick(ExpandableListView parent, View view, int groupPosition, int childPosition, long id) {
Sample sample = (Sample) view.getTag();
startActivity(sample.buildIntent(this));
Intent intent = sample.buildIntent(this);

KeyHelperModel keyHelperModel = getKeyHelper(id, (UriSample) sample);
intent.putExtra("keyHelperModel", keyHelperModel);
startActivity(intent);
return true;
}

public KeyHelperModel getKeyHelper(long videoId, UriSample sample) {
String key = null;
if(sample.uri.toString().contains("tutorial")){
key = "http://54.152.186.92:60801/drm/static/tutorial/tutorial.key";
} else if(sample.uri.toString().contains("sample")){
key = "http://54.152.186.92:60801/static/sample/enc.key";
}

return new KeyHelperModel().setVideoId("videoId: " + videoId)
.setKeyPath(key)
.setM3u8Path(sample.uri.toString());
}

private void onSampleDownloadButtonClicked(Sample sample) {
int downloadUnsupportedStringId = getDownloadUnsupportedStringId(sample);
if (downloadUnsupportedStringId != 0) {
Toast.makeText(getApplicationContext(), downloadUnsupportedStringId, Toast.LENGTH_LONG)
.show();
} else {
UriSample uriSample = (UriSample) sample;
KeyHelperModel keyHelper = getKeyHelper(5, uriSample);
downloadTracker = ((DemoApplication)getApplication()).getDownloadTracker(keyHelper);
downloadTracker.toggleDownload(this, sample.name, uriSample.uri, uriSample.extension);
}
}
Expand Down Expand Up @@ -426,7 +444,6 @@ public boolean isChildSelectable(int groupPosition, int childPosition) {

@Override
public void onClick(View view) {
CredUpdateSingleton.getInstance().setVideoId("5");
onSampleDownloadButtonClicked((Sample) view.getTag());
}

Expand Down
Loading

0 comments on commit 00e1b15

Please sign in to comment.