blob: 47024c08dcb59ef43cc9032ba8ae56eda9a5eb1d [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 Burk5ed503c2017-02-01 09:38:15 -080037// A stream that talks to the AAudioService or directly to a HAL.
Phil Burk965650e2017-09-07 21:00:09 -070038class AudioStreamInternal : public AudioStream {
Phil Burk204a1632017-01-03 17:23:43 -080039
40public:
Phil Burk87c9f642017-05-17 07:22:39 -070041 AudioStreamInternal(AAudioServiceInterface &serviceInterface, bool inService);
Phil Burk204a1632017-01-03 17:23:43 -080042 virtual ~AudioStreamInternal();
43
Phil Burkc0c70e32017-02-09 13:18:38 -080044 aaudio_result_t requestStart() override;
Phil Burk204a1632017-01-03 17:23:43 -080045
Phil Burkc0c70e32017-02-09 13:18:38 -080046 aaudio_result_t requestStop() override;
Phil Burk204a1632017-01-03 17:23:43 -080047
Phil Burkc0c70e32017-02-09 13:18:38 -080048 aaudio_result_t getTimestamp(clockid_t clockId,
Phil Burk3316d5e2017-02-15 11:23:01 -080049 int64_t *framePosition,
50 int64_t *timeNanoseconds) override;
Phil Burk204a1632017-01-03 17:23:43 -080051
Phil Burk0befec62017-07-28 15:12:13 -070052 virtual aaudio_result_t updateStateMachine() override;
Phil Burk204a1632017-01-03 17:23:43 -080053
Phil Burkc0c70e32017-02-09 13:18:38 -080054 aaudio_result_t open(const AudioStreamBuilder &builder) override;
Phil Burk204a1632017-01-03 17:23:43 -080055
Phil Burkc0c70e32017-02-09 13:18:38 -080056 aaudio_result_t close() override;
Phil Burk204a1632017-01-03 17:23:43 -080057
Phil Burkc0c70e32017-02-09 13:18:38 -080058 aaudio_result_t setBufferSize(int32_t requestedFrames) override;
Phil Burk204a1632017-01-03 17:23:43 -080059
Phil Burkc0c70e32017-02-09 13:18:38 -080060 int32_t getBufferSize() const override;
Phil Burk204a1632017-01-03 17:23:43 -080061
Phil Burkc0c70e32017-02-09 13:18:38 -080062 int32_t getBufferCapacity() const override;
Phil Burk204a1632017-01-03 17:23:43 -080063
Phil Burkc0c70e32017-02-09 13:18:38 -080064 int32_t getFramesPerBurst() const override;
Phil Burk204a1632017-01-03 17:23:43 -080065
Phil Burkc0c70e32017-02-09 13:18:38 -080066 int32_t getXRunCount() const override {
Phil Burk204a1632017-01-03 17:23:43 -080067 return mXRunCount;
68 }
69
Phil Burkc0c70e32017-02-09 13:18:38 -080070 aaudio_result_t registerThread() override;
Phil Burk204a1632017-01-03 17:23:43 -080071
Phil Burkc0c70e32017-02-09 13:18:38 -080072 aaudio_result_t unregisterThread() override;
Phil Burk204a1632017-01-03 17:23:43 -080073
Phil Burk87c9f642017-05-17 07:22:39 -070074 aaudio_result_t joinThread(void** returnArg);
75
Phil Burke4d7bb42017-03-28 11:32:39 -070076 // Called internally from 'C'
Phil Burk87c9f642017-05-17 07:22:39 -070077 virtual void *callbackLoop() = 0;
Phil Burke4d7bb42017-03-28 11:32:39 -070078
Phil Burke2fbb592017-05-01 15:05:52 -070079
80 bool isMMap() override {
81 return true;
82 }
83
Phil Burk87c9f642017-05-17 07:22:39 -070084 // Calculate timeout based on framesPerBurst
85 int64_t calculateReasonableTimeout();
86
Eric Laurentcb4dae22017-07-01 19:39:32 -070087 aaudio_result_t startClient(const android::AudioClient& client,
88 audio_port_handle_t *clientHandle);
89
90 aaudio_result_t stopClient(audio_port_handle_t clientHandle);
91
Phil Burk39f02dd2017-08-04 09:13:31 -070092 aaudio_handle_t getServiceHandle() const {
93 return mServiceStreamHandle;
94 }
95
Phil Burk204a1632017-01-03 17:23:43 -080096protected:
97
Phil Burk87c9f642017-05-17 07:22:39 -070098 aaudio_result_t processData(void *buffer,
99 int32_t numFrames,
100 int64_t timeoutNanoseconds);
101
102/**
103 * Low level data processing that will not block. It will just read or write as much as it can.
104 *
105 * It passed back a recommended time to wake up if wakeTimePtr is not NULL.
106 *
107 * @return the number of frames processed or a negative error code.
108 */
109 virtual aaudio_result_t processDataNow(void *buffer,
110 int32_t numFrames,
111 int64_t currentTimeNanos,
112 int64_t *wakeTimePtr) = 0;
113
Phil Burkbcc36742017-08-31 17:24:51 -0700114 aaudio_result_t drainTimestampsFromService();
115
Phil Burk5ed503c2017-02-01 09:38:15 -0800116 aaudio_result_t processCommands();
Phil Burk204a1632017-01-03 17:23:43 -0800117
Phil Burk71f35bb2017-04-13 16:05:07 -0700118 aaudio_result_t requestStopInternal();
Phil Burke4d7bb42017-03-28 11:32:39 -0700119
120 aaudio_result_t stopCallback();
121
Phil Burkbcc36742017-08-31 17:24:51 -0700122 virtual void advanceClientToMatchServerPosition() = 0;
Phil Burk204a1632017-01-03 17:23:43 -0800123
Phil Burkb336e892017-07-05 15:35:43 -0700124 virtual void onFlushFromServer() {}
Phil Burk204a1632017-01-03 17:23:43 -0800125
Phil Burk5ed503c2017-02-01 09:38:15 -0800126 aaudio_result_t onEventFromServer(AAudioServiceMessage *message);
Phil Burk204a1632017-01-03 17:23:43 -0800127
Phil Burk97350f92017-07-21 15:59:44 -0700128 aaudio_result_t onTimestampService(AAudioServiceMessage *message);
129
130 aaudio_result_t onTimestampHardware(AAudioServiceMessage *message);
Phil Burk204a1632017-01-03 17:23:43 -0800131
Phil Burkec89b2e2017-06-20 15:05:06 -0700132 void logTimestamp(AAudioServiceMessage &message);
133
Phil Burke4d7bb42017-03-28 11:32:39 -0700134 // Calculate timeout for an operation involving framesPerOperation.
135 int64_t calculateReasonableTimeout(int32_t framesPerOperation);
136
Phil Burk9dca9822017-05-26 14:27:43 -0700137 aaudio_format_t mDeviceFormat = AAUDIO_FORMAT_UNSPECIFIED;
Phil Burk87c9f642017-05-17 07:22:39 -0700138
139 IsochronousClockModel mClockModel; // timing model for chasing the HAL
140
141 AudioEndpoint mAudioEndpoint; // source for reads or sink for writes
142 aaudio_handle_t mServiceStreamHandle; // opaque handle returned from service
143
144 int32_t mFramesPerBurst; // frames per HAL transfer
145 int32_t mXRunCount = 0; // how many underrun events?
146
Phil Burk87c9f642017-05-17 07:22:39 -0700147 // Offset from underlying frame position.
148 int64_t mFramesOffsetFromService = 0; // offset for timestamps
149
150 uint8_t *mCallbackBuffer = nullptr;
151 int32_t mCallbackFrames = 0;
152
Phil Burkec89b2e2017-06-20 15:05:06 -0700153 // The service uses this for SHARED mode.
154 bool mInService = false; // Is this running in the client or the service?
155
Phil Burkb336e892017-07-05 15:35:43 -0700156 AAudioServiceInterface &mServiceInterface; // abstract interface to the service
157
Phil Burkbcc36742017-08-31 17:24:51 -0700158 SimpleDoubleBuffer<Timestamp> mAtomicTimestamp;
159
160 AtomicRequestor mNeedCatchUp; // Ask read() or write() to sync on first timestamp.
161
Phil Burk965650e2017-09-07 21:00:09 -0700162 float mStreamVolume = 1.0f;
163
Phil Burk204a1632017-01-03 17:23:43 -0800164private:
Phil Burkc0c70e32017-02-09 13:18:38 -0800165 /*
166 * Asynchronous write with data conversion.
167 * @param buffer
168 * @param numFrames
169 * @return fdrames written or negative error
170 */
171 aaudio_result_t writeNowWithConversion(const void *buffer,
172 int32_t numFrames);
Phil Burkc0c70e32017-02-09 13:18:38 -0800173
Phil Burk87c9f642017-05-17 07:22:39 -0700174 // Adjust timing model based on timestamp from service.
175 void processTimestamp(uint64_t position, int64_t time);
Phil Burk71f35bb2017-04-13 16:05:07 -0700176
Phil Burkfd34a932017-07-19 07:03:52 -0700177 // Thread on other side of FIFO will have wakeup jitter.
178 // By delaying slightly we can avoid waking up before other side is ready.
179 const int32_t mWakeupDelayNanos; // delay past typical wakeup jitter
180 const int32_t mMinimumSleepNanos; // minimum sleep while polling
181
Phil Burkc0c70e32017-02-09 13:18:38 -0800182 AudioEndpointParcelable mEndPointParcelable; // description of the buffers filled by service
183 EndpointDescriptor mEndpointDescriptor; // buffer description with resolved addresses
Phil Burk97350f92017-07-21 15:59:44 -0700184
Phil Burk97350f92017-07-21 15:59:44 -0700185 int64_t mServiceLatencyNanos = 0;
Phil Burk204a1632017-01-03 17:23:43 -0800186};
187
Phil Burk5ed503c2017-02-01 09:38:15 -0800188} /* namespace aaudio */
Phil Burk204a1632017-01-03 17:23:43 -0800189
Phil Burk5204d312017-05-04 17:16:13 -0700190#endif //ANDROID_AAUDIO_AUDIO_STREAM_INTERNAL_H