pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. |
| 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 | 16e03b7 | 2013-10-28 16:32:01 +0000 | [diff] [blame] | 11 | #include "webrtc/video/video_send_stream.h" |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 12 | |
pbos@webrtc.org | dde16f1 | 2014-08-05 23:35:43 +0000 | [diff] [blame] | 13 | #include <algorithm> |
pbos@webrtc.org | 1e92b0a | 2014-05-15 09:35:06 +0000 | [diff] [blame] | 14 | #include <sstream> |
henrik.lundin@webrtc.org | ba975e2 | 2013-10-23 11:04:57 +0000 | [diff] [blame] | 15 | #include <string> |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 16 | #include <vector> |
| 17 | |
| 18 | #include "webrtc/common_video/libyuv/include/webrtc_libyuv.h" |
pbos@webrtc.org | 6ae48c6 | 2014-06-06 10:49:19 +0000 | [diff] [blame] | 19 | #include "webrtc/system_wrappers/interface/logging.h" |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 20 | #include "webrtc/video_engine/include/vie_base.h" |
| 21 | #include "webrtc/video_engine/include/vie_capture.h" |
| 22 | #include "webrtc/video_engine/include/vie_codec.h" |
stefan@webrtc.org | 360e376 | 2013-08-22 09:29:56 +0000 | [diff] [blame] | 23 | #include "webrtc/video_engine/include/vie_external_codec.h" |
pbos@webrtc.org | fe1ef93 | 2013-10-21 10:34:43 +0000 | [diff] [blame] | 24 | #include "webrtc/video_engine/include/vie_image_process.h" |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 25 | #include "webrtc/video_engine/include/vie_network.h" |
| 26 | #include "webrtc/video_engine/include/vie_rtp_rtcp.h" |
pbos@webrtc.org | f577ae9 | 2014-03-19 08:43:57 +0000 | [diff] [blame] | 27 | #include "webrtc/video_engine/vie_defines.h" |
pbos@webrtc.org | 16e03b7 | 2013-10-28 16:32:01 +0000 | [diff] [blame] | 28 | #include "webrtc/video_send_stream.h" |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 29 | |
| 30 | namespace webrtc { |
pbos@webrtc.org | 1e92b0a | 2014-05-15 09:35:06 +0000 | [diff] [blame] | 31 | std::string |
pbos@webrtc.org | 024e4d5 | 2014-05-15 10:03:24 +0000 | [diff] [blame] | 32 | VideoSendStream::Config::EncoderSettings::ToString() const { |
pbos@webrtc.org | 1e92b0a | 2014-05-15 09:35:06 +0000 | [diff] [blame] | 33 | std::stringstream ss; |
| 34 | ss << "{payload_name: " << payload_name; |
| 35 | ss << ", payload_type: " << payload_type; |
| 36 | if (encoder != NULL) |
pbos@webrtc.org | 6ae48c6 | 2014-06-06 10:49:19 +0000 | [diff] [blame] | 37 | ss << ", encoder: " << (encoder != NULL ? "(encoder)" : "NULL"); |
pbos@webrtc.org | 1e92b0a | 2014-05-15 09:35:06 +0000 | [diff] [blame] | 38 | ss << '}'; |
| 39 | return ss.str(); |
| 40 | } |
| 41 | |
pbos@webrtc.org | 024e4d5 | 2014-05-15 10:03:24 +0000 | [diff] [blame] | 42 | std::string VideoSendStream::Config::Rtp::Rtx::ToString() |
pbos@webrtc.org | 1e92b0a | 2014-05-15 09:35:06 +0000 | [diff] [blame] | 43 | const { |
| 44 | std::stringstream ss; |
| 45 | ss << "{ssrcs: {"; |
| 46 | for (size_t i = 0; i < ssrcs.size(); ++i) { |
| 47 | ss << ssrcs[i]; |
| 48 | if (i != ssrcs.size() - 1) |
| 49 | ss << "}, {"; |
| 50 | } |
| 51 | ss << '}'; |
| 52 | |
| 53 | ss << ", payload_type: " << payload_type; |
| 54 | ss << '}'; |
| 55 | return ss.str(); |
| 56 | } |
| 57 | |
pbos@webrtc.org | 024e4d5 | 2014-05-15 10:03:24 +0000 | [diff] [blame] | 58 | std::string VideoSendStream::Config::Rtp::ToString() const { |
pbos@webrtc.org | 1e92b0a | 2014-05-15 09:35:06 +0000 | [diff] [blame] | 59 | std::stringstream ss; |
| 60 | ss << "{ssrcs: {"; |
| 61 | for (size_t i = 0; i < ssrcs.size(); ++i) { |
| 62 | ss << ssrcs[i]; |
| 63 | if (i != ssrcs.size() - 1) |
| 64 | ss << "}, {"; |
| 65 | } |
| 66 | ss << '}'; |
| 67 | |
| 68 | ss << ", max_packet_size: " << max_packet_size; |
| 69 | if (min_transmit_bitrate_bps != 0) |
| 70 | ss << ", min_transmit_bitrate_bps: " << min_transmit_bitrate_bps; |
| 71 | |
| 72 | ss << ", extensions: {"; |
| 73 | for (size_t i = 0; i < extensions.size(); ++i) { |
| 74 | ss << extensions[i].ToString(); |
| 75 | if (i != extensions.size() - 1) |
| 76 | ss << "}, {"; |
| 77 | } |
| 78 | ss << '}'; |
| 79 | |
| 80 | if (nack.rtp_history_ms != 0) |
| 81 | ss << ", nack.rtp_history_ms: " << nack.rtp_history_ms; |
| 82 | if (fec.ulpfec_payload_type != -1 || fec.red_payload_type != -1) |
| 83 | ss << ", fec: " << fec.ToString(); |
| 84 | if (rtx.payload_type != 0 || !rtx.ssrcs.empty()) |
| 85 | ss << ", rtx: " << rtx.ToString(); |
| 86 | if (c_name != "") |
| 87 | ss << ", c_name: " << c_name; |
| 88 | ss << '}'; |
| 89 | return ss.str(); |
| 90 | } |
| 91 | |
pbos@webrtc.org | 024e4d5 | 2014-05-15 10:03:24 +0000 | [diff] [blame] | 92 | std::string VideoSendStream::Config::ToString() const { |
pbos@webrtc.org | 1e92b0a | 2014-05-15 09:35:06 +0000 | [diff] [blame] | 93 | std::stringstream ss; |
| 94 | ss << "{encoder_settings: " << encoder_settings.ToString(); |
| 95 | ss << ", rtp: " << rtp.ToString(); |
| 96 | if (pre_encode_callback != NULL) |
| 97 | ss << ", (pre_encode_callback)"; |
| 98 | if (post_encode_callback != NULL) |
| 99 | ss << ", (post_encode_callback)"; |
| 100 | if (local_renderer != NULL) { |
| 101 | ss << ", (local_renderer, render_delay_ms: " << render_delay_ms << ")"; |
| 102 | } |
| 103 | if (target_delay_ms > 0) |
| 104 | ss << ", target_delay_ms: " << target_delay_ms; |
pbos@webrtc.org | 1e92b0a | 2014-05-15 09:35:06 +0000 | [diff] [blame] | 105 | if (suspend_below_min_bitrate) |
| 106 | ss << ", suspend_below_min_bitrate: on"; |
| 107 | ss << '}'; |
| 108 | return ss.str(); |
| 109 | } |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 110 | |
pbos@webrtc.org | 024e4d5 | 2014-05-15 10:03:24 +0000 | [diff] [blame] | 111 | namespace internal { |
pbos@webrtc.org | 2bb1bda | 2014-07-07 13:06:48 +0000 | [diff] [blame] | 112 | VideoSendStream::VideoSendStream( |
| 113 | newapi::Transport* transport, |
| 114 | CpuOveruseObserver* overuse_observer, |
| 115 | webrtc::VideoEngine* video_engine, |
| 116 | const VideoSendStream::Config& config, |
pbos@webrtc.org | bbe0a85 | 2014-09-19 12:30:25 +0000 | [diff] [blame^] | 117 | const VideoEncoderConfig& encoder_config, |
pbos@webrtc.org | 2bb1bda | 2014-07-07 13:06:48 +0000 | [diff] [blame] | 118 | const std::map<uint32_t, RtpState>& suspended_ssrcs, |
| 119 | int base_channel, |
| 120 | int start_bitrate_bps) |
pbos@webrtc.org | 6488761 | 2013-11-14 08:58:14 +0000 | [diff] [blame] | 121 | : transport_adapter_(transport), |
sprang@webrtc.org | 4070935 | 2013-11-26 11:41:59 +0000 | [diff] [blame] | 122 | encoded_frame_proxy_(config.post_encode_callback), |
pbos@webrtc.org | 6488761 | 2013-11-14 08:58:14 +0000 | [diff] [blame] | 123 | config_(config), |
mflodman@webrtc.org | eb16b81 | 2014-06-16 08:57:39 +0000 | [diff] [blame] | 124 | start_bitrate_bps_(start_bitrate_bps), |
pbos@webrtc.org | 2bb1bda | 2014-07-07 13:06:48 +0000 | [diff] [blame] | 125 | suspended_ssrcs_(suspended_ssrcs), |
mflodman@webrtc.org | f3973e8 | 2013-12-13 09:40:45 +0000 | [diff] [blame] | 126 | external_codec_(NULL), |
pbos@webrtc.org | de1429e | 2014-04-28 13:00:21 +0000 | [diff] [blame] | 127 | channel_(-1), |
stefan@webrtc.org | 168f23f | 2014-07-11 13:44:02 +0000 | [diff] [blame] | 128 | stats_proxy_(config) { |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 129 | video_engine_base_ = ViEBase::GetInterface(video_engine); |
mflodman@webrtc.org | f3973e8 | 2013-12-13 09:40:45 +0000 | [diff] [blame] | 130 | video_engine_base_->CreateChannel(channel_, base_channel); |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 131 | assert(channel_ != -1); |
mflodman@webrtc.org | eb16b81 | 2014-06-16 08:57:39 +0000 | [diff] [blame] | 132 | assert(start_bitrate_bps_ > 0); |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 133 | |
| 134 | rtp_rtcp_ = ViERTP_RTCP::GetInterface(video_engine); |
| 135 | assert(rtp_rtcp_ != NULL); |
| 136 | |
pbos@webrtc.org | 6488761 | 2013-11-14 08:58:14 +0000 | [diff] [blame] | 137 | assert(config_.rtp.ssrcs.size() > 0); |
pbos@webrtc.org | 2902328 | 2013-09-11 10:14:56 +0000 | [diff] [blame] | 138 | |
pbos@webrtc.org | 709e297 | 2014-03-19 10:59:52 +0000 | [diff] [blame] | 139 | assert(config_.rtp.min_transmit_bitrate_bps >= 0); |
pbos@webrtc.org | 3349ae0 | 2014-03-13 12:52:27 +0000 | [diff] [blame] | 140 | rtp_rtcp_->SetMinTransmitBitrate(channel_, |
pbos@webrtc.org | 709e297 | 2014-03-19 10:59:52 +0000 | [diff] [blame] | 141 | config_.rtp.min_transmit_bitrate_bps / 1000); |
pbos@webrtc.org | 3349ae0 | 2014-03-13 12:52:27 +0000 | [diff] [blame] | 142 | |
pbos@webrtc.org | 2902328 | 2013-09-11 10:14:56 +0000 | [diff] [blame] | 143 | for (size_t i = 0; i < config_.rtp.extensions.size(); ++i) { |
| 144 | const std::string& extension = config_.rtp.extensions[i].name; |
| 145 | int id = config_.rtp.extensions[i].id; |
pbos@webrtc.org | ce90eff | 2013-11-20 11:48:56 +0000 | [diff] [blame] | 146 | if (extension == RtpExtension::kTOffset) { |
pbos@webrtc.org | 2902328 | 2013-09-11 10:14:56 +0000 | [diff] [blame] | 147 | if (rtp_rtcp_->SetSendTimestampOffsetStatus(channel_, true, id) != 0) |
| 148 | abort(); |
pbos@webrtc.org | ce90eff | 2013-11-20 11:48:56 +0000 | [diff] [blame] | 149 | } else if (extension == RtpExtension::kAbsSendTime) { |
pbos@webrtc.org | 5c678ea | 2013-09-11 19:00:39 +0000 | [diff] [blame] | 150 | if (rtp_rtcp_->SetSendAbsoluteSendTimeStatus(channel_, true, id) != 0) |
| 151 | abort(); |
pbos@webrtc.org | 2902328 | 2013-09-11 10:14:56 +0000 | [diff] [blame] | 152 | } else { |
| 153 | abort(); // Unsupported extension. |
| 154 | } |
| 155 | } |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 156 | |
mflodman@webrtc.org | 92c2793 | 2013-12-13 16:36:28 +0000 | [diff] [blame] | 157 | rtp_rtcp_->SetRembStatus(channel_, true, false); |
| 158 | |
pbos@webrtc.org | 0e63e76 | 2013-09-20 11:56:26 +0000 | [diff] [blame] | 159 | // Enable NACK, FEC or both. |
| 160 | if (config_.rtp.fec.red_payload_type != -1) { |
| 161 | assert(config_.rtp.fec.ulpfec_payload_type != -1); |
| 162 | if (config_.rtp.nack.rtp_history_ms > 0) { |
| 163 | rtp_rtcp_->SetHybridNACKFECStatus( |
| 164 | channel_, |
| 165 | true, |
| 166 | static_cast<unsigned char>(config_.rtp.fec.red_payload_type), |
| 167 | static_cast<unsigned char>(config_.rtp.fec.ulpfec_payload_type)); |
| 168 | } else { |
| 169 | rtp_rtcp_->SetFECStatus( |
| 170 | channel_, |
| 171 | true, |
| 172 | static_cast<unsigned char>(config_.rtp.fec.red_payload_type), |
| 173 | static_cast<unsigned char>(config_.rtp.fec.ulpfec_payload_type)); |
| 174 | } |
| 175 | } else { |
| 176 | rtp_rtcp_->SetNACKStatus(channel_, config_.rtp.nack.rtp_history_ms > 0); |
| 177 | } |
| 178 | |
pbos@webrtc.org | be9d2a4 | 2014-06-30 13:19:09 +0000 | [diff] [blame] | 179 | ConfigureSsrcs(); |
| 180 | |
pbos@webrtc.org | 013d994 | 2013-08-22 09:42:17 +0000 | [diff] [blame] | 181 | char rtcp_cname[ViERTP_RTCP::KMaxRTCPCNameLength]; |
| 182 | assert(config_.rtp.c_name.length() < ViERTP_RTCP::KMaxRTCPCNameLength); |
| 183 | strncpy(rtcp_cname, config_.rtp.c_name.c_str(), sizeof(rtcp_cname) - 1); |
| 184 | rtcp_cname[sizeof(rtcp_cname) - 1] = '\0'; |
| 185 | |
| 186 | rtp_rtcp_->SetRTCPCName(channel_, rtcp_cname); |
| 187 | |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 188 | capture_ = ViECapture::GetInterface(video_engine); |
| 189 | capture_->AllocateExternalCaptureDevice(capture_id_, external_capture_); |
| 190 | capture_->ConnectCaptureDevice(capture_id_, channel_); |
| 191 | |
| 192 | network_ = ViENetwork::GetInterface(video_engine); |
| 193 | assert(network_ != NULL); |
| 194 | |
pbos@webrtc.org | e75a1bf | 2013-09-18 11:52:42 +0000 | [diff] [blame] | 195 | network_->RegisterSendTransport(channel_, transport_adapter_); |
sprang@webrtc.org | 25fce9a | 2013-10-16 13:29:14 +0000 | [diff] [blame] | 196 | // 28 to match packet overhead in ModuleRtpRtcpImpl. |
pbos@webrtc.org | 16e03b7 | 2013-10-28 16:32:01 +0000 | [diff] [blame] | 197 | network_->SetMTU(channel_, |
| 198 | static_cast<unsigned int>(config_.rtp.max_packet_size + 28)); |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 199 | |
pbos@webrtc.org | f577ae9 | 2014-03-19 08:43:57 +0000 | [diff] [blame] | 200 | assert(config.encoder_settings.encoder != NULL); |
| 201 | assert(config.encoder_settings.payload_type >= 0); |
| 202 | assert(config.encoder_settings.payload_type <= 127); |
| 203 | external_codec_ = ViEExternalCodec::GetInterface(video_engine); |
| 204 | if (external_codec_->RegisterExternalSendCodec( |
| 205 | channel_, |
| 206 | config.encoder_settings.payload_type, |
| 207 | config.encoder_settings.encoder, |
| 208 | false) != 0) { |
| 209 | abort(); |
stefan@webrtc.org | 360e376 | 2013-08-22 09:29:56 +0000 | [diff] [blame] | 210 | } |
| 211 | |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 212 | codec_ = ViECodec::GetInterface(video_engine); |
pbos@webrtc.org | bbe0a85 | 2014-09-19 12:30:25 +0000 | [diff] [blame^] | 213 | if (!ReconfigureVideoEncoder(encoder_config)) |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 214 | abort(); |
pbos@webrtc.org | fe1ef93 | 2013-10-21 10:34:43 +0000 | [diff] [blame] | 215 | |
pbos@webrtc.org | f577ae9 | 2014-03-19 08:43:57 +0000 | [diff] [blame] | 216 | if (overuse_observer) |
| 217 | video_engine_base_->RegisterCpuOveruseObserver(channel_, overuse_observer); |
| 218 | |
stefan@webrtc.org | 168f23f | 2014-07-11 13:44:02 +0000 | [diff] [blame] | 219 | video_engine_base_->RegisterSendSideDelayObserver(channel_, &stats_proxy_); |
| 220 | |
pbos@webrtc.org | fe1ef93 | 2013-10-21 10:34:43 +0000 | [diff] [blame] | 221 | image_process_ = ViEImageProcess::GetInterface(video_engine); |
| 222 | image_process_->RegisterPreEncodeCallback(channel_, |
| 223 | config_.pre_encode_callback); |
sprang@webrtc.org | 4070935 | 2013-11-26 11:41:59 +0000 | [diff] [blame] | 224 | if (config_.post_encode_callback) { |
| 225 | image_process_->RegisterPostEncodeImageCallback(channel_, |
| 226 | &encoded_frame_proxy_); |
| 227 | } |
henrik.lundin@webrtc.org | ba975e2 | 2013-10-23 11:04:57 +0000 | [diff] [blame] | 228 | |
pbos@webrtc.org | 6ae48c6 | 2014-06-06 10:49:19 +0000 | [diff] [blame] | 229 | if (config_.suspend_below_min_bitrate) |
henrik.lundin@webrtc.org | ce8e093 | 2013-11-18 12:18:43 +0000 | [diff] [blame] | 230 | codec_->SuspendBelowMinBitrate(channel_); |
sprang@webrtc.org | ccd4284 | 2014-01-07 09:54:34 +0000 | [diff] [blame] | 231 | |
sprang@webrtc.org | ccd4284 | 2014-01-07 09:54:34 +0000 | [diff] [blame] | 232 | rtp_rtcp_->RegisterSendChannelRtcpStatisticsCallback(channel_, |
stefan@webrtc.org | 168f23f | 2014-07-11 13:44:02 +0000 | [diff] [blame] | 233 | &stats_proxy_); |
sprang@webrtc.org | ccd4284 | 2014-01-07 09:54:34 +0000 | [diff] [blame] | 234 | rtp_rtcp_->RegisterSendChannelRtpStatisticsCallback(channel_, |
stefan@webrtc.org | 168f23f | 2014-07-11 13:44:02 +0000 | [diff] [blame] | 235 | &stats_proxy_); |
| 236 | rtp_rtcp_->RegisterSendBitrateObserver(channel_, &stats_proxy_); |
| 237 | rtp_rtcp_->RegisterSendFrameCountObserver(channel_, &stats_proxy_); |
sprang@webrtc.org | ccd4284 | 2014-01-07 09:54:34 +0000 | [diff] [blame] | 238 | |
stefan@webrtc.org | 168f23f | 2014-07-11 13:44:02 +0000 | [diff] [blame] | 239 | codec_->RegisterEncoderObserver(channel_, stats_proxy_); |
| 240 | capture_->RegisterObserver(capture_id_, stats_proxy_); |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | VideoSendStream::~VideoSendStream() { |
sprang@webrtc.org | ccd4284 | 2014-01-07 09:54:34 +0000 | [diff] [blame] | 244 | capture_->DeregisterObserver(capture_id_); |
| 245 | codec_->DeregisterEncoderObserver(channel_); |
| 246 | |
stefan@webrtc.org | 168f23f | 2014-07-11 13:44:02 +0000 | [diff] [blame] | 247 | rtp_rtcp_->DeregisterSendFrameCountObserver(channel_, &stats_proxy_); |
| 248 | rtp_rtcp_->DeregisterSendBitrateObserver(channel_, &stats_proxy_); |
sprang@webrtc.org | ccd4284 | 2014-01-07 09:54:34 +0000 | [diff] [blame] | 249 | rtp_rtcp_->DeregisterSendChannelRtpStatisticsCallback(channel_, |
stefan@webrtc.org | 168f23f | 2014-07-11 13:44:02 +0000 | [diff] [blame] | 250 | &stats_proxy_); |
sprang@webrtc.org | ccd4284 | 2014-01-07 09:54:34 +0000 | [diff] [blame] | 251 | rtp_rtcp_->DeregisterSendChannelRtcpStatisticsCallback(channel_, |
stefan@webrtc.org | 168f23f | 2014-07-11 13:44:02 +0000 | [diff] [blame] | 252 | &stats_proxy_); |
sprang@webrtc.org | ccd4284 | 2014-01-07 09:54:34 +0000 | [diff] [blame] | 253 | |
pbos@webrtc.org | fe1ef93 | 2013-10-21 10:34:43 +0000 | [diff] [blame] | 254 | image_process_->DeRegisterPreEncodeCallback(channel_); |
| 255 | |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 256 | network_->DeregisterSendTransport(channel_); |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 257 | |
| 258 | capture_->DisconnectCaptureDevice(channel_); |
| 259 | capture_->ReleaseCaptureDevice(capture_id_); |
| 260 | |
pbos@webrtc.org | f577ae9 | 2014-03-19 08:43:57 +0000 | [diff] [blame] | 261 | external_codec_->DeRegisterExternalSendCodec( |
| 262 | channel_, config_.encoder_settings.payload_type); |
stefan@webrtc.org | 360e376 | 2013-08-22 09:29:56 +0000 | [diff] [blame] | 263 | |
pbos@webrtc.org | fe1ef93 | 2013-10-21 10:34:43 +0000 | [diff] [blame] | 264 | video_engine_base_->DeleteChannel(channel_); |
| 265 | |
| 266 | image_process_->Release(); |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 267 | video_engine_base_->Release(); |
| 268 | capture_->Release(); |
| 269 | codec_->Release(); |
stefan@webrtc.org | 360e376 | 2013-08-22 09:29:56 +0000 | [diff] [blame] | 270 | if (external_codec_) |
| 271 | external_codec_->Release(); |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 272 | network_->Release(); |
| 273 | rtp_rtcp_->Release(); |
| 274 | } |
| 275 | |
pbos@webrtc.org | 724947b | 2013-12-11 16:26:16 +0000 | [diff] [blame] | 276 | void VideoSendStream::SwapFrame(I420VideoFrame* frame) { |
pbos@webrtc.org | 724947b | 2013-12-11 16:26:16 +0000 | [diff] [blame] | 277 | // TODO(pbos): Local rendering should not be done on the capture thread. |
| 278 | if (config_.local_renderer != NULL) |
pbos@webrtc.org | 1566ee2 | 2014-05-23 13:03:45 +0000 | [diff] [blame] | 279 | config_.local_renderer->RenderFrame(*frame, 0); |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 280 | |
pbos@webrtc.org | 1566ee2 | 2014-05-23 13:03:45 +0000 | [diff] [blame] | 281 | external_capture_->SwapFrame(frame); |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 282 | } |
| 283 | |
pbos@webrtc.org | 74fa489 | 2013-08-23 09:19:30 +0000 | [diff] [blame] | 284 | VideoSendStreamInput* VideoSendStream::Input() { return this; } |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 285 | |
pbos@webrtc.org | a5c8d2c | 2014-04-24 11:13:21 +0000 | [diff] [blame] | 286 | void VideoSendStream::Start() { |
sprang@webrtc.org | d9b9560 | 2014-01-27 13:03:02 +0000 | [diff] [blame] | 287 | transport_adapter_.Enable(); |
pbos@webrtc.org | f777cf2 | 2014-01-10 18:47:32 +0000 | [diff] [blame] | 288 | video_engine_base_->StartSend(channel_); |
| 289 | video_engine_base_->StartReceive(channel_); |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 290 | } |
| 291 | |
pbos@webrtc.org | a5c8d2c | 2014-04-24 11:13:21 +0000 | [diff] [blame] | 292 | void VideoSendStream::Stop() { |
pbos@webrtc.org | f777cf2 | 2014-01-10 18:47:32 +0000 | [diff] [blame] | 293 | video_engine_base_->StopSend(channel_); |
| 294 | video_engine_base_->StopReceive(channel_); |
sprang@webrtc.org | d9b9560 | 2014-01-27 13:03:02 +0000 | [diff] [blame] | 295 | transport_adapter_.Disable(); |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 296 | } |
| 297 | |
pbos@webrtc.org | f577ae9 | 2014-03-19 08:43:57 +0000 | [diff] [blame] | 298 | bool VideoSendStream::ReconfigureVideoEncoder( |
pbos@webrtc.org | bbe0a85 | 2014-09-19 12:30:25 +0000 | [diff] [blame^] | 299 | const VideoEncoderConfig& config) { |
| 300 | const std::vector<VideoStream>& streams = config.streams; |
pbos@webrtc.org | f577ae9 | 2014-03-19 08:43:57 +0000 | [diff] [blame] | 301 | assert(!streams.empty()); |
| 302 | assert(config_.rtp.ssrcs.size() >= streams.size()); |
pbos@webrtc.org | 6488761 | 2013-11-14 08:58:14 +0000 | [diff] [blame] | 303 | |
pbos@webrtc.org | f577ae9 | 2014-03-19 08:43:57 +0000 | [diff] [blame] | 304 | VideoCodec video_codec; |
| 305 | memset(&video_codec, 0, sizeof(video_codec)); |
stefan@webrtc.org | 79c3359 | 2014-08-06 09:24:53 +0000 | [diff] [blame] | 306 | if (config_.encoder_settings.payload_name == "VP8") { |
| 307 | video_codec.codecType = kVideoCodecVP8; |
| 308 | } else if (config_.encoder_settings.payload_name == "H264") { |
| 309 | video_codec.codecType = kVideoCodecH264; |
| 310 | } else { |
| 311 | video_codec.codecType = kVideoCodecGeneric; |
| 312 | } |
pbos@webrtc.org | bbe0a85 | 2014-09-19 12:30:25 +0000 | [diff] [blame^] | 313 | switch (config.content_type) { |
| 314 | case VideoEncoderConfig::kRealtimeVideo: |
| 315 | video_codec.mode = kRealtimeVideo; |
| 316 | break; |
| 317 | case VideoEncoderConfig::kScreenshare: |
| 318 | video_codec.mode = kScreensharing; |
| 319 | break; |
| 320 | } |
pbos@webrtc.org | f577ae9 | 2014-03-19 08:43:57 +0000 | [diff] [blame] | 321 | |
| 322 | if (video_codec.codecType == kVideoCodecVP8) { |
pbos@webrtc.org | 6cd6ba8 | 2014-09-18 12:42:28 +0000 | [diff] [blame] | 323 | video_codec.codecSpecific.VP8 = VideoEncoder::GetDefaultVp8Settings(); |
stefan@webrtc.org | 79c3359 | 2014-08-06 09:24:53 +0000 | [diff] [blame] | 324 | } else if (video_codec.codecType == kVideoCodecH264) { |
pbos@webrtc.org | 6cd6ba8 | 2014-09-18 12:42:28 +0000 | [diff] [blame] | 325 | video_codec.codecSpecific.H264 = VideoEncoder::GetDefaultH264Settings(); |
pbos@webrtc.org | f577ae9 | 2014-03-19 08:43:57 +0000 | [diff] [blame] | 326 | } |
| 327 | |
pbos@webrtc.org | 91f1752 | 2014-07-10 10:13:37 +0000 | [diff] [blame] | 328 | if (video_codec.codecType == kVideoCodecVP8) { |
pbos@webrtc.org | bbe0a85 | 2014-09-19 12:30:25 +0000 | [diff] [blame^] | 329 | if (config.encoder_specific_settings != NULL) { |
| 330 | video_codec.codecSpecific.VP8 = *reinterpret_cast<const VideoCodecVP8*>( |
| 331 | config.encoder_specific_settings); |
pbos@webrtc.org | 91f1752 | 2014-07-10 10:13:37 +0000 | [diff] [blame] | 332 | } |
| 333 | } else { |
| 334 | // TODO(pbos): Support encoder_settings codec-agnostically. |
pbos@webrtc.org | bbe0a85 | 2014-09-19 12:30:25 +0000 | [diff] [blame^] | 335 | assert(config.encoder_specific_settings == NULL); |
pbos@webrtc.org | 91f1752 | 2014-07-10 10:13:37 +0000 | [diff] [blame] | 336 | } |
| 337 | |
pbos@webrtc.org | f577ae9 | 2014-03-19 08:43:57 +0000 | [diff] [blame] | 338 | strncpy(video_codec.plName, |
| 339 | config_.encoder_settings.payload_name.c_str(), |
| 340 | kPayloadNameSize - 1); |
| 341 | video_codec.plName[kPayloadNameSize - 1] = '\0'; |
| 342 | video_codec.plType = config_.encoder_settings.payload_type; |
| 343 | video_codec.numberOfSimulcastStreams = |
| 344 | static_cast<unsigned char>(streams.size()); |
| 345 | video_codec.minBitrate = streams[0].min_bitrate_bps / 1000; |
| 346 | assert(streams.size() <= kMaxSimulcastStreams); |
| 347 | for (size_t i = 0; i < streams.size(); ++i) { |
| 348 | SimulcastStream* sim_stream = &video_codec.simulcastStream[i]; |
| 349 | assert(streams[i].width > 0); |
| 350 | assert(streams[i].height > 0); |
| 351 | assert(streams[i].max_framerate > 0); |
| 352 | // Different framerates not supported per stream at the moment. |
| 353 | assert(streams[i].max_framerate == streams[0].max_framerate); |
| 354 | assert(streams[i].min_bitrate_bps >= 0); |
| 355 | assert(streams[i].target_bitrate_bps >= streams[i].min_bitrate_bps); |
| 356 | assert(streams[i].max_bitrate_bps >= streams[i].target_bitrate_bps); |
| 357 | assert(streams[i].max_qp >= 0); |
| 358 | |
| 359 | sim_stream->width = static_cast<unsigned short>(streams[i].width); |
| 360 | sim_stream->height = static_cast<unsigned short>(streams[i].height); |
| 361 | sim_stream->minBitrate = streams[i].min_bitrate_bps / 1000; |
| 362 | sim_stream->targetBitrate = streams[i].target_bitrate_bps / 1000; |
| 363 | sim_stream->maxBitrate = streams[i].max_bitrate_bps / 1000; |
| 364 | sim_stream->qpMax = streams[i].max_qp; |
| 365 | // TODO(pbos): Implement mapping for temporal layers. |
| 366 | assert(streams[i].temporal_layers.empty()); |
| 367 | |
| 368 | video_codec.width = std::max(video_codec.width, |
| 369 | static_cast<unsigned short>(streams[i].width)); |
| 370 | video_codec.height = std::max( |
| 371 | video_codec.height, static_cast<unsigned short>(streams[i].height)); |
| 372 | video_codec.minBitrate = |
| 373 | std::min(video_codec.minBitrate, |
| 374 | static_cast<unsigned int>(streams[i].min_bitrate_bps / 1000)); |
| 375 | video_codec.maxBitrate += streams[i].max_bitrate_bps / 1000; |
| 376 | video_codec.qpMax = std::max(video_codec.qpMax, |
| 377 | static_cast<unsigned int>(streams[i].max_qp)); |
| 378 | } |
mflodman@webrtc.org | eb16b81 | 2014-06-16 08:57:39 +0000 | [diff] [blame] | 379 | video_codec.startBitrate = |
| 380 | static_cast<unsigned int>(start_bitrate_bps_) / 1000; |
pbos@webrtc.org | f577ae9 | 2014-03-19 08:43:57 +0000 | [diff] [blame] | 381 | |
| 382 | if (video_codec.minBitrate < kViEMinCodecBitrate) |
| 383 | video_codec.minBitrate = kViEMinCodecBitrate; |
| 384 | if (video_codec.maxBitrate < kViEMinCodecBitrate) |
| 385 | video_codec.maxBitrate = kViEMinCodecBitrate; |
mflodman@webrtc.org | eb16b81 | 2014-06-16 08:57:39 +0000 | [diff] [blame] | 386 | if (video_codec.startBitrate < video_codec.minBitrate) |
| 387 | video_codec.startBitrate = video_codec.minBitrate; |
| 388 | if (video_codec.startBitrate > video_codec.maxBitrate) |
| 389 | video_codec.startBitrate = video_codec.maxBitrate; |
pbos@webrtc.org | f577ae9 | 2014-03-19 08:43:57 +0000 | [diff] [blame] | 390 | |
| 391 | if (video_codec.startBitrate < video_codec.minBitrate) |
| 392 | video_codec.startBitrate = video_codec.minBitrate; |
| 393 | if (video_codec.startBitrate > video_codec.maxBitrate) |
| 394 | video_codec.startBitrate = video_codec.maxBitrate; |
| 395 | |
pbos@webrtc.org | 6ae48c6 | 2014-06-06 10:49:19 +0000 | [diff] [blame] | 396 | assert(streams[0].max_framerate > 0); |
| 397 | video_codec.maxFramerate = streams[0].max_framerate; |
pbos@webrtc.org | f577ae9 | 2014-03-19 08:43:57 +0000 | [diff] [blame] | 398 | |
pbos@webrtc.org | be9d2a4 | 2014-06-30 13:19:09 +0000 | [diff] [blame] | 399 | return codec_->SetSendCodec(channel_, video_codec) == 0; |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 400 | } |
| 401 | |
pbos@webrtc.org | bbb07e6 | 2013-08-05 12:01:36 +0000 | [diff] [blame] | 402 | bool VideoSendStream::DeliverRtcp(const uint8_t* packet, size_t length) { |
| 403 | return network_->ReceivedRTCPPacket( |
pbos@webrtc.org | ccdcbae | 2013-08-05 13:25:51 +0000 | [diff] [blame] | 404 | channel_, packet, static_cast<int>(length)) == 0; |
pbos@webrtc.org | bbb07e6 | 2013-08-05 12:01:36 +0000 | [diff] [blame] | 405 | } |
sprang@webrtc.org | ccd4284 | 2014-01-07 09:54:34 +0000 | [diff] [blame] | 406 | |
| 407 | VideoSendStream::Stats VideoSendStream::GetStats() const { |
stefan@webrtc.org | 168f23f | 2014-07-11 13:44:02 +0000 | [diff] [blame] | 408 | return stats_proxy_.GetStats(); |
sprang@webrtc.org | ccd4284 | 2014-01-07 09:54:34 +0000 | [diff] [blame] | 409 | } |
| 410 | |
pbos@webrtc.org | be9d2a4 | 2014-06-30 13:19:09 +0000 | [diff] [blame] | 411 | void VideoSendStream::ConfigureSsrcs() { |
| 412 | for (size_t i = 0; i < config_.rtp.ssrcs.size(); ++i) { |
| 413 | uint32_t ssrc = config_.rtp.ssrcs[i]; |
| 414 | rtp_rtcp_->SetLocalSSRC( |
| 415 | channel_, ssrc, kViEStreamTypeNormal, static_cast<unsigned char>(i)); |
pbos@webrtc.org | 2bb1bda | 2014-07-07 13:06:48 +0000 | [diff] [blame] | 416 | RtpStateMap::iterator it = suspended_ssrcs_.find(ssrc); |
| 417 | if (it != suspended_ssrcs_.end()) |
| 418 | rtp_rtcp_->SetRtpStateForSsrc(channel_, ssrc, it->second); |
pbos@webrtc.org | be9d2a4 | 2014-06-30 13:19:09 +0000 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | if (config_.rtp.rtx.ssrcs.empty()) { |
| 422 | assert(!config_.rtp.rtx.pad_with_redundant_payloads); |
| 423 | return; |
| 424 | } |
| 425 | |
| 426 | // Set up RTX. |
| 427 | assert(config_.rtp.rtx.ssrcs.size() == config_.rtp.ssrcs.size()); |
pbos@webrtc.org | 2bb1bda | 2014-07-07 13:06:48 +0000 | [diff] [blame] | 428 | for (size_t i = 0; i < config_.rtp.rtx.ssrcs.size(); ++i) { |
| 429 | uint32_t ssrc = config_.rtp.rtx.ssrcs[i]; |
pbos@webrtc.org | be9d2a4 | 2014-06-30 13:19:09 +0000 | [diff] [blame] | 430 | rtp_rtcp_->SetLocalSSRC(channel_, |
| 431 | config_.rtp.rtx.ssrcs[i], |
| 432 | kViEStreamTypeRtx, |
| 433 | static_cast<unsigned char>(i)); |
pbos@webrtc.org | 2bb1bda | 2014-07-07 13:06:48 +0000 | [diff] [blame] | 434 | RtpStateMap::iterator it = suspended_ssrcs_.find(ssrc); |
| 435 | if (it != suspended_ssrcs_.end()) |
| 436 | rtp_rtcp_->SetRtpStateForSsrc(channel_, ssrc, it->second); |
pbos@webrtc.org | be9d2a4 | 2014-06-30 13:19:09 +0000 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | if (config_.rtp.rtx.pad_with_redundant_payloads) { |
| 440 | rtp_rtcp_->SetPadWithRedundantPayloads(channel_, true); |
| 441 | } |
| 442 | |
| 443 | assert(config_.rtp.rtx.payload_type >= 0); |
| 444 | rtp_rtcp_->SetRtxSendPayloadType(channel_, config_.rtp.rtx.payload_type); |
| 445 | } |
| 446 | |
pbos@webrtc.org | 2bb1bda | 2014-07-07 13:06:48 +0000 | [diff] [blame] | 447 | std::map<uint32_t, RtpState> VideoSendStream::GetRtpStates() const { |
| 448 | std::map<uint32_t, RtpState> rtp_states; |
| 449 | for (size_t i = 0; i < config_.rtp.ssrcs.size(); ++i) { |
| 450 | uint32_t ssrc = config_.rtp.ssrcs[i]; |
| 451 | rtp_states[ssrc] = rtp_rtcp_->GetRtpStateForSsrc(channel_, ssrc); |
| 452 | } |
| 453 | |
| 454 | for (size_t i = 0; i < config_.rtp.rtx.ssrcs.size(); ++i) { |
| 455 | uint32_t ssrc = config_.rtp.rtx.ssrcs[i]; |
| 456 | rtp_states[ssrc] = rtp_rtcp_->GetRtpStateForSsrc(channel_, ssrc); |
| 457 | } |
| 458 | |
| 459 | return rtp_states; |
| 460 | } |
| 461 | |
pbos@webrtc.org | 26c0c41 | 2014-09-03 16:17:12 +0000 | [diff] [blame] | 462 | void VideoSendStream::SignalNetworkState(Call::NetworkState state) { |
| 463 | // When network goes up, enable RTCP status before setting transmission state. |
| 464 | // When it goes down, disable RTCP afterwards. This ensures that any packets |
| 465 | // sent due to the network state changed will not be dropped. |
| 466 | if (state == Call::kNetworkUp) |
| 467 | rtp_rtcp_->SetRTCPStatus(channel_, kRtcpCompound_RFC4585); |
| 468 | network_->SetNetworkTransmissionState(channel_, state == Call::kNetworkUp); |
| 469 | if (state == Call::kNetworkDown) |
| 470 | rtp_rtcp_->SetRTCPStatus(channel_, kRtcpNone); |
| 471 | } |
| 472 | |
pbos@webrtc.org | 29d5839 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 473 | } // namespace internal |
| 474 | } // namespace webrtc |