blob: d7024cf64ae4a8f82137a488ed17d740a05fa50c [file] [log] [blame]
Phil Burk204a1632017-01-03 17:23:43 -08001/*
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 Burk5204d312017-05-04 17:16:13 -070017#ifndef ANDROID_AAUDIO_AUDIO_STREAM_INTERNAL_H
18#define ANDROID_AAUDIO_AUDIO_STREAM_INTERNAL_H
Phil Burk204a1632017-01-03 17:23:43 -080019
20#include <stdint.h>
Phil Burk5ed503c2017-02-01 09:38:15 -080021#include <aaudio/AAudio.h>
Phil Burk204a1632017-01-03 17:23:43 -080022
Phil Burk204a1632017-01-03 17:23:43 -080023#include "binding/AudioEndpointParcelable.h"
Phil Burke572f462017-04-20 13:03:19 -070024#include "binding/AAudioServiceInterface.h"
Phil Burk204a1632017-01-03 17:23:43 -080025#include "client/IsochronousClockModel.h"
26#include "client/AudioEndpoint.h"
27#include "core/AudioStream.h"
Phil Burkfd34a932017-07-19 07:03:52 -070028#include "utility/AudioClock.h"
Phil Burkc0c70e32017-02-09 13:18:38 -080029
Phil Burk204a1632017-01-03 17:23:43 -080030using android::sp;
Phil Burk204a1632017-01-03 17:23:43 -080031
Phil Burk5ed503c2017-02-01 09:38:15 -080032namespace aaudio {
Phil Burk204a1632017-01-03 17:23:43 -080033
Phil Burk6479d502017-11-20 09:32:52 -080034 // 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 Burk5ed503c2017-02-01 09:38:15 -080040// A stream that talks to the AAudioService or directly to a HAL.
Phil Burk965650e2017-09-07 21:00:09 -070041class AudioStreamInternal : public AudioStream {
Phil Burk204a1632017-01-03 17:23:43 -080042
43public:
Phil Burk87c9f642017-05-17 07:22:39 -070044 AudioStreamInternal(AAudioServiceInterface &serviceInterface, bool inService);
Phil Burk204a1632017-01-03 17:23:43 -080045 virtual ~AudioStreamInternal();
46
Phil Burkc0c70e32017-02-09 13:18:38 -080047 aaudio_result_t requestStart() override;
Phil Burk204a1632017-01-03 17:23:43 -080048
Phil Burkc0c70e32017-02-09 13:18:38 -080049 aaudio_result_t requestStop() override;
Phil Burk204a1632017-01-03 17:23:43 -080050
Phil Burkc0c70e32017-02-09 13:18:38 -080051 aaudio_result_t getTimestamp(clockid_t clockId,
Phil Burk3316d5e2017-02-15 11:23:01 -080052 int64_t *framePosition,
53 int64_t *timeNanoseconds) override;
Phil Burk204a1632017-01-03 17:23:43 -080054
Phil Burk0befec62017-07-28 15:12:13 -070055 virtual aaudio_result_t updateStateMachine() override;
Phil Burk204a1632017-01-03 17:23:43 -080056
Phil Burkc0c70e32017-02-09 13:18:38 -080057 aaudio_result_t open(const AudioStreamBuilder &builder) override;
Phil Burk204a1632017-01-03 17:23:43 -080058
Phil Burk8b4e05e2019-12-17 12:12:09 -080059 aaudio_result_t release_l() override;
Phil Burk204a1632017-01-03 17:23:43 -080060
Phil Burkc0c70e32017-02-09 13:18:38 -080061 aaudio_result_t setBufferSize(int32_t requestedFrames) override;
Phil Burk204a1632017-01-03 17:23:43 -080062
Phil Burkc0c70e32017-02-09 13:18:38 -080063 int32_t getBufferSize() const override;
Phil Burk204a1632017-01-03 17:23:43 -080064
Phil Burkc0c70e32017-02-09 13:18:38 -080065 int32_t getBufferCapacity() const override;
Phil Burk204a1632017-01-03 17:23:43 -080066
Phil Burkc0c70e32017-02-09 13:18:38 -080067 int32_t getXRunCount() const override {
Phil Burk204a1632017-01-03 17:23:43 -080068 return mXRunCount;
69 }
70
Phil Burkc0c70e32017-02-09 13:18:38 -080071 aaudio_result_t registerThread() override;
Phil Burk204a1632017-01-03 17:23:43 -080072
Phil Burkc0c70e32017-02-09 13:18:38 -080073 aaudio_result_t unregisterThread() override;
Phil Burk204a1632017-01-03 17:23:43 -080074
Phil Burk87c9f642017-05-17 07:22:39 -070075 aaudio_result_t joinThread(void** returnArg);
76
Phil Burke4d7bb42017-03-28 11:32:39 -070077 // Called internally from 'C'
Phil Burk87c9f642017-05-17 07:22:39 -070078 virtual void *callbackLoop() = 0;
Phil Burke4d7bb42017-03-28 11:32:39 -070079
Phil Burke2fbb592017-05-01 15:05:52 -070080
81 bool isMMap() override {
82 return true;
83 }
84
Phil Burk87c9f642017-05-17 07:22:39 -070085 // Calculate timeout based on framesPerBurst
86 int64_t calculateReasonableTimeout();
87
Eric Laurentcb4dae22017-07-01 19:39:32 -070088 aaudio_result_t startClient(const android::AudioClient& client,
jiabind1f1cb62020-03-24 11:57:57 -070089 const audio_attributes_t *attr,
Eric Laurentcb4dae22017-07-01 19:39:32 -070090 audio_port_handle_t *clientHandle);
91
92 aaudio_result_t stopClient(audio_port_handle_t clientHandle);
93
Phil Burk39f02dd2017-08-04 09:13:31 -070094 aaudio_handle_t getServiceHandle() const {
95 return mServiceStreamHandle;
96 }
97
Phil Burk204a1632017-01-03 17:23:43 -080098protected:
99
Phil Burk87c9f642017-05-17 07:22:39 -0700100 aaudio_result_t processData(void *buffer,
101 int32_t numFrames,
102 int64_t timeoutNanoseconds);
103
104/**
105 * Low level data processing that will not block. It will just read or write as much as it can.
106 *
107 * It passed back a recommended time to wake up if wakeTimePtr is not NULL.
108 *
109 * @return the number of frames processed or a negative error code.
110 */
111 virtual aaudio_result_t processDataNow(void *buffer,
112 int32_t numFrames,
113 int64_t currentTimeNanos,
114 int64_t *wakeTimePtr) = 0;
115
Phil Burkbcc36742017-08-31 17:24:51 -0700116 aaudio_result_t drainTimestampsFromService();
117
Phil Burk5ed503c2017-02-01 09:38:15 -0800118 aaudio_result_t processCommands();
Phil Burk204a1632017-01-03 17:23:43 -0800119
Phil Burke4d7bb42017-03-28 11:32:39 -0700120 aaudio_result_t stopCallback();
121
Phil Burkec8ca522020-05-19 10:05:58 -0700122 virtual void prepareBuffersForStart() {}
123
124 virtual void advanceClientToMatchServerPosition(int32_t serverMargin = 0) = 0;
Phil Burk204a1632017-01-03 17:23:43 -0800125
Phil Burkb336e892017-07-05 15:35:43 -0700126 virtual void onFlushFromServer() {}
Phil Burk204a1632017-01-03 17:23:43 -0800127
Phil Burk5ed503c2017-02-01 09:38:15 -0800128 aaudio_result_t onEventFromServer(AAudioServiceMessage *message);
Phil Burk204a1632017-01-03 17:23:43 -0800129
Phil Burk97350f92017-07-21 15:59:44 -0700130 aaudio_result_t onTimestampService(AAudioServiceMessage *message);
131
132 aaudio_result_t onTimestampHardware(AAudioServiceMessage *message);
Phil Burk204a1632017-01-03 17:23:43 -0800133
Phil Burkec89b2e2017-06-20 15:05:06 -0700134 void logTimestamp(AAudioServiceMessage &message);
135
Phil Burke4d7bb42017-03-28 11:32:39 -0700136 // Calculate timeout for an operation involving framesPerOperation.
137 int64_t calculateReasonableTimeout(int32_t framesPerOperation);
138
Phil Burk41f19d82018-02-13 14:59:10 -0800139 int32_t getDeviceChannelCount() const { return mDeviceChannelCount; }
140
141 /**
142 * @return true if running in audio service, versus in app process
143 */
144 bool isInService() const { return mInService; }
Phil Burk87c9f642017-05-17 07:22:39 -0700145
Phil Burk377c1c22018-12-12 16:06:54 -0800146 /**
147 * Is the service FIFO position currently controlled by the AAudio service or HAL,
148 * or set based on the Clock Model.
149 *
150 * @return true if the ClockModel is currently determining the FIFO position
151 */
152 bool isClockModelInControl() const;
153
Phil Burk87c9f642017-05-17 07:22:39 -0700154 IsochronousClockModel mClockModel; // timing model for chasing the HAL
155
Phil Burk5edc4ea2020-04-17 08:15:42 -0700156 std::unique_ptr<AudioEndpoint> mAudioEndpoint; // source for reads or sink for writes
157
Phil Burk87c9f642017-05-17 07:22:39 -0700158 aaudio_handle_t mServiceStreamHandle; // opaque handle returned from service
159
Phil Burk87c9f642017-05-17 07:22:39 -0700160 int32_t mXRunCount = 0; // how many underrun events?
161
Phil Burk87c9f642017-05-17 07:22:39 -0700162 // Offset from underlying frame position.
163 int64_t mFramesOffsetFromService = 0; // offset for timestamps
164
Phil Burkbf821e22020-04-17 11:51:43 -0700165 std::unique_ptr<uint8_t[]> mCallbackBuffer;
Phil Burk87c9f642017-05-17 07:22:39 -0700166 int32_t mCallbackFrames = 0;
167
Phil Burkec89b2e2017-06-20 15:05:06 -0700168 // The service uses this for SHARED mode.
169 bool mInService = false; // Is this running in the client or the service?
170
Phil Burkb336e892017-07-05 15:35:43 -0700171 AAudioServiceInterface &mServiceInterface; // abstract interface to the service
172
Phil Burka53ffa62018-10-10 16:21:37 -0700173 SimpleDoubleBuffer<Timestamp> mAtomicInternalTimestamp;
Phil Burkbcc36742017-08-31 17:24:51 -0700174
175 AtomicRequestor mNeedCatchUp; // Ask read() or write() to sync on first timestamp.
176
Phil Burk965650e2017-09-07 21:00:09 -0700177 float mStreamVolume = 1.0f;
178
Phil Burk5edc4ea2020-04-17 08:15:42 -0700179 int64_t mLastFramesWritten = 0;
180 int64_t mLastFramesRead = 0;
181
Phil Burk204a1632017-01-03 17:23:43 -0800182private:
Phil Burkc0c70e32017-02-09 13:18:38 -0800183 /*
184 * Asynchronous write with data conversion.
185 * @param buffer
186 * @param numFrames
187 * @return fdrames written or negative error
188 */
189 aaudio_result_t writeNowWithConversion(const void *buffer,
190 int32_t numFrames);
Phil Burkc0c70e32017-02-09 13:18:38 -0800191
Phil Burk87c9f642017-05-17 07:22:39 -0700192 // Adjust timing model based on timestamp from service.
193 void processTimestamp(uint64_t position, int64_t time);
Phil Burk71f35bb2017-04-13 16:05:07 -0700194
Phil Burkfd34a932017-07-19 07:03:52 -0700195 // Thread on other side of FIFO will have wakeup jitter.
196 // By delaying slightly we can avoid waking up before other side is ready.
197 const int32_t mWakeupDelayNanos; // delay past typical wakeup jitter
198 const int32_t mMinimumSleepNanos; // minimum sleep while polling
Phil Burkb31b66f2019-09-30 09:33:41 -0700199 int32_t mTimeOffsetNanos = 0; // add to time part of an MMAP timestamp
Phil Burkfd34a932017-07-19 07:03:52 -0700200
Phil Burkc0c70e32017-02-09 13:18:38 -0800201 AudioEndpointParcelable mEndPointParcelable; // description of the buffers filled by service
202 EndpointDescriptor mEndpointDescriptor; // buffer description with resolved addresses
Phil Burk97350f92017-07-21 15:59:44 -0700203
Phil Burk97350f92017-07-21 15:59:44 -0700204 int64_t mServiceLatencyNanos = 0;
Phil Burk41f19d82018-02-13 14:59:10 -0800205
Phil Burk0127c1b2018-03-29 13:48:06 -0700206 // Sometimes the hardware is operating with a different channel count from the app.
207 // Then we require conversion in AAudio.
Phil Burk41f19d82018-02-13 14:59:10 -0800208 int32_t mDeviceChannelCount = 0;
Phil Burk8d4f0062019-10-03 15:55:41 -0700209
210 int32_t mBufferSizeInFrames = 0; // local threshold to control latency
Phil Burk5edc4ea2020-04-17 08:15:42 -0700211 int32_t mBufferCapacityInFrames = 0;
212
Phil Burk8d4f0062019-10-03 15:55:41 -0700213
Phil Burk204a1632017-01-03 17:23:43 -0800214};
215
Phil Burk5ed503c2017-02-01 09:38:15 -0800216} /* namespace aaudio */
Phil Burk204a1632017-01-03 17:23:43 -0800217
Phil Burk5204d312017-05-04 17:16:13 -0700218#endif //ANDROID_AAUDIO_AUDIO_STREAM_INTERNAL_H