henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | /* |
| 2 | * libjingle |
| 3 | * Copyright 2013, Google Inc. |
| 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 | |
| 28 | #include "talk/app/webrtc/localaudiosource.h" |
| 29 | |
| 30 | #include <vector> |
| 31 | |
| 32 | #include "talk/media/base/mediaengine.h" |
| 33 | #include "talk/app/webrtc/mediaconstraintsinterface.h" |
| 34 | |
| 35 | using webrtc::MediaConstraintsInterface; |
| 36 | using webrtc::MediaSourceInterface; |
| 37 | |
| 38 | namespace webrtc { |
| 39 | |
| 40 | // Constraint keys. |
| 41 | // They are declared as static members in mediaconstraintsinterface.h |
| 42 | const char MediaConstraintsInterface::kEchoCancellation[] = |
| 43 | "googEchoCancellation"; |
| 44 | const char MediaConstraintsInterface::kExperimentalEchoCancellation[] = |
| 45 | "googEchoCancellation2"; |
| 46 | const char MediaConstraintsInterface::kAutoGainControl[] = |
| 47 | "googAutoGainControl"; |
| 48 | const char MediaConstraintsInterface::kExperimentalAutoGainControl[] = |
| 49 | "googAutoGainControl2"; |
| 50 | const char MediaConstraintsInterface::kNoiseSuppression[] = |
| 51 | "googNoiseSuppression"; |
| 52 | const char MediaConstraintsInterface::kHighpassFilter[] = |
| 53 | "googHighpassFilter"; |
sergeyu@chromium.org | a59696b | 2013-09-13 23:48:58 +0000 | [diff] [blame] | 54 | const char MediaConstraintsInterface::kTypingNoiseDetection[] = |
| 55 | "googTypingNoiseDetection"; |
wu@webrtc.org | 97077a3 | 2013-10-25 21:18:33 +0000 | [diff] [blame] | 56 | const char MediaConstraintsInterface::kAudioMirroring[] = "googAudioMirroring"; |
| 57 | // TODO(perkj): Remove kInternalAecDump once its not used by Chrome. |
| 58 | const char MediaConstraintsInterface::kInternalAecDump[] = "deprecatedAecDump"; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 59 | |
| 60 | namespace { |
| 61 | |
| 62 | // Convert constraints to audio options. Return false if constraints are |
| 63 | // invalid. |
| 64 | bool FromConstraints(const MediaConstraintsInterface::Constraints& constraints, |
| 65 | cricket::AudioOptions* options) { |
| 66 | bool success = true; |
| 67 | MediaConstraintsInterface::Constraints::const_iterator iter; |
| 68 | |
| 69 | // This design relies on the fact that all the audio constraints are actually |
| 70 | // "options", i.e. boolean-valued and always satisfiable. If the constraints |
| 71 | // are extended to include non-boolean values or actual format constraints, |
| 72 | // a different algorithm will be required. |
| 73 | for (iter = constraints.begin(); iter != constraints.end(); ++iter) { |
| 74 | bool value = false; |
| 75 | |
| 76 | if (!talk_base::FromString(iter->value, &value)) { |
| 77 | success = false; |
| 78 | continue; |
| 79 | } |
| 80 | |
| 81 | if (iter->key == MediaConstraintsInterface::kEchoCancellation) |
| 82 | options->echo_cancellation.Set(value); |
| 83 | else if (iter->key == |
| 84 | MediaConstraintsInterface::kExperimentalEchoCancellation) |
| 85 | options->experimental_aec.Set(value); |
| 86 | else if (iter->key == MediaConstraintsInterface::kAutoGainControl) |
| 87 | options->auto_gain_control.Set(value); |
| 88 | else if (iter->key == |
| 89 | MediaConstraintsInterface::kExperimentalAutoGainControl) |
| 90 | options->experimental_agc.Set(value); |
| 91 | else if (iter->key == MediaConstraintsInterface::kNoiseSuppression) |
| 92 | options->noise_suppression.Set(value); |
| 93 | else if (iter->key == MediaConstraintsInterface::kHighpassFilter) |
| 94 | options->highpass_filter.Set(value); |
sergeyu@chromium.org | a59696b | 2013-09-13 23:48:58 +0000 | [diff] [blame] | 95 | else if (iter->key == MediaConstraintsInterface::kTypingNoiseDetection) |
| 96 | options->typing_detection.Set(value); |
wu@webrtc.org | 97077a3 | 2013-10-25 21:18:33 +0000 | [diff] [blame] | 97 | else if (iter->key == MediaConstraintsInterface::kAudioMirroring) |
| 98 | options->stereo_swapping.Set(value); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 99 | else |
| 100 | success = false; |
| 101 | } |
| 102 | return success; |
| 103 | } |
| 104 | |
| 105 | } // namespace |
| 106 | |
| 107 | talk_base::scoped_refptr<LocalAudioSource> LocalAudioSource::Create( |
wu@webrtc.org | 97077a3 | 2013-10-25 21:18:33 +0000 | [diff] [blame] | 108 | const PeerConnectionFactoryInterface::Options& options, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 109 | const MediaConstraintsInterface* constraints) { |
| 110 | talk_base::scoped_refptr<LocalAudioSource> source( |
| 111 | new talk_base::RefCountedObject<LocalAudioSource>()); |
wu@webrtc.org | 97077a3 | 2013-10-25 21:18:33 +0000 | [diff] [blame] | 112 | source->Initialize(options, constraints); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 113 | return source; |
| 114 | } |
| 115 | |
| 116 | void LocalAudioSource::Initialize( |
wu@webrtc.org | 97077a3 | 2013-10-25 21:18:33 +0000 | [diff] [blame] | 117 | const PeerConnectionFactoryInterface::Options& options, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 118 | const MediaConstraintsInterface* constraints) { |
| 119 | if (!constraints) |
| 120 | return; |
| 121 | |
| 122 | // Apply optional constraints first, they will be overwritten by mandatory |
| 123 | // constraints. |
| 124 | FromConstraints(constraints->GetOptional(), &options_); |
| 125 | |
wu@webrtc.org | 97077a3 | 2013-10-25 21:18:33 +0000 | [diff] [blame] | 126 | cricket::AudioOptions audio_options; |
| 127 | if (!FromConstraints(constraints->GetMandatory(), &audio_options)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 128 | source_state_ = kEnded; |
| 129 | return; |
| 130 | } |
wu@webrtc.org | 97077a3 | 2013-10-25 21:18:33 +0000 | [diff] [blame] | 131 | options_.SetAll(audio_options); |
| 132 | if (options.enable_aec_dump) |
| 133 | options_.aec_dump.Set(true); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 134 | source_state_ = kLive; |
| 135 | } |
| 136 | |
| 137 | } // namespace webrtc |