| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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_recorder/video_track_recorder.h" | 5 #include "content/renderer/media_recorder/video_track_recorder.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 421 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 432 | 432 |
| 433 void UseOutputBitstreamBufferId(int32_t bitstream_buffer_id); | 433 void UseOutputBitstreamBufferId(int32_t bitstream_buffer_id); |
| 434 void FrameFinished(std::unique_ptr<base::SharedMemory> shm); | 434 void FrameFinished(std::unique_ptr<base::SharedMemory> shm); |
| 435 | 435 |
| 436 // VideoTrackRecorder::Encoder implementation. | 436 // VideoTrackRecorder::Encoder implementation. |
| 437 ~VEAEncoder() override; | 437 ~VEAEncoder() override; |
| 438 void EncodeOnEncodingTaskRunner(scoped_refptr<VideoFrame> frame, | 438 void EncodeOnEncodingTaskRunner(scoped_refptr<VideoFrame> frame, |
| 439 base::TimeTicks capture_timestamp) override; | 439 base::TimeTicks capture_timestamp) override; |
| 440 void ConfigureEncoderOnEncodingTaskRunner(const gfx::Size& size) override; | 440 void ConfigureEncoderOnEncodingTaskRunner(const gfx::Size& size) override; |
| 441 | 441 |
| 442 void DestroyOnEncodingTaskRunner(base::WaitableEvent* async_waiter); |
| 443 |
| 442 media::GpuVideoAcceleratorFactories* const gpu_factories_; | 444 media::GpuVideoAcceleratorFactories* const gpu_factories_; |
| 443 | 445 |
| 444 const media::VideoCodecProfile codec_; | 446 const media::VideoCodecProfile codec_; |
| 445 | 447 |
| 446 // The underlying VEA to perform encoding on. | 448 // The underlying VEA to perform encoding on. |
| 447 std::unique_ptr<media::VideoEncodeAccelerator> video_encoder_; | 449 std::unique_ptr<media::VideoEncodeAccelerator> video_encoder_; |
| 448 | 450 |
| 449 // Shared memory buffers for output with the VEA. | 451 // Shared memory buffers for output with the VEA. |
| 450 std::vector<std::unique_ptr<base::SharedMemory>> output_buffers_; | 452 std::vector<std::unique_ptr<base::SharedMemory>> output_buffers_; |
| 451 | 453 |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 568 DCHECK(gpu_factories_); | 570 DCHECK(gpu_factories_); |
| 569 DCHECK_GE(size.width(), kVEAEncoderMinResolutionWidth); | 571 DCHECK_GE(size.width(), kVEAEncoderMinResolutionWidth); |
| 570 DCHECK_GE(size.height(), kVEAEncoderMinResolutionHeight); | 572 DCHECK_GE(size.height(), kVEAEncoderMinResolutionHeight); |
| 571 | 573 |
| 572 encoding_task_runner_->PostTask( | 574 encoding_task_runner_->PostTask( |
| 573 FROM_HERE, base::Bind(&VEAEncoder::ConfigureEncoderOnEncodingTaskRunner, | 575 FROM_HERE, base::Bind(&VEAEncoder::ConfigureEncoderOnEncodingTaskRunner, |
| 574 this, size)); | 576 this, size)); |
| 575 } | 577 } |
| 576 | 578 |
| 577 VEAEncoder::~VEAEncoder() { | 579 VEAEncoder::~VEAEncoder() { |
| 580 base::WaitableEvent release_waiter( |
| 581 base::WaitableEvent::ResetPolicy::MANUAL, |
| 582 base::WaitableEvent::InitialState::NOT_SIGNALED); |
| 583 // base::Unretained is safe because the class will be alive until |
| 584 // |release_waiter| is signaled. |
| 585 // TODO(emircan): Consider refactoring media::VideoEncodeAccelerator to avoid |
| 586 // using naked pointers and using DeleteSoon() here, see |
| 587 // http://crbug.com/701627. |
| 588 // It is currently unsafe because |video_encoder_| might be in use on another |
| 589 // function on |encoding_task_runner_|, see http://crbug.com/701030. |
| 578 encoding_task_runner_->PostTask( | 590 encoding_task_runner_->PostTask( |
| 579 FROM_HERE, base::Bind(&media::VideoEncodeAccelerator::Destroy, | 591 FROM_HERE, base::Bind(&VEAEncoder::DestroyOnEncodingTaskRunner, |
| 580 base::Unretained(video_encoder_.release()))); | 592 base::Unretained(this), &release_waiter)); |
| 593 release_waiter.Wait(); |
| 581 } | 594 } |
| 582 | 595 |
| 583 void VEAEncoder::RequireBitstreamBuffers(unsigned int /*input_count*/, | 596 void VEAEncoder::RequireBitstreamBuffers(unsigned int /*input_count*/, |
| 584 const gfx::Size& input_coded_size, | 597 const gfx::Size& input_coded_size, |
| 585 size_t output_buffer_size) { | 598 size_t output_buffer_size) { |
| 586 DVLOG(3) << __func__; | 599 DVLOG(3) << __func__; |
| 587 DCHECK(encoding_task_runner_->BelongsToCurrentThread()); | 600 DCHECK(encoding_task_runner_->BelongsToCurrentThread()); |
| 588 | 601 |
| 589 vea_requested_input_size_ = input_coded_size; | 602 vea_requested_input_size_ = input_coded_size; |
| 590 output_buffers_.clear(); | 603 output_buffers_.clear(); |
| (...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 725 video_frame->stride(media::VideoFrame::kYPlane), | 738 video_frame->stride(media::VideoFrame::kYPlane), |
| 726 video_frame->visible_data(media::VideoFrame::kUPlane), | 739 video_frame->visible_data(media::VideoFrame::kUPlane), |
| 727 video_frame->stride(media::VideoFrame::kUPlane), | 740 video_frame->stride(media::VideoFrame::kUPlane), |
| 728 video_frame->visible_data(media::VideoFrame::kVPlane), | 741 video_frame->visible_data(media::VideoFrame::kVPlane), |
| 729 video_frame->stride(media::VideoFrame::kVPlane), | 742 video_frame->stride(media::VideoFrame::kVPlane), |
| 730 input_size_.width(), input_size_.height()); | 743 input_size_.width(), input_size_.height()); |
| 731 } | 744 } |
| 732 frames_in_encode_.push(std::make_pair( | 745 frames_in_encode_.push(std::make_pair( |
| 733 media::WebmMuxer::VideoParameters(frame), capture_timestamp)); | 746 media::WebmMuxer::VideoParameters(frame), capture_timestamp)); |
| 734 | 747 |
| 735 encoding_task_runner_->PostTask( | 748 video_encoder_->Encode(video_frame, false); |
| 736 FROM_HERE, | |
| 737 base::Bind(&media::VideoEncodeAccelerator::Encode, | |
| 738 base::Unretained(video_encoder_.get()), video_frame, false)); | |
| 739 } | 749 } |
| 740 | 750 |
| 741 void VEAEncoder::ConfigureEncoderOnEncodingTaskRunner(const gfx::Size& size) { | 751 void VEAEncoder::ConfigureEncoderOnEncodingTaskRunner(const gfx::Size& size) { |
| 742 DVLOG(3) << __func__; | 752 DVLOG(3) << __func__; |
| 743 DCHECK(encoding_task_runner_->BelongsToCurrentThread()); | 753 DCHECK(encoding_task_runner_->BelongsToCurrentThread()); |
| 744 DCHECK(gpu_factories_->GetTaskRunner()->BelongsToCurrentThread()); | 754 DCHECK(gpu_factories_->GetTaskRunner()->BelongsToCurrentThread()); |
| 745 DCHECK_GT(bits_per_second_, 0); | 755 DCHECK_GT(bits_per_second_, 0); |
| 746 | 756 |
| 747 input_size_ = size; | 757 input_size_ = size; |
| 748 video_encoder_ = gpu_factories_->CreateVideoEncodeAccelerator(); | 758 video_encoder_ = gpu_factories_->CreateVideoEncodeAccelerator(); |
| 749 if (!video_encoder_ || | 759 if (!video_encoder_ || |
| 750 !video_encoder_->Initialize(media::PIXEL_FORMAT_I420, input_size_, codec_, | 760 !video_encoder_->Initialize(media::PIXEL_FORMAT_I420, input_size_, codec_, |
| 751 bits_per_second_, this)) { | 761 bits_per_second_, this)) { |
| 752 NotifyError(media::VideoEncodeAccelerator::kPlatformFailureError); | 762 NotifyError(media::VideoEncodeAccelerator::kPlatformFailureError); |
| 753 } | 763 } |
| 754 } | 764 } |
| 755 | 765 |
| 766 void VEAEncoder::DestroyOnEncodingTaskRunner( |
| 767 base::WaitableEvent* async_waiter) { |
| 768 DCHECK(encoding_task_runner_->BelongsToCurrentThread()); |
| 769 video_encoder_.reset(); |
| 770 async_waiter->Signal(); |
| 771 } |
| 772 |
| 756 // static | 773 // static |
| 757 void VpxEncoder::ShutdownEncoder(std::unique_ptr<base::Thread> encoding_thread, | 774 void VpxEncoder::ShutdownEncoder(std::unique_ptr<base::Thread> encoding_thread, |
| 758 ScopedVpxCodecCtxPtr encoder) { | 775 ScopedVpxCodecCtxPtr encoder) { |
| 759 DCHECK(encoding_thread->IsRunning()); | 776 DCHECK(encoding_thread->IsRunning()); |
| 760 encoding_thread->Stop(); | 777 encoding_thread->Stop(); |
| 761 // Both |encoding_thread| and |encoder| will be destroyed at end-of-scope. | 778 // Both |encoding_thread| and |encoder| will be destroyed at end-of-scope. |
| 762 } | 779 } |
| 763 | 780 |
| 764 VpxEncoder::VpxEncoder( | 781 VpxEncoder::VpxEncoder( |
| 765 bool use_vp9, | 782 bool use_vp9, |
| (...skipping 452 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1218 encoder_->SetPaused(paused_before_init_); | 1235 encoder_->SetPaused(paused_before_init_); |
| 1219 | 1236 |
| 1220 // StartFrameEncode() will be called on Render IO thread. | 1237 // StartFrameEncode() will be called on Render IO thread. |
| 1221 MediaStreamVideoSink::ConnectToTrack( | 1238 MediaStreamVideoSink::ConnectToTrack( |
| 1222 track_, | 1239 track_, |
| 1223 base::Bind(&VideoTrackRecorder::Encoder::StartFrameEncode, encoder_), | 1240 base::Bind(&VideoTrackRecorder::Encoder::StartFrameEncode, encoder_), |
| 1224 false); | 1241 false); |
| 1225 } | 1242 } |
| 1226 | 1243 |
| 1227 } // namespace content | 1244 } // namespace content |
| OLD | NEW |