media_codec_audio_decoder.cc 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  1. // Copyright 2016 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/filters/android/media_codec_audio_decoder.h"
  5. #include <cmath>
  6. #include <memory>
  7. #include "base/android/build_info.h"
  8. #include "base/bind.h"
  9. #include "base/callback.h"
  10. #include "base/callback_helpers.h"
  11. #include "base/logging.h"
  12. #include "base/task/single_thread_task_runner.h"
  13. #include "base/threading/thread_task_runner_handle.h"
  14. #include "media/base/android/media_codec_bridge_impl.h"
  15. #include "media/base/android/media_codec_util.h"
  16. #include "media/base/audio_timestamp_helper.h"
  17. #include "media/base/bind_to_current_loop.h"
  18. #include "media/base/status.h"
  19. #include "media/base/timestamp_constants.h"
  20. #include "media/formats/ac3/ac3_util.h"
  21. #include "media/formats/dts/dts_util.h"
  22. #include "media/media_buildflags.h"
  23. namespace media {
  24. MediaCodecAudioDecoder::MediaCodecAudioDecoder(
  25. scoped_refptr<base::SingleThreadTaskRunner> task_runner)
  26. : task_runner_(task_runner),
  27. state_(STATE_UNINITIALIZED),
  28. is_passthrough_(false),
  29. sample_format_(kSampleFormatS16),
  30. channel_count_(0),
  31. channel_layout_(CHANNEL_LAYOUT_NONE),
  32. sample_rate_(0),
  33. media_crypto_context_(nullptr),
  34. pool_(new AudioBufferMemoryPool()) {
  35. DVLOG(1) << __func__;
  36. }
  37. MediaCodecAudioDecoder::~MediaCodecAudioDecoder() {
  38. DVLOG(1) << __func__;
  39. codec_loop_.reset();
  40. // Cancel previously registered callback (if any).
  41. if (media_crypto_context_)
  42. media_crypto_context_->SetMediaCryptoReadyCB(base::NullCallback());
  43. ClearInputQueue(DecoderStatus::Codes::kAborted);
  44. }
  45. AudioDecoderType MediaCodecAudioDecoder::GetDecoderType() const {
  46. return AudioDecoderType::kMediaCodec;
  47. }
  48. void MediaCodecAudioDecoder::Initialize(const AudioDecoderConfig& config,
  49. CdmContext* cdm_context,
  50. InitCB init_cb,
  51. const OutputCB& output_cb,
  52. const WaitingCB& waiting_cb) {
  53. DVLOG(1) << __func__ << ": " << config.AsHumanReadableString();
  54. DCHECK_NE(state_, STATE_WAITING_FOR_MEDIA_CRYPTO);
  55. DCHECK(output_cb);
  56. DCHECK(waiting_cb);
  57. // Initialization and reinitialization should not be called during pending
  58. // decode.
  59. DCHECK(input_queue_.empty());
  60. ClearInputQueue(DecoderStatus::Codes::kAborted);
  61. if (state_ == STATE_ERROR) {
  62. DVLOG(1) << "Decoder is in error state.";
  63. BindToCurrentLoop(std::move(init_cb)).Run(DecoderStatus::Codes::kFailed);
  64. return;
  65. }
  66. // We can support only the codecs that MediaCodecBridge can decode.
  67. // TODO(xhwang): Get this list from MediaCodecBridge or just rely on
  68. // attempting to create one to determine whether the codec is supported.
  69. bool platform_codec_supported = false;
  70. is_passthrough_ = false;
  71. sample_format_ = config.target_output_sample_format();
  72. switch (config.codec()) {
  73. case AudioCodec::kVorbis:
  74. case AudioCodec::kFLAC:
  75. case AudioCodec::kAAC:
  76. case AudioCodec::kOpus:
  77. platform_codec_supported = true;
  78. break;
  79. case AudioCodec::kUnknown:
  80. case AudioCodec::kMP3:
  81. case AudioCodec::kPCM:
  82. case AudioCodec::kAMR_NB:
  83. case AudioCodec::kAMR_WB:
  84. case AudioCodec::kPCM_MULAW:
  85. case AudioCodec::kGSM_MS:
  86. case AudioCodec::kPCM_S16BE:
  87. case AudioCodec::kPCM_S24BE:
  88. case AudioCodec::kPCM_ALAW:
  89. case AudioCodec::kALAC:
  90. platform_codec_supported = false;
  91. break;
  92. case AudioCodec::kAC3:
  93. case AudioCodec::kEAC3:
  94. case AudioCodec::kDTS:
  95. case AudioCodec::kDTSXP2:
  96. case AudioCodec::kMpegHAudio:
  97. is_passthrough_ = sample_format_ != kUnknownSampleFormat;
  98. // Check if MediaCodec Library supports decoding of the sample format.
  99. platform_codec_supported = MediaCodecUtil::CanDecode(config.codec());
  100. break;
  101. }
  102. // sample_format_ is stream type for pass-through. Otherwise sample_format_
  103. // should be set to kSampleFormatS16, which is what Android MediaCodec
  104. // supports for PCM decode.
  105. if (!is_passthrough_)
  106. sample_format_ = kSampleFormatS16;
  107. const bool is_codec_supported = platform_codec_supported || is_passthrough_;
  108. if (!is_codec_supported) {
  109. DVLOG(1) << "Unsupported codec " << GetCodecName(config.codec());
  110. BindToCurrentLoop(std::move(init_cb))
  111. .Run(DecoderStatus::Codes::kUnsupportedCodec);
  112. return;
  113. }
  114. config_ = config;
  115. // TODO(xhwang): Check whether BindToCurrentLoop is needed here.
  116. output_cb_ = BindToCurrentLoop(output_cb);
  117. waiting_cb_ = BindToCurrentLoop(waiting_cb);
  118. SetInitialConfiguration();
  119. if (config_.is_encrypted() && !media_crypto_) {
  120. if (!cdm_context || !cdm_context->GetMediaCryptoContext()) {
  121. LOG(ERROR) << "The stream is encrypted but there is no CdmContext or "
  122. "MediaCryptoContext is not supported";
  123. SetState(STATE_ERROR);
  124. BindToCurrentLoop(std::move(init_cb))
  125. .Run(DecoderStatus::Codes::kUnsupportedEncryptionMode);
  126. return;
  127. }
  128. // Postpone initialization after MediaCrypto is available.
  129. // SetCdm uses init_cb in a method that's already bound to the current loop.
  130. SetState(STATE_WAITING_FOR_MEDIA_CRYPTO);
  131. SetCdm(cdm_context, std::move(init_cb));
  132. return;
  133. }
  134. if (!CreateMediaCodecLoop()) {
  135. BindToCurrentLoop(std::move(init_cb)).Run(DecoderStatus::Codes::kFailed);
  136. return;
  137. }
  138. SetState(STATE_READY);
  139. BindToCurrentLoop(std::move(init_cb)).Run(DecoderStatus::Codes::kOk);
  140. }
  141. bool MediaCodecAudioDecoder::CreateMediaCodecLoop() {
  142. DVLOG(1) << __func__ << ": config:" << config_.AsHumanReadableString();
  143. codec_loop_.reset();
  144. const base::android::JavaRef<jobject>& media_crypto =
  145. media_crypto_ ? *media_crypto_ : nullptr;
  146. std::unique_ptr<MediaCodecBridge> audio_codec_bridge(
  147. MediaCodecBridgeImpl::CreateAudioDecoder(
  148. config_, media_crypto,
  149. // Use the asynchronous API if we're on Marshallow or higher.
  150. base::android::BuildInfo::GetInstance()->sdk_int() >=
  151. base::android::SDK_VERSION_MARSHMALLOW
  152. ? BindToCurrentLoop(base::BindRepeating(
  153. &MediaCodecAudioDecoder::PumpMediaCodecLoop,
  154. weak_factory_.GetWeakPtr()))
  155. : base::RepeatingClosure()));
  156. if (!audio_codec_bridge) {
  157. DLOG(ERROR) << __func__ << " failed: cannot create MediaCodecBridge";
  158. return false;
  159. }
  160. codec_loop_ = std::make_unique<MediaCodecLoop>(
  161. base::android::BuildInfo::GetInstance()->sdk_int(), this,
  162. std::move(audio_codec_bridge),
  163. scoped_refptr<base::SingleThreadTaskRunner>());
  164. return true;
  165. }
  166. void MediaCodecAudioDecoder::Decode(scoped_refptr<DecoderBuffer> buffer,
  167. DecodeCB decode_cb) {
  168. DecodeCB bound_decode_cb = BindToCurrentLoop(std::move(decode_cb));
  169. if (!buffer->end_of_stream() && buffer->timestamp() == kNoTimestamp) {
  170. DVLOG(2) << __func__ << " " << buffer->AsHumanReadableString()
  171. << ": no timestamp, skipping this buffer";
  172. std::move(bound_decode_cb).Run(DecoderStatus::Codes::kFailed);
  173. return;
  174. }
  175. // Note that we transition to STATE_ERROR if |codec_loop_| does.
  176. if (state_ == STATE_ERROR) {
  177. // We get here if an error happens in DequeueOutput() or Reset().
  178. DVLOG(2) << __func__ << " " << buffer->AsHumanReadableString()
  179. << ": Error state, returning decode error for all buffers";
  180. ClearInputQueue(DecoderStatus::Codes::kFailed);
  181. std::move(bound_decode_cb).Run(DecoderStatus::Codes::kFailed);
  182. return;
  183. }
  184. DCHECK(codec_loop_);
  185. DVLOG(3) << __func__ << " " << buffer->AsHumanReadableString();
  186. DCHECK_EQ(state_, STATE_READY) << " unexpected state " << AsString(state_);
  187. // AudioDecoder requires that "Only one decode may be in flight at any given
  188. // time".
  189. DCHECK(input_queue_.empty());
  190. input_queue_.push_back(
  191. std::make_pair(std::move(buffer), std::move(bound_decode_cb)));
  192. codec_loop_->ExpectWork();
  193. }
  194. void MediaCodecAudioDecoder::Reset(base::OnceClosure closure) {
  195. DVLOG(2) << __func__;
  196. ClearInputQueue(DecoderStatus::Codes::kAborted);
  197. // Flush if we can, otherwise completely recreate and reconfigure the codec.
  198. bool success = codec_loop_->TryFlush();
  199. // If the flush failed, then we have to re-create the codec.
  200. if (!success)
  201. success = CreateMediaCodecLoop();
  202. timestamp_helper_->SetBaseTimestamp(kNoTimestamp);
  203. SetState(success ? STATE_READY : STATE_ERROR);
  204. task_runner_->PostTask(FROM_HERE, std::move(closure));
  205. }
  206. bool MediaCodecAudioDecoder::NeedsBitstreamConversion() const {
  207. // An AAC stream needs to be converted as ADTS stream.
  208. DCHECK_NE(config_.codec(), AudioCodec::kUnknown);
  209. return config_.codec() == AudioCodec::kAAC;
  210. }
  211. void MediaCodecAudioDecoder::SetCdm(CdmContext* cdm_context, InitCB init_cb) {
  212. DVLOG(1) << __func__;
  213. DCHECK(cdm_context) << "No CDM provided";
  214. DCHECK(cdm_context->GetMediaCryptoContext());
  215. media_crypto_context_ = cdm_context->GetMediaCryptoContext();
  216. // CdmContext will always post the registered callback back to this thread.
  217. event_cb_registration_ = cdm_context->RegisterEventCB(base::BindRepeating(
  218. &MediaCodecAudioDecoder::OnCdmContextEvent, weak_factory_.GetWeakPtr()));
  219. // The callback will be posted back to this thread via BindToCurrentLoop.
  220. media_crypto_context_->SetMediaCryptoReadyCB(media::BindToCurrentLoop(
  221. base::BindOnce(&MediaCodecAudioDecoder::OnMediaCryptoReady,
  222. weak_factory_.GetWeakPtr(), std::move(init_cb))));
  223. }
  224. void MediaCodecAudioDecoder::OnCdmContextEvent(CdmContext::Event event) {
  225. DVLOG(1) << __func__;
  226. if (event != CdmContext::Event::kHasAdditionalUsableKey)
  227. return;
  228. // We don't register |codec_loop_| directly with the DRM bridge, since it's
  229. // subject to replacement.
  230. if (codec_loop_)
  231. codec_loop_->OnKeyAdded();
  232. }
  233. void MediaCodecAudioDecoder::OnMediaCryptoReady(
  234. InitCB init_cb,
  235. JavaObjectPtr media_crypto,
  236. bool /*requires_secure_video_codec*/) {
  237. DVLOG(1) << __func__;
  238. DCHECK(state_ == STATE_WAITING_FOR_MEDIA_CRYPTO);
  239. DCHECK(media_crypto);
  240. if (media_crypto->is_null()) {
  241. LOG(ERROR) << "MediaCrypto is not available, can't play encrypted stream.";
  242. SetState(STATE_UNINITIALIZED);
  243. std::move(init_cb).Run(DecoderStatus::Codes::kUnsupportedEncryptionMode);
  244. return;
  245. }
  246. media_crypto_ = std::move(media_crypto);
  247. // We assume this is a part of the initialization process, thus MediaCodec
  248. // is not created yet.
  249. DCHECK(!codec_loop_);
  250. // After receiving |media_crypto_| we can configure MediaCodec.
  251. if (!CreateMediaCodecLoop()) {
  252. SetState(STATE_UNINITIALIZED);
  253. std::move(init_cb).Run(DecoderStatus::Codes::kFailed);
  254. return;
  255. }
  256. SetState(STATE_READY);
  257. std::move(init_cb).Run(DecoderStatus::Codes::kOk);
  258. }
  259. bool MediaCodecAudioDecoder::IsAnyInputPending() const {
  260. if (state_ != STATE_READY)
  261. return false;
  262. return !input_queue_.empty();
  263. }
  264. MediaCodecLoop::InputData MediaCodecAudioDecoder::ProvideInputData() {
  265. DVLOG(3) << __func__;
  266. const DecoderBuffer* decoder_buffer = input_queue_.front().first.get();
  267. MediaCodecLoop::InputData input_data;
  268. if (decoder_buffer->end_of_stream()) {
  269. input_data.is_eos = true;
  270. } else {
  271. input_data.memory = static_cast<const uint8_t*>(decoder_buffer->data());
  272. input_data.length = decoder_buffer->data_size();
  273. const DecryptConfig* decrypt_config = decoder_buffer->decrypt_config();
  274. if (decrypt_config) {
  275. input_data.key_id = decrypt_config->key_id();
  276. input_data.iv = decrypt_config->iv();
  277. input_data.subsamples = decrypt_config->subsamples();
  278. input_data.encryption_scheme = decrypt_config->encryption_scheme();
  279. input_data.encryption_pattern = decrypt_config->encryption_pattern();
  280. }
  281. input_data.presentation_time = decoder_buffer->timestamp();
  282. }
  283. // We do not pop |input_queue_| here. MediaCodecLoop may refer to data that
  284. // it owns until OnInputDataQueued is called.
  285. return input_data;
  286. }
  287. void MediaCodecAudioDecoder::OnInputDataQueued(bool success) {
  288. // If this is an EOS buffer, then wait to call back until we are notified that
  289. // it has been processed via OnDecodedEos(). If the EOS was not queued
  290. // successfully, then we do want to signal error now since there is no queued
  291. // EOS to process later.
  292. if (input_queue_.front().first->end_of_stream() && success)
  293. return;
  294. std::move(input_queue_.front().second)
  295. .Run(success ? DecoderStatus::Codes::kOk : DecoderStatus::Codes::kFailed);
  296. input_queue_.pop_front();
  297. }
  298. void MediaCodecAudioDecoder::ClearInputQueue(DecoderStatus decode_status) {
  299. DVLOG(2) << __func__;
  300. for (auto& entry : input_queue_)
  301. std::move(entry.second).Run(decode_status);
  302. input_queue_.clear();
  303. }
  304. void MediaCodecAudioDecoder::SetState(State new_state) {
  305. DVLOG(3) << __func__ << ": " << AsString(state_) << "->"
  306. << AsString(new_state);
  307. state_ = new_state;
  308. }
  309. void MediaCodecAudioDecoder::OnCodecLoopError() {
  310. // If the codec transitions into the error state, then so should we.
  311. SetState(STATE_ERROR);
  312. ClearInputQueue(DecoderStatus::Codes::kFailed);
  313. }
  314. bool MediaCodecAudioDecoder::OnDecodedEos(
  315. const MediaCodecLoop::OutputBuffer& out) {
  316. DVLOG(2) << __func__ << " pts:" << out.pts;
  317. // Rarely, we seem to get multiple EOSes or, possibly, unsolicited ones from
  318. // MediaCodec. Just transition to the error state.
  319. // https://crbug.com/818866
  320. if (!input_queue_.size() || !input_queue_.front().first->end_of_stream()) {
  321. LOG(WARNING) << "MCAD received unexpected eos";
  322. return false;
  323. }
  324. // If we've transitioned into the error state, then we don't really know what
  325. // to do. If we transitioned because of OnCodecError, then all of our
  326. // buffers have been returned anyway. Otherwise, it's unclear. Note that
  327. // MCL does not call us back after OnCodecError(), since it stops decoding.
  328. // So, we shouldn't be in that state. So, just DCHECK here.
  329. DCHECK_NE(state_, STATE_ERROR);
  330. std::move(input_queue_.front()).second.Run(DecoderStatus::Codes::kOk);
  331. input_queue_.pop_front();
  332. return true;
  333. }
  334. bool MediaCodecAudioDecoder::OnDecodedFrame(
  335. const MediaCodecLoop::OutputBuffer& out) {
  336. DVLOG(3) << __func__ << " pts:" << out.pts;
  337. DCHECK_NE(out.size, 0U);
  338. DCHECK_NE(out.index, MediaCodecLoop::kInvalidBufferIndex);
  339. DCHECK(codec_loop_);
  340. MediaCodecBridge* media_codec = codec_loop_->GetCodec();
  341. DCHECK(media_codec);
  342. // For proper |frame_count| calculation we need to use the actual number
  343. // of channels which can be different from |config_| value.
  344. DCHECK_GT(channel_count_, 0);
  345. size_t frame_count = 1;
  346. scoped_refptr<AudioBuffer> audio_buffer;
  347. if (is_passthrough_) {
  348. audio_buffer = AudioBuffer::CreateBitstreamBuffer(
  349. sample_format_, channel_layout_, channel_count_, sample_rate_,
  350. frame_count, out.size, pool_);
  351. MediaCodecStatus status = media_codec->CopyFromOutputBuffer(
  352. out.index, out.offset, audio_buffer->channel_data()[0], out.size);
  353. if (status != MEDIA_CODEC_OK) {
  354. media_codec->ReleaseOutputBuffer(out.index, false);
  355. return false;
  356. }
  357. if (config_.codec() == AudioCodec::kAC3) {
  358. frame_count = Ac3Util::ParseTotalAc3SampleCount(
  359. audio_buffer->channel_data()[0], out.size);
  360. } else if (config_.codec() == AudioCodec::kEAC3) {
  361. frame_count = Ac3Util::ParseTotalEac3SampleCount(
  362. audio_buffer->channel_data()[0], out.size);
  363. } else if (config_.codec() == AudioCodec::kDTS) {
  364. frame_count = media::dts::ParseTotalSampleCount(
  365. audio_buffer->channel_data()[0], out.size, AudioCodec::kDTS);
  366. DVLOG(2) << ": DTS Frame Count = " << frame_count;
  367. } else {
  368. NOTREACHED() << "Unsupported passthrough format.";
  369. }
  370. // Create AudioOutput buffer based on current parameters.
  371. audio_buffer = AudioBuffer::CreateBitstreamBuffer(
  372. sample_format_, channel_layout_, channel_count_, sample_rate_,
  373. frame_count, out.size, pool_);
  374. } else {
  375. // Android MediaCodec can only output 16bit PCM audio.
  376. const int bytes_per_frame = sizeof(uint16_t) * channel_count_;
  377. frame_count = out.size / bytes_per_frame;
  378. // Create AudioOutput buffer based on current parameters.
  379. audio_buffer = AudioBuffer::CreateBuffer(sample_format_, channel_layout_,
  380. channel_count_, sample_rate_,
  381. frame_count, pool_);
  382. }
  383. // Copy data into AudioBuffer.
  384. CHECK_LE(out.size, audio_buffer->data_size());
  385. MediaCodecStatus status = media_codec->CopyFromOutputBuffer(
  386. out.index, out.offset, audio_buffer->channel_data()[0], out.size);
  387. // Release MediaCodec output buffer.
  388. media_codec->ReleaseOutputBuffer(out.index, false);
  389. if (status != MEDIA_CODEC_OK)
  390. return false;
  391. // Calculate and set buffer timestamp.
  392. const bool first_buffer = timestamp_helper_->base_timestamp() == kNoTimestamp;
  393. if (first_buffer) {
  394. // Clamp the base timestamp to zero.
  395. timestamp_helper_->SetBaseTimestamp(std::max(base::TimeDelta(), out.pts));
  396. }
  397. audio_buffer->set_timestamp(timestamp_helper_->GetTimestamp());
  398. timestamp_helper_->AddFrames(frame_count);
  399. // Call the |output_cb_|.
  400. output_cb_.Run(audio_buffer);
  401. return true;
  402. }
  403. void MediaCodecAudioDecoder::OnWaiting(WaitingReason reason) {
  404. DVLOG(2) << __func__;
  405. waiting_cb_.Run(reason);
  406. }
  407. bool MediaCodecAudioDecoder::OnOutputFormatChanged() {
  408. DVLOG(2) << __func__;
  409. MediaCodecBridge* media_codec = codec_loop_->GetCodec();
  410. // Note that if we return false to transition |codec_loop_| to the error
  411. // state, then we'll also transition to the error state when it notifies us.
  412. int new_sampling_rate = 0;
  413. MediaCodecStatus status =
  414. media_codec->GetOutputSamplingRate(&new_sampling_rate);
  415. if (status != MEDIA_CODEC_OK) {
  416. DLOG(ERROR) << "GetOutputSamplingRate failed.";
  417. return false;
  418. }
  419. if (new_sampling_rate != sample_rate_) {
  420. DVLOG(1) << __func__ << ": detected sample rate change: " << sample_rate_
  421. << " -> " << new_sampling_rate;
  422. sample_rate_ = new_sampling_rate;
  423. const base::TimeDelta base_timestamp =
  424. timestamp_helper_->base_timestamp() == kNoTimestamp
  425. ? kNoTimestamp
  426. : timestamp_helper_->GetTimestamp();
  427. timestamp_helper_ = std::make_unique<AudioTimestampHelper>(sample_rate_);
  428. if (base_timestamp != kNoTimestamp)
  429. timestamp_helper_->SetBaseTimestamp(base_timestamp);
  430. }
  431. int new_channel_count = 0;
  432. status = media_codec->GetOutputChannelCount(&new_channel_count);
  433. if (status != MEDIA_CODEC_OK || !new_channel_count) {
  434. DLOG(ERROR) << "GetOutputChannelCount failed.";
  435. return false;
  436. }
  437. if (new_channel_count != channel_count_) {
  438. DVLOG(1) << __func__
  439. << ": detected channel count change: " << channel_count_ << " -> "
  440. << new_channel_count;
  441. channel_count_ = new_channel_count;
  442. channel_layout_ = GuessChannelLayout(channel_count_);
  443. }
  444. return true;
  445. }
  446. void MediaCodecAudioDecoder::SetInitialConfiguration() {
  447. // Guess the channel count from |config_| in case OnOutputFormatChanged
  448. // that delivers the true count is not called before the first data arrives.
  449. // It seems upon certain input errors a codec may substitute silence and
  450. // not call OnOutputFormatChanged in this case.
  451. channel_layout_ = config_.channel_layout();
  452. channel_count_ = ChannelLayoutToChannelCount(channel_layout_);
  453. sample_rate_ = config_.samples_per_second();
  454. timestamp_helper_ = std::make_unique<AudioTimestampHelper>(sample_rate_);
  455. }
  456. void MediaCodecAudioDecoder::PumpMediaCodecLoop() {
  457. codec_loop_->ExpectWork();
  458. }
  459. #undef RETURN_STRING
  460. #define RETURN_STRING(x) \
  461. case x: \
  462. return #x;
  463. // static
  464. const char* MediaCodecAudioDecoder::AsString(State state) {
  465. switch (state) {
  466. RETURN_STRING(STATE_UNINITIALIZED);
  467. RETURN_STRING(STATE_WAITING_FOR_MEDIA_CRYPTO);
  468. RETURN_STRING(STATE_READY);
  469. RETURN_STRING(STATE_ERROR);
  470. }
  471. NOTREACHED() << "Unknown state " << state;
  472. return nullptr;
  473. }
  474. #undef RETURN_STRING
  475. } // namespace media