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 #include "media/cast/sender/vp8_encoder.h"
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
,
51 base::Bind(frame_encoded_callback
, base::Passed(&encoded_frame
)));
56 bool VideoEncoderImpl::IsSupported(const VideoSenderConfig
& video_config
) {
57 #ifndef OFFICIAL_BUILD
58 if (video_config
.codec
== CODEC_VIDEO_FAKE
)
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
,
76 base::Bind(&InitializeEncoderOnEncoderThread
,
79 #ifndef OFFICIAL_BUILD
80 } else if (video_config
.codec
== CODEC_VIDEO_FAKE
) {
81 encoder_
.reset(new FakeSoftwareVideoEncoder(video_config
));
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
,
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
));
102 cast_environment_
->PostTask(
103 CastEnvironment::VIDEO
,
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
,
120 base::Bind(&EncodeVideoFrameOnEncoderThread
,
126 frame_encoded_callback
));
128 dynamic_config_
.key_frame_requested
= false;
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
;