Skip to content

Commit

Permalink
feat: add android VideoRenderCallbac api
Browse files Browse the repository at this point in the history
  • Loading branch information
I-m-SuperMan committed Jan 17, 2020
1 parent 6c5768b commit 3dcc7cc
Show file tree
Hide file tree
Showing 6 changed files with 187 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -212,14 +212,14 @@ public interface CicadaPlayer {
abstract public void setTraceId(String traceId);

/**
* 设置音量。
* 设置音量(非系统音量),范围0.0~2.0,当音量大于1.0时,可能出现噪音,不推荐使用
*
* @param gain 范围[0,1]
* @param gain 范围[0,2]
*/
/****
* Set the volume of the player.
* Set the volume of the player(Not system volume). The range is 0.0~2.0,it maybe lead to noise if set volume more then 1.0, not recommended.
*
* @param gain Valid values: [0,1].
* @param gain Valid values: [0,2].
*/
abstract public void setVolume(float gain);

Expand Down Expand Up @@ -674,10 +674,10 @@ public int getValue() {
/**
* 跳转到。不精准。
*
* @param position 位置
* @param position 位置。单位毫秒。
*/
/****
* Specify a position for inaccurate seeking.
* Specify a position for inaccurate seeking. Unit: millisecond.
*
* @param position The specified position.
*/
Expand All @@ -686,13 +686,13 @@ public int getValue() {
/**
* 跳转到。
*
* @param position 位置。
* @param position 位置。单位毫秒。
* @param seekMode seek模式。见{@link SeekMode}。
*/
/****
* Seek to a specified position.
*
* @param position The specified position.
* @param position The specified position. Unit: millisecond.
* @param seekMode The specified seeking mode. See {@link SeekMode}.
*/
abstract public void seekTo(long position, SeekMode seekMode);
Expand Down Expand Up @@ -1099,6 +1099,19 @@ public interface OnTrackChangedListener {
* Subtitle display callback.
*/
public interface OnSubtitleDisplayListener {

/**
* 外挂字幕添加成功
* @param trackIndex 流id
* @param url 地址
*/
/****
* external subtitles added successfully
* @param trackIndex stream id
* @param url
*/
void onSubtitleExtAdded(int trackIndex,String url);

/**
* 显示字幕
*
Expand Down Expand Up @@ -1129,19 +1142,30 @@ public interface OnSubtitleDisplayListener {
*/
void onSubtitleHide(int trackIndex, long id);

/**
* 外挂字幕添加成功
* @param trackIndex 流id
* @param url 地址
*/
/****
* external subtitles added successfully
* @param trackIndex stream id
* @param url
*/
void onSubtitleExtAdded(int trackIndex,String url);
}

/**
* 添加外挂字幕
* @param url 字幕地址
*/
/****
* Add external subtitles
* @param url subtitle address
*/
abstract public void addExtSubtitle(String url);

/**
* 选择外挂字幕
* @param trackIndex 字幕索引
* @param select true:选择,false:关闭
*/
/****
* Select external subtitles
* @param trackIndex caption index
* @param select true: select, false: close
*/
abstract public void selectExtSubtitle(int trackIndex, boolean select);

/**
* 设置字幕显示通知
*
Expand Down Expand Up @@ -1268,26 +1292,50 @@ public static interface ConvertURLCallback {
*/
abstract public void reload();

/**
* 设置多码率时默认播放的码率。将会选择与之最接近的一路流播放。
* @param bandWidth 播放的码率。
*/
/****
* Set the default playback bitrate for multi-bit rate. The nearest stream will be selected.
* @param bandWidth bit rate .
*/
abstract public void setDefaultBandWidth(int bandWidth);

/**
* 添加外挂字幕
* @param url 字幕地址
*
*/
/****
* add subtitle
* @param url Subtitle address
*/
void addExtSubtitle(String url);
public static class Option {
/**
* 渲染的fps。类型为Float
*/
/****
* render fps. Return value type is Float
*/
public static Option RenderFPS = new Option("renderFps");

private String mValue;

private Option(String value) {
mValue = value;
}

public String getValue() {
return mValue;
}
}

/**
* 选择外挂字幕
* @param index 字幕索引
* @param select true:选择,false:关闭
* 获取播放器的参数
*
* @param key 参数值
* @return
*/
/****
* select subtitle
* @param index Subtitle index.
* @param select Set false to close.
*/
void selectExtSubtitle(int index, boolean select);
abstract public Object getOption(Option key);

public interface OnVideoRenderedListener{
void onVideoRendered(long timeMs , long pts);
}

abstract public void setOnVideoRenderedListener(OnVideoRenderedListener l);
}
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,30 @@ private void onStateChanged(int newState) {
}
}

private OnVideoRenderedListener mOutOnVideoRenderedListener = null;
private OnVideoRenderedListener mInnerOnVideoRenderedListener = new InnerVideoRenderedListener(this);

private static class InnerVideoRenderedListener implements OnVideoRenderedListener {
private WeakReference<CicadaPlayerImpl> cicadaPlayerImplWR;

InnerVideoRenderedListener(CicadaPlayerImpl avpBase) {
cicadaPlayerImplWR = new WeakReference<CicadaPlayerImpl>(avpBase);
}

@Override
public void onVideoRendered(long timeMs, long pts) {
CicadaPlayerImpl cicadaPlayerImpl = cicadaPlayerImplWR.get();
if (cicadaPlayerImpl != null) {
cicadaPlayerImpl.onVideoRendered(timeMs, pts);
}
}
}

private void onVideoRendered(long timeMs, long pts){
if(mOutOnVideoRenderedListener != null){
mOutOnVideoRenderedListener.onVideoRendered(timeMs ,pts);
}
};

public CicadaPlayerImpl(Context context, String traceID) {
mContext = context;
Expand Down Expand Up @@ -726,6 +750,7 @@ public void selectTrack(int trackInfoIndex) {
}

@Override
@Deprecated
public TrackInfo currentTrack(int type) {
return mCorePlayer.getCurrentTrackInfo(type);
}
Expand Down Expand Up @@ -994,6 +1019,27 @@ public void addExtSubtitle(String url){
public void selectExtSubtitle(int index, boolean select){
mCorePlayer.selectExtSubtitle(index,select);
}

@Override
public void setDefaultBandWidth(int bandWidth) {
mCorePlayer.setDefaultBandWidth(bandWidth);
}

@Override
public Object getOption(Option key) {
return mCorePlayer.getOption(key);
}

@Override
public void setOnVideoRenderedListener(OnVideoRenderedListener listener){
mOutOnVideoRenderedListener = listener;
if(mOutOnVideoRenderedListener!= null) {
mCorePlayer.setOnVideoRenderedListener(mInnerOnVideoRenderedListener);
}else{
mCorePlayer.setOnVideoRenderedListener(null);
}
}

}


Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,21 @@ public void setOption(String key, String value) {
nSetOption(key, value);
}

public Object getOption(CicadaPlayer.Option key) {
String optionValue = nGetOption(key.getValue());
if (optionValue == null) {
return null;
}
if (key == CicadaPlayer.Option.RenderFPS) {
try {
return Float.valueOf(optionValue);
} catch (Exception e) {
return Float.valueOf("0");
}

}
return optionValue;
}
public void setAutoPlay(boolean autoPlay) {
log(TAG, "setAutoPlay = " + autoPlay);
nSetAutoPlay(autoPlay);
Expand Down Expand Up @@ -412,6 +427,10 @@ public void selectExtSubtitle(int index, boolean select){
nSelectExtSubtitle(index,select);
}

public synchronized void setDefaultBandWidth(int bandWidth) {
nSetDefaultBandWidth(bandWidth);
}

public static String getSdkVersion() {
return nGetSdkVersion();
}
Expand Down Expand Up @@ -506,14 +525,20 @@ public static void setBlackType(int type) {

protected native void nSetOption(String key, String value);

protected native String nGetOption(String key);

protected native void nSetAutoPlay(boolean autoPlay);

protected native void nEnableVideoRenderedCallback(boolean enable);

protected native boolean nIsAutoPlay();

protected native void nSnapShot();

protected native String nGetCacheFilePath(String URL);

protected native void nSetDefaultBandWidth(int bandWidth);

protected static native String nGetSdkVersion();

protected static native void nSetBlackType(int type);
Expand All @@ -522,6 +547,7 @@ public static void setBlackType(int type) {
//////==========--------------==================------------------================//

private CicadaPlayer.OnVideoSizeChangedListener mOnVideoSizeChangedListener = null;
private CicadaPlayer.OnVideoRenderedListener mOnVideoRenderedListener = null;
private CicadaPlayer.OnInfoListener mOnInfoListener = null;
private CicadaPlayer.OnTrackReadyListener mOnTrackReadyListener = null;
private CicadaPlayer.OnPreparedListener mOnPreparedListener = null;
Expand Down Expand Up @@ -577,6 +603,12 @@ public void setOnVideoSizeChangedListener(CicadaPlayer.OnVideoSizeChangedListene
mOnVideoSizeChangedListener = l;
}

public void setOnVideoRenderedListener(CicadaPlayer.OnVideoRenderedListener l){
log(TAG, "setOnVideoRenderedListener = " + l);
mOnVideoRenderedListener = l;
nEnableVideoRenderedCallback(l != null);
}

public void setOnTrackSelectRetListener(CicadaPlayer.OnTrackChangedListener l) {
log(TAG, "setOnSwitchStreamResultListener = " + l);
mOnTrackChangedListener = l;
Expand Down Expand Up @@ -746,6 +778,18 @@ public void run() {
});
}

protected void onVideoRendered(final long timeMs, final long pts) {
log(TAG, "onVideoRendered = " + timeMs + " , pts = " + pts);
mCurrentThreadHandler.post(new Runnable() {
@Override
public void run() {
if (mOnVideoRenderedListener != null) {
mOnVideoRenderedListener.onVideoRendered(timeMs, pts);
}
}
});
}

protected void onStreamInfoGet(final MediaInfo mediaInfo) {
log(TAG, "onStreamInfoGet = " + mediaInfo.getTrackInfos().size());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ void JavaPlayerConfig::init(JNIEnv *env)
if (gj_PlayerConfig_class == nullptr) {
FindClass cls(env, playerConfigPath);
gj_PlayerConfig_class = (jclass) env->NewGlobalRef(
cls.getClass());
cls.getClass());
gj_PlayerConfig_Init = env->GetMethodID(gj_PlayerConfig_class,
"<init>",
"()V");
Expand Down Expand Up @@ -103,7 +103,6 @@ jobject JavaPlayerConfig::getJPlayerConfig(JNIEnv *mEnv, const MediaPlayerConfig
}

jobject jPlayerConfig = mEnv->NewObject(gj_PlayerConfig_class, gj_PlayerConfig_Init);
mEnv->SetIntField(jPlayerConfig, gj_playerconfig_MaxProbeSize, playerConfig->maxProbeSize);
mEnv->SetIntField(jPlayerConfig, gj_playerconfig_MaxBufferedPacketDuration,
playerConfig->maxBufferDuration);
mEnv->SetIntField(jPlayerConfig, gj_playerconfig_FirstStartBufferLevel,
Expand Down Expand Up @@ -193,7 +192,6 @@ MediaPlayerConfig JavaPlayerConfig::convertTo(JNIEnv *env, jobject playerConfig)
config.highBufferDuration = highBufferLevel;
config.maxBufferDuration = maxBufferedPacketDuration;
config.maxDelayTime = maxDelayTime;
config.maxProbeSize = maxProbeSize;
config.networkTimeout = networkTimeout;
config.bClearShowWhenStop = clearFrameWhenStop;
config.bEnableTunnelRender = enableTunnelRender;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,6 @@ void NativeBase::java_SelectTrack(JNIEnv *env, jobject instance, jint index)
player->SelectTrack(index);
}


void NativeBase::java_Prepare(JNIEnv *env, jobject instance)
{
AF_TRACE;
Expand Down Expand Up @@ -791,6 +790,17 @@ jstring NativeBase::java_GetCacheFilePathByURL(JNIEnv *env, jobject instance, js
return nullptr;
}


void NativeBase::java_SetDefaultBandWidth(JNIEnv *env, jobject instance, jint defaultBandWidth)
{
AF_TRACE;
MediaPlayer *player = getPlayer(env, instance);

if (player != nullptr) {
player->SetDefaultBandWidth((int)defaultBandWidth);
}
}

//callback...

void NativeBase::init(JNIEnv *env)
Expand Down Expand Up @@ -933,6 +943,7 @@ static JNINativeMethod nativePlayer_method_table[] = {
{"nGetSdkVersion", "()Ljava/lang/String;", (void *) NativeBase::java_GetSdkVersion},
{"nSetBlackType", "(I)V", (void *) NativeBase::java_SetBlackType},
{"nGetCacheFilePath", "(Ljava/lang/String;)Ljava/lang/String;", (void *) NativeBase::java_GetCacheFilePathByURL},
{"nSetDefaultBandWidth", "(I)V", (void *) NativeBase::java_SetDefaultBandWidth},

};

Expand Down
Loading

0 comments on commit 3dcc7cc

Please sign in to comment.