Cast: Stop logging kVideoFrameSentToEncoder and rename a couple events.
[chromium-blink-merge.git] / media / cast / video_sender / video_encoder_impl.cc
blob2eceda30af00f893e506f03ee275d5d9c6ef65f3
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.
5 #include "media/cast/video_sender/video_encoder_impl.h"
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"
17 namespace media {
18 namespace cast {
20 namespace {
22 typedef base::Callback<void(Vp8Encoder*)> PassEncoderCallback;
24 void InitializeEncoderOnEncoderThread(
25 const scoped_refptr<CastEnvironment>& environment,
26 SoftwareVideoEncoder* encoder) {
27 DCHECK(environment->CurrentlyOn(CastEnvironment::VIDEO));
28 encoder->Initialize();
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();
42 encoder->LatestFrameIdToReference(
43 dynamic_config.latest_frame_id_to_reference);
44 encoder->UpdateRates(dynamic_config.bit_rate);
46 scoped_ptr<transport::EncodedVideoFrame> encoded_frame(
47 new transport::EncodedVideoFrame());
48 bool retval = encoder->Encode(video_frame, encoded_frame.get());
50 encoded_frame->rtp_timestamp = transport::GetVideoRtpTimestamp(capture_time);
52 if (!retval) {
53 VLOG(1) << "Encoding failed";
54 return;
56 if (encoded_frame->data.size() <= 0) {
57 VLOG(1) << "Encoding resulted in an empty frame";
58 return;
60 environment->PostTask(
61 CastEnvironment::MAIN,
62 FROM_HERE,
63 base::Bind(
64 frame_encoded_callback, base::Passed(&encoded_frame), capture_time));
66 } // namespace
68 VideoEncoderImpl::VideoEncoderImpl(
69 scoped_refptr<CastEnvironment> cast_environment,
70 const VideoSenderConfig& video_config,
71 uint8 max_unacked_frames)
72 : video_config_(video_config),
73 cast_environment_(cast_environment),
74 skip_next_frame_(false),
75 skip_count_(0) {
76 if (video_config.codec == transport::kVp8) {
77 encoder_.reset(new Vp8Encoder(video_config, max_unacked_frames));
78 cast_environment_->PostTask(CastEnvironment::VIDEO,
79 FROM_HERE,
80 base::Bind(&InitializeEncoderOnEncoderThread,
81 cast_environment,
82 encoder_.get()));
83 #ifndef OFFICIAL_BUILD
84 } else if (video_config.codec == transport::kFakeSoftwareVideo) {
85 encoder_.reset(new FakeSoftwareVideoEncoder());
86 #endif
87 } else {
88 DCHECK(false) << "Invalid config"; // Codec not supported.
91 dynamic_config_.key_frame_requested = false;
92 dynamic_config_.latest_frame_id_to_reference = kStartFrameId;
93 dynamic_config_.bit_rate = video_config.start_bitrate;
96 VideoEncoderImpl::~VideoEncoderImpl() {
97 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN));
98 if (encoder_) {
99 cast_environment_->PostTask(
100 CastEnvironment::VIDEO,
101 FROM_HERE,
102 base::Bind(&base::DeletePointer<SoftwareVideoEncoder>,
103 encoder_.release()));
107 bool VideoEncoderImpl::EncodeVideoFrame(
108 const scoped_refptr<media::VideoFrame>& video_frame,
109 const base::TimeTicks& capture_time,
110 const FrameEncodedCallback& frame_encoded_callback) {
111 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN));
112 if (skip_next_frame_) {
113 ++skip_count_;
114 return false;
117 cast_environment_->PostTask(CastEnvironment::VIDEO,
118 FROM_HERE,
119 base::Bind(&EncodeVideoFrameOnEncoderThread,
120 cast_environment_,
121 encoder_.get(),
122 video_frame,
123 capture_time,
124 dynamic_config_,
125 frame_encoded_callback));
127 dynamic_config_.key_frame_requested = false;
128 return true;
131 // Inform the encoder about the new target bit rate.
132 void VideoEncoderImpl::SetBitRate(int new_bit_rate) {
133 dynamic_config_.bit_rate = new_bit_rate;
136 // Inform the encoder to not encode the next frame.
137 void VideoEncoderImpl::SkipNextFrame(bool skip_next_frame) {
138 skip_next_frame_ = skip_next_frame;
141 // Inform the encoder to encode the next frame as a key frame.
142 void VideoEncoderImpl::GenerateKeyFrame() {
143 dynamic_config_.key_frame_requested = true;
146 // Inform the encoder to only reference frames older or equal to frame_id;
147 void VideoEncoderImpl::LatestFrameIdToReference(uint32 frame_id) {
148 dynamic_config_.latest_frame_id_to_reference = frame_id;
151 int VideoEncoderImpl::NumberOfSkippedFrames() const { return skip_count_; }
153 } // namespace cast
154 } // namespace media