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
  23  * questions.
  24  */
  25 
  26 package com.sun.media.jfxmediaimpl.platform;
  27 
  28 import com.sun.media.jfxmedia.Media;
  29 import com.sun.media.jfxmedia.MediaPlayer;
  30 import com.sun.media.jfxmedia.MetadataParser;
  31 import com.sun.media.jfxmedia.locator.Locator;
  32 import com.sun.media.jfxmedia.logging.Logger;
  33 import com.sun.media.jfxmediaimpl.platform.java.JavaPlatform;
  34 import com.sun.media.jfxmediaimpl.HostUtils;
  35 import com.sun.media.jfxmediaimpl.platform.gstreamer.GSTPlatform;
  36 import com.sun.media.jfxmediaimpl.platform.osx.OSXPlatform;
  37 import com.sun.media.jfxmediaimpl.platform.ios.IOSPlatform;
  38 import java.security.AccessController;
  39 import java.security.PrivilegedAction;
  40 import java.util.ArrayList;
  41 import java.util.Iterator;
  42 import java.util.List;
  43 
  44 /**
  45  * Core media platform management code.
  46  */
  47 public final class PlatformManager {
  48     private static String enabledPlatforms;
  49     static {
  50         AccessController.doPrivileged((PrivilegedAction) () -> {
  51             getPlatformSettings();
  52             return null;
  53         });
  54     }
  55 
  56     private static void getPlatformSettings() {
  57         // get enabled platforms, comma separated list, e.g., -Djfxmedia.platforms=GSTPlatform,OSXPlatform
  58         enabledPlatforms = System.getProperty("jfxmedia.platforms", "").toLowerCase();
  59     }
  60 
  61     private static boolean isPlatformEnabled(String name) {
  62         if (null == enabledPlatforms || enabledPlatforms.length() == 0) {
  63             // everything enabled
  64             return true;
  65         }
  66         return (enabledPlatforms.indexOf(name.toLowerCase()) != -1);
  67     }
  68 
  69     private static final class PlatformManagerInitializer {
  70         private static final PlatformManager globalInstance = new PlatformManager();
  71     }
  72 
  73     public static PlatformManager getManager() {
  74         return PlatformManagerInitializer.globalInstance;
  75     }
  76 
  77     private final List<Platform> platforms;
  78 
  79     private PlatformManager() {
  80         platforms = new ArrayList<Platform>();
  81 
  82         Platform platty;
  83 
  84         /*
  85          * We don't want to fully initialize the platforms here for performance
  86          * reasons but some platforms may be dependent on native resources that
  87          * need to be loaded, those platforms need to be given a chance to load
  88          * those resources (without initializing) and determine if the natives
  89          * are available.
  90          * 
  91          */
  92 
  93         // Now "universal" platform(s)
  94         if (isPlatformEnabled("JavaPlatform")) {
  95             platty = JavaPlatform.getPlatformInstance();
  96             if (null != platty) {
  97                 platforms.add(platty);
  98             }
  99         }
 100 
 101         if (!HostUtils.isIOS() && isPlatformEnabled("GSTPlatform")) {
 102             platty = GSTPlatform.getPlatformInstance();
 103             if (null != platty) {
 104                 platforms.add(platty);
 105             }
 106         }
 107 
 108         // Add after GSTPlatform so it's used as a fallback
 109         if (HostUtils.isMacOSX() && isPlatformEnabled("OSXPlatform")) {
 110             platty = OSXPlatform.getPlatformInstance();
 111             if (null != platty) {
 112                 platforms.add(platty);
 113             }
 114         }
 115 
 116         if (HostUtils.isIOS() && isPlatformEnabled("IOSPlatform")) {
 117             platty = IOSPlatform.getPlatformInstance();
 118             if (null != platty) {
 119                 platforms.add(platty);
 120             }
 121         }
 122 
 123         if (Logger.canLog(Logger.DEBUG)) {
 124             StringBuilder sb = new StringBuilder("Enabled JFXMedia platforms: ");
 125             for (Platform p : platforms) {
 126                 sb.append("\n   - ");
 127                 sb.append(p.getClass().getName());
 128             }
 129             Logger.logMsg(Logger.DEBUG, sb.toString());
 130         }
 131     }
 132 
 133     public synchronized void loadPlatforms() {
 134         // Use an iterator so we can remove on failure
 135         Iterator<Platform> iter = platforms.iterator();
 136         while (iter.hasNext()) {
 137             Platform platty = iter.next();
 138             if (!platty.loadPlatform()) {
 139                 if (Logger.canLog(Logger.DEBUG)) {
 140                     Logger.logMsg(Logger.DEBUG, "Failed to load platform: "+platty);
 141                 }
 142                 // remove it so it can't be reused
 143                 iter.remove();
 144             }
 145         }
 146     }
 147 
 148     public List<String> getSupportedContentTypes() {
 149         ArrayList<String> outTypes = new ArrayList<String>();
 150 
 151         if (!platforms.isEmpty()) {
 152             for (Platform platty : platforms) {
 153                 if (Logger.canLog(Logger.DEBUG)) {
 154                     Logger.logMsg(Logger.DEBUG, "Getting content types from platform: "+platty);
 155                 }
 156                 String[] npt = platty.getSupportedContentTypes();
 157                 if (npt != null) {
 158                     for (String type : npt) {
 159                         if (!outTypes.contains(type)) {
 160                             outTypes.add(type);
 161                         }
 162                     }
 163                 }
 164             }
 165         }
 166 
 167         return outTypes;
 168     }
 169 
 170     public MetadataParser createMetadataParser(Locator source) {
 171         for (Platform platty : platforms) {
 172             MetadataParser parser = platty.createMetadataParser(source);
 173             if (parser != null) {
 174                 return parser;
 175             }
 176         }
 177 
 178         return null;
 179     }
 180 
 181     // FIXME: Make Media non-platform specific, it doesn't need to be
 182     public Media createMedia(Locator source) {
 183         String mimeType = source.getContentType();
 184         // go down the list until we get one that can be created
 185         for (Platform platty : platforms) {
 186             if (platty.canPlayContentType(mimeType)) {
 187                 Media outMedia = platty.createMedia(source);
 188                 if (null != outMedia) {
 189                     return outMedia;
 190                 }
 191             }
 192         }
 193 
 194         return null;
 195     }
 196 
 197     public MediaPlayer createMediaPlayer(Locator source) {
 198         String mimeType = source.getContentType();
 199         // go down the list until we get one that can be created
 200         for (Platform platty : platforms) {
 201             if (platty.canPlayContentType(mimeType)) {
 202                 // attempt to preroll the player
 203                 Object cookie = platty.prerollMediaPlayer(source);
 204                 if (null != cookie) {
 205                     // OK to play, go ahead with player creation
 206                     MediaPlayer outPlayer = platty.createMediaPlayer(source, cookie);
 207                     if (null != outPlayer) {
 208                         return outPlayer;
 209                     }
 210                 }
 211             }
 212         }
 213 
 214         return null;
 215     }
 216 }