blob: 3bb9e1ea1b161de09a8c73dac31df0ebd27069cf [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 Burkc0c70e32017-02-09 13:18:38 -080061 aaudio_result_t close() 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,
93 audio_port_handle_t *clientHandle);
94
95 aaudio_result_t stopClient(audio_port_handle_t clientHandle);
96
Phil Burk39f02dd2017-08-04 09:13:31 -070097 aaudio_handle_t getServiceHandle() const {
98 return mServiceStreamHandle;
99 }
100
Phil Burk204a1632017-01-03 17:23:43 -0800101protected:
102
Phil Burk87c9f642017-05-17 07:22:39 -0700103 aaudio_result_t processData(void *buffer,
104 int32_t numFrames,
105 int64_t timeoutNanoseconds);
106
107/**
108 * Low level data processing that will not block. It will just read or write as much as it can.
109 *
110 * It passed back a recommended time to wake up if wakeTimePtr is not NULL.
111 *
112 * @return the number of frames processed or a negative error code.
113 */
114 virtual aaudio_result_t processDataNow(void *buffer,
115 int32_t numFrames,
116 int64_t currentTimeNanos,
117 int64_t *wakeTimePtr) = 0;
118
Phil Burkbcc36742017-08-31 17:24:51 -0700119 aaudio_result_t drainTimestampsFromService();
120
Phil Burk5ed503c2017-02-01 09:38:15 -0800121 aaudio_result_t processCommands();
Phil Burk204a1632017-01-03 17:23:43 -0800122
Phil Burke4d7bb42017-03-28 11:32:39 -0700123 aaudio_result_t stopCallback();
124
Phil Burkbcc36742017-08-31 17:24:51 -0700125 virtual void advanceClientToMatchServerPosition() = 0;
Phil Burk204a1632017-01-03 17:23:43 -0800126
Phil Burkb336e892017-07-05 15:35:43 -0700127 virtual void onFlushFromServer() {}
Phil Burk204a1632017-01-03 17:23:43 -0800128
Phil Burk5ed503c2017-02-01 09:38:15 -0800129 aaudio_result_t onEventFromServer(AAudioServiceMessage *message);
Phil Burk204a1632017-01-03 17:23:43 -0800130
Phil Burk97350f92017-07-21 15:59:44 -0700131 aaudio_result_t onTimestampService(AAudioServiceMessage *message);
132
133 aaudio_result_t onTimestampHardware(AAudioServiceMessage *message);
Phil Burk204a1632017-01-03 17:23:43 -0800134
Phil Burkec89b2e2017-06-20 15:05:06 -0700135 void logTimestamp(AAudioServiceMessage &message);
136
Phil Burke4d7bb42017-03-28 11:32:39 -0700137 // Calculate timeout for an operation involving framesPerOperation.
138 int64_t calculateReasonableTimeout(int32_t framesPerOperation);
139
Phil Burk41f19d82018-02-13 14:59:10 -0800140 int32_t getDeviceChannelCount() const { return mDeviceChannelCount; }
141
142 /**
143 * @return true if running in audio service, versus in app process
144 */
145 bool isInService() const { return mInService; }
Phil Burk87c9f642017-05-17 07:22:39 -0700146
147 IsochronousClockModel mClockModel; // timing model for chasing the HAL
148
149 AudioEndpoint mAudioEndpoint; // source for reads or sink for writes
150 aaudio_handle_t mServiceStreamHandle; // opaque handle returned from service
151
Phil Burk6479d502017-11-20 09:32:52 -0800152 int32_t mFramesPerBurst = MIN_FRAMES_PER_BURST; // frames per HAL transfer
Phil Burk87c9f642017-05-17 07:22:39 -0700153 int32_t mXRunCount = 0; // how many underrun events?
154
Phil Burk87c9f642017-05-17 07:22:39 -0700155 // Offset from underlying frame position.
156 int64_t mFramesOffsetFromService = 0; // offset for timestamps
157
158 uint8_t *mCallbackBuffer = nullptr;
159 int32_t mCallbackFrames = 0;
160
Phil Burkec89b2e2017-06-20 15:05:06 -0700161 // The service uses this for SHARED mode.
162 bool mInService = false; // Is this running in the client or the service?
163
Phil Burkb336e892017-07-05 15:35:43 -0700164 AAudioServiceInterface &mServiceInterface; // abstract interface to the service
165
Phil Burkbcc36742017-08-31 17:24:51 -0700166 SimpleDoubleBuffer<Timestamp> mAtomicTimestamp;
167
168 AtomicRequestor mNeedCatchUp; // Ask read() or write() to sync on first timestamp.
169
Phil Burk965650e2017-09-07 21:00:09 -0700170 float mStreamVolume = 1.0f;
171
Phil Burk204a1632017-01-03 17:23:43 -0800172private:
Phil Burkc0c70e32017-02-09 13:18:38 -0800173 /*
174 * Asynchronous write with data conversion.
175 * @param buffer
176 * @param numFrames
177 * @return fdrames written or negative error
178 */
179 aaudio_result_t writeNowWithConversion(const void *buffer,
180 int32_t numFrames);
Phil Burkc0c70e32017-02-09 13:18:38 -0800181
Phil Burk87c9f642017-05-17 07:22:39 -0700182 // Adjust timing model based on timestamp from service.
183 void processTimestamp(uint64_t position, int64_t time);
Phil Burk71f35bb2017-04-13 16:05:07 -0700184
Phil Burkfd34a932017-07-19 07:03:52 -0700185 // Thread on other side of FIFO will have wakeup jitter.
186 // By delaying slightly we can avoid waking up before other side is ready.
187 const int32_t mWakeupDelayNanos; // delay past typical wakeup jitter
188 const int32_t mMinimumSleepNanos; // minimum sleep while polling
189
Phil Burkc0c70e32017-02-09 13:18:38 -0800190 AudioEndpointParcelable mEndPointParcelable; // description of the buffers filled by service
191 EndpointDescriptor mEndpointDescriptor; // buffer description with resolved addresses
Phil Burk97350f92017-07-21 15:59:44 -0700192
Phil Burk97350f92017-07-21 15:59:44 -0700193 int64_t mServiceLatencyNanos = 0;
Phil Burk41f19d82018-02-13 14:59:10 -0800194
Phil Burk0127c1b2018-03-29 13:48:06 -0700195 // Sometimes the hardware is operating with a different channel count from the app.
196 // Then we require conversion in AAudio.
Phil Burk41f19d82018-02-13 14:59:10 -0800197 int32_t mDeviceChannelCount = 0;
Phil Burk204a1632017-01-03 17:23:43 -0800198};
199
Phil Burk5ed503c2017-02-01 09:38:15 -0800200} /* namespace aaudio */
Phil Burk204a1632017-01-03 17:23:43 -0800201
Phil Burk5204d312017-05-04 17:16:13 -0700202#endif //ANDROID_AAUDIO_AUDIO_STREAM_INTERNAL_H