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 28 namespace 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 34 class AAudioEndpointManager; 35 class AAudioServiceEndpoint; 36 class 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 */ 43 class AAudioServiceStreamShared : public AAudioServiceStreamBase { 44 45 public: 46 explicit 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) override; 54 55 aaudio_result_t close_l() override; 56 57 /** 58 * This must be locked when calling getAudioDataFifoBuffer_l() and while 59 * using the FifoBuffer it returns. 60 */ getAudioDataQueueLock()61 std::mutex &getAudioDataQueueLock() { 62 return mAudioDataQueueLock; 63 } 64 65 /** 66 * This must only be call under getAudioDataQueueLock(). 67 * @return 68 */ getAudioDataFifoBuffer_l()69 android::FifoBuffer *getAudioDataFifoBuffer_l() { return (mAudioDataQueue == nullptr) 70 ? nullptr 71 : mAudioDataQueue->getFifoBuffer(); } 72 73 /* Keep a record of when a buffer transfer completed. 74 * This allows for a more accurate timing model. 75 */ 76 void markTransferTime(Timestamp ×tamp); 77 setTimestampPositionOffset(int64_t deltaFrames)78 void setTimestampPositionOffset(int64_t deltaFrames) { 79 mTimestampPositionOffset.store(deltaFrames); 80 } 81 incrementXRunCount()82 void incrementXRunCount() { 83 sendXRunCount(++mXRunCount); 84 } 85 getXRunCount()86 int32_t getXRunCount() const { 87 return mXRunCount.load(); 88 } 89 getTypeText()90 const char *getTypeText() const override { return "Shared"; } 91 92 protected: 93 94 aaudio_result_t getAudioDataDescription(AudioEndpointParcelable &parcelable) override; 95 96 aaudio_result_t getFreeRunningPosition(int64_t *positionFrames, int64_t *timeNanos) override; 97 98 aaudio_result_t getHardwareTimestamp(int64_t *positionFrames, int64_t *timeNanos) override; 99 100 /** 101 * @param requestedCapacityFrames 102 * @param framesPerBurst 103 * @return capacity or negative error 104 */ 105 static int32_t calculateBufferCapacity(int32_t requestedCapacityFrames, 106 int32_t framesPerBurst); 107 108 private: 109 SharedRingBuffer *mAudioDataQueue = nullptr; // protected by mAudioDataQueueLock 110 std::mutex mAudioDataQueueLock; 111 112 std::atomic<int64_t> mTimestampPositionOffset; 113 std::atomic<int32_t> mXRunCount; 114 115 }; 116 117 } /* namespace aaudio */ 118 119 #endif //AAUDIO_AAUDIO_SERVICE_STREAM_SHARED_H 120