Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 17 | #ifndef AAUDIO_AAUDIO_SERVICE_STREAM_BASE_H |
| 18 | #define AAUDIO_AAUDIO_SERVICE_STREAM_BASE_H |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 19 | |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 20 | #include <assert.h> |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 21 | #include <mutex> |
Phil Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame] | 22 | |
Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 23 | #include <media/AudioClient.h> |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 24 | #include <utils/RefBase.h> |
| 25 | |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 26 | #include "fifo/FifoBuffer.h" |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 27 | #include "binding/IAAudioService.h" |
| 28 | #include "binding/AudioEndpointParcelable.h" |
| 29 | #include "binding/AAudioServiceMessage.h" |
| 30 | #include "utility/AAudioUtilities.h" |
Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 31 | #include "utility/AudioClock.h" |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 32 | |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 33 | #include "SharedRingBuffer.h" |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 34 | #include "AAudioThread.h" |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 35 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 36 | namespace aaudio { |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 37 | |
| 38 | // We expect the queue to only have a few commands. |
| 39 | // This should be way more than we need. |
| 40 | #define QUEUE_UP_CAPACITY_COMMANDS (128) |
| 41 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 42 | /** |
| 43 | * Base class for a stream in the AAudio service. |
| 44 | */ |
| 45 | class AAudioServiceStreamBase |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 46 | : public virtual android::RefBase |
| 47 | , public Runnable { |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 48 | |
| 49 | public: |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 50 | AAudioServiceStreamBase(); |
| 51 | virtual ~AAudioServiceStreamBase(); |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 52 | |
| 53 | enum { |
| 54 | ILLEGAL_THREAD_ID = 0 |
| 55 | }; |
| 56 | |
Phil Burk | a5222e2 | 2017-07-28 13:31:14 -0700 | [diff] [blame^] | 57 | static std::string dumpHeader(); |
| 58 | |
| 59 | // does not include EOL |
| 60 | virtual std::string dump() const; |
Phil Burk | 4501b35 | 2017-06-29 18:12:36 -0700 | [diff] [blame] | 61 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 62 | // ------------------------------------------------------------------- |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 63 | /** |
| 64 | * Open the device. |
| 65 | */ |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 66 | virtual aaudio_result_t open(const aaudio::AAudioStreamRequest &request, |
| 67 | aaudio::AAudioStreamConfiguration &configurationOutput) = 0; |
| 68 | |
| 69 | virtual aaudio_result_t close(); |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 70 | |
| 71 | /** |
| 72 | * Start the flow of data. |
| 73 | */ |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 74 | virtual aaudio_result_t start(); |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 75 | |
| 76 | /** |
| 77 | * Stop the flow of data such that start() can resume with loss of data. |
| 78 | */ |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 79 | virtual aaudio_result_t pause(); |
| 80 | |
| 81 | /** |
| 82 | * Stop the flow of data after data in buffer has played. |
| 83 | */ |
| 84 | virtual aaudio_result_t stop(); |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 85 | |
Phil Burk | 98d6d92 | 2017-07-06 11:52:45 -0700 | [diff] [blame] | 86 | aaudio_result_t stopTimestampThread(); |
| 87 | |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 88 | /** |
| 89 | * Discard any data held by the underlying HAL or Service. |
| 90 | */ |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 91 | virtual aaudio_result_t flush(); |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 92 | |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 93 | virtual aaudio_result_t startClient(const android::AudioClient& client __unused, |
| 94 | audio_port_handle_t *clientHandle __unused) { |
| 95 | return AAUDIO_ERROR_UNAVAILABLE; |
| 96 | } |
| 97 | |
| 98 | virtual aaudio_result_t stopClient(audio_port_handle_t clientHandle __unused) { |
| 99 | return AAUDIO_ERROR_UNAVAILABLE; |
| 100 | } |
| 101 | |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 102 | bool isRunning() const { |
| 103 | return mState == AAUDIO_STREAM_STATE_STARTED; |
| 104 | } |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 105 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 106 | // ------------------------------------------------------------------- |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 107 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 108 | /** |
| 109 | * Send a message to the client. |
| 110 | */ |
| 111 | aaudio_result_t sendServiceEvent(aaudio_service_event_t event, |
| 112 | double dataDouble = 0.0, |
| 113 | int64_t dataLong = 0); |
Phil Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame] | 114 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 115 | /** |
| 116 | * Fill in a parcelable description of stream. |
| 117 | */ |
| 118 | aaudio_result_t getDescription(AudioEndpointParcelable &parcelable); |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 119 | |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 120 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 121 | void setRegisteredThread(pid_t pid) { |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 122 | mRegisteredClientThread = pid; |
| 123 | } |
Phil Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame] | 124 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 125 | pid_t getRegisteredThread() const { |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 126 | return mRegisteredClientThread; |
| 127 | } |
| 128 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 129 | int32_t getFramesPerBurst() const { |
| 130 | return mFramesPerBurst; |
| 131 | } |
| 132 | |
| 133 | int32_t calculateBytesPerFrame() const { |
| 134 | return mSamplesPerFrame * AAudioConvert_formatToSizeInBytes(mAudioFormat); |
| 135 | } |
| 136 | |
| 137 | void run() override; // to implement Runnable |
| 138 | |
Phil Burk | 5ef003b | 2017-06-30 11:43:37 -0700 | [diff] [blame] | 139 | void disconnect(); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 140 | |
Phil Burk | 2ac035f | 2017-06-23 14:51:14 -0700 | [diff] [blame] | 141 | uid_t getOwnerUserId() const { |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 142 | return mMmapClient.clientUid; |
Phil Burk | 2ac035f | 2017-06-23 14:51:14 -0700 | [diff] [blame] | 143 | } |
| 144 | |
Phil Burk | b63320a | 2017-06-30 10:28:20 -0700 | [diff] [blame] | 145 | pid_t getOwnerProcessId() const { |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 146 | return mMmapClient.clientPid; |
Phil Burk | b63320a | 2017-06-30 10:28:20 -0700 | [diff] [blame] | 147 | } |
| 148 | |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 149 | aaudio_handle_t getHandle() const { |
| 150 | return mHandle; |
| 151 | } |
| 152 | void setHandle(aaudio_handle_t handle) { |
| 153 | mHandle = handle; |
| 154 | } |
| 155 | |
Phil Burk | 5a26e66 | 2017-07-07 12:44:48 -0700 | [diff] [blame] | 156 | aaudio_stream_state_t getState() const { |
| 157 | return mState; |
| 158 | } |
| 159 | |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 160 | protected: |
Phil Burk | 98d6d92 | 2017-07-06 11:52:45 -0700 | [diff] [blame] | 161 | |
Phil Burk | 5a26e66 | 2017-07-07 12:44:48 -0700 | [diff] [blame] | 162 | void setState(aaudio_stream_state_t state) { |
| 163 | mState = state; |
| 164 | } |
| 165 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 166 | aaudio_result_t writeUpMessageQueue(AAudioServiceMessage *command); |
| 167 | |
| 168 | aaudio_result_t sendCurrentTimestamp(); |
| 169 | |
Phil Burk | 940083c | 2017-07-17 17:00:02 -0700 | [diff] [blame] | 170 | /** |
| 171 | * @param positionFrames |
| 172 | * @param timeNanos |
| 173 | * @return AAUDIO_OK or AAUDIO_ERROR_UNAVAILABLE or other negative error |
| 174 | */ |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 175 | virtual aaudio_result_t getFreeRunningPosition(int64_t *positionFrames, int64_t *timeNanos) = 0; |
| 176 | |
Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 177 | virtual aaudio_result_t getHardwareTimestamp(int64_t *positionFrames, int64_t *timeNanos) = 0; |
| 178 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 179 | virtual aaudio_result_t getDownDataDescription(AudioEndpointParcelable &parcelable) = 0; |
| 180 | |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 181 | aaudio_stream_state_t mState = AAUDIO_STREAM_STATE_UNINITIALIZED; |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 182 | |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 183 | pid_t mRegisteredClientThread = ILLEGAL_THREAD_ID; |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 184 | |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 185 | SharedRingBuffer* mUpMessageQueue; |
| 186 | std::mutex mLockUpMessageQueue; |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 187 | |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 188 | AAudioThread mAAudioThread; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 189 | // This is used by one thread to tell another thread to exit. So it must be atomic. |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 190 | std::atomic<bool> mThreadEnabled; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 191 | |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 192 | aaudio_format_t mAudioFormat = AAUDIO_FORMAT_UNSPECIFIED; |
| 193 | int32_t mFramesPerBurst = 0; |
| 194 | int32_t mSamplesPerFrame = AAUDIO_UNSPECIFIED; |
| 195 | int32_t mSampleRate = AAUDIO_UNSPECIFIED; |
| 196 | int32_t mCapacityInFrames = AAUDIO_UNSPECIFIED; |
| 197 | android::AudioClient mMmapClient; |
| 198 | audio_port_handle_t mClientHandle = AUDIO_PORT_HANDLE_NONE; |
| 199 | |
Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 200 | SimpleDoubleBuffer<Timestamp> mAtomicTimestamp; |
| 201 | |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 202 | private: |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 203 | aaudio_handle_t mHandle = -1; |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 204 | }; |
| 205 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 206 | } /* namespace aaudio */ |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 207 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 208 | #endif //AAUDIO_AAUDIO_SERVICE_STREAM_BASE_H |