changeset 5607:d5d1dfe43275

RT-33866 - MediaPlayer.play() method stops execution by almost 2 minutes while it must be asynchronous
author "Alexander Matveev <Alexander.Matveev@oracle.com>"
date Thu, 31 Oct 2013 15:23:14 -0700
parents 68f799d4b93c
children 71e86b48806e
files modules/media/src/main/java/com/sun/media/jfxmedia/locator/Locator.java modules/media/src/main/java/javafx/scene/media/MediaPlayer.java
diffstat 2 files changed, 32 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/modules/media/src/main/java/com/sun/media/jfxmedia/locator/Locator.java	Thu Oct 31 14:07:32 2013 -0700
+++ b/modules/media/src/main/java/com/sun/media/jfxmedia/locator/Locator.java	Thu Oct 31 15:23:14 2013 -0700
@@ -494,6 +494,16 @@
         }
         return contentLength;
     }
+    
+    /**
+     * Blocks until locator is ready (connection is established or failed).
+     */
+    public void waitForReadySignal() {
+        try {
+            readySignal.await();
+        } catch (Exception e) {
+        }
+    }
 
     /**
      * Retrieves the associated
--- a/modules/media/src/main/java/javafx/scene/media/MediaPlayer.java	Thu Oct 31 14:07:32 2013 -0700
+++ b/modules/media/src/main/java/javafx/scene/media/MediaPlayer.java	Thu Oct 31 15:23:14 2013 -0700
@@ -450,14 +450,19 @@
     }
     
     private void init() throws MediaException {
-        synchronized (disposeLock) {
-            if (getStatus() == Status.DISPOSED) {
-                return;
-            }
+        try {
+            // Create a new player
+            Locator locator = media.retrieveJfxLocator();
             
-            try {
-                // Create a new player
-                Locator locator = media.retrieveJfxLocator();
+            // This call will block until we connected or fail to connect.
+            // Call it here, so we do not block while initializing and holding locks like disposeLock.
+            locator.waitForReadySignal();
+
+            synchronized (disposeLock) {
+                if (getStatus() == Status.DISPOSED) {
+                    return;
+                }
+                
                 jfxPlayer = MediaManager.getPlayer(locator);
 
                 if (jfxPlayer != null) {
@@ -500,18 +505,18 @@
                         }
                     }
                 }
-            } catch (com.sun.media.jfxmedia.MediaException e) {
-                throw MediaException.exceptionToMediaException(e);
             }
+        } catch (com.sun.media.jfxmedia.MediaException e) {
+            throw MediaException.exceptionToMediaException(e);
+        }
 
-            // Register for the Player's event
-            Platform.runLater(new Runnable() {
-                @Override
-                public void run() {
-                    registerListeners();
-                }
-            });
-        }
+        // Register for the Player's event
+        Platform.runLater(new Runnable() {
+            @Override
+            public void run() {
+                registerListeners();
+            }
+        });
     }
 
     private class InitMediaPlayer implements Runnable {