Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / libjingle / source / talk / app / webrtc / localaudiosource.cc
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/app/webrtc/mediaconstraintsinterface.h"
33 #include "talk/media/base/mediaengine.h"
34
35 using webrtc::MediaConstraintsInterface;
36 using webrtc::MediaSourceInterface;
37
38 namespace webrtc {
39
40 namespace {
41
42 // Convert constraints to audio options. Return false if constraints are
43 // invalid.
44 void FromConstraints(const MediaConstraintsInterface::Constraints& constraints,
45                      cricket::AudioOptions* options) {
46   MediaConstraintsInterface::Constraints::const_iterator iter;
47
48   // This design relies on the fact that all the audio constraints are actually
49   // "options", i.e. boolean-valued and always satisfiable.  If the constraints
50   // are extended to include non-boolean values or actual format constraints,
51   // a different algorithm will be required.
52   for (iter = constraints.begin(); iter != constraints.end(); ++iter) {
53     bool value = false;
54
55     if (!rtc::FromString(iter->value, &value))
56       continue;
57
58     if (iter->key == MediaConstraintsInterface::kEchoCancellation)
59       options->echo_cancellation.Set(value);
60     else if (iter->key ==
61         MediaConstraintsInterface::kExperimentalEchoCancellation)
62       options->experimental_aec.Set(value);
63     else if (iter->key == MediaConstraintsInterface::kAutoGainControl)
64       options->auto_gain_control.Set(value);
65     else if (iter->key ==
66         MediaConstraintsInterface::kExperimentalAutoGainControl)
67       options->experimental_agc.Set(value);
68     else if (iter->key == MediaConstraintsInterface::kNoiseSuppression)
69       options->noise_suppression.Set(value);
70     else if (iter->key ==
71           MediaConstraintsInterface::kExperimentalNoiseSuppression)
72       options->experimental_ns.Set(value);
73     else if (iter->key == MediaConstraintsInterface::kHighpassFilter)
74       options->highpass_filter.Set(value);
75     else if (iter->key == MediaConstraintsInterface::kTypingNoiseDetection)
76       options->typing_detection.Set(value);
77     else if (iter->key == MediaConstraintsInterface::kAudioMirroring)
78       options->stereo_swapping.Set(value);
79   }
80 }
81
82 }  // namespace
83
84 rtc::scoped_refptr<LocalAudioSource> LocalAudioSource::Create(
85     const PeerConnectionFactoryInterface::Options& options,
86     const MediaConstraintsInterface* constraints) {
87   rtc::scoped_refptr<LocalAudioSource> source(
88       new rtc::RefCountedObject<LocalAudioSource>());
89   source->Initialize(options, constraints);
90   return source;
91 }
92
93 void LocalAudioSource::Initialize(
94     const PeerConnectionFactoryInterface::Options& options,
95     const MediaConstraintsInterface* constraints) {
96   if (!constraints)
97     return;
98
99   // Apply optional constraints first, they will be overwritten by mandatory
100   // constraints.
101   FromConstraints(constraints->GetOptional(), &options_);
102
103   cricket::AudioOptions mandatory_options;
104   FromConstraints(constraints->GetMandatory(), &mandatory_options);
105   options_.SetAll(mandatory_options);
106   source_state_ = kLive;
107 }
108
109 }  // namespace webrtc