Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "media/mojo/services/mojo_video_decoder_service.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/bind_helpers.h" | |
| 9 #include "base/logging.h" | |
| 10 #include "media/base/decoder_buffer.h" | |
| 11 #include "media/base/video_decoder.h" | |
| 12 #include "media/base/video_decoder_config.h" | |
| 13 #include "media/base/video_frame.h" | |
| 14 #include "media/mojo/common/media_type_converters.h" | |
| 15 #include "media/mojo/services/mojo_media_client.h" | |
| 16 #include "mojo/public/c/system/types.h" | |
| 17 #include "mojo/public/cpp/system/buffer.h" | |
| 18 #include "mojo/public/cpp/system/handle.h" | |
| 19 | |
| 20 namespace media { | |
| 21 | |
| 22 MojoVideoDecoderService::MojoVideoDecoderService( | |
| 23 mojo::InterfaceRequest<interfaces::VideoDecoder> request, | |
| 24 MojoMediaClient* mojo_media_client) | |
| 25 : binding_(this, std::move(request)), | |
| 26 mojo_media_client_(mojo_media_client), | |
| 27 weak_factory_(this) { | |
| 28 weak_this_ = weak_factory_.GetWeakPtr(); | |
| 29 } | |
| 30 | |
| 31 MojoVideoDecoderService::~MojoVideoDecoderService() {} | |
| 32 | |
| 33 void MojoVideoDecoderService::Initialize( | |
| 34 interfaces::VideoDecoderClientPtr client, | |
| 35 mojo::ScopedDataPipeConsumerHandle decoder_buffer_pipe) { | |
| 36 DVLOG(1) << __FUNCTION__; | |
| 37 CHECK(!decoder_); | |
| 38 | |
| 39 // TODO(sandersd): Provide callback for requesting a stub. | |
| 40 decoder_ = mojo_media_client_->CreateVideoDecoder( | |
| 41 base::ThreadTaskRunnerHandle::Get()); | |
| 42 | |
| 43 client_ = std::move(client); | |
| 44 decoder_buffer_pipe_ = std::move(decoder_buffer_pipe); | |
| 45 } | |
| 46 | |
| 47 void MojoVideoDecoderService::Configure( | |
| 48 interfaces::VideoDecoderConfigPtr config, | |
| 49 bool low_delay, | |
| 50 const ConfigureCallback& callback) { | |
| 51 DVLOG(1) << __FUNCTION__; | |
| 52 | |
| 53 if (!decoder_) { | |
| 54 callback.Run(false); | |
| 55 return; | |
| 56 } | |
| 57 | |
| 58 decoder_->Initialize( | |
| 59 config.To<VideoDecoderConfig>(), low_delay, nullptr, | |
| 60 base::Bind(&MojoVideoDecoderService::OnDecoderInitialized, | |
| 61 base::Unretained(this), callback), | |
|
xhwang
2016/05/05 17:26:37
weak_this_?
| |
| 62 base::Bind(&MojoVideoDecoderService::OnDecoderOutput, | |
| 63 base::Unretained(this))); | |
| 64 } | |
| 65 | |
| 66 void MojoVideoDecoderService::OnDecoderInitialized( | |
| 67 const ConfigureCallback& callback, | |
| 68 bool success) { | |
| 69 DVLOG(1) << __FUNCTION__; | |
| 70 callback.Run(success); | |
| 71 } | |
| 72 | |
| 73 void MojoVideoDecoderService::OnDecoderOutput( | |
| 74 const scoped_refptr<VideoFrame>& frame) { | |
| 75 DVLOG(1) << __FUNCTION__; | |
| 76 client_->OnVideoFrameDecoded(interfaces::VideoFrame::From(frame)); | |
| 77 } | |
| 78 | |
| 79 void MojoVideoDecoderService::Decode(interfaces::DecoderBufferPtr buffer, | |
| 80 const DecodeCallback& callback) { | |
| 81 DVLOG(1) << __FUNCTION__; | |
| 82 | |
| 83 if (!decoder_) { | |
| 84 callback.Run(interfaces::DecodeStatus::DECODE_ERROR); | |
| 85 return; | |
| 86 } | |
| 87 | |
| 88 scoped_refptr<DecoderBuffer> media_buffer( | |
| 89 buffer.To<scoped_refptr<DecoderBuffer>>()); | |
| 90 if (!media_buffer->end_of_stream()) { | |
| 91 MojoHandleSignalsState state; | |
| 92 CHECK_EQ( | |
| 93 MojoWait(decoder_buffer_pipe_.get().value(), | |
| 94 MOJO_HANDLE_SIGNAL_READABLE, MOJO_DEADLINE_INDEFINITE, &state), | |
| 95 MOJO_RESULT_OK); | |
| 96 CHECK_EQ(state.satisfied_signals, MOJO_HANDLE_SIGNAL_READABLE); | |
| 97 uint32_t data_size = | |
| 98 base::checked_cast<uint32_t>(media_buffer->data_size()); | |
| 99 DCHECK_GT(data_size, 0u); | |
| 100 uint32_t bytes_read = data_size; | |
| 101 CHECK_EQ( | |
| 102 ReadDataRaw(decoder_buffer_pipe_.get(), media_buffer->writable_data(), | |
| 103 &bytes_read, MOJO_READ_DATA_FLAG_ALL_OR_NONE), | |
| 104 MOJO_RESULT_OK); | |
| 105 CHECK_EQ(bytes_read, data_size); | |
| 106 } | |
| 107 | |
| 108 decoder_->Decode(media_buffer, | |
| 109 base::Bind(&MojoVideoDecoderService::OnDecoderDecoded, | |
| 110 base::Unretained(this), callback)); | |
|
xhwang
2016/05/05 17:26:37
weak_this_?
sandersd (OOO until July 31)
2016/05/06 01:23:55
I totally forgot that I already set up a weak poin
| |
| 111 } | |
| 112 | |
| 113 void MojoVideoDecoderService::OnDecoderDecoded(const DecodeCallback& callback, | |
| 114 DecodeStatus status) { | |
| 115 DVLOG(1) << __FUNCTION__; | |
| 116 callback.Run(static_cast<interfaces::DecodeStatus>(status)); | |
| 117 } | |
| 118 | |
| 119 void MojoVideoDecoderService::Reset(const ResetCallback& callback) { | |
| 120 DVLOG(1) << __FUNCTION__; | |
| 121 if (!decoder_) { | |
| 122 callback.Run(); | |
| 123 return; | |
| 124 } | |
| 125 decoder_->Reset(base::Bind(&MojoVideoDecoderService::OnDecoderReset, | |
| 126 base::Unretained(this), callback)); | |
| 127 } | |
| 128 | |
| 129 void MojoVideoDecoderService::OnDecoderReset(const ResetCallback& callback) { | |
| 130 DVLOG(1) << __FUNCTION__; | |
| 131 callback.Run(); | |
| 132 } | |
| 133 | |
| 134 } // namespace media | |
| OLD | NEW |