blob: 117756d8c2264d5bf9397c7882bf9d1f5d0bea84 [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 Burk71f35bb2017-04-13 16:05:07 -0700124 aaudio_result_t requestStopInternal();
Phil Burke4d7bb42017-03-28 11:32:39 -0700125
126 aaudio_result_t stopCallback();
127
Phil Burkbcc36742017-08-31 17:24:51 -0700128 virtual void advanceClientToMatchServerPosition() = 0;
Phil Burk204a1632017-01-03 17:23:43 -0800129
Phil Burkb336e892017-07-05 15:35:43 -0700130 virtual void onFlushFromServer() {}
Phil Burk204a1632017-01-03 17:23:43 -0800131
Phil Burk5ed503c2017-02-01 09:38:15 -0800132 aaudio_result_t onEventFromServer(AAudioServiceMessage *message);
Phil Burk204a1632017-01-03 17:23:43 -0800133
Phil Burk97350f92017-07-21 15:59:44 -0700134 aaudio_result_t onTimestampService(AAudioServiceMessage *message);
135
136 aaudio_result_t onTimestampHardware(AAudioServiceMessage *message);
Phil Burk204a1632017-01-03 17:23:43 -0800137
Phil Burkec89b2e2017-06-20 15:05:06 -0700138 void logTimestamp(AAudioServiceMessage &message);
139
Phil Burke4d7bb42017-03-28 11:32:39 -0700140 // Calculate timeout for an operation involving framesPerOperation.
141 int64_t calculateReasonableTimeout(int32_t framesPerOperation);
142
Phil Burk9dca9822017-05-26 14:27:43 -0700143 aaudio_format_t mDeviceFormat = AAUDIO_FORMAT_UNSPECIFIED;
Phil Burk87c9f642017-05-17 07:22:39 -0700144
145 IsochronousClockModel mClockModel; // timing model for chasing the HAL
146
147 AudioEndpoint mAudioEndpoint; // source for reads or sink for writes
148 aaudio_handle_t mServiceStreamHandle; // opaque handle returned from service
149
Phil Burk6479d502017-11-20 09:32:52 -0800150 int32_t mFramesPerBurst = MIN_FRAMES_PER_BURST; // frames per HAL transfer
Phil Burk87c9f642017-05-17 07:22:39 -0700151 int32_t mXRunCount = 0; // how many underrun events?
152
Phil Burk87c9f642017-05-17 07:22:39 -0700153 // Offset from underlying frame position.
154 int64_t mFramesOffsetFromService = 0; // offset for timestamps
155
156 uint8_t *mCallbackBuffer = nullptr;
157 int32_t mCallbackFrames = 0;
158
Phil Burkec89b2e2017-06-20 15:05:06 -0700159 // The service uses this for SHARED mode.
160 bool mInService = false; // Is this running in the client or the service?
161
Phil Burkb336e892017-07-05 15:35:43 -0700162 AAudioServiceInterface &mServiceInterface; // abstract interface to the service
163
Phil Burkbcc36742017-08-31 17:24:51 -0700164 SimpleDoubleBuffer<Timestamp> mAtomicTimestamp;
165
166 AtomicRequestor mNeedCatchUp; // Ask read() or write() to sync on first timestamp.
167
Phil Burk965650e2017-09-07 21:00:09 -0700168 float mStreamVolume = 1.0f;
169
Phil Burk204a1632017-01-03 17:23:43 -0800170private:
Phil Burkc0c70e32017-02-09 13:18:38 -0800171 /*
172 * Asynchronous write with data conversion.
173 * @param buffer
174 * @param numFrames
175 * @return fdrames written or negative error
176 */
177 aaudio_result_t writeNowWithConversion(const void *buffer,
178 int32_t numFrames);
Phil Burkc0c70e32017-02-09 13:18:38 -0800179
Phil Burk87c9f642017-05-17 07:22:39 -0700180 // Adjust timing model based on timestamp from service.
181 void processTimestamp(uint64_t position, int64_t time);
Phil Burk71f35bb2017-04-13 16:05:07 -0700182
Phil Burkfd34a932017-07-19 07:03:52 -0700183 // Thread on other side of FIFO will have wakeup jitter.
184 // By delaying slightly we can avoid waking up before other side is ready.
185 const int32_t mWakeupDelayNanos; // delay past typical wakeup jitter
186 const int32_t mMinimumSleepNanos; // minimum sleep while polling
187
Phil Burkc0c70e32017-02-09 13:18:38 -0800188 AudioEndpointParcelable mEndPointParcelable; // description of the buffers filled by service
189 EndpointDescriptor mEndpointDescriptor; // buffer description with resolved addresses
Phil Burk97350f92017-07-21 15:59:44 -0700190
Phil Burk97350f92017-07-21 15:59:44 -0700191 int64_t mServiceLatencyNanos = 0;
Phil Burk204a1632017-01-03 17:23:43 -0800192};
193
Phil Burk5ed503c2017-02-01 09:38:15 -0800194} /* namespace aaudio */
Phil Burk204a1632017-01-03 17:23:43 -0800195
Phil Burk5204d312017-05-04 17:16:13 -0700196#endif //ANDROID_AAUDIO_AUDIO_STREAM_INTERNAL_H