MediaPlayer2AudioOutput.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  1. /*
  2. **
  3. ** Copyright 2018, The Android Open Source Project
  4. **
  5. ** Licensed under the Apache License, Version 2.0 (the "License");
  6. ** you may not use this file except in compliance with the License.
  7. ** You may obtain a copy of the License at
  8. **
  9. ** http://www.apache.org/licenses/LICENSE-2.0
  10. **
  11. ** Unless required by applicable law or agreed to in writing, software
  12. ** distributed under the License is distributed on an "AS IS" BASIS,
  13. ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. ** See the License for the specific language governing permissions and
  15. ** limitations under the License.
  16. */
  17. //#define LOG_NDEBUG 0
  18. #define LOG_TAG "MediaPlayer2AudioOutput"
  19. #include <mediaplayer2/MediaPlayer2AudioOutput.h>
  20. #include <cutils/properties.h> // for property_get
  21. #include <utils/Log.h>
  22. #include <media/stagefright/foundation/ADebug.h>
  23. namespace {
  24. const float kMaxRequiredSpeed = 8.0f; // for PCM tracks allow up to 8x speedup.
  25. } // anonymous namespace
  26. namespace android {
  27. // TODO: Find real cause of Audio/Video delay in PV framework and remove this workaround
  28. /* static */ int MediaPlayer2AudioOutput::mMinBufferCount = 4;
  29. /* static */ bool MediaPlayer2AudioOutput::mIsOnEmulator = false;
  30. status_t MediaPlayer2AudioOutput::dump(int fd, const Vector<String16>& args) const {
  31. const size_t SIZE = 256;
  32. char buffer[SIZE];
  33. String8 result;
  34. result.append(" MediaPlayer2AudioOutput\n");
  35. snprintf(buffer, 255, " volume(%f)\n", mVolume);
  36. result.append(buffer);
  37. snprintf(buffer, 255, " msec per frame(%f), latency (%d)\n",
  38. mMsecsPerFrame, (mJAudioTrack != nullptr) ? mJAudioTrack->latency() : -1);
  39. result.append(buffer);
  40. snprintf(buffer, 255, " aux effect id(%d), send level (%f)\n",
  41. mAuxEffectId, mSendLevel);
  42. result.append(buffer);
  43. ::write(fd, result.string(), result.size());
  44. if (mJAudioTrack != nullptr) {
  45. mJAudioTrack->dump(fd, args);
  46. }
  47. return NO_ERROR;
  48. }
  49. MediaPlayer2AudioOutput::MediaPlayer2AudioOutput(int32_t sessionId, uid_t uid, int pid,
  50. const jobject attributes)
  51. : mCallback(nullptr),
  52. mCallbackCookie(nullptr),
  53. mCallbackData(nullptr),
  54. mVolume(1.0),
  55. mPlaybackRate(AUDIO_PLAYBACK_RATE_DEFAULT),
  56. mSampleRateHz(0),
  57. mMsecsPerFrame(0),
  58. mFrameSize(0),
  59. mSessionId(sessionId),
  60. mUid(uid),
  61. mPid(pid),
  62. mSendLevel(0.0),
  63. mAuxEffectId(0),
  64. mFlags(AUDIO_OUTPUT_FLAG_NONE) {
  65. ALOGV("MediaPlayer2AudioOutput(%d)", sessionId);
  66. if (attributes != nullptr) {
  67. mAttributes = new JObjectHolder(attributes);
  68. }
  69. setMinBufferCount();
  70. mRoutingDelegates.clear();
  71. }
  72. MediaPlayer2AudioOutput::~MediaPlayer2AudioOutput() {
  73. close();
  74. delete mCallbackData;
  75. }
  76. //static
  77. void MediaPlayer2AudioOutput::setMinBufferCount() {
  78. char value[PROPERTY_VALUE_MAX];
  79. if (property_get("ro.kernel.qemu", value, 0)) {
  80. mIsOnEmulator = true;
  81. mMinBufferCount = 12; // to prevent systematic buffer underrun for emulator
  82. }
  83. }
  84. // static
  85. bool MediaPlayer2AudioOutput::isOnEmulator() {
  86. setMinBufferCount(); // benign race wrt other threads
  87. return mIsOnEmulator;
  88. }
  89. // static
  90. int MediaPlayer2AudioOutput::getMinBufferCount() {
  91. setMinBufferCount(); // benign race wrt other threads
  92. return mMinBufferCount;
  93. }
  94. ssize_t MediaPlayer2AudioOutput::bufferSize() const {
  95. Mutex::Autolock lock(mLock);
  96. if (mJAudioTrack == nullptr) {
  97. return NO_INIT;
  98. }
  99. return mJAudioTrack->frameCount() * mFrameSize;
  100. }
  101. ssize_t MediaPlayer2AudioOutput::frameCount() const {
  102. Mutex::Autolock lock(mLock);
  103. if (mJAudioTrack == nullptr) {
  104. return NO_INIT;
  105. }
  106. return mJAudioTrack->frameCount();
  107. }
  108. ssize_t MediaPlayer2AudioOutput::channelCount() const {
  109. Mutex::Autolock lock(mLock);
  110. if (mJAudioTrack == nullptr) {
  111. return NO_INIT;
  112. }
  113. return mJAudioTrack->channelCount();
  114. }
  115. ssize_t MediaPlayer2AudioOutput::frameSize() const {
  116. Mutex::Autolock lock(mLock);
  117. if (mJAudioTrack == nullptr) {
  118. return NO_INIT;
  119. }
  120. return mFrameSize;
  121. }
  122. uint32_t MediaPlayer2AudioOutput::latency () const {
  123. Mutex::Autolock lock(mLock);
  124. if (mJAudioTrack == nullptr) {
  125. return 0;
  126. }
  127. return mJAudioTrack->latency();
  128. }
  129. float MediaPlayer2AudioOutput::msecsPerFrame() const {
  130. Mutex::Autolock lock(mLock);
  131. return mMsecsPerFrame;
  132. }
  133. status_t MediaPlayer2AudioOutput::getPosition(uint32_t *position) const {
  134. Mutex::Autolock lock(mLock);
  135. if (mJAudioTrack == nullptr) {
  136. return NO_INIT;
  137. }
  138. return mJAudioTrack->getPosition(position);
  139. }
  140. status_t MediaPlayer2AudioOutput::getTimestamp(AudioTimestamp &ts) const {
  141. Mutex::Autolock lock(mLock);
  142. if (mJAudioTrack == nullptr) {
  143. return NO_INIT;
  144. }
  145. return mJAudioTrack->getTimestamp(ts);
  146. }
  147. // TODO: Remove unnecessary calls to getPlayedOutDurationUs()
  148. // as it acquires locks and may query the audio driver.
  149. //
  150. // Some calls could conceivably retrieve extrapolated data instead of
  151. // accessing getTimestamp() or getPosition() every time a data buffer with
  152. // a media time is received.
  153. //
  154. // Calculate duration of played samples if played at normal rate (i.e., 1.0).
  155. int64_t MediaPlayer2AudioOutput::getPlayedOutDurationUs(int64_t nowUs) const {
  156. Mutex::Autolock lock(mLock);
  157. if (mJAudioTrack == nullptr || mSampleRateHz == 0) {
  158. return 0;
  159. }
  160. uint32_t numFramesPlayed;
  161. int64_t numFramesPlayedAtUs;
  162. AudioTimestamp ts;
  163. status_t res = mJAudioTrack->getTimestamp(ts);
  164. if (res == OK) { // case 1: mixing audio tracks and offloaded tracks.
  165. numFramesPlayed = ts.mPosition;
  166. numFramesPlayedAtUs = ts.mTime.tv_sec * 1000000LL + ts.mTime.tv_nsec / 1000;
  167. //ALOGD("getTimestamp: OK %d %lld", numFramesPlayed, (long long)numFramesPlayedAtUs);
  168. } else { // case 2: transitory state on start of a new track
  169. // case 3: transitory at new track or audio fast tracks.
  170. numFramesPlayed = 0;
  171. numFramesPlayedAtUs = nowUs;
  172. //ALOGD("getTimestamp: WOULD_BLOCK %d %lld",
  173. // numFramesPlayed, (long long)numFramesPlayedAtUs);
  174. }
  175. // CHECK_EQ(numFramesPlayed & (1 << 31), 0); // can't be negative until 12.4 hrs, test
  176. // TODO: remove the (int32_t) casting below as it may overflow at 12.4 hours.
  177. int64_t durationUs = (int64_t)((int32_t)numFramesPlayed * 1000000LL / mSampleRateHz)
  178. + nowUs - numFramesPlayedAtUs;
  179. if (durationUs < 0) {
  180. // Occurs when numFramesPlayed position is very small and the following:
  181. // (1) In case 1, the time nowUs is computed before getTimestamp() is called and
  182. // numFramesPlayedAtUs is greater than nowUs by time more than numFramesPlayed.
  183. // (2) In case 3, using getPosition and adding mAudioSink->latency() to
  184. // numFramesPlayedAtUs, by a time amount greater than numFramesPlayed.
  185. //
  186. // Both of these are transitory conditions.
  187. ALOGV("getPlayedOutDurationUs: negative duration %lld set to zero", (long long)durationUs);
  188. durationUs = 0;
  189. }
  190. ALOGV("getPlayedOutDurationUs(%lld) nowUs(%lld) frames(%u) framesAt(%lld)",
  191. (long long)durationUs, (long long)nowUs,
  192. numFramesPlayed, (long long)numFramesPlayedAtUs);
  193. return durationUs;
  194. }
  195. status_t MediaPlayer2AudioOutput::getFramesWritten(uint32_t *frameswritten) const {
  196. Mutex::Autolock lock(mLock);
  197. if (mJAudioTrack == nullptr) {
  198. return NO_INIT;
  199. }
  200. ExtendedTimestamp ets;
  201. status_t status = mJAudioTrack->getTimestamp(&ets);
  202. if (status == OK || status == WOULD_BLOCK) {
  203. *frameswritten = (uint32_t)ets.mPosition[ExtendedTimestamp::LOCATION_CLIENT];
  204. }
  205. return status;
  206. }
  207. void MediaPlayer2AudioOutput::setAudioAttributes(const jobject attributes) {
  208. Mutex::Autolock lock(mLock);
  209. mAttributes = (attributes == nullptr) ? nullptr : new JObjectHolder(attributes);
  210. }
  211. audio_stream_type_t MediaPlayer2AudioOutput::getAudioStreamType() const {
  212. ALOGV("getAudioStreamType");
  213. Mutex::Autolock lock(mLock);
  214. if (mJAudioTrack == nullptr) {
  215. return AUDIO_STREAM_DEFAULT;
  216. }
  217. return mJAudioTrack->getAudioStreamType();
  218. }
  219. void MediaPlayer2AudioOutput::close_l() {
  220. mJAudioTrack.clear();
  221. }
  222. status_t MediaPlayer2AudioOutput::open(
  223. uint32_t sampleRate, int channelCount, audio_channel_mask_t channelMask,
  224. audio_format_t format,
  225. AudioCallback cb, void *cookie,
  226. audio_output_flags_t flags,
  227. const audio_offload_info_t *offloadInfo,
  228. uint32_t suggestedFrameCount) {
  229. ALOGV("open(%u, %d, 0x%x, 0x%x, %d 0x%x)", sampleRate, channelCount, channelMask,
  230. format, mSessionId, flags);
  231. // offloading is only supported in callback mode for now.
  232. // offloadInfo must be present if offload flag is set
  233. if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) &&
  234. ((cb == nullptr) || (offloadInfo == nullptr))) {
  235. return BAD_VALUE;
  236. }
  237. // compute frame count for the AudioTrack internal buffer
  238. const size_t frameCount =
  239. ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) ? 0 : suggestedFrameCount;
  240. if (channelMask == CHANNEL_MASK_USE_CHANNEL_ORDER) {
  241. channelMask = audio_channel_out_mask_from_count(channelCount);
  242. if (0 == channelMask) {
  243. ALOGE("open() error, can\'t derive mask for %d audio channels", channelCount);
  244. return NO_INIT;
  245. }
  246. }
  247. Mutex::Autolock lock(mLock);
  248. mCallback = cb;
  249. mCallbackCookie = cookie;
  250. sp<JAudioTrack> jT;
  251. CallbackData *newcbd = nullptr;
  252. ALOGV("creating new JAudioTrack");
  253. if (mCallback != nullptr) {
  254. newcbd = new CallbackData(this);
  255. jT = new JAudioTrack(
  256. sampleRate,
  257. format,
  258. channelMask,
  259. CallbackWrapper,
  260. newcbd,
  261. frameCount,
  262. mSessionId,
  263. mAttributes != nullptr ? mAttributes->getJObject() : nullptr,
  264. 1.0f); // default value for maxRequiredSpeed
  265. } else {
  266. // TODO: Due to buffer memory concerns, we use a max target playback speed
  267. // based on mPlaybackRate at the time of open (instead of kMaxRequiredSpeed),
  268. // also clamping the target speed to 1.0 <= targetSpeed <= kMaxRequiredSpeed.
  269. const float targetSpeed =
  270. std::min(std::max(mPlaybackRate.mSpeed, 1.0f), kMaxRequiredSpeed);
  271. ALOGW_IF(targetSpeed != mPlaybackRate.mSpeed,
  272. "track target speed:%f clamped from playback speed:%f",
  273. targetSpeed, mPlaybackRate.mSpeed);
  274. jT = new JAudioTrack(
  275. sampleRate,
  276. format,
  277. channelMask,
  278. nullptr,
  279. nullptr,
  280. frameCount,
  281. mSessionId,
  282. mAttributes != nullptr ? mAttributes->getJObject() : nullptr,
  283. targetSpeed);
  284. }
  285. if (jT == 0) {
  286. ALOGE("Unable to create audio track");
  287. delete newcbd;
  288. // t goes out of scope, so reference count drops to zero
  289. return NO_INIT;
  290. }
  291. CHECK((jT != nullptr) && ((mCallback == nullptr) || (newcbd != nullptr)));
  292. mCallbackData = newcbd;
  293. ALOGV("setVolume");
  294. jT->setVolume(mVolume);
  295. mSampleRateHz = sampleRate;
  296. mFlags = flags;
  297. mMsecsPerFrame = 1E3f / (mPlaybackRate.mSpeed * sampleRate);
  298. mFrameSize = jT->frameSize();
  299. mJAudioTrack = jT;
  300. return updateTrack_l();
  301. }
  302. status_t MediaPlayer2AudioOutput::updateTrack_l() {
  303. if (mJAudioTrack == nullptr) {
  304. return NO_ERROR;
  305. }
  306. status_t res = NO_ERROR;
  307. // Note some output devices may give us a direct track even though we don't specify it.
  308. // Example: Line application b/17459982.
  309. if ((mJAudioTrack->getFlags()
  310. & (AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD | AUDIO_OUTPUT_FLAG_DIRECT)) == 0) {
  311. res = mJAudioTrack->setPlaybackRate(mPlaybackRate);
  312. if (res == NO_ERROR) {
  313. mJAudioTrack->setAuxEffectSendLevel(mSendLevel);
  314. res = mJAudioTrack->attachAuxEffect(mAuxEffectId);
  315. }
  316. }
  317. if (mPreferredDevice != nullptr) {
  318. mJAudioTrack->setPreferredDevice(mPreferredDevice->getJObject());
  319. }
  320. mJAudioTrack->registerRoutingDelegates(mRoutingDelegates);
  321. ALOGV("updateTrack_l() DONE status %d", res);
  322. return res;
  323. }
  324. status_t MediaPlayer2AudioOutput::start() {
  325. ALOGV("start");
  326. Mutex::Autolock lock(mLock);
  327. if (mCallbackData != nullptr) {
  328. mCallbackData->endTrackSwitch();
  329. }
  330. if (mJAudioTrack != nullptr) {
  331. mJAudioTrack->setVolume(mVolume);
  332. mJAudioTrack->setAuxEffectSendLevel(mSendLevel);
  333. status_t status = mJAudioTrack->start();
  334. return status;
  335. }
  336. return NO_INIT;
  337. }
  338. ssize_t MediaPlayer2AudioOutput::write(const void* buffer, size_t size, bool blocking) {
  339. Mutex::Autolock lock(mLock);
  340. LOG_ALWAYS_FATAL_IF(mCallback != nullptr, "Don't call write if supplying a callback.");
  341. //ALOGV("write(%p, %u)", buffer, size);
  342. if (mJAudioTrack != nullptr) {
  343. return mJAudioTrack->write(buffer, size, blocking);
  344. }
  345. return NO_INIT;
  346. }
  347. void MediaPlayer2AudioOutput::stop() {
  348. ALOGV("stop");
  349. Mutex::Autolock lock(mLock);
  350. if (mJAudioTrack != nullptr) {
  351. mJAudioTrack->stop();
  352. }
  353. }
  354. void MediaPlayer2AudioOutput::flush() {
  355. ALOGV("flush");
  356. Mutex::Autolock lock(mLock);
  357. if (mJAudioTrack != nullptr) {
  358. mJAudioTrack->flush();
  359. }
  360. }
  361. void MediaPlayer2AudioOutput::pause() {
  362. ALOGV("pause");
  363. Mutex::Autolock lock(mLock);
  364. if (mJAudioTrack != nullptr) {
  365. mJAudioTrack->pause();
  366. }
  367. }
  368. void MediaPlayer2AudioOutput::close() {
  369. ALOGV("close");
  370. sp<JAudioTrack> track;
  371. {
  372. Mutex::Autolock lock(mLock);
  373. track = mJAudioTrack;
  374. close_l(); // clears mJAudioTrack
  375. }
  376. // destruction of the track occurs outside of mutex.
  377. }
  378. void MediaPlayer2AudioOutput::setVolume(float volume) {
  379. ALOGV("setVolume(%f)", volume);
  380. Mutex::Autolock lock(mLock);
  381. mVolume = volume;
  382. if (mJAudioTrack != nullptr) {
  383. mJAudioTrack->setVolume(volume);
  384. }
  385. }
  386. status_t MediaPlayer2AudioOutput::setPlaybackRate(const AudioPlaybackRate &rate) {
  387. ALOGV("setPlaybackRate(%f %f %d %d)",
  388. rate.mSpeed, rate.mPitch, rate.mFallbackMode, rate.mStretchMode);
  389. Mutex::Autolock lock(mLock);
  390. if (mJAudioTrack == 0) {
  391. // remember rate so that we can set it when the track is opened
  392. mPlaybackRate = rate;
  393. return OK;
  394. }
  395. status_t res = mJAudioTrack->setPlaybackRate(rate);
  396. if (res != NO_ERROR) {
  397. return res;
  398. }
  399. // rate.mSpeed is always greater than 0 if setPlaybackRate succeeded
  400. CHECK_GT(rate.mSpeed, 0.f);
  401. mPlaybackRate = rate;
  402. if (mSampleRateHz != 0) {
  403. mMsecsPerFrame = 1E3f / (rate.mSpeed * mSampleRateHz);
  404. }
  405. return res;
  406. }
  407. status_t MediaPlayer2AudioOutput::getPlaybackRate(AudioPlaybackRate *rate) {
  408. ALOGV("getPlaybackRate");
  409. Mutex::Autolock lock(mLock);
  410. if (mJAudioTrack == 0) {
  411. return NO_INIT;
  412. }
  413. *rate = mJAudioTrack->getPlaybackRate();
  414. return NO_ERROR;
  415. }
  416. status_t MediaPlayer2AudioOutput::setAuxEffectSendLevel(float level) {
  417. ALOGV("setAuxEffectSendLevel(%f)", level);
  418. Mutex::Autolock lock(mLock);
  419. mSendLevel = level;
  420. if (mJAudioTrack != nullptr) {
  421. return mJAudioTrack->setAuxEffectSendLevel(level);
  422. }
  423. return NO_ERROR;
  424. }
  425. status_t MediaPlayer2AudioOutput::attachAuxEffect(int effectId) {
  426. ALOGV("attachAuxEffect(%d)", effectId);
  427. Mutex::Autolock lock(mLock);
  428. mAuxEffectId = effectId;
  429. if (mJAudioTrack != nullptr) {
  430. return mJAudioTrack->attachAuxEffect(effectId);
  431. }
  432. return NO_ERROR;
  433. }
  434. status_t MediaPlayer2AudioOutput::setPreferredDevice(jobject device) {
  435. ALOGV("setPreferredDevice");
  436. Mutex::Autolock lock(mLock);
  437. status_t ret = NO_ERROR;
  438. if (mJAudioTrack != nullptr) {
  439. ret = mJAudioTrack->setPreferredDevice(device);
  440. }
  441. if (ret == NO_ERROR) {
  442. mPreferredDevice = new JObjectHolder(device);
  443. }
  444. return ret;
  445. }
  446. jobject MediaPlayer2AudioOutput::getRoutedDevice() {
  447. ALOGV("getRoutedDevice");
  448. Mutex::Autolock lock(mLock);
  449. if (mJAudioTrack != nullptr) {
  450. return mJAudioTrack->getRoutedDevice();
  451. }
  452. return nullptr;
  453. }
  454. status_t MediaPlayer2AudioOutput::addAudioDeviceCallback(jobject jRoutingDelegate) {
  455. ALOGV("addAudioDeviceCallback");
  456. Mutex::Autolock lock(mLock);
  457. jobject listener = JAudioTrack::getListener(jRoutingDelegate);
  458. if (JAudioTrack::findByKey(mRoutingDelegates, listener) == nullptr) {
  459. sp<JObjectHolder> listenerHolder = new JObjectHolder(listener);
  460. jobject handler = JAudioTrack::getHandler(jRoutingDelegate);
  461. sp<JObjectHolder> routingDelegateHolder = new JObjectHolder(jRoutingDelegate);
  462. mRoutingDelegates.push_back(std::pair<sp<JObjectHolder>, sp<JObjectHolder>>(
  463. listenerHolder, routingDelegateHolder));
  464. if (mJAudioTrack != nullptr) {
  465. return mJAudioTrack->addAudioDeviceCallback(
  466. routingDelegateHolder->getJObject(), handler);
  467. }
  468. }
  469. return NO_ERROR;
  470. }
  471. status_t MediaPlayer2AudioOutput::removeAudioDeviceCallback(jobject listener) {
  472. ALOGV("removeAudioDeviceCallback");
  473. Mutex::Autolock lock(mLock);
  474. jobject routingDelegate = nullptr;
  475. if ((routingDelegate = JAudioTrack::findByKey(mRoutingDelegates, listener)) != nullptr) {
  476. if (mJAudioTrack != nullptr) {
  477. mJAudioTrack->removeAudioDeviceCallback(routingDelegate);
  478. }
  479. JAudioTrack::eraseByKey(mRoutingDelegates, listener);
  480. }
  481. return NO_ERROR;
  482. }
  483. // static
  484. void MediaPlayer2AudioOutput::CallbackWrapper(
  485. int event, void *cookie, void *info) {
  486. //ALOGV("callbackwrapper");
  487. CallbackData *data = (CallbackData*)cookie;
  488. // lock to ensure we aren't caught in the middle of a track switch.
  489. data->lock();
  490. MediaPlayer2AudioOutput *me = data->getOutput();
  491. JAudioTrack::Buffer *buffer = (JAudioTrack::Buffer *)info;
  492. if (me == nullptr) {
  493. // no output set, likely because the track was scheduled to be reused
  494. // by another player, but the format turned out to be incompatible.
  495. data->unlock();
  496. if (buffer != nullptr) {
  497. buffer->mSize = 0;
  498. }
  499. return;
  500. }
  501. switch(event) {
  502. case JAudioTrack::EVENT_MORE_DATA: {
  503. size_t actualSize = (*me->mCallback)(
  504. me, buffer->mData, buffer->mSize, me->mCallbackCookie,
  505. CB_EVENT_FILL_BUFFER);
  506. // Log when no data is returned from the callback.
  507. // (1) We may have no data (especially with network streaming sources).
  508. // (2) We may have reached the EOS and the audio track is not stopped yet.
  509. // Note that AwesomePlayer/AudioPlayer will only return zero size when it reaches the EOS.
  510. // NuPlayer2Renderer will return zero when it doesn't have data (it doesn't block to fill).
  511. //
  512. // This is a benign busy-wait, with the next data request generated 10 ms or more later;
  513. // nevertheless for power reasons, we don't want to see too many of these.
  514. ALOGV_IF(actualSize == 0 && buffer->mSize > 0, "callbackwrapper: empty buffer returned");
  515. buffer->mSize = actualSize;
  516. } break;
  517. case JAudioTrack::EVENT_STREAM_END:
  518. // currently only occurs for offloaded callbacks
  519. ALOGV("callbackwrapper: deliver EVENT_STREAM_END");
  520. (*me->mCallback)(me, nullptr /* buffer */, 0 /* size */,
  521. me->mCallbackCookie, CB_EVENT_STREAM_END);
  522. break;
  523. case JAudioTrack::EVENT_NEW_IAUDIOTRACK :
  524. ALOGV("callbackwrapper: deliver EVENT_TEAR_DOWN");
  525. (*me->mCallback)(me, nullptr /* buffer */, 0 /* size */,
  526. me->mCallbackCookie, CB_EVENT_TEAR_DOWN);
  527. break;
  528. case JAudioTrack::EVENT_UNDERRUN:
  529. // This occurs when there is no data available, typically
  530. // when there is a failure to supply data to the AudioTrack. It can also
  531. // occur in non-offloaded mode when the audio device comes out of standby.
  532. //
  533. // If an AudioTrack underruns it outputs silence. Since this happens suddenly
  534. // it may sound like an audible pop or glitch.
  535. //
  536. // The underrun event is sent once per track underrun; the condition is reset
  537. // when more data is sent to the AudioTrack.
  538. ALOGD("callbackwrapper: EVENT_UNDERRUN (discarded)");
  539. break;
  540. default:
  541. ALOGE("received unknown event type: %d inside CallbackWrapper !", event);
  542. }
  543. data->unlock();
  544. }
  545. int32_t MediaPlayer2AudioOutput::getSessionId() const {
  546. Mutex::Autolock lock(mLock);
  547. return mSessionId;
  548. }
  549. void MediaPlayer2AudioOutput::setSessionId(const int32_t sessionId) {
  550. Mutex::Autolock lock(mLock);
  551. mSessionId = sessionId;
  552. }
  553. uint32_t MediaPlayer2AudioOutput::getSampleRate() const {
  554. Mutex::Autolock lock(mLock);
  555. if (mJAudioTrack == 0) {
  556. return 0;
  557. }
  558. return mJAudioTrack->getSampleRate();
  559. }
  560. int64_t MediaPlayer2AudioOutput::getBufferDurationInUs() const {
  561. Mutex::Autolock lock(mLock);
  562. if (mJAudioTrack == 0) {
  563. return 0;
  564. }
  565. int64_t duration;
  566. if (mJAudioTrack->getBufferDurationInUs(&duration) != OK) {
  567. return 0;
  568. }
  569. return duration;
  570. }
  571. } // namespace android