1 /*
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 
17 #ifndef ANDROID_AAUDIO_AUDIO_STREAM_INTERNAL_H
18 #define ANDROID_AAUDIO_AUDIO_STREAM_INTERNAL_H
19 
20 #include <stdint.h>
21 #include <aaudio/AAudio.h>
22 
23 #include "binding/AudioEndpointParcelable.h"
24 #include "binding/AAudioServiceInterface.h"
25 #include "client/AAudioFlowGraph.h"
26 #include "client/AudioEndpoint.h"
27 #include "client/IsochronousClockModel.h"
28 #include "core/AudioStream.h"
29 #include "utility/AudioClock.h"
30 
31 using android::sp;
32 
33 namespace aaudio {
34 
35     // These are intended to be outside the range of what is normally encountered.
36     // TODO MAXes should probably be much bigger.
37     constexpr int32_t MIN_FRAMES_PER_BURST = 16; // arbitrary
38     constexpr int32_t MAX_FRAMES_PER_BURST = 16 * 1024;  // arbitrary
39     constexpr int32_t MAX_BUFFER_CAPACITY_IN_FRAMES = 32 * 1024;  // arbitrary
40 
41 // A stream that talks to the AAudioService or directly to a HAL.
42 class AudioStreamInternal : public AudioStream {
43 
44 public:
45     AudioStreamInternal(AAudioServiceInterface  &serviceInterface, bool inService);
46     virtual ~AudioStreamInternal();
47 
48     aaudio_result_t getTimestamp(clockid_t clockId,
49                                        int64_t *framePosition,
50                                        int64_t *timeNanoseconds) override;
51 
52     virtual aaudio_result_t processCommands() override;
53 
54     aaudio_result_t open(const AudioStreamBuilder &builder) override;
55 
56     aaudio_result_t setBufferSize(int32_t requestedFrames) override;
57 
58     int32_t getBufferSize() const override;
59 
60     int32_t getDeviceBufferSize() const;
61 
62     int32_t getBufferCapacity() const override;
63 
64     int32_t getDeviceBufferCapacity() const override;
65 
getXRunCount()66     int32_t getXRunCount() const override {
67         return mXRunCount;
68     }
69 
70     aaudio_result_t registerThread() override;
71 
72     aaudio_result_t unregisterThread() override;
73 
74     // Called internally from 'C'
75     virtual void *callbackLoop() = 0;
76 
isMMap()77     bool isMMap() override {
78         return true;
79     }
80 
81     // Calculate timeout based on framesPerBurst
82     int64_t calculateReasonableTimeout();
83 
84     aaudio_result_t startClient(const android::AudioClient& client,
85                                 const audio_attributes_t *attr,
86                                 audio_port_handle_t *clientHandle);
87 
88     aaudio_result_t stopClient(audio_port_handle_t clientHandle);
89 
getServiceHandle()90     aaudio_handle_t getServiceHandle() const {
91         return mServiceStreamHandleInfo.getHandle();
92     }
93 
getServiceLifetimeId()94     int32_t getServiceLifetimeId() const {
95         return mServiceStreamHandleInfo.getServiceLifetimeId();
96     }
97 
98 protected:
99     aaudio_result_t requestStart_l() REQUIRES(mStreamLock) override;
100     aaudio_result_t requestStop_l() REQUIRES(mStreamLock) override;
101 
102     aaudio_result_t release_l() REQUIRES(mStreamLock) override;
103 
104     aaudio_result_t processData(void *buffer,
105                          int32_t numFrames,
106                          int64_t timeoutNanoseconds);
107 
108 /**
109  * Low level data processing that will not block. It will just read or write as much as it can.
110  *
111  * It passed back a recommended time to wake up if wakeTimePtr is not NULL.
112  *
113  * @return the number of frames processed or a negative error code.
114  */
115     virtual aaudio_result_t processDataNow(void *buffer,
116                             int32_t numFrames,
117                             int64_t currentTimeNanos,
118                             int64_t *wakeTimePtr) = 0;
119 
120     aaudio_result_t drainTimestampsFromService();
121 
122     aaudio_result_t stopCallback_l();
123 
prepareBuffersForStart()124     virtual void prepareBuffersForStart() {}
125 
prepareBuffersForStop()126     virtual void prepareBuffersForStop() {}
127 
128     virtual void advanceClientToMatchServerPosition(int32_t serverMargin) = 0;
129 
onFlushFromServer()130     virtual void onFlushFromServer() {}
131 
132     aaudio_result_t onEventFromServer(AAudioServiceMessage *message);
133 
134     aaudio_result_t onTimestampService(AAudioServiceMessage *message);
135 
136     aaudio_result_t onTimestampHardware(AAudioServiceMessage *message);
137 
138     void logTimestamp(AAudioServiceMessage &message);
139 
140     // Calculate timeout for an operation involving framesPerOperation.
141     int64_t calculateReasonableTimeout(int32_t framesPerOperation);
142 
143     /**
144      * @return true if running in audio service, versus in app process
145      */
isInService()146     bool isInService() const { return mInService; }
147 
148     /**
149      * Is the service FIFO position currently controlled by the AAudio service or HAL,
150      * or set based on the Clock Model.
151      *
152      * @return true if the ClockModel is currently determining the FIFO position
153      */
154     bool isClockModelInControl() const;
155 
156     IsochronousClockModel    mClockModel;      // timing model for chasing the HAL
157 
158     std::unique_ptr<AudioEndpoint> mAudioEndpoint;   // source for reads or sink for writes
159 
160     // opaque handle returned from service
161     AAudioHandleInfo         mServiceStreamHandleInfo;
162 
163     int32_t                  mXRunCount = 0;      // how many underrun events?
164 
165     // Offset from underlying frame position.
166     int64_t                  mFramesOffsetFromService = 0; // offset for timestamps
167 
168     std::unique_ptr<uint8_t[]> mCallbackBuffer;
169     int32_t                  mCallbackFrames = 0;
170 
171     // The service uses this for SHARED mode.
172     bool                     mInService = false;  // Is this running in the client or the service?
173 
174     AAudioServiceInterface  &mServiceInterface;   // abstract interface to the service
175 
176     SimpleDoubleBuffer<Timestamp>  mAtomicInternalTimestamp;
177 
178     AtomicRequestor          mNeedCatchUp;   // Ask read() or write() to sync on first timestamp.
179 
180     float                    mStreamVolume = 1.0f;
181 
182     int64_t                  mLastFramesWritten = 0;
183     int64_t                  mLastFramesRead = 0;
184 
185     AAudioFlowGraph          mFlowGraph;
186 
187 private:
188     /*
189      * Asynchronous write with data conversion.
190      * @param buffer
191      * @param numFrames
192      * @return fdrames written or negative error
193      */
194     aaudio_result_t writeNowWithConversion(const void *buffer,
195                                      int32_t numFrames);
196 
197     // Exit the stream from standby, will reconstruct data path.
198     aaudio_result_t exitStandby_l() REQUIRES(mStreamLock);
199 
200     // Adjust timing model based on timestamp from service.
201     void processTimestamp(uint64_t position, int64_t time);
202 
203     aaudio_result_t configureDataInformation(int32_t callbackFrames);
204 
205     // Thread on other side of FIFO will have wakeup jitter.
206     // By delaying slightly we can avoid waking up before other side is ready.
207     const int32_t            mWakeupDelayNanos; // delay past typical wakeup jitter
208     const int32_t            mMinimumSleepNanos; // minimum sleep while polling
209     int32_t                  mTimeOffsetNanos = 0; // add to time part of an MMAP timestamp
210 
211     AudioEndpointParcelable  mEndPointParcelable; // description of the buffers filled by service
212     EndpointDescriptor       mEndpointDescriptor; // buffer description with resolved addresses
213 
214     int64_t                  mServiceLatencyNanos = 0;
215 
216     int32_t                  mBufferSizeInFrames = 0; // local threshold to control latency
217     int32_t                  mDeviceBufferSizeInFrames = 0;
218     int32_t                  mBufferCapacityInFrames = 0;
219     int32_t                  mDeviceBufferCapacityInFrames = 0;
220 
221 };
222 
223 } /* namespace aaudio */
224 
225 #endif //ANDROID_AAUDIO_AUDIO_STREAM_INTERNAL_H
226