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 #define LOG_TAG "AudioStreamInternal"
18 //#define LOG_NDEBUG 0
19 #include <utils/Log.h>
20 
21 #define ATRACE_TAG ATRACE_TAG_AUDIO
22 
23 #include <stdint.h>
24 
25 #include <binder/IServiceManager.h>
26 
27 #include <aaudio/AAudio.h>
28 #include <cutils/properties.h>
29 
30 #include <media/AudioParameter.h>
31 #include <media/AudioSystem.h>
32 #include <media/MediaMetricsItem.h>
33 #include <utils/Trace.h>
34 
35 #include "AudioEndpointParcelable.h"
36 #include "binding/AAudioBinderClient.h"
37 #include "binding/AAudioStreamRequest.h"
38 #include "binding/AAudioStreamConfiguration.h"
39 #include "binding/AAudioServiceMessage.h"
40 #include "core/AudioGlobal.h"
41 #include "core/AudioStreamBuilder.h"
42 #include "fifo/FifoBuffer.h"
43 #include "utility/AudioClock.h"
44 #include <media/AidlConversion.h>
45 #include <com_android_media_aaudio.h>
46 
47 #include "AudioStreamInternal.h"
48 
49 // We do this after the #includes because if a header uses ALOG.
50 // it would fail on the reference to mInService.
51 #undef LOG_TAG
52 // This file is used in both client and server processes.
53 // This is needed to make sense of the logs more easily.
54 #define LOG_TAG (mInService ? "AudioStreamInternal_Service" : "AudioStreamInternal_Client")
55 
56 using android::content::AttributionSourceState;
57 
58 using namespace aaudio;
59 
60 #define MIN_TIMEOUT_NANOS        (1000 * AAUDIO_NANOS_PER_MILLISECOND)
61 
62 // Wait at least this many times longer than the operation should take.
63 #define MIN_TIMEOUT_OPERATIONS    4
64 
65 #define LOG_TIMESTAMPS            0
66 
67 // Minimum number of bursts to use when sample rate conversion is used.
68 #define MIN_SAMPLE_RATE_CONVERSION_NUM_BURSTS    3
69 
AudioStreamInternal(AAudioServiceInterface & serviceInterface,bool inService)70 AudioStreamInternal::AudioStreamInternal(AAudioServiceInterface  &serviceInterface, bool inService)
71         : AudioStream()
72         , mClockModel()
73         , mInService(inService)
74         , mServiceInterface(serviceInterface)
75         , mAtomicInternalTimestamp()
76         , mWakeupDelayNanos(AAudioProperty_getWakeupDelayMicros() * AAUDIO_NANOS_PER_MICROSECOND)
77         , mMinimumSleepNanos(AAudioProperty_getMinimumSleepMicros() * AAUDIO_NANOS_PER_MICROSECOND)
78         {
79 
80 }
81 
~AudioStreamInternal()82 AudioStreamInternal::~AudioStreamInternal() {
83     ALOGD("%s() %p called", __func__, this);
84 }
85 
open(const AudioStreamBuilder & builder)86 aaudio_result_t AudioStreamInternal::open(const AudioStreamBuilder &builder) {
87 
88     aaudio_result_t result = AAUDIO_OK;
89     AAudioStreamRequest request;
90     AAudioStreamConfiguration configurationOutput;
91 
92     if (getState() != AAUDIO_STREAM_STATE_UNINITIALIZED) {
93         ALOGE("%s - already open! state = %d", __func__, getState());
94         return AAUDIO_ERROR_INVALID_STATE;
95     }
96 
97     // Copy requested parameters to the stream.
98     result = AudioStream::open(builder);
99     if (result < 0) {
100         return result;
101     }
102 
103     const audio_format_t requestedFormat = getFormat();
104     // We have to do volume scaling. So we prefer FLOAT format.
105     if (requestedFormat == AUDIO_FORMAT_DEFAULT) {
106         setFormat(AUDIO_FORMAT_PCM_FLOAT);
107     }
108     // Request FLOAT for the shared mixer or the device.
109     request.getConfiguration().setFormat(AUDIO_FORMAT_PCM_FLOAT);
110 
111     // TODO b/182392769: use attribution source util
112     AttributionSourceState attributionSource;
113     attributionSource.uid = VALUE_OR_FATAL(android::legacy2aidl_uid_t_int32_t(getuid()));
114     attributionSource.pid = VALUE_OR_FATAL(android::legacy2aidl_pid_t_int32_t(getpid()));
115     attributionSource.packageName = builder.getOpPackageName();
116     attributionSource.attributionTag = builder.getAttributionTag();
117     attributionSource.token = sp<android::BBinder>::make();
118 
119     // Build the request to send to the server.
120     request.setAttributionSource(attributionSource);
121     request.setSharingModeMatchRequired(isSharingModeMatchRequired());
122     request.setInService(isInService());
123 
124     request.getConfiguration().setDeviceId(getDeviceId());
125     request.getConfiguration().setSampleRate(getSampleRate());
126     request.getConfiguration().setDirection(getDirection());
127     request.getConfiguration().setSharingMode(getSharingMode());
128     request.getConfiguration().setChannelMask(getChannelMask());
129 
130     request.getConfiguration().setUsage(getUsage());
131     request.getConfiguration().setContentType(getContentType());
132     request.getConfiguration().setSpatializationBehavior(getSpatializationBehavior());
133     request.getConfiguration().setIsContentSpatialized(isContentSpatialized());
134     request.getConfiguration().setInputPreset(getInputPreset());
135     request.getConfiguration().setPrivacySensitive(isPrivacySensitive());
136 
137     request.getConfiguration().setBufferCapacity(builder.getBufferCapacity());
138 
139     mServiceStreamHandleInfo = mServiceInterface.openStream(request, configurationOutput);
140     if (getServiceHandle() < 0
141             && (request.getConfiguration().getSamplesPerFrame() == 1
142                     || request.getConfiguration().getChannelMask() == AAUDIO_CHANNEL_MONO)
143             && getDirection() == AAUDIO_DIRECTION_OUTPUT
144             && !isInService()) {
145         // if that failed then try switching from mono to stereo if OUTPUT.
146         // Only do this in the client. Otherwise we end up with a mono mixer in the service
147         // that writes to a stereo MMAP stream.
148         ALOGD("%s() - openStream() returned %d, try switching from MONO to STEREO",
149               __func__, getServiceHandle());
150         request.getConfiguration().setChannelMask(AAUDIO_CHANNEL_STEREO);
151         mServiceStreamHandleInfo = mServiceInterface.openStream(request, configurationOutput);
152     }
153     if (getServiceHandle() < 0) {
154         return getServiceHandle();
155     }
156 
157     // This must match the key generated in oboeservice/AAudioServiceStreamBase.cpp
158     // so the client can have permission to log.
159     if (!mInService) {
160         // No need to log if it is from service side.
161         mMetricsId = std::string(AMEDIAMETRICS_KEY_PREFIX_AUDIO_STREAM)
162                      + std::to_string(getServiceHandle());
163     }
164 
165     android::mediametrics::LogItem(mMetricsId)
166             .set(AMEDIAMETRICS_PROP_PERFORMANCEMODE,
167                  AudioGlobal_convertPerformanceModeToText(builder.getPerformanceMode()))
168             .set(AMEDIAMETRICS_PROP_SHARINGMODE,
169                  AudioGlobal_convertSharingModeToText(builder.getSharingMode()))
170             .set(AMEDIAMETRICS_PROP_ENCODINGCLIENT,
171                  android::toString(requestedFormat).c_str()).record();
172 
173     result = configurationOutput.validate();
174     if (result != AAUDIO_OK) {
175         goto error;
176     }
177     // Save results of the open.
178     if (getChannelMask() == AAUDIO_UNSPECIFIED) {
179         setChannelMask(configurationOutput.getChannelMask());
180     }
181 
182     setDeviceId(configurationOutput.getDeviceId());
183     setSessionId(configurationOutput.getSessionId());
184     setSharingMode(configurationOutput.getSharingMode());
185 
186     setUsage(configurationOutput.getUsage());
187     setContentType(configurationOutput.getContentType());
188     setSpatializationBehavior(configurationOutput.getSpatializationBehavior());
189     setIsContentSpatialized(configurationOutput.isContentSpatialized());
190     setInputPreset(configurationOutput.getInputPreset());
191 
192     setDeviceSampleRate(configurationOutput.getSampleRate());
193 
194     if (getSampleRate() == AAUDIO_UNSPECIFIED) {
195         setSampleRate(configurationOutput.getSampleRate());
196     }
197 
198     if (!com::android::media::aaudio::sample_rate_conversion()) {
199         if (getSampleRate() != getDeviceSampleRate()) {
200             ALOGD("%s - skipping sample rate converter. SR = %d, Device SR = %d", __func__,
201                     getSampleRate(), getDeviceSampleRate());
202             result = AAUDIO_ERROR_INVALID_RATE;
203             goto error;
204         }
205     }
206 
207     // Save device format so we can do format conversion and volume scaling together.
208     setDeviceFormat(configurationOutput.getFormat());
209     setDeviceSamplesPerFrame(configurationOutput.getSamplesPerFrame());
210 
211     setHardwareSamplesPerFrame(configurationOutput.getHardwareSamplesPerFrame());
212     setHardwareSampleRate(configurationOutput.getHardwareSampleRate());
213     setHardwareFormat(configurationOutput.getHardwareFormat());
214 
215     result = mServiceInterface.getStreamDescription(mServiceStreamHandleInfo, mEndPointParcelable);
216     if (result != AAUDIO_OK) {
217         goto error;
218     }
219 
220     // Resolve parcelable into a descriptor.
221     result = mEndPointParcelable.resolve(&mEndpointDescriptor);
222     if (result != AAUDIO_OK) {
223         goto error;
224     }
225 
226     // Configure endpoint based on descriptor.
227     mAudioEndpoint = std::make_unique<AudioEndpoint>();
228     result = mAudioEndpoint->configure(&mEndpointDescriptor, getDirection());
229     if (result != AAUDIO_OK) {
230         goto error;
231     }
232 
233     if ((result = configureDataInformation(builder.getFramesPerDataCallback())) != AAUDIO_OK) {
234         goto error;
235     }
236 
237     setState(AAUDIO_STREAM_STATE_OPEN);
238 
239     return result;
240 
241 error:
242     safeReleaseClose();
243     return result;
244 }
245 
configureDataInformation(int32_t callbackFrames)246 aaudio_result_t AudioStreamInternal::configureDataInformation(int32_t callbackFrames) {
247     int32_t originalFramesPerBurst = mEndpointDescriptor.dataQueueDescriptor.framesPerBurst;
248     int32_t deviceFramesPerBurst = originalFramesPerBurst;
249 
250     // Scale up the burst size to meet the minimum equivalent in microseconds.
251     // This is to avoid waking the CPU too often when the HW burst is very small
252     // or at high sample rates. The actual number of frames that we call back to
253     // the app with will be 0 < N <= framesPerBurst so round up the division.
254     int32_t burstMicros = 0;
255     const int32_t burstMinMicros = android::AudioSystem::getAAudioHardwareBurstMinUsec();
256     do {
257         if (burstMicros > 0) {  // skip first loop
258             deviceFramesPerBurst *= 2;
259         }
260         burstMicros = deviceFramesPerBurst * static_cast<int64_t>(1000000) / getDeviceSampleRate();
261     } while (burstMicros < burstMinMicros);
262     ALOGD("%s() original HW burst = %d, minMicros = %d => SW burst = %d\n",
263           __func__, originalFramesPerBurst, burstMinMicros, deviceFramesPerBurst);
264 
265     // Validate final burst size.
266     if (deviceFramesPerBurst < MIN_FRAMES_PER_BURST
267             || deviceFramesPerBurst > MAX_FRAMES_PER_BURST) {
268         ALOGE("%s - deviceFramesPerBurst out of range = %d", __func__, deviceFramesPerBurst);
269         return AAUDIO_ERROR_OUT_OF_RANGE;
270     }
271 
272     // Calculate the application framesPerBurst from the deviceFramesPerBurst
273     int32_t framesPerBurst = (static_cast<int64_t>(deviceFramesPerBurst) * getSampleRate() +
274              getDeviceSampleRate() - 1) / getDeviceSampleRate();
275 
276     setDeviceFramesPerBurst(deviceFramesPerBurst);
277     setFramesPerBurst(framesPerBurst); // only save good value
278 
279     mDeviceBufferCapacityInFrames = mEndpointDescriptor.dataQueueDescriptor.capacityInFrames;
280 
281     mBufferCapacityInFrames = static_cast<int64_t>(mDeviceBufferCapacityInFrames)
282             * getSampleRate() / getDeviceSampleRate();
283     if (mBufferCapacityInFrames < getFramesPerBurst()
284             || mBufferCapacityInFrames > MAX_BUFFER_CAPACITY_IN_FRAMES) {
285         ALOGE("%s - bufferCapacity out of range = %d", __func__, mBufferCapacityInFrames);
286         return AAUDIO_ERROR_OUT_OF_RANGE;
287     }
288 
289     mClockModel.setSampleRate(getDeviceSampleRate());
290     mClockModel.setFramesPerBurst(deviceFramesPerBurst);
291 
292     if (isDataCallbackSet()) {
293         mCallbackFrames = callbackFrames;
294         if (mCallbackFrames > getBufferCapacity() / 2) {
295             ALOGW("%s - framesPerCallback too big = %d, capacity = %d",
296                   __func__, mCallbackFrames, getBufferCapacity());
297             return AAUDIO_ERROR_OUT_OF_RANGE;
298         } else if (mCallbackFrames < 0) {
299             ALOGW("%s - framesPerCallback negative", __func__);
300             return AAUDIO_ERROR_OUT_OF_RANGE;
301         }
302         if (mCallbackFrames == AAUDIO_UNSPECIFIED) {
303             mCallbackFrames = getFramesPerBurst();
304         }
305 
306         const int32_t callbackBufferSize = mCallbackFrames * getBytesPerFrame();
307         mCallbackBuffer = std::make_unique<uint8_t[]>(callbackBufferSize);
308     }
309 
310     // Exclusive output streams should combine channels when mono audio adjustment
311     // is enabled. They should also adjust for audio balance.
312     if ((getDirection() == AAUDIO_DIRECTION_OUTPUT) &&
313         (getSharingMode() == AAUDIO_SHARING_MODE_EXCLUSIVE)) {
314         bool isMasterMono = false;
315         android::AudioSystem::getMasterMono(&isMasterMono);
316         setRequireMonoBlend(isMasterMono);
317         float audioBalance = 0;
318         android::AudioSystem::getMasterBalance(&audioBalance);
319         setAudioBalance(audioBalance);
320     }
321 
322     // For debugging and analyzing the distribution of MMAP timestamps.
323     // For OUTPUT, use a NEGATIVE offset to move the CPU writes further BEFORE the HW reads.
324     // For INPUT, use a POSITIVE offset to move the CPU reads further AFTER the HW writes.
325     // You can use this offset to reduce glitching.
326     // You can also use this offset to force glitching. By iterating over multiple
327     // values you can reveal the distribution of the hardware timing jitter.
328     if (mAudioEndpoint->isFreeRunning()) { // MMAP?
329         int32_t offsetMicros = (getDirection() == AAUDIO_DIRECTION_OUTPUT)
330                 ? AAudioProperty_getOutputMMapOffsetMicros()
331                 : AAudioProperty_getInputMMapOffsetMicros();
332         // This log is used to debug some tricky glitch issues. Please leave.
333         ALOGD_IF(offsetMicros, "%s() - %s mmap offset = %d micros",
334                 __func__,
335                 (getDirection() == AAUDIO_DIRECTION_OUTPUT) ? "output" : "input",
336                 offsetMicros);
337         mTimeOffsetNanos = offsetMicros * AAUDIO_NANOS_PER_MICROSECOND;
338     }
339 
340     // Default buffer size to match Q
341     setBufferSize(mBufferCapacityInFrames / 2);
342     return AAUDIO_OK;
343 }
344 
345 // This must be called under mStreamLock.
release_l()346 aaudio_result_t AudioStreamInternal::release_l() {
347     aaudio_result_t result = AAUDIO_OK;
348     ALOGD("%s(): mServiceStreamHandle = 0x%08X", __func__, getServiceHandle());
349     if (getServiceHandle() != AAUDIO_HANDLE_INVALID) {
350         // Don't release a stream while it is running. Stop it first.
351         // If DISCONNECTED then we should still try to stop in case the
352         // error callback is still running.
353         if (isActive() || isDisconnected()) {
354             requestStop_l();
355         }
356 
357         logReleaseBufferState();
358 
359         setState(AAUDIO_STREAM_STATE_CLOSING);
360         auto serviceStreamHandleInfo = mServiceStreamHandleInfo;
361         mServiceStreamHandleInfo = AAudioHandleInfo();
362 
363         mServiceInterface.closeStream(serviceStreamHandleInfo);
364         mCallbackBuffer.reset();
365 
366         // Update local frame counters so we can query them after releasing the endpoint.
367         getFramesRead();
368         getFramesWritten();
369         mAudioEndpoint.reset();
370         result = mEndPointParcelable.close();
371         aaudio_result_t result2 = AudioStream::release_l();
372         return (result != AAUDIO_OK) ? result : result2;
373     } else {
374         return AAUDIO_ERROR_INVALID_HANDLE;
375     }
376 }
377 
aaudio_callback_thread_proc(void * context)378 static void *aaudio_callback_thread_proc(void *context)
379 {
380     AudioStreamInternal *stream = (AudioStreamInternal *)context;
381     //LOGD("oboe_callback_thread, stream = %p", stream);
382     if (stream != nullptr) {
383         return stream->callbackLoop();
384     } else {
385         return nullptr;
386     }
387 }
388 
exitStandby_l()389 aaudio_result_t AudioStreamInternal::exitStandby_l() {
390     AudioEndpointParcelable endpointParcelable;
391     // The stream is in standby mode, copy all available data and then close the duplicated
392     // shared file descriptor so that it won't cause issue when the HAL try to reallocate new
393     // shared file descriptor when exiting from standby.
394     // Cache current read counter, which will be reset to new read and write counter
395     // when the new data queue and endpoint are reconfigured.
396     const android::fifo_counter_t readCounter = mAudioEndpoint->getDataReadCounter();
397     // Cache the buffer size which may be from client.
398     const int32_t previousBufferSize = mBufferSizeInFrames;
399     // Copy all available data from current data queue.
400     uint8_t buffer[getDeviceBufferCapacity() * getBytesPerFrame()];
401     android::fifo_frames_t fullFramesAvailable = mAudioEndpoint->read(buffer,
402             getDeviceBufferCapacity());
403     // Before releasing the data queue, update the frames read and written.
404     getFramesRead();
405     getFramesWritten();
406     // Call freeDataQueue() here because the following call to
407     // closeDataFileDescriptor() will invalidate the pointers used by the data queue.
408     mAudioEndpoint->freeDataQueue();
409     mEndPointParcelable.closeDataFileDescriptor();
410     aaudio_result_t result = mServiceInterface.exitStandby(
411             mServiceStreamHandleInfo, endpointParcelable);
412     if (result != AAUDIO_OK) {
413         ALOGE("Failed to exit standby, error=%d", result);
414         goto exit;
415     }
416     // Reconstruct data queue descriptor using new shared file descriptor.
417     result = mEndPointParcelable.updateDataFileDescriptor(&endpointParcelable);
418     if (result != AAUDIO_OK) {
419         ALOGE("%s failed to update data file descriptor, error=%d", __func__, result);
420         goto exit;
421     }
422     result = mEndPointParcelable.resolveDataQueue(&mEndpointDescriptor.dataQueueDescriptor);
423     if (result != AAUDIO_OK) {
424         ALOGE("Failed to resolve data queue after exiting standby, error=%d", result);
425         goto exit;
426     }
427     // Reconfigure audio endpoint with new data queue descriptor.
428     mAudioEndpoint->configureDataQueue(
429             mEndpointDescriptor.dataQueueDescriptor, getDirection());
430     // Set read and write counters with previous read counter, the later write action
431     // will make the counter at the correct place.
432     mAudioEndpoint->setDataReadCounter(readCounter);
433     mAudioEndpoint->setDataWriteCounter(readCounter);
434     result = configureDataInformation(mCallbackFrames);
435     if (result != AAUDIO_OK) {
436         ALOGE("Failed to configure data information after exiting standby, error=%d", result);
437         goto exit;
438     }
439     // Write data from previous data buffer to new endpoint.
440     if (const android::fifo_frames_t framesWritten =
441                 mAudioEndpoint->write(buffer, fullFramesAvailable);
442             framesWritten != fullFramesAvailable) {
443         ALOGW("Some data lost after exiting standby, frames written: %d, "
444               "frames to write: %d", framesWritten, fullFramesAvailable);
445     }
446     // Reset previous buffer size as it may be requested by the client.
447     setBufferSize(previousBufferSize);
448 
449 exit:
450     return result;
451 }
452 
453 /*
454  * It normally takes about 20-30 msec to start a stream on the server.
455  * But the first time can take as much as 200-300 msec. The HW
456  * starts right away so by the time the client gets a chance to write into
457  * the buffer, it is already in a deep underflow state. That can cause the
458  * XRunCount to be non-zero, which could lead an app to tune its latency higher.
459  * To avoid this problem, we set a request for the processing code to start the
460  * client stream at the same position as the server stream.
461  * The processing code will then save the current offset
462  * between client and server and apply that to any position given to the app.
463  */
requestStart_l()464 aaudio_result_t AudioStreamInternal::requestStart_l()
465 {
466     int64_t startTime;
467     if (getServiceHandle() == AAUDIO_HANDLE_INVALID) {
468         ALOGD("requestStart() mServiceStreamHandle invalid");
469         return AAUDIO_ERROR_INVALID_STATE;
470     }
471     if (isActive()) {
472         ALOGD("requestStart() already active");
473         return AAUDIO_ERROR_INVALID_STATE;
474     }
475 
476     if (isDisconnected()) {
477         ALOGD("requestStart() but DISCONNECTED");
478         return AAUDIO_ERROR_DISCONNECTED;
479     }
480     const aaudio_stream_state_t originalState = getState();
481     setState(AAUDIO_STREAM_STATE_STARTING);
482 
483     // Clear any stale timestamps from the previous run.
484     drainTimestampsFromService();
485 
486     prepareBuffersForStart(); // tell subclasses to get ready
487 
488     aaudio_result_t result = mServiceInterface.startStream(mServiceStreamHandleInfo);
489     if (result == AAUDIO_ERROR_STANDBY) {
490         // The stream is at standby mode. Need to exit standby before starting the stream.
491         result = exitStandby_l();
492         if (result == AAUDIO_OK) {
493             result = mServiceInterface.startStream(mServiceStreamHandleInfo);
494         }
495     }
496     if (result != AAUDIO_OK) {
497         ALOGD("%s() error = %d, stream was probably stolen", __func__, result);
498         // Stealing was added in R. Coerce result to improve backward compatibility.
499         result = AAUDIO_ERROR_DISCONNECTED;
500         setDisconnected();
501     }
502 
503     startTime = AudioClock::getNanoseconds();
504     mClockModel.start(startTime);
505     mNeedCatchUp.request();  // Ask data processing code to catch up when first timestamp received.
506 
507     // Start data callback thread.
508     if (result == AAUDIO_OK && isDataCallbackSet()) {
509         // Launch the callback loop thread.
510         int64_t periodNanos = mCallbackFrames
511                               * AAUDIO_NANOS_PER_SECOND
512                               / getSampleRate();
513         mCallbackEnabled.store(true);
514         result = createThread_l(periodNanos, aaudio_callback_thread_proc, this);
515     }
516     if (result != AAUDIO_OK) {
517         setState(originalState);
518     }
519     return result;
520 }
521 
calculateReasonableTimeout(int32_t framesPerOperation)522 int64_t AudioStreamInternal::calculateReasonableTimeout(int32_t framesPerOperation) {
523 
524     // Wait for at least a second or some number of callbacks to join the thread.
525     int64_t timeoutNanoseconds = (MIN_TIMEOUT_OPERATIONS
526                                   * framesPerOperation
527                                   * AAUDIO_NANOS_PER_SECOND)
528                                   / getSampleRate();
529     if (timeoutNanoseconds < MIN_TIMEOUT_NANOS) { // arbitrary number of seconds
530         timeoutNanoseconds = MIN_TIMEOUT_NANOS;
531     }
532     return timeoutNanoseconds;
533 }
534 
calculateReasonableTimeout()535 int64_t AudioStreamInternal::calculateReasonableTimeout() {
536     return calculateReasonableTimeout(getFramesPerBurst());
537 }
538 
539 // This must be called under mStreamLock.
stopCallback_l()540 aaudio_result_t AudioStreamInternal::stopCallback_l()
541 {
542     if (isDataCallbackSet() && (isActive() || isDisconnected())) {
543         mCallbackEnabled.store(false);
544         aaudio_result_t result = joinThread_l(nullptr); // may temporarily unlock mStreamLock
545         if (result == AAUDIO_ERROR_INVALID_HANDLE) {
546             ALOGD("%s() INVALID_HANDLE, stream was probably stolen", __func__);
547             result = AAUDIO_OK;
548         }
549         return result;
550     } else {
551         ALOGD("%s() skipped, isDataCallbackSet() = %d, isActive() = %d, getState()  = %d", __func__,
552             isDataCallbackSet(), isActive(), getState());
553         return AAUDIO_OK;
554     }
555 }
556 
requestStop_l()557 aaudio_result_t AudioStreamInternal::requestStop_l() {
558     aaudio_result_t result = stopCallback_l();
559     if (result != AAUDIO_OK) {
560         ALOGW("%s() stop callback returned %d, returning early", __func__, result);
561         return result;
562     }
563     // The stream may have been unlocked temporarily to let a callback finish
564     // and the callback may have stopped the stream.
565     // Check to make sure the stream still needs to be stopped.
566     // See also AudioStream::safeStop_l().
567     if (!(isActive() || isDisconnected())) {
568         ALOGD("%s() returning early, not active or disconnected", __func__);
569         return AAUDIO_OK;
570     }
571 
572     if (getServiceHandle() == AAUDIO_HANDLE_INVALID) {
573         ALOGW("%s() mServiceStreamHandle invalid = 0x%08X",
574               __func__, getServiceHandle());
575         return AAUDIO_ERROR_INVALID_STATE;
576     }
577 
578     // For playback, sleep until all the audio data has played.
579     // Then clear the buffer to prevent noise.
580     prepareBuffersForStop();
581 
582     mClockModel.stop(AudioClock::getNanoseconds());
583     setState(AAUDIO_STREAM_STATE_STOPPING);
584     mAtomicInternalTimestamp.clear();
585 
586 #if 0
587     // Simulate very slow CPU, force race condition where the
588     // DSP keeps playing after we stop writing.
589     AudioClock::sleepForNanos(800 * AAUDIO_NANOS_PER_MILLISECOND);
590 #endif
591 
592     result = mServiceInterface.stopStream(mServiceStreamHandleInfo);
593     if (result == AAUDIO_ERROR_INVALID_HANDLE) {
594         ALOGD("%s() INVALID_HANDLE, stream was probably stolen", __func__);
595         result = AAUDIO_OK;
596     }
597     return result;
598 }
599 
registerThread()600 aaudio_result_t AudioStreamInternal::registerThread() {
601     if (getServiceHandle() == AAUDIO_HANDLE_INVALID) {
602         ALOGW("%s() mServiceStreamHandle invalid", __func__);
603         return AAUDIO_ERROR_INVALID_STATE;
604     }
605     return mServiceInterface.registerAudioThread(mServiceStreamHandleInfo,
606                                                  gettid(),
607                                                  getPeriodNanoseconds());
608 }
609 
unregisterThread()610 aaudio_result_t AudioStreamInternal::unregisterThread() {
611     if (getServiceHandle() == AAUDIO_HANDLE_INVALID) {
612         ALOGW("%s() mServiceStreamHandle invalid", __func__);
613         return AAUDIO_ERROR_INVALID_STATE;
614     }
615     return mServiceInterface.unregisterAudioThread(mServiceStreamHandleInfo, gettid());
616 }
617 
startClient(const android::AudioClient & client,const audio_attributes_t * attr,audio_port_handle_t * portHandle)618 aaudio_result_t AudioStreamInternal::startClient(const android::AudioClient& client,
619                                                  const audio_attributes_t *attr,
620                                                  audio_port_handle_t *portHandle) {
621     ALOGV("%s() called", __func__);
622     if (getServiceHandle() == AAUDIO_HANDLE_INVALID) {
623         ALOGE("%s() getServiceHandle() is invalid", __func__);
624         return AAUDIO_ERROR_INVALID_STATE;
625     }
626     aaudio_result_t result =  mServiceInterface.startClient(mServiceStreamHandleInfo,
627                                                             client, attr, portHandle);
628     ALOGV("%s(), got %d, returning %d", __func__, *portHandle, result);
629     return result;
630 }
631 
stopClient(audio_port_handle_t portHandle)632 aaudio_result_t AudioStreamInternal::stopClient(audio_port_handle_t portHandle) {
633     ALOGV("%s(%d) called", __func__, portHandle);
634     if (getServiceHandle() == AAUDIO_HANDLE_INVALID) {
635         ALOGE("%s(%d) getServiceHandle() is invalid", __func__, portHandle);
636         return AAUDIO_ERROR_INVALID_STATE;
637     }
638     aaudio_result_t result = mServiceInterface.stopClient(mServiceStreamHandleInfo, portHandle);
639     ALOGV("%s(%d) returning %d", __func__, portHandle, result);
640     return result;
641 }
642 
getTimestamp(clockid_t,int64_t * framePosition,int64_t * timeNanoseconds)643 aaudio_result_t AudioStreamInternal::getTimestamp(clockid_t /*clockId*/,
644                            int64_t *framePosition,
645                            int64_t *timeNanoseconds) {
646     // Generated in server and passed to client. Return latest.
647     if (mAtomicInternalTimestamp.isValid()) {
648         Timestamp timestamp = mAtomicInternalTimestamp.read();
649         // This should not overflow as timestamp.getPosition() should be a position in a buffer and
650         // not the actual timestamp. timestamp.getNanoseconds() below uses the actual timestamp.
651         // At 48000 Hz we can run for over 100 years before overflowing the int64_t.
652         int64_t position = (timestamp.getPosition() + mFramesOffsetFromService) * getSampleRate() /
653                 getDeviceSampleRate();
654         if (position >= 0) {
655             *framePosition = position;
656             *timeNanoseconds = timestamp.getNanoseconds();
657             return AAUDIO_OK;
658         }
659     }
660     return AAUDIO_ERROR_INVALID_STATE;
661 }
662 
logTimestamp(AAudioServiceMessage & command)663 void AudioStreamInternal::logTimestamp(AAudioServiceMessage &command) {
664     static int64_t oldPosition = 0;
665     static int64_t oldTime = 0;
666     int64_t framePosition = command.timestamp.position;
667     int64_t nanoTime = command.timestamp.timestamp;
668     ALOGD("logTimestamp: timestamp says framePosition = %8lld at nanoTime %lld",
669          (long long) framePosition,
670          (long long) nanoTime);
671     int64_t nanosDelta = nanoTime - oldTime;
672     if (nanosDelta > 0 && oldTime > 0) {
673         int64_t framesDelta = framePosition - oldPosition;
674         int64_t rate = (framesDelta * AAUDIO_NANOS_PER_SECOND) / nanosDelta;
675         ALOGD("logTimestamp:     framesDelta = %8lld, nanosDelta = %8lld, rate = %lld",
676               (long long) framesDelta, (long long) nanosDelta, (long long) rate);
677     }
678     oldPosition = framePosition;
679     oldTime = nanoTime;
680 }
681 
onTimestampService(AAudioServiceMessage * message)682 aaudio_result_t AudioStreamInternal::onTimestampService(AAudioServiceMessage *message) {
683 #if LOG_TIMESTAMPS
684     logTimestamp(*message);
685 #endif
686     processTimestamp(message->timestamp.position,
687             message->timestamp.timestamp + mTimeOffsetNanos);
688     return AAUDIO_OK;
689 }
690 
onTimestampHardware(AAudioServiceMessage * message)691 aaudio_result_t AudioStreamInternal::onTimestampHardware(AAudioServiceMessage *message) {
692     Timestamp timestamp(message->timestamp.position, message->timestamp.timestamp);
693     mAtomicInternalTimestamp.write(timestamp);
694     return AAUDIO_OK;
695 }
696 
onEventFromServer(AAudioServiceMessage * message)697 aaudio_result_t AudioStreamInternal::onEventFromServer(AAudioServiceMessage *message) {
698     aaudio_result_t result = AAUDIO_OK;
699     switch (message->event.event) {
700         case AAUDIO_SERVICE_EVENT_STARTED:
701             ALOGD("%s - got AAUDIO_SERVICE_EVENT_STARTED", __func__);
702             if (getState() == AAUDIO_STREAM_STATE_STARTING) {
703                 setState(AAUDIO_STREAM_STATE_STARTED);
704             }
705             mPlayerBase->triggerPortIdUpdate(static_cast<audio_port_handle_t>(
706                                                  message->event.dataLong));
707             break;
708         case AAUDIO_SERVICE_EVENT_PAUSED:
709             ALOGD("%s - got AAUDIO_SERVICE_EVENT_PAUSED", __func__);
710             if (getState() == AAUDIO_STREAM_STATE_PAUSING) {
711                 setState(AAUDIO_STREAM_STATE_PAUSED);
712             }
713             break;
714         case AAUDIO_SERVICE_EVENT_STOPPED:
715             ALOGD("%s - got AAUDIO_SERVICE_EVENT_STOPPED", __func__);
716             if (getState() == AAUDIO_STREAM_STATE_STOPPING) {
717                 setState(AAUDIO_STREAM_STATE_STOPPED);
718             }
719             break;
720         case AAUDIO_SERVICE_EVENT_FLUSHED:
721             ALOGD("%s - got AAUDIO_SERVICE_EVENT_FLUSHED", __func__);
722             if (getState() == AAUDIO_STREAM_STATE_FLUSHING) {
723                 setState(AAUDIO_STREAM_STATE_FLUSHED);
724                 onFlushFromServer();
725             }
726             break;
727         case AAUDIO_SERVICE_EVENT_DISCONNECTED:
728             // Prevent hardware from looping on old data and making buzzing sounds.
729             if (getDirection() == AAUDIO_DIRECTION_OUTPUT) {
730                 mAudioEndpoint->eraseDataMemory();
731             }
732             result = AAUDIO_ERROR_DISCONNECTED;
733             setDisconnected();
734             ALOGW("%s - AAUDIO_SERVICE_EVENT_DISCONNECTED - FIFO cleared", __func__);
735             break;
736         case AAUDIO_SERVICE_EVENT_VOLUME:
737             ALOGD("%s - AAUDIO_SERVICE_EVENT_VOLUME %lf", __func__, message->event.dataDouble);
738             mStreamVolume = (float)message->event.dataDouble;
739             doSetVolume();
740             break;
741         case AAUDIO_SERVICE_EVENT_XRUN:
742             mXRunCount = static_cast<int32_t>(message->event.dataLong);
743             break;
744         default:
745             ALOGE("%s - Unrecognized event = %d", __func__, (int) message->event.event);
746             break;
747     }
748     return result;
749 }
750 
drainTimestampsFromService()751 aaudio_result_t AudioStreamInternal::drainTimestampsFromService() {
752     aaudio_result_t result = AAUDIO_OK;
753 
754     while (result == AAUDIO_OK) {
755         AAudioServiceMessage message;
756         if (!mAudioEndpoint) {
757             break;
758         }
759         if (mAudioEndpoint->readUpCommand(&message) != 1) {
760             break; // no command this time, no problem
761         }
762         switch (message.what) {
763             // ignore most messages
764             case AAudioServiceMessage::code::TIMESTAMP_SERVICE:
765             case AAudioServiceMessage::code::TIMESTAMP_HARDWARE:
766                 break;
767 
768             case AAudioServiceMessage::code::EVENT:
769                 result = onEventFromServer(&message);
770                 break;
771 
772             default:
773                 ALOGE("%s - unrecognized message.what = %d", __func__, (int) message.what);
774                 result = AAUDIO_ERROR_INTERNAL;
775                 break;
776         }
777     }
778     return result;
779 }
780 
781 // Process all the commands coming from the server.
processCommands()782 aaudio_result_t AudioStreamInternal::processCommands() {
783     aaudio_result_t result = AAUDIO_OK;
784 
785     while (result == AAUDIO_OK) {
786         AAudioServiceMessage message;
787         if (!mAudioEndpoint) {
788             break;
789         }
790         if (mAudioEndpoint->readUpCommand(&message) != 1) {
791             break; // no command this time, no problem
792         }
793         switch (message.what) {
794         case AAudioServiceMessage::code::TIMESTAMP_SERVICE:
795             result = onTimestampService(&message);
796             break;
797 
798         case AAudioServiceMessage::code::TIMESTAMP_HARDWARE:
799             result = onTimestampHardware(&message);
800             break;
801 
802         case AAudioServiceMessage::code::EVENT:
803             result = onEventFromServer(&message);
804             break;
805 
806         default:
807             ALOGE("%s - unrecognized message.what = %d", __func__, (int) message.what);
808             result = AAUDIO_ERROR_INTERNAL;
809             break;
810         }
811     }
812     return result;
813 }
814 
815 // Read or write the data, block if needed and timeoutMillis > 0
processData(void * buffer,int32_t numFrames,int64_t timeoutNanoseconds)816 aaudio_result_t AudioStreamInternal::processData(void *buffer, int32_t numFrames,
817                                                  int64_t timeoutNanoseconds)
818 {
819     if (isDisconnected()) {
820         return AAUDIO_ERROR_DISCONNECTED;
821     }
822     if (!mInService &&
823         AAudioBinderClient::getInstance().getServiceLifetimeId() != getServiceLifetimeId()) {
824         // The service lifetime id will be changed whenever the binder died. In that case, if
825         // the service lifetime id from AAudioBinderClient is different from the cached one,
826         // returns AAUDIO_ERROR_DISCONNECTED.
827         // Note that only compare the service lifetime id if it is not in service as the streams
828         // in service will all be gone when aaudio service dies.
829         mClockModel.stop(AudioClock::getNanoseconds());
830         // Set the stream as disconnected as the service lifetime id will only change when
831         // the binder dies.
832         setDisconnected();
833         return AAUDIO_ERROR_DISCONNECTED;
834     }
835     const char * traceName = "aaProc";
836     const char * fifoName = "aaRdy";
837     ATRACE_BEGIN(traceName);
838     if (ATRACE_ENABLED()) {
839         int32_t fullFrames = mAudioEndpoint->getFullFramesAvailable();
840         ATRACE_INT(fifoName, fullFrames);
841     }
842 
843     aaudio_result_t result = AAUDIO_OK;
844     int32_t loopCount = 0;
845     uint8_t* audioData = (uint8_t*)buffer;
846     int64_t currentTimeNanos = AudioClock::getNanoseconds();
847     const int64_t entryTimeNanos = currentTimeNanos;
848     const int64_t deadlineNanos = currentTimeNanos + timeoutNanoseconds;
849     int32_t framesLeft = numFrames;
850 
851     // Loop until all the data has been processed or until a timeout occurs.
852     while (framesLeft > 0) {
853         // The call to processDataNow() will not block. It will just process as much as it can.
854         int64_t wakeTimeNanos = 0;
855         aaudio_result_t framesProcessed = processDataNow(audioData, framesLeft,
856                                                   currentTimeNanos, &wakeTimeNanos);
857         if (framesProcessed < 0) {
858             result = framesProcessed;
859             break;
860         }
861         framesLeft -= (int32_t) framesProcessed;
862         audioData += framesProcessed * getBytesPerFrame();
863 
864         // Should we block?
865         if (timeoutNanoseconds == 0) {
866             break; // don't block
867         } else if (wakeTimeNanos != 0) {
868             if (!mAudioEndpoint->isFreeRunning()) {
869                 // If there is software on the other end of the FIFO then it may get delayed.
870                 // So wake up just a little after we expect it to be ready.
871                 wakeTimeNanos += mWakeupDelayNanos;
872             }
873 
874             currentTimeNanos = AudioClock::getNanoseconds();
875             int64_t earliestWakeTime = currentTimeNanos + mMinimumSleepNanos;
876             // Guarantee a minimum sleep time.
877             if (wakeTimeNanos < earliestWakeTime) {
878                 wakeTimeNanos = earliestWakeTime;
879             }
880 
881             if (wakeTimeNanos > deadlineNanos) {
882                 // If we time out, just return the framesWritten so far.
883                 ALOGW("processData(): entered at %lld nanos, currently %lld",
884                       (long long) entryTimeNanos, (long long) currentTimeNanos);
885                 ALOGW("processData(): TIMEOUT after %lld nanos",
886                       (long long) timeoutNanoseconds);
887                 ALOGW("processData(): wakeTime = %lld, deadline = %lld nanos",
888                       (long long) wakeTimeNanos, (long long) deadlineNanos);
889                 ALOGW("processData(): past deadline by %d micros",
890                       (int)((wakeTimeNanos - deadlineNanos) / AAUDIO_NANOS_PER_MICROSECOND));
891                 mClockModel.dump();
892                 mAudioEndpoint->dump();
893                 break;
894             }
895 
896             if (ATRACE_ENABLED()) {
897                 int32_t fullFrames = mAudioEndpoint->getFullFramesAvailable();
898                 ATRACE_INT(fifoName, fullFrames);
899                 int64_t sleepForNanos = wakeTimeNanos - currentTimeNanos;
900                 ATRACE_INT("aaSlpNs", (int32_t)sleepForNanos);
901             }
902 
903             AudioClock::sleepUntilNanoTime(wakeTimeNanos);
904             currentTimeNanos = AudioClock::getNanoseconds();
905         }
906     }
907 
908     if (ATRACE_ENABLED()) {
909         int32_t fullFrames = mAudioEndpoint->getFullFramesAvailable();
910         ATRACE_INT(fifoName, fullFrames);
911     }
912 
913     // return error or framesProcessed
914     (void) loopCount;
915     ATRACE_END();
916     return (result < 0) ? result : numFrames - framesLeft;
917 }
918 
processTimestamp(uint64_t position,int64_t time)919 void AudioStreamInternal::processTimestamp(uint64_t position, int64_t time) {
920     mClockModel.processTimestamp(position, time);
921 }
922 
setBufferSize(int32_t requestedFrames)923 aaudio_result_t AudioStreamInternal::setBufferSize(int32_t requestedFrames) {
924     const int32_t maximumSize = getBufferCapacity() - getFramesPerBurst();
925     int32_t adjustedFrames = std::min(requestedFrames, maximumSize);
926     // Buffer sizes should always be a multiple of framesPerBurst.
927     int32_t numBursts = (static_cast<int64_t>(adjustedFrames) + getFramesPerBurst() - 1) /
928         getFramesPerBurst();
929 
930     // Use at least one burst
931     if (numBursts == 0) {
932         numBursts = 1;
933     }
934 
935     // Set a minimum number of bursts if sample rate conversion is used.
936     if ((getSampleRate() != getDeviceSampleRate()) &&
937             (numBursts < MIN_SAMPLE_RATE_CONVERSION_NUM_BURSTS)) {
938         numBursts = MIN_SAMPLE_RATE_CONVERSION_NUM_BURSTS;
939     }
940 
941     if (mAudioEndpoint) {
942         // Clip against the actual size from the endpoint.
943         int32_t actualFramesDevice = 0;
944         int32_t maximumFramesDevice = getDeviceBufferCapacity() - getDeviceFramesPerBurst();
945         // Set to maximum size so we can write extra data when ready in order to reduce glitches.
946         // The amount we keep in the buffer is controlled by mBufferSizeInFrames.
947         mAudioEndpoint->setBufferSizeInFrames(maximumFramesDevice, &actualFramesDevice);
948         int32_t actualNumBursts = actualFramesDevice / getDeviceFramesPerBurst();
949         numBursts = std::min(numBursts, actualNumBursts);
950     }
951 
952     const int32_t bufferSizeInFrames = numBursts * getFramesPerBurst();
953     const int32_t deviceBufferSizeInFrames = numBursts * getDeviceFramesPerBurst();
954 
955     if (deviceBufferSizeInFrames != mDeviceBufferSizeInFrames) {
956         android::mediametrics::LogItem(mMetricsId)
957                 .set(AMEDIAMETRICS_PROP_EVENT, AMEDIAMETRICS_PROP_EVENT_VALUE_SETBUFFERSIZE)
958                 .set(AMEDIAMETRICS_PROP_BUFFERSIZEFRAMES, deviceBufferSizeInFrames)
959                 .set(AMEDIAMETRICS_PROP_UNDERRUN, (int32_t) getXRunCount())
960                 .record();
961     }
962 
963     mBufferSizeInFrames = bufferSizeInFrames;
964     mDeviceBufferSizeInFrames = deviceBufferSizeInFrames;
965     ALOGV("%s(%d) returns %d", __func__, requestedFrames, adjustedFrames);
966     return (aaudio_result_t) adjustedFrames;
967 }
968 
getBufferSize() const969 int32_t AudioStreamInternal::getBufferSize() const {
970     return mBufferSizeInFrames;
971 }
972 
getDeviceBufferSize() const973 int32_t AudioStreamInternal::getDeviceBufferSize() const {
974     return mDeviceBufferSizeInFrames;
975 }
976 
getBufferCapacity() const977 int32_t AudioStreamInternal::getBufferCapacity() const {
978     return mBufferCapacityInFrames;
979 }
980 
getDeviceBufferCapacity() const981 int32_t AudioStreamInternal::getDeviceBufferCapacity() const {
982     return mDeviceBufferCapacityInFrames;
983 }
984 
isClockModelInControl() const985 bool AudioStreamInternal::isClockModelInControl() const {
986     return isActive() && mAudioEndpoint->isFreeRunning() && mClockModel.isRunning();
987 }
988