blob: a43f6c5949013760e88f0dd076a5e64767004cd7 [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 Burk5ed503c2017-02-01 09:38:15 -080017#ifndef AAUDIO_AUDIOSTREAMINTERNAL_H
18#define AAUDIO_AUDIOSTREAMINTERNAL_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 Burkc0c70e32017-02-09 13:18:38 -080040 AudioStreamInternal(AAudioServiceInterface &serviceInterface, bool inService = false);
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 Burk5ed503c2017-02-01 09:38:15 -080052 // TODO use aaudio_clockid_t all the way down to AudioClock
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
57
Phil Burkc0c70e32017-02-09 13:18:38 -080058
Phil Burke4d7bb42017-03-28 11:32:39 -070059 virtual aaudio_result_t updateStateWhileWaiting() override;
Phil Burkc0c70e32017-02-09 13:18:38 -080060
Phil Burk204a1632017-01-03 17:23:43 -080061 // =========== End ABSTRACT methods ===========================
62
Phil Burkc0c70e32017-02-09 13:18:38 -080063 aaudio_result_t open(const AudioStreamBuilder &builder) override;
Phil Burk204a1632017-01-03 17:23:43 -080064
Phil Burkc0c70e32017-02-09 13:18:38 -080065 aaudio_result_t close() override;
Phil Burk204a1632017-01-03 17:23:43 -080066
Phil Burkc0c70e32017-02-09 13:18:38 -080067 aaudio_result_t write(const void *buffer,
Phil Burk204a1632017-01-03 17:23:43 -080068 int32_t numFrames,
Phil Burk3316d5e2017-02-15 11:23:01 -080069 int64_t timeoutNanoseconds) override;
Phil Burk204a1632017-01-03 17:23:43 -080070
Phil Burkc0c70e32017-02-09 13:18:38 -080071 aaudio_result_t setBufferSize(int32_t requestedFrames) override;
Phil Burk204a1632017-01-03 17:23:43 -080072
Phil Burkc0c70e32017-02-09 13:18:38 -080073 int32_t getBufferSize() const override;
Phil Burk204a1632017-01-03 17:23:43 -080074
Phil Burkc0c70e32017-02-09 13:18:38 -080075 int32_t getBufferCapacity() const override;
Phil Burk204a1632017-01-03 17:23:43 -080076
Phil Burkc0c70e32017-02-09 13:18:38 -080077 int32_t getFramesPerBurst() const override;
Phil Burk204a1632017-01-03 17:23:43 -080078
Phil Burkc0c70e32017-02-09 13:18:38 -080079 int64_t getFramesRead() override;
Phil Burk4c5129b2017-04-28 15:17:32 -070080 int64_t getFramesWritten() override;
Phil Burk204a1632017-01-03 17:23:43 -080081
Phil Burkc0c70e32017-02-09 13:18:38 -080082 int32_t getXRunCount() const override {
Phil Burk204a1632017-01-03 17:23:43 -080083 return mXRunCount;
84 }
85
Phil Burkc0c70e32017-02-09 13:18:38 -080086 aaudio_result_t registerThread() override;
Phil Burk204a1632017-01-03 17:23:43 -080087
Phil Burkc0c70e32017-02-09 13:18:38 -080088 aaudio_result_t unregisterThread() override;
Phil Burk204a1632017-01-03 17:23:43 -080089
Phil Burke4d7bb42017-03-28 11:32:39 -070090 // Called internally from 'C'
91 void *callbackLoop();
92
Phil Burke2fbb592017-05-01 15:05:52 -070093
94 bool isMMap() override {
95 return true;
96 }
97
Phil Burk204a1632017-01-03 17:23:43 -080098protected:
99
Phil Burk5ed503c2017-02-01 09:38:15 -0800100 aaudio_result_t processCommands();
Phil Burk204a1632017-01-03 17:23:43 -0800101
Phil Burke4d7bb42017-03-28 11:32:39 -0700102 aaudio_result_t requestPauseInternal();
Phil Burk71f35bb2017-04-13 16:05:07 -0700103 aaudio_result_t requestStopInternal();
Phil Burke4d7bb42017-03-28 11:32:39 -0700104
105 aaudio_result_t stopCallback();
106
Phil Burk204a1632017-01-03 17:23:43 -0800107/**
108 * Low level write that will not block. It will just write as much as it can.
109 *
110 * It passed back a recommended time to wake up if wakeTimePtr is not NULL.
111 *
112 * @return the number of frames written or a negative error code.
113 */
Phil Burkc0c70e32017-02-09 13:18:38 -0800114 aaudio_result_t writeNow(const void *buffer,
115 int32_t numFrames,
116 int64_t currentTimeNanos,
117 int64_t *wakeTimePtr);
Phil Burk204a1632017-01-03 17:23:43 -0800118
119 void onFlushFromServer();
120
Phil Burk5ed503c2017-02-01 09:38:15 -0800121 aaudio_result_t onEventFromServer(AAudioServiceMessage *message);
Phil Burk204a1632017-01-03 17:23:43 -0800122
Phil Burk5ed503c2017-02-01 09:38:15 -0800123 aaudio_result_t onTimestampFromServer(AAudioServiceMessage *message);
Phil Burk204a1632017-01-03 17:23:43 -0800124
Phil Burke4d7bb42017-03-28 11:32:39 -0700125 // Calculate timeout for an operation involving framesPerOperation.
126 int64_t calculateReasonableTimeout(int32_t framesPerOperation);
127
Phil Burk204a1632017-01-03 17:23:43 -0800128private:
Phil Burkc0c70e32017-02-09 13:18:38 -0800129 /*
130 * Asynchronous write with data conversion.
131 * @param buffer
132 * @param numFrames
133 * @return fdrames written or negative error
134 */
135 aaudio_result_t writeNowWithConversion(const void *buffer,
136 int32_t numFrames);
137 void processTimestamp(uint64_t position, int64_t time);
138
Phil Burk71f35bb2017-04-13 16:05:07 -0700139
140 const char *getLocationName() const {
141 return mInService ? "SERVICE" : "CLIENT";
142 }
143
Phil Burkc0c70e32017-02-09 13:18:38 -0800144 // Adjust timing model based on timestamp from service.
145
146 IsochronousClockModel mClockModel; // timing model for chasing the HAL
147 AudioEndpoint mAudioEndpoint; // sink for writes
148 aaudio_handle_t mServiceStreamHandle; // opaque handle returned from service
149
150 AudioEndpointParcelable mEndPointParcelable; // description of the buffers filled by service
151 EndpointDescriptor mEndpointDescriptor; // buffer description with resolved addresses
152
153 aaudio_audio_format_t mDeviceFormat = AAUDIO_FORMAT_UNSPECIFIED;
154
Phil Burke4d7bb42017-03-28 11:32:39 -0700155 uint8_t *mCallbackBuffer = nullptr;
156 int32_t mCallbackFrames = 0;
157
Phil Burk204a1632017-01-03 17:23:43 -0800158 // Offset from underlying frame position.
Phil Burkc0c70e32017-02-09 13:18:38 -0800159 int64_t mFramesOffsetFromService = 0; // offset for timestamps
160 int64_t mLastFramesRead = 0; // used to prevent retrograde motion
161 int32_t mFramesPerBurst; // frames per HAL transfer
162 int32_t mXRunCount = 0; // how many underrun events?
Phil Burke572f462017-04-20 13:03:19 -0700163 LinearRamp mVolumeRamp;
Phil Burkc0c70e32017-02-09 13:18:38 -0800164
165 AAudioServiceInterface &mServiceInterface; // abstract interface to the service
166
167 // The service uses this for SHARED mode.
168 bool mInService = false; // Are running in the client or the service?
Phil Burk204a1632017-01-03 17:23:43 -0800169};
170
Phil Burk5ed503c2017-02-01 09:38:15 -0800171} /* namespace aaudio */
Phil Burk204a1632017-01-03 17:23:43 -0800172
Phil Burk5ed503c2017-02-01 09:38:15 -0800173#endif //AAUDIO_AUDIOSTREAMINTERNAL_H