Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(80)

Side by Side Diff: content/renderer/media/gpu/rtc_video_encoder.cc

Issue 2435693004: Reland: Use webrtc::VideoFrame timestamp in RTCVideoEncoder (Closed)
Patch Set: Rebase. Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | content/renderer/media/gpu/rtc_video_encoder_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 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 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 "content/renderer/media/gpu/rtc_video_encoder.h" 5 #include "content/renderer/media/gpu/rtc_video_encoder.h"
6 6
7 #include <string.h> 7 #include <string.h>
8 8
9 #include <deque>
9 #include <memory> 10 #include <memory>
10 #include <vector> 11 #include <vector>
11 12
12 #include "base/bind.h" 13 #include "base/bind.h"
13 #include "base/location.h" 14 #include "base/location.h"
14 #include "base/logging.h" 15 #include "base/logging.h"
15 #include "base/macros.h" 16 #include "base/macros.h"
16 #include "base/metrics/histogram_macros.h" 17 #include "base/metrics/histogram_macros.h"
17 #include "base/numerics/safe_conversions.h" 18 #include "base/numerics/safe_conversions.h"
18 #include "base/rand_util.h" 19 #include "base/rand_util.h"
19 #include "base/single_thread_task_runner.h" 20 #include "base/single_thread_task_runner.h"
20 #include "base/synchronization/lock.h" 21 #include "base/synchronization/lock.h"
21 #include "base/synchronization/waitable_event.h" 22 #include "base/synchronization/waitable_event.h"
22 #include "base/threading/thread_task_runner_handle.h" 23 #include "base/threading/thread_task_runner_handle.h"
24 #include "base/time/time.h"
23 #include "media/base/bind_to_current_loop.h" 25 #include "media/base/bind_to_current_loop.h"
24 #include "media/base/bitstream_buffer.h" 26 #include "media/base/bitstream_buffer.h"
25 #include "media/base/video_frame.h" 27 #include "media/base/video_frame.h"
26 #include "media/base/video_util.h" 28 #include "media/base/video_util.h"
27 #include "media/filters/h264_parser.h" 29 #include "media/filters/h264_parser.h"
28 #include "media/renderers/gpu_video_accelerator_factories.h" 30 #include "media/renderers/gpu_video_accelerator_factories.h"
29 #include "media/video/video_encode_accelerator.h" 31 #include "media/video/video_encode_accelerator.h"
30 #include "third_party/libyuv/include/libyuv.h" 32 #include "third_party/libyuv/include/libyuv.h"
31 #include "third_party/webrtc/base/timeutils.h" 33 #include "third_party/webrtc/base/timeutils.h"
32 34
33 namespace content { 35 namespace content {
34 36
35 namespace { 37 namespace {
36 38
39 struct RTCTimestamps {
40 RTCTimestamps(const base::TimeDelta& media_timestamp, int32_t rtp_timestamp)
41 : media_timestamp_in_microseconds(media_timestamp.InMicroseconds()),
42 rtp_timestamp(rtp_timestamp) {}
43 const int64_t media_timestamp_in_microseconds;
liberato (no reviews please) 2017/03/27 19:31:49 why not use base::TimeDelta for this? seems like
emircan 2017/03/27 20:20:32 Done.
44 const int32_t rtp_timestamp;
45
46 private:
47 DISALLOW_IMPLICIT_CONSTRUCTORS(RTCTimestamps);
48 };
49
37 // Translate from webrtc::VideoCodecType and webrtc::VideoCodec to 50 // Translate from webrtc::VideoCodecType and webrtc::VideoCodec to
38 // media::VideoCodecProfile. 51 // media::VideoCodecProfile.
39 media::VideoCodecProfile WebRTCVideoCodecToVideoCodecProfile( 52 media::VideoCodecProfile WebRTCVideoCodecToVideoCodecProfile(
40 webrtc::VideoCodecType type, 53 webrtc::VideoCodecType type,
41 const webrtc::VideoCodec* codec_settings) { 54 const webrtc::VideoCodec* codec_settings) {
42 DCHECK_EQ(type, codec_settings->codecType); 55 DCHECK_EQ(type, codec_settings->codecType);
43 switch (type) { 56 switch (type) {
44 case webrtc::kVideoCodecVP8: 57 case webrtc::kVideoCodecVP8:
45 return media::VP8PROFILE_ANY; 58 return media::VP8PROFILE_ANY;
46 case webrtc::kVideoCodecVP9: 59 case webrtc::kVideoCodecVP9:
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after
205 // webrtc::VideoEncoder expects InitEncode() and Encode() to be synchronous. 218 // webrtc::VideoEncoder expects InitEncode() and Encode() to be synchronous.
206 // Do this by waiting on the |async_waiter_| and returning the return value in 219 // Do this by waiting on the |async_waiter_| and returning the return value in
207 // |async_retval_| when initialization completes, encoding completes, or 220 // |async_retval_| when initialization completes, encoding completes, or
208 // an error occurs. 221 // an error occurs.
209 base::WaitableEvent* async_waiter_; 222 base::WaitableEvent* async_waiter_;
210 int32_t* async_retval_; 223 int32_t* async_retval_;
211 224
212 // The underlying VEA to perform encoding on. 225 // The underlying VEA to perform encoding on.
213 std::unique_ptr<media::VideoEncodeAccelerator> video_encoder_; 226 std::unique_ptr<media::VideoEncodeAccelerator> video_encoder_;
214 227
228 // Used to match the encoded frame timestamp with WebRTC's given RTP
229 // timestamp.
230 std::deque<RTCTimestamps> pending_timestamps_;
231
232 // Indicates that timestamp match failed and we should no longer attempt
233 // matching.
234 bool failed_timestamp_match_;
235
215 // Next input frame. Since there is at most one next frame, a single-element 236 // Next input frame. Since there is at most one next frame, a single-element
216 // queue is sufficient. 237 // queue is sufficient.
217 const webrtc::VideoFrame* input_next_frame_; 238 const webrtc::VideoFrame* input_next_frame_;
218 239
219 // Whether to encode a keyframe next. 240 // Whether to encode a keyframe next.
220 bool input_next_frame_keyframe_; 241 bool input_next_frame_keyframe_;
221 242
222 // Frame sizes. 243 // Frame sizes.
223 gfx::Size input_frame_coded_size_; 244 gfx::Size input_frame_coded_size_;
224 gfx::Size input_visible_size_; 245 gfx::Size input_visible_size_;
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
257 // Instead, we cache an error status here and return it the next time an 278 // Instead, we cache an error status here and return it the next time an
258 // interface entry point is called. This is protected by |status_lock_|. 279 // interface entry point is called. This is protected by |status_lock_|.
259 int32_t status_; 280 int32_t status_;
260 281
261 DISALLOW_COPY_AND_ASSIGN(Impl); 282 DISALLOW_COPY_AND_ASSIGN(Impl);
262 }; 283 };
263 284
264 RTCVideoEncoder::Impl::Impl(media::GpuVideoAcceleratorFactories* gpu_factories, 285 RTCVideoEncoder::Impl::Impl(media::GpuVideoAcceleratorFactories* gpu_factories,
265 webrtc::VideoCodecType video_codec_type) 286 webrtc::VideoCodecType video_codec_type)
266 : gpu_factories_(gpu_factories), 287 : gpu_factories_(gpu_factories),
267 async_waiter_(NULL), 288 async_waiter_(nullptr),
268 async_retval_(NULL), 289 async_retval_(nullptr),
269 input_next_frame_(NULL), 290 failed_timestamp_match_(false),
291 input_next_frame_(nullptr),
270 input_next_frame_keyframe_(false), 292 input_next_frame_keyframe_(false),
271 output_buffers_free_count_(0), 293 output_buffers_free_count_(0),
272 last_capture_time_ms_(-1), 294 last_capture_time_ms_(-1),
273 encoded_image_callback_(nullptr), 295 encoded_image_callback_(nullptr),
274 video_codec_type_(video_codec_type), 296 video_codec_type_(video_codec_type),
275 status_(WEBRTC_VIDEO_CODEC_UNINITIALIZED) { 297 status_(WEBRTC_VIDEO_CODEC_UNINITIALIZED) {
276 thread_checker_.DetachFromThread(); 298 thread_checker_.DetachFromThread();
277 // Picture ID should start on a random number. 299 // Picture ID should start on a random number.
278 picture_id_ = static_cast<uint16_t>(base::RandInt(0, 0x7FFF)); 300 picture_id_ = static_cast<uint16_t>(base::RandInt(0, 0x7FFF));
279 } 301 }
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after
448 video_encoder_->UseOutputBitstreamBuffer(media::BitstreamBuffer( 470 video_encoder_->UseOutputBitstreamBuffer(media::BitstreamBuffer(
449 i, output_buffers_[i]->handle(), output_buffers_[i]->mapped_size())); 471 i, output_buffers_[i]->handle(), output_buffers_[i]->mapped_size()));
450 output_buffers_free_count_++; 472 output_buffers_free_count_++;
451 } 473 }
452 DCHECK_EQ(GetStatus(), WEBRTC_VIDEO_CODEC_UNINITIALIZED); 474 DCHECK_EQ(GetStatus(), WEBRTC_VIDEO_CODEC_UNINITIALIZED);
453 SetStatus(WEBRTC_VIDEO_CODEC_OK); 475 SetStatus(WEBRTC_VIDEO_CODEC_OK);
454 SignalAsyncWaiter(WEBRTC_VIDEO_CODEC_OK); 476 SignalAsyncWaiter(WEBRTC_VIDEO_CODEC_OK);
455 } 477 }
456 478
457 void RTCVideoEncoder::Impl::BitstreamBufferReady(int32_t bitstream_buffer_id, 479 void RTCVideoEncoder::Impl::BitstreamBufferReady(int32_t bitstream_buffer_id,
458 size_t payload_size, 480 size_t payload_size,
459 bool key_frame, 481 bool key_frame,
460 base::TimeDelta timestamp) { 482 base::TimeDelta timestamp) {
461 DVLOG(3) << "Impl::BitstreamBufferReady(): bitstream_buffer_id=" 483 DVLOG(3) << "Impl::BitstreamBufferReady(): bitstream_buffer_id="
462 << bitstream_buffer_id << ", payload_size=" << payload_size 484 << bitstream_buffer_id << ", payload_size=" << payload_size
463 << ", key_frame=" << key_frame 485 << ", key_frame=" << key_frame
464 << ", timestamp ms=" << timestamp.InMilliseconds(); 486 << ", timestamp ms=" << timestamp.InMilliseconds();
465 DCHECK(thread_checker_.CalledOnValidThread()); 487 DCHECK(thread_checker_.CalledOnValidThread());
466 488
467 if (bitstream_buffer_id < 0 || 489 if (bitstream_buffer_id < 0 ||
468 bitstream_buffer_id >= static_cast<int>(output_buffers_.size())) { 490 bitstream_buffer_id >= static_cast<int>(output_buffers_.size())) {
469 LogAndNotifyError(FROM_HERE, "invalid bitstream_buffer_id", 491 LogAndNotifyError(FROM_HERE, "invalid bitstream_buffer_id",
470 media::VideoEncodeAccelerator::kPlatformFailureError); 492 media::VideoEncodeAccelerator::kPlatformFailureError);
471 return; 493 return;
472 } 494 }
473 base::SharedMemory* output_buffer = 495 base::SharedMemory* output_buffer =
474 output_buffers_[bitstream_buffer_id].get(); 496 output_buffers_[bitstream_buffer_id].get();
475 if (payload_size > output_buffer->mapped_size()) { 497 if (payload_size > output_buffer->mapped_size()) {
476 LogAndNotifyError(FROM_HERE, "invalid payload_size", 498 LogAndNotifyError(FROM_HERE, "invalid payload_size",
477 media::VideoEncodeAccelerator::kPlatformFailureError); 499 media::VideoEncodeAccelerator::kPlatformFailureError);
478 return; 500 return;
479 } 501 }
480 output_buffers_free_count_--; 502 output_buffers_free_count_--;
481 503
482 // Derive the capture time in ms from system clock. Make sure that it is 504 // Derive the capture time in ms from system clock. Make sure that it is
483 // greater than the last. 505 // greater than the last.
484 const int64_t capture_time_us = rtc::TimeMicros(); 506 const int64_t capture_time_us = rtc::TimeMicros();
485 int64_t capture_time_ms = 507 int64_t capture_time_ms =
486 capture_time_us / base::Time::kMicrosecondsPerMillisecond; 508 capture_time_us / base::Time::kMicrosecondsPerMillisecond;
487 capture_time_ms = std::max(capture_time_ms, last_capture_time_ms_ + 1); 509 capture_time_ms = std::max(capture_time_ms, last_capture_time_ms_ + 1);
488 last_capture_time_ms_ = capture_time_ms; 510 last_capture_time_ms_ = capture_time_ms;
489 511
490 // Fallback to the current time if encoder does not provide timestamp. 512 // Find RTP timestamp by going through |pending_timestamps_|. Derive it from
491 const int64_t encoder_time_us = 513 // capture time otherwise.
492 timestamp.is_zero() ? capture_time_us : timestamp.InMicroseconds(); 514 base::Optional<uint32_t> rtp_timestamp;
493 515 if (!timestamp.is_zero() && !failed_timestamp_match_) {
494 // Derive the RTP timestamp (in 90KHz ticks). It can wrap around, get the 516 // Pop timestamps until we have a match.
495 // lower 32 bits. 517 while (!pending_timestamps_.empty()) {
496 const uint32_t rtp_timestamp = static_cast<uint32_t>( 518 const auto& front_timestamps = pending_timestamps_.front();
497 encoder_time_us * 90 / base::Time::kMicrosecondsPerMillisecond); 519 if (front_timestamps.media_timestamp_in_microseconds ==
520 timestamp.InMicroseconds()) {
521 rtp_timestamp = front_timestamps.rtp_timestamp;
522 pending_timestamps_.pop_front();
523 break;
524 }
525 pending_timestamps_.pop_front();
526 }
527 DCHECK(rtp_timestamp.has_value());
528 }
529 if (!rtp_timestamp.has_value()) {
530 failed_timestamp_match_ = true;
531 pending_timestamps_.clear();
532 // RTP timestamp can wrap around. Get the lower 32 bits.
533 rtp_timestamp = static_cast<uint32_t>(
534 capture_time_us * 90 / base::Time::kMicrosecondsPerMillisecond);
535 }
498 536
499 webrtc::EncodedImage image( 537 webrtc::EncodedImage image(
500 reinterpret_cast<uint8_t*>(output_buffer->memory()), payload_size, 538 reinterpret_cast<uint8_t*>(output_buffer->memory()), payload_size,
501 output_buffer->mapped_size()); 539 output_buffer->mapped_size());
502 image._encodedWidth = input_visible_size_.width(); 540 image._encodedWidth = input_visible_size_.width();
503 image._encodedHeight = input_visible_size_.height(); 541 image._encodedHeight = input_visible_size_.height();
504 image._timeStamp = rtp_timestamp; 542 image._timeStamp = rtp_timestamp.value();
505 image.capture_time_ms_ = capture_time_ms; 543 image.capture_time_ms_ = capture_time_ms;
506 image._frameType = 544 image._frameType =
507 (key_frame ? webrtc::kVideoFrameKey : webrtc::kVideoFrameDelta); 545 (key_frame ? webrtc::kVideoFrameKey : webrtc::kVideoFrameDelta);
508 image._completeFrame = true; 546 image._completeFrame = true;
509 547
510 ReturnEncodedImage(image, bitstream_buffer_id, picture_id_); 548 ReturnEncodedImage(image, bitstream_buffer_id, picture_id_);
511 // Picture ID must wrap after reaching the maximum. 549 // Picture ID must wrap after reaching the maximum.
512 picture_id_ = (picture_id_ + 1) & 0x7FFF; 550 picture_id_ = (picture_id_ + 1) & 0x7FFF;
513 } 551 }
514 552
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after
614 frame->visible_rect().width(), 652 frame->visible_rect().width(),
615 frame->visible_rect().height(), 653 frame->visible_rect().height(),
616 libyuv::kFilterBox)) { 654 libyuv::kFilterBox)) {
617 LogAndNotifyError(FROM_HERE, "Failed to copy buffer", 655 LogAndNotifyError(FROM_HERE, "Failed to copy buffer",
618 media::VideoEncodeAccelerator::kPlatformFailureError); 656 media::VideoEncodeAccelerator::kPlatformFailureError);
619 return; 657 return;
620 } 658 }
621 } 659 }
622 frame->AddDestructionObserver(media::BindToCurrentLoop( 660 frame->AddDestructionObserver(media::BindToCurrentLoop(
623 base::Bind(&RTCVideoEncoder::Impl::EncodeFrameFinished, this, index))); 661 base::Bind(&RTCVideoEncoder::Impl::EncodeFrameFinished, this, index)));
662 if (!failed_timestamp_match_) {
663 DCHECK(std::find_if(pending_timestamps_.begin(), pending_timestamps_.end(),
664 [&frame](const RTCTimestamps& entry) {
665 return entry.media_timestamp_in_microseconds ==
666 frame->timestamp().InMicroseconds();
667 }) == pending_timestamps_.end());
668 pending_timestamps_.emplace_back(frame->timestamp(),
669 next_frame->timestamp());
670 }
624 video_encoder_->Encode(frame, next_frame_keyframe); 671 video_encoder_->Encode(frame, next_frame_keyframe);
625 input_buffers_free_.pop_back(); 672 input_buffers_free_.pop_back();
626 SignalAsyncWaiter(WEBRTC_VIDEO_CODEC_OK); 673 SignalAsyncWaiter(WEBRTC_VIDEO_CODEC_OK);
627 } 674 }
628 675
629 void RTCVideoEncoder::Impl::EncodeFrameFinished(int index) { 676 void RTCVideoEncoder::Impl::EncodeFrameFinished(int index) {
630 DVLOG(3) << "Impl::EncodeFrameFinished(): index=" << index; 677 DVLOG(3) << "Impl::EncodeFrameFinished(): index=" << index;
631 DCHECK(thread_checker_.CalledOnValidThread()); 678 DCHECK(thread_checker_.CalledOnValidThread());
632 DCHECK_GE(index, 0); 679 DCHECK_GE(index, 0);
633 DCHECK_LT(index, static_cast<int>(input_buffers_.size())); 680 DCHECK_LT(index, static_cast<int>(input_buffers_.size()));
(...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after
891 UMA_HISTOGRAM_BOOLEAN("Media.RTCVideoEncoderInitEncodeSuccess", 938 UMA_HISTOGRAM_BOOLEAN("Media.RTCVideoEncoderInitEncodeSuccess",
892 init_retval == WEBRTC_VIDEO_CODEC_OK); 939 init_retval == WEBRTC_VIDEO_CODEC_OK);
893 if (init_retval == WEBRTC_VIDEO_CODEC_OK) { 940 if (init_retval == WEBRTC_VIDEO_CODEC_OK) {
894 UMA_HISTOGRAM_ENUMERATION("Media.RTCVideoEncoderProfile", 941 UMA_HISTOGRAM_ENUMERATION("Media.RTCVideoEncoderProfile",
895 profile, 942 profile,
896 media::VIDEO_CODEC_PROFILE_MAX + 1); 943 media::VIDEO_CODEC_PROFILE_MAX + 1);
897 } 944 }
898 } 945 }
899 946
900 } // namespace content 947 } // namespace content
OLDNEW
« no previous file with comments | « no previous file | content/renderer/media/gpu/rtc_video_encoder_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698