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

Print this page


   1 /*
   2  * Copyright (c) 2010, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  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


  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 


 147             // already populated, just return
 148             return;
 149         }
 150 
 151         List<String> npt = PlatformManager.getManager().getSupportedContentTypes();
 152         if (null != npt && !npt.isEmpty()) {
 153             supportedContentTypes.addAll(npt);
 154         }
 155 
 156         if (Logger.canLog(Logger.DEBUG)) {
 157             StringBuilder sb = new StringBuilder("JFXMedia supported content types:\n");
 158             for (String type : supportedContentTypes) {
 159                 sb.append("    ");
 160                 sb.append(type);
 161                 sb.append("\n");
 162             }
 163             Logger.logMsg(Logger.DEBUG, sb.toString());
 164         }
 165     }
 166 






















 167     /**
 168      * Whether a media source having the indicated content type may be played.
 169      *
 170      * @see MediaManager#canPlayContentType(java.lang.String)
 171      *
 172      * @throws IllegalArgumentException if
 173      * <code>contentType</code> is
 174      * <code>null</code>.
 175      */
 176     public boolean canPlayContentType(String contentType) {
 177         if (contentType == null) {
 178             throw new IllegalArgumentException("contentType == null!");
 179         }
 180 
 181         if (supportedContentTypes.isEmpty()) {
 182             loadContentTypes();
 183         }
 184 
 185         /*
 186          * Don't just use supportedContentType.contains(contentType) as that
 187          * is case sensitive, which we do not want
 188          */
 189         for (String type : supportedContentTypes) {
 190             if (contentType.equalsIgnoreCase(type)) {
 191                 return true;
 192             }
 193         }
 194 
 195         return false;
 196     }
 197 
 198     /**
 199      * Returns a copy of the array of supported content types.
 200      *
 201      * @return {@link String} array of supported content types.
 202      */
 203     public String[] getSupportedContentTypes() {
 204         if (supportedContentTypes.isEmpty()) {
 205             loadContentTypes();
 206         }
 207 
 208         return supportedContentTypes.toArray(new String[1]);































 209     }
 210 
 211     public static MetadataParser getMetadataParser(Locator locator) {
 212         return PlatformManager.getManager().createMetadataParser(locator);
 213     }
 214 
 215     /**
 216      * @see MediaManager#getPlayer(com.sun.media.jfxmedia.locator.Locator, int)
 217      */
 218     public MediaPlayer getPlayer(Locator locator) {
 219         // FIXME: remove this
 220         initNativeLayer();
 221 
 222         MediaPlayer player = PlatformManager.getManager().createMediaPlayer(locator);
 223         if (null == player) {
 224             throw new MediaException("Could not create player!");
 225         }
 226 
 227         // Cache a reference to the player.
 228         allMediaPlayers.put(player, Boolean.TRUE);


   1 /*
   2  * Copyright (c) 2010, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  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


  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     private final List<String> supportedProtocols =
  71             new ArrayList<>();
  72 
  73     /**
  74      * The NativeMediaManager singleton.
  75      */
  76     private static class NativeMediaManagerInitializer {
  77         private static final NativeMediaManager globalInstance
  78                 = new NativeMediaManager();
  79     }
  80 
  81     /**
  82      * Get the default
  83      * <code>NativeMediaManager</code>.
  84      *
  85      * @return the singleton
  86      * <code>NativeMediaManager</code> instance.
  87      */
  88     public static NativeMediaManager getDefaultInstance() {
  89         return NativeMediaManagerInitializer.globalInstance;
  90     }
  91 


 149             // already populated, just return
 150             return;
 151         }
 152 
 153         List<String> npt = PlatformManager.getManager().getSupportedContentTypes();
 154         if (null != npt && !npt.isEmpty()) {
 155             supportedContentTypes.addAll(npt);
 156         }
 157 
 158         if (Logger.canLog(Logger.DEBUG)) {
 159             StringBuilder sb = new StringBuilder("JFXMedia supported content types:\n");
 160             for (String type : supportedContentTypes) {
 161                 sb.append("    ");
 162                 sb.append(type);
 163                 sb.append("\n");
 164             }
 165             Logger.logMsg(Logger.DEBUG, sb.toString());
 166         }
 167     }
 168 
 169     private synchronized void loadProtocols() {
 170         if (!supportedProtocols.isEmpty()) {
 171             // already populated, just return
 172             return;
 173         }
 174 
 175         List<String> npt = PlatformManager.getManager().getSupportedProtocols();
 176         if (null != npt && !npt.isEmpty()) {
 177             supportedProtocols.addAll(npt);
 178         }
 179 
 180         if (Logger.canLog(Logger.DEBUG)) {
 181             StringBuilder sb = new StringBuilder("JFXMedia supported protocols:\n");
 182             for (String type : supportedProtocols) {
 183                 sb.append("    ");
 184                 sb.append(type);
 185                 sb.append("\n");
 186             }
 187             Logger.logMsg(Logger.DEBUG, sb.toString());
 188         }
 189     }
 190 
 191     /**
 192      * Whether a media source having the indicated content type may be played.
 193      *
 194      * @see MediaManager#canPlayContentType(java.lang.String)
 195      *
 196      * @throws IllegalArgumentException if
 197      * <code>contentType</code> is
 198      * <code>null</code>.
 199      */
 200     public boolean canPlayContentType(String contentType) {
 201         if (contentType == null) {
 202             throw new IllegalArgumentException("contentType == null!");
 203         }
 204 
 205         if (supportedContentTypes.isEmpty()) {
 206             loadContentTypes();
 207         }
 208 
 209         /*
 210          * Don't just use supportedContentType.contains(contentType) as that
 211          * is case sensitive, which we do not want
 212          */
 213         for (String type : supportedContentTypes) {
 214             if (contentType.equalsIgnoreCase(type)) {
 215                 return true;
 216             }
 217         }
 218 
 219         return false;
 220     }
 221 





 222     public String[] getSupportedContentTypes() {
 223         if (supportedContentTypes.isEmpty()) {
 224             loadContentTypes();
 225         }
 226 
 227         return supportedContentTypes.toArray(new String[1]);
 228     }
 229 
 230     /**
 231      * Whether a media source having the indicated protocol may be played.
 232      *
 233      * @see MediaManager#canPlayProtocol(java.lang.String)
 234      *
 235      * @throws IllegalArgumentException if
 236      * <code>protocol</code> is
 237      * <code>null</code>.
 238      */
 239     public boolean canPlayProtocol(String protocol) {
 240         if (protocol == null) {
 241             throw new IllegalArgumentException("protocol == null!");
 242         }
 243 
 244         if (supportedProtocols.isEmpty()) {
 245             loadProtocols();
 246         }
 247 
 248         /*
 249          * Don't just use supportedProtocols.contains(protocol) as that
 250          * is case sensitive, which we do not want
 251          */
 252         for (String type : supportedProtocols) {
 253             if (protocol.equalsIgnoreCase(type)) {
 254                 return true;
 255             }
 256         }
 257 
 258         return false;
 259     }
 260 
 261     public static MetadataParser getMetadataParser(Locator locator) {
 262         return PlatformManager.getManager().createMetadataParser(locator);
 263     }
 264 
 265     /**
 266      * @see MediaManager#getPlayer(com.sun.media.jfxmedia.locator.Locator, int)
 267      */
 268     public MediaPlayer getPlayer(Locator locator) {
 269         // FIXME: remove this
 270         initNativeLayer();
 271 
 272         MediaPlayer player = PlatformManager.getManager().createMediaPlayer(locator);
 273         if (null == player) {
 274             throw new MediaException("Could not create player!");
 275         }
 276 
 277         // Cache a reference to the player.
 278         allMediaPlayers.put(player, Boolean.TRUE);