blob: ae5284b9a54b4557a90625383680dfefc1469ae8 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
pwestin@webrtc.org52fd98d2012-02-13 09:03:53 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
Guo-wei Shieh2c370782015-04-08 13:00:10 -070011#include "webrtc/base/checks.h"
pbos854e84c2015-11-16 16:39:06 -080012#include "webrtc/base/logging.h"
pbosd9eec762015-11-17 06:03:43 -080013#include "webrtc/base/trace_event.h"
pbos@webrtc.orga4407322013-07-16 12:32:05 +000014#include "webrtc/engine_configurations.h"
Henrik Kjellander2557b862015-11-18 22:00:21 +010015#include "webrtc/modules/video_coding/encoded_frame.h"
16#include "webrtc/modules/video_coding/generic_encoder.h"
17#include "webrtc/modules/video_coding/media_optimization.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010018#include "webrtc/system_wrappers/include/critical_section_wrapper.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000019
20namespace webrtc {
andresp@webrtc.orgc5aeb2a2014-01-09 08:04:32 +000021namespace {
22// Map information from info into rtp. If no relevant information is found
23// in info, rtp is set to NULL.
Guo-wei Shieh2c370782015-04-08 13:00:10 -070024void CopyCodecSpecific(const CodecSpecificInfo* info, RTPVideoHeader* rtp) {
henrikg91d6ede2015-09-17 00:24:34 -070025 RTC_DCHECK(info);
andresp@webrtc.orgc5aeb2a2014-01-09 08:04:32 +000026 switch (info->codecType) {
27 case kVideoCodecVP8: {
Guo-wei Shieh2c370782015-04-08 13:00:10 -070028 rtp->codec = kRtpVideoVp8;
29 rtp->codecHeader.VP8.InitRTPVideoHeaderVP8();
30 rtp->codecHeader.VP8.pictureId = info->codecSpecific.VP8.pictureId;
31 rtp->codecHeader.VP8.nonReference =
andresp@webrtc.orgc5aeb2a2014-01-09 08:04:32 +000032 info->codecSpecific.VP8.nonReference;
Guo-wei Shieh2c370782015-04-08 13:00:10 -070033 rtp->codecHeader.VP8.temporalIdx = info->codecSpecific.VP8.temporalIdx;
34 rtp->codecHeader.VP8.layerSync = info->codecSpecific.VP8.layerSync;
35 rtp->codecHeader.VP8.tl0PicIdx = info->codecSpecific.VP8.tl0PicIdx;
36 rtp->codecHeader.VP8.keyIdx = info->codecSpecific.VP8.keyIdx;
37 rtp->simulcastIdx = info->codecSpecific.VP8.simulcastIdx;
andresp@webrtc.orgc5aeb2a2014-01-09 08:04:32 +000038 return;
39 }
asaperssona9455ab2015-07-31 06:10:09 -070040 case kVideoCodecVP9: {
41 rtp->codec = kRtpVideoVp9;
42 rtp->codecHeader.VP9.InitRTPVideoHeaderVP9();
43 rtp->codecHeader.VP9.inter_pic_predicted =
44 info->codecSpecific.VP9.inter_pic_predicted;
45 rtp->codecHeader.VP9.flexible_mode =
46 info->codecSpecific.VP9.flexible_mode;
47 rtp->codecHeader.VP9.ss_data_available =
48 info->codecSpecific.VP9.ss_data_available;
49 rtp->codecHeader.VP9.picture_id = info->codecSpecific.VP9.picture_id;
50 rtp->codecHeader.VP9.tl0_pic_idx = info->codecSpecific.VP9.tl0_pic_idx;
51 rtp->codecHeader.VP9.temporal_idx = info->codecSpecific.VP9.temporal_idx;
52 rtp->codecHeader.VP9.spatial_idx = info->codecSpecific.VP9.spatial_idx;
53 rtp->codecHeader.VP9.temporal_up_switch =
54 info->codecSpecific.VP9.temporal_up_switch;
55 rtp->codecHeader.VP9.inter_layer_predicted =
56 info->codecSpecific.VP9.inter_layer_predicted;
57 rtp->codecHeader.VP9.gof_idx = info->codecSpecific.VP9.gof_idx;
ivica7f6a6fc2015-09-08 02:40:29 -070058 rtp->codecHeader.VP9.num_spatial_layers =
59 info->codecSpecific.VP9.num_spatial_layers;
philipelcfc319b2015-11-10 07:17:23 -080060
asaperssona9455ab2015-07-31 06:10:09 -070061 if (info->codecSpecific.VP9.ss_data_available) {
asaperssona9455ab2015-07-31 06:10:09 -070062 rtp->codecHeader.VP9.spatial_layer_resolution_present =
63 info->codecSpecific.VP9.spatial_layer_resolution_present;
64 if (info->codecSpecific.VP9.spatial_layer_resolution_present) {
65 for (size_t i = 0; i < info->codecSpecific.VP9.num_spatial_layers;
66 ++i) {
67 rtp->codecHeader.VP9.width[i] = info->codecSpecific.VP9.width[i];
68 rtp->codecHeader.VP9.height[i] = info->codecSpecific.VP9.height[i];
69 }
70 }
71 rtp->codecHeader.VP9.gof.CopyGofInfoVP9(info->codecSpecific.VP9.gof);
72 }
philipelcfc319b2015-11-10 07:17:23 -080073
74 rtp->codecHeader.VP9.num_ref_pics = info->codecSpecific.VP9.num_ref_pics;
75 for (int i = 0; i < info->codecSpecific.VP9.num_ref_pics; ++i)
76 rtp->codecHeader.VP9.pid_diff[i] = info->codecSpecific.VP9.p_diff[i];
asaperssona9455ab2015-07-31 06:10:09 -070077 return;
78 }
stefan@webrtc.org2ec56062014-07-31 14:59:24 +000079 case kVideoCodecH264:
Guo-wei Shieh2c370782015-04-08 13:00:10 -070080 rtp->codec = kRtpVideoH264;
stefan@webrtc.org2ec56062014-07-31 14:59:24 +000081 return;
andresp@webrtc.orgc5aeb2a2014-01-09 08:04:32 +000082 case kVideoCodecGeneric:
Guo-wei Shieh2c370782015-04-08 13:00:10 -070083 rtp->codec = kRtpVideoGeneric;
84 rtp->simulcastIdx = info->codecSpecific.generic.simulcast_idx;
andresp@webrtc.orgc5aeb2a2014-01-09 08:04:32 +000085 return;
86 default:
andresp@webrtc.orgc5aeb2a2014-01-09 08:04:32 +000087 return;
88 }
89}
90} // namespace
niklase@google.com470e71d2011-07-07 08:21:25 +000091
92//#define DEBUG_ENCODER_BIT_STREAM
93
Peter Boström4f5db112015-10-29 16:53:59 +010094VCMGenericEncoder::VCMGenericEncoder(
95 VideoEncoder* encoder,
96 VideoEncoderRateObserver* rate_observer,
97 VCMEncodedFrameCallback* encoded_frame_callback,
98 bool internalSource)
pbos@webrtc.org891d4832015-02-26 13:15:22 +000099 : encoder_(encoder),
100 rate_observer_(rate_observer),
Peter Boström4f5db112015-10-29 16:53:59 +0100101 vcm_encoded_frame_callback_(encoded_frame_callback),
guoweis@webrtc.org54d072e2015-03-17 21:54:50 +0000102 internal_source_(internalSource),
Peter Boström4f5db112015-10-29 16:53:59 +0100103 encoder_params_({0, 0, 0, 0}),
Erik Språng2c4c9142015-06-24 11:24:44 +0200104 rotation_(kVideoRotation_0),
Peter Boström69ccb332015-10-29 16:30:23 +0100105 is_screenshare_(false) {}
niklase@google.com470e71d2011-07-07 08:21:25 +0000106
Peter Boström4f5db112015-10-29 16:53:59 +0100107VCMGenericEncoder::~VCMGenericEncoder() {}
108
109int32_t VCMGenericEncoder::Release() {
110 return encoder_->Release();
niklase@google.com470e71d2011-07-07 08:21:25 +0000111}
112
Peter Boström4f5db112015-10-29 16:53:59 +0100113int32_t VCMGenericEncoder::InitEncode(const VideoCodec* settings,
114 int32_t numberOfCores,
115 size_t maxPayloadSize) {
pbosd9eec762015-11-17 06:03:43 -0800116 TRACE_EVENT0("webrtc", "VCMGenericEncoder::InitEncode");
Peter Boström4f5db112015-10-29 16:53:59 +0100117 {
118 rtc::CritScope lock(&params_lock_);
119 encoder_params_.target_bitrate = settings->startBitrate * 1000;
120 encoder_params_.input_frame_rate = settings->maxFramerate;
121 }
tommi@webrtc.org558dc402015-03-07 20:55:56 +0000122
Peter Boström4f5db112015-10-29 16:53:59 +0100123 is_screenshare_ = settings->mode == VideoCodecMode::kScreensharing;
124 if (encoder_->InitEncode(settings, numberOfCores, maxPayloadSize) != 0) {
125 LOG(LS_ERROR) << "Failed to initialize the encoder associated with "
126 "payload name: "
127 << settings->plName;
128 return -1;
129 }
130 encoder_->RegisterEncodeCompleteCallback(vcm_encoded_frame_callback_);
131 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000132}
133
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -0700134int32_t VCMGenericEncoder::Encode(const VideoFrame& inputFrame,
135 const CodecSpecificInfo* codecSpecificInfo,
136 const std::vector<FrameType>& frameTypes) {
pbosd9eec762015-11-17 06:03:43 -0800137 TRACE_EVENT1("webrtc", "VCMGenericEncoder::Encode", "timestamp",
138 inputFrame.timestamp());
139
pbos22993e12015-10-19 02:39:06 -0700140 for (FrameType frame_type : frameTypes)
141 RTC_DCHECK(frame_type == kVideoFrameKey || frame_type == kVideoFrameDelta);
guoweis@webrtc.org54d072e2015-03-17 21:54:50 +0000142
143 rotation_ = inputFrame.rotation();
144
Peter Boström4f5db112015-10-29 16:53:59 +0100145 // Keep track of the current frame rotation and apply to the output of the
146 // encoder. There might not be exact as the encoder could have one frame delay
147 // but it should be close enough.
148 // TODO(pbos): Map from timestamp, this is racy (even if rotation_ is locked
149 // properly, which it isn't). More than one frame may be in the pipeline.
150 vcm_encoded_frame_callback_->SetRotation(rotation_);
guoweis@webrtc.org54d072e2015-03-17 21:54:50 +0000151
pbos22993e12015-10-19 02:39:06 -0700152 int32_t result = encoder_->Encode(inputFrame, codecSpecificInfo, &frameTypes);
Peter Boströmb7d9a972015-12-18 16:01:11 +0100153
154 if (vcm_encoded_frame_callback_) {
155 vcm_encoded_frame_callback_->SignalLastEncoderImplementationUsed(
156 encoder_->ImplementationName());
157 }
158
Erik Språng2c4c9142015-06-24 11:24:44 +0200159 if (is_screenshare_ &&
160 result == WEBRTC_VIDEO_CODEC_TARGET_BITRATE_OVERSHOOT) {
161 // Target bitrate exceeded, encoder state has been reset - try again.
pbos22993e12015-10-19 02:39:06 -0700162 return encoder_->Encode(inputFrame, codecSpecificInfo, &frameTypes);
Erik Språng2c4c9142015-06-24 11:24:44 +0200163 }
164
165 return result;
niklase@google.com470e71d2011-07-07 08:21:25 +0000166}
167
Peter Boström69ccb332015-10-29 16:30:23 +0100168void VCMGenericEncoder::SetEncoderParameters(const EncoderParameters& params) {
169 bool channel_parameters_have_changed;
170 bool rates_have_changed;
171 {
172 rtc::CritScope lock(&params_lock_);
173 channel_parameters_have_changed =
174 params.loss_rate != encoder_params_.loss_rate ||
175 params.rtt != encoder_params_.rtt;
176 rates_have_changed =
177 params.target_bitrate != encoder_params_.target_bitrate ||
178 params.input_frame_rate != encoder_params_.input_frame_rate;
179 encoder_params_ = params;
180 }
181 if (channel_parameters_have_changed)
182 encoder_->SetChannelParameters(params.loss_rate, params.rtt);
183 if (rates_have_changed) {
184 uint32_t target_bitrate_kbps = (params.target_bitrate + 500) / 1000;
185 encoder_->SetRates(target_bitrate_kbps, params.input_frame_rate);
186 if (rate_observer_ != nullptr) {
187 rate_observer_->OnSetRates(params.target_bitrate,
188 params.input_frame_rate);
niklase@google.com470e71d2011-07-07 08:21:25 +0000189 }
Peter Boström69ccb332015-10-29 16:30:23 +0100190 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000191}
192
Peter Boström69ccb332015-10-29 16:30:23 +0100193EncoderParameters VCMGenericEncoder::GetEncoderParameters() const {
194 rtc::CritScope lock(&params_lock_);
195 return encoder_params_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000196}
197
pbos@webrtc.org7b859cc2013-04-02 15:54:38 +0000198int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +0000199VCMGenericEncoder::SetPeriodicKeyFrames(bool enable)
200{
pbos@webrtc.org891d4832015-02-26 13:15:22 +0000201 return encoder_->SetPeriodicKeyFrames(enable);
niklase@google.com470e71d2011-07-07 08:21:25 +0000202}
203
pbos@webrtc.org7b859cc2013-04-02 15:54:38 +0000204int32_t VCMGenericEncoder::RequestFrame(
stefan@webrtc.orgcf216862012-10-25 11:29:51 +0000205 const std::vector<FrameType>& frame_types) {
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -0700206 VideoFrame image;
pbos22993e12015-10-19 02:39:06 -0700207 return encoder_->Encode(image, NULL, &frame_types);
niklase@google.com470e71d2011-07-07 08:21:25 +0000208}
209
niklase@google.com470e71d2011-07-07 08:21:25 +0000210bool
211VCMGenericEncoder::InternalSource() const
212{
tommi@webrtc.org558dc402015-03-07 20:55:56 +0000213 return internal_source_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000214}
215
jackychen61b4d512015-04-21 15:30:11 -0700216void VCMGenericEncoder::OnDroppedFrame() {
217 encoder_->OnDroppedFrame();
218}
219
Peter Boströmeb66e802015-06-05 11:08:03 +0200220bool VCMGenericEncoder::SupportsNativeHandle() const {
221 return encoder_->SupportsNativeHandle();
222}
223
jackychen6e2ce6e2015-07-13 16:26:33 -0700224int VCMGenericEncoder::GetTargetFramerate() {
225 return encoder_->GetTargetFramerate();
226}
227
niklase@google.com470e71d2011-07-07 08:21:25 +0000228 /***************************
229 * Callback Implementation
230 ***************************/
andresp@webrtc.org1df9dc32014-01-09 08:01:57 +0000231VCMEncodedFrameCallback::VCMEncodedFrameCallback(
guoweis@webrtc.org54d072e2015-03-17 21:54:50 +0000232 EncodedImageCallback* post_encode_callback)
Peter Boströmb7d9a972015-12-18 16:01:11 +0100233 : send_callback_(),
guoweis@webrtc.org54d072e2015-03-17 21:54:50 +0000234 _mediaOpt(NULL),
235 _payloadType(0),
236 _internalSource(false),
237 _rotation(kVideoRotation_0),
238 post_encode_callback_(post_encode_callback)
tommi@webrtc.orga9da4c52012-07-20 11:17:23 +0000239#ifdef DEBUG_ENCODER_BIT_STREAM
guoweis@webrtc.org54d072e2015-03-17 21:54:50 +0000240 ,
241 _bitStreamAfterEncoder(NULL)
tommi@webrtc.orga9da4c52012-07-20 11:17:23 +0000242#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000243{
244#ifdef DEBUG_ENCODER_BIT_STREAM
245 _bitStreamAfterEncoder = fopen("encoderBitStream.bit", "wb");
246#endif
247}
248
249VCMEncodedFrameCallback::~VCMEncodedFrameCallback()
250{
251#ifdef DEBUG_ENCODER_BIT_STREAM
252 fclose(_bitStreamAfterEncoder);
253#endif
254}
255
pbos@webrtc.org7b859cc2013-04-02 15:54:38 +0000256int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +0000257VCMEncodedFrameCallback::SetTransportCallback(VCMPacketizationCallback* transport)
258{
Peter Boströmb7d9a972015-12-18 16:01:11 +0100259 send_callback_ = transport;
niklase@google.com470e71d2011-07-07 08:21:25 +0000260 return VCM_OK;
261}
262
changbin.shao@webrtc.orgf31f56d2015-02-09 09:14:03 +0000263int32_t VCMEncodedFrameCallback::Encoded(
Peter Boströmb7d9a972015-12-18 16:01:11 +0100264 const EncodedImage& encoded_image,
niklase@google.com470e71d2011-07-07 08:21:25 +0000265 const CodecSpecificInfo* codecSpecificInfo,
changbin.shao@webrtc.orgf31f56d2015-02-09 09:14:03 +0000266 const RTPFragmentationHeader* fragmentationHeader) {
pbosd9eec762015-11-17 06:03:43 -0800267 TRACE_EVENT_INSTANT1("webrtc", "VCMEncodedFrameCallback::Encoded",
Peter Boströmb7d9a972015-12-18 16:01:11 +0100268 "timestamp", encoded_image._timeStamp);
269 post_encode_callback_->Encoded(encoded_image, NULL, NULL);
andresp@webrtc.org1df9dc32014-01-09 08:01:57 +0000270
Peter Boströmb7d9a972015-12-18 16:01:11 +0100271 if (send_callback_ == NULL) {
changbin.shao@webrtc.orgf31f56d2015-02-09 09:14:03 +0000272 return VCM_UNINITIALIZED;
273 }
274
wjia@webrtc.orgf0cd3942011-10-25 00:40:43 +0000275#ifdef DEBUG_ENCODER_BIT_STREAM
changbin.shao@webrtc.orgf31f56d2015-02-09 09:14:03 +0000276 if (_bitStreamAfterEncoder != NULL) {
Peter Boströmb7d9a972015-12-18 16:01:11 +0100277 fwrite(encoded_image._buffer, 1, encoded_image._length,
changbin.shao@webrtc.orgf31f56d2015-02-09 09:14:03 +0000278 _bitStreamAfterEncoder);
279 }
wjia@webrtc.orgf0cd3942011-10-25 00:40:43 +0000280#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000281
changbin.shao@webrtc.orgf31f56d2015-02-09 09:14:03 +0000282 RTPVideoHeader rtpVideoHeader;
glaznev@webrtc.org30540fe2015-02-18 20:30:03 +0000283 memset(&rtpVideoHeader, 0, sizeof(RTPVideoHeader));
changbin.shao@webrtc.orgf31f56d2015-02-09 09:14:03 +0000284 RTPVideoHeader* rtpVideoHeaderPtr = &rtpVideoHeader;
Guo-wei Shieh2c370782015-04-08 13:00:10 -0700285 if (codecSpecificInfo) {
286 CopyCodecSpecific(codecSpecificInfo, rtpVideoHeaderPtr);
287 }
guoweis@webrtc.org54d072e2015-03-17 21:54:50 +0000288 rtpVideoHeader.rotation = _rotation;
niklase@google.com470e71d2011-07-07 08:21:25 +0000289
Peter Boströmb7d9a972015-12-18 16:01:11 +0100290 int32_t callbackReturn = send_callback_->SendData(
291 _payloadType, encoded_image, *fragmentationHeader, rtpVideoHeaderPtr);
changbin.shao@webrtc.orgf31f56d2015-02-09 09:14:03 +0000292 if (callbackReturn < 0) {
293 return callbackReturn;
294 }
295
296 if (_mediaOpt != NULL) {
Peter Boströmb7d9a972015-12-18 16:01:11 +0100297 _mediaOpt->UpdateWithEncodedData(encoded_image);
changbin.shao@webrtc.orgf31f56d2015-02-09 09:14:03 +0000298 if (_internalSource)
299 return _mediaOpt->DropFrame(); // Signal to encoder to drop next frame.
300 }
301 return VCM_OK;
niklase@google.com470e71d2011-07-07 08:21:25 +0000302}
303
Peter Boströmb7d9a972015-12-18 16:01:11 +0100304void VCMEncodedFrameCallback::SetMediaOpt(
305 media_optimization::MediaOptimization* mediaOpt) {
306 _mediaOpt = mediaOpt;
307}
308
309void VCMEncodedFrameCallback::SignalLastEncoderImplementationUsed(
310 const char* implementation_name) {
311 if (send_callback_)
312 send_callback_->OnEncoderImplementationName(implementation_name);
niklase@google.com470e71d2011-07-07 08:21:25 +0000313}
314
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000315} // namespace webrtc