OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "media/mojo/services/mojo_audio_decoder.h" | 5 #include "media/mojo/services/mojo_audio_decoder.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" |
| 9 #include "base/callback_helpers.h" |
8 #include "base/location.h" | 10 #include "base/location.h" |
9 #include "base/logging.h" | 11 #include "base/logging.h" |
10 #include "base/single_thread_task_runner.h" | 12 #include "base/single_thread_task_runner.h" |
11 #include "base/thread_task_runner_handle.h" | 13 #include "base/thread_task_runner_handle.h" |
| 14 #include "media/base/cdm_context.h" |
| 15 #include "media/mojo/common/media_type_converters.h" |
12 | 16 |
13 namespace media { | 17 namespace media { |
14 | 18 |
15 MojoAudioDecoder::MojoAudioDecoder( | 19 MojoAudioDecoder::MojoAudioDecoder( |
16 scoped_refptr<base::SingleThreadTaskRunner> task_runner, | 20 scoped_refptr<base::SingleThreadTaskRunner> task_runner, |
17 interfaces::AudioDecoderPtr remote_decoder) | 21 interfaces::AudioDecoderPtr remote_decoder) |
18 : task_runner_(task_runner), remote_decoder_(std::move(remote_decoder)) { | 22 : task_runner_(task_runner), |
| 23 remote_decoder_(std::move(remote_decoder)), |
| 24 binding_(this), |
| 25 has_connection_error_(false), |
| 26 needs_bitstream_conversion_(false) { |
19 DVLOG(1) << __FUNCTION__; | 27 DVLOG(1) << __FUNCTION__; |
20 } | 28 } |
21 | 29 |
22 MojoAudioDecoder::~MojoAudioDecoder() { | 30 MojoAudioDecoder::~MojoAudioDecoder() { |
23 DVLOG(1) << __FUNCTION__; | 31 DVLOG(1) << __FUNCTION__; |
24 } | 32 } |
25 | 33 |
26 std::string MojoAudioDecoder::GetDisplayName() const { | 34 std::string MojoAudioDecoder::GetDisplayName() const { |
27 return "MojoAudioDecoder"; | 35 return "MojoAudioDecoder"; |
28 } | 36 } |
29 | 37 |
30 void MojoAudioDecoder::Initialize(const AudioDecoderConfig& config, | 38 void MojoAudioDecoder::Initialize(const AudioDecoderConfig& config, |
31 CdmContext* cdm_context, | 39 CdmContext* cdm_context, |
32 const InitCB& init_cb, | 40 const InitCB& init_cb, |
33 const OutputCB& output_cb) { | 41 const OutputCB& output_cb) { |
34 DVLOG(1) << __FUNCTION__; | 42 DVLOG(1) << __FUNCTION__; |
35 DCHECK(task_runner_->BelongsToCurrentThread()); | 43 DCHECK(task_runner_->BelongsToCurrentThread()); |
36 | 44 |
37 NOTIMPLEMENTED(); | 45 // Fail immediately if the stream is encrypted but |cdm_context| is invalid. |
| 46 int cdm_id = (config.is_encrypted() && cdm_context) |
| 47 ? cdm_context->GetCdmId() |
| 48 : CdmContext::kInvalidCdmId; |
38 | 49 |
39 // Fail initialization while not implemented. | 50 if (config.is_encrypted() && CdmContext::kInvalidCdmId == cdm_id) { |
40 task_runner_->PostTask(FROM_HERE, base::Bind(init_cb, false)); | 51 task_runner_->PostTask(FROM_HERE, base::Bind(init_cb, false)); |
| 52 return; |
| 53 } |
| 54 |
| 55 // If connection error has happened, fail immediately. |
| 56 if (remote_decoder_.encountered_error()) { |
| 57 task_runner_->PostTask(FROM_HERE, base::Bind(init_cb, false)); |
| 58 return; |
| 59 } |
| 60 |
| 61 // Otherwise, set an error handler to catch the connection error. |
| 62 // Using base::Unretained(this) is safe because |this| owns |remote_decoder_|, |
| 63 // and the error handler can't be invoked once |remote_decoder_| is destroyed. |
| 64 remote_decoder_.set_connection_error_handler( |
| 65 base::Bind(&MojoAudioDecoder::OnConnectionError, base::Unretained(this))); |
| 66 |
| 67 init_cb_ = init_cb; |
| 68 |
| 69 // Using base::Unretained(this) is safe because |this| owns |remote_decoder_|, |
| 70 // and the callback won't be dispatched if |remote_decoder_| is destroyed. |
| 71 remote_decoder_->Initialize( |
| 72 binding_.CreateInterfacePtrAndBind(), |
| 73 interfaces::AudioDecoderConfig::From(config), cdm_id, |
| 74 base::Bind(&MojoAudioDecoder::OnInitialized, base::Unretained(this))); |
41 } | 75 } |
42 | 76 |
43 void MojoAudioDecoder::Decode(const scoped_refptr<DecoderBuffer>& buffer, | 77 void MojoAudioDecoder::Decode(const scoped_refptr<DecoderBuffer>& buffer, |
44 const DecodeCB& decode_cb) { | 78 const DecodeCB& decode_cb) { |
45 DVLOG(3) << __FUNCTION__; | 79 DVLOG(3) << __FUNCTION__; |
46 DCHECK(task_runner_->BelongsToCurrentThread()); | 80 DCHECK(task_runner_->BelongsToCurrentThread()); |
47 | 81 |
48 NOTIMPLEMENTED(); | 82 if (has_connection_error_) { |
| 83 task_runner_->PostTask(FROM_HERE, base::Bind(decode_cb, kDecodeError)); |
| 84 return; |
| 85 } |
49 | 86 |
50 // Actually we can't decode anything. | 87 DCHECK(decode_cb_.is_null()); |
51 task_runner_->PostTask(FROM_HERE, base::Bind(decode_cb, kDecodeError)); | 88 decode_cb_ = decode_cb; |
| 89 |
| 90 // This code won't work because |buffer| is not serialized. |
| 91 // TODO(timav): Serialize DecodeBuffer into data pipe. |
| 92 remote_decoder_->Decode( |
| 93 interfaces::DecoderBuffer::From(buffer), |
| 94 base::Bind(&MojoAudioDecoder::OnDecodeStatus, base::Unretained(this))); |
52 } | 95 } |
53 | 96 |
54 void MojoAudioDecoder::Reset(const base::Closure& closure) { | 97 void MojoAudioDecoder::Reset(const base::Closure& closure) { |
55 DVLOG(2) << __FUNCTION__; | 98 DVLOG(2) << __FUNCTION__; |
56 DCHECK(task_runner_->BelongsToCurrentThread()); | 99 DCHECK(task_runner_->BelongsToCurrentThread()); |
57 | 100 |
58 NOTIMPLEMENTED(); | 101 if (has_connection_error_) { |
| 102 if (!decode_cb_.is_null()) { |
| 103 task_runner_->PostTask( |
| 104 FROM_HERE, |
| 105 base::Bind(base::ResetAndReturn(&decode_cb_), kDecodeError)); |
| 106 } |
| 107 |
| 108 task_runner_->PostTask(FROM_HERE, closure); |
| 109 return; |
| 110 } |
| 111 |
| 112 DCHECK(reset_cb_.is_null()); |
| 113 reset_cb_ = closure; |
| 114 remote_decoder_->Reset( |
| 115 base::Bind(&MojoAudioDecoder::OnResetDone, base::Unretained(this))); |
59 } | 116 } |
60 | 117 |
61 bool MojoAudioDecoder::NeedsBitstreamConversion() const { | 118 bool MojoAudioDecoder::NeedsBitstreamConversion() const { |
62 DVLOG(1) << __FUNCTION__; | 119 DVLOG(1) << __FUNCTION__; |
63 DCHECK(task_runner_->BelongsToCurrentThread()); | 120 DCHECK(task_runner_->BelongsToCurrentThread()); |
64 | 121 |
| 122 return needs_bitstream_conversion_; |
| 123 } |
| 124 |
| 125 void MojoAudioDecoder::OnBufferDecoded(interfaces::AudioBufferPtr buffer) { |
| 126 DVLOG(1) << __FUNCTION__; |
| 127 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 128 |
65 NOTIMPLEMENTED(); | 129 NOTIMPLEMENTED(); |
66 return false; | 130 } |
| 131 |
| 132 void MojoAudioDecoder::OnConnectionError() { |
| 133 DVLOG(1) << __FUNCTION__; |
| 134 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 135 |
| 136 has_connection_error_ = true; |
| 137 |
| 138 if (!init_cb_.is_null()) { |
| 139 base::ResetAndReturn(&init_cb_).Run(false); |
| 140 return; |
| 141 } |
| 142 |
| 143 if (!decode_cb_.is_null()) |
| 144 base::ResetAndReturn(&decode_cb_).Run(kDecodeError); |
| 145 if (!reset_cb_.is_null()) |
| 146 base::ResetAndReturn(&reset_cb_).Run(); |
| 147 } |
| 148 |
| 149 void MojoAudioDecoder::OnInitialized(bool status, |
| 150 bool needs_bitstream_conversion) { |
| 151 DVLOG(1) << __FUNCTION__ << ": status:" << status; |
| 152 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 153 |
| 154 needs_bitstream_conversion_ = needs_bitstream_conversion; |
| 155 |
| 156 task_runner_->PostTask(FROM_HERE, base::Bind(init_cb_, status)); |
| 157 } |
| 158 |
| 159 static media::AudioDecoder::Status ConvertDecodeStatus( |
| 160 interfaces::AudioDecoder::DecodeStatus status) { |
| 161 switch (status) { |
| 162 case interfaces::AudioDecoder::DecodeStatus::OK: |
| 163 return media::AudioDecoder::kOk; |
| 164 case interfaces::AudioDecoder::DecodeStatus::ABORTED: |
| 165 return media::AudioDecoder::kAborted; |
| 166 case interfaces::AudioDecoder::DecodeStatus::DECODE_ERROR: |
| 167 return media::AudioDecoder::kDecodeError; |
| 168 } |
| 169 NOTREACHED(); |
| 170 return media::AudioDecoder::kDecodeError; |
| 171 } |
| 172 |
| 173 void MojoAudioDecoder::OnDecodeStatus( |
| 174 interfaces::AudioDecoder::DecodeStatus status) { |
| 175 DVLOG(1) << __FUNCTION__ << ": status:" << status; |
| 176 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 177 |
| 178 DCHECK(!decode_cb_.is_null()); |
| 179 base::ResetAndReturn(&decode_cb_).Run(ConvertDecodeStatus(status)); |
| 180 } |
| 181 |
| 182 void MojoAudioDecoder::OnResetDone() { |
| 183 DVLOG(1) << __FUNCTION__; |
| 184 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 185 |
| 186 // For pending decodes OnDecodeStatus() should arrive before OnResetDone(). |
| 187 DCHECK(decode_cb_.is_null()); |
| 188 |
| 189 DCHECK(!reset_cb_.is_null()); |
| 190 base::ResetAndReturn(&reset_cb_).Run(); |
67 } | 191 } |
68 | 192 |
69 } // namespace media | 193 } // namespace media |
OLD | NEW |