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/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 mojom::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 mojom::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 mojom::DecoderBufferPtr mojo_buffer = mojom::DecoderBuffer::From(buffer); |
| 82 |
| 83 // TODO(sandersd): Destruct cleanly on error. |
| 84 if (!buffer->end_of_stream()) { |
| 85 uint32_t data_size = base::checked_cast<uint32_t>(buffer->data_size()); |
| 86 DCHECK_GT(data_size, 0u); |
| 87 uint32_t bytes_written = data_size; |
| 88 CHECK_EQ(WriteDataRaw(decoder_buffer_pipe_.get(), buffer->data(), |
| 89 &bytes_written, MOJO_READ_DATA_FLAG_ALL_OR_NONE), |
| 90 MOJO_RESULT_OK); |
| 91 CHECK_EQ(bytes_written, data_size); |
| 92 } |
| 93 |
| 94 // TODO(sandersd): Support more than one decode at a time. |
| 95 decode_cb_ = decode_cb; |
| 96 remote_decoder_->Decode( |
| 97 std::move(mojo_buffer), |
| 98 base::Bind(&MojoVideoDecoder::OnDecodeDone, base::Unretained(this))); |
49 } | 99 } |
50 | 100 |
51 void MojoVideoDecoder::Reset(const base::Closure& closure) { | 101 void MojoVideoDecoder::OnVideoFrameDecoded(mojom::VideoFramePtr frame) { |
52 DVLOG(2) << __FUNCTION__; | 102 DVLOG(1) << __FUNCTION__; |
53 NOTIMPLEMENTED(); | 103 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 104 output_cb_.Run(frame.To<scoped_refptr<VideoFrame>>()); |
| 105 } |
| 106 |
| 107 void MojoVideoDecoder::OnDecodeDone(mojom::DecodeStatus status) { |
| 108 DVLOG(1) << __FUNCTION__; |
| 109 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 110 base::ResetAndReturn(&decode_cb_).Run(static_cast<DecodeStatus>(status)); |
| 111 } |
| 112 |
| 113 void MojoVideoDecoder::Reset(const base::Closure& reset_cb) { |
| 114 DVLOG(1) << __FUNCTION__; |
| 115 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 116 |
| 117 if (has_connection_error_) { |
| 118 task_runner_->PostTask(FROM_HERE, reset_cb); |
| 119 return; |
| 120 } |
| 121 |
| 122 reset_cb_ = reset_cb; |
| 123 remote_decoder_->Reset( |
| 124 base::Bind(&MojoVideoDecoder::OnResetDone, base::Unretained(this))); |
| 125 } |
| 126 |
| 127 void MojoVideoDecoder::OnResetDone() { |
| 128 DVLOG(1) << __FUNCTION__; |
| 129 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 130 base::ResetAndReturn(&reset_cb_).Run(); |
54 } | 131 } |
55 | 132 |
56 bool MojoVideoDecoder::NeedsBitstreamConversion() const { | 133 bool MojoVideoDecoder::NeedsBitstreamConversion() const { |
57 DVLOG(1) << __FUNCTION__; | 134 DVLOG(1) << __FUNCTION__; |
58 NOTIMPLEMENTED(); | |
59 return false; | 135 return false; |
60 } | 136 } |
61 | 137 |
62 bool MojoVideoDecoder::CanReadWithoutStalling() const { | 138 bool MojoVideoDecoder::CanReadWithoutStalling() const { |
63 DVLOG(1) << __FUNCTION__; | 139 DVLOG(1) << __FUNCTION__; |
64 NOTIMPLEMENTED(); | |
65 return true; | 140 return true; |
66 } | 141 } |
67 | 142 |
68 int MojoVideoDecoder::GetMaxDecodeRequests() const { | 143 int MojoVideoDecoder::GetMaxDecodeRequests() const { |
69 DVLOG(1) << __FUNCTION__; | 144 DVLOG(1) << __FUNCTION__; |
70 NOTIMPLEMENTED(); | |
71 return 1; | 145 return 1; |
72 } | 146 } |
73 | 147 |
| 148 void MojoVideoDecoder::BindRemoteDecoder() { |
| 149 DVLOG(1) << __FUNCTION__; |
| 150 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 151 DCHECK(!remote_decoder_bound_); |
| 152 |
| 153 remote_decoder_.Bind(std::move(remote_decoder_info_)); |
| 154 remote_decoder_bound_ = true; |
| 155 |
| 156 if (remote_decoder_.encountered_error()) { |
| 157 has_connection_error_ = true; |
| 158 return; |
| 159 } |
| 160 |
| 161 remote_decoder_.set_connection_error_handler( |
| 162 base::Bind(&MojoVideoDecoder::OnConnectionError, base::Unretained(this))); |
| 163 |
| 164 // TODO(sandersd): Better buffer sizing. |
| 165 MojoCreateDataPipeOptions options; |
| 166 options.struct_size = sizeof(options); |
| 167 options.flags = MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE; |
| 168 options.element_num_bytes = 1; |
| 169 options.capacity_num_bytes = 2 * 1024 * 1024; |
| 170 mojo::DataPipe decoder_buffer_pipe(options); |
| 171 |
| 172 decoder_buffer_pipe_ = std::move(decoder_buffer_pipe.producer_handle); |
| 173 remote_decoder_->Construct(binding_.CreateInterfacePtrAndBind(), |
| 174 std::move(decoder_buffer_pipe.consumer_handle)); |
| 175 } |
| 176 |
| 177 void MojoVideoDecoder::OnConnectionError() { |
| 178 DVLOG(1) << __FUNCTION__; |
| 179 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 180 |
| 181 has_connection_error_ = true; |
| 182 |
| 183 // TODO(sandersd): Write a wrapper class (like BindToCurrentLoop) that handles |
| 184 // the lifetime of callbacks like this. |
| 185 if (!init_cb_.is_null()) |
| 186 base::ResetAndReturn(&init_cb_).Run(false); |
| 187 // TODO(sandersd): If there is a pending reset, should these be aborted? |
| 188 if (!decode_cb_.is_null()) |
| 189 base::ResetAndReturn(&decode_cb_).Run(DecodeStatus::DECODE_ERROR); |
| 190 if (!reset_cb_.is_null()) |
| 191 base::ResetAndReturn(&reset_cb_).Run(); |
| 192 } |
| 193 |
74 } // namespace media | 194 } // namespace media |
OLD | NEW |