AAudioServiceStreamShared.h revision a5222e298c294781067950a7cc319d3278fa845a
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 AAUDIO_AAUDIO_SERVICE_STREAM_SHARED_H 18#define AAUDIO_AAUDIO_SERVICE_STREAM_SHARED_H 19 20#include "fifo/FifoBuffer.h" 21#include "binding/AAudioServiceMessage.h" 22#include "binding/AAudioStreamRequest.h" 23#include "binding/AAudioStreamConfiguration.h" 24 25#include "AAudioService.h" 26#include "AAudioServiceStreamBase.h" 27 28namespace aaudio { 29 30// We expect the queue to only have a few commands. 31// This should be way more than we need. 32#define QUEUE_UP_CAPACITY_COMMANDS (128) 33 34class AAudioEndpointManager; 35class AAudioServiceEndpoint; 36class SharedRingBuffer; 37 38/** 39 * One of these is created for every MODE_SHARED stream in the AAudioService. 40 * 41 * Each Shared stream will register itself with an AAudioServiceEndpoint when it is opened. 42 */ 43class AAudioServiceStreamShared : public AAudioServiceStreamBase { 44 45public: 46 AAudioServiceStreamShared(android::AAudioService &aAudioService); 47 virtual ~AAudioServiceStreamShared() = default; 48 49 static std::string dumpHeader(); 50 51 std::string dump() const override; 52 53 aaudio_result_t open(const aaudio::AAudioStreamRequest &request, 54 aaudio::AAudioStreamConfiguration &configurationOutput) override; 55 56 /** 57 * Start the flow of audio data. 58 * 59 * This is not guaranteed to be synchronous but it currently is. 60 * An AAUDIO_SERVICE_EVENT_STARTED will be sent to the client when complete. 61 */ 62 aaudio_result_t start() override; 63 64 /** 65 * Stop the flow of data so that start() can resume without loss of data. 66 * 67 * This is not guaranteed to be synchronous but it currently is. 68 * An AAUDIO_SERVICE_EVENT_PAUSED will be sent to the client when complete. 69 */ 70 aaudio_result_t pause() override; 71 72 /** 73 * Stop the flow of data after data in buffer has played. 74 */ 75 aaudio_result_t stop() override; 76 77 /** 78 * Discard any data held by the underlying HAL or Service. 79 * 80 * This is not guaranteed to be synchronous but it currently is. 81 * An AAUDIO_SERVICE_EVENT_FLUSHED will be sent to the client when complete. 82 */ 83 aaudio_result_t flush() override; 84 85 aaudio_result_t close() override; 86 87 android::FifoBuffer *getDataFifoBuffer() { return mAudioDataQueue->getFifoBuffer(); } 88 89 /* Keep a record of when a buffer transfer completed. 90 * This allows for a more accurate timing model. 91 */ 92 void markTransferTime(Timestamp ×tamp); 93 94 void setTimestampPositionOffset(int64_t deltaFrames) { 95 mTimestampPositionOffset.store(deltaFrames); 96 } 97 98 void incrementXRunCount() { 99 mXRunCount++; 100 } 101 102 int32_t getXRunCount() const { 103 return mXRunCount.load(); 104 } 105 106protected: 107 108 aaudio_result_t getDownDataDescription(AudioEndpointParcelable &parcelable) override; 109 110 aaudio_result_t getFreeRunningPosition(int64_t *positionFrames, int64_t *timeNanos) override; 111 112 virtual aaudio_result_t getHardwareTimestamp(int64_t *positionFrames, 113 int64_t *timeNanos) override; 114 115 /** 116 * @param requestedCapacityFrames 117 * @param framesPerBurst 118 * @return capacity or negative error 119 */ 120 static int32_t calculateBufferCapacity(int32_t requestedCapacityFrames, 121 int32_t framesPerBurst); 122 123private: 124 android::AAudioService &mAudioService; 125 AAudioServiceEndpoint *mServiceEndpoint = nullptr; 126 SharedRingBuffer *mAudioDataQueue = nullptr; 127 128 std::atomic<int64_t> mTimestampPositionOffset; 129 std::atomic<int32_t> mXRunCount; 130}; 131 132} /* namespace aaudio */ 133 134#endif //AAUDIO_AAUDIO_SERVICE_STREAM_SHARED_H 135