Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 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 | 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 | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 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 | /** |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 73 | * Stop the flow of data after data in buffer has played. |
| 74 | */ |
| 75 | aaudio_result_t stop() override; |
| 76 | |
| 77 | /** |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 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 | |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 89 | /* Keep a record of when a buffer transfer completed. |
| 90 | * This allows for a more accurate timing model. |
| 91 | */ |
Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 92 | void markTransferTime(Timestamp ×tamp); |
| 93 | |
| 94 | void setTimestampPositionOffset(int64_t deltaFrames) { |
| 95 | mTimestampPositionOffset.store(deltaFrames); |
| 96 | } |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 97 | |
Phil Burk | a5222e2 | 2017-07-28 13:31:14 -0700 | [diff] [blame^] | 98 | void incrementXRunCount() { |
| 99 | mXRunCount++; |
| 100 | } |
| 101 | |
| 102 | int32_t getXRunCount() const { |
| 103 | return mXRunCount.load(); |
| 104 | } |
| 105 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 106 | protected: |
| 107 | |
| 108 | aaudio_result_t getDownDataDescription(AudioEndpointParcelable &parcelable) override; |
| 109 | |
| 110 | aaudio_result_t getFreeRunningPosition(int64_t *positionFrames, int64_t *timeNanos) override; |
| 111 | |
Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 112 | virtual aaudio_result_t getHardwareTimestamp(int64_t *positionFrames, |
| 113 | int64_t *timeNanos) override; |
| 114 | |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 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 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 123 | private: |
| 124 | android::AAudioService &mAudioService; |
| 125 | AAudioServiceEndpoint *mServiceEndpoint = nullptr; |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 126 | SharedRingBuffer *mAudioDataQueue = nullptr; |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 127 | |
Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 128 | std::atomic<int64_t> mTimestampPositionOffset; |
Phil Burk | a5222e2 | 2017-07-28 13:31:14 -0700 | [diff] [blame^] | 129 | std::atomic<int32_t> mXRunCount; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 130 | }; |
| 131 | |
| 132 | } /* namespace aaudio */ |
| 133 | |
| 134 | #endif //AAUDIO_AAUDIO_SERVICE_STREAM_SHARED_H |