blob: 2cd472a5a018ae027d7c7fee3caeed9b7e69592a [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
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
35using webrtc::MediaConstraintsInterface;
36using webrtc::MediaSourceInterface;
37
38namespace webrtc {
39
40// Constraint keys.
41// They are declared as static members in mediaconstraintsinterface.h
42const char MediaConstraintsInterface::kEchoCancellation[] =
43 "googEchoCancellation";
44const char MediaConstraintsInterface::kExperimentalEchoCancellation[] =
45 "googEchoCancellation2";
46const char MediaConstraintsInterface::kAutoGainControl[] =
47 "googAutoGainControl";
48const char MediaConstraintsInterface::kExperimentalAutoGainControl[] =
49 "googAutoGainControl2";
50const char MediaConstraintsInterface::kNoiseSuppression[] =
51 "googNoiseSuppression";
52const char MediaConstraintsInterface::kHighpassFilter[] =
53 "googHighpassFilter";
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +000054const char MediaConstraintsInterface::kTypingNoiseDetection[] =
55 "googTypingNoiseDetection";
wu@webrtc.org97077a32013-10-25 21:18:33 +000056const char MediaConstraintsInterface::kAudioMirroring[] = "googAudioMirroring";
57// TODO(perkj): Remove kInternalAecDump once its not used by Chrome.
58const char MediaConstraintsInterface::kInternalAecDump[] = "deprecatedAecDump";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000059
60namespace {
61
62// Convert constraints to audio options. Return false if constraints are
63// invalid.
64bool 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.orga59696b2013-09-13 23:48:58 +000095 else if (iter->key == MediaConstraintsInterface::kTypingNoiseDetection)
96 options->typing_detection.Set(value);
wu@webrtc.org97077a32013-10-25 21:18:33 +000097 else if (iter->key == MediaConstraintsInterface::kAudioMirroring)
98 options->stereo_swapping.Set(value);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000099 else
100 success = false;
101 }
102 return success;
103}
104
105} // namespace
106
107talk_base::scoped_refptr<LocalAudioSource> LocalAudioSource::Create(
wu@webrtc.org97077a32013-10-25 21:18:33 +0000108 const PeerConnectionFactoryInterface::Options& options,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000109 const MediaConstraintsInterface* constraints) {
110 talk_base::scoped_refptr<LocalAudioSource> source(
111 new talk_base::RefCountedObject<LocalAudioSource>());
wu@webrtc.org97077a32013-10-25 21:18:33 +0000112 source->Initialize(options, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000113 return source;
114}
115
116void LocalAudioSource::Initialize(
wu@webrtc.org97077a32013-10-25 21:18:33 +0000117 const PeerConnectionFactoryInterface::Options& options,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000118 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.org97077a32013-10-25 21:18:33 +0000126 cricket::AudioOptions audio_options;
127 if (!FromConstraints(constraints->GetMandatory(), &audio_options)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000128 source_state_ = kEnded;
129 return;
130 }
wu@webrtc.org97077a32013-10-25 21:18:33 +0000131 options_.SetAll(audio_options);
132 if (options.enable_aec_dump)
133 options_.aec_dump.Set(true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000134 source_state_ = kLive;
135}
136
137} // namespace webrtc