henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | /* |
| 2 | * libjingle |
jlmiller@webrtc.org | 5f93d0a | 2015-01-20 21:36:13 +0000 | [diff] [blame] | 3 | * Copyright 2013 Google Inc. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions are met: |
| 7 | * |
| 8 | * 1. Redistributions of source code must retain the above copyright notice, |
| 9 | * this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 11 | * this list of conditions and the following disclaimer in the documentation |
| 12 | * and/or other materials provided with the distribution. |
| 13 | * 3. The name of the author may not be used to endorse or promote products |
| 14 | * derived from this software without specific prior written permission. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
| 17 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 18 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
| 19 | * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 20 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 21 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
| 22 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 23 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 24 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
| 25 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 | |
Henrik Kjellander | 15583c1 | 2016-02-10 10:53:12 +0100 | [diff] [blame^] | 28 | #include "webrtc/api/localaudiosource.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 29 | |
| 30 | #include <vector> |
| 31 | |
Henrik Kjellander | 15583c1 | 2016-02-10 10:53:12 +0100 | [diff] [blame^] | 32 | #include "webrtc/api/mediaconstraintsinterface.h" |
kjellander | a96e2d7 | 2016-02-04 23:52:28 -0800 | [diff] [blame] | 33 | #include "webrtc/media/base/mediaengine.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 34 | |
| 35 | using webrtc::MediaConstraintsInterface; |
| 36 | using webrtc::MediaSourceInterface; |
| 37 | |
| 38 | namespace webrtc { |
| 39 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 40 | namespace { |
| 41 | |
| 42 | // Convert constraints to audio options. Return false if constraints are |
| 43 | // invalid. |
xians@webrtc.org | 38d8881 | 2014-08-13 13:51:58 +0000 | [diff] [blame] | 44 | void FromConstraints(const MediaConstraintsInterface::Constraints& constraints, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 45 | cricket::AudioOptions* options) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 46 | // This design relies on the fact that all the audio constraints are actually |
| 47 | // "options", i.e. boolean-valued and always satisfiable. If the constraints |
| 48 | // are extended to include non-boolean values or actual format constraints, |
| 49 | // a different algorithm will be required. |
Henrik Lundin | 441f634 | 2015-06-09 16:03:13 +0200 | [diff] [blame] | 50 | struct { |
| 51 | const char* name; |
Karl Wiberg | be57983 | 2015-11-10 22:34:18 +0100 | [diff] [blame] | 52 | rtc::Optional<bool>& value; |
Henrik Lundin | 441f634 | 2015-06-09 16:03:13 +0200 | [diff] [blame] | 53 | } key_to_value[] = { |
Tommi | 70c7fe1 | 2015-06-15 09:14:03 +0200 | [diff] [blame] | 54 | {MediaConstraintsInterface::kGoogEchoCancellation, |
Henrik Lundin | 441f634 | 2015-06-09 16:03:13 +0200 | [diff] [blame] | 55 | options->echo_cancellation}, |
Henrik Lundin | 441f634 | 2015-06-09 16:03:13 +0200 | [diff] [blame] | 56 | {MediaConstraintsInterface::kExtendedFilterEchoCancellation, |
| 57 | options->extended_filter_aec}, |
| 58 | {MediaConstraintsInterface::kDAEchoCancellation, |
| 59 | options->delay_agnostic_aec}, |
| 60 | {MediaConstraintsInterface::kAutoGainControl, options->auto_gain_control}, |
| 61 | {MediaConstraintsInterface::kExperimentalAutoGainControl, |
| 62 | options->experimental_agc}, |
| 63 | {MediaConstraintsInterface::kNoiseSuppression, |
| 64 | options->noise_suppression}, |
| 65 | {MediaConstraintsInterface::kExperimentalNoiseSuppression, |
| 66 | options->experimental_ns}, |
| 67 | {MediaConstraintsInterface::kHighpassFilter, options->highpass_filter}, |
| 68 | {MediaConstraintsInterface::kTypingNoiseDetection, |
| 69 | options->typing_detection}, |
| 70 | {MediaConstraintsInterface::kAudioMirroring, options->stereo_swapping}, |
| 71 | {MediaConstraintsInterface::kAecDump, options->aec_dump} |
| 72 | }; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 73 | |
Henrik Lundin | 441f634 | 2015-06-09 16:03:13 +0200 | [diff] [blame] | 74 | for (const auto& constraint : constraints) { |
| 75 | bool value = false; |
| 76 | if (!rtc::FromString(constraint.value, &value)) |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 77 | continue; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 78 | |
Henrik Lundin | 441f634 | 2015-06-09 16:03:13 +0200 | [diff] [blame] | 79 | for (auto& entry : key_to_value) { |
| 80 | if (constraint.key.compare(entry.name) == 0) |
Karl Wiberg | be57983 | 2015-11-10 22:34:18 +0100 | [diff] [blame] | 81 | entry.value = rtc::Optional<bool>(value); |
Henrik Lundin | 441f634 | 2015-06-09 16:03:13 +0200 | [diff] [blame] | 82 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 83 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | } // namespace |
| 87 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 88 | rtc::scoped_refptr<LocalAudioSource> LocalAudioSource::Create( |
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) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 91 | rtc::scoped_refptr<LocalAudioSource> source( |
| 92 | new rtc::RefCountedObject<LocalAudioSource>()); |
wu@webrtc.org | 97077a3 | 2013-10-25 21:18:33 +0000 | [diff] [blame] | 93 | source->Initialize(options, constraints); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 94 | return source; |
| 95 | } |
| 96 | |
| 97 | void LocalAudioSource::Initialize( |
wu@webrtc.org | 97077a3 | 2013-10-25 21:18:33 +0000 | [diff] [blame] | 98 | const PeerConnectionFactoryInterface::Options& options, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 99 | const MediaConstraintsInterface* constraints) { |
| 100 | if (!constraints) |
| 101 | return; |
| 102 | |
| 103 | // Apply optional constraints first, they will be overwritten by mandatory |
| 104 | // constraints. |
| 105 | FromConstraints(constraints->GetOptional(), &options_); |
| 106 | |
xians@webrtc.org | 38d8881 | 2014-08-13 13:51:58 +0000 | [diff] [blame] | 107 | cricket::AudioOptions mandatory_options; |
| 108 | FromConstraints(constraints->GetMandatory(), &mandatory_options); |
| 109 | options_.SetAll(mandatory_options); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 110 | source_state_ = kLive; |
| 111 | } |
| 112 | |
| 113 | } // namespace webrtc |