blob: 9da9fd2612316c384100ee6faf8ac6a773f60500 [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},
Alejandro Luebsc9b0c262016-05-16 15:32:38 -070050 {MediaConstraintsInterface::kIntelligibilityEnhancer,
51 options->intelligibility_enhancer},
Henrik Lundin441f6342015-06-09 16:03:13 +020052 {MediaConstraintsInterface::kHighpassFilter, options->highpass_filter},
53 {MediaConstraintsInterface::kTypingNoiseDetection,
54 options->typing_detection},
solenberg65c8fd72016-02-24 14:43:11 -080055 {MediaConstraintsInterface::kAudioMirroring, options->stereo_swapping}
Henrik Lundin441f6342015-06-09 16:03:13 +020056 };
henrike@webrtc.org28e20752013-07-10 00:45:36 +000057
Henrik Lundin441f6342015-06-09 16:03:13 +020058 for (const auto& constraint : constraints) {
59 bool value = false;
60 if (!rtc::FromString(constraint.value, &value))
henrike@webrtc.org28e20752013-07-10 00:45:36 +000061 continue;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000062
Henrik Lundin441f6342015-06-09 16:03:13 +020063 for (auto& entry : key_to_value) {
64 if (constraint.key.compare(entry.name) == 0)
Karl Wibergbe579832015-11-10 22:34:18 +010065 entry.value = rtc::Optional<bool>(value);
Henrik Lundin441f6342015-06-09 16:03:13 +020066 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +000067 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +000068}
69
70} // namespace
71
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000072rtc::scoped_refptr<LocalAudioSource> LocalAudioSource::Create(
wu@webrtc.org97077a32013-10-25 21:18:33 +000073 const PeerConnectionFactoryInterface::Options& options,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000074 const MediaConstraintsInterface* constraints) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000075 rtc::scoped_refptr<LocalAudioSource> source(
76 new rtc::RefCountedObject<LocalAudioSource>());
wu@webrtc.org97077a32013-10-25 21:18:33 +000077 source->Initialize(options, constraints);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000078 return source;
79}
80
htaa2a49d92016-03-04 02:51:39 -080081rtc::scoped_refptr<LocalAudioSource> LocalAudioSource::Create(
82 const PeerConnectionFactoryInterface::Options& options,
83 const cricket::AudioOptions* audio_options) {
84 rtc::scoped_refptr<LocalAudioSource> source(
85 new rtc::RefCountedObject<LocalAudioSource>());
86 source->Initialize(options, audio_options);
87 return source;
88}
89
henrike@webrtc.org28e20752013-07-10 00:45:36 +000090void LocalAudioSource::Initialize(
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 if (!constraints)
94 return;
95
96 // Apply optional constraints first, they will be overwritten by mandatory
97 // constraints.
98 FromConstraints(constraints->GetOptional(), &options_);
99
xians@webrtc.org38d88812014-08-13 13:51:58 +0000100 cricket::AudioOptions mandatory_options;
101 FromConstraints(constraints->GetMandatory(), &mandatory_options);
102 options_.SetAll(mandatory_options);
htaa2a49d92016-03-04 02:51:39 -0800103}
104
105void LocalAudioSource::Initialize(
106 const PeerConnectionFactoryInterface::Options& options,
107 const cricket::AudioOptions* audio_options) {
108 if (!audio_options)
109 return;
110
111 options_ = *audio_options;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000112}
113
114} // namespace webrtc