modules/media/src/main/java/com/sun/media/jfxmediaimpl/NativeMediaManager.java

Print this page
rev 7591 : RT-38074: [macosx] Separate QTKit platform code from core media code so it can be removed for MAS
Reviewed-by:


  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.media.jfxmediaimpl;
  27 
  28 import com.sun.glass.utils.NativeLibLoader;
  29 import com.sun.media.jfxmedia.*;
  30 import com.sun.media.jfxmedia.events.MediaErrorListener;
  31 import com.sun.media.jfxmedia.locator.Locator;
  32 import com.sun.media.jfxmedia.logging.Logger;
  33 import com.sun.media.jfxmediaimpl.platform.PlatformManager;
  34 import java.lang.ref.WeakReference;
  35 import java.security.AccessController;

  36 import java.security.PrivilegedExceptionAction;
  37 import java.util.ArrayList;
  38 import java.util.List;
  39 import java.util.ListIterator;
  40 import java.util.Map;
  41 import java.util.WeakHashMap;
  42 
  43 /**
  44  * A class representing a native media engine.
  45  */
  46 public class NativeMediaManager {
  47     /**
  48      * The NativeMediaManager singleton.
  49      */
  50     // If we create NativeMediaManager here we will not be able to catch exception from constructor.
  51     private static NativeMediaManager theInstance = null;
  52     /**
  53      * Whether the native layer has been initialized.
  54      */
  55     private static boolean isNativeLayerInitialized = false;
  56     /**
  57      * The {@link MediaErrorListener}s.
  58      */
  59     // FIXME: Change to WeakHashMap<MediaErrorListener,Boolean> as it's more efficient
  60     private List<WeakReference<MediaErrorListener>> errorListeners =
  61             new ArrayList<WeakReference<MediaErrorListener>>();
  62     private static NativeMediaPlayerDisposer playerDisposer = new NativeMediaPlayerDisposer();

  63     /**
  64      * List of all un-disposed players.
  65      */
  66     private static Map<MediaPlayer,Boolean> allMediaPlayers =
  67             new WeakHashMap<MediaPlayer,Boolean>();
  68 
  69     // cached content types, so we don't have to poll and sort each time, this list
  70     // should never change once we're initialized
  71     private final List<String> supportedContentTypes =
  72             new ArrayList<String>();








  73 
  74     /**
  75      * Get the default
  76      * <code>NativeMediaManager</code>.
  77      *
  78      * @return the singleton
  79      * <code>NativeMediaManager</code> instance.
  80      */
  81     public static synchronized NativeMediaManager getDefaultInstance() {
  82         if (theInstance == null) {
  83             theInstance = new NativeMediaManager();
  84         }
  85         return theInstance;
  86     }
  87 
  88     //**************************************************************************
  89     //***** Constructors
  90     //**************************************************************************
  91     /**
  92      * Create a <code>NativeMediaManager</code>.
  93      */
  94     protected NativeMediaManager() {}
  95 
  96     /**
  97      * Initialize the native layer if it has not been so already.


  98      */
  99     synchronized static void initNativeLayer() {
 100         if (!isNativeLayerInitialized) {
 101             // preload platforms
 102             PlatformManager.getManager().preloadPlatforms();
 103 
 104             // Load native libraries.
 105             try {
 106                 AccessController.doPrivileged((PrivilegedExceptionAction) () -> {
 107                     if (HostUtils.isWindows() || HostUtils.isMacOSX()) {
 108                         NativeLibLoader.loadLibrary("glib-lite");
 109                     }
 110 
 111                     if (!HostUtils.isLinux() && !HostUtils.isIOS()) {
 112                         NativeLibLoader.loadLibrary("gstreamer-lite");
 113                     }
 114 
 115                     NativeLibLoader.loadLibrary("jfxmedia");
 116                     return null;
 117                 });
 118             } catch (Exception e) {
 119                 MediaUtils.error(null, MediaError.ERROR_MANAGER_ENGINEINIT_FAIL.code(),
 120                         "Unable to load one or more dependent libraries.", e);
 121                 return; // abort
 122             }
 123 
 124             // Get the Logger native side rolling before we load platforms
 125             if (!Logger.initNative()) {
 126                 MediaUtils.error(null, MediaError.ERROR_MANAGER_LOGGER_INIT.code(),
 127                         "Unable to init logger", null);
 128                 return; // abort
 129             }
 130 





 131             // load platforms
 132             PlatformManager.getManager().loadPlatforms();
 133 
 134             // Set the native initialization flag, even if initialization failed.
 135             isNativeLayerInitialized = true;
 136         }
 137     }
 138 
 139     //**************************************************************************
 140     //***** Public control functions
 141     //**************************************************************************
 142 
 143     private synchronized void loadContentTypes() {
 144         if (!supportedContentTypes.isEmpty()) {
 145             // already populated, just return
 146             return;
 147         }
 148 
 149         List<String> npt = PlatformManager.getManager().getSupportedContentTypes();
 150         if (null != npt && !npt.isEmpty()) {




  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.media.jfxmediaimpl;
  27 
  28 import com.sun.glass.utils.NativeLibLoader;
  29 import com.sun.media.jfxmedia.*;
  30 import com.sun.media.jfxmedia.events.MediaErrorListener;
  31 import com.sun.media.jfxmedia.locator.Locator;
  32 import com.sun.media.jfxmedia.logging.Logger;
  33 import com.sun.media.jfxmediaimpl.platform.PlatformManager;
  34 import java.lang.ref.WeakReference;
  35 import java.security.AccessController;
  36 import java.security.PrivilegedActionException;
  37 import java.security.PrivilegedExceptionAction;
  38 import java.util.ArrayList;
  39 import java.util.List;
  40 import java.util.ListIterator;
  41 import java.util.Map;
  42 import java.util.WeakHashMap;
  43 
  44 /**
  45  * A class representing a native media engine.
  46  */
  47 public class NativeMediaManager {
  48     /**





  49      * Whether the native layer has been initialized.
  50      */
  51     private static boolean isNativeLayerInitialized = false;
  52     /**
  53      * The {@link MediaErrorListener}s.
  54      */
  55     // FIXME: Change to WeakHashMap<MediaErrorListener,Boolean> as it's more efficient
  56     private final List<WeakReference<MediaErrorListener>> errorListeners =
  57             new ArrayList();
  58     private final static NativeMediaPlayerDisposer playerDisposer =
  59             new NativeMediaPlayerDisposer();
  60     /**
  61      * List of all un-disposed players.
  62      */
  63     private final static Map<MediaPlayer,Boolean> allMediaPlayers =
  64             new WeakHashMap();
  65 
  66     // cached content types, so we don't have to poll and sort each time, this list
  67     // should never change once we're initialized
  68     private final List<String> supportedContentTypes =
  69             new ArrayList();
  70 
  71     /**
  72      * The NativeMediaManager singleton.
  73      */
  74     private static class NativeMediaManagerInitializer {
  75         private static final NativeMediaManager globalInstance
  76                 = new NativeMediaManager();
  77     }
  78 
  79     /**
  80      * Get the default
  81      * <code>NativeMediaManager</code>.
  82      *
  83      * @return the singleton
  84      * <code>NativeMediaManager</code> instance.
  85      */
  86     public static NativeMediaManager getDefaultInstance() {
  87         return NativeMediaManagerInitializer.globalInstance;



  88     }
  89 
  90     //**************************************************************************
  91     //***** Constructors
  92     //**************************************************************************
  93     /**
  94      * Create a <code>NativeMediaManager</code>.
  95      */
  96     protected NativeMediaManager() {
  97         /*
  98          * Load native libraries. This must be done early as platforms may need
  99          * to attempt loading their own native libs that are dependent on these
 100          * This is a slight performance hit, but necessary otherwise we could
 101          * erroneously report content types for platforms that cannot be loaded
 102          */






 103         try {
 104             AccessController.doPrivileged((PrivilegedExceptionAction) () -> {
 105                 if (HostUtils.isWindows() || HostUtils.isMacOSX()) {
 106                     NativeLibLoader.loadLibrary("glib-lite");
 107                 }
 108 
 109                 if (!HostUtils.isLinux() && !HostUtils.isIOS()) {
 110                     NativeLibLoader.loadLibrary("gstreamer-lite");
 111                 }
 112 
 113                 NativeLibLoader.loadLibrary("jfxmedia");
 114                 return null;
 115             });
 116         } catch (PrivilegedActionException pae) {
 117             MediaUtils.error(null, MediaError.ERROR_MANAGER_ENGINEINIT_FAIL.code(),
 118                     "Unable to load one or more dependent libraries.", pae);

 119         }
 120 
 121         // Get the Logger native side rolling before we load platforms
 122         if (!Logger.initNative()) {
 123             MediaUtils.error(null, MediaError.ERROR_MANAGER_LOGGER_INIT.code(),
 124                     "Unable to init logger", null);
 125         }
 126     }
 127 
 128     /**
 129      * Initialize the native layer if it has not been so already.
 130      */
 131     synchronized static void initNativeLayer() {
 132         if (!isNativeLayerInitialized) {
 133             // load platforms
 134             PlatformManager.getManager().loadPlatforms();
 135 
 136             // Set the native initialization flag, even if initialization failed.
 137             isNativeLayerInitialized = true;
 138         }
 139     }
 140 
 141     //**************************************************************************
 142     //***** Public control functions
 143     //**************************************************************************
 144 
 145     private synchronized void loadContentTypes() {
 146         if (!supportedContentTypes.isEmpty()) {
 147             // already populated, just return
 148             return;
 149         }
 150 
 151         List<String> npt = PlatformManager.getManager().getSupportedContentTypes();
 152         if (null != npt && !npt.isEmpty()) {