blob: c2c64190a125ccea98e5a1171bfceec0a9fa0772 [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 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.
Eric Laurenta2f296e2017-06-21 18:51:47 -070038class AudioStreamInternal : public AudioStream, public android::PlayerBase {
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
44 // =========== Begin ABSTRACT methods ===========================
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 requestPause() override;
Phil Burk204a1632017-01-03 17:23:43 -080048
Phil Burkc0c70e32017-02-09 13:18:38 -080049 aaudio_result_t requestFlush() 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 Burke4d7bb42017-03-28 11:32:39 -070057 virtual aaudio_result_t updateStateWhileWaiting() override;
Phil Burk204a1632017-01-03 17:23:43 -080058 // =========== End ABSTRACT methods ===========================
59
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 Laurenta2f296e2017-06-21 18:51:47 -070093 //PlayerBase virtuals
94 virtual void destroy();
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 Burke4d7bb42017-03-28 11:32:39 -0700116 aaudio_result_t requestPauseInternal();
Phil Burk71f35bb2017-04-13 16:05:07 -0700117 aaudio_result_t requestStopInternal();
Phil Burke4d7bb42017-03-28 11:32:39 -0700118
119 aaudio_result_t stopCallback();
120
Phil Burk204a1632017-01-03 17:23:43 -0800121
122 void onFlushFromServer();
123
Phil Burk5ed503c2017-02-01 09:38:15 -0800124 aaudio_result_t onEventFromServer(AAudioServiceMessage *message);
Phil Burk204a1632017-01-03 17:23:43 -0800125
Phil Burk5ed503c2017-02-01 09:38:15 -0800126 aaudio_result_t onTimestampFromServer(AAudioServiceMessage *message);
Phil Burk204a1632017-01-03 17:23:43 -0800127
Phil Burkec89b2e2017-06-20 15:05:06 -0700128 void logTimestamp(AAudioServiceMessage &message);
129
Phil Burke4d7bb42017-03-28 11:32:39 -0700130 // Calculate timeout for an operation involving framesPerOperation.
131 int64_t calculateReasonableTimeout(int32_t framesPerOperation);
132
Eric Laurenta2f296e2017-06-21 18:51:47 -0700133 void doSetVolume();
134
135 //PlayerBase virtuals
136 virtual status_t playerStart();
137 virtual status_t playerPause();
138 virtual status_t playerStop();
139 virtual status_t playerSetVolume();
140
Phil Burk9dca9822017-05-26 14:27:43 -0700141 aaudio_format_t mDeviceFormat = AAUDIO_FORMAT_UNSPECIFIED;
Phil Burk87c9f642017-05-17 07:22:39 -0700142
143 IsochronousClockModel mClockModel; // timing model for chasing the HAL
144
145 AudioEndpoint mAudioEndpoint; // source for reads or sink for writes
146 aaudio_handle_t mServiceStreamHandle; // opaque handle returned from service
147
148 int32_t mFramesPerBurst; // frames per HAL transfer
149 int32_t mXRunCount = 0; // how many underrun events?
150
151 LinearRamp mVolumeRamp;
Eric Laurenta2f296e2017-06-21 18:51:47 -0700152 float mStreamVolume;
Phil Burk87c9f642017-05-17 07:22:39 -0700153
154 // Offset from underlying frame position.
155 int64_t mFramesOffsetFromService = 0; // offset for timestamps
156
157 uint8_t *mCallbackBuffer = nullptr;
158 int32_t mCallbackFrames = 0;
159
Phil Burkec89b2e2017-06-20 15:05:06 -0700160 // The service uses this for SHARED mode.
161 bool mInService = false; // Is this running in the client or the service?
162
Phil Burk204a1632017-01-03 17:23:43 -0800163private:
Phil Burkc0c70e32017-02-09 13:18:38 -0800164 /*
165 * Asynchronous write with data conversion.
166 * @param buffer
167 * @param numFrames
168 * @return fdrames written or negative error
169 */
170 aaudio_result_t writeNowWithConversion(const void *buffer,
171 int32_t numFrames);
Phil Burkc0c70e32017-02-09 13:18:38 -0800172
Phil Burk87c9f642017-05-17 07:22:39 -0700173 // Adjust timing model based on timestamp from service.
174 void processTimestamp(uint64_t position, int64_t time);
Phil Burk71f35bb2017-04-13 16:05:07 -0700175
Phil Burkc0c70e32017-02-09 13:18:38 -0800176 AudioEndpointParcelable mEndPointParcelable; // description of the buffers filled by service
177 EndpointDescriptor mEndpointDescriptor; // buffer description with resolved addresses
Phil Burkc0c70e32017-02-09 13:18:38 -0800178 AAudioServiceInterface &mServiceInterface; // abstract interface to the service
179
Phil Burk204a1632017-01-03 17:23:43 -0800180};
181
Phil Burk5ed503c2017-02-01 09:38:15 -0800182} /* namespace aaudio */
Phil Burk204a1632017-01-03 17:23:43 -0800183
Phil Burk5204d312017-05-04 17:16:13 -0700184#endif //ANDROID_AAUDIO_AUDIO_STREAM_INTERNAL_H