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.
5 #include "init_webrtc.h"
7 #include "base/command_line.h"
8 #include "base/debug/trace_event.h"
9 #include "base/files/file_path.h"
10 #include "base/files/file_util.h"
11 #include "base/metrics/field_trial.h"
12 #include "base/native_library.h"
13 #include "base/path_service.h"
14 #include "third_party/webrtc/common.h"
15 #include "third_party/webrtc/modules/audio_processing/include/audio_processing.h"
16 #include "webrtc/base/basictypes.h"
17 #include "webrtc/base/logging.h"
19 const unsigned char* GetCategoryGroupEnabled(const char* category_group) {
20 return TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED(category_group);
23 void AddTraceEvent(char phase,
24 const unsigned char* category_group_enabled,
26 unsigned long long id,
28 const char** arg_names,
29 const unsigned char* arg_types,
30 const unsigned long long* arg_values,
31 unsigned char flags) {
32 TRACE_EVENT_API_ADD_TRACE_EVENT(phase, category_group_enabled, name, id,
33 num_args, arg_names, arg_types, arg_values,
37 // Define webrtc:field_trial::FindFullName to provide webrtc with a field trial
40 namespace field_trial {
41 std::string FindFullName(const std::string& trial_name) {
42 return base::FieldTrialList::FindFullName(trial_name);
44 } // namespace field_trial
47 #if defined(LIBPEERCONNECTION_LIB)
49 // libpeerconnection is being compiled as a static lib. In this case
50 // we don't need to do any initializing but to keep things simple we
51 // provide an empty intialization routine so that this #ifdef doesn't
52 // have to be in other places.
53 bool InitializeWebRtcModule() {
54 webrtc::SetupEventTracer(&GetCategoryGroupEnabled, &AddTraceEvent);
58 webrtc::AudioProcessing* CreateWebRtcAudioProcessing(
59 const webrtc::Config& config) {
60 // libpeerconnection is being compiled as a static lib, use
61 // webrtc::AudioProcessing directly.
62 return webrtc::AudioProcessing::Create(config);
65 #else // !LIBPEERCONNECTION_LIB
67 // When being compiled as a shared library, we need to bridge the gap between
68 // the current module and the libpeerconnection module, so things get a tad
71 // Global function pointers to the factory functions in the shared library.
72 CreateWebRtcMediaEngineFunction g_create_webrtc_media_engine = NULL;
73 DestroyWebRtcMediaEngineFunction g_destroy_webrtc_media_engine = NULL;
74 CreateWebRtcAudioProcessingFunction g_create_webrtc_audio_processing = NULL;
76 // Returns the full or relative path to the libpeerconnection module depending
77 // on what platform we're on.
78 static base::FilePath GetLibPeerConnectionPath() {
80 CHECK(PathService::Get(base::DIR_MODULE, &path));
82 path = path.Append(FILE_PATH_LITERAL("libpeerconnection.dll"));
83 #elif defined(OS_MACOSX)
84 // Simulate '@loader_path/Libraries'.
85 path = path.Append(FILE_PATH_LITERAL("Libraries"))
86 .Append(FILE_PATH_LITERAL("libpeerconnection.so"));
87 #elif defined(OS_ANDROID)
88 path = path.Append(FILE_PATH_LITERAL("libpeerconnection.so"));
90 path = path.Append(FILE_PATH_LITERAL("lib"))
91 .Append(FILE_PATH_LITERAL("libpeerconnection.so"));
96 bool InitializeWebRtcModule() {
97 TRACE_EVENT0("webrtc", "InitializeWebRtcModule");
99 if (g_create_webrtc_media_engine)
100 return true; // InitializeWebRtcModule has already been called.
102 base::FilePath path(GetLibPeerConnectionPath());
103 DVLOG(1) << "Loading WebRTC module: " << path.value();
105 base::NativeLibraryLoadError error;
106 static base::NativeLibrary lib = base::LoadNativeLibrary(path, &error);
108 // We've been seeing problems on Windows with loading the DLL and we're
109 // not sure exactly why. It could be that AV programs are quarantining the
110 // file or disallowing loading the DLL. To get a better picture of the errors
111 // we're checking these specific error codes.
112 if (error.code == ERROR_MOD_NOT_FOUND) {
113 // It's possible that we get this error due to failure to load other
114 // dependencies, so check first that libpeerconnection actually exists.
115 CHECK(base::PathExists(path)); // libpeerconnection itself is missing.
116 CHECK(lib); // If we hit this, a dependency is missing.
117 } else if (error.code == ERROR_ACCESS_DENIED) {
118 CHECK(lib); // AV blocking access?
122 // Catch-all error handler for all other sorts of errors.
123 CHECK(lib) << error.ToString();
125 InitializeModuleFunction initialize_module =
126 reinterpret_cast<InitializeModuleFunction>(
127 base::GetFunctionPointerFromNativeLibrary(
128 lib, "InitializeModule"));
130 // Initialize the proxy by supplying it with a pointer to our
131 // allocator/deallocator routines.
132 // On mac we use malloc zones, which are global, so we provide NULLs for
133 // the alloc/dealloc functions.
134 // PS: This function is actually implemented in allocator_proxy.cc with the
135 // new/delete overrides.
136 InitDiagnosticLoggingDelegateFunctionFunction init_diagnostic_logging = NULL;
137 bool init_ok = initialize_module(*CommandLine::ForCurrentProcess(),
138 #if !defined(OS_MACOSX) && !defined(OS_ANDROID)
142 &webrtc::field_trial::FindFullName,
143 logging::GetLogMessageHandler(),
144 &GetCategoryGroupEnabled,
146 &g_create_webrtc_media_engine,
147 &g_destroy_webrtc_media_engine,
148 &init_diagnostic_logging,
149 &g_create_webrtc_audio_processing);
151 rtc::SetExtraLoggingInit(init_diagnostic_logging);
155 cricket::MediaEngineInterface* CreateWebRtcMediaEngine(
156 webrtc::AudioDeviceModule* adm,
157 webrtc::AudioDeviceModule* adm_sc,
158 cricket::WebRtcVideoEncoderFactory* encoder_factory,
159 cricket::WebRtcVideoDecoderFactory* decoder_factory) {
160 // For convenience of tests etc, we call InitializeWebRtcModule here.
161 // For Chrome however, InitializeWebRtcModule must be called
162 // explicitly before the sandbox is initialized. In that case, this call is
163 // effectively a noop.
164 InitializeWebRtcModule();
165 return g_create_webrtc_media_engine(adm, adm_sc, encoder_factory,
169 void DestroyWebRtcMediaEngine(cricket::MediaEngineInterface* media_engine) {
170 g_destroy_webrtc_media_engine(media_engine);
173 webrtc::AudioProcessing* CreateWebRtcAudioProcessing(
174 const webrtc::Config& config) {
175 // The same as CreateWebRtcMediaEngine(), we call InitializeWebRtcModule here
176 // for convenience of tests.
177 InitializeWebRtcModule();
178 return g_create_webrtc_audio_processing(config);
181 #endif // LIBPEERCONNECTION_LIB