JavaCV FrameGrabber問題匯總

JavaCV FrameGrabber問題匯總

@Date 2018.09.27

FrameGrabber類
  • JavaCV中FrameGrabber類可以連接直播流地址, 進(jìn)行解碼, 獲取Frame幀信息, 常用方式如下
FrameGrabber grabber = new FrameGrabber("rtsp:/192.168.0.0");
while(true) {
    Frame frame = grabber.grabImage();
    // ...
}
問題(1)
  • 在如上代碼中, 若直播地址網(wǎng)絡(luò)不通, 或者連接超時, 則會出現(xiàn)程序Hang住的情況, 也就是同步阻塞卡死. 因?yàn)榭梢栽黾右粋€超時參數(shù)進(jìn)行解決.
解決辦法(1)
FrameGrabber grabber = new FrameGrabber("rtsp:/192.168.0.0");
// 增加超時參數(shù)
grabber.setOption(TimeoutOption.STIMEOUT.key(), "5000000");
while(true) {
    Frame frame = grabber.grabImage();
    // ...
}

問題(2)
  • FrameGrabber hangs when server connection is broken
  • 在問題(1)的基礎(chǔ)上, 若剛開始網(wǎng)絡(luò)是通的, 程序可以獲取到解碼后的視頻幀. 而在已經(jīng)運(yùn)行通了的時候, 網(wǎng)絡(luò)突然斷掉, 此時上面的timeout是不生效的.
  • 同時可以參考GitHub上的提問(https://github.com/bytedeco/javacv/issues/1063)
解決辦法(2)
public class CustomFrameGrabber extends FrameGrabber {
    /**
     * 讀流超時時間 : 10秒
     */
    private static final int FRAME_READ_TIMEOUT = 10000;

    // 增加callbacjk 監(jiān)聽
    private final AtomicLong lastFrameTime = new AtomicLong(System.currentTimeMillis());
    private final AVIOInterruptCB.Callback_Pointer cp = new avformat.AVIOInterruptCB.Callback_Pointer() {
        @Override
        public int call(Pointer pointer) {
            // 0:continue, 1:exit
            if (lastFrameTime.get() + FRAME_READ_TIMEOUT < System.currentTimeMillis()) {
                MONITOR_LOGGER.warn("frame read timeout 10s.");
                return 1;
            }
            return 0;
        }
    };
    
    // ... 省略中間代碼
    
    void startUnsafe() throws Exception {
        int ret;
        img_convert_ctx = null;
        oc = new AVFormatContext(null);
        video_c = null;
        audio_c = null;
        pkt = new AVPacket();
        pkt2 = new AVPacket();
        sizeof_pkt = pkt.sizeof();
        got_frame = new int[1];
        frameGrabbed = false;
        frame = new Frame();
        timestamp = 0;
        frameNumber = 0;

        pkt2.size(0);

        // Open video file
        AVInputFormat f = null;
        if (format != null && format.length() > 0) {
            if ((f = av_find_input_format(format)) == null) {
                throw new Exception("av_find_input_format() error: Could not find input format \"" + format + "\".");
            }
        }
        AVDictionary options = new AVDictionary(null);
        if (frameRate > 0) {
            AVRational r = av_d2q(frameRate, 1001000);
            av_dict_set(options, "framerate", r.num() + "/" + r.den(), 0);
        }
        if (pixelFormat >= 0) {
            av_dict_set(options, "pixel_format", av_get_pix_fmt_name(pixelFormat).getString(), 0);
        } else if (imageMode != ImageMode.RAW) {
            av_dict_set(options, "pixel_format", imageMode == ImageMode.COLOR ? "bgr24" : "gray8", 0);
        }
        if (imageWidth > 0 && imageHeight > 0) {
            av_dict_set(options, "video_size", imageWidth + "x" + imageHeight, 0);
        }
        if (sampleRate > 0) {
            av_dict_set(options, "sample_rate", "" + sampleRate, 0);
        }
        if (audioChannels > 0) {
            av_dict_set(options, "channels", "" + audioChannels, 0);
        }
        for (Entry<String, String> e : this.options.entrySet()) {
            av_dict_set(options, e.getKey(), e.getValue(), 0);
        }
        if (inputStream != null) {
            if (readCallback == null) {
                readCallback = new CustomFFmpegFrameGrabber.ReadCallback();
            }
            if (seekCallback == null) {
                seekCallback = new CustomFFmpegFrameGrabber.SeekCallback();
            }
            if (!inputStream.markSupported()) {
                inputStream = new BufferedInputStream(inputStream, 1024 * 1024);
            }
            inputStream.mark(1024 * 1024);
            oc = avformat_alloc_context();
            avio = avio_alloc_context(new BytePointer(av_malloc(4096)), 4096, 0, oc, readCallback, null, seekCallback);
            oc.pb(avio);

            filename = inputStream.toString();
            inputStreams.put(oc, inputStream);
        }
        // 2018.09.07 add 此處一行為新增代碼, 意思是每次取到幀時, 都更新一下時間. 若遇到網(wǎng)絡(luò)斷開, hang住時, 則可用此時間判斷超時
        lastFrameTime.set(System.currentTimeMillis());
        oc = avformat_alloc_context();
        avformat.AVIOInterruptCB cb = new avformat.AVIOInterruptCB();
        cb.callback(cp);
        oc.interrupt_callback(cb);
        // 省略后續(xù)代碼...
    }
    
    public Frame grabFrame(boolean doAudio, boolean doVideo, boolean processImage, boolean keyFrames) throws Exception {
        if (oc == null || oc.isNull()) {
            throw new Exception("Could not grab: No AVFormatContext. (Has start() been called?)");
        } else if ((!doVideo || video_st == null) && (!doAudio || audio_st == null)) {
            return null;
        }
        // 2018.09.07 add 獲取幀時, 更新最新時間戳
        lastFrameTime.set(System.currentTimeMillis());
        
        // 省略后續(xù)代碼...
    }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末痊银,一起剝皮案震驚了整個濱河市澳迫,隨后出現(xiàn)的幾起案子杠河,更是在濱河造成了極大的恐慌,老刑警劉巖乏屯,帶你破解...
    沈念sama閱讀 218,546評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異压彭,居然都是意外死亡运翼,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,224評論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來席揽,“玉大人顽馋,你說我怎么就攤上這事』闲撸” “怎么了寸谜?”我有些...
    開封第一講書人閱讀 164,911評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長属桦。 經(jīng)常有香客問我程帕,道長,這世上最難降的妖魔是什么地啰? 我笑而不...
    開封第一講書人閱讀 58,737評論 1 294
  • 正文 為了忘掉前任愁拭,我火速辦了婚禮,結(jié)果婚禮上亏吝,老公的妹妹穿的比我還像新娘岭埠。我一直安慰自己,他們只是感情好蔚鸥,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,753評論 6 392
  • 文/花漫 我一把揭開白布惜论。 她就那樣靜靜地躺著,像睡著了一般止喷。 火紅的嫁衣襯著肌膚如雪馆类。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,598評論 1 305
  • 那天弹谁,我揣著相機(jī)與錄音乾巧,去河邊找鬼。 笑死预愤,一個胖子當(dāng)著我的面吹牛沟于,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播植康,決...
    沈念sama閱讀 40,338評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼旷太,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了销睁?” 一聲冷哼從身側(cè)響起供璧,我...
    開封第一講書人閱讀 39,249評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎冻记,沒想到半個月后睡毒,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,696評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡檩赢,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,888評論 3 336
  • 正文 我和宋清朗相戀三年吕嘀,在試婚紗的時候發(fā)現(xiàn)自己被綠了违寞。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,013評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡偶房,死狀恐怖趁曼,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情棕洋,我是刑警寧澤挡闰,帶...
    沈念sama閱讀 35,731評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站掰盘,受9級特大地震影響摄悯,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜愧捕,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,348評論 3 330
  • 文/蒙蒙 一奢驯、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧次绘,春花似錦瘪阁、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,929評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至禾进,卻和暖如春豁跑,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背泻云。 一陣腳步聲響...
    開封第一講書人閱讀 33,048評論 1 270
  • 我被黑心中介騙來泰國打工艇拍, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人壶愤。 一個月前我還...
    沈念sama閱讀 48,203評論 3 370
  • 正文 我出身青樓淑倾,卻偏偏與公主長得像馏鹤,于是被迫代替她去往敵國和親征椒。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,960評論 2 355

推薦閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,151評論 25 707
  • 我們的封面湃累,和我們課程價格一樣樸素而專業(yè) 查戶口了勃救,趕緊填上你的個人資料。為什么沒微信呢??可能秘密太對吧 這是一...
    Joey_宇閱讀 521評論 1 0
  • 有董永宏者治力,地理蒙秒,文學(xué),歷史宵统,武術(shù)晕讲,樣樣精通;有馬鈴者,通曉歷史與文學(xué)瓢省,又善講課弄息,為人甚好,眾人皆愛之…且慢勤婚,此等...
    喝蕎麥茶的幽靈閱讀 1,548評論 10 10
  • 成年人的世界從來不是那么容易摹量,生活對于任何人都是——熬不住就出局,熬得住就出眾馒胆。 也許每個人缨称,在遇到挫折時都會有莫...
    江宏祥閱讀 222評論 1 6