Phil Burk | 204a163 | 2017-01-03 17:23:43 -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 | 5204d31 | 2017-05-04 17:16:13 -0700 | [diff] [blame] | 17 | #ifndef ANDROID_AAUDIO_AUDIO_STREAM_INTERNAL_H |
| 18 | #define ANDROID_AAUDIO_AUDIO_STREAM_INTERNAL_H |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 19 | |
| 20 | #include <stdint.h> |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 21 | #include <aaudio/AAudio.h> |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 22 | |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 23 | #include "binding/AudioEndpointParcelable.h" |
Phil Burk | e572f46 | 2017-04-20 13:03:19 -0700 | [diff] [blame] | 24 | #include "binding/AAudioServiceInterface.h" |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 25 | #include "client/IsochronousClockModel.h" |
| 26 | #include "client/AudioEndpoint.h" |
| 27 | #include "core/AudioStream.h" |
Phil Burk | fd34a93 | 2017-07-19 07:03:52 -0700 | [diff] [blame] | 28 | #include "utility/AudioClock.h" |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 29 | |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 30 | using android::sp; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 31 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 32 | namespace aaudio { |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 33 | |
Phil Burk | 6479d50 | 2017-11-20 09:32:52 -0800 | [diff] [blame] | 34 | // These are intended to be outside the range of what is normally encountered. |
| 35 | // TODO MAXes should probably be much bigger. |
| 36 | constexpr int32_t MIN_FRAMES_PER_BURST = 16; // arbitrary |
| 37 | constexpr int32_t MAX_FRAMES_PER_BURST = 16 * 1024; // arbitrary |
| 38 | constexpr int32_t MAX_BUFFER_CAPACITY_IN_FRAMES = 32 * 1024; // arbitrary |
| 39 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 40 | // A stream that talks to the AAudioService or directly to a HAL. |
Phil Burk | 965650e | 2017-09-07 21:00:09 -0700 | [diff] [blame] | 41 | class AudioStreamInternal : public AudioStream { |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 42 | |
| 43 | public: |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 44 | AudioStreamInternal(AAudioServiceInterface &serviceInterface, bool inService); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 45 | virtual ~AudioStreamInternal(); |
| 46 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 47 | aaudio_result_t getTimestamp(clockid_t clockId, |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 48 | int64_t *framePosition, |
| 49 | int64_t *timeNanoseconds) override; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 50 | |
Phil Burk | 0befec6 | 2017-07-28 15:12:13 -0700 | [diff] [blame] | 51 | virtual aaudio_result_t updateStateMachine() override; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 52 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 53 | aaudio_result_t open(const AudioStreamBuilder &builder) override; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 54 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 55 | aaudio_result_t setBufferSize(int32_t requestedFrames) override; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 56 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 57 | int32_t getBufferSize() const override; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 58 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 59 | int32_t getBufferCapacity() const override; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 60 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 61 | int32_t getXRunCount() const override { |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 62 | return mXRunCount; |
| 63 | } |
| 64 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 65 | aaudio_result_t registerThread() override; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 66 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 67 | aaudio_result_t unregisterThread() override; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 68 | |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 69 | // Called internally from 'C' |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 70 | virtual void *callbackLoop() = 0; |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 71 | |
Phil Burk | e2fbb59 | 2017-05-01 15:05:52 -0700 | [diff] [blame] | 72 | bool isMMap() override { |
| 73 | return true; |
| 74 | } |
| 75 | |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 76 | // Calculate timeout based on framesPerBurst |
| 77 | int64_t calculateReasonableTimeout(); |
| 78 | |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 79 | aaudio_result_t startClient(const android::AudioClient& client, |
jiabin | d1f1cb6 | 2020-03-24 11:57:57 -0700 | [diff] [blame] | 80 | const audio_attributes_t *attr, |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 81 | audio_port_handle_t *clientHandle); |
| 82 | |
| 83 | aaudio_result_t stopClient(audio_port_handle_t clientHandle); |
| 84 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 85 | aaudio_handle_t getServiceHandle() const { |
| 86 | return mServiceStreamHandle; |
| 87 | } |
| 88 | |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 89 | protected: |
Phil Burk | 0bd745e | 2020-10-17 18:20:01 +0000 | [diff] [blame] | 90 | aaudio_result_t requestStart_l() REQUIRES(mStreamLock) override; |
| 91 | aaudio_result_t requestStop_l() REQUIRES(mStreamLock) override; |
| 92 | |
| 93 | aaudio_result_t release_l() REQUIRES(mStreamLock) override; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 94 | |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 95 | aaudio_result_t processData(void *buffer, |
| 96 | int32_t numFrames, |
| 97 | int64_t timeoutNanoseconds); |
| 98 | |
| 99 | /** |
| 100 | * Low level data processing that will not block. It will just read or write as much as it can. |
| 101 | * |
| 102 | * It passed back a recommended time to wake up if wakeTimePtr is not NULL. |
| 103 | * |
| 104 | * @return the number of frames processed or a negative error code. |
| 105 | */ |
| 106 | virtual aaudio_result_t processDataNow(void *buffer, |
| 107 | int32_t numFrames, |
| 108 | int64_t currentTimeNanos, |
| 109 | int64_t *wakeTimePtr) = 0; |
| 110 | |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 111 | aaudio_result_t drainTimestampsFromService(); |
| 112 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 113 | aaudio_result_t processCommands(); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 114 | |
Phil Burk | dd58292 | 2020-10-15 20:29:51 +0000 | [diff] [blame] | 115 | aaudio_result_t stopCallback_l(); |
| 116 | |
Phil Burk | ec8ca52 | 2020-05-19 10:05:58 -0700 | [diff] [blame] | 117 | virtual void prepareBuffersForStart() {} |
| 118 | |
| 119 | virtual void advanceClientToMatchServerPosition(int32_t serverMargin = 0) = 0; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 120 | |
Phil Burk | b336e89 | 2017-07-05 15:35:43 -0700 | [diff] [blame] | 121 | virtual void onFlushFromServer() {} |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 122 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 123 | aaudio_result_t onEventFromServer(AAudioServiceMessage *message); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 124 | |
Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 125 | aaudio_result_t onTimestampService(AAudioServiceMessage *message); |
| 126 | |
| 127 | aaudio_result_t onTimestampHardware(AAudioServiceMessage *message); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 128 | |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 129 | void logTimestamp(AAudioServiceMessage &message); |
| 130 | |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 131 | // Calculate timeout for an operation involving framesPerOperation. |
| 132 | int64_t calculateReasonableTimeout(int32_t framesPerOperation); |
| 133 | |
Phil Burk | 41f19d8 | 2018-02-13 14:59:10 -0800 | [diff] [blame] | 134 | int32_t getDeviceChannelCount() const { return mDeviceChannelCount; } |
| 135 | |
| 136 | /** |
| 137 | * @return true if running in audio service, versus in app process |
| 138 | */ |
| 139 | bool isInService() const { return mInService; } |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 140 | |
Phil Burk | 377c1c2 | 2018-12-12 16:06:54 -0800 | [diff] [blame] | 141 | /** |
| 142 | * Is the service FIFO position currently controlled by the AAudio service or HAL, |
| 143 | * or set based on the Clock Model. |
| 144 | * |
| 145 | * @return true if the ClockModel is currently determining the FIFO position |
| 146 | */ |
| 147 | bool isClockModelInControl() const; |
| 148 | |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 149 | IsochronousClockModel mClockModel; // timing model for chasing the HAL |
| 150 | |
Phil Burk | 5edc4ea | 2020-04-17 08:15:42 -0700 | [diff] [blame] | 151 | std::unique_ptr<AudioEndpoint> mAudioEndpoint; // source for reads or sink for writes |
| 152 | |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 153 | aaudio_handle_t mServiceStreamHandle; // opaque handle returned from service |
| 154 | |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 155 | int32_t mXRunCount = 0; // how many underrun events? |
| 156 | |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 157 | // Offset from underlying frame position. |
| 158 | int64_t mFramesOffsetFromService = 0; // offset for timestamps |
| 159 | |
Phil Burk | bf821e2 | 2020-04-17 11:51:43 -0700 | [diff] [blame] | 160 | std::unique_ptr<uint8_t[]> mCallbackBuffer; |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 161 | int32_t mCallbackFrames = 0; |
| 162 | |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 163 | // The service uses this for SHARED mode. |
| 164 | bool mInService = false; // Is this running in the client or the service? |
| 165 | |
Phil Burk | b336e89 | 2017-07-05 15:35:43 -0700 | [diff] [blame] | 166 | AAudioServiceInterface &mServiceInterface; // abstract interface to the service |
| 167 | |
Phil Burk | a53ffa6 | 2018-10-10 16:21:37 -0700 | [diff] [blame] | 168 | SimpleDoubleBuffer<Timestamp> mAtomicInternalTimestamp; |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 169 | |
| 170 | AtomicRequestor mNeedCatchUp; // Ask read() or write() to sync on first timestamp. |
| 171 | |
Phil Burk | 965650e | 2017-09-07 21:00:09 -0700 | [diff] [blame] | 172 | float mStreamVolume = 1.0f; |
| 173 | |
Phil Burk | 5edc4ea | 2020-04-17 08:15:42 -0700 | [diff] [blame] | 174 | int64_t mLastFramesWritten = 0; |
| 175 | int64_t mLastFramesRead = 0; |
| 176 | |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 177 | private: |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 178 | /* |
| 179 | * Asynchronous write with data conversion. |
| 180 | * @param buffer |
| 181 | * @param numFrames |
| 182 | * @return fdrames written or negative error |
| 183 | */ |
| 184 | aaudio_result_t writeNowWithConversion(const void *buffer, |
| 185 | int32_t numFrames); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 186 | |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 187 | // Adjust timing model based on timestamp from service. |
| 188 | void processTimestamp(uint64_t position, int64_t time); |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 189 | |
Phil Burk | fd34a93 | 2017-07-19 07:03:52 -0700 | [diff] [blame] | 190 | // Thread on other side of FIFO will have wakeup jitter. |
| 191 | // By delaying slightly we can avoid waking up before other side is ready. |
| 192 | const int32_t mWakeupDelayNanos; // delay past typical wakeup jitter |
| 193 | const int32_t mMinimumSleepNanos; // minimum sleep while polling |
Phil Burk | b31b66f | 2019-09-30 09:33:41 -0700 | [diff] [blame] | 194 | int32_t mTimeOffsetNanos = 0; // add to time part of an MMAP timestamp |
Phil Burk | fd34a93 | 2017-07-19 07:03:52 -0700 | [diff] [blame] | 195 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 196 | AudioEndpointParcelable mEndPointParcelable; // description of the buffers filled by service |
| 197 | EndpointDescriptor mEndpointDescriptor; // buffer description with resolved addresses |
Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 198 | |
Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 199 | int64_t mServiceLatencyNanos = 0; |
Phil Burk | 41f19d8 | 2018-02-13 14:59:10 -0800 | [diff] [blame] | 200 | |
Phil Burk | 0127c1b | 2018-03-29 13:48:06 -0700 | [diff] [blame] | 201 | // Sometimes the hardware is operating with a different channel count from the app. |
| 202 | // Then we require conversion in AAudio. |
Phil Burk | 41f19d8 | 2018-02-13 14:59:10 -0800 | [diff] [blame] | 203 | int32_t mDeviceChannelCount = 0; |
Phil Burk | 8d4f006 | 2019-10-03 15:55:41 -0700 | [diff] [blame] | 204 | |
| 205 | int32_t mBufferSizeInFrames = 0; // local threshold to control latency |
Phil Burk | 5edc4ea | 2020-04-17 08:15:42 -0700 | [diff] [blame] | 206 | int32_t mBufferCapacityInFrames = 0; |
| 207 | |
Phil Burk | 8d4f006 | 2019-10-03 15:55:41 -0700 | [diff] [blame] | 208 | |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 209 | }; |
| 210 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 211 | } /* namespace aaudio */ |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 212 | |
Phil Burk | 5204d31 | 2017-05-04 17:16:13 -0700 | [diff] [blame] | 213 | #endif //ANDROID_AAUDIO_AUDIO_STREAM_INTERNAL_H |