blob: 3c930831b1f02c8c2542186bebec59d1e867edcd [file] [log] [blame]
Eric Laurent81784c32012-11-19 14:55:58 -08001/*
2**
3** Copyright 2012, 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
Eric Laurent81784c32012-11-19 14:55:58 -080018#define LOG_TAG "AudioFlinger"
19//#define LOG_NDEBUG 0
Mikhail Naganov938be412019-09-04 11:38:47 -070020#define ATRACE_TAG ATRACE_TAG_AUDIO
Eric Laurent81784c32012-11-19 14:55:58 -080021
Andy Hungce240472023-07-19 11:40:07 -070022#include "MmapTracks.h"
23#include "PlaybackTracks.h"
24#include "RecordTracks.h"
25
26#include "Client.h"
27#include "IAfEffect.h"
28#include "IAfThread.h"
29#include "ResamplerBufferProvider.h"
30
31#include <audio_utils/minifloat.h>
32#include <media/AudioValidator.h>
33#include <media/RecordBufferConverter.h>
34#include <media/nbaio/Pipe.h>
35#include <media/nbaio/PipeReader.h>
36#include <mediautils/ServiceUtilities.h>
37#include <mediautils/SharedMemoryAllocator.h>
38#include <private/media/AudioTrackShared.h>
Eric Laurent81784c32012-11-19 14:55:58 -080039#include <utils/Log.h>
Mikhail Naganov938be412019-09-04 11:38:47 -070040#include <utils/Trace.h>
Eric Laurent81784c32012-11-19 14:55:58 -080041
Andy Hungce240472023-07-19 11:40:07 -070042#include <linux/futex.h>
43#include <math.h>
44#include <sys/syscall.h>
Glenn Kastenda6ef132013-01-10 12:31:01 -080045
Eric Laurent81784c32012-11-19 14:55:58 -080046// ----------------------------------------------------------------------------
47
48// Note: the following macro is used for extremely verbose logging message. In
49// order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to
50// 0; but one side effect of this is to turn all LOGV's as well. Some messages
51// are so verbose that we want to suppress them even when we have ALOG_ASSERT
52// turned on. Do not uncomment the #def below unless you really know what you
53// are doing and want to see all of the extremely verbose messages.
54//#define VERY_VERY_VERBOSE_LOGGING
55#ifdef VERY_VERY_VERBOSE_LOGGING
56#define ALOGVV ALOGV
57#else
58#define ALOGVV(a...) do { } while(0)
59#endif
60
Kuowei Lid4adbdb2020-08-13 14:44:25 +080061// TODO: Remove when this is put into AidlConversionUtil.h
62#define VALUE_OR_RETURN_BINDER_STATUS(x) \
63 ({ \
64 auto _tmp = (x); \
65 if (!_tmp.ok()) return ::android::aidl_utils::binderStatusFromStatusT(_tmp.error()); \
66 std::move(_tmp.value()); \
67 })
68
Eric Laurent81784c32012-11-19 14:55:58 -080069namespace android {
70
Kuowei Lid4adbdb2020-08-13 14:44:25 +080071using ::android::aidl_utils::binderStatusFromStatusT;
Ytai Ben-Tsvibdc293a2020-11-02 17:01:38 -080072using binder::Status;
Svet Ganov33761132021-05-13 22:51:08 +000073using content::AttributionSourceState;
Ivan Lozano8cf3a072017-08-09 09:01:33 -070074using media::VolumeShaper;
Eric Laurent81784c32012-11-19 14:55:58 -080075// ----------------------------------------------------------------------------
76// TrackBase
77// ----------------------------------------------------------------------------
Andy Hung9d84af52018-09-12 18:03:44 -070078#undef LOG_TAG
79#define LOG_TAG "AF::TrackBase"
Eric Laurent81784c32012-11-19 14:55:58 -080080
Glenn Kastenda6ef132013-01-10 12:31:01 -080081static volatile int32_t nextTrackId = 55;
82
Eric Laurent81784c32012-11-19 14:55:58 -080083// TrackBase constructor must be called with AudioFlinger::mLock held
Andy Hung3ff4b552023-06-26 19:20:57 -070084TrackBase::TrackBase(
Andy Hung44f27182023-07-06 20:56:16 -070085 IAfThreadBase *thread,
Eric Laurent81784c32012-11-19 14:55:58 -080086 const sp<Client>& client,
Kevin Rocard1f564ac2018-03-29 13:53:10 -070087 const audio_attributes_t& attr,
Eric Laurent81784c32012-11-19 14:55:58 -080088 uint32_t sampleRate,
89 audio_format_t format,
90 audio_channel_mask_t channelMask,
91 size_t frameCount,
Eric Laurent83b88082014-06-20 18:31:16 -070092 void *buffer,
Andy Hung8fe68032017-06-05 16:17:51 -070093 size_t bufferSize,
Glenn Kastend848eb42016-03-08 13:42:11 -080094 audio_session_t sessionId,
Eric Laurent09f1ed22019-04-24 17:45:17 -070095 pid_t creatorPid,
Andy Hung1f12a8a2016-11-07 16:10:30 -080096 uid_t clientUid,
Glenn Kastend776ac62014-05-07 09:16:09 -070097 bool isOut,
Dmitry Sidorenkova41c2732023-05-15 13:47:07 -070098 const alloc_type alloc,
Eric Laurent20b9ef02016-12-05 11:03:16 -080099 track_type type,
Andy Hungb68f5eb2019-12-03 16:49:17 -0800100 audio_port_handle_t portId,
101 std::string metricsId)
Andy Hung02a6c4e2023-06-23 19:27:19 -0700102 :
Eric Laurent81784c32012-11-19 14:55:58 -0800103 mThread(thread),
Dmitry Sidorenkova41c2732023-05-15 13:47:07 -0700104 mAllocType(alloc),
Eric Laurent81784c32012-11-19 14:55:58 -0800105 mClient(client),
106 mCblk(NULL),
Andy Hung8fe68032017-06-05 16:17:51 -0700107 // mBuffer, mBufferSize
Eric Laurent81784c32012-11-19 14:55:58 -0800108 mState(IDLE),
Kevin Rocard1f564ac2018-03-29 13:53:10 -0700109 mAttr(attr),
Eric Laurent81784c32012-11-19 14:55:58 -0800110 mSampleRate(sampleRate),
111 mFormat(format),
112 mChannelMask(channelMask),
Andy Hunge5412692014-05-16 11:25:07 -0700113 mChannelCount(isOut ?
114 audio_channel_count_from_out_mask(channelMask) :
115 audio_channel_count_from_in_mask(channelMask)),
Phil Burkfdb3c072016-02-09 10:47:02 -0800116 mFrameSize(audio_has_proportional_frames(format) ?
Eric Laurent81784c32012-11-19 14:55:58 -0800117 mChannelCount * audio_bytes_per_sample(format) : sizeof(int8_t)),
118 mFrameCount(frameCount),
Glenn Kastene3aa6592012-12-04 12:22:46 -0800119 mSessionId(sessionId),
120 mIsOut(isOut),
Eric Laurentbfb1b832013-01-07 09:53:42 -0800121 mId(android_atomic_inc(&nextTrackId)),
Eric Laurent83b88082014-06-20 18:31:16 -0700122 mTerminated(false),
Eric Laurentaaa44472014-09-12 17:41:50 -0700123 mType(type),
Kevin Rocard153f92d2018-12-18 18:33:28 -0800124 mThreadIoHandle(thread ? thread->id() : AUDIO_IO_HANDLE_NONE),
Eric Laurent6acd1d42017-01-04 14:23:29 -0800125 mPortId(portId),
Eric Laurent09f1ed22019-04-24 17:45:17 -0700126 mIsInvalid(false),
Kunal Malhotra3be68902023-02-28 22:03:15 +0000127 mTrackMetrics(std::move(metricsId), isOut, clientUid),
Eric Laurent09f1ed22019-04-24 17:45:17 -0700128 mCreatorPid(creatorPid)
Eric Laurent81784c32012-11-19 14:55:58 -0800129{
Marco Nelissendcb346b2015-09-09 10:47:29 -0700130 const uid_t callingUid = IPCThreadState::self()->getCallingUid();
Andy Hung4ef19fa2018-05-15 19:35:29 -0700131 if (!isAudioServerOrMediaServerUid(callingUid) || clientUid == AUDIO_UID_INVALID) {
Andy Hung1f12a8a2016-11-07 16:10:30 -0800132 ALOGW_IF(clientUid != AUDIO_UID_INVALID && clientUid != callingUid,
Andy Hung9d84af52018-09-12 18:03:44 -0700133 "%s(%d): uid %d tried to pass itself off as %d",
134 __func__, mId, callingUid, clientUid);
Andy Hung1f12a8a2016-11-07 16:10:30 -0800135 clientUid = callingUid;
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800136 }
137 // clientUid contains the uid of the app that is responsible for this track, so we can blame
138 // battery usage on it.
139 mUid = clientUid;
140
Eric Laurent81784c32012-11-19 14:55:58 -0800141 // ALOGD("Creating track with %d buffers @ %d bytes", bufferCount, bufferSize);
Andy Hung1883f692017-02-13 18:48:39 -0800142
Andy Hung8fe68032017-06-05 16:17:51 -0700143 size_t minBufferSize = buffer == NULL ? roundup(frameCount) : frameCount;
Andy Hung1883f692017-02-13 18:48:39 -0800144 // check overflow when computing bufferSize due to multiplication by mFrameSize.
Andy Hung8fe68032017-06-05 16:17:51 -0700145 if (minBufferSize < frameCount // roundup rounds down for values above UINT_MAX / 2
Andy Hung1883f692017-02-13 18:48:39 -0800146 || mFrameSize == 0 // format needs to be correct
Andy Hung8fe68032017-06-05 16:17:51 -0700147 || minBufferSize > SIZE_MAX / mFrameSize) {
Andy Hung1883f692017-02-13 18:48:39 -0800148 android_errorWriteLog(0x534e4554, "34749571");
149 return;
150 }
Andy Hung8fe68032017-06-05 16:17:51 -0700151 minBufferSize *= mFrameSize;
152
153 if (buffer == nullptr) {
154 bufferSize = minBufferSize; // allocated here.
155 } else if (minBufferSize > bufferSize) {
156 android_errorWriteLog(0x534e4554, "38340117");
157 return;
158 }
Andy Hung1883f692017-02-13 18:48:39 -0800159
Eric Laurent81784c32012-11-19 14:55:58 -0800160 size_t size = sizeof(audio_track_cblk_t);
Eric Laurent83b88082014-06-20 18:31:16 -0700161 if (buffer == NULL && alloc == ALLOC_CBLK) {
Andy Hung1883f692017-02-13 18:48:39 -0800162 // check overflow when computing allocation size for streaming tracks.
163 if (size > SIZE_MAX - bufferSize) {
164 android_errorWriteLog(0x534e4554, "34749571");
165 return;
166 }
Eric Laurent81784c32012-11-19 14:55:58 -0800167 size += bufferSize;
168 }
169
170 if (client != 0) {
Atneya3c61d882021-09-20 14:52:15 -0400171 mCblkMemory = client->allocator().allocate(mediautils::NamedAllocRequest{{size},
172 std::string("Track ID: ").append(std::to_string(mId))});
Glenn Kasten663c2242013-09-24 11:52:37 -0700173 if (mCblkMemory == 0 ||
Ytai Ben-Tsvi7dd39722019-09-05 15:14:30 -0700174 (mCblk = static_cast<audio_track_cblk_t *>(mCblkMemory->unsecurePointer())) == NULL) {
Andy Hung9d84af52018-09-12 18:03:44 -0700175 ALOGE("%s(%d): not enough memory for AudioTrack size=%zu", __func__, mId, size);
Atneya3c61d882021-09-20 14:52:15 -0400176 ALOGE("%s", client->allocator().dump().c_str());
Glenn Kasten663c2242013-09-24 11:52:37 -0700177 mCblkMemory.clear();
Eric Laurent81784c32012-11-19 14:55:58 -0800178 return;
179 }
180 } else {
Andy Hungafb31482017-02-13 18:50:48 -0800181 mCblk = (audio_track_cblk_t *) malloc(size);
182 if (mCblk == NULL) {
Andy Hung9d84af52018-09-12 18:03:44 -0700183 ALOGE("%s(%d): not enough memory for AudioTrack size=%zu", __func__, mId, size);
Andy Hungafb31482017-02-13 18:50:48 -0800184 return;
185 }
Eric Laurent81784c32012-11-19 14:55:58 -0800186 }
187
188 // construct the shared structure in-place.
189 if (mCblk != NULL) {
190 new(mCblk) audio_track_cblk_t();
Glenn Kastenc263ca02014-06-04 20:31:46 -0700191 switch (alloc) {
192 case ALLOC_READONLY: {
Glenn Kastend776ac62014-05-07 09:16:09 -0700193 const sp<MemoryDealer> roHeap(thread->readOnlyHeap());
194 if (roHeap == 0 ||
195 (mBufferMemory = roHeap->allocate(bufferSize)) == 0 ||
Ytai Ben-Tsvi7dd39722019-09-05 15:14:30 -0700196 (mBuffer = mBufferMemory->unsecurePointer()) == NULL) {
Andy Hung9d84af52018-09-12 18:03:44 -0700197 ALOGE("%s(%d): not enough memory for read-only buffer size=%zu",
198 __func__, mId, bufferSize);
Glenn Kastend776ac62014-05-07 09:16:09 -0700199 if (roHeap != 0) {
200 roHeap->dump("buffer");
201 }
202 mCblkMemory.clear();
203 mBufferMemory.clear();
204 return;
205 }
Eric Laurent81784c32012-11-19 14:55:58 -0800206 memset(mBuffer, 0, bufferSize);
Glenn Kastenc263ca02014-06-04 20:31:46 -0700207 } break;
208 case ALLOC_PIPE:
209 mBufferMemory = thread->pipeMemory();
210 // mBuffer is the virtual address as seen from current process (mediaserver),
Ytai Ben-Tsvi7dd39722019-09-05 15:14:30 -0700211 // and should normally be coming from mBufferMemory->unsecurePointer().
Glenn Kastenc263ca02014-06-04 20:31:46 -0700212 // However in this case the TrackBase does not reference the buffer directly.
213 // It should references the buffer via the pipe.
214 // Therefore, to detect incorrect usage of the buffer, we set mBuffer to NULL.
215 mBuffer = NULL;
Andy Hung8fe68032017-06-05 16:17:51 -0700216 bufferSize = 0;
Glenn Kastenc263ca02014-06-04 20:31:46 -0700217 break;
218 case ALLOC_CBLK:
Glenn Kastend776ac62014-05-07 09:16:09 -0700219 // clear all buffers
Eric Laurent83b88082014-06-20 18:31:16 -0700220 if (buffer == NULL) {
Glenn Kastend776ac62014-05-07 09:16:09 -0700221 mBuffer = (char*)mCblk + sizeof(audio_track_cblk_t);
222 memset(mBuffer, 0, bufferSize);
223 } else {
Eric Laurent83b88082014-06-20 18:31:16 -0700224 mBuffer = buffer;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800225#if 0
Glenn Kastend776ac62014-05-07 09:16:09 -0700226 mCblk->mFlags = CBLK_FORCEREADY; // FIXME hack, need to fix the track ready logic
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800227#endif
Glenn Kastend776ac62014-05-07 09:16:09 -0700228 }
Glenn Kastenc263ca02014-06-04 20:31:46 -0700229 break;
Eric Laurent83b88082014-06-20 18:31:16 -0700230 case ALLOC_LOCAL:
231 mBuffer = calloc(1, bufferSize);
232 break;
233 case ALLOC_NONE:
234 mBuffer = buffer;
235 break;
Andy Hung8fe68032017-06-05 16:17:51 -0700236 default:
Andy Hung9d84af52018-09-12 18:03:44 -0700237 LOG_ALWAYS_FATAL("%s(%d): invalid allocation type: %d", __func__, mId, (int)alloc);
Eric Laurent81784c32012-11-19 14:55:58 -0800238 }
Andy Hung8fe68032017-06-05 16:17:51 -0700239 mBufferSize = bufferSize;
Glenn Kastenda6ef132013-01-10 12:31:01 -0800240
Glenn Kasten46909e72013-02-26 09:20:22 -0800241#ifdef TEE_SINK
Andy Hung8946a282018-04-19 20:04:56 -0700242 mTee.set(sampleRate, mChannelCount, format, NBAIO_Tee::TEE_FLAG_TRACK);
Glenn Kasten46909e72013-02-26 09:20:22 -0800243#endif
Andy Hung959b5b82021-09-24 10:46:20 -0700244 // mState is mirrored for the client to read.
245 mState.setMirror(&mCblk->mState);
246 // ensure our state matches up until we consolidate the enumeration.
247 static_assert(CBLK_STATE_IDLE == IDLE);
248 static_assert(CBLK_STATE_PAUSING == PAUSING);
Eric Laurent81784c32012-11-19 14:55:58 -0800249 }
250}
251
Svet Ganov33761132021-05-13 22:51:08 +0000252// TODO b/182392769: use attribution source util
253static AttributionSourceState audioServerAttributionSource(pid_t pid) {
254 AttributionSourceState attributionSource{};
255 attributionSource.uid = AID_AUDIOSERVER;
256 attributionSource.pid = pid;
257 attributionSource.token = sp<BBinder>::make();
258 return attributionSource;
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700259}
260
Andy Hung3ff4b552023-06-26 19:20:57 -0700261status_t TrackBase::initCheck() const
Eric Laurent83b88082014-06-20 18:31:16 -0700262{
263 status_t status;
264 if (mType == TYPE_OUTPUT || mType == TYPE_PATCH) {
265 status = cblk() != NULL ? NO_ERROR : NO_MEMORY;
266 } else {
267 status = getCblk() != 0 ? NO_ERROR : NO_MEMORY;
268 }
269 return status;
270}
271
Andy Hung3ff4b552023-06-26 19:20:57 -0700272TrackBase::~TrackBase()
Eric Laurent81784c32012-11-19 14:55:58 -0800273{
Glenn Kastene3aa6592012-12-04 12:22:46 -0800274 // delete the proxy before deleting the shared memory it refers to, to avoid dangling reference
Eric Laurent5bba2f62016-03-18 11:14:14 -0700275 mServerProxy.clear();
Andy Hung689e82c2019-08-21 17:53:17 -0700276 releaseCblk();
Eric Laurent81784c32012-11-19 14:55:58 -0800277 mCblkMemory.clear(); // free the shared memory before releasing the heap it belongs to
278 if (mClient != 0) {
Eric Laurent021cf962014-05-13 10:18:14 -0700279 // Client destructor must run with AudioFlinger client mutex locked
Andy Hung2ac52f12023-08-28 18:36:53 -0700280 audio_utils::lock_guard _l(mClient->afClientCallback()->clientMutex());
Eric Laurent81784c32012-11-19 14:55:58 -0800281 // If the client's reference count drops to zero, the associated destructor
282 // must run with AudioFlinger lock held. Thus the explicit clear() rather than
283 // relying on the automatic clear() at end of scope.
284 mClient.clear();
285 }
Dmitry Sidorenkova41c2732023-05-15 13:47:07 -0700286 if (mAllocType == ALLOC_LOCAL) {
287 free(mBuffer);
288 mBuffer = nullptr;
289 }
Eric Laurent3bcffa12014-06-12 18:38:45 -0700290 // flush the binder command buffer
291 IPCThreadState::self()->flushCommands();
Eric Laurent81784c32012-11-19 14:55:58 -0800292}
293
294// AudioBufferProvider interface
295// getNextBuffer() = 0;
Glenn Kastend79072e2016-01-06 08:41:20 -0800296// This implementation of releaseBuffer() is used by Track and RecordTrack
Andy Hung3ff4b552023-06-26 19:20:57 -0700297void TrackBase::releaseBuffer(AudioBufferProvider::Buffer* buffer)
Eric Laurent81784c32012-11-19 14:55:58 -0800298{
Glenn Kasten46909e72013-02-26 09:20:22 -0800299#ifdef TEE_SINK
Andy Hung8946a282018-04-19 20:04:56 -0700300 mTee.write(buffer->raw, buffer->frameCount);
Glenn Kasten46909e72013-02-26 09:20:22 -0800301#endif
Glenn Kastenda6ef132013-01-10 12:31:01 -0800302
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800303 ServerProxy::Buffer buf;
304 buf.mFrameCount = buffer->frameCount;
305 buf.mRaw = buffer->raw;
Eric Laurent81784c32012-11-19 14:55:58 -0800306 buffer->frameCount = 0;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800307 buffer->raw = NULL;
308 mServerProxy->releaseBuffer(&buf);
Eric Laurent81784c32012-11-19 14:55:58 -0800309}
310
Andy Hung3ff4b552023-06-26 19:20:57 -0700311status_t TrackBase::setSyncEvent(
Andy Hung068e08e2023-05-15 19:02:55 -0700312 const sp<audioflinger::SyncEvent>& event)
Eric Laurent81784c32012-11-19 14:55:58 -0800313{
Andy Hung068e08e2023-05-15 19:02:55 -0700314 mSyncEvents.emplace_back(event);
Eric Laurent81784c32012-11-19 14:55:58 -0800315 return NO_ERROR;
316}
317
Andy Hung3ff4b552023-06-26 19:20:57 -0700318PatchTrackBase::PatchTrackBase(const sp<ClientProxy>& proxy,
Andy Hung837229a2023-07-14 16:57:01 -0700319 IAfThreadBase* thread, const Timeout& timeout)
Kevin Rocard45986c72018-12-18 18:22:59 -0800320 : mProxy(proxy)
321{
322 if (timeout) {
323 setPeerTimeout(*timeout);
324 } else {
325 // Double buffer mixer
Andy Hung837229a2023-07-14 16:57:01 -0700326 uint64_t mixBufferNs = ((uint64_t)2 * thread->frameCount() * 1000000000) /
327 thread->sampleRate();
Kevin Rocard45986c72018-12-18 18:22:59 -0800328 setPeerTimeout(std::chrono::nanoseconds{mixBufferNs});
329 }
330}
331
Andy Hung3ff4b552023-06-26 19:20:57 -0700332void PatchTrackBase::setPeerTimeout(std::chrono::nanoseconds timeout) {
Kevin Rocard45986c72018-12-18 18:22:59 -0800333 mPeerTimeout.tv_sec = timeout.count() / std::nano::den;
334 mPeerTimeout.tv_nsec = timeout.count() % std::nano::den;
335}
336
337
Eric Laurent81784c32012-11-19 14:55:58 -0800338// ----------------------------------------------------------------------------
339// Playback
340// ----------------------------------------------------------------------------
Andy Hung9d84af52018-09-12 18:03:44 -0700341#undef LOG_TAG
342#define LOG_TAG "AF::TrackHandle"
Eric Laurent81784c32012-11-19 14:55:58 -0800343
Andy Hungaaa18282023-06-23 19:27:19 -0700344class TrackHandle : public android::media::BnAudioTrack {
345public:
Andy Hung02a6c4e2023-06-23 19:27:19 -0700346 explicit TrackHandle(const sp<IAfTrack>& track);
Andy Hungaaa18282023-06-23 19:27:19 -0700347 ~TrackHandle() override;
348
349 binder::Status getCblk(std::optional<media::SharedFileRegion>* _aidl_return) final;
350 binder::Status start(int32_t* _aidl_return) final;
351 binder::Status stop() final;
352 binder::Status flush() final;
353 binder::Status pause() final;
354 binder::Status attachAuxEffect(int32_t effectId, int32_t* _aidl_return) final;
355 binder::Status setParameters(const std::string& keyValuePairs,
356 int32_t* _aidl_return) final;
357 binder::Status selectPresentation(int32_t presentationId, int32_t programId,
358 int32_t* _aidl_return) final;
359 binder::Status getTimestamp(media::AudioTimestampInternal* timestamp,
360 int32_t* _aidl_return) final;
361 binder::Status signal() final;
362 binder::Status applyVolumeShaper(const media::VolumeShaperConfiguration& configuration,
363 const media::VolumeShaperOperation& operation,
364 int32_t* _aidl_return) final;
365 binder::Status getVolumeShaperState(
366 int32_t id,
367 std::optional<media::VolumeShaperState>* _aidl_return) final;
368 binder::Status getDualMonoMode(
369 media::audio::common::AudioDualMonoMode* _aidl_return) final;
370 binder::Status setDualMonoMode(
371 media::audio::common::AudioDualMonoMode mode) final;
372 binder::Status getAudioDescriptionMixLevel(float* _aidl_return) final;
373 binder::Status setAudioDescriptionMixLevel(float leveldB) final;
374 binder::Status getPlaybackRateParameters(
375 media::audio::common::AudioPlaybackRate* _aidl_return) final;
376 binder::Status setPlaybackRateParameters(
377 const media::audio::common::AudioPlaybackRate& playbackRate) final;
378
379private:
Andy Hung02a6c4e2023-06-23 19:27:19 -0700380 const sp<IAfTrack> mTrack;
Andy Hungaaa18282023-06-23 19:27:19 -0700381};
382
383/* static */
Andy Hung02a6c4e2023-06-23 19:27:19 -0700384sp<media::IAudioTrack> IAfTrack::createIAudioTrackAdapter(const sp<IAfTrack>& track) {
Andy Hungaaa18282023-06-23 19:27:19 -0700385 return sp<TrackHandle>::make(track);
386}
387
Andy Hung02a6c4e2023-06-23 19:27:19 -0700388TrackHandle::TrackHandle(const sp<IAfTrack>& track)
Eric Laurent81784c32012-11-19 14:55:58 -0800389 : BnAudioTrack(),
390 mTrack(track)
391{
Andy Hung225aef62022-12-06 16:33:20 -0800392 setMinSchedulerPolicy(SCHED_NORMAL, ANDROID_PRIORITY_AUDIO);
Eric Laurent81784c32012-11-19 14:55:58 -0800393}
394
Andy Hungaaa18282023-06-23 19:27:19 -0700395TrackHandle::~TrackHandle() {
Eric Laurent81784c32012-11-19 14:55:58 -0800396 // just stop the track on deletion, associated resources
397 // will be freed from the main thread once all pending buffers have
398 // been played. Unless it's not in the active track list, in which
399 // case we free everything now...
400 mTrack->destroy();
401}
402
Andy Hungaaa18282023-06-23 19:27:19 -0700403Status TrackHandle::getCblk(
Ytai Ben-Tsvibdc293a2020-11-02 17:01:38 -0800404 std::optional<media::SharedFileRegion>* _aidl_return) {
405 *_aidl_return = legacy2aidl_NullableIMemory_SharedFileRegion(mTrack->getCblk()).value();
406 return Status::ok();
Eric Laurent81784c32012-11-19 14:55:58 -0800407}
408
Andy Hungaaa18282023-06-23 19:27:19 -0700409Status TrackHandle::start(int32_t* _aidl_return) {
Ytai Ben-Tsvibdc293a2020-11-02 17:01:38 -0800410 *_aidl_return = mTrack->start();
411 return Status::ok();
Eric Laurent81784c32012-11-19 14:55:58 -0800412}
413
Andy Hungaaa18282023-06-23 19:27:19 -0700414Status TrackHandle::stop() {
Eric Laurent81784c32012-11-19 14:55:58 -0800415 mTrack->stop();
Ytai Ben-Tsvibdc293a2020-11-02 17:01:38 -0800416 return Status::ok();
Eric Laurent81784c32012-11-19 14:55:58 -0800417}
418
Andy Hungaaa18282023-06-23 19:27:19 -0700419Status TrackHandle::flush() {
Eric Laurent81784c32012-11-19 14:55:58 -0800420 mTrack->flush();
Ytai Ben-Tsvibdc293a2020-11-02 17:01:38 -0800421 return Status::ok();
Eric Laurent81784c32012-11-19 14:55:58 -0800422}
423
Andy Hungaaa18282023-06-23 19:27:19 -0700424Status TrackHandle::pause() {
Eric Laurent81784c32012-11-19 14:55:58 -0800425 mTrack->pause();
Ytai Ben-Tsvibdc293a2020-11-02 17:01:38 -0800426 return Status::ok();
Eric Laurent81784c32012-11-19 14:55:58 -0800427}
428
Andy Hungaaa18282023-06-23 19:27:19 -0700429Status TrackHandle::attachAuxEffect(int32_t effectId,
Ytai Ben-Tsvibdc293a2020-11-02 17:01:38 -0800430 int32_t* _aidl_return) {
431 *_aidl_return = mTrack->attachAuxEffect(effectId);
432 return Status::ok();
Eric Laurent81784c32012-11-19 14:55:58 -0800433}
434
Andy Hungaaa18282023-06-23 19:27:19 -0700435Status TrackHandle::setParameters(const std::string& keyValuePairs,
Ytai Ben-Tsvibdc293a2020-11-02 17:01:38 -0800436 int32_t* _aidl_return) {
437 *_aidl_return = mTrack->setParameters(String8(keyValuePairs.c_str()));
438 return Status::ok();
Glenn Kasten3dcd00d2013-07-17 10:10:23 -0700439}
440
Andy Hungaaa18282023-06-23 19:27:19 -0700441Status TrackHandle::selectPresentation(int32_t presentationId, int32_t programId,
Ytai Ben-Tsvibdc293a2020-11-02 17:01:38 -0800442 int32_t* _aidl_return) {
443 *_aidl_return = mTrack->selectPresentation(presentationId, programId);
444 return Status::ok();
Mikhail Naganovac917ac2018-11-28 14:03:52 -0800445}
446
Andy Hungaaa18282023-06-23 19:27:19 -0700447Status TrackHandle::getTimestamp(media::AudioTimestampInternal* timestamp,
Ytai Ben-Tsvibdc293a2020-11-02 17:01:38 -0800448 int32_t* _aidl_return) {
449 AudioTimestamp legacy;
450 *_aidl_return = mTrack->getTimestamp(legacy);
451 if (*_aidl_return != OK) {
452 return Status::ok();
453 }
Andy Hung973638a2020-12-08 20:47:45 -0800454 *timestamp = legacy2aidl_AudioTimestamp_AudioTimestampInternal(legacy).value();
Ytai Ben-Tsvibdc293a2020-11-02 17:01:38 -0800455 return Status::ok();
Andy Hung9fc8b5c2017-01-24 13:36:48 -0800456}
457
Andy Hungaaa18282023-06-23 19:27:19 -0700458Status TrackHandle::signal() {
Ytai Ben-Tsvibdc293a2020-11-02 17:01:38 -0800459 mTrack->signal();
460 return Status::ok();
Andy Hung9fc8b5c2017-01-24 13:36:48 -0800461}
462
Andy Hungaaa18282023-06-23 19:27:19 -0700463Status TrackHandle::applyVolumeShaper(
Ytai Ben-Tsvibdc293a2020-11-02 17:01:38 -0800464 const media::VolumeShaperConfiguration& configuration,
465 const media::VolumeShaperOperation& operation,
466 int32_t* _aidl_return) {
467 sp<VolumeShaper::Configuration> conf = new VolumeShaper::Configuration();
468 *_aidl_return = conf->readFromParcelable(configuration);
469 if (*_aidl_return != OK) {
470 return Status::ok();
471 }
472
473 sp<VolumeShaper::Operation> op = new VolumeShaper::Operation();
474 *_aidl_return = op->readFromParcelable(operation);
475 if (*_aidl_return != OK) {
476 return Status::ok();
477 }
478
479 *_aidl_return = mTrack->applyVolumeShaper(conf, op);
480 return Status::ok();
Glenn Kasten53cec222013-08-29 09:01:02 -0700481}
482
Andy Hungaaa18282023-06-23 19:27:19 -0700483Status TrackHandle::getVolumeShaperState(
Ytai Ben-Tsvibdc293a2020-11-02 17:01:38 -0800484 int32_t id,
485 std::optional<media::VolumeShaperState>* _aidl_return) {
486 sp<VolumeShaper::State> legacy = mTrack->getVolumeShaperState(id);
487 if (legacy == nullptr) {
488 _aidl_return->reset();
489 return Status::ok();
490 }
491 media::VolumeShaperState aidl;
492 legacy->writeToParcelable(&aidl);
493 *_aidl_return = aidl;
494 return Status::ok();
Eric Laurent81784c32012-11-19 14:55:58 -0800495}
496
Andy Hungaaa18282023-06-23 19:27:19 -0700497Status TrackHandle::getDualMonoMode(
Mikhail Naganova77d5552022-12-18 02:48:14 +0000498 media::audio::common::AudioDualMonoMode* _aidl_return)
Kuowei Lid4adbdb2020-08-13 14:44:25 +0800499{
500 audio_dual_mono_mode_t mode = AUDIO_DUAL_MONO_MODE_OFF;
501 const status_t status = mTrack->getDualMonoMode(&mode)
502 ?: AudioValidator::validateDualMonoMode(mode);
503 if (status == OK) {
504 *_aidl_return = VALUE_OR_RETURN_BINDER_STATUS(
505 legacy2aidl_audio_dual_mono_mode_t_AudioDualMonoMode(mode));
506 }
507 return binderStatusFromStatusT(status);
508}
509
Andy Hungaaa18282023-06-23 19:27:19 -0700510Status TrackHandle::setDualMonoMode(
Mikhail Naganova77d5552022-12-18 02:48:14 +0000511 media::audio::common::AudioDualMonoMode mode)
Kuowei Lid4adbdb2020-08-13 14:44:25 +0800512{
513 const auto localMonoMode = VALUE_OR_RETURN_BINDER_STATUS(
514 aidl2legacy_AudioDualMonoMode_audio_dual_mono_mode_t(mode));
515 return binderStatusFromStatusT(AudioValidator::validateDualMonoMode(localMonoMode)
516 ?: mTrack->setDualMonoMode(localMonoMode));
517}
518
Andy Hungaaa18282023-06-23 19:27:19 -0700519Status TrackHandle::getAudioDescriptionMixLevel(float* _aidl_return)
Kuowei Lid4adbdb2020-08-13 14:44:25 +0800520{
521 float leveldB = -std::numeric_limits<float>::infinity();
522 const status_t status = mTrack->getAudioDescriptionMixLevel(&leveldB)
523 ?: AudioValidator::validateAudioDescriptionMixLevel(leveldB);
524 if (status == OK) *_aidl_return = leveldB;
525 return binderStatusFromStatusT(status);
526}
527
Andy Hungaaa18282023-06-23 19:27:19 -0700528Status TrackHandle::setAudioDescriptionMixLevel(float leveldB)
Kuowei Lid4adbdb2020-08-13 14:44:25 +0800529{
530 return binderStatusFromStatusT(AudioValidator::validateAudioDescriptionMixLevel(leveldB)
531 ?: mTrack->setAudioDescriptionMixLevel(leveldB));
532}
533
Andy Hungaaa18282023-06-23 19:27:19 -0700534Status TrackHandle::getPlaybackRateParameters(
Mikhail Naganova77d5552022-12-18 02:48:14 +0000535 media::audio::common::AudioPlaybackRate* _aidl_return)
Kuowei Lid4adbdb2020-08-13 14:44:25 +0800536{
537 audio_playback_rate_t localPlaybackRate{};
538 status_t status = mTrack->getPlaybackRateParameters(&localPlaybackRate)
539 ?: AudioValidator::validatePlaybackRate(localPlaybackRate);
540 if (status == NO_ERROR) {
541 *_aidl_return = VALUE_OR_RETURN_BINDER_STATUS(
542 legacy2aidl_audio_playback_rate_t_AudioPlaybackRate(localPlaybackRate));
543 }
544 return binderStatusFromStatusT(status);
545}
546
Andy Hungaaa18282023-06-23 19:27:19 -0700547Status TrackHandle::setPlaybackRateParameters(
Mikhail Naganova77d5552022-12-18 02:48:14 +0000548 const media::audio::common::AudioPlaybackRate& playbackRate)
Kuowei Lid4adbdb2020-08-13 14:44:25 +0800549{
550 const audio_playback_rate_t localPlaybackRate = VALUE_OR_RETURN_BINDER_STATUS(
551 aidl2legacy_AudioPlaybackRate_audio_playback_rate_t(playbackRate));
552 return binderStatusFromStatusT(AudioValidator::validatePlaybackRate(localPlaybackRate)
553 ?: mTrack->setPlaybackRateParameters(localPlaybackRate));
554}
555
Eric Laurent81784c32012-11-19 14:55:58 -0800556// ----------------------------------------------------------------------------
Jean-Michel Trivi74e01fa2019-02-25 12:18:09 -0800557// AppOp for audio playback
558// -------------------------------
Mikhail Naganovf7e3a3a2019-04-22 16:43:26 -0700559
560// static
Andy Hung3ff4b552023-06-26 19:20:57 -0700561sp<OpPlayAudioMonitor> OpPlayAudioMonitor::createIfNeeded(
Andy Hung44f27182023-07-06 20:56:16 -0700562 IAfThreadBase* thread,
Svet Ganov33761132021-05-13 22:51:08 +0000563 const AttributionSourceState& attributionSource, const audio_attributes_t& attr, int id,
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700564 audio_stream_type_t streamType)
Jean-Michel Trivi74e01fa2019-02-25 12:18:09 -0800565{
Vlad Popa103be862023-07-10 20:27:41 -0700566 Vector<String16> packages;
567 const uid_t uid = VALUE_OR_FATAL(aidl2legacy_int32_t_uid_t(attributionSource.uid));
Colin Crossb8a9dbb2020-08-27 04:12:26 +0000568 getPackagesForUid(uid, packages);
Eric Laurent9066ad32019-05-20 14:40:10 -0700569 if (isServiceUid(uid)) {
Eric Laurent9066ad32019-05-20 14:40:10 -0700570 if (packages.isEmpty()) {
571 ALOGD("OpPlayAudio: not muting track:%d usage:%d for service UID %d",
572 id,
573 attr.usage,
574 uid);
575 return nullptr;
576 }
Jean-Michel Trivi74e01fa2019-02-25 12:18:09 -0800577 }
578 // stream type has been filtered by audio policy to indicate whether it can be muted
579 if (streamType == AUDIO_STREAM_ENFORCED_AUDIBLE) {
Eric Laurent2dab0302019-05-08 18:15:55 -0700580 ALOGD("OpPlayAudio: not muting track:%d usage:%d ENFORCED_AUDIBLE", id, attr.usage);
Mikhail Naganovf7e3a3a2019-04-22 16:43:26 -0700581 return nullptr;
Jean-Michel Trivi74e01fa2019-02-25 12:18:09 -0800582 }
Eric Laurent2dab0302019-05-08 18:15:55 -0700583 if ((attr.flags & AUDIO_FLAG_BYPASS_INTERRUPTION_POLICY)
584 == AUDIO_FLAG_BYPASS_INTERRUPTION_POLICY) {
585 ALOGD("OpPlayAudio: not muting track:%d flags %#x have FLAG_BYPASS_INTERRUPTION_POLICY",
586 id, attr.flags);
587 return nullptr;
588 }
Vlad Popa103be862023-07-10 20:27:41 -0700589 return sp<OpPlayAudioMonitor>::make(thread, attributionSource, attr.usage, id, uid);
Mikhail Naganovf7e3a3a2019-04-22 16:43:26 -0700590}
591
Andy Hung44f27182023-07-06 20:56:16 -0700592OpPlayAudioMonitor::OpPlayAudioMonitor(IAfThreadBase* thread,
593 const AttributionSourceState& attributionSource,
594 audio_usage_t usage, int id, uid_t uid)
595 : mThread(wp<IAfThreadBase>::fromExisting(thread)),
Vlad Popa103be862023-07-10 20:27:41 -0700596 mHasOpPlayAudio(true),
597 mAttributionSource(attributionSource),
598 mUsage((int32_t)usage),
599 mId(id),
600 mUid(uid),
601 mPackageName(VALUE_OR_FATAL(aidl2legacy_string_view_String16(
602 attributionSource.packageName.value_or("")))) {}
Jean-Michel Trivi74e01fa2019-02-25 12:18:09 -0800603
Andy Hung3ff4b552023-06-26 19:20:57 -0700604OpPlayAudioMonitor::~OpPlayAudioMonitor()
Jean-Michel Trivi74e01fa2019-02-25 12:18:09 -0800605{
606 if (mOpCallback != 0) {
607 mAppOpsManager.stopWatchingMode(mOpCallback);
608 }
609 mOpCallback.clear();
610}
611
Andy Hung3ff4b552023-06-26 19:20:57 -0700612void OpPlayAudioMonitor::onFirstRef()
Mikhail Naganovf7e3a3a2019-04-22 16:43:26 -0700613{
Vlad Popad2152122023-08-02 18:36:04 -0700614 // make sure not to broadcast the initial state since it is not needed and could
615 // cause a deadlock since this method can be called with the mThread->mLock held
616 checkPlayAudioForUsage(/*doBroadcast=*/false);
Svet Ganov33761132021-05-13 22:51:08 +0000617 if (mAttributionSource.packageName.has_value()) {
Mikhail Naganovf7e3a3a2019-04-22 16:43:26 -0700618 mOpCallback = new PlayAudioOpCallback(this);
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700619 mAppOpsManager.startWatchingMode(AppOpsManager::OP_PLAY_AUDIO,
Vlad Popa103be862023-07-10 20:27:41 -0700620 mPackageName, mOpCallback);
Mikhail Naganovf7e3a3a2019-04-22 16:43:26 -0700621 }
622}
623
Andy Hung3ff4b552023-06-26 19:20:57 -0700624bool OpPlayAudioMonitor::hasOpPlayAudio() const {
Jean-Michel Trivi74e01fa2019-02-25 12:18:09 -0800625 return mHasOpPlayAudio.load();
626}
627
Jean-Michel Trividdf87ef2019-08-20 15:42:04 -0700628// Note this method is never called (and never to be) for audio server / patch record track
Jean-Michel Trivi74e01fa2019-02-25 12:18:09 -0800629// - not called from constructor due to check on UID,
630// - not called from PlayAudioOpCallback because the callback is not installed in this case
Andy Hung3ff4b552023-06-26 19:20:57 -0700631void OpPlayAudioMonitor::checkPlayAudioForUsage(bool doBroadcast)
Jean-Michel Trivi74e01fa2019-02-25 12:18:09 -0800632{
Vlad Popa103be862023-07-10 20:27:41 -0700633 const bool hasAppOps = mAttributionSource.packageName.has_value()
634 && mAppOpsManager.checkAudioOpNoThrow(
635 AppOpsManager::OP_PLAY_AUDIO, mUsage, mUid, mPackageName) ==
636 AppOpsManager::MODE_ALLOWED;
637
638 bool shouldChange = !hasAppOps; // check if we need to update.
639 if (mHasOpPlayAudio.compare_exchange_strong(shouldChange, hasAppOps)) {
640 ALOGD("OpPlayAudio: track:%d usage:%d %smuted", mId, mUsage, hasAppOps ? "not " : "");
Vlad Popad2152122023-08-02 18:36:04 -0700641 if (doBroadcast) {
642 auto thread = mThread.promote();
Andy Hung71742ab2023-07-07 13:47:37 -0700643 if (thread != nullptr && thread->type() == IAfThreadBase::OFFLOAD) {
Vlad Popad2152122023-08-02 18:36:04 -0700644 // Wake up Thread if offloaded, otherwise it may be several seconds for update.
Andy Hung44f27182023-07-06 20:56:16 -0700645 Mutex::Autolock _l(thread->mutex());
Vlad Popad2152122023-08-02 18:36:04 -0700646 thread->broadcast_l();
647 }
Vlad Popa103be862023-07-10 20:27:41 -0700648 }
Jean-Michel Trivi74e01fa2019-02-25 12:18:09 -0800649 }
650}
651
Andy Hung3ff4b552023-06-26 19:20:57 -0700652OpPlayAudioMonitor::PlayAudioOpCallback::PlayAudioOpCallback(
Jean-Michel Trivi74e01fa2019-02-25 12:18:09 -0800653 const wp<OpPlayAudioMonitor>& monitor) : mMonitor(monitor)
654{ }
655
Andy Hung3ff4b552023-06-26 19:20:57 -0700656void OpPlayAudioMonitor::PlayAudioOpCallback::opChanged(int32_t op,
Jean-Michel Trivi74e01fa2019-02-25 12:18:09 -0800657 const String16& packageName) {
658 // we only have uid, so we need to check all package names anyway
659 UNUSED(packageName);
660 if (op != AppOpsManager::OP_PLAY_AUDIO) {
661 return;
662 }
663 sp<OpPlayAudioMonitor> monitor = mMonitor.promote();
664 if (monitor != NULL) {
Vlad Popad2152122023-08-02 18:36:04 -0700665 monitor->checkPlayAudioForUsage(/*doBroadcast=*/true);
Jean-Michel Trivi74e01fa2019-02-25 12:18:09 -0800666 }
667}
668
Eric Laurent9066ad32019-05-20 14:40:10 -0700669// static
Andy Hung3ff4b552023-06-26 19:20:57 -0700670void OpPlayAudioMonitor::getPackagesForUid(
Eric Laurent9066ad32019-05-20 14:40:10 -0700671 uid_t uid, Vector<String16>& packages)
672{
673 PermissionController permissionController;
674 permissionController.getPackagesForUid(uid, packages);
675}
676
Jean-Michel Trivi74e01fa2019-02-25 12:18:09 -0800677// ----------------------------------------------------------------------------
Andy Hung9d84af52018-09-12 18:03:44 -0700678#undef LOG_TAG
679#define LOG_TAG "AF::Track"
Eric Laurent81784c32012-11-19 14:55:58 -0800680
Andy Hung3ff4b552023-06-26 19:20:57 -0700681/* static */
Andy Hung44f27182023-07-06 20:56:16 -0700682sp<IAfTrack> IAfTrack::create(
683 IAfPlaybackThread* thread,
Andy Hung3ff4b552023-06-26 19:20:57 -0700684 const sp<Client>& client,
685 audio_stream_type_t streamType,
686 const audio_attributes_t& attr,
687 uint32_t sampleRate,
688 audio_format_t format,
689 audio_channel_mask_t channelMask,
690 size_t frameCount,
691 void *buffer,
692 size_t bufferSize,
693 const sp<IMemory>& sharedBuffer,
694 audio_session_t sessionId,
695 pid_t creatorPid,
696 const AttributionSourceState& attributionSource,
697 audio_output_flags_t flags,
698 track_type type,
699 audio_port_handle_t portId,
700 /** default behaviour is to start when there are as many frames
701 * ready as possible (aka. Buffer is full). */
702 size_t frameCountToBeReady,
703 float speed,
704 bool isSpatialized,
705 bool isBitPerfect) {
Andy Hung44f27182023-07-06 20:56:16 -0700706 return sp<Track>::make(thread,
Andy Hung3ff4b552023-06-26 19:20:57 -0700707 client,
708 streamType,
709 attr,
710 sampleRate,
711 format,
712 channelMask,
713 frameCount,
714 buffer,
715 bufferSize,
716 sharedBuffer,
717 sessionId,
718 creatorPid,
719 attributionSource,
720 flags,
721 type,
722 portId,
723 frameCountToBeReady,
724 speed,
725 isSpatialized,
726 isBitPerfect);
727}
728
Eric Laurent81784c32012-11-19 14:55:58 -0800729// Track constructor must be called with AudioFlinger::mLock and ThreadBase::mLock held
Andy Hung3ff4b552023-06-26 19:20:57 -0700730Track::Track(
Andy Hung44f27182023-07-06 20:56:16 -0700731 IAfPlaybackThread* thread,
Eric Laurent81784c32012-11-19 14:55:58 -0800732 const sp<Client>& client,
733 audio_stream_type_t streamType,
Kevin Rocard1f564ac2018-03-29 13:53:10 -0700734 const audio_attributes_t& attr,
Eric Laurent81784c32012-11-19 14:55:58 -0800735 uint32_t sampleRate,
736 audio_format_t format,
737 audio_channel_mask_t channelMask,
738 size_t frameCount,
Eric Laurent83b88082014-06-20 18:31:16 -0700739 void *buffer,
Andy Hung8fe68032017-06-05 16:17:51 -0700740 size_t bufferSize,
Eric Laurent81784c32012-11-19 14:55:58 -0800741 const sp<IMemory>& sharedBuffer,
Glenn Kastend848eb42016-03-08 13:42:11 -0800742 audio_session_t sessionId,
Eric Laurent09f1ed22019-04-24 17:45:17 -0700743 pid_t creatorPid,
Svet Ganov33761132021-05-13 22:51:08 +0000744 const AttributionSourceState& attributionSource,
Eric Laurent05067782016-06-01 18:27:28 -0700745 audio_output_flags_t flags,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800746 track_type type,
Kevin Rocard01c7d9e2019-09-18 11:24:52 +0100747 audio_port_handle_t portId,
jiabinf042b9b2021-05-07 23:46:28 +0000748 size_t frameCountToBeReady,
Eric Laurentb0a7bc92022-04-05 15:06:08 +0200749 float speed,
jiabinc658e452022-10-21 20:52:21 +0000750 bool isSpatialized,
751 bool isBitPerfect)
Kevin Rocard1f564ac2018-03-29 13:53:10 -0700752 : TrackBase(thread, client, attr, sampleRate, format, channelMask, frameCount,
Ytai Ben-Tsvi7dd39722019-09-05 15:14:30 -0700753 // TODO: Using unsecurePointer() has some associated security pitfalls
754 // (see declaration for details).
755 // Either document why it is safe in this case or address the
756 // issue (e.g. by copying).
757 (sharedBuffer != 0) ? sharedBuffer->unsecurePointer() : buffer,
Andy Hung8fe68032017-06-05 16:17:51 -0700758 (sharedBuffer != 0) ? sharedBuffer->size() : bufferSize,
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700759 sessionId, creatorPid,
Svet Ganov33761132021-05-13 22:51:08 +0000760 VALUE_OR_FATAL(aidl2legacy_int32_t_uid_t(attributionSource.uid)), true /*isOut*/,
Eric Laurent83b88082014-06-20 18:31:16 -0700761 (type == TYPE_PATCH) ? ( buffer == NULL ? ALLOC_LOCAL : ALLOC_NONE) : ALLOC_CBLK,
Andy Hungb68f5eb2019-12-03 16:49:17 -0800762 type,
763 portId,
764 std::string(AMEDIAMETRICS_KEY_PREFIX_AUDIO_TRACK) + std::to_string(portId)),
Andy Hung3ff4b552023-06-26 19:20:57 -0700765 mFillingStatus(FS_INVALID),
Eric Laurent81784c32012-11-19 14:55:58 -0800766 // mRetryCount initialized later when needed
767 mSharedBuffer(sharedBuffer),
768 mStreamType(streamType),
rago94a1ee82017-07-21 15:11:02 -0700769 mMainBuffer(thread->sinkBuffer()),
Eric Laurent81784c32012-11-19 14:55:58 -0800770 mAuxBuffer(NULL),
771 mAuxEffectId(0), mHasVolumeController(false),
Andy Hunge10393e2015-06-12 13:59:33 -0700772 mFrameMap(16 /* sink-frame-to-track-frame map memory */),
Ivan Lozano8cf3a072017-08-09 09:01:33 -0700773 mVolumeHandler(new media::VolumeHandler(sampleRate)),
Vlad Popa103be862023-07-10 20:27:41 -0700774 mOpPlayAudioMonitor(OpPlayAudioMonitor::createIfNeeded(thread, attributionSource, attr, id(),
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700775 streamType)),
Andy Hunge10393e2015-06-12 13:59:33 -0700776 // mSinkTimestamp
Eric Laurent81784c32012-11-19 14:55:58 -0800777 mFastIndex(-1),
Glenn Kasten5736c352012-12-04 12:12:34 -0800778 mCachedVolume(1.0),
Kevin Rocard12381092018-04-11 09:19:59 -0700779 /* The track might not play immediately after being active, similarly as if its volume was 0.
780 * When the track starts playing, its volume will be computed. */
781 mFinalVolume(0.f),
Haynes Mathew George7844f672014-01-15 12:32:55 -0800782 mResumeToStopping(false),
Eric Laurent05067782016-06-01 18:27:28 -0700783 mFlushHwPending(false),
jiabinf042b9b2021-05-07 23:46:28 +0000784 mFlags(flags),
Eric Laurentb0a7bc92022-04-05 15:06:08 +0200785 mSpeed(speed),
jiabinc658e452022-10-21 20:52:21 +0000786 mIsSpatialized(isSpatialized),
787 mIsBitPerfect(isBitPerfect)
Eric Laurent81784c32012-11-19 14:55:58 -0800788{
Eric Laurent83b88082014-06-20 18:31:16 -0700789 // client == 0 implies sharedBuffer == 0
790 ALOG_ASSERT(!(client == 0 && sharedBuffer != 0));
791
Andy Hung9d84af52018-09-12 18:03:44 -0700792 ALOGV_IF(sharedBuffer != 0, "%s(%d): sharedBuffer: %p, size: %zu",
Ytai Ben-Tsvi7dd39722019-09-05 15:14:30 -0700793 __func__, mId, sharedBuffer->unsecurePointer(), sharedBuffer->size());
Eric Laurent83b88082014-06-20 18:31:16 -0700794
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700795 if (mCblk == NULL) {
796 return;
Eric Laurent81784c32012-11-19 14:55:58 -0800797 }
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700798
Svet Ganov33761132021-05-13 22:51:08 +0000799 uid_t uid = VALUE_OR_FATAL(aidl2legacy_int32_t_uid_t(attributionSource.uid));
Andy Hung689e82c2019-08-21 17:53:17 -0700800 if (!thread->isTrackAllowed_l(channelMask, format, sessionId, uid)) {
801 ALOGE("%s(%d): no more tracks available", __func__, mId);
802 releaseCblk(); // this makes the track invalid.
803 return;
804 }
805
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700806 if (sharedBuffer == 0) {
807 mAudioTrackServerProxy = new AudioTrackServerProxy(mCblk, mBuffer, frameCount,
Eric Laurent83b88082014-06-20 18:31:16 -0700808 mFrameSize, !isExternalTrack(), sampleRate);
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700809 } else {
810 mAudioTrackServerProxy = new StaticAudioTrackServerProxy(mCblk, mBuffer, frameCount,
Kevin Rocard36862032019-10-10 10:52:19 +0100811 mFrameSize, sampleRate);
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700812 }
813 mServerProxy = mAudioTrackServerProxy;
Andy Hung3c7f47a2021-03-16 17:30:09 -0700814 mServerProxy->setStartThresholdInFrames(frameCountToBeReady); // update the Cblk value
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700815
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700816 // only allocate a fast track index if we were able to allocate a normal track name
Eric Laurent05067782016-06-01 18:27:28 -0700817 if (flags & AUDIO_OUTPUT_FLAG_FAST) {
Andy Hunga5427822015-09-11 16:15:35 -0700818 // FIXME: Not calling framesReadyIsCalledByMultipleThreads() exposes a potential
819 // race with setSyncEvent(). However, if we call it, we cannot properly start
820 // static fast tracks (SoundPool) immediately after stopping.
821 //mAudioTrackServerProxy->framesReadyIsCalledByMultipleThreads();
Andy Hung44f27182023-07-06 20:56:16 -0700822 ALOG_ASSERT(thread->fastTrackAvailMask_l() != 0);
823 const int i = __builtin_ctz(thread->fastTrackAvailMask_l());
Glenn Kastendc2c50b2016-04-21 08:13:14 -0700824 ALOG_ASSERT(0 < i && i < (int)FastMixerState::sMaxFastTracks);
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700825 // FIXME This is too eager. We allocate a fast track index before the
826 // fast track becomes active. Since fast tracks are a scarce resource,
827 // this means we are potentially denying other more important fast tracks from
828 // being created. It would be better to allocate the index dynamically.
829 mFastIndex = i;
Andy Hung44f27182023-07-06 20:56:16 -0700830 thread->fastTrackAvailMask_l() &= ~(1 << i);
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700831 }
Andy Hung8946a282018-04-19 20:04:56 -0700832
Dean Wheatley7b036912020-06-18 16:22:11 +1000833 mServerLatencySupported = checkServerLatencySupported(format, flags);
Andy Hung8946a282018-04-19 20:04:56 -0700834#ifdef TEE_SINK
835 mTee.setId(std::string("_") + std::to_string(mThreadIoHandle)
Kevin Rocard51f0e982019-02-01 19:19:11 -0800836 + "_" + std::to_string(mId) + "_T");
Andy Hung8946a282018-04-19 20:04:56 -0700837#endif
jiabin57303cc2018-12-18 15:45:57 -0800838
jiabineb3bda02020-06-30 14:07:03 -0700839 if (thread->supportsHapticPlayback()) {
840 // If the track is attached to haptic playback thread, it is potentially to have
841 // HapticGenerator effect, which will generate haptic data, on the track. In that case,
842 // external vibration is always created for all tracks attached to haptic playback thread.
jiabin57303cc2018-12-18 15:45:57 -0800843 mAudioVibrationController = new AudioVibrationController(this);
Svet Ganov33761132021-05-13 22:51:08 +0000844 std::string packageName = attributionSource.packageName.has_value() ?
845 attributionSource.packageName.value() : "";
jiabin57303cc2018-12-18 15:45:57 -0800846 mExternalVibration = new os::ExternalVibration(
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700847 mUid, packageName, mAttr, mAudioVibrationController);
jiabin57303cc2018-12-18 15:45:57 -0800848 }
Andy Hungb68f5eb2019-12-03 16:49:17 -0800849
850 // Once this item is logged by the server, the client can add properties.
Andy Hunga629bd12020-06-05 16:03:53 -0700851 const char * const traits = sharedBuffer == 0 ? "" : "static";
Andy Hung5837c7f2021-02-25 10:48:24 -0800852 mTrackMetrics.logConstructor(creatorPid, uid, id(), traits, streamType);
Eric Laurent81784c32012-11-19 14:55:58 -0800853}
854
Andy Hung3ff4b552023-06-26 19:20:57 -0700855Track::~Track()
Eric Laurent81784c32012-11-19 14:55:58 -0800856{
Andy Hung9d84af52018-09-12 18:03:44 -0700857 ALOGV("%s(%d)", __func__, mId);
Glenn Kasten0c72b242013-09-11 09:14:16 -0700858
859 // The destructor would clear mSharedBuffer,
860 // but it will not push the decremented reference count,
861 // leaving the client's IMemory dangling indefinitely.
862 // This prevents that leak.
863 if (mSharedBuffer != 0) {
864 mSharedBuffer.clear();
Glenn Kasten0c72b242013-09-11 09:14:16 -0700865 }
Eric Laurent81784c32012-11-19 14:55:58 -0800866}
867
Andy Hung3ff4b552023-06-26 19:20:57 -0700868status_t Track::initCheck() const
Glenn Kasten03003332013-08-06 15:40:54 -0700869{
870 status_t status = TrackBase::initCheck();
Andy Hungc0691382018-09-12 18:01:57 -0700871 if (status == NO_ERROR && mCblk == nullptr) {
Glenn Kasten03003332013-08-06 15:40:54 -0700872 status = NO_MEMORY;
873 }
874 return status;
875}
876
Andy Hung3ff4b552023-06-26 19:20:57 -0700877void Track::destroy()
Eric Laurent81784c32012-11-19 14:55:58 -0800878{
879 // NOTE: destroyTrack_l() can remove a strong reference to this Track
880 // by removing it from mTracks vector, so there is a risk that this Tracks's
881 // destructor is called. As the destructor needs to lock mLock,
882 // we must acquire a strong reference on this Track before locking mLock
883 // here so that the destructor is called only when exiting this function.
884 // On the other hand, as long as Track::destroy() is only called by
885 // TrackHandle destructor, the TrackHandle still holds a strong ref on
886 // this Track with its member mTrack.
887 sp<Track> keep(this);
888 { // scope for mLock
Eric Laurentaaa44472014-09-12 17:41:50 -0700889 bool wasActive = false;
Andy Hung44f27182023-07-06 20:56:16 -0700890 const sp<IAfThreadBase> thread = mThread.promote();
Eric Laurent81784c32012-11-19 14:55:58 -0800891 if (thread != 0) {
Andy Hung44f27182023-07-06 20:56:16 -0700892 Mutex::Autolock _l(thread->mutex());
893 auto* const playbackThread = thread->asIAfPlaybackThread().get();
Eric Laurentaaa44472014-09-12 17:41:50 -0700894 wasActive = playbackThread->destroyTrack_l(this);
895 }
896 if (isExternalTrack() && !wasActive) {
Eric Laurentd7fe0862018-07-14 16:48:01 -0700897 AudioSystem::releaseOutput(mPortId);
Eric Laurent81784c32012-11-19 14:55:58 -0800898 }
899 }
Kevin Rocardc43ea142019-01-31 18:17:37 -0800900 forEachTeePatchTrack([](auto patchTrack) { patchTrack->destroy(); });
Eric Laurent81784c32012-11-19 14:55:58 -0800901}
902
Andy Hung3ff4b552023-06-26 19:20:57 -0700903void Track::appendDumpHeader(String8& result) const
Eric Laurent81784c32012-11-19 14:55:58 -0800904{
Eric Laurent973db022018-11-20 14:54:31 -0800905 result.appendFormat("Type Id Active Client Session Port Id S Flags "
Kevin Rocard5f2136e2018-05-11 22:03:00 -0700906 " Format Chn mask SRate "
907 "ST Usg CT "
908 " G db L dB R dB VS dB "
jiabin5eaf0962022-12-20 20:11:38 +0000909 " Server FrmCnt FrmRdy F Underruns Flushed BitPerfect"
Kevin Rocard5f2136e2018-05-11 22:03:00 -0700910 "%s\n",
911 isServerLatencySupported() ? " Latency" : "");
Eric Laurent81784c32012-11-19 14:55:58 -0800912}
913
Andy Hung3ff4b552023-06-26 19:20:57 -0700914void Track::appendDump(String8& result, bool active) const
Eric Laurent81784c32012-11-19 14:55:58 -0800915{
Andy Hung2c6c3bb2017-06-16 14:01:45 -0700916 char trackType;
917 switch (mType) {
918 case TYPE_DEFAULT:
919 case TYPE_OUTPUT:
Andy Hungf6ab58d2018-05-25 12:50:39 -0700920 if (isStatic()) {
Andy Hung2c6c3bb2017-06-16 14:01:45 -0700921 trackType = 'S'; // static
922 } else {
923 trackType = ' '; // normal
Eric Laurentbfb1b832013-01-07 09:53:42 -0800924 }
Andy Hung2c6c3bb2017-06-16 14:01:45 -0700925 break;
926 case TYPE_PATCH:
927 trackType = 'P';
928 break;
929 default:
930 trackType = '?';
Eric Laurent81784c32012-11-19 14:55:58 -0800931 }
Andy Hung2c6c3bb2017-06-16 14:01:45 -0700932
933 if (isFastTrack()) {
Andy Hungc0691382018-09-12 18:01:57 -0700934 result.appendFormat("F%d %c %6d", mFastIndex, trackType, mId);
Andy Hung2c6c3bb2017-06-16 14:01:45 -0700935 } else {
Andy Hungc0691382018-09-12 18:01:57 -0700936 result.appendFormat(" %c %6d", trackType, mId);
Andy Hung2c6c3bb2017-06-16 14:01:45 -0700937 }
938
Eric Laurent81784c32012-11-19 14:55:58 -0800939 char nowInUnderrun;
940 switch (mObservedUnderruns.mBitFields.mMostRecent) {
941 case UNDERRUN_FULL:
942 nowInUnderrun = ' ';
943 break;
944 case UNDERRUN_PARTIAL:
945 nowInUnderrun = '<';
946 break;
947 case UNDERRUN_EMPTY:
948 nowInUnderrun = '*';
949 break;
950 default:
951 nowInUnderrun = '?';
952 break;
953 }
Andy Hungda540db2017-04-20 14:06:17 -0700954
Andy Hung2c6c3bb2017-06-16 14:01:45 -0700955 char fillingStatus;
Andy Hung3ff4b552023-06-26 19:20:57 -0700956 switch (mFillingStatus) {
Andy Hung2c6c3bb2017-06-16 14:01:45 -0700957 case FS_INVALID:
958 fillingStatus = 'I';
959 break;
960 case FS_FILLING:
961 fillingStatus = 'f';
962 break;
963 case FS_FILLED:
964 fillingStatus = 'F';
965 break;
966 case FS_ACTIVE:
967 fillingStatus = 'A';
968 break;
969 default:
970 fillingStatus = '?';
971 break;
972 }
973
974 // clip framesReadySafe to max representation in dump
975 const size_t framesReadySafe =
976 std::min(mAudioTrackServerProxy->framesReadySafe(), (size_t)99999999);
977
978 // obtain volumes
979 const gain_minifloat_packed_t vlr = mAudioTrackServerProxy->getVolumeLR();
980 const std::pair<float /* volume */, bool /* active */> vsVolume =
981 mVolumeHandler->getLastVolume();
982
983 // Our effective frame count is obtained by ServerProxy::getBufferSizeInFrames()
984 // as it may be reduced by the application.
985 const size_t bufferSizeInFrames = (size_t)mAudioTrackServerProxy->getBufferSizeInFrames();
986 // Check whether the buffer size has been modified by the app.
987 const char modifiedBufferChar = bufferSizeInFrames < mFrameCount
988 ? 'r' /* buffer reduced */: bufferSizeInFrames > mFrameCount
989 ? 'e' /* error */ : ' ' /* identical */;
990
Eric Laurent973db022018-11-20 14:54:31 -0800991 result.appendFormat("%7s %6u %7u %7u %2s 0x%03X "
Kevin Rocard5f2136e2018-05-11 22:03:00 -0700992 "%08X %08X %6u "
993 "%2u %3x %2x "
994 "%5.2g %5.2g %5.2g %5.2g%c "
jiabin5eaf0962022-12-20 20:11:38 +0000995 "%08X %6zu%c %6zu %c %9u%c %7u %10s",
Marco Nelissenb2208842014-02-07 14:00:50 -0800996 active ? "yes" : "no",
Andy Hung4ef19fa2018-05-15 19:35:29 -0700997 (mClient == 0) ? getpid() : mClient->pid(),
Andy Hung2c6c3bb2017-06-16 14:01:45 -0700998 mSessionId,
Eric Laurent973db022018-11-20 14:54:31 -0800999 mPortId,
Andy Hunge2e830f2019-12-03 12:54:46 -08001000 getTrackStateAsCodedString(),
Andy Hung2c6c3bb2017-06-16 14:01:45 -07001001 mCblk->mFlags,
1002
Eric Laurent81784c32012-11-19 14:55:58 -08001003 mFormat,
1004 mChannelMask,
Andy Hungcef2daa2018-06-01 15:31:49 -07001005 sampleRate(),
Andy Hung2c6c3bb2017-06-16 14:01:45 -07001006
1007 mStreamType,
Kevin Rocard5f2136e2018-05-11 22:03:00 -07001008 mAttr.usage,
1009 mAttr.content_type,
1010
1011 20.0 * log10(mFinalVolume),
Glenn Kastenc56f3422014-03-21 17:53:17 -07001012 20.0 * log10(float_from_gain(gain_minifloat_unpack_left(vlr))),
1013 20.0 * log10(float_from_gain(gain_minifloat_unpack_right(vlr))),
Andy Hungda540db2017-04-20 14:06:17 -07001014 20.0 * log10(vsVolume.first), // VolumeShaper(s) total volume
1015 vsVolume.second ? 'A' : ' ', // if any VolumeShapers active
Andy Hung2c6c3bb2017-06-16 14:01:45 -07001016
Glenn Kastenf20e1d82013-07-12 09:45:18 -07001017 mCblk->mServer,
Andy Hung2c6c3bb2017-06-16 14:01:45 -07001018 bufferSizeInFrames,
1019 modifiedBufferChar,
1020 framesReadySafe,
1021 fillingStatus,
Glenn Kasten82aaf942013-07-17 16:05:07 -07001022 mAudioTrackServerProxy->getUnderrunFrames(),
Andy Hung2148bf02016-11-28 19:01:02 -08001023 nowInUnderrun,
jiabin5eaf0962022-12-20 20:11:38 +00001024 (unsigned)mAudioTrackServerProxy->framesFlushed() % 10000000,
1025 isBitPerfect() ? "true" : "false"
Andy Hung2c6c3bb2017-06-16 14:01:45 -07001026 );
Andy Hungcef2daa2018-06-01 15:31:49 -07001027
1028 if (isServerLatencySupported()) {
1029 double latencyMs;
1030 bool fromTrack;
1031 if (getTrackLatencyMs(&latencyMs, &fromTrack) == OK) {
1032 // Show latency in msec, followed by 't' if from track timestamp (the most accurate)
1033 // or 'k' if estimated from kernel because track frames haven't been presented yet.
1034 result.appendFormat(" %7.2lf %c", latencyMs, fromTrack ? 't' : 'k');
Andy Hungf6ab58d2018-05-25 12:50:39 -07001035 } else {
Andy Hungcef2daa2018-06-01 15:31:49 -07001036 result.appendFormat("%10s", mCblk->mServer != 0 ? "unavail" : "new");
Andy Hungf6ab58d2018-05-25 12:50:39 -07001037 }
1038 }
1039 result.append("\n");
Eric Laurent81784c32012-11-19 14:55:58 -08001040}
1041
Andy Hung3ff4b552023-06-26 19:20:57 -07001042uint32_t Track::sampleRate() const {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001043 return mAudioTrackServerProxy->getSampleRate();
1044}
1045
Eric Laurent81784c32012-11-19 14:55:58 -08001046// AudioBufferProvider interface
Andy Hung3ff4b552023-06-26 19:20:57 -07001047status_t Track::getNextBuffer(AudioBufferProvider::Buffer* buffer)
Eric Laurent81784c32012-11-19 14:55:58 -08001048{
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001049 ServerProxy::Buffer buf;
1050 size_t desiredFrames = buffer->frameCount;
1051 buf.mFrameCount = desiredFrames;
1052 status_t status = mServerProxy->obtainBuffer(&buf);
1053 buffer->frameCount = buf.mFrameCount;
1054 buffer->raw = buf.mRaw;
Andy Hungfc629172020-06-22 10:06:23 -07001055 if (buf.mFrameCount == 0 && !isStopping() && !isStopped() && !isPaused() && !isOffloaded()) {
Andy Hung9d84af52018-09-12 18:03:44 -07001056 ALOGV("%s(%d): underrun, framesReady(%zu) < framesDesired(%zd), state: %d",
Andy Hung959b5b82021-09-24 10:46:20 -07001057 __func__, mId, buf.mFrameCount, desiredFrames, (int)mState);
Glenn Kasten82aaf942013-07-17 16:05:07 -07001058 mAudioTrackServerProxy->tallyUnderrunFrames(desiredFrames);
Phil Burk2812d9e2016-01-04 10:34:30 -08001059 } else {
1060 mAudioTrackServerProxy->tallyUnderrunFrames(0);
Eric Laurent81784c32012-11-19 14:55:58 -08001061 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001062 return status;
Eric Laurent81784c32012-11-19 14:55:58 -08001063}
1064
Andy Hung3ff4b552023-06-26 19:20:57 -07001065void Track::releaseBuffer(AudioBufferProvider::Buffer* buffer)
Kevin Rocard153f92d2018-12-18 18:33:28 -08001066{
1067 interceptBuffer(*buffer);
1068 TrackBase::releaseBuffer(buffer);
1069}
1070
1071// TODO: compensate for time shift between HW modules.
Andy Hung3ff4b552023-06-26 19:20:57 -07001072void Track::interceptBuffer(
Kevin Rocarda134b002019-02-07 18:05:31 -08001073 const AudioBufferProvider::Buffer& sourceBuffer) {
Kevin Rocard6057fa22019-02-08 14:08:07 -08001074 auto start = std::chrono::steady_clock::now();
Kevin Rocarda134b002019-02-07 18:05:31 -08001075 const size_t frameCount = sourceBuffer.frameCount;
Kevin Rocardd83b08a2019-02-27 15:05:54 -08001076 if (frameCount == 0) {
1077 return; // No audio to intercept.
1078 // Additionally PatchProxyBufferProvider::obtainBuffer (called by PathTrack::getNextBuffer)
1079 // does not allow 0 frame size request contrary to getNextBuffer
1080 }
1081 for (auto& teePatch : mTeePatches) {
Andy Hung3ff4b552023-06-26 19:20:57 -07001082 IAfPatchRecord* patchRecord = teePatch.patchRecord.get();
Mikhail Naganov8296c252019-09-25 14:59:54 -07001083 const size_t framesWritten = patchRecord->writeFrames(
1084 sourceBuffer.i8, frameCount, mFrameSize);
1085 const size_t framesLeft = frameCount - framesWritten;
Kevin Rocarda134b002019-02-07 18:05:31 -08001086 ALOGW_IF(framesLeft != 0, "%s(%d) PatchRecord %d can not provide big enough "
Andy Hung3ff4b552023-06-26 19:20:57 -07001087 "buffer %zu/%zu, dropping %zu frames", __func__, mId, patchRecord->id(),
Kevin Rocarda134b002019-02-07 18:05:31 -08001088 framesWritten, frameCount, framesLeft);
Kevin Rocard153f92d2018-12-18 18:33:28 -08001089 }
Kevin Rocard6057fa22019-02-08 14:08:07 -08001090 auto spent = ceil<std::chrono::microseconds>(std::chrono::steady_clock::now() - start);
1091 using namespace std::chrono_literals;
1092 // Average is ~20us per track, this should virtually never be logged (Logging takes >200us)
Kevin Rocard01c7d9e2019-09-18 11:24:52 +01001093 ALOGD_IF(spent > 500us, "%s: took %lldus to intercept %zu tracks", __func__,
Kevin Rocard6057fa22019-02-08 14:08:07 -08001094 spent.count(), mTeePatches.size());
Kevin Rocard153f92d2018-12-18 18:33:28 -08001095}
1096
Glenn Kasten6466c9e2013-08-23 10:54:07 -07001097// ExtendedAudioBufferProvider interface
1098
Andy Hung27876c02014-09-09 18:07:55 -07001099// framesReady() may return an approximation of the number of frames if called
1100// from a different thread than the one calling Proxy->obtainBuffer() and
1101// Proxy->releaseBuffer(). Also note there is no mutual exclusion in the
1102// AudioTrackServerProxy so be especially careful calling with FastTracks.
Andy Hung3ff4b552023-06-26 19:20:57 -07001103size_t Track::framesReady() const {
Andy Hung27876c02014-09-09 18:07:55 -07001104 if (mSharedBuffer != 0 && (isStopped() || isStopping())) {
1105 // Static tracks return zero frames immediately upon stopping (for FastTracks).
1106 // The remainder of the buffer is not drained.
1107 return 0;
1108 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001109 return mAudioTrackServerProxy->framesReady();
Eric Laurent81784c32012-11-19 14:55:58 -08001110}
1111
Andy Hung3ff4b552023-06-26 19:20:57 -07001112int64_t Track::framesReleased() const
Glenn Kasten6466c9e2013-08-23 10:54:07 -07001113{
1114 return mAudioTrackServerProxy->framesReleased();
1115}
1116
Andy Hung3ff4b552023-06-26 19:20:57 -07001117void Track::onTimestamp(const ExtendedTimestamp &timestamp)
Andy Hung6ae58432016-02-16 18:32:24 -08001118{
1119 // This call comes from a FastTrack and should be kept lockless.
1120 // The server side frames are already translated to client frames.
Andy Hung818e7a32016-02-16 18:08:07 -08001121 mAudioTrackServerProxy->setTimestamp(timestamp);
Andy Hung6ae58432016-02-16 18:32:24 -08001122
Andy Hung818e7a32016-02-16 18:08:07 -08001123 // We do not set drained here, as FastTrack timestamp may not go to very last frame.
Andy Hungcef2daa2018-06-01 15:31:49 -07001124
1125 // Compute latency.
1126 // TODO: Consider whether the server latency may be passed in by FastMixer
1127 // as a constant for all active FastTracks.
1128 const double latencyMs = timestamp.getOutputServerLatencyMs(sampleRate());
1129 mServerLatencyFromTrack.store(true);
1130 mServerLatencyMs.store(latencyMs);
Andy Hung6ae58432016-02-16 18:32:24 -08001131}
1132
Eric Laurent81784c32012-11-19 14:55:58 -08001133// Don't call for fast tracks; the framesReady() could result in priority inversion
Andy Hung3ff4b552023-06-26 19:20:57 -07001134bool Track::isReady() const {
1135 if (mFillingStatus != FS_FILLING || isStopped() || isPausing()) {
Krishnankutty Kolathappilly8d6c2922014-02-04 16:23:42 -08001136 return true;
1137 }
1138
Eric Laurent16498512014-03-17 17:22:08 -07001139 if (isStopping()) {
1140 if (framesReady() > 0) {
Andy Hung3ff4b552023-06-26 19:20:57 -07001141 mFillingStatus = FS_FILLED;
Eric Laurent16498512014-03-17 17:22:08 -07001142 }
Eric Laurent81784c32012-11-19 14:55:58 -08001143 return true;
1144 }
1145
Kevin Rocard01c7d9e2019-09-18 11:24:52 +01001146 size_t bufferSizeInFrames = mServerProxy->getBufferSizeInFrames();
Andy Hung3c7f47a2021-03-16 17:30:09 -07001147 // Note: mServerProxy->getStartThresholdInFrames() is clamped.
1148 const size_t startThresholdInFrames = mServerProxy->getStartThresholdInFrames();
1149 const size_t framesToBeReady = std::clamp( // clamp again to validate client values.
1150 std::min(startThresholdInFrames, bufferSizeInFrames), size_t(1), mFrameCount);
Kevin Rocard01c7d9e2019-09-18 11:24:52 +01001151
1152 if (framesReady() >= framesToBeReady || (mCblk->mFlags & CBLK_FORCEREADY)) {
1153 ALOGV("%s(%d): consider track ready with %zu/%zu, target was %zu)",
1154 __func__, mId, framesReady(), bufferSizeInFrames, framesToBeReady);
Andy Hung3ff4b552023-06-26 19:20:57 -07001155 mFillingStatus = FS_FILLED;
Glenn Kasten96f60d82013-07-12 10:21:18 -07001156 android_atomic_and(~CBLK_FORCEREADY, &mCblk->mFlags);
Eric Laurent81784c32012-11-19 14:55:58 -08001157 return true;
1158 }
1159 return false;
1160}
1161
Andy Hung3ff4b552023-06-26 19:20:57 -07001162status_t Track::start(AudioSystem::sync_event_t event __unused,
Glenn Kastend848eb42016-03-08 13:42:11 -08001163 audio_session_t triggerSession __unused)
Eric Laurent81784c32012-11-19 14:55:58 -08001164{
1165 status_t status = NO_ERROR;
Andy Hungc0691382018-09-12 18:01:57 -07001166 ALOGV("%s(%d): calling pid %d session %d",
1167 __func__, mId, IPCThreadState::self()->getCallingPid(), mSessionId);
Eric Laurent81784c32012-11-19 14:55:58 -08001168
Andy Hung44f27182023-07-06 20:56:16 -07001169 const sp<IAfThreadBase> thread = mThread.promote();
Eric Laurent81784c32012-11-19 14:55:58 -08001170 if (thread != 0) {
Eric Laurent813e2a72013-08-31 12:59:48 -07001171 if (isOffloaded()) {
Andy Hung2ac52f12023-08-28 18:36:53 -07001172 audio_utils::lock_guard _laf(thread->afThreadCallback()->mutex());
Andy Hung44f27182023-07-06 20:56:16 -07001173 Mutex::Autolock _lth(thread->mutex());
Andy Hungbd72c542023-06-20 18:56:17 -07001174 sp<IAfEffectChain> ec = thread->getEffectChain_l(mSessionId);
Andy Hung2cbc2722023-07-17 17:05:00 -07001175 if (thread->afThreadCallback()->isNonOffloadableGlobalEffectEnabled_l() ||
Eric Laurent5baf2af2013-09-12 17:37:00 -07001176 (ec != 0 && ec->isNonOffloadableEnabled())) {
Eric Laurent813e2a72013-08-31 12:59:48 -07001177 invalidate();
1178 return PERMISSION_DENIED;
1179 }
1180 }
Andy Hung44f27182023-07-06 20:56:16 -07001181 Mutex::Autolock _lth(thread->mutex());
Eric Laurent81784c32012-11-19 14:55:58 -08001182 track_state state = mState;
1183 // here the track could be either new, or restarted
1184 // in both cases "unstop" the track
Eric Laurentbfb1b832013-01-07 09:53:42 -08001185
Krishnankutty Kolathappilly8d6c2922014-02-04 16:23:42 -08001186 // initial state-stopping. next state-pausing.
1187 // What if resume is called ?
1188
Zhou Song1ed46a22020-08-17 15:36:56 +08001189 if (state == FLUSHED) {
1190 // avoid underrun glitches when starting after flush
1191 reset();
1192 }
1193
kuowei.li576f1362021-05-11 18:02:32 +08001194 // clear mPauseHwPending because of pause (and possibly flush) during underrun.
1195 mPauseHwPending = false;
Krishnankutty Kolathappilly8d6c2922014-02-04 16:23:42 -08001196 if (state == PAUSED || state == PAUSING) {
Eric Laurentbfb1b832013-01-07 09:53:42 -08001197 if (mResumeToStopping) {
1198 // happened we need to resume to STOPPING_1
1199 mState = TrackBase::STOPPING_1;
Andy Hungc0691382018-09-12 18:01:57 -07001200 ALOGV("%s(%d): PAUSED => STOPPING_1 on thread %d",
1201 __func__, mId, (int)mThreadIoHandle);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001202 } else {
1203 mState = TrackBase::RESUMING;
Andy Hungc0691382018-09-12 18:01:57 -07001204 ALOGV("%s(%d): PAUSED => RESUMING on thread %d",
1205 __func__, mId, (int)mThreadIoHandle);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001206 }
Eric Laurent81784c32012-11-19 14:55:58 -08001207 } else {
1208 mState = TrackBase::ACTIVE;
Andy Hungc0691382018-09-12 18:01:57 -07001209 ALOGV("%s(%d): ? => ACTIVE on thread %d",
1210 __func__, mId, (int)mThreadIoHandle);
Eric Laurent81784c32012-11-19 14:55:58 -08001211 }
1212
Andy Hung44f27182023-07-06 20:56:16 -07001213 auto* const playbackThread = thread->asIAfPlaybackThread().get();
yucliu91503922022-07-20 17:40:39 -07001214
1215 // states to reset position info for pcm tracks
1216 if (audio_is_linear_pcm(mFormat)
Andy Hunge10393e2015-06-12 13:59:33 -07001217 && (state == IDLE || state == STOPPED || state == FLUSHED)) {
1218 mFrameMap.reset();
yucliu91503922022-07-20 17:40:39 -07001219
1220 if (!isFastTrack() && (isDirect() || isOffloaded())) {
1221 // Start point of track -> sink frame map. If the HAL returns a
1222 // frame position smaller than the first written frame in
1223 // updateTrackFrameInfo, the timestamp can be interpolated
1224 // instead of using a larger value.
1225 mFrameMap.push(mAudioTrackServerProxy->framesReleased(),
1226 playbackThread->framesWritten());
1227 }
Andy Hunge10393e2015-06-12 13:59:33 -07001228 }
Haynes Mathew George240934b2015-03-11 18:25:50 -07001229 if (isFastTrack()) {
1230 // refresh fast track underruns on start because that field is never cleared
1231 // by the fast mixer; furthermore, the same track can be recycled, i.e. start
1232 // after stop.
1233 mObservedUnderruns = playbackThread->getFastTrackUnderruns(mFastIndex);
1234 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08001235 status = playbackThread->addTrack_l(this);
jiabina84c3d32022-12-02 18:59:55 +00001236 if (status == INVALID_OPERATION || status == PERMISSION_DENIED || status == DEAD_OBJECT) {
Eric Laurent81784c32012-11-19 14:55:58 -08001237 triggerEvents(AudioSystem::SYNC_EVENT_PRESENTATION_COMPLETE);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001238 // restore previous state if start was rejected by policy manager
jiabina84c3d32022-12-02 18:59:55 +00001239 if (status == PERMISSION_DENIED || status == DEAD_OBJECT) {
Eric Laurentbfb1b832013-01-07 09:53:42 -08001240 mState = state;
1241 }
1242 }
Andy Hung1d3556d2018-03-29 16:30:14 -07001243
Andy Hungb68f5eb2019-12-03 16:49:17 -08001244 // Audio timing metrics are computed a few mix cycles after starting.
1245 {
1246 mLogStartCountdown = LOG_START_COUNTDOWN;
1247 mLogStartTimeNs = systemTime();
1248 mLogStartFrames = mAudioTrackServerProxy->getTimestamp()
Andy Hung62921122020-05-18 10:47:31 -07001249 .mPosition[ExtendedTimestamp::LOCATION_KERNEL];
1250 mLogLatencyMs = 0.;
Andy Hungb68f5eb2019-12-03 16:49:17 -08001251 }
Andy Hungcb6cc752022-05-19 19:24:51 -07001252 mLogForceVolumeUpdate = true; // at least one volume logged for metrics when starting.
Andy Hungb68f5eb2019-12-03 16:49:17 -08001253
Andy Hung1d3556d2018-03-29 16:30:14 -07001254 if (status == NO_ERROR || status == ALREADY_EXISTS) {
1255 // for streaming tracks, remove the buffer read stop limit.
1256 mAudioTrackServerProxy->start();
1257 }
1258
Eric Laurentbfb1b832013-01-07 09:53:42 -08001259 // track was already in the active list, not a problem
1260 if (status == ALREADY_EXISTS) {
1261 status = NO_ERROR;
Glenn Kasten12022ff2013-10-17 11:32:39 -07001262 } else {
1263 // Acknowledge any pending flush(), so that subsequent new data isn't discarded.
1264 // It is usually unsafe to access the server proxy from a binder thread.
1265 // But in this case we know the mixer thread (whether normal mixer or fast mixer)
1266 // isn't looking at this track yet: we still hold the normal mixer thread lock,
1267 // and for fast tracks the track is not yet in the fast mixer thread's active set.
Andy Hunge6fb82a2015-09-09 14:39:02 -07001268 // For static tracks, this is used to acknowledge change in position or loop.
Eric Laurent564d1442015-09-09 12:26:52 -07001269 ServerProxy::Buffer buffer;
1270 buffer.mFrameCount = 1;
1271 (void) mAudioTrackServerProxy->obtainBuffer(&buffer, true /*ackFlush*/);
Eric Laurent81784c32012-11-19 14:55:58 -08001272 }
1273 } else {
1274 status = BAD_VALUE;
1275 }
Kevin Rocardc43ea142019-01-31 18:17:37 -08001276 if (status == NO_ERROR) {
1277 forEachTeePatchTrack([](auto patchTrack) { patchTrack->start(); });
Jean-Michel Trivi16395ca2022-12-11 22:10:11 +00001278
1279 // send format to AudioManager for playback activity monitoring
Andy Hung2cbc2722023-07-17 17:05:00 -07001280 const sp<IAudioManager> audioManager =
1281 thread->afThreadCallback()->getOrCreateAudioManager();
Jean-Michel Trivi16395ca2022-12-11 22:10:11 +00001282 if (audioManager && mPortId != AUDIO_PORT_HANDLE_NONE) {
1283 std::unique_ptr<os::PersistableBundle> bundle =
1284 std::make_unique<os::PersistableBundle>();
1285 bundle->putBoolean(String16(kExtraPlayerEventSpatializedKey),
1286 isSpatialized());
1287 bundle->putInt(String16(kExtraPlayerEventSampleRateKey), mSampleRate);
1288 bundle->putInt(String16(kExtraPlayerEventChannelMaskKey), mChannelMask);
1289 status_t result = audioManager->portEvent(mPortId,
1290 PLAYER_UPDATE_FORMAT, bundle);
1291 if (result != OK) {
1292 ALOGE("%s: unable to send playback format for port ID %d, status error %d",
1293 __func__, mPortId, result);
1294 }
1295 }
Kevin Rocardc43ea142019-01-31 18:17:37 -08001296 }
Eric Laurent81784c32012-11-19 14:55:58 -08001297 return status;
1298}
1299
Andy Hung3ff4b552023-06-26 19:20:57 -07001300void Track::stop()
Eric Laurent81784c32012-11-19 14:55:58 -08001301{
Andy Hungc0691382018-09-12 18:01:57 -07001302 ALOGV("%s(%d): calling pid %d", __func__, mId, IPCThreadState::self()->getCallingPid());
Andy Hung44f27182023-07-06 20:56:16 -07001303 const sp<IAfThreadBase> thread = mThread.promote();
Eric Laurent81784c32012-11-19 14:55:58 -08001304 if (thread != 0) {
Andy Hung44f27182023-07-06 20:56:16 -07001305 Mutex::Autolock _l(thread->mutex());
Eric Laurent81784c32012-11-19 14:55:58 -08001306 track_state state = mState;
1307 if (state == RESUMING || state == ACTIVE || state == PAUSING || state == PAUSED) {
1308 // If the track is not active (PAUSED and buffers full), flush buffers
Andy Hung44f27182023-07-06 20:56:16 -07001309 auto* const playbackThread = thread->asIAfPlaybackThread().get();
1310 if (!playbackThread->isTrackActive(this)) {
Eric Laurent81784c32012-11-19 14:55:58 -08001311 reset();
1312 mState = STOPPED;
Eric Laurentab5cdba2014-06-09 17:22:27 -07001313 } else if (!isFastTrack() && !isOffloaded() && !isDirect()) {
Eric Laurent81784c32012-11-19 14:55:58 -08001314 mState = STOPPED;
1315 } else {
Eric Laurentbfb1b832013-01-07 09:53:42 -08001316 // For fast tracks prepareTracks_l() will set state to STOPPING_2
1317 // presentation is complete
1318 // For an offloaded track this starts a drain and state will
1319 // move to STOPPING_2 when drain completes and then STOPPED
Eric Laurent81784c32012-11-19 14:55:58 -08001320 mState = STOPPING_1;
Eric Laurente93cc032016-05-05 10:15:10 -07001321 if (isOffloaded()) {
Andy Hung44f27182023-07-06 20:56:16 -07001322 mRetryCount = IAfPlaybackThread::kMaxTrackStopRetriesOffload;
Eric Laurente93cc032016-05-05 10:15:10 -07001323 }
Eric Laurent81784c32012-11-19 14:55:58 -08001324 }
Eric Laurentb369caf2015-03-30 20:51:47 -07001325 playbackThread->broadcast_l();
Andy Hungc0691382018-09-12 18:01:57 -07001326 ALOGV("%s(%d): not stopping/stopped => stopping/stopped on thread %d",
1327 __func__, mId, (int)mThreadIoHandle);
Eric Laurent81784c32012-11-19 14:55:58 -08001328 }
Eric Laurent81784c32012-11-19 14:55:58 -08001329 }
Kevin Rocardc43ea142019-01-31 18:17:37 -08001330 forEachTeePatchTrack([](auto patchTrack) { patchTrack->stop(); });
Eric Laurent81784c32012-11-19 14:55:58 -08001331}
1332
Andy Hung3ff4b552023-06-26 19:20:57 -07001333void Track::pause()
Eric Laurent81784c32012-11-19 14:55:58 -08001334{
Andy Hungc0691382018-09-12 18:01:57 -07001335 ALOGV("%s(%d): calling pid %d", __func__, mId, IPCThreadState::self()->getCallingPid());
Andy Hung44f27182023-07-06 20:56:16 -07001336 const sp<IAfThreadBase> thread = mThread.promote();
Eric Laurent81784c32012-11-19 14:55:58 -08001337 if (thread != 0) {
Andy Hung44f27182023-07-06 20:56:16 -07001338 Mutex::Autolock _l(thread->mutex());
1339 auto* const playbackThread = thread->asIAfPlaybackThread().get();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001340 switch (mState) {
1341 case STOPPING_1:
1342 case STOPPING_2:
1343 if (!isOffloaded()) {
1344 /* nothing to do if track is not offloaded */
1345 break;
1346 }
1347
1348 // Offloaded track was draining, we need to carry on draining when resumed
1349 mResumeToStopping = true;
Chih-Hung Hsieh2b487032018-09-13 14:16:02 -07001350 FALLTHROUGH_INTENDED;
Eric Laurentbfb1b832013-01-07 09:53:42 -08001351 case ACTIVE:
1352 case RESUMING:
Eric Laurent81784c32012-11-19 14:55:58 -08001353 mState = PAUSING;
Andy Hungc0691382018-09-12 18:01:57 -07001354 ALOGV("%s(%d): ACTIVE/RESUMING => PAUSING on thread %d",
1355 __func__, mId, (int)mThreadIoHandle);
Kuowei Li23666472021-01-20 10:23:25 +08001356 if (isOffloadedOrDirect()) {
1357 mPauseHwPending = true;
1358 }
Eric Laurentede6c3b2013-09-19 14:37:46 -07001359 playbackThread->broadcast_l();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001360 break;
Eric Laurent81784c32012-11-19 14:55:58 -08001361
Eric Laurentbfb1b832013-01-07 09:53:42 -08001362 default:
1363 break;
Eric Laurent81784c32012-11-19 14:55:58 -08001364 }
1365 }
Kevin Rocardc43ea142019-01-31 18:17:37 -08001366 // Pausing the TeePatch to avoid a glitch on underrun, at the cost of buffered audio loss.
1367 forEachTeePatchTrack([](auto patchTrack) { patchTrack->pause(); });
Eric Laurent81784c32012-11-19 14:55:58 -08001368}
1369
Andy Hung3ff4b552023-06-26 19:20:57 -07001370void Track::flush()
Eric Laurent81784c32012-11-19 14:55:58 -08001371{
Andy Hungc0691382018-09-12 18:01:57 -07001372 ALOGV("%s(%d)", __func__, mId);
Andy Hung44f27182023-07-06 20:56:16 -07001373 const sp<IAfThreadBase> thread = mThread.promote();
Eric Laurent81784c32012-11-19 14:55:58 -08001374 if (thread != 0) {
Andy Hung44f27182023-07-06 20:56:16 -07001375 Mutex::Autolock _l(thread->mutex());
1376 auto* const playbackThread = thread->asIAfPlaybackThread().get();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001377
Phil Burk4bb650b2016-09-09 12:11:17 -07001378 // Flush the ring buffer now if the track is not active in the PlaybackThread.
1379 // Otherwise the flush would not be done until the track is resumed.
1380 // Requires FastTrack removal be BLOCK_UNTIL_ACKED
Andy Hung44f27182023-07-06 20:56:16 -07001381 if (!playbackThread->isTrackActive(this)) {
Phil Burk4bb650b2016-09-09 12:11:17 -07001382 (void)mServerProxy->flushBufferIfNeeded();
1383 }
1384
Eric Laurentbfb1b832013-01-07 09:53:42 -08001385 if (isOffloaded()) {
1386 // If offloaded we allow flush during any state except terminated
1387 // and keep the track active to avoid problems if user is seeking
1388 // rapidly and underlying hardware has a significant delay handling
1389 // a pause
1390 if (isTerminated()) {
1391 return;
1392 }
1393
Andy Hung9d84af52018-09-12 18:03:44 -07001394 ALOGV("%s(%d): offload flush", __func__, mId);
Eric Laurent81784c32012-11-19 14:55:58 -08001395 reset();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001396
1397 if (mState == STOPPING_1 || mState == STOPPING_2) {
Andy Hung9d84af52018-09-12 18:03:44 -07001398 ALOGV("%s(%d): flushed in STOPPING_1 or 2 state, change state to ACTIVE",
1399 __func__, mId);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001400 mState = ACTIVE;
1401 }
1402
Haynes Mathew George7844f672014-01-15 12:32:55 -08001403 mFlushHwPending = true;
Eric Laurentbfb1b832013-01-07 09:53:42 -08001404 mResumeToStopping = false;
1405 } else {
1406 if (mState != STOPPING_1 && mState != STOPPING_2 && mState != STOPPED &&
1407 mState != PAUSED && mState != PAUSING && mState != IDLE && mState != FLUSHED) {
1408 return;
1409 }
1410 // No point remaining in PAUSED state after a flush => go to
1411 // FLUSHED state
1412 mState = FLUSHED;
1413 // do not reset the track if it is still in the process of being stopped or paused.
1414 // this will be done by prepareTracks_l() when the track is stopped.
1415 // prepareTracks_l() will see mState == FLUSHED, then
1416 // remove from active track list, reset(), and trigger presentation complete
Eric Laurentd1f69b02014-12-15 14:33:13 -08001417 if (isDirect()) {
1418 mFlushHwPending = true;
1419 }
Andy Hung44f27182023-07-06 20:56:16 -07001420 if (!playbackThread->isTrackActive(this)) {
Eric Laurentbfb1b832013-01-07 09:53:42 -08001421 reset();
1422 }
Eric Laurent81784c32012-11-19 14:55:58 -08001423 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08001424 // Prevent flush being lost if the track is flushed and then resumed
1425 // before mixer thread can run. This is important when offloading
1426 // because the hardware buffer could hold a large amount of audio
Eric Laurentede6c3b2013-09-19 14:37:46 -07001427 playbackThread->broadcast_l();
Eric Laurent81784c32012-11-19 14:55:58 -08001428 }
Kevin Rocardc43ea142019-01-31 18:17:37 -08001429 // Flush the Tee to avoid on resume playing old data and glitching on the transition to new data
1430 forEachTeePatchTrack([](auto patchTrack) { patchTrack->flush(); });
Eric Laurent81784c32012-11-19 14:55:58 -08001431}
1432
Haynes Mathew George7844f672014-01-15 12:32:55 -08001433// must be called with thread lock held
Andy Hung3ff4b552023-06-26 19:20:57 -07001434void Track::flushAck()
Haynes Mathew George7844f672014-01-15 12:32:55 -08001435{
Andy Hung71ba4b32022-10-06 12:09:49 -07001436 if (!isOffloaded() && !isDirect()) {
Haynes Mathew George7844f672014-01-15 12:32:55 -08001437 return;
Andy Hung71ba4b32022-10-06 12:09:49 -07001438 }
Haynes Mathew George7844f672014-01-15 12:32:55 -08001439
Phil Burk4bb650b2016-09-09 12:11:17 -07001440 // Clear the client ring buffer so that the app can prime the buffer while paused.
1441 // Otherwise it might not get cleared until playback is resumed and obtainBuffer() is called.
1442 mServerProxy->flushBufferIfNeeded();
1443
Haynes Mathew George7844f672014-01-15 12:32:55 -08001444 mFlushHwPending = false;
1445}
1446
Andy Hung3ff4b552023-06-26 19:20:57 -07001447void Track::pauseAck()
Kuowei Li23666472021-01-20 10:23:25 +08001448{
1449 mPauseHwPending = false;
1450}
1451
Andy Hung3ff4b552023-06-26 19:20:57 -07001452void Track::reset()
Eric Laurent81784c32012-11-19 14:55:58 -08001453{
1454 // Do not reset twice to avoid discarding data written just after a flush and before
1455 // the audioflinger thread detects the track is stopped.
1456 if (!mResetDone) {
Eric Laurent81784c32012-11-19 14:55:58 -08001457 // Force underrun condition to avoid false underrun callback until first data is
1458 // written to buffer
Glenn Kasten96f60d82013-07-12 10:21:18 -07001459 android_atomic_and(~CBLK_FORCEREADY, &mCblk->mFlags);
Andy Hung3ff4b552023-06-26 19:20:57 -07001460 mFillingStatus = FS_FILLING;
Eric Laurent81784c32012-11-19 14:55:58 -08001461 mResetDone = true;
1462 if (mState == FLUSHED) {
1463 mState = IDLE;
1464 }
1465 }
1466}
1467
Andy Hung3ff4b552023-06-26 19:20:57 -07001468status_t Track::setParameters(const String8& keyValuePairs)
Eric Laurentbfb1b832013-01-07 09:53:42 -08001469{
Andy Hung44f27182023-07-06 20:56:16 -07001470 const sp<IAfThreadBase> thread = mThread.promote();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001471 if (thread == 0) {
Andy Hung9d84af52018-09-12 18:03:44 -07001472 ALOGE("%s(%d): thread is dead", __func__, mId);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001473 return FAILED_TRANSACTION;
Andy Hung44f27182023-07-06 20:56:16 -07001474 } else if (thread->type() == IAfThreadBase::DIRECT
1475 || thread->type() == IAfThreadBase::OFFLOAD) {
Eric Laurentbfb1b832013-01-07 09:53:42 -08001476 return thread->setParameters(keyValuePairs);
1477 } else {
1478 return PERMISSION_DENIED;
1479 }
1480}
1481
Andy Hung3ff4b552023-06-26 19:20:57 -07001482status_t Track::selectPresentation(int presentationId,
Mikhail Naganovac917ac2018-11-28 14:03:52 -08001483 int programId) {
Andy Hung44f27182023-07-06 20:56:16 -07001484 const sp<IAfThreadBase> thread = mThread.promote();
Mikhail Naganovac917ac2018-11-28 14:03:52 -08001485 if (thread == 0) {
1486 ALOGE("thread is dead");
1487 return FAILED_TRANSACTION;
Andy Hung44f27182023-07-06 20:56:16 -07001488 } else if (thread->type() == IAfThreadBase::DIRECT
1489 || thread->type() == IAfThreadBase::OFFLOAD) {
1490 auto directOutputThread = thread->asIAfDirectOutputThread().get();
Mikhail Naganovac917ac2018-11-28 14:03:52 -08001491 return directOutputThread->selectPresentation(presentationId, programId);
1492 }
1493 return INVALID_OPERATION;
1494}
1495
Andy Hung3ff4b552023-06-26 19:20:57 -07001496VolumeShaper::Status Track::applyVolumeShaper(
Andy Hung9fc8b5c2017-01-24 13:36:48 -08001497 const sp<VolumeShaper::Configuration>& configuration,
1498 const sp<VolumeShaper::Operation>& operation)
1499{
Andy Hungee86cee2022-12-13 19:19:53 -08001500 VolumeShaper::Status status = mVolumeHandler->applyVolumeShaper(configuration, operation);
Andy Hung10cbff12017-02-21 17:30:14 -08001501
1502 if (isOffloadedOrDirect()) {
1503 // Signal thread to fetch new volume.
Andy Hung44f27182023-07-06 20:56:16 -07001504 const sp<IAfThreadBase> thread = mThread.promote();
Andy Hung10cbff12017-02-21 17:30:14 -08001505 if (thread != 0) {
Andy Hung44f27182023-07-06 20:56:16 -07001506 Mutex::Autolock _l(thread->mutex());
Andy Hung10cbff12017-02-21 17:30:14 -08001507 thread->broadcast_l();
1508 }
1509 }
1510 return status;
Andy Hung9fc8b5c2017-01-24 13:36:48 -08001511}
1512
Andy Hung3ff4b552023-06-26 19:20:57 -07001513sp<VolumeShaper::State> Track::getVolumeShaperState(int id) const
Andy Hung9fc8b5c2017-01-24 13:36:48 -08001514{
1515 // Note: We don't check if Thread exists.
1516
1517 // mVolumeHandler is thread safe.
1518 return mVolumeHandler->getVolumeShaperState(id);
1519}
1520
Andy Hung3ff4b552023-06-26 19:20:57 -07001521void Track::setFinalVolume(float volumeLeft, float volumeRight)
Kevin Rocard12381092018-04-11 09:19:59 -07001522{
jiabin76d94692022-12-15 21:51:21 +00001523 mFinalVolumeLeft = volumeLeft;
1524 mFinalVolumeRight = volumeRight;
1525 const float volume = (volumeLeft + volumeRight) * 0.5f;
Kevin Rocard12381092018-04-11 09:19:59 -07001526 if (mFinalVolume != volume) { // Compare to an epsilon if too many meaningless updates
1527 mFinalVolume = volume;
1528 setMetadataHasChanged();
Andy Hungcb6cc752022-05-19 19:24:51 -07001529 mLogForceVolumeUpdate = true;
1530 }
1531 if (mLogForceVolumeUpdate) {
1532 mLogForceVolumeUpdate = false;
1533 mTrackMetrics.logVolume(mFinalVolume);
Kevin Rocard12381092018-04-11 09:19:59 -07001534 }
1535}
1536
Andy Hung3ff4b552023-06-26 19:20:57 -07001537void Track::copyMetadataTo(MetadataInserter& backInserter) const
Kevin Rocard12381092018-04-11 09:19:59 -07001538{
Eric Laurent49e39282022-06-24 18:42:45 +02001539 // Do not forward metadata for PatchTrack with unspecified stream type
1540 if (mStreamType == AUDIO_STREAM_PATCH) {
1541 return;
1542 }
1543
Eric Laurent94579172020-11-20 18:41:04 +01001544 playback_track_metadata_v7_t metadata;
1545 metadata.base = {
Kevin Rocard12381092018-04-11 09:19:59 -07001546 .usage = mAttr.usage,
1547 .content_type = mAttr.content_type,
1548 .gain = mFinalVolume,
1549 };
Eric Laurentfdf99502021-11-26 19:05:02 +01001550
1551 // When attributes are undefined, derive default values from stream type.
1552 // See AudioAttributes.java, usageForStreamType() and Builder.setInternalLegacyStreamType()
1553 if (mAttr.usage == AUDIO_USAGE_UNKNOWN) {
1554 switch (mStreamType) {
1555 case AUDIO_STREAM_VOICE_CALL:
1556 metadata.base.usage = AUDIO_USAGE_VOICE_COMMUNICATION;
1557 metadata.base.content_type = AUDIO_CONTENT_TYPE_SPEECH;
1558 break;
1559 case AUDIO_STREAM_SYSTEM:
1560 metadata.base.usage = AUDIO_USAGE_ASSISTANCE_SONIFICATION;
1561 metadata.base.content_type = AUDIO_CONTENT_TYPE_SONIFICATION;
1562 break;
1563 case AUDIO_STREAM_RING:
1564 metadata.base.usage = AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE;
1565 metadata.base.content_type = AUDIO_CONTENT_TYPE_SONIFICATION;
1566 break;
1567 case AUDIO_STREAM_MUSIC:
1568 metadata.base.usage = AUDIO_USAGE_MEDIA;
1569 metadata.base.content_type = AUDIO_CONTENT_TYPE_MUSIC;
1570 break;
1571 case AUDIO_STREAM_ALARM:
1572 metadata.base.usage = AUDIO_USAGE_ALARM;
1573 metadata.base.content_type = AUDIO_CONTENT_TYPE_SONIFICATION;
1574 break;
1575 case AUDIO_STREAM_NOTIFICATION:
1576 metadata.base.usage = AUDIO_USAGE_NOTIFICATION;
1577 metadata.base.content_type = AUDIO_CONTENT_TYPE_SONIFICATION;
1578 break;
1579 case AUDIO_STREAM_DTMF:
1580 metadata.base.usage = AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING;
1581 metadata.base.content_type = AUDIO_CONTENT_TYPE_SONIFICATION;
1582 break;
1583 case AUDIO_STREAM_ACCESSIBILITY:
1584 metadata.base.usage = AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY;
1585 metadata.base.content_type = AUDIO_CONTENT_TYPE_SPEECH;
1586 break;
1587 case AUDIO_STREAM_ASSISTANT:
1588 metadata.base.usage = AUDIO_USAGE_ASSISTANT;
1589 metadata.base.content_type = AUDIO_CONTENT_TYPE_SPEECH;
1590 break;
1591 case AUDIO_STREAM_REROUTING:
1592 metadata.base.usage = AUDIO_USAGE_VIRTUAL_SOURCE;
1593 // unknown content type
1594 break;
1595 case AUDIO_STREAM_CALL_ASSISTANT:
1596 metadata.base.usage = AUDIO_USAGE_CALL_ASSISTANT;
1597 metadata.base.content_type = AUDIO_CONTENT_TYPE_SPEECH;
1598 break;
1599 default:
1600 break;
1601 }
1602 }
1603
Eric Laurent78b07302022-10-07 16:20:34 +02001604 metadata.channel_mask = mChannelMask;
Eric Laurent94579172020-11-20 18:41:04 +01001605 strncpy(metadata.tags, mAttr.tags, AUDIO_ATTRIBUTES_TAGS_MAX_SIZE);
1606 *backInserter++ = metadata;
Kevin Rocard12381092018-04-11 09:19:59 -07001607}
1608
Andy Hung3ff4b552023-06-26 19:20:57 -07001609void Track::updateTeePatches() {
Jiabin Huangfb476842022-12-06 03:18:10 +00001610 if (mTeePatchesToUpdate.has_value()) {
1611 forEachTeePatchTrack([](auto patchTrack) { patchTrack->destroy(); });
1612 mTeePatches = mTeePatchesToUpdate.value();
1613 if (mState == TrackBase::ACTIVE || mState == TrackBase::RESUMING ||
1614 mState == TrackBase::STOPPING_1) {
1615 forEachTeePatchTrack([](auto patchTrack) { patchTrack->start(); });
1616 }
1617 mTeePatchesToUpdate.reset();
jiabinf042b9b2021-05-07 23:46:28 +00001618 }
Kevin Rocard153f92d2018-12-18 18:33:28 -08001619}
1620
Andy Hung34645da2023-07-14 11:45:38 -07001621void Track::setTeePatchesToUpdate(TeePatches teePatchesToUpdate) {
Jiabin Huangfb476842022-12-06 03:18:10 +00001622 ALOGW_IF(mTeePatchesToUpdate.has_value(),
1623 "%s, existing tee patches to update will be ignored", __func__);
1624 mTeePatchesToUpdate = std::move(teePatchesToUpdate);
1625}
1626
Vlad Popae8d99472022-06-30 16:02:48 +02001627// must be called with player thread lock held
Andy Hung3ff4b552023-06-26 19:20:57 -07001628void Track::processMuteEvent_l(const sp<
Vlad Popae8d99472022-06-30 16:02:48 +02001629 IAudioManager>& audioManager, mute_state_t muteState)
1630{
1631 if (mMuteState == muteState) {
1632 // mute state did not change, do nothing
1633 return;
1634 }
1635
1636 status_t result = UNKNOWN_ERROR;
1637 if (audioManager && mPortId != AUDIO_PORT_HANDLE_NONE) {
1638 if (mMuteEventExtras == nullptr) {
1639 mMuteEventExtras = std::make_unique<os::PersistableBundle>();
1640 }
1641 mMuteEventExtras->putInt(String16(kExtraPlayerEventMuteKey),
1642 static_cast<int>(muteState));
1643
1644 result = audioManager->portEvent(mPortId,
1645 PLAYER_UPDATE_MUTED,
1646 mMuteEventExtras);
1647 }
1648
1649 if (result == OK) {
1650 mMuteState = muteState;
1651 } else {
1652 ALOGW("%s(%d): cannot process mute state for port ID %d, status error %d",
1653 __func__,
1654 id(),
1655 mPortId,
1656 result);
Andy Hung818e7a32016-02-16 18:08:07 -08001657 }
Glenn Kastenfe346c72013-08-30 13:28:22 -07001658}
Glenn Kasten573d80a2013-08-26 09:36:23 -07001659
Andy Hung3ff4b552023-06-26 19:20:57 -07001660status_t Track::getTimestamp(AudioTimestamp& timestamp)
Eric Laurent81784c32012-11-19 14:55:58 -08001661{
1662 if (!isOffloaded() && !isDirect()) {
Glenn Kasten573d80a2013-08-26 09:36:23 -07001663 return INVALID_OPERATION; // normal tracks handled through SSQ
1664 }
Andy Hung44f27182023-07-06 20:56:16 -07001665 const sp<IAfThreadBase> thread = mThread.promote();
Glenn Kasten573d80a2013-08-26 09:36:23 -07001666 if (thread == 0) {
Glenn Kastenfe346c72013-08-30 13:28:22 -07001667 return INVALID_OPERATION;
Glenn Kasten573d80a2013-08-26 09:36:23 -07001668 }
Phil Burk6140c792015-03-19 14:30:21 -07001669
Andy Hung44f27182023-07-06 20:56:16 -07001670 Mutex::Autolock _l(thread->mutex());
1671 auto* const playbackThread = thread->asIAfPlaybackThread().get();
Andy Hung818e7a32016-02-16 18:08:07 -08001672 return playbackThread->getTimestamp_l(timestamp);
Glenn Kasten573d80a2013-08-26 09:36:23 -07001673}
1674
Andy Hung3ff4b552023-06-26 19:20:57 -07001675status_t Track::attachAuxEffect(int EffectId)
Eric Laurent81784c32012-11-19 14:55:58 -08001676{
Andy Hung44f27182023-07-06 20:56:16 -07001677 const sp<IAfThreadBase> thread = mThread.promote();
Eric Laurent6c796322019-04-09 14:13:17 -07001678 if (thread == nullptr) {
1679 return DEAD_OBJECT;
1680 }
Eric Laurent81784c32012-11-19 14:55:58 -08001681
Andy Hung44f27182023-07-06 20:56:16 -07001682 auto dstThread = thread->asIAfPlaybackThread();
Andy Hung3ff4b552023-06-26 19:20:57 -07001683 // srcThread is initialized by call to moveAuxEffectToIo()
Andy Hung44f27182023-07-06 20:56:16 -07001684 sp<IAfPlaybackThread> srcThread;
Andy Hungfa2f4f32023-07-17 12:40:43 -07001685 const auto& af = mClient->afClientCallback();
Eric Laurent6c796322019-04-09 14:13:17 -07001686 status_t status = af->moveAuxEffectToIo(EffectId, dstThread, &srcThread);
Eric Laurent81784c32012-11-19 14:55:58 -08001687
Eric Laurent6c796322019-04-09 14:13:17 -07001688 if (EffectId != 0 && status == NO_ERROR) {
1689 status = dstThread->attachAuxEffect(this, EffectId);
1690 if (status == NO_ERROR) {
1691 AudioSystem::moveEffectsToIo(std::vector<int>(EffectId), dstThread->id());
Eric Laurent81784c32012-11-19 14:55:58 -08001692 }
Eric Laurent6c796322019-04-09 14:13:17 -07001693 }
1694
1695 if (status != NO_ERROR && srcThread != nullptr) {
1696 af->moveAuxEffectToIo(EffectId, srcThread, &dstThread);
Eric Laurent81784c32012-11-19 14:55:58 -08001697 }
1698 return status;
1699}
1700
Andy Hung3ff4b552023-06-26 19:20:57 -07001701void Track::setAuxBuffer(int EffectId, int32_t *buffer)
Eric Laurent81784c32012-11-19 14:55:58 -08001702{
1703 mAuxEffectId = EffectId;
1704 mAuxBuffer = buffer;
1705}
1706
Andy Hung59de4262021-06-14 10:53:54 -07001707// presentationComplete verified by frames, used by Mixed tracks.
Andy Hung3ff4b552023-06-26 19:20:57 -07001708bool Track::presentationComplete(
Andy Hung818e7a32016-02-16 18:08:07 -08001709 int64_t framesWritten, size_t audioHalFrames)
Eric Laurent81784c32012-11-19 14:55:58 -08001710{
Andy Hung818e7a32016-02-16 18:08:07 -08001711 // TODO: improve this based on FrameMap if it exists, to ensure full drain.
1712 // This assists in proper timestamp computation as well as wakelock management.
1713
Eric Laurent81784c32012-11-19 14:55:58 -08001714 // a track is considered presented when the total number of frames written to audio HAL
1715 // corresponds to the number of frames written when presentationComplete() is called for the
1716 // first time (mPresentationCompleteFrames == 0) plus the buffer filling status at that time.
Eric Laurentbfb1b832013-01-07 09:53:42 -08001717 // For an offloaded track the HAL+h/w delay is variable so a HAL drain() is used
1718 // to detect when all frames have been played. In this case framesWritten isn't
1719 // useful because it doesn't always reflect whether there is data in the h/w
1720 // buffers, particularly if a track has been paused and resumed during draining
Andy Hung9d84af52018-09-12 18:03:44 -07001721 ALOGV("%s(%d): presentationComplete() mPresentationCompleteFrames %lld framesWritten %lld",
1722 __func__, mId,
Andy Hung818e7a32016-02-16 18:08:07 -08001723 (long long)mPresentationCompleteFrames, (long long)framesWritten);
Eric Laurent81784c32012-11-19 14:55:58 -08001724 if (mPresentationCompleteFrames == 0) {
1725 mPresentationCompleteFrames = framesWritten + audioHalFrames;
Andy Hung59de4262021-06-14 10:53:54 -07001726 ALOGV("%s(%d): set:"
Andy Hung9d84af52018-09-12 18:03:44 -07001727 " mPresentationCompleteFrames %lld audioHalFrames %zu",
1728 __func__, mId,
Andy Hung818e7a32016-02-16 18:08:07 -08001729 (long long)mPresentationCompleteFrames, audioHalFrames);
Eric Laurent81784c32012-11-19 14:55:58 -08001730 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08001731
Andy Hungc54b1ff2016-02-23 14:07:07 -08001732 bool complete;
Andy Hung59de4262021-06-14 10:53:54 -07001733 if (isFastTrack()) { // does not go through linear map
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001734 complete = framesWritten >= (int64_t) mPresentationCompleteFrames;
Andy Hung59de4262021-06-14 10:53:54 -07001735 ALOGV("%s(%d): %s framesWritten:%lld mPresentationCompleteFrames:%lld",
1736 __func__, mId, (complete ? "complete" : "waiting"),
1737 (long long) framesWritten, (long long) mPresentationCompleteFrames);
Andy Hungc54b1ff2016-02-23 14:07:07 -08001738 } else { // Normal tracks, OutputTracks, and PatchTracks
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001739 complete = framesWritten >= (int64_t) mPresentationCompleteFrames
Andy Hungc54b1ff2016-02-23 14:07:07 -08001740 && mAudioTrackServerProxy->isDrained();
1741 }
1742
1743 if (complete) {
Andy Hung59de4262021-06-14 10:53:54 -07001744 notifyPresentationComplete();
Eric Laurent81784c32012-11-19 14:55:58 -08001745 return true;
1746 }
1747 return false;
1748}
1749
Andy Hung59de4262021-06-14 10:53:54 -07001750// presentationComplete checked by time, used by DirectTracks.
Andy Hung3ff4b552023-06-26 19:20:57 -07001751bool Track::presentationComplete(uint32_t latencyMs)
Andy Hung59de4262021-06-14 10:53:54 -07001752{
1753 // For Offloaded or Direct tracks.
1754
1755 // For a direct track, we incorporated time based testing for presentationComplete.
1756
1757 // For an offloaded track the HAL+h/w delay is variable so a HAL drain() is used
1758 // to detect when all frames have been played. In this case latencyMs isn't
1759 // useful because it doesn't always reflect whether there is data in the h/w
1760 // buffers, particularly if a track has been paused and resumed during draining
1761
1762 constexpr float MIN_SPEED = 0.125f; // min speed scaling allowed for timely response.
1763 if (mPresentationCompleteTimeNs == 0) {
1764 mPresentationCompleteTimeNs = systemTime() + latencyMs * 1e6 / fmax(mSpeed, MIN_SPEED);
1765 ALOGV("%s(%d): set: latencyMs %u mPresentationCompleteTimeNs:%lld",
1766 __func__, mId, latencyMs, (long long) mPresentationCompleteTimeNs);
1767 }
1768
1769 bool complete;
1770 if (isOffloaded()) {
1771 complete = true;
1772 } else { // Direct
1773 complete = systemTime() >= mPresentationCompleteTimeNs;
1774 ALOGV("%s(%d): %s", __func__, mId, (complete ? "complete" : "waiting"));
1775 }
1776 if (complete) {
1777 notifyPresentationComplete();
1778 return true;
1779 }
1780 return false;
1781}
1782
Andy Hung3ff4b552023-06-26 19:20:57 -07001783void Track::notifyPresentationComplete()
Andy Hung59de4262021-06-14 10:53:54 -07001784{
1785 // This only triggers once. TODO: should we enforce this?
1786 triggerEvents(AudioSystem::SYNC_EVENT_PRESENTATION_COMPLETE);
1787 mAudioTrackServerProxy->setStreamEndDone();
1788}
1789
Andy Hung3ff4b552023-06-26 19:20:57 -07001790void Track::triggerEvents(AudioSystem::sync_event_t type)
Eric Laurent81784c32012-11-19 14:55:58 -08001791{
Andy Hung068e08e2023-05-15 19:02:55 -07001792 for (auto it = mSyncEvents.begin(); it != mSyncEvents.end();) {
1793 if ((*it)->type() == type) {
Andy Hung93bb5732023-05-04 21:16:34 -07001794 ALOGV("%s: triggering SyncEvent type %d", __func__, type);
Andy Hung068e08e2023-05-15 19:02:55 -07001795 (*it)->trigger();
1796 it = mSyncEvents.erase(it);
Ivan Lozano5ec161b2017-12-06 10:00:28 -08001797 } else {
Andy Hung068e08e2023-05-15 19:02:55 -07001798 ++it;
Eric Laurent81784c32012-11-19 14:55:58 -08001799 }
1800 }
1801}
1802
1803// implement VolumeBufferProvider interface
1804
Andy Hung3ff4b552023-06-26 19:20:57 -07001805gain_minifloat_packed_t Track::getVolumeLR() const
Eric Laurent81784c32012-11-19 14:55:58 -08001806{
1807 // called by FastMixer, so not allowed to take any locks, block, or do I/O including logs
1808 ALOG_ASSERT(isFastTrack() && (mCblk != NULL));
Glenn Kastenc56f3422014-03-21 17:53:17 -07001809 gain_minifloat_packed_t vlr = mAudioTrackServerProxy->getVolumeLR();
1810 float vl = float_from_gain(gain_minifloat_unpack_left(vlr));
1811 float vr = float_from_gain(gain_minifloat_unpack_right(vlr));
Eric Laurent81784c32012-11-19 14:55:58 -08001812 // track volumes come from shared memory, so can't be trusted and must be clamped
Glenn Kastenc56f3422014-03-21 17:53:17 -07001813 if (vl > GAIN_FLOAT_UNITY) {
1814 vl = GAIN_FLOAT_UNITY;
Eric Laurent81784c32012-11-19 14:55:58 -08001815 }
Glenn Kastenc56f3422014-03-21 17:53:17 -07001816 if (vr > GAIN_FLOAT_UNITY) {
1817 vr = GAIN_FLOAT_UNITY;
Eric Laurent81784c32012-11-19 14:55:58 -08001818 }
1819 // now apply the cached master volume and stream type volume;
1820 // this is trusted but lacks any synchronization or barrier so may be stale
1821 float v = mCachedVolume;
1822 vl *= v;
1823 vr *= v;
Glenn Kastenc56f3422014-03-21 17:53:17 -07001824 // re-combine into packed minifloat
1825 vlr = gain_minifloat_pack(gain_from_float(vl), gain_from_float(vr));
Eric Laurent81784c32012-11-19 14:55:58 -08001826 // FIXME look at mute, pause, and stop flags
1827 return vlr;
1828}
1829
Andy Hung3ff4b552023-06-26 19:20:57 -07001830status_t Track::setSyncEvent(
Andy Hung068e08e2023-05-15 19:02:55 -07001831 const sp<audioflinger::SyncEvent>& event)
Eric Laurent81784c32012-11-19 14:55:58 -08001832{
Eric Laurentbfb1b832013-01-07 09:53:42 -08001833 if (isTerminated() || mState == PAUSED ||
Eric Laurent81784c32012-11-19 14:55:58 -08001834 ((framesReady() == 0) && ((mSharedBuffer != 0) ||
1835 (mState == STOPPED)))) {
Andy Hung9d84af52018-09-12 18:03:44 -07001836 ALOGW("%s(%d): in invalid state %d on session %d %s mode, framesReady %zu",
1837 __func__, mId,
Andy Hung959b5b82021-09-24 10:46:20 -07001838 (int)mState, mSessionId, (mSharedBuffer != 0) ? "static" : "stream", framesReady());
Eric Laurent81784c32012-11-19 14:55:58 -08001839 event->cancel();
1840 return INVALID_OPERATION;
1841 }
1842 (void) TrackBase::setSyncEvent(event);
1843 return NO_ERROR;
1844}
1845
Andy Hung3ff4b552023-06-26 19:20:57 -07001846void Track::invalidate()
Glenn Kasten5736c352012-12-04 12:12:34 -08001847{
Eric Laurent6acd1d42017-01-04 14:23:29 -08001848 TrackBase::invalidate();
Eric Laurent4d231dc2016-03-11 18:38:23 -08001849 signalClientFlag(CBLK_INVALID);
Eric Laurent4d231dc2016-03-11 18:38:23 -08001850}
1851
Andy Hung3ff4b552023-06-26 19:20:57 -07001852void Track::disable()
Eric Laurent4d231dc2016-03-11 18:38:23 -08001853{
Kevin Rocard01c7d9e2019-09-18 11:24:52 +01001854 // TODO(b/142394888): the filling status should also be reset to filling
Eric Laurent4d231dc2016-03-11 18:38:23 -08001855 signalClientFlag(CBLK_DISABLED);
1856}
1857
Andy Hung3ff4b552023-06-26 19:20:57 -07001858void Track::signalClientFlag(int32_t flag)
Eric Laurent4d231dc2016-03-11 18:38:23 -08001859{
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001860 // FIXME should use proxy, and needs work
1861 audio_track_cblk_t* cblk = mCblk;
Eric Laurent4d231dc2016-03-11 18:38:23 -08001862 android_atomic_or(flag, &cblk->mFlags);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001863 android_atomic_release_store(0x40000000, &cblk->mFutex);
1864 // client is not in server, so FUTEX_WAKE is needed instead of FUTEX_WAKE_PRIVATE
Elliott Hughesee499292014-05-21 17:55:51 -07001865 (void) syscall(__NR_futex, &cblk->mFutex, FUTEX_WAKE, INT_MAX);
Glenn Kasten5736c352012-12-04 12:12:34 -08001866}
1867
Andy Hung3ff4b552023-06-26 19:20:57 -07001868void Track::signal()
Eric Laurent59fe0102013-09-27 18:48:26 -07001869{
Andy Hung44f27182023-07-06 20:56:16 -07001870 const sp<IAfThreadBase> thread = mThread.promote();
Eric Laurent59fe0102013-09-27 18:48:26 -07001871 if (thread != 0) {
Andy Hung44f27182023-07-06 20:56:16 -07001872 auto* const t = thread->asIAfPlaybackThread().get();
1873 Mutex::Autolock _l(t->mutex());
Eric Laurent59fe0102013-09-27 18:48:26 -07001874 t->broadcast_l();
1875 }
1876}
1877
Andy Hung3ff4b552023-06-26 19:20:57 -07001878status_t Track::getDualMonoMode(audio_dual_mono_mode_t* mode) const
Kuowei Lid4adbdb2020-08-13 14:44:25 +08001879{
1880 status_t status = INVALID_OPERATION;
1881 if (isOffloadedOrDirect()) {
Andy Hung44f27182023-07-06 20:56:16 -07001882 const sp<IAfThreadBase> thread = mThread.promote();
Kuowei Lid4adbdb2020-08-13 14:44:25 +08001883 if (thread != nullptr) {
Andy Hung44f27182023-07-06 20:56:16 -07001884 auto* const t = thread->asIAfPlaybackThread().get();
1885 Mutex::Autolock _l(t->mutex());
1886 status = t->getOutput_l()->stream->getDualMonoMode(mode);
Kuowei Lid4adbdb2020-08-13 14:44:25 +08001887 ALOGD_IF((status == NO_ERROR) && (mDualMonoMode != *mode),
1888 "%s: mode %d inconsistent", __func__, mDualMonoMode);
1889 }
1890 }
1891 return status;
1892}
1893
Andy Hung3ff4b552023-06-26 19:20:57 -07001894status_t Track::setDualMonoMode(audio_dual_mono_mode_t mode)
Kuowei Lid4adbdb2020-08-13 14:44:25 +08001895{
1896 status_t status = INVALID_OPERATION;
1897 if (isOffloadedOrDirect()) {
Andy Hung44f27182023-07-06 20:56:16 -07001898 const sp<IAfThreadBase> thread = mThread.promote();
Kuowei Lid4adbdb2020-08-13 14:44:25 +08001899 if (thread != nullptr) {
Andy Hung44f27182023-07-06 20:56:16 -07001900 auto* const t = thread->asIAfPlaybackThread().get();
1901 Mutex::Autolock lock(t->mutex());
1902 status = t->getOutput_l()->stream->setDualMonoMode(mode);
Kuowei Lid4adbdb2020-08-13 14:44:25 +08001903 if (status == NO_ERROR) {
1904 mDualMonoMode = mode;
1905 }
1906 }
1907 }
1908 return status;
1909}
1910
Andy Hung3ff4b552023-06-26 19:20:57 -07001911status_t Track::getAudioDescriptionMixLevel(float* leveldB) const
Kuowei Lid4adbdb2020-08-13 14:44:25 +08001912{
1913 status_t status = INVALID_OPERATION;
1914 if (isOffloadedOrDirect()) {
Andy Hung44f27182023-07-06 20:56:16 -07001915 sp<IAfThreadBase> thread = mThread.promote();
Kuowei Lid4adbdb2020-08-13 14:44:25 +08001916 if (thread != nullptr) {
Andy Hung44f27182023-07-06 20:56:16 -07001917 auto* const t = thread->asIAfPlaybackThread().get();
1918 Mutex::Autolock lock(t->mutex());
1919 status = t->getOutput_l()->stream->getAudioDescriptionMixLevel(leveldB);
Kuowei Lid4adbdb2020-08-13 14:44:25 +08001920 ALOGD_IF((status == NO_ERROR) && (mAudioDescriptionMixLevel != *leveldB),
1921 "%s: level %.3f inconsistent", __func__, mAudioDescriptionMixLevel);
1922 }
1923 }
1924 return status;
1925}
1926
Andy Hung3ff4b552023-06-26 19:20:57 -07001927status_t Track::setAudioDescriptionMixLevel(float leveldB)
Kuowei Lid4adbdb2020-08-13 14:44:25 +08001928{
1929 status_t status = INVALID_OPERATION;
1930 if (isOffloadedOrDirect()) {
Andy Hung44f27182023-07-06 20:56:16 -07001931 const sp<IAfThreadBase> thread = mThread.promote();
Kuowei Lid4adbdb2020-08-13 14:44:25 +08001932 if (thread != nullptr) {
Andy Hung44f27182023-07-06 20:56:16 -07001933 auto* const t = thread->asIAfPlaybackThread().get();
1934 Mutex::Autolock lock(t->mutex());
1935 status = t->getOutput_l()->stream->setAudioDescriptionMixLevel(leveldB);
Kuowei Lid4adbdb2020-08-13 14:44:25 +08001936 if (status == NO_ERROR) {
1937 mAudioDescriptionMixLevel = leveldB;
1938 }
1939 }
1940 }
1941 return status;
1942}
1943
Andy Hung3ff4b552023-06-26 19:20:57 -07001944status_t Track::getPlaybackRateParameters(
Andy Hung02a6c4e2023-06-23 19:27:19 -07001945 audio_playback_rate_t* playbackRate) const
Kuowei Lid4adbdb2020-08-13 14:44:25 +08001946{
1947 status_t status = INVALID_OPERATION;
1948 if (isOffloadedOrDirect()) {
Andy Hung44f27182023-07-06 20:56:16 -07001949 const sp<IAfThreadBase> thread = mThread.promote();
Kuowei Lid4adbdb2020-08-13 14:44:25 +08001950 if (thread != nullptr) {
Andy Hung44f27182023-07-06 20:56:16 -07001951 auto* const t = thread->asIAfPlaybackThread().get();
1952 Mutex::Autolock lock(t->mutex());
1953 status = t->getOutput_l()->stream->getPlaybackRateParameters(playbackRate);
Kuowei Lid4adbdb2020-08-13 14:44:25 +08001954 ALOGD_IF((status == NO_ERROR) &&
1955 !isAudioPlaybackRateEqual(mPlaybackRateParameters, *playbackRate),
1956 "%s: playbackRate inconsistent", __func__);
1957 }
1958 }
1959 return status;
1960}
1961
Andy Hung3ff4b552023-06-26 19:20:57 -07001962status_t Track::setPlaybackRateParameters(
Kuowei Lid4adbdb2020-08-13 14:44:25 +08001963 const audio_playback_rate_t& playbackRate)
1964{
1965 status_t status = INVALID_OPERATION;
1966 if (isOffloadedOrDirect()) {
Andy Hung44f27182023-07-06 20:56:16 -07001967 const sp<IAfThreadBase> thread = mThread.promote();
Kuowei Lid4adbdb2020-08-13 14:44:25 +08001968 if (thread != nullptr) {
Andy Hung44f27182023-07-06 20:56:16 -07001969 auto* const t = thread->asIAfPlaybackThread().get();
1970 Mutex::Autolock lock(t->mutex());
1971 status = t->getOutput_l()->stream->setPlaybackRateParameters(playbackRate);
Kuowei Lid4adbdb2020-08-13 14:44:25 +08001972 if (status == NO_ERROR) {
1973 mPlaybackRateParameters = playbackRate;
1974 }
1975 }
1976 }
1977 return status;
1978}
1979
Krishnankutty Kolathappilly8d6c2922014-02-04 16:23:42 -08001980//To be called with thread lock held
Andy Hung3ff4b552023-06-26 19:20:57 -07001981bool Track::isResumePending() const {
Andy Hung71ba4b32022-10-06 12:09:49 -07001982 if (mState == RESUMING) {
Krishnankutty Kolathappilly8d6c2922014-02-04 16:23:42 -08001983 return true;
Andy Hung71ba4b32022-10-06 12:09:49 -07001984 }
Krishnankutty Kolathappilly8d6c2922014-02-04 16:23:42 -08001985 /* Resume is pending if track was stopping before pause was called */
1986 if (mState == STOPPING_1 &&
Andy Hung71ba4b32022-10-06 12:09:49 -07001987 mResumeToStopping) {
Krishnankutty Kolathappilly8d6c2922014-02-04 16:23:42 -08001988 return true;
Andy Hung71ba4b32022-10-06 12:09:49 -07001989 }
Krishnankutty Kolathappilly8d6c2922014-02-04 16:23:42 -08001990
1991 return false;
1992}
1993
1994//To be called with thread lock held
Andy Hung3ff4b552023-06-26 19:20:57 -07001995void Track::resumeAck() {
Andy Hung71ba4b32022-10-06 12:09:49 -07001996 if (mState == RESUMING) {
Krishnankutty Kolathappilly8d6c2922014-02-04 16:23:42 -08001997 mState = ACTIVE;
Andy Hung71ba4b32022-10-06 12:09:49 -07001998 }
Haynes Mathew George2d3ca682014-03-07 13:43:49 -08001999
Krishnankutty Kolathappilly8d6c2922014-02-04 16:23:42 -08002000 // Other possibility of pending resume is stopping_1 state
2001 // Do not update the state from stopping as this prevents
Haynes Mathew George2d3ca682014-03-07 13:43:49 -08002002 // drain being called.
2003 if (mState == STOPPING_1) {
2004 mResumeToStopping = false;
2005 }
Krishnankutty Kolathappilly8d6c2922014-02-04 16:23:42 -08002006}
Andy Hunge10393e2015-06-12 13:59:33 -07002007
2008//To be called with thread lock held
Andy Hung3ff4b552023-06-26 19:20:57 -07002009void Track::updateTrackFrameInfo(
Andy Hung818e7a32016-02-16 18:08:07 -08002010 int64_t trackFramesReleased, int64_t sinkFramesWritten,
Andy Hungcef2daa2018-06-01 15:31:49 -07002011 uint32_t halSampleRate, const ExtendedTimestamp &timeStamp) {
Andy Hung30282562018-08-08 18:27:03 -07002012 // Make the kernel frametime available.
2013 const FrameTime ft{
2014 timeStamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL],
2015 timeStamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL]};
2016 // ALOGD("FrameTime: %lld %lld", (long long)ft.frames, (long long)ft.timeNs);
2017 mKernelFrameTime.store(ft);
2018 if (!audio_is_linear_pcm(mFormat)) {
2019 return;
2020 }
2021
Andy Hung818e7a32016-02-16 18:08:07 -08002022 //update frame map
Andy Hunge10393e2015-06-12 13:59:33 -07002023 mFrameMap.push(trackFramesReleased, sinkFramesWritten);
Andy Hung818e7a32016-02-16 18:08:07 -08002024
2025 // adjust server times and set drained state.
2026 //
2027 // Our timestamps are only updated when the track is on the Thread active list.
2028 // We need to ensure that tracks are not removed before full drain.
2029 ExtendedTimestamp local = timeStamp;
Andy Hungcef2daa2018-06-01 15:31:49 -07002030 bool drained = true; // default assume drained, if no server info found
Andy Hung818e7a32016-02-16 18:08:07 -08002031 bool checked = false;
2032 for (int i = ExtendedTimestamp::LOCATION_MAX - 1;
2033 i >= ExtendedTimestamp::LOCATION_SERVER; --i) {
2034 // Lookup the track frame corresponding to the sink frame position.
2035 if (local.mTimeNs[i] > 0) {
2036 local.mPosition[i] = mFrameMap.findX(local.mPosition[i]);
2037 // check drain state from the latest stage in the pipeline.
Andy Hung6d7b1192016-05-07 22:59:48 -07002038 if (!checked && i <= ExtendedTimestamp::LOCATION_KERNEL) {
Andy Hungcef2daa2018-06-01 15:31:49 -07002039 drained = local.mPosition[i] >= mAudioTrackServerProxy->framesReleased();
Andy Hung818e7a32016-02-16 18:08:07 -08002040 checked = true;
2041 }
2042 }
Andy Hunge10393e2015-06-12 13:59:33 -07002043 }
Andy Hungcef2daa2018-06-01 15:31:49 -07002044
Andy Hung93bb5732023-05-04 21:16:34 -07002045 ALOGV("%s: trackFramesReleased:%lld sinkFramesWritten:%lld setDrained: %d",
2046 __func__, (long long)trackFramesReleased, (long long)sinkFramesWritten, drained);
Andy Hungcef2daa2018-06-01 15:31:49 -07002047 mAudioTrackServerProxy->setDrained(drained);
Andy Hungea2b9c02016-02-12 17:06:53 -08002048 // Set correction for flushed frames that are not accounted for in released.
Andy Hungea2b9c02016-02-12 17:06:53 -08002049 local.mFlushed = mAudioTrackServerProxy->framesFlushed();
Andy Hung818e7a32016-02-16 18:08:07 -08002050 mServerProxy->setTimestamp(local);
Andy Hungcef2daa2018-06-01 15:31:49 -07002051
2052 // Compute latency info.
2053 const bool useTrackTimestamp = !drained;
2054 const double latencyMs = useTrackTimestamp
2055 ? local.getOutputServerLatencyMs(sampleRate())
2056 : timeStamp.getOutputServerLatencyMs(halSampleRate);
2057
2058 mServerLatencyFromTrack.store(useTrackTimestamp);
2059 mServerLatencyMs.store(latencyMs);
Andy Hungb68f5eb2019-12-03 16:49:17 -08002060
Andy Hung62921122020-05-18 10:47:31 -07002061 if (mLogStartCountdown > 0
2062 && local.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] > 0
2063 && local.mPosition[ExtendedTimestamp::LOCATION_KERNEL] > 0)
2064 {
2065 if (mLogStartCountdown > 1) {
2066 --mLogStartCountdown;
2067 } else if (latencyMs < mLogLatencyMs) { // wait for latency to stabilize (dip)
2068 mLogStartCountdown = 0;
Andy Hungb68f5eb2019-12-03 16:49:17 -08002069 // startup is the difference in times for the current timestamp and our start
2070 double startUpMs =
Andy Hung62921122020-05-18 10:47:31 -07002071 (local.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] - mLogStartTimeNs) * 1e-6;
Andy Hungb68f5eb2019-12-03 16:49:17 -08002072 // adjust for frames played.
Andy Hung62921122020-05-18 10:47:31 -07002073 startUpMs -= (local.mPosition[ExtendedTimestamp::LOCATION_KERNEL] - mLogStartFrames)
2074 * 1e3 / mSampleRate;
2075 ALOGV("%s: latencyMs:%lf startUpMs:%lf"
2076 " localTime:%lld startTime:%lld"
2077 " localPosition:%lld startPosition:%lld",
2078 __func__, latencyMs, startUpMs,
2079 (long long)local.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL],
Andy Hungb68f5eb2019-12-03 16:49:17 -08002080 (long long)mLogStartTimeNs,
Andy Hung62921122020-05-18 10:47:31 -07002081 (long long)local.mPosition[ExtendedTimestamp::LOCATION_KERNEL],
Andy Hungb68f5eb2019-12-03 16:49:17 -08002082 (long long)mLogStartFrames);
Andy Hungc2b11cb2020-04-22 09:04:01 -07002083 mTrackMetrics.logLatencyAndStartup(latencyMs, startUpMs);
Andy Hungb68f5eb2019-12-03 16:49:17 -08002084 }
Andy Hung62921122020-05-18 10:47:31 -07002085 mLogLatencyMs = latencyMs;
Andy Hungb68f5eb2019-12-03 16:49:17 -08002086 }
Andy Hunge10393e2015-06-12 13:59:33 -07002087}
2088
Andy Hung3ff4b552023-06-26 19:20:57 -07002089bool Track::AudioVibrationController::setMute(bool muted) {
Andy Hung44f27182023-07-06 20:56:16 -07002090 const sp<IAfThreadBase> thread = mTrack->mThread.promote();
jiabin57303cc2018-12-18 15:45:57 -08002091 if (thread != 0) {
2092 // Lock for updating mHapticPlaybackEnabled.
Andy Hung44f27182023-07-06 20:56:16 -07002093 Mutex::Autolock _l(thread->mutex());
2094 auto* const playbackThread = thread->asIAfPlaybackThread().get();
jiabin57303cc2018-12-18 15:45:57 -08002095 if ((mTrack->channelMask() & AUDIO_CHANNEL_HAPTIC_ALL) != AUDIO_CHANNEL_NONE
Andy Hung44f27182023-07-06 20:56:16 -07002096 && playbackThread->hapticChannelCount() > 0) {
SPeak Shenc19aa9e2022-11-11 00:28:50 +08002097 ALOGD("%s, haptic playback was %s for track %d",
2098 __func__, muted ? "muted" : "unmuted", mTrack->id());
SPeak Shen0db56b32022-11-11 00:28:50 +08002099 mTrack->setHapticPlaybackEnabled(!muted);
2100 return true;
jiabin57303cc2018-12-18 15:45:57 -08002101 }
2102 }
SPeak Shen0db56b32022-11-11 00:28:50 +08002103 return false;
2104}
2105
Andy Hung3ff4b552023-06-26 19:20:57 -07002106binder::Status Track::AudioVibrationController::mute(
SPeak Shen0db56b32022-11-11 00:28:50 +08002107 /*out*/ bool *ret) {
2108 *ret = setMute(true);
jiabin57303cc2018-12-18 15:45:57 -08002109 return binder::Status::ok();
2110}
2111
Andy Hung3ff4b552023-06-26 19:20:57 -07002112binder::Status Track::AudioVibrationController::unmute(
jiabin57303cc2018-12-18 15:45:57 -08002113 /*out*/ bool *ret) {
SPeak Shen0db56b32022-11-11 00:28:50 +08002114 *ret = setMute(false);
jiabin57303cc2018-12-18 15:45:57 -08002115 return binder::Status::ok();
2116}
2117
Eric Laurent81784c32012-11-19 14:55:58 -08002118// ----------------------------------------------------------------------------
Andy Hung9d84af52018-09-12 18:03:44 -07002119#undef LOG_TAG
2120#define LOG_TAG "AF::OutputTrack"
Eric Laurent81784c32012-11-19 14:55:58 -08002121
Andy Hung3ff4b552023-06-26 19:20:57 -07002122/* static */
Andy Hung44f27182023-07-06 20:56:16 -07002123sp<IAfOutputTrack> IAfOutputTrack::create(
2124 IAfPlaybackThread* playbackThread,
2125 IAfDuplicatingThread* sourceThread,
Andy Hung3ff4b552023-06-26 19:20:57 -07002126 uint32_t sampleRate,
2127 audio_format_t format,
2128 audio_channel_mask_t channelMask,
2129 size_t frameCount,
2130 const AttributionSourceState& attributionSource) {
2131 return sp<OutputTrack>::make(
Andy Hung44f27182023-07-06 20:56:16 -07002132 playbackThread,
2133 sourceThread,
Andy Hung3ff4b552023-06-26 19:20:57 -07002134 sampleRate,
2135 format,
2136 channelMask,
2137 frameCount,
2138 attributionSource);
2139}
2140
2141OutputTrack::OutputTrack(
Andy Hung44f27182023-07-06 20:56:16 -07002142 IAfPlaybackThread* playbackThread,
2143 IAfDuplicatingThread* sourceThread,
Eric Laurent81784c32012-11-19 14:55:58 -08002144 uint32_t sampleRate,
2145 audio_format_t format,
2146 audio_channel_mask_t channelMask,
Marco Nelissen462fd2f2013-01-14 14:12:05 -08002147 size_t frameCount,
Svet Ganov33761132021-05-13 22:51:08 +00002148 const AttributionSourceState& attributionSource)
Eric Laurent223fd5c2014-11-11 13:43:36 -08002149 : Track(playbackThread, NULL, AUDIO_STREAM_PATCH,
Kevin Rocard1f564ac2018-03-29 13:53:10 -07002150 audio_attributes_t{} /* currently unused for output track */,
Eric Laurent223fd5c2014-11-11 13:43:36 -08002151 sampleRate, format, channelMask, frameCount,
Andy Hung8fe68032017-06-05 16:17:51 -07002152 nullptr /* buffer */, (size_t)0 /* bufferSize */, nullptr /* sharedBuffer */,
Svet Ganov33761132021-05-13 22:51:08 +00002153 AUDIO_SESSION_NONE, getpid(), attributionSource, AUDIO_OUTPUT_FLAG_NONE,
Glenn Kastend848eb42016-03-08 13:42:11 -08002154 TYPE_OUTPUT),
Eric Laurent5bba2f62016-03-18 11:14:14 -07002155 mActive(false), mSourceThread(sourceThread)
Eric Laurent81784c32012-11-19 14:55:58 -08002156{
2157
2158 if (mCblk != NULL) {
Eric Laurent81784c32012-11-19 14:55:58 -08002159 mOutBuffer.frameCount = 0;
Andy Hung44f27182023-07-06 20:56:16 -07002160 playbackThread->addOutputTrack_l(this);
Andy Hung9d84af52018-09-12 18:03:44 -07002161 ALOGV("%s(): mCblk %p, mBuffer %p, "
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002162 "frameCount %zu, mChannelMask 0x%08x",
Andy Hung9d84af52018-09-12 18:03:44 -07002163 __func__, mCblk, mBuffer,
Glenn Kasten74935e42013-12-19 08:56:45 -08002164 frameCount, mChannelMask);
Glenn Kastene3aa6592012-12-04 12:22:46 -08002165 // since client and server are in the same process,
2166 // the buffer has the same virtual address on both sides
Glenn Kasten529c61b2014-07-18 15:31:02 -07002167 mClientProxy = new AudioTrackClientProxy(mCblk, mBuffer, mFrameCount, mFrameSize,
2168 true /*clientInServer*/);
Glenn Kastenc56f3422014-03-21 17:53:17 -07002169 mClientProxy->setVolumeLR(GAIN_MINIFLOAT_PACKED_UNITY);
Eric Laurent8d2d4932013-04-25 12:56:18 -07002170 mClientProxy->setSendLevel(0.0);
2171 mClientProxy->setSampleRate(sampleRate);
Eric Laurent81784c32012-11-19 14:55:58 -08002172 } else {
Andy Hung9d84af52018-09-12 18:03:44 -07002173 ALOGW("%s(%d): Error creating output track on thread %d",
2174 __func__, mId, (int)mThreadIoHandle);
Eric Laurent81784c32012-11-19 14:55:58 -08002175 }
2176}
2177
Andy Hung3ff4b552023-06-26 19:20:57 -07002178OutputTrack::~OutputTrack()
Eric Laurent81784c32012-11-19 14:55:58 -08002179{
2180 clearBufferQueue();
Glenn Kastene3aa6592012-12-04 12:22:46 -08002181 // superclass destructor will now delete the server proxy and shared memory both refer to
Eric Laurent81784c32012-11-19 14:55:58 -08002182}
2183
Andy Hung3ff4b552023-06-26 19:20:57 -07002184status_t OutputTrack::start(AudioSystem::sync_event_t event,
Glenn Kastend848eb42016-03-08 13:42:11 -08002185 audio_session_t triggerSession)
Eric Laurent81784c32012-11-19 14:55:58 -08002186{
2187 status_t status = Track::start(event, triggerSession);
2188 if (status != NO_ERROR) {
2189 return status;
2190 }
2191
2192 mActive = true;
2193 mRetryCount = 127;
2194 return status;
2195}
2196
Andy Hung3ff4b552023-06-26 19:20:57 -07002197void OutputTrack::stop()
Eric Laurent81784c32012-11-19 14:55:58 -08002198{
2199 Track::stop();
2200 clearBufferQueue();
2201 mOutBuffer.frameCount = 0;
2202 mActive = false;
2203}
2204
Andy Hung3ff4b552023-06-26 19:20:57 -07002205ssize_t OutputTrack::write(void* data, uint32_t frames)
Eric Laurent81784c32012-11-19 14:55:58 -08002206{
Eric Laurent19952e12023-04-20 10:08:29 +02002207 if (!mActive && frames != 0) {
Andy Hung44f27182023-07-06 20:56:16 -07002208 const sp<IAfThreadBase> thread = mThread.promote();
Andy Hung4989d312023-06-29 21:19:25 -07002209 if (thread != nullptr && thread->inStandby()) {
Eric Laurent19952e12023-04-20 10:08:29 +02002210 // preload one silent buffer to trigger mixer on start()
2211 ClientProxy::Buffer buf { .mFrameCount = mClientProxy->getStartThresholdInFrames() };
2212 status_t status = mClientProxy->obtainBuffer(&buf);
2213 if (status != NO_ERROR && status != NOT_ENOUGH_DATA && status != WOULD_BLOCK) {
2214 ALOGE("%s(%d): could not obtain buffer on start", __func__, mId);
2215 return 0;
2216 }
2217 memset(buf.mRaw, 0, buf.mFrameCount * mFrameSize);
2218 mClientProxy->releaseBuffer(&buf);
2219
2220 (void) start();
2221
2222 // wait for HAL stream to start before sending actual audio. Doing this on each
2223 // OutputTrack makes that playback start on all output streams is synchronized.
2224 // If another OutputTrack has already started it can underrun but this is OK
2225 // as only silence has been played so far and the retry count is very high on
2226 // OutputTrack.
Andy Hung44f27182023-07-06 20:56:16 -07002227 auto* const pt = thread->asIAfPlaybackThread().get();
Eric Laurent19952e12023-04-20 10:08:29 +02002228 if (!pt->waitForHalStart()) {
2229 ALOGW("%s(%d): timeout waiting for thread to exit standby", __func__, mId);
2230 stop();
2231 return 0;
2232 }
2233
2234 // enqueue the first buffer and exit so that other OutputTracks will also start before
2235 // write() is called again and this buffer actually consumed.
2236 Buffer firstBuffer;
2237 firstBuffer.frameCount = frames;
2238 firstBuffer.raw = data;
2239 queueBuffer(firstBuffer);
2240 return frames;
2241 } else {
2242 (void) start();
2243 }
2244 }
2245
Eric Laurent81784c32012-11-19 14:55:58 -08002246 Buffer *pInBuffer;
2247 Buffer inBuffer;
Eric Laurent81784c32012-11-19 14:55:58 -08002248 inBuffer.frameCount = frames;
Andy Hungc25b84a2015-01-14 19:04:10 -08002249 inBuffer.raw = data;
Eric Laurent81784c32012-11-19 14:55:58 -08002250 uint32_t waitTimeLeftMs = mSourceThread->waitTimeMs();
Eric Laurent81784c32012-11-19 14:55:58 -08002251 while (waitTimeLeftMs) {
2252 // First write pending buffers, then new data
2253 if (mBufferQueue.size()) {
2254 pInBuffer = mBufferQueue.itemAt(0);
2255 } else {
2256 pInBuffer = &inBuffer;
2257 }
2258
2259 if (pInBuffer->frameCount == 0) {
2260 break;
2261 }
2262
2263 if (mOutBuffer.frameCount == 0) {
2264 mOutBuffer.frameCount = pInBuffer->frameCount;
2265 nsecs_t startTime = systemTime();
Glenn Kasten9f80dd22012-12-18 15:57:32 -08002266 status_t status = obtainBuffer(&mOutBuffer, waitTimeLeftMs);
Eric Laurent4d231dc2016-03-11 18:38:23 -08002267 if (status != NO_ERROR && status != NOT_ENOUGH_DATA) {
Andy Hung9d84af52018-09-12 18:03:44 -07002268 ALOGV("%s(%d): thread %d no more output buffers; status %d",
2269 __func__, mId,
2270 (int)mThreadIoHandle, status);
Eric Laurent81784c32012-11-19 14:55:58 -08002271 break;
2272 }
2273 uint32_t waitTimeMs = (uint32_t)ns2ms(systemTime() - startTime);
2274 if (waitTimeLeftMs >= waitTimeMs) {
2275 waitTimeLeftMs -= waitTimeMs;
2276 } else {
2277 waitTimeLeftMs = 0;
2278 }
Eric Laurent4d231dc2016-03-11 18:38:23 -08002279 if (status == NOT_ENOUGH_DATA) {
2280 restartIfDisabled();
2281 continue;
2282 }
Eric Laurent81784c32012-11-19 14:55:58 -08002283 }
2284
2285 uint32_t outFrames = pInBuffer->frameCount > mOutBuffer.frameCount ? mOutBuffer.frameCount :
2286 pInBuffer->frameCount;
Andy Hungc25b84a2015-01-14 19:04:10 -08002287 memcpy(mOutBuffer.raw, pInBuffer->raw, outFrames * mFrameSize);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08002288 Proxy::Buffer buf;
2289 buf.mFrameCount = outFrames;
2290 buf.mRaw = NULL;
2291 mClientProxy->releaseBuffer(&buf);
Eric Laurent4d231dc2016-03-11 18:38:23 -08002292 restartIfDisabled();
Eric Laurent81784c32012-11-19 14:55:58 -08002293 pInBuffer->frameCount -= outFrames;
Andy Hungc25b84a2015-01-14 19:04:10 -08002294 pInBuffer->raw = (int8_t *)pInBuffer->raw + outFrames * mFrameSize;
Eric Laurent81784c32012-11-19 14:55:58 -08002295 mOutBuffer.frameCount -= outFrames;
Andy Hungc25b84a2015-01-14 19:04:10 -08002296 mOutBuffer.raw = (int8_t *)mOutBuffer.raw + outFrames * mFrameSize;
Eric Laurent81784c32012-11-19 14:55:58 -08002297
2298 if (pInBuffer->frameCount == 0) {
2299 if (mBufferQueue.size()) {
2300 mBufferQueue.removeAt(0);
Andy Hungc25b84a2015-01-14 19:04:10 -08002301 free(pInBuffer->mBuffer);
Yunlian Jiang8adc8082017-06-06 15:59:44 -07002302 if (pInBuffer != &inBuffer) {
2303 delete pInBuffer;
2304 }
Andy Hung9d84af52018-09-12 18:03:44 -07002305 ALOGV("%s(%d): thread %d released overflow buffer %zu",
2306 __func__, mId,
2307 (int)mThreadIoHandle, mBufferQueue.size());
Eric Laurent81784c32012-11-19 14:55:58 -08002308 } else {
2309 break;
2310 }
2311 }
2312 }
2313
2314 // If we could not write all frames, allocate a buffer and queue it for next time.
2315 if (inBuffer.frameCount) {
Andy Hung44f27182023-07-06 20:56:16 -07002316 const sp<IAfThreadBase> thread = mThread.promote();
Andy Hung4989d312023-06-29 21:19:25 -07002317 if (thread != nullptr && !thread->inStandby()) {
Eric Laurent19952e12023-04-20 10:08:29 +02002318 queueBuffer(inBuffer);
Eric Laurent81784c32012-11-19 14:55:58 -08002319 }
2320 }
2321
Andy Hungc25b84a2015-01-14 19:04:10 -08002322 // Calling write() with a 0 length buffer means that no more data will be written:
2323 // We rely on stop() to set the appropriate flags to allow the remaining frames to play out.
2324 if (frames == 0 && mBufferQueue.size() == 0 && mActive) {
2325 stop();
Eric Laurent81784c32012-11-19 14:55:58 -08002326 }
2327
Andy Hung1c86ebe2018-05-29 20:29:08 -07002328 return frames - inBuffer.frameCount; // number of frames consumed.
Eric Laurent81784c32012-11-19 14:55:58 -08002329}
2330
Andy Hung3ff4b552023-06-26 19:20:57 -07002331void OutputTrack::queueBuffer(Buffer& inBuffer) {
Eric Laurent19952e12023-04-20 10:08:29 +02002332
2333 if (mBufferQueue.size() < kMaxOverFlowBuffers) {
2334 Buffer *pInBuffer = new Buffer;
2335 const size_t bufferSize = inBuffer.frameCount * mFrameSize;
2336 pInBuffer->mBuffer = malloc(bufferSize);
2337 LOG_ALWAYS_FATAL_IF(pInBuffer->mBuffer == nullptr,
2338 "%s: Unable to malloc size %zu", __func__, bufferSize);
2339 pInBuffer->frameCount = inBuffer.frameCount;
2340 pInBuffer->raw = pInBuffer->mBuffer;
2341 memcpy(pInBuffer->raw, inBuffer.raw, inBuffer.frameCount * mFrameSize);
2342 mBufferQueue.add(pInBuffer);
2343 ALOGV("%s(%d): thread %d adding overflow buffer %zu", __func__, mId,
2344 (int)mThreadIoHandle, mBufferQueue.size());
2345 // audio data is consumed (stored locally); set frameCount to 0.
2346 inBuffer.frameCount = 0;
2347 } else {
2348 ALOGW("%s(%d): thread %d no more overflow buffers",
2349 __func__, mId, (int)mThreadIoHandle);
2350 // TODO: return error for this.
2351 }
2352}
2353
Andy Hung3ff4b552023-06-26 19:20:57 -07002354void OutputTrack::copyMetadataTo(MetadataInserter& backInserter) const
Kevin Rocard12381092018-04-11 09:19:59 -07002355{
2356 std::lock_guard<std::mutex> lock(mTrackMetadatasMutex);
2357 backInserter = std::copy(mTrackMetadatas.begin(), mTrackMetadatas.end(), backInserter);
2358}
2359
Andy Hung3ff4b552023-06-26 19:20:57 -07002360void OutputTrack::setMetadatas(const SourceMetadatas& metadatas) {
Kevin Rocard12381092018-04-11 09:19:59 -07002361 {
2362 std::lock_guard<std::mutex> lock(mTrackMetadatasMutex);
2363 mTrackMetadatas = metadatas;
2364 }
2365 // No need to adjust metadata track volumes as OutputTrack volumes are always 0dBFS.
2366 setMetadataHasChanged();
2367}
2368
Andy Hung3ff4b552023-06-26 19:20:57 -07002369status_t OutputTrack::obtainBuffer(
Eric Laurent81784c32012-11-19 14:55:58 -08002370 AudioBufferProvider::Buffer* buffer, uint32_t waitTimeMs)
2371{
Glenn Kasten9f80dd22012-12-18 15:57:32 -08002372 ClientProxy::Buffer buf;
2373 buf.mFrameCount = buffer->frameCount;
2374 struct timespec timeout;
2375 timeout.tv_sec = waitTimeMs / 1000;
2376 timeout.tv_nsec = (int) (waitTimeMs % 1000) * 1000000;
2377 status_t status = mClientProxy->obtainBuffer(&buf, &timeout);
2378 buffer->frameCount = buf.mFrameCount;
2379 buffer->raw = buf.mRaw;
2380 return status;
Eric Laurent81784c32012-11-19 14:55:58 -08002381}
2382
Andy Hung3ff4b552023-06-26 19:20:57 -07002383void OutputTrack::clearBufferQueue()
Eric Laurent81784c32012-11-19 14:55:58 -08002384{
2385 size_t size = mBufferQueue.size();
2386
2387 for (size_t i = 0; i < size; i++) {
2388 Buffer *pBuffer = mBufferQueue.itemAt(i);
Andy Hungc25b84a2015-01-14 19:04:10 -08002389 free(pBuffer->mBuffer);
Eric Laurent81784c32012-11-19 14:55:58 -08002390 delete pBuffer;
2391 }
2392 mBufferQueue.clear();
2393}
2394
Andy Hung3ff4b552023-06-26 19:20:57 -07002395void OutputTrack::restartIfDisabled()
Eric Laurent4d231dc2016-03-11 18:38:23 -08002396{
2397 int32_t flags = android_atomic_and(~CBLK_DISABLED, &mCblk->mFlags);
2398 if (mActive && (flags & CBLK_DISABLED)) {
2399 start();
2400 }
2401}
Eric Laurent81784c32012-11-19 14:55:58 -08002402
Andy Hung9d84af52018-09-12 18:03:44 -07002403// ----------------------------------------------------------------------------
2404#undef LOG_TAG
2405#define LOG_TAG "AF::PatchTrack"
2406
Andy Hung3ff4b552023-06-26 19:20:57 -07002407/* static */
2408sp<IAfPatchTrack> IAfPatchTrack::create(
Andy Hung44f27182023-07-06 20:56:16 -07002409 IAfPlaybackThread* playbackThread,
Andy Hung3ff4b552023-06-26 19:20:57 -07002410 audio_stream_type_t streamType,
2411 uint32_t sampleRate,
2412 audio_channel_mask_t channelMask,
2413 audio_format_t format,
2414 size_t frameCount,
2415 void* buffer,
2416 size_t bufferSize,
2417 audio_output_flags_t flags,
2418 const Timeout& timeout,
2419 size_t frameCountToBeReady /** Default behaviour is to start
2420 * as soon as possible to have
2421 * the lowest possible latency
2422 * even if it might glitch. */)
2423{
2424 return sp<PatchTrack>::make(
Andy Hung44f27182023-07-06 20:56:16 -07002425 playbackThread,
Andy Hung3ff4b552023-06-26 19:20:57 -07002426 streamType,
2427 sampleRate,
2428 channelMask,
2429 format,
2430 frameCount,
2431 buffer,
2432 bufferSize,
2433 flags,
2434 timeout,
2435 frameCountToBeReady);
2436}
2437
Andy Hung44f27182023-07-06 20:56:16 -07002438PatchTrack::PatchTrack(IAfPlaybackThread* playbackThread,
Eric Laurent3bcf8592015-04-03 12:13:24 -07002439 audio_stream_type_t streamType,
Eric Laurent83b88082014-06-20 18:31:16 -07002440 uint32_t sampleRate,
2441 audio_channel_mask_t channelMask,
2442 audio_format_t format,
2443 size_t frameCount,
2444 void *buffer,
Andy Hung8fe68032017-06-05 16:17:51 -07002445 size_t bufferSize,
Kevin Rocard45986c72018-12-18 18:22:59 -08002446 audio_output_flags_t flags,
Kevin Rocard01c7d9e2019-09-18 11:24:52 +01002447 const Timeout& timeout,
2448 size_t frameCountToBeReady)
Eric Laurent3bcf8592015-04-03 12:13:24 -07002449 : Track(playbackThread, NULL, streamType,
Kevin Rocard1f564ac2018-03-29 13:53:10 -07002450 audio_attributes_t{} /* currently unused for patch track */,
Eric Laurent223fd5c2014-11-11 13:43:36 -08002451 sampleRate, format, channelMask, frameCount,
Andy Hung8fe68032017-06-05 16:17:51 -07002452 buffer, bufferSize, nullptr /* sharedBuffer */,
Svet Ganov33761132021-05-13 22:51:08 +00002453 AUDIO_SESSION_NONE, getpid(), audioServerAttributionSource(getpid()), flags,
Philip P. Moltmannbda45752020-07-17 16:41:18 -07002454 TYPE_PATCH, AUDIO_PORT_HANDLE_NONE, frameCountToBeReady),
gaoxiupei8e3a5682023-07-07 20:30:23 +08002455 PatchTrackBase(mCblk ? new ClientProxy(mCblk, mBuffer, frameCount, mFrameSize, true, true)
2456 : nullptr,
Andy Hung837229a2023-07-14 16:57:01 -07002457 playbackThread, timeout)
Eric Laurent83b88082014-06-20 18:31:16 -07002458{
Andy Hung9d84af52018-09-12 18:03:44 -07002459 ALOGV("%s(%d): sampleRate %d mPeerTimeout %d.%03d sec",
2460 __func__, mId, sampleRate,
Eric Laurent83b88082014-06-20 18:31:16 -07002461 (int)mPeerTimeout.tv_sec,
2462 (int)(mPeerTimeout.tv_nsec / 1000000));
2463}
2464
Andy Hung3ff4b552023-06-26 19:20:57 -07002465PatchTrack::~PatchTrack()
Eric Laurent83b88082014-06-20 18:31:16 -07002466{
Andy Hungabfab202019-03-07 19:45:54 -08002467 ALOGV("%s(%d)", __func__, mId);
Eric Laurent83b88082014-06-20 18:31:16 -07002468}
2469
Andy Hung3ff4b552023-06-26 19:20:57 -07002470size_t PatchTrack::framesReady() const
Mikhail Naganovcaf59942019-09-25 14:05:29 -07002471{
2472 if (mPeerProxy && mPeerProxy->producesBufferOnDemand()) {
2473 return std::numeric_limits<size_t>::max();
2474 } else {
2475 return Track::framesReady();
2476 }
2477}
2478
Andy Hung3ff4b552023-06-26 19:20:57 -07002479status_t PatchTrack::start(AudioSystem::sync_event_t event,
Kevin Rocard5f2136e2018-05-11 22:03:00 -07002480 audio_session_t triggerSession)
Eric Laurent4d231dc2016-03-11 18:38:23 -08002481{
2482 status_t status = Track::start(event, triggerSession);
2483 if (status != NO_ERROR) {
2484 return status;
2485 }
2486 android_atomic_and(~CBLK_DISABLED, &mCblk->mFlags);
2487 return status;
2488}
2489
Eric Laurent83b88082014-06-20 18:31:16 -07002490// AudioBufferProvider interface
Andy Hung3ff4b552023-06-26 19:20:57 -07002491status_t PatchTrack::getNextBuffer(
Glenn Kastend79072e2016-01-06 08:41:20 -08002492 AudioBufferProvider::Buffer* buffer)
Eric Laurent83b88082014-06-20 18:31:16 -07002493{
Andy Hung9d84af52018-09-12 18:03:44 -07002494 ALOG_ASSERT(mPeerProxy != 0, "%s(%d): called without peer proxy", __func__, mId);
Eric Laurent83b88082014-06-20 18:31:16 -07002495 Proxy::Buffer buf;
2496 buf.mFrameCount = buffer->frameCount;
Mikhail Naganov938be412019-09-04 11:38:47 -07002497 if (ATRACE_ENABLED()) {
2498 std::string traceName("PTnReq");
2499 traceName += std::to_string(id());
2500 ATRACE_INT(traceName.c_str(), buf.mFrameCount);
2501 }
Eric Laurent83b88082014-06-20 18:31:16 -07002502 status_t status = mPeerProxy->obtainBuffer(&buf, &mPeerTimeout);
Andy Hung9d84af52018-09-12 18:03:44 -07002503 ALOGV_IF(status != NO_ERROR, "%s(%d): getNextBuffer status %d", __func__, mId, status);
Eric Laurentc2730ba2014-07-20 15:47:07 -07002504 buffer->frameCount = buf.mFrameCount;
Mikhail Naganov938be412019-09-04 11:38:47 -07002505 if (ATRACE_ENABLED()) {
2506 std::string traceName("PTnObt");
2507 traceName += std::to_string(id());
2508 ATRACE_INT(traceName.c_str(), buf.mFrameCount);
2509 }
Eric Laurent83b88082014-06-20 18:31:16 -07002510 if (buf.mFrameCount == 0) {
2511 return WOULD_BLOCK;
2512 }
Glenn Kastend79072e2016-01-06 08:41:20 -08002513 status = Track::getNextBuffer(buffer);
Eric Laurent83b88082014-06-20 18:31:16 -07002514 return status;
2515}
2516
Andy Hung3ff4b552023-06-26 19:20:57 -07002517void PatchTrack::releaseBuffer(AudioBufferProvider::Buffer* buffer)
Eric Laurent83b88082014-06-20 18:31:16 -07002518{
Andy Hung9d84af52018-09-12 18:03:44 -07002519 ALOG_ASSERT(mPeerProxy != 0, "%s(%d): called without peer proxy", __func__, mId);
Eric Laurent83b88082014-06-20 18:31:16 -07002520 Proxy::Buffer buf;
2521 buf.mFrameCount = buffer->frameCount;
2522 buf.mRaw = buffer->raw;
2523 mPeerProxy->releaseBuffer(&buf);
Andy Hung71ba4b32022-10-06 12:09:49 -07002524 TrackBase::releaseBuffer(buffer); // Note: this is the base class.
Eric Laurent83b88082014-06-20 18:31:16 -07002525}
2526
Andy Hung3ff4b552023-06-26 19:20:57 -07002527status_t PatchTrack::obtainBuffer(Proxy::Buffer* buffer,
Eric Laurent83b88082014-06-20 18:31:16 -07002528 const struct timespec *timeOut)
2529{
Eric Laurent4d231dc2016-03-11 18:38:23 -08002530 status_t status = NO_ERROR;
2531 static const int32_t kMaxTries = 5;
2532 int32_t tryCounter = kMaxTries;
Andy Hungf62e1a22018-05-08 18:32:11 -07002533 const size_t originalFrameCount = buffer->mFrameCount;
Eric Laurent4d231dc2016-03-11 18:38:23 -08002534 do {
2535 if (status == NOT_ENOUGH_DATA) {
2536 restartIfDisabled();
Andy Hungf62e1a22018-05-08 18:32:11 -07002537 buffer->mFrameCount = originalFrameCount; // cleared on error, must be restored.
Eric Laurent4d231dc2016-03-11 18:38:23 -08002538 }
2539 status = mProxy->obtainBuffer(buffer, timeOut);
2540 } while ((status == NOT_ENOUGH_DATA) && (tryCounter-- > 0));
2541 return status;
Eric Laurent83b88082014-06-20 18:31:16 -07002542}
2543
Andy Hung3ff4b552023-06-26 19:20:57 -07002544void PatchTrack::releaseBuffer(Proxy::Buffer* buffer)
Eric Laurent83b88082014-06-20 18:31:16 -07002545{
2546 mProxy->releaseBuffer(buffer);
Eric Laurent4d231dc2016-03-11 18:38:23 -08002547 restartIfDisabled();
naoki miyazuf37f9982019-11-28 11:18:18 +09002548
2549 // Check if the PatchTrack has enough data to write once in releaseBuffer().
2550 // If not, prevent an underrun from occurring by moving the track into FS_FILLING;
2551 // this logic avoids glitches when suspending A2DP with AudioPlaybackCapture.
2552 // TODO: perhaps underrun avoidance could be a track property checked in isReady() instead.
Andy Hung3ff4b552023-06-26 19:20:57 -07002553 if (mFillingStatus == FS_ACTIVE
naoki miyazuf37f9982019-11-28 11:18:18 +09002554 && audio_is_linear_pcm(mFormat)
2555 && !isOffloadedOrDirect()) {
Andy Hung44f27182023-07-06 20:56:16 -07002556 if (const sp<IAfThreadBase> thread = mThread.promote();
naoki miyazuf37f9982019-11-28 11:18:18 +09002557 thread != 0) {
Andy Hung44f27182023-07-06 20:56:16 -07002558 auto* const playbackThread = thread->asIAfPlaybackThread().get();
naoki miyazuf37f9982019-11-28 11:18:18 +09002559 const size_t frameCount = playbackThread->frameCount() * sampleRate()
2560 / playbackThread->sampleRate();
2561 if (framesReady() < frameCount) {
2562 ALOGD("%s(%d) Not enough data, wait for buffer to fill", __func__, mId);
Andy Hung3ff4b552023-06-26 19:20:57 -07002563 mFillingStatus = FS_FILLING;
naoki miyazuf37f9982019-11-28 11:18:18 +09002564 }
2565 }
2566 }
Eric Laurent4d231dc2016-03-11 18:38:23 -08002567}
2568
Andy Hung3ff4b552023-06-26 19:20:57 -07002569void PatchTrack::restartIfDisabled()
Eric Laurent4d231dc2016-03-11 18:38:23 -08002570{
Eric Laurent83b88082014-06-20 18:31:16 -07002571 if (android_atomic_and(~CBLK_DISABLED, &mCblk->mFlags) & CBLK_DISABLED) {
Andy Hung9d84af52018-09-12 18:03:44 -07002572 ALOGW("%s(%d): disabled due to previous underrun, restarting", __func__, mId);
Eric Laurent83b88082014-06-20 18:31:16 -07002573 start();
2574 }
Eric Laurent83b88082014-06-20 18:31:16 -07002575}
2576
Eric Laurent81784c32012-11-19 14:55:58 -08002577// ----------------------------------------------------------------------------
2578// Record
2579// ----------------------------------------------------------------------------
Jean-Michel Trividdf87ef2019-08-20 15:42:04 -07002580
2581
Andy Hung9d84af52018-09-12 18:03:44 -07002582#undef LOG_TAG
2583#define LOG_TAG "AF::RecordHandle"
Eric Laurent81784c32012-11-19 14:55:58 -08002584
Andy Hungaaa18282023-06-23 19:27:19 -07002585class RecordHandle : public android::media::BnAudioRecord {
2586public:
Andy Hung02a6c4e2023-06-23 19:27:19 -07002587 explicit RecordHandle(const sp<IAfRecordTrack>& recordTrack);
Andy Hungaaa18282023-06-23 19:27:19 -07002588 ~RecordHandle() override;
2589 binder::Status start(int /*AudioSystem::sync_event_t*/ event,
2590 int /*audio_session_t*/ triggerSession) final;
2591 binder::Status stop() final;
2592 binder::Status getActiveMicrophones(
2593 std::vector<media::MicrophoneInfoFw>* activeMicrophones) final;
2594 binder::Status setPreferredMicrophoneDirection(
2595 int /*audio_microphone_direction_t*/ direction) final;
2596 binder::Status setPreferredMicrophoneFieldDimension(float zoom) final;
2597 binder::Status shareAudioHistory(
2598 const std::string& sharedAudioPackageName, int64_t sharedAudioStartMs) final;
2599
2600private:
Andy Hung02a6c4e2023-06-23 19:27:19 -07002601 const sp<IAfRecordTrack> mRecordTrack;
Andy Hungaaa18282023-06-23 19:27:19 -07002602
2603 // for use from destructor
2604 void stop_nonvirtual();
2605};
2606
2607/* static */
Andy Hung02a6c4e2023-06-23 19:27:19 -07002608sp<media::IAudioRecord> IAfRecordTrack::createIAudioRecordAdapter(
2609 const sp<IAfRecordTrack>& recordTrack) {
Andy Hungaaa18282023-06-23 19:27:19 -07002610 return sp<RecordHandle>::make(recordTrack);
2611}
2612
2613RecordHandle::RecordHandle(
Andy Hung02a6c4e2023-06-23 19:27:19 -07002614 const sp<IAfRecordTrack>& recordTrack)
Eric Laurent81784c32012-11-19 14:55:58 -08002615 : BnAudioRecord(),
2616 mRecordTrack(recordTrack)
2617{
Andy Hung225aef62022-12-06 16:33:20 -08002618 setMinSchedulerPolicy(SCHED_NORMAL, ANDROID_PRIORITY_AUDIO);
Eric Laurent81784c32012-11-19 14:55:58 -08002619}
2620
Andy Hungaaa18282023-06-23 19:27:19 -07002621RecordHandle::~RecordHandle() {
Eric Laurent81784c32012-11-19 14:55:58 -08002622 stop_nonvirtual();
2623 mRecordTrack->destroy();
2624}
2625
Andy Hungaaa18282023-06-23 19:27:19 -07002626binder::Status RecordHandle::start(int /*AudioSystem::sync_event_t*/ event,
Ivan Lozanoff6900d2017-08-01 15:47:38 -07002627 int /*audio_session_t*/ triggerSession) {
Andy Hung9d84af52018-09-12 18:03:44 -07002628 ALOGV("%s()", __func__);
Andy Hung1131b6e2020-12-08 20:47:45 -08002629 return binderStatusFromStatusT(
Ivan Lozanoff6900d2017-08-01 15:47:38 -07002630 mRecordTrack->start((AudioSystem::sync_event_t)event, (audio_session_t) triggerSession));
Eric Laurent81784c32012-11-19 14:55:58 -08002631}
2632
Andy Hungaaa18282023-06-23 19:27:19 -07002633binder::Status RecordHandle::stop() {
Eric Laurent81784c32012-11-19 14:55:58 -08002634 stop_nonvirtual();
Ivan Lozanoff6900d2017-08-01 15:47:38 -07002635 return binder::Status::ok();
Eric Laurent81784c32012-11-19 14:55:58 -08002636}
2637
Andy Hungaaa18282023-06-23 19:27:19 -07002638void RecordHandle::stop_nonvirtual() {
Andy Hung9d84af52018-09-12 18:03:44 -07002639 ALOGV("%s()", __func__);
Eric Laurent81784c32012-11-19 14:55:58 -08002640 mRecordTrack->stop();
2641}
2642
Andy Hungaaa18282023-06-23 19:27:19 -07002643binder::Status RecordHandle::getActiveMicrophones(
Mikhail Naganov2a6a3012023-02-13 11:45:03 -08002644 std::vector<media::MicrophoneInfoFw>* activeMicrophones) {
Andy Hung9d84af52018-09-12 18:03:44 -07002645 ALOGV("%s()", __func__);
Mikhail Naganov2a6a3012023-02-13 11:45:03 -08002646 return binderStatusFromStatusT(mRecordTrack->getActiveMicrophones(activeMicrophones));
jiabin653cc0a2018-01-17 17:54:10 -08002647}
2648
Andy Hungaaa18282023-06-23 19:27:19 -07002649binder::Status RecordHandle::setPreferredMicrophoneDirection(
Paul McLean03a6e6a2018-12-04 10:54:13 -07002650 int /*audio_microphone_direction_t*/ direction) {
2651 ALOGV("%s()", __func__);
Andy Hung1131b6e2020-12-08 20:47:45 -08002652 return binderStatusFromStatusT(mRecordTrack->setPreferredMicrophoneDirection(
Paul McLean03a6e6a2018-12-04 10:54:13 -07002653 static_cast<audio_microphone_direction_t>(direction)));
2654}
2655
Andy Hungaaa18282023-06-23 19:27:19 -07002656binder::Status RecordHandle::setPreferredMicrophoneFieldDimension(float zoom) {
Paul McLean03a6e6a2018-12-04 10:54:13 -07002657 ALOGV("%s()", __func__);
Andy Hung1131b6e2020-12-08 20:47:45 -08002658 return binderStatusFromStatusT(mRecordTrack->setPreferredMicrophoneFieldDimension(zoom));
Paul McLean03a6e6a2018-12-04 10:54:13 -07002659}
2660
Andy Hungaaa18282023-06-23 19:27:19 -07002661binder::Status RecordHandle::shareAudioHistory(
Eric Laurentec376dc2021-04-08 20:41:22 +02002662 const std::string& sharedAudioPackageName, int64_t sharedAudioStartMs) {
2663 return binderStatusFromStatusT(
2664 mRecordTrack->shareAudioHistory(sharedAudioPackageName, sharedAudioStartMs));
2665}
2666
Eric Laurent81784c32012-11-19 14:55:58 -08002667// ----------------------------------------------------------------------------
Andy Hung9d84af52018-09-12 18:03:44 -07002668#undef LOG_TAG
2669#define LOG_TAG "AF::RecordTrack"
Eric Laurent81784c32012-11-19 14:55:58 -08002670
Andy Hung3ff4b552023-06-26 19:20:57 -07002671
Andy Hung56126702023-07-14 11:00:08 -07002672/* static */
Andy Hung44f27182023-07-06 20:56:16 -07002673sp<IAfRecordTrack> IAfRecordTrack::create(IAfRecordThread* thread,
Andy Hung3ff4b552023-06-26 19:20:57 -07002674 const sp<Client>& client,
2675 const audio_attributes_t& attr,
2676 uint32_t sampleRate,
2677 audio_format_t format,
2678 audio_channel_mask_t channelMask,
2679 size_t frameCount,
2680 void* buffer,
2681 size_t bufferSize,
2682 audio_session_t sessionId,
2683 pid_t creatorPid,
2684 const AttributionSourceState& attributionSource,
2685 audio_input_flags_t flags,
2686 track_type type,
2687 audio_port_handle_t portId,
2688 int32_t startFrames)
2689{
2690 return sp<RecordTrack>::make(
Andy Hung44f27182023-07-06 20:56:16 -07002691 thread,
Andy Hung3ff4b552023-06-26 19:20:57 -07002692 client,
2693 attr,
2694 sampleRate,
2695 format,
2696 channelMask,
2697 frameCount,
2698 buffer,
2699 bufferSize,
2700 sessionId,
2701 creatorPid,
2702 attributionSource,
2703 flags,
2704 type,
2705 portId,
2706 startFrames);
2707}
2708
Glenn Kasten05997e22014-03-13 15:08:33 -07002709// RecordTrack constructor must be called with AudioFlinger::mLock and ThreadBase::mLock held
Andy Hung3ff4b552023-06-26 19:20:57 -07002710RecordTrack::RecordTrack(
Andy Hung44f27182023-07-06 20:56:16 -07002711 IAfRecordThread* thread,
Eric Laurent81784c32012-11-19 14:55:58 -08002712 const sp<Client>& client,
Kevin Rocard1f564ac2018-03-29 13:53:10 -07002713 const audio_attributes_t& attr,
Eric Laurent81784c32012-11-19 14:55:58 -08002714 uint32_t sampleRate,
2715 audio_format_t format,
2716 audio_channel_mask_t channelMask,
2717 size_t frameCount,
Eric Laurent83b88082014-06-20 18:31:16 -07002718 void *buffer,
Andy Hung8fe68032017-06-05 16:17:51 -07002719 size_t bufferSize,
Glenn Kastend848eb42016-03-08 13:42:11 -08002720 audio_session_t sessionId,
Eric Laurent09f1ed22019-04-24 17:45:17 -07002721 pid_t creatorPid,
Svet Ganov33761132021-05-13 22:51:08 +00002722 const AttributionSourceState& attributionSource,
Eric Laurent05067782016-06-01 18:27:28 -07002723 audio_input_flags_t flags,
Eric Laurent20b9ef02016-12-05 11:03:16 -08002724 track_type type,
Eric Laurentec376dc2021-04-08 20:41:22 +02002725 audio_port_handle_t portId,
Eric Laurent2407ce32021-04-26 14:56:03 +02002726 int32_t startFrames)
Kevin Rocard1f564ac2018-03-29 13:53:10 -07002727 : TrackBase(thread, client, attr, sampleRate, format,
Eric Laurent09f1ed22019-04-24 17:45:17 -07002728 channelMask, frameCount, buffer, bufferSize, sessionId,
Philip P. Moltmannbda45752020-07-17 16:41:18 -07002729 creatorPid,
Svet Ganov33761132021-05-13 22:51:08 +00002730 VALUE_OR_FATAL(aidl2legacy_int32_t_uid_t(attributionSource.uid)),
Philip P. Moltmannbda45752020-07-17 16:41:18 -07002731 false /*isOut*/,
Eric Laurent83b88082014-06-20 18:31:16 -07002732 (type == TYPE_DEFAULT) ?
Eric Laurent05067782016-06-01 18:27:28 -07002733 ((flags & AUDIO_INPUT_FLAG_FAST) ? ALLOC_PIPE : ALLOC_CBLK) :
Eric Laurent83b88082014-06-20 18:31:16 -07002734 ((buffer == NULL) ? ALLOC_LOCAL : ALLOC_NONE),
Andy Hungb68f5eb2019-12-03 16:49:17 -08002735 type, portId,
2736 std::string(AMEDIAMETRICS_KEY_PREFIX_AUDIO_RECORD) + std::to_string(portId)),
Andy Hung97a893e2015-03-29 01:03:07 -07002737 mOverflow(false),
Andy Hung4c6afaf2015-06-12 18:23:35 -07002738 mResamplerBufferProvider(NULL), // initialize in case of early constructor exit
Eric Laurent05067782016-06-01 18:27:28 -07002739 mRecordBufferConverter(NULL),
jiabin9378eb92018-05-02 15:26:35 -07002740 mFlags(flags),
Jean-Michel Trividdf87ef2019-08-20 15:42:04 -07002741 mSilenced(false),
Eric Laurent2407ce32021-04-26 14:56:03 +02002742 mStartFrames(startFrames)
Eric Laurent81784c32012-11-19 14:55:58 -08002743{
Glenn Kasten3ef14ef2014-03-13 15:08:51 -07002744 if (mCblk == NULL) {
2745 return;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08002746 }
Glenn Kasten6dd62fb2013-12-05 16:35:58 -08002747
Mikhail Naganov7c6ae982018-06-14 12:33:38 -07002748 if (!isDirect()) {
2749 mRecordBufferConverter = new RecordBufferConverter(
Andy Hung44f27182023-07-06 20:56:16 -07002750 thread->channelMask(), thread->format(), thread->sampleRate(),
Mikhail Naganov7c6ae982018-06-14 12:33:38 -07002751 channelMask, format, sampleRate);
2752 // Check if the RecordBufferConverter construction was successful.
2753 // If not, don't continue with construction.
2754 //
2755 // NOTE: It would be extremely rare that the record track cannot be created
2756 // for the current device, but a pending or future device change would make
2757 // the record track configuration valid.
2758 if (mRecordBufferConverter->initCheck() != NO_ERROR) {
Andy Hung9d84af52018-09-12 18:03:44 -07002759 ALOGE("%s(%d): RecordTrack unable to create record buffer converter", __func__, mId);
Mikhail Naganov7c6ae982018-06-14 12:33:38 -07002760 return;
2761 }
Andy Hung97a893e2015-03-29 01:03:07 -07002762 }
2763
Andy Hung6ae58432016-02-16 18:32:24 -08002764 mServerProxy = new AudioRecordServerProxy(mCblk, mBuffer, frameCount,
Andy Hung3f0c9022016-01-15 17:49:46 -08002765 mFrameSize, !isExternalTrack());
Andy Hung3f0c9022016-01-15 17:49:46 -08002766
Andy Hung97a893e2015-03-29 01:03:07 -07002767 mResamplerBufferProvider = new ResamplerBufferProvider(this);
Glenn Kastenc263ca02014-06-04 20:31:46 -07002768
Eric Laurent05067782016-06-01 18:27:28 -07002769 if (flags & AUDIO_INPUT_FLAG_FAST) {
Andy Hung44f27182023-07-06 20:56:16 -07002770 ALOG_ASSERT(thread->fastTrackAvailable());
2771 thread->setFastTrackAvailable(false);
Andy Hung000adb52018-06-01 15:43:26 -07002772 } else {
2773 // TODO: only Normal Record has timestamps (Fast Record does not).
Andy Hung5d3d9562018-10-04 19:27:26 -07002774 mServerLatencySupported = checkServerLatencySupported(mFormat, flags);
Glenn Kastenc263ca02014-06-04 20:31:46 -07002775 }
Andy Hung8946a282018-04-19 20:04:56 -07002776#ifdef TEE_SINK
2777 mTee.setId(std::string("_") + std::to_string(mThreadIoHandle)
2778 + "_" + std::to_string(mId)
2779 + "_R");
2780#endif
Andy Hungb68f5eb2019-12-03 16:49:17 -08002781
2782 // Once this item is logged by the server, the client can add properties.
Andy Hung94235282021-03-24 15:50:14 -07002783 mTrackMetrics.logConstructor(creatorPid, uid(), id());
Eric Laurent81784c32012-11-19 14:55:58 -08002784}
2785
Andy Hung3ff4b552023-06-26 19:20:57 -07002786RecordTrack::~RecordTrack()
Eric Laurent81784c32012-11-19 14:55:58 -08002787{
Andy Hung9d84af52018-09-12 18:03:44 -07002788 ALOGV("%s()", __func__);
Andy Hung97a893e2015-03-29 01:03:07 -07002789 delete mRecordBufferConverter;
Glenn Kasten6dd62fb2013-12-05 16:35:58 -08002790 delete mResamplerBufferProvider;
Eric Laurent81784c32012-11-19 14:55:58 -08002791}
2792
Andy Hung3ff4b552023-06-26 19:20:57 -07002793status_t RecordTrack::initCheck() const
Andy Hung97a893e2015-03-29 01:03:07 -07002794{
2795 status_t status = TrackBase::initCheck();
2796 if (status == NO_ERROR && mServerProxy == 0) {
2797 status = BAD_VALUE;
2798 }
2799 return status;
2800}
2801
Eric Laurent81784c32012-11-19 14:55:58 -08002802// AudioBufferProvider interface
Andy Hung3ff4b552023-06-26 19:20:57 -07002803status_t RecordTrack::getNextBuffer(AudioBufferProvider::Buffer* buffer)
Eric Laurent81784c32012-11-19 14:55:58 -08002804{
Glenn Kasten9f80dd22012-12-18 15:57:32 -08002805 ServerProxy::Buffer buf;
2806 buf.mFrameCount = buffer->frameCount;
2807 status_t status = mServerProxy->obtainBuffer(&buf);
2808 buffer->frameCount = buf.mFrameCount;
2809 buffer->raw = buf.mRaw;
2810 if (buf.mFrameCount == 0) {
2811 // FIXME also wake futex so that overrun is noticed more quickly
Glenn Kasten96f60d82013-07-12 10:21:18 -07002812 (void) android_atomic_or(CBLK_OVERRUN, &mCblk->mFlags);
Eric Laurent81784c32012-11-19 14:55:58 -08002813 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08002814 return status;
Eric Laurent81784c32012-11-19 14:55:58 -08002815}
2816
Andy Hung3ff4b552023-06-26 19:20:57 -07002817status_t RecordTrack::start(AudioSystem::sync_event_t event,
Glenn Kastend848eb42016-03-08 13:42:11 -08002818 audio_session_t triggerSession)
Eric Laurent81784c32012-11-19 14:55:58 -08002819{
Andy Hung44f27182023-07-06 20:56:16 -07002820 const sp<IAfThreadBase> thread = mThread.promote();
Eric Laurent81784c32012-11-19 14:55:58 -08002821 if (thread != 0) {
Andy Hung44f27182023-07-06 20:56:16 -07002822 auto* const recordThread = thread->asIAfRecordThread().get();
Eric Laurent81784c32012-11-19 14:55:58 -08002823 return recordThread->start(this, event, triggerSession);
2824 } else {
Eric Laurentd52a28c2020-08-21 17:10:39 -07002825 ALOGW("%s track %d: thread was destroyed", __func__, portId());
2826 return DEAD_OBJECT;
Eric Laurent81784c32012-11-19 14:55:58 -08002827 }
2828}
2829
Andy Hung3ff4b552023-06-26 19:20:57 -07002830void RecordTrack::stop()
Eric Laurent81784c32012-11-19 14:55:58 -08002831{
Andy Hung44f27182023-07-06 20:56:16 -07002832 const sp<IAfThreadBase> thread = mThread.promote();
Eric Laurent81784c32012-11-19 14:55:58 -08002833 if (thread != 0) {
Andy Hung44f27182023-07-06 20:56:16 -07002834 auto* const recordThread = thread->asIAfRecordThread().get();
Eric Laurent83b88082014-06-20 18:31:16 -07002835 if (recordThread->stop(this) && isExternalTrack()) {
Eric Laurentfee19762018-01-29 18:44:13 -08002836 AudioSystem::stopInput(mPortId);
Eric Laurent81784c32012-11-19 14:55:58 -08002837 }
2838 }
2839}
2840
Andy Hung3ff4b552023-06-26 19:20:57 -07002841void RecordTrack::destroy()
Eric Laurent81784c32012-11-19 14:55:58 -08002842{
Andy Hung3ff4b552023-06-26 19:20:57 -07002843 // see comments at Track::destroy()
Eric Laurent81784c32012-11-19 14:55:58 -08002844 sp<RecordTrack> keep(this);
2845 {
Andy Hungce685402018-10-05 17:23:27 -07002846 track_state priorState = mState;
Andy Hung44f27182023-07-06 20:56:16 -07002847 const sp<IAfThreadBase> thread = mThread.promote();
Eric Laurent81784c32012-11-19 14:55:58 -08002848 if (thread != 0) {
Andy Hung44f27182023-07-06 20:56:16 -07002849 Mutex::Autolock _l(thread->mutex());
2850 auto* const recordThread = thread->asIAfRecordThread().get();
Andy Hungce685402018-10-05 17:23:27 -07002851 priorState = mState;
Eric Laurentec376dc2021-04-08 20:41:22 +02002852 if (!mSharedAudioPackageName.empty()) {
Eric Laurent92d0a322021-07-16 15:32:33 +02002853 recordThread->resetAudioHistory_l();
Eric Laurentec376dc2021-04-08 20:41:22 +02002854 }
Andy Hungce685402018-10-05 17:23:27 -07002855 recordThread->destroyTrack_l(this); // move mState to STOPPED, terminate
2856 }
2857 // APM portid/client management done outside of lock.
2858 // NOTE: if thread doesn't exist, the input descriptor probably doesn't either.
2859 if (isExternalTrack()) {
2860 switch (priorState) {
2861 case ACTIVE: // invalidated while still active
2862 case STARTING_2: // invalidated/start-aborted after startInput successfully called
2863 case PAUSING: // invalidated while in the middle of stop() pausing (still active)
2864 AudioSystem::stopInput(mPortId);
2865 break;
2866
2867 case STARTING_1: // invalidated/start-aborted and startInput not successful
2868 case PAUSED: // OK, not active
2869 case IDLE: // OK, not active
2870 break;
2871
2872 case STOPPED: // unexpected (destroyed)
2873 default:
2874 LOG_ALWAYS_FATAL("%s(%d): invalid prior state: %d", __func__, mId, priorState);
2875 }
2876 AudioSystem::releaseInput(mPortId);
Eric Laurent81784c32012-11-19 14:55:58 -08002877 }
2878 }
2879}
2880
Andy Hung3ff4b552023-06-26 19:20:57 -07002881void RecordTrack::invalidate()
Eric Laurent9a54bc22013-09-09 09:08:44 -07002882{
Eric Laurent6acd1d42017-01-04 14:23:29 -08002883 TrackBase::invalidate();
Eric Laurent9a54bc22013-09-09 09:08:44 -07002884 // FIXME should use proxy, and needs work
2885 audio_track_cblk_t* cblk = mCblk;
2886 android_atomic_or(CBLK_INVALID, &cblk->mFlags);
2887 android_atomic_release_store(0x40000000, &cblk->mFutex);
2888 // client is not in server, so FUTEX_WAKE is needed instead of FUTEX_WAKE_PRIVATE
Elliott Hughesee499292014-05-21 17:55:51 -07002889 (void) syscall(__NR_futex, &cblk->mFutex, FUTEX_WAKE, INT_MAX);
Eric Laurent9a54bc22013-09-09 09:08:44 -07002890}
2891
Eric Laurent81784c32012-11-19 14:55:58 -08002892
Andy Hung3ff4b552023-06-26 19:20:57 -07002893void RecordTrack::appendDumpHeader(String8& result) const
Eric Laurent81784c32012-11-19 14:55:58 -08002894{
Eric Laurent973db022018-11-20 14:54:31 -08002895 result.appendFormat("Active Id Client Session Port Id S Flags "
Andy Hung9d84af52018-09-12 18:03:44 -07002896 " Format Chn mask SRate Source "
Kevin Rocard5f2136e2018-05-11 22:03:00 -07002897 " Server FrmCnt FrmRdy Sil%s\n",
2898 isServerLatencySupported() ? " Latency" : "");
Eric Laurent81784c32012-11-19 14:55:58 -08002899}
2900
Andy Hung3ff4b552023-06-26 19:20:57 -07002901void RecordTrack::appendDump(String8& result, bool active) const
Eric Laurent81784c32012-11-19 14:55:58 -08002902{
Eric Laurent973db022018-11-20 14:54:31 -08002903 result.appendFormat("%c%5s %6d %6u %7u %7u %2s 0x%03X "
Kevin Rocard5f2136e2018-05-11 22:03:00 -07002904 "%08X %08X %6u %6X "
Andy Hung000adb52018-06-01 15:43:26 -07002905 "%08X %6zu %6zu %3c",
Andy Hung2c6c3bb2017-06-16 14:01:45 -07002906 isFastTrack() ? 'F' : ' ',
Marco Nelissenb2208842014-02-07 14:00:50 -08002907 active ? "yes" : "no",
Andy Hung9d84af52018-09-12 18:03:44 -07002908 mId,
Andy Hung4ef19fa2018-05-15 19:35:29 -07002909 (mClient == 0) ? getpid() : mClient->pid(),
Andy Hung2c6c3bb2017-06-16 14:01:45 -07002910 mSessionId,
Eric Laurent973db022018-11-20 14:54:31 -08002911 mPortId,
Andy Hunge2e830f2019-12-03 12:54:46 -08002912 getTrackStateAsCodedString(),
Andy Hung2c6c3bb2017-06-16 14:01:45 -07002913 mCblk->mFlags,
2914
Eric Laurent81784c32012-11-19 14:55:58 -08002915 mFormat,
2916 mChannelMask,
Andy Hung2c6c3bb2017-06-16 14:01:45 -07002917 mSampleRate,
Kevin Rocard5f2136e2018-05-11 22:03:00 -07002918 mAttr.source,
Glenn Kasten6dd62fb2013-12-05 16:35:58 -08002919
Andy Hung2c6c3bb2017-06-16 14:01:45 -07002920 mCblk->mServer,
Jean-Michel Trivi7d665ab2018-04-11 17:26:51 -07002921 mFrameCount,
Andy Hung000adb52018-06-01 15:43:26 -07002922 mServerProxy->framesReadySafe(),
Jean-Michel Trivi7d665ab2018-04-11 17:26:51 -07002923 isSilenced() ? 's' : 'n'
Andy Hung2c6c3bb2017-06-16 14:01:45 -07002924 );
Andy Hung000adb52018-06-01 15:43:26 -07002925 if (isServerLatencySupported()) {
2926 double latencyMs;
2927 bool fromTrack;
2928 if (getTrackLatencyMs(&latencyMs, &fromTrack) == OK) {
2929 // Show latency in msec, followed by 't' if from track timestamp (the most accurate)
2930 // or 'k' if estimated from kernel (usually for debugging).
2931 result.appendFormat(" %7.2lf %c", latencyMs, fromTrack ? 't' : 'k');
2932 } else {
2933 result.appendFormat("%10s", mCblk->mServer != 0 ? "unavail" : "new");
2934 }
2935 }
2936 result.append("\n");
Eric Laurent81784c32012-11-19 14:55:58 -08002937}
2938
Andy Hung93bb5732023-05-04 21:16:34 -07002939// This is invoked by SyncEvent callback.
Andy Hung3ff4b552023-06-26 19:20:57 -07002940void RecordTrack::handleSyncStartEvent(
Andy Hung068e08e2023-05-15 19:02:55 -07002941 const sp<audioflinger::SyncEvent>& event)
Glenn Kasten25f4aa82014-02-07 10:50:43 -08002942{
Andy Hung93bb5732023-05-04 21:16:34 -07002943 size_t framesToDrop = 0;
Andy Hung44f27182023-07-06 20:56:16 -07002944 const sp<IAfThreadBase> threadBase = mThread.promote();
Andy Hung93bb5732023-05-04 21:16:34 -07002945 if (threadBase != 0) {
2946 // TODO: use actual buffer filling status instead of 2 buffers when info is available
2947 // from audio HAL
Andy Hung44f27182023-07-06 20:56:16 -07002948 framesToDrop = threadBase->frameCount() * 2;
Glenn Kasten25f4aa82014-02-07 10:50:43 -08002949 }
Andy Hung93bb5732023-05-04 21:16:34 -07002950
2951 mSynchronizedRecordState.onPlaybackFinished(event, framesToDrop);
Glenn Kasten25f4aa82014-02-07 10:50:43 -08002952}
2953
Andy Hung3ff4b552023-06-26 19:20:57 -07002954void RecordTrack::clearSyncStartEvent()
Glenn Kasten25f4aa82014-02-07 10:50:43 -08002955{
Andy Hung93bb5732023-05-04 21:16:34 -07002956 mSynchronizedRecordState.clear();
Glenn Kasten25f4aa82014-02-07 10:50:43 -08002957}
2958
Andy Hung3ff4b552023-06-26 19:20:57 -07002959void RecordTrack::updateTrackFrameInfo(
Andy Hung3f0c9022016-01-15 17:49:46 -08002960 int64_t trackFramesReleased, int64_t sourceFramesRead,
2961 uint32_t halSampleRate, const ExtendedTimestamp &timestamp)
2962{
Andy Hung30282562018-08-08 18:27:03 -07002963 // Make the kernel frametime available.
2964 const FrameTime ft{
2965 timestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL],
2966 timestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL]};
2967 // ALOGD("FrameTime: %lld %lld", (long long)ft.frames, (long long)ft.timeNs);
2968 mKernelFrameTime.store(ft);
2969 if (!audio_is_linear_pcm(mFormat)) {
Atneya Nair497fff12022-01-18 16:23:04 -05002970 // Stream is direct, return provided timestamp with no conversion
2971 mServerProxy->setTimestamp(timestamp);
Andy Hung30282562018-08-08 18:27:03 -07002972 return;
2973 }
2974
Andy Hung3f0c9022016-01-15 17:49:46 -08002975 ExtendedTimestamp local = timestamp;
2976
2977 // Convert HAL frames to server-side track frames at track sample rate.
2978 // We use trackFramesReleased and sourceFramesRead as an anchor point.
2979 for (int i = ExtendedTimestamp::LOCATION_SERVER; i < ExtendedTimestamp::LOCATION_MAX; ++i) {
2980 if (local.mTimeNs[i] != 0) {
2981 const int64_t relativeServerFrames = local.mPosition[i] - sourceFramesRead;
2982 const int64_t relativeTrackFrames = relativeServerFrames
2983 * mSampleRate / halSampleRate; // TODO: potential computation overflow
2984 local.mPosition[i] = relativeTrackFrames + trackFramesReleased;
2985 }
2986 }
Andy Hung6ae58432016-02-16 18:32:24 -08002987 mServerProxy->setTimestamp(local);
Andy Hung000adb52018-06-01 15:43:26 -07002988
2989 // Compute latency info.
2990 const bool useTrackTimestamp = true; // use track unless debugging.
2991 const double latencyMs = - (useTrackTimestamp
2992 ? local.getOutputServerLatencyMs(sampleRate())
2993 : timestamp.getOutputServerLatencyMs(halSampleRate));
2994
2995 mServerLatencyFromTrack.store(useTrackTimestamp);
2996 mServerLatencyMs.store(latencyMs);
Andy Hung3f0c9022016-01-15 17:49:46 -08002997}
Eric Laurent83b88082014-06-20 18:31:16 -07002998
Andy Hung3ff4b552023-06-26 19:20:57 -07002999status_t RecordTrack::getActiveMicrophones(
Andy Hung02a6c4e2023-06-23 19:27:19 -07003000 std::vector<media::MicrophoneInfoFw>* activeMicrophones) const
jiabin653cc0a2018-01-17 17:54:10 -08003001{
Andy Hung44f27182023-07-06 20:56:16 -07003002 const sp<IAfThreadBase> thread = mThread.promote();
jiabin653cc0a2018-01-17 17:54:10 -08003003 if (thread != 0) {
Andy Hung44f27182023-07-06 20:56:16 -07003004 auto* const recordThread = thread->asIAfRecordThread().get();
jiabin653cc0a2018-01-17 17:54:10 -08003005 return recordThread->getActiveMicrophones(activeMicrophones);
3006 } else {
3007 return BAD_VALUE;
3008 }
3009}
3010
Andy Hung3ff4b552023-06-26 19:20:57 -07003011status_t RecordTrack::setPreferredMicrophoneDirection(
Paul McLean03a6e6a2018-12-04 10:54:13 -07003012 audio_microphone_direction_t direction) {
Andy Hung44f27182023-07-06 20:56:16 -07003013 const sp<IAfThreadBase> thread = mThread.promote();
Paul McLean03a6e6a2018-12-04 10:54:13 -07003014 if (thread != 0) {
Andy Hung44f27182023-07-06 20:56:16 -07003015 auto* const recordThread = thread->asIAfRecordThread().get();
Paul McLean12340082019-03-19 09:35:05 -06003016 return recordThread->setPreferredMicrophoneDirection(direction);
Paul McLean03a6e6a2018-12-04 10:54:13 -07003017 } else {
3018 return BAD_VALUE;
3019 }
3020}
3021
Andy Hung3ff4b552023-06-26 19:20:57 -07003022status_t RecordTrack::setPreferredMicrophoneFieldDimension(float zoom) {
Andy Hung44f27182023-07-06 20:56:16 -07003023 const sp<IAfThreadBase> thread = mThread.promote();
Paul McLean03a6e6a2018-12-04 10:54:13 -07003024 if (thread != 0) {
Andy Hung44f27182023-07-06 20:56:16 -07003025 auto* const recordThread = thread->asIAfRecordThread().get();
Paul McLean12340082019-03-19 09:35:05 -06003026 return recordThread->setPreferredMicrophoneFieldDimension(zoom);
Paul McLean03a6e6a2018-12-04 10:54:13 -07003027 } else {
3028 return BAD_VALUE;
3029 }
3030}
3031
Andy Hung3ff4b552023-06-26 19:20:57 -07003032status_t RecordTrack::shareAudioHistory(
Eric Laurentec376dc2021-04-08 20:41:22 +02003033 const std::string& sharedAudioPackageName, int64_t sharedAudioStartMs) {
3034
3035 const uid_t callingUid = IPCThreadState::self()->getCallingUid();
3036 const pid_t callingPid = IPCThreadState::self()->getCallingPid();
3037 if (callingUid != mUid || callingPid != mCreatorPid) {
3038 return PERMISSION_DENIED;
3039 }
3040
Svet Ganov33761132021-05-13 22:51:08 +00003041 AttributionSourceState attributionSource{};
3042 attributionSource.uid = VALUE_OR_RETURN_STATUS(legacy2aidl_uid_t_int32_t(callingUid));
3043 attributionSource.pid = VALUE_OR_RETURN_STATUS(legacy2aidl_uid_t_int32_t(callingPid));
3044 attributionSource.token = sp<BBinder>::make();
3045 if (!captureHotwordAllowed(attributionSource)) {
Eric Laurentec376dc2021-04-08 20:41:22 +02003046 return PERMISSION_DENIED;
3047 }
3048
Andy Hung44f27182023-07-06 20:56:16 -07003049 const sp<IAfThreadBase> thread = mThread.promote();
Eric Laurentec376dc2021-04-08 20:41:22 +02003050 if (thread != 0) {
Andy Hung44f27182023-07-06 20:56:16 -07003051 auto* const recordThread = thread->asIAfRecordThread().get();
Eric Laurentec376dc2021-04-08 20:41:22 +02003052 status_t status = recordThread->shareAudioHistory(
3053 sharedAudioPackageName, mSessionId, sharedAudioStartMs);
3054 if (status == NO_ERROR) {
3055 mSharedAudioPackageName = sharedAudioPackageName;
3056 }
3057 return status;
3058 } else {
3059 return BAD_VALUE;
3060 }
3061}
3062
Andy Hung3ff4b552023-06-26 19:20:57 -07003063void RecordTrack::copyMetadataTo(MetadataInserter& backInserter) const
Eric Laurent78b07302022-10-07 16:20:34 +02003064{
3065
3066 // Do not forward PatchRecord metadata with unspecified audio source
3067 if (mAttr.source == AUDIO_SOURCE_DEFAULT) {
3068 return;
3069 }
3070
3071 // No track is invalid as this is called after prepareTrack_l in the same critical section
3072 record_track_metadata_v7_t metadata;
3073 metadata.base = {
3074 .source = mAttr.source,
3075 .gain = 1, // capture tracks do not have volumes
3076 };
3077 metadata.channel_mask = mChannelMask;
3078 strncpy(metadata.tags, mAttr.tags, AUDIO_ATTRIBUTES_TAGS_MAX_SIZE);
3079
3080 *backInserter++ = metadata;
3081}
Eric Laurentec376dc2021-04-08 20:41:22 +02003082
Andy Hung9d84af52018-09-12 18:03:44 -07003083// ----------------------------------------------------------------------------
3084#undef LOG_TAG
3085#define LOG_TAG "AF::PatchRecord"
3086
Andy Hung3ff4b552023-06-26 19:20:57 -07003087/* static */
3088sp<IAfPatchRecord> IAfPatchRecord::create(
Andy Hung44f27182023-07-06 20:56:16 -07003089 IAfRecordThread* recordThread,
Andy Hung3ff4b552023-06-26 19:20:57 -07003090 uint32_t sampleRate,
3091 audio_channel_mask_t channelMask,
3092 audio_format_t format,
3093 size_t frameCount,
3094 void *buffer,
3095 size_t bufferSize,
3096 audio_input_flags_t flags,
3097 const Timeout& timeout,
3098 audio_source_t source)
3099{
3100 return sp<PatchRecord>::make(
Andy Hung44f27182023-07-06 20:56:16 -07003101 recordThread,
Andy Hung3ff4b552023-06-26 19:20:57 -07003102 sampleRate,
3103 channelMask,
3104 format,
3105 frameCount,
3106 buffer,
3107 bufferSize,
3108 flags,
3109 timeout,
3110 source);
3111}
3112
Andy Hung44f27182023-07-06 20:56:16 -07003113PatchRecord::PatchRecord(IAfRecordThread* recordThread,
Eric Laurent83b88082014-06-20 18:31:16 -07003114 uint32_t sampleRate,
3115 audio_channel_mask_t channelMask,
3116 audio_format_t format,
3117 size_t frameCount,
3118 void *buffer,
Andy Hung8fe68032017-06-05 16:17:51 -07003119 size_t bufferSize,
Kevin Rocard45986c72018-12-18 18:22:59 -08003120 audio_input_flags_t flags,
Eric Laurent78b07302022-10-07 16:20:34 +02003121 const Timeout& timeout,
3122 audio_source_t source)
Kevin Rocard1f564ac2018-03-29 13:53:10 -07003123 : RecordTrack(recordThread, NULL,
Eric Laurent78b07302022-10-07 16:20:34 +02003124 audio_attributes_t{ .source = source } ,
Kevin Rocard1f564ac2018-03-29 13:53:10 -07003125 sampleRate, format, channelMask, frameCount,
Philip P. Moltmannbda45752020-07-17 16:41:18 -07003126 buffer, bufferSize, AUDIO_SESSION_NONE, getpid(),
Svet Ganov33761132021-05-13 22:51:08 +00003127 audioServerAttributionSource(getpid()), flags, TYPE_PATCH),
gaoxiupei8e3a5682023-07-07 20:30:23 +08003128 PatchTrackBase(mCblk ? new ClientProxy(mCblk, mBuffer, frameCount, mFrameSize, false, true)
3129 : nullptr,
Andy Hung837229a2023-07-14 16:57:01 -07003130 recordThread, timeout)
Eric Laurent83b88082014-06-20 18:31:16 -07003131{
Andy Hung9d84af52018-09-12 18:03:44 -07003132 ALOGV("%s(%d): sampleRate %d mPeerTimeout %d.%03d sec",
3133 __func__, mId, sampleRate,
Eric Laurent83b88082014-06-20 18:31:16 -07003134 (int)mPeerTimeout.tv_sec,
3135 (int)(mPeerTimeout.tv_nsec / 1000000));
3136}
3137
Andy Hung3ff4b552023-06-26 19:20:57 -07003138PatchRecord::~PatchRecord()
Eric Laurent83b88082014-06-20 18:31:16 -07003139{
Andy Hungabfab202019-03-07 19:45:54 -08003140 ALOGV("%s(%d)", __func__, mId);
Eric Laurent83b88082014-06-20 18:31:16 -07003141}
3142
Mikhail Naganov8296c252019-09-25 14:59:54 -07003143static size_t writeFramesHelper(
3144 AudioBufferProvider* dest, const void* src, size_t frameCount, size_t frameSize)
3145{
3146 AudioBufferProvider::Buffer patchBuffer;
3147 patchBuffer.frameCount = frameCount;
3148 auto status = dest->getNextBuffer(&patchBuffer);
3149 if (status != NO_ERROR) {
3150 ALOGW("%s PathRecord getNextBuffer failed with error %d: %s",
3151 __func__, status, strerror(-status));
3152 return 0;
3153 }
3154 ALOG_ASSERT(patchBuffer.frameCount <= frameCount);
3155 memcpy(patchBuffer.raw, src, patchBuffer.frameCount * frameSize);
3156 size_t framesWritten = patchBuffer.frameCount;
3157 dest->releaseBuffer(&patchBuffer);
3158 return framesWritten;
3159}
3160
3161// static
Andy Hung3ff4b552023-06-26 19:20:57 -07003162size_t PatchRecord::writeFrames(
Mikhail Naganov8296c252019-09-25 14:59:54 -07003163 AudioBufferProvider* dest, const void* src, size_t frameCount, size_t frameSize)
3164{
3165 size_t framesWritten = writeFramesHelper(dest, src, frameCount, frameSize);
3166 // On buffer wrap, the buffer frame count will be less than requested,
3167 // when this happens a second buffer needs to be used to write the leftover audio
3168 const size_t framesLeft = frameCount - framesWritten;
3169 if (framesWritten != 0 && framesLeft != 0) {
3170 framesWritten += writeFramesHelper(dest, (const char*)src + framesWritten * frameSize,
3171 framesLeft, frameSize);
3172 }
3173 return framesWritten;
3174}
3175
Eric Laurent83b88082014-06-20 18:31:16 -07003176// AudioBufferProvider interface
Andy Hung3ff4b552023-06-26 19:20:57 -07003177status_t PatchRecord::getNextBuffer(
Glenn Kastend79072e2016-01-06 08:41:20 -08003178 AudioBufferProvider::Buffer* buffer)
Eric Laurent83b88082014-06-20 18:31:16 -07003179{
Andy Hung9d84af52018-09-12 18:03:44 -07003180 ALOG_ASSERT(mPeerProxy != 0, "%s(%d): called without peer proxy", __func__, mId);
Eric Laurent83b88082014-06-20 18:31:16 -07003181 Proxy::Buffer buf;
3182 buf.mFrameCount = buffer->frameCount;
3183 status_t status = mPeerProxy->obtainBuffer(&buf, &mPeerTimeout);
3184 ALOGV_IF(status != NO_ERROR,
Andy Hung9d84af52018-09-12 18:03:44 -07003185 "%s(%d): mPeerProxy->obtainBuffer status %d", __func__, mId, status);
Eric Laurentc2730ba2014-07-20 15:47:07 -07003186 buffer->frameCount = buf.mFrameCount;
Mikhail Naganov938be412019-09-04 11:38:47 -07003187 if (ATRACE_ENABLED()) {
3188 std::string traceName("PRnObt");
3189 traceName += std::to_string(id());
3190 ATRACE_INT(traceName.c_str(), buf.mFrameCount);
3191 }
Eric Laurent83b88082014-06-20 18:31:16 -07003192 if (buf.mFrameCount == 0) {
3193 return WOULD_BLOCK;
3194 }
Glenn Kastend79072e2016-01-06 08:41:20 -08003195 status = RecordTrack::getNextBuffer(buffer);
Eric Laurent83b88082014-06-20 18:31:16 -07003196 return status;
3197}
3198
Andy Hung3ff4b552023-06-26 19:20:57 -07003199void PatchRecord::releaseBuffer(AudioBufferProvider::Buffer* buffer)
Eric Laurent83b88082014-06-20 18:31:16 -07003200{
Andy Hung9d84af52018-09-12 18:03:44 -07003201 ALOG_ASSERT(mPeerProxy != 0, "%s(%d): called without peer proxy", __func__, mId);
Eric Laurent83b88082014-06-20 18:31:16 -07003202 Proxy::Buffer buf;
3203 buf.mFrameCount = buffer->frameCount;
3204 buf.mRaw = buffer->raw;
3205 mPeerProxy->releaseBuffer(&buf);
3206 TrackBase::releaseBuffer(buffer);
3207}
3208
Andy Hung3ff4b552023-06-26 19:20:57 -07003209status_t PatchRecord::obtainBuffer(Proxy::Buffer* buffer,
Eric Laurent83b88082014-06-20 18:31:16 -07003210 const struct timespec *timeOut)
3211{
3212 return mProxy->obtainBuffer(buffer, timeOut);
3213}
3214
Andy Hung3ff4b552023-06-26 19:20:57 -07003215void PatchRecord::releaseBuffer(Proxy::Buffer* buffer)
Eric Laurent83b88082014-06-20 18:31:16 -07003216{
3217 mProxy->releaseBuffer(buffer);
3218}
3219
Mikhail Naganovcaf59942019-09-25 14:05:29 -07003220#undef LOG_TAG
3221#define LOG_TAG "AF::PthrPatchRecord"
3222
3223static std::unique_ptr<void, decltype(free)*> allocAligned(size_t alignment, size_t size)
3224{
3225 void *ptr = nullptr;
3226 (void)posix_memalign(&ptr, alignment, size);
Andy Hung71ba4b32022-10-06 12:09:49 -07003227 return {ptr, free};
Mikhail Naganovcaf59942019-09-25 14:05:29 -07003228}
3229
Andy Hung3ff4b552023-06-26 19:20:57 -07003230/* static */
3231sp<IAfPatchRecord> IAfPatchRecord::createPassThru(
Andy Hung44f27182023-07-06 20:56:16 -07003232 IAfRecordThread* recordThread,
Andy Hung3ff4b552023-06-26 19:20:57 -07003233 uint32_t sampleRate,
3234 audio_channel_mask_t channelMask,
3235 audio_format_t format,
3236 size_t frameCount,
3237 audio_input_flags_t flags,
3238 audio_source_t source)
3239{
3240 return sp<PassthruPatchRecord>::make(
Andy Hung44f27182023-07-06 20:56:16 -07003241 recordThread,
Andy Hung3ff4b552023-06-26 19:20:57 -07003242 sampleRate,
3243 channelMask,
3244 format,
3245 frameCount,
3246 flags,
3247 source);
3248}
3249
3250PassthruPatchRecord::PassthruPatchRecord(
Andy Hung44f27182023-07-06 20:56:16 -07003251 IAfRecordThread* recordThread,
Mikhail Naganovcaf59942019-09-25 14:05:29 -07003252 uint32_t sampleRate,
3253 audio_channel_mask_t channelMask,
3254 audio_format_t format,
3255 size_t frameCount,
Eric Laurent78b07302022-10-07 16:20:34 +02003256 audio_input_flags_t flags,
3257 audio_source_t source)
Mikhail Naganovcaf59942019-09-25 14:05:29 -07003258 : PatchRecord(recordThread, sampleRate, channelMask, format, frameCount,
Eric Laurent78b07302022-10-07 16:20:34 +02003259 nullptr /*buffer*/, 0 /*bufferSize*/, flags, {} /* timeout */, source),
Mikhail Naganovcaf59942019-09-25 14:05:29 -07003260 mPatchRecordAudioBufferProvider(*this),
3261 mSinkBuffer(allocAligned(32, mFrameCount * mFrameSize)),
3262 mStubBuffer(allocAligned(32, mFrameCount * mFrameSize))
3263{
3264 memset(mStubBuffer.get(), 0, mFrameCount * mFrameSize);
3265}
3266
Andy Hung3ff4b552023-06-26 19:20:57 -07003267sp<StreamInHalInterface> PassthruPatchRecord::obtainStream(
Andy Hung44f27182023-07-06 20:56:16 -07003268 sp<IAfThreadBase>* thread)
Mikhail Naganovcaf59942019-09-25 14:05:29 -07003269{
3270 *thread = mThread.promote();
3271 if (!*thread) return nullptr;
Andy Hung44f27182023-07-06 20:56:16 -07003272 auto* const recordThread = (*thread)->asIAfRecordThread().get();
3273 Mutex::Autolock _l(recordThread->mutex());
3274 return recordThread->getInput() ? recordThread->getInput()->stream : nullptr;
Mikhail Naganovcaf59942019-09-25 14:05:29 -07003275}
3276
3277// PatchProxyBufferProvider methods are called on DirectOutputThread
Andy Hung3ff4b552023-06-26 19:20:57 -07003278status_t PassthruPatchRecord::obtainBuffer(
Mikhail Naganovcaf59942019-09-25 14:05:29 -07003279 Proxy::Buffer* buffer, const struct timespec* timeOut)
3280{
3281 if (mUnconsumedFrames) {
3282 buffer->mFrameCount = std::min(buffer->mFrameCount, mUnconsumedFrames);
3283 // mUnconsumedFrames is decreased in releaseBuffer to use actual frame consumption figure.
3284 return PatchRecord::obtainBuffer(buffer, timeOut);
3285 }
3286
3287 // Otherwise, execute a read from HAL and write into the buffer.
3288 nsecs_t startTimeNs = 0;
3289 if (timeOut && (timeOut->tv_sec != 0 || timeOut->tv_nsec != 0) && timeOut->tv_sec != INT_MAX) {
3290 // Will need to correct timeOut by elapsed time.
3291 startTimeNs = systemTime();
3292 }
3293 const size_t framesToRead = std::min(buffer->mFrameCount, mFrameCount);
3294 buffer->mFrameCount = 0;
3295 buffer->mRaw = nullptr;
Andy Hung44f27182023-07-06 20:56:16 -07003296 sp<IAfThreadBase> thread;
Mikhail Naganovcaf59942019-09-25 14:05:29 -07003297 sp<StreamInHalInterface> stream = obtainStream(&thread);
3298 if (!stream) return NO_INIT; // If there is no stream, RecordThread is not reading.
3299
3300 status_t result = NO_ERROR;
Mikhail Naganovcaf59942019-09-25 14:05:29 -07003301 size_t bytesRead = 0;
3302 {
3303 ATRACE_NAME("read");
3304 result = stream->read(mSinkBuffer.get(), framesToRead * mFrameSize, &bytesRead);
3305 if (result != NO_ERROR) goto stream_error;
3306 if (bytesRead == 0) return NO_ERROR;
3307 }
3308
3309 {
3310 std::lock_guard<std::mutex> lock(mReadLock);
3311 mReadBytes += bytesRead;
3312 mReadError = NO_ERROR;
3313 }
3314 mReadCV.notify_one();
3315 // writeFrames handles wraparound and should write all the provided frames.
3316 // If it couldn't, there is something wrong with the client/server buffer of the software patch.
3317 buffer->mFrameCount = writeFrames(
3318 &mPatchRecordAudioBufferProvider,
3319 mSinkBuffer.get(), bytesRead / mFrameSize, mFrameSize);
3320 ALOGW_IF(buffer->mFrameCount < bytesRead / mFrameSize,
3321 "Lost %zu frames obtained from HAL", bytesRead / mFrameSize - buffer->mFrameCount);
3322 mUnconsumedFrames = buffer->mFrameCount;
Mikhail Naganov4de49972019-10-07 09:53:58 -07003323 struct timespec newTimeOut;
Mikhail Naganovcaf59942019-09-25 14:05:29 -07003324 if (startTimeNs) {
Mikhail Naganov4de49972019-10-07 09:53:58 -07003325 // Correct the timeout by elapsed time.
3326 nsecs_t newTimeOutNs = audio_utils_ns_from_timespec(timeOut) - (systemTime() - startTimeNs);
Mikhail Naganovcaf59942019-09-25 14:05:29 -07003327 if (newTimeOutNs < 0) newTimeOutNs = 0;
3328 newTimeOut.tv_sec = newTimeOutNs / NANOS_PER_SECOND;
3329 newTimeOut.tv_nsec = newTimeOutNs - newTimeOut.tv_sec * NANOS_PER_SECOND;
Mikhail Naganov4de49972019-10-07 09:53:58 -07003330 timeOut = &newTimeOut;
Mikhail Naganovcaf59942019-09-25 14:05:29 -07003331 }
Mikhail Naganov4de49972019-10-07 09:53:58 -07003332 return PatchRecord::obtainBuffer(buffer, timeOut);
Mikhail Naganovcaf59942019-09-25 14:05:29 -07003333
3334stream_error:
3335 stream->standby();
3336 {
3337 std::lock_guard<std::mutex> lock(mReadLock);
3338 mReadError = result;
3339 }
3340 mReadCV.notify_one();
3341 return result;
3342}
3343
Andy Hung3ff4b552023-06-26 19:20:57 -07003344void PassthruPatchRecord::releaseBuffer(Proxy::Buffer* buffer)
Mikhail Naganovcaf59942019-09-25 14:05:29 -07003345{
3346 if (buffer->mFrameCount <= mUnconsumedFrames) {
3347 mUnconsumedFrames -= buffer->mFrameCount;
3348 } else {
3349 ALOGW("Write side has consumed more frames than we had: %zu > %zu",
3350 buffer->mFrameCount, mUnconsumedFrames);
3351 mUnconsumedFrames = 0;
3352 }
3353 PatchRecord::releaseBuffer(buffer);
3354}
3355
3356// AudioBufferProvider and Source methods are called on RecordThread
3357// 'read' emulates actual audio data with 0's. This is OK as 'getNextBuffer'
3358// and 'releaseBuffer' are stubbed out and ignore their input.
3359// It's not possible to retrieve actual data here w/o blocking 'obtainBuffer'
3360// until we copy it.
Andy Hung3ff4b552023-06-26 19:20:57 -07003361status_t PassthruPatchRecord::read(
Mikhail Naganovcaf59942019-09-25 14:05:29 -07003362 void* buffer, size_t bytes, size_t* read)
3363{
3364 bytes = std::min(bytes, mFrameCount * mFrameSize);
3365 {
3366 std::unique_lock<std::mutex> lock(mReadLock);
3367 mReadCV.wait(lock, [&]{ return mReadError != NO_ERROR || mReadBytes != 0; });
3368 if (mReadError != NO_ERROR) {
3369 mLastReadFrames = 0;
3370 return mReadError;
3371 }
3372 *read = std::min(bytes, mReadBytes);
3373 mReadBytes -= *read;
3374 }
3375 mLastReadFrames = *read / mFrameSize;
3376 memset(buffer, 0, *read);
3377 return 0;
3378}
3379
Andy Hung3ff4b552023-06-26 19:20:57 -07003380status_t PassthruPatchRecord::getCapturePosition(
Mikhail Naganovcaf59942019-09-25 14:05:29 -07003381 int64_t* frames, int64_t* time)
3382{
Andy Hung44f27182023-07-06 20:56:16 -07003383 sp<IAfThreadBase> thread;
Mikhail Naganovcaf59942019-09-25 14:05:29 -07003384 sp<StreamInHalInterface> stream = obtainStream(&thread);
3385 return stream ? stream->getCapturePosition(frames, time) : NO_INIT;
3386}
3387
Andy Hung3ff4b552023-06-26 19:20:57 -07003388status_t PassthruPatchRecord::standby()
Mikhail Naganovcaf59942019-09-25 14:05:29 -07003389{
3390 // RecordThread issues 'standby' command in two major cases:
3391 // 1. Error on read--this case is handled in 'obtainBuffer'.
3392 // 2. Track is stopping--as PassthruPatchRecord assumes continuous
3393 // output, this can only happen when the software patch
3394 // is being torn down. In this case, the RecordThread
3395 // will terminate and close the HAL stream.
3396 return 0;
3397}
3398
3399// As the buffer gets filled in obtainBuffer, here we only simulate data consumption.
Andy Hung3ff4b552023-06-26 19:20:57 -07003400status_t PassthruPatchRecord::getNextBuffer(
Mikhail Naganovcaf59942019-09-25 14:05:29 -07003401 AudioBufferProvider::Buffer* buffer)
3402{
3403 buffer->frameCount = mLastReadFrames;
3404 buffer->raw = buffer->frameCount != 0 ? mStubBuffer.get() : nullptr;
3405 return NO_ERROR;
3406}
3407
Andy Hung3ff4b552023-06-26 19:20:57 -07003408void PassthruPatchRecord::releaseBuffer(
Mikhail Naganovcaf59942019-09-25 14:05:29 -07003409 AudioBufferProvider::Buffer* buffer)
3410{
3411 buffer->frameCount = 0;
3412 buffer->raw = nullptr;
3413}
3414
Andy Hung9d84af52018-09-12 18:03:44 -07003415// ----------------------------------------------------------------------------
3416#undef LOG_TAG
3417#define LOG_TAG "AF::MmapTrack"
Eric Laurent6acd1d42017-01-04 14:23:29 -08003418
Andy Hung3ff4b552023-06-26 19:20:57 -07003419/* static */
Andy Hung44f27182023-07-06 20:56:16 -07003420sp<IAfMmapTrack> IAfMmapTrack::create(IAfThreadBase* thread,
Andy Hung3ff4b552023-06-26 19:20:57 -07003421 const audio_attributes_t& attr,
3422 uint32_t sampleRate,
3423 audio_format_t format,
3424 audio_channel_mask_t channelMask,
3425 audio_session_t sessionId,
3426 bool isOut,
3427 const android::content::AttributionSourceState& attributionSource,
3428 pid_t creatorPid,
3429 audio_port_handle_t portId)
3430{
3431 return sp<MmapTrack>::make(
Andy Hung44f27182023-07-06 20:56:16 -07003432 thread,
Andy Hung3ff4b552023-06-26 19:20:57 -07003433 attr,
3434 sampleRate,
3435 format,
3436 channelMask,
3437 sessionId,
3438 isOut,
3439 attributionSource,
3440 creatorPid,
3441 portId);
3442}
3443
Andy Hung44f27182023-07-06 20:56:16 -07003444MmapTrack::MmapTrack(IAfThreadBase* thread,
Kevin Rocard1f564ac2018-03-29 13:53:10 -07003445 const audio_attributes_t& attr,
Eric Laurent6acd1d42017-01-04 14:23:29 -08003446 uint32_t sampleRate,
3447 audio_format_t format,
3448 audio_channel_mask_t channelMask,
3449 audio_session_t sessionId,
Kevin Rocard5f2136e2018-05-11 22:03:00 -07003450 bool isOut,
Svet Ganov33761132021-05-13 22:51:08 +00003451 const AttributionSourceState& attributionSource,
Eric Laurent09f1ed22019-04-24 17:45:17 -07003452 pid_t creatorPid,
Eric Laurent6acd1d42017-01-04 14:23:29 -08003453 audio_port_handle_t portId)
Kevin Rocard1f564ac2018-03-29 13:53:10 -07003454 : TrackBase(thread, NULL, attr, sampleRate, format,
Andy Hung8fe68032017-06-05 16:17:51 -07003455 channelMask, (size_t)0 /* frameCount */,
3456 nullptr /* buffer */, (size_t)0 /* bufferSize */,
Philip P. Moltmannbda45752020-07-17 16:41:18 -07003457 sessionId, creatorPid,
Svet Ganov33761132021-05-13 22:51:08 +00003458 VALUE_OR_FATAL(aidl2legacy_int32_t_uid_t(attributionSource.uid)),
Philip P. Moltmannbda45752020-07-17 16:41:18 -07003459 isOut,
Eric Laurent6acd1d42017-01-04 14:23:29 -08003460 ALLOC_NONE,
Andy Hungc2b11cb2020-04-22 09:04:01 -07003461 TYPE_DEFAULT, portId,
3462 std::string(AMEDIAMETRICS_KEY_PREFIX_AUDIO_MMAP) + std::to_string(portId)),
Svet Ganov33761132021-05-13 22:51:08 +00003463 mPid(VALUE_OR_FATAL(aidl2legacy_int32_t_uid_t(attributionSource.pid))),
Philip P. Moltmannbda45752020-07-17 16:41:18 -07003464 mSilenced(false), mSilencedNotified(false)
Eric Laurent6acd1d42017-01-04 14:23:29 -08003465{
Andy Hungc2b11cb2020-04-22 09:04:01 -07003466 // Once this item is logged by the server, the client can add properties.
Andy Hung94235282021-03-24 15:50:14 -07003467 mTrackMetrics.logConstructor(creatorPid, uid(), id());
Eric Laurent6acd1d42017-01-04 14:23:29 -08003468}
3469
Andy Hung3ff4b552023-06-26 19:20:57 -07003470MmapTrack::~MmapTrack()
Eric Laurent6acd1d42017-01-04 14:23:29 -08003471{
3472}
3473
Andy Hung3ff4b552023-06-26 19:20:57 -07003474status_t MmapTrack::initCheck() const
Eric Laurent6acd1d42017-01-04 14:23:29 -08003475{
3476 return NO_ERROR;
3477}
3478
Andy Hung3ff4b552023-06-26 19:20:57 -07003479status_t MmapTrack::start(AudioSystem::sync_event_t event __unused,
Kevin Rocard5f2136e2018-05-11 22:03:00 -07003480 audio_session_t triggerSession __unused)
Eric Laurent6acd1d42017-01-04 14:23:29 -08003481{
3482 return NO_ERROR;
3483}
3484
Andy Hung3ff4b552023-06-26 19:20:57 -07003485void MmapTrack::stop()
Eric Laurent6acd1d42017-01-04 14:23:29 -08003486{
3487}
3488
3489// AudioBufferProvider interface
Andy Hung3ff4b552023-06-26 19:20:57 -07003490status_t MmapTrack::getNextBuffer(AudioBufferProvider::Buffer* buffer)
Eric Laurent6acd1d42017-01-04 14:23:29 -08003491{
3492 buffer->frameCount = 0;
3493 buffer->raw = nullptr;
3494 return INVALID_OPERATION;
3495}
3496
3497// ExtendedAudioBufferProvider interface
Andy Hung3ff4b552023-06-26 19:20:57 -07003498size_t MmapTrack::framesReady() const {
Eric Laurent6acd1d42017-01-04 14:23:29 -08003499 return 0;
3500}
3501
Andy Hung3ff4b552023-06-26 19:20:57 -07003502int64_t MmapTrack::framesReleased() const
Eric Laurent6acd1d42017-01-04 14:23:29 -08003503{
3504 return 0;
3505}
3506
Andy Hung3ff4b552023-06-26 19:20:57 -07003507void MmapTrack::onTimestamp(const ExtendedTimestamp& timestamp __unused)
Eric Laurent6acd1d42017-01-04 14:23:29 -08003508{
3509}
3510
Andy Hung3ff4b552023-06-26 19:20:57 -07003511void MmapTrack::processMuteEvent_l(const sp<IAudioManager>& audioManager, mute_state_t muteState)
Vlad Popaec1788e2022-08-04 11:23:30 +02003512{
3513 if (mMuteState == muteState) {
3514 // mute state did not change, do nothing
3515 return;
3516 }
3517
3518 status_t result = UNKNOWN_ERROR;
3519 if (audioManager && mPortId != AUDIO_PORT_HANDLE_NONE) {
3520 if (mMuteEventExtras == nullptr) {
3521 mMuteEventExtras = std::make_unique<os::PersistableBundle>();
3522 }
3523 mMuteEventExtras->putInt(String16(kExtraPlayerEventMuteKey),
3524 static_cast<int>(muteState));
3525
3526 result = audioManager->portEvent(mPortId,
3527 PLAYER_UPDATE_MUTED,
3528 mMuteEventExtras);
3529 }
3530
3531 if (result == OK) {
3532 mMuteState = muteState;
3533 } else {
3534 ALOGW("%s(%d): cannot process mute state for port ID %d, status error %d",
3535 __func__,
3536 id(),
3537 mPortId,
3538 result);
3539 }
3540}
3541
Andy Hung3ff4b552023-06-26 19:20:57 -07003542void MmapTrack::appendDumpHeader(String8& result) const
Eric Laurent6acd1d42017-01-04 14:23:29 -08003543{
Eric Laurent973db022018-11-20 14:54:31 -08003544 result.appendFormat("Client Session Port Id Format Chn mask SRate Flags %s\n",
Kevin Rocard5f2136e2018-05-11 22:03:00 -07003545 isOut() ? "Usg CT": "Source");
Eric Laurent6acd1d42017-01-04 14:23:29 -08003546}
3547
Andy Hung3ff4b552023-06-26 19:20:57 -07003548void MmapTrack::appendDump(String8& result, bool active __unused) const
Eric Laurent6acd1d42017-01-04 14:23:29 -08003549{
Eric Laurent973db022018-11-20 14:54:31 -08003550 result.appendFormat("%6u %7u %7u %08X %08X %6u 0x%03X ",
Andy Hung2c6c3bb2017-06-16 14:01:45 -07003551 mPid,
3552 mSessionId,
Eric Laurent973db022018-11-20 14:54:31 -08003553 mPortId,
Eric Laurent6acd1d42017-01-04 14:23:29 -08003554 mFormat,
3555 mChannelMask,
Kevin Rocard5f2136e2018-05-11 22:03:00 -07003556 mSampleRate,
3557 mAttr.flags);
3558 if (isOut()) {
3559 result.appendFormat("%3x %2x", mAttr.usage, mAttr.content_type);
3560 } else {
3561 result.appendFormat("%6x", mAttr.source);
3562 }
3563 result.append("\n");
Eric Laurent6acd1d42017-01-04 14:23:29 -08003564}
3565
Glenn Kasten63238ef2015-03-02 15:50:29 -08003566} // namespace android