1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "media/cast/video_sender/video_encoder.h"
#include "base/bind.h"
#include "base/logging.h"
#include "base/message_loop/message_loop.h"
#include "media/base/video_frame.h"
#include "media/cast/cast_defines.h"
namespace media {
namespace cast {
void LogFrameEncodedEvent(CastEnvironment* const cast_environment,
const base::TimeTicks& capture_time) {
cast_environment->Logging()->InsertFrameEvent(kVideoFrameEncoded,
GetVideoRtpTimestamp(capture_time), kFrameIdUnknown);
}
VideoEncoder::VideoEncoder(scoped_refptr<CastEnvironment> cast_environment,
const VideoSenderConfig& video_config,
uint8 max_unacked_frames)
: video_config_(video_config),
cast_environment_(cast_environment),
skip_next_frame_(false),
skip_count_(0) {
if (video_config.codec == kVp8) {
vp8_encoder_.reset(new Vp8Encoder(video_config, max_unacked_frames));
} else {
DCHECK(false) << "Invalid config"; // Codec not supported.
}
dynamic_config_.key_frame_requested = false;
dynamic_config_.latest_frame_id_to_reference = kStartFrameId;
dynamic_config_.bit_rate = video_config.start_bitrate;
}
VideoEncoder::~VideoEncoder() {}
bool VideoEncoder::EncodeVideoFrame(
const scoped_refptr<media::VideoFrame>& video_frame,
const base::TimeTicks& capture_time,
const FrameEncodedCallback& frame_encoded_callback) {
DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN));
if (video_config_.codec != kVp8) return false;
if (skip_next_frame_) {
++skip_count_;
VLOG(1) << "Skip encoding frame";
return false;
}
cast_environment_->Logging()->InsertFrameEvent(kVideoFrameSentToEncoder,
GetVideoRtpTimestamp(capture_time), kFrameIdUnknown);
cast_environment_->PostTask(CastEnvironment::VIDEO_ENCODER, FROM_HERE,
base::Bind(&VideoEncoder::EncodeVideoFrameEncoderThread,
base::Unretained(this), video_frame, capture_time,
dynamic_config_, frame_encoded_callback));
dynamic_config_.key_frame_requested = false;
return true;
}
void VideoEncoder::EncodeVideoFrameEncoderThread(
const scoped_refptr<media::VideoFrame>& video_frame,
const base::TimeTicks& capture_time,
const CodecDynamicConfig& dynamic_config,
const FrameEncodedCallback& frame_encoded_callback) {
DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::VIDEO_ENCODER));
if (dynamic_config.key_frame_requested) {
vp8_encoder_->GenerateKeyFrame();
}
vp8_encoder_->LatestFrameIdToReference(
dynamic_config.latest_frame_id_to_reference);
vp8_encoder_->UpdateRates(dynamic_config.bit_rate);
scoped_ptr<EncodedVideoFrame> encoded_frame(new EncodedVideoFrame());
bool retval = vp8_encoder_->Encode(video_frame, encoded_frame.get());
cast_environment_->PostTask(CastEnvironment::MAIN, FROM_HERE,
base::Bind(LogFrameEncodedEvent, cast_environment_, capture_time));
if (!retval) {
VLOG(1) << "Encoding failed";
return;
}
if (encoded_frame->data.size() <= 0) {
VLOG(1) << "Encoding resulted in an empty frame";
return;
}
cast_environment_->PostTask(CastEnvironment::MAIN, FROM_HERE,
base::Bind(frame_encoded_callback,
base::Passed(&encoded_frame), capture_time));
}
// Inform the encoder about the new target bit rate.
void VideoEncoder::SetBitRate(int new_bit_rate) {
dynamic_config_.bit_rate = new_bit_rate;
}
// Inform the encoder to not encode the next frame.
void VideoEncoder::SkipNextFrame(bool skip_next_frame) {
skip_next_frame_ = skip_next_frame;
}
// Inform the encoder to encode the next frame as a key frame.
void VideoEncoder::GenerateKeyFrame() {
dynamic_config_.key_frame_requested = true;
}
// Inform the encoder to only reference frames older or equal to frame_id;
void VideoEncoder::LatestFrameIdToReference(uint32 frame_id) {
dynamic_config_.latest_frame_id_to_reference = frame_id;
}
int VideoEncoder::NumberOfSkippedFrames() const {
return skip_count_;
}
} // namespace cast
} // namespace media
|