Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(91)

Side by Side Diff: media/mojo/services/mojo_video_decoder.cc

Issue 1899363002: Finish plumbing MojoVideoDecoder. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase. Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/threading/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)));
60 }
61
62 // TODO(sandersd): Remove this indirection once a working decoder has been
63 // brought up.
64 void MojoVideoDecoder::OnInitializeDone(bool status) {
65 DVLOG(1) << __FUNCTION__;
66 DCHECK(task_runner_->BelongsToCurrentThread());
67 base::ResetAndReturn(&init_cb_).Run(status);
39 } 68 }
40 69
41 void MojoVideoDecoder::Decode(const scoped_refptr<DecoderBuffer>& buffer, 70 void MojoVideoDecoder::Decode(const scoped_refptr<DecoderBuffer>& buffer,
42 const DecodeCB& decode_cb) { 71 const DecodeCB& decode_cb) {
43 DVLOG(3) << __FUNCTION__; 72 DVLOG(1) << __FUNCTION__;
44 NOTIMPLEMENTED(); 73 DCHECK(task_runner_->BelongsToCurrentThread());
45 74
46 // Actually we can't decode anything. 75 if (has_connection_error_) {
47 task_runner_->PostTask(FROM_HERE, 76 task_runner_->PostTask(FROM_HERE,
48 base::Bind(decode_cb, DecodeStatus::DECODE_ERROR)); 77 base::Bind(decode_cb, DecodeStatus::DECODE_ERROR));
78 return;
79 }
80
81 interfaces::DecoderBufferPtr mojo_buffer =
82 interfaces::DecoderBuffer::From(buffer);
83
84 // TODO(sandersd): Destruct cleanly on error.
85 if (!buffer->end_of_stream()) {
86 uint32_t data_size = base::checked_cast<uint32_t>(buffer->data_size());
87 DCHECK_GT(data_size, 0u);
88 uint32_t bytes_written = data_size;
89 CHECK_EQ(WriteDataRaw(decoder_buffer_pipe_.get(), buffer->data(),
90 &bytes_written, MOJO_READ_DATA_FLAG_ALL_OR_NONE),
91 MOJO_RESULT_OK);
92 CHECK_EQ(bytes_written, data_size);
93 }
94
95 // TODO(sandersd): Support more than one decode at a time.
96 decode_cb_ = decode_cb;
97 remote_decoder_->Decode(
98 std::move(mojo_buffer),
99 base::Bind(&MojoVideoDecoder::OnDecodeDone, base::Unretained(this)));
49 } 100 }
50 101
51 void MojoVideoDecoder::Reset(const base::Closure& closure) { 102 void MojoVideoDecoder::OnVideoFrameDecoded(interfaces::VideoFramePtr frame) {
52 DVLOG(2) << __FUNCTION__; 103 DVLOG(1) << __FUNCTION__;
53 NOTIMPLEMENTED(); 104 DCHECK(task_runner_->BelongsToCurrentThread());
105 output_cb_.Run(frame.To<scoped_refptr<VideoFrame>>());
106 }
107
108 void MojoVideoDecoder::OnDecodeDone(interfaces::DecodeStatus status) {
109 DVLOG(1) << __FUNCTION__;
110 DCHECK(task_runner_->BelongsToCurrentThread());
111 base::ResetAndReturn(&decode_cb_).Run(static_cast<DecodeStatus>(status));
112 }
113
114 void MojoVideoDecoder::Reset(const base::Closure& reset_cb) {
115 DVLOG(1) << __FUNCTION__;
116 DCHECK(task_runner_->BelongsToCurrentThread());
117
118 if (has_connection_error_) {
119 task_runner_->PostTask(FROM_HERE, reset_cb);
120 return;
121 }
122
123 reset_cb_ = reset_cb;
124 remote_decoder_->Reset(
125 base::Bind(&MojoVideoDecoder::OnResetDone, base::Unretained(this)));
126 }
127
128 void MojoVideoDecoder::OnResetDone() {
129 DVLOG(1) << __FUNCTION__;
130 DCHECK(task_runner_->BelongsToCurrentThread());
131 base::ResetAndReturn(&reset_cb_).Run();
54 } 132 }
55 133
56 bool MojoVideoDecoder::NeedsBitstreamConversion() const { 134 bool MojoVideoDecoder::NeedsBitstreamConversion() const {
57 DVLOG(1) << __FUNCTION__; 135 DVLOG(1) << __FUNCTION__;
58 NOTIMPLEMENTED();
59 return false; 136 return false;
60 } 137 }
61 138
62 bool MojoVideoDecoder::CanReadWithoutStalling() const { 139 bool MojoVideoDecoder::CanReadWithoutStalling() const {
63 DVLOG(1) << __FUNCTION__; 140 DVLOG(1) << __FUNCTION__;
64 NOTIMPLEMENTED();
65 return true; 141 return true;
66 } 142 }
67 143
68 int MojoVideoDecoder::GetMaxDecodeRequests() const { 144 int MojoVideoDecoder::GetMaxDecodeRequests() const {
69 DVLOG(1) << __FUNCTION__; 145 DVLOG(1) << __FUNCTION__;
70 NOTIMPLEMENTED();
71 return 1; 146 return 1;
72 } 147 }
73 148
149 void MojoVideoDecoder::BindRemoteDecoder() {
150 DVLOG(1) << __FUNCTION__;
151 DCHECK(task_runner_->BelongsToCurrentThread());
152 DCHECK(!remote_decoder_bound_);
153
154 remote_decoder_.Bind(std::move(remote_decoder_info_));
155 remote_decoder_bound_ = true;
156
157 if (remote_decoder_.encountered_error()) {
158 has_connection_error_ = true;
159 return;
160 }
161
162 remote_decoder_.set_connection_error_handler(
163 base::Bind(&MojoVideoDecoder::OnConnectionError, base::Unretained(this)));
164
165 // TODO(sandersd): Better buffer sizing.
166 MojoCreateDataPipeOptions options;
167 options.struct_size = sizeof(options);
168 options.flags = MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE;
169 options.element_num_bytes = 1;
170 options.capacity_num_bytes = 2 * 1024 * 1024;
171 mojo::DataPipe decoder_buffer_pipe(options);
172
173 decoder_buffer_pipe_ = std::move(decoder_buffer_pipe.producer_handle);
174 remote_decoder_->Construct(binding_.CreateInterfacePtrAndBind(),
175 std::move(decoder_buffer_pipe.consumer_handle));
176 }
177
178 void MojoVideoDecoder::OnConnectionError() {
179 DVLOG(1) << __FUNCTION__;
180 DCHECK(task_runner_->BelongsToCurrentThread());
181
182 has_connection_error_ = true;
183
184 // TODO(sandersd): Write a wrapper class (like BindToCurrentLoop) that handles
185 // the lifetime of callbacks like this.
186 if (!init_cb_.is_null())
187 base::ResetAndReturn(&init_cb_).Run(false);
188 // TODO(sandersd): If there is a pending reset, should these be aborted?
189 if (!decode_cb_.is_null())
190 base::ResetAndReturn(&decode_cb_).Run(DecodeStatus::DECODE_ERROR);
191 if (!reset_cb_.is_null())
192 base::ResetAndReturn(&reset_cb_).Run();
193 }
194
74 } // namespace media 195 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698