blob: 0e0724beb3a0bd4e3402321269754a87d4070bb0 [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 Burke572f462017-04-20 13:03:19 -070030#include "utility/LinearRamp.h"
Phil Burkc0c70e32017-02-09 13:18:38 -080031
Phil Burk204a1632017-01-03 17:23:43 -080032using android::sp;
Phil Burk5ed503c2017-02-01 09:38:15 -080033using android::IAAudioService;
Phil Burk204a1632017-01-03 17:23:43 -080034
Phil Burk5ed503c2017-02-01 09:38:15 -080035namespace aaudio {
Phil Burk204a1632017-01-03 17:23:43 -080036
Phil Burk6479d502017-11-20 09:32:52 -080037 // These are intended to be outside the range of what is normally encountered.
38 // TODO MAXes should probably be much bigger.
39 constexpr int32_t MIN_FRAMES_PER_BURST = 16; // arbitrary
40 constexpr int32_t MAX_FRAMES_PER_BURST = 16 * 1024; // arbitrary
41 constexpr int32_t MAX_BUFFER_CAPACITY_IN_FRAMES = 32 * 1024; // arbitrary
42
Phil Burk5ed503c2017-02-01 09:38:15 -080043// A stream that talks to the AAudioService or directly to a HAL.
Phil Burk965650e2017-09-07 21:00:09 -070044class AudioStreamInternal : public AudioStream {
Phil Burk204a1632017-01-03 17:23:43 -080045
46public:
Phil Burk87c9f642017-05-17 07:22:39 -070047 AudioStreamInternal(AAudioServiceInterface &serviceInterface, bool inService);
Phil Burk204a1632017-01-03 17:23:43 -080048 virtual ~AudioStreamInternal();
49
Phil Burkc0c70e32017-02-09 13:18:38 -080050 aaudio_result_t requestStart() override;
Phil Burk204a1632017-01-03 17:23:43 -080051
Phil Burkc0c70e32017-02-09 13:18:38 -080052 aaudio_result_t requestStop() override;
Phil Burk204a1632017-01-03 17:23:43 -080053
Phil Burkc0c70e32017-02-09 13:18:38 -080054 aaudio_result_t getTimestamp(clockid_t clockId,
Phil Burk3316d5e2017-02-15 11:23:01 -080055 int64_t *framePosition,
56 int64_t *timeNanoseconds) override;
Phil Burk204a1632017-01-03 17:23:43 -080057
Phil Burk0befec62017-07-28 15:12:13 -070058 virtual aaudio_result_t updateStateMachine() override;
Phil Burk204a1632017-01-03 17:23:43 -080059
Phil Burkc0c70e32017-02-09 13:18:38 -080060 aaudio_result_t open(const AudioStreamBuilder &builder) override;
Phil Burk204a1632017-01-03 17:23:43 -080061
Phil Burkc0c70e32017-02-09 13:18:38 -080062 aaudio_result_t close() override;
Phil Burk204a1632017-01-03 17:23:43 -080063
Phil Burkc0c70e32017-02-09 13:18:38 -080064 aaudio_result_t setBufferSize(int32_t requestedFrames) override;
Phil Burk204a1632017-01-03 17:23:43 -080065
Phil Burkc0c70e32017-02-09 13:18:38 -080066 int32_t getBufferSize() const override;
Phil Burk204a1632017-01-03 17:23:43 -080067
Phil Burkc0c70e32017-02-09 13:18:38 -080068 int32_t getBufferCapacity() const override;
Phil Burk204a1632017-01-03 17:23:43 -080069
Phil Burkc0c70e32017-02-09 13:18:38 -080070 int32_t getFramesPerBurst() const override;
Phil Burk204a1632017-01-03 17:23:43 -080071
Phil Burkc0c70e32017-02-09 13:18:38 -080072 int32_t getXRunCount() const override {
Phil Burk204a1632017-01-03 17:23:43 -080073 return mXRunCount;
74 }
75
Phil Burkc0c70e32017-02-09 13:18:38 -080076 aaudio_result_t registerThread() override;
Phil Burk204a1632017-01-03 17:23:43 -080077
Phil Burkc0c70e32017-02-09 13:18:38 -080078 aaudio_result_t unregisterThread() override;
Phil Burk204a1632017-01-03 17:23:43 -080079
Phil Burk87c9f642017-05-17 07:22:39 -070080 aaudio_result_t joinThread(void** returnArg);
81
Phil Burke4d7bb42017-03-28 11:32:39 -070082 // Called internally from 'C'
Phil Burk87c9f642017-05-17 07:22:39 -070083 virtual void *callbackLoop() = 0;
Phil Burke4d7bb42017-03-28 11:32:39 -070084
Phil Burke2fbb592017-05-01 15:05:52 -070085
86 bool isMMap() override {
87 return true;
88 }
89
Phil Burk87c9f642017-05-17 07:22:39 -070090 // Calculate timeout based on framesPerBurst
91 int64_t calculateReasonableTimeout();
92
Eric Laurentcb4dae22017-07-01 19:39:32 -070093 aaudio_result_t startClient(const android::AudioClient& client,
94 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 aaudio_format_t getDeviceFormat() const { return mDeviceFormat; }
142
143 int32_t getDeviceChannelCount() const { return mDeviceChannelCount; }
144
145 /**
146 * @return true if running in audio service, versus in app process
147 */
148 bool isInService() const { return mInService; }
Phil Burk87c9f642017-05-17 07:22:39 -0700149
150 IsochronousClockModel mClockModel; // timing model for chasing the HAL
151
152 AudioEndpoint mAudioEndpoint; // source for reads or sink for writes
153 aaudio_handle_t mServiceStreamHandle; // opaque handle returned from service
154
Phil Burk6479d502017-11-20 09:32:52 -0800155 int32_t mFramesPerBurst = MIN_FRAMES_PER_BURST; // frames per HAL transfer
Phil Burk87c9f642017-05-17 07:22:39 -0700156 int32_t mXRunCount = 0; // how many underrun events?
157
Phil Burk87c9f642017-05-17 07:22:39 -0700158 // Offset from underlying frame position.
159 int64_t mFramesOffsetFromService = 0; // offset for timestamps
160
161 uint8_t *mCallbackBuffer = nullptr;
162 int32_t mCallbackFrames = 0;
163
Phil Burkec89b2e2017-06-20 15:05:06 -0700164 // The service uses this for SHARED mode.
165 bool mInService = false; // Is this running in the client or the service?
166
Phil Burkb336e892017-07-05 15:35:43 -0700167 AAudioServiceInterface &mServiceInterface; // abstract interface to the service
168
Phil Burkbcc36742017-08-31 17:24:51 -0700169 SimpleDoubleBuffer<Timestamp> mAtomicTimestamp;
170
171 AtomicRequestor mNeedCatchUp; // Ask read() or write() to sync on first timestamp.
172
Phil Burk965650e2017-09-07 21:00:09 -0700173 float mStreamVolume = 1.0f;
174
Phil Burk204a1632017-01-03 17:23:43 -0800175private:
Phil Burkc0c70e32017-02-09 13:18:38 -0800176 /*
177 * Asynchronous write with data conversion.
178 * @param buffer
179 * @param numFrames
180 * @return fdrames written or negative error
181 */
182 aaudio_result_t writeNowWithConversion(const void *buffer,
183 int32_t numFrames);
Phil Burkc0c70e32017-02-09 13:18:38 -0800184
Phil Burk87c9f642017-05-17 07:22:39 -0700185 // Adjust timing model based on timestamp from service.
186 void processTimestamp(uint64_t position, int64_t time);
Phil Burk71f35bb2017-04-13 16:05:07 -0700187
Phil Burkfd34a932017-07-19 07:03:52 -0700188 // Thread on other side of FIFO will have wakeup jitter.
189 // By delaying slightly we can avoid waking up before other side is ready.
190 const int32_t mWakeupDelayNanos; // delay past typical wakeup jitter
191 const int32_t mMinimumSleepNanos; // minimum sleep while polling
192
Phil Burkc0c70e32017-02-09 13:18:38 -0800193 AudioEndpointParcelable mEndPointParcelable; // description of the buffers filled by service
194 EndpointDescriptor mEndpointDescriptor; // buffer description with resolved addresses
Phil Burk97350f92017-07-21 15:59:44 -0700195
Phil Burk97350f92017-07-21 15:59:44 -0700196 int64_t mServiceLatencyNanos = 0;
Phil Burk41f19d82018-02-13 14:59:10 -0800197
198 // Sometimes the hardware is operating with a different format or channel count from the app.
199 // Then we require conversion in AAudio.
200 aaudio_format_t mDeviceFormat = AAUDIO_FORMAT_UNSPECIFIED;
201 int32_t mDeviceChannelCount = 0;
Phil Burk204a1632017-01-03 17:23:43 -0800202};
203
Phil Burk5ed503c2017-02-01 09:38:15 -0800204} /* namespace aaudio */
Phil Burk204a1632017-01-03 17:23:43 -0800205
Phil Burk5204d312017-05-04 17:16:13 -0700206#endif //ANDROID_AAUDIO_AUDIO_STREAM_INTERNAL_H