blob: c21d9113262d475b83d57cb296e225e9afd7747f [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
bjornv@webrtc.org0c6f9312012-01-30 09:39:08 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
4 * 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.
9 */
10
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef MODULES_AUDIO_PROCESSING_GAIN_CONTROL_IMPL_H_
12#define MODULES_AUDIO_PROCESSING_GAIN_CONTROL_IMPL_H_
niklase@google.com470e71d2011-07-07 08:21:25 +000013
Yves Gerey988cc082018-10-23 12:03:01 +020014#include <stddef.h>
15#include <stdint.h>
kwiberg88788ad2016-02-19 07:04:49 -080016#include <memory>
niklase@google.com470e71d2011-07-07 08:21:25 +000017#include <vector>
18
Yves Gerey988cc082018-10-23 12:03:01 +020019#include "absl/types/optional.h"
20#include "api/array_view.h"
21#include "modules/audio_processing/include/gain_control.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "rtc_base/constructormagic.h"
23#include "rtc_base/criticalsection.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "rtc_base/thread_annotations.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000025
26namespace webrtc {
andrew@webrtc.org56e4a052014-02-27 22:23:17 +000027
peah135259a2016-10-28 03:12:11 -070028class ApmDataDumper;
niklase@google.com470e71d2011-07-07 08:21:25 +000029class AudioBuffer;
30
peahbfa97112016-03-10 21:09:04 -080031class GainControlImpl : public GainControl {
niklase@google.com470e71d2011-07-07 08:21:25 +000032 public:
peahb8fbb542016-03-15 02:28:08 -070033 GainControlImpl(rtc::CriticalSection* crit_render,
peahdf3efa82015-11-28 12:35:15 -080034 rtc::CriticalSection* crit_capture);
peahbfa97112016-03-10 21:09:04 -080035 ~GainControlImpl() override;
niklase@google.com470e71d2011-07-07 08:21:25 +000036
peah701d6282016-10-25 05:42:20 -070037 void ProcessRenderAudio(rtc::ArrayView<const int16_t> packed_render_audio);
niklase@google.com470e71d2011-07-07 08:21:25 +000038 int AnalyzeCaptureAudio(AudioBuffer* audio);
peahb8fbb542016-03-15 02:28:08 -070039 int ProcessCaptureAudio(AudioBuffer* audio, bool stream_has_echo);
niklase@google.com470e71d2011-07-07 08:21:25 +000040
peahb8fbb542016-03-15 02:28:08 -070041 void Initialize(size_t num_proc_channels, int sample_rate_hz);
niklase@google.com470e71d2011-07-07 08:21:25 +000042
peah701d6282016-10-25 05:42:20 -070043 static void PackRenderAudioBuffer(AudioBuffer* audio,
44 std::vector<int16_t>* packed_buffer);
45
niklase@google.com470e71d2011-07-07 08:21:25 +000046 // GainControl implementation.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000047 bool is_enabled() const override;
48 int stream_analog_level() override;
Minyue13b96ba2015-10-03 00:39:14 +020049 bool is_limiter_enabled() const override;
50 Mode mode() const override;
niklase@google.com470e71d2011-07-07 08:21:25 +000051
aluebs11d4a422016-04-28 14:58:32 -070052 int compression_gain_db() const override;
53
niklase@google.com470e71d2011-07-07 08:21:25 +000054 private:
peahbfa97112016-03-10 21:09:04 -080055 class GainController;
56
niklase@google.com470e71d2011-07-07 08:21:25 +000057 // GainControl implementation.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000058 int Enable(bool enable) override;
59 int set_stream_analog_level(int level) override;
60 int set_mode(Mode mode) override;
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000061 int set_target_level_dbfs(int level) override;
62 int target_level_dbfs() const override;
63 int set_compression_gain_db(int gain) override;
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000064 int enable_limiter(bool enable) override;
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000065 int set_analog_level_limits(int minimum, int maximum) override;
66 int analog_level_minimum() const override;
67 int analog_level_maximum() const override;
68 bool stream_is_saturated() const override;
niklase@google.com470e71d2011-07-07 08:21:25 +000069
peahbfa97112016-03-10 21:09:04 -080070 int Configure();
peah4d291f72015-11-16 23:52:25 -080071
danilchap56359be2017-09-07 07:53:45 -070072 rtc::CriticalSection* const crit_render_ RTC_ACQUIRED_BEFORE(crit_capture_);
peahdf3efa82015-11-28 12:35:15 -080073 rtc::CriticalSection* const crit_capture_;
74
peah135259a2016-10-28 03:12:11 -070075 std::unique_ptr<ApmDataDumper> data_dumper_;
76
peahbfa97112016-03-10 21:09:04 -080077 bool enabled_ = false;
78
danilchap56359be2017-09-07 07:53:45 -070079 Mode mode_ RTC_GUARDED_BY(crit_capture_);
80 int minimum_capture_level_ RTC_GUARDED_BY(crit_capture_);
81 int maximum_capture_level_ RTC_GUARDED_BY(crit_capture_);
82 bool limiter_enabled_ RTC_GUARDED_BY(crit_capture_);
83 int target_level_dbfs_ RTC_GUARDED_BY(crit_capture_);
84 int compression_gain_db_ RTC_GUARDED_BY(crit_capture_);
85 int analog_capture_level_ RTC_GUARDED_BY(crit_capture_);
86 bool was_analog_level_set_ RTC_GUARDED_BY(crit_capture_);
87 bool stream_is_saturated_ RTC_GUARDED_BY(crit_capture_);
peahdf3efa82015-11-28 12:35:15 -080088
peahbfa97112016-03-10 21:09:04 -080089 std::vector<std::unique_ptr<GainController>> gain_controllers_;
90
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +020091 absl::optional<size_t> num_proc_channels_ RTC_GUARDED_BY(crit_capture_);
92 absl::optional<int> sample_rate_hz_ RTC_GUARDED_BY(crit_capture_);
peahb8fbb542016-03-15 02:28:08 -070093
peah135259a2016-10-28 03:12:11 -070094 static int instance_counter_;
peahbfa97112016-03-10 21:09:04 -080095 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(GainControlImpl);
niklase@google.com470e71d2011-07-07 08:21:25 +000096};
97} // namespace webrtc
98
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020099#endif // MODULES_AUDIO_PROCESSING_GAIN_CONTROL_IMPL_H_