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 | |
Phil Burk | 8f4fe50 | 2020-07-15 23:54:50 +0000 | [diff] [blame] | 55 | void writeDataIfRoom(int64_t mmapFramesRead, const void *buffer, int32_t numFrames); |
| 56 | |
Phil Burk | 523b304 | 2017-09-13 13:03:08 -0700 | [diff] [blame] | 57 | /** |
Phil Burk | 8f4fe50 | 2020-07-15 23:54:50 +0000 | [diff] [blame] | 58 | * This must only be called under getAudioDataQueueLock(). |
Phil Burk | 523b304 | 2017-09-13 13:03:08 -0700 | [diff] [blame] | 59 | * @return |
| 60 | */ |
Phil Burk | 0bd745e | 2020-10-17 18:20:01 +0000 | [diff] [blame] | 61 | std::shared_ptr<SharedRingBuffer> getAudioDataQueue_l() |
| 62 | REQUIRES(audioDataQueueLock) { |
| 63 | return mAudioDataQueue; |
Phil Burk | 8f4fe50 | 2020-07-15 23:54:50 +0000 | [diff] [blame] | 64 | } |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 65 | |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 66 | /* Keep a record of when a buffer transfer completed. |
| 67 | * This allows for a more accurate timing model. |
| 68 | */ |
Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 69 | void markTransferTime(Timestamp ×tamp); |
| 70 | |
| 71 | void setTimestampPositionOffset(int64_t deltaFrames) { |
| 72 | mTimestampPositionOffset.store(deltaFrames); |
| 73 | } |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 74 | |
Phil Burk | a5222e2 | 2017-07-28 13:31:14 -0700 | [diff] [blame] | 75 | void incrementXRunCount() { |
Phil Burk | 2329638 | 2017-11-20 15:45:11 -0800 | [diff] [blame] | 76 | sendXRunCount(++mXRunCount); |
Phil Burk | a5222e2 | 2017-07-28 13:31:14 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | int32_t getXRunCount() const { |
| 80 | return mXRunCount.load(); |
| 81 | } |
| 82 | |
Phil Burk | 19e990e | 2018-03-22 13:59:34 -0700 | [diff] [blame] | 83 | const char *getTypeText() const override { return "Shared"; } |
| 84 | |
Phil Burk | 0bd745e | 2020-10-17 18:20:01 +0000 | [diff] [blame] | 85 | // This is public so that the thread safety annotation, GUARDED_BY(), |
| 86 | // Can work when another object takes the lock. |
| 87 | mutable std::mutex audioDataQueueLock; |
| 88 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 89 | protected: |
| 90 | |
Phil Burk | 523b304 | 2017-09-13 13:03:08 -0700 | [diff] [blame] | 91 | aaudio_result_t getAudioDataDescription(AudioEndpointParcelable &parcelable) override; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 92 | |
| 93 | aaudio_result_t getFreeRunningPosition(int64_t *positionFrames, int64_t *timeNanos) override; |
| 94 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 95 | aaudio_result_t getHardwareTimestamp(int64_t *positionFrames, int64_t *timeNanos) override; |
Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 96 | |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 97 | /** |
| 98 | * @param requestedCapacityFrames |
| 99 | * @param framesPerBurst |
| 100 | * @return capacity or negative error |
| 101 | */ |
| 102 | static int32_t calculateBufferCapacity(int32_t requestedCapacityFrames, |
| 103 | int32_t framesPerBurst); |
| 104 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 105 | private: |
Phil Burk | 8f4fe50 | 2020-07-15 23:54:50 +0000 | [diff] [blame] | 106 | |
Phil Burk | 0bd745e | 2020-10-17 18:20:01 +0000 | [diff] [blame] | 107 | std::shared_ptr<SharedRingBuffer> mAudioDataQueue GUARDED_BY(audioDataQueueLock); |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 108 | |
Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 109 | std::atomic<int64_t> mTimestampPositionOffset; |
Phil Burk | a5222e2 | 2017-07-28 13:31:14 -0700 | [diff] [blame] | 110 | std::atomic<int32_t> mXRunCount; |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 111 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 112 | }; |
| 113 | |
| 114 | } /* namespace aaudio */ |
| 115 | |
| 116 | #endif //AAUDIO_AAUDIO_SERVICE_STREAM_SHARED_H |