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 | |
| 11 | #include "webrtc/video_engine/internal/video_send_stream.h" |
| 12 | |
pbos@webrtc.org | debc672 | 2013-08-22 09:42:17 +0000 | [diff] [blame] | 13 | #include <string.h> |
| 14 | |
pbos@webrtc.org | dc8c883 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 15 | #include <vector> |
| 16 | |
| 17 | #include "webrtc/common_video/libyuv/include/webrtc_libyuv.h" |
| 18 | #include "webrtc/video_engine/include/vie_base.h" |
| 19 | #include "webrtc/video_engine/include/vie_capture.h" |
| 20 | #include "webrtc/video_engine/include/vie_codec.h" |
stefan@webrtc.org | a0a91d8 | 2013-08-22 09:29:56 +0000 | [diff] [blame] | 21 | #include "webrtc/video_engine/include/vie_external_codec.h" |
pbos@webrtc.org | 3ba57eb | 2013-10-21 10:34:43 +0000 | [diff] [blame^] | 22 | #include "webrtc/video_engine/include/vie_image_process.h" |
pbos@webrtc.org | dc8c883 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 23 | #include "webrtc/video_engine/include/vie_network.h" |
| 24 | #include "webrtc/video_engine/include/vie_rtp_rtcp.h" |
| 25 | #include "webrtc/video_engine/new_include/video_send_stream.h" |
| 26 | |
| 27 | namespace webrtc { |
| 28 | namespace internal { |
| 29 | |
mflodman@webrtc.org | ecbeb2b | 2013-07-23 11:35:00 +0000 | [diff] [blame] | 30 | // Super simple and temporary overuse logic. This will move to the application |
| 31 | // as soon as the new API allows changing send codec on the fly. |
| 32 | class ResolutionAdaptor : public webrtc::CpuOveruseObserver { |
| 33 | public: |
| 34 | ResolutionAdaptor(ViECodec* codec, int channel, size_t width, size_t height) |
| 35 | : codec_(codec), |
| 36 | channel_(channel), |
| 37 | max_width_(width), |
| 38 | max_height_(height) {} |
| 39 | |
| 40 | virtual ~ResolutionAdaptor() {} |
| 41 | |
| 42 | virtual void OveruseDetected() OVERRIDE { |
| 43 | VideoCodec codec; |
| 44 | if (codec_->GetSendCodec(channel_, codec) != 0) |
| 45 | return; |
| 46 | |
| 47 | if (codec.width / 2 < min_width || codec.height / 2 < min_height) |
| 48 | return; |
| 49 | |
| 50 | codec.width /= 2; |
| 51 | codec.height /= 2; |
| 52 | codec_->SetSendCodec(channel_, codec); |
| 53 | } |
| 54 | |
| 55 | virtual void NormalUsage() OVERRIDE { |
| 56 | VideoCodec codec; |
| 57 | if (codec_->GetSendCodec(channel_, codec) != 0) |
| 58 | return; |
| 59 | |
| 60 | if (codec.width * 2u > max_width_ || codec.height * 2u > max_height_) |
| 61 | return; |
| 62 | |
| 63 | codec.width *= 2; |
| 64 | codec.height *= 2; |
| 65 | codec_->SetSendCodec(channel_, codec); |
| 66 | } |
| 67 | |
| 68 | private: |
| 69 | // Temporary and arbitrary chosen minimum resolution. |
| 70 | static const size_t min_width = 160; |
| 71 | static const size_t min_height = 120; |
| 72 | |
| 73 | ViECodec* codec_; |
| 74 | const int channel_; |
| 75 | |
| 76 | const size_t max_width_; |
| 77 | const size_t max_height_; |
| 78 | }; |
| 79 | |
pbos@webrtc.org | 12d5ede | 2013-07-09 08:02:33 +0000 | [diff] [blame] | 80 | VideoSendStream::VideoSendStream(newapi::Transport* transport, |
mflodman@webrtc.org | ecbeb2b | 2013-07-23 11:35:00 +0000 | [diff] [blame] | 81 | bool overuse_detection, |
pbos@webrtc.org | 12d5ede | 2013-07-09 08:02:33 +0000 | [diff] [blame] | 82 | webrtc::VideoEngine* video_engine, |
pbos@webrtc.org | d8e92c9 | 2013-08-23 09:19:30 +0000 | [diff] [blame] | 83 | const VideoSendStream::Config& config) |
pbos@webrtc.org | 26d75f3 | 2013-09-18 11:52:42 +0000 | [diff] [blame] | 84 | : transport_adapter_(transport), config_(config), external_codec_(NULL) { |
pbos@webrtc.org | dc8c883 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 85 | |
| 86 | if (config_.codec.numberOfSimulcastStreams > 0) { |
| 87 | assert(config_.rtp.ssrcs.size() == config_.codec.numberOfSimulcastStreams); |
| 88 | } else { |
| 89 | assert(config_.rtp.ssrcs.size() == 1); |
| 90 | } |
| 91 | |
| 92 | video_engine_base_ = ViEBase::GetInterface(video_engine); |
| 93 | video_engine_base_->CreateChannel(channel_); |
| 94 | assert(channel_ != -1); |
| 95 | |
| 96 | rtp_rtcp_ = ViERTP_RTCP::GetInterface(video_engine); |
| 97 | assert(rtp_rtcp_ != NULL); |
| 98 | |
stefan@webrtc.org | a0a91d8 | 2013-08-22 09:29:56 +0000 | [diff] [blame] | 99 | if (config_.rtp.ssrcs.size() == 1) { |
| 100 | rtp_rtcp_->SetLocalSSRC(channel_, config_.rtp.ssrcs[0]); |
| 101 | } else { |
| 102 | for (size_t i = 0; i < config_.rtp.ssrcs.size(); ++i) { |
| 103 | rtp_rtcp_->SetLocalSSRC(channel_, config_.rtp.ssrcs[i], |
| 104 | kViEStreamTypeNormal, i); |
| 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) { |
| 111 | rtp_rtcp_->SetLocalSSRC( |
| 112 | channel_, config_.rtp.rtx.ssrcs[i], kViEStreamTypeRtx, i); |
| 113 | } |
| 114 | |
| 115 | if (config_.rtp.rtx.rtx_payload_type != 0) { |
| 116 | rtp_rtcp_->SetRtxSendPayloadType(channel_, |
| 117 | config_.rtp.rtx.rtx_payload_type); |
| 118 | } |
| 119 | } |
pbos@webrtc.org | 905cebd | 2013-09-11 10:14:56 +0000 | [diff] [blame] | 120 | |
| 121 | for (size_t i = 0; i < config_.rtp.extensions.size(); ++i) { |
| 122 | const std::string& extension = config_.rtp.extensions[i].name; |
| 123 | int id = config_.rtp.extensions[i].id; |
| 124 | if (extension == "toffset") { |
| 125 | if (rtp_rtcp_->SetSendTimestampOffsetStatus(channel_, true, id) != 0) |
| 126 | abort(); |
pbos@webrtc.org | e22b761 | 2013-09-11 19:00:39 +0000 | [diff] [blame] | 127 | } else if (extension == "abs-send-time") { |
| 128 | if (rtp_rtcp_->SetSendAbsoluteSendTimeStatus(channel_, true, id) != 0) |
| 129 | abort(); |
pbos@webrtc.org | 905cebd | 2013-09-11 10:14:56 +0000 | [diff] [blame] | 130 | } else { |
| 131 | abort(); // Unsupported extension. |
| 132 | } |
| 133 | } |
pbos@webrtc.org | dc8c883 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 134 | |
pbos@webrtc.org | aa693dd | 2013-09-20 11:56:26 +0000 | [diff] [blame] | 135 | // Enable NACK, FEC or both. |
| 136 | if (config_.rtp.fec.red_payload_type != -1) { |
| 137 | assert(config_.rtp.fec.ulpfec_payload_type != -1); |
| 138 | if (config_.rtp.nack.rtp_history_ms > 0) { |
| 139 | rtp_rtcp_->SetHybridNACKFECStatus( |
| 140 | channel_, |
| 141 | true, |
| 142 | static_cast<unsigned char>(config_.rtp.fec.red_payload_type), |
| 143 | static_cast<unsigned char>(config_.rtp.fec.ulpfec_payload_type)); |
| 144 | } else { |
| 145 | rtp_rtcp_->SetFECStatus( |
| 146 | channel_, |
| 147 | true, |
| 148 | static_cast<unsigned char>(config_.rtp.fec.red_payload_type), |
| 149 | static_cast<unsigned char>(config_.rtp.fec.ulpfec_payload_type)); |
| 150 | } |
| 151 | } else { |
| 152 | rtp_rtcp_->SetNACKStatus(channel_, config_.rtp.nack.rtp_history_ms > 0); |
| 153 | } |
| 154 | |
pbos@webrtc.org | debc672 | 2013-08-22 09:42:17 +0000 | [diff] [blame] | 155 | char rtcp_cname[ViERTP_RTCP::KMaxRTCPCNameLength]; |
| 156 | assert(config_.rtp.c_name.length() < ViERTP_RTCP::KMaxRTCPCNameLength); |
| 157 | strncpy(rtcp_cname, config_.rtp.c_name.c_str(), sizeof(rtcp_cname) - 1); |
| 158 | rtcp_cname[sizeof(rtcp_cname) - 1] = '\0'; |
| 159 | |
| 160 | rtp_rtcp_->SetRTCPCName(channel_, rtcp_cname); |
| 161 | |
pbos@webrtc.org | dc8c883 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 162 | capture_ = ViECapture::GetInterface(video_engine); |
| 163 | capture_->AllocateExternalCaptureDevice(capture_id_, external_capture_); |
| 164 | capture_->ConnectCaptureDevice(capture_id_, channel_); |
| 165 | |
| 166 | network_ = ViENetwork::GetInterface(video_engine); |
| 167 | assert(network_ != NULL); |
| 168 | |
pbos@webrtc.org | 26d75f3 | 2013-09-18 11:52:42 +0000 | [diff] [blame] | 169 | network_->RegisterSendTransport(channel_, transport_adapter_); |
sprang@webrtc.org | 6133dd5 | 2013-10-16 13:29:14 +0000 | [diff] [blame] | 170 | // 28 to match packet overhead in ModuleRtpRtcpImpl. |
| 171 | network_->SetMTU(channel_, config_.rtp.max_packet_size + 28); |
pbos@webrtc.org | dc8c883 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 172 | |
stefan@webrtc.org | a0a91d8 | 2013-08-22 09:29:56 +0000 | [diff] [blame] | 173 | if (config.encoder) { |
| 174 | external_codec_ = ViEExternalCodec::GetInterface(video_engine); |
| 175 | if (external_codec_->RegisterExternalSendCodec( |
| 176 | channel_, config.codec.plType, config.encoder, |
| 177 | config.internal_source) != 0) { |
| 178 | abort(); |
| 179 | } |
| 180 | } |
| 181 | |
pbos@webrtc.org | dc8c883 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 182 | codec_ = ViECodec::GetInterface(video_engine); |
| 183 | if (codec_->SetSendCodec(channel_, config_.codec) != 0) { |
| 184 | abort(); |
| 185 | } |
mflodman@webrtc.org | ecbeb2b | 2013-07-23 11:35:00 +0000 | [diff] [blame] | 186 | |
| 187 | if (overuse_detection) { |
| 188 | overuse_observer_.reset( |
| 189 | new ResolutionAdaptor(codec_, channel_, config_.codec.width, |
| 190 | config_.codec.height)); |
| 191 | video_engine_base_->RegisterCpuOveruseObserver(channel_, |
pbos@webrtc.org | 905cebd | 2013-09-11 10:14:56 +0000 | [diff] [blame] | 192 | overuse_observer_.get()); |
mflodman@webrtc.org | ecbeb2b | 2013-07-23 11:35:00 +0000 | [diff] [blame] | 193 | } |
pbos@webrtc.org | 3ba57eb | 2013-10-21 10:34:43 +0000 | [diff] [blame^] | 194 | |
| 195 | image_process_ = ViEImageProcess::GetInterface(video_engine); |
| 196 | image_process_->RegisterPreEncodeCallback(channel_, |
| 197 | config_.pre_encode_callback); |
pbos@webrtc.org | dc8c883 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | VideoSendStream::~VideoSendStream() { |
pbos@webrtc.org | 3ba57eb | 2013-10-21 10:34:43 +0000 | [diff] [blame^] | 201 | image_process_->DeRegisterPreEncodeCallback(channel_); |
| 202 | |
pbos@webrtc.org | dc8c883 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 203 | network_->DeregisterSendTransport(channel_); |
pbos@webrtc.org | dc8c883 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 204 | |
| 205 | capture_->DisconnectCaptureDevice(channel_); |
| 206 | capture_->ReleaseCaptureDevice(capture_id_); |
| 207 | |
stefan@webrtc.org | a0a91d8 | 2013-08-22 09:29:56 +0000 | [diff] [blame] | 208 | if (external_codec_) { |
| 209 | external_codec_->DeRegisterExternalSendCodec(channel_, |
| 210 | config_.codec.plType); |
| 211 | } |
| 212 | |
pbos@webrtc.org | 3ba57eb | 2013-10-21 10:34:43 +0000 | [diff] [blame^] | 213 | video_engine_base_->DeleteChannel(channel_); |
| 214 | |
| 215 | image_process_->Release(); |
pbos@webrtc.org | dc8c883 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 216 | video_engine_base_->Release(); |
| 217 | capture_->Release(); |
| 218 | codec_->Release(); |
stefan@webrtc.org | a0a91d8 | 2013-08-22 09:29:56 +0000 | [diff] [blame] | 219 | if (external_codec_) |
| 220 | external_codec_->Release(); |
pbos@webrtc.org | dc8c883 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 221 | network_->Release(); |
| 222 | rtp_rtcp_->Release(); |
| 223 | } |
| 224 | |
| 225 | void VideoSendStream::PutFrame(const I420VideoFrame& frame, |
pbos@webrtc.org | d9f9185 | 2013-05-23 12:37:11 +0000 | [diff] [blame] | 226 | uint32_t time_since_capture_ms) { |
| 227 | // TODO(pbos): frame_copy should happen after the VideoProcessingModule has |
| 228 | // resized the frame. |
pbos@webrtc.org | dc8c883 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 229 | I420VideoFrame frame_copy; |
| 230 | frame_copy.CopyFrame(frame); |
| 231 | |
pbos@webrtc.org | dc8c883 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 232 | ViEVideoFrameI420 vf; |
| 233 | |
| 234 | // TODO(pbos): This represents a memcpy step and is only required because |
| 235 | // external_capture_ only takes ViEVideoFrameI420s. |
| 236 | vf.y_plane = frame_copy.buffer(kYPlane); |
| 237 | vf.u_plane = frame_copy.buffer(kUPlane); |
| 238 | vf.v_plane = frame_copy.buffer(kVPlane); |
| 239 | vf.y_pitch = frame.stride(kYPlane); |
| 240 | vf.u_pitch = frame.stride(kUPlane); |
| 241 | vf.v_pitch = frame.stride(kVPlane); |
| 242 | vf.width = frame.width(); |
| 243 | vf.height = frame.height(); |
| 244 | |
pbos@webrtc.org | d9f9185 | 2013-05-23 12:37:11 +0000 | [diff] [blame] | 245 | external_capture_->IncomingFrameI420(vf, frame.render_time_ms()); |
pbos@webrtc.org | dc8c883 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 246 | |
| 247 | if (config_.local_renderer != NULL) { |
| 248 | config_.local_renderer->RenderFrame(frame, 0); |
| 249 | } |
| 250 | } |
| 251 | |
pbos@webrtc.org | d8e92c9 | 2013-08-23 09:19:30 +0000 | [diff] [blame] | 252 | VideoSendStreamInput* VideoSendStream::Input() { return this; } |
pbos@webrtc.org | dc8c883 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 253 | |
| 254 | void VideoSendStream::StartSend() { |
pbos@webrtc.org | d9f9185 | 2013-05-23 12:37:11 +0000 | [diff] [blame] | 255 | if (video_engine_base_->StartSend(channel_) != 0) |
| 256 | abort(); |
pbos@webrtc.org | bf9bc32 | 2013-08-05 12:01:36 +0000 | [diff] [blame] | 257 | if (video_engine_base_->StartReceive(channel_) != 0) |
| 258 | abort(); |
pbos@webrtc.org | dc8c883 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | void VideoSendStream::StopSend() { |
pbos@webrtc.org | d9f9185 | 2013-05-23 12:37:11 +0000 | [diff] [blame] | 262 | if (video_engine_base_->StopSend(channel_) != 0) |
| 263 | abort(); |
pbos@webrtc.org | bf9bc32 | 2013-08-05 12:01:36 +0000 | [diff] [blame] | 264 | if (video_engine_base_->StopReceive(channel_) != 0) |
| 265 | abort(); |
pbos@webrtc.org | dc8c883 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 266 | } |
| 267 | |
pbos@webrtc.org | dc8c883 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 268 | bool VideoSendStream::SetTargetBitrate( |
pbos@webrtc.org | d9f9185 | 2013-05-23 12:37:11 +0000 | [diff] [blame] | 269 | int min_bitrate, |
| 270 | int max_bitrate, |
pbos@webrtc.org | dc8c883 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 271 | const std::vector<SimulcastStream>& streams) { |
| 272 | return false; |
| 273 | } |
| 274 | |
| 275 | void VideoSendStream::GetSendCodec(VideoCodec* send_codec) { |
| 276 | *send_codec = config_.codec; |
| 277 | } |
| 278 | |
pbos@webrtc.org | bf9bc32 | 2013-08-05 12:01:36 +0000 | [diff] [blame] | 279 | bool VideoSendStream::DeliverRtcp(const uint8_t* packet, size_t length) { |
| 280 | return network_->ReceivedRTCPPacket( |
pbos@webrtc.org | 30c741a | 2013-08-05 13:25:51 +0000 | [diff] [blame] | 281 | channel_, packet, static_cast<int>(length)) == 0; |
pbos@webrtc.org | bf9bc32 | 2013-08-05 12:01:36 +0000 | [diff] [blame] | 282 | } |
| 283 | |
pbos@webrtc.org | dc8c883 | 2013-05-16 12:08:03 +0000 | [diff] [blame] | 284 | } // namespace internal |
| 285 | } // namespace webrtc |