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