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/downsampled_render_buffer.h"
20 #include "modules/audio_processing/aec3/render_buffer.h"
21 
22 namespace webrtc {
23 
24 // Class for buffering the incoming render blocks such that these may be
25 // extracted with a specified delay.
26 class RenderDelayBuffer {
27  public:
28   enum class BufferingEvent {
29     kNone,
30     kRenderUnderrun,
31     kRenderOverrun,
32     kApiCallSkew
33   };
34 
35   static RenderDelayBuffer* Create(const EchoCanceller3Config& config,
36                                    int sample_rate_hz,
37                                    size_t num_render_channels);
38   virtual ~RenderDelayBuffer() = default;
39 
40   // Resets the buffer alignment.
41   virtual void Reset() = 0;
42 
43   // Inserts a block into the buffer.
44   virtual BufferingEvent Insert(
45       const std::vector<std::vector<std::vector<float>>>& 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   // Sets the buffer delay and returns a bool indicating whether the delay
52   // changed.
53   virtual bool AlignFromDelay(size_t delay) = 0;
54 
55   // Sets the buffer delay from the most recently reported external delay.
56   virtual void AlignFromExternalDelay() = 0;
57 
58   // Gets the buffer delay.
59   virtual size_t Delay() const = 0;
60 
61   // Gets the buffer delay.
62   virtual size_t MaxDelay() const = 0;
63 
64   // Returns the render buffer for the echo remover.
65   virtual RenderBuffer* GetRenderBuffer() = 0;
66 
67   // Returns the downsampled render buffer.
68   virtual const DownsampledRenderBuffer& GetDownsampledRenderBuffer() const = 0;
69 
70   // Returns the maximum non calusal offset that can occur in the delay buffer.
71   static int DelayEstimatorOffset(const EchoCanceller3Config& config);
72 
73   // Provides an optional external estimate of the audio buffer delay.
74   virtual void SetAudioBufferDelay(int delay_ms) = 0;
75 
76   // Returns whether an external delay estimate has been reported via
77   // SetAudioBufferDelay.
78   virtual bool HasReceivedBufferDelay() = 0;
79 };
80 
81 }  // namespace webrtc
82 
83 #endif  // MODULES_AUDIO_PROCESSING_AEC3_RENDER_DELAY_BUFFER_H_
84