pbos@webrtc.org | dc8c883 | 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 | b581c90 | 2013-10-28 16:32:01 +0000 | [diff] [blame] | 11 | #include "webrtc/video/video_send_stream.h" |
pbos@webrtc.org | dc8c883 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 12 | |
pbos@webrtc.org | debc672 | 2013-08-22 09:42:17 +0000 | [diff] [blame] | 13 | #include <string.h> |
| 14 | |
henrik.lundin@webrtc.org | ce21c82 | 2013-10-23 11:04:57 +0000 | [diff] [blame] | 15 | #include <string> |
pbos@webrtc.org | dc8c883 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 16 | #include <vector> |
| 17 | |
| 18 | #include "webrtc/common_video/libyuv/include/webrtc_libyuv.h" |
| 19 | #include "webrtc/video_engine/include/vie_base.h" |
| 20 | #include "webrtc/video_engine/include/vie_capture.h" |
| 21 | #include "webrtc/video_engine/include/vie_codec.h" |
stefan@webrtc.org | a0a91d8 | 2013-08-22 09:29:56 +0000 | [diff] [blame] | 22 | #include "webrtc/video_engine/include/vie_external_codec.h" |
pbos@webrtc.org | 3ba57eb | 2013-10-21 10:34:43 +0000 | [diff] [blame] | 23 | #include "webrtc/video_engine/include/vie_image_process.h" |
pbos@webrtc.org | dc8c883 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 24 | #include "webrtc/video_engine/include/vie_network.h" |
| 25 | #include "webrtc/video_engine/include/vie_rtp_rtcp.h" |
pbos@webrtc.org | b581c90 | 2013-10-28 16:32:01 +0000 | [diff] [blame] | 26 | #include "webrtc/video_send_stream.h" |
pbos@webrtc.org | dc8c883 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 27 | |
| 28 | namespace webrtc { |
| 29 | namespace internal { |
| 30 | |
mflodman@webrtc.org | ecbeb2b | 2013-07-23 11:35:00 +0000 | [diff] [blame] | 31 | // Super simple and temporary overuse logic. This will move to the application |
| 32 | // as soon as the new API allows changing send codec on the fly. |
| 33 | class ResolutionAdaptor : public webrtc::CpuOveruseObserver { |
| 34 | public: |
| 35 | ResolutionAdaptor(ViECodec* codec, int channel, size_t width, size_t height) |
| 36 | : codec_(codec), |
| 37 | channel_(channel), |
| 38 | max_width_(width), |
| 39 | max_height_(height) {} |
| 40 | |
| 41 | virtual ~ResolutionAdaptor() {} |
| 42 | |
| 43 | virtual void OveruseDetected() OVERRIDE { |
| 44 | VideoCodec codec; |
| 45 | if (codec_->GetSendCodec(channel_, codec) != 0) |
| 46 | return; |
| 47 | |
| 48 | if (codec.width / 2 < min_width || codec.height / 2 < min_height) |
| 49 | return; |
| 50 | |
| 51 | codec.width /= 2; |
| 52 | codec.height /= 2; |
| 53 | codec_->SetSendCodec(channel_, codec); |
| 54 | } |
| 55 | |
| 56 | virtual void NormalUsage() OVERRIDE { |
| 57 | VideoCodec codec; |
| 58 | if (codec_->GetSendCodec(channel_, codec) != 0) |
| 59 | return; |
| 60 | |
| 61 | if (codec.width * 2u > max_width_ || codec.height * 2u > max_height_) |
| 62 | return; |
| 63 | |
| 64 | codec.width *= 2; |
| 65 | codec.height *= 2; |
| 66 | codec_->SetSendCodec(channel_, codec); |
| 67 | } |
| 68 | |
| 69 | private: |
| 70 | // Temporary and arbitrary chosen minimum resolution. |
| 71 | static const size_t min_width = 160; |
| 72 | static const size_t min_height = 120; |
| 73 | |
| 74 | ViECodec* codec_; |
| 75 | const int channel_; |
| 76 | |
| 77 | const size_t max_width_; |
| 78 | const size_t max_height_; |
| 79 | }; |
| 80 | |
pbos@webrtc.org | 12d5ede | 2013-07-09 08:02:33 +0000 | [diff] [blame] | 81 | VideoSendStream::VideoSendStream(newapi::Transport* transport, |
mflodman@webrtc.org | ecbeb2b | 2013-07-23 11:35:00 +0000 | [diff] [blame] | 82 | bool overuse_detection, |
pbos@webrtc.org | 12d5ede | 2013-07-09 08:02:33 +0000 | [diff] [blame] | 83 | webrtc::VideoEngine* video_engine, |
mflodman@webrtc.org | e4d538a | 2013-12-13 09:40:45 +0000 | [diff] [blame^] | 84 | const VideoSendStream::Config& config, |
| 85 | int base_channel) |
pbos@webrtc.org | 8f2997c | 2013-11-14 08:58:14 +0000 | [diff] [blame] | 86 | : transport_adapter_(transport), |
sprang@webrtc.org | 4a9843f | 2013-11-26 11:41:59 +0000 | [diff] [blame] | 87 | encoded_frame_proxy_(config.post_encode_callback), |
pbos@webrtc.org | 8f2997c | 2013-11-14 08:58:14 +0000 | [diff] [blame] | 88 | codec_lock_(CriticalSectionWrapper::CreateCriticalSection()), |
| 89 | config_(config), |
mflodman@webrtc.org | e4d538a | 2013-12-13 09:40:45 +0000 | [diff] [blame^] | 90 | external_codec_(NULL), |
| 91 | channel_(-1) { |
pbos@webrtc.org | dc8c883 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 92 | video_engine_base_ = ViEBase::GetInterface(video_engine); |
mflodman@webrtc.org | e4d538a | 2013-12-13 09:40:45 +0000 | [diff] [blame^] | 93 | video_engine_base_->CreateChannel(channel_, base_channel); |
pbos@webrtc.org | dc8c883 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 94 | assert(channel_ != -1); |
| 95 | |
| 96 | rtp_rtcp_ = ViERTP_RTCP::GetInterface(video_engine); |
| 97 | assert(rtp_rtcp_ != NULL); |
| 98 | |
pbos@webrtc.org | 8f2997c | 2013-11-14 08:58:14 +0000 | [diff] [blame] | 99 | assert(config_.rtp.ssrcs.size() > 0); |
henrik.lundin@webrtc.org | d7d60c8 | 2013-11-21 14:05:40 +0000 | [diff] [blame] | 100 | if (config_.suspend_below_min_bitrate) |
| 101 | config_.pacing = true; |
stefan@webrtc.org | a0a91d8 | 2013-08-22 09:29:56 +0000 | [diff] [blame] | 102 | rtp_rtcp_->SetTransmissionSmoothingStatus(channel_, config_.pacing); |
pbos@webrtc.org | 905cebd | 2013-09-11 10:14:56 +0000 | [diff] [blame] | 103 | |
| 104 | for (size_t i = 0; i < config_.rtp.extensions.size(); ++i) { |
| 105 | const std::string& extension = config_.rtp.extensions[i].name; |
| 106 | int id = config_.rtp.extensions[i].id; |
pbos@webrtc.org | 60108c2 | 2013-11-20 11:48:56 +0000 | [diff] [blame] | 107 | if (extension == RtpExtension::kTOffset) { |
pbos@webrtc.org | 905cebd | 2013-09-11 10:14:56 +0000 | [diff] [blame] | 108 | if (rtp_rtcp_->SetSendTimestampOffsetStatus(channel_, true, id) != 0) |
| 109 | abort(); |
pbos@webrtc.org | 60108c2 | 2013-11-20 11:48:56 +0000 | [diff] [blame] | 110 | } else if (extension == RtpExtension::kAbsSendTime) { |
pbos@webrtc.org | e22b761 | 2013-09-11 19:00:39 +0000 | [diff] [blame] | 111 | if (rtp_rtcp_->SetSendAbsoluteSendTimeStatus(channel_, true, id) != 0) |
| 112 | abort(); |
pbos@webrtc.org | 905cebd | 2013-09-11 10:14:56 +0000 | [diff] [blame] | 113 | } else { |
| 114 | abort(); // Unsupported extension. |
| 115 | } |
| 116 | } |
pbos@webrtc.org | dc8c883 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 117 | |
pbos@webrtc.org | aa693dd | 2013-09-20 11:56:26 +0000 | [diff] [blame] | 118 | // Enable NACK, FEC or both. |
| 119 | if (config_.rtp.fec.red_payload_type != -1) { |
| 120 | assert(config_.rtp.fec.ulpfec_payload_type != -1); |
| 121 | if (config_.rtp.nack.rtp_history_ms > 0) { |
| 122 | rtp_rtcp_->SetHybridNACKFECStatus( |
| 123 | channel_, |
| 124 | true, |
| 125 | static_cast<unsigned char>(config_.rtp.fec.red_payload_type), |
| 126 | static_cast<unsigned char>(config_.rtp.fec.ulpfec_payload_type)); |
| 127 | } else { |
| 128 | rtp_rtcp_->SetFECStatus( |
| 129 | channel_, |
| 130 | true, |
| 131 | static_cast<unsigned char>(config_.rtp.fec.red_payload_type), |
| 132 | static_cast<unsigned char>(config_.rtp.fec.ulpfec_payload_type)); |
| 133 | } |
| 134 | } else { |
| 135 | rtp_rtcp_->SetNACKStatus(channel_, config_.rtp.nack.rtp_history_ms > 0); |
| 136 | } |
| 137 | |
pbos@webrtc.org | debc672 | 2013-08-22 09:42:17 +0000 | [diff] [blame] | 138 | char rtcp_cname[ViERTP_RTCP::KMaxRTCPCNameLength]; |
| 139 | assert(config_.rtp.c_name.length() < ViERTP_RTCP::KMaxRTCPCNameLength); |
| 140 | strncpy(rtcp_cname, config_.rtp.c_name.c_str(), sizeof(rtcp_cname) - 1); |
| 141 | rtcp_cname[sizeof(rtcp_cname) - 1] = '\0'; |
| 142 | |
| 143 | rtp_rtcp_->SetRTCPCName(channel_, rtcp_cname); |
| 144 | |
pbos@webrtc.org | dc8c883 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 145 | capture_ = ViECapture::GetInterface(video_engine); |
| 146 | capture_->AllocateExternalCaptureDevice(capture_id_, external_capture_); |
| 147 | capture_->ConnectCaptureDevice(capture_id_, channel_); |
| 148 | |
| 149 | network_ = ViENetwork::GetInterface(video_engine); |
| 150 | assert(network_ != NULL); |
| 151 | |
pbos@webrtc.org | 26d75f3 | 2013-09-18 11:52:42 +0000 | [diff] [blame] | 152 | network_->RegisterSendTransport(channel_, transport_adapter_); |
sprang@webrtc.org | 6133dd5 | 2013-10-16 13:29:14 +0000 | [diff] [blame] | 153 | // 28 to match packet overhead in ModuleRtpRtcpImpl. |
pbos@webrtc.org | b581c90 | 2013-10-28 16:32:01 +0000 | [diff] [blame] | 154 | network_->SetMTU(channel_, |
| 155 | static_cast<unsigned int>(config_.rtp.max_packet_size + 28)); |
pbos@webrtc.org | dc8c883 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 156 | |
stefan@webrtc.org | a0a91d8 | 2013-08-22 09:29:56 +0000 | [diff] [blame] | 157 | if (config.encoder) { |
| 158 | external_codec_ = ViEExternalCodec::GetInterface(video_engine); |
| 159 | if (external_codec_->RegisterExternalSendCodec( |
| 160 | channel_, config.codec.plType, config.encoder, |
| 161 | config.internal_source) != 0) { |
| 162 | abort(); |
| 163 | } |
| 164 | } |
| 165 | |
pbos@webrtc.org | dc8c883 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 166 | codec_ = ViECodec::GetInterface(video_engine); |
pbos@webrtc.org | 8f2997c | 2013-11-14 08:58:14 +0000 | [diff] [blame] | 167 | if (!SetCodec(config_.codec)) |
pbos@webrtc.org | dc8c883 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 168 | abort(); |
mflodman@webrtc.org | ecbeb2b | 2013-07-23 11:35:00 +0000 | [diff] [blame] | 169 | |
| 170 | if (overuse_detection) { |
| 171 | overuse_observer_.reset( |
| 172 | new ResolutionAdaptor(codec_, channel_, config_.codec.width, |
| 173 | config_.codec.height)); |
| 174 | video_engine_base_->RegisterCpuOveruseObserver(channel_, |
pbos@webrtc.org | 905cebd | 2013-09-11 10:14:56 +0000 | [diff] [blame] | 175 | overuse_observer_.get()); |
mflodman@webrtc.org | ecbeb2b | 2013-07-23 11:35:00 +0000 | [diff] [blame] | 176 | } |
pbos@webrtc.org | 3ba57eb | 2013-10-21 10:34:43 +0000 | [diff] [blame] | 177 | |
| 178 | image_process_ = ViEImageProcess::GetInterface(video_engine); |
| 179 | image_process_->RegisterPreEncodeCallback(channel_, |
| 180 | config_.pre_encode_callback); |
sprang@webrtc.org | 4a9843f | 2013-11-26 11:41:59 +0000 | [diff] [blame] | 181 | if (config_.post_encode_callback) { |
| 182 | image_process_->RegisterPostEncodeImageCallback(channel_, |
| 183 | &encoded_frame_proxy_); |
| 184 | } |
henrik.lundin@webrtc.org | ce21c82 | 2013-10-23 11:04:57 +0000 | [diff] [blame] | 185 | |
henrik.lundin@webrtc.org | 8fdf191 | 2013-11-18 12:18:43 +0000 | [diff] [blame] | 186 | if (config.suspend_below_min_bitrate) { |
| 187 | codec_->SuspendBelowMinBitrate(channel_); |
henrik.lundin@webrtc.org | ce21c82 | 2013-10-23 11:04:57 +0000 | [diff] [blame] | 188 | } |
pbos@webrtc.org | dc8c883 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | VideoSendStream::~VideoSendStream() { |
pbos@webrtc.org | 3ba57eb | 2013-10-21 10:34:43 +0000 | [diff] [blame] | 192 | image_process_->DeRegisterPreEncodeCallback(channel_); |
| 193 | |
pbos@webrtc.org | dc8c883 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 194 | network_->DeregisterSendTransport(channel_); |
pbos@webrtc.org | dc8c883 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 195 | |
| 196 | capture_->DisconnectCaptureDevice(channel_); |
| 197 | capture_->ReleaseCaptureDevice(capture_id_); |
| 198 | |
stefan@webrtc.org | a0a91d8 | 2013-08-22 09:29:56 +0000 | [diff] [blame] | 199 | if (external_codec_) { |
| 200 | external_codec_->DeRegisterExternalSendCodec(channel_, |
| 201 | config_.codec.plType); |
| 202 | } |
| 203 | |
pbos@webrtc.org | 3ba57eb | 2013-10-21 10:34:43 +0000 | [diff] [blame] | 204 | video_engine_base_->DeleteChannel(channel_); |
| 205 | |
| 206 | image_process_->Release(); |
pbos@webrtc.org | dc8c883 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 207 | video_engine_base_->Release(); |
| 208 | capture_->Release(); |
| 209 | codec_->Release(); |
stefan@webrtc.org | a0a91d8 | 2013-08-22 09:29:56 +0000 | [diff] [blame] | 210 | if (external_codec_) |
| 211 | external_codec_->Release(); |
pbos@webrtc.org | dc8c883 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 212 | network_->Release(); |
| 213 | rtp_rtcp_->Release(); |
| 214 | } |
| 215 | |
pbos@webrtc.org | 7123a80 | 2013-12-11 16:26:16 +0000 | [diff] [blame] | 216 | void VideoSendStream::PutFrame(const I420VideoFrame& frame) { |
| 217 | input_frame_.CopyFrame(frame); |
| 218 | SwapFrame(&input_frame_); |
| 219 | } |
pbos@webrtc.org | dc8c883 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 220 | |
pbos@webrtc.org | 7123a80 | 2013-12-11 16:26:16 +0000 | [diff] [blame] | 221 | void VideoSendStream::SwapFrame(I420VideoFrame* frame) { |
| 222 | // TODO(pbos): Warn if frame is "too far" into the future, or too old. This |
| 223 | // would help detect if frame's being used without NTP. |
| 224 | // TO REVIEWER: Is there any good check for this? Should it be |
| 225 | // skipped? |
| 226 | if (frame != &input_frame_) |
| 227 | input_frame_.SwapFrame(frame); |
pbos@webrtc.org | dc8c883 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 228 | |
pbos@webrtc.org | 7123a80 | 2013-12-11 16:26:16 +0000 | [diff] [blame] | 229 | // TODO(pbos): Local rendering should not be done on the capture thread. |
| 230 | if (config_.local_renderer != NULL) |
| 231 | config_.local_renderer->RenderFrame(input_frame_, 0); |
pbos@webrtc.org | dc8c883 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 232 | |
pbos@webrtc.org | 7123a80 | 2013-12-11 16:26:16 +0000 | [diff] [blame] | 233 | external_capture_->SwapFrame(&input_frame_); |
pbos@webrtc.org | dc8c883 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 234 | } |
| 235 | |
pbos@webrtc.org | d8e92c9 | 2013-08-23 09:19:30 +0000 | [diff] [blame] | 236 | VideoSendStreamInput* VideoSendStream::Input() { return this; } |
pbos@webrtc.org | dc8c883 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 237 | |
pbos@webrtc.org | 48cc9dc | 2013-11-20 11:36:47 +0000 | [diff] [blame] | 238 | void VideoSendStream::StartSending() { |
pbos@webrtc.org | d9f9185 | 2013-05-23 12:37:11 +0000 | [diff] [blame] | 239 | if (video_engine_base_->StartSend(channel_) != 0) |
| 240 | abort(); |
pbos@webrtc.org | bf9bc32 | 2013-08-05 12:01:36 +0000 | [diff] [blame] | 241 | if (video_engine_base_->StartReceive(channel_) != 0) |
| 242 | abort(); |
pbos@webrtc.org | dc8c883 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 243 | } |
| 244 | |
pbos@webrtc.org | 48cc9dc | 2013-11-20 11:36:47 +0000 | [diff] [blame] | 245 | void VideoSendStream::StopSending() { |
pbos@webrtc.org | d9f9185 | 2013-05-23 12:37:11 +0000 | [diff] [blame] | 246 | if (video_engine_base_->StopSend(channel_) != 0) |
| 247 | abort(); |
pbos@webrtc.org | bf9bc32 | 2013-08-05 12:01:36 +0000 | [diff] [blame] | 248 | if (video_engine_base_->StopReceive(channel_) != 0) |
| 249 | abort(); |
pbos@webrtc.org | dc8c883 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 250 | } |
| 251 | |
pbos@webrtc.org | 8f2997c | 2013-11-14 08:58:14 +0000 | [diff] [blame] | 252 | bool VideoSendStream::SetCodec(const VideoCodec& codec) { |
pbos@webrtc.org | 9105cbd | 2013-11-28 11:59:31 +0000 | [diff] [blame] | 253 | assert(config_.rtp.ssrcs.size() >= codec.numberOfSimulcastStreams); |
pbos@webrtc.org | 8f2997c | 2013-11-14 08:58:14 +0000 | [diff] [blame] | 254 | |
| 255 | CriticalSectionScoped crit(codec_lock_.get()); |
| 256 | if (codec_->SetSendCodec(channel_, codec) != 0) |
| 257 | return false; |
| 258 | |
pbos@webrtc.org | 9105cbd | 2013-11-28 11:59:31 +0000 | [diff] [blame] | 259 | for (size_t i = 0; i < config_.rtp.ssrcs.size(); ++i) { |
| 260 | rtp_rtcp_->SetLocalSSRC(channel_, |
| 261 | config_.rtp.ssrcs[i], |
| 262 | kViEStreamTypeNormal, |
| 263 | static_cast<unsigned char>(i)); |
| 264 | } |
| 265 | |
pbos@webrtc.org | 8f2997c | 2013-11-14 08:58:14 +0000 | [diff] [blame] | 266 | config_.codec = codec; |
pbos@webrtc.org | 9105cbd | 2013-11-28 11:59:31 +0000 | [diff] [blame] | 267 | if (config_.rtp.rtx.ssrcs.empty()) |
| 268 | return true; |
| 269 | |
| 270 | // Set up RTX. |
| 271 | assert(config_.rtp.rtx.ssrcs.size() == config_.rtp.ssrcs.size()); |
| 272 | for (size_t i = 0; i < config_.rtp.ssrcs.size(); ++i) { |
| 273 | rtp_rtcp_->SetLocalSSRC(channel_, |
| 274 | config_.rtp.rtx.ssrcs[i], |
| 275 | kViEStreamTypeRtx, |
| 276 | static_cast<unsigned char>(i)); |
| 277 | } |
| 278 | |
| 279 | if (config_.rtp.rtx.rtx_payload_type != 0) { |
| 280 | rtp_rtcp_->SetRtxSendPayloadType(channel_, |
| 281 | config_.rtp.rtx.rtx_payload_type); |
| 282 | } |
| 283 | |
pbos@webrtc.org | 8f2997c | 2013-11-14 08:58:14 +0000 | [diff] [blame] | 284 | return true; |
pbos@webrtc.org | dc8c883 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 285 | } |
| 286 | |
pbos@webrtc.org | 8f2997c | 2013-11-14 08:58:14 +0000 | [diff] [blame] | 287 | VideoCodec VideoSendStream::GetCodec() { |
| 288 | CriticalSectionScoped crit(codec_lock_.get()); |
| 289 | return config_.codec; |
pbos@webrtc.org | dc8c883 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 290 | } |
| 291 | |
pbos@webrtc.org | bf9bc32 | 2013-08-05 12:01:36 +0000 | [diff] [blame] | 292 | bool VideoSendStream::DeliverRtcp(const uint8_t* packet, size_t length) { |
| 293 | return network_->ReceivedRTCPPacket( |
pbos@webrtc.org | 30c741a | 2013-08-05 13:25:51 +0000 | [diff] [blame] | 294 | channel_, packet, static_cast<int>(length)) == 0; |
pbos@webrtc.org | bf9bc32 | 2013-08-05 12:01:36 +0000 | [diff] [blame] | 295 | } |
pbos@webrtc.org | dc8c883 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 296 | } // namespace internal |
| 297 | } // namespace webrtc |