blob: ec20ad95fdede4ff6a26737542bdd3f98e8da05e [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
stefan@webrtc.org07b45a52012-02-02 08:37:48 +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
Peter Boström7623ce42015-12-09 12:13:30 +010011#include "webrtc/video/vie_encoder.h"
mflodman@webrtc.org84d17832011-12-01 17:02:23 +000012
pbos@webrtc.org12dc1a32013-08-05 16:22:53 +000013#include <assert.h>
14
stefan@webrtc.orgc3cc3752013-06-04 09:36:56 +000015#include <algorithm>
niklase@google.com470e71d2011-07-07 08:21:25 +000016
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +000017#include "webrtc/base/checks.h"
Peter Boström415d2cd2015-10-26 11:35:17 +010018#include "webrtc/base/logging.h"
tommie4f96502015-10-20 23:00:48 -070019#include "webrtc/base/trace_event.h"
Henrik Kjellander0b9e29c2015-11-16 11:12:24 +010020#include "webrtc/modules/pacing/paced_sender.h"
Henrik Kjellander2557b862015-11-18 22:00:21 +010021#include "webrtc/modules/video_coding/include/video_coding.h"
22#include "webrtc/modules/video_coding/include/video_coding_defines.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010023#include "webrtc/system_wrappers/include/metrics.h"
24#include "webrtc/system_wrappers/include/tick_util.h"
Peter Boströme4499152016-02-05 11:13:28 +010025#include "webrtc/video/overuse_frame_detector.h"
pbos@webrtc.org273a4142014-12-01 15:23:21 +000026#include "webrtc/video/send_statistics_proxy.h"
Peter Boströme4499152016-02-05 11:13:28 +010027#include "webrtc/video_frame.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000028
niklase@google.com470e71d2011-07-07 08:21:25 +000029namespace webrtc {
30
stefan@webrtc.org3e005052013-10-18 15:05:29 +000031static const float kStopPaddingThresholdMs = 2000;
mflodmanc4a1c372015-11-06 04:33:51 -080032
stefan@webrtc.org439be292012-02-16 14:45:37 +000033class QMVideoSettingsCallback : public VCMQMSettingsCallback {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +000034 public:
mflodmana8565422015-12-07 01:09:52 -080035 explicit QMVideoSettingsCallback(VideoProcessing* vpm);
mflodman@webrtc.org6879c8a2013-07-23 11:35:00 +000036
stefan@webrtc.org439be292012-02-16 14:45:37 +000037 ~QMVideoSettingsCallback();
niklase@google.com470e71d2011-07-07 08:21:25 +000038
mflodman@webrtc.org84d17832011-12-01 17:02:23 +000039 // Update VPM with QM (quality modes: frame size & frame rate) settings.
pbos@webrtc.orgb238d122013-04-09 13:41:51 +000040 int32_t SetVideoQMSettings(const uint32_t frame_rate,
41 const uint32_t width,
42 const uint32_t height);
niklase@google.com470e71d2011-07-07 08:21:25 +000043
mflodman@webrtc.org84d17832011-12-01 17:02:23 +000044 private:
mflodmana8565422015-12-07 01:09:52 -080045 VideoProcessing* vp_;
mflodman@webrtc.org84d17832011-12-01 17:02:23 +000046};
niklase@google.com470e71d2011-07-07 08:21:25 +000047
mflodman0dbf0092015-10-19 08:12:12 -070048ViEEncoder::ViEEncoder(uint32_t number_of_cores,
Peter Boström7083e112015-09-22 16:28:51 +020049 ProcessThread* module_process_thread,
50 SendStatisticsProxy* stats_proxy,
Per28a44562016-05-04 17:12:51 +020051 OveruseFrameDetector* overuse_detector)
mflodman0dbf0092015-10-19 08:12:12 -070052 : number_of_cores_(number_of_cores),
mflodmana8565422015-12-07 01:09:52 -080053 vp_(VideoProcessing::Create()),
54 qm_callback_(new QMVideoSettingsCallback(vp_.get())),
perkj376b1922016-05-02 11:35:24 -070055 video_sender_(Clock::GetRealTimeClock(),
56 this,
57 this,
58 qm_callback_.get(),
59 this),
Peter Boström7083e112015-09-22 16:28:51 +020060 stats_proxy_(stats_proxy),
Peter Boströme4499152016-02-05 11:13:28 +010061 overuse_detector_(overuse_detector),
Noah Richards099323e2015-04-15 09:14:12 -070062 time_of_last_frame_activity_ms_(0),
Peter Boströmd153a372015-11-10 15:27:12 +000063 encoder_config_(),
Peter Boströma03785e2016-03-03 11:36:18 +010064 min_transmit_bitrate_bps_(0),
pbos@webrtc.org143451d2015-03-18 14:40:03 +000065 last_observed_bitrate_bps_(0),
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000066 network_is_transmitting_(true),
perkjbc75d972016-05-02 06:31:25 -070067 encoder_paused_(true),
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000068 encoder_paused_and_dropped_frame_(false),
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000069 module_process_thread_(module_process_thread),
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000070 has_received_sli_(false),
71 picture_id_sli_(0),
72 has_received_rpsi_(false),
73 picture_id_rpsi_(0),
asaperssondec5ebf2015-10-05 02:36:17 -070074 video_suspended_(false) {
Peter Boströmcd5c25c2016-04-21 16:48:08 +020075 module_process_thread_->RegisterModule(&video_sender_);
mflodmana8565422015-12-07 01:09:52 -080076 vp_->EnableTemporalDecimation(true);
mflodman@webrtc.org84d17832011-12-01 17:02:23 +000077
78 // Enable/disable content analysis: off by default for now.
mflodmana8565422015-12-07 01:09:52 -080079 vp_->EnableContentAnalysis(false);
niklase@google.com470e71d2011-07-07 08:21:25 +000080}
81
Peter Boströmcd5c25c2016-04-21 16:48:08 +020082vcm::VideoSender* ViEEncoder::video_sender() {
83 return &video_sender_;
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +000084}
85
mflodman@webrtc.org84d17832011-12-01 17:02:23 +000086ViEEncoder::~ViEEncoder() {
Peter Boströmcd5c25c2016-04-21 16:48:08 +020087 module_process_thread_->DeRegisterModule(&video_sender_);
asapersson@webrtc.org96dc6852014-11-03 14:40:38 +000088}
89
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +000090void ViEEncoder::SetNetworkTransmissionState(bool is_transmitting) {
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +000091 {
Tommi97888bd2016-01-21 23:24:59 +010092 rtc::CritScope lock(&data_cs_);
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +000093 network_is_transmitting_ = is_transmitting;
94 }
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +000095}
96
mflodman@webrtc.org84d17832011-12-01 17:02:23 +000097void ViEEncoder::Pause() {
Tommi97888bd2016-01-21 23:24:59 +010098 rtc::CritScope lock(&data_cs_);
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +000099 encoder_paused_ = true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000100}
101
perkjbc75d972016-05-02 06:31:25 -0700102void ViEEncoder::Start() {
Tommi97888bd2016-01-21 23:24:59 +0100103 rtc::CritScope lock(&data_cs_);
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000104 encoder_paused_ = false;
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000105}
106
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000107int32_t ViEEncoder::RegisterExternalEncoder(webrtc::VideoEncoder* encoder,
108 uint8_t pl_type,
109 bool internal_source) {
Peter Boströmcd5c25c2016-04-21 16:48:08 +0200110 video_sender_.RegisterExternalEncoder(encoder, pl_type, internal_source);
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000111 return 0;
112}
113
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000114int32_t ViEEncoder::DeRegisterExternalEncoder(uint8_t pl_type) {
Peter Boströmcd5c25c2016-04-21 16:48:08 +0200115 video_sender_.RegisterExternalEncoder(nullptr, pl_type, false);
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000116 return 0;
117}
Peter Boströma03785e2016-03-03 11:36:18 +0100118void ViEEncoder::SetEncoder(const webrtc::VideoCodec& video_codec,
perkjbc75d972016-05-02 06:31:25 -0700119 int min_transmit_bitrate_bps,
120 size_t max_data_payload_length,
121 EncodedImageCallback* sink) {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000122 // Setting target width and height for VPM.
Peter Boström905f8e72016-03-02 16:59:56 +0100123 RTC_CHECK_EQ(VPM_OK,
124 vp_->SetTargetResolution(video_codec.width, video_codec.height,
125 video_codec.maxFramerate));
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000126
mflodman86aabb22016-03-11 15:44:32 +0100127 // Cache codec before calling AddBitrateObserver (which calls OnBitrateUpdated
Peter Boströmd153a372015-11-10 15:27:12 +0000128 // that makes use of the number of simulcast streams configured).
stefan@webrtc.org9075d512014-02-14 09:45:58 +0000129 {
Tommi97888bd2016-01-21 23:24:59 +0100130 rtc::CritScope lock(&data_cs_);
Peter Boströmd153a372015-11-10 15:27:12 +0000131 encoder_config_ = video_codec;
Peter Boströma03785e2016-03-03 11:36:18 +0100132 min_transmit_bitrate_bps_ = min_transmit_bitrate_bps;
stefan@webrtc.org9075d512014-02-14 09:45:58 +0000133 }
perkjbc75d972016-05-02 06:31:25 -0700134 {
135 rtc::CritScope lock(&sink_cs_);
136 sink_ = sink;
137 }
Stefan Holmere5904162015-03-26 11:11:06 +0100138
Peter Boströmcd5c25c2016-04-21 16:48:08 +0200139 bool success = video_sender_.RegisterSendCodec(
mflodman86aabb22016-03-11 15:44:32 +0100140 &video_codec, number_of_cores_,
Peter Boström905f8e72016-03-02 16:59:56 +0100141 static_cast<uint32_t>(max_data_payload_length)) == VCM_OK;
142 if (!success) {
143 LOG(LS_ERROR) << "Failed to configure encoder.";
144 RTC_DCHECK(success);
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000145 }
Peter Boström905f8e72016-03-02 16:59:56 +0100146
Peter Boströma03785e2016-03-03 11:36:18 +0100147 if (stats_proxy_) {
Peter Boström79a5cf92016-03-03 11:49:06 +0100148 VideoEncoderConfig::ContentType content_type =
149 VideoEncoderConfig::ContentType::kRealtimeVideo;
Peter Boströma03785e2016-03-03 11:36:18 +0100150 switch (video_codec.mode) {
151 case kRealtimeVideo:
152 content_type = VideoEncoderConfig::ContentType::kRealtimeVideo;
153 break;
154 case kScreensharing:
155 content_type = VideoEncoderConfig::ContentType::kScreen;
156 break;
157 default:
158 RTC_NOTREACHED();
159 break;
160 }
161 stats_proxy_->SetContentType(content_type);
162 }
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000163}
164
stefana4a8d4a2015-07-15 04:39:22 -0700165int ViEEncoder::GetPaddingNeededBps() const {
Noah Richards099323e2015-04-15 09:14:12 -0700166 int64_t time_of_last_frame_activity_ms;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000167 int min_transmit_bitrate_bps;
stefana4a8d4a2015-07-15 04:39:22 -0700168 int bitrate_bps;
Peter Boströmd153a372015-11-10 15:27:12 +0000169 VideoCodec send_codec;
henrik.lundin@webrtc.org331d4402013-11-21 14:05:40 +0000170 {
Tommi97888bd2016-01-21 23:24:59 +0100171 rtc::CritScope lock(&data_cs_);
Peter Boströmd153a372015-11-10 15:27:12 +0000172 bool send_padding = encoder_config_.numberOfSimulcastStreams > 1 ||
Peter Boströma03785e2016-03-03 11:36:18 +0100173 video_suspended_ || min_transmit_bitrate_bps_ > 0;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000174 if (!send_padding)
175 return 0;
Noah Richards099323e2015-04-15 09:14:12 -0700176 time_of_last_frame_activity_ms = time_of_last_frame_activity_ms_;
Peter Boströma03785e2016-03-03 11:36:18 +0100177 min_transmit_bitrate_bps = min_transmit_bitrate_bps_;
stefana4a8d4a2015-07-15 04:39:22 -0700178 bitrate_bps = last_observed_bitrate_bps_;
Peter Boströmd153a372015-11-10 15:27:12 +0000179 send_codec = encoder_config_;
henrik.lundin@webrtc.org331d4402013-11-21 14:05:40 +0000180 }
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000181
Peter Boströmcd5c25c2016-04-21 16:48:08 +0200182 bool video_is_suspended = video_sender_.VideoSuspended();
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000183
184 // Find the max amount of padding we can allow ourselves to send at this
185 // point, based on which streams are currently active and what our current
186 // available bandwidth is.
187 int pad_up_to_bitrate_bps = 0;
188 if (send_codec.numberOfSimulcastStreams == 0) {
189 pad_up_to_bitrate_bps = send_codec.minBitrate * 1000;
190 } else {
stefana4a8d4a2015-07-15 04:39:22 -0700191 SimulcastStream* stream_configs = send_codec.simulcastStream;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000192 pad_up_to_bitrate_bps =
193 stream_configs[send_codec.numberOfSimulcastStreams - 1].minBitrate *
194 1000;
195 for (int i = 0; i < send_codec.numberOfSimulcastStreams - 1; ++i) {
196 pad_up_to_bitrate_bps += stream_configs[i].targetBitrate * 1000;
197 }
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000198 }
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000199
200 // Disable padding if only sending one stream and video isn't suspended and
201 // min-transmit bitrate isn't used (applied later).
202 if (!video_is_suspended && send_codec.numberOfSimulcastStreams <= 1)
203 pad_up_to_bitrate_bps = 0;
204
205 // The amount of padding should decay to zero if no frames are being
Noah Richards099323e2015-04-15 09:14:12 -0700206 // captured/encoded unless a min-transmit bitrate is used.
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000207 int64_t now_ms = TickTime::MillisecondTimestamp();
Noah Richards099323e2015-04-15 09:14:12 -0700208 if (now_ms - time_of_last_frame_activity_ms > kStopPaddingThresholdMs)
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000209 pad_up_to_bitrate_bps = 0;
210
211 // Pad up to min bitrate.
212 if (pad_up_to_bitrate_bps < min_transmit_bitrate_bps)
213 pad_up_to_bitrate_bps = min_transmit_bitrate_bps;
214
215 // Padding may never exceed bitrate estimate.
216 if (pad_up_to_bitrate_bps > bitrate_bps)
217 pad_up_to_bitrate_bps = bitrate_bps;
218
219 return pad_up_to_bitrate_bps;
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000220}
221
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000222bool ViEEncoder::EncoderPaused() const {
pwestin@webrtc.org91563e42013-04-25 22:20:08 +0000223 // Pause video if paused by caller or as long as the network is down or the
224 // pacer queue has grown too large in buffered mode.
225 if (encoder_paused_) {
226 return true;
227 }
Per28a44562016-05-04 17:12:51 +0200228 if (video_suspended_ || last_observed_bitrate_bps_ == 0) {
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000229 return true;
230 }
pwestin@webrtc.org91563e42013-04-25 22:20:08 +0000231 return !network_is_transmitting_;
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000232}
233
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000234void ViEEncoder::TraceFrameDropStart() {
235 // Start trace event only on the first frame after encoder is paused.
236 if (!encoder_paused_and_dropped_frame_) {
237 TRACE_EVENT_ASYNC_BEGIN0("webrtc", "EncoderPaused", this);
238 }
239 encoder_paused_and_dropped_frame_ = true;
240 return;
241}
242
243void ViEEncoder::TraceFrameDropEnd() {
244 // End trace event on first frame after encoder resumes, if frame was dropped.
245 if (encoder_paused_and_dropped_frame_) {
246 TRACE_EVENT_ASYNC_END0("webrtc", "EncoderPaused", this);
247 }
248 encoder_paused_and_dropped_frame_ = false;
249}
250
Peter Boströma4c76882016-03-03 16:29:02 +0100251void ViEEncoder::EncodeVideoFrame(const VideoFrame& video_frame) {
Peter Boströmd153a372015-11-10 15:27:12 +0000252 VideoCodecType codec_type;
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000253 {
Tommi97888bd2016-01-21 23:24:59 +0100254 rtc::CritScope lock(&data_cs_);
Noah Richards099323e2015-04-15 09:14:12 -0700255 time_of_last_frame_activity_ms_ = TickTime::MillisecondTimestamp();
pwestin@webrtc.org52b4e882013-05-02 19:02:17 +0000256 if (EncoderPaused()) {
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000257 TraceFrameDropStart();
pwestin@webrtc.org52b4e882013-05-02 19:02:17 +0000258 return;
259 }
sprang@webrtc.orgdcebf2d2014-11-04 16:27:16 +0000260 TraceFrameDropEnd();
Peter Boströmd153a372015-11-10 15:27:12 +0000261 codec_type = encoder_config_.codecType;
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000262 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000263
Magnus Jedvert26679d62015-04-07 14:07:41 +0200264 TRACE_EVENT_ASYNC_STEP0("webrtc", "Video", video_frame.render_time_ms(),
hclam@chromium.org1a7b9b92013-07-08 21:31:18 +0000265 "Encode");
mflodmana8565422015-12-07 01:09:52 -0800266 const VideoFrame* frame_to_send = &video_frame;
wuchengli@chromium.orgf425b552014-06-20 12:04:05 +0000267 // TODO(wuchengli): support texture frames.
nisse26acec42016-04-15 03:43:39 -0700268 if (!video_frame.video_frame_buffer()->native_handle()) {
wuchengli@chromium.orgf425b552014-06-20 12:04:05 +0000269 // Pass frame via preprocessor.
mflodmana8565422015-12-07 01:09:52 -0800270 frame_to_send = vp_->PreprocessFrame(video_frame);
271 if (!frame_to_send) {
272 // Drop this frame, or there was an error processing it.
wuchengli@chromium.orgf425b552014-06-20 12:04:05 +0000273 return;
274 }
pwestin@webrtc.org2f476ed2012-10-30 16:21:52 +0000275 }
pbos@webrtc.orgfe1ef932013-10-21 10:34:43 +0000276
Peter Boströmd153a372015-11-10 15:27:12 +0000277 if (codec_type == webrtc::kVideoCodecVP8) {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000278 webrtc::CodecSpecificInfo codec_specific_info;
279 codec_specific_info.codecType = webrtc::kVideoCodecVP8;
stefan@webrtc.org7af12be2014-07-09 14:46:31 +0000280 {
Tommi97888bd2016-01-21 23:24:59 +0100281 rtc::CritScope lock(&data_cs_);
stefan@webrtc.org7af12be2014-07-09 14:46:31 +0000282 codec_specific_info.codecSpecific.VP8.hasReceivedRPSI =
283 has_received_rpsi_;
284 codec_specific_info.codecSpecific.VP8.hasReceivedSLI =
285 has_received_sli_;
286 codec_specific_info.codecSpecific.VP8.pictureIdRPSI =
287 picture_id_rpsi_;
288 codec_specific_info.codecSpecific.VP8.pictureIdSLI =
289 picture_id_sli_;
290 has_received_sli_ = false;
291 has_received_rpsi_ = false;
292 }
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000293
Peter Boströmcd5c25c2016-04-21 16:48:08 +0200294 video_sender_.AddVideoFrame(*frame_to_send, vp_->GetContentMetrics(),
295 &codec_specific_info);
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000296 return;
297 }
Peter Boströmcd5c25c2016-04-21 16:48:08 +0200298 video_sender_.AddVideoFrame(*frame_to_send, nullptr, nullptr);
niklase@google.com470e71d2011-07-07 08:21:25 +0000299}
niklase@google.com470e71d2011-07-07 08:21:25 +0000300
Peter Boström233bfd22016-01-18 20:23:40 +0100301void ViEEncoder::SendKeyFrame() {
Peter Boströmcd5c25c2016-04-21 16:48:08 +0200302 video_sender_.IntraFrameRequest(0);
stefan@webrtc.org07b45a52012-02-02 08:37:48 +0000303}
304
Peter Boströmd153a372015-11-10 15:27:12 +0000305void ViEEncoder::SetProtectionMethod(bool nack, bool fec) {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000306 // Set Video Protection for VCM.
pbosba8c15b2015-07-14 09:36:34 -0700307 VCMVideoProtection protection_mode;
Peter Boströmd153a372015-11-10 15:27:12 +0000308 if (fec) {
pbosba8c15b2015-07-14 09:36:34 -0700309 protection_mode =
Peter Boströmd153a372015-11-10 15:27:12 +0000310 nack ? webrtc::kProtectionNackFEC : kProtectionFEC;
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000311 } else {
Peter Boströmd153a372015-11-10 15:27:12 +0000312 protection_mode = nack ? kProtectionNack : kProtectionNone;
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000313 }
Peter Boströmcd5c25c2016-04-21 16:48:08 +0200314 video_sender_.SetVideoProtection(protection_mode);
niklase@google.com470e71d2011-07-07 08:21:25 +0000315}
316
pbos@webrtc.org891d4832015-02-26 13:15:22 +0000317void ViEEncoder::OnSetRates(uint32_t bitrate_bps, int framerate) {
Peter Boström7083e112015-09-22 16:28:51 +0200318 if (stats_proxy_)
319 stats_proxy_->OnSetRates(bitrate_bps, framerate);
pbos@webrtc.org891d4832015-02-26 13:15:22 +0000320}
321
kjellander02b3d272016-04-20 05:05:54 -0700322int32_t ViEEncoder::Encoded(const EncodedImage& encoded_image,
323 const CodecSpecificInfo* codec_specific_info,
324 const RTPFragmentationHeader* fragmentation) {
pbos@webrtc.org891d4832015-02-26 13:15:22 +0000325 {
Tommi97888bd2016-01-21 23:24:59 +0100326 rtc::CritScope lock(&data_cs_);
Noah Richards099323e2015-04-15 09:14:12 -0700327 time_of_last_frame_activity_ms_ = TickTime::MillisecondTimestamp();
328 }
kjellander02b3d272016-04-20 05:05:54 -0700329 if (stats_proxy_) {
330 stats_proxy_->OnSendEncodedImage(encoded_image, codec_specific_info);
331 }
sprang3911c262016-04-15 01:24:14 -0700332
perkjbc75d972016-05-02 06:31:25 -0700333 int success = 0;
334 {
335 rtc::CritScope lock(&sink_cs_);
336 success = sink_->Encoded(encoded_image, codec_specific_info, fragmentation);
337 }
338
339 overuse_detector_->FrameSent(encoded_image._timeStamp);
kjellander02b3d272016-04-20 05:05:54 -0700340 return success;
Peter Boströmb7d9a972015-12-18 16:01:11 +0100341}
342
perkj376b1922016-05-02 11:35:24 -0700343void ViEEncoder::SendStatistics(uint32_t bit_rate,
344 uint32_t frame_rate,
345 const std::string& encoder_name) {
Peter Boström7083e112015-09-22 16:28:51 +0200346 if (stats_proxy_)
perkj376b1922016-05-02 11:35:24 -0700347 stats_proxy_->OnEncoderStatsUpdate(frame_rate, bit_rate, encoder_name);
niklase@google.com470e71d2011-07-07 08:21:25 +0000348}
349
perkj600246e2016-05-04 11:26:51 -0700350void ViEEncoder::OnReceivedSLI(uint8_t picture_id) {
Tommi97888bd2016-01-21 23:24:59 +0100351 rtc::CritScope lock(&data_cs_);
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000352 picture_id_sli_ = picture_id;
353 has_received_sli_ = true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000354}
355
perkj600246e2016-05-04 11:26:51 -0700356void ViEEncoder::OnReceivedRPSI(uint64_t picture_id) {
Tommi97888bd2016-01-21 23:24:59 +0100357 rtc::CritScope lock(&data_cs_);
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000358 picture_id_rpsi_ = picture_id;
359 has_received_rpsi_ = true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000360}
361
perkj600246e2016-05-04 11:26:51 -0700362void ViEEncoder::OnReceivedIntraFrameRequest(size_t stream_index) {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000363 // Key frame request from remote side, signal to VCM.
justinlin@chromium.org7bfb3a32013-05-13 22:59:00 +0000364 TRACE_EVENT0("webrtc", "OnKeyFrameRequest");
perkj600246e2016-05-04 11:26:51 -0700365 video_sender_.IntraFrameRequest(stream_index);
mflodman@webrtc.orgaca26292012-10-05 16:17:41 +0000366}
367
mflodman86aabb22016-03-11 15:44:32 +0100368void ViEEncoder::OnBitrateUpdated(uint32_t bitrate_bps,
stefan@webrtc.orgedeea912014-12-08 19:46:23 +0000369 uint8_t fraction_lost,
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000370 int64_t round_trip_time_ms) {
Per28a44562016-05-04 17:12:51 +0200371 LOG(LS_VERBOSE) << "OnBitrateUpdated, bitrate " << bitrate_bps
mflodman8602a3d2015-05-20 15:54:42 -0700372 << " packet loss " << static_cast<int>(fraction_lost)
mflodman@webrtc.org5574dac2014-04-07 10:56:31 +0000373 << " rtt " << round_trip_time_ms;
Peter Boströmcd5c25c2016-04-21 16:48:08 +0200374 video_sender_.SetChannelParameters(bitrate_bps, fraction_lost,
375 round_trip_time_ms);
376 bool video_is_suspended = video_sender_.VideoSuspended();
Peter Boströmd153a372015-11-10 15:27:12 +0000377 bool video_suspension_changed;
Peter Boströmd153a372015-11-10 15:27:12 +0000378 {
Tommi97888bd2016-01-21 23:24:59 +0100379 rtc::CritScope lock(&data_cs_);
Peter Boströmd153a372015-11-10 15:27:12 +0000380 last_observed_bitrate_bps_ = bitrate_bps;
381 video_suspension_changed = video_suspended_ != video_is_suspended;
382 video_suspended_ = video_is_suspended;
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000383 }
Peter Boströmd153a372015-11-10 15:27:12 +0000384
Peter Boströmd153a372015-11-10 15:27:12 +0000385 if (!video_suspension_changed)
386 return;
Peter Boström7083e112015-09-22 16:28:51 +0200387 // Video suspend-state changed, inform codec observer.
perkj600246e2016-05-04 11:26:51 -0700388 LOG(LS_INFO) << "Video suspend state changed " << video_is_suspended;
389
Peter Boström7083e112015-09-22 16:28:51 +0200390 if (stats_proxy_)
391 stats_proxy_->OnSuspendChange(video_is_suspended);
niklase@google.com470e71d2011-07-07 08:21:25 +0000392}
393
mflodmana8565422015-12-07 01:09:52 -0800394QMVideoSettingsCallback::QMVideoSettingsCallback(VideoProcessing* vpm)
395 : vp_(vpm) {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000396}
niklase@google.com470e71d2011-07-07 08:21:25 +0000397
stefan@webrtc.org439be292012-02-16 14:45:37 +0000398QMVideoSettingsCallback::~QMVideoSettingsCallback() {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000399}
400
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000401int32_t QMVideoSettingsCallback::SetVideoQMSettings(
402 const uint32_t frame_rate,
403 const uint32_t width,
404 const uint32_t height) {
mflodmana8565422015-12-07 01:09:52 -0800405 return vp_->SetTargetResolution(width, height, frame_rate);
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000406}
407
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000408} // namespace webrtc