Skip to content

Commit

Permalink
custom protocol impl, working for online.
Browse files Browse the repository at this point in the history
  • Loading branch information
hishamMuneer committed Oct 16, 2018
1 parent 00e1b15 commit 2684afb
Show file tree
Hide file tree
Showing 24 changed files with 697 additions and 246 deletions.
8 changes: 8 additions & 0 deletions demos/main/src/main/assets/media.exolist.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
"name": "Encrypted Custom",
"uri": "https://vocatest-a40ab.firebaseapp.com/encrypted_android_no_key/prog_index.m3u8"
},
{
"name": "Video 30 - Master Playlist",
"uri":"https://voca2hosting.firebaseapp.com/vid30/playlist.m3u8"
},
{
"name": "Encrypted Custom Small",
"uri": "https://voca2hosting.firebaseapp.com/small_files/encrypted_without_key/index.m3u8"
Expand All @@ -41,6 +45,10 @@
{
"name": "Video from server in video list",
"uri":"http://54.152.186.92:60801/drm/static/tutorial/playlist.m3u8"
},
{
"name": "Video 20",
"uri":"http://54.152.186.92:60801/drm/static/video/inayat/sample_category/vid5/playlist.m3u8"
}
]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,16 +145,33 @@ public boolean onChildClick(ExpandableListView parent, View view, int groupPosit
}

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";
// 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";
// }

// keyHelperModel.setVideoId(String.valueOf(model.getVideoId()));
// keyHelperModel.setM3u8Path(RetroUtils.BASE_URL +"/drm/" + model.getM3u8Url());
// keyHelperModel.setToken("l8TmQpaBEdDGCtbefPfzTx54Bt4nOQLgaH8s3edJDhs=");
// keyHelperModel.setLicecnceUrl("http://54.152.186.92:60801/drm/get_key_for_a_video/20");

// return new KeyHelperModel().setVideoId("videoId: " + videoId)
// .setKeyPath(key)
// .setM3u8Path(sample.uri.toString());
//
if(sample.uri.toString().contains("vid30")) {
return new KeyHelperModel().setVideoId("videoId: " + videoId)
.setM3u8Path("https://voca2hosting.firebaseapp.com/vid30/playlist.m3u8")
.setLicecnceUrl("https://voca2hosting.firebaseapp.com/vid30/licence")
.setToken("rmaC0c9VqdoDDCku3MsXLJw_LL2IM_62zw8lOwfJsLU=");
}

return new KeyHelperModel().setVideoId("videoId: " + videoId)
.setKeyPath(key)
.setM3u8Path(sample.uri.toString());
.setM3u8Path("http://54.152.186.92:60801/drm/static/video/inayat/sample_category/vid5/playlist.m3u8")
.setLicecnceUrl("http://54.152.186.92:60801/drm/get_key_for_a_video/20")
.setToken("l8TmQpaBEdDGCtbefPfzTx54Bt4nOQLgaH8s3edJDhs=");
}

private void onSampleDownloadButtonClicked(Sample sample) {
Expand Down
2 changes: 1 addition & 1 deletion library/core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ dependencies {
testImplementation 'com.google.auto.value:auto-value-annotations:' + autoValueVersion
testAnnotationProcessor 'com.google.auto.value:auto-value:' + autoValueVersion
implementation 'com.google.code.gson:gson:2.8.2'

implementation project(':protocol')
}

ext {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
package com.google.android.exoplayer2.offline;

import android.util.Log;
import com.google.android.exoplayer2.upstream.HttpDataSource;
import com.google.android.exoplayer2.upstream.vocabimate_stream.VocaDataSourceHelper;
import com.vocabimate.protocol.LicenceModel;
import com.google.gson.Gson;
import com.google.gson.JsonObject;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;

public class LicenceObtainer {

Expand Down Expand Up @@ -59,7 +55,7 @@ public void getLicence() throws IOException
InputStream in = urlConnection.getInputStream();

String result = readStream(in);
listener.onLicenceReceived(new Gson().fromJson(result, VocaDataSourceHelper.LicenceModel.class));
listener.onLicenceReceived(new Gson().fromJson(result, LicenceModel.class));

/*InputStreamReader isw = new InputStreamReader(in);
int data = isw.read();
Expand Down Expand Up @@ -102,6 +98,6 @@ private String readStream(InputStream in) {


interface ILicenceData {
public void onLicenceReceived(VocaDataSourceHelper.LicenceModel licenceModel);
public void onLicenceReceived(LicenceModel licenceModel);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,12 @@
import com.google.android.exoplayer2.upstream.cache.CacheDataSource;
import com.google.android.exoplayer2.upstream.cache.CacheUtil;
import com.google.android.exoplayer2.upstream.cache.CacheUtil.CachingCounters;
import com.google.android.exoplayer2.upstream.vocabimate_stream.VocaDataSourceHelper;
import com.google.android.exoplayer2.util.PriorityTaskManager;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.ListIterator;
import java.util.concurrent.atomic.AtomicBoolean;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,16 @@
import android.text.TextUtils;
import android.util.Log;
import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.upstream.vocabimate_stream.AesEncryptionUtil;
import com.vocabimate.protocol.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.KeyHelperModel;
import com.google.gson.Gson;
import java.io.BufferedReader;

import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.InterruptedIOException;
import java.io.OutputStream;
import java.lang.reflect.Method;
Expand Down Expand Up @@ -267,121 +264,6 @@ public long open(DataSpec dataSpec) throws HttpDataSourceException { // Note: Th
return bytesToRead;
}

/**
* Custom method for custom scheme.
*/
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 videoId = keyHelper.getVideoId(); // todo check this
if (videoId == null) {
throw new NullPointerException("Video id is not set.");
}

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();
while (data != -1) {
char current = (char) data;
data = isw.read();
System.out.print(current);
}*/
} catch (Exception e) {
e.printStackTrace();
} finally {
if (httpURLConnection != null) {
httpURLConnection.disconnect();
}
}
} else {
keyUrl = new URL(keyHelper.getKeyPath());
}
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<String, String> property : defaultRequestProperties.getSnapshot()
.entrySet()) {
connection.setRequestProperty(property.getKey(), property.getValue());
}
}
for (Map.Entry<String, String> property : requestProperties.getSnapshot().entrySet()) {
connection.setRequestProperty(property.getKey(), property.getValue());
}
connection.setRequestProperty("User-Agent", userAgent);
connection.setRequestMethod("GET");
// if(!TextUtils.isEmpty(TokenManager.getToken())) {
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());
}
return connection;
}
}
return connection;
}

private String readStream(InputStream in) {
BufferedReader reader = null;
StringBuffer response = new StringBuffer();
try {
reader = new BufferedReader(new InputStreamReader(in));
String line = "";
while ((line = reader.readLine()) != null) {
response.append(line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return response.toString();
}


@Override
public int read(byte[] buffer, int offset, int readLength) throws HttpDataSourceException {
try {
Expand Down Expand Up @@ -464,10 +346,7 @@ private HttpURLConnection makeConnection(DataSpec dataSpec) throws IOException {
try {
url = new URL(dataSpec.uri.toString());
} catch (MalformedURLException e) {
if (e.getMessage().contains("vcb")) {
connection = makeConnectionCustom();
return connection;
}
e.printStackTrace();
}
byte[] postBody = dataSpec.postBody;
long position = dataSpec.position;
Expand Down Expand Up @@ -531,6 +410,7 @@ private HttpURLConnection makeConnection(URL url, byte[] postBody, long position
String token = keyHelperModel.getToken();
if(token != null && token.length() > 0) {
defaultRequestProperties.set("access_token", token);
defaultRequestProperties.set("licence_url", keyHelperModel.getLicecnceUrl());
}
}
}
Expand Down Expand Up @@ -722,13 +602,16 @@ private int readInternal(byte[] buffer, int offset, int readLength) throws IOExc
* We then override the read variable with the result received after our encryption.
* When we get -1 from read variable that means file has ended.
*/
if(dataSpec.key != null && dataSpec.key.contains(".key")) {
if(dataSpec.key != null && (dataSpec.key.contains(".key") || dataSpec.key.contains("vcb"))) {
// if(inputStream == null){
// read = parseData(buffer);
// } else {
read = inputStream.read(buffer);
// }
if (read == 16) {
byte[] testValue = new byte[16];
System.arraycopy(buffer, 0, testValue, 0, 16);
byte[] finalDataWritten = AesEncryptionUtil
.encrypt("Bar12345Bar12345", "pppppppppppppppp", testValue);
byte[] finalDataWritten = AesEncryptionUtil.encrypt("Bar12345Bar12345", "pppppppppppppppp", testValue);
System.arraycopy(finalDataWritten, 0, buffer, 0, finalDataWritten.length);
read = finalDataWritten.length;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import android.support.annotation.IntDef;
import android.text.TextUtils;

import com.google.android.exoplayer2.upstream.vocabimate_stream.AesEncryptionUtil;
import com.google.android.exoplayer2.util.Predicate;
import com.google.android.exoplayer2.util.Util;
import java.io.IOException;
Expand Down
Loading

0 comments on commit 2684afb

Please sign in to comment.