blob: 6cb3d34a4421cee4006b3228b650de754a4de2d9 [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
18
19#define LOG_TAG "AudioFlinger"
20//#define LOG_NDEBUG 0
Mikhail Naganov938be412019-09-04 11:38:47 -070021#define ATRACE_TAG ATRACE_TAG_AUDIO
Eric Laurent81784c32012-11-19 14:55:58 -080022
Glenn Kasten153b9fe2013-07-15 11:23:36 -070023#include "Configuration.h"
Glenn Kastenad8510a2015-02-17 16:24:07 -080024#include <linux/futex.h>
Eric Laurent81784c32012-11-19 14:55:58 -080025#include <math.h>
Elliott Hughesee499292014-05-21 17:55:51 -070026#include <sys/syscall.h>
Eric Laurent81784c32012-11-19 14:55:58 -080027#include <utils/Log.h>
Mikhail Naganov938be412019-09-04 11:38:47 -070028#include <utils/Trace.h>
Eric Laurent81784c32012-11-19 14:55:58 -080029
30#include <private/media/AudioTrackShared.h>
31
Eric Laurent81784c32012-11-19 14:55:58 -080032#include "AudioFlinger.h"
Andy Hung8d31fd22023-06-26 19:20:57 -070033#include "TrackBase.h"
34#include "PlaybackTracks.h"
35#include "RecordTracks.h"
36#include "MmapTracks.h"
Eric Laurent81784c32012-11-19 14:55:58 -080037
Glenn Kastenda6ef132013-01-10 12:31:01 -080038#include <media/nbaio/Pipe.h>
39#include <media/nbaio/PipeReader.h>
Kuowei Lid4adbdb2020-08-13 14:44:25 +080040#include <media/AudioValidator.h>
Andy Hung89816052017-01-11 17:08:23 -080041#include <media/RecordBufferConverter.h>
Andy Hungab7ef302018-05-15 19:35:29 -070042#include <mediautils/ServiceUtilities.h>
Glenn Kastenc56f3422014-03-21 17:53:17 -070043#include <audio_utils/minifloat.h>
Glenn Kastenda6ef132013-01-10 12:31:01 -080044
Eric Laurent81784c32012-11-19 14:55:58 -080045// ----------------------------------------------------------------------------
46
47// Note: the following macro is used for extremely verbose logging message. In
48// order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to
49// 0; but one side effect of this is to turn all LOGV's as well. Some messages
50// are so verbose that we want to suppress them even when we have ALOG_ASSERT
51// turned on. Do not uncomment the #def below unless you really know what you
52// are doing and want to see all of the extremely verbose messages.
53//#define VERY_VERY_VERBOSE_LOGGING
54#ifdef VERY_VERY_VERBOSE_LOGGING
55#define ALOGVV ALOGV
56#else
57#define ALOGVV(a...) do { } while(0)
58#endif
59
Kuowei Lid4adbdb2020-08-13 14:44:25 +080060// TODO: Remove when this is put into AidlConversionUtil.h
61#define VALUE_OR_RETURN_BINDER_STATUS(x) \
62 ({ \
63 auto _tmp = (x); \
64 if (!_tmp.ok()) return ::android::aidl_utils::binderStatusFromStatusT(_tmp.error()); \
65 std::move(_tmp.value()); \
66 })
67
Eric Laurent81784c32012-11-19 14:55:58 -080068namespace android {
69
Kuowei Lid4adbdb2020-08-13 14:44:25 +080070using ::android::aidl_utils::binderStatusFromStatusT;
Ytai Ben-Tsvibdc293a2020-11-02 17:01:38 -080071using binder::Status;
Svet Ganov33761132021-05-13 22:51:08 +000072using content::AttributionSourceState;
Ivan Lozano8cf3a072017-08-09 09:01:33 -070073using media::VolumeShaper;
Eric Laurent81784c32012-11-19 14:55:58 -080074// ----------------------------------------------------------------------------
75// TrackBase
76// ----------------------------------------------------------------------------
Andy Hung9d84af52018-09-12 18:03:44 -070077#undef LOG_TAG
78#define LOG_TAG "AF::TrackBase"
Eric Laurent81784c32012-11-19 14:55:58 -080079
Glenn Kastenda6ef132013-01-10 12:31:01 -080080static volatile int32_t nextTrackId = 55;
81
Eric Laurent81784c32012-11-19 14:55:58 -080082// TrackBase constructor must be called with AudioFlinger::mLock held
Andy Hung8d31fd22023-06-26 19:20:57 -070083TrackBase::TrackBase(
Andy Hung87c693c2023-07-06 20:56:16 -070084 IAfThreadBase *thread,
Eric Laurent81784c32012-11-19 14:55:58 -080085 const sp<Client>& client,
Kevin Rocard1f564ac2018-03-29 13:53:10 -070086 const audio_attributes_t& attr,
Eric Laurent81784c32012-11-19 14:55:58 -080087 uint32_t sampleRate,
88 audio_format_t format,
89 audio_channel_mask_t channelMask,
90 size_t frameCount,
Eric Laurent83b88082014-06-20 18:31:16 -070091 void *buffer,
Andy Hung8fe68032017-06-05 16:17:51 -070092 size_t bufferSize,
Glenn Kastend848eb42016-03-08 13:42:11 -080093 audio_session_t sessionId,
Eric Laurent09f1ed22019-04-24 17:45:17 -070094 pid_t creatorPid,
Andy Hung1f12a8a2016-11-07 16:10:30 -080095 uid_t clientUid,
Glenn Kastend776ac62014-05-07 09:16:09 -070096 bool isOut,
Dmitry Sidorenkova41c2732023-05-15 13:47:07 -070097 const alloc_type alloc,
Eric Laurent20b9ef02016-12-05 11:03:16 -080098 track_type type,
Andy Hungb68f5eb2019-12-03 16:49:17 -080099 audio_port_handle_t portId,
100 std::string metricsId)
Andy Hungd29af632023-06-23 19:27:19 -0700101 :
Eric Laurent81784c32012-11-19 14:55:58 -0800102 mThread(thread),
Dmitry Sidorenkova41c2732023-05-15 13:47:07 -0700103 mAllocType(alloc),
Eric Laurent81784c32012-11-19 14:55:58 -0800104 mClient(client),
105 mCblk(NULL),
Andy Hung8fe68032017-06-05 16:17:51 -0700106 // mBuffer, mBufferSize
Eric Laurent81784c32012-11-19 14:55:58 -0800107 mState(IDLE),
Kevin Rocard1f564ac2018-03-29 13:53:10 -0700108 mAttr(attr),
Eric Laurent81784c32012-11-19 14:55:58 -0800109 mSampleRate(sampleRate),
110 mFormat(format),
111 mChannelMask(channelMask),
Andy Hunge5412692014-05-16 11:25:07 -0700112 mChannelCount(isOut ?
113 audio_channel_count_from_out_mask(channelMask) :
114 audio_channel_count_from_in_mask(channelMask)),
Phil Burkfdb3c072016-02-09 10:47:02 -0800115 mFrameSize(audio_has_proportional_frames(format) ?
Eric Laurent81784c32012-11-19 14:55:58 -0800116 mChannelCount * audio_bytes_per_sample(format) : sizeof(int8_t)),
117 mFrameCount(frameCount),
Glenn Kastene3aa6592012-12-04 12:22:46 -0800118 mSessionId(sessionId),
119 mIsOut(isOut),
Eric Laurentbfb1b832013-01-07 09:53:42 -0800120 mId(android_atomic_inc(&nextTrackId)),
Eric Laurent83b88082014-06-20 18:31:16 -0700121 mTerminated(false),
Eric Laurentaaa44472014-09-12 17:41:50 -0700122 mType(type),
Kevin Rocard153f92d2018-12-18 18:33:28 -0800123 mThreadIoHandle(thread ? thread->id() : AUDIO_IO_HANDLE_NONE),
Eric Laurent6acd1d42017-01-04 14:23:29 -0800124 mPortId(portId),
Eric Laurent09f1ed22019-04-24 17:45:17 -0700125 mIsInvalid(false),
Kunal Malhotra3be68902023-02-28 22:03:15 +0000126 mTrackMetrics(std::move(metricsId), isOut, clientUid),
Eric Laurent09f1ed22019-04-24 17:45:17 -0700127 mCreatorPid(creatorPid)
Eric Laurent81784c32012-11-19 14:55:58 -0800128{
Marco Nelissendcb346b2015-09-09 10:47:29 -0700129 const uid_t callingUid = IPCThreadState::self()->getCallingUid();
Andy Hung4ef19fa2018-05-15 19:35:29 -0700130 if (!isAudioServerOrMediaServerUid(callingUid) || clientUid == AUDIO_UID_INVALID) {
Andy Hung1f12a8a2016-11-07 16:10:30 -0800131 ALOGW_IF(clientUid != AUDIO_UID_INVALID && clientUid != callingUid,
Andy Hung9d84af52018-09-12 18:03:44 -0700132 "%s(%d): uid %d tried to pass itself off as %d",
133 __func__, mId, callingUid, clientUid);
Andy Hung1f12a8a2016-11-07 16:10:30 -0800134 clientUid = callingUid;
Marco Nelissen462fd2f2013-01-14 14:12:05 -0800135 }
136 // clientUid contains the uid of the app that is responsible for this track, so we can blame
137 // battery usage on it.
138 mUid = clientUid;
139
Eric Laurent81784c32012-11-19 14:55:58 -0800140 // ALOGD("Creating track with %d buffers @ %d bytes", bufferCount, bufferSize);
Andy Hung1883f692017-02-13 18:48:39 -0800141
Andy Hung8fe68032017-06-05 16:17:51 -0700142 size_t minBufferSize = buffer == NULL ? roundup(frameCount) : frameCount;
Andy Hung1883f692017-02-13 18:48:39 -0800143 // check overflow when computing bufferSize due to multiplication by mFrameSize.
Andy Hung8fe68032017-06-05 16:17:51 -0700144 if (minBufferSize < frameCount // roundup rounds down for values above UINT_MAX / 2
Andy Hung1883f692017-02-13 18:48:39 -0800145 || mFrameSize == 0 // format needs to be correct
Andy Hung8fe68032017-06-05 16:17:51 -0700146 || minBufferSize > SIZE_MAX / mFrameSize) {
Andy Hung1883f692017-02-13 18:48:39 -0800147 android_errorWriteLog(0x534e4554, "34749571");
148 return;
149 }
Andy Hung8fe68032017-06-05 16:17:51 -0700150 minBufferSize *= mFrameSize;
151
152 if (buffer == nullptr) {
153 bufferSize = minBufferSize; // allocated here.
154 } else if (minBufferSize > bufferSize) {
155 android_errorWriteLog(0x534e4554, "38340117");
156 return;
157 }
Andy Hung1883f692017-02-13 18:48:39 -0800158
Eric Laurent81784c32012-11-19 14:55:58 -0800159 size_t size = sizeof(audio_track_cblk_t);
Eric Laurent83b88082014-06-20 18:31:16 -0700160 if (buffer == NULL && alloc == ALLOC_CBLK) {
Andy Hung1883f692017-02-13 18:48:39 -0800161 // check overflow when computing allocation size for streaming tracks.
162 if (size > SIZE_MAX - bufferSize) {
163 android_errorWriteLog(0x534e4554, "34749571");
164 return;
165 }
Eric Laurent81784c32012-11-19 14:55:58 -0800166 size += bufferSize;
167 }
168
169 if (client != 0) {
Atneya3c61d882021-09-20 14:52:15 -0400170 mCblkMemory = client->allocator().allocate(mediautils::NamedAllocRequest{{size},
171 std::string("Track ID: ").append(std::to_string(mId))});
Glenn Kasten663c2242013-09-24 11:52:37 -0700172 if (mCblkMemory == 0 ||
Ytai Ben-Tsvi7dd39722019-09-05 15:14:30 -0700173 (mCblk = static_cast<audio_track_cblk_t *>(mCblkMemory->unsecurePointer())) == NULL) {
Andy Hung9d84af52018-09-12 18:03:44 -0700174 ALOGE("%s(%d): not enough memory for AudioTrack size=%zu", __func__, mId, size);
Atneya3c61d882021-09-20 14:52:15 -0400175 ALOGE("%s", client->allocator().dump().c_str());
Glenn Kasten663c2242013-09-24 11:52:37 -0700176 mCblkMemory.clear();
Eric Laurent81784c32012-11-19 14:55:58 -0800177 return;
178 }
179 } else {
Andy Hungafb31482017-02-13 18:50:48 -0800180 mCblk = (audio_track_cblk_t *) malloc(size);
181 if (mCblk == NULL) {
Andy Hung9d84af52018-09-12 18:03:44 -0700182 ALOGE("%s(%d): not enough memory for AudioTrack size=%zu", __func__, mId, size);
Andy Hungafb31482017-02-13 18:50:48 -0800183 return;
184 }
Eric Laurent81784c32012-11-19 14:55:58 -0800185 }
186
187 // construct the shared structure in-place.
188 if (mCblk != NULL) {
189 new(mCblk) audio_track_cblk_t();
Glenn Kastenc263ca02014-06-04 20:31:46 -0700190 switch (alloc) {
191 case ALLOC_READONLY: {
Glenn Kastend776ac62014-05-07 09:16:09 -0700192 const sp<MemoryDealer> roHeap(thread->readOnlyHeap());
193 if (roHeap == 0 ||
194 (mBufferMemory = roHeap->allocate(bufferSize)) == 0 ||
Ytai Ben-Tsvi7dd39722019-09-05 15:14:30 -0700195 (mBuffer = mBufferMemory->unsecurePointer()) == NULL) {
Andy Hung9d84af52018-09-12 18:03:44 -0700196 ALOGE("%s(%d): not enough memory for read-only buffer size=%zu",
197 __func__, mId, bufferSize);
Glenn Kastend776ac62014-05-07 09:16:09 -0700198 if (roHeap != 0) {
199 roHeap->dump("buffer");
200 }
201 mCblkMemory.clear();
202 mBufferMemory.clear();
203 return;
204 }
Eric Laurent81784c32012-11-19 14:55:58 -0800205 memset(mBuffer, 0, bufferSize);
Glenn Kastenc263ca02014-06-04 20:31:46 -0700206 } break;
207 case ALLOC_PIPE:
208 mBufferMemory = thread->pipeMemory();
209 // mBuffer is the virtual address as seen from current process (mediaserver),
Ytai Ben-Tsvi7dd39722019-09-05 15:14:30 -0700210 // and should normally be coming from mBufferMemory->unsecurePointer().
Glenn Kastenc263ca02014-06-04 20:31:46 -0700211 // However in this case the TrackBase does not reference the buffer directly.
212 // It should references the buffer via the pipe.
213 // Therefore, to detect incorrect usage of the buffer, we set mBuffer to NULL.
214 mBuffer = NULL;
Andy Hung8fe68032017-06-05 16:17:51 -0700215 bufferSize = 0;
Glenn Kastenc263ca02014-06-04 20:31:46 -0700216 break;
217 case ALLOC_CBLK:
Glenn Kastend776ac62014-05-07 09:16:09 -0700218 // clear all buffers
Eric Laurent83b88082014-06-20 18:31:16 -0700219 if (buffer == NULL) {
Glenn Kastend776ac62014-05-07 09:16:09 -0700220 mBuffer = (char*)mCblk + sizeof(audio_track_cblk_t);
221 memset(mBuffer, 0, bufferSize);
222 } else {
Eric Laurent83b88082014-06-20 18:31:16 -0700223 mBuffer = buffer;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800224#if 0
Glenn Kastend776ac62014-05-07 09:16:09 -0700225 mCblk->mFlags = CBLK_FORCEREADY; // FIXME hack, need to fix the track ready logic
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800226#endif
Glenn Kastend776ac62014-05-07 09:16:09 -0700227 }
Glenn Kastenc263ca02014-06-04 20:31:46 -0700228 break;
Eric Laurent83b88082014-06-20 18:31:16 -0700229 case ALLOC_LOCAL:
230 mBuffer = calloc(1, bufferSize);
231 break;
232 case ALLOC_NONE:
233 mBuffer = buffer;
234 break;
Andy Hung8fe68032017-06-05 16:17:51 -0700235 default:
Andy Hung9d84af52018-09-12 18:03:44 -0700236 LOG_ALWAYS_FATAL("%s(%d): invalid allocation type: %d", __func__, mId, (int)alloc);
Eric Laurent81784c32012-11-19 14:55:58 -0800237 }
Andy Hung8fe68032017-06-05 16:17:51 -0700238 mBufferSize = bufferSize;
Glenn Kastenda6ef132013-01-10 12:31:01 -0800239
Glenn Kasten46909e72013-02-26 09:20:22 -0800240#ifdef TEE_SINK
Andy Hung8946a282018-04-19 20:04:56 -0700241 mTee.set(sampleRate, mChannelCount, format, NBAIO_Tee::TEE_FLAG_TRACK);
Glenn Kasten46909e72013-02-26 09:20:22 -0800242#endif
Andy Hung959b5b82021-09-24 10:46:20 -0700243 // mState is mirrored for the client to read.
244 mState.setMirror(&mCblk->mState);
245 // ensure our state matches up until we consolidate the enumeration.
246 static_assert(CBLK_STATE_IDLE == IDLE);
247 static_assert(CBLK_STATE_PAUSING == PAUSING);
Eric Laurent81784c32012-11-19 14:55:58 -0800248 }
249}
250
Svet Ganov33761132021-05-13 22:51:08 +0000251// TODO b/182392769: use attribution source util
252static AttributionSourceState audioServerAttributionSource(pid_t pid) {
253 AttributionSourceState attributionSource{};
254 attributionSource.uid = AID_AUDIOSERVER;
255 attributionSource.pid = pid;
256 attributionSource.token = sp<BBinder>::make();
257 return attributionSource;
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700258}
259
Andy Hung8d31fd22023-06-26 19:20:57 -0700260status_t TrackBase::initCheck() const
Eric Laurent83b88082014-06-20 18:31:16 -0700261{
262 status_t status;
263 if (mType == TYPE_OUTPUT || mType == TYPE_PATCH) {
264 status = cblk() != NULL ? NO_ERROR : NO_MEMORY;
265 } else {
266 status = getCblk() != 0 ? NO_ERROR : NO_MEMORY;
267 }
268 return status;
269}
270
Andy Hung8d31fd22023-06-26 19:20:57 -0700271TrackBase::~TrackBase()
Eric Laurent81784c32012-11-19 14:55:58 -0800272{
Glenn Kastene3aa6592012-12-04 12:22:46 -0800273 // delete the proxy before deleting the shared memory it refers to, to avoid dangling reference
Eric Laurent5bba2f62016-03-18 11:14:14 -0700274 mServerProxy.clear();
Andy Hung689e82c2019-08-21 17:53:17 -0700275 releaseCblk();
Eric Laurent81784c32012-11-19 14:55:58 -0800276 mCblkMemory.clear(); // free the shared memory before releasing the heap it belongs to
277 if (mClient != 0) {
Eric Laurent021cf962014-05-13 10:18:14 -0700278 // Client destructor must run with AudioFlinger client mutex locked
279 Mutex::Autolock _l(mClient->audioFlinger()->mClientLock);
Eric Laurent81784c32012-11-19 14:55:58 -0800280 // If the client's reference count drops to zero, the associated destructor
281 // must run with AudioFlinger lock held. Thus the explicit clear() rather than
282 // relying on the automatic clear() at end of scope.
283 mClient.clear();
284 }
Dmitry Sidorenkova41c2732023-05-15 13:47:07 -0700285 if (mAllocType == ALLOC_LOCAL) {
286 free(mBuffer);
287 mBuffer = nullptr;
288 }
Eric Laurent3bcffa12014-06-12 18:38:45 -0700289 // flush the binder command buffer
290 IPCThreadState::self()->flushCommands();
Eric Laurent81784c32012-11-19 14:55:58 -0800291}
292
293// AudioBufferProvider interface
294// getNextBuffer() = 0;
Glenn Kastend79072e2016-01-06 08:41:20 -0800295// This implementation of releaseBuffer() is used by Track and RecordTrack
Andy Hung8d31fd22023-06-26 19:20:57 -0700296void TrackBase::releaseBuffer(AudioBufferProvider::Buffer* buffer)
Eric Laurent81784c32012-11-19 14:55:58 -0800297{
Glenn Kasten46909e72013-02-26 09:20:22 -0800298#ifdef TEE_SINK
Andy Hung8946a282018-04-19 20:04:56 -0700299 mTee.write(buffer->raw, buffer->frameCount);
Glenn Kasten46909e72013-02-26 09:20:22 -0800300#endif
Glenn Kastenda6ef132013-01-10 12:31:01 -0800301
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800302 ServerProxy::Buffer buf;
303 buf.mFrameCount = buffer->frameCount;
304 buf.mRaw = buffer->raw;
Eric Laurent81784c32012-11-19 14:55:58 -0800305 buffer->frameCount = 0;
Glenn Kasten9f80dd22012-12-18 15:57:32 -0800306 buffer->raw = NULL;
307 mServerProxy->releaseBuffer(&buf);
Eric Laurent81784c32012-11-19 14:55:58 -0800308}
309
Andy Hung8d31fd22023-06-26 19:20:57 -0700310status_t TrackBase::setSyncEvent(
Andy Hung068e08e2023-05-15 19:02:55 -0700311 const sp<audioflinger::SyncEvent>& event)
Eric Laurent81784c32012-11-19 14:55:58 -0800312{
Andy Hung068e08e2023-05-15 19:02:55 -0700313 mSyncEvents.emplace_back(event);
Eric Laurent81784c32012-11-19 14:55:58 -0800314 return NO_ERROR;
315}
316
Andy Hung8d31fd22023-06-26 19:20:57 -0700317PatchTrackBase::PatchTrackBase(const sp<ClientProxy>& proxy,
Andy Hung87c693c2023-07-06 20:56:16 -0700318 const IAfThreadBase& thread, const Timeout& timeout)
Kevin Rocard45986c72018-12-18 18:22:59 -0800319 : mProxy(proxy)
320{
321 if (timeout) {
322 setPeerTimeout(*timeout);
323 } else {
324 // Double buffer mixer
325 uint64_t mixBufferNs = ((uint64_t)2 * thread.frameCount() * 1000000000) /
326 thread.sampleRate();
327 setPeerTimeout(std::chrono::nanoseconds{mixBufferNs});
328 }
329}
330
Andy Hung8d31fd22023-06-26 19:20:57 -0700331void PatchTrackBase::setPeerTimeout(std::chrono::nanoseconds timeout) {
Kevin Rocard45986c72018-12-18 18:22:59 -0800332 mPeerTimeout.tv_sec = timeout.count() / std::nano::den;
333 mPeerTimeout.tv_nsec = timeout.count() % std::nano::den;
334}
335
336
Eric Laurent81784c32012-11-19 14:55:58 -0800337// ----------------------------------------------------------------------------
338// Playback
339// ----------------------------------------------------------------------------
Andy Hung9d84af52018-09-12 18:03:44 -0700340#undef LOG_TAG
341#define LOG_TAG "AF::TrackHandle"
Eric Laurent81784c32012-11-19 14:55:58 -0800342
Andy Hunga5a7fc92023-06-23 19:27:19 -0700343class TrackHandle : public android::media::BnAudioTrack {
344public:
Andy Hungd29af632023-06-23 19:27:19 -0700345 explicit TrackHandle(const sp<IAfTrack>& track);
Andy Hunga5a7fc92023-06-23 19:27:19 -0700346 ~TrackHandle() override;
347
348 binder::Status getCblk(std::optional<media::SharedFileRegion>* _aidl_return) final;
349 binder::Status start(int32_t* _aidl_return) final;
350 binder::Status stop() final;
351 binder::Status flush() final;
352 binder::Status pause() final;
353 binder::Status attachAuxEffect(int32_t effectId, int32_t* _aidl_return) final;
354 binder::Status setParameters(const std::string& keyValuePairs,
355 int32_t* _aidl_return) final;
356 binder::Status selectPresentation(int32_t presentationId, int32_t programId,
357 int32_t* _aidl_return) final;
358 binder::Status getTimestamp(media::AudioTimestampInternal* timestamp,
359 int32_t* _aidl_return) final;
360 binder::Status signal() final;
361 binder::Status applyVolumeShaper(const media::VolumeShaperConfiguration& configuration,
362 const media::VolumeShaperOperation& operation,
363 int32_t* _aidl_return) final;
364 binder::Status getVolumeShaperState(
365 int32_t id,
366 std::optional<media::VolumeShaperState>* _aidl_return) final;
367 binder::Status getDualMonoMode(
368 media::audio::common::AudioDualMonoMode* _aidl_return) final;
369 binder::Status setDualMonoMode(
370 media::audio::common::AudioDualMonoMode mode) final;
371 binder::Status getAudioDescriptionMixLevel(float* _aidl_return) final;
372 binder::Status setAudioDescriptionMixLevel(float leveldB) final;
373 binder::Status getPlaybackRateParameters(
374 media::audio::common::AudioPlaybackRate* _aidl_return) final;
375 binder::Status setPlaybackRateParameters(
376 const media::audio::common::AudioPlaybackRate& playbackRate) final;
377
378private:
Andy Hungd29af632023-06-23 19:27:19 -0700379 const sp<IAfTrack> mTrack;
Andy Hunga5a7fc92023-06-23 19:27:19 -0700380};
381
382/* static */
Andy Hungd29af632023-06-23 19:27:19 -0700383sp<media::IAudioTrack> IAfTrack::createIAudioTrackAdapter(const sp<IAfTrack>& track) {
Andy Hunga5a7fc92023-06-23 19:27:19 -0700384 return sp<TrackHandle>::make(track);
385}
386
Andy Hungd29af632023-06-23 19:27:19 -0700387TrackHandle::TrackHandle(const sp<IAfTrack>& track)
Eric Laurent81784c32012-11-19 14:55:58 -0800388 : BnAudioTrack(),
389 mTrack(track)
390{
Andy Hunga5a7fc92023-06-23 19:27:19 -0700391 // TODO(b/288339104) binder thread priority change not needed.
Andy Hung225aef62022-12-06 16:33:20 -0800392 setMinSchedulerPolicy(SCHED_NORMAL, ANDROID_PRIORITY_AUDIO);
Eric Laurent81784c32012-11-19 14:55:58 -0800393}
394
Andy Hunga5a7fc92023-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 Hunga5a7fc92023-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 Hunga5a7fc92023-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 Hunga5a7fc92023-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 Hunga5a7fc92023-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 Hunga5a7fc92023-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 Hunga5a7fc92023-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 Hunga5a7fc92023-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 Hunga5a7fc92023-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 Hunga5a7fc92023-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 Hunga5a7fc92023-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 Hunga5a7fc92023-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 Hunga5a7fc92023-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 Hunga5a7fc92023-06-23 19:27:19 -0700497Status TrackHandle::getDualMonoMode(
Mikhail Naganovf53e1822022-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 Hunga5a7fc92023-06-23 19:27:19 -0700510Status TrackHandle::setDualMonoMode(
Mikhail Naganovf53e1822022-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 Hunga5a7fc92023-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 Hunga5a7fc92023-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 Hunga5a7fc92023-06-23 19:27:19 -0700534Status TrackHandle::getPlaybackRateParameters(
Mikhail Naganovf53e1822022-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 Hunga5a7fc92023-06-23 19:27:19 -0700547Status TrackHandle::setPlaybackRateParameters(
Mikhail Naganovf53e1822022-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 Hung8d31fd22023-06-26 19:20:57 -0700561sp<OpPlayAudioMonitor> OpPlayAudioMonitor::createIfNeeded(
Andy Hung87c693c2023-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 Popa9d7c6f82023-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 Popa9d7c6f82023-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 Hung87c693c2023-07-06 20:56:16 -0700592OpPlayAudioMonitor::OpPlayAudioMonitor(IAfThreadBase* thread,
Vlad Popa9d7c6f82023-07-10 20:27:41 -0700593 const AttributionSourceState& attributionSource,
594 audio_usage_t usage, int id, uid_t uid)
Andy Hung87c693c2023-07-06 20:56:16 -0700595 : mThread(wp<IAfThreadBase>::fromExisting(thread)),
Vlad Popa9d7c6f82023-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 Hung8d31fd22023-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 Hung8d31fd22023-06-26 19:20:57 -0700612void OpPlayAudioMonitor::onFirstRef()
Mikhail Naganovf7e3a3a2019-04-22 16:43:26 -0700613{
Mikhail Naganovf7e3a3a2019-04-22 16:43:26 -0700614 checkPlayAudioForUsage();
Svet Ganov33761132021-05-13 22:51:08 +0000615 if (mAttributionSource.packageName.has_value()) {
Mikhail Naganovf7e3a3a2019-04-22 16:43:26 -0700616 mOpCallback = new PlayAudioOpCallback(this);
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700617 mAppOpsManager.startWatchingMode(AppOpsManager::OP_PLAY_AUDIO,
Vlad Popa9d7c6f82023-07-10 20:27:41 -0700618 mPackageName, mOpCallback);
Mikhail Naganovf7e3a3a2019-04-22 16:43:26 -0700619 }
620}
621
Andy Hung8d31fd22023-06-26 19:20:57 -0700622bool OpPlayAudioMonitor::hasOpPlayAudio() const {
Jean-Michel Trivi74e01fa2019-02-25 12:18:09 -0800623 return mHasOpPlayAudio.load();
624}
625
Jean-Michel Trividdf87ef2019-08-20 15:42:04 -0700626// Note this method is never called (and never to be) for audio server / patch record track
Jean-Michel Trivi74e01fa2019-02-25 12:18:09 -0800627// - not called from constructor due to check on UID,
628// - not called from PlayAudioOpCallback because the callback is not installed in this case
Andy Hung8d31fd22023-06-26 19:20:57 -0700629void OpPlayAudioMonitor::checkPlayAudioForUsage()
Jean-Michel Trivi74e01fa2019-02-25 12:18:09 -0800630{
Vlad Popa9d7c6f82023-07-10 20:27:41 -0700631 const bool hasAppOps = mAttributionSource.packageName.has_value()
632 && mAppOpsManager.checkAudioOpNoThrow(
633 AppOpsManager::OP_PLAY_AUDIO, mUsage, mUid, mPackageName) ==
634 AppOpsManager::MODE_ALLOWED;
635
636 bool shouldChange = !hasAppOps; // check if we need to update.
637 if (mHasOpPlayAudio.compare_exchange_strong(shouldChange, hasAppOps)) {
638 ALOGD("OpPlayAudio: track:%d usage:%d %smuted", mId, mUsage, hasAppOps ? "not " : "");
639 auto thread = mThread.promote();
Andy Hungee58e4a2023-07-07 13:47:37 -0700640 if (thread != nullptr && thread->type() == IAfThreadBase::OFFLOAD) {
Vlad Popa9d7c6f82023-07-10 20:27:41 -0700641 // Wake up Thread if offloaded, otherwise it may be several seconds for update.
Andy Hung87c693c2023-07-06 20:56:16 -0700642 Mutex::Autolock _l(thread->mutex());
Vlad Popa9d7c6f82023-07-10 20:27:41 -0700643 thread->broadcast_l();
644 }
Jean-Michel Trivi74e01fa2019-02-25 12:18:09 -0800645 }
646}
647
Andy Hung8d31fd22023-06-26 19:20:57 -0700648OpPlayAudioMonitor::PlayAudioOpCallback::PlayAudioOpCallback(
Jean-Michel Trivi74e01fa2019-02-25 12:18:09 -0800649 const wp<OpPlayAudioMonitor>& monitor) : mMonitor(monitor)
650{ }
651
Andy Hung8d31fd22023-06-26 19:20:57 -0700652void OpPlayAudioMonitor::PlayAudioOpCallback::opChanged(int32_t op,
Jean-Michel Trivi74e01fa2019-02-25 12:18:09 -0800653 const String16& packageName) {
654 // we only have uid, so we need to check all package names anyway
655 UNUSED(packageName);
656 if (op != AppOpsManager::OP_PLAY_AUDIO) {
657 return;
658 }
659 sp<OpPlayAudioMonitor> monitor = mMonitor.promote();
660 if (monitor != NULL) {
661 monitor->checkPlayAudioForUsage();
662 }
663}
664
Eric Laurent9066ad32019-05-20 14:40:10 -0700665// static
Andy Hung8d31fd22023-06-26 19:20:57 -0700666void OpPlayAudioMonitor::getPackagesForUid(
Eric Laurent9066ad32019-05-20 14:40:10 -0700667 uid_t uid, Vector<String16>& packages)
668{
669 PermissionController permissionController;
670 permissionController.getPackagesForUid(uid, packages);
671}
672
Jean-Michel Trivi74e01fa2019-02-25 12:18:09 -0800673// ----------------------------------------------------------------------------
Andy Hung9d84af52018-09-12 18:03:44 -0700674#undef LOG_TAG
675#define LOG_TAG "AF::Track"
Eric Laurent81784c32012-11-19 14:55:58 -0800676
Andy Hung8d31fd22023-06-26 19:20:57 -0700677/* static */
Andy Hung87c693c2023-07-06 20:56:16 -0700678sp<IAfTrack> IAfTrack::create(
679 IAfPlaybackThread* thread,
Andy Hung8d31fd22023-06-26 19:20:57 -0700680 const sp<Client>& client,
681 audio_stream_type_t streamType,
682 const audio_attributes_t& attr,
683 uint32_t sampleRate,
684 audio_format_t format,
685 audio_channel_mask_t channelMask,
686 size_t frameCount,
687 void *buffer,
688 size_t bufferSize,
689 const sp<IMemory>& sharedBuffer,
690 audio_session_t sessionId,
691 pid_t creatorPid,
692 const AttributionSourceState& attributionSource,
693 audio_output_flags_t flags,
694 track_type type,
695 audio_port_handle_t portId,
696 /** default behaviour is to start when there are as many frames
697 * ready as possible (aka. Buffer is full). */
698 size_t frameCountToBeReady,
699 float speed,
700 bool isSpatialized,
701 bool isBitPerfect) {
Andy Hung87c693c2023-07-06 20:56:16 -0700702 return sp<Track>::make(thread,
Andy Hung8d31fd22023-06-26 19:20:57 -0700703 client,
704 streamType,
705 attr,
706 sampleRate,
707 format,
708 channelMask,
709 frameCount,
710 buffer,
711 bufferSize,
712 sharedBuffer,
713 sessionId,
714 creatorPid,
715 attributionSource,
716 flags,
717 type,
718 portId,
719 frameCountToBeReady,
720 speed,
721 isSpatialized,
722 isBitPerfect);
723}
724
Eric Laurent81784c32012-11-19 14:55:58 -0800725// Track constructor must be called with AudioFlinger::mLock and ThreadBase::mLock held
Andy Hung8d31fd22023-06-26 19:20:57 -0700726Track::Track(
Andy Hung87c693c2023-07-06 20:56:16 -0700727 IAfPlaybackThread* thread,
Eric Laurent81784c32012-11-19 14:55:58 -0800728 const sp<Client>& client,
729 audio_stream_type_t streamType,
Kevin Rocard1f564ac2018-03-29 13:53:10 -0700730 const audio_attributes_t& attr,
Eric Laurent81784c32012-11-19 14:55:58 -0800731 uint32_t sampleRate,
732 audio_format_t format,
733 audio_channel_mask_t channelMask,
734 size_t frameCount,
Eric Laurent83b88082014-06-20 18:31:16 -0700735 void *buffer,
Andy Hung8fe68032017-06-05 16:17:51 -0700736 size_t bufferSize,
Eric Laurent81784c32012-11-19 14:55:58 -0800737 const sp<IMemory>& sharedBuffer,
Glenn Kastend848eb42016-03-08 13:42:11 -0800738 audio_session_t sessionId,
Eric Laurent09f1ed22019-04-24 17:45:17 -0700739 pid_t creatorPid,
Svet Ganov33761132021-05-13 22:51:08 +0000740 const AttributionSourceState& attributionSource,
Eric Laurent05067782016-06-01 18:27:28 -0700741 audio_output_flags_t flags,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800742 track_type type,
Kevin Rocard01c7d9e2019-09-18 11:24:52 +0100743 audio_port_handle_t portId,
jiabinf042b9b2021-05-07 23:46:28 +0000744 size_t frameCountToBeReady,
Eric Laurentb0a7bc92022-04-05 15:06:08 +0200745 float speed,
jiabinc658e452022-10-21 20:52:21 +0000746 bool isSpatialized,
747 bool isBitPerfect)
Kevin Rocard1f564ac2018-03-29 13:53:10 -0700748 : TrackBase(thread, client, attr, sampleRate, format, channelMask, frameCount,
Ytai Ben-Tsvi7dd39722019-09-05 15:14:30 -0700749 // TODO: Using unsecurePointer() has some associated security pitfalls
750 // (see declaration for details).
751 // Either document why it is safe in this case or address the
752 // issue (e.g. by copying).
753 (sharedBuffer != 0) ? sharedBuffer->unsecurePointer() : buffer,
Andy Hung8fe68032017-06-05 16:17:51 -0700754 (sharedBuffer != 0) ? sharedBuffer->size() : bufferSize,
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700755 sessionId, creatorPid,
Svet Ganov33761132021-05-13 22:51:08 +0000756 VALUE_OR_FATAL(aidl2legacy_int32_t_uid_t(attributionSource.uid)), true /*isOut*/,
Eric Laurent83b88082014-06-20 18:31:16 -0700757 (type == TYPE_PATCH) ? ( buffer == NULL ? ALLOC_LOCAL : ALLOC_NONE) : ALLOC_CBLK,
Andy Hungb68f5eb2019-12-03 16:49:17 -0800758 type,
759 portId,
760 std::string(AMEDIAMETRICS_KEY_PREFIX_AUDIO_TRACK) + std::to_string(portId)),
Andy Hung8d31fd22023-06-26 19:20:57 -0700761 mFillingStatus(FS_INVALID),
Eric Laurent81784c32012-11-19 14:55:58 -0800762 // mRetryCount initialized later when needed
763 mSharedBuffer(sharedBuffer),
764 mStreamType(streamType),
rago94a1ee82017-07-21 15:11:02 -0700765 mMainBuffer(thread->sinkBuffer()),
Eric Laurent81784c32012-11-19 14:55:58 -0800766 mAuxBuffer(NULL),
767 mAuxEffectId(0), mHasVolumeController(false),
Andy Hunge10393e2015-06-12 13:59:33 -0700768 mFrameMap(16 /* sink-frame-to-track-frame map memory */),
Ivan Lozano8cf3a072017-08-09 09:01:33 -0700769 mVolumeHandler(new media::VolumeHandler(sampleRate)),
Vlad Popa9d7c6f82023-07-10 20:27:41 -0700770 mOpPlayAudioMonitor(OpPlayAudioMonitor::createIfNeeded(thread, attributionSource, attr, id(),
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700771 streamType)),
Andy Hunge10393e2015-06-12 13:59:33 -0700772 // mSinkTimestamp
Eric Laurent81784c32012-11-19 14:55:58 -0800773 mFastIndex(-1),
Glenn Kasten5736c352012-12-04 12:12:34 -0800774 mCachedVolume(1.0),
Kevin Rocard12381092018-04-11 09:19:59 -0700775 /* The track might not play immediately after being active, similarly as if its volume was 0.
776 * When the track starts playing, its volume will be computed. */
777 mFinalVolume(0.f),
Haynes Mathew George7844f672014-01-15 12:32:55 -0800778 mResumeToStopping(false),
Eric Laurent05067782016-06-01 18:27:28 -0700779 mFlushHwPending(false),
jiabinf042b9b2021-05-07 23:46:28 +0000780 mFlags(flags),
Eric Laurentb0a7bc92022-04-05 15:06:08 +0200781 mSpeed(speed),
jiabinc658e452022-10-21 20:52:21 +0000782 mIsSpatialized(isSpatialized),
783 mIsBitPerfect(isBitPerfect)
Eric Laurent81784c32012-11-19 14:55:58 -0800784{
Eric Laurent83b88082014-06-20 18:31:16 -0700785 // client == 0 implies sharedBuffer == 0
786 ALOG_ASSERT(!(client == 0 && sharedBuffer != 0));
787
Andy Hung9d84af52018-09-12 18:03:44 -0700788 ALOGV_IF(sharedBuffer != 0, "%s(%d): sharedBuffer: %p, size: %zu",
Ytai Ben-Tsvi7dd39722019-09-05 15:14:30 -0700789 __func__, mId, sharedBuffer->unsecurePointer(), sharedBuffer->size());
Eric Laurent83b88082014-06-20 18:31:16 -0700790
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700791 if (mCblk == NULL) {
792 return;
Eric Laurent81784c32012-11-19 14:55:58 -0800793 }
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700794
Svet Ganov33761132021-05-13 22:51:08 +0000795 uid_t uid = VALUE_OR_FATAL(aidl2legacy_int32_t_uid_t(attributionSource.uid));
Andy Hung689e82c2019-08-21 17:53:17 -0700796 if (!thread->isTrackAllowed_l(channelMask, format, sessionId, uid)) {
797 ALOGE("%s(%d): no more tracks available", __func__, mId);
798 releaseCblk(); // this makes the track invalid.
799 return;
800 }
801
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700802 if (sharedBuffer == 0) {
803 mAudioTrackServerProxy = new AudioTrackServerProxy(mCblk, mBuffer, frameCount,
Eric Laurent83b88082014-06-20 18:31:16 -0700804 mFrameSize, !isExternalTrack(), sampleRate);
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700805 } else {
806 mAudioTrackServerProxy = new StaticAudioTrackServerProxy(mCblk, mBuffer, frameCount,
Kevin Rocard36862032019-10-10 10:52:19 +0100807 mFrameSize, sampleRate);
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700808 }
809 mServerProxy = mAudioTrackServerProxy;
Andy Hung3c7f47a2021-03-16 17:30:09 -0700810 mServerProxy->setStartThresholdInFrames(frameCountToBeReady); // update the Cblk value
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700811
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700812 // only allocate a fast track index if we were able to allocate a normal track name
Eric Laurent05067782016-06-01 18:27:28 -0700813 if (flags & AUDIO_OUTPUT_FLAG_FAST) {
Andy Hunga5427822015-09-11 16:15:35 -0700814 // FIXME: Not calling framesReadyIsCalledByMultipleThreads() exposes a potential
815 // race with setSyncEvent(). However, if we call it, we cannot properly start
816 // static fast tracks (SoundPool) immediately after stopping.
817 //mAudioTrackServerProxy->framesReadyIsCalledByMultipleThreads();
Andy Hung87c693c2023-07-06 20:56:16 -0700818 ALOG_ASSERT(thread->fastTrackAvailMask_l() != 0);
819 const int i = __builtin_ctz(thread->fastTrackAvailMask_l());
Glenn Kastendc2c50b2016-04-21 08:13:14 -0700820 ALOG_ASSERT(0 < i && i < (int)FastMixerState::sMaxFastTracks);
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700821 // FIXME This is too eager. We allocate a fast track index before the
822 // fast track becomes active. Since fast tracks are a scarce resource,
823 // this means we are potentially denying other more important fast tracks from
824 // being created. It would be better to allocate the index dynamically.
825 mFastIndex = i;
Andy Hung87c693c2023-07-06 20:56:16 -0700826 thread->fastTrackAvailMask_l() &= ~(1 << i);
Glenn Kasten3ef14ef2014-03-13 15:08:51 -0700827 }
Andy Hung8946a282018-04-19 20:04:56 -0700828
Dean Wheatley7b036912020-06-18 16:22:11 +1000829 mServerLatencySupported = checkServerLatencySupported(format, flags);
Andy Hung8946a282018-04-19 20:04:56 -0700830#ifdef TEE_SINK
831 mTee.setId(std::string("_") + std::to_string(mThreadIoHandle)
Kevin Rocard51f0e982019-02-01 19:19:11 -0800832 + "_" + std::to_string(mId) + "_T");
Andy Hung8946a282018-04-19 20:04:56 -0700833#endif
jiabin57303cc2018-12-18 15:45:57 -0800834
jiabineb3bda02020-06-30 14:07:03 -0700835 if (thread->supportsHapticPlayback()) {
836 // If the track is attached to haptic playback thread, it is potentially to have
837 // HapticGenerator effect, which will generate haptic data, on the track. In that case,
838 // external vibration is always created for all tracks attached to haptic playback thread.
jiabin57303cc2018-12-18 15:45:57 -0800839 mAudioVibrationController = new AudioVibrationController(this);
Svet Ganov33761132021-05-13 22:51:08 +0000840 std::string packageName = attributionSource.packageName.has_value() ?
841 attributionSource.packageName.value() : "";
jiabin57303cc2018-12-18 15:45:57 -0800842 mExternalVibration = new os::ExternalVibration(
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700843 mUid, packageName, mAttr, mAudioVibrationController);
jiabin57303cc2018-12-18 15:45:57 -0800844 }
Andy Hungb68f5eb2019-12-03 16:49:17 -0800845
846 // Once this item is logged by the server, the client can add properties.
Andy Hunga629bd12020-06-05 16:03:53 -0700847 const char * const traits = sharedBuffer == 0 ? "" : "static";
Andy Hung5837c7f2021-02-25 10:48:24 -0800848 mTrackMetrics.logConstructor(creatorPid, uid, id(), traits, streamType);
Eric Laurent81784c32012-11-19 14:55:58 -0800849}
850
Andy Hung8d31fd22023-06-26 19:20:57 -0700851Track::~Track()
Eric Laurent81784c32012-11-19 14:55:58 -0800852{
Andy Hung9d84af52018-09-12 18:03:44 -0700853 ALOGV("%s(%d)", __func__, mId);
Glenn Kasten0c72b242013-09-11 09:14:16 -0700854
855 // The destructor would clear mSharedBuffer,
856 // but it will not push the decremented reference count,
857 // leaving the client's IMemory dangling indefinitely.
858 // This prevents that leak.
859 if (mSharedBuffer != 0) {
860 mSharedBuffer.clear();
Glenn Kasten0c72b242013-09-11 09:14:16 -0700861 }
Eric Laurent81784c32012-11-19 14:55:58 -0800862}
863
Andy Hung8d31fd22023-06-26 19:20:57 -0700864status_t Track::initCheck() const
Glenn Kasten03003332013-08-06 15:40:54 -0700865{
866 status_t status = TrackBase::initCheck();
Andy Hungc0691382018-09-12 18:01:57 -0700867 if (status == NO_ERROR && mCblk == nullptr) {
Glenn Kasten03003332013-08-06 15:40:54 -0700868 status = NO_MEMORY;
869 }
870 return status;
871}
872
Andy Hung8d31fd22023-06-26 19:20:57 -0700873void Track::destroy()
Eric Laurent81784c32012-11-19 14:55:58 -0800874{
875 // NOTE: destroyTrack_l() can remove a strong reference to this Track
876 // by removing it from mTracks vector, so there is a risk that this Tracks's
877 // destructor is called. As the destructor needs to lock mLock,
878 // we must acquire a strong reference on this Track before locking mLock
879 // here so that the destructor is called only when exiting this function.
880 // On the other hand, as long as Track::destroy() is only called by
881 // TrackHandle destructor, the TrackHandle still holds a strong ref on
882 // this Track with its member mTrack.
883 sp<Track> keep(this);
884 { // scope for mLock
Eric Laurentaaa44472014-09-12 17:41:50 -0700885 bool wasActive = false;
Andy Hung87c693c2023-07-06 20:56:16 -0700886 const sp<IAfThreadBase> thread = mThread.promote();
Eric Laurent81784c32012-11-19 14:55:58 -0800887 if (thread != 0) {
Andy Hung87c693c2023-07-06 20:56:16 -0700888 Mutex::Autolock _l(thread->mutex());
889 auto* const playbackThread = thread->asIAfPlaybackThread().get();
Eric Laurentaaa44472014-09-12 17:41:50 -0700890 wasActive = playbackThread->destroyTrack_l(this);
jiabin7434e812023-06-27 18:22:35 +0000891 forEachTeePatchTrack_l([](const auto& patchTrack) { patchTrack->destroy(); });
Eric Laurentaaa44472014-09-12 17:41:50 -0700892 }
893 if (isExternalTrack() && !wasActive) {
Eric Laurentd7fe0862018-07-14 16:48:01 -0700894 AudioSystem::releaseOutput(mPortId);
Eric Laurent81784c32012-11-19 14:55:58 -0800895 }
896 }
897}
898
Andy Hung8d31fd22023-06-26 19:20:57 -0700899void Track::appendDumpHeader(String8& result) const
Eric Laurent81784c32012-11-19 14:55:58 -0800900{
Eric Laurent973db022018-11-20 14:54:31 -0800901 result.appendFormat("Type Id Active Client Session Port Id S Flags "
Kevin Rocard5f2136e2018-05-11 22:03:00 -0700902 " Format Chn mask SRate "
903 "ST Usg CT "
904 " G db L dB R dB VS dB "
jiabin5eaf0962022-12-20 20:11:38 +0000905 " Server FrmCnt FrmRdy F Underruns Flushed BitPerfect"
Kevin Rocard5f2136e2018-05-11 22:03:00 -0700906 "%s\n",
907 isServerLatencySupported() ? " Latency" : "");
Eric Laurent81784c32012-11-19 14:55:58 -0800908}
909
Andy Hung8d31fd22023-06-26 19:20:57 -0700910void Track::appendDump(String8& result, bool active) const
Eric Laurent81784c32012-11-19 14:55:58 -0800911{
Andy Hung2c6c3bb2017-06-16 14:01:45 -0700912 char trackType;
913 switch (mType) {
914 case TYPE_DEFAULT:
915 case TYPE_OUTPUT:
Andy Hungf6ab58d2018-05-25 12:50:39 -0700916 if (isStatic()) {
Andy Hung2c6c3bb2017-06-16 14:01:45 -0700917 trackType = 'S'; // static
918 } else {
919 trackType = ' '; // normal
Eric Laurentbfb1b832013-01-07 09:53:42 -0800920 }
Andy Hung2c6c3bb2017-06-16 14:01:45 -0700921 break;
922 case TYPE_PATCH:
923 trackType = 'P';
924 break;
925 default:
926 trackType = '?';
Eric Laurent81784c32012-11-19 14:55:58 -0800927 }
Andy Hung2c6c3bb2017-06-16 14:01:45 -0700928
929 if (isFastTrack()) {
Andy Hungc0691382018-09-12 18:01:57 -0700930 result.appendFormat("F%d %c %6d", mFastIndex, trackType, mId);
Andy Hung2c6c3bb2017-06-16 14:01:45 -0700931 } else {
Andy Hungc0691382018-09-12 18:01:57 -0700932 result.appendFormat(" %c %6d", trackType, mId);
Andy Hung2c6c3bb2017-06-16 14:01:45 -0700933 }
934
Eric Laurent81784c32012-11-19 14:55:58 -0800935 char nowInUnderrun;
936 switch (mObservedUnderruns.mBitFields.mMostRecent) {
937 case UNDERRUN_FULL:
938 nowInUnderrun = ' ';
939 break;
940 case UNDERRUN_PARTIAL:
941 nowInUnderrun = '<';
942 break;
943 case UNDERRUN_EMPTY:
944 nowInUnderrun = '*';
945 break;
946 default:
947 nowInUnderrun = '?';
948 break;
949 }
Andy Hungda540db2017-04-20 14:06:17 -0700950
Andy Hung2c6c3bb2017-06-16 14:01:45 -0700951 char fillingStatus;
Andy Hung8d31fd22023-06-26 19:20:57 -0700952 switch (mFillingStatus) {
Andy Hung2c6c3bb2017-06-16 14:01:45 -0700953 case FS_INVALID:
954 fillingStatus = 'I';
955 break;
956 case FS_FILLING:
957 fillingStatus = 'f';
958 break;
959 case FS_FILLED:
960 fillingStatus = 'F';
961 break;
962 case FS_ACTIVE:
963 fillingStatus = 'A';
964 break;
965 default:
966 fillingStatus = '?';
967 break;
968 }
969
970 // clip framesReadySafe to max representation in dump
971 const size_t framesReadySafe =
972 std::min(mAudioTrackServerProxy->framesReadySafe(), (size_t)99999999);
973
974 // obtain volumes
975 const gain_minifloat_packed_t vlr = mAudioTrackServerProxy->getVolumeLR();
976 const std::pair<float /* volume */, bool /* active */> vsVolume =
977 mVolumeHandler->getLastVolume();
978
979 // Our effective frame count is obtained by ServerProxy::getBufferSizeInFrames()
980 // as it may be reduced by the application.
981 const size_t bufferSizeInFrames = (size_t)mAudioTrackServerProxy->getBufferSizeInFrames();
982 // Check whether the buffer size has been modified by the app.
983 const char modifiedBufferChar = bufferSizeInFrames < mFrameCount
984 ? 'r' /* buffer reduced */: bufferSizeInFrames > mFrameCount
985 ? 'e' /* error */ : ' ' /* identical */;
986
Eric Laurent973db022018-11-20 14:54:31 -0800987 result.appendFormat("%7s %6u %7u %7u %2s 0x%03X "
Kevin Rocard5f2136e2018-05-11 22:03:00 -0700988 "%08X %08X %6u "
989 "%2u %3x %2x "
990 "%5.2g %5.2g %5.2g %5.2g%c "
jiabin5eaf0962022-12-20 20:11:38 +0000991 "%08X %6zu%c %6zu %c %9u%c %7u %10s",
Marco Nelissenb2208842014-02-07 14:00:50 -0800992 active ? "yes" : "no",
Andy Hung4ef19fa2018-05-15 19:35:29 -0700993 (mClient == 0) ? getpid() : mClient->pid(),
Andy Hung2c6c3bb2017-06-16 14:01:45 -0700994 mSessionId,
Eric Laurent973db022018-11-20 14:54:31 -0800995 mPortId,
Andy Hunge2e830f2019-12-03 12:54:46 -0800996 getTrackStateAsCodedString(),
Andy Hung2c6c3bb2017-06-16 14:01:45 -0700997 mCblk->mFlags,
998
Eric Laurent81784c32012-11-19 14:55:58 -0800999 mFormat,
1000 mChannelMask,
Andy Hungcef2daa2018-06-01 15:31:49 -07001001 sampleRate(),
Andy Hung2c6c3bb2017-06-16 14:01:45 -07001002
1003 mStreamType,
Kevin Rocard5f2136e2018-05-11 22:03:00 -07001004 mAttr.usage,
1005 mAttr.content_type,
1006
1007 20.0 * log10(mFinalVolume),
Glenn Kastenc56f3422014-03-21 17:53:17 -07001008 20.0 * log10(float_from_gain(gain_minifloat_unpack_left(vlr))),
1009 20.0 * log10(float_from_gain(gain_minifloat_unpack_right(vlr))),
Andy Hungda540db2017-04-20 14:06:17 -07001010 20.0 * log10(vsVolume.first), // VolumeShaper(s) total volume
1011 vsVolume.second ? 'A' : ' ', // if any VolumeShapers active
Andy Hung2c6c3bb2017-06-16 14:01:45 -07001012
Glenn Kastenf20e1d82013-07-12 09:45:18 -07001013 mCblk->mServer,
Andy Hung2c6c3bb2017-06-16 14:01:45 -07001014 bufferSizeInFrames,
1015 modifiedBufferChar,
1016 framesReadySafe,
1017 fillingStatus,
Glenn Kasten82aaf942013-07-17 16:05:07 -07001018 mAudioTrackServerProxy->getUnderrunFrames(),
Andy Hung2148bf02016-11-28 19:01:02 -08001019 nowInUnderrun,
jiabin5eaf0962022-12-20 20:11:38 +00001020 (unsigned)mAudioTrackServerProxy->framesFlushed() % 10000000,
1021 isBitPerfect() ? "true" : "false"
Andy Hung2c6c3bb2017-06-16 14:01:45 -07001022 );
Andy Hungcef2daa2018-06-01 15:31:49 -07001023
1024 if (isServerLatencySupported()) {
1025 double latencyMs;
1026 bool fromTrack;
1027 if (getTrackLatencyMs(&latencyMs, &fromTrack) == OK) {
1028 // Show latency in msec, followed by 't' if from track timestamp (the most accurate)
1029 // or 'k' if estimated from kernel because track frames haven't been presented yet.
1030 result.appendFormat(" %7.2lf %c", latencyMs, fromTrack ? 't' : 'k');
Andy Hungf6ab58d2018-05-25 12:50:39 -07001031 } else {
Andy Hungcef2daa2018-06-01 15:31:49 -07001032 result.appendFormat("%10s", mCblk->mServer != 0 ? "unavail" : "new");
Andy Hungf6ab58d2018-05-25 12:50:39 -07001033 }
1034 }
1035 result.append("\n");
Eric Laurent81784c32012-11-19 14:55:58 -08001036}
1037
Andy Hung8d31fd22023-06-26 19:20:57 -07001038uint32_t Track::sampleRate() const {
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001039 return mAudioTrackServerProxy->getSampleRate();
1040}
1041
Eric Laurent81784c32012-11-19 14:55:58 -08001042// AudioBufferProvider interface
Andy Hung8d31fd22023-06-26 19:20:57 -07001043status_t Track::getNextBuffer(AudioBufferProvider::Buffer* buffer)
Eric Laurent81784c32012-11-19 14:55:58 -08001044{
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001045 ServerProxy::Buffer buf;
1046 size_t desiredFrames = buffer->frameCount;
1047 buf.mFrameCount = desiredFrames;
1048 status_t status = mServerProxy->obtainBuffer(&buf);
1049 buffer->frameCount = buf.mFrameCount;
1050 buffer->raw = buf.mRaw;
Andy Hungfc629172020-06-22 10:06:23 -07001051 if (buf.mFrameCount == 0 && !isStopping() && !isStopped() && !isPaused() && !isOffloaded()) {
Andy Hung9d84af52018-09-12 18:03:44 -07001052 ALOGV("%s(%d): underrun, framesReady(%zu) < framesDesired(%zd), state: %d",
Andy Hung959b5b82021-09-24 10:46:20 -07001053 __func__, mId, buf.mFrameCount, desiredFrames, (int)mState);
Glenn Kasten82aaf942013-07-17 16:05:07 -07001054 mAudioTrackServerProxy->tallyUnderrunFrames(desiredFrames);
Phil Burk2812d9e2016-01-04 10:34:30 -08001055 } else {
1056 mAudioTrackServerProxy->tallyUnderrunFrames(0);
Eric Laurent81784c32012-11-19 14:55:58 -08001057 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001058 return status;
Eric Laurent81784c32012-11-19 14:55:58 -08001059}
1060
Andy Hung8d31fd22023-06-26 19:20:57 -07001061void Track::releaseBuffer(AudioBufferProvider::Buffer* buffer)
Kevin Rocard153f92d2018-12-18 18:33:28 -08001062{
1063 interceptBuffer(*buffer);
1064 TrackBase::releaseBuffer(buffer);
1065}
1066
1067// TODO: compensate for time shift between HW modules.
Andy Hung8d31fd22023-06-26 19:20:57 -07001068void Track::interceptBuffer(
Kevin Rocarda134b002019-02-07 18:05:31 -08001069 const AudioBufferProvider::Buffer& sourceBuffer) {
Kevin Rocard6057fa22019-02-08 14:08:07 -08001070 auto start = std::chrono::steady_clock::now();
Kevin Rocarda134b002019-02-07 18:05:31 -08001071 const size_t frameCount = sourceBuffer.frameCount;
Kevin Rocardd83b08a2019-02-27 15:05:54 -08001072 if (frameCount == 0) {
1073 return; // No audio to intercept.
1074 // Additionally PatchProxyBufferProvider::obtainBuffer (called by PathTrack::getNextBuffer)
1075 // does not allow 0 frame size request contrary to getNextBuffer
1076 }
1077 for (auto& teePatch : mTeePatches) {
Andy Hung8d31fd22023-06-26 19:20:57 -07001078 IAfPatchRecord* patchRecord = teePatch.patchRecord.get();
Mikhail Naganov8296c252019-09-25 14:59:54 -07001079 const size_t framesWritten = patchRecord->writeFrames(
1080 sourceBuffer.i8, frameCount, mFrameSize);
1081 const size_t framesLeft = frameCount - framesWritten;
Kevin Rocarda134b002019-02-07 18:05:31 -08001082 ALOGW_IF(framesLeft != 0, "%s(%d) PatchRecord %d can not provide big enough "
Andy Hung8d31fd22023-06-26 19:20:57 -07001083 "buffer %zu/%zu, dropping %zu frames", __func__, mId, patchRecord->id(),
Kevin Rocarda134b002019-02-07 18:05:31 -08001084 framesWritten, frameCount, framesLeft);
Kevin Rocard153f92d2018-12-18 18:33:28 -08001085 }
Kevin Rocard6057fa22019-02-08 14:08:07 -08001086 auto spent = ceil<std::chrono::microseconds>(std::chrono::steady_clock::now() - start);
1087 using namespace std::chrono_literals;
1088 // Average is ~20us per track, this should virtually never be logged (Logging takes >200us)
Kevin Rocard01c7d9e2019-09-18 11:24:52 +01001089 ALOGD_IF(spent > 500us, "%s: took %lldus to intercept %zu tracks", __func__,
Kevin Rocard6057fa22019-02-08 14:08:07 -08001090 spent.count(), mTeePatches.size());
Kevin Rocard153f92d2018-12-18 18:33:28 -08001091}
1092
Glenn Kasten6466c9e2013-08-23 10:54:07 -07001093// ExtendedAudioBufferProvider interface
1094
Andy Hung27876c02014-09-09 18:07:55 -07001095// framesReady() may return an approximation of the number of frames if called
1096// from a different thread than the one calling Proxy->obtainBuffer() and
1097// Proxy->releaseBuffer(). Also note there is no mutual exclusion in the
1098// AudioTrackServerProxy so be especially careful calling with FastTracks.
Andy Hung8d31fd22023-06-26 19:20:57 -07001099size_t Track::framesReady() const {
Andy Hung27876c02014-09-09 18:07:55 -07001100 if (mSharedBuffer != 0 && (isStopped() || isStopping())) {
1101 // Static tracks return zero frames immediately upon stopping (for FastTracks).
1102 // The remainder of the buffer is not drained.
1103 return 0;
1104 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001105 return mAudioTrackServerProxy->framesReady();
Eric Laurent81784c32012-11-19 14:55:58 -08001106}
1107
Andy Hung8d31fd22023-06-26 19:20:57 -07001108int64_t Track::framesReleased() const
Glenn Kasten6466c9e2013-08-23 10:54:07 -07001109{
1110 return mAudioTrackServerProxy->framesReleased();
1111}
1112
Andy Hung8d31fd22023-06-26 19:20:57 -07001113void Track::onTimestamp(const ExtendedTimestamp &timestamp)
Andy Hung6ae58432016-02-16 18:32:24 -08001114{
1115 // This call comes from a FastTrack and should be kept lockless.
1116 // The server side frames are already translated to client frames.
Andy Hung818e7a32016-02-16 18:08:07 -08001117 mAudioTrackServerProxy->setTimestamp(timestamp);
Andy Hung6ae58432016-02-16 18:32:24 -08001118
Andy Hung818e7a32016-02-16 18:08:07 -08001119 // We do not set drained here, as FastTrack timestamp may not go to very last frame.
Andy Hungcef2daa2018-06-01 15:31:49 -07001120
1121 // Compute latency.
1122 // TODO: Consider whether the server latency may be passed in by FastMixer
1123 // as a constant for all active FastTracks.
1124 const double latencyMs = timestamp.getOutputServerLatencyMs(sampleRate());
1125 mServerLatencyFromTrack.store(true);
1126 mServerLatencyMs.store(latencyMs);
Andy Hung6ae58432016-02-16 18:32:24 -08001127}
1128
Eric Laurent81784c32012-11-19 14:55:58 -08001129// Don't call for fast tracks; the framesReady() could result in priority inversion
Andy Hung8d31fd22023-06-26 19:20:57 -07001130bool Track::isReady() const {
1131 if (mFillingStatus != FS_FILLING || isStopped() || isPausing()) {
Krishnankutty Kolathappilly8d6c2922014-02-04 16:23:42 -08001132 return true;
1133 }
1134
Eric Laurent16498512014-03-17 17:22:08 -07001135 if (isStopping()) {
1136 if (framesReady() > 0) {
Andy Hung8d31fd22023-06-26 19:20:57 -07001137 mFillingStatus = FS_FILLED;
Eric Laurent16498512014-03-17 17:22:08 -07001138 }
Eric Laurent81784c32012-11-19 14:55:58 -08001139 return true;
1140 }
1141
Kevin Rocard01c7d9e2019-09-18 11:24:52 +01001142 size_t bufferSizeInFrames = mServerProxy->getBufferSizeInFrames();
Andy Hung3c7f47a2021-03-16 17:30:09 -07001143 // Note: mServerProxy->getStartThresholdInFrames() is clamped.
1144 const size_t startThresholdInFrames = mServerProxy->getStartThresholdInFrames();
1145 const size_t framesToBeReady = std::clamp( // clamp again to validate client values.
1146 std::min(startThresholdInFrames, bufferSizeInFrames), size_t(1), mFrameCount);
Kevin Rocard01c7d9e2019-09-18 11:24:52 +01001147
1148 if (framesReady() >= framesToBeReady || (mCblk->mFlags & CBLK_FORCEREADY)) {
1149 ALOGV("%s(%d): consider track ready with %zu/%zu, target was %zu)",
1150 __func__, mId, framesReady(), bufferSizeInFrames, framesToBeReady);
Andy Hung8d31fd22023-06-26 19:20:57 -07001151 mFillingStatus = FS_FILLED;
Glenn Kasten96f60d82013-07-12 10:21:18 -07001152 android_atomic_and(~CBLK_FORCEREADY, &mCblk->mFlags);
Eric Laurent81784c32012-11-19 14:55:58 -08001153 return true;
1154 }
1155 return false;
1156}
1157
Andy Hung8d31fd22023-06-26 19:20:57 -07001158status_t Track::start(AudioSystem::sync_event_t event __unused,
Glenn Kastend848eb42016-03-08 13:42:11 -08001159 audio_session_t triggerSession __unused)
Eric Laurent81784c32012-11-19 14:55:58 -08001160{
1161 status_t status = NO_ERROR;
Andy Hungc0691382018-09-12 18:01:57 -07001162 ALOGV("%s(%d): calling pid %d session %d",
1163 __func__, mId, IPCThreadState::self()->getCallingPid(), mSessionId);
Eric Laurent81784c32012-11-19 14:55:58 -08001164
Andy Hung87c693c2023-07-06 20:56:16 -07001165 const sp<IAfThreadBase> thread = mThread.promote();
Eric Laurent81784c32012-11-19 14:55:58 -08001166 if (thread != 0) {
Eric Laurent813e2a72013-08-31 12:59:48 -07001167 if (isOffloaded()) {
Andy Hung87c693c2023-07-06 20:56:16 -07001168 Mutex::Autolock _laf(thread->audioFlinger()->mLock);
1169 Mutex::Autolock _lth(thread->mutex());
Andy Hung116bc262023-06-20 18:56:17 -07001170 sp<IAfEffectChain> ec = thread->getEffectChain_l(mSessionId);
Andy Hung87c693c2023-07-06 20:56:16 -07001171 if (thread->audioFlinger()->isNonOffloadableGlobalEffectEnabled_l() ||
Eric Laurent5baf2af2013-09-12 17:37:00 -07001172 (ec != 0 && ec->isNonOffloadableEnabled())) {
Eric Laurent813e2a72013-08-31 12:59:48 -07001173 invalidate();
1174 return PERMISSION_DENIED;
1175 }
1176 }
Andy Hung87c693c2023-07-06 20:56:16 -07001177 Mutex::Autolock _lth(thread->mutex());
Eric Laurent81784c32012-11-19 14:55:58 -08001178 track_state state = mState;
1179 // here the track could be either new, or restarted
1180 // in both cases "unstop" the track
Eric Laurentbfb1b832013-01-07 09:53:42 -08001181
Krishnankutty Kolathappilly8d6c2922014-02-04 16:23:42 -08001182 // initial state-stopping. next state-pausing.
1183 // What if resume is called ?
1184
Zhou Song1ed46a22020-08-17 15:36:56 +08001185 if (state == FLUSHED) {
1186 // avoid underrun glitches when starting after flush
1187 reset();
1188 }
1189
kuowei.li576f1362021-05-11 18:02:32 +08001190 // clear mPauseHwPending because of pause (and possibly flush) during underrun.
1191 mPauseHwPending = false;
Krishnankutty Kolathappilly8d6c2922014-02-04 16:23:42 -08001192 if (state == PAUSED || state == PAUSING) {
Eric Laurentbfb1b832013-01-07 09:53:42 -08001193 if (mResumeToStopping) {
1194 // happened we need to resume to STOPPING_1
1195 mState = TrackBase::STOPPING_1;
Andy Hungc0691382018-09-12 18:01:57 -07001196 ALOGV("%s(%d): PAUSED => STOPPING_1 on thread %d",
1197 __func__, mId, (int)mThreadIoHandle);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001198 } else {
1199 mState = TrackBase::RESUMING;
Andy Hungc0691382018-09-12 18:01:57 -07001200 ALOGV("%s(%d): PAUSED => RESUMING on thread %d",
1201 __func__, mId, (int)mThreadIoHandle);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001202 }
Eric Laurent81784c32012-11-19 14:55:58 -08001203 } else {
1204 mState = TrackBase::ACTIVE;
Andy Hungc0691382018-09-12 18:01:57 -07001205 ALOGV("%s(%d): ? => ACTIVE on thread %d",
1206 __func__, mId, (int)mThreadIoHandle);
Eric Laurent81784c32012-11-19 14:55:58 -08001207 }
1208
Andy Hung87c693c2023-07-06 20:56:16 -07001209 auto* const playbackThread = thread->asIAfPlaybackThread().get();
yucliu6cfb5932022-07-20 17:40:39 -07001210
1211 // states to reset position info for pcm tracks
1212 if (audio_is_linear_pcm(mFormat)
Andy Hunge10393e2015-06-12 13:59:33 -07001213 && (state == IDLE || state == STOPPED || state == FLUSHED)) {
1214 mFrameMap.reset();
yucliu6cfb5932022-07-20 17:40:39 -07001215
1216 if (!isFastTrack() && (isDirect() || isOffloaded())) {
1217 // Start point of track -> sink frame map. If the HAL returns a
1218 // frame position smaller than the first written frame in
1219 // updateTrackFrameInfo, the timestamp can be interpolated
1220 // instead of using a larger value.
1221 mFrameMap.push(mAudioTrackServerProxy->framesReleased(),
1222 playbackThread->framesWritten());
1223 }
Andy Hunge10393e2015-06-12 13:59:33 -07001224 }
Haynes Mathew George240934b2015-03-11 18:25:50 -07001225 if (isFastTrack()) {
1226 // refresh fast track underruns on start because that field is never cleared
1227 // by the fast mixer; furthermore, the same track can be recycled, i.e. start
1228 // after stop.
1229 mObservedUnderruns = playbackThread->getFastTrackUnderruns(mFastIndex);
1230 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08001231 status = playbackThread->addTrack_l(this);
jiabina84c3d32022-12-02 18:59:55 +00001232 if (status == INVALID_OPERATION || status == PERMISSION_DENIED || status == DEAD_OBJECT) {
Eric Laurent81784c32012-11-19 14:55:58 -08001233 triggerEvents(AudioSystem::SYNC_EVENT_PRESENTATION_COMPLETE);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001234 // restore previous state if start was rejected by policy manager
jiabina84c3d32022-12-02 18:59:55 +00001235 if (status == PERMISSION_DENIED || status == DEAD_OBJECT) {
Eric Laurentbfb1b832013-01-07 09:53:42 -08001236 mState = state;
1237 }
1238 }
Andy Hung1d3556d2018-03-29 16:30:14 -07001239
Andy Hungb68f5eb2019-12-03 16:49:17 -08001240 // Audio timing metrics are computed a few mix cycles after starting.
1241 {
1242 mLogStartCountdown = LOG_START_COUNTDOWN;
1243 mLogStartTimeNs = systemTime();
1244 mLogStartFrames = mAudioTrackServerProxy->getTimestamp()
Andy Hung62921122020-05-18 10:47:31 -07001245 .mPosition[ExtendedTimestamp::LOCATION_KERNEL];
1246 mLogLatencyMs = 0.;
Andy Hungb68f5eb2019-12-03 16:49:17 -08001247 }
Andy Hunga81a4b42022-05-19 19:24:51 -07001248 mLogForceVolumeUpdate = true; // at least one volume logged for metrics when starting.
Andy Hungb68f5eb2019-12-03 16:49:17 -08001249
Andy Hung1d3556d2018-03-29 16:30:14 -07001250 if (status == NO_ERROR || status == ALREADY_EXISTS) {
1251 // for streaming tracks, remove the buffer read stop limit.
1252 mAudioTrackServerProxy->start();
1253 }
1254
Eric Laurentbfb1b832013-01-07 09:53:42 -08001255 // track was already in the active list, not a problem
1256 if (status == ALREADY_EXISTS) {
1257 status = NO_ERROR;
Glenn Kasten12022ff2013-10-17 11:32:39 -07001258 } else {
1259 // Acknowledge any pending flush(), so that subsequent new data isn't discarded.
1260 // It is usually unsafe to access the server proxy from a binder thread.
1261 // But in this case we know the mixer thread (whether normal mixer or fast mixer)
1262 // isn't looking at this track yet: we still hold the normal mixer thread lock,
1263 // and for fast tracks the track is not yet in the fast mixer thread's active set.
Andy Hunge6fb82a2015-09-09 14:39:02 -07001264 // For static tracks, this is used to acknowledge change in position or loop.
Eric Laurent564d1442015-09-09 12:26:52 -07001265 ServerProxy::Buffer buffer;
1266 buffer.mFrameCount = 1;
1267 (void) mAudioTrackServerProxy->obtainBuffer(&buffer, true /*ackFlush*/);
Eric Laurent81784c32012-11-19 14:55:58 -08001268 }
jiabin7434e812023-06-27 18:22:35 +00001269 if (status == NO_ERROR) {
1270 forEachTeePatchTrack_l([](const auto& patchTrack) { patchTrack->start(); });
1271 }
Eric Laurent81784c32012-11-19 14:55:58 -08001272 } else {
1273 status = BAD_VALUE;
1274 }
Kevin Rocardc43ea142019-01-31 18:17:37 -08001275 if (status == NO_ERROR) {
Jean-Michel Trivi16395ca2022-12-11 22:10:11 +00001276 // send format to AudioManager for playback activity monitoring
Andy Hung87c693c2023-07-06 20:56:16 -07001277 const sp<IAudioManager> audioManager = thread->audioFlinger()->getOrCreateAudioManager();
Jean-Michel Trivi16395ca2022-12-11 22:10:11 +00001278 if (audioManager && mPortId != AUDIO_PORT_HANDLE_NONE) {
1279 std::unique_ptr<os::PersistableBundle> bundle =
1280 std::make_unique<os::PersistableBundle>();
1281 bundle->putBoolean(String16(kExtraPlayerEventSpatializedKey),
1282 isSpatialized());
1283 bundle->putInt(String16(kExtraPlayerEventSampleRateKey), mSampleRate);
1284 bundle->putInt(String16(kExtraPlayerEventChannelMaskKey), mChannelMask);
1285 status_t result = audioManager->portEvent(mPortId,
1286 PLAYER_UPDATE_FORMAT, bundle);
1287 if (result != OK) {
1288 ALOGE("%s: unable to send playback format for port ID %d, status error %d",
1289 __func__, mPortId, result);
1290 }
1291 }
Kevin Rocardc43ea142019-01-31 18:17:37 -08001292 }
Eric Laurent81784c32012-11-19 14:55:58 -08001293 return status;
1294}
1295
Andy Hung8d31fd22023-06-26 19:20:57 -07001296void Track::stop()
Eric Laurent81784c32012-11-19 14:55:58 -08001297{
Andy Hungc0691382018-09-12 18:01:57 -07001298 ALOGV("%s(%d): calling pid %d", __func__, mId, IPCThreadState::self()->getCallingPid());
Andy Hung87c693c2023-07-06 20:56:16 -07001299 const sp<IAfThreadBase> thread = mThread.promote();
Eric Laurent81784c32012-11-19 14:55:58 -08001300 if (thread != 0) {
Andy Hung87c693c2023-07-06 20:56:16 -07001301 Mutex::Autolock _l(thread->mutex());
Eric Laurent81784c32012-11-19 14:55:58 -08001302 track_state state = mState;
1303 if (state == RESUMING || state == ACTIVE || state == PAUSING || state == PAUSED) {
1304 // If the track is not active (PAUSED and buffers full), flush buffers
Andy Hung87c693c2023-07-06 20:56:16 -07001305 auto* const playbackThread = thread->asIAfPlaybackThread().get();
1306 if (!playbackThread->isTrackActive(this)) {
Eric Laurent81784c32012-11-19 14:55:58 -08001307 reset();
1308 mState = STOPPED;
Eric Laurentab5cdba2014-06-09 17:22:27 -07001309 } else if (!isFastTrack() && !isOffloaded() && !isDirect()) {
Eric Laurent81784c32012-11-19 14:55:58 -08001310 mState = STOPPED;
1311 } else {
Eric Laurentbfb1b832013-01-07 09:53:42 -08001312 // For fast tracks prepareTracks_l() will set state to STOPPING_2
1313 // presentation is complete
1314 // For an offloaded track this starts a drain and state will
1315 // move to STOPPING_2 when drain completes and then STOPPED
Eric Laurent81784c32012-11-19 14:55:58 -08001316 mState = STOPPING_1;
Eric Laurente93cc032016-05-05 10:15:10 -07001317 if (isOffloaded()) {
Andy Hung87c693c2023-07-06 20:56:16 -07001318 mRetryCount = IAfPlaybackThread::kMaxTrackStopRetriesOffload;
Eric Laurente93cc032016-05-05 10:15:10 -07001319 }
Eric Laurent81784c32012-11-19 14:55:58 -08001320 }
Eric Laurentb369caf2015-03-30 20:51:47 -07001321 playbackThread->broadcast_l();
Andy Hungc0691382018-09-12 18:01:57 -07001322 ALOGV("%s(%d): not stopping/stopped => stopping/stopped on thread %d",
1323 __func__, mId, (int)mThreadIoHandle);
Eric Laurent81784c32012-11-19 14:55:58 -08001324 }
jiabin7434e812023-06-27 18:22:35 +00001325 forEachTeePatchTrack_l([](const auto& patchTrack) { patchTrack->stop(); });
Eric Laurent81784c32012-11-19 14:55:58 -08001326 }
1327}
1328
Andy Hung8d31fd22023-06-26 19:20:57 -07001329void Track::pause()
Eric Laurent81784c32012-11-19 14:55:58 -08001330{
Andy Hungc0691382018-09-12 18:01:57 -07001331 ALOGV("%s(%d): calling pid %d", __func__, mId, IPCThreadState::self()->getCallingPid());
Andy Hung87c693c2023-07-06 20:56:16 -07001332 const sp<IAfThreadBase> thread = mThread.promote();
Eric Laurent81784c32012-11-19 14:55:58 -08001333 if (thread != 0) {
Andy Hung87c693c2023-07-06 20:56:16 -07001334 Mutex::Autolock _l(thread->mutex());
1335 auto* const playbackThread = thread->asIAfPlaybackThread().get();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001336 switch (mState) {
1337 case STOPPING_1:
1338 case STOPPING_2:
1339 if (!isOffloaded()) {
1340 /* nothing to do if track is not offloaded */
1341 break;
1342 }
1343
1344 // Offloaded track was draining, we need to carry on draining when resumed
1345 mResumeToStopping = true;
Chih-Hung Hsieh2b487032018-09-13 14:16:02 -07001346 FALLTHROUGH_INTENDED;
Eric Laurentbfb1b832013-01-07 09:53:42 -08001347 case ACTIVE:
1348 case RESUMING:
Eric Laurent81784c32012-11-19 14:55:58 -08001349 mState = PAUSING;
Andy Hungc0691382018-09-12 18:01:57 -07001350 ALOGV("%s(%d): ACTIVE/RESUMING => PAUSING on thread %d",
1351 __func__, mId, (int)mThreadIoHandle);
Kuowei Li23666472021-01-20 10:23:25 +08001352 if (isOffloadedOrDirect()) {
1353 mPauseHwPending = true;
1354 }
Eric Laurentede6c3b2013-09-19 14:37:46 -07001355 playbackThread->broadcast_l();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001356 break;
Eric Laurent81784c32012-11-19 14:55:58 -08001357
Eric Laurentbfb1b832013-01-07 09:53:42 -08001358 default:
1359 break;
Eric Laurent81784c32012-11-19 14:55:58 -08001360 }
jiabin7434e812023-06-27 18:22:35 +00001361 // Pausing the TeePatch to avoid a glitch on underrun, at the cost of buffered audio loss.
1362 forEachTeePatchTrack_l([](const auto& patchTrack) { patchTrack->pause(); });
Eric Laurent81784c32012-11-19 14:55:58 -08001363 }
1364}
1365
Andy Hung8d31fd22023-06-26 19:20:57 -07001366void Track::flush()
Eric Laurent81784c32012-11-19 14:55:58 -08001367{
Andy Hungc0691382018-09-12 18:01:57 -07001368 ALOGV("%s(%d)", __func__, mId);
Andy Hung87c693c2023-07-06 20:56:16 -07001369 const sp<IAfThreadBase> thread = mThread.promote();
Eric Laurent81784c32012-11-19 14:55:58 -08001370 if (thread != 0) {
Andy Hung87c693c2023-07-06 20:56:16 -07001371 Mutex::Autolock _l(thread->mutex());
1372 auto* const playbackThread = thread->asIAfPlaybackThread().get();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001373
Phil Burk4bb650b2016-09-09 12:11:17 -07001374 // Flush the ring buffer now if the track is not active in the PlaybackThread.
1375 // Otherwise the flush would not be done until the track is resumed.
1376 // Requires FastTrack removal be BLOCK_UNTIL_ACKED
Andy Hung87c693c2023-07-06 20:56:16 -07001377 if (!playbackThread->isTrackActive(this)) {
Phil Burk4bb650b2016-09-09 12:11:17 -07001378 (void)mServerProxy->flushBufferIfNeeded();
1379 }
1380
Eric Laurentbfb1b832013-01-07 09:53:42 -08001381 if (isOffloaded()) {
1382 // If offloaded we allow flush during any state except terminated
1383 // and keep the track active to avoid problems if user is seeking
1384 // rapidly and underlying hardware has a significant delay handling
1385 // a pause
1386 if (isTerminated()) {
1387 return;
1388 }
1389
Andy Hung9d84af52018-09-12 18:03:44 -07001390 ALOGV("%s(%d): offload flush", __func__, mId);
Eric Laurent81784c32012-11-19 14:55:58 -08001391 reset();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001392
1393 if (mState == STOPPING_1 || mState == STOPPING_2) {
Andy Hung9d84af52018-09-12 18:03:44 -07001394 ALOGV("%s(%d): flushed in STOPPING_1 or 2 state, change state to ACTIVE",
1395 __func__, mId);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001396 mState = ACTIVE;
1397 }
1398
Haynes Mathew George7844f672014-01-15 12:32:55 -08001399 mFlushHwPending = true;
Eric Laurentbfb1b832013-01-07 09:53:42 -08001400 mResumeToStopping = false;
1401 } else {
1402 if (mState != STOPPING_1 && mState != STOPPING_2 && mState != STOPPED &&
1403 mState != PAUSED && mState != PAUSING && mState != IDLE && mState != FLUSHED) {
1404 return;
1405 }
1406 // No point remaining in PAUSED state after a flush => go to
1407 // FLUSHED state
1408 mState = FLUSHED;
1409 // do not reset the track if it is still in the process of being stopped or paused.
1410 // this will be done by prepareTracks_l() when the track is stopped.
1411 // prepareTracks_l() will see mState == FLUSHED, then
1412 // remove from active track list, reset(), and trigger presentation complete
Eric Laurentd1f69b02014-12-15 14:33:13 -08001413 if (isDirect()) {
1414 mFlushHwPending = true;
1415 }
Andy Hung87c693c2023-07-06 20:56:16 -07001416 if (!playbackThread->isTrackActive(this)) {
Eric Laurentbfb1b832013-01-07 09:53:42 -08001417 reset();
1418 }
Eric Laurent81784c32012-11-19 14:55:58 -08001419 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08001420 // Prevent flush being lost if the track is flushed and then resumed
1421 // before mixer thread can run. This is important when offloading
1422 // because the hardware buffer could hold a large amount of audio
Eric Laurentede6c3b2013-09-19 14:37:46 -07001423 playbackThread->broadcast_l();
jiabin7434e812023-06-27 18:22:35 +00001424 // Flush the Tee to avoid on resume playing old data and glitching on the transition to
1425 // new data
1426 forEachTeePatchTrack_l([](const auto& patchTrack) { patchTrack->flush(); });
Eric Laurent81784c32012-11-19 14:55:58 -08001427 }
1428}
1429
Haynes Mathew George7844f672014-01-15 12:32:55 -08001430// must be called with thread lock held
Andy Hung8d31fd22023-06-26 19:20:57 -07001431void Track::flushAck()
Haynes Mathew George7844f672014-01-15 12:32:55 -08001432{
Andy Hung920f6572022-10-06 12:09:49 -07001433 if (!isOffloaded() && !isDirect()) {
Haynes Mathew George7844f672014-01-15 12:32:55 -08001434 return;
Andy Hung920f6572022-10-06 12:09:49 -07001435 }
Haynes Mathew George7844f672014-01-15 12:32:55 -08001436
Phil Burk4bb650b2016-09-09 12:11:17 -07001437 // Clear the client ring buffer so that the app can prime the buffer while paused.
1438 // Otherwise it might not get cleared until playback is resumed and obtainBuffer() is called.
1439 mServerProxy->flushBufferIfNeeded();
1440
Haynes Mathew George7844f672014-01-15 12:32:55 -08001441 mFlushHwPending = false;
1442}
1443
Andy Hung8d31fd22023-06-26 19:20:57 -07001444void Track::pauseAck()
Kuowei Li23666472021-01-20 10:23:25 +08001445{
1446 mPauseHwPending = false;
1447}
1448
Andy Hung8d31fd22023-06-26 19:20:57 -07001449void Track::reset()
Eric Laurent81784c32012-11-19 14:55:58 -08001450{
1451 // Do not reset twice to avoid discarding data written just after a flush and before
1452 // the audioflinger thread detects the track is stopped.
1453 if (!mResetDone) {
Eric Laurent81784c32012-11-19 14:55:58 -08001454 // Force underrun condition to avoid false underrun callback until first data is
1455 // written to buffer
Glenn Kasten96f60d82013-07-12 10:21:18 -07001456 android_atomic_and(~CBLK_FORCEREADY, &mCblk->mFlags);
Andy Hung8d31fd22023-06-26 19:20:57 -07001457 mFillingStatus = FS_FILLING;
Eric Laurent81784c32012-11-19 14:55:58 -08001458 mResetDone = true;
1459 if (mState == FLUSHED) {
1460 mState = IDLE;
1461 }
1462 }
1463}
1464
Andy Hung8d31fd22023-06-26 19:20:57 -07001465status_t Track::setParameters(const String8& keyValuePairs)
Eric Laurentbfb1b832013-01-07 09:53:42 -08001466{
Andy Hung87c693c2023-07-06 20:56:16 -07001467 const sp<IAfThreadBase> thread = mThread.promote();
Eric Laurentbfb1b832013-01-07 09:53:42 -08001468 if (thread == 0) {
Andy Hung9d84af52018-09-12 18:03:44 -07001469 ALOGE("%s(%d): thread is dead", __func__, mId);
Eric Laurentbfb1b832013-01-07 09:53:42 -08001470 return FAILED_TRANSACTION;
Andy Hung87c693c2023-07-06 20:56:16 -07001471 } else if (thread->type() == IAfThreadBase::DIRECT
1472 || thread->type() == IAfThreadBase::OFFLOAD) {
Eric Laurentbfb1b832013-01-07 09:53:42 -08001473 return thread->setParameters(keyValuePairs);
1474 } else {
1475 return PERMISSION_DENIED;
1476 }
1477}
1478
Andy Hung8d31fd22023-06-26 19:20:57 -07001479status_t Track::selectPresentation(int presentationId,
Mikhail Naganovac917ac2018-11-28 14:03:52 -08001480 int programId) {
Andy Hung87c693c2023-07-06 20:56:16 -07001481 const sp<IAfThreadBase> thread = mThread.promote();
Mikhail Naganovac917ac2018-11-28 14:03:52 -08001482 if (thread == 0) {
1483 ALOGE("thread is dead");
1484 return FAILED_TRANSACTION;
Andy Hung87c693c2023-07-06 20:56:16 -07001485 } else if (thread->type() == IAfThreadBase::DIRECT
1486 || thread->type() == IAfThreadBase::OFFLOAD) {
1487 auto directOutputThread = thread->asIAfDirectOutputThread().get();
Mikhail Naganovac917ac2018-11-28 14:03:52 -08001488 return directOutputThread->selectPresentation(presentationId, programId);
1489 }
1490 return INVALID_OPERATION;
1491}
1492
Andy Hung8d31fd22023-06-26 19:20:57 -07001493VolumeShaper::Status Track::applyVolumeShaper(
Andy Hung9fc8b5c2017-01-24 13:36:48 -08001494 const sp<VolumeShaper::Configuration>& configuration,
1495 const sp<VolumeShaper::Operation>& operation)
1496{
Andy Hung398ffa22022-12-13 19:19:53 -08001497 VolumeShaper::Status status = mVolumeHandler->applyVolumeShaper(configuration, operation);
Andy Hung10cbff12017-02-21 17:30:14 -08001498
1499 if (isOffloadedOrDirect()) {
1500 // Signal thread to fetch new volume.
Andy Hung87c693c2023-07-06 20:56:16 -07001501 const sp<IAfThreadBase> thread = mThread.promote();
Andy Hung10cbff12017-02-21 17:30:14 -08001502 if (thread != 0) {
Andy Hung87c693c2023-07-06 20:56:16 -07001503 Mutex::Autolock _l(thread->mutex());
Andy Hung10cbff12017-02-21 17:30:14 -08001504 thread->broadcast_l();
1505 }
1506 }
1507 return status;
Andy Hung9fc8b5c2017-01-24 13:36:48 -08001508}
1509
Andy Hung8d31fd22023-06-26 19:20:57 -07001510sp<VolumeShaper::State> Track::getVolumeShaperState(int id) const
Andy Hung9fc8b5c2017-01-24 13:36:48 -08001511{
1512 // Note: We don't check if Thread exists.
1513
1514 // mVolumeHandler is thread safe.
1515 return mVolumeHandler->getVolumeShaperState(id);
1516}
1517
Andy Hung8d31fd22023-06-26 19:20:57 -07001518void Track::setFinalVolume(float volumeLeft, float volumeRight)
Kevin Rocard12381092018-04-11 09:19:59 -07001519{
jiabin76d94692022-12-15 21:51:21 +00001520 mFinalVolumeLeft = volumeLeft;
1521 mFinalVolumeRight = volumeRight;
1522 const float volume = (volumeLeft + volumeRight) * 0.5f;
Kevin Rocard12381092018-04-11 09:19:59 -07001523 if (mFinalVolume != volume) { // Compare to an epsilon if too many meaningless updates
1524 mFinalVolume = volume;
1525 setMetadataHasChanged();
Andy Hunga81a4b42022-05-19 19:24:51 -07001526 mLogForceVolumeUpdate = true;
1527 }
1528 if (mLogForceVolumeUpdate) {
1529 mLogForceVolumeUpdate = false;
1530 mTrackMetrics.logVolume(mFinalVolume);
Kevin Rocard12381092018-04-11 09:19:59 -07001531 }
1532}
1533
Andy Hung8d31fd22023-06-26 19:20:57 -07001534void Track::copyMetadataTo(MetadataInserter& backInserter) const
Kevin Rocard12381092018-04-11 09:19:59 -07001535{
Eric Laurent49e39282022-06-24 18:42:45 +02001536 // Do not forward metadata for PatchTrack with unspecified stream type
1537 if (mStreamType == AUDIO_STREAM_PATCH) {
1538 return;
1539 }
1540
Eric Laurent94579172020-11-20 18:41:04 +01001541 playback_track_metadata_v7_t metadata;
1542 metadata.base = {
Kevin Rocard12381092018-04-11 09:19:59 -07001543 .usage = mAttr.usage,
1544 .content_type = mAttr.content_type,
1545 .gain = mFinalVolume,
1546 };
Eric Laurentfdf99502021-11-26 19:05:02 +01001547
1548 // When attributes are undefined, derive default values from stream type.
1549 // See AudioAttributes.java, usageForStreamType() and Builder.setInternalLegacyStreamType()
1550 if (mAttr.usage == AUDIO_USAGE_UNKNOWN) {
1551 switch (mStreamType) {
1552 case AUDIO_STREAM_VOICE_CALL:
1553 metadata.base.usage = AUDIO_USAGE_VOICE_COMMUNICATION;
1554 metadata.base.content_type = AUDIO_CONTENT_TYPE_SPEECH;
1555 break;
1556 case AUDIO_STREAM_SYSTEM:
1557 metadata.base.usage = AUDIO_USAGE_ASSISTANCE_SONIFICATION;
1558 metadata.base.content_type = AUDIO_CONTENT_TYPE_SONIFICATION;
1559 break;
1560 case AUDIO_STREAM_RING:
1561 metadata.base.usage = AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE;
1562 metadata.base.content_type = AUDIO_CONTENT_TYPE_SONIFICATION;
1563 break;
1564 case AUDIO_STREAM_MUSIC:
1565 metadata.base.usage = AUDIO_USAGE_MEDIA;
1566 metadata.base.content_type = AUDIO_CONTENT_TYPE_MUSIC;
1567 break;
1568 case AUDIO_STREAM_ALARM:
1569 metadata.base.usage = AUDIO_USAGE_ALARM;
1570 metadata.base.content_type = AUDIO_CONTENT_TYPE_SONIFICATION;
1571 break;
1572 case AUDIO_STREAM_NOTIFICATION:
1573 metadata.base.usage = AUDIO_USAGE_NOTIFICATION;
1574 metadata.base.content_type = AUDIO_CONTENT_TYPE_SONIFICATION;
1575 break;
1576 case AUDIO_STREAM_DTMF:
1577 metadata.base.usage = AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING;
1578 metadata.base.content_type = AUDIO_CONTENT_TYPE_SONIFICATION;
1579 break;
1580 case AUDIO_STREAM_ACCESSIBILITY:
1581 metadata.base.usage = AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY;
1582 metadata.base.content_type = AUDIO_CONTENT_TYPE_SPEECH;
1583 break;
1584 case AUDIO_STREAM_ASSISTANT:
1585 metadata.base.usage = AUDIO_USAGE_ASSISTANT;
1586 metadata.base.content_type = AUDIO_CONTENT_TYPE_SPEECH;
1587 break;
1588 case AUDIO_STREAM_REROUTING:
1589 metadata.base.usage = AUDIO_USAGE_VIRTUAL_SOURCE;
1590 // unknown content type
1591 break;
1592 case AUDIO_STREAM_CALL_ASSISTANT:
1593 metadata.base.usage = AUDIO_USAGE_CALL_ASSISTANT;
1594 metadata.base.content_type = AUDIO_CONTENT_TYPE_SPEECH;
1595 break;
1596 default:
1597 break;
1598 }
1599 }
1600
Eric Laurent78b07302022-10-07 16:20:34 +02001601 metadata.channel_mask = mChannelMask;
Eric Laurent94579172020-11-20 18:41:04 +01001602 strncpy(metadata.tags, mAttr.tags, AUDIO_ATTRIBUTES_TAGS_MAX_SIZE);
1603 *backInserter++ = metadata;
Kevin Rocard12381092018-04-11 09:19:59 -07001604}
1605
Andy Hung8d31fd22023-06-26 19:20:57 -07001606void Track::updateTeePatches_l() {
Jiabin Huangfb476842022-12-06 03:18:10 +00001607 if (mTeePatchesToUpdate.has_value()) {
jiabin7434e812023-06-27 18:22:35 +00001608 forEachTeePatchTrack_l([](const auto& patchTrack) { patchTrack->destroy(); });
Jiabin Huangfb476842022-12-06 03:18:10 +00001609 mTeePatches = mTeePatchesToUpdate.value();
1610 if (mState == TrackBase::ACTIVE || mState == TrackBase::RESUMING ||
1611 mState == TrackBase::STOPPING_1) {
jiabin7434e812023-06-27 18:22:35 +00001612 forEachTeePatchTrack_l([](const auto& patchTrack) { patchTrack->start(); });
Jiabin Huangfb476842022-12-06 03:18:10 +00001613 }
1614 mTeePatchesToUpdate.reset();
jiabinf042b9b2021-05-07 23:46:28 +00001615 }
Kevin Rocard153f92d2018-12-18 18:33:28 -08001616}
1617
Andy Hung8d31fd22023-06-26 19:20:57 -07001618void Track::setTeePatchesToUpdate_l(AudioFlinger::TeePatches teePatchesToUpdate) {
Jiabin Huangfb476842022-12-06 03:18:10 +00001619 ALOGW_IF(mTeePatchesToUpdate.has_value(),
1620 "%s, existing tee patches to update will be ignored", __func__);
1621 mTeePatchesToUpdate = std::move(teePatchesToUpdate);
1622}
1623
Vlad Popae8d99472022-06-30 16:02:48 +02001624// must be called with player thread lock held
Andy Hung8d31fd22023-06-26 19:20:57 -07001625void Track::processMuteEvent_l(const sp<
Vlad Popae8d99472022-06-30 16:02:48 +02001626 IAudioManager>& audioManager, mute_state_t muteState)
1627{
1628 if (mMuteState == muteState) {
1629 // mute state did not change, do nothing
1630 return;
1631 }
1632
1633 status_t result = UNKNOWN_ERROR;
1634 if (audioManager && mPortId != AUDIO_PORT_HANDLE_NONE) {
1635 if (mMuteEventExtras == nullptr) {
1636 mMuteEventExtras = std::make_unique<os::PersistableBundle>();
1637 }
1638 mMuteEventExtras->putInt(String16(kExtraPlayerEventMuteKey),
1639 static_cast<int>(muteState));
1640
1641 result = audioManager->portEvent(mPortId,
1642 PLAYER_UPDATE_MUTED,
1643 mMuteEventExtras);
1644 }
1645
1646 if (result == OK) {
1647 mMuteState = muteState;
1648 } else {
1649 ALOGW("%s(%d): cannot process mute state for port ID %d, status error %d",
1650 __func__,
1651 id(),
1652 mPortId,
1653 result);
1654 }
1655}
1656
Andy Hung8d31fd22023-06-26 19:20:57 -07001657status_t Track::getTimestamp(AudioTimestamp& timestamp)
Glenn Kasten573d80a2013-08-26 09:36:23 -07001658{
Andy Hung818e7a32016-02-16 18:08:07 -08001659 if (!isOffloaded() && !isDirect()) {
1660 return INVALID_OPERATION; // normal tracks handled through SSQ
Glenn Kastenfe346c72013-08-30 13:28:22 -07001661 }
Andy Hung87c693c2023-07-06 20:56:16 -07001662 const sp<IAfThreadBase> thread = mThread.promote();
Glenn Kasten573d80a2013-08-26 09:36:23 -07001663 if (thread == 0) {
Glenn Kastenfe346c72013-08-30 13:28:22 -07001664 return INVALID_OPERATION;
Glenn Kasten573d80a2013-08-26 09:36:23 -07001665 }
Phil Burk6140c792015-03-19 14:30:21 -07001666
Andy Hung87c693c2023-07-06 20:56:16 -07001667 Mutex::Autolock _l(thread->mutex());
1668 auto* const playbackThread = thread->asIAfPlaybackThread().get();
Andy Hung818e7a32016-02-16 18:08:07 -08001669 return playbackThread->getTimestamp_l(timestamp);
Glenn Kasten573d80a2013-08-26 09:36:23 -07001670}
1671
Andy Hung8d31fd22023-06-26 19:20:57 -07001672status_t Track::attachAuxEffect(int EffectId)
Eric Laurent81784c32012-11-19 14:55:58 -08001673{
Andy Hung87c693c2023-07-06 20:56:16 -07001674 const sp<IAfThreadBase> thread = mThread.promote();
Eric Laurent6c796322019-04-09 14:13:17 -07001675 if (thread == nullptr) {
1676 return DEAD_OBJECT;
1677 }
Eric Laurent81784c32012-11-19 14:55:58 -08001678
Andy Hung87c693c2023-07-06 20:56:16 -07001679 auto dstThread = thread->asIAfPlaybackThread();
Andy Hung8d31fd22023-06-26 19:20:57 -07001680 // srcThread is initialized by call to moveAuxEffectToIo()
Andy Hung87c693c2023-07-06 20:56:16 -07001681 sp<IAfPlaybackThread> srcThread;
Eric Laurent6c796322019-04-09 14:13:17 -07001682 sp<AudioFlinger> af = mClient->audioFlinger();
1683 status_t status = af->moveAuxEffectToIo(EffectId, dstThread, &srcThread);
Eric Laurent81784c32012-11-19 14:55:58 -08001684
Eric Laurent6c796322019-04-09 14:13:17 -07001685 if (EffectId != 0 && status == NO_ERROR) {
1686 status = dstThread->attachAuxEffect(this, EffectId);
1687 if (status == NO_ERROR) {
1688 AudioSystem::moveEffectsToIo(std::vector<int>(EffectId), dstThread->id());
Eric Laurent81784c32012-11-19 14:55:58 -08001689 }
Eric Laurent6c796322019-04-09 14:13:17 -07001690 }
1691
1692 if (status != NO_ERROR && srcThread != nullptr) {
1693 af->moveAuxEffectToIo(EffectId, srcThread, &dstThread);
Eric Laurent81784c32012-11-19 14:55:58 -08001694 }
1695 return status;
1696}
1697
Andy Hung8d31fd22023-06-26 19:20:57 -07001698void Track::setAuxBuffer(int EffectId, int32_t *buffer)
Eric Laurent81784c32012-11-19 14:55:58 -08001699{
1700 mAuxEffectId = EffectId;
1701 mAuxBuffer = buffer;
1702}
1703
Andy Hung59de4262021-06-14 10:53:54 -07001704// presentationComplete verified by frames, used by Mixed tracks.
Andy Hung8d31fd22023-06-26 19:20:57 -07001705bool Track::presentationComplete(
Andy Hung818e7a32016-02-16 18:08:07 -08001706 int64_t framesWritten, size_t audioHalFrames)
Eric Laurent81784c32012-11-19 14:55:58 -08001707{
Andy Hung818e7a32016-02-16 18:08:07 -08001708 // TODO: improve this based on FrameMap if it exists, to ensure full drain.
1709 // This assists in proper timestamp computation as well as wakelock management.
1710
Eric Laurent81784c32012-11-19 14:55:58 -08001711 // a track is considered presented when the total number of frames written to audio HAL
1712 // corresponds to the number of frames written when presentationComplete() is called for the
1713 // first time (mPresentationCompleteFrames == 0) plus the buffer filling status at that time.
Eric Laurentbfb1b832013-01-07 09:53:42 -08001714 // For an offloaded track the HAL+h/w delay is variable so a HAL drain() is used
1715 // to detect when all frames have been played. In this case framesWritten isn't
1716 // useful because it doesn't always reflect whether there is data in the h/w
1717 // buffers, particularly if a track has been paused and resumed during draining
Andy Hung9d84af52018-09-12 18:03:44 -07001718 ALOGV("%s(%d): presentationComplete() mPresentationCompleteFrames %lld framesWritten %lld",
1719 __func__, mId,
Andy Hung818e7a32016-02-16 18:08:07 -08001720 (long long)mPresentationCompleteFrames, (long long)framesWritten);
Eric Laurent81784c32012-11-19 14:55:58 -08001721 if (mPresentationCompleteFrames == 0) {
1722 mPresentationCompleteFrames = framesWritten + audioHalFrames;
Andy Hung59de4262021-06-14 10:53:54 -07001723 ALOGV("%s(%d): set:"
Andy Hung9d84af52018-09-12 18:03:44 -07001724 " mPresentationCompleteFrames %lld audioHalFrames %zu",
1725 __func__, mId,
Andy Hung818e7a32016-02-16 18:08:07 -08001726 (long long)mPresentationCompleteFrames, audioHalFrames);
Eric Laurent81784c32012-11-19 14:55:58 -08001727 }
Eric Laurentbfb1b832013-01-07 09:53:42 -08001728
Andy Hungc54b1ff2016-02-23 14:07:07 -08001729 bool complete;
Andy Hung59de4262021-06-14 10:53:54 -07001730 if (isFastTrack()) { // does not go through linear map
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001731 complete = framesWritten >= (int64_t) mPresentationCompleteFrames;
Andy Hung59de4262021-06-14 10:53:54 -07001732 ALOGV("%s(%d): %s framesWritten:%lld mPresentationCompleteFrames:%lld",
1733 __func__, mId, (complete ? "complete" : "waiting"),
1734 (long long) framesWritten, (long long) mPresentationCompleteFrames);
Andy Hungc54b1ff2016-02-23 14:07:07 -08001735 } else { // Normal tracks, OutputTracks, and PatchTracks
Glenn Kastenc42e9b42016-03-21 11:35:03 -07001736 complete = framesWritten >= (int64_t) mPresentationCompleteFrames
Andy Hungc54b1ff2016-02-23 14:07:07 -08001737 && mAudioTrackServerProxy->isDrained();
1738 }
1739
1740 if (complete) {
Andy Hung59de4262021-06-14 10:53:54 -07001741 notifyPresentationComplete();
Eric Laurent81784c32012-11-19 14:55:58 -08001742 return true;
1743 }
1744 return false;
1745}
1746
Andy Hung59de4262021-06-14 10:53:54 -07001747// presentationComplete checked by time, used by DirectTracks.
Andy Hung8d31fd22023-06-26 19:20:57 -07001748bool Track::presentationComplete(uint32_t latencyMs)
Andy Hung59de4262021-06-14 10:53:54 -07001749{
1750 // For Offloaded or Direct tracks.
1751
1752 // For a direct track, we incorporated time based testing for presentationComplete.
1753
1754 // For an offloaded track the HAL+h/w delay is variable so a HAL drain() is used
1755 // to detect when all frames have been played. In this case latencyMs isn't
1756 // useful because it doesn't always reflect whether there is data in the h/w
1757 // buffers, particularly if a track has been paused and resumed during draining
1758
1759 constexpr float MIN_SPEED = 0.125f; // min speed scaling allowed for timely response.
1760 if (mPresentationCompleteTimeNs == 0) {
1761 mPresentationCompleteTimeNs = systemTime() + latencyMs * 1e6 / fmax(mSpeed, MIN_SPEED);
1762 ALOGV("%s(%d): set: latencyMs %u mPresentationCompleteTimeNs:%lld",
1763 __func__, mId, latencyMs, (long long) mPresentationCompleteTimeNs);
1764 }
1765
1766 bool complete;
1767 if (isOffloaded()) {
1768 complete = true;
1769 } else { // Direct
1770 complete = systemTime() >= mPresentationCompleteTimeNs;
1771 ALOGV("%s(%d): %s", __func__, mId, (complete ? "complete" : "waiting"));
1772 }
1773 if (complete) {
1774 notifyPresentationComplete();
1775 return true;
1776 }
1777 return false;
1778}
1779
Andy Hung8d31fd22023-06-26 19:20:57 -07001780void Track::notifyPresentationComplete()
Andy Hung59de4262021-06-14 10:53:54 -07001781{
1782 // This only triggers once. TODO: should we enforce this?
1783 triggerEvents(AudioSystem::SYNC_EVENT_PRESENTATION_COMPLETE);
1784 mAudioTrackServerProxy->setStreamEndDone();
1785}
1786
Andy Hung8d31fd22023-06-26 19:20:57 -07001787void Track::triggerEvents(AudioSystem::sync_event_t type)
Eric Laurent81784c32012-11-19 14:55:58 -08001788{
Andy Hung068e08e2023-05-15 19:02:55 -07001789 for (auto it = mSyncEvents.begin(); it != mSyncEvents.end();) {
1790 if ((*it)->type() == type) {
Andy Hung93bb5732023-05-04 21:16:34 -07001791 ALOGV("%s: triggering SyncEvent type %d", __func__, type);
Andy Hung068e08e2023-05-15 19:02:55 -07001792 (*it)->trigger();
1793 it = mSyncEvents.erase(it);
Ivan Lozano5ec161b2017-12-06 10:00:28 -08001794 } else {
Andy Hung068e08e2023-05-15 19:02:55 -07001795 ++it;
Eric Laurent81784c32012-11-19 14:55:58 -08001796 }
1797 }
1798}
1799
1800// implement VolumeBufferProvider interface
1801
Andy Hung8d31fd22023-06-26 19:20:57 -07001802gain_minifloat_packed_t Track::getVolumeLR() const
Eric Laurent81784c32012-11-19 14:55:58 -08001803{
1804 // called by FastMixer, so not allowed to take any locks, block, or do I/O including logs
1805 ALOG_ASSERT(isFastTrack() && (mCblk != NULL));
Glenn Kastenc56f3422014-03-21 17:53:17 -07001806 gain_minifloat_packed_t vlr = mAudioTrackServerProxy->getVolumeLR();
1807 float vl = float_from_gain(gain_minifloat_unpack_left(vlr));
1808 float vr = float_from_gain(gain_minifloat_unpack_right(vlr));
Eric Laurent81784c32012-11-19 14:55:58 -08001809 // track volumes come from shared memory, so can't be trusted and must be clamped
Glenn Kastenc56f3422014-03-21 17:53:17 -07001810 if (vl > GAIN_FLOAT_UNITY) {
1811 vl = GAIN_FLOAT_UNITY;
Eric Laurent81784c32012-11-19 14:55:58 -08001812 }
Glenn Kastenc56f3422014-03-21 17:53:17 -07001813 if (vr > GAIN_FLOAT_UNITY) {
1814 vr = GAIN_FLOAT_UNITY;
Eric Laurent81784c32012-11-19 14:55:58 -08001815 }
1816 // now apply the cached master volume and stream type volume;
1817 // this is trusted but lacks any synchronization or barrier so may be stale
1818 float v = mCachedVolume;
1819 vl *= v;
1820 vr *= v;
Glenn Kastenc56f3422014-03-21 17:53:17 -07001821 // re-combine into packed minifloat
1822 vlr = gain_minifloat_pack(gain_from_float(vl), gain_from_float(vr));
Eric Laurent81784c32012-11-19 14:55:58 -08001823 // FIXME look at mute, pause, and stop flags
1824 return vlr;
1825}
1826
Andy Hung8d31fd22023-06-26 19:20:57 -07001827status_t Track::setSyncEvent(
Andy Hung068e08e2023-05-15 19:02:55 -07001828 const sp<audioflinger::SyncEvent>& event)
Eric Laurent81784c32012-11-19 14:55:58 -08001829{
Eric Laurentbfb1b832013-01-07 09:53:42 -08001830 if (isTerminated() || mState == PAUSED ||
Eric Laurent81784c32012-11-19 14:55:58 -08001831 ((framesReady() == 0) && ((mSharedBuffer != 0) ||
1832 (mState == STOPPED)))) {
Andy Hung9d84af52018-09-12 18:03:44 -07001833 ALOGW("%s(%d): in invalid state %d on session %d %s mode, framesReady %zu",
1834 __func__, mId,
Andy Hung959b5b82021-09-24 10:46:20 -07001835 (int)mState, mSessionId, (mSharedBuffer != 0) ? "static" : "stream", framesReady());
Eric Laurent81784c32012-11-19 14:55:58 -08001836 event->cancel();
1837 return INVALID_OPERATION;
1838 }
1839 (void) TrackBase::setSyncEvent(event);
1840 return NO_ERROR;
1841}
1842
Andy Hung8d31fd22023-06-26 19:20:57 -07001843void Track::invalidate()
Glenn Kasten5736c352012-12-04 12:12:34 -08001844{
Eric Laurent6acd1d42017-01-04 14:23:29 -08001845 TrackBase::invalidate();
Eric Laurent4d231dc2016-03-11 18:38:23 -08001846 signalClientFlag(CBLK_INVALID);
Eric Laurent4d231dc2016-03-11 18:38:23 -08001847}
1848
Andy Hung8d31fd22023-06-26 19:20:57 -07001849void Track::disable()
Eric Laurent4d231dc2016-03-11 18:38:23 -08001850{
Kevin Rocard01c7d9e2019-09-18 11:24:52 +01001851 // TODO(b/142394888): the filling status should also be reset to filling
Eric Laurent4d231dc2016-03-11 18:38:23 -08001852 signalClientFlag(CBLK_DISABLED);
1853}
1854
Andy Hung8d31fd22023-06-26 19:20:57 -07001855void Track::signalClientFlag(int32_t flag)
Eric Laurent4d231dc2016-03-11 18:38:23 -08001856{
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001857 // FIXME should use proxy, and needs work
1858 audio_track_cblk_t* cblk = mCblk;
Eric Laurent4d231dc2016-03-11 18:38:23 -08001859 android_atomic_or(flag, &cblk->mFlags);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08001860 android_atomic_release_store(0x40000000, &cblk->mFutex);
1861 // client is not in server, so FUTEX_WAKE is needed instead of FUTEX_WAKE_PRIVATE
Elliott Hughesee499292014-05-21 17:55:51 -07001862 (void) syscall(__NR_futex, &cblk->mFutex, FUTEX_WAKE, INT_MAX);
Glenn Kasten5736c352012-12-04 12:12:34 -08001863}
1864
Andy Hung8d31fd22023-06-26 19:20:57 -07001865void Track::signal()
Eric Laurent59fe0102013-09-27 18:48:26 -07001866{
Andy Hung87c693c2023-07-06 20:56:16 -07001867 const sp<IAfThreadBase> thread = mThread.promote();
Eric Laurent59fe0102013-09-27 18:48:26 -07001868 if (thread != 0) {
Andy Hung87c693c2023-07-06 20:56:16 -07001869 auto* const t = thread->asIAfPlaybackThread().get();
1870 Mutex::Autolock _l(t->mutex());
Eric Laurent59fe0102013-09-27 18:48:26 -07001871 t->broadcast_l();
1872 }
1873}
1874
Andy Hung8d31fd22023-06-26 19:20:57 -07001875status_t Track::getDualMonoMode(audio_dual_mono_mode_t* mode) const
Kuowei Lid4adbdb2020-08-13 14:44:25 +08001876{
1877 status_t status = INVALID_OPERATION;
1878 if (isOffloadedOrDirect()) {
Andy Hung87c693c2023-07-06 20:56:16 -07001879 const sp<IAfThreadBase> thread = mThread.promote();
Kuowei Lid4adbdb2020-08-13 14:44:25 +08001880 if (thread != nullptr) {
Andy Hung87c693c2023-07-06 20:56:16 -07001881 auto* const t = thread->asIAfPlaybackThread().get();
1882 Mutex::Autolock _l(t->mutex());
1883 status = t->getOutput_l()->stream->getDualMonoMode(mode);
Kuowei Lid4adbdb2020-08-13 14:44:25 +08001884 ALOGD_IF((status == NO_ERROR) && (mDualMonoMode != *mode),
1885 "%s: mode %d inconsistent", __func__, mDualMonoMode);
1886 }
1887 }
1888 return status;
1889}
1890
Andy Hung8d31fd22023-06-26 19:20:57 -07001891status_t Track::setDualMonoMode(audio_dual_mono_mode_t mode)
Kuowei Lid4adbdb2020-08-13 14:44:25 +08001892{
1893 status_t status = INVALID_OPERATION;
1894 if (isOffloadedOrDirect()) {
Andy Hung87c693c2023-07-06 20:56:16 -07001895 const sp<IAfThreadBase> thread = mThread.promote();
Kuowei Lid4adbdb2020-08-13 14:44:25 +08001896 if (thread != nullptr) {
Andy Hung87c693c2023-07-06 20:56:16 -07001897 auto* const t = thread->asIAfPlaybackThread().get();
1898 Mutex::Autolock lock(t->mutex());
1899 status = t->getOutput_l()->stream->setDualMonoMode(mode);
Kuowei Lid4adbdb2020-08-13 14:44:25 +08001900 if (status == NO_ERROR) {
1901 mDualMonoMode = mode;
1902 }
1903 }
1904 }
1905 return status;
1906}
1907
Andy Hung8d31fd22023-06-26 19:20:57 -07001908status_t Track::getAudioDescriptionMixLevel(float* leveldB) const
Kuowei Lid4adbdb2020-08-13 14:44:25 +08001909{
1910 status_t status = INVALID_OPERATION;
1911 if (isOffloadedOrDirect()) {
Andy Hung87c693c2023-07-06 20:56:16 -07001912 sp<IAfThreadBase> thread = mThread.promote();
Kuowei Lid4adbdb2020-08-13 14:44:25 +08001913 if (thread != nullptr) {
Andy Hung87c693c2023-07-06 20:56:16 -07001914 auto* const t = thread->asIAfPlaybackThread().get();
1915 Mutex::Autolock lock(t->mutex());
1916 status = t->getOutput_l()->stream->getAudioDescriptionMixLevel(leveldB);
Kuowei Lid4adbdb2020-08-13 14:44:25 +08001917 ALOGD_IF((status == NO_ERROR) && (mAudioDescriptionMixLevel != *leveldB),
1918 "%s: level %.3f inconsistent", __func__, mAudioDescriptionMixLevel);
1919 }
1920 }
1921 return status;
1922}
1923
Andy Hung8d31fd22023-06-26 19:20:57 -07001924status_t Track::setAudioDescriptionMixLevel(float leveldB)
Kuowei Lid4adbdb2020-08-13 14:44:25 +08001925{
1926 status_t status = INVALID_OPERATION;
1927 if (isOffloadedOrDirect()) {
Andy Hung87c693c2023-07-06 20:56:16 -07001928 const sp<IAfThreadBase> thread = mThread.promote();
Kuowei Lid4adbdb2020-08-13 14:44:25 +08001929 if (thread != nullptr) {
Andy Hung87c693c2023-07-06 20:56:16 -07001930 auto* const t = thread->asIAfPlaybackThread().get();
1931 Mutex::Autolock lock(t->mutex());
1932 status = t->getOutput_l()->stream->setAudioDescriptionMixLevel(leveldB);
Kuowei Lid4adbdb2020-08-13 14:44:25 +08001933 if (status == NO_ERROR) {
1934 mAudioDescriptionMixLevel = leveldB;
1935 }
1936 }
1937 }
1938 return status;
1939}
1940
Andy Hung8d31fd22023-06-26 19:20:57 -07001941status_t Track::getPlaybackRateParameters(
Andy Hungd29af632023-06-23 19:27:19 -07001942 audio_playback_rate_t* playbackRate) const
Kuowei Lid4adbdb2020-08-13 14:44:25 +08001943{
1944 status_t status = INVALID_OPERATION;
1945 if (isOffloadedOrDirect()) {
Andy Hung87c693c2023-07-06 20:56:16 -07001946 const sp<IAfThreadBase> thread = mThread.promote();
Kuowei Lid4adbdb2020-08-13 14:44:25 +08001947 if (thread != nullptr) {
Andy Hung87c693c2023-07-06 20:56:16 -07001948 auto* const t = thread->asIAfPlaybackThread().get();
1949 Mutex::Autolock lock(t->mutex());
1950 status = t->getOutput_l()->stream->getPlaybackRateParameters(playbackRate);
Kuowei Lid4adbdb2020-08-13 14:44:25 +08001951 ALOGD_IF((status == NO_ERROR) &&
1952 !isAudioPlaybackRateEqual(mPlaybackRateParameters, *playbackRate),
1953 "%s: playbackRate inconsistent", __func__);
1954 }
1955 }
1956 return status;
1957}
1958
Andy Hung8d31fd22023-06-26 19:20:57 -07001959status_t Track::setPlaybackRateParameters(
Kuowei Lid4adbdb2020-08-13 14:44:25 +08001960 const audio_playback_rate_t& playbackRate)
1961{
1962 status_t status = INVALID_OPERATION;
1963 if (isOffloadedOrDirect()) {
Andy Hung87c693c2023-07-06 20:56:16 -07001964 const sp<IAfThreadBase> thread = mThread.promote();
Kuowei Lid4adbdb2020-08-13 14:44:25 +08001965 if (thread != nullptr) {
Andy Hung87c693c2023-07-06 20:56:16 -07001966 auto* const t = thread->asIAfPlaybackThread().get();
1967 Mutex::Autolock lock(t->mutex());
1968 status = t->getOutput_l()->stream->setPlaybackRateParameters(playbackRate);
Kuowei Lid4adbdb2020-08-13 14:44:25 +08001969 if (status == NO_ERROR) {
1970 mPlaybackRateParameters = playbackRate;
1971 }
1972 }
1973 }
1974 return status;
1975}
1976
Krishnankutty Kolathappilly8d6c2922014-02-04 16:23:42 -08001977//To be called with thread lock held
Andy Hung8d31fd22023-06-26 19:20:57 -07001978bool Track::isResumePending() const {
Andy Hung920f6572022-10-06 12:09:49 -07001979 if (mState == RESUMING) {
Krishnankutty Kolathappilly8d6c2922014-02-04 16:23:42 -08001980 return true;
Andy Hung920f6572022-10-06 12:09:49 -07001981 }
Krishnankutty Kolathappilly8d6c2922014-02-04 16:23:42 -08001982 /* Resume is pending if track was stopping before pause was called */
1983 if (mState == STOPPING_1 &&
Andy Hung920f6572022-10-06 12:09:49 -07001984 mResumeToStopping) {
Krishnankutty Kolathappilly8d6c2922014-02-04 16:23:42 -08001985 return true;
Andy Hung920f6572022-10-06 12:09:49 -07001986 }
Krishnankutty Kolathappilly8d6c2922014-02-04 16:23:42 -08001987
1988 return false;
1989}
1990
1991//To be called with thread lock held
Andy Hung8d31fd22023-06-26 19:20:57 -07001992void Track::resumeAck() {
Andy Hung920f6572022-10-06 12:09:49 -07001993 if (mState == RESUMING) {
Krishnankutty Kolathappilly8d6c2922014-02-04 16:23:42 -08001994 mState = ACTIVE;
Andy Hung920f6572022-10-06 12:09:49 -07001995 }
Haynes Mathew George2d3ca682014-03-07 13:43:49 -08001996
Krishnankutty Kolathappilly8d6c2922014-02-04 16:23:42 -08001997 // Other possibility of pending resume is stopping_1 state
1998 // Do not update the state from stopping as this prevents
Haynes Mathew George2d3ca682014-03-07 13:43:49 -08001999 // drain being called.
2000 if (mState == STOPPING_1) {
2001 mResumeToStopping = false;
2002 }
Krishnankutty Kolathappilly8d6c2922014-02-04 16:23:42 -08002003}
Andy Hunge10393e2015-06-12 13:59:33 -07002004
2005//To be called with thread lock held
Andy Hung8d31fd22023-06-26 19:20:57 -07002006void Track::updateTrackFrameInfo(
Andy Hung818e7a32016-02-16 18:08:07 -08002007 int64_t trackFramesReleased, int64_t sinkFramesWritten,
Andy Hungcef2daa2018-06-01 15:31:49 -07002008 uint32_t halSampleRate, const ExtendedTimestamp &timeStamp) {
Andy Hung30282562018-08-08 18:27:03 -07002009 // Make the kernel frametime available.
2010 const FrameTime ft{
2011 timeStamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL],
2012 timeStamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL]};
2013 // ALOGD("FrameTime: %lld %lld", (long long)ft.frames, (long long)ft.timeNs);
2014 mKernelFrameTime.store(ft);
2015 if (!audio_is_linear_pcm(mFormat)) {
2016 return;
2017 }
2018
Andy Hung818e7a32016-02-16 18:08:07 -08002019 //update frame map
Andy Hunge10393e2015-06-12 13:59:33 -07002020 mFrameMap.push(trackFramesReleased, sinkFramesWritten);
Andy Hung818e7a32016-02-16 18:08:07 -08002021
2022 // adjust server times and set drained state.
2023 //
2024 // Our timestamps are only updated when the track is on the Thread active list.
2025 // We need to ensure that tracks are not removed before full drain.
2026 ExtendedTimestamp local = timeStamp;
Andy Hungcef2daa2018-06-01 15:31:49 -07002027 bool drained = true; // default assume drained, if no server info found
Andy Hung818e7a32016-02-16 18:08:07 -08002028 bool checked = false;
2029 for (int i = ExtendedTimestamp::LOCATION_MAX - 1;
2030 i >= ExtendedTimestamp::LOCATION_SERVER; --i) {
2031 // Lookup the track frame corresponding to the sink frame position.
2032 if (local.mTimeNs[i] > 0) {
2033 local.mPosition[i] = mFrameMap.findX(local.mPosition[i]);
2034 // check drain state from the latest stage in the pipeline.
Andy Hung6d7b1192016-05-07 22:59:48 -07002035 if (!checked && i <= ExtendedTimestamp::LOCATION_KERNEL) {
Andy Hungcef2daa2018-06-01 15:31:49 -07002036 drained = local.mPosition[i] >= mAudioTrackServerProxy->framesReleased();
Andy Hung818e7a32016-02-16 18:08:07 -08002037 checked = true;
2038 }
2039 }
Andy Hunge10393e2015-06-12 13:59:33 -07002040 }
Andy Hungcef2daa2018-06-01 15:31:49 -07002041
Andy Hung93bb5732023-05-04 21:16:34 -07002042 ALOGV("%s: trackFramesReleased:%lld sinkFramesWritten:%lld setDrained: %d",
2043 __func__, (long long)trackFramesReleased, (long long)sinkFramesWritten, drained);
Andy Hungcef2daa2018-06-01 15:31:49 -07002044 mAudioTrackServerProxy->setDrained(drained);
Andy Hungea2b9c02016-02-12 17:06:53 -08002045 // Set correction for flushed frames that are not accounted for in released.
Andy Hungea2b9c02016-02-12 17:06:53 -08002046 local.mFlushed = mAudioTrackServerProxy->framesFlushed();
Andy Hung818e7a32016-02-16 18:08:07 -08002047 mServerProxy->setTimestamp(local);
Andy Hungcef2daa2018-06-01 15:31:49 -07002048
2049 // Compute latency info.
2050 const bool useTrackTimestamp = !drained;
2051 const double latencyMs = useTrackTimestamp
2052 ? local.getOutputServerLatencyMs(sampleRate())
2053 : timeStamp.getOutputServerLatencyMs(halSampleRate);
2054
2055 mServerLatencyFromTrack.store(useTrackTimestamp);
2056 mServerLatencyMs.store(latencyMs);
Andy Hungb68f5eb2019-12-03 16:49:17 -08002057
Andy Hung62921122020-05-18 10:47:31 -07002058 if (mLogStartCountdown > 0
2059 && local.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] > 0
2060 && local.mPosition[ExtendedTimestamp::LOCATION_KERNEL] > 0)
2061 {
2062 if (mLogStartCountdown > 1) {
2063 --mLogStartCountdown;
2064 } else if (latencyMs < mLogLatencyMs) { // wait for latency to stabilize (dip)
2065 mLogStartCountdown = 0;
Andy Hungb68f5eb2019-12-03 16:49:17 -08002066 // startup is the difference in times for the current timestamp and our start
2067 double startUpMs =
Andy Hung62921122020-05-18 10:47:31 -07002068 (local.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] - mLogStartTimeNs) * 1e-6;
Andy Hungb68f5eb2019-12-03 16:49:17 -08002069 // adjust for frames played.
Andy Hung62921122020-05-18 10:47:31 -07002070 startUpMs -= (local.mPosition[ExtendedTimestamp::LOCATION_KERNEL] - mLogStartFrames)
2071 * 1e3 / mSampleRate;
2072 ALOGV("%s: latencyMs:%lf startUpMs:%lf"
2073 " localTime:%lld startTime:%lld"
2074 " localPosition:%lld startPosition:%lld",
2075 __func__, latencyMs, startUpMs,
2076 (long long)local.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL],
Andy Hungb68f5eb2019-12-03 16:49:17 -08002077 (long long)mLogStartTimeNs,
Andy Hung62921122020-05-18 10:47:31 -07002078 (long long)local.mPosition[ExtendedTimestamp::LOCATION_KERNEL],
Andy Hungb68f5eb2019-12-03 16:49:17 -08002079 (long long)mLogStartFrames);
Andy Hungc2b11cb2020-04-22 09:04:01 -07002080 mTrackMetrics.logLatencyAndStartup(latencyMs, startUpMs);
Andy Hungb68f5eb2019-12-03 16:49:17 -08002081 }
Andy Hung62921122020-05-18 10:47:31 -07002082 mLogLatencyMs = latencyMs;
Andy Hungb68f5eb2019-12-03 16:49:17 -08002083 }
Andy Hunge10393e2015-06-12 13:59:33 -07002084}
2085
Andy Hung8d31fd22023-06-26 19:20:57 -07002086bool Track::AudioVibrationController::setMute(bool muted) {
Andy Hung87c693c2023-07-06 20:56:16 -07002087 const sp<IAfThreadBase> thread = mTrack->mThread.promote();
jiabin57303cc2018-12-18 15:45:57 -08002088 if (thread != 0) {
2089 // Lock for updating mHapticPlaybackEnabled.
Andy Hung87c693c2023-07-06 20:56:16 -07002090 Mutex::Autolock _l(thread->mutex());
2091 auto* const playbackThread = thread->asIAfPlaybackThread().get();
jiabin57303cc2018-12-18 15:45:57 -08002092 if ((mTrack->channelMask() & AUDIO_CHANNEL_HAPTIC_ALL) != AUDIO_CHANNEL_NONE
Andy Hung87c693c2023-07-06 20:56:16 -07002093 && playbackThread->hapticChannelCount() > 0) {
SPeak Shenc19aa9e2022-11-11 00:28:50 +08002094 ALOGD("%s, haptic playback was %s for track %d",
2095 __func__, muted ? "muted" : "unmuted", mTrack->id());
2096 mTrack->setHapticPlaybackEnabled(!muted);
2097 return true;
jiabin57303cc2018-12-18 15:45:57 -08002098 }
2099 }
SPeak Shenc19aa9e2022-11-11 00:28:50 +08002100 return false;
2101}
2102
Andy Hung8d31fd22023-06-26 19:20:57 -07002103binder::Status Track::AudioVibrationController::mute(
SPeak Shenc19aa9e2022-11-11 00:28:50 +08002104 /*out*/ bool *ret) {
2105 *ret = setMute(true);
jiabin57303cc2018-12-18 15:45:57 -08002106 return binder::Status::ok();
2107}
2108
Andy Hung8d31fd22023-06-26 19:20:57 -07002109binder::Status Track::AudioVibrationController::unmute(
jiabin57303cc2018-12-18 15:45:57 -08002110 /*out*/ bool *ret) {
SPeak Shenc19aa9e2022-11-11 00:28:50 +08002111 *ret = setMute(false);
jiabin57303cc2018-12-18 15:45:57 -08002112 return binder::Status::ok();
2113}
2114
Eric Laurent81784c32012-11-19 14:55:58 -08002115// ----------------------------------------------------------------------------
Andy Hung9d84af52018-09-12 18:03:44 -07002116#undef LOG_TAG
2117#define LOG_TAG "AF::OutputTrack"
Eric Laurent81784c32012-11-19 14:55:58 -08002118
Andy Hung8d31fd22023-06-26 19:20:57 -07002119/* static */
Andy Hung87c693c2023-07-06 20:56:16 -07002120sp<IAfOutputTrack> IAfOutputTrack::create(
2121 IAfPlaybackThread* playbackThread,
2122 IAfDuplicatingThread* sourceThread,
Andy Hung8d31fd22023-06-26 19:20:57 -07002123 uint32_t sampleRate,
2124 audio_format_t format,
2125 audio_channel_mask_t channelMask,
2126 size_t frameCount,
2127 const AttributionSourceState& attributionSource) {
2128 return sp<OutputTrack>::make(
Andy Hung87c693c2023-07-06 20:56:16 -07002129 playbackThread,
2130 sourceThread,
Andy Hung8d31fd22023-06-26 19:20:57 -07002131 sampleRate,
2132 format,
2133 channelMask,
2134 frameCount,
2135 attributionSource);
2136}
2137
2138OutputTrack::OutputTrack(
Andy Hung87c693c2023-07-06 20:56:16 -07002139 IAfPlaybackThread* playbackThread,
2140 IAfDuplicatingThread* sourceThread,
Eric Laurent81784c32012-11-19 14:55:58 -08002141 uint32_t sampleRate,
2142 audio_format_t format,
2143 audio_channel_mask_t channelMask,
Marco Nelissen462fd2f2013-01-14 14:12:05 -08002144 size_t frameCount,
Svet Ganov33761132021-05-13 22:51:08 +00002145 const AttributionSourceState& attributionSource)
Eric Laurent223fd5c2014-11-11 13:43:36 -08002146 : Track(playbackThread, NULL, AUDIO_STREAM_PATCH,
Kevin Rocard1f564ac2018-03-29 13:53:10 -07002147 audio_attributes_t{} /* currently unused for output track */,
Eric Laurent223fd5c2014-11-11 13:43:36 -08002148 sampleRate, format, channelMask, frameCount,
Andy Hung8fe68032017-06-05 16:17:51 -07002149 nullptr /* buffer */, (size_t)0 /* bufferSize */, nullptr /* sharedBuffer */,
Svet Ganov33761132021-05-13 22:51:08 +00002150 AUDIO_SESSION_NONE, getpid(), attributionSource, AUDIO_OUTPUT_FLAG_NONE,
Glenn Kastend848eb42016-03-08 13:42:11 -08002151 TYPE_OUTPUT),
Eric Laurent5bba2f62016-03-18 11:14:14 -07002152 mActive(false), mSourceThread(sourceThread)
Eric Laurent81784c32012-11-19 14:55:58 -08002153{
2154
2155 if (mCblk != NULL) {
Eric Laurent81784c32012-11-19 14:55:58 -08002156 mOutBuffer.frameCount = 0;
Andy Hung87c693c2023-07-06 20:56:16 -07002157 playbackThread->addOutputTrack_l(this);
Andy Hung9d84af52018-09-12 18:03:44 -07002158 ALOGV("%s(): mCblk %p, mBuffer %p, "
Glenn Kastenc42e9b42016-03-21 11:35:03 -07002159 "frameCount %zu, mChannelMask 0x%08x",
Andy Hung9d84af52018-09-12 18:03:44 -07002160 __func__, mCblk, mBuffer,
Glenn Kasten74935e42013-12-19 08:56:45 -08002161 frameCount, mChannelMask);
Glenn Kastene3aa6592012-12-04 12:22:46 -08002162 // since client and server are in the same process,
2163 // the buffer has the same virtual address on both sides
Glenn Kasten529c61b2014-07-18 15:31:02 -07002164 mClientProxy = new AudioTrackClientProxy(mCblk, mBuffer, mFrameCount, mFrameSize,
2165 true /*clientInServer*/);
Glenn Kastenc56f3422014-03-21 17:53:17 -07002166 mClientProxy->setVolumeLR(GAIN_MINIFLOAT_PACKED_UNITY);
Eric Laurent8d2d4932013-04-25 12:56:18 -07002167 mClientProxy->setSendLevel(0.0);
2168 mClientProxy->setSampleRate(sampleRate);
Eric Laurent81784c32012-11-19 14:55:58 -08002169 } else {
Andy Hung9d84af52018-09-12 18:03:44 -07002170 ALOGW("%s(%d): Error creating output track on thread %d",
2171 __func__, mId, (int)mThreadIoHandle);
Eric Laurent81784c32012-11-19 14:55:58 -08002172 }
2173}
2174
Andy Hung8d31fd22023-06-26 19:20:57 -07002175OutputTrack::~OutputTrack()
Eric Laurent81784c32012-11-19 14:55:58 -08002176{
2177 clearBufferQueue();
Glenn Kastene3aa6592012-12-04 12:22:46 -08002178 // superclass destructor will now delete the server proxy and shared memory both refer to
Eric Laurent81784c32012-11-19 14:55:58 -08002179}
2180
Andy Hung8d31fd22023-06-26 19:20:57 -07002181status_t OutputTrack::start(AudioSystem::sync_event_t event,
Glenn Kastend848eb42016-03-08 13:42:11 -08002182 audio_session_t triggerSession)
Eric Laurent81784c32012-11-19 14:55:58 -08002183{
2184 status_t status = Track::start(event, triggerSession);
2185 if (status != NO_ERROR) {
2186 return status;
2187 }
2188
2189 mActive = true;
2190 mRetryCount = 127;
2191 return status;
2192}
2193
Andy Hung8d31fd22023-06-26 19:20:57 -07002194void OutputTrack::stop()
Eric Laurent81784c32012-11-19 14:55:58 -08002195{
2196 Track::stop();
2197 clearBufferQueue();
2198 mOutBuffer.frameCount = 0;
2199 mActive = false;
2200}
2201
Andy Hung8d31fd22023-06-26 19:20:57 -07002202ssize_t OutputTrack::write(void* data, uint32_t frames)
Eric Laurent81784c32012-11-19 14:55:58 -08002203{
Eric Laurent19952e12023-04-20 10:08:29 +02002204 if (!mActive && frames != 0) {
Andy Hung87c693c2023-07-06 20:56:16 -07002205 const sp<IAfThreadBase> thread = mThread.promote();
Andy Hung440901d2023-06-29 21:19:25 -07002206 if (thread != nullptr && thread->inStandby()) {
Eric Laurent19952e12023-04-20 10:08:29 +02002207 // preload one silent buffer to trigger mixer on start()
2208 ClientProxy::Buffer buf { .mFrameCount = mClientProxy->getStartThresholdInFrames() };
2209 status_t status = mClientProxy->obtainBuffer(&buf);
2210 if (status != NO_ERROR && status != NOT_ENOUGH_DATA && status != WOULD_BLOCK) {
2211 ALOGE("%s(%d): could not obtain buffer on start", __func__, mId);
2212 return 0;
2213 }
2214 memset(buf.mRaw, 0, buf.mFrameCount * mFrameSize);
2215 mClientProxy->releaseBuffer(&buf);
2216
2217 (void) start();
2218
2219 // wait for HAL stream to start before sending actual audio. Doing this on each
2220 // OutputTrack makes that playback start on all output streams is synchronized.
2221 // If another OutputTrack has already started it can underrun but this is OK
2222 // as only silence has been played so far and the retry count is very high on
2223 // OutputTrack.
Andy Hung87c693c2023-07-06 20:56:16 -07002224 auto* const pt = thread->asIAfPlaybackThread().get();
Eric Laurent19952e12023-04-20 10:08:29 +02002225 if (!pt->waitForHalStart()) {
2226 ALOGW("%s(%d): timeout waiting for thread to exit standby", __func__, mId);
2227 stop();
2228 return 0;
2229 }
2230
2231 // enqueue the first buffer and exit so that other OutputTracks will also start before
2232 // write() is called again and this buffer actually consumed.
2233 Buffer firstBuffer;
2234 firstBuffer.frameCount = frames;
2235 firstBuffer.raw = data;
2236 queueBuffer(firstBuffer);
2237 return frames;
2238 } else {
2239 (void) start();
2240 }
2241 }
2242
Eric Laurent81784c32012-11-19 14:55:58 -08002243 Buffer *pInBuffer;
2244 Buffer inBuffer;
Eric Laurent81784c32012-11-19 14:55:58 -08002245 inBuffer.frameCount = frames;
Andy Hungc25b84a2015-01-14 19:04:10 -08002246 inBuffer.raw = data;
Eric Laurent81784c32012-11-19 14:55:58 -08002247 uint32_t waitTimeLeftMs = mSourceThread->waitTimeMs();
Eric Laurent81784c32012-11-19 14:55:58 -08002248 while (waitTimeLeftMs) {
2249 // First write pending buffers, then new data
2250 if (mBufferQueue.size()) {
2251 pInBuffer = mBufferQueue.itemAt(0);
2252 } else {
2253 pInBuffer = &inBuffer;
2254 }
2255
2256 if (pInBuffer->frameCount == 0) {
2257 break;
2258 }
2259
2260 if (mOutBuffer.frameCount == 0) {
2261 mOutBuffer.frameCount = pInBuffer->frameCount;
2262 nsecs_t startTime = systemTime();
Glenn Kasten9f80dd22012-12-18 15:57:32 -08002263 status_t status = obtainBuffer(&mOutBuffer, waitTimeLeftMs);
Eric Laurent4d231dc2016-03-11 18:38:23 -08002264 if (status != NO_ERROR && status != NOT_ENOUGH_DATA) {
Andy Hung9d84af52018-09-12 18:03:44 -07002265 ALOGV("%s(%d): thread %d no more output buffers; status %d",
2266 __func__, mId,
2267 (int)mThreadIoHandle, status);
Eric Laurent81784c32012-11-19 14:55:58 -08002268 break;
2269 }
2270 uint32_t waitTimeMs = (uint32_t)ns2ms(systemTime() - startTime);
2271 if (waitTimeLeftMs >= waitTimeMs) {
2272 waitTimeLeftMs -= waitTimeMs;
2273 } else {
2274 waitTimeLeftMs = 0;
2275 }
Eric Laurent4d231dc2016-03-11 18:38:23 -08002276 if (status == NOT_ENOUGH_DATA) {
2277 restartIfDisabled();
2278 continue;
2279 }
Eric Laurent81784c32012-11-19 14:55:58 -08002280 }
2281
2282 uint32_t outFrames = pInBuffer->frameCount > mOutBuffer.frameCount ? mOutBuffer.frameCount :
2283 pInBuffer->frameCount;
Andy Hungc25b84a2015-01-14 19:04:10 -08002284 memcpy(mOutBuffer.raw, pInBuffer->raw, outFrames * mFrameSize);
Glenn Kasten9f80dd22012-12-18 15:57:32 -08002285 Proxy::Buffer buf;
2286 buf.mFrameCount = outFrames;
2287 buf.mRaw = NULL;
2288 mClientProxy->releaseBuffer(&buf);
Eric Laurent4d231dc2016-03-11 18:38:23 -08002289 restartIfDisabled();
Eric Laurent81784c32012-11-19 14:55:58 -08002290 pInBuffer->frameCount -= outFrames;
Andy Hungc25b84a2015-01-14 19:04:10 -08002291 pInBuffer->raw = (int8_t *)pInBuffer->raw + outFrames * mFrameSize;
Eric Laurent81784c32012-11-19 14:55:58 -08002292 mOutBuffer.frameCount -= outFrames;
Andy Hungc25b84a2015-01-14 19:04:10 -08002293 mOutBuffer.raw = (int8_t *)mOutBuffer.raw + outFrames * mFrameSize;
Eric Laurent81784c32012-11-19 14:55:58 -08002294
2295 if (pInBuffer->frameCount == 0) {
2296 if (mBufferQueue.size()) {
2297 mBufferQueue.removeAt(0);
Andy Hungc25b84a2015-01-14 19:04:10 -08002298 free(pInBuffer->mBuffer);
Yunlian Jiang8adc8082017-06-06 15:59:44 -07002299 if (pInBuffer != &inBuffer) {
2300 delete pInBuffer;
2301 }
Andy Hung9d84af52018-09-12 18:03:44 -07002302 ALOGV("%s(%d): thread %d released overflow buffer %zu",
2303 __func__, mId,
2304 (int)mThreadIoHandle, mBufferQueue.size());
Eric Laurent81784c32012-11-19 14:55:58 -08002305 } else {
2306 break;
2307 }
2308 }
2309 }
2310
2311 // If we could not write all frames, allocate a buffer and queue it for next time.
2312 if (inBuffer.frameCount) {
Andy Hung87c693c2023-07-06 20:56:16 -07002313 const sp<IAfThreadBase> thread = mThread.promote();
Andy Hung440901d2023-06-29 21:19:25 -07002314 if (thread != nullptr && !thread->inStandby()) {
Eric Laurent19952e12023-04-20 10:08:29 +02002315 queueBuffer(inBuffer);
Eric Laurent81784c32012-11-19 14:55:58 -08002316 }
2317 }
2318
Andy Hungc25b84a2015-01-14 19:04:10 -08002319 // Calling write() with a 0 length buffer means that no more data will be written:
2320 // We rely on stop() to set the appropriate flags to allow the remaining frames to play out.
2321 if (frames == 0 && mBufferQueue.size() == 0 && mActive) {
2322 stop();
Eric Laurent81784c32012-11-19 14:55:58 -08002323 }
2324
Andy Hung1c86ebe2018-05-29 20:29:08 -07002325 return frames - inBuffer.frameCount; // number of frames consumed.
Eric Laurent81784c32012-11-19 14:55:58 -08002326}
2327
Andy Hung8d31fd22023-06-26 19:20:57 -07002328void OutputTrack::queueBuffer(Buffer& inBuffer) {
Eric Laurent19952e12023-04-20 10:08:29 +02002329
2330 if (mBufferQueue.size() < kMaxOverFlowBuffers) {
2331 Buffer *pInBuffer = new Buffer;
2332 const size_t bufferSize = inBuffer.frameCount * mFrameSize;
2333 pInBuffer->mBuffer = malloc(bufferSize);
2334 LOG_ALWAYS_FATAL_IF(pInBuffer->mBuffer == nullptr,
2335 "%s: Unable to malloc size %zu", __func__, bufferSize);
2336 pInBuffer->frameCount = inBuffer.frameCount;
2337 pInBuffer->raw = pInBuffer->mBuffer;
2338 memcpy(pInBuffer->raw, inBuffer.raw, inBuffer.frameCount * mFrameSize);
2339 mBufferQueue.add(pInBuffer);
2340 ALOGV("%s(%d): thread %d adding overflow buffer %zu", __func__, mId,
2341 (int)mThreadIoHandle, mBufferQueue.size());
2342 // audio data is consumed (stored locally); set frameCount to 0.
2343 inBuffer.frameCount = 0;
2344 } else {
2345 ALOGW("%s(%d): thread %d no more overflow buffers",
2346 __func__, mId, (int)mThreadIoHandle);
2347 // TODO: return error for this.
2348 }
2349}
2350
Andy Hung8d31fd22023-06-26 19:20:57 -07002351void OutputTrack::copyMetadataTo(MetadataInserter& backInserter) const
Kevin Rocard12381092018-04-11 09:19:59 -07002352{
2353 std::lock_guard<std::mutex> lock(mTrackMetadatasMutex);
2354 backInserter = std::copy(mTrackMetadatas.begin(), mTrackMetadatas.end(), backInserter);
2355}
2356
Andy Hung8d31fd22023-06-26 19:20:57 -07002357void OutputTrack::setMetadatas(const SourceMetadatas& metadatas) {
Kevin Rocard12381092018-04-11 09:19:59 -07002358 {
2359 std::lock_guard<std::mutex> lock(mTrackMetadatasMutex);
2360 mTrackMetadatas = metadatas;
2361 }
2362 // No need to adjust metadata track volumes as OutputTrack volumes are always 0dBFS.
2363 setMetadataHasChanged();
2364}
2365
Andy Hung8d31fd22023-06-26 19:20:57 -07002366status_t OutputTrack::obtainBuffer(
Eric Laurent81784c32012-11-19 14:55:58 -08002367 AudioBufferProvider::Buffer* buffer, uint32_t waitTimeMs)
2368{
Glenn Kasten9f80dd22012-12-18 15:57:32 -08002369 ClientProxy::Buffer buf;
2370 buf.mFrameCount = buffer->frameCount;
2371 struct timespec timeout;
2372 timeout.tv_sec = waitTimeMs / 1000;
2373 timeout.tv_nsec = (int) (waitTimeMs % 1000) * 1000000;
2374 status_t status = mClientProxy->obtainBuffer(&buf, &timeout);
2375 buffer->frameCount = buf.mFrameCount;
2376 buffer->raw = buf.mRaw;
2377 return status;
Eric Laurent81784c32012-11-19 14:55:58 -08002378}
2379
Andy Hung8d31fd22023-06-26 19:20:57 -07002380void OutputTrack::clearBufferQueue()
Eric Laurent81784c32012-11-19 14:55:58 -08002381{
2382 size_t size = mBufferQueue.size();
2383
2384 for (size_t i = 0; i < size; i++) {
2385 Buffer *pBuffer = mBufferQueue.itemAt(i);
Andy Hungc25b84a2015-01-14 19:04:10 -08002386 free(pBuffer->mBuffer);
Eric Laurent81784c32012-11-19 14:55:58 -08002387 delete pBuffer;
2388 }
2389 mBufferQueue.clear();
2390}
2391
Andy Hung8d31fd22023-06-26 19:20:57 -07002392void OutputTrack::restartIfDisabled()
Eric Laurent4d231dc2016-03-11 18:38:23 -08002393{
2394 int32_t flags = android_atomic_and(~CBLK_DISABLED, &mCblk->mFlags);
2395 if (mActive && (flags & CBLK_DISABLED)) {
2396 start();
2397 }
2398}
Eric Laurent81784c32012-11-19 14:55:58 -08002399
Andy Hung9d84af52018-09-12 18:03:44 -07002400// ----------------------------------------------------------------------------
2401#undef LOG_TAG
2402#define LOG_TAG "AF::PatchTrack"
2403
Andy Hung8d31fd22023-06-26 19:20:57 -07002404/* static */
2405sp<IAfPatchTrack> IAfPatchTrack::create(
Andy Hung87c693c2023-07-06 20:56:16 -07002406 IAfPlaybackThread* playbackThread,
Andy Hung8d31fd22023-06-26 19:20:57 -07002407 audio_stream_type_t streamType,
2408 uint32_t sampleRate,
2409 audio_channel_mask_t channelMask,
2410 audio_format_t format,
2411 size_t frameCount,
2412 void* buffer,
2413 size_t bufferSize,
2414 audio_output_flags_t flags,
2415 const Timeout& timeout,
2416 size_t frameCountToBeReady /** Default behaviour is to start
2417 * as soon as possible to have
2418 * the lowest possible latency
2419 * even if it might glitch. */)
2420{
2421 return sp<PatchTrack>::make(
Andy Hung87c693c2023-07-06 20:56:16 -07002422 playbackThread,
Andy Hung8d31fd22023-06-26 19:20:57 -07002423 streamType,
2424 sampleRate,
2425 channelMask,
2426 format,
2427 frameCount,
2428 buffer,
2429 bufferSize,
2430 flags,
2431 timeout,
2432 frameCountToBeReady);
2433}
2434
Andy Hung87c693c2023-07-06 20:56:16 -07002435PatchTrack::PatchTrack(IAfPlaybackThread* playbackThread,
Eric Laurent3bcf8592015-04-03 12:13:24 -07002436 audio_stream_type_t streamType,
Eric Laurent83b88082014-06-20 18:31:16 -07002437 uint32_t sampleRate,
2438 audio_channel_mask_t channelMask,
2439 audio_format_t format,
2440 size_t frameCount,
2441 void *buffer,
Andy Hung8fe68032017-06-05 16:17:51 -07002442 size_t bufferSize,
Kevin Rocard45986c72018-12-18 18:22:59 -08002443 audio_output_flags_t flags,
Kevin Rocard01c7d9e2019-09-18 11:24:52 +01002444 const Timeout& timeout,
2445 size_t frameCountToBeReady)
Eric Laurent3bcf8592015-04-03 12:13:24 -07002446 : Track(playbackThread, NULL, streamType,
Kevin Rocard1f564ac2018-03-29 13:53:10 -07002447 audio_attributes_t{} /* currently unused for patch track */,
Eric Laurent223fd5c2014-11-11 13:43:36 -08002448 sampleRate, format, channelMask, frameCount,
Andy Hung8fe68032017-06-05 16:17:51 -07002449 buffer, bufferSize, nullptr /* sharedBuffer */,
Svet Ganov33761132021-05-13 22:51:08 +00002450 AUDIO_SESSION_NONE, getpid(), audioServerAttributionSource(getpid()), flags,
Philip P. Moltmannbda45752020-07-17 16:41:18 -07002451 TYPE_PATCH, AUDIO_PORT_HANDLE_NONE, frameCountToBeReady),
gaoxiupei8e3a5682023-07-07 20:30:23 +08002452 PatchTrackBase(mCblk ? new ClientProxy(mCblk, mBuffer, frameCount, mFrameSize, true, true)
2453 : nullptr,
Kevin Rocard45986c72018-12-18 18:22:59 -08002454 *playbackThread, timeout)
Eric Laurent83b88082014-06-20 18:31:16 -07002455{
Andy Hung9d84af52018-09-12 18:03:44 -07002456 ALOGV("%s(%d): sampleRate %d mPeerTimeout %d.%03d sec",
2457 __func__, mId, sampleRate,
Eric Laurent83b88082014-06-20 18:31:16 -07002458 (int)mPeerTimeout.tv_sec,
2459 (int)(mPeerTimeout.tv_nsec / 1000000));
2460}
2461
Andy Hung8d31fd22023-06-26 19:20:57 -07002462PatchTrack::~PatchTrack()
Eric Laurent83b88082014-06-20 18:31:16 -07002463{
Andy Hungabfab202019-03-07 19:45:54 -08002464 ALOGV("%s(%d)", __func__, mId);
Eric Laurent83b88082014-06-20 18:31:16 -07002465}
2466
Andy Hung8d31fd22023-06-26 19:20:57 -07002467size_t PatchTrack::framesReady() const
Mikhail Naganovcaf59942019-09-25 14:05:29 -07002468{
2469 if (mPeerProxy && mPeerProxy->producesBufferOnDemand()) {
2470 return std::numeric_limits<size_t>::max();
2471 } else {
2472 return Track::framesReady();
2473 }
2474}
2475
Andy Hung8d31fd22023-06-26 19:20:57 -07002476status_t PatchTrack::start(AudioSystem::sync_event_t event,
Kevin Rocard5f2136e2018-05-11 22:03:00 -07002477 audio_session_t triggerSession)
Eric Laurent4d231dc2016-03-11 18:38:23 -08002478{
2479 status_t status = Track::start(event, triggerSession);
2480 if (status != NO_ERROR) {
2481 return status;
2482 }
2483 android_atomic_and(~CBLK_DISABLED, &mCblk->mFlags);
2484 return status;
2485}
2486
Eric Laurent83b88082014-06-20 18:31:16 -07002487// AudioBufferProvider interface
Andy Hung8d31fd22023-06-26 19:20:57 -07002488status_t PatchTrack::getNextBuffer(
Glenn Kastend79072e2016-01-06 08:41:20 -08002489 AudioBufferProvider::Buffer* buffer)
Eric Laurent83b88082014-06-20 18:31:16 -07002490{
Andy Hung9d84af52018-09-12 18:03:44 -07002491 ALOG_ASSERT(mPeerProxy != 0, "%s(%d): called without peer proxy", __func__, mId);
Eric Laurent83b88082014-06-20 18:31:16 -07002492 Proxy::Buffer buf;
2493 buf.mFrameCount = buffer->frameCount;
Mikhail Naganov938be412019-09-04 11:38:47 -07002494 if (ATRACE_ENABLED()) {
2495 std::string traceName("PTnReq");
2496 traceName += std::to_string(id());
2497 ATRACE_INT(traceName.c_str(), buf.mFrameCount);
2498 }
Eric Laurent83b88082014-06-20 18:31:16 -07002499 status_t status = mPeerProxy->obtainBuffer(&buf, &mPeerTimeout);
Andy Hung9d84af52018-09-12 18:03:44 -07002500 ALOGV_IF(status != NO_ERROR, "%s(%d): getNextBuffer status %d", __func__, mId, status);
Eric Laurentc2730ba2014-07-20 15:47:07 -07002501 buffer->frameCount = buf.mFrameCount;
Mikhail Naganov938be412019-09-04 11:38:47 -07002502 if (ATRACE_ENABLED()) {
2503 std::string traceName("PTnObt");
2504 traceName += std::to_string(id());
2505 ATRACE_INT(traceName.c_str(), buf.mFrameCount);
2506 }
Eric Laurent83b88082014-06-20 18:31:16 -07002507 if (buf.mFrameCount == 0) {
2508 return WOULD_BLOCK;
2509 }
Glenn Kastend79072e2016-01-06 08:41:20 -08002510 status = Track::getNextBuffer(buffer);
Eric Laurent83b88082014-06-20 18:31:16 -07002511 return status;
2512}
2513
Andy Hung8d31fd22023-06-26 19:20:57 -07002514void PatchTrack::releaseBuffer(AudioBufferProvider::Buffer* buffer)
Eric Laurent83b88082014-06-20 18:31:16 -07002515{
Andy Hung9d84af52018-09-12 18:03:44 -07002516 ALOG_ASSERT(mPeerProxy != 0, "%s(%d): called without peer proxy", __func__, mId);
Eric Laurent83b88082014-06-20 18:31:16 -07002517 Proxy::Buffer buf;
2518 buf.mFrameCount = buffer->frameCount;
2519 buf.mRaw = buffer->raw;
2520 mPeerProxy->releaseBuffer(&buf);
Andy Hung920f6572022-10-06 12:09:49 -07002521 TrackBase::releaseBuffer(buffer); // Note: this is the base class.
Eric Laurent83b88082014-06-20 18:31:16 -07002522}
2523
Andy Hung8d31fd22023-06-26 19:20:57 -07002524status_t PatchTrack::obtainBuffer(Proxy::Buffer* buffer,
Eric Laurent83b88082014-06-20 18:31:16 -07002525 const struct timespec *timeOut)
2526{
Eric Laurent4d231dc2016-03-11 18:38:23 -08002527 status_t status = NO_ERROR;
2528 static const int32_t kMaxTries = 5;
2529 int32_t tryCounter = kMaxTries;
Andy Hungf62e1a22018-05-08 18:32:11 -07002530 const size_t originalFrameCount = buffer->mFrameCount;
Eric Laurent4d231dc2016-03-11 18:38:23 -08002531 do {
2532 if (status == NOT_ENOUGH_DATA) {
2533 restartIfDisabled();
Andy Hungf62e1a22018-05-08 18:32:11 -07002534 buffer->mFrameCount = originalFrameCount; // cleared on error, must be restored.
Eric Laurent4d231dc2016-03-11 18:38:23 -08002535 }
2536 status = mProxy->obtainBuffer(buffer, timeOut);
2537 } while ((status == NOT_ENOUGH_DATA) && (tryCounter-- > 0));
2538 return status;
Eric Laurent83b88082014-06-20 18:31:16 -07002539}
2540
Andy Hung8d31fd22023-06-26 19:20:57 -07002541void PatchTrack::releaseBuffer(Proxy::Buffer* buffer)
Eric Laurent83b88082014-06-20 18:31:16 -07002542{
2543 mProxy->releaseBuffer(buffer);
Eric Laurent4d231dc2016-03-11 18:38:23 -08002544 restartIfDisabled();
naoki miyazuf37f9982019-11-28 11:18:18 +09002545
2546 // Check if the PatchTrack has enough data to write once in releaseBuffer().
2547 // If not, prevent an underrun from occurring by moving the track into FS_FILLING;
2548 // this logic avoids glitches when suspending A2DP with AudioPlaybackCapture.
2549 // TODO: perhaps underrun avoidance could be a track property checked in isReady() instead.
Andy Hung8d31fd22023-06-26 19:20:57 -07002550 if (mFillingStatus == FS_ACTIVE
naoki miyazuf37f9982019-11-28 11:18:18 +09002551 && audio_is_linear_pcm(mFormat)
2552 && !isOffloadedOrDirect()) {
Andy Hung87c693c2023-07-06 20:56:16 -07002553 if (const sp<IAfThreadBase> thread = mThread.promote();
naoki miyazuf37f9982019-11-28 11:18:18 +09002554 thread != 0) {
Andy Hung87c693c2023-07-06 20:56:16 -07002555 auto* const playbackThread = thread->asIAfPlaybackThread().get();
naoki miyazuf37f9982019-11-28 11:18:18 +09002556 const size_t frameCount = playbackThread->frameCount() * sampleRate()
2557 / playbackThread->sampleRate();
2558 if (framesReady() < frameCount) {
2559 ALOGD("%s(%d) Not enough data, wait for buffer to fill", __func__, mId);
Andy Hung8d31fd22023-06-26 19:20:57 -07002560 mFillingStatus = FS_FILLING;
naoki miyazuf37f9982019-11-28 11:18:18 +09002561 }
2562 }
2563 }
Eric Laurent4d231dc2016-03-11 18:38:23 -08002564}
2565
Andy Hung8d31fd22023-06-26 19:20:57 -07002566void PatchTrack::restartIfDisabled()
Eric Laurent4d231dc2016-03-11 18:38:23 -08002567{
Eric Laurent83b88082014-06-20 18:31:16 -07002568 if (android_atomic_and(~CBLK_DISABLED, &mCblk->mFlags) & CBLK_DISABLED) {
Andy Hung9d84af52018-09-12 18:03:44 -07002569 ALOGW("%s(%d): disabled due to previous underrun, restarting", __func__, mId);
Eric Laurent83b88082014-06-20 18:31:16 -07002570 start();
2571 }
Eric Laurent83b88082014-06-20 18:31:16 -07002572}
2573
Eric Laurent81784c32012-11-19 14:55:58 -08002574// ----------------------------------------------------------------------------
2575// Record
2576// ----------------------------------------------------------------------------
Jean-Michel Trividdf87ef2019-08-20 15:42:04 -07002577
2578
Andy Hung9d84af52018-09-12 18:03:44 -07002579#undef LOG_TAG
2580#define LOG_TAG "AF::RecordHandle"
Eric Laurent81784c32012-11-19 14:55:58 -08002581
Andy Hunga5a7fc92023-06-23 19:27:19 -07002582class RecordHandle : public android::media::BnAudioRecord {
2583public:
Andy Hungd29af632023-06-23 19:27:19 -07002584 explicit RecordHandle(const sp<IAfRecordTrack>& recordTrack);
Andy Hunga5a7fc92023-06-23 19:27:19 -07002585 ~RecordHandle() override;
2586 binder::Status start(int /*AudioSystem::sync_event_t*/ event,
2587 int /*audio_session_t*/ triggerSession) final;
2588 binder::Status stop() final;
2589 binder::Status getActiveMicrophones(
2590 std::vector<media::MicrophoneInfoFw>* activeMicrophones) final;
2591 binder::Status setPreferredMicrophoneDirection(
2592 int /*audio_microphone_direction_t*/ direction) final;
2593 binder::Status setPreferredMicrophoneFieldDimension(float zoom) final;
2594 binder::Status shareAudioHistory(
2595 const std::string& sharedAudioPackageName, int64_t sharedAudioStartMs) final;
2596
2597private:
Andy Hungd29af632023-06-23 19:27:19 -07002598 const sp<IAfRecordTrack> mRecordTrack;
Andy Hunga5a7fc92023-06-23 19:27:19 -07002599
2600 // for use from destructor
2601 void stop_nonvirtual();
2602};
2603
2604/* static */
Andy Hungd29af632023-06-23 19:27:19 -07002605sp<media::IAudioRecord> IAfRecordTrack::createIAudioRecordAdapter(
2606 const sp<IAfRecordTrack>& recordTrack) {
Andy Hunga5a7fc92023-06-23 19:27:19 -07002607 return sp<RecordHandle>::make(recordTrack);
2608}
2609
2610RecordHandle::RecordHandle(
Andy Hungd29af632023-06-23 19:27:19 -07002611 const sp<IAfRecordTrack>& recordTrack)
Eric Laurent81784c32012-11-19 14:55:58 -08002612 : BnAudioRecord(),
2613 mRecordTrack(recordTrack)
2614{
Andy Hunga5a7fc92023-06-23 19:27:19 -07002615 // TODO(b/288339104) binder thread priority change not needed.
Andy Hung225aef62022-12-06 16:33:20 -08002616 setMinSchedulerPolicy(SCHED_NORMAL, ANDROID_PRIORITY_AUDIO);
Eric Laurent81784c32012-11-19 14:55:58 -08002617}
2618
Andy Hunga5a7fc92023-06-23 19:27:19 -07002619RecordHandle::~RecordHandle() {
Eric Laurent81784c32012-11-19 14:55:58 -08002620 stop_nonvirtual();
2621 mRecordTrack->destroy();
2622}
2623
Andy Hunga5a7fc92023-06-23 19:27:19 -07002624binder::Status RecordHandle::start(int /*AudioSystem::sync_event_t*/ event,
Ivan Lozanoff6900d2017-08-01 15:47:38 -07002625 int /*audio_session_t*/ triggerSession) {
Andy Hung9d84af52018-09-12 18:03:44 -07002626 ALOGV("%s()", __func__);
Andy Hung1131b6e2020-12-08 20:47:45 -08002627 return binderStatusFromStatusT(
Ivan Lozanoff6900d2017-08-01 15:47:38 -07002628 mRecordTrack->start((AudioSystem::sync_event_t)event, (audio_session_t) triggerSession));
Eric Laurent81784c32012-11-19 14:55:58 -08002629}
2630
Andy Hunga5a7fc92023-06-23 19:27:19 -07002631binder::Status RecordHandle::stop() {
Eric Laurent81784c32012-11-19 14:55:58 -08002632 stop_nonvirtual();
Ivan Lozanoff6900d2017-08-01 15:47:38 -07002633 return binder::Status::ok();
Eric Laurent81784c32012-11-19 14:55:58 -08002634}
2635
Andy Hunga5a7fc92023-06-23 19:27:19 -07002636void RecordHandle::stop_nonvirtual() {
Andy Hung9d84af52018-09-12 18:03:44 -07002637 ALOGV("%s()", __func__);
Eric Laurent81784c32012-11-19 14:55:58 -08002638 mRecordTrack->stop();
2639}
2640
Andy Hunga5a7fc92023-06-23 19:27:19 -07002641binder::Status RecordHandle::getActiveMicrophones(
Mikhail Naganovd5d9de72023-02-13 11:45:03 -08002642 std::vector<media::MicrophoneInfoFw>* activeMicrophones) {
Andy Hung9d84af52018-09-12 18:03:44 -07002643 ALOGV("%s()", __func__);
Mikhail Naganovd5d9de72023-02-13 11:45:03 -08002644 return binderStatusFromStatusT(mRecordTrack->getActiveMicrophones(activeMicrophones));
jiabin653cc0a2018-01-17 17:54:10 -08002645}
2646
Andy Hunga5a7fc92023-06-23 19:27:19 -07002647binder::Status RecordHandle::setPreferredMicrophoneDirection(
Paul McLean03a6e6a2018-12-04 10:54:13 -07002648 int /*audio_microphone_direction_t*/ direction) {
2649 ALOGV("%s()", __func__);
Andy Hung1131b6e2020-12-08 20:47:45 -08002650 return binderStatusFromStatusT(mRecordTrack->setPreferredMicrophoneDirection(
Paul McLean03a6e6a2018-12-04 10:54:13 -07002651 static_cast<audio_microphone_direction_t>(direction)));
2652}
2653
Andy Hunga5a7fc92023-06-23 19:27:19 -07002654binder::Status RecordHandle::setPreferredMicrophoneFieldDimension(float zoom) {
Paul McLean03a6e6a2018-12-04 10:54:13 -07002655 ALOGV("%s()", __func__);
Andy Hung1131b6e2020-12-08 20:47:45 -08002656 return binderStatusFromStatusT(mRecordTrack->setPreferredMicrophoneFieldDimension(zoom));
Paul McLean03a6e6a2018-12-04 10:54:13 -07002657}
2658
Andy Hunga5a7fc92023-06-23 19:27:19 -07002659binder::Status RecordHandle::shareAudioHistory(
Eric Laurentec376dc2021-04-08 20:41:22 +02002660 const std::string& sharedAudioPackageName, int64_t sharedAudioStartMs) {
2661 return binderStatusFromStatusT(
2662 mRecordTrack->shareAudioHistory(sharedAudioPackageName, sharedAudioStartMs));
2663}
2664
Eric Laurent81784c32012-11-19 14:55:58 -08002665// ----------------------------------------------------------------------------
Andy Hung9d84af52018-09-12 18:03:44 -07002666#undef LOG_TAG
2667#define LOG_TAG "AF::RecordTrack"
Eric Laurent81784c32012-11-19 14:55:58 -08002668
Andy Hung8d31fd22023-06-26 19:20:57 -07002669
2670/* static */ // TODO(b/288339104)
Andy Hung87c693c2023-07-06 20:56:16 -07002671sp<IAfRecordTrack> IAfRecordTrack::create(IAfRecordThread* thread,
Andy Hung8d31fd22023-06-26 19:20:57 -07002672 const sp<Client>& client,
2673 const audio_attributes_t& attr,
2674 uint32_t sampleRate,
2675 audio_format_t format,
2676 audio_channel_mask_t channelMask,
2677 size_t frameCount,
2678 void* buffer,
2679 size_t bufferSize,
2680 audio_session_t sessionId,
2681 pid_t creatorPid,
2682 const AttributionSourceState& attributionSource,
2683 audio_input_flags_t flags,
2684 track_type type,
2685 audio_port_handle_t portId,
2686 int32_t startFrames)
2687{
2688 return sp<RecordTrack>::make(
Andy Hung87c693c2023-07-06 20:56:16 -07002689 thread,
Andy Hung8d31fd22023-06-26 19:20:57 -07002690 client,
2691 attr,
2692 sampleRate,
2693 format,
2694 channelMask,
2695 frameCount,
2696 buffer,
2697 bufferSize,
2698 sessionId,
2699 creatorPid,
2700 attributionSource,
2701 flags,
2702 type,
2703 portId,
2704 startFrames);
2705}
2706
Glenn Kasten05997e22014-03-13 15:08:33 -07002707// RecordTrack constructor must be called with AudioFlinger::mLock and ThreadBase::mLock held
Andy Hung8d31fd22023-06-26 19:20:57 -07002708RecordTrack::RecordTrack(
Andy Hung87c693c2023-07-06 20:56:16 -07002709 IAfRecordThread* thread,
Eric Laurent81784c32012-11-19 14:55:58 -08002710 const sp<Client>& client,
Kevin Rocard1f564ac2018-03-29 13:53:10 -07002711 const audio_attributes_t& attr,
Eric Laurent81784c32012-11-19 14:55:58 -08002712 uint32_t sampleRate,
2713 audio_format_t format,
2714 audio_channel_mask_t channelMask,
2715 size_t frameCount,
Eric Laurent83b88082014-06-20 18:31:16 -07002716 void *buffer,
Andy Hung8fe68032017-06-05 16:17:51 -07002717 size_t bufferSize,
Glenn Kastend848eb42016-03-08 13:42:11 -08002718 audio_session_t sessionId,
Eric Laurent09f1ed22019-04-24 17:45:17 -07002719 pid_t creatorPid,
Svet Ganov33761132021-05-13 22:51:08 +00002720 const AttributionSourceState& attributionSource,
Eric Laurent05067782016-06-01 18:27:28 -07002721 audio_input_flags_t flags,
Eric Laurent20b9ef02016-12-05 11:03:16 -08002722 track_type type,
Eric Laurentec376dc2021-04-08 20:41:22 +02002723 audio_port_handle_t portId,
Eric Laurent2407ce32021-04-26 14:56:03 +02002724 int32_t startFrames)
Kevin Rocard1f564ac2018-03-29 13:53:10 -07002725 : TrackBase(thread, client, attr, sampleRate, format,
Eric Laurent09f1ed22019-04-24 17:45:17 -07002726 channelMask, frameCount, buffer, bufferSize, sessionId,
Philip P. Moltmannbda45752020-07-17 16:41:18 -07002727 creatorPid,
Svet Ganov33761132021-05-13 22:51:08 +00002728 VALUE_OR_FATAL(aidl2legacy_int32_t_uid_t(attributionSource.uid)),
Philip P. Moltmannbda45752020-07-17 16:41:18 -07002729 false /*isOut*/,
Eric Laurent83b88082014-06-20 18:31:16 -07002730 (type == TYPE_DEFAULT) ?
Eric Laurent05067782016-06-01 18:27:28 -07002731 ((flags & AUDIO_INPUT_FLAG_FAST) ? ALLOC_PIPE : ALLOC_CBLK) :
Eric Laurent83b88082014-06-20 18:31:16 -07002732 ((buffer == NULL) ? ALLOC_LOCAL : ALLOC_NONE),
Andy Hungb68f5eb2019-12-03 16:49:17 -08002733 type, portId,
2734 std::string(AMEDIAMETRICS_KEY_PREFIX_AUDIO_RECORD) + std::to_string(portId)),
Andy Hung97a893e2015-03-29 01:03:07 -07002735 mOverflow(false),
Andy Hung4c6afaf2015-06-12 18:23:35 -07002736 mResamplerBufferProvider(NULL), // initialize in case of early constructor exit
Eric Laurent05067782016-06-01 18:27:28 -07002737 mRecordBufferConverter(NULL),
jiabin9378eb92018-05-02 15:26:35 -07002738 mFlags(flags),
Jean-Michel Trividdf87ef2019-08-20 15:42:04 -07002739 mSilenced(false),
Eric Laurent2407ce32021-04-26 14:56:03 +02002740 mStartFrames(startFrames)
Eric Laurent81784c32012-11-19 14:55:58 -08002741{
Glenn Kasten3ef14ef2014-03-13 15:08:51 -07002742 if (mCblk == NULL) {
2743 return;
Glenn Kasten9f80dd22012-12-18 15:57:32 -08002744 }
Glenn Kasten6dd62fb2013-12-05 16:35:58 -08002745
Mikhail Naganov7c6ae982018-06-14 12:33:38 -07002746 if (!isDirect()) {
2747 mRecordBufferConverter = new RecordBufferConverter(
Andy Hung87c693c2023-07-06 20:56:16 -07002748 thread->channelMask(), thread->format(), thread->sampleRate(),
Mikhail Naganov7c6ae982018-06-14 12:33:38 -07002749 channelMask, format, sampleRate);
2750 // Check if the RecordBufferConverter construction was successful.
2751 // If not, don't continue with construction.
2752 //
2753 // NOTE: It would be extremely rare that the record track cannot be created
2754 // for the current device, but a pending or future device change would make
2755 // the record track configuration valid.
2756 if (mRecordBufferConverter->initCheck() != NO_ERROR) {
Andy Hung9d84af52018-09-12 18:03:44 -07002757 ALOGE("%s(%d): RecordTrack unable to create record buffer converter", __func__, mId);
Mikhail Naganov7c6ae982018-06-14 12:33:38 -07002758 return;
2759 }
Andy Hung97a893e2015-03-29 01:03:07 -07002760 }
2761
Andy Hung6ae58432016-02-16 18:32:24 -08002762 mServerProxy = new AudioRecordServerProxy(mCblk, mBuffer, frameCount,
Andy Hung3f0c9022016-01-15 17:49:46 -08002763 mFrameSize, !isExternalTrack());
Andy Hung3f0c9022016-01-15 17:49:46 -08002764
Andy Hung97a893e2015-03-29 01:03:07 -07002765 mResamplerBufferProvider = new ResamplerBufferProvider(this);
Glenn Kastenc263ca02014-06-04 20:31:46 -07002766
Eric Laurent05067782016-06-01 18:27:28 -07002767 if (flags & AUDIO_INPUT_FLAG_FAST) {
Andy Hung87c693c2023-07-06 20:56:16 -07002768 ALOG_ASSERT(thread->fastTrackAvailable());
2769 thread->setFastTrackAvailable(false);
Andy Hung000adb52018-06-01 15:43:26 -07002770 } else {
2771 // TODO: only Normal Record has timestamps (Fast Record does not).
Andy Hung5d3d9562018-10-04 19:27:26 -07002772 mServerLatencySupported = checkServerLatencySupported(mFormat, flags);
Glenn Kastenc263ca02014-06-04 20:31:46 -07002773 }
Andy Hung8946a282018-04-19 20:04:56 -07002774#ifdef TEE_SINK
2775 mTee.setId(std::string("_") + std::to_string(mThreadIoHandle)
2776 + "_" + std::to_string(mId)
2777 + "_R");
2778#endif
Andy Hungb68f5eb2019-12-03 16:49:17 -08002779
2780 // Once this item is logged by the server, the client can add properties.
Andy Hung94235282021-03-24 15:50:14 -07002781 mTrackMetrics.logConstructor(creatorPid, uid(), id());
Eric Laurent81784c32012-11-19 14:55:58 -08002782}
2783
Andy Hung8d31fd22023-06-26 19:20:57 -07002784RecordTrack::~RecordTrack()
Eric Laurent81784c32012-11-19 14:55:58 -08002785{
Andy Hung9d84af52018-09-12 18:03:44 -07002786 ALOGV("%s()", __func__);
Andy Hung97a893e2015-03-29 01:03:07 -07002787 delete mRecordBufferConverter;
Glenn Kasten6dd62fb2013-12-05 16:35:58 -08002788 delete mResamplerBufferProvider;
Eric Laurent81784c32012-11-19 14:55:58 -08002789}
2790
Andy Hung8d31fd22023-06-26 19:20:57 -07002791status_t RecordTrack::initCheck() const
Andy Hung97a893e2015-03-29 01:03:07 -07002792{
2793 status_t status = TrackBase::initCheck();
2794 if (status == NO_ERROR && mServerProxy == 0) {
2795 status = BAD_VALUE;
2796 }
2797 return status;
2798}
2799
Eric Laurent81784c32012-11-19 14:55:58 -08002800// AudioBufferProvider interface
Andy Hung8d31fd22023-06-26 19:20:57 -07002801status_t RecordTrack::getNextBuffer(AudioBufferProvider::Buffer* buffer)
Eric Laurent81784c32012-11-19 14:55:58 -08002802{
Glenn Kasten9f80dd22012-12-18 15:57:32 -08002803 ServerProxy::Buffer buf;
2804 buf.mFrameCount = buffer->frameCount;
2805 status_t status = mServerProxy->obtainBuffer(&buf);
2806 buffer->frameCount = buf.mFrameCount;
2807 buffer->raw = buf.mRaw;
2808 if (buf.mFrameCount == 0) {
2809 // FIXME also wake futex so that overrun is noticed more quickly
Glenn Kasten96f60d82013-07-12 10:21:18 -07002810 (void) android_atomic_or(CBLK_OVERRUN, &mCblk->mFlags);
Eric Laurent81784c32012-11-19 14:55:58 -08002811 }
Glenn Kasten9f80dd22012-12-18 15:57:32 -08002812 return status;
Eric Laurent81784c32012-11-19 14:55:58 -08002813}
2814
Andy Hung8d31fd22023-06-26 19:20:57 -07002815status_t RecordTrack::start(AudioSystem::sync_event_t event,
Glenn Kastend848eb42016-03-08 13:42:11 -08002816 audio_session_t triggerSession)
Eric Laurent81784c32012-11-19 14:55:58 -08002817{
Andy Hung87c693c2023-07-06 20:56:16 -07002818 const sp<IAfThreadBase> thread = mThread.promote();
Eric Laurent81784c32012-11-19 14:55:58 -08002819 if (thread != 0) {
Andy Hung87c693c2023-07-06 20:56:16 -07002820 auto* const recordThread = thread->asIAfRecordThread().get();
Eric Laurent81784c32012-11-19 14:55:58 -08002821 return recordThread->start(this, event, triggerSession);
2822 } else {
Eric Laurentd52a28c2020-08-21 17:10:39 -07002823 ALOGW("%s track %d: thread was destroyed", __func__, portId());
2824 return DEAD_OBJECT;
Eric Laurent81784c32012-11-19 14:55:58 -08002825 }
2826}
2827
Andy Hung8d31fd22023-06-26 19:20:57 -07002828void RecordTrack::stop()
Eric Laurent81784c32012-11-19 14:55:58 -08002829{
Andy Hung87c693c2023-07-06 20:56:16 -07002830 const sp<IAfThreadBase> thread = mThread.promote();
Eric Laurent81784c32012-11-19 14:55:58 -08002831 if (thread != 0) {
Andy Hung87c693c2023-07-06 20:56:16 -07002832 auto* const recordThread = thread->asIAfRecordThread().get();
Eric Laurent83b88082014-06-20 18:31:16 -07002833 if (recordThread->stop(this) && isExternalTrack()) {
Eric Laurentfee19762018-01-29 18:44:13 -08002834 AudioSystem::stopInput(mPortId);
Eric Laurent81784c32012-11-19 14:55:58 -08002835 }
2836 }
2837}
2838
Andy Hung8d31fd22023-06-26 19:20:57 -07002839void RecordTrack::destroy()
Eric Laurent81784c32012-11-19 14:55:58 -08002840{
Andy Hung8d31fd22023-06-26 19:20:57 -07002841 // see comments at Track::destroy()
Eric Laurent81784c32012-11-19 14:55:58 -08002842 sp<RecordTrack> keep(this);
2843 {
Andy Hungce685402018-10-05 17:23:27 -07002844 track_state priorState = mState;
Andy Hung87c693c2023-07-06 20:56:16 -07002845 const sp<IAfThreadBase> thread = mThread.promote();
Eric Laurent81784c32012-11-19 14:55:58 -08002846 if (thread != 0) {
Andy Hung87c693c2023-07-06 20:56:16 -07002847 Mutex::Autolock _l(thread->mutex());
2848 auto* const recordThread = thread->asIAfRecordThread().get();
Andy Hungce685402018-10-05 17:23:27 -07002849 priorState = mState;
Eric Laurentec376dc2021-04-08 20:41:22 +02002850 if (!mSharedAudioPackageName.empty()) {
Eric Laurent92d0a322021-07-16 15:32:33 +02002851 recordThread->resetAudioHistory_l();
Eric Laurentec376dc2021-04-08 20:41:22 +02002852 }
Andy Hungce685402018-10-05 17:23:27 -07002853 recordThread->destroyTrack_l(this); // move mState to STOPPED, terminate
2854 }
2855 // APM portid/client management done outside of lock.
2856 // NOTE: if thread doesn't exist, the input descriptor probably doesn't either.
2857 if (isExternalTrack()) {
2858 switch (priorState) {
2859 case ACTIVE: // invalidated while still active
2860 case STARTING_2: // invalidated/start-aborted after startInput successfully called
2861 case PAUSING: // invalidated while in the middle of stop() pausing (still active)
2862 AudioSystem::stopInput(mPortId);
2863 break;
2864
2865 case STARTING_1: // invalidated/start-aborted and startInput not successful
2866 case PAUSED: // OK, not active
2867 case IDLE: // OK, not active
2868 break;
2869
2870 case STOPPED: // unexpected (destroyed)
2871 default:
2872 LOG_ALWAYS_FATAL("%s(%d): invalid prior state: %d", __func__, mId, priorState);
2873 }
2874 AudioSystem::releaseInput(mPortId);
Eric Laurent81784c32012-11-19 14:55:58 -08002875 }
2876 }
2877}
2878
Andy Hung8d31fd22023-06-26 19:20:57 -07002879void RecordTrack::invalidate()
Eric Laurent9a54bc22013-09-09 09:08:44 -07002880{
Eric Laurent6acd1d42017-01-04 14:23:29 -08002881 TrackBase::invalidate();
Eric Laurent9a54bc22013-09-09 09:08:44 -07002882 // FIXME should use proxy, and needs work
2883 audio_track_cblk_t* cblk = mCblk;
2884 android_atomic_or(CBLK_INVALID, &cblk->mFlags);
2885 android_atomic_release_store(0x40000000, &cblk->mFutex);
2886 // client is not in server, so FUTEX_WAKE is needed instead of FUTEX_WAKE_PRIVATE
Elliott Hughesee499292014-05-21 17:55:51 -07002887 (void) syscall(__NR_futex, &cblk->mFutex, FUTEX_WAKE, INT_MAX);
Eric Laurent9a54bc22013-09-09 09:08:44 -07002888}
2889
Eric Laurent81784c32012-11-19 14:55:58 -08002890
Andy Hung8d31fd22023-06-26 19:20:57 -07002891void RecordTrack::appendDumpHeader(String8& result) const
Eric Laurent81784c32012-11-19 14:55:58 -08002892{
Eric Laurent973db022018-11-20 14:54:31 -08002893 result.appendFormat("Active Id Client Session Port Id S Flags "
Andy Hung9d84af52018-09-12 18:03:44 -07002894 " Format Chn mask SRate Source "
Kevin Rocard5f2136e2018-05-11 22:03:00 -07002895 " Server FrmCnt FrmRdy Sil%s\n",
2896 isServerLatencySupported() ? " Latency" : "");
Eric Laurent81784c32012-11-19 14:55:58 -08002897}
2898
Andy Hung8d31fd22023-06-26 19:20:57 -07002899void RecordTrack::appendDump(String8& result, bool active) const
Eric Laurent81784c32012-11-19 14:55:58 -08002900{
Eric Laurent973db022018-11-20 14:54:31 -08002901 result.appendFormat("%c%5s %6d %6u %7u %7u %2s 0x%03X "
Kevin Rocard5f2136e2018-05-11 22:03:00 -07002902 "%08X %08X %6u %6X "
Andy Hung000adb52018-06-01 15:43:26 -07002903 "%08X %6zu %6zu %3c",
Andy Hung2c6c3bb2017-06-16 14:01:45 -07002904 isFastTrack() ? 'F' : ' ',
Marco Nelissenb2208842014-02-07 14:00:50 -08002905 active ? "yes" : "no",
Andy Hung9d84af52018-09-12 18:03:44 -07002906 mId,
Andy Hung4ef19fa2018-05-15 19:35:29 -07002907 (mClient == 0) ? getpid() : mClient->pid(),
Andy Hung2c6c3bb2017-06-16 14:01:45 -07002908 mSessionId,
Eric Laurent973db022018-11-20 14:54:31 -08002909 mPortId,
Andy Hunge2e830f2019-12-03 12:54:46 -08002910 getTrackStateAsCodedString(),
Andy Hung2c6c3bb2017-06-16 14:01:45 -07002911 mCblk->mFlags,
2912
Eric Laurent81784c32012-11-19 14:55:58 -08002913 mFormat,
2914 mChannelMask,
Andy Hung2c6c3bb2017-06-16 14:01:45 -07002915 mSampleRate,
Kevin Rocard5f2136e2018-05-11 22:03:00 -07002916 mAttr.source,
Glenn Kasten6dd62fb2013-12-05 16:35:58 -08002917
Andy Hung2c6c3bb2017-06-16 14:01:45 -07002918 mCblk->mServer,
Jean-Michel Trivi7d665ab2018-04-11 17:26:51 -07002919 mFrameCount,
Andy Hung000adb52018-06-01 15:43:26 -07002920 mServerProxy->framesReadySafe(),
Jean-Michel Trivi7d665ab2018-04-11 17:26:51 -07002921 isSilenced() ? 's' : 'n'
Andy Hung2c6c3bb2017-06-16 14:01:45 -07002922 );
Andy Hung000adb52018-06-01 15:43:26 -07002923 if (isServerLatencySupported()) {
2924 double latencyMs;
2925 bool fromTrack;
2926 if (getTrackLatencyMs(&latencyMs, &fromTrack) == OK) {
2927 // Show latency in msec, followed by 't' if from track timestamp (the most accurate)
2928 // or 'k' if estimated from kernel (usually for debugging).
2929 result.appendFormat(" %7.2lf %c", latencyMs, fromTrack ? 't' : 'k');
2930 } else {
2931 result.appendFormat("%10s", mCblk->mServer != 0 ? "unavail" : "new");
2932 }
2933 }
2934 result.append("\n");
Eric Laurent81784c32012-11-19 14:55:58 -08002935}
2936
Andy Hung93bb5732023-05-04 21:16:34 -07002937// This is invoked by SyncEvent callback.
Andy Hung8d31fd22023-06-26 19:20:57 -07002938void RecordTrack::handleSyncStartEvent(
Andy Hung068e08e2023-05-15 19:02:55 -07002939 const sp<audioflinger::SyncEvent>& event)
Glenn Kasten25f4aa82014-02-07 10:50:43 -08002940{
Andy Hung93bb5732023-05-04 21:16:34 -07002941 size_t framesToDrop = 0;
Andy Hung87c693c2023-07-06 20:56:16 -07002942 const sp<IAfThreadBase> threadBase = mThread.promote();
Andy Hung93bb5732023-05-04 21:16:34 -07002943 if (threadBase != 0) {
2944 // TODO: use actual buffer filling status instead of 2 buffers when info is available
2945 // from audio HAL
Andy Hung87c693c2023-07-06 20:56:16 -07002946 framesToDrop = threadBase->frameCount() * 2;
Glenn Kasten25f4aa82014-02-07 10:50:43 -08002947 }
Andy Hung93bb5732023-05-04 21:16:34 -07002948
2949 mSynchronizedRecordState.onPlaybackFinished(event, framesToDrop);
Glenn Kasten25f4aa82014-02-07 10:50:43 -08002950}
2951
Andy Hung8d31fd22023-06-26 19:20:57 -07002952void RecordTrack::clearSyncStartEvent()
Glenn Kasten25f4aa82014-02-07 10:50:43 -08002953{
Andy Hung93bb5732023-05-04 21:16:34 -07002954 mSynchronizedRecordState.clear();
Glenn Kasten25f4aa82014-02-07 10:50:43 -08002955}
2956
Andy Hung8d31fd22023-06-26 19:20:57 -07002957void RecordTrack::updateTrackFrameInfo(
Andy Hung3f0c9022016-01-15 17:49:46 -08002958 int64_t trackFramesReleased, int64_t sourceFramesRead,
2959 uint32_t halSampleRate, const ExtendedTimestamp &timestamp)
2960{
Andy Hung30282562018-08-08 18:27:03 -07002961 // Make the kernel frametime available.
2962 const FrameTime ft{
2963 timestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL],
2964 timestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL]};
2965 // ALOGD("FrameTime: %lld %lld", (long long)ft.frames, (long long)ft.timeNs);
2966 mKernelFrameTime.store(ft);
2967 if (!audio_is_linear_pcm(mFormat)) {
Atneya Nair497fff12022-01-18 16:23:04 -05002968 // Stream is direct, return provided timestamp with no conversion
2969 mServerProxy->setTimestamp(timestamp);
Andy Hung30282562018-08-08 18:27:03 -07002970 return;
2971 }
2972
Andy Hung3f0c9022016-01-15 17:49:46 -08002973 ExtendedTimestamp local = timestamp;
2974
2975 // Convert HAL frames to server-side track frames at track sample rate.
2976 // We use trackFramesReleased and sourceFramesRead as an anchor point.
2977 for (int i = ExtendedTimestamp::LOCATION_SERVER; i < ExtendedTimestamp::LOCATION_MAX; ++i) {
2978 if (local.mTimeNs[i] != 0) {
2979 const int64_t relativeServerFrames = local.mPosition[i] - sourceFramesRead;
2980 const int64_t relativeTrackFrames = relativeServerFrames
2981 * mSampleRate / halSampleRate; // TODO: potential computation overflow
2982 local.mPosition[i] = relativeTrackFrames + trackFramesReleased;
2983 }
2984 }
Andy Hung6ae58432016-02-16 18:32:24 -08002985 mServerProxy->setTimestamp(local);
Andy Hung000adb52018-06-01 15:43:26 -07002986
2987 // Compute latency info.
2988 const bool useTrackTimestamp = true; // use track unless debugging.
2989 const double latencyMs = - (useTrackTimestamp
2990 ? local.getOutputServerLatencyMs(sampleRate())
2991 : timestamp.getOutputServerLatencyMs(halSampleRate));
2992
2993 mServerLatencyFromTrack.store(useTrackTimestamp);
2994 mServerLatencyMs.store(latencyMs);
Andy Hung3f0c9022016-01-15 17:49:46 -08002995}
Eric Laurent83b88082014-06-20 18:31:16 -07002996
Andy Hung8d31fd22023-06-26 19:20:57 -07002997status_t RecordTrack::getActiveMicrophones(
Andy Hungd29af632023-06-23 19:27:19 -07002998 std::vector<media::MicrophoneInfoFw>* activeMicrophones) const
jiabin653cc0a2018-01-17 17:54:10 -08002999{
Andy Hung87c693c2023-07-06 20:56:16 -07003000 const sp<IAfThreadBase> thread = mThread.promote();
jiabin653cc0a2018-01-17 17:54:10 -08003001 if (thread != 0) {
Andy Hung87c693c2023-07-06 20:56:16 -07003002 auto* const recordThread = thread->asIAfRecordThread().get();
jiabin653cc0a2018-01-17 17:54:10 -08003003 return recordThread->getActiveMicrophones(activeMicrophones);
3004 } else {
3005 return BAD_VALUE;
3006 }
3007}
3008
Andy Hung8d31fd22023-06-26 19:20:57 -07003009status_t RecordTrack::setPreferredMicrophoneDirection(
Paul McLean03a6e6a2018-12-04 10:54:13 -07003010 audio_microphone_direction_t direction) {
Andy Hung87c693c2023-07-06 20:56:16 -07003011 const sp<IAfThreadBase> thread = mThread.promote();
Paul McLean03a6e6a2018-12-04 10:54:13 -07003012 if (thread != 0) {
Andy Hung87c693c2023-07-06 20:56:16 -07003013 auto* const recordThread = thread->asIAfRecordThread().get();
Paul McLean12340082019-03-19 09:35:05 -06003014 return recordThread->setPreferredMicrophoneDirection(direction);
Paul McLean03a6e6a2018-12-04 10:54:13 -07003015 } else {
3016 return BAD_VALUE;
3017 }
3018}
3019
Andy Hung8d31fd22023-06-26 19:20:57 -07003020status_t RecordTrack::setPreferredMicrophoneFieldDimension(float zoom) {
Andy Hung87c693c2023-07-06 20:56:16 -07003021 const sp<IAfThreadBase> thread = mThread.promote();
Paul McLean03a6e6a2018-12-04 10:54:13 -07003022 if (thread != 0) {
Andy Hung87c693c2023-07-06 20:56:16 -07003023 auto* const recordThread = thread->asIAfRecordThread().get();
Paul McLean12340082019-03-19 09:35:05 -06003024 return recordThread->setPreferredMicrophoneFieldDimension(zoom);
Paul McLean03a6e6a2018-12-04 10:54:13 -07003025 } else {
3026 return BAD_VALUE;
3027 }
3028}
3029
Andy Hung8d31fd22023-06-26 19:20:57 -07003030status_t RecordTrack::shareAudioHistory(
Eric Laurentec376dc2021-04-08 20:41:22 +02003031 const std::string& sharedAudioPackageName, int64_t sharedAudioStartMs) {
3032
3033 const uid_t callingUid = IPCThreadState::self()->getCallingUid();
3034 const pid_t callingPid = IPCThreadState::self()->getCallingPid();
3035 if (callingUid != mUid || callingPid != mCreatorPid) {
3036 return PERMISSION_DENIED;
3037 }
3038
Svet Ganov33761132021-05-13 22:51:08 +00003039 AttributionSourceState attributionSource{};
3040 attributionSource.uid = VALUE_OR_RETURN_STATUS(legacy2aidl_uid_t_int32_t(callingUid));
3041 attributionSource.pid = VALUE_OR_RETURN_STATUS(legacy2aidl_uid_t_int32_t(callingPid));
3042 attributionSource.token = sp<BBinder>::make();
3043 if (!captureHotwordAllowed(attributionSource)) {
Eric Laurentec376dc2021-04-08 20:41:22 +02003044 return PERMISSION_DENIED;
3045 }
3046
Andy Hung87c693c2023-07-06 20:56:16 -07003047 const sp<IAfThreadBase> thread = mThread.promote();
Eric Laurentec376dc2021-04-08 20:41:22 +02003048 if (thread != 0) {
Andy Hung87c693c2023-07-06 20:56:16 -07003049 auto* const recordThread = thread->asIAfRecordThread().get();
Eric Laurentec376dc2021-04-08 20:41:22 +02003050 status_t status = recordThread->shareAudioHistory(
3051 sharedAudioPackageName, mSessionId, sharedAudioStartMs);
3052 if (status == NO_ERROR) {
3053 mSharedAudioPackageName = sharedAudioPackageName;
3054 }
3055 return status;
3056 } else {
3057 return BAD_VALUE;
3058 }
3059}
3060
Andy Hung8d31fd22023-06-26 19:20:57 -07003061void RecordTrack::copyMetadataTo(MetadataInserter& backInserter) const
Eric Laurent78b07302022-10-07 16:20:34 +02003062{
3063
3064 // Do not forward PatchRecord metadata with unspecified audio source
3065 if (mAttr.source == AUDIO_SOURCE_DEFAULT) {
3066 return;
3067 }
3068
3069 // No track is invalid as this is called after prepareTrack_l in the same critical section
3070 record_track_metadata_v7_t metadata;
3071 metadata.base = {
3072 .source = mAttr.source,
3073 .gain = 1, // capture tracks do not have volumes
3074 };
3075 metadata.channel_mask = mChannelMask;
3076 strncpy(metadata.tags, mAttr.tags, AUDIO_ATTRIBUTES_TAGS_MAX_SIZE);
3077
3078 *backInserter++ = metadata;
3079}
Eric Laurentec376dc2021-04-08 20:41:22 +02003080
Andy Hung9d84af52018-09-12 18:03:44 -07003081// ----------------------------------------------------------------------------
3082#undef LOG_TAG
3083#define LOG_TAG "AF::PatchRecord"
3084
Andy Hung8d31fd22023-06-26 19:20:57 -07003085/* static */
3086sp<IAfPatchRecord> IAfPatchRecord::create(
Andy Hung87c693c2023-07-06 20:56:16 -07003087 IAfRecordThread* recordThread,
Andy Hung8d31fd22023-06-26 19:20:57 -07003088 uint32_t sampleRate,
3089 audio_channel_mask_t channelMask,
3090 audio_format_t format,
3091 size_t frameCount,
3092 void *buffer,
3093 size_t bufferSize,
3094 audio_input_flags_t flags,
3095 const Timeout& timeout,
3096 audio_source_t source)
3097{
3098 return sp<PatchRecord>::make(
Andy Hung87c693c2023-07-06 20:56:16 -07003099 recordThread,
Andy Hung8d31fd22023-06-26 19:20:57 -07003100 sampleRate,
3101 channelMask,
3102 format,
3103 frameCount,
3104 buffer,
3105 bufferSize,
3106 flags,
3107 timeout,
3108 source);
3109}
3110
Andy Hung87c693c2023-07-06 20:56:16 -07003111PatchRecord::PatchRecord(IAfRecordThread* recordThread,
Eric Laurent83b88082014-06-20 18:31:16 -07003112 uint32_t sampleRate,
3113 audio_channel_mask_t channelMask,
3114 audio_format_t format,
3115 size_t frameCount,
3116 void *buffer,
Andy Hung8fe68032017-06-05 16:17:51 -07003117 size_t bufferSize,
Kevin Rocard45986c72018-12-18 18:22:59 -08003118 audio_input_flags_t flags,
Eric Laurent78b07302022-10-07 16:20:34 +02003119 const Timeout& timeout,
3120 audio_source_t source)
Kevin Rocard1f564ac2018-03-29 13:53:10 -07003121 : RecordTrack(recordThread, NULL,
Eric Laurent78b07302022-10-07 16:20:34 +02003122 audio_attributes_t{ .source = source } ,
Kevin Rocard1f564ac2018-03-29 13:53:10 -07003123 sampleRate, format, channelMask, frameCount,
Philip P. Moltmannbda45752020-07-17 16:41:18 -07003124 buffer, bufferSize, AUDIO_SESSION_NONE, getpid(),
Svet Ganov33761132021-05-13 22:51:08 +00003125 audioServerAttributionSource(getpid()), flags, TYPE_PATCH),
gaoxiupei8e3a5682023-07-07 20:30:23 +08003126 PatchTrackBase(mCblk ? new ClientProxy(mCblk, mBuffer, frameCount, mFrameSize, false, true)
3127 : nullptr,
Kevin Rocard45986c72018-12-18 18:22:59 -08003128 *recordThread, timeout)
Eric Laurent83b88082014-06-20 18:31:16 -07003129{
Andy Hung9d84af52018-09-12 18:03:44 -07003130 ALOGV("%s(%d): sampleRate %d mPeerTimeout %d.%03d sec",
3131 __func__, mId, sampleRate,
Eric Laurent83b88082014-06-20 18:31:16 -07003132 (int)mPeerTimeout.tv_sec,
3133 (int)(mPeerTimeout.tv_nsec / 1000000));
3134}
3135
Andy Hung8d31fd22023-06-26 19:20:57 -07003136PatchRecord::~PatchRecord()
Eric Laurent83b88082014-06-20 18:31:16 -07003137{
Andy Hungabfab202019-03-07 19:45:54 -08003138 ALOGV("%s(%d)", __func__, mId);
Eric Laurent83b88082014-06-20 18:31:16 -07003139}
3140
Mikhail Naganov8296c252019-09-25 14:59:54 -07003141static size_t writeFramesHelper(
3142 AudioBufferProvider* dest, const void* src, size_t frameCount, size_t frameSize)
3143{
3144 AudioBufferProvider::Buffer patchBuffer;
3145 patchBuffer.frameCount = frameCount;
3146 auto status = dest->getNextBuffer(&patchBuffer);
3147 if (status != NO_ERROR) {
3148 ALOGW("%s PathRecord getNextBuffer failed with error %d: %s",
3149 __func__, status, strerror(-status));
3150 return 0;
3151 }
3152 ALOG_ASSERT(patchBuffer.frameCount <= frameCount);
3153 memcpy(patchBuffer.raw, src, patchBuffer.frameCount * frameSize);
3154 size_t framesWritten = patchBuffer.frameCount;
3155 dest->releaseBuffer(&patchBuffer);
3156 return framesWritten;
3157}
3158
3159// static
Andy Hung8d31fd22023-06-26 19:20:57 -07003160size_t PatchRecord::writeFrames(
Mikhail Naganov8296c252019-09-25 14:59:54 -07003161 AudioBufferProvider* dest, const void* src, size_t frameCount, size_t frameSize)
3162{
3163 size_t framesWritten = writeFramesHelper(dest, src, frameCount, frameSize);
3164 // On buffer wrap, the buffer frame count will be less than requested,
3165 // when this happens a second buffer needs to be used to write the leftover audio
3166 const size_t framesLeft = frameCount - framesWritten;
3167 if (framesWritten != 0 && framesLeft != 0) {
3168 framesWritten += writeFramesHelper(dest, (const char*)src + framesWritten * frameSize,
3169 framesLeft, frameSize);
3170 }
3171 return framesWritten;
3172}
3173
Eric Laurent83b88082014-06-20 18:31:16 -07003174// AudioBufferProvider interface
Andy Hung8d31fd22023-06-26 19:20:57 -07003175status_t PatchRecord::getNextBuffer(
Glenn Kastend79072e2016-01-06 08:41:20 -08003176 AudioBufferProvider::Buffer* buffer)
Eric Laurent83b88082014-06-20 18:31:16 -07003177{
Andy Hung9d84af52018-09-12 18:03:44 -07003178 ALOG_ASSERT(mPeerProxy != 0, "%s(%d): called without peer proxy", __func__, mId);
Eric Laurent83b88082014-06-20 18:31:16 -07003179 Proxy::Buffer buf;
3180 buf.mFrameCount = buffer->frameCount;
3181 status_t status = mPeerProxy->obtainBuffer(&buf, &mPeerTimeout);
3182 ALOGV_IF(status != NO_ERROR,
Andy Hung9d84af52018-09-12 18:03:44 -07003183 "%s(%d): mPeerProxy->obtainBuffer status %d", __func__, mId, status);
Eric Laurentc2730ba2014-07-20 15:47:07 -07003184 buffer->frameCount = buf.mFrameCount;
Mikhail Naganov938be412019-09-04 11:38:47 -07003185 if (ATRACE_ENABLED()) {
3186 std::string traceName("PRnObt");
3187 traceName += std::to_string(id());
3188 ATRACE_INT(traceName.c_str(), buf.mFrameCount);
3189 }
Eric Laurent83b88082014-06-20 18:31:16 -07003190 if (buf.mFrameCount == 0) {
3191 return WOULD_BLOCK;
3192 }
Glenn Kastend79072e2016-01-06 08:41:20 -08003193 status = RecordTrack::getNextBuffer(buffer);
Eric Laurent83b88082014-06-20 18:31:16 -07003194 return status;
3195}
3196
Andy Hung8d31fd22023-06-26 19:20:57 -07003197void PatchRecord::releaseBuffer(AudioBufferProvider::Buffer* buffer)
Eric Laurent83b88082014-06-20 18:31:16 -07003198{
Andy Hung9d84af52018-09-12 18:03:44 -07003199 ALOG_ASSERT(mPeerProxy != 0, "%s(%d): called without peer proxy", __func__, mId);
Eric Laurent83b88082014-06-20 18:31:16 -07003200 Proxy::Buffer buf;
3201 buf.mFrameCount = buffer->frameCount;
3202 buf.mRaw = buffer->raw;
3203 mPeerProxy->releaseBuffer(&buf);
3204 TrackBase::releaseBuffer(buffer);
3205}
3206
Andy Hung8d31fd22023-06-26 19:20:57 -07003207status_t PatchRecord::obtainBuffer(Proxy::Buffer* buffer,
Eric Laurent83b88082014-06-20 18:31:16 -07003208 const struct timespec *timeOut)
3209{
3210 return mProxy->obtainBuffer(buffer, timeOut);
3211}
3212
Andy Hung8d31fd22023-06-26 19:20:57 -07003213void PatchRecord::releaseBuffer(Proxy::Buffer* buffer)
Eric Laurent83b88082014-06-20 18:31:16 -07003214{
3215 mProxy->releaseBuffer(buffer);
3216}
3217
Mikhail Naganovcaf59942019-09-25 14:05:29 -07003218#undef LOG_TAG
3219#define LOG_TAG "AF::PthrPatchRecord"
3220
3221static std::unique_ptr<void, decltype(free)*> allocAligned(size_t alignment, size_t size)
3222{
3223 void *ptr = nullptr;
3224 (void)posix_memalign(&ptr, alignment, size);
Andy Hung920f6572022-10-06 12:09:49 -07003225 return {ptr, free};
Mikhail Naganovcaf59942019-09-25 14:05:29 -07003226}
3227
Andy Hung8d31fd22023-06-26 19:20:57 -07003228/* static */
3229sp<IAfPatchRecord> IAfPatchRecord::createPassThru(
Andy Hung87c693c2023-07-06 20:56:16 -07003230 IAfRecordThread* recordThread,
Andy Hung8d31fd22023-06-26 19:20:57 -07003231 uint32_t sampleRate,
3232 audio_channel_mask_t channelMask,
3233 audio_format_t format,
3234 size_t frameCount,
3235 audio_input_flags_t flags,
3236 audio_source_t source)
3237{
3238 return sp<PassthruPatchRecord>::make(
Andy Hung87c693c2023-07-06 20:56:16 -07003239 recordThread,
Andy Hung8d31fd22023-06-26 19:20:57 -07003240 sampleRate,
3241 channelMask,
3242 format,
3243 frameCount,
3244 flags,
3245 source);
3246}
3247
3248PassthruPatchRecord::PassthruPatchRecord(
Andy Hung87c693c2023-07-06 20:56:16 -07003249 IAfRecordThread* recordThread,
Mikhail Naganovcaf59942019-09-25 14:05:29 -07003250 uint32_t sampleRate,
3251 audio_channel_mask_t channelMask,
3252 audio_format_t format,
3253 size_t frameCount,
Eric Laurent78b07302022-10-07 16:20:34 +02003254 audio_input_flags_t flags,
3255 audio_source_t source)
Mikhail Naganovcaf59942019-09-25 14:05:29 -07003256 : PatchRecord(recordThread, sampleRate, channelMask, format, frameCount,
Eric Laurent78b07302022-10-07 16:20:34 +02003257 nullptr /*buffer*/, 0 /*bufferSize*/, flags, {} /* timeout */, source),
Mikhail Naganovcaf59942019-09-25 14:05:29 -07003258 mPatchRecordAudioBufferProvider(*this),
3259 mSinkBuffer(allocAligned(32, mFrameCount * mFrameSize)),
3260 mStubBuffer(allocAligned(32, mFrameCount * mFrameSize))
3261{
3262 memset(mStubBuffer.get(), 0, mFrameCount * mFrameSize);
3263}
3264
Andy Hung8d31fd22023-06-26 19:20:57 -07003265sp<StreamInHalInterface> PassthruPatchRecord::obtainStream(
Andy Hung87c693c2023-07-06 20:56:16 -07003266 sp<IAfThreadBase>* thread)
Mikhail Naganovcaf59942019-09-25 14:05:29 -07003267{
3268 *thread = mThread.promote();
3269 if (!*thread) return nullptr;
Andy Hung87c693c2023-07-06 20:56:16 -07003270 auto* const recordThread = (*thread)->asIAfRecordThread().get();
3271 Mutex::Autolock _l(recordThread->mutex());
3272 return recordThread->getInput() ? recordThread->getInput()->stream : nullptr;
Mikhail Naganovcaf59942019-09-25 14:05:29 -07003273}
3274
3275// PatchProxyBufferProvider methods are called on DirectOutputThread
Andy Hung8d31fd22023-06-26 19:20:57 -07003276status_t PassthruPatchRecord::obtainBuffer(
Mikhail Naganovcaf59942019-09-25 14:05:29 -07003277 Proxy::Buffer* buffer, const struct timespec* timeOut)
3278{
3279 if (mUnconsumedFrames) {
3280 buffer->mFrameCount = std::min(buffer->mFrameCount, mUnconsumedFrames);
3281 // mUnconsumedFrames is decreased in releaseBuffer to use actual frame consumption figure.
3282 return PatchRecord::obtainBuffer(buffer, timeOut);
3283 }
3284
3285 // Otherwise, execute a read from HAL and write into the buffer.
3286 nsecs_t startTimeNs = 0;
3287 if (timeOut && (timeOut->tv_sec != 0 || timeOut->tv_nsec != 0) && timeOut->tv_sec != INT_MAX) {
3288 // Will need to correct timeOut by elapsed time.
3289 startTimeNs = systemTime();
3290 }
3291 const size_t framesToRead = std::min(buffer->mFrameCount, mFrameCount);
3292 buffer->mFrameCount = 0;
3293 buffer->mRaw = nullptr;
Andy Hung87c693c2023-07-06 20:56:16 -07003294 sp<IAfThreadBase> thread;
Mikhail Naganovcaf59942019-09-25 14:05:29 -07003295 sp<StreamInHalInterface> stream = obtainStream(&thread);
3296 if (!stream) return NO_INIT; // If there is no stream, RecordThread is not reading.
3297
3298 status_t result = NO_ERROR;
Mikhail Naganovcaf59942019-09-25 14:05:29 -07003299 size_t bytesRead = 0;
3300 {
3301 ATRACE_NAME("read");
3302 result = stream->read(mSinkBuffer.get(), framesToRead * mFrameSize, &bytesRead);
3303 if (result != NO_ERROR) goto stream_error;
3304 if (bytesRead == 0) return NO_ERROR;
3305 }
3306
3307 {
3308 std::lock_guard<std::mutex> lock(mReadLock);
3309 mReadBytes += bytesRead;
3310 mReadError = NO_ERROR;
3311 }
3312 mReadCV.notify_one();
3313 // writeFrames handles wraparound and should write all the provided frames.
3314 // If it couldn't, there is something wrong with the client/server buffer of the software patch.
3315 buffer->mFrameCount = writeFrames(
3316 &mPatchRecordAudioBufferProvider,
3317 mSinkBuffer.get(), bytesRead / mFrameSize, mFrameSize);
3318 ALOGW_IF(buffer->mFrameCount < bytesRead / mFrameSize,
3319 "Lost %zu frames obtained from HAL", bytesRead / mFrameSize - buffer->mFrameCount);
3320 mUnconsumedFrames = buffer->mFrameCount;
Mikhail Naganov4de49972019-10-07 09:53:58 -07003321 struct timespec newTimeOut;
Mikhail Naganovcaf59942019-09-25 14:05:29 -07003322 if (startTimeNs) {
Mikhail Naganov4de49972019-10-07 09:53:58 -07003323 // Correct the timeout by elapsed time.
3324 nsecs_t newTimeOutNs = audio_utils_ns_from_timespec(timeOut) - (systemTime() - startTimeNs);
Mikhail Naganovcaf59942019-09-25 14:05:29 -07003325 if (newTimeOutNs < 0) newTimeOutNs = 0;
3326 newTimeOut.tv_sec = newTimeOutNs / NANOS_PER_SECOND;
3327 newTimeOut.tv_nsec = newTimeOutNs - newTimeOut.tv_sec * NANOS_PER_SECOND;
Mikhail Naganov4de49972019-10-07 09:53:58 -07003328 timeOut = &newTimeOut;
Mikhail Naganovcaf59942019-09-25 14:05:29 -07003329 }
Mikhail Naganov4de49972019-10-07 09:53:58 -07003330 return PatchRecord::obtainBuffer(buffer, timeOut);
Mikhail Naganovcaf59942019-09-25 14:05:29 -07003331
3332stream_error:
3333 stream->standby();
3334 {
3335 std::lock_guard<std::mutex> lock(mReadLock);
3336 mReadError = result;
3337 }
3338 mReadCV.notify_one();
3339 return result;
3340}
3341
Andy Hung8d31fd22023-06-26 19:20:57 -07003342void PassthruPatchRecord::releaseBuffer(Proxy::Buffer* buffer)
Mikhail Naganovcaf59942019-09-25 14:05:29 -07003343{
3344 if (buffer->mFrameCount <= mUnconsumedFrames) {
3345 mUnconsumedFrames -= buffer->mFrameCount;
3346 } else {
3347 ALOGW("Write side has consumed more frames than we had: %zu > %zu",
3348 buffer->mFrameCount, mUnconsumedFrames);
3349 mUnconsumedFrames = 0;
3350 }
3351 PatchRecord::releaseBuffer(buffer);
3352}
3353
3354// AudioBufferProvider and Source methods are called on RecordThread
3355// 'read' emulates actual audio data with 0's. This is OK as 'getNextBuffer'
3356// and 'releaseBuffer' are stubbed out and ignore their input.
3357// It's not possible to retrieve actual data here w/o blocking 'obtainBuffer'
3358// until we copy it.
Andy Hung8d31fd22023-06-26 19:20:57 -07003359status_t PassthruPatchRecord::read(
Mikhail Naganovcaf59942019-09-25 14:05:29 -07003360 void* buffer, size_t bytes, size_t* read)
3361{
3362 bytes = std::min(bytes, mFrameCount * mFrameSize);
3363 {
3364 std::unique_lock<std::mutex> lock(mReadLock);
3365 mReadCV.wait(lock, [&]{ return mReadError != NO_ERROR || mReadBytes != 0; });
3366 if (mReadError != NO_ERROR) {
3367 mLastReadFrames = 0;
3368 return mReadError;
3369 }
3370 *read = std::min(bytes, mReadBytes);
3371 mReadBytes -= *read;
3372 }
3373 mLastReadFrames = *read / mFrameSize;
3374 memset(buffer, 0, *read);
3375 return 0;
3376}
3377
Andy Hung8d31fd22023-06-26 19:20:57 -07003378status_t PassthruPatchRecord::getCapturePosition(
Mikhail Naganovcaf59942019-09-25 14:05:29 -07003379 int64_t* frames, int64_t* time)
3380{
Andy Hung87c693c2023-07-06 20:56:16 -07003381 sp<IAfThreadBase> thread;
Mikhail Naganovcaf59942019-09-25 14:05:29 -07003382 sp<StreamInHalInterface> stream = obtainStream(&thread);
3383 return stream ? stream->getCapturePosition(frames, time) : NO_INIT;
3384}
3385
Andy Hung8d31fd22023-06-26 19:20:57 -07003386status_t PassthruPatchRecord::standby()
Mikhail Naganovcaf59942019-09-25 14:05:29 -07003387{
3388 // RecordThread issues 'standby' command in two major cases:
3389 // 1. Error on read--this case is handled in 'obtainBuffer'.
3390 // 2. Track is stopping--as PassthruPatchRecord assumes continuous
3391 // output, this can only happen when the software patch
3392 // is being torn down. In this case, the RecordThread
3393 // will terminate and close the HAL stream.
3394 return 0;
3395}
3396
3397// As the buffer gets filled in obtainBuffer, here we only simulate data consumption.
Andy Hung8d31fd22023-06-26 19:20:57 -07003398status_t PassthruPatchRecord::getNextBuffer(
Mikhail Naganovcaf59942019-09-25 14:05:29 -07003399 AudioBufferProvider::Buffer* buffer)
3400{
3401 buffer->frameCount = mLastReadFrames;
3402 buffer->raw = buffer->frameCount != 0 ? mStubBuffer.get() : nullptr;
3403 return NO_ERROR;
3404}
3405
Andy Hung8d31fd22023-06-26 19:20:57 -07003406void PassthruPatchRecord::releaseBuffer(
Mikhail Naganovcaf59942019-09-25 14:05:29 -07003407 AudioBufferProvider::Buffer* buffer)
3408{
3409 buffer->frameCount = 0;
3410 buffer->raw = nullptr;
3411}
3412
Andy Hung9d84af52018-09-12 18:03:44 -07003413// ----------------------------------------------------------------------------
3414#undef LOG_TAG
3415#define LOG_TAG "AF::MmapTrack"
Eric Laurent6acd1d42017-01-04 14:23:29 -08003416
Andy Hung8d31fd22023-06-26 19:20:57 -07003417/* static */
Andy Hung87c693c2023-07-06 20:56:16 -07003418sp<IAfMmapTrack> IAfMmapTrack::create(IAfThreadBase* thread,
Andy Hung8d31fd22023-06-26 19:20:57 -07003419 const audio_attributes_t& attr,
3420 uint32_t sampleRate,
3421 audio_format_t format,
3422 audio_channel_mask_t channelMask,
3423 audio_session_t sessionId,
3424 bool isOut,
3425 const android::content::AttributionSourceState& attributionSource,
3426 pid_t creatorPid,
3427 audio_port_handle_t portId)
3428{
3429 return sp<MmapTrack>::make(
Andy Hung87c693c2023-07-06 20:56:16 -07003430 thread,
Andy Hung8d31fd22023-06-26 19:20:57 -07003431 attr,
3432 sampleRate,
3433 format,
3434 channelMask,
3435 sessionId,
3436 isOut,
3437 attributionSource,
3438 creatorPid,
3439 portId);
3440}
3441
Andy Hung87c693c2023-07-06 20:56:16 -07003442MmapTrack::MmapTrack(IAfThreadBase* thread,
Kevin Rocard1f564ac2018-03-29 13:53:10 -07003443 const audio_attributes_t& attr,
Eric Laurent6acd1d42017-01-04 14:23:29 -08003444 uint32_t sampleRate,
3445 audio_format_t format,
3446 audio_channel_mask_t channelMask,
3447 audio_session_t sessionId,
Kevin Rocard5f2136e2018-05-11 22:03:00 -07003448 bool isOut,
Svet Ganov33761132021-05-13 22:51:08 +00003449 const AttributionSourceState& attributionSource,
Eric Laurent09f1ed22019-04-24 17:45:17 -07003450 pid_t creatorPid,
Eric Laurent6acd1d42017-01-04 14:23:29 -08003451 audio_port_handle_t portId)
Kevin Rocard1f564ac2018-03-29 13:53:10 -07003452 : TrackBase(thread, NULL, attr, sampleRate, format,
Andy Hung8fe68032017-06-05 16:17:51 -07003453 channelMask, (size_t)0 /* frameCount */,
3454 nullptr /* buffer */, (size_t)0 /* bufferSize */,
Philip P. Moltmannbda45752020-07-17 16:41:18 -07003455 sessionId, creatorPid,
Svet Ganov33761132021-05-13 22:51:08 +00003456 VALUE_OR_FATAL(aidl2legacy_int32_t_uid_t(attributionSource.uid)),
Philip P. Moltmannbda45752020-07-17 16:41:18 -07003457 isOut,
Eric Laurent6acd1d42017-01-04 14:23:29 -08003458 ALLOC_NONE,
Andy Hungc2b11cb2020-04-22 09:04:01 -07003459 TYPE_DEFAULT, portId,
3460 std::string(AMEDIAMETRICS_KEY_PREFIX_AUDIO_MMAP) + std::to_string(portId)),
Svet Ganov33761132021-05-13 22:51:08 +00003461 mPid(VALUE_OR_FATAL(aidl2legacy_int32_t_uid_t(attributionSource.pid))),
Philip P. Moltmannbda45752020-07-17 16:41:18 -07003462 mSilenced(false), mSilencedNotified(false)
Eric Laurent6acd1d42017-01-04 14:23:29 -08003463{
Andy Hungc2b11cb2020-04-22 09:04:01 -07003464 // Once this item is logged by the server, the client can add properties.
Andy Hung94235282021-03-24 15:50:14 -07003465 mTrackMetrics.logConstructor(creatorPid, uid(), id());
Eric Laurent6acd1d42017-01-04 14:23:29 -08003466}
3467
Andy Hung8d31fd22023-06-26 19:20:57 -07003468MmapTrack::~MmapTrack()
Eric Laurent6acd1d42017-01-04 14:23:29 -08003469{
3470}
3471
Andy Hung8d31fd22023-06-26 19:20:57 -07003472status_t MmapTrack::initCheck() const
Eric Laurent6acd1d42017-01-04 14:23:29 -08003473{
3474 return NO_ERROR;
3475}
3476
Andy Hung8d31fd22023-06-26 19:20:57 -07003477status_t MmapTrack::start(AudioSystem::sync_event_t event __unused,
Kevin Rocard5f2136e2018-05-11 22:03:00 -07003478 audio_session_t triggerSession __unused)
Eric Laurent6acd1d42017-01-04 14:23:29 -08003479{
3480 return NO_ERROR;
3481}
3482
Andy Hung8d31fd22023-06-26 19:20:57 -07003483void MmapTrack::stop()
Eric Laurent6acd1d42017-01-04 14:23:29 -08003484{
3485}
3486
3487// AudioBufferProvider interface
Andy Hung8d31fd22023-06-26 19:20:57 -07003488status_t MmapTrack::getNextBuffer(AudioBufferProvider::Buffer* buffer)
Eric Laurent6acd1d42017-01-04 14:23:29 -08003489{
3490 buffer->frameCount = 0;
3491 buffer->raw = nullptr;
3492 return INVALID_OPERATION;
3493}
3494
3495// ExtendedAudioBufferProvider interface
Andy Hung8d31fd22023-06-26 19:20:57 -07003496size_t MmapTrack::framesReady() const {
Eric Laurent6acd1d42017-01-04 14:23:29 -08003497 return 0;
3498}
3499
Andy Hung8d31fd22023-06-26 19:20:57 -07003500int64_t MmapTrack::framesReleased() const
Eric Laurent6acd1d42017-01-04 14:23:29 -08003501{
3502 return 0;
3503}
3504
Andy Hung8d31fd22023-06-26 19:20:57 -07003505void MmapTrack::onTimestamp(const ExtendedTimestamp& timestamp __unused)
Eric Laurent6acd1d42017-01-04 14:23:29 -08003506{
3507}
3508
Andy Hung8d31fd22023-06-26 19:20:57 -07003509void MmapTrack::processMuteEvent_l(const sp<IAudioManager>& audioManager, mute_state_t muteState)
Vlad Popaec1788e2022-08-04 11:23:30 +02003510{
3511 if (mMuteState == muteState) {
3512 // mute state did not change, do nothing
3513 return;
3514 }
3515
3516 status_t result = UNKNOWN_ERROR;
3517 if (audioManager && mPortId != AUDIO_PORT_HANDLE_NONE) {
3518 if (mMuteEventExtras == nullptr) {
3519 mMuteEventExtras = std::make_unique<os::PersistableBundle>();
3520 }
3521 mMuteEventExtras->putInt(String16(kExtraPlayerEventMuteKey),
3522 static_cast<int>(muteState));
3523
3524 result = audioManager->portEvent(mPortId,
3525 PLAYER_UPDATE_MUTED,
3526 mMuteEventExtras);
3527 }
3528
3529 if (result == OK) {
3530 mMuteState = muteState;
3531 } else {
3532 ALOGW("%s(%d): cannot process mute state for port ID %d, status error %d",
3533 __func__,
3534 id(),
3535 mPortId,
3536 result);
3537 }
3538}
3539
Andy Hung8d31fd22023-06-26 19:20:57 -07003540void MmapTrack::appendDumpHeader(String8& result) const
Eric Laurent6acd1d42017-01-04 14:23:29 -08003541{
Eric Laurent973db022018-11-20 14:54:31 -08003542 result.appendFormat("Client Session Port Id Format Chn mask SRate Flags %s\n",
Kevin Rocard5f2136e2018-05-11 22:03:00 -07003543 isOut() ? "Usg CT": "Source");
Eric Laurent6acd1d42017-01-04 14:23:29 -08003544}
3545
Andy Hung8d31fd22023-06-26 19:20:57 -07003546void MmapTrack::appendDump(String8& result, bool active __unused) const
Eric Laurent6acd1d42017-01-04 14:23:29 -08003547{
Eric Laurent973db022018-11-20 14:54:31 -08003548 result.appendFormat("%6u %7u %7u %08X %08X %6u 0x%03X ",
Andy Hung2c6c3bb2017-06-16 14:01:45 -07003549 mPid,
3550 mSessionId,
Eric Laurent973db022018-11-20 14:54:31 -08003551 mPortId,
Eric Laurent6acd1d42017-01-04 14:23:29 -08003552 mFormat,
3553 mChannelMask,
Kevin Rocard5f2136e2018-05-11 22:03:00 -07003554 mSampleRate,
3555 mAttr.flags);
3556 if (isOut()) {
3557 result.appendFormat("%3x %2x", mAttr.usage, mAttr.content_type);
3558 } else {
3559 result.appendFormat("%6x", mAttr.source);
3560 }
3561 result.append("\n");
Eric Laurent6acd1d42017-01-04 14:23:29 -08003562}
3563
Glenn Kasten63238ef2015-03-02 15:50:29 -08003564} // namespace android