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" |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 33 | |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 34 | #include <media/nbaio/Pipe.h> |
| 35 | #include <media/nbaio/PipeReader.h> |
Andy Hung | 8981605 | 2017-01-11 17:08:23 -0800 | [diff] [blame] | 36 | #include <media/RecordBufferConverter.h> |
Andy Hung | ab7ef30 | 2018-05-15 19:35:29 -0700 | [diff] [blame] | 37 | #include <mediautils/ServiceUtilities.h> |
Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 38 | #include <audio_utils/minifloat.h> |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 39 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 40 | // ---------------------------------------------------------------------------- |
| 41 | |
| 42 | // Note: the following macro is used for extremely verbose logging message. In |
| 43 | // order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to |
| 44 | // 0; but one side effect of this is to turn all LOGV's as well. Some messages |
| 45 | // are so verbose that we want to suppress them even when we have ALOG_ASSERT |
| 46 | // turned on. Do not uncomment the #def below unless you really know what you |
| 47 | // are doing and want to see all of the extremely verbose messages. |
| 48 | //#define VERY_VERY_VERBOSE_LOGGING |
| 49 | #ifdef VERY_VERY_VERBOSE_LOGGING |
| 50 | #define ALOGVV ALOGV |
| 51 | #else |
| 52 | #define ALOGVV(a...) do { } while(0) |
| 53 | #endif |
| 54 | |
| 55 | namespace android { |
| 56 | |
Ivan Lozano | 8cf3a07 | 2017-08-09 09:01:33 -0700 | [diff] [blame] | 57 | using media::VolumeShaper; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 58 | // ---------------------------------------------------------------------------- |
| 59 | // TrackBase |
| 60 | // ---------------------------------------------------------------------------- |
Andy Hung | 9d84af5 | 2018-09-12 18:03:44 -0700 | [diff] [blame] | 61 | #undef LOG_TAG |
| 62 | #define LOG_TAG "AF::TrackBase" |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 63 | |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 64 | static volatile int32_t nextTrackId = 55; |
| 65 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 66 | // TrackBase constructor must be called with AudioFlinger::mLock held |
| 67 | AudioFlinger::ThreadBase::TrackBase::TrackBase( |
| 68 | ThreadBase *thread, |
| 69 | const sp<Client>& client, |
Kevin Rocard | 1f564ac | 2018-03-29 13:53:10 -0700 | [diff] [blame] | 70 | const audio_attributes_t& attr, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 71 | uint32_t sampleRate, |
| 72 | audio_format_t format, |
| 73 | audio_channel_mask_t channelMask, |
| 74 | size_t frameCount, |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 75 | void *buffer, |
Andy Hung | 8fe6803 | 2017-06-05 16:17:51 -0700 | [diff] [blame] | 76 | size_t bufferSize, |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 77 | audio_session_t sessionId, |
Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 78 | pid_t creatorPid, |
Andy Hung | 1f12a8a | 2016-11-07 16:10:30 -0800 | [diff] [blame] | 79 | uid_t clientUid, |
Glenn Kasten | d776ac6 | 2014-05-07 09:16:09 -0700 | [diff] [blame] | 80 | bool isOut, |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 81 | alloc_type alloc, |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 82 | track_type type, |
| 83 | audio_port_handle_t portId) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 84 | : RefBase(), |
| 85 | mThread(thread), |
| 86 | mClient(client), |
| 87 | mCblk(NULL), |
Andy Hung | 8fe6803 | 2017-06-05 16:17:51 -0700 | [diff] [blame] | 88 | // mBuffer, mBufferSize |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 89 | mState(IDLE), |
Kevin Rocard | 1f564ac | 2018-03-29 13:53:10 -0700 | [diff] [blame] | 90 | mAttr(attr), |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 91 | mSampleRate(sampleRate), |
| 92 | mFormat(format), |
| 93 | mChannelMask(channelMask), |
Andy Hung | e541269 | 2014-05-16 11:25:07 -0700 | [diff] [blame] | 94 | mChannelCount(isOut ? |
| 95 | audio_channel_count_from_out_mask(channelMask) : |
| 96 | audio_channel_count_from_in_mask(channelMask)), |
Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 97 | mFrameSize(audio_has_proportional_frames(format) ? |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 98 | mChannelCount * audio_bytes_per_sample(format) : sizeof(int8_t)), |
| 99 | mFrameCount(frameCount), |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 100 | mSessionId(sessionId), |
| 101 | mIsOut(isOut), |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 102 | mId(android_atomic_inc(&nextTrackId)), |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 103 | mTerminated(false), |
Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 104 | mType(type), |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 105 | mThreadIoHandle(thread ? thread->id() : AUDIO_IO_HANDLE_NONE), |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 106 | mPortId(portId), |
Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 107 | mIsInvalid(false), |
| 108 | mCreatorPid(creatorPid) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 109 | { |
Marco Nelissen | dcb346b | 2015-09-09 10:47:29 -0700 | [diff] [blame] | 110 | const uid_t callingUid = IPCThreadState::self()->getCallingUid(); |
Andy Hung | 4ef19fa | 2018-05-15 19:35:29 -0700 | [diff] [blame] | 111 | if (!isAudioServerOrMediaServerUid(callingUid) || clientUid == AUDIO_UID_INVALID) { |
Andy Hung | 1f12a8a | 2016-11-07 16:10:30 -0800 | [diff] [blame] | 112 | ALOGW_IF(clientUid != AUDIO_UID_INVALID && clientUid != callingUid, |
Andy Hung | 9d84af5 | 2018-09-12 18:03:44 -0700 | [diff] [blame] | 113 | "%s(%d): uid %d tried to pass itself off as %d", |
| 114 | __func__, mId, callingUid, clientUid); |
Andy Hung | 1f12a8a | 2016-11-07 16:10:30 -0800 | [diff] [blame] | 115 | clientUid = callingUid; |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 116 | } |
| 117 | // clientUid contains the uid of the app that is responsible for this track, so we can blame |
| 118 | // battery usage on it. |
| 119 | mUid = clientUid; |
| 120 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 121 | // ALOGD("Creating track with %d buffers @ %d bytes", bufferCount, bufferSize); |
Andy Hung | 1883f69 | 2017-02-13 18:48:39 -0800 | [diff] [blame] | 122 | |
Andy Hung | 8fe6803 | 2017-06-05 16:17:51 -0700 | [diff] [blame] | 123 | size_t minBufferSize = buffer == NULL ? roundup(frameCount) : frameCount; |
Andy Hung | 1883f69 | 2017-02-13 18:48:39 -0800 | [diff] [blame] | 124 | // check overflow when computing bufferSize due to multiplication by mFrameSize. |
Andy Hung | 8fe6803 | 2017-06-05 16:17:51 -0700 | [diff] [blame] | 125 | if (minBufferSize < frameCount // roundup rounds down for values above UINT_MAX / 2 |
Andy Hung | 1883f69 | 2017-02-13 18:48:39 -0800 | [diff] [blame] | 126 | || mFrameSize == 0 // format needs to be correct |
Andy Hung | 8fe6803 | 2017-06-05 16:17:51 -0700 | [diff] [blame] | 127 | || minBufferSize > SIZE_MAX / mFrameSize) { |
Andy Hung | 1883f69 | 2017-02-13 18:48:39 -0800 | [diff] [blame] | 128 | android_errorWriteLog(0x534e4554, "34749571"); |
| 129 | return; |
| 130 | } |
Andy Hung | 8fe6803 | 2017-06-05 16:17:51 -0700 | [diff] [blame] | 131 | minBufferSize *= mFrameSize; |
| 132 | |
| 133 | if (buffer == nullptr) { |
| 134 | bufferSize = minBufferSize; // allocated here. |
| 135 | } else if (minBufferSize > bufferSize) { |
| 136 | android_errorWriteLog(0x534e4554, "38340117"); |
| 137 | return; |
| 138 | } |
Andy Hung | 1883f69 | 2017-02-13 18:48:39 -0800 | [diff] [blame] | 139 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 140 | size_t size = sizeof(audio_track_cblk_t); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 141 | if (buffer == NULL && alloc == ALLOC_CBLK) { |
Andy Hung | 1883f69 | 2017-02-13 18:48:39 -0800 | [diff] [blame] | 142 | // check overflow when computing allocation size for streaming tracks. |
| 143 | if (size > SIZE_MAX - bufferSize) { |
| 144 | android_errorWriteLog(0x534e4554, "34749571"); |
| 145 | return; |
| 146 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 147 | size += bufferSize; |
| 148 | } |
| 149 | |
| 150 | if (client != 0) { |
| 151 | mCblkMemory = client->heap()->allocate(size); |
Glenn Kasten | 663c224 | 2013-09-24 11:52:37 -0700 | [diff] [blame] | 152 | if (mCblkMemory == 0 || |
| 153 | (mCblk = static_cast<audio_track_cblk_t *>(mCblkMemory->pointer())) == NULL) { |
Andy Hung | 9d84af5 | 2018-09-12 18:03:44 -0700 | [diff] [blame] | 154 | ALOGE("%s(%d): not enough memory for AudioTrack size=%zu", __func__, mId, size); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 155 | client->heap()->dump("AudioTrack"); |
Glenn Kasten | 663c224 | 2013-09-24 11:52:37 -0700 | [diff] [blame] | 156 | mCblkMemory.clear(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 157 | return; |
| 158 | } |
| 159 | } else { |
Andy Hung | afb3148 | 2017-02-13 18:50:48 -0800 | [diff] [blame] | 160 | mCblk = (audio_track_cblk_t *) malloc(size); |
| 161 | if (mCblk == NULL) { |
Andy Hung | 9d84af5 | 2018-09-12 18:03:44 -0700 | [diff] [blame] | 162 | 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] | 163 | return; |
| 164 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | // construct the shared structure in-place. |
| 168 | if (mCblk != NULL) { |
| 169 | new(mCblk) audio_track_cblk_t(); |
Glenn Kasten | c263ca0 | 2014-06-04 20:31:46 -0700 | [diff] [blame] | 170 | switch (alloc) { |
| 171 | case ALLOC_READONLY: { |
Glenn Kasten | d776ac6 | 2014-05-07 09:16:09 -0700 | [diff] [blame] | 172 | const sp<MemoryDealer> roHeap(thread->readOnlyHeap()); |
| 173 | if (roHeap == 0 || |
| 174 | (mBufferMemory = roHeap->allocate(bufferSize)) == 0 || |
| 175 | (mBuffer = mBufferMemory->pointer()) == NULL) { |
Andy Hung | 9d84af5 | 2018-09-12 18:03:44 -0700 | [diff] [blame] | 176 | ALOGE("%s(%d): not enough memory for read-only buffer size=%zu", |
| 177 | __func__, mId, bufferSize); |
Glenn Kasten | d776ac6 | 2014-05-07 09:16:09 -0700 | [diff] [blame] | 178 | if (roHeap != 0) { |
| 179 | roHeap->dump("buffer"); |
| 180 | } |
| 181 | mCblkMemory.clear(); |
| 182 | mBufferMemory.clear(); |
| 183 | return; |
| 184 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 185 | memset(mBuffer, 0, bufferSize); |
Glenn Kasten | c263ca0 | 2014-06-04 20:31:46 -0700 | [diff] [blame] | 186 | } break; |
| 187 | case ALLOC_PIPE: |
| 188 | mBufferMemory = thread->pipeMemory(); |
| 189 | // mBuffer is the virtual address as seen from current process (mediaserver), |
| 190 | // and should normally be coming from mBufferMemory->pointer(). |
| 191 | // However in this case the TrackBase does not reference the buffer directly. |
| 192 | // It should references the buffer via the pipe. |
| 193 | // Therefore, to detect incorrect usage of the buffer, we set mBuffer to NULL. |
| 194 | mBuffer = NULL; |
Andy Hung | 8fe6803 | 2017-06-05 16:17:51 -0700 | [diff] [blame] | 195 | bufferSize = 0; |
Glenn Kasten | c263ca0 | 2014-06-04 20:31:46 -0700 | [diff] [blame] | 196 | break; |
| 197 | case ALLOC_CBLK: |
Glenn Kasten | d776ac6 | 2014-05-07 09:16:09 -0700 | [diff] [blame] | 198 | // clear all buffers |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 199 | if (buffer == NULL) { |
Glenn Kasten | d776ac6 | 2014-05-07 09:16:09 -0700 | [diff] [blame] | 200 | mBuffer = (char*)mCblk + sizeof(audio_track_cblk_t); |
| 201 | memset(mBuffer, 0, bufferSize); |
| 202 | } else { |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 203 | mBuffer = buffer; |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 204 | #if 0 |
Glenn Kasten | d776ac6 | 2014-05-07 09:16:09 -0700 | [diff] [blame] | 205 | 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] | 206 | #endif |
Glenn Kasten | d776ac6 | 2014-05-07 09:16:09 -0700 | [diff] [blame] | 207 | } |
Glenn Kasten | c263ca0 | 2014-06-04 20:31:46 -0700 | [diff] [blame] | 208 | break; |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 209 | case ALLOC_LOCAL: |
| 210 | mBuffer = calloc(1, bufferSize); |
| 211 | break; |
| 212 | case ALLOC_NONE: |
| 213 | mBuffer = buffer; |
| 214 | break; |
Andy Hung | 8fe6803 | 2017-06-05 16:17:51 -0700 | [diff] [blame] | 215 | default: |
Andy Hung | 9d84af5 | 2018-09-12 18:03:44 -0700 | [diff] [blame] | 216 | 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] | 217 | } |
Andy Hung | 8fe6803 | 2017-06-05 16:17:51 -0700 | [diff] [blame] | 218 | mBufferSize = bufferSize; |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 219 | |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 220 | #ifdef TEE_SINK |
Andy Hung | 8946a28 | 2018-04-19 20:04:56 -0700 | [diff] [blame] | 221 | mTee.set(sampleRate, mChannelCount, format, NBAIO_Tee::TEE_FLAG_TRACK); |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 222 | #endif |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 223 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 224 | } |
| 225 | } |
| 226 | |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 227 | status_t AudioFlinger::ThreadBase::TrackBase::initCheck() const |
| 228 | { |
| 229 | status_t status; |
| 230 | if (mType == TYPE_OUTPUT || mType == TYPE_PATCH) { |
| 231 | status = cblk() != NULL ? NO_ERROR : NO_MEMORY; |
| 232 | } else { |
| 233 | status = getCblk() != 0 ? NO_ERROR : NO_MEMORY; |
| 234 | } |
| 235 | return status; |
| 236 | } |
| 237 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 238 | AudioFlinger::ThreadBase::TrackBase::~TrackBase() |
| 239 | { |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 240 | // 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] | 241 | mServerProxy.clear(); |
Andy Hung | 689e82c | 2019-08-21 17:53:17 -0700 | [diff] [blame] | 242 | releaseCblk(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 243 | mCblkMemory.clear(); // free the shared memory before releasing the heap it belongs to |
| 244 | if (mClient != 0) { |
Eric Laurent | 021cf96 | 2014-05-13 10:18:14 -0700 | [diff] [blame] | 245 | // Client destructor must run with AudioFlinger client mutex locked |
| 246 | Mutex::Autolock _l(mClient->audioFlinger()->mClientLock); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 247 | // If the client's reference count drops to zero, the associated destructor |
| 248 | // must run with AudioFlinger lock held. Thus the explicit clear() rather than |
| 249 | // relying on the automatic clear() at end of scope. |
| 250 | mClient.clear(); |
| 251 | } |
Eric Laurent | 3bcffa1 | 2014-06-12 18:38:45 -0700 | [diff] [blame] | 252 | // flush the binder command buffer |
| 253 | IPCThreadState::self()->flushCommands(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | // AudioBufferProvider interface |
| 257 | // getNextBuffer() = 0; |
Glenn Kasten | d79072e | 2016-01-06 08:41:20 -0800 | [diff] [blame] | 258 | // This implementation of releaseBuffer() is used by Track and RecordTrack |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 259 | void AudioFlinger::ThreadBase::TrackBase::releaseBuffer(AudioBufferProvider::Buffer* buffer) |
| 260 | { |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 261 | #ifdef TEE_SINK |
Andy Hung | 8946a28 | 2018-04-19 20:04:56 -0700 | [diff] [blame] | 262 | mTee.write(buffer->raw, buffer->frameCount); |
Glenn Kasten | 46909e7 | 2013-02-26 09:20:22 -0800 | [diff] [blame] | 263 | #endif |
Glenn Kasten | da6ef13 | 2013-01-10 12:31:01 -0800 | [diff] [blame] | 264 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 265 | ServerProxy::Buffer buf; |
| 266 | buf.mFrameCount = buffer->frameCount; |
| 267 | buf.mRaw = buffer->raw; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 268 | buffer->frameCount = 0; |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 269 | buffer->raw = NULL; |
| 270 | mServerProxy->releaseBuffer(&buf); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 271 | } |
| 272 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 273 | status_t AudioFlinger::ThreadBase::TrackBase::setSyncEvent(const sp<SyncEvent>& event) |
| 274 | { |
| 275 | mSyncEvents.add(event); |
| 276 | return NO_ERROR; |
| 277 | } |
| 278 | |
Kevin Rocard | 45986c7 | 2018-12-18 18:22:59 -0800 | [diff] [blame] | 279 | AudioFlinger::ThreadBase::PatchTrackBase::PatchTrackBase(sp<ClientProxy> proxy, |
| 280 | const ThreadBase& thread, |
| 281 | const Timeout& timeout) |
| 282 | : mProxy(proxy) |
| 283 | { |
| 284 | if (timeout) { |
| 285 | setPeerTimeout(*timeout); |
| 286 | } else { |
| 287 | // Double buffer mixer |
| 288 | uint64_t mixBufferNs = ((uint64_t)2 * thread.frameCount() * 1000000000) / |
| 289 | thread.sampleRate(); |
| 290 | setPeerTimeout(std::chrono::nanoseconds{mixBufferNs}); |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | void AudioFlinger::ThreadBase::PatchTrackBase::setPeerTimeout(std::chrono::nanoseconds timeout) { |
| 295 | mPeerTimeout.tv_sec = timeout.count() / std::nano::den; |
| 296 | mPeerTimeout.tv_nsec = timeout.count() % std::nano::den; |
| 297 | } |
| 298 | |
| 299 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 300 | // ---------------------------------------------------------------------------- |
| 301 | // Playback |
| 302 | // ---------------------------------------------------------------------------- |
Andy Hung | 9d84af5 | 2018-09-12 18:03:44 -0700 | [diff] [blame] | 303 | #undef LOG_TAG |
| 304 | #define LOG_TAG "AF::TrackHandle" |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 305 | |
| 306 | AudioFlinger::TrackHandle::TrackHandle(const sp<AudioFlinger::PlaybackThread::Track>& track) |
| 307 | : BnAudioTrack(), |
| 308 | mTrack(track) |
| 309 | { |
| 310 | } |
| 311 | |
| 312 | AudioFlinger::TrackHandle::~TrackHandle() { |
| 313 | // just stop the track on deletion, associated resources |
| 314 | // will be freed from the main thread once all pending buffers have |
| 315 | // been played. Unless it's not in the active track list, in which |
| 316 | // case we free everything now... |
| 317 | mTrack->destroy(); |
| 318 | } |
| 319 | |
| 320 | sp<IMemory> AudioFlinger::TrackHandle::getCblk() const { |
| 321 | return mTrack->getCblk(); |
| 322 | } |
| 323 | |
| 324 | status_t AudioFlinger::TrackHandle::start() { |
| 325 | return mTrack->start(); |
| 326 | } |
| 327 | |
| 328 | void AudioFlinger::TrackHandle::stop() { |
| 329 | mTrack->stop(); |
| 330 | } |
| 331 | |
| 332 | void AudioFlinger::TrackHandle::flush() { |
| 333 | mTrack->flush(); |
| 334 | } |
| 335 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 336 | void AudioFlinger::TrackHandle::pause() { |
| 337 | mTrack->pause(); |
| 338 | } |
| 339 | |
| 340 | status_t AudioFlinger::TrackHandle::attachAuxEffect(int EffectId) |
| 341 | { |
| 342 | return mTrack->attachAuxEffect(EffectId); |
| 343 | } |
| 344 | |
Glenn Kasten | 3dcd00d | 2013-07-17 10:10:23 -0700 | [diff] [blame] | 345 | status_t AudioFlinger::TrackHandle::setParameters(const String8& keyValuePairs) { |
| 346 | return mTrack->setParameters(keyValuePairs); |
| 347 | } |
| 348 | |
Mikhail Naganov | ac917ac | 2018-11-28 14:03:52 -0800 | [diff] [blame] | 349 | status_t AudioFlinger::TrackHandle::selectPresentation(int presentationId, int programId) { |
| 350 | return mTrack->selectPresentation(presentationId, programId); |
| 351 | } |
| 352 | |
Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 353 | VolumeShaper::Status AudioFlinger::TrackHandle::applyVolumeShaper( |
| 354 | const sp<VolumeShaper::Configuration>& configuration, |
| 355 | const sp<VolumeShaper::Operation>& operation) { |
| 356 | return mTrack->applyVolumeShaper(configuration, operation); |
| 357 | } |
| 358 | |
| 359 | sp<VolumeShaper::State> AudioFlinger::TrackHandle::getVolumeShaperState(int id) { |
| 360 | return mTrack->getVolumeShaperState(id); |
| 361 | } |
| 362 | |
Glenn Kasten | 53cec22 | 2013-08-29 09:01:02 -0700 | [diff] [blame] | 363 | status_t AudioFlinger::TrackHandle::getTimestamp(AudioTimestamp& timestamp) |
| 364 | { |
Glenn Kasten | 573d80a | 2013-08-26 09:36:23 -0700 | [diff] [blame] | 365 | return mTrack->getTimestamp(timestamp); |
Glenn Kasten | 53cec22 | 2013-08-29 09:01:02 -0700 | [diff] [blame] | 366 | } |
| 367 | |
Eric Laurent | 59fe010 | 2013-09-27 18:48:26 -0700 | [diff] [blame] | 368 | |
| 369 | void AudioFlinger::TrackHandle::signal() |
| 370 | { |
| 371 | return mTrack->signal(); |
| 372 | } |
| 373 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 374 | status_t AudioFlinger::TrackHandle::onTransact( |
| 375 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) |
| 376 | { |
| 377 | return BnAudioTrack::onTransact(code, data, reply, flags); |
| 378 | } |
| 379 | |
| 380 | // ---------------------------------------------------------------------------- |
Jean-Michel Trivi | 74e01fa | 2019-02-25 12:18:09 -0800 | [diff] [blame] | 381 | // AppOp for audio playback |
| 382 | // ------------------------------- |
Mikhail Naganov | f7e3a3a | 2019-04-22 16:43:26 -0700 | [diff] [blame] | 383 | |
| 384 | // static |
| 385 | sp<AudioFlinger::PlaybackThread::OpPlayAudioMonitor> |
| 386 | AudioFlinger::PlaybackThread::OpPlayAudioMonitor::createIfNeeded( |
Eric Laurent | 2dab030 | 2019-05-08 18:15:55 -0700 | [diff] [blame] | 387 | uid_t uid, const audio_attributes_t& attr, int id, audio_stream_type_t streamType) |
Jean-Michel Trivi | 74e01fa | 2019-02-25 12:18:09 -0800 | [diff] [blame] | 388 | { |
Eric Laurent | 9066ad3 | 2019-05-20 14:40:10 -0700 | [diff] [blame] | 389 | if (isServiceUid(uid)) { |
| 390 | Vector <String16> packages; |
| 391 | getPackagesForUid(uid, packages); |
| 392 | if (packages.isEmpty()) { |
| 393 | ALOGD("OpPlayAudio: not muting track:%d usage:%d for service UID %d", |
| 394 | id, |
| 395 | attr.usage, |
| 396 | uid); |
| 397 | return nullptr; |
| 398 | } |
Jean-Michel Trivi | 74e01fa | 2019-02-25 12:18:09 -0800 | [diff] [blame] | 399 | } |
| 400 | // stream type has been filtered by audio policy to indicate whether it can be muted |
| 401 | if (streamType == AUDIO_STREAM_ENFORCED_AUDIBLE) { |
Eric Laurent | 2dab030 | 2019-05-08 18:15:55 -0700 | [diff] [blame] | 402 | 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] | 403 | return nullptr; |
Jean-Michel Trivi | 74e01fa | 2019-02-25 12:18:09 -0800 | [diff] [blame] | 404 | } |
Eric Laurent | 2dab030 | 2019-05-08 18:15:55 -0700 | [diff] [blame] | 405 | if ((attr.flags & AUDIO_FLAG_BYPASS_INTERRUPTION_POLICY) |
| 406 | == AUDIO_FLAG_BYPASS_INTERRUPTION_POLICY) { |
| 407 | ALOGD("OpPlayAudio: not muting track:%d flags %#x have FLAG_BYPASS_INTERRUPTION_POLICY", |
| 408 | id, attr.flags); |
| 409 | return nullptr; |
| 410 | } |
| 411 | return new OpPlayAudioMonitor(uid, attr.usage, id); |
Mikhail Naganov | f7e3a3a | 2019-04-22 16:43:26 -0700 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | AudioFlinger::PlaybackThread::OpPlayAudioMonitor::OpPlayAudioMonitor( |
| 415 | uid_t uid, audio_usage_t usage, int id) |
| 416 | : mHasOpPlayAudio(true), mUid(uid), mUsage((int32_t) usage), mId(id) |
| 417 | { |
Jean-Michel Trivi | 74e01fa | 2019-02-25 12:18:09 -0800 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | AudioFlinger::PlaybackThread::OpPlayAudioMonitor::~OpPlayAudioMonitor() |
| 421 | { |
| 422 | if (mOpCallback != 0) { |
| 423 | mAppOpsManager.stopWatchingMode(mOpCallback); |
| 424 | } |
| 425 | mOpCallback.clear(); |
| 426 | } |
| 427 | |
Mikhail Naganov | f7e3a3a | 2019-04-22 16:43:26 -0700 | [diff] [blame] | 428 | void AudioFlinger::PlaybackThread::OpPlayAudioMonitor::onFirstRef() |
| 429 | { |
Eric Laurent | 9066ad3 | 2019-05-20 14:40:10 -0700 | [diff] [blame] | 430 | getPackagesForUid(mUid, mPackages); |
Mikhail Naganov | f7e3a3a | 2019-04-22 16:43:26 -0700 | [diff] [blame] | 431 | checkPlayAudioForUsage(); |
| 432 | if (!mPackages.isEmpty()) { |
| 433 | mOpCallback = new PlayAudioOpCallback(this); |
| 434 | mAppOpsManager.startWatchingMode(AppOpsManager::OP_PLAY_AUDIO, mPackages[0], mOpCallback); |
| 435 | } |
| 436 | } |
| 437 | |
Jean-Michel Trivi | 74e01fa | 2019-02-25 12:18:09 -0800 | [diff] [blame] | 438 | bool AudioFlinger::PlaybackThread::OpPlayAudioMonitor::hasOpPlayAudio() const { |
| 439 | return mHasOpPlayAudio.load(); |
| 440 | } |
| 441 | |
Jean-Michel Trivi | ddf87ef | 2019-08-20 15:42:04 -0700 | [diff] [blame] | 442 | // 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] | 443 | // - not called from constructor due to check on UID, |
| 444 | // - not called from PlayAudioOpCallback because the callback is not installed in this case |
| 445 | void AudioFlinger::PlaybackThread::OpPlayAudioMonitor::checkPlayAudioForUsage() |
| 446 | { |
| 447 | if (mPackages.isEmpty()) { |
| 448 | mHasOpPlayAudio.store(false); |
| 449 | } else { |
| 450 | bool hasIt = true; |
| 451 | for (const String16& packageName : mPackages) { |
| 452 | const int32_t mode = mAppOpsManager.checkAudioOpNoThrow(AppOpsManager::OP_PLAY_AUDIO, |
| 453 | mUsage, mUid, packageName); |
| 454 | if (mode != AppOpsManager::MODE_ALLOWED) { |
| 455 | hasIt = false; |
| 456 | break; |
| 457 | } |
| 458 | } |
| 459 | ALOGD("OpPlayAudio: track:%d usage:%d %smuted", mId, mUsage, hasIt ? "not " : ""); |
| 460 | mHasOpPlayAudio.store(hasIt); |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | AudioFlinger::PlaybackThread::OpPlayAudioMonitor::PlayAudioOpCallback::PlayAudioOpCallback( |
| 465 | const wp<OpPlayAudioMonitor>& monitor) : mMonitor(monitor) |
| 466 | { } |
| 467 | |
| 468 | void AudioFlinger::PlaybackThread::OpPlayAudioMonitor::PlayAudioOpCallback::opChanged(int32_t op, |
| 469 | const String16& packageName) { |
| 470 | // we only have uid, so we need to check all package names anyway |
| 471 | UNUSED(packageName); |
| 472 | if (op != AppOpsManager::OP_PLAY_AUDIO) { |
| 473 | return; |
| 474 | } |
| 475 | sp<OpPlayAudioMonitor> monitor = mMonitor.promote(); |
| 476 | if (monitor != NULL) { |
| 477 | monitor->checkPlayAudioForUsage(); |
| 478 | } |
| 479 | } |
| 480 | |
Eric Laurent | 9066ad3 | 2019-05-20 14:40:10 -0700 | [diff] [blame] | 481 | // static |
| 482 | void AudioFlinger::PlaybackThread::OpPlayAudioMonitor::getPackagesForUid( |
| 483 | uid_t uid, Vector<String16>& packages) |
| 484 | { |
| 485 | PermissionController permissionController; |
| 486 | permissionController.getPackagesForUid(uid, packages); |
| 487 | } |
| 488 | |
Jean-Michel Trivi | 74e01fa | 2019-02-25 12:18:09 -0800 | [diff] [blame] | 489 | // ---------------------------------------------------------------------------- |
Andy Hung | 9d84af5 | 2018-09-12 18:03:44 -0700 | [diff] [blame] | 490 | #undef LOG_TAG |
| 491 | #define LOG_TAG "AF::Track" |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 492 | |
| 493 | // Track constructor must be called with AudioFlinger::mLock and ThreadBase::mLock held |
| 494 | AudioFlinger::PlaybackThread::Track::Track( |
| 495 | PlaybackThread *thread, |
| 496 | const sp<Client>& client, |
| 497 | audio_stream_type_t streamType, |
Kevin Rocard | 1f564ac | 2018-03-29 13:53:10 -0700 | [diff] [blame] | 498 | const audio_attributes_t& attr, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 499 | uint32_t sampleRate, |
| 500 | audio_format_t format, |
| 501 | audio_channel_mask_t channelMask, |
| 502 | size_t frameCount, |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 503 | void *buffer, |
Andy Hung | 8fe6803 | 2017-06-05 16:17:51 -0700 | [diff] [blame] | 504 | size_t bufferSize, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 505 | const sp<IMemory>& sharedBuffer, |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 506 | audio_session_t sessionId, |
Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 507 | pid_t creatorPid, |
Andy Hung | 1f12a8a | 2016-11-07 16:10:30 -0800 | [diff] [blame] | 508 | uid_t uid, |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 509 | audio_output_flags_t flags, |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 510 | track_type type, |
| 511 | audio_port_handle_t portId) |
Kevin Rocard | 1f564ac | 2018-03-29 13:53:10 -0700 | [diff] [blame] | 512 | : TrackBase(thread, client, attr, sampleRate, format, channelMask, frameCount, |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 513 | (sharedBuffer != 0) ? sharedBuffer->pointer() : buffer, |
Andy Hung | 8fe6803 | 2017-06-05 16:17:51 -0700 | [diff] [blame] | 514 | (sharedBuffer != 0) ? sharedBuffer->size() : bufferSize, |
Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 515 | sessionId, creatorPid, uid, true /*isOut*/, |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 516 | (type == TYPE_PATCH) ? ( buffer == NULL ? ALLOC_LOCAL : ALLOC_NONE) : ALLOC_CBLK, |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 517 | type, portId), |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 518 | mFillingUpStatus(FS_INVALID), |
| 519 | // mRetryCount initialized later when needed |
| 520 | mSharedBuffer(sharedBuffer), |
| 521 | mStreamType(streamType), |
rago | 94a1ee8 | 2017-07-21 15:11:02 -0700 | [diff] [blame] | 522 | mMainBuffer(thread->sinkBuffer()), |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 523 | mAuxBuffer(NULL), |
| 524 | mAuxEffectId(0), mHasVolumeController(false), |
| 525 | mPresentationCompleteFrames(0), |
Andy Hung | e10393e | 2015-06-12 13:59:33 -0700 | [diff] [blame] | 526 | mFrameMap(16 /* sink-frame-to-track-frame map memory */), |
Ivan Lozano | 8cf3a07 | 2017-08-09 09:01:33 -0700 | [diff] [blame] | 527 | mVolumeHandler(new media::VolumeHandler(sampleRate)), |
Eric Laurent | 2dab030 | 2019-05-08 18:15:55 -0700 | [diff] [blame] | 528 | mOpPlayAudioMonitor(OpPlayAudioMonitor::createIfNeeded(uid, attr, id(), streamType)), |
Andy Hung | e10393e | 2015-06-12 13:59:33 -0700 | [diff] [blame] | 529 | // mSinkTimestamp |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 530 | mFastIndex(-1), |
Glenn Kasten | 5736c35 | 2012-12-04 12:12:34 -0800 | [diff] [blame] | 531 | mCachedVolume(1.0), |
Kevin Rocard | 1238109 | 2018-04-11 09:19:59 -0700 | [diff] [blame] | 532 | /* The track might not play immediately after being active, similarly as if its volume was 0. |
| 533 | * When the track starts playing, its volume will be computed. */ |
| 534 | mFinalVolume(0.f), |
Haynes Mathew George | 7844f67 | 2014-01-15 12:32:55 -0800 | [diff] [blame] | 535 | mResumeToStopping(false), |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 536 | mFlushHwPending(false), |
| 537 | mFlags(flags) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 538 | { |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 539 | // client == 0 implies sharedBuffer == 0 |
| 540 | ALOG_ASSERT(!(client == 0 && sharedBuffer != 0)); |
| 541 | |
Andy Hung | 9d84af5 | 2018-09-12 18:03:44 -0700 | [diff] [blame] | 542 | ALOGV_IF(sharedBuffer != 0, "%s(%d): sharedBuffer: %p, size: %zu", |
| 543 | __func__, mId, sharedBuffer->pointer(), sharedBuffer->size()); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 544 | |
Glenn Kasten | 3ef14ef | 2014-03-13 15:08:51 -0700 | [diff] [blame] | 545 | if (mCblk == NULL) { |
| 546 | return; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 547 | } |
Glenn Kasten | 3ef14ef | 2014-03-13 15:08:51 -0700 | [diff] [blame] | 548 | |
Andy Hung | 689e82c | 2019-08-21 17:53:17 -0700 | [diff] [blame] | 549 | if (!thread->isTrackAllowed_l(channelMask, format, sessionId, uid)) { |
| 550 | ALOGE("%s(%d): no more tracks available", __func__, mId); |
| 551 | releaseCblk(); // this makes the track invalid. |
| 552 | return; |
| 553 | } |
| 554 | |
Glenn Kasten | 3ef14ef | 2014-03-13 15:08:51 -0700 | [diff] [blame] | 555 | if (sharedBuffer == 0) { |
| 556 | mAudioTrackServerProxy = new AudioTrackServerProxy(mCblk, mBuffer, frameCount, |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 557 | mFrameSize, !isExternalTrack(), sampleRate); |
Glenn Kasten | 3ef14ef | 2014-03-13 15:08:51 -0700 | [diff] [blame] | 558 | } else { |
| 559 | mAudioTrackServerProxy = new StaticAudioTrackServerProxy(mCblk, mBuffer, frameCount, |
| 560 | mFrameSize); |
| 561 | } |
| 562 | mServerProxy = mAudioTrackServerProxy; |
| 563 | |
Glenn Kasten | 3ef14ef | 2014-03-13 15:08:51 -0700 | [diff] [blame] | 564 | // 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] | 565 | if (flags & AUDIO_OUTPUT_FLAG_FAST) { |
Andy Hung | a542782 | 2015-09-11 16:15:35 -0700 | [diff] [blame] | 566 | // FIXME: Not calling framesReadyIsCalledByMultipleThreads() exposes a potential |
| 567 | // race with setSyncEvent(). However, if we call it, we cannot properly start |
| 568 | // static fast tracks (SoundPool) immediately after stopping. |
| 569 | //mAudioTrackServerProxy->framesReadyIsCalledByMultipleThreads(); |
Glenn Kasten | 3ef14ef | 2014-03-13 15:08:51 -0700 | [diff] [blame] | 570 | ALOG_ASSERT(thread->mFastTrackAvailMask != 0); |
| 571 | int i = __builtin_ctz(thread->mFastTrackAvailMask); |
Glenn Kasten | dc2c50b | 2016-04-21 08:13:14 -0700 | [diff] [blame] | 572 | ALOG_ASSERT(0 < i && i < (int)FastMixerState::sMaxFastTracks); |
Glenn Kasten | 3ef14ef | 2014-03-13 15:08:51 -0700 | [diff] [blame] | 573 | // FIXME This is too eager. We allocate a fast track index before the |
| 574 | // fast track becomes active. Since fast tracks are a scarce resource, |
| 575 | // this means we are potentially denying other more important fast tracks from |
| 576 | // being created. It would be better to allocate the index dynamically. |
| 577 | mFastIndex = i; |
Glenn Kasten | 3ef14ef | 2014-03-13 15:08:51 -0700 | [diff] [blame] | 578 | thread->mFastTrackAvailMask &= ~(1 << i); |
| 579 | } |
Andy Hung | 8946a28 | 2018-04-19 20:04:56 -0700 | [diff] [blame] | 580 | |
Andy Hung | 1c86ebe | 2018-05-29 20:29:08 -0700 | [diff] [blame] | 581 | mServerLatencySupported = thread->type() == ThreadBase::MIXER |
| 582 | || thread->type() == ThreadBase::DUPLICATING; |
Andy Hung | 8946a28 | 2018-04-19 20:04:56 -0700 | [diff] [blame] | 583 | #ifdef TEE_SINK |
| 584 | mTee.setId(std::string("_") + std::to_string(mThreadIoHandle) |
Kevin Rocard | 51f0e98 | 2019-02-01 19:19:11 -0800 | [diff] [blame] | 585 | + "_" + std::to_string(mId) + "_T"); |
Andy Hung | 8946a28 | 2018-04-19 20:04:56 -0700 | [diff] [blame] | 586 | #endif |
jiabin | 57303cc | 2018-12-18 15:45:57 -0800 | [diff] [blame] | 587 | |
| 588 | if (channelMask & AUDIO_CHANNEL_HAPTIC_ALL) { |
| 589 | mAudioVibrationController = new AudioVibrationController(this); |
| 590 | mExternalVibration = new os::ExternalVibration( |
| 591 | mUid, "" /* pkg */, mAttr, mAudioVibrationController); |
| 592 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 593 | } |
| 594 | |
| 595 | AudioFlinger::PlaybackThread::Track::~Track() |
| 596 | { |
Andy Hung | 9d84af5 | 2018-09-12 18:03:44 -0700 | [diff] [blame] | 597 | ALOGV("%s(%d)", __func__, mId); |
Glenn Kasten | 0c72b24 | 2013-09-11 09:14:16 -0700 | [diff] [blame] | 598 | |
| 599 | // The destructor would clear mSharedBuffer, |
| 600 | // but it will not push the decremented reference count, |
| 601 | // leaving the client's IMemory dangling indefinitely. |
| 602 | // This prevents that leak. |
| 603 | if (mSharedBuffer != 0) { |
| 604 | mSharedBuffer.clear(); |
Glenn Kasten | 0c72b24 | 2013-09-11 09:14:16 -0700 | [diff] [blame] | 605 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 606 | } |
| 607 | |
Glenn Kasten | 0300333 | 2013-08-06 15:40:54 -0700 | [diff] [blame] | 608 | status_t AudioFlinger::PlaybackThread::Track::initCheck() const |
| 609 | { |
| 610 | status_t status = TrackBase::initCheck(); |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 611 | if (status == NO_ERROR && mCblk == nullptr) { |
Glenn Kasten | 0300333 | 2013-08-06 15:40:54 -0700 | [diff] [blame] | 612 | status = NO_MEMORY; |
| 613 | } |
| 614 | return status; |
| 615 | } |
| 616 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 617 | void AudioFlinger::PlaybackThread::Track::destroy() |
| 618 | { |
| 619 | // NOTE: destroyTrack_l() can remove a strong reference to this Track |
| 620 | // by removing it from mTracks vector, so there is a risk that this Tracks's |
| 621 | // destructor is called. As the destructor needs to lock mLock, |
| 622 | // we must acquire a strong reference on this Track before locking mLock |
| 623 | // here so that the destructor is called only when exiting this function. |
| 624 | // On the other hand, as long as Track::destroy() is only called by |
| 625 | // TrackHandle destructor, the TrackHandle still holds a strong ref on |
| 626 | // this Track with its member mTrack. |
| 627 | sp<Track> keep(this); |
| 628 | { // scope for mLock |
Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 629 | bool wasActive = false; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 630 | sp<ThreadBase> thread = mThread.promote(); |
| 631 | if (thread != 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 632 | Mutex::Autolock _l(thread->mLock); |
| 633 | PlaybackThread *playbackThread = (PlaybackThread *)thread.get(); |
Eric Laurent | aaa4447 | 2014-09-12 17:41:50 -0700 | [diff] [blame] | 634 | wasActive = playbackThread->destroyTrack_l(this); |
| 635 | } |
| 636 | if (isExternalTrack() && !wasActive) { |
Eric Laurent | d7fe086 | 2018-07-14 16:48:01 -0700 | [diff] [blame] | 637 | AudioSystem::releaseOutput(mPortId); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 638 | } |
| 639 | } |
Kevin Rocard | c43ea14 | 2019-01-31 18:17:37 -0800 | [diff] [blame] | 640 | forEachTeePatchTrack([](auto patchTrack) { patchTrack->destroy(); }); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 641 | } |
| 642 | |
Andy Hung | f6ab58d | 2018-05-25 12:50:39 -0700 | [diff] [blame] | 643 | void AudioFlinger::PlaybackThread::Track::appendDumpHeader(String8& result) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 644 | { |
Eric Laurent | 973db02 | 2018-11-20 14:54:31 -0800 | [diff] [blame] | 645 | result.appendFormat("Type Id Active Client Session Port Id S Flags " |
Kevin Rocard | 5f2136e | 2018-05-11 22:03:00 -0700 | [diff] [blame] | 646 | " Format Chn mask SRate " |
| 647 | "ST Usg CT " |
| 648 | " G db L dB R dB VS dB " |
| 649 | " Server FrmCnt FrmRdy F Underruns Flushed" |
| 650 | "%s\n", |
| 651 | isServerLatencySupported() ? " Latency" : ""); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 652 | } |
| 653 | |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 654 | void AudioFlinger::PlaybackThread::Track::appendDump(String8& result, bool active) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 655 | { |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 656 | char trackType; |
| 657 | switch (mType) { |
| 658 | case TYPE_DEFAULT: |
| 659 | case TYPE_OUTPUT: |
Andy Hung | f6ab58d | 2018-05-25 12:50:39 -0700 | [diff] [blame] | 660 | if (isStatic()) { |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 661 | trackType = 'S'; // static |
| 662 | } else { |
| 663 | trackType = ' '; // normal |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 664 | } |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 665 | break; |
| 666 | case TYPE_PATCH: |
| 667 | trackType = 'P'; |
| 668 | break; |
| 669 | default: |
| 670 | trackType = '?'; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 671 | } |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 672 | |
| 673 | if (isFastTrack()) { |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 674 | result.appendFormat("F%d %c %6d", mFastIndex, trackType, mId); |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 675 | } else { |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 676 | result.appendFormat(" %c %6d", trackType, mId); |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 677 | } |
| 678 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 679 | char nowInUnderrun; |
| 680 | switch (mObservedUnderruns.mBitFields.mMostRecent) { |
| 681 | case UNDERRUN_FULL: |
| 682 | nowInUnderrun = ' '; |
| 683 | break; |
| 684 | case UNDERRUN_PARTIAL: |
| 685 | nowInUnderrun = '<'; |
| 686 | break; |
| 687 | case UNDERRUN_EMPTY: |
| 688 | nowInUnderrun = '*'; |
| 689 | break; |
| 690 | default: |
| 691 | nowInUnderrun = '?'; |
| 692 | break; |
| 693 | } |
Andy Hung | da540db | 2017-04-20 14:06:17 -0700 | [diff] [blame] | 694 | |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 695 | char fillingStatus; |
| 696 | switch (mFillingUpStatus) { |
| 697 | case FS_INVALID: |
| 698 | fillingStatus = 'I'; |
| 699 | break; |
| 700 | case FS_FILLING: |
| 701 | fillingStatus = 'f'; |
| 702 | break; |
| 703 | case FS_FILLED: |
| 704 | fillingStatus = 'F'; |
| 705 | break; |
| 706 | case FS_ACTIVE: |
| 707 | fillingStatus = 'A'; |
| 708 | break; |
| 709 | default: |
| 710 | fillingStatus = '?'; |
| 711 | break; |
| 712 | } |
| 713 | |
| 714 | // clip framesReadySafe to max representation in dump |
| 715 | const size_t framesReadySafe = |
| 716 | std::min(mAudioTrackServerProxy->framesReadySafe(), (size_t)99999999); |
| 717 | |
| 718 | // obtain volumes |
| 719 | const gain_minifloat_packed_t vlr = mAudioTrackServerProxy->getVolumeLR(); |
| 720 | const std::pair<float /* volume */, bool /* active */> vsVolume = |
| 721 | mVolumeHandler->getLastVolume(); |
| 722 | |
| 723 | // Our effective frame count is obtained by ServerProxy::getBufferSizeInFrames() |
| 724 | // as it may be reduced by the application. |
| 725 | const size_t bufferSizeInFrames = (size_t)mAudioTrackServerProxy->getBufferSizeInFrames(); |
| 726 | // Check whether the buffer size has been modified by the app. |
| 727 | const char modifiedBufferChar = bufferSizeInFrames < mFrameCount |
| 728 | ? 'r' /* buffer reduced */: bufferSizeInFrames > mFrameCount |
| 729 | ? 'e' /* error */ : ' ' /* identical */; |
| 730 | |
Eric Laurent | 973db02 | 2018-11-20 14:54:31 -0800 | [diff] [blame] | 731 | result.appendFormat("%7s %6u %7u %7u %2s 0x%03X " |
Kevin Rocard | 5f2136e | 2018-05-11 22:03:00 -0700 | [diff] [blame] | 732 | "%08X %08X %6u " |
| 733 | "%2u %3x %2x " |
| 734 | "%5.2g %5.2g %5.2g %5.2g%c " |
| 735 | "%08X %6zu%c %6zu %c %9u%c %7u", |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 736 | active ? "yes" : "no", |
Andy Hung | 4ef19fa | 2018-05-15 19:35:29 -0700 | [diff] [blame] | 737 | (mClient == 0) ? getpid() : mClient->pid(), |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 738 | mSessionId, |
Eric Laurent | 973db02 | 2018-11-20 14:54:31 -0800 | [diff] [blame] | 739 | mPortId, |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 740 | getTrackStateString(), |
| 741 | mCblk->mFlags, |
| 742 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 743 | mFormat, |
| 744 | mChannelMask, |
Andy Hung | cef2daa | 2018-06-01 15:31:49 -0700 | [diff] [blame] | 745 | sampleRate(), |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 746 | |
| 747 | mStreamType, |
Kevin Rocard | 5f2136e | 2018-05-11 22:03:00 -0700 | [diff] [blame] | 748 | mAttr.usage, |
| 749 | mAttr.content_type, |
| 750 | |
| 751 | 20.0 * log10(mFinalVolume), |
Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 752 | 20.0 * log10(float_from_gain(gain_minifloat_unpack_left(vlr))), |
| 753 | 20.0 * log10(float_from_gain(gain_minifloat_unpack_right(vlr))), |
Andy Hung | da540db | 2017-04-20 14:06:17 -0700 | [diff] [blame] | 754 | 20.0 * log10(vsVolume.first), // VolumeShaper(s) total volume |
| 755 | vsVolume.second ? 'A' : ' ', // if any VolumeShapers active |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 756 | |
Glenn Kasten | f20e1d8 | 2013-07-12 09:45:18 -0700 | [diff] [blame] | 757 | mCblk->mServer, |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 758 | bufferSizeInFrames, |
| 759 | modifiedBufferChar, |
| 760 | framesReadySafe, |
| 761 | fillingStatus, |
Glenn Kasten | 82aaf94 | 2013-07-17 16:05:07 -0700 | [diff] [blame] | 762 | mAudioTrackServerProxy->getUnderrunFrames(), |
Andy Hung | 2148bf0 | 2016-11-28 19:01:02 -0800 | [diff] [blame] | 763 | nowInUnderrun, |
Andy Hung | f6ab58d | 2018-05-25 12:50:39 -0700 | [diff] [blame] | 764 | (unsigned)mAudioTrackServerProxy->framesFlushed() % 10000000 |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 765 | ); |
Andy Hung | cef2daa | 2018-06-01 15:31:49 -0700 | [diff] [blame] | 766 | |
| 767 | if (isServerLatencySupported()) { |
| 768 | double latencyMs; |
| 769 | bool fromTrack; |
| 770 | if (getTrackLatencyMs(&latencyMs, &fromTrack) == OK) { |
| 771 | // Show latency in msec, followed by 't' if from track timestamp (the most accurate) |
| 772 | // or 'k' if estimated from kernel because track frames haven't been presented yet. |
| 773 | result.appendFormat(" %7.2lf %c", latencyMs, fromTrack ? 't' : 'k'); |
Andy Hung | f6ab58d | 2018-05-25 12:50:39 -0700 | [diff] [blame] | 774 | } else { |
Andy Hung | cef2daa | 2018-06-01 15:31:49 -0700 | [diff] [blame] | 775 | result.appendFormat("%10s", mCblk->mServer != 0 ? "unavail" : "new"); |
Andy Hung | f6ab58d | 2018-05-25 12:50:39 -0700 | [diff] [blame] | 776 | } |
| 777 | } |
| 778 | result.append("\n"); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 779 | } |
| 780 | |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 781 | uint32_t AudioFlinger::PlaybackThread::Track::sampleRate() const { |
| 782 | return mAudioTrackServerProxy->getSampleRate(); |
| 783 | } |
| 784 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 785 | // AudioBufferProvider interface |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 786 | status_t AudioFlinger::PlaybackThread::Track::getNextBuffer(AudioBufferProvider::Buffer* buffer) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 787 | { |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 788 | ServerProxy::Buffer buf; |
| 789 | size_t desiredFrames = buffer->frameCount; |
| 790 | buf.mFrameCount = desiredFrames; |
| 791 | status_t status = mServerProxy->obtainBuffer(&buf); |
| 792 | buffer->frameCount = buf.mFrameCount; |
| 793 | buffer->raw = buf.mRaw; |
Mikhail Naganov | a66d389 | 2017-05-03 16:50:56 -0700 | [diff] [blame] | 794 | if (buf.mFrameCount == 0 && !isStopping() && !isStopped() && !isPaused()) { |
Andy Hung | 9d84af5 | 2018-09-12 18:03:44 -0700 | [diff] [blame] | 795 | ALOGV("%s(%d): underrun, framesReady(%zu) < framesDesired(%zd), state: %d", |
| 796 | __func__, mId, buf.mFrameCount, desiredFrames, mState); |
Glenn Kasten | 82aaf94 | 2013-07-17 16:05:07 -0700 | [diff] [blame] | 797 | mAudioTrackServerProxy->tallyUnderrunFrames(desiredFrames); |
Phil Burk | 2812d9e | 2016-01-04 10:34:30 -0800 | [diff] [blame] | 798 | } else { |
| 799 | mAudioTrackServerProxy->tallyUnderrunFrames(0); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 800 | } |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 801 | return status; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 802 | } |
| 803 | |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 804 | void AudioFlinger::PlaybackThread::Track::releaseBuffer(AudioBufferProvider::Buffer* buffer) |
| 805 | { |
| 806 | interceptBuffer(*buffer); |
| 807 | TrackBase::releaseBuffer(buffer); |
| 808 | } |
| 809 | |
| 810 | // TODO: compensate for time shift between HW modules. |
| 811 | void AudioFlinger::PlaybackThread::Track::interceptBuffer( |
Kevin Rocard | a134b00 | 2019-02-07 18:05:31 -0800 | [diff] [blame] | 812 | const AudioBufferProvider::Buffer& sourceBuffer) { |
Kevin Rocard | 6057fa2 | 2019-02-08 14:08:07 -0800 | [diff] [blame] | 813 | auto start = std::chrono::steady_clock::now(); |
Kevin Rocard | a134b00 | 2019-02-07 18:05:31 -0800 | [diff] [blame] | 814 | const size_t frameCount = sourceBuffer.frameCount; |
Kevin Rocard | d83b08a | 2019-02-27 15:05:54 -0800 | [diff] [blame] | 815 | if (frameCount == 0) { |
| 816 | return; // No audio to intercept. |
| 817 | // Additionally PatchProxyBufferProvider::obtainBuffer (called by PathTrack::getNextBuffer) |
| 818 | // does not allow 0 frame size request contrary to getNextBuffer |
| 819 | } |
| 820 | for (auto& teePatch : mTeePatches) { |
| 821 | RecordThread::PatchRecord* patchRecord = teePatch.patchRecord.get(); |
Kevin Rocard | a134b00 | 2019-02-07 18:05:31 -0800 | [diff] [blame] | 822 | |
| 823 | size_t framesWritten = writeFrames(patchRecord, sourceBuffer.i8, frameCount); |
| 824 | // On buffer wrap, the buffer frame count will be less than requested, |
| 825 | // when this happens a second buffer needs to be used to write the leftover audio |
| 826 | size_t framesLeft = frameCount - framesWritten; |
| 827 | if (framesWritten != 0 && framesLeft != 0) { |
| 828 | framesWritten += |
| 829 | writeFrames(patchRecord, sourceBuffer.i8 + framesWritten * mFrameSize, framesLeft); |
| 830 | framesLeft = frameCount - framesWritten; |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 831 | } |
Kevin Rocard | a134b00 | 2019-02-07 18:05:31 -0800 | [diff] [blame] | 832 | ALOGW_IF(framesLeft != 0, "%s(%d) PatchRecord %d can not provide big enough " |
| 833 | "buffer %zu/%zu, dropping %zu frames", __func__, mId, patchRecord->mId, |
| 834 | framesWritten, frameCount, framesLeft); |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 835 | } |
Kevin Rocard | 6057fa2 | 2019-02-08 14:08:07 -0800 | [diff] [blame] | 836 | auto spent = ceil<std::chrono::microseconds>(std::chrono::steady_clock::now() - start); |
| 837 | using namespace std::chrono_literals; |
| 838 | // Average is ~20us per track, this should virtually never be logged (Logging takes >200us) |
| 839 | ALOGD_IF(spent > 200us, "%s: took %lldus to intercept %zu tracks", __func__, |
| 840 | spent.count(), mTeePatches.size()); |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 841 | } |
| 842 | |
Kevin Rocard | a134b00 | 2019-02-07 18:05:31 -0800 | [diff] [blame] | 843 | size_t AudioFlinger::PlaybackThread::Track::writeFrames(AudioBufferProvider* dest, |
| 844 | const void* src, |
| 845 | size_t frameCount) { |
| 846 | AudioBufferProvider::Buffer patchBuffer; |
| 847 | patchBuffer.frameCount = frameCount; |
| 848 | auto status = dest->getNextBuffer(&patchBuffer); |
| 849 | if (status != NO_ERROR) { |
| 850 | ALOGW("%s PathRecord getNextBuffer failed with error %d: %s", |
| 851 | __func__, status, strerror(-status)); |
| 852 | return 0; |
| 853 | } |
| 854 | ALOG_ASSERT(patchBuffer.frameCount <= frameCount); |
| 855 | memcpy(patchBuffer.raw, src, patchBuffer.frameCount * mFrameSize); |
| 856 | auto framesWritten = patchBuffer.frameCount; |
| 857 | dest->releaseBuffer(&patchBuffer); |
| 858 | return framesWritten; |
| 859 | } |
| 860 | |
Glenn Kasten | 6466c9e | 2013-08-23 10:54:07 -0700 | [diff] [blame] | 861 | // releaseBuffer() is not overridden |
| 862 | |
| 863 | // ExtendedAudioBufferProvider interface |
| 864 | |
Andy Hung | 27876c0 | 2014-09-09 18:07:55 -0700 | [diff] [blame] | 865 | // framesReady() may return an approximation of the number of frames if called |
| 866 | // from a different thread than the one calling Proxy->obtainBuffer() and |
| 867 | // Proxy->releaseBuffer(). Also note there is no mutual exclusion in the |
| 868 | // AudioTrackServerProxy so be especially careful calling with FastTracks. |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 869 | size_t AudioFlinger::PlaybackThread::Track::framesReady() const { |
Andy Hung | 27876c0 | 2014-09-09 18:07:55 -0700 | [diff] [blame] | 870 | if (mSharedBuffer != 0 && (isStopped() || isStopping())) { |
| 871 | // Static tracks return zero frames immediately upon stopping (for FastTracks). |
| 872 | // The remainder of the buffer is not drained. |
| 873 | return 0; |
| 874 | } |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 875 | return mAudioTrackServerProxy->framesReady(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 876 | } |
| 877 | |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 878 | int64_t AudioFlinger::PlaybackThread::Track::framesReleased() const |
Glenn Kasten | 6466c9e | 2013-08-23 10:54:07 -0700 | [diff] [blame] | 879 | { |
| 880 | return mAudioTrackServerProxy->framesReleased(); |
| 881 | } |
| 882 | |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 883 | void AudioFlinger::PlaybackThread::Track::onTimestamp(const ExtendedTimestamp ×tamp) |
Andy Hung | 6ae5843 | 2016-02-16 18:32:24 -0800 | [diff] [blame] | 884 | { |
| 885 | // This call comes from a FastTrack and should be kept lockless. |
| 886 | // The server side frames are already translated to client frames. |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 887 | mAudioTrackServerProxy->setTimestamp(timestamp); |
Andy Hung | 6ae5843 | 2016-02-16 18:32:24 -0800 | [diff] [blame] | 888 | |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 889 | // 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] | 890 | |
| 891 | // Compute latency. |
| 892 | // TODO: Consider whether the server latency may be passed in by FastMixer |
| 893 | // as a constant for all active FastTracks. |
| 894 | const double latencyMs = timestamp.getOutputServerLatencyMs(sampleRate()); |
| 895 | mServerLatencyFromTrack.store(true); |
| 896 | mServerLatencyMs.store(latencyMs); |
Andy Hung | 6ae5843 | 2016-02-16 18:32:24 -0800 | [diff] [blame] | 897 | } |
| 898 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 899 | // Don't call for fast tracks; the framesReady() could result in priority inversion |
| 900 | bool AudioFlinger::PlaybackThread::Track::isReady() const { |
Krishnankutty Kolathappilly | 8d6c292 | 2014-02-04 16:23:42 -0800 | [diff] [blame] | 901 | if (mFillingUpStatus != FS_FILLING || isStopped() || isPausing()) { |
| 902 | return true; |
| 903 | } |
| 904 | |
Eric Laurent | 1649851 | 2014-03-17 17:22:08 -0700 | [diff] [blame] | 905 | if (isStopping()) { |
| 906 | if (framesReady() > 0) { |
| 907 | mFillingUpStatus = FS_FILLED; |
| 908 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 909 | return true; |
| 910 | } |
| 911 | |
Phil Burk | e8972b0 | 2016-03-04 11:29:57 -0800 | [diff] [blame] | 912 | if (framesReady() >= mServerProxy->getBufferSizeInFrames() || |
Glenn Kasten | 96f60d8 | 2013-07-12 10:21:18 -0700 | [diff] [blame] | 913 | (mCblk->mFlags & CBLK_FORCEREADY)) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 914 | mFillingUpStatus = FS_FILLED; |
Glenn Kasten | 96f60d8 | 2013-07-12 10:21:18 -0700 | [diff] [blame] | 915 | android_atomic_and(~CBLK_FORCEREADY, &mCblk->mFlags); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 916 | return true; |
| 917 | } |
| 918 | return false; |
| 919 | } |
| 920 | |
Glenn Kasten | 0f11b51 | 2014-01-31 16:18:54 -0800 | [diff] [blame] | 921 | status_t AudioFlinger::PlaybackThread::Track::start(AudioSystem::sync_event_t event __unused, |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 922 | audio_session_t triggerSession __unused) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 923 | { |
| 924 | status_t status = NO_ERROR; |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 925 | ALOGV("%s(%d): calling pid %d session %d", |
| 926 | __func__, mId, IPCThreadState::self()->getCallingPid(), mSessionId); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 927 | |
| 928 | sp<ThreadBase> thread = mThread.promote(); |
| 929 | if (thread != 0) { |
Eric Laurent | 813e2a7 | 2013-08-31 12:59:48 -0700 | [diff] [blame] | 930 | if (isOffloaded()) { |
| 931 | Mutex::Autolock _laf(thread->mAudioFlinger->mLock); |
| 932 | Mutex::Autolock _lth(thread->mLock); |
| 933 | sp<EffectChain> ec = thread->getEffectChain_l(mSessionId); |
Eric Laurent | 5baf2af | 2013-09-12 17:37:00 -0700 | [diff] [blame] | 934 | if (thread->mAudioFlinger->isNonOffloadableGlobalEffectEnabled_l() || |
| 935 | (ec != 0 && ec->isNonOffloadableEnabled())) { |
Eric Laurent | 813e2a7 | 2013-08-31 12:59:48 -0700 | [diff] [blame] | 936 | invalidate(); |
| 937 | return PERMISSION_DENIED; |
| 938 | } |
| 939 | } |
| 940 | Mutex::Autolock _lth(thread->mLock); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 941 | track_state state = mState; |
| 942 | // here the track could be either new, or restarted |
| 943 | // in both cases "unstop" the track |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 944 | |
Krishnankutty Kolathappilly | 8d6c292 | 2014-02-04 16:23:42 -0800 | [diff] [blame] | 945 | // initial state-stopping. next state-pausing. |
| 946 | // What if resume is called ? |
| 947 | |
| 948 | if (state == PAUSED || state == PAUSING) { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 949 | if (mResumeToStopping) { |
| 950 | // happened we need to resume to STOPPING_1 |
| 951 | mState = TrackBase::STOPPING_1; |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 952 | ALOGV("%s(%d): PAUSED => STOPPING_1 on thread %d", |
| 953 | __func__, mId, (int)mThreadIoHandle); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 954 | } else { |
| 955 | mState = TrackBase::RESUMING; |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 956 | ALOGV("%s(%d): PAUSED => RESUMING on thread %d", |
| 957 | __func__, mId, (int)mThreadIoHandle); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 958 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 959 | } else { |
| 960 | mState = TrackBase::ACTIVE; |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 961 | ALOGV("%s(%d): ? => ACTIVE on thread %d", |
| 962 | __func__, mId, (int)mThreadIoHandle); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 963 | } |
| 964 | |
Andy Hung | e10393e | 2015-06-12 13:59:33 -0700 | [diff] [blame] | 965 | // states to reset position info for non-offloaded/direct tracks |
| 966 | if (!isOffloaded() && !isDirect() |
| 967 | && (state == IDLE || state == STOPPED || state == FLUSHED)) { |
| 968 | mFrameMap.reset(); |
| 969 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 970 | PlaybackThread *playbackThread = (PlaybackThread *)thread.get(); |
Haynes Mathew George | 240934b | 2015-03-11 18:25:50 -0700 | [diff] [blame] | 971 | if (isFastTrack()) { |
| 972 | // refresh fast track underruns on start because that field is never cleared |
| 973 | // by the fast mixer; furthermore, the same track can be recycled, i.e. start |
| 974 | // after stop. |
| 975 | mObservedUnderruns = playbackThread->getFastTrackUnderruns(mFastIndex); |
| 976 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 977 | status = playbackThread->addTrack_l(this); |
| 978 | if (status == INVALID_OPERATION || status == PERMISSION_DENIED) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 979 | triggerEvents(AudioSystem::SYNC_EVENT_PRESENTATION_COMPLETE); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 980 | // restore previous state if start was rejected by policy manager |
| 981 | if (status == PERMISSION_DENIED) { |
| 982 | mState = state; |
| 983 | } |
| 984 | } |
Andy Hung | 1d3556d | 2018-03-29 16:30:14 -0700 | [diff] [blame] | 985 | |
| 986 | if (status == NO_ERROR || status == ALREADY_EXISTS) { |
| 987 | // for streaming tracks, remove the buffer read stop limit. |
| 988 | mAudioTrackServerProxy->start(); |
| 989 | } |
| 990 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 991 | // track was already in the active list, not a problem |
| 992 | if (status == ALREADY_EXISTS) { |
| 993 | status = NO_ERROR; |
Glenn Kasten | 12022ff | 2013-10-17 11:32:39 -0700 | [diff] [blame] | 994 | } else { |
| 995 | // Acknowledge any pending flush(), so that subsequent new data isn't discarded. |
| 996 | // It is usually unsafe to access the server proxy from a binder thread. |
| 997 | // But in this case we know the mixer thread (whether normal mixer or fast mixer) |
| 998 | // isn't looking at this track yet: we still hold the normal mixer thread lock, |
| 999 | // 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] | 1000 | // 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] | 1001 | ServerProxy::Buffer buffer; |
| 1002 | buffer.mFrameCount = 1; |
| 1003 | (void) mAudioTrackServerProxy->obtainBuffer(&buffer, true /*ackFlush*/); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1004 | } |
| 1005 | } else { |
| 1006 | status = BAD_VALUE; |
| 1007 | } |
Kevin Rocard | c43ea14 | 2019-01-31 18:17:37 -0800 | [diff] [blame] | 1008 | if (status == NO_ERROR) { |
| 1009 | forEachTeePatchTrack([](auto patchTrack) { patchTrack->start(); }); |
| 1010 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1011 | return status; |
| 1012 | } |
| 1013 | |
| 1014 | void AudioFlinger::PlaybackThread::Track::stop() |
| 1015 | { |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 1016 | ALOGV("%s(%d): calling pid %d", __func__, mId, IPCThreadState::self()->getCallingPid()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1017 | sp<ThreadBase> thread = mThread.promote(); |
| 1018 | if (thread != 0) { |
| 1019 | Mutex::Autolock _l(thread->mLock); |
| 1020 | track_state state = mState; |
| 1021 | if (state == RESUMING || state == ACTIVE || state == PAUSING || state == PAUSED) { |
| 1022 | // If the track is not active (PAUSED and buffers full), flush buffers |
| 1023 | PlaybackThread *playbackThread = (PlaybackThread *)thread.get(); |
| 1024 | if (playbackThread->mActiveTracks.indexOf(this) < 0) { |
| 1025 | reset(); |
| 1026 | mState = STOPPED; |
Eric Laurent | ab5cdba | 2014-06-09 17:22:27 -0700 | [diff] [blame] | 1027 | } else if (!isFastTrack() && !isOffloaded() && !isDirect()) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1028 | mState = STOPPED; |
| 1029 | } else { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1030 | // For fast tracks prepareTracks_l() will set state to STOPPING_2 |
| 1031 | // presentation is complete |
| 1032 | // For an offloaded track this starts a drain and state will |
| 1033 | // move to STOPPING_2 when drain completes and then STOPPED |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1034 | mState = STOPPING_1; |
Eric Laurent | e93cc03 | 2016-05-05 10:15:10 -0700 | [diff] [blame] | 1035 | if (isOffloaded()) { |
| 1036 | mRetryCount = PlaybackThread::kMaxTrackStopRetriesOffload; |
| 1037 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1038 | } |
Eric Laurent | b369caf | 2015-03-30 20:51:47 -0700 | [diff] [blame] | 1039 | playbackThread->broadcast_l(); |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 1040 | ALOGV("%s(%d): not stopping/stopped => stopping/stopped on thread %d", |
| 1041 | __func__, mId, (int)mThreadIoHandle); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1042 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1043 | } |
Kevin Rocard | c43ea14 | 2019-01-31 18:17:37 -0800 | [diff] [blame] | 1044 | forEachTeePatchTrack([](auto patchTrack) { patchTrack->stop(); }); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1045 | } |
| 1046 | |
| 1047 | void AudioFlinger::PlaybackThread::Track::pause() |
| 1048 | { |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 1049 | ALOGV("%s(%d): calling pid %d", __func__, mId, IPCThreadState::self()->getCallingPid()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1050 | sp<ThreadBase> thread = mThread.promote(); |
| 1051 | if (thread != 0) { |
| 1052 | Mutex::Autolock _l(thread->mLock); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1053 | PlaybackThread *playbackThread = (PlaybackThread *)thread.get(); |
| 1054 | switch (mState) { |
| 1055 | case STOPPING_1: |
| 1056 | case STOPPING_2: |
| 1057 | if (!isOffloaded()) { |
| 1058 | /* nothing to do if track is not offloaded */ |
| 1059 | break; |
| 1060 | } |
| 1061 | |
| 1062 | // Offloaded track was draining, we need to carry on draining when resumed |
| 1063 | mResumeToStopping = true; |
Chih-Hung Hsieh | 2b48703 | 2018-09-13 14:16:02 -0700 | [diff] [blame] | 1064 | FALLTHROUGH_INTENDED; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1065 | case ACTIVE: |
| 1066 | case RESUMING: |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1067 | mState = PAUSING; |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 1068 | ALOGV("%s(%d): ACTIVE/RESUMING => PAUSING on thread %d", |
| 1069 | __func__, mId, (int)mThreadIoHandle); |
Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 1070 | playbackThread->broadcast_l(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1071 | break; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1072 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1073 | default: |
| 1074 | break; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1075 | } |
| 1076 | } |
Kevin Rocard | c43ea14 | 2019-01-31 18:17:37 -0800 | [diff] [blame] | 1077 | // Pausing the TeePatch to avoid a glitch on underrun, at the cost of buffered audio loss. |
| 1078 | forEachTeePatchTrack([](auto patchTrack) { patchTrack->pause(); }); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1079 | } |
| 1080 | |
| 1081 | void AudioFlinger::PlaybackThread::Track::flush() |
| 1082 | { |
Andy Hung | c069138 | 2018-09-12 18:01:57 -0700 | [diff] [blame] | 1083 | ALOGV("%s(%d)", __func__, mId); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1084 | sp<ThreadBase> thread = mThread.promote(); |
| 1085 | if (thread != 0) { |
| 1086 | Mutex::Autolock _l(thread->mLock); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1087 | PlaybackThread *playbackThread = (PlaybackThread *)thread.get(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1088 | |
Phil Burk | 4bb650b | 2016-09-09 12:11:17 -0700 | [diff] [blame] | 1089 | // Flush the ring buffer now if the track is not active in the PlaybackThread. |
| 1090 | // Otherwise the flush would not be done until the track is resumed. |
| 1091 | // Requires FastTrack removal be BLOCK_UNTIL_ACKED |
| 1092 | if (playbackThread->mActiveTracks.indexOf(this) < 0) { |
| 1093 | (void)mServerProxy->flushBufferIfNeeded(); |
| 1094 | } |
| 1095 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1096 | if (isOffloaded()) { |
| 1097 | // If offloaded we allow flush during any state except terminated |
| 1098 | // and keep the track active to avoid problems if user is seeking |
| 1099 | // rapidly and underlying hardware has a significant delay handling |
| 1100 | // a pause |
| 1101 | if (isTerminated()) { |
| 1102 | return; |
| 1103 | } |
| 1104 | |
Andy Hung | 9d84af5 | 2018-09-12 18:03:44 -0700 | [diff] [blame] | 1105 | ALOGV("%s(%d): offload flush", __func__, mId); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1106 | reset(); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1107 | |
| 1108 | if (mState == STOPPING_1 || mState == STOPPING_2) { |
Andy Hung | 9d84af5 | 2018-09-12 18:03:44 -0700 | [diff] [blame] | 1109 | ALOGV("%s(%d): flushed in STOPPING_1 or 2 state, change state to ACTIVE", |
| 1110 | __func__, mId); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1111 | mState = ACTIVE; |
| 1112 | } |
| 1113 | |
Haynes Mathew George | 7844f67 | 2014-01-15 12:32:55 -0800 | [diff] [blame] | 1114 | mFlushHwPending = true; |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1115 | mResumeToStopping = false; |
| 1116 | } else { |
| 1117 | if (mState != STOPPING_1 && mState != STOPPING_2 && mState != STOPPED && |
| 1118 | mState != PAUSED && mState != PAUSING && mState != IDLE && mState != FLUSHED) { |
| 1119 | return; |
| 1120 | } |
| 1121 | // No point remaining in PAUSED state after a flush => go to |
| 1122 | // FLUSHED state |
| 1123 | mState = FLUSHED; |
| 1124 | // do not reset the track if it is still in the process of being stopped or paused. |
| 1125 | // this will be done by prepareTracks_l() when the track is stopped. |
| 1126 | // prepareTracks_l() will see mState == FLUSHED, then |
| 1127 | // remove from active track list, reset(), and trigger presentation complete |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 1128 | if (isDirect()) { |
| 1129 | mFlushHwPending = true; |
| 1130 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1131 | if (playbackThread->mActiveTracks.indexOf(this) < 0) { |
| 1132 | reset(); |
| 1133 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1134 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1135 | // Prevent flush being lost if the track is flushed and then resumed |
| 1136 | // before mixer thread can run. This is important when offloading |
| 1137 | // because the hardware buffer could hold a large amount of audio |
Eric Laurent | ede6c3b | 2013-09-19 14:37:46 -0700 | [diff] [blame] | 1138 | playbackThread->broadcast_l(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1139 | } |
Kevin Rocard | c43ea14 | 2019-01-31 18:17:37 -0800 | [diff] [blame] | 1140 | // Flush the Tee to avoid on resume playing old data and glitching on the transition to new data |
| 1141 | forEachTeePatchTrack([](auto patchTrack) { patchTrack->flush(); }); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1142 | } |
| 1143 | |
Haynes Mathew George | 7844f67 | 2014-01-15 12:32:55 -0800 | [diff] [blame] | 1144 | // must be called with thread lock held |
| 1145 | void AudioFlinger::PlaybackThread::Track::flushAck() |
| 1146 | { |
Eric Laurent | d1f69b0 | 2014-12-15 14:33:13 -0800 | [diff] [blame] | 1147 | if (!isOffloaded() && !isDirect()) |
Haynes Mathew George | 7844f67 | 2014-01-15 12:32:55 -0800 | [diff] [blame] | 1148 | return; |
| 1149 | |
Phil Burk | 4bb650b | 2016-09-09 12:11:17 -0700 | [diff] [blame] | 1150 | // Clear the client ring buffer so that the app can prime the buffer while paused. |
| 1151 | // Otherwise it might not get cleared until playback is resumed and obtainBuffer() is called. |
| 1152 | mServerProxy->flushBufferIfNeeded(); |
| 1153 | |
Haynes Mathew George | 7844f67 | 2014-01-15 12:32:55 -0800 | [diff] [blame] | 1154 | mFlushHwPending = false; |
| 1155 | } |
| 1156 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1157 | void AudioFlinger::PlaybackThread::Track::reset() |
| 1158 | { |
| 1159 | // Do not reset twice to avoid discarding data written just after a flush and before |
| 1160 | // the audioflinger thread detects the track is stopped. |
| 1161 | if (!mResetDone) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1162 | // Force underrun condition to avoid false underrun callback until first data is |
| 1163 | // written to buffer |
Glenn Kasten | 96f60d8 | 2013-07-12 10:21:18 -0700 | [diff] [blame] | 1164 | android_atomic_and(~CBLK_FORCEREADY, &mCblk->mFlags); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1165 | mFillingUpStatus = FS_FILLING; |
| 1166 | mResetDone = true; |
| 1167 | if (mState == FLUSHED) { |
| 1168 | mState = IDLE; |
| 1169 | } |
| 1170 | } |
| 1171 | } |
| 1172 | |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1173 | status_t AudioFlinger::PlaybackThread::Track::setParameters(const String8& keyValuePairs) |
| 1174 | { |
| 1175 | sp<ThreadBase> thread = mThread.promote(); |
| 1176 | if (thread == 0) { |
Andy Hung | 9d84af5 | 2018-09-12 18:03:44 -0700 | [diff] [blame] | 1177 | ALOGE("%s(%d): thread is dead", __func__, mId); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1178 | return FAILED_TRANSACTION; |
| 1179 | } else if ((thread->type() == ThreadBase::DIRECT) || |
| 1180 | (thread->type() == ThreadBase::OFFLOAD)) { |
| 1181 | return thread->setParameters(keyValuePairs); |
| 1182 | } else { |
| 1183 | return PERMISSION_DENIED; |
| 1184 | } |
| 1185 | } |
| 1186 | |
Mikhail Naganov | ac917ac | 2018-11-28 14:03:52 -0800 | [diff] [blame] | 1187 | status_t AudioFlinger::PlaybackThread::Track::selectPresentation(int presentationId, |
| 1188 | int programId) { |
| 1189 | sp<ThreadBase> thread = mThread.promote(); |
| 1190 | if (thread == 0) { |
| 1191 | ALOGE("thread is dead"); |
| 1192 | return FAILED_TRANSACTION; |
| 1193 | } else if ((thread->type() == ThreadBase::DIRECT) || (thread->type() == ThreadBase::OFFLOAD)) { |
| 1194 | DirectOutputThread *directOutputThread = static_cast<DirectOutputThread*>(thread.get()); |
| 1195 | return directOutputThread->selectPresentation(presentationId, programId); |
| 1196 | } |
| 1197 | return INVALID_OPERATION; |
| 1198 | } |
| 1199 | |
Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 1200 | VolumeShaper::Status AudioFlinger::PlaybackThread::Track::applyVolumeShaper( |
| 1201 | const sp<VolumeShaper::Configuration>& configuration, |
| 1202 | const sp<VolumeShaper::Operation>& operation) |
| 1203 | { |
Andy Hung | 10cbff1 | 2017-02-21 17:30:14 -0800 | [diff] [blame] | 1204 | sp<VolumeShaper::Configuration> newConfiguration; |
Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 1205 | |
Andy Hung | 10cbff1 | 2017-02-21 17:30:14 -0800 | [diff] [blame] | 1206 | if (isOffloadedOrDirect()) { |
| 1207 | const VolumeShaper::Configuration::OptionFlag optionFlag |
| 1208 | = configuration->getOptionFlags(); |
| 1209 | if ((optionFlag & VolumeShaper::Configuration::OPTION_FLAG_CLOCK_TIME) == 0) { |
Andy Hung | 9d84af5 | 2018-09-12 18:03:44 -0700 | [diff] [blame] | 1210 | ALOGW("%s(%d): %s tracks do not support frame counted VolumeShaper," |
| 1211 | " using clock time instead", |
| 1212 | __func__, mId, |
| 1213 | isOffloaded() ? "Offload" : "Direct"); |
Andy Hung | 10cbff1 | 2017-02-21 17:30:14 -0800 | [diff] [blame] | 1214 | newConfiguration = new VolumeShaper::Configuration(*configuration); |
| 1215 | newConfiguration->setOptionFlags( |
| 1216 | VolumeShaper::Configuration::OptionFlag(optionFlag |
| 1217 | | VolumeShaper::Configuration::OPTION_FLAG_CLOCK_TIME)); |
| 1218 | } |
| 1219 | } |
| 1220 | |
| 1221 | VolumeShaper::Status status = mVolumeHandler->applyVolumeShaper( |
| 1222 | (newConfiguration.get() != nullptr ? newConfiguration : configuration), operation); |
| 1223 | |
| 1224 | if (isOffloadedOrDirect()) { |
| 1225 | // Signal thread to fetch new volume. |
| 1226 | sp<ThreadBase> thread = mThread.promote(); |
| 1227 | if (thread != 0) { |
Kevin Rocard | 5f2136e | 2018-05-11 22:03:00 -0700 | [diff] [blame] | 1228 | Mutex::Autolock _l(thread->mLock); |
Andy Hung | 10cbff1 | 2017-02-21 17:30:14 -0800 | [diff] [blame] | 1229 | thread->broadcast_l(); |
| 1230 | } |
| 1231 | } |
| 1232 | return status; |
Andy Hung | 9fc8b5c | 2017-01-24 13:36:48 -0800 | [diff] [blame] | 1233 | } |
| 1234 | |
| 1235 | sp<VolumeShaper::State> AudioFlinger::PlaybackThread::Track::getVolumeShaperState(int id) |
| 1236 | { |
| 1237 | // Note: We don't check if Thread exists. |
| 1238 | |
| 1239 | // mVolumeHandler is thread safe. |
| 1240 | return mVolumeHandler->getVolumeShaperState(id); |
| 1241 | } |
| 1242 | |
Kevin Rocard | 1238109 | 2018-04-11 09:19:59 -0700 | [diff] [blame] | 1243 | void AudioFlinger::PlaybackThread::Track::setFinalVolume(float volume) |
| 1244 | { |
| 1245 | if (mFinalVolume != volume) { // Compare to an epsilon if too many meaningless updates |
| 1246 | mFinalVolume = volume; |
| 1247 | setMetadataHasChanged(); |
| 1248 | } |
| 1249 | } |
| 1250 | |
| 1251 | void AudioFlinger::PlaybackThread::Track::copyMetadataTo(MetadataInserter& backInserter) const |
| 1252 | { |
| 1253 | *backInserter++ = { |
| 1254 | .usage = mAttr.usage, |
| 1255 | .content_type = mAttr.content_type, |
| 1256 | .gain = mFinalVolume, |
| 1257 | }; |
| 1258 | } |
| 1259 | |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1260 | void AudioFlinger::PlaybackThread::Track::setTeePatches(TeePatches teePatches) { |
Kevin Rocard | c43ea14 | 2019-01-31 18:17:37 -0800 | [diff] [blame] | 1261 | forEachTeePatchTrack([](auto patchTrack) { patchTrack->destroy(); }); |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1262 | mTeePatches = std::move(teePatches); |
| 1263 | } |
| 1264 | |
Glenn Kasten | 573d80a | 2013-08-26 09:36:23 -0700 | [diff] [blame] | 1265 | status_t AudioFlinger::PlaybackThread::Track::getTimestamp(AudioTimestamp& timestamp) |
| 1266 | { |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 1267 | if (!isOffloaded() && !isDirect()) { |
| 1268 | return INVALID_OPERATION; // normal tracks handled through SSQ |
Glenn Kasten | fe346c7 | 2013-08-30 13:28:22 -0700 | [diff] [blame] | 1269 | } |
Glenn Kasten | 573d80a | 2013-08-26 09:36:23 -0700 | [diff] [blame] | 1270 | sp<ThreadBase> thread = mThread.promote(); |
| 1271 | if (thread == 0) { |
Glenn Kasten | fe346c7 | 2013-08-30 13:28:22 -0700 | [diff] [blame] | 1272 | return INVALID_OPERATION; |
Glenn Kasten | 573d80a | 2013-08-26 09:36:23 -0700 | [diff] [blame] | 1273 | } |
Phil Burk | 6140c79 | 2015-03-19 14:30:21 -0700 | [diff] [blame] | 1274 | |
Glenn Kasten | 573d80a | 2013-08-26 09:36:23 -0700 | [diff] [blame] | 1275 | Mutex::Autolock _l(thread->mLock); |
| 1276 | PlaybackThread *playbackThread = (PlaybackThread *)thread.get(); |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 1277 | return playbackThread->getTimestamp_l(timestamp); |
Glenn Kasten | 573d80a | 2013-08-26 09:36:23 -0700 | [diff] [blame] | 1278 | } |
| 1279 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1280 | status_t AudioFlinger::PlaybackThread::Track::attachAuxEffect(int EffectId) |
| 1281 | { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1282 | sp<ThreadBase> thread = mThread.promote(); |
Eric Laurent | 6c79632 | 2019-04-09 14:13:17 -0700 | [diff] [blame] | 1283 | if (thread == nullptr) { |
| 1284 | return DEAD_OBJECT; |
| 1285 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1286 | |
Eric Laurent | 6c79632 | 2019-04-09 14:13:17 -0700 | [diff] [blame] | 1287 | sp<PlaybackThread> dstThread = (PlaybackThread *)thread.get(); |
| 1288 | sp<PlaybackThread> srcThread; // srcThread is initialized by call to moveAuxEffectToIo() |
| 1289 | sp<AudioFlinger> af = mClient->audioFlinger(); |
| 1290 | status_t status = af->moveAuxEffectToIo(EffectId, dstThread, &srcThread); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1291 | |
Eric Laurent | 6c79632 | 2019-04-09 14:13:17 -0700 | [diff] [blame] | 1292 | if (EffectId != 0 && status == NO_ERROR) { |
| 1293 | status = dstThread->attachAuxEffect(this, EffectId); |
| 1294 | if (status == NO_ERROR) { |
| 1295 | AudioSystem::moveEffectsToIo(std::vector<int>(EffectId), dstThread->id()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1296 | } |
Eric Laurent | 6c79632 | 2019-04-09 14:13:17 -0700 | [diff] [blame] | 1297 | } |
| 1298 | |
| 1299 | if (status != NO_ERROR && srcThread != nullptr) { |
| 1300 | af->moveAuxEffectToIo(EffectId, srcThread, &dstThread); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1301 | } |
| 1302 | return status; |
| 1303 | } |
| 1304 | |
| 1305 | void AudioFlinger::PlaybackThread::Track::setAuxBuffer(int EffectId, int32_t *buffer) |
| 1306 | { |
| 1307 | mAuxEffectId = EffectId; |
| 1308 | mAuxBuffer = buffer; |
| 1309 | } |
| 1310 | |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 1311 | bool AudioFlinger::PlaybackThread::Track::presentationComplete( |
| 1312 | int64_t framesWritten, size_t audioHalFrames) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1313 | { |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 1314 | // TODO: improve this based on FrameMap if it exists, to ensure full drain. |
| 1315 | // This assists in proper timestamp computation as well as wakelock management. |
| 1316 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1317 | // a track is considered presented when the total number of frames written to audio HAL |
| 1318 | // corresponds to the number of frames written when presentationComplete() is called for the |
| 1319 | // first time (mPresentationCompleteFrames == 0) plus the buffer filling status at that time. |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1320 | // For an offloaded track the HAL+h/w delay is variable so a HAL drain() is used |
| 1321 | // to detect when all frames have been played. In this case framesWritten isn't |
| 1322 | // useful because it doesn't always reflect whether there is data in the h/w |
| 1323 | // buffers, particularly if a track has been paused and resumed during draining |
Andy Hung | 9d84af5 | 2018-09-12 18:03:44 -0700 | [diff] [blame] | 1324 | ALOGV("%s(%d): presentationComplete() mPresentationCompleteFrames %lld framesWritten %lld", |
| 1325 | __func__, mId, |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 1326 | (long long)mPresentationCompleteFrames, (long long)framesWritten); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1327 | if (mPresentationCompleteFrames == 0) { |
| 1328 | mPresentationCompleteFrames = framesWritten + audioHalFrames; |
Andy Hung | 9d84af5 | 2018-09-12 18:03:44 -0700 | [diff] [blame] | 1329 | ALOGV("%s(%d): presentationComplete() reset:" |
| 1330 | " mPresentationCompleteFrames %lld audioHalFrames %zu", |
| 1331 | __func__, mId, |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 1332 | (long long)mPresentationCompleteFrames, audioHalFrames); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1333 | } |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1334 | |
Andy Hung | c54b1ff | 2016-02-23 14:07:07 -0800 | [diff] [blame] | 1335 | bool complete; |
| 1336 | if (isOffloaded()) { |
| 1337 | complete = true; |
| 1338 | } else if (isDirect() || isFastTrack()) { // these do not go through linear map |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 1339 | complete = framesWritten >= (int64_t) mPresentationCompleteFrames; |
Andy Hung | c54b1ff | 2016-02-23 14:07:07 -0800 | [diff] [blame] | 1340 | } else { // Normal tracks, OutputTracks, and PatchTracks |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 1341 | complete = framesWritten >= (int64_t) mPresentationCompleteFrames |
Andy Hung | c54b1ff | 2016-02-23 14:07:07 -0800 | [diff] [blame] | 1342 | && mAudioTrackServerProxy->isDrained(); |
| 1343 | } |
| 1344 | |
| 1345 | if (complete) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1346 | triggerEvents(AudioSystem::SYNC_EVENT_PRESENTATION_COMPLETE); |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1347 | mAudioTrackServerProxy->setStreamEndDone(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1348 | return true; |
| 1349 | } |
| 1350 | return false; |
| 1351 | } |
| 1352 | |
| 1353 | void AudioFlinger::PlaybackThread::Track::triggerEvents(AudioSystem::sync_event_t type) |
| 1354 | { |
Ivan Lozano | 5ec161b | 2017-12-06 10:00:28 -0800 | [diff] [blame] | 1355 | for (size_t i = 0; i < mSyncEvents.size();) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1356 | if (mSyncEvents[i]->type() == type) { |
| 1357 | mSyncEvents[i]->trigger(); |
| 1358 | mSyncEvents.removeAt(i); |
Ivan Lozano | 5ec161b | 2017-12-06 10:00:28 -0800 | [diff] [blame] | 1359 | } else { |
| 1360 | ++i; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1361 | } |
| 1362 | } |
| 1363 | } |
| 1364 | |
| 1365 | // implement VolumeBufferProvider interface |
| 1366 | |
Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 1367 | gain_minifloat_packed_t AudioFlinger::PlaybackThread::Track::getVolumeLR() |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1368 | { |
| 1369 | // called by FastMixer, so not allowed to take any locks, block, or do I/O including logs |
| 1370 | ALOG_ASSERT(isFastTrack() && (mCblk != NULL)); |
Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 1371 | gain_minifloat_packed_t vlr = mAudioTrackServerProxy->getVolumeLR(); |
| 1372 | float vl = float_from_gain(gain_minifloat_unpack_left(vlr)); |
| 1373 | float vr = float_from_gain(gain_minifloat_unpack_right(vlr)); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1374 | // 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] | 1375 | if (vl > GAIN_FLOAT_UNITY) { |
| 1376 | vl = GAIN_FLOAT_UNITY; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1377 | } |
Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 1378 | if (vr > GAIN_FLOAT_UNITY) { |
| 1379 | vr = GAIN_FLOAT_UNITY; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1380 | } |
| 1381 | // now apply the cached master volume and stream type volume; |
| 1382 | // this is trusted but lacks any synchronization or barrier so may be stale |
| 1383 | float v = mCachedVolume; |
| 1384 | vl *= v; |
| 1385 | vr *= v; |
Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 1386 | // re-combine into packed minifloat |
| 1387 | vlr = gain_minifloat_pack(gain_from_float(vl), gain_from_float(vr)); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1388 | // FIXME look at mute, pause, and stop flags |
| 1389 | return vlr; |
| 1390 | } |
| 1391 | |
| 1392 | status_t AudioFlinger::PlaybackThread::Track::setSyncEvent(const sp<SyncEvent>& event) |
| 1393 | { |
Eric Laurent | bfb1b83 | 2013-01-07 09:53:42 -0800 | [diff] [blame] | 1394 | if (isTerminated() || mState == PAUSED || |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1395 | ((framesReady() == 0) && ((mSharedBuffer != 0) || |
| 1396 | (mState == STOPPED)))) { |
Andy Hung | 9d84af5 | 2018-09-12 18:03:44 -0700 | [diff] [blame] | 1397 | ALOGW("%s(%d): in invalid state %d on session %d %s mode, framesReady %zu", |
| 1398 | __func__, mId, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1399 | mState, mSessionId, (mSharedBuffer != 0) ? "static" : "stream", framesReady()); |
| 1400 | event->cancel(); |
| 1401 | return INVALID_OPERATION; |
| 1402 | } |
| 1403 | (void) TrackBase::setSyncEvent(event); |
| 1404 | return NO_ERROR; |
| 1405 | } |
| 1406 | |
Glenn Kasten | 5736c35 | 2012-12-04 12:12:34 -0800 | [diff] [blame] | 1407 | void AudioFlinger::PlaybackThread::Track::invalidate() |
| 1408 | { |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 1409 | TrackBase::invalidate(); |
Eric Laurent | 4d231dc | 2016-03-11 18:38:23 -0800 | [diff] [blame] | 1410 | signalClientFlag(CBLK_INVALID); |
Eric Laurent | 4d231dc | 2016-03-11 18:38:23 -0800 | [diff] [blame] | 1411 | } |
| 1412 | |
| 1413 | void AudioFlinger::PlaybackThread::Track::disable() |
| 1414 | { |
| 1415 | signalClientFlag(CBLK_DISABLED); |
| 1416 | } |
| 1417 | |
| 1418 | void AudioFlinger::PlaybackThread::Track::signalClientFlag(int32_t flag) |
| 1419 | { |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1420 | // FIXME should use proxy, and needs work |
| 1421 | audio_track_cblk_t* cblk = mCblk; |
Eric Laurent | 4d231dc | 2016-03-11 18:38:23 -0800 | [diff] [blame] | 1422 | android_atomic_or(flag, &cblk->mFlags); |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1423 | android_atomic_release_store(0x40000000, &cblk->mFutex); |
| 1424 | // 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] | 1425 | (void) syscall(__NR_futex, &cblk->mFutex, FUTEX_WAKE, INT_MAX); |
Glenn Kasten | 5736c35 | 2012-12-04 12:12:34 -0800 | [diff] [blame] | 1426 | } |
| 1427 | |
Eric Laurent | 59fe010 | 2013-09-27 18:48:26 -0700 | [diff] [blame] | 1428 | void AudioFlinger::PlaybackThread::Track::signal() |
| 1429 | { |
| 1430 | sp<ThreadBase> thread = mThread.promote(); |
| 1431 | if (thread != 0) { |
| 1432 | PlaybackThread *t = (PlaybackThread *)thread.get(); |
| 1433 | Mutex::Autolock _l(t->mLock); |
| 1434 | t->broadcast_l(); |
| 1435 | } |
| 1436 | } |
| 1437 | |
Krishnankutty Kolathappilly | 8d6c292 | 2014-02-04 16:23:42 -0800 | [diff] [blame] | 1438 | //To be called with thread lock held |
| 1439 | bool AudioFlinger::PlaybackThread::Track::isResumePending() { |
| 1440 | |
| 1441 | if (mState == RESUMING) |
| 1442 | return true; |
| 1443 | /* Resume is pending if track was stopping before pause was called */ |
| 1444 | if (mState == STOPPING_1 && |
| 1445 | mResumeToStopping) |
| 1446 | return true; |
| 1447 | |
| 1448 | return false; |
| 1449 | } |
| 1450 | |
| 1451 | //To be called with thread lock held |
| 1452 | void AudioFlinger::PlaybackThread::Track::resumeAck() { |
| 1453 | |
| 1454 | |
| 1455 | if (mState == RESUMING) |
| 1456 | mState = ACTIVE; |
Haynes Mathew George | 2d3ca68 | 2014-03-07 13:43:49 -0800 | [diff] [blame] | 1457 | |
Krishnankutty Kolathappilly | 8d6c292 | 2014-02-04 16:23:42 -0800 | [diff] [blame] | 1458 | // Other possibility of pending resume is stopping_1 state |
| 1459 | // Do not update the state from stopping as this prevents |
Haynes Mathew George | 2d3ca68 | 2014-03-07 13:43:49 -0800 | [diff] [blame] | 1460 | // drain being called. |
| 1461 | if (mState == STOPPING_1) { |
| 1462 | mResumeToStopping = false; |
| 1463 | } |
Krishnankutty Kolathappilly | 8d6c292 | 2014-02-04 16:23:42 -0800 | [diff] [blame] | 1464 | } |
Andy Hung | e10393e | 2015-06-12 13:59:33 -0700 | [diff] [blame] | 1465 | |
| 1466 | //To be called with thread lock held |
| 1467 | void AudioFlinger::PlaybackThread::Track::updateTrackFrameInfo( |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 1468 | int64_t trackFramesReleased, int64_t sinkFramesWritten, |
Andy Hung | cef2daa | 2018-06-01 15:31:49 -0700 | [diff] [blame] | 1469 | uint32_t halSampleRate, const ExtendedTimestamp &timeStamp) { |
Andy Hung | 3028256 | 2018-08-08 18:27:03 -0700 | [diff] [blame] | 1470 | // Make the kernel frametime available. |
| 1471 | const FrameTime ft{ |
| 1472 | timeStamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL], |
| 1473 | timeStamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL]}; |
| 1474 | // ALOGD("FrameTime: %lld %lld", (long long)ft.frames, (long long)ft.timeNs); |
| 1475 | mKernelFrameTime.store(ft); |
| 1476 | if (!audio_is_linear_pcm(mFormat)) { |
| 1477 | return; |
| 1478 | } |
| 1479 | |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 1480 | //update frame map |
Andy Hung | e10393e | 2015-06-12 13:59:33 -0700 | [diff] [blame] | 1481 | mFrameMap.push(trackFramesReleased, sinkFramesWritten); |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 1482 | |
| 1483 | // adjust server times and set drained state. |
| 1484 | // |
| 1485 | // Our timestamps are only updated when the track is on the Thread active list. |
| 1486 | // We need to ensure that tracks are not removed before full drain. |
| 1487 | ExtendedTimestamp local = timeStamp; |
Andy Hung | cef2daa | 2018-06-01 15:31:49 -0700 | [diff] [blame] | 1488 | bool drained = true; // default assume drained, if no server info found |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 1489 | bool checked = false; |
| 1490 | for (int i = ExtendedTimestamp::LOCATION_MAX - 1; |
| 1491 | i >= ExtendedTimestamp::LOCATION_SERVER; --i) { |
| 1492 | // Lookup the track frame corresponding to the sink frame position. |
| 1493 | if (local.mTimeNs[i] > 0) { |
| 1494 | local.mPosition[i] = mFrameMap.findX(local.mPosition[i]); |
| 1495 | // check drain state from the latest stage in the pipeline. |
Andy Hung | 6d7b119 | 2016-05-07 22:59:48 -0700 | [diff] [blame] | 1496 | if (!checked && i <= ExtendedTimestamp::LOCATION_KERNEL) { |
Andy Hung | cef2daa | 2018-06-01 15:31:49 -0700 | [diff] [blame] | 1497 | drained = local.mPosition[i] >= mAudioTrackServerProxy->framesReleased(); |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 1498 | checked = true; |
| 1499 | } |
| 1500 | } |
Andy Hung | e10393e | 2015-06-12 13:59:33 -0700 | [diff] [blame] | 1501 | } |
Andy Hung | cef2daa | 2018-06-01 15:31:49 -0700 | [diff] [blame] | 1502 | |
| 1503 | mAudioTrackServerProxy->setDrained(drained); |
Andy Hung | ea2b9c0 | 2016-02-12 17:06:53 -0800 | [diff] [blame] | 1504 | // Set correction for flushed frames that are not accounted for in released. |
Andy Hung | ea2b9c0 | 2016-02-12 17:06:53 -0800 | [diff] [blame] | 1505 | local.mFlushed = mAudioTrackServerProxy->framesFlushed(); |
Andy Hung | 818e7a3 | 2016-02-16 18:08:07 -0800 | [diff] [blame] | 1506 | mServerProxy->setTimestamp(local); |
Andy Hung | cef2daa | 2018-06-01 15:31:49 -0700 | [diff] [blame] | 1507 | |
| 1508 | // Compute latency info. |
| 1509 | const bool useTrackTimestamp = !drained; |
| 1510 | const double latencyMs = useTrackTimestamp |
| 1511 | ? local.getOutputServerLatencyMs(sampleRate()) |
| 1512 | : timeStamp.getOutputServerLatencyMs(halSampleRate); |
| 1513 | |
| 1514 | mServerLatencyFromTrack.store(useTrackTimestamp); |
| 1515 | mServerLatencyMs.store(latencyMs); |
Andy Hung | e10393e | 2015-06-12 13:59:33 -0700 | [diff] [blame] | 1516 | } |
| 1517 | |
jiabin | 57303cc | 2018-12-18 15:45:57 -0800 | [diff] [blame] | 1518 | binder::Status AudioFlinger::PlaybackThread::Track::AudioVibrationController::mute( |
| 1519 | /*out*/ bool *ret) { |
| 1520 | *ret = false; |
| 1521 | sp<ThreadBase> thread = mTrack->mThread.promote(); |
| 1522 | if (thread != 0) { |
| 1523 | // Lock for updating mHapticPlaybackEnabled. |
| 1524 | Mutex::Autolock _l(thread->mLock); |
| 1525 | PlaybackThread *playbackThread = (PlaybackThread *)thread.get(); |
| 1526 | if ((mTrack->channelMask() & AUDIO_CHANNEL_HAPTIC_ALL) != AUDIO_CHANNEL_NONE |
| 1527 | && playbackThread->mHapticChannelCount > 0) { |
| 1528 | mTrack->setHapticPlaybackEnabled(false); |
| 1529 | *ret = true; |
| 1530 | } |
| 1531 | } |
| 1532 | return binder::Status::ok(); |
| 1533 | } |
| 1534 | |
| 1535 | binder::Status AudioFlinger::PlaybackThread::Track::AudioVibrationController::unmute( |
| 1536 | /*out*/ bool *ret) { |
| 1537 | *ret = false; |
| 1538 | sp<ThreadBase> thread = mTrack->mThread.promote(); |
| 1539 | if (thread != 0) { |
| 1540 | // Lock for updating mHapticPlaybackEnabled. |
| 1541 | Mutex::Autolock _l(thread->mLock); |
| 1542 | PlaybackThread *playbackThread = (PlaybackThread *)thread.get(); |
| 1543 | if ((mTrack->channelMask() & AUDIO_CHANNEL_HAPTIC_ALL) != AUDIO_CHANNEL_NONE |
| 1544 | && playbackThread->mHapticChannelCount > 0) { |
| 1545 | mTrack->setHapticPlaybackEnabled(true); |
| 1546 | *ret = true; |
| 1547 | } |
| 1548 | } |
| 1549 | return binder::Status::ok(); |
| 1550 | } |
| 1551 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1552 | // ---------------------------------------------------------------------------- |
Andy Hung | 9d84af5 | 2018-09-12 18:03:44 -0700 | [diff] [blame] | 1553 | #undef LOG_TAG |
| 1554 | #define LOG_TAG "AF::OutputTrack" |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1555 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1556 | AudioFlinger::PlaybackThread::OutputTrack::OutputTrack( |
| 1557 | PlaybackThread *playbackThread, |
| 1558 | DuplicatingThread *sourceThread, |
| 1559 | uint32_t sampleRate, |
| 1560 | audio_format_t format, |
| 1561 | audio_channel_mask_t channelMask, |
Marco Nelissen | 462fd2f | 2013-01-14 14:12:05 -0800 | [diff] [blame] | 1562 | size_t frameCount, |
Andy Hung | 1f12a8a | 2016-11-07 16:10:30 -0800 | [diff] [blame] | 1563 | uid_t uid) |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 1564 | : Track(playbackThread, NULL, AUDIO_STREAM_PATCH, |
Kevin Rocard | 1f564ac | 2018-03-29 13:53:10 -0700 | [diff] [blame] | 1565 | audio_attributes_t{} /* currently unused for output track */, |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 1566 | sampleRate, format, channelMask, frameCount, |
Andy Hung | 8fe6803 | 2017-06-05 16:17:51 -0700 | [diff] [blame] | 1567 | nullptr /* buffer */, (size_t)0 /* bufferSize */, nullptr /* sharedBuffer */, |
Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 1568 | AUDIO_SESSION_NONE, getpid(), uid, AUDIO_OUTPUT_FLAG_NONE, |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1569 | TYPE_OUTPUT), |
Eric Laurent | 5bba2f6 | 2016-03-18 11:14:14 -0700 | [diff] [blame] | 1570 | mActive(false), mSourceThread(sourceThread) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1571 | { |
| 1572 | |
| 1573 | if (mCblk != NULL) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1574 | mOutBuffer.frameCount = 0; |
| 1575 | playbackThread->mTracks.add(this); |
Andy Hung | 9d84af5 | 2018-09-12 18:03:44 -0700 | [diff] [blame] | 1576 | ALOGV("%s(): mCblk %p, mBuffer %p, " |
Glenn Kasten | c42e9b4 | 2016-03-21 11:35:03 -0700 | [diff] [blame] | 1577 | "frameCount %zu, mChannelMask 0x%08x", |
Andy Hung | 9d84af5 | 2018-09-12 18:03:44 -0700 | [diff] [blame] | 1578 | __func__, mCblk, mBuffer, |
Glenn Kasten | 74935e4 | 2013-12-19 08:56:45 -0800 | [diff] [blame] | 1579 | frameCount, mChannelMask); |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 1580 | // since client and server are in the same process, |
| 1581 | // the buffer has the same virtual address on both sides |
Glenn Kasten | 529c61b | 2014-07-18 15:31:02 -0700 | [diff] [blame] | 1582 | mClientProxy = new AudioTrackClientProxy(mCblk, mBuffer, mFrameCount, mFrameSize, |
| 1583 | true /*clientInServer*/); |
Glenn Kasten | c56f342 | 2014-03-21 17:53:17 -0700 | [diff] [blame] | 1584 | mClientProxy->setVolumeLR(GAIN_MINIFLOAT_PACKED_UNITY); |
Eric Laurent | 8d2d493 | 2013-04-25 12:56:18 -0700 | [diff] [blame] | 1585 | mClientProxy->setSendLevel(0.0); |
| 1586 | mClientProxy->setSampleRate(sampleRate); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1587 | } else { |
Andy Hung | 9d84af5 | 2018-09-12 18:03:44 -0700 | [diff] [blame] | 1588 | ALOGW("%s(%d): Error creating output track on thread %d", |
| 1589 | __func__, mId, (int)mThreadIoHandle); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1590 | } |
| 1591 | } |
| 1592 | |
| 1593 | AudioFlinger::PlaybackThread::OutputTrack::~OutputTrack() |
| 1594 | { |
| 1595 | clearBufferQueue(); |
Glenn Kasten | e3aa659 | 2012-12-04 12:22:46 -0800 | [diff] [blame] | 1596 | // 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] | 1597 | } |
| 1598 | |
| 1599 | status_t AudioFlinger::PlaybackThread::OutputTrack::start(AudioSystem::sync_event_t event, |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 1600 | audio_session_t triggerSession) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1601 | { |
| 1602 | status_t status = Track::start(event, triggerSession); |
| 1603 | if (status != NO_ERROR) { |
| 1604 | return status; |
| 1605 | } |
| 1606 | |
| 1607 | mActive = true; |
| 1608 | mRetryCount = 127; |
| 1609 | return status; |
| 1610 | } |
| 1611 | |
| 1612 | void AudioFlinger::PlaybackThread::OutputTrack::stop() |
| 1613 | { |
| 1614 | Track::stop(); |
| 1615 | clearBufferQueue(); |
| 1616 | mOutBuffer.frameCount = 0; |
| 1617 | mActive = false; |
| 1618 | } |
| 1619 | |
Andy Hung | 1c86ebe | 2018-05-29 20:29:08 -0700 | [diff] [blame] | 1620 | ssize_t AudioFlinger::PlaybackThread::OutputTrack::write(void* data, uint32_t frames) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1621 | { |
| 1622 | Buffer *pInBuffer; |
| 1623 | Buffer inBuffer; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1624 | bool outputBufferFull = false; |
| 1625 | inBuffer.frameCount = frames; |
Andy Hung | c25b84a | 2015-01-14 19:04:10 -0800 | [diff] [blame] | 1626 | inBuffer.raw = data; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1627 | |
| 1628 | uint32_t waitTimeLeftMs = mSourceThread->waitTimeMs(); |
| 1629 | |
| 1630 | if (!mActive && frames != 0) { |
Andy Hung | 5bedff6 | 2015-01-16 11:05:32 -0800 | [diff] [blame] | 1631 | (void) start(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1632 | } |
| 1633 | |
| 1634 | while (waitTimeLeftMs) { |
| 1635 | // First write pending buffers, then new data |
| 1636 | if (mBufferQueue.size()) { |
| 1637 | pInBuffer = mBufferQueue.itemAt(0); |
| 1638 | } else { |
| 1639 | pInBuffer = &inBuffer; |
| 1640 | } |
| 1641 | |
| 1642 | if (pInBuffer->frameCount == 0) { |
| 1643 | break; |
| 1644 | } |
| 1645 | |
| 1646 | if (mOutBuffer.frameCount == 0) { |
| 1647 | mOutBuffer.frameCount = pInBuffer->frameCount; |
| 1648 | nsecs_t startTime = systemTime(); |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1649 | status_t status = obtainBuffer(&mOutBuffer, waitTimeLeftMs); |
Eric Laurent | 4d231dc | 2016-03-11 18:38:23 -0800 | [diff] [blame] | 1650 | if (status != NO_ERROR && status != NOT_ENOUGH_DATA) { |
Andy Hung | 9d84af5 | 2018-09-12 18:03:44 -0700 | [diff] [blame] | 1651 | ALOGV("%s(%d): thread %d no more output buffers; status %d", |
| 1652 | __func__, mId, |
| 1653 | (int)mThreadIoHandle, status); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1654 | outputBufferFull = true; |
| 1655 | break; |
| 1656 | } |
| 1657 | uint32_t waitTimeMs = (uint32_t)ns2ms(systemTime() - startTime); |
| 1658 | if (waitTimeLeftMs >= waitTimeMs) { |
| 1659 | waitTimeLeftMs -= waitTimeMs; |
| 1660 | } else { |
| 1661 | waitTimeLeftMs = 0; |
| 1662 | } |
Eric Laurent | 4d231dc | 2016-03-11 18:38:23 -0800 | [diff] [blame] | 1663 | if (status == NOT_ENOUGH_DATA) { |
| 1664 | restartIfDisabled(); |
| 1665 | continue; |
| 1666 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1667 | } |
| 1668 | |
| 1669 | uint32_t outFrames = pInBuffer->frameCount > mOutBuffer.frameCount ? mOutBuffer.frameCount : |
| 1670 | pInBuffer->frameCount; |
Andy Hung | c25b84a | 2015-01-14 19:04:10 -0800 | [diff] [blame] | 1671 | memcpy(mOutBuffer.raw, pInBuffer->raw, outFrames * mFrameSize); |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1672 | Proxy::Buffer buf; |
| 1673 | buf.mFrameCount = outFrames; |
| 1674 | buf.mRaw = NULL; |
| 1675 | mClientProxy->releaseBuffer(&buf); |
Eric Laurent | 4d231dc | 2016-03-11 18:38:23 -0800 | [diff] [blame] | 1676 | restartIfDisabled(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1677 | pInBuffer->frameCount -= outFrames; |
Andy Hung | c25b84a | 2015-01-14 19:04:10 -0800 | [diff] [blame] | 1678 | pInBuffer->raw = (int8_t *)pInBuffer->raw + outFrames * mFrameSize; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1679 | mOutBuffer.frameCount -= outFrames; |
Andy Hung | c25b84a | 2015-01-14 19:04:10 -0800 | [diff] [blame] | 1680 | mOutBuffer.raw = (int8_t *)mOutBuffer.raw + outFrames * mFrameSize; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1681 | |
| 1682 | if (pInBuffer->frameCount == 0) { |
| 1683 | if (mBufferQueue.size()) { |
| 1684 | mBufferQueue.removeAt(0); |
Andy Hung | c25b84a | 2015-01-14 19:04:10 -0800 | [diff] [blame] | 1685 | free(pInBuffer->mBuffer); |
Yunlian Jiang | 8adc808 | 2017-06-06 15:59:44 -0700 | [diff] [blame] | 1686 | if (pInBuffer != &inBuffer) { |
| 1687 | delete pInBuffer; |
| 1688 | } |
Andy Hung | 9d84af5 | 2018-09-12 18:03:44 -0700 | [diff] [blame] | 1689 | ALOGV("%s(%d): thread %d released overflow buffer %zu", |
| 1690 | __func__, mId, |
| 1691 | (int)mThreadIoHandle, mBufferQueue.size()); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1692 | } else { |
| 1693 | break; |
| 1694 | } |
| 1695 | } |
| 1696 | } |
| 1697 | |
| 1698 | // If we could not write all frames, allocate a buffer and queue it for next time. |
| 1699 | if (inBuffer.frameCount) { |
| 1700 | sp<ThreadBase> thread = mThread.promote(); |
| 1701 | if (thread != 0 && !thread->standby()) { |
| 1702 | if (mBufferQueue.size() < kMaxOverFlowBuffers) { |
| 1703 | pInBuffer = new Buffer; |
Andy Hung | c25b84a | 2015-01-14 19:04:10 -0800 | [diff] [blame] | 1704 | pInBuffer->mBuffer = malloc(inBuffer.frameCount * mFrameSize); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1705 | pInBuffer->frameCount = inBuffer.frameCount; |
Andy Hung | c25b84a | 2015-01-14 19:04:10 -0800 | [diff] [blame] | 1706 | pInBuffer->raw = pInBuffer->mBuffer; |
| 1707 | memcpy(pInBuffer->raw, inBuffer.raw, inBuffer.frameCount * mFrameSize); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1708 | mBufferQueue.add(pInBuffer); |
Andy Hung | 9d84af5 | 2018-09-12 18:03:44 -0700 | [diff] [blame] | 1709 | ALOGV("%s(%d): thread %d adding overflow buffer %zu", __func__, mId, |
| 1710 | (int)mThreadIoHandle, mBufferQueue.size()); |
Andy Hung | 1c86ebe | 2018-05-29 20:29:08 -0700 | [diff] [blame] | 1711 | // audio data is consumed (stored locally); set frameCount to 0. |
| 1712 | inBuffer.frameCount = 0; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1713 | } else { |
Andy Hung | 9d84af5 | 2018-09-12 18:03:44 -0700 | [diff] [blame] | 1714 | ALOGW("%s(%d): thread %d no more overflow buffers", |
| 1715 | __func__, mId, (int)mThreadIoHandle); |
Andy Hung | 1c86ebe | 2018-05-29 20:29:08 -0700 | [diff] [blame] | 1716 | // TODO: return error for this. |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1717 | } |
| 1718 | } |
| 1719 | } |
| 1720 | |
Andy Hung | c25b84a | 2015-01-14 19:04:10 -0800 | [diff] [blame] | 1721 | // Calling write() with a 0 length buffer means that no more data will be written: |
| 1722 | // We rely on stop() to set the appropriate flags to allow the remaining frames to play out. |
| 1723 | if (frames == 0 && mBufferQueue.size() == 0 && mActive) { |
| 1724 | stop(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1725 | } |
| 1726 | |
Andy Hung | 1c86ebe | 2018-05-29 20:29:08 -0700 | [diff] [blame] | 1727 | return frames - inBuffer.frameCount; // number of frames consumed. |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1728 | } |
| 1729 | |
Kevin Rocard | 1238109 | 2018-04-11 09:19:59 -0700 | [diff] [blame] | 1730 | void AudioFlinger::PlaybackThread::OutputTrack::copyMetadataTo(MetadataInserter& backInserter) const |
| 1731 | { |
| 1732 | std::lock_guard<std::mutex> lock(mTrackMetadatasMutex); |
| 1733 | backInserter = std::copy(mTrackMetadatas.begin(), mTrackMetadatas.end(), backInserter); |
| 1734 | } |
| 1735 | |
| 1736 | void AudioFlinger::PlaybackThread::OutputTrack::setMetadatas(const SourceMetadatas& metadatas) { |
| 1737 | { |
| 1738 | std::lock_guard<std::mutex> lock(mTrackMetadatasMutex); |
| 1739 | mTrackMetadatas = metadatas; |
| 1740 | } |
| 1741 | // No need to adjust metadata track volumes as OutputTrack volumes are always 0dBFS. |
| 1742 | setMetadataHasChanged(); |
| 1743 | } |
| 1744 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1745 | status_t AudioFlinger::PlaybackThread::OutputTrack::obtainBuffer( |
| 1746 | AudioBufferProvider::Buffer* buffer, uint32_t waitTimeMs) |
| 1747 | { |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 1748 | ClientProxy::Buffer buf; |
| 1749 | buf.mFrameCount = buffer->frameCount; |
| 1750 | struct timespec timeout; |
| 1751 | timeout.tv_sec = waitTimeMs / 1000; |
| 1752 | timeout.tv_nsec = (int) (waitTimeMs % 1000) * 1000000; |
| 1753 | status_t status = mClientProxy->obtainBuffer(&buf, &timeout); |
| 1754 | buffer->frameCount = buf.mFrameCount; |
| 1755 | buffer->raw = buf.mRaw; |
| 1756 | return status; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1757 | } |
| 1758 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1759 | void AudioFlinger::PlaybackThread::OutputTrack::clearBufferQueue() |
| 1760 | { |
| 1761 | size_t size = mBufferQueue.size(); |
| 1762 | |
| 1763 | for (size_t i = 0; i < size; i++) { |
| 1764 | Buffer *pBuffer = mBufferQueue.itemAt(i); |
Andy Hung | c25b84a | 2015-01-14 19:04:10 -0800 | [diff] [blame] | 1765 | free(pBuffer->mBuffer); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1766 | delete pBuffer; |
| 1767 | } |
| 1768 | mBufferQueue.clear(); |
| 1769 | } |
| 1770 | |
Eric Laurent | 4d231dc | 2016-03-11 18:38:23 -0800 | [diff] [blame] | 1771 | void AudioFlinger::PlaybackThread::OutputTrack::restartIfDisabled() |
| 1772 | { |
| 1773 | int32_t flags = android_atomic_and(~CBLK_DISABLED, &mCblk->mFlags); |
| 1774 | if (mActive && (flags & CBLK_DISABLED)) { |
| 1775 | start(); |
| 1776 | } |
| 1777 | } |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1778 | |
Andy Hung | 9d84af5 | 2018-09-12 18:03:44 -0700 | [diff] [blame] | 1779 | // ---------------------------------------------------------------------------- |
| 1780 | #undef LOG_TAG |
| 1781 | #define LOG_TAG "AF::PatchTrack" |
| 1782 | |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1783 | AudioFlinger::PlaybackThread::PatchTrack::PatchTrack(PlaybackThread *playbackThread, |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 1784 | audio_stream_type_t streamType, |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1785 | uint32_t sampleRate, |
| 1786 | audio_channel_mask_t channelMask, |
| 1787 | audio_format_t format, |
| 1788 | size_t frameCount, |
| 1789 | void *buffer, |
Andy Hung | 8fe6803 | 2017-06-05 16:17:51 -0700 | [diff] [blame] | 1790 | size_t bufferSize, |
Kevin Rocard | 45986c7 | 2018-12-18 18:22:59 -0800 | [diff] [blame] | 1791 | audio_output_flags_t flags, |
| 1792 | const Timeout& timeout) |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 1793 | : Track(playbackThread, NULL, streamType, |
Kevin Rocard | 1f564ac | 2018-03-29 13:53:10 -0700 | [diff] [blame] | 1794 | audio_attributes_t{} /* currently unused for patch track */, |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 1795 | sampleRate, format, channelMask, frameCount, |
Andy Hung | 8fe6803 | 2017-06-05 16:17:51 -0700 | [diff] [blame] | 1796 | buffer, bufferSize, nullptr /* sharedBuffer */, |
Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 1797 | AUDIO_SESSION_NONE, getpid(), AID_AUDIOSERVER, flags, TYPE_PATCH), |
Kevin Rocard | 45986c7 | 2018-12-18 18:22:59 -0800 | [diff] [blame] | 1798 | PatchTrackBase(new ClientProxy(mCblk, mBuffer, frameCount, mFrameSize, true, true), |
| 1799 | *playbackThread, timeout) |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1800 | { |
Andy Hung | 9d84af5 | 2018-09-12 18:03:44 -0700 | [diff] [blame] | 1801 | ALOGV("%s(%d): sampleRate %d mPeerTimeout %d.%03d sec", |
| 1802 | __func__, mId, sampleRate, |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1803 | (int)mPeerTimeout.tv_sec, |
| 1804 | (int)(mPeerTimeout.tv_nsec / 1000000)); |
| 1805 | } |
| 1806 | |
| 1807 | AudioFlinger::PlaybackThread::PatchTrack::~PatchTrack() |
| 1808 | { |
Andy Hung | abfab20 | 2019-03-07 19:45:54 -0800 | [diff] [blame] | 1809 | ALOGV("%s(%d)", __func__, mId); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1810 | } |
| 1811 | |
Eric Laurent | 4d231dc | 2016-03-11 18:38:23 -0800 | [diff] [blame] | 1812 | status_t AudioFlinger::PlaybackThread::PatchTrack::start(AudioSystem::sync_event_t event, |
Kevin Rocard | 5f2136e | 2018-05-11 22:03:00 -0700 | [diff] [blame] | 1813 | audio_session_t triggerSession) |
Eric Laurent | 4d231dc | 2016-03-11 18:38:23 -0800 | [diff] [blame] | 1814 | { |
| 1815 | status_t status = Track::start(event, triggerSession); |
| 1816 | if (status != NO_ERROR) { |
| 1817 | return status; |
| 1818 | } |
| 1819 | android_atomic_and(~CBLK_DISABLED, &mCblk->mFlags); |
| 1820 | return status; |
| 1821 | } |
| 1822 | |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1823 | // AudioBufferProvider interface |
| 1824 | status_t AudioFlinger::PlaybackThread::PatchTrack::getNextBuffer( |
Glenn Kasten | d79072e | 2016-01-06 08:41:20 -0800 | [diff] [blame] | 1825 | AudioBufferProvider::Buffer* buffer) |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1826 | { |
Andy Hung | 9d84af5 | 2018-09-12 18:03:44 -0700 | [diff] [blame] | 1827 | ALOG_ASSERT(mPeerProxy != 0, "%s(%d): called without peer proxy", __func__, mId); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1828 | Proxy::Buffer buf; |
| 1829 | buf.mFrameCount = buffer->frameCount; |
Mikhail Naganov | 938be41 | 2019-09-04 11:38:47 -0700 | [diff] [blame] | 1830 | if (ATRACE_ENABLED()) { |
| 1831 | std::string traceName("PTnReq"); |
| 1832 | traceName += std::to_string(id()); |
| 1833 | ATRACE_INT(traceName.c_str(), buf.mFrameCount); |
| 1834 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1835 | status_t status = mPeerProxy->obtainBuffer(&buf, &mPeerTimeout); |
Andy Hung | 9d84af5 | 2018-09-12 18:03:44 -0700 | [diff] [blame] | 1836 | 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] | 1837 | buffer->frameCount = buf.mFrameCount; |
Mikhail Naganov | 938be41 | 2019-09-04 11:38:47 -0700 | [diff] [blame] | 1838 | if (ATRACE_ENABLED()) { |
| 1839 | std::string traceName("PTnObt"); |
| 1840 | traceName += std::to_string(id()); |
| 1841 | ATRACE_INT(traceName.c_str(), buf.mFrameCount); |
| 1842 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1843 | if (buf.mFrameCount == 0) { |
| 1844 | return WOULD_BLOCK; |
| 1845 | } |
Glenn Kasten | d79072e | 2016-01-06 08:41:20 -0800 | [diff] [blame] | 1846 | status = Track::getNextBuffer(buffer); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1847 | return status; |
| 1848 | } |
| 1849 | |
| 1850 | void AudioFlinger::PlaybackThread::PatchTrack::releaseBuffer(AudioBufferProvider::Buffer* buffer) |
| 1851 | { |
Andy Hung | 9d84af5 | 2018-09-12 18:03:44 -0700 | [diff] [blame] | 1852 | ALOG_ASSERT(mPeerProxy != 0, "%s(%d): called without peer proxy", __func__, mId); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1853 | Proxy::Buffer buf; |
| 1854 | buf.mFrameCount = buffer->frameCount; |
| 1855 | buf.mRaw = buffer->raw; |
| 1856 | mPeerProxy->releaseBuffer(&buf); |
| 1857 | TrackBase::releaseBuffer(buffer); |
| 1858 | } |
| 1859 | |
| 1860 | status_t AudioFlinger::PlaybackThread::PatchTrack::obtainBuffer(Proxy::Buffer* buffer, |
| 1861 | const struct timespec *timeOut) |
| 1862 | { |
Eric Laurent | 4d231dc | 2016-03-11 18:38:23 -0800 | [diff] [blame] | 1863 | status_t status = NO_ERROR; |
| 1864 | static const int32_t kMaxTries = 5; |
| 1865 | int32_t tryCounter = kMaxTries; |
Andy Hung | f62e1a2 | 2018-05-08 18:32:11 -0700 | [diff] [blame] | 1866 | const size_t originalFrameCount = buffer->mFrameCount; |
Eric Laurent | 4d231dc | 2016-03-11 18:38:23 -0800 | [diff] [blame] | 1867 | do { |
| 1868 | if (status == NOT_ENOUGH_DATA) { |
| 1869 | restartIfDisabled(); |
Andy Hung | f62e1a2 | 2018-05-08 18:32:11 -0700 | [diff] [blame] | 1870 | buffer->mFrameCount = originalFrameCount; // cleared on error, must be restored. |
Eric Laurent | 4d231dc | 2016-03-11 18:38:23 -0800 | [diff] [blame] | 1871 | } |
| 1872 | status = mProxy->obtainBuffer(buffer, timeOut); |
| 1873 | } while ((status == NOT_ENOUGH_DATA) && (tryCounter-- > 0)); |
| 1874 | return status; |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1875 | } |
| 1876 | |
| 1877 | void AudioFlinger::PlaybackThread::PatchTrack::releaseBuffer(Proxy::Buffer* buffer) |
| 1878 | { |
| 1879 | mProxy->releaseBuffer(buffer); |
Eric Laurent | 4d231dc | 2016-03-11 18:38:23 -0800 | [diff] [blame] | 1880 | restartIfDisabled(); |
| 1881 | android_atomic_or(CBLK_FORCEREADY, &mCblk->mFlags); |
| 1882 | } |
| 1883 | |
| 1884 | void AudioFlinger::PlaybackThread::PatchTrack::restartIfDisabled() |
| 1885 | { |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1886 | if (android_atomic_and(~CBLK_DISABLED, &mCblk->mFlags) & CBLK_DISABLED) { |
Andy Hung | 9d84af5 | 2018-09-12 18:03:44 -0700 | [diff] [blame] | 1887 | ALOGW("%s(%d): disabled due to previous underrun, restarting", __func__, mId); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1888 | start(); |
| 1889 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 1890 | } |
| 1891 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1892 | // ---------------------------------------------------------------------------- |
| 1893 | // Record |
| 1894 | // ---------------------------------------------------------------------------- |
Jean-Michel Trivi | ddf87ef | 2019-08-20 15:42:04 -0700 | [diff] [blame] | 1895 | |
| 1896 | |
| 1897 | // ---------------------------------------------------------------------------- |
| 1898 | // AppOp for audio recording |
| 1899 | // ------------------------------- |
| 1900 | |
| 1901 | #undef LOG_TAG |
| 1902 | #define LOG_TAG "AF::OpRecordAudioMonitor" |
| 1903 | |
| 1904 | // static |
| 1905 | sp<AudioFlinger::RecordThread::OpRecordAudioMonitor> |
| 1906 | AudioFlinger::RecordThread::OpRecordAudioMonitor::createIfNeeded( |
| 1907 | uid_t uid, const String16& opPackageName) |
| 1908 | { |
| 1909 | if (isServiceUid(uid)) { |
| 1910 | ALOGV("not silencing record for service uid:%d pack:%s", |
| 1911 | uid, String8(opPackageName).string()); |
| 1912 | return nullptr; |
| 1913 | } |
| 1914 | |
| 1915 | if (opPackageName.size() == 0) { |
| 1916 | Vector<String16> packages; |
| 1917 | // no package name, happens with SL ES clients |
| 1918 | // query package manager to find one |
| 1919 | PermissionController permissionController; |
| 1920 | permissionController.getPackagesForUid(uid, packages); |
| 1921 | if (packages.isEmpty()) { |
| 1922 | return nullptr; |
| 1923 | } else { |
| 1924 | ALOGV("using pack:%s for uid:%d", String8(packages[0]).string(), uid); |
| 1925 | return new OpRecordAudioMonitor(uid, packages[0]); |
| 1926 | } |
| 1927 | } |
| 1928 | |
| 1929 | return new OpRecordAudioMonitor(uid, opPackageName); |
| 1930 | } |
| 1931 | |
| 1932 | AudioFlinger::RecordThread::OpRecordAudioMonitor::OpRecordAudioMonitor( |
| 1933 | uid_t uid, const String16& opPackageName) |
| 1934 | : mHasOpRecordAudio(true), mUid(uid), mPackage(opPackageName) |
| 1935 | { |
| 1936 | } |
| 1937 | |
| 1938 | AudioFlinger::RecordThread::OpRecordAudioMonitor::~OpRecordAudioMonitor() |
| 1939 | { |
| 1940 | if (mOpCallback != 0) { |
| 1941 | mAppOpsManager.stopWatchingMode(mOpCallback); |
| 1942 | } |
| 1943 | mOpCallback.clear(); |
| 1944 | } |
| 1945 | |
| 1946 | void AudioFlinger::RecordThread::OpRecordAudioMonitor::onFirstRef() |
| 1947 | { |
| 1948 | checkRecordAudio(); |
| 1949 | mOpCallback = new RecordAudioOpCallback(this); |
| 1950 | ALOGV("start watching OP_RECORD_AUDIO for pack:%s", String8(mPackage).string()); |
| 1951 | mAppOpsManager.startWatchingMode(AppOpsManager::OP_RECORD_AUDIO, mPackage, mOpCallback); |
| 1952 | } |
| 1953 | |
| 1954 | bool AudioFlinger::RecordThread::OpRecordAudioMonitor::hasOpRecordAudio() const { |
| 1955 | return mHasOpRecordAudio.load(); |
| 1956 | } |
| 1957 | |
| 1958 | // Called by RecordAudioOpCallback when OP_RECORD_AUDIO is updated in AppOp callback |
| 1959 | // and in onFirstRef() |
| 1960 | // Note this method is never called (and never to be) for audio server / root track |
| 1961 | // due to the UID in createIfNeeded(). As a result for those record track, it's: |
| 1962 | // - not called from constructor, |
| 1963 | // - not called from RecordAudioOpCallback because the callback is not installed in this case |
| 1964 | void AudioFlinger::RecordThread::OpRecordAudioMonitor::checkRecordAudio() |
| 1965 | { |
| 1966 | const int32_t mode = mAppOpsManager.checkOp(AppOpsManager::OP_RECORD_AUDIO, |
| 1967 | mUid, mPackage); |
| 1968 | const bool hasIt = (mode == AppOpsManager::MODE_ALLOWED); |
| 1969 | // verbose logging only log when appOp changed |
| 1970 | ALOGI_IF(hasIt != mHasOpRecordAudio.load(), |
| 1971 | "OP_RECORD_AUDIO missing, %ssilencing record uid%d pack:%s", |
| 1972 | hasIt ? "un" : "", mUid, String8(mPackage).string()); |
| 1973 | mHasOpRecordAudio.store(hasIt); |
| 1974 | } |
| 1975 | |
| 1976 | AudioFlinger::RecordThread::OpRecordAudioMonitor::RecordAudioOpCallback::RecordAudioOpCallback( |
| 1977 | const wp<OpRecordAudioMonitor>& monitor) : mMonitor(monitor) |
| 1978 | { } |
| 1979 | |
| 1980 | void AudioFlinger::RecordThread::OpRecordAudioMonitor::RecordAudioOpCallback::opChanged(int32_t op, |
| 1981 | const String16& packageName) { |
| 1982 | UNUSED(packageName); |
| 1983 | if (op != AppOpsManager::OP_RECORD_AUDIO) { |
| 1984 | return; |
| 1985 | } |
| 1986 | sp<OpRecordAudioMonitor> monitor = mMonitor.promote(); |
| 1987 | if (monitor != NULL) { |
| 1988 | monitor->checkRecordAudio(); |
| 1989 | } |
| 1990 | } |
| 1991 | |
| 1992 | |
| 1993 | |
Andy Hung | 9d84af5 | 2018-09-12 18:03:44 -0700 | [diff] [blame] | 1994 | #undef LOG_TAG |
| 1995 | #define LOG_TAG "AF::RecordHandle" |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 1996 | |
| 1997 | AudioFlinger::RecordHandle::RecordHandle( |
| 1998 | const sp<AudioFlinger::RecordThread::RecordTrack>& recordTrack) |
| 1999 | : BnAudioRecord(), |
| 2000 | mRecordTrack(recordTrack) |
| 2001 | { |
| 2002 | } |
| 2003 | |
| 2004 | AudioFlinger::RecordHandle::~RecordHandle() { |
| 2005 | stop_nonvirtual(); |
| 2006 | mRecordTrack->destroy(); |
| 2007 | } |
| 2008 | |
Ivan Lozano | ff6900d | 2017-08-01 15:47:38 -0700 | [diff] [blame] | 2009 | binder::Status AudioFlinger::RecordHandle::start(int /*AudioSystem::sync_event_t*/ event, |
| 2010 | int /*audio_session_t*/ triggerSession) { |
Andy Hung | 9d84af5 | 2018-09-12 18:03:44 -0700 | [diff] [blame] | 2011 | ALOGV("%s()", __func__); |
Ivan Lozano | ff6900d | 2017-08-01 15:47:38 -0700 | [diff] [blame] | 2012 | return binder::Status::fromStatusT( |
| 2013 | mRecordTrack->start((AudioSystem::sync_event_t)event, (audio_session_t) triggerSession)); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2014 | } |
| 2015 | |
Ivan Lozano | ff6900d | 2017-08-01 15:47:38 -0700 | [diff] [blame] | 2016 | binder::Status AudioFlinger::RecordHandle::stop() { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2017 | stop_nonvirtual(); |
Ivan Lozano | ff6900d | 2017-08-01 15:47:38 -0700 | [diff] [blame] | 2018 | return binder::Status::ok(); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2019 | } |
| 2020 | |
| 2021 | void AudioFlinger::RecordHandle::stop_nonvirtual() { |
Andy Hung | 9d84af5 | 2018-09-12 18:03:44 -0700 | [diff] [blame] | 2022 | ALOGV("%s()", __func__); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2023 | mRecordTrack->stop(); |
| 2024 | } |
| 2025 | |
jiabin | 653cc0a | 2018-01-17 17:54:10 -0800 | [diff] [blame] | 2026 | binder::Status AudioFlinger::RecordHandle::getActiveMicrophones( |
| 2027 | std::vector<media::MicrophoneInfo>* activeMicrophones) { |
Andy Hung | 9d84af5 | 2018-09-12 18:03:44 -0700 | [diff] [blame] | 2028 | ALOGV("%s()", __func__); |
jiabin | 653cc0a | 2018-01-17 17:54:10 -0800 | [diff] [blame] | 2029 | return binder::Status::fromStatusT( |
| 2030 | mRecordTrack->getActiveMicrophones(activeMicrophones)); |
| 2031 | } |
| 2032 | |
Paul McLean | 1234008 | 2019-03-19 09:35:05 -0600 | [diff] [blame] | 2033 | binder::Status AudioFlinger::RecordHandle::setPreferredMicrophoneDirection( |
Paul McLean | 03a6e6a | 2018-12-04 10:54:13 -0700 | [diff] [blame] | 2034 | int /*audio_microphone_direction_t*/ direction) { |
| 2035 | ALOGV("%s()", __func__); |
Paul McLean | 1234008 | 2019-03-19 09:35:05 -0600 | [diff] [blame] | 2036 | return binder::Status::fromStatusT(mRecordTrack->setPreferredMicrophoneDirection( |
Paul McLean | 03a6e6a | 2018-12-04 10:54:13 -0700 | [diff] [blame] | 2037 | static_cast<audio_microphone_direction_t>(direction))); |
| 2038 | } |
| 2039 | |
Paul McLean | 1234008 | 2019-03-19 09:35:05 -0600 | [diff] [blame] | 2040 | binder::Status AudioFlinger::RecordHandle::setPreferredMicrophoneFieldDimension(float zoom) { |
Paul McLean | 03a6e6a | 2018-12-04 10:54:13 -0700 | [diff] [blame] | 2041 | ALOGV("%s()", __func__); |
Paul McLean | 1234008 | 2019-03-19 09:35:05 -0600 | [diff] [blame] | 2042 | return binder::Status::fromStatusT(mRecordTrack->setPreferredMicrophoneFieldDimension(zoom)); |
Paul McLean | 03a6e6a | 2018-12-04 10:54:13 -0700 | [diff] [blame] | 2043 | } |
| 2044 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2045 | // ---------------------------------------------------------------------------- |
Andy Hung | 9d84af5 | 2018-09-12 18:03:44 -0700 | [diff] [blame] | 2046 | #undef LOG_TAG |
| 2047 | #define LOG_TAG "AF::RecordTrack" |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2048 | |
Glenn Kasten | 05997e2 | 2014-03-13 15:08:33 -0700 | [diff] [blame] | 2049 | // RecordTrack constructor must be called with AudioFlinger::mLock and ThreadBase::mLock held |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2050 | AudioFlinger::RecordThread::RecordTrack::RecordTrack( |
| 2051 | RecordThread *thread, |
| 2052 | const sp<Client>& client, |
Kevin Rocard | 1f564ac | 2018-03-29 13:53:10 -0700 | [diff] [blame] | 2053 | const audio_attributes_t& attr, |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2054 | uint32_t sampleRate, |
| 2055 | audio_format_t format, |
| 2056 | audio_channel_mask_t channelMask, |
| 2057 | size_t frameCount, |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2058 | void *buffer, |
Andy Hung | 8fe6803 | 2017-06-05 16:17:51 -0700 | [diff] [blame] | 2059 | size_t bufferSize, |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 2060 | audio_session_t sessionId, |
Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 2061 | pid_t creatorPid, |
Andy Hung | 1f12a8a | 2016-11-07 16:10:30 -0800 | [diff] [blame] | 2062 | uid_t uid, |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 2063 | audio_input_flags_t flags, |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 2064 | track_type type, |
Jean-Michel Trivi | ddf87ef | 2019-08-20 15:42:04 -0700 | [diff] [blame] | 2065 | const String16& opPackageName, |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 2066 | audio_port_handle_t portId) |
Kevin Rocard | 1f564ac | 2018-03-29 13:53:10 -0700 | [diff] [blame] | 2067 | : TrackBase(thread, client, attr, sampleRate, format, |
Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 2068 | channelMask, frameCount, buffer, bufferSize, sessionId, |
| 2069 | creatorPid, uid, false /*isOut*/, |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2070 | (type == TYPE_DEFAULT) ? |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 2071 | ((flags & AUDIO_INPUT_FLAG_FAST) ? ALLOC_PIPE : ALLOC_CBLK) : |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2072 | ((buffer == NULL) ? ALLOC_LOCAL : ALLOC_NONE), |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 2073 | type, portId), |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 2074 | mOverflow(false), |
Andy Hung | 4c6afaf | 2015-06-12 18:23:35 -0700 | [diff] [blame] | 2075 | mFramesToDrop(0), |
| 2076 | mResamplerBufferProvider(NULL), // initialize in case of early constructor exit |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 2077 | mRecordBufferConverter(NULL), |
jiabin | 9378eb9 | 2018-05-02 15:26:35 -0700 | [diff] [blame] | 2078 | mFlags(flags), |
Jean-Michel Trivi | ddf87ef | 2019-08-20 15:42:04 -0700 | [diff] [blame] | 2079 | mSilenced(false), |
| 2080 | mOpRecordAudioMonitor(OpRecordAudioMonitor::createIfNeeded(uid, opPackageName)) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2081 | { |
Glenn Kasten | 3ef14ef | 2014-03-13 15:08:51 -0700 | [diff] [blame] | 2082 | if (mCblk == NULL) { |
| 2083 | return; |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 2084 | } |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 2085 | |
Mikhail Naganov | 7c6ae98 | 2018-06-14 12:33:38 -0700 | [diff] [blame] | 2086 | if (!isDirect()) { |
| 2087 | mRecordBufferConverter = new RecordBufferConverter( |
| 2088 | thread->mChannelMask, thread->mFormat, thread->mSampleRate, |
| 2089 | channelMask, format, sampleRate); |
| 2090 | // Check if the RecordBufferConverter construction was successful. |
| 2091 | // If not, don't continue with construction. |
| 2092 | // |
| 2093 | // NOTE: It would be extremely rare that the record track cannot be created |
| 2094 | // for the current device, but a pending or future device change would make |
| 2095 | // the record track configuration valid. |
| 2096 | if (mRecordBufferConverter->initCheck() != NO_ERROR) { |
Andy Hung | 9d84af5 | 2018-09-12 18:03:44 -0700 | [diff] [blame] | 2097 | ALOGE("%s(%d): RecordTrack unable to create record buffer converter", __func__, mId); |
Mikhail Naganov | 7c6ae98 | 2018-06-14 12:33:38 -0700 | [diff] [blame] | 2098 | return; |
| 2099 | } |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 2100 | } |
| 2101 | |
Andy Hung | 6ae5843 | 2016-02-16 18:32:24 -0800 | [diff] [blame] | 2102 | mServerProxy = new AudioRecordServerProxy(mCblk, mBuffer, frameCount, |
Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 2103 | mFrameSize, !isExternalTrack()); |
Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 2104 | |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 2105 | mResamplerBufferProvider = new ResamplerBufferProvider(this); |
Glenn Kasten | c263ca0 | 2014-06-04 20:31:46 -0700 | [diff] [blame] | 2106 | |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 2107 | if (flags & AUDIO_INPUT_FLAG_FAST) { |
Glenn Kasten | c263ca0 | 2014-06-04 20:31:46 -0700 | [diff] [blame] | 2108 | ALOG_ASSERT(thread->mFastTrackAvail); |
| 2109 | thread->mFastTrackAvail = false; |
Andy Hung | 000adb5 | 2018-06-01 15:43:26 -0700 | [diff] [blame] | 2110 | } else { |
| 2111 | // TODO: only Normal Record has timestamps (Fast Record does not). |
Andy Hung | 5d3d956 | 2018-10-04 19:27:26 -0700 | [diff] [blame] | 2112 | mServerLatencySupported = checkServerLatencySupported(mFormat, flags); |
Glenn Kasten | c263ca0 | 2014-06-04 20:31:46 -0700 | [diff] [blame] | 2113 | } |
Andy Hung | 8946a28 | 2018-04-19 20:04:56 -0700 | [diff] [blame] | 2114 | #ifdef TEE_SINK |
| 2115 | mTee.setId(std::string("_") + std::to_string(mThreadIoHandle) |
| 2116 | + "_" + std::to_string(mId) |
| 2117 | + "_R"); |
| 2118 | #endif |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2119 | } |
| 2120 | |
| 2121 | AudioFlinger::RecordThread::RecordTrack::~RecordTrack() |
| 2122 | { |
Andy Hung | 9d84af5 | 2018-09-12 18:03:44 -0700 | [diff] [blame] | 2123 | ALOGV("%s()", __func__); |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 2124 | delete mRecordBufferConverter; |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 2125 | delete mResamplerBufferProvider; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2126 | } |
| 2127 | |
Andy Hung | 97a893e | 2015-03-29 01:03:07 -0700 | [diff] [blame] | 2128 | status_t AudioFlinger::RecordThread::RecordTrack::initCheck() const |
| 2129 | { |
| 2130 | status_t status = TrackBase::initCheck(); |
| 2131 | if (status == NO_ERROR && mServerProxy == 0) { |
| 2132 | status = BAD_VALUE; |
| 2133 | } |
| 2134 | return status; |
| 2135 | } |
| 2136 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2137 | // AudioBufferProvider interface |
Glenn Kasten | d79072e | 2016-01-06 08:41:20 -0800 | [diff] [blame] | 2138 | status_t AudioFlinger::RecordThread::RecordTrack::getNextBuffer(AudioBufferProvider::Buffer* buffer) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2139 | { |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 2140 | ServerProxy::Buffer buf; |
| 2141 | buf.mFrameCount = buffer->frameCount; |
| 2142 | status_t status = mServerProxy->obtainBuffer(&buf); |
| 2143 | buffer->frameCount = buf.mFrameCount; |
| 2144 | buffer->raw = buf.mRaw; |
| 2145 | if (buf.mFrameCount == 0) { |
| 2146 | // FIXME also wake futex so that overrun is noticed more quickly |
Glenn Kasten | 96f60d8 | 2013-07-12 10:21:18 -0700 | [diff] [blame] | 2147 | (void) android_atomic_or(CBLK_OVERRUN, &mCblk->mFlags); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2148 | } |
Glenn Kasten | 9f80dd2 | 2012-12-18 15:57:32 -0800 | [diff] [blame] | 2149 | return status; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2150 | } |
| 2151 | |
| 2152 | status_t AudioFlinger::RecordThread::RecordTrack::start(AudioSystem::sync_event_t event, |
Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 2153 | audio_session_t triggerSession) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2154 | { |
| 2155 | sp<ThreadBase> thread = mThread.promote(); |
| 2156 | if (thread != 0) { |
| 2157 | RecordThread *recordThread = (RecordThread *)thread.get(); |
| 2158 | return recordThread->start(this, event, triggerSession); |
| 2159 | } else { |
| 2160 | return BAD_VALUE; |
| 2161 | } |
| 2162 | } |
| 2163 | |
| 2164 | void AudioFlinger::RecordThread::RecordTrack::stop() |
| 2165 | { |
| 2166 | sp<ThreadBase> thread = mThread.promote(); |
| 2167 | if (thread != 0) { |
| 2168 | RecordThread *recordThread = (RecordThread *)thread.get(); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2169 | if (recordThread->stop(this) && isExternalTrack()) { |
Eric Laurent | fee1976 | 2018-01-29 18:44:13 -0800 | [diff] [blame] | 2170 | AudioSystem::stopInput(mPortId); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2171 | } |
| 2172 | } |
| 2173 | } |
| 2174 | |
| 2175 | void AudioFlinger::RecordThread::RecordTrack::destroy() |
| 2176 | { |
| 2177 | // see comments at AudioFlinger::PlaybackThread::Track::destroy() |
| 2178 | sp<RecordTrack> keep(this); |
| 2179 | { |
Andy Hung | ce68540 | 2018-10-05 17:23:27 -0700 | [diff] [blame] | 2180 | track_state priorState = mState; |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2181 | sp<ThreadBase> thread = mThread.promote(); |
| 2182 | if (thread != 0) { |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2183 | Mutex::Autolock _l(thread->mLock); |
| 2184 | RecordThread *recordThread = (RecordThread *) thread.get(); |
Andy Hung | ce68540 | 2018-10-05 17:23:27 -0700 | [diff] [blame] | 2185 | priorState = mState; |
| 2186 | recordThread->destroyTrack_l(this); // move mState to STOPPED, terminate |
| 2187 | } |
| 2188 | // APM portid/client management done outside of lock. |
| 2189 | // NOTE: if thread doesn't exist, the input descriptor probably doesn't either. |
| 2190 | if (isExternalTrack()) { |
| 2191 | switch (priorState) { |
| 2192 | case ACTIVE: // invalidated while still active |
| 2193 | case STARTING_2: // invalidated/start-aborted after startInput successfully called |
| 2194 | case PAUSING: // invalidated while in the middle of stop() pausing (still active) |
| 2195 | AudioSystem::stopInput(mPortId); |
| 2196 | break; |
| 2197 | |
| 2198 | case STARTING_1: // invalidated/start-aborted and startInput not successful |
| 2199 | case PAUSED: // OK, not active |
| 2200 | case IDLE: // OK, not active |
| 2201 | break; |
| 2202 | |
| 2203 | case STOPPED: // unexpected (destroyed) |
| 2204 | default: |
| 2205 | LOG_ALWAYS_FATAL("%s(%d): invalid prior state: %d", __func__, mId, priorState); |
| 2206 | } |
| 2207 | AudioSystem::releaseInput(mPortId); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2208 | } |
| 2209 | } |
| 2210 | } |
| 2211 | |
Eric Laurent | 9a54bc2 | 2013-09-09 09:08:44 -0700 | [diff] [blame] | 2212 | void AudioFlinger::RecordThread::RecordTrack::invalidate() |
| 2213 | { |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2214 | TrackBase::invalidate(); |
Eric Laurent | 9a54bc2 | 2013-09-09 09:08:44 -0700 | [diff] [blame] | 2215 | // FIXME should use proxy, and needs work |
| 2216 | audio_track_cblk_t* cblk = mCblk; |
| 2217 | android_atomic_or(CBLK_INVALID, &cblk->mFlags); |
| 2218 | android_atomic_release_store(0x40000000, &cblk->mFutex); |
| 2219 | // 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] | 2220 | (void) syscall(__NR_futex, &cblk->mFutex, FUTEX_WAKE, INT_MAX); |
Eric Laurent | 9a54bc2 | 2013-09-09 09:08:44 -0700 | [diff] [blame] | 2221 | } |
| 2222 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2223 | |
Andy Hung | 000adb5 | 2018-06-01 15:43:26 -0700 | [diff] [blame] | 2224 | void AudioFlinger::RecordThread::RecordTrack::appendDumpHeader(String8& result) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2225 | { |
Eric Laurent | 973db02 | 2018-11-20 14:54:31 -0800 | [diff] [blame] | 2226 | result.appendFormat("Active Id Client Session Port Id S Flags " |
Andy Hung | 9d84af5 | 2018-09-12 18:03:44 -0700 | [diff] [blame] | 2227 | " Format Chn mask SRate Source " |
Kevin Rocard | 5f2136e | 2018-05-11 22:03:00 -0700 | [diff] [blame] | 2228 | " Server FrmCnt FrmRdy Sil%s\n", |
| 2229 | isServerLatencySupported() ? " Latency" : ""); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2230 | } |
| 2231 | |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 2232 | void AudioFlinger::RecordThread::RecordTrack::appendDump(String8& result, bool active) |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2233 | { |
Eric Laurent | 973db02 | 2018-11-20 14:54:31 -0800 | [diff] [blame] | 2234 | result.appendFormat("%c%5s %6d %6u %7u %7u %2s 0x%03X " |
Kevin Rocard | 5f2136e | 2018-05-11 22:03:00 -0700 | [diff] [blame] | 2235 | "%08X %08X %6u %6X " |
Andy Hung | 000adb5 | 2018-06-01 15:43:26 -0700 | [diff] [blame] | 2236 | "%08X %6zu %6zu %3c", |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 2237 | isFastTrack() ? 'F' : ' ', |
Marco Nelissen | b220884 | 2014-02-07 14:00:50 -0800 | [diff] [blame] | 2238 | active ? "yes" : "no", |
Andy Hung | 9d84af5 | 2018-09-12 18:03:44 -0700 | [diff] [blame] | 2239 | mId, |
Andy Hung | 4ef19fa | 2018-05-15 19:35:29 -0700 | [diff] [blame] | 2240 | (mClient == 0) ? getpid() : mClient->pid(), |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 2241 | mSessionId, |
Eric Laurent | 973db02 | 2018-11-20 14:54:31 -0800 | [diff] [blame] | 2242 | mPortId, |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 2243 | getTrackStateString(), |
| 2244 | mCblk->mFlags, |
| 2245 | |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2246 | mFormat, |
| 2247 | mChannelMask, |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 2248 | mSampleRate, |
Kevin Rocard | 5f2136e | 2018-05-11 22:03:00 -0700 | [diff] [blame] | 2249 | mAttr.source, |
Glenn Kasten | 6dd62fb | 2013-12-05 16:35:58 -0800 | [diff] [blame] | 2250 | |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 2251 | mCblk->mServer, |
Jean-Michel Trivi | 7d665ab | 2018-04-11 17:26:51 -0700 | [diff] [blame] | 2252 | mFrameCount, |
Andy Hung | 000adb5 | 2018-06-01 15:43:26 -0700 | [diff] [blame] | 2253 | mServerProxy->framesReadySafe(), |
Jean-Michel Trivi | 7d665ab | 2018-04-11 17:26:51 -0700 | [diff] [blame] | 2254 | isSilenced() ? 's' : 'n' |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 2255 | ); |
Andy Hung | 000adb5 | 2018-06-01 15:43:26 -0700 | [diff] [blame] | 2256 | if (isServerLatencySupported()) { |
| 2257 | double latencyMs; |
| 2258 | bool fromTrack; |
| 2259 | if (getTrackLatencyMs(&latencyMs, &fromTrack) == OK) { |
| 2260 | // Show latency in msec, followed by 't' if from track timestamp (the most accurate) |
| 2261 | // or 'k' if estimated from kernel (usually for debugging). |
| 2262 | result.appendFormat(" %7.2lf %c", latencyMs, fromTrack ? 't' : 'k'); |
| 2263 | } else { |
| 2264 | result.appendFormat("%10s", mCblk->mServer != 0 ? "unavail" : "new"); |
| 2265 | } |
| 2266 | } |
| 2267 | result.append("\n"); |
Eric Laurent | 81784c3 | 2012-11-19 14:55:58 -0800 | [diff] [blame] | 2268 | } |
| 2269 | |
Glenn Kasten | 25f4aa8 | 2014-02-07 10:50:43 -0800 | [diff] [blame] | 2270 | void AudioFlinger::RecordThread::RecordTrack::handleSyncStartEvent(const sp<SyncEvent>& event) |
| 2271 | { |
| 2272 | if (event == mSyncStartEvent) { |
| 2273 | ssize_t framesToDrop = 0; |
| 2274 | sp<ThreadBase> threadBase = mThread.promote(); |
| 2275 | if (threadBase != 0) { |
| 2276 | // TODO: use actual buffer filling status instead of 2 buffers when info is available |
| 2277 | // from audio HAL |
| 2278 | framesToDrop = threadBase->mFrameCount * 2; |
| 2279 | } |
| 2280 | mFramesToDrop = framesToDrop; |
| 2281 | } |
| 2282 | } |
| 2283 | |
| 2284 | void AudioFlinger::RecordThread::RecordTrack::clearSyncStartEvent() |
| 2285 | { |
| 2286 | if (mSyncStartEvent != 0) { |
| 2287 | mSyncStartEvent->cancel(); |
| 2288 | mSyncStartEvent.clear(); |
| 2289 | } |
| 2290 | mFramesToDrop = 0; |
| 2291 | } |
| 2292 | |
Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 2293 | void AudioFlinger::RecordThread::RecordTrack::updateTrackFrameInfo( |
| 2294 | int64_t trackFramesReleased, int64_t sourceFramesRead, |
| 2295 | uint32_t halSampleRate, const ExtendedTimestamp ×tamp) |
| 2296 | { |
Andy Hung | 3028256 | 2018-08-08 18:27:03 -0700 | [diff] [blame] | 2297 | // Make the kernel frametime available. |
| 2298 | const FrameTime ft{ |
| 2299 | timestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL], |
| 2300 | timestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL]}; |
| 2301 | // ALOGD("FrameTime: %lld %lld", (long long)ft.frames, (long long)ft.timeNs); |
| 2302 | mKernelFrameTime.store(ft); |
| 2303 | if (!audio_is_linear_pcm(mFormat)) { |
| 2304 | return; |
| 2305 | } |
| 2306 | |
Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 2307 | ExtendedTimestamp local = timestamp; |
| 2308 | |
| 2309 | // Convert HAL frames to server-side track frames at track sample rate. |
| 2310 | // We use trackFramesReleased and sourceFramesRead as an anchor point. |
| 2311 | for (int i = ExtendedTimestamp::LOCATION_SERVER; i < ExtendedTimestamp::LOCATION_MAX; ++i) { |
| 2312 | if (local.mTimeNs[i] != 0) { |
| 2313 | const int64_t relativeServerFrames = local.mPosition[i] - sourceFramesRead; |
| 2314 | const int64_t relativeTrackFrames = relativeServerFrames |
| 2315 | * mSampleRate / halSampleRate; // TODO: potential computation overflow |
| 2316 | local.mPosition[i] = relativeTrackFrames + trackFramesReleased; |
| 2317 | } |
| 2318 | } |
Andy Hung | 6ae5843 | 2016-02-16 18:32:24 -0800 | [diff] [blame] | 2319 | mServerProxy->setTimestamp(local); |
Andy Hung | 000adb5 | 2018-06-01 15:43:26 -0700 | [diff] [blame] | 2320 | |
| 2321 | // Compute latency info. |
| 2322 | const bool useTrackTimestamp = true; // use track unless debugging. |
| 2323 | const double latencyMs = - (useTrackTimestamp |
| 2324 | ? local.getOutputServerLatencyMs(sampleRate()) |
| 2325 | : timestamp.getOutputServerLatencyMs(halSampleRate)); |
| 2326 | |
| 2327 | mServerLatencyFromTrack.store(useTrackTimestamp); |
| 2328 | mServerLatencyMs.store(latencyMs); |
Andy Hung | 3f0c902 | 2016-01-15 17:49:46 -0800 | [diff] [blame] | 2329 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2330 | |
Jean-Michel Trivi | ddf87ef | 2019-08-20 15:42:04 -0700 | [diff] [blame] | 2331 | bool AudioFlinger::RecordThread::RecordTrack::isSilenced() const { |
| 2332 | if (mSilenced) { |
| 2333 | return true; |
| 2334 | } |
| 2335 | // The monitor is only created for record tracks that can be silenced. |
| 2336 | return mOpRecordAudioMonitor ? !mOpRecordAudioMonitor->hasOpRecordAudio() : false; |
| 2337 | } |
| 2338 | |
jiabin | 653cc0a | 2018-01-17 17:54:10 -0800 | [diff] [blame] | 2339 | status_t AudioFlinger::RecordThread::RecordTrack::getActiveMicrophones( |
| 2340 | std::vector<media::MicrophoneInfo>* activeMicrophones) |
| 2341 | { |
| 2342 | sp<ThreadBase> thread = mThread.promote(); |
| 2343 | if (thread != 0) { |
| 2344 | RecordThread *recordThread = (RecordThread *)thread.get(); |
| 2345 | return recordThread->getActiveMicrophones(activeMicrophones); |
| 2346 | } else { |
| 2347 | return BAD_VALUE; |
| 2348 | } |
| 2349 | } |
| 2350 | |
Paul McLean | 1234008 | 2019-03-19 09:35:05 -0600 | [diff] [blame] | 2351 | status_t AudioFlinger::RecordThread::RecordTrack::setPreferredMicrophoneDirection( |
Paul McLean | 03a6e6a | 2018-12-04 10:54:13 -0700 | [diff] [blame] | 2352 | audio_microphone_direction_t direction) { |
| 2353 | sp<ThreadBase> thread = mThread.promote(); |
| 2354 | if (thread != 0) { |
| 2355 | RecordThread *recordThread = (RecordThread *)thread.get(); |
Paul McLean | 1234008 | 2019-03-19 09:35:05 -0600 | [diff] [blame] | 2356 | return recordThread->setPreferredMicrophoneDirection(direction); |
Paul McLean | 03a6e6a | 2018-12-04 10:54:13 -0700 | [diff] [blame] | 2357 | } else { |
| 2358 | return BAD_VALUE; |
| 2359 | } |
| 2360 | } |
| 2361 | |
Paul McLean | 1234008 | 2019-03-19 09:35:05 -0600 | [diff] [blame] | 2362 | status_t AudioFlinger::RecordThread::RecordTrack::setPreferredMicrophoneFieldDimension(float zoom) { |
Paul McLean | 03a6e6a | 2018-12-04 10:54:13 -0700 | [diff] [blame] | 2363 | sp<ThreadBase> thread = mThread.promote(); |
| 2364 | if (thread != 0) { |
| 2365 | RecordThread *recordThread = (RecordThread *)thread.get(); |
Paul McLean | 1234008 | 2019-03-19 09:35:05 -0600 | [diff] [blame] | 2366 | return recordThread->setPreferredMicrophoneFieldDimension(zoom); |
Paul McLean | 03a6e6a | 2018-12-04 10:54:13 -0700 | [diff] [blame] | 2367 | } else { |
| 2368 | return BAD_VALUE; |
| 2369 | } |
| 2370 | } |
| 2371 | |
Andy Hung | 9d84af5 | 2018-09-12 18:03:44 -0700 | [diff] [blame] | 2372 | // ---------------------------------------------------------------------------- |
| 2373 | #undef LOG_TAG |
| 2374 | #define LOG_TAG "AF::PatchRecord" |
| 2375 | |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2376 | AudioFlinger::RecordThread::PatchRecord::PatchRecord(RecordThread *recordThread, |
| 2377 | uint32_t sampleRate, |
| 2378 | audio_channel_mask_t channelMask, |
| 2379 | audio_format_t format, |
| 2380 | size_t frameCount, |
| 2381 | void *buffer, |
Andy Hung | 8fe6803 | 2017-06-05 16:17:51 -0700 | [diff] [blame] | 2382 | size_t bufferSize, |
Kevin Rocard | 45986c7 | 2018-12-18 18:22:59 -0800 | [diff] [blame] | 2383 | audio_input_flags_t flags, |
| 2384 | const Timeout& timeout) |
Kevin Rocard | 1f564ac | 2018-03-29 13:53:10 -0700 | [diff] [blame] | 2385 | : RecordTrack(recordThread, NULL, |
| 2386 | audio_attributes_t{} /* currently unused for patch track */, |
| 2387 | sampleRate, format, channelMask, frameCount, |
Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 2388 | buffer, bufferSize, AUDIO_SESSION_NONE, getpid(), AID_AUDIOSERVER, |
Jean-Michel Trivi | ddf87ef | 2019-08-20 15:42:04 -0700 | [diff] [blame] | 2389 | flags, TYPE_PATCH, String16()), |
Kevin Rocard | 45986c7 | 2018-12-18 18:22:59 -0800 | [diff] [blame] | 2390 | PatchTrackBase(new ClientProxy(mCblk, mBuffer, frameCount, mFrameSize, false, true), |
| 2391 | *recordThread, timeout) |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2392 | { |
Andy Hung | 9d84af5 | 2018-09-12 18:03:44 -0700 | [diff] [blame] | 2393 | ALOGV("%s(%d): sampleRate %d mPeerTimeout %d.%03d sec", |
| 2394 | __func__, mId, sampleRate, |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2395 | (int)mPeerTimeout.tv_sec, |
| 2396 | (int)(mPeerTimeout.tv_nsec / 1000000)); |
| 2397 | } |
| 2398 | |
| 2399 | AudioFlinger::RecordThread::PatchRecord::~PatchRecord() |
| 2400 | { |
Andy Hung | abfab20 | 2019-03-07 19:45:54 -0800 | [diff] [blame] | 2401 | ALOGV("%s(%d)", __func__, mId); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2402 | } |
| 2403 | |
| 2404 | // AudioBufferProvider interface |
| 2405 | status_t AudioFlinger::RecordThread::PatchRecord::getNextBuffer( |
Glenn Kasten | d79072e | 2016-01-06 08:41:20 -0800 | [diff] [blame] | 2406 | AudioBufferProvider::Buffer* buffer) |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2407 | { |
Andy Hung | 9d84af5 | 2018-09-12 18:03:44 -0700 | [diff] [blame] | 2408 | ALOG_ASSERT(mPeerProxy != 0, "%s(%d): called without peer proxy", __func__, mId); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2409 | Proxy::Buffer buf; |
| 2410 | buf.mFrameCount = buffer->frameCount; |
| 2411 | status_t status = mPeerProxy->obtainBuffer(&buf, &mPeerTimeout); |
| 2412 | ALOGV_IF(status != NO_ERROR, |
Andy Hung | 9d84af5 | 2018-09-12 18:03:44 -0700 | [diff] [blame] | 2413 | "%s(%d): mPeerProxy->obtainBuffer status %d", __func__, mId, status); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 2414 | buffer->frameCount = buf.mFrameCount; |
Mikhail Naganov | 938be41 | 2019-09-04 11:38:47 -0700 | [diff] [blame] | 2415 | if (ATRACE_ENABLED()) { |
| 2416 | std::string traceName("PRnObt"); |
| 2417 | traceName += std::to_string(id()); |
| 2418 | ATRACE_INT(traceName.c_str(), buf.mFrameCount); |
| 2419 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2420 | if (buf.mFrameCount == 0) { |
| 2421 | return WOULD_BLOCK; |
| 2422 | } |
Glenn Kasten | d79072e | 2016-01-06 08:41:20 -0800 | [diff] [blame] | 2423 | status = RecordTrack::getNextBuffer(buffer); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2424 | return status; |
| 2425 | } |
| 2426 | |
| 2427 | void AudioFlinger::RecordThread::PatchRecord::releaseBuffer(AudioBufferProvider::Buffer* buffer) |
| 2428 | { |
Andy Hung | 9d84af5 | 2018-09-12 18:03:44 -0700 | [diff] [blame] | 2429 | ALOG_ASSERT(mPeerProxy != 0, "%s(%d): called without peer proxy", __func__, mId); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2430 | Proxy::Buffer buf; |
| 2431 | buf.mFrameCount = buffer->frameCount; |
| 2432 | buf.mRaw = buffer->raw; |
| 2433 | mPeerProxy->releaseBuffer(&buf); |
| 2434 | TrackBase::releaseBuffer(buffer); |
| 2435 | } |
| 2436 | |
| 2437 | status_t AudioFlinger::RecordThread::PatchRecord::obtainBuffer(Proxy::Buffer* buffer, |
| 2438 | const struct timespec *timeOut) |
| 2439 | { |
| 2440 | return mProxy->obtainBuffer(buffer, timeOut); |
| 2441 | } |
| 2442 | |
| 2443 | void AudioFlinger::RecordThread::PatchRecord::releaseBuffer(Proxy::Buffer* buffer) |
| 2444 | { |
| 2445 | mProxy->releaseBuffer(buffer); |
| 2446 | } |
| 2447 | |
Andy Hung | 9d84af5 | 2018-09-12 18:03:44 -0700 | [diff] [blame] | 2448 | // ---------------------------------------------------------------------------- |
| 2449 | #undef LOG_TAG |
| 2450 | #define LOG_TAG "AF::MmapTrack" |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2451 | |
| 2452 | AudioFlinger::MmapThread::MmapTrack::MmapTrack(ThreadBase *thread, |
Kevin Rocard | 1f564ac | 2018-03-29 13:53:10 -0700 | [diff] [blame] | 2453 | const audio_attributes_t& attr, |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2454 | uint32_t sampleRate, |
| 2455 | audio_format_t format, |
| 2456 | audio_channel_mask_t channelMask, |
| 2457 | audio_session_t sessionId, |
Kevin Rocard | 5f2136e | 2018-05-11 22:03:00 -0700 | [diff] [blame] | 2458 | bool isOut, |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2459 | uid_t uid, |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 2460 | pid_t pid, |
Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 2461 | pid_t creatorPid, |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2462 | audio_port_handle_t portId) |
Kevin Rocard | 1f564ac | 2018-03-29 13:53:10 -0700 | [diff] [blame] | 2463 | : TrackBase(thread, NULL, attr, sampleRate, format, |
Andy Hung | 8fe6803 | 2017-06-05 16:17:51 -0700 | [diff] [blame] | 2464 | channelMask, (size_t)0 /* frameCount */, |
| 2465 | nullptr /* buffer */, (size_t)0 /* bufferSize */, |
Eric Laurent | 09f1ed2 | 2019-04-24 17:45:17 -0700 | [diff] [blame] | 2466 | sessionId, creatorPid, uid, isOut, |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2467 | ALLOC_NONE, |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 2468 | TYPE_DEFAULT, portId), |
Eric Laurent | 331679c | 2018-04-16 17:03:16 -0700 | [diff] [blame] | 2469 | mPid(pid), mSilenced(false), mSilencedNotified(false) |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2470 | { |
| 2471 | } |
| 2472 | |
| 2473 | AudioFlinger::MmapThread::MmapTrack::~MmapTrack() |
| 2474 | { |
| 2475 | } |
| 2476 | |
| 2477 | status_t AudioFlinger::MmapThread::MmapTrack::initCheck() const |
| 2478 | { |
| 2479 | return NO_ERROR; |
| 2480 | } |
| 2481 | |
| 2482 | status_t AudioFlinger::MmapThread::MmapTrack::start(AudioSystem::sync_event_t event __unused, |
Kevin Rocard | 5f2136e | 2018-05-11 22:03:00 -0700 | [diff] [blame] | 2483 | audio_session_t triggerSession __unused) |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2484 | { |
| 2485 | return NO_ERROR; |
| 2486 | } |
| 2487 | |
| 2488 | void AudioFlinger::MmapThread::MmapTrack::stop() |
| 2489 | { |
| 2490 | } |
| 2491 | |
| 2492 | // AudioBufferProvider interface |
| 2493 | status_t AudioFlinger::MmapThread::MmapTrack::getNextBuffer(AudioBufferProvider::Buffer* buffer) |
| 2494 | { |
| 2495 | buffer->frameCount = 0; |
| 2496 | buffer->raw = nullptr; |
| 2497 | return INVALID_OPERATION; |
| 2498 | } |
| 2499 | |
| 2500 | // ExtendedAudioBufferProvider interface |
| 2501 | size_t AudioFlinger::MmapThread::MmapTrack::framesReady() const { |
| 2502 | return 0; |
| 2503 | } |
| 2504 | |
| 2505 | int64_t AudioFlinger::MmapThread::MmapTrack::framesReleased() const |
| 2506 | { |
| 2507 | return 0; |
| 2508 | } |
| 2509 | |
| 2510 | void AudioFlinger::MmapThread::MmapTrack::onTimestamp(const ExtendedTimestamp ×tamp __unused) |
| 2511 | { |
| 2512 | } |
| 2513 | |
Kevin Rocard | 5f2136e | 2018-05-11 22:03:00 -0700 | [diff] [blame] | 2514 | void AudioFlinger::MmapThread::MmapTrack::appendDumpHeader(String8& result) |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2515 | { |
Eric Laurent | 973db02 | 2018-11-20 14:54:31 -0800 | [diff] [blame] | 2516 | 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] | 2517 | isOut() ? "Usg CT": "Source"); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2518 | } |
| 2519 | |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 2520 | void AudioFlinger::MmapThread::MmapTrack::appendDump(String8& result, bool active __unused) |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2521 | { |
Eric Laurent | 973db02 | 2018-11-20 14:54:31 -0800 | [diff] [blame] | 2522 | result.appendFormat("%6u %7u %7u %08X %08X %6u 0x%03X ", |
Andy Hung | 2c6c3bb | 2017-06-16 14:01:45 -0700 | [diff] [blame] | 2523 | mPid, |
| 2524 | mSessionId, |
Eric Laurent | 973db02 | 2018-11-20 14:54:31 -0800 | [diff] [blame] | 2525 | mPortId, |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2526 | mFormat, |
| 2527 | mChannelMask, |
Kevin Rocard | 5f2136e | 2018-05-11 22:03:00 -0700 | [diff] [blame] | 2528 | mSampleRate, |
| 2529 | mAttr.flags); |
| 2530 | if (isOut()) { |
| 2531 | result.appendFormat("%3x %2x", mAttr.usage, mAttr.content_type); |
| 2532 | } else { |
| 2533 | result.appendFormat("%6x", mAttr.source); |
| 2534 | } |
| 2535 | result.append("\n"); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 2536 | } |
| 2537 | |
Glenn Kasten | 63238ef | 2015-03-02 15:50:29 -0800 | [diff] [blame] | 2538 | } // namespace android |