OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. | |
3 * | |
4 * Use of this source code is governed by a BSD-style license | |
5 * that can be found in the LICENSE file in the root of the source | |
6 * tree. An additional intellectual property rights grant can be found | |
7 * in the file PATENTS. All contributing project authors may | |
8 * be found in the AUTHORS file in the root of the source tree. | |
9 */ | |
10 | |
11 #include "webrtc/media/engine/webrtcvideoengine2.h" | |
12 | |
13 #include <stdio.h> | |
14 #include <algorithm> | |
15 #include <set> | |
16 #include <string> | |
17 #include <utility> | |
18 | |
19 #include "webrtc/api/video/i420_buffer.h" | |
20 #include "webrtc/api/video_codecs/video_decoder.h" | |
21 #include "webrtc/api/video_codecs/video_encoder.h" | |
22 #include "webrtc/base/copyonwritebuffer.h" | |
23 #include "webrtc/base/logging.h" | |
24 #include "webrtc/base/stringutils.h" | |
25 #include "webrtc/base/timeutils.h" | |
26 #include "webrtc/base/trace_event.h" | |
27 #include "webrtc/call/call.h" | |
28 #include "webrtc/common_video/h264/profile_level_id.h" | |
29 #include "webrtc/media/engine/constants.h" | |
30 #include "webrtc/media/engine/internalencoderfactory.h" | |
31 #include "webrtc/media/engine/internaldecoderfactory.h" | |
32 #include "webrtc/media/engine/simulcast.h" | |
33 #include "webrtc/media/engine/videoencodersoftwarefallbackwrapper.h" | |
34 #include "webrtc/media/engine/videodecodersoftwarefallbackwrapper.h" | |
35 #include "webrtc/media/engine/webrtcmediaengine.h" | |
36 #include "webrtc/media/engine/webrtcvideoencoderfactory.h" | |
37 #include "webrtc/media/engine/webrtcvoiceengine.h" | |
38 #include "webrtc/modules/video_coding/codecs/vp8/simulcast_encoder_adapter.h" | |
39 #include "webrtc/system_wrappers/include/field_trial.h" | |
40 | |
41 using DegradationPreference = webrtc::VideoSendStream::DegradationPreference; | |
42 | |
43 namespace cricket { | |
44 namespace { | |
45 // If this field trial is enabled, we will enable sending FlexFEC and disable | |
46 // sending ULPFEC whenever the former has been negotiated in the SDPs. | |
47 bool IsFlexfecFieldTrialEnabled() { | |
48 return webrtc::field_trial::IsEnabled("WebRTC-FlexFEC-03"); | |
49 } | |
50 | |
51 // If this field trial is enabled, the "flexfec-03" codec may have been | |
52 // advertised as being supported in the local SDP. That means that we must be | |
53 // ready to receive FlexFEC packets. See internalencoderfactory.cc. | |
54 bool IsFlexfecAdvertisedFieldTrialEnabled() { | |
55 return webrtc::field_trial::IsEnabled("WebRTC-FlexFEC-03-Advertised"); | |
56 } | |
57 | |
58 // If this field trial is enabled, we will report VideoContentType RTP extension | |
59 // in capabilities (thus, it will end up in the default SDP and extension will | |
60 // be sent for all key-frames). | |
61 bool IsVideoContentTypeExtensionFieldTrialEnabled() { | |
62 return webrtc::field_trial::IsEnabled("WebRTC-VideoContentTypeExtension"); | |
63 } | |
64 | |
65 // Wrap cricket::WebRtcVideoEncoderFactory as a webrtc::VideoEncoderFactory. | |
66 class EncoderFactoryAdapter : public webrtc::VideoEncoderFactory { | |
67 public: | |
68 // EncoderFactoryAdapter doesn't take ownership of |factory|, which is owned | |
69 // by e.g. PeerConnectionFactory. | |
70 explicit EncoderFactoryAdapter(cricket::WebRtcVideoEncoderFactory* factory) | |
71 : factory_(factory) {} | |
72 virtual ~EncoderFactoryAdapter() {} | |
73 | |
74 // Implement webrtc::VideoEncoderFactory. | |
75 webrtc::VideoEncoder* Create() override { | |
76 return factory_->CreateVideoEncoder(VideoCodec(kVp8CodecName)); | |
77 } | |
78 | |
79 void Destroy(webrtc::VideoEncoder* encoder) override { | |
80 return factory_->DestroyVideoEncoder(encoder); | |
81 } | |
82 | |
83 private: | |
84 cricket::WebRtcVideoEncoderFactory* const factory_; | |
85 }; | |
86 | |
87 // An encoder factory that wraps Create requests for simulcastable codec types | |
88 // with a webrtc::SimulcastEncoderAdapter. Non simulcastable codec type | |
89 // requests are just passed through to the contained encoder factory. | |
90 class WebRtcSimulcastEncoderFactory | |
91 : public cricket::WebRtcVideoEncoderFactory { | |
92 public: | |
93 // WebRtcSimulcastEncoderFactory doesn't take ownership of |factory|, which is | |
94 // owned by e.g. PeerConnectionFactory. | |
95 explicit WebRtcSimulcastEncoderFactory( | |
96 cricket::WebRtcVideoEncoderFactory* factory) | |
97 : factory_(factory) {} | |
98 | |
99 static bool UseSimulcastEncoderFactory( | |
100 const std::vector<cricket::VideoCodec>& codecs) { | |
101 // If any codec is VP8, use the simulcast factory. If asked to create a | |
102 // non-VP8 codec, we'll just return a contained factory encoder directly. | |
103 for (const auto& codec : codecs) { | |
104 if (CodecNamesEq(codec.name.c_str(), kVp8CodecName)) { | |
105 return true; | |
106 } | |
107 } | |
108 return false; | |
109 } | |
110 | |
111 webrtc::VideoEncoder* CreateVideoEncoder( | |
112 const cricket::VideoCodec& codec) override { | |
113 RTC_DCHECK(factory_ != NULL); | |
114 // If it's a codec type we can simulcast, create a wrapped encoder. | |
115 if (CodecNamesEq(codec.name.c_str(), kVp8CodecName)) { | |
116 return new webrtc::SimulcastEncoderAdapter( | |
117 new EncoderFactoryAdapter(factory_)); | |
118 } | |
119 webrtc::VideoEncoder* encoder = factory_->CreateVideoEncoder(codec); | |
120 if (encoder) { | |
121 non_simulcast_encoders_.push_back(encoder); | |
122 } | |
123 return encoder; | |
124 } | |
125 | |
126 const std::vector<cricket::VideoCodec>& supported_codecs() const override { | |
127 return factory_->supported_codecs(); | |
128 } | |
129 | |
130 bool EncoderTypeHasInternalSource( | |
131 webrtc::VideoCodecType type) const override { | |
132 return factory_->EncoderTypeHasInternalSource(type); | |
133 } | |
134 | |
135 void DestroyVideoEncoder(webrtc::VideoEncoder* encoder) override { | |
136 // Check first to see if the encoder wasn't wrapped in a | |
137 // SimulcastEncoderAdapter. In that case, ask the factory to destroy it. | |
138 if (std::remove(non_simulcast_encoders_.begin(), | |
139 non_simulcast_encoders_.end(), | |
140 encoder) != non_simulcast_encoders_.end()) { | |
141 factory_->DestroyVideoEncoder(encoder); | |
142 return; | |
143 } | |
144 | |
145 // Otherwise, SimulcastEncoderAdapter can be deleted directly, and will call | |
146 // DestroyVideoEncoder on the factory for individual encoder instances. | |
147 delete encoder; | |
148 } | |
149 | |
150 private: | |
151 cricket::WebRtcVideoEncoderFactory* factory_; | |
152 // A list of encoders that were created without being wrapped in a | |
153 // SimulcastEncoderAdapter. | |
154 std::vector<webrtc::VideoEncoder*> non_simulcast_encoders_; | |
155 }; | |
156 | |
157 void AddDefaultFeedbackParams(VideoCodec* codec) { | |
158 codec->AddFeedbackParam(FeedbackParam(kRtcpFbParamCcm, kRtcpFbCcmParamFir)); | |
159 codec->AddFeedbackParam(FeedbackParam(kRtcpFbParamNack, kParamValueEmpty)); | |
160 codec->AddFeedbackParam(FeedbackParam(kRtcpFbParamNack, kRtcpFbNackParamPli)); | |
161 codec->AddFeedbackParam(FeedbackParam(kRtcpFbParamRemb, kParamValueEmpty)); | |
162 codec->AddFeedbackParam( | |
163 FeedbackParam(kRtcpFbParamTransportCc, kParamValueEmpty)); | |
164 } | |
165 | |
166 static std::string CodecVectorToString(const std::vector<VideoCodec>& codecs) { | |
167 std::stringstream out; | |
168 out << '{'; | |
169 for (size_t i = 0; i < codecs.size(); ++i) { | |
170 out << codecs[i].ToString(); | |
171 if (i != codecs.size() - 1) { | |
172 out << ", "; | |
173 } | |
174 } | |
175 out << '}'; | |
176 return out.str(); | |
177 } | |
178 | |
179 static bool ValidateCodecFormats(const std::vector<VideoCodec>& codecs) { | |
180 bool has_video = false; | |
181 for (size_t i = 0; i < codecs.size(); ++i) { | |
182 if (!codecs[i].ValidateCodecFormat()) { | |
183 return false; | |
184 } | |
185 if (codecs[i].GetCodecType() == VideoCodec::CODEC_VIDEO) { | |
186 has_video = true; | |
187 } | |
188 } | |
189 if (!has_video) { | |
190 LOG(LS_ERROR) << "Setting codecs without a video codec is invalid: " | |
191 << CodecVectorToString(codecs); | |
192 return false; | |
193 } | |
194 return true; | |
195 } | |
196 | |
197 static bool ValidateStreamParams(const StreamParams& sp) { | |
198 if (sp.ssrcs.empty()) { | |
199 LOG(LS_ERROR) << "No SSRCs in stream parameters: " << sp.ToString(); | |
200 return false; | |
201 } | |
202 | |
203 std::vector<uint32_t> primary_ssrcs; | |
204 sp.GetPrimarySsrcs(&primary_ssrcs); | |
205 std::vector<uint32_t> rtx_ssrcs; | |
206 sp.GetFidSsrcs(primary_ssrcs, &rtx_ssrcs); | |
207 for (uint32_t rtx_ssrc : rtx_ssrcs) { | |
208 bool rtx_ssrc_present = false; | |
209 for (uint32_t sp_ssrc : sp.ssrcs) { | |
210 if (sp_ssrc == rtx_ssrc) { | |
211 rtx_ssrc_present = true; | |
212 break; | |
213 } | |
214 } | |
215 if (!rtx_ssrc_present) { | |
216 LOG(LS_ERROR) << "RTX SSRC '" << rtx_ssrc | |
217 << "' missing from StreamParams ssrcs: " << sp.ToString(); | |
218 return false; | |
219 } | |
220 } | |
221 if (!rtx_ssrcs.empty() && primary_ssrcs.size() != rtx_ssrcs.size()) { | |
222 LOG(LS_ERROR) | |
223 << "RTX SSRCs exist, but don't cover all SSRCs (unsupported): " | |
224 << sp.ToString(); | |
225 return false; | |
226 } | |
227 | |
228 return true; | |
229 } | |
230 | |
231 // Returns true if the given codec is disallowed from doing simulcast. | |
232 bool IsCodecBlacklistedForSimulcast(const std::string& codec_name) { | |
233 return CodecNamesEq(codec_name, kH264CodecName) || | |
234 CodecNamesEq(codec_name, kVp9CodecName); | |
235 } | |
236 | |
237 // The selected thresholds for QVGA and VGA corresponded to a QP around 10. | |
238 // The change in QP declined above the selected bitrates. | |
239 static int GetMaxDefaultVideoBitrateKbps(int width, int height) { | |
240 if (width * height <= 320 * 240) { | |
241 return 600; | |
242 } else if (width * height <= 640 * 480) { | |
243 return 1700; | |
244 } else if (width * height <= 960 * 540) { | |
245 return 2000; | |
246 } else { | |
247 return 2500; | |
248 } | |
249 } | |
250 | |
251 bool GetVp9LayersFromFieldTrialGroup(int* num_spatial_layers, | |
252 int* num_temporal_layers) { | |
253 std::string group = webrtc::field_trial::FindFullName("WebRTC-SupportVP9SVC"); | |
254 if (group.empty()) | |
255 return false; | |
256 | |
257 if (sscanf(group.c_str(), "EnabledByFlag_%dSL%dTL", num_spatial_layers, | |
258 num_temporal_layers) != 2) { | |
259 return false; | |
260 } | |
261 const int kMaxSpatialLayers = 2; | |
262 if (*num_spatial_layers > kMaxSpatialLayers || *num_spatial_layers < 1) | |
263 return false; | |
264 | |
265 const int kMaxTemporalLayers = 3; | |
266 if (*num_temporal_layers > kMaxTemporalLayers || *num_temporal_layers < 1) | |
267 return false; | |
268 | |
269 return true; | |
270 } | |
271 | |
272 int GetDefaultVp9SpatialLayers() { | |
273 int num_sl; | |
274 int num_tl; | |
275 if (GetVp9LayersFromFieldTrialGroup(&num_sl, &num_tl)) { | |
276 return num_sl; | |
277 } | |
278 return 1; | |
279 } | |
280 | |
281 int GetDefaultVp9TemporalLayers() { | |
282 int num_sl; | |
283 int num_tl; | |
284 if (GetVp9LayersFromFieldTrialGroup(&num_sl, &num_tl)) { | |
285 return num_tl; | |
286 } | |
287 return 1; | |
288 } | |
289 | |
290 class EncoderStreamFactory | |
291 : public webrtc::VideoEncoderConfig::VideoStreamFactoryInterface { | |
292 public: | |
293 EncoderStreamFactory(std::string codec_name, | |
294 int max_qp, | |
295 int max_framerate, | |
296 bool is_screencast, | |
297 bool conference_mode) | |
298 : codec_name_(codec_name), | |
299 max_qp_(max_qp), | |
300 max_framerate_(max_framerate), | |
301 is_screencast_(is_screencast), | |
302 conference_mode_(conference_mode) {} | |
303 | |
304 private: | |
305 std::vector<webrtc::VideoStream> CreateEncoderStreams( | |
306 int width, | |
307 int height, | |
308 const webrtc::VideoEncoderConfig& encoder_config) override { | |
309 if (is_screencast_ && | |
310 (!conference_mode_ || !cricket::UseSimulcastScreenshare())) { | |
311 RTC_DCHECK_EQ(1, encoder_config.number_of_streams); | |
312 } | |
313 if (encoder_config.number_of_streams > 1 || | |
314 (CodecNamesEq(codec_name_, kVp8CodecName) && is_screencast_ && | |
315 conference_mode_)) { | |
316 return GetSimulcastConfig(encoder_config.number_of_streams, width, height, | |
317 encoder_config.max_bitrate_bps, max_qp_, | |
318 max_framerate_, is_screencast_); | |
319 } | |
320 | |
321 // For unset max bitrates set default bitrate for non-simulcast. | |
322 int max_bitrate_bps = | |
323 (encoder_config.max_bitrate_bps > 0) | |
324 ? encoder_config.max_bitrate_bps | |
325 : GetMaxDefaultVideoBitrateKbps(width, height) * 1000; | |
326 | |
327 webrtc::VideoStream stream; | |
328 stream.width = width; | |
329 stream.height = height; | |
330 stream.max_framerate = max_framerate_; | |
331 stream.min_bitrate_bps = kMinVideoBitrateKbps * 1000; | |
332 stream.target_bitrate_bps = stream.max_bitrate_bps = max_bitrate_bps; | |
333 stream.max_qp = max_qp_; | |
334 | |
335 if (CodecNamesEq(codec_name_, kVp9CodecName) && !is_screencast_) { | |
336 stream.temporal_layer_thresholds_bps.resize( | |
337 GetDefaultVp9TemporalLayers() - 1); | |
338 } | |
339 | |
340 std::vector<webrtc::VideoStream> streams; | |
341 streams.push_back(stream); | |
342 return streams; | |
343 } | |
344 | |
345 const std::string codec_name_; | |
346 const int max_qp_; | |
347 const int max_framerate_; | |
348 const bool is_screencast_; | |
349 const bool conference_mode_; | |
350 }; | |
351 | |
352 } // namespace | |
353 | |
354 // Constants defined in webrtc/media/engine/constants.h | |
355 // TODO(pbos): Move these to a separate constants.cc file. | |
356 const int kMinVideoBitrateKbps = 30; | |
357 | |
358 const int kVideoMtu = 1200; | |
359 const int kVideoRtpBufferSize = 65536; | |
360 | |
361 // This constant is really an on/off, lower-level configurable NACK history | |
362 // duration hasn't been implemented. | |
363 static const int kNackHistoryMs = 1000; | |
364 | |
365 static const int kDefaultQpMax = 56; | |
366 | |
367 static const int kDefaultRtcpReceiverReportSsrc = 1; | |
368 | |
369 // Minimum time interval for logging stats. | |
370 static const int64_t kStatsLogIntervalMs = 10000; | |
371 | |
372 static std::vector<VideoCodec> GetSupportedCodecs( | |
373 const WebRtcVideoEncoderFactory* external_encoder_factory); | |
374 | |
375 rtc::scoped_refptr<webrtc::VideoEncoderConfig::EncoderSpecificSettings> | |
376 WebRtcVideoChannel2::WebRtcVideoSendStream::ConfigureVideoEncoderSettings( | |
377 const VideoCodec& codec) { | |
378 RTC_DCHECK_RUN_ON(&thread_checker_); | |
379 bool is_screencast = parameters_.options.is_screencast.value_or(false); | |
380 // No automatic resizing when using simulcast or screencast. | |
381 bool automatic_resize = | |
382 !is_screencast && parameters_.config.rtp.ssrcs.size() == 1; | |
383 bool frame_dropping = !is_screencast; | |
384 bool denoising; | |
385 bool codec_default_denoising = false; | |
386 if (is_screencast) { | |
387 denoising = false; | |
388 } else { | |
389 // Use codec default if video_noise_reduction is unset. | |
390 codec_default_denoising = !parameters_.options.video_noise_reduction; | |
391 denoising = parameters_.options.video_noise_reduction.value_or(false); | |
392 } | |
393 | |
394 if (CodecNamesEq(codec.name, kH264CodecName)) { | |
395 webrtc::VideoCodecH264 h264_settings = | |
396 webrtc::VideoEncoder::GetDefaultH264Settings(); | |
397 h264_settings.frameDroppingOn = frame_dropping; | |
398 return new rtc::RefCountedObject< | |
399 webrtc::VideoEncoderConfig::H264EncoderSpecificSettings>(h264_settings); | |
400 } | |
401 if (CodecNamesEq(codec.name, kVp8CodecName)) { | |
402 webrtc::VideoCodecVP8 vp8_settings = | |
403 webrtc::VideoEncoder::GetDefaultVp8Settings(); | |
404 vp8_settings.automaticResizeOn = automatic_resize; | |
405 // VP8 denoising is enabled by default. | |
406 vp8_settings.denoisingOn = codec_default_denoising ? true : denoising; | |
407 vp8_settings.frameDroppingOn = frame_dropping; | |
408 return new rtc::RefCountedObject< | |
409 webrtc::VideoEncoderConfig::Vp8EncoderSpecificSettings>(vp8_settings); | |
410 } | |
411 if (CodecNamesEq(codec.name, kVp9CodecName)) { | |
412 webrtc::VideoCodecVP9 vp9_settings = | |
413 webrtc::VideoEncoder::GetDefaultVp9Settings(); | |
414 if (is_screencast) { | |
415 // TODO(asapersson): Set to 2 for now since there is a DCHECK in | |
416 // VideoSendStream::ReconfigureVideoEncoder. | |
417 vp9_settings.numberOfSpatialLayers = 2; | |
418 } else { | |
419 vp9_settings.numberOfSpatialLayers = GetDefaultVp9SpatialLayers(); | |
420 } | |
421 // VP9 denoising is disabled by default. | |
422 vp9_settings.denoisingOn = codec_default_denoising ? true : denoising; | |
423 vp9_settings.frameDroppingOn = frame_dropping; | |
424 vp9_settings.automaticResizeOn = automatic_resize; | |
425 return new rtc::RefCountedObject< | |
426 webrtc::VideoEncoderConfig::Vp9EncoderSpecificSettings>(vp9_settings); | |
427 } | |
428 return nullptr; | |
429 } | |
430 | |
431 DefaultUnsignalledSsrcHandler::DefaultUnsignalledSsrcHandler() | |
432 : default_sink_(nullptr) {} | |
433 | |
434 UnsignalledSsrcHandler::Action DefaultUnsignalledSsrcHandler::OnUnsignalledSsrc( | |
435 WebRtcVideoChannel2* channel, | |
436 uint32_t ssrc) { | |
437 rtc::Optional<uint32_t> default_recv_ssrc = | |
438 channel->GetDefaultReceiveStreamSsrc(); | |
439 | |
440 if (default_recv_ssrc) { | |
441 LOG(LS_INFO) << "Destroying old default receive stream for SSRC=" << ssrc | |
442 << "."; | |
443 channel->RemoveRecvStream(*default_recv_ssrc); | |
444 } | |
445 | |
446 StreamParams sp; | |
447 sp.ssrcs.push_back(ssrc); | |
448 LOG(LS_INFO) << "Creating default receive stream for SSRC=" << ssrc << "."; | |
449 if (!channel->AddRecvStream(sp, true)) { | |
450 LOG(LS_WARNING) << "Could not create default receive stream."; | |
451 } | |
452 | |
453 channel->SetSink(ssrc, default_sink_); | |
454 return kDeliverPacket; | |
455 } | |
456 | |
457 rtc::VideoSinkInterface<webrtc::VideoFrame>* | |
458 DefaultUnsignalledSsrcHandler::GetDefaultSink() const { | |
459 return default_sink_; | |
460 } | |
461 | |
462 void DefaultUnsignalledSsrcHandler::SetDefaultSink( | |
463 WebRtcVideoChannel2* channel, | |
464 rtc::VideoSinkInterface<webrtc::VideoFrame>* sink) { | |
465 default_sink_ = sink; | |
466 rtc::Optional<uint32_t> default_recv_ssrc = | |
467 channel->GetDefaultReceiveStreamSsrc(); | |
468 if (default_recv_ssrc) { | |
469 channel->SetSink(*default_recv_ssrc, default_sink_); | |
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 } | |
479 | |
480 WebRtcVideoEngine2::~WebRtcVideoEngine2() { | |
481 LOG(LS_INFO) << "WebRtcVideoEngine2::~WebRtcVideoEngine2"; | |
482 } | |
483 | |
484 void WebRtcVideoEngine2::Init() { | |
485 LOG(LS_INFO) << "WebRtcVideoEngine2::Init"; | |
486 initialized_ = true; | |
487 } | |
488 | |
489 WebRtcVideoChannel2* WebRtcVideoEngine2::CreateChannel( | |
490 webrtc::Call* call, | |
491 const MediaConfig& config, | |
492 const VideoOptions& options) { | |
493 RTC_DCHECK(initialized_); | |
494 LOG(LS_INFO) << "CreateChannel. Options: " << options.ToString(); | |
495 return new WebRtcVideoChannel2(call, config, options, | |
496 external_encoder_factory_, | |
497 external_decoder_factory_); | |
498 } | |
499 | |
500 std::vector<VideoCodec> WebRtcVideoEngine2::codecs() const { | |
501 return GetSupportedCodecs(external_encoder_factory_); | |
502 } | |
503 | |
504 RtpCapabilities WebRtcVideoEngine2::GetCapabilities() const { | |
505 RtpCapabilities capabilities; | |
506 capabilities.header_extensions.push_back( | |
507 webrtc::RtpExtension(webrtc::RtpExtension::kTimestampOffsetUri, | |
508 webrtc::RtpExtension::kTimestampOffsetDefaultId)); | |
509 capabilities.header_extensions.push_back( | |
510 webrtc::RtpExtension(webrtc::RtpExtension::kAbsSendTimeUri, | |
511 webrtc::RtpExtension::kAbsSendTimeDefaultId)); | |
512 capabilities.header_extensions.push_back( | |
513 webrtc::RtpExtension(webrtc::RtpExtension::kVideoRotationUri, | |
514 webrtc::RtpExtension::kVideoRotationDefaultId)); | |
515 capabilities.header_extensions.push_back(webrtc::RtpExtension( | |
516 webrtc::RtpExtension::kTransportSequenceNumberUri, | |
517 webrtc::RtpExtension::kTransportSequenceNumberDefaultId)); | |
518 capabilities.header_extensions.push_back( | |
519 webrtc::RtpExtension(webrtc::RtpExtension::kPlayoutDelayUri, | |
520 webrtc::RtpExtension::kPlayoutDelayDefaultId)); | |
521 if (IsVideoContentTypeExtensionFieldTrialEnabled()) { | |
522 capabilities.header_extensions.push_back( | |
523 webrtc::RtpExtension(webrtc::RtpExtension::kVideoContentTypeUri, | |
524 webrtc::RtpExtension::kVideoContentTypeDefaultId)); | |
525 } | |
526 return capabilities; | |
527 } | |
528 | |
529 void WebRtcVideoEngine2::SetExternalDecoderFactory( | |
530 WebRtcVideoDecoderFactory* decoder_factory) { | |
531 RTC_DCHECK(!initialized_); | |
532 external_decoder_factory_ = decoder_factory; | |
533 } | |
534 | |
535 void WebRtcVideoEngine2::SetExternalEncoderFactory( | |
536 WebRtcVideoEncoderFactory* encoder_factory) { | |
537 RTC_DCHECK(!initialized_); | |
538 if (external_encoder_factory_ == encoder_factory) | |
539 return; | |
540 | |
541 // No matter what happens we shouldn't hold on to a stale | |
542 // WebRtcSimulcastEncoderFactory. | |
543 simulcast_encoder_factory_.reset(); | |
544 | |
545 if (encoder_factory && | |
546 WebRtcSimulcastEncoderFactory::UseSimulcastEncoderFactory( | |
547 encoder_factory->supported_codecs())) { | |
548 simulcast_encoder_factory_.reset( | |
549 new WebRtcSimulcastEncoderFactory(encoder_factory)); | |
550 encoder_factory = simulcast_encoder_factory_.get(); | |
551 } | |
552 external_encoder_factory_ = encoder_factory; | |
553 } | |
554 | |
555 // This is a helper function for AppendVideoCodecs below. It will return the | |
556 // first unused dynamic payload type (in the range [96, 127]), or nothing if no | |
557 // payload type is unused. | |
558 static rtc::Optional<int> NextFreePayloadType( | |
559 const std::vector<VideoCodec>& codecs) { | |
560 static const int kFirstDynamicPayloadType = 96; | |
561 static const int kLastDynamicPayloadType = 127; | |
562 bool is_payload_used[1 + kLastDynamicPayloadType - kFirstDynamicPayloadType] = | |
563 {false}; | |
564 for (const VideoCodec& codec : codecs) { | |
565 if (kFirstDynamicPayloadType <= codec.id && | |
566 codec.id <= kLastDynamicPayloadType) { | |
567 is_payload_used[codec.id - kFirstDynamicPayloadType] = true; | |
568 } | |
569 } | |
570 for (int i = kFirstDynamicPayloadType; i <= kLastDynamicPayloadType; ++i) { | |
571 if (!is_payload_used[i - kFirstDynamicPayloadType]) | |
572 return rtc::Optional<int>(i); | |
573 } | |
574 // No free payload type. | |
575 return rtc::Optional<int>(); | |
576 } | |
577 | |
578 // This is a helper function for GetSupportedCodecs below. It will append new | |
579 // unique codecs from |input_codecs| to |unified_codecs|. It will add default | |
580 // feedback params to the codecs and will also add an associated RTX codec for | |
581 // recognized codecs (VP8, VP9, H264, and RED). | |
582 static void AppendVideoCodecs(const std::vector<VideoCodec>& input_codecs, | |
583 std::vector<VideoCodec>* unified_codecs) { | |
584 for (VideoCodec codec : input_codecs) { | |
585 const rtc::Optional<int> payload_type = | |
586 NextFreePayloadType(*unified_codecs); | |
587 if (!payload_type) | |
588 return; | |
589 codec.id = *payload_type; | |
590 // TODO(magjed): Move the responsibility of setting these parameters to the | |
591 // encoder factories instead. | |
592 if (codec.name != kRedCodecName && codec.name != kUlpfecCodecName && | |
593 codec.name != kFlexfecCodecName) | |
594 AddDefaultFeedbackParams(&codec); | |
595 // Don't add same codec twice. | |
596 if (FindMatchingCodec(*unified_codecs, codec)) | |
597 continue; | |
598 | |
599 unified_codecs->push_back(codec); | |
600 | |
601 // Add associated RTX codec for recognized codecs. | |
602 // TODO(deadbeef): Should we add RTX codecs for external codecs whose names | |
603 // we don't recognize? | |
604 if (CodecNamesEq(codec.name, kVp8CodecName) || | |
605 CodecNamesEq(codec.name, kVp9CodecName) || | |
606 CodecNamesEq(codec.name, kH264CodecName) || | |
607 CodecNamesEq(codec.name, kRedCodecName)) { | |
608 const rtc::Optional<int> rtx_payload_type = | |
609 NextFreePayloadType(*unified_codecs); | |
610 if (!rtx_payload_type) | |
611 return; | |
612 unified_codecs->push_back( | |
613 VideoCodec::CreateRtxCodec(*rtx_payload_type, codec.id)); | |
614 } | |
615 } | |
616 } | |
617 | |
618 static std::vector<VideoCodec> GetSupportedCodecs( | |
619 const WebRtcVideoEncoderFactory* external_encoder_factory) { | |
620 const std::vector<VideoCodec> internal_codecs = | |
621 InternalEncoderFactory().supported_codecs(); | |
622 LOG(LS_INFO) << "Internally supported codecs: " | |
623 << CodecVectorToString(internal_codecs); | |
624 | |
625 std::vector<VideoCodec> unified_codecs; | |
626 AppendVideoCodecs(internal_codecs, &unified_codecs); | |
627 | |
628 if (external_encoder_factory != nullptr) { | |
629 const std::vector<VideoCodec>& external_codecs = | |
630 external_encoder_factory->supported_codecs(); | |
631 AppendVideoCodecs(external_codecs, &unified_codecs); | |
632 LOG(LS_INFO) << "Codecs supported by the external encoder factory: " | |
633 << CodecVectorToString(external_codecs); | |
634 } | |
635 | |
636 return unified_codecs; | |
637 } | |
638 | |
639 WebRtcVideoChannel2::WebRtcVideoChannel2( | |
640 webrtc::Call* call, | |
641 const MediaConfig& config, | |
642 const VideoOptions& options, | |
643 WebRtcVideoEncoderFactory* external_encoder_factory, | |
644 WebRtcVideoDecoderFactory* external_decoder_factory) | |
645 : VideoMediaChannel(config), | |
646 call_(call), | |
647 unsignalled_ssrc_handler_(&default_unsignalled_ssrc_handler_), | |
648 video_config_(config.video), | |
649 external_encoder_factory_(external_encoder_factory), | |
650 external_decoder_factory_(external_decoder_factory), | |
651 default_send_options_(options), | |
652 last_stats_log_ms_(-1) { | |
653 RTC_DCHECK(thread_checker_.CalledOnValidThread()); | |
654 | |
655 rtcp_receiver_report_ssrc_ = kDefaultRtcpReceiverReportSsrc; | |
656 sending_ = false; | |
657 recv_codecs_ = MapCodecs(GetSupportedCodecs(external_encoder_factory)); | |
658 recv_flexfec_payload_type_ = recv_codecs_.front().flexfec_payload_type; | |
659 } | |
660 | |
661 WebRtcVideoChannel2::~WebRtcVideoChannel2() { | |
662 for (auto& kv : send_streams_) | |
663 delete kv.second; | |
664 for (auto& kv : receive_streams_) | |
665 delete kv.second; | |
666 } | |
667 | |
668 rtc::Optional<WebRtcVideoChannel2::VideoCodecSettings> | |
669 WebRtcVideoChannel2::SelectSendVideoCodec( | |
670 const std::vector<VideoCodecSettings>& remote_mapped_codecs) const { | |
671 const std::vector<VideoCodec> local_supported_codecs = | |
672 GetSupportedCodecs(external_encoder_factory_); | |
673 // Select the first remote codec that is supported locally. | |
674 for (const VideoCodecSettings& remote_mapped_codec : remote_mapped_codecs) { | |
675 // For H264, we will limit the encode level to the remote offered level | |
676 // regardless if level asymmetry is allowed or not. This is strictly not | |
677 // following the spec in https://tools.ietf.org/html/rfc6184#section-8.2.2 | |
678 // since we should limit the encode level to the lower of local and remote | |
679 // level when level asymmetry is not allowed. | |
680 if (FindMatchingCodec(local_supported_codecs, remote_mapped_codec.codec)) | |
681 return rtc::Optional<VideoCodecSettings>(remote_mapped_codec); | |
682 } | |
683 // No remote codec was supported. | |
684 return rtc::Optional<VideoCodecSettings>(); | |
685 } | |
686 | |
687 bool WebRtcVideoChannel2::NonFlexfecReceiveCodecsHaveChanged( | |
688 std::vector<VideoCodecSettings> before, | |
689 std::vector<VideoCodecSettings> after) { | |
690 if (before.size() != after.size()) { | |
691 return true; | |
692 } | |
693 | |
694 // The receive codec order doesn't matter, so we sort the codecs before | |
695 // comparing. This is necessary because currently the | |
696 // only way to change the send codec is to munge SDP, which causes | |
697 // the receive codec list to change order, which causes the streams | |
698 // to be recreates which causes a "blink" of black video. In order | |
699 // to support munging the SDP in this way without recreating receive | |
700 // streams, we ignore the order of the received codecs so that | |
701 // changing the order doesn't cause this "blink". | |
702 auto comparison = | |
703 [](const VideoCodecSettings& codec1, const VideoCodecSettings& codec2) { | |
704 return codec1.codec.id > codec2.codec.id; | |
705 }; | |
706 std::sort(before.begin(), before.end(), comparison); | |
707 std::sort(after.begin(), after.end(), comparison); | |
708 | |
709 // Changes in FlexFEC payload type are handled separately in | |
710 // WebRtcVideoChannel2::GetChangedRecvParameters, so disregard FlexFEC in the | |
711 // comparison here. | |
712 return !std::equal(before.begin(), before.end(), after.begin(), | |
713 VideoCodecSettings::EqualsDisregardingFlexfec); | |
714 } | |
715 | |
716 bool WebRtcVideoChannel2::GetChangedSendParameters( | |
717 const VideoSendParameters& params, | |
718 ChangedSendParameters* changed_params) const { | |
719 if (!ValidateCodecFormats(params.codecs) || | |
720 !ValidateRtpExtensions(params.extensions)) { | |
721 return false; | |
722 } | |
723 | |
724 // Select one of the remote codecs that will be used as send codec. | |
725 rtc::Optional<VideoCodecSettings> selected_send_codec = | |
726 SelectSendVideoCodec(MapCodecs(params.codecs)); | |
727 | |
728 if (!selected_send_codec) { | |
729 LOG(LS_ERROR) << "No video codecs supported."; | |
730 return false; | |
731 } | |
732 | |
733 // Never enable sending FlexFEC, unless we are in the experiment. | |
734 if (!IsFlexfecFieldTrialEnabled()) { | |
735 if (selected_send_codec->flexfec_payload_type != -1) { | |
736 LOG(LS_INFO) << "Remote supports flexfec-03, but we will not send since " | |
737 << "WebRTC-FlexFEC-03 field trial is not enabled."; | |
738 } | |
739 selected_send_codec->flexfec_payload_type = -1; | |
740 } | |
741 | |
742 if (!send_codec_ || *selected_send_codec != *send_codec_) | |
743 changed_params->codec = selected_send_codec; | |
744 | |
745 // Handle RTP header extensions. | |
746 std::vector<webrtc::RtpExtension> filtered_extensions = FilterRtpExtensions( | |
747 params.extensions, webrtc::RtpExtension::IsSupportedForVideo, true); | |
748 if (!send_rtp_extensions_ || (*send_rtp_extensions_ != filtered_extensions)) { | |
749 changed_params->rtp_header_extensions = | |
750 rtc::Optional<std::vector<webrtc::RtpExtension>>(filtered_extensions); | |
751 } | |
752 | |
753 // Handle max bitrate. | |
754 if (params.max_bandwidth_bps != send_params_.max_bandwidth_bps && | |
755 params.max_bandwidth_bps >= -1) { | |
756 // 0 or -1 uncaps max bitrate. | |
757 // TODO(pbos): Reconsider how 0 should be treated. It is not mentioned as a | |
758 // special value and might very well be used for stopping sending. | |
759 changed_params->max_bandwidth_bps = rtc::Optional<int>( | |
760 params.max_bandwidth_bps == 0 ? -1 : params.max_bandwidth_bps); | |
761 } | |
762 | |
763 // Handle conference mode. | |
764 if (params.conference_mode != send_params_.conference_mode) { | |
765 changed_params->conference_mode = | |
766 rtc::Optional<bool>(params.conference_mode); | |
767 } | |
768 | |
769 // Handle RTCP mode. | |
770 if (params.rtcp.reduced_size != send_params_.rtcp.reduced_size) { | |
771 changed_params->rtcp_mode = rtc::Optional<webrtc::RtcpMode>( | |
772 params.rtcp.reduced_size ? webrtc::RtcpMode::kReducedSize | |
773 : webrtc::RtcpMode::kCompound); | |
774 } | |
775 | |
776 return true; | |
777 } | |
778 | |
779 rtc::DiffServCodePoint WebRtcVideoChannel2::PreferredDscp() const { | |
780 return rtc::DSCP_AF41; | |
781 } | |
782 | |
783 bool WebRtcVideoChannel2::SetSendParameters(const VideoSendParameters& params) { | |
784 TRACE_EVENT0("webrtc", "WebRtcVideoChannel2::SetSendParameters"); | |
785 LOG(LS_INFO) << "SetSendParameters: " << params.ToString(); | |
786 ChangedSendParameters changed_params; | |
787 if (!GetChangedSendParameters(params, &changed_params)) { | |
788 return false; | |
789 } | |
790 | |
791 if (changed_params.codec) { | |
792 const VideoCodecSettings& codec_settings = *changed_params.codec; | |
793 send_codec_ = rtc::Optional<VideoCodecSettings>(codec_settings); | |
794 LOG(LS_INFO) << "Using codec: " << codec_settings.codec.ToString(); | |
795 } | |
796 | |
797 if (changed_params.rtp_header_extensions) { | |
798 send_rtp_extensions_ = changed_params.rtp_header_extensions; | |
799 } | |
800 | |
801 if (changed_params.codec || changed_params.max_bandwidth_bps) { | |
802 if (params.max_bandwidth_bps == -1) { | |
803 // Unset the global max bitrate (max_bitrate_bps) if max_bandwidth_bps is | |
804 // -1, which corresponds to no "b=AS" attribute in SDP. Note that the | |
805 // global max bitrate may be set below in GetBitrateConfigForCodec, from | |
806 // the codec max bitrate. | |
807 // TODO(pbos): This should be reconsidered (codec max bitrate should | |
808 // probably not affect global call max bitrate). | |
809 bitrate_config_.max_bitrate_bps = -1; | |
810 } | |
811 if (send_codec_) { | |
812 // TODO(holmer): Changing the codec parameters shouldn't necessarily mean | |
813 // that we change the min/max of bandwidth estimation. Reevaluate this. | |
814 bitrate_config_ = GetBitrateConfigForCodec(send_codec_->codec); | |
815 if (!changed_params.codec) { | |
816 // If the codec isn't changing, set the start bitrate to -1 which means | |
817 // "unchanged" so that BWE isn't affected. | |
818 bitrate_config_.start_bitrate_bps = -1; | |
819 } | |
820 } | |
821 if (params.max_bandwidth_bps >= 0) { | |
822 // Note that max_bandwidth_bps intentionally takes priority over the | |
823 // bitrate config for the codec. This allows FEC to be applied above the | |
824 // codec target bitrate. | |
825 // TODO(pbos): Figure out whether b=AS means max bitrate for this | |
826 // WebRtcVideoChannel2 (in which case we're good), or per sender (SSRC), | |
827 // in which case this should not set a Call::BitrateConfig but rather | |
828 // reconfigure all senders. | |
829 bitrate_config_.max_bitrate_bps = | |
830 params.max_bandwidth_bps == 0 ? -1 : params.max_bandwidth_bps; | |
831 } | |
832 call_->SetBitrateConfig(bitrate_config_); | |
833 } | |
834 | |
835 { | |
836 rtc::CritScope stream_lock(&stream_crit_); | |
837 for (auto& kv : send_streams_) { | |
838 kv.second->SetSendParameters(changed_params); | |
839 } | |
840 if (changed_params.codec || changed_params.rtcp_mode) { | |
841 // Update receive feedback parameters from new codec or RTCP mode. | |
842 LOG(LS_INFO) | |
843 << "SetFeedbackOptions on all the receive streams because the send " | |
844 "codec or RTCP mode has changed."; | |
845 for (auto& kv : receive_streams_) { | |
846 RTC_DCHECK(kv.second != nullptr); | |
847 kv.second->SetFeedbackParameters( | |
848 HasNack(send_codec_->codec), HasRemb(send_codec_->codec), | |
849 HasTransportCc(send_codec_->codec), | |
850 params.rtcp.reduced_size ? webrtc::RtcpMode::kReducedSize | |
851 : webrtc::RtcpMode::kCompound); | |
852 } | |
853 } | |
854 } | |
855 send_params_ = params; | |
856 return true; | |
857 } | |
858 | |
859 webrtc::RtpParameters WebRtcVideoChannel2::GetRtpSendParameters( | |
860 uint32_t ssrc) const { | |
861 rtc::CritScope stream_lock(&stream_crit_); | |
862 auto it = send_streams_.find(ssrc); | |
863 if (it == send_streams_.end()) { | |
864 LOG(LS_WARNING) << "Attempting to get RTP send parameters for stream " | |
865 << "with ssrc " << ssrc << " which doesn't exist."; | |
866 return webrtc::RtpParameters(); | |
867 } | |
868 | |
869 webrtc::RtpParameters rtp_params = it->second->GetRtpParameters(); | |
870 // Need to add the common list of codecs to the send stream-specific | |
871 // RTP parameters. | |
872 for (const VideoCodec& codec : send_params_.codecs) { | |
873 rtp_params.codecs.push_back(codec.ToCodecParameters()); | |
874 } | |
875 return rtp_params; | |
876 } | |
877 | |
878 bool WebRtcVideoChannel2::SetRtpSendParameters( | |
879 uint32_t ssrc, | |
880 const webrtc::RtpParameters& parameters) { | |
881 TRACE_EVENT0("webrtc", "WebRtcVideoChannel2::SetRtpSendParameters"); | |
882 rtc::CritScope stream_lock(&stream_crit_); | |
883 auto it = send_streams_.find(ssrc); | |
884 if (it == send_streams_.end()) { | |
885 LOG(LS_ERROR) << "Attempting to set RTP send parameters for stream " | |
886 << "with ssrc " << ssrc << " which doesn't exist."; | |
887 return false; | |
888 } | |
889 | |
890 // TODO(deadbeef): Handle setting parameters with a list of codecs in a | |
891 // different order (which should change the send codec). | |
892 webrtc::RtpParameters current_parameters = GetRtpSendParameters(ssrc); | |
893 if (current_parameters.codecs != parameters.codecs) { | |
894 LOG(LS_ERROR) << "Using SetParameters to change the set of codecs " | |
895 << "is not currently supported."; | |
896 return false; | |
897 } | |
898 | |
899 return it->second->SetRtpParameters(parameters); | |
900 } | |
901 | |
902 webrtc::RtpParameters WebRtcVideoChannel2::GetRtpReceiveParameters( | |
903 uint32_t ssrc) const { | |
904 webrtc::RtpParameters rtp_params; | |
905 rtc::CritScope stream_lock(&stream_crit_); | |
906 // SSRC of 0 represents an unsignaled receive stream. | |
907 if (ssrc == 0) { | |
908 if (!default_unsignalled_ssrc_handler_.GetDefaultSink()) { | |
909 LOG(LS_WARNING) << "Attempting to get RTP parameters for the default, " | |
910 "unsignaled video receive stream, but not yet " | |
911 "configured to receive such a stream."; | |
912 return rtp_params; | |
913 } | |
914 rtp_params.encodings.emplace_back(); | |
915 } else { | |
916 auto it = receive_streams_.find(ssrc); | |
917 if (it == receive_streams_.end()) { | |
918 LOG(LS_WARNING) << "Attempting to get RTP receive parameters for stream " | |
919 << "with SSRC " << ssrc << " which doesn't exist."; | |
920 return webrtc::RtpParameters(); | |
921 } | |
922 // TODO(deadbeef): Return stream-specific parameters, beyond just SSRC. | |
923 rtp_params.encodings.emplace_back(); | |
924 rtp_params.encodings[0].ssrc = it->second->GetFirstPrimarySsrc(); | |
925 } | |
926 | |
927 // Add codecs, which any stream is prepared to receive. | |
928 for (const VideoCodec& codec : recv_params_.codecs) { | |
929 rtp_params.codecs.push_back(codec.ToCodecParameters()); | |
930 } | |
931 return rtp_params; | |
932 } | |
933 | |
934 bool WebRtcVideoChannel2::SetRtpReceiveParameters( | |
935 uint32_t ssrc, | |
936 const webrtc::RtpParameters& parameters) { | |
937 TRACE_EVENT0("webrtc", "WebRtcVideoChannel2::SetRtpReceiveParameters"); | |
938 rtc::CritScope stream_lock(&stream_crit_); | |
939 | |
940 // SSRC of 0 represents an unsignaled receive stream. | |
941 if (ssrc == 0) { | |
942 if (!default_unsignalled_ssrc_handler_.GetDefaultSink()) { | |
943 LOG(LS_WARNING) << "Attempting to set RTP parameters for the default, " | |
944 "unsignaled video receive stream, but not yet " | |
945 "configured to receive such a stream."; | |
946 return false; | |
947 } | |
948 } else { | |
949 auto it = receive_streams_.find(ssrc); | |
950 if (it == receive_streams_.end()) { | |
951 LOG(LS_WARNING) << "Attempting to set RTP receive parameters for stream " | |
952 << "with SSRC " << ssrc << " which doesn't exist."; | |
953 return false; | |
954 } | |
955 } | |
956 | |
957 webrtc::RtpParameters current_parameters = GetRtpReceiveParameters(ssrc); | |
958 if (current_parameters != parameters) { | |
959 LOG(LS_ERROR) << "Changing the RTP receive parameters is currently " | |
960 << "unsupported."; | |
961 return false; | |
962 } | |
963 return true; | |
964 } | |
965 | |
966 bool WebRtcVideoChannel2::GetChangedRecvParameters( | |
967 const VideoRecvParameters& params, | |
968 ChangedRecvParameters* changed_params) const { | |
969 if (!ValidateCodecFormats(params.codecs) || | |
970 !ValidateRtpExtensions(params.extensions)) { | |
971 return false; | |
972 } | |
973 | |
974 // Handle receive codecs. | |
975 const std::vector<VideoCodecSettings> mapped_codecs = | |
976 MapCodecs(params.codecs); | |
977 if (mapped_codecs.empty()) { | |
978 LOG(LS_ERROR) << "SetRecvParameters called without any video codecs."; | |
979 return false; | |
980 } | |
981 | |
982 // Verify that every mapped codec is supported locally. | |
983 const std::vector<VideoCodec> local_supported_codecs = | |
984 GetSupportedCodecs(external_encoder_factory_); | |
985 for (const VideoCodecSettings& mapped_codec : mapped_codecs) { | |
986 if (!FindMatchingCodec(local_supported_codecs, mapped_codec.codec)) { | |
987 LOG(LS_ERROR) << "SetRecvParameters called with unsupported video codec: " | |
988 << mapped_codec.codec.ToString(); | |
989 return false; | |
990 } | |
991 } | |
992 | |
993 if (NonFlexfecReceiveCodecsHaveChanged(recv_codecs_, mapped_codecs)) { | |
994 changed_params->codec_settings = | |
995 rtc::Optional<std::vector<VideoCodecSettings>>(mapped_codecs); | |
996 } | |
997 | |
998 // Handle RTP header extensions. | |
999 std::vector<webrtc::RtpExtension> filtered_extensions = FilterRtpExtensions( | |
1000 params.extensions, webrtc::RtpExtension::IsSupportedForVideo, false); | |
1001 if (filtered_extensions != recv_rtp_extensions_) { | |
1002 changed_params->rtp_header_extensions = | |
1003 rtc::Optional<std::vector<webrtc::RtpExtension>>(filtered_extensions); | |
1004 } | |
1005 | |
1006 int flexfec_payload_type = mapped_codecs.front().flexfec_payload_type; | |
1007 if (flexfec_payload_type != recv_flexfec_payload_type_) { | |
1008 changed_params->flexfec_payload_type = | |
1009 rtc::Optional<int>(flexfec_payload_type); | |
1010 } | |
1011 | |
1012 return true; | |
1013 } | |
1014 | |
1015 bool WebRtcVideoChannel2::SetRecvParameters(const VideoRecvParameters& params) { | |
1016 TRACE_EVENT0("webrtc", "WebRtcVideoChannel2::SetRecvParameters"); | |
1017 LOG(LS_INFO) << "SetRecvParameters: " << params.ToString(); | |
1018 ChangedRecvParameters changed_params; | |
1019 if (!GetChangedRecvParameters(params, &changed_params)) { | |
1020 return false; | |
1021 } | |
1022 if (changed_params.flexfec_payload_type) { | |
1023 LOG(LS_INFO) << "Changing FlexFEC payload type (recv) from " | |
1024 << recv_flexfec_payload_type_ << " to " | |
1025 << *changed_params.flexfec_payload_type; | |
1026 recv_flexfec_payload_type_ = *changed_params.flexfec_payload_type; | |
1027 } | |
1028 if (changed_params.rtp_header_extensions) { | |
1029 recv_rtp_extensions_ = *changed_params.rtp_header_extensions; | |
1030 } | |
1031 if (changed_params.codec_settings) { | |
1032 LOG(LS_INFO) << "Changing recv codecs from " | |
1033 << CodecSettingsVectorToString(recv_codecs_) << " to " | |
1034 << CodecSettingsVectorToString(*changed_params.codec_settings); | |
1035 recv_codecs_ = *changed_params.codec_settings; | |
1036 } | |
1037 | |
1038 { | |
1039 rtc::CritScope stream_lock(&stream_crit_); | |
1040 for (auto& kv : receive_streams_) { | |
1041 kv.second->SetRecvParameters(changed_params); | |
1042 } | |
1043 } | |
1044 recv_params_ = params; | |
1045 return true; | |
1046 } | |
1047 | |
1048 std::string WebRtcVideoChannel2::CodecSettingsVectorToString( | |
1049 const std::vector<VideoCodecSettings>& codecs) { | |
1050 std::stringstream out; | |
1051 out << '{'; | |
1052 for (size_t i = 0; i < codecs.size(); ++i) { | |
1053 out << codecs[i].codec.ToString(); | |
1054 if (i != codecs.size() - 1) { | |
1055 out << ", "; | |
1056 } | |
1057 } | |
1058 out << '}'; | |
1059 return out.str(); | |
1060 } | |
1061 | |
1062 bool WebRtcVideoChannel2::GetSendCodec(VideoCodec* codec) { | |
1063 if (!send_codec_) { | |
1064 LOG(LS_VERBOSE) << "GetSendCodec: No send codec set."; | |
1065 return false; | |
1066 } | |
1067 *codec = send_codec_->codec; | |
1068 return true; | |
1069 } | |
1070 | |
1071 bool WebRtcVideoChannel2::SetSend(bool send) { | |
1072 TRACE_EVENT0("webrtc", "WebRtcVideoChannel2::SetSend"); | |
1073 LOG(LS_VERBOSE) << "SetSend: " << (send ? "true" : "false"); | |
1074 if (send && !send_codec_) { | |
1075 LOG(LS_ERROR) << "SetSend(true) called before setting codec."; | |
1076 return false; | |
1077 } | |
1078 { | |
1079 rtc::CritScope stream_lock(&stream_crit_); | |
1080 for (const auto& kv : send_streams_) { | |
1081 kv.second->SetSend(send); | |
1082 } | |
1083 } | |
1084 sending_ = send; | |
1085 return true; | |
1086 } | |
1087 | |
1088 // TODO(nisse): The enable argument was used for mute logic which has | |
1089 // been moved to VideoBroadcaster. So remove the argument from this | |
1090 // method. | |
1091 bool WebRtcVideoChannel2::SetVideoSend( | |
1092 uint32_t ssrc, | |
1093 bool enable, | |
1094 const VideoOptions* options, | |
1095 rtc::VideoSourceInterface<webrtc::VideoFrame>* source) { | |
1096 TRACE_EVENT0("webrtc", "SetVideoSend"); | |
1097 RTC_DCHECK(ssrc != 0); | |
1098 LOG(LS_INFO) << "SetVideoSend (ssrc= " << ssrc << ", enable = " << enable | |
1099 << ", options: " << (options ? options->ToString() : "nullptr") | |
1100 << ", source = " << (source ? "(source)" : "nullptr") << ")"; | |
1101 | |
1102 rtc::CritScope stream_lock(&stream_crit_); | |
1103 const auto& kv = send_streams_.find(ssrc); | |
1104 if (kv == send_streams_.end()) { | |
1105 // Allow unknown ssrc only if source is null. | |
1106 RTC_CHECK(source == nullptr); | |
1107 LOG(LS_ERROR) << "No sending stream on ssrc " << ssrc; | |
1108 return false; | |
1109 } | |
1110 | |
1111 return kv->second->SetVideoSend(enable, options, source); | |
1112 } | |
1113 | |
1114 bool WebRtcVideoChannel2::ValidateSendSsrcAvailability( | |
1115 const StreamParams& sp) const { | |
1116 for (uint32_t ssrc : sp.ssrcs) { | |
1117 if (send_ssrcs_.find(ssrc) != send_ssrcs_.end()) { | |
1118 LOG(LS_ERROR) << "Send stream with SSRC '" << ssrc << "' already exists."; | |
1119 return false; | |
1120 } | |
1121 } | |
1122 return true; | |
1123 } | |
1124 | |
1125 bool WebRtcVideoChannel2::ValidateReceiveSsrcAvailability( | |
1126 const StreamParams& sp) const { | |
1127 for (uint32_t ssrc : sp.ssrcs) { | |
1128 if (receive_ssrcs_.find(ssrc) != receive_ssrcs_.end()) { | |
1129 LOG(LS_ERROR) << "Receive stream with SSRC '" << ssrc | |
1130 << "' already exists."; | |
1131 return false; | |
1132 } | |
1133 } | |
1134 return true; | |
1135 } | |
1136 | |
1137 bool WebRtcVideoChannel2::AddSendStream(const StreamParams& sp) { | |
1138 LOG(LS_INFO) << "AddSendStream: " << sp.ToString(); | |
1139 if (!ValidateStreamParams(sp)) | |
1140 return false; | |
1141 | |
1142 rtc::CritScope stream_lock(&stream_crit_); | |
1143 | |
1144 if (!ValidateSendSsrcAvailability(sp)) | |
1145 return false; | |
1146 | |
1147 for (uint32_t used_ssrc : sp.ssrcs) | |
1148 send_ssrcs_.insert(used_ssrc); | |
1149 | |
1150 webrtc::VideoSendStream::Config config(this); | |
1151 config.suspend_below_min_bitrate = video_config_.suspend_below_min_bitrate; | |
1152 config.periodic_alr_bandwidth_probing = | |
1153 video_config_.periodic_alr_bandwidth_probing; | |
1154 WebRtcVideoSendStream* stream = new WebRtcVideoSendStream( | |
1155 call_, sp, std::move(config), default_send_options_, | |
1156 external_encoder_factory_, video_config_.enable_cpu_overuse_detection, | |
1157 bitrate_config_.max_bitrate_bps, send_codec_, send_rtp_extensions_, | |
1158 send_params_); | |
1159 | |
1160 uint32_t ssrc = sp.first_ssrc(); | |
1161 RTC_DCHECK(ssrc != 0); | |
1162 send_streams_[ssrc] = stream; | |
1163 | |
1164 if (rtcp_receiver_report_ssrc_ == kDefaultRtcpReceiverReportSsrc) { | |
1165 rtcp_receiver_report_ssrc_ = ssrc; | |
1166 LOG(LS_INFO) << "SetLocalSsrc on all the receive streams because we added " | |
1167 "a send stream."; | |
1168 for (auto& kv : receive_streams_) | |
1169 kv.second->SetLocalSsrc(ssrc); | |
1170 } | |
1171 if (sending_) { | |
1172 stream->SetSend(true); | |
1173 } | |
1174 | |
1175 return true; | |
1176 } | |
1177 | |
1178 bool WebRtcVideoChannel2::RemoveSendStream(uint32_t ssrc) { | |
1179 LOG(LS_INFO) << "RemoveSendStream: " << ssrc; | |
1180 | |
1181 WebRtcVideoSendStream* removed_stream; | |
1182 { | |
1183 rtc::CritScope stream_lock(&stream_crit_); | |
1184 std::map<uint32_t, WebRtcVideoSendStream*>::iterator it = | |
1185 send_streams_.find(ssrc); | |
1186 if (it == send_streams_.end()) { | |
1187 return false; | |
1188 } | |
1189 | |
1190 for (uint32_t old_ssrc : it->second->GetSsrcs()) | |
1191 send_ssrcs_.erase(old_ssrc); | |
1192 | |
1193 removed_stream = it->second; | |
1194 send_streams_.erase(it); | |
1195 | |
1196 // Switch receiver report SSRCs, the one in use is no longer valid. | |
1197 if (rtcp_receiver_report_ssrc_ == ssrc) { | |
1198 rtcp_receiver_report_ssrc_ = send_streams_.empty() | |
1199 ? kDefaultRtcpReceiverReportSsrc | |
1200 : send_streams_.begin()->first; | |
1201 LOG(LS_INFO) << "SetLocalSsrc on all the receive streams because the " | |
1202 "previous local SSRC was removed."; | |
1203 | |
1204 for (auto& kv : receive_streams_) { | |
1205 kv.second->SetLocalSsrc(rtcp_receiver_report_ssrc_); | |
1206 } | |
1207 } | |
1208 } | |
1209 | |
1210 delete removed_stream; | |
1211 | |
1212 return true; | |
1213 } | |
1214 | |
1215 void WebRtcVideoChannel2::DeleteReceiveStream( | |
1216 WebRtcVideoChannel2::WebRtcVideoReceiveStream* stream) { | |
1217 for (uint32_t old_ssrc : stream->GetSsrcs()) | |
1218 receive_ssrcs_.erase(old_ssrc); | |
1219 delete stream; | |
1220 } | |
1221 | |
1222 bool WebRtcVideoChannel2::AddRecvStream(const StreamParams& sp) { | |
1223 return AddRecvStream(sp, false); | |
1224 } | |
1225 | |
1226 bool WebRtcVideoChannel2::AddRecvStream(const StreamParams& sp, | |
1227 bool default_stream) { | |
1228 RTC_DCHECK(thread_checker_.CalledOnValidThread()); | |
1229 | |
1230 LOG(LS_INFO) << "AddRecvStream" << (default_stream ? " (default stream)" : "") | |
1231 << ": " << sp.ToString(); | |
1232 if (!ValidateStreamParams(sp)) | |
1233 return false; | |
1234 | |
1235 uint32_t ssrc = sp.first_ssrc(); | |
1236 RTC_DCHECK(ssrc != 0); // TODO(pbos): Is this ever valid? | |
1237 | |
1238 rtc::CritScope stream_lock(&stream_crit_); | |
1239 // Remove running stream if this was a default stream. | |
1240 const auto& prev_stream = receive_streams_.find(ssrc); | |
1241 if (prev_stream != receive_streams_.end()) { | |
1242 if (default_stream || !prev_stream->second->IsDefaultStream()) { | |
1243 LOG(LS_ERROR) << "Receive stream for SSRC '" << ssrc | |
1244 << "' already exists."; | |
1245 return false; | |
1246 } | |
1247 DeleteReceiveStream(prev_stream->second); | |
1248 receive_streams_.erase(prev_stream); | |
1249 } | |
1250 | |
1251 if (!ValidateReceiveSsrcAvailability(sp)) | |
1252 return false; | |
1253 | |
1254 for (uint32_t used_ssrc : sp.ssrcs) | |
1255 receive_ssrcs_.insert(used_ssrc); | |
1256 | |
1257 webrtc::VideoReceiveStream::Config config(this); | |
1258 webrtc::FlexfecReceiveStream::Config flexfec_config(this); | |
1259 ConfigureReceiverRtp(&config, &flexfec_config, sp); | |
1260 | |
1261 config.disable_prerenderer_smoothing = | |
1262 video_config_.disable_prerenderer_smoothing; | |
1263 config.sync_group = sp.sync_label; | |
1264 | |
1265 receive_streams_[ssrc] = new WebRtcVideoReceiveStream( | |
1266 call_, sp, std::move(config), external_decoder_factory_, default_stream, | |
1267 recv_codecs_, flexfec_config); | |
1268 | |
1269 return true; | |
1270 } | |
1271 | |
1272 void WebRtcVideoChannel2::ConfigureReceiverRtp( | |
1273 webrtc::VideoReceiveStream::Config* config, | |
1274 webrtc::FlexfecReceiveStream::Config* flexfec_config, | |
1275 const StreamParams& sp) const { | |
1276 uint32_t ssrc = sp.first_ssrc(); | |
1277 | |
1278 config->rtp.remote_ssrc = ssrc; | |
1279 config->rtp.local_ssrc = rtcp_receiver_report_ssrc_; | |
1280 | |
1281 // TODO(pbos): This protection is against setting the same local ssrc as | |
1282 // remote which is not permitted by the lower-level API. RTCP requires a | |
1283 // corresponding sender SSRC. Figure out what to do when we don't have | |
1284 // (receive-only) or know a good local SSRC. | |
1285 if (config->rtp.remote_ssrc == config->rtp.local_ssrc) { | |
1286 if (config->rtp.local_ssrc != kDefaultRtcpReceiverReportSsrc) { | |
1287 config->rtp.local_ssrc = kDefaultRtcpReceiverReportSsrc; | |
1288 } else { | |
1289 config->rtp.local_ssrc = kDefaultRtcpReceiverReportSsrc + 1; | |
1290 } | |
1291 } | |
1292 | |
1293 // Whether or not the receive stream sends reduced size RTCP is determined | |
1294 // by the send params. | |
1295 // TODO(deadbeef): Once we change "send_params" to "sender_params" and | |
1296 // "recv_params" to "receiver_params", we should get this out of | |
1297 // receiver_params_. | |
1298 config->rtp.rtcp_mode = send_params_.rtcp.reduced_size | |
1299 ? webrtc::RtcpMode::kReducedSize | |
1300 : webrtc::RtcpMode::kCompound; | |
1301 | |
1302 config->rtp.remb = send_codec_ ? HasRemb(send_codec_->codec) : false; | |
1303 config->rtp.transport_cc = | |
1304 send_codec_ ? HasTransportCc(send_codec_->codec) : false; | |
1305 | |
1306 sp.GetFidSsrc(ssrc, &config->rtp.rtx_ssrc); | |
1307 | |
1308 config->rtp.extensions = recv_rtp_extensions_; | |
1309 | |
1310 // TODO(brandtr): Generalize when we add support for multistream protection. | |
1311 flexfec_config->payload_type = recv_flexfec_payload_type_; | |
1312 if (IsFlexfecAdvertisedFieldTrialEnabled() && | |
1313 sp.GetFecFrSsrc(ssrc, &flexfec_config->remote_ssrc)) { | |
1314 flexfec_config->protected_media_ssrcs = {ssrc}; | |
1315 flexfec_config->local_ssrc = config->rtp.local_ssrc; | |
1316 flexfec_config->rtcp_mode = config->rtp.rtcp_mode; | |
1317 // TODO(brandtr): We should be spec-compliant and set |transport_cc| here | |
1318 // based on the rtcp-fb for the FlexFEC codec, not the media codec. | |
1319 flexfec_config->transport_cc = config->rtp.transport_cc; | |
1320 flexfec_config->rtp_header_extensions = config->rtp.extensions; | |
1321 } | |
1322 } | |
1323 | |
1324 bool WebRtcVideoChannel2::RemoveRecvStream(uint32_t ssrc) { | |
1325 LOG(LS_INFO) << "RemoveRecvStream: " << ssrc; | |
1326 if (ssrc == 0) { | |
1327 LOG(LS_ERROR) << "RemoveRecvStream with 0 ssrc is not supported."; | |
1328 return false; | |
1329 } | |
1330 | |
1331 rtc::CritScope stream_lock(&stream_crit_); | |
1332 std::map<uint32_t, WebRtcVideoReceiveStream*>::iterator stream = | |
1333 receive_streams_.find(ssrc); | |
1334 if (stream == receive_streams_.end()) { | |
1335 LOG(LS_ERROR) << "Stream not found for ssrc: " << ssrc; | |
1336 return false; | |
1337 } | |
1338 DeleteReceiveStream(stream->second); | |
1339 receive_streams_.erase(stream); | |
1340 | |
1341 return true; | |
1342 } | |
1343 | |
1344 bool WebRtcVideoChannel2::SetSink( | |
1345 uint32_t ssrc, | |
1346 rtc::VideoSinkInterface<webrtc::VideoFrame>* sink) { | |
1347 LOG(LS_INFO) << "SetSink: ssrc:" << ssrc << " " | |
1348 << (sink ? "(ptr)" : "nullptr"); | |
1349 if (ssrc == 0) { | |
1350 // Do not hold |stream_crit_| here, since SetDefaultSink will call | |
1351 // WebRtcVideoChannel2::GetDefaultReceiveStreamSsrc(). | |
1352 default_unsignalled_ssrc_handler_.SetDefaultSink(this, sink); | |
1353 return true; | |
1354 } | |
1355 | |
1356 rtc::CritScope stream_lock(&stream_crit_); | |
1357 std::map<uint32_t, WebRtcVideoReceiveStream*>::iterator it = | |
1358 receive_streams_.find(ssrc); | |
1359 if (it == receive_streams_.end()) { | |
1360 return false; | |
1361 } | |
1362 | |
1363 it->second->SetSink(sink); | |
1364 return true; | |
1365 } | |
1366 | |
1367 bool WebRtcVideoChannel2::GetStats(VideoMediaInfo* info) { | |
1368 TRACE_EVENT0("webrtc", "WebRtcVideoChannel2::GetStats"); | |
1369 | |
1370 // Log stats periodically. | |
1371 bool log_stats = false; | |
1372 int64_t now_ms = rtc::TimeMillis(); | |
1373 if (last_stats_log_ms_ == -1 || | |
1374 now_ms - last_stats_log_ms_ > kStatsLogIntervalMs) { | |
1375 last_stats_log_ms_ = now_ms; | |
1376 log_stats = true; | |
1377 } | |
1378 | |
1379 info->Clear(); | |
1380 FillSenderStats(info, log_stats); | |
1381 FillReceiverStats(info, log_stats); | |
1382 FillSendAndReceiveCodecStats(info); | |
1383 // TODO(holmer): We should either have rtt available as a metric on | |
1384 // VideoSend/ReceiveStreams, or we should remove rtt from VideoSenderInfo. | |
1385 webrtc::Call::Stats stats = call_->GetStats(); | |
1386 if (stats.rtt_ms != -1) { | |
1387 for (size_t i = 0; i < info->senders.size(); ++i) { | |
1388 info->senders[i].rtt_ms = stats.rtt_ms; | |
1389 } | |
1390 } | |
1391 | |
1392 if (log_stats) | |
1393 LOG(LS_INFO) << stats.ToString(now_ms); | |
1394 | |
1395 return true; | |
1396 } | |
1397 | |
1398 void WebRtcVideoChannel2::FillSenderStats(VideoMediaInfo* video_media_info, | |
1399 bool log_stats) { | |
1400 rtc::CritScope stream_lock(&stream_crit_); | |
1401 for (std::map<uint32_t, WebRtcVideoSendStream*>::iterator it = | |
1402 send_streams_.begin(); | |
1403 it != send_streams_.end(); ++it) { | |
1404 video_media_info->senders.push_back( | |
1405 it->second->GetVideoSenderInfo(log_stats)); | |
1406 } | |
1407 } | |
1408 | |
1409 void WebRtcVideoChannel2::FillReceiverStats(VideoMediaInfo* video_media_info, | |
1410 bool log_stats) { | |
1411 rtc::CritScope stream_lock(&stream_crit_); | |
1412 for (std::map<uint32_t, WebRtcVideoReceiveStream*>::iterator it = | |
1413 receive_streams_.begin(); | |
1414 it != receive_streams_.end(); ++it) { | |
1415 video_media_info->receivers.push_back( | |
1416 it->second->GetVideoReceiverInfo(log_stats)); | |
1417 } | |
1418 } | |
1419 | |
1420 void WebRtcVideoChannel2::FillBitrateInfo(BandwidthEstimationInfo* bwe_info) { | |
1421 rtc::CritScope stream_lock(&stream_crit_); | |
1422 for (std::map<uint32_t, WebRtcVideoSendStream*>::iterator stream = | |
1423 send_streams_.begin(); | |
1424 stream != send_streams_.end(); ++stream) { | |
1425 stream->second->FillBitrateInfo(bwe_info); | |
1426 } | |
1427 } | |
1428 | |
1429 void WebRtcVideoChannel2::FillSendAndReceiveCodecStats( | |
1430 VideoMediaInfo* video_media_info) { | |
1431 for (const VideoCodec& codec : send_params_.codecs) { | |
1432 webrtc::RtpCodecParameters codec_params = codec.ToCodecParameters(); | |
1433 video_media_info->send_codecs.insert( | |
1434 std::make_pair(codec_params.payload_type, std::move(codec_params))); | |
1435 } | |
1436 for (const VideoCodec& codec : recv_params_.codecs) { | |
1437 webrtc::RtpCodecParameters codec_params = codec.ToCodecParameters(); | |
1438 video_media_info->receive_codecs.insert( | |
1439 std::make_pair(codec_params.payload_type, std::move(codec_params))); | |
1440 } | |
1441 } | |
1442 | |
1443 void WebRtcVideoChannel2::OnPacketReceived( | |
1444 rtc::CopyOnWriteBuffer* packet, | |
1445 const rtc::PacketTime& packet_time) { | |
1446 const webrtc::PacketTime webrtc_packet_time(packet_time.timestamp, | |
1447 packet_time.not_before); | |
1448 const webrtc::PacketReceiver::DeliveryStatus delivery_result = | |
1449 call_->Receiver()->DeliverPacket( | |
1450 webrtc::MediaType::VIDEO, | |
1451 packet->cdata(), packet->size(), | |
1452 webrtc_packet_time); | |
1453 switch (delivery_result) { | |
1454 case webrtc::PacketReceiver::DELIVERY_OK: | |
1455 return; | |
1456 case webrtc::PacketReceiver::DELIVERY_PACKET_ERROR: | |
1457 return; | |
1458 case webrtc::PacketReceiver::DELIVERY_UNKNOWN_SSRC: | |
1459 break; | |
1460 } | |
1461 | |
1462 uint32_t ssrc = 0; | |
1463 if (!GetRtpSsrc(packet->cdata(), packet->size(), &ssrc)) { | |
1464 return; | |
1465 } | |
1466 | |
1467 int payload_type = 0; | |
1468 if (!GetRtpPayloadType(packet->cdata(), packet->size(), &payload_type)) { | |
1469 return; | |
1470 } | |
1471 | |
1472 // See if this payload_type is registered as one that usually gets its own | |
1473 // SSRC (RTX) or at least is safe to drop either way (FEC). If it is, and | |
1474 // it wasn't handled above by DeliverPacket, that means we don't know what | |
1475 // stream it associates with, and we shouldn't ever create an implicit channel | |
1476 // for these. | |
1477 for (auto& codec : recv_codecs_) { | |
1478 if (payload_type == codec.rtx_payload_type || | |
1479 payload_type == codec.ulpfec.red_rtx_payload_type || | |
1480 payload_type == codec.ulpfec.ulpfec_payload_type) { | |
1481 return; | |
1482 } | |
1483 } | |
1484 if (payload_type == recv_flexfec_payload_type_) { | |
1485 return; | |
1486 } | |
1487 | |
1488 switch (unsignalled_ssrc_handler_->OnUnsignalledSsrc(this, ssrc)) { | |
1489 case UnsignalledSsrcHandler::kDropPacket: | |
1490 return; | |
1491 case UnsignalledSsrcHandler::kDeliverPacket: | |
1492 break; | |
1493 } | |
1494 | |
1495 if (call_->Receiver()->DeliverPacket( | |
1496 webrtc::MediaType::VIDEO, | |
1497 packet->cdata(), packet->size(), | |
1498 webrtc_packet_time) != webrtc::PacketReceiver::DELIVERY_OK) { | |
1499 LOG(LS_WARNING) << "Failed to deliver RTP packet on re-delivery."; | |
1500 return; | |
1501 } | |
1502 } | |
1503 | |
1504 void WebRtcVideoChannel2::OnRtcpReceived( | |
1505 rtc::CopyOnWriteBuffer* packet, | |
1506 const rtc::PacketTime& packet_time) { | |
1507 const webrtc::PacketTime webrtc_packet_time(packet_time.timestamp, | |
1508 packet_time.not_before); | |
1509 // TODO(pbos): Check webrtc::PacketReceiver::DELIVERY_OK once we deliver | |
1510 // for both audio and video on the same path. Since BundleFilter doesn't | |
1511 // filter RTCP anymore incoming RTCP packets could've been going to audio (so | |
1512 // logging failures spam the log). | |
1513 call_->Receiver()->DeliverPacket( | |
1514 webrtc::MediaType::VIDEO, | |
1515 packet->cdata(), packet->size(), | |
1516 webrtc_packet_time); | |
1517 } | |
1518 | |
1519 void WebRtcVideoChannel2::OnReadyToSend(bool ready) { | |
1520 LOG(LS_VERBOSE) << "OnReadyToSend: " << (ready ? "Ready." : "Not ready."); | |
1521 call_->SignalChannelNetworkState( | |
1522 webrtc::MediaType::VIDEO, | |
1523 ready ? webrtc::kNetworkUp : webrtc::kNetworkDown); | |
1524 } | |
1525 | |
1526 void WebRtcVideoChannel2::OnNetworkRouteChanged( | |
1527 const std::string& transport_name, | |
1528 const rtc::NetworkRoute& network_route) { | |
1529 call_->OnNetworkRouteChanged(transport_name, network_route); | |
1530 } | |
1531 | |
1532 void WebRtcVideoChannel2::OnTransportOverheadChanged( | |
1533 int transport_overhead_per_packet) { | |
1534 call_->OnTransportOverheadChanged(webrtc::MediaType::VIDEO, | |
1535 transport_overhead_per_packet); | |
1536 } | |
1537 | |
1538 void WebRtcVideoChannel2::SetInterface(NetworkInterface* iface) { | |
1539 MediaChannel::SetInterface(iface); | |
1540 // Set the RTP recv/send buffer to a bigger size | |
1541 MediaChannel::SetOption(NetworkInterface::ST_RTP, | |
1542 rtc::Socket::OPT_RCVBUF, | |
1543 kVideoRtpBufferSize); | |
1544 | |
1545 // Speculative change to increase the outbound socket buffer size. | |
1546 // In b/15152257, we are seeing a significant number of packets discarded | |
1547 // due to lack of socket buffer space, although it's not yet clear what the | |
1548 // ideal value should be. | |
1549 MediaChannel::SetOption(NetworkInterface::ST_RTP, | |
1550 rtc::Socket::OPT_SNDBUF, | |
1551 kVideoRtpBufferSize); | |
1552 } | |
1553 | |
1554 rtc::Optional<uint32_t> WebRtcVideoChannel2::GetDefaultReceiveStreamSsrc() { | |
1555 rtc::CritScope stream_lock(&stream_crit_); | |
1556 rtc::Optional<uint32_t> ssrc; | |
1557 for (auto it = receive_streams_.begin(); it != receive_streams_.end(); ++it) { | |
1558 if (it->second->IsDefaultStream()) { | |
1559 ssrc.emplace(it->first); | |
1560 break; | |
1561 } | |
1562 } | |
1563 return ssrc; | |
1564 } | |
1565 | |
1566 bool WebRtcVideoChannel2::SendRtp(const uint8_t* data, | |
1567 size_t len, | |
1568 const webrtc::PacketOptions& options) { | |
1569 rtc::CopyOnWriteBuffer packet(data, len, kMaxRtpPacketLen); | |
1570 rtc::PacketOptions rtc_options; | |
1571 rtc_options.packet_id = options.packet_id; | |
1572 return MediaChannel::SendPacket(&packet, rtc_options); | |
1573 } | |
1574 | |
1575 bool WebRtcVideoChannel2::SendRtcp(const uint8_t* data, size_t len) { | |
1576 rtc::CopyOnWriteBuffer packet(data, len, kMaxRtpPacketLen); | |
1577 return MediaChannel::SendRtcp(&packet, rtc::PacketOptions()); | |
1578 } | |
1579 | |
1580 WebRtcVideoChannel2::WebRtcVideoSendStream::VideoSendStreamParameters:: | |
1581 VideoSendStreamParameters( | |
1582 webrtc::VideoSendStream::Config config, | |
1583 const VideoOptions& options, | |
1584 int max_bitrate_bps, | |
1585 const rtc::Optional<VideoCodecSettings>& codec_settings) | |
1586 : config(std::move(config)), | |
1587 options(options), | |
1588 max_bitrate_bps(max_bitrate_bps), | |
1589 conference_mode(false), | |
1590 codec_settings(codec_settings) {} | |
1591 | |
1592 WebRtcVideoChannel2::WebRtcVideoSendStream::AllocatedEncoder::AllocatedEncoder( | |
1593 webrtc::VideoEncoder* encoder, | |
1594 const cricket::VideoCodec& codec, | |
1595 bool external) | |
1596 : encoder(encoder), | |
1597 external_encoder(nullptr), | |
1598 codec(codec), | |
1599 external(external) { | |
1600 if (external) { | |
1601 external_encoder = encoder; | |
1602 this->encoder = | |
1603 new webrtc::VideoEncoderSoftwareFallbackWrapper(codec, encoder); | |
1604 } | |
1605 } | |
1606 | |
1607 WebRtcVideoChannel2::WebRtcVideoSendStream::WebRtcVideoSendStream( | |
1608 webrtc::Call* call, | |
1609 const StreamParams& sp, | |
1610 webrtc::VideoSendStream::Config config, | |
1611 const VideoOptions& options, | |
1612 WebRtcVideoEncoderFactory* external_encoder_factory, | |
1613 bool enable_cpu_overuse_detection, | |
1614 int max_bitrate_bps, | |
1615 const rtc::Optional<VideoCodecSettings>& codec_settings, | |
1616 const rtc::Optional<std::vector<webrtc::RtpExtension>>& rtp_extensions, | |
1617 // TODO(deadbeef): Don't duplicate information between send_params, | |
1618 // rtp_extensions, options, etc. | |
1619 const VideoSendParameters& send_params) | |
1620 : worker_thread_(rtc::Thread::Current()), | |
1621 ssrcs_(sp.ssrcs), | |
1622 ssrc_groups_(sp.ssrc_groups), | |
1623 call_(call), | |
1624 enable_cpu_overuse_detection_(enable_cpu_overuse_detection), | |
1625 source_(nullptr), | |
1626 external_encoder_factory_(external_encoder_factory), | |
1627 internal_encoder_factory_(new InternalEncoderFactory()), | |
1628 stream_(nullptr), | |
1629 encoder_sink_(nullptr), | |
1630 parameters_(std::move(config), options, max_bitrate_bps, codec_settings), | |
1631 rtp_parameters_(CreateRtpParametersWithOneEncoding()), | |
1632 allocated_encoder_(nullptr, cricket::VideoCodec(), false), | |
1633 sending_(false) { | |
1634 parameters_.config.rtp.max_packet_size = kVideoMtu; | |
1635 parameters_.conference_mode = send_params.conference_mode; | |
1636 | |
1637 sp.GetPrimarySsrcs(¶meters_.config.rtp.ssrcs); | |
1638 | |
1639 // ValidateStreamParams should prevent this from happening. | |
1640 RTC_CHECK(!parameters_.config.rtp.ssrcs.empty()); | |
1641 rtp_parameters_.encodings[0].ssrc = | |
1642 rtc::Optional<uint32_t>(parameters_.config.rtp.ssrcs[0]); | |
1643 | |
1644 // RTX. | |
1645 sp.GetFidSsrcs(parameters_.config.rtp.ssrcs, | |
1646 ¶meters_.config.rtp.rtx.ssrcs); | |
1647 | |
1648 // FlexFEC SSRCs. | |
1649 // TODO(brandtr): This code needs to be generalized when we add support for | |
1650 // multistream protection. | |
1651 if (IsFlexfecFieldTrialEnabled()) { | |
1652 uint32_t flexfec_ssrc; | |
1653 bool flexfec_enabled = false; | |
1654 for (uint32_t primary_ssrc : parameters_.config.rtp.ssrcs) { | |
1655 if (sp.GetFecFrSsrc(primary_ssrc, &flexfec_ssrc)) { | |
1656 if (flexfec_enabled) { | |
1657 LOG(LS_INFO) << "Multiple FlexFEC streams in local SDP, but " | |
1658 "our implementation only supports a single FlexFEC " | |
1659 "stream. Will not enable FlexFEC for proposed " | |
1660 "stream with SSRC: " | |
1661 << flexfec_ssrc << "."; | |
1662 continue; | |
1663 } | |
1664 | |
1665 flexfec_enabled = true; | |
1666 parameters_.config.rtp.flexfec.ssrc = flexfec_ssrc; | |
1667 parameters_.config.rtp.flexfec.protected_media_ssrcs = {primary_ssrc}; | |
1668 } | |
1669 } | |
1670 } | |
1671 | |
1672 parameters_.config.rtp.c_name = sp.cname; | |
1673 if (rtp_extensions) { | |
1674 parameters_.config.rtp.extensions = *rtp_extensions; | |
1675 } | |
1676 parameters_.config.rtp.rtcp_mode = send_params.rtcp.reduced_size | |
1677 ? webrtc::RtcpMode::kReducedSize | |
1678 : webrtc::RtcpMode::kCompound; | |
1679 if (codec_settings) { | |
1680 bool force_encoder_allocation = false; | |
1681 SetCodec(*codec_settings, force_encoder_allocation); | |
1682 } | |
1683 } | |
1684 | |
1685 WebRtcVideoChannel2::WebRtcVideoSendStream::~WebRtcVideoSendStream() { | |
1686 if (stream_ != NULL) { | |
1687 call_->DestroyVideoSendStream(stream_); | |
1688 } | |
1689 DestroyVideoEncoder(&allocated_encoder_); | |
1690 } | |
1691 | |
1692 bool WebRtcVideoChannel2::WebRtcVideoSendStream::SetVideoSend( | |
1693 bool enable, | |
1694 const VideoOptions* options, | |
1695 rtc::VideoSourceInterface<webrtc::VideoFrame>* source) { | |
1696 TRACE_EVENT0("webrtc", "WebRtcVideoSendStream::SetVideoSend"); | |
1697 RTC_DCHECK_RUN_ON(&thread_checker_); | |
1698 | |
1699 // Ignore |options| pointer if |enable| is false. | |
1700 bool options_present = enable && options; | |
1701 | |
1702 if (options_present) { | |
1703 VideoOptions old_options = parameters_.options; | |
1704 parameters_.options.SetAll(*options); | |
1705 if (parameters_.options.is_screencast.value_or(false) != | |
1706 old_options.is_screencast.value_or(false) && | |
1707 parameters_.codec_settings) { | |
1708 // If screen content settings change, we may need to recreate the codec | |
1709 // instance so that the correct type is used. | |
1710 | |
1711 bool force_encoder_allocation = true; | |
1712 SetCodec(*parameters_.codec_settings, force_encoder_allocation); | |
1713 // Mark screenshare parameter as being updated, then test for any other | |
1714 // changes that may require codec reconfiguration. | |
1715 old_options.is_screencast = options->is_screencast; | |
1716 } | |
1717 if (parameters_.options != old_options) { | |
1718 ReconfigureEncoder(); | |
1719 } | |
1720 } | |
1721 | |
1722 if (source_ && stream_) { | |
1723 stream_->SetSource(nullptr, DegradationPreference::kDegradationDisabled); | |
1724 } | |
1725 // Switch to the new source. | |
1726 source_ = source; | |
1727 if (source && stream_) { | |
1728 stream_->SetSource(this, GetDegradationPreference()); | |
1729 } | |
1730 return true; | |
1731 } | |
1732 | |
1733 webrtc::VideoSendStream::DegradationPreference | |
1734 WebRtcVideoChannel2::WebRtcVideoSendStream::GetDegradationPreference() const { | |
1735 // Do not adapt resolution for screen content as this will likely | |
1736 // result in blurry and unreadable text. | |
1737 // |this| acts like a VideoSource to make sure SinkWants are handled on the | |
1738 // correct thread. | |
1739 DegradationPreference degradation_preference; | |
1740 if (!enable_cpu_overuse_detection_) { | |
1741 degradation_preference = DegradationPreference::kDegradationDisabled; | |
1742 } else { | |
1743 if (parameters_.options.is_screencast.value_or(false)) { | |
1744 degradation_preference = DegradationPreference::kMaintainResolution; | |
1745 } else { | |
1746 degradation_preference = DegradationPreference::kMaintainFramerate; | |
1747 } | |
1748 } | |
1749 return degradation_preference; | |
1750 } | |
1751 | |
1752 const std::vector<uint32_t>& | |
1753 WebRtcVideoChannel2::WebRtcVideoSendStream::GetSsrcs() const { | |
1754 return ssrcs_; | |
1755 } | |
1756 | |
1757 WebRtcVideoChannel2::WebRtcVideoSendStream::AllocatedEncoder | |
1758 WebRtcVideoChannel2::WebRtcVideoSendStream::CreateVideoEncoder( | |
1759 const VideoCodec& codec, | |
1760 bool force_encoder_allocation) { | |
1761 RTC_DCHECK_RUN_ON(&thread_checker_); | |
1762 // Do not re-create encoders of the same type. | |
1763 if (!force_encoder_allocation && codec == allocated_encoder_.codec && | |
1764 allocated_encoder_.encoder != nullptr) { | |
1765 return allocated_encoder_; | |
1766 } | |
1767 | |
1768 // Try creating external encoder. | |
1769 if (external_encoder_factory_ != nullptr && | |
1770 FindMatchingCodec(external_encoder_factory_->supported_codecs(), codec)) { | |
1771 webrtc::VideoEncoder* encoder = | |
1772 external_encoder_factory_->CreateVideoEncoder(codec); | |
1773 if (encoder != nullptr) | |
1774 return AllocatedEncoder(encoder, codec, true /* is_external */); | |
1775 } | |
1776 | |
1777 // Try creating internal encoder. | |
1778 if (FindMatchingCodec(internal_encoder_factory_->supported_codecs(), codec)) { | |
1779 if (parameters_.encoder_config.content_type == | |
1780 webrtc::VideoEncoderConfig::ContentType::kScreen && | |
1781 parameters_.conference_mode && UseSimulcastScreenshare()) { | |
1782 // TODO(sprang): Remove this adapter once libvpx supports simulcast with | |
1783 // same-resolution substreams. | |
1784 WebRtcSimulcastEncoderFactory adapter_factory( | |
1785 internal_encoder_factory_.get()); | |
1786 return AllocatedEncoder(adapter_factory.CreateVideoEncoder(codec), codec, | |
1787 false /* is_external */); | |
1788 } | |
1789 return AllocatedEncoder( | |
1790 internal_encoder_factory_->CreateVideoEncoder(codec), codec, | |
1791 false /* is_external */); | |
1792 } | |
1793 | |
1794 // This shouldn't happen, we should not be trying to create something we don't | |
1795 // support. | |
1796 RTC_NOTREACHED(); | |
1797 return AllocatedEncoder(NULL, cricket::VideoCodec(), false); | |
1798 } | |
1799 | |
1800 void WebRtcVideoChannel2::WebRtcVideoSendStream::DestroyVideoEncoder( | |
1801 AllocatedEncoder* encoder) { | |
1802 RTC_DCHECK_RUN_ON(&thread_checker_); | |
1803 if (encoder->external) { | |
1804 external_encoder_factory_->DestroyVideoEncoder(encoder->external_encoder); | |
1805 } | |
1806 delete encoder->encoder; | |
1807 } | |
1808 | |
1809 void WebRtcVideoChannel2::WebRtcVideoSendStream::SetCodec( | |
1810 const VideoCodecSettings& codec_settings, | |
1811 bool force_encoder_allocation) { | |
1812 RTC_DCHECK_RUN_ON(&thread_checker_); | |
1813 parameters_.encoder_config = CreateVideoEncoderConfig(codec_settings.codec); | |
1814 RTC_DCHECK_GT(parameters_.encoder_config.number_of_streams, 0); | |
1815 | |
1816 AllocatedEncoder new_encoder = | |
1817 CreateVideoEncoder(codec_settings.codec, force_encoder_allocation); | |
1818 parameters_.config.encoder_settings.encoder = new_encoder.encoder; | |
1819 parameters_.config.encoder_settings.full_overuse_time = new_encoder.external; | |
1820 parameters_.config.encoder_settings.payload_name = codec_settings.codec.name; | |
1821 parameters_.config.encoder_settings.payload_type = codec_settings.codec.id; | |
1822 if (new_encoder.external) { | |
1823 webrtc::VideoCodecType type = | |
1824 webrtc::PayloadNameToCodecType(codec_settings.codec.name) | |
1825 .value_or(webrtc::kVideoCodecUnknown); | |
1826 parameters_.config.encoder_settings.internal_source = | |
1827 external_encoder_factory_->EncoderTypeHasInternalSource(type); | |
1828 } else { | |
1829 parameters_.config.encoder_settings.internal_source = false; | |
1830 } | |
1831 parameters_.config.rtp.ulpfec = codec_settings.ulpfec; | |
1832 parameters_.config.rtp.flexfec.payload_type = | |
1833 codec_settings.flexfec_payload_type; | |
1834 | |
1835 // Set RTX payload type if RTX is enabled. | |
1836 if (!parameters_.config.rtp.rtx.ssrcs.empty()) { | |
1837 if (codec_settings.rtx_payload_type == -1) { | |
1838 LOG(LS_WARNING) << "RTX SSRCs configured but there's no configured RTX " | |
1839 "payload type. Ignoring."; | |
1840 parameters_.config.rtp.rtx.ssrcs.clear(); | |
1841 } else { | |
1842 parameters_.config.rtp.rtx.payload_type = codec_settings.rtx_payload_type; | |
1843 } | |
1844 } | |
1845 | |
1846 parameters_.config.rtp.nack.rtp_history_ms = | |
1847 HasNack(codec_settings.codec) ? kNackHistoryMs : 0; | |
1848 | |
1849 parameters_.codec_settings = | |
1850 rtc::Optional<WebRtcVideoChannel2::VideoCodecSettings>(codec_settings); | |
1851 | |
1852 LOG(LS_INFO) << "RecreateWebRtcStream (send) because of SetCodec."; | |
1853 RecreateWebRtcStream(); | |
1854 if (allocated_encoder_.encoder != new_encoder.encoder) { | |
1855 DestroyVideoEncoder(&allocated_encoder_); | |
1856 allocated_encoder_ = new_encoder; | |
1857 } | |
1858 } | |
1859 | |
1860 void WebRtcVideoChannel2::WebRtcVideoSendStream::SetSendParameters( | |
1861 const ChangedSendParameters& params) { | |
1862 RTC_DCHECK_RUN_ON(&thread_checker_); | |
1863 // |recreate_stream| means construction-time parameters have changed and the | |
1864 // sending stream needs to be reset with the new config. | |
1865 bool recreate_stream = false; | |
1866 if (params.rtcp_mode) { | |
1867 parameters_.config.rtp.rtcp_mode = *params.rtcp_mode; | |
1868 recreate_stream = true; | |
1869 } | |
1870 if (params.rtp_header_extensions) { | |
1871 parameters_.config.rtp.extensions = *params.rtp_header_extensions; | |
1872 recreate_stream = true; | |
1873 } | |
1874 if (params.max_bandwidth_bps) { | |
1875 parameters_.max_bitrate_bps = *params.max_bandwidth_bps; | |
1876 ReconfigureEncoder(); | |
1877 } | |
1878 if (params.conference_mode) { | |
1879 parameters_.conference_mode = *params.conference_mode; | |
1880 } | |
1881 | |
1882 // Set codecs and options. | |
1883 if (params.codec) { | |
1884 bool force_encoder_allocation = false; | |
1885 SetCodec(*params.codec, force_encoder_allocation); | |
1886 recreate_stream = false; // SetCodec has already recreated the stream. | |
1887 } else if (params.conference_mode && parameters_.codec_settings) { | |
1888 bool force_encoder_allocation = false; | |
1889 SetCodec(*parameters_.codec_settings, force_encoder_allocation); | |
1890 recreate_stream = false; // SetCodec has already recreated the stream. | |
1891 } | |
1892 if (recreate_stream) { | |
1893 LOG(LS_INFO) << "RecreateWebRtcStream (send) because of SetSendParameters"; | |
1894 RecreateWebRtcStream(); | |
1895 } | |
1896 } | |
1897 | |
1898 bool WebRtcVideoChannel2::WebRtcVideoSendStream::SetRtpParameters( | |
1899 const webrtc::RtpParameters& new_parameters) { | |
1900 RTC_DCHECK_RUN_ON(&thread_checker_); | |
1901 if (!ValidateRtpParameters(new_parameters)) { | |
1902 return false; | |
1903 } | |
1904 | |
1905 bool reconfigure_encoder = new_parameters.encodings[0].max_bitrate_bps != | |
1906 rtp_parameters_.encodings[0].max_bitrate_bps; | |
1907 rtp_parameters_ = new_parameters; | |
1908 // Codecs are currently handled at the WebRtcVideoChannel2 level. | |
1909 rtp_parameters_.codecs.clear(); | |
1910 if (reconfigure_encoder) { | |
1911 ReconfigureEncoder(); | |
1912 } | |
1913 // Encoding may have been activated/deactivated. | |
1914 UpdateSendState(); | |
1915 return true; | |
1916 } | |
1917 | |
1918 webrtc::RtpParameters | |
1919 WebRtcVideoChannel2::WebRtcVideoSendStream::GetRtpParameters() const { | |
1920 RTC_DCHECK_RUN_ON(&thread_checker_); | |
1921 return rtp_parameters_; | |
1922 } | |
1923 | |
1924 bool WebRtcVideoChannel2::WebRtcVideoSendStream::ValidateRtpParameters( | |
1925 const webrtc::RtpParameters& rtp_parameters) { | |
1926 RTC_DCHECK_RUN_ON(&thread_checker_); | |
1927 if (rtp_parameters.encodings.size() != 1) { | |
1928 LOG(LS_ERROR) | |
1929 << "Attempted to set RtpParameters without exactly one encoding"; | |
1930 return false; | |
1931 } | |
1932 if (rtp_parameters.encodings[0].ssrc != rtp_parameters_.encodings[0].ssrc) { | |
1933 LOG(LS_ERROR) << "Attempted to set RtpParameters with modified SSRC"; | |
1934 return false; | |
1935 } | |
1936 return true; | |
1937 } | |
1938 | |
1939 void WebRtcVideoChannel2::WebRtcVideoSendStream::UpdateSendState() { | |
1940 RTC_DCHECK_RUN_ON(&thread_checker_); | |
1941 // TODO(deadbeef): Need to handle more than one encoding in the future. | |
1942 RTC_DCHECK(rtp_parameters_.encodings.size() == 1u); | |
1943 if (sending_ && rtp_parameters_.encodings[0].active) { | |
1944 RTC_DCHECK(stream_ != nullptr); | |
1945 stream_->Start(); | |
1946 } else { | |
1947 if (stream_ != nullptr) { | |
1948 stream_->Stop(); | |
1949 } | |
1950 } | |
1951 } | |
1952 | |
1953 webrtc::VideoEncoderConfig | |
1954 WebRtcVideoChannel2::WebRtcVideoSendStream::CreateVideoEncoderConfig( | |
1955 const VideoCodec& codec) const { | |
1956 RTC_DCHECK_RUN_ON(&thread_checker_); | |
1957 webrtc::VideoEncoderConfig encoder_config; | |
1958 bool is_screencast = parameters_.options.is_screencast.value_or(false); | |
1959 if (is_screencast) { | |
1960 encoder_config.min_transmit_bitrate_bps = | |
1961 1000 * parameters_.options.screencast_min_bitrate_kbps.value_or(0); | |
1962 encoder_config.content_type = | |
1963 webrtc::VideoEncoderConfig::ContentType::kScreen; | |
1964 } else { | |
1965 encoder_config.min_transmit_bitrate_bps = 0; | |
1966 encoder_config.content_type = | |
1967 webrtc::VideoEncoderConfig::ContentType::kRealtimeVideo; | |
1968 } | |
1969 | |
1970 // By default, the stream count for the codec configuration should match the | |
1971 // number of negotiated ssrcs. But if the codec is blacklisted for simulcast | |
1972 // or a screencast (and not in simulcast screenshare experiment), only | |
1973 // configure a single stream. | |
1974 encoder_config.number_of_streams = parameters_.config.rtp.ssrcs.size(); | |
1975 if (IsCodecBlacklistedForSimulcast(codec.name) || | |
1976 (is_screencast && | |
1977 (!UseSimulcastScreenshare() || !parameters_.conference_mode))) { | |
1978 encoder_config.number_of_streams = 1; | |
1979 } | |
1980 | |
1981 int stream_max_bitrate = parameters_.max_bitrate_bps; | |
1982 if (rtp_parameters_.encodings[0].max_bitrate_bps) { | |
1983 stream_max_bitrate = | |
1984 MinPositive(*(rtp_parameters_.encodings[0].max_bitrate_bps), | |
1985 parameters_.max_bitrate_bps); | |
1986 } | |
1987 | |
1988 int codec_max_bitrate_kbps; | |
1989 if (codec.GetParam(kCodecParamMaxBitrate, &codec_max_bitrate_kbps)) { | |
1990 stream_max_bitrate = codec_max_bitrate_kbps * 1000; | |
1991 } | |
1992 encoder_config.max_bitrate_bps = stream_max_bitrate; | |
1993 | |
1994 int max_qp = kDefaultQpMax; | |
1995 codec.GetParam(kCodecParamMaxQuantization, &max_qp); | |
1996 encoder_config.video_stream_factory = | |
1997 new rtc::RefCountedObject<EncoderStreamFactory>( | |
1998 codec.name, max_qp, kDefaultVideoMaxFramerate, is_screencast, | |
1999 parameters_.conference_mode); | |
2000 return encoder_config; | |
2001 } | |
2002 | |
2003 void WebRtcVideoChannel2::WebRtcVideoSendStream::ReconfigureEncoder() { | |
2004 RTC_DCHECK_RUN_ON(&thread_checker_); | |
2005 if (!stream_) { | |
2006 // The webrtc::VideoSendStream |stream_| has not yet been created but other | |
2007 // parameters has changed. | |
2008 return; | |
2009 } | |
2010 | |
2011 RTC_DCHECK_GT(parameters_.encoder_config.number_of_streams, 0); | |
2012 | |
2013 RTC_CHECK(parameters_.codec_settings); | |
2014 VideoCodecSettings codec_settings = *parameters_.codec_settings; | |
2015 | |
2016 webrtc::VideoEncoderConfig encoder_config = | |
2017 CreateVideoEncoderConfig(codec_settings.codec); | |
2018 | |
2019 encoder_config.encoder_specific_settings = ConfigureVideoEncoderSettings( | |
2020 codec_settings.codec); | |
2021 | |
2022 stream_->ReconfigureVideoEncoder(encoder_config.Copy()); | |
2023 | |
2024 encoder_config.encoder_specific_settings = NULL; | |
2025 | |
2026 parameters_.encoder_config = std::move(encoder_config); | |
2027 } | |
2028 | |
2029 void WebRtcVideoChannel2::WebRtcVideoSendStream::SetSend(bool send) { | |
2030 RTC_DCHECK_RUN_ON(&thread_checker_); | |
2031 sending_ = send; | |
2032 UpdateSendState(); | |
2033 } | |
2034 | |
2035 void WebRtcVideoChannel2::WebRtcVideoSendStream::RemoveSink( | |
2036 rtc::VideoSinkInterface<webrtc::VideoFrame>* sink) { | |
2037 RTC_DCHECK_RUN_ON(&thread_checker_); | |
2038 RTC_DCHECK(encoder_sink_ == sink); | |
2039 encoder_sink_ = nullptr; | |
2040 source_->RemoveSink(sink); | |
2041 } | |
2042 | |
2043 void WebRtcVideoChannel2::WebRtcVideoSendStream::AddOrUpdateSink( | |
2044 rtc::VideoSinkInterface<webrtc::VideoFrame>* sink, | |
2045 const rtc::VideoSinkWants& wants) { | |
2046 if (worker_thread_ == rtc::Thread::Current()) { | |
2047 // AddOrUpdateSink is called on |worker_thread_| if this is the first | |
2048 // registration of |sink|. | |
2049 RTC_DCHECK_RUN_ON(&thread_checker_); | |
2050 encoder_sink_ = sink; | |
2051 source_->AddOrUpdateSink(encoder_sink_, wants); | |
2052 } else { | |
2053 // Subsequent calls to AddOrUpdateSink will happen on the encoder task | |
2054 // queue. | |
2055 invoker_.AsyncInvoke<void>( | |
2056 RTC_FROM_HERE, worker_thread_, [this, sink, wants] { | |
2057 RTC_DCHECK_RUN_ON(&thread_checker_); | |
2058 // |sink| may be invalidated after this task was posted since | |
2059 // RemoveSink is called on the worker thread. | |
2060 bool encoder_sink_valid = (sink == encoder_sink_); | |
2061 if (source_ && encoder_sink_valid) { | |
2062 source_->AddOrUpdateSink(encoder_sink_, wants); | |
2063 } | |
2064 }); | |
2065 } | |
2066 } | |
2067 | |
2068 VideoSenderInfo WebRtcVideoChannel2::WebRtcVideoSendStream::GetVideoSenderInfo( | |
2069 bool log_stats) { | |
2070 VideoSenderInfo info; | |
2071 RTC_DCHECK_RUN_ON(&thread_checker_); | |
2072 for (uint32_t ssrc : parameters_.config.rtp.ssrcs) | |
2073 info.add_ssrc(ssrc); | |
2074 | |
2075 if (parameters_.codec_settings) { | |
2076 info.codec_name = parameters_.codec_settings->codec.name; | |
2077 info.codec_payload_type = rtc::Optional<int>( | |
2078 parameters_.codec_settings->codec.id); | |
2079 } | |
2080 | |
2081 if (stream_ == NULL) | |
2082 return info; | |
2083 | |
2084 webrtc::VideoSendStream::Stats stats = stream_->GetStats(); | |
2085 | |
2086 if (log_stats) | |
2087 LOG(LS_INFO) << stats.ToString(rtc::TimeMillis()); | |
2088 | |
2089 info.adapt_changes = stats.number_of_cpu_adapt_changes; | |
2090 info.adapt_reason = | |
2091 stats.cpu_limited_resolution ? ADAPTREASON_CPU : ADAPTREASON_NONE; | |
2092 | |
2093 // Get bandwidth limitation info from stream_->GetStats(). | |
2094 // Input resolution (output from video_adapter) can be further scaled down or | |
2095 // higher video layer(s) can be dropped due to bitrate constraints. | |
2096 // Note, adapt_changes only include changes from the video_adapter. | |
2097 if (stats.bw_limited_resolution) | |
2098 info.adapt_reason |= ADAPTREASON_BANDWIDTH; | |
2099 | |
2100 info.encoder_implementation_name = stats.encoder_implementation_name; | |
2101 info.ssrc_groups = ssrc_groups_; | |
2102 info.framerate_input = stats.input_frame_rate; | |
2103 info.framerate_sent = stats.encode_frame_rate; | |
2104 info.avg_encode_ms = stats.avg_encode_time_ms; | |
2105 info.encode_usage_percent = stats.encode_usage_percent; | |
2106 info.frames_encoded = stats.frames_encoded; | |
2107 info.qp_sum = stats.qp_sum; | |
2108 | |
2109 info.nominal_bitrate = stats.media_bitrate_bps; | |
2110 info.preferred_bitrate = stats.preferred_media_bitrate_bps; | |
2111 | |
2112 info.send_frame_width = 0; | |
2113 info.send_frame_height = 0; | |
2114 for (std::map<uint32_t, webrtc::VideoSendStream::StreamStats>::iterator it = | |
2115 stats.substreams.begin(); | |
2116 it != stats.substreams.end(); ++it) { | |
2117 // TODO(pbos): Wire up additional stats, such as padding bytes. | |
2118 webrtc::VideoSendStream::StreamStats stream_stats = it->second; | |
2119 info.bytes_sent += stream_stats.rtp_stats.transmitted.payload_bytes + | |
2120 stream_stats.rtp_stats.transmitted.header_bytes + | |
2121 stream_stats.rtp_stats.transmitted.padding_bytes; | |
2122 info.packets_sent += stream_stats.rtp_stats.transmitted.packets; | |
2123 info.packets_lost += stream_stats.rtcp_stats.cumulative_lost; | |
2124 if (stream_stats.width > info.send_frame_width) | |
2125 info.send_frame_width = stream_stats.width; | |
2126 if (stream_stats.height > info.send_frame_height) | |
2127 info.send_frame_height = stream_stats.height; | |
2128 info.firs_rcvd += stream_stats.rtcp_packet_type_counts.fir_packets; | |
2129 info.nacks_rcvd += stream_stats.rtcp_packet_type_counts.nack_packets; | |
2130 info.plis_rcvd += stream_stats.rtcp_packet_type_counts.pli_packets; | |
2131 } | |
2132 | |
2133 if (!stats.substreams.empty()) { | |
2134 // TODO(pbos): Report fraction lost per SSRC. | |
2135 webrtc::VideoSendStream::StreamStats first_stream_stats = | |
2136 stats.substreams.begin()->second; | |
2137 info.fraction_lost = | |
2138 static_cast<float>(first_stream_stats.rtcp_stats.fraction_lost) / | |
2139 (1 << 8); | |
2140 } | |
2141 | |
2142 return info; | |
2143 } | |
2144 | |
2145 void WebRtcVideoChannel2::WebRtcVideoSendStream::FillBitrateInfo( | |
2146 BandwidthEstimationInfo* bwe_info) { | |
2147 RTC_DCHECK_RUN_ON(&thread_checker_); | |
2148 if (stream_ == NULL) { | |
2149 return; | |
2150 } | |
2151 webrtc::VideoSendStream::Stats stats = stream_->GetStats(); | |
2152 for (std::map<uint32_t, webrtc::VideoSendStream::StreamStats>::iterator it = | |
2153 stats.substreams.begin(); | |
2154 it != stats.substreams.end(); ++it) { | |
2155 bwe_info->transmit_bitrate += it->second.total_bitrate_bps; | |
2156 bwe_info->retransmit_bitrate += it->second.retransmit_bitrate_bps; | |
2157 } | |
2158 bwe_info->target_enc_bitrate += stats.target_media_bitrate_bps; | |
2159 bwe_info->actual_enc_bitrate += stats.media_bitrate_bps; | |
2160 } | |
2161 | |
2162 void WebRtcVideoChannel2::WebRtcVideoSendStream::RecreateWebRtcStream() { | |
2163 RTC_DCHECK_RUN_ON(&thread_checker_); | |
2164 if (stream_ != NULL) { | |
2165 call_->DestroyVideoSendStream(stream_); | |
2166 } | |
2167 | |
2168 RTC_CHECK(parameters_.codec_settings); | |
2169 RTC_DCHECK_EQ((parameters_.encoder_config.content_type == | |
2170 webrtc::VideoEncoderConfig::ContentType::kScreen), | |
2171 parameters_.options.is_screencast.value_or(false)) | |
2172 << "encoder content type inconsistent with screencast option"; | |
2173 parameters_.encoder_config.encoder_specific_settings = | |
2174 ConfigureVideoEncoderSettings(parameters_.codec_settings->codec); | |
2175 | |
2176 webrtc::VideoSendStream::Config config = parameters_.config.Copy(); | |
2177 if (!config.rtp.rtx.ssrcs.empty() && config.rtp.rtx.payload_type == -1) { | |
2178 LOG(LS_WARNING) << "RTX SSRCs configured but there's no configured RTX " | |
2179 "payload type the set codec. Ignoring RTX."; | |
2180 config.rtp.rtx.ssrcs.clear(); | |
2181 } | |
2182 stream_ = call_->CreateVideoSendStream(std::move(config), | |
2183 parameters_.encoder_config.Copy()); | |
2184 | |
2185 parameters_.encoder_config.encoder_specific_settings = NULL; | |
2186 | |
2187 if (source_) { | |
2188 stream_->SetSource(this, GetDegradationPreference()); | |
2189 } | |
2190 | |
2191 // Call stream_->Start() if necessary conditions are met. | |
2192 UpdateSendState(); | |
2193 } | |
2194 | |
2195 WebRtcVideoChannel2::WebRtcVideoReceiveStream::WebRtcVideoReceiveStream( | |
2196 webrtc::Call* call, | |
2197 const StreamParams& sp, | |
2198 webrtc::VideoReceiveStream::Config config, | |
2199 WebRtcVideoDecoderFactory* external_decoder_factory, | |
2200 bool default_stream, | |
2201 const std::vector<VideoCodecSettings>& recv_codecs, | |
2202 const webrtc::FlexfecReceiveStream::Config& flexfec_config) | |
2203 : call_(call), | |
2204 stream_params_(sp), | |
2205 stream_(NULL), | |
2206 default_stream_(default_stream), | |
2207 config_(std::move(config)), | |
2208 flexfec_config_(flexfec_config), | |
2209 flexfec_stream_(nullptr), | |
2210 external_decoder_factory_(external_decoder_factory), | |
2211 sink_(NULL), | |
2212 first_frame_timestamp_(-1), | |
2213 estimated_remote_start_ntp_time_ms_(0) { | |
2214 config_.renderer = this; | |
2215 std::vector<AllocatedDecoder> old_decoders; | |
2216 ConfigureCodecs(recv_codecs, &old_decoders); | |
2217 ConfigureFlexfecCodec(flexfec_config.payload_type); | |
2218 MaybeRecreateWebRtcFlexfecStream(); | |
2219 RecreateWebRtcVideoStream(); | |
2220 RTC_DCHECK(old_decoders.empty()); | |
2221 } | |
2222 | |
2223 WebRtcVideoChannel2::WebRtcVideoReceiveStream::AllocatedDecoder:: | |
2224 AllocatedDecoder(webrtc::VideoDecoder* decoder, | |
2225 webrtc::VideoCodecType type, | |
2226 bool external) | |
2227 : decoder(decoder), | |
2228 external_decoder(nullptr), | |
2229 type(type), | |
2230 external(external) { | |
2231 if (external) { | |
2232 external_decoder = decoder; | |
2233 this->decoder = | |
2234 new webrtc::VideoDecoderSoftwareFallbackWrapper(type, external_decoder); | |
2235 } | |
2236 } | |
2237 | |
2238 WebRtcVideoChannel2::WebRtcVideoReceiveStream::~WebRtcVideoReceiveStream() { | |
2239 if (flexfec_stream_) { | |
2240 call_->DestroyFlexfecReceiveStream(flexfec_stream_); | |
2241 } | |
2242 call_->DestroyVideoReceiveStream(stream_); | |
2243 ClearDecoders(&allocated_decoders_); | |
2244 } | |
2245 | |
2246 const std::vector<uint32_t>& | |
2247 WebRtcVideoChannel2::WebRtcVideoReceiveStream::GetSsrcs() const { | |
2248 return stream_params_.ssrcs; | |
2249 } | |
2250 | |
2251 rtc::Optional<uint32_t> | |
2252 WebRtcVideoChannel2::WebRtcVideoReceiveStream::GetFirstPrimarySsrc() const { | |
2253 std::vector<uint32_t> primary_ssrcs; | |
2254 stream_params_.GetPrimarySsrcs(&primary_ssrcs); | |
2255 | |
2256 if (primary_ssrcs.empty()) { | |
2257 LOG(LS_WARNING) << "Empty primary ssrcs vector, returning empty optional"; | |
2258 return rtc::Optional<uint32_t>(); | |
2259 } else { | |
2260 return rtc::Optional<uint32_t>(primary_ssrcs[0]); | |
2261 } | |
2262 } | |
2263 | |
2264 WebRtcVideoChannel2::WebRtcVideoReceiveStream::AllocatedDecoder | |
2265 WebRtcVideoChannel2::WebRtcVideoReceiveStream::CreateOrReuseVideoDecoder( | |
2266 std::vector<AllocatedDecoder>* old_decoders, | |
2267 const VideoCodec& codec) { | |
2268 webrtc::VideoCodecType type = webrtc::PayloadNameToCodecType(codec.name) | |
2269 .value_or(webrtc::kVideoCodecUnknown); | |
2270 | |
2271 for (size_t i = 0; i < old_decoders->size(); ++i) { | |
2272 if ((*old_decoders)[i].type == type) { | |
2273 AllocatedDecoder decoder = (*old_decoders)[i]; | |
2274 (*old_decoders)[i] = old_decoders->back(); | |
2275 old_decoders->pop_back(); | |
2276 return decoder; | |
2277 } | |
2278 } | |
2279 | |
2280 if (external_decoder_factory_ != NULL) { | |
2281 webrtc::VideoDecoder* decoder = | |
2282 external_decoder_factory_->CreateVideoDecoderWithParams( | |
2283 type, {stream_params_.id}); | |
2284 if (decoder != NULL) { | |
2285 return AllocatedDecoder(decoder, type, true /* is_external */); | |
2286 } | |
2287 } | |
2288 | |
2289 InternalDecoderFactory internal_decoder_factory; | |
2290 return AllocatedDecoder(internal_decoder_factory.CreateVideoDecoderWithParams( | |
2291 type, {stream_params_.id}), | |
2292 type, false /* is_external */); | |
2293 } | |
2294 | |
2295 void WebRtcVideoChannel2::WebRtcVideoReceiveStream::ConfigureCodecs( | |
2296 const std::vector<VideoCodecSettings>& recv_codecs, | |
2297 std::vector<AllocatedDecoder>* old_decoders) { | |
2298 *old_decoders = allocated_decoders_; | |
2299 allocated_decoders_.clear(); | |
2300 config_.decoders.clear(); | |
2301 for (size_t i = 0; i < recv_codecs.size(); ++i) { | |
2302 AllocatedDecoder allocated_decoder = | |
2303 CreateOrReuseVideoDecoder(old_decoders, recv_codecs[i].codec); | |
2304 allocated_decoders_.push_back(allocated_decoder); | |
2305 | |
2306 webrtc::VideoReceiveStream::Decoder decoder; | |
2307 decoder.decoder = allocated_decoder.decoder; | |
2308 decoder.payload_type = recv_codecs[i].codec.id; | |
2309 decoder.payload_name = recv_codecs[i].codec.name; | |
2310 decoder.codec_params = recv_codecs[i].codec.params; | |
2311 config_.decoders.push_back(decoder); | |
2312 } | |
2313 | |
2314 config_.rtp.rtx_payload_types.clear(); | |
2315 for (const VideoCodecSettings& recv_codec : recv_codecs) { | |
2316 config_.rtp.rtx_payload_types[recv_codec.codec.id] = | |
2317 recv_codec.rtx_payload_type; | |
2318 } | |
2319 | |
2320 config_.rtp.ulpfec = recv_codecs.front().ulpfec; | |
2321 | |
2322 config_.rtp.nack.rtp_history_ms = | |
2323 HasNack(recv_codecs.begin()->codec) ? kNackHistoryMs : 0; | |
2324 } | |
2325 | |
2326 void WebRtcVideoChannel2::WebRtcVideoReceiveStream::ConfigureFlexfecCodec( | |
2327 int flexfec_payload_type) { | |
2328 flexfec_config_.payload_type = flexfec_payload_type; | |
2329 } | |
2330 | |
2331 void WebRtcVideoChannel2::WebRtcVideoReceiveStream::SetLocalSsrc( | |
2332 uint32_t local_ssrc) { | |
2333 // TODO(pbos): Consider turning this sanity check into a RTC_DCHECK. You | |
2334 // should not be able to create a sender with the same SSRC as a receiver, but | |
2335 // right now this can't be done due to unittests depending on receiving what | |
2336 // they are sending from the same MediaChannel. | |
2337 if (local_ssrc == config_.rtp.remote_ssrc) { | |
2338 LOG(LS_INFO) << "Ignoring call to SetLocalSsrc because parameters are " | |
2339 "unchanged; local_ssrc=" << local_ssrc; | |
2340 return; | |
2341 } | |
2342 | |
2343 config_.rtp.local_ssrc = local_ssrc; | |
2344 flexfec_config_.local_ssrc = local_ssrc; | |
2345 LOG(LS_INFO) | |
2346 << "RecreateWebRtcStream (recv) because of SetLocalSsrc; local_ssrc=" | |
2347 << local_ssrc; | |
2348 MaybeRecreateWebRtcFlexfecStream(); | |
2349 RecreateWebRtcVideoStream(); | |
2350 } | |
2351 | |
2352 void WebRtcVideoChannel2::WebRtcVideoReceiveStream::SetFeedbackParameters( | |
2353 bool nack_enabled, | |
2354 bool remb_enabled, | |
2355 bool transport_cc_enabled, | |
2356 webrtc::RtcpMode rtcp_mode) { | |
2357 int nack_history_ms = nack_enabled ? kNackHistoryMs : 0; | |
2358 if (config_.rtp.nack.rtp_history_ms == nack_history_ms && | |
2359 config_.rtp.remb == remb_enabled && | |
2360 config_.rtp.transport_cc == transport_cc_enabled && | |
2361 config_.rtp.rtcp_mode == rtcp_mode) { | |
2362 LOG(LS_INFO) | |
2363 << "Ignoring call to SetFeedbackParameters because parameters are " | |
2364 "unchanged; nack=" | |
2365 << nack_enabled << ", remb=" << remb_enabled | |
2366 << ", transport_cc=" << transport_cc_enabled; | |
2367 return; | |
2368 } | |
2369 config_.rtp.remb = remb_enabled; | |
2370 config_.rtp.nack.rtp_history_ms = nack_history_ms; | |
2371 config_.rtp.transport_cc = transport_cc_enabled; | |
2372 config_.rtp.rtcp_mode = rtcp_mode; | |
2373 // TODO(brandtr): We should be spec-compliant and set |transport_cc| here | |
2374 // based on the rtcp-fb for the FlexFEC codec, not the media codec. | |
2375 flexfec_config_.transport_cc = config_.rtp.transport_cc; | |
2376 flexfec_config_.rtcp_mode = config_.rtp.rtcp_mode; | |
2377 LOG(LS_INFO) | |
2378 << "RecreateWebRtcStream (recv) because of SetFeedbackParameters; nack=" | |
2379 << nack_enabled << ", remb=" << remb_enabled | |
2380 << ", transport_cc=" << transport_cc_enabled; | |
2381 MaybeRecreateWebRtcFlexfecStream(); | |
2382 RecreateWebRtcVideoStream(); | |
2383 } | |
2384 | |
2385 void WebRtcVideoChannel2::WebRtcVideoReceiveStream::SetRecvParameters( | |
2386 const ChangedRecvParameters& params) { | |
2387 bool video_needs_recreation = false; | |
2388 bool flexfec_needs_recreation = false; | |
2389 std::vector<AllocatedDecoder> old_decoders; | |
2390 if (params.codec_settings) { | |
2391 ConfigureCodecs(*params.codec_settings, &old_decoders); | |
2392 video_needs_recreation = true; | |
2393 } | |
2394 if (params.rtp_header_extensions) { | |
2395 config_.rtp.extensions = *params.rtp_header_extensions; | |
2396 flexfec_config_.rtp_header_extensions = *params.rtp_header_extensions; | |
2397 video_needs_recreation = true; | |
2398 flexfec_needs_recreation = true; | |
2399 } | |
2400 if (params.flexfec_payload_type) { | |
2401 ConfigureFlexfecCodec(*params.flexfec_payload_type); | |
2402 flexfec_needs_recreation = true; | |
2403 } | |
2404 if (flexfec_needs_recreation) { | |
2405 LOG(LS_INFO) << "MaybeRecreateWebRtcFlexfecStream (recv) because of " | |
2406 "SetRecvParameters"; | |
2407 MaybeRecreateWebRtcFlexfecStream(); | |
2408 } | |
2409 if (video_needs_recreation) { | |
2410 LOG(LS_INFO) | |
2411 << "RecreateWebRtcVideoStream (recv) because of SetRecvParameters"; | |
2412 RecreateWebRtcVideoStream(); | |
2413 ClearDecoders(&old_decoders); | |
2414 } | |
2415 } | |
2416 | |
2417 void WebRtcVideoChannel2::WebRtcVideoReceiveStream:: | |
2418 RecreateWebRtcVideoStream() { | |
2419 if (stream_) { | |
2420 call_->DestroyVideoReceiveStream(stream_); | |
2421 stream_ = nullptr; | |
2422 } | |
2423 webrtc::VideoReceiveStream::Config config = config_.Copy(); | |
2424 config.rtp.protected_by_flexfec = (flexfec_stream_ != nullptr); | |
2425 stream_ = call_->CreateVideoReceiveStream(std::move(config)); | |
2426 stream_->Start(); | |
2427 } | |
2428 | |
2429 void WebRtcVideoChannel2::WebRtcVideoReceiveStream:: | |
2430 MaybeRecreateWebRtcFlexfecStream() { | |
2431 if (flexfec_stream_) { | |
2432 call_->DestroyFlexfecReceiveStream(flexfec_stream_); | |
2433 flexfec_stream_ = nullptr; | |
2434 } | |
2435 if (flexfec_config_.IsCompleteAndEnabled()) { | |
2436 flexfec_stream_ = call_->CreateFlexfecReceiveStream(flexfec_config_); | |
2437 flexfec_stream_->Start(); | |
2438 } | |
2439 } | |
2440 | |
2441 void WebRtcVideoChannel2::WebRtcVideoReceiveStream::ClearDecoders( | |
2442 std::vector<AllocatedDecoder>* allocated_decoders) { | |
2443 for (size_t i = 0; i < allocated_decoders->size(); ++i) { | |
2444 if ((*allocated_decoders)[i].external) { | |
2445 external_decoder_factory_->DestroyVideoDecoder( | |
2446 (*allocated_decoders)[i].external_decoder); | |
2447 } | |
2448 delete (*allocated_decoders)[i].decoder; | |
2449 } | |
2450 allocated_decoders->clear(); | |
2451 } | |
2452 | |
2453 void WebRtcVideoChannel2::WebRtcVideoReceiveStream::OnFrame( | |
2454 const webrtc::VideoFrame& frame) { | |
2455 rtc::CritScope crit(&sink_lock_); | |
2456 | |
2457 if (first_frame_timestamp_ < 0) | |
2458 first_frame_timestamp_ = frame.timestamp(); | |
2459 int64_t rtp_time_elapsed_since_first_frame = | |
2460 (timestamp_wraparound_handler_.Unwrap(frame.timestamp()) - | |
2461 first_frame_timestamp_); | |
2462 int64_t elapsed_time_ms = rtp_time_elapsed_since_first_frame / | |
2463 (cricket::kVideoCodecClockrate / 1000); | |
2464 if (frame.ntp_time_ms() > 0) | |
2465 estimated_remote_start_ntp_time_ms_ = frame.ntp_time_ms() - elapsed_time_ms; | |
2466 | |
2467 if (sink_ == NULL) { | |
2468 LOG(LS_WARNING) << "VideoReceiveStream not connected to a VideoSink."; | |
2469 return; | |
2470 } | |
2471 | |
2472 sink_->OnFrame(frame); | |
2473 } | |
2474 | |
2475 bool WebRtcVideoChannel2::WebRtcVideoReceiveStream::IsDefaultStream() const { | |
2476 return default_stream_; | |
2477 } | |
2478 | |
2479 void WebRtcVideoChannel2::WebRtcVideoReceiveStream::SetSink( | |
2480 rtc::VideoSinkInterface<webrtc::VideoFrame>* sink) { | |
2481 rtc::CritScope crit(&sink_lock_); | |
2482 sink_ = sink; | |
2483 } | |
2484 | |
2485 std::string | |
2486 WebRtcVideoChannel2::WebRtcVideoReceiveStream::GetCodecNameFromPayloadType( | |
2487 int payload_type) { | |
2488 for (const webrtc::VideoReceiveStream::Decoder& decoder : config_.decoders) { | |
2489 if (decoder.payload_type == payload_type) { | |
2490 return decoder.payload_name; | |
2491 } | |
2492 } | |
2493 return ""; | |
2494 } | |
2495 | |
2496 VideoReceiverInfo | |
2497 WebRtcVideoChannel2::WebRtcVideoReceiveStream::GetVideoReceiverInfo( | |
2498 bool log_stats) { | |
2499 VideoReceiverInfo info; | |
2500 info.ssrc_groups = stream_params_.ssrc_groups; | |
2501 info.add_ssrc(config_.rtp.remote_ssrc); | |
2502 webrtc::VideoReceiveStream::Stats stats = stream_->GetStats(); | |
2503 info.decoder_implementation_name = stats.decoder_implementation_name; | |
2504 if (stats.current_payload_type != -1) { | |
2505 info.codec_payload_type = rtc::Optional<int>( | |
2506 stats.current_payload_type); | |
2507 } | |
2508 info.bytes_rcvd = stats.rtp_stats.transmitted.payload_bytes + | |
2509 stats.rtp_stats.transmitted.header_bytes + | |
2510 stats.rtp_stats.transmitted.padding_bytes; | |
2511 info.packets_rcvd = stats.rtp_stats.transmitted.packets; | |
2512 info.packets_lost = stats.rtcp_stats.cumulative_lost; | |
2513 info.fraction_lost = | |
2514 static_cast<float>(stats.rtcp_stats.fraction_lost) / (1 << 8); | |
2515 | |
2516 info.framerate_rcvd = stats.network_frame_rate; | |
2517 info.framerate_decoded = stats.decode_frame_rate; | |
2518 info.framerate_output = stats.render_frame_rate; | |
2519 info.frame_width = stats.width; | |
2520 info.frame_height = stats.height; | |
2521 | |
2522 { | |
2523 rtc::CritScope frame_cs(&sink_lock_); | |
2524 info.capture_start_ntp_time_ms = estimated_remote_start_ntp_time_ms_; | |
2525 } | |
2526 | |
2527 info.decode_ms = stats.decode_ms; | |
2528 info.max_decode_ms = stats.max_decode_ms; | |
2529 info.current_delay_ms = stats.current_delay_ms; | |
2530 info.target_delay_ms = stats.target_delay_ms; | |
2531 info.jitter_buffer_ms = stats.jitter_buffer_ms; | |
2532 info.min_playout_delay_ms = stats.min_playout_delay_ms; | |
2533 info.render_delay_ms = stats.render_delay_ms; | |
2534 info.frames_received = stats.frame_counts.key_frames + | |
2535 stats.frame_counts.delta_frames; | |
2536 info.frames_decoded = stats.frames_decoded; | |
2537 info.frames_rendered = stats.frames_rendered; | |
2538 info.qp_sum = stats.qp_sum; | |
2539 | |
2540 info.codec_name = GetCodecNameFromPayloadType(stats.current_payload_type); | |
2541 | |
2542 info.firs_sent = stats.rtcp_packet_type_counts.fir_packets; | |
2543 info.plis_sent = stats.rtcp_packet_type_counts.pli_packets; | |
2544 info.nacks_sent = stats.rtcp_packet_type_counts.nack_packets; | |
2545 | |
2546 if (log_stats) | |
2547 LOG(LS_INFO) << stats.ToString(rtc::TimeMillis()); | |
2548 | |
2549 return info; | |
2550 } | |
2551 | |
2552 WebRtcVideoChannel2::VideoCodecSettings::VideoCodecSettings() | |
2553 : flexfec_payload_type(-1), rtx_payload_type(-1) {} | |
2554 | |
2555 bool WebRtcVideoChannel2::VideoCodecSettings::operator==( | |
2556 const WebRtcVideoChannel2::VideoCodecSettings& other) const { | |
2557 return codec == other.codec && ulpfec == other.ulpfec && | |
2558 flexfec_payload_type == other.flexfec_payload_type && | |
2559 rtx_payload_type == other.rtx_payload_type; | |
2560 } | |
2561 | |
2562 bool WebRtcVideoChannel2::VideoCodecSettings::EqualsDisregardingFlexfec( | |
2563 const WebRtcVideoChannel2::VideoCodecSettings& a, | |
2564 const WebRtcVideoChannel2::VideoCodecSettings& b) { | |
2565 return a.codec == b.codec && a.ulpfec == b.ulpfec && | |
2566 a.rtx_payload_type == b.rtx_payload_type; | |
2567 } | |
2568 | |
2569 bool WebRtcVideoChannel2::VideoCodecSettings::operator!=( | |
2570 const WebRtcVideoChannel2::VideoCodecSettings& other) const { | |
2571 return !(*this == other); | |
2572 } | |
2573 | |
2574 std::vector<WebRtcVideoChannel2::VideoCodecSettings> | |
2575 WebRtcVideoChannel2::MapCodecs(const std::vector<VideoCodec>& codecs) { | |
2576 RTC_DCHECK(!codecs.empty()); | |
2577 | |
2578 std::vector<VideoCodecSettings> video_codecs; | |
2579 std::map<int, bool> payload_used; | |
2580 std::map<int, VideoCodec::CodecType> payload_codec_type; | |
2581 // |rtx_mapping| maps video payload type to rtx payload type. | |
2582 std::map<int, int> rtx_mapping; | |
2583 | |
2584 webrtc::UlpfecConfig ulpfec_config; | |
2585 int flexfec_payload_type = -1; | |
2586 | |
2587 for (size_t i = 0; i < codecs.size(); ++i) { | |
2588 const VideoCodec& in_codec = codecs[i]; | |
2589 int payload_type = in_codec.id; | |
2590 | |
2591 if (payload_used[payload_type]) { | |
2592 LOG(LS_ERROR) << "Payload type already registered: " | |
2593 << in_codec.ToString(); | |
2594 return std::vector<VideoCodecSettings>(); | |
2595 } | |
2596 payload_used[payload_type] = true; | |
2597 payload_codec_type[payload_type] = in_codec.GetCodecType(); | |
2598 | |
2599 switch (in_codec.GetCodecType()) { | |
2600 case VideoCodec::CODEC_RED: { | |
2601 // RED payload type, should not have duplicates. | |
2602 RTC_DCHECK_EQ(-1, ulpfec_config.red_payload_type); | |
2603 ulpfec_config.red_payload_type = in_codec.id; | |
2604 continue; | |
2605 } | |
2606 | |
2607 case VideoCodec::CODEC_ULPFEC: { | |
2608 // ULPFEC payload type, should not have duplicates. | |
2609 RTC_DCHECK_EQ(-1, ulpfec_config.ulpfec_payload_type); | |
2610 ulpfec_config.ulpfec_payload_type = in_codec.id; | |
2611 continue; | |
2612 } | |
2613 | |
2614 case VideoCodec::CODEC_FLEXFEC: { | |
2615 // FlexFEC payload type, should not have duplicates. | |
2616 RTC_DCHECK_EQ(-1, flexfec_payload_type); | |
2617 flexfec_payload_type = in_codec.id; | |
2618 continue; | |
2619 } | |
2620 | |
2621 case VideoCodec::CODEC_RTX: { | |
2622 int associated_payload_type; | |
2623 if (!in_codec.GetParam(kCodecParamAssociatedPayloadType, | |
2624 &associated_payload_type) || | |
2625 !IsValidRtpPayloadType(associated_payload_type)) { | |
2626 LOG(LS_ERROR) | |
2627 << "RTX codec with invalid or no associated payload type: " | |
2628 << in_codec.ToString(); | |
2629 return std::vector<VideoCodecSettings>(); | |
2630 } | |
2631 rtx_mapping[associated_payload_type] = in_codec.id; | |
2632 continue; | |
2633 } | |
2634 | |
2635 case VideoCodec::CODEC_VIDEO: | |
2636 break; | |
2637 } | |
2638 | |
2639 video_codecs.push_back(VideoCodecSettings()); | |
2640 video_codecs.back().codec = in_codec; | |
2641 } | |
2642 | |
2643 // One of these codecs should have been a video codec. Only having FEC | |
2644 // parameters into this code is a logic error. | |
2645 RTC_DCHECK(!video_codecs.empty()); | |
2646 | |
2647 for (std::map<int, int>::const_iterator it = rtx_mapping.begin(); | |
2648 it != rtx_mapping.end(); | |
2649 ++it) { | |
2650 if (!payload_used[it->first]) { | |
2651 LOG(LS_ERROR) << "RTX mapped to payload not in codec list."; | |
2652 return std::vector<VideoCodecSettings>(); | |
2653 } | |
2654 if (payload_codec_type[it->first] != VideoCodec::CODEC_VIDEO && | |
2655 payload_codec_type[it->first] != VideoCodec::CODEC_RED) { | |
2656 LOG(LS_ERROR) << "RTX not mapped to regular video codec or RED codec."; | |
2657 return std::vector<VideoCodecSettings>(); | |
2658 } | |
2659 | |
2660 if (it->first == ulpfec_config.red_payload_type) { | |
2661 ulpfec_config.red_rtx_payload_type = it->second; | |
2662 } | |
2663 } | |
2664 | |
2665 for (size_t i = 0; i < video_codecs.size(); ++i) { | |
2666 video_codecs[i].ulpfec = ulpfec_config; | |
2667 video_codecs[i].flexfec_payload_type = flexfec_payload_type; | |
2668 if (rtx_mapping[video_codecs[i].codec.id] != 0 && | |
2669 rtx_mapping[video_codecs[i].codec.id] != | |
2670 ulpfec_config.red_payload_type) { | |
2671 video_codecs[i].rtx_payload_type = rtx_mapping[video_codecs[i].codec.id]; | |
2672 } | |
2673 } | |
2674 | |
2675 return video_codecs; | |
2676 } | |
2677 | |
2678 } // namespace cricket | |
OLD | NEW |