庖丁解牛之Android平台RTSP|RTMP播放器设计

2021年11月25日 阅读数:4
这篇文章主要向大家介绍庖丁解牛之Android平台RTSP|RTMP播放器设计,主要内容包括基础应用、实用技巧、原理机制等方面,希望对大家有所帮助。

背景

咱们在作Android平台RTSP或者RTMP播放器开发的时候,须要注意的点很是多,如下,以大牛直播SDK(​​官方​​)的接口为例,大概介绍下相关接口设计:java

接口设计

1. Open() 接口

Open接口的目的,主要是建立实例,正常返回player实例句柄,若有多路播放诉求,建立多个实例便可。android



  /**
* Initialize Player(启动播放实例)
*
* @param ctx: get by this.getApplicationContext()
*
* <pre>This function must be called firstly.</pre>
*
* @return player handle if successful, if return 0, which means init failed.
*/

public native long SmartPlayerOpen(Object ctx);

庖丁解牛之Android平台RTSP|RTMP播放器设计_android播放rtsp

2. Close()接口

Close接口,和Open()接口对应,负责释放相应实例的资源,调用Close()接口后,记得实例句柄置0便可。缓存

注意:好比一个实例既能够实现播放,又可同时录像,亦或拉流(转发),这种状况下,调Close()接口时,须要确保录像、拉流都正常中止后,再调用。服务器




/**
* 关闭播放实例,结束时必须调用close接口释放资源
*
* @param handle: return value from SmartPlayerOpen()
*
* <pre> NOTE: it could not use player handle after call this function. </pre>
*
* @return {0} if successful
*/
public native int SmartPlayerClose(long handle);

庖丁解牛之Android平台RTSP|RTMP播放器设计_android播放rtmp_02

3. 网络状态回调

一个好的播放器,好的状态回调必不可少,好比网络连通状态、快照、录像状态、当前下载速度等实时反馈,可让上层开发者更好的掌控播放端状态,给用户更好的播放体验。网络



  /**
* Set callback event(设置事件回调)
*
* @param handle: return value from SmartPlayerOpen()
*
* @param callbackv2: callback function
*
* @return {0} if successful
*/
public native int SetSmartPlayerEventCallbackV2(long handle, NTSmartEventCallbackV2 callbackv2);

庖丁解牛之Android平台RTSP|RTMP播放器设计_android播放rtmp_03

demo实现实例:tcp



    class EventHandeV2 implements NTSmartEventCallbackV2 {
@Override
public void onNTSmartEventCallbackV2(long handle, int id, long param1,
long param2, String param3, String param4, Object param5) {

//Log.i(TAG, "EventHandeV2: handle=" + handle + " id:" + id);

String player_event = "";

switch (id) {
case NTSmartEventID.EVENT_DANIULIVE_ERC_PLAYER_STARTED:
player_event = "开始..";
break;
case NTSmartEventID.EVENT_DANIULIVE_ERC_PLAYER_CONNECTING:
player_event = "链接中..";
break;
case NTSmartEventID.EVENT_DANIULIVE_ERC_PLAYER_CONNECTION_FAILED:
player_event = "链接失败..";
break;
case NTSmartEventID.EVENT_DANIULIVE_ERC_PLAYER_CONNECTED:
player_event = "链接成功..";
break;
case NTSmartEventID.EVENT_DANIULIVE_ERC_PLAYER_DISCONNECTED:
player_event = "链接断开..";
break;
case NTSmartEventID.EVENT_DANIULIVE_ERC_PLAYER_STOP:
player_event = "中止播放..";
break;
case NTSmartEventID.EVENT_DANIULIVE_ERC_PLAYER_RESOLUTION_INFO:
player_event = "分辨率信息: width: " + param1 + ", height: " + param2;
break;
case NTSmartEventID.EVENT_DANIULIVE_ERC_PLAYER_NO_MEDIADATA_RECEIVED:
player_event = "收不到媒体数据,多是url错误..";
break;
case NTSmartEventID.EVENT_DANIULIVE_ERC_PLAYER_SWITCH_URL:
player_event = "切换播放URL..";
break;
case NTSmartEventID.EVENT_DANIULIVE_ERC_PLAYER_CAPTURE_IMAGE:
player_event = "快照: " + param1 + " 路径:" + param3;

if (param1 == 0) {
player_event = player_event + ", 截取快照成功";
} else {
player_event = player_event + ", 截取快照失败";
}
break;

case NTSmartEventID.EVENT_DANIULIVE_ERC_PLAYER_RECORDER_START_NEW_FILE:
player_event = "[record]开始一个新的录像文件 : " + param3;
break;
case NTSmartEventID.EVENT_DANIULIVE_ERC_PLAYER_ONE_RECORDER_FILE_FINISHED:
player_event = "[record]已生成一个录像文件 : " + param3;
break;

case NTSmartEventID.EVENT_DANIULIVE_ERC_PLAYER_START_BUFFERING:
Log.i(TAG, "Start Buffering");
break;

case NTSmartEventID.EVENT_DANIULIVE_ERC_PLAYER_BUFFERING:
Log.i(TAG, "Buffering:" + param1 + "%");
break;

case NTSmartEventID.EVENT_DANIULIVE_ERC_PLAYER_STOP_BUFFERING:
Log.i(TAG, "Stop Buffering");
break;

case NTSmartEventID.EVENT_DANIULIVE_ERC_PLAYER_DOWNLOAD_SPEED:
player_event = "download_speed:" + param1 + "Byte/s" + ", "
+ (param1 * 8 / 1000) + "kbps" + ", " + (param1 / 1024)
+ "KB/s";
break;

case NTSmartEventID.EVENT_DANIULIVE_ERC_PLAYER_RTSP_STATUS_CODE:
Log.e(TAG, "RTSP error code received, please make sure username/password is correct, error code:" + param1);
player_event = "RTSP error code:" + param1;
break;

case NTSmartEventID.EVENT_DANIULIVE_ERC_PLAYER_NEED_KEY:
Log.e(TAG, "RTMP加密流,请设置播放须要的Key..");
player_event = "RTMP加密流,请设置播放须要的Key..";
break;

case NTSmartEventID.EVENT_DANIULIVE_ERC_PLAYER_KEY_ERROR:
Log.e(TAG, "RTMP加密流,Key错误,请从新设置..");
player_event = "RTMP加密流,Key错误,请从新设置..";
break;
}

if (player_event.length() > 0) {
Log.i(TAG, player_event);
Message message = new Message();
message.what = PLAYER_EVENT_MSG;
message.obj = player_event;
handler.sendMessage(message);
}
}
}

庖丁解牛之Android平台RTSP|RTMP播放器设计_android播放rtmp_04

4. 软解码仍是硬解码?

随着Android发展愈来愈好,各个厂商芯片对硬解码的支持,也愈来愈友好,通常状况下,若是适合通用的产品,在设备性能保障的状况下,优先建议软解,若是特定机型设备,可酌情考虑硬解,硬解码,又分为264硬解、HEVC硬解,直接设置surface模式的硬解,接口以下:ide



  /**
* Set Video H.264 HW decoder(设置H.264硬解码)
*
* @param handle: return value from SmartPlayerOpen()
*
* @param isHWDecoder: 0: software decoder; 1: hardware decoder.
*
* @return {0} if successful
*/
public native int SetSmartPlayerVideoHWDecoder(long handle, int isHWDecoder);

/**
* Set Video H.265(hevc) HW decoder(设置H.265硬解码)
*
* @param handle: return value from SmartPlayerOpen()
*
* @param isHevcHWDecoder: 0: software decoder; 1: hardware decoder.
*
* @return {0} if successful
*/
public native int SetSmartPlayerVideoHevcHWDecoder(long handle, int isHevcHWDecoder);

庖丁解牛之Android平台RTSP|RTMP播放器设计_android rtsp播放器_05

设置surface模式的硬解:oop



  /**
* 设置视频硬解码下Mediacodec自行绘制模式(此种模式下,硬解码兼容性和效率更好,回调YUV/RGB和快照功能将不可用)
*
* @param handle: return value from SmartPlayerOpen()
*
* @param isHWRenderMode: 0: not enable; 1: 用SmartPlayerSetSurface设置的surface自行绘制
*
* @return {0} if successful
*/
public native int SmartPlayerSetHWRenderMode(long handle, int isHWRenderMode);

/**
* 更新硬解码surface
*
* @param handle: return value from SmartPlayerOpen()
*
* @return {0} if successful
*/
public native int SmartPlayerUpdateHWRenderSurface(long handle);

庖丁解牛之Android平台RTSP|RTMP播放器设计_android rtsp播放器_06

5. 音频输出类型

音频输出,android平台支持audiotrack模式和opensl es模式,通常来讲,考虑到设备通用性,建议采用audiotrack模式。性能



  /**
* Set AudioOutput Type(设置audio输出类型)
*
* @param handle: return value from SmartPlayerOpen()
*
* @param use_audiotrack:
*
* <pre> NOTE: if use_audiotrack with 0: it will use auto-select output devices; if with 1: will use audio-track mode. </pre>
*
* @return {0} if successful
*/
public native int SmartPlayerSetAudioOutputType(long handle, int use_audiotrack);

庖丁解牛之Android平台RTSP|RTMP播放器设计_android rtmp player_07

6. 缓冲时间设置

缓冲时间,顾名思义,缓存多少数据才开始播放,好比设置2000ms的buffer time,直播模式下,收到2秒数据后,才正常播放。ui

加大buffer time,会增大播放延迟,好处是,网络抖动的时候,流畅性更好。



  /**
* Set buffer(设置缓冲时间,单位:毫秒)
*
* @param handle: return value from SmartPlayerOpen()
*
* @param buffer:
*
* <pre> NOTE: Unit is millisecond, range is 0-5000 ms </pre>
*
* @return {0} if successful
*/
public native int SmartPlayerSetBuffer(long handle, int buffer);

庖丁解牛之Android平台RTSP|RTMP播放器设计_android rtsp播放器_08

7. 实时静音、实时音量调节

实时静音、实时音量调节顾名思义,播放端能够实时调整播放音量,或者直接静音掉,特别是多路播放场景下,很是有必要。

此外咱们还提供了音量放大的功能,支持放大至原始音量的2倍。



  /**
* Set mute or not(设置实时静音)
*
* @param handle: return value from SmartPlayerOpen()
*
* @param is_mute: if with 1:mute, if with 0: does not mute
*
* @return {0} if successful
*/
public native int SmartPlayerSetMute(long handle, int is_mute);

/**
* 设置播放音量
*
* @param handle: return value from SmartPlayerOpen()
*
* @param volume: 范围是[0, 100], 0是静音,100是最大音量, 默认是100
*
* @return {0} if successful
*/
public native int SmartPlayerSetAudioVolume(long handle, int volume);

庖丁解牛之Android平台RTSP|RTMP播放器设计_android rtsp播放器_09

8. RTSP TCP-UDP模式设置、超时时间设置或模式切换

有的RTSP服务器或摄像机,只支持RTSP TCP模式或者UDP模式,这个时候,默认设置TCP、UDP模式就相当重要,此外,咱们还设计支持如TCP或UDP模式收不到数据,在超时时间后,能够自动切换到UDP或TCP。



  /**
* 设置RTSP TCP/UDP模式(默认UDP模式)
*
* @param handle: return value from SmartPlayerOpen()
*
* @param is_using_tcp: if with 1, it will via TCP mode, while 0 with UDP mode
*
* @return {0} if successful
*/
public native int SmartPlayerSetRTSPTcpMode(long handle, int is_using_tcp);

/**
* 设置RTSP超时时间, timeout单位为秒,必须大于0
*
* @param handle: return value from SmartPlayerOpen()
*
* @param timeout: RTSP timeout setting
*
* @return {0} if successful
*/
public native int SmartPlayerSetRTSPTimeout(long handle, int timeout);

/**
* 设置RTSP TCP/UDP自动切换
*
* @param handle: return value from SmartPlayerOpen()
*
* NOTE: 对于RTSP来讲,有些可能支持rtp over udp方式,有些可能支持使用rtp over tcp方式.
* 为了方便使用,有些场景下能够开启自动尝试切换开关, 打开后若是udp没法播放,sdk会自动尝试tcp, 若是tcp方式播放不了,sdk会自动尝试udp.
*
* @param is_auto_switch_tcp_udp 若是设置1的话, sdk将在tcp和udp之间尝试切换播放,若是设置为0,则不尝试切换.
*
* @return {0} if successful
*/
public native int SmartPlayerSetRTSPAutoSwitchTcpUdp(long handle, int is_auto_switch_tcp_udp);

庖丁解牛之Android平台RTSP|RTMP播放器设计_android播放rtsp_10

9. 快速启动

快速启动,主要是针对服务器缓存GOP的场景下,快速刷到最新的数据,确保画面的持续性。



  /**
* Set fast startup(设置快速启动模式,此模式针对服务器缓存GOP的场景有效)
*
* @param handle: return value from SmartPlayerOpen()
*
* @param is_fast_startup: if with 1, it will second play back, if with 0: does not it
*
* @return {0} if successful
*/
public native int SmartPlayerSetFastStartup(long handle, int is_fast_startup);

庖丁解牛之Android平台RTSP|RTMP播放器设计_android播放rtsp_11

10. 低延迟模式

低延迟模式下,设置buffer time为0,延迟更低,适用于好比须要操控控制的超低延迟场景下。



  /**
* Set low latency mode(设置低延迟模式)
*
* @param handle: return value from SmartPlayerOpen()
*
* @param mode: if with 1, low latency mode, if with 0: normal mode
*
* @return {0} if successful
*/
public native int SmartPlayerSetLowLatencyMode(long handle, int mode);

庖丁解牛之Android平台RTSP|RTMP播放器设计_android rtmp player_12

11. 视频view旋转、水平|垂直翻转

接口主要用于,好比原始的视频倒置等场景下,设备端没法调整时,经过播放端完成图像的正常角度播放。



  /**
* 设置视频垂直反转
*
* @param handle: return value from SmartPlayerOpen()
*
* @param is_flip: 0: 不反转, 1: 反转
*
* @return {0} if successful
*/
public native int SmartPlayerSetFlipVertical(long handle, int is_flip);

/**
* 设置视频水平反转
*
* @param handle: return value from SmartPlayerOpen()
*
* @param is_flip: 0: 不反转, 1: 反转
*
* @return {0} if successful
*/
public native int SmartPlayerSetFlipHorizontal(long handle, int is_flip);

/**
* 设置顺时针旋转, 注意除了0度以外, 其余角度都会额外消耗性能
*
* @param handle: return value from SmartPlayerOpen()
*
* @param degress: 当前支持 0度,90度, 180度, 270度 旋转
*
* @return {0} if successful
*/
public native int SmartPlayerSetRotation(long handle, int degress);

庖丁解牛之Android平台RTSP|RTMP播放器设计_android播放rtmp_13

12. 设置实时回调下载速度

调用实时下载速度接口,经过设置下载速度时间间隔,和是否须要上报当前下载速度,实现APP层和底层SDK更友好的交互。



  /**
* Set report download speed(设置实时回调下载速度)
*
* @param handle: return value from SmartPlayerOpen()
*
* @param is_report: if with 1, it will report download speed, it with 0: does not it.
*
* @param report_interval: report interval, unit is second, it must be greater than 0.
*
* @return {0} if successful
*/
public native int SmartPlayerSetReportDownloadSpeed(long handle, int is_report, int report_interval );

庖丁解牛之Android平台RTSP|RTMP播放器设计_android播放rtsp_14

13. 实时快照

简单来讲,播放过程当中,是否是要存取当前的播放画面。



  /**
* Set if needs to save image during playback stream(是否启动快照功能)
*
* @param handle: return value from SmartPlayerOpen()
*
* @param is_save_image: if with 1, it will save current image via the interface of SmartPlayerSaveCurImage(), if with 0: does not it
*
* @return {0} if successful
*/
public native int SmartPlayerSaveImageFlag(long handle, int is_save_image);

/**
* Save current image during playback stream(实时快照)
*
* @param handle: return value from SmartPlayerOpen()
*
* @param imageName: image name, which including fully path, "/sdcard/daniuliveimage/daniu.png", etc.
*
* @return {0} if successful
*/
public native int SmartPlayerSaveCurImage(long handle, String imageName);

庖丁解牛之Android平台RTSP|RTMP播放器设计_android rtsp播放器_15

14. 扩展录像操做

播放端录像,咱们作的很是细化,好比能够只录制音频或者只录制视频,设置录像存储路径,设置单个文件size,若是非AAC数据,能够转AAC后再录像。



  /**
* Create file directory(建立录像目录)
*
* @param path, E.g: /sdcard/daniulive/rec
*
* <pre> The interface is only used for recording the stream data to local side. </pre>
*
* @return {0} if successful
*/
public native int SmartPlayerCreateFileDirectory(String path);

/**
* Set recorder directory(设置录像目录)
*
* @param handle: return value from SmartPlayerOpen()
*
* @param path: the directory of recorder file
*
* <pre> NOTE: make sure the path should be existed, or else the setting failed. </pre>
*
* @return {0} if successful
*/
public native int SmartPlayerSetRecorderDirectory(long handle, String path);

/**
* Set the size of every recorded file(设置单个录像文件大小,如超过设定大小则自动切换到下个文件录制)
*
* @param handle: return value from SmartPlayerOpen()
*
* @param size: (MB), (5M~500M), if not in this range, set default size with 200MB.
*
* @return {0} if successful
*/
public native int SmartPlayerSetRecorderFileMaxSize(long handle, int size);

/*
* 设置录像时音频转AAC编码的开关
*
* @param handle: return value from SmartPlayerOpen()
*
* aac比较通用,sdk增长其余音频编码(好比speex, pcmu, pcma等)转aac的功能.
*
* @param is_transcode: 设置为1的话,若是音频编码不是aac,则转成aac,若是是aac,则不作转换. 设置为0的话,则不作任何转换. 默认是0.
*
* 注意: 转码会增长性能消耗
*
* @return {0} if successful
*/
public native int SmartPlayerSetRecorderAudioTranscodeAAC(long handle, int is_transcode);


/*
*设置是否录视频,默认的话,若是视频源有视频就录,没有就没得录, 但有些场景下可能不想录制视频,只想录音频,因此增长个开关
*
*@param is_record_video: 1 表示录制视频, 0 表示不录制视频, 默认是1
*
* @return {0} if successful
*/
public native int SmartPlayerSetRecorderVideo(long handle, int is_record_video);


/*
*设置是否录音频,默认的话,若是视频源有音频就录,没有就没得录, 但有些场景下可能不想录制音频,只想录视频,因此增长个开关
*
*@param is_record_audio: 1 表示录制音频, 0 表示不录制音频, 默认是1
*
* @return {0} if successful
*/
public native int SmartPlayerSetRecorderAudio(long handle, int is_record_audio);

庖丁解牛之Android平台RTSP|RTMP播放器设计_android rtmp播放器_16


  /**
* Start recorder stream(开始录像)
*
* @param handle: return value from SmartPlayerOpen()
*
* @return {0} if successful
*/
public native int SmartPlayerStartRecorder(long handle);

/**
* Stop recorder stream(中止录像)
*
* @param handle: return value from SmartPlayerOpen()
*
* @return {0} if successful
*/
public native int SmartPlayerStopRecorder(long handle);

庖丁解牛之Android平台RTSP|RTMP播放器设计_android rtmp播放器_17

15. 拉流回调编码后的数据(配合转发模块使用)

拉流回调编码后的数据,主要是为了配合转发模块使用,好比拉取rtsp流数据,直接转RTMP推送到RTMP服务。



  /*
* 设置拉流时音频转AAC编码的开关
*
* @param handle: return value from SmartPlayerOpen()
*
* aac比较通用,sdk增长其余音频编码(好比speex, pcmu, pcma等)转aac的功能.
*
* @param is_transcode: 设置为1的话,若是音频编码不是aac,则转成aac, 若是是aac,则不作转换. 设置为0的话,则不作任何转换. 默认是0.

* 注意: 转码会增长性能消耗
*
* @return {0} if successful
*/
public native int SmartPlayerSetPullStreamAudioTranscodeAAC(long handle, int is_transcode);

/**
* Start pull stream(开始拉流,用于数据转发,只拉流不播放)
*
* @param handle: return value from SmartPlayerOpen()
*
* @return {0} if successful
*/
public native int SmartPlayerStartPullStream(long handle);

/**
* Stop pull stream(中止拉流)
*
* @param handle: return value from SmartPlayerOpen()
*
* @return {0} if successful
*/
public native int SmartPlayerStopPullStream(long handle);

庖丁解牛之Android平台RTSP|RTMP播放器设计_android rtmp player_18


  /**
* Set Audio Data Callback(设置回调编码后音频数据)
*
* @param handle: return value from SmartPlayerOpen()
*
* @param audio_data_callback: Audio Data Callback.
*
* @return {0} if successful
*/
public native int SmartPlayerSetAudioDataCallback(long handle, Object audio_data_callback);

/**
* Set Video Data Callback(设置回调编码后视频数据)
*
* @param handle: return value from SmartPlayerOpen()
*
* @param video_data_callback: Video Data Callback.
*
* @return {0} if successful
*/
public native int SmartPlayerSetVideoDataCallback(long handle, Object video_data_callback);

庖丁解牛之Android平台RTSP|RTMP播放器设计_android rtmp播放器_19

16. H264用户数据回调或SEI数据回调

如发送端在264编码时,加了自定义的user data数据,能够经过如下接口实现数据回调,如需直接回调SEI数据,调下面SEI回调接口便可。



  /**
* Set user data Callback(设置回调SEI扩展用户数据)
*
* @param handle: return value from SmartPlayerOpen()
*
* @param user_data_callback: user data callback.
*
* @return {0} if successful
*/
public native int SmartPlayerSetUserDataCallback(long handle, Object user_data_callback);

/**
* Set SEI data Callback(设置回调SEI数据)
*
* @param handle: return value from SmartPlayerOpen()
*
* @param sei_data_callback: sei data callback.
*
* @return {0} if successful
*/
public native int SmartPlayerSetSEIDataCallback(long handle, Object sei_data_callback);

庖丁解牛之Android平台RTSP|RTMP播放器设计_android rtsp播放器_20

17. 设置回调解码后YUV、RGB数据

如需对解码后的yuv或rgb数据,进行二次处理,如人脸识别等,能够通回调yuv rgb接口实现数据二次处理。

以YUV为例:



    class I420ExternalRender implements NTExternalRender {
// public static final int NT_FRAME_FORMAT_RGBA = 1;
// public static final int NT_FRAME_FORMAT_ABGR = 2;
// public static final int NT_FRAME_FORMAT_I420 = 3;

private int width_ = 0;
private int height_ = 0;

private int y_row_bytes_ = 0;
private int u_row_bytes_ = 0;
private int v_row_bytes_ = 0;

private ByteBuffer y_buffer_ = null;
private ByteBuffer u_buffer_ = null;
private ByteBuffer v_buffer_ = null;

@Override
public int getNTFrameFormat() {
Log.i(TAG, "I420ExternalRender::getNTFrameFormat return "
+ NT_FRAME_FORMAT_I420);
return NT_FRAME_FORMAT_I420;
}

@Override
public void onNTFrameSizeChanged(int width, int height) {
width_ = width;
height_ = height;

y_row_bytes_ = (width_ + 15) & (~15);
u_row_bytes_ = ((width_ + 1) / 2 + 15) & (~15);
v_row_bytes_ = ((width_ + 1) / 2 + 15) & (~15);

y_buffer_ = ByteBuffer.allocateDirect(y_row_bytes_ * height_);
u_buffer_ = ByteBuffer.allocateDirect(u_row_bytes_
* ((height_ + 1) / 2));
v_buffer_ = ByteBuffer.allocateDirect(v_row_bytes_
* ((height_ + 1) / 2));

Log.i(TAG, "I420ExternalRender::onNTFrameSizeChanged width_="
+ width_ + " height_=" + height_ + " y_row_bytes_="
+ y_row_bytes_ + " u_row_bytes_=" + u_row_bytes_
+ " v_row_bytes_=" + v_row_bytes_);
}

@Override
public ByteBuffer getNTPlaneByteBuffer(int index) {
if (index == 0) {
return y_buffer_;
} else if (index == 1) {
return u_buffer_;
} else if (index == 2) {
return v_buffer_;
} else {
Log.e(TAG, "I420ExternalRender::getNTPlaneByteBuffer index error:" + index);
return null;
}
}

@Override
public int getNTPlanePerRowBytes(int index) {
if (index == 0) {
return y_row_bytes_;
} else if (index == 1) {
return u_row_bytes_;
} else if (index == 2) {
return v_row_bytes_;
} else {
Log.e(TAG, "I420ExternalRender::getNTPlanePerRowBytes index error:" + index);
return 0;
}
}

public void onNTRenderFrame(int width, int height, long timestamp) {
if (y_buffer_ == null)
return;

if (u_buffer_ == null)
return;

if (v_buffer_ == null)
return;


y_buffer_.rewind();

u_buffer_.rewind();

v_buffer_.rewind();

/*
if ( !is_saved_image )
{
is_saved_image = true;

int y_len = y_row_bytes_*height_;

int u_len = u_row_bytes_*((height_+1)/2);
int v_len = v_row_bytes_*((height_+1)/2);

int data_len = y_len + (y_row_bytes_*((height_+1)/2));

byte[] nv21_data = new byte[data_len];

byte[] u_data = new byte[u_len];
byte[] v_data = new byte[v_len];

y_buffer_.get(nv21_data, 0, y_len);
u_buffer_.get(u_data, 0, u_len);
v_buffer_.get(v_data, 0, v_len);

int[] strides = new int[2];
strides[0] = y_row_bytes_;
strides[1] = y_row_bytes_;


int loop_row_c = ((height_+1)/2);
int loop_c = ((width_+1)/2);

int dst_row = y_len;
int src_v_row = 0;
int src_u_row = 0;

for ( int i = 0; i < loop_row_c; ++i)
{
int dst_pos = dst_row;

for ( int j = 0; j <loop_c; ++j )
{
nv21_data[dst_pos++] = v_data[src_v_row + j];
nv21_data[dst_pos++] = u_data[src_u_row + j];
}

dst_row += y_row_bytes_;
src_v_row += v_row_bytes_;
src_u_row += u_row_bytes_;
}

String imagePath = "/sdcard" + "/" + "testonv21" + ".jpeg";

Log.e(TAG, "I420ExternalRender::begin test save iamge++ image_path:" + imagePath);

try
{
File file = new File(imagePath);

FileOutputStream image_os = new FileOutputStream(file);

YuvImage image = new YuvImage(nv21_data, ImageFormat.NV21, width_, height_, strides);

image.compressToJpeg(new android.graphics.Rect(0, 0, width_, height_), 50, image_os);

image_os.flush();
image_os.close();
}
catch(IOException e)
{
e.printStackTrace();
}

Log.e(TAG, "I420ExternalRender::begin test save iamge--");
}

*/


Log.i(TAG, "I420ExternalRender::onNTRenderFrame w=" + width + " h=" + height + " timestamp=" + timestamp);

// copy buffer

// test
// byte[] test_buffer = new byte[16];
// y_buffer_.get(test_buffer);

// Log.i(TAG, "I420ExternalRender::onNTRenderFrame y data:" + bytesToHexString(test_buffer));

// u_buffer_.get(test_buffer);
// Log.i(TAG, "I420ExternalRender::onNTRenderFrame u data:" + bytesToHexString(test_buffer));

// v_buffer_.get(test_buffer);
// Log.i(TAG, "I420ExternalRender::onNTRenderFrame v data:" + bytesToHexString(test_buffer));
}
}

庖丁解牛之Android平台RTSP|RTMP播放器设计_android rtmp播放器_21

18. 设置视频画面填充模式

设置视频画面的填充模式,如填充整个view、等比例填充view,如不设置,默认填充整个view。

相关接口设计以下:



  /**
* 设置视频画面的填充模式,如填充整个view、等比例填充view,如不设置,默认填充整个view
* @param handle: return value from SmartPlayerOpen()
* @param render_scale_mode 0: 填充整个view; 1: 等比例填充view, 默认值是0
* @return {0} if successful
*/
public native int SmartPlayerSetRenderScaleMode(long handle, int render_scale_mode);

庖丁解牛之Android平台RTSP|RTMP播放器设计_android rtmp播放器_22

19. 设置SurfaceView模式下render类型

format: 0: RGB565格式,如不设置,默认此模式; 1: ARGB8888格式



  /**
* 设置SurfaceView模式下(NTRenderer.CreateRenderer第二个参数传false的状况),render类型
*
* @param handle: return value from SmartPlayerOpen()
*
* @param format: 0: RGB565格式,如不设置,默认此模式; 1: ARGB8888格式
*
* @return {0} if successful
*/
public native int SmartPlayerSetSurfaceRenderFormat(long handle, int format);

庖丁解牛之Android平台RTSP|RTMP播放器设计_android播放rtmp_23

20. 设置surfaceview模式下抗锯齿

须要注意的是:抗锯齿模式开启后,会增长性能消耗。



  /**
* 设置SurfaceView模式下(NTRenderer.CreateRenderer第二个参数传false的状况),抗锯齿效果,注意:抗锯齿模式开启后,可能会影像性能,请慎用
*
* @param handle: return value from SmartPlayerOpen()
*
* @param isEnableAntiAlias: 0: 如不设置,默认不开启抗锯齿模式; 1: 开启抗锯齿模式
*
* @return {0} if successful
*/
public native int SmartPlayerSetSurfaceAntiAlias(long handle, int isEnableAntiAlias);

庖丁解牛之Android平台RTSP|RTMP播放器设计_android rtsp播放器_24

总结

以上就是Android平台RTSP、RTMP播放器接口设计须要参考的点,对于大多数开发者来讲,不必定须要实现上述全部部分,只要按照产品诉求,实现其中的40%就足够知足特定场景使用了。

一个好的播放器,特别是要知足低延迟稳定的播放(毫秒级延迟),须要注意的点远不止如此,感兴趣的开发者,能够参考blog其余文章。