blob: 377f9c7d0332108cf57e41c99985134c9cacf9f6 [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 Burke572f462017-04-20 13:03:19 -070029#include "utility/LinearRamp.h"
Phil Burkc0c70e32017-02-09 13:18:38 -080030
Phil Burk204a1632017-01-03 17:23:43 -080031using android::sp;
Phil Burk5ed503c2017-02-01 09:38:15 -080032using android::IAAudioService;
Phil Burk204a1632017-01-03 17:23:43 -080033
Phil Burk5ed503c2017-02-01 09:38:15 -080034namespace aaudio {
Phil Burk204a1632017-01-03 17:23:43 -080035
Phil Burk5ed503c2017-02-01 09:38:15 -080036// A stream that talks to the AAudioService or directly to a HAL.
Phil Burk204a1632017-01-03 17:23:43 -080037class AudioStreamInternal : public AudioStream {
38
39public:
Phil Burk87c9f642017-05-17 07:22:39 -070040 AudioStreamInternal(AAudioServiceInterface &serviceInterface, bool inService);
Phil Burk204a1632017-01-03 17:23:43 -080041 virtual ~AudioStreamInternal();
42
43 // =========== Begin ABSTRACT methods ===========================
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 requestPause() override;
Phil Burk204a1632017-01-03 17:23:43 -080047
Phil Burkc0c70e32017-02-09 13:18:38 -080048 aaudio_result_t requestFlush() override;
Phil Burk204a1632017-01-03 17:23:43 -080049
Phil Burkc0c70e32017-02-09 13:18:38 -080050 aaudio_result_t requestStop() override;
Phil Burk204a1632017-01-03 17:23:43 -080051
Phil Burkc0c70e32017-02-09 13:18:38 -080052 aaudio_result_t getTimestamp(clockid_t clockId,
Phil Burk3316d5e2017-02-15 11:23:01 -080053 int64_t *framePosition,
54 int64_t *timeNanoseconds) override;
Phil Burk204a1632017-01-03 17:23:43 -080055
Phil Burke4d7bb42017-03-28 11:32:39 -070056 virtual aaudio_result_t updateStateWhileWaiting() override;
Phil Burk204a1632017-01-03 17:23:43 -080057 // =========== End ABSTRACT methods ===========================
58
Phil Burkc0c70e32017-02-09 13:18:38 -080059 aaudio_result_t open(const AudioStreamBuilder &builder) override;
Phil Burk204a1632017-01-03 17:23:43 -080060
Phil Burkc0c70e32017-02-09 13:18:38 -080061 aaudio_result_t close() override;
Phil Burk204a1632017-01-03 17:23:43 -080062
Phil Burkc0c70e32017-02-09 13:18:38 -080063 aaudio_result_t setBufferSize(int32_t requestedFrames) override;
Phil Burk204a1632017-01-03 17:23:43 -080064
Phil Burkc0c70e32017-02-09 13:18:38 -080065 int32_t getBufferSize() const override;
Phil Burk204a1632017-01-03 17:23:43 -080066
Phil Burkc0c70e32017-02-09 13:18:38 -080067 int32_t getBufferCapacity() const override;
Phil Burk204a1632017-01-03 17:23:43 -080068
Phil Burkc0c70e32017-02-09 13:18:38 -080069 int32_t getFramesPerBurst() const override;
Phil Burk204a1632017-01-03 17:23:43 -080070
Phil Burkc0c70e32017-02-09 13:18:38 -080071 int32_t getXRunCount() const override {
Phil Burk204a1632017-01-03 17:23:43 -080072 return mXRunCount;
73 }
74
Phil Burkc0c70e32017-02-09 13:18:38 -080075 aaudio_result_t registerThread() override;
Phil Burk204a1632017-01-03 17:23:43 -080076
Phil Burkc0c70e32017-02-09 13:18:38 -080077 aaudio_result_t unregisterThread() override;
Phil Burk204a1632017-01-03 17:23:43 -080078
Phil Burk87c9f642017-05-17 07:22:39 -070079 aaudio_result_t joinThread(void** returnArg);
80
Phil Burke4d7bb42017-03-28 11:32:39 -070081 // Called internally from 'C'
Phil Burk87c9f642017-05-17 07:22:39 -070082 virtual void *callbackLoop() = 0;
Phil Burke4d7bb42017-03-28 11:32:39 -070083
Phil Burke2fbb592017-05-01 15:05:52 -070084
85 bool isMMap() override {
86 return true;
87 }
88
Phil Burk87c9f642017-05-17 07:22:39 -070089 // Calculate timeout based on framesPerBurst
90 int64_t calculateReasonableTimeout();
91
Phil Burk204a1632017-01-03 17:23:43 -080092protected:
93
Phil Burk87c9f642017-05-17 07:22:39 -070094 aaudio_result_t processData(void *buffer,
95 int32_t numFrames,
96 int64_t timeoutNanoseconds);
97
98/**
99 * Low level data processing that will not block. It will just read or write as much as it can.
100 *
101 * It passed back a recommended time to wake up if wakeTimePtr is not NULL.
102 *
103 * @return the number of frames processed or a negative error code.
104 */
105 virtual aaudio_result_t processDataNow(void *buffer,
106 int32_t numFrames,
107 int64_t currentTimeNanos,
108 int64_t *wakeTimePtr) = 0;
109
Phil Burk5ed503c2017-02-01 09:38:15 -0800110 aaudio_result_t processCommands();
Phil Burk204a1632017-01-03 17:23:43 -0800111
Phil Burke4d7bb42017-03-28 11:32:39 -0700112 aaudio_result_t requestPauseInternal();
Phil Burk71f35bb2017-04-13 16:05:07 -0700113 aaudio_result_t requestStopInternal();
Phil Burke4d7bb42017-03-28 11:32:39 -0700114
115 aaudio_result_t stopCallback();
116
Phil Burk204a1632017-01-03 17:23:43 -0800117
118 void onFlushFromServer();
119
Phil Burk5ed503c2017-02-01 09:38:15 -0800120 aaudio_result_t onEventFromServer(AAudioServiceMessage *message);
Phil Burk204a1632017-01-03 17:23:43 -0800121
Phil Burk5ed503c2017-02-01 09:38:15 -0800122 aaudio_result_t onTimestampFromServer(AAudioServiceMessage *message);
Phil Burk204a1632017-01-03 17:23:43 -0800123
Phil Burke4d7bb42017-03-28 11:32:39 -0700124 // Calculate timeout for an operation involving framesPerOperation.
125 int64_t calculateReasonableTimeout(int32_t framesPerOperation);
126
Phil Burk87c9f642017-05-17 07:22:39 -0700127 aaudio_audio_format_t mDeviceFormat = AAUDIO_FORMAT_UNSPECIFIED;
128
129 IsochronousClockModel mClockModel; // timing model for chasing the HAL
130
131 AudioEndpoint mAudioEndpoint; // source for reads or sink for writes
132 aaudio_handle_t mServiceStreamHandle; // opaque handle returned from service
133
134 int32_t mFramesPerBurst; // frames per HAL transfer
135 int32_t mXRunCount = 0; // how many underrun events?
136
137 LinearRamp mVolumeRamp;
138
139 // Offset from underlying frame position.
140 int64_t mFramesOffsetFromService = 0; // offset for timestamps
141
142 uint8_t *mCallbackBuffer = nullptr;
143 int32_t mCallbackFrames = 0;
144
Phil Burk204a1632017-01-03 17:23:43 -0800145private:
Phil Burkc0c70e32017-02-09 13:18:38 -0800146 /*
147 * Asynchronous write with data conversion.
148 * @param buffer
149 * @param numFrames
150 * @return fdrames written or negative error
151 */
152 aaudio_result_t writeNowWithConversion(const void *buffer,
153 int32_t numFrames);
Phil Burkc0c70e32017-02-09 13:18:38 -0800154
Phil Burk87c9f642017-05-17 07:22:39 -0700155 // Adjust timing model based on timestamp from service.
156 void processTimestamp(uint64_t position, int64_t time);
Phil Burk71f35bb2017-04-13 16:05:07 -0700157
158 const char *getLocationName() const {
159 return mInService ? "SERVICE" : "CLIENT";
160 }
161
Phil Burkc0c70e32017-02-09 13:18:38 -0800162 AudioEndpointParcelable mEndPointParcelable; // description of the buffers filled by service
163 EndpointDescriptor mEndpointDescriptor; // buffer description with resolved addresses
Phil Burkc0c70e32017-02-09 13:18:38 -0800164 AAudioServiceInterface &mServiceInterface; // abstract interface to the service
165
166 // The service uses this for SHARED mode.
Phil Burk87c9f642017-05-17 07:22:39 -0700167 bool mInService = false; // Is this running in the client or the service?
Phil Burk204a1632017-01-03 17:23:43 -0800168};
169
Phil Burk5ed503c2017-02-01 09:38:15 -0800170} /* namespace aaudio */
Phil Burk204a1632017-01-03 17:23:43 -0800171
Phil Burk5204d312017-05-04 17:16:13 -0700172#endif //ANDROID_AAUDIO_AUDIO_STREAM_INTERNAL_H