| 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 "media/cast/video_sender/video_encoder.h" | 5 #include "media/cast/video_sender/video_encoder.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
| 10 #include "media/base/video_frame.h" | 10 #include "media/base/video_frame.h" |
| 11 #include "media/cast/cast_defines.h" | 11 #include "media/cast/cast_defines.h" |
| 12 #include "media/cast/video_sender/video_encoder_impl.h" |
| 12 | 13 |
| 13 namespace media { | 14 namespace media { |
| 14 namespace cast { | 15 namespace cast { |
| 15 | 16 |
| 16 void LogFrameEncodedEvent(CastEnvironment* const cast_environment, | 17 void LogFrameEncodedEvent(CastEnvironment* const cast_environment, |
| 17 const base::TimeTicks& capture_time) { | 18 const base::TimeTicks& capture_time) { |
| 18 cast_environment->Logging()->InsertFrameEvent(kVideoFrameEncoded, | 19 cast_environment->Logging()->InsertFrameEvent(kVideoFrameEncoded, |
| 19 GetVideoRtpTimestamp(capture_time), kFrameIdUnknown); | 20 GetVideoRtpTimestamp(capture_time), kFrameIdUnknown); |
| 20 } | 21 } |
| 21 | 22 |
| 22 VideoEncoder::VideoEncoder(scoped_refptr<CastEnvironment> cast_environment, | 23 VideoEncoderImpl::VideoEncoderImpl( |
| 23 const VideoSenderConfig& video_config, | 24 scoped_refptr<CastEnvironment> cast_environment, |
| 24 uint8 max_unacked_frames) | 25 const VideoSenderConfig& video_config, |
| 26 uint8 max_unacked_frames) |
| 25 : video_config_(video_config), | 27 : video_config_(video_config), |
| 26 cast_environment_(cast_environment), | 28 cast_environment_(cast_environment), |
| 27 skip_next_frame_(false), | 29 skip_next_frame_(false), |
| 28 skip_count_(0) { | 30 skip_count_(0) { |
| 29 if (video_config.codec == kVp8) { | 31 if (video_config.codec == kVp8) { |
| 30 vp8_encoder_.reset(new Vp8Encoder(video_config, max_unacked_frames)); | 32 vp8_encoder_.reset(new Vp8Encoder(video_config, max_unacked_frames)); |
| 31 } else { | 33 } else { |
| 32 DCHECK(false) << "Invalid config"; // Codec not supported. | 34 DCHECK(false) << "Invalid config"; // Codec not supported. |
| 33 } | 35 } |
| 34 | 36 |
| 35 dynamic_config_.key_frame_requested = false; | 37 dynamic_config_.key_frame_requested = false; |
| 36 dynamic_config_.latest_frame_id_to_reference = kStartFrameId; | 38 dynamic_config_.latest_frame_id_to_reference = kStartFrameId; |
| 37 dynamic_config_.bit_rate = video_config.start_bitrate; | 39 dynamic_config_.bit_rate = video_config.start_bitrate; |
| 38 } | 40 } |
| 39 | 41 |
| 40 VideoEncoder::~VideoEncoder() {} | 42 VideoEncoderImpl::~VideoEncoderImpl() {} |
| 41 | 43 |
| 42 bool VideoEncoder::EncodeVideoFrame( | 44 bool VideoEncoderImpl::EncodeVideoFrame( |
| 43 const scoped_refptr<media::VideoFrame>& video_frame, | 45 const scoped_refptr<media::VideoFrame>& video_frame, |
| 44 const base::TimeTicks& capture_time, | 46 const base::TimeTicks& capture_time, |
| 45 const FrameEncodedCallback& frame_encoded_callback) { | 47 const FrameEncodedCallback& frame_encoded_callback) { |
| 46 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN)); | 48 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN)); |
| 47 if (video_config_.codec != kVp8) return false; | 49 if (video_config_.codec != kVp8) return false; |
| 48 | 50 |
| 49 if (skip_next_frame_) { | 51 if (skip_next_frame_) { |
| 50 ++skip_count_; | 52 ++skip_count_; |
| 51 VLOG(1) << "Skip encoding frame"; | 53 VLOG(1) << "Skip encoding frame"; |
| 52 return false; | 54 return false; |
| 53 } | 55 } |
| 54 | 56 |
| 55 cast_environment_->Logging()->InsertFrameEvent(kVideoFrameSentToEncoder, | 57 cast_environment_->Logging()->InsertFrameEvent(kVideoFrameSentToEncoder, |
| 56 GetVideoRtpTimestamp(capture_time), kFrameIdUnknown); | 58 GetVideoRtpTimestamp(capture_time), kFrameIdUnknown); |
| 57 cast_environment_->PostTask(CastEnvironment::VIDEO_ENCODER, FROM_HERE, | 59 cast_environment_->PostTask(CastEnvironment::VIDEO_ENCODER, FROM_HERE, |
| 58 base::Bind(&VideoEncoder::EncodeVideoFrameEncoderThread, | 60 base::Bind(&VideoEncoderImpl::EncodeVideoFrameEncoderThread, |
| 59 base::Unretained(this), video_frame, capture_time, | 61 base::Unretained(this), video_frame, capture_time, |
| 60 dynamic_config_, frame_encoded_callback)); | 62 dynamic_config_, frame_encoded_callback)); |
| 61 | 63 |
| 62 dynamic_config_.key_frame_requested = false; | 64 dynamic_config_.key_frame_requested = false; |
| 63 return true; | 65 return true; |
| 64 } | 66 } |
| 65 | 67 |
| 66 void VideoEncoder::EncodeVideoFrameEncoderThread( | 68 void VideoEncoderImpl::EncodeVideoFrameEncoderThread( |
| 67 const scoped_refptr<media::VideoFrame>& video_frame, | 69 const scoped_refptr<media::VideoFrame>& video_frame, |
| 68 const base::TimeTicks& capture_time, | 70 const base::TimeTicks& capture_time, |
| 69 const CodecDynamicConfig& dynamic_config, | 71 const CodecDynamicConfig& dynamic_config, |
| 70 const FrameEncodedCallback& frame_encoded_callback) { | 72 const FrameEncodedCallback& frame_encoded_callback) { |
| 71 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::VIDEO_ENCODER)); | 73 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::VIDEO_ENCODER)); |
| 72 if (dynamic_config.key_frame_requested) { | 74 if (dynamic_config.key_frame_requested) { |
| 73 vp8_encoder_->GenerateKeyFrame(); | 75 vp8_encoder_->GenerateKeyFrame(); |
| 74 } | 76 } |
| 75 vp8_encoder_->LatestFrameIdToReference( | 77 vp8_encoder_->LatestFrameIdToReference( |
| 76 dynamic_config.latest_frame_id_to_reference); | 78 dynamic_config.latest_frame_id_to_reference); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 89 if (encoded_frame->data.size() <= 0) { | 91 if (encoded_frame->data.size() <= 0) { |
| 90 VLOG(1) << "Encoding resulted in an empty frame"; | 92 VLOG(1) << "Encoding resulted in an empty frame"; |
| 91 return; | 93 return; |
| 92 } | 94 } |
| 93 cast_environment_->PostTask(CastEnvironment::MAIN, FROM_HERE, | 95 cast_environment_->PostTask(CastEnvironment::MAIN, FROM_HERE, |
| 94 base::Bind(frame_encoded_callback, | 96 base::Bind(frame_encoded_callback, |
| 95 base::Passed(&encoded_frame), capture_time)); | 97 base::Passed(&encoded_frame), capture_time)); |
| 96 } | 98 } |
| 97 | 99 |
| 98 // Inform the encoder about the new target bit rate. | 100 // Inform the encoder about the new target bit rate. |
| 99 void VideoEncoder::SetBitRate(int new_bit_rate) { | 101 void VideoEncoderImpl::SetBitRate(int new_bit_rate) { |
| 100 dynamic_config_.bit_rate = new_bit_rate; | 102 dynamic_config_.bit_rate = new_bit_rate; |
| 101 } | 103 } |
| 102 | 104 |
| 103 // Inform the encoder to not encode the next frame. | 105 // Inform the encoder to not encode the next frame. |
| 104 void VideoEncoder::SkipNextFrame(bool skip_next_frame) { | 106 void VideoEncoderImpl::SkipNextFrame(bool skip_next_frame) { |
| 105 skip_next_frame_ = skip_next_frame; | 107 skip_next_frame_ = skip_next_frame; |
| 106 } | 108 } |
| 107 | 109 |
| 108 // Inform the encoder to encode the next frame as a key frame. | 110 // Inform the encoder to encode the next frame as a key frame. |
| 109 void VideoEncoder::GenerateKeyFrame() { | 111 void VideoEncoderImpl::GenerateKeyFrame() { |
| 110 dynamic_config_.key_frame_requested = true; | 112 dynamic_config_.key_frame_requested = true; |
| 111 } | 113 } |
| 112 | 114 |
| 113 // Inform the encoder to only reference frames older or equal to frame_id; | 115 // Inform the encoder to only reference frames older or equal to frame_id; |
| 114 void VideoEncoder::LatestFrameIdToReference(uint32 frame_id) { | 116 void VideoEncoderImpl::LatestFrameIdToReference(uint32 frame_id) { |
| 115 dynamic_config_.latest_frame_id_to_reference = frame_id; | 117 dynamic_config_.latest_frame_id_to_reference = frame_id; |
| 116 } | 118 } |
| 117 | 119 |
| 118 int VideoEncoder::NumberOfSkippedFrames() const { | 120 int VideoEncoderImpl::NumberOfSkippedFrames() const { |
| 119 return skip_count_; | 121 return skip_count_; |
| 120 } | 122 } |
| 121 | 123 |
| 122 } // namespace cast | 124 } // namespace cast |
| 123 } // namespace media | 125 } // namespace media |
| OLD | NEW |