Threads.cpp 368 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489
  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. #define ATRACE_TAG ATRACE_TAG_AUDIO
  20. #include "Configuration.h"
  21. #include <math.h>
  22. #include <fcntl.h>
  23. #include <memory>
  24. #include <string>
  25. #include <linux/futex.h>
  26. #include <sys/stat.h>
  27. #include <sys/syscall.h>
  28. #include <cutils/properties.h>
  29. #include <media/AudioParameter.h>
  30. #include <media/AudioResamplerPublic.h>
  31. #include <media/RecordBufferConverter.h>
  32. #include <media/TypeConverter.h>
  33. #include <utils/Log.h>
  34. #include <utils/Trace.h>
  35. #include <private/media/AudioTrackShared.h>
  36. #include <private/android_filesystem_config.h>
  37. #include <audio_utils/Balance.h>
  38. #include <audio_utils/channels.h>
  39. #include <audio_utils/mono_blend.h>
  40. #include <audio_utils/primitives.h>
  41. #include <audio_utils/format.h>
  42. #include <audio_utils/minifloat.h>
  43. #include <audio_utils/safe_math.h>
  44. #include <system/audio_effects/effect_ns.h>
  45. #include <system/audio_effects/effect_aec.h>
  46. #include <system/audio.h>
  47. // NBAIO implementations
  48. #include <media/nbaio/AudioStreamInSource.h>
  49. #include <media/nbaio/AudioStreamOutSink.h>
  50. #include <media/nbaio/MonoPipe.h>
  51. #include <media/nbaio/MonoPipeReader.h>
  52. #include <media/nbaio/Pipe.h>
  53. #include <media/nbaio/PipeReader.h>
  54. #include <media/nbaio/SourceAudioBufferProvider.h>
  55. #include <mediautils/BatteryNotifier.h>
  56. #include <audiomanager/AudioManager.h>
  57. #include <powermanager/PowerManager.h>
  58. #include <media/audiohal/EffectsFactoryHalInterface.h>
  59. #include <media/audiohal/StreamHalInterface.h>
  60. #include "AudioFlinger.h"
  61. #include "FastMixer.h"
  62. #include "FastCapture.h"
  63. #include <mediautils/SchedulingPolicyService.h>
  64. #include <mediautils/ServiceUtilities.h>
  65. #ifdef ADD_BATTERY_DATA
  66. #include <media/IMediaPlayerService.h>
  67. #include <media/IMediaDeathNotifier.h>
  68. #endif
  69. #ifdef DEBUG_CPU_USAGE
  70. #include <audio_utils/Statistics.h>
  71. #include <cpustats/ThreadCpuUsage.h>
  72. #endif
  73. #include "AutoPark.h"
  74. #include <pthread.h>
  75. #include "TypedLogger.h"
  76. // ----------------------------------------------------------------------------
  77. // Note: the following macro is used for extremely verbose logging message. In
  78. // order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to
  79. // 0; but one side effect of this is to turn all LOGV's as well. Some messages
  80. // are so verbose that we want to suppress them even when we have ALOG_ASSERT
  81. // turned on. Do not uncomment the #def below unless you really know what you
  82. // are doing and want to see all of the extremely verbose messages.
  83. //#define VERY_VERY_VERBOSE_LOGGING
  84. #ifdef VERY_VERY_VERBOSE_LOGGING
  85. #define ALOGVV ALOGV
  86. #else
  87. #define ALOGVV(a...) do { } while(0)
  88. #endif
  89. // TODO: Move these macro/inlines to a header file.
  90. #define max(a, b) ((a) > (b) ? (a) : (b))
  91. template <typename T>
  92. static inline T min(const T& a, const T& b)
  93. {
  94. return a < b ? a : b;
  95. }
  96. namespace android {
  97. // retry counts for buffer fill timeout
  98. // 50 * ~20msecs = 1 second
  99. static const int8_t kMaxTrackRetries = 50;
  100. static const int8_t kMaxTrackStartupRetries = 50;
  101. // allow less retry attempts on direct output thread.
  102. // direct outputs can be a scarce resource in audio hardware and should
  103. // be released as quickly as possible.
  104. static const int8_t kMaxTrackRetriesDirect = 2;
  105. // don't warn about blocked writes or record buffer overflows more often than this
  106. static const nsecs_t kWarningThrottleNs = seconds(5);
  107. // RecordThread loop sleep time upon application overrun or audio HAL read error
  108. static const int kRecordThreadSleepUs = 5000;
  109. // maximum time to wait in sendConfigEvent_l() for a status to be received
  110. static const nsecs_t kConfigEventTimeoutNs = seconds(2);
  111. // minimum sleep time for the mixer thread loop when tracks are active but in underrun
  112. static const uint32_t kMinThreadSleepTimeUs = 5000;
  113. // maximum divider applied to the active sleep time in the mixer thread loop
  114. static const uint32_t kMaxThreadSleepTimeShift = 2;
  115. // minimum normal sink buffer size, expressed in milliseconds rather than frames
  116. // FIXME This should be based on experimentally observed scheduling jitter
  117. static const uint32_t kMinNormalSinkBufferSizeMs = 20;
  118. // maximum normal sink buffer size
  119. static const uint32_t kMaxNormalSinkBufferSizeMs = 24;
  120. // minimum capture buffer size in milliseconds to _not_ need a fast capture thread
  121. // FIXME This should be based on experimentally observed scheduling jitter
  122. static const uint32_t kMinNormalCaptureBufferSizeMs = 12;
  123. // Offloaded output thread standby delay: allows track transition without going to standby
  124. static const nsecs_t kOffloadStandbyDelayNs = seconds(1);
  125. // Direct output thread minimum sleep time in idle or active(underrun) state
  126. static const nsecs_t kDirectMinSleepTimeUs = 10000;
  127. // The universal constant for ubiquitous 20ms value. The value of 20ms seems to provide a good
  128. // balance between power consumption and latency, and allows threads to be scheduled reliably
  129. // by the CFS scheduler.
  130. // FIXME Express other hardcoded references to 20ms with references to this constant and move
  131. // it appropriately.
  132. #define FMS_20 20
  133. // Whether to use fast mixer
  134. static const enum {
  135. FastMixer_Never, // never initialize or use: for debugging only
  136. FastMixer_Always, // always initialize and use, even if not needed: for debugging only
  137. // normal mixer multiplier is 1
  138. FastMixer_Static, // initialize if needed, then use all the time if initialized,
  139. // multiplier is calculated based on min & max normal mixer buffer size
  140. FastMixer_Dynamic, // initialize if needed, then use dynamically depending on track load,
  141. // multiplier is calculated based on min & max normal mixer buffer size
  142. // FIXME for FastMixer_Dynamic:
  143. // Supporting this option will require fixing HALs that can't handle large writes.
  144. // For example, one HAL implementation returns an error from a large write,
  145. // and another HAL implementation corrupts memory, possibly in the sample rate converter.
  146. // We could either fix the HAL implementations, or provide a wrapper that breaks
  147. // up large writes into smaller ones, and the wrapper would need to deal with scheduler.
  148. } kUseFastMixer = FastMixer_Static;
  149. // Whether to use fast capture
  150. static const enum {
  151. FastCapture_Never, // never initialize or use: for debugging only
  152. FastCapture_Always, // always initialize and use, even if not needed: for debugging only
  153. FastCapture_Static, // initialize if needed, then use all the time if initialized
  154. } kUseFastCapture = FastCapture_Static;
  155. // Priorities for requestPriority
  156. static const int kPriorityAudioApp = 2;
  157. static const int kPriorityFastMixer = 3;
  158. static const int kPriorityFastCapture = 3;
  159. // IAudioFlinger::createTrack() has an in/out parameter 'pFrameCount' for the total size of the
  160. // track buffer in shared memory. Zero on input means to use a default value. For fast tracks,
  161. // AudioFlinger derives the default from HAL buffer size and 'fast track multiplier'.
  162. // This is the default value, if not specified by property.
  163. static const int kFastTrackMultiplier = 2;
  164. // The minimum and maximum allowed values
  165. static const int kFastTrackMultiplierMin = 1;
  166. static const int kFastTrackMultiplierMax = 2;
  167. // The actual value to use, which can be specified per-device via property af.fast_track_multiplier.
  168. static int sFastTrackMultiplier = kFastTrackMultiplier;
  169. // See Thread::readOnlyHeap().
  170. // Initially this heap is used to allocate client buffers for "fast" AudioRecord.
  171. // Eventually it will be the single buffer that FastCapture writes into via HAL read(),
  172. // and that all "fast" AudioRecord clients read from. In either case, the size can be small.
  173. static const size_t kRecordThreadReadOnlyHeapSize = 0xD000;
  174. // ----------------------------------------------------------------------------
  175. static pthread_once_t sFastTrackMultiplierOnce = PTHREAD_ONCE_INIT;
  176. static void sFastTrackMultiplierInit()
  177. {
  178. char value[PROPERTY_VALUE_MAX];
  179. if (property_get("af.fast_track_multiplier", value, NULL) > 0) {
  180. char *endptr;
  181. unsigned long ul = strtoul(value, &endptr, 0);
  182. if (*endptr == '\0' && kFastTrackMultiplierMin <= ul && ul <= kFastTrackMultiplierMax) {
  183. sFastTrackMultiplier = (int) ul;
  184. }
  185. }
  186. }
  187. // ----------------------------------------------------------------------------
  188. #ifdef ADD_BATTERY_DATA
  189. // To collect the amplifier usage
  190. static void addBatteryData(uint32_t params) {
  191. sp<IMediaPlayerService> service = IMediaDeathNotifier::getMediaPlayerService();
  192. if (service == NULL) {
  193. // it already logged
  194. return;
  195. }
  196. service->addBatteryData(params);
  197. }
  198. #endif
  199. // Track the CLOCK_BOOTTIME versus CLOCK_MONOTONIC timebase offset
  200. struct {
  201. // call when you acquire a partial wakelock
  202. void acquire(const sp<IBinder> &wakeLockToken) {
  203. pthread_mutex_lock(&mLock);
  204. if (wakeLockToken.get() == nullptr) {
  205. adjustTimebaseOffset(&mBoottimeOffset, ExtendedTimestamp::TIMEBASE_BOOTTIME);
  206. } else {
  207. if (mCount == 0) {
  208. adjustTimebaseOffset(&mBoottimeOffset, ExtendedTimestamp::TIMEBASE_BOOTTIME);
  209. }
  210. ++mCount;
  211. }
  212. pthread_mutex_unlock(&mLock);
  213. }
  214. // call when you release a partial wakelock.
  215. void release(const sp<IBinder> &wakeLockToken) {
  216. if (wakeLockToken.get() == nullptr) {
  217. return;
  218. }
  219. pthread_mutex_lock(&mLock);
  220. if (--mCount < 0) {
  221. ALOGE("negative wakelock count");
  222. mCount = 0;
  223. }
  224. pthread_mutex_unlock(&mLock);
  225. }
  226. // retrieves the boottime timebase offset from monotonic.
  227. int64_t getBoottimeOffset() {
  228. pthread_mutex_lock(&mLock);
  229. int64_t boottimeOffset = mBoottimeOffset;
  230. pthread_mutex_unlock(&mLock);
  231. return boottimeOffset;
  232. }
  233. // Adjusts the timebase offset between TIMEBASE_MONOTONIC
  234. // and the selected timebase.
  235. // Currently only TIMEBASE_BOOTTIME is allowed.
  236. //
  237. // This only needs to be called upon acquiring the first partial wakelock
  238. // after all other partial wakelocks are released.
  239. //
  240. // We do an empirical measurement of the offset rather than parsing
  241. // /proc/timer_list since the latter is not a formal kernel ABI.
  242. static void adjustTimebaseOffset(int64_t *offset, ExtendedTimestamp::Timebase timebase) {
  243. int clockbase;
  244. switch (timebase) {
  245. case ExtendedTimestamp::TIMEBASE_BOOTTIME:
  246. clockbase = SYSTEM_TIME_BOOTTIME;
  247. break;
  248. default:
  249. LOG_ALWAYS_FATAL("invalid timebase %d", timebase);
  250. break;
  251. }
  252. // try three times to get the clock offset, choose the one
  253. // with the minimum gap in measurements.
  254. const int tries = 3;
  255. nsecs_t bestGap, measured;
  256. for (int i = 0; i < tries; ++i) {
  257. const nsecs_t tmono = systemTime(SYSTEM_TIME_MONOTONIC);
  258. const nsecs_t tbase = systemTime(clockbase);
  259. const nsecs_t tmono2 = systemTime(SYSTEM_TIME_MONOTONIC);
  260. const nsecs_t gap = tmono2 - tmono;
  261. if (i == 0 || gap < bestGap) {
  262. bestGap = gap;
  263. measured = tbase - ((tmono + tmono2) >> 1);
  264. }
  265. }
  266. // to avoid micro-adjusting, we don't change the timebase
  267. // unless it is significantly different.
  268. //
  269. // Assumption: It probably takes more than toleranceNs to
  270. // suspend and resume the device.
  271. static int64_t toleranceNs = 10000; // 10 us
  272. if (llabs(*offset - measured) > toleranceNs) {
  273. ALOGV("Adjusting timebase offset old: %lld new: %lld",
  274. (long long)*offset, (long long)measured);
  275. *offset = measured;
  276. }
  277. }
  278. pthread_mutex_t mLock;
  279. int32_t mCount;
  280. int64_t mBoottimeOffset;
  281. } gBoottime = { PTHREAD_MUTEX_INITIALIZER, 0, 0 }; // static, so use POD initialization
  282. // ----------------------------------------------------------------------------
  283. // CPU Stats
  284. // ----------------------------------------------------------------------------
  285. class CpuStats {
  286. public:
  287. CpuStats();
  288. void sample(const String8 &title);
  289. #ifdef DEBUG_CPU_USAGE
  290. private:
  291. ThreadCpuUsage mCpuUsage; // instantaneous thread CPU usage in wall clock ns
  292. audio_utils::Statistics<double> mWcStats; // statistics on thread CPU usage in wall clock ns
  293. audio_utils::Statistics<double> mHzStats; // statistics on thread CPU usage in cycles
  294. int mCpuNum; // thread's current CPU number
  295. int mCpukHz; // frequency of thread's current CPU in kHz
  296. #endif
  297. };
  298. CpuStats::CpuStats()
  299. #ifdef DEBUG_CPU_USAGE
  300. : mCpuNum(-1), mCpukHz(-1)
  301. #endif
  302. {
  303. }
  304. void CpuStats::sample(const String8 &title
  305. #ifndef DEBUG_CPU_USAGE
  306. __unused
  307. #endif
  308. ) {
  309. #ifdef DEBUG_CPU_USAGE
  310. // get current thread's delta CPU time in wall clock ns
  311. double wcNs;
  312. bool valid = mCpuUsage.sampleAndEnable(wcNs);
  313. // record sample for wall clock statistics
  314. if (valid) {
  315. mWcStats.add(wcNs);
  316. }
  317. // get the current CPU number
  318. int cpuNum = sched_getcpu();
  319. // get the current CPU frequency in kHz
  320. int cpukHz = mCpuUsage.getCpukHz(cpuNum);
  321. // check if either CPU number or frequency changed
  322. if (cpuNum != mCpuNum || cpukHz != mCpukHz) {
  323. mCpuNum = cpuNum;
  324. mCpukHz = cpukHz;
  325. // ignore sample for purposes of cycles
  326. valid = false;
  327. }
  328. // if no change in CPU number or frequency, then record sample for cycle statistics
  329. if (valid && mCpukHz > 0) {
  330. const double cycles = wcNs * cpukHz * 0.000001;
  331. mHzStats.add(cycles);
  332. }
  333. const unsigned n = mWcStats.getN();
  334. // mCpuUsage.elapsed() is expensive, so don't call it every loop
  335. if ((n & 127) == 1) {
  336. const long long elapsed = mCpuUsage.elapsed();
  337. if (elapsed >= DEBUG_CPU_USAGE * 1000000000LL) {
  338. const double perLoop = elapsed / (double) n;
  339. const double perLoop100 = perLoop * 0.01;
  340. const double perLoop1k = perLoop * 0.001;
  341. const double mean = mWcStats.getMean();
  342. const double stddev = mWcStats.getStdDev();
  343. const double minimum = mWcStats.getMin();
  344. const double maximum = mWcStats.getMax();
  345. const double meanCycles = mHzStats.getMean();
  346. const double stddevCycles = mHzStats.getStdDev();
  347. const double minCycles = mHzStats.getMin();
  348. const double maxCycles = mHzStats.getMax();
  349. mCpuUsage.resetElapsed();
  350. mWcStats.reset();
  351. mHzStats.reset();
  352. ALOGD("CPU usage for %s over past %.1f secs\n"
  353. " (%u mixer loops at %.1f mean ms per loop):\n"
  354. " us per mix loop: mean=%.0f stddev=%.0f min=%.0f max=%.0f\n"
  355. " %% of wall: mean=%.1f stddev=%.1f min=%.1f max=%.1f\n"
  356. " MHz: mean=%.1f, stddev=%.1f, min=%.1f max=%.1f",
  357. title.string(),
  358. elapsed * .000000001, n, perLoop * .000001,
  359. mean * .001,
  360. stddev * .001,
  361. minimum * .001,
  362. maximum * .001,
  363. mean / perLoop100,
  364. stddev / perLoop100,
  365. minimum / perLoop100,
  366. maximum / perLoop100,
  367. meanCycles / perLoop1k,
  368. stddevCycles / perLoop1k,
  369. minCycles / perLoop1k,
  370. maxCycles / perLoop1k);
  371. }
  372. }
  373. #endif
  374. };
  375. // ----------------------------------------------------------------------------
  376. // ThreadBase
  377. // ----------------------------------------------------------------------------
  378. // static
  379. const char *AudioFlinger::ThreadBase::threadTypeToString(AudioFlinger::ThreadBase::type_t type)
  380. {
  381. switch (type) {
  382. case MIXER:
  383. return "MIXER";
  384. case DIRECT:
  385. return "DIRECT";
  386. case DUPLICATING:
  387. return "DUPLICATING";
  388. case RECORD:
  389. return "RECORD";
  390. case OFFLOAD:
  391. return "OFFLOAD";
  392. case MMAP:
  393. return "MMAP";
  394. default:
  395. return "unknown";
  396. }
  397. }
  398. AudioFlinger::ThreadBase::ThreadBase(const sp<AudioFlinger>& audioFlinger, audio_io_handle_t id,
  399. audio_devices_t outDevice, audio_devices_t inDevice, type_t type, bool systemReady)
  400. : Thread(false /*canCallJava*/),
  401. mType(type),
  402. mAudioFlinger(audioFlinger),
  403. // mSampleRate, mFrameCount, mChannelMask, mChannelCount, mFrameSize, mFormat, mBufferSize
  404. // are set by PlaybackThread::readOutputParameters_l() or
  405. // RecordThread::readInputParameters_l()
  406. //FIXME: mStandby should be true here. Is this some kind of hack?
  407. mStandby(false), mOutDevice(outDevice), mInDevice(inDevice),
  408. mPrevOutDevice(AUDIO_DEVICE_NONE), mPrevInDevice(AUDIO_DEVICE_NONE),
  409. mAudioSource(AUDIO_SOURCE_DEFAULT), mId(id),
  410. // mName will be set by concrete (non-virtual) subclass
  411. mDeathRecipient(new PMDeathRecipient(this)),
  412. mSystemReady(systemReady),
  413. mSignalPending(false)
  414. {
  415. memset(&mPatch, 0, sizeof(struct audio_patch));
  416. }
  417. AudioFlinger::ThreadBase::~ThreadBase()
  418. {
  419. // mConfigEvents should be empty, but just in case it isn't, free the memory it owns
  420. mConfigEvents.clear();
  421. // do not lock the mutex in destructor
  422. releaseWakeLock_l();
  423. if (mPowerManager != 0) {
  424. sp<IBinder> binder = IInterface::asBinder(mPowerManager);
  425. binder->unlinkToDeath(mDeathRecipient);
  426. }
  427. sendStatistics(true /* force */);
  428. }
  429. status_t AudioFlinger::ThreadBase::readyToRun()
  430. {
  431. status_t status = initCheck();
  432. if (status == NO_ERROR) {
  433. ALOGI("AudioFlinger's thread %p tid=%d ready to run", this, getTid());
  434. } else {
  435. ALOGE("No working audio driver found.");
  436. }
  437. return status;
  438. }
  439. void AudioFlinger::ThreadBase::exit()
  440. {
  441. ALOGV("ThreadBase::exit");
  442. // do any cleanup required for exit to succeed
  443. preExit();
  444. {
  445. // This lock prevents the following race in thread (uniprocessor for illustration):
  446. // if (!exitPending()) {
  447. // // context switch from here to exit()
  448. // // exit() calls requestExit(), what exitPending() observes
  449. // // exit() calls signal(), which is dropped since no waiters
  450. // // context switch back from exit() to here
  451. // mWaitWorkCV.wait(...);
  452. // // now thread is hung
  453. // }
  454. AutoMutex lock(mLock);
  455. requestExit();
  456. mWaitWorkCV.broadcast();
  457. }
  458. // When Thread::requestExitAndWait is made virtual and this method is renamed to
  459. // "virtual status_t requestExitAndWait()", replace by "return Thread::requestExitAndWait();"
  460. requestExitAndWait();
  461. }
  462. status_t AudioFlinger::ThreadBase::setParameters(const String8& keyValuePairs)
  463. {
  464. ALOGV("ThreadBase::setParameters() %s", keyValuePairs.string());
  465. Mutex::Autolock _l(mLock);
  466. return sendSetParameterConfigEvent_l(keyValuePairs);
  467. }
  468. // sendConfigEvent_l() must be called with ThreadBase::mLock held
  469. // Can temporarily release the lock if waiting for a reply from processConfigEvents_l().
  470. status_t AudioFlinger::ThreadBase::sendConfigEvent_l(sp<ConfigEvent>& event)
  471. {
  472. status_t status = NO_ERROR;
  473. if (event->mRequiresSystemReady && !mSystemReady) {
  474. event->mWaitStatus = false;
  475. mPendingConfigEvents.add(event);
  476. return status;
  477. }
  478. mConfigEvents.add(event);
  479. ALOGV("sendConfigEvent_l() num events %zu event %d", mConfigEvents.size(), event->mType);
  480. mWaitWorkCV.signal();
  481. mLock.unlock();
  482. {
  483. Mutex::Autolock _l(event->mLock);
  484. while (event->mWaitStatus) {
  485. if (event->mCond.waitRelative(event->mLock, kConfigEventTimeoutNs) != NO_ERROR) {
  486. event->mStatus = TIMED_OUT;
  487. event->mWaitStatus = false;
  488. }
  489. }
  490. status = event->mStatus;
  491. }
  492. mLock.lock();
  493. return status;
  494. }
  495. void AudioFlinger::ThreadBase::sendIoConfigEvent(audio_io_config_event event, pid_t pid,
  496. audio_port_handle_t portId)
  497. {
  498. Mutex::Autolock _l(mLock);
  499. sendIoConfigEvent_l(event, pid, portId);
  500. }
  501. // sendIoConfigEvent_l() must be called with ThreadBase::mLock held
  502. void AudioFlinger::ThreadBase::sendIoConfigEvent_l(audio_io_config_event event, pid_t pid,
  503. audio_port_handle_t portId)
  504. {
  505. // The audio statistics history is exponentially weighted to forget events
  506. // about five or more seconds in the past. In order to have
  507. // crisper statistics for mediametrics, we reset the statistics on
  508. // an IoConfigEvent, to reflect different properties for a new device.
  509. mIoJitterMs.reset();
  510. mLatencyMs.reset();
  511. mProcessTimeMs.reset();
  512. mTimestampVerifier.discontinuity();
  513. sp<ConfigEvent> configEvent = (ConfigEvent *)new IoConfigEvent(event, pid, portId);
  514. sendConfigEvent_l(configEvent);
  515. }
  516. void AudioFlinger::ThreadBase::sendPrioConfigEvent(pid_t pid, pid_t tid, int32_t prio, bool forApp)
  517. {
  518. Mutex::Autolock _l(mLock);
  519. sendPrioConfigEvent_l(pid, tid, prio, forApp);
  520. }
  521. // sendPrioConfigEvent_l() must be called with ThreadBase::mLock held
  522. void AudioFlinger::ThreadBase::sendPrioConfigEvent_l(
  523. pid_t pid, pid_t tid, int32_t prio, bool forApp)
  524. {
  525. sp<ConfigEvent> configEvent = (ConfigEvent *)new PrioConfigEvent(pid, tid, prio, forApp);
  526. sendConfigEvent_l(configEvent);
  527. }
  528. // sendSetParameterConfigEvent_l() must be called with ThreadBase::mLock held
  529. status_t AudioFlinger::ThreadBase::sendSetParameterConfigEvent_l(const String8& keyValuePair)
  530. {
  531. sp<ConfigEvent> configEvent;
  532. AudioParameter param(keyValuePair);
  533. int value;
  534. if (param.getInt(String8(AudioParameter::keyMonoOutput), value) == NO_ERROR) {
  535. setMasterMono_l(value != 0);
  536. if (param.size() == 1) {
  537. return NO_ERROR; // should be a solo parameter - we don't pass down
  538. }
  539. param.remove(String8(AudioParameter::keyMonoOutput));
  540. configEvent = new SetParameterConfigEvent(param.toString());
  541. } else {
  542. configEvent = new SetParameterConfigEvent(keyValuePair);
  543. }
  544. return sendConfigEvent_l(configEvent);
  545. }
  546. status_t AudioFlinger::ThreadBase::sendCreateAudioPatchConfigEvent(
  547. const struct audio_patch *patch,
  548. audio_patch_handle_t *handle)
  549. {
  550. Mutex::Autolock _l(mLock);
  551. sp<ConfigEvent> configEvent = (ConfigEvent *)new CreateAudioPatchConfigEvent(*patch, *handle);
  552. status_t status = sendConfigEvent_l(configEvent);
  553. if (status == NO_ERROR) {
  554. CreateAudioPatchConfigEventData *data =
  555. (CreateAudioPatchConfigEventData *)configEvent->mData.get();
  556. *handle = data->mHandle;
  557. }
  558. return status;
  559. }
  560. status_t AudioFlinger::ThreadBase::sendReleaseAudioPatchConfigEvent(
  561. const audio_patch_handle_t handle)
  562. {
  563. Mutex::Autolock _l(mLock);
  564. sp<ConfigEvent> configEvent = (ConfigEvent *)new ReleaseAudioPatchConfigEvent(handle);
  565. return sendConfigEvent_l(configEvent);
  566. }
  567. // post condition: mConfigEvents.isEmpty()
  568. void AudioFlinger::ThreadBase::processConfigEvents_l()
  569. {
  570. bool configChanged = false;
  571. while (!mConfigEvents.isEmpty()) {
  572. ALOGV("processConfigEvents_l() remaining events %zu", mConfigEvents.size());
  573. sp<ConfigEvent> event = mConfigEvents[0];
  574. mConfigEvents.removeAt(0);
  575. switch (event->mType) {
  576. case CFG_EVENT_PRIO: {
  577. PrioConfigEventData *data = (PrioConfigEventData *)event->mData.get();
  578. // FIXME Need to understand why this has to be done asynchronously
  579. int err = requestPriority(data->mPid, data->mTid, data->mPrio, data->mForApp,
  580. true /*asynchronous*/);
  581. if (err != 0) {
  582. ALOGW("Policy SCHED_FIFO priority %d is unavailable for pid %d tid %d; error %d",
  583. data->mPrio, data->mPid, data->mTid, err);
  584. }
  585. } break;
  586. case CFG_EVENT_IO: {
  587. IoConfigEventData *data = (IoConfigEventData *)event->mData.get();
  588. ioConfigChanged(data->mEvent, data->mPid, data->mPortId);
  589. } break;
  590. case CFG_EVENT_SET_PARAMETER: {
  591. SetParameterConfigEventData *data = (SetParameterConfigEventData *)event->mData.get();
  592. if (checkForNewParameter_l(data->mKeyValuePairs, event->mStatus)) {
  593. configChanged = true;
  594. mLocalLog.log("CFG_EVENT_SET_PARAMETER: (%s) configuration changed",
  595. data->mKeyValuePairs.string());
  596. }
  597. } break;
  598. case CFG_EVENT_CREATE_AUDIO_PATCH: {
  599. const audio_devices_t oldDevice = getDevice();
  600. CreateAudioPatchConfigEventData *data =
  601. (CreateAudioPatchConfigEventData *)event->mData.get();
  602. event->mStatus = createAudioPatch_l(&data->mPatch, &data->mHandle);
  603. const audio_devices_t newDevice = getDevice();
  604. mLocalLog.log("CFG_EVENT_CREATE_AUDIO_PATCH: old device %#x (%s) new device %#x (%s)",
  605. (unsigned)oldDevice, toString(oldDevice).c_str(),
  606. (unsigned)newDevice, toString(newDevice).c_str());
  607. } break;
  608. case CFG_EVENT_RELEASE_AUDIO_PATCH: {
  609. const audio_devices_t oldDevice = getDevice();
  610. ReleaseAudioPatchConfigEventData *data =
  611. (ReleaseAudioPatchConfigEventData *)event->mData.get();
  612. event->mStatus = releaseAudioPatch_l(data->mHandle);
  613. const audio_devices_t newDevice = getDevice();
  614. mLocalLog.log("CFG_EVENT_RELEASE_AUDIO_PATCH: old device %#x (%s) new device %#x (%s)",
  615. (unsigned)oldDevice, toString(oldDevice).c_str(),
  616. (unsigned)newDevice, toString(newDevice).c_str());
  617. } break;
  618. default:
  619. ALOG_ASSERT(false, "processConfigEvents_l() unknown event type %d", event->mType);
  620. break;
  621. }
  622. {
  623. Mutex::Autolock _l(event->mLock);
  624. if (event->mWaitStatus) {
  625. event->mWaitStatus = false;
  626. event->mCond.signal();
  627. }
  628. }
  629. ALOGV_IF(mConfigEvents.isEmpty(), "processConfigEvents_l() DONE thread %p", this);
  630. }
  631. if (configChanged) {
  632. cacheParameters_l();
  633. }
  634. }
  635. String8 channelMaskToString(audio_channel_mask_t mask, bool output) {
  636. String8 s;
  637. const audio_channel_representation_t representation =
  638. audio_channel_mask_get_representation(mask);
  639. switch (representation) {
  640. // Travel all single bit channel mask to convert channel mask to string.
  641. case AUDIO_CHANNEL_REPRESENTATION_POSITION: {
  642. if (output) {
  643. if (mask & AUDIO_CHANNEL_OUT_FRONT_LEFT) s.append("front-left, ");
  644. if (mask & AUDIO_CHANNEL_OUT_FRONT_RIGHT) s.append("front-right, ");
  645. if (mask & AUDIO_CHANNEL_OUT_FRONT_CENTER) s.append("front-center, ");
  646. if (mask & AUDIO_CHANNEL_OUT_LOW_FREQUENCY) s.append("low freq, ");
  647. if (mask & AUDIO_CHANNEL_OUT_BACK_LEFT) s.append("back-left, ");
  648. if (mask & AUDIO_CHANNEL_OUT_BACK_RIGHT) s.append("back-right, ");
  649. if (mask & AUDIO_CHANNEL_OUT_FRONT_LEFT_OF_CENTER) s.append("front-left-of-center, ");
  650. if (mask & AUDIO_CHANNEL_OUT_FRONT_RIGHT_OF_CENTER) s.append("front-right-of-center, ");
  651. if (mask & AUDIO_CHANNEL_OUT_BACK_CENTER) s.append("back-center, ");
  652. if (mask & AUDIO_CHANNEL_OUT_SIDE_LEFT) s.append("side-left, ");
  653. if (mask & AUDIO_CHANNEL_OUT_SIDE_RIGHT) s.append("side-right, ");
  654. if (mask & AUDIO_CHANNEL_OUT_TOP_CENTER) s.append("top-center ,");
  655. if (mask & AUDIO_CHANNEL_OUT_TOP_FRONT_LEFT) s.append("top-front-left, ");
  656. if (mask & AUDIO_CHANNEL_OUT_TOP_FRONT_CENTER) s.append("top-front-center, ");
  657. if (mask & AUDIO_CHANNEL_OUT_TOP_FRONT_RIGHT) s.append("top-front-right, ");
  658. if (mask & AUDIO_CHANNEL_OUT_TOP_BACK_LEFT) s.append("top-back-left, ");
  659. if (mask & AUDIO_CHANNEL_OUT_TOP_BACK_CENTER) s.append("top-back-center, " );
  660. if (mask & AUDIO_CHANNEL_OUT_TOP_BACK_RIGHT) s.append("top-back-right, " );
  661. if (mask & AUDIO_CHANNEL_OUT_TOP_SIDE_LEFT) s.append("top-side-left, " );
  662. if (mask & AUDIO_CHANNEL_OUT_TOP_SIDE_RIGHT) s.append("top-side-right, " );
  663. if (mask & AUDIO_CHANNEL_OUT_HAPTIC_B) s.append("haptic-B, " );
  664. if (mask & AUDIO_CHANNEL_OUT_HAPTIC_A) s.append("haptic-A, " );
  665. if (mask & ~AUDIO_CHANNEL_OUT_ALL) s.append("unknown, ");
  666. } else {
  667. if (mask & AUDIO_CHANNEL_IN_LEFT) s.append("left, ");
  668. if (mask & AUDIO_CHANNEL_IN_RIGHT) s.append("right, ");
  669. if (mask & AUDIO_CHANNEL_IN_FRONT) s.append("front, ");
  670. if (mask & AUDIO_CHANNEL_IN_BACK) s.append("back, ");
  671. if (mask & AUDIO_CHANNEL_IN_LEFT_PROCESSED) s.append("left-processed, ");
  672. if (mask & AUDIO_CHANNEL_IN_RIGHT_PROCESSED) s.append("right-processed, ");
  673. if (mask & AUDIO_CHANNEL_IN_FRONT_PROCESSED) s.append("front-processed, ");
  674. if (mask & AUDIO_CHANNEL_IN_BACK_PROCESSED) s.append("back-processed, ");
  675. if (mask & AUDIO_CHANNEL_IN_PRESSURE) s.append("pressure, ");
  676. if (mask & AUDIO_CHANNEL_IN_X_AXIS) s.append("X, ");
  677. if (mask & AUDIO_CHANNEL_IN_Y_AXIS) s.append("Y, ");
  678. if (mask & AUDIO_CHANNEL_IN_Z_AXIS) s.append("Z, ");
  679. if (mask & AUDIO_CHANNEL_IN_BACK_LEFT) s.append("back-left, ");
  680. if (mask & AUDIO_CHANNEL_IN_BACK_RIGHT) s.append("back-right, ");
  681. if (mask & AUDIO_CHANNEL_IN_CENTER) s.append("center, ");
  682. if (mask & AUDIO_CHANNEL_IN_LOW_FREQUENCY) s.append("low freq, ");
  683. if (mask & AUDIO_CHANNEL_IN_TOP_LEFT) s.append("top-left, " );
  684. if (mask & AUDIO_CHANNEL_IN_TOP_RIGHT) s.append("top-right, " );
  685. if (mask & AUDIO_CHANNEL_IN_VOICE_UPLINK) s.append("voice-uplink, ");
  686. if (mask & AUDIO_CHANNEL_IN_VOICE_DNLINK) s.append("voice-dnlink, ");
  687. if (mask & ~AUDIO_CHANNEL_IN_ALL) s.append("unknown, ");
  688. }
  689. const int len = s.length();
  690. if (len > 2) {
  691. (void) s.lockBuffer(len); // needed?
  692. s.unlockBuffer(len - 2); // remove trailing ", "
  693. }
  694. return s;
  695. }
  696. case AUDIO_CHANNEL_REPRESENTATION_INDEX:
  697. s.appendFormat("index mask, bits:%#x", audio_channel_mask_get_bits(mask));
  698. return s;
  699. default:
  700. s.appendFormat("unknown mask, representation:%d bits:%#x",
  701. representation, audio_channel_mask_get_bits(mask));
  702. return s;
  703. }
  704. }
  705. void AudioFlinger::ThreadBase::dump(int fd, const Vector<String16>& args)
  706. {
  707. dprintf(fd, "\n%s thread %p, name %s, tid %d, type %d (%s):\n", isOutput() ? "Output" : "Input",
  708. this, mThreadName, getTid(), type(), threadTypeToString(type()));
  709. bool locked = AudioFlinger::dumpTryLock(mLock);
  710. if (!locked) {
  711. dprintf(fd, " Thread may be deadlocked\n");
  712. }
  713. dumpBase_l(fd, args);
  714. dumpInternals_l(fd, args);
  715. dumpTracks_l(fd, args);
  716. dumpEffectChains_l(fd, args);
  717. if (locked) {
  718. mLock.unlock();
  719. }
  720. dprintf(fd, " Local log:\n");
  721. mLocalLog.dump(fd, " " /* prefix */, 40 /* lines */);
  722. }
  723. void AudioFlinger::ThreadBase::dumpBase_l(int fd, const Vector<String16>& args __unused)
  724. {
  725. dprintf(fd, " I/O handle: %d\n", mId);
  726. dprintf(fd, " Standby: %s\n", mStandby ? "yes" : "no");
  727. dprintf(fd, " Sample rate: %u Hz\n", mSampleRate);
  728. dprintf(fd, " HAL frame count: %zu\n", mFrameCount);
  729. dprintf(fd, " HAL format: 0x%x (%s)\n", mHALFormat, formatToString(mHALFormat).c_str());
  730. dprintf(fd, " HAL buffer size: %zu bytes\n", mBufferSize);
  731. dprintf(fd, " Channel count: %u\n", mChannelCount);
  732. dprintf(fd, " Channel mask: 0x%08x (%s)\n", mChannelMask,
  733. channelMaskToString(mChannelMask, mType != RECORD).string());
  734. dprintf(fd, " Processing format: 0x%x (%s)\n", mFormat, formatToString(mFormat).c_str());
  735. dprintf(fd, " Processing frame size: %zu bytes\n", mFrameSize);
  736. dprintf(fd, " Pending config events:");
  737. size_t numConfig = mConfigEvents.size();
  738. if (numConfig) {
  739. const size_t SIZE = 256;
  740. char buffer[SIZE];
  741. for (size_t i = 0; i < numConfig; i++) {
  742. mConfigEvents[i]->dump(buffer, SIZE);
  743. dprintf(fd, "\n %s", buffer);
  744. }
  745. dprintf(fd, "\n");
  746. } else {
  747. dprintf(fd, " none\n");
  748. }
  749. // Note: output device may be used by capture threads for effects such as AEC.
  750. dprintf(fd, " Output device: %#x (%s)\n", mOutDevice, toString(mOutDevice).c_str());
  751. dprintf(fd, " Input device: %#x (%s)\n", mInDevice, toString(mInDevice).c_str());
  752. dprintf(fd, " Audio source: %d (%s)\n", mAudioSource, toString(mAudioSource).c_str());
  753. // Dump timestamp statistics for the Thread types that support it.
  754. if (mType == RECORD
  755. || mType == MIXER
  756. || mType == DUPLICATING
  757. || mType == DIRECT
  758. || mType == OFFLOAD) {
  759. dprintf(fd, " Timestamp stats: %s\n", mTimestampVerifier.toString().c_str());
  760. dprintf(fd, " Timestamp corrected: %s\n", isTimestampCorrectionEnabled() ? "yes" : "no");
  761. }
  762. if (mLastIoBeginNs > 0) { // MMAP may not set this
  763. dprintf(fd, " Last %s occurred (msecs): %lld\n",
  764. isOutput() ? "write" : "read",
  765. (long long) (systemTime() - mLastIoBeginNs) / NANOS_PER_MILLISECOND);
  766. }
  767. if (mProcessTimeMs.getN() > 0) {
  768. dprintf(fd, " Process time ms stats: %s\n", mProcessTimeMs.toString().c_str());
  769. }
  770. if (mIoJitterMs.getN() > 0) {
  771. dprintf(fd, " Hal %s jitter ms stats: %s\n",
  772. isOutput() ? "write" : "read",
  773. mIoJitterMs.toString().c_str());
  774. }
  775. if (mLatencyMs.getN() > 0) {
  776. dprintf(fd, " Threadloop %s latency stats: %s\n",
  777. isOutput() ? "write" : "read",
  778. mLatencyMs.toString().c_str());
  779. }
  780. }
  781. void AudioFlinger::ThreadBase::dumpEffectChains_l(int fd, const Vector<String16>& args)
  782. {
  783. const size_t SIZE = 256;
  784. char buffer[SIZE];
  785. size_t numEffectChains = mEffectChains.size();
  786. snprintf(buffer, SIZE, " %zu Effect Chains\n", numEffectChains);
  787. write(fd, buffer, strlen(buffer));
  788. for (size_t i = 0; i < numEffectChains; ++i) {
  789. sp<EffectChain> chain = mEffectChains[i];
  790. if (chain != 0) {
  791. chain->dump(fd, args);
  792. }
  793. }
  794. }
  795. void AudioFlinger::ThreadBase::acquireWakeLock()
  796. {
  797. Mutex::Autolock _l(mLock);
  798. acquireWakeLock_l();
  799. }
  800. String16 AudioFlinger::ThreadBase::getWakeLockTag()
  801. {
  802. switch (mType) {
  803. case MIXER:
  804. return String16("AudioMix");
  805. case DIRECT:
  806. return String16("AudioDirectOut");
  807. case DUPLICATING:
  808. return String16("AudioDup");
  809. case RECORD:
  810. return String16("AudioIn");
  811. case OFFLOAD:
  812. return String16("AudioOffload");
  813. case MMAP:
  814. return String16("Mmap");
  815. default:
  816. ALOG_ASSERT(false);
  817. return String16("AudioUnknown");
  818. }
  819. }
  820. void AudioFlinger::ThreadBase::acquireWakeLock_l()
  821. {
  822. getPowerManager_l();
  823. if (mPowerManager != 0) {
  824. sp<IBinder> binder = new BBinder();
  825. // Uses AID_AUDIOSERVER for wakelock. updateWakeLockUids_l() updates with client uids.
  826. status_t status = mPowerManager->acquireWakeLock(POWERMANAGER_PARTIAL_WAKE_LOCK,
  827. binder,
  828. getWakeLockTag(),
  829. String16("audioserver"),
  830. true /* FIXME force oneway contrary to .aidl */);
  831. if (status == NO_ERROR) {
  832. mWakeLockToken = binder;
  833. }
  834. ALOGV("acquireWakeLock_l() %s status %d", mThreadName, status);
  835. }
  836. gBoottime.acquire(mWakeLockToken);
  837. mTimestamp.mTimebaseOffset[ExtendedTimestamp::TIMEBASE_BOOTTIME] =
  838. gBoottime.getBoottimeOffset();
  839. }
  840. void AudioFlinger::ThreadBase::releaseWakeLock()
  841. {
  842. Mutex::Autolock _l(mLock);
  843. releaseWakeLock_l();
  844. }
  845. void AudioFlinger::ThreadBase::releaseWakeLock_l()
  846. {
  847. gBoottime.release(mWakeLockToken);
  848. if (mWakeLockToken != 0) {
  849. ALOGV("releaseWakeLock_l() %s", mThreadName);
  850. if (mPowerManager != 0) {
  851. mPowerManager->releaseWakeLock(mWakeLockToken, 0,
  852. true /* FIXME force oneway contrary to .aidl */);
  853. }
  854. mWakeLockToken.clear();
  855. }
  856. }
  857. void AudioFlinger::ThreadBase::getPowerManager_l() {
  858. if (mSystemReady && mPowerManager == 0) {
  859. // use checkService() to avoid blocking if power service is not up yet
  860. sp<IBinder> binder =
  861. defaultServiceManager()->checkService(String16("power"));
  862. if (binder == 0) {
  863. ALOGW("Thread %s cannot connect to the power manager service", mThreadName);
  864. } else {
  865. mPowerManager = interface_cast<IPowerManager>(binder);
  866. binder->linkToDeath(mDeathRecipient);
  867. }
  868. }
  869. }
  870. void AudioFlinger::ThreadBase::updateWakeLockUids_l(const SortedVector<uid_t> &uids) {
  871. getPowerManager_l();
  872. #if !LOG_NDEBUG
  873. std::stringstream s;
  874. for (uid_t uid : uids) {
  875. s << uid << " ";
  876. }
  877. ALOGD("updateWakeLockUids_l %s uids:%s", mThreadName, s.str().c_str());
  878. #endif
  879. if (mWakeLockToken == NULL) { // token may be NULL if AudioFlinger::systemReady() not called.
  880. if (mSystemReady) {
  881. ALOGE("no wake lock to update, but system ready!");
  882. } else {
  883. ALOGW("no wake lock to update, system not ready yet");
  884. }
  885. return;
  886. }
  887. if (mPowerManager != 0) {
  888. std::vector<int> uidsAsInt(uids.begin(), uids.end()); // powermanager expects uids as ints
  889. status_t status = mPowerManager->updateWakeLockUids(
  890. mWakeLockToken, uidsAsInt.size(), uidsAsInt.data(),
  891. true /* FIXME force oneway contrary to .aidl */);
  892. ALOGV("updateWakeLockUids_l() %s status %d", mThreadName, status);
  893. }
  894. }
  895. void AudioFlinger::ThreadBase::clearPowerManager()
  896. {
  897. Mutex::Autolock _l(mLock);
  898. releaseWakeLock_l();
  899. mPowerManager.clear();
  900. }
  901. void AudioFlinger::ThreadBase::PMDeathRecipient::binderDied(const wp<IBinder>& who __unused)
  902. {
  903. sp<ThreadBase> thread = mThread.promote();
  904. if (thread != 0) {
  905. thread->clearPowerManager();
  906. }
  907. ALOGW("power manager service died !!!");
  908. }
  909. void AudioFlinger::ThreadBase::setEffectSuspended_l(
  910. const effect_uuid_t *type, bool suspend, audio_session_t sessionId)
  911. {
  912. sp<EffectChain> chain = getEffectChain_l(sessionId);
  913. if (chain != 0) {
  914. if (type != NULL) {
  915. chain->setEffectSuspended_l(type, suspend);
  916. } else {
  917. chain->setEffectSuspendedAll_l(suspend);
  918. }
  919. }
  920. updateSuspendedSessions_l(type, suspend, sessionId);
  921. }
  922. void AudioFlinger::ThreadBase::checkSuspendOnAddEffectChain_l(const sp<EffectChain>& chain)
  923. {
  924. ssize_t index = mSuspendedSessions.indexOfKey(chain->sessionId());
  925. if (index < 0) {
  926. return;
  927. }
  928. const KeyedVector <int, sp<SuspendedSessionDesc> >& sessionEffects =
  929. mSuspendedSessions.valueAt(index);
  930. for (size_t i = 0; i < sessionEffects.size(); i++) {
  931. const sp<SuspendedSessionDesc>& desc = sessionEffects.valueAt(i);
  932. for (int j = 0; j < desc->mRefCount; j++) {
  933. if (sessionEffects.keyAt(i) == EffectChain::kKeyForSuspendAll) {
  934. chain->setEffectSuspendedAll_l(true);
  935. } else {
  936. ALOGV("checkSuspendOnAddEffectChain_l() suspending effects %08x",
  937. desc->mType.timeLow);
  938. chain->setEffectSuspended_l(&desc->mType, true);
  939. }
  940. }
  941. }
  942. }
  943. void AudioFlinger::ThreadBase::updateSuspendedSessions_l(const effect_uuid_t *type,
  944. bool suspend,
  945. audio_session_t sessionId)
  946. {
  947. ssize_t index = mSuspendedSessions.indexOfKey(sessionId);
  948. KeyedVector <int, sp<SuspendedSessionDesc> > sessionEffects;
  949. if (suspend) {
  950. if (index >= 0) {
  951. sessionEffects = mSuspendedSessions.valueAt(index);
  952. } else {
  953. mSuspendedSessions.add(sessionId, sessionEffects);
  954. }
  955. } else {
  956. if (index < 0) {
  957. return;
  958. }
  959. sessionEffects = mSuspendedSessions.valueAt(index);
  960. }
  961. int key = EffectChain::kKeyForSuspendAll;
  962. if (type != NULL) {
  963. key = type->timeLow;
  964. }
  965. index = sessionEffects.indexOfKey(key);
  966. sp<SuspendedSessionDesc> desc;
  967. if (suspend) {
  968. if (index >= 0) {
  969. desc = sessionEffects.valueAt(index);
  970. } else {
  971. desc = new SuspendedSessionDesc();
  972. if (type != NULL) {
  973. desc->mType = *type;
  974. }
  975. sessionEffects.add(key, desc);
  976. ALOGV("updateSuspendedSessions_l() suspend adding effect %08x", key);
  977. }
  978. desc->mRefCount++;
  979. } else {
  980. if (index < 0) {
  981. return;
  982. }
  983. desc = sessionEffects.valueAt(index);
  984. if (--desc->mRefCount == 0) {
  985. ALOGV("updateSuspendedSessions_l() restore removing effect %08x", key);
  986. sessionEffects.removeItemsAt(index);
  987. if (sessionEffects.isEmpty()) {
  988. ALOGV("updateSuspendedSessions_l() restore removing session %d",
  989. sessionId);
  990. mSuspendedSessions.removeItem(sessionId);
  991. }
  992. }
  993. }
  994. if (!sessionEffects.isEmpty()) {
  995. mSuspendedSessions.replaceValueFor(sessionId, sessionEffects);
  996. }
  997. }
  998. void AudioFlinger::ThreadBase::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
  999. bool enabled,
  1000. audio_session_t sessionId)
  1001. {
  1002. Mutex::Autolock _l(mLock);
  1003. checkSuspendOnEffectEnabled_l(effect, enabled, sessionId);
  1004. }
  1005. void AudioFlinger::ThreadBase::checkSuspendOnEffectEnabled_l(const sp<EffectModule>& effect,
  1006. bool enabled,
  1007. audio_session_t sessionId)
  1008. {
  1009. if (mType != RECORD) {
  1010. // suspend all effects in AUDIO_SESSION_OUTPUT_MIX when enabling any effect on
  1011. // another session. This gives the priority to well behaved effect control panels
  1012. // and applications not using global effects.
  1013. // Enabling post processing in AUDIO_SESSION_OUTPUT_STAGE session does not affect
  1014. // global effects
  1015. if ((sessionId != AUDIO_SESSION_OUTPUT_MIX) && (sessionId != AUDIO_SESSION_OUTPUT_STAGE)) {
  1016. setEffectSuspended_l(NULL, enabled, AUDIO_SESSION_OUTPUT_MIX);
  1017. }
  1018. }
  1019. sp<EffectChain> chain = getEffectChain_l(sessionId);
  1020. if (chain != 0) {
  1021. chain->checkSuspendOnEffectEnabled(effect, enabled);
  1022. }
  1023. }
  1024. // checkEffectCompatibility_l() must be called with ThreadBase::mLock held
  1025. status_t AudioFlinger::RecordThread::checkEffectCompatibility_l(
  1026. const effect_descriptor_t *desc, audio_session_t sessionId)
  1027. {
  1028. // No global effect sessions on record threads
  1029. if (sessionId == AUDIO_SESSION_OUTPUT_MIX || sessionId == AUDIO_SESSION_OUTPUT_STAGE) {
  1030. ALOGW("checkEffectCompatibility_l(): global effect %s on record thread %s",
  1031. desc->name, mThreadName);
  1032. return BAD_VALUE;
  1033. }
  1034. // only pre processing effects on record thread
  1035. if ((desc->flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_PRE_PROC) {
  1036. ALOGW("checkEffectCompatibility_l(): non pre processing effect %s on record thread %s",
  1037. desc->name, mThreadName);
  1038. return BAD_VALUE;
  1039. }
  1040. // always allow effects without processing load or latency
  1041. if ((desc->flags & EFFECT_FLAG_NO_PROCESS_MASK) == EFFECT_FLAG_NO_PROCESS) {
  1042. return NO_ERROR;
  1043. }
  1044. audio_input_flags_t flags = mInput->flags;
  1045. if (hasFastCapture() || (flags & AUDIO_INPUT_FLAG_FAST)) {
  1046. if (flags & AUDIO_INPUT_FLAG_RAW) {
  1047. ALOGW("checkEffectCompatibility_l(): effect %s on record thread %s in raw mode",
  1048. desc->name, mThreadName);
  1049. return BAD_VALUE;
  1050. }
  1051. if ((desc->flags & EFFECT_FLAG_HW_ACC_TUNNEL) == 0) {
  1052. ALOGW("checkEffectCompatibility_l(): non HW effect %s on record thread %s in fast mode",
  1053. desc->name, mThreadName);
  1054. return BAD_VALUE;
  1055. }
  1056. }
  1057. return NO_ERROR;
  1058. }
  1059. // checkEffectCompatibility_l() must be called with ThreadBase::mLock held
  1060. status_t AudioFlinger::PlaybackThread::checkEffectCompatibility_l(
  1061. const effect_descriptor_t *desc, audio_session_t sessionId)
  1062. {
  1063. // no preprocessing on playback threads
  1064. if ((desc->flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC) {
  1065. ALOGW("checkEffectCompatibility_l(): pre processing effect %s created on playback"
  1066. " thread %s", desc->name, mThreadName);
  1067. return BAD_VALUE;
  1068. }
  1069. // always allow effects without processing load or latency
  1070. if ((desc->flags & EFFECT_FLAG_NO_PROCESS_MASK) == EFFECT_FLAG_NO_PROCESS) {
  1071. return NO_ERROR;
  1072. }
  1073. switch (mType) {
  1074. case MIXER: {
  1075. #ifndef MULTICHANNEL_EFFECT_CHAIN
  1076. // Reject any effect on mixer multichannel sinks.
  1077. // TODO: fix both format and multichannel issues with effects.
  1078. if (mChannelCount != FCC_2) {
  1079. ALOGW("checkEffectCompatibility_l(): effect %s for multichannel(%d) on MIXER"
  1080. " thread %s", desc->name, mChannelCount, mThreadName);
  1081. return BAD_VALUE;
  1082. }
  1083. #endif
  1084. audio_output_flags_t flags = mOutput->flags;
  1085. if (hasFastMixer() || (flags & AUDIO_OUTPUT_FLAG_FAST)) {
  1086. if (sessionId == AUDIO_SESSION_OUTPUT_MIX) {
  1087. // global effects are applied only to non fast tracks if they are SW
  1088. if ((desc->flags & EFFECT_FLAG_HW_ACC_TUNNEL) == 0) {
  1089. break;
  1090. }
  1091. } else if (sessionId == AUDIO_SESSION_OUTPUT_STAGE) {
  1092. // only post processing on output stage session
  1093. if ((desc->flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_POST_PROC) {
  1094. ALOGW("checkEffectCompatibility_l(): non post processing effect %s not allowed"
  1095. " on output stage session", desc->name);
  1096. return BAD_VALUE;
  1097. }
  1098. } else {
  1099. // no restriction on effects applied on non fast tracks
  1100. if ((hasAudioSession_l(sessionId) & ThreadBase::FAST_SESSION) == 0) {
  1101. break;
  1102. }
  1103. }
  1104. if (flags & AUDIO_OUTPUT_FLAG_RAW) {
  1105. ALOGW("checkEffectCompatibility_l(): effect %s on playback thread in raw mode",
  1106. desc->name);
  1107. return BAD_VALUE;
  1108. }
  1109. if ((desc->flags & EFFECT_FLAG_HW_ACC_TUNNEL) == 0) {
  1110. ALOGW("checkEffectCompatibility_l(): non HW effect %s on playback thread"
  1111. " in fast mode", desc->name);
  1112. return BAD_VALUE;
  1113. }
  1114. }
  1115. } break;
  1116. case OFFLOAD:
  1117. // nothing actionable on offload threads, if the effect:
  1118. // - is offloadable: the effect can be created
  1119. // - is NOT offloadable: the effect should still be created, but EffectHandle::enable()
  1120. // will take care of invalidating the tracks of the thread
  1121. break;
  1122. case DIRECT:
  1123. // Reject any effect on Direct output threads for now, since the format of
  1124. // mSinkBuffer is not guaranteed to be compatible with effect processing (PCM 16 stereo).
  1125. ALOGW("checkEffectCompatibility_l(): effect %s on DIRECT output thread %s",
  1126. desc->name, mThreadName);
  1127. return BAD_VALUE;
  1128. case DUPLICATING:
  1129. #ifndef MULTICHANNEL_EFFECT_CHAIN
  1130. // Reject any effect on mixer multichannel sinks.
  1131. // TODO: fix both format and multichannel issues with effects.
  1132. if (mChannelCount != FCC_2) {
  1133. ALOGW("checkEffectCompatibility_l(): effect %s for multichannel(%d)"
  1134. " on DUPLICATING thread %s", desc->name, mChannelCount, mThreadName);
  1135. return BAD_VALUE;
  1136. }
  1137. #endif
  1138. if ((sessionId == AUDIO_SESSION_OUTPUT_STAGE) || (sessionId == AUDIO_SESSION_OUTPUT_MIX)) {
  1139. ALOGW("checkEffectCompatibility_l(): global effect %s on DUPLICATING"
  1140. " thread %s", desc->name, mThreadName);
  1141. return BAD_VALUE;
  1142. }
  1143. if ((desc->flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
  1144. ALOGW("checkEffectCompatibility_l(): post processing effect %s on"
  1145. " DUPLICATING thread %s", desc->name, mThreadName);
  1146. return BAD_VALUE;
  1147. }
  1148. if ((desc->flags & EFFECT_FLAG_HW_ACC_TUNNEL) != 0) {
  1149. ALOGW("checkEffectCompatibility_l(): HW tunneled effect %s on"
  1150. " DUPLICATING thread %s", desc->name, mThreadName);
  1151. return BAD_VALUE;
  1152. }
  1153. break;
  1154. default:
  1155. LOG_ALWAYS_FATAL("checkEffectCompatibility_l(): wrong thread type %d", mType);
  1156. }
  1157. return NO_ERROR;
  1158. }
  1159. // ThreadBase::createEffect_l() must be called with AudioFlinger::mLock held
  1160. sp<AudioFlinger::EffectHandle> AudioFlinger::ThreadBase::createEffect_l(
  1161. const sp<AudioFlinger::Client>& client,
  1162. const sp<IEffectClient>& effectClient,
  1163. int32_t priority,
  1164. audio_session_t sessionId,
  1165. effect_descriptor_t *desc,
  1166. int *enabled,
  1167. status_t *status,
  1168. bool pinned)
  1169. {
  1170. sp<EffectModule> effect;
  1171. sp<EffectHandle> handle;
  1172. status_t lStatus;
  1173. sp<EffectChain> chain;
  1174. bool chainCreated = false;
  1175. bool effectCreated = false;
  1176. audio_unique_id_t effectId = AUDIO_UNIQUE_ID_USE_UNSPECIFIED;
  1177. lStatus = initCheck();
  1178. if (lStatus != NO_ERROR) {
  1179. ALOGW("createEffect_l() Audio driver not initialized.");
  1180. goto Exit;
  1181. }
  1182. ALOGV("createEffect_l() thread %p effect %s on session %d", this, desc->name, sessionId);
  1183. { // scope for mLock
  1184. Mutex::Autolock _l(mLock);
  1185. lStatus = checkEffectCompatibility_l(desc, sessionId);
  1186. if (lStatus != NO_ERROR) {
  1187. goto Exit;
  1188. }
  1189. // check for existing effect chain with the requested audio session
  1190. chain = getEffectChain_l(sessionId);
  1191. if (chain == 0) {
  1192. // create a new chain for this session
  1193. ALOGV("createEffect_l() new effect chain for session %d", sessionId);
  1194. chain = new EffectChain(this, sessionId);
  1195. addEffectChain_l(chain);
  1196. chain->setStrategy(getStrategyForSession_l(sessionId));
  1197. chainCreated = true;
  1198. } else {
  1199. effect = chain->getEffectFromDesc_l(desc);
  1200. }
  1201. ALOGV("createEffect_l() got effect %p on chain %p", effect.get(), chain.get());
  1202. if (effect == 0) {
  1203. effectId = mAudioFlinger->nextUniqueId(AUDIO_UNIQUE_ID_USE_EFFECT);
  1204. // create a new effect module if none present in the chain
  1205. lStatus = chain->createEffect_l(effect, this, desc, effectId, sessionId, pinned);
  1206. if (lStatus != NO_ERROR) {
  1207. goto Exit;
  1208. }
  1209. effectCreated = true;
  1210. effect->setDevice(mOutDevice);
  1211. effect->setDevice(mInDevice);
  1212. effect->setMode(mAudioFlinger->getMode());
  1213. effect->setAudioSource(mAudioSource);
  1214. }
  1215. // create effect handle and connect it to effect module
  1216. handle = new EffectHandle(effect, client, effectClient, priority);
  1217. lStatus = handle->initCheck();
  1218. if (lStatus == OK) {
  1219. lStatus = effect->addHandle(handle.get());
  1220. }
  1221. if (enabled != NULL) {
  1222. *enabled = (int)effect->isEnabled();
  1223. }
  1224. }
  1225. Exit:
  1226. if (lStatus != NO_ERROR && lStatus != ALREADY_EXISTS) {
  1227. Mutex::Autolock _l(mLock);
  1228. if (effectCreated) {
  1229. chain->removeEffect_l(effect);
  1230. }
  1231. if (chainCreated) {
  1232. removeEffectChain_l(chain);
  1233. }
  1234. // handle must be cleared by caller to avoid deadlock.
  1235. }
  1236. *status = lStatus;
  1237. return handle;
  1238. }
  1239. void AudioFlinger::ThreadBase::disconnectEffectHandle(EffectHandle *handle,
  1240. bool unpinIfLast)
  1241. {
  1242. bool remove = false;
  1243. sp<EffectModule> effect;
  1244. {
  1245. Mutex::Autolock _l(mLock);
  1246. effect = handle->effect().promote();
  1247. if (effect == 0) {
  1248. return;
  1249. }
  1250. // restore suspended effects if the disconnected handle was enabled and the last one.
  1251. remove = (effect->removeHandle(handle) == 0) && (!effect->isPinned() || unpinIfLast);
  1252. if (remove) {
  1253. removeEffect_l(effect, true);
  1254. }
  1255. }
  1256. if (remove) {
  1257. mAudioFlinger->updateOrphanEffectChains(effect);
  1258. if (handle->enabled()) {
  1259. checkSuspendOnEffectEnabled(effect, false, effect->sessionId());
  1260. }
  1261. }
  1262. }
  1263. sp<AudioFlinger::EffectModule> AudioFlinger::ThreadBase::getEffect(audio_session_t sessionId,
  1264. int effectId)
  1265. {
  1266. Mutex::Autolock _l(mLock);
  1267. return getEffect_l(sessionId, effectId);
  1268. }
  1269. sp<AudioFlinger::EffectModule> AudioFlinger::ThreadBase::getEffect_l(audio_session_t sessionId,
  1270. int effectId)
  1271. {
  1272. sp<EffectChain> chain = getEffectChain_l(sessionId);
  1273. return chain != 0 ? chain->getEffectFromId_l(effectId) : 0;
  1274. }
  1275. std::vector<int> AudioFlinger::ThreadBase::getEffectIds_l(audio_session_t sessionId)
  1276. {
  1277. sp<EffectChain> chain = getEffectChain_l(sessionId);
  1278. return chain != nullptr ? chain->getEffectIds() : std::vector<int>{};
  1279. }
  1280. // PlaybackThread::addEffect_l() must be called with AudioFlinger::mLock and
  1281. // PlaybackThread::mLock held
  1282. status_t AudioFlinger::ThreadBase::addEffect_l(const sp<EffectModule>& effect)
  1283. {
  1284. // check for existing effect chain with the requested audio session
  1285. audio_session_t sessionId = effect->sessionId();
  1286. sp<EffectChain> chain = getEffectChain_l(sessionId);
  1287. bool chainCreated = false;
  1288. ALOGD_IF((mType == OFFLOAD) && !effect->isOffloadable(),
  1289. "addEffect_l() on offloaded thread %p: effect %s does not support offload flags %#x",
  1290. this, effect->desc().name, effect->desc().flags);
  1291. if (chain == 0) {
  1292. // create a new chain for this session
  1293. ALOGV("addEffect_l() new effect chain for session %d", sessionId);
  1294. chain = new EffectChain(this, sessionId);
  1295. addEffectChain_l(chain);
  1296. chain->setStrategy(getStrategyForSession_l(sessionId));
  1297. chainCreated = true;
  1298. }
  1299. ALOGV("addEffect_l() %p chain %p effect %p", this, chain.get(), effect.get());
  1300. if (chain->getEffectFromId_l(effect->id()) != 0) {
  1301. ALOGW("addEffect_l() %p effect %s already present in chain %p",
  1302. this, effect->desc().name, chain.get());
  1303. return BAD_VALUE;
  1304. }
  1305. effect->setOffloaded(mType == OFFLOAD, mId);
  1306. status_t status = chain->addEffect_l(effect);
  1307. if (status != NO_ERROR) {
  1308. if (chainCreated) {
  1309. removeEffectChain_l(chain);
  1310. }
  1311. return status;
  1312. }
  1313. effect->setDevice(mOutDevice);
  1314. effect->setDevice(mInDevice);
  1315. effect->setMode(mAudioFlinger->getMode());
  1316. effect->setAudioSource(mAudioSource);
  1317. return NO_ERROR;
  1318. }
  1319. void AudioFlinger::ThreadBase::removeEffect_l(const sp<EffectModule>& effect, bool release) {
  1320. ALOGV("%s %p effect %p", __FUNCTION__, this, effect.get());
  1321. effect_descriptor_t desc = effect->desc();
  1322. if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
  1323. detachAuxEffect_l(effect->id());
  1324. }
  1325. sp<EffectChain> chain = effect->chain().promote();
  1326. if (chain != 0) {
  1327. // remove effect chain if removing last effect
  1328. if (chain->removeEffect_l(effect, release) == 0) {
  1329. removeEffectChain_l(chain);
  1330. }
  1331. } else {
  1332. ALOGW("removeEffect_l() %p cannot promote chain for effect %p", this, effect.get());
  1333. }
  1334. }
  1335. void AudioFlinger::ThreadBase::lockEffectChains_l(
  1336. Vector< sp<AudioFlinger::EffectChain> >& effectChains)
  1337. {
  1338. effectChains = mEffectChains;
  1339. for (size_t i = 0; i < mEffectChains.size(); i++) {
  1340. mEffectChains[i]->lock();
  1341. }
  1342. }
  1343. void AudioFlinger::ThreadBase::unlockEffectChains(
  1344. const Vector< sp<AudioFlinger::EffectChain> >& effectChains)
  1345. {
  1346. for (size_t i = 0; i < effectChains.size(); i++) {
  1347. effectChains[i]->unlock();
  1348. }
  1349. }
  1350. sp<AudioFlinger::EffectChain> AudioFlinger::ThreadBase::getEffectChain(audio_session_t sessionId)
  1351. {
  1352. Mutex::Autolock _l(mLock);
  1353. return getEffectChain_l(sessionId);
  1354. }
  1355. sp<AudioFlinger::EffectChain> AudioFlinger::ThreadBase::getEffectChain_l(audio_session_t sessionId)
  1356. const
  1357. {
  1358. size_t size = mEffectChains.size();
  1359. for (size_t i = 0; i < size; i++) {
  1360. if (mEffectChains[i]->sessionId() == sessionId) {
  1361. return mEffectChains[i];
  1362. }
  1363. }
  1364. return 0;
  1365. }
  1366. void AudioFlinger::ThreadBase::setMode(audio_mode_t mode)
  1367. {
  1368. Mutex::Autolock _l(mLock);
  1369. size_t size = mEffectChains.size();
  1370. for (size_t i = 0; i < size; i++) {
  1371. mEffectChains[i]->setMode_l(mode);
  1372. }
  1373. }
  1374. void AudioFlinger::ThreadBase::toAudioPortConfig(struct audio_port_config *config)
  1375. {
  1376. config->type = AUDIO_PORT_TYPE_MIX;
  1377. config->ext.mix.handle = mId;
  1378. config->sample_rate = mSampleRate;
  1379. config->format = mFormat;
  1380. config->channel_mask = mChannelMask;
  1381. config->config_mask = AUDIO_PORT_CONFIG_SAMPLE_RATE|AUDIO_PORT_CONFIG_CHANNEL_MASK|
  1382. AUDIO_PORT_CONFIG_FORMAT;
  1383. }
  1384. void AudioFlinger::ThreadBase::systemReady()
  1385. {
  1386. Mutex::Autolock _l(mLock);
  1387. if (mSystemReady) {
  1388. return;
  1389. }
  1390. mSystemReady = true;
  1391. for (size_t i = 0; i < mPendingConfigEvents.size(); i++) {
  1392. sendConfigEvent_l(mPendingConfigEvents.editItemAt(i));
  1393. }
  1394. mPendingConfigEvents.clear();
  1395. }
  1396. template <typename T>
  1397. ssize_t AudioFlinger::ThreadBase::ActiveTracks<T>::add(const sp<T> &track) {
  1398. ssize_t index = mActiveTracks.indexOf(track);
  1399. if (index >= 0) {
  1400. ALOGW("ActiveTracks<T>::add track %p already there", track.get());
  1401. return index;
  1402. }
  1403. logTrack("add", track);
  1404. mActiveTracksGeneration++;
  1405. mLatestActiveTrack = track;
  1406. ++mBatteryCounter[track->uid()].second;
  1407. mHasChanged = true;
  1408. return mActiveTracks.add(track);
  1409. }
  1410. template <typename T>
  1411. ssize_t AudioFlinger::ThreadBase::ActiveTracks<T>::remove(const sp<T> &track) {
  1412. ssize_t index = mActiveTracks.remove(track);
  1413. if (index < 0) {
  1414. ALOGW("ActiveTracks<T>::remove nonexistent track %p", track.get());
  1415. return index;
  1416. }
  1417. logTrack("remove", track);
  1418. mActiveTracksGeneration++;
  1419. --mBatteryCounter[track->uid()].second;
  1420. // mLatestActiveTrack is not cleared even if is the same as track.
  1421. mHasChanged = true;
  1422. #ifdef TEE_SINK
  1423. track->dumpTee(-1 /* fd */, "_REMOVE");
  1424. #endif
  1425. return index;
  1426. }
  1427. template <typename T>
  1428. void AudioFlinger::ThreadBase::ActiveTracks<T>::clear() {
  1429. for (const sp<T> &track : mActiveTracks) {
  1430. BatteryNotifier::getInstance().noteStopAudio(track->uid());
  1431. logTrack("clear", track);
  1432. }
  1433. mLastActiveTracksGeneration = mActiveTracksGeneration;
  1434. if (!mActiveTracks.empty()) { mHasChanged = true; }
  1435. mActiveTracks.clear();
  1436. mLatestActiveTrack.clear();
  1437. mBatteryCounter.clear();
  1438. }
  1439. template <typename T>
  1440. void AudioFlinger::ThreadBase::ActiveTracks<T>::updatePowerState(
  1441. sp<ThreadBase> thread, bool force) {
  1442. // Updates ActiveTracks client uids to the thread wakelock.
  1443. if (mActiveTracksGeneration != mLastActiveTracksGeneration || force) {
  1444. thread->updateWakeLockUids_l(getWakeLockUids());
  1445. mLastActiveTracksGeneration = mActiveTracksGeneration;
  1446. }
  1447. // Updates BatteryNotifier uids
  1448. for (auto it = mBatteryCounter.begin(); it != mBatteryCounter.end();) {
  1449. const uid_t uid = it->first;
  1450. ssize_t &previous = it->second.first;
  1451. ssize_t &current = it->second.second;
  1452. if (current > 0) {
  1453. if (previous == 0) {
  1454. BatteryNotifier::getInstance().noteStartAudio(uid);
  1455. }
  1456. previous = current;
  1457. ++it;
  1458. } else if (current == 0) {
  1459. if (previous > 0) {
  1460. BatteryNotifier::getInstance().noteStopAudio(uid);
  1461. }
  1462. it = mBatteryCounter.erase(it); // std::map<> is stable on iterator erase.
  1463. } else /* (current < 0) */ {
  1464. LOG_ALWAYS_FATAL("negative battery count %zd", current);
  1465. }
  1466. }
  1467. }
  1468. template <typename T>
  1469. bool AudioFlinger::ThreadBase::ActiveTracks<T>::readAndClearHasChanged() {
  1470. const bool hasChanged = mHasChanged;
  1471. mHasChanged = false;
  1472. return hasChanged;
  1473. }
  1474. template <typename T>
  1475. void AudioFlinger::ThreadBase::ActiveTracks<T>::logTrack(
  1476. const char *funcName, const sp<T> &track) const {
  1477. if (mLocalLog != nullptr) {
  1478. String8 result;
  1479. track->appendDump(result, false /* active */);
  1480. mLocalLog->log("AT::%-10s(%p) %s", funcName, track.get(), result.string());
  1481. }
  1482. }
  1483. void AudioFlinger::ThreadBase::broadcast_l()
  1484. {
  1485. // Thread could be blocked waiting for async
  1486. // so signal it to handle state changes immediately
  1487. // If threadLoop is currently unlocked a signal of mWaitWorkCV will
  1488. // be lost so we also flag to prevent it blocking on mWaitWorkCV
  1489. mSignalPending = true;
  1490. mWaitWorkCV.broadcast();
  1491. }
  1492. // Call only from threadLoop() or when it is idle.
  1493. // Do not call from high performance code as this may do binder rpc to the MediaMetrics service.
  1494. void AudioFlinger::ThreadBase::sendStatistics(bool force)
  1495. {
  1496. // Do not log if we have no stats.
  1497. // We choose the timestamp verifier because it is the most likely item to be present.
  1498. const int64_t nstats = mTimestampVerifier.getN() - mLastRecordedTimestampVerifierN;
  1499. if (nstats == 0) {
  1500. return;
  1501. }
  1502. // Don't log more frequently than once per 12 hours.
  1503. // We use BOOTTIME to include suspend time.
  1504. const int64_t timeNs = systemTime(SYSTEM_TIME_BOOTTIME);
  1505. const int64_t sinceNs = timeNs - mLastRecordedTimeNs; // ok if mLastRecordedTimeNs = 0
  1506. if (!force && sinceNs <= 12 * NANOS_PER_HOUR) {
  1507. return;
  1508. }
  1509. mLastRecordedTimestampVerifierN = mTimestampVerifier.getN();
  1510. mLastRecordedTimeNs = timeNs;
  1511. std::unique_ptr<MediaAnalyticsItem> item(MediaAnalyticsItem::create("audiothread"));
  1512. #define MM_PREFIX "android.media.audiothread." // avoid cut-n-paste errors.
  1513. // thread configuration
  1514. item->setInt32(MM_PREFIX "id", (int32_t)mId); // IO handle
  1515. // item->setInt32(MM_PREFIX "portId", (int32_t)mPortId);
  1516. item->setCString(MM_PREFIX "type", threadTypeToString(mType));
  1517. item->setInt32(MM_PREFIX "sampleRate", (int32_t)mSampleRate);
  1518. item->setInt64(MM_PREFIX "channelMask", (int64_t)mChannelMask);
  1519. item->setCString(MM_PREFIX "encoding", toString(mFormat).c_str());
  1520. item->setInt32(MM_PREFIX "frameCount", (int32_t)mFrameCount);
  1521. item->setCString(MM_PREFIX "outDevice", toString(mOutDevice).c_str());
  1522. item->setCString(MM_PREFIX "inDevice", toString(mInDevice).c_str());
  1523. // thread statistics
  1524. if (mIoJitterMs.getN() > 0) {
  1525. item->setDouble(MM_PREFIX "ioJitterMs.mean", mIoJitterMs.getMean());
  1526. item->setDouble(MM_PREFIX "ioJitterMs.std", mIoJitterMs.getStdDev());
  1527. }
  1528. if (mProcessTimeMs.getN() > 0) {
  1529. item->setDouble(MM_PREFIX "processTimeMs.mean", mProcessTimeMs.getMean());
  1530. item->setDouble(MM_PREFIX "processTimeMs.std", mProcessTimeMs.getStdDev());
  1531. }
  1532. const auto tsjitter = mTimestampVerifier.getJitterMs();
  1533. if (tsjitter.getN() > 0) {
  1534. item->setDouble(MM_PREFIX "timestampJitterMs.mean", tsjitter.getMean());
  1535. item->setDouble(MM_PREFIX "timestampJitterMs.std", tsjitter.getStdDev());
  1536. }
  1537. if (mLatencyMs.getN() > 0) {
  1538. item->setDouble(MM_PREFIX "latencyMs.mean", mLatencyMs.getMean());
  1539. item->setDouble(MM_PREFIX "latencyMs.std", mLatencyMs.getStdDev());
  1540. }
  1541. item->selfrecord();
  1542. }
  1543. // ----------------------------------------------------------------------------
  1544. // Playback
  1545. // ----------------------------------------------------------------------------
  1546. AudioFlinger::PlaybackThread::PlaybackThread(const sp<AudioFlinger>& audioFlinger,
  1547. AudioStreamOut* output,
  1548. audio_io_handle_t id,
  1549. audio_devices_t device,
  1550. type_t type,
  1551. bool systemReady)
  1552. : ThreadBase(audioFlinger, id, device, AUDIO_DEVICE_NONE, type, systemReady),
  1553. mNormalFrameCount(0), mSinkBuffer(NULL),
  1554. mMixerBufferEnabled(AudioFlinger::kEnableExtendedPrecision),
  1555. mMixerBuffer(NULL),
  1556. mMixerBufferSize(0),
  1557. mMixerBufferFormat(AUDIO_FORMAT_INVALID),
  1558. mMixerBufferValid(false),
  1559. mEffectBufferEnabled(AudioFlinger::kEnableExtendedPrecision),
  1560. mEffectBuffer(NULL),
  1561. mEffectBufferSize(0),
  1562. mEffectBufferFormat(AUDIO_FORMAT_INVALID),
  1563. mEffectBufferValid(false),
  1564. mSuspended(0), mBytesWritten(0),
  1565. mFramesWritten(0),
  1566. mSuspendedFrames(0),
  1567. mActiveTracks(&this->mLocalLog),
  1568. // mStreamTypes[] initialized in constructor body
  1569. mTracks(type == MIXER),
  1570. mOutput(output),
  1571. mNumWrites(0), mNumDelayedWrites(0), mInWrite(false),
  1572. mMixerStatus(MIXER_IDLE),
  1573. mMixerStatusIgnoringFastTracks(MIXER_IDLE),
  1574. mStandbyDelayNs(AudioFlinger::mStandbyTimeInNsecs),
  1575. mBytesRemaining(0),
  1576. mCurrentWriteLength(0),
  1577. mUseAsyncWrite(false),
  1578. mWriteAckSequence(0),
  1579. mDrainSequence(0),
  1580. mScreenState(AudioFlinger::mScreenState),
  1581. // index 0 is reserved for normal mixer's submix
  1582. mFastTrackAvailMask(((1 << FastMixerState::sMaxFastTracks) - 1) & ~1),
  1583. mHwSupportsPause(false), mHwPaused(false), mFlushPending(false),
  1584. mLeftVolFloat(-1.0), mRightVolFloat(-1.0)
  1585. {
  1586. snprintf(mThreadName, kThreadNameLength, "AudioOut_%X", id);
  1587. mNBLogWriter = audioFlinger->newWriter_l(kLogSize, mThreadName);
  1588. // Assumes constructor is called by AudioFlinger with it's mLock held, but
  1589. // it would be safer to explicitly pass initial masterVolume/masterMute as
  1590. // parameter.
  1591. //
  1592. // If the HAL we are using has support for master volume or master mute,
  1593. // then do not attenuate or mute during mixing (just leave the volume at 1.0
  1594. // and the mute set to false).
  1595. mMasterVolume = audioFlinger->masterVolume_l();
  1596. mMasterMute = audioFlinger->masterMute_l();
  1597. if (mOutput && mOutput->audioHwDev) {
  1598. if (mOutput->audioHwDev->canSetMasterVolume()) {
  1599. mMasterVolume = 1.0;
  1600. }
  1601. if (mOutput->audioHwDev->canSetMasterMute()) {
  1602. mMasterMute = false;
  1603. }
  1604. mIsMsdDevice = strcmp(
  1605. mOutput->audioHwDev->moduleName(), AUDIO_HARDWARE_MODULE_ID_MSD) == 0;
  1606. }
  1607. readOutputParameters_l();
  1608. // TODO: We may also match on address as well as device type for
  1609. // AUDIO_DEVICE_OUT_BUS, AUDIO_DEVICE_OUT_ALL_A2DP, AUDIO_DEVICE_OUT_REMOTE_SUBMIX
  1610. if (type == MIXER || type == DIRECT) {
  1611. mTimestampCorrectedDevices = (audio_devices_t)property_get_int64(
  1612. "audio.timestamp.corrected_output_devices",
  1613. (int64_t)(mIsMsdDevice ? AUDIO_DEVICE_OUT_BUS // turn on by default for MSD
  1614. : AUDIO_DEVICE_NONE));
  1615. }
  1616. // ++ operator does not compile
  1617. for (audio_stream_type_t stream = AUDIO_STREAM_MIN; stream < AUDIO_STREAM_FOR_POLICY_CNT;
  1618. stream = (audio_stream_type_t) (stream + 1)) {
  1619. mStreamTypes[stream].volume = 0.0f;
  1620. mStreamTypes[stream].mute = mAudioFlinger->streamMute_l(stream);
  1621. }
  1622. // Audio patch volume is always max
  1623. mStreamTypes[AUDIO_STREAM_PATCH].volume = 1.0f;
  1624. mStreamTypes[AUDIO_STREAM_PATCH].mute = false;
  1625. }
  1626. AudioFlinger::PlaybackThread::~PlaybackThread()
  1627. {
  1628. mAudioFlinger->unregisterWriter(mNBLogWriter);
  1629. free(mSinkBuffer);
  1630. free(mMixerBuffer);
  1631. free(mEffectBuffer);
  1632. }
  1633. // Thread virtuals
  1634. void AudioFlinger::PlaybackThread::onFirstRef()
  1635. {
  1636. run(mThreadName, ANDROID_PRIORITY_URGENT_AUDIO);
  1637. }
  1638. // ThreadBase virtuals
  1639. void AudioFlinger::PlaybackThread::preExit()
  1640. {
  1641. ALOGV(" preExit()");
  1642. // FIXME this is using hard-coded strings but in the future, this functionality will be
  1643. // converted to use audio HAL extensions required to support tunneling
  1644. status_t result = mOutput->stream->setParameters(String8("exiting=1"));
  1645. ALOGE_IF(result != OK, "Error when setting parameters on exit: %d", result);
  1646. }
  1647. void AudioFlinger::PlaybackThread::dumpTracks_l(int fd, const Vector<String16>& args __unused)
  1648. {
  1649. String8 result;
  1650. result.appendFormat(" Stream volumes in dB: ");
  1651. for (int i = 0; i < AUDIO_STREAM_CNT; ++i) {
  1652. const stream_type_t *st = &mStreamTypes[i];
  1653. if (i > 0) {
  1654. result.appendFormat(", ");
  1655. }
  1656. result.appendFormat("%d:%.2g", i, 20.0 * log10(st->volume));
  1657. if (st->mute) {
  1658. result.append("M");
  1659. }
  1660. }
  1661. result.append("\n");
  1662. write(fd, result.string(), result.length());
  1663. result.clear();
  1664. // These values are "raw"; they will wrap around. See prepareTracks_l() for a better way.
  1665. FastTrackUnderruns underruns = getFastTrackUnderruns(0);
  1666. dprintf(fd, " Normal mixer raw underrun counters: partial=%u empty=%u\n",
  1667. underruns.mBitFields.mPartial, underruns.mBitFields.mEmpty);
  1668. size_t numtracks = mTracks.size();
  1669. size_t numactive = mActiveTracks.size();
  1670. dprintf(fd, " %zu Tracks", numtracks);
  1671. size_t numactiveseen = 0;
  1672. const char *prefix = " ";
  1673. if (numtracks) {
  1674. dprintf(fd, " of which %zu are active\n", numactive);
  1675. result.append(prefix);
  1676. mTracks[0]->appendDumpHeader(result);
  1677. for (size_t i = 0; i < numtracks; ++i) {
  1678. sp<Track> track = mTracks[i];
  1679. if (track != 0) {
  1680. bool active = mActiveTracks.indexOf(track) >= 0;
  1681. if (active) {
  1682. numactiveseen++;
  1683. }
  1684. result.append(prefix);
  1685. track->appendDump(result, active);
  1686. }
  1687. }
  1688. } else {
  1689. result.append("\n");
  1690. }
  1691. if (numactiveseen != numactive) {
  1692. // some tracks in the active list were not in the tracks list
  1693. result.append(" The following tracks are in the active list but"
  1694. " not in the track list\n");
  1695. result.append(prefix);
  1696. mActiveTracks[0]->appendDumpHeader(result);
  1697. for (size_t i = 0; i < numactive; ++i) {
  1698. sp<Track> track = mActiveTracks[i];
  1699. if (mTracks.indexOf(track) < 0) {
  1700. result.append(prefix);
  1701. track->appendDump(result, true /* active */);
  1702. }
  1703. }
  1704. }
  1705. write(fd, result.string(), result.size());
  1706. }
  1707. void AudioFlinger::PlaybackThread::dumpInternals_l(int fd, const Vector<String16>& args __unused)
  1708. {
  1709. dprintf(fd, " Master mute: %s\n", mMasterMute ? "on" : "off");
  1710. if (mHapticChannelMask != AUDIO_CHANNEL_NONE) {
  1711. dprintf(fd, " Haptic channel mask: %#x (%s)\n", mHapticChannelMask,
  1712. channelMaskToString(mHapticChannelMask, true /* output */).c_str());
  1713. }
  1714. dprintf(fd, " Normal frame count: %zu\n", mNormalFrameCount);
  1715. dprintf(fd, " Total writes: %d\n", mNumWrites);
  1716. dprintf(fd, " Delayed writes: %d\n", mNumDelayedWrites);
  1717. dprintf(fd, " Blocked in write: %s\n", mInWrite ? "yes" : "no");
  1718. dprintf(fd, " Suspend count: %d\n", mSuspended);
  1719. dprintf(fd, " Sink buffer : %p\n", mSinkBuffer);
  1720. dprintf(fd, " Mixer buffer: %p\n", mMixerBuffer);
  1721. dprintf(fd, " Effect buffer: %p\n", mEffectBuffer);
  1722. dprintf(fd, " Fast track availMask=%#x\n", mFastTrackAvailMask);
  1723. dprintf(fd, " Standby delay ns=%lld\n", (long long)mStandbyDelayNs);
  1724. AudioStreamOut *output = mOutput;
  1725. audio_output_flags_t flags = output != NULL ? output->flags : AUDIO_OUTPUT_FLAG_NONE;
  1726. dprintf(fd, " AudioStreamOut: %p flags %#x (%s)\n",
  1727. output, flags, toString(flags).c_str());
  1728. dprintf(fd, " Frames written: %lld\n", (long long)mFramesWritten);
  1729. dprintf(fd, " Suspended frames: %lld\n", (long long)mSuspendedFrames);
  1730. if (mPipeSink.get() != nullptr) {
  1731. dprintf(fd, " PipeSink frames written: %lld\n", (long long)mPipeSink->framesWritten());
  1732. }
  1733. if (output != nullptr) {
  1734. dprintf(fd, " Hal stream dump:\n");
  1735. (void)output->stream->dump(fd);
  1736. }
  1737. }
  1738. // PlaybackThread::createTrack_l() must be called with AudioFlinger::mLock held
  1739. sp<AudioFlinger::PlaybackThread::Track> AudioFlinger::PlaybackThread::createTrack_l(
  1740. const sp<AudioFlinger::Client>& client,
  1741. audio_stream_type_t streamType,
  1742. const audio_attributes_t& attr,
  1743. uint32_t *pSampleRate,
  1744. audio_format_t format,
  1745. audio_channel_mask_t channelMask,
  1746. size_t *pFrameCount,
  1747. size_t *pNotificationFrameCount,
  1748. uint32_t notificationsPerBuffer,
  1749. float speed,
  1750. const sp<IMemory>& sharedBuffer,
  1751. audio_session_t sessionId,
  1752. audio_output_flags_t *flags,
  1753. pid_t creatorPid,
  1754. pid_t tid,
  1755. uid_t uid,
  1756. status_t *status,
  1757. audio_port_handle_t portId)
  1758. {
  1759. size_t frameCount = *pFrameCount;
  1760. size_t notificationFrameCount = *pNotificationFrameCount;
  1761. sp<Track> track;
  1762. status_t lStatus;
  1763. audio_output_flags_t outputFlags = mOutput->flags;
  1764. audio_output_flags_t requestedFlags = *flags;
  1765. uint32_t sampleRate;
  1766. if (sharedBuffer != 0 && checkIMemory(sharedBuffer) != NO_ERROR) {
  1767. lStatus = BAD_VALUE;
  1768. goto Exit;
  1769. }
  1770. if (*pSampleRate == 0) {
  1771. *pSampleRate = mSampleRate;
  1772. }
  1773. sampleRate = *pSampleRate;
  1774. // special case for FAST flag considered OK if fast mixer is present
  1775. if (hasFastMixer()) {
  1776. outputFlags = (audio_output_flags_t)(outputFlags | AUDIO_OUTPUT_FLAG_FAST);
  1777. }
  1778. // Check if requested flags are compatible with output stream flags
  1779. if ((*flags & outputFlags) != *flags) {
  1780. ALOGW("createTrack_l(): mismatch between requested flags (%08x) and output flags (%08x)",
  1781. *flags, outputFlags);
  1782. *flags = (audio_output_flags_t)(*flags & outputFlags);
  1783. }
  1784. // client expresses a preference for FAST, but we get the final say
  1785. if (*flags & AUDIO_OUTPUT_FLAG_FAST) {
  1786. if (
  1787. // PCM data
  1788. audio_is_linear_pcm(format) &&
  1789. // TODO: extract as a data library function that checks that a computationally
  1790. // expensive downmixer is not required: isFastOutputChannelConversion()
  1791. (channelMask == (mChannelMask | mHapticChannelMask) ||
  1792. mChannelMask != AUDIO_CHANNEL_OUT_STEREO ||
  1793. (channelMask == AUDIO_CHANNEL_OUT_MONO
  1794. /* && mChannelMask == AUDIO_CHANNEL_OUT_STEREO */)) &&
  1795. // hardware sample rate
  1796. (sampleRate == mSampleRate) &&
  1797. // normal mixer has an associated fast mixer
  1798. hasFastMixer() &&
  1799. // there are sufficient fast track slots available
  1800. (mFastTrackAvailMask != 0)
  1801. // FIXME test that MixerThread for this fast track has a capable output HAL
  1802. // FIXME add a permission test also?
  1803. ) {
  1804. // static tracks can have any nonzero framecount, streaming tracks check against minimum.
  1805. if (sharedBuffer == 0) {
  1806. // read the fast track multiplier property the first time it is needed
  1807. int ok = pthread_once(&sFastTrackMultiplierOnce, sFastTrackMultiplierInit);
  1808. if (ok != 0) {
  1809. ALOGE("%s pthread_once failed: %d", __func__, ok);
  1810. }
  1811. frameCount = max(frameCount, mFrameCount * sFastTrackMultiplier); // incl framecount 0
  1812. }
  1813. // check compatibility with audio effects.
  1814. { // scope for mLock
  1815. Mutex::Autolock _l(mLock);
  1816. for (audio_session_t session : {
  1817. AUDIO_SESSION_OUTPUT_STAGE,
  1818. AUDIO_SESSION_OUTPUT_MIX,
  1819. sessionId,
  1820. }) {
  1821. sp<EffectChain> chain = getEffectChain_l(session);
  1822. if (chain.get() != nullptr) {
  1823. audio_output_flags_t old = *flags;
  1824. chain->checkOutputFlagCompatibility(flags);
  1825. if (old != *flags) {
  1826. ALOGV("AUDIO_OUTPUT_FLAGS denied by effect, session=%d old=%#x new=%#x",
  1827. (int)session, (int)old, (int)*flags);
  1828. }
  1829. }
  1830. }
  1831. }
  1832. ALOGV_IF((*flags & AUDIO_OUTPUT_FLAG_FAST) != 0,
  1833. "AUDIO_OUTPUT_FLAG_FAST accepted: frameCount=%zu mFrameCount=%zu",
  1834. frameCount, mFrameCount);
  1835. } else {
  1836. ALOGV("AUDIO_OUTPUT_FLAG_FAST denied: sharedBuffer=%p frameCount=%zu "
  1837. "mFrameCount=%zu format=%#x mFormat=%#x isLinear=%d channelMask=%#x "
  1838. "sampleRate=%u mSampleRate=%u "
  1839. "hasFastMixer=%d tid=%d fastTrackAvailMask=%#x",
  1840. sharedBuffer.get(), frameCount, mFrameCount, format, mFormat,
  1841. audio_is_linear_pcm(format),
  1842. channelMask, sampleRate, mSampleRate, hasFastMixer(), tid, mFastTrackAvailMask);
  1843. *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_FAST);
  1844. }
  1845. }
  1846. if (!audio_has_proportional_frames(format)) {
  1847. if (sharedBuffer != 0) {
  1848. // Same comment as below about ignoring frameCount parameter for set()
  1849. frameCount = sharedBuffer->size();
  1850. } else if (frameCount == 0) {
  1851. frameCount = mNormalFrameCount;
  1852. }
  1853. if (notificationFrameCount != frameCount) {
  1854. notificationFrameCount = frameCount;
  1855. }
  1856. } else if (sharedBuffer != 0) {
  1857. // FIXME: Ensure client side memory buffers need
  1858. // not have additional alignment beyond sample
  1859. // (e.g. 16 bit stereo accessed as 32 bit frame).
  1860. size_t alignment = audio_bytes_per_sample(format);
  1861. if (alignment & 1) {
  1862. // for AUDIO_FORMAT_PCM_24_BIT_PACKED (not exposed through Java).
  1863. alignment = 1;
  1864. }
  1865. uint32_t channelCount = audio_channel_count_from_out_mask(channelMask);
  1866. size_t frameSize = channelCount * audio_bytes_per_sample(format);
  1867. if (channelCount > 1) {
  1868. // More than 2 channels does not require stronger alignment than stereo
  1869. alignment <<= 1;
  1870. }
  1871. if (((uintptr_t)sharedBuffer->pointer() & (alignment - 1)) != 0) {
  1872. ALOGE("Invalid buffer alignment: address %p, channel count %u",
  1873. sharedBuffer->pointer(), channelCount);
  1874. lStatus = BAD_VALUE;
  1875. goto Exit;
  1876. }
  1877. // When initializing a shared buffer AudioTrack via constructors,
  1878. // there's no frameCount parameter.
  1879. // But when initializing a shared buffer AudioTrack via set(),
  1880. // there _is_ a frameCount parameter. We silently ignore it.
  1881. frameCount = sharedBuffer->size() / frameSize;
  1882. } else {
  1883. size_t minFrameCount = 0;
  1884. // For fast tracks we try to respect the application's request for notifications per buffer.
  1885. if (*flags & AUDIO_OUTPUT_FLAG_FAST) {
  1886. if (notificationsPerBuffer > 0) {
  1887. // Avoid possible arithmetic overflow during multiplication.
  1888. if (notificationsPerBuffer > SIZE_MAX / mFrameCount) {
  1889. ALOGE("Requested notificationPerBuffer=%u ignored for HAL frameCount=%zu",
  1890. notificationsPerBuffer, mFrameCount);
  1891. } else {
  1892. minFrameCount = mFrameCount * notificationsPerBuffer;
  1893. }
  1894. }
  1895. } else {
  1896. // For normal PCM streaming tracks, update minimum frame count.
  1897. // Buffer depth is forced to be at least 2 x the normal mixer frame count and
  1898. // cover audio hardware latency.
  1899. // This is probably too conservative, but legacy application code may depend on it.
  1900. // If you change this calculation, also review the start threshold which is related.
  1901. uint32_t latencyMs = latency_l();
  1902. if (latencyMs == 0) {
  1903. ALOGE("Error when retrieving output stream latency");
  1904. lStatus = UNKNOWN_ERROR;
  1905. goto Exit;
  1906. }
  1907. minFrameCount = AudioSystem::calculateMinFrameCount(latencyMs, mNormalFrameCount,
  1908. mSampleRate, sampleRate, speed /*, 0 mNotificationsPerBufferReq*/);
  1909. }
  1910. if (frameCount < minFrameCount) {
  1911. frameCount = minFrameCount;
  1912. }
  1913. }
  1914. // Make sure that application is notified with sufficient margin before underrun.
  1915. // The client can divide the AudioTrack buffer into sub-buffers,
  1916. // and expresses its desire to server as the notification frame count.
  1917. if (sharedBuffer == 0 && audio_is_linear_pcm(format)) {
  1918. size_t maxNotificationFrames;
  1919. if (*flags & AUDIO_OUTPUT_FLAG_FAST) {
  1920. // notify every HAL buffer, regardless of the size of the track buffer
  1921. maxNotificationFrames = mFrameCount;
  1922. } else {
  1923. // Triple buffer the notification period for a triple buffered mixer period;
  1924. // otherwise, double buffering for the notification period is fine.
  1925. //
  1926. // TODO: This should be moved to AudioTrack to modify the notification period
  1927. // on AudioTrack::setBufferSizeInFrames() changes.
  1928. const int nBuffering =
  1929. (uint64_t{frameCount} * mSampleRate)
  1930. / (uint64_t{mNormalFrameCount} * sampleRate) == 3 ? 3 : 2;
  1931. maxNotificationFrames = frameCount / nBuffering;
  1932. // If client requested a fast track but this was denied, then use the smaller maximum.
  1933. if (requestedFlags & AUDIO_OUTPUT_FLAG_FAST) {
  1934. size_t maxNotificationFramesFastDenied = FMS_20 * sampleRate / 1000;
  1935. if (maxNotificationFrames > maxNotificationFramesFastDenied) {
  1936. maxNotificationFrames = maxNotificationFramesFastDenied;
  1937. }
  1938. }
  1939. }
  1940. if (notificationFrameCount == 0 || notificationFrameCount > maxNotificationFrames) {
  1941. if (notificationFrameCount == 0) {
  1942. ALOGD("Client defaulted notificationFrames to %zu for frameCount %zu",
  1943. maxNotificationFrames, frameCount);
  1944. } else {
  1945. ALOGW("Client adjusted notificationFrames from %zu to %zu for frameCount %zu",
  1946. notificationFrameCount, maxNotificationFrames, frameCount);
  1947. }
  1948. notificationFrameCount = maxNotificationFrames;
  1949. }
  1950. }
  1951. *pFrameCount = frameCount;
  1952. *pNotificationFrameCount = notificationFrameCount;
  1953. switch (mType) {
  1954. case DIRECT:
  1955. if (audio_is_linear_pcm(format)) { // TODO maybe use audio_has_proportional_frames()?
  1956. if (sampleRate != mSampleRate || format != mFormat || channelMask != mChannelMask) {
  1957. ALOGE("createTrack_l() Bad parameter: sampleRate %u format %#x, channelMask 0x%08x "
  1958. "for output %p with format %#x",
  1959. sampleRate, format, channelMask, mOutput, mFormat);
  1960. lStatus = BAD_VALUE;
  1961. goto Exit;
  1962. }
  1963. }
  1964. break;
  1965. case OFFLOAD:
  1966. if (sampleRate != mSampleRate || format != mFormat || channelMask != mChannelMask) {
  1967. ALOGE("createTrack_l() Bad parameter: sampleRate %d format %#x, channelMask 0x%08x \""
  1968. "for output %p with format %#x",
  1969. sampleRate, format, channelMask, mOutput, mFormat);
  1970. lStatus = BAD_VALUE;
  1971. goto Exit;
  1972. }
  1973. break;
  1974. default:
  1975. if (!audio_is_linear_pcm(format)) {
  1976. ALOGE("createTrack_l() Bad parameter: format %#x \""
  1977. "for output %p with format %#x",
  1978. format, mOutput, mFormat);
  1979. lStatus = BAD_VALUE;
  1980. goto Exit;
  1981. }
  1982. if (sampleRate > mSampleRate * AUDIO_RESAMPLER_DOWN_RATIO_MAX) {
  1983. ALOGE("Sample rate out of range: %u mSampleRate %u", sampleRate, mSampleRate);
  1984. lStatus = BAD_VALUE;
  1985. goto Exit;
  1986. }
  1987. break;
  1988. }
  1989. lStatus = initCheck();
  1990. if (lStatus != NO_ERROR) {
  1991. ALOGE("createTrack_l() audio driver not initialized");
  1992. goto Exit;
  1993. }
  1994. { // scope for mLock
  1995. Mutex::Autolock _l(mLock);
  1996. // all tracks in same audio session must share the same routing strategy otherwise
  1997. // conflicts will happen when tracks are moved from one output to another by audio policy
  1998. // manager
  1999. uint32_t strategy = AudioSystem::getStrategyForStream(streamType);
  2000. for (size_t i = 0; i < mTracks.size(); ++i) {
  2001. sp<Track> t = mTracks[i];
  2002. if (t != 0 && t->isExternalTrack()) {
  2003. uint32_t actual = AudioSystem::getStrategyForStream(t->streamType());
  2004. if (sessionId == t->sessionId() && strategy != actual) {
  2005. ALOGE("createTrack_l() mismatched strategy; expected %u but found %u",
  2006. strategy, actual);
  2007. lStatus = BAD_VALUE;
  2008. goto Exit;
  2009. }
  2010. }
  2011. }
  2012. track = new Track(this, client, streamType, attr, sampleRate, format,
  2013. channelMask, frameCount,
  2014. nullptr /* buffer */, (size_t)0 /* bufferSize */, sharedBuffer,
  2015. sessionId, creatorPid, uid, *flags, TrackBase::TYPE_DEFAULT, portId);
  2016. lStatus = track != 0 ? track->initCheck() : (status_t) NO_MEMORY;
  2017. if (lStatus != NO_ERROR) {
  2018. ALOGE("createTrack_l() initCheck failed %d; no control block?", lStatus);
  2019. // track must be cleared from the caller as the caller has the AF lock
  2020. goto Exit;
  2021. }
  2022. mTracks.add(track);
  2023. sp<EffectChain> chain = getEffectChain_l(sessionId);
  2024. if (chain != 0) {
  2025. ALOGV("createTrack_l() setting main buffer %p", chain->inBuffer());
  2026. track->setMainBuffer(chain->inBuffer());
  2027. chain->setStrategy(AudioSystem::getStrategyForStream(track->streamType()));
  2028. chain->incTrackCnt();
  2029. }
  2030. if ((*flags & AUDIO_OUTPUT_FLAG_FAST) && (tid != -1)) {
  2031. pid_t callingPid = IPCThreadState::self()->getCallingPid();
  2032. // we don't have CAP_SYS_NICE, nor do we want to have it as it's too powerful,
  2033. // so ask activity manager to do this on our behalf
  2034. sendPrioConfigEvent_l(callingPid, tid, kPriorityAudioApp, true /*forApp*/);
  2035. }
  2036. }
  2037. lStatus = NO_ERROR;
  2038. Exit:
  2039. *status = lStatus;
  2040. return track;
  2041. }
  2042. template<typename T>
  2043. ssize_t AudioFlinger::PlaybackThread::Tracks<T>::remove(const sp<T> &track)
  2044. {
  2045. const int trackId = track->id();
  2046. const ssize_t index = mTracks.remove(track);
  2047. if (index >= 0) {
  2048. if (mSaveDeletedTrackIds) {
  2049. // We can't directly access mAudioMixer since the caller may be outside of threadLoop.
  2050. // Instead, we add to mDeletedTrackIds which is solely used for mAudioMixer update,
  2051. // to be handled when MixerThread::prepareTracks_l() next changes mAudioMixer.
  2052. mDeletedTrackIds.emplace(trackId);
  2053. }
  2054. }
  2055. return index;
  2056. }
  2057. uint32_t AudioFlinger::PlaybackThread::correctLatency_l(uint32_t latency) const
  2058. {
  2059. return latency;
  2060. }
  2061. uint32_t AudioFlinger::PlaybackThread::latency() const
  2062. {
  2063. Mutex::Autolock _l(mLock);
  2064. return latency_l();
  2065. }
  2066. uint32_t AudioFlinger::PlaybackThread::latency_l() const
  2067. {
  2068. uint32_t latency;
  2069. if (initCheck() == NO_ERROR && mOutput->stream->getLatency(&latency) == OK) {
  2070. return correctLatency_l(latency);
  2071. }
  2072. return 0;
  2073. }
  2074. void AudioFlinger::PlaybackThread::setMasterVolume(float value)
  2075. {
  2076. Mutex::Autolock _l(mLock);
  2077. // Don't apply master volume in SW if our HAL can do it for us.
  2078. if (mOutput && mOutput->audioHwDev &&
  2079. mOutput->audioHwDev->canSetMasterVolume()) {
  2080. mMasterVolume = 1.0;
  2081. } else {
  2082. mMasterVolume = value;
  2083. }
  2084. }
  2085. void AudioFlinger::PlaybackThread::setMasterBalance(float balance)
  2086. {
  2087. mMasterBalance.store(balance);
  2088. }
  2089. void AudioFlinger::PlaybackThread::setMasterMute(bool muted)
  2090. {
  2091. if (isDuplicating()) {
  2092. return;
  2093. }
  2094. Mutex::Autolock _l(mLock);
  2095. // Don't apply master mute in SW if our HAL can do it for us.
  2096. if (mOutput && mOutput->audioHwDev &&
  2097. mOutput->audioHwDev->canSetMasterMute()) {
  2098. mMasterMute = false;
  2099. } else {
  2100. mMasterMute = muted;
  2101. }
  2102. }
  2103. void AudioFlinger::PlaybackThread::setStreamVolume(audio_stream_type_t stream, float value)
  2104. {
  2105. Mutex::Autolock _l(mLock);
  2106. mStreamTypes[stream].volume = value;
  2107. broadcast_l();
  2108. }
  2109. void AudioFlinger::PlaybackThread::setStreamMute(audio_stream_type_t stream, bool muted)
  2110. {
  2111. Mutex::Autolock _l(mLock);
  2112. mStreamTypes[stream].mute = muted;
  2113. broadcast_l();
  2114. }
  2115. float AudioFlinger::PlaybackThread::streamVolume(audio_stream_type_t stream) const
  2116. {
  2117. Mutex::Autolock _l(mLock);
  2118. return mStreamTypes[stream].volume;
  2119. }
  2120. void AudioFlinger::PlaybackThread::setVolumeForOutput_l(float left, float right) const
  2121. {
  2122. mOutput->stream->setVolume(left, right);
  2123. }
  2124. // addTrack_l() must be called with ThreadBase::mLock held
  2125. status_t AudioFlinger::PlaybackThread::addTrack_l(const sp<Track>& track)
  2126. {
  2127. status_t status = ALREADY_EXISTS;
  2128. if (mActiveTracks.indexOf(track) < 0) {
  2129. // the track is newly added, make sure it fills up all its
  2130. // buffers before playing. This is to ensure the client will
  2131. // effectively get the latency it requested.
  2132. if (track->isExternalTrack()) {
  2133. TrackBase::track_state state = track->mState;
  2134. mLock.unlock();
  2135. status = AudioSystem::startOutput(track->portId());
  2136. mLock.lock();
  2137. // abort track was stopped/paused while we released the lock
  2138. if (state != track->mState) {
  2139. if (status == NO_ERROR) {
  2140. mLock.unlock();
  2141. AudioSystem::stopOutput(track->portId());
  2142. mLock.lock();
  2143. }
  2144. return INVALID_OPERATION;
  2145. }
  2146. // abort if start is rejected by audio policy manager
  2147. if (status != NO_ERROR) {
  2148. return PERMISSION_DENIED;
  2149. }
  2150. #ifdef ADD_BATTERY_DATA
  2151. // to track the speaker usage
  2152. addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStart);
  2153. #endif
  2154. sendIoConfigEvent_l(AUDIO_CLIENT_STARTED, track->creatorPid(), track->portId());
  2155. }
  2156. // set retry count for buffer fill
  2157. if (track->isOffloaded()) {
  2158. if (track->isStopping_1()) {
  2159. track->mRetryCount = kMaxTrackStopRetriesOffload;
  2160. } else {
  2161. track->mRetryCount = kMaxTrackStartupRetriesOffload;
  2162. }
  2163. track->mFillingUpStatus = mStandby ? Track::FS_FILLING : Track::FS_FILLED;
  2164. } else {
  2165. track->mRetryCount = kMaxTrackStartupRetries;
  2166. track->mFillingUpStatus =
  2167. track->sharedBuffer() != 0 ? Track::FS_FILLED : Track::FS_FILLING;
  2168. }
  2169. if ((track->channelMask() & AUDIO_CHANNEL_HAPTIC_ALL) != AUDIO_CHANNEL_NONE
  2170. && mHapticChannelMask != AUDIO_CHANNEL_NONE) {
  2171. // Unlock due to VibratorService will lock for this call and will
  2172. // call Tracks.mute/unmute which also require thread's lock.
  2173. mLock.unlock();
  2174. const int intensity = AudioFlinger::onExternalVibrationStart(
  2175. track->getExternalVibration());
  2176. mLock.lock();
  2177. track->setHapticIntensity(static_cast<AudioMixer::haptic_intensity_t>(intensity));
  2178. // Haptic playback should be enabled by vibrator service.
  2179. if (track->getHapticPlaybackEnabled()) {
  2180. // Disable haptic playback of all active track to ensure only
  2181. // one track playing haptic if current track should play haptic.
  2182. for (const auto &t : mActiveTracks) {
  2183. t->setHapticPlaybackEnabled(false);
  2184. }
  2185. }
  2186. }
  2187. track->mResetDone = false;
  2188. track->mPresentationCompleteFrames = 0;
  2189. mActiveTracks.add(track);
  2190. sp<EffectChain> chain = getEffectChain_l(track->sessionId());
  2191. if (chain != 0) {
  2192. ALOGV("addTrack_l() starting track on chain %p for session %d", chain.get(),
  2193. track->sessionId());
  2194. chain->incActiveTrackCnt();
  2195. }
  2196. status = NO_ERROR;
  2197. }
  2198. onAddNewTrack_l();
  2199. return status;
  2200. }
  2201. bool AudioFlinger::PlaybackThread::destroyTrack_l(const sp<Track>& track)
  2202. {
  2203. track->terminate();
  2204. // active tracks are removed by threadLoop()
  2205. bool trackActive = (mActiveTracks.indexOf(track) >= 0);
  2206. track->mState = TrackBase::STOPPED;
  2207. if (!trackActive) {
  2208. removeTrack_l(track);
  2209. } else if (track->isFastTrack() || track->isOffloaded() || track->isDirect()) {
  2210. track->mState = TrackBase::STOPPING_1;
  2211. }
  2212. return trackActive;
  2213. }
  2214. void AudioFlinger::PlaybackThread::removeTrack_l(const sp<Track>& track)
  2215. {
  2216. track->triggerEvents(AudioSystem::SYNC_EVENT_PRESENTATION_COMPLETE);
  2217. String8 result;
  2218. track->appendDump(result, false /* active */);
  2219. mLocalLog.log("removeTrack_l (%p) %s", track.get(), result.string());
  2220. mTracks.remove(track);
  2221. if (track->isFastTrack()) {
  2222. int index = track->mFastIndex;
  2223. ALOG_ASSERT(0 < index && index < (int)FastMixerState::sMaxFastTracks);
  2224. ALOG_ASSERT(!(mFastTrackAvailMask & (1 << index)));
  2225. mFastTrackAvailMask |= 1 << index;
  2226. // redundant as track is about to be destroyed, for dumpsys only
  2227. track->mFastIndex = -1;
  2228. }
  2229. sp<EffectChain> chain = getEffectChain_l(track->sessionId());
  2230. if (chain != 0) {
  2231. chain->decTrackCnt();
  2232. }
  2233. }
  2234. String8 AudioFlinger::PlaybackThread::getParameters(const String8& keys)
  2235. {
  2236. Mutex::Autolock _l(mLock);
  2237. String8 out_s8;
  2238. if (initCheck() == NO_ERROR && mOutput->stream->getParameters(keys, &out_s8) == OK) {
  2239. return out_s8;
  2240. }
  2241. return String8();
  2242. }
  2243. status_t AudioFlinger::DirectOutputThread::selectPresentation(int presentationId, int programId) {
  2244. Mutex::Autolock _l(mLock);
  2245. if (mOutput == nullptr || mOutput->stream == nullptr) {
  2246. return NO_INIT;
  2247. }
  2248. return mOutput->stream->selectPresentation(presentationId, programId);
  2249. }
  2250. void AudioFlinger::PlaybackThread::ioConfigChanged(audio_io_config_event event, pid_t pid,
  2251. audio_port_handle_t portId) {
  2252. sp<AudioIoDescriptor> desc = new AudioIoDescriptor();
  2253. ALOGV("PlaybackThread::ioConfigChanged, thread %p, event %d", this, event);
  2254. desc->mIoHandle = mId;
  2255. switch (event) {
  2256. case AUDIO_OUTPUT_OPENED:
  2257. case AUDIO_OUTPUT_REGISTERED:
  2258. case AUDIO_OUTPUT_CONFIG_CHANGED:
  2259. desc->mPatch = mPatch;
  2260. desc->mChannelMask = mChannelMask;
  2261. desc->mSamplingRate = mSampleRate;
  2262. desc->mFormat = mFormat;
  2263. desc->mFrameCount = mNormalFrameCount; // FIXME see
  2264. // AudioFlinger::frameCount(audio_io_handle_t)
  2265. desc->mFrameCountHAL = mFrameCount;
  2266. desc->mLatency = latency_l();
  2267. break;
  2268. case AUDIO_CLIENT_STARTED:
  2269. desc->mPatch = mPatch;
  2270. desc->mPortId = portId;
  2271. break;
  2272. case AUDIO_OUTPUT_CLOSED:
  2273. default:
  2274. break;
  2275. }
  2276. mAudioFlinger->ioConfigChanged(event, desc, pid);
  2277. }
  2278. void AudioFlinger::PlaybackThread::onWriteReady()
  2279. {
  2280. mCallbackThread->resetWriteBlocked();
  2281. }
  2282. void AudioFlinger::PlaybackThread::onDrainReady()
  2283. {
  2284. mCallbackThread->resetDraining();
  2285. }
  2286. void AudioFlinger::PlaybackThread::onError()
  2287. {
  2288. mCallbackThread->setAsyncError();
  2289. }
  2290. void AudioFlinger::PlaybackThread::resetWriteBlocked(uint32_t sequence)
  2291. {
  2292. Mutex::Autolock _l(mLock);
  2293. // reject out of sequence requests
  2294. if ((mWriteAckSequence & 1) && (sequence == mWriteAckSequence)) {
  2295. mWriteAckSequence &= ~1;
  2296. mWaitWorkCV.signal();
  2297. }
  2298. }
  2299. void AudioFlinger::PlaybackThread::resetDraining(uint32_t sequence)
  2300. {
  2301. Mutex::Autolock _l(mLock);
  2302. // reject out of sequence requests
  2303. if ((mDrainSequence & 1) && (sequence == mDrainSequence)) {
  2304. // Register discontinuity when HW drain is completed because that can cause
  2305. // the timestamp frame position to reset to 0 for direct and offload threads.
  2306. // (Out of sequence requests are ignored, since the discontinuity would be handled
  2307. // elsewhere, e.g. in flush).
  2308. mTimestampVerifier.discontinuity();
  2309. mDrainSequence &= ~1;
  2310. mWaitWorkCV.signal();
  2311. }
  2312. }
  2313. void AudioFlinger::PlaybackThread::readOutputParameters_l()
  2314. {
  2315. // unfortunately we have no way of recovering from errors here, hence the LOG_ALWAYS_FATAL
  2316. mSampleRate = mOutput->getSampleRate();
  2317. mChannelMask = mOutput->getChannelMask();
  2318. if (!audio_is_output_channel(mChannelMask)) {
  2319. LOG_ALWAYS_FATAL("HAL channel mask %#x not valid for output", mChannelMask);
  2320. }
  2321. if ((mType == MIXER || mType == DUPLICATING)
  2322. && !isValidPcmSinkChannelMask(mChannelMask)) {
  2323. LOG_ALWAYS_FATAL("HAL channel mask %#x not supported for mixed output",
  2324. mChannelMask);
  2325. }
  2326. mChannelCount = audio_channel_count_from_out_mask(mChannelMask);
  2327. mBalance.setChannelMask(mChannelMask);
  2328. // Get actual HAL format.
  2329. status_t result = mOutput->stream->getFormat(&mHALFormat);
  2330. LOG_ALWAYS_FATAL_IF(result != OK, "Error when retrieving output stream format: %d", result);
  2331. // Get format from the shim, which will be different than the HAL format
  2332. // if playing compressed audio over HDMI passthrough.
  2333. mFormat = mOutput->getFormat();
  2334. if (!audio_is_valid_format(mFormat)) {
  2335. LOG_ALWAYS_FATAL("HAL format %#x not valid for output", mFormat);
  2336. }
  2337. if ((mType == MIXER || mType == DUPLICATING)
  2338. && !isValidPcmSinkFormat(mFormat)) {
  2339. LOG_FATAL("HAL format %#x not supported for mixed output",
  2340. mFormat);
  2341. }
  2342. mFrameSize = mOutput->getFrameSize();
  2343. result = mOutput->stream->getBufferSize(&mBufferSize);
  2344. LOG_ALWAYS_FATAL_IF(result != OK,
  2345. "Error when retrieving output stream buffer size: %d", result);
  2346. mFrameCount = mBufferSize / mFrameSize;
  2347. if (mFrameCount & 15) {
  2348. ALOGW("HAL output buffer size is %zu frames but AudioMixer requires multiples of 16 frames",
  2349. mFrameCount);
  2350. }
  2351. if (mOutput->flags & AUDIO_OUTPUT_FLAG_NON_BLOCKING) {
  2352. if (mOutput->stream->setCallback(this) == OK) {
  2353. mUseAsyncWrite = true;
  2354. mCallbackThread = new AudioFlinger::AsyncCallbackThread(this);
  2355. }
  2356. }
  2357. mHwSupportsPause = false;
  2358. if (mOutput->flags & AUDIO_OUTPUT_FLAG_DIRECT) {
  2359. bool supportsPause = false, supportsResume = false;
  2360. if (mOutput->stream->supportsPauseAndResume(&supportsPause, &supportsResume) == OK) {
  2361. if (supportsPause && supportsResume) {
  2362. mHwSupportsPause = true;
  2363. } else if (supportsPause) {
  2364. ALOGW("direct output implements pause but not resume");
  2365. } else if (supportsResume) {
  2366. ALOGW("direct output implements resume but not pause");
  2367. }
  2368. }
  2369. }
  2370. if (!mHwSupportsPause && mOutput->flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) {
  2371. LOG_ALWAYS_FATAL("HW_AV_SYNC requested but HAL does not implement pause and resume");
  2372. }
  2373. if (mType == DUPLICATING && mMixerBufferEnabled && mEffectBufferEnabled) {
  2374. // For best precision, we use float instead of the associated output
  2375. // device format (typically PCM 16 bit).
  2376. mFormat = AUDIO_FORMAT_PCM_FLOAT;
  2377. mFrameSize = mChannelCount * audio_bytes_per_sample(mFormat);
  2378. mBufferSize = mFrameSize * mFrameCount;
  2379. // TODO: We currently use the associated output device channel mask and sample rate.
  2380. // (1) Perhaps use the ORed channel mask of all downstream MixerThreads
  2381. // (if a valid mask) to avoid premature downmix.
  2382. // (2) Perhaps use the maximum sample rate of all downstream MixerThreads
  2383. // instead of the output device sample rate to avoid loss of high frequency information.
  2384. // This may need to be updated as MixerThread/OutputTracks are added and not here.
  2385. }
  2386. // Calculate size of normal sink buffer relative to the HAL output buffer size
  2387. double multiplier = 1.0;
  2388. if (mType == MIXER && (kUseFastMixer == FastMixer_Static ||
  2389. kUseFastMixer == FastMixer_Dynamic)) {
  2390. size_t minNormalFrameCount = (kMinNormalSinkBufferSizeMs * mSampleRate) / 1000;
  2391. size_t maxNormalFrameCount = (kMaxNormalSinkBufferSizeMs * mSampleRate) / 1000;
  2392. // round up minimum and round down maximum to nearest 16 frames to satisfy AudioMixer
  2393. minNormalFrameCount = (minNormalFrameCount + 15) & ~15;
  2394. maxNormalFrameCount = maxNormalFrameCount & ~15;
  2395. if (maxNormalFrameCount < minNormalFrameCount) {
  2396. maxNormalFrameCount = minNormalFrameCount;
  2397. }
  2398. multiplier = (double) minNormalFrameCount / (double) mFrameCount;
  2399. if (multiplier <= 1.0) {
  2400. multiplier = 1.0;
  2401. } else if (multiplier <= 2.0) {
  2402. if (2 * mFrameCount <= maxNormalFrameCount) {
  2403. multiplier = 2.0;
  2404. } else {
  2405. multiplier = (double) maxNormalFrameCount / (double) mFrameCount;
  2406. }
  2407. } else {
  2408. multiplier = floor(multiplier);
  2409. }
  2410. }
  2411. mNormalFrameCount = multiplier * mFrameCount;
  2412. // round up to nearest 16 frames to satisfy AudioMixer
  2413. if (mType == MIXER || mType == DUPLICATING) {
  2414. mNormalFrameCount = (mNormalFrameCount + 15) & ~15;
  2415. }
  2416. ALOGI("HAL output buffer size %zu frames, normal sink buffer size %zu frames", mFrameCount,
  2417. mNormalFrameCount);
  2418. // Check if we want to throttle the processing to no more than 2x normal rate
  2419. mThreadThrottle = property_get_bool("af.thread.throttle", true /* default_value */);
  2420. mThreadThrottleTimeMs = 0;
  2421. mThreadThrottleEndMs = 0;
  2422. mHalfBufferMs = mNormalFrameCount * 1000 / (2 * mSampleRate);
  2423. // mSinkBuffer is the sink buffer. Size is always multiple-of-16 frames.
  2424. // Originally this was int16_t[] array, need to remove legacy implications.
  2425. free(mSinkBuffer);
  2426. mSinkBuffer = NULL;
  2427. // For sink buffer size, we use the frame size from the downstream sink to avoid problems
  2428. // with non PCM formats for compressed music, e.g. AAC, and Offload threads.
  2429. const size_t sinkBufferSize = mNormalFrameCount * mFrameSize;
  2430. (void)posix_memalign(&mSinkBuffer, 32, sinkBufferSize);
  2431. // We resize the mMixerBuffer according to the requirements of the sink buffer which
  2432. // drives the output.
  2433. free(mMixerBuffer);
  2434. mMixerBuffer = NULL;
  2435. if (mMixerBufferEnabled) {
  2436. mMixerBufferFormat = AUDIO_FORMAT_PCM_FLOAT; // no longer valid: AUDIO_FORMAT_PCM_16_BIT.
  2437. mMixerBufferSize = mNormalFrameCount * mChannelCount
  2438. * audio_bytes_per_sample(mMixerBufferFormat);
  2439. (void)posix_memalign(&mMixerBuffer, 32, mMixerBufferSize);
  2440. }
  2441. free(mEffectBuffer);
  2442. mEffectBuffer = NULL;
  2443. if (mEffectBufferEnabled) {
  2444. mEffectBufferFormat = EFFECT_BUFFER_FORMAT;
  2445. mEffectBufferSize = mNormalFrameCount * mChannelCount
  2446. * audio_bytes_per_sample(mEffectBufferFormat);
  2447. (void)posix_memalign(&mEffectBuffer, 32, mEffectBufferSize);
  2448. }
  2449. mHapticChannelMask = mChannelMask & AUDIO_CHANNEL_HAPTIC_ALL;
  2450. mChannelMask &= ~mHapticChannelMask;
  2451. mHapticChannelCount = audio_channel_count_from_out_mask(mHapticChannelMask);
  2452. mChannelCount -= mHapticChannelCount;
  2453. // force reconfiguration of effect chains and engines to take new buffer size and audio
  2454. // parameters into account
  2455. // Note that mLock is not held when readOutputParameters_l() is called from the constructor
  2456. // but in this case nothing is done below as no audio sessions have effect yet so it doesn't
  2457. // matter.
  2458. // create a copy of mEffectChains as calling moveEffectChain_l() can reorder some effect chains
  2459. Vector< sp<EffectChain> > effectChains = mEffectChains;
  2460. for (size_t i = 0; i < effectChains.size(); i ++) {
  2461. mAudioFlinger->moveEffectChain_l(effectChains[i]->sessionId(),
  2462. this/* srcThread */, this/* dstThread */);
  2463. }
  2464. }
  2465. void AudioFlinger::PlaybackThread::updateMetadata_l()
  2466. {
  2467. if (mOutput == nullptr || mOutput->stream == nullptr ) {
  2468. return; // That should not happen
  2469. }
  2470. bool hasChanged = mActiveTracks.readAndClearHasChanged();
  2471. for (const sp<Track> &track : mActiveTracks) {
  2472. // Do not short-circuit as all hasChanged states must be reset
  2473. // as all the metadata are going to be sent
  2474. hasChanged |= track->readAndClearHasChanged();
  2475. }
  2476. if (!hasChanged) {
  2477. return; // nothing to do
  2478. }
  2479. StreamOutHalInterface::SourceMetadata metadata;
  2480. auto backInserter = std::back_inserter(metadata.tracks);
  2481. for (const sp<Track> &track : mActiveTracks) {
  2482. // No track is invalid as this is called after prepareTrack_l in the same critical section
  2483. track->copyMetadataTo(backInserter);
  2484. }
  2485. sendMetadataToBackend_l(metadata);
  2486. }
  2487. void AudioFlinger::PlaybackThread::sendMetadataToBackend_l(
  2488. const StreamOutHalInterface::SourceMetadata& metadata)
  2489. {
  2490. mOutput->stream->updateSourceMetadata(metadata);
  2491. };
  2492. status_t AudioFlinger::PlaybackThread::getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames)
  2493. {
  2494. if (halFrames == NULL || dspFrames == NULL) {
  2495. return BAD_VALUE;
  2496. }
  2497. Mutex::Autolock _l(mLock);
  2498. if (initCheck() != NO_ERROR) {
  2499. return INVALID_OPERATION;
  2500. }
  2501. int64_t framesWritten = mBytesWritten / mFrameSize;
  2502. *halFrames = framesWritten;
  2503. if (isSuspended()) {
  2504. // return an estimation of rendered frames when the output is suspended
  2505. size_t latencyFrames = (latency_l() * mSampleRate) / 1000;
  2506. *dspFrames = (uint32_t)
  2507. (framesWritten >= (int64_t)latencyFrames ? framesWritten - latencyFrames : 0);
  2508. return NO_ERROR;
  2509. } else {
  2510. status_t status;
  2511. uint32_t frames;
  2512. status = mOutput->getRenderPosition(&frames);
  2513. *dspFrames = (size_t)frames;
  2514. return status;
  2515. }
  2516. }
  2517. uint32_t AudioFlinger::PlaybackThread::getStrategyForSession_l(audio_session_t sessionId)
  2518. {
  2519. // session AUDIO_SESSION_OUTPUT_MIX is placed in same strategy as MUSIC stream so that
  2520. // it is moved to correct output by audio policy manager when A2DP is connected or disconnected
  2521. if (sessionId == AUDIO_SESSION_OUTPUT_MIX) {
  2522. return AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC);
  2523. }
  2524. for (size_t i = 0; i < mTracks.size(); i++) {
  2525. sp<Track> track = mTracks[i];
  2526. if (sessionId == track->sessionId() && !track->isInvalid()) {
  2527. return AudioSystem::getStrategyForStream(track->streamType());
  2528. }
  2529. }
  2530. return AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC);
  2531. }
  2532. AudioStreamOut* AudioFlinger::PlaybackThread::getOutput() const
  2533. {
  2534. Mutex::Autolock _l(mLock);
  2535. return mOutput;
  2536. }
  2537. AudioStreamOut* AudioFlinger::PlaybackThread::clearOutput()
  2538. {
  2539. Mutex::Autolock _l(mLock);
  2540. AudioStreamOut *output = mOutput;
  2541. mOutput = NULL;
  2542. // FIXME FastMixer might also have a raw ptr to mOutputSink;
  2543. // must push a NULL and wait for ack
  2544. mOutputSink.clear();
  2545. mPipeSink.clear();
  2546. mNormalSink.clear();
  2547. return output;
  2548. }
  2549. // this method must always be called either with ThreadBase mLock held or inside the thread loop
  2550. sp<StreamHalInterface> AudioFlinger::PlaybackThread::stream() const
  2551. {
  2552. if (mOutput == NULL) {
  2553. return NULL;
  2554. }
  2555. return mOutput->stream;
  2556. }
  2557. uint32_t AudioFlinger::PlaybackThread::activeSleepTimeUs() const
  2558. {
  2559. return (uint32_t)((uint32_t)((mNormalFrameCount * 1000) / mSampleRate) * 1000);
  2560. }
  2561. status_t AudioFlinger::PlaybackThread::setSyncEvent(const sp<SyncEvent>& event)
  2562. {
  2563. if (!isValidSyncEvent(event)) {
  2564. return BAD_VALUE;
  2565. }
  2566. Mutex::Autolock _l(mLock);
  2567. for (size_t i = 0; i < mTracks.size(); ++i) {
  2568. sp<Track> track = mTracks[i];
  2569. if (event->triggerSession() == track->sessionId()) {
  2570. (void) track->setSyncEvent(event);
  2571. return NO_ERROR;
  2572. }
  2573. }
  2574. return NAME_NOT_FOUND;
  2575. }
  2576. bool AudioFlinger::PlaybackThread::isValidSyncEvent(const sp<SyncEvent>& event) const
  2577. {
  2578. return event->type() == AudioSystem::SYNC_EVENT_PRESENTATION_COMPLETE;
  2579. }
  2580. void AudioFlinger::PlaybackThread::threadLoop_removeTracks(
  2581. const Vector< sp<Track> >& tracksToRemove)
  2582. {
  2583. // Miscellaneous track cleanup when removed from the active list,
  2584. // called without Thread lock but synchronized with threadLoop processing.
  2585. #ifdef ADD_BATTERY_DATA
  2586. for (const auto& track : tracksToRemove) {
  2587. if (track->isExternalTrack()) {
  2588. // to track the speaker usage
  2589. addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStop);
  2590. }
  2591. }
  2592. #else
  2593. (void)tracksToRemove; // suppress unused warning
  2594. #endif
  2595. }
  2596. void AudioFlinger::PlaybackThread::checkSilentMode_l()
  2597. {
  2598. if (!mMasterMute) {
  2599. char value[PROPERTY_VALUE_MAX];
  2600. if (mOutDevice == AUDIO_DEVICE_OUT_REMOTE_SUBMIX) {
  2601. ALOGD("ro.audio.silent will be ignored for threads on AUDIO_DEVICE_OUT_REMOTE_SUBMIX");
  2602. return;
  2603. }
  2604. if (property_get("ro.audio.silent", value, "0") > 0) {
  2605. char *endptr;
  2606. unsigned long ul = strtoul(value, &endptr, 0);
  2607. if (*endptr == '\0' && ul != 0) {
  2608. ALOGD("Silence is golden");
  2609. // The setprop command will not allow a property to be changed after
  2610. // the first time it is set, so we don't have to worry about un-muting.
  2611. setMasterMute_l(true);
  2612. }
  2613. }
  2614. }
  2615. }
  2616. // shared by MIXER and DIRECT, overridden by DUPLICATING
  2617. ssize_t AudioFlinger::PlaybackThread::threadLoop_write()
  2618. {
  2619. LOG_HIST_TS();
  2620. mInWrite = true;
  2621. ssize_t bytesWritten;
  2622. const size_t offset = mCurrentWriteLength - mBytesRemaining;
  2623. // If an NBAIO sink is present, use it to write the normal mixer's submix
  2624. if (mNormalSink != 0) {
  2625. const size_t count = mBytesRemaining / mFrameSize;
  2626. ATRACE_BEGIN("write");
  2627. // update the setpoint when AudioFlinger::mScreenState changes
  2628. uint32_t screenState = AudioFlinger::mScreenState;
  2629. if (screenState != mScreenState) {
  2630. mScreenState = screenState;
  2631. MonoPipe *pipe = (MonoPipe *)mPipeSink.get();
  2632. if (pipe != NULL) {
  2633. pipe->setAvgFrames((mScreenState & 1) ?
  2634. (pipe->maxFrames() * 7) / 8 : mNormalFrameCount * 2);
  2635. }
  2636. }
  2637. ssize_t framesWritten = mNormalSink->write((char *)mSinkBuffer + offset, count);
  2638. ATRACE_END();
  2639. if (framesWritten > 0) {
  2640. bytesWritten = framesWritten * mFrameSize;
  2641. #ifdef TEE_SINK
  2642. mTee.write((char *)mSinkBuffer + offset, framesWritten);
  2643. #endif
  2644. } else {
  2645. bytesWritten = framesWritten;
  2646. }
  2647. // otherwise use the HAL / AudioStreamOut directly
  2648. } else {
  2649. // Direct output and offload threads
  2650. if (mUseAsyncWrite) {
  2651. ALOGW_IF(mWriteAckSequence & 1, "threadLoop_write(): out of sequence write request");
  2652. mWriteAckSequence += 2;
  2653. mWriteAckSequence |= 1;
  2654. ALOG_ASSERT(mCallbackThread != 0);
  2655. mCallbackThread->setWriteBlocked(mWriteAckSequence);
  2656. }
  2657. // FIXME We should have an implementation of timestamps for direct output threads.
  2658. // They are used e.g for multichannel PCM playback over HDMI.
  2659. bytesWritten = mOutput->write((char *)mSinkBuffer + offset, mBytesRemaining);
  2660. if (mUseAsyncWrite &&
  2661. ((bytesWritten < 0) || (bytesWritten == (ssize_t)mBytesRemaining))) {
  2662. // do not wait for async callback in case of error of full write
  2663. mWriteAckSequence &= ~1;
  2664. ALOG_ASSERT(mCallbackThread != 0);
  2665. mCallbackThread->setWriteBlocked(mWriteAckSequence);
  2666. }
  2667. }
  2668. mNumWrites++;
  2669. mInWrite = false;
  2670. mStandby = false;
  2671. return bytesWritten;
  2672. }
  2673. void AudioFlinger::PlaybackThread::threadLoop_drain()
  2674. {
  2675. bool supportsDrain = false;
  2676. if (mOutput->stream->supportsDrain(&supportsDrain) == OK && supportsDrain) {
  2677. ALOGV("draining %s", (mMixerStatus == MIXER_DRAIN_TRACK) ? "early" : "full");
  2678. if (mUseAsyncWrite) {
  2679. ALOGW_IF(mDrainSequence & 1, "threadLoop_drain(): out of sequence drain request");
  2680. mDrainSequence |= 1;
  2681. ALOG_ASSERT(mCallbackThread != 0);
  2682. mCallbackThread->setDraining(mDrainSequence);
  2683. }
  2684. status_t result = mOutput->stream->drain(mMixerStatus == MIXER_DRAIN_TRACK);
  2685. ALOGE_IF(result != OK, "Error when draining stream: %d", result);
  2686. }
  2687. }
  2688. void AudioFlinger::PlaybackThread::threadLoop_exit()
  2689. {
  2690. {
  2691. Mutex::Autolock _l(mLock);
  2692. for (size_t i = 0; i < mTracks.size(); i++) {
  2693. sp<Track> track = mTracks[i];
  2694. track->invalidate();
  2695. }
  2696. // Clear ActiveTracks to update BatteryNotifier in case active tracks remain.
  2697. // After we exit there are no more track changes sent to BatteryNotifier
  2698. // because that requires an active threadLoop.
  2699. // TODO: should we decActiveTrackCnt() of the cleared track effect chain?
  2700. mActiveTracks.clear();
  2701. }
  2702. }
  2703. /*
  2704. The derived values that are cached:
  2705. - mSinkBufferSize from frame count * frame size
  2706. - mActiveSleepTimeUs from activeSleepTimeUs()
  2707. - mIdleSleepTimeUs from idleSleepTimeUs()
  2708. - mStandbyDelayNs from mActiveSleepTimeUs (DIRECT only) or forced to at least
  2709. kDefaultStandbyTimeInNsecs when connected to an A2DP device.
  2710. - maxPeriod from frame count and sample rate (MIXER only)
  2711. The parameters that affect these derived values are:
  2712. - frame count
  2713. - frame size
  2714. - sample rate
  2715. - device type: A2DP or not
  2716. - device latency
  2717. - format: PCM or not
  2718. - active sleep time
  2719. - idle sleep time
  2720. */
  2721. void AudioFlinger::PlaybackThread::cacheParameters_l()
  2722. {
  2723. mSinkBufferSize = mNormalFrameCount * mFrameSize;
  2724. mActiveSleepTimeUs = activeSleepTimeUs();
  2725. mIdleSleepTimeUs = idleSleepTimeUs();
  2726. // make sure standby delay is not too short when connected to an A2DP sink to avoid
  2727. // truncating audio when going to standby.
  2728. mStandbyDelayNs = AudioFlinger::mStandbyTimeInNsecs;
  2729. if ((mOutDevice & AUDIO_DEVICE_OUT_ALL_A2DP) != 0) {
  2730. if (mStandbyDelayNs < kDefaultStandbyTimeInNsecs) {
  2731. mStandbyDelayNs = kDefaultStandbyTimeInNsecs;
  2732. }
  2733. }
  2734. }
  2735. bool AudioFlinger::PlaybackThread::invalidateTracks_l(audio_stream_type_t streamType)
  2736. {
  2737. ALOGV("MixerThread::invalidateTracks() mixer %p, streamType %d, mTracks.size %zu",
  2738. this, streamType, mTracks.size());
  2739. bool trackMatch = false;
  2740. size_t size = mTracks.size();
  2741. for (size_t i = 0; i < size; i++) {
  2742. sp<Track> t = mTracks[i];
  2743. if (t->streamType() == streamType && t->isExternalTrack()) {
  2744. t->invalidate();
  2745. trackMatch = true;
  2746. }
  2747. }
  2748. return trackMatch;
  2749. }
  2750. void AudioFlinger::PlaybackThread::invalidateTracks(audio_stream_type_t streamType)
  2751. {
  2752. Mutex::Autolock _l(mLock);
  2753. invalidateTracks_l(streamType);
  2754. }
  2755. status_t AudioFlinger::PlaybackThread::addEffectChain_l(const sp<EffectChain>& chain)
  2756. {
  2757. audio_session_t session = chain->sessionId();
  2758. sp<EffectBufferHalInterface> halInBuffer, halOutBuffer;
  2759. status_t result = mAudioFlinger->mEffectsFactoryHal->mirrorBuffer(
  2760. mEffectBufferEnabled ? mEffectBuffer : mSinkBuffer,
  2761. mEffectBufferEnabled ? mEffectBufferSize : mSinkBufferSize,
  2762. &halInBuffer);
  2763. if (result != OK) return result;
  2764. halOutBuffer = halInBuffer;
  2765. effect_buffer_t *buffer = reinterpret_cast<effect_buffer_t*>(halInBuffer->externalData());
  2766. ALOGV("addEffectChain_l() %p on thread %p for session %d", chain.get(), this, session);
  2767. if (session > AUDIO_SESSION_OUTPUT_MIX) {
  2768. // Only one effect chain can be present in direct output thread and it uses
  2769. // the sink buffer as input
  2770. if (mType != DIRECT) {
  2771. size_t numSamples = mNormalFrameCount * (mChannelCount + mHapticChannelCount);
  2772. status_t result = mAudioFlinger->mEffectsFactoryHal->allocateBuffer(
  2773. numSamples * sizeof(effect_buffer_t),
  2774. &halInBuffer);
  2775. if (result != OK) return result;
  2776. #ifdef FLOAT_EFFECT_CHAIN
  2777. buffer = halInBuffer->audioBuffer()->f32;
  2778. #else
  2779. buffer = halInBuffer->audioBuffer()->s16;
  2780. #endif
  2781. ALOGV("addEffectChain_l() creating new input buffer %p session %d",
  2782. buffer, session);
  2783. }
  2784. // Attach all tracks with same session ID to this chain.
  2785. for (size_t i = 0; i < mTracks.size(); ++i) {
  2786. sp<Track> track = mTracks[i];
  2787. if (session == track->sessionId()) {
  2788. ALOGV("addEffectChain_l() track->setMainBuffer track %p buffer %p", track.get(),
  2789. buffer);
  2790. track->setMainBuffer(buffer);
  2791. chain->incTrackCnt();
  2792. }
  2793. }
  2794. // indicate all active tracks in the chain
  2795. for (const sp<Track> &track : mActiveTracks) {
  2796. if (session == track->sessionId()) {
  2797. ALOGV("addEffectChain_l() activating track %p on session %d", track.get(), session);
  2798. chain->incActiveTrackCnt();
  2799. }
  2800. }
  2801. }
  2802. chain->setThread(this);
  2803. chain->setInBuffer(halInBuffer);
  2804. chain->setOutBuffer(halOutBuffer);
  2805. // Effect chain for session AUDIO_SESSION_OUTPUT_STAGE is inserted at end of effect
  2806. // chains list in order to be processed last as it contains output stage effects.
  2807. // Effect chain for session AUDIO_SESSION_OUTPUT_MIX is inserted before
  2808. // session AUDIO_SESSION_OUTPUT_STAGE to be processed
  2809. // after track specific effects and before output stage.
  2810. // It is therefore mandatory that AUDIO_SESSION_OUTPUT_MIX == 0 and
  2811. // that AUDIO_SESSION_OUTPUT_STAGE < AUDIO_SESSION_OUTPUT_MIX.
  2812. // Effect chain for other sessions are inserted at beginning of effect
  2813. // chains list to be processed before output mix effects. Relative order between other
  2814. // sessions is not important.
  2815. static_assert(AUDIO_SESSION_OUTPUT_MIX == 0 &&
  2816. AUDIO_SESSION_OUTPUT_STAGE < AUDIO_SESSION_OUTPUT_MIX,
  2817. "audio_session_t constants misdefined");
  2818. size_t size = mEffectChains.size();
  2819. size_t i = 0;
  2820. for (i = 0; i < size; i++) {
  2821. if (mEffectChains[i]->sessionId() < session) {
  2822. break;
  2823. }
  2824. }
  2825. mEffectChains.insertAt(chain, i);
  2826. checkSuspendOnAddEffectChain_l(chain);
  2827. return NO_ERROR;
  2828. }
  2829. size_t AudioFlinger::PlaybackThread::removeEffectChain_l(const sp<EffectChain>& chain)
  2830. {
  2831. audio_session_t session = chain->sessionId();
  2832. ALOGV("removeEffectChain_l() %p from thread %p for session %d", chain.get(), this, session);
  2833. for (size_t i = 0; i < mEffectChains.size(); i++) {
  2834. if (chain == mEffectChains[i]) {
  2835. mEffectChains.removeAt(i);
  2836. // detach all active tracks from the chain
  2837. for (const sp<Track> &track : mActiveTracks) {
  2838. if (session == track->sessionId()) {
  2839. ALOGV("removeEffectChain_l(): stopping track on chain %p for session Id: %d",
  2840. chain.get(), session);
  2841. chain->decActiveTrackCnt();
  2842. }
  2843. }
  2844. // detach all tracks with same session ID from this chain
  2845. for (size_t i = 0; i < mTracks.size(); ++i) {
  2846. sp<Track> track = mTracks[i];
  2847. if (session == track->sessionId()) {
  2848. track->setMainBuffer(reinterpret_cast<effect_buffer_t*>(mSinkBuffer));
  2849. chain->decTrackCnt();
  2850. }
  2851. }
  2852. break;
  2853. }
  2854. }
  2855. return mEffectChains.size();
  2856. }
  2857. status_t AudioFlinger::PlaybackThread::attachAuxEffect(
  2858. const sp<AudioFlinger::PlaybackThread::Track>& track, int EffectId)
  2859. {
  2860. Mutex::Autolock _l(mLock);
  2861. return attachAuxEffect_l(track, EffectId);
  2862. }
  2863. status_t AudioFlinger::PlaybackThread::attachAuxEffect_l(
  2864. const sp<AudioFlinger::PlaybackThread::Track>& track, int EffectId)
  2865. {
  2866. status_t status = NO_ERROR;
  2867. if (EffectId == 0) {
  2868. track->setAuxBuffer(0, NULL);
  2869. } else {
  2870. // Auxiliary effects are always in audio session AUDIO_SESSION_OUTPUT_MIX
  2871. sp<EffectModule> effect = getEffect_l(AUDIO_SESSION_OUTPUT_MIX, EffectId);
  2872. if (effect != 0) {
  2873. if ((effect->desc().flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
  2874. track->setAuxBuffer(EffectId, (int32_t *)effect->inBuffer());
  2875. } else {
  2876. status = INVALID_OPERATION;
  2877. }
  2878. } else {
  2879. status = BAD_VALUE;
  2880. }
  2881. }
  2882. return status;
  2883. }
  2884. void AudioFlinger::PlaybackThread::detachAuxEffect_l(int effectId)
  2885. {
  2886. for (size_t i = 0; i < mTracks.size(); ++i) {
  2887. sp<Track> track = mTracks[i];
  2888. if (track->auxEffectId() == effectId) {
  2889. attachAuxEffect_l(track, 0);
  2890. }
  2891. }
  2892. }
  2893. bool AudioFlinger::PlaybackThread::threadLoop()
  2894. {
  2895. tlNBLogWriter = mNBLogWriter.get();
  2896. Vector< sp<Track> > tracksToRemove;
  2897. mStandbyTimeNs = systemTime();
  2898. int64_t lastLoopCountWritten = -2; // never matches "previous" loop, when loopCount = 0.
  2899. int64_t lastFramesWritten = -1; // track changes in timestamp server frames written
  2900. // MIXER
  2901. nsecs_t lastWarning = 0;
  2902. // DUPLICATING
  2903. // FIXME could this be made local to while loop?
  2904. writeFrames = 0;
  2905. cacheParameters_l();
  2906. mSleepTimeUs = mIdleSleepTimeUs;
  2907. if (mType == MIXER) {
  2908. sleepTimeShift = 0;
  2909. }
  2910. CpuStats cpuStats;
  2911. const String8 myName(String8::format("thread %p type %d TID %d", this, mType, gettid()));
  2912. acquireWakeLock();
  2913. // mNBLogWriter logging APIs can only be called by a single thread, typically the
  2914. // thread associated with this PlaybackThread.
  2915. // If you want to share the mNBLogWriter with other threads (for example, binder threads)
  2916. // then all such threads must agree to hold a common mutex before logging.
  2917. // So if you need to log when mutex is unlocked, set logString to a non-NULL string,
  2918. // and then that string will be logged at the next convenient opportunity.
  2919. // See reference to logString below.
  2920. const char *logString = NULL;
  2921. // Estimated time for next buffer to be written to hal. This is used only on
  2922. // suspended mode (for now) to help schedule the wait time until next iteration.
  2923. nsecs_t timeLoopNextNs = 0;
  2924. checkSilentMode_l();
  2925. // DIRECT and OFFLOAD threads should reset frame count to zero on stop/flush
  2926. // TODO: add confirmation checks:
  2927. // 1) DIRECT threads and linear PCM format really resets to 0?
  2928. // 2) Is frame count really valid if not linear pcm?
  2929. // 3) Are all 64 bits of position returned, not just lowest 32 bits?
  2930. if (mType == OFFLOAD || mType == DIRECT) {
  2931. mTimestampVerifier.setDiscontinuityMode(mTimestampVerifier.DISCONTINUITY_MODE_ZERO);
  2932. }
  2933. audio_patch_handle_t lastDownstreamPatchHandle = AUDIO_PATCH_HANDLE_NONE;
  2934. // loopCount is used for statistics and diagnostics.
  2935. for (int64_t loopCount = 0; !exitPending(); ++loopCount)
  2936. {
  2937. // Log merge requests are performed during AudioFlinger binder transactions, but
  2938. // that does not cover audio playback. It's requested here for that reason.
  2939. mAudioFlinger->requestLogMerge();
  2940. cpuStats.sample(myName);
  2941. Vector< sp<EffectChain> > effectChains;
  2942. audio_session_t activeHapticSessionId = AUDIO_SESSION_NONE;
  2943. std::vector<sp<Track>> activeTracks;
  2944. // If the device is AUDIO_DEVICE_OUT_BUS, check for downstream latency.
  2945. //
  2946. // Note: we access outDevice() outside of mLock.
  2947. if (isMsdDevice() && (outDevice() & AUDIO_DEVICE_OUT_BUS) != 0) {
  2948. // Here, we try for the AF lock, but do not block on it as the latency
  2949. // is more informational.
  2950. if (mAudioFlinger->mLock.tryLock() == NO_ERROR) {
  2951. std::vector<PatchPanel::SoftwarePatch> swPatches;
  2952. double latencyMs;
  2953. status_t status = INVALID_OPERATION;
  2954. audio_patch_handle_t downstreamPatchHandle = AUDIO_PATCH_HANDLE_NONE;
  2955. if (mAudioFlinger->mPatchPanel.getDownstreamSoftwarePatches(id(), &swPatches) == OK
  2956. && swPatches.size() > 0) {
  2957. status = swPatches[0].getLatencyMs_l(&latencyMs);
  2958. downstreamPatchHandle = swPatches[0].getPatchHandle();
  2959. }
  2960. if (downstreamPatchHandle != lastDownstreamPatchHandle) {
  2961. mDownstreamLatencyStatMs.reset();
  2962. lastDownstreamPatchHandle = downstreamPatchHandle;
  2963. }
  2964. if (status == OK) {
  2965. // verify downstream latency (we assume a max reasonable
  2966. // latency of 5 seconds).
  2967. const double minLatency = 0., maxLatency = 5000.;
  2968. if (latencyMs >= minLatency && latencyMs <= maxLatency) {
  2969. ALOGV("new downstream latency %lf ms", latencyMs);
  2970. } else {
  2971. ALOGD("out of range downstream latency %lf ms", latencyMs);
  2972. if (latencyMs < minLatency) latencyMs = minLatency;
  2973. else if (latencyMs > maxLatency) latencyMs = maxLatency;
  2974. }
  2975. mDownstreamLatencyStatMs.add(latencyMs);
  2976. }
  2977. mAudioFlinger->mLock.unlock();
  2978. }
  2979. } else {
  2980. if (lastDownstreamPatchHandle != AUDIO_PATCH_HANDLE_NONE) {
  2981. // our device is no longer AUDIO_DEVICE_OUT_BUS, reset patch handle and stats.
  2982. mDownstreamLatencyStatMs.reset();
  2983. lastDownstreamPatchHandle = AUDIO_PATCH_HANDLE_NONE;
  2984. }
  2985. }
  2986. { // scope for mLock
  2987. Mutex::Autolock _l(mLock);
  2988. processConfigEvents_l();
  2989. // See comment at declaration of logString for why this is done under mLock
  2990. if (logString != NULL) {
  2991. mNBLogWriter->logTimestamp();
  2992. mNBLogWriter->log(logString);
  2993. logString = NULL;
  2994. }
  2995. // Collect timestamp statistics for the Playback Thread types that support it.
  2996. if (mType == MIXER
  2997. || mType == DUPLICATING
  2998. || mType == DIRECT
  2999. || mType == OFFLOAD) { // no indentation
  3000. // Gather the framesReleased counters for all active tracks,
  3001. // and associate with the sink frames written out. We need
  3002. // this to convert the sink timestamp to the track timestamp.
  3003. bool kernelLocationUpdate = false;
  3004. ExtendedTimestamp timestamp; // use private copy to fetch
  3005. if (mStandby) {
  3006. mTimestampVerifier.discontinuity();
  3007. } else if (threadloop_getHalTimestamp_l(&timestamp) == OK) {
  3008. mTimestampVerifier.add(timestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL],
  3009. timestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL],
  3010. mSampleRate);
  3011. if (isTimestampCorrectionEnabled()) {
  3012. ALOGV("TS_BEFORE: %d %lld %lld", id(),
  3013. (long long)timestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL],
  3014. (long long)timestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL]);
  3015. auto correctedTimestamp = mTimestampVerifier.getLastCorrectedTimestamp();
  3016. timestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL]
  3017. = correctedTimestamp.mFrames;
  3018. timestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL]
  3019. = correctedTimestamp.mTimeNs;
  3020. ALOGV("TS_AFTER: %d %lld %lld", id(),
  3021. (long long)timestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL],
  3022. (long long)timestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL]);
  3023. // Note: Downstream latency only added if timestamp correction enabled.
  3024. if (mDownstreamLatencyStatMs.getN() > 0) { // we have latency info.
  3025. const int64_t newPosition =
  3026. timestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL]
  3027. - int64_t(mDownstreamLatencyStatMs.getMean() * mSampleRate * 1e-3);
  3028. // prevent retrograde
  3029. timestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL] = max(
  3030. newPosition,
  3031. (mTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL]
  3032. - mSuspendedFrames));
  3033. }
  3034. }
  3035. // We always fetch the timestamp here because often the downstream
  3036. // sink will block while writing.
  3037. // We keep track of the last valid kernel position in case we are in underrun
  3038. // and the normal mixer period is the same as the fast mixer period, or there
  3039. // is some error from the HAL.
  3040. if (mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] >= 0) {
  3041. mTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL_LASTKERNELOK] =
  3042. mTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL];
  3043. mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL_LASTKERNELOK] =
  3044. mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL];
  3045. mTimestamp.mPosition[ExtendedTimestamp::LOCATION_SERVER_LASTKERNELOK] =
  3046. mTimestamp.mPosition[ExtendedTimestamp::LOCATION_SERVER];
  3047. mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_SERVER_LASTKERNELOK] =
  3048. mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_SERVER];
  3049. }
  3050. if (timestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] >= 0) {
  3051. kernelLocationUpdate = true;
  3052. } else {
  3053. ALOGVV("getTimestamp error - no valid kernel position");
  3054. }
  3055. // copy over kernel info
  3056. mTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL] =
  3057. timestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL]
  3058. + mSuspendedFrames; // add frames discarded when suspended
  3059. mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] =
  3060. timestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL];
  3061. } else {
  3062. mTimestampVerifier.error();
  3063. }
  3064. // mFramesWritten for non-offloaded tracks are contiguous
  3065. // even after standby() is called. This is useful for the track frame
  3066. // to sink frame mapping.
  3067. bool serverLocationUpdate = false;
  3068. if (mFramesWritten != lastFramesWritten) {
  3069. serverLocationUpdate = true;
  3070. lastFramesWritten = mFramesWritten;
  3071. }
  3072. // Only update timestamps if there is a meaningful change.
  3073. // Either the kernel timestamp must be valid or we have written something.
  3074. if (kernelLocationUpdate || serverLocationUpdate) {
  3075. if (serverLocationUpdate) {
  3076. // use the time before we called the HAL write - it is a bit more accurate
  3077. // to when the server last read data than the current time here.
  3078. //
  3079. // If we haven't written anything, mLastIoBeginNs will be -1
  3080. // and we use systemTime().
  3081. mTimestamp.mPosition[ExtendedTimestamp::LOCATION_SERVER] = mFramesWritten;
  3082. mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_SERVER] = mLastIoBeginNs == -1
  3083. ? systemTime() : mLastIoBeginNs;
  3084. }
  3085. for (const sp<Track> &t : mActiveTracks) {
  3086. if (!t->isFastTrack()) {
  3087. t->updateTrackFrameInfo(
  3088. t->mAudioTrackServerProxy->framesReleased(),
  3089. mFramesWritten,
  3090. mSampleRate,
  3091. mTimestamp);
  3092. }
  3093. }
  3094. }
  3095. if (audio_has_proportional_frames(mFormat)) {
  3096. const double latencyMs = mTimestamp.getOutputServerLatencyMs(mSampleRate);
  3097. if (latencyMs != 0.) { // note 0. means timestamp is empty.
  3098. mLatencyMs.add(latencyMs);
  3099. }
  3100. }
  3101. } // if (mType ... ) { // no indentation
  3102. #if 0
  3103. // logFormat example
  3104. if (z % 100 == 0) {
  3105. timespec ts;
  3106. clock_gettime(CLOCK_MONOTONIC, &ts);
  3107. LOGT("This is an integer %d, this is a float %f, this is my "
  3108. "pid %p %% %s %t", 42, 3.14, "and this is a timestamp", ts);
  3109. LOGT("A deceptive null-terminated string %\0");
  3110. }
  3111. ++z;
  3112. #endif
  3113. saveOutputTracks();
  3114. if (mSignalPending) {
  3115. // A signal was raised while we were unlocked
  3116. mSignalPending = false;
  3117. } else if (waitingAsyncCallback_l()) {
  3118. if (exitPending()) {
  3119. break;
  3120. }
  3121. bool released = false;
  3122. if (!keepWakeLock()) {
  3123. releaseWakeLock_l();
  3124. released = true;
  3125. }
  3126. const int64_t waitNs = computeWaitTimeNs_l();
  3127. ALOGV("wait async completion (wait time: %lld)", (long long)waitNs);
  3128. status_t status = mWaitWorkCV.waitRelative(mLock, waitNs);
  3129. if (status == TIMED_OUT) {
  3130. mSignalPending = true; // if timeout recheck everything
  3131. }
  3132. ALOGV("async completion/wake");
  3133. if (released) {
  3134. acquireWakeLock_l();
  3135. }
  3136. mStandbyTimeNs = systemTime() + mStandbyDelayNs;
  3137. mSleepTimeUs = 0;
  3138. continue;
  3139. }
  3140. if ((mActiveTracks.isEmpty() && systemTime() > mStandbyTimeNs) ||
  3141. isSuspended()) {
  3142. // put audio hardware into standby after short delay
  3143. if (shouldStandby_l()) {
  3144. threadLoop_standby();
  3145. // This is where we go into standby
  3146. if (!mStandby) {
  3147. LOG_AUDIO_STATE();
  3148. }
  3149. mStandby = true;
  3150. sendStatistics(false /* force */);
  3151. }
  3152. if (mActiveTracks.isEmpty() && mConfigEvents.isEmpty()) {
  3153. // we're about to wait, flush the binder command buffer
  3154. IPCThreadState::self()->flushCommands();
  3155. clearOutputTracks();
  3156. if (exitPending()) {
  3157. break;
  3158. }
  3159. releaseWakeLock_l();
  3160. // wait until we have something to do...
  3161. ALOGV("%s going to sleep", myName.string());
  3162. mWaitWorkCV.wait(mLock);
  3163. ALOGV("%s waking up", myName.string());
  3164. acquireWakeLock_l();
  3165. mMixerStatus = MIXER_IDLE;
  3166. mMixerStatusIgnoringFastTracks = MIXER_IDLE;
  3167. mBytesWritten = 0;
  3168. mBytesRemaining = 0;
  3169. checkSilentMode_l();
  3170. mStandbyTimeNs = systemTime() + mStandbyDelayNs;
  3171. mSleepTimeUs = mIdleSleepTimeUs;
  3172. if (mType == MIXER) {
  3173. sleepTimeShift = 0;
  3174. }
  3175. continue;
  3176. }
  3177. }
  3178. // mMixerStatusIgnoringFastTracks is also updated internally
  3179. mMixerStatus = prepareTracks_l(&tracksToRemove);
  3180. mActiveTracks.updatePowerState(this);
  3181. updateMetadata_l();
  3182. // prevent any changes in effect chain list and in each effect chain
  3183. // during mixing and effect process as the audio buffers could be deleted
  3184. // or modified if an effect is created or deleted
  3185. lockEffectChains_l(effectChains);
  3186. // Determine which session to pick up haptic data.
  3187. // This must be done under the same lock as prepareTracks_l().
  3188. // TODO: Write haptic data directly to sink buffer when mixing.
  3189. if (mHapticChannelCount > 0 && effectChains.size() > 0) {
  3190. for (const auto& track : mActiveTracks) {
  3191. if (track->getHapticPlaybackEnabled()) {
  3192. activeHapticSessionId = track->sessionId();
  3193. break;
  3194. }
  3195. }
  3196. }
  3197. // Acquire a local copy of active tracks with lock (release w/o lock).
  3198. //
  3199. // Control methods on the track acquire the ThreadBase lock (e.g. start()
  3200. // stop(), pause(), etc.), but the threadLoop is entitled to call audio
  3201. // data / buffer methods on tracks from activeTracks without the ThreadBase lock.
  3202. activeTracks.insert(activeTracks.end(), mActiveTracks.begin(), mActiveTracks.end());
  3203. } // mLock scope ends
  3204. if (mBytesRemaining == 0) {
  3205. mCurrentWriteLength = 0;
  3206. if (mMixerStatus == MIXER_TRACKS_READY) {
  3207. // threadLoop_mix() sets mCurrentWriteLength
  3208. threadLoop_mix();
  3209. } else if ((mMixerStatus != MIXER_DRAIN_TRACK)
  3210. && (mMixerStatus != MIXER_DRAIN_ALL)) {
  3211. // threadLoop_sleepTime sets mSleepTimeUs to 0 if data
  3212. // must be written to HAL
  3213. threadLoop_sleepTime();
  3214. if (mSleepTimeUs == 0) {
  3215. mCurrentWriteLength = mSinkBufferSize;
  3216. // Tally underrun frames as we are inserting 0s here.
  3217. for (const auto& track : activeTracks) {
  3218. if (track->mFillingUpStatus == Track::FS_ACTIVE) {
  3219. track->mAudioTrackServerProxy->tallyUnderrunFrames(mNormalFrameCount);
  3220. }
  3221. }
  3222. }
  3223. }
  3224. // Either threadLoop_mix() or threadLoop_sleepTime() should have set
  3225. // mMixerBuffer with data if mMixerBufferValid is true and mSleepTimeUs == 0.
  3226. // Merge mMixerBuffer data into mEffectBuffer (if any effects are valid)
  3227. // or mSinkBuffer (if there are no effects).
  3228. //
  3229. // This is done pre-effects computation; if effects change to
  3230. // support higher precision, this needs to move.
  3231. //
  3232. // mMixerBufferValid is only set true by MixerThread::prepareTracks_l().
  3233. // TODO use mSleepTimeUs == 0 as an additional condition.
  3234. if (mMixerBufferValid) {
  3235. void *buffer = mEffectBufferValid ? mEffectBuffer : mSinkBuffer;
  3236. audio_format_t format = mEffectBufferValid ? mEffectBufferFormat : mFormat;
  3237. // mono blend occurs for mixer threads only (not direct or offloaded)
  3238. // and is handled here if we're going directly to the sink.
  3239. if (requireMonoBlend() && !mEffectBufferValid) {
  3240. mono_blend(mMixerBuffer, mMixerBufferFormat, mChannelCount, mNormalFrameCount,
  3241. true /*limit*/);
  3242. }
  3243. if (!hasFastMixer()) {
  3244. // Balance must take effect after mono conversion.
  3245. // We do it here if there is no FastMixer.
  3246. // mBalance detects zero balance within the class for speed (not needed here).
  3247. mBalance.setBalance(mMasterBalance.load());
  3248. mBalance.process((float *)mMixerBuffer, mNormalFrameCount);
  3249. }
  3250. memcpy_by_audio_format(buffer, format, mMixerBuffer, mMixerBufferFormat,
  3251. mNormalFrameCount * (mChannelCount + mHapticChannelCount));
  3252. // If we're going directly to the sink and there are haptic channels,
  3253. // we should adjust channels as the sample data is partially interleaved
  3254. // in this case.
  3255. if (!mEffectBufferValid && mHapticChannelCount > 0) {
  3256. adjust_channels_non_destructive(buffer, mChannelCount, buffer,
  3257. mChannelCount + mHapticChannelCount,
  3258. audio_bytes_per_sample(format),
  3259. audio_bytes_per_frame(mChannelCount, format) * mNormalFrameCount);
  3260. }
  3261. }
  3262. mBytesRemaining = mCurrentWriteLength;
  3263. if (isSuspended()) {
  3264. // Simulate write to HAL when suspended (e.g. BT SCO phone call).
  3265. mSleepTimeUs = suspendSleepTimeUs(); // assumes full buffer.
  3266. const size_t framesRemaining = mBytesRemaining / mFrameSize;
  3267. mBytesWritten += mBytesRemaining;
  3268. mFramesWritten += framesRemaining;
  3269. mSuspendedFrames += framesRemaining; // to adjust kernel HAL position
  3270. mBytesRemaining = 0;
  3271. }
  3272. // only process effects if we're going to write
  3273. if (mSleepTimeUs == 0 && mType != OFFLOAD) {
  3274. for (size_t i = 0; i < effectChains.size(); i ++) {
  3275. effectChains[i]->process_l();
  3276. // TODO: Write haptic data directly to sink buffer when mixing.
  3277. if (activeHapticSessionId != AUDIO_SESSION_NONE
  3278. && activeHapticSessionId == effectChains[i]->sessionId()) {
  3279. // Haptic data is active in this case, copy it directly from
  3280. // in buffer to out buffer.
  3281. const size_t audioBufferSize = mNormalFrameCount
  3282. * audio_bytes_per_frame(mChannelCount, EFFECT_BUFFER_FORMAT);
  3283. memcpy_by_audio_format(
  3284. (uint8_t*)effectChains[i]->outBuffer() + audioBufferSize,
  3285. EFFECT_BUFFER_FORMAT,
  3286. (const uint8_t*)effectChains[i]->inBuffer() + audioBufferSize,
  3287. EFFECT_BUFFER_FORMAT, mNormalFrameCount * mHapticChannelCount);
  3288. }
  3289. }
  3290. }
  3291. }
  3292. // Process effect chains for offloaded thread even if no audio
  3293. // was read from audio track: process only updates effect state
  3294. // and thus does have to be synchronized with audio writes but may have
  3295. // to be called while waiting for async write callback
  3296. if (mType == OFFLOAD) {
  3297. for (size_t i = 0; i < effectChains.size(); i ++) {
  3298. effectChains[i]->process_l();
  3299. }
  3300. }
  3301. // Only if the Effects buffer is enabled and there is data in the
  3302. // Effects buffer (buffer valid), we need to
  3303. // copy into the sink buffer.
  3304. // TODO use mSleepTimeUs == 0 as an additional condition.
  3305. if (mEffectBufferValid) {
  3306. //ALOGV("writing effect buffer to sink buffer format %#x", mFormat);
  3307. if (requireMonoBlend()) {
  3308. mono_blend(mEffectBuffer, mEffectBufferFormat, mChannelCount, mNormalFrameCount,
  3309. true /*limit*/);
  3310. }
  3311. if (!hasFastMixer()) {
  3312. // Balance must take effect after mono conversion.
  3313. // We do it here if there is no FastMixer.
  3314. // mBalance detects zero balance within the class for speed (not needed here).
  3315. mBalance.setBalance(mMasterBalance.load());
  3316. mBalance.process((float *)mEffectBuffer, mNormalFrameCount);
  3317. }
  3318. memcpy_by_audio_format(mSinkBuffer, mFormat, mEffectBuffer, mEffectBufferFormat,
  3319. mNormalFrameCount * (mChannelCount + mHapticChannelCount));
  3320. // The sample data is partially interleaved when haptic channels exist,
  3321. // we need to adjust channels here.
  3322. if (mHapticChannelCount > 0) {
  3323. adjust_channels_non_destructive(mSinkBuffer, mChannelCount, mSinkBuffer,
  3324. mChannelCount + mHapticChannelCount,
  3325. audio_bytes_per_sample(mFormat),
  3326. audio_bytes_per_frame(mChannelCount, mFormat) * mNormalFrameCount);
  3327. }
  3328. }
  3329. // enable changes in effect chain
  3330. unlockEffectChains(effectChains);
  3331. if (!waitingAsyncCallback()) {
  3332. // mSleepTimeUs == 0 means we must write to audio hardware
  3333. if (mSleepTimeUs == 0) {
  3334. ssize_t ret = 0;
  3335. // writePeriodNs is updated >= 0 when ret > 0.
  3336. int64_t writePeriodNs = -1;
  3337. if (mBytesRemaining) {
  3338. // FIXME rewrite to reduce number of system calls
  3339. const int64_t lastIoBeginNs = systemTime();
  3340. ret = threadLoop_write();
  3341. const int64_t lastIoEndNs = systemTime();
  3342. if (ret < 0) {
  3343. mBytesRemaining = 0;
  3344. } else if (ret > 0) {
  3345. mBytesWritten += ret;
  3346. mBytesRemaining -= ret;
  3347. const int64_t frames = ret / mFrameSize;
  3348. mFramesWritten += frames;
  3349. writePeriodNs = lastIoEndNs - mLastIoEndNs;
  3350. // process information relating to write time.
  3351. if (audio_has_proportional_frames(mFormat)) {
  3352. // we are in a continuous mixing cycle
  3353. if (mMixerStatus == MIXER_TRACKS_READY &&
  3354. loopCount == lastLoopCountWritten + 1) {
  3355. const double jitterMs =
  3356. TimestampVerifier<int64_t, int64_t>::computeJitterMs(
  3357. {frames, writePeriodNs},
  3358. {0, 0} /* lastTimestamp */, mSampleRate);
  3359. const double processMs =
  3360. (lastIoBeginNs - mLastIoEndNs) * 1e-6;
  3361. Mutex::Autolock _l(mLock);
  3362. mIoJitterMs.add(jitterMs);
  3363. mProcessTimeMs.add(processMs);
  3364. }
  3365. // write blocked detection
  3366. const int64_t deltaWriteNs = lastIoEndNs - lastIoBeginNs;
  3367. if (mType == MIXER && deltaWriteNs > maxPeriod) {
  3368. mNumDelayedWrites++;
  3369. if ((lastIoEndNs - lastWarning) > kWarningThrottleNs) {
  3370. ATRACE_NAME("underrun");
  3371. ALOGW("write blocked for %lld msecs, "
  3372. "%d delayed writes, thread %d",
  3373. (long long)deltaWriteNs / NANOS_PER_MILLISECOND,
  3374. mNumDelayedWrites, mId);
  3375. lastWarning = lastIoEndNs;
  3376. }
  3377. }
  3378. }
  3379. // update timing info.
  3380. mLastIoBeginNs = lastIoBeginNs;
  3381. mLastIoEndNs = lastIoEndNs;
  3382. lastLoopCountWritten = loopCount;
  3383. }
  3384. } else if ((mMixerStatus == MIXER_DRAIN_TRACK) ||
  3385. (mMixerStatus == MIXER_DRAIN_ALL)) {
  3386. threadLoop_drain();
  3387. }
  3388. if (mType == MIXER && !mStandby) {
  3389. if (mThreadThrottle
  3390. && mMixerStatus == MIXER_TRACKS_READY // we are mixing (active tracks)
  3391. && writePeriodNs > 0) { // we have write period info
  3392. // Limit MixerThread data processing to no more than twice the
  3393. // expected processing rate.
  3394. //
  3395. // This helps prevent underruns with NuPlayer and other applications
  3396. // which may set up buffers that are close to the minimum size, or use
  3397. // deep buffers, and rely on a double-buffering sleep strategy to fill.
  3398. //
  3399. // The throttle smooths out sudden large data drains from the device,
  3400. // e.g. when it comes out of standby, which often causes problems with
  3401. // (1) mixer threads without a fast mixer (which has its own warm-up)
  3402. // (2) minimum buffer sized tracks (even if the track is full,
  3403. // the app won't fill fast enough to handle the sudden draw).
  3404. //
  3405. // Total time spent in last processing cycle equals time spent in
  3406. // 1. threadLoop_write, as well as time spent in
  3407. // 2. threadLoop_mix (significant for heavy mixing, especially
  3408. // on low tier processors)
  3409. // it's OK if deltaMs is an overestimate.
  3410. const int32_t deltaMs = writePeriodNs / NANOS_PER_MILLISECOND;
  3411. const int32_t throttleMs = (int32_t)mHalfBufferMs - deltaMs;
  3412. if ((signed)mHalfBufferMs >= throttleMs && throttleMs > 0) {
  3413. usleep(throttleMs * 1000);
  3414. // notify of throttle start on verbose log
  3415. ALOGV_IF(mThreadThrottleEndMs == mThreadThrottleTimeMs,
  3416. "mixer(%p) throttle begin:"
  3417. " ret(%zd) deltaMs(%d) requires sleep %d ms",
  3418. this, ret, deltaMs, throttleMs);
  3419. mThreadThrottleTimeMs += throttleMs;
  3420. // Throttle must be attributed to the previous mixer loop's write time
  3421. // to allow back-to-back throttling.
  3422. // This also ensures proper timing statistics.
  3423. mLastIoEndNs = systemTime(); // we fetch the write end time again.
  3424. } else {
  3425. uint32_t diff = mThreadThrottleTimeMs - mThreadThrottleEndMs;
  3426. if (diff > 0) {
  3427. // notify of throttle end on debug log
  3428. // but prevent spamming for bluetooth
  3429. ALOGD_IF(!audio_is_a2dp_out_device(outDevice()) &&
  3430. !audio_is_hearing_aid_out_device(outDevice()),
  3431. "mixer(%p) throttle end: throttle time(%u)", this, diff);
  3432. mThreadThrottleEndMs = mThreadThrottleTimeMs;
  3433. }
  3434. }
  3435. }
  3436. }
  3437. } else {
  3438. ATRACE_BEGIN("sleep");
  3439. Mutex::Autolock _l(mLock);
  3440. // suspended requires accurate metering of sleep time.
  3441. if (isSuspended()) {
  3442. // advance by expected sleepTime
  3443. timeLoopNextNs += microseconds((nsecs_t)mSleepTimeUs);
  3444. const nsecs_t nowNs = systemTime();
  3445. // compute expected next time vs current time.
  3446. // (negative deltas are treated as delays).
  3447. nsecs_t deltaNs = timeLoopNextNs - nowNs;
  3448. if (deltaNs < -kMaxNextBufferDelayNs) {
  3449. // Delays longer than the max allowed trigger a reset.
  3450. ALOGV("DelayNs: %lld, resetting timeLoopNextNs", (long long) deltaNs);
  3451. deltaNs = microseconds((nsecs_t)mSleepTimeUs);
  3452. timeLoopNextNs = nowNs + deltaNs;
  3453. } else if (deltaNs < 0) {
  3454. // Delays within the max delay allowed: zero the delta/sleepTime
  3455. // to help the system catch up in the next iteration(s)
  3456. ALOGV("DelayNs: %lld, catching-up", (long long) deltaNs);
  3457. deltaNs = 0;
  3458. }
  3459. // update sleep time (which is >= 0)
  3460. mSleepTimeUs = deltaNs / 1000;
  3461. }
  3462. if (!mSignalPending && mConfigEvents.isEmpty() && !exitPending()) {
  3463. mWaitWorkCV.waitRelative(mLock, microseconds((nsecs_t)mSleepTimeUs));
  3464. }
  3465. ATRACE_END();
  3466. }
  3467. }
  3468. // Finally let go of removed track(s), without the lock held
  3469. // since we can't guarantee the destructors won't acquire that
  3470. // same lock. This will also mutate and push a new fast mixer state.
  3471. threadLoop_removeTracks(tracksToRemove);
  3472. tracksToRemove.clear();
  3473. // FIXME I don't understand the need for this here;
  3474. // it was in the original code but maybe the
  3475. // assignment in saveOutputTracks() makes this unnecessary?
  3476. clearOutputTracks();
  3477. // Effect chains will be actually deleted here if they were removed from
  3478. // mEffectChains list during mixing or effects processing
  3479. effectChains.clear();
  3480. // FIXME Note that the above .clear() is no longer necessary since effectChains
  3481. // is now local to this block, but will keep it for now (at least until merge done).
  3482. }
  3483. threadLoop_exit();
  3484. if (!mStandby) {
  3485. threadLoop_standby();
  3486. mStandby = true;
  3487. }
  3488. releaseWakeLock();
  3489. ALOGV("Thread %p type %d exiting", this, mType);
  3490. return false;
  3491. }
  3492. // removeTracks_l() must be called with ThreadBase::mLock held
  3493. void AudioFlinger::PlaybackThread::removeTracks_l(const Vector< sp<Track> >& tracksToRemove)
  3494. {
  3495. for (const auto& track : tracksToRemove) {
  3496. mActiveTracks.remove(track);
  3497. ALOGV("%s(%d): removing track on session %d", __func__, track->id(), track->sessionId());
  3498. sp<EffectChain> chain = getEffectChain_l(track->sessionId());
  3499. if (chain != 0) {
  3500. ALOGV("%s(%d): stopping track on chain %p for session Id: %d",
  3501. __func__, track->id(), chain.get(), track->sessionId());
  3502. chain->decActiveTrackCnt();
  3503. }
  3504. // If an external client track, inform APM we're no longer active, and remove if needed.
  3505. // We do this under lock so that the state is consistent if the Track is destroyed.
  3506. if (track->isExternalTrack()) {
  3507. AudioSystem::stopOutput(track->portId());
  3508. if (track->isTerminated()) {
  3509. AudioSystem::releaseOutput(track->portId());
  3510. }
  3511. }
  3512. if (track->isTerminated()) {
  3513. // remove from our tracks vector
  3514. removeTrack_l(track);
  3515. }
  3516. if ((track->channelMask() & AUDIO_CHANNEL_HAPTIC_ALL) != AUDIO_CHANNEL_NONE
  3517. && mHapticChannelCount > 0) {
  3518. mLock.unlock();
  3519. // Unlock due to VibratorService will lock for this call and will
  3520. // call Tracks.mute/unmute which also require thread's lock.
  3521. AudioFlinger::onExternalVibrationStop(track->getExternalVibration());
  3522. mLock.lock();
  3523. }
  3524. }
  3525. }
  3526. status_t AudioFlinger::PlaybackThread::getTimestamp_l(AudioTimestamp& timestamp)
  3527. {
  3528. if (mNormalSink != 0) {
  3529. ExtendedTimestamp ets;
  3530. status_t status = mNormalSink->getTimestamp(ets);
  3531. if (status == NO_ERROR) {
  3532. status = ets.getBestTimestamp(&timestamp);
  3533. }
  3534. return status;
  3535. }
  3536. if ((mType == OFFLOAD || mType == DIRECT) && mOutput != NULL) {
  3537. uint64_t position64;
  3538. if (mOutput->getPresentationPosition(&position64, &timestamp.mTime) == OK) {
  3539. timestamp.mPosition = (uint32_t)position64;
  3540. if (mDownstreamLatencyStatMs.getN() > 0) {
  3541. const uint32_t positionOffset =
  3542. (uint32_t)(mDownstreamLatencyStatMs.getMean() * mSampleRate * 1e-3);
  3543. if (positionOffset > timestamp.mPosition) {
  3544. timestamp.mPosition = 0;
  3545. } else {
  3546. timestamp.mPosition -= positionOffset;
  3547. }
  3548. }
  3549. return NO_ERROR;
  3550. }
  3551. }
  3552. return INVALID_OPERATION;
  3553. }
  3554. // For dedicated VoIP outputs, let the HAL apply the stream volume. Track volume is
  3555. // still applied by the mixer.
  3556. // All tracks attached to a mixer with flag VOIP_RX are tied to the same
  3557. // stream type STREAM_VOICE_CALL so this will only change the HAL volume once even
  3558. // if more than one track are active
  3559. status_t AudioFlinger::PlaybackThread::handleVoipVolume_l(float *volume)
  3560. {
  3561. status_t result = NO_ERROR;
  3562. if ((mOutput->flags & AUDIO_OUTPUT_FLAG_VOIP_RX) != 0) {
  3563. if (*volume != mLeftVolFloat) {
  3564. result = mOutput->stream->setVolume(*volume, *volume);
  3565. ALOGE_IF(result != OK,
  3566. "Error when setting output stream volume: %d", result);
  3567. if (result == NO_ERROR) {
  3568. mLeftVolFloat = *volume;
  3569. }
  3570. }
  3571. // if stream volume was successfully sent to the HAL, mLeftVolFloat == v here and we
  3572. // remove stream volume contribution from software volume.
  3573. if (mLeftVolFloat == *volume) {
  3574. *volume = 1.0f;
  3575. }
  3576. }
  3577. return result;
  3578. }
  3579. status_t AudioFlinger::MixerThread::createAudioPatch_l(const struct audio_patch *patch,
  3580. audio_patch_handle_t *handle)
  3581. {
  3582. status_t status;
  3583. if (property_get_bool("af.patch_park", false /* default_value */)) {
  3584. // Park FastMixer to avoid potential DOS issues with writing to the HAL
  3585. // or if HAL does not properly lock against access.
  3586. AutoPark<FastMixer> park(mFastMixer);
  3587. status = PlaybackThread::createAudioPatch_l(patch, handle);
  3588. } else {
  3589. status = PlaybackThread::createAudioPatch_l(patch, handle);
  3590. }
  3591. return status;
  3592. }
  3593. status_t AudioFlinger::PlaybackThread::createAudioPatch_l(const struct audio_patch *patch,
  3594. audio_patch_handle_t *handle)
  3595. {
  3596. status_t status = NO_ERROR;
  3597. // store new device and send to effects
  3598. audio_devices_t type = AUDIO_DEVICE_NONE;
  3599. for (unsigned int i = 0; i < patch->num_sinks; i++) {
  3600. type |= patch->sinks[i].ext.device.type;
  3601. }
  3602. audio_port_handle_t sinkPortId = patch->sinks[0].id;
  3603. #ifdef ADD_BATTERY_DATA
  3604. // when changing the audio output device, call addBatteryData to notify
  3605. // the change
  3606. if (mOutDevice != type) {
  3607. uint32_t params = 0;
  3608. // check whether speaker is on
  3609. if (type & AUDIO_DEVICE_OUT_SPEAKER) {
  3610. params |= IMediaPlayerService::kBatteryDataSpeakerOn;
  3611. }
  3612. audio_devices_t deviceWithoutSpeaker
  3613. = AUDIO_DEVICE_OUT_ALL & ~AUDIO_DEVICE_OUT_SPEAKER;
  3614. // check if any other device (except speaker) is on
  3615. if (type & deviceWithoutSpeaker) {
  3616. params |= IMediaPlayerService::kBatteryDataOtherAudioDeviceOn;
  3617. }
  3618. if (params != 0) {
  3619. addBatteryData(params);
  3620. }
  3621. }
  3622. #endif
  3623. for (size_t i = 0; i < mEffectChains.size(); i++) {
  3624. mEffectChains[i]->setDevice_l(type);
  3625. }
  3626. // mPrevOutDevice is the latest device set by createAudioPatch_l(). It is not set when
  3627. // the thread is created so that the first patch creation triggers an ioConfigChanged callback
  3628. bool configChanged = (mPrevOutDevice != type) || (mDeviceId != sinkPortId);
  3629. mOutDevice = type;
  3630. mPatch = *patch;
  3631. if (mOutput->audioHwDev->supportsAudioPatches()) {
  3632. sp<DeviceHalInterface> hwDevice = mOutput->audioHwDev->hwDevice();
  3633. status = hwDevice->createAudioPatch(patch->num_sources,
  3634. patch->sources,
  3635. patch->num_sinks,
  3636. patch->sinks,
  3637. handle);
  3638. } else {
  3639. char *address;
  3640. if (strcmp(patch->sinks[0].ext.device.address, "") != 0) {
  3641. //FIXME: we only support address on first sink with HAL version < 3.0
  3642. address = audio_device_address_to_parameter(
  3643. patch->sinks[0].ext.device.type,
  3644. patch->sinks[0].ext.device.address);
  3645. } else {
  3646. address = (char *)calloc(1, 1);
  3647. }
  3648. AudioParameter param = AudioParameter(String8(address));
  3649. free(address);
  3650. param.addInt(String8(AudioParameter::keyRouting), (int)type);
  3651. status = mOutput->stream->setParameters(param.toString());
  3652. *handle = AUDIO_PATCH_HANDLE_NONE;
  3653. }
  3654. if (configChanged) {
  3655. mPrevOutDevice = type;
  3656. mDeviceId = sinkPortId;
  3657. sendIoConfigEvent_l(AUDIO_OUTPUT_CONFIG_CHANGED);
  3658. }
  3659. return status;
  3660. }
  3661. status_t AudioFlinger::MixerThread::releaseAudioPatch_l(const audio_patch_handle_t handle)
  3662. {
  3663. status_t status;
  3664. if (property_get_bool("af.patch_park", false /* default_value */)) {
  3665. // Park FastMixer to avoid potential DOS issues with writing to the HAL
  3666. // or if HAL does not properly lock against access.
  3667. AutoPark<FastMixer> park(mFastMixer);
  3668. status = PlaybackThread::releaseAudioPatch_l(handle);
  3669. } else {
  3670. status = PlaybackThread::releaseAudioPatch_l(handle);
  3671. }
  3672. return status;
  3673. }
  3674. status_t AudioFlinger::PlaybackThread::releaseAudioPatch_l(const audio_patch_handle_t handle)
  3675. {
  3676. status_t status = NO_ERROR;
  3677. mOutDevice = AUDIO_DEVICE_NONE;
  3678. if (mOutput->audioHwDev->supportsAudioPatches()) {
  3679. sp<DeviceHalInterface> hwDevice = mOutput->audioHwDev->hwDevice();
  3680. status = hwDevice->releaseAudioPatch(handle);
  3681. } else {
  3682. AudioParameter param;
  3683. param.addInt(String8(AudioParameter::keyRouting), 0);
  3684. status = mOutput->stream->setParameters(param.toString());
  3685. }
  3686. return status;
  3687. }
  3688. void AudioFlinger::PlaybackThread::addPatchTrack(const sp<PatchTrack>& track)
  3689. {
  3690. Mutex::Autolock _l(mLock);
  3691. mTracks.add(track);
  3692. }
  3693. void AudioFlinger::PlaybackThread::deletePatchTrack(const sp<PatchTrack>& track)
  3694. {
  3695. Mutex::Autolock _l(mLock);
  3696. destroyTrack_l(track);
  3697. }
  3698. void AudioFlinger::PlaybackThread::toAudioPortConfig(struct audio_port_config *config)
  3699. {
  3700. ThreadBase::toAudioPortConfig(config);
  3701. config->role = AUDIO_PORT_ROLE_SOURCE;
  3702. config->ext.mix.hw_module = mOutput->audioHwDev->handle();
  3703. config->ext.mix.usecase.stream = AUDIO_STREAM_DEFAULT;
  3704. if (mOutput && mOutput->flags != AUDIO_OUTPUT_FLAG_NONE) {
  3705. config->config_mask |= AUDIO_PORT_CONFIG_FLAGS;
  3706. config->flags.output = mOutput->flags;
  3707. }
  3708. }
  3709. // ----------------------------------------------------------------------------
  3710. AudioFlinger::MixerThread::MixerThread(const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output,
  3711. audio_io_handle_t id, audio_devices_t device, bool systemReady, type_t type)
  3712. : PlaybackThread(audioFlinger, output, id, device, type, systemReady),
  3713. // mAudioMixer below
  3714. // mFastMixer below
  3715. mFastMixerFutex(0),
  3716. mMasterMono(false)
  3717. // mOutputSink below
  3718. // mPipeSink below
  3719. // mNormalSink below
  3720. {
  3721. setMasterBalance(audioFlinger->getMasterBalance_l());
  3722. ALOGV("MixerThread() id=%d device=%#x type=%d", id, device, type);
  3723. ALOGV("mSampleRate=%u, mChannelMask=%#x, mChannelCount=%u, mFormat=%#x, mFrameSize=%zu, "
  3724. "mFrameCount=%zu, mNormalFrameCount=%zu",
  3725. mSampleRate, mChannelMask, mChannelCount, mFormat, mFrameSize, mFrameCount,
  3726. mNormalFrameCount);
  3727. mAudioMixer = new AudioMixer(mNormalFrameCount, mSampleRate);
  3728. if (type == DUPLICATING) {
  3729. // The Duplicating thread uses the AudioMixer and delivers data to OutputTracks
  3730. // (downstream MixerThreads) in DuplicatingThread::threadLoop_write().
  3731. // Do not create or use mFastMixer, mOutputSink, mPipeSink, or mNormalSink.
  3732. return;
  3733. }
  3734. // create an NBAIO sink for the HAL output stream, and negotiate
  3735. mOutputSink = new AudioStreamOutSink(output->stream);
  3736. size_t numCounterOffers = 0;
  3737. const NBAIO_Format offers[1] = {Format_from_SR_C(
  3738. mSampleRate, mChannelCount + mHapticChannelCount, mFormat)};
  3739. #if !LOG_NDEBUG
  3740. ssize_t index =
  3741. #else
  3742. (void)
  3743. #endif
  3744. mOutputSink->negotiate(offers, 1, NULL, numCounterOffers);
  3745. ALOG_ASSERT(index == 0);
  3746. // initialize fast mixer depending on configuration
  3747. bool initFastMixer;
  3748. switch (kUseFastMixer) {
  3749. case FastMixer_Never:
  3750. initFastMixer = false;
  3751. break;
  3752. case FastMixer_Always:
  3753. initFastMixer = true;
  3754. break;
  3755. case FastMixer_Static:
  3756. case FastMixer_Dynamic:
  3757. // FastMixer was designed to operate with a HAL that pulls at a regular rate,
  3758. // where the period is less than an experimentally determined threshold that can be
  3759. // scheduled reliably with CFS. However, the BT A2DP HAL is
  3760. // bursty (does not pull at a regular rate) and so cannot operate with FastMixer.
  3761. initFastMixer = mFrameCount < mNormalFrameCount
  3762. && (mOutDevice & AUDIO_DEVICE_OUT_ALL_A2DP) == 0;
  3763. break;
  3764. }
  3765. ALOGW_IF(initFastMixer == false && mFrameCount < mNormalFrameCount,
  3766. "FastMixer is preferred for this sink as frameCount %zu is less than threshold %zu",
  3767. mFrameCount, mNormalFrameCount);
  3768. if (initFastMixer) {
  3769. audio_format_t fastMixerFormat;
  3770. if (mMixerBufferEnabled && mEffectBufferEnabled) {
  3771. fastMixerFormat = AUDIO_FORMAT_PCM_FLOAT;
  3772. } else {
  3773. fastMixerFormat = AUDIO_FORMAT_PCM_16_BIT;
  3774. }
  3775. if (mFormat != fastMixerFormat) {
  3776. // change our Sink format to accept our intermediate precision
  3777. mFormat = fastMixerFormat;
  3778. free(mSinkBuffer);
  3779. mFrameSize = audio_bytes_per_frame(mChannelCount + mHapticChannelCount, mFormat);
  3780. const size_t sinkBufferSize = mNormalFrameCount * mFrameSize;
  3781. (void)posix_memalign(&mSinkBuffer, 32, sinkBufferSize);
  3782. }
  3783. // create a MonoPipe to connect our submix to FastMixer
  3784. NBAIO_Format format = mOutputSink->format();
  3785. // adjust format to match that of the Fast Mixer
  3786. ALOGV("format changed from %#x to %#x", format.mFormat, fastMixerFormat);
  3787. format.mFormat = fastMixerFormat;
  3788. format.mFrameSize = audio_bytes_per_sample(format.mFormat) * format.mChannelCount;
  3789. // This pipe depth compensates for scheduling latency of the normal mixer thread.
  3790. // When it wakes up after a maximum latency, it runs a few cycles quickly before
  3791. // finally blocking. Note the pipe implementation rounds up the request to a power of 2.
  3792. MonoPipe *monoPipe = new MonoPipe(mNormalFrameCount * 4, format, true /*writeCanBlock*/);
  3793. const NBAIO_Format offers[1] = {format};
  3794. size_t numCounterOffers = 0;
  3795. #if !LOG_NDEBUG
  3796. ssize_t index =
  3797. #else
  3798. (void)
  3799. #endif
  3800. monoPipe->negotiate(offers, 1, NULL, numCounterOffers);
  3801. ALOG_ASSERT(index == 0);
  3802. monoPipe->setAvgFrames((mScreenState & 1) ?
  3803. (monoPipe->maxFrames() * 7) / 8 : mNormalFrameCount * 2);
  3804. mPipeSink = monoPipe;
  3805. // create fast mixer and configure it initially with just one fast track for our submix
  3806. mFastMixer = new FastMixer(mId);
  3807. FastMixerStateQueue *sq = mFastMixer->sq();
  3808. #ifdef STATE_QUEUE_DUMP
  3809. sq->setObserverDump(&mStateQueueObserverDump);
  3810. sq->setMutatorDump(&mStateQueueMutatorDump);
  3811. #endif
  3812. FastMixerState *state = sq->begin();
  3813. FastTrack *fastTrack = &state->mFastTracks[0];
  3814. // wrap the source side of the MonoPipe to make it an AudioBufferProvider
  3815. fastTrack->mBufferProvider = new SourceAudioBufferProvider(new MonoPipeReader(monoPipe));
  3816. fastTrack->mVolumeProvider = NULL;
  3817. fastTrack->mChannelMask = mChannelMask | mHapticChannelMask; // mPipeSink channel mask for
  3818. // audio to FastMixer
  3819. fastTrack->mFormat = mFormat; // mPipeSink format for audio to FastMixer
  3820. fastTrack->mHapticPlaybackEnabled = mHapticChannelMask != AUDIO_CHANNEL_NONE;
  3821. fastTrack->mHapticIntensity = AudioMixer::HAPTIC_SCALE_NONE;
  3822. fastTrack->mGeneration++;
  3823. state->mFastTracksGen++;
  3824. state->mTrackMask = 1;
  3825. // fast mixer will use the HAL output sink
  3826. state->mOutputSink = mOutputSink.get();
  3827. state->mOutputSinkGen++;
  3828. state->mFrameCount = mFrameCount;
  3829. // specify sink channel mask when haptic channel mask present as it can not
  3830. // be calculated directly from channel count
  3831. state->mSinkChannelMask = mHapticChannelMask == AUDIO_CHANNEL_NONE
  3832. ? AUDIO_CHANNEL_NONE : mChannelMask | mHapticChannelMask;
  3833. state->mCommand = FastMixerState::COLD_IDLE;
  3834. // already done in constructor initialization list
  3835. //mFastMixerFutex = 0;
  3836. state->mColdFutexAddr = &mFastMixerFutex;
  3837. state->mColdGen++;
  3838. state->mDumpState = &mFastMixerDumpState;
  3839. mFastMixerNBLogWriter = audioFlinger->newWriter_l(kFastMixerLogSize, "FastMixer");
  3840. state->mNBLogWriter = mFastMixerNBLogWriter.get();
  3841. sq->end();
  3842. sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED);
  3843. NBLog::thread_info_t info;
  3844. info.id = mId;
  3845. info.type = NBLog::FASTMIXER;
  3846. mFastMixerNBLogWriter->log<NBLog::EVENT_THREAD_INFO>(info);
  3847. // start the fast mixer
  3848. mFastMixer->run("FastMixer", PRIORITY_URGENT_AUDIO);
  3849. pid_t tid = mFastMixer->getTid();
  3850. sendPrioConfigEvent(getpid(), tid, kPriorityFastMixer, false /*forApp*/);
  3851. stream()->setHalThreadPriority(kPriorityFastMixer);
  3852. #ifdef AUDIO_WATCHDOG
  3853. // create and start the watchdog
  3854. mAudioWatchdog = new AudioWatchdog();
  3855. mAudioWatchdog->setDump(&mAudioWatchdogDump);
  3856. mAudioWatchdog->run("AudioWatchdog", PRIORITY_URGENT_AUDIO);
  3857. tid = mAudioWatchdog->getTid();
  3858. sendPrioConfigEvent(getpid(), tid, kPriorityFastMixer, false /*forApp*/);
  3859. #endif
  3860. } else {
  3861. #ifdef TEE_SINK
  3862. // Only use the MixerThread tee if there is no FastMixer.
  3863. mTee.set(mOutputSink->format(), NBAIO_Tee::TEE_FLAG_OUTPUT_THREAD);
  3864. mTee.setId(std::string("_") + std::to_string(mId) + "_M");
  3865. #endif
  3866. }
  3867. switch (kUseFastMixer) {
  3868. case FastMixer_Never:
  3869. case FastMixer_Dynamic:
  3870. mNormalSink = mOutputSink;
  3871. break;
  3872. case FastMixer_Always:
  3873. mNormalSink = mPipeSink;
  3874. break;
  3875. case FastMixer_Static:
  3876. mNormalSink = initFastMixer ? mPipeSink : mOutputSink;
  3877. break;
  3878. }
  3879. }
  3880. AudioFlinger::MixerThread::~MixerThread()
  3881. {
  3882. if (mFastMixer != 0) {
  3883. FastMixerStateQueue *sq = mFastMixer->sq();
  3884. FastMixerState *state = sq->begin();
  3885. if (state->mCommand == FastMixerState::COLD_IDLE) {
  3886. int32_t old = android_atomic_inc(&mFastMixerFutex);
  3887. if (old == -1) {
  3888. (void) syscall(__NR_futex, &mFastMixerFutex, FUTEX_WAKE_PRIVATE, 1);
  3889. }
  3890. }
  3891. state->mCommand = FastMixerState::EXIT;
  3892. sq->end();
  3893. sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED);
  3894. mFastMixer->join();
  3895. // Though the fast mixer thread has exited, it's state queue is still valid.
  3896. // We'll use that extract the final state which contains one remaining fast track
  3897. // corresponding to our sub-mix.
  3898. state = sq->begin();
  3899. ALOG_ASSERT(state->mTrackMask == 1);
  3900. FastTrack *fastTrack = &state->mFastTracks[0];
  3901. ALOG_ASSERT(fastTrack->mBufferProvider != NULL);
  3902. delete fastTrack->mBufferProvider;
  3903. sq->end(false /*didModify*/);
  3904. mFastMixer.clear();
  3905. #ifdef AUDIO_WATCHDOG
  3906. if (mAudioWatchdog != 0) {
  3907. mAudioWatchdog->requestExit();
  3908. mAudioWatchdog->requestExitAndWait();
  3909. mAudioWatchdog.clear();
  3910. }
  3911. #endif
  3912. }
  3913. mAudioFlinger->unregisterWriter(mFastMixerNBLogWriter);
  3914. delete mAudioMixer;
  3915. }
  3916. uint32_t AudioFlinger::MixerThread::correctLatency_l(uint32_t latency) const
  3917. {
  3918. if (mFastMixer != 0) {
  3919. MonoPipe *pipe = (MonoPipe *)mPipeSink.get();
  3920. latency += (pipe->getAvgFrames() * 1000) / mSampleRate;
  3921. }
  3922. return latency;
  3923. }
  3924. ssize_t AudioFlinger::MixerThread::threadLoop_write()
  3925. {
  3926. // FIXME we should only do one push per cycle; confirm this is true
  3927. // Start the fast mixer if it's not already running
  3928. if (mFastMixer != 0) {
  3929. FastMixerStateQueue *sq = mFastMixer->sq();
  3930. FastMixerState *state = sq->begin();
  3931. if (state->mCommand != FastMixerState::MIX_WRITE &&
  3932. (kUseFastMixer != FastMixer_Dynamic || state->mTrackMask > 1)) {
  3933. if (state->mCommand == FastMixerState::COLD_IDLE) {
  3934. // FIXME workaround for first HAL write being CPU bound on some devices
  3935. ATRACE_BEGIN("write");
  3936. mOutput->write((char *)mSinkBuffer, 0);
  3937. ATRACE_END();
  3938. int32_t old = android_atomic_inc(&mFastMixerFutex);
  3939. if (old == -1) {
  3940. (void) syscall(__NR_futex, &mFastMixerFutex, FUTEX_WAKE_PRIVATE, 1);
  3941. }
  3942. #ifdef AUDIO_WATCHDOG
  3943. if (mAudioWatchdog != 0) {
  3944. mAudioWatchdog->resume();
  3945. }
  3946. #endif
  3947. }
  3948. state->mCommand = FastMixerState::MIX_WRITE;
  3949. #ifdef FAST_THREAD_STATISTICS
  3950. mFastMixerDumpState.increaseSamplingN(mAudioFlinger->isLowRamDevice() ?
  3951. FastThreadDumpState::kSamplingNforLowRamDevice : FastThreadDumpState::kSamplingN);
  3952. #endif
  3953. sq->end();
  3954. sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED);
  3955. if (kUseFastMixer == FastMixer_Dynamic) {
  3956. mNormalSink = mPipeSink;
  3957. }
  3958. } else {
  3959. sq->end(false /*didModify*/);
  3960. }
  3961. }
  3962. return PlaybackThread::threadLoop_write();
  3963. }
  3964. void AudioFlinger::MixerThread::threadLoop_standby()
  3965. {
  3966. // Idle the fast mixer if it's currently running
  3967. if (mFastMixer != 0) {
  3968. FastMixerStateQueue *sq = mFastMixer->sq();
  3969. FastMixerState *state = sq->begin();
  3970. if (!(state->mCommand & FastMixerState::IDLE)) {
  3971. // Report any frames trapped in the Monopipe
  3972. MonoPipe *monoPipe = (MonoPipe *)mPipeSink.get();
  3973. const long long pipeFrames = monoPipe->maxFrames() - monoPipe->availableToWrite();
  3974. mLocalLog.log("threadLoop_standby: framesWritten:%lld suspendedFrames:%lld "
  3975. "monoPipeWritten:%lld monoPipeLeft:%lld",
  3976. (long long)mFramesWritten, (long long)mSuspendedFrames,
  3977. (long long)mPipeSink->framesWritten(), pipeFrames);
  3978. mLocalLog.log("threadLoop_standby: %s", mTimestamp.toString().c_str());
  3979. state->mCommand = FastMixerState::COLD_IDLE;
  3980. state->mColdFutexAddr = &mFastMixerFutex;
  3981. state->mColdGen++;
  3982. mFastMixerFutex = 0;
  3983. sq->end();
  3984. // BLOCK_UNTIL_PUSHED would be insufficient, as we need it to stop doing I/O now
  3985. sq->push(FastMixerStateQueue::BLOCK_UNTIL_ACKED);
  3986. if (kUseFastMixer == FastMixer_Dynamic) {
  3987. mNormalSink = mOutputSink;
  3988. }
  3989. #ifdef AUDIO_WATCHDOG
  3990. if (mAudioWatchdog != 0) {
  3991. mAudioWatchdog->pause();
  3992. }
  3993. #endif
  3994. } else {
  3995. sq->end(false /*didModify*/);
  3996. }
  3997. }
  3998. PlaybackThread::threadLoop_standby();
  3999. }
  4000. bool AudioFlinger::PlaybackThread::waitingAsyncCallback_l()
  4001. {
  4002. return false;
  4003. }
  4004. bool AudioFlinger::PlaybackThread::shouldStandby_l()
  4005. {
  4006. return !mStandby;
  4007. }
  4008. bool AudioFlinger::PlaybackThread::waitingAsyncCallback()
  4009. {
  4010. Mutex::Autolock _l(mLock);
  4011. return waitingAsyncCallback_l();
  4012. }
  4013. // shared by MIXER and DIRECT, overridden by DUPLICATING
  4014. void AudioFlinger::PlaybackThread::threadLoop_standby()
  4015. {
  4016. ALOGV("Audio hardware entering standby, mixer %p, suspend count %d", this, mSuspended);
  4017. mOutput->standby();
  4018. if (mUseAsyncWrite != 0) {
  4019. // discard any pending drain or write ack by incrementing sequence
  4020. mWriteAckSequence = (mWriteAckSequence + 2) & ~1;
  4021. mDrainSequence = (mDrainSequence + 2) & ~1;
  4022. ALOG_ASSERT(mCallbackThread != 0);
  4023. mCallbackThread->setWriteBlocked(mWriteAckSequence);
  4024. mCallbackThread->setDraining(mDrainSequence);
  4025. }
  4026. mHwPaused = false;
  4027. }
  4028. void AudioFlinger::PlaybackThread::onAddNewTrack_l()
  4029. {
  4030. ALOGV("signal playback thread");
  4031. broadcast_l();
  4032. }
  4033. void AudioFlinger::PlaybackThread::onAsyncError()
  4034. {
  4035. for (int i = AUDIO_STREAM_SYSTEM; i < (int)AUDIO_STREAM_CNT; i++) {
  4036. invalidateTracks((audio_stream_type_t)i);
  4037. }
  4038. }
  4039. void AudioFlinger::MixerThread::threadLoop_mix()
  4040. {
  4041. // mix buffers...
  4042. mAudioMixer->process();
  4043. mCurrentWriteLength = mSinkBufferSize;
  4044. // increase sleep time progressively when application underrun condition clears.
  4045. // Only increase sleep time if the mixer is ready for two consecutive times to avoid
  4046. // that a steady state of alternating ready/not ready conditions keeps the sleep time
  4047. // such that we would underrun the audio HAL.
  4048. if ((mSleepTimeUs == 0) && (sleepTimeShift > 0)) {
  4049. sleepTimeShift--;
  4050. }
  4051. mSleepTimeUs = 0;
  4052. mStandbyTimeNs = systemTime() + mStandbyDelayNs;
  4053. //TODO: delay standby when effects have a tail
  4054. }
  4055. void AudioFlinger::MixerThread::threadLoop_sleepTime()
  4056. {
  4057. // If no tracks are ready, sleep once for the duration of an output
  4058. // buffer size, then write 0s to the output
  4059. if (mSleepTimeUs == 0) {
  4060. if (mMixerStatus == MIXER_TRACKS_ENABLED) {
  4061. if (mPipeSink.get() != nullptr && mPipeSink == mNormalSink) {
  4062. // Using the Monopipe availableToWrite, we estimate the
  4063. // sleep time to retry for more data (before we underrun).
  4064. MonoPipe *monoPipe = static_cast<MonoPipe *>(mPipeSink.get());
  4065. const ssize_t availableToWrite = mPipeSink->availableToWrite();
  4066. const size_t pipeFrames = monoPipe->maxFrames();
  4067. const size_t framesLeft = pipeFrames - max(availableToWrite, 0);
  4068. // HAL_framecount <= framesDelay ~ framesLeft / 2 <= Normal_Mixer_framecount
  4069. const size_t framesDelay = std::min(
  4070. mNormalFrameCount, max(framesLeft / 2, mFrameCount));
  4071. ALOGV("pipeFrames:%zu framesLeft:%zu framesDelay:%zu",
  4072. pipeFrames, framesLeft, framesDelay);
  4073. mSleepTimeUs = framesDelay * MICROS_PER_SECOND / mSampleRate;
  4074. } else {
  4075. mSleepTimeUs = mActiveSleepTimeUs >> sleepTimeShift;
  4076. if (mSleepTimeUs < kMinThreadSleepTimeUs) {
  4077. mSleepTimeUs = kMinThreadSleepTimeUs;
  4078. }
  4079. // reduce sleep time in case of consecutive application underruns to avoid
  4080. // starving the audio HAL. As activeSleepTimeUs() is larger than a buffer
  4081. // duration we would end up writing less data than needed by the audio HAL if
  4082. // the condition persists.
  4083. if (sleepTimeShift < kMaxThreadSleepTimeShift) {
  4084. sleepTimeShift++;
  4085. }
  4086. }
  4087. } else {
  4088. mSleepTimeUs = mIdleSleepTimeUs;
  4089. }
  4090. } else if (mBytesWritten != 0 || (mMixerStatus == MIXER_TRACKS_ENABLED)) {
  4091. // clear out mMixerBuffer or mSinkBuffer, to ensure buffers are cleared
  4092. // before effects processing or output.
  4093. if (mMixerBufferValid) {
  4094. memset(mMixerBuffer, 0, mMixerBufferSize);
  4095. } else {
  4096. memset(mSinkBuffer, 0, mSinkBufferSize);
  4097. }
  4098. mSleepTimeUs = 0;
  4099. ALOGV_IF(mBytesWritten == 0 && (mMixerStatus == MIXER_TRACKS_ENABLED),
  4100. "anticipated start");
  4101. }
  4102. // TODO add standby time extension fct of effect tail
  4103. }
  4104. // prepareTracks_l() must be called with ThreadBase::mLock held
  4105. AudioFlinger::PlaybackThread::mixer_state AudioFlinger::MixerThread::prepareTracks_l(
  4106. Vector< sp<Track> > *tracksToRemove)
  4107. {
  4108. // clean up deleted track ids in AudioMixer before allocating new tracks
  4109. (void)mTracks.processDeletedTrackIds([this](int trackId) {
  4110. // for each trackId, destroy it in the AudioMixer
  4111. if (mAudioMixer->exists(trackId)) {
  4112. mAudioMixer->destroy(trackId);
  4113. }
  4114. });
  4115. mTracks.clearDeletedTrackIds();
  4116. mixer_state mixerStatus = MIXER_IDLE;
  4117. // find out which tracks need to be processed
  4118. size_t count = mActiveTracks.size();
  4119. size_t mixedTracks = 0;
  4120. size_t tracksWithEffect = 0;
  4121. // counts only _active_ fast tracks
  4122. size_t fastTracks = 0;
  4123. uint32_t resetMask = 0; // bit mask of fast tracks that need to be reset
  4124. float masterVolume = mMasterVolume;
  4125. bool masterMute = mMasterMute;
  4126. if (masterMute) {
  4127. masterVolume = 0;
  4128. }
  4129. // Delegate master volume control to effect in output mix effect chain if needed
  4130. sp<EffectChain> chain = getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX);
  4131. if (chain != 0) {
  4132. uint32_t v = (uint32_t)(masterVolume * (1 << 24));
  4133. chain->setVolume_l(&v, &v);
  4134. masterVolume = (float)((v + (1 << 23)) >> 24);
  4135. chain.clear();
  4136. }
  4137. // prepare a new state to push
  4138. FastMixerStateQueue *sq = NULL;
  4139. FastMixerState *state = NULL;
  4140. bool didModify = false;
  4141. FastMixerStateQueue::block_t block = FastMixerStateQueue::BLOCK_UNTIL_PUSHED;
  4142. bool coldIdle = false;
  4143. if (mFastMixer != 0) {
  4144. sq = mFastMixer->sq();
  4145. state = sq->begin();
  4146. coldIdle = state->mCommand == FastMixerState::COLD_IDLE;
  4147. }
  4148. mMixerBufferValid = false; // mMixerBuffer has no valid data until appropriate tracks found.
  4149. mEffectBufferValid = false; // mEffectBuffer has no valid data until tracks found.
  4150. // DeferredOperations handles statistics after setting mixerStatus.
  4151. class DeferredOperations {
  4152. public:
  4153. DeferredOperations(mixer_state *mixerStatus)
  4154. : mMixerStatus(mixerStatus) { }
  4155. // when leaving scope, tally frames properly.
  4156. ~DeferredOperations() {
  4157. // Tally underrun frames only if we are actually mixing (MIXER_TRACKS_READY)
  4158. // because that is when the underrun occurs.
  4159. // We do not distinguish between FastTracks and NormalTracks here.
  4160. if (*mMixerStatus == MIXER_TRACKS_READY) {
  4161. for (const auto &underrun : mUnderrunFrames) {
  4162. underrun.first->mAudioTrackServerProxy->tallyUnderrunFrames(
  4163. underrun.second);
  4164. }
  4165. }
  4166. }
  4167. // tallyUnderrunFrames() is called to update the track counters
  4168. // with the number of underrun frames for a particular mixer period.
  4169. // We defer tallying until we know the final mixer status.
  4170. void tallyUnderrunFrames(sp<Track> track, size_t underrunFrames) {
  4171. mUnderrunFrames.emplace_back(track, underrunFrames);
  4172. }
  4173. private:
  4174. const mixer_state * const mMixerStatus;
  4175. std::vector<std::pair<sp<Track>, size_t>> mUnderrunFrames;
  4176. } deferredOperations(&mixerStatus); // implicit nested scope for variable capture
  4177. bool noFastHapticTrack = true;
  4178. for (size_t i=0 ; i<count ; i++) {
  4179. const sp<Track> t = mActiveTracks[i];
  4180. // this const just means the local variable doesn't change
  4181. Track* const track = t.get();
  4182. // process fast tracks
  4183. if (track->isFastTrack()) {
  4184. LOG_ALWAYS_FATAL_IF(mFastMixer.get() == nullptr,
  4185. "%s(%d): FastTrack(%d) present without FastMixer",
  4186. __func__, id(), track->id());
  4187. if (track->getHapticPlaybackEnabled()) {
  4188. noFastHapticTrack = false;
  4189. }
  4190. // It's theoretically possible (though unlikely) for a fast track to be created
  4191. // and then removed within the same normal mix cycle. This is not a problem, as
  4192. // the track never becomes active so it's fast mixer slot is never touched.
  4193. // The converse, of removing an (active) track and then creating a new track
  4194. // at the identical fast mixer slot within the same normal mix cycle,
  4195. // is impossible because the slot isn't marked available until the end of each cycle.
  4196. int j = track->mFastIndex;
  4197. ALOG_ASSERT(0 < j && j < (int)FastMixerState::sMaxFastTracks);
  4198. ALOG_ASSERT(!(mFastTrackAvailMask & (1 << j)));
  4199. FastTrack *fastTrack = &state->mFastTracks[j];
  4200. // Determine whether the track is currently in underrun condition,
  4201. // and whether it had a recent underrun.
  4202. FastTrackDump *ftDump = &mFastMixerDumpState.mTracks[j];
  4203. FastTrackUnderruns underruns = ftDump->mUnderruns;
  4204. uint32_t recentFull = (underruns.mBitFields.mFull -
  4205. track->mObservedUnderruns.mBitFields.mFull) & UNDERRUN_MASK;
  4206. uint32_t recentPartial = (underruns.mBitFields.mPartial -
  4207. track->mObservedUnderruns.mBitFields.mPartial) & UNDERRUN_MASK;
  4208. uint32_t recentEmpty = (underruns.mBitFields.mEmpty -
  4209. track->mObservedUnderruns.mBitFields.mEmpty) & UNDERRUN_MASK;
  4210. uint32_t recentUnderruns = recentPartial + recentEmpty;
  4211. track->mObservedUnderruns = underruns;
  4212. // don't count underruns that occur while stopping or pausing
  4213. // or stopped which can occur when flush() is called while active
  4214. size_t underrunFrames = 0;
  4215. if (!(track->isStopping() || track->isPausing() || track->isStopped()) &&
  4216. recentUnderruns > 0) {
  4217. // FIXME fast mixer will pull & mix partial buffers, but we count as a full underrun
  4218. underrunFrames = recentUnderruns * mFrameCount;
  4219. }
  4220. // Immediately account for FastTrack underruns.
  4221. track->mAudioTrackServerProxy->tallyUnderrunFrames(underrunFrames);
  4222. // This is similar to the state machine for normal tracks,
  4223. // with a few modifications for fast tracks.
  4224. bool isActive = true;
  4225. switch (track->mState) {
  4226. case TrackBase::STOPPING_1:
  4227. // track stays active in STOPPING_1 state until first underrun
  4228. if (recentUnderruns > 0 || track->isTerminated()) {
  4229. track->mState = TrackBase::STOPPING_2;
  4230. }
  4231. break;
  4232. case TrackBase::PAUSING:
  4233. // ramp down is not yet implemented
  4234. track->setPaused();
  4235. break;
  4236. case TrackBase::RESUMING:
  4237. // ramp up is not yet implemented
  4238. track->mState = TrackBase::ACTIVE;
  4239. break;
  4240. case TrackBase::ACTIVE:
  4241. if (recentFull > 0 || recentPartial > 0) {
  4242. // track has provided at least some frames recently: reset retry count
  4243. track->mRetryCount = kMaxTrackRetries;
  4244. }
  4245. if (recentUnderruns == 0) {
  4246. // no recent underruns: stay active
  4247. break;
  4248. }
  4249. // there has recently been an underrun of some kind
  4250. if (track->sharedBuffer() == 0) {
  4251. // were any of the recent underruns "empty" (no frames available)?
  4252. if (recentEmpty == 0) {
  4253. // no, then ignore the partial underruns as they are allowed indefinitely
  4254. break;
  4255. }
  4256. // there has recently been an "empty" underrun: decrement the retry counter
  4257. if (--(track->mRetryCount) > 0) {
  4258. break;
  4259. }
  4260. // indicate to client process that the track was disabled because of underrun;
  4261. // it will then automatically call start() when data is available
  4262. track->disable();
  4263. // remove from active list, but state remains ACTIVE [confusing but true]
  4264. isActive = false;
  4265. break;
  4266. }
  4267. FALLTHROUGH_INTENDED;
  4268. case TrackBase::STOPPING_2:
  4269. case TrackBase::PAUSED:
  4270. case TrackBase::STOPPED:
  4271. case TrackBase::FLUSHED: // flush() while active
  4272. // Check for presentation complete if track is inactive
  4273. // We have consumed all the buffers of this track.
  4274. // This would be incomplete if we auto-paused on underrun
  4275. {
  4276. uint32_t latency = 0;
  4277. status_t result = mOutput->stream->getLatency(&latency);
  4278. ALOGE_IF(result != OK,
  4279. "Error when retrieving output stream latency: %d", result);
  4280. size_t audioHALFrames = (latency * mSampleRate) / 1000;
  4281. int64_t framesWritten = mBytesWritten / mFrameSize;
  4282. if (!(mStandby || track->presentationComplete(framesWritten, audioHALFrames))) {
  4283. // track stays in active list until presentation is complete
  4284. break;
  4285. }
  4286. }
  4287. if (track->isStopping_2()) {
  4288. track->mState = TrackBase::STOPPED;
  4289. }
  4290. if (track->isStopped()) {
  4291. // Can't reset directly, as fast mixer is still polling this track
  4292. // track->reset();
  4293. // So instead mark this track as needing to be reset after push with ack
  4294. resetMask |= 1 << i;
  4295. }
  4296. isActive = false;
  4297. break;
  4298. case TrackBase::IDLE:
  4299. default:
  4300. LOG_ALWAYS_FATAL("unexpected track state %d", track->mState);
  4301. }
  4302. if (isActive) {
  4303. // was it previously inactive?
  4304. if (!(state->mTrackMask & (1 << j))) {
  4305. ExtendedAudioBufferProvider *eabp = track;
  4306. VolumeProvider *vp = track;
  4307. fastTrack->mBufferProvider = eabp;
  4308. fastTrack->mVolumeProvider = vp;
  4309. fastTrack->mChannelMask = track->mChannelMask;
  4310. fastTrack->mFormat = track->mFormat;
  4311. fastTrack->mHapticPlaybackEnabled = track->getHapticPlaybackEnabled();
  4312. fastTrack->mHapticIntensity = track->getHapticIntensity();
  4313. fastTrack->mGeneration++;
  4314. state->mTrackMask |= 1 << j;
  4315. didModify = true;
  4316. // no acknowledgement required for newly active tracks
  4317. }
  4318. sp<AudioTrackServerProxy> proxy = track->mAudioTrackServerProxy;
  4319. float volume;
  4320. if (track->isPlaybackRestricted() || mStreamTypes[track->streamType()].mute) {
  4321. volume = 0.f;
  4322. } else {
  4323. volume = masterVolume * mStreamTypes[track->streamType()].volume;
  4324. }
  4325. handleVoipVolume_l(&volume);
  4326. // cache the combined master volume and stream type volume for fast mixer; this
  4327. // lacks any synchronization or barrier so VolumeProvider may read a stale value
  4328. const float vh = track->getVolumeHandler()->getVolume(
  4329. proxy->framesReleased()).first;
  4330. volume *= vh;
  4331. track->mCachedVolume = volume;
  4332. gain_minifloat_packed_t vlr = proxy->getVolumeLR();
  4333. float vlf = volume * float_from_gain(gain_minifloat_unpack_left(vlr));
  4334. float vrf = volume * float_from_gain(gain_minifloat_unpack_right(vlr));
  4335. track->setFinalVolume((vlf + vrf) / 2.f);
  4336. ++fastTracks;
  4337. } else {
  4338. // was it previously active?
  4339. if (state->mTrackMask & (1 << j)) {
  4340. fastTrack->mBufferProvider = NULL;
  4341. fastTrack->mGeneration++;
  4342. state->mTrackMask &= ~(1 << j);
  4343. didModify = true;
  4344. // If any fast tracks were removed, we must wait for acknowledgement
  4345. // because we're about to decrement the last sp<> on those tracks.
  4346. block = FastMixerStateQueue::BLOCK_UNTIL_ACKED;
  4347. } else {
  4348. // ALOGW rather than LOG_ALWAYS_FATAL because it seems there are cases where an
  4349. // AudioTrack may start (which may not be with a start() but with a write()
  4350. // after underrun) and immediately paused or released. In that case the
  4351. // FastTrack state hasn't had time to update.
  4352. // TODO Remove the ALOGW when this theory is confirmed.
  4353. ALOGW("fast track %d should have been active; "
  4354. "mState=%d, mTrackMask=%#x, recentUnderruns=%u, isShared=%d",
  4355. j, track->mState, state->mTrackMask, recentUnderruns,
  4356. track->sharedBuffer() != 0);
  4357. // Since the FastMixer state already has the track inactive, do nothing here.
  4358. }
  4359. tracksToRemove->add(track);
  4360. // Avoids a misleading display in dumpsys
  4361. track->mObservedUnderruns.mBitFields.mMostRecent = UNDERRUN_FULL;
  4362. }
  4363. if (fastTrack->mHapticPlaybackEnabled != track->getHapticPlaybackEnabled()) {
  4364. fastTrack->mHapticPlaybackEnabled = track->getHapticPlaybackEnabled();
  4365. didModify = true;
  4366. }
  4367. continue;
  4368. }
  4369. { // local variable scope to avoid goto warning
  4370. audio_track_cblk_t* cblk = track->cblk();
  4371. // The first time a track is added we wait
  4372. // for all its buffers to be filled before processing it
  4373. const int trackId = track->id();
  4374. // if an active track doesn't exist in the AudioMixer, create it.
  4375. // use the trackId as the AudioMixer name.
  4376. if (!mAudioMixer->exists(trackId)) {
  4377. status_t status = mAudioMixer->create(
  4378. trackId,
  4379. track->mChannelMask,
  4380. track->mFormat,
  4381. track->mSessionId);
  4382. if (status != OK) {
  4383. ALOGW("%s(): AudioMixer cannot create track(%d)"
  4384. " mask %#x, format %#x, sessionId %d",
  4385. __func__, trackId,
  4386. track->mChannelMask, track->mFormat, track->mSessionId);
  4387. tracksToRemove->add(track);
  4388. track->invalidate(); // consider it dead.
  4389. continue;
  4390. }
  4391. }
  4392. // make sure that we have enough frames to mix one full buffer.
  4393. // enforce this condition only once to enable draining the buffer in case the client
  4394. // app does not call stop() and relies on underrun to stop:
  4395. // hence the test on (mMixerStatus == MIXER_TRACKS_READY) meaning the track was mixed
  4396. // during last round
  4397. size_t desiredFrames;
  4398. const uint32_t sampleRate = track->mAudioTrackServerProxy->getSampleRate();
  4399. AudioPlaybackRate playbackRate = track->mAudioTrackServerProxy->getPlaybackRate();
  4400. desiredFrames = sourceFramesNeededWithTimestretch(
  4401. sampleRate, mNormalFrameCount, mSampleRate, playbackRate.mSpeed);
  4402. // TODO: ONLY USED FOR LEGACY RESAMPLERS, remove when they are removed.
  4403. // add frames already consumed but not yet released by the resampler
  4404. // because mAudioTrackServerProxy->framesReady() will include these frames
  4405. desiredFrames += mAudioMixer->getUnreleasedFrames(trackId);
  4406. uint32_t minFrames = 1;
  4407. if ((track->sharedBuffer() == 0) && !track->isStopped() && !track->isPausing() &&
  4408. (mMixerStatusIgnoringFastTracks == MIXER_TRACKS_READY)) {
  4409. minFrames = desiredFrames;
  4410. }
  4411. size_t framesReady = track->framesReady();
  4412. if (ATRACE_ENABLED()) {
  4413. // I wish we had formatted trace names
  4414. std::string traceName("nRdy");
  4415. traceName += std::to_string(trackId);
  4416. ATRACE_INT(traceName.c_str(), framesReady);
  4417. }
  4418. if ((framesReady >= minFrames) && track->isReady() &&
  4419. !track->isPaused() && !track->isTerminated())
  4420. {
  4421. ALOGVV("track(%d) s=%08x [OK] on thread %p", trackId, cblk->mServer, this);
  4422. mixedTracks++;
  4423. // track->mainBuffer() != mSinkBuffer or mMixerBuffer means
  4424. // there is an effect chain connected to the track
  4425. chain.clear();
  4426. if (track->mainBuffer() != mSinkBuffer &&
  4427. track->mainBuffer() != mMixerBuffer) {
  4428. if (mEffectBufferEnabled) {
  4429. mEffectBufferValid = true; // Later can set directly.
  4430. }
  4431. chain = getEffectChain_l(track->sessionId());
  4432. // Delegate volume control to effect in track effect chain if needed
  4433. if (chain != 0) {
  4434. tracksWithEffect++;
  4435. } else {
  4436. ALOGW("prepareTracks_l(): track(%d) attached to effect but no chain found on "
  4437. "session %d",
  4438. trackId, track->sessionId());
  4439. }
  4440. }
  4441. int param = AudioMixer::VOLUME;
  4442. if (track->mFillingUpStatus == Track::FS_FILLED) {
  4443. // no ramp for the first volume setting
  4444. track->mFillingUpStatus = Track::FS_ACTIVE;
  4445. if (track->mState == TrackBase::RESUMING) {
  4446. track->mState = TrackBase::ACTIVE;
  4447. // If a new track is paused immediately after start, do not ramp on resume.
  4448. if (cblk->mServer != 0) {
  4449. param = AudioMixer::RAMP_VOLUME;
  4450. }
  4451. }
  4452. mAudioMixer->setParameter(trackId, AudioMixer::RESAMPLE, AudioMixer::RESET, NULL);
  4453. mLeftVolFloat = -1.0;
  4454. // FIXME should not make a decision based on mServer
  4455. } else if (cblk->mServer != 0) {
  4456. // If the track is stopped before the first frame was mixed,
  4457. // do not apply ramp
  4458. param = AudioMixer::RAMP_VOLUME;
  4459. }
  4460. // compute volume for this track
  4461. uint32_t vl, vr; // in U8.24 integer format
  4462. float vlf, vrf, vaf; // in [0.0, 1.0] float format
  4463. // read original volumes with volume control
  4464. float v = masterVolume * mStreamTypes[track->streamType()].volume;
  4465. // Always fetch volumeshaper volume to ensure state is updated.
  4466. const sp<AudioTrackServerProxy> proxy = track->mAudioTrackServerProxy;
  4467. const float vh = track->getVolumeHandler()->getVolume(
  4468. track->mAudioTrackServerProxy->framesReleased()).first;
  4469. if (mStreamTypes[track->streamType()].mute || track->isPlaybackRestricted()) {
  4470. v = 0;
  4471. }
  4472. handleVoipVolume_l(&v);
  4473. if (track->isPausing()) {
  4474. vl = vr = 0;
  4475. vlf = vrf = vaf = 0.;
  4476. track->setPaused();
  4477. } else {
  4478. gain_minifloat_packed_t vlr = proxy->getVolumeLR();
  4479. vlf = float_from_gain(gain_minifloat_unpack_left(vlr));
  4480. vrf = float_from_gain(gain_minifloat_unpack_right(vlr));
  4481. // track volumes come from shared memory, so can't be trusted and must be clamped
  4482. if (vlf > GAIN_FLOAT_UNITY) {
  4483. ALOGV("Track left volume out of range: %.3g", vlf);
  4484. vlf = GAIN_FLOAT_UNITY;
  4485. }
  4486. if (vrf > GAIN_FLOAT_UNITY) {
  4487. ALOGV("Track right volume out of range: %.3g", vrf);
  4488. vrf = GAIN_FLOAT_UNITY;
  4489. }
  4490. // now apply the master volume and stream type volume and shaper volume
  4491. vlf *= v * vh;
  4492. vrf *= v * vh;
  4493. // assuming master volume and stream type volume each go up to 1.0,
  4494. // then derive vl and vr as U8.24 versions for the effect chain
  4495. const float scaleto8_24 = MAX_GAIN_INT * MAX_GAIN_INT;
  4496. vl = (uint32_t) (scaleto8_24 * vlf);
  4497. vr = (uint32_t) (scaleto8_24 * vrf);
  4498. // vl and vr are now in U8.24 format
  4499. uint16_t sendLevel = proxy->getSendLevel_U4_12();
  4500. // send level comes from shared memory and so may be corrupt
  4501. if (sendLevel > MAX_GAIN_INT) {
  4502. ALOGV("Track send level out of range: %04X", sendLevel);
  4503. sendLevel = MAX_GAIN_INT;
  4504. }
  4505. // vaf is represented as [0.0, 1.0] float by rescaling sendLevel
  4506. vaf = v * sendLevel * (1. / MAX_GAIN_INT);
  4507. }
  4508. track->setFinalVolume((vrf + vlf) / 2.f);
  4509. // Delegate volume control to effect in track effect chain if needed
  4510. if (chain != 0 && chain->setVolume_l(&vl, &vr)) {
  4511. // Do not ramp volume if volume is controlled by effect
  4512. param = AudioMixer::VOLUME;
  4513. // Update remaining floating point volume levels
  4514. vlf = (float)vl / (1 << 24);
  4515. vrf = (float)vr / (1 << 24);
  4516. track->mHasVolumeController = true;
  4517. } else {
  4518. // force no volume ramp when volume controller was just disabled or removed
  4519. // from effect chain to avoid volume spike
  4520. if (track->mHasVolumeController) {
  4521. param = AudioMixer::VOLUME;
  4522. }
  4523. track->mHasVolumeController = false;
  4524. }
  4525. // XXX: these things DON'T need to be done each time
  4526. mAudioMixer->setBufferProvider(trackId, track);
  4527. mAudioMixer->enable(trackId);
  4528. mAudioMixer->setParameter(trackId, param, AudioMixer::VOLUME0, &vlf);
  4529. mAudioMixer->setParameter(trackId, param, AudioMixer::VOLUME1, &vrf);
  4530. mAudioMixer->setParameter(trackId, param, AudioMixer::AUXLEVEL, &vaf);
  4531. mAudioMixer->setParameter(
  4532. trackId,
  4533. AudioMixer::TRACK,
  4534. AudioMixer::FORMAT, (void *)track->format());
  4535. mAudioMixer->setParameter(
  4536. trackId,
  4537. AudioMixer::TRACK,
  4538. AudioMixer::CHANNEL_MASK, (void *)(uintptr_t)track->channelMask());
  4539. mAudioMixer->setParameter(
  4540. trackId,
  4541. AudioMixer::TRACK,
  4542. AudioMixer::MIXER_CHANNEL_MASK,
  4543. (void *)(uintptr_t)(mChannelMask | mHapticChannelMask));
  4544. // limit track sample rate to 2 x output sample rate, which changes at re-configuration
  4545. uint32_t maxSampleRate = mSampleRate * AUDIO_RESAMPLER_DOWN_RATIO_MAX;
  4546. uint32_t reqSampleRate = proxy->getSampleRate();
  4547. if (reqSampleRate == 0) {
  4548. reqSampleRate = mSampleRate;
  4549. } else if (reqSampleRate > maxSampleRate) {
  4550. reqSampleRate = maxSampleRate;
  4551. }
  4552. mAudioMixer->setParameter(
  4553. trackId,
  4554. AudioMixer::RESAMPLE,
  4555. AudioMixer::SAMPLE_RATE,
  4556. (void *)(uintptr_t)reqSampleRate);
  4557. AudioPlaybackRate playbackRate = proxy->getPlaybackRate();
  4558. mAudioMixer->setParameter(
  4559. trackId,
  4560. AudioMixer::TIMESTRETCH,
  4561. AudioMixer::PLAYBACK_RATE,
  4562. &playbackRate);
  4563. /*
  4564. * Select the appropriate output buffer for the track.
  4565. *
  4566. * Tracks with effects go into their own effects chain buffer
  4567. * and from there into either mEffectBuffer or mSinkBuffer.
  4568. *
  4569. * Other tracks can use mMixerBuffer for higher precision
  4570. * channel accumulation. If this buffer is enabled
  4571. * (mMixerBufferEnabled true), then selected tracks will accumulate
  4572. * into it.
  4573. *
  4574. */
  4575. if (mMixerBufferEnabled
  4576. && (track->mainBuffer() == mSinkBuffer
  4577. || track->mainBuffer() == mMixerBuffer)) {
  4578. mAudioMixer->setParameter(
  4579. trackId,
  4580. AudioMixer::TRACK,
  4581. AudioMixer::MIXER_FORMAT, (void *)mMixerBufferFormat);
  4582. mAudioMixer->setParameter(
  4583. trackId,
  4584. AudioMixer::TRACK,
  4585. AudioMixer::MAIN_BUFFER, (void *)mMixerBuffer);
  4586. // TODO: override track->mainBuffer()?
  4587. mMixerBufferValid = true;
  4588. } else {
  4589. mAudioMixer->setParameter(
  4590. trackId,
  4591. AudioMixer::TRACK,
  4592. AudioMixer::MIXER_FORMAT, (void *)EFFECT_BUFFER_FORMAT);
  4593. mAudioMixer->setParameter(
  4594. trackId,
  4595. AudioMixer::TRACK,
  4596. AudioMixer::MAIN_BUFFER, (void *)track->mainBuffer());
  4597. }
  4598. mAudioMixer->setParameter(
  4599. trackId,
  4600. AudioMixer::TRACK,
  4601. AudioMixer::AUX_BUFFER, (void *)track->auxBuffer());
  4602. mAudioMixer->setParameter(
  4603. trackId,
  4604. AudioMixer::TRACK,
  4605. AudioMixer::HAPTIC_ENABLED, (void *)(uintptr_t)track->getHapticPlaybackEnabled());
  4606. mAudioMixer->setParameter(
  4607. trackId,
  4608. AudioMixer::TRACK,
  4609. AudioMixer::HAPTIC_INTENSITY, (void *)(uintptr_t)track->getHapticIntensity());
  4610. // reset retry count
  4611. track->mRetryCount = kMaxTrackRetries;
  4612. // If one track is ready, set the mixer ready if:
  4613. // - the mixer was not ready during previous round OR
  4614. // - no other track is not ready
  4615. if (mMixerStatusIgnoringFastTracks != MIXER_TRACKS_READY ||
  4616. mixerStatus != MIXER_TRACKS_ENABLED) {
  4617. mixerStatus = MIXER_TRACKS_READY;
  4618. }
  4619. } else {
  4620. size_t underrunFrames = 0;
  4621. if (framesReady < desiredFrames && !track->isStopped() && !track->isPaused()) {
  4622. ALOGV("track(%d) underrun, framesReady(%zu) < framesDesired(%zd)",
  4623. trackId, framesReady, desiredFrames);
  4624. underrunFrames = desiredFrames;
  4625. }
  4626. deferredOperations.tallyUnderrunFrames(track, underrunFrames);
  4627. // clear effect chain input buffer if an active track underruns to avoid sending
  4628. // previous audio buffer again to effects
  4629. chain = getEffectChain_l(track->sessionId());
  4630. if (chain != 0) {
  4631. chain->clearInputBuffer();
  4632. }
  4633. ALOGVV("track(%d) s=%08x [NOT READY] on thread %p", trackId, cblk->mServer, this);
  4634. if ((track->sharedBuffer() != 0) || track->isTerminated() ||
  4635. track->isStopped() || track->isPaused()) {
  4636. // We have consumed all the buffers of this track.
  4637. // Remove it from the list of active tracks.
  4638. // TODO: use actual buffer filling status instead of latency when available from
  4639. // audio HAL
  4640. size_t audioHALFrames = (latency_l() * mSampleRate) / 1000;
  4641. int64_t framesWritten = mBytesWritten / mFrameSize;
  4642. if (mStandby || track->presentationComplete(framesWritten, audioHALFrames)) {
  4643. if (track->isStopped()) {
  4644. track->reset();
  4645. }
  4646. tracksToRemove->add(track);
  4647. }
  4648. } else {
  4649. // No buffers for this track. Give it a few chances to
  4650. // fill a buffer, then remove it from active list.
  4651. if (--(track->mRetryCount) <= 0) {
  4652. ALOGI("BUFFER TIMEOUT: remove(%d) from active list on thread %p",
  4653. trackId, this);
  4654. tracksToRemove->add(track);
  4655. // indicate to client process that the track was disabled because of underrun;
  4656. // it will then automatically call start() when data is available
  4657. track->disable();
  4658. // If one track is not ready, mark the mixer also not ready if:
  4659. // - the mixer was ready during previous round OR
  4660. // - no other track is ready
  4661. } else if (mMixerStatusIgnoringFastTracks == MIXER_TRACKS_READY ||
  4662. mixerStatus != MIXER_TRACKS_READY) {
  4663. mixerStatus = MIXER_TRACKS_ENABLED;
  4664. }
  4665. }
  4666. mAudioMixer->disable(trackId);
  4667. }
  4668. } // local variable scope to avoid goto warning
  4669. }
  4670. if (mHapticChannelMask != AUDIO_CHANNEL_NONE && sq != NULL) {
  4671. // When there is no fast track playing haptic and FastMixer exists,
  4672. // enabling the first FastTrack, which provides mixed data from normal
  4673. // tracks, to play haptic data.
  4674. FastTrack *fastTrack = &state->mFastTracks[0];
  4675. if (fastTrack->mHapticPlaybackEnabled != noFastHapticTrack) {
  4676. fastTrack->mHapticPlaybackEnabled = noFastHapticTrack;
  4677. didModify = true;
  4678. }
  4679. }
  4680. // Push the new FastMixer state if necessary
  4681. bool pauseAudioWatchdog = false;
  4682. if (didModify) {
  4683. state->mFastTracksGen++;
  4684. // if the fast mixer was active, but now there are no fast tracks, then put it in cold idle
  4685. if (kUseFastMixer == FastMixer_Dynamic &&
  4686. state->mCommand == FastMixerState::MIX_WRITE && state->mTrackMask <= 1) {
  4687. state->mCommand = FastMixerState::COLD_IDLE;
  4688. state->mColdFutexAddr = &mFastMixerFutex;
  4689. state->mColdGen++;
  4690. mFastMixerFutex = 0;
  4691. if (kUseFastMixer == FastMixer_Dynamic) {
  4692. mNormalSink = mOutputSink;
  4693. }
  4694. // If we go into cold idle, need to wait for acknowledgement
  4695. // so that fast mixer stops doing I/O.
  4696. block = FastMixerStateQueue::BLOCK_UNTIL_ACKED;
  4697. pauseAudioWatchdog = true;
  4698. }
  4699. }
  4700. if (sq != NULL) {
  4701. sq->end(didModify);
  4702. // No need to block if the FastMixer is in COLD_IDLE as the FastThread
  4703. // is not active. (We BLOCK_UNTIL_ACKED when entering COLD_IDLE
  4704. // when bringing the output sink into standby.)
  4705. //
  4706. // We will get the latest FastMixer state when we come out of COLD_IDLE.
  4707. //
  4708. // This occurs with BT suspend when we idle the FastMixer with
  4709. // active tracks, which may be added or removed.
  4710. sq->push(coldIdle ? FastMixerStateQueue::BLOCK_NEVER : block);
  4711. }
  4712. #ifdef AUDIO_WATCHDOG
  4713. if (pauseAudioWatchdog && mAudioWatchdog != 0) {
  4714. mAudioWatchdog->pause();
  4715. }
  4716. #endif
  4717. // Now perform the deferred reset on fast tracks that have stopped
  4718. while (resetMask != 0) {
  4719. size_t i = __builtin_ctz(resetMask);
  4720. ALOG_ASSERT(i < count);
  4721. resetMask &= ~(1 << i);
  4722. sp<Track> track = mActiveTracks[i];
  4723. ALOG_ASSERT(track->isFastTrack() && track->isStopped());
  4724. track->reset();
  4725. }
  4726. // Track destruction may occur outside of threadLoop once it is removed from active tracks.
  4727. // Ensure the AudioMixer doesn't have a raw "buffer provider" pointer to the track if
  4728. // it ceases to be active, to allow safe removal from the AudioMixer at the start
  4729. // of prepareTracks_l(); this releases any outstanding buffer back to the track.
  4730. // See also the implementation of destroyTrack_l().
  4731. for (const auto &track : *tracksToRemove) {
  4732. const int trackId = track->id();
  4733. if (mAudioMixer->exists(trackId)) { // Normal tracks here, fast tracks in FastMixer.
  4734. mAudioMixer->setBufferProvider(trackId, nullptr /* bufferProvider */);
  4735. }
  4736. }
  4737. // remove all the tracks that need to be...
  4738. removeTracks_l(*tracksToRemove);
  4739. if (getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX) != 0) {
  4740. mEffectBufferValid = true;
  4741. }
  4742. if (mEffectBufferValid) {
  4743. // as long as there are effects we should clear the effects buffer, to avoid
  4744. // passing a non-clean buffer to the effect chain
  4745. memset(mEffectBuffer, 0, mEffectBufferSize);
  4746. }
  4747. // sink or mix buffer must be cleared if all tracks are connected to an
  4748. // effect chain as in this case the mixer will not write to the sink or mix buffer
  4749. // and track effects will accumulate into it
  4750. if ((mBytesRemaining == 0) && ((mixedTracks != 0 && mixedTracks == tracksWithEffect) ||
  4751. (mixedTracks == 0 && fastTracks > 0))) {
  4752. // FIXME as a performance optimization, should remember previous zero status
  4753. if (mMixerBufferValid) {
  4754. memset(mMixerBuffer, 0, mMixerBufferSize);
  4755. // TODO: In testing, mSinkBuffer below need not be cleared because
  4756. // the PlaybackThread::threadLoop() copies mMixerBuffer into mSinkBuffer
  4757. // after mixing.
  4758. //
  4759. // To enforce this guarantee:
  4760. // ((mixedTracks != 0 && mixedTracks == tracksWithEffect) ||
  4761. // (mixedTracks == 0 && fastTracks > 0))
  4762. // must imply MIXER_TRACKS_READY.
  4763. // Later, we may clear buffers regardless, and skip much of this logic.
  4764. }
  4765. // FIXME as a performance optimization, should remember previous zero status
  4766. memset(mSinkBuffer, 0, mNormalFrameCount * mFrameSize);
  4767. }
  4768. // if any fast tracks, then status is ready
  4769. mMixerStatusIgnoringFastTracks = mixerStatus;
  4770. if (fastTracks > 0) {
  4771. mixerStatus = MIXER_TRACKS_READY;
  4772. }
  4773. return mixerStatus;
  4774. }
  4775. // trackCountForUid_l() must be called with ThreadBase::mLock held
  4776. uint32_t AudioFlinger::PlaybackThread::trackCountForUid_l(uid_t uid) const
  4777. {
  4778. uint32_t trackCount = 0;
  4779. for (size_t i = 0; i < mTracks.size() ; i++) {
  4780. if (mTracks[i]->uid() == uid) {
  4781. trackCount++;
  4782. }
  4783. }
  4784. return trackCount;
  4785. }
  4786. // isTrackAllowed_l() must be called with ThreadBase::mLock held
  4787. bool AudioFlinger::MixerThread::isTrackAllowed_l(
  4788. audio_channel_mask_t channelMask, audio_format_t format,
  4789. audio_session_t sessionId, uid_t uid) const
  4790. {
  4791. if (!PlaybackThread::isTrackAllowed_l(channelMask, format, sessionId, uid)) {
  4792. return false;
  4793. }
  4794. // Check validity as we don't call AudioMixer::create() here.
  4795. if (!AudioMixer::isValidFormat(format)) {
  4796. ALOGW("%s: invalid format: %#x", __func__, format);
  4797. return false;
  4798. }
  4799. if (!AudioMixer::isValidChannelMask(channelMask)) {
  4800. ALOGW("%s: invalid channelMask: %#x", __func__, channelMask);
  4801. return false;
  4802. }
  4803. return true;
  4804. }
  4805. // checkForNewParameter_l() must be called with ThreadBase::mLock held
  4806. bool AudioFlinger::MixerThread::checkForNewParameter_l(const String8& keyValuePair,
  4807. status_t& status)
  4808. {
  4809. bool reconfig = false;
  4810. bool a2dpDeviceChanged = false;
  4811. status = NO_ERROR;
  4812. AutoPark<FastMixer> park(mFastMixer);
  4813. AudioParameter param = AudioParameter(keyValuePair);
  4814. int value;
  4815. if (param.getInt(String8(AudioParameter::keySamplingRate), value) == NO_ERROR) {
  4816. reconfig = true;
  4817. }
  4818. if (param.getInt(String8(AudioParameter::keyFormat), value) == NO_ERROR) {
  4819. if (!isValidPcmSinkFormat((audio_format_t) value)) {
  4820. status = BAD_VALUE;
  4821. } else {
  4822. // no need to save value, since it's constant
  4823. reconfig = true;
  4824. }
  4825. }
  4826. if (param.getInt(String8(AudioParameter::keyChannels), value) == NO_ERROR) {
  4827. if (!isValidPcmSinkChannelMask((audio_channel_mask_t) value)) {
  4828. status = BAD_VALUE;
  4829. } else {
  4830. // no need to save value, since it's constant
  4831. reconfig = true;
  4832. }
  4833. }
  4834. if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) {
  4835. // do not accept frame count changes if tracks are open as the track buffer
  4836. // size depends on frame count and correct behavior would not be guaranteed
  4837. // if frame count is changed after track creation
  4838. if (!mTracks.isEmpty()) {
  4839. status = INVALID_OPERATION;
  4840. } else {
  4841. reconfig = true;
  4842. }
  4843. }
  4844. if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) {
  4845. #ifdef ADD_BATTERY_DATA
  4846. // when changing the audio output device, call addBatteryData to notify
  4847. // the change
  4848. if (mOutDevice != value) {
  4849. uint32_t params = 0;
  4850. // check whether speaker is on
  4851. if (value & AUDIO_DEVICE_OUT_SPEAKER) {
  4852. params |= IMediaPlayerService::kBatteryDataSpeakerOn;
  4853. }
  4854. audio_devices_t deviceWithoutSpeaker
  4855. = AUDIO_DEVICE_OUT_ALL & ~AUDIO_DEVICE_OUT_SPEAKER;
  4856. // check if any other device (except speaker) is on
  4857. if (value & deviceWithoutSpeaker) {
  4858. params |= IMediaPlayerService::kBatteryDataOtherAudioDeviceOn;
  4859. }
  4860. if (params != 0) {
  4861. addBatteryData(params);
  4862. }
  4863. }
  4864. #endif
  4865. // forward device change to effects that have requested to be
  4866. // aware of attached audio device.
  4867. if (value != AUDIO_DEVICE_NONE) {
  4868. a2dpDeviceChanged =
  4869. (mOutDevice & AUDIO_DEVICE_OUT_ALL_A2DP) != (value & AUDIO_DEVICE_OUT_ALL_A2DP);
  4870. mOutDevice = value;
  4871. for (size_t i = 0; i < mEffectChains.size(); i++) {
  4872. mEffectChains[i]->setDevice_l(mOutDevice);
  4873. }
  4874. }
  4875. }
  4876. if (status == NO_ERROR) {
  4877. status = mOutput->stream->setParameters(keyValuePair);
  4878. if (!mStandby && status == INVALID_OPERATION) {
  4879. mOutput->standby();
  4880. mStandby = true;
  4881. mBytesWritten = 0;
  4882. status = mOutput->stream->setParameters(keyValuePair);
  4883. }
  4884. if (status == NO_ERROR && reconfig) {
  4885. readOutputParameters_l();
  4886. delete mAudioMixer;
  4887. mAudioMixer = new AudioMixer(mNormalFrameCount, mSampleRate);
  4888. for (const auto &track : mTracks) {
  4889. const int trackId = track->id();
  4890. status_t status = mAudioMixer->create(
  4891. trackId,
  4892. track->mChannelMask,
  4893. track->mFormat,
  4894. track->mSessionId);
  4895. ALOGW_IF(status != NO_ERROR,
  4896. "%s(): AudioMixer cannot create track(%d)"
  4897. " mask %#x, format %#x, sessionId %d",
  4898. __func__,
  4899. trackId, track->mChannelMask, track->mFormat, track->mSessionId);
  4900. }
  4901. sendIoConfigEvent_l(AUDIO_OUTPUT_CONFIG_CHANGED);
  4902. }
  4903. }
  4904. return reconfig || a2dpDeviceChanged;
  4905. }
  4906. void AudioFlinger::MixerThread::dumpInternals_l(int fd, const Vector<String16>& args)
  4907. {
  4908. PlaybackThread::dumpInternals_l(fd, args);
  4909. dprintf(fd, " Thread throttle time (msecs): %u\n", mThreadThrottleTimeMs);
  4910. dprintf(fd, " AudioMixer tracks: %s\n", mAudioMixer->trackNames().c_str());
  4911. dprintf(fd, " Master mono: %s\n", mMasterMono ? "on" : "off");
  4912. dprintf(fd, " Master balance: %f (%s)\n", mMasterBalance.load(),
  4913. (hasFastMixer() ? std::to_string(mFastMixer->getMasterBalance())
  4914. : mBalance.toString()).c_str());
  4915. if (hasFastMixer()) {
  4916. dprintf(fd, " FastMixer thread %p tid=%d", mFastMixer.get(), mFastMixer->getTid());
  4917. // Make a non-atomic copy of fast mixer dump state so it won't change underneath us
  4918. // while we are dumping it. It may be inconsistent, but it won't mutate!
  4919. // This is a large object so we place it on the heap.
  4920. // FIXME 25972958: Need an intelligent copy constructor that does not touch unused pages.
  4921. const std::unique_ptr<FastMixerDumpState> copy =
  4922. std::make_unique<FastMixerDumpState>(mFastMixerDumpState);
  4923. copy->dump(fd);
  4924. #ifdef STATE_QUEUE_DUMP
  4925. // Similar for state queue
  4926. StateQueueObserverDump observerCopy = mStateQueueObserverDump;
  4927. observerCopy.dump(fd);
  4928. StateQueueMutatorDump mutatorCopy = mStateQueueMutatorDump;
  4929. mutatorCopy.dump(fd);
  4930. #endif
  4931. #ifdef AUDIO_WATCHDOG
  4932. if (mAudioWatchdog != 0) {
  4933. // Make a non-atomic copy of audio watchdog dump so it won't change underneath us
  4934. AudioWatchdogDump wdCopy = mAudioWatchdogDump;
  4935. wdCopy.dump(fd);
  4936. }
  4937. #endif
  4938. } else {
  4939. dprintf(fd, " No FastMixer\n");
  4940. }
  4941. }
  4942. uint32_t AudioFlinger::MixerThread::idleSleepTimeUs() const
  4943. {
  4944. return (uint32_t)(((mNormalFrameCount * 1000) / mSampleRate) * 1000) / 2;
  4945. }
  4946. uint32_t AudioFlinger::MixerThread::suspendSleepTimeUs() const
  4947. {
  4948. return (uint32_t)(((mNormalFrameCount * 1000) / mSampleRate) * 1000);
  4949. }
  4950. void AudioFlinger::MixerThread::cacheParameters_l()
  4951. {
  4952. PlaybackThread::cacheParameters_l();
  4953. // FIXME: Relaxed timing because of a certain device that can't meet latency
  4954. // Should be reduced to 2x after the vendor fixes the driver issue
  4955. // increase threshold again due to low power audio mode. The way this warning
  4956. // threshold is calculated and its usefulness should be reconsidered anyway.
  4957. maxPeriod = seconds(mNormalFrameCount) / mSampleRate * 15;
  4958. }
  4959. // ----------------------------------------------------------------------------
  4960. AudioFlinger::DirectOutputThread::DirectOutputThread(const sp<AudioFlinger>& audioFlinger,
  4961. AudioStreamOut* output, audio_io_handle_t id, audio_devices_t device,
  4962. ThreadBase::type_t type, bool systemReady)
  4963. : PlaybackThread(audioFlinger, output, id, device, type, systemReady)
  4964. {
  4965. setMasterBalance(audioFlinger->getMasterBalance_l());
  4966. }
  4967. AudioFlinger::DirectOutputThread::~DirectOutputThread()
  4968. {
  4969. }
  4970. void AudioFlinger::DirectOutputThread::dumpInternals_l(int fd, const Vector<String16>& args)
  4971. {
  4972. PlaybackThread::dumpInternals_l(fd, args);
  4973. dprintf(fd, " Master balance: %f Left: %f Right: %f\n",
  4974. mMasterBalance.load(), mMasterBalanceLeft, mMasterBalanceRight);
  4975. }
  4976. void AudioFlinger::DirectOutputThread::setMasterBalance(float balance)
  4977. {
  4978. Mutex::Autolock _l(mLock);
  4979. if (mMasterBalance != balance) {
  4980. mMasterBalance.store(balance);
  4981. mBalance.computeStereoBalance(balance, &mMasterBalanceLeft, &mMasterBalanceRight);
  4982. broadcast_l();
  4983. }
  4984. }
  4985. void AudioFlinger::DirectOutputThread::processVolume_l(Track *track, bool lastTrack)
  4986. {
  4987. float left, right;
  4988. // Ensure volumeshaper state always advances even when muted.
  4989. const sp<AudioTrackServerProxy> proxy = track->mAudioTrackServerProxy;
  4990. const auto [shaperVolume, shaperActive] = track->getVolumeHandler()->getVolume(
  4991. proxy->framesReleased());
  4992. mVolumeShaperActive = shaperActive;
  4993. if (mMasterMute || mStreamTypes[track->streamType()].mute || track->isPlaybackRestricted()) {
  4994. left = right = 0;
  4995. } else {
  4996. float typeVolume = mStreamTypes[track->streamType()].volume;
  4997. const float v = mMasterVolume * typeVolume * shaperVolume;
  4998. gain_minifloat_packed_t vlr = proxy->getVolumeLR();
  4999. left = float_from_gain(gain_minifloat_unpack_left(vlr));
  5000. if (left > GAIN_FLOAT_UNITY) {
  5001. left = GAIN_FLOAT_UNITY;
  5002. }
  5003. left *= v * mMasterBalanceLeft; // DirectOutputThread balance applied as track volume
  5004. right = float_from_gain(gain_minifloat_unpack_right(vlr));
  5005. if (right > GAIN_FLOAT_UNITY) {
  5006. right = GAIN_FLOAT_UNITY;
  5007. }
  5008. right *= v * mMasterBalanceRight;
  5009. }
  5010. if (lastTrack) {
  5011. track->setFinalVolume((left + right) / 2.f);
  5012. if (left != mLeftVolFloat || right != mRightVolFloat) {
  5013. mLeftVolFloat = left;
  5014. mRightVolFloat = right;
  5015. // Delegate volume control to effect in track effect chain if needed
  5016. // only one effect chain can be present on DirectOutputThread, so if
  5017. // there is one, the track is connected to it
  5018. if (!mEffectChains.isEmpty()) {
  5019. // if effect chain exists, volume is handled by it.
  5020. // Convert volumes from float to 8.24
  5021. uint32_t vl = (uint32_t)(left * (1 << 24));
  5022. uint32_t vr = (uint32_t)(right * (1 << 24));
  5023. // Direct/Offload effect chains set output volume in setVolume_l().
  5024. (void)mEffectChains[0]->setVolume_l(&vl, &vr);
  5025. } else {
  5026. // otherwise we directly set the volume.
  5027. setVolumeForOutput_l(left, right);
  5028. }
  5029. }
  5030. }
  5031. }
  5032. void AudioFlinger::DirectOutputThread::onAddNewTrack_l()
  5033. {
  5034. sp<Track> previousTrack = mPreviousTrack.promote();
  5035. sp<Track> latestTrack = mActiveTracks.getLatest();
  5036. if (previousTrack != 0 && latestTrack != 0) {
  5037. if (mType == DIRECT) {
  5038. if (previousTrack.get() != latestTrack.get()) {
  5039. mFlushPending = true;
  5040. }
  5041. } else /* mType == OFFLOAD */ {
  5042. if (previousTrack->sessionId() != latestTrack->sessionId()) {
  5043. mFlushPending = true;
  5044. }
  5045. }
  5046. } else if (previousTrack == 0) {
  5047. // there could be an old track added back during track transition for direct
  5048. // output, so always issues flush to flush data of the previous track if it
  5049. // was already destroyed with HAL paused, then flush can resume the playback
  5050. mFlushPending = true;
  5051. }
  5052. PlaybackThread::onAddNewTrack_l();
  5053. }
  5054. AudioFlinger::PlaybackThread::mixer_state AudioFlinger::DirectOutputThread::prepareTracks_l(
  5055. Vector< sp<Track> > *tracksToRemove
  5056. )
  5057. {
  5058. size_t count = mActiveTracks.size();
  5059. mixer_state mixerStatus = MIXER_IDLE;
  5060. bool doHwPause = false;
  5061. bool doHwResume = false;
  5062. // find out which tracks need to be processed
  5063. for (const sp<Track> &t : mActiveTracks) {
  5064. if (t->isInvalid()) {
  5065. ALOGW("An invalidated track shouldn't be in active list");
  5066. tracksToRemove->add(t);
  5067. continue;
  5068. }
  5069. Track* const track = t.get();
  5070. #ifdef VERY_VERY_VERBOSE_LOGGING
  5071. audio_track_cblk_t* cblk = track->cblk();
  5072. #endif
  5073. // Only consider last track started for volume and mixer state control.
  5074. // In theory an older track could underrun and restart after the new one starts
  5075. // but as we only care about the transition phase between two tracks on a
  5076. // direct output, it is not a problem to ignore the underrun case.
  5077. sp<Track> l = mActiveTracks.getLatest();
  5078. bool last = l.get() == track;
  5079. if (track->isPausing()) {
  5080. track->setPaused();
  5081. if (mHwSupportsPause && last && !mHwPaused) {
  5082. doHwPause = true;
  5083. mHwPaused = true;
  5084. }
  5085. } else if (track->isFlushPending()) {
  5086. track->flushAck();
  5087. if (last) {
  5088. mFlushPending = true;
  5089. }
  5090. } else if (track->isResumePending()) {
  5091. track->resumeAck();
  5092. if (last) {
  5093. mLeftVolFloat = mRightVolFloat = -1.0;
  5094. if (mHwPaused) {
  5095. doHwResume = true;
  5096. mHwPaused = false;
  5097. }
  5098. }
  5099. }
  5100. // The first time a track is added we wait
  5101. // for all its buffers to be filled before processing it.
  5102. // Allow draining the buffer in case the client
  5103. // app does not call stop() and relies on underrun to stop:
  5104. // hence the test on (track->mRetryCount > 1).
  5105. // If retryCount<=1 then track is about to underrun and be removed.
  5106. // Do not use a high threshold for compressed audio.
  5107. uint32_t minFrames;
  5108. if ((track->sharedBuffer() == 0) && !track->isStopping_1() && !track->isPausing()
  5109. && (track->mRetryCount > 1) && audio_has_proportional_frames(mFormat)) {
  5110. minFrames = mNormalFrameCount;
  5111. } else {
  5112. minFrames = 1;
  5113. }
  5114. if ((track->framesReady() >= minFrames) && track->isReady() && !track->isPaused() &&
  5115. !track->isStopping_2() && !track->isStopped())
  5116. {
  5117. ALOGVV("track(%d) s=%08x [OK]", track->id(), cblk->mServer);
  5118. if (track->mFillingUpStatus == Track::FS_FILLED) {
  5119. track->mFillingUpStatus = Track::FS_ACTIVE;
  5120. if (last) {
  5121. // make sure processVolume_l() will apply new volume even if 0
  5122. mLeftVolFloat = mRightVolFloat = -1.0;
  5123. }
  5124. if (!mHwSupportsPause) {
  5125. track->resumeAck();
  5126. }
  5127. }
  5128. // compute volume for this track
  5129. processVolume_l(track, last);
  5130. if (last) {
  5131. sp<Track> previousTrack = mPreviousTrack.promote();
  5132. if (previousTrack != 0) {
  5133. if (track != previousTrack.get()) {
  5134. // Flush any data still being written from last track
  5135. mBytesRemaining = 0;
  5136. // Invalidate previous track to force a seek when resuming.
  5137. previousTrack->invalidate();
  5138. }
  5139. }
  5140. mPreviousTrack = track;
  5141. // reset retry count
  5142. track->mRetryCount = kMaxTrackRetriesDirect;
  5143. mActiveTrack = t;
  5144. mixerStatus = MIXER_TRACKS_READY;
  5145. if (mHwPaused) {
  5146. doHwResume = true;
  5147. mHwPaused = false;
  5148. }
  5149. }
  5150. } else {
  5151. // clear effect chain input buffer if the last active track started underruns
  5152. // to avoid sending previous audio buffer again to effects
  5153. if (!mEffectChains.isEmpty() && last) {
  5154. mEffectChains[0]->clearInputBuffer();
  5155. }
  5156. if (track->isStopping_1()) {
  5157. track->mState = TrackBase::STOPPING_2;
  5158. if (last && mHwPaused) {
  5159. doHwResume = true;
  5160. mHwPaused = false;
  5161. }
  5162. }
  5163. if ((track->sharedBuffer() != 0) || track->isStopped() ||
  5164. track->isStopping_2() || track->isPaused()) {
  5165. // We have consumed all the buffers of this track.
  5166. // Remove it from the list of active tracks.
  5167. size_t audioHALFrames;
  5168. if (audio_has_proportional_frames(mFormat)) {
  5169. audioHALFrames = (latency_l() * mSampleRate) / 1000;
  5170. } else {
  5171. audioHALFrames = 0;
  5172. }
  5173. int64_t framesWritten = mBytesWritten / mFrameSize;
  5174. if (mStandby || !last ||
  5175. track->presentationComplete(framesWritten, audioHALFrames) ||
  5176. track->isPaused()) {
  5177. if (track->isStopping_2()) {
  5178. track->mState = TrackBase::STOPPED;
  5179. }
  5180. if (track->isStopped()) {
  5181. track->reset();
  5182. }
  5183. tracksToRemove->add(track);
  5184. }
  5185. } else {
  5186. // No buffers for this track. Give it a few chances to
  5187. // fill a buffer, then remove it from active list.
  5188. // Only consider last track started for mixer state control
  5189. if (--(track->mRetryCount) <= 0) {
  5190. ALOGV("BUFFER TIMEOUT: remove track(%d) from active list", track->id());
  5191. tracksToRemove->add(track);
  5192. // indicate to client process that the track was disabled because of underrun;
  5193. // it will then automatically call start() when data is available
  5194. track->disable();
  5195. } else if (last) {
  5196. ALOGW("pause because of UNDERRUN, framesReady = %zu,"
  5197. "minFrames = %u, mFormat = %#x",
  5198. track->framesReady(), minFrames, mFormat);
  5199. mixerStatus = MIXER_TRACKS_ENABLED;
  5200. if (mHwSupportsPause && !mHwPaused && !mStandby) {
  5201. doHwPause = true;
  5202. mHwPaused = true;
  5203. }
  5204. }
  5205. }
  5206. }
  5207. }
  5208. // if an active track did not command a flush, check for pending flush on stopped tracks
  5209. if (!mFlushPending) {
  5210. for (size_t i = 0; i < mTracks.size(); i++) {
  5211. if (mTracks[i]->isFlushPending()) {
  5212. mTracks[i]->flushAck();
  5213. mFlushPending = true;
  5214. }
  5215. }
  5216. }
  5217. // make sure the pause/flush/resume sequence is executed in the right order.
  5218. // If a flush is pending and a track is active but the HW is not paused, force a HW pause
  5219. // before flush and then resume HW. This can happen in case of pause/flush/resume
  5220. // if resume is received before pause is executed.
  5221. if (mHwSupportsPause && !mStandby &&
  5222. (doHwPause || (mFlushPending && !mHwPaused && (count != 0)))) {
  5223. status_t result = mOutput->stream->pause();
  5224. ALOGE_IF(result != OK, "Error when pausing output stream: %d", result);
  5225. }
  5226. if (mFlushPending) {
  5227. flushHw_l();
  5228. }
  5229. if (mHwSupportsPause && !mStandby && doHwResume) {
  5230. status_t result = mOutput->stream->resume();
  5231. ALOGE_IF(result != OK, "Error when resuming output stream: %d", result);
  5232. }
  5233. // remove all the tracks that need to be...
  5234. removeTracks_l(*tracksToRemove);
  5235. return mixerStatus;
  5236. }
  5237. void AudioFlinger::DirectOutputThread::threadLoop_mix()
  5238. {
  5239. size_t frameCount = mFrameCount;
  5240. int8_t *curBuf = (int8_t *)mSinkBuffer;
  5241. // output audio to hardware
  5242. while (frameCount) {
  5243. AudioBufferProvider::Buffer buffer;
  5244. buffer.frameCount = frameCount;
  5245. status_t status = mActiveTrack->getNextBuffer(&buffer);
  5246. if (status != NO_ERROR || buffer.raw == NULL) {
  5247. // no need to pad with 0 for compressed audio
  5248. if (audio_has_proportional_frames(mFormat)) {
  5249. memset(curBuf, 0, frameCount * mFrameSize);
  5250. }
  5251. break;
  5252. }
  5253. memcpy(curBuf, buffer.raw, buffer.frameCount * mFrameSize);
  5254. frameCount -= buffer.frameCount;
  5255. curBuf += buffer.frameCount * mFrameSize;
  5256. mActiveTrack->releaseBuffer(&buffer);
  5257. }
  5258. mCurrentWriteLength = curBuf - (int8_t *)mSinkBuffer;
  5259. mSleepTimeUs = 0;
  5260. mStandbyTimeNs = systemTime() + mStandbyDelayNs;
  5261. mActiveTrack.clear();
  5262. }
  5263. void AudioFlinger::DirectOutputThread::threadLoop_sleepTime()
  5264. {
  5265. // do not write to HAL when paused
  5266. if (mHwPaused || (usesHwAvSync() && mStandby)) {
  5267. mSleepTimeUs = mIdleSleepTimeUs;
  5268. return;
  5269. }
  5270. if (mSleepTimeUs == 0) {
  5271. if (mMixerStatus == MIXER_TRACKS_ENABLED) {
  5272. mSleepTimeUs = mActiveSleepTimeUs;
  5273. } else {
  5274. mSleepTimeUs = mIdleSleepTimeUs;
  5275. }
  5276. } else if (mBytesWritten != 0 && audio_has_proportional_frames(mFormat)) {
  5277. memset(mSinkBuffer, 0, mFrameCount * mFrameSize);
  5278. mSleepTimeUs = 0;
  5279. }
  5280. }
  5281. void AudioFlinger::DirectOutputThread::threadLoop_exit()
  5282. {
  5283. {
  5284. Mutex::Autolock _l(mLock);
  5285. for (size_t i = 0; i < mTracks.size(); i++) {
  5286. if (mTracks[i]->isFlushPending()) {
  5287. mTracks[i]->flushAck();
  5288. mFlushPending = true;
  5289. }
  5290. }
  5291. if (mFlushPending) {
  5292. flushHw_l();
  5293. }
  5294. }
  5295. PlaybackThread::threadLoop_exit();
  5296. }
  5297. // must be called with thread mutex locked
  5298. bool AudioFlinger::DirectOutputThread::shouldStandby_l()
  5299. {
  5300. bool trackPaused = false;
  5301. bool trackStopped = false;
  5302. if ((mType == DIRECT) && audio_is_linear_pcm(mFormat) && !usesHwAvSync()) {
  5303. return !mStandby;
  5304. }
  5305. // do not put the HAL in standby when paused. AwesomePlayer clear the offloaded AudioTrack
  5306. // after a timeout and we will enter standby then.
  5307. if (mTracks.size() > 0) {
  5308. trackPaused = mTracks[mTracks.size() - 1]->isPaused();
  5309. trackStopped = mTracks[mTracks.size() - 1]->isStopped() ||
  5310. mTracks[mTracks.size() - 1]->mState == TrackBase::IDLE;
  5311. }
  5312. return !mStandby && !(trackPaused || (mHwPaused && !trackStopped));
  5313. }
  5314. // checkForNewParameter_l() must be called with ThreadBase::mLock held
  5315. bool AudioFlinger::DirectOutputThread::checkForNewParameter_l(const String8& keyValuePair,
  5316. status_t& status)
  5317. {
  5318. bool reconfig = false;
  5319. bool a2dpDeviceChanged = false;
  5320. status = NO_ERROR;
  5321. AudioParameter param = AudioParameter(keyValuePair);
  5322. int value;
  5323. if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) {
  5324. // forward device change to effects that have requested to be
  5325. // aware of attached audio device.
  5326. if (value != AUDIO_DEVICE_NONE) {
  5327. a2dpDeviceChanged =
  5328. (mOutDevice & AUDIO_DEVICE_OUT_ALL_A2DP) != (value & AUDIO_DEVICE_OUT_ALL_A2DP);
  5329. mOutDevice = value;
  5330. for (size_t i = 0; i < mEffectChains.size(); i++) {
  5331. mEffectChains[i]->setDevice_l(mOutDevice);
  5332. }
  5333. }
  5334. }
  5335. if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) {
  5336. // do not accept frame count changes if tracks are open as the track buffer
  5337. // size depends on frame count and correct behavior would not be garantied
  5338. // if frame count is changed after track creation
  5339. if (!mTracks.isEmpty()) {
  5340. status = INVALID_OPERATION;
  5341. } else {
  5342. reconfig = true;
  5343. }
  5344. }
  5345. if (status == NO_ERROR) {
  5346. status = mOutput->stream->setParameters(keyValuePair);
  5347. if (!mStandby && status == INVALID_OPERATION) {
  5348. mOutput->standby();
  5349. mStandby = true;
  5350. mBytesWritten = 0;
  5351. status = mOutput->stream->setParameters(keyValuePair);
  5352. }
  5353. if (status == NO_ERROR && reconfig) {
  5354. readOutputParameters_l();
  5355. sendIoConfigEvent_l(AUDIO_OUTPUT_CONFIG_CHANGED);
  5356. }
  5357. }
  5358. return reconfig || a2dpDeviceChanged;
  5359. }
  5360. uint32_t AudioFlinger::DirectOutputThread::activeSleepTimeUs() const
  5361. {
  5362. uint32_t time;
  5363. if (audio_has_proportional_frames(mFormat)) {
  5364. time = PlaybackThread::activeSleepTimeUs();
  5365. } else {
  5366. time = kDirectMinSleepTimeUs;
  5367. }
  5368. return time;
  5369. }
  5370. uint32_t AudioFlinger::DirectOutputThread::idleSleepTimeUs() const
  5371. {
  5372. uint32_t time;
  5373. if (audio_has_proportional_frames(mFormat)) {
  5374. time = (uint32_t)(((mFrameCount * 1000) / mSampleRate) * 1000) / 2;
  5375. } else {
  5376. time = kDirectMinSleepTimeUs;
  5377. }
  5378. return time;
  5379. }
  5380. uint32_t AudioFlinger::DirectOutputThread::suspendSleepTimeUs() const
  5381. {
  5382. uint32_t time;
  5383. if (audio_has_proportional_frames(mFormat)) {
  5384. time = (uint32_t)(((mFrameCount * 1000) / mSampleRate) * 1000);
  5385. } else {
  5386. time = kDirectMinSleepTimeUs;
  5387. }
  5388. return time;
  5389. }
  5390. void AudioFlinger::DirectOutputThread::cacheParameters_l()
  5391. {
  5392. PlaybackThread::cacheParameters_l();
  5393. // use shorter standby delay as on normal output to release
  5394. // hardware resources as soon as possible
  5395. // no delay on outputs with HW A/V sync
  5396. if (usesHwAvSync()) {
  5397. mStandbyDelayNs = 0;
  5398. } else if ((mType == OFFLOAD) && !audio_has_proportional_frames(mFormat)) {
  5399. mStandbyDelayNs = kOffloadStandbyDelayNs;
  5400. } else {
  5401. mStandbyDelayNs = microseconds(mActiveSleepTimeUs*2);
  5402. }
  5403. }
  5404. void AudioFlinger::DirectOutputThread::flushHw_l()
  5405. {
  5406. mOutput->flush();
  5407. mHwPaused = false;
  5408. mFlushPending = false;
  5409. mTimestampVerifier.discontinuity(); // DIRECT and OFFLOADED flush resets frame count.
  5410. }
  5411. int64_t AudioFlinger::DirectOutputThread::computeWaitTimeNs_l() const {
  5412. // If a VolumeShaper is active, we must wake up periodically to update volume.
  5413. const int64_t NS_PER_MS = 1000000;
  5414. return mVolumeShaperActive ?
  5415. kMinNormalSinkBufferSizeMs * NS_PER_MS : PlaybackThread::computeWaitTimeNs_l();
  5416. }
  5417. // ----------------------------------------------------------------------------
  5418. AudioFlinger::AsyncCallbackThread::AsyncCallbackThread(
  5419. const wp<AudioFlinger::PlaybackThread>& playbackThread)
  5420. : Thread(false /*canCallJava*/),
  5421. mPlaybackThread(playbackThread),
  5422. mWriteAckSequence(0),
  5423. mDrainSequence(0),
  5424. mAsyncError(false)
  5425. {
  5426. }
  5427. AudioFlinger::AsyncCallbackThread::~AsyncCallbackThread()
  5428. {
  5429. }
  5430. void AudioFlinger::AsyncCallbackThread::onFirstRef()
  5431. {
  5432. run("Offload Cbk", ANDROID_PRIORITY_URGENT_AUDIO);
  5433. }
  5434. bool AudioFlinger::AsyncCallbackThread::threadLoop()
  5435. {
  5436. while (!exitPending()) {
  5437. uint32_t writeAckSequence;
  5438. uint32_t drainSequence;
  5439. bool asyncError;
  5440. {
  5441. Mutex::Autolock _l(mLock);
  5442. while (!((mWriteAckSequence & 1) ||
  5443. (mDrainSequence & 1) ||
  5444. mAsyncError ||
  5445. exitPending())) {
  5446. mWaitWorkCV.wait(mLock);
  5447. }
  5448. if (exitPending()) {
  5449. break;
  5450. }
  5451. ALOGV("AsyncCallbackThread mWriteAckSequence %d mDrainSequence %d",
  5452. mWriteAckSequence, mDrainSequence);
  5453. writeAckSequence = mWriteAckSequence;
  5454. mWriteAckSequence &= ~1;
  5455. drainSequence = mDrainSequence;
  5456. mDrainSequence &= ~1;
  5457. asyncError = mAsyncError;
  5458. mAsyncError = false;
  5459. }
  5460. {
  5461. sp<AudioFlinger::PlaybackThread> playbackThread = mPlaybackThread.promote();
  5462. if (playbackThread != 0) {
  5463. if (writeAckSequence & 1) {
  5464. playbackThread->resetWriteBlocked(writeAckSequence >> 1);
  5465. }
  5466. if (drainSequence & 1) {
  5467. playbackThread->resetDraining(drainSequence >> 1);
  5468. }
  5469. if (asyncError) {
  5470. playbackThread->onAsyncError();
  5471. }
  5472. }
  5473. }
  5474. }
  5475. return false;
  5476. }
  5477. void AudioFlinger::AsyncCallbackThread::exit()
  5478. {
  5479. ALOGV("AsyncCallbackThread::exit");
  5480. Mutex::Autolock _l(mLock);
  5481. requestExit();
  5482. mWaitWorkCV.broadcast();
  5483. }
  5484. void AudioFlinger::AsyncCallbackThread::setWriteBlocked(uint32_t sequence)
  5485. {
  5486. Mutex::Autolock _l(mLock);
  5487. // bit 0 is cleared
  5488. mWriteAckSequence = sequence << 1;
  5489. }
  5490. void AudioFlinger::AsyncCallbackThread::resetWriteBlocked()
  5491. {
  5492. Mutex::Autolock _l(mLock);
  5493. // ignore unexpected callbacks
  5494. if (mWriteAckSequence & 2) {
  5495. mWriteAckSequence |= 1;
  5496. mWaitWorkCV.signal();
  5497. }
  5498. }
  5499. void AudioFlinger::AsyncCallbackThread::setDraining(uint32_t sequence)
  5500. {
  5501. Mutex::Autolock _l(mLock);
  5502. // bit 0 is cleared
  5503. mDrainSequence = sequence << 1;
  5504. }
  5505. void AudioFlinger::AsyncCallbackThread::resetDraining()
  5506. {
  5507. Mutex::Autolock _l(mLock);
  5508. // ignore unexpected callbacks
  5509. if (mDrainSequence & 2) {
  5510. mDrainSequence |= 1;
  5511. mWaitWorkCV.signal();
  5512. }
  5513. }
  5514. void AudioFlinger::AsyncCallbackThread::setAsyncError()
  5515. {
  5516. Mutex::Autolock _l(mLock);
  5517. mAsyncError = true;
  5518. mWaitWorkCV.signal();
  5519. }
  5520. // ----------------------------------------------------------------------------
  5521. AudioFlinger::OffloadThread::OffloadThread(const sp<AudioFlinger>& audioFlinger,
  5522. AudioStreamOut* output, audio_io_handle_t id, uint32_t device, bool systemReady)
  5523. : DirectOutputThread(audioFlinger, output, id, device, OFFLOAD, systemReady),
  5524. mPausedWriteLength(0), mPausedBytesRemaining(0), mKeepWakeLock(true),
  5525. mOffloadUnderrunPosition(~0LL)
  5526. {
  5527. //FIXME: mStandby should be set to true by ThreadBase constructo
  5528. mStandby = true;
  5529. mKeepWakeLock = property_get_bool("ro.audio.offload_wakelock", true /* default_value */);
  5530. }
  5531. void AudioFlinger::OffloadThread::threadLoop_exit()
  5532. {
  5533. if (mFlushPending || mHwPaused) {
  5534. // If a flush is pending or track was paused, just discard buffered data
  5535. flushHw_l();
  5536. } else {
  5537. mMixerStatus = MIXER_DRAIN_ALL;
  5538. threadLoop_drain();
  5539. }
  5540. if (mUseAsyncWrite) {
  5541. ALOG_ASSERT(mCallbackThread != 0);
  5542. mCallbackThread->exit();
  5543. }
  5544. PlaybackThread::threadLoop_exit();
  5545. }
  5546. AudioFlinger::PlaybackThread::mixer_state AudioFlinger::OffloadThread::prepareTracks_l(
  5547. Vector< sp<Track> > *tracksToRemove
  5548. )
  5549. {
  5550. size_t count = mActiveTracks.size();
  5551. mixer_state mixerStatus = MIXER_IDLE;
  5552. bool doHwPause = false;
  5553. bool doHwResume = false;
  5554. ALOGV("OffloadThread::prepareTracks_l active tracks %zu", count);
  5555. // find out which tracks need to be processed
  5556. for (const sp<Track> &t : mActiveTracks) {
  5557. Track* const track = t.get();
  5558. #ifdef VERY_VERY_VERBOSE_LOGGING
  5559. audio_track_cblk_t* cblk = track->cblk();
  5560. #endif
  5561. // Only consider last track started for volume and mixer state control.
  5562. // In theory an older track could underrun and restart after the new one starts
  5563. // but as we only care about the transition phase between two tracks on a
  5564. // direct output, it is not a problem to ignore the underrun case.
  5565. sp<Track> l = mActiveTracks.getLatest();
  5566. bool last = l.get() == track;
  5567. if (track->isInvalid()) {
  5568. ALOGW("An invalidated track shouldn't be in active list");
  5569. tracksToRemove->add(track);
  5570. continue;
  5571. }
  5572. if (track->mState == TrackBase::IDLE) {
  5573. ALOGW("An idle track shouldn't be in active list");
  5574. continue;
  5575. }
  5576. if (track->isPausing()) {
  5577. track->setPaused();
  5578. if (last) {
  5579. if (mHwSupportsPause && !mHwPaused) {
  5580. doHwPause = true;
  5581. mHwPaused = true;
  5582. }
  5583. // If we were part way through writing the mixbuffer to
  5584. // the HAL we must save this until we resume
  5585. // BUG - this will be wrong if a different track is made active,
  5586. // in that case we want to discard the pending data in the
  5587. // mixbuffer and tell the client to present it again when the
  5588. // track is resumed
  5589. mPausedWriteLength = mCurrentWriteLength;
  5590. mPausedBytesRemaining = mBytesRemaining;
  5591. mBytesRemaining = 0; // stop writing
  5592. }
  5593. tracksToRemove->add(track);
  5594. } else if (track->isFlushPending()) {
  5595. if (track->isStopping_1()) {
  5596. track->mRetryCount = kMaxTrackStopRetriesOffload;
  5597. } else {
  5598. track->mRetryCount = kMaxTrackRetriesOffload;
  5599. }
  5600. track->flushAck();
  5601. if (last) {
  5602. mFlushPending = true;
  5603. }
  5604. } else if (track->isResumePending()){
  5605. track->resumeAck();
  5606. if (last) {
  5607. if (mPausedBytesRemaining) {
  5608. // Need to continue write that was interrupted
  5609. mCurrentWriteLength = mPausedWriteLength;
  5610. mBytesRemaining = mPausedBytesRemaining;
  5611. mPausedBytesRemaining = 0;
  5612. }
  5613. if (mHwPaused) {
  5614. doHwResume = true;
  5615. mHwPaused = false;
  5616. // threadLoop_mix() will handle the case that we need to
  5617. // resume an interrupted write
  5618. }
  5619. // enable write to audio HAL
  5620. mSleepTimeUs = 0;
  5621. mLeftVolFloat = mRightVolFloat = -1.0;
  5622. // Do not handle new data in this iteration even if track->framesReady()
  5623. mixerStatus = MIXER_TRACKS_ENABLED;
  5624. }
  5625. } else if (track->framesReady() && track->isReady() &&
  5626. !track->isPaused() && !track->isTerminated() && !track->isStopping_2()) {
  5627. ALOGVV("OffloadThread: track(%d) s=%08x [OK]", track->id(), cblk->mServer);
  5628. if (track->mFillingUpStatus == Track::FS_FILLED) {
  5629. track->mFillingUpStatus = Track::FS_ACTIVE;
  5630. if (last) {
  5631. // make sure processVolume_l() will apply new volume even if 0
  5632. mLeftVolFloat = mRightVolFloat = -1.0;
  5633. }
  5634. }
  5635. if (last) {
  5636. sp<Track> previousTrack = mPreviousTrack.promote();
  5637. if (previousTrack != 0) {
  5638. if (track != previousTrack.get()) {
  5639. // Flush any data still being written from last track
  5640. mBytesRemaining = 0;
  5641. if (mPausedBytesRemaining) {
  5642. // Last track was paused so we also need to flush saved
  5643. // mixbuffer state and invalidate track so that it will
  5644. // re-submit that unwritten data when it is next resumed
  5645. mPausedBytesRemaining = 0;
  5646. // Invalidate is a bit drastic - would be more efficient
  5647. // to have a flag to tell client that some of the
  5648. // previously written data was lost
  5649. previousTrack->invalidate();
  5650. }
  5651. // flush data already sent to the DSP if changing audio session as audio
  5652. // comes from a different source. Also invalidate previous track to force a
  5653. // seek when resuming.
  5654. if (previousTrack->sessionId() != track->sessionId()) {
  5655. previousTrack->invalidate();
  5656. }
  5657. }
  5658. }
  5659. mPreviousTrack = track;
  5660. // reset retry count
  5661. if (track->isStopping_1()) {
  5662. track->mRetryCount = kMaxTrackStopRetriesOffload;
  5663. } else {
  5664. track->mRetryCount = kMaxTrackRetriesOffload;
  5665. }
  5666. mActiveTrack = t;
  5667. mixerStatus = MIXER_TRACKS_READY;
  5668. }
  5669. } else {
  5670. ALOGVV("OffloadThread: track(%d) s=%08x [NOT READY]", track->id(), cblk->mServer);
  5671. if (track->isStopping_1()) {
  5672. if (--(track->mRetryCount) <= 0) {
  5673. // Hardware buffer can hold a large amount of audio so we must
  5674. // wait for all current track's data to drain before we say
  5675. // that the track is stopped.
  5676. if (mBytesRemaining == 0) {
  5677. // Only start draining when all data in mixbuffer
  5678. // has been written
  5679. ALOGV("OffloadThread: underrun and STOPPING_1 -> draining, STOPPING_2");
  5680. track->mState = TrackBase::STOPPING_2; // so presentation completes after
  5681. // drain do not drain if no data was ever sent to HAL (mStandby == true)
  5682. if (last && !mStandby) {
  5683. // do not modify drain sequence if we are already draining. This happens
  5684. // when resuming from pause after drain.
  5685. if ((mDrainSequence & 1) == 0) {
  5686. mSleepTimeUs = 0;
  5687. mStandbyTimeNs = systemTime() + mStandbyDelayNs;
  5688. mixerStatus = MIXER_DRAIN_TRACK;
  5689. mDrainSequence += 2;
  5690. }
  5691. if (mHwPaused) {
  5692. // It is possible to move from PAUSED to STOPPING_1 without
  5693. // a resume so we must ensure hardware is running
  5694. doHwResume = true;
  5695. mHwPaused = false;
  5696. }
  5697. }
  5698. }
  5699. } else if (last) {
  5700. ALOGV("stopping1 underrun retries left %d", track->mRetryCount);
  5701. mixerStatus = MIXER_TRACKS_ENABLED;
  5702. }
  5703. } else if (track->isStopping_2()) {
  5704. // Drain has completed or we are in standby, signal presentation complete
  5705. if (!(mDrainSequence & 1) || !last || mStandby) {
  5706. track->mState = TrackBase::STOPPED;
  5707. uint32_t latency = 0;
  5708. status_t result = mOutput->stream->getLatency(&latency);
  5709. ALOGE_IF(result != OK,
  5710. "Error when retrieving output stream latency: %d", result);
  5711. size_t audioHALFrames = (latency * mSampleRate) / 1000;
  5712. int64_t framesWritten =
  5713. mBytesWritten / mOutput->getFrameSize();
  5714. track->presentationComplete(framesWritten, audioHALFrames);
  5715. track->reset();
  5716. tracksToRemove->add(track);
  5717. // DIRECT and OFFLOADED stop resets frame counts.
  5718. if (!mUseAsyncWrite) {
  5719. // If we don't get explicit drain notification we must
  5720. // register discontinuity regardless of whether this is
  5721. // the previous (!last) or the upcoming (last) track
  5722. // to avoid skipping the discontinuity.
  5723. mTimestampVerifier.discontinuity();
  5724. }
  5725. }
  5726. } else {
  5727. // No buffers for this track. Give it a few chances to
  5728. // fill a buffer, then remove it from active list.
  5729. if (--(track->mRetryCount) <= 0) {
  5730. bool running = false;
  5731. uint64_t position = 0;
  5732. struct timespec unused;
  5733. // The running check restarts the retry counter at least once.
  5734. status_t ret = mOutput->stream->getPresentationPosition(&position, &unused);
  5735. if (ret == NO_ERROR && position != mOffloadUnderrunPosition) {
  5736. running = true;
  5737. mOffloadUnderrunPosition = position;
  5738. }
  5739. if (ret == NO_ERROR) {
  5740. ALOGVV("underrun counter, running(%d): %lld vs %lld", running,
  5741. (long long)position, (long long)mOffloadUnderrunPosition);
  5742. }
  5743. if (running) { // still running, give us more time.
  5744. track->mRetryCount = kMaxTrackRetriesOffload;
  5745. } else {
  5746. ALOGV("OffloadThread: BUFFER TIMEOUT: remove track(%d) from active list",
  5747. track->id());
  5748. tracksToRemove->add(track);
  5749. // tell client process that the track was disabled because of underrun;
  5750. // it will then automatically call start() when data is available
  5751. track->disable();
  5752. }
  5753. } else if (last){
  5754. mixerStatus = MIXER_TRACKS_ENABLED;
  5755. }
  5756. }
  5757. }
  5758. // compute volume for this track
  5759. processVolume_l(track, last);
  5760. }
  5761. // make sure the pause/flush/resume sequence is executed in the right order.
  5762. // If a flush is pending and a track is active but the HW is not paused, force a HW pause
  5763. // before flush and then resume HW. This can happen in case of pause/flush/resume
  5764. // if resume is received before pause is executed.
  5765. if (!mStandby && (doHwPause || (mFlushPending && !mHwPaused && (count != 0)))) {
  5766. status_t result = mOutput->stream->pause();
  5767. ALOGE_IF(result != OK, "Error when pausing output stream: %d", result);
  5768. }
  5769. if (mFlushPending) {
  5770. flushHw_l();
  5771. }
  5772. if (!mStandby && doHwResume) {
  5773. status_t result = mOutput->stream->resume();
  5774. ALOGE_IF(result != OK, "Error when resuming output stream: %d", result);
  5775. }
  5776. // remove all the tracks that need to be...
  5777. removeTracks_l(*tracksToRemove);
  5778. return mixerStatus;
  5779. }
  5780. // must be called with thread mutex locked
  5781. bool AudioFlinger::OffloadThread::waitingAsyncCallback_l()
  5782. {
  5783. ALOGVV("waitingAsyncCallback_l mWriteAckSequence %d mDrainSequence %d",
  5784. mWriteAckSequence, mDrainSequence);
  5785. if (mUseAsyncWrite && ((mWriteAckSequence & 1) || (mDrainSequence & 1))) {
  5786. return true;
  5787. }
  5788. return false;
  5789. }
  5790. bool AudioFlinger::OffloadThread::waitingAsyncCallback()
  5791. {
  5792. Mutex::Autolock _l(mLock);
  5793. return waitingAsyncCallback_l();
  5794. }
  5795. void AudioFlinger::OffloadThread::flushHw_l()
  5796. {
  5797. DirectOutputThread::flushHw_l();
  5798. // Flush anything still waiting in the mixbuffer
  5799. mCurrentWriteLength = 0;
  5800. mBytesRemaining = 0;
  5801. mPausedWriteLength = 0;
  5802. mPausedBytesRemaining = 0;
  5803. // reset bytes written count to reflect that DSP buffers are empty after flush.
  5804. mBytesWritten = 0;
  5805. mOffloadUnderrunPosition = ~0LL;
  5806. if (mUseAsyncWrite) {
  5807. // discard any pending drain or write ack by incrementing sequence
  5808. mWriteAckSequence = (mWriteAckSequence + 2) & ~1;
  5809. mDrainSequence = (mDrainSequence + 2) & ~1;
  5810. ALOG_ASSERT(mCallbackThread != 0);
  5811. mCallbackThread->setWriteBlocked(mWriteAckSequence);
  5812. mCallbackThread->setDraining(mDrainSequence);
  5813. }
  5814. }
  5815. void AudioFlinger::OffloadThread::invalidateTracks(audio_stream_type_t streamType)
  5816. {
  5817. Mutex::Autolock _l(mLock);
  5818. if (PlaybackThread::invalidateTracks_l(streamType)) {
  5819. mFlushPending = true;
  5820. }
  5821. }
  5822. // ----------------------------------------------------------------------------
  5823. AudioFlinger::DuplicatingThread::DuplicatingThread(const sp<AudioFlinger>& audioFlinger,
  5824. AudioFlinger::MixerThread* mainThread, audio_io_handle_t id, bool systemReady)
  5825. : MixerThread(audioFlinger, mainThread->getOutput(), id, mainThread->outDevice(),
  5826. systemReady, DUPLICATING),
  5827. mWaitTimeMs(UINT_MAX)
  5828. {
  5829. addOutputTrack(mainThread);
  5830. }
  5831. AudioFlinger::DuplicatingThread::~DuplicatingThread()
  5832. {
  5833. for (size_t i = 0; i < mOutputTracks.size(); i++) {
  5834. mOutputTracks[i]->destroy();
  5835. }
  5836. }
  5837. void AudioFlinger::DuplicatingThread::threadLoop_mix()
  5838. {
  5839. // mix buffers...
  5840. if (outputsReady(outputTracks)) {
  5841. mAudioMixer->process();
  5842. } else {
  5843. if (mMixerBufferValid) {
  5844. memset(mMixerBuffer, 0, mMixerBufferSize);
  5845. } else {
  5846. memset(mSinkBuffer, 0, mSinkBufferSize);
  5847. }
  5848. }
  5849. mSleepTimeUs = 0;
  5850. writeFrames = mNormalFrameCount;
  5851. mCurrentWriteLength = mSinkBufferSize;
  5852. mStandbyTimeNs = systemTime() + mStandbyDelayNs;
  5853. }
  5854. void AudioFlinger::DuplicatingThread::threadLoop_sleepTime()
  5855. {
  5856. if (mSleepTimeUs == 0) {
  5857. if (mMixerStatus == MIXER_TRACKS_ENABLED) {
  5858. mSleepTimeUs = mActiveSleepTimeUs;
  5859. } else {
  5860. mSleepTimeUs = mIdleSleepTimeUs;
  5861. }
  5862. } else if (mBytesWritten != 0) {
  5863. if (mMixerStatus == MIXER_TRACKS_ENABLED) {
  5864. writeFrames = mNormalFrameCount;
  5865. memset(mSinkBuffer, 0, mSinkBufferSize);
  5866. } else {
  5867. // flush remaining overflow buffers in output tracks
  5868. writeFrames = 0;
  5869. }
  5870. mSleepTimeUs = 0;
  5871. }
  5872. }
  5873. ssize_t AudioFlinger::DuplicatingThread::threadLoop_write()
  5874. {
  5875. for (size_t i = 0; i < outputTracks.size(); i++) {
  5876. const ssize_t actualWritten = outputTracks[i]->write(mSinkBuffer, writeFrames);
  5877. // Consider the first OutputTrack for timestamp and frame counting.
  5878. // The threadLoop() generally assumes writing a full sink buffer size at a time.
  5879. // Here, we correct for writeFrames of 0 (a stop) or underruns because
  5880. // we always claim success.
  5881. if (i == 0) {
  5882. const ssize_t correction = mSinkBufferSize / mFrameSize - actualWritten;
  5883. ALOGD_IF(correction != 0 && writeFrames != 0,
  5884. "%s: writeFrames:%u actualWritten:%zd correction:%zd mFramesWritten:%lld",
  5885. __func__, writeFrames, actualWritten, correction, (long long)mFramesWritten);
  5886. mFramesWritten -= correction;
  5887. }
  5888. // TODO: Report correction for the other output tracks and show in the dump.
  5889. }
  5890. mStandby = false;
  5891. return (ssize_t)mSinkBufferSize;
  5892. }
  5893. void AudioFlinger::DuplicatingThread::threadLoop_standby()
  5894. {
  5895. // DuplicatingThread implements standby by stopping all tracks
  5896. for (size_t i = 0; i < outputTracks.size(); i++) {
  5897. outputTracks[i]->stop();
  5898. }
  5899. }
  5900. void AudioFlinger::DuplicatingThread::dumpInternals_l(int fd, const Vector<String16>& args __unused)
  5901. {
  5902. MixerThread::dumpInternals_l(fd, args);
  5903. std::stringstream ss;
  5904. const size_t numTracks = mOutputTracks.size();
  5905. ss << " " << numTracks << " OutputTracks";
  5906. if (numTracks > 0) {
  5907. ss << ":";
  5908. for (const auto &track : mOutputTracks) {
  5909. const sp<ThreadBase> thread = track->thread().promote();
  5910. ss << " (" << track->id() << " : ";
  5911. if (thread.get() != nullptr) {
  5912. ss << thread.get() << ", " << thread->id();
  5913. } else {
  5914. ss << "null";
  5915. }
  5916. ss << ")";
  5917. }
  5918. }
  5919. ss << "\n";
  5920. std::string result = ss.str();
  5921. write(fd, result.c_str(), result.size());
  5922. }
  5923. void AudioFlinger::DuplicatingThread::saveOutputTracks()
  5924. {
  5925. outputTracks = mOutputTracks;
  5926. }
  5927. void AudioFlinger::DuplicatingThread::clearOutputTracks()
  5928. {
  5929. outputTracks.clear();
  5930. }
  5931. void AudioFlinger::DuplicatingThread::addOutputTrack(MixerThread *thread)
  5932. {
  5933. Mutex::Autolock _l(mLock);
  5934. // The downstream MixerThread consumes thread->frameCount() amount of frames per mix pass.
  5935. // Adjust for thread->sampleRate() to determine minimum buffer frame count.
  5936. // Then triple buffer because Threads do not run synchronously and may not be clock locked.
  5937. const size_t frameCount =
  5938. 3 * sourceFramesNeeded(mSampleRate, thread->frameCount(), thread->sampleRate());
  5939. // TODO: Consider asynchronous sample rate conversion to handle clock disparity
  5940. // from different OutputTracks and their associated MixerThreads (e.g. one may
  5941. // nearly empty and the other may be dropping data).
  5942. sp<OutputTrack> outputTrack = new OutputTrack(thread,
  5943. this,
  5944. mSampleRate,
  5945. mFormat,
  5946. mChannelMask,
  5947. frameCount,
  5948. IPCThreadState::self()->getCallingUid());
  5949. status_t status = outputTrack != 0 ? outputTrack->initCheck() : (status_t) NO_MEMORY;
  5950. if (status != NO_ERROR) {
  5951. ALOGE("addOutputTrack() initCheck failed %d", status);
  5952. return;
  5953. }
  5954. thread->setStreamVolume(AUDIO_STREAM_PATCH, 1.0f);
  5955. mOutputTracks.add(outputTrack);
  5956. ALOGV("addOutputTrack() track %p, on thread %p", outputTrack.get(), thread);
  5957. updateWaitTime_l();
  5958. }
  5959. void AudioFlinger::DuplicatingThread::removeOutputTrack(MixerThread *thread)
  5960. {
  5961. Mutex::Autolock _l(mLock);
  5962. for (size_t i = 0; i < mOutputTracks.size(); i++) {
  5963. if (mOutputTracks[i]->thread() == thread) {
  5964. mOutputTracks[i]->destroy();
  5965. mOutputTracks.removeAt(i);
  5966. updateWaitTime_l();
  5967. if (thread->getOutput() == mOutput) {
  5968. mOutput = NULL;
  5969. }
  5970. return;
  5971. }
  5972. }
  5973. ALOGV("removeOutputTrack(): unknown thread: %p", thread);
  5974. }
  5975. // caller must hold mLock
  5976. void AudioFlinger::DuplicatingThread::updateWaitTime_l()
  5977. {
  5978. mWaitTimeMs = UINT_MAX;
  5979. for (size_t i = 0; i < mOutputTracks.size(); i++) {
  5980. sp<ThreadBase> strong = mOutputTracks[i]->thread().promote();
  5981. if (strong != 0) {
  5982. uint32_t waitTimeMs = (strong->frameCount() * 2 * 1000) / strong->sampleRate();
  5983. if (waitTimeMs < mWaitTimeMs) {
  5984. mWaitTimeMs = waitTimeMs;
  5985. }
  5986. }
  5987. }
  5988. }
  5989. bool AudioFlinger::DuplicatingThread::outputsReady(
  5990. const SortedVector< sp<OutputTrack> > &outputTracks)
  5991. {
  5992. for (size_t i = 0; i < outputTracks.size(); i++) {
  5993. sp<ThreadBase> thread = outputTracks[i]->thread().promote();
  5994. if (thread == 0) {
  5995. ALOGW("DuplicatingThread::outputsReady() could not promote thread on output track %p",
  5996. outputTracks[i].get());
  5997. return false;
  5998. }
  5999. PlaybackThread *playbackThread = (PlaybackThread *)thread.get();
  6000. // see note at standby() declaration
  6001. if (playbackThread->standby() && !playbackThread->isSuspended()) {
  6002. ALOGV("DuplicatingThread output track %p on thread %p Not Ready", outputTracks[i].get(),
  6003. thread.get());
  6004. return false;
  6005. }
  6006. }
  6007. return true;
  6008. }
  6009. void AudioFlinger::DuplicatingThread::sendMetadataToBackend_l(
  6010. const StreamOutHalInterface::SourceMetadata& metadata)
  6011. {
  6012. for (auto& outputTrack : outputTracks) { // not mOutputTracks
  6013. outputTrack->setMetadatas(metadata.tracks);
  6014. }
  6015. }
  6016. uint32_t AudioFlinger::DuplicatingThread::activeSleepTimeUs() const
  6017. {
  6018. return (mWaitTimeMs * 1000) / 2;
  6019. }
  6020. void AudioFlinger::DuplicatingThread::cacheParameters_l()
  6021. {
  6022. // updateWaitTime_l() sets mWaitTimeMs, which affects activeSleepTimeUs(), so call it first
  6023. updateWaitTime_l();
  6024. MixerThread::cacheParameters_l();
  6025. }
  6026. // ----------------------------------------------------------------------------
  6027. // Record
  6028. // ----------------------------------------------------------------------------
  6029. AudioFlinger::RecordThread::RecordThread(const sp<AudioFlinger>& audioFlinger,
  6030. AudioStreamIn *input,
  6031. audio_io_handle_t id,
  6032. audio_devices_t outDevice,
  6033. audio_devices_t inDevice,
  6034. bool systemReady
  6035. ) :
  6036. ThreadBase(audioFlinger, id, outDevice, inDevice, RECORD, systemReady),
  6037. mInput(input),
  6038. mActiveTracks(&this->mLocalLog),
  6039. mRsmpInBuffer(NULL),
  6040. // mRsmpInFrames, mRsmpInFramesP2, and mRsmpInFramesOA are set by readInputParameters_l()
  6041. mRsmpInRear(0)
  6042. , mReadOnlyHeap(new MemoryDealer(kRecordThreadReadOnlyHeapSize,
  6043. "RecordThreadRO", MemoryHeapBase::READ_ONLY))
  6044. // mFastCapture below
  6045. , mFastCaptureFutex(0)
  6046. // mInputSource
  6047. // mPipeSink
  6048. // mPipeSource
  6049. , mPipeFramesP2(0)
  6050. // mPipeMemory
  6051. // mFastCaptureNBLogWriter
  6052. , mFastTrackAvail(false)
  6053. , mBtNrecSuspended(false)
  6054. {
  6055. snprintf(mThreadName, kThreadNameLength, "AudioIn_%X", id);
  6056. mNBLogWriter = audioFlinger->newWriter_l(kLogSize, mThreadName);
  6057. if (mInput != nullptr && mInput->audioHwDev != nullptr) {
  6058. mIsMsdDevice = strcmp(
  6059. mInput->audioHwDev->moduleName(), AUDIO_HARDWARE_MODULE_ID_MSD) == 0;
  6060. }
  6061. readInputParameters_l();
  6062. // TODO: We may also match on address as well as device type for
  6063. // AUDIO_DEVICE_IN_BUS, AUDIO_DEVICE_IN_BLUETOOTH_A2DP, AUDIO_DEVICE_IN_REMOTE_SUBMIX
  6064. mTimestampCorrectedDevices = (audio_devices_t)property_get_int64(
  6065. "audio.timestamp.corrected_input_devices",
  6066. (int64_t)(mIsMsdDevice ? AUDIO_DEVICE_IN_BUS // turn on by default for MSD
  6067. : AUDIO_DEVICE_NONE));
  6068. // create an NBAIO source for the HAL input stream, and negotiate
  6069. mInputSource = new AudioStreamInSource(input->stream);
  6070. size_t numCounterOffers = 0;
  6071. const NBAIO_Format offers[1] = {Format_from_SR_C(mSampleRate, mChannelCount, mFormat)};
  6072. #if !LOG_NDEBUG
  6073. ssize_t index =
  6074. #else
  6075. (void)
  6076. #endif
  6077. mInputSource->negotiate(offers, 1, NULL, numCounterOffers);
  6078. ALOG_ASSERT(index == 0);
  6079. // initialize fast capture depending on configuration
  6080. bool initFastCapture;
  6081. switch (kUseFastCapture) {
  6082. case FastCapture_Never:
  6083. initFastCapture = false;
  6084. ALOGV("%p kUseFastCapture = Never, initFastCapture = false", this);
  6085. break;
  6086. case FastCapture_Always:
  6087. initFastCapture = true;
  6088. ALOGV("%p kUseFastCapture = Always, initFastCapture = true", this);
  6089. break;
  6090. case FastCapture_Static:
  6091. initFastCapture = (mFrameCount * 1000) / mSampleRate < kMinNormalCaptureBufferSizeMs;
  6092. ALOGV("%p kUseFastCapture = Static, (%lld * 1000) / %u vs %u, initFastCapture = %d",
  6093. this, (long long)mFrameCount, mSampleRate, kMinNormalCaptureBufferSizeMs,
  6094. initFastCapture);
  6095. break;
  6096. // case FastCapture_Dynamic:
  6097. }
  6098. if (initFastCapture) {
  6099. // create a Pipe for FastCapture to write to, and for us and fast tracks to read from
  6100. NBAIO_Format format = mInputSource->format();
  6101. // quadruple-buffering of 20 ms each; this ensures we can sleep for 20ms in RecordThread
  6102. size_t pipeFramesP2 = roundup(4 * FMS_20 * mSampleRate / 1000);
  6103. size_t pipeSize = pipeFramesP2 * Format_frameSize(format);
  6104. void *pipeBuffer = nullptr;
  6105. const sp<MemoryDealer> roHeap(readOnlyHeap());
  6106. sp<IMemory> pipeMemory;
  6107. if ((roHeap == 0) ||
  6108. (pipeMemory = roHeap->allocate(pipeSize)) == 0 ||
  6109. (pipeBuffer = pipeMemory->pointer()) == nullptr) {
  6110. ALOGE("not enough memory for pipe buffer size=%zu; "
  6111. "roHeap=%p, pipeMemory=%p, pipeBuffer=%p; roHeapSize: %lld",
  6112. pipeSize, roHeap.get(), pipeMemory.get(), pipeBuffer,
  6113. (long long)kRecordThreadReadOnlyHeapSize);
  6114. goto failed;
  6115. }
  6116. // pipe will be shared directly with fast clients, so clear to avoid leaking old information
  6117. memset(pipeBuffer, 0, pipeSize);
  6118. Pipe *pipe = new Pipe(pipeFramesP2, format, pipeBuffer);
  6119. const NBAIO_Format offers[1] = {format};
  6120. size_t numCounterOffers = 0;
  6121. ssize_t index = pipe->negotiate(offers, 1, NULL, numCounterOffers);
  6122. ALOG_ASSERT(index == 0);
  6123. mPipeSink = pipe;
  6124. PipeReader *pipeReader = new PipeReader(*pipe);
  6125. numCounterOffers = 0;
  6126. index = pipeReader->negotiate(offers, 1, NULL, numCounterOffers);
  6127. ALOG_ASSERT(index == 0);
  6128. mPipeSource = pipeReader;
  6129. mPipeFramesP2 = pipeFramesP2;
  6130. mPipeMemory = pipeMemory;
  6131. // create fast capture
  6132. mFastCapture = new FastCapture();
  6133. FastCaptureStateQueue *sq = mFastCapture->sq();
  6134. #ifdef STATE_QUEUE_DUMP
  6135. // FIXME
  6136. #endif
  6137. FastCaptureState *state = sq->begin();
  6138. state->mCblk = NULL;
  6139. state->mInputSource = mInputSource.get();
  6140. state->mInputSourceGen++;
  6141. state->mPipeSink = pipe;
  6142. state->mPipeSinkGen++;
  6143. state->mFrameCount = mFrameCount;
  6144. state->mCommand = FastCaptureState::COLD_IDLE;
  6145. // already done in constructor initialization list
  6146. //mFastCaptureFutex = 0;
  6147. state->mColdFutexAddr = &mFastCaptureFutex;
  6148. state->mColdGen++;
  6149. state->mDumpState = &mFastCaptureDumpState;
  6150. #ifdef TEE_SINK
  6151. // FIXME
  6152. #endif
  6153. mFastCaptureNBLogWriter = audioFlinger->newWriter_l(kFastCaptureLogSize, "FastCapture");
  6154. state->mNBLogWriter = mFastCaptureNBLogWriter.get();
  6155. sq->end();
  6156. sq->push(FastCaptureStateQueue::BLOCK_UNTIL_PUSHED);
  6157. // start the fast capture
  6158. mFastCapture->run("FastCapture", ANDROID_PRIORITY_URGENT_AUDIO);
  6159. pid_t tid = mFastCapture->getTid();
  6160. sendPrioConfigEvent(getpid(), tid, kPriorityFastCapture, false /*forApp*/);
  6161. stream()->setHalThreadPriority(kPriorityFastCapture);
  6162. #ifdef AUDIO_WATCHDOG
  6163. // FIXME
  6164. #endif
  6165. mFastTrackAvail = true;
  6166. }
  6167. #ifdef TEE_SINK
  6168. mTee.set(mInputSource->format(), NBAIO_Tee::TEE_FLAG_INPUT_THREAD);
  6169. mTee.setId(std::string("_") + std::to_string(mId) + "_C");
  6170. #endif
  6171. failed: ;
  6172. // FIXME mNormalSource
  6173. }
  6174. AudioFlinger::RecordThread::~RecordThread()
  6175. {
  6176. if (mFastCapture != 0) {
  6177. FastCaptureStateQueue *sq = mFastCapture->sq();
  6178. FastCaptureState *state = sq->begin();
  6179. if (state->mCommand == FastCaptureState::COLD_IDLE) {
  6180. int32_t old = android_atomic_inc(&mFastCaptureFutex);
  6181. if (old == -1) {
  6182. (void) syscall(__NR_futex, &mFastCaptureFutex, FUTEX_WAKE_PRIVATE, 1);
  6183. }
  6184. }
  6185. state->mCommand = FastCaptureState::EXIT;
  6186. sq->end();
  6187. sq->push(FastCaptureStateQueue::BLOCK_UNTIL_PUSHED);
  6188. mFastCapture->join();
  6189. mFastCapture.clear();
  6190. }
  6191. mAudioFlinger->unregisterWriter(mFastCaptureNBLogWriter);
  6192. mAudioFlinger->unregisterWriter(mNBLogWriter);
  6193. free(mRsmpInBuffer);
  6194. }
  6195. void AudioFlinger::RecordThread::onFirstRef()
  6196. {
  6197. run(mThreadName, PRIORITY_URGENT_AUDIO);
  6198. }
  6199. void AudioFlinger::RecordThread::preExit()
  6200. {
  6201. ALOGV(" preExit()");
  6202. Mutex::Autolock _l(mLock);
  6203. for (size_t i = 0; i < mTracks.size(); i++) {
  6204. sp<RecordTrack> track = mTracks[i];
  6205. track->invalidate();
  6206. }
  6207. mActiveTracks.clear();
  6208. mStartStopCond.broadcast();
  6209. }
  6210. bool AudioFlinger::RecordThread::threadLoop()
  6211. {
  6212. nsecs_t lastWarning = 0;
  6213. inputStandBy();
  6214. reacquire_wakelock:
  6215. sp<RecordTrack> activeTrack;
  6216. {
  6217. Mutex::Autolock _l(mLock);
  6218. acquireWakeLock_l();
  6219. }
  6220. // used to request a deferred sleep, to be executed later while mutex is unlocked
  6221. uint32_t sleepUs = 0;
  6222. int64_t lastLoopCountRead = -2; // never matches "previous" loop, when loopCount = 0.
  6223. // loop while there is work to do
  6224. for (int64_t loopCount = 0;; ++loopCount) { // loopCount used for statistics tracking
  6225. Vector< sp<EffectChain> > effectChains;
  6226. // activeTracks accumulates a copy of a subset of mActiveTracks
  6227. Vector< sp<RecordTrack> > activeTracks;
  6228. // reference to the (first and only) active fast track
  6229. sp<RecordTrack> fastTrack;
  6230. // reference to a fast track which is about to be removed
  6231. sp<RecordTrack> fastTrackToRemove;
  6232. { // scope for mLock
  6233. Mutex::Autolock _l(mLock);
  6234. processConfigEvents_l();
  6235. // check exitPending here because checkForNewParameters_l() and
  6236. // checkForNewParameters_l() can temporarily release mLock
  6237. if (exitPending()) {
  6238. break;
  6239. }
  6240. // sleep with mutex unlocked
  6241. if (sleepUs > 0) {
  6242. ATRACE_BEGIN("sleepC");
  6243. mWaitWorkCV.waitRelative(mLock, microseconds((nsecs_t)sleepUs));
  6244. ATRACE_END();
  6245. sleepUs = 0;
  6246. continue;
  6247. }
  6248. // if no active track(s), then standby and release wakelock
  6249. size_t size = mActiveTracks.size();
  6250. if (size == 0) {
  6251. standbyIfNotAlreadyInStandby();
  6252. // exitPending() can't become true here
  6253. releaseWakeLock_l();
  6254. ALOGV("RecordThread: loop stopping");
  6255. // go to sleep
  6256. mWaitWorkCV.wait(mLock);
  6257. ALOGV("RecordThread: loop starting");
  6258. goto reacquire_wakelock;
  6259. }
  6260. bool doBroadcast = false;
  6261. bool allStopped = true;
  6262. for (size_t i = 0; i < size; ) {
  6263. activeTrack = mActiveTracks[i];
  6264. if (activeTrack->isTerminated()) {
  6265. if (activeTrack->isFastTrack()) {
  6266. ALOG_ASSERT(fastTrackToRemove == 0);
  6267. fastTrackToRemove = activeTrack;
  6268. }
  6269. removeTrack_l(activeTrack);
  6270. mActiveTracks.remove(activeTrack);
  6271. size--;
  6272. continue;
  6273. }
  6274. TrackBase::track_state activeTrackState = activeTrack->mState;
  6275. switch (activeTrackState) {
  6276. case TrackBase::PAUSING:
  6277. mActiveTracks.remove(activeTrack);
  6278. activeTrack->mState = TrackBase::PAUSED;
  6279. doBroadcast = true;
  6280. size--;
  6281. continue;
  6282. case TrackBase::STARTING_1:
  6283. sleepUs = 10000;
  6284. i++;
  6285. allStopped = false;
  6286. continue;
  6287. case TrackBase::STARTING_2:
  6288. doBroadcast = true;
  6289. mStandby = false;
  6290. activeTrack->mState = TrackBase::ACTIVE;
  6291. allStopped = false;
  6292. break;
  6293. case TrackBase::ACTIVE:
  6294. allStopped = false;
  6295. break;
  6296. case TrackBase::IDLE: // cannot be on ActiveTracks if idle
  6297. case TrackBase::PAUSED: // cannot be on ActiveTracks if paused
  6298. case TrackBase::STOPPED: // cannot be on ActiveTracks if destroyed/terminated
  6299. default:
  6300. LOG_ALWAYS_FATAL("%s: Unexpected active track state:%d, id:%d, tracks:%zu",
  6301. __func__, activeTrackState, activeTrack->id(), size);
  6302. }
  6303. activeTracks.add(activeTrack);
  6304. i++;
  6305. if (activeTrack->isFastTrack()) {
  6306. ALOG_ASSERT(!mFastTrackAvail);
  6307. ALOG_ASSERT(fastTrack == 0);
  6308. fastTrack = activeTrack;
  6309. }
  6310. }
  6311. mActiveTracks.updatePowerState(this);
  6312. updateMetadata_l();
  6313. if (allStopped) {
  6314. standbyIfNotAlreadyInStandby();
  6315. }
  6316. if (doBroadcast) {
  6317. mStartStopCond.broadcast();
  6318. }
  6319. // sleep if there are no active tracks to process
  6320. if (activeTracks.isEmpty()) {
  6321. if (sleepUs == 0) {
  6322. sleepUs = kRecordThreadSleepUs;
  6323. }
  6324. continue;
  6325. }
  6326. sleepUs = 0;
  6327. lockEffectChains_l(effectChains);
  6328. }
  6329. // thread mutex is now unlocked, mActiveTracks unknown, activeTracks.size() > 0
  6330. size_t size = effectChains.size();
  6331. for (size_t i = 0; i < size; i++) {
  6332. // thread mutex is not locked, but effect chain is locked
  6333. effectChains[i]->process_l();
  6334. }
  6335. // Push a new fast capture state if fast capture is not already running, or cblk change
  6336. if (mFastCapture != 0) {
  6337. FastCaptureStateQueue *sq = mFastCapture->sq();
  6338. FastCaptureState *state = sq->begin();
  6339. bool didModify = false;
  6340. FastCaptureStateQueue::block_t block = FastCaptureStateQueue::BLOCK_UNTIL_PUSHED;
  6341. if (state->mCommand != FastCaptureState::READ_WRITE /* FIXME &&
  6342. (kUseFastMixer != FastMixer_Dynamic || state->mTrackMask > 1)*/) {
  6343. if (state->mCommand == FastCaptureState::COLD_IDLE) {
  6344. int32_t old = android_atomic_inc(&mFastCaptureFutex);
  6345. if (old == -1) {
  6346. (void) syscall(__NR_futex, &mFastCaptureFutex, FUTEX_WAKE_PRIVATE, 1);
  6347. }
  6348. }
  6349. state->mCommand = FastCaptureState::READ_WRITE;
  6350. #if 0 // FIXME
  6351. mFastCaptureDumpState.increaseSamplingN(mAudioFlinger->isLowRamDevice() ?
  6352. FastThreadDumpState::kSamplingNforLowRamDevice :
  6353. FastThreadDumpState::kSamplingN);
  6354. #endif
  6355. didModify = true;
  6356. }
  6357. audio_track_cblk_t *cblkOld = state->mCblk;
  6358. audio_track_cblk_t *cblkNew = fastTrack != 0 ? fastTrack->cblk() : NULL;
  6359. if (cblkNew != cblkOld) {
  6360. state->mCblk = cblkNew;
  6361. // block until acked if removing a fast track
  6362. if (cblkOld != NULL) {
  6363. block = FastCaptureStateQueue::BLOCK_UNTIL_ACKED;
  6364. }
  6365. didModify = true;
  6366. }
  6367. AudioBufferProvider* abp = (fastTrack != 0 && fastTrack->isPatchTrack()) ?
  6368. reinterpret_cast<AudioBufferProvider*>(fastTrack.get()) : nullptr;
  6369. if (state->mFastPatchRecordBufferProvider != abp) {
  6370. state->mFastPatchRecordBufferProvider = abp;
  6371. state->mFastPatchRecordFormat = fastTrack == 0 ?
  6372. AUDIO_FORMAT_INVALID : fastTrack->format();
  6373. didModify = true;
  6374. }
  6375. sq->end(didModify);
  6376. if (didModify) {
  6377. sq->push(block);
  6378. #if 0
  6379. if (kUseFastCapture == FastCapture_Dynamic) {
  6380. mNormalSource = mPipeSource;
  6381. }
  6382. #endif
  6383. }
  6384. }
  6385. // now run the fast track destructor with thread mutex unlocked
  6386. fastTrackToRemove.clear();
  6387. // Read from HAL to keep up with fastest client if multiple active tracks, not slowest one.
  6388. // Only the client(s) that are too slow will overrun. But if even the fastest client is too
  6389. // slow, then this RecordThread will overrun by not calling HAL read often enough.
  6390. // If destination is non-contiguous, first read past the nominal end of buffer, then
  6391. // copy to the right place. Permitted because mRsmpInBuffer was over-allocated.
  6392. int32_t rear = mRsmpInRear & (mRsmpInFramesP2 - 1);
  6393. ssize_t framesRead;
  6394. const int64_t lastIoBeginNs = systemTime(); // start IO timing
  6395. // If an NBAIO source is present, use it to read the normal capture's data
  6396. if (mPipeSource != 0) {
  6397. size_t framesToRead = min(mRsmpInFramesOA - rear, mRsmpInFramesP2 / 2);
  6398. // The audio fifo read() returns OVERRUN on overflow, and advances the read pointer
  6399. // to the full buffer point (clearing the overflow condition). Upon OVERRUN error,
  6400. // we immediately retry the read() to get data and prevent another overflow.
  6401. for (int retries = 0; retries <= 2; ++retries) {
  6402. ALOGW_IF(retries > 0, "overrun on read from pipe, retry #%d", retries);
  6403. framesRead = mPipeSource->read((uint8_t*)mRsmpInBuffer + rear * mFrameSize,
  6404. framesToRead);
  6405. if (framesRead != OVERRUN) break;
  6406. }
  6407. const ssize_t availableToRead = mPipeSource->availableToRead();
  6408. if (availableToRead >= 0) {
  6409. // PipeSource is the master clock. It is up to the AudioRecord client to keep up.
  6410. LOG_ALWAYS_FATAL_IF((size_t)availableToRead > mPipeFramesP2,
  6411. "more frames to read than fifo size, %zd > %zu",
  6412. availableToRead, mPipeFramesP2);
  6413. const size_t pipeFramesFree = mPipeFramesP2 - availableToRead;
  6414. const size_t sleepFrames = min(pipeFramesFree, mRsmpInFramesP2) / 2;
  6415. ALOGVV("mPipeFramesP2:%zu mRsmpInFramesP2:%zu sleepFrames:%zu availableToRead:%zd",
  6416. mPipeFramesP2, mRsmpInFramesP2, sleepFrames, availableToRead);
  6417. sleepUs = (sleepFrames * 1000000LL) / mSampleRate;
  6418. }
  6419. if (framesRead < 0) {
  6420. status_t status = (status_t) framesRead;
  6421. switch (status) {
  6422. case OVERRUN:
  6423. ALOGW("overrun on read from pipe");
  6424. framesRead = 0;
  6425. break;
  6426. case NEGOTIATE:
  6427. ALOGE("re-negotiation is needed");
  6428. framesRead = -1; // Will cause an attempt to recover.
  6429. break;
  6430. default:
  6431. ALOGE("unknown error %d on read from pipe", status);
  6432. break;
  6433. }
  6434. }
  6435. // otherwise use the HAL / AudioStreamIn directly
  6436. } else {
  6437. ATRACE_BEGIN("read");
  6438. size_t bytesRead;
  6439. status_t result = mInput->stream->read(
  6440. (uint8_t*)mRsmpInBuffer + rear * mFrameSize, mBufferSize, &bytesRead);
  6441. ATRACE_END();
  6442. if (result < 0) {
  6443. framesRead = result;
  6444. } else {
  6445. framesRead = bytesRead / mFrameSize;
  6446. }
  6447. }
  6448. const int64_t lastIoEndNs = systemTime(); // end IO timing
  6449. // Update server timestamp with server stats
  6450. // systemTime() is optional if the hardware supports timestamps.
  6451. mTimestamp.mPosition[ExtendedTimestamp::LOCATION_SERVER] += framesRead;
  6452. mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_SERVER] = lastIoEndNs;
  6453. // Update server timestamp with kernel stats
  6454. if (mPipeSource.get() == nullptr /* don't obtain for FastCapture, could block */) {
  6455. int64_t position, time;
  6456. if (mStandby) {
  6457. mTimestampVerifier.discontinuity();
  6458. } else if (mInput->stream->getCapturePosition(&position, &time) == NO_ERROR
  6459. && time > mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL]) {
  6460. mTimestampVerifier.add(position, time, mSampleRate);
  6461. // Correct timestamps
  6462. if (isTimestampCorrectionEnabled()) {
  6463. ALOGV("TS_BEFORE: %d %lld %lld",
  6464. id(), (long long)time, (long long)position);
  6465. auto correctedTimestamp = mTimestampVerifier.getLastCorrectedTimestamp();
  6466. position = correctedTimestamp.mFrames;
  6467. time = correctedTimestamp.mTimeNs;
  6468. ALOGV("TS_AFTER: %d %lld %lld",
  6469. id(), (long long)time, (long long)position);
  6470. }
  6471. mTimestamp.mPosition[ExtendedTimestamp::LOCATION_KERNEL] = position;
  6472. mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_KERNEL] = time;
  6473. // Note: In general record buffers should tend to be empty in
  6474. // a properly running pipeline.
  6475. //
  6476. // Also, it is not advantageous to call get_presentation_position during the read
  6477. // as the read obtains a lock, preventing the timestamp call from executing.
  6478. } else {
  6479. mTimestampVerifier.error();
  6480. }
  6481. }
  6482. // From the timestamp, input read latency is negative output write latency.
  6483. const audio_input_flags_t flags = mInput != NULL ? mInput->flags : AUDIO_INPUT_FLAG_NONE;
  6484. const double latencyMs = RecordTrack::checkServerLatencySupported(mFormat, flags)
  6485. ? - mTimestamp.getOutputServerLatencyMs(mSampleRate) : 0.;
  6486. if (latencyMs != 0.) { // note 0. means timestamp is empty.
  6487. mLatencyMs.add(latencyMs);
  6488. }
  6489. // Use this to track timestamp information
  6490. // ALOGD("%s", mTimestamp.toString().c_str());
  6491. if (framesRead < 0 || (framesRead == 0 && mPipeSource == 0)) {
  6492. ALOGE("read failed: framesRead=%zd", framesRead);
  6493. // Force input into standby so that it tries to recover at next read attempt
  6494. inputStandBy();
  6495. sleepUs = kRecordThreadSleepUs;
  6496. }
  6497. if (framesRead <= 0) {
  6498. goto unlock;
  6499. }
  6500. ALOG_ASSERT(framesRead > 0);
  6501. mFramesRead += framesRead;
  6502. #ifdef TEE_SINK
  6503. (void)mTee.write((uint8_t*)mRsmpInBuffer + rear * mFrameSize, framesRead);
  6504. #endif
  6505. // If destination is non-contiguous, we now correct for reading past end of buffer.
  6506. {
  6507. size_t part1 = mRsmpInFramesP2 - rear;
  6508. if ((size_t) framesRead > part1) {
  6509. memcpy(mRsmpInBuffer, (uint8_t*)mRsmpInBuffer + mRsmpInFramesP2 * mFrameSize,
  6510. (framesRead - part1) * mFrameSize);
  6511. }
  6512. }
  6513. rear = mRsmpInRear += framesRead;
  6514. size = activeTracks.size();
  6515. // loop over each active track
  6516. for (size_t i = 0; i < size; i++) {
  6517. activeTrack = activeTracks[i];
  6518. // skip fast tracks, as those are handled directly by FastCapture
  6519. if (activeTrack->isFastTrack()) {
  6520. continue;
  6521. }
  6522. // TODO: This code probably should be moved to RecordTrack.
  6523. // TODO: Update the activeTrack buffer converter in case of reconfigure.
  6524. enum {
  6525. OVERRUN_UNKNOWN,
  6526. OVERRUN_TRUE,
  6527. OVERRUN_FALSE
  6528. } overrun = OVERRUN_UNKNOWN;
  6529. // loop over getNextBuffer to handle circular sink
  6530. for (;;) {
  6531. activeTrack->mSink.frameCount = ~0;
  6532. status_t status = activeTrack->getNextBuffer(&activeTrack->mSink);
  6533. size_t framesOut = activeTrack->mSink.frameCount;
  6534. LOG_ALWAYS_FATAL_IF((status == OK) != (framesOut > 0));
  6535. // check available frames and handle overrun conditions
  6536. // if the record track isn't draining fast enough.
  6537. bool hasOverrun;
  6538. size_t framesIn;
  6539. activeTrack->mResamplerBufferProvider->sync(&framesIn, &hasOverrun);
  6540. if (hasOverrun) {
  6541. overrun = OVERRUN_TRUE;
  6542. }
  6543. if (framesOut == 0 || framesIn == 0) {
  6544. break;
  6545. }
  6546. // Don't allow framesOut to be larger than what is possible with resampling
  6547. // from framesIn.
  6548. // This isn't strictly necessary but helps limit buffer resizing in
  6549. // RecordBufferConverter. TODO: remove when no longer needed.
  6550. framesOut = min(framesOut,
  6551. destinationFramesPossible(
  6552. framesIn, mSampleRate, activeTrack->mSampleRate));
  6553. if (activeTrack->isDirect()) {
  6554. // No RecordBufferConverter used for direct streams. Pass
  6555. // straight from RecordThread buffer to RecordTrack buffer.
  6556. AudioBufferProvider::Buffer buffer;
  6557. buffer.frameCount = framesOut;
  6558. status_t status = activeTrack->mResamplerBufferProvider->getNextBuffer(&buffer);
  6559. if (status == OK && buffer.frameCount != 0) {
  6560. ALOGV_IF(buffer.frameCount != framesOut,
  6561. "%s() read less than expected (%zu vs %zu)",
  6562. __func__, buffer.frameCount, framesOut);
  6563. framesOut = buffer.frameCount;
  6564. memcpy(activeTrack->mSink.raw, buffer.raw, buffer.frameCount * mFrameSize);
  6565. activeTrack->mResamplerBufferProvider->releaseBuffer(&buffer);
  6566. } else {
  6567. framesOut = 0;
  6568. ALOGE("%s() cannot fill request, status: %d, frameCount: %zu",
  6569. __func__, status, buffer.frameCount);
  6570. }
  6571. } else {
  6572. // process frames from the RecordThread buffer provider to the RecordTrack
  6573. // buffer
  6574. framesOut = activeTrack->mRecordBufferConverter->convert(
  6575. activeTrack->mSink.raw,
  6576. activeTrack->mResamplerBufferProvider,
  6577. framesOut);
  6578. }
  6579. if (framesOut > 0 && (overrun == OVERRUN_UNKNOWN)) {
  6580. overrun = OVERRUN_FALSE;
  6581. }
  6582. if (activeTrack->mFramesToDrop == 0) {
  6583. if (framesOut > 0) {
  6584. activeTrack->mSink.frameCount = framesOut;
  6585. // Sanitize before releasing if the track has no access to the source data
  6586. // An idle UID receives silence from non virtual devices until active
  6587. if (activeTrack->isSilenced()) {
  6588. memset(activeTrack->mSink.raw, 0, framesOut * activeTrack->frameSize());
  6589. }
  6590. activeTrack->releaseBuffer(&activeTrack->mSink);
  6591. }
  6592. } else {
  6593. // FIXME could do a partial drop of framesOut
  6594. if (activeTrack->mFramesToDrop > 0) {
  6595. activeTrack->mFramesToDrop -= (ssize_t)framesOut;
  6596. if (activeTrack->mFramesToDrop <= 0) {
  6597. activeTrack->clearSyncStartEvent();
  6598. }
  6599. } else {
  6600. activeTrack->mFramesToDrop += framesOut;
  6601. if (activeTrack->mFramesToDrop >= 0 || activeTrack->mSyncStartEvent == 0 ||
  6602. activeTrack->mSyncStartEvent->isCancelled()) {
  6603. ALOGW("Synced record %s, session %d, trigger session %d",
  6604. (activeTrack->mFramesToDrop >= 0) ? "timed out" : "cancelled",
  6605. activeTrack->sessionId(),
  6606. (activeTrack->mSyncStartEvent != 0) ?
  6607. activeTrack->mSyncStartEvent->triggerSession() :
  6608. AUDIO_SESSION_NONE);
  6609. activeTrack->clearSyncStartEvent();
  6610. }
  6611. }
  6612. }
  6613. if (framesOut == 0) {
  6614. break;
  6615. }
  6616. }
  6617. switch (overrun) {
  6618. case OVERRUN_TRUE:
  6619. // client isn't retrieving buffers fast enough
  6620. if (!activeTrack->setOverflow()) {
  6621. nsecs_t now = systemTime();
  6622. // FIXME should lastWarning per track?
  6623. if ((now - lastWarning) > kWarningThrottleNs) {
  6624. ALOGW("RecordThread: buffer overflow");
  6625. lastWarning = now;
  6626. }
  6627. }
  6628. break;
  6629. case OVERRUN_FALSE:
  6630. activeTrack->clearOverflow();
  6631. break;
  6632. case OVERRUN_UNKNOWN:
  6633. break;
  6634. }
  6635. // update frame information and push timestamp out
  6636. activeTrack->updateTrackFrameInfo(
  6637. activeTrack->mServerProxy->framesReleased(),
  6638. mTimestamp.mPosition[ExtendedTimestamp::LOCATION_SERVER],
  6639. mSampleRate, mTimestamp);
  6640. }
  6641. unlock:
  6642. // enable changes in effect chain
  6643. unlockEffectChains(effectChains);
  6644. // effectChains doesn't need to be cleared, since it is cleared by destructor at scope end
  6645. if (audio_has_proportional_frames(mFormat)
  6646. && loopCount == lastLoopCountRead + 1) {
  6647. const int64_t readPeriodNs = lastIoEndNs - mLastIoEndNs;
  6648. const double jitterMs =
  6649. TimestampVerifier<int64_t, int64_t>::computeJitterMs(
  6650. {framesRead, readPeriodNs},
  6651. {0, 0} /* lastTimestamp */, mSampleRate);
  6652. const double processMs = (lastIoBeginNs - mLastIoEndNs) * 1e-6;
  6653. Mutex::Autolock _l(mLock);
  6654. mIoJitterMs.add(jitterMs);
  6655. mProcessTimeMs.add(processMs);
  6656. }
  6657. // update timing info.
  6658. mLastIoBeginNs = lastIoBeginNs;
  6659. mLastIoEndNs = lastIoEndNs;
  6660. lastLoopCountRead = loopCount;
  6661. }
  6662. standbyIfNotAlreadyInStandby();
  6663. {
  6664. Mutex::Autolock _l(mLock);
  6665. for (size_t i = 0; i < mTracks.size(); i++) {
  6666. sp<RecordTrack> track = mTracks[i];
  6667. track->invalidate();
  6668. }
  6669. mActiveTracks.clear();
  6670. mStartStopCond.broadcast();
  6671. }
  6672. releaseWakeLock();
  6673. ALOGV("RecordThread %p exiting", this);
  6674. return false;
  6675. }
  6676. void AudioFlinger::RecordThread::standbyIfNotAlreadyInStandby()
  6677. {
  6678. if (!mStandby) {
  6679. inputStandBy();
  6680. mStandby = true;
  6681. }
  6682. }
  6683. void AudioFlinger::RecordThread::inputStandBy()
  6684. {
  6685. // Idle the fast capture if it's currently running
  6686. if (mFastCapture != 0) {
  6687. FastCaptureStateQueue *sq = mFastCapture->sq();
  6688. FastCaptureState *state = sq->begin();
  6689. if (!(state->mCommand & FastCaptureState::IDLE)) {
  6690. state->mCommand = FastCaptureState::COLD_IDLE;
  6691. state->mColdFutexAddr = &mFastCaptureFutex;
  6692. state->mColdGen++;
  6693. mFastCaptureFutex = 0;
  6694. sq->end();
  6695. // BLOCK_UNTIL_PUSHED would be insufficient, as we need it to stop doing I/O now
  6696. sq->push(FastCaptureStateQueue::BLOCK_UNTIL_ACKED);
  6697. #if 0
  6698. if (kUseFastCapture == FastCapture_Dynamic) {
  6699. // FIXME
  6700. }
  6701. #endif
  6702. #ifdef AUDIO_WATCHDOG
  6703. // FIXME
  6704. #endif
  6705. } else {
  6706. sq->end(false /*didModify*/);
  6707. }
  6708. }
  6709. status_t result = mInput->stream->standby();
  6710. ALOGE_IF(result != OK, "Error when putting input stream into standby: %d", result);
  6711. // If going into standby, flush the pipe source.
  6712. if (mPipeSource.get() != nullptr) {
  6713. const ssize_t flushed = mPipeSource->flush();
  6714. if (flushed > 0) {
  6715. ALOGV("Input standby flushed PipeSource %zd frames", flushed);
  6716. mTimestamp.mPosition[ExtendedTimestamp::LOCATION_SERVER] += flushed;
  6717. mTimestamp.mTimeNs[ExtendedTimestamp::LOCATION_SERVER] = systemTime();
  6718. }
  6719. }
  6720. }
  6721. // RecordThread::createRecordTrack_l() must be called with AudioFlinger::mLock held
  6722. sp<AudioFlinger::RecordThread::RecordTrack> AudioFlinger::RecordThread::createRecordTrack_l(
  6723. const sp<AudioFlinger::Client>& client,
  6724. const audio_attributes_t& attr,
  6725. uint32_t *pSampleRate,
  6726. audio_format_t format,
  6727. audio_channel_mask_t channelMask,
  6728. size_t *pFrameCount,
  6729. audio_session_t sessionId,
  6730. size_t *pNotificationFrameCount,
  6731. pid_t creatorPid,
  6732. uid_t uid,
  6733. audio_input_flags_t *flags,
  6734. pid_t tid,
  6735. status_t *status,
  6736. audio_port_handle_t portId,
  6737. const String16& opPackageName)
  6738. {
  6739. size_t frameCount = *pFrameCount;
  6740. size_t notificationFrameCount = *pNotificationFrameCount;
  6741. sp<RecordTrack> track;
  6742. status_t lStatus;
  6743. audio_input_flags_t inputFlags = mInput->flags;
  6744. audio_input_flags_t requestedFlags = *flags;
  6745. uint32_t sampleRate;
  6746. lStatus = initCheck();
  6747. if (lStatus != NO_ERROR) {
  6748. ALOGE("createRecordTrack_l() audio driver not initialized");
  6749. goto Exit;
  6750. }
  6751. if (!audio_is_linear_pcm(mFormat) && (*flags & AUDIO_INPUT_FLAG_DIRECT) == 0) {
  6752. ALOGE("createRecordTrack_l() on an encoded stream requires AUDIO_INPUT_FLAG_DIRECT");
  6753. lStatus = BAD_VALUE;
  6754. goto Exit;
  6755. }
  6756. if (*pSampleRate == 0) {
  6757. *pSampleRate = mSampleRate;
  6758. }
  6759. sampleRate = *pSampleRate;
  6760. // special case for FAST flag considered OK if fast capture is present
  6761. if (hasFastCapture()) {
  6762. inputFlags = (audio_input_flags_t)(inputFlags | AUDIO_INPUT_FLAG_FAST);
  6763. }
  6764. // Check if requested flags are compatible with input stream flags
  6765. if ((*flags & inputFlags) != *flags) {
  6766. ALOGW("createRecordTrack_l(): mismatch between requested flags (%08x) and"
  6767. " input flags (%08x)",
  6768. *flags, inputFlags);
  6769. *flags = (audio_input_flags_t)(*flags & inputFlags);
  6770. }
  6771. // client expresses a preference for FAST, but we get the final say
  6772. if (*flags & AUDIO_INPUT_FLAG_FAST) {
  6773. if (
  6774. // we formerly checked for a callback handler (non-0 tid),
  6775. // but that is no longer required for TRANSFER_OBTAIN mode
  6776. //
  6777. // Frame count is not specified (0), or is less than or equal the pipe depth.
  6778. // It is OK to provide a higher capacity than requested.
  6779. // We will force it to mPipeFramesP2 below.
  6780. (frameCount <= mPipeFramesP2) &&
  6781. // PCM data
  6782. audio_is_linear_pcm(format) &&
  6783. // hardware format
  6784. (format == mFormat) &&
  6785. // hardware channel mask
  6786. (channelMask == mChannelMask) &&
  6787. // hardware sample rate
  6788. (sampleRate == mSampleRate) &&
  6789. // record thread has an associated fast capture
  6790. hasFastCapture() &&
  6791. // there are sufficient fast track slots available
  6792. mFastTrackAvail
  6793. ) {
  6794. // check compatibility with audio effects.
  6795. Mutex::Autolock _l(mLock);
  6796. // Do not accept FAST flag if the session has software effects
  6797. sp<EffectChain> chain = getEffectChain_l(sessionId);
  6798. if (chain != 0) {
  6799. audio_input_flags_t old = *flags;
  6800. chain->checkInputFlagCompatibility(flags);
  6801. if (old != *flags) {
  6802. ALOGV("%p AUDIO_INPUT_FLAGS denied by effect old=%#x new=%#x",
  6803. this, (int)old, (int)*flags);
  6804. }
  6805. }
  6806. ALOGV_IF((*flags & AUDIO_INPUT_FLAG_FAST) != 0,
  6807. "%p AUDIO_INPUT_FLAG_FAST accepted: frameCount=%zu mFrameCount=%zu",
  6808. this, frameCount, mFrameCount);
  6809. } else {
  6810. ALOGV("%p AUDIO_INPUT_FLAG_FAST denied: frameCount=%zu mFrameCount=%zu mPipeFramesP2=%zu "
  6811. "format=%#x isLinear=%d mFormat=%#x channelMask=%#x sampleRate=%u mSampleRate=%u "
  6812. "hasFastCapture=%d tid=%d mFastTrackAvail=%d",
  6813. this, frameCount, mFrameCount, mPipeFramesP2,
  6814. format, audio_is_linear_pcm(format), mFormat, channelMask, sampleRate, mSampleRate,
  6815. hasFastCapture(), tid, mFastTrackAvail);
  6816. *flags = (audio_input_flags_t)(*flags & ~AUDIO_INPUT_FLAG_FAST);
  6817. }
  6818. }
  6819. // If FAST or RAW flags were corrected, ask caller to request new input from audio policy
  6820. if ((*flags & AUDIO_INPUT_FLAG_FAST) !=
  6821. (requestedFlags & AUDIO_INPUT_FLAG_FAST)) {
  6822. *flags = (audio_input_flags_t) (*flags & ~(AUDIO_INPUT_FLAG_FAST | AUDIO_INPUT_FLAG_RAW));
  6823. lStatus = BAD_TYPE;
  6824. goto Exit;
  6825. }
  6826. // compute track buffer size in frames, and suggest the notification frame count
  6827. if (*flags & AUDIO_INPUT_FLAG_FAST) {
  6828. // fast track: frame count is exactly the pipe depth
  6829. frameCount = mPipeFramesP2;
  6830. // ignore requested notificationFrames, and always notify exactly once every HAL buffer
  6831. notificationFrameCount = mFrameCount;
  6832. } else {
  6833. // not fast track: max notification period is resampled equivalent of one HAL buffer time
  6834. // or 20 ms if there is a fast capture
  6835. // TODO This could be a roundupRatio inline, and const
  6836. size_t maxNotificationFrames = ((int64_t) (hasFastCapture() ? mSampleRate/50 : mFrameCount)
  6837. * sampleRate + mSampleRate - 1) / mSampleRate;
  6838. // minimum number of notification periods is at least kMinNotifications,
  6839. // and at least kMinMs rounded up to a whole notification period (minNotificationsByMs)
  6840. static const size_t kMinNotifications = 3;
  6841. static const uint32_t kMinMs = 30;
  6842. // TODO This could be a roundupRatio inline
  6843. const size_t minFramesByMs = (sampleRate * kMinMs + 1000 - 1) / 1000;
  6844. // TODO This could be a roundupRatio inline
  6845. const size_t minNotificationsByMs = (minFramesByMs + maxNotificationFrames - 1) /
  6846. maxNotificationFrames;
  6847. const size_t minFrameCount = maxNotificationFrames *
  6848. max(kMinNotifications, minNotificationsByMs);
  6849. frameCount = max(frameCount, minFrameCount);
  6850. if (notificationFrameCount == 0 || notificationFrameCount > maxNotificationFrames) {
  6851. notificationFrameCount = maxNotificationFrames;
  6852. }
  6853. }
  6854. *pFrameCount = frameCount;
  6855. *pNotificationFrameCount = notificationFrameCount;
  6856. { // scope for mLock
  6857. Mutex::Autolock _l(mLock);
  6858. track = new RecordTrack(this, client, attr, sampleRate,
  6859. format, channelMask, frameCount,
  6860. nullptr /* buffer */, (size_t)0 /* bufferSize */, sessionId, creatorPid, uid,
  6861. *flags, TrackBase::TYPE_DEFAULT, opPackageName, portId);
  6862. lStatus = track->initCheck();
  6863. if (lStatus != NO_ERROR) {
  6864. ALOGE("createRecordTrack_l() initCheck failed %d; no control block?", lStatus);
  6865. // track must be cleared from the caller as the caller has the AF lock
  6866. goto Exit;
  6867. }
  6868. mTracks.add(track);
  6869. if ((*flags & AUDIO_INPUT_FLAG_FAST) && (tid != -1)) {
  6870. pid_t callingPid = IPCThreadState::self()->getCallingPid();
  6871. // we don't have CAP_SYS_NICE, nor do we want to have it as it's too powerful,
  6872. // so ask activity manager to do this on our behalf
  6873. sendPrioConfigEvent_l(callingPid, tid, kPriorityAudioApp, true /*forApp*/);
  6874. }
  6875. }
  6876. lStatus = NO_ERROR;
  6877. Exit:
  6878. *status = lStatus;
  6879. return track;
  6880. }
  6881. status_t AudioFlinger::RecordThread::start(RecordThread::RecordTrack* recordTrack,
  6882. AudioSystem::sync_event_t event,
  6883. audio_session_t triggerSession)
  6884. {
  6885. ALOGV("RecordThread::start event %d, triggerSession %d", event, triggerSession);
  6886. sp<ThreadBase> strongMe = this;
  6887. status_t status = NO_ERROR;
  6888. if (event == AudioSystem::SYNC_EVENT_NONE) {
  6889. recordTrack->clearSyncStartEvent();
  6890. } else if (event != AudioSystem::SYNC_EVENT_SAME) {
  6891. recordTrack->mSyncStartEvent = mAudioFlinger->createSyncEvent(event,
  6892. triggerSession,
  6893. recordTrack->sessionId(),
  6894. syncStartEventCallback,
  6895. recordTrack);
  6896. // Sync event can be cancelled by the trigger session if the track is not in a
  6897. // compatible state in which case we start record immediately
  6898. if (recordTrack->mSyncStartEvent->isCancelled()) {
  6899. recordTrack->clearSyncStartEvent();
  6900. } else {
  6901. // do not wait for the event for more than AudioSystem::kSyncRecordStartTimeOutMs
  6902. recordTrack->mFramesToDrop = -(ssize_t)
  6903. ((AudioSystem::kSyncRecordStartTimeOutMs * recordTrack->mSampleRate) / 1000);
  6904. }
  6905. }
  6906. {
  6907. // This section is a rendezvous between binder thread executing start() and RecordThread
  6908. AutoMutex lock(mLock);
  6909. if (recordTrack->isInvalid()) {
  6910. recordTrack->clearSyncStartEvent();
  6911. return INVALID_OPERATION;
  6912. }
  6913. if (mActiveTracks.indexOf(recordTrack) >= 0) {
  6914. if (recordTrack->mState == TrackBase::PAUSING) {
  6915. // We haven't stopped yet (moved to PAUSED and not in mActiveTracks)
  6916. // so no need to startInput().
  6917. ALOGV("active record track PAUSING -> ACTIVE");
  6918. recordTrack->mState = TrackBase::ACTIVE;
  6919. } else {
  6920. ALOGV("active record track state %d", recordTrack->mState);
  6921. }
  6922. return status;
  6923. }
  6924. // TODO consider other ways of handling this, such as changing the state to :STARTING and
  6925. // adding the track to mActiveTracks after returning from AudioSystem::startInput(),
  6926. // or using a separate command thread
  6927. recordTrack->mState = TrackBase::STARTING_1;
  6928. mActiveTracks.add(recordTrack);
  6929. status_t status = NO_ERROR;
  6930. if (recordTrack->isExternalTrack()) {
  6931. mLock.unlock();
  6932. status = AudioSystem::startInput(recordTrack->portId());
  6933. mLock.lock();
  6934. if (recordTrack->isInvalid()) {
  6935. recordTrack->clearSyncStartEvent();
  6936. if (status == NO_ERROR && recordTrack->mState == TrackBase::STARTING_1) {
  6937. recordTrack->mState = TrackBase::STARTING_2;
  6938. // STARTING_2 forces destroy to call stopInput.
  6939. }
  6940. return INVALID_OPERATION;
  6941. }
  6942. if (recordTrack->mState != TrackBase::STARTING_1) {
  6943. ALOGW("%s(%d): unsynchronized mState:%d change",
  6944. __func__, recordTrack->id(), recordTrack->mState);
  6945. // Someone else has changed state, let them take over,
  6946. // leave mState in the new state.
  6947. recordTrack->clearSyncStartEvent();
  6948. return INVALID_OPERATION;
  6949. }
  6950. // we're ok, but perhaps startInput has failed
  6951. if (status != NO_ERROR) {
  6952. ALOGW("%s(%d): startInput failed, status %d",
  6953. __func__, recordTrack->id(), status);
  6954. // We are in ActiveTracks if STARTING_1 and valid, so remove from ActiveTracks,
  6955. // leave in STARTING_1, so destroy() will not call stopInput.
  6956. mActiveTracks.remove(recordTrack);
  6957. recordTrack->clearSyncStartEvent();
  6958. return status;
  6959. }
  6960. sendIoConfigEvent_l(
  6961. AUDIO_CLIENT_STARTED, recordTrack->creatorPid(), recordTrack->portId());
  6962. }
  6963. // Catch up with current buffer indices if thread is already running.
  6964. // This is what makes a new client discard all buffered data. If the track's mRsmpInFront
  6965. // was initialized to some value closer to the thread's mRsmpInFront, then the track could
  6966. // see previously buffered data before it called start(), but with greater risk of overrun.
  6967. recordTrack->mResamplerBufferProvider->reset();
  6968. if (!recordTrack->isDirect()) {
  6969. // clear any converter state as new data will be discontinuous
  6970. recordTrack->mRecordBufferConverter->reset();
  6971. }
  6972. recordTrack->mState = TrackBase::STARTING_2;
  6973. // signal thread to start
  6974. mWaitWorkCV.broadcast();
  6975. return status;
  6976. }
  6977. }
  6978. void AudioFlinger::RecordThread::syncStartEventCallback(const wp<SyncEvent>& event)
  6979. {
  6980. sp<SyncEvent> strongEvent = event.promote();
  6981. if (strongEvent != 0) {
  6982. sp<RefBase> ptr = strongEvent->cookie().promote();
  6983. if (ptr != 0) {
  6984. RecordTrack *recordTrack = (RecordTrack *)ptr.get();
  6985. recordTrack->handleSyncStartEvent(strongEvent);
  6986. }
  6987. }
  6988. }
  6989. bool AudioFlinger::RecordThread::stop(RecordThread::RecordTrack* recordTrack) {
  6990. ALOGV("RecordThread::stop");
  6991. AutoMutex _l(mLock);
  6992. // if we're invalid, we can't be on the ActiveTracks.
  6993. if (mActiveTracks.indexOf(recordTrack) < 0 || recordTrack->mState == TrackBase::PAUSING) {
  6994. return false;
  6995. }
  6996. // note that threadLoop may still be processing the track at this point [without lock]
  6997. recordTrack->mState = TrackBase::PAUSING;
  6998. // NOTE: Waiting here is important to keep stop synchronous.
  6999. // This is needed for proper patchRecord peer release.
  7000. while (recordTrack->mState == TrackBase::PAUSING && !recordTrack->isInvalid()) {
  7001. mWaitWorkCV.broadcast(); // signal thread to stop
  7002. mStartStopCond.wait(mLock);
  7003. }
  7004. if (recordTrack->mState == TrackBase::PAUSED) { // successful stop
  7005. ALOGV("Record stopped OK");
  7006. return true;
  7007. }
  7008. // don't handle anything - we've been invalidated or restarted and in a different state
  7009. ALOGW_IF("%s(%d): unsynchronized stop, state: %d",
  7010. __func__, recordTrack->id(), recordTrack->mState);
  7011. return false;
  7012. }
  7013. bool AudioFlinger::RecordThread::isValidSyncEvent(const sp<SyncEvent>& event __unused) const
  7014. {
  7015. return false;
  7016. }
  7017. status_t AudioFlinger::RecordThread::setSyncEvent(const sp<SyncEvent>& event __unused)
  7018. {
  7019. #if 0 // This branch is currently dead code, but is preserved in case it will be needed in future
  7020. if (!isValidSyncEvent(event)) {
  7021. return BAD_VALUE;
  7022. }
  7023. audio_session_t eventSession = event->triggerSession();
  7024. status_t ret = NAME_NOT_FOUND;
  7025. Mutex::Autolock _l(mLock);
  7026. for (size_t i = 0; i < mTracks.size(); i++) {
  7027. sp<RecordTrack> track = mTracks[i];
  7028. if (eventSession == track->sessionId()) {
  7029. (void) track->setSyncEvent(event);
  7030. ret = NO_ERROR;
  7031. }
  7032. }
  7033. return ret;
  7034. #else
  7035. return BAD_VALUE;
  7036. #endif
  7037. }
  7038. status_t AudioFlinger::RecordThread::getActiveMicrophones(
  7039. std::vector<media::MicrophoneInfo>* activeMicrophones)
  7040. {
  7041. ALOGV("RecordThread::getActiveMicrophones");
  7042. AutoMutex _l(mLock);
  7043. status_t status = mInput->stream->getActiveMicrophones(activeMicrophones);
  7044. return status;
  7045. }
  7046. status_t AudioFlinger::RecordThread::setPreferredMicrophoneDirection(
  7047. audio_microphone_direction_t direction)
  7048. {
  7049. ALOGV("setPreferredMicrophoneDirection(%d)", direction);
  7050. AutoMutex _l(mLock);
  7051. return mInput->stream->setPreferredMicrophoneDirection(direction);
  7052. }
  7053. status_t AudioFlinger::RecordThread::setPreferredMicrophoneFieldDimension(float zoom)
  7054. {
  7055. ALOGV("setPreferredMicrophoneFieldDimension(%f)", zoom);
  7056. AutoMutex _l(mLock);
  7057. return mInput->stream->setPreferredMicrophoneFieldDimension(zoom);
  7058. }
  7059. void AudioFlinger::RecordThread::updateMetadata_l()
  7060. {
  7061. if (mInput == nullptr || mInput->stream == nullptr ||
  7062. !mActiveTracks.readAndClearHasChanged()) {
  7063. return;
  7064. }
  7065. StreamInHalInterface::SinkMetadata metadata;
  7066. for (const sp<RecordTrack> &track : mActiveTracks) {
  7067. // No track is invalid as this is called after prepareTrack_l in the same critical section
  7068. metadata.tracks.push_back({
  7069. .source = track->attributes().source,
  7070. .gain = 1, // capture tracks do not have volumes
  7071. });
  7072. }
  7073. mInput->stream->updateSinkMetadata(metadata);
  7074. }
  7075. // destroyTrack_l() must be called with ThreadBase::mLock held
  7076. void AudioFlinger::RecordThread::destroyTrack_l(const sp<RecordTrack>& track)
  7077. {
  7078. track->terminate();
  7079. track->mState = TrackBase::STOPPED;
  7080. // active tracks are removed by threadLoop()
  7081. if (mActiveTracks.indexOf(track) < 0) {
  7082. removeTrack_l(track);
  7083. }
  7084. }
  7085. void AudioFlinger::RecordThread::removeTrack_l(const sp<RecordTrack>& track)
  7086. {
  7087. String8 result;
  7088. track->appendDump(result, false /* active */);
  7089. mLocalLog.log("removeTrack_l (%p) %s", track.get(), result.string());
  7090. mTracks.remove(track);
  7091. // need anything related to effects here?
  7092. if (track->isFastTrack()) {
  7093. ALOG_ASSERT(!mFastTrackAvail);
  7094. mFastTrackAvail = true;
  7095. }
  7096. }
  7097. void AudioFlinger::RecordThread::dumpInternals_l(int fd, const Vector<String16>& args __unused)
  7098. {
  7099. AudioStreamIn *input = mInput;
  7100. audio_input_flags_t flags = input != NULL ? input->flags : AUDIO_INPUT_FLAG_NONE;
  7101. dprintf(fd, " AudioStreamIn: %p flags %#x (%s)\n",
  7102. input, flags, toString(flags).c_str());
  7103. dprintf(fd, " Frames read: %lld\n", (long long)mFramesRead);
  7104. if (mActiveTracks.isEmpty()) {
  7105. dprintf(fd, " No active record clients\n");
  7106. }
  7107. if (input != nullptr) {
  7108. dprintf(fd, " Hal stream dump:\n");
  7109. (void)input->stream->dump(fd);
  7110. }
  7111. dprintf(fd, " Fast capture thread: %s\n", hasFastCapture() ? "yes" : "no");
  7112. dprintf(fd, " Fast track available: %s\n", mFastTrackAvail ? "yes" : "no");
  7113. // Make a non-atomic copy of fast capture dump state so it won't change underneath us
  7114. // while we are dumping it. It may be inconsistent, but it won't mutate!
  7115. // This is a large object so we place it on the heap.
  7116. // FIXME 25972958: Need an intelligent copy constructor that does not touch unused pages.
  7117. const std::unique_ptr<FastCaptureDumpState> copy =
  7118. std::make_unique<FastCaptureDumpState>(mFastCaptureDumpState);
  7119. copy->dump(fd);
  7120. }
  7121. void AudioFlinger::RecordThread::dumpTracks_l(int fd, const Vector<String16>& args __unused)
  7122. {
  7123. String8 result;
  7124. size_t numtracks = mTracks.size();
  7125. size_t numactive = mActiveTracks.size();
  7126. size_t numactiveseen = 0;
  7127. dprintf(fd, " %zu Tracks", numtracks);
  7128. const char *prefix = " ";
  7129. if (numtracks) {
  7130. dprintf(fd, " of which %zu are active\n", numactive);
  7131. result.append(prefix);
  7132. mTracks[0]->appendDumpHeader(result);
  7133. for (size_t i = 0; i < numtracks ; ++i) {
  7134. sp<RecordTrack> track = mTracks[i];
  7135. if (track != 0) {
  7136. bool active = mActiveTracks.indexOf(track) >= 0;
  7137. if (active) {
  7138. numactiveseen++;
  7139. }
  7140. result.append(prefix);
  7141. track->appendDump(result, active);
  7142. }
  7143. }
  7144. } else {
  7145. dprintf(fd, "\n");
  7146. }
  7147. if (numactiveseen != numactive) {
  7148. result.append(" The following tracks are in the active list but"
  7149. " not in the track list\n");
  7150. result.append(prefix);
  7151. mActiveTracks[0]->appendDumpHeader(result);
  7152. for (size_t i = 0; i < numactive; ++i) {
  7153. sp<RecordTrack> track = mActiveTracks[i];
  7154. if (mTracks.indexOf(track) < 0) {
  7155. result.append(prefix);
  7156. track->appendDump(result, true /* active */);
  7157. }
  7158. }
  7159. }
  7160. write(fd, result.string(), result.size());
  7161. }
  7162. void AudioFlinger::RecordThread::setRecordSilenced(uid_t uid, bool silenced)
  7163. {
  7164. Mutex::Autolock _l(mLock);
  7165. for (size_t i = 0; i < mTracks.size() ; i++) {
  7166. sp<RecordTrack> track = mTracks[i];
  7167. if (track != 0 && track->uid() == uid) {
  7168. track->setSilenced(silenced);
  7169. }
  7170. }
  7171. }
  7172. void AudioFlinger::RecordThread::ResamplerBufferProvider::reset()
  7173. {
  7174. sp<ThreadBase> threadBase = mRecordTrack->mThread.promote();
  7175. RecordThread *recordThread = (RecordThread *) threadBase.get();
  7176. mRsmpInFront = recordThread->mRsmpInRear;
  7177. mRsmpInUnrel = 0;
  7178. }
  7179. void AudioFlinger::RecordThread::ResamplerBufferProvider::sync(
  7180. size_t *framesAvailable, bool *hasOverrun)
  7181. {
  7182. sp<ThreadBase> threadBase = mRecordTrack->mThread.promote();
  7183. RecordThread *recordThread = (RecordThread *) threadBase.get();
  7184. const int32_t rear = recordThread->mRsmpInRear;
  7185. const int32_t front = mRsmpInFront;
  7186. const ssize_t filled = audio_utils::safe_sub_overflow(rear, front);
  7187. size_t framesIn;
  7188. bool overrun = false;
  7189. if (filled < 0) {
  7190. // should not happen, but treat like a massive overrun and re-sync
  7191. framesIn = 0;
  7192. mRsmpInFront = rear;
  7193. overrun = true;
  7194. } else if ((size_t) filled <= recordThread->mRsmpInFrames) {
  7195. framesIn = (size_t) filled;
  7196. } else {
  7197. // client is not keeping up with server, but give it latest data
  7198. framesIn = recordThread->mRsmpInFrames;
  7199. mRsmpInFront = /* front = */ audio_utils::safe_sub_overflow(
  7200. rear, static_cast<int32_t>(framesIn));
  7201. overrun = true;
  7202. }
  7203. if (framesAvailable != NULL) {
  7204. *framesAvailable = framesIn;
  7205. }
  7206. if (hasOverrun != NULL) {
  7207. *hasOverrun = overrun;
  7208. }
  7209. }
  7210. // AudioBufferProvider interface
  7211. status_t AudioFlinger::RecordThread::ResamplerBufferProvider::getNextBuffer(
  7212. AudioBufferProvider::Buffer* buffer)
  7213. {
  7214. sp<ThreadBase> threadBase = mRecordTrack->mThread.promote();
  7215. if (threadBase == 0) {
  7216. buffer->frameCount = 0;
  7217. buffer->raw = NULL;
  7218. return NOT_ENOUGH_DATA;
  7219. }
  7220. RecordThread *recordThread = (RecordThread *) threadBase.get();
  7221. int32_t rear = recordThread->mRsmpInRear;
  7222. int32_t front = mRsmpInFront;
  7223. ssize_t filled = audio_utils::safe_sub_overflow(rear, front);
  7224. // FIXME should not be P2 (don't want to increase latency)
  7225. // FIXME if client not keeping up, discard
  7226. LOG_ALWAYS_FATAL_IF(!(0 <= filled && (size_t) filled <= recordThread->mRsmpInFrames));
  7227. // 'filled' may be non-contiguous, so return only the first contiguous chunk
  7228. front &= recordThread->mRsmpInFramesP2 - 1;
  7229. size_t part1 = recordThread->mRsmpInFramesP2 - front;
  7230. if (part1 > (size_t) filled) {
  7231. part1 = filled;
  7232. }
  7233. size_t ask = buffer->frameCount;
  7234. ALOG_ASSERT(ask > 0);
  7235. if (part1 > ask) {
  7236. part1 = ask;
  7237. }
  7238. if (part1 == 0) {
  7239. // out of data is fine since the resampler will return a short-count.
  7240. buffer->raw = NULL;
  7241. buffer->frameCount = 0;
  7242. mRsmpInUnrel = 0;
  7243. return NOT_ENOUGH_DATA;
  7244. }
  7245. buffer->raw = (uint8_t*)recordThread->mRsmpInBuffer + front * recordThread->mFrameSize;
  7246. buffer->frameCount = part1;
  7247. mRsmpInUnrel = part1;
  7248. return NO_ERROR;
  7249. }
  7250. // AudioBufferProvider interface
  7251. void AudioFlinger::RecordThread::ResamplerBufferProvider::releaseBuffer(
  7252. AudioBufferProvider::Buffer* buffer)
  7253. {
  7254. int32_t stepCount = static_cast<int32_t>(buffer->frameCount);
  7255. if (stepCount == 0) {
  7256. return;
  7257. }
  7258. ALOG_ASSERT(stepCount <= mRsmpInUnrel);
  7259. mRsmpInUnrel -= stepCount;
  7260. mRsmpInFront = audio_utils::safe_add_overflow(mRsmpInFront, stepCount);
  7261. buffer->raw = NULL;
  7262. buffer->frameCount = 0;
  7263. }
  7264. void AudioFlinger::RecordThread::checkBtNrec()
  7265. {
  7266. Mutex::Autolock _l(mLock);
  7267. checkBtNrec_l();
  7268. }
  7269. void AudioFlinger::RecordThread::checkBtNrec_l()
  7270. {
  7271. // disable AEC and NS if the device is a BT SCO headset supporting those
  7272. // pre processings
  7273. bool suspend = audio_is_bluetooth_sco_device(mInDevice) &&
  7274. mAudioFlinger->btNrecIsOff();
  7275. if (mBtNrecSuspended.exchange(suspend) != suspend) {
  7276. for (size_t i = 0; i < mEffectChains.size(); i++) {
  7277. setEffectSuspended_l(FX_IID_AEC, suspend, mEffectChains[i]->sessionId());
  7278. setEffectSuspended_l(FX_IID_NS, suspend, mEffectChains[i]->sessionId());
  7279. }
  7280. }
  7281. }
  7282. bool AudioFlinger::RecordThread::checkForNewParameter_l(const String8& keyValuePair,
  7283. status_t& status)
  7284. {
  7285. bool reconfig = false;
  7286. status = NO_ERROR;
  7287. audio_format_t reqFormat = mFormat;
  7288. uint32_t samplingRate = mSampleRate;
  7289. // TODO this may change if we want to support capture from HDMI PCM multi channel (e.g on TVs).
  7290. audio_channel_mask_t channelMask = audio_channel_in_mask_from_count(mChannelCount);
  7291. AudioParameter param = AudioParameter(keyValuePair);
  7292. int value;
  7293. // scope for AutoPark extends to end of method
  7294. AutoPark<FastCapture> park(mFastCapture);
  7295. // TODO Investigate when this code runs. Check with audio policy when a sample rate and
  7296. // channel count change can be requested. Do we mandate the first client defines the
  7297. // HAL sampling rate and channel count or do we allow changes on the fly?
  7298. if (param.getInt(String8(AudioParameter::keySamplingRate), value) == NO_ERROR) {
  7299. samplingRate = value;
  7300. reconfig = true;
  7301. }
  7302. if (param.getInt(String8(AudioParameter::keyFormat), value) == NO_ERROR) {
  7303. if (!audio_is_linear_pcm((audio_format_t) value)) {
  7304. status = BAD_VALUE;
  7305. } else {
  7306. reqFormat = (audio_format_t) value;
  7307. reconfig = true;
  7308. }
  7309. }
  7310. if (param.getInt(String8(AudioParameter::keyChannels), value) == NO_ERROR) {
  7311. audio_channel_mask_t mask = (audio_channel_mask_t) value;
  7312. if (!audio_is_input_channel(mask) ||
  7313. audio_channel_count_from_in_mask(mask) > FCC_8) {
  7314. status = BAD_VALUE;
  7315. } else {
  7316. channelMask = mask;
  7317. reconfig = true;
  7318. }
  7319. }
  7320. if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) {
  7321. // do not accept frame count changes if tracks are open as the track buffer
  7322. // size depends on frame count and correct behavior would not be guaranteed
  7323. // if frame count is changed after track creation
  7324. if (mActiveTracks.size() > 0) {
  7325. status = INVALID_OPERATION;
  7326. } else {
  7327. reconfig = true;
  7328. }
  7329. }
  7330. if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) {
  7331. // forward device change to effects that have requested to be
  7332. // aware of attached audio device.
  7333. for (size_t i = 0; i < mEffectChains.size(); i++) {
  7334. mEffectChains[i]->setDevice_l(value);
  7335. }
  7336. // store input device and output device but do not forward output device to audio HAL.
  7337. // Note that status is ignored by the caller for output device
  7338. // (see AudioFlinger::setParameters()
  7339. if (audio_is_output_devices(value)) {
  7340. mOutDevice = value;
  7341. status = BAD_VALUE;
  7342. } else {
  7343. mInDevice = value;
  7344. if (value != AUDIO_DEVICE_NONE) {
  7345. mPrevInDevice = value;
  7346. }
  7347. checkBtNrec_l();
  7348. }
  7349. }
  7350. if (param.getInt(String8(AudioParameter::keyInputSource), value) == NO_ERROR &&
  7351. mAudioSource != (audio_source_t)value) {
  7352. // forward device change to effects that have requested to be
  7353. // aware of attached audio device.
  7354. for (size_t i = 0; i < mEffectChains.size(); i++) {
  7355. mEffectChains[i]->setAudioSource_l((audio_source_t)value);
  7356. }
  7357. mAudioSource = (audio_source_t)value;
  7358. }
  7359. if (status == NO_ERROR) {
  7360. status = mInput->stream->setParameters(keyValuePair);
  7361. if (status == INVALID_OPERATION) {
  7362. inputStandBy();
  7363. status = mInput->stream->setParameters(keyValuePair);
  7364. }
  7365. if (reconfig) {
  7366. if (status == BAD_VALUE) {
  7367. uint32_t sRate;
  7368. audio_channel_mask_t channelMask;
  7369. audio_format_t format;
  7370. if (mInput->stream->getAudioProperties(&sRate, &channelMask, &format) == OK &&
  7371. audio_is_linear_pcm(format) && audio_is_linear_pcm(reqFormat) &&
  7372. sRate <= (AUDIO_RESAMPLER_DOWN_RATIO_MAX * samplingRate) &&
  7373. audio_channel_count_from_in_mask(channelMask) <= FCC_8) {
  7374. status = NO_ERROR;
  7375. }
  7376. }
  7377. if (status == NO_ERROR) {
  7378. readInputParameters_l();
  7379. sendIoConfigEvent_l(AUDIO_INPUT_CONFIG_CHANGED);
  7380. }
  7381. }
  7382. }
  7383. return reconfig;
  7384. }
  7385. String8 AudioFlinger::RecordThread::getParameters(const String8& keys)
  7386. {
  7387. Mutex::Autolock _l(mLock);
  7388. if (initCheck() == NO_ERROR) {
  7389. String8 out_s8;
  7390. if (mInput->stream->getParameters(keys, &out_s8) == OK) {
  7391. return out_s8;
  7392. }
  7393. }
  7394. return String8();
  7395. }
  7396. void AudioFlinger::RecordThread::ioConfigChanged(audio_io_config_event event, pid_t pid,
  7397. audio_port_handle_t portId) {
  7398. sp<AudioIoDescriptor> desc = new AudioIoDescriptor();
  7399. desc->mIoHandle = mId;
  7400. switch (event) {
  7401. case AUDIO_INPUT_OPENED:
  7402. case AUDIO_INPUT_REGISTERED:
  7403. case AUDIO_INPUT_CONFIG_CHANGED:
  7404. desc->mPatch = mPatch;
  7405. desc->mChannelMask = mChannelMask;
  7406. desc->mSamplingRate = mSampleRate;
  7407. desc->mFormat = mFormat;
  7408. desc->mFrameCount = mFrameCount;
  7409. desc->mFrameCountHAL = mFrameCount;
  7410. desc->mLatency = 0;
  7411. break;
  7412. case AUDIO_CLIENT_STARTED:
  7413. desc->mPatch = mPatch;
  7414. desc->mPortId = portId;
  7415. break;
  7416. case AUDIO_INPUT_CLOSED:
  7417. default:
  7418. break;
  7419. }
  7420. mAudioFlinger->ioConfigChanged(event, desc, pid);
  7421. }
  7422. void AudioFlinger::RecordThread::readInputParameters_l()
  7423. {
  7424. status_t result = mInput->stream->getAudioProperties(&mSampleRate, &mChannelMask, &mHALFormat);
  7425. LOG_ALWAYS_FATAL_IF(result != OK, "Error retrieving audio properties from HAL: %d", result);
  7426. mFormat = mHALFormat;
  7427. mChannelCount = audio_channel_count_from_in_mask(mChannelMask);
  7428. if (audio_is_linear_pcm(mFormat)) {
  7429. LOG_ALWAYS_FATAL_IF(mChannelCount > FCC_8, "HAL channel count %d > %d",
  7430. mChannelCount, FCC_8);
  7431. } else {
  7432. // Can have more that FCC_8 channels in encoded streams.
  7433. ALOGI("HAL format %#x is not linear pcm", mFormat);
  7434. }
  7435. result = mInput->stream->getFrameSize(&mFrameSize);
  7436. LOG_ALWAYS_FATAL_IF(result != OK, "Error retrieving frame size from HAL: %d", result);
  7437. result = mInput->stream->getBufferSize(&mBufferSize);
  7438. LOG_ALWAYS_FATAL_IF(result != OK, "Error retrieving buffer size from HAL: %d", result);
  7439. mFrameCount = mBufferSize / mFrameSize;
  7440. ALOGV("%p RecordThread params: mChannelCount=%u, mFormat=%#x, mFrameSize=%lld, "
  7441. "mBufferSize=%lld, mFrameCount=%lld",
  7442. this, mChannelCount, mFormat, (long long)mFrameSize, (long long)mBufferSize,
  7443. (long long)mFrameCount);
  7444. // This is the formula for calculating the temporary buffer size.
  7445. // With 7 HAL buffers, we can guarantee ability to down-sample the input by ratio of 6:1 to
  7446. // 1 full output buffer, regardless of the alignment of the available input.
  7447. // The value is somewhat arbitrary, and could probably be even larger.
  7448. // A larger value should allow more old data to be read after a track calls start(),
  7449. // without increasing latency.
  7450. //
  7451. // Note this is independent of the maximum downsampling ratio permitted for capture.
  7452. mRsmpInFrames = mFrameCount * 7;
  7453. mRsmpInFramesP2 = roundup(mRsmpInFrames);
  7454. free(mRsmpInBuffer);
  7455. mRsmpInBuffer = NULL;
  7456. // TODO optimize audio capture buffer sizes ...
  7457. // Here we calculate the size of the sliding buffer used as a source
  7458. // for resampling. mRsmpInFramesP2 is currently roundup(mFrameCount * 7).
  7459. // For current HAL frame counts, this is usually 2048 = 40 ms. It would
  7460. // be better to have it derived from the pipe depth in the long term.
  7461. // The current value is higher than necessary. However it should not add to latency.
  7462. // Over-allocate beyond mRsmpInFramesP2 to permit a HAL read past end of buffer
  7463. mRsmpInFramesOA = mRsmpInFramesP2 + mFrameCount - 1;
  7464. (void)posix_memalign(&mRsmpInBuffer, 32, mRsmpInFramesOA * mFrameSize);
  7465. // if posix_memalign fails, will segv here.
  7466. memset(mRsmpInBuffer, 0, mRsmpInFramesOA * mFrameSize);
  7467. // AudioRecord mSampleRate and mChannelCount are constant due to AudioRecord API constraints.
  7468. // But if thread's mSampleRate or mChannelCount changes, how will that affect active tracks?
  7469. }
  7470. uint32_t AudioFlinger::RecordThread::getInputFramesLost()
  7471. {
  7472. Mutex::Autolock _l(mLock);
  7473. uint32_t result;
  7474. if (initCheck() == NO_ERROR && mInput->stream->getInputFramesLost(&result) == OK) {
  7475. return result;
  7476. }
  7477. return 0;
  7478. }
  7479. KeyedVector<audio_session_t, bool> AudioFlinger::RecordThread::sessionIds() const
  7480. {
  7481. KeyedVector<audio_session_t, bool> ids;
  7482. Mutex::Autolock _l(mLock);
  7483. for (size_t j = 0; j < mTracks.size(); ++j) {
  7484. sp<RecordThread::RecordTrack> track = mTracks[j];
  7485. audio_session_t sessionId = track->sessionId();
  7486. if (ids.indexOfKey(sessionId) < 0) {
  7487. ids.add(sessionId, true);
  7488. }
  7489. }
  7490. return ids;
  7491. }
  7492. AudioFlinger::AudioStreamIn* AudioFlinger::RecordThread::clearInput()
  7493. {
  7494. Mutex::Autolock _l(mLock);
  7495. AudioStreamIn *input = mInput;
  7496. mInput = NULL;
  7497. return input;
  7498. }
  7499. // this method must always be called either with ThreadBase mLock held or inside the thread loop
  7500. sp<StreamHalInterface> AudioFlinger::RecordThread::stream() const
  7501. {
  7502. if (mInput == NULL) {
  7503. return NULL;
  7504. }
  7505. return mInput->stream;
  7506. }
  7507. status_t AudioFlinger::RecordThread::addEffectChain_l(const sp<EffectChain>& chain)
  7508. {
  7509. ALOGV("addEffectChain_l() %p on thread %p", chain.get(), this);
  7510. chain->setThread(this);
  7511. chain->setInBuffer(NULL);
  7512. chain->setOutBuffer(NULL);
  7513. checkSuspendOnAddEffectChain_l(chain);
  7514. // make sure enabled pre processing effects state is communicated to the HAL as we
  7515. // just moved them to a new input stream.
  7516. chain->syncHalEffectsState();
  7517. mEffectChains.add(chain);
  7518. return NO_ERROR;
  7519. }
  7520. size_t AudioFlinger::RecordThread::removeEffectChain_l(const sp<EffectChain>& chain)
  7521. {
  7522. ALOGV("removeEffectChain_l() %p from thread %p", chain.get(), this);
  7523. for (size_t i = 0; i < mEffectChains.size(); i++) {
  7524. if (chain == mEffectChains[i]) {
  7525. mEffectChains.removeAt(i);
  7526. break;
  7527. }
  7528. }
  7529. return mEffectChains.size();
  7530. }
  7531. status_t AudioFlinger::RecordThread::createAudioPatch_l(const struct audio_patch *patch,
  7532. audio_patch_handle_t *handle)
  7533. {
  7534. status_t status = NO_ERROR;
  7535. // store new device and send to effects
  7536. mInDevice = patch->sources[0].ext.device.type;
  7537. audio_port_handle_t deviceId = patch->sources[0].id;
  7538. mPatch = *patch;
  7539. for (size_t i = 0; i < mEffectChains.size(); i++) {
  7540. mEffectChains[i]->setDevice_l(mInDevice);
  7541. }
  7542. checkBtNrec_l();
  7543. // store new source and send to effects
  7544. if (mAudioSource != patch->sinks[0].ext.mix.usecase.source) {
  7545. mAudioSource = patch->sinks[0].ext.mix.usecase.source;
  7546. for (size_t i = 0; i < mEffectChains.size(); i++) {
  7547. mEffectChains[i]->setAudioSource_l(mAudioSource);
  7548. }
  7549. }
  7550. if (mInput->audioHwDev->supportsAudioPatches()) {
  7551. sp<DeviceHalInterface> hwDevice = mInput->audioHwDev->hwDevice();
  7552. status = hwDevice->createAudioPatch(patch->num_sources,
  7553. patch->sources,
  7554. patch->num_sinks,
  7555. patch->sinks,
  7556. handle);
  7557. } else {
  7558. char *address;
  7559. if (strcmp(patch->sources[0].ext.device.address, "") != 0) {
  7560. address = audio_device_address_to_parameter(
  7561. patch->sources[0].ext.device.type,
  7562. patch->sources[0].ext.device.address);
  7563. } else {
  7564. address = (char *)calloc(1, 1);
  7565. }
  7566. AudioParameter param = AudioParameter(String8(address));
  7567. free(address);
  7568. param.addInt(String8(AudioParameter::keyRouting),
  7569. (int)patch->sources[0].ext.device.type);
  7570. param.addInt(String8(AudioParameter::keyInputSource),
  7571. (int)patch->sinks[0].ext.mix.usecase.source);
  7572. status = mInput->stream->setParameters(param.toString());
  7573. *handle = AUDIO_PATCH_HANDLE_NONE;
  7574. }
  7575. if ((mInDevice != mPrevInDevice) || (mDeviceId != deviceId)) {
  7576. sendIoConfigEvent_l(AUDIO_INPUT_CONFIG_CHANGED);
  7577. mPrevInDevice = mInDevice;
  7578. mDeviceId = deviceId;
  7579. }
  7580. return status;
  7581. }
  7582. status_t AudioFlinger::RecordThread::releaseAudioPatch_l(const audio_patch_handle_t handle)
  7583. {
  7584. status_t status = NO_ERROR;
  7585. mInDevice = AUDIO_DEVICE_NONE;
  7586. if (mInput->audioHwDev->supportsAudioPatches()) {
  7587. sp<DeviceHalInterface> hwDevice = mInput->audioHwDev->hwDevice();
  7588. status = hwDevice->releaseAudioPatch(handle);
  7589. } else {
  7590. AudioParameter param;
  7591. param.addInt(String8(AudioParameter::keyRouting), 0);
  7592. status = mInput->stream->setParameters(param.toString());
  7593. }
  7594. return status;
  7595. }
  7596. void AudioFlinger::RecordThread::addPatchTrack(const sp<PatchRecord>& record)
  7597. {
  7598. Mutex::Autolock _l(mLock);
  7599. mTracks.add(record);
  7600. }
  7601. void AudioFlinger::RecordThread::deletePatchTrack(const sp<PatchRecord>& record)
  7602. {
  7603. Mutex::Autolock _l(mLock);
  7604. destroyTrack_l(record);
  7605. }
  7606. void AudioFlinger::RecordThread::toAudioPortConfig(struct audio_port_config *config)
  7607. {
  7608. ThreadBase::toAudioPortConfig(config);
  7609. config->role = AUDIO_PORT_ROLE_SINK;
  7610. config->ext.mix.hw_module = mInput->audioHwDev->handle();
  7611. config->ext.mix.usecase.source = mAudioSource;
  7612. if (mInput && mInput->flags != AUDIO_INPUT_FLAG_NONE) {
  7613. config->config_mask |= AUDIO_PORT_CONFIG_FLAGS;
  7614. config->flags.input = mInput->flags;
  7615. }
  7616. }
  7617. // ----------------------------------------------------------------------------
  7618. // Mmap
  7619. // ----------------------------------------------------------------------------
  7620. AudioFlinger::MmapThreadHandle::MmapThreadHandle(const sp<MmapThread>& thread)
  7621. : mThread(thread)
  7622. {
  7623. assert(thread != 0); // thread must start non-null and stay non-null
  7624. }
  7625. AudioFlinger::MmapThreadHandle::~MmapThreadHandle()
  7626. {
  7627. mThread->disconnect();
  7628. }
  7629. status_t AudioFlinger::MmapThreadHandle::createMmapBuffer(int32_t minSizeFrames,
  7630. struct audio_mmap_buffer_info *info)
  7631. {
  7632. return mThread->createMmapBuffer(minSizeFrames, info);
  7633. }
  7634. status_t AudioFlinger::MmapThreadHandle::getMmapPosition(struct audio_mmap_position *position)
  7635. {
  7636. return mThread->getMmapPosition(position);
  7637. }
  7638. status_t AudioFlinger::MmapThreadHandle::start(const AudioClient& client,
  7639. audio_port_handle_t *handle)
  7640. {
  7641. return mThread->start(client, handle);
  7642. }
  7643. status_t AudioFlinger::MmapThreadHandle::stop(audio_port_handle_t handle)
  7644. {
  7645. return mThread->stop(handle);
  7646. }
  7647. status_t AudioFlinger::MmapThreadHandle::standby()
  7648. {
  7649. return mThread->standby();
  7650. }
  7651. AudioFlinger::MmapThread::MmapThread(
  7652. const sp<AudioFlinger>& audioFlinger, audio_io_handle_t id,
  7653. AudioHwDevice *hwDev, sp<StreamHalInterface> stream,
  7654. audio_devices_t outDevice, audio_devices_t inDevice, bool systemReady)
  7655. : ThreadBase(audioFlinger, id, outDevice, inDevice, MMAP, systemReady),
  7656. mSessionId(AUDIO_SESSION_NONE),
  7657. mPortId(AUDIO_PORT_HANDLE_NONE),
  7658. mHalStream(stream), mHalDevice(hwDev->hwDevice()), mAudioHwDev(hwDev),
  7659. mActiveTracks(&this->mLocalLog),
  7660. mHalVolFloat(-1.0f), // Initialize to illegal value so it always gets set properly later.
  7661. mNoCallbackWarningCount(0)
  7662. {
  7663. mStandby = true;
  7664. readHalParameters_l();
  7665. }
  7666. AudioFlinger::MmapThread::~MmapThread()
  7667. {
  7668. releaseWakeLock_l();
  7669. }
  7670. void AudioFlinger::MmapThread::onFirstRef()
  7671. {
  7672. run(mThreadName, ANDROID_PRIORITY_URGENT_AUDIO);
  7673. }
  7674. void AudioFlinger::MmapThread::disconnect()
  7675. {
  7676. ActiveTracks<MmapTrack> activeTracks;
  7677. {
  7678. Mutex::Autolock _l(mLock);
  7679. for (const sp<MmapTrack> &t : mActiveTracks) {
  7680. activeTracks.add(t);
  7681. }
  7682. }
  7683. for (const sp<MmapTrack> &t : activeTracks) {
  7684. stop(t->portId());
  7685. }
  7686. // This will decrement references and may cause the destruction of this thread.
  7687. if (isOutput()) {
  7688. AudioSystem::releaseOutput(mPortId);
  7689. } else {
  7690. AudioSystem::releaseInput(mPortId);
  7691. }
  7692. }
  7693. void AudioFlinger::MmapThread::configure(const audio_attributes_t *attr,
  7694. audio_stream_type_t streamType __unused,
  7695. audio_session_t sessionId,
  7696. const sp<MmapStreamCallback>& callback,
  7697. audio_port_handle_t deviceId,
  7698. audio_port_handle_t portId)
  7699. {
  7700. mAttr = *attr;
  7701. mSessionId = sessionId;
  7702. mCallback = callback;
  7703. mDeviceId = deviceId;
  7704. mPortId = portId;
  7705. }
  7706. status_t AudioFlinger::MmapThread::createMmapBuffer(int32_t minSizeFrames,
  7707. struct audio_mmap_buffer_info *info)
  7708. {
  7709. if (mHalStream == 0) {
  7710. return NO_INIT;
  7711. }
  7712. mStandby = true;
  7713. acquireWakeLock();
  7714. return mHalStream->createMmapBuffer(minSizeFrames, info);
  7715. }
  7716. status_t AudioFlinger::MmapThread::getMmapPosition(struct audio_mmap_position *position)
  7717. {
  7718. if (mHalStream == 0) {
  7719. return NO_INIT;
  7720. }
  7721. return mHalStream->getMmapPosition(position);
  7722. }
  7723. status_t AudioFlinger::MmapThread::exitStandby()
  7724. {
  7725. status_t ret = mHalStream->start();
  7726. if (ret != NO_ERROR) {
  7727. ALOGE("%s: error mHalStream->start() = %d for first track", __FUNCTION__, ret);
  7728. return ret;
  7729. }
  7730. mStandby = false;
  7731. return NO_ERROR;
  7732. }
  7733. status_t AudioFlinger::MmapThread::start(const AudioClient& client,
  7734. audio_port_handle_t *handle)
  7735. {
  7736. ALOGV("%s clientUid %d mStandby %d mPortId %d *handle %d", __FUNCTION__,
  7737. client.clientUid, mStandby, mPortId, *handle);
  7738. if (mHalStream == 0) {
  7739. return NO_INIT;
  7740. }
  7741. status_t ret;
  7742. if (*handle == mPortId) {
  7743. // for the first track, reuse portId and session allocated when the stream was opened
  7744. return exitStandby();
  7745. }
  7746. audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE;
  7747. audio_io_handle_t io = mId;
  7748. if (isOutput()) {
  7749. audio_config_t config = AUDIO_CONFIG_INITIALIZER;
  7750. config.sample_rate = mSampleRate;
  7751. config.channel_mask = mChannelMask;
  7752. config.format = mFormat;
  7753. audio_stream_type_t stream = streamType();
  7754. audio_output_flags_t flags =
  7755. (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_MMAP_NOIRQ | AUDIO_OUTPUT_FLAG_DIRECT);
  7756. audio_port_handle_t deviceId = mDeviceId;
  7757. std::vector<audio_io_handle_t> secondaryOutputs;
  7758. ret = AudioSystem::getOutputForAttr(&mAttr, &io,
  7759. mSessionId,
  7760. &stream,
  7761. client.clientPid,
  7762. client.clientUid,
  7763. &config,
  7764. flags,
  7765. &deviceId,
  7766. &portId,
  7767. &secondaryOutputs);
  7768. ALOGD_IF(!secondaryOutputs.empty(),
  7769. "MmapThread::start does not support secondary outputs, ignoring them");
  7770. } else {
  7771. audio_config_base_t config;
  7772. config.sample_rate = mSampleRate;
  7773. config.channel_mask = mChannelMask;
  7774. config.format = mFormat;
  7775. audio_port_handle_t deviceId = mDeviceId;
  7776. ret = AudioSystem::getInputForAttr(&mAttr, &io,
  7777. RECORD_RIID_INVALID,
  7778. mSessionId,
  7779. client.clientPid,
  7780. client.clientUid,
  7781. client.packageName,
  7782. &config,
  7783. AUDIO_INPUT_FLAG_MMAP_NOIRQ,
  7784. &deviceId,
  7785. &portId);
  7786. }
  7787. // APM should not chose a different input or output stream for the same set of attributes
  7788. // and audo configuration
  7789. if (ret != NO_ERROR || io != mId) {
  7790. ALOGE("%s: error getting output or input from APM (error %d, io %d expected io %d)",
  7791. __FUNCTION__, ret, io, mId);
  7792. return BAD_VALUE;
  7793. }
  7794. if (isOutput()) {
  7795. ret = AudioSystem::startOutput(portId);
  7796. } else {
  7797. ret = AudioSystem::startInput(portId);
  7798. }
  7799. Mutex::Autolock _l(mLock);
  7800. // abort if start is rejected by audio policy manager
  7801. if (ret != NO_ERROR) {
  7802. ALOGE("%s: error start rejected by AudioPolicyManager = %d", __FUNCTION__, ret);
  7803. if (!mActiveTracks.isEmpty()) {
  7804. mLock.unlock();
  7805. if (isOutput()) {
  7806. AudioSystem::releaseOutput(portId);
  7807. } else {
  7808. AudioSystem::releaseInput(portId);
  7809. }
  7810. mLock.lock();
  7811. } else {
  7812. mHalStream->stop();
  7813. }
  7814. return PERMISSION_DENIED;
  7815. }
  7816. // Given that MmapThread::mAttr is mutable, should a MmapTrack have attributes ?
  7817. sp<MmapTrack> track = new MmapTrack(this, mAttr, mSampleRate, mFormat, mChannelMask, mSessionId,
  7818. isOutput(), client.clientUid, client.clientPid,
  7819. IPCThreadState::self()->getCallingPid(), portId);
  7820. if (isOutput()) {
  7821. // force volume update when a new track is added
  7822. mHalVolFloat = -1.0f;
  7823. } else if (!track->isSilenced_l()) {
  7824. for (const sp<MmapTrack> &t : mActiveTracks) {
  7825. if (t->isSilenced_l() && t->uid() != client.clientUid)
  7826. t->invalidate();
  7827. }
  7828. }
  7829. mActiveTracks.add(track);
  7830. sp<EffectChain> chain = getEffectChain_l(mSessionId);
  7831. if (chain != 0) {
  7832. chain->setStrategy(AudioSystem::getStrategyForStream(streamType()));
  7833. chain->incTrackCnt();
  7834. chain->incActiveTrackCnt();
  7835. }
  7836. *handle = portId;
  7837. broadcast_l();
  7838. ALOGV("%s DONE handle %d stream %p", __FUNCTION__, *handle, mHalStream.get());
  7839. return NO_ERROR;
  7840. }
  7841. status_t AudioFlinger::MmapThread::stop(audio_port_handle_t handle)
  7842. {
  7843. ALOGV("%s handle %d", __FUNCTION__, handle);
  7844. if (mHalStream == 0) {
  7845. return NO_INIT;
  7846. }
  7847. if (handle == mPortId) {
  7848. mHalStream->stop();
  7849. return NO_ERROR;
  7850. }
  7851. Mutex::Autolock _l(mLock);
  7852. sp<MmapTrack> track;
  7853. for (const sp<MmapTrack> &t : mActiveTracks) {
  7854. if (handle == t->portId()) {
  7855. track = t;
  7856. break;
  7857. }
  7858. }
  7859. if (track == 0) {
  7860. return BAD_VALUE;
  7861. }
  7862. mActiveTracks.remove(track);
  7863. mLock.unlock();
  7864. if (isOutput()) {
  7865. AudioSystem::stopOutput(track->portId());
  7866. AudioSystem::releaseOutput(track->portId());
  7867. } else {
  7868. AudioSystem::stopInput(track->portId());
  7869. AudioSystem::releaseInput(track->portId());
  7870. }
  7871. mLock.lock();
  7872. sp<EffectChain> chain = getEffectChain_l(track->sessionId());
  7873. if (chain != 0) {
  7874. chain->decActiveTrackCnt();
  7875. chain->decTrackCnt();
  7876. }
  7877. broadcast_l();
  7878. return NO_ERROR;
  7879. }
  7880. status_t AudioFlinger::MmapThread::standby()
  7881. {
  7882. ALOGV("%s", __FUNCTION__);
  7883. if (mHalStream == 0) {
  7884. return NO_INIT;
  7885. }
  7886. if (!mActiveTracks.isEmpty()) {
  7887. return INVALID_OPERATION;
  7888. }
  7889. mHalStream->standby();
  7890. mStandby = true;
  7891. releaseWakeLock();
  7892. return NO_ERROR;
  7893. }
  7894. void AudioFlinger::MmapThread::readHalParameters_l()
  7895. {
  7896. status_t result = mHalStream->getAudioProperties(&mSampleRate, &mChannelMask, &mHALFormat);
  7897. LOG_ALWAYS_FATAL_IF(result != OK, "Error retrieving audio properties from HAL: %d", result);
  7898. mFormat = mHALFormat;
  7899. LOG_ALWAYS_FATAL_IF(!audio_is_linear_pcm(mFormat), "HAL format %#x is not linear pcm", mFormat);
  7900. result = mHalStream->getFrameSize(&mFrameSize);
  7901. LOG_ALWAYS_FATAL_IF(result != OK, "Error retrieving frame size from HAL: %d", result);
  7902. result = mHalStream->getBufferSize(&mBufferSize);
  7903. LOG_ALWAYS_FATAL_IF(result != OK, "Error retrieving buffer size from HAL: %d", result);
  7904. mFrameCount = mBufferSize / mFrameSize;
  7905. }
  7906. bool AudioFlinger::MmapThread::threadLoop()
  7907. {
  7908. checkSilentMode_l();
  7909. const String8 myName(String8::format("thread %p type %d TID %d", this, mType, gettid()));
  7910. while (!exitPending())
  7911. {
  7912. Vector< sp<EffectChain> > effectChains;
  7913. { // under Thread lock
  7914. Mutex::Autolock _l(mLock);
  7915. if (mSignalPending) {
  7916. // A signal was raised while we were unlocked
  7917. mSignalPending = false;
  7918. } else {
  7919. if (mConfigEvents.isEmpty()) {
  7920. // we're about to wait, flush the binder command buffer
  7921. IPCThreadState::self()->flushCommands();
  7922. if (exitPending()) {
  7923. break;
  7924. }
  7925. // wait until we have something to do...
  7926. ALOGV("%s going to sleep", myName.string());
  7927. mWaitWorkCV.wait(mLock);
  7928. ALOGV("%s waking up", myName.string());
  7929. checkSilentMode_l();
  7930. continue;
  7931. }
  7932. }
  7933. processConfigEvents_l();
  7934. processVolume_l();
  7935. checkInvalidTracks_l();
  7936. mActiveTracks.updatePowerState(this);
  7937. updateMetadata_l();
  7938. lockEffectChains_l(effectChains);
  7939. } // release Thread lock
  7940. for (size_t i = 0; i < effectChains.size(); i ++) {
  7941. effectChains[i]->process_l(); // Thread is not locked, but effect chain is locked
  7942. }
  7943. // enable changes in effect chain, including moving to another thread.
  7944. unlockEffectChains(effectChains);
  7945. // Effect chains will be actually deleted here if they were removed from
  7946. // mEffectChains list during mixing or effects processing
  7947. }
  7948. threadLoop_exit();
  7949. if (!mStandby) {
  7950. threadLoop_standby();
  7951. mStandby = true;
  7952. }
  7953. ALOGV("Thread %p type %d exiting", this, mType);
  7954. return false;
  7955. }
  7956. // checkForNewParameter_l() must be called with ThreadBase::mLock held
  7957. bool AudioFlinger::MmapThread::checkForNewParameter_l(const String8& keyValuePair,
  7958. status_t& status)
  7959. {
  7960. AudioParameter param = AudioParameter(keyValuePair);
  7961. int value;
  7962. bool sendToHal = true;
  7963. if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) {
  7964. audio_devices_t device = (audio_devices_t)value;
  7965. // forward device change to effects that have requested to be
  7966. // aware of attached audio device.
  7967. if (device != AUDIO_DEVICE_NONE) {
  7968. for (size_t i = 0; i < mEffectChains.size(); i++) {
  7969. mEffectChains[i]->setDevice_l(device);
  7970. }
  7971. }
  7972. if (audio_is_output_devices(device)) {
  7973. mOutDevice = device;
  7974. if (!isOutput()) {
  7975. sendToHal = false;
  7976. }
  7977. } else {
  7978. mInDevice = device;
  7979. if (device != AUDIO_DEVICE_NONE) {
  7980. mPrevInDevice = value;
  7981. }
  7982. // TODO: implement and call checkBtNrec_l();
  7983. }
  7984. }
  7985. if (sendToHal) {
  7986. status = mHalStream->setParameters(keyValuePair);
  7987. } else {
  7988. status = NO_ERROR;
  7989. }
  7990. return false;
  7991. }
  7992. String8 AudioFlinger::MmapThread::getParameters(const String8& keys)
  7993. {
  7994. Mutex::Autolock _l(mLock);
  7995. String8 out_s8;
  7996. if (initCheck() == NO_ERROR && mHalStream->getParameters(keys, &out_s8) == OK) {
  7997. return out_s8;
  7998. }
  7999. return String8();
  8000. }
  8001. void AudioFlinger::MmapThread::ioConfigChanged(audio_io_config_event event, pid_t pid,
  8002. audio_port_handle_t portId __unused) {
  8003. sp<AudioIoDescriptor> desc = new AudioIoDescriptor();
  8004. desc->mIoHandle = mId;
  8005. switch (event) {
  8006. case AUDIO_INPUT_OPENED:
  8007. case AUDIO_INPUT_REGISTERED:
  8008. case AUDIO_INPUT_CONFIG_CHANGED:
  8009. case AUDIO_OUTPUT_OPENED:
  8010. case AUDIO_OUTPUT_REGISTERED:
  8011. case AUDIO_OUTPUT_CONFIG_CHANGED:
  8012. desc->mPatch = mPatch;
  8013. desc->mChannelMask = mChannelMask;
  8014. desc->mSamplingRate = mSampleRate;
  8015. desc->mFormat = mFormat;
  8016. desc->mFrameCount = mFrameCount;
  8017. desc->mFrameCountHAL = mFrameCount;
  8018. desc->mLatency = 0;
  8019. break;
  8020. case AUDIO_INPUT_CLOSED:
  8021. case AUDIO_OUTPUT_CLOSED:
  8022. default:
  8023. break;
  8024. }
  8025. mAudioFlinger->ioConfigChanged(event, desc, pid);
  8026. }
  8027. status_t AudioFlinger::MmapThread::createAudioPatch_l(const struct audio_patch *patch,
  8028. audio_patch_handle_t *handle)
  8029. {
  8030. status_t status = NO_ERROR;
  8031. // store new device and send to effects
  8032. audio_devices_t type = AUDIO_DEVICE_NONE;
  8033. audio_port_handle_t deviceId;
  8034. if (isOutput()) {
  8035. for (unsigned int i = 0; i < patch->num_sinks; i++) {
  8036. type |= patch->sinks[i].ext.device.type;
  8037. }
  8038. deviceId = patch->sinks[0].id;
  8039. } else {
  8040. type = patch->sources[0].ext.device.type;
  8041. deviceId = patch->sources[0].id;
  8042. }
  8043. for (size_t i = 0; i < mEffectChains.size(); i++) {
  8044. mEffectChains[i]->setDevice_l(type);
  8045. }
  8046. if (isOutput()) {
  8047. mOutDevice = type;
  8048. } else {
  8049. mInDevice = type;
  8050. // store new source and send to effects
  8051. if (mAudioSource != patch->sinks[0].ext.mix.usecase.source) {
  8052. mAudioSource = patch->sinks[0].ext.mix.usecase.source;
  8053. for (size_t i = 0; i < mEffectChains.size(); i++) {
  8054. mEffectChains[i]->setAudioSource_l(mAudioSource);
  8055. }
  8056. }
  8057. }
  8058. if (mAudioHwDev->supportsAudioPatches()) {
  8059. status = mHalDevice->createAudioPatch(patch->num_sources,
  8060. patch->sources,
  8061. patch->num_sinks,
  8062. patch->sinks,
  8063. handle);
  8064. } else {
  8065. char *address;
  8066. if (strcmp(patch->sinks[0].ext.device.address, "") != 0) {
  8067. //FIXME: we only support address on first sink with HAL version < 3.0
  8068. address = audio_device_address_to_parameter(
  8069. patch->sinks[0].ext.device.type,
  8070. patch->sinks[0].ext.device.address);
  8071. } else {
  8072. address = (char *)calloc(1, 1);
  8073. }
  8074. AudioParameter param = AudioParameter(String8(address));
  8075. free(address);
  8076. param.addInt(String8(AudioParameter::keyRouting), (int)type);
  8077. if (!isOutput()) {
  8078. param.addInt(String8(AudioParameter::keyInputSource),
  8079. (int)patch->sinks[0].ext.mix.usecase.source);
  8080. }
  8081. status = mHalStream->setParameters(param.toString());
  8082. *handle = AUDIO_PATCH_HANDLE_NONE;
  8083. }
  8084. if (isOutput() && (mPrevOutDevice != mOutDevice || mDeviceId != deviceId)) {
  8085. mPrevOutDevice = type;
  8086. sendIoConfigEvent_l(AUDIO_OUTPUT_CONFIG_CHANGED);
  8087. sp<MmapStreamCallback> callback = mCallback.promote();
  8088. if (mDeviceId != deviceId && callback != 0) {
  8089. mLock.unlock();
  8090. callback->onRoutingChanged(deviceId);
  8091. mLock.lock();
  8092. }
  8093. mDeviceId = deviceId;
  8094. }
  8095. if (!isOutput() && (mPrevInDevice != mInDevice || mDeviceId != deviceId)) {
  8096. mPrevInDevice = type;
  8097. sendIoConfigEvent_l(AUDIO_INPUT_CONFIG_CHANGED);
  8098. sp<MmapStreamCallback> callback = mCallback.promote();
  8099. if (mDeviceId != deviceId && callback != 0) {
  8100. mLock.unlock();
  8101. callback->onRoutingChanged(deviceId);
  8102. mLock.lock();
  8103. }
  8104. mDeviceId = deviceId;
  8105. }
  8106. return status;
  8107. }
  8108. status_t AudioFlinger::MmapThread::releaseAudioPatch_l(const audio_patch_handle_t handle)
  8109. {
  8110. status_t status = NO_ERROR;
  8111. mInDevice = AUDIO_DEVICE_NONE;
  8112. bool supportsAudioPatches = mHalDevice->supportsAudioPatches(&supportsAudioPatches) == OK ?
  8113. supportsAudioPatches : false;
  8114. if (supportsAudioPatches) {
  8115. status = mHalDevice->releaseAudioPatch(handle);
  8116. } else {
  8117. AudioParameter param;
  8118. param.addInt(String8(AudioParameter::keyRouting), 0);
  8119. status = mHalStream->setParameters(param.toString());
  8120. }
  8121. return status;
  8122. }
  8123. void AudioFlinger::MmapThread::toAudioPortConfig(struct audio_port_config *config)
  8124. {
  8125. ThreadBase::toAudioPortConfig(config);
  8126. if (isOutput()) {
  8127. config->role = AUDIO_PORT_ROLE_SOURCE;
  8128. config->ext.mix.hw_module = mAudioHwDev->handle();
  8129. config->ext.mix.usecase.stream = AUDIO_STREAM_DEFAULT;
  8130. } else {
  8131. config->role = AUDIO_PORT_ROLE_SINK;
  8132. config->ext.mix.hw_module = mAudioHwDev->handle();
  8133. config->ext.mix.usecase.source = mAudioSource;
  8134. }
  8135. }
  8136. status_t AudioFlinger::MmapThread::addEffectChain_l(const sp<EffectChain>& chain)
  8137. {
  8138. audio_session_t session = chain->sessionId();
  8139. ALOGV("addEffectChain_l() %p on thread %p for session %d", chain.get(), this, session);
  8140. // Attach all tracks with same session ID to this chain.
  8141. // indicate all active tracks in the chain
  8142. for (const sp<MmapTrack> &track : mActiveTracks) {
  8143. if (session == track->sessionId()) {
  8144. chain->incTrackCnt();
  8145. chain->incActiveTrackCnt();
  8146. }
  8147. }
  8148. chain->setThread(this);
  8149. chain->setInBuffer(nullptr);
  8150. chain->setOutBuffer(nullptr);
  8151. chain->syncHalEffectsState();
  8152. mEffectChains.add(chain);
  8153. checkSuspendOnAddEffectChain_l(chain);
  8154. return NO_ERROR;
  8155. }
  8156. size_t AudioFlinger::MmapThread::removeEffectChain_l(const sp<EffectChain>& chain)
  8157. {
  8158. audio_session_t session = chain->sessionId();
  8159. ALOGV("removeEffectChain_l() %p from thread %p for session %d", chain.get(), this, session);
  8160. for (size_t i = 0; i < mEffectChains.size(); i++) {
  8161. if (chain == mEffectChains[i]) {
  8162. mEffectChains.removeAt(i);
  8163. // detach all active tracks from the chain
  8164. // detach all tracks with same session ID from this chain
  8165. for (const sp<MmapTrack> &track : mActiveTracks) {
  8166. if (session == track->sessionId()) {
  8167. chain->decActiveTrackCnt();
  8168. chain->decTrackCnt();
  8169. }
  8170. }
  8171. break;
  8172. }
  8173. }
  8174. return mEffectChains.size();
  8175. }
  8176. void AudioFlinger::MmapThread::threadLoop_standby()
  8177. {
  8178. mHalStream->standby();
  8179. }
  8180. void AudioFlinger::MmapThread::threadLoop_exit()
  8181. {
  8182. // Do not call callback->onTearDown() because it is redundant for thread exit
  8183. // and because it can cause a recursive mutex lock on stop().
  8184. }
  8185. status_t AudioFlinger::MmapThread::setSyncEvent(const sp<SyncEvent>& event __unused)
  8186. {
  8187. return BAD_VALUE;
  8188. }
  8189. bool AudioFlinger::MmapThread::isValidSyncEvent(const sp<SyncEvent>& event __unused) const
  8190. {
  8191. return false;
  8192. }
  8193. status_t AudioFlinger::MmapThread::checkEffectCompatibility_l(
  8194. const effect_descriptor_t *desc, audio_session_t sessionId)
  8195. {
  8196. // No global effect sessions on mmap threads
  8197. if (sessionId == AUDIO_SESSION_OUTPUT_MIX || sessionId == AUDIO_SESSION_OUTPUT_STAGE) {
  8198. ALOGW("checkEffectCompatibility_l(): global effect %s on record thread %s",
  8199. desc->name, mThreadName);
  8200. return BAD_VALUE;
  8201. }
  8202. if (!isOutput() && ((desc->flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_PRE_PROC)) {
  8203. ALOGW("checkEffectCompatibility_l(): non pre processing effect %s on capture mmap thread",
  8204. desc->name);
  8205. return BAD_VALUE;
  8206. }
  8207. if (isOutput() && ((desc->flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC)) {
  8208. ALOGW("checkEffectCompatibility_l(): pre processing effect %s created on playback mmap "
  8209. "thread", desc->name);
  8210. return BAD_VALUE;
  8211. }
  8212. // Only allow effects without processing load or latency
  8213. if ((desc->flags & EFFECT_FLAG_NO_PROCESS_MASK) != EFFECT_FLAG_NO_PROCESS) {
  8214. return BAD_VALUE;
  8215. }
  8216. return NO_ERROR;
  8217. }
  8218. void AudioFlinger::MmapThread::checkInvalidTracks_l()
  8219. {
  8220. for (const sp<MmapTrack> &track : mActiveTracks) {
  8221. if (track->isInvalid()) {
  8222. sp<MmapStreamCallback> callback = mCallback.promote();
  8223. if (callback != 0) {
  8224. mLock.unlock();
  8225. callback->onTearDown(track->portId());
  8226. mLock.lock();
  8227. } else if (mNoCallbackWarningCount < kMaxNoCallbackWarnings) {
  8228. ALOGW("Could not notify MMAP stream tear down: no onTearDown callback!");
  8229. mNoCallbackWarningCount++;
  8230. }
  8231. }
  8232. }
  8233. }
  8234. void AudioFlinger::MmapThread::dumpInternals_l(int fd, const Vector<String16>& args __unused)
  8235. {
  8236. dprintf(fd, " Attributes: content type %d usage %d source %d\n",
  8237. mAttr.content_type, mAttr.usage, mAttr.source);
  8238. dprintf(fd, " Session: %d port Id: %d\n", mSessionId, mPortId);
  8239. if (mActiveTracks.isEmpty()) {
  8240. dprintf(fd, " No active clients\n");
  8241. }
  8242. }
  8243. void AudioFlinger::MmapThread::dumpTracks_l(int fd, const Vector<String16>& args __unused)
  8244. {
  8245. String8 result;
  8246. size_t numtracks = mActiveTracks.size();
  8247. dprintf(fd, " %zu Tracks\n", numtracks);
  8248. const char *prefix = " ";
  8249. if (numtracks) {
  8250. result.append(prefix);
  8251. mActiveTracks[0]->appendDumpHeader(result);
  8252. for (size_t i = 0; i < numtracks ; ++i) {
  8253. sp<MmapTrack> track = mActiveTracks[i];
  8254. result.append(prefix);
  8255. track->appendDump(result, true /* active */);
  8256. }
  8257. } else {
  8258. dprintf(fd, "\n");
  8259. }
  8260. write(fd, result.string(), result.size());
  8261. }
  8262. AudioFlinger::MmapPlaybackThread::MmapPlaybackThread(
  8263. const sp<AudioFlinger>& audioFlinger, audio_io_handle_t id,
  8264. AudioHwDevice *hwDev, AudioStreamOut *output,
  8265. audio_devices_t outDevice, audio_devices_t inDevice, bool systemReady)
  8266. : MmapThread(audioFlinger, id, hwDev, output->stream, outDevice, inDevice, systemReady),
  8267. mStreamType(AUDIO_STREAM_MUSIC),
  8268. mStreamVolume(1.0),
  8269. mStreamMute(false),
  8270. mOutput(output)
  8271. {
  8272. snprintf(mThreadName, kThreadNameLength, "AudioMmapOut_%X", id);
  8273. mChannelCount = audio_channel_count_from_out_mask(mChannelMask);
  8274. mMasterVolume = audioFlinger->masterVolume_l();
  8275. mMasterMute = audioFlinger->masterMute_l();
  8276. if (mAudioHwDev) {
  8277. if (mAudioHwDev->canSetMasterVolume()) {
  8278. mMasterVolume = 1.0;
  8279. }
  8280. if (mAudioHwDev->canSetMasterMute()) {
  8281. mMasterMute = false;
  8282. }
  8283. }
  8284. }
  8285. void AudioFlinger::MmapPlaybackThread::configure(const audio_attributes_t *attr,
  8286. audio_stream_type_t streamType,
  8287. audio_session_t sessionId,
  8288. const sp<MmapStreamCallback>& callback,
  8289. audio_port_handle_t deviceId,
  8290. audio_port_handle_t portId)
  8291. {
  8292. MmapThread::configure(attr, streamType, sessionId, callback, deviceId, portId);
  8293. mStreamType = streamType;
  8294. }
  8295. AudioStreamOut* AudioFlinger::MmapPlaybackThread::clearOutput()
  8296. {
  8297. Mutex::Autolock _l(mLock);
  8298. AudioStreamOut *output = mOutput;
  8299. mOutput = NULL;
  8300. return output;
  8301. }
  8302. void AudioFlinger::MmapPlaybackThread::setMasterVolume(float value)
  8303. {
  8304. Mutex::Autolock _l(mLock);
  8305. // Don't apply master volume in SW if our HAL can do it for us.
  8306. if (mAudioHwDev &&
  8307. mAudioHwDev->canSetMasterVolume()) {
  8308. mMasterVolume = 1.0;
  8309. } else {
  8310. mMasterVolume = value;
  8311. }
  8312. }
  8313. void AudioFlinger::MmapPlaybackThread::setMasterMute(bool muted)
  8314. {
  8315. Mutex::Autolock _l(mLock);
  8316. // Don't apply master mute in SW if our HAL can do it for us.
  8317. if (mAudioHwDev && mAudioHwDev->canSetMasterMute()) {
  8318. mMasterMute = false;
  8319. } else {
  8320. mMasterMute = muted;
  8321. }
  8322. }
  8323. void AudioFlinger::MmapPlaybackThread::setStreamVolume(audio_stream_type_t stream, float value)
  8324. {
  8325. Mutex::Autolock _l(mLock);
  8326. if (stream == mStreamType) {
  8327. mStreamVolume = value;
  8328. broadcast_l();
  8329. }
  8330. }
  8331. float AudioFlinger::MmapPlaybackThread::streamVolume(audio_stream_type_t stream) const
  8332. {
  8333. Mutex::Autolock _l(mLock);
  8334. if (stream == mStreamType) {
  8335. return mStreamVolume;
  8336. }
  8337. return 0.0f;
  8338. }
  8339. void AudioFlinger::MmapPlaybackThread::setStreamMute(audio_stream_type_t stream, bool muted)
  8340. {
  8341. Mutex::Autolock _l(mLock);
  8342. if (stream == mStreamType) {
  8343. mStreamMute= muted;
  8344. broadcast_l();
  8345. }
  8346. }
  8347. void AudioFlinger::MmapPlaybackThread::invalidateTracks(audio_stream_type_t streamType)
  8348. {
  8349. Mutex::Autolock _l(mLock);
  8350. if (streamType == mStreamType) {
  8351. for (const sp<MmapTrack> &track : mActiveTracks) {
  8352. track->invalidate();
  8353. }
  8354. broadcast_l();
  8355. }
  8356. }
  8357. void AudioFlinger::MmapPlaybackThread::processVolume_l()
  8358. {
  8359. float volume;
  8360. if (mMasterMute || mStreamMute) {
  8361. volume = 0;
  8362. } else {
  8363. volume = mMasterVolume * mStreamVolume;
  8364. }
  8365. if (volume != mHalVolFloat) {
  8366. // Convert volumes from float to 8.24
  8367. uint32_t vol = (uint32_t)(volume * (1 << 24));
  8368. // Delegate volume control to effect in track effect chain if needed
  8369. // only one effect chain can be present on DirectOutputThread, so if
  8370. // there is one, the track is connected to it
  8371. if (!mEffectChains.isEmpty()) {
  8372. mEffectChains[0]->setVolume_l(&vol, &vol);
  8373. volume = (float)vol / (1 << 24);
  8374. }
  8375. // Try to use HW volume control and fall back to SW control if not implemented
  8376. if (mOutput->stream->setVolume(volume, volume) == NO_ERROR) {
  8377. mHalVolFloat = volume; // HW volume control worked, so update value.
  8378. mNoCallbackWarningCount = 0;
  8379. } else {
  8380. sp<MmapStreamCallback> callback = mCallback.promote();
  8381. if (callback != 0) {
  8382. int channelCount;
  8383. if (isOutput()) {
  8384. channelCount = audio_channel_count_from_out_mask(mChannelMask);
  8385. } else {
  8386. channelCount = audio_channel_count_from_in_mask(mChannelMask);
  8387. }
  8388. Vector<float> values;
  8389. for (int i = 0; i < channelCount; i++) {
  8390. values.add(volume);
  8391. }
  8392. mHalVolFloat = volume; // SW volume control worked, so update value.
  8393. mNoCallbackWarningCount = 0;
  8394. mLock.unlock();
  8395. callback->onVolumeChanged(mChannelMask, values);
  8396. mLock.lock();
  8397. } else {
  8398. if (mNoCallbackWarningCount < kMaxNoCallbackWarnings) {
  8399. ALOGW("Could not set MMAP stream volume: no volume callback!");
  8400. mNoCallbackWarningCount++;
  8401. }
  8402. }
  8403. }
  8404. }
  8405. }
  8406. void AudioFlinger::MmapPlaybackThread::updateMetadata_l()
  8407. {
  8408. if (mOutput == nullptr || mOutput->stream == nullptr ||
  8409. !mActiveTracks.readAndClearHasChanged()) {
  8410. return;
  8411. }
  8412. StreamOutHalInterface::SourceMetadata metadata;
  8413. for (const sp<MmapTrack> &track : mActiveTracks) {
  8414. // No track is invalid as this is called after prepareTrack_l in the same critical section
  8415. metadata.tracks.push_back({
  8416. .usage = track->attributes().usage,
  8417. .content_type = track->attributes().content_type,
  8418. .gain = mHalVolFloat, // TODO: propagate from aaudio pre-mix volume
  8419. });
  8420. }
  8421. mOutput->stream->updateSourceMetadata(metadata);
  8422. }
  8423. void AudioFlinger::MmapPlaybackThread::checkSilentMode_l()
  8424. {
  8425. if (!mMasterMute) {
  8426. char value[PROPERTY_VALUE_MAX];
  8427. if (property_get("ro.audio.silent", value, "0") > 0) {
  8428. char *endptr;
  8429. unsigned long ul = strtoul(value, &endptr, 0);
  8430. if (*endptr == '\0' && ul != 0) {
  8431. ALOGD("Silence is golden");
  8432. // The setprop command will not allow a property to be changed after
  8433. // the first time it is set, so we don't have to worry about un-muting.
  8434. setMasterMute_l(true);
  8435. }
  8436. }
  8437. }
  8438. }
  8439. void AudioFlinger::MmapPlaybackThread::toAudioPortConfig(struct audio_port_config *config)
  8440. {
  8441. MmapThread::toAudioPortConfig(config);
  8442. if (mOutput && mOutput->flags != AUDIO_OUTPUT_FLAG_NONE) {
  8443. config->config_mask |= AUDIO_PORT_CONFIG_FLAGS;
  8444. config->flags.output = mOutput->flags;
  8445. }
  8446. }
  8447. void AudioFlinger::MmapPlaybackThread::dumpInternals_l(int fd, const Vector<String16>& args)
  8448. {
  8449. MmapThread::dumpInternals_l(fd, args);
  8450. dprintf(fd, " Stream type: %d Stream volume: %f HAL volume: %f Stream mute %d\n",
  8451. mStreamType, mStreamVolume, mHalVolFloat, mStreamMute);
  8452. dprintf(fd, " Master volume: %f Master mute %d\n", mMasterVolume, mMasterMute);
  8453. }
  8454. AudioFlinger::MmapCaptureThread::MmapCaptureThread(
  8455. const sp<AudioFlinger>& audioFlinger, audio_io_handle_t id,
  8456. AudioHwDevice *hwDev, AudioStreamIn *input,
  8457. audio_devices_t outDevice, audio_devices_t inDevice, bool systemReady)
  8458. : MmapThread(audioFlinger, id, hwDev, input->stream, outDevice, inDevice, systemReady),
  8459. mInput(input)
  8460. {
  8461. snprintf(mThreadName, kThreadNameLength, "AudioMmapIn_%X", id);
  8462. mChannelCount = audio_channel_count_from_in_mask(mChannelMask);
  8463. }
  8464. status_t AudioFlinger::MmapCaptureThread::exitStandby()
  8465. {
  8466. {
  8467. // mInput might have been cleared by clearInput()
  8468. Mutex::Autolock _l(mLock);
  8469. if (mInput != nullptr && mInput->stream != nullptr) {
  8470. mInput->stream->setGain(1.0f);
  8471. }
  8472. }
  8473. return MmapThread::exitStandby();
  8474. }
  8475. AudioFlinger::AudioStreamIn* AudioFlinger::MmapCaptureThread::clearInput()
  8476. {
  8477. Mutex::Autolock _l(mLock);
  8478. AudioStreamIn *input = mInput;
  8479. mInput = NULL;
  8480. return input;
  8481. }
  8482. void AudioFlinger::MmapCaptureThread::processVolume_l()
  8483. {
  8484. bool changed = false;
  8485. bool silenced = false;
  8486. sp<MmapStreamCallback> callback = mCallback.promote();
  8487. if (callback == 0) {
  8488. if (mNoCallbackWarningCount < kMaxNoCallbackWarnings) {
  8489. ALOGW("Could not set MMAP stream silenced: no onStreamSilenced callback!");
  8490. mNoCallbackWarningCount++;
  8491. }
  8492. }
  8493. // After a change occurred in track silenced state, mute capture in audio DSP if at least one
  8494. // track is silenced and unmute otherwise
  8495. for (size_t i = 0; i < mActiveTracks.size() && !silenced; i++) {
  8496. if (!mActiveTracks[i]->getAndSetSilencedNotified_l()) {
  8497. changed = true;
  8498. silenced = mActiveTracks[i]->isSilenced_l();
  8499. }
  8500. }
  8501. if (changed) {
  8502. mInput->stream->setGain(silenced ? 0.0f: 1.0f);
  8503. }
  8504. }
  8505. void AudioFlinger::MmapCaptureThread::updateMetadata_l()
  8506. {
  8507. if (mInput == nullptr || mInput->stream == nullptr ||
  8508. !mActiveTracks.readAndClearHasChanged()) {
  8509. return;
  8510. }
  8511. StreamInHalInterface::SinkMetadata metadata;
  8512. for (const sp<MmapTrack> &track : mActiveTracks) {
  8513. // No track is invalid as this is called after prepareTrack_l in the same critical section
  8514. metadata.tracks.push_back({
  8515. .source = track->attributes().source,
  8516. .gain = 1, // capture tracks do not have volumes
  8517. });
  8518. }
  8519. mInput->stream->updateSinkMetadata(metadata);
  8520. }
  8521. void AudioFlinger::MmapCaptureThread::setRecordSilenced(uid_t uid, bool silenced)
  8522. {
  8523. Mutex::Autolock _l(mLock);
  8524. for (size_t i = 0; i < mActiveTracks.size() ; i++) {
  8525. if (mActiveTracks[i]->uid() == uid) {
  8526. mActiveTracks[i]->setSilenced_l(silenced);
  8527. broadcast_l();
  8528. }
  8529. }
  8530. }
  8531. void AudioFlinger::MmapCaptureThread::toAudioPortConfig(struct audio_port_config *config)
  8532. {
  8533. MmapThread::toAudioPortConfig(config);
  8534. if (mInput && mInput->flags != AUDIO_INPUT_FLAG_NONE) {
  8535. config->config_mask |= AUDIO_PORT_CONFIG_FLAGS;
  8536. config->flags.input = mInput->flags;
  8537. }
  8538. }
  8539. } // namespace android