Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1 | /* |
| 2 | ** |
| 3 | ** Copyright 2012, The Android Open Source Project |
| 4 | ** |
| 5 | ** Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | ** you may not use this file except in compliance with the License. |
| 7 | ** You may obtain a copy of the License at |
| 8 | ** |
| 9 | ** http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | ** |
| 11 | ** Unless required by applicable law or agreed to in writing, software |
| 12 | ** distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | ** See the License for the specific language governing permissions and |
| 15 | ** limitations under the License. |
| 16 | */ |
| 17 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 18 | #define LOG_TAG "AudioFlinger" |
| 19 | //#define LOG_NDEBUG 0 |
Mikhail Naganov | 938be41 | 2019-09-04 11:38:47 -0700 | [diff] [blame] | 20 | #define ATRACE_TAG ATRACE_TAG_AUDIO |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 21 | |
Andy Hung | 0f725b4 | 2023-07-19 11:40:07 -0700 | [diff] [blame] | 22 | #include "MmapTracks.h" |
| 23 | #include "PlaybackTracks.h" |
| 24 | #include "RecordTracks.h" |
| 25 | |
| 26 | #include "Client.h" |
| 27 | #include "IAfEffect.h" |
| 28 | #include "IAfThread.h" |
| 29 | #include "ResamplerBufferProvider.h" |
| 30 | |
| 31 | #include <audio_utils/minifloat.h> |
| 32 | #include <media/AudioValidator.h> |
| 33 | #include <media/RecordBufferConverter.h> |
| 34 | #include <media/nbaio/Pipe.h> |
| 35 | #include <media/nbaio/PipeReader.h> |
| 36 | #include <mediautils/ServiceUtilities.h> |
| 37 | #include <mediautils/SharedMemoryAllocator.h> |
| 38 | #include <private/media/AudioTrackShared.h> |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 39 | #include <utils/Log.h> |
Mikhail Naganov | 938be41 | 2019-09-04 11:38:47 -0700 | [diff] [blame] | 40 | #include <utils/Trace.h> |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 41 | |
Andy Hung | 0f725b4 | 2023-07-19 11:40:07 -0700 | [diff] [blame] | 42 | #include <linux/futex.h> |
| 43 | #include <math.h> |
| 44 | #include <sys/syscall.h> |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 45 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 46 | // ---------------------------------------------------------------------------- |
| 47 | |
| 48 | // Note: the following macro is used for extremely verbose logging message. In |
| 49 | // order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to |
| 50 | // 0; but one side effect of this is to turn all LOGV's as well. Some messages |
| 51 | // are so verbose that we want to suppress them even when we have ALOG_ASSERT |
| 52 | // turned on. Do not uncomment the #def below unless you really know what you |
| 53 | // are doing and want to see all of the extremely verbose messages. |
| 54 | //#define VERY_VERY_VERBOSE_LOGGING |
| 55 | #ifdef VERY_VERY_VERBOSE_LOGGING |
| 56 | #define ALOGVV ALOGV |
| 57 | #else |
| 58 | #define ALOGVV(a...) do { } while(0) |
| 59 | #endif |
| 60 | |
Kuowei Li | d4adbdb | 2020-08-13 14:44:25 +0800 | [diff] [blame] | 61 | // TODO: Remove when this is put into AidlConversionUtil.h |
| 62 | #define VALUE_OR_RETURN_BINDER_STATUS(x) \ |
| 63 | ({ \ |
| 64 | auto _tmp = (x); \ |
| 65 | if (!_tmp.ok()) return ::android::aidl_utils::binderStatusFromStatusT(_tmp.error()); \ |
| 66 | std::move(_tmp.value()); \ |
| 67 | }) |
| 68 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 69 | namespace android { |
| 70 | |
Kuowei Li | d4adbdb | 2020-08-13 14:44:25 +0800 | [diff] [blame] | 71 | using ::android::aidl_utils::binderStatusFromStatusT; |
Ytai Ben-Tsvi | bdc293a | 2020-11-02 17:01:38 -0800 | [diff] [blame] | 72 | using binder::Status; |
Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 73 | using content::AttributionSourceState; |
Ivan Lozano | 8cf3a07 | 2017-08-09 09:01:33 -0700 | [diff] [blame] | 74 | using media::VolumeShaper; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 75 | // ---------------------------------------------------------------------------- |
| 76 | // TrackBase |
| 77 | // ---------------------------------------------------------------------------- |
Andy Hung | 9d84af5 | 2018-09-12 18:03:44 -0700 | [diff] [blame] | 78 | #undef LOG_TAG |
| 79 | #define LOG_TAG "AF::TrackBase" |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 80 | |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 81 | static volatile int32_t nextTrackId = 55; |
| 82 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 83 | // TrackBase constructor must be called with AudioFlinger::mLock held |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 84 | TrackBase::TrackBase( |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 85 | IAfThreadBase *thread, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 86 | const sp<Client>& client, |
Kevin Rocard | 1f564ac | 2018-03-29 13:53:10 -0700 | [diff] [blame] | 87 | const audio_attributes_t& attr, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 88 | uint32_t sampleRate, |
| 89 | audio_format_t format, |
| 90 | audio_channel_mask_t channelMask, |
| 91 | size_t frameCount, |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 92 | void *buffer, |
Andy Hung | 8fe6803 | 2017-06-05 16:17:51 -0700 | [diff] [blame] | 93 | size_t bufferSize, |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 94 | audio_session_t sessionId, |
Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 95 | pid_t creatorPid, |
Andy Hung | 1f12a8a | 2016-11-07 16:10:30 -0800 | [diff] [blame] | 96 | uid_t clientUid, |
Glenn Kasten | d776ac6 | 2014-05-07 09:16:09 -0700 | [diff] [blame] | 97 | bool isOut, |
Dmitry Sidorenkov | a41c273 | 2023-05-15 13:47:07 -0700 | [diff] [blame] | 98 | const alloc_type alloc, |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 99 | track_type type, |
Andy Hung | b68f5eb | 2019-12-03 16:49:17 -0800 | [diff] [blame] | 100 | audio_port_handle_t portId, |
| 101 | std::string metricsId) |
Andy Hung | d29af63 | 2023-06-23 19:27:19 -0700 | [diff] [blame] | 102 | : |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 103 | mThread(thread), |
Dmitry Sidorenkov | a41c273 | 2023-05-15 13:47:07 -0700 | [diff] [blame] | 104 | mAllocType(alloc), |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 105 | mClient(client), |
| 106 | mCblk(NULL), |
Andy Hung | 8fe6803 | 2017-06-05 16:17:51 -0700 | [diff] [blame] | 107 | // mBuffer, mBufferSize |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 108 | mState(IDLE), |
Kevin Rocard | 1f564ac | 2018-03-29 13:53:10 -0700 | [diff] [blame] | 109 | mAttr(attr), |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 110 | mSampleRate(sampleRate), |
| 111 | mFormat(format), |
| 112 | mChannelMask(channelMask), |
Andy Hung | e541269 | 2014-05-16 11:25:07 -0700 | [diff] [blame] | 113 | mChannelCount(isOut ? |
| 114 | audio_channel_count_from_out_mask(channelMask) : |
| 115 | audio_channel_count_from_in_mask(channelMask)), |
Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 116 | mFrameSize(audio_has_proportional_frames(format) ? |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 117 | mChannelCount * audio_bytes_per_sample(format) : sizeof(int8_t)), |
| 118 | mFrameCount(frameCount), |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 119 | mSessionId(sessionId), |
| 120 | mIsOut(isOut), |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 121 | mId(android_atomic_inc(&nextTrackId)), |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 122 | mTerminated(false), |
Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 123 | mType(type), |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 124 | mThreadIoHandle(thread ? thread->id() : AUDIO_IO_HANDLE_NONE), |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 125 | mPortId(portId), |
Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 126 | mIsInvalid(false), |
Kunal Malhotra | 3be6890 | 2023-02-28 22:03:15 +0000 | [diff] [blame] | 127 | mTrackMetrics(std::move(metricsId), isOut, clientUid), |
Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 128 | mCreatorPid(creatorPid) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 129 | { |
Marco Nelissen | dcb346b | 2015-09-09 10:47:29 -0700 | [diff] [blame] | 130 | const uid_t callingUid = IPCThreadState::self()->getCallingUid(); |
Andy Hung | 4ef19fa | 2018-05-15 19:35:29 -0700 | [diff] [blame] | 131 | if (!isAudioServerOrMediaServerUid(callingUid) || clientUid == AUDIO_UID_INVALID) { |
Andy Hung | 1f12a8a | 2016-11-07 16:10:30 -0800 | [diff] [blame] | 132 | ALOGW_IF(clientUid != AUDIO_UID_INVALID && clientUid != callingUid, |
Andy Hung | 9d84af5 | 2018-09-12 18:03:44 -0700 | [diff] [blame] | 133 | "%s(%d): uid %d tried to pass itself off as %d", |
| 134 | __func__, mId, callingUid, clientUid); |
Andy Hung | 1f12a8a | 2016-11-07 16:10:30 -0800 | [diff] [blame] | 135 | clientUid = callingUid; |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 136 | } |
| 137 | // clientUid contains the uid of the app that is responsible for this track, so we can blame |
| 138 | // battery usage on it. |
| 139 | mUid = clientUid; |
| 140 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 141 | // ALOGD("Creating track with %d buffers @ %d bytes", bufferCount, bufferSize); |
Andy Hung | 1883f69 | 2017-02-13 18:48:39 -0800 | [diff] [blame] | 142 | |
Andy Hung | 8fe6803 | 2017-06-05 16:17:51 -0700 | [diff] [blame] | 143 | size_t minBufferSize = buffer == NULL ? roundup(frameCount) : frameCount; |
Andy Hung | 1883f69 | 2017-02-13 18:48:39 -0800 | [diff] [blame] | 144 | // check overflow when computing bufferSize due to multiplication by mFrameSize. |
Andy Hung | 8fe6803 | 2017-06-05 16:17:51 -0700 | [diff] [blame] | 145 | if (minBufferSize < frameCount // roundup rounds down for values above UINT_MAX / 2 |
Andy Hung | 1883f69 | 2017-02-13 18:48:39 -0800 | [diff] [blame] | 146 | || mFrameSize == 0 // format needs to be correct |
Andy Hung | 8fe6803 | 2017-06-05 16:17:51 -0700 | [diff] [blame] | 147 | || minBufferSize > SIZE_MAX / mFrameSize) { |
Andy Hung | 1883f69 | 2017-02-13 18:48:39 -0800 | [diff] [blame] | 148 | android_errorWriteLog(0x534e4554, "34749571"); |
| 149 | return; |
| 150 | } |
Andy Hung | 8fe6803 | 2017-06-05 16:17:51 -0700 | [diff] [blame] | 151 | minBufferSize *= mFrameSize; |
| 152 | |
| 153 | if (buffer == nullptr) { |
| 154 | bufferSize = minBufferSize; // allocated here. |
| 155 | } else if (minBufferSize > bufferSize) { |
| 156 | android_errorWriteLog(0x534e4554, "38340117"); |
| 157 | return; |
| 158 | } |
Andy Hung | 1883f69 | 2017-02-13 18:48:39 -0800 | [diff] [blame] | 159 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 160 | size_t size = sizeof(audio_track_cblk_t); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 161 | if (buffer == NULL && alloc == ALLOC_CBLK) { |
Andy Hung | 1883f69 | 2017-02-13 18:48:39 -0800 | [diff] [blame] | 162 | // check overflow when computing allocation size for streaming tracks. |
| 163 | if (size > SIZE_MAX - bufferSize) { |
| 164 | android_errorWriteLog(0x534e4554, "34749571"); |
| 165 | return; |
| 166 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 167 | size += bufferSize; |
| 168 | } |
| 169 | |
| 170 | if (client != 0) { |
Atneya | 3c61d88 | 2021-09-20 14:52:15 -0400 | [diff] [blame] | 171 | mCblkMemory = client->allocator().allocate(mediautils::NamedAllocRequest{{size}, |
| 172 | std::string("Track ID: ").append(std::to_string(mId))}); |
Glenn Kasten | 663c224 | 2013-09-24 11:52:37 -0700 | [diff] [blame] | 173 | if (mCblkMemory == 0 || |
Ytai Ben-Tsvi | 7dd3972 | 2019-09-05 15:14:30 -0700 | [diff] [blame] | 174 | (mCblk = static_cast<audio_track_cblk_t *>(mCblkMemory->unsecurePointer())) == NULL) { |
Andy Hung | 9d84af5 | 2018-09-12 18:03:44 -0700 | [diff] [blame] | 175 | ALOGE("%s(%d): not enough memory for AudioTrack size=%zu", __func__, mId, size); |
Atneya | 3c61d88 | 2021-09-20 14:52:15 -0400 | [diff] [blame] | 176 | ALOGE("%s", client->allocator().dump().c_str()); |
Glenn Kasten | 663c224 | 2013-09-24 11:52:37 -0700 | [diff] [blame] | 177 | mCblkMemory.clear(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 178 | return; |
| 179 | } |
| 180 | } else { |
Andy Hung | afb3148 | 2017-02-13 18:50:48 -0800 | [diff] [blame] | 181 | mCblk = (audio_track_cblk_t *) malloc(size); |
| 182 | if (mCblk == NULL) { |
Andy Hung | 9d84af5 | 2018-09-12 18:03:44 -0700 | [diff] [blame] | 183 | ALOGE("%s(%d): not enough memory for AudioTrack size=%zu", __func__, mId, size); |
Andy Hung | afb3148 | 2017-02-13 18:50:48 -0800 | [diff] [blame] | 184 | return; |
| 185 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | // construct the shared structure in-place. |
| 189 | if (mCblk != NULL) { |
| 190 | new(mCblk) audio_track_cblk_t(); |
Glenn Kasten | c263ca0 | 2014-06-04 20:31:46 -0700 | [diff] [blame] | 191 | switch (alloc) { |
| 192 | case ALLOC_READONLY: { |
Glenn Kasten | d776ac6 | 2014-05-07 09:16:09 -0700 | [diff] [blame] | 193 | const sp<MemoryDealer> roHeap(thread->readOnlyHeap()); |
| 194 | if (roHeap == 0 || |
| 195 | (mBufferMemory = roHeap->allocate(bufferSize)) == 0 || |
Ytai Ben-Tsvi | 7dd3972 | 2019-09-05 15:14:30 -0700 | [diff] [blame] | 196 | (mBuffer = mBufferMemory->unsecurePointer()) == NULL) { |
Andy Hung | 9d84af5 | 2018-09-12 18:03:44 -0700 | [diff] [blame] | 197 | ALOGE("%s(%d): not enough memory for read-only buffer size=%zu", |
| 198 | __func__, mId, bufferSize); |
Glenn Kasten | d776ac6 | 2014-05-07 09:16:09 -0700 | [diff] [blame] | 199 | if (roHeap != 0) { |
| 200 | roHeap->dump("buffer"); |
| 201 | } |
| 202 | mCblkMemory.clear(); |
| 203 | mBufferMemory.clear(); |
| 204 | return; |
| 205 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 206 | memset(mBuffer, 0, bufferSize); |
Glenn Kasten | c263ca0 | 2014-06-04 20:31:46 -0700 | [diff] [blame] | 207 | } break; |
| 208 | case ALLOC_PIPE: |
| 209 | mBufferMemory = thread->pipeMemory(); |
| 210 | // mBuffer is the virtual address as seen from current process (mediaserver), |
Ytai Ben-Tsvi | 7dd3972 | 2019-09-05 15:14:30 -0700 | [diff] [blame] | 211 | // and should normally be coming from mBufferMemory->unsecurePointer(). |
Glenn Kasten | c263ca0 | 2014-06-04 20:31:46 -0700 | [diff] [blame] | 212 | // However in this case the TrackBase does not reference the buffer directly. |
| 213 | // It should references the buffer via the pipe. |
| 214 | // Therefore, to detect incorrect usage of the buffer, we set mBuffer to NULL. |
| 215 | mBuffer = NULL; |
Andy Hung | 8fe6803 | 2017-06-05 16:17:51 -0700 | [diff] [blame] | 216 | bufferSize = 0; |
Glenn Kasten | c263ca0 | 2014-06-04 20:31:46 -0700 | [diff] [blame] | 217 | break; |
| 218 | case ALLOC_CBLK: |
Glenn Kasten | d776ac6 | 2014-05-07 09:16:09 -0700 | [diff] [blame] | 219 | // clear all buffers |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 220 | if (buffer == NULL) { |
Glenn Kasten | d776ac6 | 2014-05-07 09:16:09 -0700 | [diff] [blame] | 221 | mBuffer = (char*)mCblk + sizeof(audio_track_cblk_t); |
| 222 | memset(mBuffer, 0, bufferSize); |
| 223 | } else { |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 224 | mBuffer = buffer; |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 225 | #if 0 |
Glenn Kasten | d776ac6 | 2014-05-07 09:16:09 -0700 | [diff] [blame] | 226 | mCblk->mFlags = CBLK_FORCEREADY; // FIXME hack, need to fix the track ready logic |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 227 | #endif |
Glenn Kasten | d776ac6 | 2014-05-07 09:16:09 -0700 | [diff] [blame] | 228 | } |
Glenn Kasten | c263ca0 | 2014-06-04 20:31:46 -0700 | [diff] [blame] | 229 | break; |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 230 | case ALLOC_LOCAL: |
| 231 | mBuffer = calloc(1, bufferSize); |
| 232 | break; |
| 233 | case ALLOC_NONE: |
| 234 | mBuffer = buffer; |
| 235 | break; |
Andy Hung | 8fe6803 | 2017-06-05 16:17:51 -0700 | [diff] [blame] | 236 | default: |
Andy Hung | 9d84af5 | 2018-09-12 18:03:44 -0700 | [diff] [blame] | 237 | LOG_ALWAYS_FATAL("%s(%d): invalid allocation type: %d", __func__, mId, (int)alloc); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 238 | } |
Andy Hung | 8fe6803 | 2017-06-05 16:17:51 -0700 | [diff] [blame] | 239 | mBufferSize = bufferSize; |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 240 | |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 241 | #ifdef TEE_SINK |
Andy Hung | 8946a28 | 2018-04-19 20:04:56 -0700 | [diff] [blame] | 242 | mTee.set(sampleRate, mChannelCount, format, NBAIO_Tee::TEE_FLAG_TRACK); |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 243 | #endif |
Andy Hung | 959b5b8 | 2021-09-24 10:46:20 -0700 | [diff] [blame] | 244 | // mState is mirrored for the client to read. |
| 245 | mState.setMirror(&mCblk->mState); |
| 246 | // ensure our state matches up until we consolidate the enumeration. |
| 247 | static_assert(CBLK_STATE_IDLE == IDLE); |
| 248 | static_assert(CBLK_STATE_PAUSING == PAUSING); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 249 | } |
| 250 | } |
| 251 | |
Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 252 | // TODO b/182392769: use attribution source util |
| 253 | static AttributionSourceState audioServerAttributionSource(pid_t pid) { |
| 254 | AttributionSourceState attributionSource{}; |
| 255 | attributionSource.uid = AID_AUDIOSERVER; |
| 256 | attributionSource.pid = pid; |
| 257 | attributionSource.token = sp<BBinder>::make(); |
| 258 | return attributionSource; |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 259 | } |
| 260 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 261 | status_t TrackBase::initCheck() const |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 262 | { |
| 263 | status_t status; |
| 264 | if (mType == TYPE_OUTPUT || mType == TYPE_PATCH) { |
| 265 | status = cblk() != NULL ? NO_ERROR : NO_MEMORY; |
| 266 | } else { |
| 267 | status = getCblk() != 0 ? NO_ERROR : NO_MEMORY; |
| 268 | } |
| 269 | return status; |
| 270 | } |
| 271 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 272 | TrackBase::~TrackBase() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 273 | { |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 274 | // delete the proxy before deleting the shared memory it refers to, to avoid dangling reference |
Eric Laurent | 5bba2f6 | 2016-03-18 11:14:14 -0700 | [diff] [blame] | 275 | mServerProxy.clear(); |
Andy Hung | 689e82c | 2019-08-21 17:53:17 -0700 | [diff] [blame] | 276 | releaseCblk(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 277 | mCblkMemory.clear(); // free the shared memory before releasing the heap it belongs to |
| 278 | if (mClient != 0) { |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 279 | // Client destructor must run with AudioFlinger client mutex locked |
Andy Hung | 954b971 | 2023-08-28 18:36:53 -0700 | [diff] [blame] | 280 | audio_utils::lock_guard _l(mClient->afClientCallback()->clientMutex()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 281 | // If the client's reference count drops to zero, the associated destructor |
| 282 | // must run with AudioFlinger lock held. Thus the explicit clear() rather than |
| 283 | // relying on the automatic clear() at end of scope. |
| 284 | mClient.clear(); |
| 285 | } |
Dmitry Sidorenkov | a41c273 | 2023-05-15 13:47:07 -0700 | [diff] [blame] | 286 | if (mAllocType == ALLOC_LOCAL) { |
| 287 | free(mBuffer); |
| 288 | mBuffer = nullptr; |
| 289 | } |
Eric Laurent | 3bcffa1 | 2014-06-12 18:38:45 -0700 | [diff] [blame] | 290 | // flush the binder command buffer |
| 291 | IPCThreadState::self()->flushCommands(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | // AudioBufferProvider interface |
| 295 | // getNextBuffer() = 0; |
Glenn Kasten | d79072e | 2016-01-06 08:41:20 -0800 | [diff] [blame] | 296 | // This implementation of releaseBuffer() is used by Track and RecordTrack |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 297 | void TrackBase::releaseBuffer(AudioBufferProvider::Buffer* buffer) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 298 | { |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 299 | #ifdef TEE_SINK |
Andy Hung | 8946a28 | 2018-04-19 20:04:56 -0700 | [diff] [blame] | 300 | mTee.write(buffer->raw, buffer->frameCount); |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 301 | #endif |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 302 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 303 | ServerProxy::Buffer buf; |
| 304 | buf.mFrameCount = buffer->frameCount; |
| 305 | buf.mRaw = buffer->raw; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 306 | buffer->frameCount = 0; |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 307 | buffer->raw = NULL; |
| 308 | mServerProxy->releaseBuffer(&buf); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 309 | } |
| 310 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 311 | status_t TrackBase::setSyncEvent( |
Andy Hung | 068e08e | 2023-05-15 19:02:55 -0700 | [diff] [blame] | 312 | const sp<audioflinger::SyncEvent>& event) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 313 | { |
Andy Hung | 068e08e | 2023-05-15 19:02:55 -0700 | [diff] [blame] | 314 | mSyncEvents.emplace_back(event); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 315 | return NO_ERROR; |
| 316 | } |
| 317 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 318 | PatchTrackBase::PatchTrackBase(const sp<ClientProxy>& proxy, |
Andy Hung | 4fd6901 | 2023-07-14 16:57:01 -0700 | [diff] [blame] | 319 | IAfThreadBase* thread, const Timeout& timeout) |
Kevin Rocard | 45986c7 | 2018-12-18 18:22:59 -0800 | [diff] [blame] | 320 | : mProxy(proxy) |
| 321 | { |
| 322 | if (timeout) { |
| 323 | setPeerTimeout(*timeout); |
| 324 | } else { |
| 325 | // Double buffer mixer |
Andy Hung | 4fd6901 | 2023-07-14 16:57:01 -0700 | [diff] [blame] | 326 | uint64_t mixBufferNs = ((uint64_t)2 * thread->frameCount() * 1000000000) / |
| 327 | thread->sampleRate(); |
Kevin Rocard | 45986c7 | 2018-12-18 18:22:59 -0800 | [diff] [blame] | 328 | setPeerTimeout(std::chrono::nanoseconds{mixBufferNs}); |
| 329 | } |
| 330 | } |
| 331 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 332 | void PatchTrackBase::setPeerTimeout(std::chrono::nanoseconds timeout) { |
Kevin Rocard | 45986c7 | 2018-12-18 18:22:59 -0800 | [diff] [blame] | 333 | mPeerTimeout.tv_sec = timeout.count() / std::nano::den; |
| 334 | mPeerTimeout.tv_nsec = timeout.count() % std::nano::den; |
| 335 | } |
| 336 | |
| 337 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 338 | // ---------------------------------------------------------------------------- |
| 339 | // Playback |
| 340 | // ---------------------------------------------------------------------------- |
Andy Hung | 9d84af5 | 2018-09-12 18:03:44 -0700 | [diff] [blame] | 341 | #undef LOG_TAG |
| 342 | #define LOG_TAG "AF::TrackHandle" |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 343 | |
Andy Hung | a5a7fc9 | 2023-06-23 19:27:19 -0700 | [diff] [blame] | 344 | class TrackHandle : public android::media::BnAudioTrack { |
| 345 | public: |
Andy Hung | d29af63 | 2023-06-23 19:27:19 -0700 | [diff] [blame] | 346 | explicit TrackHandle(const sp<IAfTrack>& track); |
Andy Hung | a5a7fc9 | 2023-06-23 19:27:19 -0700 | [diff] [blame] | 347 | ~TrackHandle() override; |
| 348 | |
| 349 | binder::Status getCblk(std::optional<media::SharedFileRegion>* _aidl_return) final; |
| 350 | binder::Status start(int32_t* _aidl_return) final; |
| 351 | binder::Status stop() final; |
| 352 | binder::Status flush() final; |
| 353 | binder::Status pause() final; |
| 354 | binder::Status attachAuxEffect(int32_t effectId, int32_t* _aidl_return) final; |
| 355 | binder::Status setParameters(const std::string& keyValuePairs, |
| 356 | int32_t* _aidl_return) final; |
| 357 | binder::Status selectPresentation(int32_t presentationId, int32_t programId, |
| 358 | int32_t* _aidl_return) final; |
| 359 | binder::Status getTimestamp(media::AudioTimestampInternal* timestamp, |
| 360 | int32_t* _aidl_return) final; |
| 361 | binder::Status signal() final; |
| 362 | binder::Status applyVolumeShaper(const media::VolumeShaperConfiguration& configuration, |
| 363 | const media::VolumeShaperOperation& operation, |
| 364 | int32_t* _aidl_return) final; |
| 365 | binder::Status getVolumeShaperState( |
| 366 | int32_t id, |
| 367 | std::optional<media::VolumeShaperState>* _aidl_return) final; |
| 368 | binder::Status getDualMonoMode( |
| 369 | media::audio::common::AudioDualMonoMode* _aidl_return) final; |
| 370 | binder::Status setDualMonoMode( |
| 371 | media::audio::common::AudioDualMonoMode mode) final; |
| 372 | binder::Status getAudioDescriptionMixLevel(float* _aidl_return) final; |
| 373 | binder::Status setAudioDescriptionMixLevel(float leveldB) final; |
| 374 | binder::Status getPlaybackRateParameters( |
| 375 | media::audio::common::AudioPlaybackRate* _aidl_return) final; |
| 376 | binder::Status setPlaybackRateParameters( |
| 377 | const media::audio::common::AudioPlaybackRate& playbackRate) final; |
| 378 | |
| 379 | private: |
Andy Hung | d29af63 | 2023-06-23 19:27:19 -0700 | [diff] [blame] | 380 | const sp<IAfTrack> mTrack; |
Andy Hung | a5a7fc9 | 2023-06-23 19:27:19 -0700 | [diff] [blame] | 381 | }; |
| 382 | |
| 383 | /* static */ |
Andy Hung | d29af63 | 2023-06-23 19:27:19 -0700 | [diff] [blame] | 384 | sp<media::IAudioTrack> IAfTrack::createIAudioTrackAdapter(const sp<IAfTrack>& track) { |
Andy Hung | a5a7fc9 | 2023-06-23 19:27:19 -0700 | [diff] [blame] | 385 | return sp<TrackHandle>::make(track); |
| 386 | } |
| 387 | |
Andy Hung | d29af63 | 2023-06-23 19:27:19 -0700 | [diff] [blame] | 388 | TrackHandle::TrackHandle(const sp<IAfTrack>& track) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 389 | : BnAudioTrack(), |
| 390 | mTrack(track) |
| 391 | { |
Andy Hung | 225aef6 | 2022-12-06 16:33:20 -0800 | [diff] [blame] | 392 | setMinSchedulerPolicy(SCHED_NORMAL, ANDROID_PRIORITY_AUDIO); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 393 | } |
| 394 | |
Andy Hung | a5a7fc9 | 2023-06-23 19:27:19 -0700 | [diff] [blame] | 395 | TrackHandle::~TrackHandle() { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 396 | // 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 Hung | a5a7fc9 | 2023-06-23 19:27:19 -0700 | [diff] [blame] | 403 | Status TrackHandle::getCblk( |
Ytai Ben-Tsvi | bdc293a | 2020-11-02 17:01:38 -0800 | [diff] [blame] | 404 | std::optional<media::SharedFileRegion>* _aidl_return) { |
| 405 | *_aidl_return = legacy2aidl_NullableIMemory_SharedFileRegion(mTrack->getCblk()).value(); |
| 406 | return Status::ok(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 407 | } |
| 408 | |
Andy Hung | a5a7fc9 | 2023-06-23 19:27:19 -0700 | [diff] [blame] | 409 | Status TrackHandle::start(int32_t* _aidl_return) { |
Ytai Ben-Tsvi | bdc293a | 2020-11-02 17:01:38 -0800 | [diff] [blame] | 410 | *_aidl_return = mTrack->start(); |
| 411 | return Status::ok(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 412 | } |
| 413 | |
Andy Hung | a5a7fc9 | 2023-06-23 19:27:19 -0700 | [diff] [blame] | 414 | Status TrackHandle::stop() { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 415 | mTrack->stop(); |
Ytai Ben-Tsvi | bdc293a | 2020-11-02 17:01:38 -0800 | [diff] [blame] | 416 | return Status::ok(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 417 | } |
| 418 | |
Andy Hung | a5a7fc9 | 2023-06-23 19:27:19 -0700 | [diff] [blame] | 419 | Status TrackHandle::flush() { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 420 | mTrack->flush(); |
Ytai Ben-Tsvi | bdc293a | 2020-11-02 17:01:38 -0800 | [diff] [blame] | 421 | return Status::ok(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 422 | } |
| 423 | |
Andy Hung | a5a7fc9 | 2023-06-23 19:27:19 -0700 | [diff] [blame] | 424 | Status TrackHandle::pause() { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 425 | mTrack->pause(); |
Ytai Ben-Tsvi | bdc293a | 2020-11-02 17:01:38 -0800 | [diff] [blame] | 426 | return Status::ok(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 427 | } |
| 428 | |
Andy Hung | a5a7fc9 | 2023-06-23 19:27:19 -0700 | [diff] [blame] | 429 | Status TrackHandle::attachAuxEffect(int32_t effectId, |
Ytai Ben-Tsvi | bdc293a | 2020-11-02 17:01:38 -0800 | [diff] [blame] | 430 | int32_t* _aidl_return) { |
| 431 | *_aidl_return = mTrack->attachAuxEffect(effectId); |
| 432 | return Status::ok(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 433 | } |
| 434 | |
Andy Hung | a5a7fc9 | 2023-06-23 19:27:19 -0700 | [diff] [blame] | 435 | Status TrackHandle::setParameters(const std::string& keyValuePairs, |
Ytai Ben-Tsvi | bdc293a | 2020-11-02 17:01:38 -0800 | [diff] [blame] | 436 | int32_t* _aidl_return) { |
| 437 | *_aidl_return = mTrack->setParameters(String8(keyValuePairs.c_str())); |
| 438 | return Status::ok(); |
Glenn Kasten | 3dcd00d | 2013-07-17 10:10:23 -0700 | [diff] [blame] | 439 | } |
| 440 | |
Andy Hung | a5a7fc9 | 2023-06-23 19:27:19 -0700 | [diff] [blame] | 441 | Status TrackHandle::selectPresentation(int32_t presentationId, int32_t programId, |
Ytai Ben-Tsvi | bdc293a | 2020-11-02 17:01:38 -0800 | [diff] [blame] | 442 | int32_t* _aidl_return) { |
| 443 | *_aidl_return = mTrack->selectPresentation(presentationId, programId); |
| 444 | return Status::ok(); |
Mikhail Naganov | ac917ac | 2018-11-28 14:03:52 -0800 | [diff] [blame] | 445 | } |
| 446 | |
Andy Hung | a5a7fc9 | 2023-06-23 19:27:19 -0700 | [diff] [blame] | 447 | Status TrackHandle::getTimestamp(media::AudioTimestampInternal* timestamp, |
Ytai Ben-Tsvi | bdc293a | 2020-11-02 17:01:38 -0800 | [diff] [blame] | 448 | int32_t* _aidl_return) { |
| 449 | AudioTimestamp legacy; |
| 450 | *_aidl_return = mTrack->getTimestamp(legacy); |
| 451 | if (*_aidl_return != OK) { |
| 452 | return Status::ok(); |
| 453 | } |
Andy Hung | 973638a | 2020-12-08 20:47:45 -0800 | [diff] [blame] | 454 | *timestamp = legacy2aidl_AudioTimestamp_AudioTimestampInternal(legacy).value(); |
Ytai Ben-Tsvi | bdc293a | 2020-11-02 17:01:38 -0800 | [diff] [blame] | 455 | return Status::ok(); |
Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 456 | } |
| 457 | |
Andy Hung | a5a7fc9 | 2023-06-23 19:27:19 -0700 | [diff] [blame] | 458 | Status TrackHandle::signal() { |
Ytai Ben-Tsvi | bdc293a | 2020-11-02 17:01:38 -0800 | [diff] [blame] | 459 | mTrack->signal(); |
| 460 | return Status::ok(); |
Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 461 | } |
| 462 | |
Andy Hung | a5a7fc9 | 2023-06-23 19:27:19 -0700 | [diff] [blame] | 463 | Status TrackHandle::applyVolumeShaper( |
Ytai Ben-Tsvi | bdc293a | 2020-11-02 17:01:38 -0800 | [diff] [blame] | 464 | 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 Kasten | 53cec22 | 2013-08-29 09:01:02 -0700 | [diff] [blame] | 481 | } |
| 482 | |
Andy Hung | a5a7fc9 | 2023-06-23 19:27:19 -0700 | [diff] [blame] | 483 | Status TrackHandle::getVolumeShaperState( |
Ytai Ben-Tsvi | bdc293a | 2020-11-02 17:01:38 -0800 | [diff] [blame] | 484 | 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 Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 495 | } |
| 496 | |
Andy Hung | a5a7fc9 | 2023-06-23 19:27:19 -0700 | [diff] [blame] | 497 | Status TrackHandle::getDualMonoMode( |
Mikhail Naganov | f53e182 | 2022-12-18 02:48:14 +0000 | [diff] [blame] | 498 | media::audio::common::AudioDualMonoMode* _aidl_return) |
Kuowei Li | d4adbdb | 2020-08-13 14:44:25 +0800 | [diff] [blame] | 499 | { |
| 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 Hung | a5a7fc9 | 2023-06-23 19:27:19 -0700 | [diff] [blame] | 510 | Status TrackHandle::setDualMonoMode( |
Mikhail Naganov | f53e182 | 2022-12-18 02:48:14 +0000 | [diff] [blame] | 511 | media::audio::common::AudioDualMonoMode mode) |
Kuowei Li | d4adbdb | 2020-08-13 14:44:25 +0800 | [diff] [blame] | 512 | { |
| 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 Hung | a5a7fc9 | 2023-06-23 19:27:19 -0700 | [diff] [blame] | 519 | Status TrackHandle::getAudioDescriptionMixLevel(float* _aidl_return) |
Kuowei Li | d4adbdb | 2020-08-13 14:44:25 +0800 | [diff] [blame] | 520 | { |
| 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 Hung | a5a7fc9 | 2023-06-23 19:27:19 -0700 | [diff] [blame] | 528 | Status TrackHandle::setAudioDescriptionMixLevel(float leveldB) |
Kuowei Li | d4adbdb | 2020-08-13 14:44:25 +0800 | [diff] [blame] | 529 | { |
| 530 | return binderStatusFromStatusT(AudioValidator::validateAudioDescriptionMixLevel(leveldB) |
| 531 | ?: mTrack->setAudioDescriptionMixLevel(leveldB)); |
| 532 | } |
| 533 | |
Andy Hung | a5a7fc9 | 2023-06-23 19:27:19 -0700 | [diff] [blame] | 534 | Status TrackHandle::getPlaybackRateParameters( |
Mikhail Naganov | f53e182 | 2022-12-18 02:48:14 +0000 | [diff] [blame] | 535 | media::audio::common::AudioPlaybackRate* _aidl_return) |
Kuowei Li | d4adbdb | 2020-08-13 14:44:25 +0800 | [diff] [blame] | 536 | { |
| 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 Hung | a5a7fc9 | 2023-06-23 19:27:19 -0700 | [diff] [blame] | 547 | Status TrackHandle::setPlaybackRateParameters( |
Mikhail Naganov | f53e182 | 2022-12-18 02:48:14 +0000 | [diff] [blame] | 548 | const media::audio::common::AudioPlaybackRate& playbackRate) |
Kuowei Li | d4adbdb | 2020-08-13 14:44:25 +0800 | [diff] [blame] | 549 | { |
| 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 Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 556 | // ---------------------------------------------------------------------------- |
Jean-Michel Trivi | 74e01fa | 2019-02-25 12:18:09 -0800 | [diff] [blame] | 557 | // AppOp for audio playback |
| 558 | // ------------------------------- |
Mikhail Naganov | f7e3a3a | 2019-04-22 16:43:26 -0700 | [diff] [blame] | 559 | |
| 560 | // static |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 561 | sp<OpPlayAudioMonitor> OpPlayAudioMonitor::createIfNeeded( |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 562 | IAfThreadBase* thread, |
Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 563 | const AttributionSourceState& attributionSource, const audio_attributes_t& attr, int id, |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 564 | audio_stream_type_t streamType) |
Jean-Michel Trivi | 74e01fa | 2019-02-25 12:18:09 -0800 | [diff] [blame] | 565 | { |
Vlad Popa | 9d7c6f8 | 2023-07-10 20:27:41 -0700 | [diff] [blame] | 566 | Vector<String16> packages; |
| 567 | const uid_t uid = VALUE_OR_FATAL(aidl2legacy_int32_t_uid_t(attributionSource.uid)); |
Colin Cross | b8a9dbb | 2020-08-27 04:12:26 +0000 | [diff] [blame] | 568 | getPackagesForUid(uid, packages); |
Eric Laurent | 9066ad3 | 2019-05-20 14:40:10 -0700 | [diff] [blame] | 569 | if (isServiceUid(uid)) { |
Eric Laurent | 9066ad3 | 2019-05-20 14:40:10 -0700 | [diff] [blame] | 570 | if (packages.isEmpty()) { |
Shunkai Yao | af7990a | 2023-08-18 02:24:01 +0000 | [diff] [blame] | 571 | ALOGW("OpPlayAudio: not muting track:%d usage:%d for service UID %d", id, attr.usage, |
Eric Laurent | 9066ad3 | 2019-05-20 14:40:10 -0700 | [diff] [blame] | 572 | uid); |
| 573 | return nullptr; |
| 574 | } |
Jean-Michel Trivi | 74e01fa | 2019-02-25 12:18:09 -0800 | [diff] [blame] | 575 | } |
| 576 | // stream type has been filtered by audio policy to indicate whether it can be muted |
| 577 | if (streamType == AUDIO_STREAM_ENFORCED_AUDIBLE) { |
Eric Laurent | 2dab030 | 2019-05-08 18:15:55 -0700 | [diff] [blame] | 578 | ALOGD("OpPlayAudio: not muting track:%d usage:%d ENFORCED_AUDIBLE", id, attr.usage); |
Mikhail Naganov | f7e3a3a | 2019-04-22 16:43:26 -0700 | [diff] [blame] | 579 | return nullptr; |
Jean-Michel Trivi | 74e01fa | 2019-02-25 12:18:09 -0800 | [diff] [blame] | 580 | } |
Eric Laurent | 2dab030 | 2019-05-08 18:15:55 -0700 | [diff] [blame] | 581 | if ((attr.flags & AUDIO_FLAG_BYPASS_INTERRUPTION_POLICY) |
| 582 | == AUDIO_FLAG_BYPASS_INTERRUPTION_POLICY) { |
| 583 | ALOGD("OpPlayAudio: not muting track:%d flags %#x have FLAG_BYPASS_INTERRUPTION_POLICY", |
| 584 | id, attr.flags); |
| 585 | return nullptr; |
| 586 | } |
Vlad Popa | 9d7c6f8 | 2023-07-10 20:27:41 -0700 | [diff] [blame] | 587 | return sp<OpPlayAudioMonitor>::make(thread, attributionSource, attr.usage, id, uid); |
Mikhail Naganov | f7e3a3a | 2019-04-22 16:43:26 -0700 | [diff] [blame] | 588 | } |
| 589 | |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 590 | OpPlayAudioMonitor::OpPlayAudioMonitor(IAfThreadBase* thread, |
Vlad Popa | 9d7c6f8 | 2023-07-10 20:27:41 -0700 | [diff] [blame] | 591 | const AttributionSourceState& attributionSource, |
| 592 | audio_usage_t usage, int id, uid_t uid) |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 593 | : mThread(wp<IAfThreadBase>::fromExisting(thread)), |
Vlad Popa | 9d7c6f8 | 2023-07-10 20:27:41 -0700 | [diff] [blame] | 594 | mHasOpPlayAudio(true), |
Vlad Popa | 9d7c6f8 | 2023-07-10 20:27:41 -0700 | [diff] [blame] | 595 | mUsage((int32_t)usage), |
| 596 | mId(id), |
| 597 | mUid(uid), |
| 598 | mPackageName(VALUE_OR_FATAL(aidl2legacy_string_view_String16( |
| 599 | attributionSource.packageName.value_or("")))) {} |
Jean-Michel Trivi | 74e01fa | 2019-02-25 12:18:09 -0800 | [diff] [blame] | 600 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 601 | OpPlayAudioMonitor::~OpPlayAudioMonitor() |
Jean-Michel Trivi | 74e01fa | 2019-02-25 12:18:09 -0800 | [diff] [blame] | 602 | { |
| 603 | if (mOpCallback != 0) { |
| 604 | mAppOpsManager.stopWatchingMode(mOpCallback); |
| 605 | } |
| 606 | mOpCallback.clear(); |
| 607 | } |
| 608 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 609 | void OpPlayAudioMonitor::onFirstRef() |
Mikhail Naganov | f7e3a3a | 2019-04-22 16:43:26 -0700 | [diff] [blame] | 610 | { |
Vlad Popa | d585959 | 2023-08-02 18:36:04 -0700 | [diff] [blame] | 611 | // make sure not to broadcast the initial state since it is not needed and could |
| 612 | // cause a deadlock since this method can be called with the mThread->mLock held |
| 613 | checkPlayAudioForUsage(/*doBroadcast=*/false); |
Shunkai Yao | af7990a | 2023-08-18 02:24:01 +0000 | [diff] [blame] | 614 | if (mPackageName.size()) { |
Mikhail Naganov | f7e3a3a | 2019-04-22 16:43:26 -0700 | [diff] [blame] | 615 | mOpCallback = new PlayAudioOpCallback(this); |
Shunkai Yao | af7990a | 2023-08-18 02:24:01 +0000 | [diff] [blame] | 616 | mAppOpsManager.startWatchingMode(AppOpsManager::OP_PLAY_AUDIO, mPackageName, mOpCallback); |
| 617 | } else { |
| 618 | ALOGW("Skipping OpPlayAudioMonitor due to null package name"); |
Mikhail Naganov | f7e3a3a | 2019-04-22 16:43:26 -0700 | [diff] [blame] | 619 | } |
| 620 | } |
| 621 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 622 | bool OpPlayAudioMonitor::hasOpPlayAudio() const { |
Jean-Michel Trivi | 74e01fa | 2019-02-25 12:18:09 -0800 | [diff] [blame] | 623 | return mHasOpPlayAudio.load(); |
| 624 | } |
| 625 | |
Jean-Michel Trivi | ddf87ef | 2019-08-20 15:42:04 -0700 | [diff] [blame] | 626 | // Note this method is never called (and never to be) for audio server / patch record track |
Jean-Michel Trivi | 74e01fa | 2019-02-25 12:18:09 -0800 | [diff] [blame] | 627 | // - not called from constructor due to check on UID, |
| 628 | // - not called from PlayAudioOpCallback because the callback is not installed in this case |
Shunkai Yao | af7990a | 2023-08-18 02:24:01 +0000 | [diff] [blame] | 629 | void OpPlayAudioMonitor::checkPlayAudioForUsage(bool doBroadcast) { |
| 630 | const bool hasAppOps = |
| 631 | mPackageName.size() && |
| 632 | mAppOpsManager.checkAudioOpNoThrow(AppOpsManager::OP_PLAY_AUDIO, mUsage, mUid, |
| 633 | mPackageName) == AppOpsManager::MODE_ALLOWED; |
Vlad Popa | 9d7c6f8 | 2023-07-10 20:27:41 -0700 | [diff] [blame] | 634 | |
| 635 | bool shouldChange = !hasAppOps; // check if we need to update. |
| 636 | if (mHasOpPlayAudio.compare_exchange_strong(shouldChange, hasAppOps)) { |
Shunkai Yao | af7990a | 2023-08-18 02:24:01 +0000 | [diff] [blame] | 637 | ALOGI("OpPlayAudio: track:%d package:%s usage:%d %smuted", mId, |
Tomasz Wasilczyk | b61c721 | 2023-09-08 17:32:11 +0000 | [diff] [blame] | 638 | String8(mPackageName).c_str(), mUsage, hasAppOps ? "not " : ""); |
Vlad Popa | d585959 | 2023-08-02 18:36:04 -0700 | [diff] [blame] | 639 | if (doBroadcast) { |
| 640 | auto thread = mThread.promote(); |
| 641 | if (thread != nullptr && thread->type() == IAfThreadBase::OFFLOAD) { |
| 642 | // Wake up Thread if offloaded, otherwise it may be several seconds for update. |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 643 | audio_utils::lock_guard _l(thread->mutex()); |
Vlad Popa | d585959 | 2023-08-02 18:36:04 -0700 | [diff] [blame] | 644 | thread->broadcast_l(); |
| 645 | } |
Vlad Popa | 9d7c6f8 | 2023-07-10 20:27:41 -0700 | [diff] [blame] | 646 | } |
Jean-Michel Trivi | 74e01fa | 2019-02-25 12:18:09 -0800 | [diff] [blame] | 647 | } |
| 648 | } |
| 649 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 650 | OpPlayAudioMonitor::PlayAudioOpCallback::PlayAudioOpCallback( |
Jean-Michel Trivi | 74e01fa | 2019-02-25 12:18:09 -0800 | [diff] [blame] | 651 | const wp<OpPlayAudioMonitor>& monitor) : mMonitor(monitor) |
| 652 | { } |
| 653 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 654 | void OpPlayAudioMonitor::PlayAudioOpCallback::opChanged(int32_t op, |
Jean-Michel Trivi | 74e01fa | 2019-02-25 12:18:09 -0800 | [diff] [blame] | 655 | const String16& packageName) { |
Jean-Michel Trivi | 74e01fa | 2019-02-25 12:18:09 -0800 | [diff] [blame] | 656 | if (op != AppOpsManager::OP_PLAY_AUDIO) { |
| 657 | return; |
| 658 | } |
Shunkai Yao | af7990a | 2023-08-18 02:24:01 +0000 | [diff] [blame] | 659 | |
Tomasz Wasilczyk | b61c721 | 2023-09-08 17:32:11 +0000 | [diff] [blame] | 660 | ALOGI("%s OP_PLAY_AUDIO callback received for %s", __func__, String8(packageName).c_str()); |
Jean-Michel Trivi | 74e01fa | 2019-02-25 12:18:09 -0800 | [diff] [blame] | 661 | sp<OpPlayAudioMonitor> monitor = mMonitor.promote(); |
| 662 | if (monitor != NULL) { |
Vlad Popa | d585959 | 2023-08-02 18:36:04 -0700 | [diff] [blame] | 663 | monitor->checkPlayAudioForUsage(/*doBroadcast=*/true); |
Jean-Michel Trivi | 74e01fa | 2019-02-25 12:18:09 -0800 | [diff] [blame] | 664 | } |
| 665 | } |
| 666 | |
Eric Laurent | 9066ad3 | 2019-05-20 14:40:10 -0700 | [diff] [blame] | 667 | // static |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 668 | void OpPlayAudioMonitor::getPackagesForUid( |
Eric Laurent | 9066ad3 | 2019-05-20 14:40:10 -0700 | [diff] [blame] | 669 | uid_t uid, Vector<String16>& packages) |
| 670 | { |
| 671 | PermissionController permissionController; |
| 672 | permissionController.getPackagesForUid(uid, packages); |
| 673 | } |
| 674 | |
Jean-Michel Trivi | 74e01fa | 2019-02-25 12:18:09 -0800 | [diff] [blame] | 675 | // ---------------------------------------------------------------------------- |
Andy Hung | 9d84af5 | 2018-09-12 18:03:44 -0700 | [diff] [blame] | 676 | #undef LOG_TAG |
| 677 | #define LOG_TAG "AF::Track" |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 678 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 679 | /* static */ |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 680 | sp<IAfTrack> IAfTrack::create( |
| 681 | IAfPlaybackThread* thread, |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 682 | const sp<Client>& client, |
| 683 | audio_stream_type_t streamType, |
| 684 | const audio_attributes_t& attr, |
| 685 | uint32_t sampleRate, |
| 686 | audio_format_t format, |
| 687 | audio_channel_mask_t channelMask, |
| 688 | size_t frameCount, |
| 689 | void *buffer, |
| 690 | size_t bufferSize, |
| 691 | const sp<IMemory>& sharedBuffer, |
| 692 | audio_session_t sessionId, |
| 693 | pid_t creatorPid, |
| 694 | const AttributionSourceState& attributionSource, |
| 695 | audio_output_flags_t flags, |
| 696 | track_type type, |
| 697 | audio_port_handle_t portId, |
| 698 | /** default behaviour is to start when there are as many frames |
| 699 | * ready as possible (aka. Buffer is full). */ |
| 700 | size_t frameCountToBeReady, |
| 701 | float speed, |
| 702 | bool isSpatialized, |
| 703 | bool isBitPerfect) { |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 704 | return sp<Track>::make(thread, |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 705 | client, |
| 706 | streamType, |
| 707 | attr, |
| 708 | sampleRate, |
| 709 | format, |
| 710 | channelMask, |
| 711 | frameCount, |
| 712 | buffer, |
| 713 | bufferSize, |
| 714 | sharedBuffer, |
| 715 | sessionId, |
| 716 | creatorPid, |
| 717 | attributionSource, |
| 718 | flags, |
| 719 | type, |
| 720 | portId, |
| 721 | frameCountToBeReady, |
| 722 | speed, |
| 723 | isSpatialized, |
| 724 | isBitPerfect); |
| 725 | } |
| 726 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 727 | // Track constructor must be called with AudioFlinger::mLock and ThreadBase::mLock held |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 728 | Track::Track( |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 729 | IAfPlaybackThread* thread, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 730 | const sp<Client>& client, |
| 731 | audio_stream_type_t streamType, |
Kevin Rocard | 1f564ac | 2018-03-29 13:53:10 -0700 | [diff] [blame] | 732 | const audio_attributes_t& attr, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 733 | uint32_t sampleRate, |
| 734 | audio_format_t format, |
| 735 | audio_channel_mask_t channelMask, |
| 736 | size_t frameCount, |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 737 | void *buffer, |
Andy Hung | 8fe6803 | 2017-06-05 16:17:51 -0700 | [diff] [blame] | 738 | size_t bufferSize, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 739 | const sp<IMemory>& sharedBuffer, |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 740 | audio_session_t sessionId, |
Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 741 | pid_t creatorPid, |
Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 742 | const AttributionSourceState& attributionSource, |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 743 | audio_output_flags_t flags, |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 744 | track_type type, |
Kevin Rocard | 01c7d9e | 2019-09-18 11:24:52 +0100 | [diff] [blame] | 745 | audio_port_handle_t portId, |
jiabin | f042b9b | 2021-05-07 23:46:28 +0000 | [diff] [blame] | 746 | size_t frameCountToBeReady, |
Eric Laurent | b0a7bc9 | 2022-04-05 15:06:08 +0200 | [diff] [blame] | 747 | float speed, |
jiabin | c658e45 | 2022-10-21 20:52:21 +0000 | [diff] [blame] | 748 | bool isSpatialized, |
| 749 | bool isBitPerfect) |
Kevin Rocard | 1f564ac | 2018-03-29 13:53:10 -0700 | [diff] [blame] | 750 | : TrackBase(thread, client, attr, sampleRate, format, channelMask, frameCount, |
Ytai Ben-Tsvi | 7dd3972 | 2019-09-05 15:14:30 -0700 | [diff] [blame] | 751 | // TODO: Using unsecurePointer() has some associated security pitfalls |
| 752 | // (see declaration for details). |
| 753 | // Either document why it is safe in this case or address the |
| 754 | // issue (e.g. by copying). |
| 755 | (sharedBuffer != 0) ? sharedBuffer->unsecurePointer() : buffer, |
Andy Hung | 8fe6803 | 2017-06-05 16:17:51 -0700 | [diff] [blame] | 756 | (sharedBuffer != 0) ? sharedBuffer->size() : bufferSize, |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 757 | sessionId, creatorPid, |
Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 758 | VALUE_OR_FATAL(aidl2legacy_int32_t_uid_t(attributionSource.uid)), true /*isOut*/, |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 759 | (type == TYPE_PATCH) ? ( buffer == NULL ? ALLOC_LOCAL : ALLOC_NONE) : ALLOC_CBLK, |
Andy Hung | b68f5eb | 2019-12-03 16:49:17 -0800 | [diff] [blame] | 760 | type, |
| 761 | portId, |
| 762 | std::string(AMEDIAMETRICS_KEY_PREFIX_AUDIO_TRACK) + std::to_string(portId)), |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 763 | mFillingStatus(FS_INVALID), |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 764 | // mRetryCount initialized later when needed |
| 765 | mSharedBuffer(sharedBuffer), |
| 766 | mStreamType(streamType), |
rago | 94a1ee8 | 2017-07-21 15:11:02 -0700 | [diff] [blame] | 767 | mMainBuffer(thread->sinkBuffer()), |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 768 | mAuxBuffer(NULL), |
| 769 | mAuxEffectId(0), mHasVolumeController(false), |
Andy Hung | e10393e | 2015-06-12 13:59:33 -0700 | [diff] [blame] | 770 | mFrameMap(16 /* sink-frame-to-track-frame map memory */), |
Ivan Lozano | 8cf3a07 | 2017-08-09 09:01:33 -0700 | [diff] [blame] | 771 | mVolumeHandler(new media::VolumeHandler(sampleRate)), |
Vlad Popa | 9d7c6f8 | 2023-07-10 20:27:41 -0700 | [diff] [blame] | 772 | mOpPlayAudioMonitor(OpPlayAudioMonitor::createIfNeeded(thread, attributionSource, attr, id(), |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 773 | streamType)), |
Andy Hung | e10393e | 2015-06-12 13:59:33 -0700 | [diff] [blame] | 774 | // mSinkTimestamp |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 775 | mFastIndex(-1), |
Glenn Kasten | 5736c35 | 2012-12-04 12:12:34 -0800 | [diff] [blame] | 776 | mCachedVolume(1.0), |
Kevin Rocard | 1238109 | 2018-04-11 09:19:59 -0700 | [diff] [blame] | 777 | /* The track might not play immediately after being active, similarly as if its volume was 0. |
| 778 | * When the track starts playing, its volume will be computed. */ |
| 779 | mFinalVolume(0.f), |
Haynes Mathew George | 7844f67 | 2014-01-15 12:32:55 -0800 | [diff] [blame] | 780 | mResumeToStopping(false), |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 781 | mFlushHwPending(false), |
jiabin | f042b9b | 2021-05-07 23:46:28 +0000 | [diff] [blame] | 782 | mFlags(flags), |
Eric Laurent | b0a7bc9 | 2022-04-05 15:06:08 +0200 | [diff] [blame] | 783 | mSpeed(speed), |
jiabin | c658e45 | 2022-10-21 20:52:21 +0000 | [diff] [blame] | 784 | mIsSpatialized(isSpatialized), |
| 785 | mIsBitPerfect(isBitPerfect) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 786 | { |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 787 | // client == 0 implies sharedBuffer == 0 |
| 788 | ALOG_ASSERT(!(client == 0 && sharedBuffer != 0)); |
| 789 | |
Andy Hung | 9d84af5 | 2018-09-12 18:03:44 -0700 | [diff] [blame] | 790 | ALOGV_IF(sharedBuffer != 0, "%s(%d): sharedBuffer: %p, size: %zu", |
Ytai Ben-Tsvi | 7dd3972 | 2019-09-05 15:14:30 -0700 | [diff] [blame] | 791 | __func__, mId, sharedBuffer->unsecurePointer(), sharedBuffer->size()); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 792 | |
Glenn Kasten | 3ef14ef | 2014-03-13 15:08:51 -0700 | [diff] [blame] | 793 | if (mCblk == NULL) { |
| 794 | return; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 795 | } |
Glenn Kasten | 3ef14ef | 2014-03-13 15:08:51 -0700 | [diff] [blame] | 796 | |
Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 797 | uid_t uid = VALUE_OR_FATAL(aidl2legacy_int32_t_uid_t(attributionSource.uid)); |
Andy Hung | 689e82c | 2019-08-21 17:53:17 -0700 | [diff] [blame] | 798 | if (!thread->isTrackAllowed_l(channelMask, format, sessionId, uid)) { |
| 799 | ALOGE("%s(%d): no more tracks available", __func__, mId); |
| 800 | releaseCblk(); // this makes the track invalid. |
| 801 | return; |
| 802 | } |
| 803 | |
Glenn Kasten | 3ef14ef | 2014-03-13 15:08:51 -0700 | [diff] [blame] | 804 | if (sharedBuffer == 0) { |
| 805 | mAudioTrackServerProxy = new AudioTrackServerProxy(mCblk, mBuffer, frameCount, |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 806 | mFrameSize, !isExternalTrack(), sampleRate); |
Glenn Kasten | 3ef14ef | 2014-03-13 15:08:51 -0700 | [diff] [blame] | 807 | } else { |
| 808 | mAudioTrackServerProxy = new StaticAudioTrackServerProxy(mCblk, mBuffer, frameCount, |
Kevin Rocard | 3686203 | 2019-10-10 10:52:19 +0100 | [diff] [blame] | 809 | mFrameSize, sampleRate); |
Glenn Kasten | 3ef14ef | 2014-03-13 15:08:51 -0700 | [diff] [blame] | 810 | } |
| 811 | mServerProxy = mAudioTrackServerProxy; |
Andy Hung | 3c7f47a | 2021-03-16 17:30:09 -0700 | [diff] [blame] | 812 | mServerProxy->setStartThresholdInFrames(frameCountToBeReady); // update the Cblk value |
Glenn Kasten | 3ef14ef | 2014-03-13 15:08:51 -0700 | [diff] [blame] | 813 | |
Glenn Kasten | 3ef14ef | 2014-03-13 15:08:51 -0700 | [diff] [blame] | 814 | // only allocate a fast track index if we were able to allocate a normal track name |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 815 | if (flags & AUDIO_OUTPUT_FLAG_FAST) { |
Andy Hung | a542782 | 2015-09-11 16:15:35 -0700 | [diff] [blame] | 816 | // FIXME: Not calling framesReadyIsCalledByMultipleThreads() exposes a potential |
| 817 | // race with setSyncEvent(). However, if we call it, we cannot properly start |
| 818 | // static fast tracks (SoundPool) immediately after stopping. |
| 819 | //mAudioTrackServerProxy->framesReadyIsCalledByMultipleThreads(); |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 820 | ALOG_ASSERT(thread->fastTrackAvailMask_l() != 0); |
| 821 | const int i = __builtin_ctz(thread->fastTrackAvailMask_l()); |
Glenn Kasten | dc2c50b | 2016-04-21 08:13:14 -0700 | [diff] [blame] | 822 | ALOG_ASSERT(0 < i && i < (int)FastMixerState::sMaxFastTracks); |
Glenn Kasten | 3ef14ef | 2014-03-13 15:08:51 -0700 | [diff] [blame] | 823 | // FIXME This is too eager. We allocate a fast track index before the |
| 824 | // fast track becomes active. Since fast tracks are a scarce resource, |
| 825 | // this means we are potentially denying other more important fast tracks from |
| 826 | // being created. It would be better to allocate the index dynamically. |
| 827 | mFastIndex = i; |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 828 | thread->fastTrackAvailMask_l() &= ~(1 << i); |
Glenn Kasten | 3ef14ef | 2014-03-13 15:08:51 -0700 | [diff] [blame] | 829 | } |
Andy Hung | 8946a28 | 2018-04-19 20:04:56 -0700 | [diff] [blame] | 830 | |
Dean Wheatley | 7b03691 | 2020-06-18 16:22:11 +1000 | [diff] [blame] | 831 | mServerLatencySupported = checkServerLatencySupported(format, flags); |
Andy Hung | 8946a28 | 2018-04-19 20:04:56 -0700 | [diff] [blame] | 832 | #ifdef TEE_SINK |
| 833 | mTee.setId(std::string("_") + std::to_string(mThreadIoHandle) |
Kevin Rocard | 51f0e98 | 2019-02-01 19:19:11 -0800 | [diff] [blame] | 834 | + "_" + std::to_string(mId) + "_T"); |
Andy Hung | 8946a28 | 2018-04-19 20:04:56 -0700 | [diff] [blame] | 835 | #endif |
jiabin | 57303cc | 2018-12-18 15:45:57 -0800 | [diff] [blame] | 836 | |
jiabin | eb3bda0 | 2020-06-30 14:07:03 -0700 | [diff] [blame] | 837 | if (thread->supportsHapticPlayback()) { |
| 838 | // If the track is attached to haptic playback thread, it is potentially to have |
| 839 | // HapticGenerator effect, which will generate haptic data, on the track. In that case, |
| 840 | // external vibration is always created for all tracks attached to haptic playback thread. |
jiabin | 57303cc | 2018-12-18 15:45:57 -0800 | [diff] [blame] | 841 | mAudioVibrationController = new AudioVibrationController(this); |
Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 842 | std::string packageName = attributionSource.packageName.has_value() ? |
| 843 | attributionSource.packageName.value() : ""; |
jiabin | 57303cc | 2018-12-18 15:45:57 -0800 | [diff] [blame] | 844 | mExternalVibration = new os::ExternalVibration( |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 845 | mUid, packageName, mAttr, mAudioVibrationController); |
jiabin | 57303cc | 2018-12-18 15:45:57 -0800 | [diff] [blame] | 846 | } |
Andy Hung | b68f5eb | 2019-12-03 16:49:17 -0800 | [diff] [blame] | 847 | |
| 848 | // Once this item is logged by the server, the client can add properties. |
Andy Hung | a629bd1 | 2020-06-05 16:03:53 -0700 | [diff] [blame] | 849 | const char * const traits = sharedBuffer == 0 ? "" : "static"; |
Andy Hung | 5837c7f | 2021-02-25 10:48:24 -0800 | [diff] [blame] | 850 | mTrackMetrics.logConstructor(creatorPid, uid, id(), traits, streamType); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 851 | } |
| 852 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 853 | Track::~Track() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 854 | { |
Andy Hung | 9d84af5 | 2018-09-12 18:03:44 -0700 | [diff] [blame] | 855 | ALOGV("%s(%d)", __func__, mId); |
Glenn Kasten | 0c72b24 | 2013-09-11 09:14:16 -0700 | [diff] [blame] | 856 | |
| 857 | // The destructor would clear mSharedBuffer, |
| 858 | // but it will not push the decremented reference count, |
| 859 | // leaving the client's IMemory dangling indefinitely. |
| 860 | // This prevents that leak. |
| 861 | if (mSharedBuffer != 0) { |
| 862 | mSharedBuffer.clear(); |
Glenn Kasten | 0c72b24 | 2013-09-11 09:14:16 -0700 | [diff] [blame] | 863 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 864 | } |
| 865 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 866 | status_t Track::initCheck() const |
Glenn Kasten | 0300333 | 2013-08-06 15:40:54 -0700 | [diff] [blame] | 867 | { |
| 868 | status_t status = TrackBase::initCheck(); |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 869 | if (status == NO_ERROR && mCblk == nullptr) { |
Glenn Kasten | 0300333 | 2013-08-06 15:40:54 -0700 | [diff] [blame] | 870 | status = NO_MEMORY; |
| 871 | } |
| 872 | return status; |
| 873 | } |
| 874 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 875 | void Track::destroy() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 876 | { |
| 877 | // NOTE: destroyTrack_l() can remove a strong reference to this Track |
| 878 | // by removing it from mTracks vector, so there is a risk that this Tracks's |
| 879 | // destructor is called. As the destructor needs to lock mLock, |
| 880 | // we must acquire a strong reference on this Track before locking mLock |
| 881 | // here so that the destructor is called only when exiting this function. |
| 882 | // On the other hand, as long as Track::destroy() is only called by |
| 883 | // TrackHandle destructor, the TrackHandle still holds a strong ref on |
| 884 | // this Track with its member mTrack. |
| 885 | sp<Track> keep(this); |
| 886 | { // scope for mLock |
Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 887 | bool wasActive = false; |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 888 | const sp<IAfThreadBase> thread = mThread.promote(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 889 | if (thread != 0) { |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 890 | audio_utils::lock_guard _l(thread->mutex()); |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 891 | auto* const playbackThread = thread->asIAfPlaybackThread().get(); |
Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 892 | wasActive = playbackThread->destroyTrack_l(this); |
jiabin | 7434e81 | 2023-06-27 18:22:35 +0000 | [diff] [blame] | 893 | forEachTeePatchTrack_l([](const auto& patchTrack) { patchTrack->destroy(); }); |
Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 894 | } |
| 895 | if (isExternalTrack() && !wasActive) { |
Eric Laurent | d7fe086 | 2018-07-14 16:48:01 -0700 | [diff] [blame] | 896 | AudioSystem::releaseOutput(mPortId); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 897 | } |
| 898 | } |
| 899 | } |
| 900 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 901 | void Track::appendDumpHeader(String8& result) const |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 902 | { |
Eric Laurent | 973db02 | 2018-11-20 14:54:31 -0800 | [diff] [blame] | 903 | result.appendFormat("Type Id Active Client Session Port Id S Flags " |
Kevin Rocard | 5f2136e | 2018-05-11 22:03:00 -0700 | [diff] [blame] | 904 | " Format Chn mask SRate " |
| 905 | "ST Usg CT " |
| 906 | " G db L dB R dB VS dB " |
jiabin | 5eaf096 | 2022-12-20 20:11:38 +0000 | [diff] [blame] | 907 | " Server FrmCnt FrmRdy F Underruns Flushed BitPerfect" |
Kevin Rocard | 5f2136e | 2018-05-11 22:03:00 -0700 | [diff] [blame] | 908 | "%s\n", |
| 909 | isServerLatencySupported() ? " Latency" : ""); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 910 | } |
| 911 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 912 | void Track::appendDump(String8& result, bool active) const |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 913 | { |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 914 | char trackType; |
| 915 | switch (mType) { |
| 916 | case TYPE_DEFAULT: |
| 917 | case TYPE_OUTPUT: |
Andy Hung | f6ab58d | 2018-05-25 12:50:39 -0700 | [diff] [blame] | 918 | if (isStatic()) { |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 919 | trackType = 'S'; // static |
| 920 | } else { |
| 921 | trackType = ' '; // normal |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 922 | } |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 923 | break; |
| 924 | case TYPE_PATCH: |
| 925 | trackType = 'P'; |
| 926 | break; |
| 927 | default: |
| 928 | trackType = '?'; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 929 | } |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 930 | |
| 931 | if (isFastTrack()) { |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 932 | result.appendFormat("F%d %c %6d", mFastIndex, trackType, mId); |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 933 | } else { |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 934 | result.appendFormat(" %c %6d", trackType, mId); |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 935 | } |
| 936 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 937 | char nowInUnderrun; |
| 938 | switch (mObservedUnderruns.mBitFields.mMostRecent) { |
| 939 | case UNDERRUN_FULL: |
| 940 | nowInUnderrun = ' '; |
| 941 | break; |
| 942 | case UNDERRUN_PARTIAL: |
| 943 | nowInUnderrun = '<'; |
| 944 | break; |
| 945 | case UNDERRUN_EMPTY: |
| 946 | nowInUnderrun = '*'; |
| 947 | break; |
| 948 | default: |
| 949 | nowInUnderrun = '?'; |
| 950 | break; |
| 951 | } |
Andy Hung | da540db | 2017-04-20 14:06:17 -0700 | [diff] [blame] | 952 | |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 953 | char fillingStatus; |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 954 | switch (mFillingStatus) { |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 955 | case FS_INVALID: |
| 956 | fillingStatus = 'I'; |
| 957 | break; |
| 958 | case FS_FILLING: |
| 959 | fillingStatus = 'f'; |
| 960 | break; |
| 961 | case FS_FILLED: |
| 962 | fillingStatus = 'F'; |
| 963 | break; |
| 964 | case FS_ACTIVE: |
| 965 | fillingStatus = 'A'; |
| 966 | break; |
| 967 | default: |
| 968 | fillingStatus = '?'; |
| 969 | break; |
| 970 | } |
| 971 | |
| 972 | // clip framesReadySafe to max representation in dump |
| 973 | const size_t framesReadySafe = |
| 974 | std::min(mAudioTrackServerProxy->framesReadySafe(), (size_t)99999999); |
| 975 | |
| 976 | // obtain volumes |
| 977 | const gain_minifloat_packed_t vlr = mAudioTrackServerProxy->getVolumeLR(); |
| 978 | const std::pair<float /* volume */, bool /* active */> vsVolume = |
| 979 | mVolumeHandler->getLastVolume(); |
| 980 | |
| 981 | // Our effective frame count is obtained by ServerProxy::getBufferSizeInFrames() |
| 982 | // as it may be reduced by the application. |
| 983 | const size_t bufferSizeInFrames = (size_t)mAudioTrackServerProxy->getBufferSizeInFrames(); |
| 984 | // Check whether the buffer size has been modified by the app. |
| 985 | const char modifiedBufferChar = bufferSizeInFrames < mFrameCount |
| 986 | ? 'r' /* buffer reduced */: bufferSizeInFrames > mFrameCount |
| 987 | ? 'e' /* error */ : ' ' /* identical */; |
| 988 | |
Eric Laurent | 973db02 | 2018-11-20 14:54:31 -0800 | [diff] [blame] | 989 | result.appendFormat("%7s %6u %7u %7u %2s 0x%03X " |
Kevin Rocard | 5f2136e | 2018-05-11 22:03:00 -0700 | [diff] [blame] | 990 | "%08X %08X %6u " |
| 991 | "%2u %3x %2x " |
| 992 | "%5.2g %5.2g %5.2g %5.2g%c " |
jiabin | 5eaf096 | 2022-12-20 20:11:38 +0000 | [diff] [blame] | 993 | "%08X %6zu%c %6zu %c %9u%c %7u %10s", |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 994 | active ? "yes" : "no", |
Andy Hung | 4ef19fa | 2018-05-15 19:35:29 -0700 | [diff] [blame] | 995 | (mClient == 0) ? getpid() : mClient->pid(), |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 996 | mSessionId, |
Eric Laurent | 973db02 | 2018-11-20 14:54:31 -0800 | [diff] [blame] | 997 | mPortId, |
Andy Hung | e2e830f | 2019-12-03 12:54:46 -0800 | [diff] [blame] | 998 | getTrackStateAsCodedString(), |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 999 | mCblk->mFlags, |
| 1000 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1001 | mFormat, |
| 1002 | mChannelMask, |
Andy Hung | cef2daa | 2018-06-01 15:31:49 -0700 | [diff] [blame] | 1003 | sampleRate(), |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 1004 | |
| 1005 | mStreamType, |
Kevin Rocard | 5f2136e | 2018-05-11 22:03:00 -0700 | [diff] [blame] | 1006 | mAttr.usage, |
| 1007 | mAttr.content_type, |
| 1008 | |
| 1009 | 20.0 * log10(mFinalVolume), |
Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 1010 | 20.0 * log10(float_from_gain(gain_minifloat_unpack_left(vlr))), |
| 1011 | 20.0 * log10(float_from_gain(gain_minifloat_unpack_right(vlr))), |
Andy Hung | da540db | 2017-04-20 14:06:17 -0700 | [diff] [blame] | 1012 | 20.0 * log10(vsVolume.first), // VolumeShaper(s) total volume |
| 1013 | vsVolume.second ? 'A' : ' ', // if any VolumeShapers active |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 1014 | |
Glenn Kasten | f20e1d8 | 2013-07-12 09:45:18 -0700 | [diff] [blame] | 1015 | mCblk->mServer, |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 1016 | bufferSizeInFrames, |
| 1017 | modifiedBufferChar, |
| 1018 | framesReadySafe, |
| 1019 | fillingStatus, |
Glenn Kasten | 82aaf94 | 2013-07-17 16:05:07 -0700 | [diff] [blame] | 1020 | mAudioTrackServerProxy->getUnderrunFrames(), |
Andy Hung | 2148bf0 | 2016-11-28 19:01:02 -0800 | [diff] [blame] | 1021 | nowInUnderrun, |
jiabin | 5eaf096 | 2022-12-20 20:11:38 +0000 | [diff] [blame] | 1022 | (unsigned)mAudioTrackServerProxy->framesFlushed() % 10000000, |
| 1023 | isBitPerfect() ? "true" : "false" |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 1024 | ); |
Andy Hung | cef2daa | 2018-06-01 15:31:49 -0700 | [diff] [blame] | 1025 | |
| 1026 | if (isServerLatencySupported()) { |
| 1027 | double latencyMs; |
| 1028 | bool fromTrack; |
| 1029 | if (getTrackLatencyMs(&latencyMs, &fromTrack) == OK) { |
| 1030 | // Show latency in msec, followed by 't' if from track timestamp (the most accurate) |
| 1031 | // or 'k' if estimated from kernel because track frames haven't been presented yet. |
| 1032 | result.appendFormat(" %7.2lf %c", latencyMs, fromTrack ? 't' : 'k'); |
Andy Hung | f6ab58d | 2018-05-25 12:50:39 -0700 | [diff] [blame] | 1033 | } else { |
Andy Hung | cef2daa | 2018-06-01 15:31:49 -0700 | [diff] [blame] | 1034 | result.appendFormat("%10s", mCblk->mServer != 0 ? "unavail" : "new"); |
Andy Hung | f6ab58d | 2018-05-25 12:50:39 -0700 | [diff] [blame] | 1035 | } |
| 1036 | } |
| 1037 | result.append("\n"); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1038 | } |
| 1039 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 1040 | uint32_t Track::sampleRate() const { |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1041 | return mAudioTrackServerProxy->getSampleRate(); |
| 1042 | } |
| 1043 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1044 | // AudioBufferProvider interface |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 1045 | status_t Track::getNextBuffer(AudioBufferProvider::Buffer* buffer) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1046 | { |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1047 | ServerProxy::Buffer buf; |
| 1048 | size_t desiredFrames = buffer->frameCount; |
| 1049 | buf.mFrameCount = desiredFrames; |
| 1050 | status_t status = mServerProxy->obtainBuffer(&buf); |
| 1051 | buffer->frameCount = buf.mFrameCount; |
| 1052 | buffer->raw = buf.mRaw; |
Andy Hung | fc62917 | 2020-06-22 10:06:23 -0700 | [diff] [blame] | 1053 | if (buf.mFrameCount == 0 && !isStopping() && !isStopped() && !isPaused() && !isOffloaded()) { |
Andy Hung | 9d84af5 | 2018-09-12 18:03:44 -0700 | [diff] [blame] | 1054 | ALOGV("%s(%d): underrun, framesReady(%zu) < framesDesired(%zd), state: %d", |
Andy Hung | 959b5b8 | 2021-09-24 10:46:20 -0700 | [diff] [blame] | 1055 | __func__, mId, buf.mFrameCount, desiredFrames, (int)mState); |
Glenn Kasten | 82aaf94 | 2013-07-17 16:05:07 -0700 | [diff] [blame] | 1056 | mAudioTrackServerProxy->tallyUnderrunFrames(desiredFrames); |
Phil Burk | 2812d9e | 2016-01-04 10:34:30 -0800 | [diff] [blame] | 1057 | } else { |
| 1058 | mAudioTrackServerProxy->tallyUnderrunFrames(0); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1059 | } |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1060 | return status; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1061 | } |
| 1062 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 1063 | void Track::releaseBuffer(AudioBufferProvider::Buffer* buffer) |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1064 | { |
| 1065 | interceptBuffer(*buffer); |
| 1066 | TrackBase::releaseBuffer(buffer); |
| 1067 | } |
| 1068 | |
| 1069 | // TODO: compensate for time shift between HW modules. |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 1070 | void Track::interceptBuffer( |
Kevin Rocard | a134b00 | 2019-02-07 18:05:31 -0800 | [diff] [blame] | 1071 | const AudioBufferProvider::Buffer& sourceBuffer) { |
Kevin Rocard | 6057fa2 | 2019-02-08 14:08:07 -0800 | [diff] [blame] | 1072 | auto start = std::chrono::steady_clock::now(); |
Kevin Rocard | a134b00 | 2019-02-07 18:05:31 -0800 | [diff] [blame] | 1073 | const size_t frameCount = sourceBuffer.frameCount; |
Kevin Rocard | d83b08a | 2019-02-27 15:05:54 -0800 | [diff] [blame] | 1074 | if (frameCount == 0) { |
| 1075 | return; // No audio to intercept. |
| 1076 | // Additionally PatchProxyBufferProvider::obtainBuffer (called by PathTrack::getNextBuffer) |
| 1077 | // does not allow 0 frame size request contrary to getNextBuffer |
| 1078 | } |
| 1079 | for (auto& teePatch : mTeePatches) { |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 1080 | IAfPatchRecord* patchRecord = teePatch.patchRecord.get(); |
Mikhail Naganov | 8296c25 | 2019-09-25 14:59:54 -0700 | [diff] [blame] | 1081 | const size_t framesWritten = patchRecord->writeFrames( |
| 1082 | sourceBuffer.i8, frameCount, mFrameSize); |
| 1083 | const size_t framesLeft = frameCount - framesWritten; |
Kevin Rocard | a134b00 | 2019-02-07 18:05:31 -0800 | [diff] [blame] | 1084 | ALOGW_IF(framesLeft != 0, "%s(%d) PatchRecord %d can not provide big enough " |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 1085 | "buffer %zu/%zu, dropping %zu frames", __func__, mId, patchRecord->id(), |
Kevin Rocard | a134b00 | 2019-02-07 18:05:31 -0800 | [diff] [blame] | 1086 | framesWritten, frameCount, framesLeft); |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1087 | } |
Kevin Rocard | 6057fa2 | 2019-02-08 14:08:07 -0800 | [diff] [blame] | 1088 | auto spent = ceil<std::chrono::microseconds>(std::chrono::steady_clock::now() - start); |
| 1089 | using namespace std::chrono_literals; |
| 1090 | // Average is ~20us per track, this should virtually never be logged (Logging takes >200us) |
Kevin Rocard | 01c7d9e | 2019-09-18 11:24:52 +0100 | [diff] [blame] | 1091 | ALOGD_IF(spent > 500us, "%s: took %lldus to intercept %zu tracks", __func__, |
Kevin Rocard | 6057fa2 | 2019-02-08 14:08:07 -0800 | [diff] [blame] | 1092 | spent.count(), mTeePatches.size()); |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1093 | } |
| 1094 | |
Glenn Kasten | 6466c9e | 2013-08-23 10:54:07 -0700 | [diff] [blame] | 1095 | // ExtendedAudioBufferProvider interface |
| 1096 | |
Andy Hung | 27876c0 | 2014-09-09 18:07:55 -0700 | [diff] [blame] | 1097 | // framesReady() may return an approximation of the number of frames if called |
| 1098 | // from a different thread than the one calling Proxy->obtainBuffer() and |
| 1099 | // Proxy->releaseBuffer(). Also note there is no mutual exclusion in the |
| 1100 | // AudioTrackServerProxy so be especially careful calling with FastTracks. |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 1101 | size_t Track::framesReady() const { |
Andy Hung | 27876c0 | 2014-09-09 18:07:55 -0700 | [diff] [blame] | 1102 | if (mSharedBuffer != 0 && (isStopped() || isStopping())) { |
| 1103 | // Static tracks return zero frames immediately upon stopping (for FastTracks). |
| 1104 | // The remainder of the buffer is not drained. |
| 1105 | return 0; |
| 1106 | } |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1107 | return mAudioTrackServerProxy->framesReady(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1108 | } |
| 1109 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 1110 | int64_t Track::framesReleased() const |
Glenn Kasten | 6466c9e | 2013-08-23 10:54:07 -0700 | [diff] [blame] | 1111 | { |
| 1112 | return mAudioTrackServerProxy->framesReleased(); |
| 1113 | } |
| 1114 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 1115 | void Track::onTimestamp(const ExtendedTimestamp ×tamp) |
Andy Hung | 6ae5843 | 2016-02-16 18:32:24 -0800 | [diff] [blame] | 1116 | { |
| 1117 | // This call comes from a FastTrack and should be kept lockless. |
| 1118 | // The server side frames are already translated to client frames. |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 1119 | mAudioTrackServerProxy->setTimestamp(timestamp); |
Andy Hung | 6ae5843 | 2016-02-16 18:32:24 -0800 | [diff] [blame] | 1120 | |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 1121 | // We do not set drained here, as FastTrack timestamp may not go to very last frame. |
Andy Hung | cef2daa | 2018-06-01 15:31:49 -0700 | [diff] [blame] | 1122 | |
| 1123 | // Compute latency. |
| 1124 | // TODO: Consider whether the server latency may be passed in by FastMixer |
| 1125 | // as a constant for all active FastTracks. |
| 1126 | const double latencyMs = timestamp.getOutputServerLatencyMs(sampleRate()); |
| 1127 | mServerLatencyFromTrack.store(true); |
| 1128 | mServerLatencyMs.store(latencyMs); |
Andy Hung | 6ae5843 | 2016-02-16 18:32:24 -0800 | [diff] [blame] | 1129 | } |
| 1130 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1131 | // Don't call for fast tracks; the framesReady() could result in priority inversion |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 1132 | bool Track::isReady() const { |
| 1133 | if (mFillingStatus != FS_FILLING || isStopped() || isPausing()) { |
Krishnankutty Kolathappilly | 8d6c292 | 2014-02-04 16:23:42 -0800 | [diff] [blame] | 1134 | return true; |
| 1135 | } |
| 1136 | |
Eric Laurent | 1649851 | 2014-03-17 17:22:08 -0700 | [diff] [blame] | 1137 | if (isStopping()) { |
| 1138 | if (framesReady() > 0) { |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 1139 | mFillingStatus = FS_FILLED; |
Eric Laurent | 1649851 | 2014-03-17 17:22:08 -0700 | [diff] [blame] | 1140 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1141 | return true; |
| 1142 | } |
| 1143 | |
Kevin Rocard | 01c7d9e | 2019-09-18 11:24:52 +0100 | [diff] [blame] | 1144 | size_t bufferSizeInFrames = mServerProxy->getBufferSizeInFrames(); |
Andy Hung | 3c7f47a | 2021-03-16 17:30:09 -0700 | [diff] [blame] | 1145 | // Note: mServerProxy->getStartThresholdInFrames() is clamped. |
| 1146 | const size_t startThresholdInFrames = mServerProxy->getStartThresholdInFrames(); |
| 1147 | const size_t framesToBeReady = std::clamp( // clamp again to validate client values. |
| 1148 | std::min(startThresholdInFrames, bufferSizeInFrames), size_t(1), mFrameCount); |
Kevin Rocard | 01c7d9e | 2019-09-18 11:24:52 +0100 | [diff] [blame] | 1149 | |
| 1150 | if (framesReady() >= framesToBeReady || (mCblk->mFlags & CBLK_FORCEREADY)) { |
| 1151 | ALOGV("%s(%d): consider track ready with %zu/%zu, target was %zu)", |
| 1152 | __func__, mId, framesReady(), bufferSizeInFrames, framesToBeReady); |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 1153 | mFillingStatus = FS_FILLED; |
Glenn Kasten | 96f60d8 | 2013-07-12 10:21:18 -0700 | [diff] [blame] | 1154 | android_atomic_and(~CBLK_FORCEREADY, &mCblk->mFlags); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1155 | return true; |
| 1156 | } |
| 1157 | return false; |
| 1158 | } |
| 1159 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 1160 | status_t Track::start(AudioSystem::sync_event_t event __unused, |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1161 | audio_session_t triggerSession __unused) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1162 | { |
| 1163 | status_t status = NO_ERROR; |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 1164 | ALOGV("%s(%d): calling pid %d session %d", |
| 1165 | __func__, mId, IPCThreadState::self()->getCallingPid(), mSessionId); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1166 | |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 1167 | const sp<IAfThreadBase> thread = mThread.promote(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1168 | if (thread != 0) { |
Eric Laurent | 813e2a7 | 2013-08-31 12:59:48 -0700 | [diff] [blame] | 1169 | if (isOffloaded()) { |
Andy Hung | 954b971 | 2023-08-28 18:36:53 -0700 | [diff] [blame] | 1170 | audio_utils::lock_guard _laf(thread->afThreadCallback()->mutex()); |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 1171 | const bool nonOffloadableGlobalEffectEnabled = |
| 1172 | thread->afThreadCallback()->isNonOffloadableGlobalEffectEnabled_l(); |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 1173 | audio_utils::lock_guard _lth(thread->mutex()); |
Andy Hung | 116bc26 | 2023-06-20 18:56:17 -0700 | [diff] [blame] | 1174 | sp<IAfEffectChain> ec = thread->getEffectChain_l(mSessionId); |
Andy Hung | ab65b18 | 2023-09-06 19:41:47 -0700 | [diff] [blame] | 1175 | if (nonOffloadableGlobalEffectEnabled || |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 1176 | (ec != 0 && ec->isNonOffloadableEnabled())) { |
Eric Laurent | 813e2a7 | 2013-08-31 12:59:48 -0700 | [diff] [blame] | 1177 | invalidate(); |
| 1178 | return PERMISSION_DENIED; |
| 1179 | } |
| 1180 | } |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 1181 | audio_utils::lock_guard _lth(thread->mutex()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1182 | track_state state = mState; |
| 1183 | // here the track could be either new, or restarted |
| 1184 | // in both cases "unstop" the track |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1185 | |
Krishnankutty Kolathappilly | 8d6c292 | 2014-02-04 16:23:42 -0800 | [diff] [blame] | 1186 | // initial state-stopping. next state-pausing. |
| 1187 | // What if resume is called ? |
| 1188 | |
Zhou Song | 1ed46a2 | 2020-08-17 15:36:56 +0800 | [diff] [blame] | 1189 | if (state == FLUSHED) { |
| 1190 | // avoid underrun glitches when starting after flush |
| 1191 | reset(); |
| 1192 | } |
| 1193 | |
kuowei.li | 576f136 | 2021-05-11 18:02:32 +0800 | [diff] [blame] | 1194 | // clear mPauseHwPending because of pause (and possibly flush) during underrun. |
| 1195 | mPauseHwPending = false; |
Krishnankutty Kolathappilly | 8d6c292 | 2014-02-04 16:23:42 -0800 | [diff] [blame] | 1196 | if (state == PAUSED || state == PAUSING) { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1197 | if (mResumeToStopping) { |
| 1198 | // happened we need to resume to STOPPING_1 |
| 1199 | mState = TrackBase::STOPPING_1; |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 1200 | ALOGV("%s(%d): PAUSED => STOPPING_1 on thread %d", |
| 1201 | __func__, mId, (int)mThreadIoHandle); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1202 | } else { |
| 1203 | mState = TrackBase::RESUMING; |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 1204 | ALOGV("%s(%d): PAUSED => RESUMING on thread %d", |
| 1205 | __func__, mId, (int)mThreadIoHandle); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1206 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1207 | } else { |
| 1208 | mState = TrackBase::ACTIVE; |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 1209 | ALOGV("%s(%d): ? => ACTIVE on thread %d", |
| 1210 | __func__, mId, (int)mThreadIoHandle); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1211 | } |
| 1212 | |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 1213 | auto* const playbackThread = thread->asIAfPlaybackThread().get(); |
yucliu | 6cfb593 | 2022-07-20 17:40:39 -0700 | [diff] [blame] | 1214 | |
| 1215 | // states to reset position info for pcm tracks |
| 1216 | if (audio_is_linear_pcm(mFormat) |
Andy Hung | e10393e | 2015-06-12 13:59:33 -0700 | [diff] [blame] | 1217 | && (state == IDLE || state == STOPPED || state == FLUSHED)) { |
| 1218 | mFrameMap.reset(); |
yucliu | 6cfb593 | 2022-07-20 17:40:39 -0700 | [diff] [blame] | 1219 | |
| 1220 | if (!isFastTrack() && (isDirect() || isOffloaded())) { |
| 1221 | // Start point of track -> sink frame map. If the HAL returns a |
| 1222 | // frame position smaller than the first written frame in |
| 1223 | // updateTrackFrameInfo, the timestamp can be interpolated |
| 1224 | // instead of using a larger value. |
| 1225 | mFrameMap.push(mAudioTrackServerProxy->framesReleased(), |
| 1226 | playbackThread->framesWritten()); |
| 1227 | } |
Andy Hung | e10393e | 2015-06-12 13:59:33 -0700 | [diff] [blame] | 1228 | } |
Haynes Mathew George | 240934b | 2015-03-11 18:25:50 -0700 | [diff] [blame] | 1229 | if (isFastTrack()) { |
| 1230 | // refresh fast track underruns on start because that field is never cleared |
| 1231 | // by the fast mixer; furthermore, the same track can be recycled, i.e. start |
| 1232 | // after stop. |
| 1233 | mObservedUnderruns = playbackThread->getFastTrackUnderruns(mFastIndex); |
| 1234 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1235 | status = playbackThread->addTrack_l(this); |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 1236 | if (status == INVALID_OPERATION || status == PERMISSION_DENIED || status == DEAD_OBJECT) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1237 | triggerEvents(AudioSystem::SYNC_EVENT_PRESENTATION_COMPLETE); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1238 | // restore previous state if start was rejected by policy manager |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 1239 | if (status == PERMISSION_DENIED || status == DEAD_OBJECT) { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1240 | mState = state; |
| 1241 | } |
| 1242 | } |
Andy Hung | 1d3556d | 2018-03-29 16:30:14 -0700 | [diff] [blame] | 1243 | |
Andy Hung | b68f5eb | 2019-12-03 16:49:17 -0800 | [diff] [blame] | 1244 | // Audio timing metrics are computed a few mix cycles after starting. |
| 1245 | { |
| 1246 | mLogStartCountdown = LOG_START_COUNTDOWN; |
| 1247 | mLogStartTimeNs = systemTime(); |
| 1248 | mLogStartFrames = mAudioTrackServerProxy->getTimestamp() |
Andy Hung | 6292112 | 2020-05-18 10:47:31 -0700 | [diff] [blame] | 1249 | .mPosition[ExtendedTimestamp::LOCATION_KERNEL]; |
| 1250 | mLogLatencyMs = 0.; |
Andy Hung | b68f5eb | 2019-12-03 16:49:17 -0800 | [diff] [blame] | 1251 | } |
Andy Hung | a81a4b4 | 2022-05-19 19:24:51 -0700 | [diff] [blame] | 1252 | mLogForceVolumeUpdate = true; // at least one volume logged for metrics when starting. |
Andy Hung | b68f5eb | 2019-12-03 16:49:17 -0800 | [diff] [blame] | 1253 | |
Andy Hung | 1d3556d | 2018-03-29 16:30:14 -0700 | [diff] [blame] | 1254 | if (status == NO_ERROR || status == ALREADY_EXISTS) { |
| 1255 | // for streaming tracks, remove the buffer read stop limit. |
| 1256 | mAudioTrackServerProxy->start(); |
| 1257 | } |
| 1258 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1259 | // track was already in the active list, not a problem |
| 1260 | if (status == ALREADY_EXISTS) { |
| 1261 | status = NO_ERROR; |
Glenn Kasten | 12022ff | 2013-10-17 11:32:39 -0700 | [diff] [blame] | 1262 | } else { |
| 1263 | // Acknowledge any pending flush(), so that subsequent new data isn't discarded. |
| 1264 | // It is usually unsafe to access the server proxy from a binder thread. |
| 1265 | // But in this case we know the mixer thread (whether normal mixer or fast mixer) |
| 1266 | // isn't looking at this track yet: we still hold the normal mixer thread lock, |
| 1267 | // and for fast tracks the track is not yet in the fast mixer thread's active set. |
Andy Hung | e6fb82a | 2015-09-09 14:39:02 -0700 | [diff] [blame] | 1268 | // For static tracks, this is used to acknowledge change in position or loop. |
Eric Laurent | 564d144 | 2015-09-09 12:26:52 -0700 | [diff] [blame] | 1269 | ServerProxy::Buffer buffer; |
| 1270 | buffer.mFrameCount = 1; |
| 1271 | (void) mAudioTrackServerProxy->obtainBuffer(&buffer, true /*ackFlush*/); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1272 | } |
jiabin | 7434e81 | 2023-06-27 18:22:35 +0000 | [diff] [blame] | 1273 | if (status == NO_ERROR) { |
| 1274 | forEachTeePatchTrack_l([](const auto& patchTrack) { patchTrack->start(); }); |
| 1275 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1276 | } else { |
| 1277 | status = BAD_VALUE; |
| 1278 | } |
Kevin Rocard | c43ea14 | 2019-01-31 18:17:37 -0800 | [diff] [blame] | 1279 | if (status == NO_ERROR) { |
Jean-Michel Trivi | 16395ca | 2022-12-11 22:10:11 +0000 | [diff] [blame] | 1280 | // send format to AudioManager for playback activity monitoring |
Andy Hung | 583043b | 2023-07-17 17:05:00 -0700 | [diff] [blame] | 1281 | const sp<IAudioManager> audioManager = |
| 1282 | thread->afThreadCallback()->getOrCreateAudioManager(); |
Jean-Michel Trivi | 16395ca | 2022-12-11 22:10:11 +0000 | [diff] [blame] | 1283 | if (audioManager && mPortId != AUDIO_PORT_HANDLE_NONE) { |
| 1284 | std::unique_ptr<os::PersistableBundle> bundle = |
| 1285 | std::make_unique<os::PersistableBundle>(); |
| 1286 | bundle->putBoolean(String16(kExtraPlayerEventSpatializedKey), |
| 1287 | isSpatialized()); |
| 1288 | bundle->putInt(String16(kExtraPlayerEventSampleRateKey), mSampleRate); |
| 1289 | bundle->putInt(String16(kExtraPlayerEventChannelMaskKey), mChannelMask); |
| 1290 | status_t result = audioManager->portEvent(mPortId, |
| 1291 | PLAYER_UPDATE_FORMAT, bundle); |
| 1292 | if (result != OK) { |
| 1293 | ALOGE("%s: unable to send playback format for port ID %d, status error %d", |
| 1294 | __func__, mPortId, result); |
| 1295 | } |
| 1296 | } |
Kevin Rocard | c43ea14 | 2019-01-31 18:17:37 -0800 | [diff] [blame] | 1297 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1298 | return status; |
| 1299 | } |
| 1300 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 1301 | void Track::stop() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1302 | { |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 1303 | ALOGV("%s(%d): calling pid %d", __func__, mId, IPCThreadState::self()->getCallingPid()); |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 1304 | const sp<IAfThreadBase> thread = mThread.promote(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1305 | if (thread != 0) { |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 1306 | audio_utils::lock_guard _l(thread->mutex()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1307 | track_state state = mState; |
| 1308 | if (state == RESUMING || state == ACTIVE || state == PAUSING || state == PAUSED) { |
| 1309 | // If the track is not active (PAUSED and buffers full), flush buffers |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 1310 | auto* const playbackThread = thread->asIAfPlaybackThread().get(); |
| 1311 | if (!playbackThread->isTrackActive(this)) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1312 | reset(); |
| 1313 | mState = STOPPED; |
François Gaffie | 1353b29 | 2023-11-03 13:09:53 +0100 | [diff] [blame^] | 1314 | } else if (isPatchTrack() || (!isFastTrack() && !isOffloaded() && !isDirect())) { |
| 1315 | // for a PatchTrack (whatever fast ot not), do not drain but move directly |
| 1316 | // to STOPPED to avoid closing while active. |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1317 | mState = STOPPED; |
| 1318 | } else { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1319 | // For fast tracks prepareTracks_l() will set state to STOPPING_2 |
| 1320 | // presentation is complete |
| 1321 | // For an offloaded track this starts a drain and state will |
| 1322 | // move to STOPPING_2 when drain completes and then STOPPED |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1323 | mState = STOPPING_1; |
Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 1324 | if (isOffloaded()) { |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 1325 | mRetryCount = IAfPlaybackThread::kMaxTrackStopRetriesOffload; |
Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 1326 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1327 | } |
Eric Laurent | b369caf | 2015-03-30 20:51:47 -0700 | [diff] [blame] | 1328 | playbackThread->broadcast_l(); |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 1329 | ALOGV("%s(%d): not stopping/stopped => stopping/stopped on thread %d", |
| 1330 | __func__, mId, (int)mThreadIoHandle); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1331 | } |
jiabin | 7434e81 | 2023-06-27 18:22:35 +0000 | [diff] [blame] | 1332 | forEachTeePatchTrack_l([](const auto& patchTrack) { patchTrack->stop(); }); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1333 | } |
| 1334 | } |
| 1335 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 1336 | void Track::pause() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1337 | { |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 1338 | ALOGV("%s(%d): calling pid %d", __func__, mId, IPCThreadState::self()->getCallingPid()); |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 1339 | const sp<IAfThreadBase> thread = mThread.promote(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1340 | if (thread != 0) { |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 1341 | audio_utils::lock_guard _l(thread->mutex()); |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 1342 | auto* const playbackThread = thread->asIAfPlaybackThread().get(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1343 | switch (mState) { |
| 1344 | case STOPPING_1: |
| 1345 | case STOPPING_2: |
| 1346 | if (!isOffloaded()) { |
| 1347 | /* nothing to do if track is not offloaded */ |
| 1348 | break; |
| 1349 | } |
| 1350 | |
| 1351 | // Offloaded track was draining, we need to carry on draining when resumed |
| 1352 | mResumeToStopping = true; |
Chih-Hung Hsieh | 2b48703 | 2018-09-13 14:16:02 -0700 | [diff] [blame] | 1353 | FALLTHROUGH_INTENDED; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1354 | case ACTIVE: |
| 1355 | case RESUMING: |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1356 | mState = PAUSING; |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 1357 | ALOGV("%s(%d): ACTIVE/RESUMING => PAUSING on thread %d", |
| 1358 | __func__, mId, (int)mThreadIoHandle); |
Kuowei Li | 2366647 | 2021-01-20 10:23:25 +0800 | [diff] [blame] | 1359 | if (isOffloadedOrDirect()) { |
| 1360 | mPauseHwPending = true; |
| 1361 | } |
Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 1362 | playbackThread->broadcast_l(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1363 | break; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1364 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1365 | default: |
| 1366 | break; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1367 | } |
jiabin | 7434e81 | 2023-06-27 18:22:35 +0000 | [diff] [blame] | 1368 | // Pausing the TeePatch to avoid a glitch on underrun, at the cost of buffered audio loss. |
| 1369 | forEachTeePatchTrack_l([](const auto& patchTrack) { patchTrack->pause(); }); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1370 | } |
| 1371 | } |
| 1372 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 1373 | void Track::flush() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1374 | { |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 1375 | ALOGV("%s(%d)", __func__, mId); |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 1376 | const sp<IAfThreadBase> thread = mThread.promote(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1377 | if (thread != 0) { |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 1378 | audio_utils::lock_guard _l(thread->mutex()); |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 1379 | auto* const playbackThread = thread->asIAfPlaybackThread().get(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1380 | |
Phil Burk | 4bb650b | 2016-09-09 12:11:17 -0700 | [diff] [blame] | 1381 | // Flush the ring buffer now if the track is not active in the PlaybackThread. |
| 1382 | // Otherwise the flush would not be done until the track is resumed. |
| 1383 | // Requires FastTrack removal be BLOCK_UNTIL_ACKED |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 1384 | if (!playbackThread->isTrackActive(this)) { |
Phil Burk | 4bb650b | 2016-09-09 12:11:17 -0700 | [diff] [blame] | 1385 | (void)mServerProxy->flushBufferIfNeeded(); |
| 1386 | } |
| 1387 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1388 | if (isOffloaded()) { |
| 1389 | // If offloaded we allow flush during any state except terminated |
| 1390 | // and keep the track active to avoid problems if user is seeking |
| 1391 | // rapidly and underlying hardware has a significant delay handling |
| 1392 | // a pause |
| 1393 | if (isTerminated()) { |
| 1394 | return; |
| 1395 | } |
| 1396 | |
Andy Hung | 9d84af5 | 2018-09-12 18:03:44 -0700 | [diff] [blame] | 1397 | ALOGV("%s(%d): offload flush", __func__, mId); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1398 | reset(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1399 | |
| 1400 | if (mState == STOPPING_1 || mState == STOPPING_2) { |
Andy Hung | 9d84af5 | 2018-09-12 18:03:44 -0700 | [diff] [blame] | 1401 | ALOGV("%s(%d): flushed in STOPPING_1 or 2 state, change state to ACTIVE", |
| 1402 | __func__, mId); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1403 | mState = ACTIVE; |
| 1404 | } |
| 1405 | |
Haynes Mathew George | 7844f67 | 2014-01-15 12:32:55 -0800 | [diff] [blame] | 1406 | mFlushHwPending = true; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1407 | mResumeToStopping = false; |
| 1408 | } else { |
| 1409 | if (mState != STOPPING_1 && mState != STOPPING_2 && mState != STOPPED && |
| 1410 | mState != PAUSED && mState != PAUSING && mState != IDLE && mState != FLUSHED) { |
| 1411 | return; |
| 1412 | } |
| 1413 | // No point remaining in PAUSED state after a flush => go to |
| 1414 | // FLUSHED state |
| 1415 | mState = FLUSHED; |
| 1416 | // do not reset the track if it is still in the process of being stopped or paused. |
| 1417 | // this will be done by prepareTracks_l() when the track is stopped. |
| 1418 | // prepareTracks_l() will see mState == FLUSHED, then |
| 1419 | // remove from active track list, reset(), and trigger presentation complete |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 1420 | if (isDirect()) { |
| 1421 | mFlushHwPending = true; |
| 1422 | } |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 1423 | if (!playbackThread->isTrackActive(this)) { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1424 | reset(); |
| 1425 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1426 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1427 | // Prevent flush being lost if the track is flushed and then resumed |
| 1428 | // before mixer thread can run. This is important when offloading |
| 1429 | // because the hardware buffer could hold a large amount of audio |
Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 1430 | playbackThread->broadcast_l(); |
jiabin | 7434e81 | 2023-06-27 18:22:35 +0000 | [diff] [blame] | 1431 | // Flush the Tee to avoid on resume playing old data and glitching on the transition to |
| 1432 | // new data |
| 1433 | forEachTeePatchTrack_l([](const auto& patchTrack) { patchTrack->flush(); }); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1434 | } |
| 1435 | } |
| 1436 | |
Haynes Mathew George | 7844f67 | 2014-01-15 12:32:55 -0800 | [diff] [blame] | 1437 | // must be called with thread lock held |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 1438 | void Track::flushAck() |
Haynes Mathew George | 7844f67 | 2014-01-15 12:32:55 -0800 | [diff] [blame] | 1439 | { |
Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 1440 | if (!isOffloaded() && !isDirect()) { |
Haynes Mathew George | 7844f67 | 2014-01-15 12:32:55 -0800 | [diff] [blame] | 1441 | return; |
Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 1442 | } |
Haynes Mathew George | 7844f67 | 2014-01-15 12:32:55 -0800 | [diff] [blame] | 1443 | |
Phil Burk | 4bb650b | 2016-09-09 12:11:17 -0700 | [diff] [blame] | 1444 | // Clear the client ring buffer so that the app can prime the buffer while paused. |
| 1445 | // Otherwise it might not get cleared until playback is resumed and obtainBuffer() is called. |
| 1446 | mServerProxy->flushBufferIfNeeded(); |
| 1447 | |
Haynes Mathew George | 7844f67 | 2014-01-15 12:32:55 -0800 | [diff] [blame] | 1448 | mFlushHwPending = false; |
| 1449 | } |
| 1450 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 1451 | void Track::pauseAck() |
Kuowei Li | 2366647 | 2021-01-20 10:23:25 +0800 | [diff] [blame] | 1452 | { |
| 1453 | mPauseHwPending = false; |
| 1454 | } |
| 1455 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 1456 | void Track::reset() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1457 | { |
| 1458 | // Do not reset twice to avoid discarding data written just after a flush and before |
| 1459 | // the audioflinger thread detects the track is stopped. |
| 1460 | if (!mResetDone) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1461 | // Force underrun condition to avoid false underrun callback until first data is |
| 1462 | // written to buffer |
Glenn Kasten | 96f60d8 | 2013-07-12 10:21:18 -0700 | [diff] [blame] | 1463 | android_atomic_and(~CBLK_FORCEREADY, &mCblk->mFlags); |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 1464 | mFillingStatus = FS_FILLING; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1465 | mResetDone = true; |
| 1466 | if (mState == FLUSHED) { |
| 1467 | mState = IDLE; |
| 1468 | } |
| 1469 | } |
| 1470 | } |
| 1471 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 1472 | status_t Track::setParameters(const String8& keyValuePairs) |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1473 | { |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 1474 | const sp<IAfThreadBase> thread = mThread.promote(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1475 | if (thread == 0) { |
Andy Hung | 9d84af5 | 2018-09-12 18:03:44 -0700 | [diff] [blame] | 1476 | ALOGE("%s(%d): thread is dead", __func__, mId); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1477 | return FAILED_TRANSACTION; |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 1478 | } else if (thread->type() == IAfThreadBase::DIRECT |
| 1479 | || thread->type() == IAfThreadBase::OFFLOAD) { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1480 | return thread->setParameters(keyValuePairs); |
| 1481 | } else { |
| 1482 | return PERMISSION_DENIED; |
| 1483 | } |
| 1484 | } |
| 1485 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 1486 | status_t Track::selectPresentation(int presentationId, |
Mikhail Naganov | ac917ac | 2018-11-28 14:03:52 -0800 | [diff] [blame] | 1487 | int programId) { |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 1488 | const sp<IAfThreadBase> thread = mThread.promote(); |
Mikhail Naganov | ac917ac | 2018-11-28 14:03:52 -0800 | [diff] [blame] | 1489 | if (thread == 0) { |
| 1490 | ALOGE("thread is dead"); |
| 1491 | return FAILED_TRANSACTION; |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 1492 | } else if (thread->type() == IAfThreadBase::DIRECT |
| 1493 | || thread->type() == IAfThreadBase::OFFLOAD) { |
| 1494 | auto directOutputThread = thread->asIAfDirectOutputThread().get(); |
Mikhail Naganov | ac917ac | 2018-11-28 14:03:52 -0800 | [diff] [blame] | 1495 | return directOutputThread->selectPresentation(presentationId, programId); |
| 1496 | } |
| 1497 | return INVALID_OPERATION; |
| 1498 | } |
| 1499 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 1500 | VolumeShaper::Status Track::applyVolumeShaper( |
Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 1501 | const sp<VolumeShaper::Configuration>& configuration, |
| 1502 | const sp<VolumeShaper::Operation>& operation) |
| 1503 | { |
Andy Hung | 398ffa2 | 2022-12-13 19:19:53 -0800 | [diff] [blame] | 1504 | VolumeShaper::Status status = mVolumeHandler->applyVolumeShaper(configuration, operation); |
Andy Hung | 10cbff1 | 2017-02-21 17:30:14 -0800 | [diff] [blame] | 1505 | |
| 1506 | if (isOffloadedOrDirect()) { |
| 1507 | // Signal thread to fetch new volume. |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 1508 | const sp<IAfThreadBase> thread = mThread.promote(); |
Andy Hung | 10cbff1 | 2017-02-21 17:30:14 -0800 | [diff] [blame] | 1509 | if (thread != 0) { |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 1510 | audio_utils::lock_guard _l(thread->mutex()); |
Andy Hung | 10cbff1 | 2017-02-21 17:30:14 -0800 | [diff] [blame] | 1511 | thread->broadcast_l(); |
| 1512 | } |
| 1513 | } |
| 1514 | return status; |
Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 1515 | } |
| 1516 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 1517 | sp<VolumeShaper::State> Track::getVolumeShaperState(int id) const |
Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 1518 | { |
| 1519 | // Note: We don't check if Thread exists. |
| 1520 | |
| 1521 | // mVolumeHandler is thread safe. |
| 1522 | return mVolumeHandler->getVolumeShaperState(id); |
| 1523 | } |
| 1524 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 1525 | void Track::setFinalVolume(float volumeLeft, float volumeRight) |
Kevin Rocard | 1238109 | 2018-04-11 09:19:59 -0700 | [diff] [blame] | 1526 | { |
jiabin | 76d9469 | 2022-12-15 21:51:21 +0000 | [diff] [blame] | 1527 | mFinalVolumeLeft = volumeLeft; |
| 1528 | mFinalVolumeRight = volumeRight; |
| 1529 | const float volume = (volumeLeft + volumeRight) * 0.5f; |
Kevin Rocard | 1238109 | 2018-04-11 09:19:59 -0700 | [diff] [blame] | 1530 | if (mFinalVolume != volume) { // Compare to an epsilon if too many meaningless updates |
| 1531 | mFinalVolume = volume; |
| 1532 | setMetadataHasChanged(); |
Andy Hung | a81a4b4 | 2022-05-19 19:24:51 -0700 | [diff] [blame] | 1533 | mLogForceVolumeUpdate = true; |
| 1534 | } |
| 1535 | if (mLogForceVolumeUpdate) { |
| 1536 | mLogForceVolumeUpdate = false; |
| 1537 | mTrackMetrics.logVolume(mFinalVolume); |
Kevin Rocard | 1238109 | 2018-04-11 09:19:59 -0700 | [diff] [blame] | 1538 | } |
| 1539 | } |
| 1540 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 1541 | void Track::copyMetadataTo(MetadataInserter& backInserter) const |
Kevin Rocard | 1238109 | 2018-04-11 09:19:59 -0700 | [diff] [blame] | 1542 | { |
Eric Laurent | 49e3928 | 2022-06-24 18:42:45 +0200 | [diff] [blame] | 1543 | // Do not forward metadata for PatchTrack with unspecified stream type |
| 1544 | if (mStreamType == AUDIO_STREAM_PATCH) { |
| 1545 | return; |
| 1546 | } |
| 1547 | |
Eric Laurent | 9457917 | 2020-11-20 18:41:04 +0100 | [diff] [blame] | 1548 | playback_track_metadata_v7_t metadata; |
| 1549 | metadata.base = { |
Kevin Rocard | 1238109 | 2018-04-11 09:19:59 -0700 | [diff] [blame] | 1550 | .usage = mAttr.usage, |
| 1551 | .content_type = mAttr.content_type, |
| 1552 | .gain = mFinalVolume, |
| 1553 | }; |
Eric Laurent | fdf9950 | 2021-11-26 19:05:02 +0100 | [diff] [blame] | 1554 | |
| 1555 | // When attributes are undefined, derive default values from stream type. |
| 1556 | // See AudioAttributes.java, usageForStreamType() and Builder.setInternalLegacyStreamType() |
| 1557 | if (mAttr.usage == AUDIO_USAGE_UNKNOWN) { |
| 1558 | switch (mStreamType) { |
| 1559 | case AUDIO_STREAM_VOICE_CALL: |
| 1560 | metadata.base.usage = AUDIO_USAGE_VOICE_COMMUNICATION; |
| 1561 | metadata.base.content_type = AUDIO_CONTENT_TYPE_SPEECH; |
| 1562 | break; |
| 1563 | case AUDIO_STREAM_SYSTEM: |
| 1564 | metadata.base.usage = AUDIO_USAGE_ASSISTANCE_SONIFICATION; |
| 1565 | metadata.base.content_type = AUDIO_CONTENT_TYPE_SONIFICATION; |
| 1566 | break; |
| 1567 | case AUDIO_STREAM_RING: |
| 1568 | metadata.base.usage = AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE; |
| 1569 | metadata.base.content_type = AUDIO_CONTENT_TYPE_SONIFICATION; |
| 1570 | break; |
| 1571 | case AUDIO_STREAM_MUSIC: |
| 1572 | metadata.base.usage = AUDIO_USAGE_MEDIA; |
| 1573 | metadata.base.content_type = AUDIO_CONTENT_TYPE_MUSIC; |
| 1574 | break; |
| 1575 | case AUDIO_STREAM_ALARM: |
| 1576 | metadata.base.usage = AUDIO_USAGE_ALARM; |
| 1577 | metadata.base.content_type = AUDIO_CONTENT_TYPE_SONIFICATION; |
| 1578 | break; |
| 1579 | case AUDIO_STREAM_NOTIFICATION: |
| 1580 | metadata.base.usage = AUDIO_USAGE_NOTIFICATION; |
| 1581 | metadata.base.content_type = AUDIO_CONTENT_TYPE_SONIFICATION; |
| 1582 | break; |
| 1583 | case AUDIO_STREAM_DTMF: |
| 1584 | metadata.base.usage = AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING; |
| 1585 | metadata.base.content_type = AUDIO_CONTENT_TYPE_SONIFICATION; |
| 1586 | break; |
| 1587 | case AUDIO_STREAM_ACCESSIBILITY: |
| 1588 | metadata.base.usage = AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY; |
| 1589 | metadata.base.content_type = AUDIO_CONTENT_TYPE_SPEECH; |
| 1590 | break; |
| 1591 | case AUDIO_STREAM_ASSISTANT: |
| 1592 | metadata.base.usage = AUDIO_USAGE_ASSISTANT; |
| 1593 | metadata.base.content_type = AUDIO_CONTENT_TYPE_SPEECH; |
| 1594 | break; |
| 1595 | case AUDIO_STREAM_REROUTING: |
| 1596 | metadata.base.usage = AUDIO_USAGE_VIRTUAL_SOURCE; |
| 1597 | // unknown content type |
| 1598 | break; |
| 1599 | case AUDIO_STREAM_CALL_ASSISTANT: |
| 1600 | metadata.base.usage = AUDIO_USAGE_CALL_ASSISTANT; |
| 1601 | metadata.base.content_type = AUDIO_CONTENT_TYPE_SPEECH; |
| 1602 | break; |
| 1603 | default: |
| 1604 | break; |
| 1605 | } |
| 1606 | } |
| 1607 | |
Eric Laurent | 78b0730 | 2022-10-07 16:20:34 +0200 | [diff] [blame] | 1608 | metadata.channel_mask = mChannelMask; |
Eric Laurent | 9457917 | 2020-11-20 18:41:04 +0100 | [diff] [blame] | 1609 | strncpy(metadata.tags, mAttr.tags, AUDIO_ATTRIBUTES_TAGS_MAX_SIZE); |
| 1610 | *backInserter++ = metadata; |
Kevin Rocard | 1238109 | 2018-04-11 09:19:59 -0700 | [diff] [blame] | 1611 | } |
| 1612 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 1613 | void Track::updateTeePatches_l() { |
Jiabin Huang | fb47684 | 2022-12-06 03:18:10 +0000 | [diff] [blame] | 1614 | if (mTeePatchesToUpdate.has_value()) { |
jiabin | 7434e81 | 2023-06-27 18:22:35 +0000 | [diff] [blame] | 1615 | forEachTeePatchTrack_l([](const auto& patchTrack) { patchTrack->destroy(); }); |
Jiabin Huang | fb47684 | 2022-12-06 03:18:10 +0000 | [diff] [blame] | 1616 | mTeePatches = mTeePatchesToUpdate.value(); |
| 1617 | if (mState == TrackBase::ACTIVE || mState == TrackBase::RESUMING || |
| 1618 | mState == TrackBase::STOPPING_1) { |
jiabin | 7434e81 | 2023-06-27 18:22:35 +0000 | [diff] [blame] | 1619 | forEachTeePatchTrack_l([](const auto& patchTrack) { patchTrack->start(); }); |
Jiabin Huang | fb47684 | 2022-12-06 03:18:10 +0000 | [diff] [blame] | 1620 | } |
| 1621 | mTeePatchesToUpdate.reset(); |
jiabin | f042b9b | 2021-05-07 23:46:28 +0000 | [diff] [blame] | 1622 | } |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1623 | } |
| 1624 | |
Andy Hung | 16ed0da | 2023-07-14 11:45:38 -0700 | [diff] [blame] | 1625 | void Track::setTeePatchesToUpdate_l(TeePatches teePatchesToUpdate) { |
Jiabin Huang | fb47684 | 2022-12-06 03:18:10 +0000 | [diff] [blame] | 1626 | ALOGW_IF(mTeePatchesToUpdate.has_value(), |
| 1627 | "%s, existing tee patches to update will be ignored", __func__); |
| 1628 | mTeePatchesToUpdate = std::move(teePatchesToUpdate); |
| 1629 | } |
| 1630 | |
Vlad Popa | e8d9947 | 2022-06-30 16:02:48 +0200 | [diff] [blame] | 1631 | // must be called with player thread lock held |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 1632 | void Track::processMuteEvent_l(const sp< |
Vlad Popa | e8d9947 | 2022-06-30 16:02:48 +0200 | [diff] [blame] | 1633 | IAudioManager>& audioManager, mute_state_t muteState) |
| 1634 | { |
| 1635 | if (mMuteState == muteState) { |
| 1636 | // mute state did not change, do nothing |
| 1637 | return; |
| 1638 | } |
| 1639 | |
| 1640 | status_t result = UNKNOWN_ERROR; |
| 1641 | if (audioManager && mPortId != AUDIO_PORT_HANDLE_NONE) { |
| 1642 | if (mMuteEventExtras == nullptr) { |
| 1643 | mMuteEventExtras = std::make_unique<os::PersistableBundle>(); |
| 1644 | } |
Shunkai Yao | af7990a | 2023-08-18 02:24:01 +0000 | [diff] [blame] | 1645 | mMuteEventExtras->putInt(String16(kExtraPlayerEventMuteKey), static_cast<int>(muteState)); |
Vlad Popa | e8d9947 | 2022-06-30 16:02:48 +0200 | [diff] [blame] | 1646 | |
Shunkai Yao | af7990a | 2023-08-18 02:24:01 +0000 | [diff] [blame] | 1647 | result = audioManager->portEvent(mPortId, PLAYER_UPDATE_MUTED, mMuteEventExtras); |
Vlad Popa | e8d9947 | 2022-06-30 16:02:48 +0200 | [diff] [blame] | 1648 | } |
| 1649 | |
| 1650 | if (result == OK) { |
Shunkai Yao | af7990a | 2023-08-18 02:24:01 +0000 | [diff] [blame] | 1651 | ALOGI("%s(%d): processed mute state for port ID %d from %d to %d", __func__, id(), mPortId, |
| 1652 | int(muteState), int(mMuteState)); |
Vlad Popa | e8d9947 | 2022-06-30 16:02:48 +0200 | [diff] [blame] | 1653 | mMuteState = muteState; |
| 1654 | } else { |
Shunkai Yao | af7990a | 2023-08-18 02:24:01 +0000 | [diff] [blame] | 1655 | ALOGW("%s(%d): cannot process mute state for port ID %d, status error %d", __func__, id(), |
| 1656 | mPortId, result); |
Vlad Popa | e8d9947 | 2022-06-30 16:02:48 +0200 | [diff] [blame] | 1657 | } |
| 1658 | } |
| 1659 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 1660 | status_t Track::getTimestamp(AudioTimestamp& timestamp) |
Glenn Kasten | 573d80a | 2013-08-26 09:36:23 -0700 | [diff] [blame] | 1661 | { |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 1662 | if (!isOffloaded() && !isDirect()) { |
| 1663 | return INVALID_OPERATION; // normal tracks handled through SSQ |
Glenn Kasten | fe346c7 | 2013-08-30 13:28:22 -0700 | [diff] [blame] | 1664 | } |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 1665 | const sp<IAfThreadBase> thread = mThread.promote(); |
Glenn Kasten | 573d80a | 2013-08-26 09:36:23 -0700 | [diff] [blame] | 1666 | if (thread == 0) { |
Glenn Kasten | fe346c7 | 2013-08-30 13:28:22 -0700 | [diff] [blame] | 1667 | return INVALID_OPERATION; |
Glenn Kasten | 573d80a | 2013-08-26 09:36:23 -0700 | [diff] [blame] | 1668 | } |
Phil Burk | 6140c79 | 2015-03-19 14:30:21 -0700 | [diff] [blame] | 1669 | |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 1670 | audio_utils::lock_guard _l(thread->mutex()); |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 1671 | auto* const playbackThread = thread->asIAfPlaybackThread().get(); |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 1672 | return playbackThread->getTimestamp_l(timestamp); |
Glenn Kasten | 573d80a | 2013-08-26 09:36:23 -0700 | [diff] [blame] | 1673 | } |
| 1674 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 1675 | status_t Track::attachAuxEffect(int EffectId) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1676 | { |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 1677 | const sp<IAfThreadBase> thread = mThread.promote(); |
Eric Laurent | 6c79632 | 2019-04-09 14:13:17 -0700 | [diff] [blame] | 1678 | if (thread == nullptr) { |
| 1679 | return DEAD_OBJECT; |
| 1680 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1681 | |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 1682 | auto dstThread = thread->asIAfPlaybackThread(); |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 1683 | // srcThread is initialized by call to moveAuxEffectToIo() |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 1684 | sp<IAfPlaybackThread> srcThread; |
Andy Hung | 47c0117 | 2023-07-17 12:40:43 -0700 | [diff] [blame] | 1685 | const auto& af = mClient->afClientCallback(); |
Eric Laurent | 6c79632 | 2019-04-09 14:13:17 -0700 | [diff] [blame] | 1686 | status_t status = af->moveAuxEffectToIo(EffectId, dstThread, &srcThread); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1687 | |
Eric Laurent | 6c79632 | 2019-04-09 14:13:17 -0700 | [diff] [blame] | 1688 | if (EffectId != 0 && status == NO_ERROR) { |
| 1689 | status = dstThread->attachAuxEffect(this, EffectId); |
| 1690 | if (status == NO_ERROR) { |
| 1691 | AudioSystem::moveEffectsToIo(std::vector<int>(EffectId), dstThread->id()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1692 | } |
Eric Laurent | 6c79632 | 2019-04-09 14:13:17 -0700 | [diff] [blame] | 1693 | } |
| 1694 | |
| 1695 | if (status != NO_ERROR && srcThread != nullptr) { |
| 1696 | af->moveAuxEffectToIo(EffectId, srcThread, &dstThread); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1697 | } |
| 1698 | return status; |
| 1699 | } |
| 1700 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 1701 | void Track::setAuxBuffer(int EffectId, int32_t *buffer) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1702 | { |
| 1703 | mAuxEffectId = EffectId; |
| 1704 | mAuxBuffer = buffer; |
| 1705 | } |
| 1706 | |
Andy Hung | 59de426 | 2021-06-14 10:53:54 -0700 | [diff] [blame] | 1707 | // presentationComplete verified by frames, used by Mixed tracks. |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 1708 | bool Track::presentationComplete( |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 1709 | int64_t framesWritten, size_t audioHalFrames) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1710 | { |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 1711 | // TODO: improve this based on FrameMap if it exists, to ensure full drain. |
| 1712 | // This assists in proper timestamp computation as well as wakelock management. |
| 1713 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1714 | // a track is considered presented when the total number of frames written to audio HAL |
| 1715 | // corresponds to the number of frames written when presentationComplete() is called for the |
| 1716 | // first time (mPresentationCompleteFrames == 0) plus the buffer filling status at that time. |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1717 | // For an offloaded track the HAL+h/w delay is variable so a HAL drain() is used |
| 1718 | // to detect when all frames have been played. In this case framesWritten isn't |
| 1719 | // useful because it doesn't always reflect whether there is data in the h/w |
| 1720 | // buffers, particularly if a track has been paused and resumed during draining |
Andy Hung | 9d84af5 | 2018-09-12 18:03:44 -0700 | [diff] [blame] | 1721 | ALOGV("%s(%d): presentationComplete() mPresentationCompleteFrames %lld framesWritten %lld", |
| 1722 | __func__, mId, |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 1723 | (long long)mPresentationCompleteFrames, (long long)framesWritten); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1724 | if (mPresentationCompleteFrames == 0) { |
| 1725 | mPresentationCompleteFrames = framesWritten + audioHalFrames; |
Andy Hung | 59de426 | 2021-06-14 10:53:54 -0700 | [diff] [blame] | 1726 | ALOGV("%s(%d): set:" |
Andy Hung | 9d84af5 | 2018-09-12 18:03:44 -0700 | [diff] [blame] | 1727 | " mPresentationCompleteFrames %lld audioHalFrames %zu", |
| 1728 | __func__, mId, |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 1729 | (long long)mPresentationCompleteFrames, audioHalFrames); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1730 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1731 | |
Andy Hung | c54b1ff | 2016-02-23 14:07:07 -0800 | [diff] [blame] | 1732 | bool complete; |
Andy Hung | 59de426 | 2021-06-14 10:53:54 -0700 | [diff] [blame] | 1733 | if (isFastTrack()) { // does not go through linear map |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 1734 | complete = framesWritten >= (int64_t) mPresentationCompleteFrames; |
Andy Hung | 59de426 | 2021-06-14 10:53:54 -0700 | [diff] [blame] | 1735 | ALOGV("%s(%d): %s framesWritten:%lld mPresentationCompleteFrames:%lld", |
| 1736 | __func__, mId, (complete ? "complete" : "waiting"), |
| 1737 | (long long) framesWritten, (long long) mPresentationCompleteFrames); |
Andy Hung | c54b1ff | 2016-02-23 14:07:07 -0800 | [diff] [blame] | 1738 | } else { // Normal tracks, OutputTracks, and PatchTracks |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 1739 | complete = framesWritten >= (int64_t) mPresentationCompleteFrames |
Andy Hung | c54b1ff | 2016-02-23 14:07:07 -0800 | [diff] [blame] | 1740 | && mAudioTrackServerProxy->isDrained(); |
| 1741 | } |
| 1742 | |
| 1743 | if (complete) { |
Andy Hung | 59de426 | 2021-06-14 10:53:54 -0700 | [diff] [blame] | 1744 | notifyPresentationComplete(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1745 | return true; |
| 1746 | } |
| 1747 | return false; |
| 1748 | } |
| 1749 | |
Andy Hung | 59de426 | 2021-06-14 10:53:54 -0700 | [diff] [blame] | 1750 | // presentationComplete checked by time, used by DirectTracks. |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 1751 | bool Track::presentationComplete(uint32_t latencyMs) |
Andy Hung | 59de426 | 2021-06-14 10:53:54 -0700 | [diff] [blame] | 1752 | { |
| 1753 | // For Offloaded or Direct tracks. |
| 1754 | |
| 1755 | // For a direct track, we incorporated time based testing for presentationComplete. |
| 1756 | |
| 1757 | // For an offloaded track the HAL+h/w delay is variable so a HAL drain() is used |
| 1758 | // to detect when all frames have been played. In this case latencyMs isn't |
| 1759 | // useful because it doesn't always reflect whether there is data in the h/w |
| 1760 | // buffers, particularly if a track has been paused and resumed during draining |
| 1761 | |
| 1762 | constexpr float MIN_SPEED = 0.125f; // min speed scaling allowed for timely response. |
| 1763 | if (mPresentationCompleteTimeNs == 0) { |
| 1764 | mPresentationCompleteTimeNs = systemTime() + latencyMs * 1e6 / fmax(mSpeed, MIN_SPEED); |
| 1765 | ALOGV("%s(%d): set: latencyMs %u mPresentationCompleteTimeNs:%lld", |
| 1766 | __func__, mId, latencyMs, (long long) mPresentationCompleteTimeNs); |
| 1767 | } |
| 1768 | |
| 1769 | bool complete; |
| 1770 | if (isOffloaded()) { |
| 1771 | complete = true; |
| 1772 | } else { // Direct |
| 1773 | complete = systemTime() >= mPresentationCompleteTimeNs; |
| 1774 | ALOGV("%s(%d): %s", __func__, mId, (complete ? "complete" : "waiting")); |
| 1775 | } |
| 1776 | if (complete) { |
| 1777 | notifyPresentationComplete(); |
| 1778 | return true; |
| 1779 | } |
| 1780 | return false; |
| 1781 | } |
| 1782 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 1783 | void Track::notifyPresentationComplete() |
Andy Hung | 59de426 | 2021-06-14 10:53:54 -0700 | [diff] [blame] | 1784 | { |
| 1785 | // This only triggers once. TODO: should we enforce this? |
| 1786 | triggerEvents(AudioSystem::SYNC_EVENT_PRESENTATION_COMPLETE); |
| 1787 | mAudioTrackServerProxy->setStreamEndDone(); |
| 1788 | } |
| 1789 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 1790 | void Track::triggerEvents(AudioSystem::sync_event_t type) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1791 | { |
Andy Hung | 068e08e | 2023-05-15 19:02:55 -0700 | [diff] [blame] | 1792 | for (auto it = mSyncEvents.begin(); it != mSyncEvents.end();) { |
| 1793 | if ((*it)->type() == type) { |
Andy Hung | 93bb573 | 2023-05-04 21:16:34 -0700 | [diff] [blame] | 1794 | ALOGV("%s: triggering SyncEvent type %d", __func__, type); |
Andy Hung | 068e08e | 2023-05-15 19:02:55 -0700 | [diff] [blame] | 1795 | (*it)->trigger(); |
| 1796 | it = mSyncEvents.erase(it); |
Ivan Lozano | 5ec161b | 2017-12-06 10:00:28 -0800 | [diff] [blame] | 1797 | } else { |
Andy Hung | 068e08e | 2023-05-15 19:02:55 -0700 | [diff] [blame] | 1798 | ++it; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1799 | } |
| 1800 | } |
| 1801 | } |
| 1802 | |
| 1803 | // implement VolumeBufferProvider interface |
| 1804 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 1805 | gain_minifloat_packed_t Track::getVolumeLR() const |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1806 | { |
| 1807 | // called by FastMixer, so not allowed to take any locks, block, or do I/O including logs |
| 1808 | ALOG_ASSERT(isFastTrack() && (mCblk != NULL)); |
Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 1809 | gain_minifloat_packed_t vlr = mAudioTrackServerProxy->getVolumeLR(); |
| 1810 | float vl = float_from_gain(gain_minifloat_unpack_left(vlr)); |
| 1811 | float vr = float_from_gain(gain_minifloat_unpack_right(vlr)); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1812 | // track volumes come from shared memory, so can't be trusted and must be clamped |
Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 1813 | if (vl > GAIN_FLOAT_UNITY) { |
| 1814 | vl = GAIN_FLOAT_UNITY; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1815 | } |
Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 1816 | if (vr > GAIN_FLOAT_UNITY) { |
| 1817 | vr = GAIN_FLOAT_UNITY; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1818 | } |
| 1819 | // now apply the cached master volume and stream type volume; |
| 1820 | // this is trusted but lacks any synchronization or barrier so may be stale |
| 1821 | float v = mCachedVolume; |
| 1822 | vl *= v; |
| 1823 | vr *= v; |
Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 1824 | // re-combine into packed minifloat |
| 1825 | vlr = gain_minifloat_pack(gain_from_float(vl), gain_from_float(vr)); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1826 | // FIXME look at mute, pause, and stop flags |
| 1827 | return vlr; |
| 1828 | } |
| 1829 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 1830 | status_t Track::setSyncEvent( |
Andy Hung | 068e08e | 2023-05-15 19:02:55 -0700 | [diff] [blame] | 1831 | const sp<audioflinger::SyncEvent>& event) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1832 | { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1833 | if (isTerminated() || mState == PAUSED || |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1834 | ((framesReady() == 0) && ((mSharedBuffer != 0) || |
| 1835 | (mState == STOPPED)))) { |
Andy Hung | 9d84af5 | 2018-09-12 18:03:44 -0700 | [diff] [blame] | 1836 | ALOGW("%s(%d): in invalid state %d on session %d %s mode, framesReady %zu", |
| 1837 | __func__, mId, |
Andy Hung | 959b5b8 | 2021-09-24 10:46:20 -0700 | [diff] [blame] | 1838 | (int)mState, mSessionId, (mSharedBuffer != 0) ? "static" : "stream", framesReady()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1839 | event->cancel(); |
| 1840 | return INVALID_OPERATION; |
| 1841 | } |
| 1842 | (void) TrackBase::setSyncEvent(event); |
| 1843 | return NO_ERROR; |
| 1844 | } |
| 1845 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 1846 | void Track::invalidate() |
Glenn Kasten | 5736c35 | 2012-12-04 12:12:34 -0800 | [diff] [blame] | 1847 | { |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 1848 | TrackBase::invalidate(); |
Eric Laurent | 4d231dc | 2016-03-11 18:38:23 -0800 | [diff] [blame] | 1849 | signalClientFlag(CBLK_INVALID); |
Eric Laurent | 4d231dc | 2016-03-11 18:38:23 -0800 | [diff] [blame] | 1850 | } |
| 1851 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 1852 | void Track::disable() |
Eric Laurent | 4d231dc | 2016-03-11 18:38:23 -0800 | [diff] [blame] | 1853 | { |
Kevin Rocard | 01c7d9e | 2019-09-18 11:24:52 +0100 | [diff] [blame] | 1854 | // TODO(b/142394888): the filling status should also be reset to filling |
Eric Laurent | 4d231dc | 2016-03-11 18:38:23 -0800 | [diff] [blame] | 1855 | signalClientFlag(CBLK_DISABLED); |
| 1856 | } |
| 1857 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 1858 | void Track::signalClientFlag(int32_t flag) |
Eric Laurent | 4d231dc | 2016-03-11 18:38:23 -0800 | [diff] [blame] | 1859 | { |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1860 | // FIXME should use proxy, and needs work |
| 1861 | audio_track_cblk_t* cblk = mCblk; |
Eric Laurent | 4d231dc | 2016-03-11 18:38:23 -0800 | [diff] [blame] | 1862 | android_atomic_or(flag, &cblk->mFlags); |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1863 | android_atomic_release_store(0x40000000, &cblk->mFutex); |
| 1864 | // client is not in server, so FUTEX_WAKE is needed instead of FUTEX_WAKE_PRIVATE |
Elliott Hughes | ee49929 | 2014-05-21 17:55:51 -0700 | [diff] [blame] | 1865 | (void) syscall(__NR_futex, &cblk->mFutex, FUTEX_WAKE, INT_MAX); |
Glenn Kasten | 5736c35 | 2012-12-04 12:12:34 -0800 | [diff] [blame] | 1866 | } |
| 1867 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 1868 | void Track::signal() |
Eric Laurent | 59fe010 | 2013-09-27 18:48:26 -0700 | [diff] [blame] | 1869 | { |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 1870 | const sp<IAfThreadBase> thread = mThread.promote(); |
Eric Laurent | 59fe010 | 2013-09-27 18:48:26 -0700 | [diff] [blame] | 1871 | if (thread != 0) { |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 1872 | auto* const t = thread->asIAfPlaybackThread().get(); |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 1873 | audio_utils::lock_guard _l(t->mutex()); |
Eric Laurent | 59fe010 | 2013-09-27 18:48:26 -0700 | [diff] [blame] | 1874 | t->broadcast_l(); |
| 1875 | } |
| 1876 | } |
| 1877 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 1878 | status_t Track::getDualMonoMode(audio_dual_mono_mode_t* mode) const |
Kuowei Li | d4adbdb | 2020-08-13 14:44:25 +0800 | [diff] [blame] | 1879 | { |
| 1880 | status_t status = INVALID_OPERATION; |
| 1881 | if (isOffloadedOrDirect()) { |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 1882 | const sp<IAfThreadBase> thread = mThread.promote(); |
Kuowei Li | d4adbdb | 2020-08-13 14:44:25 +0800 | [diff] [blame] | 1883 | if (thread != nullptr) { |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 1884 | auto* const t = thread->asIAfPlaybackThread().get(); |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 1885 | audio_utils::lock_guard _l(t->mutex()); |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 1886 | status = t->getOutput_l()->stream->getDualMonoMode(mode); |
Kuowei Li | d4adbdb | 2020-08-13 14:44:25 +0800 | [diff] [blame] | 1887 | ALOGD_IF((status == NO_ERROR) && (mDualMonoMode != *mode), |
| 1888 | "%s: mode %d inconsistent", __func__, mDualMonoMode); |
| 1889 | } |
| 1890 | } |
| 1891 | return status; |
| 1892 | } |
| 1893 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 1894 | status_t Track::setDualMonoMode(audio_dual_mono_mode_t mode) |
Kuowei Li | d4adbdb | 2020-08-13 14:44:25 +0800 | [diff] [blame] | 1895 | { |
| 1896 | status_t status = INVALID_OPERATION; |
| 1897 | if (isOffloadedOrDirect()) { |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 1898 | const sp<IAfThreadBase> thread = mThread.promote(); |
Kuowei Li | d4adbdb | 2020-08-13 14:44:25 +0800 | [diff] [blame] | 1899 | if (thread != nullptr) { |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 1900 | auto* const t = thread->asIAfPlaybackThread().get(); |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 1901 | audio_utils::lock_guard lock(t->mutex()); |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 1902 | status = t->getOutput_l()->stream->setDualMonoMode(mode); |
Kuowei Li | d4adbdb | 2020-08-13 14:44:25 +0800 | [diff] [blame] | 1903 | if (status == NO_ERROR) { |
| 1904 | mDualMonoMode = mode; |
| 1905 | } |
| 1906 | } |
| 1907 | } |
| 1908 | return status; |
| 1909 | } |
| 1910 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 1911 | status_t Track::getAudioDescriptionMixLevel(float* leveldB) const |
Kuowei Li | d4adbdb | 2020-08-13 14:44:25 +0800 | [diff] [blame] | 1912 | { |
| 1913 | status_t status = INVALID_OPERATION; |
| 1914 | if (isOffloadedOrDirect()) { |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 1915 | sp<IAfThreadBase> thread = mThread.promote(); |
Kuowei Li | d4adbdb | 2020-08-13 14:44:25 +0800 | [diff] [blame] | 1916 | if (thread != nullptr) { |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 1917 | auto* const t = thread->asIAfPlaybackThread().get(); |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 1918 | audio_utils::lock_guard lock(t->mutex()); |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 1919 | status = t->getOutput_l()->stream->getAudioDescriptionMixLevel(leveldB); |
Kuowei Li | d4adbdb | 2020-08-13 14:44:25 +0800 | [diff] [blame] | 1920 | ALOGD_IF((status == NO_ERROR) && (mAudioDescriptionMixLevel != *leveldB), |
| 1921 | "%s: level %.3f inconsistent", __func__, mAudioDescriptionMixLevel); |
| 1922 | } |
| 1923 | } |
| 1924 | return status; |
| 1925 | } |
| 1926 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 1927 | status_t Track::setAudioDescriptionMixLevel(float leveldB) |
Kuowei Li | d4adbdb | 2020-08-13 14:44:25 +0800 | [diff] [blame] | 1928 | { |
| 1929 | status_t status = INVALID_OPERATION; |
| 1930 | if (isOffloadedOrDirect()) { |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 1931 | const sp<IAfThreadBase> thread = mThread.promote(); |
Kuowei Li | d4adbdb | 2020-08-13 14:44:25 +0800 | [diff] [blame] | 1932 | if (thread != nullptr) { |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 1933 | auto* const t = thread->asIAfPlaybackThread().get(); |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 1934 | audio_utils::lock_guard lock(t->mutex()); |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 1935 | status = t->getOutput_l()->stream->setAudioDescriptionMixLevel(leveldB); |
Kuowei Li | d4adbdb | 2020-08-13 14:44:25 +0800 | [diff] [blame] | 1936 | if (status == NO_ERROR) { |
| 1937 | mAudioDescriptionMixLevel = leveldB; |
| 1938 | } |
| 1939 | } |
| 1940 | } |
| 1941 | return status; |
| 1942 | } |
| 1943 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 1944 | status_t Track::getPlaybackRateParameters( |
Andy Hung | d29af63 | 2023-06-23 19:27:19 -0700 | [diff] [blame] | 1945 | audio_playback_rate_t* playbackRate) const |
Kuowei Li | d4adbdb | 2020-08-13 14:44:25 +0800 | [diff] [blame] | 1946 | { |
| 1947 | status_t status = INVALID_OPERATION; |
| 1948 | if (isOffloadedOrDirect()) { |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 1949 | const sp<IAfThreadBase> thread = mThread.promote(); |
Kuowei Li | d4adbdb | 2020-08-13 14:44:25 +0800 | [diff] [blame] | 1950 | if (thread != nullptr) { |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 1951 | auto* const t = thread->asIAfPlaybackThread().get(); |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 1952 | audio_utils::lock_guard lock(t->mutex()); |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 1953 | status = t->getOutput_l()->stream->getPlaybackRateParameters(playbackRate); |
Kuowei Li | d4adbdb | 2020-08-13 14:44:25 +0800 | [diff] [blame] | 1954 | ALOGD_IF((status == NO_ERROR) && |
| 1955 | !isAudioPlaybackRateEqual(mPlaybackRateParameters, *playbackRate), |
| 1956 | "%s: playbackRate inconsistent", __func__); |
| 1957 | } |
| 1958 | } |
| 1959 | return status; |
| 1960 | } |
| 1961 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 1962 | status_t Track::setPlaybackRateParameters( |
Kuowei Li | d4adbdb | 2020-08-13 14:44:25 +0800 | [diff] [blame] | 1963 | const audio_playback_rate_t& playbackRate) |
| 1964 | { |
| 1965 | status_t status = INVALID_OPERATION; |
| 1966 | if (isOffloadedOrDirect()) { |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 1967 | const sp<IAfThreadBase> thread = mThread.promote(); |
Kuowei Li | d4adbdb | 2020-08-13 14:44:25 +0800 | [diff] [blame] | 1968 | if (thread != nullptr) { |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 1969 | auto* const t = thread->asIAfPlaybackThread().get(); |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 1970 | audio_utils::lock_guard lock(t->mutex()); |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 1971 | status = t->getOutput_l()->stream->setPlaybackRateParameters(playbackRate); |
Kuowei Li | d4adbdb | 2020-08-13 14:44:25 +0800 | [diff] [blame] | 1972 | if (status == NO_ERROR) { |
| 1973 | mPlaybackRateParameters = playbackRate; |
| 1974 | } |
| 1975 | } |
| 1976 | } |
| 1977 | return status; |
| 1978 | } |
| 1979 | |
Krishnankutty Kolathappilly | 8d6c292 | 2014-02-04 16:23:42 -0800 | [diff] [blame] | 1980 | //To be called with thread lock held |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 1981 | bool Track::isResumePending() const { |
Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 1982 | if (mState == RESUMING) { |
Krishnankutty Kolathappilly | 8d6c292 | 2014-02-04 16:23:42 -0800 | [diff] [blame] | 1983 | return true; |
Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 1984 | } |
Krishnankutty Kolathappilly | 8d6c292 | 2014-02-04 16:23:42 -0800 | [diff] [blame] | 1985 | /* Resume is pending if track was stopping before pause was called */ |
| 1986 | if (mState == STOPPING_1 && |
Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 1987 | mResumeToStopping) { |
Krishnankutty Kolathappilly | 8d6c292 | 2014-02-04 16:23:42 -0800 | [diff] [blame] | 1988 | return true; |
Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 1989 | } |
Krishnankutty Kolathappilly | 8d6c292 | 2014-02-04 16:23:42 -0800 | [diff] [blame] | 1990 | |
| 1991 | return false; |
| 1992 | } |
| 1993 | |
| 1994 | //To be called with thread lock held |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 1995 | void Track::resumeAck() { |
Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 1996 | if (mState == RESUMING) { |
Krishnankutty Kolathappilly | 8d6c292 | 2014-02-04 16:23:42 -0800 | [diff] [blame] | 1997 | mState = ACTIVE; |
Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 1998 | } |
Haynes Mathew George | 2d3ca68 | 2014-03-07 13:43:49 -0800 | [diff] [blame] | 1999 | |
Krishnankutty Kolathappilly | 8d6c292 | 2014-02-04 16:23:42 -0800 | [diff] [blame] | 2000 | // Other possibility of pending resume is stopping_1 state |
| 2001 | // Do not update the state from stopping as this prevents |
Haynes Mathew George | 2d3ca68 | 2014-03-07 13:43:49 -0800 | [diff] [blame] | 2002 | // drain being called. |
| 2003 | if (mState == STOPPING_1) { |
| 2004 | mResumeToStopping = false; |
| 2005 | } |
Krishnankutty Kolathappilly | 8d6c292 | 2014-02-04 16:23:42 -0800 | [diff] [blame] | 2006 | } |
Andy Hung | e10393e | 2015-06-12 13:59:33 -0700 | [diff] [blame] | 2007 | |
| 2008 | //To be called with thread lock held |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 2009 | void Track::updateTrackFrameInfo( |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 2010 | int64_t trackFramesReleased, int64_t sinkFramesWritten, |
Andy Hung | cef2daa | 2018-06-01 15:31:49 -0700 | [diff] [blame] | 2011 | uint32_t halSampleRate, const ExtendedTimestamp &timeStamp) { |
Andy Hung | 3028256 | 2018-08-08 18:27:03 -0700 | [diff] [blame] | 2012 | // Make the kernel frametime available. |
| 2013 | const FrameTime ft{ |
| 2014 | timeStamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL], |
| 2015 | timeStamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL]}; |
| 2016 | // ALOGD("FrameTime: %lld %lld", (long long)ft.frames, (long long)ft.timeNs); |
| 2017 | mKernelFrameTime.store(ft); |
| 2018 | if (!audio_is_linear_pcm(mFormat)) { |
| 2019 | return; |
| 2020 | } |
| 2021 | |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 2022 | //update frame map |
Andy Hung | e10393e | 2015-06-12 13:59:33 -0700 | [diff] [blame] | 2023 | mFrameMap.push(trackFramesReleased, sinkFramesWritten); |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 2024 | |
| 2025 | // adjust server times and set drained state. |
| 2026 | // |
| 2027 | // Our timestamps are only updated when the track is on the Thread active list. |
| 2028 | // We need to ensure that tracks are not removed before full drain. |
| 2029 | ExtendedTimestamp local = timeStamp; |
Andy Hung | cef2daa | 2018-06-01 15:31:49 -0700 | [diff] [blame] | 2030 | bool drained = true; // default assume drained, if no server info found |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 2031 | bool checked = false; |
| 2032 | for (int i = ExtendedTimestamp::LOCATION_MAX - 1; |
| 2033 | i >= ExtendedTimestamp::LOCATION_SERVER; --i) { |
| 2034 | // Lookup the track frame corresponding to the sink frame position. |
| 2035 | if (local.mTimeNs[i] > 0) { |
| 2036 | local.mPosition[i] = mFrameMap.findX(local.mPosition[i]); |
| 2037 | // check drain state from the latest stage in the pipeline. |
Andy Hung | 6d7b119 | 2016-05-07 22:59:48 -0700 | [diff] [blame] | 2038 | if (!checked && i <= ExtendedTimestamp::LOCATION_KERNEL) { |
Andy Hung | cef2daa | 2018-06-01 15:31:49 -0700 | [diff] [blame] | 2039 | drained = local.mPosition[i] >= mAudioTrackServerProxy->framesReleased(); |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 2040 | checked = true; |
| 2041 | } |
| 2042 | } |
Andy Hung | e10393e | 2015-06-12 13:59:33 -0700 | [diff] [blame] | 2043 | } |
Andy Hung | cef2daa | 2018-06-01 15:31:49 -0700 | [diff] [blame] | 2044 | |
Andy Hung | 93bb573 | 2023-05-04 21:16:34 -0700 | [diff] [blame] | 2045 | ALOGV("%s: trackFramesReleased:%lld sinkFramesWritten:%lld setDrained: %d", |
| 2046 | __func__, (long long)trackFramesReleased, (long long)sinkFramesWritten, drained); |
Andy Hung | cef2daa | 2018-06-01 15:31:49 -0700 | [diff] [blame] | 2047 | mAudioTrackServerProxy->setDrained(drained); |
Andy Hung | ea2b9c0 | 2016-02-12 17:06:53 -0800 | [diff] [blame] | 2048 | // Set correction for flushed frames that are not accounted for in released. |
Andy Hung | ea2b9c0 | 2016-02-12 17:06:53 -0800 | [diff] [blame] | 2049 | local.mFlushed = mAudioTrackServerProxy->framesFlushed(); |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 2050 | mServerProxy->setTimestamp(local); |
Andy Hung | cef2daa | 2018-06-01 15:31:49 -0700 | [diff] [blame] | 2051 | |
| 2052 | // Compute latency info. |
| 2053 | const bool useTrackTimestamp = !drained; |
| 2054 | const double latencyMs = useTrackTimestamp |
| 2055 | ? local.getOutputServerLatencyMs(sampleRate()) |
| 2056 | : timeStamp.getOutputServerLatencyMs(halSampleRate); |
| 2057 | |
| 2058 | mServerLatencyFromTrack.store(useTrackTimestamp); |
| 2059 | mServerLatencyMs.store(latencyMs); |
Andy Hung | b68f5eb | 2019-12-03 16:49:17 -0800 | [diff] [blame] | 2060 | |
Andy Hung | 6292112 | 2020-05-18 10:47:31 -0700 | [diff] [blame] | 2061 | if (mLogStartCountdown > 0 |
| 2062 | && local.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] > 0 |
| 2063 | && local.mPosition[ExtendedTimestamp::LOCATION_KERNEL] > 0) |
| 2064 | { |
| 2065 | if (mLogStartCountdown > 1) { |
| 2066 | --mLogStartCountdown; |
| 2067 | } else if (latencyMs < mLogLatencyMs) { // wait for latency to stabilize (dip) |
| 2068 | mLogStartCountdown = 0; |
Andy Hung | b68f5eb | 2019-12-03 16:49:17 -0800 | [diff] [blame] | 2069 | // startup is the difference in times for the current timestamp and our start |
| 2070 | double startUpMs = |
Andy Hung | 6292112 | 2020-05-18 10:47:31 -0700 | [diff] [blame] | 2071 | (local.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] - mLogStartTimeNs) * 1e-6; |
Andy Hung | b68f5eb | 2019-12-03 16:49:17 -0800 | [diff] [blame] | 2072 | // adjust for frames played. |
Andy Hung | 6292112 | 2020-05-18 10:47:31 -0700 | [diff] [blame] | 2073 | startUpMs -= (local.mPosition[ExtendedTimestamp::LOCATION_KERNEL] - mLogStartFrames) |
| 2074 | * 1e3 / mSampleRate; |
| 2075 | ALOGV("%s: latencyMs:%lf startUpMs:%lf" |
| 2076 | " localTime:%lld startTime:%lld" |
| 2077 | " localPosition:%lld startPosition:%lld", |
| 2078 | __func__, latencyMs, startUpMs, |
| 2079 | (long long)local.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL], |
Andy Hung | b68f5eb | 2019-12-03 16:49:17 -0800 | [diff] [blame] | 2080 | (long long)mLogStartTimeNs, |
Andy Hung | 6292112 | 2020-05-18 10:47:31 -0700 | [diff] [blame] | 2081 | (long long)local.mPosition[ExtendedTimestamp::LOCATION_KERNEL], |
Andy Hung | b68f5eb | 2019-12-03 16:49:17 -0800 | [diff] [blame] | 2082 | (long long)mLogStartFrames); |
Andy Hung | c2b11cb | 2020-04-22 09:04:01 -0700 | [diff] [blame] | 2083 | mTrackMetrics.logLatencyAndStartup(latencyMs, startUpMs); |
Andy Hung | b68f5eb | 2019-12-03 16:49:17 -0800 | [diff] [blame] | 2084 | } |
Andy Hung | 6292112 | 2020-05-18 10:47:31 -0700 | [diff] [blame] | 2085 | mLogLatencyMs = latencyMs; |
Andy Hung | b68f5eb | 2019-12-03 16:49:17 -0800 | [diff] [blame] | 2086 | } |
Andy Hung | e10393e | 2015-06-12 13:59:33 -0700 | [diff] [blame] | 2087 | } |
| 2088 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 2089 | bool Track::AudioVibrationController::setMute(bool muted) { |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 2090 | const sp<IAfThreadBase> thread = mTrack->mThread.promote(); |
jiabin | 57303cc | 2018-12-18 15:45:57 -0800 | [diff] [blame] | 2091 | if (thread != 0) { |
| 2092 | // Lock for updating mHapticPlaybackEnabled. |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 2093 | audio_utils::lock_guard _l(thread->mutex()); |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 2094 | auto* const playbackThread = thread->asIAfPlaybackThread().get(); |
jiabin | 57303cc | 2018-12-18 15:45:57 -0800 | [diff] [blame] | 2095 | if ((mTrack->channelMask() & AUDIO_CHANNEL_HAPTIC_ALL) != AUDIO_CHANNEL_NONE |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 2096 | && playbackThread->hapticChannelCount() > 0) { |
SPeak Shen | c19aa9e | 2022-11-11 00:28:50 +0800 | [diff] [blame] | 2097 | ALOGD("%s, haptic playback was %s for track %d", |
| 2098 | __func__, muted ? "muted" : "unmuted", mTrack->id()); |
| 2099 | mTrack->setHapticPlaybackEnabled(!muted); |
| 2100 | return true; |
jiabin | 57303cc | 2018-12-18 15:45:57 -0800 | [diff] [blame] | 2101 | } |
| 2102 | } |
SPeak Shen | c19aa9e | 2022-11-11 00:28:50 +0800 | [diff] [blame] | 2103 | return false; |
| 2104 | } |
| 2105 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 2106 | binder::Status Track::AudioVibrationController::mute( |
SPeak Shen | c19aa9e | 2022-11-11 00:28:50 +0800 | [diff] [blame] | 2107 | /*out*/ bool *ret) { |
| 2108 | *ret = setMute(true); |
jiabin | 57303cc | 2018-12-18 15:45:57 -0800 | [diff] [blame] | 2109 | return binder::Status::ok(); |
| 2110 | } |
| 2111 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 2112 | binder::Status Track::AudioVibrationController::unmute( |
jiabin | 57303cc | 2018-12-18 15:45:57 -0800 | [diff] [blame] | 2113 | /*out*/ bool *ret) { |
SPeak Shen | c19aa9e | 2022-11-11 00:28:50 +0800 | [diff] [blame] | 2114 | *ret = setMute(false); |
jiabin | 57303cc | 2018-12-18 15:45:57 -0800 | [diff] [blame] | 2115 | return binder::Status::ok(); |
| 2116 | } |
| 2117 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2118 | // ---------------------------------------------------------------------------- |
Andy Hung | 9d84af5 | 2018-09-12 18:03:44 -0700 | [diff] [blame] | 2119 | #undef LOG_TAG |
| 2120 | #define LOG_TAG "AF::OutputTrack" |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2121 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 2122 | /* static */ |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 2123 | sp<IAfOutputTrack> IAfOutputTrack::create( |
| 2124 | IAfPlaybackThread* playbackThread, |
| 2125 | IAfDuplicatingThread* sourceThread, |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 2126 | uint32_t sampleRate, |
| 2127 | audio_format_t format, |
| 2128 | audio_channel_mask_t channelMask, |
| 2129 | size_t frameCount, |
| 2130 | const AttributionSourceState& attributionSource) { |
| 2131 | return sp<OutputTrack>::make( |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 2132 | playbackThread, |
| 2133 | sourceThread, |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 2134 | sampleRate, |
| 2135 | format, |
| 2136 | channelMask, |
| 2137 | frameCount, |
| 2138 | attributionSource); |
| 2139 | } |
| 2140 | |
| 2141 | OutputTrack::OutputTrack( |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 2142 | IAfPlaybackThread* playbackThread, |
| 2143 | IAfDuplicatingThread* sourceThread, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2144 | uint32_t sampleRate, |
| 2145 | audio_format_t format, |
| 2146 | audio_channel_mask_t channelMask, |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 2147 | size_t frameCount, |
Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 2148 | const AttributionSourceState& attributionSource) |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 2149 | : Track(playbackThread, NULL, AUDIO_STREAM_PATCH, |
Kevin Rocard | 1f564ac | 2018-03-29 13:53:10 -0700 | [diff] [blame] | 2150 | audio_attributes_t{} /* currently unused for output track */, |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 2151 | sampleRate, format, channelMask, frameCount, |
Andy Hung | 8fe6803 | 2017-06-05 16:17:51 -0700 | [diff] [blame] | 2152 | nullptr /* buffer */, (size_t)0 /* bufferSize */, nullptr /* sharedBuffer */, |
Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 2153 | AUDIO_SESSION_NONE, getpid(), attributionSource, AUDIO_OUTPUT_FLAG_NONE, |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 2154 | TYPE_OUTPUT), |
Eric Laurent | 5bba2f6 | 2016-03-18 11:14:14 -0700 | [diff] [blame] | 2155 | mActive(false), mSourceThread(sourceThread) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2156 | { |
| 2157 | |
| 2158 | if (mCblk != NULL) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2159 | mOutBuffer.frameCount = 0; |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 2160 | playbackThread->addOutputTrack_l(this); |
Andy Hung | 9d84af5 | 2018-09-12 18:03:44 -0700 | [diff] [blame] | 2161 | ALOGV("%s(): mCblk %p, mBuffer %p, " |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 2162 | "frameCount %zu, mChannelMask 0x%08x", |
Andy Hung | 9d84af5 | 2018-09-12 18:03:44 -0700 | [diff] [blame] | 2163 | __func__, mCblk, mBuffer, |
Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 2164 | frameCount, mChannelMask); |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 2165 | // since client and server are in the same process, |
| 2166 | // the buffer has the same virtual address on both sides |
Glenn Kasten | 529c61b | 2014-07-18 15:31:02 -0700 | [diff] [blame] | 2167 | mClientProxy = new AudioTrackClientProxy(mCblk, mBuffer, mFrameCount, mFrameSize, |
| 2168 | true /*clientInServer*/); |
Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 2169 | mClientProxy->setVolumeLR(GAIN_MINIFLOAT_PACKED_UNITY); |
Eric Laurent | 8d2d493 | 2013-04-25 12:56:18 -0700 | [diff] [blame] | 2170 | mClientProxy->setSendLevel(0.0); |
| 2171 | mClientProxy->setSampleRate(sampleRate); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2172 | } else { |
Andy Hung | 9d84af5 | 2018-09-12 18:03:44 -0700 | [diff] [blame] | 2173 | ALOGW("%s(%d): Error creating output track on thread %d", |
| 2174 | __func__, mId, (int)mThreadIoHandle); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2175 | } |
| 2176 | } |
| 2177 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 2178 | OutputTrack::~OutputTrack() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2179 | { |
| 2180 | clearBufferQueue(); |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 2181 | // superclass destructor will now delete the server proxy and shared memory both refer to |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2182 | } |
| 2183 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 2184 | status_t OutputTrack::start(AudioSystem::sync_event_t event, |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 2185 | audio_session_t triggerSession) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2186 | { |
| 2187 | status_t status = Track::start(event, triggerSession); |
| 2188 | if (status != NO_ERROR) { |
| 2189 | return status; |
| 2190 | } |
| 2191 | |
| 2192 | mActive = true; |
| 2193 | mRetryCount = 127; |
| 2194 | return status; |
| 2195 | } |
| 2196 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 2197 | void OutputTrack::stop() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2198 | { |
| 2199 | Track::stop(); |
| 2200 | clearBufferQueue(); |
| 2201 | mOutBuffer.frameCount = 0; |
| 2202 | mActive = false; |
| 2203 | } |
| 2204 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 2205 | ssize_t OutputTrack::write(void* data, uint32_t frames) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2206 | { |
Eric Laurent | 19952e1 | 2023-04-20 10:08:29 +0200 | [diff] [blame] | 2207 | if (!mActive && frames != 0) { |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 2208 | const sp<IAfThreadBase> thread = mThread.promote(); |
Andy Hung | 440901d | 2023-06-29 21:19:25 -0700 | [diff] [blame] | 2209 | if (thread != nullptr && thread->inStandby()) { |
Eric Laurent | 19952e1 | 2023-04-20 10:08:29 +0200 | [diff] [blame] | 2210 | // preload one silent buffer to trigger mixer on start() |
| 2211 | ClientProxy::Buffer buf { .mFrameCount = mClientProxy->getStartThresholdInFrames() }; |
| 2212 | status_t status = mClientProxy->obtainBuffer(&buf); |
| 2213 | if (status != NO_ERROR && status != NOT_ENOUGH_DATA && status != WOULD_BLOCK) { |
| 2214 | ALOGE("%s(%d): could not obtain buffer on start", __func__, mId); |
| 2215 | return 0; |
| 2216 | } |
| 2217 | memset(buf.mRaw, 0, buf.mFrameCount * mFrameSize); |
| 2218 | mClientProxy->releaseBuffer(&buf); |
| 2219 | |
| 2220 | (void) start(); |
| 2221 | |
| 2222 | // wait for HAL stream to start before sending actual audio. Doing this on each |
| 2223 | // OutputTrack makes that playback start on all output streams is synchronized. |
| 2224 | // If another OutputTrack has already started it can underrun but this is OK |
| 2225 | // as only silence has been played so far and the retry count is very high on |
| 2226 | // OutputTrack. |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 2227 | auto* const pt = thread->asIAfPlaybackThread().get(); |
Eric Laurent | 19952e1 | 2023-04-20 10:08:29 +0200 | [diff] [blame] | 2228 | if (!pt->waitForHalStart()) { |
| 2229 | ALOGW("%s(%d): timeout waiting for thread to exit standby", __func__, mId); |
| 2230 | stop(); |
| 2231 | return 0; |
| 2232 | } |
| 2233 | |
| 2234 | // enqueue the first buffer and exit so that other OutputTracks will also start before |
| 2235 | // write() is called again and this buffer actually consumed. |
| 2236 | Buffer firstBuffer; |
| 2237 | firstBuffer.frameCount = frames; |
| 2238 | firstBuffer.raw = data; |
| 2239 | queueBuffer(firstBuffer); |
| 2240 | return frames; |
| 2241 | } else { |
| 2242 | (void) start(); |
| 2243 | } |
| 2244 | } |
| 2245 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2246 | Buffer *pInBuffer; |
| 2247 | Buffer inBuffer; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2248 | inBuffer.frameCount = frames; |
Andy Hung | c25b84a | 2015-01-14 19:04:10 -0800 | [diff] [blame] | 2249 | inBuffer.raw = data; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2250 | uint32_t waitTimeLeftMs = mSourceThread->waitTimeMs(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2251 | while (waitTimeLeftMs) { |
| 2252 | // First write pending buffers, then new data |
| 2253 | if (mBufferQueue.size()) { |
| 2254 | pInBuffer = mBufferQueue.itemAt(0); |
| 2255 | } else { |
| 2256 | pInBuffer = &inBuffer; |
| 2257 | } |
| 2258 | |
| 2259 | if (pInBuffer->frameCount == 0) { |
| 2260 | break; |
| 2261 | } |
| 2262 | |
| 2263 | if (mOutBuffer.frameCount == 0) { |
| 2264 | mOutBuffer.frameCount = pInBuffer->frameCount; |
| 2265 | nsecs_t startTime = systemTime(); |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 2266 | status_t status = obtainBuffer(&mOutBuffer, waitTimeLeftMs); |
Eric Laurent | 4d231dc | 2016-03-11 18:38:23 -0800 | [diff] [blame] | 2267 | if (status != NO_ERROR && status != NOT_ENOUGH_DATA) { |
Andy Hung | 9d84af5 | 2018-09-12 18:03:44 -0700 | [diff] [blame] | 2268 | ALOGV("%s(%d): thread %d no more output buffers; status %d", |
| 2269 | __func__, mId, |
| 2270 | (int)mThreadIoHandle, status); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2271 | break; |
| 2272 | } |
| 2273 | uint32_t waitTimeMs = (uint32_t)ns2ms(systemTime() - startTime); |
| 2274 | if (waitTimeLeftMs >= waitTimeMs) { |
| 2275 | waitTimeLeftMs -= waitTimeMs; |
| 2276 | } else { |
| 2277 | waitTimeLeftMs = 0; |
| 2278 | } |
Eric Laurent | 4d231dc | 2016-03-11 18:38:23 -0800 | [diff] [blame] | 2279 | if (status == NOT_ENOUGH_DATA) { |
| 2280 | restartIfDisabled(); |
| 2281 | continue; |
| 2282 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2283 | } |
| 2284 | |
| 2285 | uint32_t outFrames = pInBuffer->frameCount > mOutBuffer.frameCount ? mOutBuffer.frameCount : |
| 2286 | pInBuffer->frameCount; |
Andy Hung | c25b84a | 2015-01-14 19:04:10 -0800 | [diff] [blame] | 2287 | memcpy(mOutBuffer.raw, pInBuffer->raw, outFrames * mFrameSize); |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 2288 | Proxy::Buffer buf; |
| 2289 | buf.mFrameCount = outFrames; |
| 2290 | buf.mRaw = NULL; |
| 2291 | mClientProxy->releaseBuffer(&buf); |
Eric Laurent | 4d231dc | 2016-03-11 18:38:23 -0800 | [diff] [blame] | 2292 | restartIfDisabled(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2293 | pInBuffer->frameCount -= outFrames; |
Andy Hung | c25b84a | 2015-01-14 19:04:10 -0800 | [diff] [blame] | 2294 | pInBuffer->raw = (int8_t *)pInBuffer->raw + outFrames * mFrameSize; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2295 | mOutBuffer.frameCount -= outFrames; |
Andy Hung | c25b84a | 2015-01-14 19:04:10 -0800 | [diff] [blame] | 2296 | mOutBuffer.raw = (int8_t *)mOutBuffer.raw + outFrames * mFrameSize; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2297 | |
| 2298 | if (pInBuffer->frameCount == 0) { |
| 2299 | if (mBufferQueue.size()) { |
| 2300 | mBufferQueue.removeAt(0); |
Andy Hung | c25b84a | 2015-01-14 19:04:10 -0800 | [diff] [blame] | 2301 | free(pInBuffer->mBuffer); |
Yunlian Jiang | 8adc808 | 2017-06-06 15:59:44 -0700 | [diff] [blame] | 2302 | if (pInBuffer != &inBuffer) { |
| 2303 | delete pInBuffer; |
| 2304 | } |
Andy Hung | 9d84af5 | 2018-09-12 18:03:44 -0700 | [diff] [blame] | 2305 | ALOGV("%s(%d): thread %d released overflow buffer %zu", |
| 2306 | __func__, mId, |
| 2307 | (int)mThreadIoHandle, mBufferQueue.size()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2308 | } else { |
| 2309 | break; |
| 2310 | } |
| 2311 | } |
| 2312 | } |
| 2313 | |
| 2314 | // If we could not write all frames, allocate a buffer and queue it for next time. |
| 2315 | if (inBuffer.frameCount) { |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 2316 | const sp<IAfThreadBase> thread = mThread.promote(); |
Andy Hung | 440901d | 2023-06-29 21:19:25 -0700 | [diff] [blame] | 2317 | if (thread != nullptr && !thread->inStandby()) { |
Eric Laurent | 19952e1 | 2023-04-20 10:08:29 +0200 | [diff] [blame] | 2318 | queueBuffer(inBuffer); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2319 | } |
| 2320 | } |
| 2321 | |
Andy Hung | c25b84a | 2015-01-14 19:04:10 -0800 | [diff] [blame] | 2322 | // Calling write() with a 0 length buffer means that no more data will be written: |
| 2323 | // We rely on stop() to set the appropriate flags to allow the remaining frames to play out. |
| 2324 | if (frames == 0 && mBufferQueue.size() == 0 && mActive) { |
| 2325 | stop(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2326 | } |
| 2327 | |
Andy Hung | 1c86ebe | 2018-05-29 20:29:08 -0700 | [diff] [blame] | 2328 | return frames - inBuffer.frameCount; // number of frames consumed. |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2329 | } |
| 2330 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 2331 | void OutputTrack::queueBuffer(Buffer& inBuffer) { |
Eric Laurent | 19952e1 | 2023-04-20 10:08:29 +0200 | [diff] [blame] | 2332 | |
| 2333 | if (mBufferQueue.size() < kMaxOverFlowBuffers) { |
| 2334 | Buffer *pInBuffer = new Buffer; |
| 2335 | const size_t bufferSize = inBuffer.frameCount * mFrameSize; |
| 2336 | pInBuffer->mBuffer = malloc(bufferSize); |
| 2337 | LOG_ALWAYS_FATAL_IF(pInBuffer->mBuffer == nullptr, |
| 2338 | "%s: Unable to malloc size %zu", __func__, bufferSize); |
| 2339 | pInBuffer->frameCount = inBuffer.frameCount; |
| 2340 | pInBuffer->raw = pInBuffer->mBuffer; |
| 2341 | memcpy(pInBuffer->raw, inBuffer.raw, inBuffer.frameCount * mFrameSize); |
| 2342 | mBufferQueue.add(pInBuffer); |
| 2343 | ALOGV("%s(%d): thread %d adding overflow buffer %zu", __func__, mId, |
| 2344 | (int)mThreadIoHandle, mBufferQueue.size()); |
| 2345 | // audio data is consumed (stored locally); set frameCount to 0. |
| 2346 | inBuffer.frameCount = 0; |
| 2347 | } else { |
| 2348 | ALOGW("%s(%d): thread %d no more overflow buffers", |
| 2349 | __func__, mId, (int)mThreadIoHandle); |
| 2350 | // TODO: return error for this. |
| 2351 | } |
| 2352 | } |
| 2353 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 2354 | void OutputTrack::copyMetadataTo(MetadataInserter& backInserter) const |
Kevin Rocard | 1238109 | 2018-04-11 09:19:59 -0700 | [diff] [blame] | 2355 | { |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 2356 | audio_utils::lock_guard lock(trackMetadataMutex()); |
Kevin Rocard | 1238109 | 2018-04-11 09:19:59 -0700 | [diff] [blame] | 2357 | backInserter = std::copy(mTrackMetadatas.begin(), mTrackMetadatas.end(), backInserter); |
| 2358 | } |
| 2359 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 2360 | void OutputTrack::setMetadatas(const SourceMetadatas& metadatas) { |
Kevin Rocard | 1238109 | 2018-04-11 09:19:59 -0700 | [diff] [blame] | 2361 | { |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 2362 | audio_utils::lock_guard lock(trackMetadataMutex()); |
Kevin Rocard | 1238109 | 2018-04-11 09:19:59 -0700 | [diff] [blame] | 2363 | mTrackMetadatas = metadatas; |
| 2364 | } |
| 2365 | // No need to adjust metadata track volumes as OutputTrack volumes are always 0dBFS. |
| 2366 | setMetadataHasChanged(); |
| 2367 | } |
| 2368 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 2369 | status_t OutputTrack::obtainBuffer( |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2370 | AudioBufferProvider::Buffer* buffer, uint32_t waitTimeMs) |
| 2371 | { |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 2372 | ClientProxy::Buffer buf; |
| 2373 | buf.mFrameCount = buffer->frameCount; |
| 2374 | struct timespec timeout; |
| 2375 | timeout.tv_sec = waitTimeMs / 1000; |
| 2376 | timeout.tv_nsec = (int) (waitTimeMs % 1000) * 1000000; |
| 2377 | status_t status = mClientProxy->obtainBuffer(&buf, &timeout); |
| 2378 | buffer->frameCount = buf.mFrameCount; |
| 2379 | buffer->raw = buf.mRaw; |
| 2380 | return status; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2381 | } |
| 2382 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 2383 | void OutputTrack::clearBufferQueue() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2384 | { |
| 2385 | size_t size = mBufferQueue.size(); |
| 2386 | |
| 2387 | for (size_t i = 0; i < size; i++) { |
| 2388 | Buffer *pBuffer = mBufferQueue.itemAt(i); |
Andy Hung | c25b84a | 2015-01-14 19:04:10 -0800 | [diff] [blame] | 2389 | free(pBuffer->mBuffer); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2390 | delete pBuffer; |
| 2391 | } |
| 2392 | mBufferQueue.clear(); |
| 2393 | } |
| 2394 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 2395 | void OutputTrack::restartIfDisabled() |
Eric Laurent | 4d231dc | 2016-03-11 18:38:23 -0800 | [diff] [blame] | 2396 | { |
| 2397 | int32_t flags = android_atomic_and(~CBLK_DISABLED, &mCblk->mFlags); |
| 2398 | if (mActive && (flags & CBLK_DISABLED)) { |
| 2399 | start(); |
| 2400 | } |
| 2401 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2402 | |
Andy Hung | 9d84af5 | 2018-09-12 18:03:44 -0700 | [diff] [blame] | 2403 | // ---------------------------------------------------------------------------- |
| 2404 | #undef LOG_TAG |
| 2405 | #define LOG_TAG "AF::PatchTrack" |
| 2406 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 2407 | /* static */ |
| 2408 | sp<IAfPatchTrack> IAfPatchTrack::create( |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 2409 | IAfPlaybackThread* playbackThread, |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 2410 | audio_stream_type_t streamType, |
| 2411 | uint32_t sampleRate, |
| 2412 | audio_channel_mask_t channelMask, |
| 2413 | audio_format_t format, |
| 2414 | size_t frameCount, |
| 2415 | void* buffer, |
| 2416 | size_t bufferSize, |
| 2417 | audio_output_flags_t flags, |
| 2418 | const Timeout& timeout, |
| 2419 | size_t frameCountToBeReady /** Default behaviour is to start |
| 2420 | * as soon as possible to have |
| 2421 | * the lowest possible latency |
| 2422 | * even if it might glitch. */) |
| 2423 | { |
| 2424 | return sp<PatchTrack>::make( |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 2425 | playbackThread, |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 2426 | streamType, |
| 2427 | sampleRate, |
| 2428 | channelMask, |
| 2429 | format, |
| 2430 | frameCount, |
| 2431 | buffer, |
| 2432 | bufferSize, |
| 2433 | flags, |
| 2434 | timeout, |
| 2435 | frameCountToBeReady); |
| 2436 | } |
| 2437 | |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 2438 | PatchTrack::PatchTrack(IAfPlaybackThread* playbackThread, |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 2439 | audio_stream_type_t streamType, |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2440 | uint32_t sampleRate, |
| 2441 | audio_channel_mask_t channelMask, |
| 2442 | audio_format_t format, |
| 2443 | size_t frameCount, |
| 2444 | void *buffer, |
Andy Hung | 8fe6803 | 2017-06-05 16:17:51 -0700 | [diff] [blame] | 2445 | size_t bufferSize, |
Kevin Rocard | 45986c7 | 2018-12-18 18:22:59 -0800 | [diff] [blame] | 2446 | audio_output_flags_t flags, |
Kevin Rocard | 01c7d9e | 2019-09-18 11:24:52 +0100 | [diff] [blame] | 2447 | const Timeout& timeout, |
| 2448 | size_t frameCountToBeReady) |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 2449 | : Track(playbackThread, NULL, streamType, |
Kevin Rocard | 1f564ac | 2018-03-29 13:53:10 -0700 | [diff] [blame] | 2450 | audio_attributes_t{} /* currently unused for patch track */, |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 2451 | sampleRate, format, channelMask, frameCount, |
Andy Hung | 8fe6803 | 2017-06-05 16:17:51 -0700 | [diff] [blame] | 2452 | buffer, bufferSize, nullptr /* sharedBuffer */, |
Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 2453 | AUDIO_SESSION_NONE, getpid(), audioServerAttributionSource(getpid()), flags, |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 2454 | TYPE_PATCH, AUDIO_PORT_HANDLE_NONE, frameCountToBeReady), |
gaoxiupei | 8e3a568 | 2023-07-07 20:30:23 +0800 | [diff] [blame] | 2455 | PatchTrackBase(mCblk ? new ClientProxy(mCblk, mBuffer, frameCount, mFrameSize, true, true) |
| 2456 | : nullptr, |
Andy Hung | 4fd6901 | 2023-07-14 16:57:01 -0700 | [diff] [blame] | 2457 | playbackThread, timeout) |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2458 | { |
Andy Hung | 9d84af5 | 2018-09-12 18:03:44 -0700 | [diff] [blame] | 2459 | ALOGV("%s(%d): sampleRate %d mPeerTimeout %d.%03d sec", |
| 2460 | __func__, mId, sampleRate, |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2461 | (int)mPeerTimeout.tv_sec, |
| 2462 | (int)(mPeerTimeout.tv_nsec / 1000000)); |
| 2463 | } |
| 2464 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 2465 | PatchTrack::~PatchTrack() |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2466 | { |
Andy Hung | abfab20 | 2019-03-07 19:45:54 -0800 | [diff] [blame] | 2467 | ALOGV("%s(%d)", __func__, mId); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2468 | } |
| 2469 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 2470 | size_t PatchTrack::framesReady() const |
Mikhail Naganov | caf5994 | 2019-09-25 14:05:29 -0700 | [diff] [blame] | 2471 | { |
| 2472 | if (mPeerProxy && mPeerProxy->producesBufferOnDemand()) { |
| 2473 | return std::numeric_limits<size_t>::max(); |
| 2474 | } else { |
| 2475 | return Track::framesReady(); |
| 2476 | } |
| 2477 | } |
| 2478 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 2479 | status_t PatchTrack::start(AudioSystem::sync_event_t event, |
Kevin Rocard | 5f2136e | 2018-05-11 22:03:00 -0700 | [diff] [blame] | 2480 | audio_session_t triggerSession) |
Eric Laurent | 4d231dc | 2016-03-11 18:38:23 -0800 | [diff] [blame] | 2481 | { |
| 2482 | status_t status = Track::start(event, triggerSession); |
| 2483 | if (status != NO_ERROR) { |
| 2484 | return status; |
| 2485 | } |
| 2486 | android_atomic_and(~CBLK_DISABLED, &mCblk->mFlags); |
| 2487 | return status; |
| 2488 | } |
| 2489 | |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2490 | // AudioBufferProvider interface |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 2491 | status_t PatchTrack::getNextBuffer( |
Glenn Kasten | d79072e | 2016-01-06 08:41:20 -0800 | [diff] [blame] | 2492 | AudioBufferProvider::Buffer* buffer) |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2493 | { |
Andy Hung | 9d84af5 | 2018-09-12 18:03:44 -0700 | [diff] [blame] | 2494 | ALOG_ASSERT(mPeerProxy != 0, "%s(%d): called without peer proxy", __func__, mId); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2495 | Proxy::Buffer buf; |
| 2496 | buf.mFrameCount = buffer->frameCount; |
Mikhail Naganov | 938be41 | 2019-09-04 11:38:47 -0700 | [diff] [blame] | 2497 | if (ATRACE_ENABLED()) { |
| 2498 | std::string traceName("PTnReq"); |
| 2499 | traceName += std::to_string(id()); |
| 2500 | ATRACE_INT(traceName.c_str(), buf.mFrameCount); |
| 2501 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2502 | status_t status = mPeerProxy->obtainBuffer(&buf, &mPeerTimeout); |
Andy Hung | 9d84af5 | 2018-09-12 18:03:44 -0700 | [diff] [blame] | 2503 | ALOGV_IF(status != NO_ERROR, "%s(%d): getNextBuffer status %d", __func__, mId, status); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 2504 | buffer->frameCount = buf.mFrameCount; |
Mikhail Naganov | 938be41 | 2019-09-04 11:38:47 -0700 | [diff] [blame] | 2505 | if (ATRACE_ENABLED()) { |
| 2506 | std::string traceName("PTnObt"); |
| 2507 | traceName += std::to_string(id()); |
| 2508 | ATRACE_INT(traceName.c_str(), buf.mFrameCount); |
| 2509 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2510 | if (buf.mFrameCount == 0) { |
| 2511 | return WOULD_BLOCK; |
| 2512 | } |
Glenn Kasten | d79072e | 2016-01-06 08:41:20 -0800 | [diff] [blame] | 2513 | status = Track::getNextBuffer(buffer); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2514 | return status; |
| 2515 | } |
| 2516 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 2517 | void PatchTrack::releaseBuffer(AudioBufferProvider::Buffer* buffer) |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2518 | { |
Andy Hung | 9d84af5 | 2018-09-12 18:03:44 -0700 | [diff] [blame] | 2519 | ALOG_ASSERT(mPeerProxy != 0, "%s(%d): called without peer proxy", __func__, mId); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2520 | Proxy::Buffer buf; |
| 2521 | buf.mFrameCount = buffer->frameCount; |
| 2522 | buf.mRaw = buffer->raw; |
| 2523 | mPeerProxy->releaseBuffer(&buf); |
Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 2524 | TrackBase::releaseBuffer(buffer); // Note: this is the base class. |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2525 | } |
| 2526 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 2527 | status_t PatchTrack::obtainBuffer(Proxy::Buffer* buffer, |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2528 | const struct timespec *timeOut) |
| 2529 | { |
Eric Laurent | 4d231dc | 2016-03-11 18:38:23 -0800 | [diff] [blame] | 2530 | status_t status = NO_ERROR; |
| 2531 | static const int32_t kMaxTries = 5; |
| 2532 | int32_t tryCounter = kMaxTries; |
Andy Hung | f62e1a2 | 2018-05-08 18:32:11 -0700 | [diff] [blame] | 2533 | const size_t originalFrameCount = buffer->mFrameCount; |
Eric Laurent | 4d231dc | 2016-03-11 18:38:23 -0800 | [diff] [blame] | 2534 | do { |
| 2535 | if (status == NOT_ENOUGH_DATA) { |
| 2536 | restartIfDisabled(); |
Andy Hung | f62e1a2 | 2018-05-08 18:32:11 -0700 | [diff] [blame] | 2537 | buffer->mFrameCount = originalFrameCount; // cleared on error, must be restored. |
Eric Laurent | 4d231dc | 2016-03-11 18:38:23 -0800 | [diff] [blame] | 2538 | } |
| 2539 | status = mProxy->obtainBuffer(buffer, timeOut); |
| 2540 | } while ((status == NOT_ENOUGH_DATA) && (tryCounter-- > 0)); |
| 2541 | return status; |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2542 | } |
| 2543 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 2544 | void PatchTrack::releaseBuffer(Proxy::Buffer* buffer) |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2545 | { |
| 2546 | mProxy->releaseBuffer(buffer); |
Eric Laurent | 4d231dc | 2016-03-11 18:38:23 -0800 | [diff] [blame] | 2547 | restartIfDisabled(); |
naoki miyazu | f37f998 | 2019-11-28 11:18:18 +0900 | [diff] [blame] | 2548 | |
| 2549 | // Check if the PatchTrack has enough data to write once in releaseBuffer(). |
| 2550 | // If not, prevent an underrun from occurring by moving the track into FS_FILLING; |
| 2551 | // this logic avoids glitches when suspending A2DP with AudioPlaybackCapture. |
| 2552 | // TODO: perhaps underrun avoidance could be a track property checked in isReady() instead. |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 2553 | if (mFillingStatus == FS_ACTIVE |
naoki miyazu | f37f998 | 2019-11-28 11:18:18 +0900 | [diff] [blame] | 2554 | && audio_is_linear_pcm(mFormat) |
| 2555 | && !isOffloadedOrDirect()) { |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 2556 | if (const sp<IAfThreadBase> thread = mThread.promote(); |
naoki miyazu | f37f998 | 2019-11-28 11:18:18 +0900 | [diff] [blame] | 2557 | thread != 0) { |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 2558 | auto* const playbackThread = thread->asIAfPlaybackThread().get(); |
naoki miyazu | f37f998 | 2019-11-28 11:18:18 +0900 | [diff] [blame] | 2559 | const size_t frameCount = playbackThread->frameCount() * sampleRate() |
| 2560 | / playbackThread->sampleRate(); |
| 2561 | if (framesReady() < frameCount) { |
| 2562 | ALOGD("%s(%d) Not enough data, wait for buffer to fill", __func__, mId); |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 2563 | mFillingStatus = FS_FILLING; |
naoki miyazu | f37f998 | 2019-11-28 11:18:18 +0900 | [diff] [blame] | 2564 | } |
| 2565 | } |
| 2566 | } |
Eric Laurent | 4d231dc | 2016-03-11 18:38:23 -0800 | [diff] [blame] | 2567 | } |
| 2568 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 2569 | void PatchTrack::restartIfDisabled() |
Eric Laurent | 4d231dc | 2016-03-11 18:38:23 -0800 | [diff] [blame] | 2570 | { |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2571 | if (android_atomic_and(~CBLK_DISABLED, &mCblk->mFlags) & CBLK_DISABLED) { |
Andy Hung | 9d84af5 | 2018-09-12 18:03:44 -0700 | [diff] [blame] | 2572 | ALOGW("%s(%d): disabled due to previous underrun, restarting", __func__, mId); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2573 | start(); |
| 2574 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2575 | } |
| 2576 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2577 | // ---------------------------------------------------------------------------- |
| 2578 | // Record |
| 2579 | // ---------------------------------------------------------------------------- |
Jean-Michel Trivi | ddf87ef | 2019-08-20 15:42:04 -0700 | [diff] [blame] | 2580 | |
| 2581 | |
Andy Hung | 9d84af5 | 2018-09-12 18:03:44 -0700 | [diff] [blame] | 2582 | #undef LOG_TAG |
| 2583 | #define LOG_TAG "AF::RecordHandle" |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2584 | |
Andy Hung | a5a7fc9 | 2023-06-23 19:27:19 -0700 | [diff] [blame] | 2585 | class RecordHandle : public android::media::BnAudioRecord { |
| 2586 | public: |
Andy Hung | d29af63 | 2023-06-23 19:27:19 -0700 | [diff] [blame] | 2587 | explicit RecordHandle(const sp<IAfRecordTrack>& recordTrack); |
Andy Hung | a5a7fc9 | 2023-06-23 19:27:19 -0700 | [diff] [blame] | 2588 | ~RecordHandle() override; |
| 2589 | binder::Status start(int /*AudioSystem::sync_event_t*/ event, |
| 2590 | int /*audio_session_t*/ triggerSession) final; |
| 2591 | binder::Status stop() final; |
| 2592 | binder::Status getActiveMicrophones( |
| 2593 | std::vector<media::MicrophoneInfoFw>* activeMicrophones) final; |
| 2594 | binder::Status setPreferredMicrophoneDirection( |
| 2595 | int /*audio_microphone_direction_t*/ direction) final; |
| 2596 | binder::Status setPreferredMicrophoneFieldDimension(float zoom) final; |
| 2597 | binder::Status shareAudioHistory( |
| 2598 | const std::string& sharedAudioPackageName, int64_t sharedAudioStartMs) final; |
| 2599 | |
| 2600 | private: |
Andy Hung | d29af63 | 2023-06-23 19:27:19 -0700 | [diff] [blame] | 2601 | const sp<IAfRecordTrack> mRecordTrack; |
Andy Hung | a5a7fc9 | 2023-06-23 19:27:19 -0700 | [diff] [blame] | 2602 | |
| 2603 | // for use from destructor |
| 2604 | void stop_nonvirtual(); |
| 2605 | }; |
| 2606 | |
| 2607 | /* static */ |
Andy Hung | d29af63 | 2023-06-23 19:27:19 -0700 | [diff] [blame] | 2608 | sp<media::IAudioRecord> IAfRecordTrack::createIAudioRecordAdapter( |
| 2609 | const sp<IAfRecordTrack>& recordTrack) { |
Andy Hung | a5a7fc9 | 2023-06-23 19:27:19 -0700 | [diff] [blame] | 2610 | return sp<RecordHandle>::make(recordTrack); |
| 2611 | } |
| 2612 | |
| 2613 | RecordHandle::RecordHandle( |
Andy Hung | d29af63 | 2023-06-23 19:27:19 -0700 | [diff] [blame] | 2614 | const sp<IAfRecordTrack>& recordTrack) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2615 | : BnAudioRecord(), |
| 2616 | mRecordTrack(recordTrack) |
| 2617 | { |
Andy Hung | 225aef6 | 2022-12-06 16:33:20 -0800 | [diff] [blame] | 2618 | setMinSchedulerPolicy(SCHED_NORMAL, ANDROID_PRIORITY_AUDIO); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2619 | } |
| 2620 | |
Andy Hung | a5a7fc9 | 2023-06-23 19:27:19 -0700 | [diff] [blame] | 2621 | RecordHandle::~RecordHandle() { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2622 | stop_nonvirtual(); |
| 2623 | mRecordTrack->destroy(); |
| 2624 | } |
| 2625 | |
Andy Hung | a5a7fc9 | 2023-06-23 19:27:19 -0700 | [diff] [blame] | 2626 | binder::Status RecordHandle::start(int /*AudioSystem::sync_event_t*/ event, |
Ivan Lozano | ff6900d | 2017-08-01 15:47:38 -0700 | [diff] [blame] | 2627 | int /*audio_session_t*/ triggerSession) { |
Andy Hung | 9d84af5 | 2018-09-12 18:03:44 -0700 | [diff] [blame] | 2628 | ALOGV("%s()", __func__); |
Andy Hung | 1131b6e | 2020-12-08 20:47:45 -0800 | [diff] [blame] | 2629 | return binderStatusFromStatusT( |
Ivan Lozano | ff6900d | 2017-08-01 15:47:38 -0700 | [diff] [blame] | 2630 | mRecordTrack->start((AudioSystem::sync_event_t)event, (audio_session_t) triggerSession)); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2631 | } |
| 2632 | |
Andy Hung | a5a7fc9 | 2023-06-23 19:27:19 -0700 | [diff] [blame] | 2633 | binder::Status RecordHandle::stop() { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2634 | stop_nonvirtual(); |
Ivan Lozano | ff6900d | 2017-08-01 15:47:38 -0700 | [diff] [blame] | 2635 | return binder::Status::ok(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2636 | } |
| 2637 | |
Andy Hung | a5a7fc9 | 2023-06-23 19:27:19 -0700 | [diff] [blame] | 2638 | void RecordHandle::stop_nonvirtual() { |
Andy Hung | 9d84af5 | 2018-09-12 18:03:44 -0700 | [diff] [blame] | 2639 | ALOGV("%s()", __func__); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2640 | mRecordTrack->stop(); |
| 2641 | } |
| 2642 | |
Andy Hung | a5a7fc9 | 2023-06-23 19:27:19 -0700 | [diff] [blame] | 2643 | binder::Status RecordHandle::getActiveMicrophones( |
Mikhail Naganov | d5d9de7 | 2023-02-13 11:45:03 -0800 | [diff] [blame] | 2644 | std::vector<media::MicrophoneInfoFw>* activeMicrophones) { |
Andy Hung | 9d84af5 | 2018-09-12 18:03:44 -0700 | [diff] [blame] | 2645 | ALOGV("%s()", __func__); |
Mikhail Naganov | d5d9de7 | 2023-02-13 11:45:03 -0800 | [diff] [blame] | 2646 | return binderStatusFromStatusT(mRecordTrack->getActiveMicrophones(activeMicrophones)); |
jiabin | 653cc0a | 2018-01-17 17:54:10 -0800 | [diff] [blame] | 2647 | } |
| 2648 | |
Andy Hung | a5a7fc9 | 2023-06-23 19:27:19 -0700 | [diff] [blame] | 2649 | binder::Status RecordHandle::setPreferredMicrophoneDirection( |
Paul McLean | 03a6e6a | 2018-12-04 10:54:13 -0700 | [diff] [blame] | 2650 | int /*audio_microphone_direction_t*/ direction) { |
| 2651 | ALOGV("%s()", __func__); |
Andy Hung | 1131b6e | 2020-12-08 20:47:45 -0800 | [diff] [blame] | 2652 | return binderStatusFromStatusT(mRecordTrack->setPreferredMicrophoneDirection( |
Paul McLean | 03a6e6a | 2018-12-04 10:54:13 -0700 | [diff] [blame] | 2653 | static_cast<audio_microphone_direction_t>(direction))); |
| 2654 | } |
| 2655 | |
Andy Hung | a5a7fc9 | 2023-06-23 19:27:19 -0700 | [diff] [blame] | 2656 | binder::Status RecordHandle::setPreferredMicrophoneFieldDimension(float zoom) { |
Paul McLean | 03a6e6a | 2018-12-04 10:54:13 -0700 | [diff] [blame] | 2657 | ALOGV("%s()", __func__); |
Andy Hung | 1131b6e | 2020-12-08 20:47:45 -0800 | [diff] [blame] | 2658 | return binderStatusFromStatusT(mRecordTrack->setPreferredMicrophoneFieldDimension(zoom)); |
Paul McLean | 03a6e6a | 2018-12-04 10:54:13 -0700 | [diff] [blame] | 2659 | } |
| 2660 | |
Andy Hung | a5a7fc9 | 2023-06-23 19:27:19 -0700 | [diff] [blame] | 2661 | binder::Status RecordHandle::shareAudioHistory( |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 2662 | const std::string& sharedAudioPackageName, int64_t sharedAudioStartMs) { |
| 2663 | return binderStatusFromStatusT( |
| 2664 | mRecordTrack->shareAudioHistory(sharedAudioPackageName, sharedAudioStartMs)); |
| 2665 | } |
| 2666 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2667 | // ---------------------------------------------------------------------------- |
Andy Hung | 9d84af5 | 2018-09-12 18:03:44 -0700 | [diff] [blame] | 2668 | #undef LOG_TAG |
| 2669 | #define LOG_TAG "AF::RecordTrack" |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2670 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 2671 | |
Andy Hung | 99b1ba6 | 2023-07-14 11:00:08 -0700 | [diff] [blame] | 2672 | /* static */ |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 2673 | sp<IAfRecordTrack> IAfRecordTrack::create(IAfRecordThread* thread, |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 2674 | const sp<Client>& client, |
| 2675 | const audio_attributes_t& attr, |
| 2676 | uint32_t sampleRate, |
| 2677 | audio_format_t format, |
| 2678 | audio_channel_mask_t channelMask, |
| 2679 | size_t frameCount, |
| 2680 | void* buffer, |
| 2681 | size_t bufferSize, |
| 2682 | audio_session_t sessionId, |
| 2683 | pid_t creatorPid, |
| 2684 | const AttributionSourceState& attributionSource, |
| 2685 | audio_input_flags_t flags, |
| 2686 | track_type type, |
| 2687 | audio_port_handle_t portId, |
| 2688 | int32_t startFrames) |
| 2689 | { |
| 2690 | return sp<RecordTrack>::make( |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 2691 | thread, |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 2692 | client, |
| 2693 | attr, |
| 2694 | sampleRate, |
| 2695 | format, |
| 2696 | channelMask, |
| 2697 | frameCount, |
| 2698 | buffer, |
| 2699 | bufferSize, |
| 2700 | sessionId, |
| 2701 | creatorPid, |
| 2702 | attributionSource, |
| 2703 | flags, |
| 2704 | type, |
| 2705 | portId, |
| 2706 | startFrames); |
| 2707 | } |
| 2708 | |
Glenn Kasten | 05997e2 | 2014-03-13 15:08:33 -0700 | [diff] [blame] | 2709 | // RecordTrack constructor must be called with AudioFlinger::mLock and ThreadBase::mLock held |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 2710 | RecordTrack::RecordTrack( |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 2711 | IAfRecordThread* thread, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2712 | const sp<Client>& client, |
Kevin Rocard | 1f564ac | 2018-03-29 13:53:10 -0700 | [diff] [blame] | 2713 | const audio_attributes_t& attr, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2714 | uint32_t sampleRate, |
| 2715 | audio_format_t format, |
| 2716 | audio_channel_mask_t channelMask, |
| 2717 | size_t frameCount, |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2718 | void *buffer, |
Andy Hung | 8fe6803 | 2017-06-05 16:17:51 -0700 | [diff] [blame] | 2719 | size_t bufferSize, |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 2720 | audio_session_t sessionId, |
Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 2721 | pid_t creatorPid, |
Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 2722 | const AttributionSourceState& attributionSource, |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 2723 | audio_input_flags_t flags, |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 2724 | track_type type, |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 2725 | audio_port_handle_t portId, |
Eric Laurent | 2407ce3 | 2021-04-26 14:56:03 +0200 | [diff] [blame] | 2726 | int32_t startFrames) |
Kevin Rocard | 1f564ac | 2018-03-29 13:53:10 -0700 | [diff] [blame] | 2727 | : TrackBase(thread, client, attr, sampleRate, format, |
Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 2728 | channelMask, frameCount, buffer, bufferSize, sessionId, |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 2729 | creatorPid, |
Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 2730 | VALUE_OR_FATAL(aidl2legacy_int32_t_uid_t(attributionSource.uid)), |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 2731 | false /*isOut*/, |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2732 | (type == TYPE_DEFAULT) ? |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 2733 | ((flags & AUDIO_INPUT_FLAG_FAST) ? ALLOC_PIPE : ALLOC_CBLK) : |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2734 | ((buffer == NULL) ? ALLOC_LOCAL : ALLOC_NONE), |
Andy Hung | b68f5eb | 2019-12-03 16:49:17 -0800 | [diff] [blame] | 2735 | type, portId, |
| 2736 | std::string(AMEDIAMETRICS_KEY_PREFIX_AUDIO_RECORD) + std::to_string(portId)), |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 2737 | mOverflow(false), |
Andy Hung | 4c6afaf | 2015-06-12 18:23:35 -0700 | [diff] [blame] | 2738 | mResamplerBufferProvider(NULL), // initialize in case of early constructor exit |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 2739 | mRecordBufferConverter(NULL), |
jiabin | 9378eb9 | 2018-05-02 15:26:35 -0700 | [diff] [blame] | 2740 | mFlags(flags), |
Jean-Michel Trivi | ddf87ef | 2019-08-20 15:42:04 -0700 | [diff] [blame] | 2741 | mSilenced(false), |
Eric Laurent | 2407ce3 | 2021-04-26 14:56:03 +0200 | [diff] [blame] | 2742 | mStartFrames(startFrames) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2743 | { |
Glenn Kasten | 3ef14ef | 2014-03-13 15:08:51 -0700 | [diff] [blame] | 2744 | if (mCblk == NULL) { |
| 2745 | return; |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 2746 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 2747 | |
Mikhail Naganov | 7c6ae98 | 2018-06-14 12:33:38 -0700 | [diff] [blame] | 2748 | if (!isDirect()) { |
| 2749 | mRecordBufferConverter = new RecordBufferConverter( |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 2750 | thread->channelMask(), thread->format(), thread->sampleRate(), |
Mikhail Naganov | 7c6ae98 | 2018-06-14 12:33:38 -0700 | [diff] [blame] | 2751 | channelMask, format, sampleRate); |
| 2752 | // Check if the RecordBufferConverter construction was successful. |
| 2753 | // If not, don't continue with construction. |
| 2754 | // |
| 2755 | // NOTE: It would be extremely rare that the record track cannot be created |
| 2756 | // for the current device, but a pending or future device change would make |
| 2757 | // the record track configuration valid. |
| 2758 | if (mRecordBufferConverter->initCheck() != NO_ERROR) { |
Andy Hung | 9d84af5 | 2018-09-12 18:03:44 -0700 | [diff] [blame] | 2759 | ALOGE("%s(%d): RecordTrack unable to create record buffer converter", __func__, mId); |
Mikhail Naganov | 7c6ae98 | 2018-06-14 12:33:38 -0700 | [diff] [blame] | 2760 | return; |
| 2761 | } |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 2762 | } |
| 2763 | |
Andy Hung | 6ae5843 | 2016-02-16 18:32:24 -0800 | [diff] [blame] | 2764 | mServerProxy = new AudioRecordServerProxy(mCblk, mBuffer, frameCount, |
Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 2765 | mFrameSize, !isExternalTrack()); |
Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 2766 | |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 2767 | mResamplerBufferProvider = new ResamplerBufferProvider(this); |
Glenn Kasten | c263ca0 | 2014-06-04 20:31:46 -0700 | [diff] [blame] | 2768 | |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 2769 | if (flags & AUDIO_INPUT_FLAG_FAST) { |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 2770 | ALOG_ASSERT(thread->fastTrackAvailable()); |
| 2771 | thread->setFastTrackAvailable(false); |
Andy Hung | 000adb5 | 2018-06-01 15:43:26 -0700 | [diff] [blame] | 2772 | } else { |
| 2773 | // TODO: only Normal Record has timestamps (Fast Record does not). |
Andy Hung | 5d3d956 | 2018-10-04 19:27:26 -0700 | [diff] [blame] | 2774 | mServerLatencySupported = checkServerLatencySupported(mFormat, flags); |
Glenn Kasten | c263ca0 | 2014-06-04 20:31:46 -0700 | [diff] [blame] | 2775 | } |
Andy Hung | 8946a28 | 2018-04-19 20:04:56 -0700 | [diff] [blame] | 2776 | #ifdef TEE_SINK |
| 2777 | mTee.setId(std::string("_") + std::to_string(mThreadIoHandle) |
| 2778 | + "_" + std::to_string(mId) |
| 2779 | + "_R"); |
| 2780 | #endif |
Andy Hung | b68f5eb | 2019-12-03 16:49:17 -0800 | [diff] [blame] | 2781 | |
| 2782 | // Once this item is logged by the server, the client can add properties. |
Andy Hung | 9423528 | 2021-03-24 15:50:14 -0700 | [diff] [blame] | 2783 | mTrackMetrics.logConstructor(creatorPid, uid(), id()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2784 | } |
| 2785 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 2786 | RecordTrack::~RecordTrack() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2787 | { |
Andy Hung | 9d84af5 | 2018-09-12 18:03:44 -0700 | [diff] [blame] | 2788 | ALOGV("%s()", __func__); |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 2789 | delete mRecordBufferConverter; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 2790 | delete mResamplerBufferProvider; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2791 | } |
| 2792 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 2793 | status_t RecordTrack::initCheck() const |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 2794 | { |
| 2795 | status_t status = TrackBase::initCheck(); |
| 2796 | if (status == NO_ERROR && mServerProxy == 0) { |
| 2797 | status = BAD_VALUE; |
| 2798 | } |
| 2799 | return status; |
| 2800 | } |
| 2801 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2802 | // AudioBufferProvider interface |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 2803 | status_t RecordTrack::getNextBuffer(AudioBufferProvider::Buffer* buffer) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2804 | { |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 2805 | ServerProxy::Buffer buf; |
| 2806 | buf.mFrameCount = buffer->frameCount; |
| 2807 | status_t status = mServerProxy->obtainBuffer(&buf); |
| 2808 | buffer->frameCount = buf.mFrameCount; |
| 2809 | buffer->raw = buf.mRaw; |
| 2810 | if (buf.mFrameCount == 0) { |
| 2811 | // FIXME also wake futex so that overrun is noticed more quickly |
Glenn Kasten | 96f60d8 | 2013-07-12 10:21:18 -0700 | [diff] [blame] | 2812 | (void) android_atomic_or(CBLK_OVERRUN, &mCblk->mFlags); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2813 | } |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 2814 | return status; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2815 | } |
| 2816 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 2817 | status_t RecordTrack::start(AudioSystem::sync_event_t event, |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 2818 | audio_session_t triggerSession) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2819 | { |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 2820 | const sp<IAfThreadBase> thread = mThread.promote(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2821 | if (thread != 0) { |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 2822 | auto* const recordThread = thread->asIAfRecordThread().get(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2823 | return recordThread->start(this, event, triggerSession); |
| 2824 | } else { |
Eric Laurent | d52a28c | 2020-08-21 17:10:39 -0700 | [diff] [blame] | 2825 | ALOGW("%s track %d: thread was destroyed", __func__, portId()); |
| 2826 | return DEAD_OBJECT; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2827 | } |
| 2828 | } |
| 2829 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 2830 | void RecordTrack::stop() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2831 | { |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 2832 | const sp<IAfThreadBase> thread = mThread.promote(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2833 | if (thread != 0) { |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 2834 | auto* const recordThread = thread->asIAfRecordThread().get(); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2835 | if (recordThread->stop(this) && isExternalTrack()) { |
Eric Laurent | fee1976 | 2018-01-29 18:44:13 -0800 | [diff] [blame] | 2836 | AudioSystem::stopInput(mPortId); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2837 | } |
| 2838 | } |
| 2839 | } |
| 2840 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 2841 | void RecordTrack::destroy() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2842 | { |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 2843 | // see comments at Track::destroy() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2844 | sp<RecordTrack> keep(this); |
| 2845 | { |
Andy Hung | ce68540 | 2018-10-05 17:23:27 -0700 | [diff] [blame] | 2846 | track_state priorState = mState; |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 2847 | const sp<IAfThreadBase> thread = mThread.promote(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2848 | if (thread != 0) { |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 2849 | audio_utils::lock_guard _l(thread->mutex()); |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 2850 | auto* const recordThread = thread->asIAfRecordThread().get(); |
Andy Hung | ce68540 | 2018-10-05 17:23:27 -0700 | [diff] [blame] | 2851 | priorState = mState; |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 2852 | if (!mSharedAudioPackageName.empty()) { |
Eric Laurent | 92d0a32 | 2021-07-16 15:32:33 +0200 | [diff] [blame] | 2853 | recordThread->resetAudioHistory_l(); |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 2854 | } |
Andy Hung | ce68540 | 2018-10-05 17:23:27 -0700 | [diff] [blame] | 2855 | recordThread->destroyTrack_l(this); // move mState to STOPPED, terminate |
| 2856 | } |
| 2857 | // APM portid/client management done outside of lock. |
| 2858 | // NOTE: if thread doesn't exist, the input descriptor probably doesn't either. |
| 2859 | if (isExternalTrack()) { |
| 2860 | switch (priorState) { |
| 2861 | case ACTIVE: // invalidated while still active |
| 2862 | case STARTING_2: // invalidated/start-aborted after startInput successfully called |
| 2863 | case PAUSING: // invalidated while in the middle of stop() pausing (still active) |
| 2864 | AudioSystem::stopInput(mPortId); |
| 2865 | break; |
| 2866 | |
| 2867 | case STARTING_1: // invalidated/start-aborted and startInput not successful |
| 2868 | case PAUSED: // OK, not active |
| 2869 | case IDLE: // OK, not active |
| 2870 | break; |
| 2871 | |
| 2872 | case STOPPED: // unexpected (destroyed) |
| 2873 | default: |
| 2874 | LOG_ALWAYS_FATAL("%s(%d): invalid prior state: %d", __func__, mId, priorState); |
| 2875 | } |
| 2876 | AudioSystem::releaseInput(mPortId); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2877 | } |
| 2878 | } |
| 2879 | } |
| 2880 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 2881 | void RecordTrack::invalidate() |
Eric Laurent | 9a54bc2 | 2013-09-09 09:08:44 -0700 | [diff] [blame] | 2882 | { |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2883 | TrackBase::invalidate(); |
Eric Laurent | 9a54bc2 | 2013-09-09 09:08:44 -0700 | [diff] [blame] | 2884 | // FIXME should use proxy, and needs work |
| 2885 | audio_track_cblk_t* cblk = mCblk; |
| 2886 | android_atomic_or(CBLK_INVALID, &cblk->mFlags); |
| 2887 | android_atomic_release_store(0x40000000, &cblk->mFutex); |
| 2888 | // client is not in server, so FUTEX_WAKE is needed instead of FUTEX_WAKE_PRIVATE |
Elliott Hughes | ee49929 | 2014-05-21 17:55:51 -0700 | [diff] [blame] | 2889 | (void) syscall(__NR_futex, &cblk->mFutex, FUTEX_WAKE, INT_MAX); |
Eric Laurent | 9a54bc2 | 2013-09-09 09:08:44 -0700 | [diff] [blame] | 2890 | } |
| 2891 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2892 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 2893 | void RecordTrack::appendDumpHeader(String8& result) const |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2894 | { |
Eric Laurent | 973db02 | 2018-11-20 14:54:31 -0800 | [diff] [blame] | 2895 | result.appendFormat("Active Id Client Session Port Id S Flags " |
Andy Hung | 9d84af5 | 2018-09-12 18:03:44 -0700 | [diff] [blame] | 2896 | " Format Chn mask SRate Source " |
Kevin Rocard | 5f2136e | 2018-05-11 22:03:00 -0700 | [diff] [blame] | 2897 | " Server FrmCnt FrmRdy Sil%s\n", |
| 2898 | isServerLatencySupported() ? " Latency" : ""); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2899 | } |
| 2900 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 2901 | void RecordTrack::appendDump(String8& result, bool active) const |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2902 | { |
Eric Laurent | 973db02 | 2018-11-20 14:54:31 -0800 | [diff] [blame] | 2903 | result.appendFormat("%c%5s %6d %6u %7u %7u %2s 0x%03X " |
Kevin Rocard | 5f2136e | 2018-05-11 22:03:00 -0700 | [diff] [blame] | 2904 | "%08X %08X %6u %6X " |
Andy Hung | 000adb5 | 2018-06-01 15:43:26 -0700 | [diff] [blame] | 2905 | "%08X %6zu %6zu %3c", |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 2906 | isFastTrack() ? 'F' : ' ', |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 2907 | active ? "yes" : "no", |
Andy Hung | 9d84af5 | 2018-09-12 18:03:44 -0700 | [diff] [blame] | 2908 | mId, |
Andy Hung | 4ef19fa | 2018-05-15 19:35:29 -0700 | [diff] [blame] | 2909 | (mClient == 0) ? getpid() : mClient->pid(), |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 2910 | mSessionId, |
Eric Laurent | 973db02 | 2018-11-20 14:54:31 -0800 | [diff] [blame] | 2911 | mPortId, |
Andy Hung | e2e830f | 2019-12-03 12:54:46 -0800 | [diff] [blame] | 2912 | getTrackStateAsCodedString(), |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 2913 | mCblk->mFlags, |
| 2914 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2915 | mFormat, |
| 2916 | mChannelMask, |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 2917 | mSampleRate, |
Kevin Rocard | 5f2136e | 2018-05-11 22:03:00 -0700 | [diff] [blame] | 2918 | mAttr.source, |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 2919 | |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 2920 | mCblk->mServer, |
Jean-Michel Trivi | 7d665ab | 2018-04-11 17:26:51 -0700 | [diff] [blame] | 2921 | mFrameCount, |
Andy Hung | 000adb5 | 2018-06-01 15:43:26 -0700 | [diff] [blame] | 2922 | mServerProxy->framesReadySafe(), |
Jean-Michel Trivi | 7d665ab | 2018-04-11 17:26:51 -0700 | [diff] [blame] | 2923 | isSilenced() ? 's' : 'n' |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 2924 | ); |
Andy Hung | 000adb5 | 2018-06-01 15:43:26 -0700 | [diff] [blame] | 2925 | if (isServerLatencySupported()) { |
| 2926 | double latencyMs; |
| 2927 | bool fromTrack; |
| 2928 | if (getTrackLatencyMs(&latencyMs, &fromTrack) == OK) { |
| 2929 | // Show latency in msec, followed by 't' if from track timestamp (the most accurate) |
| 2930 | // or 'k' if estimated from kernel (usually for debugging). |
| 2931 | result.appendFormat(" %7.2lf %c", latencyMs, fromTrack ? 't' : 'k'); |
| 2932 | } else { |
| 2933 | result.appendFormat("%10s", mCblk->mServer != 0 ? "unavail" : "new"); |
| 2934 | } |
| 2935 | } |
| 2936 | result.append("\n"); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2937 | } |
| 2938 | |
Andy Hung | 93bb573 | 2023-05-04 21:16:34 -0700 | [diff] [blame] | 2939 | // This is invoked by SyncEvent callback. |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 2940 | void RecordTrack::handleSyncStartEvent( |
Andy Hung | 068e08e | 2023-05-15 19:02:55 -0700 | [diff] [blame] | 2941 | const sp<audioflinger::SyncEvent>& event) |
Glenn Kasten | 25f4aa8 | 2014-02-07 10:50:43 -0800 | [diff] [blame] | 2942 | { |
Andy Hung | 93bb573 | 2023-05-04 21:16:34 -0700 | [diff] [blame] | 2943 | size_t framesToDrop = 0; |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 2944 | const sp<IAfThreadBase> threadBase = mThread.promote(); |
Andy Hung | 93bb573 | 2023-05-04 21:16:34 -0700 | [diff] [blame] | 2945 | if (threadBase != 0) { |
| 2946 | // TODO: use actual buffer filling status instead of 2 buffers when info is available |
| 2947 | // from audio HAL |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 2948 | framesToDrop = threadBase->frameCount() * 2; |
Glenn Kasten | 25f4aa8 | 2014-02-07 10:50:43 -0800 | [diff] [blame] | 2949 | } |
Andy Hung | 93bb573 | 2023-05-04 21:16:34 -0700 | [diff] [blame] | 2950 | |
| 2951 | mSynchronizedRecordState.onPlaybackFinished(event, framesToDrop); |
Glenn Kasten | 25f4aa8 | 2014-02-07 10:50:43 -0800 | [diff] [blame] | 2952 | } |
| 2953 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 2954 | void RecordTrack::clearSyncStartEvent() |
Glenn Kasten | 25f4aa8 | 2014-02-07 10:50:43 -0800 | [diff] [blame] | 2955 | { |
Andy Hung | 93bb573 | 2023-05-04 21:16:34 -0700 | [diff] [blame] | 2956 | mSynchronizedRecordState.clear(); |
Glenn Kasten | 25f4aa8 | 2014-02-07 10:50:43 -0800 | [diff] [blame] | 2957 | } |
| 2958 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 2959 | void RecordTrack::updateTrackFrameInfo( |
Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 2960 | int64_t trackFramesReleased, int64_t sourceFramesRead, |
| 2961 | uint32_t halSampleRate, const ExtendedTimestamp ×tamp) |
| 2962 | { |
Andy Hung | 3028256 | 2018-08-08 18:27:03 -0700 | [diff] [blame] | 2963 | // Make the kernel frametime available. |
| 2964 | const FrameTime ft{ |
| 2965 | timestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL], |
| 2966 | timestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL]}; |
| 2967 | // ALOGD("FrameTime: %lld %lld", (long long)ft.frames, (long long)ft.timeNs); |
| 2968 | mKernelFrameTime.store(ft); |
| 2969 | if (!audio_is_linear_pcm(mFormat)) { |
Atneya Nair | 497fff1 | 2022-01-18 16:23:04 -0500 | [diff] [blame] | 2970 | // Stream is direct, return provided timestamp with no conversion |
| 2971 | mServerProxy->setTimestamp(timestamp); |
Andy Hung | 3028256 | 2018-08-08 18:27:03 -0700 | [diff] [blame] | 2972 | return; |
| 2973 | } |
| 2974 | |
Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 2975 | ExtendedTimestamp local = timestamp; |
| 2976 | |
| 2977 | // Convert HAL frames to server-side track frames at track sample rate. |
| 2978 | // We use trackFramesReleased and sourceFramesRead as an anchor point. |
| 2979 | for (int i = ExtendedTimestamp::LOCATION_SERVER; i < ExtendedTimestamp::LOCATION_MAX; ++i) { |
| 2980 | if (local.mTimeNs[i] != 0) { |
| 2981 | const int64_t relativeServerFrames = local.mPosition[i] - sourceFramesRead; |
| 2982 | const int64_t relativeTrackFrames = relativeServerFrames |
| 2983 | * mSampleRate / halSampleRate; // TODO: potential computation overflow |
| 2984 | local.mPosition[i] = relativeTrackFrames + trackFramesReleased; |
| 2985 | } |
| 2986 | } |
Andy Hung | 6ae5843 | 2016-02-16 18:32:24 -0800 | [diff] [blame] | 2987 | mServerProxy->setTimestamp(local); |
Andy Hung | 000adb5 | 2018-06-01 15:43:26 -0700 | [diff] [blame] | 2988 | |
| 2989 | // Compute latency info. |
| 2990 | const bool useTrackTimestamp = true; // use track unless debugging. |
| 2991 | const double latencyMs = - (useTrackTimestamp |
| 2992 | ? local.getOutputServerLatencyMs(sampleRate()) |
| 2993 | : timestamp.getOutputServerLatencyMs(halSampleRate)); |
| 2994 | |
| 2995 | mServerLatencyFromTrack.store(useTrackTimestamp); |
| 2996 | mServerLatencyMs.store(latencyMs); |
Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 2997 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2998 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 2999 | status_t RecordTrack::getActiveMicrophones( |
Andy Hung | d29af63 | 2023-06-23 19:27:19 -0700 | [diff] [blame] | 3000 | std::vector<media::MicrophoneInfoFw>* activeMicrophones) const |
jiabin | 653cc0a | 2018-01-17 17:54:10 -0800 | [diff] [blame] | 3001 | { |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 3002 | const sp<IAfThreadBase> thread = mThread.promote(); |
jiabin | 653cc0a | 2018-01-17 17:54:10 -0800 | [diff] [blame] | 3003 | if (thread != 0) { |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 3004 | auto* const recordThread = thread->asIAfRecordThread().get(); |
jiabin | 653cc0a | 2018-01-17 17:54:10 -0800 | [diff] [blame] | 3005 | return recordThread->getActiveMicrophones(activeMicrophones); |
| 3006 | } else { |
| 3007 | return BAD_VALUE; |
| 3008 | } |
| 3009 | } |
| 3010 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 3011 | status_t RecordTrack::setPreferredMicrophoneDirection( |
Paul McLean | 03a6e6a | 2018-12-04 10:54:13 -0700 | [diff] [blame] | 3012 | audio_microphone_direction_t direction) { |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 3013 | const sp<IAfThreadBase> thread = mThread.promote(); |
Paul McLean | 03a6e6a | 2018-12-04 10:54:13 -0700 | [diff] [blame] | 3014 | if (thread != 0) { |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 3015 | auto* const recordThread = thread->asIAfRecordThread().get(); |
Paul McLean | 1234008 | 2019-03-19 09:35:05 -0600 | [diff] [blame] | 3016 | return recordThread->setPreferredMicrophoneDirection(direction); |
Paul McLean | 03a6e6a | 2018-12-04 10:54:13 -0700 | [diff] [blame] | 3017 | } else { |
| 3018 | return BAD_VALUE; |
| 3019 | } |
| 3020 | } |
| 3021 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 3022 | status_t RecordTrack::setPreferredMicrophoneFieldDimension(float zoom) { |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 3023 | const sp<IAfThreadBase> thread = mThread.promote(); |
Paul McLean | 03a6e6a | 2018-12-04 10:54:13 -0700 | [diff] [blame] | 3024 | if (thread != 0) { |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 3025 | auto* const recordThread = thread->asIAfRecordThread().get(); |
Paul McLean | 1234008 | 2019-03-19 09:35:05 -0600 | [diff] [blame] | 3026 | return recordThread->setPreferredMicrophoneFieldDimension(zoom); |
Paul McLean | 03a6e6a | 2018-12-04 10:54:13 -0700 | [diff] [blame] | 3027 | } else { |
| 3028 | return BAD_VALUE; |
| 3029 | } |
| 3030 | } |
| 3031 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 3032 | status_t RecordTrack::shareAudioHistory( |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 3033 | const std::string& sharedAudioPackageName, int64_t sharedAudioStartMs) { |
| 3034 | |
| 3035 | const uid_t callingUid = IPCThreadState::self()->getCallingUid(); |
| 3036 | const pid_t callingPid = IPCThreadState::self()->getCallingPid(); |
| 3037 | if (callingUid != mUid || callingPid != mCreatorPid) { |
| 3038 | return PERMISSION_DENIED; |
| 3039 | } |
| 3040 | |
Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 3041 | AttributionSourceState attributionSource{}; |
| 3042 | attributionSource.uid = VALUE_OR_RETURN_STATUS(legacy2aidl_uid_t_int32_t(callingUid)); |
| 3043 | attributionSource.pid = VALUE_OR_RETURN_STATUS(legacy2aidl_uid_t_int32_t(callingPid)); |
| 3044 | attributionSource.token = sp<BBinder>::make(); |
| 3045 | if (!captureHotwordAllowed(attributionSource)) { |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 3046 | return PERMISSION_DENIED; |
| 3047 | } |
| 3048 | |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 3049 | const sp<IAfThreadBase> thread = mThread.promote(); |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 3050 | if (thread != 0) { |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 3051 | auto* const recordThread = thread->asIAfRecordThread().get(); |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 3052 | status_t status = recordThread->shareAudioHistory( |
| 3053 | sharedAudioPackageName, mSessionId, sharedAudioStartMs); |
| 3054 | if (status == NO_ERROR) { |
| 3055 | mSharedAudioPackageName = sharedAudioPackageName; |
| 3056 | } |
| 3057 | return status; |
| 3058 | } else { |
| 3059 | return BAD_VALUE; |
| 3060 | } |
| 3061 | } |
| 3062 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 3063 | void RecordTrack::copyMetadataTo(MetadataInserter& backInserter) const |
Eric Laurent | 78b0730 | 2022-10-07 16:20:34 +0200 | [diff] [blame] | 3064 | { |
| 3065 | |
| 3066 | // Do not forward PatchRecord metadata with unspecified audio source |
| 3067 | if (mAttr.source == AUDIO_SOURCE_DEFAULT) { |
| 3068 | return; |
| 3069 | } |
| 3070 | |
| 3071 | // No track is invalid as this is called after prepareTrack_l in the same critical section |
| 3072 | record_track_metadata_v7_t metadata; |
| 3073 | metadata.base = { |
| 3074 | .source = mAttr.source, |
| 3075 | .gain = 1, // capture tracks do not have volumes |
| 3076 | }; |
| 3077 | metadata.channel_mask = mChannelMask; |
| 3078 | strncpy(metadata.tags, mAttr.tags, AUDIO_ATTRIBUTES_TAGS_MAX_SIZE); |
| 3079 | |
| 3080 | *backInserter++ = metadata; |
| 3081 | } |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 3082 | |
Andy Hung | 9d84af5 | 2018-09-12 18:03:44 -0700 | [diff] [blame] | 3083 | // ---------------------------------------------------------------------------- |
| 3084 | #undef LOG_TAG |
| 3085 | #define LOG_TAG "AF::PatchRecord" |
| 3086 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 3087 | /* static */ |
| 3088 | sp<IAfPatchRecord> IAfPatchRecord::create( |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 3089 | IAfRecordThread* recordThread, |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 3090 | uint32_t sampleRate, |
| 3091 | audio_channel_mask_t channelMask, |
| 3092 | audio_format_t format, |
| 3093 | size_t frameCount, |
| 3094 | void *buffer, |
| 3095 | size_t bufferSize, |
| 3096 | audio_input_flags_t flags, |
| 3097 | const Timeout& timeout, |
| 3098 | audio_source_t source) |
| 3099 | { |
| 3100 | return sp<PatchRecord>::make( |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 3101 | recordThread, |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 3102 | sampleRate, |
| 3103 | channelMask, |
| 3104 | format, |
| 3105 | frameCount, |
| 3106 | buffer, |
| 3107 | bufferSize, |
| 3108 | flags, |
| 3109 | timeout, |
| 3110 | source); |
| 3111 | } |
| 3112 | |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 3113 | PatchRecord::PatchRecord(IAfRecordThread* recordThread, |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 3114 | uint32_t sampleRate, |
| 3115 | audio_channel_mask_t channelMask, |
| 3116 | audio_format_t format, |
| 3117 | size_t frameCount, |
| 3118 | void *buffer, |
Andy Hung | 8fe6803 | 2017-06-05 16:17:51 -0700 | [diff] [blame] | 3119 | size_t bufferSize, |
Kevin Rocard | 45986c7 | 2018-12-18 18:22:59 -0800 | [diff] [blame] | 3120 | audio_input_flags_t flags, |
Eric Laurent | 78b0730 | 2022-10-07 16:20:34 +0200 | [diff] [blame] | 3121 | const Timeout& timeout, |
| 3122 | audio_source_t source) |
Kevin Rocard | 1f564ac | 2018-03-29 13:53:10 -0700 | [diff] [blame] | 3123 | : RecordTrack(recordThread, NULL, |
Eric Laurent | 78b0730 | 2022-10-07 16:20:34 +0200 | [diff] [blame] | 3124 | audio_attributes_t{ .source = source } , |
Kevin Rocard | 1f564ac | 2018-03-29 13:53:10 -0700 | [diff] [blame] | 3125 | sampleRate, format, channelMask, frameCount, |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 3126 | buffer, bufferSize, AUDIO_SESSION_NONE, getpid(), |
Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 3127 | audioServerAttributionSource(getpid()), flags, TYPE_PATCH), |
gaoxiupei | 8e3a568 | 2023-07-07 20:30:23 +0800 | [diff] [blame] | 3128 | PatchTrackBase(mCblk ? new ClientProxy(mCblk, mBuffer, frameCount, mFrameSize, false, true) |
| 3129 | : nullptr, |
Andy Hung | 4fd6901 | 2023-07-14 16:57:01 -0700 | [diff] [blame] | 3130 | recordThread, timeout) |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 3131 | { |
Andy Hung | 9d84af5 | 2018-09-12 18:03:44 -0700 | [diff] [blame] | 3132 | ALOGV("%s(%d): sampleRate %d mPeerTimeout %d.%03d sec", |
| 3133 | __func__, mId, sampleRate, |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 3134 | (int)mPeerTimeout.tv_sec, |
| 3135 | (int)(mPeerTimeout.tv_nsec / 1000000)); |
| 3136 | } |
| 3137 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 3138 | PatchRecord::~PatchRecord() |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 3139 | { |
Andy Hung | abfab20 | 2019-03-07 19:45:54 -0800 | [diff] [blame] | 3140 | ALOGV("%s(%d)", __func__, mId); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 3141 | } |
| 3142 | |
Mikhail Naganov | 8296c25 | 2019-09-25 14:59:54 -0700 | [diff] [blame] | 3143 | static size_t writeFramesHelper( |
| 3144 | AudioBufferProvider* dest, const void* src, size_t frameCount, size_t frameSize) |
| 3145 | { |
| 3146 | AudioBufferProvider::Buffer patchBuffer; |
| 3147 | patchBuffer.frameCount = frameCount; |
| 3148 | auto status = dest->getNextBuffer(&patchBuffer); |
| 3149 | if (status != NO_ERROR) { |
| 3150 | ALOGW("%s PathRecord getNextBuffer failed with error %d: %s", |
| 3151 | __func__, status, strerror(-status)); |
| 3152 | return 0; |
| 3153 | } |
| 3154 | ALOG_ASSERT(patchBuffer.frameCount <= frameCount); |
| 3155 | memcpy(patchBuffer.raw, src, patchBuffer.frameCount * frameSize); |
| 3156 | size_t framesWritten = patchBuffer.frameCount; |
| 3157 | dest->releaseBuffer(&patchBuffer); |
| 3158 | return framesWritten; |
| 3159 | } |
| 3160 | |
| 3161 | // static |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 3162 | size_t PatchRecord::writeFrames( |
Mikhail Naganov | 8296c25 | 2019-09-25 14:59:54 -0700 | [diff] [blame] | 3163 | AudioBufferProvider* dest, const void* src, size_t frameCount, size_t frameSize) |
| 3164 | { |
| 3165 | size_t framesWritten = writeFramesHelper(dest, src, frameCount, frameSize); |
| 3166 | // On buffer wrap, the buffer frame count will be less than requested, |
| 3167 | // when this happens a second buffer needs to be used to write the leftover audio |
| 3168 | const size_t framesLeft = frameCount - framesWritten; |
| 3169 | if (framesWritten != 0 && framesLeft != 0) { |
| 3170 | framesWritten += writeFramesHelper(dest, (const char*)src + framesWritten * frameSize, |
| 3171 | framesLeft, frameSize); |
| 3172 | } |
| 3173 | return framesWritten; |
| 3174 | } |
| 3175 | |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 3176 | // AudioBufferProvider interface |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 3177 | status_t PatchRecord::getNextBuffer( |
Glenn Kasten | d79072e | 2016-01-06 08:41:20 -0800 | [diff] [blame] | 3178 | AudioBufferProvider::Buffer* buffer) |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 3179 | { |
Andy Hung | 9d84af5 | 2018-09-12 18:03:44 -0700 | [diff] [blame] | 3180 | ALOG_ASSERT(mPeerProxy != 0, "%s(%d): called without peer proxy", __func__, mId); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 3181 | Proxy::Buffer buf; |
| 3182 | buf.mFrameCount = buffer->frameCount; |
| 3183 | status_t status = mPeerProxy->obtainBuffer(&buf, &mPeerTimeout); |
| 3184 | ALOGV_IF(status != NO_ERROR, |
Andy Hung | 9d84af5 | 2018-09-12 18:03:44 -0700 | [diff] [blame] | 3185 | "%s(%d): mPeerProxy->obtainBuffer status %d", __func__, mId, status); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 3186 | buffer->frameCount = buf.mFrameCount; |
Mikhail Naganov | 938be41 | 2019-09-04 11:38:47 -0700 | [diff] [blame] | 3187 | if (ATRACE_ENABLED()) { |
| 3188 | std::string traceName("PRnObt"); |
| 3189 | traceName += std::to_string(id()); |
| 3190 | ATRACE_INT(traceName.c_str(), buf.mFrameCount); |
| 3191 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 3192 | if (buf.mFrameCount == 0) { |
| 3193 | return WOULD_BLOCK; |
| 3194 | } |
Glenn Kasten | d79072e | 2016-01-06 08:41:20 -0800 | [diff] [blame] | 3195 | status = RecordTrack::getNextBuffer(buffer); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 3196 | return status; |
| 3197 | } |
| 3198 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 3199 | void PatchRecord::releaseBuffer(AudioBufferProvider::Buffer* buffer) |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 3200 | { |
Andy Hung | 9d84af5 | 2018-09-12 18:03:44 -0700 | [diff] [blame] | 3201 | ALOG_ASSERT(mPeerProxy != 0, "%s(%d): called without peer proxy", __func__, mId); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 3202 | Proxy::Buffer buf; |
| 3203 | buf.mFrameCount = buffer->frameCount; |
| 3204 | buf.mRaw = buffer->raw; |
| 3205 | mPeerProxy->releaseBuffer(&buf); |
| 3206 | TrackBase::releaseBuffer(buffer); |
| 3207 | } |
| 3208 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 3209 | status_t PatchRecord::obtainBuffer(Proxy::Buffer* buffer, |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 3210 | const struct timespec *timeOut) |
| 3211 | { |
| 3212 | return mProxy->obtainBuffer(buffer, timeOut); |
| 3213 | } |
| 3214 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 3215 | void PatchRecord::releaseBuffer(Proxy::Buffer* buffer) |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 3216 | { |
| 3217 | mProxy->releaseBuffer(buffer); |
| 3218 | } |
| 3219 | |
Mikhail Naganov | caf5994 | 2019-09-25 14:05:29 -0700 | [diff] [blame] | 3220 | #undef LOG_TAG |
| 3221 | #define LOG_TAG "AF::PthrPatchRecord" |
| 3222 | |
| 3223 | static std::unique_ptr<void, decltype(free)*> allocAligned(size_t alignment, size_t size) |
| 3224 | { |
| 3225 | void *ptr = nullptr; |
| 3226 | (void)posix_memalign(&ptr, alignment, size); |
Andy Hung | 920f657 | 2022-10-06 12:09:49 -0700 | [diff] [blame] | 3227 | return {ptr, free}; |
Mikhail Naganov | caf5994 | 2019-09-25 14:05:29 -0700 | [diff] [blame] | 3228 | } |
| 3229 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 3230 | /* static */ |
| 3231 | sp<IAfPatchRecord> IAfPatchRecord::createPassThru( |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 3232 | IAfRecordThread* recordThread, |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 3233 | uint32_t sampleRate, |
| 3234 | audio_channel_mask_t channelMask, |
| 3235 | audio_format_t format, |
| 3236 | size_t frameCount, |
| 3237 | audio_input_flags_t flags, |
| 3238 | audio_source_t source) |
| 3239 | { |
| 3240 | return sp<PassthruPatchRecord>::make( |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 3241 | recordThread, |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 3242 | sampleRate, |
| 3243 | channelMask, |
| 3244 | format, |
| 3245 | frameCount, |
| 3246 | flags, |
| 3247 | source); |
| 3248 | } |
| 3249 | |
| 3250 | PassthruPatchRecord::PassthruPatchRecord( |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 3251 | IAfRecordThread* recordThread, |
Mikhail Naganov | caf5994 | 2019-09-25 14:05:29 -0700 | [diff] [blame] | 3252 | uint32_t sampleRate, |
| 3253 | audio_channel_mask_t channelMask, |
| 3254 | audio_format_t format, |
| 3255 | size_t frameCount, |
Eric Laurent | 78b0730 | 2022-10-07 16:20:34 +0200 | [diff] [blame] | 3256 | audio_input_flags_t flags, |
| 3257 | audio_source_t source) |
Mikhail Naganov | caf5994 | 2019-09-25 14:05:29 -0700 | [diff] [blame] | 3258 | : PatchRecord(recordThread, sampleRate, channelMask, format, frameCount, |
Eric Laurent | 78b0730 | 2022-10-07 16:20:34 +0200 | [diff] [blame] | 3259 | nullptr /*buffer*/, 0 /*bufferSize*/, flags, {} /* timeout */, source), |
Mikhail Naganov | caf5994 | 2019-09-25 14:05:29 -0700 | [diff] [blame] | 3260 | mPatchRecordAudioBufferProvider(*this), |
| 3261 | mSinkBuffer(allocAligned(32, mFrameCount * mFrameSize)), |
| 3262 | mStubBuffer(allocAligned(32, mFrameCount * mFrameSize)) |
| 3263 | { |
| 3264 | memset(mStubBuffer.get(), 0, mFrameCount * mFrameSize); |
| 3265 | } |
| 3266 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 3267 | sp<StreamInHalInterface> PassthruPatchRecord::obtainStream( |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 3268 | sp<IAfThreadBase>* thread) |
Mikhail Naganov | caf5994 | 2019-09-25 14:05:29 -0700 | [diff] [blame] | 3269 | { |
| 3270 | *thread = mThread.promote(); |
| 3271 | if (!*thread) return nullptr; |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 3272 | auto* const recordThread = (*thread)->asIAfRecordThread().get(); |
Andy Hung | c5007f8 | 2023-08-29 14:26:09 -0700 | [diff] [blame] | 3273 | audio_utils::lock_guard _l(recordThread->mutex()); |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 3274 | return recordThread->getInput() ? recordThread->getInput()->stream : nullptr; |
Mikhail Naganov | caf5994 | 2019-09-25 14:05:29 -0700 | [diff] [blame] | 3275 | } |
| 3276 | |
| 3277 | // PatchProxyBufferProvider methods are called on DirectOutputThread |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 3278 | status_t PassthruPatchRecord::obtainBuffer( |
Mikhail Naganov | caf5994 | 2019-09-25 14:05:29 -0700 | [diff] [blame] | 3279 | Proxy::Buffer* buffer, const struct timespec* timeOut) |
| 3280 | { |
| 3281 | if (mUnconsumedFrames) { |
| 3282 | buffer->mFrameCount = std::min(buffer->mFrameCount, mUnconsumedFrames); |
| 3283 | // mUnconsumedFrames is decreased in releaseBuffer to use actual frame consumption figure. |
| 3284 | return PatchRecord::obtainBuffer(buffer, timeOut); |
| 3285 | } |
| 3286 | |
| 3287 | // Otherwise, execute a read from HAL and write into the buffer. |
| 3288 | nsecs_t startTimeNs = 0; |
| 3289 | if (timeOut && (timeOut->tv_sec != 0 || timeOut->tv_nsec != 0) && timeOut->tv_sec != INT_MAX) { |
| 3290 | // Will need to correct timeOut by elapsed time. |
| 3291 | startTimeNs = systemTime(); |
| 3292 | } |
| 3293 | const size_t framesToRead = std::min(buffer->mFrameCount, mFrameCount); |
| 3294 | buffer->mFrameCount = 0; |
| 3295 | buffer->mRaw = nullptr; |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 3296 | sp<IAfThreadBase> thread; |
Mikhail Naganov | caf5994 | 2019-09-25 14:05:29 -0700 | [diff] [blame] | 3297 | sp<StreamInHalInterface> stream = obtainStream(&thread); |
| 3298 | if (!stream) return NO_INIT; // If there is no stream, RecordThread is not reading. |
| 3299 | |
| 3300 | status_t result = NO_ERROR; |
Mikhail Naganov | caf5994 | 2019-09-25 14:05:29 -0700 | [diff] [blame] | 3301 | size_t bytesRead = 0; |
| 3302 | { |
| 3303 | ATRACE_NAME("read"); |
| 3304 | result = stream->read(mSinkBuffer.get(), framesToRead * mFrameSize, &bytesRead); |
| 3305 | if (result != NO_ERROR) goto stream_error; |
| 3306 | if (bytesRead == 0) return NO_ERROR; |
| 3307 | } |
| 3308 | |
| 3309 | { |
Andy Hung | 0169fbc | 2023-08-28 19:12:14 -0700 | [diff] [blame] | 3310 | audio_utils::lock_guard lock(readMutex()); |
Mikhail Naganov | caf5994 | 2019-09-25 14:05:29 -0700 | [diff] [blame] | 3311 | mReadBytes += bytesRead; |
| 3312 | mReadError = NO_ERROR; |
| 3313 | } |
| 3314 | mReadCV.notify_one(); |
| 3315 | // writeFrames handles wraparound and should write all the provided frames. |
| 3316 | // If it couldn't, there is something wrong with the client/server buffer of the software patch. |
| 3317 | buffer->mFrameCount = writeFrames( |
| 3318 | &mPatchRecordAudioBufferProvider, |
| 3319 | mSinkBuffer.get(), bytesRead / mFrameSize, mFrameSize); |
| 3320 | ALOGW_IF(buffer->mFrameCount < bytesRead / mFrameSize, |
| 3321 | "Lost %zu frames obtained from HAL", bytesRead / mFrameSize - buffer->mFrameCount); |
| 3322 | mUnconsumedFrames = buffer->mFrameCount; |
Mikhail Naganov | 4de4997 | 2019-10-07 09:53:58 -0700 | [diff] [blame] | 3323 | struct timespec newTimeOut; |
Mikhail Naganov | caf5994 | 2019-09-25 14:05:29 -0700 | [diff] [blame] | 3324 | if (startTimeNs) { |
Mikhail Naganov | 4de4997 | 2019-10-07 09:53:58 -0700 | [diff] [blame] | 3325 | // Correct the timeout by elapsed time. |
| 3326 | nsecs_t newTimeOutNs = audio_utils_ns_from_timespec(timeOut) - (systemTime() - startTimeNs); |
Mikhail Naganov | caf5994 | 2019-09-25 14:05:29 -0700 | [diff] [blame] | 3327 | if (newTimeOutNs < 0) newTimeOutNs = 0; |
| 3328 | newTimeOut.tv_sec = newTimeOutNs / NANOS_PER_SECOND; |
| 3329 | newTimeOut.tv_nsec = newTimeOutNs - newTimeOut.tv_sec * NANOS_PER_SECOND; |
Mikhail Naganov | 4de4997 | 2019-10-07 09:53:58 -0700 | [diff] [blame] | 3330 | timeOut = &newTimeOut; |
Mikhail Naganov | caf5994 | 2019-09-25 14:05:29 -0700 | [diff] [blame] | 3331 | } |
Mikhail Naganov | 4de4997 | 2019-10-07 09:53:58 -0700 | [diff] [blame] | 3332 | return PatchRecord::obtainBuffer(buffer, timeOut); |
Mikhail Naganov | caf5994 | 2019-09-25 14:05:29 -0700 | [diff] [blame] | 3333 | |
| 3334 | stream_error: |
| 3335 | stream->standby(); |
| 3336 | { |
Andy Hung | 0169fbc | 2023-08-28 19:12:14 -0700 | [diff] [blame] | 3337 | audio_utils::lock_guard lock(readMutex()); |
Mikhail Naganov | caf5994 | 2019-09-25 14:05:29 -0700 | [diff] [blame] | 3338 | mReadError = result; |
| 3339 | } |
| 3340 | mReadCV.notify_one(); |
| 3341 | return result; |
| 3342 | } |
| 3343 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 3344 | void PassthruPatchRecord::releaseBuffer(Proxy::Buffer* buffer) |
Mikhail Naganov | caf5994 | 2019-09-25 14:05:29 -0700 | [diff] [blame] | 3345 | { |
| 3346 | if (buffer->mFrameCount <= mUnconsumedFrames) { |
| 3347 | mUnconsumedFrames -= buffer->mFrameCount; |
| 3348 | } else { |
| 3349 | ALOGW("Write side has consumed more frames than we had: %zu > %zu", |
| 3350 | buffer->mFrameCount, mUnconsumedFrames); |
| 3351 | mUnconsumedFrames = 0; |
| 3352 | } |
| 3353 | PatchRecord::releaseBuffer(buffer); |
| 3354 | } |
| 3355 | |
| 3356 | // AudioBufferProvider and Source methods are called on RecordThread |
| 3357 | // 'read' emulates actual audio data with 0's. This is OK as 'getNextBuffer' |
| 3358 | // and 'releaseBuffer' are stubbed out and ignore their input. |
| 3359 | // It's not possible to retrieve actual data here w/o blocking 'obtainBuffer' |
| 3360 | // until we copy it. |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 3361 | status_t PassthruPatchRecord::read( |
Mikhail Naganov | caf5994 | 2019-09-25 14:05:29 -0700 | [diff] [blame] | 3362 | void* buffer, size_t bytes, size_t* read) |
| 3363 | { |
| 3364 | bytes = std::min(bytes, mFrameCount * mFrameSize); |
| 3365 | { |
Andy Hung | 0169fbc | 2023-08-28 19:12:14 -0700 | [diff] [blame] | 3366 | audio_utils::unique_lock lock(readMutex()); |
Mikhail Naganov | caf5994 | 2019-09-25 14:05:29 -0700 | [diff] [blame] | 3367 | mReadCV.wait(lock, [&]{ return mReadError != NO_ERROR || mReadBytes != 0; }); |
| 3368 | if (mReadError != NO_ERROR) { |
| 3369 | mLastReadFrames = 0; |
| 3370 | return mReadError; |
| 3371 | } |
| 3372 | *read = std::min(bytes, mReadBytes); |
| 3373 | mReadBytes -= *read; |
| 3374 | } |
| 3375 | mLastReadFrames = *read / mFrameSize; |
| 3376 | memset(buffer, 0, *read); |
| 3377 | return 0; |
| 3378 | } |
| 3379 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 3380 | status_t PassthruPatchRecord::getCapturePosition( |
Mikhail Naganov | caf5994 | 2019-09-25 14:05:29 -0700 | [diff] [blame] | 3381 | int64_t* frames, int64_t* time) |
| 3382 | { |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 3383 | sp<IAfThreadBase> thread; |
Mikhail Naganov | caf5994 | 2019-09-25 14:05:29 -0700 | [diff] [blame] | 3384 | sp<StreamInHalInterface> stream = obtainStream(&thread); |
| 3385 | return stream ? stream->getCapturePosition(frames, time) : NO_INIT; |
| 3386 | } |
| 3387 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 3388 | status_t PassthruPatchRecord::standby() |
Mikhail Naganov | caf5994 | 2019-09-25 14:05:29 -0700 | [diff] [blame] | 3389 | { |
| 3390 | // RecordThread issues 'standby' command in two major cases: |
| 3391 | // 1. Error on read--this case is handled in 'obtainBuffer'. |
| 3392 | // 2. Track is stopping--as PassthruPatchRecord assumes continuous |
| 3393 | // output, this can only happen when the software patch |
| 3394 | // is being torn down. In this case, the RecordThread |
| 3395 | // will terminate and close the HAL stream. |
| 3396 | return 0; |
| 3397 | } |
| 3398 | |
| 3399 | // As the buffer gets filled in obtainBuffer, here we only simulate data consumption. |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 3400 | status_t PassthruPatchRecord::getNextBuffer( |
Mikhail Naganov | caf5994 | 2019-09-25 14:05:29 -0700 | [diff] [blame] | 3401 | AudioBufferProvider::Buffer* buffer) |
| 3402 | { |
| 3403 | buffer->frameCount = mLastReadFrames; |
| 3404 | buffer->raw = buffer->frameCount != 0 ? mStubBuffer.get() : nullptr; |
| 3405 | return NO_ERROR; |
| 3406 | } |
| 3407 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 3408 | void PassthruPatchRecord::releaseBuffer( |
Mikhail Naganov | caf5994 | 2019-09-25 14:05:29 -0700 | [diff] [blame] | 3409 | AudioBufferProvider::Buffer* buffer) |
| 3410 | { |
| 3411 | buffer->frameCount = 0; |
| 3412 | buffer->raw = nullptr; |
| 3413 | } |
| 3414 | |
Andy Hung | 9d84af5 | 2018-09-12 18:03:44 -0700 | [diff] [blame] | 3415 | // ---------------------------------------------------------------------------- |
| 3416 | #undef LOG_TAG |
| 3417 | #define LOG_TAG "AF::MmapTrack" |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 3418 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 3419 | /* static */ |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 3420 | sp<IAfMmapTrack> IAfMmapTrack::create(IAfThreadBase* thread, |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 3421 | const audio_attributes_t& attr, |
| 3422 | uint32_t sampleRate, |
| 3423 | audio_format_t format, |
| 3424 | audio_channel_mask_t channelMask, |
| 3425 | audio_session_t sessionId, |
| 3426 | bool isOut, |
| 3427 | const android::content::AttributionSourceState& attributionSource, |
| 3428 | pid_t creatorPid, |
| 3429 | audio_port_handle_t portId) |
| 3430 | { |
| 3431 | return sp<MmapTrack>::make( |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 3432 | thread, |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 3433 | attr, |
| 3434 | sampleRate, |
| 3435 | format, |
| 3436 | channelMask, |
| 3437 | sessionId, |
| 3438 | isOut, |
| 3439 | attributionSource, |
| 3440 | creatorPid, |
| 3441 | portId); |
| 3442 | } |
| 3443 | |
Andy Hung | 87c693c | 2023-07-06 20:56:16 -0700 | [diff] [blame] | 3444 | MmapTrack::MmapTrack(IAfThreadBase* thread, |
Kevin Rocard | 1f564ac | 2018-03-29 13:53:10 -0700 | [diff] [blame] | 3445 | const audio_attributes_t& attr, |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 3446 | uint32_t sampleRate, |
| 3447 | audio_format_t format, |
| 3448 | audio_channel_mask_t channelMask, |
| 3449 | audio_session_t sessionId, |
Kevin Rocard | 5f2136e | 2018-05-11 22:03:00 -0700 | [diff] [blame] | 3450 | bool isOut, |
Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 3451 | const AttributionSourceState& attributionSource, |
Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 3452 | pid_t creatorPid, |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 3453 | audio_port_handle_t portId) |
Kevin Rocard | 1f564ac | 2018-03-29 13:53:10 -0700 | [diff] [blame] | 3454 | : TrackBase(thread, NULL, attr, sampleRate, format, |
Andy Hung | 8fe6803 | 2017-06-05 16:17:51 -0700 | [diff] [blame] | 3455 | channelMask, (size_t)0 /* frameCount */, |
| 3456 | nullptr /* buffer */, (size_t)0 /* bufferSize */, |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 3457 | sessionId, creatorPid, |
Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 3458 | VALUE_OR_FATAL(aidl2legacy_int32_t_uid_t(attributionSource.uid)), |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 3459 | isOut, |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 3460 | ALLOC_NONE, |
Andy Hung | c2b11cb | 2020-04-22 09:04:01 -0700 | [diff] [blame] | 3461 | TYPE_DEFAULT, portId, |
| 3462 | std::string(AMEDIAMETRICS_KEY_PREFIX_AUDIO_MMAP) + std::to_string(portId)), |
Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 3463 | mPid(VALUE_OR_FATAL(aidl2legacy_int32_t_uid_t(attributionSource.pid))), |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 3464 | mSilenced(false), mSilencedNotified(false) |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 3465 | { |
Andy Hung | c2b11cb | 2020-04-22 09:04:01 -0700 | [diff] [blame] | 3466 | // Once this item is logged by the server, the client can add properties. |
Andy Hung | 9423528 | 2021-03-24 15:50:14 -0700 | [diff] [blame] | 3467 | mTrackMetrics.logConstructor(creatorPid, uid(), id()); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 3468 | } |
| 3469 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 3470 | MmapTrack::~MmapTrack() |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 3471 | { |
| 3472 | } |
| 3473 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 3474 | status_t MmapTrack::initCheck() const |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 3475 | { |
| 3476 | return NO_ERROR; |
| 3477 | } |
| 3478 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 3479 | status_t MmapTrack::start(AudioSystem::sync_event_t event __unused, |
Kevin Rocard | 5f2136e | 2018-05-11 22:03:00 -0700 | [diff] [blame] | 3480 | audio_session_t triggerSession __unused) |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 3481 | { |
| 3482 | return NO_ERROR; |
| 3483 | } |
| 3484 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 3485 | void MmapTrack::stop() |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 3486 | { |
| 3487 | } |
| 3488 | |
| 3489 | // AudioBufferProvider interface |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 3490 | status_t MmapTrack::getNextBuffer(AudioBufferProvider::Buffer* buffer) |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 3491 | { |
| 3492 | buffer->frameCount = 0; |
| 3493 | buffer->raw = nullptr; |
| 3494 | return INVALID_OPERATION; |
| 3495 | } |
| 3496 | |
| 3497 | // ExtendedAudioBufferProvider interface |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 3498 | size_t MmapTrack::framesReady() const { |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 3499 | return 0; |
| 3500 | } |
| 3501 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 3502 | int64_t MmapTrack::framesReleased() const |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 3503 | { |
| 3504 | return 0; |
| 3505 | } |
| 3506 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 3507 | void MmapTrack::onTimestamp(const ExtendedTimestamp& timestamp __unused) |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 3508 | { |
| 3509 | } |
| 3510 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 3511 | void MmapTrack::processMuteEvent_l(const sp<IAudioManager>& audioManager, mute_state_t muteState) |
Vlad Popa | ec1788e | 2022-08-04 11:23:30 +0200 | [diff] [blame] | 3512 | { |
| 3513 | if (mMuteState == muteState) { |
| 3514 | // mute state did not change, do nothing |
| 3515 | return; |
| 3516 | } |
| 3517 | |
| 3518 | status_t result = UNKNOWN_ERROR; |
| 3519 | if (audioManager && mPortId != AUDIO_PORT_HANDLE_NONE) { |
| 3520 | if (mMuteEventExtras == nullptr) { |
| 3521 | mMuteEventExtras = std::make_unique<os::PersistableBundle>(); |
| 3522 | } |
| 3523 | mMuteEventExtras->putInt(String16(kExtraPlayerEventMuteKey), |
| 3524 | static_cast<int>(muteState)); |
| 3525 | |
| 3526 | result = audioManager->portEvent(mPortId, |
| 3527 | PLAYER_UPDATE_MUTED, |
| 3528 | mMuteEventExtras); |
| 3529 | } |
| 3530 | |
| 3531 | if (result == OK) { |
| 3532 | mMuteState = muteState; |
| 3533 | } else { |
| 3534 | ALOGW("%s(%d): cannot process mute state for port ID %d, status error %d", |
| 3535 | __func__, |
| 3536 | id(), |
| 3537 | mPortId, |
| 3538 | result); |
| 3539 | } |
| 3540 | } |
| 3541 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 3542 | void MmapTrack::appendDumpHeader(String8& result) const |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 3543 | { |
Eric Laurent | 973db02 | 2018-11-20 14:54:31 -0800 | [diff] [blame] | 3544 | result.appendFormat("Client Session Port Id Format Chn mask SRate Flags %s\n", |
Kevin Rocard | 5f2136e | 2018-05-11 22:03:00 -0700 | [diff] [blame] | 3545 | isOut() ? "Usg CT": "Source"); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 3546 | } |
| 3547 | |
Andy Hung | 8d31fd2 | 2023-06-26 19:20:57 -0700 | [diff] [blame] | 3548 | void MmapTrack::appendDump(String8& result, bool active __unused) const |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 3549 | { |
Eric Laurent | 973db02 | 2018-11-20 14:54:31 -0800 | [diff] [blame] | 3550 | result.appendFormat("%6u %7u %7u %08X %08X %6u 0x%03X ", |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 3551 | mPid, |
| 3552 | mSessionId, |
Eric Laurent | 973db02 | 2018-11-20 14:54:31 -0800 | [diff] [blame] | 3553 | mPortId, |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 3554 | mFormat, |
| 3555 | mChannelMask, |
Kevin Rocard | 5f2136e | 2018-05-11 22:03:00 -0700 | [diff] [blame] | 3556 | mSampleRate, |
| 3557 | mAttr.flags); |
| 3558 | if (isOut()) { |
| 3559 | result.appendFormat("%3x %2x", mAttr.usage, mAttr.content_type); |
| 3560 | } else { |
| 3561 | result.appendFormat("%6x", mAttr.source); |
| 3562 | } |
| 3563 | result.append("\n"); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 3564 | } |
| 3565 | |
Glenn Kasten | 63238ef | 2015-03-02 15:50:29 -0800 | [diff] [blame] | 3566 | } // namespace android |