• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef LB2_LOOPBACK_TEST_H_
18 #define LB2_LOOPBACK_TEST_H_
19 
20 #include <atomic>
21 #include <memory>
22 
23 #include <audio_utils/fifo.h>
24 
25 #include "lb2/audio_buffer.h"
26 #include "lb2/sound_system.h"
27 #include "lb2/test_context.h"
28 
29 // Generic test interface. The test is driven by the write callback
30 // of the sound system and periodic polling via 'collectRecording'
31 // method.
32 class LoopbackTest {
33   public:
34     LoopbackTest(SoundSystem* soundSys, TestContext* testCtx);
35     LoopbackTest(const LoopbackTest&) = delete;
36     LoopbackTest& operator=(const LoopbackTest&) = delete;
37     virtual ~LoopbackTest();
38 
39     virtual bool init();
40     virtual int collectRecording(AudioBufferView<double> buffer);
41 
42   protected:
43     // This method is called on the sound system callback thread.
44     void receiveRecording(size_t framesRead);
45 
46     SoundSystem* mSoundSys;
47     AudioBuffer<sample_t> mReadBuffer;
48 
49   private:
50     static constexpr size_t RECORDING_FIFO_FRAMES = 65536;
51     static constexpr size_t COLLECTION_LOOPS = 10;
52     static constexpr size_t COLLECTION_PERIOD_MS = 100;
53 
54     TestContext* mTestCtx;
55     std::unique_ptr<sample_t[]> mRecordingFifoData;
56     struct audio_utils_fifo mRecordingFifo;
57 };
58 
59 
60 // Latency test implementation. Using the parameters from the test
61 // context, first it drains the audio system read queue, then injects
62 // provided impulse, and then copies read audio input to output.
63 class LatencyTest : public LoopbackTest {
64   public:
65     LatencyTest(SoundSystem* soundSys, LatencyTestContext* testCtx);
66     LatencyTest(const LatencyTest&) = delete;
67     LatencyTest& operator=(const LatencyTest&) = delete;
68     virtual ~LatencyTest();
69 
70     bool init() override;
71 
72   private:
73     static constexpr size_t INITIAL_SILENCE_MS = 240;  // Approx. as in the legacy version.
74 
75     AudioBufferView<sample_t> writeCallback(size_t expectedFrames);
76 
77     //LatencyTestContext* mTestCtx;
78     int mDrainInput;
79     int mInputFramesToDiscard;
80     int mInitialSilenceFrameCount;
81     int mInjectImpulseNextFramePos;
82     AudioBufferView<sample_t> mImpulse;
83 };
84 
85 
86 // Glitch test implementation. Writes the test signal to output,
87 // and reads back input.
88 class GlitchTest : public LoopbackTest {
89   public:
90     GlitchTest(SoundSystem* soundSys, GlitchTestContext* testCtx);
91     GlitchTest(const GlitchTest&) = delete;
92     GlitchTest& operator=(const GlitchTest&) = delete;
93     virtual ~GlitchTest();
94 
95     bool init() override;
96 
97   private:
98     AudioBufferView<sample_t> writeCallback(size_t expectedFrames);
99 
100     GlitchTestContext* mTestCtx;
101 };
102 
103 #endif  // LB2_LOOPBACK_TEST_H_
104