blob: fbe4c13efd17203c2ee1503657a20529e3dd4084 [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 getTimestamp(clockid_t clockId,
Phil Burk3316d5e2017-02-15 11:23:01 -080048 int64_t *framePosition,
49 int64_t *timeNanoseconds) override;
Phil Burk204a1632017-01-03 17:23:43 -080050
Phil Burk0befec62017-07-28 15:12:13 -070051 virtual aaudio_result_t updateStateMachine() override;
Phil Burk204a1632017-01-03 17:23:43 -080052
Phil Burkc0c70e32017-02-09 13:18:38 -080053 aaudio_result_t open(const AudioStreamBuilder &builder) override;
Phil Burk204a1632017-01-03 17:23:43 -080054
Phil Burkc0c70e32017-02-09 13:18:38 -080055 aaudio_result_t setBufferSize(int32_t requestedFrames) override;
Phil Burk204a1632017-01-03 17:23:43 -080056
Phil Burkc0c70e32017-02-09 13:18:38 -080057 int32_t getBufferSize() const override;
Phil Burk204a1632017-01-03 17:23:43 -080058
Phil Burkc0c70e32017-02-09 13:18:38 -080059 int32_t getBufferCapacity() const override;
Phil Burk204a1632017-01-03 17:23:43 -080060
Phil Burkc0c70e32017-02-09 13:18:38 -080061 int32_t getXRunCount() const override {
Phil Burk204a1632017-01-03 17:23:43 -080062 return mXRunCount;
63 }
64
Phil Burkc0c70e32017-02-09 13:18:38 -080065 aaudio_result_t registerThread() override;
Phil Burk204a1632017-01-03 17:23:43 -080066
Phil Burkc0c70e32017-02-09 13:18:38 -080067 aaudio_result_t unregisterThread() override;
Phil Burk204a1632017-01-03 17:23:43 -080068
Phil Burke4d7bb42017-03-28 11:32:39 -070069 // Called internally from 'C'
Phil Burk87c9f642017-05-17 07:22:39 -070070 virtual void *callbackLoop() = 0;
Phil Burke4d7bb42017-03-28 11:32:39 -070071
Phil Burke2fbb592017-05-01 15:05:52 -070072 bool isMMap() override {
73 return true;
74 }
75
Phil Burk87c9f642017-05-17 07:22:39 -070076 // Calculate timeout based on framesPerBurst
77 int64_t calculateReasonableTimeout();
78
Eric Laurentcb4dae22017-07-01 19:39:32 -070079 aaudio_result_t startClient(const android::AudioClient& client,
jiabind1f1cb62020-03-24 11:57:57 -070080 const audio_attributes_t *attr,
Eric Laurentcb4dae22017-07-01 19:39:32 -070081 audio_port_handle_t *clientHandle);
82
83 aaudio_result_t stopClient(audio_port_handle_t clientHandle);
84
Phil Burk39f02dd2017-08-04 09:13:31 -070085 aaudio_handle_t getServiceHandle() const {
86 return mServiceStreamHandle;
87 }
88
Phil Burk204a1632017-01-03 17:23:43 -080089protected:
Phil Burk0bd745e2020-10-17 18:20:01 +000090 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 Burk204a1632017-01-03 17:23:43 -080094
Phil Burk87c9f642017-05-17 07:22:39 -070095 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 Burkbcc36742017-08-31 17:24:51 -0700111 aaudio_result_t drainTimestampsFromService();
112
Phil Burk5ed503c2017-02-01 09:38:15 -0800113 aaudio_result_t processCommands();
Phil Burk204a1632017-01-03 17:23:43 -0800114
Phil Burkdd582922020-10-15 20:29:51 +0000115 aaudio_result_t stopCallback_l();
116
Phil Burkec8ca522020-05-19 10:05:58 -0700117 virtual void prepareBuffersForStart() {}
118
119 virtual void advanceClientToMatchServerPosition(int32_t serverMargin = 0) = 0;
Phil Burk204a1632017-01-03 17:23:43 -0800120
Phil Burkb336e892017-07-05 15:35:43 -0700121 virtual void onFlushFromServer() {}
Phil Burk204a1632017-01-03 17:23:43 -0800122
Phil Burk5ed503c2017-02-01 09:38:15 -0800123 aaudio_result_t onEventFromServer(AAudioServiceMessage *message);
Phil Burk204a1632017-01-03 17:23:43 -0800124
Phil Burk97350f92017-07-21 15:59:44 -0700125 aaudio_result_t onTimestampService(AAudioServiceMessage *message);
126
127 aaudio_result_t onTimestampHardware(AAudioServiceMessage *message);
Phil Burk204a1632017-01-03 17:23:43 -0800128
Phil Burkec89b2e2017-06-20 15:05:06 -0700129 void logTimestamp(AAudioServiceMessage &message);
130
Phil Burke4d7bb42017-03-28 11:32:39 -0700131 // Calculate timeout for an operation involving framesPerOperation.
132 int64_t calculateReasonableTimeout(int32_t framesPerOperation);
133
Phil Burk41f19d82018-02-13 14:59:10 -0800134 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 Burk87c9f642017-05-17 07:22:39 -0700140
Phil Burk377c1c22018-12-12 16:06:54 -0800141 /**
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 Burk87c9f642017-05-17 07:22:39 -0700149 IsochronousClockModel mClockModel; // timing model for chasing the HAL
150
Phil Burk5edc4ea2020-04-17 08:15:42 -0700151 std::unique_ptr<AudioEndpoint> mAudioEndpoint; // source for reads or sink for writes
152
Phil Burk87c9f642017-05-17 07:22:39 -0700153 aaudio_handle_t mServiceStreamHandle; // opaque handle returned from service
154
Phil Burk87c9f642017-05-17 07:22:39 -0700155 int32_t mXRunCount = 0; // how many underrun events?
156
Phil Burk87c9f642017-05-17 07:22:39 -0700157 // Offset from underlying frame position.
158 int64_t mFramesOffsetFromService = 0; // offset for timestamps
159
Phil Burkbf821e22020-04-17 11:51:43 -0700160 std::unique_ptr<uint8_t[]> mCallbackBuffer;
Phil Burk87c9f642017-05-17 07:22:39 -0700161 int32_t mCallbackFrames = 0;
162
Phil Burkec89b2e2017-06-20 15:05:06 -0700163 // The service uses this for SHARED mode.
164 bool mInService = false; // Is this running in the client or the service?
165
Phil Burkb336e892017-07-05 15:35:43 -0700166 AAudioServiceInterface &mServiceInterface; // abstract interface to the service
167
Phil Burka53ffa62018-10-10 16:21:37 -0700168 SimpleDoubleBuffer<Timestamp> mAtomicInternalTimestamp;
Phil Burkbcc36742017-08-31 17:24:51 -0700169
170 AtomicRequestor mNeedCatchUp; // Ask read() or write() to sync on first timestamp.
171
Phil Burk965650e2017-09-07 21:00:09 -0700172 float mStreamVolume = 1.0f;
173
Phil Burk5edc4ea2020-04-17 08:15:42 -0700174 int64_t mLastFramesWritten = 0;
175 int64_t mLastFramesRead = 0;
176
Phil Burk204a1632017-01-03 17:23:43 -0800177private:
Phil Burkc0c70e32017-02-09 13:18:38 -0800178 /*
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 Burkc0c70e32017-02-09 13:18:38 -0800186
Phil Burk87c9f642017-05-17 07:22:39 -0700187 // Adjust timing model based on timestamp from service.
188 void processTimestamp(uint64_t position, int64_t time);
Phil Burk71f35bb2017-04-13 16:05:07 -0700189
Phil Burkfd34a932017-07-19 07:03:52 -0700190 // 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 Burkb31b66f2019-09-30 09:33:41 -0700194 int32_t mTimeOffsetNanos = 0; // add to time part of an MMAP timestamp
Phil Burkfd34a932017-07-19 07:03:52 -0700195
Phil Burkc0c70e32017-02-09 13:18:38 -0800196 AudioEndpointParcelable mEndPointParcelable; // description of the buffers filled by service
197 EndpointDescriptor mEndpointDescriptor; // buffer description with resolved addresses
Phil Burk97350f92017-07-21 15:59:44 -0700198
Phil Burk97350f92017-07-21 15:59:44 -0700199 int64_t mServiceLatencyNanos = 0;
Phil Burk41f19d82018-02-13 14:59:10 -0800200
Phil Burk0127c1b2018-03-29 13:48:06 -0700201 // Sometimes the hardware is operating with a different channel count from the app.
202 // Then we require conversion in AAudio.
Phil Burk41f19d82018-02-13 14:59:10 -0800203 int32_t mDeviceChannelCount = 0;
Phil Burk8d4f0062019-10-03 15:55:41 -0700204
205 int32_t mBufferSizeInFrames = 0; // local threshold to control latency
Phil Burk5edc4ea2020-04-17 08:15:42 -0700206 int32_t mBufferCapacityInFrames = 0;
207
Phil Burk8d4f0062019-10-03 15:55:41 -0700208
Phil Burk204a1632017-01-03 17:23:43 -0800209};
210
Phil Burk5ed503c2017-02-01 09:38:15 -0800211} /* namespace aaudio */
Phil Burk204a1632017-01-03 17:23:43 -0800212
Phil Burk5204d312017-05-04 17:16:13 -0700213#endif //ANDROID_AAUDIO_AUDIO_STREAM_INTERNAL_H