blob: c8180f35a29663267fcb7e41fcbc5f2595be010c [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);
Erik SprĂ¥ng2c4c9142015-06-24 11:24:44 +0200153 if (is_screenshare_ &&
154 result == WEBRTC_VIDEO_CODEC_TARGET_BITRATE_OVERSHOOT) {
155 // Target bitrate exceeded, encoder state has been reset - try again.
pbos22993e12015-10-19 02:39:06 -0700156 return encoder_->Encode(inputFrame, codecSpecificInfo, &frameTypes);
Erik SprĂ¥ng2c4c9142015-06-24 11:24:44 +0200157 }
158
159 return result;
niklase@google.com470e71d2011-07-07 08:21:25 +0000160}
161
Peter Boström69ccb332015-10-29 16:30:23 +0100162void VCMGenericEncoder::SetEncoderParameters(const EncoderParameters& params) {
163 bool channel_parameters_have_changed;
164 bool rates_have_changed;
165 {
166 rtc::CritScope lock(&params_lock_);
167 channel_parameters_have_changed =
168 params.loss_rate != encoder_params_.loss_rate ||
169 params.rtt != encoder_params_.rtt;
170 rates_have_changed =
171 params.target_bitrate != encoder_params_.target_bitrate ||
172 params.input_frame_rate != encoder_params_.input_frame_rate;
173 encoder_params_ = params;
174 }
175 if (channel_parameters_have_changed)
176 encoder_->SetChannelParameters(params.loss_rate, params.rtt);
177 if (rates_have_changed) {
178 uint32_t target_bitrate_kbps = (params.target_bitrate + 500) / 1000;
179 encoder_->SetRates(target_bitrate_kbps, params.input_frame_rate);
180 if (rate_observer_ != nullptr) {
181 rate_observer_->OnSetRates(params.target_bitrate,
182 params.input_frame_rate);
niklase@google.com470e71d2011-07-07 08:21:25 +0000183 }
Peter Boström69ccb332015-10-29 16:30:23 +0100184 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000185}
186
Peter Boström69ccb332015-10-29 16:30:23 +0100187EncoderParameters VCMGenericEncoder::GetEncoderParameters() const {
188 rtc::CritScope lock(&params_lock_);
189 return encoder_params_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000190}
191
pbos@webrtc.org7b859cc2013-04-02 15:54:38 +0000192int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +0000193VCMGenericEncoder::SetPeriodicKeyFrames(bool enable)
194{
pbos@webrtc.org891d4832015-02-26 13:15:22 +0000195 return encoder_->SetPeriodicKeyFrames(enable);
niklase@google.com470e71d2011-07-07 08:21:25 +0000196}
197
pbos@webrtc.org7b859cc2013-04-02 15:54:38 +0000198int32_t VCMGenericEncoder::RequestFrame(
stefan@webrtc.orgcf216862012-10-25 11:29:51 +0000199 const std::vector<FrameType>& frame_types) {
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -0700200 VideoFrame image;
pbos22993e12015-10-19 02:39:06 -0700201 return encoder_->Encode(image, NULL, &frame_types);
niklase@google.com470e71d2011-07-07 08:21:25 +0000202}
203
niklase@google.com470e71d2011-07-07 08:21:25 +0000204bool
205VCMGenericEncoder::InternalSource() const
206{
tommi@webrtc.org558dc402015-03-07 20:55:56 +0000207 return internal_source_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000208}
209
jackychen61b4d512015-04-21 15:30:11 -0700210void VCMGenericEncoder::OnDroppedFrame() {
211 encoder_->OnDroppedFrame();
212}
213
Peter Boströmeb66e802015-06-05 11:08:03 +0200214bool VCMGenericEncoder::SupportsNativeHandle() const {
215 return encoder_->SupportsNativeHandle();
216}
217
jackychen6e2ce6e2015-07-13 16:26:33 -0700218int VCMGenericEncoder::GetTargetFramerate() {
219 return encoder_->GetTargetFramerate();
220}
221
niklase@google.com470e71d2011-07-07 08:21:25 +0000222 /***************************
223 * Callback Implementation
224 ***************************/
andresp@webrtc.org1df9dc32014-01-09 08:01:57 +0000225VCMEncodedFrameCallback::VCMEncodedFrameCallback(
guoweis@webrtc.org54d072e2015-03-17 21:54:50 +0000226 EncodedImageCallback* post_encode_callback)
227 : _sendCallback(),
228 _mediaOpt(NULL),
229 _payloadType(0),
230 _internalSource(false),
231 _rotation(kVideoRotation_0),
232 post_encode_callback_(post_encode_callback)
tommi@webrtc.orga9da4c52012-07-20 11:17:23 +0000233#ifdef DEBUG_ENCODER_BIT_STREAM
guoweis@webrtc.org54d072e2015-03-17 21:54:50 +0000234 ,
235 _bitStreamAfterEncoder(NULL)
tommi@webrtc.orga9da4c52012-07-20 11:17:23 +0000236#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000237{
238#ifdef DEBUG_ENCODER_BIT_STREAM
239 _bitStreamAfterEncoder = fopen("encoderBitStream.bit", "wb");
240#endif
241}
242
243VCMEncodedFrameCallback::~VCMEncodedFrameCallback()
244{
245#ifdef DEBUG_ENCODER_BIT_STREAM
246 fclose(_bitStreamAfterEncoder);
247#endif
248}
249
pbos@webrtc.org7b859cc2013-04-02 15:54:38 +0000250int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +0000251VCMEncodedFrameCallback::SetTransportCallback(VCMPacketizationCallback* transport)
252{
253 _sendCallback = transport;
254 return VCM_OK;
255}
256
changbin.shao@webrtc.orgf31f56d2015-02-09 09:14:03 +0000257int32_t VCMEncodedFrameCallback::Encoded(
258 const EncodedImage& encodedImage,
niklase@google.com470e71d2011-07-07 08:21:25 +0000259 const CodecSpecificInfo* codecSpecificInfo,
changbin.shao@webrtc.orgf31f56d2015-02-09 09:14:03 +0000260 const RTPFragmentationHeader* fragmentationHeader) {
pbosd9eec762015-11-17 06:03:43 -0800261 TRACE_EVENT_INSTANT1("webrtc", "VCMEncodedFrameCallback::Encoded",
262 "timestamp", encodedImage._timeStamp);
pbos22993e12015-10-19 02:39:06 -0700263 RTC_DCHECK(encodedImage._frameType == kVideoFrameKey ||
264 encodedImage._frameType == kVideoFrameDelta);
changbin.shao@webrtc.orgf31f56d2015-02-09 09:14:03 +0000265 post_encode_callback_->Encoded(encodedImage, NULL, NULL);
andresp@webrtc.org1df9dc32014-01-09 08:01:57 +0000266
changbin.shao@webrtc.orgf31f56d2015-02-09 09:14:03 +0000267 if (_sendCallback == NULL) {
268 return VCM_UNINITIALIZED;
269 }
270
wjia@webrtc.orgf0cd3942011-10-25 00:40:43 +0000271#ifdef DEBUG_ENCODER_BIT_STREAM
changbin.shao@webrtc.orgf31f56d2015-02-09 09:14:03 +0000272 if (_bitStreamAfterEncoder != NULL) {
273 fwrite(encodedImage._buffer, 1, encodedImage._length,
274 _bitStreamAfterEncoder);
275 }
wjia@webrtc.orgf0cd3942011-10-25 00:40:43 +0000276#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000277
changbin.shao@webrtc.orgf31f56d2015-02-09 09:14:03 +0000278 RTPVideoHeader rtpVideoHeader;
glaznev@webrtc.org30540fe2015-02-18 20:30:03 +0000279 memset(&rtpVideoHeader, 0, sizeof(RTPVideoHeader));
changbin.shao@webrtc.orgf31f56d2015-02-09 09:14:03 +0000280 RTPVideoHeader* rtpVideoHeaderPtr = &rtpVideoHeader;
Guo-wei Shieh2c370782015-04-08 13:00:10 -0700281 if (codecSpecificInfo) {
282 CopyCodecSpecific(codecSpecificInfo, rtpVideoHeaderPtr);
283 }
guoweis@webrtc.org54d072e2015-03-17 21:54:50 +0000284 rtpVideoHeader.rotation = _rotation;
niklase@google.com470e71d2011-07-07 08:21:25 +0000285
changbin.shao@webrtc.orgf31f56d2015-02-09 09:14:03 +0000286 int32_t callbackReturn = _sendCallback->SendData(
287 _payloadType, encodedImage, *fragmentationHeader, rtpVideoHeaderPtr);
288 if (callbackReturn < 0) {
289 return callbackReturn;
290 }
291
292 if (_mediaOpt != NULL) {
293 _mediaOpt->UpdateWithEncodedData(encodedImage);
294 if (_internalSource)
295 return _mediaOpt->DropFrame(); // Signal to encoder to drop next frame.
296 }
297 return VCM_OK;
niklase@google.com470e71d2011-07-07 08:21:25 +0000298}
299
niklase@google.com470e71d2011-07-07 08:21:25 +0000300void
stefan@webrtc.orga64300a2013-03-04 15:24:40 +0000301VCMEncodedFrameCallback::SetMediaOpt(
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +0000302 media_optimization::MediaOptimization *mediaOpt)
niklase@google.com470e71d2011-07-07 08:21:25 +0000303{
304 _mediaOpt = mediaOpt;
305}
306
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000307} // namespace webrtc