From 00e1b158e38913280bec3c0adac834f79a474a5e Mon Sep 17 00:00:00 2001 From: Hisham Date: Fri, 12 Oct 2018 15:34:23 +0530 Subject: [PATCH] + online case working with keyhelper. Created custom datasource and factory. --- demos/main/src/main/assets/media.exolist.json | 14 +- .../exoplayer2/demo/DemoApplication.java | 29 ++-- .../exoplayer2/demo/PlayerActivity.java | 51 ++++--- .../demo/SampleChooserActivity.java | 29 +++- .../upstream/DefaultHttpDataSource.java | 138 +++++++++++------- .../vocabimate_stream/CustomDataSource.java | 39 +++++ .../CustomDataSourceFactory.java | 97 ++++++++++++ .../exoplayer2/vocab/CredUpdateSingleton.java | 50 ------- .../exoplayer2/vocab/KeyHelperModel.java | 64 ++++++++ 9 files changed, 369 insertions(+), 142 deletions(-) create mode 100644 library/core/src/main/java/com/google/android/exoplayer2/upstream/vocabimate_stream/CustomDataSource.java create mode 100644 library/core/src/main/java/com/google/android/exoplayer2/upstream/vocabimate_stream/CustomDataSourceFactory.java delete mode 100644 library/core/src/main/java/com/google/android/exoplayer2/vocab/CredUpdateSingleton.java create mode 100644 library/core/src/main/java/com/google/android/exoplayer2/vocab/KeyHelperModel.java diff --git a/demos/main/src/main/assets/media.exolist.json b/demos/main/src/main/assets/media.exolist.json index 1cd8534885a..7f0b39142e4 100644 --- a/demos/main/src/main/assets/media.exolist.json +++ b/demos/main/src/main/assets/media.exolist.json @@ -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" } ] } diff --git a/demos/main/src/main/java/com/google/android/exoplayer2/demo/DemoApplication.java b/demos/main/src/main/java/com/google/android/exoplayer2/demo/DemoApplication.java index b5c127d2e35..b4d7d83df93 100644 --- a/demos/main/src/main/java/com/google/android/exoplayer2/demo/DemoApplication.java +++ b/demos/main/src/main/java/com/google/android/exoplayer2/demo/DemoApplication.java @@ -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; @@ -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; /** @@ -68,16 +69,16 @@ public void onCreate() { } /** Returns a {@link DataSource.Factory}. */ - public DataSource.Factory buildDataSourceFactory(TransferListener listener) { + public DataSource.Factory buildDataSourceFactory(TransferListener 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 listener) { - return new DefaultHttpDataSourceFactory(userAgent, listener); + public HttpDataSource.Factory buildHttpDataSourceFactory(TransferListener listener, + KeyHelperModel keyHelper) { + return new CustomDataSourceFactory(userAgent, listener, keyHelper); } /** Returns whether extension renderers should be used. */ @@ -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, @@ -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); diff --git a/demos/main/src/main/java/com/google/android/exoplayer2/demo/PlayerActivity.java b/demos/main/src/main/java/com/google/android/exoplayer2/demo/PlayerActivity.java index 67c56127ee5..5d16dca6455 100644 --- a/demos/main/src/main/java/com/google/android/exoplayer2/demo/PlayerActivity.java +++ b/demos/main/src/main/java/com/google/android/exoplayer2/demo/PlayerActivity.java @@ -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; @@ -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); } @@ -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) getOfflineStreamKeys(uri))) - .createMediaSource(uri); - case C.TYPE_SS: - return new SsMediaSource.Factory( - new DefaultSsChunkSource.Factory(mediaDataSourceFactory), - buildDataSourceFactory(false)) - .setManifestParser( - new FilteringManifestParser<>( - new SsManifestParser(), (List) getOfflineStreamKeys(uri))) - .createMediaSource(uri); +// case C.TYPE_DASH: +// return new DashMediaSource.Factory( +// new DefaultDashChunkSource.Factory(mediaDataSourceFactory), +// buildDataSourceFactory(false)) +// .setManifestParser( +// new FilteringManifestParser<>( +// new DashManifestParser(), (List) getOfflineStreamKeys(uri))) +// .createMediaSource(uri); +// case C.TYPE_SS: +// return new SsMediaSource.Factory( +// new DefaultSsChunkSource.Factory(mediaDataSourceFactory), +// buildDataSourceFactory(false)) +// .setManifestParser( +// new FilteringManifestParser<>( +// new SsManifestParser(), (List) getOfflineStreamKeys(uri))) +// .createMediaSource(uri); case C.TYPE_HLS: return new HlsMediaSource.Factory(mediaDataSourceFactory) .setPlaylistParser( @@ -480,7 +490,7 @@ private DefaultDrmSessionManager 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) { @@ -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. */ diff --git a/demos/main/src/main/java/com/google/android/exoplayer2/demo/SampleChooserActivity.java b/demos/main/src/main/java/com/google/android/exoplayer2/demo/SampleChooserActivity.java index 581fe37906c..7812d795ccb 100644 --- a/demos/main/src/main/java/com/google/android/exoplayer2/demo/SampleChooserActivity.java +++ b/demos/main/src/main/java/com/google/android/exoplayer2/demo/SampleChooserActivity.java @@ -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; @@ -133,14 +134,29 @@ private void onSampleGroups(final List 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) { @@ -148,6 +164,8 @@ private void onSampleDownloadButtonClicked(Sample sample) { .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); } } @@ -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()); } diff --git a/library/core/src/main/java/com/google/android/exoplayer2/upstream/DefaultHttpDataSource.java b/library/core/src/main/java/com/google/android/exoplayer2/upstream/DefaultHttpDataSource.java index c69bf3fb7f0..b549a46cba1 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/upstream/DefaultHttpDataSource.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/upstream/DefaultHttpDataSource.java @@ -20,11 +20,12 @@ import android.util.Log; import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.upstream.vocabimate_stream.AesEncryptionUtil; +import com.google.android.exoplayer2.upstream.vocabimate_stream.CustomDataSource; import com.google.android.exoplayer2.upstream.vocabimate_stream.VocaDataSourceHelper; import com.google.android.exoplayer2.util.Assertions; import com.google.android.exoplayer2.util.Predicate; import com.google.android.exoplayer2.util.Util; -import com.google.android.exoplayer2.vocab.CredUpdateSingleton; +import com.google.android.exoplayer2.vocab.KeyHelperModel; import com.google.gson.Gson; import java.io.BufferedReader; import java.io.EOFException; @@ -90,8 +91,6 @@ public class DefaultHttpDataSource implements HttpDataSource { private long bytesSkipped; private long bytesRead; - private String ACCESS_TOKEN = "l8TmQpaBEdDGCtbefPfzTx54Bt4nOQLgaH8s3edJDhs="; - /** * @param userAgent The User-Agent string that should be used. * @param contentTypePredicate An optional {@link Predicate}. If a content type is rejected by the @@ -268,35 +267,42 @@ public long open(DataSpec dataSpec) throws HttpDataSourceException { // Note: Th return bytesToRead; } - - /** * Custom method for custom scheme. */ - private HttpURLConnection makeConnectionCustom(DataSpec dataSpec) throws IOException { - - String videoId = CredUpdateSingleton.getInstance().getVideoIdAndResetIt(); - if(videoId == null){ - throw new NullPointerException("Video id is not set, call: CredUpdateSingleton.getInstance().setVideoId(int);"); - } - - URL keyUrl = null; + private HttpURLConnection makeConnectionCustom() throws IOException { + + if (this instanceof CustomDataSource) { + KeyHelperModel keyHelper = ((CustomDataSource) this).getKeyHelperModel(); + if (keyHelper != null) { + String token = keyHelper.getToken(); + if(token != null && token.length() > 0) { + connection.setRequestProperty("access_token", token); + } - String licenceUrl = "https://voca2hosting.firebaseapp.com/small_files/license_key_path_absolute.json"; - URL url = new URL(licenceUrl); + String videoId = keyHelper.getVideoId(); // todo check this + if (videoId == null) { + throw new NullPointerException("Video id is not set."); + } - // parse licence - HttpURLConnection httpURLConnection = null; - try { - httpURLConnection = (HttpURLConnection) url - .openConnection(); - - InputStream in = httpURLConnection.getInputStream(); - String result = readStream(in); - VocaDataSourceHelper.LicenceModel licenceModel = new Gson().fromJson(result, VocaDataSourceHelper.LicenceModel.class); - if(licenceModel != null){ - keyUrl = new URL(licenceModel.getPath()); - } + URL keyUrl = null; + if (keyHelper.getKeyPath() == null) { + String licenceUrl = keyHelper.getLicecnceUrl(); //"https://voca2hosting.firebaseapp.com/small_files/license_key_path_absolute.json"; + URL url = new URL(licenceUrl); + + // parse licence + HttpURLConnection httpURLConnection = null; + try { + httpURLConnection = (HttpURLConnection) url + .openConnection(); + + InputStream in = httpURLConnection.getInputStream(); + String result = readStream(in); + VocaDataSourceHelper.LicenceModel licenceModel = new Gson() + .fromJson(result, VocaDataSourceHelper.LicenceModel.class); + if (licenceModel != null) { + keyUrl = new URL(licenceModel.getPath()); + } /*InputStreamReader isw = new InputStreamReader(in); int data = isw.read(); @@ -305,34 +311,49 @@ private HttpURLConnection makeConnectionCustom(DataSpec dataSpec) throws IOExcep data = isw.read(); System.out.print(current); }*/ - } catch (Exception e) { - e.printStackTrace(); - } finally { - if (httpURLConnection != null) { - httpURLConnection.disconnect(); - } - } - - HttpURLConnection connection = null; - if (keyUrl != null) { - connection = (HttpURLConnection) keyUrl.openConnection(); - connection.setConnectTimeout(connectTimeoutMillis); - connection.setReadTimeout(readTimeoutMillis); - if (defaultRequestProperties != null) { - for (Map.Entry property : defaultRequestProperties.getSnapshot() - .entrySet()) { - connection.setRequestProperty(property.getKey(), property.getValue()); + } catch (Exception e) { + e.printStackTrace(); + } finally { + if (httpURLConnection != null) { + httpURLConnection.disconnect(); + } + } + } else { + keyUrl = new URL(keyHelper.getKeyPath()); } - } - for (Map.Entry property : requestProperties.getSnapshot().entrySet()) { - connection.setRequestProperty(property.getKey(), property.getValue()); - } - connection.setRequestProperty("User-Agent", userAgent); - connection.setRequestMethod("GET"); + HttpURLConnection connection = null; +// keyUrl = new URL("http://54.152.186.92:60801/static/sample/enc.key"); // temp + if (keyUrl != null) { + connection = (HttpURLConnection) keyUrl.openConnection(); + connection.setConnectTimeout(connectTimeoutMillis); + connection.setReadTimeout(readTimeoutMillis); + if (defaultRequestProperties != null) { + for (Map.Entry property : defaultRequestProperties.getSnapshot() + .entrySet()) { + connection.setRequestProperty(property.getKey(), property.getValue()); + } + } + for (Map.Entry property : requestProperties.getSnapshot().entrySet()) { + connection.setRequestProperty(property.getKey(), property.getValue()); + } + connection.setRequestProperty("User-Agent", userAgent); + connection.setRequestMethod("GET"); // if(!TextUtils.isEmpty(TokenManager.getToken())) { - connection.setRequestProperty("access_token", ACCESS_TOKEN); //TODO this is hardcoded we need to make it dynamic. 08-10-2018 + if (this instanceof CustomDataSource) { + KeyHelperModel keyHelperModel = ((CustomDataSource) this).getKeyHelperModel(); + if (keyHelperModel != null) { + String token2 = keyHelper.getToken(); + if(token2 != null && token2.length() > 0) { + connection.setRequestProperty("access_token", token2); + } + } + } + // } - Log.d(TAG, "ResponseCode: " + connection.getResponseCode()); + Log.d(TAG, "ResponseCode: " + connection.getResponseCode()); + } + return connection; + } } return connection; } @@ -444,7 +465,7 @@ private HttpURLConnection makeConnection(DataSpec dataSpec) throws IOException { url = new URL(dataSpec.uri.toString()); } catch (MalformedURLException e) { if (e.getMessage().contains("vcb")) { - connection = makeConnectionCustom(dataSpec); + connection = makeConnectionCustom(); return connection; } } @@ -503,7 +524,16 @@ private HttpURLConnection makeConnection(URL url, byte[] postBody, long position connection.setConnectTimeout(connectTimeoutMillis); connection.setReadTimeout(readTimeoutMillis); if (defaultRequestProperties != null) { - defaultRequestProperties.set("access_token", ACCESS_TOKEN); //TODO this is hardcoded we need to make it dynamic. 08-10-2018 + + if(this instanceof CustomDataSource){ + KeyHelperModel keyHelperModel = ((CustomDataSource) this).getKeyHelperModel(); + if(keyHelperModel != null) { + String token = keyHelperModel.getToken(); + if(token != null && token.length() > 0) { + defaultRequestProperties.set("access_token", token); + } + } + } for (Map.Entry property : defaultRequestProperties.getSnapshot().entrySet()) { connection.setRequestProperty(property.getKey(), property.getValue()); } diff --git a/library/core/src/main/java/com/google/android/exoplayer2/upstream/vocabimate_stream/CustomDataSource.java b/library/core/src/main/java/com/google/android/exoplayer2/upstream/vocabimate_stream/CustomDataSource.java new file mode 100644 index 00000000000..dbe5498a443 --- /dev/null +++ b/library/core/src/main/java/com/google/android/exoplayer2/upstream/vocabimate_stream/CustomDataSource.java @@ -0,0 +1,39 @@ +package com.google.android.exoplayer2.upstream.vocabimate_stream; + +import com.google.android.exoplayer2.upstream.DefaultHttpDataSource; +import com.google.android.exoplayer2.upstream.TransferListener; +import com.google.android.exoplayer2.util.Predicate; +import com.google.android.exoplayer2.vocab.KeyHelperModel; + +/** + * Created by Hisham on 12/Oct/2018 - 13:55 + */ +public final class CustomDataSource extends DefaultHttpDataSource { + + private KeyHelperModel keyHelperModel; + + public CustomDataSource setKeyHelperModel(KeyHelperModel keyHelperModel) { + this.keyHelperModel = keyHelperModel; + return this; + } + + public KeyHelperModel getKeyHelperModel() { + return keyHelperModel; + } + + public CustomDataSource(String userAgent, Predicate contentTypePredicate) { + super(userAgent, contentTypePredicate); + } + + public CustomDataSource(String userAgent, Predicate contentTypePredicate, TransferListener listener) { + super(userAgent, contentTypePredicate, listener); + } + + public CustomDataSource(String userAgent, Predicate contentTypePredicate, TransferListener listener, int connectTimeoutMillis, int readTimeoutMillis) { + super(userAgent, contentTypePredicate, listener, connectTimeoutMillis, readTimeoutMillis); + } + + public CustomDataSource(String userAgent, Predicate contentTypePredicate, TransferListener listener, int connectTimeoutMillis, int readTimeoutMillis, boolean allowCrossProtocolRedirects, RequestProperties defaultRequestProperties) { + super(userAgent, contentTypePredicate, listener, connectTimeoutMillis, readTimeoutMillis, allowCrossProtocolRedirects, defaultRequestProperties); + } +} diff --git a/library/core/src/main/java/com/google/android/exoplayer2/upstream/vocabimate_stream/CustomDataSourceFactory.java b/library/core/src/main/java/com/google/android/exoplayer2/upstream/vocabimate_stream/CustomDataSourceFactory.java new file mode 100644 index 00000000000..1b7dcb86dac --- /dev/null +++ b/library/core/src/main/java/com/google/android/exoplayer2/upstream/vocabimate_stream/CustomDataSourceFactory.java @@ -0,0 +1,97 @@ +/* + * Copyright (C) 2016 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.upstream.vocabimate_stream; + +import com.google.android.exoplayer2.upstream.DataSource; +import com.google.android.exoplayer2.upstream.DefaultHttpDataSource; +import com.google.android.exoplayer2.upstream.HttpDataSource; +import com.google.android.exoplayer2.upstream.HttpDataSource.BaseFactory; +import com.google.android.exoplayer2.upstream.HttpDataSource.Factory; +import com.google.android.exoplayer2.upstream.TransferListener; +import com.google.android.exoplayer2.vocab.KeyHelperModel; + +/** + * A {@link Factory} that produces {@link DefaultHttpDataSource} instances. + */ +public final class CustomDataSourceFactory extends BaseFactory { + + private final String userAgent; + private final TransferListener listener; + private final int connectTimeoutMillis; + private final int readTimeoutMillis; + private final boolean allowCrossProtocolRedirects; + private KeyHelperModel keyHelperModel; + + /** + * Constructs a DefaultHttpDataSourceFactory. Sets {@link + * DefaultHttpDataSource#DEFAULT_CONNECT_TIMEOUT_MILLIS} as the connection timeout, {@link + * DefaultHttpDataSource#DEFAULT_READ_TIMEOUT_MILLIS} as the read timeout and disables + * cross-protocol redirects. + * + * @param userAgent The User-Agent string that should be used. + */ + public CustomDataSourceFactory(String userAgent, KeyHelperModel keyHelperModel) { + this(userAgent, null, keyHelperModel); + } + + /** + * Constructs a DefaultHttpDataSourceFactory. Sets {@link + * DefaultHttpDataSource#DEFAULT_CONNECT_TIMEOUT_MILLIS} as the connection timeout, {@link + * DefaultHttpDataSource#DEFAULT_READ_TIMEOUT_MILLIS} as the read timeout and disables + * cross-protocol redirects. + * + * @param userAgent The User-Agent string that should be used. + * @param listener An optional listener. + */ + + + public CustomDataSourceFactory( + String userAgent, TransferListener listener, KeyHelperModel keyHelperModel) { + this(userAgent, listener, DefaultHttpDataSource.DEFAULT_CONNECT_TIMEOUT_MILLIS, + DefaultHttpDataSource.DEFAULT_READ_TIMEOUT_MILLIS, false, keyHelperModel); + } + + /** + * @param userAgent The User-Agent string that should be used. + * @param listener An optional listener. + * @param connectTimeoutMillis The connection timeout that should be used when requesting remote + * data, in milliseconds. A timeout of zero is interpreted as an infinite timeout. + * @param readTimeoutMillis The read timeout that should be used when requesting remote data, in + * milliseconds. A timeout of zero is interpreted as an infinite timeout. + * @param allowCrossProtocolRedirects Whether cross-protocol redirects (i.e. redirects from HTTP + * to HTTPS and vice versa) are enabled. + */ + public CustomDataSourceFactory(String userAgent, + TransferListener listener, int connectTimeoutMillis, + int readTimeoutMillis, boolean allowCrossProtocolRedirects, KeyHelperModel keyHelperModel) { + this.userAgent = userAgent; + this.listener = listener; + this.connectTimeoutMillis = connectTimeoutMillis; + this.readTimeoutMillis = readTimeoutMillis; + this.allowCrossProtocolRedirects = allowCrossProtocolRedirects; + this.keyHelperModel = keyHelperModel; + } + + @Override + protected DefaultHttpDataSource createDataSourceInternal( + HttpDataSource.RequestProperties defaultRequestProperties) { + CustomDataSource defaultHttpDataSource = new CustomDataSource(userAgent, null, + listener, connectTimeoutMillis, + readTimeoutMillis, allowCrossProtocolRedirects, defaultRequestProperties).setKeyHelperModel(keyHelperModel); + return defaultHttpDataSource; + } + +} diff --git a/library/core/src/main/java/com/google/android/exoplayer2/vocab/CredUpdateSingleton.java b/library/core/src/main/java/com/google/android/exoplayer2/vocab/CredUpdateSingleton.java deleted file mode 100644 index 50f86bfd031..00000000000 --- a/library/core/src/main/java/com/google/android/exoplayer2/vocab/CredUpdateSingleton.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.google.android.exoplayer2.vocab; - -/** - * Created by Hisham on 04/Oct/2018 - 17:51 - */ -public class CredUpdateSingleton { - - private CredUpdateSingleton() { - } - - private static final CredUpdateSingleton ourInstance = new CredUpdateSingleton(); - - private String token; - private String videoId; - private String licecnceUrl; - - public static CredUpdateSingleton getInstance() { - return ourInstance; - } - - - public String getToken() { - return token; - } - - public CredUpdateSingleton setToken(String token) { - this.token = token; - return this; - } - - public String getVideoIdAndResetIt() { - String temp = videoId; - videoId = null; - return temp; - } - - public CredUpdateSingleton setVideoId(String videoId) { - this.videoId = videoId; - return this; - } - - public String getLicecnceUrl() { - return licecnceUrl; - } - - public CredUpdateSingleton setLicecnceUrl(String licecnceUrl) { - this.licecnceUrl = licecnceUrl; - return this; - } -} diff --git a/library/core/src/main/java/com/google/android/exoplayer2/vocab/KeyHelperModel.java b/library/core/src/main/java/com/google/android/exoplayer2/vocab/KeyHelperModel.java new file mode 100644 index 00000000000..d7a4e873a54 --- /dev/null +++ b/library/core/src/main/java/com/google/android/exoplayer2/vocab/KeyHelperModel.java @@ -0,0 +1,64 @@ +package com.google.android.exoplayer2.vocab; + +import java.io.Serializable; + +/** + * Created by Hisham on 10/Oct/2018 - 17:29 + */ +public class KeyHelperModel implements Serializable { + + private String m3u8Path; + private String token; + private String videoId; + private String licecnceUrl; + private String keyPath; + + public KeyHelperModel(){ + + } + + public String getM3u8Path() { + return m3u8Path; + } + + public KeyHelperModel setM3u8Path(String m3u8Path) { + this.m3u8Path = m3u8Path; + return this; + } + + public String getToken() { + return token; + } + + public KeyHelperModel setToken(String token) { + this.token = token; + return this; + } + + public String getVideoId() { + return videoId; + } + + public KeyHelperModel setVideoId(String videoId) { + this.videoId = videoId; + return this; + } + + public String getLicecnceUrl() { + return licecnceUrl; + } + + public KeyHelperModel setLicecnceUrl(String licecnceUrl) { + this.licecnceUrl = licecnceUrl; + return this; + } + + public String getKeyPath() { + return keyPath; + } + + public KeyHelperModel setKeyPath(String keyPath) { + this.keyPath = keyPath; + return this; + } +}