niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
stefan@webrtc.org | 07b45a5 | 2012-02-02 08:37:48 +0000 | [diff] [blame] | 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 3 | * |
| 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 | |
pbos@webrtc.org | f5d4cb1 | 2013-05-17 13:44:48 +0000 | [diff] [blame] | 11 | #include "webrtc/video_engine/vie_encoder.h" |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 12 | |
pbos@webrtc.org | 12dc1a3 | 2013-08-05 16:22:53 +0000 | [diff] [blame] | 13 | #include <assert.h> |
| 14 | |
stefan@webrtc.org | c3cc375 | 2013-06-04 09:36:56 +0000 | [diff] [blame] | 15 | #include <algorithm> |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 16 | |
sprang@webrtc.org | 4070935 | 2013-11-26 11:41:59 +0000 | [diff] [blame] | 17 | #include "webrtc/common_video/interface/video_image.h" |
pbos@webrtc.org | f5d4cb1 | 2013-05-17 13:44:48 +0000 | [diff] [blame] | 18 | #include "webrtc/common_video/libyuv/include/webrtc_libyuv.h" |
| 19 | #include "webrtc/modules/pacing/include/paced_sender.h" |
| 20 | #include "webrtc/modules/rtp_rtcp/interface/rtp_rtcp.h" |
| 21 | #include "webrtc/modules/utility/interface/process_thread.h" |
| 22 | #include "webrtc/modules/video_coding/codecs/interface/video_codec_interface.h" |
| 23 | #include "webrtc/modules/video_coding/main/interface/video_coding.h" |
| 24 | #include "webrtc/modules/video_coding/main/interface/video_coding_defines.h" |
sprang@webrtc.org | 4070935 | 2013-11-26 11:41:59 +0000 | [diff] [blame] | 25 | #include "webrtc/modules/video_coding/main/source/encoded_frame.h" |
stefan@webrtc.org | 88e0dda | 2014-07-04 09:20:42 +0000 | [diff] [blame] | 26 | #include "webrtc/system_wrappers/interface/clock.h" |
pbos@webrtc.org | f5d4cb1 | 2013-05-17 13:44:48 +0000 | [diff] [blame] | 27 | #include "webrtc/system_wrappers/interface/critical_section_wrapper.h" |
| 28 | #include "webrtc/system_wrappers/interface/logging.h" |
asapersson@webrtc.org | 96dc685 | 2014-11-03 14:40:38 +0000 | [diff] [blame] | 29 | #include "webrtc/system_wrappers/interface/metrics.h" |
pbos@webrtc.org | f5d4cb1 | 2013-05-17 13:44:48 +0000 | [diff] [blame] | 30 | #include "webrtc/system_wrappers/interface/tick_util.h" |
pbos@webrtc.org | f5d4cb1 | 2013-05-17 13:44:48 +0000 | [diff] [blame] | 31 | #include "webrtc/system_wrappers/interface/trace_event.h" |
| 32 | #include "webrtc/video_engine/include/vie_codec.h" |
| 33 | #include "webrtc/video_engine/include/vie_image_process.h" |
pbos@webrtc.org | 16e03b7 | 2013-10-28 16:32:01 +0000 | [diff] [blame] | 34 | #include "webrtc/frame_callback.h" |
pbos@webrtc.org | f5d4cb1 | 2013-05-17 13:44:48 +0000 | [diff] [blame] | 35 | #include "webrtc/video_engine/vie_defines.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 36 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 37 | namespace webrtc { |
| 38 | |
pwestin@webrtc.org | 52b4e88 | 2013-05-02 19:02:17 +0000 | [diff] [blame] | 39 | // Margin on when we pause the encoder when the pacing buffer overflows relative |
| 40 | // to the configured buffer delay. |
| 41 | static const float kEncoderPausePacerMargin = 2.0f; |
| 42 | |
pwestin@webrtc.org | 91563e4 | 2013-04-25 22:20:08 +0000 | [diff] [blame] | 43 | // Don't stop the encoder unless the delay is above this configured value. |
| 44 | static const int kMinPacingDelayMs = 200; |
| 45 | |
stefan@webrtc.org | bfacda6 | 2013-03-27 16:36:01 +0000 | [diff] [blame] | 46 | // Allow packets to be transmitted in up to 2 times max video bitrate if the |
| 47 | // bandwidth estimate allows it. |
| 48 | // TODO(holmer): Expose transmission start, min and max bitrates in the |
| 49 | // VideoEngine API and remove the kTransmissionMaxBitrateMultiplier. |
| 50 | static const int kTransmissionMaxBitrateMultiplier = 2; |
pwestin@webrtc.org | 571a1c0 | 2012-11-13 21:12:39 +0000 | [diff] [blame] | 51 | |
stefan@webrtc.org | 3e00505 | 2013-10-18 15:05:29 +0000 | [diff] [blame] | 52 | static const float kStopPaddingThresholdMs = 2000; |
| 53 | |
stefan@webrtc.org | b2c8a95 | 2013-09-06 13:58:01 +0000 | [diff] [blame] | 54 | std::vector<uint32_t> AllocateStreamBitrates( |
| 55 | uint32_t total_bitrate, |
| 56 | const SimulcastStream* stream_configs, |
| 57 | size_t number_of_streams) { |
| 58 | if (number_of_streams == 0) { |
| 59 | std::vector<uint32_t> stream_bitrates(1, 0); |
| 60 | stream_bitrates[0] = total_bitrate; |
| 61 | return stream_bitrates; |
| 62 | } |
| 63 | std::vector<uint32_t> stream_bitrates(number_of_streams, 0); |
| 64 | uint32_t bitrate_remainder = total_bitrate; |
| 65 | for (size_t i = 0; i < stream_bitrates.size() && bitrate_remainder > 0; ++i) { |
| 66 | if (stream_configs[i].maxBitrate * 1000 > bitrate_remainder) { |
| 67 | stream_bitrates[i] = bitrate_remainder; |
| 68 | } else { |
| 69 | stream_bitrates[i] = stream_configs[i].maxBitrate * 1000; |
| 70 | } |
| 71 | bitrate_remainder -= stream_bitrates[i]; |
| 72 | } |
| 73 | return stream_bitrates; |
| 74 | } |
| 75 | |
stefan@webrtc.org | 439be29 | 2012-02-16 14:45:37 +0000 | [diff] [blame] | 76 | class QMVideoSettingsCallback : public VCMQMSettingsCallback { |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 77 | public: |
marpan@webrtc.org | efd01fd | 2012-04-18 15:56:34 +0000 | [diff] [blame] | 78 | explicit QMVideoSettingsCallback(VideoProcessingModule* vpm); |
mflodman@webrtc.org | 6879c8a | 2013-07-23 11:35:00 +0000 | [diff] [blame] | 79 | |
stefan@webrtc.org | 439be29 | 2012-02-16 14:45:37 +0000 | [diff] [blame] | 80 | ~QMVideoSettingsCallback(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 81 | |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 82 | // Update VPM with QM (quality modes: frame size & frame rate) settings. |
pbos@webrtc.org | b238d12 | 2013-04-09 13:41:51 +0000 | [diff] [blame] | 83 | int32_t SetVideoQMSettings(const uint32_t frame_rate, |
| 84 | const uint32_t width, |
| 85 | const uint32_t height); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 86 | |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 87 | private: |
| 88 | VideoProcessingModule* vpm_; |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 89 | }; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 90 | |
pwestin@webrtc.org | 49888ce | 2012-04-27 05:25:53 +0000 | [diff] [blame] | 91 | class ViEBitrateObserver : public BitrateObserver { |
| 92 | public: |
mflodman@webrtc.org | f5e99db | 2012-06-27 09:49:37 +0000 | [diff] [blame] | 93 | explicit ViEBitrateObserver(ViEEncoder* owner) |
pwestin@webrtc.org | 49888ce | 2012-04-27 05:25:53 +0000 | [diff] [blame] | 94 | : owner_(owner) { |
| 95 | } |
mflodman@webrtc.org | 6879c8a | 2013-07-23 11:35:00 +0000 | [diff] [blame] | 96 | virtual ~ViEBitrateObserver() {} |
pwestin@webrtc.org | 49888ce | 2012-04-27 05:25:53 +0000 | [diff] [blame] | 97 | // Implements BitrateObserver. |
| 98 | virtual void OnNetworkChanged(const uint32_t bitrate_bps, |
| 99 | const uint8_t fraction_lost, |
| 100 | const uint32_t rtt) { |
| 101 | owner_->OnNetworkChanged(bitrate_bps, fraction_lost, rtt); |
| 102 | } |
| 103 | private: |
| 104 | ViEEncoder* owner_; |
| 105 | }; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 106 | |
pwestin@webrtc.org | 571a1c0 | 2012-11-13 21:12:39 +0000 | [diff] [blame] | 107 | class ViEPacedSenderCallback : public PacedSender::Callback { |
| 108 | public: |
| 109 | explicit ViEPacedSenderCallback(ViEEncoder* owner) |
| 110 | : owner_(owner) { |
| 111 | } |
mflodman@webrtc.org | 6879c8a | 2013-07-23 11:35:00 +0000 | [diff] [blame] | 112 | virtual ~ViEPacedSenderCallback() {} |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 113 | virtual bool TimeToSendPacket(uint32_t ssrc, |
| 114 | uint16_t sequence_number, |
| 115 | int64_t capture_time_ms, |
| 116 | bool retransmission) { |
stefan@webrtc.org | 9b82f5a | 2013-11-13 15:29:21 +0000 | [diff] [blame] | 117 | return owner_->TimeToSendPacket(ssrc, sequence_number, capture_time_ms, |
| 118 | retransmission); |
pwestin@webrtc.org | 571a1c0 | 2012-11-13 21:12:39 +0000 | [diff] [blame] | 119 | } |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame^] | 120 | virtual size_t TimeToSendPadding(size_t bytes) { |
stefan@webrtc.org | 508a84b | 2013-06-17 12:53:37 +0000 | [diff] [blame] | 121 | return owner_->TimeToSendPadding(bytes); |
pwestin@webrtc.org | 571a1c0 | 2012-11-13 21:12:39 +0000 | [diff] [blame] | 122 | } |
| 123 | private: |
| 124 | ViEEncoder* owner_; |
| 125 | }; |
| 126 | |
pbos@webrtc.org | b238d12 | 2013-04-09 13:41:51 +0000 | [diff] [blame] | 127 | ViEEncoder::ViEEncoder(int32_t engine_id, |
| 128 | int32_t channel_id, |
| 129 | uint32_t number_of_cores, |
andresp@webrtc.org | 7707d06 | 2013-05-13 10:50:50 +0000 | [diff] [blame] | 130 | const Config& config, |
pwestin@webrtc.org | 49888ce | 2012-04-27 05:25:53 +0000 | [diff] [blame] | 131 | ProcessThread& module_process_thread, |
| 132 | BitrateController* bitrate_controller) |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 133 | : engine_id_(engine_id), |
| 134 | channel_id_(channel_id), |
| 135 | number_of_cores_(number_of_cores), |
stefan@webrtc.org | 34c5da6 | 2014-04-11 14:08:35 +0000 | [diff] [blame] | 136 | vcm_(*webrtc::VideoCodingModule::Create()), |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 137 | vpm_(*webrtc::VideoProcessingModule::Create(ViEModuleId(engine_id, |
| 138 | channel_id))), |
mflodman@webrtc.org | d32c447 | 2011-12-22 14:17:53 +0000 | [diff] [blame] | 139 | callback_cs_(CriticalSectionWrapper::CreateCriticalSection()), |
| 140 | data_cs_(CriticalSectionWrapper::CreateCriticalSection()), |
pwestin@webrtc.org | 49888ce | 2012-04-27 05:25:53 +0000 | [diff] [blame] | 141 | bitrate_controller_(bitrate_controller), |
stefan@webrtc.org | 3e00505 | 2013-10-18 15:05:29 +0000 | [diff] [blame] | 142 | time_of_last_incoming_frame_ms_(0), |
stefan@webrtc.org | 508a84b | 2013-06-17 12:53:37 +0000 | [diff] [blame] | 143 | send_padding_(false), |
pbos@webrtc.org | 3349ae0 | 2014-03-13 12:52:27 +0000 | [diff] [blame] | 144 | min_transmit_bitrate_kbps_(0), |
stefan@webrtc.org | bfacda6 | 2013-03-27 16:36:01 +0000 | [diff] [blame] | 145 | target_delay_ms_(0), |
| 146 | network_is_transmitting_(true), |
| 147 | encoder_paused_(false), |
pwestin@webrtc.org | 52b4e88 | 2013-05-02 19:02:17 +0000 | [diff] [blame] | 148 | encoder_paused_and_dropped_frame_(false), |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 149 | fec_enabled_(false), |
| 150 | nack_enabled_(false), |
| 151 | codec_observer_(NULL), |
| 152 | effect_filter_(NULL), |
| 153 | module_process_thread_(module_process_thread), |
| 154 | has_received_sli_(false), |
| 155 | picture_id_sli_(0), |
| 156 | has_received_rpsi_(false), |
| 157 | picture_id_rpsi_(0), |
henrik.lundin@webrtc.org | 7ea4f24 | 2013-10-02 13:34:26 +0000 | [diff] [blame] | 158 | qm_callback_(NULL), |
henrik.lundin@webrtc.org | ce8e093 | 2013-11-18 12:18:43 +0000 | [diff] [blame] | 159 | video_suspended_(false), |
asapersson@webrtc.org | 96dc685 | 2014-11-03 14:40:38 +0000 | [diff] [blame] | 160 | pre_encode_callback_(NULL), |
| 161 | start_ms_(Clock::GetRealTimeClock()->TimeInMilliseconds()) { |
pwestin@webrtc.org | 2853dde | 2012-05-11 11:08:54 +0000 | [diff] [blame] | 162 | RtpRtcp::Configuration configuration; |
| 163 | configuration.id = ViEModuleId(engine_id_, channel_id_); |
| 164 | configuration.audio = false; // Video. |
| 165 | |
| 166 | default_rtp_rtcp_.reset(RtpRtcp::CreateRtpRtcp(configuration)); |
pwestin@webrtc.org | 49888ce | 2012-04-27 05:25:53 +0000 | [diff] [blame] | 167 | bitrate_observer_.reset(new ViEBitrateObserver(this)); |
pwestin@webrtc.org | 571a1c0 | 2012-11-13 21:12:39 +0000 | [diff] [blame] | 168 | pacing_callback_.reset(new ViEPacedSenderCallback(this)); |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 169 | paced_sender_.reset(new PacedSender( |
| 170 | Clock::GetRealTimeClock(), |
| 171 | pacing_callback_.get(), |
| 172 | kDefaultStartBitrateKbps, |
| 173 | PacedSender::kDefaultPaceMultiplier * kDefaultStartBitrateKbps, |
| 174 | 0)); |
wu@webrtc.org | 5d8c102 | 2012-04-10 16:54:05 +0000 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | bool ViEEncoder::Init() { |
| 178 | if (vcm_.InitializeSender() != 0) { |
wu@webrtc.org | 5d8c102 | 2012-04-10 16:54:05 +0000 | [diff] [blame] | 179 | return false; |
| 180 | } |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 181 | vpm_.EnableTemporalDecimation(true); |
| 182 | |
| 183 | // Enable/disable content analysis: off by default for now. |
| 184 | vpm_.EnableContentAnalysis(false); |
| 185 | |
pwestin@webrtc.org | 571a1c0 | 2012-11-13 21:12:39 +0000 | [diff] [blame] | 186 | if (module_process_thread_.RegisterModule(&vcm_) != 0 || |
| 187 | module_process_thread_.RegisterModule(default_rtp_rtcp_.get()) != 0 || |
| 188 | module_process_thread_.RegisterModule(paced_sender_.get()) != 0) { |
wu@webrtc.org | 5d8c102 | 2012-04-10 16:54:05 +0000 | [diff] [blame] | 189 | return false; |
| 190 | } |
stefan@webrtc.org | 9784512 | 2012-04-13 07:47:05 +0000 | [diff] [blame] | 191 | if (qm_callback_) { |
| 192 | delete qm_callback_; |
| 193 | } |
marpan@webrtc.org | efd01fd | 2012-04-18 15:56:34 +0000 | [diff] [blame] | 194 | qm_callback_ = new QMVideoSettingsCallback(&vpm_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 195 | |
| 196 | #ifdef VIDEOCODEC_VP8 |
andresp@webrtc.org | a84b0a6 | 2014-08-14 16:46:46 +0000 | [diff] [blame] | 197 | VideoCodecType codec_type = webrtc::kVideoCodecVP8; |
| 198 | #else |
| 199 | VideoCodecType codec_type = webrtc::kVideoCodecI420; |
| 200 | #endif |
| 201 | |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 202 | VideoCodec video_codec; |
andresp@webrtc.org | a84b0a6 | 2014-08-14 16:46:46 +0000 | [diff] [blame] | 203 | if (vcm_.Codec(codec_type, &video_codec) != VCM_OK) { |
wu@webrtc.org | 5d8c102 | 2012-04-10 16:54:05 +0000 | [diff] [blame] | 204 | return false; |
| 205 | } |
stefan@webrtc.org | ae2563a | 2014-02-13 13:48:38 +0000 | [diff] [blame] | 206 | { |
| 207 | CriticalSectionScoped cs(data_cs_.get()); |
| 208 | send_padding_ = video_codec.numberOfSimulcastStreams > 1; |
| 209 | } |
wu@webrtc.org | 5d8c102 | 2012-04-10 16:54:05 +0000 | [diff] [blame] | 210 | if (vcm_.RegisterSendCodec(&video_codec, number_of_cores_, |
pwestin@webrtc.org | 2853dde | 2012-05-11 11:08:54 +0000 | [diff] [blame] | 211 | default_rtp_rtcp_->MaxDataPayloadLength()) != 0) { |
wu@webrtc.org | 5d8c102 | 2012-04-10 16:54:05 +0000 | [diff] [blame] | 212 | return false; |
| 213 | } |
pwestin@webrtc.org | 2853dde | 2012-05-11 11:08:54 +0000 | [diff] [blame] | 214 | if (default_rtp_rtcp_->RegisterSendPayload(video_codec) != 0) { |
wu@webrtc.org | 5d8c102 | 2012-04-10 16:54:05 +0000 | [diff] [blame] | 215 | return false; |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 216 | } |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 217 | if (vcm_.RegisterTransportCallback(this) != 0) { |
wu@webrtc.org | 5d8c102 | 2012-04-10 16:54:05 +0000 | [diff] [blame] | 218 | return false; |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 219 | } |
| 220 | if (vcm_.RegisterSendStatisticsCallback(this) != 0) { |
wu@webrtc.org | 5d8c102 | 2012-04-10 16:54:05 +0000 | [diff] [blame] | 221 | return false; |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 222 | } |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 223 | if (vcm_.RegisterVideoQMCallback(qm_callback_) != 0) { |
wu@webrtc.org | 5d8c102 | 2012-04-10 16:54:05 +0000 | [diff] [blame] | 224 | return false; |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 225 | } |
wu@webrtc.org | 5d8c102 | 2012-04-10 16:54:05 +0000 | [diff] [blame] | 226 | return true; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 227 | } |
| 228 | |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 229 | ViEEncoder::~ViEEncoder() { |
asapersson@webrtc.org | 96dc685 | 2014-11-03 14:40:38 +0000 | [diff] [blame] | 230 | UpdateHistograms(); |
stefan@webrtc.org | bf41508 | 2012-11-29 09:18:53 +0000 | [diff] [blame] | 231 | if (bitrate_controller_) { |
| 232 | bitrate_controller_->RemoveBitrateObserver(bitrate_observer_.get()); |
| 233 | } |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 234 | module_process_thread_.DeRegisterModule(&vcm_); |
| 235 | module_process_thread_.DeRegisterModule(&vpm_); |
pwestin@webrtc.org | 2853dde | 2012-05-11 11:08:54 +0000 | [diff] [blame] | 236 | module_process_thread_.DeRegisterModule(default_rtp_rtcp_.get()); |
pwestin@webrtc.org | 571a1c0 | 2012-11-13 21:12:39 +0000 | [diff] [blame] | 237 | module_process_thread_.DeRegisterModule(paced_sender_.get()); |
mflodman@webrtc.org | 6648093 | 2013-03-01 14:51:23 +0000 | [diff] [blame] | 238 | VideoCodingModule::Destroy(&vcm_); |
| 239 | VideoProcessingModule::Destroy(&vpm_); |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 240 | delete qm_callback_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 241 | } |
| 242 | |
asapersson@webrtc.org | 96dc685 | 2014-11-03 14:40:38 +0000 | [diff] [blame] | 243 | void ViEEncoder::UpdateHistograms() { |
| 244 | const float kMinCallLengthInMinutes = 0.5f; |
| 245 | float elapsed_minutes = |
| 246 | (Clock::GetRealTimeClock()->TimeInMilliseconds() - start_ms_) / 60000.0f; |
| 247 | if (elapsed_minutes < kMinCallLengthInMinutes) { |
| 248 | return; |
| 249 | } |
| 250 | webrtc::VCMFrameCount frames; |
| 251 | if (vcm_.SentFrameCount(frames) != VCM_OK) { |
| 252 | return; |
| 253 | } |
| 254 | uint32_t total_frames = frames.numKeyFrames + frames.numDeltaFrames; |
| 255 | if (total_frames > 0) { |
| 256 | RTC_HISTOGRAM_COUNTS_1000("WebRTC.Video.KeyFramesSentInPermille", |
| 257 | static_cast<int>( |
| 258 | (frames.numKeyFrames * 1000.0f / total_frames) + 0.5f)); |
| 259 | } |
| 260 | } |
| 261 | |
mflodman@webrtc.org | 9ec883e | 2012-03-05 17:12:41 +0000 | [diff] [blame] | 262 | int ViEEncoder::Owner() const { |
| 263 | return channel_id_; |
| 264 | } |
| 265 | |
stefan@webrtc.org | bfacda6 | 2013-03-27 16:36:01 +0000 | [diff] [blame] | 266 | void ViEEncoder::SetNetworkTransmissionState(bool is_transmitting) { |
stefan@webrtc.org | bfacda6 | 2013-03-27 16:36:01 +0000 | [diff] [blame] | 267 | { |
| 268 | CriticalSectionScoped cs(data_cs_.get()); |
| 269 | network_is_transmitting_ = is_transmitting; |
| 270 | } |
| 271 | if (is_transmitting) { |
| 272 | paced_sender_->Resume(); |
| 273 | } else { |
| 274 | paced_sender_->Pause(); |
| 275 | } |
| 276 | } |
| 277 | |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 278 | void ViEEncoder::Pause() { |
mflodman@webrtc.org | d32c447 | 2011-12-22 14:17:53 +0000 | [diff] [blame] | 279 | CriticalSectionScoped cs(data_cs_.get()); |
stefan@webrtc.org | bfacda6 | 2013-03-27 16:36:01 +0000 | [diff] [blame] | 280 | encoder_paused_ = true; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 281 | } |
| 282 | |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 283 | void ViEEncoder::Restart() { |
mflodman@webrtc.org | d32c447 | 2011-12-22 14:17:53 +0000 | [diff] [blame] | 284 | CriticalSectionScoped cs(data_cs_.get()); |
stefan@webrtc.org | bfacda6 | 2013-03-27 16:36:01 +0000 | [diff] [blame] | 285 | encoder_paused_ = false; |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 286 | } |
| 287 | |
pbos@webrtc.org | b238d12 | 2013-04-09 13:41:51 +0000 | [diff] [blame] | 288 | uint8_t ViEEncoder::NumberOfCodecs() { |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 289 | return vcm_.NumberOfCodecs(); |
| 290 | } |
| 291 | |
pbos@webrtc.org | b238d12 | 2013-04-09 13:41:51 +0000 | [diff] [blame] | 292 | int32_t ViEEncoder::GetCodec(uint8_t list_index, VideoCodec* video_codec) { |
mflodman@webrtc.org | f5e99db | 2012-06-27 09:49:37 +0000 | [diff] [blame] | 293 | if (vcm_.Codec(list_index, video_codec) != 0) { |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 294 | return -1; |
| 295 | } |
| 296 | return 0; |
| 297 | } |
| 298 | |
pbos@webrtc.org | b238d12 | 2013-04-09 13:41:51 +0000 | [diff] [blame] | 299 | int32_t ViEEncoder::RegisterExternalEncoder(webrtc::VideoEncoder* encoder, |
| 300 | uint8_t pl_type, |
| 301 | bool internal_source) { |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 302 | if (encoder == NULL) |
| 303 | return -1; |
| 304 | |
stefan@webrtc.org | fcd8585 | 2013-01-09 08:35:40 +0000 | [diff] [blame] | 305 | if (vcm_.RegisterExternalEncoder(encoder, pl_type, internal_source) != |
mflodman@webrtc.org | 5574dac | 2014-04-07 10:56:31 +0000 | [diff] [blame] | 306 | VCM_OK) { |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 307 | return -1; |
| 308 | } |
| 309 | return 0; |
| 310 | } |
| 311 | |
pbos@webrtc.org | b238d12 | 2013-04-09 13:41:51 +0000 | [diff] [blame] | 312 | int32_t ViEEncoder::DeRegisterExternalEncoder(uint8_t pl_type) { |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 313 | webrtc::VideoCodec current_send_codec; |
| 314 | if (vcm_.SendCodec(¤t_send_codec) == VCM_OK) { |
stefan@webrtc.org | 3d0b0d6 | 2013-03-19 10:04:57 +0000 | [diff] [blame] | 315 | uint32_t current_bitrate_bps = 0; |
| 316 | if (vcm_.Bitrate(¤t_bitrate_bps) != 0) { |
mflodman@webrtc.org | 5574dac | 2014-04-07 10:56:31 +0000 | [diff] [blame] | 317 | LOG(LS_WARNING) << "Failed to get the current encoder target bitrate."; |
stefan@webrtc.org | 439be29 | 2012-02-16 14:45:37 +0000 | [diff] [blame] | 318 | } |
stefan@webrtc.org | 3d0b0d6 | 2013-03-19 10:04:57 +0000 | [diff] [blame] | 319 | current_send_codec.startBitrate = (current_bitrate_bps + 500) / 1000; |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | if (vcm_.RegisterExternalEncoder(NULL, pl_type) != VCM_OK) { |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 323 | return -1; |
| 324 | } |
| 325 | |
stefan@webrtc.org | 508a84b | 2013-06-17 12:53:37 +0000 | [diff] [blame] | 326 | // If the external encoder is the current send codec, use vcm internal |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 327 | // encoder. |
| 328 | if (current_send_codec.plType == pl_type) { |
pbos@webrtc.org | b238d12 | 2013-04-09 13:41:51 +0000 | [diff] [blame] | 329 | uint16_t max_data_payload_length = |
pwestin@webrtc.org | 2853dde | 2012-05-11 11:08:54 +0000 | [diff] [blame] | 330 | default_rtp_rtcp_->MaxDataPayloadLength(); |
stefan@webrtc.org | ae2563a | 2014-02-13 13:48:38 +0000 | [diff] [blame] | 331 | { |
| 332 | CriticalSectionScoped cs(data_cs_.get()); |
| 333 | send_padding_ = current_send_codec.numberOfSimulcastStreams > 1; |
| 334 | } |
fischman@webrtc.org | 64e0405 | 2014-03-07 18:00:05 +0000 | [diff] [blame] | 335 | // TODO(mflodman): Unfortunately the VideoCodec that VCM has cached a |
| 336 | // raw pointer to an |extra_options| that's long gone. Clearing it here is |
| 337 | // a hack to prevent the following code from crashing. This should be fixed |
| 338 | // for realz. https://code.google.com/p/chromium/issues/detail?id=348222 |
| 339 | current_send_codec.extra_options = NULL; |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 340 | if (vcm_.RegisterSendCodec(¤t_send_codec, number_of_cores_, |
| 341 | max_data_payload_length) != VCM_OK) { |
stefan@webrtc.org | 4070b1d | 2014-07-16 11:20:40 +0000 | [diff] [blame] | 342 | LOG(LS_INFO) << "De-registered the currently used external encoder (" |
| 343 | << static_cast<int>(pl_type) << ") and therefore tried to " |
| 344 | << "register the corresponding internal encoder, but none " |
| 345 | << "was supported."; |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 346 | } |
| 347 | } |
| 348 | return 0; |
| 349 | } |
| 350 | |
pbos@webrtc.org | b238d12 | 2013-04-09 13:41:51 +0000 | [diff] [blame] | 351 | int32_t ViEEncoder::SetEncoder(const webrtc::VideoCodec& video_codec) { |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 352 | // Setting target width and height for VPM. |
| 353 | if (vpm_.SetTargetResolution(video_codec.width, video_codec.height, |
| 354 | video_codec.maxFramerate) != VPM_OK) { |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 355 | return -1; |
| 356 | } |
| 357 | |
pwestin@webrtc.org | 2853dde | 2012-05-11 11:08:54 +0000 | [diff] [blame] | 358 | if (default_rtp_rtcp_->RegisterSendPayload(video_codec) != 0) { |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 359 | return -1; |
| 360 | } |
pwestin@webrtc.org | 49888ce | 2012-04-27 05:25:53 +0000 | [diff] [blame] | 361 | // Convert from kbps to bps. |
stefan@webrtc.org | b2c8a95 | 2013-09-06 13:58:01 +0000 | [diff] [blame] | 362 | std::vector<uint32_t> stream_bitrates = AllocateStreamBitrates( |
| 363 | video_codec.startBitrate * 1000, |
| 364 | video_codec.simulcastStream, |
| 365 | video_codec.numberOfSimulcastStreams); |
| 366 | default_rtp_rtcp_->SetTargetSendBitrate(stream_bitrates); |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 367 | |
pbos@webrtc.org | b238d12 | 2013-04-09 13:41:51 +0000 | [diff] [blame] | 368 | uint16_t max_data_payload_length = |
pwestin@webrtc.org | 2853dde | 2012-05-11 11:08:54 +0000 | [diff] [blame] | 369 | default_rtp_rtcp_->MaxDataPayloadLength(); |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 370 | |
stefan@webrtc.org | 9075d51 | 2014-02-14 09:45:58 +0000 | [diff] [blame] | 371 | { |
| 372 | CriticalSectionScoped cs(data_cs_.get()); |
| 373 | send_padding_ = video_codec.numberOfSimulcastStreams > 1; |
| 374 | } |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 375 | if (vcm_.RegisterSendCodec(&video_codec, number_of_cores_, |
| 376 | max_data_payload_length) != VCM_OK) { |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 377 | return -1; |
| 378 | } |
| 379 | |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 380 | // Set this module as sending right away, let the slave module in the channel |
| 381 | // start and stop sending. |
andresp@webrtc.org | a84b0a6 | 2014-08-14 16:46:46 +0000 | [diff] [blame] | 382 | if (default_rtp_rtcp_->SetSendingStatus(true) != 0) { |
| 383 | return -1; |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 384 | } |
andresp@webrtc.org | a84b0a6 | 2014-08-14 16:46:46 +0000 | [diff] [blame] | 385 | |
pwestin@webrtc.org | 49888ce | 2012-04-27 05:25:53 +0000 | [diff] [blame] | 386 | bitrate_controller_->SetBitrateObserver(bitrate_observer_.get(), |
| 387 | video_codec.startBitrate * 1000, |
| 388 | video_codec.minBitrate * 1000, |
stefan@webrtc.org | bfacda6 | 2013-03-27 16:36:01 +0000 | [diff] [blame] | 389 | kTransmissionMaxBitrateMultiplier * |
pwestin@webrtc.org | 49888ce | 2012-04-27 05:25:53 +0000 | [diff] [blame] | 390 | video_codec.maxBitrate * 1000); |
| 391 | |
pbos@webrtc.org | 3349ae0 | 2014-03-13 12:52:27 +0000 | [diff] [blame] | 392 | CriticalSectionScoped crit(data_cs_.get()); |
| 393 | int pad_up_to_bitrate_kbps = video_codec.startBitrate; |
| 394 | if (pad_up_to_bitrate_kbps < min_transmit_bitrate_kbps_) |
| 395 | pad_up_to_bitrate_kbps = min_transmit_bitrate_kbps_; |
| 396 | |
stefan@webrtc.org | 88e0dda | 2014-07-04 09:20:42 +0000 | [diff] [blame] | 397 | paced_sender_->UpdateBitrate( |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 398 | video_codec.startBitrate, |
stefan@webrtc.org | 88e0dda | 2014-07-04 09:20:42 +0000 | [diff] [blame] | 399 | PacedSender::kDefaultPaceMultiplier * video_codec.startBitrate, |
| 400 | pad_up_to_bitrate_kbps); |
pbos@webrtc.org | 3349ae0 | 2014-03-13 12:52:27 +0000 | [diff] [blame] | 401 | |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 402 | return 0; |
| 403 | } |
| 404 | |
pbos@webrtc.org | b238d12 | 2013-04-09 13:41:51 +0000 | [diff] [blame] | 405 | int32_t ViEEncoder::GetEncoder(VideoCodec* video_codec) { |
mflodman@webrtc.org | f5e99db | 2012-06-27 09:49:37 +0000 | [diff] [blame] | 406 | if (vcm_.SendCodec(video_codec) != 0) { |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 407 | return -1; |
| 408 | } |
| 409 | return 0; |
| 410 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 411 | |
pbos@webrtc.org | b238d12 | 2013-04-09 13:41:51 +0000 | [diff] [blame] | 412 | int32_t ViEEncoder::GetCodecConfigParameters( |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 413 | unsigned char config_parameters[kConfigParameterSize], |
| 414 | unsigned char& config_parameters_size) { |
pbos@webrtc.org | b238d12 | 2013-04-09 13:41:51 +0000 | [diff] [blame] | 415 | int32_t num_parameters = |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 416 | vcm_.CodecConfigParameters(config_parameters, kConfigParameterSize); |
| 417 | if (num_parameters <= 0) { |
| 418 | config_parameters_size = 0; |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 419 | return -1; |
| 420 | } |
| 421 | config_parameters_size = static_cast<unsigned char>(num_parameters); |
| 422 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 423 | } |
| 424 | |
pbos@webrtc.org | b238d12 | 2013-04-09 13:41:51 +0000 | [diff] [blame] | 425 | int32_t ViEEncoder::ScaleInputImage(bool enable) { |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 426 | VideoFrameResampling resampling_mode = kFastRescaling; |
mflodman@webrtc.org | 5574dac | 2014-04-07 10:56:31 +0000 | [diff] [blame] | 427 | // TODO(mflodman) What? |
| 428 | if (enable) { |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 429 | // kInterpolation is currently not supported. |
mflodman@webrtc.org | 5574dac | 2014-04-07 10:56:31 +0000 | [diff] [blame] | 430 | LOG_F(LS_ERROR) << "Not supported."; |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 431 | return -1; |
| 432 | } |
| 433 | vpm_.SetInputFrameResampleMode(resampling_mode); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 434 | |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 435 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 436 | } |
| 437 | |
stefan@webrtc.org | 9b82f5a | 2013-11-13 15:29:21 +0000 | [diff] [blame] | 438 | bool ViEEncoder::TimeToSendPacket(uint32_t ssrc, |
| 439 | uint16_t sequence_number, |
| 440 | int64_t capture_time_ms, |
| 441 | bool retransmission) { |
hclam@chromium.org | 2e402ce | 2013-06-20 20:18:31 +0000 | [diff] [blame] | 442 | return default_rtp_rtcp_->TimeToSendPacket(ssrc, sequence_number, |
stefan@webrtc.org | 9b82f5a | 2013-11-13 15:29:21 +0000 | [diff] [blame] | 443 | capture_time_ms, retransmission); |
pwestin@webrtc.org | 571a1c0 | 2012-11-13 21:12:39 +0000 | [diff] [blame] | 444 | } |
| 445 | |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame^] | 446 | size_t ViEEncoder::TimeToSendPadding(size_t bytes) { |
henrik.lundin@webrtc.org | 331d440 | 2013-11-21 14:05:40 +0000 | [diff] [blame] | 447 | bool send_padding; |
| 448 | { |
| 449 | CriticalSectionScoped cs(data_cs_.get()); |
pbos@webrtc.org | 3349ae0 | 2014-03-13 12:52:27 +0000 | [diff] [blame] | 450 | send_padding = |
| 451 | send_padding_ || video_suspended_ || min_transmit_bitrate_kbps_ > 0; |
henrik.lundin@webrtc.org | 331d440 | 2013-11-21 14:05:40 +0000 | [diff] [blame] | 452 | } |
| 453 | if (send_padding) { |
stefan@webrtc.org | 508a84b | 2013-06-17 12:53:37 +0000 | [diff] [blame] | 454 | return default_rtp_rtcp_->TimeToSendPadding(bytes); |
| 455 | } |
| 456 | return 0; |
| 457 | } |
| 458 | |
stefan@webrtc.org | bfacda6 | 2013-03-27 16:36:01 +0000 | [diff] [blame] | 459 | bool ViEEncoder::EncoderPaused() const { |
pwestin@webrtc.org | 91563e4 | 2013-04-25 22:20:08 +0000 | [diff] [blame] | 460 | // Pause video if paused by caller or as long as the network is down or the |
| 461 | // pacer queue has grown too large in buffered mode. |
| 462 | if (encoder_paused_) { |
| 463 | return true; |
| 464 | } |
| 465 | if (target_delay_ms_ > 0) { |
| 466 | // Buffered mode. |
| 467 | // TODO(pwestin): Workaround until nack is configured as a time and not |
| 468 | // number of packets. |
| 469 | return paced_sender_->QueueInMs() >= |
pwestin@webrtc.org | 52b4e88 | 2013-05-02 19:02:17 +0000 | [diff] [blame] | 470 | std::max(static_cast<int>(target_delay_ms_ * kEncoderPausePacerMargin), |
| 471 | kMinPacingDelayMs); |
pwestin@webrtc.org | 91563e4 | 2013-04-25 22:20:08 +0000 | [diff] [blame] | 472 | } |
sprang@webrtc.org | dcebf2d | 2014-11-04 16:27:16 +0000 | [diff] [blame] | 473 | if (paced_sender_->ExpectedQueueTimeMs() > |
| 474 | PacedSender::kDefaultMaxQueueLengthMs) { |
| 475 | // Too much data in pacer queue, drop frame. |
| 476 | return true; |
| 477 | } |
pwestin@webrtc.org | 91563e4 | 2013-04-25 22:20:08 +0000 | [diff] [blame] | 478 | return !network_is_transmitting_; |
stefan@webrtc.org | bfacda6 | 2013-03-27 16:36:01 +0000 | [diff] [blame] | 479 | } |
| 480 | |
sprang@webrtc.org | dcebf2d | 2014-11-04 16:27:16 +0000 | [diff] [blame] | 481 | void ViEEncoder::TraceFrameDropStart() { |
| 482 | // Start trace event only on the first frame after encoder is paused. |
| 483 | if (!encoder_paused_and_dropped_frame_) { |
| 484 | TRACE_EVENT_ASYNC_BEGIN0("webrtc", "EncoderPaused", this); |
| 485 | } |
| 486 | encoder_paused_and_dropped_frame_ = true; |
| 487 | return; |
| 488 | } |
| 489 | |
| 490 | void ViEEncoder::TraceFrameDropEnd() { |
| 491 | // End trace event on first frame after encoder resumes, if frame was dropped. |
| 492 | if (encoder_paused_and_dropped_frame_) { |
| 493 | TRACE_EVENT_ASYNC_END0("webrtc", "EncoderPaused", this); |
| 494 | } |
| 495 | encoder_paused_and_dropped_frame_ = false; |
| 496 | } |
| 497 | |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 498 | RtpRtcp* ViEEncoder::SendRtpRtcpModule() { |
pwestin@webrtc.org | 2853dde | 2012-05-11 11:08:54 +0000 | [diff] [blame] | 499 | return default_rtp_rtcp_.get(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 500 | } |
| 501 | |
mflodman@webrtc.org | 8baed51 | 2012-06-21 12:11:50 +0000 | [diff] [blame] | 502 | void ViEEncoder::DeliverFrame(int id, |
mikhal@webrtc.org | 9fedff7 | 2012-10-24 18:33:04 +0000 | [diff] [blame] | 503 | I420VideoFrame* video_frame, |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 504 | int num_csrcs, |
pbos@webrtc.org | b238d12 | 2013-04-09 13:41:51 +0000 | [diff] [blame] | 505 | const uint32_t CSRC[kRtpCsrcSize]) { |
wuchengli@chromium.org | ac4b87c | 2014-03-19 03:44:20 +0000 | [diff] [blame] | 506 | if (default_rtp_rtcp_->SendingMedia() == false) { |
| 507 | // We've paused or we have no channels attached, don't encode. |
| 508 | return; |
| 509 | } |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 510 | { |
mflodman@webrtc.org | d32c447 | 2011-12-22 14:17:53 +0000 | [diff] [blame] | 511 | CriticalSectionScoped cs(data_cs_.get()); |
stefan@webrtc.org | 3e00505 | 2013-10-18 15:05:29 +0000 | [diff] [blame] | 512 | time_of_last_incoming_frame_ms_ = TickTime::MillisecondTimestamp(); |
pwestin@webrtc.org | 52b4e88 | 2013-05-02 19:02:17 +0000 | [diff] [blame] | 513 | if (EncoderPaused()) { |
sprang@webrtc.org | dcebf2d | 2014-11-04 16:27:16 +0000 | [diff] [blame] | 514 | TraceFrameDropStart(); |
pwestin@webrtc.org | 52b4e88 | 2013-05-02 19:02:17 +0000 | [diff] [blame] | 515 | return; |
| 516 | } |
sprang@webrtc.org | dcebf2d | 2014-11-04 16:27:16 +0000 | [diff] [blame] | 517 | TraceFrameDropEnd(); |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 518 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 519 | |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 520 | // Convert render time, in ms, to RTP timestamp. |
mflodman@webrtc.org | 8baed51 | 2012-06-21 12:11:50 +0000 | [diff] [blame] | 521 | const int kMsToRtpTimestamp = 90; |
pbos@webrtc.org | b238d12 | 2013-04-09 13:41:51 +0000 | [diff] [blame] | 522 | const uint32_t time_stamp = |
mflodman@webrtc.org | 8baed51 | 2012-06-21 12:11:50 +0000 | [diff] [blame] | 523 | kMsToRtpTimestamp * |
pbos@webrtc.org | b238d12 | 2013-04-09 13:41:51 +0000 | [diff] [blame] | 524 | static_cast<uint32_t>(video_frame->render_time_ms()); |
hclam@chromium.org | 806dc3b | 2013-04-09 19:54:10 +0000 | [diff] [blame] | 525 | |
hclam@chromium.org | 1a7b9b9 | 2013-07-08 21:31:18 +0000 | [diff] [blame] | 526 | TRACE_EVENT_ASYNC_STEP0("webrtc", "Video", video_frame->render_time_ms(), |
| 527 | "Encode"); |
mikhal@webrtc.org | 9fedff7 | 2012-10-24 18:33:04 +0000 | [diff] [blame] | 528 | video_frame->set_timestamp(time_stamp); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 529 | |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 530 | // Make sure the CSRC list is correct. |
| 531 | if (num_csrcs > 0) { |
pbos@webrtc.org | b238d12 | 2013-04-09 13:41:51 +0000 | [diff] [blame] | 532 | uint32_t tempCSRC[kRtpCsrcSize]; |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 533 | for (int i = 0; i < num_csrcs; i++) { |
| 534 | if (CSRC[i] == 1) { |
pwestin@webrtc.org | 2853dde | 2012-05-11 11:08:54 +0000 | [diff] [blame] | 535 | tempCSRC[i] = default_rtp_rtcp_->SSRC(); |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 536 | } else { |
| 537 | tempCSRC[i] = CSRC[i]; |
| 538 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 539 | } |
pbos@webrtc.org | b238d12 | 2013-04-09 13:41:51 +0000 | [diff] [blame] | 540 | default_rtp_rtcp_->SetCSRCs(tempCSRC, (uint8_t) num_csrcs); |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 541 | } |
wuchengli@chromium.org | f425b55 | 2014-06-20 12:04:05 +0000 | [diff] [blame] | 542 | |
pwestin@webrtc.org | 2f476ed | 2012-10-30 16:21:52 +0000 | [diff] [blame] | 543 | I420VideoFrame* decimated_frame = NULL; |
wuchengli@chromium.org | f425b55 | 2014-06-20 12:04:05 +0000 | [diff] [blame] | 544 | // TODO(wuchengli): support texture frames. |
| 545 | if (video_frame->native_handle() == NULL) { |
| 546 | { |
| 547 | CriticalSectionScoped cs(callback_cs_.get()); |
| 548 | if (effect_filter_) { |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame^] | 549 | size_t length = |
wuchengli@chromium.org | f425b55 | 2014-06-20 12:04:05 +0000 | [diff] [blame] | 550 | CalcBufferSize(kI420, video_frame->width(), video_frame->height()); |
| 551 | scoped_ptr<uint8_t[]> video_buffer(new uint8_t[length]); |
| 552 | ExtractBuffer(*video_frame, length, video_buffer.get()); |
| 553 | effect_filter_->Transform(length, |
| 554 | video_buffer.get(), |
| 555 | video_frame->ntp_time_ms(), |
| 556 | video_frame->timestamp(), |
| 557 | video_frame->width(), |
| 558 | video_frame->height()); |
| 559 | } |
| 560 | } |
| 561 | |
| 562 | // Pass frame via preprocessor. |
| 563 | const int ret = vpm_.PreprocessFrame(*video_frame, &decimated_frame); |
| 564 | if (ret == 1) { |
| 565 | // Drop this frame. |
| 566 | return; |
| 567 | } |
| 568 | if (ret != VPM_OK) { |
| 569 | return; |
| 570 | } |
pwestin@webrtc.org | 2f476ed | 2012-10-30 16:21:52 +0000 | [diff] [blame] | 571 | } |
wuchengli@chromium.org | f425b55 | 2014-06-20 12:04:05 +0000 | [diff] [blame] | 572 | // If the frame was not resampled or scaled => use original. |
pwestin@webrtc.org | 2f476ed | 2012-10-30 16:21:52 +0000 | [diff] [blame] | 573 | if (decimated_frame == NULL) { |
| 574 | decimated_frame = video_frame; |
| 575 | } |
pbos@webrtc.org | fe1ef93 | 2013-10-21 10:34:43 +0000 | [diff] [blame] | 576 | |
| 577 | { |
| 578 | CriticalSectionScoped cs(callback_cs_.get()); |
| 579 | if (pre_encode_callback_) |
| 580 | pre_encode_callback_->FrameCallback(decimated_frame); |
| 581 | } |
| 582 | |
wuchengli@chromium.org | f425b55 | 2014-06-20 12:04:05 +0000 | [diff] [blame] | 583 | if (video_frame->native_handle() != NULL) { |
| 584 | // TODO(wuchengli): add texture support. http://crbug.com/362437 |
| 585 | return; |
| 586 | } |
| 587 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 588 | #ifdef VIDEOCODEC_VP8 |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 589 | if (vcm_.SendCodec() == webrtc::kVideoCodecVP8) { |
| 590 | webrtc::CodecSpecificInfo codec_specific_info; |
| 591 | codec_specific_info.codecType = webrtc::kVideoCodecVP8; |
stefan@webrtc.org | 7af12be | 2014-07-09 14:46:31 +0000 | [diff] [blame] | 592 | { |
| 593 | CriticalSectionScoped cs(data_cs_.get()); |
| 594 | codec_specific_info.codecSpecific.VP8.hasReceivedRPSI = |
| 595 | has_received_rpsi_; |
| 596 | codec_specific_info.codecSpecific.VP8.hasReceivedSLI = |
| 597 | has_received_sli_; |
| 598 | codec_specific_info.codecSpecific.VP8.pictureIdRPSI = |
| 599 | picture_id_rpsi_; |
| 600 | codec_specific_info.codecSpecific.VP8.pictureIdSLI = |
| 601 | picture_id_sli_; |
| 602 | has_received_sli_ = false; |
| 603 | has_received_rpsi_ = false; |
| 604 | } |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 605 | |
mflodman@webrtc.org | 5574dac | 2014-04-07 10:56:31 +0000 | [diff] [blame] | 606 | vcm_.AddVideoFrame(*decimated_frame, vpm_.ContentMetrics(), |
| 607 | &codec_specific_info); |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 608 | return; |
| 609 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 610 | #endif |
mflodman@webrtc.org | 5574dac | 2014-04-07 10:56:31 +0000 | [diff] [blame] | 611 | vcm_.AddVideoFrame(*decimated_frame); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 612 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 613 | |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 614 | void ViEEncoder::DelayChanged(int id, int frame_delay) { |
stefan@webrtc.org | 7da3459 | 2013-04-09 14:56:29 +0000 | [diff] [blame] | 615 | default_rtp_rtcp_->SetCameraDelay(frame_delay); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 616 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 617 | |
mflodman@webrtc.org | 8baed51 | 2012-06-21 12:11:50 +0000 | [diff] [blame] | 618 | int ViEEncoder::GetPreferedFrameSettings(int* width, |
| 619 | int* height, |
| 620 | int* frame_rate) { |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 621 | webrtc::VideoCodec video_codec; |
| 622 | memset(&video_codec, 0, sizeof(video_codec)); |
| 623 | if (vcm_.SendCodec(&video_codec) != VCM_OK) { |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 624 | return -1; |
| 625 | } |
| 626 | |
mflodman@webrtc.org | 8baed51 | 2012-06-21 12:11:50 +0000 | [diff] [blame] | 627 | *width = video_codec.width; |
| 628 | *height = video_codec.height; |
| 629 | *frame_rate = video_codec.maxFramerate; |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 630 | return 0; |
| 631 | } |
| 632 | |
pwestin@webrtc.org | ce33035 | 2012-04-12 06:59:14 +0000 | [diff] [blame] | 633 | int ViEEncoder::SendKeyFrame() { |
stefan@webrtc.org | c530043 | 2012-10-08 07:06:53 +0000 | [diff] [blame] | 634 | return vcm_.IntraFrameRequest(0); |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 635 | } |
| 636 | |
pbos@webrtc.org | b238d12 | 2013-04-09 13:41:51 +0000 | [diff] [blame] | 637 | int32_t ViEEncoder::SendCodecStatistics( |
| 638 | uint32_t* num_key_frames, uint32_t* num_delta_frames) { |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 639 | webrtc::VCMFrameCount sent_frames; |
| 640 | if (vcm_.SentFrameCount(sent_frames) != VCM_OK) { |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 641 | return -1; |
| 642 | } |
mflodman@webrtc.org | f5e99db | 2012-06-27 09:49:37 +0000 | [diff] [blame] | 643 | *num_key_frames = sent_frames.numKeyFrames; |
| 644 | *num_delta_frames = sent_frames.numDeltaFrames; |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 645 | return 0; |
| 646 | } |
| 647 | |
jiayl@webrtc.org | 9fd8d87 | 2014-02-27 22:32:40 +0000 | [diff] [blame] | 648 | int32_t ViEEncoder::PacerQueuingDelayMs() const { |
| 649 | return paced_sender_->QueueInMs(); |
| 650 | } |
| 651 | |
pbos@webrtc.org | b238d12 | 2013-04-09 13:41:51 +0000 | [diff] [blame] | 652 | int ViEEncoder::CodecTargetBitrate(uint32_t* bitrate) const { |
stefan@webrtc.org | 439be29 | 2012-02-16 14:45:37 +0000 | [diff] [blame] | 653 | if (vcm_.Bitrate(bitrate) != 0) |
| 654 | return -1; |
| 655 | return 0; |
stefan@webrtc.org | 07b45a5 | 2012-02-02 08:37:48 +0000 | [diff] [blame] | 656 | } |
| 657 | |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 658 | int32_t ViEEncoder::UpdateProtectionMethod(bool enable_nack) { |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 659 | bool fec_enabled = false; |
pbos@webrtc.org | b238d12 | 2013-04-09 13:41:51 +0000 | [diff] [blame] | 660 | uint8_t dummy_ptype_red = 0; |
| 661 | uint8_t dummy_ptypeFEC = 0; |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 662 | |
| 663 | // Updated protection method to VCM to get correct packetization sizes. |
| 664 | // FEC has larger overhead than NACK -> set FEC if used. |
pbos@webrtc.org | b238d12 | 2013-04-09 13:41:51 +0000 | [diff] [blame] | 665 | int32_t error = default_rtp_rtcp_->GenericFECStatus(fec_enabled, |
| 666 | dummy_ptype_red, |
| 667 | dummy_ptypeFEC); |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 668 | if (error) { |
| 669 | return -1; |
| 670 | } |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 671 | if (fec_enabled_ == fec_enabled && nack_enabled_ == enable_nack) { |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 672 | // No change needed, we're already in correct state. |
| 673 | return 0; |
| 674 | } |
| 675 | fec_enabled_ = fec_enabled; |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 676 | nack_enabled_ = enable_nack; |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 677 | |
| 678 | // Set Video Protection for VCM. |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 679 | if (fec_enabled && nack_enabled_) { |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 680 | vcm_.SetVideoProtection(webrtc::kProtectionNackFEC, true); |
| 681 | } else { |
| 682 | vcm_.SetVideoProtection(webrtc::kProtectionFEC, fec_enabled_); |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 683 | vcm_.SetVideoProtection(webrtc::kProtectionNackSender, nack_enabled_); |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 684 | vcm_.SetVideoProtection(webrtc::kProtectionNackFEC, false); |
| 685 | } |
| 686 | |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 687 | if (fec_enabled_ || nack_enabled_) { |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 688 | vcm_.RegisterProtectionCallback(this); |
| 689 | // The send codec must be registered to set correct MTU. |
| 690 | webrtc::VideoCodec codec; |
| 691 | if (vcm_.SendCodec(&codec) == 0) { |
pbos@webrtc.org | b238d12 | 2013-04-09 13:41:51 +0000 | [diff] [blame] | 692 | uint16_t max_pay_load = default_rtp_rtcp_->MaxDataPayloadLength(); |
stefan@webrtc.org | 3d0b0d6 | 2013-03-19 10:04:57 +0000 | [diff] [blame] | 693 | uint32_t current_bitrate_bps = 0; |
| 694 | if (vcm_.Bitrate(¤t_bitrate_bps) != 0) { |
mflodman@webrtc.org | 5574dac | 2014-04-07 10:56:31 +0000 | [diff] [blame] | 695 | LOG_F(LS_WARNING) << |
| 696 | "Failed to get the current encoder target bitrate."; |
stefan@webrtc.org | 439be29 | 2012-02-16 14:45:37 +0000 | [diff] [blame] | 697 | } |
stefan@webrtc.org | 3d0b0d6 | 2013-03-19 10:04:57 +0000 | [diff] [blame] | 698 | // Convert to start bitrate in kbps. |
| 699 | codec.startBitrate = (current_bitrate_bps + 500) / 1000; |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 700 | if (vcm_.RegisterSendCodec(&codec, number_of_cores_, max_pay_load) != 0) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 701 | return -1; |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 702 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 703 | } |
| 704 | return 0; |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 705 | } else { |
| 706 | // FEC and NACK are disabled. |
| 707 | vcm_.RegisterProtectionCallback(NULL); |
| 708 | } |
| 709 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 710 | } |
| 711 | |
mikhal@webrtc.org | ef9f76a | 2013-02-15 23:22:18 +0000 | [diff] [blame] | 712 | void ViEEncoder::SetSenderBufferingMode(int target_delay_ms) { |
stefan@webrtc.org | bfacda6 | 2013-03-27 16:36:01 +0000 | [diff] [blame] | 713 | { |
| 714 | CriticalSectionScoped cs(data_cs_.get()); |
| 715 | target_delay_ms_ = target_delay_ms; |
| 716 | } |
mikhal@webrtc.org | 3d305c6 | 2013-02-10 18:42:55 +0000 | [diff] [blame] | 717 | if (target_delay_ms > 0) { |
stefan@webrtc.org | bfacda6 | 2013-03-27 16:36:01 +0000 | [diff] [blame] | 718 | // Disable external frame-droppers. |
| 719 | vcm_.EnableFrameDropper(false); |
| 720 | vpm_.EnableTemporalDecimation(false); |
mikhal@webrtc.org | 3d305c6 | 2013-02-10 18:42:55 +0000 | [diff] [blame] | 721 | } else { |
mikhal@webrtc.org | ef9f76a | 2013-02-15 23:22:18 +0000 | [diff] [blame] | 722 | // Real-time mode - enable frame droppers. |
mikhal@webrtc.org | 3d305c6 | 2013-02-10 18:42:55 +0000 | [diff] [blame] | 723 | vpm_.EnableTemporalDecimation(true); |
| 724 | vcm_.EnableFrameDropper(true); |
| 725 | } |
| 726 | } |
| 727 | |
pbos@webrtc.org | b238d12 | 2013-04-09 13:41:51 +0000 | [diff] [blame] | 728 | int32_t ViEEncoder::SendData( |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 729 | const FrameType frame_type, |
pbos@webrtc.org | b238d12 | 2013-04-09 13:41:51 +0000 | [diff] [blame] | 730 | const uint8_t payload_type, |
| 731 | const uint32_t time_stamp, |
stefan@webrtc.org | ddfdfed | 2012-07-03 13:21:22 +0000 | [diff] [blame] | 732 | int64_t capture_time_ms, |
pbos@webrtc.org | b238d12 | 2013-04-09 13:41:51 +0000 | [diff] [blame] | 733 | const uint8_t* payload_data, |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame^] | 734 | const size_t payload_size, |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 735 | const webrtc::RTPFragmentationHeader& fragmentation_header, |
| 736 | const RTPVideoHeader* rtp_video_hdr) { |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 737 | // New encoded data, hand over to the rtp module. |
stefan@webrtc.org | ddfdfed | 2012-07-03 13:21:22 +0000 | [diff] [blame] | 738 | return default_rtp_rtcp_->SendOutgoingData(frame_type, |
| 739 | payload_type, |
| 740 | time_stamp, |
| 741 | capture_time_ms, |
| 742 | payload_data, |
pwestin@webrtc.org | 2853dde | 2012-05-11 11:08:54 +0000 | [diff] [blame] | 743 | payload_size, |
| 744 | &fragmentation_header, |
| 745 | rtp_video_hdr); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 746 | } |
| 747 | |
pbos@webrtc.org | b238d12 | 2013-04-09 13:41:51 +0000 | [diff] [blame] | 748 | int32_t ViEEncoder::ProtectionRequest( |
stefan@webrtc.org | e0d6fa4 | 2012-03-20 22:10:56 +0000 | [diff] [blame] | 749 | const FecProtectionParams* delta_fec_params, |
| 750 | const FecProtectionParams* key_fec_params, |
pbos@webrtc.org | b238d12 | 2013-04-09 13:41:51 +0000 | [diff] [blame] | 751 | uint32_t* sent_video_rate_bps, |
| 752 | uint32_t* sent_nack_rate_bps, |
| 753 | uint32_t* sent_fec_rate_bps) { |
mflodman@webrtc.org | 5574dac | 2014-04-07 10:56:31 +0000 | [diff] [blame] | 754 | default_rtp_rtcp_->SetFecParameters(delta_fec_params, key_fec_params); |
| 755 | default_rtp_rtcp_->BitrateSent(NULL, sent_video_rate_bps, sent_fec_rate_bps, |
stefan@webrtc.org | f4c8286 | 2011-12-13 15:38:14 +0000 | [diff] [blame] | 756 | sent_nack_rate_bps); |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 757 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 758 | } |
| 759 | |
pbos@webrtc.org | b238d12 | 2013-04-09 13:41:51 +0000 | [diff] [blame] | 760 | int32_t ViEEncoder::SendStatistics(const uint32_t bit_rate, |
| 761 | const uint32_t frame_rate) { |
mflodman@webrtc.org | d32c447 | 2011-12-22 14:17:53 +0000 | [diff] [blame] | 762 | CriticalSectionScoped cs(callback_cs_.get()); |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 763 | if (codec_observer_) { |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 764 | codec_observer_->OutgoingRate(channel_id_, frame_rate, bit_rate); |
| 765 | } |
| 766 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 767 | } |
| 768 | |
pbos@webrtc.org | b238d12 | 2013-04-09 13:41:51 +0000 | [diff] [blame] | 769 | int32_t ViEEncoder::RegisterCodecObserver(ViEEncoderObserver* observer) { |
mflodman@webrtc.org | d32c447 | 2011-12-22 14:17:53 +0000 | [diff] [blame] | 770 | CriticalSectionScoped cs(callback_cs_.get()); |
mflodman@webrtc.org | 5574dac | 2014-04-07 10:56:31 +0000 | [diff] [blame] | 771 | if (observer && codec_observer_) { |
| 772 | LOG_F(LS_ERROR) << "Observer already set."; |
| 773 | return -1; |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 774 | } |
mflodman@webrtc.org | 5574dac | 2014-04-07 10:56:31 +0000 | [diff] [blame] | 775 | codec_observer_ = observer; |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 776 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 777 | } |
| 778 | |
andrew@webrtc.org | 9663686 | 2012-09-20 23:33:17 +0000 | [diff] [blame] | 779 | void ViEEncoder::OnReceivedSLI(uint32_t /*ssrc*/, |
| 780 | uint8_t picture_id) { |
stefan@webrtc.org | 7af12be | 2014-07-09 14:46:31 +0000 | [diff] [blame] | 781 | CriticalSectionScoped cs(data_cs_.get()); |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 782 | picture_id_sli_ = picture_id; |
| 783 | has_received_sli_ = true; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 784 | } |
| 785 | |
andrew@webrtc.org | 9663686 | 2012-09-20 23:33:17 +0000 | [diff] [blame] | 786 | void ViEEncoder::OnReceivedRPSI(uint32_t /*ssrc*/, |
| 787 | uint64_t picture_id) { |
stefan@webrtc.org | 7af12be | 2014-07-09 14:46:31 +0000 | [diff] [blame] | 788 | CriticalSectionScoped cs(data_cs_.get()); |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 789 | picture_id_rpsi_ = picture_id; |
| 790 | has_received_rpsi_ = true; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 791 | } |
| 792 | |
mflodman@webrtc.org | d6ec386 | 2012-10-25 11:30:29 +0000 | [diff] [blame] | 793 | void ViEEncoder::OnReceivedIntraFrameRequest(uint32_t ssrc) { |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 794 | // Key frame request from remote side, signal to VCM. |
justinlin@chromium.org | 7bfb3a3 | 2013-05-13 22:59:00 +0000 | [diff] [blame] | 795 | TRACE_EVENT0("webrtc", "OnKeyFrameRequest"); |
pwestin@webrtc.org | 1da1ce0 | 2011-10-13 15:19:55 +0000 | [diff] [blame] | 796 | |
mflodman@webrtc.org | d6ec386 | 2012-10-25 11:30:29 +0000 | [diff] [blame] | 797 | int idx = 0; |
| 798 | { |
| 799 | CriticalSectionScoped cs(data_cs_.get()); |
| 800 | std::map<unsigned int, int>::iterator stream_it = ssrc_streams_.find(ssrc); |
| 801 | if (stream_it == ssrc_streams_.end()) { |
mflodman@webrtc.org | d73527c | 2012-12-20 09:26:17 +0000 | [diff] [blame] | 802 | LOG_F(LS_WARNING) << "ssrc not found: " << ssrc << ", map size " |
| 803 | << ssrc_streams_.size(); |
mflodman@webrtc.org | d6ec386 | 2012-10-25 11:30:29 +0000 | [diff] [blame] | 804 | return; |
| 805 | } |
pbos@webrtc.org | b238d12 | 2013-04-09 13:41:51 +0000 | [diff] [blame] | 806 | std::map<unsigned int, int64_t>::iterator time_it = |
mflodman@webrtc.org | d6ec386 | 2012-10-25 11:30:29 +0000 | [diff] [blame] | 807 | time_last_intra_request_ms_.find(ssrc); |
| 808 | if (time_it == time_last_intra_request_ms_.end()) { |
| 809 | time_last_intra_request_ms_[ssrc] = 0; |
| 810 | } |
| 811 | |
pbos@webrtc.org | b238d12 | 2013-04-09 13:41:51 +0000 | [diff] [blame] | 812 | int64_t now = TickTime::MillisecondTimestamp(); |
mflodman@webrtc.org | d6ec386 | 2012-10-25 11:30:29 +0000 | [diff] [blame] | 813 | if (time_last_intra_request_ms_[ssrc] + kViEMinKeyRequestIntervalMs > now) { |
mflodman@webrtc.org | d6ec386 | 2012-10-25 11:30:29 +0000 | [diff] [blame] | 814 | return; |
| 815 | } |
| 816 | time_last_intra_request_ms_[ssrc] = now; |
| 817 | idx = stream_it->second; |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 818 | } |
mflodman@webrtc.org | d6ec386 | 2012-10-25 11:30:29 +0000 | [diff] [blame] | 819 | // Release the critsect before triggering key frame. |
| 820 | vcm_.IntraFrameRequest(idx); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 821 | } |
| 822 | |
mflodman@webrtc.org | aca2629 | 2012-10-05 16:17:41 +0000 | [diff] [blame] | 823 | void ViEEncoder::OnLocalSsrcChanged(uint32_t old_ssrc, uint32_t new_ssrc) { |
mflodman@webrtc.org | d6ec386 | 2012-10-25 11:30:29 +0000 | [diff] [blame] | 824 | CriticalSectionScoped cs(data_cs_.get()); |
| 825 | std::map<unsigned int, int>::iterator it = ssrc_streams_.find(old_ssrc); |
| 826 | if (it == ssrc_streams_.end()) { |
| 827 | return; |
| 828 | } |
| 829 | |
| 830 | ssrc_streams_[new_ssrc] = it->second; |
| 831 | ssrc_streams_.erase(it); |
| 832 | |
| 833 | std::map<unsigned int, int64_t>::iterator time_it = |
| 834 | time_last_intra_request_ms_.find(old_ssrc); |
| 835 | int64_t last_intra_request_ms = 0; |
| 836 | if (time_it != time_last_intra_request_ms_.end()) { |
| 837 | last_intra_request_ms = time_it->second; |
| 838 | time_last_intra_request_ms_.erase(time_it); |
| 839 | } |
| 840 | time_last_intra_request_ms_[new_ssrc] = last_intra_request_ms; |
| 841 | } |
| 842 | |
| 843 | bool ViEEncoder::SetSsrcs(const std::list<unsigned int>& ssrcs) { |
| 844 | VideoCodec codec; |
| 845 | if (vcm_.SendCodec(&codec) != 0) |
| 846 | return false; |
| 847 | |
| 848 | if (codec.numberOfSimulcastStreams > 0 && |
| 849 | ssrcs.size() != codec.numberOfSimulcastStreams) { |
| 850 | return false; |
| 851 | } |
| 852 | |
| 853 | CriticalSectionScoped cs(data_cs_.get()); |
| 854 | ssrc_streams_.clear(); |
| 855 | time_last_intra_request_ms_.clear(); |
| 856 | int idx = 0; |
| 857 | for (std::list<unsigned int>::const_iterator it = ssrcs.begin(); |
| 858 | it != ssrcs.end(); ++it, ++idx) { |
| 859 | unsigned int ssrc = *it; |
| 860 | ssrc_streams_[ssrc] = idx; |
| 861 | } |
| 862 | return true; |
mflodman@webrtc.org | aca2629 | 2012-10-05 16:17:41 +0000 | [diff] [blame] | 863 | } |
| 864 | |
pbos@webrtc.org | 3349ae0 | 2014-03-13 12:52:27 +0000 | [diff] [blame] | 865 | void ViEEncoder::SetMinTransmitBitrate(int min_transmit_bitrate_kbps) { |
| 866 | assert(min_transmit_bitrate_kbps >= 0); |
| 867 | CriticalSectionScoped crit(data_cs_.get()); |
| 868 | min_transmit_bitrate_kbps_ = min_transmit_bitrate_kbps; |
| 869 | } |
| 870 | |
pwestin@webrtc.org | 49888ce | 2012-04-27 05:25:53 +0000 | [diff] [blame] | 871 | // Called from ViEBitrateObserver. |
| 872 | void ViEEncoder::OnNetworkChanged(const uint32_t bitrate_bps, |
| 873 | const uint8_t fraction_lost, |
| 874 | const uint32_t round_trip_time_ms) { |
mflodman@webrtc.org | 5574dac | 2014-04-07 10:56:31 +0000 | [diff] [blame] | 875 | LOG(LS_VERBOSE) << "OnNetworkChanged, bitrate" << bitrate_bps |
| 876 | << " packet loss " << fraction_lost |
| 877 | << " rtt " << round_trip_time_ms; |
stefan@webrtc.org | abc9d5b | 2013-03-18 17:00:51 +0000 | [diff] [blame] | 878 | vcm_.SetChannelParameters(bitrate_bps, fraction_lost, round_trip_time_ms); |
henrik.lundin@webrtc.org | ce8e093 | 2013-11-18 12:18:43 +0000 | [diff] [blame] | 879 | bool video_is_suspended = vcm_.VideoSuspended(); |
pwestin@webrtc.org | 571a1c0 | 2012-11-13 21:12:39 +0000 | [diff] [blame] | 880 | int bitrate_kbps = bitrate_bps / 1000; |
stefan@webrtc.org | 508a84b | 2013-06-17 12:53:37 +0000 | [diff] [blame] | 881 | VideoCodec send_codec; |
| 882 | if (vcm_.SendCodec(&send_codec) != 0) { |
| 883 | return; |
| 884 | } |
stefan@webrtc.org | b2c8a95 | 2013-09-06 13:58:01 +0000 | [diff] [blame] | 885 | SimulcastStream* stream_configs = send_codec.simulcastStream; |
| 886 | // Allocate the bandwidth between the streams. |
| 887 | std::vector<uint32_t> stream_bitrates = AllocateStreamBitrates( |
| 888 | bitrate_bps, |
| 889 | stream_configs, |
| 890 | send_codec.numberOfSimulcastStreams); |
| 891 | // Find the max amount of padding we can allow ourselves to send at this |
| 892 | // point, based on which streams are currently active and what our current |
| 893 | // available bandwidth is. |
stefan@webrtc.org | b400aa7 | 2013-10-16 13:03:10 +0000 | [diff] [blame] | 894 | int pad_up_to_bitrate_kbps = 0; |
| 895 | if (send_codec.numberOfSimulcastStreams == 0) { |
stefan@webrtc.org | b400aa7 | 2013-10-16 13:03:10 +0000 | [diff] [blame] | 896 | pad_up_to_bitrate_kbps = send_codec.minBitrate; |
| 897 | } else { |
stefan@webrtc.org | b400aa7 | 2013-10-16 13:03:10 +0000 | [diff] [blame] | 898 | pad_up_to_bitrate_kbps = |
| 899 | stream_configs[send_codec.numberOfSimulcastStreams - 1].minBitrate; |
| 900 | for (int i = 0; i < send_codec.numberOfSimulcastStreams - 1; ++i) { |
| 901 | pad_up_to_bitrate_kbps += stream_configs[i].targetBitrate; |
| 902 | } |
stefan@webrtc.org | b2c8a95 | 2013-09-06 13:58:01 +0000 | [diff] [blame] | 903 | } |
pbos@webrtc.org | 709e297 | 2014-03-19 10:59:52 +0000 | [diff] [blame] | 904 | |
| 905 | // Disable padding if only sending one stream and video isn't suspended and |
| 906 | // min-transmit bitrate isn't used (applied later). |
| 907 | if (!video_is_suspended && send_codec.numberOfSimulcastStreams <= 1) |
stefan@webrtc.org | b400aa7 | 2013-10-16 13:03:10 +0000 | [diff] [blame] | 908 | pad_up_to_bitrate_kbps = 0; |
stefan@webrtc.org | 3e00505 | 2013-10-18 15:05:29 +0000 | [diff] [blame] | 909 | |
| 910 | { |
stefan@webrtc.org | 3e00505 | 2013-10-18 15:05:29 +0000 | [diff] [blame] | 911 | CriticalSectionScoped cs(data_cs_.get()); |
pbos@webrtc.org | 709e297 | 2014-03-19 10:59:52 +0000 | [diff] [blame] | 912 | // The amount of padding should decay to zero if no frames are being |
| 913 | // captured unless a min-transmit bitrate is used. |
stefan@webrtc.org | 3e00505 | 2013-10-18 15:05:29 +0000 | [diff] [blame] | 914 | int64_t now_ms = TickTime::MillisecondTimestamp(); |
| 915 | if (now_ms - time_of_last_incoming_frame_ms_ > kStopPaddingThresholdMs) |
pbos@webrtc.org | 709e297 | 2014-03-19 10:59:52 +0000 | [diff] [blame] | 916 | pad_up_to_bitrate_kbps = 0; |
stefan@webrtc.org | 3e00505 | 2013-10-18 15:05:29 +0000 | [diff] [blame] | 917 | |
pbos@webrtc.org | 709e297 | 2014-03-19 10:59:52 +0000 | [diff] [blame] | 918 | // Pad up to min bitrate. |
pbos@webrtc.org | 3349ae0 | 2014-03-13 12:52:27 +0000 | [diff] [blame] | 919 | if (pad_up_to_bitrate_kbps < min_transmit_bitrate_kbps_) |
| 920 | pad_up_to_bitrate_kbps = min_transmit_bitrate_kbps_; |
pbos@webrtc.org | 709e297 | 2014-03-19 10:59:52 +0000 | [diff] [blame] | 921 | |
| 922 | // Padding may never exceed bitrate estimate. |
| 923 | if (pad_up_to_bitrate_kbps > bitrate_kbps) |
| 924 | pad_up_to_bitrate_kbps = bitrate_kbps; |
| 925 | |
stefan@webrtc.org | 88e0dda | 2014-07-04 09:20:42 +0000 | [diff] [blame] | 926 | paced_sender_->UpdateBitrate( |
stefan@webrtc.org | 82462aa | 2014-10-23 11:57:05 +0000 | [diff] [blame] | 927 | bitrate_kbps, |
stefan@webrtc.org | 88e0dda | 2014-07-04 09:20:42 +0000 | [diff] [blame] | 928 | PacedSender::kDefaultPaceMultiplier * bitrate_kbps, |
| 929 | pad_up_to_bitrate_kbps); |
pbos@webrtc.org | 3349ae0 | 2014-03-13 12:52:27 +0000 | [diff] [blame] | 930 | default_rtp_rtcp_->SetTargetSendBitrate(stream_bitrates); |
pbos@webrtc.org | 484ee96 | 2013-11-21 18:44:23 +0000 | [diff] [blame] | 931 | if (video_suspended_ == video_is_suspended) |
| 932 | return; |
| 933 | video_suspended_ = video_is_suspended; |
| 934 | } |
pbos@webrtc.org | 709e297 | 2014-03-19 10:59:52 +0000 | [diff] [blame] | 935 | |
| 936 | // Video suspend-state changed, inform codec observer. |
pbos@webrtc.org | 3349ae0 | 2014-03-13 12:52:27 +0000 | [diff] [blame] | 937 | CriticalSectionScoped crit(callback_cs_.get()); |
pbos@webrtc.org | 484ee96 | 2013-11-21 18:44:23 +0000 | [diff] [blame] | 938 | if (codec_observer_) { |
mflodman@webrtc.org | 5574dac | 2014-04-07 10:56:31 +0000 | [diff] [blame] | 939 | LOG(LS_INFO) << "Video suspended " << video_is_suspended |
| 940 | << " for channel " << channel_id_; |
henrik.lundin@webrtc.org | 9fe3603 | 2013-11-21 23:00:40 +0000 | [diff] [blame] | 941 | codec_observer_->SuspendChange(channel_id_, video_is_suspended); |
henrik.lundin@webrtc.org | 7ea4f24 | 2013-10-02 13:34:26 +0000 | [diff] [blame] | 942 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 943 | } |
| 944 | |
pwestin@webrtc.org | 571a1c0 | 2012-11-13 21:12:39 +0000 | [diff] [blame] | 945 | PacedSender* ViEEncoder::GetPacedSender() { |
| 946 | return paced_sender_.get(); |
| 947 | } |
| 948 | |
pbos@webrtc.org | b238d12 | 2013-04-09 13:41:51 +0000 | [diff] [blame] | 949 | int32_t ViEEncoder::RegisterEffectFilter(ViEEffectFilter* effect_filter) { |
mflodman@webrtc.org | d32c447 | 2011-12-22 14:17:53 +0000 | [diff] [blame] | 950 | CriticalSectionScoped cs(callback_cs_.get()); |
mflodman@webrtc.org | 5574dac | 2014-04-07 10:56:31 +0000 | [diff] [blame] | 951 | if (effect_filter != NULL && effect_filter_ != NULL) { |
| 952 | LOG_F(LS_ERROR) << "Filter already set."; |
| 953 | return -1; |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 954 | } |
| 955 | effect_filter_ = effect_filter; |
| 956 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 957 | } |
| 958 | |
mikhal@webrtc.org | e41bbdf | 2012-08-28 16:15:16 +0000 | [diff] [blame] | 959 | int ViEEncoder::StartDebugRecording(const char* fileNameUTF8) { |
| 960 | return vcm_.StartDebugRecording(fileNameUTF8); |
| 961 | } |
| 962 | |
| 963 | int ViEEncoder::StopDebugRecording() { |
| 964 | return vcm_.StopDebugRecording(); |
| 965 | } |
| 966 | |
henrik.lundin@webrtc.org | ce8e093 | 2013-11-18 12:18:43 +0000 | [diff] [blame] | 967 | void ViEEncoder::SuspendBelowMinBitrate() { |
| 968 | vcm_.SuspendBelowMinBitrate(); |
henrik.lundin@webrtc.org | 1a3a6e5 | 2013-10-28 10:16:14 +0000 | [diff] [blame] | 969 | bitrate_controller_->EnforceMinBitrate(false); |
henrik.lundin@webrtc.org | 7ea4f24 | 2013-10-02 13:34:26 +0000 | [diff] [blame] | 970 | } |
| 971 | |
pbos@webrtc.org | fe1ef93 | 2013-10-21 10:34:43 +0000 | [diff] [blame] | 972 | void ViEEncoder::RegisterPreEncodeCallback( |
| 973 | I420FrameCallback* pre_encode_callback) { |
| 974 | CriticalSectionScoped cs(callback_cs_.get()); |
| 975 | pre_encode_callback_ = pre_encode_callback; |
| 976 | } |
| 977 | |
| 978 | void ViEEncoder::DeRegisterPreEncodeCallback() { |
| 979 | CriticalSectionScoped cs(callback_cs_.get()); |
| 980 | pre_encode_callback_ = NULL; |
| 981 | } |
| 982 | |
sprang@webrtc.org | 4070935 | 2013-11-26 11:41:59 +0000 | [diff] [blame] | 983 | void ViEEncoder::RegisterPostEncodeImageCallback( |
| 984 | EncodedImageCallback* post_encode_callback) { |
| 985 | vcm_.RegisterPostEncodeImageCallback(post_encode_callback); |
| 986 | } |
| 987 | |
| 988 | void ViEEncoder::DeRegisterPostEncodeImageCallback() { |
| 989 | vcm_.RegisterPostEncodeImageCallback(NULL); |
| 990 | } |
| 991 | |
marpan@webrtc.org | efd01fd | 2012-04-18 15:56:34 +0000 | [diff] [blame] | 992 | QMVideoSettingsCallback::QMVideoSettingsCallback(VideoProcessingModule* vpm) |
| 993 | : vpm_(vpm) { |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 994 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 995 | |
stefan@webrtc.org | 439be29 | 2012-02-16 14:45:37 +0000 | [diff] [blame] | 996 | QMVideoSettingsCallback::~QMVideoSettingsCallback() { |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 997 | } |
| 998 | |
pbos@webrtc.org | b238d12 | 2013-04-09 13:41:51 +0000 | [diff] [blame] | 999 | int32_t QMVideoSettingsCallback::SetVideoQMSettings( |
| 1000 | const uint32_t frame_rate, |
| 1001 | const uint32_t width, |
| 1002 | const uint32_t height) { |
marpan@webrtc.org | cf706c2 | 2012-03-27 21:04:13 +0000 | [diff] [blame] | 1003 | return vpm_->SetTargetResolution(width, height, frame_rate); |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 1004 | } |
| 1005 | |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 1006 | } // namespace webrtc |