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 | e572f46 | 2017-04-20 13:03:19 -0700 | [diff] [blame] | 29 | #include "utility/LinearRamp.h" |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 30 | |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 31 | using android::sp; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 32 | using android::IAAudioService; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 33 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 34 | namespace aaudio { |
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 | // A stream that talks to the AAudioService or directly to a HAL. |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 37 | class AudioStreamInternal : public AudioStream { |
| 38 | |
| 39 | public: |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 40 | AudioStreamInternal(AAudioServiceInterface &serviceInterface, bool inService); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 41 | virtual ~AudioStreamInternal(); |
| 42 | |
| 43 | // =========== Begin ABSTRACT methods =========================== |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 44 | aaudio_result_t requestStart() override; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 45 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 46 | aaudio_result_t requestPause() override; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 47 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 48 | aaudio_result_t requestFlush() override; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 49 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 50 | aaudio_result_t requestStop() 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 getTimestamp(clockid_t clockId, |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 53 | int64_t *framePosition, |
| 54 | int64_t *timeNanoseconds) override; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 55 | |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 56 | virtual aaudio_result_t updateStateWhileWaiting() override; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 57 | // =========== End ABSTRACT methods =========================== |
| 58 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 59 | aaudio_result_t open(const AudioStreamBuilder &builder) 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 | aaudio_result_t close() 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 | aaudio_result_t setBufferSize(int32_t requestedFrames) 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 getBufferSize() 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 getBufferCapacity() const override; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 68 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 69 | int32_t getFramesPerBurst() const override; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 70 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 71 | int32_t getXRunCount() const override { |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 72 | return mXRunCount; |
| 73 | } |
| 74 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 75 | aaudio_result_t registerThread() override; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 76 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 77 | aaudio_result_t unregisterThread() override; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 78 | |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 79 | aaudio_result_t joinThread(void** returnArg); |
| 80 | |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 81 | // Called internally from 'C' |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 82 | virtual void *callbackLoop() = 0; |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 83 | |
Phil Burk | e2fbb59 | 2017-05-01 15:05:52 -0700 | [diff] [blame] | 84 | |
| 85 | bool isMMap() override { |
| 86 | return true; |
| 87 | } |
| 88 | |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 89 | // Calculate timeout based on framesPerBurst |
| 90 | int64_t calculateReasonableTimeout(); |
| 91 | |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 92 | protected: |
| 93 | |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 94 | aaudio_result_t processData(void *buffer, |
| 95 | int32_t numFrames, |
| 96 | int64_t timeoutNanoseconds); |
| 97 | |
| 98 | /** |
| 99 | * Low level data processing that will not block. It will just read or write as much as it can. |
| 100 | * |
| 101 | * It passed back a recommended time to wake up if wakeTimePtr is not NULL. |
| 102 | * |
| 103 | * @return the number of frames processed or a negative error code. |
| 104 | */ |
| 105 | virtual aaudio_result_t processDataNow(void *buffer, |
| 106 | int32_t numFrames, |
| 107 | int64_t currentTimeNanos, |
| 108 | int64_t *wakeTimePtr) = 0; |
| 109 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 110 | aaudio_result_t processCommands(); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 111 | |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 112 | aaudio_result_t requestPauseInternal(); |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 113 | aaudio_result_t requestStopInternal(); |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 114 | |
| 115 | aaudio_result_t stopCallback(); |
| 116 | |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 117 | |
| 118 | void onFlushFromServer(); |
| 119 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 120 | aaudio_result_t onEventFromServer(AAudioServiceMessage *message); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 121 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 122 | aaudio_result_t onTimestampFromServer(AAudioServiceMessage *message); |
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 | // Calculate timeout for an operation involving framesPerOperation. |
| 125 | int64_t calculateReasonableTimeout(int32_t framesPerOperation); |
| 126 | |
Phil Burk | 9dca982 | 2017-05-26 14:27:43 -0700 | [diff] [blame^] | 127 | aaudio_format_t mDeviceFormat = AAUDIO_FORMAT_UNSPECIFIED; |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 128 | |
| 129 | IsochronousClockModel mClockModel; // timing model for chasing the HAL |
| 130 | |
| 131 | AudioEndpoint mAudioEndpoint; // source for reads or sink for writes |
| 132 | aaudio_handle_t mServiceStreamHandle; // opaque handle returned from service |
| 133 | |
| 134 | int32_t mFramesPerBurst; // frames per HAL transfer |
| 135 | int32_t mXRunCount = 0; // how many underrun events? |
| 136 | |
| 137 | LinearRamp mVolumeRamp; |
| 138 | |
| 139 | // Offset from underlying frame position. |
| 140 | int64_t mFramesOffsetFromService = 0; // offset for timestamps |
| 141 | |
| 142 | uint8_t *mCallbackBuffer = nullptr; |
| 143 | int32_t mCallbackFrames = 0; |
| 144 | |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 145 | private: |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 146 | /* |
| 147 | * Asynchronous write with data conversion. |
| 148 | * @param buffer |
| 149 | * @param numFrames |
| 150 | * @return fdrames written or negative error |
| 151 | */ |
| 152 | aaudio_result_t writeNowWithConversion(const void *buffer, |
| 153 | int32_t numFrames); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 154 | |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 155 | // Adjust timing model based on timestamp from service. |
| 156 | void processTimestamp(uint64_t position, int64_t time); |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 157 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 158 | AudioEndpointParcelable mEndPointParcelable; // description of the buffers filled by service |
| 159 | EndpointDescriptor mEndpointDescriptor; // buffer description with resolved addresses |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 160 | AAudioServiceInterface &mServiceInterface; // abstract interface to the service |
| 161 | |
| 162 | // The service uses this for SHARED mode. |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 163 | bool mInService = false; // Is this running in the client or the service? |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 164 | }; |
| 165 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 166 | } /* namespace aaudio */ |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 167 | |
Phil Burk | 5204d31 | 2017-05-04 17:16:13 -0700 | [diff] [blame] | 168 | #endif //ANDROID_AAUDIO_AUDIO_STREAM_INTERNAL_H |