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, |
pbos@webrtc.org | d8e92c9 | 2013-08-23 09:19:30 +0000 | [diff] [blame] | 84 | const VideoSendStream::Config& config) |
pbos@webrtc.org | 8f2997c | 2013-11-14 08:58:14 +0000 | [diff] [blame] | 85 | : transport_adapter_(transport), |
| 86 | codec_lock_(CriticalSectionWrapper::CreateCriticalSection()), |
| 87 | config_(config), |
| 88 | external_codec_(NULL) { |
pbos@webrtc.org | dc8c883 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 89 | video_engine_base_ = ViEBase::GetInterface(video_engine); |
| 90 | video_engine_base_->CreateChannel(channel_); |
| 91 | assert(channel_ != -1); |
| 92 | |
| 93 | rtp_rtcp_ = ViERTP_RTCP::GetInterface(video_engine); |
| 94 | assert(rtp_rtcp_ != NULL); |
| 95 | |
pbos@webrtc.org | 8f2997c | 2013-11-14 08:58:14 +0000 | [diff] [blame] | 96 | assert(config_.rtp.ssrcs.size() > 0); |
stefan@webrtc.org | a0a91d8 | 2013-08-22 09:29:56 +0000 | [diff] [blame] | 97 | if (config_.rtp.ssrcs.size() == 1) { |
| 98 | rtp_rtcp_->SetLocalSSRC(channel_, config_.rtp.ssrcs[0]); |
| 99 | } else { |
| 100 | for (size_t i = 0; i < config_.rtp.ssrcs.size(); ++i) { |
pbos@webrtc.org | b581c90 | 2013-10-28 16:32:01 +0000 | [diff] [blame] | 101 | rtp_rtcp_->SetLocalSSRC(channel_, |
| 102 | config_.rtp.ssrcs[i], |
| 103 | kViEStreamTypeNormal, |
| 104 | static_cast<unsigned char>(i)); |
stefan@webrtc.org | a0a91d8 | 2013-08-22 09:29:56 +0000 | [diff] [blame] | 105 | } |
| 106 | } |
stefan@webrtc.org | a0a91d8 | 2013-08-22 09:29:56 +0000 | [diff] [blame] | 107 | rtp_rtcp_->SetTransmissionSmoothingStatus(channel_, config_.pacing); |
pbos@webrtc.org | f952fce | 2013-09-16 13:01:47 +0000 | [diff] [blame] | 108 | if (!config_.rtp.rtx.ssrcs.empty()) { |
| 109 | assert(config_.rtp.rtx.ssrcs.size() == config_.rtp.ssrcs.size()); |
| 110 | for (size_t i = 0; i < config_.rtp.rtx.ssrcs.size(); ++i) { |
pbos@webrtc.org | b581c90 | 2013-10-28 16:32:01 +0000 | [diff] [blame] | 111 | rtp_rtcp_->SetLocalSSRC(channel_, |
| 112 | config_.rtp.rtx.ssrcs[i], |
| 113 | kViEStreamTypeRtx, |
| 114 | static_cast<unsigned char>(i)); |
pbos@webrtc.org | f952fce | 2013-09-16 13:01:47 +0000 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | if (config_.rtp.rtx.rtx_payload_type != 0) { |
| 118 | rtp_rtcp_->SetRtxSendPayloadType(channel_, |
| 119 | config_.rtp.rtx.rtx_payload_type); |
| 120 | } |
| 121 | } |
pbos@webrtc.org | 905cebd | 2013-09-11 10:14:56 +0000 | [diff] [blame] | 122 | |
| 123 | for (size_t i = 0; i < config_.rtp.extensions.size(); ++i) { |
| 124 | const std::string& extension = config_.rtp.extensions[i].name; |
| 125 | int id = config_.rtp.extensions[i].id; |
| 126 | if (extension == "toffset") { |
| 127 | if (rtp_rtcp_->SetSendTimestampOffsetStatus(channel_, true, id) != 0) |
| 128 | abort(); |
pbos@webrtc.org | e22b761 | 2013-09-11 19:00:39 +0000 | [diff] [blame] | 129 | } else if (extension == "abs-send-time") { |
| 130 | if (rtp_rtcp_->SetSendAbsoluteSendTimeStatus(channel_, true, id) != 0) |
| 131 | abort(); |
pbos@webrtc.org | 905cebd | 2013-09-11 10:14:56 +0000 | [diff] [blame] | 132 | } else { |
| 133 | abort(); // Unsupported extension. |
| 134 | } |
| 135 | } |
pbos@webrtc.org | dc8c883 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 136 | |
pbos@webrtc.org | aa693dd | 2013-09-20 11:56:26 +0000 | [diff] [blame] | 137 | // Enable NACK, FEC or both. |
| 138 | if (config_.rtp.fec.red_payload_type != -1) { |
| 139 | assert(config_.rtp.fec.ulpfec_payload_type != -1); |
| 140 | if (config_.rtp.nack.rtp_history_ms > 0) { |
| 141 | rtp_rtcp_->SetHybridNACKFECStatus( |
| 142 | channel_, |
| 143 | true, |
| 144 | static_cast<unsigned char>(config_.rtp.fec.red_payload_type), |
| 145 | static_cast<unsigned char>(config_.rtp.fec.ulpfec_payload_type)); |
| 146 | } else { |
| 147 | rtp_rtcp_->SetFECStatus( |
| 148 | channel_, |
| 149 | true, |
| 150 | static_cast<unsigned char>(config_.rtp.fec.red_payload_type), |
| 151 | static_cast<unsigned char>(config_.rtp.fec.ulpfec_payload_type)); |
| 152 | } |
| 153 | } else { |
| 154 | rtp_rtcp_->SetNACKStatus(channel_, config_.rtp.nack.rtp_history_ms > 0); |
| 155 | } |
| 156 | |
pbos@webrtc.org | debc672 | 2013-08-22 09:42:17 +0000 | [diff] [blame] | 157 | char rtcp_cname[ViERTP_RTCP::KMaxRTCPCNameLength]; |
| 158 | assert(config_.rtp.c_name.length() < ViERTP_RTCP::KMaxRTCPCNameLength); |
| 159 | strncpy(rtcp_cname, config_.rtp.c_name.c_str(), sizeof(rtcp_cname) - 1); |
| 160 | rtcp_cname[sizeof(rtcp_cname) - 1] = '\0'; |
| 161 | |
| 162 | rtp_rtcp_->SetRTCPCName(channel_, rtcp_cname); |
| 163 | |
pbos@webrtc.org | dc8c883 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 164 | capture_ = ViECapture::GetInterface(video_engine); |
| 165 | capture_->AllocateExternalCaptureDevice(capture_id_, external_capture_); |
| 166 | capture_->ConnectCaptureDevice(capture_id_, channel_); |
| 167 | |
| 168 | network_ = ViENetwork::GetInterface(video_engine); |
| 169 | assert(network_ != NULL); |
| 170 | |
pbos@webrtc.org | 26d75f3 | 2013-09-18 11:52:42 +0000 | [diff] [blame] | 171 | network_->RegisterSendTransport(channel_, transport_adapter_); |
sprang@webrtc.org | 6133dd5 | 2013-10-16 13:29:14 +0000 | [diff] [blame] | 172 | // 28 to match packet overhead in ModuleRtpRtcpImpl. |
pbos@webrtc.org | b581c90 | 2013-10-28 16:32:01 +0000 | [diff] [blame] | 173 | network_->SetMTU(channel_, |
| 174 | static_cast<unsigned int>(config_.rtp.max_packet_size + 28)); |
pbos@webrtc.org | dc8c883 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 175 | |
stefan@webrtc.org | a0a91d8 | 2013-08-22 09:29:56 +0000 | [diff] [blame] | 176 | if (config.encoder) { |
| 177 | external_codec_ = ViEExternalCodec::GetInterface(video_engine); |
| 178 | if (external_codec_->RegisterExternalSendCodec( |
| 179 | channel_, config.codec.plType, config.encoder, |
| 180 | config.internal_source) != 0) { |
| 181 | abort(); |
| 182 | } |
| 183 | } |
| 184 | |
pbos@webrtc.org | dc8c883 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 185 | codec_ = ViECodec::GetInterface(video_engine); |
pbos@webrtc.org | 8f2997c | 2013-11-14 08:58:14 +0000 | [diff] [blame] | 186 | if (!SetCodec(config_.codec)) |
pbos@webrtc.org | dc8c883 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 187 | abort(); |
mflodman@webrtc.org | ecbeb2b | 2013-07-23 11:35:00 +0000 | [diff] [blame] | 188 | |
| 189 | if (overuse_detection) { |
| 190 | overuse_observer_.reset( |
| 191 | new ResolutionAdaptor(codec_, channel_, config_.codec.width, |
| 192 | config_.codec.height)); |
| 193 | video_engine_base_->RegisterCpuOveruseObserver(channel_, |
pbos@webrtc.org | 905cebd | 2013-09-11 10:14:56 +0000 | [diff] [blame] | 194 | overuse_observer_.get()); |
mflodman@webrtc.org | ecbeb2b | 2013-07-23 11:35:00 +0000 | [diff] [blame] | 195 | } |
pbos@webrtc.org | 3ba57eb | 2013-10-21 10:34:43 +0000 | [diff] [blame] | 196 | |
| 197 | image_process_ = ViEImageProcess::GetInterface(video_engine); |
| 198 | image_process_->RegisterPreEncodeCallback(channel_, |
| 199 | config_.pre_encode_callback); |
henrik.lundin@webrtc.org | ce21c82 | 2013-10-23 11:04:57 +0000 | [diff] [blame] | 200 | |
henrik.lundin@webrtc.org | 8fdf191 | 2013-11-18 12:18:43 +0000 | [diff] [blame^] | 201 | if (config.suspend_below_min_bitrate) { |
| 202 | codec_->SuspendBelowMinBitrate(channel_); |
henrik.lundin@webrtc.org | ce21c82 | 2013-10-23 11:04:57 +0000 | [diff] [blame] | 203 | } |
pbos@webrtc.org | dc8c883 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | VideoSendStream::~VideoSendStream() { |
pbos@webrtc.org | 3ba57eb | 2013-10-21 10:34:43 +0000 | [diff] [blame] | 207 | image_process_->DeRegisterPreEncodeCallback(channel_); |
| 208 | |
pbos@webrtc.org | dc8c883 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 209 | network_->DeregisterSendTransport(channel_); |
pbos@webrtc.org | dc8c883 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 210 | |
| 211 | capture_->DisconnectCaptureDevice(channel_); |
| 212 | capture_->ReleaseCaptureDevice(capture_id_); |
| 213 | |
stefan@webrtc.org | a0a91d8 | 2013-08-22 09:29:56 +0000 | [diff] [blame] | 214 | if (external_codec_) { |
| 215 | external_codec_->DeRegisterExternalSendCodec(channel_, |
| 216 | config_.codec.plType); |
| 217 | } |
| 218 | |
pbos@webrtc.org | 3ba57eb | 2013-10-21 10:34:43 +0000 | [diff] [blame] | 219 | video_engine_base_->DeleteChannel(channel_); |
| 220 | |
| 221 | image_process_->Release(); |
pbos@webrtc.org | dc8c883 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 222 | video_engine_base_->Release(); |
| 223 | capture_->Release(); |
| 224 | codec_->Release(); |
stefan@webrtc.org | a0a91d8 | 2013-08-22 09:29:56 +0000 | [diff] [blame] | 225 | if (external_codec_) |
| 226 | external_codec_->Release(); |
pbos@webrtc.org | dc8c883 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 227 | network_->Release(); |
| 228 | rtp_rtcp_->Release(); |
| 229 | } |
| 230 | |
| 231 | void VideoSendStream::PutFrame(const I420VideoFrame& frame, |
pbos@webrtc.org | d9f9185 | 2013-05-23 12:37:11 +0000 | [diff] [blame] | 232 | uint32_t time_since_capture_ms) { |
| 233 | // TODO(pbos): frame_copy should happen after the VideoProcessingModule has |
| 234 | // resized the frame. |
pbos@webrtc.org | dc8c883 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 235 | I420VideoFrame frame_copy; |
| 236 | frame_copy.CopyFrame(frame); |
| 237 | |
pbos@webrtc.org | dc8c883 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 238 | ViEVideoFrameI420 vf; |
| 239 | |
| 240 | // TODO(pbos): This represents a memcpy step and is only required because |
| 241 | // external_capture_ only takes ViEVideoFrameI420s. |
| 242 | vf.y_plane = frame_copy.buffer(kYPlane); |
| 243 | vf.u_plane = frame_copy.buffer(kUPlane); |
| 244 | vf.v_plane = frame_copy.buffer(kVPlane); |
| 245 | vf.y_pitch = frame.stride(kYPlane); |
| 246 | vf.u_pitch = frame.stride(kUPlane); |
| 247 | vf.v_pitch = frame.stride(kVPlane); |
| 248 | vf.width = frame.width(); |
| 249 | vf.height = frame.height(); |
| 250 | |
pbos@webrtc.org | d9f9185 | 2013-05-23 12:37:11 +0000 | [diff] [blame] | 251 | external_capture_->IncomingFrameI420(vf, frame.render_time_ms()); |
pbos@webrtc.org | dc8c883 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 252 | |
| 253 | if (config_.local_renderer != NULL) { |
| 254 | config_.local_renderer->RenderFrame(frame, 0); |
| 255 | } |
| 256 | } |
| 257 | |
pbos@webrtc.org | d8e92c9 | 2013-08-23 09:19:30 +0000 | [diff] [blame] | 258 | VideoSendStreamInput* VideoSendStream::Input() { return this; } |
pbos@webrtc.org | dc8c883 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 259 | |
| 260 | void VideoSendStream::StartSend() { |
pbos@webrtc.org | d9f9185 | 2013-05-23 12:37:11 +0000 | [diff] [blame] | 261 | if (video_engine_base_->StartSend(channel_) != 0) |
| 262 | abort(); |
pbos@webrtc.org | bf9bc32 | 2013-08-05 12:01:36 +0000 | [diff] [blame] | 263 | if (video_engine_base_->StartReceive(channel_) != 0) |
| 264 | abort(); |
pbos@webrtc.org | dc8c883 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | void VideoSendStream::StopSend() { |
pbos@webrtc.org | d9f9185 | 2013-05-23 12:37:11 +0000 | [diff] [blame] | 268 | if (video_engine_base_->StopSend(channel_) != 0) |
| 269 | abort(); |
pbos@webrtc.org | bf9bc32 | 2013-08-05 12:01:36 +0000 | [diff] [blame] | 270 | if (video_engine_base_->StopReceive(channel_) != 0) |
| 271 | abort(); |
pbos@webrtc.org | dc8c883 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 272 | } |
| 273 | |
pbos@webrtc.org | 8f2997c | 2013-11-14 08:58:14 +0000 | [diff] [blame] | 274 | bool VideoSendStream::SetCodec(const VideoCodec& codec) { |
| 275 | if (codec.numberOfSimulcastStreams > 0) |
| 276 | assert(config_.rtp.ssrcs.size() >= codec.numberOfSimulcastStreams); |
| 277 | |
| 278 | CriticalSectionScoped crit(codec_lock_.get()); |
| 279 | if (codec_->SetSendCodec(channel_, codec) != 0) |
| 280 | return false; |
| 281 | |
| 282 | config_.codec = codec; |
| 283 | return true; |
pbos@webrtc.org | dc8c883 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 284 | } |
| 285 | |
pbos@webrtc.org | 8f2997c | 2013-11-14 08:58:14 +0000 | [diff] [blame] | 286 | VideoCodec VideoSendStream::GetCodec() { |
| 287 | CriticalSectionScoped crit(codec_lock_.get()); |
| 288 | return config_.codec; |
pbos@webrtc.org | dc8c883 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 289 | } |
| 290 | |
pbos@webrtc.org | bf9bc32 | 2013-08-05 12:01:36 +0000 | [diff] [blame] | 291 | bool VideoSendStream::DeliverRtcp(const uint8_t* packet, size_t length) { |
| 292 | return network_->ReceivedRTCPPacket( |
pbos@webrtc.org | 30c741a | 2013-08-05 13:25:51 +0000 | [diff] [blame] | 293 | channel_, packet, static_cast<int>(length)) == 0; |
pbos@webrtc.org | bf9bc32 | 2013-08-05 12:01:36 +0000 | [diff] [blame] | 294 | } |
| 295 | |
pbos@webrtc.org | dc8c883 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 296 | } // namespace internal |
| 297 | } // namespace webrtc |