1 // Copyright (C) 2021 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #pragma once
16 
17 #include "BusOutputStream.h"
18 
19 #include <time.h>
20 
21 namespace audio_proxy::service {
22 
23 // Impl of BusOutputStream which has a small buffer and consumes the audio data
24 // in real time.
25 class DummyBusOutputStream : public BusOutputStream {
26  public:
27   DummyBusOutputStream(const std::string& address,
28                        const AidlAudioConfig& config, int32_t flags);
29   ~DummyBusOutputStream() override;
30 
31   bool standby() override;
32   bool pause() override;
33   bool resume() override;
34   bool drain(AidlAudioDrain drain) override;
35   bool flush() override;
36   bool close() override;
37   bool setVolume(float left, float right) override;
38 
39   size_t availableToWrite() override;
40   AidlWriteStatus writeRingBuffer(const uint8_t* firstMem, size_t firstLength,
41                                   const uint8_t* secondMem,
42                                   size_t secondLength) override;
43 
44   bool start() override;
45   bool stop() override;
46   AidlMmapBufferInfo createMmapBuffer(int32_t minBufferSizeFrames) override;
47   AidlPresentationPosition getMmapPosition() override;
48 
49  protected:
50   bool prepareForWritingImpl(uint32_t frameSize, uint32_t frameCount) override;
51 
52  private:
53   // Buffer capacity.
54   int64_t mMaxBufferUs = 0;
55 
56   // Timestamp for the first played frame. Underrun will reset it.
57   timespec mStartTime = {0, 0};
58 
59   // Total written buffer size in us after `mStartTime` reset.
60   int64_t mInputUsSinceStart = 0;
61 
62   // Total played buffer size in us before underrun.
63   int64_t mPlayedUsBeforeUnderrun = 0;
64 };
65 
66 }  // namespace audio_proxy::service