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"; 54 const char MediaConstraintsInterface::kInternalAecDump[] = "internalAecDump"; 55 56 namespace { 57 58 // Convert constraints to audio options. Return false if constraints are 59 // invalid. 60 bool FromConstraints(const MediaConstraintsInterface::Constraints& constraints, 61 cricket::AudioOptions* options) { 62 bool success = true; 63 MediaConstraintsInterface::Constraints::const_iterator iter; 64 65 // This design relies on the fact that all the audio constraints are actually 66 // "options", i.e. boolean-valued and always satisfiable. If the constraints 67 // are extended to include non-boolean values or actual format constraints, 68 // a different algorithm will be required. 69 for (iter = constraints.begin(); iter != constraints.end(); ++iter) { 70 bool value = false; 71 72 if (!talk_base::FromString(iter->value, &value)) { 73 success = false; 74 continue; 75 } 76 77 if (iter->key == MediaConstraintsInterface::kEchoCancellation) 78 options->echo_cancellation.Set(value); 79 else if (iter->key == 80 MediaConstraintsInterface::kExperimentalEchoCancellation) 81 options->experimental_aec.Set(value); 82 else if (iter->key == MediaConstraintsInterface::kAutoGainControl) 83 options->auto_gain_control.Set(value); 84 else if (iter->key == 85 MediaConstraintsInterface::kExperimentalAutoGainControl) 86 options->experimental_agc.Set(value); 87 else if (iter->key == MediaConstraintsInterface::kNoiseSuppression) 88 options->noise_suppression.Set(value); 89 else if (iter->key == MediaConstraintsInterface::kHighpassFilter) 90 options->highpass_filter.Set(value); 91 else if (iter->key == MediaConstraintsInterface::kInternalAecDump) 92 options->aec_dump.Set(value); 93 else 94 success = false; 95 } 96 return success; 97 } 98 99 } // namespace 100 101 talk_base::scoped_refptr<LocalAudioSource> LocalAudioSource::Create( 102 const MediaConstraintsInterface* constraints) { 103 talk_base::scoped_refptr<LocalAudioSource> source( 104 new talk_base::RefCountedObject<LocalAudioSource>()); 105 source->Initialize(constraints); 106 return source; 107 } 108 109 void LocalAudioSource::Initialize( 110 const MediaConstraintsInterface* constraints) { 111 if (!constraints) 112 return; 113 114 // Apply optional constraints first, they will be overwritten by mandatory 115 // constraints. 116 FromConstraints(constraints->GetOptional(), &options_); 117 118 cricket::AudioOptions options; 119 if (!FromConstraints(constraints->GetMandatory(), &options)) { 120 source_state_ = kEnded; 121 return; 122 } 123 options_.SetAll(options); 124 source_state_ = kLive; 125 } 126 127 } // namespace webrtc 128