blob: 13cf16c49f48e7e0b46829fd7ebbcae984fd8206 [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>
Eric Laurenta2f296e2017-06-21 18:51:47 -070021#include <media/PlayerBase.h>
Phil Burk5ed503c2017-02-01 09:38:15 -080022#include <aaudio/AAudio.h>
Phil Burk204a1632017-01-03 17:23:43 -080023
Phil Burk5ed503c2017-02-01 09:38:15 -080024#include "binding/IAAudioService.h"
Phil Burk204a1632017-01-03 17:23:43 -080025#include "binding/AudioEndpointParcelable.h"
Phil Burke572f462017-04-20 13:03:19 -070026#include "binding/AAudioServiceInterface.h"
Phil Burk204a1632017-01-03 17:23:43 -080027#include "client/IsochronousClockModel.h"
28#include "client/AudioEndpoint.h"
29#include "core/AudioStream.h"
Phil Burkfd34a932017-07-19 07:03:52 -070030#include "utility/AudioClock.h"
Phil Burke572f462017-04-20 13:03:19 -070031#include "utility/LinearRamp.h"
Phil Burkc0c70e32017-02-09 13:18:38 -080032
Phil Burk204a1632017-01-03 17:23:43 -080033using android::sp;
Phil Burk5ed503c2017-02-01 09:38:15 -080034using android::IAAudioService;
Phil Burk204a1632017-01-03 17:23:43 -080035
Phil Burk5ed503c2017-02-01 09:38:15 -080036namespace aaudio {
Phil Burk204a1632017-01-03 17:23:43 -080037
Phil Burk5ed503c2017-02-01 09:38:15 -080038// A stream that talks to the AAudioService or directly to a HAL.
Eric Laurenta2f296e2017-06-21 18:51:47 -070039class AudioStreamInternal : public AudioStream, public android::PlayerBase {
Phil Burk204a1632017-01-03 17:23:43 -080040
41public:
Phil Burk87c9f642017-05-17 07:22:39 -070042 AudioStreamInternal(AAudioServiceInterface &serviceInterface, bool inService);
Phil Burk204a1632017-01-03 17:23:43 -080043 virtual ~AudioStreamInternal();
44
Phil Burkc0c70e32017-02-09 13:18:38 -080045 aaudio_result_t requestStart() override;
Phil Burk204a1632017-01-03 17:23:43 -080046
Phil Burkc0c70e32017-02-09 13:18:38 -080047 aaudio_result_t requestStop() override;
Phil Burk204a1632017-01-03 17:23:43 -080048
Phil Burkc0c70e32017-02-09 13:18:38 -080049 aaudio_result_t getTimestamp(clockid_t clockId,
Phil Burk3316d5e2017-02-15 11:23:01 -080050 int64_t *framePosition,
51 int64_t *timeNanoseconds) override;
Phil Burk204a1632017-01-03 17:23:43 -080052
Phil Burk0befec62017-07-28 15:12:13 -070053 virtual aaudio_result_t updateStateMachine() override;
Phil Burk204a1632017-01-03 17:23:43 -080054
Phil Burkc0c70e32017-02-09 13:18:38 -080055 aaudio_result_t open(const AudioStreamBuilder &builder) override;
Phil Burk204a1632017-01-03 17:23:43 -080056
Phil Burkc0c70e32017-02-09 13:18:38 -080057 aaudio_result_t close() override;
Phil Burk204a1632017-01-03 17:23:43 -080058
Phil Burkc0c70e32017-02-09 13:18:38 -080059 aaudio_result_t setBufferSize(int32_t requestedFrames) override;
Phil Burk204a1632017-01-03 17:23:43 -080060
Phil Burkc0c70e32017-02-09 13:18:38 -080061 int32_t getBufferSize() const override;
Phil Burk204a1632017-01-03 17:23:43 -080062
Phil Burkc0c70e32017-02-09 13:18:38 -080063 int32_t getBufferCapacity() const override;
Phil Burk204a1632017-01-03 17:23:43 -080064
Phil Burkc0c70e32017-02-09 13:18:38 -080065 int32_t getFramesPerBurst() const override;
Phil Burk204a1632017-01-03 17:23:43 -080066
Phil Burkc0c70e32017-02-09 13:18:38 -080067 int32_t getXRunCount() const override {
Phil Burk204a1632017-01-03 17:23:43 -080068 return mXRunCount;
69 }
70
Phil Burkc0c70e32017-02-09 13:18:38 -080071 aaudio_result_t registerThread() override;
Phil Burk204a1632017-01-03 17:23:43 -080072
Phil Burkc0c70e32017-02-09 13:18:38 -080073 aaudio_result_t unregisterThread() override;
Phil Burk204a1632017-01-03 17:23:43 -080074
Phil Burk87c9f642017-05-17 07:22:39 -070075 aaudio_result_t joinThread(void** returnArg);
76
Phil Burke4d7bb42017-03-28 11:32:39 -070077 // Called internally from 'C'
Phil Burk87c9f642017-05-17 07:22:39 -070078 virtual void *callbackLoop() = 0;
Phil Burke4d7bb42017-03-28 11:32:39 -070079
Phil Burke2fbb592017-05-01 15:05:52 -070080
81 bool isMMap() override {
82 return true;
83 }
84
Phil Burk87c9f642017-05-17 07:22:39 -070085 // Calculate timeout based on framesPerBurst
86 int64_t calculateReasonableTimeout();
87
Eric Laurenta2f296e2017-06-21 18:51:47 -070088 //PlayerBase virtuals
89 virtual void destroy();
90
Eric Laurentcb4dae22017-07-01 19:39:32 -070091 aaudio_result_t startClient(const android::AudioClient& client,
92 audio_port_handle_t *clientHandle);
93
94 aaudio_result_t stopClient(audio_port_handle_t clientHandle);
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 Burk5ed503c2017-02-01 09:38:15 -0800114 aaudio_result_t processCommands();
Phil Burk204a1632017-01-03 17:23:43 -0800115
Phil Burk71f35bb2017-04-13 16:05:07 -0700116 aaudio_result_t requestStopInternal();
Phil Burke4d7bb42017-03-28 11:32:39 -0700117
118 aaudio_result_t stopCallback();
119
Phil Burk204a1632017-01-03 17:23:43 -0800120
Phil Burkb336e892017-07-05 15:35:43 -0700121 virtual void onFlushFromServer() {}
Phil Burk204a1632017-01-03 17:23:43 -0800122
Phil Burk5ed503c2017-02-01 09:38:15 -0800123 aaudio_result_t onEventFromServer(AAudioServiceMessage *message);
Phil Burk204a1632017-01-03 17:23:43 -0800124
Phil Burk97350f92017-07-21 15:59:44 -0700125 aaudio_result_t onTimestampService(AAudioServiceMessage *message);
126
127 aaudio_result_t onTimestampHardware(AAudioServiceMessage *message);
Phil Burk204a1632017-01-03 17:23:43 -0800128
Phil Burkec89b2e2017-06-20 15:05:06 -0700129 void logTimestamp(AAudioServiceMessage &message);
130
Phil Burke4d7bb42017-03-28 11:32:39 -0700131 // Calculate timeout for an operation involving framesPerOperation.
132 int64_t calculateReasonableTimeout(int32_t framesPerOperation);
133
Eric Laurenta2f296e2017-06-21 18:51:47 -0700134 void doSetVolume();
135
136 //PlayerBase virtuals
137 virtual status_t playerStart();
138 virtual status_t playerPause();
139 virtual status_t playerStop();
140 virtual status_t playerSetVolume();
141
Phil Burk9dca9822017-05-26 14:27:43 -0700142 aaudio_format_t mDeviceFormat = AAUDIO_FORMAT_UNSPECIFIED;
Phil Burk87c9f642017-05-17 07:22:39 -0700143
144 IsochronousClockModel mClockModel; // timing model for chasing the HAL
145
146 AudioEndpoint mAudioEndpoint; // source for reads or sink for writes
147 aaudio_handle_t mServiceStreamHandle; // opaque handle returned from service
148
149 int32_t mFramesPerBurst; // frames per HAL transfer
150 int32_t mXRunCount = 0; // how many underrun events?
151
152 LinearRamp mVolumeRamp;
Eric Laurenta2f296e2017-06-21 18:51:47 -0700153 float mStreamVolume;
Phil Burk87c9f642017-05-17 07:22:39 -0700154
155 // 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 Burk204a1632017-01-03 17:23:43 -0800166private:
Phil Burkc0c70e32017-02-09 13:18:38 -0800167 /*
168 * Asynchronous write with data conversion.
169 * @param buffer
170 * @param numFrames
171 * @return fdrames written or negative error
172 */
173 aaudio_result_t writeNowWithConversion(const void *buffer,
174 int32_t numFrames);
Phil Burkc0c70e32017-02-09 13:18:38 -0800175
Phil Burk87c9f642017-05-17 07:22:39 -0700176 // Adjust timing model based on timestamp from service.
177 void processTimestamp(uint64_t position, int64_t time);
Phil Burk71f35bb2017-04-13 16:05:07 -0700178
Phil Burkfd34a932017-07-19 07:03:52 -0700179 // Thread on other side of FIFO will have wakeup jitter.
180 // By delaying slightly we can avoid waking up before other side is ready.
181 const int32_t mWakeupDelayNanos; // delay past typical wakeup jitter
182 const int32_t mMinimumSleepNanos; // minimum sleep while polling
183
Phil Burkc0c70e32017-02-09 13:18:38 -0800184 AudioEndpointParcelable mEndPointParcelable; // description of the buffers filled by service
185 EndpointDescriptor mEndpointDescriptor; // buffer description with resolved addresses
Phil Burk97350f92017-07-21 15:59:44 -0700186
187 SimpleDoubleBuffer<Timestamp> mAtomicTimestamp;
188
189 int64_t mServiceLatencyNanos = 0;
190
Phil Burk204a1632017-01-03 17:23:43 -0800191};
192
Phil Burk5ed503c2017-02-01 09:38:15 -0800193} /* namespace aaudio */
Phil Burk204a1632017-01-03 17:23:43 -0800194
Phil Burk5204d312017-05-04 17:16:13 -0700195#endif //ANDROID_AAUDIO_AUDIO_STREAM_INTERNAL_H