| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "init_webrtc.h" | 5 #include "init_webrtc.h" |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
| 9 #include "base/files/file_util.h" | 9 #include "base/files/file_util.h" |
| 10 #include "base/logging.h" |
| 10 #include "base/metrics/field_trial.h" | 11 #include "base/metrics/field_trial.h" |
| 11 #include "base/metrics/histogram.h" | 12 #include "base/metrics/histogram.h" |
| 12 #include "base/native_library.h" | 13 #include "base/native_library.h" |
| 13 #include "base/path_service.h" | 14 #include "base/path_service.h" |
| 14 #include "base/trace_event/trace_event.h" | 15 #include "base/trace_event/trace_event.h" |
| 15 #include "third_party/webrtc/overrides/webrtc/base/basictypes.h" | 16 #include "third_party/webrtc/overrides/webrtc/base/basictypes.h" |
| 16 #include "third_party/webrtc/overrides/webrtc/base/logging.h" | 17 #include "third_party/webrtc/overrides/webrtc/base/logging.h" |
| 18 #include "third_party/webrtc/system_wrappers/interface/event_tracer.h" |
| 17 | 19 |
| 18 const unsigned char* GetCategoryGroupEnabled(const char* category_group) { | 20 const unsigned char* GetCategoryGroupEnabled(const char* category_group) { |
| 19 return TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED(category_group); | 21 return TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED(category_group); |
| 20 } | 22 } |
| 21 | 23 |
| 22 void AddTraceEvent(char phase, | 24 void AddTraceEvent(char phase, |
| 23 const unsigned char* category_group_enabled, | 25 const unsigned char* category_group_enabled, |
| 24 const char* name, | 26 const char* name, |
| 25 unsigned long long id, | 27 unsigned long long id, |
| 26 int num_args, | 28 int num_args, |
| (...skipping 10 matching lines...) Expand all Loading... |
| 37 // Define webrtc::field_trial::FindFullName to provide webrtc with a field trial | 39 // Define webrtc::field_trial::FindFullName to provide webrtc with a field trial |
| 38 // implementation. | 40 // implementation. |
| 39 namespace field_trial { | 41 namespace field_trial { |
| 40 std::string FindFullName(const std::string& trial_name) { | 42 std::string FindFullName(const std::string& trial_name) { |
| 41 return base::FieldTrialList::FindFullName(trial_name); | 43 return base::FieldTrialList::FindFullName(trial_name); |
| 42 } | 44 } |
| 43 } // namespace field_trial | 45 } // namespace field_trial |
| 44 | 46 |
| 45 // Define webrtc::metrics functions to provide webrtc with implementations. | 47 // Define webrtc::metrics functions to provide webrtc with implementations. |
| 46 namespace metrics { | 48 namespace metrics { |
| 49 |
| 50 // This class doesn't actually exist, so don't go looking for it :) |
| 51 // This type is just fwd declared here in order to use it as an opaque type |
| 52 // between the Histogram functions in this file. |
| 53 class Histogram; |
| 54 |
| 47 Histogram* HistogramFactoryGetCounts( | 55 Histogram* HistogramFactoryGetCounts( |
| 48 const std::string& name, int min, int max, int bucket_count) { | 56 const std::string& name, int min, int max, int bucket_count) { |
| 49 return reinterpret_cast<Histogram*>( | 57 return reinterpret_cast<Histogram*>( |
| 50 base::Histogram::FactoryGet(name, min, max, bucket_count, | 58 base::Histogram::FactoryGet(name, min, max, bucket_count, |
| 51 base::HistogramBase::kUmaTargetedHistogramFlag)); | 59 base::HistogramBase::kUmaTargetedHistogramFlag)); |
| 52 } | 60 } |
| 53 | 61 |
| 54 Histogram* HistogramFactoryGetEnumeration( | 62 Histogram* HistogramFactoryGetEnumeration( |
| 55 const std::string& name, int boundary) { | 63 const std::string& name, int boundary) { |
| 56 return reinterpret_cast<Histogram*>( | 64 return reinterpret_cast<Histogram*>( |
| 57 base::LinearHistogram::FactoryGet(name, 1, boundary, boundary + 1, | 65 base::LinearHistogram::FactoryGet(name, 1, boundary, boundary + 1, |
| 58 base::HistogramBase::kUmaTargetedHistogramFlag)); | 66 base::HistogramBase::kUmaTargetedHistogramFlag)); |
| 59 } | 67 } |
| 60 | 68 |
| 61 void HistogramAdd( | 69 void HistogramAdd( |
| 62 Histogram* histogram_pointer, const std::string& name, int sample) { | 70 Histogram* histogram_pointer, const std::string& name, int sample) { |
| 63 base::HistogramBase* ptr = | 71 base::HistogramBase* ptr = |
| 64 reinterpret_cast<base::HistogramBase*>(histogram_pointer); | 72 reinterpret_cast<base::HistogramBase*>(histogram_pointer); |
| 65 // The name should not vary. | 73 // The name should not vary. |
| 66 DCHECK(ptr->histogram_name() == name); | 74 DCHECK(ptr->histogram_name() == name); |
| 67 ptr->Add(sample); | 75 ptr->Add(sample); |
| 68 } | 76 } |
| 69 } // namespace metrics | 77 } // namespace metrics |
| 70 } // namespace webrtc | 78 } // namespace webrtc |
| 71 | 79 |
| 72 #if defined(LIBPEERCONNECTION_LIB) | |
| 73 | |
| 74 // libpeerconnection is being compiled as a static lib. In this case | 80 // libpeerconnection is being compiled as a static lib. In this case |
| 75 // we don't need to do any initializing but to keep things simple we | 81 // we don't need to do any initializing but to keep things simple we |
| 76 // provide an empty intialization routine so that this #ifdef doesn't | 82 // provide an empty intialization routine so that this #ifdef doesn't |
| 77 // have to be in other places. | 83 // have to be in other places. |
| 78 bool InitializeWebRtcModule() { | 84 bool InitializeWebRtcModule() { |
| 79 webrtc::SetupEventTracer(&GetCategoryGroupEnabled, &AddTraceEvent); | 85 webrtc::SetupEventTracer(&GetCategoryGroupEnabled, &AddTraceEvent); |
| 80 return true; | 86 return true; |
| 81 } | 87 } |
| 82 | |
| 83 #else // !LIBPEERCONNECTION_LIB | |
| 84 | |
| 85 // When being compiled as a shared library, we need to bridge the gap between | |
| 86 // the current module and the libpeerconnection module, so things get a tad | |
| 87 // more complicated. | |
| 88 | |
| 89 // Global function pointers to the factory functions in the shared library. | |
| 90 CreateWebRtcMediaEngineFunction g_create_webrtc_media_engine = NULL; | |
| 91 DestroyWebRtcMediaEngineFunction g_destroy_webrtc_media_engine = NULL; | |
| 92 | |
| 93 // Returns the full or relative path to the libpeerconnection module depending | |
| 94 // on what platform we're on. | |
| 95 static base::FilePath GetLibPeerConnectionPath() { | |
| 96 base::FilePath path; | |
| 97 CHECK(PathService::Get(base::DIR_MODULE, &path)); | |
| 98 #if defined(OS_WIN) | |
| 99 path = path.Append(FILE_PATH_LITERAL("libpeerconnection.dll")); | |
| 100 #elif defined(OS_MACOSX) | |
| 101 // Simulate '@loader_path/Libraries'. | |
| 102 path = path.Append(FILE_PATH_LITERAL("Libraries")) | |
| 103 .Append(FILE_PATH_LITERAL("libpeerconnection.so")); | |
| 104 #elif defined(OS_ANDROID) | |
| 105 path = path.Append(FILE_PATH_LITERAL("libpeerconnection.so")); | |
| 106 #else | |
| 107 path = path.Append(FILE_PATH_LITERAL("lib")) | |
| 108 .Append(FILE_PATH_LITERAL("libpeerconnection.so")); | |
| 109 #endif | |
| 110 return path; | |
| 111 } | |
| 112 | |
| 113 bool InitializeWebRtcModule() { | |
| 114 TRACE_EVENT0("webrtc", "InitializeWebRtcModule"); | |
| 115 | |
| 116 if (g_create_webrtc_media_engine) | |
| 117 return true; // InitializeWebRtcModule has already been called. | |
| 118 | |
| 119 base::FilePath path(GetLibPeerConnectionPath()); | |
| 120 DVLOG(1) << "Loading WebRTC module: " << path.value(); | |
| 121 | |
| 122 base::NativeLibraryLoadError error; | |
| 123 static base::NativeLibrary lib = base::LoadNativeLibrary(path, &error); | |
| 124 #if defined(OS_WIN) | |
| 125 // We've been seeing problems on Windows with loading the DLL and we're | |
| 126 // not sure exactly why. It could be that AV programs are quarantining the | |
| 127 // file or disallowing loading the DLL. To get a better picture of the errors | |
| 128 // we're checking these specific error codes. | |
| 129 if (error.code == ERROR_MOD_NOT_FOUND) { | |
| 130 // It's possible that we get this error due to failure to load other | |
| 131 // dependencies, so check first that libpeerconnection actually exists. | |
| 132 CHECK(base::PathExists(path)); // libpeerconnection itself is missing. | |
| 133 CHECK(lib); // If we hit this, a dependency is missing. | |
| 134 } else if (error.code == ERROR_ACCESS_DENIED) { | |
| 135 CHECK(lib); // AV blocking access? | |
| 136 } | |
| 137 #endif | |
| 138 | |
| 139 // Catch-all error handler for all other sorts of errors. | |
| 140 CHECK(lib) << error.ToString(); | |
| 141 | |
| 142 InitializeModuleFunction initialize_module = | |
| 143 reinterpret_cast<InitializeModuleFunction>( | |
| 144 base::GetFunctionPointerFromNativeLibrary( | |
| 145 lib, "InitializeModule")); | |
| 146 | |
| 147 // Initialize the proxy by supplying it with a pointer to our | |
| 148 // allocator/deallocator routines. | |
| 149 // On mac we use malloc zones, which are global, so we provide NULLs for | |
| 150 // the alloc/dealloc functions. | |
| 151 // PS: This function is actually implemented in allocator_proxy.cc with the | |
| 152 // new/delete overrides. | |
| 153 InitDiagnosticLoggingDelegateFunctionFunction init_diagnostic_logging = NULL; | |
| 154 bool init_ok = initialize_module(*base::CommandLine::ForCurrentProcess(), | |
| 155 #if !defined(OS_MACOSX) && !defined(OS_ANDROID) | |
| 156 &Allocate, | |
| 157 &Dellocate, | |
| 158 #endif | |
| 159 &webrtc::field_trial::FindFullName, | |
| 160 &webrtc::metrics::HistogramFactoryGetCounts, | |
| 161 &webrtc::metrics::HistogramFactoryGetEnumeration, | |
| 162 &webrtc::metrics::HistogramAdd, | |
| 163 logging::GetLogMessageHandler(), | |
| 164 &GetCategoryGroupEnabled, | |
| 165 &AddTraceEvent, | |
| 166 &g_create_webrtc_media_engine, | |
| 167 &g_destroy_webrtc_media_engine, | |
| 168 &init_diagnostic_logging); | |
| 169 | |
| 170 if (init_ok) | |
| 171 rtc::SetExtraLoggingInit(init_diagnostic_logging); | |
| 172 return init_ok; | |
| 173 } | |
| 174 | |
| 175 cricket::MediaEngineInterface* CreateWebRtcMediaEngine( | |
| 176 webrtc::AudioDeviceModule* adm, | |
| 177 webrtc::AudioDeviceModule* adm_sc, | |
| 178 cricket::WebRtcVideoEncoderFactory* encoder_factory, | |
| 179 cricket::WebRtcVideoDecoderFactory* decoder_factory) { | |
| 180 // For convenience of tests etc, we call InitializeWebRtcModule here. | |
| 181 // For Chrome however, InitializeWebRtcModule must be called | |
| 182 // explicitly before the sandbox is initialized. In that case, this call is | |
| 183 // effectively a noop. | |
| 184 InitializeWebRtcModule(); | |
| 185 return g_create_webrtc_media_engine(adm, adm_sc, encoder_factory, | |
| 186 decoder_factory); | |
| 187 } | |
| 188 | |
| 189 void DestroyWebRtcMediaEngine(cricket::MediaEngineInterface* media_engine) { | |
| 190 g_destroy_webrtc_media_engine(media_engine); | |
| 191 } | |
| 192 | |
| 193 #endif // LIBPEERCONNECTION_LIB | |
| OLD | NEW |