1 /*
   2  * Copyright (c) 2010, 2013, 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 #include "MediaManager.h"
  27 #include <Common/ProductFlags.h>
  28 #include <Common/VSMemory.h>
  29 
  30 #include <PipelineManagement/PipelineFactory.h>
  31 #include <Locator/Locator.h>
  32 
  33 #include <platform/gstreamer/GstMediaManager.h>
  34 #include <Utils/JfxCriticalSection.h>
  35 #include <jfxmedia_errors.h>
  36 
  37 CMediaManager::MMSingleton CMediaManager::s_Singleton;
  38 
  39 //*************************************************************************************************
  40 //********** Empty content types list in case PipelineFactory is not available.
  41 //*************************************************************************************************
  42 const static ContentTypesList EMPTY_LIST;
  43 
  44 //*************************************************************************************************
  45 //********** class CMediaManager
  46 //*************************************************************************************************
  47 CMediaManager::CMediaManager()
  48 :   m_uInternalError(ERROR_NONE)
  49 {}
  50 
  51 CMediaManager::~CMediaManager()
  52 {}
  53 
  54 /**
  55  * CMediaManager::GetInstance()
  56  *
  57  * @return  CMediaManager* singleton
  58  */
  59 uint32_t CMediaManager::GetInstance(CMediaManager** ppMediaManager)
  60 {
  61     return s_Singleton.GetInstance(ppMediaManager);
  62 }
  63 
  64 /**
  65  * CMediaManager::CreateInstance() creates an instance of the class
  66  * This method is used by Singleton class to create the actual instace of a class.
  67  * When the method is protected of private, Singleton class should be a friend for the class.
  68  *
  69  * @return  CMediaManager* instance
  70  */
  71 uint32_t CMediaManager::CreateInstance(CMediaManager** ppMediaManager)
  72 {
  73 #if ENABLE_PLATFORM_GSTREAMER
  74 #if !defined(TARGET_OS_WIN32) && !defined(TARGET_OS_MAC) && !defined(TARGET_OS_LINUX)
  75     return ERROR_OS_UNSUPPORTED;
  76 #else
  77     CGstMediaManager* pGstManager = new(nothrow) CGstMediaManager();
  78     if (NULL == pGstManager)
  79         return ERROR_MEMORY_ALLOCATION;
  80 
  81     if (ERROR_NONE != (pGstManager->m_uInternalError = pGstManager->Init()))
  82         return ERROR_MANAGER_CREATION;
  83 
  84     *ppMediaManager = pGstManager;
  85 
  86     return ERROR_NONE;
  87 #endif  // !defined ...
  88 #else
  89     return ERROR_PLATFORM_UNSUPPORTED;
  90 #endif  //ENABLE_PLATFORM_GSTREAMER
  91 }
  92 
  93 /**
  94  * CMediaManager::SetWarningListener(const CMediaWarningListener* pWarningListener)
  95  *
  96  * Sets the listener to receive notifications of warnings
  97  * which are not specific to a given pipeline.
  98  *
  99  * @param   The listener.
 100  */
 101 void CMediaManager::SetWarningListener(CMediaWarningListener* pWarningListener)
 102 {
 103     m_pWarningListener = pWarningListener;
 104 }
 105 
 106 bool CMediaManager::CanPlayContentType(string contentType)
 107 {
 108     CPipelineFactory*   pPipelineFactory = NULL;
 109     uint32_t            uRetCode;
 110 
 111     uRetCode = CPipelineFactory::GetInstance(&pPipelineFactory);
 112     if (ERROR_NONE != uRetCode)
 113         return false;
 114     else if (NULL == pPipelineFactory)
 115         return false;
 116 
 117     return pPipelineFactory->CanPlayContentType(contentType);
 118 }
 119 
 120 const ContentTypesList& CMediaManager::GetSupportedContentTypes()
 121 {
 122     CPipelineFactory*   pPipelineFactory = NULL;
 123     uint32_t            uRetCode;
 124 
 125     uRetCode = CPipelineFactory::GetInstance(&pPipelineFactory);
 126     if (ERROR_NONE != uRetCode)
 127         return EMPTY_LIST;
 128     else if (NULL == pPipelineFactory)
 129         return EMPTY_LIST;
 130 
 131     return pPipelineFactory->GetSupportedContentTypes();
 132 }
 133 
 134 /**
 135  * CMediaManager::CreatePlayer(CLocator locator)
 136  *
 137  * @param   locator
 138  *
 139  * @return  Pointer to a new CMedia object.
 140  */
 141 uint32_t CMediaManager::CreatePlayer(CLocator* pLocator, CPipelineOptions* pOptions, CMedia** ppMedia)
 142 {
 143     CPipeline*          pPipeline = NULL;
 144     CPipelineFactory*   pPipelineFactory = NULL;
 145     uint32_t            uRetCode;
 146 
 147     if (NULL == pLocator)
 148         return ERROR_LOCATOR_NULL;
 149 
 150     uRetCode = CPipelineFactory::GetInstance(&pPipelineFactory);
 151     if (ERROR_NONE != uRetCode)
 152         return uRetCode;
 153     else if (NULL == pPipelineFactory)
 154         return ERROR_FACTORY_NULL;
 155 
 156     //***** Initialize the return value
 157     *ppMedia    = NULL;
 158 
 159     //***** If we have a null option object, create one
 160     if (NULL == pOptions)
 161     {
 162         pOptions = new (nothrow)CPipelineOptions();
 163         if (NULL == pOptions)
 164             return ERROR_MEMORY_ALLOCATION;
 165     }
 166 
 167     //***** Try to create a pipeline
 168     uRetCode = pPipelineFactory->CreatePlayerPipeline(pLocator, pOptions, &pPipeline);
 169 
 170     //***** Create the new CMedia object
 171     if (ERROR_NONE == uRetCode)
 172     {
 173         //***** Try to create a CMedia to associate with the pipeline
 174         *ppMedia = new(nothrow) CMedia(pPipeline);
 175 
 176         if (NULL == *ppMedia)
 177         {
 178             //Cleanup if media creation failed.
 179             delete pPipeline;
 180             uRetCode = ERROR_MEDIA_CREATION;
 181         }
 182     }
 183 
 184     return uRetCode;
 185 }
 186 
 187 /**
 188  * CMediaManager::CreateMedia(CLocator locator)
 189  *
 190  * Creates a media object, given a locator and a set of options.
 191  *
 192  * @param   pLocator    pointer to a CLocator object
 193  * @param   pOptions    pointer to a CPipelienOptions object
 194  *
 195  * @return  Pointer to a new CMedia object.
 196  */
 197 uint32_t CMediaManager::CreateMedia(CLocator* pLocator, CPipelineOptions* pOptions, CMedia** ppMedia)
 198 {
 199     CPipeline*          pPipeline = NULL;
 200     CPipelineFactory*   pPipelineFactory = NULL;
 201     uint32_t            uRetCode;
 202 
 203     if (NULL == pLocator)
 204         return ERROR_LOCATOR_NULL;
 205 
 206     uRetCode = CPipelineFactory::GetInstance(&pPipelineFactory);
 207     if (ERROR_NONE != uRetCode)
 208         return uRetCode;
 209     else if (NULL == pPipelineFactory)
 210         return ERROR_FACTORY_NULL;
 211 
 212     //***** Initialize the return value
 213     *ppMedia    = NULL;
 214 
 215     //***** If we have a null option object, create one
 216     if (NULL == pOptions)
 217     {
 218         pOptions = new (nothrow) CPipelineOptions();
 219         if (NULL == pOptions)
 220             return ERROR_MEMORY_ALLOCATION;
 221     }
 222 
 223     //***** Do the real work
 224     if ((CPipelineOptions::kAudioPlaybackPipeline == pOptions->GetPipelineType()) || (CPipelineOptions::kAVPlaybackPipeline == pOptions->GetPipelineType()))
 225     {
 226         //***** Create a player pipleine first
 227 #if JFXMEDIA_DEBUG
 228         printf("-- CreateMedia : create player pipeline\n");
 229 #endif
 230         uRetCode = pPipelineFactory->CreatePlayerPipeline(pLocator, pOptions, &pPipeline);
 231 
 232         //***** Create the new CMedia object
 233         if (ERROR_NONE == uRetCode)
 234         {
 235             //***** Create a media object and attach the pipeline to the media object
 236             *ppMedia    = new(nothrow) CMedia(pPipeline);
 237 
 238             if (NULL == *ppMedia)
 239             {
 240                 //Cleanup if media creation failed.
 241                 delete pPipeline;
 242                 uRetCode = ERROR_MEDIA_CREATION;
 243             }
 244         }
 245     }
 246 
 247 #if JFXMEDIA_DEBUG
 248         printf("-- CreateMedia : finish\n");
 249 #endif
 250     return uRetCode;
 251 }