blob: 70035fececf80bf97647a4292754e79fbc0050f1 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
kjellanderb24317b2016-02-10 07:54:43 -08002 * Copyright 2013 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
kjellanderb24317b2016-02-10 07:54:43 -08004 * 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
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00009 */
10
Henrik Kjellander15583c12016-02-10 10:53:12 +010011#include "webrtc/api/localaudiosource.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000012
13#include <vector>
14
Henrik Kjellander15583c12016-02-10 10:53:12 +010015#include "webrtc/api/mediaconstraintsinterface.h"
kjellandera96e2d72016-02-04 23:52:28 -080016#include "webrtc/media/base/mediaengine.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000017
18using webrtc::MediaConstraintsInterface;
19using webrtc::MediaSourceInterface;
20
21namespace webrtc {
22
henrike@webrtc.org28e20752013-07-10 00:45:36 +000023namespace {
24
25// Convert constraints to audio options. Return false if constraints are
26// invalid.
xians@webrtc.org38d88812014-08-13 13:51:58 +000027void FromConstraints(const MediaConstraintsInterface::Constraints& constraints,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000028 cricket::AudioOptions* options) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000029 // 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.
Henrik Lundin441f6342015-06-09 16:03:13 +020033 struct {
34 const char* name;
Karl Wibergbe579832015-11-10 22:34:18 +010035 rtc::Optional<bool>& value;
Henrik Lundin441f6342015-06-09 16:03:13 +020036 } key_to_value[] = {
Tommi70c7fe12015-06-15 09:14:03 +020037 {MediaConstraintsInterface::kGoogEchoCancellation,
Henrik Lundin441f6342015-06-09 16:03:13 +020038 options->echo_cancellation},
Henrik Lundin441f6342015-06-09 16:03:13 +020039 {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},
Alejandro Luebsc9b0c262016-05-16 15:32:38 -070050 {MediaConstraintsInterface::kIntelligibilityEnhancer,
51 options->intelligibility_enhancer},
peaha3333bf2016-06-30 00:02:34 -070052 {MediaConstraintsInterface::kLevelControl, options->level_control},
Henrik Lundin441f6342015-06-09 16:03:13 +020053 {MediaConstraintsInterface::kHighpassFilter, options->highpass_filter},
54 {MediaConstraintsInterface::kTypingNoiseDetection,
55 options->typing_detection},
solenberg65c8fd72016-02-24 14:43:11 -080056 {MediaConstraintsInterface::kAudioMirroring, options->stereo_swapping}
Henrik Lundin441f6342015-06-09 16:03:13 +020057 };
henrike@webrtc.org28e20752013-07-10 00:45:36 +000058
Henrik Lundin441f6342015-06-09 16:03:13 +020059 for (const auto& constraint : constraints) {
minyueba414282016-12-11 02:17:52 -080060 // 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.
Henrik Lundin441f6342015-06-09 16:03:13 +020082 bool value = false;
83 if (!rtc::FromString(constraint.value, &value))
henrike@webrtc.org28e20752013-07-10 00:45:36 +000084 continue;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000085
Henrik Lundin441f6342015-06-09 16:03:13 +020086 for (auto& entry : key_to_value) {
87 if (constraint.key.compare(entry.name) == 0)
Karl Wibergbe579832015-11-10 22:34:18 +010088 entry.value = rtc::Optional<bool>(value);
Henrik Lundin441f6342015-06-09 16:03:13 +020089 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +000090 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +000091}
92
93} // namespace
94
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000095rtc::scoped_refptr<LocalAudioSource> LocalAudioSource::Create(
wu@webrtc.org97077a32013-10-25 21:18:33 +000096 const PeerConnectionFactoryInterface::Options& options,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000097 const MediaConstraintsInterface* constraints) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000098 rtc::scoped_refptr<LocalAudioSource> source(
99 new rtc::RefCountedObject<LocalAudioSource>());
wu@webrtc.org97077a32013-10-25 21:18:33 +0000100 source->Initialize(options, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000101 return source;
102}
103
htaa2a49d92016-03-04 02:51:39 -0800104rtc::scoped_refptr<LocalAudioSource> LocalAudioSource::Create(
105 const PeerConnectionFactoryInterface::Options& options,
106 const cricket::AudioOptions* audio_options) {
107 rtc::scoped_refptr<LocalAudioSource> source(
108 new rtc::RefCountedObject<LocalAudioSource>());
109 source->Initialize(options, audio_options);
110 return source;
111}
112
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000113void LocalAudioSource::Initialize(
wu@webrtc.org97077a32013-10-25 21:18:33 +0000114 const PeerConnectionFactoryInterface::Options& options,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000115 const MediaConstraintsInterface* constraints) {
116 if (!constraints)
117 return;
118
119 // Apply optional constraints first, they will be overwritten by mandatory
120 // constraints.
121 FromConstraints(constraints->GetOptional(), &options_);
122
xians@webrtc.org38d88812014-08-13 13:51:58 +0000123 cricket::AudioOptions mandatory_options;
124 FromConstraints(constraints->GetMandatory(), &mandatory_options);
125 options_.SetAll(mandatory_options);
htaa2a49d92016-03-04 02:51:39 -0800126}
127
128void LocalAudioSource::Initialize(
129 const PeerConnectionFactoryInterface::Options& options,
130 const cricket::AudioOptions* audio_options) {
131 if (!audio_options)
132 return;
133
134 options_ = *audio_options;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000135}
136
137} // namespace webrtc