• 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 #include "lb2/sound_system_echo.h"
18 
19 #include <chrono>
20 #include <functional>
21 
22 #define LOG_TAG "ss_echo"
23 #include "lb2/logging.h"
24 #include "lb2/util.h"
25 
SoundSystemEcho(const TestContext * testCtx)26 SoundSystemEcho::SoundSystemEcho(const TestContext *testCtx)
27         : mTestCtx(testCtx),
28           mFifoData(new sample_t[testCtx->getSampleCount()]),
29           mMsecPerBuffer(calculateMsecPerBuffer(testCtx)),
30           mThreadRunning(false) {
31     audio_utils_fifo_init(
32             &mFifo,
33             mTestCtx->getFrameCount(),
34             mTestCtx->getFrameSize(),
35             mFifoData.get());
36 }
37 
~SoundSystemEcho()38 SoundSystemEcho::~SoundSystemEcho() {
39     shutdown();
40     audio_utils_fifo_deinit(&mFifo);
41 }
42 
calculateMsecPerBuffer(const TestContext * testCtx)43 int SoundSystemEcho::calculateMsecPerBuffer(const TestContext *testCtx) {
44     return wholeMultiplier(MS_PER_SECOND * testCtx->getFrameCount(), testCtx->getSamplingRateHz());
45 }
46 
startThread()47 void SoundSystemEcho::startThread() {
48     mThreadRunning = true;
49     mThread.reset(new std::thread(std::bind(&SoundSystemEcho::threadLoop, this)));
50 }
51 
stopThread()52 void SoundSystemEcho::stopThread() {
53     mThreadRunning = false;
54     mThread->join();
55     mThread.reset();
56 }
57 
threadLoop()58 void SoundSystemEcho::threadLoop() {
59     while (mThreadRunning) {
60         AudioBufferView<sample_t> buffer = mWriteCallback(mTestCtx->getFrameCount());
61         // The FIFO will cut the data if it exceeds the buffer size.
62         audio_utils_fifo_write(&mFifo, buffer.getData(), buffer.getFrameCount());
63         std::this_thread::sleep_for(std::chrono::milliseconds(mMsecPerBuffer));
64     }
65 }
66 
init(WriteCallback callback)67 bool SoundSystemEcho::init(WriteCallback callback) {
68     if (mThreadRunning) {
69         shutdown();
70     }
71     mWriteCallback = callback;
72     startThread();
73     return true;
74 }
75 
drainInput()76 bool SoundSystemEcho::drainInput() {
77     AudioBuffer<sample_t> drainBuffer(
78             audio_utils_fifo_availToRead(&mFifo), mTestCtx->getChannelCount());
79     return audio_utils_fifo_read(&mFifo, drainBuffer.getData(), drainBuffer.getFrameCount()) >= 0;
80 }
81 
readAudio(AudioBufferView<sample_t> buffer)82 ssize_t SoundSystemEcho::readAudio(AudioBufferView<sample_t> buffer) {
83     std::this_thread::sleep_for(std::chrono::milliseconds(mMsecPerBuffer));
84     ssize_t result = audio_utils_fifo_read(&mFifo, buffer.getData(), buffer.getFrameCount());
85     if (result != 0) return result;
86     buffer.clear();
87     return buffer.getFrameCount();
88 }
89 
shutdown()90 void SoundSystemEcho::shutdown() {
91     if (!mThreadRunning) return;
92     stopThread();
93     mWriteCallback = nullptr;
94 }
95