blob: ab9ae4fa99cb78fd8f20fd7c08c5d15e0a4fcf13 [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
henrike@webrtc.org28e20752013-07-10 00:45:36 +000040namespace {
41
42// Convert constraints to audio options. Return false if constraints are
43// invalid.
44bool FromConstraints(const MediaConstraintsInterface::Constraints& constraints,
45 cricket::AudioOptions* options) {
46 bool success = true;
47 MediaConstraintsInterface::Constraints::const_iterator iter;
48
49 // This design relies on the fact that all the audio constraints are actually
50 // "options", i.e. boolean-valued and always satisfiable. If the constraints
51 // are extended to include non-boolean values or actual format constraints,
52 // a different algorithm will be required.
53 for (iter = constraints.begin(); iter != constraints.end(); ++iter) {
54 bool value = false;
55
56 if (!talk_base::FromString(iter->value, &value)) {
57 success = false;
58 continue;
59 }
60
61 if (iter->key == MediaConstraintsInterface::kEchoCancellation)
62 options->echo_cancellation.Set(value);
63 else if (iter->key ==
64 MediaConstraintsInterface::kExperimentalEchoCancellation)
65 options->experimental_aec.Set(value);
66 else if (iter->key == MediaConstraintsInterface::kAutoGainControl)
67 options->auto_gain_control.Set(value);
68 else if (iter->key ==
69 MediaConstraintsInterface::kExperimentalAutoGainControl)
70 options->experimental_agc.Set(value);
71 else if (iter->key == MediaConstraintsInterface::kNoiseSuppression)
72 options->noise_suppression.Set(value);
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +000073 else if (iter->key ==
74 MediaConstraintsInterface::kExperimentalNoiseSuppression)
75 options->experimental_ns.Set(value);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000076 else if (iter->key == MediaConstraintsInterface::kHighpassFilter)
77 options->highpass_filter.Set(value);
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +000078 else if (iter->key == MediaConstraintsInterface::kTypingNoiseDetection)
79 options->typing_detection.Set(value);
wu@webrtc.org97077a32013-10-25 21:18:33 +000080 else if (iter->key == MediaConstraintsInterface::kAudioMirroring)
81 options->stereo_swapping.Set(value);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000082 else
83 success = false;
84 }
85 return success;
86}
87
88} // namespace
89
90talk_base::scoped_refptr<LocalAudioSource> LocalAudioSource::Create(
wu@webrtc.org97077a32013-10-25 21:18:33 +000091 const PeerConnectionFactoryInterface::Options& options,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000092 const MediaConstraintsInterface* constraints) {
93 talk_base::scoped_refptr<LocalAudioSource> source(
94 new talk_base::RefCountedObject<LocalAudioSource>());
wu@webrtc.org97077a32013-10-25 21:18:33 +000095 source->Initialize(options, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000096 return source;
97}
98
99void LocalAudioSource::Initialize(
wu@webrtc.org97077a32013-10-25 21:18:33 +0000100 const PeerConnectionFactoryInterface::Options& options,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000101 const MediaConstraintsInterface* constraints) {
102 if (!constraints)
103 return;
104
105 // Apply optional constraints first, they will be overwritten by mandatory
106 // constraints.
107 FromConstraints(constraints->GetOptional(), &options_);
108
wu@webrtc.org97077a32013-10-25 21:18:33 +0000109 cricket::AudioOptions audio_options;
110 if (!FromConstraints(constraints->GetMandatory(), &audio_options)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000111 source_state_ = kEnded;
112 return;
113 }
wu@webrtc.org97077a32013-10-25 21:18:33 +0000114 options_.SetAll(audio_options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000115 source_state_ = kLive;
116}
117
118} // namespace webrtc