Roll src/third_party/WebKit d10c917:a1123a1 (svn 198729:198730)
[chromium-blink-merge.git] / content / renderer / media / rtc_video_decoder.cc
blobdf0daffa45a2c52f803ad4c11f14ae29158f05b1
1 // Copyright 2013 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.
5 #include "content/renderer/media/rtc_video_decoder.h"
7 #include "base/bind.h"
8 #include "base/logging.h"
9 #include "base/memory/ref_counted.h"
10 #include "base/metrics/histogram.h"
11 #include "base/numerics/safe_conversions.h"
12 #include "base/stl_util.h"
13 #include "base/synchronization/waitable_event.h"
14 #include "base/task_runner_util.h"
15 #include "content/renderer/media/webrtc/webrtc_video_frame_adapter.h"
16 #include "gpu/command_buffer/common/mailbox_holder.h"
17 #include "media/base/bind_to_current_loop.h"
18 #include "media/renderers/gpu_video_accelerator_factories.h"
19 #include "third_party/skia/include/core/SkBitmap.h"
20 #include "third_party/webrtc/base/bind.h"
21 #include "third_party/webrtc/system_wrappers/interface/ref_count.h"
22 #include "third_party/webrtc/video_frame.h"
24 namespace content {
26 const int32 RTCVideoDecoder::ID_LAST = 0x3FFFFFFF;
27 const int32 RTCVideoDecoder::ID_HALF = 0x20000000;
28 const int32 RTCVideoDecoder::ID_INVALID = -1;
30 // Maximum number of concurrent VDA::Decode() operations RVD will maintain.
31 // Higher values allow better pipelining in the GPU, but also require more
32 // resources.
33 static const size_t kMaxInFlightDecodes = 8;
35 // Size of shared-memory segments we allocate. Since we reuse them we let them
36 // be on the beefy side.
37 static const size_t kSharedMemorySegmentBytes = 100 << 10;
39 // Maximum number of allocated shared-memory segments.
40 static const int kMaxNumSharedMemorySegments = 16;
42 // Maximum number of pending WebRTC buffers that are waiting for the shared
43 // memory. 10 seconds for 30 fps.
44 static const size_t kMaxNumOfPendingBuffers = 300;
46 // A shared memory segment and its allocated size. This class has the ownership
47 // of |shm|.
48 class RTCVideoDecoder::SHMBuffer {
49 public:
50 SHMBuffer(scoped_ptr<base::SharedMemory> shm, size_t size);
51 ~SHMBuffer();
52 scoped_ptr<base::SharedMemory> const shm;
53 const size_t size;
56 RTCVideoDecoder::SHMBuffer::SHMBuffer(scoped_ptr<base::SharedMemory> shm,
57 size_t size)
58 : shm(shm.Pass()), size(size) {
61 RTCVideoDecoder::SHMBuffer::~SHMBuffer() {
64 RTCVideoDecoder::BufferData::BufferData(int32 bitstream_buffer_id,
65 uint32_t timestamp,
66 size_t size)
67 : bitstream_buffer_id(bitstream_buffer_id),
68 timestamp(timestamp),
69 size(size) {}
71 RTCVideoDecoder::BufferData::BufferData() {}
73 RTCVideoDecoder::BufferData::~BufferData() {}
75 RTCVideoDecoder::RTCVideoDecoder(
76 webrtc::VideoCodecType type,
77 const scoped_refptr<media::GpuVideoAcceleratorFactories>& factories)
78 : video_codec_type_(type),
79 factories_(factories),
80 decoder_texture_target_(0),
81 next_picture_buffer_id_(0),
82 state_(UNINITIALIZED),
83 decode_complete_callback_(NULL),
84 num_shm_buffers_(0),
85 next_bitstream_buffer_id_(0),
86 reset_bitstream_buffer_id_(ID_INVALID),
87 weak_factory_(this) {
88 DCHECK(!factories_->GetTaskRunner()->BelongsToCurrentThread());
91 RTCVideoDecoder::~RTCVideoDecoder() {
92 DVLOG(2) << "~RTCVideoDecoder";
93 DCheckGpuVideoAcceleratorFactoriesTaskRunnerIsCurrent();
94 DestroyVDA();
96 // Delete all shared memories.
97 STLDeleteElements(&available_shm_segments_);
98 STLDeleteValues(&bitstream_buffers_in_decoder_);
99 STLDeleteContainerPairFirstPointers(decode_buffers_.begin(),
100 decode_buffers_.end());
101 decode_buffers_.clear();
103 // Delete WebRTC input buffers.
104 for (std::deque<std::pair<webrtc::EncodedImage, BufferData> >::iterator it =
105 pending_buffers_.begin();
106 it != pending_buffers_.end();
107 ++it) {
108 delete[] it->first._buffer;
112 // static
113 scoped_ptr<RTCVideoDecoder> RTCVideoDecoder::Create(
114 webrtc::VideoCodecType type,
115 const scoped_refptr<media::GpuVideoAcceleratorFactories>& factories) {
116 scoped_ptr<RTCVideoDecoder> decoder;
117 // Convert WebRTC codec type to media codec profile.
118 media::VideoCodecProfile profile;
119 switch (type) {
120 case webrtc::kVideoCodecVP8:
121 profile = media::VP8PROFILE_ANY;
122 break;
123 case webrtc::kVideoCodecH264:
124 profile = media::H264PROFILE_MAIN;
125 break;
126 default:
127 DVLOG(2) << "Video codec not supported:" << type;
128 return decoder.Pass();
131 base::WaitableEvent waiter(true, false);
132 decoder.reset(new RTCVideoDecoder(type, factories));
133 decoder->factories_->GetTaskRunner()->PostTask(
134 FROM_HERE,
135 base::Bind(&RTCVideoDecoder::CreateVDA,
136 base::Unretained(decoder.get()),
137 profile,
138 &waiter));
139 waiter.Wait();
140 // vda can be NULL if the codec is not supported.
141 if (decoder->vda_ != NULL) {
142 decoder->state_ = INITIALIZED;
143 } else {
144 factories->GetTaskRunner()->DeleteSoon(FROM_HERE, decoder.release());
146 return decoder.Pass();
149 int32_t RTCVideoDecoder::InitDecode(const webrtc::VideoCodec* codecSettings,
150 int32_t /*numberOfCores*/) {
151 DVLOG(2) << "InitDecode";
152 DCHECK_EQ(video_codec_type_, codecSettings->codecType);
153 if (codecSettings->codecType == webrtc::kVideoCodecVP8 &&
154 codecSettings->codecSpecific.VP8.feedbackModeOn) {
155 LOG(ERROR) << "Feedback mode not supported";
156 return RecordInitDecodeUMA(WEBRTC_VIDEO_CODEC_ERROR);
159 base::AutoLock auto_lock(lock_);
160 if (state_ == UNINITIALIZED || state_ == DECODE_ERROR) {
161 LOG(ERROR) << "VDA is not initialized. state=" << state_;
162 return RecordInitDecodeUMA(WEBRTC_VIDEO_CODEC_UNINITIALIZED);
164 // Create some shared memory if the queue is empty.
165 if (available_shm_segments_.size() == 0) {
166 factories_->GetTaskRunner()->PostTask(
167 FROM_HERE,
168 base::Bind(&RTCVideoDecoder::CreateSHM,
169 weak_factory_.GetWeakPtr(),
170 kMaxInFlightDecodes,
171 kSharedMemorySegmentBytes));
173 return RecordInitDecodeUMA(WEBRTC_VIDEO_CODEC_OK);
176 int32_t RTCVideoDecoder::Decode(
177 const webrtc::EncodedImage& inputImage,
178 bool missingFrames,
179 const webrtc::RTPFragmentationHeader* /*fragmentation*/,
180 const webrtc::CodecSpecificInfo* /*codecSpecificInfo*/,
181 int64_t /*renderTimeMs*/) {
182 DVLOG(3) << "Decode";
184 base::AutoLock auto_lock(lock_);
186 if (state_ == UNINITIALIZED || decode_complete_callback_ == NULL) {
187 LOG(ERROR) << "The decoder has not initialized.";
188 return WEBRTC_VIDEO_CODEC_UNINITIALIZED;
191 if (state_ == DECODE_ERROR) {
192 LOG(ERROR) << "Decoding error occurred.";
193 return WEBRTC_VIDEO_CODEC_ERROR;
196 if (missingFrames || !inputImage._completeFrame) {
197 DLOG(ERROR) << "Missing or incomplete frames.";
198 // Unlike the SW decoder in libvpx, hw decoder cannot handle broken frames.
199 // Return an error to request a key frame.
200 return WEBRTC_VIDEO_CODEC_ERROR;
203 // Most platforms' VDA implementations support mid-stream resolution change
204 // internally. Platforms whose VDAs fail to support mid-stream resolution
205 // change gracefully need to have their clients cover for them, and we do that
206 // here.
207 #ifdef ANDROID
208 const bool kVDACanHandleMidstreamResize = false;
209 #else
210 const bool kVDACanHandleMidstreamResize = true;
211 #endif
213 bool need_to_reset_for_midstream_resize = false;
214 if (inputImage._frameType == webrtc::kKeyFrame) {
215 gfx::Size new_frame_size(inputImage._encodedWidth,
216 inputImage._encodedHeight);
217 DVLOG(2) << "Got key frame. size=" << new_frame_size.ToString();
219 if (new_frame_size.width() > max_resolution_.width() ||
220 new_frame_size.width() < min_resolution_.width() ||
221 new_frame_size.height() > max_resolution_.height() ||
222 new_frame_size.height() < min_resolution_.height()) {
223 DVLOG(1) << "Resolution unsupported, falling back to software decode";
224 return WEBRTC_VIDEO_CODEC_FALLBACK_SOFTWARE;
227 gfx::Size prev_frame_size = frame_size_;
228 frame_size_ = new_frame_size;
229 if (!kVDACanHandleMidstreamResize && !prev_frame_size.IsEmpty() &&
230 prev_frame_size != frame_size_) {
231 need_to_reset_for_midstream_resize = true;
233 } else if (IsFirstBufferAfterReset(next_bitstream_buffer_id_,
234 reset_bitstream_buffer_id_)) {
235 // TODO(wuchengli): VDA should handle it. Remove this when
236 // http://crosbug.com/p/21913 is fixed.
237 DVLOG(1) << "The first frame should be a key frame. Drop this.";
238 return WEBRTC_VIDEO_CODEC_ERROR;
241 // Create buffer metadata.
242 BufferData buffer_data(next_bitstream_buffer_id_,
243 inputImage._timeStamp,
244 inputImage._length);
245 // Mask against 30 bits, to avoid (undefined) wraparound on signed integer.
246 next_bitstream_buffer_id_ = (next_bitstream_buffer_id_ + 1) & ID_LAST;
248 // If a shared memory segment is available, there are no pending buffers, and
249 // this isn't a mid-stream resolution change, then send the buffer for decode
250 // immediately. Otherwise, save the buffer in the queue for later decode.
251 scoped_ptr<SHMBuffer> shm_buffer;
252 if (!need_to_reset_for_midstream_resize && pending_buffers_.size() == 0)
253 shm_buffer = GetSHM_Locked(inputImage._length);
254 if (!shm_buffer) {
255 if (!SaveToPendingBuffers_Locked(inputImage, buffer_data))
256 return WEBRTC_VIDEO_CODEC_ERROR;
257 if (need_to_reset_for_midstream_resize) {
258 base::AutoUnlock auto_unlock(lock_);
259 Reset();
261 return WEBRTC_VIDEO_CODEC_OK;
264 SaveToDecodeBuffers_Locked(inputImage, shm_buffer.Pass(), buffer_data);
265 factories_->GetTaskRunner()->PostTask(
266 FROM_HERE,
267 base::Bind(&RTCVideoDecoder::RequestBufferDecode,
268 weak_factory_.GetWeakPtr()));
269 return WEBRTC_VIDEO_CODEC_OK;
272 int32_t RTCVideoDecoder::RegisterDecodeCompleteCallback(
273 webrtc::DecodedImageCallback* callback) {
274 DVLOG(2) << "RegisterDecodeCompleteCallback";
275 base::AutoLock auto_lock(lock_);
276 decode_complete_callback_ = callback;
277 return WEBRTC_VIDEO_CODEC_OK;
280 int32_t RTCVideoDecoder::Release() {
281 DVLOG(2) << "Release";
282 // Do not destroy VDA because WebRTC can call InitDecode and start decoding
283 // again.
284 return Reset();
287 int32_t RTCVideoDecoder::Reset() {
288 DVLOG(2) << "Reset";
289 base::AutoLock auto_lock(lock_);
290 if (state_ == UNINITIALIZED) {
291 LOG(ERROR) << "Decoder not initialized.";
292 return WEBRTC_VIDEO_CODEC_UNINITIALIZED;
294 if (next_bitstream_buffer_id_ != 0)
295 reset_bitstream_buffer_id_ = next_bitstream_buffer_id_ - 1;
296 else
297 reset_bitstream_buffer_id_ = ID_LAST;
298 // If VDA is already resetting, no need to request the reset again.
299 if (state_ != RESETTING) {
300 state_ = RESETTING;
301 factories_->GetTaskRunner()->PostTask(
302 FROM_HERE,
303 base::Bind(&RTCVideoDecoder::ResetInternal,
304 weak_factory_.GetWeakPtr()));
306 return WEBRTC_VIDEO_CODEC_OK;
309 void RTCVideoDecoder::ProvidePictureBuffers(uint32 count,
310 const gfx::Size& size,
311 uint32 texture_target) {
312 DCheckGpuVideoAcceleratorFactoriesTaskRunnerIsCurrent();
313 DVLOG(3) << "ProvidePictureBuffers. texture_target=" << texture_target;
315 if (!vda_)
316 return;
318 std::vector<uint32> texture_ids;
319 std::vector<gpu::Mailbox> texture_mailboxes;
320 decoder_texture_target_ = texture_target;
321 if (!factories_->CreateTextures(count,
322 size,
323 &texture_ids,
324 &texture_mailboxes,
325 decoder_texture_target_)) {
326 NotifyError(media::VideoDecodeAccelerator::PLATFORM_FAILURE);
327 return;
329 DCHECK_EQ(count, texture_ids.size());
330 DCHECK_EQ(count, texture_mailboxes.size());
332 std::vector<media::PictureBuffer> picture_buffers;
333 for (size_t i = 0; i < texture_ids.size(); ++i) {
334 picture_buffers.push_back(media::PictureBuffer(
335 next_picture_buffer_id_++, size, texture_ids[i], texture_mailboxes[i]));
336 bool inserted = assigned_picture_buffers_.insert(std::make_pair(
337 picture_buffers.back().id(), picture_buffers.back())).second;
338 DCHECK(inserted);
340 vda_->AssignPictureBuffers(picture_buffers);
343 void RTCVideoDecoder::DismissPictureBuffer(int32 id) {
344 DVLOG(3) << "DismissPictureBuffer. id=" << id;
345 DCheckGpuVideoAcceleratorFactoriesTaskRunnerIsCurrent();
347 std::map<int32, media::PictureBuffer>::iterator it =
348 assigned_picture_buffers_.find(id);
349 if (it == assigned_picture_buffers_.end()) {
350 NOTREACHED() << "Missing picture buffer: " << id;
351 return;
354 media::PictureBuffer buffer_to_dismiss = it->second;
355 assigned_picture_buffers_.erase(it);
357 if (!picture_buffers_at_display_.count(id)) {
358 // We can delete the texture immediately as it's not being displayed.
359 factories_->DeleteTexture(buffer_to_dismiss.texture_id());
360 return;
362 // Not destroying a texture in display in |picture_buffers_at_display_|.
363 // Postpone deletion until after it's returned to us.
366 void RTCVideoDecoder::PictureReady(const media::Picture& picture) {
367 DVLOG(3) << "PictureReady";
368 DCheckGpuVideoAcceleratorFactoriesTaskRunnerIsCurrent();
370 std::map<int32, media::PictureBuffer>::iterator it =
371 assigned_picture_buffers_.find(picture.picture_buffer_id());
372 if (it == assigned_picture_buffers_.end()) {
373 NOTREACHED() << "Missing picture buffer: " << picture.picture_buffer_id();
374 NotifyError(media::VideoDecodeAccelerator::PLATFORM_FAILURE);
375 return;
377 const media::PictureBuffer& pb = it->second;
379 // Validate picture rectangle from GPU.
380 if (picture.visible_rect().IsEmpty() ||
381 !gfx::Rect(pb.size()).Contains(picture.visible_rect())) {
382 NOTREACHED() << "Invalid picture size from VDA: "
383 << picture.visible_rect().ToString() << " should fit in "
384 << pb.size().ToString();
385 NotifyError(media::VideoDecodeAccelerator::PLATFORM_FAILURE);
386 return;
389 // Create a media::VideoFrame.
390 uint32_t timestamp = 0;
391 GetBufferData(picture.bitstream_buffer_id(), &timestamp);
392 scoped_refptr<media::VideoFrame> frame =
393 CreateVideoFrame(picture, pb, timestamp);
394 bool inserted =
395 picture_buffers_at_display_.insert(std::make_pair(
396 picture.picture_buffer_id(),
397 pb.texture_id())).second;
398 DCHECK(inserted);
400 // Create a WebRTC video frame.
401 webrtc::VideoFrame decoded_image(
402 new rtc::RefCountedObject<WebRtcVideoFrameAdapter>(frame), timestamp, 0,
403 webrtc::kVideoRotation_0);
405 // Invoke decode callback. WebRTC expects no callback after Reset or Release.
407 base::AutoLock auto_lock(lock_);
408 DCHECK(decode_complete_callback_ != NULL);
409 if (IsBufferAfterReset(picture.bitstream_buffer_id(),
410 reset_bitstream_buffer_id_)) {
411 decode_complete_callback_->Decoded(decoded_image);
416 scoped_refptr<media::VideoFrame> RTCVideoDecoder::CreateVideoFrame(
417 const media::Picture& picture,
418 const media::PictureBuffer& pb,
419 uint32_t timestamp) {
420 gfx::Rect visible_rect(picture.visible_rect());
421 DCHECK(decoder_texture_target_);
422 // Convert timestamp from 90KHz to ms.
423 base::TimeDelta timestamp_ms = base::TimeDelta::FromInternalValue(
424 base::checked_cast<uint64_t>(timestamp) * 1000 / 90);
425 // TODO(mcasas): The incoming data is actually a YUV format, but is labelled
426 // as ARGB. This prevents the compositor from messing with it, since the
427 // underlying platform can handle the former format natively. Make sure the
428 // correct format is used and everyone down the line understands it.
429 scoped_refptr<media::VideoFrame> frame(media::VideoFrame::WrapNativeTexture(
430 media::VideoFrame::ARGB,
431 gpu::MailboxHolder(pb.texture_mailbox(), decoder_texture_target_, 0),
432 media::BindToCurrentLoop(base::Bind(
433 &RTCVideoDecoder::ReleaseMailbox, weak_factory_.GetWeakPtr(),
434 factories_, picture.picture_buffer_id(), pb.texture_id())),
435 pb.size(), visible_rect, visible_rect.size(), timestamp_ms));
436 if (picture.allow_overlay()) {
437 frame->metadata()->SetBoolean(media::VideoFrameMetadata::ALLOW_OVERLAY,
438 true);
440 return frame;
443 void RTCVideoDecoder::NotifyEndOfBitstreamBuffer(int32 id) {
444 DVLOG(3) << "NotifyEndOfBitstreamBuffer. id=" << id;
445 DCheckGpuVideoAcceleratorFactoriesTaskRunnerIsCurrent();
447 std::map<int32, SHMBuffer*>::iterator it =
448 bitstream_buffers_in_decoder_.find(id);
449 if (it == bitstream_buffers_in_decoder_.end()) {
450 NotifyError(media::VideoDecodeAccelerator::PLATFORM_FAILURE);
451 NOTREACHED() << "Missing bitstream buffer: " << id;
452 return;
456 base::AutoLock auto_lock(lock_);
457 PutSHM_Locked(scoped_ptr<SHMBuffer>(it->second));
459 bitstream_buffers_in_decoder_.erase(it);
461 RequestBufferDecode();
464 void RTCVideoDecoder::NotifyFlushDone() {
465 DVLOG(3) << "NotifyFlushDone";
466 NOTREACHED() << "Unexpected flush done notification.";
469 void RTCVideoDecoder::NotifyResetDone() {
470 DCheckGpuVideoAcceleratorFactoriesTaskRunnerIsCurrent();
471 DVLOG(3) << "NotifyResetDone";
473 if (!vda_)
474 return;
476 input_buffer_data_.clear();
478 base::AutoLock auto_lock(lock_);
479 state_ = INITIALIZED;
481 // Send the pending buffers for decoding.
482 RequestBufferDecode();
485 void RTCVideoDecoder::NotifyError(media::VideoDecodeAccelerator::Error error) {
486 DCheckGpuVideoAcceleratorFactoriesTaskRunnerIsCurrent();
487 if (!vda_)
488 return;
490 LOG(ERROR) << "VDA Error:" << error;
491 UMA_HISTOGRAM_ENUMERATION("Media.RTCVideoDecoderError",
492 error,
493 media::VideoDecodeAccelerator::LARGEST_ERROR_ENUM);
494 DestroyVDA();
496 base::AutoLock auto_lock(lock_);
497 state_ = DECODE_ERROR;
500 void RTCVideoDecoder::RequestBufferDecode() {
501 DCheckGpuVideoAcceleratorFactoriesTaskRunnerIsCurrent();
502 if (!vda_)
503 return;
505 MovePendingBuffersToDecodeBuffers();
507 while (CanMoreDecodeWorkBeDone()) {
508 // Get a buffer and data from the queue.
509 SHMBuffer* shm_buffer = NULL;
510 BufferData buffer_data;
512 base::AutoLock auto_lock(lock_);
513 // Do not request decode if VDA is resetting.
514 if (decode_buffers_.size() == 0 || state_ == RESETTING)
515 return;
516 shm_buffer = decode_buffers_.front().first;
517 buffer_data = decode_buffers_.front().second;
518 decode_buffers_.pop_front();
519 // Drop the buffers before Reset or Release is called.
520 if (!IsBufferAfterReset(buffer_data.bitstream_buffer_id,
521 reset_bitstream_buffer_id_)) {
522 PutSHM_Locked(scoped_ptr<SHMBuffer>(shm_buffer));
523 continue;
527 // Create a BitstreamBuffer and send to VDA to decode.
528 media::BitstreamBuffer bitstream_buffer(buffer_data.bitstream_buffer_id,
529 shm_buffer->shm->handle(),
530 buffer_data.size);
531 bool inserted = bitstream_buffers_in_decoder_
532 .insert(std::make_pair(bitstream_buffer.id(), shm_buffer)).second;
533 DCHECK(inserted);
534 RecordBufferData(buffer_data);
535 vda_->Decode(bitstream_buffer);
539 bool RTCVideoDecoder::CanMoreDecodeWorkBeDone() {
540 return bitstream_buffers_in_decoder_.size() < kMaxInFlightDecodes;
543 bool RTCVideoDecoder::IsBufferAfterReset(int32 id_buffer, int32 id_reset) {
544 if (id_reset == ID_INVALID)
545 return true;
546 int32 diff = id_buffer - id_reset;
547 if (diff <= 0)
548 diff += ID_LAST + 1;
549 return diff < ID_HALF;
552 bool RTCVideoDecoder::IsFirstBufferAfterReset(int32 id_buffer, int32 id_reset) {
553 if (id_reset == ID_INVALID)
554 return id_buffer == 0;
555 return id_buffer == ((id_reset + 1) & ID_LAST);
558 void RTCVideoDecoder::SaveToDecodeBuffers_Locked(
559 const webrtc::EncodedImage& input_image,
560 scoped_ptr<SHMBuffer> shm_buffer,
561 const BufferData& buffer_data) {
562 memcpy(shm_buffer->shm->memory(), input_image._buffer, input_image._length);
563 std::pair<SHMBuffer*, BufferData> buffer_pair =
564 std::make_pair(shm_buffer.release(), buffer_data);
566 // Store the buffer and the metadata to the queue.
567 decode_buffers_.push_back(buffer_pair);
570 bool RTCVideoDecoder::SaveToPendingBuffers_Locked(
571 const webrtc::EncodedImage& input_image,
572 const BufferData& buffer_data) {
573 DVLOG(2) << "SaveToPendingBuffers_Locked"
574 << ". pending_buffers size=" << pending_buffers_.size()
575 << ". decode_buffers_ size=" << decode_buffers_.size()
576 << ". available_shm size=" << available_shm_segments_.size();
577 // Queued too many buffers. Something goes wrong.
578 if (pending_buffers_.size() >= kMaxNumOfPendingBuffers) {
579 LOG(WARNING) << "Too many pending buffers!";
580 return false;
583 // Clone the input image and save it to the queue.
584 uint8_t* buffer = new uint8_t[input_image._length];
585 // TODO(wuchengli): avoid memcpy. Extend webrtc::VideoDecoder::Decode()
586 // interface to take a non-const ptr to the frame and add a method to the
587 // frame that will swap buffers with another.
588 memcpy(buffer, input_image._buffer, input_image._length);
589 webrtc::EncodedImage encoded_image(
590 buffer, input_image._length, input_image._length);
591 std::pair<webrtc::EncodedImage, BufferData> buffer_pair =
592 std::make_pair(encoded_image, buffer_data);
594 pending_buffers_.push_back(buffer_pair);
595 return true;
598 void RTCVideoDecoder::MovePendingBuffersToDecodeBuffers() {
599 base::AutoLock auto_lock(lock_);
600 while (pending_buffers_.size() > 0) {
601 // Get a pending buffer from the queue.
602 const webrtc::EncodedImage& input_image = pending_buffers_.front().first;
603 const BufferData& buffer_data = pending_buffers_.front().second;
605 // Drop the frame if it comes before Reset or Release.
606 if (!IsBufferAfterReset(buffer_data.bitstream_buffer_id,
607 reset_bitstream_buffer_id_)) {
608 delete[] input_image._buffer;
609 pending_buffers_.pop_front();
610 continue;
612 // Get shared memory and save it to decode buffers.
613 scoped_ptr<SHMBuffer> shm_buffer = GetSHM_Locked(input_image._length);
614 if (!shm_buffer)
615 return;
616 SaveToDecodeBuffers_Locked(input_image, shm_buffer.Pass(), buffer_data);
617 delete[] input_image._buffer;
618 pending_buffers_.pop_front();
622 void RTCVideoDecoder::ResetInternal() {
623 DCheckGpuVideoAcceleratorFactoriesTaskRunnerIsCurrent();
624 DVLOG(2) << "ResetInternal";
625 if (vda_)
626 vda_->Reset();
629 // static
630 void RTCVideoDecoder::ReleaseMailbox(
631 base::WeakPtr<RTCVideoDecoder> decoder,
632 const scoped_refptr<media::GpuVideoAcceleratorFactories>& factories,
633 int64 picture_buffer_id,
634 uint32 texture_id,
635 uint32 release_sync_point) {
636 DCHECK(factories->GetTaskRunner()->BelongsToCurrentThread());
637 factories->WaitSyncPoint(release_sync_point);
639 if (decoder) {
640 decoder->ReusePictureBuffer(picture_buffer_id);
641 return;
643 // It's the last chance to delete the texture after display,
644 // because RTCVideoDecoder was destructed.
645 factories->DeleteTexture(texture_id);
648 void RTCVideoDecoder::ReusePictureBuffer(int64 picture_buffer_id) {
649 DCheckGpuVideoAcceleratorFactoriesTaskRunnerIsCurrent();
650 DVLOG(3) << "ReusePictureBuffer. id=" << picture_buffer_id;
652 DCHECK(!picture_buffers_at_display_.empty());
653 PictureBufferTextureMap::iterator display_iterator =
654 picture_buffers_at_display_.find(picture_buffer_id);
655 uint32 texture_id = display_iterator->second;
656 DCHECK(display_iterator != picture_buffers_at_display_.end());
657 picture_buffers_at_display_.erase(display_iterator);
659 if (!assigned_picture_buffers_.count(picture_buffer_id)) {
660 // This picture was dismissed while in display, so we postponed deletion.
661 factories_->DeleteTexture(texture_id);
662 return;
665 // DestroyVDA() might already have been called.
666 if (vda_)
667 vda_->ReusePictureBuffer(picture_buffer_id);
670 bool RTCVideoDecoder::IsProfileSupported(media::VideoCodecProfile profile) {
671 DCheckGpuVideoAcceleratorFactoriesTaskRunnerIsCurrent();
672 media::VideoDecodeAccelerator::SupportedProfiles supported_profiles =
673 factories_->GetVideoDecodeAcceleratorSupportedProfiles();
675 for (const auto& supported_profile : supported_profiles) {
676 if (profile == supported_profile.profile) {
677 min_resolution_ = supported_profile.min_resolution;
678 max_resolution_ = supported_profile.max_resolution;
679 return true;
683 return false;
686 void RTCVideoDecoder::CreateVDA(media::VideoCodecProfile profile,
687 base::WaitableEvent* waiter) {
688 DCheckGpuVideoAcceleratorFactoriesTaskRunnerIsCurrent();
690 if (!IsProfileSupported(profile)) {
691 DVLOG(1) << "Unsupported profile " << profile;
692 } else {
693 vda_ = factories_->CreateVideoDecodeAccelerator();
694 if (vda_ && !vda_->Initialize(profile, this))
695 vda_.release()->Destroy();
698 waiter->Signal();
701 void RTCVideoDecoder::DestroyTextures() {
702 DCheckGpuVideoAcceleratorFactoriesTaskRunnerIsCurrent();
704 // Not destroying PictureBuffers in |picture_buffers_at_display_| yet, since
705 // their textures may still be in use by the user of this RTCVideoDecoder.
706 for (PictureBufferTextureMap::iterator it =
707 picture_buffers_at_display_.begin();
708 it != picture_buffers_at_display_.end();
709 ++it) {
710 assigned_picture_buffers_.erase(it->first);
713 for (std::map<int32, media::PictureBuffer>::iterator it =
714 assigned_picture_buffers_.begin();
715 it != assigned_picture_buffers_.end();
716 ++it) {
717 factories_->DeleteTexture(it->second.texture_id());
719 assigned_picture_buffers_.clear();
722 void RTCVideoDecoder::DestroyVDA() {
723 DVLOG(2) << "DestroyVDA";
724 DCheckGpuVideoAcceleratorFactoriesTaskRunnerIsCurrent();
725 if (vda_)
726 vda_.release()->Destroy();
727 DestroyTextures();
728 base::AutoLock auto_lock(lock_);
729 state_ = UNINITIALIZED;
732 scoped_ptr<RTCVideoDecoder::SHMBuffer> RTCVideoDecoder::GetSHM_Locked(
733 size_t min_size) {
734 // Reuse a SHM if possible.
735 SHMBuffer* ret = NULL;
736 if (!available_shm_segments_.empty() &&
737 available_shm_segments_.back()->size >= min_size) {
738 ret = available_shm_segments_.back();
739 available_shm_segments_.pop_back();
741 // Post to vda thread to create shared memory if SHM cannot be reused or the
742 // queue is almost empty.
743 if (num_shm_buffers_ < kMaxNumSharedMemorySegments &&
744 (ret == NULL || available_shm_segments_.size() <= 1)) {
745 factories_->GetTaskRunner()->PostTask(
746 FROM_HERE,
747 base::Bind(&RTCVideoDecoder::CreateSHM,
748 weak_factory_.GetWeakPtr(),
750 min_size));
752 return scoped_ptr<SHMBuffer>(ret);
755 void RTCVideoDecoder::PutSHM_Locked(scoped_ptr<SHMBuffer> shm_buffer) {
756 available_shm_segments_.push_back(shm_buffer.release());
759 void RTCVideoDecoder::CreateSHM(int number, size_t min_size) {
760 DCheckGpuVideoAcceleratorFactoriesTaskRunnerIsCurrent();
761 DVLOG(2) << "CreateSHM. size=" << min_size;
762 int number_to_allocate;
764 base::AutoLock auto_lock(lock_);
765 number_to_allocate =
766 std::min(kMaxNumSharedMemorySegments - num_shm_buffers_, number);
768 size_t size_to_allocate = std::max(min_size, kSharedMemorySegmentBytes);
769 for (int i = 0; i < number_to_allocate; i++) {
770 scoped_ptr<base::SharedMemory> shm =
771 factories_->CreateSharedMemory(size_to_allocate);
772 if (shm) {
773 base::AutoLock auto_lock(lock_);
774 num_shm_buffers_++;
775 PutSHM_Locked(
776 scoped_ptr<SHMBuffer>(new SHMBuffer(shm.Pass(), size_to_allocate)));
779 // Kick off the decoding.
780 RequestBufferDecode();
783 void RTCVideoDecoder::RecordBufferData(const BufferData& buffer_data) {
784 input_buffer_data_.push_front(buffer_data);
785 // Why this value? Because why not. avformat.h:MAX_REORDER_DELAY is 16, but
786 // that's too small for some pathological B-frame test videos. The cost of
787 // using too-high a value is low (192 bits per extra slot).
788 static const size_t kMaxInputBufferDataSize = 128;
789 // Pop from the back of the list, because that's the oldest and least likely
790 // to be useful in the future data.
791 if (input_buffer_data_.size() > kMaxInputBufferDataSize)
792 input_buffer_data_.pop_back();
795 void RTCVideoDecoder::GetBufferData(int32 bitstream_buffer_id,
796 uint32_t* timestamp) {
797 for (std::list<BufferData>::iterator it = input_buffer_data_.begin();
798 it != input_buffer_data_.end();
799 ++it) {
800 if (it->bitstream_buffer_id != bitstream_buffer_id)
801 continue;
802 *timestamp = it->timestamp;
803 return;
805 NOTREACHED() << "Missing bitstream buffer id: " << bitstream_buffer_id;
808 int32_t RTCVideoDecoder::RecordInitDecodeUMA(int32_t status) {
809 // Logging boolean is enough to know if HW decoding has been used. Also,
810 // InitDecode is less likely to return an error so enum is not used here.
811 bool sample = (status == WEBRTC_VIDEO_CODEC_OK) ? true : false;
812 UMA_HISTOGRAM_BOOLEAN("Media.RTCVideoDecoderInitDecodeSuccess", sample);
813 return status;
816 void RTCVideoDecoder::DCheckGpuVideoAcceleratorFactoriesTaskRunnerIsCurrent()
817 const {
818 DCHECK(factories_->GetTaskRunner()->BelongsToCurrentThread());
821 } // namespace content