Javacv: Thread block in FFmpegFrameGrabber.start() can block the FFmpegFrameRecorder.start()

Created on 2 Jun 2019  路  5Comments  路  Source: bytedeco/javacv

Here's my situation. I created some FFmpegFrameGrabbers in my project to grab multiple streams, and I process these streams into a single stream, then I record it.

But I noticed that only when all the grabbers start successfully can the recorder start. It means that if there is a grabber blocked in the start(), the recorder blocked in the start() too.

I wrote a test script to verify this, here the test code:

package org.seekloud.piscu
import org.bytedeco.javacv.{FFmpegFrameGrabber, FFmpegFrameRecorder}

object VerifyMultiWorker {

  def main(args: Array[String]): Unit = {
    val srcFile1 = "rtmp://localhost/oflaDemo/live_10000001_1"
    val srcFile2 = "rtmp://localhost/oflaDemo/live_10000001_2"
    val outTarget = "rtmp://localhost/oflaDemo/10000001"

    val grabber1 = new FFmpegFrameGrabber(srcFile1)
    val grabber2 = new FFmpegFrameGrabber(srcFile2)

    println(s"grabber1 before start")
    grabber1.start()
    println(s"grabber1 end start")

    val waiterThread = new Thread(() => {
      println(s"grabber2 begin start!!!!")
      grabber2.start()
      println(s"grabber2 end start!!!!")
    })

    waiterThread.start()

    val recorder =
      new FFmpegFrameRecorder(
        outTarget,
        800,
        600,
        2
      )

    recorder.setVideoOption("crf", "25")
    recorder.setVideoBitrate(2000000)
    recorder.setFormat("flv")

    println(s"Recorder begin start...")
    recorder.start()
    println(s"Recorder end start...")

    println("-----------ALL Done---------------")

  }

}

And this is the output in console:

grabber1 before start
grabber1 end start
grabber2 begin start!!!!
Recorder begin start...

You can see that the grabber2.start() blocked in the waiterThread(cause I didn't push stream to srcFile2) causing the recorder.start() blocked in the main thread.

If I push stream to the srcFile2, both of the blocking will end.

I wonder if there are some relationships between FFmpegFrameGrabber and FFmpegFrameRecorder ? I thought this two should work independently.

Btw, I use the 1.5 version javacpp and javacv.

question

All 5 comments

Call startUnsafe() instead of start().

Thanks, it works.

@saudet
Is there any risk using startUnsafe() instead of start() in this case?

It depends on the codecs you're using, see issue #1139.

Thank your.

Was this page helpful?
0 / 5 - 0 ratings