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         // Now "universal" platform(s)
  93         if (isPlatformEnabled("JavaPlatform")) {
  94             platty = JavaPlatform.getPlatformInstance();
  95             if (null != platty) {
  96                 platforms.add(platty);
  97             }
  98         }
  99 
 100         if (!HostUtils.isIOS() && isPlatformEnabled("GSTPlatform")) {
 101             platty = GSTPlatform.getPlatformInstance();
 102             if (null != platty) {
 103                 platforms.add(platty);
 104             }
 105         }
 106 
 107         // Add after GSTPlatform so it's used as a fallback
 108         if (HostUtils.isMacOSX() && isPlatformEnabled("OSXPlatform")) {
 109             platty = OSXPlatform.getPlatformInstance();
 110             if (null != platty) {
 111                 platforms.add(platty);
 112             }
 113         }
 114 
 115         if (HostUtils.isIOS() && isPlatformEnabled("IOSPlatform")) {
 116             platty = IOSPlatform.getPlatformInstance();
 117             if (null != platty) {
 118                 platforms.add(platty);
 119             }
 120         }
 121 
 122         if (Logger.canLog(Logger.DEBUG)) {
 123             StringBuilder sb = new StringBuilder("Enabled JFXMedia platforms: ");
 124             for (Platform p : platforms) {
 125                 sb.append("\n   - ");
 126                 sb.append(p.getClass().getName());
 127             }
 128             Logger.logMsg(Logger.DEBUG, sb.toString());
 129         }
 130     }
 131 
 132     public synchronized void loadPlatforms() {
 133         // Use an iterator so we can remove on failure
 134         Iterator<Platform> iter = platforms.iterator();
 135         while (iter.hasNext()) {
 136             Platform platty = iter.next();
 137             if (!platty.loadPlatform()) {
 138                 if (Logger.canLog(Logger.DEBUG)) {
 139                     Logger.logMsg(Logger.DEBUG, "Failed to load platform: "+platty);
 140                 }
 141                 // remove it so it can't be reused
 142                 iter.remove();
 143             }
 144         }
 145     }
 146 
 147     public List<String> getSupportedContentTypes() {
 148         ArrayList<String> outTypes = new ArrayList<String>();
 149 
 150         if (!platforms.isEmpty()) {
 151             for (Platform platty : platforms) {
 152                 if (Logger.canLog(Logger.DEBUG)) {
 153                     Logger.logMsg(Logger.DEBUG, "Getting content types from platform: "+platty);
 154                 }
 155                 String[] npt = platty.getSupportedContentTypes();
 156                 if (npt != null) {
 157                     for (String type : npt) {
 158                         if (!outTypes.contains(type)) {
 159                             outTypes.add(type);
 160                         }
 161                     }
 162                 }
 163             }
 164         }
 165 
 166         return outTypes;
 167     }
 168 
 169     public MetadataParser createMetadataParser(Locator source) {
 170         for (Platform platty : platforms) {
 171             MetadataParser parser = platty.createMetadataParser(source);
 172             if (parser != null) {
 173                 return parser;
 174             }
 175         }
 176 
 177         return null;
 178     }
 179 
 180     // FIXME: Make Media non-platform specific, it doesn't need to be
 181     public Media createMedia(Locator source) {
 182         String mimeType = source.getContentType();
 183         // go down the list until we get one that can be created
 184         for (Platform platty : platforms) {
 185             if (platty.canPlayContentType(mimeType)) {
 186                 Media outMedia = platty.createMedia(source);
 187                 if (null != outMedia) {
 188                     return outMedia;
 189                 }
 190             }
 191         }
 192 
 193         return null;
 194     }
 195 
 196     public MediaPlayer createMediaPlayer(Locator source) {
 197         String mimeType = source.getContentType();
 198         // go down the list until we get one that can be created
 199         for (Platform platty : platforms) {
 200             if (platty.canPlayContentType(mimeType)) {
 201                 // attempt to preroll the player
 202                 Object cookie = platty.prerollMediaPlayer(source);
 203                 if (null != cookie) {
 204                     // OK to play, go ahead with player creation
 205                     MediaPlayer outPlayer = platty.createMediaPlayer(source, cookie);
 206                     if (null != outPlayer) {
 207                         return outPlayer;
 208                     }
 209                 }
 210             }
 211         }
 212 
 213         return null;
 214     }
 215 }