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/file_util.h"
10 #include "base/files/file_path.h"
11 #include "base/native_library.h"
12 #include "base/path_service.h"
13 #include "talk/base/basictypes.h"
14 #include "third_party/libjingle/overrides/talk/base/logging.h"
16 const unsigned char* GetCategoryGroupEnabled(const char* category_group
) {
17 return TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED(category_group
);
20 void AddTraceEvent(char phase
,
21 const unsigned char* category_group_enabled
,
23 unsigned long long id
,
25 const char** arg_names
,
26 const unsigned char* arg_types
,
27 const unsigned long long* arg_values
,
28 unsigned char flags
) {
29 TRACE_EVENT_API_ADD_TRACE_EVENT(phase
, category_group_enabled
, name
, id
,
30 num_args
, arg_names
, arg_types
, arg_values
,
34 #if defined(LIBPEERCONNECTION_LIB)
36 // libpeerconnection is being compiled as a static lib. In this case
37 // we don't need to do any initializing but to keep things simple we
38 // provide an empty intialization routine so that this #ifdef doesn't
39 // have to be in other places.
40 bool InitializeWebRtcModule() {
41 webrtc::SetupEventTracer(&GetCategoryGroupEnabled
, &AddTraceEvent
);
45 #else // !LIBPEERCONNECTION_LIB
47 // When being compiled as a shared library, we need to bridge the gap between
48 // the current module and the libpeerconnection module, so things get a tad
51 // Global function pointers to the factory functions in the shared library.
52 CreateWebRtcMediaEngineFunction g_create_webrtc_media_engine
= NULL
;
53 DestroyWebRtcMediaEngineFunction g_destroy_webrtc_media_engine
= NULL
;
55 // Returns the full or relative path to the libpeerconnection module depending
56 // on what platform we're on.
57 static base::FilePath
GetLibPeerConnectionPath() {
59 CHECK(PathService::Get(base::DIR_MODULE
, &path
));
61 path
= path
.Append(FILE_PATH_LITERAL("libpeerconnection.dll"));
62 #elif defined(OS_MACOSX)
63 // Simulate '@loader_path/Libraries'.
64 path
= path
.Append(FILE_PATH_LITERAL("Libraries"))
65 .Append(FILE_PATH_LITERAL("libpeerconnection.so"));
66 #elif defined(OS_ANDROID)
67 path
= path
.Append(FILE_PATH_LITERAL("libpeerconnection.so"));
69 path
= path
.Append(FILE_PATH_LITERAL("lib"))
70 .Append(FILE_PATH_LITERAL("libpeerconnection.so"));
75 bool InitializeWebRtcModule() {
76 TRACE_EVENT0("webrtc", "InitializeWebRtcModule");
78 if (g_create_webrtc_media_engine
)
79 return true; // InitializeWebRtcModule has already been called.
81 base::FilePath
path(GetLibPeerConnectionPath());
82 DVLOG(1) << "Loading WebRTC module: " << path
.value();
84 base::NativeLibraryLoadError error
;
85 static base::NativeLibrary lib
= base::LoadNativeLibrary(path
, &error
);
87 // We've been seeing problems on Windows with loading the DLL and we're
88 // not sure exactly why. It could be that AV programs are quarantining the
89 // file or disallowing loading the DLL. To get a better picture of the errors
90 // we're checking these specific error codes.
91 if (error
.code
== ERROR_MOD_NOT_FOUND
) {
92 // It's possible that we get this error due to failure to load other
93 // dependencies, so check first that libpeerconnection actually exists.
94 CHECK(base::PathExists(path
)); // libpeerconnection itself is missing.
95 CHECK(lib
); // If we hit this, a dependency is missing.
96 } else if (error
.code
== ERROR_ACCESS_DENIED
) {
97 CHECK(lib
); // AV blocking access?
101 // Catch-all error handler for all other sorts of errors.
102 CHECK(lib
) << error
.ToString();
104 InitializeModuleFunction initialize_module
=
105 reinterpret_cast<InitializeModuleFunction
>(
106 base::GetFunctionPointerFromNativeLibrary(
107 lib
, "InitializeModule"));
109 // Initialize the proxy by supplying it with a pointer to our
110 // allocator/deallocator routines.
111 // On mac we use malloc zones, which are global, so we provide NULLs for
112 // the alloc/dealloc functions.
113 // PS: This function is actually implemented in allocator_proxy.cc with the
114 // new/delete overrides.
115 InitDiagnosticLoggingDelegateFunctionFunction init_diagnostic_logging
= NULL
;
116 bool init_ok
= initialize_module(*CommandLine::ForCurrentProcess(),
117 #if !defined(OS_MACOSX) && !defined(OS_ANDROID)
118 &Allocate
, &Dellocate
,
120 logging::GetLogMessageHandler(),
121 &GetCategoryGroupEnabled
, &AddTraceEvent
,
122 &g_create_webrtc_media_engine
, &g_destroy_webrtc_media_engine
,
123 &init_diagnostic_logging
);
126 talk_base::SetExtraLoggingInit(init_diagnostic_logging
);
130 cricket::MediaEngineInterface
* CreateWebRtcMediaEngine(
131 webrtc::AudioDeviceModule
* adm
,
132 webrtc::AudioDeviceModule
* adm_sc
,
133 cricket::WebRtcVideoEncoderFactory
* encoder_factory
,
134 cricket::WebRtcVideoDecoderFactory
* decoder_factory
) {
135 // For convenience of tests etc, we call InitializeWebRtcModule here.
136 // For Chrome however, InitializeWebRtcModule must be called
137 // explicitly before the sandbox is initialized. In that case, this call is
138 // effectively a noop.
139 InitializeWebRtcModule();
140 return g_create_webrtc_media_engine(adm
, adm_sc
, encoder_factory
,
144 void DestroyWebRtcMediaEngine(cricket::MediaEngineInterface
* media_engine
) {
145 g_destroy_webrtc_media_engine(media_engine
);
148 #endif // LIBPEERCONNECTION_LIB