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 "webrtc/base/basictypes.h"
15 #include "webrtc/base/logging.h"
17 const unsigned char* GetCategoryGroupEnabled(const char* category_group
) {
18 return TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED(category_group
);
21 void AddTraceEvent(char phase
,
22 const unsigned char* category_group_enabled
,
24 unsigned long long id
,
26 const char** arg_names
,
27 const unsigned char* arg_types
,
28 const unsigned long long* arg_values
,
29 unsigned char flags
) {
30 TRACE_EVENT_API_ADD_TRACE_EVENT(phase
, category_group_enabled
, name
, id
,
31 num_args
, arg_names
, arg_types
, arg_values
,
35 // Define webrtc:field_trial::FindFullName to provide webrtc with a field trial
38 namespace field_trial
{
39 std::string
FindFullName(const std::string
& trial_name
) {
40 return base::FieldTrialList::FindFullName(trial_name
);
42 } // namespace field_trial
45 #if defined(LIBPEERCONNECTION_LIB)
47 // libpeerconnection is being compiled as a static lib. In this case
48 // we don't need to do any initializing but to keep things simple we
49 // provide an empty intialization routine so that this #ifdef doesn't
50 // have to be in other places.
51 bool InitializeWebRtcModule() {
52 webrtc::SetupEventTracer(&GetCategoryGroupEnabled
, &AddTraceEvent
);
56 #else // !LIBPEERCONNECTION_LIB
58 // When being compiled as a shared library, we need to bridge the gap between
59 // the current module and the libpeerconnection module, so things get a tad
62 // Global function pointers to the factory functions in the shared library.
63 CreateWebRtcMediaEngineFunction g_create_webrtc_media_engine
= NULL
;
64 DestroyWebRtcMediaEngineFunction g_destroy_webrtc_media_engine
= NULL
;
66 // Returns the full or relative path to the libpeerconnection module depending
67 // on what platform we're on.
68 static base::FilePath
GetLibPeerConnectionPath() {
70 CHECK(PathService::Get(base::DIR_MODULE
, &path
));
72 path
= path
.Append(FILE_PATH_LITERAL("libpeerconnection.dll"));
73 #elif defined(OS_MACOSX)
74 // Simulate '@loader_path/Libraries'.
75 path
= path
.Append(FILE_PATH_LITERAL("Libraries"))
76 .Append(FILE_PATH_LITERAL("libpeerconnection.so"));
77 #elif defined(OS_ANDROID)
78 path
= path
.Append(FILE_PATH_LITERAL("libpeerconnection.so"));
80 path
= path
.Append(FILE_PATH_LITERAL("lib"))
81 .Append(FILE_PATH_LITERAL("libpeerconnection.so"));
86 bool InitializeWebRtcModule() {
87 TRACE_EVENT0("webrtc", "InitializeWebRtcModule");
89 if (g_create_webrtc_media_engine
)
90 return true; // InitializeWebRtcModule has already been called.
92 base::FilePath
path(GetLibPeerConnectionPath());
93 DVLOG(1) << "Loading WebRTC module: " << path
.value();
95 base::NativeLibraryLoadError error
;
96 static base::NativeLibrary lib
= base::LoadNativeLibrary(path
, &error
);
98 // We've been seeing problems on Windows with loading the DLL and we're
99 // not sure exactly why. It could be that AV programs are quarantining the
100 // file or disallowing loading the DLL. To get a better picture of the errors
101 // we're checking these specific error codes.
102 if (error
.code
== ERROR_MOD_NOT_FOUND
) {
103 // It's possible that we get this error due to failure to load other
104 // dependencies, so check first that libpeerconnection actually exists.
105 CHECK(base::PathExists(path
)); // libpeerconnection itself is missing.
106 CHECK(lib
); // If we hit this, a dependency is missing.
107 } else if (error
.code
== ERROR_ACCESS_DENIED
) {
108 CHECK(lib
); // AV blocking access?
112 // Catch-all error handler for all other sorts of errors.
113 CHECK(lib
) << error
.ToString();
115 InitializeModuleFunction initialize_module
=
116 reinterpret_cast<InitializeModuleFunction
>(
117 base::GetFunctionPointerFromNativeLibrary(
118 lib
, "InitializeModule"));
120 // Initialize the proxy by supplying it with a pointer to our
121 // allocator/deallocator routines.
122 // On mac we use malloc zones, which are global, so we provide NULLs for
123 // the alloc/dealloc functions.
124 // PS: This function is actually implemented in allocator_proxy.cc with the
125 // new/delete overrides.
126 InitDiagnosticLoggingDelegateFunctionFunction init_diagnostic_logging
= NULL
;
127 bool init_ok
= initialize_module(*CommandLine::ForCurrentProcess(),
128 #if !defined(OS_MACOSX) && !defined(OS_ANDROID)
132 &webrtc::field_trial::FindFullName
,
133 logging::GetLogMessageHandler(),
134 &GetCategoryGroupEnabled
,
136 &g_create_webrtc_media_engine
,
137 &g_destroy_webrtc_media_engine
,
138 &init_diagnostic_logging
);
141 rtc::SetExtraLoggingInit(init_diagnostic_logging
);
145 cricket::MediaEngineInterface
* CreateWebRtcMediaEngine(
146 webrtc::AudioDeviceModule
* adm
,
147 webrtc::AudioDeviceModule
* adm_sc
,
148 cricket::WebRtcVideoEncoderFactory
* encoder_factory
,
149 cricket::WebRtcVideoDecoderFactory
* decoder_factory
) {
150 // For convenience of tests etc, we call InitializeWebRtcModule here.
151 // For Chrome however, InitializeWebRtcModule must be called
152 // explicitly before the sandbox is initialized. In that case, this call is
153 // effectively a noop.
154 InitializeWebRtcModule();
155 return g_create_webrtc_media_engine(adm
, adm_sc
, encoder_factory
,
159 void DestroyWebRtcMediaEngine(cricket::MediaEngineInterface
* media_engine
) {
160 g_destroy_webrtc_media_engine(media_engine
);
163 #endif // LIBPEERCONNECTION_LIB