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