henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | /* |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame] | 2 | * Copyright 2013 The WebRTC project authors. All Rights Reserved. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3 | * |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame] | 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 |
| 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.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
Henrik Kjellander | 15583c1 | 2016-02-10 10:53:12 +0100 | [diff] [blame] | 11 | #include "webrtc/api/localaudiosource.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 12 | |
| 13 | #include <vector> |
| 14 | |
Henrik Kjellander | 15583c1 | 2016-02-10 10:53:12 +0100 | [diff] [blame] | 15 | #include "webrtc/api/mediaconstraintsinterface.h" |
kjellander | a96e2d7 | 2016-02-04 23:52:28 -0800 | [diff] [blame] | 16 | #include "webrtc/media/base/mediaengine.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 17 | |
| 18 | using webrtc::MediaConstraintsInterface; |
| 19 | using webrtc::MediaSourceInterface; |
| 20 | |
| 21 | namespace webrtc { |
| 22 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 23 | namespace { |
| 24 | |
| 25 | // Convert constraints to audio options. Return false if constraints are |
| 26 | // invalid. |
xians@webrtc.org | 38d8881 | 2014-08-13 13:51:58 +0000 | [diff] [blame] | 27 | void FromConstraints(const MediaConstraintsInterface::Constraints& constraints, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 28 | cricket::AudioOptions* options) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 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. |
Henrik Lundin | 441f634 | 2015-06-09 16:03:13 +0200 | [diff] [blame] | 33 | struct { |
| 34 | const char* name; |
Karl Wiberg | be57983 | 2015-11-10 22:34:18 +0100 | [diff] [blame] | 35 | rtc::Optional<bool>& value; |
Henrik Lundin | 441f634 | 2015-06-09 16:03:13 +0200 | [diff] [blame] | 36 | } key_to_value[] = { |
Tommi | 70c7fe1 | 2015-06-15 09:14:03 +0200 | [diff] [blame] | 37 | {MediaConstraintsInterface::kGoogEchoCancellation, |
Henrik Lundin | 441f634 | 2015-06-09 16:03:13 +0200 | [diff] [blame] | 38 | options->echo_cancellation}, |
Henrik Lundin | 441f634 | 2015-06-09 16:03:13 +0200 | [diff] [blame] | 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::kHighpassFilter, options->highpass_filter}, |
| 51 | {MediaConstraintsInterface::kTypingNoiseDetection, |
| 52 | options->typing_detection}, |
solenberg | 65c8fd7 | 2016-02-24 14:43:11 -0800 | [diff] [blame] | 53 | {MediaConstraintsInterface::kAudioMirroring, options->stereo_swapping} |
Henrik Lundin | 441f634 | 2015-06-09 16:03:13 +0200 | [diff] [blame] | 54 | }; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 55 | |
Henrik Lundin | 441f634 | 2015-06-09 16:03:13 +0200 | [diff] [blame] | 56 | for (const auto& constraint : constraints) { |
| 57 | bool value = false; |
| 58 | if (!rtc::FromString(constraint.value, &value)) |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 59 | continue; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 60 | |
Henrik Lundin | 441f634 | 2015-06-09 16:03:13 +0200 | [diff] [blame] | 61 | for (auto& entry : key_to_value) { |
| 62 | if (constraint.key.compare(entry.name) == 0) |
Karl Wiberg | be57983 | 2015-11-10 22:34:18 +0100 | [diff] [blame] | 63 | entry.value = rtc::Optional<bool>(value); |
Henrik Lundin | 441f634 | 2015-06-09 16:03:13 +0200 | [diff] [blame] | 64 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 65 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | } // namespace |
| 69 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 70 | rtc::scoped_refptr<LocalAudioSource> LocalAudioSource::Create( |
wu@webrtc.org | 97077a3 | 2013-10-25 21:18:33 +0000 | [diff] [blame] | 71 | const PeerConnectionFactoryInterface::Options& options, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 72 | const MediaConstraintsInterface* constraints) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 73 | rtc::scoped_refptr<LocalAudioSource> source( |
| 74 | new rtc::RefCountedObject<LocalAudioSource>()); |
wu@webrtc.org | 97077a3 | 2013-10-25 21:18:33 +0000 | [diff] [blame] | 75 | source->Initialize(options, constraints); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 76 | return source; |
| 77 | } |
| 78 | |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 79 | rtc::scoped_refptr<LocalAudioSource> LocalAudioSource::Create( |
| 80 | const PeerConnectionFactoryInterface::Options& options, |
| 81 | const cricket::AudioOptions* audio_options) { |
| 82 | rtc::scoped_refptr<LocalAudioSource> source( |
| 83 | new rtc::RefCountedObject<LocalAudioSource>()); |
| 84 | source->Initialize(options, audio_options); |
| 85 | return source; |
| 86 | } |
| 87 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 88 | void LocalAudioSource::Initialize( |
wu@webrtc.org | 97077a3 | 2013-10-25 21:18:33 +0000 | [diff] [blame] | 89 | const PeerConnectionFactoryInterface::Options& options, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 90 | const MediaConstraintsInterface* constraints) { |
| 91 | if (!constraints) |
| 92 | return; |
| 93 | |
| 94 | // Apply optional constraints first, they will be overwritten by mandatory |
| 95 | // constraints. |
| 96 | FromConstraints(constraints->GetOptional(), &options_); |
| 97 | |
xians@webrtc.org | 38d8881 | 2014-08-13 13:51:58 +0000 | [diff] [blame] | 98 | cricket::AudioOptions mandatory_options; |
| 99 | FromConstraints(constraints->GetMandatory(), &mandatory_options); |
| 100 | options_.SetAll(mandatory_options); |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | void LocalAudioSource::Initialize( |
| 104 | const PeerConnectionFactoryInterface::Options& options, |
| 105 | const cricket::AudioOptions* audio_options) { |
| 106 | if (!audio_options) |
| 107 | return; |
| 108 | |
| 109 | options_ = *audio_options; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | } // namespace webrtc |