Updating XTBs based on .GRDs from branch master
[chromium-blink-merge.git] / media / cast / sender / video_encoder_impl.cc
blobd6b8e45f4cc8c9653c2459e91a6f1feb68d3f714
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"
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/sender/fake_software_video_encoder.h"
15 #include "media/cast/sender/vp8_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& reference_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<SenderEncodedFrame> encoded_frame(new SenderEncodedFrame());
47 encoder->Encode(video_frame, reference_time, encoded_frame.get());
48 environment->PostTask(
49 CastEnvironment::MAIN,
50 FROM_HERE,
51 base::Bind(frame_encoded_callback, base::Passed(&encoded_frame)));
53 } // namespace
55 // static
56 bool VideoEncoderImpl::IsSupported(const VideoSenderConfig& video_config) {
57 #ifndef OFFICIAL_BUILD
58 if (video_config.codec == CODEC_VIDEO_FAKE)
59 return true;
60 #endif
61 return video_config.codec == CODEC_VIDEO_VP8;
64 VideoEncoderImpl::VideoEncoderImpl(
65 scoped_refptr<CastEnvironment> cast_environment,
66 const VideoSenderConfig& video_config,
67 const StatusChangeCallback& status_change_cb)
68 : cast_environment_(cast_environment) {
69 CHECK(cast_environment_->HasVideoThread());
70 DCHECK(!status_change_cb.is_null());
72 if (video_config.codec == CODEC_VIDEO_VP8) {
73 encoder_.reset(new Vp8Encoder(video_config));
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 == CODEC_VIDEO_FAKE) {
81 encoder_.reset(new FakeSoftwareVideoEncoder(video_config));
82 #endif
83 } else {
84 DCHECK(false) << "Invalid config"; // Codec not supported.
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;
91 cast_environment_->PostTask(
92 CastEnvironment::MAIN,
93 FROM_HERE,
94 base::Bind(status_change_cb,
95 encoder_.get() ? STATUS_INITIALIZED :
96 STATUS_UNSUPPORTED_CODEC));
99 VideoEncoderImpl::~VideoEncoderImpl() {
100 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN));
101 if (encoder_) {
102 cast_environment_->PostTask(
103 CastEnvironment::VIDEO,
104 FROM_HERE,
105 base::Bind(&base::DeletePointer<SoftwareVideoEncoder>,
106 encoder_.release()));
110 bool VideoEncoderImpl::EncodeVideoFrame(
111 const scoped_refptr<media::VideoFrame>& video_frame,
112 const base::TimeTicks& reference_time,
113 const FrameEncodedCallback& frame_encoded_callback) {
114 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN));
115 DCHECK(!video_frame->visible_rect().IsEmpty());
116 DCHECK(!frame_encoded_callback.is_null());
118 cast_environment_->PostTask(CastEnvironment::VIDEO,
119 FROM_HERE,
120 base::Bind(&EncodeVideoFrameOnEncoderThread,
121 cast_environment_,
122 encoder_.get(),
123 video_frame,
124 reference_time,
125 dynamic_config_,
126 frame_encoded_callback));
128 dynamic_config_.key_frame_requested = false;
129 return true;
132 // Inform the encoder about the new target bit rate.
133 void VideoEncoderImpl::SetBitRate(int new_bit_rate) {
134 dynamic_config_.bit_rate = new_bit_rate;
137 // Inform the encoder to encode the next frame as a key frame.
138 void VideoEncoderImpl::GenerateKeyFrame() {
139 dynamic_config_.key_frame_requested = true;
142 // Inform the encoder to only reference frames older or equal to frame_id;
143 void VideoEncoderImpl::LatestFrameIdToReference(uint32 frame_id) {
144 dynamic_config_.latest_frame_id_to_reference = frame_id;
147 } // namespace cast
148 } // namespace media