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/files/file_path.h"
9 #include "base/files/file_util.h"
10 #include "base/metrics/field_trial.h"
11 #include "base/metrics/histogram.h"
12 #include "base/native_library.h"
13 #include "base/path_service.h"
14 #include "base/trace_event/trace_event.h"
15 #include "webrtc/base/basictypes.h"
16 #include "webrtc/base/logging.h"
18 const unsigned char* GetCategoryGroupEnabled(const char* category_group
) {
19 return TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED(category_group
);
22 void AddTraceEvent(char phase
,
23 const unsigned char* category_group_enabled
,
25 unsigned long long id
,
27 const char** arg_names
,
28 const unsigned char* arg_types
,
29 const unsigned long long* arg_values
,
30 unsigned char flags
) {
31 TRACE_EVENT_API_ADD_TRACE_EVENT(phase
, category_group_enabled
, name
, id
,
32 num_args
, arg_names
, arg_types
, arg_values
,
37 // Define webrtc::field_trial::FindFullName to provide webrtc with a field trial
39 namespace field_trial
{
40 std::string
FindFullName(const std::string
& trial_name
) {
41 return base::FieldTrialList::FindFullName(trial_name
);
43 } // namespace field_trial
45 // Define webrtc::metrics functions to provide webrtc with implementations.
47 Histogram
* HistogramFactoryGetCounts(
48 const std::string
& name
, int min
, int max
, int bucket_count
) {
49 return reinterpret_cast<Histogram
*>(
50 base::Histogram::FactoryGet(name
, min
, max
, bucket_count
,
51 base::HistogramBase::kUmaTargetedHistogramFlag
));
54 Histogram
* HistogramFactoryGetEnumeration(
55 const std::string
& name
, int boundary
) {
56 return reinterpret_cast<Histogram
*>(
57 base::LinearHistogram::FactoryGet(name
, 1, boundary
, boundary
+ 1,
58 base::HistogramBase::kUmaTargetedHistogramFlag
));
62 Histogram
* histogram_pointer
, const std::string
& name
, int sample
) {
63 base::HistogramBase
* ptr
=
64 reinterpret_cast<base::HistogramBase
*>(histogram_pointer
);
65 // The name should not vary.
66 DCHECK(ptr
->histogram_name() == name
);
69 } // namespace metrics
72 #if defined(LIBPEERCONNECTION_LIB)
74 // libpeerconnection is being compiled as a static lib. In this case
75 // we don't need to do any initializing but to keep things simple we
76 // provide an empty intialization routine so that this #ifdef doesn't
77 // have to be in other places.
78 bool InitializeWebRtcModule() {
79 webrtc::SetupEventTracer(&GetCategoryGroupEnabled
, &AddTraceEvent
);
83 #else // !LIBPEERCONNECTION_LIB
85 // When being compiled as a shared library, we need to bridge the gap between
86 // the current module and the libpeerconnection module, so things get a tad
89 // Global function pointers to the factory functions in the shared library.
90 CreateWebRtcMediaEngineFunction g_create_webrtc_media_engine
= NULL
;
91 DestroyWebRtcMediaEngineFunction g_destroy_webrtc_media_engine
= NULL
;
93 // Returns the full or relative path to the libpeerconnection module depending
94 // on what platform we're on.
95 static base::FilePath
GetLibPeerConnectionPath() {
97 CHECK(PathService::Get(base::DIR_MODULE
, &path
));
99 path
= path
.Append(FILE_PATH_LITERAL("libpeerconnection.dll"));
100 #elif defined(OS_MACOSX)
101 // Simulate '@loader_path/Libraries'.
102 path
= path
.Append(FILE_PATH_LITERAL("Libraries"))
103 .Append(FILE_PATH_LITERAL("libpeerconnection.so"));
104 #elif defined(OS_ANDROID)
105 path
= path
.Append(FILE_PATH_LITERAL("libpeerconnection.so"));
107 path
= path
.Append(FILE_PATH_LITERAL("lib"))
108 .Append(FILE_PATH_LITERAL("libpeerconnection.so"));
113 bool InitializeWebRtcModule() {
114 TRACE_EVENT0("webrtc", "InitializeWebRtcModule");
116 if (g_create_webrtc_media_engine
)
117 return true; // InitializeWebRtcModule has already been called.
119 base::FilePath
path(GetLibPeerConnectionPath());
120 DVLOG(1) << "Loading WebRTC module: " << path
.value();
122 base::NativeLibraryLoadError error
;
123 static base::NativeLibrary lib
= base::LoadNativeLibrary(path
, &error
);
125 // We've been seeing problems on Windows with loading the DLL and we're
126 // not sure exactly why. It could be that AV programs are quarantining the
127 // file or disallowing loading the DLL. To get a better picture of the errors
128 // we're checking these specific error codes.
129 if (error
.code
== ERROR_MOD_NOT_FOUND
) {
130 // It's possible that we get this error due to failure to load other
131 // dependencies, so check first that libpeerconnection actually exists.
132 CHECK(base::PathExists(path
)); // libpeerconnection itself is missing.
133 CHECK(lib
); // If we hit this, a dependency is missing.
134 } else if (error
.code
== ERROR_ACCESS_DENIED
) {
135 CHECK(lib
); // AV blocking access?
139 // Catch-all error handler for all other sorts of errors.
140 CHECK(lib
) << error
.ToString();
142 InitializeModuleFunction initialize_module
=
143 reinterpret_cast<InitializeModuleFunction
>(
144 base::GetFunctionPointerFromNativeLibrary(
145 lib
, "InitializeModule"));
147 // Initialize the proxy by supplying it with a pointer to our
148 // allocator/deallocator routines.
149 // On mac we use malloc zones, which are global, so we provide NULLs for
150 // the alloc/dealloc functions.
151 // PS: This function is actually implemented in allocator_proxy.cc with the
152 // new/delete overrides.
153 InitDiagnosticLoggingDelegateFunctionFunction init_diagnostic_logging
= NULL
;
154 bool init_ok
= initialize_module(*base::CommandLine::ForCurrentProcess(),
155 #if !defined(OS_MACOSX) && !defined(OS_ANDROID)
159 &webrtc::field_trial::FindFullName
,
160 &webrtc::metrics::HistogramFactoryGetCounts
,
161 &webrtc::metrics::HistogramFactoryGetEnumeration
,
162 &webrtc::metrics::HistogramAdd
,
163 logging::GetLogMessageHandler(),
164 &GetCategoryGroupEnabled
,
166 &g_create_webrtc_media_engine
,
167 &g_destroy_webrtc_media_engine
,
168 &init_diagnostic_logging
);
171 rtc::SetExtraLoggingInit(init_diagnostic_logging
);
175 cricket::MediaEngineInterface
* CreateWebRtcMediaEngine(
176 webrtc::AudioDeviceModule
* adm
,
177 webrtc::AudioDeviceModule
* adm_sc
,
178 cricket::WebRtcVideoEncoderFactory
* encoder_factory
,
179 cricket::WebRtcVideoDecoderFactory
* decoder_factory
) {
180 // For convenience of tests etc, we call InitializeWebRtcModule here.
181 // For Chrome however, InitializeWebRtcModule must be called
182 // explicitly before the sandbox is initialized. In that case, this call is
183 // effectively a noop.
184 InitializeWebRtcModule();
185 return g_create_webrtc_media_engine(adm
, adm_sc
, encoder_factory
,
189 void DestroyWebRtcMediaEngine(cricket::MediaEngineInterface
* media_engine
) {
190 g_destroy_webrtc_media_engine(media_engine
);
193 #endif // LIBPEERCONNECTION_LIB