ab89d5884adf1ffca2d5b5fe929eb344a212165a
[platform/framework/web/crosswalk.git] / src / third_party / libjingle / overrides / init_webrtc.cc
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.
4
5 #include "init_webrtc.h"
6
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"
16
17 const unsigned char* GetCategoryGroupEnabled(const char* category_group) {
18   return TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED(category_group);
19 }
20
21 void AddTraceEvent(char phase,
22                    const unsigned char* category_group_enabled,
23                    const char* name,
24                    unsigned long long id,
25                    int num_args,
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,
32                                   NULL, flags);
33 }
34
35 // Define webrtc:field_trial::FindFullName to provide webrtc with a field trial
36 // implementation.
37 namespace webrtc {
38 namespace field_trial {
39 std::string FindFullName(const std::string& trial_name) {
40   return base::FieldTrialList::FindFullName(trial_name);
41 }
42 }  // namespace field_trial
43 }  // namespace webrtc
44
45 #if defined(LIBPEERCONNECTION_LIB)
46
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);
53   return true;
54 }
55
56 #else  // !LIBPEERCONNECTION_LIB
57
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
60 // more complicated.
61
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;
65
66 // Returns the full or relative path to the libpeerconnection module depending
67 // on what platform we're on.
68 static base::FilePath GetLibPeerConnectionPath() {
69   base::FilePath path;
70   CHECK(PathService::Get(base::DIR_MODULE, &path));
71 #if defined(OS_WIN)
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"));
79 #else
80   path = path.Append(FILE_PATH_LITERAL("lib"))
81              .Append(FILE_PATH_LITERAL("libpeerconnection.so"));
82 #endif
83   return path;
84 }
85
86 bool InitializeWebRtcModule() {
87   TRACE_EVENT0("webrtc", "InitializeWebRtcModule");
88
89   if (g_create_webrtc_media_engine)
90     return true;  // InitializeWebRtcModule has already been called.
91
92   base::FilePath path(GetLibPeerConnectionPath());
93   DVLOG(1) << "Loading WebRTC module: " << path.value();
94
95   base::NativeLibraryLoadError error;
96   static base::NativeLibrary lib = base::LoadNativeLibrary(path, &error);
97 #if defined(OS_WIN)
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?
109   }
110 #endif
111
112   // Catch-all error handler for all other sorts of errors.
113   CHECK(lib) << error.ToString();
114
115   InitializeModuleFunction initialize_module =
116       reinterpret_cast<InitializeModuleFunction>(
117           base::GetFunctionPointerFromNativeLibrary(
118               lib, "InitializeModule"));
119
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)
129                                    &Allocate,
130                                    &Dellocate,
131 #endif
132                                    &webrtc::field_trial::FindFullName,
133                                    logging::GetLogMessageHandler(),
134                                    &GetCategoryGroupEnabled,
135                                    &AddTraceEvent,
136                                    &g_create_webrtc_media_engine,
137                                    &g_destroy_webrtc_media_engine,
138                                    &init_diagnostic_logging);
139
140   if (init_ok)
141     rtc::SetExtraLoggingInit(init_diagnostic_logging);
142   return init_ok;
143 }
144
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,
156       decoder_factory);
157 }
158
159 void DestroyWebRtcMediaEngine(cricket::MediaEngineInterface* media_engine) {
160   g_destroy_webrtc_media_engine(media_engine);
161 }
162
163 #endif  // LIBPEERCONNECTION_LIB