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 #if defined(OS_WIN) && defined(ARCH_CPU_X86_64)
6 #include <math.h> // needed for _set_FMA3_enable
7 #endif // WIN && ARCH_CPU_X86_64
9 #include "allocator_shim/allocator_stub.h"
10 #include "base/command_line.h"
11 #include "base/files/file_path.h"
12 #include "base/logging.h"
13 #include "init_webrtc.h"
14 #include "talk/media/webrtc/webrtcmediaengine.h"
15 #include "webrtc/base/basictypes.h"
16 #include "webrtc/base/logging.h"
18 #if !defined(LIBPEERCONNECTION_IMPLEMENTATION) || defined(LIBPEERCONNECTION_LIB)
19 #error "Only compile the allocator proxy with the shared_library implementation"
23 #define ALLOC_EXPORT __declspec(dllexport)
25 #define ALLOC_EXPORT __attribute__((visibility("default")))
28 #if !defined(OS_MACOSX) && !defined(OS_ANDROID)
29 // These are used by our new/delete overrides in
30 // allocator_shim/allocator_proxy.cc
31 AllocateFunction g_alloc
= NULL
;
32 DellocateFunction g_dealloc
= NULL
;
35 // Forward declare of the libjingle internal factory and destroy methods for the
36 // WebRTC media engine.
37 cricket::MediaEngineInterface
* CreateWebRtcMediaEngine(
38 webrtc::AudioDeviceModule
* adm
,
39 webrtc::AudioDeviceModule
* adm_sc
,
40 cricket::WebRtcVideoEncoderFactory
* encoder_factory
,
41 cricket::WebRtcVideoDecoderFactory
* decoder_factory
);
43 void DestroyWebRtcMediaEngine(cricket::MediaEngineInterface
* media_engine
);
46 // Provide webrtc with a field trial and metrics implementations.
47 // The implementations are provided by the loader via the InitializeModule.
49 // Defines webrtc::field_trial::FindFullName.
50 FieldTrialFindFullName g_field_trial_find_
= NULL
;
51 // Defines webrtc::metrics::RtcFactoryGetCounts.
52 RtcHistogramFactoryGetCounts g_factory_get_counts
= NULL
;
53 // Defines webrtc::metrics::RtcFactoryGetEnumeration.
54 RtcHistogramFactoryGetEnumeration g_factory_get_enumeration
= NULL
;
55 // Defines webrtc::metrics::RtcAdd.
56 RtcHistogramAdd g_histogram_add
= NULL
;
60 namespace field_trial
{
61 std::string
FindFullName(const std::string
& trial_name
) {
62 return g_field_trial_find_(trial_name
);
64 } // namespace field_trial
67 Histogram
* HistogramFactoryGetCounts(
68 const std::string
& name
, int min
, int max
, int bucket_count
) {
69 return g_factory_get_counts(name
, min
, max
, bucket_count
);
72 Histogram
* HistogramFactoryGetEnumeration(
73 const std::string
& name
, int boundary
) {
74 return g_factory_get_enumeration(name
, boundary
);
78 Histogram
* histogram_pointer
, const std::string
& name
, int sample
) {
79 g_histogram_add(histogram_pointer
, name
, sample
);
81 } // namespace metrics
86 // Initialize logging, set the forward allocator functions (not on mac), and
87 // return pointers to libjingle's WebRTC factory methods.
88 // Called from init_webrtc.cc.
90 bool InitializeModule(const base::CommandLine
& command_line
,
91 #if !defined(OS_MACOSX) && !defined(OS_ANDROID)
92 AllocateFunction alloc
,
93 DellocateFunction dealloc
,
95 FieldTrialFindFullName field_trial_find
,
96 RtcHistogramFactoryGetCounts factory_get_counts
,
97 RtcHistogramFactoryGetEnumeration factory_get_enumeration
,
98 RtcHistogramAdd histogram_add
,
99 logging::LogMessageHandlerFunction log_handler
,
100 webrtc::GetCategoryEnabledPtr trace_get_category_enabled
,
101 webrtc::AddTraceEventPtr trace_add_trace_event
,
102 CreateWebRtcMediaEngineFunction
* create_media_engine
,
103 DestroyWebRtcMediaEngineFunction
* destroy_media_engine
,
104 InitDiagnosticLoggingDelegateFunctionFunction
*
105 init_diagnostic_logging
) {
106 #if !defined(OS_MACOSX) && !defined(OS_ANDROID)
111 g_field_trial_find_
= field_trial_find
;
112 g_factory_get_counts
= factory_get_counts
;
113 g_factory_get_enumeration
= factory_get_enumeration
;
114 g_histogram_add
= histogram_add
;
116 *create_media_engine
= &CreateWebRtcMediaEngine
;
117 *destroy_media_engine
= &DestroyWebRtcMediaEngine
;
118 *init_diagnostic_logging
= &rtc::InitDiagnosticLoggingDelegateFunction
;
120 #if defined(OS_WIN) && defined(ARCH_CPU_X86_64)
121 // VS2013 only checks the existence of FMA3 instructions, not the enabled-ness
122 // of them at the OS level (this is fixed in VS2015). We force off usage of
123 // FMA3 instructions in the CRT to avoid using that path and hitting illegal
124 // instructions when running on CPUs that support FMA3, but OSs that don't.
125 // See http://crbug.com/436603 and http://crbug.com/446983.
127 #endif // WIN && ARCH_CPU_X86_64
129 if (base::CommandLine::Init(0, NULL
)) {
131 // This is not needed on Windows since CommandLine::Init has already
132 // done the equivalent thing via the GetCommandLine() API.
133 base::CommandLine::ForCurrentProcess()->AppendArguments(command_line
, true);
135 logging::LoggingSettings settings
;
136 settings
.logging_dest
= logging::LOG_TO_SYSTEM_DEBUG_LOG
;
137 logging::InitLogging(settings
);
139 // Override the log message handler to forward logs to chrome's handler.
140 logging::SetLogMessageHandler(log_handler
);
141 webrtc::SetupEventTracer(trace_get_category_enabled
,
142 trace_add_trace_event
);