1 /*
2 **
3 ** Copyright 2008, The Android Open Source Project
4 **
5 ** Licensed under the Apache License, Version 2.0 (the "License");
6 ** you may not use this file except in compliance with the License.
7 ** You may obtain a copy of the License at
8 **
9 **     http://www.apache.org/licenses/LICENSE-2.0
10 **
11 ** Unless required by applicable law or agreed to in writing, software
12 ** distributed under the License is distributed on an "AS IS" BASIS,
13 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 ** See the License for the specific language governing permissions and
15 ** limitations under the License.
16 */
17 
18 //#define LOG_NDEBUG 0
19 #define LOG_TAG "AudioRecord"
20 
21 #include <inttypes.h>
22 #include <android-base/macros.h>
23 #include <sys/resource.h>
24 
25 #include <audiomanager/AudioManager.h>
26 #include <audiomanager/IAudioManager.h>
27 #include <binder/Binder.h>
28 #include <binder/IPCThreadState.h>
29 #include <binder/IServiceManager.h>
30 #include <media/AudioRecord.h>
31 #include <utils/Log.h>
32 #include <private/media/AudioTrackShared.h>
33 #include <processgroup/sched_policy.h>
34 #include <media/IAudioFlinger.h>
35 #include <media/MediaMetricsItem.h>
36 #include <media/TypeConverter.h>
37 
38 #define WAIT_PERIOD_MS          10
39 
40 namespace android {
41 // ---------------------------------------------------------------------------
42 
43 // static
getMinFrameCount(size_t * frameCount,uint32_t sampleRate,audio_format_t format,audio_channel_mask_t channelMask)44 status_t AudioRecord::getMinFrameCount(
45         size_t* frameCount,
46         uint32_t sampleRate,
47         audio_format_t format,
48         audio_channel_mask_t channelMask)
49 {
50     if (frameCount == NULL) {
51         return BAD_VALUE;
52     }
53 
54     size_t size;
55     status_t status = AudioSystem::getInputBufferSize(sampleRate, format, channelMask, &size);
56     if (status != NO_ERROR) {
57         ALOGE("%s(): AudioSystem could not query the input buffer size for"
58               " sampleRate %u, format %#x, channelMask %#x; status %d",
59                __func__, sampleRate, format, channelMask, status);
60         return status;
61     }
62 
63     // We double the size of input buffer for ping pong use of record buffer.
64     // Assumes audio_is_linear_pcm(format)
65     if ((*frameCount = (size * 2) / (audio_channel_count_from_in_mask(channelMask) *
66             audio_bytes_per_sample(format))) == 0) {
67         ALOGE("%s(): Unsupported configuration: sampleRate %u, format %#x, channelMask %#x",
68                 __func__, sampleRate, format, channelMask);
69         return BAD_VALUE;
70     }
71 
72     return NO_ERROR;
73 }
74 
75 // ---------------------------------------------------------------------------
76 
gather(const AudioRecord * record)77 void AudioRecord::MediaMetrics::gather(const AudioRecord *record)
78 {
79 #define MM_PREFIX "android.media.audiorecord." // avoid cut-n-paste errors.
80 
81     // Java API 28 entries, do not change.
82     mMetricsItem->setCString(MM_PREFIX "encoding", toString(record->mFormat).c_str());
83     mMetricsItem->setCString(MM_PREFIX "source", toString(record->mAttributes.source).c_str());
84     mMetricsItem->setInt32(MM_PREFIX "latency", (int32_t)record->mLatency); // bad estimate.
85     mMetricsItem->setInt32(MM_PREFIX "samplerate", (int32_t)record->mSampleRate);
86     mMetricsItem->setInt32(MM_PREFIX "channels", (int32_t)record->mChannelCount);
87 
88     // Non-API entries, these can change.
89     mMetricsItem->setInt32(MM_PREFIX "portId", (int32_t)record->mPortId);
90     mMetricsItem->setInt32(MM_PREFIX "frameCount", (int32_t)record->mFrameCount);
91     mMetricsItem->setCString(MM_PREFIX "attributes", toString(record->mAttributes).c_str());
92     mMetricsItem->setInt64(MM_PREFIX "channelMask", (int64_t)record->mChannelMask);
93 
94     // log total duration recording, including anything currently running.
95     int64_t activeNs = 0;
96     if (mStartedNs != 0) {
97         activeNs = systemTime() - mStartedNs;
98     }
99     mMetricsItem->setDouble(MM_PREFIX "durationMs", (mDurationNs + activeNs) * 1e-6);
100     mMetricsItem->setInt64(MM_PREFIX "startCount", (int64_t)mCount);
101 
102     if (mLastError != NO_ERROR) {
103         mMetricsItem->setInt32(MM_PREFIX "lastError.code", (int32_t)mLastError);
104         mMetricsItem->setCString(MM_PREFIX "lastError.at", mLastErrorFunc.c_str());
105     }
106 }
107 
stateToString(bool active)108 static const char *stateToString(bool active) {
109     return active ? "ACTIVE" : "STOPPED";
110 }
111 
112 // hand the user a snapshot of the metrics.
getMetrics(mediametrics::Item * & item)113 status_t AudioRecord::getMetrics(mediametrics::Item * &item)
114 {
115     mMediaMetrics.gather(this);
116     mediametrics::Item *tmp = mMediaMetrics.dup();
117     if (tmp == nullptr) {
118         return BAD_VALUE;
119     }
120     item = tmp;
121     return NO_ERROR;
122 }
123 
AudioRecord(const String16 & opPackageName)124 AudioRecord::AudioRecord(const String16 &opPackageName)
125     : mActive(false), mStatus(NO_INIT), mOpPackageName(opPackageName),
126       mSessionId(AUDIO_SESSION_ALLOCATE),
127       mPreviousPriority(ANDROID_PRIORITY_NORMAL), mPreviousSchedulingGroup(SP_DEFAULT),
128       mSelectedDeviceId(AUDIO_PORT_HANDLE_NONE), mRoutedDeviceId(AUDIO_PORT_HANDLE_NONE),
129       mSelectedMicDirection(MIC_DIRECTION_UNSPECIFIED),
130       mSelectedMicFieldDimension(MIC_FIELD_DIMENSION_DEFAULT)
131 {
132 }
133 
AudioRecord(audio_source_t inputSource,uint32_t sampleRate,audio_format_t format,audio_channel_mask_t channelMask,const String16 & opPackageName,size_t frameCount,callback_t cbf,void * user,uint32_t notificationFrames,audio_session_t sessionId,transfer_type transferType,audio_input_flags_t flags,uid_t uid,pid_t pid,const audio_attributes_t * pAttributes,audio_port_handle_t selectedDeviceId,audio_microphone_direction_t selectedMicDirection,float microphoneFieldDimension)134 AudioRecord::AudioRecord(
135         audio_source_t inputSource,
136         uint32_t sampleRate,
137         audio_format_t format,
138         audio_channel_mask_t channelMask,
139         const String16& opPackageName,
140         size_t frameCount,
141         callback_t cbf,
142         void* user,
143         uint32_t notificationFrames,
144         audio_session_t sessionId,
145         transfer_type transferType,
146         audio_input_flags_t flags,
147         uid_t uid,
148         pid_t pid,
149         const audio_attributes_t* pAttributes,
150         audio_port_handle_t selectedDeviceId,
151         audio_microphone_direction_t selectedMicDirection,
152         float microphoneFieldDimension)
153     : mActive(false),
154       mStatus(NO_INIT),
155       mOpPackageName(opPackageName),
156       mSessionId(AUDIO_SESSION_ALLOCATE),
157       mPreviousPriority(ANDROID_PRIORITY_NORMAL),
158       mPreviousSchedulingGroup(SP_DEFAULT),
159       mProxy(NULL)
160 {
161     (void)set(inputSource, sampleRate, format, channelMask, frameCount, cbf, user,
162             notificationFrames, false /*threadCanCallJava*/, sessionId, transferType, flags,
163             uid, pid, pAttributes, selectedDeviceId,
164             selectedMicDirection, microphoneFieldDimension);
165 }
166 
~AudioRecord()167 AudioRecord::~AudioRecord()
168 {
169     mMediaMetrics.gather(this);
170 
171     mediametrics::LogItem(mMetricsId)
172         .set(AMEDIAMETRICS_PROP_EVENT, AMEDIAMETRICS_PROP_EVENT_VALUE_DTOR)
173         .set(AMEDIAMETRICS_PROP_CALLERNAME,
174                 mCallerName.empty()
175                 ? AMEDIAMETRICS_PROP_CALLERNAME_VALUE_UNKNOWN
176                 : mCallerName.c_str())
177         .set(AMEDIAMETRICS_PROP_STATUS, (int32_t)mStatus)
178         .record();
179 
180     if (mStatus == NO_ERROR) {
181         // Make sure that callback function exits in the case where
182         // it is looping on buffer empty condition in obtainBuffer().
183         // Otherwise the callback thread will never exit.
184         stop();
185         if (mAudioRecordThread != 0) {
186             mProxy->interrupt();
187             mAudioRecordThread->requestExit();  // see comment in AudioRecord.h
188             mAudioRecordThread->requestExitAndWait();
189             mAudioRecordThread.clear();
190         }
191         // No lock here: worst case we remove a NULL callback which will be a nop
192         if (mDeviceCallback != 0 && mInput != AUDIO_IO_HANDLE_NONE) {
193             AudioSystem::removeAudioDeviceCallback(this, mInput, mPortId);
194         }
195         IInterface::asBinder(mAudioRecord)->unlinkToDeath(mDeathNotifier, this);
196         mAudioRecord.clear();
197         mCblkMemory.clear();
198         mBufferMemory.clear();
199         IPCThreadState::self()->flushCommands();
200         ALOGV("%s(%d): releasing session id %d",
201                 __func__, mPortId, mSessionId);
202         AudioSystem::releaseAudioSessionId(mSessionId, mClientPid);
203     }
204 }
205 
set(audio_source_t inputSource,uint32_t sampleRate,audio_format_t format,audio_channel_mask_t channelMask,size_t frameCount,callback_t cbf,void * user,uint32_t notificationFrames,bool threadCanCallJava,audio_session_t sessionId,transfer_type transferType,audio_input_flags_t flags,uid_t uid,pid_t pid,const audio_attributes_t * pAttributes,audio_port_handle_t selectedDeviceId,audio_microphone_direction_t selectedMicDirection,float microphoneFieldDimension)206 status_t AudioRecord::set(
207         audio_source_t inputSource,
208         uint32_t sampleRate,
209         audio_format_t format,
210         audio_channel_mask_t channelMask,
211         size_t frameCount,
212         callback_t cbf,
213         void* user,
214         uint32_t notificationFrames,
215         bool threadCanCallJava,
216         audio_session_t sessionId,
217         transfer_type transferType,
218         audio_input_flags_t flags,
219         uid_t uid,
220         pid_t pid,
221         const audio_attributes_t* pAttributes,
222         audio_port_handle_t selectedDeviceId,
223         audio_microphone_direction_t selectedMicDirection,
224         float microphoneFieldDimension)
225 {
226     status_t status = NO_ERROR;
227     uint32_t channelCount;
228     pid_t callingPid;
229     pid_t myPid;
230 
231     // Note mPortId is not valid until the track is created, so omit mPortId in ALOG for set.
232     ALOGV("%s(): inputSource %d, sampleRate %u, format %#x, channelMask %#x, frameCount %zu, "
233           "notificationFrames %u, sessionId %d, transferType %d, flags %#x, opPackageName %s "
234           "uid %d, pid %d",
235           __func__,
236           inputSource, sampleRate, format, channelMask, frameCount, notificationFrames,
237           sessionId, transferType, flags, String8(mOpPackageName).string(), uid, pid);
238 
239     mTracker.reset(new RecordingActivityTracker());
240 
241     mSelectedDeviceId = selectedDeviceId;
242     mSelectedMicDirection = selectedMicDirection;
243     mSelectedMicFieldDimension = microphoneFieldDimension;
244 
245     switch (transferType) {
246     case TRANSFER_DEFAULT:
247         if (cbf == NULL || threadCanCallJava) {
248             transferType = TRANSFER_SYNC;
249         } else {
250             transferType = TRANSFER_CALLBACK;
251         }
252         break;
253     case TRANSFER_CALLBACK:
254         if (cbf == NULL) {
255             ALOGE("%s(): Transfer type TRANSFER_CALLBACK but cbf == NULL", __func__);
256             status = BAD_VALUE;
257             goto exit;
258         }
259         break;
260     case TRANSFER_OBTAIN:
261     case TRANSFER_SYNC:
262         break;
263     default:
264         ALOGE("%s(): Invalid transfer type %d", __func__, transferType);
265         status = BAD_VALUE;
266         goto exit;
267     }
268     mTransfer = transferType;
269 
270     // invariant that mAudioRecord != 0 is true only after set() returns successfully
271     if (mAudioRecord != 0) {
272         ALOGE("%s(): Track already in use", __func__);
273         status = INVALID_OPERATION;
274         goto exit;
275     }
276 
277     if (pAttributes == NULL) {
278         mAttributes = AUDIO_ATTRIBUTES_INITIALIZER;
279         mAttributes.source = inputSource;
280         if (inputSource == AUDIO_SOURCE_VOICE_COMMUNICATION
281                 || inputSource == AUDIO_SOURCE_CAMCORDER) {
282             mAttributes.flags |= AUDIO_FLAG_CAPTURE_PRIVATE;
283         }
284     } else {
285         // stream type shouldn't be looked at, this track has audio attributes
286         memcpy(&mAttributes, pAttributes, sizeof(audio_attributes_t));
287         ALOGV("%s(): Building AudioRecord with attributes: source=%d flags=0x%x tags=[%s]",
288                 __func__, mAttributes.source, mAttributes.flags, mAttributes.tags);
289     }
290 
291     mSampleRate = sampleRate;
292 
293     // these below should probably come from the audioFlinger too...
294     if (format == AUDIO_FORMAT_DEFAULT) {
295         format = AUDIO_FORMAT_PCM_16_BIT;
296     }
297 
298     // validate parameters
299     // AudioFlinger capture only supports linear PCM
300     if (!audio_is_valid_format(format) || !audio_is_linear_pcm(format)) {
301         ALOGE("%s(): Format %#x is not linear pcm", __func__, format);
302         status = BAD_VALUE;
303         goto exit;
304     }
305     mFormat = format;
306 
307     if (!audio_is_input_channel(channelMask)) {
308         ALOGE("%s(): Invalid channel mask %#x", __func__, channelMask);
309         status = BAD_VALUE;
310         goto exit;
311     }
312     mChannelMask = channelMask;
313     channelCount = audio_channel_count_from_in_mask(channelMask);
314     mChannelCount = channelCount;
315 
316     if (audio_is_linear_pcm(format)) {
317         mFrameSize = channelCount * audio_bytes_per_sample(format);
318     } else {
319         mFrameSize = sizeof(uint8_t);
320     }
321 
322     // mFrameCount is initialized in createRecord_l
323     mReqFrameCount = frameCount;
324 
325     mNotificationFramesReq = notificationFrames;
326     // mNotificationFramesAct is initialized in createRecord_l
327 
328     mSessionId = sessionId;
329     ALOGV("%s(): mSessionId %d", __func__, mSessionId);
330 
331     callingPid = IPCThreadState::self()->getCallingPid();
332     myPid = getpid();
333     if (uid == AUDIO_UID_INVALID || (callingPid != myPid)) {
334         mClientUid = IPCThreadState::self()->getCallingUid();
335     } else {
336         mClientUid = uid;
337     }
338     if (pid == -1 || (callingPid != myPid)) {
339         mClientPid = callingPid;
340     } else {
341         mClientPid = pid;
342     }
343 
344     mOrigFlags = mFlags = flags;
345     mCbf = cbf;
346 
347     if (cbf != NULL) {
348         mAudioRecordThread = new AudioRecordThread(*this);
349         mAudioRecordThread->run("AudioRecord", ANDROID_PRIORITY_AUDIO);
350         // thread begins in paused state, and will not reference us until start()
351     }
352 
353     // create the IAudioRecord
354     {
355         AutoMutex lock(mLock);
356         status = createRecord_l(0 /*epoch*/, mOpPackageName);
357     }
358 
359     ALOGV("%s(%d): status %d", __func__, mPortId, status);
360 
361     if (status != NO_ERROR) {
362         if (mAudioRecordThread != 0) {
363             mAudioRecordThread->requestExit();   // see comment in AudioRecord.h
364             mAudioRecordThread->requestExitAndWait();
365             mAudioRecordThread.clear();
366         }
367         goto exit;
368     }
369 
370     mUserData = user;
371     // TODO: add audio hardware input latency here
372     mLatency = (1000LL * mFrameCount) / mSampleRate;
373     mMarkerPosition = 0;
374     mMarkerReached = false;
375     mNewPosition = 0;
376     mUpdatePeriod = 0;
377     AudioSystem::acquireAudioSessionId(mSessionId, mClientPid, mClientUid);
378     mSequence = 1;
379     mObservedSequence = mSequence;
380     mInOverrun = false;
381     mFramesRead = 0;
382     mFramesReadServerOffset = 0;
383 
384 exit:
385     mStatus = status;
386     if (status != NO_ERROR) {
387         mMediaMetrics.markError(status, __FUNCTION__);
388     }
389     return status;
390 }
391 
392 // -------------------------------------------------------------------------
393 
start(AudioSystem::sync_event_t event,audio_session_t triggerSession)394 status_t AudioRecord::start(AudioSystem::sync_event_t event, audio_session_t triggerSession)
395 {
396     const int64_t beginNs = systemTime();
397     ALOGV("%s(%d): sync event %d trigger session %d", __func__, mPortId, event, triggerSession);
398     AutoMutex lock(mLock);
399 
400     status_t status = NO_ERROR;
401     mediametrics::Defer defer([&] {
402         mediametrics::LogItem(mMetricsId)
403             .set(AMEDIAMETRICS_PROP_CALLERNAME,
404                     mCallerName.empty()
405                     ? AMEDIAMETRICS_PROP_CALLERNAME_VALUE_UNKNOWN
406                     : mCallerName.c_str())
407             .set(AMEDIAMETRICS_PROP_EVENT, AMEDIAMETRICS_PROP_EVENT_VALUE_START)
408             .set(AMEDIAMETRICS_PROP_EXECUTIONTIMENS, (int64_t)(systemTime() - beginNs))
409             .set(AMEDIAMETRICS_PROP_STATE, stateToString(mActive))
410             .set(AMEDIAMETRICS_PROP_STATUS, (int32_t)status)
411             .record(); });
412 
413     if (mActive) {
414         return status;
415     }
416 
417     // discard data in buffer
418     const uint32_t framesFlushed = mProxy->flush();
419     mFramesReadServerOffset -= mFramesRead + framesFlushed;
420     mFramesRead = 0;
421     mProxy->clearTimestamp();  // timestamp is invalid until next server push
422     mPreviousTimestamp.clear();
423     mTimestampRetrogradePositionReported = false;
424     mTimestampRetrogradeTimeReported = false;
425 
426     // reset current position as seen by client to 0
427     mProxy->setEpoch(mProxy->getEpoch() - mProxy->getPosition());
428     // force refresh of remaining frames by processAudioBuffer() as last
429     // read before stop could be partial.
430     mRefreshRemaining = true;
431 
432     mNewPosition = mProxy->getPosition() + mUpdatePeriod;
433     int32_t flags = android_atomic_acquire_load(&mCblk->mFlags);
434 
435     // we reactivate markers (mMarkerPosition != 0) as the position is reset to 0.
436     // This is legacy behavior.  This is not done in stop() to avoid a race condition
437     // where the last marker event is issued twice.
438     mMarkerReached = false;
439     // mActive is checked by restoreRecord_l
440     mActive = true;
441 
442     if (!(flags & CBLK_INVALID)) {
443         status = mAudioRecord->start(event, triggerSession).transactionError();
444         if (status == DEAD_OBJECT) {
445             flags |= CBLK_INVALID;
446         }
447     }
448     if (flags & CBLK_INVALID) {
449         status = restoreRecord_l("start");
450     }
451 
452     // Call these directly because we are already holding the lock.
453     mAudioRecord->setPreferredMicrophoneDirection(mSelectedMicDirection);
454     mAudioRecord->setPreferredMicrophoneFieldDimension(mSelectedMicFieldDimension);
455 
456     if (status != NO_ERROR) {
457         mActive = false;
458         ALOGE("%s(%d): status %d", __func__, mPortId, status);
459         mMediaMetrics.markError(status, __FUNCTION__);
460     } else {
461         mTracker->recordingStarted();
462         sp<AudioRecordThread> t = mAudioRecordThread;
463         if (t != 0) {
464             t->resume();
465         } else {
466             mPreviousPriority = getpriority(PRIO_PROCESS, 0);
467             get_sched_policy(0, &mPreviousSchedulingGroup);
468             androidSetThreadPriority(0, ANDROID_PRIORITY_AUDIO);
469         }
470 
471         // we've successfully started, log that time
472         mMediaMetrics.logStart(systemTime());
473     }
474     return status;
475 }
476 
stop()477 void AudioRecord::stop()
478 {
479     const int64_t beginNs = systemTime();
480     AutoMutex lock(mLock);
481     mediametrics::Defer defer([&] {
482         mediametrics::LogItem(mMetricsId)
483             .set(AMEDIAMETRICS_PROP_EVENT, AMEDIAMETRICS_PROP_EVENT_VALUE_STOP)
484             .set(AMEDIAMETRICS_PROP_EXECUTIONTIMENS, (int64_t)(systemTime() - beginNs))
485             .set(AMEDIAMETRICS_PROP_STATE, stateToString(mActive))
486             .record(); });
487 
488     ALOGV("%s(%d): mActive:%d\n", __func__, mPortId, mActive);
489     if (!mActive) {
490         return;
491     }
492 
493     mActive = false;
494     mProxy->interrupt();
495     mAudioRecord->stop();
496     mTracker->recordingStopped();
497 
498     // Note: legacy handling - stop does not clear record marker and
499     // periodic update position; we update those on start().
500 
501     sp<AudioRecordThread> t = mAudioRecordThread;
502     if (t != 0) {
503         t->pause();
504     } else {
505         setpriority(PRIO_PROCESS, 0, mPreviousPriority);
506         set_sched_policy(0, mPreviousSchedulingGroup);
507     }
508 
509     // we've successfully started, log that time
510     mMediaMetrics.logStop(systemTime());
511 }
512 
stopped() const513 bool AudioRecord::stopped() const
514 {
515     AutoMutex lock(mLock);
516     return !mActive;
517 }
518 
setMarkerPosition(uint32_t marker)519 status_t AudioRecord::setMarkerPosition(uint32_t marker)
520 {
521     // The only purpose of setting marker position is to get a callback
522     if (mCbf == NULL) {
523         return INVALID_OPERATION;
524     }
525 
526     AutoMutex lock(mLock);
527     mMarkerPosition = marker;
528     mMarkerReached = false;
529 
530     sp<AudioRecordThread> t = mAudioRecordThread;
531     if (t != 0) {
532         t->wake();
533     }
534     return NO_ERROR;
535 }
536 
getMarkerPosition(uint32_t * marker) const537 status_t AudioRecord::getMarkerPosition(uint32_t *marker) const
538 {
539     if (marker == NULL) {
540         return BAD_VALUE;
541     }
542 
543     AutoMutex lock(mLock);
544     mMarkerPosition.getValue(marker);
545 
546     return NO_ERROR;
547 }
548 
setPositionUpdatePeriod(uint32_t updatePeriod)549 status_t AudioRecord::setPositionUpdatePeriod(uint32_t updatePeriod)
550 {
551     // The only purpose of setting position update period is to get a callback
552     if (mCbf == NULL) {
553         return INVALID_OPERATION;
554     }
555 
556     AutoMutex lock(mLock);
557     mNewPosition = mProxy->getPosition() + updatePeriod;
558     mUpdatePeriod = updatePeriod;
559 
560     sp<AudioRecordThread> t = mAudioRecordThread;
561     if (t != 0) {
562         t->wake();
563     }
564     return NO_ERROR;
565 }
566 
getPositionUpdatePeriod(uint32_t * updatePeriod) const567 status_t AudioRecord::getPositionUpdatePeriod(uint32_t *updatePeriod) const
568 {
569     if (updatePeriod == NULL) {
570         return BAD_VALUE;
571     }
572 
573     AutoMutex lock(mLock);
574     *updatePeriod = mUpdatePeriod;
575 
576     return NO_ERROR;
577 }
578 
getPosition(uint32_t * position) const579 status_t AudioRecord::getPosition(uint32_t *position) const
580 {
581     if (position == NULL) {
582         return BAD_VALUE;
583     }
584 
585     AutoMutex lock(mLock);
586     mProxy->getPosition().getValue(position);
587 
588     return NO_ERROR;
589 }
590 
getInputFramesLost() const591 uint32_t AudioRecord::getInputFramesLost() const
592 {
593     // no need to check mActive, because if inactive this will return 0, which is what we want
594     return AudioSystem::getInputFramesLost(getInputPrivate());
595 }
596 
getTimestamp(ExtendedTimestamp * timestamp)597 status_t AudioRecord::getTimestamp(ExtendedTimestamp *timestamp)
598 {
599     if (timestamp == nullptr) {
600         return BAD_VALUE;
601     }
602     AutoMutex lock(mLock);
603     status_t status = mProxy->getTimestamp(timestamp);
604     if (status == OK) {
605         timestamp->mPosition[ExtendedTimestamp::LOCATION_CLIENT] = mFramesRead;
606         timestamp->mTimeNs[ExtendedTimestamp::LOCATION_CLIENT] = 0;
607         // server side frame offset in case AudioRecord has been restored.
608         for (int i = ExtendedTimestamp::LOCATION_SERVER;
609                 i < ExtendedTimestamp::LOCATION_MAX; ++i) {
610             if (timestamp->mTimeNs[i] >= 0) {
611                 timestamp->mPosition[i] += mFramesReadServerOffset;
612             }
613         }
614 
615         bool timestampRetrogradeTimeReported = false;
616         bool timestampRetrogradePositionReported = false;
617         for (int i = 0; i < ExtendedTimestamp::LOCATION_MAX; ++i) {
618             if (timestamp->mTimeNs[i] >= 0 && mPreviousTimestamp.mTimeNs[i] >= 0) {
619                 if (timestamp->mTimeNs[i] < mPreviousTimestamp.mTimeNs[i]) {
620                     if (!mTimestampRetrogradeTimeReported) {
621                         ALOGD("%s: retrograde time adjusting [%d] current:%lld to previous:%lld",
622                                 __func__, i, (long long)timestamp->mTimeNs[i],
623                                 (long long)mPreviousTimestamp.mTimeNs[i]);
624                         timestampRetrogradeTimeReported = true;
625                     }
626                     timestamp->mTimeNs[i] = mPreviousTimestamp.mTimeNs[i];
627                 }
628                 if (timestamp->mPosition[i] < mPreviousTimestamp.mPosition[i]) {
629                     if (!mTimestampRetrogradePositionReported) {
630                         ALOGD("%s: retrograde position"
631                                 " adjusting [%d] current:%lld to previous:%lld",
632                                 __func__, i, (long long)timestamp->mPosition[i],
633                                 (long long)mPreviousTimestamp.mPosition[i]);
634                         timestampRetrogradePositionReported = true;
635                     }
636                     timestamp->mPosition[i] = mPreviousTimestamp.mPosition[i];
637                 }
638             }
639         }
640         mPreviousTimestamp = *timestamp;
641         if (timestampRetrogradeTimeReported) {
642             mTimestampRetrogradeTimeReported = true;
643         }
644         if (timestampRetrogradePositionReported) {
645             mTimestampRetrogradePositionReported = true;
646         }
647     }
648     return status;
649 }
650 
651 // ---- Explicit Routing ---------------------------------------------------
setInputDevice(audio_port_handle_t deviceId)652 status_t AudioRecord::setInputDevice(audio_port_handle_t deviceId) {
653     AutoMutex lock(mLock);
654     if (mSelectedDeviceId != deviceId) {
655         mSelectedDeviceId = deviceId;
656         if (mStatus == NO_ERROR) {
657             // stop capture so that audio policy manager does not reject the new instance start request
658             // as only one capture can be active at a time.
659             if (mAudioRecord != 0 && mActive) {
660                 mAudioRecord->stop();
661             }
662             android_atomic_or(CBLK_INVALID, &mCblk->mFlags);
663             mProxy->interrupt();
664         }
665     }
666     return NO_ERROR;
667 }
668 
getInputDevice()669 audio_port_handle_t AudioRecord::getInputDevice() {
670     AutoMutex lock(mLock);
671     return mSelectedDeviceId;
672 }
673 
674 // must be called with mLock held
updateRoutedDeviceId_l()675 void AudioRecord::updateRoutedDeviceId_l()
676 {
677     // if the record is inactive, do not update actual device as the input stream maybe routed
678     // from a device not relevant to this client because of other active use cases.
679     if (!mActive) {
680         return;
681     }
682     if (mInput != AUDIO_IO_HANDLE_NONE) {
683         audio_port_handle_t deviceId = AudioSystem::getDeviceIdForIo(mInput);
684         if (deviceId != AUDIO_PORT_HANDLE_NONE) {
685             mRoutedDeviceId = deviceId;
686         }
687      }
688 }
689 
getRoutedDeviceId()690 audio_port_handle_t AudioRecord::getRoutedDeviceId() {
691     AutoMutex lock(mLock);
692     updateRoutedDeviceId_l();
693     return mRoutedDeviceId;
694 }
695 
dump(int fd,const Vector<String16> & args __unused) const696 status_t AudioRecord::dump(int fd, const Vector<String16>& args __unused) const
697 {
698     String8 result;
699 
700     result.append(" AudioRecord::dump\n");
701     result.appendFormat("  id(%d) status(%d), active(%d), session Id(%d)\n",
702                         mPortId, mStatus, mActive, mSessionId);
703     result.appendFormat("  flags(%#x), req. flags(%#x), audio source(%d)\n",
704                         mFlags, mOrigFlags, mAttributes.source);
705     result.appendFormat("  format(%#x), channel mask(%#x), channel count(%u), sample rate(%u)\n",
706                   mFormat, mChannelMask, mChannelCount, mSampleRate);
707     result.appendFormat("  frame count(%zu), req. frame count(%zu)\n",
708                   mFrameCount, mReqFrameCount);
709     result.appendFormat("  notif. frame count(%u), req. notif. frame count(%u)\n",
710              mNotificationFramesAct, mNotificationFramesReq);
711     result.appendFormat("  input(%d), latency(%u), selected device Id(%d), routed device Id(%d)\n",
712                         mInput, mLatency, mSelectedDeviceId, mRoutedDeviceId);
713     result.appendFormat("  mic direction(%d) mic field dimension(%f)",
714                         mSelectedMicDirection, mSelectedMicFieldDimension);
715     ::write(fd, result.string(), result.size());
716     return NO_ERROR;
717 }
718 
719 // -------------------------------------------------------------------------
720 // TODO Move this macro to a common header file for enum to string conversion in audio framework.
721 #define MEDIA_CASE_ENUM(name) case name: return #name
convertTransferToText(transfer_type transferType)722 const char * AudioRecord::convertTransferToText(transfer_type transferType) {
723     switch (transferType) {
724         MEDIA_CASE_ENUM(TRANSFER_DEFAULT);
725         MEDIA_CASE_ENUM(TRANSFER_CALLBACK);
726         MEDIA_CASE_ENUM(TRANSFER_OBTAIN);
727         MEDIA_CASE_ENUM(TRANSFER_SYNC);
728         default:
729             return "UNRECOGNIZED";
730     }
731 }
732 
733 // must be called with mLock held
createRecord_l(const Modulo<uint32_t> & epoch,const String16 & opPackageName)734 status_t AudioRecord::createRecord_l(const Modulo<uint32_t> &epoch, const String16& opPackageName)
735 {
736     const int64_t beginNs = systemTime();
737     const sp<IAudioFlinger>& audioFlinger = AudioSystem::get_audio_flinger();
738     IAudioFlinger::CreateRecordInput input;
739     IAudioFlinger::CreateRecordOutput output;
740     audio_session_t originalSessionId;
741     sp<media::IAudioRecord> record;
742     void *iMemPointer;
743     audio_track_cblk_t* cblk;
744     status_t status;
745 
746     if (audioFlinger == 0) {
747         ALOGE("%s(%d): Could not get audioflinger", __func__, mPortId);
748         status = NO_INIT;
749         goto exit;
750     }
751 
752     // mFlags (not mOrigFlags) is modified depending on whether fast request is accepted.
753     // After fast request is denied, we will request again if IAudioRecord is re-created.
754 
755     // Now that we have a reference to an I/O handle and have not yet handed it off to AudioFlinger,
756     // we must release it ourselves if anything goes wrong.
757 
758     // Client can only express a preference for FAST.  Server will perform additional tests.
759     if (mFlags & AUDIO_INPUT_FLAG_FAST) {
760         bool useCaseAllowed =
761             // any of these use cases:
762             // use case 1: callback transfer mode
763             (mTransfer == TRANSFER_CALLBACK) ||
764             // use case 2: blocking read mode
765             // The default buffer capacity at 48 kHz is 2048 frames, or ~42.6 ms.
766             // That's enough for double-buffering with our standard 20 ms rule of thumb for
767             // the minimum period of a non-SCHED_FIFO thread.
768             // This is needed so that AAudio apps can do a low latency non-blocking read from a
769             // callback running with SCHED_FIFO.
770             (mTransfer == TRANSFER_SYNC) ||
771             // use case 3: obtain/release mode
772             (mTransfer == TRANSFER_OBTAIN);
773         if (!useCaseAllowed) {
774             ALOGD("%s(%d): AUDIO_INPUT_FLAG_FAST denied, incompatible transfer = %s",
775                   __func__, mPortId,
776                   convertTransferToText(mTransfer));
777             mFlags = (audio_input_flags_t) (mFlags & ~(AUDIO_INPUT_FLAG_FAST |
778                     AUDIO_INPUT_FLAG_RAW));
779         }
780     }
781 
782     input.attr = mAttributes;
783     input.config.sample_rate = mSampleRate;
784     input.config.channel_mask = mChannelMask;
785     input.config.format = mFormat;
786     input.clientInfo.clientUid = mClientUid;
787     input.clientInfo.clientPid = mClientPid;
788     input.clientInfo.clientTid = -1;
789     if (mFlags & AUDIO_INPUT_FLAG_FAST) {
790         if (mAudioRecordThread != 0) {
791             input.clientInfo.clientTid = mAudioRecordThread->getTid();
792         }
793     }
794     input.opPackageName = opPackageName;
795     input.riid = mTracker->getRiid();
796 
797     input.flags = mFlags;
798     // The notification frame count is the period between callbacks, as suggested by the client
799     // but moderated by the server.  For record, the calculations are done entirely on server side.
800     input.frameCount = mReqFrameCount;
801     input.notificationFrameCount = mNotificationFramesReq;
802     input.selectedDeviceId = mSelectedDeviceId;
803     input.sessionId = mSessionId;
804     originalSessionId = mSessionId;
805 
806     record = audioFlinger->createRecord(input,
807                                                               output,
808                                                               &status);
809 
810     if (status != NO_ERROR) {
811         ALOGE("%s(%d): AudioFlinger could not create record track, status: %d",
812               __func__, mPortId, status);
813         goto exit;
814     }
815     ALOG_ASSERT(record != 0);
816 
817     // AudioFlinger now owns the reference to the I/O handle,
818     // so we are no longer responsible for releasing it.
819 
820     mAwaitBoost = false;
821     if (output.flags & AUDIO_INPUT_FLAG_FAST) {
822         ALOGI("%s(%d): AUDIO_INPUT_FLAG_FAST successful; frameCount %zu -> %zu",
823               __func__, mPortId,
824               mReqFrameCount, output.frameCount);
825         mAwaitBoost = true;
826     }
827     mFlags = output.flags;
828     mRoutedDeviceId = output.selectedDeviceId;
829     mSessionId = output.sessionId;
830     mSampleRate = output.sampleRate;
831 
832     if (output.cblk == 0) {
833         ALOGE("%s(%d): Could not get control block", __func__, mPortId);
834         status = NO_INIT;
835         goto exit;
836     }
837     // TODO: Using unsecurePointer() has some associated security pitfalls
838     //       (see declaration for details).
839     //       Either document why it is safe in this case or address the
840     //       issue (e.g. by copying).
841     iMemPointer = output.cblk ->unsecurePointer();
842     if (iMemPointer == NULL) {
843         ALOGE("%s(%d): Could not get control block pointer", __func__, mPortId);
844         status = NO_INIT;
845         goto exit;
846     }
847     cblk = static_cast<audio_track_cblk_t*>(iMemPointer);
848 
849     // Starting address of buffers in shared memory.
850     // The buffers are either immediately after the control block,
851     // or in a separate area at discretion of server.
852     void *buffers;
853     if (output.buffers == 0) {
854         buffers = cblk + 1;
855     } else {
856         // TODO: Using unsecurePointer() has some associated security pitfalls
857         //       (see declaration for details).
858         //       Either document why it is safe in this case or address the
859         //       issue (e.g. by copying).
860         buffers = output.buffers->unsecurePointer();
861         if (buffers == NULL) {
862             ALOGE("%s(%d): Could not get buffer pointer", __func__, mPortId);
863             status = NO_INIT;
864             goto exit;
865         }
866     }
867 
868     // invariant that mAudioRecord != 0 is true only after set() returns successfully
869     if (mAudioRecord != 0) {
870         IInterface::asBinder(mAudioRecord)->unlinkToDeath(mDeathNotifier, this);
871         mDeathNotifier.clear();
872     }
873     mAudioRecord = record;
874     mCblkMemory = output.cblk;
875     mBufferMemory = output.buffers;
876     IPCThreadState::self()->flushCommands();
877 
878     mCblk = cblk;
879     // note that output.frameCount is the (possibly revised) value of mReqFrameCount
880     if (output.frameCount < mReqFrameCount || (mReqFrameCount == 0 && output.frameCount == 0)) {
881         ALOGW("%s(%d): Requested frameCount %zu but received frameCount %zu",
882               __func__, output.portId,
883               mReqFrameCount,  output.frameCount);
884     }
885 
886     // Make sure that application is notified with sufficient margin before overrun.
887     // The computation is done on server side.
888     if (mNotificationFramesReq > 0 && output.notificationFrameCount != mNotificationFramesReq) {
889         ALOGW("%s(%d): Server adjusted notificationFrames from %u to %zu for frameCount %zu",
890                 __func__, output.portId,
891                 mNotificationFramesReq, output.notificationFrameCount, output.frameCount);
892     }
893     mNotificationFramesAct = (uint32_t)output.notificationFrameCount;
894 
895     //mInput != input includes the case where mInput == AUDIO_IO_HANDLE_NONE for first creation
896     if (mDeviceCallback != 0) {
897         if (mInput != AUDIO_IO_HANDLE_NONE) {
898             AudioSystem::removeAudioDeviceCallback(this, mInput, mPortId);
899         }
900         AudioSystem::addAudioDeviceCallback(this, output.inputId, output.portId);
901     }
902 
903     mPortId = output.portId;
904     // We retain a copy of the I/O handle, but don't own the reference
905     mInput = output.inputId;
906     mRefreshRemaining = true;
907 
908     mFrameCount = output.frameCount;
909     // If IAudioRecord is re-created, don't let the requested frameCount
910     // decrease.  This can confuse clients that cache frameCount().
911     if (mFrameCount > mReqFrameCount) {
912         mReqFrameCount = mFrameCount;
913     }
914 
915     // update proxy
916     mProxy = new AudioRecordClientProxy(cblk, buffers, mFrameCount, mFrameSize);
917     mProxy->setEpoch(epoch);
918     mProxy->setMinimum(mNotificationFramesAct);
919 
920     mDeathNotifier = new DeathNotifier(this);
921     IInterface::asBinder(mAudioRecord)->linkToDeath(mDeathNotifier, this);
922 
923     mMetricsId = std::string(AMEDIAMETRICS_KEY_PREFIX_AUDIO_RECORD) + std::to_string(mPortId);
924     mediametrics::LogItem(mMetricsId)
925         .set(AMEDIAMETRICS_PROP_EVENT, AMEDIAMETRICS_PROP_EVENT_VALUE_CREATE)
926         .set(AMEDIAMETRICS_PROP_EXECUTIONTIMENS, (int64_t)(systemTime() - beginNs))
927         // the following are immutable (at least until restore)
928         .set(AMEDIAMETRICS_PROP_FLAGS, toString(mFlags).c_str())
929         .set(AMEDIAMETRICS_PROP_ORIGINALFLAGS, toString(mOrigFlags).c_str())
930         .set(AMEDIAMETRICS_PROP_SESSIONID, (int32_t)mSessionId)
931         .set(AMEDIAMETRICS_PROP_TRACKID, mPortId)
932         .set(AMEDIAMETRICS_PROP_SOURCE, toString(mAttributes.source).c_str())
933         .set(AMEDIAMETRICS_PROP_THREADID, (int32_t)output.inputId)
934         .set(AMEDIAMETRICS_PROP_SELECTEDDEVICEID, (int32_t)mSelectedDeviceId)
935         .set(AMEDIAMETRICS_PROP_ROUTEDDEVICEID, (int32_t)mRoutedDeviceId)
936         .set(AMEDIAMETRICS_PROP_ENCODING, toString(mFormat).c_str())
937         .set(AMEDIAMETRICS_PROP_CHANNELMASK, (int32_t)mChannelMask)
938         .set(AMEDIAMETRICS_PROP_FRAMECOUNT, (int32_t)mFrameCount)
939         .set(AMEDIAMETRICS_PROP_SAMPLERATE, (int32_t)mSampleRate)
940         // the following are NOT immutable
941         .set(AMEDIAMETRICS_PROP_STATE, stateToString(mActive))
942         .set(AMEDIAMETRICS_PROP_SELECTEDMICDIRECTION, (int32_t)mSelectedMicDirection)
943         .set(AMEDIAMETRICS_PROP_SELECTEDMICFIELDDIRECTION, (double)mSelectedMicFieldDimension)
944         .record();
945 
946 exit:
947     mStatus = status;
948     // sp<IAudioTrack> track destructor will cause releaseOutput() to be called by AudioFlinger
949     return status;
950 }
951 
obtainBuffer(Buffer * audioBuffer,int32_t waitCount,size_t * nonContig)952 status_t AudioRecord::obtainBuffer(Buffer* audioBuffer, int32_t waitCount, size_t *nonContig)
953 {
954     if (audioBuffer == NULL) {
955         if (nonContig != NULL) {
956             *nonContig = 0;
957         }
958         return BAD_VALUE;
959     }
960     if (mTransfer != TRANSFER_OBTAIN) {
961         audioBuffer->frameCount = 0;
962         audioBuffer->size = 0;
963         audioBuffer->raw = NULL;
964         if (nonContig != NULL) {
965             *nonContig = 0;
966         }
967         return INVALID_OPERATION;
968     }
969 
970     const struct timespec *requested;
971     struct timespec timeout;
972     if (waitCount == -1) {
973         requested = &ClientProxy::kForever;
974     } else if (waitCount == 0) {
975         requested = &ClientProxy::kNonBlocking;
976     } else if (waitCount > 0) {
977         time_t ms = WAIT_PERIOD_MS * (time_t) waitCount;
978         timeout.tv_sec = ms / 1000;
979         timeout.tv_nsec = (long) (ms % 1000) * 1000000;
980         requested = &timeout;
981     } else {
982         ALOGE("%s(%d): invalid waitCount %d", __func__, mPortId, waitCount);
983         requested = NULL;
984     }
985     return obtainBuffer(audioBuffer, requested, NULL /*elapsed*/, nonContig);
986 }
987 
obtainBuffer(Buffer * audioBuffer,const struct timespec * requested,struct timespec * elapsed,size_t * nonContig)988 status_t AudioRecord::obtainBuffer(Buffer* audioBuffer, const struct timespec *requested,
989         struct timespec *elapsed, size_t *nonContig)
990 {
991     // previous and new IAudioRecord sequence numbers are used to detect track re-creation
992     uint32_t oldSequence = 0;
993 
994     Proxy::Buffer buffer;
995     status_t status = NO_ERROR;
996 
997     static const int32_t kMaxTries = 5;
998     int32_t tryCounter = kMaxTries;
999 
1000     do {
1001         // obtainBuffer() is called with mutex unlocked, so keep extra references to these fields to
1002         // keep them from going away if another thread re-creates the track during obtainBuffer()
1003         sp<AudioRecordClientProxy> proxy;
1004         sp<IMemory> iMem;
1005         sp<IMemory> bufferMem;
1006         {
1007             // start of lock scope
1008             AutoMutex lock(mLock);
1009 
1010             uint32_t newSequence = mSequence;
1011             // did previous obtainBuffer() fail due to media server death or voluntary invalidation?
1012             if (status == DEAD_OBJECT) {
1013                 // re-create track, unless someone else has already done so
1014                 if (newSequence == oldSequence) {
1015                     status = restoreRecord_l("obtainBuffer");
1016                     if (status != NO_ERROR) {
1017                         buffer.mFrameCount = 0;
1018                         buffer.mRaw = NULL;
1019                         buffer.mNonContig = 0;
1020                         break;
1021                     }
1022                 }
1023             }
1024             oldSequence = newSequence;
1025 
1026             // Keep the extra references
1027             proxy = mProxy;
1028             iMem = mCblkMemory;
1029             bufferMem = mBufferMemory;
1030 
1031             // Non-blocking if track is stopped
1032             if (!mActive) {
1033                 requested = &ClientProxy::kNonBlocking;
1034             }
1035 
1036         }   // end of lock scope
1037 
1038         buffer.mFrameCount = audioBuffer->frameCount;
1039         // FIXME starts the requested timeout and elapsed over from scratch
1040         status = proxy->obtainBuffer(&buffer, requested, elapsed);
1041 
1042     } while ((status == DEAD_OBJECT) && (tryCounter-- > 0));
1043 
1044     audioBuffer->frameCount = buffer.mFrameCount;
1045     audioBuffer->size = buffer.mFrameCount * mFrameSize;
1046     audioBuffer->raw = buffer.mRaw;
1047     audioBuffer->sequence = oldSequence;
1048     if (nonContig != NULL) {
1049         *nonContig = buffer.mNonContig;
1050     }
1051     return status;
1052 }
1053 
releaseBuffer(const Buffer * audioBuffer)1054 void AudioRecord::releaseBuffer(const Buffer* audioBuffer)
1055 {
1056     // FIXME add error checking on mode, by adding an internal version
1057 
1058     size_t stepCount = audioBuffer->size / mFrameSize;
1059     if (stepCount == 0) {
1060         return;
1061     }
1062 
1063     Proxy::Buffer buffer;
1064     buffer.mFrameCount = stepCount;
1065     buffer.mRaw = audioBuffer->raw;
1066 
1067     AutoMutex lock(mLock);
1068     if (audioBuffer->sequence != mSequence) {
1069         // This Buffer came from a different IAudioRecord instance, so ignore the releaseBuffer
1070         ALOGD("%s is no-op due to IAudioRecord sequence mismatch %u != %u",
1071                 __func__, audioBuffer->sequence, mSequence);
1072         return;
1073     }
1074     mInOverrun = false;
1075     mProxy->releaseBuffer(&buffer);
1076 
1077     // the server does not automatically disable recorder on overrun, so no need to restart
1078 }
1079 
getInputPrivate() const1080 audio_io_handle_t AudioRecord::getInputPrivate() const
1081 {
1082     AutoMutex lock(mLock);
1083     return mInput;
1084 }
1085 
1086 // -------------------------------------------------------------------------
1087 
read(void * buffer,size_t userSize,bool blocking)1088 ssize_t AudioRecord::read(void* buffer, size_t userSize, bool blocking)
1089 {
1090     if (mTransfer != TRANSFER_SYNC) {
1091         return INVALID_OPERATION;
1092     }
1093 
1094     if (ssize_t(userSize) < 0 || (buffer == NULL && userSize != 0)) {
1095         // sanity-check. user is most-likely passing an error code, and it would
1096         // make the return value ambiguous (actualSize vs error).
1097         ALOGE("%s(%d) (buffer=%p, size=%zu (%zu)",
1098                 __func__, mPortId, buffer, userSize, userSize);
1099         return BAD_VALUE;
1100     }
1101 
1102     ssize_t read = 0;
1103     Buffer audioBuffer;
1104 
1105     while (userSize >= mFrameSize) {
1106         audioBuffer.frameCount = userSize / mFrameSize;
1107 
1108         status_t err = obtainBuffer(&audioBuffer,
1109                 blocking ? &ClientProxy::kForever : &ClientProxy::kNonBlocking);
1110         if (err < 0) {
1111             if (read > 0) {
1112                 break;
1113             }
1114             if (err == TIMED_OUT || err == -EINTR) {
1115                 err = WOULD_BLOCK;
1116             }
1117             return ssize_t(err);
1118         }
1119 
1120         size_t bytesRead = audioBuffer.size;
1121         memcpy(buffer, audioBuffer.i8, bytesRead);
1122         buffer = ((char *) buffer) + bytesRead;
1123         userSize -= bytesRead;
1124         read += bytesRead;
1125 
1126         releaseBuffer(&audioBuffer);
1127     }
1128     if (read > 0) {
1129         mFramesRead += read / mFrameSize;
1130         // mFramesReadTime = systemTime(SYSTEM_TIME_MONOTONIC); // not provided at this time.
1131     }
1132     return read;
1133 }
1134 
1135 // -------------------------------------------------------------------------
1136 
processAudioBuffer()1137 nsecs_t AudioRecord::processAudioBuffer()
1138 {
1139     mLock.lock();
1140     if (mAwaitBoost) {
1141         mAwaitBoost = false;
1142         mLock.unlock();
1143         static const int32_t kMaxTries = 5;
1144         int32_t tryCounter = kMaxTries;
1145         uint32_t pollUs = 10000;
1146         do {
1147             int policy = sched_getscheduler(0) & ~SCHED_RESET_ON_FORK;
1148             if (policy == SCHED_FIFO || policy == SCHED_RR) {
1149                 break;
1150             }
1151             usleep(pollUs);
1152             pollUs <<= 1;
1153         } while (tryCounter-- > 0);
1154         if (tryCounter < 0) {
1155             ALOGE("%s(%d): did not receive expected priority boost on time", __func__, mPortId);
1156         }
1157         // Run again immediately
1158         return 0;
1159     }
1160 
1161     // Can only reference mCblk while locked
1162     int32_t flags = android_atomic_and(~CBLK_OVERRUN, &mCblk->mFlags);
1163 
1164     // Check for track invalidation
1165     if (flags & CBLK_INVALID) {
1166         (void) restoreRecord_l("processAudioBuffer");
1167         mLock.unlock();
1168         // Run again immediately, but with a new IAudioRecord
1169         return 0;
1170     }
1171 
1172     bool active = mActive;
1173 
1174     // Manage overrun callback, must be done under lock to avoid race with releaseBuffer()
1175     bool newOverrun = false;
1176     if (flags & CBLK_OVERRUN) {
1177         if (!mInOverrun) {
1178             mInOverrun = true;
1179             newOverrun = true;
1180         }
1181     }
1182 
1183     // Get current position of server
1184     Modulo<uint32_t> position(mProxy->getPosition());
1185 
1186     // Manage marker callback
1187     bool markerReached = false;
1188     Modulo<uint32_t> markerPosition(mMarkerPosition);
1189     // FIXME fails for wraparound, need 64 bits
1190     if (!mMarkerReached && markerPosition.value() > 0 && position >= markerPosition) {
1191         mMarkerReached = markerReached = true;
1192     }
1193 
1194     // Determine the number of new position callback(s) that will be needed, while locked
1195     size_t newPosCount = 0;
1196     Modulo<uint32_t> newPosition(mNewPosition);
1197     uint32_t updatePeriod = mUpdatePeriod;
1198     // FIXME fails for wraparound, need 64 bits
1199     if (updatePeriod > 0 && position >= newPosition) {
1200         newPosCount = ((position - newPosition).value() / updatePeriod) + 1;
1201         mNewPosition += updatePeriod * newPosCount;
1202     }
1203 
1204     // Cache other fields that will be needed soon
1205     uint32_t notificationFrames = mNotificationFramesAct;
1206     if (mRefreshRemaining) {
1207         mRefreshRemaining = false;
1208         mRemainingFrames = notificationFrames;
1209         mRetryOnPartialBuffer = false;
1210     }
1211     size_t misalignment = mProxy->getMisalignment();
1212     uint32_t sequence = mSequence;
1213 
1214     // These fields don't need to be cached, because they are assigned only by set():
1215     //      mTransfer, mCbf, mUserData, mSampleRate, mFrameSize
1216 
1217     mLock.unlock();
1218 
1219     // perform callbacks while unlocked
1220     if (newOverrun) {
1221         mCbf(EVENT_OVERRUN, mUserData, NULL);
1222     }
1223     if (markerReached) {
1224         mCbf(EVENT_MARKER, mUserData, &markerPosition);
1225     }
1226     while (newPosCount > 0) {
1227         size_t temp = newPosition.value(); // FIXME size_t != uint32_t
1228         mCbf(EVENT_NEW_POS, mUserData, &temp);
1229         newPosition += updatePeriod;
1230         newPosCount--;
1231     }
1232     if (mObservedSequence != sequence) {
1233         mObservedSequence = sequence;
1234         mCbf(EVENT_NEW_IAUDIORECORD, mUserData, NULL);
1235     }
1236 
1237     // if inactive, then don't run me again until re-started
1238     if (!active) {
1239         return NS_INACTIVE;
1240     }
1241 
1242     // Compute the estimated time until the next timed event (position, markers)
1243     uint32_t minFrames = ~0;
1244     if (!markerReached && position < markerPosition) {
1245         minFrames = (markerPosition - position).value();
1246     }
1247     if (updatePeriod > 0) {
1248         uint32_t remaining = (newPosition - position).value();
1249         if (remaining < minFrames) {
1250             minFrames = remaining;
1251         }
1252     }
1253 
1254     // If > 0, poll periodically to recover from a stuck server.  A good value is 2.
1255     static const uint32_t kPoll = 0;
1256     if (kPoll > 0 && mTransfer == TRANSFER_CALLBACK && kPoll * notificationFrames < minFrames) {
1257         minFrames = kPoll * notificationFrames;
1258     }
1259 
1260     // Convert frame units to time units
1261     nsecs_t ns = NS_WHENEVER;
1262     if (minFrames != (uint32_t) ~0) {
1263         // This "fudge factor" avoids soaking CPU, and compensates for late progress by server
1264         static const nsecs_t kFudgeNs = 10000000LL; // 10 ms
1265         ns = ((minFrames * 1000000000LL) / mSampleRate) + kFudgeNs;
1266     }
1267 
1268     // If not supplying data by EVENT_MORE_DATA, then we're done
1269     if (mTransfer != TRANSFER_CALLBACK) {
1270         return ns;
1271     }
1272 
1273     struct timespec timeout;
1274     const struct timespec *requested = &ClientProxy::kForever;
1275     if (ns != NS_WHENEVER) {
1276         timeout.tv_sec = ns / 1000000000LL;
1277         timeout.tv_nsec = ns % 1000000000LL;
1278         ALOGV("%s(%d): timeout %ld.%03d",
1279                 __func__, mPortId, timeout.tv_sec, (int) timeout.tv_nsec / 1000000);
1280         requested = &timeout;
1281     }
1282 
1283     size_t readFrames = 0;
1284     while (mRemainingFrames > 0) {
1285 
1286         Buffer audioBuffer;
1287         audioBuffer.frameCount = mRemainingFrames;
1288         size_t nonContig;
1289         status_t err = obtainBuffer(&audioBuffer, requested, NULL, &nonContig);
1290         LOG_ALWAYS_FATAL_IF((err != NO_ERROR) != (audioBuffer.frameCount == 0),
1291                 "%s(%d): obtainBuffer() err=%d frameCount=%zu",
1292                 __func__, mPortId, err, audioBuffer.frameCount);
1293         requested = &ClientProxy::kNonBlocking;
1294         size_t avail = audioBuffer.frameCount + nonContig;
1295         ALOGV("%s(%d): obtainBuffer(%u) returned %zu = %zu + %zu err %d",
1296                 __func__, mPortId, mRemainingFrames, avail, audioBuffer.frameCount, nonContig, err);
1297         if (err != NO_ERROR) {
1298             if (err == TIMED_OUT || err == WOULD_BLOCK || err == -EINTR) {
1299                 break;
1300             }
1301             ALOGE("%s(%d): Error %d obtaining an audio buffer, giving up.",
1302                     __func__, mPortId, err);
1303             return NS_NEVER;
1304         }
1305 
1306         if (mRetryOnPartialBuffer) {
1307             mRetryOnPartialBuffer = false;
1308             if (avail < mRemainingFrames) {
1309                 int64_t myns = ((mRemainingFrames - avail) *
1310                         1100000000LL) / mSampleRate;
1311                 if (ns < 0 || myns < ns) {
1312                     ns = myns;
1313                 }
1314                 return ns;
1315             }
1316         }
1317 
1318         size_t reqSize = audioBuffer.size;
1319         mCbf(EVENT_MORE_DATA, mUserData, &audioBuffer);
1320         size_t readSize = audioBuffer.size;
1321 
1322         // Sanity check on returned size
1323         if (ssize_t(readSize) < 0 || readSize > reqSize) {
1324             ALOGE("%s(%d):  EVENT_MORE_DATA requested %zu bytes but callback returned %zd bytes",
1325                     __func__, mPortId, reqSize, ssize_t(readSize));
1326             return NS_NEVER;
1327         }
1328 
1329         if (readSize == 0) {
1330             // The callback is done consuming buffers
1331             // Keep this thread going to handle timed events and
1332             // still try to provide more data in intervals of WAIT_PERIOD_MS
1333             // but don't just loop and block the CPU, so wait
1334             return WAIT_PERIOD_MS * 1000000LL;
1335         }
1336 
1337         size_t releasedFrames = readSize / mFrameSize;
1338         audioBuffer.frameCount = releasedFrames;
1339         mRemainingFrames -= releasedFrames;
1340         if (misalignment >= releasedFrames) {
1341             misalignment -= releasedFrames;
1342         } else {
1343             misalignment = 0;
1344         }
1345 
1346         releaseBuffer(&audioBuffer);
1347         readFrames += releasedFrames;
1348 
1349         // FIXME here is where we would repeat EVENT_MORE_DATA again on same advanced buffer
1350         // if callback doesn't like to accept the full chunk
1351         if (readSize < reqSize) {
1352             continue;
1353         }
1354 
1355         // There could be enough non-contiguous frames available to satisfy the remaining request
1356         if (mRemainingFrames <= nonContig) {
1357             continue;
1358         }
1359 
1360 #if 0
1361         // This heuristic tries to collapse a series of EVENT_MORE_DATA that would total to a
1362         // sum <= notificationFrames.  It replaces that series by at most two EVENT_MORE_DATA
1363         // that total to a sum == notificationFrames.
1364         if (0 < misalignment && misalignment <= mRemainingFrames) {
1365             mRemainingFrames = misalignment;
1366             return (mRemainingFrames * 1100000000LL) / mSampleRate;
1367         }
1368 #endif
1369 
1370     }
1371     if (readFrames > 0) {
1372         AutoMutex lock(mLock);
1373         mFramesRead += readFrames;
1374         // mFramesReadTime = systemTime(SYSTEM_TIME_MONOTONIC); // not provided at this time.
1375     }
1376     mRemainingFrames = notificationFrames;
1377     mRetryOnPartialBuffer = true;
1378 
1379     // A lot has transpired since ns was calculated, so run again immediately and re-calculate
1380     return 0;
1381 }
1382 
restoreRecord_l(const char * from)1383 status_t AudioRecord::restoreRecord_l(const char *from)
1384 {
1385     status_t result = NO_ERROR;  // logged: make sure to set this before returning.
1386     const int64_t beginNs = systemTime();
1387     mediametrics::Defer defer([&] {
1388         mediametrics::LogItem(mMetricsId)
1389             .set(AMEDIAMETRICS_PROP_EVENT, AMEDIAMETRICS_PROP_EVENT_VALUE_RESTORE)
1390             .set(AMEDIAMETRICS_PROP_EXECUTIONTIMENS, (int64_t)(systemTime() - beginNs))
1391             .set(AMEDIAMETRICS_PROP_STATE, stateToString(mActive))
1392             .set(AMEDIAMETRICS_PROP_STATUS, (int32_t)result)
1393             .set(AMEDIAMETRICS_PROP_WHERE, from)
1394             .record(); });
1395 
1396     ALOGW("%s(%d): dead IAudioRecord, creating a new one from %s()", __func__, mPortId, from);
1397     ++mSequence;
1398 
1399     const int INITIAL_RETRIES = 3;
1400     int retries = INITIAL_RETRIES;
1401 retry:
1402     if (retries < INITIAL_RETRIES) {
1403         // refresh the audio configuration cache in this process to make sure we get new
1404         // input parameters and new IAudioRecord in createRecord_l()
1405         AudioSystem::clearAudioConfigCache();
1406     }
1407     mFlags = mOrigFlags;
1408 
1409     // if the new IAudioRecord is created, createRecord_l() will modify the
1410     // following member variables: mAudioRecord, mCblkMemory, mCblk, mBufferMemory.
1411     // It will also delete the strong references on previous IAudioRecord and IMemory
1412     Modulo<uint32_t> position(mProxy->getPosition());
1413     mNewPosition = position + mUpdatePeriod;
1414     result = createRecord_l(position, mOpPackageName);
1415 
1416     if (result == NO_ERROR) {
1417         if (mActive) {
1418             // callback thread or sync event hasn't changed
1419             // FIXME this fails if we have a new AudioFlinger instance
1420             result = mAudioRecord->start(
1421                 AudioSystem::SYNC_EVENT_SAME, AUDIO_SESSION_NONE).transactionError();
1422         }
1423         mFramesReadServerOffset = mFramesRead; // server resets to zero so we need an offset.
1424     }
1425 
1426     if (result != NO_ERROR) {
1427         ALOGW("%s(%d): failed status %d, retries %d", __func__, mPortId, result, retries);
1428         if (--retries > 0) {
1429             // leave time for an eventual race condition to clear before retrying
1430             usleep(500000);
1431             goto retry;
1432         }
1433         // if no retries left, set invalid bit to force restoring at next occasion
1434         // and avoid inconsistent active state on client and server sides
1435         if (mCblk != nullptr) {
1436             android_atomic_or(CBLK_INVALID, &mCblk->mFlags);
1437         }
1438     }
1439 
1440     return result;
1441 }
1442 
addAudioDeviceCallback(const sp<AudioSystem::AudioDeviceCallback> & callback)1443 status_t AudioRecord::addAudioDeviceCallback(const sp<AudioSystem::AudioDeviceCallback>& callback)
1444 {
1445     if (callback == 0) {
1446         ALOGW("%s(%d): adding NULL callback!", __func__, mPortId);
1447         return BAD_VALUE;
1448     }
1449     AutoMutex lock(mLock);
1450     if (mDeviceCallback.unsafe_get() == callback.get()) {
1451         ALOGW("%s(%d): adding same callback!", __func__, mPortId);
1452         return INVALID_OPERATION;
1453     }
1454     status_t status = NO_ERROR;
1455     if (mInput != AUDIO_IO_HANDLE_NONE) {
1456         if (mDeviceCallback != 0) {
1457             ALOGW("%s(%d): callback already present!", __func__, mPortId);
1458             AudioSystem::removeAudioDeviceCallback(this, mInput, mPortId);
1459         }
1460         status = AudioSystem::addAudioDeviceCallback(this, mInput, mPortId);
1461     }
1462     mDeviceCallback = callback;
1463     return status;
1464 }
1465 
removeAudioDeviceCallback(const sp<AudioSystem::AudioDeviceCallback> & callback)1466 status_t AudioRecord::removeAudioDeviceCallback(
1467         const sp<AudioSystem::AudioDeviceCallback>& callback)
1468 {
1469     if (callback == 0) {
1470         ALOGW("%s(%d): removing NULL callback!", __func__, mPortId);
1471         return BAD_VALUE;
1472     }
1473     AutoMutex lock(mLock);
1474     if (mDeviceCallback.unsafe_get() != callback.get()) {
1475         ALOGW("%s(%d): removing different callback!", __func__, mPortId);
1476         return INVALID_OPERATION;
1477     }
1478     mDeviceCallback.clear();
1479     if (mInput != AUDIO_IO_HANDLE_NONE) {
1480         AudioSystem::removeAudioDeviceCallback(this, mInput, mPortId);
1481     }
1482     return NO_ERROR;
1483 }
1484 
onAudioDeviceUpdate(audio_io_handle_t audioIo,audio_port_handle_t deviceId)1485 void AudioRecord::onAudioDeviceUpdate(audio_io_handle_t audioIo,
1486                                  audio_port_handle_t deviceId)
1487 {
1488     sp<AudioSystem::AudioDeviceCallback> callback;
1489     {
1490         AutoMutex lock(mLock);
1491         if (audioIo != mInput) {
1492             return;
1493         }
1494         callback = mDeviceCallback.promote();
1495         // only update device if the record is active as route changes due to other use cases are
1496         // irrelevant for this client
1497         if (mActive) {
1498             mRoutedDeviceId = deviceId;
1499         }
1500     }
1501     if (callback.get() != nullptr) {
1502         callback->onAudioDeviceUpdate(mInput, mRoutedDeviceId);
1503     }
1504 }
1505 
1506 // -------------------------------------------------------------------------
1507 
getActiveMicrophones(std::vector<media::MicrophoneInfo> * activeMicrophones)1508 status_t AudioRecord::getActiveMicrophones(std::vector<media::MicrophoneInfo>* activeMicrophones)
1509 {
1510     AutoMutex lock(mLock);
1511     return mAudioRecord->getActiveMicrophones(activeMicrophones).transactionError();
1512 }
1513 
setPreferredMicrophoneDirection(audio_microphone_direction_t direction)1514 status_t AudioRecord::setPreferredMicrophoneDirection(audio_microphone_direction_t direction)
1515 {
1516     AutoMutex lock(mLock);
1517     if (mSelectedMicDirection == direction) {
1518         // NOP
1519         return OK;
1520     }
1521 
1522     mSelectedMicDirection = direction;
1523     if (mAudioRecord == 0) {
1524         // the internal AudioRecord hasn't be created yet, so just stash the attribute.
1525         return OK;
1526     } else {
1527         return mAudioRecord->setPreferredMicrophoneDirection(direction).transactionError();
1528     }
1529 }
1530 
setPreferredMicrophoneFieldDimension(float zoom)1531 status_t AudioRecord::setPreferredMicrophoneFieldDimension(float zoom) {
1532     AutoMutex lock(mLock);
1533     if (mSelectedMicFieldDimension == zoom) {
1534         // NOP
1535         return OK;
1536     }
1537 
1538     mSelectedMicFieldDimension = zoom;
1539     if (mAudioRecord == 0) {
1540         // the internal AudioRecord hasn't be created yet, so just stash the attribute.
1541         return OK;
1542     } else {
1543         return mAudioRecord->setPreferredMicrophoneFieldDimension(zoom).transactionError();
1544     }
1545 }
1546 
1547 // =========================================================================
1548 
binderDied(const wp<IBinder> & who __unused)1549 void AudioRecord::DeathNotifier::binderDied(const wp<IBinder>& who __unused)
1550 {
1551     sp<AudioRecord> audioRecord = mAudioRecord.promote();
1552     if (audioRecord != 0) {
1553         AutoMutex lock(audioRecord->mLock);
1554         audioRecord->mProxy->binderDied();
1555     }
1556 }
1557 
1558 // =========================================================================
1559 
AudioRecordThread(AudioRecord & receiver)1560 AudioRecord::AudioRecordThread::AudioRecordThread(AudioRecord& receiver)
1561     : Thread(true /* bCanCallJava */)  // binder recursion on restoreRecord_l() may call Java.
1562     , mReceiver(receiver), mPaused(true), mPausedInt(false), mPausedNs(0LL),
1563       mIgnoreNextPausedInt(false)
1564 {
1565 }
1566 
~AudioRecordThread()1567 AudioRecord::AudioRecordThread::~AudioRecordThread()
1568 {
1569 }
1570 
threadLoop()1571 bool AudioRecord::AudioRecordThread::threadLoop()
1572 {
1573     {
1574         AutoMutex _l(mMyLock);
1575         if (mPaused) {
1576             // TODO check return value and handle or log
1577             mMyCond.wait(mMyLock);
1578             // caller will check for exitPending()
1579             return true;
1580         }
1581         if (mIgnoreNextPausedInt) {
1582             mIgnoreNextPausedInt = false;
1583             mPausedInt = false;
1584         }
1585         if (mPausedInt) {
1586             if (mPausedNs > 0) {
1587                 // TODO check return value and handle or log
1588                 (void) mMyCond.waitRelative(mMyLock, mPausedNs);
1589             } else {
1590                 // TODO check return value and handle or log
1591                 mMyCond.wait(mMyLock);
1592             }
1593             mPausedInt = false;
1594             return true;
1595         }
1596     }
1597     if (exitPending()) {
1598         return false;
1599     }
1600     nsecs_t ns =  mReceiver.processAudioBuffer();
1601     switch (ns) {
1602     case 0:
1603         return true;
1604     case NS_INACTIVE:
1605         pauseInternal();
1606         return true;
1607     case NS_NEVER:
1608         return false;
1609     case NS_WHENEVER:
1610         // Event driven: call wake() when callback notifications conditions change.
1611         ns = INT64_MAX;
1612         FALLTHROUGH_INTENDED;
1613     default:
1614         LOG_ALWAYS_FATAL_IF(ns < 0, "%s() returned %lld", __func__, (long long)ns);
1615         pauseInternal(ns);
1616         return true;
1617     }
1618 }
1619 
requestExit()1620 void AudioRecord::AudioRecordThread::requestExit()
1621 {
1622     // must be in this order to avoid a race condition
1623     Thread::requestExit();
1624     resume();
1625 }
1626 
pause()1627 void AudioRecord::AudioRecordThread::pause()
1628 {
1629     AutoMutex _l(mMyLock);
1630     mPaused = true;
1631 }
1632 
resume()1633 void AudioRecord::AudioRecordThread::resume()
1634 {
1635     AutoMutex _l(mMyLock);
1636     mIgnoreNextPausedInt = true;
1637     if (mPaused || mPausedInt) {
1638         mPaused = false;
1639         mPausedInt = false;
1640         mMyCond.signal();
1641     }
1642 }
1643 
wake()1644 void AudioRecord::AudioRecordThread::wake()
1645 {
1646     AutoMutex _l(mMyLock);
1647     if (!mPaused) {
1648         // wake() might be called while servicing a callback - ignore the next
1649         // pause time and call processAudioBuffer.
1650         mIgnoreNextPausedInt = true;
1651         if (mPausedInt && mPausedNs > 0) {
1652             // audio record is active and internally paused with timeout.
1653             mPausedInt = false;
1654             mMyCond.signal();
1655         }
1656     }
1657 }
1658 
pauseInternal(nsecs_t ns)1659 void AudioRecord::AudioRecordThread::pauseInternal(nsecs_t ns)
1660 {
1661     AutoMutex _l(mMyLock);
1662     mPausedInt = true;
1663     mPausedNs = ns;
1664 }
1665 
1666 // -------------------------------------------------------------------------
1667 
1668 } // namespace android
1669