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> |
Eric Laurent | a2f296e | 2017-06-21 18:51:47 -0700 | [diff] [blame] | 21 | #include <media/PlayerBase.h> |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 22 | #include <aaudio/AAudio.h> |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 23 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 24 | #include "binding/IAAudioService.h" |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 25 | #include "binding/AudioEndpointParcelable.h" |
Phil Burk | e572f46 | 2017-04-20 13:03:19 -0700 | [diff] [blame] | 26 | #include "binding/AAudioServiceInterface.h" |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 27 | #include "client/IsochronousClockModel.h" |
| 28 | #include "client/AudioEndpoint.h" |
| 29 | #include "core/AudioStream.h" |
Phil Burk | fd34a93 | 2017-07-19 07:03:52 -0700 | [diff] [blame^] | 30 | #include "utility/AudioClock.h" |
Phil Burk | e572f46 | 2017-04-20 13:03:19 -0700 | [diff] [blame] | 31 | #include "utility/LinearRamp.h" |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 32 | |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 33 | using android::sp; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 34 | using android::IAAudioService; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 35 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 36 | namespace aaudio { |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 37 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 38 | // A stream that talks to the AAudioService or directly to a HAL. |
Eric Laurent | a2f296e | 2017-06-21 18:51:47 -0700 | [diff] [blame] | 39 | class AudioStreamInternal : public AudioStream, public android::PlayerBase { |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 40 | |
| 41 | public: |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 42 | AudioStreamInternal(AAudioServiceInterface &serviceInterface, bool inService); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 43 | virtual ~AudioStreamInternal(); |
| 44 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 45 | aaudio_result_t requestStart() override; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 46 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 47 | aaudio_result_t requestStop() override; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 48 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 49 | aaudio_result_t getTimestamp(clockid_t clockId, |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 50 | int64_t *framePosition, |
| 51 | int64_t *timeNanoseconds) override; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 52 | |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 53 | virtual aaudio_result_t updateStateWhileWaiting() 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 open(const AudioStreamBuilder &builder) 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 | aaudio_result_t close() 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 | aaudio_result_t setBufferSize(int32_t requestedFrames) 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 getBufferSize() const override; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 62 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 63 | int32_t getBufferCapacity() const override; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 64 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 65 | int32_t getFramesPerBurst() const 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 | int32_t getXRunCount() const override { |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 68 | return mXRunCount; |
| 69 | } |
| 70 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 71 | aaudio_result_t registerThread() override; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 72 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 73 | aaudio_result_t unregisterThread() override; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 74 | |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 75 | aaudio_result_t joinThread(void** returnArg); |
| 76 | |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 77 | // Called internally from 'C' |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 78 | virtual void *callbackLoop() = 0; |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 79 | |
Phil Burk | e2fbb59 | 2017-05-01 15:05:52 -0700 | [diff] [blame] | 80 | |
| 81 | bool isMMap() override { |
| 82 | return true; |
| 83 | } |
| 84 | |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 85 | // Calculate timeout based on framesPerBurst |
| 86 | int64_t calculateReasonableTimeout(); |
| 87 | |
Eric Laurent | a2f296e | 2017-06-21 18:51:47 -0700 | [diff] [blame] | 88 | //PlayerBase virtuals |
| 89 | virtual void destroy(); |
| 90 | |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 91 | aaudio_result_t startClient(const android::AudioClient& client, |
| 92 | audio_port_handle_t *clientHandle); |
| 93 | |
| 94 | aaudio_result_t stopClient(audio_port_handle_t clientHandle); |
| 95 | |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 96 | protected: |
| 97 | |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 98 | aaudio_result_t processData(void *buffer, |
| 99 | int32_t numFrames, |
| 100 | int64_t timeoutNanoseconds); |
| 101 | |
| 102 | /** |
| 103 | * Low level data processing that will not block. It will just read or write as much as it can. |
| 104 | * |
| 105 | * It passed back a recommended time to wake up if wakeTimePtr is not NULL. |
| 106 | * |
| 107 | * @return the number of frames processed or a negative error code. |
| 108 | */ |
| 109 | virtual aaudio_result_t processDataNow(void *buffer, |
| 110 | int32_t numFrames, |
| 111 | int64_t currentTimeNanos, |
| 112 | int64_t *wakeTimePtr) = 0; |
| 113 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 114 | aaudio_result_t processCommands(); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 115 | |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 116 | aaudio_result_t requestStopInternal(); |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 117 | |
| 118 | aaudio_result_t stopCallback(); |
| 119 | |
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 | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 125 | aaudio_result_t onTimestampFromServer(AAudioServiceMessage *message); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 126 | |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 127 | void logTimestamp(AAudioServiceMessage &message); |
| 128 | |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 129 | // Calculate timeout for an operation involving framesPerOperation. |
| 130 | int64_t calculateReasonableTimeout(int32_t framesPerOperation); |
| 131 | |
Eric Laurent | a2f296e | 2017-06-21 18:51:47 -0700 | [diff] [blame] | 132 | void doSetVolume(); |
| 133 | |
| 134 | //PlayerBase virtuals |
| 135 | virtual status_t playerStart(); |
| 136 | virtual status_t playerPause(); |
| 137 | virtual status_t playerStop(); |
| 138 | virtual status_t playerSetVolume(); |
| 139 | |
Phil Burk | 9dca982 | 2017-05-26 14:27:43 -0700 | [diff] [blame] | 140 | aaudio_format_t mDeviceFormat = AAUDIO_FORMAT_UNSPECIFIED; |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 141 | |
| 142 | IsochronousClockModel mClockModel; // timing model for chasing the HAL |
| 143 | |
| 144 | AudioEndpoint mAudioEndpoint; // source for reads or sink for writes |
| 145 | aaudio_handle_t mServiceStreamHandle; // opaque handle returned from service |
| 146 | |
| 147 | int32_t mFramesPerBurst; // frames per HAL transfer |
| 148 | int32_t mXRunCount = 0; // how many underrun events? |
| 149 | |
| 150 | LinearRamp mVolumeRamp; |
Eric Laurent | a2f296e | 2017-06-21 18:51:47 -0700 | [diff] [blame] | 151 | float mStreamVolume; |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 152 | |
| 153 | // Offset from underlying frame position. |
| 154 | int64_t mFramesOffsetFromService = 0; // offset for timestamps |
| 155 | |
| 156 | uint8_t *mCallbackBuffer = nullptr; |
| 157 | int32_t mCallbackFrames = 0; |
| 158 | |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 159 | // The service uses this for SHARED mode. |
| 160 | bool mInService = false; // Is this running in the client or the service? |
| 161 | |
Phil Burk | b336e89 | 2017-07-05 15:35:43 -0700 | [diff] [blame] | 162 | AAudioServiceInterface &mServiceInterface; // abstract interface to the service |
| 163 | |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 164 | private: |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 165 | /* |
| 166 | * Asynchronous write with data conversion. |
| 167 | * @param buffer |
| 168 | * @param numFrames |
| 169 | * @return fdrames written or negative error |
| 170 | */ |
| 171 | aaudio_result_t writeNowWithConversion(const void *buffer, |
| 172 | int32_t numFrames); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 173 | |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 174 | // Adjust timing model based on timestamp from service. |
| 175 | void processTimestamp(uint64_t position, int64_t time); |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 176 | |
Phil Burk | fd34a93 | 2017-07-19 07:03:52 -0700 | [diff] [blame^] | 177 | // Thread on other side of FIFO will have wakeup jitter. |
| 178 | // By delaying slightly we can avoid waking up before other side is ready. |
| 179 | const int32_t mWakeupDelayNanos; // delay past typical wakeup jitter |
| 180 | const int32_t mMinimumSleepNanos; // minimum sleep while polling |
| 181 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 182 | AudioEndpointParcelable mEndPointParcelable; // description of the buffers filled by service |
| 183 | EndpointDescriptor mEndpointDescriptor; // buffer description with resolved addresses |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 184 | }; |
| 185 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 186 | } /* namespace aaudio */ |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 187 | |
Phil Burk | 5204d31 | 2017-05-04 17:16:13 -0700 | [diff] [blame] | 188 | #endif //ANDROID_AAUDIO_AUDIO_STREAM_INTERNAL_H |