| OLD | NEW |
| (Empty) |
| 1 // Copyright 2014 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_impl.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/bind_helpers.h" | |
| 9 #include "base/callback.h" | |
| 10 #include "base/logging.h" | |
| 11 #include "base/message_loop/message_loop.h" | |
| 12 #include "media/base/video_frame.h" | |
| 13 #include "media/cast/cast_defines.h" | |
| 14 #include "media/cast/video_sender/codecs/vp8/vp8_encoder.h" | |
| 15 #include "media/cast/video_sender/fake_software_video_encoder.h" | |
| 16 | |
| 17 namespace media { | |
| 18 namespace cast { | |
| 19 | |
| 20 namespace { | |
| 21 | |
| 22 typedef base::Callback<void(Vp8Encoder*)> PassEncoderCallback; | |
| 23 | |
| 24 void InitializeEncoderOnEncoderThread( | |
| 25 const scoped_refptr<CastEnvironment>& environment, | |
| 26 SoftwareVideoEncoder* encoder) { | |
| 27 DCHECK(environment->CurrentlyOn(CastEnvironment::VIDEO)); | |
| 28 encoder->Initialize(); | |
| 29 } | |
| 30 | |
| 31 void EncodeVideoFrameOnEncoderThread( | |
| 32 scoped_refptr<CastEnvironment> environment, | |
| 33 SoftwareVideoEncoder* encoder, | |
| 34 const scoped_refptr<media::VideoFrame>& video_frame, | |
| 35 const base::TimeTicks& capture_time, | |
| 36 const VideoEncoderImpl::CodecDynamicConfig& dynamic_config, | |
| 37 const VideoEncoderImpl::FrameEncodedCallback& frame_encoded_callback) { | |
| 38 DCHECK(environment->CurrentlyOn(CastEnvironment::VIDEO)); | |
| 39 if (dynamic_config.key_frame_requested) { | |
| 40 encoder->GenerateKeyFrame(); | |
| 41 } | |
| 42 encoder->LatestFrameIdToReference( | |
| 43 dynamic_config.latest_frame_id_to_reference); | |
| 44 encoder->UpdateRates(dynamic_config.bit_rate); | |
| 45 | |
| 46 scoped_ptr<transport::EncodedFrame> encoded_frame( | |
| 47 new transport::EncodedFrame()); | |
| 48 if (!encoder->Encode(video_frame, encoded_frame.get())) { | |
| 49 VLOG(1) << "Encoding failed"; | |
| 50 return; | |
| 51 } | |
| 52 if (encoded_frame->data.empty()) { | |
| 53 VLOG(1) << "Encoding resulted in an empty frame"; | |
| 54 return; | |
| 55 } | |
| 56 encoded_frame->rtp_timestamp = transport::GetVideoRtpTimestamp(capture_time); | |
| 57 encoded_frame->reference_time = capture_time; | |
| 58 | |
| 59 environment->PostTask( | |
| 60 CastEnvironment::MAIN, | |
| 61 FROM_HERE, | |
| 62 base::Bind( | |
| 63 frame_encoded_callback, base::Passed(&encoded_frame))); | |
| 64 } | |
| 65 } // namespace | |
| 66 | |
| 67 VideoEncoderImpl::VideoEncoderImpl( | |
| 68 scoped_refptr<CastEnvironment> cast_environment, | |
| 69 const VideoSenderConfig& video_config, | |
| 70 int max_unacked_frames) | |
| 71 : cast_environment_(cast_environment) { | |
| 72 if (video_config.codec == transport::CODEC_VIDEO_VP8) { | |
| 73 encoder_.reset(new Vp8Encoder(video_config, max_unacked_frames)); | |
| 74 cast_environment_->PostTask(CastEnvironment::VIDEO, | |
| 75 FROM_HERE, | |
| 76 base::Bind(&InitializeEncoderOnEncoderThread, | |
| 77 cast_environment, | |
| 78 encoder_.get())); | |
| 79 #ifndef OFFICIAL_BUILD | |
| 80 } else if (video_config.codec == transport::CODEC_VIDEO_FAKE) { | |
| 81 encoder_.reset(new FakeSoftwareVideoEncoder(video_config)); | |
| 82 #endif | |
| 83 } else { | |
| 84 DCHECK(false) << "Invalid config"; // Codec not supported. | |
| 85 } | |
| 86 | |
| 87 dynamic_config_.key_frame_requested = false; | |
| 88 dynamic_config_.latest_frame_id_to_reference = kStartFrameId; | |
| 89 dynamic_config_.bit_rate = video_config.start_bitrate; | |
| 90 } | |
| 91 | |
| 92 VideoEncoderImpl::~VideoEncoderImpl() { | |
| 93 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN)); | |
| 94 if (encoder_) { | |
| 95 cast_environment_->PostTask( | |
| 96 CastEnvironment::VIDEO, | |
| 97 FROM_HERE, | |
| 98 base::Bind(&base::DeletePointer<SoftwareVideoEncoder>, | |
| 99 encoder_.release())); | |
| 100 } | |
| 101 } | |
| 102 | |
| 103 bool VideoEncoderImpl::EncodeVideoFrame( | |
| 104 const scoped_refptr<media::VideoFrame>& video_frame, | |
| 105 const base::TimeTicks& capture_time, | |
| 106 const FrameEncodedCallback& frame_encoded_callback) { | |
| 107 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN)); | |
| 108 cast_environment_->PostTask(CastEnvironment::VIDEO, | |
| 109 FROM_HERE, | |
| 110 base::Bind(&EncodeVideoFrameOnEncoderThread, | |
| 111 cast_environment_, | |
| 112 encoder_.get(), | |
| 113 video_frame, | |
| 114 capture_time, | |
| 115 dynamic_config_, | |
| 116 frame_encoded_callback)); | |
| 117 | |
| 118 dynamic_config_.key_frame_requested = false; | |
| 119 return true; | |
| 120 } | |
| 121 | |
| 122 // Inform the encoder about the new target bit rate. | |
| 123 void VideoEncoderImpl::SetBitRate(int new_bit_rate) { | |
| 124 dynamic_config_.bit_rate = new_bit_rate; | |
| 125 } | |
| 126 | |
| 127 // Inform the encoder to encode the next frame as a key frame. | |
| 128 void VideoEncoderImpl::GenerateKeyFrame() { | |
| 129 dynamic_config_.key_frame_requested = true; | |
| 130 } | |
| 131 | |
| 132 // Inform the encoder to only reference frames older or equal to frame_id; | |
| 133 void VideoEncoderImpl::LatestFrameIdToReference(uint32 frame_id) { | |
| 134 dynamic_config_.latest_frame_id_to_reference = frame_id; | |
| 135 } | |
| 136 | |
| 137 } // namespace cast | |
| 138 } // namespace media | |
| OLD | NEW |