Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FFmpegFrameGrabber not starting in another thread #1338

Closed
caaiofarias opened this issue Nov 21, 2019 · 24 comments
Closed

FFmpegFrameGrabber not starting in another thread #1338

caaiofarias opened this issue Nov 21, 2019 · 24 comments

Comments

@caaiofarias
Copy link

Hello guys!

i want to grab frames to a streaming video in another Thread. But when i started the thread the FFmpegFrameGrabber doens't start. Any solution ? The streaming can't pause my main thread.

`public class Producer implements Runnable {
private CircularQueueImplementation buffer = new CircularQueueImplementation<>(100);
protected static Logger logger = Logger.getLogger("aa.central");
int maxSize;
String SOURCE_RTSP = "streaming_link_camera";

public Producer(CircularQueueImplementation<Frame> buffer, int size) {
	this.buffer = buffer;
	this.maxSize = size;
}

@Override
public void run() {
	
	try{
		logger.debug("this logger show");
		avutil.av_log_set_level(avutil.AV_LOG_QUIET);
		FFmpegFrameGrabber grabber = new FFmpegFrameGrabber(SOURCE_RTSP);
		logger.debug("this doesn't");
		//grabber.setImageHeight(480);
		grabber.setFrameRate(15);
		//grabber.setImageWidth(640);
		
		try{
		grabber.start();
		}
		catch(Exception e){
			e.printStackTrace();
		}
		while(true){
			synchronized (this) {
				Frame frame = grabber.grab().clone();
				long timestamp = frame.timestamp;
				//canvas.showImage(frame);
				if(frame == null) continue;
				
				
				buffer.enqueue(frame);
				System.out.println(buffer.getCurrentQueueSize());
				
				}
			}
		}
	catch (Exception e){
		e.printStackTrace();
		}
}`
@caaiofarias caaiofarias changed the title FFmpegFrameGrabber not starting in other thread FFmpegFrameGrabber not starting in another thread Nov 21, 2019
@saudet
Copy link
Member

saudet commented Nov 21, 2019 via email

@caaiofarias
Copy link
Author

It's working now, but the grabber its taking 6 minutes to start grabbing frames... Any Idea?

@saudet
Copy link
Member

saudet commented Nov 22, 2019 via email

@caaiofarias
Copy link
Author

I guess not, because when i start in main thread on a static method it works. But when i create another thread, its take this time.

`public static void main(String[] args){
Producer producer = new Producer(buffer);
new Thread t = (producer);
producer.start(); //this works!!

// but when i call this in another thread, takes much time;
}`

@caaiofarias
Copy link
Author

@saudet sorry by inconvenience, but the problem is when i build the jar file in ant builder. Do you have any idea ? I'm really stucked on this.

@saudet
Copy link
Member

saudet commented Nov 23, 2019

Like I said, Java doesn't make any guarantees that a thread is going to get any CPU time at all. You need to provide a mechanism for that.

@saudet
Copy link
Member

saudet commented Nov 23, 2019

And don't use Ant to build, use Maven.

@caaiofarias
Copy link
Author

I know that, and i'm using concurrency to solve this. But the problem is before that. When i generate the jar file and execute it, the Thread(i tested in main too) simply stop in this line and back after a long time:

FFmpegFrameGrabber grabber = new FFmpegFrameGrabber("rtsp://host:passwd@ip/live4.sdp");

@saudet
Copy link
Member

saudet commented Nov 26, 2019

What does FFmpeg's log say? Make sure you've called FFmpegLogCallback.set() if you don't see anything in the log.

@caaiofarias
Copy link
Author

The log only start after the time (4 min in media).

@caaiofarias
Copy link
Author

It is really strange, the delay is when i instantiate the FFmpegGrabber and deploy in ant builder

@saudet
Copy link
Member

saudet commented Nov 26, 2019

Also make sure to set the "org.bytedeco.javacpp.logger.debug" system property to "true".

@caaiofarias
Copy link
Author

the error is not in your lib saudet! The delay is because the loading the jars files of libs(ffmpeg, opencv,javacv, etc).

closed

@saudet
Copy link
Member

saudet commented Nov 28, 2019

I see, so this is a duplicate of bytedeco/javacpp#287. For some reason, loading libraries on Android is really slow in some cases. This doesn't happen here on my devices, so I still don't know what causes this.

@caaiofarias
Copy link
Author

It is not on Android, but the ant build are copying all jar files on a fat jar, and i guess the delay is when unpack the jars files.

@saudet
Copy link
Member

saudet commented Dec 1, 2019

In that case we can reduce the amount of files to package:
https://github.com/bytedeco/javacpp-presets/wiki/Reducing-the-Number-of-Dependencies

@saudet
Copy link
Member

saudet commented Dec 19, 2019

The changes in commit bytedeco/javacpp@af0e513 should also help with this.

@caaiofarias
Copy link
Author

Save a lot of time now! Thank you so much man!!!!

I am having only one problem from loading classes, but i guess this will be fixed when you update the version right ?

@caaiofarias
Copy link
Author

@saudet , this work in a armv7 too ? now i need put on a armv7 arch.

If not, any tutorial how work on this ? i not found anything

@saudet
Copy link
Member

saudet commented Jan 7, 2020

I am having only one problem from loading classes, but i guess this will be fixed when you update the version right ?

What problem are you referring to? Gradle not working well with snapshots? It doesn't have that issue with released artifacts, yes.

@saudet , this work in a armv7 too ? now i need put on a armv7 arch.

If not, any tutorial how work on this ? i not found anything

Sure, binaries are available, something like this: https://github.com/bytedeco/javacpp-presets/wiki/Reducing-the-Number-of-Dependencies#including-binaries-only-for-mobile-platforms

@caaiofarias
Copy link
Author

caaiofarias commented Jan 7, 2020

It was my mistake. I'm installing the wrongs jars manually. Thanks for patience!

But now the debug is showing this errors above with a lot of tmp files from javacpp.

Oh, and its take much more time on rasp to initiate. I don't understand why(15 min more).

It is a way to have all desire so or jar files extracted on my raspberry ?
`
hf/libvchiq_arm.so
Debug: Loading /root/.javacpp/cache/jar_cache3578407362696651461.tmp/org/bytedeco/ffmpeg/linux-armhf/libvchiq_arm.so
Debug: Failed to load /root/.javacpp/cache/jar_cache3578407362696651461.tmp/org/bytedeco/ffmpeg/linux-armhf/libvchiq_arm.so: java.lang.UnsatisfiedLinkError: /root/.javacpp/cache/jar_cache3578407362696651461.tmp/org/bytedeco/ffmpeg/linux-armhf/libvchiq_arm.so: libvcos.so: cannot open shared object file: No such file or directory
Debug: Loading library vchiq_arm
[Loaded java.lang.ClassLoaderHelper from /usr/lib/jvm/java-8-openjdk-armhf/jre/lib/rt.jar]
Debug: Failed to load for vchiq_arm: java.lang.UnsatisfiedLinkError: no vchiq_arm in java.library.path
Debug: Locking /root/.javacpp/cache before extracting
Debug: Extracting jar:rsrc:ffmpeg-linux-armhf.jar!/org/bytedeco/ffmpeg/linux-armhf/libvcos.so
Debug: Loading /root/.javacpp/cache/jar_cache3578407362696651461.tmp/org/bytedeco/ffmpeg/linux-armhf/libvcos.so

`

@saudet
Copy link
Member

saudet commented Jan 21, 2020

Be sure to use the snapshots: http://bytedeco.org/builds/
The slow loading problem has been fixed in commit bytedeco/javacpp@af0e513

@saudet
Copy link
Member

saudet commented Feb 6, 2020

Please let me know if this latest fix doesn't solve the issue you're describing though. Thanks!

@caaiofarias
Copy link
Author

Yes! It's much faster now.

Thanks for listening saudet!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants