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