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