Tracks.cpp 92 KB

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