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_video_decoder.h" | 5 #include "media/mojo/services/mojo_video_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 "media/base/decoder_buffer.h" |
14 #include "media/base/video_frame.h" | |
15 #include "media/mojo/common/media_type_converters.h" | |
12 | 16 |
13 namespace media { | 17 namespace media { |
14 | 18 |
15 MojoVideoDecoder::MojoVideoDecoder() { | 19 MojoVideoDecoder::MojoVideoDecoder( |
20 scoped_refptr<base::SingleThreadTaskRunner> task_runner, | |
21 GpuVideoAcceleratorFactories* gpu_factories, | |
22 interfaces::VideoDecoderPtr remote_decoder) | |
23 : task_runner_(task_runner), | |
24 gpu_factories_(gpu_factories), | |
25 remote_decoder_info_(remote_decoder.PassInterface()), | |
26 binding_(this) { | |
16 DVLOG(1) << __FUNCTION__; | 27 DVLOG(1) << __FUNCTION__; |
17 } | 28 } |
18 | 29 |
19 MojoVideoDecoder::~MojoVideoDecoder() { | 30 MojoVideoDecoder::~MojoVideoDecoder() { |
20 DVLOG(1) << __FUNCTION__; | 31 DVLOG(1) << __FUNCTION__; |
21 } | 32 } |
22 | 33 |
23 std::string MojoVideoDecoder::GetDisplayName() const { | 34 std::string MojoVideoDecoder::GetDisplayName() const { |
24 return "MojoVideoDecoder"; | 35 return "MojoVideoDecoder"; |
25 } | 36 } |
26 | 37 |
27 void MojoVideoDecoder::Initialize(const VideoDecoderConfig& config, | 38 void MojoVideoDecoder::Initialize(const VideoDecoderConfig& config, |
28 bool low_delay, | 39 bool low_delay, |
29 CdmContext* cdm_context, | 40 CdmContext* cdm_context, |
30 const InitCB& init_cb, | 41 const InitCB& init_cb, |
31 const OutputCB& output_cb) { | 42 const OutputCB& output_cb) { |
32 DVLOG(1) << __FUNCTION__; | 43 DVLOG(1) << __FUNCTION__; |
33 task_runner_ = base::ThreadTaskRunnerHandle::Get(); | 44 DCHECK(task_runner_->BelongsToCurrentThread()); |
45 DCHECK(!cdm_context); | |
34 | 46 |
35 NOTIMPLEMENTED(); | 47 if (!remote_decoder_bound_) |
48 BindRemoteDecoder(); | |
36 | 49 |
37 // Pretend to be able to decode anything. | 50 if (has_connection_error_) { |
38 task_runner_->PostTask(FROM_HERE, base::Bind(init_cb, true)); | 51 task_runner_->PostTask(FROM_HERE, base::Bind(init_cb, false)); |
52 return; | |
53 } | |
54 | |
55 init_cb_ = init_cb; | |
56 output_cb_ = output_cb; | |
57 remote_decoder_->Initialize( | |
58 interfaces::VideoDecoderConfig::From(config), low_delay, | |
59 base::Bind(&MojoVideoDecoder::OnInitializeDone, base::Unretained(this))); | |
dcheng
2016/05/11 07:37:45
Why can't we just pass init_cb directly here? Simi
xhwang
2016/05/11 16:13:57
Last time I checked, you can't implicitly convert
dcheng
2016/05/11 17:02:50
How is using base::Bind() to create a base::Callba
xhwang
2016/05/11 17:14:58
oops, this is actually different than what I was t
sandersd (OOO until July 31)
2016/05/11 18:20:18
The truth is that it's an artifact of changes to t
dcheng
2016/05/11 22:15:06
When will we actually remove them? Code like this
sandersd (OOO until July 31)
2016/05/11 23:00:50
TODO added.
| |
60 } | |
61 | |
62 void MojoVideoDecoder::OnInitializeDone(bool status) { | |
63 DVLOG(1) << __FUNCTION__; | |
64 DCHECK(task_runner_->BelongsToCurrentThread()); | |
65 base::ResetAndReturn(&init_cb_).Run(status); | |
39 } | 66 } |
40 | 67 |
41 void MojoVideoDecoder::Decode(const scoped_refptr<DecoderBuffer>& buffer, | 68 void MojoVideoDecoder::Decode(const scoped_refptr<DecoderBuffer>& buffer, |
42 const DecodeCB& decode_cb) { | 69 const DecodeCB& decode_cb) { |
43 DVLOG(3) << __FUNCTION__; | 70 DVLOG(1) << __FUNCTION__; |
44 NOTIMPLEMENTED(); | 71 DCHECK(task_runner_->BelongsToCurrentThread()); |
45 | 72 |
46 // Actually we can't decode anything. | 73 if (has_connection_error_) { |
47 task_runner_->PostTask(FROM_HERE, | 74 task_runner_->PostTask(FROM_HERE, |
48 base::Bind(decode_cb, DecodeStatus::DECODE_ERROR)); | 75 base::Bind(decode_cb, DecodeStatus::DECODE_ERROR)); |
76 return; | |
77 } | |
78 | |
79 interfaces::DecoderBufferPtr mojo_buffer = | |
80 interfaces::DecoderBuffer::From(buffer); | |
81 | |
82 // TODO(sandersd): Destruct cleanly on error. | |
83 if (!buffer->end_of_stream()) { | |
84 uint32_t data_size = base::checked_cast<uint32_t>(buffer->data_size()); | |
dcheng
2016/05/11 07:37:45
This is... in the renderer process? It's a bit har
xhwang
2016/05/11 16:13:57
I don't see examples of keeping such convention wh
dcheng
2016/05/11 17:02:50
Well, we should figure one out. I'll start a threa
sandersd (OOO until July 31)
2016/05/12 18:10:36
Since there isn't guidance from that thread yet, p
sandersd (OOO until July 31)
2016/05/17 23:05:00
I've added basic comments to the class headers. Le
| |
85 DCHECK_GT(data_size, 0u); | |
86 uint32_t bytes_written = data_size; | |
87 CHECK_EQ(WriteDataRaw(decoder_buffer_pipe_.get(), buffer->data(), | |
88 &bytes_written, MOJO_READ_DATA_FLAG_ALL_OR_NONE), | |
89 MOJO_RESULT_OK); | |
90 CHECK_EQ(bytes_written, data_size); | |
91 } | |
92 | |
93 // TODO(sandersd): Support more than one decode at a time. | |
94 decode_cb_ = decode_cb; | |
95 remote_decoder_->Decode( | |
96 std::move(mojo_buffer), | |
97 base::Bind(&MojoVideoDecoder::OnDecodeDone, base::Unretained(this))); | |
49 } | 98 } |
50 | 99 |
51 void MojoVideoDecoder::Reset(const base::Closure& closure) { | 100 void MojoVideoDecoder::OnVideoFrameDecoded(interfaces::VideoFramePtr frame) { |
52 DVLOG(2) << __FUNCTION__; | 101 DVLOG(1) << __FUNCTION__; |
53 NOTIMPLEMENTED(); | 102 DCHECK(task_runner_->BelongsToCurrentThread()); |
103 output_cb_.Run(frame.To<scoped_refptr<VideoFrame>>()); | |
104 } | |
105 | |
106 void MojoVideoDecoder::OnDecodeDone(interfaces::DecodeStatus status) { | |
107 DVLOG(1) << __FUNCTION__; | |
108 DCHECK(task_runner_->BelongsToCurrentThread()); | |
109 base::ResetAndReturn(&decode_cb_).Run(static_cast<DecodeStatus>(status)); | |
110 } | |
111 | |
112 void MojoVideoDecoder::Reset(const base::Closure& reset_cb) { | |
113 DVLOG(1) << __FUNCTION__; | |
114 DCHECK(task_runner_->BelongsToCurrentThread()); | |
115 | |
116 if (has_connection_error_) { | |
117 task_runner_->PostTask(FROM_HERE, reset_cb); | |
118 return; | |
119 } | |
120 | |
121 reset_cb_ = reset_cb; | |
122 remote_decoder_->Reset( | |
123 base::Bind(&MojoVideoDecoder::OnResetDone, base::Unretained(this))); | |
124 } | |
125 | |
126 void MojoVideoDecoder::OnResetDone() { | |
127 DVLOG(1) << __FUNCTION__; | |
128 DCHECK(task_runner_->BelongsToCurrentThread()); | |
129 base::ResetAndReturn(&reset_cb_).Run(); | |
54 } | 130 } |
55 | 131 |
56 bool MojoVideoDecoder::NeedsBitstreamConversion() const { | 132 bool MojoVideoDecoder::NeedsBitstreamConversion() const { |
57 DVLOG(1) << __FUNCTION__; | 133 DVLOG(1) << __FUNCTION__; |
58 NOTIMPLEMENTED(); | |
59 return false; | 134 return false; |
60 } | 135 } |
61 | 136 |
62 bool MojoVideoDecoder::CanReadWithoutStalling() const { | 137 bool MojoVideoDecoder::CanReadWithoutStalling() const { |
63 DVLOG(1) << __FUNCTION__; | 138 DVLOG(1) << __FUNCTION__; |
64 NOTIMPLEMENTED(); | |
65 return true; | 139 return true; |
66 } | 140 } |
67 | 141 |
68 int MojoVideoDecoder::GetMaxDecodeRequests() const { | 142 int MojoVideoDecoder::GetMaxDecodeRequests() const { |
69 DVLOG(1) << __FUNCTION__; | 143 DVLOG(1) << __FUNCTION__; |
70 NOTIMPLEMENTED(); | |
71 return 1; | 144 return 1; |
72 } | 145 } |
73 | 146 |
147 void MojoVideoDecoder::BindRemoteDecoder() { | |
148 DVLOG(1) << __FUNCTION__; | |
149 DCHECK(task_runner_->BelongsToCurrentThread()); | |
150 DCHECK(!remote_decoder_bound_); | |
151 | |
152 remote_decoder_.Bind(std::move(remote_decoder_info_)); | |
153 remote_decoder_bound_ = true; | |
154 | |
155 if (remote_decoder_.encountered_error()) { | |
156 has_connection_error_ = true; | |
157 return; | |
158 } | |
159 | |
160 remote_decoder_.set_connection_error_handler( | |
161 base::Bind(&MojoVideoDecoder::OnConnectionError, base::Unretained(this))); | |
162 | |
163 // TODO(sandersd): Better buffer sizing. | |
164 MojoCreateDataPipeOptions options; | |
165 options.struct_size = sizeof(MojoCreateDataPipeOptions); | |
dcheng
2016/05/11 07:37:45
Style guide recommends using sizeof(options).
sandersd (OOO until July 31)
2016/05/11 18:20:18
Done.
| |
166 options.flags = MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE; | |
167 options.element_num_bytes = 1; | |
168 options.capacity_num_bytes = 2 * 1024 * 1024; | |
169 mojo::DataPipe decoder_buffer_pipe(options); | |
170 | |
171 decoder_buffer_pipe_ = std::move(decoder_buffer_pipe.producer_handle); | |
172 remote_decoder_->Construct(binding_.CreateInterfacePtrAndBind(), | |
173 std::move(decoder_buffer_pipe.consumer_handle)); | |
174 } | |
175 | |
176 void MojoVideoDecoder::OnConnectionError() { | |
177 DVLOG(1) << __FUNCTION__; | |
178 DCHECK(task_runner_->BelongsToCurrentThread()); | |
179 | |
180 has_connection_error_ = true; | |
181 | |
182 // TODO(sandersd): Write a wrapper class (like BindToCurrentLoop) that handles | |
183 // the lifetime of callbacks like this. | |
184 if (!init_cb_.is_null()) | |
185 base::ResetAndReturn(&init_cb_).Run(false); | |
186 // TODO(sandersd): If there is a pending reset, should these be aborted? | |
187 if (!decode_cb_.is_null()) | |
188 base::ResetAndReturn(&decode_cb_).Run(DecodeStatus::DECODE_ERROR); | |
189 if (!reset_cb_.is_null()) | |
190 base::ResetAndReturn(&reset_cb_).Run(); | |
dcheng
2016/05/11 07:37:45
I think it'd be better if there was a separate cal
sandersd (OOO until July 31)
2016/05/11 18:20:18
We're constrained to implement the media::VideoDec
| |
191 } | |
192 | |
74 } // namespace media | 193 } // namespace media |
OLD | NEW |