iOS使用ffmpeg播放rstp實時監控視頻數據流

一、編譯針對iOS平臺的ffmpeg庫(kxmovie)
近期有一個項目,需要播放各種格式的音頻、視頻以及網絡攝像頭實時監控的視頻流數據,經過多種折騰之後,最後選擇了kxmovie,kxmovie項目已經整合了ffmpeg和簡單的播放器,具體可以參考kxmovie主頁:https://github.com/kolyvan/kxmovie 
編譯kxmovie很簡單,已經支持iOS6.1 和 armv7s,一次成功,編譯過程沒出現什麼問題:
git clone git://github.com/kolyvan/kxmovie.git
cd kxmovie
git submodule update --init 
rake
二、使用kxmovie
1.kxmovie/output文件夾下文件添加到工程
2.添加框架:MediaPlayer,CoreAudio, AudioToolbox, Accelerate, QuartzCore, OpenGLES andlibz.dylib,libiconv.dylib
3.添加lib庫:libkxmovie.a,libavcodec.a, libavformat.a, libavutil.a, libswscale.a,libswresample.a
4.播放視頻:
ViewController *vc;    vc = [KxMovieViewController movieViewControllerWithContentPath:path parameters:nil];    [self presentViewController:vc animated:YES completion:nil]; 
5.具體使用參考demo工程:KxMovieExample
三、碰到的問題
播放本地視頻和網絡視頻正常,播放網絡攝像頭實時監控視頻流(h264)的時候出現錯誤:

[rtsp @ 0x906cc00] UDP timeout, retrying withTCP

[rtsp @ 0x906cc00] Nonmatching transport in serverreply

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

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

Couldn't find streaminformation

跟蹤代碼,錯誤是在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網絡下能播放iOS使用ffmpeg播放rstp實時監控視頻數據流),具體原因估計是rstp視頻流,程序默認採用udp傳輸或者組播,導致在私有網絡視頻流不能正常傳輸。
解決方法,把視頻流的傳輸模式強制成tcp傳輸:

……

// Open video file

pFormatCtx = avformat_alloc_context();  

//有三種傳輸方式:tcp udp_multicastudp,強制採用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;

   }
……

問題解決。iOS使用ffmpeg播放rstp實時監控視頻數據流

轉載地址:http://blog.sina.com.cn/s/blog_4462d1710101k4ld.html
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章