OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "media/base/media.h" | 5 #include "media/base/media.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/path_service.h" | 11 #include "base/path_service.h" |
12 #include "base/strings/stringize_macros.h" | 12 #include "base/strings/stringize_macros.h" |
13 #include "media/ffmpeg/ffmpeg_common.h" | 13 #include "media/ffmpeg/ffmpeg_common.h" |
14 | 14 |
15 #if !defined(USE_SYSTEM_FFMPEG) | 15 #if !defined(USE_SYSTEM_FFMPEG) |
16 #include "third_party/ffmpeg/ffmpeg_stubs.h" | 16 #include "third_party/ffmpeg/ffmpeg_stubs.h" |
17 | 17 |
18 using third_party_ffmpeg::kNumStubModules; | 18 using third_party_ffmpeg::kNumStubModules; |
19 using third_party_ffmpeg::kModuleFfmpegsumo; | 19 using third_party_ffmpeg::kModuleFfmpegsumo; |
20 using third_party_ffmpeg::InitializeStubs; | 20 using third_party_ffmpeg::InitializeStubs; |
21 using third_party_ffmpeg::StubPathMap; | 21 using third_party_ffmpeg::StubPathMap; |
22 #endif // !defined(USE_SYSTEM_FFMPEG) | 22 #endif // !defined(USE_SYSTEM_FFMPEG) |
23 | 23 |
24 namespace media { | 24 namespace media { |
| 25 namespace internal { |
25 | 26 |
26 // Handy to prevent shooting ourselves in the foot with macro wizardry. | 27 // Handy to prevent shooting ourselves in the foot with macro wizardry. |
27 #if !defined(LIBAVCODEC_VERSION_MAJOR) || \ | 28 #if !defined(LIBAVCODEC_VERSION_MAJOR) || \ |
28 !defined(LIBAVFORMAT_VERSION_MAJOR) || \ | 29 !defined(LIBAVFORMAT_VERSION_MAJOR) || \ |
29 !defined(LIBAVUTIL_VERSION_MAJOR) | 30 !defined(LIBAVUTIL_VERSION_MAJOR) |
30 #error FFmpeg headers not included! | 31 #error FFmpeg headers not included! |
31 #endif | 32 #endif |
32 | 33 |
33 #define AVCODEC_VERSION STRINGIZE(LIBAVCODEC_VERSION_MAJOR) | 34 #define AVCODEC_VERSION STRINGIZE(LIBAVCODEC_VERSION_MAJOR) |
34 #define AVFORMAT_VERSION STRINGIZE(LIBAVFORMAT_VERSION_MAJOR) | 35 #define AVFORMAT_VERSION STRINGIZE(LIBAVFORMAT_VERSION_MAJOR) |
35 #define AVUTIL_VERSION STRINGIZE(LIBAVUTIL_VERSION_MAJOR) | 36 #define AVUTIL_VERSION STRINGIZE(LIBAVUTIL_VERSION_MAJOR) |
36 | 37 |
37 #if defined(OS_MACOSX) | 38 #if defined(OS_MACOSX) |
38 // TODO(evan): should be using .so like ffmepgsumo here. | 39 // TODO(evan): should be using .so like ffmepgsumo here. |
39 #define DSO_NAME(MODULE, VERSION) ("lib" MODULE "." VERSION ".dylib") | 40 #define DSO_NAME(MODULE, VERSION) ("lib" MODULE "." VERSION ".dylib") |
40 static const base::FilePath::CharType kSumoLib[] = | 41 static const base::FilePath::CharType kSumoLib[] = |
41 FILE_PATH_LITERAL("ffmpegsumo.so"); | 42 FILE_PATH_LITERAL("ffmpegsumo.so"); |
42 #elif defined(OS_POSIX) | 43 #elif defined(OS_POSIX) |
43 #define DSO_NAME(MODULE, VERSION) ("lib" MODULE ".so." VERSION) | 44 #define DSO_NAME(MODULE, VERSION) ("lib" MODULE ".so." VERSION) |
44 static const base::FilePath::CharType kSumoLib[] = | 45 static const base::FilePath::CharType kSumoLib[] = |
45 FILE_PATH_LITERAL("libffmpegsumo.so"); | 46 FILE_PATH_LITERAL("libffmpegsumo.so"); |
46 #else | 47 #else |
47 #error "Do not know how to construct DSO name for this OS." | 48 #error "Do not know how to construct DSO name for this OS." |
48 #endif | 49 #endif |
49 | 50 |
50 // Use a global to indicate whether the library has been initialized or not. We | 51 bool InitializeMediaLibraryInternal(const base::FilePath& module_dir, |
51 // rely on function level static initialization in InitializeMediaLibrary() to | 52 bool for_testing) { |
52 // guarantee this is only set once in a thread safe manner. | |
53 static bool g_media_library_is_initialized = false; | |
54 | |
55 static bool InitializeMediaLibraryInternal(const base::FilePath& module_dir) { | |
56 DCHECK(!g_media_library_is_initialized); | |
57 | |
58 #if defined(USE_SYSTEM_FFMPEG) | 53 #if defined(USE_SYSTEM_FFMPEG) |
59 // No initialization is necessary when using system ffmpeg, | 54 // No initialization is necessary when using system ffmpeg, |
60 // we just link directly with system ffmpeg libraries. | 55 // we just link directly with system ffmpeg libraries. |
61 g_media_library_is_initialized = true; | 56 return true; |
62 #else | 57 #else |
63 StubPathMap paths; | 58 StubPathMap paths; |
64 | 59 |
65 // First try to initialize with Chrome's sumo library. | 60 // First try to initialize with Chrome's sumo library. |
66 DCHECK_EQ(kNumStubModules, 1); | 61 DCHECK_EQ(kNumStubModules, 1); |
67 paths[kModuleFfmpegsumo].push_back(module_dir.Append(kSumoLib).value()); | 62 paths[kModuleFfmpegsumo].push_back(module_dir.Append(kSumoLib).value()); |
68 | 63 |
69 // If that fails, see if any system libraries are available. | 64 // If that fails, see if any system libraries are available. |
70 paths[kModuleFfmpegsumo].push_back(module_dir.Append( | 65 paths[kModuleFfmpegsumo].push_back(module_dir.Append( |
71 FILE_PATH_LITERAL(DSO_NAME("avutil", AVUTIL_VERSION))).value()); | 66 FILE_PATH_LITERAL(DSO_NAME("avutil", AVUTIL_VERSION))).value()); |
72 paths[kModuleFfmpegsumo].push_back(module_dir.Append( | 67 paths[kModuleFfmpegsumo].push_back(module_dir.Append( |
73 FILE_PATH_LITERAL(DSO_NAME("avcodec", AVCODEC_VERSION))).value()); | 68 FILE_PATH_LITERAL(DSO_NAME("avcodec", AVCODEC_VERSION))).value()); |
74 paths[kModuleFfmpegsumo].push_back(module_dir.Append( | 69 paths[kModuleFfmpegsumo].push_back(module_dir.Append( |
75 FILE_PATH_LITERAL(DSO_NAME("avformat", AVFORMAT_VERSION))).value()); | 70 FILE_PATH_LITERAL(DSO_NAME("avformat", AVFORMAT_VERSION))).value()); |
76 | 71 |
77 g_media_library_is_initialized = InitializeStubs(paths); | 72 return InitializeStubs(paths); |
78 #endif // !defined(USE_SYSTEM_FFMPEG) | 73 #endif // !defined(USE_SYSTEM_FFMPEG) |
79 return g_media_library_is_initialized; | |
80 } | 74 } |
81 | 75 |
82 bool InitializeMediaLibrary(const base::FilePath& base_path) { | 76 } // namespace internal |
83 static const bool kMediaLibraryInitialized = | |
84 InitializeMediaLibraryInternal(base_path); | |
85 DCHECK_EQ(kMediaLibraryInitialized, g_media_library_is_initialized); | |
86 return kMediaLibraryInitialized; | |
87 } | |
88 | |
89 void InitializeMediaLibraryForTesting() { | |
90 base::FilePath file_path; | |
91 CHECK(PathService::Get(base::DIR_EXE, &file_path)); | |
92 CHECK(InitializeMediaLibrary(file_path)); | |
93 } | |
94 | |
95 bool IsMediaLibraryInitialized() { | |
96 return g_media_library_is_initialized; | |
97 } | |
98 | |
99 } // namespace media | 77 } // namespace media |
OLD | NEW |