| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * libjingle | |
| 3 * Copyright 2014 Google Inc. | |
| 4 * | |
| 5 * Redistribution and use in source and binary forms, with or without | |
| 6 * modification, are permitted provided that the following conditions are met: | |
| 7 * | |
| 8 * 1. Redistributions of source code must retain the above copyright notice, | |
| 9 * this list of conditions and the following disclaimer. | |
| 10 * 2. Redistributions in binary form must reproduce the above copyright notice, | |
| 11 * this list of conditions and the following disclaimer in the documentation | |
| 12 * and/or other materials provided with the distribution. | |
| 13 * 3. The name of the author may not be used to endorse or promote products | |
| 14 * derived from this software without specific prior written permission. | |
| 15 * | |
| 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED | |
| 17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF | |
| 18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO | |
| 19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
| 20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
| 21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; | |
| 22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, | |
| 23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR | |
| 24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF | |
| 25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 26 */ | |
| 27 | |
| 28 #ifdef HAVE_WEBRTC_VIDEO | |
| 29 #include "talk/media/webrtc/webrtcvideoengine2.h" | |
| 30 | |
| 31 #include <algorithm> | |
| 32 #include <set> | |
| 33 #include <string> | |
| 34 | |
| 35 #include "talk/media/base/videocapturer.h" | |
| 36 #include "talk/media/base/videorenderer.h" | |
| 37 #include "talk/media/webrtc/constants.h" | |
| 38 #include "talk/media/webrtc/simulcast.h" | |
| 39 #include "talk/media/webrtc/webrtcmediaengine.h" | |
| 40 #include "talk/media/webrtc/webrtcvideoencoderfactory.h" | |
| 41 #include "talk/media/webrtc/webrtcvideoframe.h" | |
| 42 #include "talk/media/webrtc/webrtcvoiceengine.h" | |
| 43 #include "webrtc/base/buffer.h" | |
| 44 #include "webrtc/base/logging.h" | |
| 45 #include "webrtc/base/stringutils.h" | |
| 46 #include "webrtc/base/timeutils.h" | |
| 47 #include "webrtc/base/trace_event.h" | |
| 48 #include "webrtc/call.h" | |
| 49 #include "webrtc/modules/video_coding/codecs/h264/include/h264.h" | |
| 50 #include "webrtc/modules/video_coding/codecs/vp8/simulcast_encoder_adapter.h" | |
| 51 #include "webrtc/system_wrappers/include/field_trial.h" | |
| 52 #include "webrtc/video_decoder.h" | |
| 53 #include "webrtc/video_encoder.h" | |
| 54 | |
| 55 namespace cricket { | |
| 56 namespace { | |
| 57 | |
| 58 // Wrap cricket::WebRtcVideoEncoderFactory as a webrtc::VideoEncoderFactory. | |
| 59 class EncoderFactoryAdapter : public webrtc::VideoEncoderFactory { | |
| 60 public: | |
| 61 // EncoderFactoryAdapter doesn't take ownership of |factory|, which is owned | |
| 62 // by e.g. PeerConnectionFactory. | |
| 63 explicit EncoderFactoryAdapter(cricket::WebRtcVideoEncoderFactory* factory) | |
| 64 : factory_(factory) {} | |
| 65 virtual ~EncoderFactoryAdapter() {} | |
| 66 | |
| 67 // Implement webrtc::VideoEncoderFactory. | |
| 68 webrtc::VideoEncoder* Create() override { | |
| 69 return factory_->CreateVideoEncoder(webrtc::kVideoCodecVP8); | |
| 70 } | |
| 71 | |
| 72 void Destroy(webrtc::VideoEncoder* encoder) override { | |
| 73 return factory_->DestroyVideoEncoder(encoder); | |
| 74 } | |
| 75 | |
| 76 private: | |
| 77 cricket::WebRtcVideoEncoderFactory* const factory_; | |
| 78 }; | |
| 79 | |
| 80 // An encoder factory that wraps Create requests for simulcastable codec types | |
| 81 // with a webrtc::SimulcastEncoderAdapter. Non simulcastable codec type | |
| 82 // requests are just passed through to the contained encoder factory. | |
| 83 class WebRtcSimulcastEncoderFactory | |
| 84 : public cricket::WebRtcVideoEncoderFactory { | |
| 85 public: | |
| 86 // WebRtcSimulcastEncoderFactory doesn't take ownership of |factory|, which is | |
| 87 // owned by e.g. PeerConnectionFactory. | |
| 88 explicit WebRtcSimulcastEncoderFactory( | |
| 89 cricket::WebRtcVideoEncoderFactory* factory) | |
| 90 : factory_(factory) {} | |
| 91 | |
| 92 static bool UseSimulcastEncoderFactory( | |
| 93 const std::vector<VideoCodec>& codecs) { | |
| 94 // If any codec is VP8, use the simulcast factory. If asked to create a | |
| 95 // non-VP8 codec, we'll just return a contained factory encoder directly. | |
| 96 for (const auto& codec : codecs) { | |
| 97 if (codec.type == webrtc::kVideoCodecVP8) { | |
| 98 return true; | |
| 99 } | |
| 100 } | |
| 101 return false; | |
| 102 } | |
| 103 | |
| 104 webrtc::VideoEncoder* CreateVideoEncoder( | |
| 105 webrtc::VideoCodecType type) override { | |
| 106 RTC_DCHECK(factory_ != NULL); | |
| 107 // If it's a codec type we can simulcast, create a wrapped encoder. | |
| 108 if (type == webrtc::kVideoCodecVP8) { | |
| 109 return new webrtc::SimulcastEncoderAdapter( | |
| 110 new EncoderFactoryAdapter(factory_)); | |
| 111 } | |
| 112 webrtc::VideoEncoder* encoder = factory_->CreateVideoEncoder(type); | |
| 113 if (encoder) { | |
| 114 non_simulcast_encoders_.push_back(encoder); | |
| 115 } | |
| 116 return encoder; | |
| 117 } | |
| 118 | |
| 119 const std::vector<VideoCodec>& codecs() const override { | |
| 120 return factory_->codecs(); | |
| 121 } | |
| 122 | |
| 123 bool EncoderTypeHasInternalSource( | |
| 124 webrtc::VideoCodecType type) const override { | |
| 125 return factory_->EncoderTypeHasInternalSource(type); | |
| 126 } | |
| 127 | |
| 128 void DestroyVideoEncoder(webrtc::VideoEncoder* encoder) override { | |
| 129 // Check first to see if the encoder wasn't wrapped in a | |
| 130 // SimulcastEncoderAdapter. In that case, ask the factory to destroy it. | |
| 131 if (std::remove(non_simulcast_encoders_.begin(), | |
| 132 non_simulcast_encoders_.end(), | |
| 133 encoder) != non_simulcast_encoders_.end()) { | |
| 134 factory_->DestroyVideoEncoder(encoder); | |
| 135 return; | |
| 136 } | |
| 137 | |
| 138 // Otherwise, SimulcastEncoderAdapter can be deleted directly, and will call | |
| 139 // DestroyVideoEncoder on the factory for individual encoder instances. | |
| 140 delete encoder; | |
| 141 } | |
| 142 | |
| 143 private: | |
| 144 cricket::WebRtcVideoEncoderFactory* factory_; | |
| 145 // A list of encoders that were created without being wrapped in a | |
| 146 // SimulcastEncoderAdapter. | |
| 147 std::vector<webrtc::VideoEncoder*> non_simulcast_encoders_; | |
| 148 }; | |
| 149 | |
| 150 bool CodecIsInternallySupported(const std::string& codec_name) { | |
| 151 if (CodecNamesEq(codec_name, kVp8CodecName)) { | |
| 152 return true; | |
| 153 } | |
| 154 if (CodecNamesEq(codec_name, kVp9CodecName)) { | |
| 155 return true; | |
| 156 } | |
| 157 if (CodecNamesEq(codec_name, kH264CodecName)) { | |
| 158 return webrtc::H264Encoder::IsSupported() && | |
| 159 webrtc::H264Decoder::IsSupported(); | |
| 160 } | |
| 161 return false; | |
| 162 } | |
| 163 | |
| 164 void AddDefaultFeedbackParams(VideoCodec* codec) { | |
| 165 codec->AddFeedbackParam(FeedbackParam(kRtcpFbParamCcm, kRtcpFbCcmParamFir)); | |
| 166 codec->AddFeedbackParam(FeedbackParam(kRtcpFbParamNack, kParamValueEmpty)); | |
| 167 codec->AddFeedbackParam(FeedbackParam(kRtcpFbParamNack, kRtcpFbNackParamPli)); | |
| 168 codec->AddFeedbackParam(FeedbackParam(kRtcpFbParamRemb, kParamValueEmpty)); | |
| 169 codec->AddFeedbackParam( | |
| 170 FeedbackParam(kRtcpFbParamTransportCc, kParamValueEmpty)); | |
| 171 } | |
| 172 | |
| 173 static VideoCodec MakeVideoCodecWithDefaultFeedbackParams(int payload_type, | |
| 174 const char* name) { | |
| 175 VideoCodec codec(payload_type, name, kDefaultVideoMaxWidth, | |
| 176 kDefaultVideoMaxHeight, kDefaultVideoMaxFramerate, 0); | |
| 177 AddDefaultFeedbackParams(&codec); | |
| 178 return codec; | |
| 179 } | |
| 180 | |
| 181 static std::string CodecVectorToString(const std::vector<VideoCodec>& codecs) { | |
| 182 std::stringstream out; | |
| 183 out << '{'; | |
| 184 for (size_t i = 0; i < codecs.size(); ++i) { | |
| 185 out << codecs[i].ToString(); | |
| 186 if (i != codecs.size() - 1) { | |
| 187 out << ", "; | |
| 188 } | |
| 189 } | |
| 190 out << '}'; | |
| 191 return out.str(); | |
| 192 } | |
| 193 | |
| 194 static bool ValidateCodecFormats(const std::vector<VideoCodec>& codecs) { | |
| 195 bool has_video = false; | |
| 196 for (size_t i = 0; i < codecs.size(); ++i) { | |
| 197 if (!codecs[i].ValidateCodecFormat()) { | |
| 198 return false; | |
| 199 } | |
| 200 if (codecs[i].GetCodecType() == VideoCodec::CODEC_VIDEO) { | |
| 201 has_video = true; | |
| 202 } | |
| 203 } | |
| 204 if (!has_video) { | |
| 205 LOG(LS_ERROR) << "Setting codecs without a video codec is invalid: " | |
| 206 << CodecVectorToString(codecs); | |
| 207 return false; | |
| 208 } | |
| 209 return true; | |
| 210 } | |
| 211 | |
| 212 static bool ValidateStreamParams(const StreamParams& sp) { | |
| 213 if (sp.ssrcs.empty()) { | |
| 214 LOG(LS_ERROR) << "No SSRCs in stream parameters: " << sp.ToString(); | |
| 215 return false; | |
| 216 } | |
| 217 | |
| 218 std::vector<uint32_t> primary_ssrcs; | |
| 219 sp.GetPrimarySsrcs(&primary_ssrcs); | |
| 220 std::vector<uint32_t> rtx_ssrcs; | |
| 221 sp.GetFidSsrcs(primary_ssrcs, &rtx_ssrcs); | |
| 222 for (uint32_t rtx_ssrc : rtx_ssrcs) { | |
| 223 bool rtx_ssrc_present = false; | |
| 224 for (uint32_t sp_ssrc : sp.ssrcs) { | |
| 225 if (sp_ssrc == rtx_ssrc) { | |
| 226 rtx_ssrc_present = true; | |
| 227 break; | |
| 228 } | |
| 229 } | |
| 230 if (!rtx_ssrc_present) { | |
| 231 LOG(LS_ERROR) << "RTX SSRC '" << rtx_ssrc | |
| 232 << "' missing from StreamParams ssrcs: " << sp.ToString(); | |
| 233 return false; | |
| 234 } | |
| 235 } | |
| 236 if (!rtx_ssrcs.empty() && primary_ssrcs.size() != rtx_ssrcs.size()) { | |
| 237 LOG(LS_ERROR) | |
| 238 << "RTX SSRCs exist, but don't cover all SSRCs (unsupported): " | |
| 239 << sp.ToString(); | |
| 240 return false; | |
| 241 } | |
| 242 | |
| 243 return true; | |
| 244 } | |
| 245 | |
| 246 inline const webrtc::RtpExtension* FindHeaderExtension( | |
| 247 const std::vector<webrtc::RtpExtension>& extensions, | |
| 248 const std::string& name) { | |
| 249 for (const auto& kv : extensions) { | |
| 250 if (kv.name == name) { | |
| 251 return &kv; | |
| 252 } | |
| 253 } | |
| 254 return NULL; | |
| 255 } | |
| 256 | |
| 257 // Merges two fec configs and logs an error if a conflict arises | |
| 258 // such that merging in different order would trigger a different output. | |
| 259 static void MergeFecConfig(const webrtc::FecConfig& other, | |
| 260 webrtc::FecConfig* output) { | |
| 261 if (other.ulpfec_payload_type != -1) { | |
| 262 if (output->ulpfec_payload_type != -1 && | |
| 263 output->ulpfec_payload_type != other.ulpfec_payload_type) { | |
| 264 LOG(LS_WARNING) << "Conflict merging ulpfec_payload_type configs: " | |
| 265 << output->ulpfec_payload_type << " and " | |
| 266 << other.ulpfec_payload_type; | |
| 267 } | |
| 268 output->ulpfec_payload_type = other.ulpfec_payload_type; | |
| 269 } | |
| 270 if (other.red_payload_type != -1) { | |
| 271 if (output->red_payload_type != -1 && | |
| 272 output->red_payload_type != other.red_payload_type) { | |
| 273 LOG(LS_WARNING) << "Conflict merging red_payload_type configs: " | |
| 274 << output->red_payload_type << " and " | |
| 275 << other.red_payload_type; | |
| 276 } | |
| 277 output->red_payload_type = other.red_payload_type; | |
| 278 } | |
| 279 if (other.red_rtx_payload_type != -1) { | |
| 280 if (output->red_rtx_payload_type != -1 && | |
| 281 output->red_rtx_payload_type != other.red_rtx_payload_type) { | |
| 282 LOG(LS_WARNING) << "Conflict merging red_rtx_payload_type configs: " | |
| 283 << output->red_rtx_payload_type << " and " | |
| 284 << other.red_rtx_payload_type; | |
| 285 } | |
| 286 output->red_rtx_payload_type = other.red_rtx_payload_type; | |
| 287 } | |
| 288 } | |
| 289 | |
| 290 // Returns true if the given codec is disallowed from doing simulcast. | |
| 291 bool IsCodecBlacklistedForSimulcast(const std::string& codec_name) { | |
| 292 return CodecNamesEq(codec_name, kH264CodecName) || | |
| 293 CodecNamesEq(codec_name, kVp9CodecName); | |
| 294 } | |
| 295 | |
| 296 // The selected thresholds for QVGA and VGA corresponded to a QP around 10. | |
| 297 // The change in QP declined above the selected bitrates. | |
| 298 static int GetMaxDefaultVideoBitrateKbps(int width, int height) { | |
| 299 if (width * height <= 320 * 240) { | |
| 300 return 600; | |
| 301 } else if (width * height <= 640 * 480) { | |
| 302 return 1700; | |
| 303 } else if (width * height <= 960 * 540) { | |
| 304 return 2000; | |
| 305 } else { | |
| 306 return 2500; | |
| 307 } | |
| 308 } | |
| 309 } // namespace | |
| 310 | |
| 311 // Constants defined in talk/media/webrtc/constants.h | |
| 312 // TODO(pbos): Move these to a separate constants.cc file. | |
| 313 const int kMinVideoBitrate = 30; | |
| 314 const int kStartVideoBitrate = 300; | |
| 315 | |
| 316 const int kVideoMtu = 1200; | |
| 317 const int kVideoRtpBufferSize = 65536; | |
| 318 | |
| 319 // This constant is really an on/off, lower-level configurable NACK history | |
| 320 // duration hasn't been implemented. | |
| 321 static const int kNackHistoryMs = 1000; | |
| 322 | |
| 323 static const int kDefaultQpMax = 56; | |
| 324 | |
| 325 static const int kDefaultRtcpReceiverReportSsrc = 1; | |
| 326 | |
| 327 std::vector<VideoCodec> DefaultVideoCodecList() { | |
| 328 std::vector<VideoCodec> codecs; | |
| 329 codecs.push_back(MakeVideoCodecWithDefaultFeedbackParams(kDefaultVp8PlType, | |
| 330 kVp8CodecName)); | |
| 331 if (CodecIsInternallySupported(kVp9CodecName)) { | |
| 332 codecs.push_back(MakeVideoCodecWithDefaultFeedbackParams(kDefaultVp9PlType, | |
| 333 kVp9CodecName)); | |
| 334 // TODO(andresp): Add rtx codec for vp9 and verify it works. | |
| 335 } | |
| 336 if (CodecIsInternallySupported(kH264CodecName)) { | |
| 337 codecs.push_back(MakeVideoCodecWithDefaultFeedbackParams(kDefaultH264PlType, | |
| 338 kH264CodecName)); | |
| 339 } | |
| 340 codecs.push_back( | |
| 341 VideoCodec::CreateRtxCodec(kDefaultRtxVp8PlType, kDefaultVp8PlType)); | |
| 342 codecs.push_back(VideoCodec(kDefaultRedPlType, kRedCodecName)); | |
| 343 codecs.push_back(VideoCodec(kDefaultUlpfecType, kUlpfecCodecName)); | |
| 344 return codecs; | |
| 345 } | |
| 346 | |
| 347 std::vector<webrtc::VideoStream> | |
| 348 WebRtcVideoChannel2::WebRtcVideoSendStream::CreateSimulcastVideoStreams( | |
| 349 const VideoCodec& codec, | |
| 350 const VideoOptions& options, | |
| 351 int max_bitrate_bps, | |
| 352 size_t num_streams) { | |
| 353 int max_qp = kDefaultQpMax; | |
| 354 codec.GetParam(kCodecParamMaxQuantization, &max_qp); | |
| 355 | |
| 356 return GetSimulcastConfig( | |
| 357 num_streams, codec.width, codec.height, max_bitrate_bps, max_qp, | |
| 358 codec.framerate != 0 ? codec.framerate : kDefaultVideoMaxFramerate); | |
| 359 } | |
| 360 | |
| 361 std::vector<webrtc::VideoStream> | |
| 362 WebRtcVideoChannel2::WebRtcVideoSendStream::CreateVideoStreams( | |
| 363 const VideoCodec& codec, | |
| 364 const VideoOptions& options, | |
| 365 int max_bitrate_bps, | |
| 366 size_t num_streams) { | |
| 367 int codec_max_bitrate_kbps; | |
| 368 if (codec.GetParam(kCodecParamMaxBitrate, &codec_max_bitrate_kbps)) { | |
| 369 max_bitrate_bps = codec_max_bitrate_kbps * 1000; | |
| 370 } | |
| 371 if (num_streams != 1) { | |
| 372 return CreateSimulcastVideoStreams(codec, options, max_bitrate_bps, | |
| 373 num_streams); | |
| 374 } | |
| 375 | |
| 376 // For unset max bitrates set default bitrate for non-simulcast. | |
| 377 if (max_bitrate_bps <= 0) { | |
| 378 max_bitrate_bps = | |
| 379 GetMaxDefaultVideoBitrateKbps(codec.width, codec.height) * 1000; | |
| 380 } | |
| 381 | |
| 382 webrtc::VideoStream stream; | |
| 383 stream.width = codec.width; | |
| 384 stream.height = codec.height; | |
| 385 stream.max_framerate = | |
| 386 codec.framerate != 0 ? codec.framerate : kDefaultVideoMaxFramerate; | |
| 387 | |
| 388 stream.min_bitrate_bps = kMinVideoBitrate * 1000; | |
| 389 stream.target_bitrate_bps = stream.max_bitrate_bps = max_bitrate_bps; | |
| 390 | |
| 391 int max_qp = kDefaultQpMax; | |
| 392 codec.GetParam(kCodecParamMaxQuantization, &max_qp); | |
| 393 stream.max_qp = max_qp; | |
| 394 std::vector<webrtc::VideoStream> streams; | |
| 395 streams.push_back(stream); | |
| 396 return streams; | |
| 397 } | |
| 398 | |
| 399 void* WebRtcVideoChannel2::WebRtcVideoSendStream::ConfigureVideoEncoderSettings( | |
| 400 const VideoCodec& codec, | |
| 401 const VideoOptions& options, | |
| 402 bool is_screencast) { | |
| 403 // No automatic resizing when using simulcast or screencast. | |
| 404 bool automatic_resize = | |
| 405 !is_screencast && parameters_.config.rtp.ssrcs.size() == 1; | |
| 406 bool frame_dropping = !is_screencast; | |
| 407 bool denoising; | |
| 408 bool codec_default_denoising = false; | |
| 409 if (is_screencast) { | |
| 410 denoising = false; | |
| 411 } else { | |
| 412 // Use codec default if video_noise_reduction is unset. | |
| 413 codec_default_denoising = !options.video_noise_reduction; | |
| 414 denoising = options.video_noise_reduction.value_or(false); | |
| 415 } | |
| 416 | |
| 417 if (CodecNamesEq(codec.name, kVp8CodecName)) { | |
| 418 encoder_settings_.vp8 = webrtc::VideoEncoder::GetDefaultVp8Settings(); | |
| 419 encoder_settings_.vp8.automaticResizeOn = automatic_resize; | |
| 420 // VP8 denoising is enabled by default. | |
| 421 encoder_settings_.vp8.denoisingOn = | |
| 422 codec_default_denoising ? true : denoising; | |
| 423 encoder_settings_.vp8.frameDroppingOn = frame_dropping; | |
| 424 return &encoder_settings_.vp8; | |
| 425 } | |
| 426 if (CodecNamesEq(codec.name, kVp9CodecName)) { | |
| 427 encoder_settings_.vp9 = webrtc::VideoEncoder::GetDefaultVp9Settings(); | |
| 428 // VP9 denoising is disabled by default. | |
| 429 encoder_settings_.vp9.denoisingOn = | |
| 430 codec_default_denoising ? false : denoising; | |
| 431 encoder_settings_.vp9.frameDroppingOn = frame_dropping; | |
| 432 return &encoder_settings_.vp9; | |
| 433 } | |
| 434 return NULL; | |
| 435 } | |
| 436 | |
| 437 DefaultUnsignalledSsrcHandler::DefaultUnsignalledSsrcHandler() | |
| 438 : default_recv_ssrc_(0), default_renderer_(NULL) {} | |
| 439 | |
| 440 UnsignalledSsrcHandler::Action DefaultUnsignalledSsrcHandler::OnUnsignalledSsrc( | |
| 441 WebRtcVideoChannel2* channel, | |
| 442 uint32_t ssrc) { | |
| 443 if (default_recv_ssrc_ != 0) { // Already one default stream. | |
| 444 LOG(LS_WARNING) << "Unknown SSRC, but default receive stream already set."; | |
| 445 return kDropPacket; | |
| 446 } | |
| 447 | |
| 448 StreamParams sp; | |
| 449 sp.ssrcs.push_back(ssrc); | |
| 450 LOG(LS_INFO) << "Creating default receive stream for SSRC=" << ssrc << "."; | |
| 451 if (!channel->AddRecvStream(sp, true)) { | |
| 452 LOG(LS_WARNING) << "Could not create default receive stream."; | |
| 453 } | |
| 454 | |
| 455 channel->SetRenderer(ssrc, default_renderer_); | |
| 456 default_recv_ssrc_ = ssrc; | |
| 457 return kDeliverPacket; | |
| 458 } | |
| 459 | |
| 460 VideoRenderer* DefaultUnsignalledSsrcHandler::GetDefaultRenderer() const { | |
| 461 return default_renderer_; | |
| 462 } | |
| 463 | |
| 464 void DefaultUnsignalledSsrcHandler::SetDefaultRenderer( | |
| 465 VideoMediaChannel* channel, | |
| 466 VideoRenderer* renderer) { | |
| 467 default_renderer_ = renderer; | |
| 468 if (default_recv_ssrc_ != 0) { | |
| 469 channel->SetRenderer(default_recv_ssrc_, default_renderer_); | |
| 470 } | |
| 471 } | |
| 472 | |
| 473 WebRtcVideoEngine2::WebRtcVideoEngine2() | |
| 474 : initialized_(false), | |
| 475 external_decoder_factory_(NULL), | |
| 476 external_encoder_factory_(NULL) { | |
| 477 LOG(LS_INFO) << "WebRtcVideoEngine2::WebRtcVideoEngine2()"; | |
| 478 video_codecs_ = GetSupportedCodecs(); | |
| 479 } | |
| 480 | |
| 481 WebRtcVideoEngine2::~WebRtcVideoEngine2() { | |
| 482 LOG(LS_INFO) << "WebRtcVideoEngine2::~WebRtcVideoEngine2"; | |
| 483 } | |
| 484 | |
| 485 void WebRtcVideoEngine2::Init() { | |
| 486 LOG(LS_INFO) << "WebRtcVideoEngine2::Init"; | |
| 487 initialized_ = true; | |
| 488 } | |
| 489 | |
| 490 WebRtcVideoChannel2* WebRtcVideoEngine2::CreateChannel( | |
| 491 webrtc::Call* call, | |
| 492 const VideoOptions& options) { | |
| 493 RTC_DCHECK(initialized_); | |
| 494 LOG(LS_INFO) << "CreateChannel. Options: " << options.ToString(); | |
| 495 return new WebRtcVideoChannel2(call, options, video_codecs_, | |
| 496 external_encoder_factory_, external_decoder_factory_); | |
| 497 } | |
| 498 | |
| 499 const std::vector<VideoCodec>& WebRtcVideoEngine2::codecs() const { | |
| 500 return video_codecs_; | |
| 501 } | |
| 502 | |
| 503 RtpCapabilities WebRtcVideoEngine2::GetCapabilities() const { | |
| 504 RtpCapabilities capabilities; | |
| 505 capabilities.header_extensions.push_back( | |
| 506 RtpHeaderExtension(kRtpTimestampOffsetHeaderExtension, | |
| 507 kRtpTimestampOffsetHeaderExtensionDefaultId)); | |
| 508 capabilities.header_extensions.push_back( | |
| 509 RtpHeaderExtension(kRtpAbsoluteSenderTimeHeaderExtension, | |
| 510 kRtpAbsoluteSenderTimeHeaderExtensionDefaultId)); | |
| 511 capabilities.header_extensions.push_back( | |
| 512 RtpHeaderExtension(kRtpVideoRotationHeaderExtension, | |
| 513 kRtpVideoRotationHeaderExtensionDefaultId)); | |
| 514 if (webrtc::field_trial::FindFullName("WebRTC-SendSideBwe") == "Enabled") { | |
| 515 capabilities.header_extensions.push_back(RtpHeaderExtension( | |
| 516 kRtpTransportSequenceNumberHeaderExtension, | |
| 517 kRtpTransportSequenceNumberHeaderExtensionDefaultId)); | |
| 518 } | |
| 519 return capabilities; | |
| 520 } | |
| 521 | |
| 522 void WebRtcVideoEngine2::SetExternalDecoderFactory( | |
| 523 WebRtcVideoDecoderFactory* decoder_factory) { | |
| 524 RTC_DCHECK(!initialized_); | |
| 525 external_decoder_factory_ = decoder_factory; | |
| 526 } | |
| 527 | |
| 528 void WebRtcVideoEngine2::SetExternalEncoderFactory( | |
| 529 WebRtcVideoEncoderFactory* encoder_factory) { | |
| 530 RTC_DCHECK(!initialized_); | |
| 531 if (external_encoder_factory_ == encoder_factory) | |
| 532 return; | |
| 533 | |
| 534 // No matter what happens we shouldn't hold on to a stale | |
| 535 // WebRtcSimulcastEncoderFactory. | |
| 536 simulcast_encoder_factory_.reset(); | |
| 537 | |
| 538 if (encoder_factory && | |
| 539 WebRtcSimulcastEncoderFactory::UseSimulcastEncoderFactory( | |
| 540 encoder_factory->codecs())) { | |
| 541 simulcast_encoder_factory_.reset( | |
| 542 new WebRtcSimulcastEncoderFactory(encoder_factory)); | |
| 543 encoder_factory = simulcast_encoder_factory_.get(); | |
| 544 } | |
| 545 external_encoder_factory_ = encoder_factory; | |
| 546 | |
| 547 video_codecs_ = GetSupportedCodecs(); | |
| 548 } | |
| 549 | |
| 550 bool WebRtcVideoEngine2::EnableTimedRender() { | |
| 551 // TODO(pbos): Figure out whether this can be removed. | |
| 552 return true; | |
| 553 } | |
| 554 | |
| 555 // Checks to see whether we comprehend and could receive a particular codec | |
| 556 bool WebRtcVideoEngine2::FindCodec(const VideoCodec& in) { | |
| 557 // TODO(pbos): Probe encoder factory to figure out that the codec is supported | |
| 558 // if supported by the encoder factory. Add a corresponding test that fails | |
| 559 // with this code (that doesn't ask the factory). | |
| 560 for (size_t j = 0; j < video_codecs_.size(); ++j) { | |
| 561 VideoCodec codec(video_codecs_[j].id, video_codecs_[j].name, 0, 0, 0, 0); | |
| 562 if (codec.Matches(in)) { | |
| 563 return true; | |
| 564 } | |
| 565 } | |
| 566 return false; | |
| 567 } | |
| 568 | |
| 569 // Ignore spammy trace messages, mostly from the stats API when we haven't | |
| 570 // gotten RTCP info yet from the remote side. | |
| 571 bool WebRtcVideoEngine2::ShouldIgnoreTrace(const std::string& trace) { | |
| 572 static const char* const kTracesToIgnore[] = {NULL}; | |
| 573 for (const char* const* p = kTracesToIgnore; *p; ++p) { | |
| 574 if (trace.find(*p) == 0) { | |
| 575 return true; | |
| 576 } | |
| 577 } | |
| 578 return false; | |
| 579 } | |
| 580 | |
| 581 std::vector<VideoCodec> WebRtcVideoEngine2::GetSupportedCodecs() const { | |
| 582 std::vector<VideoCodec> supported_codecs = DefaultVideoCodecList(); | |
| 583 | |
| 584 if (external_encoder_factory_ == NULL) { | |
| 585 return supported_codecs; | |
| 586 } | |
| 587 | |
| 588 const std::vector<WebRtcVideoEncoderFactory::VideoCodec>& codecs = | |
| 589 external_encoder_factory_->codecs(); | |
| 590 for (size_t i = 0; i < codecs.size(); ++i) { | |
| 591 // Don't add internally-supported codecs twice. | |
| 592 if (CodecIsInternallySupported(codecs[i].name)) { | |
| 593 continue; | |
| 594 } | |
| 595 | |
| 596 // External video encoders are given payloads 120-127. This also means that | |
| 597 // we only support up to 8 external payload types. | |
| 598 const int kExternalVideoPayloadTypeBase = 120; | |
| 599 size_t payload_type = kExternalVideoPayloadTypeBase + i; | |
| 600 RTC_DCHECK(payload_type < 128); | |
| 601 VideoCodec codec(static_cast<int>(payload_type), | |
| 602 codecs[i].name, | |
| 603 codecs[i].max_width, | |
| 604 codecs[i].max_height, | |
| 605 codecs[i].max_fps, | |
| 606 0); | |
| 607 | |
| 608 AddDefaultFeedbackParams(&codec); | |
| 609 supported_codecs.push_back(codec); | |
| 610 } | |
| 611 return supported_codecs; | |
| 612 } | |
| 613 | |
| 614 WebRtcVideoChannel2::WebRtcVideoChannel2( | |
| 615 webrtc::Call* call, | |
| 616 const VideoOptions& options, | |
| 617 const std::vector<VideoCodec>& recv_codecs, | |
| 618 WebRtcVideoEncoderFactory* external_encoder_factory, | |
| 619 WebRtcVideoDecoderFactory* external_decoder_factory) | |
| 620 : call_(call), | |
| 621 unsignalled_ssrc_handler_(&default_unsignalled_ssrc_handler_), | |
| 622 external_encoder_factory_(external_encoder_factory), | |
| 623 external_decoder_factory_(external_decoder_factory) { | |
| 624 RTC_DCHECK(thread_checker_.CalledOnValidThread()); | |
| 625 SetDefaultOptions(); | |
| 626 options_.SetAll(options); | |
| 627 if (options_.cpu_overuse_detection) | |
| 628 signal_cpu_adaptation_ = *options_.cpu_overuse_detection; | |
| 629 rtcp_receiver_report_ssrc_ = kDefaultRtcpReceiverReportSsrc; | |
| 630 sending_ = false; | |
| 631 default_send_ssrc_ = 0; | |
| 632 SetRecvCodecs(recv_codecs); | |
| 633 } | |
| 634 | |
| 635 void WebRtcVideoChannel2::SetDefaultOptions() { | |
| 636 options_.cpu_overuse_detection = rtc::Optional<bool>(true); | |
| 637 options_.dscp = rtc::Optional<bool>(false); | |
| 638 options_.suspend_below_min_bitrate = rtc::Optional<bool>(false); | |
| 639 options_.screencast_min_bitrate = rtc::Optional<int>(0); | |
| 640 } | |
| 641 | |
| 642 WebRtcVideoChannel2::~WebRtcVideoChannel2() { | |
| 643 for (auto& kv : send_streams_) | |
| 644 delete kv.second; | |
| 645 for (auto& kv : receive_streams_) | |
| 646 delete kv.second; | |
| 647 } | |
| 648 | |
| 649 bool WebRtcVideoChannel2::CodecIsExternallySupported( | |
| 650 const std::string& name) const { | |
| 651 if (external_encoder_factory_ == NULL) { | |
| 652 return false; | |
| 653 } | |
| 654 | |
| 655 const std::vector<WebRtcVideoEncoderFactory::VideoCodec> external_codecs = | |
| 656 external_encoder_factory_->codecs(); | |
| 657 for (size_t c = 0; c < external_codecs.size(); ++c) { | |
| 658 if (CodecNamesEq(name, external_codecs[c].name)) { | |
| 659 return true; | |
| 660 } | |
| 661 } | |
| 662 return false; | |
| 663 } | |
| 664 | |
| 665 std::vector<WebRtcVideoChannel2::VideoCodecSettings> | |
| 666 WebRtcVideoChannel2::FilterSupportedCodecs( | |
| 667 const std::vector<WebRtcVideoChannel2::VideoCodecSettings>& mapped_codecs) | |
| 668 const { | |
| 669 std::vector<VideoCodecSettings> supported_codecs; | |
| 670 for (size_t i = 0; i < mapped_codecs.size(); ++i) { | |
| 671 const VideoCodecSettings& codec = mapped_codecs[i]; | |
| 672 if (CodecIsInternallySupported(codec.codec.name) || | |
| 673 CodecIsExternallySupported(codec.codec.name)) { | |
| 674 supported_codecs.push_back(codec); | |
| 675 } | |
| 676 } | |
| 677 return supported_codecs; | |
| 678 } | |
| 679 | |
| 680 bool WebRtcVideoChannel2::ReceiveCodecsHaveChanged( | |
| 681 std::vector<VideoCodecSettings> before, | |
| 682 std::vector<VideoCodecSettings> after) { | |
| 683 if (before.size() != after.size()) { | |
| 684 return true; | |
| 685 } | |
| 686 // The receive codec order doesn't matter, so we sort the codecs before | |
| 687 // comparing. This is necessary because currently the | |
| 688 // only way to change the send codec is to munge SDP, which causes | |
| 689 // the receive codec list to change order, which causes the streams | |
| 690 // to be recreates which causes a "blink" of black video. In order | |
| 691 // to support munging the SDP in this way without recreating receive | |
| 692 // streams, we ignore the order of the received codecs so that | |
| 693 // changing the order doesn't cause this "blink". | |
| 694 auto comparison = | |
| 695 [](const VideoCodecSettings& codec1, const VideoCodecSettings& codec2) { | |
| 696 return codec1.codec.id > codec2.codec.id; | |
| 697 }; | |
| 698 std::sort(before.begin(), before.end(), comparison); | |
| 699 std::sort(after.begin(), after.end(), comparison); | |
| 700 for (size_t i = 0; i < before.size(); ++i) { | |
| 701 // For the same reason that we sort the codecs, we also ignore the | |
| 702 // preference. We don't want a preference change on the receive | |
| 703 // side to cause recreation of the stream. | |
| 704 before[i].codec.preference = 0; | |
| 705 after[i].codec.preference = 0; | |
| 706 if (before[i] != after[i]) { | |
| 707 return true; | |
| 708 } | |
| 709 } | |
| 710 return false; | |
| 711 } | |
| 712 | |
| 713 bool WebRtcVideoChannel2::SetSendParameters(const VideoSendParameters& params) { | |
| 714 TRACE_EVENT0("webrtc", "WebRtcVideoChannel2::SetSendParameters"); | |
| 715 LOG(LS_INFO) << "SetSendParameters: " << params.ToString(); | |
| 716 // TODO(pbos): Refactor this to only recreate the send streams once | |
| 717 // instead of 4 times. | |
| 718 if (!SetSendCodecs(params.codecs) || | |
| 719 !SetSendRtpHeaderExtensions(params.extensions) || | |
| 720 !SetMaxSendBandwidth(params.max_bandwidth_bps) || | |
| 721 !SetOptions(params.options)) { | |
| 722 return false; | |
| 723 } | |
| 724 if (send_params_.rtcp.reduced_size != params.rtcp.reduced_size) { | |
| 725 rtc::CritScope stream_lock(&stream_crit_); | |
| 726 for (auto& kv : send_streams_) { | |
| 727 kv.second->SetSendParameters(params); | |
| 728 } | |
| 729 } | |
| 730 send_params_ = params; | |
| 731 return true; | |
| 732 } | |
| 733 | |
| 734 bool WebRtcVideoChannel2::SetRecvParameters(const VideoRecvParameters& params) { | |
| 735 TRACE_EVENT0("webrtc", "WebRtcVideoChannel2::SetRecvParameters"); | |
| 736 LOG(LS_INFO) << "SetRecvParameters: " << params.ToString(); | |
| 737 // TODO(pbos): Refactor this to only recreate the recv streams once | |
| 738 // instead of twice. | |
| 739 if (!SetRecvCodecs(params.codecs) || | |
| 740 !SetRecvRtpHeaderExtensions(params.extensions)) { | |
| 741 return false; | |
| 742 } | |
| 743 if (recv_params_.rtcp.reduced_size != params.rtcp.reduced_size) { | |
| 744 rtc::CritScope stream_lock(&stream_crit_); | |
| 745 for (auto& kv : receive_streams_) { | |
| 746 kv.second->SetRecvParameters(params); | |
| 747 } | |
| 748 } | |
| 749 recv_params_ = params; | |
| 750 return true; | |
| 751 } | |
| 752 | |
| 753 std::string WebRtcVideoChannel2::CodecSettingsVectorToString( | |
| 754 const std::vector<VideoCodecSettings>& codecs) { | |
| 755 std::stringstream out; | |
| 756 out << '{'; | |
| 757 for (size_t i = 0; i < codecs.size(); ++i) { | |
| 758 out << codecs[i].codec.ToString(); | |
| 759 if (i != codecs.size() - 1) { | |
| 760 out << ", "; | |
| 761 } | |
| 762 } | |
| 763 out << '}'; | |
| 764 return out.str(); | |
| 765 } | |
| 766 | |
| 767 bool WebRtcVideoChannel2::SetRecvCodecs(const std::vector<VideoCodec>& codecs) { | |
| 768 TRACE_EVENT0("webrtc", "WebRtcVideoChannel2::SetRecvCodecs"); | |
| 769 LOG(LS_INFO) << "SetRecvCodecs: " << CodecVectorToString(codecs); | |
| 770 if (!ValidateCodecFormats(codecs)) { | |
| 771 return false; | |
| 772 } | |
| 773 | |
| 774 const std::vector<VideoCodecSettings> mapped_codecs = MapCodecs(codecs); | |
| 775 if (mapped_codecs.empty()) { | |
| 776 LOG(LS_ERROR) << "SetRecvCodecs called without any video codecs."; | |
| 777 return false; | |
| 778 } | |
| 779 | |
| 780 std::vector<VideoCodecSettings> supported_codecs = | |
| 781 FilterSupportedCodecs(mapped_codecs); | |
| 782 | |
| 783 if (mapped_codecs.size() != supported_codecs.size()) { | |
| 784 LOG(LS_ERROR) << "SetRecvCodecs called with unsupported video codecs."; | |
| 785 return false; | |
| 786 } | |
| 787 | |
| 788 // Prevent reconfiguration when setting identical receive codecs. | |
| 789 if (!ReceiveCodecsHaveChanged(recv_codecs_, supported_codecs)) { | |
| 790 LOG(LS_INFO) | |
| 791 << "Ignoring call to SetRecvCodecs because codecs haven't changed."; | |
| 792 return true; | |
| 793 } | |
| 794 | |
| 795 LOG(LS_INFO) << "Changing recv codecs from " | |
| 796 << CodecSettingsVectorToString(recv_codecs_) << " to " | |
| 797 << CodecSettingsVectorToString(supported_codecs); | |
| 798 recv_codecs_ = supported_codecs; | |
| 799 | |
| 800 rtc::CritScope stream_lock(&stream_crit_); | |
| 801 for (std::map<uint32_t, WebRtcVideoReceiveStream*>::iterator it = | |
| 802 receive_streams_.begin(); | |
| 803 it != receive_streams_.end(); ++it) { | |
| 804 it->second->SetRecvCodecs(recv_codecs_); | |
| 805 } | |
| 806 | |
| 807 return true; | |
| 808 } | |
| 809 | |
| 810 bool WebRtcVideoChannel2::SetSendCodecs(const std::vector<VideoCodec>& codecs) { | |
| 811 TRACE_EVENT0("webrtc", "WebRtcVideoChannel2::SetSendCodecs"); | |
| 812 LOG(LS_INFO) << "SetSendCodecs: " << CodecVectorToString(codecs); | |
| 813 if (!ValidateCodecFormats(codecs)) { | |
| 814 return false; | |
| 815 } | |
| 816 | |
| 817 const std::vector<VideoCodecSettings> supported_codecs = | |
| 818 FilterSupportedCodecs(MapCodecs(codecs)); | |
| 819 | |
| 820 if (supported_codecs.empty()) { | |
| 821 LOG(LS_ERROR) << "No video codecs supported."; | |
| 822 return false; | |
| 823 } | |
| 824 | |
| 825 LOG(LS_INFO) << "Using codec: " << supported_codecs.front().codec.ToString(); | |
| 826 | |
| 827 if (send_codec_ && supported_codecs.front() == *send_codec_) { | |
| 828 LOG(LS_INFO) << "Ignore call to SetSendCodecs because first supported " | |
| 829 "codec hasn't changed."; | |
| 830 // Using same codec, avoid reconfiguring. | |
| 831 return true; | |
| 832 } | |
| 833 | |
| 834 send_codec_ = rtc::Optional<WebRtcVideoChannel2::VideoCodecSettings>( | |
| 835 supported_codecs.front()); | |
| 836 | |
| 837 rtc::CritScope stream_lock(&stream_crit_); | |
| 838 LOG(LS_INFO) << "Change the send codec because SetSendCodecs has a different " | |
| 839 "first supported codec."; | |
| 840 for (auto& kv : send_streams_) { | |
| 841 RTC_DCHECK(kv.second != nullptr); | |
| 842 kv.second->SetCodec(supported_codecs.front()); | |
| 843 } | |
| 844 LOG(LS_INFO) | |
| 845 << "SetFeedbackOptions on all the receive streams because the send " | |
| 846 "codec has changed."; | |
| 847 for (auto& kv : receive_streams_) { | |
| 848 RTC_DCHECK(kv.second != nullptr); | |
| 849 kv.second->SetFeedbackParameters( | |
| 850 HasNack(supported_codecs.front().codec), | |
| 851 HasRemb(supported_codecs.front().codec), | |
| 852 HasTransportCc(supported_codecs.front().codec)); | |
| 853 } | |
| 854 | |
| 855 // TODO(holmer): Changing the codec parameters shouldn't necessarily mean that | |
| 856 // we change the min/max of bandwidth estimation. Reevaluate this. | |
| 857 VideoCodec codec = supported_codecs.front().codec; | |
| 858 int bitrate_kbps; | |
| 859 if (codec.GetParam(kCodecParamMinBitrate, &bitrate_kbps) && | |
| 860 bitrate_kbps > 0) { | |
| 861 bitrate_config_.min_bitrate_bps = bitrate_kbps * 1000; | |
| 862 } else { | |
| 863 bitrate_config_.min_bitrate_bps = 0; | |
| 864 } | |
| 865 if (codec.GetParam(kCodecParamStartBitrate, &bitrate_kbps) && | |
| 866 bitrate_kbps > 0) { | |
| 867 bitrate_config_.start_bitrate_bps = bitrate_kbps * 1000; | |
| 868 } else { | |
| 869 // Do not reconfigure start bitrate unless it's specified and positive. | |
| 870 bitrate_config_.start_bitrate_bps = -1; | |
| 871 } | |
| 872 if (codec.GetParam(kCodecParamMaxBitrate, &bitrate_kbps) && | |
| 873 bitrate_kbps > 0) { | |
| 874 bitrate_config_.max_bitrate_bps = bitrate_kbps * 1000; | |
| 875 } else { | |
| 876 bitrate_config_.max_bitrate_bps = -1; | |
| 877 } | |
| 878 call_->SetBitrateConfig(bitrate_config_); | |
| 879 | |
| 880 return true; | |
| 881 } | |
| 882 | |
| 883 bool WebRtcVideoChannel2::GetSendCodec(VideoCodec* codec) { | |
| 884 if (!send_codec_) { | |
| 885 LOG(LS_VERBOSE) << "GetSendCodec: No send codec set."; | |
| 886 return false; | |
| 887 } | |
| 888 *codec = send_codec_->codec; | |
| 889 return true; | |
| 890 } | |
| 891 | |
| 892 bool WebRtcVideoChannel2::SetSendStreamFormat(uint32_t ssrc, | |
| 893 const VideoFormat& format) { | |
| 894 LOG(LS_VERBOSE) << "SetSendStreamFormat:" << ssrc << " -> " | |
| 895 << format.ToString(); | |
| 896 rtc::CritScope stream_lock(&stream_crit_); | |
| 897 if (send_streams_.find(ssrc) == send_streams_.end()) { | |
| 898 return false; | |
| 899 } | |
| 900 return send_streams_[ssrc]->SetVideoFormat(format); | |
| 901 } | |
| 902 | |
| 903 bool WebRtcVideoChannel2::SetSend(bool send) { | |
| 904 LOG(LS_VERBOSE) << "SetSend: " << (send ? "true" : "false"); | |
| 905 if (send && !send_codec_) { | |
| 906 LOG(LS_ERROR) << "SetSend(true) called before setting codec."; | |
| 907 return false; | |
| 908 } | |
| 909 if (send) { | |
| 910 StartAllSendStreams(); | |
| 911 } else { | |
| 912 StopAllSendStreams(); | |
| 913 } | |
| 914 sending_ = send; | |
| 915 return true; | |
| 916 } | |
| 917 | |
| 918 bool WebRtcVideoChannel2::SetVideoSend(uint32_t ssrc, bool enable, | |
| 919 const VideoOptions* options) { | |
| 920 // TODO(solenberg): The state change should be fully rolled back if any one of | |
| 921 // these calls fail. | |
| 922 if (!MuteStream(ssrc, !enable)) { | |
| 923 return false; | |
| 924 } | |
| 925 if (enable && options) { | |
| 926 return SetOptions(*options); | |
| 927 } else { | |
| 928 return true; | |
| 929 } | |
| 930 } | |
| 931 | |
| 932 bool WebRtcVideoChannel2::ValidateSendSsrcAvailability( | |
| 933 const StreamParams& sp) const { | |
| 934 for (uint32_t ssrc: sp.ssrcs) { | |
| 935 if (send_ssrcs_.find(ssrc) != send_ssrcs_.end()) { | |
| 936 LOG(LS_ERROR) << "Send stream with SSRC '" << ssrc << "' already exists."; | |
| 937 return false; | |
| 938 } | |
| 939 } | |
| 940 return true; | |
| 941 } | |
| 942 | |
| 943 bool WebRtcVideoChannel2::ValidateReceiveSsrcAvailability( | |
| 944 const StreamParams& sp) const { | |
| 945 for (uint32_t ssrc: sp.ssrcs) { | |
| 946 if (receive_ssrcs_.find(ssrc) != receive_ssrcs_.end()) { | |
| 947 LOG(LS_ERROR) << "Receive stream with SSRC '" << ssrc | |
| 948 << "' already exists."; | |
| 949 return false; | |
| 950 } | |
| 951 } | |
| 952 return true; | |
| 953 } | |
| 954 | |
| 955 bool WebRtcVideoChannel2::AddSendStream(const StreamParams& sp) { | |
| 956 LOG(LS_INFO) << "AddSendStream: " << sp.ToString(); | |
| 957 if (!ValidateStreamParams(sp)) | |
| 958 return false; | |
| 959 | |
| 960 rtc::CritScope stream_lock(&stream_crit_); | |
| 961 | |
| 962 if (!ValidateSendSsrcAvailability(sp)) | |
| 963 return false; | |
| 964 | |
| 965 for (uint32_t used_ssrc : sp.ssrcs) | |
| 966 send_ssrcs_.insert(used_ssrc); | |
| 967 | |
| 968 webrtc::VideoSendStream::Config config(this); | |
| 969 config.overuse_callback = this; | |
| 970 | |
| 971 WebRtcVideoSendStream* stream = new WebRtcVideoSendStream( | |
| 972 call_, sp, config, external_encoder_factory_, options_, | |
| 973 bitrate_config_.max_bitrate_bps, send_codec_, send_rtp_extensions_, | |
| 974 send_params_); | |
| 975 | |
| 976 uint32_t ssrc = sp.first_ssrc(); | |
| 977 RTC_DCHECK(ssrc != 0); | |
| 978 send_streams_[ssrc] = stream; | |
| 979 | |
| 980 if (rtcp_receiver_report_ssrc_ == kDefaultRtcpReceiverReportSsrc) { | |
| 981 rtcp_receiver_report_ssrc_ = ssrc; | |
| 982 LOG(LS_INFO) << "SetLocalSsrc on all the receive streams because we added " | |
| 983 "a send stream."; | |
| 984 for (auto& kv : receive_streams_) | |
| 985 kv.second->SetLocalSsrc(ssrc); | |
| 986 } | |
| 987 if (default_send_ssrc_ == 0) { | |
| 988 default_send_ssrc_ = ssrc; | |
| 989 } | |
| 990 if (sending_) { | |
| 991 stream->Start(); | |
| 992 } | |
| 993 | |
| 994 return true; | |
| 995 } | |
| 996 | |
| 997 bool WebRtcVideoChannel2::RemoveSendStream(uint32_t ssrc) { | |
| 998 LOG(LS_INFO) << "RemoveSendStream: " << ssrc; | |
| 999 | |
| 1000 if (ssrc == 0) { | |
| 1001 if (default_send_ssrc_ == 0) { | |
| 1002 LOG(LS_ERROR) << "No default send stream active."; | |
| 1003 return false; | |
| 1004 } | |
| 1005 | |
| 1006 LOG(LS_VERBOSE) << "Removing default stream: " << default_send_ssrc_; | |
| 1007 ssrc = default_send_ssrc_; | |
| 1008 } | |
| 1009 | |
| 1010 WebRtcVideoSendStream* removed_stream; | |
| 1011 { | |
| 1012 rtc::CritScope stream_lock(&stream_crit_); | |
| 1013 std::map<uint32_t, WebRtcVideoSendStream*>::iterator it = | |
| 1014 send_streams_.find(ssrc); | |
| 1015 if (it == send_streams_.end()) { | |
| 1016 return false; | |
| 1017 } | |
| 1018 | |
| 1019 for (uint32_t old_ssrc : it->second->GetSsrcs()) | |
| 1020 send_ssrcs_.erase(old_ssrc); | |
| 1021 | |
| 1022 removed_stream = it->second; | |
| 1023 send_streams_.erase(it); | |
| 1024 | |
| 1025 // Switch receiver report SSRCs, the one in use is no longer valid. | |
| 1026 if (rtcp_receiver_report_ssrc_ == ssrc) { | |
| 1027 rtcp_receiver_report_ssrc_ = send_streams_.empty() | |
| 1028 ? kDefaultRtcpReceiverReportSsrc | |
| 1029 : send_streams_.begin()->first; | |
| 1030 LOG(LS_INFO) << "SetLocalSsrc on all the receive streams because the " | |
| 1031 "previous local SSRC was removed."; | |
| 1032 | |
| 1033 for (auto& kv : receive_streams_) { | |
| 1034 kv.second->SetLocalSsrc(rtcp_receiver_report_ssrc_); | |
| 1035 } | |
| 1036 } | |
| 1037 } | |
| 1038 | |
| 1039 delete removed_stream; | |
| 1040 | |
| 1041 if (ssrc == default_send_ssrc_) { | |
| 1042 default_send_ssrc_ = 0; | |
| 1043 } | |
| 1044 | |
| 1045 return true; | |
| 1046 } | |
| 1047 | |
| 1048 void WebRtcVideoChannel2::DeleteReceiveStream( | |
| 1049 WebRtcVideoChannel2::WebRtcVideoReceiveStream* stream) { | |
| 1050 for (uint32_t old_ssrc : stream->GetSsrcs()) | |
| 1051 receive_ssrcs_.erase(old_ssrc); | |
| 1052 delete stream; | |
| 1053 } | |
| 1054 | |
| 1055 bool WebRtcVideoChannel2::AddRecvStream(const StreamParams& sp) { | |
| 1056 return AddRecvStream(sp, false); | |
| 1057 } | |
| 1058 | |
| 1059 bool WebRtcVideoChannel2::AddRecvStream(const StreamParams& sp, | |
| 1060 bool default_stream) { | |
| 1061 RTC_DCHECK(thread_checker_.CalledOnValidThread()); | |
| 1062 | |
| 1063 LOG(LS_INFO) << "AddRecvStream" << (default_stream ? " (default stream)" : "") | |
| 1064 << ": " << sp.ToString(); | |
| 1065 if (!ValidateStreamParams(sp)) | |
| 1066 return false; | |
| 1067 | |
| 1068 uint32_t ssrc = sp.first_ssrc(); | |
| 1069 RTC_DCHECK(ssrc != 0); // TODO(pbos): Is this ever valid? | |
| 1070 | |
| 1071 rtc::CritScope stream_lock(&stream_crit_); | |
| 1072 // Remove running stream if this was a default stream. | |
| 1073 auto prev_stream = receive_streams_.find(ssrc); | |
| 1074 if (prev_stream != receive_streams_.end()) { | |
| 1075 if (default_stream || !prev_stream->second->IsDefaultStream()) { | |
| 1076 LOG(LS_ERROR) << "Receive stream for SSRC '" << ssrc | |
| 1077 << "' already exists."; | |
| 1078 return false; | |
| 1079 } | |
| 1080 DeleteReceiveStream(prev_stream->second); | |
| 1081 receive_streams_.erase(prev_stream); | |
| 1082 } | |
| 1083 | |
| 1084 if (!ValidateReceiveSsrcAvailability(sp)) | |
| 1085 return false; | |
| 1086 | |
| 1087 for (uint32_t used_ssrc : sp.ssrcs) | |
| 1088 receive_ssrcs_.insert(used_ssrc); | |
| 1089 | |
| 1090 webrtc::VideoReceiveStream::Config config(this); | |
| 1091 ConfigureReceiverRtp(&config, sp); | |
| 1092 | |
| 1093 // Set up A/V sync group based on sync label. | |
| 1094 config.sync_group = sp.sync_label; | |
| 1095 | |
| 1096 config.rtp.remb = send_codec_ ? HasRemb(send_codec_->codec) : false; | |
| 1097 config.rtp.transport_cc = | |
| 1098 send_codec_ ? HasTransportCc(send_codec_->codec) : false; | |
| 1099 | |
| 1100 receive_streams_[ssrc] = new WebRtcVideoReceiveStream( | |
| 1101 call_, sp, config, external_decoder_factory_, default_stream, | |
| 1102 recv_codecs_, options_.disable_prerenderer_smoothing.value_or(false)); | |
| 1103 | |
| 1104 return true; | |
| 1105 } | |
| 1106 | |
| 1107 void WebRtcVideoChannel2::ConfigureReceiverRtp( | |
| 1108 webrtc::VideoReceiveStream::Config* config, | |
| 1109 const StreamParams& sp) const { | |
| 1110 uint32_t ssrc = sp.first_ssrc(); | |
| 1111 | |
| 1112 config->rtp.remote_ssrc = ssrc; | |
| 1113 config->rtp.local_ssrc = rtcp_receiver_report_ssrc_; | |
| 1114 | |
| 1115 config->rtp.extensions = recv_rtp_extensions_; | |
| 1116 config->rtp.rtcp_mode = recv_params_.rtcp.reduced_size | |
| 1117 ? webrtc::RtcpMode::kReducedSize | |
| 1118 : webrtc::RtcpMode::kCompound; | |
| 1119 | |
| 1120 // TODO(pbos): This protection is against setting the same local ssrc as | |
| 1121 // remote which is not permitted by the lower-level API. RTCP requires a | |
| 1122 // corresponding sender SSRC. Figure out what to do when we don't have | |
| 1123 // (receive-only) or know a good local SSRC. | |
| 1124 if (config->rtp.remote_ssrc == config->rtp.local_ssrc) { | |
| 1125 if (config->rtp.local_ssrc != kDefaultRtcpReceiverReportSsrc) { | |
| 1126 config->rtp.local_ssrc = kDefaultRtcpReceiverReportSsrc; | |
| 1127 } else { | |
| 1128 config->rtp.local_ssrc = kDefaultRtcpReceiverReportSsrc + 1; | |
| 1129 } | |
| 1130 } | |
| 1131 | |
| 1132 for (size_t i = 0; i < recv_codecs_.size(); ++i) { | |
| 1133 MergeFecConfig(recv_codecs_[i].fec, &config->rtp.fec); | |
| 1134 } | |
| 1135 | |
| 1136 for (size_t i = 0; i < recv_codecs_.size(); ++i) { | |
| 1137 uint32_t rtx_ssrc; | |
| 1138 if (recv_codecs_[i].rtx_payload_type != -1 && | |
| 1139 sp.GetFidSsrc(ssrc, &rtx_ssrc)) { | |
| 1140 webrtc::VideoReceiveStream::Config::Rtp::Rtx& rtx = | |
| 1141 config->rtp.rtx[recv_codecs_[i].codec.id]; | |
| 1142 rtx.ssrc = rtx_ssrc; | |
| 1143 rtx.payload_type = recv_codecs_[i].rtx_payload_type; | |
| 1144 } | |
| 1145 } | |
| 1146 } | |
| 1147 | |
| 1148 bool WebRtcVideoChannel2::RemoveRecvStream(uint32_t ssrc) { | |
| 1149 LOG(LS_INFO) << "RemoveRecvStream: " << ssrc; | |
| 1150 if (ssrc == 0) { | |
| 1151 LOG(LS_ERROR) << "RemoveRecvStream with 0 ssrc is not supported."; | |
| 1152 return false; | |
| 1153 } | |
| 1154 | |
| 1155 rtc::CritScope stream_lock(&stream_crit_); | |
| 1156 std::map<uint32_t, WebRtcVideoReceiveStream*>::iterator stream = | |
| 1157 receive_streams_.find(ssrc); | |
| 1158 if (stream == receive_streams_.end()) { | |
| 1159 LOG(LS_ERROR) << "Stream not found for ssrc: " << ssrc; | |
| 1160 return false; | |
| 1161 } | |
| 1162 DeleteReceiveStream(stream->second); | |
| 1163 receive_streams_.erase(stream); | |
| 1164 | |
| 1165 return true; | |
| 1166 } | |
| 1167 | |
| 1168 bool WebRtcVideoChannel2::SetRenderer(uint32_t ssrc, VideoRenderer* renderer) { | |
| 1169 LOG(LS_INFO) << "SetRenderer: ssrc:" << ssrc << " " | |
| 1170 << (renderer ? "(ptr)" : "NULL"); | |
| 1171 if (ssrc == 0) { | |
| 1172 default_unsignalled_ssrc_handler_.SetDefaultRenderer(this, renderer); | |
| 1173 return true; | |
| 1174 } | |
| 1175 | |
| 1176 rtc::CritScope stream_lock(&stream_crit_); | |
| 1177 std::map<uint32_t, WebRtcVideoReceiveStream*>::iterator it = | |
| 1178 receive_streams_.find(ssrc); | |
| 1179 if (it == receive_streams_.end()) { | |
| 1180 return false; | |
| 1181 } | |
| 1182 | |
| 1183 it->second->SetRenderer(renderer); | |
| 1184 return true; | |
| 1185 } | |
| 1186 | |
| 1187 bool WebRtcVideoChannel2::GetStats(VideoMediaInfo* info) { | |
| 1188 info->Clear(); | |
| 1189 FillSenderStats(info); | |
| 1190 FillReceiverStats(info); | |
| 1191 webrtc::Call::Stats stats = call_->GetStats(); | |
| 1192 FillBandwidthEstimationStats(stats, info); | |
| 1193 if (stats.rtt_ms != -1) { | |
| 1194 for (size_t i = 0; i < info->senders.size(); ++i) { | |
| 1195 info->senders[i].rtt_ms = stats.rtt_ms; | |
| 1196 } | |
| 1197 } | |
| 1198 return true; | |
| 1199 } | |
| 1200 | |
| 1201 void WebRtcVideoChannel2::FillSenderStats(VideoMediaInfo* video_media_info) { | |
| 1202 rtc::CritScope stream_lock(&stream_crit_); | |
| 1203 for (std::map<uint32_t, WebRtcVideoSendStream*>::iterator it = | |
| 1204 send_streams_.begin(); | |
| 1205 it != send_streams_.end(); ++it) { | |
| 1206 video_media_info->senders.push_back(it->second->GetVideoSenderInfo()); | |
| 1207 } | |
| 1208 } | |
| 1209 | |
| 1210 void WebRtcVideoChannel2::FillReceiverStats(VideoMediaInfo* video_media_info) { | |
| 1211 rtc::CritScope stream_lock(&stream_crit_); | |
| 1212 for (std::map<uint32_t, WebRtcVideoReceiveStream*>::iterator it = | |
| 1213 receive_streams_.begin(); | |
| 1214 it != receive_streams_.end(); ++it) { | |
| 1215 video_media_info->receivers.push_back(it->second->GetVideoReceiverInfo()); | |
| 1216 } | |
| 1217 } | |
| 1218 | |
| 1219 void WebRtcVideoChannel2::FillBandwidthEstimationStats( | |
| 1220 const webrtc::Call::Stats& stats, | |
| 1221 VideoMediaInfo* video_media_info) { | |
| 1222 BandwidthEstimationInfo bwe_info; | |
| 1223 bwe_info.available_send_bandwidth = stats.send_bandwidth_bps; | |
| 1224 bwe_info.available_recv_bandwidth = stats.recv_bandwidth_bps; | |
| 1225 bwe_info.bucket_delay = stats.pacer_delay_ms; | |
| 1226 | |
| 1227 // Get send stream bitrate stats. | |
| 1228 rtc::CritScope stream_lock(&stream_crit_); | |
| 1229 for (std::map<uint32_t, WebRtcVideoSendStream*>::iterator stream = | |
| 1230 send_streams_.begin(); | |
| 1231 stream != send_streams_.end(); ++stream) { | |
| 1232 stream->second->FillBandwidthEstimationInfo(&bwe_info); | |
| 1233 } | |
| 1234 video_media_info->bw_estimations.push_back(bwe_info); | |
| 1235 } | |
| 1236 | |
| 1237 bool WebRtcVideoChannel2::SetCapturer(uint32_t ssrc, VideoCapturer* capturer) { | |
| 1238 LOG(LS_INFO) << "SetCapturer: " << ssrc << " -> " | |
| 1239 << (capturer != NULL ? "(capturer)" : "NULL"); | |
| 1240 RTC_DCHECK(ssrc != 0); | |
| 1241 { | |
| 1242 rtc::CritScope stream_lock(&stream_crit_); | |
| 1243 if (send_streams_.find(ssrc) == send_streams_.end()) { | |
| 1244 LOG(LS_ERROR) << "No sending stream on ssrc " << ssrc; | |
| 1245 return false; | |
| 1246 } | |
| 1247 if (!send_streams_[ssrc]->SetCapturer(capturer)) { | |
| 1248 return false; | |
| 1249 } | |
| 1250 } | |
| 1251 | |
| 1252 if (capturer) { | |
| 1253 capturer->SetApplyRotation( | |
| 1254 !FindHeaderExtension(send_rtp_extensions_, | |
| 1255 kRtpVideoRotationHeaderExtension)); | |
| 1256 } | |
| 1257 { | |
| 1258 rtc::CritScope lock(&capturer_crit_); | |
| 1259 capturers_[ssrc] = capturer; | |
| 1260 } | |
| 1261 return true; | |
| 1262 } | |
| 1263 | |
| 1264 bool WebRtcVideoChannel2::SendIntraFrame() { | |
| 1265 // TODO(pbos): Implement. | |
| 1266 LOG(LS_VERBOSE) << "SendIntraFrame()."; | |
| 1267 return true; | |
| 1268 } | |
| 1269 | |
| 1270 bool WebRtcVideoChannel2::RequestIntraFrame() { | |
| 1271 // TODO(pbos): Implement. | |
| 1272 LOG(LS_VERBOSE) << "SendIntraFrame()."; | |
| 1273 return true; | |
| 1274 } | |
| 1275 | |
| 1276 void WebRtcVideoChannel2::OnPacketReceived( | |
| 1277 rtc::Buffer* packet, | |
| 1278 const rtc::PacketTime& packet_time) { | |
| 1279 const webrtc::PacketTime webrtc_packet_time(packet_time.timestamp, | |
| 1280 packet_time.not_before); | |
| 1281 const webrtc::PacketReceiver::DeliveryStatus delivery_result = | |
| 1282 call_->Receiver()->DeliverPacket( | |
| 1283 webrtc::MediaType::VIDEO, | |
| 1284 reinterpret_cast<const uint8_t*>(packet->data()), packet->size(), | |
| 1285 webrtc_packet_time); | |
| 1286 switch (delivery_result) { | |
| 1287 case webrtc::PacketReceiver::DELIVERY_OK: | |
| 1288 return; | |
| 1289 case webrtc::PacketReceiver::DELIVERY_PACKET_ERROR: | |
| 1290 return; | |
| 1291 case webrtc::PacketReceiver::DELIVERY_UNKNOWN_SSRC: | |
| 1292 break; | |
| 1293 } | |
| 1294 | |
| 1295 uint32_t ssrc = 0; | |
| 1296 if (!GetRtpSsrc(packet->data(), packet->size(), &ssrc)) { | |
| 1297 return; | |
| 1298 } | |
| 1299 | |
| 1300 int payload_type = 0; | |
| 1301 if (!GetRtpPayloadType(packet->data(), packet->size(), &payload_type)) { | |
| 1302 return; | |
| 1303 } | |
| 1304 | |
| 1305 // See if this payload_type is registered as one that usually gets its own | |
| 1306 // SSRC (RTX) or at least is safe to drop either way (ULPFEC). If it is, and | |
| 1307 // it wasn't handled above by DeliverPacket, that means we don't know what | |
| 1308 // stream it associates with, and we shouldn't ever create an implicit channel | |
| 1309 // for these. | |
| 1310 for (auto& codec : recv_codecs_) { | |
| 1311 if (payload_type == codec.rtx_payload_type || | |
| 1312 payload_type == codec.fec.red_rtx_payload_type || | |
| 1313 payload_type == codec.fec.ulpfec_payload_type) { | |
| 1314 return; | |
| 1315 } | |
| 1316 } | |
| 1317 | |
| 1318 switch (unsignalled_ssrc_handler_->OnUnsignalledSsrc(this, ssrc)) { | |
| 1319 case UnsignalledSsrcHandler::kDropPacket: | |
| 1320 return; | |
| 1321 case UnsignalledSsrcHandler::kDeliverPacket: | |
| 1322 break; | |
| 1323 } | |
| 1324 | |
| 1325 if (call_->Receiver()->DeliverPacket( | |
| 1326 webrtc::MediaType::VIDEO, | |
| 1327 reinterpret_cast<const uint8_t*>(packet->data()), packet->size(), | |
| 1328 webrtc_packet_time) != webrtc::PacketReceiver::DELIVERY_OK) { | |
| 1329 LOG(LS_WARNING) << "Failed to deliver RTP packet on re-delivery."; | |
| 1330 return; | |
| 1331 } | |
| 1332 } | |
| 1333 | |
| 1334 void WebRtcVideoChannel2::OnRtcpReceived( | |
| 1335 rtc::Buffer* packet, | |
| 1336 const rtc::PacketTime& packet_time) { | |
| 1337 const webrtc::PacketTime webrtc_packet_time(packet_time.timestamp, | |
| 1338 packet_time.not_before); | |
| 1339 // TODO(pbos): Check webrtc::PacketReceiver::DELIVERY_OK once we deliver | |
| 1340 // for both audio and video on the same path. Since BundleFilter doesn't | |
| 1341 // filter RTCP anymore incoming RTCP packets could've been going to audio (so | |
| 1342 // logging failures spam the log). | |
| 1343 call_->Receiver()->DeliverPacket( | |
| 1344 webrtc::MediaType::VIDEO, | |
| 1345 reinterpret_cast<const uint8_t*>(packet->data()), packet->size(), | |
| 1346 webrtc_packet_time); | |
| 1347 } | |
| 1348 | |
| 1349 void WebRtcVideoChannel2::OnReadyToSend(bool ready) { | |
| 1350 LOG(LS_VERBOSE) << "OnReadyToSend: " << (ready ? "Ready." : "Not ready."); | |
| 1351 call_->SignalNetworkState(ready ? webrtc::kNetworkUp : webrtc::kNetworkDown); | |
| 1352 } | |
| 1353 | |
| 1354 bool WebRtcVideoChannel2::MuteStream(uint32_t ssrc, bool mute) { | |
| 1355 LOG(LS_VERBOSE) << "MuteStream: " << ssrc << " -> " | |
| 1356 << (mute ? "mute" : "unmute"); | |
| 1357 RTC_DCHECK(ssrc != 0); | |
| 1358 rtc::CritScope stream_lock(&stream_crit_); | |
| 1359 if (send_streams_.find(ssrc) == send_streams_.end()) { | |
| 1360 LOG(LS_ERROR) << "No sending stream on ssrc " << ssrc; | |
| 1361 return false; | |
| 1362 } | |
| 1363 | |
| 1364 send_streams_[ssrc]->MuteStream(mute); | |
| 1365 return true; | |
| 1366 } | |
| 1367 | |
| 1368 bool WebRtcVideoChannel2::SetRecvRtpHeaderExtensions( | |
| 1369 const std::vector<RtpHeaderExtension>& extensions) { | |
| 1370 TRACE_EVENT0("webrtc", "WebRtcVideoChannel2::SetRecvRtpHeaderExtensions"); | |
| 1371 if (!ValidateRtpExtensions(extensions)) { | |
| 1372 return false; | |
| 1373 } | |
| 1374 std::vector<webrtc::RtpExtension> filtered_extensions = FilterRtpExtensions( | |
| 1375 extensions, webrtc::RtpExtension::IsSupportedForVideo, false); | |
| 1376 if (recv_rtp_extensions_ == filtered_extensions) { | |
| 1377 LOG(LS_INFO) << "Ignoring call to SetRecvRtpHeaderExtensions because " | |
| 1378 "header extensions haven't changed."; | |
| 1379 return true; | |
| 1380 } | |
| 1381 recv_rtp_extensions_.swap(filtered_extensions); | |
| 1382 | |
| 1383 rtc::CritScope stream_lock(&stream_crit_); | |
| 1384 for (std::map<uint32_t, WebRtcVideoReceiveStream*>::iterator it = | |
| 1385 receive_streams_.begin(); | |
| 1386 it != receive_streams_.end(); ++it) { | |
| 1387 it->second->SetRtpExtensions(recv_rtp_extensions_); | |
| 1388 } | |
| 1389 return true; | |
| 1390 } | |
| 1391 | |
| 1392 bool WebRtcVideoChannel2::SetSendRtpHeaderExtensions( | |
| 1393 const std::vector<RtpHeaderExtension>& extensions) { | |
| 1394 TRACE_EVENT0("webrtc", "WebRtcVideoChannel2::SetSendRtpHeaderExtensions"); | |
| 1395 if (!ValidateRtpExtensions(extensions)) { | |
| 1396 return false; | |
| 1397 } | |
| 1398 std::vector<webrtc::RtpExtension> filtered_extensions = FilterRtpExtensions( | |
| 1399 extensions, webrtc::RtpExtension::IsSupportedForVideo, true); | |
| 1400 if (send_rtp_extensions_ == filtered_extensions) { | |
| 1401 LOG(LS_INFO) << "Ignoring call to SetRecvRtpHeaderExtensions because " | |
| 1402 "header extensions haven't changed."; | |
| 1403 return true; | |
| 1404 } | |
| 1405 send_rtp_extensions_.swap(filtered_extensions); | |
| 1406 | |
| 1407 const webrtc::RtpExtension* cvo_extension = FindHeaderExtension( | |
| 1408 send_rtp_extensions_, kRtpVideoRotationHeaderExtension); | |
| 1409 | |
| 1410 rtc::CritScope stream_lock(&stream_crit_); | |
| 1411 for (std::map<uint32_t, WebRtcVideoSendStream*>::iterator it = | |
| 1412 send_streams_.begin(); | |
| 1413 it != send_streams_.end(); ++it) { | |
| 1414 it->second->SetRtpExtensions(send_rtp_extensions_); | |
| 1415 it->second->SetApplyRotation(!cvo_extension); | |
| 1416 } | |
| 1417 return true; | |
| 1418 } | |
| 1419 | |
| 1420 // Counter-intuitively this method doesn't only set global bitrate caps but also | |
| 1421 // per-stream codec max bitrates. This is to permit SetMaxSendBitrate (b=AS) to | |
| 1422 // raise bitrates above the 2000k default bitrate cap. | |
| 1423 bool WebRtcVideoChannel2::SetMaxSendBandwidth(int max_bitrate_bps) { | |
| 1424 // TODO(pbos): Figure out whether b=AS means max bitrate for this | |
| 1425 // WebRtcVideoChannel2 (in which case we're good), or per sender (SSRC), in | |
| 1426 // which case this should not set a Call::BitrateConfig but rather reconfigure | |
| 1427 // all senders. | |
| 1428 LOG(LS_INFO) << "SetMaxSendBandwidth: " << max_bitrate_bps << "bps."; | |
| 1429 if (max_bitrate_bps == bitrate_config_.max_bitrate_bps) | |
| 1430 return true; | |
| 1431 | |
| 1432 if (max_bitrate_bps < 0) { | |
| 1433 // Option not set. | |
| 1434 return true; | |
| 1435 } | |
| 1436 if (max_bitrate_bps == 0) { | |
| 1437 // Unsetting max bitrate. | |
| 1438 max_bitrate_bps = -1; | |
| 1439 } | |
| 1440 bitrate_config_.start_bitrate_bps = -1; | |
| 1441 bitrate_config_.max_bitrate_bps = max_bitrate_bps; | |
| 1442 if (max_bitrate_bps > 0 && | |
| 1443 bitrate_config_.min_bitrate_bps > max_bitrate_bps) { | |
| 1444 bitrate_config_.min_bitrate_bps = max_bitrate_bps; | |
| 1445 } | |
| 1446 call_->SetBitrateConfig(bitrate_config_); | |
| 1447 rtc::CritScope stream_lock(&stream_crit_); | |
| 1448 for (auto& kv : send_streams_) | |
| 1449 kv.second->SetMaxBitrateBps(max_bitrate_bps); | |
| 1450 return true; | |
| 1451 } | |
| 1452 | |
| 1453 bool WebRtcVideoChannel2::SetOptions(const VideoOptions& options) { | |
| 1454 TRACE_EVENT0("webrtc", "WebRtcVideoChannel2::SetOptions"); | |
| 1455 LOG(LS_INFO) << "SetOptions: " << options.ToString(); | |
| 1456 VideoOptions old_options = options_; | |
| 1457 options_.SetAll(options); | |
| 1458 if (options_ == old_options) { | |
| 1459 // No new options to set. | |
| 1460 return true; | |
| 1461 } | |
| 1462 { | |
| 1463 rtc::CritScope lock(&capturer_crit_); | |
| 1464 if (options_.cpu_overuse_detection) | |
| 1465 signal_cpu_adaptation_ = *options_.cpu_overuse_detection; | |
| 1466 } | |
| 1467 rtc::DiffServCodePoint dscp = | |
| 1468 options_.dscp.value_or(false) ? rtc::DSCP_AF41 : rtc::DSCP_DEFAULT; | |
| 1469 MediaChannel::SetDscp(dscp); | |
| 1470 rtc::CritScope stream_lock(&stream_crit_); | |
| 1471 for (std::map<uint32_t, WebRtcVideoSendStream*>::iterator it = | |
| 1472 send_streams_.begin(); | |
| 1473 it != send_streams_.end(); ++it) { | |
| 1474 it->second->SetOptions(options_); | |
| 1475 } | |
| 1476 return true; | |
| 1477 } | |
| 1478 | |
| 1479 void WebRtcVideoChannel2::SetInterface(NetworkInterface* iface) { | |
| 1480 MediaChannel::SetInterface(iface); | |
| 1481 // Set the RTP recv/send buffer to a bigger size | |
| 1482 MediaChannel::SetOption(NetworkInterface::ST_RTP, | |
| 1483 rtc::Socket::OPT_RCVBUF, | |
| 1484 kVideoRtpBufferSize); | |
| 1485 | |
| 1486 // Speculative change to increase the outbound socket buffer size. | |
| 1487 // In b/15152257, we are seeing a significant number of packets discarded | |
| 1488 // due to lack of socket buffer space, although it's not yet clear what the | |
| 1489 // ideal value should be. | |
| 1490 MediaChannel::SetOption(NetworkInterface::ST_RTP, | |
| 1491 rtc::Socket::OPT_SNDBUF, | |
| 1492 kVideoRtpBufferSize); | |
| 1493 } | |
| 1494 | |
| 1495 void WebRtcVideoChannel2::UpdateAspectRatio(int ratio_w, int ratio_h) { | |
| 1496 // TODO(pbos): Implement. | |
| 1497 } | |
| 1498 | |
| 1499 void WebRtcVideoChannel2::OnMessage(rtc::Message* msg) { | |
| 1500 // Ignored. | |
| 1501 } | |
| 1502 | |
| 1503 void WebRtcVideoChannel2::OnLoadUpdate(Load load) { | |
| 1504 // OnLoadUpdate can not take any locks that are held while creating streams | |
| 1505 // etc. Doing so establishes lock-order inversions between the webrtc process | |
| 1506 // thread on stream creation and locks such as stream_crit_ while calling out. | |
| 1507 rtc::CritScope stream_lock(&capturer_crit_); | |
| 1508 if (!signal_cpu_adaptation_) | |
| 1509 return; | |
| 1510 // Do not adapt resolution for screen content as this will likely result in | |
| 1511 // blurry and unreadable text. | |
| 1512 for (auto& kv : capturers_) { | |
| 1513 if (kv.second != nullptr | |
| 1514 && !kv.second->IsScreencast() | |
| 1515 && kv.second->video_adapter() != nullptr) { | |
| 1516 kv.second->video_adapter()->OnCpuResolutionRequest( | |
| 1517 load == kOveruse ? CoordinatedVideoAdapter::DOWNGRADE | |
| 1518 : CoordinatedVideoAdapter::UPGRADE); | |
| 1519 } | |
| 1520 } | |
| 1521 } | |
| 1522 | |
| 1523 bool WebRtcVideoChannel2::SendRtp(const uint8_t* data, | |
| 1524 size_t len, | |
| 1525 const webrtc::PacketOptions& options) { | |
| 1526 rtc::Buffer packet(data, len, kMaxRtpPacketLen); | |
| 1527 rtc::PacketOptions rtc_options; | |
| 1528 rtc_options.packet_id = options.packet_id; | |
| 1529 return MediaChannel::SendPacket(&packet, rtc_options); | |
| 1530 } | |
| 1531 | |
| 1532 bool WebRtcVideoChannel2::SendRtcp(const uint8_t* data, size_t len) { | |
| 1533 rtc::Buffer packet(data, len, kMaxRtpPacketLen); | |
| 1534 return MediaChannel::SendRtcp(&packet, rtc::PacketOptions()); | |
| 1535 } | |
| 1536 | |
| 1537 void WebRtcVideoChannel2::StartAllSendStreams() { | |
| 1538 rtc::CritScope stream_lock(&stream_crit_); | |
| 1539 for (std::map<uint32_t, WebRtcVideoSendStream*>::iterator it = | |
| 1540 send_streams_.begin(); | |
| 1541 it != send_streams_.end(); ++it) { | |
| 1542 it->second->Start(); | |
| 1543 } | |
| 1544 } | |
| 1545 | |
| 1546 void WebRtcVideoChannel2::StopAllSendStreams() { | |
| 1547 rtc::CritScope stream_lock(&stream_crit_); | |
| 1548 for (std::map<uint32_t, WebRtcVideoSendStream*>::iterator it = | |
| 1549 send_streams_.begin(); | |
| 1550 it != send_streams_.end(); ++it) { | |
| 1551 it->second->Stop(); | |
| 1552 } | |
| 1553 } | |
| 1554 | |
| 1555 WebRtcVideoChannel2::WebRtcVideoSendStream::VideoSendStreamParameters:: | |
| 1556 VideoSendStreamParameters( | |
| 1557 const webrtc::VideoSendStream::Config& config, | |
| 1558 const VideoOptions& options, | |
| 1559 int max_bitrate_bps, | |
| 1560 const rtc::Optional<VideoCodecSettings>& codec_settings) | |
| 1561 : config(config), | |
| 1562 options(options), | |
| 1563 max_bitrate_bps(max_bitrate_bps), | |
| 1564 codec_settings(codec_settings) {} | |
| 1565 | |
| 1566 WebRtcVideoChannel2::WebRtcVideoSendStream::AllocatedEncoder::AllocatedEncoder( | |
| 1567 webrtc::VideoEncoder* encoder, | |
| 1568 webrtc::VideoCodecType type, | |
| 1569 bool external) | |
| 1570 : encoder(encoder), | |
| 1571 external_encoder(nullptr), | |
| 1572 type(type), | |
| 1573 external(external) { | |
| 1574 if (external) { | |
| 1575 external_encoder = encoder; | |
| 1576 this->encoder = | |
| 1577 new webrtc::VideoEncoderSoftwareFallbackWrapper(type, encoder); | |
| 1578 } | |
| 1579 } | |
| 1580 | |
| 1581 WebRtcVideoChannel2::WebRtcVideoSendStream::WebRtcVideoSendStream( | |
| 1582 webrtc::Call* call, | |
| 1583 const StreamParams& sp, | |
| 1584 const webrtc::VideoSendStream::Config& config, | |
| 1585 WebRtcVideoEncoderFactory* external_encoder_factory, | |
| 1586 const VideoOptions& options, | |
| 1587 int max_bitrate_bps, | |
| 1588 const rtc::Optional<VideoCodecSettings>& codec_settings, | |
| 1589 const std::vector<webrtc::RtpExtension>& rtp_extensions, | |
| 1590 // TODO(deadbeef): Don't duplicate information between send_params, | |
| 1591 // rtp_extensions, options, etc. | |
| 1592 const VideoSendParameters& send_params) | |
| 1593 : ssrcs_(sp.ssrcs), | |
| 1594 ssrc_groups_(sp.ssrc_groups), | |
| 1595 call_(call), | |
| 1596 external_encoder_factory_(external_encoder_factory), | |
| 1597 stream_(NULL), | |
| 1598 parameters_(config, options, max_bitrate_bps, codec_settings), | |
| 1599 allocated_encoder_(NULL, webrtc::kVideoCodecUnknown, false), | |
| 1600 capturer_(NULL), | |
| 1601 sending_(false), | |
| 1602 muted_(false), | |
| 1603 old_adapt_changes_(0), | |
| 1604 first_frame_timestamp_ms_(0), | |
| 1605 last_frame_timestamp_ms_(0) { | |
| 1606 parameters_.config.rtp.max_packet_size = kVideoMtu; | |
| 1607 | |
| 1608 sp.GetPrimarySsrcs(¶meters_.config.rtp.ssrcs); | |
| 1609 sp.GetFidSsrcs(parameters_.config.rtp.ssrcs, | |
| 1610 ¶meters_.config.rtp.rtx.ssrcs); | |
| 1611 parameters_.config.rtp.c_name = sp.cname; | |
| 1612 parameters_.config.rtp.extensions = rtp_extensions; | |
| 1613 parameters_.config.rtp.rtcp_mode = send_params.rtcp.reduced_size | |
| 1614 ? webrtc::RtcpMode::kReducedSize | |
| 1615 : webrtc::RtcpMode::kCompound; | |
| 1616 | |
| 1617 if (codec_settings) { | |
| 1618 SetCodec(*codec_settings); | |
| 1619 } | |
| 1620 } | |
| 1621 | |
| 1622 WebRtcVideoChannel2::WebRtcVideoSendStream::~WebRtcVideoSendStream() { | |
| 1623 DisconnectCapturer(); | |
| 1624 if (stream_ != NULL) { | |
| 1625 call_->DestroyVideoSendStream(stream_); | |
| 1626 } | |
| 1627 DestroyVideoEncoder(&allocated_encoder_); | |
| 1628 } | |
| 1629 | |
| 1630 static void CreateBlackFrame(webrtc::VideoFrame* video_frame, | |
| 1631 int width, | |
| 1632 int height) { | |
| 1633 video_frame->CreateEmptyFrame(width, height, width, (width + 1) / 2, | |
| 1634 (width + 1) / 2); | |
| 1635 memset(video_frame->buffer(webrtc::kYPlane), 16, | |
| 1636 video_frame->allocated_size(webrtc::kYPlane)); | |
| 1637 memset(video_frame->buffer(webrtc::kUPlane), 128, | |
| 1638 video_frame->allocated_size(webrtc::kUPlane)); | |
| 1639 memset(video_frame->buffer(webrtc::kVPlane), 128, | |
| 1640 video_frame->allocated_size(webrtc::kVPlane)); | |
| 1641 } | |
| 1642 | |
| 1643 void WebRtcVideoChannel2::WebRtcVideoSendStream::InputFrame( | |
| 1644 VideoCapturer* capturer, | |
| 1645 const VideoFrame* frame) { | |
| 1646 TRACE_EVENT0("webrtc", "WebRtcVideoSendStream::InputFrame"); | |
| 1647 webrtc::VideoFrame video_frame(frame->GetVideoFrameBuffer(), 0, 0, | |
| 1648 frame->GetVideoRotation()); | |
| 1649 rtc::CritScope cs(&lock_); | |
| 1650 if (stream_ == NULL) { | |
| 1651 // Frame input before send codecs are configured, dropping frame. | |
| 1652 return; | |
| 1653 } | |
| 1654 | |
| 1655 // Not sending, abort early to prevent expensive reconfigurations while | |
| 1656 // setting up codecs etc. | |
| 1657 if (!sending_) | |
| 1658 return; | |
| 1659 | |
| 1660 if (format_.width == 0) { // Dropping frames. | |
| 1661 RTC_DCHECK(format_.height == 0); | |
| 1662 LOG(LS_VERBOSE) << "VideoFormat 0x0 set, Dropping frame."; | |
| 1663 return; | |
| 1664 } | |
| 1665 if (muted_) { | |
| 1666 // Create a black frame to transmit instead. | |
| 1667 CreateBlackFrame(&video_frame, | |
| 1668 static_cast<int>(frame->GetWidth()), | |
| 1669 static_cast<int>(frame->GetHeight())); | |
| 1670 } | |
| 1671 | |
| 1672 int64_t frame_delta_ms = frame->GetTimeStamp() / rtc::kNumNanosecsPerMillisec; | |
| 1673 // frame->GetTimeStamp() is essentially a delta, align to webrtc time | |
| 1674 if (first_frame_timestamp_ms_ == 0) { | |
| 1675 first_frame_timestamp_ms_ = rtc::Time() - frame_delta_ms; | |
| 1676 } | |
| 1677 | |
| 1678 last_frame_timestamp_ms_ = first_frame_timestamp_ms_ + frame_delta_ms; | |
| 1679 video_frame.set_render_time_ms(last_frame_timestamp_ms_); | |
| 1680 // Reconfigure codec if necessary. | |
| 1681 SetDimensions( | |
| 1682 video_frame.width(), video_frame.height(), capturer->IsScreencast()); | |
| 1683 | |
| 1684 stream_->Input()->IncomingCapturedFrame(video_frame); | |
| 1685 } | |
| 1686 | |
| 1687 bool WebRtcVideoChannel2::WebRtcVideoSendStream::SetCapturer( | |
| 1688 VideoCapturer* capturer) { | |
| 1689 TRACE_EVENT0("webrtc", "WebRtcVideoSendStream::SetCapturer"); | |
| 1690 if (!DisconnectCapturer() && capturer == NULL) { | |
| 1691 return false; | |
| 1692 } | |
| 1693 | |
| 1694 { | |
| 1695 rtc::CritScope cs(&lock_); | |
| 1696 | |
| 1697 // Reset timestamps to realign new incoming frames to a webrtc timestamp. A | |
| 1698 // new capturer may have a different timestamp delta than the previous one. | |
| 1699 first_frame_timestamp_ms_ = 0; | |
| 1700 | |
| 1701 if (capturer == NULL) { | |
| 1702 if (stream_ != NULL) { | |
| 1703 LOG(LS_VERBOSE) << "Disabling capturer, sending black frame."; | |
| 1704 webrtc::VideoFrame black_frame; | |
| 1705 | |
| 1706 CreateBlackFrame(&black_frame, last_dimensions_.width, | |
| 1707 last_dimensions_.height); | |
| 1708 | |
| 1709 // Force this black frame not to be dropped due to timestamp order | |
| 1710 // check. As IncomingCapturedFrame will drop the frame if this frame's | |
| 1711 // timestamp is less than or equal to last frame's timestamp, it is | |
| 1712 // necessary to give this black frame a larger timestamp than the | |
| 1713 // previous one. | |
| 1714 last_frame_timestamp_ms_ += | |
| 1715 format_.interval / rtc::kNumNanosecsPerMillisec; | |
| 1716 black_frame.set_render_time_ms(last_frame_timestamp_ms_); | |
| 1717 stream_->Input()->IncomingCapturedFrame(black_frame); | |
| 1718 } | |
| 1719 | |
| 1720 capturer_ = NULL; | |
| 1721 return true; | |
| 1722 } | |
| 1723 | |
| 1724 capturer_ = capturer; | |
| 1725 } | |
| 1726 // Lock cannot be held while connecting the capturer to prevent lock-order | |
| 1727 // violations. | |
| 1728 capturer->SignalVideoFrame.connect(this, &WebRtcVideoSendStream::InputFrame); | |
| 1729 return true; | |
| 1730 } | |
| 1731 | |
| 1732 bool WebRtcVideoChannel2::WebRtcVideoSendStream::SetVideoFormat( | |
| 1733 const VideoFormat& format) { | |
| 1734 if ((format.width == 0 || format.height == 0) && | |
| 1735 format.width != format.height) { | |
| 1736 LOG(LS_ERROR) << "Can't set VideoFormat, width or height is zero (but not " | |
| 1737 "both, 0x0 drops frames)."; | |
| 1738 return false; | |
| 1739 } | |
| 1740 | |
| 1741 rtc::CritScope cs(&lock_); | |
| 1742 if (format.width == 0 && format.height == 0) { | |
| 1743 LOG(LS_INFO) | |
| 1744 << "0x0 resolution selected. Captured frames will be dropped for ssrc: " | |
| 1745 << parameters_.config.rtp.ssrcs[0] << "."; | |
| 1746 } else { | |
| 1747 // TODO(pbos): Fix me, this only affects the last stream! | |
| 1748 parameters_.encoder_config.streams.back().max_framerate = | |
| 1749 VideoFormat::IntervalToFps(format.interval); | |
| 1750 SetDimensions(format.width, format.height, false); | |
| 1751 } | |
| 1752 | |
| 1753 format_ = format; | |
| 1754 return true; | |
| 1755 } | |
| 1756 | |
| 1757 void WebRtcVideoChannel2::WebRtcVideoSendStream::MuteStream(bool mute) { | |
| 1758 rtc::CritScope cs(&lock_); | |
| 1759 muted_ = mute; | |
| 1760 } | |
| 1761 | |
| 1762 bool WebRtcVideoChannel2::WebRtcVideoSendStream::DisconnectCapturer() { | |
| 1763 cricket::VideoCapturer* capturer; | |
| 1764 { | |
| 1765 rtc::CritScope cs(&lock_); | |
| 1766 if (capturer_ == NULL) | |
| 1767 return false; | |
| 1768 | |
| 1769 if (capturer_->video_adapter() != nullptr) | |
| 1770 old_adapt_changes_ += capturer_->video_adapter()->adaptation_changes(); | |
| 1771 | |
| 1772 capturer = capturer_; | |
| 1773 capturer_ = NULL; | |
| 1774 } | |
| 1775 capturer->SignalVideoFrame.disconnect(this); | |
| 1776 return true; | |
| 1777 } | |
| 1778 | |
| 1779 const std::vector<uint32_t>& | |
| 1780 WebRtcVideoChannel2::WebRtcVideoSendStream::GetSsrcs() const { | |
| 1781 return ssrcs_; | |
| 1782 } | |
| 1783 | |
| 1784 void WebRtcVideoChannel2::WebRtcVideoSendStream::SetApplyRotation( | |
| 1785 bool apply_rotation) { | |
| 1786 rtc::CritScope cs(&lock_); | |
| 1787 if (capturer_ == NULL) | |
| 1788 return; | |
| 1789 | |
| 1790 capturer_->SetApplyRotation(apply_rotation); | |
| 1791 } | |
| 1792 | |
| 1793 void WebRtcVideoChannel2::WebRtcVideoSendStream::SetOptions( | |
| 1794 const VideoOptions& options) { | |
| 1795 rtc::CritScope cs(&lock_); | |
| 1796 if (parameters_.codec_settings) { | |
| 1797 LOG(LS_INFO) << "SetCodecAndOptions because of SetOptions; options=" | |
| 1798 << options.ToString(); | |
| 1799 SetCodecAndOptions(*parameters_.codec_settings, options); | |
| 1800 } else { | |
| 1801 parameters_.options = options; | |
| 1802 } | |
| 1803 } | |
| 1804 | |
| 1805 void WebRtcVideoChannel2::WebRtcVideoSendStream::SetCodec( | |
| 1806 const VideoCodecSettings& codec_settings) { | |
| 1807 rtc::CritScope cs(&lock_); | |
| 1808 LOG(LS_INFO) << "SetCodecAndOptions because of SetCodec."; | |
| 1809 SetCodecAndOptions(codec_settings, parameters_.options); | |
| 1810 } | |
| 1811 | |
| 1812 webrtc::VideoCodecType CodecTypeFromName(const std::string& name) { | |
| 1813 if (CodecNamesEq(name, kVp8CodecName)) { | |
| 1814 return webrtc::kVideoCodecVP8; | |
| 1815 } else if (CodecNamesEq(name, kVp9CodecName)) { | |
| 1816 return webrtc::kVideoCodecVP9; | |
| 1817 } else if (CodecNamesEq(name, kH264CodecName)) { | |
| 1818 return webrtc::kVideoCodecH264; | |
| 1819 } | |
| 1820 return webrtc::kVideoCodecUnknown; | |
| 1821 } | |
| 1822 | |
| 1823 WebRtcVideoChannel2::WebRtcVideoSendStream::AllocatedEncoder | |
| 1824 WebRtcVideoChannel2::WebRtcVideoSendStream::CreateVideoEncoder( | |
| 1825 const VideoCodec& codec) { | |
| 1826 webrtc::VideoCodecType type = CodecTypeFromName(codec.name); | |
| 1827 | |
| 1828 // Do not re-create encoders of the same type. | |
| 1829 if (type == allocated_encoder_.type && allocated_encoder_.encoder != NULL) { | |
| 1830 return allocated_encoder_; | |
| 1831 } | |
| 1832 | |
| 1833 if (external_encoder_factory_ != NULL) { | |
| 1834 webrtc::VideoEncoder* encoder = | |
| 1835 external_encoder_factory_->CreateVideoEncoder(type); | |
| 1836 if (encoder != NULL) { | |
| 1837 return AllocatedEncoder(encoder, type, true); | |
| 1838 } | |
| 1839 } | |
| 1840 | |
| 1841 if (type == webrtc::kVideoCodecVP8) { | |
| 1842 return AllocatedEncoder( | |
| 1843 webrtc::VideoEncoder::Create(webrtc::VideoEncoder::kVp8), type, false); | |
| 1844 } else if (type == webrtc::kVideoCodecVP9) { | |
| 1845 return AllocatedEncoder( | |
| 1846 webrtc::VideoEncoder::Create(webrtc::VideoEncoder::kVp9), type, false); | |
| 1847 } else if (type == webrtc::kVideoCodecH264) { | |
| 1848 return AllocatedEncoder( | |
| 1849 webrtc::VideoEncoder::Create(webrtc::VideoEncoder::kH264), type, false); | |
| 1850 } | |
| 1851 | |
| 1852 // This shouldn't happen, we should not be trying to create something we don't | |
| 1853 // support. | |
| 1854 RTC_DCHECK(false); | |
| 1855 return AllocatedEncoder(NULL, webrtc::kVideoCodecUnknown, false); | |
| 1856 } | |
| 1857 | |
| 1858 void WebRtcVideoChannel2::WebRtcVideoSendStream::DestroyVideoEncoder( | |
| 1859 AllocatedEncoder* encoder) { | |
| 1860 if (encoder->external) { | |
| 1861 external_encoder_factory_->DestroyVideoEncoder(encoder->external_encoder); | |
| 1862 } | |
| 1863 delete encoder->encoder; | |
| 1864 } | |
| 1865 | |
| 1866 void WebRtcVideoChannel2::WebRtcVideoSendStream::SetCodecAndOptions( | |
| 1867 const VideoCodecSettings& codec_settings, | |
| 1868 const VideoOptions& options) { | |
| 1869 parameters_.encoder_config = | |
| 1870 CreateVideoEncoderConfig(last_dimensions_, codec_settings.codec); | |
| 1871 if (parameters_.encoder_config.streams.empty()) | |
| 1872 return; | |
| 1873 | |
| 1874 format_ = VideoFormat(codec_settings.codec.width, | |
| 1875 codec_settings.codec.height, | |
| 1876 VideoFormat::FpsToInterval(30), | |
| 1877 FOURCC_I420); | |
| 1878 | |
| 1879 AllocatedEncoder new_encoder = CreateVideoEncoder(codec_settings.codec); | |
| 1880 parameters_.config.encoder_settings.encoder = new_encoder.encoder; | |
| 1881 parameters_.config.encoder_settings.payload_name = codec_settings.codec.name; | |
| 1882 parameters_.config.encoder_settings.payload_type = codec_settings.codec.id; | |
| 1883 if (new_encoder.external) { | |
| 1884 webrtc::VideoCodecType type = CodecTypeFromName(codec_settings.codec.name); | |
| 1885 parameters_.config.encoder_settings.internal_source = | |
| 1886 external_encoder_factory_->EncoderTypeHasInternalSource(type); | |
| 1887 } | |
| 1888 parameters_.config.rtp.fec = codec_settings.fec; | |
| 1889 | |
| 1890 // Set RTX payload type if RTX is enabled. | |
| 1891 if (!parameters_.config.rtp.rtx.ssrcs.empty()) { | |
| 1892 if (codec_settings.rtx_payload_type == -1) { | |
| 1893 LOG(LS_WARNING) << "RTX SSRCs configured but there's no configured RTX " | |
| 1894 "payload type. Ignoring."; | |
| 1895 parameters_.config.rtp.rtx.ssrcs.clear(); | |
| 1896 } else { | |
| 1897 parameters_.config.rtp.rtx.payload_type = codec_settings.rtx_payload_type; | |
| 1898 } | |
| 1899 } | |
| 1900 | |
| 1901 parameters_.config.rtp.nack.rtp_history_ms = | |
| 1902 HasNack(codec_settings.codec) ? kNackHistoryMs : 0; | |
| 1903 | |
| 1904 RTC_CHECK(options.suspend_below_min_bitrate); | |
| 1905 parameters_.config.suspend_below_min_bitrate = | |
| 1906 *options.suspend_below_min_bitrate; | |
| 1907 | |
| 1908 parameters_.codec_settings = | |
| 1909 rtc::Optional<WebRtcVideoChannel2::VideoCodecSettings>(codec_settings); | |
| 1910 parameters_.options = options; | |
| 1911 | |
| 1912 LOG(LS_INFO) | |
| 1913 << "RecreateWebRtcStream (send) because of SetCodecAndOptions; options=" | |
| 1914 << options.ToString(); | |
| 1915 RecreateWebRtcStream(); | |
| 1916 if (allocated_encoder_.encoder != new_encoder.encoder) { | |
| 1917 DestroyVideoEncoder(&allocated_encoder_); | |
| 1918 allocated_encoder_ = new_encoder; | |
| 1919 } | |
| 1920 } | |
| 1921 | |
| 1922 void WebRtcVideoChannel2::WebRtcVideoSendStream::SetRtpExtensions( | |
| 1923 const std::vector<webrtc::RtpExtension>& rtp_extensions) { | |
| 1924 rtc::CritScope cs(&lock_); | |
| 1925 parameters_.config.rtp.extensions = rtp_extensions; | |
| 1926 if (stream_ != nullptr) { | |
| 1927 LOG(LS_INFO) << "RecreateWebRtcStream (send) because of SetRtpExtensions"; | |
| 1928 RecreateWebRtcStream(); | |
| 1929 } | |
| 1930 } | |
| 1931 | |
| 1932 void WebRtcVideoChannel2::WebRtcVideoSendStream::SetSendParameters( | |
| 1933 const VideoSendParameters& send_params) { | |
| 1934 rtc::CritScope cs(&lock_); | |
| 1935 parameters_.config.rtp.rtcp_mode = send_params.rtcp.reduced_size | |
| 1936 ? webrtc::RtcpMode::kReducedSize | |
| 1937 : webrtc::RtcpMode::kCompound; | |
| 1938 if (stream_ != nullptr) { | |
| 1939 LOG(LS_INFO) << "RecreateWebRtcStream (send) because of SetSendParameters"; | |
| 1940 RecreateWebRtcStream(); | |
| 1941 } | |
| 1942 } | |
| 1943 | |
| 1944 webrtc::VideoEncoderConfig | |
| 1945 WebRtcVideoChannel2::WebRtcVideoSendStream::CreateVideoEncoderConfig( | |
| 1946 const Dimensions& dimensions, | |
| 1947 const VideoCodec& codec) const { | |
| 1948 webrtc::VideoEncoderConfig encoder_config; | |
| 1949 if (dimensions.is_screencast) { | |
| 1950 RTC_CHECK(parameters_.options.screencast_min_bitrate); | |
| 1951 encoder_config.min_transmit_bitrate_bps = | |
| 1952 *parameters_.options.screencast_min_bitrate * 1000; | |
| 1953 encoder_config.content_type = | |
| 1954 webrtc::VideoEncoderConfig::ContentType::kScreen; | |
| 1955 } else { | |
| 1956 encoder_config.min_transmit_bitrate_bps = 0; | |
| 1957 encoder_config.content_type = | |
| 1958 webrtc::VideoEncoderConfig::ContentType::kRealtimeVideo; | |
| 1959 } | |
| 1960 | |
| 1961 // Restrict dimensions according to codec max. | |
| 1962 int width = dimensions.width; | |
| 1963 int height = dimensions.height; | |
| 1964 if (!dimensions.is_screencast) { | |
| 1965 if (codec.width < width) | |
| 1966 width = codec.width; | |
| 1967 if (codec.height < height) | |
| 1968 height = codec.height; | |
| 1969 } | |
| 1970 | |
| 1971 VideoCodec clamped_codec = codec; | |
| 1972 clamped_codec.width = width; | |
| 1973 clamped_codec.height = height; | |
| 1974 | |
| 1975 // By default, the stream count for the codec configuration should match the | |
| 1976 // number of negotiated ssrcs. But if the codec is blacklisted for simulcast | |
| 1977 // or a screencast, only configure a single stream. | |
| 1978 size_t stream_count = parameters_.config.rtp.ssrcs.size(); | |
| 1979 if (IsCodecBlacklistedForSimulcast(codec.name) || dimensions.is_screencast) { | |
| 1980 stream_count = 1; | |
| 1981 } | |
| 1982 | |
| 1983 encoder_config.streams = | |
| 1984 CreateVideoStreams(clamped_codec, parameters_.options, | |
| 1985 parameters_.max_bitrate_bps, stream_count); | |
| 1986 | |
| 1987 // Conference mode screencast uses 2 temporal layers split at 100kbit. | |
| 1988 if (parameters_.options.conference_mode.value_or(false) && | |
| 1989 dimensions.is_screencast && encoder_config.streams.size() == 1) { | |
| 1990 ScreenshareLayerConfig config = ScreenshareLayerConfig::GetDefault(); | |
| 1991 | |
| 1992 // For screenshare in conference mode, tl0 and tl1 bitrates are piggybacked | |
| 1993 // on the VideoCodec struct as target and max bitrates, respectively. | |
| 1994 // See eg. webrtc::VP8EncoderImpl::SetRates(). | |
| 1995 encoder_config.streams[0].target_bitrate_bps = | |
| 1996 config.tl0_bitrate_kbps * 1000; | |
| 1997 encoder_config.streams[0].max_bitrate_bps = config.tl1_bitrate_kbps * 1000; | |
| 1998 encoder_config.streams[0].temporal_layer_thresholds_bps.clear(); | |
| 1999 encoder_config.streams[0].temporal_layer_thresholds_bps.push_back( | |
| 2000 config.tl0_bitrate_kbps * 1000); | |
| 2001 } | |
| 2002 return encoder_config; | |
| 2003 } | |
| 2004 | |
| 2005 void WebRtcVideoChannel2::WebRtcVideoSendStream::SetDimensions( | |
| 2006 int width, | |
| 2007 int height, | |
| 2008 bool is_screencast) { | |
| 2009 if (last_dimensions_.width == width && last_dimensions_.height == height && | |
| 2010 last_dimensions_.is_screencast == is_screencast) { | |
| 2011 // Configured using the same parameters, do not reconfigure. | |
| 2012 return; | |
| 2013 } | |
| 2014 LOG(LS_INFO) << "SetDimensions: " << width << "x" << height | |
| 2015 << (is_screencast ? " (screencast)" : " (not screencast)"); | |
| 2016 | |
| 2017 last_dimensions_.width = width; | |
| 2018 last_dimensions_.height = height; | |
| 2019 last_dimensions_.is_screencast = is_screencast; | |
| 2020 | |
| 2021 RTC_DCHECK(!parameters_.encoder_config.streams.empty()); | |
| 2022 | |
| 2023 RTC_CHECK(parameters_.codec_settings); | |
| 2024 VideoCodecSettings codec_settings = *parameters_.codec_settings; | |
| 2025 | |
| 2026 webrtc::VideoEncoderConfig encoder_config = | |
| 2027 CreateVideoEncoderConfig(last_dimensions_, codec_settings.codec); | |
| 2028 | |
| 2029 encoder_config.encoder_specific_settings = ConfigureVideoEncoderSettings( | |
| 2030 codec_settings.codec, parameters_.options, is_screencast); | |
| 2031 | |
| 2032 bool stream_reconfigured = stream_->ReconfigureVideoEncoder(encoder_config); | |
| 2033 | |
| 2034 encoder_config.encoder_specific_settings = NULL; | |
| 2035 | |
| 2036 if (!stream_reconfigured) { | |
| 2037 LOG(LS_WARNING) << "Failed to reconfigure video encoder for dimensions: " | |
| 2038 << width << "x" << height; | |
| 2039 return; | |
| 2040 } | |
| 2041 | |
| 2042 parameters_.encoder_config = encoder_config; | |
| 2043 } | |
| 2044 | |
| 2045 void WebRtcVideoChannel2::WebRtcVideoSendStream::Start() { | |
| 2046 rtc::CritScope cs(&lock_); | |
| 2047 RTC_DCHECK(stream_ != NULL); | |
| 2048 stream_->Start(); | |
| 2049 sending_ = true; | |
| 2050 } | |
| 2051 | |
| 2052 void WebRtcVideoChannel2::WebRtcVideoSendStream::Stop() { | |
| 2053 rtc::CritScope cs(&lock_); | |
| 2054 if (stream_ != NULL) { | |
| 2055 stream_->Stop(); | |
| 2056 } | |
| 2057 sending_ = false; | |
| 2058 } | |
| 2059 | |
| 2060 VideoSenderInfo | |
| 2061 WebRtcVideoChannel2::WebRtcVideoSendStream::GetVideoSenderInfo() { | |
| 2062 VideoSenderInfo info; | |
| 2063 webrtc::VideoSendStream::Stats stats; | |
| 2064 { | |
| 2065 rtc::CritScope cs(&lock_); | |
| 2066 for (uint32_t ssrc : parameters_.config.rtp.ssrcs) | |
| 2067 info.add_ssrc(ssrc); | |
| 2068 | |
| 2069 if (parameters_.codec_settings) | |
| 2070 info.codec_name = parameters_.codec_settings->codec.name; | |
| 2071 for (size_t i = 0; i < parameters_.encoder_config.streams.size(); ++i) { | |
| 2072 if (i == parameters_.encoder_config.streams.size() - 1) { | |
| 2073 info.preferred_bitrate += | |
| 2074 parameters_.encoder_config.streams[i].max_bitrate_bps; | |
| 2075 } else { | |
| 2076 info.preferred_bitrate += | |
| 2077 parameters_.encoder_config.streams[i].target_bitrate_bps; | |
| 2078 } | |
| 2079 } | |
| 2080 | |
| 2081 if (stream_ == NULL) | |
| 2082 return info; | |
| 2083 | |
| 2084 stats = stream_->GetStats(); | |
| 2085 | |
| 2086 info.adapt_changes = old_adapt_changes_; | |
| 2087 info.adapt_reason = CoordinatedVideoAdapter::ADAPTREASON_NONE; | |
| 2088 | |
| 2089 if (capturer_ != NULL) { | |
| 2090 if (!capturer_->IsMuted()) { | |
| 2091 VideoFormat last_captured_frame_format; | |
| 2092 capturer_->GetStats(&info.adapt_frame_drops, &info.effects_frame_drops, | |
| 2093 &info.capturer_frame_time, | |
| 2094 &last_captured_frame_format); | |
| 2095 info.input_frame_width = last_captured_frame_format.width; | |
| 2096 info.input_frame_height = last_captured_frame_format.height; | |
| 2097 } | |
| 2098 if (capturer_->video_adapter() != nullptr) { | |
| 2099 info.adapt_changes += capturer_->video_adapter()->adaptation_changes(); | |
| 2100 info.adapt_reason = capturer_->video_adapter()->adapt_reason(); | |
| 2101 } | |
| 2102 } | |
| 2103 } | |
| 2104 | |
| 2105 // Get bandwidth limitation info from stream_->GetStats(). | |
| 2106 // Input resolution (output from video_adapter) can be further scaled down or | |
| 2107 // higher video layer(s) can be dropped due to bitrate constraints. | |
| 2108 // Note, adapt_changes only include changes from the video_adapter. | |
| 2109 if (stats.bw_limited_resolution) | |
| 2110 info.adapt_reason |= CoordinatedVideoAdapter::ADAPTREASON_BANDWIDTH; | |
| 2111 | |
| 2112 info.encoder_implementation_name = stats.encoder_implementation_name; | |
| 2113 info.ssrc_groups = ssrc_groups_; | |
| 2114 info.framerate_input = stats.input_frame_rate; | |
| 2115 info.framerate_sent = stats.encode_frame_rate; | |
| 2116 info.avg_encode_ms = stats.avg_encode_time_ms; | |
| 2117 info.encode_usage_percent = stats.encode_usage_percent; | |
| 2118 | |
| 2119 info.nominal_bitrate = stats.media_bitrate_bps; | |
| 2120 | |
| 2121 info.send_frame_width = 0; | |
| 2122 info.send_frame_height = 0; | |
| 2123 for (std::map<uint32_t, webrtc::VideoSendStream::StreamStats>::iterator it = | |
| 2124 stats.substreams.begin(); | |
| 2125 it != stats.substreams.end(); ++it) { | |
| 2126 // TODO(pbos): Wire up additional stats, such as padding bytes. | |
| 2127 webrtc::VideoSendStream::StreamStats stream_stats = it->second; | |
| 2128 info.bytes_sent += stream_stats.rtp_stats.transmitted.payload_bytes + | |
| 2129 stream_stats.rtp_stats.transmitted.header_bytes + | |
| 2130 stream_stats.rtp_stats.transmitted.padding_bytes; | |
| 2131 info.packets_sent += stream_stats.rtp_stats.transmitted.packets; | |
| 2132 info.packets_lost += stream_stats.rtcp_stats.cumulative_lost; | |
| 2133 if (stream_stats.width > info.send_frame_width) | |
| 2134 info.send_frame_width = stream_stats.width; | |
| 2135 if (stream_stats.height > info.send_frame_height) | |
| 2136 info.send_frame_height = stream_stats.height; | |
| 2137 info.firs_rcvd += stream_stats.rtcp_packet_type_counts.fir_packets; | |
| 2138 info.nacks_rcvd += stream_stats.rtcp_packet_type_counts.nack_packets; | |
| 2139 info.plis_rcvd += stream_stats.rtcp_packet_type_counts.pli_packets; | |
| 2140 } | |
| 2141 | |
| 2142 if (!stats.substreams.empty()) { | |
| 2143 // TODO(pbos): Report fraction lost per SSRC. | |
| 2144 webrtc::VideoSendStream::StreamStats first_stream_stats = | |
| 2145 stats.substreams.begin()->second; | |
| 2146 info.fraction_lost = | |
| 2147 static_cast<float>(first_stream_stats.rtcp_stats.fraction_lost) / | |
| 2148 (1 << 8); | |
| 2149 } | |
| 2150 | |
| 2151 return info; | |
| 2152 } | |
| 2153 | |
| 2154 void WebRtcVideoChannel2::WebRtcVideoSendStream::FillBandwidthEstimationInfo( | |
| 2155 BandwidthEstimationInfo* bwe_info) { | |
| 2156 rtc::CritScope cs(&lock_); | |
| 2157 if (stream_ == NULL) { | |
| 2158 return; | |
| 2159 } | |
| 2160 webrtc::VideoSendStream::Stats stats = stream_->GetStats(); | |
| 2161 for (std::map<uint32_t, webrtc::VideoSendStream::StreamStats>::iterator it = | |
| 2162 stats.substreams.begin(); | |
| 2163 it != stats.substreams.end(); ++it) { | |
| 2164 bwe_info->transmit_bitrate += it->second.total_bitrate_bps; | |
| 2165 bwe_info->retransmit_bitrate += it->second.retransmit_bitrate_bps; | |
| 2166 } | |
| 2167 bwe_info->target_enc_bitrate += stats.target_media_bitrate_bps; | |
| 2168 bwe_info->actual_enc_bitrate += stats.media_bitrate_bps; | |
| 2169 } | |
| 2170 | |
| 2171 void WebRtcVideoChannel2::WebRtcVideoSendStream::SetMaxBitrateBps( | |
| 2172 int max_bitrate_bps) { | |
| 2173 rtc::CritScope cs(&lock_); | |
| 2174 parameters_.max_bitrate_bps = max_bitrate_bps; | |
| 2175 | |
| 2176 // No need to reconfigure if the stream hasn't been configured yet. | |
| 2177 if (parameters_.encoder_config.streams.empty()) | |
| 2178 return; | |
| 2179 | |
| 2180 // Force a stream reconfigure to set the new max bitrate. | |
| 2181 int width = last_dimensions_.width; | |
| 2182 last_dimensions_.width = 0; | |
| 2183 SetDimensions(width, last_dimensions_.height, last_dimensions_.is_screencast); | |
| 2184 } | |
| 2185 | |
| 2186 void WebRtcVideoChannel2::WebRtcVideoSendStream::RecreateWebRtcStream() { | |
| 2187 if (stream_ != NULL) { | |
| 2188 call_->DestroyVideoSendStream(stream_); | |
| 2189 } | |
| 2190 | |
| 2191 RTC_CHECK(parameters_.codec_settings); | |
| 2192 parameters_.encoder_config.encoder_specific_settings = | |
| 2193 ConfigureVideoEncoderSettings( | |
| 2194 parameters_.codec_settings->codec, parameters_.options, | |
| 2195 parameters_.encoder_config.content_type == | |
| 2196 webrtc::VideoEncoderConfig::ContentType::kScreen); | |
| 2197 | |
| 2198 webrtc::VideoSendStream::Config config = parameters_.config; | |
| 2199 if (!config.rtp.rtx.ssrcs.empty() && config.rtp.rtx.payload_type == -1) { | |
| 2200 LOG(LS_WARNING) << "RTX SSRCs configured but there's no configured RTX " | |
| 2201 "payload type the set codec. Ignoring RTX."; | |
| 2202 config.rtp.rtx.ssrcs.clear(); | |
| 2203 } | |
| 2204 stream_ = call_->CreateVideoSendStream(config, parameters_.encoder_config); | |
| 2205 | |
| 2206 parameters_.encoder_config.encoder_specific_settings = NULL; | |
| 2207 | |
| 2208 if (sending_) { | |
| 2209 stream_->Start(); | |
| 2210 } | |
| 2211 } | |
| 2212 | |
| 2213 WebRtcVideoChannel2::WebRtcVideoReceiveStream::WebRtcVideoReceiveStream( | |
| 2214 webrtc::Call* call, | |
| 2215 const StreamParams& sp, | |
| 2216 const webrtc::VideoReceiveStream::Config& config, | |
| 2217 WebRtcVideoDecoderFactory* external_decoder_factory, | |
| 2218 bool default_stream, | |
| 2219 const std::vector<VideoCodecSettings>& recv_codecs, | |
| 2220 bool disable_prerenderer_smoothing) | |
| 2221 : call_(call), | |
| 2222 ssrcs_(sp.ssrcs), | |
| 2223 ssrc_groups_(sp.ssrc_groups), | |
| 2224 stream_(NULL), | |
| 2225 default_stream_(default_stream), | |
| 2226 config_(config), | |
| 2227 external_decoder_factory_(external_decoder_factory), | |
| 2228 disable_prerenderer_smoothing_(disable_prerenderer_smoothing), | |
| 2229 renderer_(NULL), | |
| 2230 last_width_(-1), | |
| 2231 last_height_(-1), | |
| 2232 first_frame_timestamp_(-1), | |
| 2233 estimated_remote_start_ntp_time_ms_(0) { | |
| 2234 config_.renderer = this; | |
| 2235 // SetRecvCodecs will also reset (start) the VideoReceiveStream. | |
| 2236 LOG(LS_INFO) << "SetRecvCodecs (recv) because we are creating the receive " | |
| 2237 "stream for the first time: " | |
| 2238 << CodecSettingsVectorToString(recv_codecs); | |
| 2239 SetRecvCodecs(recv_codecs); | |
| 2240 } | |
| 2241 | |
| 2242 WebRtcVideoChannel2::WebRtcVideoReceiveStream::AllocatedDecoder:: | |
| 2243 AllocatedDecoder(webrtc::VideoDecoder* decoder, | |
| 2244 webrtc::VideoCodecType type, | |
| 2245 bool external) | |
| 2246 : decoder(decoder), | |
| 2247 external_decoder(nullptr), | |
| 2248 type(type), | |
| 2249 external(external) { | |
| 2250 if (external) { | |
| 2251 external_decoder = decoder; | |
| 2252 this->decoder = | |
| 2253 new webrtc::VideoDecoderSoftwareFallbackWrapper(type, external_decoder); | |
| 2254 } | |
| 2255 } | |
| 2256 | |
| 2257 WebRtcVideoChannel2::WebRtcVideoReceiveStream::~WebRtcVideoReceiveStream() { | |
| 2258 call_->DestroyVideoReceiveStream(stream_); | |
| 2259 ClearDecoders(&allocated_decoders_); | |
| 2260 } | |
| 2261 | |
| 2262 const std::vector<uint32_t>& | |
| 2263 WebRtcVideoChannel2::WebRtcVideoReceiveStream::GetSsrcs() const { | |
| 2264 return ssrcs_; | |
| 2265 } | |
| 2266 | |
| 2267 WebRtcVideoChannel2::WebRtcVideoReceiveStream::AllocatedDecoder | |
| 2268 WebRtcVideoChannel2::WebRtcVideoReceiveStream::CreateOrReuseVideoDecoder( | |
| 2269 std::vector<AllocatedDecoder>* old_decoders, | |
| 2270 const VideoCodec& codec) { | |
| 2271 webrtc::VideoCodecType type = CodecTypeFromName(codec.name); | |
| 2272 | |
| 2273 for (size_t i = 0; i < old_decoders->size(); ++i) { | |
| 2274 if ((*old_decoders)[i].type == type) { | |
| 2275 AllocatedDecoder decoder = (*old_decoders)[i]; | |
| 2276 (*old_decoders)[i] = old_decoders->back(); | |
| 2277 old_decoders->pop_back(); | |
| 2278 return decoder; | |
| 2279 } | |
| 2280 } | |
| 2281 | |
| 2282 if (external_decoder_factory_ != NULL) { | |
| 2283 webrtc::VideoDecoder* decoder = | |
| 2284 external_decoder_factory_->CreateVideoDecoder(type); | |
| 2285 if (decoder != NULL) { | |
| 2286 return AllocatedDecoder(decoder, type, true); | |
| 2287 } | |
| 2288 } | |
| 2289 | |
| 2290 if (type == webrtc::kVideoCodecVP8) { | |
| 2291 return AllocatedDecoder( | |
| 2292 webrtc::VideoDecoder::Create(webrtc::VideoDecoder::kVp8), type, false); | |
| 2293 } | |
| 2294 | |
| 2295 if (type == webrtc::kVideoCodecVP9) { | |
| 2296 return AllocatedDecoder( | |
| 2297 webrtc::VideoDecoder::Create(webrtc::VideoDecoder::kVp9), type, false); | |
| 2298 } | |
| 2299 | |
| 2300 if (type == webrtc::kVideoCodecH264) { | |
| 2301 return AllocatedDecoder( | |
| 2302 webrtc::VideoDecoder::Create(webrtc::VideoDecoder::kH264), type, false); | |
| 2303 } | |
| 2304 | |
| 2305 // This shouldn't happen, we should not be trying to create something we don't | |
| 2306 // support. | |
| 2307 RTC_DCHECK(false); | |
| 2308 return AllocatedDecoder(NULL, webrtc::kVideoCodecUnknown, false); | |
| 2309 } | |
| 2310 | |
| 2311 void WebRtcVideoChannel2::WebRtcVideoReceiveStream::SetRecvCodecs( | |
| 2312 const std::vector<VideoCodecSettings>& recv_codecs) { | |
| 2313 std::vector<AllocatedDecoder> old_decoders = allocated_decoders_; | |
| 2314 allocated_decoders_.clear(); | |
| 2315 config_.decoders.clear(); | |
| 2316 for (size_t i = 0; i < recv_codecs.size(); ++i) { | |
| 2317 AllocatedDecoder allocated_decoder = | |
| 2318 CreateOrReuseVideoDecoder(&old_decoders, recv_codecs[i].codec); | |
| 2319 allocated_decoders_.push_back(allocated_decoder); | |
| 2320 | |
| 2321 webrtc::VideoReceiveStream::Decoder decoder; | |
| 2322 decoder.decoder = allocated_decoder.decoder; | |
| 2323 decoder.payload_type = recv_codecs[i].codec.id; | |
| 2324 decoder.payload_name = recv_codecs[i].codec.name; | |
| 2325 config_.decoders.push_back(decoder); | |
| 2326 } | |
| 2327 | |
| 2328 // TODO(pbos): Reconfigure RTX based on incoming recv_codecs. | |
| 2329 config_.rtp.fec = recv_codecs.front().fec; | |
| 2330 config_.rtp.nack.rtp_history_ms = | |
| 2331 HasNack(recv_codecs.begin()->codec) ? kNackHistoryMs : 0; | |
| 2332 | |
| 2333 LOG(LS_INFO) << "RecreateWebRtcStream (recv) because of SetRecvCodecs: " | |
| 2334 << CodecSettingsVectorToString(recv_codecs); | |
| 2335 RecreateWebRtcStream(); | |
| 2336 ClearDecoders(&old_decoders); | |
| 2337 } | |
| 2338 | |
| 2339 void WebRtcVideoChannel2::WebRtcVideoReceiveStream::SetLocalSsrc( | |
| 2340 uint32_t local_ssrc) { | |
| 2341 // TODO(pbos): Consider turning this sanity check into a RTC_DCHECK. You | |
| 2342 // should not be able to create a sender with the same SSRC as a receiver, but | |
| 2343 // right now this can't be done due to unittests depending on receiving what | |
| 2344 // they are sending from the same MediaChannel. | |
| 2345 if (local_ssrc == config_.rtp.remote_ssrc) { | |
| 2346 LOG(LS_INFO) << "Ignoring call to SetLocalSsrc because parameters are " | |
| 2347 "unchanged; local_ssrc=" << local_ssrc; | |
| 2348 return; | |
| 2349 } | |
| 2350 | |
| 2351 config_.rtp.local_ssrc = local_ssrc; | |
| 2352 LOG(LS_INFO) | |
| 2353 << "RecreateWebRtcStream (recv) because of SetLocalSsrc; local_ssrc=" | |
| 2354 << local_ssrc; | |
| 2355 RecreateWebRtcStream(); | |
| 2356 } | |
| 2357 | |
| 2358 void WebRtcVideoChannel2::WebRtcVideoReceiveStream::SetFeedbackParameters( | |
| 2359 bool nack_enabled, | |
| 2360 bool remb_enabled, | |
| 2361 bool transport_cc_enabled) { | |
| 2362 int nack_history_ms = nack_enabled ? kNackHistoryMs : 0; | |
| 2363 if (config_.rtp.nack.rtp_history_ms == nack_history_ms && | |
| 2364 config_.rtp.remb == remb_enabled && | |
| 2365 config_.rtp.transport_cc == transport_cc_enabled) { | |
| 2366 LOG(LS_INFO) | |
| 2367 << "Ignoring call to SetFeedbackParameters because parameters are " | |
| 2368 "unchanged; nack=" | |
| 2369 << nack_enabled << ", remb=" << remb_enabled | |
| 2370 << ", transport_cc=" << transport_cc_enabled; | |
| 2371 return; | |
| 2372 } | |
| 2373 config_.rtp.remb = remb_enabled; | |
| 2374 config_.rtp.nack.rtp_history_ms = nack_history_ms; | |
| 2375 config_.rtp.transport_cc = transport_cc_enabled; | |
| 2376 LOG(LS_INFO) | |
| 2377 << "RecreateWebRtcStream (recv) because of SetFeedbackParameters; nack=" | |
| 2378 << nack_enabled << ", remb=" << remb_enabled | |
| 2379 << ", transport_cc=" << transport_cc_enabled; | |
| 2380 RecreateWebRtcStream(); | |
| 2381 } | |
| 2382 | |
| 2383 void WebRtcVideoChannel2::WebRtcVideoReceiveStream::SetRtpExtensions( | |
| 2384 const std::vector<webrtc::RtpExtension>& extensions) { | |
| 2385 config_.rtp.extensions = extensions; | |
| 2386 LOG(LS_INFO) << "RecreateWebRtcStream (recv) because of SetRtpExtensions"; | |
| 2387 RecreateWebRtcStream(); | |
| 2388 } | |
| 2389 | |
| 2390 void WebRtcVideoChannel2::WebRtcVideoReceiveStream::SetRecvParameters( | |
| 2391 const VideoRecvParameters& recv_params) { | |
| 2392 config_.rtp.rtcp_mode = recv_params.rtcp.reduced_size | |
| 2393 ? webrtc::RtcpMode::kReducedSize | |
| 2394 : webrtc::RtcpMode::kCompound; | |
| 2395 LOG(LS_INFO) << "RecreateWebRtcStream (recv) because of SetRecvParameters"; | |
| 2396 RecreateWebRtcStream(); | |
| 2397 } | |
| 2398 | |
| 2399 void WebRtcVideoChannel2::WebRtcVideoReceiveStream::RecreateWebRtcStream() { | |
| 2400 if (stream_ != NULL) { | |
| 2401 call_->DestroyVideoReceiveStream(stream_); | |
| 2402 } | |
| 2403 stream_ = call_->CreateVideoReceiveStream(config_); | |
| 2404 stream_->Start(); | |
| 2405 } | |
| 2406 | |
| 2407 void WebRtcVideoChannel2::WebRtcVideoReceiveStream::ClearDecoders( | |
| 2408 std::vector<AllocatedDecoder>* allocated_decoders) { | |
| 2409 for (size_t i = 0; i < allocated_decoders->size(); ++i) { | |
| 2410 if ((*allocated_decoders)[i].external) { | |
| 2411 external_decoder_factory_->DestroyVideoDecoder( | |
| 2412 (*allocated_decoders)[i].external_decoder); | |
| 2413 } | |
| 2414 delete (*allocated_decoders)[i].decoder; | |
| 2415 } | |
| 2416 allocated_decoders->clear(); | |
| 2417 } | |
| 2418 | |
| 2419 void WebRtcVideoChannel2::WebRtcVideoReceiveStream::RenderFrame( | |
| 2420 const webrtc::VideoFrame& frame, | |
| 2421 int time_to_render_ms) { | |
| 2422 rtc::CritScope crit(&renderer_lock_); | |
| 2423 | |
| 2424 if (first_frame_timestamp_ < 0) | |
| 2425 first_frame_timestamp_ = frame.timestamp(); | |
| 2426 int64_t rtp_time_elapsed_since_first_frame = | |
| 2427 (timestamp_wraparound_handler_.Unwrap(frame.timestamp()) - | |
| 2428 first_frame_timestamp_); | |
| 2429 int64_t elapsed_time_ms = rtp_time_elapsed_since_first_frame / | |
| 2430 (cricket::kVideoCodecClockrate / 1000); | |
| 2431 if (frame.ntp_time_ms() > 0) | |
| 2432 estimated_remote_start_ntp_time_ms_ = frame.ntp_time_ms() - elapsed_time_ms; | |
| 2433 | |
| 2434 if (renderer_ == NULL) { | |
| 2435 LOG(LS_WARNING) << "VideoReceiveStream not connected to a VideoRenderer."; | |
| 2436 return; | |
| 2437 } | |
| 2438 | |
| 2439 last_width_ = frame.width(); | |
| 2440 last_height_ = frame.height(); | |
| 2441 | |
| 2442 const WebRtcVideoFrame render_frame( | |
| 2443 frame.video_frame_buffer(), | |
| 2444 frame.render_time_ms() * rtc::kNumNanosecsPerMillisec, frame.rotation()); | |
| 2445 renderer_->RenderFrame(&render_frame); | |
| 2446 } | |
| 2447 | |
| 2448 bool WebRtcVideoChannel2::WebRtcVideoReceiveStream::IsTextureSupported() const { | |
| 2449 return true; | |
| 2450 } | |
| 2451 | |
| 2452 bool WebRtcVideoChannel2::WebRtcVideoReceiveStream::SmoothsRenderedFrames() | |
| 2453 const { | |
| 2454 return disable_prerenderer_smoothing_; | |
| 2455 } | |
| 2456 | |
| 2457 bool WebRtcVideoChannel2::WebRtcVideoReceiveStream::IsDefaultStream() const { | |
| 2458 return default_stream_; | |
| 2459 } | |
| 2460 | |
| 2461 void WebRtcVideoChannel2::WebRtcVideoReceiveStream::SetRenderer( | |
| 2462 cricket::VideoRenderer* renderer) { | |
| 2463 rtc::CritScope crit(&renderer_lock_); | |
| 2464 renderer_ = renderer; | |
| 2465 } | |
| 2466 | |
| 2467 std::string | |
| 2468 WebRtcVideoChannel2::WebRtcVideoReceiveStream::GetCodecNameFromPayloadType( | |
| 2469 int payload_type) { | |
| 2470 for (const webrtc::VideoReceiveStream::Decoder& decoder : config_.decoders) { | |
| 2471 if (decoder.payload_type == payload_type) { | |
| 2472 return decoder.payload_name; | |
| 2473 } | |
| 2474 } | |
| 2475 return ""; | |
| 2476 } | |
| 2477 | |
| 2478 VideoReceiverInfo | |
| 2479 WebRtcVideoChannel2::WebRtcVideoReceiveStream::GetVideoReceiverInfo() { | |
| 2480 VideoReceiverInfo info; | |
| 2481 info.ssrc_groups = ssrc_groups_; | |
| 2482 info.add_ssrc(config_.rtp.remote_ssrc); | |
| 2483 webrtc::VideoReceiveStream::Stats stats = stream_->GetStats(); | |
| 2484 info.decoder_implementation_name = stats.decoder_implementation_name; | |
| 2485 info.bytes_rcvd = stats.rtp_stats.transmitted.payload_bytes + | |
| 2486 stats.rtp_stats.transmitted.header_bytes + | |
| 2487 stats.rtp_stats.transmitted.padding_bytes; | |
| 2488 info.packets_rcvd = stats.rtp_stats.transmitted.packets; | |
| 2489 info.packets_lost = stats.rtcp_stats.cumulative_lost; | |
| 2490 info.fraction_lost = | |
| 2491 static_cast<float>(stats.rtcp_stats.fraction_lost) / (1 << 8); | |
| 2492 | |
| 2493 info.framerate_rcvd = stats.network_frame_rate; | |
| 2494 info.framerate_decoded = stats.decode_frame_rate; | |
| 2495 info.framerate_output = stats.render_frame_rate; | |
| 2496 | |
| 2497 { | |
| 2498 rtc::CritScope frame_cs(&renderer_lock_); | |
| 2499 info.frame_width = last_width_; | |
| 2500 info.frame_height = last_height_; | |
| 2501 info.capture_start_ntp_time_ms = estimated_remote_start_ntp_time_ms_; | |
| 2502 } | |
| 2503 | |
| 2504 info.decode_ms = stats.decode_ms; | |
| 2505 info.max_decode_ms = stats.max_decode_ms; | |
| 2506 info.current_delay_ms = stats.current_delay_ms; | |
| 2507 info.target_delay_ms = stats.target_delay_ms; | |
| 2508 info.jitter_buffer_ms = stats.jitter_buffer_ms; | |
| 2509 info.min_playout_delay_ms = stats.min_playout_delay_ms; | |
| 2510 info.render_delay_ms = stats.render_delay_ms; | |
| 2511 | |
| 2512 info.codec_name = GetCodecNameFromPayloadType(stats.current_payload_type); | |
| 2513 | |
| 2514 info.firs_sent = stats.rtcp_packet_type_counts.fir_packets; | |
| 2515 info.plis_sent = stats.rtcp_packet_type_counts.pli_packets; | |
| 2516 info.nacks_sent = stats.rtcp_packet_type_counts.nack_packets; | |
| 2517 | |
| 2518 return info; | |
| 2519 } | |
| 2520 | |
| 2521 WebRtcVideoChannel2::VideoCodecSettings::VideoCodecSettings() | |
| 2522 : rtx_payload_type(-1) {} | |
| 2523 | |
| 2524 bool WebRtcVideoChannel2::VideoCodecSettings::operator==( | |
| 2525 const WebRtcVideoChannel2::VideoCodecSettings& other) const { | |
| 2526 return codec == other.codec && | |
| 2527 fec.ulpfec_payload_type == other.fec.ulpfec_payload_type && | |
| 2528 fec.red_payload_type == other.fec.red_payload_type && | |
| 2529 fec.red_rtx_payload_type == other.fec.red_rtx_payload_type && | |
| 2530 rtx_payload_type == other.rtx_payload_type; | |
| 2531 } | |
| 2532 | |
| 2533 bool WebRtcVideoChannel2::VideoCodecSettings::operator!=( | |
| 2534 const WebRtcVideoChannel2::VideoCodecSettings& other) const { | |
| 2535 return !(*this == other); | |
| 2536 } | |
| 2537 | |
| 2538 std::vector<WebRtcVideoChannel2::VideoCodecSettings> | |
| 2539 WebRtcVideoChannel2::MapCodecs(const std::vector<VideoCodec>& codecs) { | |
| 2540 RTC_DCHECK(!codecs.empty()); | |
| 2541 | |
| 2542 std::vector<VideoCodecSettings> video_codecs; | |
| 2543 std::map<int, bool> payload_used; | |
| 2544 std::map<int, VideoCodec::CodecType> payload_codec_type; | |
| 2545 // |rtx_mapping| maps video payload type to rtx payload type. | |
| 2546 std::map<int, int> rtx_mapping; | |
| 2547 | |
| 2548 webrtc::FecConfig fec_settings; | |
| 2549 | |
| 2550 for (size_t i = 0; i < codecs.size(); ++i) { | |
| 2551 const VideoCodec& in_codec = codecs[i]; | |
| 2552 int payload_type = in_codec.id; | |
| 2553 | |
| 2554 if (payload_used[payload_type]) { | |
| 2555 LOG(LS_ERROR) << "Payload type already registered: " | |
| 2556 << in_codec.ToString(); | |
| 2557 return std::vector<VideoCodecSettings>(); | |
| 2558 } | |
| 2559 payload_used[payload_type] = true; | |
| 2560 payload_codec_type[payload_type] = in_codec.GetCodecType(); | |
| 2561 | |
| 2562 switch (in_codec.GetCodecType()) { | |
| 2563 case VideoCodec::CODEC_RED: { | |
| 2564 // RED payload type, should not have duplicates. | |
| 2565 RTC_DCHECK(fec_settings.red_payload_type == -1); | |
| 2566 fec_settings.red_payload_type = in_codec.id; | |
| 2567 continue; | |
| 2568 } | |
| 2569 | |
| 2570 case VideoCodec::CODEC_ULPFEC: { | |
| 2571 // ULPFEC payload type, should not have duplicates. | |
| 2572 RTC_DCHECK(fec_settings.ulpfec_payload_type == -1); | |
| 2573 fec_settings.ulpfec_payload_type = in_codec.id; | |
| 2574 continue; | |
| 2575 } | |
| 2576 | |
| 2577 case VideoCodec::CODEC_RTX: { | |
| 2578 int associated_payload_type; | |
| 2579 if (!in_codec.GetParam(kCodecParamAssociatedPayloadType, | |
| 2580 &associated_payload_type) || | |
| 2581 !IsValidRtpPayloadType(associated_payload_type)) { | |
| 2582 LOG(LS_ERROR) | |
| 2583 << "RTX codec with invalid or no associated payload type: " | |
| 2584 << in_codec.ToString(); | |
| 2585 return std::vector<VideoCodecSettings>(); | |
| 2586 } | |
| 2587 rtx_mapping[associated_payload_type] = in_codec.id; | |
| 2588 continue; | |
| 2589 } | |
| 2590 | |
| 2591 case VideoCodec::CODEC_VIDEO: | |
| 2592 break; | |
| 2593 } | |
| 2594 | |
| 2595 video_codecs.push_back(VideoCodecSettings()); | |
| 2596 video_codecs.back().codec = in_codec; | |
| 2597 } | |
| 2598 | |
| 2599 // One of these codecs should have been a video codec. Only having FEC | |
| 2600 // parameters into this code is a logic error. | |
| 2601 RTC_DCHECK(!video_codecs.empty()); | |
| 2602 | |
| 2603 for (std::map<int, int>::const_iterator it = rtx_mapping.begin(); | |
| 2604 it != rtx_mapping.end(); | |
| 2605 ++it) { | |
| 2606 if (!payload_used[it->first]) { | |
| 2607 LOG(LS_ERROR) << "RTX mapped to payload not in codec list."; | |
| 2608 return std::vector<VideoCodecSettings>(); | |
| 2609 } | |
| 2610 if (payload_codec_type[it->first] != VideoCodec::CODEC_VIDEO && | |
| 2611 payload_codec_type[it->first] != VideoCodec::CODEC_RED) { | |
| 2612 LOG(LS_ERROR) << "RTX not mapped to regular video codec or RED codec."; | |
| 2613 return std::vector<VideoCodecSettings>(); | |
| 2614 } | |
| 2615 | |
| 2616 if (it->first == fec_settings.red_payload_type) { | |
| 2617 fec_settings.red_rtx_payload_type = it->second; | |
| 2618 } | |
| 2619 } | |
| 2620 | |
| 2621 for (size_t i = 0; i < video_codecs.size(); ++i) { | |
| 2622 video_codecs[i].fec = fec_settings; | |
| 2623 if (rtx_mapping[video_codecs[i].codec.id] != 0 && | |
| 2624 rtx_mapping[video_codecs[i].codec.id] != | |
| 2625 fec_settings.red_payload_type) { | |
| 2626 video_codecs[i].rtx_payload_type = rtx_mapping[video_codecs[i].codec.id]; | |
| 2627 } | |
| 2628 } | |
| 2629 | |
| 2630 return video_codecs; | |
| 2631 } | |
| 2632 | |
| 2633 } // namespace cricket | |
| 2634 | |
| 2635 #endif // HAVE_WEBRTC_VIDEO | |
| OLD | NEW |