從零開發一款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}}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章