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