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