OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2013 The WebRTC project authors. All Rights Reserved. | 2 * Copyright 2013 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include "webrtc/api/localaudiosource.h" | 11 #include "webrtc/api/localaudiosource.h" |
12 | 12 |
13 #include <vector> | 13 #include <vector> |
14 | 14 |
15 #include "webrtc/api/mediaconstraintsinterface.h" | 15 #include "webrtc/api/mediaconstraintsinterface.h" |
16 #include "webrtc/media/base/mediaengine.h" | 16 #include "webrtc/media/base/mediaengine.h" |
17 | 17 |
18 using webrtc::MediaConstraintsInterface; | 18 using webrtc::MediaConstraintsInterface; |
19 using webrtc::MediaSourceInterface; | 19 using webrtc::MediaSourceInterface; |
20 | 20 |
21 namespace webrtc { | 21 namespace webrtc { |
22 | 22 |
23 namespace { | |
24 | |
25 // Convert constraints to audio options. Return false if constraints are | |
26 // invalid. | |
27 void FromConstraints(const MediaConstraintsInterface::Constraints& constraints, | |
28 cricket::AudioOptions* options) { | |
29 // This design relies on the fact that all the audio constraints are actually | |
30 // "options", i.e. boolean-valued and always satisfiable. If the constraints | |
31 // are extended to include non-boolean values or actual format constraints, | |
32 // a different algorithm will be required. | |
33 struct { | |
34 const char* name; | |
35 rtc::Optional<bool>& value; | |
36 } key_to_value[] = { | |
37 {MediaConstraintsInterface::kGoogEchoCancellation, | |
38 options->echo_cancellation}, | |
39 {MediaConstraintsInterface::kExtendedFilterEchoCancellation, | |
40 options->extended_filter_aec}, | |
41 {MediaConstraintsInterface::kDAEchoCancellation, | |
42 options->delay_agnostic_aec}, | |
43 {MediaConstraintsInterface::kAutoGainControl, options->auto_gain_control}, | |
44 {MediaConstraintsInterface::kExperimentalAutoGainControl, | |
45 options->experimental_agc}, | |
46 {MediaConstraintsInterface::kNoiseSuppression, | |
47 options->noise_suppression}, | |
48 {MediaConstraintsInterface::kExperimentalNoiseSuppression, | |
49 options->experimental_ns}, | |
50 {MediaConstraintsInterface::kIntelligibilityEnhancer, | |
51 options->intelligibility_enhancer}, | |
52 {MediaConstraintsInterface::kLevelControl, options->level_control}, | |
53 {MediaConstraintsInterface::kHighpassFilter, options->highpass_filter}, | |
54 {MediaConstraintsInterface::kTypingNoiseDetection, | |
55 options->typing_detection}, | |
56 {MediaConstraintsInterface::kAudioMirroring, options->stereo_swapping} | |
57 }; | |
58 | |
59 for (const auto& constraint : constraints) { | |
60 // Set non-boolean constraints. | |
61 if (0 == constraint.key.compare( | |
62 MediaConstraintsInterface::kLevelControlInitialPeakLevelDBFS)) { | |
63 float level_control_initial_peak_level_dbfs = 0.0f; | |
64 if (rtc::FromString(constraint.value, | |
65 &level_control_initial_peak_level_dbfs)) { | |
66 options->level_control_initial_peak_level_dbfs = | |
67 rtc::Optional<float>(level_control_initial_peak_level_dbfs); | |
68 } | |
69 continue; | |
70 } | |
71 if (constraint.key.compare( | |
72 MediaConstraintsInterface::kAudioNetworkAdaptorConfig) == 0) { | |
73 // When |kAudioNetworkAdaptorConfig| is defined, it both means that audio | |
74 // network adaptor is desired, and provides the config string. | |
75 options->audio_network_adaptor = rtc::Optional<bool>(true); | |
76 options->audio_network_adaptor_config = | |
77 rtc::Optional<std::string>(constraint.value); | |
78 continue; | |
79 } | |
80 | |
81 // Parse boolean value. | |
82 bool value = false; | |
83 if (!rtc::FromString(constraint.value, &value)) | |
84 continue; | |
85 | |
86 for (auto& entry : key_to_value) { | |
87 if (constraint.key.compare(entry.name) == 0) | |
88 entry.value = rtc::Optional<bool>(value); | |
89 } | |
90 } | |
91 } | |
92 | |
93 } // namespace | |
94 | |
95 rtc::scoped_refptr<LocalAudioSource> LocalAudioSource::Create( | 23 rtc::scoped_refptr<LocalAudioSource> LocalAudioSource::Create( |
96 const PeerConnectionFactoryInterface::Options& options, | 24 const PeerConnectionFactoryInterface::Options& options, |
97 const MediaConstraintsInterface* constraints) { | 25 const MediaConstraintsInterface* constraints) { |
98 rtc::scoped_refptr<LocalAudioSource> source( | 26 rtc::scoped_refptr<LocalAudioSource> source( |
99 new rtc::RefCountedObject<LocalAudioSource>()); | 27 new rtc::RefCountedObject<LocalAudioSource>()); |
100 source->Initialize(options, constraints); | 28 source->Initialize(options, constraints); |
101 return source; | 29 return source; |
102 } | 30 } |
103 | 31 |
104 rtc::scoped_refptr<LocalAudioSource> LocalAudioSource::Create( | 32 rtc::scoped_refptr<LocalAudioSource> LocalAudioSource::Create( |
105 const PeerConnectionFactoryInterface::Options& options, | 33 const PeerConnectionFactoryInterface::Options& options, |
106 const cricket::AudioOptions* audio_options) { | 34 const cricket::AudioOptions* audio_options) { |
107 rtc::scoped_refptr<LocalAudioSource> source( | 35 rtc::scoped_refptr<LocalAudioSource> source( |
108 new rtc::RefCountedObject<LocalAudioSource>()); | 36 new rtc::RefCountedObject<LocalAudioSource>()); |
109 source->Initialize(options, audio_options); | 37 source->Initialize(options, audio_options); |
110 return source; | 38 return source; |
111 } | 39 } |
112 | 40 |
113 void LocalAudioSource::Initialize( | 41 void LocalAudioSource::Initialize( |
114 const PeerConnectionFactoryInterface::Options& options, | 42 const PeerConnectionFactoryInterface::Options& options, |
115 const MediaConstraintsInterface* constraints) { | 43 const MediaConstraintsInterface* constraints) { |
116 if (!constraints) | 44 CopyConstraintsIntoAudioOptions(constraints, &options_); |
117 return; | |
118 | |
119 // Apply optional constraints first, they will be overwritten by mandatory | |
120 // constraints. | |
121 FromConstraints(constraints->GetOptional(), &options_); | |
122 | |
123 cricket::AudioOptions mandatory_options; | |
124 FromConstraints(constraints->GetMandatory(), &mandatory_options); | |
125 options_.SetAll(mandatory_options); | |
126 } | 45 } |
127 | 46 |
128 void LocalAudioSource::Initialize( | 47 void LocalAudioSource::Initialize( |
129 const PeerConnectionFactoryInterface::Options& options, | 48 const PeerConnectionFactoryInterface::Options& options, |
130 const cricket::AudioOptions* audio_options) { | 49 const cricket::AudioOptions* audio_options) { |
131 if (!audio_options) | 50 if (!audio_options) |
132 return; | 51 return; |
133 | 52 |
134 options_ = *audio_options; | 53 options_ = *audio_options; |
135 } | 54 } |
136 | 55 |
137 } // namespace webrtc | 56 } // namespace webrtc |
OLD | NEW |