相城区网站建设,广州微网站建设机构,宁波高端网站制作公司,itc会议系统我们需要怎样的直播播放器#xff1f;
很多开发者在跟我聊天的时候#xff0c;经常问我#xff0c;为什么一个RTMP或RTSP播放器#xff0c;你们需要设计那么多的接口#xff0c;真的有必要吗#xff1f;带着这样的疑惑#xff0c;我们今天聊聊Android平台RTMP、RTSP播放…我们需要怎样的直播播放器
很多开发者在跟我聊天的时候经常问我为什么一个RTMP或RTSP播放器你们需要设计那么多的接口真的有必要吗带着这样的疑惑我们今天聊聊Android平台RTMP、RTSP播放器常规功能如软硬解码设置、实时音量调节、实时快照、实时录像、视频view翻转和旋转、画面填充模式设定、解码后YUV、RGB数据回调等 延迟延迟延迟
RTMP或RTSP直播播放器特别是在一些交互场景下对延迟要求近乎苛刻所以毫秒级的延迟是一个直播播放器聊下去的基础200-400ms的整体时延是大家比较期望的是的你没看错RTMP的也可以做到极低延迟。
软硬解码设置
Android平台特别是超过1080p的分辨率的流数据如果设备性能一般软解效率不一定高这时候建议硬解码硬解码我们分两种一种是设置surface模式的硬解码这种无法快照和回调yuv、rgb数据另一种就是常规硬解直接出原始解码后数据的区分两种硬解码模式我们设计了如下的接口 /*** 设置视频硬解码下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);
调用如下
if (isHardwareDecoder is_enable_hardware_render_mode) {libPlayer.SmartPlayerSetHWRenderMode(playerHandle, 1);
}
硬解码设置如下分别区分264、265硬解
if (isHardwareDecoder) {int isSupportHevcHwDecoder libPlayer.SetSmartPlayerVideoHevcHWDecoder(playerHandle, 1);int isSupportH264HwDecoder libPlayer.SetSmartPlayerVideoHWDecoder(playerHandle, 1);Log.i(TAG, isSupportH264HwDecoder: isSupportH264HwDecoder , isSupportHevcHwDecoder: isSupportHevcHwDecoder);
}
实时音量调节
实时音量调节主要用于实时静音和实时调节播放端的音量特别是多实例播放的时候非常有必要 /*** 设置播放音量** 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);RTSP模式设置
针对RTSP的TCP、UDP模式设置RTSP超时时间设置大家可能遇到过这样的场景有些RTSP服务或网络环境下只支持TCP或UDP这时候就需要有TCP/UDP模式设置还有自动切换TCP UDP模式选项如果连上RTSP服务但是收不到数据通过RTSP timeout时间来设置重连尝试间隔 /*** 设置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);
视频翻转、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);
设置视频画面填充模式 /*** 设置视频画面的填充模式如填充整个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);
设置实时回调下载速度间隔
通过设置下载速度回调间隔上层可以知道当前的网络状态也可根据回调的下载速度做逻辑上的调整。 /*** 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 );
实时快照
实时快照对RTSP或RTMP播放器来说非常有价值用户感兴趣的帧数据可以直接写png文件保存下来后续还可以针对保存下来的png数据做二次分析。 /*** 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);
调用如下
btnCaptureImage.setOnClickListener(new Button.OnClickListener() {SuppressLint(SimpleDateFormat)public void onClick(View v) {String timeStamp new SimpleDateFormat(yyyyMMdd_HHmmss).format(new Date());String imageFileName dn_ timeStamp; // 创建以时间命名的文件名称String imagePath imageSavePath / imageFileName .png;Log.i(TAG, imagePath: imagePath);libPlayer.SmartPlayerSaveCurImage(playerHandle, imagePath);}
});
实时录像
实时录像的重要性不言而喻我们好多开发者对实时录像有些误区录像不止是写文件就完事了还需要更精细化的粒度控制比如单个文件大小设置、只录视频或者音频其他音频格式转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);/*** 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);
总结
实际上除了上述提到的功能外RTMP或RTSP播放器还需要支持的客制化功能有支持回调H.264/H.265/AAC数据或回到解码后的YUV或RGB数据设置播放缓冲、设置快速播放、设置音频输出类型等一个通用的RTSP、RTMP播放器延迟是基础、功能完备是加分项性能优异稳定性好才敢推给客户让客户在现场放心使用无后顾之忧。