blob: 1b991de2ab267ba2b6ea97874be14717ace68148 [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 Burke4d7bb42017-03-28 11:32:39 -070053 virtual aaudio_result_t updateStateWhileWaiting() 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 Burk5ed503c2017-02-01 09:38:15 -0800125 aaudio_result_t onTimestampFromServer(AAudioServiceMessage *message);
Phil Burk204a1632017-01-03 17:23:43 -0800126
Phil Burkec89b2e2017-06-20 15:05:06 -0700127 void logTimestamp(AAudioServiceMessage &message);
128
Phil Burke4d7bb42017-03-28 11:32:39 -0700129 // Calculate timeout for an operation involving framesPerOperation.
130 int64_t calculateReasonableTimeout(int32_t framesPerOperation);
131
Eric Laurenta2f296e2017-06-21 18:51:47 -0700132 void doSetVolume();
133
134 //PlayerBase virtuals
135 virtual status_t playerStart();
136 virtual status_t playerPause();
137 virtual status_t playerStop();
138 virtual status_t playerSetVolume();
139
Phil Burk9dca9822017-05-26 14:27:43 -0700140 aaudio_format_t mDeviceFormat = AAUDIO_FORMAT_UNSPECIFIED;
Phil Burk87c9f642017-05-17 07:22:39 -0700141
142 IsochronousClockModel mClockModel; // timing model for chasing the HAL
143
144 AudioEndpoint mAudioEndpoint; // source for reads or sink for writes
145 aaudio_handle_t mServiceStreamHandle; // opaque handle returned from service
146
147 int32_t mFramesPerBurst; // frames per HAL transfer
148 int32_t mXRunCount = 0; // how many underrun events?
149
150 LinearRamp mVolumeRamp;
Eric Laurenta2f296e2017-06-21 18:51:47 -0700151 float mStreamVolume;
Phil Burk87c9f642017-05-17 07:22:39 -0700152
153 // 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 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 Burk204a1632017-01-03 17:23:43 -0800184};
185
Phil Burk5ed503c2017-02-01 09:38:15 -0800186} /* namespace aaudio */
Phil Burk204a1632017-01-03 17:23:43 -0800187
Phil Burk5204d312017-05-04 17:16:13 -0700188#endif //ANDROID_AAUDIO_AUDIO_STREAM_INTERNAL_H