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