iOS使用ffmpeg播放rstp即時監控視頻資料流

來源:互聯網
上載者:User

標籤:style   blog   code   http   ext   color   

一、編譯針對iOS平台的ffmpeg庫(kxmovie)

近期有一個項目,需要播放各種格式的音頻、視頻以及網路攝影機即時監控的視頻流資料,經過多種折騰之後,最後選擇了kxmovie,kxmovie項目已經整合了ffmpeg和簡單的播放器,具體可以參考kxmovie首頁:https://github.com/kolyvan/kxmovie 編譯kxmovie很簡單,已經支援iOS 6.1 和 armv7s,一次成功,編譯過程沒出現什麼問題:
git clone git://github.com/kolyvan/kxmovie.git
cd kxmovie
git submodule update --init 
rake
二、使用kxmovie1.把kxmovie/output檔案夾下檔案添加到工程2.添加架構:MediaPlayer, CoreAudio, AudioToolbox, Accelerate, QuartzCore, OpenGLES and libz.dylib,libiconv.dylib3.添加lib庫:libkxmovie.a, libavcodec.a, libavformat.a, libavutil.a, libswscale.a, libswresample.a4.播放視頻:
ViewController *vc;    vc = [KxMovieViewController movieViewControllerWithContentPath:path parameters:nil];    [self presentViewController:vc animated:YES completion:nil]; 
5.具體使用參考demo工程:KxMovieExample三、碰到的問題播放本地視頻和網路視頻正常,播放網路攝影機即時監控視頻流(h264)的時候出現錯誤:

[rtsp @ 0x906cc00] UDP timeout, retrying with TCP

[rtsp @ 0x906cc00] Nonmatching transport in server reply

[rtsp @ 0x906cc00] Could not find codec parameters for stream 0 (Video: h264): unspecified size

Consider increasing the value for the ‘analyzeduration‘ and ‘probesize‘ options

Couldn‘t find stream information

跟蹤代碼,錯誤是在avformat_find_stream_info擷取流資訊失敗的時候的時候觸發。

 

if(avformat_find_stream_info(pFormatCtx,NULL) < 0) {

    av_log(NULL, AV_LOG_ERROR, "Couldn‘t find stream information\n");

    goto initError;

}

經過幾天的摸索,最終確定是網路的問題(在模擬器播放一直出錯,在3G網路下能播放),具體原因估計是rstp視頻流,程式預設採用udp傳輸或者組播,導致在私人網路視頻流不能正常傳輸。解決方案,把視頻流的傳輸模式強製成tcp傳輸:

……

// Open video file

pFormatCtx = avformat_alloc_context();  

//有三種傳輸方式:tcp udp_multicast udp,強制採用tcp傳輸

AVDictionary* options = NULL;

av_dict_set(&options, "rtsp_transport", "tcp", 0);

if(avformat_open_input(&pFormatCtx, [moviePathcStringUsingEncoding:NSASCIIStringEncoding],                              NULL,&options) != 0) {

    av_log(NULL, AV_LOG_ERROR, "Couldn‘t open file\n");

    goto initError;

}

// Retrieve stream information

if(avformat_find_stream_info(pFormatCtx,NULL) < 0) {

    av_log(NULL, AV_LOG_ERROR, "Couldn‘t find stream information\n");

    goto initError;

    }……
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.