| OLD | NEW |
| 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/rtc_video_encoder.h" | 5 #include "content/renderer/media/rtc_video_encoder.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/location.h" | 8 #include "base/location.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/memory/scoped_vector.h" | 10 #include "base/memory/scoped_vector.h" |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 #define NOTIFY_ERROR(x) \ | 23 #define NOTIFY_ERROR(x) \ |
| 24 do { \ | 24 do { \ |
| 25 DLOG(ERROR) << "calling NotifyError(): " << x; \ | 25 DLOG(ERROR) << "calling NotifyError(): " << x; \ |
| 26 NotifyError(x); \ | 26 NotifyError(x); \ |
| 27 } while (0) | 27 } while (0) |
| 28 | 28 |
| 29 namespace content { | 29 namespace content { |
| 30 | 30 |
| 31 namespace { | 31 namespace { |
| 32 | 32 |
| 33 // Translate from webrtc::VideoCodecType and webrtc::VideoCodec to |
| 34 // media::VideoCodecProfile. |
| 35 media::VideoCodecProfile WebRTCVideoCodecToVideoCodecProfile( |
| 36 webrtc::VideoCodecType type, const webrtc::VideoCodec* codec_settings) { |
| 37 DCHECK_EQ(type, codec_settings->codecType); |
| 38 switch (type) { |
| 39 case webrtc::kVideoCodecVP8: |
| 40 return media::VP8PROFILE_ANY; |
| 41 case webrtc::kVideoCodecH264: { |
| 42 switch (codec_settings->codecSpecific.H264.profile) { |
| 43 case webrtc::kProfileBase: |
| 44 return media::H264PROFILE_BASELINE; |
| 45 case webrtc::kProfileMain: |
| 46 return media::H264PROFILE_MAIN; |
| 47 } |
| 48 } |
| 49 case webrtc::kVideoCodecGeneric: |
| 50 return media::H264PROFILE_MAIN; |
| 51 default: |
| 52 NOTREACHED() << "Unrecognized video codec type"; |
| 53 return media::VIDEO_CODEC_PROFILE_UNKNOWN; |
| 54 } |
| 55 } |
| 56 |
| 33 // Populates struct webrtc::RTPFragmentationHeader for H264 codec. | 57 // Populates struct webrtc::RTPFragmentationHeader for H264 codec. |
| 34 // Each entry specifies the offset and length (excluding start code) of a NALU. | 58 // Each entry specifies the offset and length (excluding start code) of a NALU. |
| 35 // Returns true if successful. | 59 // Returns true if successful. |
| 36 bool GetRTPFragmentationHeaderH264(webrtc::RTPFragmentationHeader* header, | 60 bool GetRTPFragmentationHeaderH264(webrtc::RTPFragmentationHeader* header, |
| 37 const uint8_t* data, uint32_t length) { | 61 const uint8_t* data, uint32_t length) { |
| 38 media::H264Parser parser; | 62 media::H264Parser parser; |
| 39 parser.SetStream(data, length); | 63 parser.SetStream(data, length); |
| 40 | 64 |
| 41 std::vector<media::H264NALU> nalu_vector; | 65 std::vector<media::H264NALU> nalu_vector; |
| 42 while (true) { | 66 while (true) { |
| (...skipping 496 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 539 #undef NOTIFY_ERROR | 563 #undef NOTIFY_ERROR |
| 540 | 564 |
| 541 //////////////////////////////////////////////////////////////////////////////// | 565 //////////////////////////////////////////////////////////////////////////////// |
| 542 // | 566 // |
| 543 // RTCVideoEncoder | 567 // RTCVideoEncoder |
| 544 // | 568 // |
| 545 //////////////////////////////////////////////////////////////////////////////// | 569 //////////////////////////////////////////////////////////////////////////////// |
| 546 | 570 |
| 547 RTCVideoEncoder::RTCVideoEncoder( | 571 RTCVideoEncoder::RTCVideoEncoder( |
| 548 webrtc::VideoCodecType type, | 572 webrtc::VideoCodecType type, |
| 549 media::VideoCodecProfile profile, | |
| 550 const scoped_refptr<media::GpuVideoAcceleratorFactories>& gpu_factories) | 573 const scoped_refptr<media::GpuVideoAcceleratorFactories>& gpu_factories) |
| 551 : video_codec_type_(type), | 574 : video_codec_type_(type), |
| 552 video_codec_profile_(profile), | |
| 553 gpu_factories_(gpu_factories), | 575 gpu_factories_(gpu_factories), |
| 554 encoded_image_callback_(NULL), | 576 encoded_image_callback_(NULL), |
| 555 impl_status_(WEBRTC_VIDEO_CODEC_UNINITIALIZED), | 577 impl_status_(WEBRTC_VIDEO_CODEC_UNINITIALIZED), |
| 556 weak_factory_(this) { | 578 weak_factory_(this) { |
| 557 DVLOG(1) << "RTCVideoEncoder(): profile=" << profile; | 579 DVLOG(1) << "RTCVideoEncoder(): codec type=" << type; |
| 558 } | 580 } |
| 559 | 581 |
| 560 RTCVideoEncoder::~RTCVideoEncoder() { | 582 RTCVideoEncoder::~RTCVideoEncoder() { |
| 561 DVLOG(3) << "~RTCVideoEncoder"; | 583 DVLOG(3) << "~RTCVideoEncoder"; |
| 562 DCHECK(thread_checker_.CalledOnValidThread()); | 584 DCHECK(thread_checker_.CalledOnValidThread()); |
| 563 Release(); | 585 Release(); |
| 564 DCHECK(!impl_.get()); | 586 DCHECK(!impl_.get()); |
| 565 } | 587 } |
| 566 | 588 |
| 567 int32_t RTCVideoEncoder::InitEncode(const webrtc::VideoCodec* codec_settings, | 589 int32_t RTCVideoEncoder::InitEncode(const webrtc::VideoCodec* codec_settings, |
| 568 int32_t number_of_cores, | 590 int32_t number_of_cores, |
| 569 uint32_t max_payload_size) { | 591 uint32_t max_payload_size) { |
| 570 DVLOG(1) << "InitEncode(): codecType=" << codec_settings->codecType | 592 DVLOG(1) << "InitEncode(): codecType=" << codec_settings->codecType |
| 571 << ", width=" << codec_settings->width | 593 << ", width=" << codec_settings->width |
| 572 << ", height=" << codec_settings->height | 594 << ", height=" << codec_settings->height |
| 573 << ", startBitrate=" << codec_settings->startBitrate; | 595 << ", startBitrate=" << codec_settings->startBitrate; |
| 574 DCHECK(thread_checker_.CalledOnValidThread()); | 596 DCHECK(thread_checker_.CalledOnValidThread()); |
| 575 DCHECK(!impl_.get()); | 597 DCHECK(!impl_.get()); |
| 576 | 598 |
| 599 media::VideoCodecProfile profile = WebRTCVideoCodecToVideoCodecProfile( |
| 600 video_codec_type_, codec_settings); |
| 601 |
| 577 weak_factory_.InvalidateWeakPtrs(); | 602 weak_factory_.InvalidateWeakPtrs(); |
| 578 impl_ = new Impl(weak_factory_.GetWeakPtr(), gpu_factories_); | 603 impl_ = new Impl(weak_factory_.GetWeakPtr(), gpu_factories_); |
| 579 base::WaitableEvent initialization_waiter(true, false); | 604 base::WaitableEvent initialization_waiter(true, false); |
| 580 int32_t initialization_retval = WEBRTC_VIDEO_CODEC_UNINITIALIZED; | 605 int32_t initialization_retval = WEBRTC_VIDEO_CODEC_UNINITIALIZED; |
| 581 gpu_factories_->GetTaskRunner()->PostTask( | 606 gpu_factories_->GetTaskRunner()->PostTask( |
| 582 FROM_HERE, | 607 FROM_HERE, |
| 583 base::Bind(&RTCVideoEncoder::Impl::CreateAndInitializeVEA, | 608 base::Bind(&RTCVideoEncoder::Impl::CreateAndInitializeVEA, |
| 584 impl_, | 609 impl_, |
| 585 gfx::Size(codec_settings->width, codec_settings->height), | 610 gfx::Size(codec_settings->width, codec_settings->height), |
| 586 codec_settings->startBitrate, | 611 codec_settings->startBitrate, |
| 587 video_codec_profile_, | 612 profile, |
| 588 &initialization_waiter, | 613 &initialization_waiter, |
| 589 &initialization_retval)); | 614 &initialization_retval)); |
| 590 | 615 |
| 591 // webrtc::VideoEncoder expects this call to be synchronous. | 616 // webrtc::VideoEncoder expects this call to be synchronous. |
| 592 initialization_waiter.Wait(); | 617 initialization_waiter.Wait(); |
| 593 RecordInitEncodeUMA(initialization_retval); | 618 RecordInitEncodeUMA(initialization_retval, profile); |
| 594 return initialization_retval; | 619 return initialization_retval; |
| 595 } | 620 } |
| 596 | 621 |
| 597 int32_t RTCVideoEncoder::Encode( | 622 int32_t RTCVideoEncoder::Encode( |
| 598 const webrtc::I420VideoFrame& input_image, | 623 const webrtc::I420VideoFrame& input_image, |
| 599 const webrtc::CodecSpecificInfo* codec_specific_info, | 624 const webrtc::CodecSpecificInfo* codec_specific_info, |
| 600 const std::vector<webrtc::VideoFrameType>* frame_types) { | 625 const std::vector<webrtc::VideoFrameType>* frame_types) { |
| 601 DVLOG(3) << "Encode()"; | 626 DVLOG(3) << "Encode()"; |
| 602 if (!impl_.get()) { | 627 if (!impl_.get()) { |
| 603 DVLOG(3) << "Encode(): returning impl_status_=" << impl_status_; | 628 DVLOG(3) << "Encode(): returning impl_status_=" << impl_status_; |
| (...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 738 void RTCVideoEncoder::NotifyError(int32_t error) { | 763 void RTCVideoEncoder::NotifyError(int32_t error) { |
| 739 DCHECK(thread_checker_.CalledOnValidThread()); | 764 DCHECK(thread_checker_.CalledOnValidThread()); |
| 740 DVLOG(1) << "NotifyError(): error=" << error; | 765 DVLOG(1) << "NotifyError(): error=" << error; |
| 741 | 766 |
| 742 impl_status_ = error; | 767 impl_status_ = error; |
| 743 gpu_factories_->GetTaskRunner()->PostTask( | 768 gpu_factories_->GetTaskRunner()->PostTask( |
| 744 FROM_HERE, base::Bind(&RTCVideoEncoder::Impl::Destroy, impl_)); | 769 FROM_HERE, base::Bind(&RTCVideoEncoder::Impl::Destroy, impl_)); |
| 745 impl_ = NULL; | 770 impl_ = NULL; |
| 746 } | 771 } |
| 747 | 772 |
| 748 void RTCVideoEncoder::RecordInitEncodeUMA(int32_t init_retval) { | 773 void RTCVideoEncoder::RecordInitEncodeUMA( |
| 774 int32_t init_retval, media::VideoCodecProfile profile) { |
| 749 UMA_HISTOGRAM_BOOLEAN("Media.RTCVideoEncoderInitEncodeSuccess", | 775 UMA_HISTOGRAM_BOOLEAN("Media.RTCVideoEncoderInitEncodeSuccess", |
| 750 init_retval == WEBRTC_VIDEO_CODEC_OK); | 776 init_retval == WEBRTC_VIDEO_CODEC_OK); |
| 751 if (init_retval == WEBRTC_VIDEO_CODEC_OK) { | 777 if (init_retval == WEBRTC_VIDEO_CODEC_OK) { |
| 752 UMA_HISTOGRAM_ENUMERATION("Media.RTCVideoEncoderProfile", | 778 UMA_HISTOGRAM_ENUMERATION("Media.RTCVideoEncoderProfile", |
| 753 video_codec_profile_, | 779 profile, |
| 754 media::VIDEO_CODEC_PROFILE_MAX + 1); | 780 media::VIDEO_CODEC_PROFILE_MAX + 1); |
| 755 } | 781 } |
| 756 } | 782 } |
| 757 | 783 |
| 758 } // namespace content | 784 } // namespace content |
| OLD | NEW |