Home | History | Annotate | Download | only in base
      1 // Copyright 2013 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "media/base/media.h"
      6 
      7 #include "base/files/file_path.h"
      8 #include "base/lazy_instance.h"
      9 #include "base/path_service.h"
     10 #include "base/synchronization/lock.h"
     11 #include "build/build_config.h"
     12 #include "media/base/yuv_convert.h"
     13 
     14 namespace media {
     15 
     16 namespace internal {
     17 // Platform specific initialization method.
     18 extern bool InitializeMediaLibraryInternal(const base::FilePath& module_dir);
     19 }  // namespace internal
     20 
     21 // Media must only be initialized once, so use a LazyInstance to ensure this.
     22 class MediaInitializer {
     23  public:
     24   bool Initialize(const base::FilePath& module_dir) {
     25     base::AutoLock auto_lock(lock_);
     26     if (!tried_initialize_) {
     27       tried_initialize_ = true;
     28       initialized_ = internal::InitializeMediaLibraryInternal(module_dir);
     29     }
     30     return initialized_;
     31   }
     32 
     33   bool IsInitialized() {
     34     base::AutoLock auto_lock(lock_);
     35     return initialized_;
     36   }
     37 
     38  private:
     39   friend struct base::DefaultLazyInstanceTraits<MediaInitializer>;
     40 
     41   MediaInitializer()
     42       : initialized_(false),
     43         tried_initialize_(false) {
     44     // Perform initialization of libraries which require runtime CPU detection.
     45     InitializeCPUSpecificYUVConversions();
     46   }
     47 
     48   ~MediaInitializer() {
     49     NOTREACHED() << "MediaInitializer should be leaky!";
     50   }
     51 
     52   base::Lock lock_;
     53   bool initialized_;
     54   bool tried_initialize_;
     55 
     56   DISALLOW_COPY_AND_ASSIGN(MediaInitializer);
     57 };
     58 
     59 static base::LazyInstance<MediaInitializer>::Leaky g_media_library =
     60     LAZY_INSTANCE_INITIALIZER;
     61 
     62 bool InitializeMediaLibrary(const base::FilePath& module_dir) {
     63   return g_media_library.Get().Initialize(module_dir);
     64 }
     65 
     66 void InitializeMediaLibraryForTesting() {
     67   base::FilePath module_dir;
     68   CHECK(PathService::Get(base::DIR_EXE, &module_dir));
     69   CHECK(g_media_library.Get().Initialize(module_dir));
     70 }
     71 
     72 bool IsMediaLibraryInitialized() {
     73   return g_media_library.Get().IsInitialized();
     74 }
     75 
     76 void InitializeCPUSpecificMediaFeatures() {
     77   // Force initialization of the media initializer, but don't call Initialize().
     78   g_media_library.Get();
     79 }
     80 
     81 }  // namespace media
     82