| 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/pepper/video_encoder_shim.h" | 5 #include "content/renderer/pepper/video_encoder_shim.h" |
| 6 | 6 |
| 7 #include <inttypes.h> | 7 #include <inttypes.h> |
| 8 | 8 |
| 9 #include <deque> | 9 #include <deque> |
| 10 | 10 |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 92 } // namespace | 92 } // namespace |
| 93 | 93 |
| 94 class VideoEncoderShim::EncoderImpl { | 94 class VideoEncoderShim::EncoderImpl { |
| 95 public: | 95 public: |
| 96 explicit EncoderImpl(const base::WeakPtr<VideoEncoderShim>& shim); | 96 explicit EncoderImpl(const base::WeakPtr<VideoEncoderShim>& shim); |
| 97 ~EncoderImpl(); | 97 ~EncoderImpl(); |
| 98 | 98 |
| 99 void Initialize(media::VideoPixelFormat input_format, | 99 void Initialize(media::VideoPixelFormat input_format, |
| 100 const gfx::Size& input_visible_size, | 100 const gfx::Size& input_visible_size, |
| 101 media::VideoCodecProfile output_profile, | 101 media::VideoCodecProfile output_profile, |
| 102 uint32 initial_bitrate); | 102 uint32_t initial_bitrate); |
| 103 void Encode(const scoped_refptr<media::VideoFrame>& frame, | 103 void Encode(const scoped_refptr<media::VideoFrame>& frame, |
| 104 bool force_keyframe); | 104 bool force_keyframe); |
| 105 void UseOutputBitstreamBuffer(const media::BitstreamBuffer& buffer, | 105 void UseOutputBitstreamBuffer(const media::BitstreamBuffer& buffer, |
| 106 uint8_t* mem); | 106 uint8_t* mem); |
| 107 void RequestEncodingParametersChange(uint32 bitrate, uint32 framerate); | 107 void RequestEncodingParametersChange(uint32_t bitrate, uint32_t framerate); |
| 108 void Stop(); | 108 void Stop(); |
| 109 | 109 |
| 110 private: | 110 private: |
| 111 struct PendingEncode { | 111 struct PendingEncode { |
| 112 PendingEncode(const scoped_refptr<media::VideoFrame>& frame, | 112 PendingEncode(const scoped_refptr<media::VideoFrame>& frame, |
| 113 bool force_keyframe) | 113 bool force_keyframe) |
| 114 : frame(frame), force_keyframe(force_keyframe) {} | 114 : frame(frame), force_keyframe(force_keyframe) {} |
| 115 ~PendingEncode() {} | 115 ~PendingEncode() {} |
| 116 | 116 |
| 117 scoped_refptr<media::VideoFrame> frame; | 117 scoped_refptr<media::VideoFrame> frame; |
| (...skipping 14 matching lines...) Expand all Loading... |
| 132 | 132 |
| 133 base::WeakPtr<VideoEncoderShim> shim_; | 133 base::WeakPtr<VideoEncoderShim> shim_; |
| 134 scoped_refptr<base::SingleThreadTaskRunner> renderer_task_runner_; | 134 scoped_refptr<base::SingleThreadTaskRunner> renderer_task_runner_; |
| 135 | 135 |
| 136 bool initialized_; | 136 bool initialized_; |
| 137 | 137 |
| 138 // Libvpx internal objects. Only valid if |initialized_| is true. | 138 // Libvpx internal objects. Only valid if |initialized_| is true. |
| 139 vpx_codec_enc_cfg_t config_; | 139 vpx_codec_enc_cfg_t config_; |
| 140 vpx_codec_ctx_t encoder_; | 140 vpx_codec_ctx_t encoder_; |
| 141 | 141 |
| 142 uint32 framerate_; | 142 uint32_t framerate_; |
| 143 | 143 |
| 144 std::deque<PendingEncode> frames_; | 144 std::deque<PendingEncode> frames_; |
| 145 std::deque<BitstreamBuffer> buffers_; | 145 std::deque<BitstreamBuffer> buffers_; |
| 146 }; | 146 }; |
| 147 | 147 |
| 148 VideoEncoderShim::EncoderImpl::EncoderImpl( | 148 VideoEncoderShim::EncoderImpl::EncoderImpl( |
| 149 const base::WeakPtr<VideoEncoderShim>& shim) | 149 const base::WeakPtr<VideoEncoderShim>& shim) |
| 150 : shim_(shim), | 150 : shim_(shim), |
| 151 renderer_task_runner_(base::ThreadTaskRunnerHandle::Get()), | 151 renderer_task_runner_(base::ThreadTaskRunnerHandle::Get()), |
| 152 initialized_(false) { | 152 initialized_(false) { |
| 153 } | 153 } |
| 154 | 154 |
| 155 VideoEncoderShim::EncoderImpl::~EncoderImpl() { | 155 VideoEncoderShim::EncoderImpl::~EncoderImpl() { |
| 156 if (initialized_) | 156 if (initialized_) |
| 157 vpx_codec_destroy(&encoder_); | 157 vpx_codec_destroy(&encoder_); |
| 158 } | 158 } |
| 159 | 159 |
| 160 void VideoEncoderShim::EncoderImpl::Initialize( | 160 void VideoEncoderShim::EncoderImpl::Initialize( |
| 161 media::VideoPixelFormat input_format, | 161 media::VideoPixelFormat input_format, |
| 162 const gfx::Size& input_visible_size, | 162 const gfx::Size& input_visible_size, |
| 163 media::VideoCodecProfile output_profile, | 163 media::VideoCodecProfile output_profile, |
| 164 uint32 initial_bitrate) { | 164 uint32_t initial_bitrate) { |
| 165 gfx::Size coded_size = | 165 gfx::Size coded_size = |
| 166 media::VideoFrame::PlaneSize(input_format, 0, input_visible_size); | 166 media::VideoFrame::PlaneSize(input_format, 0, input_visible_size); |
| 167 | 167 |
| 168 vpx_codec_iface_t* vpx_codec; | 168 vpx_codec_iface_t* vpx_codec; |
| 169 int32_t min_quantizer, max_quantizer, cpu_used; | 169 int32_t min_quantizer, max_quantizer, cpu_used; |
| 170 GetVpxCodecParameters(output_profile, &vpx_codec, &min_quantizer, | 170 GetVpxCodecParameters(output_profile, &vpx_codec, &min_quantizer, |
| 171 &max_quantizer, &cpu_used); | 171 &max_quantizer, &cpu_used); |
| 172 | 172 |
| 173 // Populate encoder configuration with default values. | 173 // Populate encoder configuration with default values. |
| 174 if (vpx_codec_enc_config_default(vpx_codec, &config_, 0) != VPX_CODEC_OK) { | 174 if (vpx_codec_enc_config_default(vpx_codec, &config_, 0) != VPX_CODEC_OK) { |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 245 } | 245 } |
| 246 | 246 |
| 247 void VideoEncoderShim::EncoderImpl::UseOutputBitstreamBuffer( | 247 void VideoEncoderShim::EncoderImpl::UseOutputBitstreamBuffer( |
| 248 const media::BitstreamBuffer& buffer, | 248 const media::BitstreamBuffer& buffer, |
| 249 uint8_t* mem) { | 249 uint8_t* mem) { |
| 250 buffers_.push_back(BitstreamBuffer(buffer, mem)); | 250 buffers_.push_back(BitstreamBuffer(buffer, mem)); |
| 251 DoEncode(); | 251 DoEncode(); |
| 252 } | 252 } |
| 253 | 253 |
| 254 void VideoEncoderShim::EncoderImpl::RequestEncodingParametersChange( | 254 void VideoEncoderShim::EncoderImpl::RequestEncodingParametersChange( |
| 255 uint32 bitrate, | 255 uint32_t bitrate, |
| 256 uint32 framerate) { | 256 uint32_t framerate) { |
| 257 framerate_ = framerate; | 257 framerate_ = framerate; |
| 258 | 258 |
| 259 uint32 bitrate_kbit = bitrate / 1000; | 259 uint32_t bitrate_kbit = bitrate / 1000; |
| 260 if (config_.rc_target_bitrate == bitrate_kbit) | 260 if (config_.rc_target_bitrate == bitrate_kbit) |
| 261 return; | 261 return; |
| 262 | 262 |
| 263 config_.rc_target_bitrate = bitrate_kbit; | 263 config_.rc_target_bitrate = bitrate_kbit; |
| 264 if (vpx_codec_enc_config_set(&encoder_, &config_) != VPX_CODEC_OK) | 264 if (vpx_codec_enc_config_set(&encoder_, &config_) != VPX_CODEC_OK) |
| 265 NotifyError(media::VideoEncodeAccelerator::kPlatformFailureError); | 265 NotifyError(media::VideoEncodeAccelerator::kPlatformFailureError); |
| 266 } | 266 } |
| 267 | 267 |
| 268 void VideoEncoderShim::EncoderImpl::Stop() { | 268 void VideoEncoderShim::EncoderImpl::Stop() { |
| 269 // Release frames on the renderer thread. | 269 // Release frames on the renderer thread. |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 396 profiles.push_back(profile); | 396 profiles.push_back(profile); |
| 397 } | 397 } |
| 398 | 398 |
| 399 return profiles; | 399 return profiles; |
| 400 } | 400 } |
| 401 | 401 |
| 402 bool VideoEncoderShim::Initialize( | 402 bool VideoEncoderShim::Initialize( |
| 403 media::VideoPixelFormat input_format, | 403 media::VideoPixelFormat input_format, |
| 404 const gfx::Size& input_visible_size, | 404 const gfx::Size& input_visible_size, |
| 405 media::VideoCodecProfile output_profile, | 405 media::VideoCodecProfile output_profile, |
| 406 uint32 initial_bitrate, | 406 uint32_t initial_bitrate, |
| 407 media::VideoEncodeAccelerator::Client* client) { | 407 media::VideoEncodeAccelerator::Client* client) { |
| 408 DCHECK(RenderThreadImpl::current()); | 408 DCHECK(RenderThreadImpl::current()); |
| 409 DCHECK_EQ(client, host_); | 409 DCHECK_EQ(client, host_); |
| 410 | 410 |
| 411 if (input_format != media::PIXEL_FORMAT_I420) | 411 if (input_format != media::PIXEL_FORMAT_I420) |
| 412 return false; | 412 return false; |
| 413 | 413 |
| 414 if (output_profile != media::VP8PROFILE_ANY && | 414 if (output_profile != media::VP8PROFILE_ANY && |
| 415 output_profile != media::VP9PROFILE_ANY) | 415 output_profile != media::VP9PROFILE_ANY) |
| 416 return false; | 416 return false; |
| (...skipping 21 matching lines...) Expand all Loading... |
| 438 const media::BitstreamBuffer& buffer) { | 438 const media::BitstreamBuffer& buffer) { |
| 439 DCHECK(RenderThreadImpl::current()); | 439 DCHECK(RenderThreadImpl::current()); |
| 440 | 440 |
| 441 media_task_runner_->PostTask( | 441 media_task_runner_->PostTask( |
| 442 FROM_HERE, | 442 FROM_HERE, |
| 443 base::Bind(&VideoEncoderShim::EncoderImpl::UseOutputBitstreamBuffer, | 443 base::Bind(&VideoEncoderShim::EncoderImpl::UseOutputBitstreamBuffer, |
| 444 base::Unretained(encoder_impl_.get()), buffer, | 444 base::Unretained(encoder_impl_.get()), buffer, |
| 445 host_->ShmHandleToAddress(buffer.id()))); | 445 host_->ShmHandleToAddress(buffer.id()))); |
| 446 } | 446 } |
| 447 | 447 |
| 448 void VideoEncoderShim::RequestEncodingParametersChange(uint32 bitrate, | 448 void VideoEncoderShim::RequestEncodingParametersChange(uint32_t bitrate, |
| 449 uint32 framerate) { | 449 uint32_t framerate) { |
| 450 DCHECK(RenderThreadImpl::current()); | 450 DCHECK(RenderThreadImpl::current()); |
| 451 | 451 |
| 452 media_task_runner_->PostTask( | 452 media_task_runner_->PostTask( |
| 453 FROM_HERE, | 453 FROM_HERE, |
| 454 base::Bind( | 454 base::Bind( |
| 455 &VideoEncoderShim::EncoderImpl::RequestEncodingParametersChange, | 455 &VideoEncoderShim::EncoderImpl::RequestEncodingParametersChange, |
| 456 base::Unretained(encoder_impl_.get()), bitrate, framerate)); | 456 base::Unretained(encoder_impl_.get()), bitrate, framerate)); |
| 457 } | 457 } |
| 458 | 458 |
| 459 void VideoEncoderShim::Destroy() { | 459 void VideoEncoderShim::Destroy() { |
| 460 DCHECK(RenderThreadImpl::current()); | 460 DCHECK(RenderThreadImpl::current()); |
| 461 | 461 |
| 462 delete this; | 462 delete this; |
| 463 } | 463 } |
| 464 | 464 |
| 465 void VideoEncoderShim::OnRequireBitstreamBuffers( | 465 void VideoEncoderShim::OnRequireBitstreamBuffers( |
| 466 unsigned int input_count, | 466 unsigned int input_count, |
| 467 const gfx::Size& input_coded_size, | 467 const gfx::Size& input_coded_size, |
| 468 size_t output_buffer_size) { | 468 size_t output_buffer_size) { |
| 469 DCHECK(RenderThreadImpl::current()); | 469 DCHECK(RenderThreadImpl::current()); |
| 470 | 470 |
| 471 host_->RequireBitstreamBuffers(input_count, input_coded_size, | 471 host_->RequireBitstreamBuffers(input_count, input_coded_size, |
| 472 output_buffer_size); | 472 output_buffer_size); |
| 473 } | 473 } |
| 474 | 474 |
| 475 void VideoEncoderShim::OnBitstreamBufferReady( | 475 void VideoEncoderShim::OnBitstreamBufferReady( |
| 476 scoped_refptr<media::VideoFrame> frame, | 476 scoped_refptr<media::VideoFrame> frame, |
| 477 int32 bitstream_buffer_id, | 477 int32_t bitstream_buffer_id, |
| 478 size_t payload_size, | 478 size_t payload_size, |
| 479 bool key_frame) { | 479 bool key_frame) { |
| 480 DCHECK(RenderThreadImpl::current()); | 480 DCHECK(RenderThreadImpl::current()); |
| 481 | 481 |
| 482 host_->BitstreamBufferReady(bitstream_buffer_id, payload_size, key_frame); | 482 host_->BitstreamBufferReady(bitstream_buffer_id, payload_size, key_frame); |
| 483 } | 483 } |
| 484 | 484 |
| 485 void VideoEncoderShim::OnNotifyError( | 485 void VideoEncoderShim::OnNotifyError( |
| 486 media::VideoEncodeAccelerator::Error error) { | 486 media::VideoEncodeAccelerator::Error error) { |
| 487 DCHECK(RenderThreadImpl::current()); | 487 DCHECK(RenderThreadImpl::current()); |
| 488 | 488 |
| 489 host_->NotifyError(error); | 489 host_->NotifyError(error); |
| 490 } | 490 } |
| 491 | 491 |
| 492 } // namespace content | 492 } // namespace content |
| OLD | NEW |