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