NuPlayer2Renderer.cpp 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096
  1. /*
  2. * Copyright (C) 2010 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. //#define LOG_NDEBUG 0
  17. #define LOG_TAG "NuPlayer2Renderer"
  18. #include <utils/Log.h>
  19. #include "JWakeLock.h"
  20. #include "NuPlayer2Renderer.h"
  21. #include <algorithm>
  22. #include <cutils/properties.h>
  23. #include <media/stagefright/foundation/ADebug.h>
  24. #include <media/stagefright/foundation/AMessage.h>
  25. #include <media/stagefright/foundation/AUtils.h>
  26. #include <media/stagefright/MediaClock.h>
  27. #include <media/stagefright/MediaCodecConstants.h>
  28. #include <media/stagefright/MediaDefs.h>
  29. #include <media/stagefright/MediaErrors.h>
  30. #include <media/stagefright/Utils.h>
  31. #include <media/stagefright/VideoFrameScheduler2.h>
  32. #include <media/MediaCodecBuffer.h>
  33. #include <inttypes.h>
  34. namespace android {
  35. /*
  36. * Example of common configuration settings in shell script form
  37. #Turn offload audio off (use PCM for Play Music) -- AudioPolicyManager
  38. adb shell setprop audio.offload.disable 1
  39. #Allow offload audio with video (requires offloading to be enabled) -- AudioPolicyManager
  40. adb shell setprop audio.offload.video 1
  41. #Use audio callbacks for PCM data
  42. adb shell setprop media.stagefright.audio.cbk 1
  43. #Use deep buffer for PCM data with video (it is generally enabled for audio-only)
  44. adb shell setprop media.stagefright.audio.deep 1
  45. #Set size of buffers for pcm audio sink in msec (example: 1000 msec)
  46. adb shell setprop media.stagefright.audio.sink 1000
  47. * These configurations take effect for the next track played (not the current track).
  48. */
  49. static inline bool getUseAudioCallbackSetting() {
  50. return property_get_bool("media.stagefright.audio.cbk", false /* default_value */);
  51. }
  52. static inline int32_t getAudioSinkPcmMsSetting() {
  53. return property_get_int32(
  54. "media.stagefright.audio.sink", 500 /* default_value */);
  55. }
  56. // Maximum time in paused state when offloading audio decompression. When elapsed, the AudioSink
  57. // is closed to allow the audio DSP to power down.
  58. static const int64_t kOffloadPauseMaxUs = 10000000LL;
  59. // Maximum allowed delay from AudioSink, 1.5 seconds.
  60. static const int64_t kMaxAllowedAudioSinkDelayUs = 1500000LL;
  61. static const int64_t kMinimumAudioClockUpdatePeriodUs = 20 /* msec */ * 1000;
  62. // Default video frame display duration when only video exists.
  63. // Used to set max media time in MediaClock.
  64. static const int64_t kDefaultVideoFrameIntervalUs = 100000LL;
  65. // static
  66. const NuPlayer2::Renderer::PcmInfo NuPlayer2::Renderer::AUDIO_PCMINFO_INITIALIZER = {
  67. AUDIO_CHANNEL_NONE,
  68. AUDIO_OUTPUT_FLAG_NONE,
  69. AUDIO_FORMAT_INVALID,
  70. 0, // mNumChannels
  71. 0 // mSampleRate
  72. };
  73. // static
  74. const int64_t NuPlayer2::Renderer::kMinPositionUpdateDelayUs = 100000LL;
  75. static audio_format_t constexpr audioFormatFromEncoding(int32_t pcmEncoding) {
  76. switch (pcmEncoding) {
  77. case kAudioEncodingPcmFloat:
  78. return AUDIO_FORMAT_PCM_FLOAT;
  79. case kAudioEncodingPcm16bit:
  80. return AUDIO_FORMAT_PCM_16_BIT;
  81. case kAudioEncodingPcm8bit:
  82. return AUDIO_FORMAT_PCM_8_BIT; // TODO: do we want to support this?
  83. default:
  84. ALOGE("%s: Invalid encoding: %d", __func__, pcmEncoding);
  85. return AUDIO_FORMAT_INVALID;
  86. }
  87. }
  88. NuPlayer2::Renderer::Renderer(
  89. const sp<MediaPlayer2Interface::AudioSink> &sink,
  90. const sp<MediaClock> &mediaClock,
  91. const sp<AMessage> &notify,
  92. const sp<JObjectHolder> &context,
  93. uint32_t flags)
  94. : mAudioSink(sink),
  95. mUseVirtualAudioSink(false),
  96. mNotify(notify),
  97. mFlags(flags),
  98. mNumFramesWritten(0),
  99. mDrainAudioQueuePending(false),
  100. mDrainVideoQueuePending(false),
  101. mAudioQueueGeneration(0),
  102. mVideoQueueGeneration(0),
  103. mAudioDrainGeneration(0),
  104. mVideoDrainGeneration(0),
  105. mAudioEOSGeneration(0),
  106. mMediaClock(mediaClock),
  107. mPlaybackSettings(AUDIO_PLAYBACK_RATE_DEFAULT),
  108. mAudioFirstAnchorTimeMediaUs(-1),
  109. mAnchorTimeMediaUs(-1),
  110. mAnchorNumFramesWritten(-1),
  111. mVideoLateByUs(0LL),
  112. mNextVideoTimeMediaUs(-1),
  113. mHasAudio(false),
  114. mHasVideo(false),
  115. mNotifyCompleteAudio(false),
  116. mNotifyCompleteVideo(false),
  117. mSyncQueues(false),
  118. mPaused(true),
  119. mPauseDrainAudioAllowedUs(0),
  120. mVideoSampleReceived(false),
  121. mVideoRenderingStarted(false),
  122. mVideoRenderingStartGeneration(0),
  123. mAudioRenderingStartGeneration(0),
  124. mRenderingDataDelivered(false),
  125. mNextAudioClockUpdateTimeUs(-1),
  126. mLastAudioMediaTimeUs(-1),
  127. mAudioOffloadPauseTimeoutGeneration(0),
  128. mAudioTornDown(false),
  129. mCurrentOffloadInfo(AUDIO_INFO_INITIALIZER),
  130. mCurrentPcmInfo(AUDIO_PCMINFO_INITIALIZER),
  131. mTotalBuffersQueued(0),
  132. mLastAudioBufferDrained(0),
  133. mUseAudioCallback(false),
  134. mWakeLock(new JWakeLock(context)) {
  135. CHECK(mediaClock != NULL);
  136. mMediaClock->setPlaybackRate(mPlaybackSettings.mSpeed);
  137. }
  138. NuPlayer2::Renderer::~Renderer() {
  139. if (offloadingAudio()) {
  140. mAudioSink->stop();
  141. mAudioSink->flush();
  142. mAudioSink->close();
  143. }
  144. // Try to avoid racing condition in case callback is still on.
  145. Mutex::Autolock autoLock(mLock);
  146. if (mUseAudioCallback) {
  147. flushQueue(&mAudioQueue);
  148. flushQueue(&mVideoQueue);
  149. }
  150. mWakeLock.clear();
  151. mVideoScheduler.clear();
  152. mNotify.clear();
  153. mAudioSink.clear();
  154. }
  155. void NuPlayer2::Renderer::queueBuffer(
  156. bool audio,
  157. const sp<MediaCodecBuffer> &buffer,
  158. const sp<AMessage> &notifyConsumed) {
  159. sp<AMessage> msg = new AMessage(kWhatQueueBuffer, this);
  160. msg->setInt32("queueGeneration", getQueueGeneration(audio));
  161. msg->setInt32("audio", static_cast<int32_t>(audio));
  162. msg->setObject("buffer", buffer);
  163. msg->setMessage("notifyConsumed", notifyConsumed);
  164. msg->post();
  165. }
  166. void NuPlayer2::Renderer::queueEOS(bool audio, status_t finalResult) {
  167. CHECK_NE(finalResult, (status_t)OK);
  168. sp<AMessage> msg = new AMessage(kWhatQueueEOS, this);
  169. msg->setInt32("queueGeneration", getQueueGeneration(audio));
  170. msg->setInt32("audio", static_cast<int32_t>(audio));
  171. msg->setInt32("finalResult", finalResult);
  172. msg->post();
  173. }
  174. status_t NuPlayer2::Renderer::setPlaybackSettings(const AudioPlaybackRate &rate) {
  175. sp<AMessage> msg = new AMessage(kWhatConfigPlayback, this);
  176. writeToAMessage(msg, rate);
  177. sp<AMessage> response;
  178. status_t err = msg->postAndAwaitResponse(&response);
  179. if (err == OK && response != NULL) {
  180. CHECK(response->findInt32("err", &err));
  181. }
  182. return err;
  183. }
  184. status_t NuPlayer2::Renderer::onConfigPlayback(const AudioPlaybackRate &rate /* sanitized */) {
  185. if (rate.mSpeed <= 0.f) {
  186. ALOGW("playback rate cannot be %f", rate.mSpeed);
  187. return BAD_VALUE;
  188. }
  189. if (mAudioSink != NULL && mAudioSink->ready()) {
  190. status_t err = mAudioSink->setPlaybackRate(rate);
  191. if (err != OK) {
  192. ALOGW("failed to get playback rate from audio sink, err(%d)", err);
  193. return err;
  194. }
  195. }
  196. mPlaybackSettings = rate;
  197. mMediaClock->setPlaybackRate(mPlaybackSettings.mSpeed);
  198. return OK;
  199. }
  200. status_t NuPlayer2::Renderer::getPlaybackSettings(AudioPlaybackRate *rate /* nonnull */) {
  201. sp<AMessage> msg = new AMessage(kWhatGetPlaybackSettings, this);
  202. sp<AMessage> response;
  203. status_t err = msg->postAndAwaitResponse(&response);
  204. if (err == OK && response != NULL) {
  205. CHECK(response->findInt32("err", &err));
  206. if (err == OK) {
  207. readFromAMessage(response, rate);
  208. }
  209. }
  210. return err;
  211. }
  212. status_t NuPlayer2::Renderer::onGetPlaybackSettings(AudioPlaybackRate *rate /* nonnull */) {
  213. if (mAudioSink != NULL && mAudioSink->ready()) {
  214. status_t err = mAudioSink->getPlaybackRate(rate);
  215. if (err == OK) {
  216. if (!isAudioPlaybackRateEqual(*rate, mPlaybackSettings)) {
  217. ALOGW("correcting mismatch in internal/external playback rate, %f vs %f",
  218. rate->mSpeed, mPlaybackSettings.mSpeed);
  219. }
  220. // get playback settings used by audiosink, as it may be
  221. // slightly off due to audiosink not taking small changes.
  222. mPlaybackSettings = *rate;
  223. }
  224. return err;
  225. }
  226. *rate = mPlaybackSettings;
  227. return OK;
  228. }
  229. status_t NuPlayer2::Renderer::setSyncSettings(const AVSyncSettings &sync, float videoFpsHint) {
  230. sp<AMessage> msg = new AMessage(kWhatConfigSync, this);
  231. writeToAMessage(msg, sync, videoFpsHint);
  232. sp<AMessage> response;
  233. status_t err = msg->postAndAwaitResponse(&response);
  234. if (err == OK && response != NULL) {
  235. CHECK(response->findInt32("err", &err));
  236. }
  237. return err;
  238. }
  239. status_t NuPlayer2::Renderer::onConfigSync(const AVSyncSettings &sync, float videoFpsHint __unused) {
  240. if (sync.mSource != AVSYNC_SOURCE_DEFAULT) {
  241. return BAD_VALUE;
  242. }
  243. // TODO: support sync sources
  244. return INVALID_OPERATION;
  245. }
  246. status_t NuPlayer2::Renderer::getSyncSettings(AVSyncSettings *sync, float *videoFps) {
  247. sp<AMessage> msg = new AMessage(kWhatGetSyncSettings, this);
  248. sp<AMessage> response;
  249. status_t err = msg->postAndAwaitResponse(&response);
  250. if (err == OK && response != NULL) {
  251. CHECK(response->findInt32("err", &err));
  252. if (err == OK) {
  253. readFromAMessage(response, sync, videoFps);
  254. }
  255. }
  256. return err;
  257. }
  258. status_t NuPlayer2::Renderer::onGetSyncSettings(
  259. AVSyncSettings *sync /* nonnull */, float *videoFps /* nonnull */) {
  260. *sync = mSyncSettings;
  261. *videoFps = -1.f;
  262. return OK;
  263. }
  264. void NuPlayer2::Renderer::flush(bool audio, bool notifyComplete) {
  265. {
  266. Mutex::Autolock autoLock(mLock);
  267. if (audio) {
  268. mNotifyCompleteAudio |= notifyComplete;
  269. clearAudioFirstAnchorTime_l();
  270. ++mAudioQueueGeneration;
  271. ++mAudioDrainGeneration;
  272. } else {
  273. mNotifyCompleteVideo |= notifyComplete;
  274. ++mVideoQueueGeneration;
  275. ++mVideoDrainGeneration;
  276. mNextVideoTimeMediaUs = -1;
  277. }
  278. mMediaClock->clearAnchor();
  279. mVideoLateByUs = 0;
  280. mSyncQueues = false;
  281. }
  282. sp<AMessage> msg = new AMessage(kWhatFlush, this);
  283. msg->setInt32("audio", static_cast<int32_t>(audio));
  284. msg->post();
  285. }
  286. void NuPlayer2::Renderer::signalTimeDiscontinuity() {
  287. }
  288. void NuPlayer2::Renderer::signalDisableOffloadAudio() {
  289. (new AMessage(kWhatDisableOffloadAudio, this))->post();
  290. }
  291. void NuPlayer2::Renderer::signalEnableOffloadAudio() {
  292. (new AMessage(kWhatEnableOffloadAudio, this))->post();
  293. }
  294. void NuPlayer2::Renderer::pause() {
  295. (new AMessage(kWhatPause, this))->post();
  296. }
  297. void NuPlayer2::Renderer::resume() {
  298. (new AMessage(kWhatResume, this))->post();
  299. }
  300. void NuPlayer2::Renderer::setVideoFrameRate(float fps) {
  301. sp<AMessage> msg = new AMessage(kWhatSetVideoFrameRate, this);
  302. msg->setFloat("frame-rate", fps);
  303. msg->post();
  304. }
  305. // Called on any threads without mLock acquired.
  306. status_t NuPlayer2::Renderer::getCurrentPosition(int64_t *mediaUs) {
  307. status_t result = mMediaClock->getMediaTime(ALooper::GetNowUs(), mediaUs);
  308. if (result == OK) {
  309. return result;
  310. }
  311. // MediaClock has not started yet. Try to start it if possible.
  312. {
  313. Mutex::Autolock autoLock(mLock);
  314. if (mAudioFirstAnchorTimeMediaUs == -1) {
  315. return result;
  316. }
  317. AudioTimestamp ts;
  318. status_t res = mAudioSink->getTimestamp(ts);
  319. if (res != OK) {
  320. return result;
  321. }
  322. // AudioSink has rendered some frames.
  323. int64_t nowUs = ALooper::GetNowUs();
  324. int64_t nowMediaUs = mAudioSink->getPlayedOutDurationUs(nowUs)
  325. + mAudioFirstAnchorTimeMediaUs;
  326. mMediaClock->updateAnchor(nowMediaUs, nowUs, -1);
  327. }
  328. return mMediaClock->getMediaTime(ALooper::GetNowUs(), mediaUs);
  329. }
  330. void NuPlayer2::Renderer::clearAudioFirstAnchorTime_l() {
  331. mAudioFirstAnchorTimeMediaUs = -1;
  332. mMediaClock->setStartingTimeMedia(-1);
  333. }
  334. void NuPlayer2::Renderer::setAudioFirstAnchorTimeIfNeeded_l(int64_t mediaUs) {
  335. if (mAudioFirstAnchorTimeMediaUs == -1) {
  336. mAudioFirstAnchorTimeMediaUs = mediaUs;
  337. mMediaClock->setStartingTimeMedia(mediaUs);
  338. }
  339. }
  340. // Called on renderer looper.
  341. void NuPlayer2::Renderer::clearAnchorTime() {
  342. mMediaClock->clearAnchor();
  343. mAnchorTimeMediaUs = -1;
  344. mAnchorNumFramesWritten = -1;
  345. }
  346. void NuPlayer2::Renderer::setVideoLateByUs(int64_t lateUs) {
  347. Mutex::Autolock autoLock(mLock);
  348. mVideoLateByUs = lateUs;
  349. }
  350. int64_t NuPlayer2::Renderer::getVideoLateByUs() {
  351. Mutex::Autolock autoLock(mLock);
  352. return mVideoLateByUs;
  353. }
  354. status_t NuPlayer2::Renderer::openAudioSink(
  355. const sp<AMessage> &format,
  356. bool offloadOnly,
  357. bool hasVideo,
  358. uint32_t flags,
  359. bool *isOffloaded,
  360. bool isStreaming) {
  361. sp<AMessage> msg = new AMessage(kWhatOpenAudioSink, this);
  362. msg->setMessage("format", format);
  363. msg->setInt32("offload-only", offloadOnly);
  364. msg->setInt32("has-video", hasVideo);
  365. msg->setInt32("flags", flags);
  366. msg->setInt32("isStreaming", isStreaming);
  367. sp<AMessage> response;
  368. status_t postStatus = msg->postAndAwaitResponse(&response);
  369. int32_t err;
  370. if (postStatus != OK || response.get() == nullptr || !response->findInt32("err", &err)) {
  371. err = INVALID_OPERATION;
  372. } else if (err == OK && isOffloaded != NULL) {
  373. int32_t offload;
  374. CHECK(response->findInt32("offload", &offload));
  375. *isOffloaded = (offload != 0);
  376. }
  377. return err;
  378. }
  379. void NuPlayer2::Renderer::closeAudioSink() {
  380. sp<AMessage> msg = new AMessage(kWhatCloseAudioSink, this);
  381. sp<AMessage> response;
  382. msg->postAndAwaitResponse(&response);
  383. }
  384. void NuPlayer2::Renderer::changeAudioFormat(
  385. const sp<AMessage> &format,
  386. bool offloadOnly,
  387. bool hasVideo,
  388. uint32_t flags,
  389. bool isStreaming,
  390. const sp<AMessage> &notify) {
  391. sp<AMessage> meta = new AMessage;
  392. meta->setMessage("format", format);
  393. meta->setInt32("offload-only", offloadOnly);
  394. meta->setInt32("has-video", hasVideo);
  395. meta->setInt32("flags", flags);
  396. meta->setInt32("isStreaming", isStreaming);
  397. sp<AMessage> msg = new AMessage(kWhatChangeAudioFormat, this);
  398. msg->setInt32("queueGeneration", getQueueGeneration(true /* audio */));
  399. msg->setMessage("notify", notify);
  400. msg->setMessage("meta", meta);
  401. msg->post();
  402. }
  403. void NuPlayer2::Renderer::onMessageReceived(const sp<AMessage> &msg) {
  404. switch (msg->what()) {
  405. case kWhatOpenAudioSink:
  406. {
  407. sp<AMessage> format;
  408. CHECK(msg->findMessage("format", &format));
  409. int32_t offloadOnly;
  410. CHECK(msg->findInt32("offload-only", &offloadOnly));
  411. int32_t hasVideo;
  412. CHECK(msg->findInt32("has-video", &hasVideo));
  413. uint32_t flags;
  414. CHECK(msg->findInt32("flags", (int32_t *)&flags));
  415. uint32_t isStreaming;
  416. CHECK(msg->findInt32("isStreaming", (int32_t *)&isStreaming));
  417. status_t err = onOpenAudioSink(format, offloadOnly, hasVideo, flags, isStreaming);
  418. sp<AMessage> response = new AMessage;
  419. response->setInt32("err", err);
  420. response->setInt32("offload", offloadingAudio());
  421. sp<AReplyToken> replyID;
  422. CHECK(msg->senderAwaitsResponse(&replyID));
  423. response->postReply(replyID);
  424. break;
  425. }
  426. case kWhatCloseAudioSink:
  427. {
  428. sp<AReplyToken> replyID;
  429. CHECK(msg->senderAwaitsResponse(&replyID));
  430. onCloseAudioSink();
  431. sp<AMessage> response = new AMessage;
  432. response->postReply(replyID);
  433. break;
  434. }
  435. case kWhatStopAudioSink:
  436. {
  437. mAudioSink->stop();
  438. break;
  439. }
  440. case kWhatChangeAudioFormat:
  441. {
  442. int32_t queueGeneration;
  443. CHECK(msg->findInt32("queueGeneration", &queueGeneration));
  444. sp<AMessage> notify;
  445. CHECK(msg->findMessage("notify", &notify));
  446. if (offloadingAudio()) {
  447. ALOGW("changeAudioFormat should NOT be called in offload mode");
  448. notify->setInt32("err", INVALID_OPERATION);
  449. notify->post();
  450. break;
  451. }
  452. sp<AMessage> meta;
  453. CHECK(msg->findMessage("meta", &meta));
  454. if (queueGeneration != getQueueGeneration(true /* audio */)
  455. || mAudioQueue.empty()) {
  456. onChangeAudioFormat(meta, notify);
  457. break;
  458. }
  459. QueueEntry entry;
  460. entry.mNotifyConsumed = notify;
  461. entry.mMeta = meta;
  462. Mutex::Autolock autoLock(mLock);
  463. mAudioQueue.push_back(entry);
  464. postDrainAudioQueue_l();
  465. break;
  466. }
  467. case kWhatDrainAudioQueue:
  468. {
  469. mDrainAudioQueuePending = false;
  470. int32_t generation;
  471. CHECK(msg->findInt32("drainGeneration", &generation));
  472. if (generation != getDrainGeneration(true /* audio */)) {
  473. break;
  474. }
  475. if (onDrainAudioQueue()) {
  476. uint32_t numFramesPlayed;
  477. CHECK_EQ(mAudioSink->getPosition(&numFramesPlayed),
  478. (status_t)OK);
  479. // Handle AudioTrack race when start is immediately called after flush.
  480. uint32_t numFramesPendingPlayout =
  481. (mNumFramesWritten > numFramesPlayed ?
  482. mNumFramesWritten - numFramesPlayed : 0);
  483. // This is how long the audio sink will have data to
  484. // play back.
  485. int64_t delayUs =
  486. mAudioSink->msecsPerFrame()
  487. * numFramesPendingPlayout * 1000ll;
  488. if (mPlaybackSettings.mSpeed > 1.0f) {
  489. delayUs /= mPlaybackSettings.mSpeed;
  490. }
  491. // Let's give it more data after about half that time
  492. // has elapsed.
  493. delayUs /= 2;
  494. // check the buffer size to estimate maximum delay permitted.
  495. const int64_t maxDrainDelayUs = std::max(
  496. mAudioSink->getBufferDurationInUs(), (int64_t)500000 /* half second */);
  497. ALOGD_IF(delayUs > maxDrainDelayUs, "postDrainAudioQueue long delay: %lld > %lld",
  498. (long long)delayUs, (long long)maxDrainDelayUs);
  499. Mutex::Autolock autoLock(mLock);
  500. postDrainAudioQueue_l(delayUs);
  501. }
  502. break;
  503. }
  504. case kWhatDrainVideoQueue:
  505. {
  506. int32_t generation;
  507. CHECK(msg->findInt32("drainGeneration", &generation));
  508. if (generation != getDrainGeneration(false /* audio */)) {
  509. break;
  510. }
  511. mDrainVideoQueuePending = false;
  512. onDrainVideoQueue();
  513. postDrainVideoQueue();
  514. break;
  515. }
  516. case kWhatPostDrainVideoQueue:
  517. {
  518. int32_t generation;
  519. CHECK(msg->findInt32("drainGeneration", &generation));
  520. if (generation != getDrainGeneration(false /* audio */)) {
  521. break;
  522. }
  523. mDrainVideoQueuePending = false;
  524. postDrainVideoQueue();
  525. break;
  526. }
  527. case kWhatQueueBuffer:
  528. {
  529. onQueueBuffer(msg);
  530. break;
  531. }
  532. case kWhatQueueEOS:
  533. {
  534. onQueueEOS(msg);
  535. break;
  536. }
  537. case kWhatEOS:
  538. {
  539. int32_t generation;
  540. CHECK(msg->findInt32("audioEOSGeneration", &generation));
  541. if (generation != mAudioEOSGeneration) {
  542. break;
  543. }
  544. status_t finalResult;
  545. CHECK(msg->findInt32("finalResult", &finalResult));
  546. notifyEOS(true /* audio */, finalResult);
  547. break;
  548. }
  549. case kWhatConfigPlayback:
  550. {
  551. sp<AReplyToken> replyID;
  552. CHECK(msg->senderAwaitsResponse(&replyID));
  553. AudioPlaybackRate rate;
  554. readFromAMessage(msg, &rate);
  555. status_t err = onConfigPlayback(rate);
  556. sp<AMessage> response = new AMessage;
  557. response->setInt32("err", err);
  558. response->postReply(replyID);
  559. break;
  560. }
  561. case kWhatGetPlaybackSettings:
  562. {
  563. sp<AReplyToken> replyID;
  564. CHECK(msg->senderAwaitsResponse(&replyID));
  565. AudioPlaybackRate rate = AUDIO_PLAYBACK_RATE_DEFAULT;
  566. status_t err = onGetPlaybackSettings(&rate);
  567. sp<AMessage> response = new AMessage;
  568. if (err == OK) {
  569. writeToAMessage(response, rate);
  570. }
  571. response->setInt32("err", err);
  572. response->postReply(replyID);
  573. break;
  574. }
  575. case kWhatConfigSync:
  576. {
  577. sp<AReplyToken> replyID;
  578. CHECK(msg->senderAwaitsResponse(&replyID));
  579. AVSyncSettings sync;
  580. float videoFpsHint;
  581. readFromAMessage(msg, &sync, &videoFpsHint);
  582. status_t err = onConfigSync(sync, videoFpsHint);
  583. sp<AMessage> response = new AMessage;
  584. response->setInt32("err", err);
  585. response->postReply(replyID);
  586. break;
  587. }
  588. case kWhatGetSyncSettings:
  589. {
  590. sp<AReplyToken> replyID;
  591. CHECK(msg->senderAwaitsResponse(&replyID));
  592. ALOGV("kWhatGetSyncSettings");
  593. AVSyncSettings sync;
  594. float videoFps = -1.f;
  595. status_t err = onGetSyncSettings(&sync, &videoFps);
  596. sp<AMessage> response = new AMessage;
  597. if (err == OK) {
  598. writeToAMessage(response, sync, videoFps);
  599. }
  600. response->setInt32("err", err);
  601. response->postReply(replyID);
  602. break;
  603. }
  604. case kWhatFlush:
  605. {
  606. onFlush(msg);
  607. break;
  608. }
  609. case kWhatDisableOffloadAudio:
  610. {
  611. onDisableOffloadAudio();
  612. break;
  613. }
  614. case kWhatEnableOffloadAudio:
  615. {
  616. onEnableOffloadAudio();
  617. break;
  618. }
  619. case kWhatPause:
  620. {
  621. onPause();
  622. break;
  623. }
  624. case kWhatResume:
  625. {
  626. onResume();
  627. break;
  628. }
  629. case kWhatSetVideoFrameRate:
  630. {
  631. float fps;
  632. CHECK(msg->findFloat("frame-rate", &fps));
  633. onSetVideoFrameRate(fps);
  634. break;
  635. }
  636. case kWhatAudioTearDown:
  637. {
  638. int32_t reason;
  639. CHECK(msg->findInt32("reason", &reason));
  640. onAudioTearDown((AudioTearDownReason)reason);
  641. break;
  642. }
  643. case kWhatAudioOffloadPauseTimeout:
  644. {
  645. int32_t generation;
  646. CHECK(msg->findInt32("drainGeneration", &generation));
  647. if (generation != mAudioOffloadPauseTimeoutGeneration) {
  648. break;
  649. }
  650. ALOGV("Audio Offload tear down due to pause timeout.");
  651. onAudioTearDown(kDueToTimeout);
  652. mWakeLock->release();
  653. break;
  654. }
  655. default:
  656. TRESPASS();
  657. break;
  658. }
  659. }
  660. void NuPlayer2::Renderer::postDrainAudioQueue_l(int64_t delayUs) {
  661. if (mDrainAudioQueuePending || mSyncQueues || mUseAudioCallback) {
  662. return;
  663. }
  664. if (mAudioQueue.empty()) {
  665. return;
  666. }
  667. // FIXME: if paused, wait until AudioTrack stop() is complete before delivering data.
  668. if (mPaused) {
  669. const int64_t diffUs = mPauseDrainAudioAllowedUs - ALooper::GetNowUs();
  670. if (diffUs > delayUs) {
  671. delayUs = diffUs;
  672. }
  673. }
  674. mDrainAudioQueuePending = true;
  675. sp<AMessage> msg = new AMessage(kWhatDrainAudioQueue, this);
  676. msg->setInt32("drainGeneration", mAudioDrainGeneration);
  677. msg->post(delayUs);
  678. }
  679. void NuPlayer2::Renderer::prepareForMediaRenderingStart_l() {
  680. mAudioRenderingStartGeneration = mAudioDrainGeneration;
  681. mVideoRenderingStartGeneration = mVideoDrainGeneration;
  682. mRenderingDataDelivered = false;
  683. }
  684. void NuPlayer2::Renderer::notifyIfMediaRenderingStarted_l() {
  685. if (mVideoRenderingStartGeneration == mVideoDrainGeneration &&
  686. mAudioRenderingStartGeneration == mAudioDrainGeneration) {
  687. mRenderingDataDelivered = true;
  688. if (mPaused) {
  689. return;
  690. }
  691. mVideoRenderingStartGeneration = -1;
  692. mAudioRenderingStartGeneration = -1;
  693. sp<AMessage> notify = mNotify->dup();
  694. notify->setInt32("what", kWhatMediaRenderingStart);
  695. notify->post();
  696. }
  697. }
  698. // static
  699. size_t NuPlayer2::Renderer::AudioSinkCallback(
  700. MediaPlayer2Interface::AudioSink * /* audioSink */,
  701. void *buffer,
  702. size_t size,
  703. void *cookie,
  704. MediaPlayer2Interface::AudioSink::cb_event_t event) {
  705. NuPlayer2::Renderer *me = (NuPlayer2::Renderer *)cookie;
  706. switch (event) {
  707. case MediaPlayer2Interface::AudioSink::CB_EVENT_FILL_BUFFER:
  708. {
  709. return me->fillAudioBuffer(buffer, size);
  710. break;
  711. }
  712. case MediaPlayer2Interface::AudioSink::CB_EVENT_STREAM_END:
  713. {
  714. ALOGV("AudioSink::CB_EVENT_STREAM_END");
  715. me->notifyEOSCallback();
  716. break;
  717. }
  718. case MediaPlayer2Interface::AudioSink::CB_EVENT_TEAR_DOWN:
  719. {
  720. ALOGV("AudioSink::CB_EVENT_TEAR_DOWN");
  721. me->notifyAudioTearDown(kDueToError);
  722. break;
  723. }
  724. }
  725. return 0;
  726. }
  727. void NuPlayer2::Renderer::notifyEOSCallback() {
  728. Mutex::Autolock autoLock(mLock);
  729. if (!mUseAudioCallback) {
  730. return;
  731. }
  732. notifyEOS_l(true /* audio */, ERROR_END_OF_STREAM);
  733. }
  734. size_t NuPlayer2::Renderer::fillAudioBuffer(void *buffer, size_t size) {
  735. Mutex::Autolock autoLock(mLock);
  736. if (!mUseAudioCallback) {
  737. return 0;
  738. }
  739. bool hasEOS = false;
  740. size_t sizeCopied = 0;
  741. bool firstEntry = true;
  742. QueueEntry *entry; // will be valid after while loop if hasEOS is set.
  743. while (sizeCopied < size && !mAudioQueue.empty()) {
  744. entry = &*mAudioQueue.begin();
  745. if (entry->mBuffer == NULL) { // EOS
  746. hasEOS = true;
  747. mAudioQueue.erase(mAudioQueue.begin());
  748. break;
  749. }
  750. if (firstEntry && entry->mOffset == 0) {
  751. firstEntry = false;
  752. int64_t mediaTimeUs;
  753. CHECK(entry->mBuffer->meta()->findInt64("timeUs", &mediaTimeUs));
  754. ALOGV("fillAudioBuffer: rendering audio at media time %.2f secs", mediaTimeUs / 1E6);
  755. setAudioFirstAnchorTimeIfNeeded_l(mediaTimeUs);
  756. }
  757. size_t copy = entry->mBuffer->size() - entry->mOffset;
  758. size_t sizeRemaining = size - sizeCopied;
  759. if (copy > sizeRemaining) {
  760. copy = sizeRemaining;
  761. }
  762. memcpy((char *)buffer + sizeCopied,
  763. entry->mBuffer->data() + entry->mOffset,
  764. copy);
  765. entry->mOffset += copy;
  766. if (entry->mOffset == entry->mBuffer->size()) {
  767. entry->mNotifyConsumed->post();
  768. mAudioQueue.erase(mAudioQueue.begin());
  769. entry = NULL;
  770. }
  771. sizeCopied += copy;
  772. notifyIfMediaRenderingStarted_l();
  773. }
  774. if (mAudioFirstAnchorTimeMediaUs >= 0) {
  775. int64_t nowUs = ALooper::GetNowUs();
  776. int64_t nowMediaUs =
  777. mAudioFirstAnchorTimeMediaUs + mAudioSink->getPlayedOutDurationUs(nowUs);
  778. // we don't know how much data we are queueing for offloaded tracks.
  779. mMediaClock->updateAnchor(nowMediaUs, nowUs, INT64_MAX);
  780. }
  781. // for non-offloaded audio, we need to compute the frames written because
  782. // there is no EVENT_STREAM_END notification. The frames written gives
  783. // an estimate on the pending played out duration.
  784. if (!offloadingAudio()) {
  785. mNumFramesWritten += sizeCopied / mAudioSink->frameSize();
  786. }
  787. if (hasEOS) {
  788. (new AMessage(kWhatStopAudioSink, this))->post();
  789. // As there is currently no EVENT_STREAM_END callback notification for
  790. // non-offloaded audio tracks, we need to post the EOS ourselves.
  791. if (!offloadingAudio()) {
  792. int64_t postEOSDelayUs = 0;
  793. if (mAudioSink->needsTrailingPadding()) {
  794. postEOSDelayUs = getPendingAudioPlayoutDurationUs(ALooper::GetNowUs());
  795. }
  796. ALOGV("fillAudioBuffer: notifyEOS_l "
  797. "mNumFramesWritten:%u finalResult:%d postEOSDelay:%lld",
  798. mNumFramesWritten, entry->mFinalResult, (long long)postEOSDelayUs);
  799. notifyEOS_l(true /* audio */, entry->mFinalResult, postEOSDelayUs);
  800. }
  801. }
  802. return sizeCopied;
  803. }
  804. void NuPlayer2::Renderer::drainAudioQueueUntilLastEOS() {
  805. List<QueueEntry>::iterator it = mAudioQueue.begin(), itEOS = it;
  806. bool foundEOS = false;
  807. while (it != mAudioQueue.end()) {
  808. int32_t eos;
  809. QueueEntry *entry = &*it++;
  810. if ((entry->mBuffer == nullptr && entry->mNotifyConsumed == nullptr)
  811. || (entry->mNotifyConsumed->findInt32("eos", &eos) && eos != 0)) {
  812. itEOS = it;
  813. foundEOS = true;
  814. }
  815. }
  816. if (foundEOS) {
  817. // post all replies before EOS and drop the samples
  818. for (it = mAudioQueue.begin(); it != itEOS; it++) {
  819. if (it->mBuffer == nullptr) {
  820. if (it->mNotifyConsumed == nullptr) {
  821. // delay doesn't matter as we don't even have an AudioTrack
  822. notifyEOS(true /* audio */, it->mFinalResult);
  823. } else {
  824. // TAG for re-opening audio sink.
  825. onChangeAudioFormat(it->mMeta, it->mNotifyConsumed);
  826. }
  827. } else {
  828. it->mNotifyConsumed->post();
  829. }
  830. }
  831. mAudioQueue.erase(mAudioQueue.begin(), itEOS);
  832. }
  833. }
  834. bool NuPlayer2::Renderer::onDrainAudioQueue() {
  835. // do not drain audio during teardown as queued buffers may be invalid.
  836. if (mAudioTornDown) {
  837. return false;
  838. }
  839. // TODO: This call to getPosition checks if AudioTrack has been created
  840. // in AudioSink before draining audio. If AudioTrack doesn't exist, then
  841. // CHECKs on getPosition will fail.
  842. // We still need to figure out why AudioTrack is not created when
  843. // this function is called. One possible reason could be leftover
  844. // audio. Another possible place is to check whether decoder
  845. // has received INFO_FORMAT_CHANGED as the first buffer since
  846. // AudioSink is opened there, and possible interactions with flush
  847. // immediately after start. Investigate error message
  848. // "vorbis_dsp_synthesis returned -135", along with RTSP.
  849. uint32_t numFramesPlayed;
  850. if (mAudioSink->getPosition(&numFramesPlayed) != OK) {
  851. // When getPosition fails, renderer will not reschedule the draining
  852. // unless new samples are queued.
  853. // If we have pending EOS (or "eos" marker for discontinuities), we need
  854. // to post these now as NuPlayer2Decoder might be waiting for it.
  855. drainAudioQueueUntilLastEOS();
  856. ALOGW("onDrainAudioQueue(): audio sink is not ready");
  857. return false;
  858. }
  859. #if 0
  860. ssize_t numFramesAvailableToWrite =
  861. mAudioSink->frameCount() - (mNumFramesWritten - numFramesPlayed);
  862. if (numFramesAvailableToWrite == mAudioSink->frameCount()) {
  863. ALOGI("audio sink underrun");
  864. } else {
  865. ALOGV("audio queue has %d frames left to play",
  866. mAudioSink->frameCount() - numFramesAvailableToWrite);
  867. }
  868. #endif
  869. uint32_t prevFramesWritten = mNumFramesWritten;
  870. while (!mAudioQueue.empty()) {
  871. QueueEntry *entry = &*mAudioQueue.begin();
  872. if (entry->mBuffer == NULL) {
  873. if (entry->mNotifyConsumed != nullptr) {
  874. // TAG for re-open audio sink.
  875. onChangeAudioFormat(entry->mMeta, entry->mNotifyConsumed);
  876. mAudioQueue.erase(mAudioQueue.begin());
  877. continue;
  878. }
  879. // EOS
  880. if (mPaused) {
  881. // Do not notify EOS when paused.
  882. // This is needed to avoid switch to next clip while in pause.
  883. ALOGV("onDrainAudioQueue(): Do not notify EOS when paused");
  884. return false;
  885. }
  886. int64_t postEOSDelayUs = 0;
  887. if (mAudioSink->needsTrailingPadding()) {
  888. postEOSDelayUs = getPendingAudioPlayoutDurationUs(ALooper::GetNowUs());
  889. }
  890. notifyEOS(true /* audio */, entry->mFinalResult, postEOSDelayUs);
  891. mLastAudioMediaTimeUs = getDurationUsIfPlayedAtSampleRate(mNumFramesWritten);
  892. mAudioQueue.erase(mAudioQueue.begin());
  893. entry = NULL;
  894. if (mAudioSink->needsTrailingPadding()) {
  895. // If we're not in gapless playback (i.e. through setNextPlayer), we
  896. // need to stop the track here, because that will play out the last
  897. // little bit at the end of the file. Otherwise short files won't play.
  898. mAudioSink->stop();
  899. mNumFramesWritten = 0;
  900. }
  901. return false;
  902. }
  903. mLastAudioBufferDrained = entry->mBufferOrdinal;
  904. // ignore 0-sized buffer which could be EOS marker with no data
  905. if (entry->mOffset == 0 && entry->mBuffer->size() > 0) {
  906. int64_t mediaTimeUs;
  907. CHECK(entry->mBuffer->meta()->findInt64("timeUs", &mediaTimeUs));
  908. ALOGV("onDrainAudioQueue: rendering audio at media time %.2f secs",
  909. mediaTimeUs / 1E6);
  910. onNewAudioMediaTime(mediaTimeUs);
  911. }
  912. size_t copy = entry->mBuffer->size() - entry->mOffset;
  913. ssize_t written = mAudioSink->write(entry->mBuffer->data() + entry->mOffset,
  914. copy, false /* blocking */);
  915. if (written < 0) {
  916. // An error in AudioSink write. Perhaps the AudioSink was not properly opened.
  917. if (written == WOULD_BLOCK) {
  918. ALOGV("AudioSink write would block when writing %zu bytes", copy);
  919. } else {
  920. ALOGE("AudioSink write error(%zd) when writing %zu bytes", written, copy);
  921. // This can only happen when AudioSink was opened with doNotReconnect flag set to
  922. // true, in which case the NuPlayer2 will handle the reconnect.
  923. notifyAudioTearDown(kDueToError);
  924. }
  925. break;
  926. }
  927. entry->mOffset += written;
  928. size_t remainder = entry->mBuffer->size() - entry->mOffset;
  929. if ((ssize_t)remainder < mAudioSink->frameSize()) {
  930. if (remainder > 0) {
  931. ALOGW("Corrupted audio buffer has fractional frames, discarding %zu bytes.",
  932. remainder);
  933. entry->mOffset += remainder;
  934. copy -= remainder;
  935. }
  936. entry->mNotifyConsumed->post();
  937. mAudioQueue.erase(mAudioQueue.begin());
  938. entry = NULL;
  939. }
  940. size_t copiedFrames = written / mAudioSink->frameSize();
  941. mNumFramesWritten += copiedFrames;
  942. {
  943. Mutex::Autolock autoLock(mLock);
  944. int64_t maxTimeMedia;
  945. maxTimeMedia =
  946. mAnchorTimeMediaUs +
  947. (int64_t)(max((long long)mNumFramesWritten - mAnchorNumFramesWritten, 0LL)
  948. * 1000LL * mAudioSink->msecsPerFrame());
  949. mMediaClock->updateMaxTimeMedia(maxTimeMedia);
  950. notifyIfMediaRenderingStarted_l();
  951. }
  952. if (written != (ssize_t)copy) {
  953. // A short count was received from AudioSink::write()
  954. //
  955. // AudioSink write is called in non-blocking mode.
  956. // It may return with a short count when:
  957. //
  958. // 1) Size to be copied is not a multiple of the frame size. Fractional frames are
  959. // discarded.
  960. // 2) The data to be copied exceeds the available buffer in AudioSink.
  961. // 3) An error occurs and data has been partially copied to the buffer in AudioSink.
  962. // 4) AudioSink is an AudioCache for data retrieval, and the AudioCache is exceeded.
  963. // (Case 1)
  964. // Must be a multiple of the frame size. If it is not a multiple of a frame size, it
  965. // needs to fail, as we should not carry over fractional frames between calls.
  966. CHECK_EQ(copy % mAudioSink->frameSize(), 0u);
  967. // (Case 2, 3, 4)
  968. // Return early to the caller.
  969. // Beware of calling immediately again as this may busy-loop if you are not careful.
  970. ALOGV("AudioSink write short frame count %zd < %zu", written, copy);
  971. break;
  972. }
  973. }
  974. // calculate whether we need to reschedule another write.
  975. bool reschedule = !mAudioQueue.empty()
  976. && (!mPaused
  977. || prevFramesWritten != mNumFramesWritten); // permit pause to fill buffers
  978. //ALOGD("reschedule:%d empty:%d mPaused:%d prevFramesWritten:%u mNumFramesWritten:%u",
  979. // reschedule, mAudioQueue.empty(), mPaused, prevFramesWritten, mNumFramesWritten);
  980. return reschedule;
  981. }
  982. int64_t NuPlayer2::Renderer::getDurationUsIfPlayedAtSampleRate(uint32_t numFrames) {
  983. int32_t sampleRate = offloadingAudio() ?
  984. mCurrentOffloadInfo.sample_rate : mCurrentPcmInfo.mSampleRate;
  985. if (sampleRate == 0) {
  986. ALOGE("sampleRate is 0 in %s mode", offloadingAudio() ? "offload" : "non-offload");
  987. return 0;
  988. }
  989. return (int64_t)(numFrames * 1000000LL / sampleRate);
  990. }
  991. // Calculate duration of pending samples if played at normal rate (i.e., 1.0).
  992. int64_t NuPlayer2::Renderer::getPendingAudioPlayoutDurationUs(int64_t nowUs) {
  993. int64_t writtenAudioDurationUs = getDurationUsIfPlayedAtSampleRate(mNumFramesWritten);
  994. if (mUseVirtualAudioSink) {
  995. int64_t nowUs = ALooper::GetNowUs();
  996. int64_t mediaUs;
  997. if (mMediaClock->getMediaTime(nowUs, &mediaUs) != OK) {
  998. return 0LL;
  999. } else {
  1000. return writtenAudioDurationUs - (mediaUs - mAudioFirstAnchorTimeMediaUs);
  1001. }
  1002. }
  1003. const int64_t audioSinkPlayedUs = mAudioSink->getPlayedOutDurationUs(nowUs);
  1004. int64_t pendingUs = writtenAudioDurationUs - audioSinkPlayedUs;
  1005. if (pendingUs < 0) {
  1006. // This shouldn't happen unless the timestamp is stale.
  1007. ALOGW("%s: pendingUs %lld < 0, clamping to zero, potential resume after pause "
  1008. "writtenAudioDurationUs: %lld, audioSinkPlayedUs: %lld",
  1009. __func__, (long long)pendingUs,
  1010. (long long)writtenAudioDurationUs, (long long)audioSinkPlayedUs);
  1011. pendingUs = 0;
  1012. }
  1013. return pendingUs;
  1014. }
  1015. int64_t NuPlayer2::Renderer::getRealTimeUs(int64_t mediaTimeUs, int64_t nowUs) {
  1016. int64_t realUs;
  1017. if (mMediaClock->getRealTimeFor(mediaTimeUs, &realUs) != OK) {
  1018. // If failed to get current position, e.g. due to audio clock is
  1019. // not ready, then just play out video immediately without delay.
  1020. return nowUs;
  1021. }
  1022. return realUs;
  1023. }
  1024. void NuPlayer2::Renderer::onNewAudioMediaTime(int64_t mediaTimeUs) {
  1025. Mutex::Autolock autoLock(mLock);
  1026. // TRICKY: vorbis decoder generates multiple frames with the same
  1027. // timestamp, so only update on the first frame with a given timestamp
  1028. if (mediaTimeUs == mAnchorTimeMediaUs) {
  1029. return;
  1030. }
  1031. setAudioFirstAnchorTimeIfNeeded_l(mediaTimeUs);
  1032. // mNextAudioClockUpdateTimeUs is -1 if we're waiting for audio sink to start
  1033. if (mNextAudioClockUpdateTimeUs == -1) {
  1034. AudioTimestamp ts;
  1035. if (mAudioSink->getTimestamp(ts) == OK && ts.mPosition > 0) {
  1036. mNextAudioClockUpdateTimeUs = 0; // start our clock updates
  1037. }
  1038. }
  1039. int64_t nowUs = ALooper::GetNowUs();
  1040. if (mNextAudioClockUpdateTimeUs >= 0) {
  1041. if (nowUs >= mNextAudioClockUpdateTimeUs) {
  1042. int64_t nowMediaUs = mediaTimeUs - getPendingAudioPlayoutDurationUs(nowUs);
  1043. mMediaClock->updateAnchor(nowMediaUs, nowUs, mediaTimeUs);
  1044. mUseVirtualAudioSink = false;
  1045. mNextAudioClockUpdateTimeUs = nowUs + kMinimumAudioClockUpdatePeriodUs;
  1046. }
  1047. } else {
  1048. int64_t unused;
  1049. if ((mMediaClock->getMediaTime(nowUs, &unused) != OK)
  1050. && (getDurationUsIfPlayedAtSampleRate(mNumFramesWritten)
  1051. > kMaxAllowedAudioSinkDelayUs)) {
  1052. // Enough data has been sent to AudioSink, but AudioSink has not rendered
  1053. // any data yet. Something is wrong with AudioSink, e.g., the device is not
  1054. // connected to audio out.
  1055. // Switch to system clock. This essentially creates a virtual AudioSink with
  1056. // initial latenty of getDurationUsIfPlayedAtSampleRate(mNumFramesWritten).
  1057. // This virtual AudioSink renders audio data starting from the very first sample
  1058. // and it's paced by system clock.
  1059. ALOGW("AudioSink stuck. ARE YOU CONNECTED TO AUDIO OUT? Switching to system clock.");
  1060. mMediaClock->updateAnchor(mAudioFirstAnchorTimeMediaUs, nowUs, mediaTimeUs);
  1061. mUseVirtualAudioSink = true;
  1062. }
  1063. }
  1064. mAnchorNumFramesWritten = mNumFramesWritten;
  1065. mAnchorTimeMediaUs = mediaTimeUs;
  1066. }
  1067. // Called without mLock acquired.
  1068. void NuPlayer2::Renderer::postDrainVideoQueue() {
  1069. if (mDrainVideoQueuePending
  1070. || getSyncQueues()
  1071. || (mPaused && mVideoSampleReceived)) {
  1072. return;
  1073. }
  1074. if (mVideoQueue.empty()) {
  1075. return;
  1076. }
  1077. QueueEntry &entry = *mVideoQueue.begin();
  1078. sp<AMessage> msg = new AMessage(kWhatDrainVideoQueue, this);
  1079. msg->setInt32("drainGeneration", getDrainGeneration(false /* audio */));
  1080. if (entry.mBuffer == NULL) {
  1081. // EOS doesn't carry a timestamp.
  1082. msg->post();
  1083. mDrainVideoQueuePending = true;
  1084. return;
  1085. }
  1086. int64_t nowUs = ALooper::GetNowUs();
  1087. if (mFlags & FLAG_REAL_TIME) {
  1088. int64_t realTimeUs;
  1089. CHECK(entry.mBuffer->meta()->findInt64("timeUs", &realTimeUs));
  1090. realTimeUs = mVideoScheduler->schedule(realTimeUs * 1000) / 1000;
  1091. int64_t twoVsyncsUs = 2 * (mVideoScheduler->getVsyncPeriod() / 1000);
  1092. int64_t delayUs = realTimeUs - nowUs;
  1093. ALOGW_IF(delayUs > 500000, "unusually high delayUs: %lld", (long long)delayUs);
  1094. // post 2 display refreshes before rendering is due
  1095. msg->post(delayUs > twoVsyncsUs ? delayUs - twoVsyncsUs : 0);
  1096. mDrainVideoQueuePending = true;
  1097. return;
  1098. }
  1099. int64_t mediaTimeUs;
  1100. CHECK(entry.mBuffer->meta()->findInt64("timeUs", &mediaTimeUs));
  1101. {
  1102. Mutex::Autolock autoLock(mLock);
  1103. if (mAnchorTimeMediaUs < 0) {
  1104. mMediaClock->updateAnchor(mediaTimeUs, nowUs, mediaTimeUs);
  1105. mAnchorTimeMediaUs = mediaTimeUs;
  1106. }
  1107. }
  1108. mNextVideoTimeMediaUs = mediaTimeUs;
  1109. if (!mHasAudio) {
  1110. // smooth out videos >= 10fps
  1111. mMediaClock->updateMaxTimeMedia(mediaTimeUs + kDefaultVideoFrameIntervalUs);
  1112. }
  1113. if (!mVideoSampleReceived || mediaTimeUs < mAudioFirstAnchorTimeMediaUs) {
  1114. msg->post();
  1115. } else {
  1116. int64_t twoVsyncsUs = 2 * (mVideoScheduler->getVsyncPeriod() / 1000);
  1117. // post 2 display refreshes before rendering is due
  1118. mMediaClock->addTimer(msg, mediaTimeUs, -twoVsyncsUs);
  1119. }
  1120. mDrainVideoQueuePending = true;
  1121. }
  1122. void NuPlayer2::Renderer::onDrainVideoQueue() {
  1123. if (mVideoQueue.empty()) {
  1124. return;
  1125. }
  1126. QueueEntry *entry = &*mVideoQueue.begin();
  1127. if (entry->mBuffer == NULL) {
  1128. // EOS
  1129. notifyEOS(false /* audio */, entry->mFinalResult);
  1130. mVideoQueue.erase(mVideoQueue.begin());
  1131. entry = NULL;
  1132. setVideoLateByUs(0);
  1133. return;
  1134. }
  1135. int64_t nowUs = ALooper::GetNowUs();
  1136. int64_t realTimeUs;
  1137. int64_t mediaTimeUs = -1;
  1138. if (mFlags & FLAG_REAL_TIME) {
  1139. CHECK(entry->mBuffer->meta()->findInt64("timeUs", &realTimeUs));
  1140. } else {
  1141. CHECK(entry->mBuffer->meta()->findInt64("timeUs", &mediaTimeUs));
  1142. realTimeUs = getRealTimeUs(mediaTimeUs, nowUs);
  1143. }
  1144. realTimeUs = mVideoScheduler->schedule(realTimeUs * 1000) / 1000;
  1145. bool tooLate = false;
  1146. if (!mPaused) {
  1147. setVideoLateByUs(nowUs - realTimeUs);
  1148. tooLate = (mVideoLateByUs > 40000);
  1149. if (tooLate) {
  1150. ALOGV("video late by %lld us (%.2f secs)",
  1151. (long long)mVideoLateByUs, mVideoLateByUs / 1E6);
  1152. } else {
  1153. int64_t mediaUs = 0;
  1154. mMediaClock->getMediaTime(realTimeUs, &mediaUs);
  1155. ALOGV("rendering video at media time %.2f secs",
  1156. (mFlags & FLAG_REAL_TIME ? realTimeUs :
  1157. mediaUs) / 1E6);
  1158. if (!(mFlags & FLAG_REAL_TIME)
  1159. && mLastAudioMediaTimeUs != -1
  1160. && mediaTimeUs > mLastAudioMediaTimeUs) {
  1161. // If audio ends before video, video continues to drive media clock.
  1162. // Also smooth out videos >= 10fps.
  1163. mMediaClock->updateMaxTimeMedia(mediaTimeUs + kDefaultVideoFrameIntervalUs);
  1164. }
  1165. }
  1166. } else {
  1167. setVideoLateByUs(0);
  1168. if (!mVideoSampleReceived && !mHasAudio) {
  1169. // This will ensure that the first frame after a flush won't be used as anchor
  1170. // when renderer is in paused state, because resume can happen any time after seek.
  1171. clearAnchorTime();
  1172. }
  1173. }
  1174. // Always render the first video frame while keeping stats on A/V sync.
  1175. if (!mVideoSampleReceived) {
  1176. realTimeUs = nowUs;
  1177. tooLate = false;
  1178. }
  1179. entry->mNotifyConsumed->setInt64("timestampNs", realTimeUs * 1000LL);
  1180. entry->mNotifyConsumed->setInt32("render", !tooLate);
  1181. entry->mNotifyConsumed->post();
  1182. mVideoQueue.erase(mVideoQueue.begin());
  1183. entry = NULL;
  1184. mVideoSampleReceived = true;
  1185. if (!mPaused) {
  1186. if (!mVideoRenderingStarted) {
  1187. mVideoRenderingStarted = true;
  1188. notifyVideoRenderingStart();
  1189. }
  1190. Mutex::Autolock autoLock(mLock);
  1191. notifyIfMediaRenderingStarted_l();
  1192. }
  1193. }
  1194. void NuPlayer2::Renderer::notifyVideoRenderingStart() {
  1195. sp<AMessage> notify = mNotify->dup();
  1196. notify->setInt32("what", kWhatVideoRenderingStart);
  1197. notify->post();
  1198. }
  1199. void NuPlayer2::Renderer::notifyEOS(bool audio, status_t finalResult, int64_t delayUs) {
  1200. Mutex::Autolock autoLock(mLock);
  1201. notifyEOS_l(audio, finalResult, delayUs);
  1202. }
  1203. void NuPlayer2::Renderer::notifyEOS_l(bool audio, status_t finalResult, int64_t delayUs) {
  1204. if (audio && delayUs > 0) {
  1205. sp<AMessage> msg = new AMessage(kWhatEOS, this);
  1206. msg->setInt32("audioEOSGeneration", mAudioEOSGeneration);
  1207. msg->setInt32("finalResult", finalResult);
  1208. msg->post(delayUs);
  1209. return;
  1210. }
  1211. sp<AMessage> notify = mNotify->dup();
  1212. notify->setInt32("what", kWhatEOS);
  1213. notify->setInt32("audio", static_cast<int32_t>(audio));
  1214. notify->setInt32("finalResult", finalResult);
  1215. notify->post(delayUs);
  1216. if (audio) {
  1217. // Video might outlive audio. Clear anchor to enable video only case.
  1218. mAnchorTimeMediaUs = -1;
  1219. mHasAudio = false;
  1220. if (mNextVideoTimeMediaUs >= 0) {
  1221. int64_t mediaUs = 0;
  1222. int64_t nowUs = ALooper::GetNowUs();
  1223. status_t result = mMediaClock->getMediaTime(nowUs, &mediaUs);
  1224. if (result == OK) {
  1225. if (mNextVideoTimeMediaUs > mediaUs) {
  1226. mMediaClock->updateMaxTimeMedia(mNextVideoTimeMediaUs);
  1227. }
  1228. } else {
  1229. mMediaClock->updateAnchor(
  1230. mNextVideoTimeMediaUs, nowUs,
  1231. mNextVideoTimeMediaUs + kDefaultVideoFrameIntervalUs);
  1232. }
  1233. }
  1234. }
  1235. }
  1236. void NuPlayer2::Renderer::notifyAudioTearDown(AudioTearDownReason reason) {
  1237. sp<AMessage> msg = new AMessage(kWhatAudioTearDown, this);
  1238. msg->setInt32("reason", reason);
  1239. msg->post();
  1240. }
  1241. void NuPlayer2::Renderer::onQueueBuffer(const sp<AMessage> &msg) {
  1242. int32_t audio;
  1243. CHECK(msg->findInt32("audio", &audio));
  1244. if (dropBufferIfStale(audio, msg)) {
  1245. return;
  1246. }
  1247. if (audio) {
  1248. mHasAudio = true;
  1249. } else {
  1250. mHasVideo = true;
  1251. }
  1252. if (mHasVideo) {
  1253. if (mVideoScheduler == NULL) {
  1254. mVideoScheduler = new VideoFrameScheduler2();
  1255. mVideoScheduler->init();
  1256. }
  1257. }
  1258. sp<RefBase> obj;
  1259. CHECK(msg->findObject("buffer", &obj));
  1260. sp<MediaCodecBuffer> buffer = static_cast<MediaCodecBuffer *>(obj.get());
  1261. sp<AMessage> notifyConsumed;
  1262. CHECK(msg->findMessage("notifyConsumed", &notifyConsumed));
  1263. QueueEntry entry;
  1264. entry.mBuffer = buffer;
  1265. entry.mNotifyConsumed = notifyConsumed;
  1266. entry.mOffset = 0;
  1267. entry.mFinalResult = OK;
  1268. entry.mBufferOrdinal = ++mTotalBuffersQueued;
  1269. if (audio) {
  1270. Mutex::Autolock autoLock(mLock);
  1271. mAudioQueue.push_back(entry);
  1272. postDrainAudioQueue_l();
  1273. } else {
  1274. mVideoQueue.push_back(entry);
  1275. postDrainVideoQueue();
  1276. }
  1277. Mutex::Autolock autoLock(mLock);
  1278. if (!mSyncQueues || mAudioQueue.empty() || mVideoQueue.empty()) {
  1279. return;
  1280. }
  1281. sp<MediaCodecBuffer> firstAudioBuffer = (*mAudioQueue.begin()).mBuffer;
  1282. sp<MediaCodecBuffer> firstVideoBuffer = (*mVideoQueue.begin()).mBuffer;
  1283. if (firstAudioBuffer == NULL || firstVideoBuffer == NULL) {
  1284. // EOS signalled on either queue.
  1285. syncQueuesDone_l();
  1286. return;
  1287. }
  1288. int64_t firstAudioTimeUs;
  1289. int64_t firstVideoTimeUs;
  1290. CHECK(firstAudioBuffer->meta()
  1291. ->findInt64("timeUs", &firstAudioTimeUs));
  1292. CHECK(firstVideoBuffer->meta()
  1293. ->findInt64("timeUs", &firstVideoTimeUs));
  1294. int64_t diff = firstVideoTimeUs - firstAudioTimeUs;
  1295. ALOGV("queueDiff = %.2f secs", diff / 1E6);
  1296. if (diff > 100000LL) {
  1297. // Audio data starts More than 0.1 secs before video.
  1298. // Drop some audio.
  1299. (*mAudioQueue.begin()).mNotifyConsumed->post();
  1300. mAudioQueue.erase(mAudioQueue.begin());
  1301. return;
  1302. }
  1303. syncQueuesDone_l();
  1304. }
  1305. void NuPlayer2::Renderer::syncQueuesDone_l() {
  1306. if (!mSyncQueues) {
  1307. return;
  1308. }
  1309. mSyncQueues = false;
  1310. if (!mAudioQueue.empty()) {
  1311. postDrainAudioQueue_l();
  1312. }
  1313. if (!mVideoQueue.empty()) {
  1314. mLock.unlock();
  1315. postDrainVideoQueue();
  1316. mLock.lock();
  1317. }
  1318. }
  1319. void NuPlayer2::Renderer::onQueueEOS(const sp<AMessage> &msg) {
  1320. int32_t audio;
  1321. CHECK(msg->findInt32("audio", &audio));
  1322. if (dropBufferIfStale(audio, msg)) {
  1323. return;
  1324. }
  1325. int32_t finalResult;
  1326. CHECK(msg->findInt32("finalResult", &finalResult));
  1327. QueueEntry entry;
  1328. entry.mOffset = 0;
  1329. entry.mFinalResult = finalResult;
  1330. if (audio) {
  1331. Mutex::Autolock autoLock(mLock);
  1332. if (mAudioQueue.empty() && mSyncQueues) {
  1333. syncQueuesDone_l();
  1334. }
  1335. mAudioQueue.push_back(entry);
  1336. postDrainAudioQueue_l();
  1337. } else {
  1338. if (mVideoQueue.empty() && getSyncQueues()) {
  1339. Mutex::Autolock autoLock(mLock);
  1340. syncQueuesDone_l();
  1341. }
  1342. mVideoQueue.push_back(entry);
  1343. postDrainVideoQueue();
  1344. }
  1345. }
  1346. void NuPlayer2::Renderer::onFlush(const sp<AMessage> &msg) {
  1347. int32_t audio, notifyComplete;
  1348. CHECK(msg->findInt32("audio", &audio));
  1349. {
  1350. Mutex::Autolock autoLock(mLock);
  1351. if (audio) {
  1352. notifyComplete = mNotifyCompleteAudio;
  1353. mNotifyCompleteAudio = false;
  1354. mLastAudioMediaTimeUs = -1;
  1355. mHasAudio = false;
  1356. if (mNextVideoTimeMediaUs >= 0) {
  1357. int64_t nowUs = ALooper::GetNowUs();
  1358. mMediaClock->updateAnchor(
  1359. mNextVideoTimeMediaUs, nowUs,
  1360. mNextVideoTimeMediaUs + kDefaultVideoFrameIntervalUs);
  1361. }
  1362. } else {
  1363. notifyComplete = mNotifyCompleteVideo;
  1364. mNotifyCompleteVideo = false;
  1365. mVideoRenderingStarted = false;
  1366. }
  1367. // If we're currently syncing the queues, i.e. dropping audio while
  1368. // aligning the first audio/video buffer times and only one of the
  1369. // two queues has data, we may starve that queue by not requesting
  1370. // more buffers from the decoder. If the other source then encounters
  1371. // a discontinuity that leads to flushing, we'll never find the
  1372. // corresponding discontinuity on the other queue.
  1373. // Therefore we'll stop syncing the queues if at least one of them
  1374. // is flushed.
  1375. syncQueuesDone_l();
  1376. }
  1377. clearAnchorTime();
  1378. ALOGV("flushing %s", audio ? "audio" : "video");
  1379. if (audio) {
  1380. {
  1381. Mutex::Autolock autoLock(mLock);
  1382. flushQueue(&mAudioQueue);
  1383. ++mAudioDrainGeneration;
  1384. ++mAudioEOSGeneration;
  1385. prepareForMediaRenderingStart_l();
  1386. // the frame count will be reset after flush.
  1387. clearAudioFirstAnchorTime_l();
  1388. }
  1389. mDrainAudioQueuePending = false;
  1390. if (offloadingAudio()) {
  1391. mAudioSink->pause();
  1392. mAudioSink->flush();
  1393. if (!mPaused) {
  1394. mAudioSink->start();
  1395. }
  1396. } else {
  1397. mAudioSink->pause();
  1398. mAudioSink->flush();
  1399. // Call stop() to signal to the AudioSink to completely fill the
  1400. // internal buffer before resuming playback.
  1401. // FIXME: this is ignored after flush().
  1402. mAudioSink->stop();
  1403. if (mPaused) {
  1404. // Race condition: if renderer is paused and audio sink is stopped,
  1405. // we need to make sure that the audio track buffer fully drains
  1406. // before delivering data.
  1407. // FIXME: remove this if we can detect if stop() is complete.
  1408. const int delayUs = 2 * 50 * 1000; // (2 full mixer thread cycles at 50ms)
  1409. mPauseDrainAudioAllowedUs = ALooper::GetNowUs() + delayUs;
  1410. } else {
  1411. mAudioSink->start();
  1412. }
  1413. mNumFramesWritten = 0;
  1414. }
  1415. mNextAudioClockUpdateTimeUs = -1;
  1416. } else {
  1417. flushQueue(&mVideoQueue);
  1418. mDrainVideoQueuePending = false;
  1419. if (mVideoScheduler != NULL) {
  1420. mVideoScheduler->restart();
  1421. }
  1422. Mutex::Autolock autoLock(mLock);
  1423. ++mVideoDrainGeneration;
  1424. prepareForMediaRenderingStart_l();
  1425. }
  1426. mVideoSampleReceived = false;
  1427. if (notifyComplete) {
  1428. notifyFlushComplete(audio);
  1429. }
  1430. }
  1431. void NuPlayer2::Renderer::flushQueue(List<QueueEntry> *queue) {
  1432. while (!queue->empty()) {
  1433. QueueEntry *entry = &*queue->begin();
  1434. if (entry->mBuffer != NULL) {
  1435. entry->mNotifyConsumed->post();
  1436. } else if (entry->mNotifyConsumed != nullptr) {
  1437. // Is it needed to open audio sink now?
  1438. onChangeAudioFormat(entry->mMeta, entry->mNotifyConsumed);
  1439. }
  1440. queue->erase(queue->begin());
  1441. entry = NULL;
  1442. }
  1443. }
  1444. void NuPlayer2::Renderer::notifyFlushComplete(bool audio) {
  1445. sp<AMessage> notify = mNotify->dup();
  1446. notify->setInt32("what", kWhatFlushComplete);
  1447. notify->setInt32("audio", static_cast<int32_t>(audio));
  1448. notify->post();
  1449. }
  1450. bool NuPlayer2::Renderer::dropBufferIfStale(
  1451. bool audio, const sp<AMessage> &msg) {
  1452. int32_t queueGeneration;
  1453. CHECK(msg->findInt32("queueGeneration", &queueGeneration));
  1454. if (queueGeneration == getQueueGeneration(audio)) {
  1455. return false;
  1456. }
  1457. sp<AMessage> notifyConsumed;
  1458. if (msg->findMessage("notifyConsumed", &notifyConsumed)) {
  1459. notifyConsumed->post();
  1460. }
  1461. return true;
  1462. }
  1463. void NuPlayer2::Renderer::onAudioSinkChanged() {
  1464. if (offloadingAudio()) {
  1465. return;
  1466. }
  1467. CHECK(!mDrainAudioQueuePending);
  1468. mNumFramesWritten = 0;
  1469. mAnchorNumFramesWritten = -1;
  1470. uint32_t written;
  1471. if (mAudioSink->getFramesWritten(&written) == OK) {
  1472. mNumFramesWritten = written;
  1473. }
  1474. }
  1475. void NuPlayer2::Renderer::onDisableOffloadAudio() {
  1476. Mutex::Autolock autoLock(mLock);
  1477. mFlags &= ~FLAG_OFFLOAD_AUDIO;
  1478. ++mAudioDrainGeneration;
  1479. if (mAudioRenderingStartGeneration != -1) {
  1480. prepareForMediaRenderingStart_l();
  1481. }
  1482. }
  1483. void NuPlayer2::Renderer::onEnableOffloadAudio() {
  1484. Mutex::Autolock autoLock(mLock);
  1485. mFlags |= FLAG_OFFLOAD_AUDIO;
  1486. ++mAudioDrainGeneration;
  1487. if (mAudioRenderingStartGeneration != -1) {
  1488. prepareForMediaRenderingStart_l();
  1489. }
  1490. }
  1491. void NuPlayer2::Renderer::onPause() {
  1492. if (mPaused) {
  1493. return;
  1494. }
  1495. {
  1496. Mutex::Autolock autoLock(mLock);
  1497. // we do not increment audio drain generation so that we fill audio buffer during pause.
  1498. ++mVideoDrainGeneration;
  1499. prepareForMediaRenderingStart_l();
  1500. mPaused = true;
  1501. mMediaClock->setPlaybackRate(0.0);
  1502. }
  1503. mDrainAudioQueuePending = false;
  1504. mDrainVideoQueuePending = false;
  1505. // Note: audio data may not have been decoded, and the AudioSink may not be opened.
  1506. mAudioSink->pause();
  1507. startAudioOffloadPauseTimeout();
  1508. ALOGV("now paused audio queue has %zu entries, video has %zu entries",
  1509. mAudioQueue.size(), mVideoQueue.size());
  1510. }
  1511. void NuPlayer2::Renderer::onResume() {
  1512. if (!mPaused) {
  1513. return;
  1514. }
  1515. // Note: audio data may not have been decoded, and the AudioSink may not be opened.
  1516. cancelAudioOffloadPauseTimeout();
  1517. if (mAudioSink->ready()) {
  1518. status_t err = mAudioSink->start();
  1519. if (err != OK) {
  1520. ALOGE("cannot start AudioSink err %d", err);
  1521. notifyAudioTearDown(kDueToError);
  1522. }
  1523. }
  1524. {
  1525. Mutex::Autolock autoLock(mLock);
  1526. mPaused = false;
  1527. // rendering started message may have been delayed if we were paused.
  1528. if (mRenderingDataDelivered) {
  1529. notifyIfMediaRenderingStarted_l();
  1530. }
  1531. // configure audiosink as we did not do it when pausing
  1532. if (mAudioSink != NULL && mAudioSink->ready()) {
  1533. mAudioSink->setPlaybackRate(mPlaybackSettings);
  1534. }
  1535. mMediaClock->setPlaybackRate(mPlaybackSettings.mSpeed);
  1536. if (!mAudioQueue.empty()) {
  1537. postDrainAudioQueue_l();
  1538. }
  1539. }
  1540. if (!mVideoQueue.empty()) {
  1541. postDrainVideoQueue();
  1542. }
  1543. }
  1544. void NuPlayer2::Renderer::onSetVideoFrameRate(float fps) {
  1545. if (mVideoScheduler == NULL) {
  1546. mVideoScheduler = new VideoFrameScheduler2();
  1547. }
  1548. mVideoScheduler->init(fps);
  1549. }
  1550. int32_t NuPlayer2::Renderer::getQueueGeneration(bool audio) {
  1551. Mutex::Autolock autoLock(mLock);
  1552. return (audio ? mAudioQueueGeneration : mVideoQueueGeneration);
  1553. }
  1554. int32_t NuPlayer2::Renderer::getDrainGeneration(bool audio) {
  1555. Mutex::Autolock autoLock(mLock);
  1556. return (audio ? mAudioDrainGeneration : mVideoDrainGeneration);
  1557. }
  1558. bool NuPlayer2::Renderer::getSyncQueues() {
  1559. Mutex::Autolock autoLock(mLock);
  1560. return mSyncQueues;
  1561. }
  1562. void NuPlayer2::Renderer::onAudioTearDown(AudioTearDownReason reason) {
  1563. if (mAudioTornDown) {
  1564. return;
  1565. }
  1566. mAudioTornDown = true;
  1567. int64_t currentPositionUs;
  1568. sp<AMessage> notify = mNotify->dup();
  1569. if (getCurrentPosition(&currentPositionUs) == OK) {
  1570. notify->setInt64("positionUs", currentPositionUs);
  1571. }
  1572. mAudioSink->stop();
  1573. mAudioSink->flush();
  1574. notify->setInt32("what", kWhatAudioTearDown);
  1575. notify->setInt32("reason", reason);
  1576. notify->post();
  1577. }
  1578. void NuPlayer2::Renderer::startAudioOffloadPauseTimeout() {
  1579. if (offloadingAudio()) {
  1580. mWakeLock->acquire();
  1581. sp<AMessage> msg = new AMessage(kWhatAudioOffloadPauseTimeout, this);
  1582. msg->setInt32("drainGeneration", mAudioOffloadPauseTimeoutGeneration);
  1583. msg->post(kOffloadPauseMaxUs);
  1584. }
  1585. }
  1586. void NuPlayer2::Renderer::cancelAudioOffloadPauseTimeout() {
  1587. // We may have called startAudioOffloadPauseTimeout() without
  1588. // the AudioSink open and with offloadingAudio enabled.
  1589. //
  1590. // When we cancel, it may be that offloadingAudio is subsequently disabled, so regardless
  1591. // we always release the wakelock and increment the pause timeout generation.
  1592. //
  1593. // Note: The acquired wakelock prevents the device from suspending
  1594. // immediately after offload pause (in case a resume happens shortly thereafter).
  1595. mWakeLock->release(true);
  1596. ++mAudioOffloadPauseTimeoutGeneration;
  1597. }
  1598. status_t NuPlayer2::Renderer::onOpenAudioSink(
  1599. const sp<AMessage> &format,
  1600. bool offloadOnly,
  1601. bool hasVideo,
  1602. uint32_t flags,
  1603. bool isStreaming) {
  1604. ALOGV("openAudioSink: offloadOnly(%d) offloadingAudio(%d)",
  1605. offloadOnly, offloadingAudio());
  1606. bool audioSinkChanged = false;
  1607. int32_t numChannels;
  1608. CHECK(format->findInt32("channel-count", &numChannels));
  1609. int32_t channelMask;
  1610. if (!format->findInt32("channel-mask", &channelMask)) {
  1611. // signal to the AudioSink to derive the mask from count.
  1612. channelMask = CHANNEL_MASK_USE_CHANNEL_ORDER;
  1613. }
  1614. int32_t sampleRate;
  1615. CHECK(format->findInt32("sample-rate", &sampleRate));
  1616. // read pcm encoding from MediaCodec output format, if available
  1617. int32_t pcmEncoding;
  1618. audio_format_t audioFormat =
  1619. format->findInt32(KEY_PCM_ENCODING, &pcmEncoding) ?
  1620. audioFormatFromEncoding(pcmEncoding) : AUDIO_FORMAT_PCM_16_BIT;
  1621. if (offloadingAudio()) {
  1622. AString mime;
  1623. CHECK(format->findString("mime", &mime));
  1624. status_t err = mapMimeToAudioFormat(audioFormat, mime.c_str());
  1625. if (err != OK) {
  1626. ALOGE("Couldn't map mime \"%s\" to a valid "
  1627. "audio_format", mime.c_str());
  1628. onDisableOffloadAudio();
  1629. } else {
  1630. ALOGV("Mime \"%s\" mapped to audio_format 0x%x",
  1631. mime.c_str(), audioFormat);
  1632. int avgBitRate = -1;
  1633. format->findInt32("bitrate", &avgBitRate);
  1634. int32_t aacProfile = -1;
  1635. if (audioFormat == AUDIO_FORMAT_AAC
  1636. && format->findInt32("aac-profile", &aacProfile)) {
  1637. // Redefine AAC format as per aac profile
  1638. mapAACProfileToAudioFormat(
  1639. audioFormat,
  1640. aacProfile);
  1641. }
  1642. audio_offload_info_t offloadInfo = AUDIO_INFO_INITIALIZER;
  1643. offloadInfo.duration_us = -1;
  1644. format->findInt64(
  1645. "durationUs", &offloadInfo.duration_us);
  1646. offloadInfo.sample_rate = sampleRate;
  1647. offloadInfo.channel_mask = channelMask;
  1648. offloadInfo.format = audioFormat;
  1649. offloadInfo.stream_type = AUDIO_STREAM_MUSIC;
  1650. offloadInfo.bit_rate = avgBitRate;
  1651. offloadInfo.has_video = hasVideo;
  1652. offloadInfo.is_streaming = isStreaming;
  1653. if (memcmp(&mCurrentOffloadInfo, &offloadInfo, sizeof(offloadInfo)) == 0) {
  1654. ALOGV("openAudioSink: no change in offload mode");
  1655. // no change from previous configuration, everything ok.
  1656. return OK;
  1657. }
  1658. mCurrentPcmInfo = AUDIO_PCMINFO_INITIALIZER;
  1659. ALOGV("openAudioSink: try to open AudioSink in offload mode");
  1660. uint32_t offloadFlags = flags;
  1661. offloadFlags |= AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD;
  1662. offloadFlags &= ~AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
  1663. audioSinkChanged = true;
  1664. mAudioSink->close();
  1665. err = mAudioSink->open(
  1666. sampleRate,
  1667. numChannels,
  1668. (audio_channel_mask_t)channelMask,
  1669. audioFormat,
  1670. &NuPlayer2::Renderer::AudioSinkCallback,
  1671. this,
  1672. (audio_output_flags_t)offloadFlags,
  1673. &offloadInfo);
  1674. if (err == OK) {
  1675. err = mAudioSink->setPlaybackRate(mPlaybackSettings);
  1676. }
  1677. if (err == OK) {
  1678. // If the playback is offloaded to h/w, we pass
  1679. // the HAL some metadata information.
  1680. // We don't want to do this for PCM because it
  1681. // will be going through the AudioFlinger mixer
  1682. // before reaching the hardware.
  1683. // TODO
  1684. mCurrentOffloadInfo = offloadInfo;
  1685. if (!mPaused) { // for preview mode, don't start if paused
  1686. err = mAudioSink->start();
  1687. }
  1688. ALOGV_IF(err == OK, "openAudioSink: offload succeeded");
  1689. }
  1690. if (err != OK) {
  1691. // Clean up, fall back to non offload mode.
  1692. mAudioSink->close();
  1693. onDisableOffloadAudio();
  1694. mCurrentOffloadInfo = AUDIO_INFO_INITIALIZER;
  1695. ALOGV("openAudioSink: offload failed");
  1696. if (offloadOnly) {
  1697. notifyAudioTearDown(kForceNonOffload);
  1698. }
  1699. } else {
  1700. mUseAudioCallback = true; // offload mode transfers data through callback
  1701. ++mAudioDrainGeneration; // discard pending kWhatDrainAudioQueue message.
  1702. }
  1703. }
  1704. }
  1705. if (!offloadOnly && !offloadingAudio()) {
  1706. ALOGV("openAudioSink: open AudioSink in NON-offload mode");
  1707. uint32_t pcmFlags = flags;
  1708. pcmFlags &= ~AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD;
  1709. const PcmInfo info = {
  1710. (audio_channel_mask_t)channelMask,
  1711. (audio_output_flags_t)pcmFlags,
  1712. audioFormat,
  1713. numChannels,
  1714. sampleRate
  1715. };
  1716. if (memcmp(&mCurrentPcmInfo, &info, sizeof(info)) == 0) {
  1717. ALOGV("openAudioSink: no change in pcm mode");
  1718. // no change from previous configuration, everything ok.
  1719. return OK;
  1720. }
  1721. audioSinkChanged = true;
  1722. mAudioSink->close();
  1723. mCurrentOffloadInfo = AUDIO_INFO_INITIALIZER;
  1724. // Note: It is possible to set up the callback, but not use it to send audio data.
  1725. // This requires a fix in AudioSink to explicitly specify the transfer mode.
  1726. mUseAudioCallback = getUseAudioCallbackSetting();
  1727. if (mUseAudioCallback) {
  1728. ++mAudioDrainGeneration; // discard pending kWhatDrainAudioQueue message.
  1729. }
  1730. // Compute the desired buffer size.
  1731. // For callback mode, the amount of time before wakeup is about half the buffer size.
  1732. const uint32_t frameCount =
  1733. (unsigned long long)sampleRate * getAudioSinkPcmMsSetting() / 1000;
  1734. // We should always be able to set our playback settings if the sink is closed.
  1735. LOG_ALWAYS_FATAL_IF(mAudioSink->setPlaybackRate(mPlaybackSettings) != OK,
  1736. "onOpenAudioSink: can't set playback rate on closed sink");
  1737. status_t err = mAudioSink->open(
  1738. sampleRate,
  1739. numChannels,
  1740. (audio_channel_mask_t)channelMask,
  1741. audioFormat,
  1742. mUseAudioCallback ? &NuPlayer2::Renderer::AudioSinkCallback : NULL,
  1743. mUseAudioCallback ? this : NULL,
  1744. (audio_output_flags_t)pcmFlags,
  1745. NULL,
  1746. frameCount);
  1747. if (err != OK) {
  1748. ALOGW("openAudioSink: non offloaded open failed status: %d", err);
  1749. mAudioSink->close();
  1750. mCurrentPcmInfo = AUDIO_PCMINFO_INITIALIZER;
  1751. return err;
  1752. }
  1753. mCurrentPcmInfo = info;
  1754. if (!mPaused) { // for preview mode, don't start if paused
  1755. mAudioSink->start();
  1756. }
  1757. }
  1758. if (audioSinkChanged) {
  1759. onAudioSinkChanged();
  1760. }
  1761. mAudioTornDown = false;
  1762. return OK;
  1763. }
  1764. void NuPlayer2::Renderer::onCloseAudioSink() {
  1765. mAudioSink->close();
  1766. mCurrentOffloadInfo = AUDIO_INFO_INITIALIZER;
  1767. mCurrentPcmInfo = AUDIO_PCMINFO_INITIALIZER;
  1768. }
  1769. void NuPlayer2::Renderer::onChangeAudioFormat(
  1770. const sp<AMessage> &meta, const sp<AMessage> &notify) {
  1771. sp<AMessage> format;
  1772. CHECK(meta->findMessage("format", &format));
  1773. int32_t offloadOnly;
  1774. CHECK(meta->findInt32("offload-only", &offloadOnly));
  1775. int32_t hasVideo;
  1776. CHECK(meta->findInt32("has-video", &hasVideo));
  1777. uint32_t flags;
  1778. CHECK(meta->findInt32("flags", (int32_t *)&flags));
  1779. uint32_t isStreaming;
  1780. CHECK(meta->findInt32("isStreaming", (int32_t *)&isStreaming));
  1781. status_t err = onOpenAudioSink(format, offloadOnly, hasVideo, flags, isStreaming);
  1782. if (err != OK) {
  1783. notify->setInt32("err", err);
  1784. }
  1785. notify->post();
  1786. }
  1787. } // namespace android