Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 1 | // Copyright 2019 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/gpu/v4l2/v4l2_slice_video_decoder.h" |
| 6 | |
| 7 | #include <algorithm> |
| 8 | |
| 9 | #include "base/bind.h" |
Chih-Yu Huang | 63b0a1d | 2019-11-19 09:31:45 | [diff] [blame] | 10 | #include "base/callback_helpers.h" |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 11 | #include "base/logging.h" |
| 12 | #include "base/memory/ptr_util.h" |
| 13 | #include "base/task/post_task.h" |
Alexandre Courbot | 139ebe2 | 2019-12-12 01:46:39 | [diff] [blame] | 14 | #include "media/base/video_types.h" |
Chih-Yu Huang | f463589 | 2019-07-11 14:20:29 | [diff] [blame] | 15 | #include "media/base/video_util.h" |
Chih-Yu Huang | 5dc635d6 | 2019-10-25 10:30:39 | [diff] [blame] | 16 | #include "media/gpu/chromeos/dmabuf_video_frame_pool.h" |
Shuo-Peng Liao | 761b0ec7 | 2019-10-09 05:00:20 | [diff] [blame] | 17 | #include "media/gpu/chromeos/fourcc.h" |
Chih-Yu Huang | a4bf834 | 2019-06-27 04:03:44 | [diff] [blame] | 18 | #include "media/gpu/gpu_video_decode_accelerator_helpers.h" |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 19 | #include "media/gpu/macros.h" |
Alexandre Courbot | 78b1e64 | 2019-10-16 03:45:39 | [diff] [blame] | 20 | #include "media/gpu/v4l2/v4l2_video_decoder_backend_stateless.h" |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 21 | |
| 22 | namespace media { |
| 23 | |
| 24 | namespace { |
| 25 | |
| 26 | // See http://crbug.com/255116. |
| 27 | constexpr int k1080pArea = 1920 * 1088; |
| 28 | // Input bitstream buffer size for up to 1080p streams. |
| 29 | constexpr size_t kInputBufferMaxSizeFor1080p = 1024 * 1024; |
| 30 | // Input bitstream buffer size for up to 4k streams. |
| 31 | constexpr size_t kInputBufferMaxSizeFor4k = 4 * kInputBufferMaxSizeFor1080p; |
| 32 | constexpr size_t kNumInputBuffers = 16; |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 33 | |
Chih-Yu Huang | a4bf834 | 2019-06-27 04:03:44 | [diff] [blame] | 34 | // Input format V4L2 fourccs this class supports. |
| 35 | constexpr uint32_t kSupportedInputFourccs[] = { |
| 36 | V4L2_PIX_FMT_H264_SLICE, |
| 37 | V4L2_PIX_FMT_VP8_FRAME, |
| 38 | V4L2_PIX_FMT_VP9_FRAME, |
| 39 | }; |
| 40 | |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 41 | } // namespace |
| 42 | |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 43 | // static |
Chih-Yu Huang | 63b0a1d | 2019-11-19 09:31:45 | [diff] [blame] | 44 | std::unique_ptr<DecoderInterface> V4L2SliceVideoDecoder::Create( |
Chih-Yu Huang | fc16380 | 2019-09-02 06:17:48 | [diff] [blame] | 45 | scoped_refptr<base::SequencedTaskRunner> decoder_task_runner, |
Chih-Yu Huang | 63b0a1d | 2019-11-19 09:31:45 | [diff] [blame] | 46 | base::WeakPtr<DecoderInterface::Client> client) { |
Chih-Yu Huang | a35c28a | 2019-10-21 02:43:09 | [diff] [blame] | 47 | DCHECK(decoder_task_runner->RunsTasksInCurrentSequence()); |
Chih-Yu Huang | 63b0a1d | 2019-11-19 09:31:45 | [diff] [blame] | 48 | DCHECK(client); |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 49 | |
| 50 | scoped_refptr<V4L2Device> device = V4L2Device::Create(); |
| 51 | if (!device) { |
| 52 | VLOGF(1) << "Failed to create V4L2 device."; |
| 53 | return nullptr; |
| 54 | } |
| 55 | |
Chih-Yu Huang | 63b0a1d | 2019-11-19 09:31:45 | [diff] [blame] | 56 | return base::WrapUnique<DecoderInterface>(new V4L2SliceVideoDecoder( |
| 57 | std::move(decoder_task_runner), std::move(client), std::move(device))); |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 58 | } |
| 59 | |
Chih-Yu Huang | a4bf834 | 2019-06-27 04:03:44 | [diff] [blame] | 60 | // static |
| 61 | SupportedVideoDecoderConfigs V4L2SliceVideoDecoder::GetSupportedConfigs() { |
| 62 | scoped_refptr<V4L2Device> device = V4L2Device::Create(); |
| 63 | if (!device) |
| 64 | return SupportedVideoDecoderConfigs(); |
| 65 | |
| 66 | return ConvertFromSupportedProfiles( |
| 67 | device->GetSupportedDecodeProfiles(base::size(kSupportedInputFourccs), |
| 68 | kSupportedInputFourccs), |
| 69 | false); |
| 70 | } |
| 71 | |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 72 | V4L2SliceVideoDecoder::V4L2SliceVideoDecoder( |
Chih-Yu Huang | fc16380 | 2019-09-02 06:17:48 | [diff] [blame] | 73 | scoped_refptr<base::SequencedTaskRunner> decoder_task_runner, |
Chih-Yu Huang | 63b0a1d | 2019-11-19 09:31:45 | [diff] [blame] | 74 | base::WeakPtr<DecoderInterface::Client> client, |
| 75 | scoped_refptr<V4L2Device> device) |
| 76 | : DecoderInterface(std::move(decoder_task_runner), std::move(client)), |
| 77 | device_(std::move(device)), |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 78 | weak_this_factory_(this) { |
Chih-Yu Huang | a35c28a | 2019-10-21 02:43:09 | [diff] [blame] | 79 | DCHECK_CALLED_ON_VALID_SEQUENCE(decoder_sequence_checker_); |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 80 | VLOGF(2); |
Chih-Yu Huang | a35c28a | 2019-10-21 02:43:09 | [diff] [blame] | 81 | |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 82 | weak_this_ = weak_this_factory_.GetWeakPtr(); |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | V4L2SliceVideoDecoder::~V4L2SliceVideoDecoder() { |
Alexandre Courbot | dcb6ebb | 2019-06-25 04:25:36 | [diff] [blame] | 86 | DCHECK_CALLED_ON_VALID_SEQUENCE(decoder_sequence_checker_); |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 87 | DVLOGF(2); |
| 88 | |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 89 | // Call all pending decode callback. |
Alexandre Courbot | 78b1e64 | 2019-10-16 03:45:39 | [diff] [blame] | 90 | if (backend_) { |
| 91 | backend_->ClearPendingRequests(DecodeStatus::ABORTED); |
| 92 | backend_ = nullptr; |
| 93 | } |
Chih-Yu Huang | 30b5fca | 2019-09-24 09:03:34 | [diff] [blame] | 94 | |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 95 | // Stop and Destroy device. |
| 96 | StopStreamV4L2Queue(); |
Chih-Yu Huang | 1c5d421 | 2019-08-01 07:04:47 | [diff] [blame] | 97 | if (input_queue_) { |
| 98 | input_queue_->DeallocateBuffers(); |
| 99 | input_queue_ = nullptr; |
| 100 | } |
| 101 | if (output_queue_) { |
| 102 | output_queue_->DeallocateBuffers(); |
| 103 | output_queue_ = nullptr; |
| 104 | } |
Francois Buergisser | 7fee4d1 | 2019-10-03 09:22:32 | [diff] [blame] | 105 | |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 106 | weak_this_factory_.InvalidateWeakPtrs(); |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | void V4L2SliceVideoDecoder::Initialize(const VideoDecoderConfig& config, |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 110 | InitCB init_cb, |
Chih-Yu Huang | 27b7250 | 2019-10-18 08:22:49 | [diff] [blame] | 111 | const OutputCB& output_cb) { |
Alexandre Courbot | dcb6ebb | 2019-06-25 04:25:36 | [diff] [blame] | 112 | DCHECK_CALLED_ON_VALID_SEQUENCE(decoder_sequence_checker_); |
Chih-Yu Huang | a35c28a | 2019-10-21 02:43:09 | [diff] [blame] | 113 | DCHECK(config.IsValidConfig()); |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 114 | DCHECK(state_ == State::kUninitialized || state_ == State::kDecoding); |
| 115 | DVLOGF(3); |
| 116 | |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 117 | // Reset V4L2 device and queue if reinitializing decoder. |
| 118 | if (state_ != State::kUninitialized) { |
| 119 | if (!StopStreamV4L2Queue()) { |
Ted Meyer | 2a41e24 | 2020-03-20 08:01:27 | [diff] [blame] | 120 | std::move(init_cb).Run(StatusCode::kV4l2FailedToStopStreamQueue); |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 121 | return; |
| 122 | } |
| 123 | |
| 124 | input_queue_->DeallocateBuffers(); |
| 125 | output_queue_->DeallocateBuffers(); |
| 126 | input_queue_ = nullptr; |
| 127 | output_queue_ = nullptr; |
| 128 | |
| 129 | device_ = V4L2Device::Create(); |
| 130 | if (!device_) { |
| 131 | VLOGF(1) << "Failed to create V4L2 device."; |
Ted Meyer | 2a41e24 | 2020-03-20 08:01:27 | [diff] [blame] | 132 | std::move(init_cb).Run(StatusCode::kV4l2NoDevice); |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 133 | return; |
| 134 | } |
| 135 | |
Alexandre Courbot | 78b1e64 | 2019-10-16 03:45:39 | [diff] [blame] | 136 | if (backend_) |
| 137 | backend_ = nullptr; |
| 138 | |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 139 | SetState(State::kUninitialized); |
| 140 | } |
| 141 | |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 142 | // Open V4L2 device. |
| 143 | VideoCodecProfile profile = config.profile(); |
| 144 | uint32_t input_format_fourcc = |
| 145 | V4L2Device::VideoCodecProfileToV4L2PixFmt(profile, true); |
Chih-Yu Huang | 6052d1c | 2019-08-26 09:35:46 | [diff] [blame] | 146 | if (!input_format_fourcc || |
| 147 | !device_->Open(V4L2Device::Type::kDecoder, input_format_fourcc)) { |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 148 | VLOGF(1) << "Failed to open device for profile: " << profile |
| 149 | << " fourcc: " << FourccToString(input_format_fourcc); |
Ted Meyer | 2a41e24 | 2020-03-20 08:01:27 | [diff] [blame] | 150 | std::move(init_cb).Run(StatusCode::kV4l2NoDecoder); |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 151 | return; |
| 152 | } |
| 153 | |
| 154 | struct v4l2_capability caps; |
| 155 | const __u32 kCapsRequired = V4L2_CAP_VIDEO_M2M_MPLANE | V4L2_CAP_STREAMING; |
| 156 | if (device_->Ioctl(VIDIOC_QUERYCAP, &caps) || |
| 157 | (caps.capabilities & kCapsRequired) != kCapsRequired) { |
| 158 | VLOGF(1) << "ioctl() failed: VIDIOC_QUERYCAP, " |
| 159 | << "caps check failed: 0x" << std::hex << caps.capabilities; |
Ted Meyer | 2a41e24 | 2020-03-20 08:01:27 | [diff] [blame] | 160 | std::move(init_cb).Run(StatusCode::kV4l2FailedFileCapabilitiesCheck); |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 161 | return; |
| 162 | } |
| 163 | |
Hirokazu Honda | 23c6a6a | 2019-07-25 11:14:45 | [diff] [blame] | 164 | pixel_aspect_ratio_ = config.GetPixelAspectRatio(); |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 165 | |
Alexandre Courbot | 229c7fe3 | 2019-10-07 06:48:01 | [diff] [blame] | 166 | // Create Input/Output V4L2Queue |
| 167 | input_queue_ = device_->GetQueue(V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE); |
| 168 | output_queue_ = device_->GetQueue(V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE); |
| 169 | if (!input_queue_ || !output_queue_) { |
| 170 | VLOGF(1) << "Failed to create V4L2 queue."; |
Ted Meyer | 2a41e24 | 2020-03-20 08:01:27 | [diff] [blame] | 171 | std::move(init_cb).Run(StatusCode::kV4l2FailedResourceAllocation); |
Alexandre Courbot | 229c7fe3 | 2019-10-07 06:48:01 | [diff] [blame] | 172 | return; |
| 173 | } |
| 174 | |
Alexandre Courbot | 78b1e64 | 2019-10-16 03:45:39 | [diff] [blame] | 175 | // Create the backend (only stateless API supported as of now). |
| 176 | backend_ = std::make_unique<V4L2StatelessVideoDecoderBackend>( |
Shuo-Peng Liao | e6bf70c | 2019-12-05 01:44:26 | [diff] [blame] | 177 | this, device_, profile, decoder_task_runner_); |
Alexandre Courbot | 78b1e64 | 2019-10-16 03:45:39 | [diff] [blame] | 178 | if (!backend_->Initialize()) { |
Ted Meyer | 2a41e24 | 2020-03-20 08:01:27 | [diff] [blame] | 179 | std::move(init_cb).Run(StatusCode::kV4l2FailedResourceAllocation); |
Alexandre Courbot | 78b1e64 | 2019-10-16 03:45:39 | [diff] [blame] | 180 | return; |
| 181 | } |
| 182 | |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 183 | // Setup input format. |
| 184 | if (!SetupInputFormat(input_format_fourcc)) { |
| 185 | VLOGF(1) << "Failed to setup input format."; |
Ted Meyer | 2a41e24 | 2020-03-20 08:01:27 | [diff] [blame] | 186 | std::move(init_cb).Run(StatusCode::kV4l2BadFormat); |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 187 | return; |
| 188 | } |
| 189 | |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 190 | if (input_queue_->AllocateBuffers(kNumInputBuffers, V4L2_MEMORY_MMAP) == 0) { |
| 191 | VLOGF(1) << "Failed to allocate input buffer."; |
Ted Meyer | 2a41e24 | 2020-03-20 08:01:27 | [diff] [blame] | 192 | std::move(init_cb).Run(StatusCode::kV4l2FailedResourceAllocation); |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 193 | return; |
| 194 | } |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 195 | |
| 196 | // Call init_cb |
| 197 | output_cb_ = output_cb; |
| 198 | SetState(State::kDecoding); |
Ted Meyer | 2a41e24 | 2020-03-20 08:01:27 | [diff] [blame] | 199 | std::move(init_cb).Run(::media::OkStatus()); |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | bool V4L2SliceVideoDecoder::SetupInputFormat(uint32_t input_format_fourcc) { |
Alexandre Courbot | dcb6ebb | 2019-06-25 04:25:36 | [diff] [blame] | 203 | DCHECK_CALLED_ON_VALID_SEQUENCE(decoder_sequence_checker_); |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 204 | DCHECK_EQ(state_, State::kUninitialized); |
| 205 | |
| 206 | // Check if the format is supported. |
| 207 | std::vector<uint32_t> formats = device_->EnumerateSupportedPixelformats( |
| 208 | V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE); |
| 209 | if (std::find(formats.begin(), formats.end(), input_format_fourcc) == |
| 210 | formats.end()) { |
| 211 | DVLOGF(3) << "Input fourcc " << input_format_fourcc |
| 212 | << " not supported by device."; |
| 213 | return false; |
| 214 | } |
| 215 | |
| 216 | // Determine the input buffer size. |
| 217 | gfx::Size max_size, min_size; |
| 218 | device_->GetSupportedResolution(input_format_fourcc, &min_size, &max_size); |
| 219 | size_t input_size = max_size.GetArea() > k1080pArea |
| 220 | ? kInputBufferMaxSizeFor4k |
| 221 | : kInputBufferMaxSizeFor1080p; |
| 222 | |
| 223 | // Setup the input format. |
Yuki Shiino | 86b64ff | 2019-10-07 05:37:35 | [diff] [blame] | 224 | auto format = |
| 225 | input_queue_->SetFormat(input_format_fourcc, gfx::Size(), input_size); |
| 226 | if (!format) { |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 227 | VPLOGF(1) << "Failed to call IOCTL to set input format."; |
| 228 | return false; |
| 229 | } |
Yuki Shiino | 86b64ff | 2019-10-07 05:37:35 | [diff] [blame] | 230 | DCHECK_EQ(format->fmt.pix_mp.pixelformat, input_format_fourcc); |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 231 | |
| 232 | return true; |
| 233 | } |
| 234 | |
Shuo-Peng Liao | e6bf70c | 2019-12-05 01:44:26 | [diff] [blame] | 235 | bool V4L2SliceVideoDecoder::SetupOutputFormat(const gfx::Size& size, |
| 236 | const gfx::Rect& visible_rect) { |
Alexandre Courbot | dcb6ebb | 2019-06-25 04:25:36 | [diff] [blame] | 237 | DCHECK_CALLED_ON_VALID_SEQUENCE(decoder_sequence_checker_); |
Shuo-Peng Liao | e6bf70c | 2019-12-05 01:44:26 | [diff] [blame] | 238 | DVLOGF(3) << "size: " << size.ToString() |
| 239 | << ", visible_rect: " << visible_rect.ToString(); |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 240 | |
Shuo-Peng Liao | e6bf70c | 2019-12-05 01:44:26 | [diff] [blame] | 241 | // Get the supported output formats and their corresponding negotiated sizes. |
| 242 | std::vector<std::pair<Fourcc, gfx::Size>> candidates; |
| 243 | for (const uint32_t& pixfmt : device_->EnumerateSupportedPixelformats( |
| 244 | V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)) { |
Alexandre Courbot | 139ebe2 | 2019-12-12 01:46:39 | [diff] [blame] | 245 | const auto candidate = Fourcc::FromV4L2PixFmt(pixfmt); |
| 246 | if (!candidate) { |
| 247 | DVLOGF(1) << "Pixel format " << FourccToString(pixfmt) |
| 248 | << " is not supported, skipping..."; |
| 249 | continue; |
| 250 | } |
| 251 | |
Hirokazu Honda | 685bf01 | 2019-08-05 01:30:00 | [diff] [blame] | 252 | base::Optional<struct v4l2_format> format = |
Shuo-Peng Liao | e6bf70c | 2019-12-05 01:44:26 | [diff] [blame] | 253 | output_queue_->SetFormat(pixfmt, size, 0); |
Alexandre Courbot | 139ebe2 | 2019-12-12 01:46:39 | [diff] [blame] | 254 | if (!format) |
| 255 | continue; |
| 256 | |
| 257 | gfx::Size adjusted_size(format->fmt.pix_mp.width, |
| 258 | format->fmt.pix_mp.height); |
| 259 | candidates.push_back(std::make_pair(*candidate, adjusted_size)); |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 260 | } |
| 261 | |
Shuo-Peng Liao | e6bf70c | 2019-12-05 01:44:26 | [diff] [blame] | 262 | // Ask the pipeline to pick the output format. |
Alexandre Courbot | 139ebe2 | 2019-12-12 01:46:39 | [diff] [blame] | 263 | const base::Optional<Fourcc> fourcc = |
Shuo-Peng Liao | e6bf70c | 2019-12-05 01:44:26 | [diff] [blame] | 264 | client_->PickDecoderOutputFormat(candidates, visible_rect); |
| 265 | if (!fourcc) { |
| 266 | VLOGF(1) << "Failed to pick a output format."; |
| 267 | return false; |
| 268 | } |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 269 | |
Shuo-Peng Liao | e6bf70c | 2019-12-05 01:44:26 | [diff] [blame] | 270 | // We successfully picked the output format. Now setup output format again. |
| 271 | base::Optional<struct v4l2_format> format = |
Alexandre Courbot | 139ebe2 | 2019-12-12 01:46:39 | [diff] [blame] | 272 | output_queue_->SetFormat(fourcc->ToV4L2PixFmt(), size, 0); |
Shuo-Peng Liao | e6bf70c | 2019-12-05 01:44:26 | [diff] [blame] | 273 | DCHECK(format); |
| 274 | gfx::Size adjusted_size(format->fmt.pix_mp.width, format->fmt.pix_mp.height); |
| 275 | DCHECK_EQ(adjusted_size.width() % 16, 0); |
| 276 | DCHECK_EQ(adjusted_size.height() % 16, 0); |
| 277 | if (!gfx::Rect(adjusted_size).Contains(gfx::Rect(size))) { |
| 278 | VLOGF(1) << "The adjusted coded size (" << adjusted_size.ToString() |
| 279 | << ") should contains the original coded size(" << size.ToString() |
| 280 | << ")."; |
| 281 | return false; |
| 282 | } |
| 283 | |
| 284 | // Got the adjusted size from the V4L2 driver. Now setup the frame pool. |
| 285 | // TODO(akahuang): It is possible there is an allocatable formats among |
| 286 | // candidates, but PickDecoderOutputFormat() selects other non-allocatable |
| 287 | // format. The correct flow is to attach an info to candidates if it is |
| 288 | // created by VideoFramePool. |
| 289 | DmabufVideoFramePool* pool = client_->GetVideoFramePool(); |
| 290 | if (pool) { |
Miguel Casas | 6ee396b | 2020-01-17 21:21:16 | [diff] [blame] | 291 | base::Optional<GpuBufferLayout> layout = pool->Initialize( |
Alexandre Courbot | 139ebe2 | 2019-12-12 01:46:39 | [diff] [blame] | 292 | *fourcc, adjusted_size, visible_rect, |
Shuo-Peng Liao | e6bf70c | 2019-12-05 01:44:26 | [diff] [blame] | 293 | GetNaturalSize(visible_rect, pixel_aspect_ratio_), num_output_frames_); |
| 294 | if (!layout) { |
| 295 | VLOGF(1) << "Failed to setup format to VFPool"; |
| 296 | return false; |
| 297 | } |
| 298 | if (layout->size() != adjusted_size) { |
| 299 | VLOGF(1) << "The size adjusted by VFPool is different from one " |
Alexandre Courbot | 139ebe2 | 2019-12-12 01:46:39 | [diff] [blame] | 300 | << "adjusted by a video driver. fourcc: " << fourcc->ToString() |
| 301 | << ", (video driver v.s. VFPool) " << adjusted_size.ToString() |
Shuo-Peng Liao | e6bf70c | 2019-12-05 01:44:26 | [diff] [blame] | 302 | << " != " << layout->size().ToString(); |
| 303 | return false; |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | return true; |
Chih-Yu Huang | f463589 | 2019-07-11 14:20:29 | [diff] [blame] | 308 | } |
| 309 | |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 310 | void V4L2SliceVideoDecoder::Reset(base::OnceClosure closure) { |
Alexandre Courbot | dcb6ebb | 2019-06-25 04:25:36 | [diff] [blame] | 311 | DCHECK_CALLED_ON_VALID_SEQUENCE(decoder_sequence_checker_); |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 312 | DVLOGF(3); |
| 313 | |
Chih-Yu Huang | 1141fdeb | 2019-12-10 06:16:13 | [diff] [blame] | 314 | // Reset callback for resolution change, because the pipeline won't notify |
| 315 | // flushed after reset. |
| 316 | continue_change_resolution_cb_.Reset(); |
| 317 | |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 318 | // Call all pending decode callback. |
Alexandre Courbot | 78b1e64 | 2019-10-16 03:45:39 | [diff] [blame] | 319 | backend_->ClearPendingRequests(DecodeStatus::ABORTED); |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 320 | |
| 321 | // Streamoff V4L2 queues to drop input and output buffers. |
| 322 | // If the queues are streaming before reset, then we need to start streaming |
| 323 | // them after stopping. |
Alexandre Courbot | ba021e3 | 2019-09-26 07:24:35 | [diff] [blame] | 324 | bool is_streaming = input_queue_->IsStreaming(); |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 325 | if (!StopStreamV4L2Queue()) |
| 326 | return; |
| 327 | |
Alexandre Courbot | ba021e3 | 2019-09-26 07:24:35 | [diff] [blame] | 328 | if (is_streaming) { |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 329 | if (!StartStreamV4L2Queue()) |
| 330 | return; |
| 331 | } |
| 332 | |
Chih-Yu Huang | 1141fdeb | 2019-12-10 06:16:13 | [diff] [blame] | 333 | // If during flushing, Reset() will abort the following flush tasks. |
| 334 | // Now we are ready to decode new buffer. Go back to decoding state. |
| 335 | SetState(State::kDecoding); |
| 336 | |
Chih-Yu Huang | a35c28a | 2019-10-21 02:43:09 | [diff] [blame] | 337 | std::move(closure).Run(); |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 338 | } |
| 339 | |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 340 | void V4L2SliceVideoDecoder::Decode(scoped_refptr<DecoderBuffer> buffer, |
| 341 | DecodeCB decode_cb) { |
Alexandre Courbot | dcb6ebb | 2019-06-25 04:25:36 | [diff] [blame] | 342 | DCHECK_CALLED_ON_VALID_SEQUENCE(decoder_sequence_checker_); |
Chih-Yu Huang | 30b5fca | 2019-09-24 09:03:34 | [diff] [blame] | 343 | DCHECK_NE(state_, State::kUninitialized); |
| 344 | |
| 345 | if (state_ == State::kError) { |
Alexandre Courbot | 51ad52a8 | 2019-10-07 04:36:36 | [diff] [blame] | 346 | std::move(decode_cb).Run(DecodeStatus::DECODE_ERROR); |
Chih-Yu Huang | 30b5fca | 2019-09-24 09:03:34 | [diff] [blame] | 347 | return; |
| 348 | } |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 349 | |
Alexandre Courbot | 51ad52a8 | 2019-10-07 04:36:36 | [diff] [blame] | 350 | const int32_t bitstream_id = bitstream_id_generator_.GetNextBitstreamId(); |
Alexandre Courbot | 78b1e64 | 2019-10-16 03:45:39 | [diff] [blame] | 351 | backend_->EnqueueDecodeTask(std::move(buffer), std::move(decode_cb), |
| 352 | bitstream_id); |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | bool V4L2SliceVideoDecoder::StartStreamV4L2Queue() { |
Alexandre Courbot | dcb6ebb | 2019-06-25 04:25:36 | [diff] [blame] | 356 | DCHECK_CALLED_ON_VALID_SEQUENCE(decoder_sequence_checker_); |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 357 | DVLOGF(3); |
| 358 | |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 359 | if (!input_queue_->Streamon() || !output_queue_->Streamon()) { |
| 360 | VLOGF(1) << "Failed to streamon V4L2 queue."; |
| 361 | SetState(State::kError); |
| 362 | return false; |
| 363 | } |
| 364 | |
Alexandre Courbot | ba021e3 | 2019-09-26 07:24:35 | [diff] [blame] | 365 | if (!device_->StartPolling( |
| 366 | base::BindRepeating(&V4L2SliceVideoDecoder::ServiceDeviceTask, |
| 367 | weak_this_), |
| 368 | base::BindRepeating(&V4L2SliceVideoDecoder::SetState, weak_this_, |
| 369 | State::kError))) { |
| 370 | SetState(State::kError); |
| 371 | return false; |
| 372 | } |
| 373 | |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 374 | return true; |
| 375 | } |
| 376 | |
| 377 | bool V4L2SliceVideoDecoder::StopStreamV4L2Queue() { |
Alexandre Courbot | dcb6ebb | 2019-06-25 04:25:36 | [diff] [blame] | 378 | DCHECK_CALLED_ON_VALID_SEQUENCE(decoder_sequence_checker_); |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 379 | DVLOGF(3); |
| 380 | |
Alexandre Courbot | ba021e3 | 2019-09-26 07:24:35 | [diff] [blame] | 381 | if (!device_->StopPolling()) { |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 382 | SetState(State::kError); |
| 383 | return false; |
| 384 | } |
| 385 | |
Chih-Yu Huang | 1c5d421 | 2019-08-01 07:04:47 | [diff] [blame] | 386 | // Streamoff input and output queue. |
| 387 | if (input_queue_) |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 388 | input_queue_->Streamoff(); |
Chih-Yu Huang | 1c5d421 | 2019-08-01 07:04:47 | [diff] [blame] | 389 | if (output_queue_) |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 390 | output_queue_->Streamoff(); |
Chih-Yu Huang | 1c5d421 | 2019-08-01 07:04:47 | [diff] [blame] | 391 | |
Alexandre Courbot | 78b1e64 | 2019-10-16 03:45:39 | [diff] [blame] | 392 | if (backend_) |
| 393 | backend_->OnStreamStopped(); |
| 394 | |
| 395 | return true; |
| 396 | } |
| 397 | |
| 398 | void V4L2SliceVideoDecoder::InitiateFlush() { |
| 399 | DCHECK_CALLED_ON_VALID_SEQUENCE(decoder_sequence_checker_); |
| 400 | DVLOGF(3); |
| 401 | |
| 402 | SetState(State::kFlushing); |
| 403 | } |
| 404 | |
| 405 | void V4L2SliceVideoDecoder::CompleteFlush() { |
| 406 | DCHECK_CALLED_ON_VALID_SEQUENCE(decoder_sequence_checker_); |
| 407 | DVLOGF(3); |
| 408 | |
| 409 | SetState(State::kDecoding); |
| 410 | } |
| 411 | |
Chih-Yu Huang | 63b0a1d | 2019-11-19 09:31:45 | [diff] [blame] | 412 | void V4L2SliceVideoDecoder::ChangeResolution(gfx::Size pic_size, |
Alexandre Courbot | 78b1e64 | 2019-10-16 03:45:39 | [diff] [blame] | 413 | gfx::Rect visible_rect, |
| 414 | size_t num_output_frames) { |
| 415 | DCHECK_CALLED_ON_VALID_SEQUENCE(decoder_sequence_checker_); |
| 416 | DVLOGF(3); |
Chih-Yu Huang | 63b0a1d | 2019-11-19 09:31:45 | [diff] [blame] | 417 | DCHECK(!continue_change_resolution_cb_); |
| 418 | |
| 419 | // After the pipeline flushes all frames, we can start changing resolution. |
| 420 | continue_change_resolution_cb_ = |
| 421 | base::BindOnce(&V4L2SliceVideoDecoder::ContinueChangeResolution, |
| 422 | weak_this_, pic_size, visible_rect, num_output_frames); |
| 423 | |
| 424 | DCHECK(client_); |
| 425 | client_->PrepareChangeResolution(); |
| 426 | } |
| 427 | |
Alexandre Courbot | 54dca90 | 2020-04-14 16:45:37 | [diff] [blame] | 428 | void V4L2SliceVideoDecoder::ApplyResolutionChange() { |
Chih-Yu Huang | 63b0a1d | 2019-11-19 09:31:45 | [diff] [blame] | 429 | DCHECK_CALLED_ON_VALID_SEQUENCE(decoder_sequence_checker_); |
| 430 | DVLOGF(3); |
| 431 | DCHECK(continue_change_resolution_cb_); |
| 432 | |
Alexandre Courbot | cddb357f | 2020-04-15 03:51:02 | [diff] [blame^] | 433 | std::move(continue_change_resolution_cb_).Run(); |
Chih-Yu Huang | 63b0a1d | 2019-11-19 09:31:45 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | void V4L2SliceVideoDecoder::ContinueChangeResolution( |
| 437 | const gfx::Size& pic_size, |
| 438 | const gfx::Rect& visible_rect, |
| 439 | const size_t num_output_frames) { |
| 440 | DCHECK_CALLED_ON_VALID_SEQUENCE(decoder_sequence_checker_); |
| 441 | DVLOGF(3); |
Alexandre Courbot | 78b1e64 | 2019-10-16 03:45:39 | [diff] [blame] | 442 | DCHECK_EQ(input_queue_->QueuedBuffersCount(), 0u); |
| 443 | DCHECK_EQ(output_queue_->QueuedBuffersCount(), 0u); |
| 444 | |
Chih-Yu Huang | 1141fdeb | 2019-12-10 06:16:13 | [diff] [blame] | 445 | // If we already reset, then skip it. |
| 446 | if (state_ == State::kDecoding) |
| 447 | return; |
| 448 | DCHECK_EQ(state_, State::kFlushing); |
| 449 | |
Chih-Yu Huang | 63b0a1d | 2019-11-19 09:31:45 | [diff] [blame] | 450 | // Notify |backend_| that changing resolution fails. |
| 451 | // Note: |backend_| is owned by this, using base::Unretained() is safe. |
| 452 | base::ScopedClosureRunner done_caller( |
| 453 | base::BindOnce(&V4L2VideoDecoderBackend::OnChangeResolutionDone, |
| 454 | base::Unretained(backend_.get()), false)); |
| 455 | |
Chih-Yu Huang | bab32ff | 2019-11-01 08:49:21 | [diff] [blame] | 456 | num_output_frames_ = num_output_frames; |
| 457 | |
Alexandre Courbot | 78b1e64 | 2019-10-16 03:45:39 | [diff] [blame] | 458 | if (!StopStreamV4L2Queue()) |
Chih-Yu Huang | 63b0a1d | 2019-11-19 09:31:45 | [diff] [blame] | 459 | return; |
Alexandre Courbot | 78b1e64 | 2019-10-16 03:45:39 | [diff] [blame] | 460 | |
Francois Buergisser | 30e651a | 2019-10-18 16:22:45 | [diff] [blame] | 461 | if (!output_queue_->DeallocateBuffers()) { |
| 462 | SetState(State::kError); |
Chih-Yu Huang | 63b0a1d | 2019-11-19 09:31:45 | [diff] [blame] | 463 | return; |
Francois Buergisser | 30e651a | 2019-10-18 16:22:45 | [diff] [blame] | 464 | } |
| 465 | DCHECK_GT(num_output_frames, 0u); |
| 466 | |
Alexandre Courbot | f5936fe | 2020-04-02 07:24:12 | [diff] [blame] | 467 | if (!backend_->ApplyResolution(pic_size, visible_rect, num_output_frames)) { |
| 468 | SetState(State::kError); |
Chih-Yu Huang | 63b0a1d | 2019-11-19 09:31:45 | [diff] [blame] | 469 | return; |
Alexandre Courbot | 78b1e64 | 2019-10-16 03:45:39 | [diff] [blame] | 470 | } |
| 471 | |
Shuo-Peng Liao | e6bf70c | 2019-12-05 01:44:26 | [diff] [blame] | 472 | if (!SetupOutputFormat(pic_size, visible_rect)) { |
| 473 | VLOGF(1) << "Failed to setup output format."; |
Alexandre Courbot | 78b1e64 | 2019-10-16 03:45:39 | [diff] [blame] | 474 | SetState(State::kError); |
Chih-Yu Huang | 63b0a1d | 2019-11-19 09:31:45 | [diff] [blame] | 475 | return; |
Alexandre Courbot | 78b1e64 | 2019-10-16 03:45:39 | [diff] [blame] | 476 | } |
| 477 | |
Shuo-Peng Liao | e6bf70c | 2019-12-05 01:44:26 | [diff] [blame] | 478 | v4l2_memory type = |
| 479 | client_->GetVideoFramePool() ? V4L2_MEMORY_DMABUF : V4L2_MEMORY_MMAP; |
| 480 | if (output_queue_->AllocateBuffers(num_output_frames_, type) == 0) { |
Alexandre Courbot | 78b1e64 | 2019-10-16 03:45:39 | [diff] [blame] | 481 | VLOGF(1) << "Failed to request output buffers."; |
| 482 | SetState(State::kError); |
Chih-Yu Huang | 63b0a1d | 2019-11-19 09:31:45 | [diff] [blame] | 483 | return; |
Alexandre Courbot | 78b1e64 | 2019-10-16 03:45:39 | [diff] [blame] | 484 | } |
Chih-Yu Huang | bab32ff | 2019-11-01 08:49:21 | [diff] [blame] | 485 | if (output_queue_->AllocatedBuffersCount() != num_output_frames_) { |
Alexandre Courbot | 78b1e64 | 2019-10-16 03:45:39 | [diff] [blame] | 486 | VLOGF(1) << "Could not allocate requested number of output buffers."; |
| 487 | SetState(State::kError); |
Chih-Yu Huang | 63b0a1d | 2019-11-19 09:31:45 | [diff] [blame] | 488 | return; |
Alexandre Courbot | 78b1e64 | 2019-10-16 03:45:39 | [diff] [blame] | 489 | } |
Alexandre Courbot | 78b1e64 | 2019-10-16 03:45:39 | [diff] [blame] | 490 | |
| 491 | if (!StartStreamV4L2Queue()) { |
| 492 | SetState(State::kError); |
Chih-Yu Huang | 63b0a1d | 2019-11-19 09:31:45 | [diff] [blame] | 493 | return; |
Alexandre Courbot | 78b1e64 | 2019-10-16 03:45:39 | [diff] [blame] | 494 | } |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 495 | |
Chih-Yu Huang | 63b0a1d | 2019-11-19 09:31:45 | [diff] [blame] | 496 | // Now notify |backend_| that changing resolution is done successfully. |
| 497 | // Note: |backend_| is owned by this, using base::Unretained() is safe. |
| 498 | done_caller.ReplaceClosure( |
| 499 | base::BindOnce(&V4L2VideoDecoderBackend::OnChangeResolutionDone, |
| 500 | base::Unretained(backend_.get()), true)); |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 501 | } |
| 502 | |
Alexandre Courbot | ba021e3 | 2019-09-26 07:24:35 | [diff] [blame] | 503 | void V4L2SliceVideoDecoder::ServiceDeviceTask(bool /* event */) { |
Alexandre Courbot | dcb6ebb | 2019-06-25 04:25:36 | [diff] [blame] | 504 | DCHECK_CALLED_ON_VALID_SEQUENCE(decoder_sequence_checker_); |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 505 | DVLOGF(3) << "Number of queued input buffers: " |
| 506 | << input_queue_->QueuedBuffersCount() |
| 507 | << ", Number of queued output buffers: " |
| 508 | << output_queue_->QueuedBuffersCount(); |
| 509 | |
| 510 | // Dequeue V4L2 output buffer first to reduce output latency. |
| 511 | bool success; |
| 512 | V4L2ReadableBufferRef dequeued_buffer; |
| 513 | while (output_queue_->QueuedBuffersCount() > 0) { |
| 514 | std::tie(success, dequeued_buffer) = output_queue_->DequeueBuffer(); |
| 515 | if (!success) { |
| 516 | SetState(State::kError); |
| 517 | return; |
| 518 | } |
| 519 | if (!dequeued_buffer) |
| 520 | break; |
| 521 | |
Alexandre Courbot | 78b1e64 | 2019-10-16 03:45:39 | [diff] [blame] | 522 | backend_->OnOutputBufferDequeued(std::move(dequeued_buffer)); |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 523 | } |
| 524 | |
| 525 | // Dequeue V4L2 input buffer. |
| 526 | while (input_queue_->QueuedBuffersCount() > 0) { |
| 527 | std::tie(success, dequeued_buffer) = input_queue_->DequeueBuffer(); |
| 528 | if (!success) { |
| 529 | SetState(State::kError); |
| 530 | return; |
| 531 | } |
| 532 | if (!dequeued_buffer) |
| 533 | break; |
| 534 | } |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 535 | } |
| 536 | |
Alexandre Courbot | 78b1e64 | 2019-10-16 03:45:39 | [diff] [blame] | 537 | void V4L2SliceVideoDecoder::OutputFrame(scoped_refptr<VideoFrame> frame, |
Chih-Yu Huang | f463589 | 2019-07-11 14:20:29 | [diff] [blame] | 538 | const gfx::Rect& visible_rect, |
Chih-Yu Huang | ccf4603 | 2019-07-11 11:48:06 | [diff] [blame] | 539 | base::TimeDelta timestamp) { |
Alexandre Courbot | dcb6ebb | 2019-06-25 04:25:36 | [diff] [blame] | 540 | DCHECK_CALLED_ON_VALID_SEQUENCE(decoder_sequence_checker_); |
Chih-Yu Huang | ccf4603 | 2019-07-11 11:48:06 | [diff] [blame] | 541 | DVLOGF(4) << "timestamp: " << timestamp; |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 542 | |
Chih-Yu Huang | e497fcc | 2019-09-25 02:33:11 | [diff] [blame] | 543 | // Set the timestamp at which the decode operation started on the |
| 544 | // |frame|. If the frame has been outputted before (e.g. because of VP9 |
| 545 | // show-existing-frame feature) we can't overwrite the timestamp directly, as |
| 546 | // the original frame might still be in use. Instead we wrap the frame in |
| 547 | // another frame with a different timestamp. |
Chih-Yu Huang | eacec32 | 2019-09-24 05:51:34 | [diff] [blame] | 548 | if (frame->timestamp().is_zero()) |
| 549 | frame->set_timestamp(timestamp); |
| 550 | |
Chih-Yu Huang | f463589 | 2019-07-11 14:20:29 | [diff] [blame] | 551 | if (frame->visible_rect() != visible_rect || |
| 552 | frame->timestamp() != timestamp) { |
| 553 | gfx::Size natural_size = GetNaturalSize(visible_rect, pixel_aspect_ratio_); |
Chih-Yu Huang | ccf4603 | 2019-07-11 11:48:06 | [diff] [blame] | 554 | scoped_refptr<VideoFrame> wrapped_frame = VideoFrame::WrapVideoFrame( |
Ricky Liang | b2075eb | 2019-10-01 07:44:50 | [diff] [blame] | 555 | frame, frame->format(), visible_rect, natural_size); |
Chih-Yu Huang | ccf4603 | 2019-07-11 11:48:06 | [diff] [blame] | 556 | wrapped_frame->set_timestamp(timestamp); |
Chih-Yu Huang | ccf4603 | 2019-07-11 11:48:06 | [diff] [blame] | 557 | |
| 558 | frame = std::move(wrapped_frame); |
| 559 | } |
Chih-Yu Huang | 63b0a1d | 2019-11-19 09:31:45 | [diff] [blame] | 560 | |
Chih-Yu Huang | a35c28a | 2019-10-21 02:43:09 | [diff] [blame] | 561 | output_cb_.Run(std::move(frame)); |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 562 | } |
| 563 | |
Shuo-Peng Liao | e6bf70c | 2019-12-05 01:44:26 | [diff] [blame] | 564 | DmabufVideoFramePool* V4L2SliceVideoDecoder::GetVideoFramePool() const { |
| 565 | DCHECK_CALLED_ON_VALID_SEQUENCE(decoder_sequence_checker_); |
| 566 | DVLOGF(4); |
| 567 | |
| 568 | return client_->GetVideoFramePool(); |
| 569 | } |
| 570 | |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 571 | void V4L2SliceVideoDecoder::SetState(State new_state) { |
Alexandre Courbot | dcb6ebb | 2019-06-25 04:25:36 | [diff] [blame] | 572 | DCHECK_CALLED_ON_VALID_SEQUENCE(decoder_sequence_checker_); |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 573 | DVLOGF(3) << "Change state from " << static_cast<int>(state_) << " to " |
| 574 | << static_cast<int>(new_state); |
| 575 | |
| 576 | if (state_ == new_state) |
| 577 | return; |
| 578 | if (state_ == State::kError) { |
| 579 | DVLOGF(3) << "Already in kError state."; |
| 580 | return; |
| 581 | } |
| 582 | |
| 583 | // Check if the state transition is valid. |
| 584 | switch (new_state) { |
| 585 | case State::kUninitialized: |
| 586 | if (state_ != State::kDecoding) { |
| 587 | VLOGF(1) << "Should not set to kUninitialized."; |
| 588 | new_state = State::kError; |
| 589 | } |
| 590 | break; |
| 591 | |
| 592 | case State::kDecoding: |
| 593 | break; |
| 594 | |
Chih-Yu Huang | 40adcdf | 2019-07-11 03:37:02 | [diff] [blame] | 595 | case State::kFlushing: |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 596 | if (state_ != State::kDecoding) { |
Chih-Yu Huang | 40adcdf | 2019-07-11 03:37:02 | [diff] [blame] | 597 | VLOGF(1) << "kFlushing should only be set when kDecoding."; |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 598 | new_state = State::kError; |
| 599 | } |
| 600 | break; |
| 601 | |
| 602 | case State::kError: |
| 603 | break; |
| 604 | } |
| 605 | |
| 606 | if (new_state == State::kError) { |
| 607 | VLOGF(1) << "Error occurred."; |
Alexandre Courbot | 78b1e64 | 2019-10-16 03:45:39 | [diff] [blame] | 608 | if (backend_) |
| 609 | backend_->ClearPendingRequests(DecodeStatus::DECODE_ERROR); |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 610 | return; |
| 611 | } |
| 612 | state_ = new_state; |
| 613 | return; |
| 614 | } |
| 615 | |
Alexandre Courbot | 78b1e64 | 2019-10-16 03:45:39 | [diff] [blame] | 616 | void V4L2SliceVideoDecoder::OnBackendError() { |
| 617 | DCHECK_CALLED_ON_VALID_SEQUENCE(decoder_sequence_checker_); |
| 618 | DVLOGF(2); |
| 619 | |
| 620 | SetState(State::kError); |
| 621 | } |
| 622 | |
| 623 | bool V4L2SliceVideoDecoder::IsDecoding() const { |
| 624 | DCHECK_CALLED_ON_VALID_SEQUENCE(decoder_sequence_checker_); |
| 625 | DVLOGF(3); |
| 626 | |
| 627 | return state_ == State::kDecoding; |
| 628 | } |
| 629 | |
Chih-Yu Huang | de529a8 | 2019-06-06 03:26:09 | [diff] [blame] | 630 | } // namespace media |