blob: 257a7023c55e196a6a5a7ceec2cb423e9b1d633f [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
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 Burke4d7bb42017-03-28 11:32:39 -070052 virtual aaudio_result_t updateStateWhileWaiting() 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 Laurenta2f296e2017-06-21 18:51:47 -070087 //PlayerBase virtuals
88 virtual void destroy();
89
Phil Burk204a1632017-01-03 17:23:43 -080090protected:
91
Phil Burk87c9f642017-05-17 07:22:39 -070092 aaudio_result_t processData(void *buffer,
93 int32_t numFrames,
94 int64_t timeoutNanoseconds);
95
96/**
97 * Low level data processing that will not block. It will just read or write as much as it can.
98 *
99 * It passed back a recommended time to wake up if wakeTimePtr is not NULL.
100 *
101 * @return the number of frames processed or a negative error code.
102 */
103 virtual aaudio_result_t processDataNow(void *buffer,
104 int32_t numFrames,
105 int64_t currentTimeNanos,
106 int64_t *wakeTimePtr) = 0;
107
Phil Burk5ed503c2017-02-01 09:38:15 -0800108 aaudio_result_t processCommands();
Phil Burk204a1632017-01-03 17:23:43 -0800109
Phil Burk71f35bb2017-04-13 16:05:07 -0700110 aaudio_result_t requestStopInternal();
Phil Burke4d7bb42017-03-28 11:32:39 -0700111
112 aaudio_result_t stopCallback();
113
Phil Burk204a1632017-01-03 17:23:43 -0800114
Phil Burkb336e892017-07-05 15:35:43 -0700115 virtual void onFlushFromServer() {}
Phil Burk204a1632017-01-03 17:23:43 -0800116
Phil Burk5ed503c2017-02-01 09:38:15 -0800117 aaudio_result_t onEventFromServer(AAudioServiceMessage *message);
Phil Burk204a1632017-01-03 17:23:43 -0800118
Phil Burk5ed503c2017-02-01 09:38:15 -0800119 aaudio_result_t onTimestampFromServer(AAudioServiceMessage *message);
Phil Burk204a1632017-01-03 17:23:43 -0800120
Phil Burkec89b2e2017-06-20 15:05:06 -0700121 void logTimestamp(AAudioServiceMessage &message);
122
Phil Burke4d7bb42017-03-28 11:32:39 -0700123 // Calculate timeout for an operation involving framesPerOperation.
124 int64_t calculateReasonableTimeout(int32_t framesPerOperation);
125
Eric Laurenta2f296e2017-06-21 18:51:47 -0700126 void doSetVolume();
127
128 //PlayerBase virtuals
129 virtual status_t playerStart();
130 virtual status_t playerPause();
131 virtual status_t playerStop();
132 virtual status_t playerSetVolume();
133
Phil Burk9dca9822017-05-26 14:27:43 -0700134 aaudio_format_t mDeviceFormat = AAUDIO_FORMAT_UNSPECIFIED;
Phil Burk87c9f642017-05-17 07:22:39 -0700135
136 IsochronousClockModel mClockModel; // timing model for chasing the HAL
137
138 AudioEndpoint mAudioEndpoint; // source for reads or sink for writes
139 aaudio_handle_t mServiceStreamHandle; // opaque handle returned from service
140
141 int32_t mFramesPerBurst; // frames per HAL transfer
142 int32_t mXRunCount = 0; // how many underrun events?
143
144 LinearRamp mVolumeRamp;
Eric Laurenta2f296e2017-06-21 18:51:47 -0700145 float mStreamVolume;
Phil Burk87c9f642017-05-17 07:22:39 -0700146
147 // 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 Burk204a1632017-01-03 17:23:43 -0800158private:
Phil Burkc0c70e32017-02-09 13:18:38 -0800159 /*
160 * Asynchronous write with data conversion.
161 * @param buffer
162 * @param numFrames
163 * @return fdrames written or negative error
164 */
165 aaudio_result_t writeNowWithConversion(const void *buffer,
166 int32_t numFrames);
Phil Burkc0c70e32017-02-09 13:18:38 -0800167
Phil Burk87c9f642017-05-17 07:22:39 -0700168 // Adjust timing model based on timestamp from service.
169 void processTimestamp(uint64_t position, int64_t time);
Phil Burk71f35bb2017-04-13 16:05:07 -0700170
Phil Burkc0c70e32017-02-09 13:18:38 -0800171 AudioEndpointParcelable mEndPointParcelable; // description of the buffers filled by service
172 EndpointDescriptor mEndpointDescriptor; // buffer description with resolved addresses
Phil Burkc0c70e32017-02-09 13:18:38 -0800173
Phil Burk204a1632017-01-03 17:23:43 -0800174};
175
Phil Burk5ed503c2017-02-01 09:38:15 -0800176} /* namespace aaudio */
Phil Burk204a1632017-01-03 17:23:43 -0800177
Phil Burk5204d312017-05-04 17:16:13 -0700178#endif //ANDROID_AAUDIO_AUDIO_STREAM_INTERNAL_H