blob: b531de98bb165a2c3cf234c7b84df5541f6d7d41 [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
14#include <vector>
15
peahdf3efa82015-11-28 12:35:15 -080016#include "webrtc/base/criticalsection.h"
peah4d291f72015-11-16 23:52:25 -080017#include "webrtc/base/scoped_ptr.h"
peahdf3efa82015-11-28 12:35:15 -080018#include "webrtc/base/thread_annotations.h"
peah4d291f72015-11-16 23:52:25 -080019#include "webrtc/common_audio/swap_queue.h"
pbos@webrtc.org7fad4b82013-05-28 08:11:59 +000020#include "webrtc/modules/audio_processing/include/audio_processing.h"
21#include "webrtc/modules/audio_processing/processing_component.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000022
23namespace webrtc {
andrew@webrtc.org56e4a052014-02-27 22:23:17 +000024
niklase@google.com470e71d2011-07-07 08:21:25 +000025class AudioBuffer;
26
27class GainControlImpl : public GainControl,
28 public ProcessingComponent {
29 public:
andrew@webrtc.org56e4a052014-02-27 22:23:17 +000030 GainControlImpl(const AudioProcessing* apm,
peahdf3efa82015-11-28 12:35:15 -080031 rtc::CriticalSection* crit_render,
32 rtc::CriticalSection* crit_capture);
niklase@google.com470e71d2011-07-07 08:21:25 +000033 virtual ~GainControlImpl();
34
35 int ProcessRenderAudio(AudioBuffer* audio);
36 int AnalyzeCaptureAudio(AudioBuffer* audio);
37 int ProcessCaptureAudio(AudioBuffer* audio);
38
39 // ProcessingComponent implementation.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000040 int Initialize() override;
niklase@google.com470e71d2011-07-07 08:21:25 +000041
42 // GainControl implementation.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000043 bool is_enabled() const override;
44 int stream_analog_level() override;
Minyue13b96ba2015-10-03 00:39:14 +020045 bool is_limiter_enabled() const override;
46 Mode mode() const override;
niklase@google.com470e71d2011-07-07 08:21:25 +000047
peah4d291f72015-11-16 23:52:25 -080048 // Reads render side data that has been queued on the render call.
49 void ReadQueuedRenderData();
50
niklase@google.com470e71d2011-07-07 08:21:25 +000051 private:
52 // GainControl implementation.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000053 int Enable(bool enable) override;
54 int set_stream_analog_level(int level) override;
55 int set_mode(Mode mode) override;
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000056 int set_target_level_dbfs(int level) override;
57 int target_level_dbfs() const override;
58 int set_compression_gain_db(int gain) override;
59 int compression_gain_db() const override;
60 int enable_limiter(bool enable) override;
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000061 int set_analog_level_limits(int minimum, int maximum) override;
62 int analog_level_minimum() const override;
63 int analog_level_maximum() const override;
64 bool stream_is_saturated() const override;
niklase@google.com470e71d2011-07-07 08:21:25 +000065
66 // ProcessingComponent implementation.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000067 void* CreateHandle() const override;
68 int InitializeHandle(void* handle) const override;
69 int ConfigureHandle(void* handle) const override;
70 void DestroyHandle(void* handle) const override;
71 int num_handles_required() const override;
72 int GetHandleError(void* handle) const override;
niklase@google.com470e71d2011-07-07 08:21:25 +000073
peah4d291f72015-11-16 23:52:25 -080074 void AllocateRenderQueue();
75
peahdf3efa82015-11-28 12:35:15 -080076 // Not guarded as its public API is thread safe.
andrew@webrtc.org56e4a052014-02-27 22:23:17 +000077 const AudioProcessing* apm_;
peah4d291f72015-11-16 23:52:25 -080078
peahdf3efa82015-11-28 12:35:15 -080079 rtc::CriticalSection* const crit_render_ ACQUIRED_BEFORE(crit_capture_);
80 rtc::CriticalSection* const crit_capture_;
81
82 Mode mode_ GUARDED_BY(crit_capture_);
83 int minimum_capture_level_ GUARDED_BY(crit_capture_);
84 int maximum_capture_level_ GUARDED_BY(crit_capture_);
85 bool limiter_enabled_ GUARDED_BY(crit_capture_);
86 int target_level_dbfs_ GUARDED_BY(crit_capture_);
87 int compression_gain_db_ GUARDED_BY(crit_capture_);
88 std::vector<int> capture_levels_ GUARDED_BY(crit_capture_);
89 int analog_capture_level_ GUARDED_BY(crit_capture_);
90 bool was_analog_level_set_ GUARDED_BY(crit_capture_);
91 bool stream_is_saturated_ GUARDED_BY(crit_capture_);
92
93 size_t render_queue_element_max_size_ GUARDED_BY(crit_render_)
94 GUARDED_BY(crit_capture_);
95 std::vector<int16_t> render_queue_buffer_ GUARDED_BY(crit_render_);
96 std::vector<int16_t> capture_queue_buffer_ GUARDED_BY(crit_capture_);
97
98 // Lock protection not needed.
peah4d291f72015-11-16 23:52:25 -080099 rtc::scoped_ptr<
100 SwapQueue<std::vector<int16_t>, RenderQueueItemVerifier<int16_t>>>
101 render_signal_queue_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000102};
103} // namespace webrtc
104
bjornv@webrtc.org0c6f9312012-01-30 09:39:08 +0000105#endif // WEBRTC_MODULES_AUDIO_PROCESSING_GAIN_CONTROL_IMPL_H_