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