xref: /aosp_15_r20/external/webrtc/modules/audio_processing/aec3/render_delay_buffer.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright (c) 2017 The WebRTC project authors. All Rights Reserved.
3  *
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 
11 #ifndef MODULES_AUDIO_PROCESSING_AEC3_RENDER_DELAY_BUFFER_H_
12 #define MODULES_AUDIO_PROCESSING_AEC3_RENDER_DELAY_BUFFER_H_
13 
14 #include <stddef.h>
15 
16 #include <vector>
17 
18 #include "api/audio/echo_canceller3_config.h"
19 #include "modules/audio_processing/aec3/block.h"
20 #include "modules/audio_processing/aec3/downsampled_render_buffer.h"
21 #include "modules/audio_processing/aec3/render_buffer.h"
22 
23 namespace webrtc {
24 
25 // Class for buffering the incoming render blocks such that these may be
26 // extracted with a specified delay.
27 class RenderDelayBuffer {
28  public:
29   enum class BufferingEvent {
30     kNone,
31     kRenderUnderrun,
32     kRenderOverrun,
33     kApiCallSkew
34   };
35 
36   static RenderDelayBuffer* Create(const EchoCanceller3Config& config,
37                                    int sample_rate_hz,
38                                    size_t num_render_channels);
39   virtual ~RenderDelayBuffer() = default;
40 
41   // Resets the buffer alignment.
42   virtual void Reset() = 0;
43 
44   // Inserts a block into the buffer.
45   virtual BufferingEvent Insert(const Block& block) = 0;
46 
47   // Updates the buffers one step based on the specified buffer delay. Returns
48   // an enum indicating whether there was a special event that occurred.
49   virtual BufferingEvent PrepareCaptureProcessing() = 0;
50 
51   // Called on capture blocks where PrepareCaptureProcessing is not called.
52   virtual void HandleSkippedCaptureProcessing() = 0;
53 
54   // Sets the buffer delay and returns a bool indicating whether the delay
55   // changed.
56   virtual bool AlignFromDelay(size_t delay) = 0;
57 
58   // Sets the buffer delay from the most recently reported external delay.
59   virtual void AlignFromExternalDelay() = 0;
60 
61   // Gets the buffer delay.
62   virtual size_t Delay() const = 0;
63 
64   // Gets the buffer delay.
65   virtual size_t MaxDelay() const = 0;
66 
67   // Returns the render buffer for the echo remover.
68   virtual RenderBuffer* GetRenderBuffer() = 0;
69 
70   // Returns the downsampled render buffer.
71   virtual const DownsampledRenderBuffer& GetDownsampledRenderBuffer() const = 0;
72 
73   // Returns the maximum non calusal offset that can occur in the delay buffer.
74   static int DelayEstimatorOffset(const EchoCanceller3Config& config);
75 
76   // Provides an optional external estimate of the audio buffer delay.
77   virtual void SetAudioBufferDelay(int delay_ms) = 0;
78 
79   // Returns whether an external delay estimate has been reported via
80   // SetAudioBufferDelay.
81   virtual bool HasReceivedBufferDelay() = 0;
82 };
83 
84 }  // namespace webrtc
85 
86 #endif  // MODULES_AUDIO_PROCESSING_AEC3_RENDER_DELAY_BUFFER_H_
87