init_webrtc.cc revision eb525c5499e34cc9c4b825d6d9e75bb07cc06ace
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/native_library.h"
11#include "base/path_service.h"
12#include "talk/base/basictypes.h"
13
14const unsigned char* GetCategoryGroupEnabled(const char* category_group) {
15  return TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED(category_group);
16}
17
18void AddTraceEvent(char phase,
19                   const unsigned char* category_group_enabled,
20                   const char* name,
21                   unsigned long long id,
22                   int num_args,
23                   const char** arg_names,
24                   const unsigned char* arg_types,
25                   const unsigned long long* arg_values,
26                   unsigned char flags) {
27  TRACE_EVENT_API_ADD_TRACE_EVENT(phase, category_group_enabled, name, id,
28                                  num_args, arg_names, arg_types, arg_values,
29                                  NULL, flags);
30}
31
32#if defined(LIBPEERCONNECTION_LIB)
33
34// libpeerconnection is being compiled as a static lib.  In this case
35// we don't need to do any initializing but to keep things simple we
36// provide an empty intialization routine so that this #ifdef doesn't
37// have to be in other places.
38bool InitializeWebRtcModule() {
39  webrtc::SetupEventTracer(&GetCategoryGroupEnabled, &AddTraceEvent);
40  return true;
41}
42
43#else  // !LIBPEERCONNECTION_LIB
44
45// When being compiled as a shared library, we need to bridge the gap between
46// the current module and the libpeerconnection module, so things get a tad
47// more complicated.
48
49// Global function pointers to the factory functions in the shared library.
50CreateWebRtcMediaEngineFunction g_create_webrtc_media_engine = NULL;
51DestroyWebRtcMediaEngineFunction g_destroy_webrtc_media_engine = NULL;
52
53// Returns the full or relative path to the libpeerconnection module depending
54// on what platform we're on.
55static base::FilePath GetLibPeerConnectionPath() {
56  base::FilePath path;
57  CHECK(PathService::Get(base::DIR_MODULE, &path));
58#if defined(OS_WIN)
59  path = path.Append(FILE_PATH_LITERAL("libpeerconnection.dll"));
60#elif defined(OS_MACOSX)
61  // Simulate '@loader_path/Libraries'.
62  path = path.Append(FILE_PATH_LITERAL("Libraries"))
63             .Append(FILE_PATH_LITERAL("libpeerconnection.so"));
64#elif defined(OS_ANDROID)
65  path = path.Append(FILE_PATH_LITERAL("libpeerconnection.so"));
66#else
67  path = path.Append(FILE_PATH_LITERAL("lib"))
68             .Append(FILE_PATH_LITERAL("libpeerconnection.so"));
69#endif
70  return path;
71}
72
73bool InitializeWebRtcModule() {
74  if (g_create_webrtc_media_engine)
75    return true;  // InitializeWebRtcModule has already been called.
76
77  base::FilePath path(GetLibPeerConnectionPath());
78  DVLOG(1) << "Loading WebRTC module: " << path.value();
79
80  std::string error;
81  static base::NativeLibrary lib =
82      base::LoadNativeLibrary(path, &error);
83  CHECK(lib) << error;
84
85  InitializeModuleFunction initialize_module =
86      reinterpret_cast<InitializeModuleFunction>(
87          base::GetFunctionPointerFromNativeLibrary(
88              lib, "InitializeModule"));
89
90  // Initialize the proxy by supplying it with a pointer to our
91  // allocator/deallocator routines.
92  // On mac we use malloc zones, which are global, so we provide NULLs for
93  // the alloc/dealloc functions.
94  // PS: This function is actually implemented in allocator_proxy.cc with the
95  // new/delete overrides.
96  return initialize_module(*CommandLine::ForCurrentProcess(),
97#if !defined(OS_MACOSX) && !defined(OS_ANDROID)
98      &Allocate, &Dellocate,
99#endif
100      logging::GetLogMessageHandler(),
101      &GetCategoryGroupEnabled, &AddTraceEvent,
102      &g_create_webrtc_media_engine, &g_destroy_webrtc_media_engine);
103}
104
105cricket::MediaEngineInterface* CreateWebRtcMediaEngine(
106    webrtc::AudioDeviceModule* adm,
107    webrtc::AudioDeviceModule* adm_sc,
108    cricket::WebRtcVideoEncoderFactory* encoder_factory,
109    cricket::WebRtcVideoDecoderFactory* decoder_factory) {
110  // For convenience of tests etc, we call InitializeWebRtcModule here.
111  // For Chrome however, InitializeWebRtcModule must be called
112  // explicitly before the sandbox is initialized.  In that case, this call is
113  // effectively a noop.
114  InitializeWebRtcModule();
115  return g_create_webrtc_media_engine(adm, adm_sc, encoder_factory,
116      decoder_factory);
117}
118
119void DestroyWebRtcMediaEngine(cricket::MediaEngineInterface* media_engine) {
120  g_destroy_webrtc_media_engine(media_engine);
121}
122
123#endif  // LIBPEERCONNECTION_LIB
124