从零开发一款Android Rtmp播放器

{"type":"doc","content":[{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"1. 背景介绍","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"15年移动端直播应用火起来的时候,主要的直播协议是RTMP,多媒体服务以Adobe的AMS、wowza、Red5、crtmpserver、nginx rtmp module等,后面过长RTMP服务SRS开始流行。Android端播放器主要以开始以EXOPlayer播放HLS,但是HLS有延迟高的确定,随后大家主要使用开源的ijkplyer,ijkplayer通过ffmpeg进行拉流及解码,支持多种音视频编码,还有跨平台,API与系统播放器保持一致等特征,后续各大厂提供的直播SDK均有ijkplayer身影。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"当时在做一款游戏SDK,SDK主要提供了游戏画面声音采集、音视频编解码、直播推流、直播拉流播放等,SDK为游戏提供直播功能,播放也是采用了现成的ijkplayer播放器。但是SDK推广的时候遇到了问题,游戏厂家嫌弃SDK体积大(其实总共也就3Mb左右),我们需要一款体积小,性能高的播放器,由于开发成本的原因一直没有时间做,后面换工作期间,花了一个月时间把这款播放器开发出来,并开源了出来。","attrs":{}},{"type":"link","attrs":{"href":"https://github.com/qingkouwei/oarplayer","title":"","type":null},"content":[{"type":"text","text":"oarplayer","attrs":{}}]},{"type":"text","text":" 是基于MediaCodec与srs-librtmp,完全不依赖ffmpeg,纯C语言实现的播放器。本文主要介绍这款播放器的实现思路。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"2. 整体架构设计","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"播放器整体播放流程如下:","attrs":{}}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/63/635e0898c17e621bd85510f97fbc0a22.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"通过srs-librtmp拉取直播流,通过package type分离音视频流,将package数据缓存到package队列,解码线程不断从package队列读取package交由解码器解码,解码器将解码后的frame存储到frame队列,opensles播放线程与opengles渲染线程从frame队列读取frame播放与渲染,这里还涉及到音视频同步。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"播放器主要涉及了以下线程:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"numberedlist","attrs":{"start":1,"normalizeStart":1},"content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":1,"align":null,"origin":null},"content":[{"type":"text","text":"rtmp拉流线程;","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":2,"align":null,"origin":null},"content":[{"type":"text","text":"音频解码线程;","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":3,"align":null,"origin":null},"content":[{"type":"text","text":"视频解码线程;","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":4,"align":null,"origin":null},"content":[{"type":"text","text":"音频播放线程;","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":5,"align":null,"origin":null},"content":[{"type":"text","text":"视频渲染线程;","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":6,"align":null,"origin":null},"content":[{"type":"text","text":"JNI回调线程。","attrs":{}}]}]}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"3. API接口设计","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"通过以下几步即可完成rtmp播放:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"numberedlist","attrs":{"start":1,"normalizeStart":1},"content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":1,"align":null,"origin":null},"content":[{"type":"text","text":"实例化OARPlayer:","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"OARPlayer player = new OARPlayer();","attrs":{}}],"attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":2,"align":null,"origin":null},"content":[{"type":"text","text":"设置视频源:","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"player.setDataSource(rtmp_url);","attrs":{}}],"attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":3,"align":null,"origin":null},"content":[{"type":"text","text":"设置surface:","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"player.setSurface(surfaceView.getHolder());","attrs":{}}],"attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":4,"align":null,"origin":null},"content":[{"type":"text","text":"开始播放:","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"player.start();","attrs":{}}],"attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":5,"align":null,"origin":null},"content":[{"type":"text","text":"停止播放:","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"player.stop();","attrs":{}}],"attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":6,"align":null,"origin":null},"content":[{"type":"text","text":"释放资源:","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"player.release();","attrs":{}}],"attrs":{}}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Java层方法封装了JNI层方法,JNI层封装调用了对应的具体功能。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"4. rtmp拉流线程","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"oarplayer使用的是srs-librtmp,srs-librtmp是从SRS服务器导出的一个客户端库,作者提供srs-librtmp初衷是:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"numberedlist","attrs":{"start":1,"normalizeStart":1},"content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":1,"align":null,"origin":null},"content":[{"type":"text","text":"觉得rtmpdump/librtmp的代码太难读了,而SRS的代码可读性很好;","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":2,"align":null,"origin":null},"content":[{"type":"text","text":"压测工具","attrs":{}},{"type":"link","attrs":{"href":"https://github.com/ossrs/srs-bench/blob/srs-librtmp/src/app/srs_librtmp.h","title":"","type":null},"content":[{"type":"text","text":"srs-bench","attrs":{}}]},{"type":"text","text":"是个客户端,需要一个客户端库;","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":3,"align":null,"origin":null},"content":[{"type":"text","text":"觉得服务器能搞好,客户端也不在话下","attrs":{}}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"目前srs-librtmp作者已经停止维护,主要原因如作者所说:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"决定开源项目正义的绝对不是技术多好,而是能跑多久。技术很牛,性能很强,代码风格很好,固然是个好事,但是这些都顶不上一个“不维护”的大罪过,代码放出来不维护,怎么跟进业内技术的不断发展呢。而决定能跑多久的,首先是技术热情,然后是维护者的领域背景。SRS的维护者都是服务器背景,大家的工作都是在服务器,客户端经验太少了,无法长久维护客户端的库。因此,SRS决定果断放弃srs-librtmp,不再维护客户端库,聚焦于服务器的快速迭代。客户端并非不重要,而是要交给专业的客户端的开源项目和朋友维护,比如FFmpeg也自己实现了librtmp。","attrs":{}}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"oarplayer当初使用srs-librtmp是基于srs-librtmp代码的可读性考虑。oarplayer有相当高的模块化特性,可以很方便的替换各个rtmp lib实现。这里介绍srs-librtmp接口:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"numberedlist","attrs":{"start":1,"normalizeStart":1},"content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":1,"align":null,"origin":null},"content":[{"type":"text","text":"创建srs_rtmp_t对象:","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"srs_rtmp_create(url)","attrs":{}}],"attrs":{}},{"type":"text","text":";","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":2,"align":null,"origin":null},"content":[{"type":"text","text":"设置读写超时时间:","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"srs_rtmp_set_timeout","attrs":{}}],"attrs":{}},{"type":"text","text":";","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":3,"align":null,"origin":null},"content":[{"type":"text","text":"开始握手:","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"srs_rtmp_handshake","attrs":{}}],"attrs":{}},{"type":"text","text":";","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":4,"align":null,"origin":null},"content":[{"type":"text","text":"开始连接:","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"srs_rtmp_connect_app","attrs":{}}],"attrs":{}},{"type":"text","text":";","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":5,"align":null,"origin":null},"content":[{"type":"text","text":"设置播放模式:","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"srs_rtmp_play_stream","attrs":{}}],"attrs":{}},{"type":"text","text":";","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":6,"align":null,"origin":null},"content":[{"type":"text","text":"循环读取音视频包:","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"srs_rtmp_read_packet(rtmp, &type, &timestamp, &data, &size)","attrs":{}}],"attrs":{}},{"type":"text","text":";","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":7,"align":null,"origin":null},"content":[{"type":"text","text":"解析音频包:","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":1,"align":null,"origin":null},"content":[{"type":"text","text":"获取编码类型:","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"srs_utils_flv_audio_sound_format","attrs":{}}],"attrs":{}},{"type":"text","text":";","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":2,"align":null,"origin":null},"content":[{"type":"text","text":"获取音频采样率:","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"srs_utils_flv_audio_sound_rate","attrs":{}}],"attrs":{}},{"type":"text","text":";","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":3,"align":null,"origin":null},"content":[{"type":"text","text":"获取采样位深:","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"srs_utils_flv_audio_sound_size","attrs":{}}],"attrs":{}},{"type":"text","text":";","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":4,"align":null,"origin":null},"content":[{"type":"text","text":"获取声道数:","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"srs_utils_flv_audio_sound_type","attrs":{}}],"attrs":{}},{"type":"text","text":";","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":5,"align":null,"origin":null},"content":[{"type":"text","text":"获取音频包类型:","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"srs_utils_flv_audio_aac_packet_type","attrs":{}}],"attrs":{}},{"type":"text","text":";","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":8,"align":null,"origin":null},"content":[{"type":"text","text":"解析视频包:","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":1,"align":null,"origin":null},"content":[{"type":"text","text":"获取编码类型:","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"srs_utils_flv_video_codec_id","attrs":{}}],"attrs":{}},{"type":"text","text":";","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":2,"align":null,"origin":null},"content":[{"type":"text","text":"是否关键帧:","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"srs_utils_flv_video_frame_type","attrs":{}}],"attrs":{}},{"type":"text","text":";","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":3,"align":null,"origin":null},"content":[{"type":"text","text":"获取视频包类型:","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"srs_utils_flv_video_avc_packet_type","attrs":{}}],"attrs":{}},{"type":"text","text":";","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":9,"align":null,"origin":null},"content":[{"type":"text","text":"解析metadata类型;","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":10,"align":null,"origin":null},"content":[{"type":"text","text":"销毁srs_rtmp_t对象:","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"srs_rtmp_destroy","attrs":{}}],"attrs":{}},{"type":"text","text":";","attrs":{}}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"这里有个小技巧,我们在拉流线程中,循环调用","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"srs_rtmp_read_packet","attrs":{}}],"attrs":{}},{"type":"text","text":"方法,可以通过","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"srs_rtmp_set_timeout","attrs":{}}],"attrs":{}},{"type":"text","text":"设置超时时间,但是如果超时时间设置的太短,会导致频繁的唤起线程,如果设置超时时间太长,我们在停止时,必须等待超时结束才会能真正结束。这里我们可以使用poll模型,将rtmp的tcp socket放入poll中,再放入一个管道fd,在需要停止时向管道写入一个指令,唤醒poll,直接停止rtmp拉流线程。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"5. 主要数据结构","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"5.1 package结构:","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"typedef struct OARPacket {\n int size;//包大小\n PktType_e type;//包类型\n int64_t dts;//解码时间戳\n int64_t pts;//显示时间戳\n int isKeyframe;//是否关键帧\n struct OARPacket *next;//下一个包地址\n uint8_t data[0];//包数据内容\n}OARPacket;\n","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"5.2 package队列:","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"typedef struct oar_packet_queue {\n PktType_e media_type;//类型\n pthread_mutex_t *mutex;//线程锁\n pthread_cond_t *cond;//条件变量\n OARPacket *cachedPackets;//队列首地址\n OARPacket *lastPacket;//队列最后一个元素\n\n int count;//数量\n int total_bytes;//总字节数\n uint64_t max_duration;//最大时长\n\n void (*full_cb)(void *);//队列满回调\n\n void (*empty_cb)(void *);//队列为空回调\n\n void *cb_data;\n} oar_packet_queue;\n","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"5.3 Frame类型","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"typedef struct OARFrame {\n int size;//帧大小\n PktType_e type;//帧类型\n int64_t dts;//解码时间戳\n int64_t pts;//显示时间戳\n int format;//格式(用于视频)\n int width;//宽(用于视频)\n int height;//高(用于视频)\n int64_t pkt_pos;\n int sample_rate;//采样率(用于音频)\n struct OARFrame *next;\n uint8_t data[0];\n}OARFrame;\n","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"5.4 Frame队列","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"typedef struct oar_frame_queue {\n pthread_mutex_t *mutex;\n pthread_cond_t *cond;\n OARFrame *cachedFrames;\n OARFrame *lastFrame;\n int count;//帧数量\n unsigned int size;\n} oar_frame_queue;\n","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"6. 解码线程","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我们的rtmp流拉取、解码、渲染、音频输出都在C层实现。在C层,Android 21之后系统提供了AMediaCodec接口,我们直接","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"find_library(media-ndk mediandk)","attrs":{}}],"attrs":{}},{"type":"text","text":",并引入","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"","attrs":{}}],"attrs":{}},{"type":"text","text":"头文件即可。对于Android 21之前版本,可以在C层调用Java层的MediaCodec。下面分别介绍两种实现:","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"6.1 Java层代理解码","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Java层MediaCodec解码使用步骤:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"numberedlist","attrs":{"start":1,"normalizeStart":1},"content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":1,"align":null,"origin":null},"content":[{"type":"text","text":"创建解码器:","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"codec = MediaCodec.createDecoderByType(codecName);","attrs":{}}],"attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":2,"align":null,"origin":null},"content":[{"type":"text","text":"配置解码器格式:","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"codec.configure(format, null, null, 0);","attrs":{}}],"attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":3,"align":null,"origin":null},"content":[{"type":"text","text":"启动解码器:","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"codec.start()","attrs":{}}],"attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":4,"align":null,"origin":null},"content":[{"type":"text","text":"获取解码输入缓存ID:","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"dequeueInputBuffer","attrs":{}}],"attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":5,"align":null,"origin":null},"content":[{"type":"text","text":"获取解码输入缓存:","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"getInputBuffer","attrs":{}}],"attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":6,"align":null,"origin":null},"content":[{"type":"text","text":"获取解码输出缓存:","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"dequeueOutputBufferIndex","attrs":{}}],"attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":7,"align":null,"origin":null},"content":[{"type":"text","text":"释放输出缓存:","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"releaseOutPutBuffer","attrs":{}}],"attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":8,"align":null,"origin":null},"content":[{"type":"text","text":"停止解码器:","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"codec.stop();","attrs":{}}],"attrs":{}}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Jni层封装对应的调用接口即可。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"6.2 C层解码器使用","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"C层接口介绍:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"numberedlist","attrs":{"start":1,"normalizeStart":1},"content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":1,"align":null,"origin":null},"content":[{"type":"text","text":"创建Format:","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"AMediaFormat_new","attrs":{}}],"attrs":{}},{"type":"text","text":";","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":2,"align":null,"origin":null},"content":[{"type":"text","text":"创建解码器:","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"AMediaCodec_createDecoderByType","attrs":{}}],"attrs":{}},{"type":"text","text":";","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":3,"align":null,"origin":null},"content":[{"type":"text","text":"配置解码参数:","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"AMediaCodec_configure","attrs":{}}],"attrs":{}},{"type":"text","text":";","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":4,"align":null,"origin":null},"content":[{"type":"text","text":"启动解码器:","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"AMediaCodec_start","attrs":{}}],"attrs":{}},{"type":"text","text":";","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":5,"align":null,"origin":null},"content":[{"type":"text","text":"输入音视频包:","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":1,"align":null,"origin":null},"content":[{"type":"text","text":"获取输入buffer序列:","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"AMediaCodec_dequeueInputBuffer","attrs":{}}],"attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":2,"align":null,"origin":null},"content":[{"type":"text","text":"获取输入buffer:","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"AMediaCodec_getInputBuffer","attrs":{}}],"attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":3,"align":null,"origin":null},"content":[{"type":"text","text":"拷贝数据:","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"memcpy","attrs":{}}],"attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":4,"align":null,"origin":null},"content":[{"type":"text","text":"输入buffer放入解码器:","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"AMediaCodec_queueInputBuffer","attrs":{}}],"attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":6,"align":null,"origin":null},"content":[{"type":"text","text":"获取解码后帧:","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":1,"align":null,"origin":null},"content":[{"type":"text","text":"获取输出buffer序列:","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"AMediaCodec_dequeueOutputBuffer","attrs":{}}],"attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":2,"align":null,"origin":null},"content":[{"type":"text","text":"获取输出buffer:","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"AMediaCodec_getOutputBuffer","attrs":{}}],"attrs":{}}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我们发现不管是Java层还是C层的接口都是提供了类似的思路,其实他们最终调用的还是系统的解码框架。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"这里我们可以根据系统版本来觉得使用Java层接口和C层接口,我们的oarplayer,主要的代码都是在C层实现,所以我们也有限使用C层接口。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"7. 音频输出线程","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"音频输出我们使用opensl实现,之前文章介绍过Android音频架构,其实也可以使用AAudio或者Oboe。这里再简单介绍下opensl es的使用。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"numberedlist","attrs":{"start":1,"normalizeStart":1},"content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":1,"align":null,"origin":null},"content":[{"type":"text","text":"创建引擎:","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"slCreateEngine(&engineObject, 0, NULL, 0, NULL, NULL);","attrs":{}}],"attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":2,"align":null,"origin":null},"content":[{"type":"text","text":"实现引擎:","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"(*engineObject)->Realize(engineObject, SL_BOOLEAN_FALSE);","attrs":{}}],"attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":3,"align":null,"origin":null},"content":[{"type":"text","text":"获取接口:","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"(*engineObject)->GetInterface(engineObject, SL_IID_ENGINE, &engineEngine);","attrs":{}}],"attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":4,"align":null,"origin":null},"content":[{"type":"text","text":"创建输出混流器:","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"(*engineEngine)->CreateOutputMix(engineEngine, &outputMixObject, 0, NULL, NULL);","attrs":{}}],"attrs":{}},{"type":"text","text":";","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":5,"align":null,"origin":null},"content":[{"type":"text","text":"实现混流器:","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"(*outputMixObject)->Realize(outputMixObject, SL_BOOLEAN_FALSE);","attrs":{}}],"attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":6,"align":null,"origin":null},"content":[{"type":"text","text":"配置音频源:","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"SLDataLocator_AndroidSimpleBufferQueue loc_bufq = {SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE, 2};","attrs":{}}],"attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":7,"align":null,"origin":null},"content":[{"type":"text","text":"配置Format:","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"SLDataFormat_PCM format_pcm = {SL_DATAFORMAT_PCM, channel, SL_SAMPLINGRATE_44_1,SL_PCMSAMPLEFORMAT_FIXED_16, SL_PCMSAMPLEFORMAT_FIXED_16,SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT, SL_BYTEORDER_LITTLEENDIAN};","attrs":{}}],"attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":8,"align":null,"origin":null},"content":[{"type":"text","text":"创建播放器:","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"(*engineEngine)->CreateAudioPlayer(engineEngine,&bqPlayerObject, &audioSrc, &audioSnk,2, ids, req);","attrs":{}}],"attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":9,"align":null,"origin":null},"content":[{"type":"text","text":"实现播放器:","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"(*bqPlayerObject)->Realize(bqPlayerObject, SL_BOOLEAN_FALSE);","attrs":{}}],"attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":10,"align":null,"origin":null},"content":[{"type":"text","text":"获取播放接口:","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"(*bqPlayerObject)->GetInterface(bqPlayerObject, SL_IID_PLAY, &bqPlayerPlay);","attrs":{}}],"attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":11,"align":null,"origin":null},"content":[{"type":"text","text":"获取缓冲区接口:","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"(*bqPlayerObject)->GetInterface(bqPlayerObject, SL_IID_ANDROIDSIMPLEBUFFERQUEUE,&bqPlayerBufferQueue);","attrs":{}}],"attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":12,"align":null,"origin":null},"content":[{"type":"text","text":"注册缓存回调:","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"(*bqPlayerBufferQueue)->RegisterCallback(bqPlayerBufferQueue, bqPlayerCallback, oar);","attrs":{}}],"attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":13,"align":null,"origin":null},"content":[{"type":"text","text":"获取音量调节器:","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"(*bqPlayerObject)->GetInterface(bqPlayerObject, SL_IID_VOLUME, &bqPlayerVolume);","attrs":{}}],"attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":14,"align":null,"origin":null},"content":[{"type":"text","text":"缓存回调中不断的从音频帧队列读取数据,并写入缓存队列:","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"(*bqPlayerBufferQueue)->Enqueue(bqPlayerBufferQueue, ctx->buffer,(SLuint32)ctx->frame_size);","attrs":{}}],"attrs":{}}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"上面就是音频播放的opensl es接口使用介绍。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"8. 渲染线程","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"相比较于音频播放,视频渲染可能更复杂一些,除了opengl引擎创建,opengl线程创建,oarplayer使用的是基于音频的同步方式,所以在视频渲染时还需要考虑音视频同步问题。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"8.1 OpenGL引擎创建","attrs":{}}]},{"type":"numberedlist","attrs":{"start":1,"normalizeStart":1},"content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":1,"align":null,"origin":null},"content":[{"type":"text","text":"生成buffer:","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"glGenBuffers","attrs":{}}],"attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":2,"align":null,"origin":null},"content":[{"type":"text","text":"绑定buffer:","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"glBindBuffer(GL_ARRAY_BUFFER, model->vbos[0])","attrs":{}}],"attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":3,"align":null,"origin":null},"content":[{"type":"text","text":"设置清屏色:","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"glClearColor","attrs":{}}],"attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":4,"align":null,"origin":null},"content":[{"type":"text","text":"创建纹理对象:","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"texture2D","attrs":{}}],"attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":5,"align":null,"origin":null},"content":[{"type":"text","text":"创建着色器对象:","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"glCreateShader","attrs":{}}],"attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":6,"align":null,"origin":null},"content":[{"type":"text","text":"设置着色器源码:","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"glShaderSource","attrs":{}}],"attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":7,"align":null,"origin":null},"content":[{"type":"text","text":"编译着色器源码:","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"glCompileShader","attrs":{}}],"attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":8,"align":null,"origin":null},"content":[{"type":"text","text":"附着着色器:","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"glAttachShader","attrs":{}}],"attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":9,"align":null,"origin":null},"content":[{"type":"text","text":"连接着色器:","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"glLinkProgram","attrs":{}}],"attrs":{}}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"opengl与硬件交互还需要EGL环境,下面展示EGL初始化流程代码:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"static void init_egl(oarplayer * oar){\n oar_video_render_context *ctx = oar->video_render_ctx;\n const EGLint attribs[] = {EGL_SURFACE_TYPE, EGL_WINDOW_BIT, EGL_RENDERABLE_TYPE,\n EGL_OPENGL_ES2_BIT, EGL_BLUE_SIZE, 8, EGL_GREEN_SIZE, 8, EGL_RED_SIZE,\n 8, EGL_ALPHA_SIZE, 8, EGL_DEPTH_SIZE, 0, EGL_STENCIL_SIZE, 0,\n EGL_NONE};\n EGLint numConfigs;\n ctx->display = eglGetDisplay(EGL_DEFAULT_DISPLAY);\n EGLint majorVersion, minorVersion;\n eglInitialize(ctx->display, &majorVersion, &minorVersion);\n eglChooseConfig(ctx->display, attribs, &ctx->config, 1, &numConfigs);\n ctx->surface = eglCreateWindowSurface(ctx->display, ctx->config, ctx->window, NULL);\n EGLint attrs[] = {EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE};\n ctx->context = eglCreateContext(ctx->display, ctx->config, NULL, attrs);\n EGLint err = eglGetError();\n if (err != EGL_SUCCESS) {\n LOGE(\"egl error\");\n }\n if (eglMakeCurrent(ctx->display, ctx->surface, ctx->surface, ctx->context) == EGL_FALSE) {\n LOGE(\"------EGL-FALSE\");\n }\n eglQuerySurface(ctx->display, ctx->surface, EGL_WIDTH, &ctx->width);\n eglQuerySurface(ctx->display, ctx->surface, EGL_HEIGHT, &ctx->height);\n initTexture(oar);\n\n oar_java_class * jc = oar->jc;\n JNIEnv * jniEnv = oar->video_render_ctx->jniEnv;\n jobject surface_texture = (*jniEnv)->CallStaticObjectMethod(jniEnv, jc->SurfaceTextureBridge, jc->texture_getSurface, ctx->texture[3]);\n ctx->texture_window = ANativeWindow_fromSurface(jniEnv, surface_texture);\n\n}\n","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"8.2 音视频同步","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"常见的音视频同步有三种:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"numberedlist","attrs":{"start":1,"normalizeStart":1},"content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":1,"align":null,"origin":null},"content":[{"type":"text","text":"基于视频同步;","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":2,"align":null,"origin":null},"content":[{"type":"text","text":"基于音频同步;","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":3,"align":null,"origin":null},"content":[{"type":"text","text":"基于第三方时间戳同步。","attrs":{}}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"这里我们使用基于音频帧同步的方法,渲染画面时,判断音频时间戳diff与视频画面渲染周期,如果大于周期,则等待,如果大于0小于周期,如果小于0则立马绘制。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"下面展示渲染代码:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"/**\n *\n * @param oar\n * @param frame\n * @return 0 draw\n * -1 sleep 33ms continue\n * -2 break\n */\nstatic inline int draw_video_frame(oarplayer *oar) {\n // 上一次可能没有画, 这种情况就不需要取新的了\n if (oar->video_frame == NULL) {\n oar->video_frame = oar_frame_queue_get(oar->video_frame_queue);\n }\n // buffer empty ==> sleep 10ms , return 0\n // eos ==> return -2\n if (oar->video_frame == NULL) {\n _LOGD(\"video_frame is null...\");\n usleep(BUFFER_EMPTY_SLEEP_US);\n return 0;\n\n }\n int64_t time_stamp = oar->video_frame->pts;\n\n\n int64_t diff = 0;\n if(oar->metadata->has_audio){\n diff = time_stamp - (oar->audio_clock->pts + oar->audio_player_ctx->get_delta_time(oar->audio_player_ctx));\n }else{\n diff = time_stamp - oar_clock_get(oar->video_clock);\n }\n _LOGD(\"time_stamp:%lld, clock:%lld, diff:%lld\",time_stamp , oar_clock_get(oar->video_clock), diff);\n oar_model *model = oar->video_render_ctx->model;\n\n\n // diff >= 33ms if draw_mode == wait_frame return -1\n // if draw_mode == fixed_frequency draw previous frame ,return 0\n // diff > 0 && diff < 33ms sleep(diff) draw return 0\n // diff <= 0 draw return 0\n if (diff >= WAIT_FRAME_SLEEP_US) {\n if (oar->video_render_ctx->draw_mode == wait_frame) {\n return -1;\n } else {\n draw_now(oar->video_render_ctx);\n return 0;\n }\n } else {\n // if diff > WAIT_FRAME_SLEEP_US then use previous frame\n // else use current frame and release frame\n// LOGI(\"start draw...\");\n pthread_mutex_lock(oar->video_render_ctx->lock);\n model->update_frame(model, oar->video_frame);\n pthread_mutex_unlock(oar->video_render_ctx->lock);\n oar_player_release_video_frame(oar, oar->video_frame);\n\n JNIEnv * jniEnv = oar->video_render_ctx->jniEnv;\n (*jniEnv)->CallStaticVoidMethod(jniEnv, oar->jc->SurfaceTextureBridge, oar->jc->texture_updateTexImage);\n jfloatArray texture_matrix_array = (*jniEnv)->CallStaticObjectMethod(jniEnv, oar->jc->SurfaceTextureBridge, oar->jc->texture_getTransformMatrix);\n (*jniEnv)->GetFloatArrayRegion(jniEnv, texture_matrix_array, 0, 16, model->texture_matrix);\n (*jniEnv)->DeleteLocalRef(jniEnv, texture_matrix_array);\n\n if (diff > 0) usleep((useconds_t) diff);\n draw_now(oar->video_render_ctx);\n oar_clock_set(oar->video_clock, time_stamp);\n return 0;\n }\n}\n","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"9. 总结","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"本文基于Android端的RTMP播放器实现过程,介绍了RTMP推拉流库、Android MediaCodec Java层与C层接口、OpenSL ES接口、OpenGL ES接口、EGL接口、以及音视频相关知识。具体播放器代码可直接在官方地址查看:","attrs":{}},{"type":"link","attrs":{"href":"https://github.com/qingkouwei/oarplayer","title":"","type":null},"content":[{"type":"text","text":"oarplayer","attrs":{}}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/d5/d5f7a2a4122a611baad795eff149f9ce.jpeg","alt":null,"title":"","style":[{"key":"width","value":"100%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章