blob: d91394eebcfca022e1e4b1270dc15985de4ab065 [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) {
60 bool value = false;
61 if (!rtc::FromString(constraint.value, &value))
henrike@webrtc.org28e20752013-07-10 00:45:36 +000062 continue;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000063
Henrik Lundin441f6342015-06-09 16:03:13 +020064 for (auto& entry : key_to_value) {
65 if (constraint.key.compare(entry.name) == 0)
Karl Wibergbe579832015-11-10 22:34:18 +010066 entry.value = rtc::Optional<bool>(value);
Henrik Lundin441f6342015-06-09 16:03:13 +020067 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +000068 }
aleloie33c5d92016-10-20 01:53:27 -070069
70 // Set non-boolean constraints.
71 std::string value;
72 if (constraints.FindFirst(
73 MediaConstraintsInterface::kLevelControlInitialPeakLevelDBFS,
74 &value)) {
75 float level_control_initial_peak_level_dbfs;
76 if (rtc::FromString(value, &level_control_initial_peak_level_dbfs)) {
77 options->level_control_initial_peak_level_dbfs =
78 rtc::Optional<float>(level_control_initial_peak_level_dbfs);
79 }
80 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +000081}
82
83} // namespace
84
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000085rtc::scoped_refptr<LocalAudioSource> LocalAudioSource::Create(
wu@webrtc.org97077a32013-10-25 21:18:33 +000086 const PeerConnectionFactoryInterface::Options& options,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000087 const MediaConstraintsInterface* constraints) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000088 rtc::scoped_refptr<LocalAudioSource> source(
89 new rtc::RefCountedObject<LocalAudioSource>());
wu@webrtc.org97077a32013-10-25 21:18:33 +000090 source->Initialize(options, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000091 return source;
92}
93
htaa2a49d92016-03-04 02:51:39 -080094rtc::scoped_refptr<LocalAudioSource> LocalAudioSource::Create(
95 const PeerConnectionFactoryInterface::Options& options,
96 const cricket::AudioOptions* audio_options) {
97 rtc::scoped_refptr<LocalAudioSource> source(
98 new rtc::RefCountedObject<LocalAudioSource>());
99 source->Initialize(options, audio_options);
100 return source;
101}
102
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000103void LocalAudioSource::Initialize(
wu@webrtc.org97077a32013-10-25 21:18:33 +0000104 const PeerConnectionFactoryInterface::Options& options,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000105 const MediaConstraintsInterface* constraints) {
106 if (!constraints)
107 return;
108
109 // Apply optional constraints first, they will be overwritten by mandatory
110 // constraints.
111 FromConstraints(constraints->GetOptional(), &options_);
112
xians@webrtc.org38d88812014-08-13 13:51:58 +0000113 cricket::AudioOptions mandatory_options;
114 FromConstraints(constraints->GetMandatory(), &mandatory_options);
115 options_.SetAll(mandatory_options);
htaa2a49d92016-03-04 02:51:39 -0800116}
117
118void LocalAudioSource::Initialize(
119 const PeerConnectionFactoryInterface::Options& options,
120 const cricket::AudioOptions* audio_options) {
121 if (!audio_options)
122 return;
123
124 options_ = *audio_options;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000125}
126
127} // namespace webrtc