blob: 61591b3a13256636ce098a6fd7714b6dc7fb529f [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 Burk5ed503c2017-02-01 09:38:15 -080023#include "binding/IAAudioService.h"
Phil Burk204a1632017-01-03 17:23:43 -080024#include "binding/AudioEndpointParcelable.h"
Phil Burke572f462017-04-20 13:03:19 -070025#include "binding/AAudioServiceInterface.h"
Phil Burk204a1632017-01-03 17:23:43 -080026#include "client/IsochronousClockModel.h"
27#include "client/AudioEndpoint.h"
28#include "core/AudioStream.h"
Phil Burkfd34a932017-07-19 07:03:52 -070029#include "utility/AudioClock.h"
Phil Burkc0c70e32017-02-09 13:18:38 -080030
Phil Burk204a1632017-01-03 17:23:43 -080031using android::sp;
Phil Burk5ed503c2017-02-01 09:38:15 -080032using android::IAAudioService;
Phil Burk204a1632017-01-03 17:23:43 -080033
Phil Burk5ed503c2017-02-01 09:38:15 -080034namespace aaudio {
Phil Burk204a1632017-01-03 17:23:43 -080035
Phil Burk6479d502017-11-20 09:32:52 -080036 // These are intended to be outside the range of what is normally encountered.
37 // TODO MAXes should probably be much bigger.
38 constexpr int32_t MIN_FRAMES_PER_BURST = 16; // arbitrary
39 constexpr int32_t MAX_FRAMES_PER_BURST = 16 * 1024; // arbitrary
40 constexpr int32_t MAX_BUFFER_CAPACITY_IN_FRAMES = 32 * 1024; // arbitrary
41
Phil Burk5ed503c2017-02-01 09:38:15 -080042// A stream that talks to the AAudioService or directly to a HAL.
Phil Burk965650e2017-09-07 21:00:09 -070043class AudioStreamInternal : public AudioStream {
Phil Burk204a1632017-01-03 17:23:43 -080044
45public:
Phil Burk87c9f642017-05-17 07:22:39 -070046 AudioStreamInternal(AAudioServiceInterface &serviceInterface, bool inService);
Phil Burk204a1632017-01-03 17:23:43 -080047 virtual ~AudioStreamInternal();
48
Phil Burkc0c70e32017-02-09 13:18:38 -080049 aaudio_result_t requestStart() override;
Phil Burk204a1632017-01-03 17:23:43 -080050
Phil Burkc0c70e32017-02-09 13:18:38 -080051 aaudio_result_t requestStop() override;
Phil Burk204a1632017-01-03 17:23:43 -080052
Phil Burkc0c70e32017-02-09 13:18:38 -080053 aaudio_result_t getTimestamp(clockid_t clockId,
Phil Burk3316d5e2017-02-15 11:23:01 -080054 int64_t *framePosition,
55 int64_t *timeNanoseconds) override;
Phil Burk204a1632017-01-03 17:23:43 -080056
Phil Burk0befec62017-07-28 15:12:13 -070057 virtual aaudio_result_t updateStateMachine() override;
Phil Burk204a1632017-01-03 17:23:43 -080058
Phil Burkc0c70e32017-02-09 13:18:38 -080059 aaudio_result_t open(const AudioStreamBuilder &builder) override;
Phil Burk204a1632017-01-03 17:23:43 -080060
Phil Burk8b4e05e2019-12-17 12:12:09 -080061 aaudio_result_t release_l() override;
Phil Burk204a1632017-01-03 17:23:43 -080062
Phil Burkc0c70e32017-02-09 13:18:38 -080063 aaudio_result_t setBufferSize(int32_t requestedFrames) override;
Phil Burk204a1632017-01-03 17:23:43 -080064
Phil Burkc0c70e32017-02-09 13:18:38 -080065 int32_t getBufferSize() const override;
Phil Burk204a1632017-01-03 17:23:43 -080066
Phil Burkc0c70e32017-02-09 13:18:38 -080067 int32_t getBufferCapacity() const override;
Phil Burk204a1632017-01-03 17:23:43 -080068
Phil Burkc0c70e32017-02-09 13:18:38 -080069 int32_t getFramesPerBurst() const override;
Phil Burk204a1632017-01-03 17:23:43 -080070
Phil Burkc0c70e32017-02-09 13:18:38 -080071 int32_t getXRunCount() const override {
Phil Burk204a1632017-01-03 17:23:43 -080072 return mXRunCount;
73 }
74
Phil Burkc0c70e32017-02-09 13:18:38 -080075 aaudio_result_t registerThread() override;
Phil Burk204a1632017-01-03 17:23:43 -080076
Phil Burkc0c70e32017-02-09 13:18:38 -080077 aaudio_result_t unregisterThread() override;
Phil Burk204a1632017-01-03 17:23:43 -080078
Phil Burk87c9f642017-05-17 07:22:39 -070079 aaudio_result_t joinThread(void** returnArg);
80
Phil Burke4d7bb42017-03-28 11:32:39 -070081 // Called internally from 'C'
Phil Burk87c9f642017-05-17 07:22:39 -070082 virtual void *callbackLoop() = 0;
Phil Burke4d7bb42017-03-28 11:32:39 -070083
Phil Burke2fbb592017-05-01 15:05:52 -070084
85 bool isMMap() override {
86 return true;
87 }
88
Phil Burk87c9f642017-05-17 07:22:39 -070089 // Calculate timeout based on framesPerBurst
90 int64_t calculateReasonableTimeout();
91
Eric Laurentcb4dae22017-07-01 19:39:32 -070092 aaudio_result_t startClient(const android::AudioClient& client,
jiabind1f1cb62020-03-24 11:57:57 -070093 const audio_attributes_t *attr,
Eric Laurentcb4dae22017-07-01 19:39:32 -070094 audio_port_handle_t *clientHandle);
95
96 aaudio_result_t stopClient(audio_port_handle_t clientHandle);
97
Phil Burk39f02dd2017-08-04 09:13:31 -070098 aaudio_handle_t getServiceHandle() const {
99 return mServiceStreamHandle;
100 }
101
Phil Burk204a1632017-01-03 17:23:43 -0800102protected:
103
Phil Burk87c9f642017-05-17 07:22:39 -0700104 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 Burkbcc36742017-08-31 17:24:51 -0700120 aaudio_result_t drainTimestampsFromService();
121
Phil Burk5ed503c2017-02-01 09:38:15 -0800122 aaudio_result_t processCommands();
Phil Burk204a1632017-01-03 17:23:43 -0800123
Phil Burke4d7bb42017-03-28 11:32:39 -0700124 aaudio_result_t stopCallback();
125
Phil Burkbcc36742017-08-31 17:24:51 -0700126 virtual void advanceClientToMatchServerPosition() = 0;
Phil Burk204a1632017-01-03 17:23:43 -0800127
Phil Burkb336e892017-07-05 15:35:43 -0700128 virtual void onFlushFromServer() {}
Phil Burk204a1632017-01-03 17:23:43 -0800129
Phil Burk5ed503c2017-02-01 09:38:15 -0800130 aaudio_result_t onEventFromServer(AAudioServiceMessage *message);
Phil Burk204a1632017-01-03 17:23:43 -0800131
Phil Burk97350f92017-07-21 15:59:44 -0700132 aaudio_result_t onTimestampService(AAudioServiceMessage *message);
133
134 aaudio_result_t onTimestampHardware(AAudioServiceMessage *message);
Phil Burk204a1632017-01-03 17:23:43 -0800135
Phil Burkec89b2e2017-06-20 15:05:06 -0700136 void logTimestamp(AAudioServiceMessage &message);
137
Phil Burke4d7bb42017-03-28 11:32:39 -0700138 // Calculate timeout for an operation involving framesPerOperation.
139 int64_t calculateReasonableTimeout(int32_t framesPerOperation);
140
Phil Burk41f19d82018-02-13 14:59:10 -0800141 int32_t getDeviceChannelCount() const { return mDeviceChannelCount; }
142
143 /**
144 * @return true if running in audio service, versus in app process
145 */
146 bool isInService() const { return mInService; }
Phil Burk87c9f642017-05-17 07:22:39 -0700147
Phil Burk377c1c22018-12-12 16:06:54 -0800148 /**
149 * Is the service FIFO position currently controlled by the AAudio service or HAL,
150 * or set based on the Clock Model.
151 *
152 * @return true if the ClockModel is currently determining the FIFO position
153 */
154 bool isClockModelInControl() const;
155
Phil Burk87c9f642017-05-17 07:22:39 -0700156 IsochronousClockModel mClockModel; // timing model for chasing the HAL
157
Phil Burk5edc4ea2020-04-17 08:15:42 -0700158 std::unique_ptr<AudioEndpoint> mAudioEndpoint; // source for reads or sink for writes
159
Phil Burk87c9f642017-05-17 07:22:39 -0700160 aaudio_handle_t mServiceStreamHandle; // opaque handle returned from service
161
Phil Burk6479d502017-11-20 09:32:52 -0800162 int32_t mFramesPerBurst = MIN_FRAMES_PER_BURST; // frames per HAL transfer
Phil Burk87c9f642017-05-17 07:22:39 -0700163 int32_t mXRunCount = 0; // how many underrun events?
164
Phil Burk87c9f642017-05-17 07:22:39 -0700165 // Offset from underlying frame position.
166 int64_t mFramesOffsetFromService = 0; // offset for timestamps
167
Phil Burkbf821e22020-04-17 11:51:43 -0700168 std::unique_ptr<uint8_t[]> mCallbackBuffer;
Phil Burk87c9f642017-05-17 07:22:39 -0700169 int32_t mCallbackFrames = 0;
170
Phil Burkec89b2e2017-06-20 15:05:06 -0700171 // The service uses this for SHARED mode.
172 bool mInService = false; // Is this running in the client or the service?
173
Phil Burkb336e892017-07-05 15:35:43 -0700174 AAudioServiceInterface &mServiceInterface; // abstract interface to the service
175
Phil Burka53ffa62018-10-10 16:21:37 -0700176 SimpleDoubleBuffer<Timestamp> mAtomicInternalTimestamp;
Phil Burkbcc36742017-08-31 17:24:51 -0700177
178 AtomicRequestor mNeedCatchUp; // Ask read() or write() to sync on first timestamp.
179
Phil Burk965650e2017-09-07 21:00:09 -0700180 float mStreamVolume = 1.0f;
181
Phil Burk5edc4ea2020-04-17 08:15:42 -0700182 int64_t mLastFramesWritten = 0;
183 int64_t mLastFramesRead = 0;
184
Phil Burk204a1632017-01-03 17:23:43 -0800185private:
Phil Burkc0c70e32017-02-09 13:18:38 -0800186 /*
187 * Asynchronous write with data conversion.
188 * @param buffer
189 * @param numFrames
190 * @return fdrames written or negative error
191 */
192 aaudio_result_t writeNowWithConversion(const void *buffer,
193 int32_t numFrames);
Phil Burkc0c70e32017-02-09 13:18:38 -0800194
Phil Burk87c9f642017-05-17 07:22:39 -0700195 // Adjust timing model based on timestamp from service.
196 void processTimestamp(uint64_t position, int64_t time);
Phil Burk71f35bb2017-04-13 16:05:07 -0700197
Phil Burkfd34a932017-07-19 07:03:52 -0700198 // Thread on other side of FIFO will have wakeup jitter.
199 // By delaying slightly we can avoid waking up before other side is ready.
200 const int32_t mWakeupDelayNanos; // delay past typical wakeup jitter
201 const int32_t mMinimumSleepNanos; // minimum sleep while polling
Phil Burkb31b66f2019-09-30 09:33:41 -0700202 int32_t mTimeOffsetNanos = 0; // add to time part of an MMAP timestamp
Phil Burkfd34a932017-07-19 07:03:52 -0700203
Phil Burkc0c70e32017-02-09 13:18:38 -0800204 AudioEndpointParcelable mEndPointParcelable; // description of the buffers filled by service
205 EndpointDescriptor mEndpointDescriptor; // buffer description with resolved addresses
Phil Burk97350f92017-07-21 15:59:44 -0700206
Phil Burk97350f92017-07-21 15:59:44 -0700207 int64_t mServiceLatencyNanos = 0;
Phil Burk41f19d82018-02-13 14:59:10 -0800208
Phil Burk0127c1b2018-03-29 13:48:06 -0700209 // Sometimes the hardware is operating with a different channel count from the app.
210 // Then we require conversion in AAudio.
Phil Burk41f19d82018-02-13 14:59:10 -0800211 int32_t mDeviceChannelCount = 0;
Phil Burk8d4f0062019-10-03 15:55:41 -0700212
213 int32_t mBufferSizeInFrames = 0; // local threshold to control latency
Phil Burk5edc4ea2020-04-17 08:15:42 -0700214 int32_t mBufferCapacityInFrames = 0;
215
Phil Burk8d4f0062019-10-03 15:55:41 -0700216
Phil Burk204a1632017-01-03 17:23:43 -0800217};
218
Phil Burk5ed503c2017-02-01 09:38:15 -0800219} /* namespace aaudio */
Phil Burk204a1632017-01-03 17:23:43 -0800220
Phil Burk5204d312017-05-04 17:16:13 -0700221#endif //ANDROID_AAUDIO_AUDIO_STREAM_INTERNAL_H