1 /*
2  * Copyright (C) 2017 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 "AAudioServiceEndpointMMAP"
18 //#define LOG_NDEBUG 0
19 #include <utils/Log.h>
20 
21 #include <algorithm>
22 #include <assert.h>
23 #include <map>
24 #include <mutex>
25 #include <set>
26 #include <sstream>
27 #include <thread>
28 #include <utils/Singleton.h>
29 #include <vector>
30 
31 #include "AAudioEndpointManager.h"
32 #include "AAudioServiceEndpoint.h"
33 
34 #include "core/AudioStreamBuilder.h"
35 #include "AAudioServiceEndpoint.h"
36 #include "AAudioServiceStreamShared.h"
37 #include "AAudioServiceEndpointPlay.h"
38 #include "AAudioServiceEndpointMMAP.h"
39 
40 #include <com_android_media_aaudio.h>
41 
42 #define AAUDIO_BUFFER_CAPACITY_MIN    (4 * 512)
43 #define AAUDIO_SAMPLE_RATE_DEFAULT    48000
44 
45 // This is an estimate of the time difference between the HW and the MMAP time.
46 // TODO Get presentation timestamps from the HAL instead of using these estimates.
47 #define OUTPUT_ESTIMATED_HARDWARE_OFFSET_NANOS  (3 * AAUDIO_NANOS_PER_MILLISECOND)
48 #define INPUT_ESTIMATED_HARDWARE_OFFSET_NANOS   (-1 * AAUDIO_NANOS_PER_MILLISECOND)
49 
50 #define AAUDIO_MAX_OPEN_ATTEMPTS    10
51 
52 using namespace android;  // TODO just import names needed
53 using namespace aaudio;   // TODO just import names needed
54 
AAudioServiceEndpointMMAP(AAudioService & audioService)55 AAudioServiceEndpointMMAP::AAudioServiceEndpointMMAP(AAudioService &audioService)
56         : mMmapStream(nullptr)
57         , mAAudioService(audioService) {}
58 
dump() const59 std::string AAudioServiceEndpointMMAP::dump() const {
60     std::stringstream result;
61 
62     result << "  MMAP: framesTransferred = " << mFramesTransferred.get();
63     result << ", HW nanos = " << mHardwareTimeOffsetNanos;
64     result << ", port handle = " << mPortHandle;
65     result << ", audio data FD = " << mAudioDataWrapper->getDataFileDescriptor();
66     result << "\n";
67 
68     result << "    HW Offset Micros:     " <<
69                                       (getHardwareTimeOffsetNanos()
70                                        / AAUDIO_NANOS_PER_MICROSECOND) << "\n";
71 
72     result << AAudioServiceEndpoint::dump();
73     return result.str();
74 }
75 
76 namespace {
77 
78 const static std::map<audio_format_t, audio_format_t> NEXT_FORMAT_TO_TRY = {
79         {AUDIO_FORMAT_PCM_FLOAT,         AUDIO_FORMAT_PCM_32_BIT},
80         {AUDIO_FORMAT_PCM_32_BIT,        AUDIO_FORMAT_PCM_24_BIT_PACKED},
81         {AUDIO_FORMAT_PCM_24_BIT_PACKED, AUDIO_FORMAT_PCM_8_24_BIT},
82         {AUDIO_FORMAT_PCM_8_24_BIT,      AUDIO_FORMAT_PCM_16_BIT}
83 };
84 
getNextFormatToTry(audio_format_t curFormat)85 audio_format_t getNextFormatToTry(audio_format_t curFormat) {
86     const auto it = NEXT_FORMAT_TO_TRY.find(curFormat);
87     return it != NEXT_FORMAT_TO_TRY.end() ? it->second : curFormat;
88 }
89 
90 struct configComp {
operator ()__anonbedb458e0111::configComp91     bool operator() (const audio_config_base_t& lhs, const audio_config_base_t& rhs) const {
92         if (lhs.sample_rate != rhs.sample_rate) {
93             return lhs.sample_rate < rhs.sample_rate;
94         } else if (lhs.channel_mask != rhs.channel_mask) {
95             return lhs.channel_mask < rhs.channel_mask;
96         } else {
97             return lhs.format < rhs.format;
98         }
99     }
100 };
101 
102 } // namespace
103 
open(const aaudio::AAudioStreamRequest & request)104 aaudio_result_t AAudioServiceEndpointMMAP::open(const aaudio::AAudioStreamRequest &request) {
105     aaudio_result_t result = AAUDIO_OK;
106     mAudioDataWrapper = std::make_unique<SharedMemoryWrapper>();
107     copyFrom(request.getConstantConfiguration());
108     mRequestedDeviceId = getDeviceId();
109 
110     mMmapClient.attributionSource = request.getAttributionSource();
111     // TODO b/182392769: use attribution source util
112     mMmapClient.attributionSource.uid = VALUE_OR_FATAL(
113         legacy2aidl_uid_t_int32_t(IPCThreadState::self()->getCallingUid()));
114     mMmapClient.attributionSource.pid = VALUE_OR_FATAL(
115         legacy2aidl_pid_t_int32_t(IPCThreadState::self()->getCallingPid()));
116 
117     audio_format_t audioFormat = getFormat();
118     int32_t sampleRate = getSampleRate();
119     if (sampleRate == AAUDIO_UNSPECIFIED) {
120         sampleRate = AAUDIO_SAMPLE_RATE_DEFAULT;
121     }
122 
123     const aaudio_direction_t direction = getDirection();
124     audio_config_base_t config;
125     config.format = audioFormat;
126     config.sample_rate = sampleRate;
127     config.channel_mask = AAudio_getChannelMaskForOpen(
128             getChannelMask(), getSamplesPerFrame(), direction == AAUDIO_DIRECTION_INPUT);
129 
130     std::set<audio_config_base_t, configComp> configsTried;
131     int32_t numberOfAttempts = 0;
132     while (numberOfAttempts < AAUDIO_MAX_OPEN_ATTEMPTS) {
133         if (configsTried.find(config) != configsTried.end()) {
134             // APM returning something that has already tried.
135             ALOGW("Have already tried to open with format=%#x and sr=%d, but failed before",
136                   config.format, config.sample_rate);
137             break;
138         }
139         configsTried.insert(config);
140 
141         audio_config_base_t previousConfig = config;
142         result = openWithConfig(&config);
143         if (result != AAUDIO_ERROR_UNAVAILABLE) {
144             // Return if it is successful or there is an error that is not
145             // AAUDIO_ERROR_UNAVAILABLE happens.
146             ALOGI("Opened format=%#x sr=%d, with result=%d", previousConfig.format,
147                     previousConfig.sample_rate, result);
148             break;
149         }
150 
151         // Try other formats if the config from APM is the same as our current config.
152         // Some HALs may report its format support incorrectly.
153         if (previousConfig.format == config.format) {
154             if (previousConfig.sample_rate == config.sample_rate) {
155                 config.format = getNextFormatToTry(config.format);
156             } else if (!com::android::media::aaudio::sample_rate_conversion()) {
157                 ALOGI("%s() - AAudio SRC feature not enabled, different rates! %d != %d",
158                       __func__, previousConfig.sample_rate, config.sample_rate);
159                 result = AAUDIO_ERROR_INVALID_RATE;
160                 break;
161             }
162         }
163 
164         ALOGD("%s() %#x %d failed, perhaps due to format or sample rate. Try again with %#x %d",
165                 __func__, previousConfig.format, previousConfig.sample_rate, config.format,
166                 config.sample_rate);
167         numberOfAttempts++;
168     }
169     return result;
170 }
171 
openWithConfig(audio_config_base_t * config)172 aaudio_result_t AAudioServiceEndpointMMAP::openWithConfig(
173         audio_config_base_t* config) {
174     aaudio_result_t result = AAUDIO_OK;
175     audio_config_base_t currentConfig = *config;
176     audio_port_handle_t deviceId;
177 
178     const audio_attributes_t attributes = getAudioAttributesFrom(this);
179 
180     deviceId = mRequestedDeviceId;
181 
182     const aaudio_direction_t direction = getDirection();
183 
184     if (direction == AAUDIO_DIRECTION_OUTPUT) {
185         mHardwareTimeOffsetNanos = OUTPUT_ESTIMATED_HARDWARE_OFFSET_NANOS; // frames at DAC later
186 
187     } else if (direction == AAUDIO_DIRECTION_INPUT) {
188         mHardwareTimeOffsetNanos = INPUT_ESTIMATED_HARDWARE_OFFSET_NANOS; // frames at ADC earlier
189 
190     } else {
191         ALOGE("%s() invalid direction = %d", __func__, direction);
192         return AAUDIO_ERROR_ILLEGAL_ARGUMENT;
193     }
194 
195     const MmapStreamInterface::stream_direction_t streamDirection =
196             (direction == AAUDIO_DIRECTION_OUTPUT)
197             ? MmapStreamInterface::DIRECTION_OUTPUT
198             : MmapStreamInterface::DIRECTION_INPUT;
199 
200     const aaudio_session_id_t requestedSessionId = getSessionId();
201     audio_session_t sessionId = AAudioConvert_aaudioToAndroidSessionId(requestedSessionId);
202 
203     // Open HAL stream. Set mMmapStream
204     ALOGD("%s trying to open MMAP stream with format=%#x, "
205           "sample_rate=%u, channel_mask=%#x, device=%d",
206           __func__, config->format, config->sample_rate,
207           config->channel_mask, deviceId);
208 
209     const std::lock_guard<std::mutex> lock(mMmapStreamLock);
210     const status_t status = MmapStreamInterface::openMmapStream(streamDirection,
211                                                                 &attributes,
212                                                                 config,
213                                                                 mMmapClient,
214                                                                 &deviceId,
215                                                                 &sessionId,
216                                                                 this, // callback
217                                                                 mMmapStream,
218                                                                 &mPortHandle);
219     ALOGD("%s() mMapClient.attributionSource = %s => portHandle = %d\n",
220           __func__, mMmapClient.attributionSource.toString().c_str(), mPortHandle);
221     if (status != OK) {
222         // This can happen if the resource is busy or the config does
223         // not match the hardware.
224         ALOGD("%s() - openMmapStream() returned status=%d, suggested format=%#x, sample_rate=%u, "
225               "channel_mask=%#x",
226               __func__, status, config->format, config->sample_rate, config->channel_mask);
227         // Keep the channel mask of the current config
228         config->channel_mask = currentConfig.channel_mask;
229         return AAUDIO_ERROR_UNAVAILABLE;
230     }
231 
232     if (deviceId == AAUDIO_UNSPECIFIED) {
233         ALOGW("%s() - openMmapStream() failed to set deviceId", __func__);
234     }
235     setDeviceId(deviceId);
236 
237     if (sessionId == AUDIO_SESSION_ALLOCATE) {
238         ALOGW("%s() - openMmapStream() failed to set sessionId", __func__);
239     }
240 
241     const aaudio_session_id_t actualSessionId =
242             (requestedSessionId == AAUDIO_SESSION_ID_NONE)
243             ? AAUDIO_SESSION_ID_NONE
244             : (aaudio_session_id_t) sessionId;
245     setSessionId(actualSessionId);
246 
247     ALOGD("%s(format = 0x%X) deviceId = %d, sessionId = %d",
248           __func__, config->format, getDeviceId(), getSessionId());
249 
250     // Create MMAP/NOIRQ buffer.
251     result = createMmapBuffer_l();
252     if (result != AAUDIO_OK) {
253         goto error;
254     }
255 
256     // Get information about the stream and pass it back to the caller.
257     setChannelMask(AAudioConvert_androidToAAudioChannelMask(
258             config->channel_mask, getDirection() == AAUDIO_DIRECTION_INPUT,
259             AAudio_isChannelIndexMask(config->channel_mask)));
260 
261     setFormat(config->format);
262     setSampleRate(config->sample_rate);
263     setHardwareSampleRate(getSampleRate());
264     setHardwareFormat(getFormat());
265     setHardwareSamplesPerFrame(AAudioConvert_channelMaskToCount(getChannelMask()));
266 
267     // If the position is not updated while the timestamp is updated for more than a certain amount,
268     // the timestamp reported from the HAL may not be accurate. Here, a timestamp grace period is
269     // set as 5 burst size. We may want to update this value if there is any report from OEMs saying
270     // that is too short.
271     static constexpr int kTimestampGraceBurstCount = 5;
272     mTimestampGracePeriodMs = ((int64_t) kTimestampGraceBurstCount * mFramesPerBurst
273             * AAUDIO_MILLIS_PER_SECOND) / getSampleRate();
274 
275     mDataReportOffsetNanos = ((int64_t)mTimestampGracePeriodMs) * AAUDIO_NANOS_PER_MILLISECOND;
276 
277     ALOGD("%s() got rate = %d, channels = %d channelMask = %#x, deviceId = %d, capacity = %d\n",
278           __func__, getSampleRate(), getSamplesPerFrame(), getChannelMask(),
279           deviceId, getBufferCapacity());
280 
281     ALOGD("%s() got format = 0x%X = %s, frame size = %d, burst size = %d",
282           __func__, getFormat(), audio_format_to_string(getFormat()),
283           calculateBytesPerFrame(), mFramesPerBurst);
284 
285     return result;
286 
287 error:
288     close_l();
289     // restore original requests
290     setDeviceId(mRequestedDeviceId);
291     setSessionId(requestedSessionId);
292     return result;
293 }
294 
close()295 void AAudioServiceEndpointMMAP::close() {
296     bool closedIt = false;
297     {
298         const std::lock_guard<std::mutex> lock(mMmapStreamLock);
299         closedIt = close_l();
300     }
301     if (closedIt) {
302         // TODO Why is this needed?
303         AudioClock::sleepForNanos(100 * AAUDIO_NANOS_PER_MILLISECOND);
304     }
305 }
306 
close_l()307 bool AAudioServiceEndpointMMAP::close_l() { // requires mMmapStreamLock
308     bool closedIt = false;
309     if (mMmapStream != nullptr) {
310         // Needs to be explicitly cleared or CTS will fail but it is not clear why.
311         ALOGD("%s() clear mMmapStream", __func__);
312         mMmapStream.clear();
313         closedIt = true;
314     }
315     return closedIt;
316 }
317 
startStream(sp<AAudioServiceStreamBase> stream,audio_port_handle_t * clientHandle __unused)318 aaudio_result_t AAudioServiceEndpointMMAP::startStream(sp<AAudioServiceStreamBase> stream,
319                                                    audio_port_handle_t *clientHandle __unused) {
320     // Start the client on behalf of the AAudio service.
321     // Use the port handle that was provided by openMmapStream().
322     audio_port_handle_t tempHandle = mPortHandle;
323     audio_attributes_t attr = {};
324     if (stream != nullptr) {
325         attr = getAudioAttributesFrom(stream.get());
326     }
327     const aaudio_result_t result = startClient(
328             mMmapClient, stream == nullptr ? nullptr : &attr, &tempHandle);
329     // When AudioFlinger is passed a valid port handle then it should not change it.
330     LOG_ALWAYS_FATAL_IF(tempHandle != mPortHandle,
331                         "%s() port handle not expected to change from %d to %d",
332                         __func__, mPortHandle, tempHandle);
333     ALOGV("%s() mPortHandle = %d", __func__, mPortHandle);
334     return result;
335 }
336 
stopStream(sp<AAudioServiceStreamBase>,audio_port_handle_t clientHandle)337 aaudio_result_t AAudioServiceEndpointMMAP::stopStream(sp<AAudioServiceStreamBase> /*stream*/,
338                                                       audio_port_handle_t clientHandle) {
339     mFramesTransferred.reset32();
340 
341     // Round 64-bit counter up to a multiple of the buffer capacity.
342     // This is required because the 64-bit counter is used as an index
343     // into a circular buffer and the actual HW position is reset to zero
344     // when the stream is stopped.
345     mFramesTransferred.roundUp64(getBufferCapacity());
346 
347     // Use the port handle that was provided by openMmapStream().
348     aaudio_result_t result = stopClient(mPortHandle);
349     ALOGD("%s(%d): called stopClient(%d=mPortHandle), returning %d", __func__,
350           (int)clientHandle, mPortHandle, result);
351     return result;
352 }
353 
startClient(const android::AudioClient & client,const audio_attributes_t * attr,audio_port_handle_t * portHandlePtr)354 aaudio_result_t AAudioServiceEndpointMMAP::startClient(const android::AudioClient& client,
355                                                        const audio_attributes_t *attr,
356                                                        audio_port_handle_t *portHandlePtr) {
357     const std::lock_guard<std::mutex> lock(mMmapStreamLock);
358     if (mMmapStream == nullptr) {
359         ALOGW("%s(): called after mMmapStream set to NULL", __func__);
360         return AAUDIO_ERROR_NULL;
361     } else if (!isConnected()) {
362         ALOGD("%s(): MMAP stream was disconnected", __func__);
363         return AAUDIO_ERROR_DISCONNECTED;
364     } else {
365         aaudio_result_t result = AAudioConvert_androidToAAudioResult(
366                 mMmapStream->start(client, attr, portHandlePtr));
367         if (!isConnected() && (portHandlePtr != nullptr)) {
368             ALOGD("%s(): MMAP stream DISCONNECTED after starting port %d, will stop it",
369                   __func__, *portHandlePtr);
370             mMmapStream->stop(*portHandlePtr);
371             *portHandlePtr = AUDIO_PORT_HANDLE_NONE;
372             result = AAUDIO_ERROR_DISCONNECTED;
373         }
374         ALOGD("%s(): returning port %d, result %d", __func__,
375               (portHandlePtr == nullptr) ? -1 : *portHandlePtr, result);
376         return result;
377     }
378 }
379 
stopClient(audio_port_handle_t portHandle)380 aaudio_result_t AAudioServiceEndpointMMAP::stopClient(audio_port_handle_t portHandle) {
381     const std::lock_guard<std::mutex> lock(mMmapStreamLock);
382     if (mMmapStream == nullptr) {
383         ALOGE("%s(%d): called after mMmapStream set to NULL", __func__, (int)portHandle);
384         return AAUDIO_ERROR_NULL;
385     } else {
386         aaudio_result_t result = AAudioConvert_androidToAAudioResult(
387                 mMmapStream->stop(portHandle));
388         ALOGD("%s(%d): returning %d", __func__, (int)portHandle, result);
389         return result;
390     }
391 }
392 
standby()393 aaudio_result_t AAudioServiceEndpointMMAP::standby() {
394     const std::lock_guard<std::mutex> lock(mMmapStreamLock);
395     if (mMmapStream == nullptr) {
396         ALOGW("%s(): called after mMmapStream set to NULL", __func__);
397         return AAUDIO_ERROR_NULL;
398     } else {
399         return AAudioConvert_androidToAAudioResult(mMmapStream->standby());
400     }
401 }
402 
exitStandby(AudioEndpointParcelable * parcelable)403 aaudio_result_t AAudioServiceEndpointMMAP::exitStandby(AudioEndpointParcelable* parcelable) {
404     const std::lock_guard<std::mutex> lock(mMmapStreamLock);
405     if (mMmapStream == nullptr) {
406         return AAUDIO_ERROR_NULL;
407     }
408     mAudioDataWrapper->reset();
409     const aaudio_result_t result = createMmapBuffer_l();
410     if (result == AAUDIO_OK) {
411         getDownDataDescription(parcelable);
412     }
413     return result;
414 }
415 
416 // Get free-running DSP or DMA hardware position from the HAL.
getFreeRunningPosition(int64_t * positionFrames,int64_t * timeNanos)417 aaudio_result_t AAudioServiceEndpointMMAP::getFreeRunningPosition(int64_t *positionFrames,
418                                                                 int64_t *timeNanos) {
419     const std::lock_guard<std::mutex> lock(mMmapStreamLock);
420     if (mMmapStream == nullptr) {
421         ALOGW("%s(): called after mMmapStream set to NULL", __func__);
422         return AAUDIO_ERROR_NULL;
423     }
424     struct audio_mmap_position position;
425     const status_t status = mMmapStream->getMmapPosition(&position);
426     ALOGV("%s() status= %d, pos = %d, nanos = %lld\n",
427           __func__, status, position.position_frames, (long long) position.time_nanoseconds);
428     const aaudio_result_t result = AAudioConvert_androidToAAudioResult(status);
429     if (result == AAUDIO_ERROR_UNAVAILABLE) {
430         ALOGW("%s(): getMmapPosition() has no position data available", __func__);
431     } else if (result != AAUDIO_OK) {
432         ALOGE("%s(): getMmapPosition() returned status %d", __func__, status);
433     } else {
434         // Convert 32-bit position to 64-bit position.
435         mFramesTransferred.update32(position.position_frames);
436         *positionFrames = mFramesTransferred.get();
437         *timeNanos = position.time_nanoseconds;
438     }
439     return result;
440 }
441 
getTimestamp(int64_t *,int64_t *)442 aaudio_result_t AAudioServiceEndpointMMAP::getTimestamp(int64_t* /*positionFrames*/,
443                                                         int64_t* /*timeNanos*/) {
444     return 0; // TODO
445 }
446 
447 // This is called by onTearDown() in a separate thread to avoid deadlocks.
handleTearDownAsync(audio_port_handle_t portHandle)448 void AAudioServiceEndpointMMAP::handleTearDownAsync(audio_port_handle_t portHandle) {
449     // Are we tearing down the EXCLUSIVE MMAP stream?
450     if (isStreamRegistered(portHandle)) {
451         ALOGD("%s(%d) tearing down this entire MMAP endpoint", __func__, portHandle);
452         disconnectRegisteredStreams();
453     } else {
454         // Must be a SHARED stream?
455         ALOGD("%s(%d) disconnect a specific stream", __func__, portHandle);
456         const aaudio_result_t result = mAAudioService.disconnectStreamByPortHandle(portHandle);
457         ALOGD("%s(%d) disconnectStreamByPortHandle returned %d", __func__, portHandle, result);
458     }
459 };
460 
461 // This is called by AudioFlinger when it wants to destroy a stream.
onTearDown(audio_port_handle_t portHandle)462 void AAudioServiceEndpointMMAP::onTearDown(audio_port_handle_t portHandle) {
463     ALOGD("%s(portHandle = %d) called", __func__, portHandle);
464     const android::sp<AAudioServiceEndpointMMAP> holdEndpoint(this);
465     std::thread asyncTask([holdEndpoint, portHandle]() {
466         holdEndpoint->handleTearDownAsync(portHandle);
467     });
468     asyncTask.detach();
469 }
470 
onVolumeChanged(float volume)471 void AAudioServiceEndpointMMAP::onVolumeChanged(float volume) {
472     ALOGD("%s() volume = %f", __func__, volume);
473     const std::lock_guard<std::mutex> lock(mLockStreams);
474     for(const auto& stream : mRegisteredStreams) {
475         stream->onVolumeChanged(volume);
476     }
477 };
478 
onRoutingChanged(audio_port_handle_t portHandle)479 void AAudioServiceEndpointMMAP::onRoutingChanged(audio_port_handle_t portHandle) {
480     const auto deviceId = static_cast<int32_t>(portHandle);
481     ALOGD("%s() called with dev %d, old = %d", __func__, deviceId, getDeviceId());
482     if (getDeviceId() != deviceId) {
483         if (getDeviceId() != AUDIO_PORT_HANDLE_NONE) {
484             // When there is a routing changed, mmap stream should be disconnected. Set `mConnected`
485             // as false here so that there won't be a new stream connect to this endpoint.
486             mConnected.store(false);
487             const android::sp<AAudioServiceEndpointMMAP> holdEndpoint(this);
488             std::thread asyncTask([holdEndpoint, deviceId]() {
489                 ALOGD("onRoutingChanged() asyncTask launched");
490                 // When routing changed, the stream is disconnected and cannot be used except for
491                 // closing. In that case, it should be safe to release all registered streams.
492                 // This can help release service side resource in case the client doesn't close
493                 // the stream after receiving disconnect event.
494                 holdEndpoint->releaseRegisteredStreams();
495                 holdEndpoint->setDeviceId(deviceId);
496             });
497             asyncTask.detach();
498         } else {
499             setDeviceId(deviceId);
500         }
501     }
502 };
503 
504 /**
505  * Get an immutable description of the data queue from the HAL.
506  */
getDownDataDescription(AudioEndpointParcelable * parcelable)507 aaudio_result_t AAudioServiceEndpointMMAP::getDownDataDescription(
508         AudioEndpointParcelable* parcelable)
509 {
510     if (mAudioDataWrapper->setupFifoBuffer(calculateBytesPerFrame(), getBufferCapacity())
511         != AAUDIO_OK) {
512         ALOGE("Failed to setup audio data wrapper, will not be able to "
513               "set data for sound dose computation");
514         // This will not affect the audio processing capability
515     }
516     // Gather information on the data queue based on HAL info.
517     mAudioDataWrapper->fillParcelable(parcelable, parcelable->mDownDataQueueParcelable,
518                                       calculateBytesPerFrame(), mFramesPerBurst,
519                                       getBufferCapacity(),
520                                       getDirection() == AAUDIO_DIRECTION_OUTPUT
521                                               ? SharedMemoryWrapper::WRITE
522                                               : SharedMemoryWrapper::NONE);
523     return AAUDIO_OK;
524 }
525 
getExternalPosition(uint64_t * positionFrames,int64_t * timeNanos)526 aaudio_result_t AAudioServiceEndpointMMAP::getExternalPosition(uint64_t *positionFrames,
527                                                                int64_t *timeNanos)
528 {
529     const std::lock_guard<std::mutex> lock(mMmapStreamLock);
530     if (mHalExternalPositionStatus != AAUDIO_OK) {
531         return mHalExternalPositionStatus;
532     }
533     if (mMmapStream == nullptr) {
534         ALOGW("%s(): called after mMmapStream set to NULL", __func__);
535         return AAUDIO_ERROR_NULL;
536     }
537     uint64_t tempPositionFrames;
538     int64_t tempTimeNanos;
539     const status_t status = mMmapStream->getExternalPosition(&tempPositionFrames, &tempTimeNanos);
540     if (status != OK) {
541         // getExternalPosition reports error. The HAL may not support the API. Cache the result
542         // so that the call will not go to the HAL next time.
543         mHalExternalPositionStatus = AAudioConvert_androidToAAudioResult(status);
544         return mHalExternalPositionStatus;
545     }
546 
547     // If the HAL keeps reporting the same position or timestamp, the HAL may be having some issues
548     // to report correct external position. In that case, we will not trust the values reported from
549     // the HAL. Ideally, we may want to stop querying external position if the HAL cannot report
550     // correct position within a period. But it may not be a good idea to get system time too often.
551     // In that case, a maximum number of frozen external position is defined so that if the
552     // count of the same timestamp or position is reported by the HAL continuously, the values from
553     // the HAL will no longer be trusted.
554     static constexpr int kMaxFrozenCount = 20;
555     // If the HAL version is less than 7.0, the getPresentationPosition is an optional API.
556     // If the HAL version is 7.0 or later, the getPresentationPosition is a mandatory API.
557     // In that case, even the returned status is NO_ERROR, it doesn't indicate the returned
558     // position is a valid one. Do a simple validation, which is checking if the position is
559     // forward within half a second or not, here so that this function can return error if
560     // the validation fails. Note that we don't only apply this validation logic to HAL API
561     // less than 7.0. The reason is that there is a chance the HAL is not reporting the
562     // timestamp and position correctly.
563     if (mLastPositionFrames > tempPositionFrames) {
564         // If the position is going backwards, there must be something wrong with the HAL.
565         // In that case, we do not trust the values reported by the HAL.
566         ALOGW("%s position is going backwards, last position(%jd) current position(%jd)",
567               __func__, mLastPositionFrames, tempPositionFrames);
568         mHalExternalPositionStatus = AAUDIO_ERROR_INTERNAL;
569         return mHalExternalPositionStatus;
570     } else if (mLastPositionFrames == tempPositionFrames) {
571         if (tempTimeNanos - mTimestampNanosForLastPosition >
572                 AAUDIO_NANOS_PER_MILLISECOND * mTimestampGracePeriodMs) {
573             ALOGW("%s, the reported position is not changed within %d msec. "
574                   "Set the external position as not supported", __func__, mTimestampGracePeriodMs);
575             mHalExternalPositionStatus = AAUDIO_ERROR_INTERNAL;
576             return mHalExternalPositionStatus;
577         }
578         mFrozenPositionCount++;
579     } else {
580         mFrozenPositionCount = 0;
581     }
582 
583     if (mTimestampNanosForLastPosition > tempTimeNanos) {
584         // If the timestamp is going backwards, there must be something wrong with the HAL.
585         // In that case, we do not trust the values reported by the HAL.
586         ALOGW("%s timestamp is going backwards, last timestamp(%jd), current timestamp(%jd)",
587               __func__, mTimestampNanosForLastPosition, tempTimeNanos);
588         mHalExternalPositionStatus = AAUDIO_ERROR_INTERNAL;
589         return mHalExternalPositionStatus;
590     } else if (mTimestampNanosForLastPosition == tempTimeNanos) {
591         mFrozenTimestampCount++;
592     } else {
593         mFrozenTimestampCount = 0;
594     }
595 
596     if (mFrozenTimestampCount + mFrozenPositionCount > kMaxFrozenCount) {
597         ALOGW("%s too many frozen external position from HAL.", __func__);
598         mHalExternalPositionStatus = AAUDIO_ERROR_INTERNAL;
599         return mHalExternalPositionStatus;
600     }
601 
602     mLastPositionFrames = tempPositionFrames;
603     mTimestampNanosForLastPosition = tempTimeNanos;
604 
605     // Only update the timestamp and position when they looks valid.
606     *positionFrames = tempPositionFrames;
607     *timeNanos = tempTimeNanos;
608     return mHalExternalPositionStatus;
609 }
610 
611 // mMmapStreamLock should be held when calling this function.
createMmapBuffer_l()612 aaudio_result_t AAudioServiceEndpointMMAP::createMmapBuffer_l()
613 {
614     memset(&mMmapBufferinfo, 0, sizeof(struct audio_mmap_buffer_info));
615     int32_t minSizeFrames = getBufferCapacity();
616     if (minSizeFrames <= 0) { // zero will get rejected
617         minSizeFrames = AAUDIO_BUFFER_CAPACITY_MIN;
618     }
619 
620     if (mMmapStream == nullptr) {
621         ALOGW("%s(): called after mMmapStream set to NULL", __func__);
622         return AAUDIO_ERROR_NULL;
623     }
624 
625     const status_t status = mMmapStream->createMmapBuffer(minSizeFrames, &mMmapBufferinfo);
626     const bool isBufferShareable = mMmapBufferinfo.flags & AUDIO_MMAP_APPLICATION_SHAREABLE;
627     if (status != OK) {
628         ALOGE("%s() - createMmapBuffer() failed with status %d %s",
629               __func__, status, strerror(-status));
630         return AAUDIO_ERROR_UNAVAILABLE;
631     } else {
632         ALOGD("%s() createMmapBuffer() buffer_size = %d fr, burst_size %d fr"
633                       ", Sharable FD: %s",
634               __func__,
635               mMmapBufferinfo.buffer_size_frames,
636               mMmapBufferinfo.burst_size_frames,
637               isBufferShareable ? "Yes" : "No");
638     }
639 
640     setBufferCapacity(mMmapBufferinfo.buffer_size_frames);
641     if (!isBufferShareable) {
642         // Exclusive mode can only be used by the service because the FD cannot be shared.
643         const int32_t audioServiceUid =
644             VALUE_OR_FATAL(legacy2aidl_uid_t_int32_t(getuid()));
645         if ((mMmapClient.attributionSource.uid != audioServiceUid) &&
646             getSharingMode() == AAUDIO_SHARING_MODE_EXCLUSIVE) {
647             ALOGW("%s() - exclusive FD cannot be used by client", __func__);
648             return AAUDIO_ERROR_UNAVAILABLE;
649         }
650     }
651 
652     // AAudio creates a copy of this FD and retains ownership of the copy.
653     // Assume that AudioFlinger will close the original shared_memory_fd.
654 
655     mAudioDataWrapper->getDataFileDescriptor().reset(dup(mMmapBufferinfo.shared_memory_fd));
656     if (mAudioDataWrapper->getDataFileDescriptor().get() == -1) {
657         ALOGE("%s() - could not dup shared_memory_fd", __func__);
658         return AAUDIO_ERROR_INTERNAL;
659     }
660 
661     // Call to HAL to make sure the transport FD was able to be closed by binder.
662     // This is a tricky workaround for a problem in Binder.
663     // TODO:[b/192048842] When that problem is fixed we may be able to remove or change this code.
664     ALOGD("%s() - call getMmapPosition() as a hack to clear FD stuck in Binder", __func__);
665     struct audio_mmap_position position;
666     mMmapStream->getMmapPosition(&position);
667 
668     mFramesPerBurst = mMmapBufferinfo.burst_size_frames;
669 
670     return AAUDIO_OK;
671 }
672 
nextDataReportTime()673 int64_t AAudioServiceEndpointMMAP::nextDataReportTime() {
674     return getDirection() == AAUDIO_DIRECTION_OUTPUT
675             ? AudioClock::getNanoseconds() + mDataReportOffsetNanos
676             : std::numeric_limits<int64_t>::max();
677 }
678 
reportData()679 void AAudioServiceEndpointMMAP::reportData() {
680     const std::lock_guard<std::mutex> lock(mMmapStreamLock);
681 
682     if (mMmapStream == nullptr) {
683         // This must not happen
684         ALOGE("%s() invalid state, mmap stream is not initialized", __func__);
685         return;
686     }
687 
688     auto fifo = mAudioDataWrapper->getFifoBuffer();
689     if (fifo == nullptr) {
690         ALOGE("%s() fifo buffer is not initialized, cannot report data", __func__);
691         return;
692     }
693 
694     WrappingBuffer wrappingBuffer;
695     fifo_frames_t framesAvailable = fifo->getFullDataAvailable(&wrappingBuffer);
696     for (size_t i = 0; i < WrappingBuffer::SIZE; ++i) {
697         if (wrappingBuffer.numFrames[i] > 0) {
698             mMmapStream->reportData(wrappingBuffer.data[i], wrappingBuffer.numFrames[i]);
699         }
700     }
701     fifo->advanceReadIndex(framesAvailable);
702 }
703