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

Strange conflict between JavaFx MediaPlayer and OpenCVFrameGrabber after Windows wakes up after Sleep mode #947

Open
d-a-gerashenko opened this issue Mar 28, 2018 · 31 comments

Comments

@d-a-gerashenko
Copy link
Contributor

Steps to reproduce:

  1. Run code:

     public class Main extends Application {
    
         MediaPlayer player;
    
         public static void main(String[] args) throws Exception {        
             Application.launch(args);
         }
    
         @Override
         public void start(Stage primaryStage) throws Exception {
             Platform.runLater(() -> {
                 player = new MediaPlayer(new Media(new File("test.mp3").toURI().toString()));
             });
             Button button = new Button("test");
             button.setOnAction((event) -> {
                 try {
                     System.out.println("row: " + Thread.currentThread().getStackTrace()[1].getLineNumber());
                     FrameGrabber grabber1 = OpenCVFrameGrabber.createDefault(0);
                     System.out.println("row: " + Thread.currentThread().getStackTrace()[1].getLineNumber());
                     grabber1.start();
                     System.out.println("row: " + Thread.currentThread().getStackTrace()[1].getLineNumber());
                     grabber1.grab();
                     System.out.println("row: " + Thread.currentThread().getStackTrace()[1].getLineNumber());
                     grabber1.close();
                     System.out.println("row: " + Thread.currentThread().getStackTrace()[1].getLineNumber());
                 } catch (Exception e) {
                     System.out.println(e);
                 }
             });
             primaryStage.setScene(new Scene(button));
             primaryStage.show();
         }
     }
    
  2. Press "test": works fine.

  3. Sleep/Wake up Windows.

  4. Press "test": stuck on start().

Stack:

"JavaFX Application Thread"
	at org.bytedeco.javacpp.opencv_videoio$VideoCapture.allocate(opencv_videoio.java)
	at org.bytedeco.javacpp.opencv_videoio$VideoCapture.<init>(opencv_videoio.java:1544)
	at org.bytedeco.javacv.OpenCVFrameGrabber.start(OpenCVFrameGrabber.java:176)
	at main.Main.lambda$start$1(Main.java:42)
	at main.Main$$Lambda$69.138177028.handle
	at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
	at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
	at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
	at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
	at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
	at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
	at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
	at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
	at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
	at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
	at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
	at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
	at javafx.event.Event.fireEvent(Event.java:198)
	at javafx.scene.Node.fireEvent(Node.java:8390)
	at javafx.scene.control.Button.fire(Button.java:185)
	at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
	at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
	at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
	at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
	at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
	at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
	at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
	at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
	at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
	at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
	at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
	at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
	at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
	at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
	at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
	at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
	at javafx.event.Event.fireEvent(Event.java:198)
	at javafx.scene.Scene$MouseHandler.process(Scene.java:3758)
	at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3486)
	at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
	at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2495)
	at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:350)
	at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:275)
	at java.security.AccessController.doPrivileged(AccessController.java)
	at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$350(GlassViewEventHandler.java:385)
	at com.sun.javafx.tk.quantum.GlassViewEventHandler$$Lambda$125.1775387165.get
	at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:404)
	at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:384)
	at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
	at com.sun.glass.ui.View.notifyMouse(View.java:927)
	at com.sun.glass.ui.win.WinApplication._runLoop(WinApplication.java)
	at com.sun.glass.ui.win.WinApplication.lambda$null$145(WinApplication.java:101)
	at com.sun.glass.ui.win.WinApplication$$Lambda$36.653687670.run
	at java.lang.Thread.run(Thread.java:745)
@d-a-gerashenko d-a-gerashenko changed the title Strange conflict between MediaPlayer and OpenCVFrameGrabber after Windows wakes up after Sleep mode Strange conflict between JavaFx\ MediaPlayer and OpenCVFrameGrabber after Windows wakes up after Sleep mode Mar 28, 2018
@d-a-gerashenko d-a-gerashenko changed the title Strange conflict between JavaFx\ MediaPlayer and OpenCVFrameGrabber after Windows wakes up after Sleep mode Strange conflict between JavaFx MediaPlayer and OpenCVFrameGrabber after Windows wakes up after Sleep mode Mar 28, 2018
@saudet
Copy link
Member

saudet commented Mar 28, 2018

Looks like a bug in OpenCV. Could you report this issue upstream?

Alternatively, you could try to use VideoInputFrameGrabber or FFmpegFrameGrabber. They usually work better than OpenCV.

@d-a-gerashenko
Copy link
Contributor Author

I don't know how to test FfmegFrameGrabber. It doesn't work with device int number. I tried to get list of devices with ffmpeg -list_devices true -f dshow -i dummy, got device name and than tried this

FrameGrabber grabber1 = FFmpegFrameGrabber.createDefault("Logitech HD Pro Webcam C920");
grabber1.setFormat("dshow");`

but I've got an error:

org.bytedeco.javacv.FrameGrabber$Exception:
avformat_open_input() error -5: Could not open input "Logitech HD Pro Webcam C920". (Has setFormat() been called?)

@saudet
Copy link
Member

saudet commented Mar 28, 2018 via email

@d-a-gerashenko
Copy link
Contributor Author

FrameGrabber grabber1 = FFmpegFrameGrabber.createDefault("video=\"Logitech HD Pro Webcam C920\"");
grabber1.setFormat("dshow");

The same result: "Could not open input..."

@saudet
Copy link
Member

saudet commented Mar 28, 2018

Does it work with the ffmpeg program?

@d-a-gerashenko
Copy link
Contributor Author

This code works fine: ffmpeg -f dshow -i video="Logitech HD Pro Webcam C920" out.avi

@saudet
Copy link
Member

saudet commented Mar 28, 2018

And this is with JavaCV 1.4.1-SNAPSHOT?

@d-a-gerashenko
Copy link
Contributor Author

<dependency>
    <groupId>org.bytedeco</groupId>
    <artifactId>javacv-platform</artifactId>
    <version>1.4</version>
</dependency>

@saudet
Copy link
Member

saudet commented Mar 28, 2018

Apparently the magic string would be "video=Logitech HD Pro Webcam C920", without the double quotes:
https://ffmpeg.zeranoe.com/forum/viewtopic.php?t=274
That works fine on my machine.

@d-a-gerashenko
Copy link
Contributor Author

d-a-gerashenko commented Mar 28, 2018

I tested FFmpegFrameGrabber. The same problem. Stuck on start after sleep.

@saudet
Copy link
Member

saudet commented Mar 29, 2018 via email

@d-a-gerashenko
Copy link
Contributor Author

Stuck.

@saudet
Copy link
Member

saudet commented Mar 29, 2018

So, bug in JavaFX? File an issue over there: https://github.com/javafxports/openjdk-jfx/issues

@d-a-gerashenko
Copy link
Contributor Author

All works except JavaCv. MedeaPlayer works fine after sleep. I noticed that I could workaround this with invoking dispose() for player after it's played a sound. I'm not sure but I suppose that this bug belongs to JavaCv project. Or maybe JavaFx and JavaCv use some native unsafe methods deep inside.

@saudet
Copy link
Member

saudet commented Mar 29, 2018

What other library are you using to capture from your webcam?

@d-a-gerashenko
Copy link
Contributor Author

The class in code that I posted above is the only. I created an empty test project and reproduced error in it.

@saudet
Copy link
Member

saudet commented Mar 29, 2018

You mean it happens without JavaFX? Sounds like a bug in your webcam driver. Could you try another application that captures from your camera like Skype?

@d-a-gerashenko
Copy link
Contributor Author

The only problem is that Grabber hangs on start(). It happens only when there is allocated JavaFx's MediaPlayer. And it happens only after Windows wakeup after sleep mode. App restarting removes this bug. Other components (JavaFx) and programs (Skype) works fine.

@saudet
Copy link
Member

saudet commented Mar 29, 2018

Ok, so it's most likely a bug in JavaFX.

@d-a-gerashenko
Copy link
Contributor Author

But why it happens only here? org.bytedeco.javacpp.opencv_videoio$VideoCapture.allocate(opencv_videoio.java)

@saudet
Copy link
Member

saudet commented Mar 29, 2018

I thought you said it also happened with FFmpeg and videoInput. Please use FFmpegFrameGrabber and VideoInputFrameGrabber instead. Like I said, they usually work better than OpenCV.

@d-a-gerashenko
Copy link
Contributor Author

d-a-gerashenko commented Mar 30, 2018

It's not a driver because I tested it on many different cameras and PCs. And I actually don't believe that JavaFX developers will agree that this is bug of JavaFX because JavaFX works fine. It doesn't stuck.

VideoInputFrameGrabber

"JavaFX Application Thread"
	at org.bytedeco.javacpp.videoInputLib$videoInput.setupDevice(videoInputLib.java)
	at org.bytedeco.javacv.VideoInputFrameGrabber.start(VideoInputFrameGrabber.java:108)
	at org.bytedeco.javacv.VideoInputFrameGrabber.start(VideoInputFrameGrabber.java:101)
	at main.Main.lambda$start$1(Main.java:45)
	at main.Main$$Lambda$69.599164406.handle

FFmpegFrameGrabber

"JavaFX Application Thread"
	at org.bytedeco.javacpp.avformat.avformat_open_input(avformat.java)
	at org.bytedeco.javacv.FFmpegFrameGrabber.startUnsafe(FFmpegFrameGrabber.java:569)
	at org.bytedeco.javacv.FFmpegFrameGrabber.start(FFmpegFrameGrabber.java:508)
	at main.Main.lambda$start$1(Main.java:46)
	at main.Main$$Lambda$69.65223853.handle

@d-a-gerashenko
Copy link
Contributor Author

It happens not only in JavaFX Thread. I changed code. Now grabber starts in new thread after button click. I pressed two times and got this:

"Thread-12"
	at org.bytedeco.javacpp.avformat.avformat_open_input(avformat.java)
	at org.bytedeco.javacv.FFmpegFrameGrabber.startUnsafe(FFmpegFrameGrabber.java:569)
	at org.bytedeco.javacv.FFmpegFrameGrabber.start(FFmpegFrameGrabber.java:508)
	at main.Main.lambda$null$1(Main.java:47)
	at main.Main$$Lambda$138.973397762.run
	at java.lang.Thread.run(Thread.java:745)

"Thread-13"
	at org.bytedeco.javacpp.avformat.avformat_open_input(avformat.java)
	at org.bytedeco.javacv.FFmpegFrameGrabber.startUnsafe(FFmpegFrameGrabber.java:569)
	at org.bytedeco.javacv.FFmpegFrameGrabber.start(FFmpegFrameGrabber.java:508)
	at main.Main.lambda$null$1(Main.java:47)
	at main.Main$$Lambda$138.973397762.run
	at java.lang.Thread.run(Thread.java:745)

@saudet
Copy link
Member

saudet commented Mar 30, 2018

It's highly unlikely that OpenCV, FFmpeg, and videoInput all have the same bug, and you say this only happens with JavaFX, so...

@d-a-gerashenko
Copy link
Contributor Author

But it's not raw cpp OpenCV, FFmpeg... It's wrapped.

@saudet
Copy link
Member

saudet commented Mar 30, 2018

So, sure, I've been telling to try to capture from the webcam with another library. I'm pretty sure it will do the same thing, but it's up to you whether you want to debug this or not!

@saudet
Copy link
Member

saudet commented Mar 31, 2018

For example, what happens when capturing with Webcam Capture?
https://github.com/sarxos/webcam-capture/tree/master/webcam-capture-examples/webcam-capture-javafx

@d-a-gerashenko
Copy link
Contributor Author

I will test it on opencv java and on this.

@saudet
Copy link
Member

saudet commented Mar 31, 2018

BTW, the JavaCPP Presets for OpenCV now bundle the official Java API as well too:
https://github.com/bytedeco/javacpp-presets/tree/master/opencv#documentation

@d-a-gerashenko
Copy link
Contributor Author

I tried it with org.opencv.core.Core and got the same result. Probably it's JavaFx's bug.

@johanvos
Copy link
Contributor

johanvos commented Apr 4, 2018

See also: javafxports/openjdk-jfx#56

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

3 participants