MediaPlayerService.cpp 91 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825
  1. /*
  2. **
  3. ** Copyright 2008, 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. // Proxy for media player implementations
  18. //#define LOG_NDEBUG 0
  19. #define LOG_TAG "MediaPlayerService"
  20. #include <utils/Log.h>
  21. #include <sys/types.h>
  22. #include <sys/stat.h>
  23. #include <sys/time.h>
  24. #include <dirent.h>
  25. #include <unistd.h>
  26. #include <string.h>
  27. #include <cutils/atomic.h>
  28. #include <cutils/properties.h> // for property_get
  29. #include <utils/misc.h>
  30. #include <android/hardware/media/omx/1.0/IOmx.h>
  31. #include <android/hardware/media/c2/1.0/IComponentStore.h>
  32. #include <binder/IPCThreadState.h>
  33. #include <binder/IServiceManager.h>
  34. #include <binder/MemoryHeapBase.h>
  35. #include <binder/MemoryBase.h>
  36. #include <gui/Surface.h>
  37. #include <utils/Errors.h> // for status_t
  38. #include <utils/String8.h>
  39. #include <utils/SystemClock.h>
  40. #include <utils/Timers.h>
  41. #include <utils/Vector.h>
  42. #include <codec2/hidl/client.h>
  43. #include <media/IMediaHTTPService.h>
  44. #include <media/IRemoteDisplay.h>
  45. #include <media/IRemoteDisplayClient.h>
  46. #include <media/MediaPlayerInterface.h>
  47. #include <media/mediarecorder.h>
  48. #include <media/MediaMetadataRetrieverInterface.h>
  49. #include <media/Metadata.h>
  50. #include <media/AudioTrack.h>
  51. #include <media/MemoryLeakTrackUtil.h>
  52. #include <media/stagefright/InterfaceUtils.h>
  53. #include <media/stagefright/MediaCodecList.h>
  54. #include <media/stagefright/MediaErrors.h>
  55. #include <media/stagefright/Utils.h>
  56. #include <media/stagefright/foundation/ADebug.h>
  57. #include <media/stagefright/foundation/ALooperRoster.h>
  58. #include <media/stagefright/SurfaceUtils.h>
  59. #include <mediautils/BatteryNotifier.h>
  60. #include <memunreachable/memunreachable.h>
  61. #include <system/audio.h>
  62. #include <private/android_filesystem_config.h>
  63. #include "ActivityManager.h"
  64. #include "MediaRecorderClient.h"
  65. #include "MediaPlayerService.h"
  66. #include "MetadataRetrieverClient.h"
  67. #include "MediaPlayerFactory.h"
  68. #include "TestPlayerStub.h"
  69. #include "nuplayer/NuPlayerDriver.h"
  70. #include "HTTPBase.h"
  71. static const int kDumpLockRetries = 50;
  72. static const int kDumpLockSleepUs = 20000;
  73. namespace {
  74. using android::media::Metadata;
  75. using android::status_t;
  76. using android::OK;
  77. using android::BAD_VALUE;
  78. using android::NOT_ENOUGH_DATA;
  79. using android::Parcel;
  80. using android::media::VolumeShaper;
  81. // Max number of entries in the filter.
  82. const int kMaxFilterSize = 64; // I pulled that out of thin air.
  83. const float kMaxRequiredSpeed = 8.0f; // for PCM tracks allow up to 8x speedup.
  84. // FIXME: Move all the metadata related function in the Metadata.cpp
  85. // Unmarshall a filter from a Parcel.
  86. // Filter format in a parcel:
  87. //
  88. // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
  89. // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  90. // | number of entries (n) |
  91. // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  92. // | metadata type 1 |
  93. // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  94. // | metadata type 2 |
  95. // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  96. // ....
  97. // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  98. // | metadata type n |
  99. // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  100. //
  101. // @param p Parcel that should start with a filter.
  102. // @param[out] filter On exit contains the list of metadata type to be
  103. // filtered.
  104. // @param[out] status On exit contains the status code to be returned.
  105. // @return true if the parcel starts with a valid filter.
  106. bool unmarshallFilter(const Parcel& p,
  107. Metadata::Filter *filter,
  108. status_t *status)
  109. {
  110. int32_t val;
  111. if (p.readInt32(&val) != OK)
  112. {
  113. ALOGE("Failed to read filter's length");
  114. *status = NOT_ENOUGH_DATA;
  115. return false;
  116. }
  117. if( val > kMaxFilterSize || val < 0)
  118. {
  119. ALOGE("Invalid filter len %d", val);
  120. *status = BAD_VALUE;
  121. return false;
  122. }
  123. const size_t num = val;
  124. filter->clear();
  125. filter->setCapacity(num);
  126. size_t size = num * sizeof(Metadata::Type);
  127. if (p.dataAvail() < size)
  128. {
  129. ALOGE("Filter too short expected %zu but got %zu", size, p.dataAvail());
  130. *status = NOT_ENOUGH_DATA;
  131. return false;
  132. }
  133. const Metadata::Type *data =
  134. static_cast<const Metadata::Type*>(p.readInplace(size));
  135. if (NULL == data)
  136. {
  137. ALOGE("Filter had no data");
  138. *status = BAD_VALUE;
  139. return false;
  140. }
  141. // TODO: The stl impl of vector would be more efficient here
  142. // because it degenerates into a memcpy on pod types. Try to
  143. // replace later or use stl::set.
  144. for (size_t i = 0; i < num; ++i)
  145. {
  146. filter->add(*data);
  147. ++data;
  148. }
  149. *status = OK;
  150. return true;
  151. }
  152. // @param filter Of metadata type.
  153. // @param val To be searched.
  154. // @return true if a match was found.
  155. bool findMetadata(const Metadata::Filter& filter, const int32_t val)
  156. {
  157. // Deal with empty and ANY right away
  158. if (filter.isEmpty()) return false;
  159. if (filter[0] == Metadata::kAny) return true;
  160. return filter.indexOf(val) >= 0;
  161. }
  162. } // anonymous namespace
  163. namespace {
  164. using android::Parcel;
  165. using android::String16;
  166. // marshalling tag indicating flattened utf16 tags
  167. // keep in sync with frameworks/base/media/java/android/media/AudioAttributes.java
  168. const int32_t kAudioAttributesMarshallTagFlattenTags = 1;
  169. // Audio attributes format in a parcel:
  170. //
  171. // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
  172. // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  173. // | usage |
  174. // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  175. // | content_type |
  176. // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  177. // | source |
  178. // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  179. // | flags |
  180. // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  181. // | kAudioAttributesMarshallTagFlattenTags | // ignore tags if not found
  182. // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  183. // | flattened tags in UTF16 |
  184. // | ... |
  185. // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  186. //
  187. // @param p Parcel that contains audio attributes.
  188. // @param[out] attributes On exit points to an initialized audio_attributes_t structure
  189. // @param[out] status On exit contains the status code to be returned.
  190. void unmarshallAudioAttributes(const Parcel& parcel, audio_attributes_t *attributes)
  191. {
  192. attributes->usage = (audio_usage_t) parcel.readInt32();
  193. attributes->content_type = (audio_content_type_t) parcel.readInt32();
  194. attributes->source = (audio_source_t) parcel.readInt32();
  195. attributes->flags = (audio_flags_mask_t) parcel.readInt32();
  196. const bool hasFlattenedTag = (parcel.readInt32() == kAudioAttributesMarshallTagFlattenTags);
  197. if (hasFlattenedTag) {
  198. // the tags are UTF16, convert to UTF8
  199. String16 tags = parcel.readString16();
  200. ssize_t realTagSize = utf16_to_utf8_length(tags.string(), tags.size());
  201. if (realTagSize <= 0) {
  202. strcpy(attributes->tags, "");
  203. } else {
  204. // copy the flattened string into the attributes as the destination for the conversion:
  205. // copying array size -1, array for tags was calloc'd, no need to NULL-terminate it
  206. size_t tagSize = realTagSize > AUDIO_ATTRIBUTES_TAGS_MAX_SIZE - 1 ?
  207. AUDIO_ATTRIBUTES_TAGS_MAX_SIZE - 1 : realTagSize;
  208. utf16_to_utf8(tags.string(), tagSize, attributes->tags,
  209. sizeof(attributes->tags) / sizeof(attributes->tags[0]));
  210. }
  211. } else {
  212. ALOGE("unmarshallAudioAttributes() received unflattened tags, ignoring tag values");
  213. strcpy(attributes->tags, "");
  214. }
  215. }
  216. } // anonymous namespace
  217. namespace android {
  218. extern ALooperRoster gLooperRoster;
  219. static bool checkPermission(const char* permissionString) {
  220. if (getpid() == IPCThreadState::self()->getCallingPid()) return true;
  221. bool ok = checkCallingPermission(String16(permissionString));
  222. if (!ok) ALOGE("Request requires %s", permissionString);
  223. return ok;
  224. }
  225. // TODO: Find real cause of Audio/Video delay in PV framework and remove this workaround
  226. /* static */ int MediaPlayerService::AudioOutput::mMinBufferCount = 4;
  227. /* static */ bool MediaPlayerService::AudioOutput::mIsOnEmulator = false;
  228. void MediaPlayerService::instantiate() {
  229. defaultServiceManager()->addService(
  230. String16("media.player"), new MediaPlayerService());
  231. }
  232. MediaPlayerService::MediaPlayerService()
  233. {
  234. ALOGV("MediaPlayerService created");
  235. mNextConnId = 1;
  236. MediaPlayerFactory::registerBuiltinFactories();
  237. }
  238. MediaPlayerService::~MediaPlayerService()
  239. {
  240. ALOGV("MediaPlayerService destroyed");
  241. }
  242. sp<IMediaRecorder> MediaPlayerService::createMediaRecorder(const String16 &opPackageName)
  243. {
  244. pid_t pid = IPCThreadState::self()->getCallingPid();
  245. sp<MediaRecorderClient> recorder = new MediaRecorderClient(this, pid, opPackageName);
  246. wp<MediaRecorderClient> w = recorder;
  247. Mutex::Autolock lock(mLock);
  248. mMediaRecorderClients.add(w);
  249. ALOGV("Create new media recorder client from pid %d", pid);
  250. return recorder;
  251. }
  252. void MediaPlayerService::removeMediaRecorderClient(const wp<MediaRecorderClient>& client)
  253. {
  254. Mutex::Autolock lock(mLock);
  255. mMediaRecorderClients.remove(client);
  256. ALOGV("Delete media recorder client");
  257. }
  258. sp<IMediaMetadataRetriever> MediaPlayerService::createMetadataRetriever()
  259. {
  260. pid_t pid = IPCThreadState::self()->getCallingPid();
  261. sp<MetadataRetrieverClient> retriever = new MetadataRetrieverClient(pid);
  262. ALOGV("Create new media retriever from pid %d", pid);
  263. return retriever;
  264. }
  265. sp<IMediaPlayer> MediaPlayerService::create(const sp<IMediaPlayerClient>& client,
  266. audio_session_t audioSessionId)
  267. {
  268. pid_t pid = IPCThreadState::self()->getCallingPid();
  269. int32_t connId = android_atomic_inc(&mNextConnId);
  270. sp<Client> c = new Client(
  271. this, pid, connId, client, audioSessionId,
  272. IPCThreadState::self()->getCallingUid());
  273. ALOGV("Create new client(%d) from pid %d, uid %d, ", connId, pid,
  274. IPCThreadState::self()->getCallingUid());
  275. wp<Client> w = c;
  276. {
  277. Mutex::Autolock lock(mLock);
  278. mClients.add(w);
  279. }
  280. return c;
  281. }
  282. sp<IMediaCodecList> MediaPlayerService::getCodecList() const {
  283. return MediaCodecList::getLocalInstance();
  284. }
  285. sp<IRemoteDisplay> MediaPlayerService::listenForRemoteDisplay(
  286. const String16 &/*opPackageName*/,
  287. const sp<IRemoteDisplayClient>& /*client*/,
  288. const String8& /*iface*/) {
  289. ALOGE("listenForRemoteDisplay is no longer supported!");
  290. return NULL;
  291. }
  292. status_t MediaPlayerService::AudioOutput::dump(int fd, const Vector<String16>& args) const
  293. {
  294. const size_t SIZE = 256;
  295. char buffer[SIZE];
  296. String8 result;
  297. result.append(" AudioOutput\n");
  298. snprintf(buffer, 255, " stream type(%d), left - right volume(%f, %f)\n",
  299. mStreamType, mLeftVolume, mRightVolume);
  300. result.append(buffer);
  301. snprintf(buffer, 255, " msec per frame(%f), latency (%d)\n",
  302. mMsecsPerFrame, (mTrack != 0) ? mTrack->latency() : -1);
  303. result.append(buffer);
  304. snprintf(buffer, 255, " aux effect id(%d), send level (%f)\n",
  305. mAuxEffectId, mSendLevel);
  306. result.append(buffer);
  307. ::write(fd, result.string(), result.size());
  308. if (mTrack != 0) {
  309. mTrack->dump(fd, args);
  310. }
  311. return NO_ERROR;
  312. }
  313. status_t MediaPlayerService::Client::dump(int fd, const Vector<String16>& args)
  314. {
  315. const size_t SIZE = 256;
  316. char buffer[SIZE];
  317. String8 result;
  318. result.append(" Client\n");
  319. snprintf(buffer, 255, " pid(%d), connId(%d), status(%d), looping(%s)\n",
  320. mPid, mConnId, mStatus, mLoop?"true": "false");
  321. result.append(buffer);
  322. sp<MediaPlayerBase> p;
  323. sp<AudioOutput> audioOutput;
  324. bool locked = false;
  325. for (int i = 0; i < kDumpLockRetries; ++i) {
  326. if (mLock.tryLock() == NO_ERROR) {
  327. locked = true;
  328. break;
  329. }
  330. usleep(kDumpLockSleepUs);
  331. }
  332. if (locked) {
  333. p = mPlayer;
  334. audioOutput = mAudioOutput;
  335. mLock.unlock();
  336. } else {
  337. result.append(" lock is taken, no dump from player and audio output\n");
  338. }
  339. write(fd, result.string(), result.size());
  340. if (p != NULL) {
  341. p->dump(fd, args);
  342. }
  343. if (audioOutput != 0) {
  344. audioOutput->dump(fd, args);
  345. }
  346. write(fd, "\n", 1);
  347. return NO_ERROR;
  348. }
  349. /**
  350. * The only arguments this understands right now are -c, -von and -voff,
  351. * which are parsed by ALooperRoster::dump()
  352. */
  353. status_t MediaPlayerService::dump(int fd, const Vector<String16>& args)
  354. {
  355. const size_t SIZE = 256;
  356. char buffer[SIZE];
  357. String8 result;
  358. SortedVector< sp<Client> > clients; //to serialise the mutex unlock & client destruction.
  359. SortedVector< sp<MediaRecorderClient> > mediaRecorderClients;
  360. if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
  361. snprintf(buffer, SIZE, "Permission Denial: "
  362. "can't dump MediaPlayerService from pid=%d, uid=%d\n",
  363. IPCThreadState::self()->getCallingPid(),
  364. IPCThreadState::self()->getCallingUid());
  365. result.append(buffer);
  366. } else {
  367. Mutex::Autolock lock(mLock);
  368. for (int i = 0, n = mClients.size(); i < n; ++i) {
  369. sp<Client> c = mClients[i].promote();
  370. if (c != 0) c->dump(fd, args);
  371. clients.add(c);
  372. }
  373. if (mMediaRecorderClients.size() == 0) {
  374. result.append(" No media recorder client\n\n");
  375. } else {
  376. for (int i = 0, n = mMediaRecorderClients.size(); i < n; ++i) {
  377. sp<MediaRecorderClient> c = mMediaRecorderClients[i].promote();
  378. if (c != 0) {
  379. snprintf(buffer, 255, " MediaRecorderClient pid(%d)\n", c->mPid);
  380. result.append(buffer);
  381. write(fd, result.string(), result.size());
  382. result = "\n";
  383. c->dump(fd, args);
  384. mediaRecorderClients.add(c);
  385. }
  386. }
  387. }
  388. result.append(" Files opened and/or mapped:\n");
  389. snprintf(buffer, SIZE, "/proc/%d/maps", getpid());
  390. FILE *f = fopen(buffer, "r");
  391. if (f) {
  392. while (!feof(f)) {
  393. fgets(buffer, SIZE, f);
  394. if (strstr(buffer, " /storage/") ||
  395. strstr(buffer, " /system/sounds/") ||
  396. strstr(buffer, " /data/") ||
  397. strstr(buffer, " /system/media/")) {
  398. result.append(" ");
  399. result.append(buffer);
  400. }
  401. }
  402. fclose(f);
  403. } else {
  404. result.append("couldn't open ");
  405. result.append(buffer);
  406. result.append("\n");
  407. }
  408. snprintf(buffer, SIZE, "/proc/%d/fd", getpid());
  409. DIR *d = opendir(buffer);
  410. if (d) {
  411. struct dirent *ent;
  412. while((ent = readdir(d)) != NULL) {
  413. if (strcmp(ent->d_name,".") && strcmp(ent->d_name,"..")) {
  414. snprintf(buffer, SIZE, "/proc/%d/fd/%s", getpid(), ent->d_name);
  415. struct stat s;
  416. if (lstat(buffer, &s) == 0) {
  417. if ((s.st_mode & S_IFMT) == S_IFLNK) {
  418. char linkto[256];
  419. int len = readlink(buffer, linkto, sizeof(linkto));
  420. if(len > 0) {
  421. if(len > 255) {
  422. linkto[252] = '.';
  423. linkto[253] = '.';
  424. linkto[254] = '.';
  425. linkto[255] = 0;
  426. } else {
  427. linkto[len] = 0;
  428. }
  429. if (strstr(linkto, "/storage/") == linkto ||
  430. strstr(linkto, "/system/sounds/") == linkto ||
  431. strstr(linkto, "/data/") == linkto ||
  432. strstr(linkto, "/system/media/") == linkto) {
  433. result.append(" ");
  434. result.append(buffer);
  435. result.append(" -> ");
  436. result.append(linkto);
  437. result.append("\n");
  438. }
  439. }
  440. } else {
  441. result.append(" unexpected type for ");
  442. result.append(buffer);
  443. result.append("\n");
  444. }
  445. }
  446. }
  447. }
  448. closedir(d);
  449. } else {
  450. result.append("couldn't open ");
  451. result.append(buffer);
  452. result.append("\n");
  453. }
  454. gLooperRoster.dump(fd, args);
  455. bool dumpMem = false;
  456. bool unreachableMemory = false;
  457. for (size_t i = 0; i < args.size(); i++) {
  458. if (args[i] == String16("-m")) {
  459. dumpMem = true;
  460. } else if (args[i] == String16("--unreachable")) {
  461. unreachableMemory = true;
  462. }
  463. }
  464. if (dumpMem) {
  465. result.append("\nDumping memory:\n");
  466. std::string s = dumpMemoryAddresses(100 /* limit */);
  467. result.append(s.c_str(), s.size());
  468. }
  469. if (unreachableMemory) {
  470. result.append("\nDumping unreachable memory:\n");
  471. // TODO - should limit be an argument parameter?
  472. std::string s = GetUnreachableMemoryString(true /* contents */, 10000 /* limit */);
  473. result.append(s.c_str(), s.size());
  474. }
  475. }
  476. write(fd, result.string(), result.size());
  477. return NO_ERROR;
  478. }
  479. void MediaPlayerService::removeClient(const wp<Client>& client)
  480. {
  481. Mutex::Autolock lock(mLock);
  482. mClients.remove(client);
  483. }
  484. bool MediaPlayerService::hasClient(wp<Client> client)
  485. {
  486. Mutex::Autolock lock(mLock);
  487. return mClients.indexOf(client) != NAME_NOT_FOUND;
  488. }
  489. MediaPlayerService::Client::Client(
  490. const sp<MediaPlayerService>& service, pid_t pid,
  491. int32_t connId, const sp<IMediaPlayerClient>& client,
  492. audio_session_t audioSessionId, uid_t uid)
  493. {
  494. ALOGV("Client(%d) constructor", connId);
  495. mPid = pid;
  496. mConnId = connId;
  497. mService = service;
  498. mClient = client;
  499. mLoop = false;
  500. mStatus = NO_INIT;
  501. mAudioSessionId = audioSessionId;
  502. mUid = uid;
  503. mRetransmitEndpointValid = false;
  504. mAudioAttributes = NULL;
  505. mListener = new Listener(this);
  506. #if CALLBACK_ANTAGONIZER
  507. ALOGD("create Antagonizer");
  508. mAntagonizer = new Antagonizer(mListener);
  509. #endif
  510. }
  511. MediaPlayerService::Client::~Client()
  512. {
  513. ALOGV("Client(%d) destructor pid = %d", mConnId, mPid);
  514. mAudioOutput.clear();
  515. wp<Client> client(this);
  516. disconnect();
  517. mService->removeClient(client);
  518. if (mAudioAttributes != NULL) {
  519. free(mAudioAttributes);
  520. }
  521. mAudioDeviceUpdatedListener.clear();
  522. }
  523. void MediaPlayerService::Client::disconnect()
  524. {
  525. ALOGV("disconnect(%d) from pid %d", mConnId, mPid);
  526. // grab local reference and clear main reference to prevent future
  527. // access to object
  528. sp<MediaPlayerBase> p;
  529. {
  530. Mutex::Autolock l(mLock);
  531. p = mPlayer;
  532. mClient.clear();
  533. mPlayer.clear();
  534. }
  535. // clear the notification to prevent callbacks to dead client
  536. // and reset the player. We assume the player will serialize
  537. // access to itself if necessary.
  538. if (p != 0) {
  539. p->setNotifyCallback(0);
  540. #if CALLBACK_ANTAGONIZER
  541. ALOGD("kill Antagonizer");
  542. mAntagonizer->kill();
  543. #endif
  544. p->reset();
  545. }
  546. {
  547. Mutex::Autolock l(mLock);
  548. disconnectNativeWindow_l();
  549. }
  550. IPCThreadState::self()->flushCommands();
  551. }
  552. sp<MediaPlayerBase> MediaPlayerService::Client::createPlayer(player_type playerType)
  553. {
  554. // determine if we have the right player type
  555. sp<MediaPlayerBase> p = getPlayer();
  556. if ((p != NULL) && (p->playerType() != playerType)) {
  557. ALOGV("delete player");
  558. p.clear();
  559. }
  560. if (p == NULL) {
  561. p = MediaPlayerFactory::createPlayer(playerType, mListener, mPid);
  562. }
  563. if (p != NULL) {
  564. p->setUID(mUid);
  565. }
  566. return p;
  567. }
  568. void MediaPlayerService::Client::AudioDeviceUpdatedNotifier::onAudioDeviceUpdate(
  569. audio_io_handle_t audioIo,
  570. audio_port_handle_t deviceId) {
  571. sp<MediaPlayerBase> listener = mListener.promote();
  572. if (listener != NULL) {
  573. listener->sendEvent(MEDIA_AUDIO_ROUTING_CHANGED, audioIo, deviceId);
  574. } else {
  575. ALOGW("listener for process %d death is gone", MEDIA_AUDIO_ROUTING_CHANGED);
  576. }
  577. }
  578. sp<MediaPlayerBase> MediaPlayerService::Client::setDataSource_pre(
  579. player_type playerType)
  580. {
  581. ALOGV("player type = %d", playerType);
  582. // create the right type of player
  583. sp<MediaPlayerBase> p = createPlayer(playerType);
  584. if (p == NULL) {
  585. return p;
  586. }
  587. std::vector<DeathNotifier> deathNotifiers;
  588. // Listen to death of media.extractor service
  589. sp<IServiceManager> sm = initdefaultServiceManager();
  590. sp<IBinder> binder = sm->getService(String16("media.extractor"));
  591. if (binder == NULL) {
  592. ALOGE("extractor service not available");
  593. return NULL;
  594. }
  595. deathNotifiers.emplace_back(
  596. binder, [l = wp<MediaPlayerBase>(p)]() {
  597. sp<MediaPlayerBase> listener = l.promote();
  598. if (listener) {
  599. ALOGI("media.extractor died. Sending death notification.");
  600. listener->sendEvent(MEDIA_ERROR, MEDIA_ERROR_SERVER_DIED,
  601. MEDIAEXTRACTOR_PROCESS_DEATH);
  602. } else {
  603. ALOGW("media.extractor died without a death handler.");
  604. }
  605. });
  606. {
  607. using ::android::hidl::base::V1_0::IBase;
  608. // Listen to death of OMX service
  609. {
  610. sp<IBase> base = ::android::hardware::media::omx::V1_0::
  611. IOmx::getService();
  612. if (base == nullptr) {
  613. ALOGD("OMX service is not available");
  614. } else {
  615. deathNotifiers.emplace_back(
  616. base, [l = wp<MediaPlayerBase>(p)]() {
  617. sp<MediaPlayerBase> listener = l.promote();
  618. if (listener) {
  619. ALOGI("OMX service died. "
  620. "Sending death notification.");
  621. listener->sendEvent(
  622. MEDIA_ERROR, MEDIA_ERROR_SERVER_DIED,
  623. MEDIACODEC_PROCESS_DEATH);
  624. } else {
  625. ALOGW("OMX service died without a death handler.");
  626. }
  627. });
  628. }
  629. }
  630. // Listen to death of Codec2 services
  631. {
  632. for (std::shared_ptr<Codec2Client> const& client :
  633. Codec2Client::CreateFromAllServices()) {
  634. sp<IBase> base = client->getBase();
  635. deathNotifiers.emplace_back(
  636. base, [l = wp<MediaPlayerBase>(p),
  637. name = std::string(client->getServiceName())]() {
  638. sp<MediaPlayerBase> listener = l.promote();
  639. if (listener) {
  640. ALOGI("Codec2 service \"%s\" died. "
  641. "Sending death notification.",
  642. name.c_str());
  643. listener->sendEvent(
  644. MEDIA_ERROR, MEDIA_ERROR_SERVER_DIED,
  645. MEDIACODEC_PROCESS_DEATH);
  646. } else {
  647. ALOGW("Codec2 service \"%s\" died "
  648. "without a death handler.",
  649. name.c_str());
  650. }
  651. });
  652. }
  653. }
  654. }
  655. Mutex::Autolock lock(mLock);
  656. mDeathNotifiers.clear();
  657. mDeathNotifiers.swap(deathNotifiers);
  658. mAudioDeviceUpdatedListener = new AudioDeviceUpdatedNotifier(p);
  659. if (!p->hardwareOutput()) {
  660. mAudioOutput = new AudioOutput(mAudioSessionId, IPCThreadState::self()->getCallingUid(),
  661. mPid, mAudioAttributes, mAudioDeviceUpdatedListener);
  662. static_cast<MediaPlayerInterface*>(p.get())->setAudioSink(mAudioOutput);
  663. }
  664. return p;
  665. }
  666. status_t MediaPlayerService::Client::setDataSource_post(
  667. const sp<MediaPlayerBase>& p,
  668. status_t status)
  669. {
  670. ALOGV(" setDataSource");
  671. if (status != OK) {
  672. ALOGE(" error: %d", status);
  673. return status;
  674. }
  675. // Set the re-transmission endpoint if one was chosen.
  676. if (mRetransmitEndpointValid) {
  677. status = p->setRetransmitEndpoint(&mRetransmitEndpoint);
  678. if (status != NO_ERROR) {
  679. ALOGE("setRetransmitEndpoint error: %d", status);
  680. }
  681. }
  682. if (status == OK) {
  683. Mutex::Autolock lock(mLock);
  684. mPlayer = p;
  685. }
  686. return status;
  687. }
  688. status_t MediaPlayerService::Client::setDataSource(
  689. const sp<IMediaHTTPService> &httpService,
  690. const char *url,
  691. const KeyedVector<String8, String8> *headers)
  692. {
  693. ALOGV("setDataSource(%s)", url);
  694. if (url == NULL)
  695. return UNKNOWN_ERROR;
  696. if ((strncmp(url, "http://", 7) == 0) ||
  697. (strncmp(url, "https://", 8) == 0) ||
  698. (strncmp(url, "rtsp://", 7) == 0)) {
  699. if (!checkPermission("android.permission.INTERNET")) {
  700. return PERMISSION_DENIED;
  701. }
  702. }
  703. if (strncmp(url, "content://", 10) == 0) {
  704. // get a filedescriptor for the content Uri and
  705. // pass it to the setDataSource(fd) method
  706. String16 url16(url);
  707. int fd = android::openContentProviderFile(url16);
  708. if (fd < 0)
  709. {
  710. ALOGE("Couldn't open fd for %s", url);
  711. return UNKNOWN_ERROR;
  712. }
  713. status_t status = setDataSource(fd, 0, 0x7fffffffffLL); // this sets mStatus
  714. close(fd);
  715. return mStatus = status;
  716. } else {
  717. player_type playerType = MediaPlayerFactory::getPlayerType(this, url);
  718. sp<MediaPlayerBase> p = setDataSource_pre(playerType);
  719. if (p == NULL) {
  720. return NO_INIT;
  721. }
  722. return mStatus =
  723. setDataSource_post(
  724. p, p->setDataSource(httpService, url, headers));
  725. }
  726. }
  727. status_t MediaPlayerService::Client::setDataSource(int fd, int64_t offset, int64_t length)
  728. {
  729. ALOGV("setDataSource fd=%d (%s), offset=%lld, length=%lld",
  730. fd, nameForFd(fd).c_str(), (long long) offset, (long long) length);
  731. struct stat sb;
  732. int ret = fstat(fd, &sb);
  733. if (ret != 0) {
  734. ALOGE("fstat(%d) failed: %d, %s", fd, ret, strerror(errno));
  735. return UNKNOWN_ERROR;
  736. }
  737. ALOGV("st_dev = %llu", static_cast<unsigned long long>(sb.st_dev));
  738. ALOGV("st_mode = %u", sb.st_mode);
  739. ALOGV("st_uid = %lu", static_cast<unsigned long>(sb.st_uid));
  740. ALOGV("st_gid = %lu", static_cast<unsigned long>(sb.st_gid));
  741. ALOGV("st_size = %llu", static_cast<unsigned long long>(sb.st_size));
  742. if (offset >= sb.st_size) {
  743. ALOGE("offset error");
  744. return UNKNOWN_ERROR;
  745. }
  746. if (offset + length > sb.st_size) {
  747. length = sb.st_size - offset;
  748. ALOGV("calculated length = %lld", (long long)length);
  749. }
  750. player_type playerType = MediaPlayerFactory::getPlayerType(this,
  751. fd,
  752. offset,
  753. length);
  754. sp<MediaPlayerBase> p = setDataSource_pre(playerType);
  755. if (p == NULL) {
  756. return NO_INIT;
  757. }
  758. // now set data source
  759. return mStatus = setDataSource_post(p, p->setDataSource(fd, offset, length));
  760. }
  761. status_t MediaPlayerService::Client::setDataSource(
  762. const sp<IStreamSource> &source) {
  763. // create the right type of player
  764. player_type playerType = MediaPlayerFactory::getPlayerType(this, source);
  765. sp<MediaPlayerBase> p = setDataSource_pre(playerType);
  766. if (p == NULL) {
  767. return NO_INIT;
  768. }
  769. // now set data source
  770. return mStatus = setDataSource_post(p, p->setDataSource(source));
  771. }
  772. status_t MediaPlayerService::Client::setDataSource(
  773. const sp<IDataSource> &source) {
  774. sp<DataSource> dataSource = CreateDataSourceFromIDataSource(source);
  775. player_type playerType = MediaPlayerFactory::getPlayerType(this, dataSource);
  776. sp<MediaPlayerBase> p = setDataSource_pre(playerType);
  777. if (p == NULL) {
  778. return NO_INIT;
  779. }
  780. // now set data source
  781. return mStatus = setDataSource_post(p, p->setDataSource(dataSource));
  782. }
  783. void MediaPlayerService::Client::disconnectNativeWindow_l() {
  784. if (mConnectedWindow != NULL) {
  785. status_t err = nativeWindowDisconnect(
  786. mConnectedWindow.get(), "disconnectNativeWindow");
  787. if (err != OK) {
  788. ALOGW("nativeWindowDisconnect returned an error: %s (%d)",
  789. strerror(-err), err);
  790. }
  791. }
  792. mConnectedWindow.clear();
  793. }
  794. status_t MediaPlayerService::Client::setVideoSurfaceTexture(
  795. const sp<IGraphicBufferProducer>& bufferProducer)
  796. {
  797. ALOGV("[%d] setVideoSurfaceTexture(%p)", mConnId, bufferProducer.get());
  798. sp<MediaPlayerBase> p = getPlayer();
  799. if (p == 0) return UNKNOWN_ERROR;
  800. sp<IBinder> binder(IInterface::asBinder(bufferProducer));
  801. if (mConnectedWindowBinder == binder) {
  802. return OK;
  803. }
  804. sp<ANativeWindow> anw;
  805. if (bufferProducer != NULL) {
  806. anw = new Surface(bufferProducer, true /* controlledByApp */);
  807. status_t err = nativeWindowConnect(anw.get(), "setVideoSurfaceTexture");
  808. if (err != OK) {
  809. ALOGE("setVideoSurfaceTexture failed: %d", err);
  810. // Note that we must do the reset before disconnecting from the ANW.
  811. // Otherwise queue/dequeue calls could be made on the disconnected
  812. // ANW, which may result in errors.
  813. reset();
  814. Mutex::Autolock lock(mLock);
  815. disconnectNativeWindow_l();
  816. return err;
  817. }
  818. }
  819. // Note that we must set the player's new GraphicBufferProducer before
  820. // disconnecting the old one. Otherwise queue/dequeue calls could be made
  821. // on the disconnected ANW, which may result in errors.
  822. status_t err = p->setVideoSurfaceTexture(bufferProducer);
  823. mLock.lock();
  824. disconnectNativeWindow_l();
  825. if (err == OK) {
  826. mConnectedWindow = anw;
  827. mConnectedWindowBinder = binder;
  828. mLock.unlock();
  829. } else {
  830. mLock.unlock();
  831. status_t err = nativeWindowDisconnect(
  832. anw.get(), "disconnectNativeWindow");
  833. if (err != OK) {
  834. ALOGW("nativeWindowDisconnect returned an error: %s (%d)",
  835. strerror(-err), err);
  836. }
  837. }
  838. return err;
  839. }
  840. status_t MediaPlayerService::Client::invoke(const Parcel& request,
  841. Parcel *reply)
  842. {
  843. sp<MediaPlayerBase> p = getPlayer();
  844. if (p == NULL) return UNKNOWN_ERROR;
  845. return p->invoke(request, reply);
  846. }
  847. // This call doesn't need to access the native player.
  848. status_t MediaPlayerService::Client::setMetadataFilter(const Parcel& filter)
  849. {
  850. status_t status;
  851. media::Metadata::Filter allow, drop;
  852. if (unmarshallFilter(filter, &allow, &status) &&
  853. unmarshallFilter(filter, &drop, &status)) {
  854. Mutex::Autolock lock(mLock);
  855. mMetadataAllow = allow;
  856. mMetadataDrop = drop;
  857. }
  858. return status;
  859. }
  860. status_t MediaPlayerService::Client::getMetadata(
  861. bool update_only, bool /*apply_filter*/, Parcel *reply)
  862. {
  863. sp<MediaPlayerBase> player = getPlayer();
  864. if (player == 0) return UNKNOWN_ERROR;
  865. status_t status;
  866. // Placeholder for the return code, updated by the caller.
  867. reply->writeInt32(-1);
  868. media::Metadata::Filter ids;
  869. // We don't block notifications while we fetch the data. We clear
  870. // mMetadataUpdated first so we don't lose notifications happening
  871. // during the rest of this call.
  872. {
  873. Mutex::Autolock lock(mLock);
  874. if (update_only) {
  875. ids = mMetadataUpdated;
  876. }
  877. mMetadataUpdated.clear();
  878. }
  879. media::Metadata metadata(reply);
  880. metadata.appendHeader();
  881. status = player->getMetadata(ids, reply);
  882. if (status != OK) {
  883. metadata.resetParcel();
  884. ALOGE("getMetadata failed %d", status);
  885. return status;
  886. }
  887. // FIXME: Implement filtering on the result. Not critical since
  888. // filtering takes place on the update notifications already. This
  889. // would be when all the metadata are fetch and a filter is set.
  890. // Everything is fine, update the metadata length.
  891. metadata.updateLength();
  892. return OK;
  893. }
  894. status_t MediaPlayerService::Client::setBufferingSettings(
  895. const BufferingSettings& buffering)
  896. {
  897. ALOGV("[%d] setBufferingSettings{%s}",
  898. mConnId, buffering.toString().string());
  899. sp<MediaPlayerBase> p = getPlayer();
  900. if (p == 0) return UNKNOWN_ERROR;
  901. return p->setBufferingSettings(buffering);
  902. }
  903. status_t MediaPlayerService::Client::getBufferingSettings(
  904. BufferingSettings* buffering /* nonnull */)
  905. {
  906. sp<MediaPlayerBase> p = getPlayer();
  907. // TODO: create mPlayer on demand.
  908. if (p == 0) return UNKNOWN_ERROR;
  909. status_t ret = p->getBufferingSettings(buffering);
  910. if (ret == NO_ERROR) {
  911. ALOGV("[%d] getBufferingSettings{%s}",
  912. mConnId, buffering->toString().string());
  913. } else {
  914. ALOGE("[%d] getBufferingSettings returned %d", mConnId, ret);
  915. }
  916. return ret;
  917. }
  918. status_t MediaPlayerService::Client::prepareAsync()
  919. {
  920. ALOGV("[%d] prepareAsync", mConnId);
  921. sp<MediaPlayerBase> p = getPlayer();
  922. if (p == 0) return UNKNOWN_ERROR;
  923. status_t ret = p->prepareAsync();
  924. #if CALLBACK_ANTAGONIZER
  925. ALOGD("start Antagonizer");
  926. if (ret == NO_ERROR) mAntagonizer->start();
  927. #endif
  928. return ret;
  929. }
  930. status_t MediaPlayerService::Client::start()
  931. {
  932. ALOGV("[%d] start", mConnId);
  933. sp<MediaPlayerBase> p = getPlayer();
  934. if (p == 0) return UNKNOWN_ERROR;
  935. p->setLooping(mLoop);
  936. return p->start();
  937. }
  938. status_t MediaPlayerService::Client::stop()
  939. {
  940. ALOGV("[%d] stop", mConnId);
  941. sp<MediaPlayerBase> p = getPlayer();
  942. if (p == 0) return UNKNOWN_ERROR;
  943. return p->stop();
  944. }
  945. status_t MediaPlayerService::Client::pause()
  946. {
  947. ALOGV("[%d] pause", mConnId);
  948. sp<MediaPlayerBase> p = getPlayer();
  949. if (p == 0) return UNKNOWN_ERROR;
  950. return p->pause();
  951. }
  952. status_t MediaPlayerService::Client::isPlaying(bool* state)
  953. {
  954. *state = false;
  955. sp<MediaPlayerBase> p = getPlayer();
  956. if (p == 0) return UNKNOWN_ERROR;
  957. *state = p->isPlaying();
  958. ALOGV("[%d] isPlaying: %d", mConnId, *state);
  959. return NO_ERROR;
  960. }
  961. status_t MediaPlayerService::Client::setPlaybackSettings(const AudioPlaybackRate& rate)
  962. {
  963. ALOGV("[%d] setPlaybackSettings(%f, %f, %d, %d)",
  964. mConnId, rate.mSpeed, rate.mPitch, rate.mFallbackMode, rate.mStretchMode);
  965. sp<MediaPlayerBase> p = getPlayer();
  966. if (p == 0) return UNKNOWN_ERROR;
  967. return p->setPlaybackSettings(rate);
  968. }
  969. status_t MediaPlayerService::Client::getPlaybackSettings(AudioPlaybackRate* rate /* nonnull */)
  970. {
  971. sp<MediaPlayerBase> p = getPlayer();
  972. if (p == 0) return UNKNOWN_ERROR;
  973. status_t ret = p->getPlaybackSettings(rate);
  974. if (ret == NO_ERROR) {
  975. ALOGV("[%d] getPlaybackSettings(%f, %f, %d, %d)",
  976. mConnId, rate->mSpeed, rate->mPitch, rate->mFallbackMode, rate->mStretchMode);
  977. } else {
  978. ALOGV("[%d] getPlaybackSettings returned %d", mConnId, ret);
  979. }
  980. return ret;
  981. }
  982. status_t MediaPlayerService::Client::setSyncSettings(
  983. const AVSyncSettings& sync, float videoFpsHint)
  984. {
  985. ALOGV("[%d] setSyncSettings(%u, %u, %f, %f)",
  986. mConnId, sync.mSource, sync.mAudioAdjustMode, sync.mTolerance, videoFpsHint);
  987. sp<MediaPlayerBase> p = getPlayer();
  988. if (p == 0) return UNKNOWN_ERROR;
  989. return p->setSyncSettings(sync, videoFpsHint);
  990. }
  991. status_t MediaPlayerService::Client::getSyncSettings(
  992. AVSyncSettings* sync /* nonnull */, float* videoFps /* nonnull */)
  993. {
  994. sp<MediaPlayerBase> p = getPlayer();
  995. if (p == 0) return UNKNOWN_ERROR;
  996. status_t ret = p->getSyncSettings(sync, videoFps);
  997. if (ret == NO_ERROR) {
  998. ALOGV("[%d] getSyncSettings(%u, %u, %f, %f)",
  999. mConnId, sync->mSource, sync->mAudioAdjustMode, sync->mTolerance, *videoFps);
  1000. } else {
  1001. ALOGV("[%d] getSyncSettings returned %d", mConnId, ret);
  1002. }
  1003. return ret;
  1004. }
  1005. status_t MediaPlayerService::Client::getCurrentPosition(int *msec)
  1006. {
  1007. ALOGV("getCurrentPosition");
  1008. sp<MediaPlayerBase> p = getPlayer();
  1009. if (p == 0) return UNKNOWN_ERROR;
  1010. status_t ret = p->getCurrentPosition(msec);
  1011. if (ret == NO_ERROR) {
  1012. ALOGV("[%d] getCurrentPosition = %d", mConnId, *msec);
  1013. } else {
  1014. ALOGE("getCurrentPosition returned %d", ret);
  1015. }
  1016. return ret;
  1017. }
  1018. status_t MediaPlayerService::Client::getDuration(int *msec)
  1019. {
  1020. ALOGV("getDuration");
  1021. sp<MediaPlayerBase> p = getPlayer();
  1022. if (p == 0) return UNKNOWN_ERROR;
  1023. status_t ret = p->getDuration(msec);
  1024. if (ret == NO_ERROR) {
  1025. ALOGV("[%d] getDuration = %d", mConnId, *msec);
  1026. } else {
  1027. ALOGE("getDuration returned %d", ret);
  1028. }
  1029. return ret;
  1030. }
  1031. status_t MediaPlayerService::Client::setNextPlayer(const sp<IMediaPlayer>& player) {
  1032. ALOGV("setNextPlayer");
  1033. Mutex::Autolock l(mLock);
  1034. sp<Client> c = static_cast<Client*>(player.get());
  1035. if (c != NULL && !mService->hasClient(c)) {
  1036. return BAD_VALUE;
  1037. }
  1038. mNextClient = c;
  1039. if (c != NULL) {
  1040. if (mAudioOutput != NULL) {
  1041. mAudioOutput->setNextOutput(c->mAudioOutput);
  1042. } else if ((mPlayer != NULL) && !mPlayer->hardwareOutput()) {
  1043. ALOGE("no current audio output");
  1044. }
  1045. if ((mPlayer != NULL) && (mNextClient->getPlayer() != NULL)) {
  1046. mPlayer->setNextPlayer(mNextClient->getPlayer());
  1047. }
  1048. }
  1049. return OK;
  1050. }
  1051. VolumeShaper::Status MediaPlayerService::Client::applyVolumeShaper(
  1052. const sp<VolumeShaper::Configuration>& configuration,
  1053. const sp<VolumeShaper::Operation>& operation) {
  1054. // for hardware output, call player instead
  1055. ALOGV("Client::applyVolumeShaper(%p)", this);
  1056. sp<MediaPlayerBase> p = getPlayer();
  1057. {
  1058. Mutex::Autolock l(mLock);
  1059. if (p != 0 && p->hardwareOutput()) {
  1060. // TODO: investigate internal implementation
  1061. return VolumeShaper::Status(INVALID_OPERATION);
  1062. }
  1063. if (mAudioOutput.get() != nullptr) {
  1064. return mAudioOutput->applyVolumeShaper(configuration, operation);
  1065. }
  1066. }
  1067. return VolumeShaper::Status(INVALID_OPERATION);
  1068. }
  1069. sp<VolumeShaper::State> MediaPlayerService::Client::getVolumeShaperState(int id) {
  1070. // for hardware output, call player instead
  1071. ALOGV("Client::getVolumeShaperState(%p)", this);
  1072. sp<MediaPlayerBase> p = getPlayer();
  1073. {
  1074. Mutex::Autolock l(mLock);
  1075. if (p != 0 && p->hardwareOutput()) {
  1076. // TODO: investigate internal implementation.
  1077. return nullptr;
  1078. }
  1079. if (mAudioOutput.get() != nullptr) {
  1080. return mAudioOutput->getVolumeShaperState(id);
  1081. }
  1082. }
  1083. return nullptr;
  1084. }
  1085. status_t MediaPlayerService::Client::seekTo(int msec, MediaPlayerSeekMode mode)
  1086. {
  1087. ALOGV("[%d] seekTo(%d, %d)", mConnId, msec, mode);
  1088. sp<MediaPlayerBase> p = getPlayer();
  1089. if (p == 0) return UNKNOWN_ERROR;
  1090. return p->seekTo(msec, mode);
  1091. }
  1092. status_t MediaPlayerService::Client::reset()
  1093. {
  1094. ALOGV("[%d] reset", mConnId);
  1095. mRetransmitEndpointValid = false;
  1096. sp<MediaPlayerBase> p = getPlayer();
  1097. if (p == 0) return UNKNOWN_ERROR;
  1098. return p->reset();
  1099. }
  1100. status_t MediaPlayerService::Client::notifyAt(int64_t mediaTimeUs)
  1101. {
  1102. ALOGV("[%d] notifyAt(%lld)", mConnId, (long long)mediaTimeUs);
  1103. sp<MediaPlayerBase> p = getPlayer();
  1104. if (p == 0) return UNKNOWN_ERROR;
  1105. return p->notifyAt(mediaTimeUs);
  1106. }
  1107. status_t MediaPlayerService::Client::setAudioStreamType(audio_stream_type_t type)
  1108. {
  1109. ALOGV("[%d] setAudioStreamType(%d)", mConnId, type);
  1110. // TODO: for hardware output, call player instead
  1111. Mutex::Autolock l(mLock);
  1112. if (mAudioOutput != 0) mAudioOutput->setAudioStreamType(type);
  1113. return NO_ERROR;
  1114. }
  1115. status_t MediaPlayerService::Client::setAudioAttributes_l(const Parcel &parcel)
  1116. {
  1117. if (mAudioAttributes != NULL) { free(mAudioAttributes); }
  1118. mAudioAttributes = (audio_attributes_t *) calloc(1, sizeof(audio_attributes_t));
  1119. if (mAudioAttributes == NULL) {
  1120. return NO_MEMORY;
  1121. }
  1122. unmarshallAudioAttributes(parcel, mAudioAttributes);
  1123. ALOGV("setAudioAttributes_l() usage=%d content=%d flags=0x%x tags=%s",
  1124. mAudioAttributes->usage, mAudioAttributes->content_type, mAudioAttributes->flags,
  1125. mAudioAttributes->tags);
  1126. if (mAudioOutput != 0) {
  1127. mAudioOutput->setAudioAttributes(mAudioAttributes);
  1128. }
  1129. return NO_ERROR;
  1130. }
  1131. status_t MediaPlayerService::Client::setLooping(int loop)
  1132. {
  1133. ALOGV("[%d] setLooping(%d)", mConnId, loop);
  1134. mLoop = loop;
  1135. sp<MediaPlayerBase> p = getPlayer();
  1136. if (p != 0) return p->setLooping(loop);
  1137. return NO_ERROR;
  1138. }
  1139. status_t MediaPlayerService::Client::setVolume(float leftVolume, float rightVolume)
  1140. {
  1141. ALOGV("[%d] setVolume(%f, %f)", mConnId, leftVolume, rightVolume);
  1142. // for hardware output, call player instead
  1143. sp<MediaPlayerBase> p = getPlayer();
  1144. {
  1145. Mutex::Autolock l(mLock);
  1146. if (p != 0 && p->hardwareOutput()) {
  1147. MediaPlayerHWInterface* hwp =
  1148. reinterpret_cast<MediaPlayerHWInterface*>(p.get());
  1149. return hwp->setVolume(leftVolume, rightVolume);
  1150. } else {
  1151. if (mAudioOutput != 0) mAudioOutput->setVolume(leftVolume, rightVolume);
  1152. return NO_ERROR;
  1153. }
  1154. }
  1155. return NO_ERROR;
  1156. }
  1157. status_t MediaPlayerService::Client::setAuxEffectSendLevel(float level)
  1158. {
  1159. ALOGV("[%d] setAuxEffectSendLevel(%f)", mConnId, level);
  1160. Mutex::Autolock l(mLock);
  1161. if (mAudioOutput != 0) return mAudioOutput->setAuxEffectSendLevel(level);
  1162. return NO_ERROR;
  1163. }
  1164. status_t MediaPlayerService::Client::attachAuxEffect(int effectId)
  1165. {
  1166. ALOGV("[%d] attachAuxEffect(%d)", mConnId, effectId);
  1167. Mutex::Autolock l(mLock);
  1168. if (mAudioOutput != 0) return mAudioOutput->attachAuxEffect(effectId);
  1169. return NO_ERROR;
  1170. }
  1171. status_t MediaPlayerService::Client::setParameter(int key, const Parcel &request) {
  1172. ALOGV("[%d] setParameter(%d)", mConnId, key);
  1173. switch (key) {
  1174. case KEY_PARAMETER_AUDIO_ATTRIBUTES:
  1175. {
  1176. Mutex::Autolock l(mLock);
  1177. return setAudioAttributes_l(request);
  1178. }
  1179. default:
  1180. sp<MediaPlayerBase> p = getPlayer();
  1181. if (p == 0) { return UNKNOWN_ERROR; }
  1182. return p->setParameter(key, request);
  1183. }
  1184. }
  1185. status_t MediaPlayerService::Client::getParameter(int key, Parcel *reply) {
  1186. ALOGV("[%d] getParameter(%d)", mConnId, key);
  1187. sp<MediaPlayerBase> p = getPlayer();
  1188. if (p == 0) return UNKNOWN_ERROR;
  1189. return p->getParameter(key, reply);
  1190. }
  1191. status_t MediaPlayerService::Client::setRetransmitEndpoint(
  1192. const struct sockaddr_in* endpoint) {
  1193. if (NULL != endpoint) {
  1194. uint32_t a = ntohl(endpoint->sin_addr.s_addr);
  1195. uint16_t p = ntohs(endpoint->sin_port);
  1196. ALOGV("[%d] setRetransmitEndpoint(%u.%u.%u.%u:%hu)", mConnId,
  1197. (a >> 24), (a >> 16) & 0xFF, (a >> 8) & 0xFF, (a & 0xFF), p);
  1198. } else {
  1199. ALOGV("[%d] setRetransmitEndpoint = <none>", mConnId);
  1200. }
  1201. sp<MediaPlayerBase> p = getPlayer();
  1202. // Right now, the only valid time to set a retransmit endpoint is before
  1203. // player selection has been made (since the presence or absence of a
  1204. // retransmit endpoint is going to determine which player is selected during
  1205. // setDataSource).
  1206. if (p != 0) return INVALID_OPERATION;
  1207. if (NULL != endpoint) {
  1208. Mutex::Autolock lock(mLock);
  1209. mRetransmitEndpoint = *endpoint;
  1210. mRetransmitEndpointValid = true;
  1211. } else {
  1212. Mutex::Autolock lock(mLock);
  1213. mRetransmitEndpointValid = false;
  1214. }
  1215. return NO_ERROR;
  1216. }
  1217. status_t MediaPlayerService::Client::getRetransmitEndpoint(
  1218. struct sockaddr_in* endpoint)
  1219. {
  1220. if (NULL == endpoint)
  1221. return BAD_VALUE;
  1222. sp<MediaPlayerBase> p = getPlayer();
  1223. if (p != NULL)
  1224. return p->getRetransmitEndpoint(endpoint);
  1225. Mutex::Autolock lock(mLock);
  1226. if (!mRetransmitEndpointValid)
  1227. return NO_INIT;
  1228. *endpoint = mRetransmitEndpoint;
  1229. return NO_ERROR;
  1230. }
  1231. void MediaPlayerService::Client::notify(
  1232. int msg, int ext1, int ext2, const Parcel *obj)
  1233. {
  1234. sp<IMediaPlayerClient> c;
  1235. sp<Client> nextClient;
  1236. status_t errStartNext = NO_ERROR;
  1237. {
  1238. Mutex::Autolock l(mLock);
  1239. c = mClient;
  1240. if (msg == MEDIA_PLAYBACK_COMPLETE && mNextClient != NULL) {
  1241. nextClient = mNextClient;
  1242. if (mAudioOutput != NULL)
  1243. mAudioOutput->switchToNextOutput();
  1244. errStartNext = nextClient->start();
  1245. }
  1246. }
  1247. if (nextClient != NULL) {
  1248. sp<IMediaPlayerClient> nc;
  1249. {
  1250. Mutex::Autolock l(nextClient->mLock);
  1251. nc = nextClient->mClient;
  1252. }
  1253. if (nc != NULL) {
  1254. if (errStartNext == NO_ERROR) {
  1255. nc->notify(MEDIA_INFO, MEDIA_INFO_STARTED_AS_NEXT, 0, obj);
  1256. } else {
  1257. nc->notify(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN , 0, obj);
  1258. ALOGE("gapless:start playback for next track failed, err(%d)", errStartNext);
  1259. }
  1260. }
  1261. }
  1262. if (MEDIA_INFO == msg &&
  1263. MEDIA_INFO_METADATA_UPDATE == ext1) {
  1264. const media::Metadata::Type metadata_type = ext2;
  1265. if(shouldDropMetadata(metadata_type)) {
  1266. return;
  1267. }
  1268. // Update the list of metadata that have changed. getMetadata
  1269. // also access mMetadataUpdated and clears it.
  1270. addNewMetadataUpdate(metadata_type);
  1271. }
  1272. if (c != NULL) {
  1273. ALOGV("[%d] notify (%d, %d, %d)", mConnId, msg, ext1, ext2);
  1274. c->notify(msg, ext1, ext2, obj);
  1275. }
  1276. }
  1277. bool MediaPlayerService::Client::shouldDropMetadata(media::Metadata::Type code) const
  1278. {
  1279. Mutex::Autolock lock(mLock);
  1280. if (findMetadata(mMetadataDrop, code)) {
  1281. return true;
  1282. }
  1283. if (mMetadataAllow.isEmpty() || findMetadata(mMetadataAllow, code)) {
  1284. return false;
  1285. } else {
  1286. return true;
  1287. }
  1288. }
  1289. void MediaPlayerService::Client::addNewMetadataUpdate(media::Metadata::Type metadata_type) {
  1290. Mutex::Autolock lock(mLock);
  1291. if (mMetadataUpdated.indexOf(metadata_type) < 0) {
  1292. mMetadataUpdated.add(metadata_type);
  1293. }
  1294. }
  1295. // Modular DRM
  1296. status_t MediaPlayerService::Client::prepareDrm(const uint8_t uuid[16],
  1297. const Vector<uint8_t>& drmSessionId)
  1298. {
  1299. ALOGV("[%d] prepareDrm", mConnId);
  1300. sp<MediaPlayerBase> p = getPlayer();
  1301. if (p == 0) return UNKNOWN_ERROR;
  1302. status_t ret = p->prepareDrm(uuid, drmSessionId);
  1303. ALOGV("prepareDrm ret: %d", ret);
  1304. return ret;
  1305. }
  1306. status_t MediaPlayerService::Client::releaseDrm()
  1307. {
  1308. ALOGV("[%d] releaseDrm", mConnId);
  1309. sp<MediaPlayerBase> p = getPlayer();
  1310. if (p == 0) return UNKNOWN_ERROR;
  1311. status_t ret = p->releaseDrm();
  1312. ALOGV("releaseDrm ret: %d", ret);
  1313. return ret;
  1314. }
  1315. status_t MediaPlayerService::Client::setOutputDevice(audio_port_handle_t deviceId)
  1316. {
  1317. ALOGV("[%d] setOutputDevice", mConnId);
  1318. {
  1319. Mutex::Autolock l(mLock);
  1320. if (mAudioOutput.get() != nullptr) {
  1321. return mAudioOutput->setOutputDevice(deviceId);
  1322. }
  1323. }
  1324. return NO_INIT;
  1325. }
  1326. status_t MediaPlayerService::Client::getRoutedDeviceId(audio_port_handle_t* deviceId)
  1327. {
  1328. ALOGV("[%d] getRoutedDeviceId", mConnId);
  1329. {
  1330. Mutex::Autolock l(mLock);
  1331. if (mAudioOutput.get() != nullptr) {
  1332. return mAudioOutput->getRoutedDeviceId(deviceId);
  1333. }
  1334. }
  1335. return NO_INIT;
  1336. }
  1337. status_t MediaPlayerService::Client::enableAudioDeviceCallback(bool enabled)
  1338. {
  1339. ALOGV("[%d] enableAudioDeviceCallback, %d", mConnId, enabled);
  1340. {
  1341. Mutex::Autolock l(mLock);
  1342. if (mAudioOutput.get() != nullptr) {
  1343. return mAudioOutput->enableAudioDeviceCallback(enabled);
  1344. }
  1345. }
  1346. return NO_INIT;
  1347. }
  1348. #if CALLBACK_ANTAGONIZER
  1349. const int Antagonizer::interval = 10000; // 10 msecs
  1350. Antagonizer::Antagonizer(const sp<MediaPlayerBase::Listener> &listener) :
  1351. mExit(false), mActive(false), mListener(listener)
  1352. {
  1353. createThread(callbackThread, this);
  1354. }
  1355. void Antagonizer::kill()
  1356. {
  1357. Mutex::Autolock _l(mLock);
  1358. mActive = false;
  1359. mExit = true;
  1360. mCondition.wait(mLock);
  1361. }
  1362. int Antagonizer::callbackThread(void* user)
  1363. {
  1364. ALOGD("Antagonizer started");
  1365. Antagonizer* p = reinterpret_cast<Antagonizer*>(user);
  1366. while (!p->mExit) {
  1367. if (p->mActive) {
  1368. ALOGV("send event");
  1369. p->mListener->notify(0, 0, 0, 0);
  1370. }
  1371. usleep(interval);
  1372. }
  1373. Mutex::Autolock _l(p->mLock);
  1374. p->mCondition.signal();
  1375. ALOGD("Antagonizer stopped");
  1376. return 0;
  1377. }
  1378. #endif
  1379. #undef LOG_TAG
  1380. #define LOG_TAG "AudioSink"
  1381. MediaPlayerService::AudioOutput::AudioOutput(audio_session_t sessionId, uid_t uid, int pid,
  1382. const audio_attributes_t* attr, const sp<AudioSystem::AudioDeviceCallback>& deviceCallback)
  1383. : mCallback(NULL),
  1384. mCallbackCookie(NULL),
  1385. mCallbackData(NULL),
  1386. mStreamType(AUDIO_STREAM_MUSIC),
  1387. mLeftVolume(1.0),
  1388. mRightVolume(1.0),
  1389. mPlaybackRate(AUDIO_PLAYBACK_RATE_DEFAULT),
  1390. mSampleRateHz(0),
  1391. mMsecsPerFrame(0),
  1392. mFrameSize(0),
  1393. mSessionId(sessionId),
  1394. mUid(uid),
  1395. mPid(pid),
  1396. mSendLevel(0.0),
  1397. mAuxEffectId(0),
  1398. mFlags(AUDIO_OUTPUT_FLAG_NONE),
  1399. mVolumeHandler(new media::VolumeHandler()),
  1400. mSelectedDeviceId(AUDIO_PORT_HANDLE_NONE),
  1401. mRoutedDeviceId(AUDIO_PORT_HANDLE_NONE),
  1402. mDeviceCallbackEnabled(false),
  1403. mDeviceCallback(deviceCallback)
  1404. {
  1405. ALOGV("AudioOutput(%d)", sessionId);
  1406. if (attr != NULL) {
  1407. mAttributes = (audio_attributes_t *) calloc(1, sizeof(audio_attributes_t));
  1408. if (mAttributes != NULL) {
  1409. memcpy(mAttributes, attr, sizeof(audio_attributes_t));
  1410. mStreamType = AudioSystem::attributesToStreamType(*attr);
  1411. }
  1412. } else {
  1413. mAttributes = NULL;
  1414. }
  1415. setMinBufferCount();
  1416. }
  1417. MediaPlayerService::AudioOutput::~AudioOutput()
  1418. {
  1419. close();
  1420. free(mAttributes);
  1421. delete mCallbackData;
  1422. }
  1423. //static
  1424. void MediaPlayerService::AudioOutput::setMinBufferCount()
  1425. {
  1426. char value[PROPERTY_VALUE_MAX];
  1427. if (property_get("ro.kernel.qemu", value, 0)) {
  1428. mIsOnEmulator = true;
  1429. mMinBufferCount = 12; // to prevent systematic buffer underrun for emulator
  1430. }
  1431. }
  1432. // static
  1433. bool MediaPlayerService::AudioOutput::isOnEmulator()
  1434. {
  1435. setMinBufferCount(); // benign race wrt other threads
  1436. return mIsOnEmulator;
  1437. }
  1438. // static
  1439. int MediaPlayerService::AudioOutput::getMinBufferCount()
  1440. {
  1441. setMinBufferCount(); // benign race wrt other threads
  1442. return mMinBufferCount;
  1443. }
  1444. ssize_t MediaPlayerService::AudioOutput::bufferSize() const
  1445. {
  1446. Mutex::Autolock lock(mLock);
  1447. if (mTrack == 0) return NO_INIT;
  1448. return mTrack->frameCount() * mFrameSize;
  1449. }
  1450. ssize_t MediaPlayerService::AudioOutput::frameCount() const
  1451. {
  1452. Mutex::Autolock lock(mLock);
  1453. if (mTrack == 0) return NO_INIT;
  1454. return mTrack->frameCount();
  1455. }
  1456. ssize_t MediaPlayerService::AudioOutput::channelCount() const
  1457. {
  1458. Mutex::Autolock lock(mLock);
  1459. if (mTrack == 0) return NO_INIT;
  1460. return mTrack->channelCount();
  1461. }
  1462. ssize_t MediaPlayerService::AudioOutput::frameSize() const
  1463. {
  1464. Mutex::Autolock lock(mLock);
  1465. if (mTrack == 0) return NO_INIT;
  1466. return mFrameSize;
  1467. }
  1468. uint32_t MediaPlayerService::AudioOutput::latency () const
  1469. {
  1470. Mutex::Autolock lock(mLock);
  1471. if (mTrack == 0) return 0;
  1472. return mTrack->latency();
  1473. }
  1474. float MediaPlayerService::AudioOutput::msecsPerFrame() const
  1475. {
  1476. Mutex::Autolock lock(mLock);
  1477. return mMsecsPerFrame;
  1478. }
  1479. status_t MediaPlayerService::AudioOutput::getPosition(uint32_t *position) const
  1480. {
  1481. Mutex::Autolock lock(mLock);
  1482. if (mTrack == 0) return NO_INIT;
  1483. return mTrack->getPosition(position);
  1484. }
  1485. status_t MediaPlayerService::AudioOutput::getTimestamp(AudioTimestamp &ts) const
  1486. {
  1487. Mutex::Autolock lock(mLock);
  1488. if (mTrack == 0) return NO_INIT;
  1489. return mTrack->getTimestamp(ts);
  1490. }
  1491. // TODO: Remove unnecessary calls to getPlayedOutDurationUs()
  1492. // as it acquires locks and may query the audio driver.
  1493. //
  1494. // Some calls could conceivably retrieve extrapolated data instead of
  1495. // accessing getTimestamp() or getPosition() every time a data buffer with
  1496. // a media time is received.
  1497. //
  1498. // Calculate duration of played samples if played at normal rate (i.e., 1.0).
  1499. int64_t MediaPlayerService::AudioOutput::getPlayedOutDurationUs(int64_t nowUs) const
  1500. {
  1501. Mutex::Autolock lock(mLock);
  1502. if (mTrack == 0 || mSampleRateHz == 0) {
  1503. return 0;
  1504. }
  1505. uint32_t numFramesPlayed;
  1506. int64_t numFramesPlayedAtUs;
  1507. AudioTimestamp ts;
  1508. status_t res = mTrack->getTimestamp(ts);
  1509. if (res == OK) { // case 1: mixing audio tracks and offloaded tracks.
  1510. numFramesPlayed = ts.mPosition;
  1511. numFramesPlayedAtUs = ts.mTime.tv_sec * 1000000LL + ts.mTime.tv_nsec / 1000;
  1512. //ALOGD("getTimestamp: OK %d %lld", numFramesPlayed, (long long)numFramesPlayedAtUs);
  1513. } else if (res == WOULD_BLOCK) { // case 2: transitory state on start of a new track
  1514. numFramesPlayed = 0;
  1515. numFramesPlayedAtUs = nowUs;
  1516. //ALOGD("getTimestamp: WOULD_BLOCK %d %lld",
  1517. // numFramesPlayed, (long long)numFramesPlayedAtUs);
  1518. } else { // case 3: transitory at new track or audio fast tracks.
  1519. res = mTrack->getPosition(&numFramesPlayed);
  1520. CHECK_EQ(res, (status_t)OK);
  1521. numFramesPlayedAtUs = nowUs;
  1522. numFramesPlayedAtUs += 1000LL * mTrack->latency() / 2; /* XXX */
  1523. //ALOGD("getPosition: %u %lld", numFramesPlayed, (long long)numFramesPlayedAtUs);
  1524. }
  1525. // CHECK_EQ(numFramesPlayed & (1 << 31), 0); // can't be negative until 12.4 hrs, test
  1526. // TODO: remove the (int32_t) casting below as it may overflow at 12.4 hours.
  1527. int64_t durationUs = (int64_t)((int32_t)numFramesPlayed * 1000000LL / mSampleRateHz)
  1528. + nowUs - numFramesPlayedAtUs;
  1529. if (durationUs < 0) {
  1530. // Occurs when numFramesPlayed position is very small and the following:
  1531. // (1) In case 1, the time nowUs is computed before getTimestamp() is called and
  1532. // numFramesPlayedAtUs is greater than nowUs by time more than numFramesPlayed.
  1533. // (2) In case 3, using getPosition and adding mAudioSink->latency() to
  1534. // numFramesPlayedAtUs, by a time amount greater than numFramesPlayed.
  1535. //
  1536. // Both of these are transitory conditions.
  1537. ALOGV("getPlayedOutDurationUs: negative duration %lld set to zero", (long long)durationUs);
  1538. durationUs = 0;
  1539. }
  1540. ALOGV("getPlayedOutDurationUs(%lld) nowUs(%lld) frames(%u) framesAt(%lld)",
  1541. (long long)durationUs, (long long)nowUs,
  1542. numFramesPlayed, (long long)numFramesPlayedAtUs);
  1543. return durationUs;
  1544. }
  1545. status_t MediaPlayerService::AudioOutput::getFramesWritten(uint32_t *frameswritten) const
  1546. {
  1547. Mutex::Autolock lock(mLock);
  1548. if (mTrack == 0) return NO_INIT;
  1549. ExtendedTimestamp ets;
  1550. status_t status = mTrack->getTimestamp(&ets);
  1551. if (status == OK || status == WOULD_BLOCK) {
  1552. *frameswritten = (uint32_t)ets.mPosition[ExtendedTimestamp::LOCATION_CLIENT];
  1553. }
  1554. return status;
  1555. }
  1556. status_t MediaPlayerService::AudioOutput::setParameters(const String8& keyValuePairs)
  1557. {
  1558. Mutex::Autolock lock(mLock);
  1559. if (mTrack == 0) return NO_INIT;
  1560. return mTrack->setParameters(keyValuePairs);
  1561. }
  1562. String8 MediaPlayerService::AudioOutput::getParameters(const String8& keys)
  1563. {
  1564. Mutex::Autolock lock(mLock);
  1565. if (mTrack == 0) return String8::empty();
  1566. return mTrack->getParameters(keys);
  1567. }
  1568. void MediaPlayerService::AudioOutput::setAudioAttributes(const audio_attributes_t * attributes) {
  1569. Mutex::Autolock lock(mLock);
  1570. if (attributes == NULL) {
  1571. free(mAttributes);
  1572. mAttributes = NULL;
  1573. } else {
  1574. if (mAttributes == NULL) {
  1575. mAttributes = (audio_attributes_t *) calloc(1, sizeof(audio_attributes_t));
  1576. }
  1577. memcpy(mAttributes, attributes, sizeof(audio_attributes_t));
  1578. mStreamType = AudioSystem::attributesToStreamType(*attributes);
  1579. }
  1580. }
  1581. void MediaPlayerService::AudioOutput::setAudioStreamType(audio_stream_type_t streamType)
  1582. {
  1583. Mutex::Autolock lock(mLock);
  1584. // do not allow direct stream type modification if attributes have been set
  1585. if (mAttributes == NULL) {
  1586. mStreamType = streamType;
  1587. }
  1588. }
  1589. void MediaPlayerService::AudioOutput::deleteRecycledTrack_l()
  1590. {
  1591. ALOGV("deleteRecycledTrack_l");
  1592. if (mRecycledTrack != 0) {
  1593. if (mCallbackData != NULL) {
  1594. mCallbackData->setOutput(NULL);
  1595. mCallbackData->endTrackSwitch();
  1596. }
  1597. if ((mRecycledTrack->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) {
  1598. int32_t msec = 0;
  1599. if (!mRecycledTrack->stopped()) { // check if active
  1600. (void)mRecycledTrack->pendingDuration(&msec);
  1601. }
  1602. mRecycledTrack->stop(); // ensure full data drain
  1603. ALOGD("deleting recycled track, waiting for data drain (%d msec)", msec);
  1604. if (msec > 0) {
  1605. static const int32_t WAIT_LIMIT_MS = 3000;
  1606. if (msec > WAIT_LIMIT_MS) {
  1607. msec = WAIT_LIMIT_MS;
  1608. }
  1609. usleep(msec * 1000LL);
  1610. }
  1611. }
  1612. // An offloaded track isn't flushed because the STREAM_END is reported
  1613. // slightly prematurely to allow time for the gapless track switch
  1614. // but this means that if we decide not to recycle the track there
  1615. // could be a small amount of residual data still playing. We leave
  1616. // AudioFlinger to drain the track.
  1617. mRecycledTrack.clear();
  1618. close_l();
  1619. delete mCallbackData;
  1620. mCallbackData = NULL;
  1621. }
  1622. }
  1623. void MediaPlayerService::AudioOutput::close_l()
  1624. {
  1625. mTrack.clear();
  1626. }
  1627. status_t MediaPlayerService::AudioOutput::open(
  1628. uint32_t sampleRate, int channelCount, audio_channel_mask_t channelMask,
  1629. audio_format_t format, int bufferCount,
  1630. AudioCallback cb, void *cookie,
  1631. audio_output_flags_t flags,
  1632. const audio_offload_info_t *offloadInfo,
  1633. bool doNotReconnect,
  1634. uint32_t suggestedFrameCount)
  1635. {
  1636. ALOGV("open(%u, %d, 0x%x, 0x%x, %d, %d 0x%x)", sampleRate, channelCount, channelMask,
  1637. format, bufferCount, mSessionId, flags);
  1638. // offloading is only supported in callback mode for now.
  1639. // offloadInfo must be present if offload flag is set
  1640. if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) &&
  1641. ((cb == NULL) || (offloadInfo == NULL))) {
  1642. return BAD_VALUE;
  1643. }
  1644. // compute frame count for the AudioTrack internal buffer
  1645. size_t frameCount;
  1646. if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
  1647. frameCount = 0; // AudioTrack will get frame count from AudioFlinger
  1648. } else {
  1649. // try to estimate the buffer processing fetch size from AudioFlinger.
  1650. // framesPerBuffer is approximate and generally correct, except when it's not :-).
  1651. uint32_t afSampleRate;
  1652. size_t afFrameCount;
  1653. if (AudioSystem::getOutputFrameCount(&afFrameCount, mStreamType) != NO_ERROR) {
  1654. return NO_INIT;
  1655. }
  1656. if (AudioSystem::getOutputSamplingRate(&afSampleRate, mStreamType) != NO_ERROR) {
  1657. return NO_INIT;
  1658. }
  1659. if (afSampleRate == 0) {
  1660. return NO_INIT;
  1661. }
  1662. const size_t framesPerBuffer =
  1663. (unsigned long long)sampleRate * afFrameCount / afSampleRate;
  1664. if (bufferCount == 0) {
  1665. if (framesPerBuffer == 0) {
  1666. return NO_INIT;
  1667. }
  1668. // use suggestedFrameCount
  1669. bufferCount = (suggestedFrameCount + framesPerBuffer - 1) / framesPerBuffer;
  1670. }
  1671. // Check argument bufferCount against the mininum buffer count
  1672. if (bufferCount != 0 && bufferCount < mMinBufferCount) {
  1673. ALOGV("bufferCount (%d) increased to %d", bufferCount, mMinBufferCount);
  1674. bufferCount = mMinBufferCount;
  1675. }
  1676. // if frameCount is 0, then AudioTrack will get frame count from AudioFlinger
  1677. // which will be the minimum size permitted.
  1678. frameCount = bufferCount * framesPerBuffer;
  1679. }
  1680. if (channelMask == CHANNEL_MASK_USE_CHANNEL_ORDER) {
  1681. channelMask = audio_channel_out_mask_from_count(channelCount);
  1682. if (0 == channelMask) {
  1683. ALOGE("open() error, can\'t derive mask for %d audio channels", channelCount);
  1684. return NO_INIT;
  1685. }
  1686. }
  1687. Mutex::Autolock lock(mLock);
  1688. mCallback = cb;
  1689. mCallbackCookie = cookie;
  1690. // Check whether we can recycle the track
  1691. bool reuse = false;
  1692. bool bothOffloaded = false;
  1693. if (mRecycledTrack != 0) {
  1694. // check whether we are switching between two offloaded tracks
  1695. bothOffloaded = (flags & mRecycledTrack->getFlags()
  1696. & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0;
  1697. // check if the existing track can be reused as-is, or if a new track needs to be created.
  1698. reuse = true;
  1699. if ((mCallbackData == NULL && mCallback != NULL) ||
  1700. (mCallbackData != NULL && mCallback == NULL)) {
  1701. // recycled track uses callbacks but the caller wants to use writes, or vice versa
  1702. ALOGV("can't chain callback and write");
  1703. reuse = false;
  1704. } else if ((mRecycledTrack->getSampleRate() != sampleRate) ||
  1705. (mRecycledTrack->channelCount() != (uint32_t)channelCount) ) {
  1706. ALOGV("samplerate, channelcount differ: %u/%u Hz, %u/%d ch",
  1707. mRecycledTrack->getSampleRate(), sampleRate,
  1708. mRecycledTrack->channelCount(), channelCount);
  1709. reuse = false;
  1710. } else if (flags != mFlags) {
  1711. ALOGV("output flags differ %08x/%08x", flags, mFlags);
  1712. reuse = false;
  1713. } else if (mRecycledTrack->format() != format) {
  1714. reuse = false;
  1715. }
  1716. } else {
  1717. ALOGV("no track available to recycle");
  1718. }
  1719. ALOGV_IF(bothOffloaded, "both tracks offloaded");
  1720. // If we can't recycle and both tracks are offloaded
  1721. // we must close the previous output before opening a new one
  1722. if (bothOffloaded && !reuse) {
  1723. ALOGV("both offloaded and not recycling");
  1724. deleteRecycledTrack_l();
  1725. }
  1726. sp<AudioTrack> t;
  1727. CallbackData *newcbd = NULL;
  1728. // We don't attempt to create a new track if we are recycling an
  1729. // offloaded track. But, if we are recycling a non-offloaded or we
  1730. // are switching where one is offloaded and one isn't then we create
  1731. // the new track in advance so that we can read additional stream info
  1732. if (!(reuse && bothOffloaded)) {
  1733. ALOGV("creating new AudioTrack");
  1734. if (mCallback != NULL) {
  1735. newcbd = new CallbackData(this);
  1736. t = new AudioTrack(
  1737. mStreamType,
  1738. sampleRate,
  1739. format,
  1740. channelMask,
  1741. frameCount,
  1742. flags,
  1743. CallbackWrapper,
  1744. newcbd,
  1745. 0, // notification frames
  1746. mSessionId,
  1747. AudioTrack::TRANSFER_CALLBACK,
  1748. offloadInfo,
  1749. mUid,
  1750. mPid,
  1751. mAttributes,
  1752. doNotReconnect,
  1753. 1.0f, // default value for maxRequiredSpeed
  1754. mSelectedDeviceId);
  1755. } else {
  1756. // TODO: Due to buffer memory concerns, we use a max target playback speed
  1757. // based on mPlaybackRate at the time of open (instead of kMaxRequiredSpeed),
  1758. // also clamping the target speed to 1.0 <= targetSpeed <= kMaxRequiredSpeed.
  1759. const float targetSpeed =
  1760. std::min(std::max(mPlaybackRate.mSpeed, 1.0f), kMaxRequiredSpeed);
  1761. ALOGW_IF(targetSpeed != mPlaybackRate.mSpeed,
  1762. "track target speed:%f clamped from playback speed:%f",
  1763. targetSpeed, mPlaybackRate.mSpeed);
  1764. t = new AudioTrack(
  1765. mStreamType,
  1766. sampleRate,
  1767. format,
  1768. channelMask,
  1769. frameCount,
  1770. flags,
  1771. NULL, // callback
  1772. NULL, // user data
  1773. 0, // notification frames
  1774. mSessionId,
  1775. AudioTrack::TRANSFER_DEFAULT,
  1776. NULL, // offload info
  1777. mUid,
  1778. mPid,
  1779. mAttributes,
  1780. doNotReconnect,
  1781. targetSpeed,
  1782. mSelectedDeviceId);
  1783. }
  1784. if ((t == 0) || (t->initCheck() != NO_ERROR)) {
  1785. ALOGE("Unable to create audio track");
  1786. delete newcbd;
  1787. // t goes out of scope, so reference count drops to zero
  1788. return NO_INIT;
  1789. } else {
  1790. // successful AudioTrack initialization implies a legacy stream type was generated
  1791. // from the audio attributes
  1792. mStreamType = t->streamType();
  1793. }
  1794. }
  1795. if (reuse) {
  1796. CHECK(mRecycledTrack != NULL);
  1797. if (!bothOffloaded) {
  1798. if (mRecycledTrack->frameCount() != t->frameCount()) {
  1799. ALOGV("framecount differs: %zu/%zu frames",
  1800. mRecycledTrack->frameCount(), t->frameCount());
  1801. reuse = false;
  1802. }
  1803. }
  1804. if (reuse) {
  1805. ALOGV("chaining to next output and recycling track");
  1806. close_l();
  1807. mTrack = mRecycledTrack;
  1808. mRecycledTrack.clear();
  1809. if (mCallbackData != NULL) {
  1810. mCallbackData->setOutput(this);
  1811. }
  1812. delete newcbd;
  1813. return updateTrack();
  1814. }
  1815. }
  1816. // we're not going to reuse the track, unblock and flush it
  1817. // this was done earlier if both tracks are offloaded
  1818. if (!bothOffloaded) {
  1819. deleteRecycledTrack_l();
  1820. }
  1821. CHECK((t != NULL) && ((mCallback == NULL) || (newcbd != NULL)));
  1822. mCallbackData = newcbd;
  1823. ALOGV("setVolume");
  1824. t->setVolume(mLeftVolume, mRightVolume);
  1825. // Restore VolumeShapers for the MediaPlayer in case the track was recreated
  1826. // due to an output sink error (e.g. offload to non-offload switch).
  1827. mVolumeHandler->forall([&t](const VolumeShaper &shaper) -> VolumeShaper::Status {
  1828. sp<VolumeShaper::Operation> operationToEnd =
  1829. new VolumeShaper::Operation(shaper.mOperation);
  1830. // TODO: Ideally we would restore to the exact xOffset position
  1831. // as returned by getVolumeShaperState(), but we don't have that
  1832. // information when restoring at the client unless we periodically poll
  1833. // the server or create shared memory state.
  1834. //
  1835. // For now, we simply advance to the end of the VolumeShaper effect
  1836. // if it has been started.
  1837. if (shaper.isStarted()) {
  1838. operationToEnd->setNormalizedTime(1.f);
  1839. }
  1840. return t->applyVolumeShaper(shaper.mConfiguration, operationToEnd);
  1841. });
  1842. mSampleRateHz = sampleRate;
  1843. mFlags = flags;
  1844. mMsecsPerFrame = 1E3f / (mPlaybackRate.mSpeed * sampleRate);
  1845. mFrameSize = t->frameSize();
  1846. mTrack = t;
  1847. return updateTrack();
  1848. }
  1849. status_t MediaPlayerService::AudioOutput::updateTrack() {
  1850. if (mTrack == NULL) {
  1851. return NO_ERROR;
  1852. }
  1853. status_t res = NO_ERROR;
  1854. // Note some output devices may give us a direct track even though we don't specify it.
  1855. // Example: Line application b/17459982.
  1856. if ((mTrack->getFlags()
  1857. & (AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD | AUDIO_OUTPUT_FLAG_DIRECT)) == 0) {
  1858. res = mTrack->setPlaybackRate(mPlaybackRate);
  1859. if (res == NO_ERROR) {
  1860. mTrack->setAuxEffectSendLevel(mSendLevel);
  1861. res = mTrack->attachAuxEffect(mAuxEffectId);
  1862. }
  1863. }
  1864. mTrack->setOutputDevice(mSelectedDeviceId);
  1865. if (mDeviceCallbackEnabled) {
  1866. mTrack->addAudioDeviceCallback(mDeviceCallback.promote());
  1867. }
  1868. ALOGV("updateTrack() DONE status %d", res);
  1869. return res;
  1870. }
  1871. status_t MediaPlayerService::AudioOutput::start()
  1872. {
  1873. ALOGV("start");
  1874. Mutex::Autolock lock(mLock);
  1875. if (mCallbackData != NULL) {
  1876. mCallbackData->endTrackSwitch();
  1877. }
  1878. if (mTrack != 0) {
  1879. mTrack->setVolume(mLeftVolume, mRightVolume);
  1880. mTrack->setAuxEffectSendLevel(mSendLevel);
  1881. status_t status = mTrack->start();
  1882. if (status == NO_ERROR) {
  1883. mVolumeHandler->setStarted();
  1884. }
  1885. return status;
  1886. }
  1887. return NO_INIT;
  1888. }
  1889. void MediaPlayerService::AudioOutput::setNextOutput(const sp<AudioOutput>& nextOutput) {
  1890. Mutex::Autolock lock(mLock);
  1891. mNextOutput = nextOutput;
  1892. }
  1893. void MediaPlayerService::AudioOutput::switchToNextOutput() {
  1894. ALOGV("switchToNextOutput");
  1895. // Try to acquire the callback lock before moving track (without incurring deadlock).
  1896. const unsigned kMaxSwitchTries = 100;
  1897. Mutex::Autolock lock(mLock);
  1898. for (unsigned tries = 0;;) {
  1899. if (mTrack == 0) {
  1900. return;
  1901. }
  1902. if (mNextOutput != NULL && mNextOutput != this) {
  1903. if (mCallbackData != NULL) {
  1904. // two alternative approaches
  1905. #if 1
  1906. CallbackData *callbackData = mCallbackData;
  1907. mLock.unlock();
  1908. // proper acquisition sequence
  1909. callbackData->lock();
  1910. mLock.lock();
  1911. // Caution: it is unlikely that someone deleted our callback or changed our target
  1912. if (callbackData != mCallbackData || mNextOutput == NULL || mNextOutput == this) {
  1913. // fatal if we are starved out.
  1914. LOG_ALWAYS_FATAL_IF(++tries > kMaxSwitchTries,
  1915. "switchToNextOutput() cannot obtain correct lock sequence");
  1916. callbackData->unlock();
  1917. continue;
  1918. }
  1919. callbackData->mSwitching = true; // begin track switch
  1920. callbackData->setOutput(NULL);
  1921. #else
  1922. // tryBeginTrackSwitch() returns false if the callback has the lock.
  1923. if (!mCallbackData->tryBeginTrackSwitch()) {
  1924. // fatal if we are starved out.
  1925. LOG_ALWAYS_FATAL_IF(++tries > kMaxSwitchTries,
  1926. "switchToNextOutput() cannot obtain callback lock");
  1927. mLock.unlock();
  1928. usleep(5 * 1000 /* usec */); // allow callback to use AudioOutput
  1929. mLock.lock();
  1930. continue;
  1931. }
  1932. #endif
  1933. }
  1934. Mutex::Autolock nextLock(mNextOutput->mLock);
  1935. // If the next output track is not NULL, then it has been
  1936. // opened already for playback.
  1937. // This is possible even without the next player being started,
  1938. // for example, the next player could be prepared and seeked.
  1939. //
  1940. // Presuming it isn't advisable to force the track over.
  1941. if (mNextOutput->mTrack == NULL) {
  1942. ALOGD("Recycling track for gapless playback");
  1943. delete mNextOutput->mCallbackData;
  1944. mNextOutput->mCallbackData = mCallbackData;
  1945. mNextOutput->mRecycledTrack = mTrack;
  1946. mNextOutput->mSampleRateHz = mSampleRateHz;
  1947. mNextOutput->mMsecsPerFrame = mMsecsPerFrame;
  1948. mNextOutput->mFlags = mFlags;
  1949. mNextOutput->mFrameSize = mFrameSize;
  1950. close_l();
  1951. mCallbackData = NULL; // destruction handled by mNextOutput
  1952. } else {
  1953. ALOGW("Ignoring gapless playback because next player has already started");
  1954. // remove track in case resource needed for future players.
  1955. if (mCallbackData != NULL) {
  1956. mCallbackData->endTrackSwitch(); // release lock for callbacks before close.
  1957. }
  1958. close_l();
  1959. }
  1960. }
  1961. break;
  1962. }
  1963. }
  1964. ssize_t MediaPlayerService::AudioOutput::write(const void* buffer, size_t size, bool blocking)
  1965. {
  1966. Mutex::Autolock lock(mLock);
  1967. LOG_ALWAYS_FATAL_IF(mCallback != NULL, "Don't call write if supplying a callback.");
  1968. //ALOGV("write(%p, %u)", buffer, size);
  1969. if (mTrack != 0) {
  1970. return mTrack->write(buffer, size, blocking);
  1971. }
  1972. return NO_INIT;
  1973. }
  1974. void MediaPlayerService::AudioOutput::stop()
  1975. {
  1976. ALOGV("stop");
  1977. Mutex::Autolock lock(mLock);
  1978. if (mTrack != 0) mTrack->stop();
  1979. }
  1980. void MediaPlayerService::AudioOutput::flush()
  1981. {
  1982. ALOGV("flush");
  1983. Mutex::Autolock lock(mLock);
  1984. if (mTrack != 0) mTrack->flush();
  1985. }
  1986. void MediaPlayerService::AudioOutput::pause()
  1987. {
  1988. ALOGV("pause");
  1989. Mutex::Autolock lock(mLock);
  1990. if (mTrack != 0) mTrack->pause();
  1991. }
  1992. void MediaPlayerService::AudioOutput::close()
  1993. {
  1994. ALOGV("close");
  1995. sp<AudioTrack> track;
  1996. {
  1997. Mutex::Autolock lock(mLock);
  1998. track = mTrack;
  1999. close_l(); // clears mTrack
  2000. }
  2001. // destruction of the track occurs outside of mutex.
  2002. }
  2003. void MediaPlayerService::AudioOutput::setVolume(float left, float right)
  2004. {
  2005. ALOGV("setVolume(%f, %f)", left, right);
  2006. Mutex::Autolock lock(mLock);
  2007. mLeftVolume = left;
  2008. mRightVolume = right;
  2009. if (mTrack != 0) {
  2010. mTrack->setVolume(left, right);
  2011. }
  2012. }
  2013. status_t MediaPlayerService::AudioOutput::setPlaybackRate(const AudioPlaybackRate &rate)
  2014. {
  2015. ALOGV("setPlaybackRate(%f %f %d %d)",
  2016. rate.mSpeed, rate.mPitch, rate.mFallbackMode, rate.mStretchMode);
  2017. Mutex::Autolock lock(mLock);
  2018. if (mTrack == 0) {
  2019. // remember rate so that we can set it when the track is opened
  2020. mPlaybackRate = rate;
  2021. return OK;
  2022. }
  2023. status_t res = mTrack->setPlaybackRate(rate);
  2024. if (res != NO_ERROR) {
  2025. return res;
  2026. }
  2027. // rate.mSpeed is always greater than 0 if setPlaybackRate succeeded
  2028. CHECK_GT(rate.mSpeed, 0.f);
  2029. mPlaybackRate = rate;
  2030. if (mSampleRateHz != 0) {
  2031. mMsecsPerFrame = 1E3f / (rate.mSpeed * mSampleRateHz);
  2032. }
  2033. return res;
  2034. }
  2035. status_t MediaPlayerService::AudioOutput::getPlaybackRate(AudioPlaybackRate *rate)
  2036. {
  2037. ALOGV("setPlaybackRate");
  2038. Mutex::Autolock lock(mLock);
  2039. if (mTrack == 0) {
  2040. return NO_INIT;
  2041. }
  2042. *rate = mTrack->getPlaybackRate();
  2043. return NO_ERROR;
  2044. }
  2045. status_t MediaPlayerService::AudioOutput::setAuxEffectSendLevel(float level)
  2046. {
  2047. ALOGV("setAuxEffectSendLevel(%f)", level);
  2048. Mutex::Autolock lock(mLock);
  2049. mSendLevel = level;
  2050. if (mTrack != 0) {
  2051. return mTrack->setAuxEffectSendLevel(level);
  2052. }
  2053. return NO_ERROR;
  2054. }
  2055. status_t MediaPlayerService::AudioOutput::attachAuxEffect(int effectId)
  2056. {
  2057. ALOGV("attachAuxEffect(%d)", effectId);
  2058. Mutex::Autolock lock(mLock);
  2059. mAuxEffectId = effectId;
  2060. if (mTrack != 0) {
  2061. return mTrack->attachAuxEffect(effectId);
  2062. }
  2063. return NO_ERROR;
  2064. }
  2065. status_t MediaPlayerService::AudioOutput::setOutputDevice(audio_port_handle_t deviceId)
  2066. {
  2067. ALOGV("setOutputDevice(%d)", deviceId);
  2068. Mutex::Autolock lock(mLock);
  2069. mSelectedDeviceId = deviceId;
  2070. if (mTrack != 0) {
  2071. return mTrack->setOutputDevice(deviceId);
  2072. }
  2073. return NO_ERROR;
  2074. }
  2075. status_t MediaPlayerService::AudioOutput::getRoutedDeviceId(audio_port_handle_t* deviceId)
  2076. {
  2077. ALOGV("getRoutedDeviceId");
  2078. Mutex::Autolock lock(mLock);
  2079. if (mTrack != 0) {
  2080. mRoutedDeviceId = mTrack->getRoutedDeviceId();
  2081. }
  2082. *deviceId = mRoutedDeviceId;
  2083. return NO_ERROR;
  2084. }
  2085. status_t MediaPlayerService::AudioOutput::enableAudioDeviceCallback(bool enabled)
  2086. {
  2087. ALOGV("enableAudioDeviceCallback, %d", enabled);
  2088. Mutex::Autolock lock(mLock);
  2089. mDeviceCallbackEnabled = enabled;
  2090. if (mTrack != 0) {
  2091. status_t status;
  2092. if (enabled) {
  2093. status = mTrack->addAudioDeviceCallback(mDeviceCallback.promote());
  2094. } else {
  2095. status = mTrack->removeAudioDeviceCallback(mDeviceCallback.promote());
  2096. }
  2097. return status;
  2098. }
  2099. return NO_ERROR;
  2100. }
  2101. VolumeShaper::Status MediaPlayerService::AudioOutput::applyVolumeShaper(
  2102. const sp<VolumeShaper::Configuration>& configuration,
  2103. const sp<VolumeShaper::Operation>& operation)
  2104. {
  2105. Mutex::Autolock lock(mLock);
  2106. ALOGV("AudioOutput::applyVolumeShaper");
  2107. mVolumeHandler->setIdIfNecessary(configuration);
  2108. VolumeShaper::Status status;
  2109. if (mTrack != 0) {
  2110. status = mTrack->applyVolumeShaper(configuration, operation);
  2111. if (status >= 0) {
  2112. (void)mVolumeHandler->applyVolumeShaper(configuration, operation);
  2113. if (mTrack->isPlaying()) { // match local AudioTrack to properly restore.
  2114. mVolumeHandler->setStarted();
  2115. }
  2116. }
  2117. } else {
  2118. // VolumeShapers are not affected when a track moves between players for
  2119. // gapless playback (setNextMediaPlayer).
  2120. // We forward VolumeShaper operations that do not change configuration
  2121. // to the new player so that unducking may occur as expected.
  2122. // Unducking is an idempotent operation, same if applied back-to-back.
  2123. if (configuration->getType() == VolumeShaper::Configuration::TYPE_ID
  2124. && mNextOutput != nullptr) {
  2125. ALOGV("applyVolumeShaper: Attempting to forward missed operation: %s %s",
  2126. configuration->toString().c_str(), operation->toString().c_str());
  2127. Mutex::Autolock nextLock(mNextOutput->mLock);
  2128. // recycled track should be forwarded from this AudioSink by switchToNextOutput
  2129. sp<AudioTrack> track = mNextOutput->mRecycledTrack;
  2130. if (track != nullptr) {
  2131. ALOGD("Forward VolumeShaper operation to recycled track %p", track.get());
  2132. (void)track->applyVolumeShaper(configuration, operation);
  2133. } else {
  2134. // There is a small chance that the unduck occurs after the next
  2135. // player has already started, but before it is registered to receive
  2136. // the unduck command.
  2137. track = mNextOutput->mTrack;
  2138. if (track != nullptr) {
  2139. ALOGD("Forward VolumeShaper operation to track %p", track.get());
  2140. (void)track->applyVolumeShaper(configuration, operation);
  2141. }
  2142. }
  2143. }
  2144. status = mVolumeHandler->applyVolumeShaper(configuration, operation);
  2145. }
  2146. return status;
  2147. }
  2148. sp<VolumeShaper::State> MediaPlayerService::AudioOutput::getVolumeShaperState(int id)
  2149. {
  2150. Mutex::Autolock lock(mLock);
  2151. if (mTrack != 0) {
  2152. return mTrack->getVolumeShaperState(id);
  2153. } else {
  2154. return mVolumeHandler->getVolumeShaperState(id);
  2155. }
  2156. }
  2157. // static
  2158. void MediaPlayerService::AudioOutput::CallbackWrapper(
  2159. int event, void *cookie, void *info) {
  2160. //ALOGV("callbackwrapper");
  2161. CallbackData *data = (CallbackData*)cookie;
  2162. // lock to ensure we aren't caught in the middle of a track switch.
  2163. data->lock();
  2164. AudioOutput *me = data->getOutput();
  2165. AudioTrack::Buffer *buffer = (AudioTrack::Buffer *)info;
  2166. if (me == NULL) {
  2167. // no output set, likely because the track was scheduled to be reused
  2168. // by another player, but the format turned out to be incompatible.
  2169. data->unlock();
  2170. if (buffer != NULL) {
  2171. buffer->size = 0;
  2172. }
  2173. return;
  2174. }
  2175. switch(event) {
  2176. case AudioTrack::EVENT_MORE_DATA: {
  2177. size_t actualSize = (*me->mCallback)(
  2178. me, buffer->raw, buffer->size, me->mCallbackCookie,
  2179. CB_EVENT_FILL_BUFFER);
  2180. // Log when no data is returned from the callback.
  2181. // (1) We may have no data (especially with network streaming sources).
  2182. // (2) We may have reached the EOS and the audio track is not stopped yet.
  2183. // Note that AwesomePlayer/AudioPlayer will only return zero size when it reaches the EOS.
  2184. // NuPlayerRenderer will return zero when it doesn't have data (it doesn't block to fill).
  2185. //
  2186. // This is a benign busy-wait, with the next data request generated 10 ms or more later;
  2187. // nevertheless for power reasons, we don't want to see too many of these.
  2188. ALOGV_IF(actualSize == 0 && buffer->size > 0, "callbackwrapper: empty buffer returned");
  2189. buffer->size = actualSize;
  2190. } break;
  2191. case AudioTrack::EVENT_STREAM_END:
  2192. // currently only occurs for offloaded callbacks
  2193. ALOGV("callbackwrapper: deliver EVENT_STREAM_END");
  2194. (*me->mCallback)(me, NULL /* buffer */, 0 /* size */,
  2195. me->mCallbackCookie, CB_EVENT_STREAM_END);
  2196. break;
  2197. case AudioTrack::EVENT_NEW_IAUDIOTRACK :
  2198. ALOGV("callbackwrapper: deliver EVENT_TEAR_DOWN");
  2199. (*me->mCallback)(me, NULL /* buffer */, 0 /* size */,
  2200. me->mCallbackCookie, CB_EVENT_TEAR_DOWN);
  2201. break;
  2202. case AudioTrack::EVENT_UNDERRUN:
  2203. // This occurs when there is no data available, typically
  2204. // when there is a failure to supply data to the AudioTrack. It can also
  2205. // occur in non-offloaded mode when the audio device comes out of standby.
  2206. //
  2207. // If an AudioTrack underruns it outputs silence. Since this happens suddenly
  2208. // it may sound like an audible pop or glitch.
  2209. //
  2210. // The underrun event is sent once per track underrun; the condition is reset
  2211. // when more data is sent to the AudioTrack.
  2212. ALOGD("callbackwrapper: EVENT_UNDERRUN (discarded)");
  2213. break;
  2214. default:
  2215. ALOGE("received unknown event type: %d inside CallbackWrapper !", event);
  2216. }
  2217. data->unlock();
  2218. }
  2219. audio_session_t MediaPlayerService::AudioOutput::getSessionId() const
  2220. {
  2221. Mutex::Autolock lock(mLock);
  2222. return mSessionId;
  2223. }
  2224. uint32_t MediaPlayerService::AudioOutput::getSampleRate() const
  2225. {
  2226. Mutex::Autolock lock(mLock);
  2227. if (mTrack == 0) return 0;
  2228. return mTrack->getSampleRate();
  2229. }
  2230. int64_t MediaPlayerService::AudioOutput::getBufferDurationInUs() const
  2231. {
  2232. Mutex::Autolock lock(mLock);
  2233. if (mTrack == 0) {
  2234. return 0;
  2235. }
  2236. int64_t duration;
  2237. if (mTrack->getBufferDurationInUs(&duration) != OK) {
  2238. return 0;
  2239. }
  2240. return duration;
  2241. }
  2242. ////////////////////////////////////////////////////////////////////////////////
  2243. struct CallbackThread : public Thread {
  2244. CallbackThread(const wp<MediaPlayerBase::AudioSink> &sink,
  2245. MediaPlayerBase::AudioSink::AudioCallback cb,
  2246. void *cookie);
  2247. protected:
  2248. virtual ~CallbackThread();
  2249. virtual bool threadLoop();
  2250. private:
  2251. wp<MediaPlayerBase::AudioSink> mSink;
  2252. MediaPlayerBase::AudioSink::AudioCallback mCallback;
  2253. void *mCookie;
  2254. void *mBuffer;
  2255. size_t mBufferSize;
  2256. CallbackThread(const CallbackThread &);
  2257. CallbackThread &operator=(const CallbackThread &);
  2258. };
  2259. CallbackThread::CallbackThread(
  2260. const wp<MediaPlayerBase::AudioSink> &sink,
  2261. MediaPlayerBase::AudioSink::AudioCallback cb,
  2262. void *cookie)
  2263. : mSink(sink),
  2264. mCallback(cb),
  2265. mCookie(cookie),
  2266. mBuffer(NULL),
  2267. mBufferSize(0) {
  2268. }
  2269. CallbackThread::~CallbackThread() {
  2270. if (mBuffer) {
  2271. free(mBuffer);
  2272. mBuffer = NULL;
  2273. }
  2274. }
  2275. bool CallbackThread::threadLoop() {
  2276. sp<MediaPlayerBase::AudioSink> sink = mSink.promote();
  2277. if (sink == NULL) {
  2278. return false;
  2279. }
  2280. if (mBuffer == NULL) {
  2281. mBufferSize = sink->bufferSize();
  2282. mBuffer = malloc(mBufferSize);
  2283. }
  2284. size_t actualSize =
  2285. (*mCallback)(sink.get(), mBuffer, mBufferSize, mCookie,
  2286. MediaPlayerBase::AudioSink::CB_EVENT_FILL_BUFFER);
  2287. if (actualSize > 0) {
  2288. sink->write(mBuffer, actualSize);
  2289. // Could return false on sink->write() error or short count.
  2290. // Not necessarily appropriate but would work for AudioCache behavior.
  2291. }
  2292. return true;
  2293. }
  2294. ////////////////////////////////////////////////////////////////////////////////
  2295. void MediaPlayerService::addBatteryData(uint32_t params) {
  2296. mBatteryTracker.addBatteryData(params);
  2297. }
  2298. status_t MediaPlayerService::pullBatteryData(Parcel* reply) {
  2299. return mBatteryTracker.pullBatteryData(reply);
  2300. }
  2301. MediaPlayerService::BatteryTracker::BatteryTracker() {
  2302. mBatteryAudio.refCount = 0;
  2303. for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
  2304. mBatteryAudio.deviceOn[i] = 0;
  2305. mBatteryAudio.lastTime[i] = 0;
  2306. mBatteryAudio.totalTime[i] = 0;
  2307. }
  2308. // speaker is on by default
  2309. mBatteryAudio.deviceOn[SPEAKER] = 1;
  2310. // reset battery stats
  2311. // if the mediaserver has crashed, battery stats could be left
  2312. // in bad state, reset the state upon service start.
  2313. BatteryNotifier::getInstance().noteResetVideo();
  2314. }
  2315. void MediaPlayerService::BatteryTracker::addBatteryData(uint32_t params)
  2316. {
  2317. Mutex::Autolock lock(mLock);
  2318. int32_t time = systemTime() / 1000000L;
  2319. // change audio output devices. This notification comes from AudioFlinger
  2320. if ((params & kBatteryDataSpeakerOn)
  2321. || (params & kBatteryDataOtherAudioDeviceOn)) {
  2322. int deviceOn[NUM_AUDIO_DEVICES];
  2323. for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
  2324. deviceOn[i] = 0;
  2325. }
  2326. if ((params & kBatteryDataSpeakerOn)
  2327. && (params & kBatteryDataOtherAudioDeviceOn)) {
  2328. deviceOn[SPEAKER_AND_OTHER] = 1;
  2329. } else if (params & kBatteryDataSpeakerOn) {
  2330. deviceOn[SPEAKER] = 1;
  2331. } else {
  2332. deviceOn[OTHER_AUDIO_DEVICE] = 1;
  2333. }
  2334. for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
  2335. if (mBatteryAudio.deviceOn[i] != deviceOn[i]){
  2336. if (mBatteryAudio.refCount > 0) { // if playing audio
  2337. if (!deviceOn[i]) {
  2338. mBatteryAudio.lastTime[i] += time;
  2339. mBatteryAudio.totalTime[i] += mBatteryAudio.lastTime[i];
  2340. mBatteryAudio.lastTime[i] = 0;
  2341. } else {
  2342. mBatteryAudio.lastTime[i] = 0 - time;
  2343. }
  2344. }
  2345. mBatteryAudio.deviceOn[i] = deviceOn[i];
  2346. }
  2347. }
  2348. return;
  2349. }
  2350. // an audio stream is started
  2351. if (params & kBatteryDataAudioFlingerStart) {
  2352. // record the start time only if currently no other audio
  2353. // is being played
  2354. if (mBatteryAudio.refCount == 0) {
  2355. for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
  2356. if (mBatteryAudio.deviceOn[i]) {
  2357. mBatteryAudio.lastTime[i] -= time;
  2358. }
  2359. }
  2360. }
  2361. mBatteryAudio.refCount ++;
  2362. return;
  2363. } else if (params & kBatteryDataAudioFlingerStop) {
  2364. if (mBatteryAudio.refCount <= 0) {
  2365. ALOGW("Battery track warning: refCount is <= 0");
  2366. return;
  2367. }
  2368. // record the stop time only if currently this is the only
  2369. // audio being played
  2370. if (mBatteryAudio.refCount == 1) {
  2371. for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
  2372. if (mBatteryAudio.deviceOn[i]) {
  2373. mBatteryAudio.lastTime[i] += time;
  2374. mBatteryAudio.totalTime[i] += mBatteryAudio.lastTime[i];
  2375. mBatteryAudio.lastTime[i] = 0;
  2376. }
  2377. }
  2378. }
  2379. mBatteryAudio.refCount --;
  2380. return;
  2381. }
  2382. uid_t uid = IPCThreadState::self()->getCallingUid();
  2383. if (uid == AID_MEDIA) {
  2384. return;
  2385. }
  2386. int index = mBatteryData.indexOfKey(uid);
  2387. if (index < 0) { // create a new entry for this UID
  2388. BatteryUsageInfo info;
  2389. info.audioTotalTime = 0;
  2390. info.videoTotalTime = 0;
  2391. info.audioLastTime = 0;
  2392. info.videoLastTime = 0;
  2393. info.refCount = 0;
  2394. if (mBatteryData.add(uid, info) == NO_MEMORY) {
  2395. ALOGE("Battery track error: no memory for new app");
  2396. return;
  2397. }
  2398. }
  2399. BatteryUsageInfo &info = mBatteryData.editValueFor(uid);
  2400. if (params & kBatteryDataCodecStarted) {
  2401. if (params & kBatteryDataTrackAudio) {
  2402. info.audioLastTime -= time;
  2403. info.refCount ++;
  2404. }
  2405. if (params & kBatteryDataTrackVideo) {
  2406. info.videoLastTime -= time;
  2407. info.refCount ++;
  2408. }
  2409. } else {
  2410. if (info.refCount == 0) {
  2411. ALOGW("Battery track warning: refCount is already 0");
  2412. return;
  2413. } else if (info.refCount < 0) {
  2414. ALOGE("Battery track error: refCount < 0");
  2415. mBatteryData.removeItem(uid);
  2416. return;
  2417. }
  2418. if (params & kBatteryDataTrackAudio) {
  2419. info.audioLastTime += time;
  2420. info.refCount --;
  2421. }
  2422. if (params & kBatteryDataTrackVideo) {
  2423. info.videoLastTime += time;
  2424. info.refCount --;
  2425. }
  2426. // no stream is being played by this UID
  2427. if (info.refCount == 0) {
  2428. info.audioTotalTime += info.audioLastTime;
  2429. info.audioLastTime = 0;
  2430. info.videoTotalTime += info.videoLastTime;
  2431. info.videoLastTime = 0;
  2432. }
  2433. }
  2434. }
  2435. status_t MediaPlayerService::BatteryTracker::pullBatteryData(Parcel* reply) {
  2436. Mutex::Autolock lock(mLock);
  2437. // audio output devices usage
  2438. int32_t time = systemTime() / 1000000L; //in ms
  2439. int32_t totalTime;
  2440. for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
  2441. totalTime = mBatteryAudio.totalTime[i];
  2442. if (mBatteryAudio.deviceOn[i]
  2443. && (mBatteryAudio.lastTime[i] != 0)) {
  2444. int32_t tmpTime = mBatteryAudio.lastTime[i] + time;
  2445. totalTime += tmpTime;
  2446. }
  2447. reply->writeInt32(totalTime);
  2448. // reset the total time
  2449. mBatteryAudio.totalTime[i] = 0;
  2450. }
  2451. // codec usage
  2452. BatteryUsageInfo info;
  2453. int size = mBatteryData.size();
  2454. reply->writeInt32(size);
  2455. int i = 0;
  2456. while (i < size) {
  2457. info = mBatteryData.valueAt(i);
  2458. reply->writeInt32(mBatteryData.keyAt(i)); //UID
  2459. reply->writeInt32(info.audioTotalTime);
  2460. reply->writeInt32(info.videoTotalTime);
  2461. info.audioTotalTime = 0;
  2462. info.videoTotalTime = 0;
  2463. // remove the UID entry where no stream is being played
  2464. if (info.refCount <= 0) {
  2465. mBatteryData.removeItemsAt(i);
  2466. size --;
  2467. i --;
  2468. }
  2469. i++;
  2470. }
  2471. return NO_ERROR;
  2472. }
  2473. } // namespace android