| 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 483 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 494 << ", width=" << codec_settings->width | 494 << ", width=" << codec_settings->width |
| 495 << ", height=" << codec_settings->height | 495 << ", height=" << codec_settings->height |
| 496 << ", startBitrate=" << codec_settings->startBitrate; | 496 << ", startBitrate=" << codec_settings->startBitrate; |
| 497 DCHECK(thread_checker_.CalledOnValidThread()); | 497 DCHECK(thread_checker_.CalledOnValidThread()); |
| 498 DCHECK(!impl_); | 498 DCHECK(!impl_); |
| 499 | 499 |
| 500 weak_this_factory_.InvalidateWeakPtrs(); | 500 weak_this_factory_.InvalidateWeakPtrs(); |
| 501 impl_ = new Impl(weak_this_factory_.GetWeakPtr(), gpu_factories_); | 501 impl_ = new Impl(weak_this_factory_.GetWeakPtr(), gpu_factories_); |
| 502 base::WaitableEvent initialization_waiter(true, false); | 502 base::WaitableEvent initialization_waiter(true, false); |
| 503 int32_t initialization_retval = WEBRTC_VIDEO_CODEC_UNINITIALIZED; | 503 int32_t initialization_retval = WEBRTC_VIDEO_CODEC_UNINITIALIZED; |
| 504 gpu_factories_->GetMessageLoop()->PostTask( | 504 gpu_factories_->GetTaskRunner()->PostTask( |
| 505 FROM_HERE, | 505 FROM_HERE, |
| 506 base::Bind(&RTCVideoEncoder::Impl::CreateAndInitializeVEA, | 506 base::Bind(&RTCVideoEncoder::Impl::CreateAndInitializeVEA, |
| 507 impl_, | 507 impl_, |
| 508 gfx::Size(codec_settings->width, codec_settings->height), | 508 gfx::Size(codec_settings->width, codec_settings->height), |
| 509 codec_settings->startBitrate, | 509 codec_settings->startBitrate, |
| 510 video_codec_profile_, | 510 video_codec_profile_, |
| 511 &initialization_waiter, | 511 &initialization_waiter, |
| 512 &initialization_retval)); | 512 &initialization_retval)); |
| 513 | 513 |
| 514 // webrtc::VideoEncoder expects this call to be synchronous. | 514 // webrtc::VideoEncoder expects this call to be synchronous. |
| 515 initialization_waiter.Wait(); | 515 initialization_waiter.Wait(); |
| 516 RecordInitEncodeUMA(initialization_retval); | 516 RecordInitEncodeUMA(initialization_retval); |
| 517 return initialization_retval; | 517 return initialization_retval; |
| 518 } | 518 } |
| 519 | 519 |
| 520 int32_t RTCVideoEncoder::Encode( | 520 int32_t RTCVideoEncoder::Encode( |
| 521 const webrtc::I420VideoFrame& input_image, | 521 const webrtc::I420VideoFrame& input_image, |
| 522 const webrtc::CodecSpecificInfo* codec_specific_info, | 522 const webrtc::CodecSpecificInfo* codec_specific_info, |
| 523 const std::vector<webrtc::VideoFrameType>* frame_types) { | 523 const std::vector<webrtc::VideoFrameType>* frame_types) { |
| 524 DVLOG(3) << "Encode()"; | 524 DVLOG(3) << "Encode()"; |
| 525 // TODO(sheu): figure out why this check fails. | 525 // TODO(sheu): figure out why this check fails. |
| 526 // DCHECK(thread_checker_.CalledOnValidThread()); | 526 // DCHECK(thread_checker_.CalledOnValidThread()); |
| 527 if (!impl_) { | 527 if (!impl_) { |
| 528 DVLOG(3) << "Encode(): returning impl_status_=" << impl_status_; | 528 DVLOG(3) << "Encode(): returning impl_status_=" << impl_status_; |
| 529 return impl_status_; | 529 return impl_status_; |
| 530 } | 530 } |
| 531 | 531 |
| 532 base::WaitableEvent encode_waiter(true, false); | 532 base::WaitableEvent encode_waiter(true, false); |
| 533 int32_t encode_retval = WEBRTC_VIDEO_CODEC_UNINITIALIZED; | 533 int32_t encode_retval = WEBRTC_VIDEO_CODEC_UNINITIALIZED; |
| 534 gpu_factories_->GetMessageLoop()->PostTask( | 534 gpu_factories_->GetTaskRunner()->PostTask( |
| 535 FROM_HERE, | 535 FROM_HERE, |
| 536 base::Bind(&RTCVideoEncoder::Impl::Enqueue, | 536 base::Bind(&RTCVideoEncoder::Impl::Enqueue, |
| 537 impl_, | 537 impl_, |
| 538 &input_image, | 538 &input_image, |
| 539 (frame_types->front() == webrtc::kKeyFrame), | 539 (frame_types->front() == webrtc::kKeyFrame), |
| 540 &encode_waiter, | 540 &encode_waiter, |
| 541 &encode_retval)); | 541 &encode_retval)); |
| 542 | 542 |
| 543 // webrtc::VideoEncoder expects this call to be synchronous. | 543 // webrtc::VideoEncoder expects this call to be synchronous. |
| 544 encode_waiter.Wait(); | 544 encode_waiter.Wait(); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 560 } | 560 } |
| 561 | 561 |
| 562 int32_t RTCVideoEncoder::Release() { | 562 int32_t RTCVideoEncoder::Release() { |
| 563 DVLOG(3) << "Release()"; | 563 DVLOG(3) << "Release()"; |
| 564 DCHECK(thread_checker_.CalledOnValidThread()); | 564 DCHECK(thread_checker_.CalledOnValidThread()); |
| 565 | 565 |
| 566 // Reset the gpu_factory_, in case we reuse this encoder. | 566 // Reset the gpu_factory_, in case we reuse this encoder. |
| 567 gpu_factories_->Abort(); | 567 gpu_factories_->Abort(); |
| 568 gpu_factories_ = gpu_factories_->Clone(); | 568 gpu_factories_ = gpu_factories_->Clone(); |
| 569 if (impl_) { | 569 if (impl_) { |
| 570 gpu_factories_->GetMessageLoop()->PostTask( | 570 gpu_factories_->GetTaskRunner()->PostTask( |
| 571 FROM_HERE, base::Bind(&RTCVideoEncoder::Impl::Destroy, impl_)); | 571 FROM_HERE, base::Bind(&RTCVideoEncoder::Impl::Destroy, impl_)); |
| 572 impl_ = NULL; | 572 impl_ = NULL; |
| 573 weak_this_factory_.InvalidateWeakPtrs(); | 573 weak_this_factory_.InvalidateWeakPtrs(); |
| 574 impl_status_ = WEBRTC_VIDEO_CODEC_UNINITIALIZED; | 574 impl_status_ = WEBRTC_VIDEO_CODEC_UNINITIALIZED; |
| 575 } | 575 } |
| 576 return WEBRTC_VIDEO_CODEC_OK; | 576 return WEBRTC_VIDEO_CODEC_OK; |
| 577 } | 577 } |
| 578 | 578 |
| 579 int32_t RTCVideoEncoder::SetChannelParameters(uint32_t packet_loss, int rtt) { | 579 int32_t RTCVideoEncoder::SetChannelParameters(uint32_t packet_loss, int rtt) { |
| 580 DVLOG(3) << "SetChannelParameters(): packet_loss=" << packet_loss | 580 DVLOG(3) << "SetChannelParameters(): packet_loss=" << packet_loss |
| 581 << ", rtt=" << rtt; | 581 << ", rtt=" << rtt; |
| 582 DCHECK(thread_checker_.CalledOnValidThread()); | 582 DCHECK(thread_checker_.CalledOnValidThread()); |
| 583 // Ignored. | 583 // Ignored. |
| 584 return WEBRTC_VIDEO_CODEC_OK; | 584 return WEBRTC_VIDEO_CODEC_OK; |
| 585 } | 585 } |
| 586 | 586 |
| 587 int32_t RTCVideoEncoder::SetRates(uint32_t new_bit_rate, uint32_t frame_rate) { | 587 int32_t RTCVideoEncoder::SetRates(uint32_t new_bit_rate, uint32_t frame_rate) { |
| 588 DVLOG(3) << "SetRates(): new_bit_rate=" << new_bit_rate | 588 DVLOG(3) << "SetRates(): new_bit_rate=" << new_bit_rate |
| 589 << ", frame_rate=" << frame_rate; | 589 << ", frame_rate=" << frame_rate; |
| 590 DCHECK(thread_checker_.CalledOnValidThread()); | 590 DCHECK(thread_checker_.CalledOnValidThread()); |
| 591 if (!impl_) { | 591 if (!impl_) { |
| 592 DVLOG(3) << "SetRates(): returning " << impl_status_; | 592 DVLOG(3) << "SetRates(): returning " << impl_status_; |
| 593 return impl_status_; | 593 return impl_status_; |
| 594 } | 594 } |
| 595 | 595 |
| 596 gpu_factories_->GetMessageLoop()->PostTask( | 596 gpu_factories_->GetTaskRunner()->PostTask( |
| 597 FROM_HERE, | 597 FROM_HERE, |
| 598 base::Bind(&RTCVideoEncoder::Impl::RequestEncodingParametersChange, | 598 base::Bind(&RTCVideoEncoder::Impl::RequestEncodingParametersChange, |
| 599 impl_, | 599 impl_, |
| 600 new_bit_rate, | 600 new_bit_rate, |
| 601 frame_rate)); | 601 frame_rate)); |
| 602 return WEBRTC_VIDEO_CODEC_OK; | 602 return WEBRTC_VIDEO_CODEC_OK; |
| 603 } | 603 } |
| 604 | 604 |
| 605 void RTCVideoEncoder::ReturnEncodedImage(scoped_ptr<webrtc::EncodedImage> image, | 605 void RTCVideoEncoder::ReturnEncodedImage(scoped_ptr<webrtc::EncodedImage> image, |
| 606 int32 bitstream_buffer_id) { | 606 int32 bitstream_buffer_id) { |
| (...skipping 23 matching lines...) Expand all Loading... |
| 630 header.fragmentationTimeDiff[0] = 0; | 630 header.fragmentationTimeDiff[0] = 0; |
| 631 | 631 |
| 632 int32_t retval = encoded_image_callback_->Encoded(*image, &info, &header); | 632 int32_t retval = encoded_image_callback_->Encoded(*image, &info, &header); |
| 633 if (retval < 0) { | 633 if (retval < 0) { |
| 634 DVLOG(2) << "ReturnEncodedImage(): encoded_image_callback_ returned " | 634 DVLOG(2) << "ReturnEncodedImage(): encoded_image_callback_ returned " |
| 635 << retval; | 635 << retval; |
| 636 } | 636 } |
| 637 | 637 |
| 638 // The call through webrtc::EncodedImageCallback is synchronous, so we can | 638 // The call through webrtc::EncodedImageCallback is synchronous, so we can |
| 639 // immediately recycle the output buffer back to the Impl. | 639 // immediately recycle the output buffer back to the Impl. |
| 640 gpu_factories_->GetMessageLoop()->PostTask( | 640 gpu_factories_->GetTaskRunner()->PostTask( |
| 641 FROM_HERE, | 641 FROM_HERE, |
| 642 base::Bind(&RTCVideoEncoder::Impl::UseOutputBitstreamBufferId, | 642 base::Bind(&RTCVideoEncoder::Impl::UseOutputBitstreamBufferId, |
| 643 impl_, | 643 impl_, |
| 644 bitstream_buffer_id)); | 644 bitstream_buffer_id)); |
| 645 } | 645 } |
| 646 | 646 |
| 647 void RTCVideoEncoder::NotifyError(int32_t error) { | 647 void RTCVideoEncoder::NotifyError(int32_t error) { |
| 648 DCHECK(thread_checker_.CalledOnValidThread()); | 648 DCHECK(thread_checker_.CalledOnValidThread()); |
| 649 DVLOG(1) << "NotifyError(): error=" << error; | 649 DVLOG(1) << "NotifyError(): error=" << error; |
| 650 | 650 |
| 651 impl_status_ = error; | 651 impl_status_ = error; |
| 652 gpu_factories_->GetMessageLoop()->PostTask( | 652 gpu_factories_->GetTaskRunner()->PostTask( |
| 653 FROM_HERE, base::Bind(&RTCVideoEncoder::Impl::Destroy, impl_)); | 653 FROM_HERE, base::Bind(&RTCVideoEncoder::Impl::Destroy, impl_)); |
| 654 impl_ = NULL; | 654 impl_ = NULL; |
| 655 } | 655 } |
| 656 | 656 |
| 657 void RTCVideoEncoder::RecordInitEncodeUMA(int32_t init_retval) { | 657 void RTCVideoEncoder::RecordInitEncodeUMA(int32_t init_retval) { |
| 658 UMA_HISTOGRAM_BOOLEAN("Media.RTCVideoEncoderInitEncodeSuccess", | 658 UMA_HISTOGRAM_BOOLEAN("Media.RTCVideoEncoderInitEncodeSuccess", |
| 659 init_retval == WEBRTC_VIDEO_CODEC_OK); | 659 init_retval == WEBRTC_VIDEO_CODEC_OK); |
| 660 if (init_retval == WEBRTC_VIDEO_CODEC_OK) { | 660 if (init_retval == WEBRTC_VIDEO_CODEC_OK) { |
| 661 UMA_HISTOGRAM_ENUMERATION("Media.RTCVideoEncoderProfile", | 661 UMA_HISTOGRAM_ENUMERATION("Media.RTCVideoEncoderProfile", |
| 662 video_codec_profile_, | 662 video_codec_profile_, |
| 663 media::VIDEO_CODEC_PROFILE_MAX); | 663 media::VIDEO_CODEC_PROFILE_MAX); |
| 664 } | 664 } |
| 665 } | 665 } |
| 666 | 666 |
| 667 } // namespace content | 667 } // namespace content |
| OLD | NEW |