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/sender/video_encoder_impl.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/sender/fake_software_video_encoder.h"
15 #if !defined(MEDIA_DISABLE_LIBVPX)
16 #include "media/cast/sender/vp8_encoder.h"
17 #endif // !defined(MEDIA_DISABLE_LIBVPX)
24 #if !defined(MEDIA_DISABLE_LIBVPX)
25 typedef base::Callback<void(Vp8Encoder*)> PassEncoderCallback;
27 void InitializeEncoderOnEncoderThread(
28 const scoped_refptr<CastEnvironment>& environment,
29 SoftwareVideoEncoder* encoder) {
30 DCHECK(environment->CurrentlyOn(CastEnvironment::VIDEO));
31 encoder->Initialize();
33 #endif // !defined(MEDIA_DISABLE_LIBVPX)
35 void EncodeVideoFrameOnEncoderThread(
36 scoped_refptr<CastEnvironment> environment,
37 SoftwareVideoEncoder* encoder,
38 const scoped_refptr<media::VideoFrame>& video_frame,
39 const base::TimeTicks& capture_time,
40 const VideoEncoderImpl::CodecDynamicConfig& dynamic_config,
41 const VideoEncoderImpl::FrameEncodedCallback& frame_encoded_callback) {
42 DCHECK(environment->CurrentlyOn(CastEnvironment::VIDEO));
43 if (dynamic_config.key_frame_requested) {
44 encoder->GenerateKeyFrame();
46 encoder->LatestFrameIdToReference(
47 dynamic_config.latest_frame_id_to_reference);
48 encoder->UpdateRates(dynamic_config.bit_rate);
50 scoped_ptr<EncodedFrame> encoded_frame(
52 if (!encoder->Encode(video_frame, encoded_frame.get())) {
53 VLOG(1) << "Encoding failed";
56 if (encoded_frame->data.empty()) {
57 VLOG(1) << "Encoding resulted in an empty frame";
60 encoded_frame->rtp_timestamp = GetVideoRtpTimestamp(capture_time);
61 encoded_frame->reference_time = capture_time;
63 environment->PostTask(
64 CastEnvironment::MAIN,
67 frame_encoded_callback, base::Passed(&encoded_frame)));
71 VideoEncoderImpl::VideoEncoderImpl(
72 scoped_refptr<CastEnvironment> cast_environment,
73 const VideoSenderConfig& video_config,
74 int max_unacked_frames)
75 : cast_environment_(cast_environment) {
76 if (video_config.codec == CODEC_VIDEO_VP8) {
77 #if !defined(MEDIA_DISABLE_LIBVPX)
78 encoder_.reset(new Vp8Encoder(video_config, max_unacked_frames));
79 cast_environment_->PostTask(CastEnvironment::VIDEO,
81 base::Bind(&InitializeEncoderOnEncoderThread,
84 #endif // !defined(MEDIA_DISABLE_LIBVPX)
85 #ifndef OFFICIAL_BUILD
86 } else if (video_config.codec == CODEC_VIDEO_FAKE) {
87 encoder_.reset(new FakeSoftwareVideoEncoder(video_config));
90 DCHECK(false) << "Invalid config"; // Codec not supported.
93 dynamic_config_.key_frame_requested = false;
94 dynamic_config_.latest_frame_id_to_reference = kStartFrameId;
95 dynamic_config_.bit_rate = video_config.start_bitrate;
98 VideoEncoderImpl::~VideoEncoderImpl() {
99 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN));
101 cast_environment_->PostTask(
102 CastEnvironment::VIDEO,
104 base::Bind(&base::DeletePointer<SoftwareVideoEncoder>,
105 encoder_.release()));
109 bool VideoEncoderImpl::EncodeVideoFrame(
110 const scoped_refptr<media::VideoFrame>& video_frame,
111 const base::TimeTicks& capture_time,
112 const FrameEncodedCallback& frame_encoded_callback) {
113 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN));
114 cast_environment_->PostTask(CastEnvironment::VIDEO,
116 base::Bind(&EncodeVideoFrameOnEncoderThread,
122 frame_encoded_callback));
124 dynamic_config_.key_frame_requested = false;
128 // Inform the encoder about the new target bit rate.
129 void VideoEncoderImpl::SetBitRate(int new_bit_rate) {
130 dynamic_config_.bit_rate = new_bit_rate;
133 // Inform the encoder to encode the next frame as a key frame.
134 void VideoEncoderImpl::GenerateKeyFrame() {
135 dynamic_config_.key_frame_requested = true;
138 // Inform the encoder to only reference frames older or equal to frame_id;
139 void VideoEncoderImpl::LatestFrameIdToReference(uint32 frame_id) {
140 dynamic_config_.latest_frame_id_to_reference = frame_id;