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 | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 35 | #include "AAudioService.h" |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 36 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 37 | namespace aaudio { |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 38 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 39 | class AAudioServiceEndpoint; |
| 40 | |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 41 | // We expect the queue to only have a few commands. |
| 42 | // This should be way more than we need. |
| 43 | #define QUEUE_UP_CAPACITY_COMMANDS (128) |
| 44 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 45 | /** |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 46 | * Each instance of AAudioServiceStreamBase corresponds to a client stream. |
| 47 | * It uses a subclass of AAudioServiceEndpoint to communicate with the underlying device or port. |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 48 | */ |
| 49 | class AAudioServiceStreamBase |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 50 | : public virtual android::RefBase |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 51 | , public AAudioStreamParameters |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 52 | , public Runnable { |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 53 | |
| 54 | public: |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 55 | AAudioServiceStreamBase(android::AAudioService &aAudioService); |
| 56 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 57 | virtual ~AAudioServiceStreamBase(); |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 58 | |
| 59 | enum { |
| 60 | ILLEGAL_THREAD_ID = 0 |
| 61 | }; |
| 62 | |
Phil Burk | a5222e2 | 2017-07-28 13:31:14 -0700 | [diff] [blame] | 63 | static std::string dumpHeader(); |
| 64 | |
| 65 | // does not include EOL |
| 66 | virtual std::string dump() const; |
Phil Burk | 4501b35 | 2017-06-29 18:12:36 -0700 | [diff] [blame] | 67 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 68 | // ------------------------------------------------------------------- |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 69 | /** |
| 70 | * Open the device. |
| 71 | */ |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 72 | virtual aaudio_result_t open(const aaudio::AAudioStreamRequest &request) = 0; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 73 | |
| 74 | virtual aaudio_result_t close(); |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 75 | |
| 76 | /** |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 77 | * Start the flow of audio data. |
| 78 | * |
| 79 | * This is not guaranteed to be synchronous but it currently is. |
| 80 | * An AAUDIO_SERVICE_EVENT_STARTED will be sent to the client when complete. |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 81 | */ |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 82 | virtual aaudio_result_t start(); |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 83 | |
| 84 | /** |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 85 | * Stop the flow of data so that start() can resume without loss of data. |
| 86 | * |
| 87 | * This is not guaranteed to be synchronous but it currently is. |
| 88 | * An AAUDIO_SERVICE_EVENT_PAUSED will be sent to the client when complete. |
| 89 | */ |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 90 | virtual aaudio_result_t pause(); |
| 91 | |
| 92 | /** |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 93 | * Stop the flow of data after the currently queued data has finished playing. |
| 94 | * |
| 95 | * This is not guaranteed to be synchronous but it currently is. |
| 96 | * An AAUDIO_SERVICE_EVENT_STOPPED will be sent to the client when complete. |
| 97 | * |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 98 | */ |
| 99 | virtual aaudio_result_t stop(); |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 100 | |
Phil Burk | 98d6d92 | 2017-07-06 11:52:45 -0700 | [diff] [blame] | 101 | aaudio_result_t stopTimestampThread(); |
| 102 | |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 103 | /** |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 104 | * Discard any data held by the underlying HAL or Service. |
| 105 | * |
| 106 | * An AAUDIO_SERVICE_EVENT_FLUSHED will be sent to the client when complete. |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 107 | */ |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 108 | virtual aaudio_result_t flush(); |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 109 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 110 | |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 111 | virtual aaudio_result_t startClient(const android::AudioClient& client __unused, |
| 112 | audio_port_handle_t *clientHandle __unused) { |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 113 | ALOGD("AAudioServiceStreamBase::startClient(%p, ...) AAUDIO_ERROR_UNAVAILABLE", &client); |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 114 | return AAUDIO_ERROR_UNAVAILABLE; |
| 115 | } |
| 116 | |
| 117 | virtual aaudio_result_t stopClient(audio_port_handle_t clientHandle __unused) { |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 118 | ALOGD("AAudioServiceStreamBase::stopClient(%d) AAUDIO_ERROR_UNAVAILABLE", clientHandle); |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 119 | return AAUDIO_ERROR_UNAVAILABLE; |
| 120 | } |
| 121 | |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 122 | bool isRunning() const { |
| 123 | return mState == AAUDIO_STREAM_STATE_STARTED; |
| 124 | } |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 125 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 126 | // ------------------------------------------------------------------- |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 127 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 128 | /** |
| 129 | * Send a message to the client. |
| 130 | */ |
| 131 | aaudio_result_t sendServiceEvent(aaudio_service_event_t event, |
| 132 | double dataDouble = 0.0, |
| 133 | int64_t dataLong = 0); |
Phil Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame] | 134 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 135 | /** |
| 136 | * Fill in a parcelable description of stream. |
| 137 | */ |
| 138 | aaudio_result_t getDescription(AudioEndpointParcelable &parcelable); |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 139 | |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 140 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 141 | void setRegisteredThread(pid_t pid) { |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 142 | mRegisteredClientThread = pid; |
| 143 | } |
Phil Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame] | 144 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 145 | pid_t getRegisteredThread() const { |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 146 | return mRegisteredClientThread; |
| 147 | } |
| 148 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 149 | int32_t getFramesPerBurst() const { |
| 150 | return mFramesPerBurst; |
| 151 | } |
| 152 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 153 | void run() override; // to implement Runnable |
| 154 | |
Phil Burk | 5ef003b | 2017-06-30 11:43:37 -0700 | [diff] [blame] | 155 | void disconnect(); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 156 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 157 | const android::AudioClient &getAudioClient() { |
| 158 | return mMmapClient; |
| 159 | } |
| 160 | |
Phil Burk | 2ac035f | 2017-06-23 14:51:14 -0700 | [diff] [blame] | 161 | uid_t getOwnerUserId() const { |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 162 | return mMmapClient.clientUid; |
Phil Burk | 2ac035f | 2017-06-23 14:51:14 -0700 | [diff] [blame] | 163 | } |
| 164 | |
Phil Burk | b63320a | 2017-06-30 10:28:20 -0700 | [diff] [blame] | 165 | pid_t getOwnerProcessId() const { |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 166 | return mMmapClient.clientPid; |
Phil Burk | b63320a | 2017-06-30 10:28:20 -0700 | [diff] [blame] | 167 | } |
| 168 | |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 169 | aaudio_handle_t getHandle() const { |
| 170 | return mHandle; |
| 171 | } |
| 172 | void setHandle(aaudio_handle_t handle) { |
| 173 | mHandle = handle; |
| 174 | } |
| 175 | |
Phil Burk | 5a26e66 | 2017-07-07 12:44:48 -0700 | [diff] [blame] | 176 | aaudio_stream_state_t getState() const { |
| 177 | return mState; |
| 178 | } |
| 179 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 180 | void onVolumeChanged(float volume); |
| 181 | |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 182 | protected: |
Phil Burk | 98d6d92 | 2017-07-06 11:52:45 -0700 | [diff] [blame] | 183 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 184 | /** |
| 185 | * Open the device. |
| 186 | */ |
| 187 | aaudio_result_t open(const aaudio::AAudioStreamRequest &request, |
| 188 | aaudio_sharing_mode_t sharingMode); |
| 189 | |
Phil Burk | 5a26e66 | 2017-07-07 12:44:48 -0700 | [diff] [blame] | 190 | void setState(aaudio_stream_state_t state) { |
| 191 | mState = state; |
| 192 | } |
| 193 | |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame^] | 194 | /** |
| 195 | * Device specific startup. |
| 196 | * @return AAUDIO_OK or negative error. |
| 197 | */ |
| 198 | virtual aaudio_result_t startDevice(); |
| 199 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 200 | aaudio_result_t writeUpMessageQueue(AAudioServiceMessage *command); |
| 201 | |
| 202 | aaudio_result_t sendCurrentTimestamp(); |
| 203 | |
Phil Burk | 940083c | 2017-07-17 17:00:02 -0700 | [diff] [blame] | 204 | /** |
| 205 | * @param positionFrames |
| 206 | * @param timeNanos |
| 207 | * @return AAUDIO_OK or AAUDIO_ERROR_UNAVAILABLE or other negative error |
| 208 | */ |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 209 | virtual aaudio_result_t getFreeRunningPosition(int64_t *positionFrames, int64_t *timeNanos) = 0; |
| 210 | |
Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 211 | virtual aaudio_result_t getHardwareTimestamp(int64_t *positionFrames, int64_t *timeNanos) = 0; |
| 212 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 213 | virtual aaudio_result_t getDownDataDescription(AudioEndpointParcelable &parcelable) = 0; |
| 214 | |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 215 | aaudio_stream_state_t mState = AAUDIO_STREAM_STATE_UNINITIALIZED; |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 216 | |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 217 | pid_t mRegisteredClientThread = ILLEGAL_THREAD_ID; |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 218 | |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 219 | SharedRingBuffer* mUpMessageQueue; |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 220 | std::mutex mUpMessageQueueLock; |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 221 | |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame^] | 222 | AAudioThread mTimestampThread; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 223 | // This is used by one thread to tell another thread to exit. So it must be atomic. |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 224 | std::atomic<bool> mThreadEnabled{false}; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 225 | |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 226 | int32_t mFramesPerBurst = 0; |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 227 | android::AudioClient mMmapClient; // set in open, used in MMAP start() |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 228 | audio_port_handle_t mClientHandle = AUDIO_PORT_HANDLE_NONE; |
| 229 | |
Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 230 | SimpleDoubleBuffer<Timestamp> mAtomicTimestamp; |
| 231 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 232 | android::AAudioService &mAudioService; |
| 233 | android::sp<AAudioServiceEndpoint> mServiceEndpoint; |
| 234 | |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 235 | private: |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 236 | aaudio_handle_t mHandle = -1; |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 237 | }; |
| 238 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 239 | } /* namespace aaudio */ |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 240 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 241 | #endif //AAUDIO_AAUDIO_SERVICE_STREAM_BASE_H |