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/sinc_resampler.h"
     13 #include "media/base/vector_math.h"
     14 #include "media/base/yuv_convert.h"
     15 
     16 namespace media {
     17 
     18 namespace internal {
     19 // Platform specific initialization method.
     20 extern bool InitializeMediaLibraryInternal(const base::FilePath& module_dir);
     21 }  // namespace internal
     22 
     23 // Media must only be initialized once, so use a LazyInstance to ensure this.
     24 class MediaInitializer {
     25  public:
     26   bool Initialize(const base::FilePath& module_dir) {
     27     base::AutoLock auto_lock(lock_);
     28     if (!tried_initialize_) {
     29       tried_initialize_ = true;
     30       initialized_ = internal::InitializeMediaLibraryInternal(module_dir);
     31     }
     32     return initialized_;
     33   }
     34 
     35   bool IsInitialized() {
     36     base::AutoLock auto_lock(lock_);
     37     return initialized_;
     38   }
     39 
     40  private:
     41   friend struct base::DefaultLazyInstanceTraits<MediaInitializer>;
     42 
     43   MediaInitializer()
     44       : initialized_(false),
     45         tried_initialize_(false) {
     46     // Perform initialization of libraries which require runtime CPU detection.
     47     // TODO(dalecurtis): Add initialization of YUV, SincResampler.
     48     vector_math::Initialize();
     49     SincResampler::InitializeCPUSpecificFeatures();
     50     InitializeCPUSpecificYUVConversions();
     51   }
     52 
     53   ~MediaInitializer() {
     54     NOTREACHED() << "MediaInitializer should be leaky!";
     55   }
     56 
     57   base::Lock lock_;
     58   bool initialized_;
     59   bool tried_initialize_;
     60 
     61   DISALLOW_COPY_AND_ASSIGN(MediaInitializer);
     62 };
     63 
     64 static base::LazyInstance<MediaInitializer>::Leaky g_media_library =
     65     LAZY_INSTANCE_INITIALIZER;
     66 
     67 bool InitializeMediaLibrary(const base::FilePath& module_dir) {
     68   return g_media_library.Get().Initialize(module_dir);
     69 }
     70 
     71 void InitializeMediaLibraryForTesting() {
     72   base::FilePath module_dir;
     73   CHECK(PathService::Get(base::DIR_EXE, &module_dir));
     74   CHECK(g_media_library.Get().Initialize(module_dir));
     75 }
     76 
     77 bool IsMediaLibraryInitialized() {
     78   return g_media_library.Get().IsInitialized();
     79 }
     80 
     81 void InitializeCPUSpecificMediaFeatures() {
     82   // Force initialization of the media initializer, but don't call Initialize().
     83   g_media_library.Get();
     84 }
     85 
     86 }  // namespace media
     87