blob: 4b961cf2a9791c218d3156ebdc8d2361a3d7d297 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
kjellanderb24317b2016-02-10 07:54:43 -08002 * Copyright 2013 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
kjellanderb24317b2016-02-10 07:54:43 -08004 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00009 */
10
Henrik Kjellander15583c12016-02-10 10:53:12 +010011#include "webrtc/api/localaudiosource.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000012
13#include <vector>
14
Henrik Kjellander15583c12016-02-10 10:53:12 +010015#include "webrtc/api/mediaconstraintsinterface.h"
kjellandera96e2d72016-02-04 23:52:28 -080016#include "webrtc/media/base/mediaengine.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000017
18using webrtc::MediaConstraintsInterface;
19using webrtc::MediaSourceInterface;
20
21namespace webrtc {
22
henrike@webrtc.org28e20752013-07-10 00:45:36 +000023namespace {
24
25// Convert constraints to audio options. Return false if constraints are
26// invalid.
xians@webrtc.org38d88812014-08-13 13:51:58 +000027void FromConstraints(const MediaConstraintsInterface::Constraints& constraints,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000028 cricket::AudioOptions* options) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000029 // This design relies on the fact that all the audio constraints are actually
30 // "options", i.e. boolean-valued and always satisfiable. If the constraints
31 // are extended to include non-boolean values or actual format constraints,
32 // a different algorithm will be required.
Henrik Lundin441f6342015-06-09 16:03:13 +020033 struct {
34 const char* name;
Karl Wibergbe579832015-11-10 22:34:18 +010035 rtc::Optional<bool>& value;
Henrik Lundin441f6342015-06-09 16:03:13 +020036 } key_to_value[] = {
Tommi70c7fe12015-06-15 09:14:03 +020037 {MediaConstraintsInterface::kGoogEchoCancellation,
Henrik Lundin441f6342015-06-09 16:03:13 +020038 options->echo_cancellation},
Henrik Lundin441f6342015-06-09 16:03:13 +020039 {MediaConstraintsInterface::kExtendedFilterEchoCancellation,
40 options->extended_filter_aec},
41 {MediaConstraintsInterface::kDAEchoCancellation,
42 options->delay_agnostic_aec},
43 {MediaConstraintsInterface::kAutoGainControl, options->auto_gain_control},
44 {MediaConstraintsInterface::kExperimentalAutoGainControl,
45 options->experimental_agc},
46 {MediaConstraintsInterface::kNoiseSuppression,
47 options->noise_suppression},
48 {MediaConstraintsInterface::kExperimentalNoiseSuppression,
49 options->experimental_ns},
50 {MediaConstraintsInterface::kHighpassFilter, options->highpass_filter},
51 {MediaConstraintsInterface::kTypingNoiseDetection,
52 options->typing_detection},
53 {MediaConstraintsInterface::kAudioMirroring, options->stereo_swapping},
54 {MediaConstraintsInterface::kAecDump, options->aec_dump}
55 };
henrike@webrtc.org28e20752013-07-10 00:45:36 +000056
Henrik Lundin441f6342015-06-09 16:03:13 +020057 for (const auto& constraint : constraints) {
58 bool value = false;
59 if (!rtc::FromString(constraint.value, &value))
henrike@webrtc.org28e20752013-07-10 00:45:36 +000060 continue;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000061
Henrik Lundin441f6342015-06-09 16:03:13 +020062 for (auto& entry : key_to_value) {
63 if (constraint.key.compare(entry.name) == 0)
Karl Wibergbe579832015-11-10 22:34:18 +010064 entry.value = rtc::Optional<bool>(value);
Henrik Lundin441f6342015-06-09 16:03:13 +020065 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +000066 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +000067}
68
69} // namespace
70
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000071rtc::scoped_refptr<LocalAudioSource> LocalAudioSource::Create(
wu@webrtc.org97077a32013-10-25 21:18:33 +000072 const PeerConnectionFactoryInterface::Options& options,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000073 const MediaConstraintsInterface* constraints) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000074 rtc::scoped_refptr<LocalAudioSource> source(
75 new rtc::RefCountedObject<LocalAudioSource>());
wu@webrtc.org97077a32013-10-25 21:18:33 +000076 source->Initialize(options, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000077 return source;
78}
79
80void LocalAudioSource::Initialize(
wu@webrtc.org97077a32013-10-25 21:18:33 +000081 const PeerConnectionFactoryInterface::Options& options,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000082 const MediaConstraintsInterface* constraints) {
83 if (!constraints)
84 return;
85
86 // Apply optional constraints first, they will be overwritten by mandatory
87 // constraints.
88 FromConstraints(constraints->GetOptional(), &options_);
89
xians@webrtc.org38d88812014-08-13 13:51:58 +000090 cricket::AudioOptions mandatory_options;
91 FromConstraints(constraints->GetMandatory(), &mandatory_options);
92 options_.SetAll(mandatory_options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000093 source_state_ = kLive;
94}
95
96} // namespace webrtc