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 interfaces::VideoDecoderPtr remote_decoder) |
| 22 : task_runner_(task_runner), |
| 23 remote_decoder_info_(remote_decoder.PassInterface()), |
| 24 binding_(this) { |
16 DVLOG(1) << __FUNCTION__; | 25 DVLOG(1) << __FUNCTION__; |
17 } | 26 } |
18 | 27 |
| 28 // TODO(sandersd): Figure out what to do with outstanding callbacks. |
19 MojoVideoDecoder::~MojoVideoDecoder() { | 29 MojoVideoDecoder::~MojoVideoDecoder() { |
20 DVLOG(1) << __FUNCTION__; | 30 DVLOG(1) << __FUNCTION__; |
21 } | 31 } |
22 | 32 |
23 std::string MojoVideoDecoder::GetDisplayName() const { | 33 std::string MojoVideoDecoder::GetDisplayName() const { |
24 return "MojoVideoDecoder"; | 34 return "MojoVideoDecoder"; |
25 } | 35 } |
26 | 36 |
27 void MojoVideoDecoder::Initialize(const VideoDecoderConfig& config, | 37 void MojoVideoDecoder::Initialize(const VideoDecoderConfig& config, |
28 bool low_delay, | 38 bool low_delay, |
29 CdmContext* cdm_context, | 39 CdmContext* cdm_context, |
30 const InitCB& init_cb, | 40 const InitCB& init_cb, |
31 const OutputCB& output_cb) { | 41 const OutputCB& output_cb) { |
32 DVLOG(1) << __FUNCTION__; | 42 DVLOG(1) << __FUNCTION__; |
33 task_runner_ = base::ThreadTaskRunnerHandle::Get(); | 43 DCHECK(task_runner_->BelongsToCurrentThread()); |
34 | 44 |
35 NOTIMPLEMENTED(); | 45 if (!remote_decoder_bound_) |
| 46 BindRemoteDecoder(); |
36 | 47 |
37 // Pretend to be able to decode anything. | 48 if (has_connection_error_) { |
38 task_runner_->PostTask(FROM_HERE, base::Bind(init_cb, true)); | 49 task_runner_->PostTask(FROM_HERE, base::Bind(init_cb, false)); |
| 50 return; |
| 51 } |
| 52 |
| 53 init_cb_ = init_cb; |
| 54 output_cb_ = output_cb; |
| 55 remote_decoder_->Configure( |
| 56 interfaces::VideoDecoderConfig::From(config), low_delay, |
| 57 base::Bind(&MojoVideoDecoder::OnInitialized, base::Unretained(this))); |
| 58 } |
| 59 |
| 60 void MojoVideoDecoder::OnInitialized() { |
| 61 DVLOG(1) << __FUNCTION__; |
| 62 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 63 base::ResetAndReturn(&init_cb_).Run(true); |
| 64 } |
| 65 |
| 66 void MojoVideoDecoder::BindRemoteDecoder() { |
| 67 DVLOG(1) << __FUNCTION__; |
| 68 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 69 DCHECK(!remote_decoder_bound_); |
| 70 |
| 71 remote_decoder_.Bind(std::move(remote_decoder_info_)); |
| 72 remote_decoder_bound_ = true; |
| 73 |
| 74 if (remote_decoder_.encountered_error()) { |
| 75 has_connection_error_ = true; |
| 76 return; |
| 77 } |
| 78 |
| 79 remote_decoder_.set_connection_error_handler( |
| 80 base::Bind(&MojoVideoDecoder::OnConnectionError, base::Unretained(this))); |
| 81 |
| 82 MojoCreateDataPipeOptions options; |
| 83 options.struct_size = sizeof(MojoCreateDataPipeOptions); |
| 84 options.flags = MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE; |
| 85 |
| 86 // TODO(sandersd): Better sizing. |
| 87 options.element_num_bytes = 1; |
| 88 options.capacity_num_bytes = 2 * 1024 * 1024; |
| 89 |
| 90 mojo::DataPipe decoder_buffer_pipe(options); |
| 91 decoder_buffer_producer_ = std::move(decoder_buffer_pipe.producer_handle); |
| 92 |
| 93 remote_decoder_->Initialize(binding_.CreateInterfacePtrAndBind(), |
| 94 std::move(decoder_buffer_pipe.consumer_handle)); |
39 } | 95 } |
40 | 96 |
41 void MojoVideoDecoder::Decode(const scoped_refptr<DecoderBuffer>& buffer, | 97 void MojoVideoDecoder::Decode(const scoped_refptr<DecoderBuffer>& buffer, |
42 const DecodeCB& decode_cb) { | 98 const DecodeCB& decode_cb) { |
43 DVLOG(3) << __FUNCTION__; | 99 DVLOG(1) << __FUNCTION__; |
44 NOTIMPLEMENTED(); | 100 DCHECK(task_runner_->BelongsToCurrentThread()); |
45 | 101 |
46 // Actually we can't decode anything. | 102 if (has_connection_error_) { |
47 task_runner_->PostTask(FROM_HERE, | 103 task_runner_->PostTask(FROM_HERE, |
48 base::Bind(decode_cb, DecodeStatus::DECODE_ERROR)); | 104 base::Bind(decode_cb, DecodeStatus::DECODE_ERROR)); |
| 105 return; |
| 106 } |
| 107 |
| 108 interfaces::DecoderBufferPtr mojo_buffer = |
| 109 interfaces::DecoderBuffer::From(buffer); |
| 110 |
| 111 // TODO(sandersd): Destruct cleanly on error. |
| 112 if (!buffer->end_of_stream()) { |
| 113 uint32_t data_size = base::checked_cast<uint32_t>(buffer->data_size()); |
| 114 DCHECK_GT(data_size, 0u); |
| 115 uint32_t bytes_written = data_size; |
| 116 CHECK_EQ(WriteDataRaw(decoder_buffer_producer_.get(), buffer->data(), |
| 117 &bytes_written, MOJO_READ_DATA_FLAG_ALL_OR_NONE), |
| 118 MOJO_RESULT_OK); |
| 119 CHECK_EQ(bytes_written, data_size); |
| 120 } |
| 121 |
| 122 decode_cb_ = decode_cb; |
| 123 remote_decoder_->Decode( |
| 124 std::move(mojo_buffer), |
| 125 base::Bind(&MojoVideoDecoder::OnDecoded, base::Unretained(this))); |
49 } | 126 } |
50 | 127 |
51 void MojoVideoDecoder::Reset(const base::Closure& closure) { | 128 void MojoVideoDecoder::OnDecoded(interfaces::DecodeStatus status) { |
52 DVLOG(2) << __FUNCTION__; | 129 DVLOG(1) << __FUNCTION__; |
53 NOTIMPLEMENTED(); | 130 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 131 base::ResetAndReturn(&decode_cb_).Run(static_cast<DecodeStatus>(status)); |
| 132 } |
| 133 |
| 134 void MojoVideoDecoder::Reset(const base::Closure& reset_cb) { |
| 135 DVLOG(1) << __FUNCTION__; |
| 136 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 137 |
| 138 if (has_connection_error_) { |
| 139 task_runner_->PostTask(FROM_HERE, reset_cb); |
| 140 return; |
| 141 } |
| 142 |
| 143 reset_cb_ = reset_cb; |
| 144 remote_decoder_->Reset( |
| 145 base::Bind(&MojoVideoDecoder::OnReset, base::Unretained(this))); |
| 146 } |
| 147 |
| 148 void MojoVideoDecoder::OnReset() { |
| 149 DVLOG(1) << __FUNCTION__; |
| 150 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 151 base::ResetAndReturn(&reset_cb_).Run(); |
54 } | 152 } |
55 | 153 |
56 bool MojoVideoDecoder::NeedsBitstreamConversion() const { | 154 bool MojoVideoDecoder::NeedsBitstreamConversion() const { |
57 DVLOG(1) << __FUNCTION__; | 155 DVLOG(1) << __FUNCTION__; |
58 NOTIMPLEMENTED(); | |
59 return false; | 156 return false; |
60 } | 157 } |
61 | 158 |
62 bool MojoVideoDecoder::CanReadWithoutStalling() const { | 159 bool MojoVideoDecoder::CanReadWithoutStalling() const { |
63 DVLOG(1) << __FUNCTION__; | 160 DVLOG(1) << __FUNCTION__; |
64 NOTIMPLEMENTED(); | |
65 return true; | 161 return true; |
66 } | 162 } |
67 | 163 |
68 int MojoVideoDecoder::GetMaxDecodeRequests() const { | 164 int MojoVideoDecoder::GetMaxDecodeRequests() const { |
69 DVLOG(1) << __FUNCTION__; | 165 DVLOG(1) << __FUNCTION__; |
70 NOTIMPLEMENTED(); | |
71 return 1; | 166 return 1; |
72 } | 167 } |
73 | 168 |
| 169 void MojoVideoDecoder::OnOutput(interfaces::VideoFramePtr frame) { |
| 170 DVLOG(1) << __FUNCTION__; |
| 171 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 172 output_cb_.Run(frame.To<scoped_refptr<VideoFrame>>()); |
| 173 } |
| 174 |
| 175 void MojoVideoDecoder::OnConnectionError() { |
| 176 DVLOG(1) << __FUNCTION__; |
| 177 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 178 |
| 179 has_connection_error_ = true; |
| 180 |
| 181 // TODO(sandersd): Write a wrapper class (like BindToCurrentLoop) that handles |
| 182 // the lifetime of callbacks like this. |
| 183 if (!init_cb_.is_null()) |
| 184 base::ResetAndReturn(&init_cb_).Run(false); |
| 185 if (!decode_cb_.is_null()) |
| 186 base::ResetAndReturn(&decode_cb_).Run(DecodeStatus::DECODE_ERROR); |
| 187 if (!reset_cb_.is_null()) |
| 188 base::ResetAndReturn(&reset_cb_).Run(); |
| 189 } |
| 190 |
74 } // namespace media | 191 } // namespace media |
OLD | NEW |