Phil Burk | 2329638 | 2017-11-20 15:45:11 -0800 | [diff] [blame] | 1 | /* |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 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: |
Phil Burk | 19e990e | 2018-03-22 13:59:34 -0700 | [diff] [blame^] | 46 | explicit AAudioServiceStreamShared(android::AAudioService &aAudioService); |
Phil Burk | 98d6d92 | 2017-07-06 11:52:45 -0700 | [diff] [blame] | 47 | virtual ~AAudioServiceStreamShared() = default; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 48 | |
Phil Burk | a5222e2 | 2017-07-28 13:31:14 -0700 | [diff] [blame] | 49 | static std::string dumpHeader(); |
| 50 | |
| 51 | std::string dump() const override; |
| 52 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 53 | aaudio_result_t open(const aaudio::AAudioStreamRequest &request) override; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 54 | |
| 55 | aaudio_result_t close() override; |
| 56 | |
Phil Burk | 523b304 | 2017-09-13 13:03:08 -0700 | [diff] [blame] | 57 | /** |
| 58 | * This must be locked when calling getAudioDataFifoBuffer_l() and while |
| 59 | * using the FifoBuffer it returns. |
| 60 | */ |
| 61 | std::mutex &getAudioDataQueueLock() { |
| 62 | return mAudioDataQueueLock; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * This must only be call under getAudioDataQueueLock(). |
| 67 | * @return |
| 68 | */ |
| 69 | android::FifoBuffer *getAudioDataFifoBuffer_l() { return (mAudioDataQueue == nullptr) |
| 70 | ? nullptr |
| 71 | : mAudioDataQueue->getFifoBuffer(); } |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 72 | |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 73 | /* Keep a record of when a buffer transfer completed. |
| 74 | * This allows for a more accurate timing model. |
| 75 | */ |
Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 76 | void markTransferTime(Timestamp ×tamp); |
| 77 | |
| 78 | void setTimestampPositionOffset(int64_t deltaFrames) { |
| 79 | mTimestampPositionOffset.store(deltaFrames); |
| 80 | } |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 81 | |
Phil Burk | a5222e2 | 2017-07-28 13:31:14 -0700 | [diff] [blame] | 82 | void incrementXRunCount() { |
Phil Burk | 2329638 | 2017-11-20 15:45:11 -0800 | [diff] [blame] | 83 | sendXRunCount(++mXRunCount); |
Phil Burk | a5222e2 | 2017-07-28 13:31:14 -0700 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | int32_t getXRunCount() const { |
| 87 | return mXRunCount.load(); |
| 88 | } |
| 89 | |
Phil Burk | 19e990e | 2018-03-22 13:59:34 -0700 | [diff] [blame^] | 90 | const char *getTypeText() const override { return "Shared"; } |
| 91 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 92 | protected: |
| 93 | |
Phil Burk | 523b304 | 2017-09-13 13:03:08 -0700 | [diff] [blame] | 94 | aaudio_result_t getAudioDataDescription(AudioEndpointParcelable &parcelable) override; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 95 | |
| 96 | aaudio_result_t getFreeRunningPosition(int64_t *positionFrames, int64_t *timeNanos) override; |
| 97 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 98 | aaudio_result_t getHardwareTimestamp(int64_t *positionFrames, int64_t *timeNanos) override; |
Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 99 | |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 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 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 108 | private: |
Phil Burk | 523b304 | 2017-09-13 13:03:08 -0700 | [diff] [blame] | 109 | SharedRingBuffer *mAudioDataQueue = nullptr; // protected by mAudioDataQueueLock |
| 110 | std::mutex mAudioDataQueueLock; |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 111 | |
Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 112 | std::atomic<int64_t> mTimestampPositionOffset; |
Phil Burk | a5222e2 | 2017-07-28 13:31:14 -0700 | [diff] [blame] | 113 | std::atomic<int32_t> mXRunCount; |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 114 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 115 | }; |
| 116 | |
| 117 | } /* namespace aaudio */ |
| 118 | |
| 119 | #endif //AAUDIO_AAUDIO_SERVICE_STREAM_SHARED_H |