Skip to content

Commit

Permalink
feat(android): add exo external player support
Browse files Browse the repository at this point in the history
  • Loading branch information
I-m-SuperMan authored and pingkai committed Aug 4, 2020
1 parent a22d75d commit 150381d
Show file tree
Hide file tree
Showing 37 changed files with 3,907 additions and 80 deletions.
4 changes: 4 additions & 0 deletions framework/base/options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,7 @@ void Cicada::options::reset()
{
mDict.clear();
}

std::map<std::string, std::string> Cicada::options::getOptions() {
return mDict;
}
2 changes: 2 additions & 0 deletions framework/base/options.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ namespace Cicada {

void reset();

std::map<std::string, std::string> getOptions();

private:
std::map<std::string, std::string> mDict;
};
Expand Down
17 changes: 7 additions & 10 deletions framework/utils/Android/JniUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,29 @@
#include "CallObjectMethod.h"
#include "GetStringUTFChars.h"

char *JniUtils::jByteArrayToChars(JNIEnv *env, jbyteArray bytearray)
{
char *JniUtils::jByteArrayToChars(JNIEnv *env, jbyteArray bytearray) {
jbyte *bytes = env->GetByteArrayElements(bytearray, 0);
int chars_len = env->GetArrayLength(bytearray);
char *chars = static_cast<char *>(malloc(chars_len));
char *chars = static_cast<char *>(malloc(chars_len + 1));
memcpy(chars, bytes, chars_len);
chars[chars_len] = 0;
env->ReleaseByteArrayElements(bytearray, bytes, 0);
JniException::clearException(env);
return chars;
}

char *JniUtils::jByteArrayToChars_New(JNIEnv *env, jbyteArray bytearray)
{
char *JniUtils::jByteArrayToChars_New(JNIEnv *env, jbyteArray bytearray) {
jbyte *bytes = env->GetByteArrayElements(bytearray, 0);
int chars_len = env->GetArrayLength(bytearray);
char *chars = new char[chars_len + 1]();
memcpy(chars, bytes, chars_len);
chars[chars_len] = 0;
env->ReleaseByteArrayElements(bytearray, bytes, 0);
JniException::clearException(env);
return chars;
}


jobject JniUtils::cmap2Jmap(JNIEnv *env, std::map<std::string, std::string> cmap)
{
jobject JniUtils::cmap2Jmap(JNIEnv *env, std::map<std::string, std::string> cmap) {
FindClass jmapclass(env, "java/util/HashMap");
jmethodID initMethod = env->GetMethodID(jmapclass.getClass(), "<init>", "()V");
jmethodID putMethod = env->GetMethodID(jmapclass.getClass(), "put",
Expand All @@ -55,8 +53,7 @@ jobject JniUtils::cmap2Jmap(JNIEnv *env, std::map<std::string, std::string> cmap
}


std::string JniUtils::callStringMethod(JNIEnv *env, jobject jObj, jmethodID method)
{
std::string JniUtils::callStringMethod(JNIEnv *env, jobject jObj, jmethodID method) {
CallObjectMethod tmpGetObject(env, jObj, method);
auto objec = (jstring) tmpGetObject.getValue();
GetStringUTFChars tmpObj(env, objec);
Expand Down
10 changes: 5 additions & 5 deletions mediaPlayer/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,11 @@ if(APPLE)
externalPlayer/AppleAVPlayerUtil.h
externalPlayer/AppleAVPlayerUtil.m
)
elseif(ANDROID)
set(SOURCE_FILES ${SOURCE_FILES}
externalPlayer/JNIPlayer.cpp
externalPlayer/JNIPlayer.h
)
#elseif(ANDROID)
# set(SOURCE_FILES ${SOURCE_FILES}
# externalPlayer/JNIPlayer.cpp
# externalPlayer/JNIPlayer.h
# )
endif()
if (TARGET_PLATFORM STREQUAL "Android")
set(SOURCE_FILES ${SOURCE_FILES}
Expand Down
5 changes: 0 additions & 5 deletions mediaPlayer/externalPlayer/JNIPlayer.cpp

This file was deleted.

15 changes: 0 additions & 15 deletions mediaPlayer/externalPlayer/JNIPlayer.h

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
31 changes: 31 additions & 0 deletions platform/Android/source/ExternalPlayerExoLibrary/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
apply plugin: 'com.android.library'

android {
compileSdkVersion 30
buildToolsVersion "30.0.0"

defaultConfig {
minSdkVersion 14
targetSdkVersion 30
versionCode 1
versionName "1.0"

}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])

implementation project(':premierlibrary')

//ExoPlayer
implementation 'com.google.android.exoplayer:exoplayer:2.9.6'
implementation 'com.android.support:support-annotations:28.0.0'
}
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.aliyun.externalplayer.exo">

/
</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
package com.aliyun.externalplayer.exo;

import android.support.annotation.Nullable;
import android.text.TextUtils;

import com.google.android.exoplayer2.upstream.DefaultHttpDataSource;
import com.google.android.exoplayer2.upstream.HttpDataSource;
import com.google.android.exoplayer2.upstream.TransferListener;

import java.util.ArrayList;
import java.util.List;

public class ExternHttpDataSourceFactory extends HttpDataSource.BaseFactory {

private final @Nullable
TransferListener listener;
private final boolean allowCrossProtocolRedirects;
List<String> mHttpHeaders = new ArrayList<>();
private String userAgent;
private int connectTimeoutMillis;
private int readTimeoutMillis;

public ExternHttpDataSourceFactory() {
this(ExternHttpDataSourceFactory.class.getSimpleName(), null);
}

/**
* 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 ExternHttpDataSourceFactory(String userAgent) {
this(userAgent, null);
}

/**
* 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.
* @see #ExternHttpDataSourceFactory(String, TransferListener, int, int, boolean)
*/
public ExternHttpDataSourceFactory(String userAgent, @Nullable TransferListener listener) {
this(userAgent, listener, DefaultHttpDataSource.DEFAULT_CONNECT_TIMEOUT_MILLIS,
DefaultHttpDataSource.DEFAULT_READ_TIMEOUT_MILLIS, false);
}

/**
* @param userAgent The User-Agent string that should be used.
* @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 ExternHttpDataSourceFactory(
String userAgent,
int connectTimeoutMillis,
int readTimeoutMillis,
boolean allowCrossProtocolRedirects) {
this(
userAgent,
/* listener= */ null,
connectTimeoutMillis,
readTimeoutMillis,
allowCrossProtocolRedirects);
}

/**
* @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 ExternHttpDataSourceFactory(
String userAgent,
@Nullable TransferListener listener,
int connectTimeoutMillis,
int readTimeoutMillis,
boolean allowCrossProtocolRedirects) {
this.userAgent = userAgent;
this.listener = listener;
this.connectTimeoutMillis = connectTimeoutMillis;
this.readTimeoutMillis = readTimeoutMillis;
this.allowCrossProtocolRedirects = allowCrossProtocolRedirects;
}

public void addHttpHeaders(String httpHeader) {
if (!TextUtils.isEmpty(httpHeader)) {
mHttpHeaders.add(httpHeader);
}
}

public void clearHttpHeaders() {
mHttpHeaders.clear();
}

public void setConnectTimeoutMillis(int timeoutMillis) {
connectTimeoutMillis = timeoutMillis;
}

public void setReadTimeoutMillis(int timeoutMillis) {
readTimeoutMillis = timeoutMillis;
}

@Override
protected DefaultHttpDataSource createDataSourceInternal(
HttpDataSource.RequestProperties defaultRequestProperties) {
DefaultHttpDataSource dataSource =
new DefaultHttpDataSource(
TextUtils.isEmpty(userAgent) ? ExternHttpDataSourceFactory.class.getSimpleName() : userAgent,
/* contentTypePredicate= */ null,
connectTimeoutMillis,
readTimeoutMillis,
allowCrossProtocolRedirects,
defaultRequestProperties);
for (String header : mHttpHeaders) {
String[] keyValue = header.split(":");
if (keyValue.length != 2) {
continue;
}
dataSource.setRequestProperty(keyValue[0], keyValue[1]);
}

if (listener != null) {
dataSource.addTransferListener(listener);
}
return dataSource;
}

public void setUserAgent(String userAgent) {
this.userAgent = userAgent;
}
}
Loading

0 comments on commit 150381d

Please sign in to comment.