| 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 464 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 475 media::VideoFrame::WrapExternalPackedMemory( | 475 media::VideoFrame::WrapExternalPackedMemory( |
| 476 media::VideoFrame::I420, | 476 media::VideoFrame::I420, |
| 477 input_frame_coded_size_, | 477 input_frame_coded_size_, |
| 478 gfx::Rect(input_visible_size_), | 478 gfx::Rect(input_visible_size_), |
| 479 input_visible_size_, | 479 input_visible_size_, |
| 480 reinterpret_cast<uint8*>(input_buffer->memory()), | 480 reinterpret_cast<uint8*>(input_buffer->memory()), |
| 481 input_buffer->mapped_size(), | 481 input_buffer->mapped_size(), |
| 482 input_buffer->handle(), | 482 input_buffer->handle(), |
| 483 base::TimeDelta(), | 483 base::TimeDelta(), |
| 484 base::Bind(&RTCVideoEncoder::Impl::EncodeFrameFinished, this, index)); | 484 base::Bind(&RTCVideoEncoder::Impl::EncodeFrameFinished, this, index)); |
| 485 if (!frame) { | 485 if (!frame.get()) { |
| 486 DLOG(ERROR) << "Impl::EncodeOneFrame(): failed to create frame"; | 486 DLOG(ERROR) << "Impl::EncodeOneFrame(): failed to create frame"; |
| 487 NOTIFY_ERROR(media::VideoEncodeAccelerator::kPlatformFailureError); | 487 NOTIFY_ERROR(media::VideoEncodeAccelerator::kPlatformFailureError); |
| 488 return; | 488 return; |
| 489 } | 489 } |
| 490 | 490 |
| 491 // Do a strided copy of the input frame to match the input requirements for | 491 // Do a strided copy of the input frame to match the input requirements for |
| 492 // the encoder. | 492 // the encoder. |
| 493 // TODO(sheu): support zero-copy from WebRTC. http://crbug.com/269312 | 493 // TODO(sheu): support zero-copy from WebRTC. http://crbug.com/269312 |
| 494 media::CopyYPlane(next_frame->buffer(webrtc::kYPlane), | 494 media::CopyYPlane(next_frame->buffer(webrtc::kYPlane), |
| 495 next_frame->stride(webrtc::kYPlane), | 495 next_frame->stride(webrtc::kYPlane), |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 554 encoded_image_callback_(NULL), | 554 encoded_image_callback_(NULL), |
| 555 impl_status_(WEBRTC_VIDEO_CODEC_UNINITIALIZED), | 555 impl_status_(WEBRTC_VIDEO_CODEC_UNINITIALIZED), |
| 556 weak_factory_(this) { | 556 weak_factory_(this) { |
| 557 DVLOG(1) << "RTCVideoEncoder(): profile=" << profile; | 557 DVLOG(1) << "RTCVideoEncoder(): profile=" << profile; |
| 558 } | 558 } |
| 559 | 559 |
| 560 RTCVideoEncoder::~RTCVideoEncoder() { | 560 RTCVideoEncoder::~RTCVideoEncoder() { |
| 561 DVLOG(3) << "~RTCVideoEncoder"; | 561 DVLOG(3) << "~RTCVideoEncoder"; |
| 562 DCHECK(thread_checker_.CalledOnValidThread()); | 562 DCHECK(thread_checker_.CalledOnValidThread()); |
| 563 Release(); | 563 Release(); |
| 564 DCHECK(!impl_); | 564 DCHECK(!impl_.get()); |
| 565 } | 565 } |
| 566 | 566 |
| 567 int32_t RTCVideoEncoder::InitEncode(const webrtc::VideoCodec* codec_settings, | 567 int32_t RTCVideoEncoder::InitEncode(const webrtc::VideoCodec* codec_settings, |
| 568 int32_t number_of_cores, | 568 int32_t number_of_cores, |
| 569 uint32_t max_payload_size) { | 569 uint32_t max_payload_size) { |
| 570 DVLOG(1) << "InitEncode(): codecType=" << codec_settings->codecType | 570 DVLOG(1) << "InitEncode(): codecType=" << codec_settings->codecType |
| 571 << ", width=" << codec_settings->width | 571 << ", width=" << codec_settings->width |
| 572 << ", height=" << codec_settings->height | 572 << ", height=" << codec_settings->height |
| 573 << ", startBitrate=" << codec_settings->startBitrate; | 573 << ", startBitrate=" << codec_settings->startBitrate; |
| 574 DCHECK(thread_checker_.CalledOnValidThread()); | 574 DCHECK(thread_checker_.CalledOnValidThread()); |
| 575 DCHECK(!impl_); | 575 DCHECK(!impl_.get()); |
| 576 | 576 |
| 577 weak_factory_.InvalidateWeakPtrs(); | 577 weak_factory_.InvalidateWeakPtrs(); |
| 578 impl_ = new Impl(weak_factory_.GetWeakPtr(), gpu_factories_); | 578 impl_ = new Impl(weak_factory_.GetWeakPtr(), gpu_factories_); |
| 579 base::WaitableEvent initialization_waiter(true, false); | 579 base::WaitableEvent initialization_waiter(true, false); |
| 580 int32_t initialization_retval = WEBRTC_VIDEO_CODEC_UNINITIALIZED; | 580 int32_t initialization_retval = WEBRTC_VIDEO_CODEC_UNINITIALIZED; |
| 581 gpu_factories_->GetTaskRunner()->PostTask( | 581 gpu_factories_->GetTaskRunner()->PostTask( |
| 582 FROM_HERE, | 582 FROM_HERE, |
| 583 base::Bind(&RTCVideoEncoder::Impl::CreateAndInitializeVEA, | 583 base::Bind(&RTCVideoEncoder::Impl::CreateAndInitializeVEA, |
| 584 impl_, | 584 impl_, |
| 585 gfx::Size(codec_settings->width, codec_settings->height), | 585 gfx::Size(codec_settings->width, codec_settings->height), |
| 586 codec_settings->startBitrate, | 586 codec_settings->startBitrate, |
| 587 video_codec_profile_, | 587 video_codec_profile_, |
| 588 &initialization_waiter, | 588 &initialization_waiter, |
| 589 &initialization_retval)); | 589 &initialization_retval)); |
| 590 | 590 |
| 591 // webrtc::VideoEncoder expects this call to be synchronous. | 591 // webrtc::VideoEncoder expects this call to be synchronous. |
| 592 initialization_waiter.Wait(); | 592 initialization_waiter.Wait(); |
| 593 RecordInitEncodeUMA(initialization_retval); | 593 RecordInitEncodeUMA(initialization_retval); |
| 594 return initialization_retval; | 594 return initialization_retval; |
| 595 } | 595 } |
| 596 | 596 |
| 597 int32_t RTCVideoEncoder::Encode( | 597 int32_t RTCVideoEncoder::Encode( |
| 598 const webrtc::I420VideoFrame& input_image, | 598 const webrtc::I420VideoFrame& input_image, |
| 599 const webrtc::CodecSpecificInfo* codec_specific_info, | 599 const webrtc::CodecSpecificInfo* codec_specific_info, |
| 600 const std::vector<webrtc::VideoFrameType>* frame_types) { | 600 const std::vector<webrtc::VideoFrameType>* frame_types) { |
| 601 DVLOG(3) << "Encode()"; | 601 DVLOG(3) << "Encode()"; |
| 602 if (!impl_) { | 602 if (!impl_.get()) { |
| 603 DVLOG(3) << "Encode(): returning impl_status_=" << impl_status_; | 603 DVLOG(3) << "Encode(): returning impl_status_=" << impl_status_; |
| 604 return impl_status_; | 604 return impl_status_; |
| 605 } | 605 } |
| 606 | 606 |
| 607 bool want_key_frame = frame_types && frame_types->size() && | 607 bool want_key_frame = frame_types && frame_types->size() && |
| 608 frame_types->front() == webrtc::kKeyFrame; | 608 frame_types->front() == webrtc::kKeyFrame; |
| 609 base::WaitableEvent encode_waiter(true, false); | 609 base::WaitableEvent encode_waiter(true, false); |
| 610 int32_t encode_retval = WEBRTC_VIDEO_CODEC_UNINITIALIZED; | 610 int32_t encode_retval = WEBRTC_VIDEO_CODEC_UNINITIALIZED; |
| 611 gpu_factories_->GetTaskRunner()->PostTask( | 611 gpu_factories_->GetTaskRunner()->PostTask( |
| 612 FROM_HERE, | 612 FROM_HERE, |
| 613 base::Bind(&RTCVideoEncoder::Impl::Enqueue, | 613 base::Bind(&RTCVideoEncoder::Impl::Enqueue, |
| 614 impl_, | 614 impl_, |
| 615 &input_image, | 615 &input_image, |
| 616 want_key_frame, | 616 want_key_frame, |
| 617 &encode_waiter, | 617 &encode_waiter, |
| 618 &encode_retval)); | 618 &encode_retval)); |
| 619 | 619 |
| 620 // webrtc::VideoEncoder expects this call to be synchronous. | 620 // webrtc::VideoEncoder expects this call to be synchronous. |
| 621 encode_waiter.Wait(); | 621 encode_waiter.Wait(); |
| 622 DVLOG(3) << "Encode(): returning encode_retval=" << encode_retval; | 622 DVLOG(3) << "Encode(): returning encode_retval=" << encode_retval; |
| 623 return encode_retval; | 623 return encode_retval; |
| 624 } | 624 } |
| 625 | 625 |
| 626 int32_t RTCVideoEncoder::RegisterEncodeCompleteCallback( | 626 int32_t RTCVideoEncoder::RegisterEncodeCompleteCallback( |
| 627 webrtc::EncodedImageCallback* callback) { | 627 webrtc::EncodedImageCallback* callback) { |
| 628 DVLOG(3) << "RegisterEncodeCompleteCallback()"; | 628 DVLOG(3) << "RegisterEncodeCompleteCallback()"; |
| 629 DCHECK(thread_checker_.CalledOnValidThread()); | 629 DCHECK(thread_checker_.CalledOnValidThread()); |
| 630 if (!impl_) { | 630 if (!impl_.get()) { |
| 631 DVLOG(3) << "RegisterEncodeCompleteCallback(): returning " << impl_status_; | 631 DVLOG(3) << "RegisterEncodeCompleteCallback(): returning " << impl_status_; |
| 632 return impl_status_; | 632 return impl_status_; |
| 633 } | 633 } |
| 634 | 634 |
| 635 encoded_image_callback_ = callback; | 635 encoded_image_callback_ = callback; |
| 636 return WEBRTC_VIDEO_CODEC_OK; | 636 return WEBRTC_VIDEO_CODEC_OK; |
| 637 } | 637 } |
| 638 | 638 |
| 639 int32_t RTCVideoEncoder::Release() { | 639 int32_t RTCVideoEncoder::Release() { |
| 640 DVLOG(3) << "Release()"; | 640 DVLOG(3) << "Release()"; |
| 641 DCHECK(thread_checker_.CalledOnValidThread()); | 641 DCHECK(thread_checker_.CalledOnValidThread()); |
| 642 | 642 |
| 643 if (impl_) { | 643 if (impl_.get()) { |
| 644 gpu_factories_->GetTaskRunner()->PostTask( | 644 gpu_factories_->GetTaskRunner()->PostTask( |
| 645 FROM_HERE, base::Bind(&RTCVideoEncoder::Impl::Destroy, impl_)); | 645 FROM_HERE, base::Bind(&RTCVideoEncoder::Impl::Destroy, impl_)); |
| 646 impl_ = NULL; | 646 impl_ = NULL; |
| 647 weak_factory_.InvalidateWeakPtrs(); | 647 weak_factory_.InvalidateWeakPtrs(); |
| 648 impl_status_ = WEBRTC_VIDEO_CODEC_UNINITIALIZED; | 648 impl_status_ = WEBRTC_VIDEO_CODEC_UNINITIALIZED; |
| 649 } | 649 } |
| 650 return WEBRTC_VIDEO_CODEC_OK; | 650 return WEBRTC_VIDEO_CODEC_OK; |
| 651 } | 651 } |
| 652 | 652 |
| 653 int32_t RTCVideoEncoder::SetChannelParameters(uint32_t packet_loss, int rtt) { | 653 int32_t RTCVideoEncoder::SetChannelParameters(uint32_t packet_loss, int rtt) { |
| 654 DVLOG(3) << "SetChannelParameters(): packet_loss=" << packet_loss | 654 DVLOG(3) << "SetChannelParameters(): packet_loss=" << packet_loss |
| 655 << ", rtt=" << rtt; | 655 << ", rtt=" << rtt; |
| 656 // Ignored. | 656 // Ignored. |
| 657 return WEBRTC_VIDEO_CODEC_OK; | 657 return WEBRTC_VIDEO_CODEC_OK; |
| 658 } | 658 } |
| 659 | 659 |
| 660 int32_t RTCVideoEncoder::SetRates(uint32_t new_bit_rate, uint32_t frame_rate) { | 660 int32_t RTCVideoEncoder::SetRates(uint32_t new_bit_rate, uint32_t frame_rate) { |
| 661 DVLOG(3) << "SetRates(): new_bit_rate=" << new_bit_rate | 661 DVLOG(3) << "SetRates(): new_bit_rate=" << new_bit_rate |
| 662 << ", frame_rate=" << frame_rate; | 662 << ", frame_rate=" << frame_rate; |
| 663 if (!impl_) { | 663 if (!impl_.get()) { |
| 664 DVLOG(3) << "SetRates(): returning " << impl_status_; | 664 DVLOG(3) << "SetRates(): returning " << impl_status_; |
| 665 return impl_status_; | 665 return impl_status_; |
| 666 } | 666 } |
| 667 | 667 |
| 668 gpu_factories_->GetTaskRunner()->PostTask( | 668 gpu_factories_->GetTaskRunner()->PostTask( |
| 669 FROM_HERE, | 669 FROM_HERE, |
| 670 base::Bind(&RTCVideoEncoder::Impl::RequestEncodingParametersChange, | 670 base::Bind(&RTCVideoEncoder::Impl::RequestEncodingParametersChange, |
| 671 impl_, | 671 impl_, |
| 672 new_bit_rate, | 672 new_bit_rate, |
| 673 frame_rate)); | 673 frame_rate)); |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 749 UMA_HISTOGRAM_BOOLEAN("Media.RTCVideoEncoderInitEncodeSuccess", | 749 UMA_HISTOGRAM_BOOLEAN("Media.RTCVideoEncoderInitEncodeSuccess", |
| 750 init_retval == WEBRTC_VIDEO_CODEC_OK); | 750 init_retval == WEBRTC_VIDEO_CODEC_OK); |
| 751 if (init_retval == WEBRTC_VIDEO_CODEC_OK) { | 751 if (init_retval == WEBRTC_VIDEO_CODEC_OK) { |
| 752 UMA_HISTOGRAM_ENUMERATION("Media.RTCVideoEncoderProfile", | 752 UMA_HISTOGRAM_ENUMERATION("Media.RTCVideoEncoderProfile", |
| 753 video_codec_profile_, | 753 video_codec_profile_, |
| 754 media::VIDEO_CODEC_PROFILE_MAX + 1); | 754 media::VIDEO_CODEC_PROFILE_MAX + 1); |
| 755 } | 755 } |
| 756 } | 756 } |
| 757 | 757 |
| 758 } // namespace content | 758 } // namespace content |
| OLD | NEW |