PageRenderTime 47ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/media/base/android/media_codec_decoder.cc

https://gitlab.com/jonnialva90/iridium-browser
C++ | 958 lines | 666 code | 210 blank | 82 comment | 109 complexity | 8313295519847001ab5a6de15bb723aa MD5 | raw file
  1. // Copyright 2015 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "media/base/android/media_codec_decoder.h"
  5. #include "base/bind.h"
  6. #include "base/bind_helpers.h"
  7. #include "base/callback_helpers.h"
  8. #include "base/logging.h"
  9. #include "media/base/android/media_codec_bridge.h"
  10. namespace media {
  11. namespace {
  12. // Stop requesting new data in the kPrefetching state when the queue size
  13. // reaches this limit.
  14. const int kPrefetchLimit = 8;
  15. // Request new data in the kRunning state if the queue size is less than this.
  16. const int kPlaybackLowLimit = 4;
  17. // Posting delay of the next frame processing, in milliseconds
  18. const int kNextFrameDelay = 1;
  19. // Timeout for dequeuing an input buffer from MediaCodec in milliseconds.
  20. const int kInputBufferTimeout = 20;
  21. // Timeout for dequeuing an output buffer from MediaCodec in milliseconds.
  22. const int kOutputBufferTimeout = 20;
  23. }
  24. MediaCodecDecoder::MediaCodecDecoder(
  25. const char* decoder_thread_name,
  26. const scoped_refptr<base::SingleThreadTaskRunner>& media_task_runner,
  27. FrameStatistics* frame_statistics,
  28. const base::Closure& external_request_data_cb,
  29. const base::Closure& starvation_cb,
  30. const base::Closure& decoder_drained_cb,
  31. const base::Closure& stop_done_cb,
  32. const base::Closure& waiting_for_decryption_key_cb,
  33. const base::Closure& error_cb)
  34. : decoder_thread_(decoder_thread_name),
  35. media_task_runner_(media_task_runner),
  36. frame_statistics_(frame_statistics),
  37. needs_reconfigure_(false),
  38. drain_decoder_(false),
  39. always_reconfigure_for_tests_(false),
  40. external_request_data_cb_(external_request_data_cb),
  41. starvation_cb_(starvation_cb),
  42. decoder_drained_cb_(decoder_drained_cb),
  43. stop_done_cb_(stop_done_cb),
  44. waiting_for_decryption_key_cb_(waiting_for_decryption_key_cb),
  45. error_cb_(error_cb),
  46. state_(kStopped),
  47. is_prepared_(false),
  48. eos_enqueued_(false),
  49. missing_key_reported_(false),
  50. completed_(false),
  51. last_frame_posted_(false),
  52. is_data_request_in_progress_(false),
  53. is_incoming_data_invalid_(false),
  54. #ifndef NDEBUG
  55. verify_next_frame_is_key_(false),
  56. #endif
  57. weak_factory_(this) {
  58. DCHECK(media_task_runner_->BelongsToCurrentThread());
  59. DVLOG(1) << "Decoder::Decoder() " << decoder_thread_name;
  60. internal_error_cb_ =
  61. base::Bind(&MediaCodecDecoder::OnCodecError, weak_factory_.GetWeakPtr());
  62. internal_preroll_done_cb_ =
  63. base::Bind(&MediaCodecDecoder::OnPrerollDone, weak_factory_.GetWeakPtr());
  64. request_data_cb_ =
  65. base::Bind(&MediaCodecDecoder::RequestData, weak_factory_.GetWeakPtr());
  66. }
  67. MediaCodecDecoder::~MediaCodecDecoder() {}
  68. const char* MediaCodecDecoder::class_name() const {
  69. return "Decoder";
  70. }
  71. void MediaCodecDecoder::Flush() {
  72. DCHECK(media_task_runner_->BelongsToCurrentThread());
  73. DVLOG(1) << class_name() << "::" << __FUNCTION__;
  74. DCHECK_EQ(GetState(), kStopped);
  75. // Flush() is a part of the Seek request. Whenever we request a seek we need
  76. // to invalidate the current data request.
  77. if (is_data_request_in_progress_)
  78. is_incoming_data_invalid_ = true;
  79. eos_enqueued_ = false;
  80. missing_key_reported_ = false;
  81. completed_ = false;
  82. drain_decoder_ = false;
  83. au_queue_.Flush();
  84. // |is_prepared_| is set on the decoder thread, it shouldn't be running now.
  85. DCHECK(!decoder_thread_.IsRunning());
  86. is_prepared_ = false;
  87. #ifndef NDEBUG
  88. // We check and reset |verify_next_frame_is_key_| on Decoder thread.
  89. // We have just DCHECKed that decoder thread is not running.
  90. // For video the first frame after flush must be key frame.
  91. verify_next_frame_is_key_ = true;
  92. #endif
  93. if (media_codec_bridge_) {
  94. // MediaCodecBridge::Reset() performs MediaCodecBridge.flush()
  95. MediaCodecStatus flush_status = media_codec_bridge_->Reset();
  96. if (flush_status != MEDIA_CODEC_OK) {
  97. DVLOG(0) << class_name() << "::" << __FUNCTION__
  98. << "MediaCodecBridge::Reset() failed";
  99. media_task_runner_->PostTask(FROM_HERE, internal_error_cb_);
  100. }
  101. }
  102. }
  103. void MediaCodecDecoder::ReleaseMediaCodec() {
  104. DCHECK(media_task_runner_->BelongsToCurrentThread());
  105. DVLOG(1) << class_name() << "::" << __FUNCTION__;
  106. DCHECK(!decoder_thread_.IsRunning());
  107. media_codec_bridge_.reset();
  108. // |is_prepared_| is set on the decoder thread, it shouldn't be running now.
  109. is_prepared_ = false;
  110. }
  111. bool MediaCodecDecoder::IsPrefetchingOrPlaying() const {
  112. DCHECK(media_task_runner_->BelongsToCurrentThread());
  113. // Whether decoder needs to be stopped.
  114. base::AutoLock lock(state_lock_);
  115. switch (state_) {
  116. case kPrefetching:
  117. case kPrefetched:
  118. case kPrerolling:
  119. case kPrerolled:
  120. case kRunning:
  121. return true;
  122. case kStopped:
  123. case kStopping:
  124. case kInEmergencyStop:
  125. case kError:
  126. return false;
  127. }
  128. NOTREACHED();
  129. return false;
  130. }
  131. bool MediaCodecDecoder::IsStopped() const {
  132. DCHECK(media_task_runner_->BelongsToCurrentThread());
  133. return GetState() == kStopped;
  134. }
  135. bool MediaCodecDecoder::IsCompleted() const {
  136. DCHECK(media_task_runner_->BelongsToCurrentThread());
  137. return completed_;
  138. }
  139. bool MediaCodecDecoder::NotCompletedAndNeedsPreroll() const {
  140. DCHECK(media_task_runner_->BelongsToCurrentThread());
  141. return HasStream() && !completed_ &&
  142. (!is_prepared_ || preroll_timestamp_ != base::TimeDelta());
  143. }
  144. void MediaCodecDecoder::SetPrerollTimestamp(base::TimeDelta preroll_timestamp) {
  145. DCHECK(media_task_runner_->BelongsToCurrentThread());
  146. DVLOG(1) << class_name() << "::" << __FUNCTION__ << ": " << preroll_timestamp;
  147. preroll_timestamp_ = preroll_timestamp;
  148. }
  149. void MediaCodecDecoder::SetNeedsReconfigure() {
  150. DCHECK(media_task_runner_->BelongsToCurrentThread());
  151. DVLOG(1) << class_name() << "::" << __FUNCTION__;
  152. needs_reconfigure_ = true;
  153. }
  154. void MediaCodecDecoder::Prefetch(const base::Closure& prefetch_done_cb) {
  155. DCHECK(media_task_runner_->BelongsToCurrentThread());
  156. DVLOG(1) << class_name() << "::" << __FUNCTION__;
  157. DCHECK(GetState() == kStopped);
  158. prefetch_done_cb_ = prefetch_done_cb;
  159. SetState(kPrefetching);
  160. PrefetchNextChunk();
  161. }
  162. MediaCodecDecoder::ConfigStatus MediaCodecDecoder::Configure(
  163. jobject media_crypto) {
  164. DCHECK(media_task_runner_->BelongsToCurrentThread());
  165. DVLOG(1) << class_name() << "::" << __FUNCTION__;
  166. if (GetState() == kError) {
  167. DVLOG(0) << class_name() << "::" << __FUNCTION__ << ": wrong state kError";
  168. return kConfigFailure;
  169. }
  170. if (needs_reconfigure_) {
  171. DVLOG(1) << class_name() << "::" << __FUNCTION__
  172. << ": needs reconfigure, deleting MediaCodec";
  173. needs_reconfigure_ = false;
  174. ReleaseMediaCodec();
  175. }
  176. if (media_codec_bridge_) {
  177. DVLOG(1) << class_name() << "::" << __FUNCTION__
  178. << ": reconfiguration is not required, ignoring";
  179. return kConfigOk;
  180. }
  181. // Read all |kConfigChanged| units preceding the data one.
  182. AccessUnitQueue::Info au_info = au_queue_.GetInfo();
  183. while (au_info.configs) {
  184. SetDemuxerConfigs(*au_info.configs);
  185. au_queue_.Advance();
  186. au_info = au_queue_.GetInfo();
  187. }
  188. MediaCodecDecoder::ConfigStatus result = ConfigureInternal(media_crypto);
  189. #ifndef NDEBUG
  190. // We check and reset |verify_next_frame_is_key_| on Decoder thread.
  191. // This DCHECK ensures we won't need to lock this variable.
  192. DCHECK(!decoder_thread_.IsRunning());
  193. // For video the first frame after reconfiguration must be key frame.
  194. if (result == kConfigOk)
  195. verify_next_frame_is_key_ = true;
  196. #endif
  197. return result;
  198. }
  199. bool MediaCodecDecoder::Preroll(const base::Closure& preroll_done_cb) {
  200. DCHECK(media_task_runner_->BelongsToCurrentThread());
  201. DVLOG(1) << class_name() << "::" << __FUNCTION__
  202. << " preroll_timestamp:" << preroll_timestamp_;
  203. DecoderState state = GetState();
  204. if (state != kPrefetched) {
  205. DVLOG(0) << class_name() << "::" << __FUNCTION__ << ": wrong state "
  206. << AsString(state) << ", ignoring";
  207. return false;
  208. }
  209. if (!media_codec_bridge_) {
  210. DVLOG(0) << class_name() << "::" << __FUNCTION__
  211. << ": not configured, ignoring";
  212. return false;
  213. }
  214. DCHECK(!decoder_thread_.IsRunning());
  215. preroll_done_cb_ = preroll_done_cb;
  216. // We only synchronize video stream.
  217. DissociatePTSFromTime(); // associaton will happen after preroll is done.
  218. last_frame_posted_ = false;
  219. // Start the decoder thread
  220. if (!decoder_thread_.Start()) {
  221. DVLOG(0) << class_name() << "::" << __FUNCTION__
  222. << ": cannot start decoder thread";
  223. return false;
  224. }
  225. SetState(kPrerolling);
  226. decoder_thread_.task_runner()->PostTask(
  227. FROM_HERE,
  228. base::Bind(&MediaCodecDecoder::ProcessNextFrame, base::Unretained(this)));
  229. return true;
  230. }
  231. bool MediaCodecDecoder::Start(base::TimeDelta start_timestamp) {
  232. DCHECK(media_task_runner_->BelongsToCurrentThread());
  233. DVLOG(1) << class_name() << "::" << __FUNCTION__
  234. << " start_timestamp:" << start_timestamp;
  235. DecoderState state = GetState();
  236. if (state != kPrefetched && state != kPrerolled) {
  237. DVLOG(0) << class_name() << "::" << __FUNCTION__ << ": wrong state "
  238. << AsString(state) << ", ignoring";
  239. return false;
  240. }
  241. if (!media_codec_bridge_) {
  242. DVLOG(0) << class_name() << "::" << __FUNCTION__
  243. << ": not configured, ignoring";
  244. return false;
  245. }
  246. // We only synchronize video stream.
  247. AssociateCurrentTimeWithPTS(start_timestamp);
  248. DCHECK(preroll_timestamp_ == base::TimeDelta());
  249. // Start the decoder thread
  250. if (!decoder_thread_.IsRunning()) {
  251. last_frame_posted_ = false;
  252. if (!decoder_thread_.Start()) {
  253. DVLOG(1) << class_name() << "::" << __FUNCTION__
  254. << ": cannot start decoder thread";
  255. return false;
  256. }
  257. }
  258. SetState(kRunning);
  259. decoder_thread_.task_runner()->PostTask(
  260. FROM_HERE,
  261. base::Bind(&MediaCodecDecoder::ProcessNextFrame, base::Unretained(this)));
  262. return true;
  263. }
  264. void MediaCodecDecoder::SyncStop() {
  265. DCHECK(media_task_runner_->BelongsToCurrentThread());
  266. DVLOG(1) << class_name() << "::" << __FUNCTION__;
  267. if (GetState() == kError) {
  268. DVLOG(0) << class_name() << "::" << __FUNCTION__
  269. << ": wrong state kError, ignoring";
  270. return;
  271. }
  272. DoEmergencyStop();
  273. ReleaseDelayedBuffers();
  274. }
  275. void MediaCodecDecoder::RequestToStop() {
  276. DCHECK(media_task_runner_->BelongsToCurrentThread());
  277. DVLOG(1) << class_name() << "::" << __FUNCTION__;
  278. DecoderState state = GetState();
  279. switch (state) {
  280. case kError:
  281. DVLOG(0) << class_name() << "::" << __FUNCTION__
  282. << ": wrong state kError, ignoring";
  283. break;
  284. case kRunning:
  285. SetState(kStopping);
  286. break;
  287. case kPrerolling:
  288. case kPrerolled:
  289. DCHECK(decoder_thread_.IsRunning());
  290. // Synchronous stop.
  291. decoder_thread_.Stop();
  292. SetState(kStopped);
  293. media_task_runner_->PostTask(FROM_HERE, stop_done_cb_);
  294. break;
  295. case kStopping:
  296. case kStopped:
  297. break; // ignore
  298. case kPrefetching:
  299. case kPrefetched:
  300. // There is nothing to wait for, we can sent notification right away.
  301. DCHECK(!decoder_thread_.IsRunning());
  302. SetState(kStopped);
  303. media_task_runner_->PostTask(FROM_HERE, stop_done_cb_);
  304. break;
  305. default:
  306. NOTREACHED();
  307. break;
  308. }
  309. }
  310. void MediaCodecDecoder::OnLastFrameRendered(bool eos_encountered) {
  311. DCHECK(media_task_runner_->BelongsToCurrentThread());
  312. DVLOG(1) << class_name() << "::" << __FUNCTION__
  313. << " eos_encountered:" << eos_encountered;
  314. decoder_thread_.Stop(); // synchronous
  315. SetState(kStopped);
  316. completed_ = (eos_encountered && !drain_decoder_);
  317. missing_key_reported_ = false;
  318. // If the stream is completed during preroll we need to report it since
  319. // another stream might be running and the player waits for two callbacks.
  320. if (completed_ && !preroll_done_cb_.is_null()) {
  321. preroll_timestamp_ = base::TimeDelta();
  322. media_task_runner_->PostTask(FROM_HERE,
  323. base::ResetAndReturn(&preroll_done_cb_));
  324. }
  325. if (eos_encountered && drain_decoder_) {
  326. drain_decoder_ = false;
  327. eos_enqueued_ = false;
  328. ReleaseMediaCodec();
  329. media_task_runner_->PostTask(FROM_HERE, decoder_drained_cb_);
  330. }
  331. media_task_runner_->PostTask(FROM_HERE, stop_done_cb_);
  332. }
  333. void MediaCodecDecoder::OnPrerollDone() {
  334. DCHECK(media_task_runner_->BelongsToCurrentThread());
  335. DVLOG(1) << class_name() << "::" << __FUNCTION__
  336. << " state:" << AsString(GetState());
  337. preroll_timestamp_ = base::TimeDelta();
  338. // The state might be kStopping (?)
  339. if (GetState() == kPrerolling)
  340. SetState(kPrerolled);
  341. if (!preroll_done_cb_.is_null())
  342. base::ResetAndReturn(&preroll_done_cb_).Run();
  343. }
  344. void MediaCodecDecoder::OnDemuxerDataAvailable(const DemuxerData& data) {
  345. DCHECK(media_task_runner_->BelongsToCurrentThread());
  346. // If |data| contains an aborted data, the last AU will have kAborted status.
  347. bool aborted_data =
  348. !data.access_units.empty() &&
  349. data.access_units.back().status == DemuxerStream::kAborted;
  350. #ifndef NDEBUG
  351. const char* explain_if_skipped =
  352. is_incoming_data_invalid_ ? " skipped as invalid"
  353. : (aborted_data ? " skipped as aborted" : "");
  354. for (const auto& unit : data.access_units)
  355. DVLOG(2) << class_name() << "::" << __FUNCTION__ << explain_if_skipped
  356. << " au: " << unit;
  357. for (const auto& configs : data.demuxer_configs)
  358. DVLOG(2) << class_name() << "::" << __FUNCTION__ << " configs: " << configs;
  359. #endif
  360. if (!is_incoming_data_invalid_ && !aborted_data)
  361. au_queue_.PushBack(data);
  362. is_incoming_data_invalid_ = false;
  363. is_data_request_in_progress_ = false;
  364. // Do not request data if we got kAborted. There is no point to request the
  365. // data after kAborted and before the OnDemuxerSeekDone.
  366. if (GetState() == kPrefetching && !aborted_data)
  367. PrefetchNextChunk();
  368. }
  369. bool MediaCodecDecoder::IsPrerollingForTests() const {
  370. // UI task runner.
  371. return GetState() == kPrerolling;
  372. }
  373. void MediaCodecDecoder::SetAlwaysReconfigureForTests() {
  374. // UI task runner.
  375. always_reconfigure_for_tests_ = true;
  376. }
  377. void MediaCodecDecoder::SetCodecCreatedCallbackForTests(base::Closure cb) {
  378. // UI task runner.
  379. codec_created_for_tests_cb_ = cb;
  380. }
  381. int MediaCodecDecoder::NumDelayedRenderTasks() const {
  382. return 0;
  383. }
  384. void MediaCodecDecoder::DoEmergencyStop() {
  385. DCHECK(media_task_runner_->BelongsToCurrentThread());
  386. DVLOG(1) << class_name() << "::" << __FUNCTION__;
  387. // After this method returns, decoder thread will not be running.
  388. // Set [kInEmergencyStop| state to block already posted ProcessNextFrame().
  389. SetState(kInEmergencyStop);
  390. decoder_thread_.Stop(); // synchronous
  391. SetState(kStopped);
  392. missing_key_reported_ = false;
  393. }
  394. void MediaCodecDecoder::CheckLastFrame(bool eos_encountered,
  395. bool has_delayed_tasks) {
  396. DCHECK(decoder_thread_.task_runner()->BelongsToCurrentThread());
  397. bool last_frame_when_stopping = GetState() == kStopping && !has_delayed_tasks;
  398. if (last_frame_when_stopping || eos_encountered) {
  399. media_task_runner_->PostTask(
  400. FROM_HERE, base::Bind(&MediaCodecDecoder::OnLastFrameRendered,
  401. weak_factory_.GetWeakPtr(), eos_encountered));
  402. last_frame_posted_ = true;
  403. }
  404. }
  405. void MediaCodecDecoder::OnCodecError() {
  406. DCHECK(media_task_runner_->BelongsToCurrentThread());
  407. // Ignore codec errors from the moment surface is changed till the
  408. // |media_codec_bridge_| is deleted.
  409. if (needs_reconfigure_) {
  410. DVLOG(1) << class_name() << "::" << __FUNCTION__
  411. << ": needs reconfigure, ignoring";
  412. return;
  413. }
  414. SetState(kError);
  415. error_cb_.Run();
  416. }
  417. void MediaCodecDecoder::RequestData() {
  418. DCHECK(media_task_runner_->BelongsToCurrentThread());
  419. // Ensure one data request at a time.
  420. if (!is_data_request_in_progress_) {
  421. is_data_request_in_progress_ = true;
  422. external_request_data_cb_.Run();
  423. }
  424. }
  425. void MediaCodecDecoder::PrefetchNextChunk() {
  426. DCHECK(media_task_runner_->BelongsToCurrentThread());
  427. DVLOG(1) << class_name() << "::" << __FUNCTION__;
  428. AccessUnitQueue::Info au_info = au_queue_.GetInfo();
  429. if (eos_enqueued_ || au_info.data_length >= kPrefetchLimit ||
  430. au_info.has_eos) {
  431. // We are done prefetching
  432. SetState(kPrefetched);
  433. DVLOG(1) << class_name() << "::" << __FUNCTION__ << " posting PrefetchDone";
  434. media_task_runner_->PostTask(FROM_HERE,
  435. base::ResetAndReturn(&prefetch_done_cb_));
  436. return;
  437. }
  438. request_data_cb_.Run();
  439. }
  440. void MediaCodecDecoder::ProcessNextFrame() {
  441. DCHECK(decoder_thread_.task_runner()->BelongsToCurrentThread());
  442. DVLOG(2) << class_name() << "::" << __FUNCTION__;
  443. DecoderState state = GetState();
  444. if (state != kPrerolling && state != kRunning && state != kStopping) {
  445. DVLOG(1) << class_name() << "::" << __FUNCTION__
  446. << ": state: " << AsString(state) << " stopping frame processing";
  447. return;
  448. }
  449. if (state == kStopping) {
  450. if (NumDelayedRenderTasks() == 0 && !last_frame_posted_) {
  451. media_task_runner_->PostTask(
  452. FROM_HERE, base::Bind(&MediaCodecDecoder::OnLastFrameRendered,
  453. weak_factory_.GetWeakPtr(), false));
  454. last_frame_posted_ = true;
  455. }
  456. // We can stop processing, the |au_queue_| and MediaCodec queues can freeze.
  457. // We only need to let finish the delayed rendering tasks.
  458. DVLOG(1) << class_name() << "::" << __FUNCTION__ << " kStopping, returning";
  459. return;
  460. }
  461. DCHECK(state == kPrerolling || state == kRunning);
  462. if (!EnqueueInputBuffer())
  463. return;
  464. if (!DepleteOutputBufferQueue())
  465. return;
  466. // We need a small delay if we want to stop this thread by
  467. // decoder_thread_.Stop() reliably.
  468. // The decoder thread message loop processes all pending
  469. // (but not delayed) tasks before it can quit; without a delay
  470. // the message loop might be forever processing the pendng tasks.
  471. decoder_thread_.task_runner()->PostDelayedTask(
  472. FROM_HERE,
  473. base::Bind(&MediaCodecDecoder::ProcessNextFrame, base::Unretained(this)),
  474. base::TimeDelta::FromMilliseconds(kNextFrameDelay));
  475. }
  476. // Returns false if we should stop decoding process. Right now
  477. // it happens if we got MediaCodec error or detected starvation.
  478. bool MediaCodecDecoder::EnqueueInputBuffer() {
  479. DCHECK(decoder_thread_.task_runner()->BelongsToCurrentThread());
  480. DVLOG(2) << class_name() << "::" << __FUNCTION__;
  481. if (eos_enqueued_) {
  482. DVLOG(1) << class_name() << "::" << __FUNCTION__
  483. << ": EOS enqueued, returning";
  484. return true; // Nothing to do
  485. }
  486. if (missing_key_reported_) {
  487. DVLOG(1) << class_name() << "::" << __FUNCTION__
  488. << ": NO KEY reported, returning";
  489. return true; // Nothing to do
  490. }
  491. // Keep the number pending video frames low, ideally maintaining
  492. // the same audio and video duration after stop request
  493. if (NumDelayedRenderTasks() > 1) {
  494. DVLOG(2) << class_name() << "::" << __FUNCTION__ << ": # delayed buffers ("
  495. << NumDelayedRenderTasks() << ") exceeds 1, returning";
  496. return true; // Nothing to do
  497. }
  498. // Get the next frame from the queue. As we go, request more data and
  499. // consume |kConfigChanged| units.
  500. // |drain_decoder_| can be already set here if we could not dequeue the input
  501. // buffer for it right away.
  502. AccessUnitQueue::Info au_info;
  503. if (!drain_decoder_) {
  504. au_info = AdvanceAccessUnitQueue(&drain_decoder_);
  505. if (!au_info.length) {
  506. // Report starvation and return, Start() will be called again later.
  507. DVLOG(1) << class_name() << "::" << __FUNCTION__
  508. << ": starvation detected";
  509. media_task_runner_->PostTask(FROM_HERE, starvation_cb_);
  510. return true;
  511. }
  512. DCHECK(au_info.front_unit);
  513. #ifndef NDEBUG
  514. if (verify_next_frame_is_key_) {
  515. verify_next_frame_is_key_ = false;
  516. VerifyUnitIsKeyFrame(au_info.front_unit);
  517. }
  518. #endif
  519. }
  520. // Dequeue input buffer
  521. base::TimeDelta timeout =
  522. base::TimeDelta::FromMilliseconds(kInputBufferTimeout);
  523. int index = -1;
  524. MediaCodecStatus status =
  525. media_codec_bridge_->DequeueInputBuffer(timeout, &index);
  526. DVLOG(2) << class_name() << ":: DequeueInputBuffer index:" << index;
  527. switch (status) {
  528. case MEDIA_CODEC_ERROR:
  529. DVLOG(0) << class_name() << "::" << __FUNCTION__
  530. << ": MEDIA_CODEC_ERROR DequeueInputBuffer failed";
  531. media_task_runner_->PostTask(FROM_HERE, internal_error_cb_);
  532. return false;
  533. case MEDIA_CODEC_DEQUEUE_INPUT_AGAIN_LATER:
  534. DVLOG(2)
  535. << class_name() << "::" << __FUNCTION__
  536. << ": DequeueInputBuffer returned MediaCodec.INFO_TRY_AGAIN_LATER.";
  537. return true;
  538. default:
  539. break;
  540. }
  541. // We got the buffer
  542. DCHECK_EQ(status, MEDIA_CODEC_OK);
  543. DCHECK_GE(index, 0);
  544. const AccessUnit* unit = au_info.front_unit;
  545. if (drain_decoder_ || unit->is_end_of_stream) {
  546. DVLOG(1) << class_name() << "::" << __FUNCTION__ << ": QueueEOS";
  547. media_codec_bridge_->QueueEOS(index);
  548. eos_enqueued_ = true;
  549. return true;
  550. }
  551. DCHECK(unit);
  552. DCHECK(!unit->data.empty());
  553. if (unit->key_id.empty() || unit->iv.empty()) {
  554. DVLOG(2) << class_name() << "::" << __FUNCTION__
  555. << ": QueueInputBuffer pts:" << unit->timestamp;
  556. status = media_codec_bridge_->QueueInputBuffer(
  557. index, &unit->data[0], unit->data.size(), unit->timestamp);
  558. } else {
  559. DVLOG(2) << class_name() << "::" << __FUNCTION__
  560. << ": QueueSecureInputBuffer pts:" << unit->timestamp
  561. << " key_id size:" << unit->key_id.size()
  562. << " iv size:" << unit->iv.size()
  563. << " subsamples size:" << unit->subsamples.size();
  564. status = media_codec_bridge_->QueueSecureInputBuffer(
  565. index, &unit->data[0], unit->data.size(),
  566. reinterpret_cast<const uint8_t*>(&unit->key_id[0]), unit->key_id.size(),
  567. reinterpret_cast<const uint8_t*>(&unit->iv[0]), unit->iv.size(),
  568. unit->subsamples.empty() ? nullptr : &unit->subsamples[0],
  569. unit->subsamples.size(), unit->timestamp);
  570. }
  571. switch (status) {
  572. case MEDIA_CODEC_OK:
  573. break;
  574. case MEDIA_CODEC_ERROR:
  575. DVLOG(0) << class_name() << "::" << __FUNCTION__
  576. << ": MEDIA_CODEC_ERROR: QueueInputBuffer failed";
  577. media_task_runner_->PostTask(FROM_HERE, internal_error_cb_);
  578. return false;
  579. case MEDIA_CODEC_NO_KEY:
  580. DVLOG(1) << class_name() << "::" << __FUNCTION__
  581. << ": MEDIA_CODEC_NO_KEY";
  582. media_task_runner_->PostTask(FROM_HERE, waiting_for_decryption_key_cb_);
  583. // In response to the |waiting_for_decryption_key_cb_| the player will
  584. // request to stop decoder. We need to keep running to properly perform
  585. // the stop, but prevent enqueuing the same frame over and over again so
  586. // we won't generate more |waiting_for_decryption_key_cb_|.
  587. missing_key_reported_ = true;
  588. return true;
  589. default:
  590. NOTREACHED() << class_name() << "::" << __FUNCTION__
  591. << ": unexpected error code " << status;
  592. media_task_runner_->PostTask(FROM_HERE, internal_error_cb_);
  593. return false;
  594. }
  595. // Have successfully queued input buffer, go to next access unit.
  596. au_queue_.Advance();
  597. return true;
  598. }
  599. AccessUnitQueue::Info MediaCodecDecoder::AdvanceAccessUnitQueue(
  600. bool* drain_decoder) {
  601. DCHECK(decoder_thread_.task_runner()->BelongsToCurrentThread());
  602. DVLOG(2) << class_name() << "::" << __FUNCTION__;
  603. // Retrieve access units from the |au_queue_| in a loop until we either get
  604. // a non-config front unit or until the queue is empty.
  605. DCHECK(drain_decoder != nullptr);
  606. AccessUnitQueue::Info au_info;
  607. do {
  608. // Get current frame
  609. au_info = au_queue_.GetInfo();
  610. // Request the data from Demuxer
  611. if (au_info.data_length <= kPlaybackLowLimit && !au_info.has_eos)
  612. media_task_runner_->PostTask(FROM_HERE, request_data_cb_);
  613. if (!au_info.length)
  614. break; // Starvation
  615. if (au_info.configs) {
  616. DVLOG(1) << class_name() << "::" << __FUNCTION__ << ": received configs "
  617. << (*au_info.configs);
  618. // Compare the new and current configs.
  619. if (IsCodecReconfigureNeeded(*au_info.configs)) {
  620. DVLOG(1) << class_name() << "::" << __FUNCTION__
  621. << ": reconfiguration and decoder drain required";
  622. *drain_decoder = true;
  623. }
  624. // Replace the current configs.
  625. SetDemuxerConfigs(*au_info.configs);
  626. // Move to the next frame
  627. au_queue_.Advance();
  628. }
  629. } while (au_info.configs);
  630. return au_info;
  631. }
  632. // Returns false if there was MediaCodec error.
  633. bool MediaCodecDecoder::DepleteOutputBufferQueue() {
  634. DCHECK(decoder_thread_.task_runner()->BelongsToCurrentThread());
  635. DVLOG(2) << class_name() << "::" << __FUNCTION__;
  636. int buffer_index = 0;
  637. size_t offset = 0;
  638. size_t size = 0;
  639. base::TimeDelta pts;
  640. MediaCodecStatus status;
  641. bool eos_encountered = false;
  642. RenderMode render_mode;
  643. base::TimeDelta timeout =
  644. base::TimeDelta::FromMilliseconds(kOutputBufferTimeout);
  645. // Extract all output buffers that are available.
  646. // Usually there will be only one, but sometimes it is preceeded by
  647. // MEDIA_CODEC_OUTPUT_BUFFERS_CHANGED or MEDIA_CODEC_OUTPUT_FORMAT_CHANGED.
  648. do {
  649. status = media_codec_bridge_->DequeueOutputBuffer(
  650. timeout, &buffer_index, &offset, &size, &pts, &eos_encountered,
  651. nullptr);
  652. // Reset the timeout to 0 for the subsequent DequeueOutputBuffer() calls
  653. // to quickly break the loop after we got all currently available buffers.
  654. timeout = base::TimeDelta::FromMilliseconds(0);
  655. switch (status) {
  656. case MEDIA_CODEC_OUTPUT_BUFFERS_CHANGED:
  657. // Output buffers are replaced in MediaCodecBridge, nothing to do.
  658. DVLOG(2) << class_name() << "::" << __FUNCTION__
  659. << " MEDIA_CODEC_OUTPUT_BUFFERS_CHANGED";
  660. break;
  661. case MEDIA_CODEC_OUTPUT_FORMAT_CHANGED:
  662. DVLOG(2) << class_name() << "::" << __FUNCTION__
  663. << " MEDIA_CODEC_OUTPUT_FORMAT_CHANGED";
  664. OnOutputFormatChanged();
  665. break;
  666. case MEDIA_CODEC_OK:
  667. // We got the decoded frame.
  668. is_prepared_ = true;
  669. if (pts < preroll_timestamp_)
  670. render_mode = kRenderSkip;
  671. else if (GetState() == kPrerolling)
  672. render_mode = kRenderAfterPreroll;
  673. else
  674. render_mode = kRenderNow;
  675. Render(buffer_index, offset, size, render_mode, pts, eos_encountered);
  676. if (render_mode == kRenderAfterPreroll) {
  677. DVLOG(1) << class_name() << "::" << __FUNCTION__ << " pts " << pts
  678. << " >= preroll timestamp " << preroll_timestamp_
  679. << " preroll done, stopping frame processing";
  680. media_task_runner_->PostTask(FROM_HERE, internal_preroll_done_cb_);
  681. return false;
  682. }
  683. break;
  684. case MEDIA_CODEC_DEQUEUE_OUTPUT_AGAIN_LATER:
  685. // Nothing to do.
  686. DVLOG(2) << class_name() << "::" << __FUNCTION__
  687. << " MEDIA_CODEC_DEQUEUE_OUTPUT_AGAIN_LATER";
  688. break;
  689. case MEDIA_CODEC_ERROR:
  690. DVLOG(0) << class_name() << "::" << __FUNCTION__
  691. << ": MEDIA_CODEC_ERROR from DequeueOutputBuffer";
  692. media_task_runner_->PostTask(FROM_HERE, internal_error_cb_);
  693. break;
  694. default:
  695. NOTREACHED();
  696. break;
  697. }
  698. } while (status != MEDIA_CODEC_DEQUEUE_OUTPUT_AGAIN_LATER &&
  699. status != MEDIA_CODEC_ERROR && !eos_encountered);
  700. if (eos_encountered) {
  701. DVLOG(1) << class_name() << "::" << __FUNCTION__
  702. << " EOS dequeued, stopping frame processing";
  703. return false;
  704. }
  705. if (status == MEDIA_CODEC_ERROR) {
  706. DVLOG(0) << class_name() << "::" << __FUNCTION__
  707. << " MediaCodec error, stopping frame processing";
  708. return false;
  709. }
  710. return true;
  711. }
  712. MediaCodecDecoder::DecoderState MediaCodecDecoder::GetState() const {
  713. base::AutoLock lock(state_lock_);
  714. return state_;
  715. }
  716. void MediaCodecDecoder::SetState(DecoderState state) {
  717. DVLOG(1) << class_name() << "::" << __FUNCTION__ << " " << AsString(state);
  718. base::AutoLock lock(state_lock_);
  719. state_ = state;
  720. }
  721. #undef RETURN_STRING
  722. #define RETURN_STRING(x) \
  723. case x: \
  724. return #x;
  725. const char* MediaCodecDecoder::AsString(RenderMode render_mode) {
  726. switch (render_mode) {
  727. RETURN_STRING(kRenderSkip);
  728. RETURN_STRING(kRenderAfterPreroll);
  729. RETURN_STRING(kRenderNow);
  730. }
  731. return nullptr; // crash early
  732. }
  733. const char* MediaCodecDecoder::AsString(DecoderState state) {
  734. switch (state) {
  735. RETURN_STRING(kStopped);
  736. RETURN_STRING(kPrefetching);
  737. RETURN_STRING(kPrefetched);
  738. RETURN_STRING(kPrerolling);
  739. RETURN_STRING(kPrerolled);
  740. RETURN_STRING(kRunning);
  741. RETURN_STRING(kStopping);
  742. RETURN_STRING(kInEmergencyStop);
  743. RETURN_STRING(kError);
  744. }
  745. return nullptr; // crash early
  746. }
  747. #undef RETURN_STRING
  748. } // namespace media