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_SOUND_SYSTEM_ECHO_H_ 18 #define LB2_SOUND_SYSTEM_ECHO_H_ 19 20 #include <atomic> 21 #include <memory> 22 #include <thread> 23 24 #include <audio_utils/fifo.h> 25 26 #include "lb2/sound_system.h" 27 #include "lb2/test_context.h" 28 29 // Simplest implementation of a sound system that echoes written data 30 // back to the reader. This represents an ideal model of a physical loopback dongle. 31 class SoundSystemEcho : public SoundSystem { 32 public: 33 SoundSystemEcho(const TestContext *testCtx); 34 SoundSystemEcho(const SoundSystemEcho&) = delete; 35 SoundSystemEcho& operator=(const SoundSystemEcho&) = delete; 36 virtual ~SoundSystemEcho(); 37 38 bool init(WriteCallback callback) override; 39 bool drainInput() override; 40 ssize_t readAudio(AudioBufferView<sample_t> buffer) override; 41 void shutdown() override; 42 43 private: 44 static int calculateMsecPerBuffer(const TestContext *testCtx); 45 46 void startThread(); 47 void stopThread(); 48 void threadLoop(); 49 50 const TestContext* mTestCtx; 51 std::unique_ptr<sample_t[]> mFifoData; 52 struct audio_utils_fifo mFifo; 53 const int mMsecPerBuffer; 54 WriteCallback mWriteCallback; // accessed by mThread 55 std::atomic<bool> mThreadRunning; // accessed by mThread 56 std::unique_ptr<std::thread> mThread; 57 }; 58 59 #endif // LB2_SOUND_SYSTEM_ECHO_H_ 60