blob: 1af6d7b246e8048f066f10af7325edfc47e040a5 [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
bjornv@webrtc.org0c6f9312012-01-30 09:39:08 +000011#ifndef WEBRTC_MODULES_AUDIO_PROCESSING_GAIN_CONTROL_IMPL_H_
12#define WEBRTC_MODULES_AUDIO_PROCESSING_GAIN_CONTROL_IMPL_H_
niklase@google.com470e71d2011-07-07 08:21:25 +000013
kwiberg88788ad2016-02-19 07:04:49 -080014#include <memory>
niklase@google.com470e71d2011-07-07 08:21:25 +000015#include <vector>
16
peahbfa97112016-03-10 21:09:04 -080017#include "webrtc/base/constructormagic.h"
peahdf3efa82015-11-28 12:35:15 -080018#include "webrtc/base/criticalsection.h"
terelius85fa7d52016-03-24 01:51:52 -070019#include "webrtc/base/swap_queue.h"
peahdf3efa82015-11-28 12:35:15 -080020#include "webrtc/base/thread_annotations.h"
pbos@webrtc.org7fad4b82013-05-28 08:11:59 +000021#include "webrtc/modules/audio_processing/include/audio_processing.h"
peah737f4b82016-03-10 23:05:28 -080022#include "webrtc/modules/audio_processing/render_queue_item_verifier.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000023
24namespace webrtc {
andrew@webrtc.org56e4a052014-02-27 22:23:17 +000025
niklase@google.com470e71d2011-07-07 08:21:25 +000026class AudioBuffer;
27
peahbfa97112016-03-10 21:09:04 -080028class GainControlImpl : public GainControl {
niklase@google.com470e71d2011-07-07 08:21:25 +000029 public:
peahb8fbb542016-03-15 02:28:08 -070030 GainControlImpl(rtc::CriticalSection* crit_render,
peahdf3efa82015-11-28 12:35:15 -080031 rtc::CriticalSection* crit_capture);
peahbfa97112016-03-10 21:09:04 -080032 ~GainControlImpl() override;
niklase@google.com470e71d2011-07-07 08:21:25 +000033
34 int ProcessRenderAudio(AudioBuffer* audio);
35 int AnalyzeCaptureAudio(AudioBuffer* audio);
peahb8fbb542016-03-15 02:28:08 -070036 int ProcessCaptureAudio(AudioBuffer* audio, bool stream_has_echo);
niklase@google.com470e71d2011-07-07 08:21:25 +000037
peahb8fbb542016-03-15 02:28:08 -070038 void Initialize(size_t num_proc_channels, int sample_rate_hz);
niklase@google.com470e71d2011-07-07 08:21:25 +000039
40 // GainControl implementation.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000041 bool is_enabled() const override;
42 int stream_analog_level() override;
Minyue13b96ba2015-10-03 00:39:14 +020043 bool is_limiter_enabled() const override;
44 Mode mode() const override;
niklase@google.com470e71d2011-07-07 08:21:25 +000045
peah4d291f72015-11-16 23:52:25 -080046 // Reads render side data that has been queued on the render call.
47 void ReadQueuedRenderData();
48
aluebs11d4a422016-04-28 14:58:32 -070049 int compression_gain_db() const override;
50
niklase@google.com470e71d2011-07-07 08:21:25 +000051 private:
peahbfa97112016-03-10 21:09:04 -080052 class GainController;
53
niklase@google.com470e71d2011-07-07 08:21:25 +000054 // GainControl implementation.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000055 int Enable(bool enable) override;
56 int set_stream_analog_level(int level) override;
57 int set_mode(Mode mode) override;
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000058 int set_target_level_dbfs(int level) override;
59 int target_level_dbfs() const override;
60 int set_compression_gain_db(int gain) override;
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000061 int enable_limiter(bool enable) override;
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000062 int set_analog_level_limits(int minimum, int maximum) override;
63 int analog_level_minimum() const override;
64 int analog_level_maximum() const override;
65 bool stream_is_saturated() const override;
niklase@google.com470e71d2011-07-07 08:21:25 +000066
peah4d291f72015-11-16 23:52:25 -080067 void AllocateRenderQueue();
peahbfa97112016-03-10 21:09:04 -080068 int Configure();
peah4d291f72015-11-16 23:52:25 -080069
peahdf3efa82015-11-28 12:35:15 -080070 rtc::CriticalSection* const crit_render_ ACQUIRED_BEFORE(crit_capture_);
71 rtc::CriticalSection* const crit_capture_;
72
peahbfa97112016-03-10 21:09:04 -080073 bool enabled_ = false;
74
peahdf3efa82015-11-28 12:35:15 -080075 Mode mode_ GUARDED_BY(crit_capture_);
76 int minimum_capture_level_ GUARDED_BY(crit_capture_);
77 int maximum_capture_level_ GUARDED_BY(crit_capture_);
78 bool limiter_enabled_ GUARDED_BY(crit_capture_);
79 int target_level_dbfs_ GUARDED_BY(crit_capture_);
80 int compression_gain_db_ GUARDED_BY(crit_capture_);
peahdf3efa82015-11-28 12:35:15 -080081 int analog_capture_level_ GUARDED_BY(crit_capture_);
82 bool was_analog_level_set_ GUARDED_BY(crit_capture_);
83 bool stream_is_saturated_ GUARDED_BY(crit_capture_);
84
85 size_t render_queue_element_max_size_ GUARDED_BY(crit_render_)
86 GUARDED_BY(crit_capture_);
87 std::vector<int16_t> render_queue_buffer_ GUARDED_BY(crit_render_);
88 std::vector<int16_t> capture_queue_buffer_ GUARDED_BY(crit_capture_);
89
90 // Lock protection not needed.
kwiberg88788ad2016-02-19 07:04:49 -080091 std::unique_ptr<
peah4d291f72015-11-16 23:52:25 -080092 SwapQueue<std::vector<int16_t>, RenderQueueItemVerifier<int16_t>>>
93 render_signal_queue_;
peahbfa97112016-03-10 21:09:04 -080094
95 std::vector<std::unique_ptr<GainController>> gain_controllers_;
96
peahb8fbb542016-03-15 02:28:08 -070097 rtc::Optional<size_t> num_proc_channels_ GUARDED_BY(crit_capture_);
98 rtc::Optional<int> sample_rate_hz_ GUARDED_BY(crit_capture_);
99
peahbfa97112016-03-10 21:09:04 -0800100 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(GainControlImpl);
niklase@google.com470e71d2011-07-07 08:21:25 +0000101};
102} // namespace webrtc
103
bjornv@webrtc.org0c6f9312012-01-30 09:39:08 +0000104#endif // WEBRTC_MODULES_AUDIO_PROCESSING_GAIN_CONTROL_IMPL_H_