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