blob: 765898685ccb293fde49193f732f14d682baf13f [file] [log] [blame]
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001/*
2 * libjingle
3 * Copyright 2014 Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#ifdef HAVE_WEBRTC_VIDEO
29#include "talk/media/webrtc/webrtcvideoengine2.h"
30
pbos@webrtc.orgc37e72e2015-01-05 18:51:13 +000031#include <algorithm>
pbos@webrtc.org3c107582014-07-20 15:27:35 +000032#include <set>
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +000033#include <string>
34
35#include "libyuv/convert_from.h"
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +000036#include "talk/media/base/videocapturer.h"
37#include "talk/media/base/videorenderer.h"
buildbot@webrtc.orgdf9bbbe2014-06-19 19:54:33 +000038#include "talk/media/webrtc/constants.h"
buildbot@webrtc.orga8530772014-12-10 09:01:18 +000039#include "talk/media/webrtc/simulcast.h"
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +000040#include "talk/media/webrtc/webrtcvideocapturer.h"
andresp@webrtc.org82775b12014-11-07 09:37:54 +000041#include "talk/media/webrtc/webrtcvideoengine.h"
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +000042#include "talk/media/webrtc/webrtcvideoframe.h"
43#include "talk/media/webrtc/webrtcvoiceengine.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000044#include "webrtc/base/buffer.h"
45#include "webrtc/base/logging.h"
46#include "webrtc/base/stringutils.h"
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +000047#include "webrtc/call.h"
pbos@webrtc.org50fe3592015-01-29 12:33:07 +000048#include "webrtc/system_wrappers/interface/trace_event.h"
pbos@webrtc.org776e6f22014-10-29 15:28:39 +000049#include "webrtc/video_decoder.h"
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000050#include "webrtc/video_encoder.h"
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +000051
52#define UNIMPLEMENTED \
53 LOG(LS_ERROR) << "Call to unimplemented function " << __FUNCTION__; \
54 ASSERT(false)
55
56namespace cricket {
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +000057namespace {
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +000058static std::string CodecVectorToString(const std::vector<VideoCodec>& codecs) {
59 std::stringstream out;
60 out << '{';
61 for (size_t i = 0; i < codecs.size(); ++i) {
62 out << codecs[i].ToString();
63 if (i != codecs.size() - 1) {
64 out << ", ";
65 }
66 }
67 out << '}';
68 return out.str();
69}
70
71static bool ValidateCodecFormats(const std::vector<VideoCodec>& codecs) {
72 bool has_video = false;
73 for (size_t i = 0; i < codecs.size(); ++i) {
74 if (!codecs[i].ValidateCodecFormat()) {
75 return false;
76 }
77 if (codecs[i].GetCodecType() == VideoCodec::CODEC_VIDEO) {
78 has_video = true;
79 }
80 }
81 if (!has_video) {
82 LOG(LS_ERROR) << "Setting codecs without a video codec is invalid: "
83 << CodecVectorToString(codecs);
84 return false;
85 }
86 return true;
87}
88
89static std::string RtpExtensionsToString(
90 const std::vector<RtpHeaderExtension>& extensions) {
91 std::stringstream out;
92 out << '{';
93 for (size_t i = 0; i < extensions.size(); ++i) {
94 out << "{" << extensions[i].uri << ": " << extensions[i].id << "}";
95 if (i != extensions.size() - 1) {
96 out << ", ";
97 }
98 }
99 out << '}';
100 return out.str();
101}
102
andresp@webrtc.org82775b12014-11-07 09:37:54 +0000103// Merges two fec configs and logs an error if a conflict arises
104// such that merging in diferent order would trigger a diferent output.
105static void MergeFecConfig(const webrtc::FecConfig& other,
106 webrtc::FecConfig* output) {
107 if (other.ulpfec_payload_type != -1) {
108 if (output->ulpfec_payload_type != -1 &&
109 output->ulpfec_payload_type != other.ulpfec_payload_type) {
110 LOG(LS_WARNING) << "Conflict merging ulpfec_payload_type configs: "
111 << output->ulpfec_payload_type << " and "
112 << other.ulpfec_payload_type;
113 }
114 output->ulpfec_payload_type = other.ulpfec_payload_type;
115 }
116 if (other.red_payload_type != -1) {
117 if (output->red_payload_type != -1 &&
118 output->red_payload_type != other.red_payload_type) {
119 LOG(LS_WARNING) << "Conflict merging red_payload_type configs: "
120 << output->red_payload_type << " and "
121 << other.red_payload_type;
122 }
123 output->red_payload_type = other.red_payload_type;
124 }
125}
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000126} // namespace
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000127
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000128// This constant is really an on/off, lower-level configurable NACK history
129// duration hasn't been implemented.
130static const int kNackHistoryMs = 1000;
131
buildbot@webrtc.org933d88a2014-09-18 20:23:05 +0000132static const int kDefaultQpMax = 56;
133
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000134static const int kDefaultRtcpReceiverReportSsrc = 1;
135
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000136// External video encoders are given payloads 120-127. This also means that we
137// only support up to 8 external payload types.
138static const int kExternalVideoPayloadTypeBase = 120;
139#ifndef NDEBUG
140static const size_t kMaxExternalVideoCodecs = 8;
141#endif
142
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000143const char kH264CodecName[] = "H264";
144
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000145static bool FindFirstMatchingCodec(const std::vector<VideoCodec>& codecs,
146 const VideoCodec& requested_codec,
147 VideoCodec* matching_codec) {
148 for (size_t i = 0; i < codecs.size(); ++i) {
149 if (requested_codec.Matches(codecs[i])) {
150 *matching_codec = codecs[i];
151 return true;
152 }
153 }
154 return false;
155}
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000156
pbos@webrtc.org3c107582014-07-20 15:27:35 +0000157static bool ValidateRtpHeaderExtensionIds(
158 const std::vector<RtpHeaderExtension>& extensions) {
159 std::set<int> extensions_used;
160 for (size_t i = 0; i < extensions.size(); ++i) {
161 if (extensions[i].id < 0 || extensions[i].id >= 15 ||
162 !extensions_used.insert(extensions[i].id).second) {
163 LOG(LS_ERROR) << "RTP extensions are with incorrect or duplicate ids.";
164 return false;
165 }
166 }
167 return true;
168}
169
pbos@webrtc.orgc37e72e2015-01-05 18:51:13 +0000170static bool CompareRtpHeaderExtensionIds(
171 const webrtc::RtpExtension& extension1,
172 const webrtc::RtpExtension& extension2) {
173 // Sorting on ID is sufficient, more than one extension per ID is unsupported.
174 return extension1.id > extension2.id;
175}
176
pbos@webrtc.org3c107582014-07-20 15:27:35 +0000177static std::vector<webrtc::RtpExtension> FilterRtpExtensions(
178 const std::vector<RtpHeaderExtension>& extensions) {
179 std::vector<webrtc::RtpExtension> webrtc_extensions;
180 for (size_t i = 0; i < extensions.size(); ++i) {
181 // Unsupported extensions will be ignored.
182 if (webrtc::RtpExtension::IsSupported(extensions[i].uri)) {
183 webrtc_extensions.push_back(webrtc::RtpExtension(
184 extensions[i].uri, extensions[i].id));
185 } else {
186 LOG(LS_WARNING) << "Unsupported RTP extension: " << extensions[i].uri;
187 }
188 }
pbos@webrtc.orgc37e72e2015-01-05 18:51:13 +0000189
190 // Sort filtered headers to make sure that they can later be compared
191 // regardless of in which order they were entered.
192 std::sort(webrtc_extensions.begin(), webrtc_extensions.end(),
193 CompareRtpHeaderExtensionIds);
pbos@webrtc.org3c107582014-07-20 15:27:35 +0000194 return webrtc_extensions;
195}
196
pbos@webrtc.orgc37e72e2015-01-05 18:51:13 +0000197static bool RtpExtensionsHaveChanged(
198 const std::vector<webrtc::RtpExtension>& before,
199 const std::vector<webrtc::RtpExtension>& after) {
200 if (before.size() != after.size())
201 return true;
202 for (size_t i = 0; i < before.size(); ++i) {
203 if (before[i].id != after[i].id)
204 return true;
205 if (before[i].name != after[i].name)
206 return true;
207 }
208 return false;
209}
210
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000211std::vector<webrtc::VideoStream>
pbos@webrtc.orgf1c8b902015-01-14 17:29:27 +0000212WebRtcVideoChannel2::WebRtcVideoSendStream::CreateSimulcastVideoStreams(
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000213 const VideoCodec& codec,
214 const VideoOptions& options,
215 size_t num_streams) {
216 // Use default factory for non-simulcast.
217 int max_qp = kDefaultQpMax;
218 codec.GetParam(kCodecParamMaxQuantization, &max_qp);
219
220 int min_bitrate_kbps;
221 if (!codec.GetParam(kCodecParamMinBitrate, &min_bitrate_kbps) ||
222 min_bitrate_kbps < kMinVideoBitrate) {
223 min_bitrate_kbps = kMinVideoBitrate;
224 }
225
226 int max_bitrate_kbps;
227 if (!codec.GetParam(kCodecParamMaxBitrate, &max_bitrate_kbps)) {
228 max_bitrate_kbps = 0;
229 }
230
231 return GetSimulcastConfig(
232 num_streams,
233 GetSimulcastBitrateMode(options),
234 codec.width,
235 codec.height,
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000236 max_bitrate_kbps * 1000,
237 max_qp,
238 codec.framerate != 0 ? codec.framerate : kDefaultVideoMaxFramerate);
239}
240
pbos@webrtc.orgf1c8b902015-01-14 17:29:27 +0000241std::vector<webrtc::VideoStream>
242WebRtcVideoChannel2::WebRtcVideoSendStream::CreateVideoStreams(
buildbot@webrtc.orgd41eaeb2014-06-12 07:13:26 +0000243 const VideoCodec& codec,
244 const VideoOptions& options,
245 size_t num_streams) {
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000246 if (num_streams != 1)
247 return CreateSimulcastVideoStreams(codec, options, num_streams);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000248
buildbot@webrtc.orgd41eaeb2014-06-12 07:13:26 +0000249 webrtc::VideoStream stream;
250 stream.width = codec.width;
251 stream.height = codec.height;
252 stream.max_framerate =
andresp@webrtc.org82775b12014-11-07 09:37:54 +0000253 codec.framerate != 0 ? codec.framerate : kDefaultVideoMaxFramerate;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000254
pbos@webrtc.org00873182014-11-25 14:03:34 +0000255 stream.min_bitrate_bps = kMinVideoBitrate * 1000;
256 stream.target_bitrate_bps = stream.max_bitrate_bps = kMaxVideoBitrate * 1000;
buildbot@webrtc.orgd41eaeb2014-06-12 07:13:26 +0000257
buildbot@webrtc.org933d88a2014-09-18 20:23:05 +0000258 int max_qp = kDefaultQpMax;
buildbot@webrtc.orgd41eaeb2014-06-12 07:13:26 +0000259 codec.GetParam(kCodecParamMaxQuantization, &max_qp);
260 stream.max_qp = max_qp;
261 std::vector<webrtc::VideoStream> streams;
262 streams.push_back(stream);
263 return streams;
264}
265
pbos@webrtc.orgf1c8b902015-01-14 17:29:27 +0000266void* WebRtcVideoChannel2::WebRtcVideoSendStream::ConfigureVideoEncoderSettings(
pbos@webrtc.org6f48f1b2014-07-22 16:29:54 +0000267 const VideoCodec& codec,
268 const VideoOptions& options) {
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000269 if (CodecNameMatches(codec.name, kVp8CodecName)) {
pbos@webrtc.orgf1c8b902015-01-14 17:29:27 +0000270 encoder_settings_.vp8 = webrtc::VideoEncoder::GetDefaultVp8Settings();
271 options.video_noise_reduction.Get(&encoder_settings_.vp8.denoisingOn);
272 return &encoder_settings_.vp8;
pbos@webrtc.org6f48f1b2014-07-22 16:29:54 +0000273 }
andresp@webrtc.org188d3b22014-11-07 13:21:04 +0000274 if (CodecNameMatches(codec.name, kVp9CodecName)) {
pbos@webrtc.orgf1c8b902015-01-14 17:29:27 +0000275 encoder_settings_.vp9 = webrtc::VideoEncoder::GetDefaultVp9Settings();
276 options.video_noise_reduction.Get(&encoder_settings_.vp9.denoisingOn);
277 return &encoder_settings_.vp9;
andresp@webrtc.org188d3b22014-11-07 13:21:04 +0000278 }
pbos@webrtc.org6f48f1b2014-07-22 16:29:54 +0000279 return NULL;
280}
281
pbos@webrtc.orgafb554f42014-08-12 23:17:13 +0000282DefaultUnsignalledSsrcHandler::DefaultUnsignalledSsrcHandler()
283 : default_recv_ssrc_(0), default_renderer_(NULL) {}
284
285UnsignalledSsrcHandler::Action DefaultUnsignalledSsrcHandler::OnUnsignalledSsrc(
286 VideoMediaChannel* channel,
287 uint32_t ssrc) {
288 if (default_recv_ssrc_ != 0) { // Already one default stream.
289 LOG(LS_WARNING) << "Unknown SSRC, but default receive stream already set.";
290 return kDropPacket;
291 }
292
293 StreamParams sp;
294 sp.ssrcs.push_back(ssrc);
295 LOG(LS_INFO) << "Creating default receive stream for SSRC=" << ssrc << ".";
296 if (!channel->AddRecvStream(sp)) {
297 LOG(LS_WARNING) << "Could not create default receive stream.";
298 }
299
300 channel->SetRenderer(ssrc, default_renderer_);
301 default_recv_ssrc_ = ssrc;
302 return kDeliverPacket;
303}
304
pbos@webrtc.org42684be2014-10-03 11:25:45 +0000305WebRtcCallFactory::~WebRtcCallFactory() {
306}
307webrtc::Call* WebRtcCallFactory::CreateCall(
308 const webrtc::Call::Config& config) {
309 return webrtc::Call::Create(config);
310}
311
pbos@webrtc.orgafb554f42014-08-12 23:17:13 +0000312VideoRenderer* DefaultUnsignalledSsrcHandler::GetDefaultRenderer() const {
313 return default_renderer_;
314}
315
316void DefaultUnsignalledSsrcHandler::SetDefaultRenderer(
317 VideoMediaChannel* channel,
318 VideoRenderer* renderer) {
319 default_renderer_ = renderer;
320 if (default_recv_ssrc_ != 0) {
321 channel->SetRenderer(default_recv_ssrc_, default_renderer_);
322 }
323}
324
pbos@webrtc.org97fdeb82014-08-22 10:36:23 +0000325WebRtcVideoEngine2::WebRtcVideoEngine2()
pbos@webrtc.orgb648b9d2014-08-26 11:08:06 +0000326 : worker_thread_(NULL),
327 voice_engine_(NULL),
andresp@webrtc.org82775b12014-11-07 09:37:54 +0000328 default_codec_format_(kDefaultVideoMaxWidth,
329 kDefaultVideoMaxHeight,
330 FPS_TO_INTERVAL(kDefaultVideoMaxFramerate),
buildbot@webrtc.org992febb2014-09-05 16:39:08 +0000331 FOURCC_ANY),
pbos@webrtc.orgb648b9d2014-08-26 11:08:06 +0000332 initialized_(false),
pbos@webrtc.org42684be2014-10-03 11:25:45 +0000333 call_factory_(&default_call_factory_),
pbos@webrtc.org0a2087a2014-09-23 09:40:22 +0000334 external_decoder_factory_(NULL),
335 external_encoder_factory_(NULL) {
pbos@webrtc.orgb648b9d2014-08-26 11:08:06 +0000336 LOG(LS_INFO) << "WebRtcVideoEngine2::WebRtcVideoEngine2()";
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000337 video_codecs_ = GetSupportedCodecs();
pbos@webrtc.org587ef602014-06-16 17:32:02 +0000338 rtp_header_extensions_.push_back(
339 RtpHeaderExtension(kRtpTimestampOffsetHeaderExtension,
340 kRtpTimestampOffsetHeaderExtensionDefaultId));
341 rtp_header_extensions_.push_back(
342 RtpHeaderExtension(kRtpAbsoluteSenderTimeHeaderExtension,
343 kRtpAbsoluteSenderTimeHeaderExtensionDefaultId));
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000344}
345
346WebRtcVideoEngine2::~WebRtcVideoEngine2() {
347 LOG(LS_INFO) << "WebRtcVideoEngine2::~WebRtcVideoEngine2";
348
349 if (initialized_) {
350 Terminate();
351 }
352}
353
pbos@webrtc.org42684be2014-10-03 11:25:45 +0000354void WebRtcVideoEngine2::SetCallFactory(WebRtcCallFactory* call_factory) {
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000355 assert(!initialized_);
pbos@webrtc.org42684be2014-10-03 11:25:45 +0000356 call_factory_ = call_factory;
357}
358
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000359bool WebRtcVideoEngine2::Init(rtc::Thread* worker_thread) {
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000360 LOG(LS_INFO) << "WebRtcVideoEngine2::Init";
361 worker_thread_ = worker_thread;
362 ASSERT(worker_thread_ != NULL);
363
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000364 initialized_ = true;
365 return true;
366}
367
368void WebRtcVideoEngine2::Terminate() {
369 LOG(LS_INFO) << "WebRtcVideoEngine2::Terminate";
370
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000371 initialized_ = false;
372}
373
374int WebRtcVideoEngine2::GetCapabilities() { return VIDEO_RECV | VIDEO_SEND; }
375
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000376bool WebRtcVideoEngine2::SetDefaultEncoderConfig(
377 const VideoEncoderConfig& config) {
pbos@webrtc.org8fdeee62014-07-20 14:40:23 +0000378 const VideoCodec& codec = config.max_codec;
pbos@webrtc.org957e8022014-11-10 12:36:11 +0000379 bool supports_codec = false;
380 for (size_t i = 0; i < video_codecs_.size(); ++i) {
381 if (CodecNameMatches(video_codecs_[i].name, codec.name)) {
382 video_codecs_[i] = codec;
383 supports_codec = true;
384 break;
385 }
386 }
387
388 if (!supports_codec) {
389 LOG(LS_ERROR) << "SetDefaultEncoderConfig, codec not supported: "
pbos@webrtc.org8fdeee62014-07-20 14:40:23 +0000390 << codec.ToString();
391 return false;
392 }
393
buildbot@webrtc.org992febb2014-09-05 16:39:08 +0000394 default_codec_format_ =
395 VideoFormat(codec.width,
396 codec.height,
397 VideoFormat::FpsToInterval(codec.framerate),
398 FOURCC_ANY);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000399 return true;
400}
401
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000402WebRtcVideoChannel2* WebRtcVideoEngine2::CreateChannel(
buildbot@webrtc.org1ecbe452014-10-14 20:29:28 +0000403 const VideoOptions& options,
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000404 VoiceMediaChannel* voice_channel) {
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000405 assert(initialized_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000406 LOG(LS_INFO) << "CreateChannel: "
407 << (voice_channel != NULL ? "With" : "Without")
pbos@webrtc.orgfa553ef2014-10-20 11:07:07 +0000408 << " voice channel. Options: " << options.ToString();
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000409 WebRtcVideoChannel2* channel =
410 new WebRtcVideoChannel2(call_factory_,
pbos@webrtc.org3bf3d232014-10-31 12:59:34 +0000411 voice_engine_,
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000412 voice_channel,
pbos@webrtc.orgfa553ef2014-10-20 11:07:07 +0000413 options,
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000414 external_encoder_factory_,
pbos@webrtc.orgf1c8b902015-01-14 17:29:27 +0000415 external_decoder_factory_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000416 if (!channel->Init()) {
417 delete channel;
418 return NULL;
419 }
pbos@webrtc.orge322a172014-06-13 11:47:28 +0000420 channel->SetRecvCodecs(video_codecs_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000421 return channel;
422}
423
424const std::vector<VideoCodec>& WebRtcVideoEngine2::codecs() const {
425 return video_codecs_;
426}
427
428const std::vector<RtpHeaderExtension>&
429WebRtcVideoEngine2::rtp_header_extensions() const {
430 return rtp_header_extensions_;
431}
432
433void WebRtcVideoEngine2::SetLogging(int min_sev, const char* filter) {
434 // TODO(pbos): Set up logging.
435 LOG(LS_VERBOSE) << "SetLogging: " << min_sev << '"' << filter << '"';
436 // if min_sev == -1, we keep the current log level.
437 if (min_sev < 0) {
438 assert(min_sev == -1);
439 return;
440 }
441}
442
pbos@webrtc.org0a2087a2014-09-23 09:40:22 +0000443void WebRtcVideoEngine2::SetExternalDecoderFactory(
444 WebRtcVideoDecoderFactory* decoder_factory) {
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000445 assert(!initialized_);
pbos@webrtc.org0a2087a2014-09-23 09:40:22 +0000446 external_decoder_factory_ = decoder_factory;
447}
448
449void WebRtcVideoEngine2::SetExternalEncoderFactory(
450 WebRtcVideoEncoderFactory* encoder_factory) {
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000451 assert(!initialized_);
pbos@webrtc.orgf18fba22015-01-14 16:26:23 +0000452 if (external_encoder_factory_ == encoder_factory)
453 return;
454
455 // No matter what happens we shouldn't hold on to a stale
456 // WebRtcSimulcastEncoderFactory.
457 simulcast_encoder_factory_.reset();
458
459 if (encoder_factory &&
460 WebRtcSimulcastEncoderFactory::UseSimulcastEncoderFactory(
461 encoder_factory->codecs())) {
462 simulcast_encoder_factory_.reset(
463 new WebRtcSimulcastEncoderFactory(encoder_factory));
464 encoder_factory = simulcast_encoder_factory_.get();
465 }
pbos@webrtc.org0a2087a2014-09-23 09:40:22 +0000466 external_encoder_factory_ = encoder_factory;
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000467
468 video_codecs_ = GetSupportedCodecs();
pbos@webrtc.org0a2087a2014-09-23 09:40:22 +0000469}
470
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000471bool WebRtcVideoEngine2::EnableTimedRender() {
472 // TODO(pbos): Figure out whether this can be removed.
473 return true;
474}
475
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000476// Checks to see whether we comprehend and could receive a particular codec
477bool WebRtcVideoEngine2::FindCodec(const VideoCodec& in) {
478 // TODO(pbos): Probe encoder factory to figure out that the codec is supported
479 // if supported by the encoder factory. Add a corresponding test that fails
480 // with this code (that doesn't ask the factory).
pbos@webrtc.org8fdeee62014-07-20 14:40:23 +0000481 for (size_t j = 0; j < video_codecs_.size(); ++j) {
482 VideoCodec codec(video_codecs_[j].id, video_codecs_[j].name, 0, 0, 0, 0);
483 if (codec.Matches(in)) {
484 return true;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000485 }
486 }
487 return false;
488}
489
490// Tells whether the |requested| codec can be transmitted or not. If it can be
491// transmitted |out| is set with the best settings supported. Aspect ratio will
492// be set as close to |current|'s as possible. If not set |requested|'s
493// dimensions will be used for aspect ratio matching.
494bool WebRtcVideoEngine2::CanSendCodec(const VideoCodec& requested,
495 const VideoCodec& current,
496 VideoCodec* out) {
497 assert(out != NULL);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000498
499 if (requested.width != requested.height &&
500 (requested.height == 0 || requested.width == 0)) {
501 // 0xn and nx0 are invalid resolutions.
502 return false;
503 }
504
505 VideoCodec matching_codec;
506 if (!FindFirstMatchingCodec(video_codecs_, requested, &matching_codec)) {
507 // Codec not supported.
508 return false;
509 }
510
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000511 out->id = requested.id;
512 out->name = requested.name;
513 out->preference = requested.preference;
514 out->params = requested.params;
andresp@webrtc.orgff689be2015-02-12 11:54:26 +0000515 out->framerate = std::min(requested.framerate, matching_codec.framerate);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000516 out->params = requested.params;
517 out->feedback_params = requested.feedback_params;
pbos@webrtc.org8fdeee62014-07-20 14:40:23 +0000518 out->width = requested.width;
519 out->height = requested.height;
520 if (requested.width == 0 && requested.height == 0) {
521 return true;
522 }
523
524 while (out->width > matching_codec.width) {
525 out->width /= 2;
526 out->height /= 2;
527 }
528
529 return out->width > 0 && out->height > 0;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000530}
531
532bool WebRtcVideoEngine2::SetVoiceEngine(WebRtcVoiceEngine* voice_engine) {
533 if (initialized_) {
534 LOG(LS_WARNING) << "SetVoiceEngine can not be called after Init";
535 return false;
536 }
537 voice_engine_ = voice_engine;
538 return true;
539}
540
541// Ignore spammy trace messages, mostly from the stats API when we haven't
542// gotten RTCP info yet from the remote side.
543bool WebRtcVideoEngine2::ShouldIgnoreTrace(const std::string& trace) {
544 static const char* const kTracesToIgnore[] = {NULL};
545 for (const char* const* p = kTracesToIgnore; *p; ++p) {
546 if (trace.find(*p) == 0) {
547 return true;
548 }
549 }
550 return false;
551}
552
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000553std::vector<VideoCodec> WebRtcVideoEngine2::GetSupportedCodecs() const {
andresp@webrtc.org82775b12014-11-07 09:37:54 +0000554 std::vector<VideoCodec> supported_codecs = DefaultVideoCodecList();
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000555
556 if (external_encoder_factory_ == NULL) {
557 return supported_codecs;
558 }
559
560 assert(external_encoder_factory_->codecs().size() <= kMaxExternalVideoCodecs);
561 const std::vector<WebRtcVideoEncoderFactory::VideoCodec>& codecs =
562 external_encoder_factory_->codecs();
563 for (size_t i = 0; i < codecs.size(); ++i) {
564 // Don't add internally-supported codecs twice.
565 if (CodecIsInternallySupported(codecs[i].name)) {
566 continue;
567 }
568
569 VideoCodec codec(kExternalVideoPayloadTypeBase + static_cast<int>(i),
570 codecs[i].name,
571 codecs[i].max_width,
572 codecs[i].max_height,
573 codecs[i].max_fps,
574 0);
575
576 AddDefaultFeedbackParams(&codec);
577 supported_codecs.push_back(codec);
578 }
579 return supported_codecs;
580}
581
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000582WebRtcVideoChannel2::WebRtcVideoChannel2(
pbos@webrtc.org42684be2014-10-03 11:25:45 +0000583 WebRtcCallFactory* call_factory,
pbos@webrtc.org3bf3d232014-10-31 12:59:34 +0000584 WebRtcVoiceEngine* voice_engine,
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000585 VoiceMediaChannel* voice_channel,
pbos@webrtc.orgfa553ef2014-10-20 11:07:07 +0000586 const VideoOptions& options,
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000587 WebRtcVideoEncoderFactory* external_encoder_factory,
pbos@webrtc.orgf1c8b902015-01-14 17:29:27 +0000588 WebRtcVideoDecoderFactory* external_decoder_factory)
pbos@webrtc.orgb648b9d2014-08-26 11:08:06 +0000589 : unsignalled_ssrc_handler_(&default_unsignalled_ssrc_handler_),
pbos@webrtc.org3bf3d232014-10-31 12:59:34 +0000590 voice_channel_(voice_channel),
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000591 external_encoder_factory_(external_encoder_factory),
pbos@webrtc.orgf1c8b902015-01-14 17:29:27 +0000592 external_decoder_factory_(external_decoder_factory) {
pbos@webrtc.orgfa553ef2014-10-20 11:07:07 +0000593 SetDefaultOptions();
594 options_.SetAll(options);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000595 webrtc::Call::Config config(this);
pbos@webrtc.org42684be2014-10-03 11:25:45 +0000596 config.overuse_callback = this;
pbos@webrtc.org3bf3d232014-10-31 12:59:34 +0000597 if (voice_engine != NULL) {
598 config.voice_engine = voice_engine->voe()->engine();
599 }
pbos@webrtc.orgfa553ef2014-10-20 11:07:07 +0000600
pbos@webrtc.org42684be2014-10-03 11:25:45 +0000601 call_.reset(call_factory->CreateCall(config));
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000602
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000603 rtcp_receiver_report_ssrc_ = kDefaultRtcpReceiverReportSsrc;
604 sending_ = false;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000605 default_send_ssrc_ = 0;
pbos@webrtc.org6f48f1b2014-07-22 16:29:54 +0000606}
607
608void WebRtcVideoChannel2::SetDefaultOptions() {
pbos@webrtc.org42684be2014-10-03 11:25:45 +0000609 options_.cpu_overuse_detection.Set(false);
pbos@webrtc.orgd8198032014-11-10 14:41:43 +0000610 options_.dscp.Set(false);
pbos@webrtc.org5ff71ab2014-07-23 07:28:56 +0000611 options_.suspend_below_min_bitrate.Set(false);
pbos@webrtc.org42684be2014-10-03 11:25:45 +0000612 options_.video_noise_reduction.Set(true);
pbos@webrtc.orgefc82c22014-10-27 13:58:00 +0000613 options_.screencast_min_bitrate.Set(0);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000614}
615
616WebRtcVideoChannel2::~WebRtcVideoChannel2() {
617 for (std::map<uint32, WebRtcVideoSendStream*>::iterator it =
618 send_streams_.begin();
619 it != send_streams_.end();
620 ++it) {
621 delete it->second;
622 }
623
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000624 for (std::map<uint32, WebRtcVideoReceiveStream*>::iterator it =
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000625 receive_streams_.begin();
626 it != receive_streams_.end();
627 ++it) {
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000628 delete it->second;
629 }
630}
631
632bool WebRtcVideoChannel2::Init() { return true; }
633
pbos@webrtc.org96a93252014-11-03 14:46:44 +0000634bool WebRtcVideoChannel2::CodecIsExternallySupported(
635 const std::string& name) const {
636 if (external_encoder_factory_ == NULL) {
637 return false;
638 }
639
640 const std::vector<WebRtcVideoEncoderFactory::VideoCodec> external_codecs =
641 external_encoder_factory_->codecs();
642 for (size_t c = 0; c < external_codecs.size(); ++c) {
643 if (CodecNameMatches(name, external_codecs[c].name)) {
644 return true;
645 }
646 }
647 return false;
648}
649
650std::vector<WebRtcVideoChannel2::VideoCodecSettings>
651WebRtcVideoChannel2::FilterSupportedCodecs(
652 const std::vector<WebRtcVideoChannel2::VideoCodecSettings>& mapped_codecs)
653 const {
654 std::vector<VideoCodecSettings> supported_codecs;
655 for (size_t i = 0; i < mapped_codecs.size(); ++i) {
656 const VideoCodecSettings& codec = mapped_codecs[i];
657 if (CodecIsInternallySupported(codec.codec.name) ||
658 CodecIsExternallySupported(codec.codec.name)) {
659 supported_codecs.push_back(codec);
660 }
661 }
662 return supported_codecs;
663}
664
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000665bool WebRtcVideoChannel2::SetRecvCodecs(const std::vector<VideoCodec>& codecs) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000666 TRACE_EVENT0("webrtc", "WebRtcVideoChannel2::SetRecvCodecs");
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000667 LOG(LS_INFO) << "SetRecvCodecs: " << CodecVectorToString(codecs);
668 if (!ValidateCodecFormats(codecs)) {
669 return false;
670 }
671
672 const std::vector<VideoCodecSettings> mapped_codecs = MapCodecs(codecs);
673 if (mapped_codecs.empty()) {
pbos@webrtc.org96a93252014-11-03 14:46:44 +0000674 LOG(LS_ERROR) << "SetRecvCodecs called without any video codecs.";
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000675 return false;
676 }
677
pbos@webrtc.org96a93252014-11-03 14:46:44 +0000678 const std::vector<VideoCodecSettings> supported_codecs =
679 FilterSupportedCodecs(mapped_codecs);
680
681 if (mapped_codecs.size() != supported_codecs.size()) {
682 LOG(LS_ERROR) << "SetRecvCodecs called with unsupported video codecs.";
683 return false;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000684 }
685
pbos@webrtc.org96a93252014-11-03 14:46:44 +0000686 recv_codecs_ = supported_codecs;
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000687
pbos@webrtc.org575d1262014-10-08 14:48:08 +0000688 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000689 for (std::map<uint32, WebRtcVideoReceiveStream*>::iterator it =
690 receive_streams_.begin();
691 it != receive_streams_.end();
692 ++it) {
693 it->second->SetRecvCodecs(recv_codecs_);
694 }
695
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000696 return true;
697}
698
699bool WebRtcVideoChannel2::SetSendCodecs(const std::vector<VideoCodec>& codecs) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000700 TRACE_EVENT0("webrtc", "WebRtcVideoChannel2::SetSendCodecs");
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000701 LOG(LS_INFO) << "SetSendCodecs: " << CodecVectorToString(codecs);
702 if (!ValidateCodecFormats(codecs)) {
703 return false;
704 }
705
706 const std::vector<VideoCodecSettings> supported_codecs =
707 FilterSupportedCodecs(MapCodecs(codecs));
708
709 if (supported_codecs.empty()) {
710 LOG(LS_ERROR) << "No video codecs supported by encoder factory.";
711 return false;
712 }
713
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000714 LOG(LS_INFO) << "Using codec: " << supported_codecs.front().codec.ToString();
715
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +0000716 VideoCodecSettings old_codec;
717 if (send_codec_.Get(&old_codec) && supported_codecs.front() == old_codec) {
718 // Using same codec, avoid reconfiguring.
719 return true;
720 }
721
722 send_codec_.Set(supported_codecs.front());
723
pbos@webrtc.org575d1262014-10-08 14:48:08 +0000724 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000725 for (std::map<uint32, WebRtcVideoSendStream*>::iterator it =
726 send_streams_.begin();
727 it != send_streams_.end();
728 ++it) {
729 assert(it->second != NULL);
730 it->second->SetCodec(supported_codecs.front());
731 }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000732
pbos@webrtc.org00873182014-11-25 14:03:34 +0000733 VideoCodec codec = supported_codecs.front().codec;
734 int bitrate_kbps;
735 if (codec.GetParam(kCodecParamMinBitrate, &bitrate_kbps) &&
736 bitrate_kbps > 0) {
737 bitrate_config_.min_bitrate_bps = bitrate_kbps * 1000;
738 } else {
739 bitrate_config_.min_bitrate_bps = 0;
740 }
741 if (codec.GetParam(kCodecParamStartBitrate, &bitrate_kbps) &&
742 bitrate_kbps > 0) {
743 bitrate_config_.start_bitrate_bps = bitrate_kbps * 1000;
744 } else {
745 // Do not reconfigure start bitrate unless it's specified and positive.
746 bitrate_config_.start_bitrate_bps = -1;
747 }
748 if (codec.GetParam(kCodecParamMaxBitrate, &bitrate_kbps) &&
749 bitrate_kbps > 0) {
750 bitrate_config_.max_bitrate_bps = bitrate_kbps * 1000;
751 } else {
752 bitrate_config_.max_bitrate_bps = -1;
753 }
754 call_->SetBitrateConfig(bitrate_config_);
755
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000756 return true;
757}
758
759bool WebRtcVideoChannel2::GetSendCodec(VideoCodec* codec) {
760 VideoCodecSettings codec_settings;
761 if (!send_codec_.Get(&codec_settings)) {
762 LOG(LS_VERBOSE) << "GetSendCodec: No send codec set.";
763 return false;
764 }
765 *codec = codec_settings.codec;
766 return true;
767}
768
769bool WebRtcVideoChannel2::SetSendStreamFormat(uint32 ssrc,
770 const VideoFormat& format) {
771 LOG(LS_VERBOSE) << "SetSendStreamFormat:" << ssrc << " -> "
772 << format.ToString();
pbos@webrtc.org575d1262014-10-08 14:48:08 +0000773 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000774 if (send_streams_.find(ssrc) == send_streams_.end()) {
775 return false;
776 }
777 return send_streams_[ssrc]->SetVideoFormat(format);
778}
779
780bool WebRtcVideoChannel2::SetRender(bool render) {
781 // TODO(pbos): Implement. Or refactor away as it shouldn't be needed.
782 LOG(LS_VERBOSE) << "SetRender: " << (render ? "true" : "false");
783 return true;
784}
785
786bool WebRtcVideoChannel2::SetSend(bool send) {
787 LOG(LS_VERBOSE) << "SetSend: " << (send ? "true" : "false");
788 if (send && !send_codec_.IsSet()) {
789 LOG(LS_ERROR) << "SetSend(true) called before setting codec.";
790 return false;
791 }
792 if (send) {
793 StartAllSendStreams();
794 } else {
795 StopAllSendStreams();
796 }
797 sending_ = send;
798 return true;
799}
800
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000801bool WebRtcVideoChannel2::AddSendStream(const StreamParams& sp) {
802 LOG(LS_INFO) << "AddSendStream: " << sp.ToString();
803 if (sp.ssrcs.empty()) {
804 LOG(LS_ERROR) << "No SSRCs in stream parameters.";
805 return false;
806 }
807
808 uint32 ssrc = sp.first_ssrc();
809 assert(ssrc != 0);
810 // TODO(pbos): Make sure none of sp.ssrcs are used, not just the identifying
811 // ssrc.
pbos@webrtc.org575d1262014-10-08 14:48:08 +0000812 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000813 if (send_streams_.find(ssrc) != send_streams_.end()) {
814 LOG(LS_ERROR) << "Send stream with ssrc '" << ssrc << "' already exists.";
815 return false;
816 }
817
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +0000818 std::vector<uint32> primary_ssrcs;
819 sp.GetPrimarySsrcs(&primary_ssrcs);
820 std::vector<uint32> rtx_ssrcs;
821 sp.GetFidSsrcs(primary_ssrcs, &rtx_ssrcs);
822 if (!rtx_ssrcs.empty() && primary_ssrcs.size() != rtx_ssrcs.size()) {
823 LOG(LS_ERROR)
824 << "RTX SSRCs exist, but don't cover all SSRCs (unsupported): "
825 << sp.ToString();
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000826 return false;
827 }
828
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000829 WebRtcVideoSendStream* stream =
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000830 new WebRtcVideoSendStream(call_.get(),
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000831 external_encoder_factory_,
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000832 options_,
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +0000833 send_codec_,
834 sp,
835 send_rtp_extensions_);
836
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000837 send_streams_[ssrc] = stream;
838
839 if (rtcp_receiver_report_ssrc_ == kDefaultRtcpReceiverReportSsrc) {
840 rtcp_receiver_report_ssrc_ = ssrc;
841 }
842 if (default_send_ssrc_ == 0) {
843 default_send_ssrc_ = ssrc;
844 }
845 if (sending_) {
846 stream->Start();
847 }
848
849 return true;
850}
851
852bool WebRtcVideoChannel2::RemoveSendStream(uint32 ssrc) {
853 LOG(LS_INFO) << "RemoveSendStream: " << ssrc;
854
855 if (ssrc == 0) {
856 if (default_send_ssrc_ == 0) {
857 LOG(LS_ERROR) << "No default send stream active.";
858 return false;
859 }
860
861 LOG(LS_VERBOSE) << "Removing default stream: " << default_send_ssrc_;
862 ssrc = default_send_ssrc_;
863 }
864
pbos@webrtc.org575d1262014-10-08 14:48:08 +0000865 WebRtcVideoSendStream* removed_stream;
866 {
867 rtc::CritScope stream_lock(&stream_crit_);
868 std::map<uint32, WebRtcVideoSendStream*>::iterator it =
869 send_streams_.find(ssrc);
870 if (it == send_streams_.end()) {
871 return false;
872 }
873
874 removed_stream = it->second;
875 send_streams_.erase(it);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000876 }
877
pbos@webrtc.org575d1262014-10-08 14:48:08 +0000878 delete removed_stream;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000879
880 if (ssrc == default_send_ssrc_) {
881 default_send_ssrc_ = 0;
882 }
883
884 return true;
885}
886
887bool WebRtcVideoChannel2::AddRecvStream(const StreamParams& sp) {
888 LOG(LS_INFO) << "AddRecvStream: " << sp.ToString();
889 assert(sp.ssrcs.size() > 0);
890
891 uint32 ssrc = sp.first_ssrc();
892 assert(ssrc != 0); // TODO(pbos): Is this ever valid?
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000893
894 // TODO(pbos): Check if any of the SSRCs overlap.
pbos@webrtc.org575d1262014-10-08 14:48:08 +0000895 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000896 if (receive_streams_.find(ssrc) != receive_streams_.end()) {
897 LOG(LS_ERROR) << "Receive stream for SSRC " << ssrc << "already exists.";
898 return false;
899 }
900
pbos@webrtc.orgbd249bc2014-07-07 04:45:15 +0000901 webrtc::VideoReceiveStream::Config config;
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000902 ConfigureReceiverRtp(&config, sp);
pbos@webrtc.org3bf3d232014-10-31 12:59:34 +0000903
904 // Set up A/V sync if there is a VoiceChannel.
905 // TODO(pbos): The A/V is synched by the receiving channel. So we need to know
906 // the SSRC of the remote audio channel in order to sync the correct webrtc
907 // VoiceEngine channel. For now sync the first channel in non-conference to
908 // match existing behavior in WebRtcVideoEngine.
909 if (voice_channel_ != NULL && receive_streams_.empty() &&
910 !options_.conference_mode.GetWithDefaultIfUnset(false)) {
911 config.audio_channel_id =
912 static_cast<WebRtcVoiceMediaChannel*>(voice_channel_)->voe_channel();
913 }
914
pbos@webrtc.org776e6f22014-10-29 15:28:39 +0000915 receive_streams_[ssrc] = new WebRtcVideoReceiveStream(
916 call_.get(), external_decoder_factory_, config, recv_codecs_);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000917
918 return true;
919}
920
921void WebRtcVideoChannel2::ConfigureReceiverRtp(
922 webrtc::VideoReceiveStream::Config* config,
923 const StreamParams& sp) const {
924 uint32 ssrc = sp.first_ssrc();
925
926 config->rtp.remote_ssrc = ssrc;
927 config->rtp.local_ssrc = rtcp_receiver_report_ssrc_;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000928
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000929 config->rtp.extensions = recv_rtp_extensions_;
pbos@webrtc.org257e1302014-07-25 19:01:32 +0000930
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000931 // TODO(pbos): This protection is against setting the same local ssrc as
932 // remote which is not permitted by the lower-level API. RTCP requires a
933 // corresponding sender SSRC. Figure out what to do when we don't have
934 // (receive-only) or know a good local SSRC.
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000935 if (config->rtp.remote_ssrc == config->rtp.local_ssrc) {
936 if (config->rtp.local_ssrc != kDefaultRtcpReceiverReportSsrc) {
937 config->rtp.local_ssrc = kDefaultRtcpReceiverReportSsrc;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000938 } else {
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000939 config->rtp.local_ssrc = kDefaultRtcpReceiverReportSsrc + 1;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000940 }
941 }
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000942
943 for (size_t i = 0; i < recv_codecs_.size(); ++i) {
andresp@webrtc.org82775b12014-11-07 09:37:54 +0000944 MergeFecConfig(recv_codecs_[i].fec, &config->rtp.fec);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000945 }
946
andresp@webrtc.org82775b12014-11-07 09:37:54 +0000947 for (size_t i = 0; i < recv_codecs_.size(); ++i) {
948 uint32 rtx_ssrc;
949 if (recv_codecs_[i].rtx_payload_type != -1 &&
950 sp.GetFidSsrc(ssrc, &rtx_ssrc)) {
951 webrtc::VideoReceiveStream::Config::Rtp::Rtx& rtx =
952 config->rtp.rtx[recv_codecs_[i].codec.id];
953 rtx.ssrc = rtx_ssrc;
954 rtx.payload_type = recv_codecs_[i].rtx_payload_type;
955 }
956 }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000957}
958
959bool WebRtcVideoChannel2::RemoveRecvStream(uint32 ssrc) {
960 LOG(LS_INFO) << "RemoveRecvStream: " << ssrc;
961 if (ssrc == 0) {
pbos@webrtc.orgafb554f42014-08-12 23:17:13 +0000962 LOG(LS_ERROR) << "RemoveRecvStream with 0 ssrc is not supported.";
963 return false;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000964 }
965
pbos@webrtc.org575d1262014-10-08 14:48:08 +0000966 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000967 std::map<uint32, WebRtcVideoReceiveStream*>::iterator stream =
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000968 receive_streams_.find(ssrc);
969 if (stream == receive_streams_.end()) {
970 LOG(LS_ERROR) << "Stream not found for ssrc: " << ssrc;
971 return false;
972 }
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000973 delete stream->second;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000974 receive_streams_.erase(stream);
975
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000976 return true;
977}
978
979bool WebRtcVideoChannel2::SetRenderer(uint32 ssrc, VideoRenderer* renderer) {
980 LOG(LS_INFO) << "SetRenderer: ssrc:" << ssrc << " "
981 << (renderer ? "(ptr)" : "NULL");
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000982 if (ssrc == 0) {
pbos@webrtc.orgafb554f42014-08-12 23:17:13 +0000983 default_unsignalled_ssrc_handler_.SetDefaultRenderer(this, renderer);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000984 return true;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000985 }
986
pbos@webrtc.org575d1262014-10-08 14:48:08 +0000987 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000988 std::map<uint32, WebRtcVideoReceiveStream*>::iterator it =
989 receive_streams_.find(ssrc);
990 if (it == receive_streams_.end()) {
991 return false;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000992 }
993
994 it->second->SetRenderer(renderer);
995 return true;
996}
997
998bool WebRtcVideoChannel2::GetRenderer(uint32 ssrc, VideoRenderer** renderer) {
999 if (ssrc == 0) {
pbos@webrtc.orgafb554f42014-08-12 23:17:13 +00001000 *renderer = default_unsignalled_ssrc_handler_.GetDefaultRenderer();
1001 return *renderer != NULL;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001002 }
1003
pbos@webrtc.org575d1262014-10-08 14:48:08 +00001004 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001005 std::map<uint32, WebRtcVideoReceiveStream*>::iterator it =
1006 receive_streams_.find(ssrc);
1007 if (it == receive_streams_.end()) {
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001008 return false;
1009 }
1010 *renderer = it->second->GetRenderer();
1011 return true;
1012}
1013
1014bool WebRtcVideoChannel2::GetStats(const StatsOptions& options,
1015 VideoMediaInfo* info) {
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00001016 info->Clear();
1017 FillSenderStats(info);
1018 FillReceiverStats(info);
pbos@webrtc.org2b19f062014-12-11 13:26:09 +00001019 webrtc::Call::Stats stats = call_->GetStats();
1020 FillBandwidthEstimationStats(stats, info);
1021 if (stats.rtt_ms != -1) {
1022 for (size_t i = 0; i < info->senders.size(); ++i) {
1023 info->senders[i].rtt_ms = stats.rtt_ms;
1024 }
1025 }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001026 return true;
1027}
1028
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00001029void WebRtcVideoChannel2::FillSenderStats(VideoMediaInfo* video_media_info) {
pbos@webrtc.org575d1262014-10-08 14:48:08 +00001030 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00001031 for (std::map<uint32, WebRtcVideoSendStream*>::iterator it =
1032 send_streams_.begin();
1033 it != send_streams_.end();
1034 ++it) {
1035 video_media_info->senders.push_back(it->second->GetVideoSenderInfo());
1036 }
1037}
1038
1039void WebRtcVideoChannel2::FillReceiverStats(VideoMediaInfo* video_media_info) {
pbos@webrtc.org575d1262014-10-08 14:48:08 +00001040 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00001041 for (std::map<uint32, WebRtcVideoReceiveStream*>::iterator it =
1042 receive_streams_.begin();
1043 it != receive_streams_.end();
1044 ++it) {
1045 video_media_info->receivers.push_back(it->second->GetVideoReceiverInfo());
1046 }
1047}
1048
1049void WebRtcVideoChannel2::FillBandwidthEstimationStats(
pbos@webrtc.org2b19f062014-12-11 13:26:09 +00001050 const webrtc::Call::Stats& stats,
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00001051 VideoMediaInfo* video_media_info) {
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001052 BandwidthEstimationInfo bwe_info;
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001053 bwe_info.available_send_bandwidth = stats.send_bandwidth_bps;
1054 bwe_info.available_recv_bandwidth = stats.recv_bandwidth_bps;
1055 bwe_info.bucket_delay = stats.pacer_delay_ms;
1056
1057 // Get send stream bitrate stats.
1058 rtc::CritScope stream_lock(&stream_crit_);
1059 for (std::map<uint32, WebRtcVideoSendStream*>::iterator stream =
1060 send_streams_.begin();
1061 stream != send_streams_.end();
1062 ++stream) {
1063 stream->second->FillBandwidthEstimationInfo(&bwe_info);
1064 }
1065 video_media_info->bw_estimations.push_back(bwe_info);
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00001066}
1067
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001068bool WebRtcVideoChannel2::SetCapturer(uint32 ssrc, VideoCapturer* capturer) {
1069 LOG(LS_INFO) << "SetCapturer: " << ssrc << " -> "
1070 << (capturer != NULL ? "(capturer)" : "NULL");
1071 assert(ssrc != 0);
pbos@webrtc.org575d1262014-10-08 14:48:08 +00001072 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001073 if (send_streams_.find(ssrc) == send_streams_.end()) {
1074 LOG(LS_ERROR) << "No sending stream on ssrc " << ssrc;
1075 return false;
1076 }
1077 return send_streams_[ssrc]->SetCapturer(capturer);
1078}
1079
1080bool WebRtcVideoChannel2::SendIntraFrame() {
1081 // TODO(pbos): Implement.
1082 LOG(LS_VERBOSE) << "SendIntraFrame().";
1083 return true;
1084}
1085
1086bool WebRtcVideoChannel2::RequestIntraFrame() {
1087 // TODO(pbos): Implement.
1088 LOG(LS_VERBOSE) << "SendIntraFrame().";
1089 return true;
1090}
1091
1092void WebRtcVideoChannel2::OnPacketReceived(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001093 rtc::Buffer* packet,
1094 const rtc::PacketTime& packet_time) {
pbos@webrtc.org4e545cc2014-05-14 13:58:13 +00001095 const webrtc::PacketReceiver::DeliveryStatus delivery_result =
1096 call_->Receiver()->DeliverPacket(
1097 reinterpret_cast<const uint8_t*>(packet->data()), packet->length());
1098 switch (delivery_result) {
1099 case webrtc::PacketReceiver::DELIVERY_OK:
1100 return;
1101 case webrtc::PacketReceiver::DELIVERY_PACKET_ERROR:
1102 return;
1103 case webrtc::PacketReceiver::DELIVERY_UNKNOWN_SSRC:
1104 break;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001105 }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001106
1107 uint32 ssrc = 0;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001108 if (!GetRtpSsrc(packet->data(), packet->length(), &ssrc)) {
1109 return;
1110 }
1111
pbos@webrtc.orgafb554f42014-08-12 23:17:13 +00001112 // TODO(pbos): Make sure that the unsignalled SSRC uses the video payload.
1113 // Also figure out whether RTX needs to be handled.
1114 switch (unsignalled_ssrc_handler_->OnUnsignalledSsrc(this, ssrc)) {
1115 case UnsignalledSsrcHandler::kDropPacket:
1116 return;
1117 case UnsignalledSsrcHandler::kDeliverPacket:
1118 break;
1119 }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001120
pbos@webrtc.org1e019d12014-05-16 11:38:45 +00001121 if (call_->Receiver()->DeliverPacket(
1122 reinterpret_cast<const uint8_t*>(packet->data()), packet->length()) !=
1123 webrtc::PacketReceiver::DELIVERY_OK) {
pbos@webrtc.orgafb554f42014-08-12 23:17:13 +00001124 LOG(LS_WARNING) << "Failed to deliver RTP packet on re-delivery.";
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001125 return;
1126 }
1127}
1128
1129void WebRtcVideoChannel2::OnRtcpReceived(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001130 rtc::Buffer* packet,
1131 const rtc::PacketTime& packet_time) {
pbos@webrtc.org1e019d12014-05-16 11:38:45 +00001132 if (call_->Receiver()->DeliverPacket(
1133 reinterpret_cast<const uint8_t*>(packet->data()), packet->length()) !=
1134 webrtc::PacketReceiver::DELIVERY_OK) {
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001135 LOG(LS_WARNING) << "Failed to deliver RTCP packet.";
1136 }
1137}
1138
1139void WebRtcVideoChannel2::OnReadyToSend(bool ready) {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +00001140 LOG(LS_VERBOSE) << "OnReadyToSend: " << (ready ? "Ready." : "Not ready.");
1141 call_->SignalNetworkState(ready ? webrtc::Call::kNetworkUp
1142 : webrtc::Call::kNetworkDown);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001143}
1144
1145bool WebRtcVideoChannel2::MuteStream(uint32 ssrc, bool mute) {
1146 LOG(LS_VERBOSE) << "MuteStream: " << ssrc << " -> "
1147 << (mute ? "mute" : "unmute");
1148 assert(ssrc != 0);
pbos@webrtc.org575d1262014-10-08 14:48:08 +00001149 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001150 if (send_streams_.find(ssrc) == send_streams_.end()) {
1151 LOG(LS_ERROR) << "No sending stream on ssrc " << ssrc;
1152 return false;
1153 }
pbos@webrtc.orgef8bb8d2014-08-13 21:36:18 +00001154
1155 send_streams_[ssrc]->MuteStream(mute);
1156 return true;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001157}
1158
1159bool WebRtcVideoChannel2::SetRecvRtpHeaderExtensions(
1160 const std::vector<RtpHeaderExtension>& extensions) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +00001161 TRACE_EVENT0("webrtc", "WebRtcVideoChannel2::SetRecvRtpHeaderExtensions");
pbos@webrtc.org587ef602014-06-16 17:32:02 +00001162 LOG(LS_INFO) << "SetRecvRtpHeaderExtensions: "
1163 << RtpExtensionsToString(extensions);
pbos@webrtc.org3c107582014-07-20 15:27:35 +00001164 if (!ValidateRtpHeaderExtensionIds(extensions))
1165 return false;
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001166
pbos@webrtc.orgc37e72e2015-01-05 18:51:13 +00001167 std::vector<webrtc::RtpExtension> filtered_extensions =
1168 FilterRtpExtensions(extensions);
1169 if (!RtpExtensionsHaveChanged(recv_rtp_extensions_, filtered_extensions))
1170 return true;
1171
1172 recv_rtp_extensions_ = filtered_extensions;
1173
pbos@webrtc.org575d1262014-10-08 14:48:08 +00001174 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001175 for (std::map<uint32, WebRtcVideoReceiveStream*>::iterator it =
1176 receive_streams_.begin();
1177 it != receive_streams_.end();
1178 ++it) {
1179 it->second->SetRtpExtensions(recv_rtp_extensions_);
1180 }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001181 return true;
1182}
1183
1184bool WebRtcVideoChannel2::SetSendRtpHeaderExtensions(
1185 const std::vector<RtpHeaderExtension>& extensions) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +00001186 TRACE_EVENT0("webrtc", "WebRtcVideoChannel2::SetSendRtpHeaderExtensions");
pbos@webrtc.org587ef602014-06-16 17:32:02 +00001187 LOG(LS_INFO) << "SetSendRtpHeaderExtensions: "
1188 << RtpExtensionsToString(extensions);
pbos@webrtc.org3c107582014-07-20 15:27:35 +00001189 if (!ValidateRtpHeaderExtensionIds(extensions))
1190 return false;
1191
pbos@webrtc.orgc37e72e2015-01-05 18:51:13 +00001192 std::vector<webrtc::RtpExtension> filtered_extensions =
1193 FilterRtpExtensions(extensions);
1194 if (!RtpExtensionsHaveChanged(send_rtp_extensions_, filtered_extensions))
1195 return true;
1196
1197 send_rtp_extensions_ = filtered_extensions;
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +00001198
pbos@webrtc.org575d1262014-10-08 14:48:08 +00001199 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001200 for (std::map<uint32, WebRtcVideoSendStream*>::iterator it =
1201 send_streams_.begin();
1202 it != send_streams_.end();
1203 ++it) {
1204 it->second->SetRtpExtensions(send_rtp_extensions_);
1205 }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001206 return true;
1207}
1208
pbos@webrtc.org00873182014-11-25 14:03:34 +00001209bool WebRtcVideoChannel2::SetMaxSendBandwidth(int max_bitrate_bps) {
1210 LOG(LS_INFO) << "SetMaxSendBandwidth: " << max_bitrate_bps << "bps.";
1211 if (max_bitrate_bps <= 0) {
1212 // Unsetting max bitrate.
1213 max_bitrate_bps = -1;
1214 }
1215 bitrate_config_.start_bitrate_bps = -1;
1216 bitrate_config_.max_bitrate_bps = max_bitrate_bps;
1217 if (max_bitrate_bps > 0 &&
1218 bitrate_config_.min_bitrate_bps > max_bitrate_bps) {
1219 bitrate_config_.min_bitrate_bps = max_bitrate_bps;
1220 }
1221 call_->SetBitrateConfig(bitrate_config_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001222 return true;
1223}
1224
1225bool WebRtcVideoChannel2::SetOptions(const VideoOptions& options) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +00001226 TRACE_EVENT0("webrtc", "WebRtcVideoChannel2::SetOptions");
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +00001227 LOG(LS_INFO) << "SetOptions: " << options.ToString();
1228 VideoOptions old_options = options_;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001229 options_.SetAll(options);
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +00001230 if (options_ == old_options) {
1231 // No new options to set.
1232 return true;
1233 }
pbos@webrtc.orgd8198032014-11-10 14:41:43 +00001234 rtc::DiffServCodePoint dscp = options_.dscp.GetWithDefaultIfUnset(false)
1235 ? rtc::DSCP_AF41
1236 : rtc::DSCP_DEFAULT;
1237 MediaChannel::SetDscp(dscp);
pbos@webrtc.org575d1262014-10-08 14:48:08 +00001238 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001239 for (std::map<uint32, WebRtcVideoSendStream*>::iterator it =
1240 send_streams_.begin();
1241 it != send_streams_.end();
1242 ++it) {
1243 it->second->SetOptions(options_);
1244 }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001245 return true;
1246}
1247
1248void WebRtcVideoChannel2::SetInterface(NetworkInterface* iface) {
1249 MediaChannel::SetInterface(iface);
1250 // Set the RTP recv/send buffer to a bigger size
1251 MediaChannel::SetOption(NetworkInterface::ST_RTP,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001252 rtc::Socket::OPT_RCVBUF,
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001253 kVideoRtpBufferSize);
1254
buildbot@webrtc.orgae694ef2014-10-28 17:37:17 +00001255 // Speculative change to increase the outbound socket buffer size.
1256 // In b/15152257, we are seeing a significant number of packets discarded
1257 // due to lack of socket buffer space, although it's not yet clear what the
1258 // ideal value should be.
1259 MediaChannel::SetOption(NetworkInterface::ST_RTP,
1260 rtc::Socket::OPT_SNDBUF,
1261 kVideoRtpBufferSize);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001262}
1263
1264void WebRtcVideoChannel2::UpdateAspectRatio(int ratio_w, int ratio_h) {
1265 // TODO(pbos): Implement.
1266}
1267
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001268void WebRtcVideoChannel2::OnMessage(rtc::Message* msg) {
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001269 // Ignored.
1270}
1271
pbos@webrtc.org42684be2014-10-03 11:25:45 +00001272void WebRtcVideoChannel2::OnLoadUpdate(Load load) {
pbos@webrtc.org575d1262014-10-08 14:48:08 +00001273 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.org42684be2014-10-03 11:25:45 +00001274 for (std::map<uint32, WebRtcVideoSendStream*>::iterator it =
1275 send_streams_.begin();
1276 it != send_streams_.end();
1277 ++it) {
1278 it->second->OnCpuResolutionRequest(load == kOveruse
1279 ? CoordinatedVideoAdapter::DOWNGRADE
1280 : CoordinatedVideoAdapter::UPGRADE);
1281 }
1282}
1283
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001284bool WebRtcVideoChannel2::SendRtp(const uint8_t* data, size_t len) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001285 rtc::Buffer packet(data, len, kMaxRtpPacketLen);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001286 return MediaChannel::SendPacket(&packet);
1287}
1288
1289bool WebRtcVideoChannel2::SendRtcp(const uint8_t* data, size_t len) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001290 rtc::Buffer packet(data, len, kMaxRtpPacketLen);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001291 return MediaChannel::SendRtcp(&packet);
1292}
1293
1294void WebRtcVideoChannel2::StartAllSendStreams() {
pbos@webrtc.org575d1262014-10-08 14:48:08 +00001295 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001296 for (std::map<uint32, WebRtcVideoSendStream*>::iterator it =
1297 send_streams_.begin();
1298 it != send_streams_.end();
1299 ++it) {
1300 it->second->Start();
1301 }
1302}
1303
1304void WebRtcVideoChannel2::StopAllSendStreams() {
pbos@webrtc.org575d1262014-10-08 14:48:08 +00001305 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001306 for (std::map<uint32, WebRtcVideoSendStream*>::iterator it =
1307 send_streams_.begin();
1308 it != send_streams_.end();
1309 ++it) {
1310 it->second->Stop();
1311 }
1312}
1313
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +00001314WebRtcVideoChannel2::WebRtcVideoSendStream::VideoSendStreamParameters::
1315 VideoSendStreamParameters(
1316 const webrtc::VideoSendStream::Config& config,
1317 const VideoOptions& options,
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001318 const Settable<VideoCodecSettings>& codec_settings)
1319 : config(config), options(options), codec_settings(codec_settings) {
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +00001320}
1321
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001322WebRtcVideoChannel2::WebRtcVideoSendStream::WebRtcVideoSendStream(
1323 webrtc::Call* call,
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +00001324 WebRtcVideoEncoderFactory* external_encoder_factory,
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +00001325 const VideoOptions& options,
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001326 const Settable<VideoCodecSettings>& codec_settings,
1327 const StreamParams& sp,
1328 const std::vector<webrtc::RtpExtension>& rtp_extensions)
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001329 : call_(call),
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +00001330 external_encoder_factory_(external_encoder_factory),
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001331 stream_(NULL),
pbos@webrtc.orgb648b9d2014-08-26 11:08:06 +00001332 parameters_(webrtc::VideoSendStream::Config(), options, codec_settings),
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +00001333 allocated_encoder_(NULL, webrtc::kVideoCodecUnknown, false),
pbos@webrtc.orgb648b9d2014-08-26 11:08:06 +00001334 capturer_(NULL),
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001335 sending_(false),
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001336 muted_(false) {
1337 parameters_.config.rtp.max_packet_size = kVideoMtu;
1338
1339 sp.GetPrimarySsrcs(&parameters_.config.rtp.ssrcs);
1340 sp.GetFidSsrcs(parameters_.config.rtp.ssrcs,
1341 &parameters_.config.rtp.rtx.ssrcs);
1342 parameters_.config.rtp.c_name = sp.cname;
1343 parameters_.config.rtp.extensions = rtp_extensions;
1344
1345 VideoCodecSettings params;
1346 if (codec_settings.Get(&params)) {
1347 SetCodec(params);
1348 }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001349}
1350
1351WebRtcVideoChannel2::WebRtcVideoSendStream::~WebRtcVideoSendStream() {
1352 DisconnectCapturer();
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001353 if (stream_ != NULL) {
1354 call_->DestroyVideoSendStream(stream_);
1355 }
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +00001356 DestroyVideoEncoder(&allocated_encoder_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001357}
1358
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001359static void CreateBlackFrame(webrtc::I420VideoFrame* video_frame,
1360 int width,
1361 int height) {
pbos@webrtc.orgb4987bf2015-02-18 10:13:09 +00001362 video_frame->CreateEmptyFrame(width, height, width, (width + 1) / 2,
1363 (width + 1) / 2);
1364 memset(video_frame->buffer(webrtc::kYPlane), 16,
1365 video_frame->allocated_size(webrtc::kYPlane));
1366 memset(video_frame->buffer(webrtc::kUPlane), 128,
1367 video_frame->allocated_size(webrtc::kUPlane));
1368 memset(video_frame->buffer(webrtc::kVPlane), 128,
1369 video_frame->allocated_size(webrtc::kVPlane));
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001370}
1371
1372static void ConvertToI420VideoFrame(const VideoFrame& frame,
1373 webrtc::I420VideoFrame* i420_frame) {
1374 i420_frame->CreateFrame(
1375 static_cast<int>(frame.GetYPitch() * frame.GetHeight()),
1376 frame.GetYPlane(),
1377 static_cast<int>(frame.GetUPitch() * ((frame.GetHeight() + 1) / 2)),
1378 frame.GetUPlane(),
1379 static_cast<int>(frame.GetVPitch() * ((frame.GetHeight() + 1) / 2)),
1380 frame.GetVPlane(),
1381 static_cast<int>(frame.GetWidth()),
1382 static_cast<int>(frame.GetHeight()),
1383 static_cast<int>(frame.GetYPitch()),
1384 static_cast<int>(frame.GetUPitch()),
1385 static_cast<int>(frame.GetVPitch()));
1386}
1387
1388void WebRtcVideoChannel2::WebRtcVideoSendStream::InputFrame(
1389 VideoCapturer* capturer,
1390 const VideoFrame* frame) {
pbos@webrtc.org86196c42015-02-16 21:02:00 +00001391 TRACE_EVENT0("webrtc", "WebRtcVideoSendStream::InputFrame");
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001392 LOG(LS_VERBOSE) << "InputFrame: " << frame->GetWidth() << "x"
1393 << frame->GetHeight();
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001394 // Lock before copying, can be called concurrently when swapping input source.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001395 rtc::CritScope frame_cs(&frame_lock_);
pbos@webrtc.orgd60d79a2014-09-24 07:10:57 +00001396 ConvertToI420VideoFrame(*frame, &video_frame_);
1397
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001398 rtc::CritScope cs(&lock_);
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001399 if (stream_ == NULL) {
1400 LOG(LS_WARNING) << "Capturer inputting frames before send codecs are "
1401 "configured, dropping.";
1402 return;
1403 }
pbos@webrtc.org86196c42015-02-16 21:02:00 +00001404
1405 // Not sending, abort early to prevent expensive reconfigurations while
1406 // setting up codecs etc.
1407 if (!sending_)
1408 return;
1409
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001410 if (format_.width == 0) { // Dropping frames.
1411 assert(format_.height == 0);
1412 LOG(LS_VERBOSE) << "VideoFormat 0x0 set, Dropping frame.";
1413 return;
1414 }
pbos@webrtc.orgd60d79a2014-09-24 07:10:57 +00001415 if (muted_) {
1416 // Create a black frame to transmit instead.
1417 CreateBlackFrame(&video_frame_,
1418 static_cast<int>(frame->GetWidth()),
1419 static_cast<int>(frame->GetHeight()));
1420 }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001421 // Reconfigure codec if necessary.
pbos@webrtc.orgc4175b92014-09-03 15:25:49 +00001422 SetDimensions(
1423 video_frame_.width(), video_frame_.height(), capturer->IsScreencast());
1424
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001425 LOG(LS_VERBOSE) << "SwapFrame: " << video_frame_.width() << "x"
1426 << video_frame_.height() << " -> (codec) "
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +00001427 << parameters_.encoder_config.streams.back().width << "x"
1428 << parameters_.encoder_config.streams.back().height;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001429 stream_->Input()->SwapFrame(&video_frame_);
1430}
1431
1432bool WebRtcVideoChannel2::WebRtcVideoSendStream::SetCapturer(
1433 VideoCapturer* capturer) {
pbos@webrtc.orgb4987bf2015-02-18 10:13:09 +00001434 TRACE_EVENT0("webrtc", "WebRtcVideoSendStream::SetCapturer");
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001435 if (!DisconnectCapturer() && capturer == NULL) {
1436 return false;
1437 }
1438
1439 {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001440 rtc::CritScope cs(&lock_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001441
pbos@webrtc.org9359cb32014-07-23 15:44:48 +00001442 if (capturer == NULL) {
1443 if (stream_ != NULL) {
1444 LOG(LS_VERBOSE) << "Disabling capturer, sending black frame.";
1445 webrtc::I420VideoFrame black_frame;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001446
pbos@webrtc.orgb4987bf2015-02-18 10:13:09 +00001447 CreateBlackFrame(&black_frame, last_dimensions_.width,
1448 last_dimensions_.height);
pbos@webrtc.org9359cb32014-07-23 15:44:48 +00001449 stream_->Input()->SwapFrame(&black_frame);
1450 }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001451
1452 capturer_ = NULL;
1453 return true;
1454 }
1455
1456 capturer_ = capturer;
1457 }
1458 // Lock cannot be held while connecting the capturer to prevent lock-order
1459 // violations.
1460 capturer->SignalVideoFrame.connect(this, &WebRtcVideoSendStream::InputFrame);
1461 return true;
1462}
1463
1464bool WebRtcVideoChannel2::WebRtcVideoSendStream::SetVideoFormat(
1465 const VideoFormat& format) {
1466 if ((format.width == 0 || format.height == 0) &&
1467 format.width != format.height) {
1468 LOG(LS_ERROR) << "Can't set VideoFormat, width or height is zero (but not "
1469 "both, 0x0 drops frames).";
1470 return false;
1471 }
1472
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001473 rtc::CritScope cs(&lock_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001474 if (format.width == 0 && format.height == 0) {
1475 LOG(LS_INFO)
1476 << "0x0 resolution selected. Captured frames will be dropped for ssrc: "
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +00001477 << parameters_.config.rtp.ssrcs[0] << ".";
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001478 } else {
1479 // TODO(pbos): Fix me, this only affects the last stream!
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +00001480 parameters_.encoder_config.streams.back().max_framerate =
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001481 VideoFormat::IntervalToFps(format.interval);
pbos@webrtc.orgc4175b92014-09-03 15:25:49 +00001482 SetDimensions(format.width, format.height, false);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001483 }
1484
1485 format_ = format;
1486 return true;
1487}
1488
pbos@webrtc.orgef8bb8d2014-08-13 21:36:18 +00001489void WebRtcVideoChannel2::WebRtcVideoSendStream::MuteStream(bool mute) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001490 rtc::CritScope cs(&lock_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001491 muted_ = mute;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001492}
1493
1494bool WebRtcVideoChannel2::WebRtcVideoSendStream::DisconnectCapturer() {
pbos@webrtc.org963b9792014-10-07 14:27:27 +00001495 cricket::VideoCapturer* capturer;
1496 {
1497 rtc::CritScope cs(&lock_);
1498 if (capturer_ == NULL) {
1499 return false;
1500 }
1501 capturer = capturer_;
1502 capturer_ = NULL;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001503 }
pbos@webrtc.org963b9792014-10-07 14:27:27 +00001504 capturer->SignalVideoFrame.disconnect(this);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001505 return true;
1506}
1507
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001508void WebRtcVideoChannel2::WebRtcVideoSendStream::SetOptions(
1509 const VideoOptions& options) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001510 rtc::CritScope cs(&lock_);
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001511 VideoCodecSettings codec_settings;
1512 if (parameters_.codec_settings.Get(&codec_settings)) {
1513 SetCodecAndOptions(codec_settings, options);
1514 } else {
1515 parameters_.options = options;
1516 }
1517}
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +00001518
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001519void WebRtcVideoChannel2::WebRtcVideoSendStream::SetCodec(
1520 const VideoCodecSettings& codec_settings) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001521 rtc::CritScope cs(&lock_);
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001522 SetCodecAndOptions(codec_settings, parameters_.options);
1523}
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +00001524
1525webrtc::VideoCodecType CodecTypeFromName(const std::string& name) {
1526 if (CodecNameMatches(name, kVp8CodecName)) {
1527 return webrtc::kVideoCodecVP8;
andresp@webrtc.org188d3b22014-11-07 13:21:04 +00001528 } else if (CodecNameMatches(name, kVp9CodecName)) {
1529 return webrtc::kVideoCodecVP9;
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +00001530 } else if (CodecNameMatches(name, kH264CodecName)) {
1531 return webrtc::kVideoCodecH264;
1532 }
1533 return webrtc::kVideoCodecUnknown;
1534}
1535
1536WebRtcVideoChannel2::WebRtcVideoSendStream::AllocatedEncoder
1537WebRtcVideoChannel2::WebRtcVideoSendStream::CreateVideoEncoder(
1538 const VideoCodec& codec) {
1539 webrtc::VideoCodecType type = CodecTypeFromName(codec.name);
1540
1541 // Do not re-create encoders of the same type.
1542 if (type == allocated_encoder_.type && allocated_encoder_.encoder != NULL) {
1543 return allocated_encoder_;
1544 }
1545
1546 if (external_encoder_factory_ != NULL) {
1547 webrtc::VideoEncoder* encoder =
1548 external_encoder_factory_->CreateVideoEncoder(type);
1549 if (encoder != NULL) {
1550 return AllocatedEncoder(encoder, type, true);
1551 }
1552 }
1553
1554 if (type == webrtc::kVideoCodecVP8) {
1555 return AllocatedEncoder(
1556 webrtc::VideoEncoder::Create(webrtc::VideoEncoder::kVp8), type, false);
andresp@webrtc.org188d3b22014-11-07 13:21:04 +00001557 } else if (type == webrtc::kVideoCodecVP9) {
1558 return AllocatedEncoder(
1559 webrtc::VideoEncoder::Create(webrtc::VideoEncoder::kVp9), type, false);
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +00001560 }
1561
1562 // This shouldn't happen, we should not be trying to create something we don't
1563 // support.
1564 assert(false);
1565 return AllocatedEncoder(NULL, webrtc::kVideoCodecUnknown, false);
1566}
1567
1568void WebRtcVideoChannel2::WebRtcVideoSendStream::DestroyVideoEncoder(
1569 AllocatedEncoder* encoder) {
1570 if (encoder->external) {
1571 external_encoder_factory_->DestroyVideoEncoder(encoder->encoder);
1572 } else {
1573 delete encoder->encoder;
1574 }
1575}
1576
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001577void WebRtcVideoChannel2::WebRtcVideoSendStream::SetCodecAndOptions(
1578 const VideoCodecSettings& codec_settings,
1579 const VideoOptions& options) {
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +00001580 parameters_.encoder_config =
1581 CreateVideoEncoderConfig(last_dimensions_, codec_settings.codec);
pbos@webrtc.org86196c42015-02-16 21:02:00 +00001582 if (parameters_.encoder_config.streams.empty())
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001583 return;
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +00001584
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001585 format_ = VideoFormat(codec_settings.codec.width,
1586 codec_settings.codec.height,
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001587 VideoFormat::FpsToInterval(30),
1588 FOURCC_I420);
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +00001589
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +00001590 AllocatedEncoder new_encoder = CreateVideoEncoder(codec_settings.codec);
1591 parameters_.config.encoder_settings.encoder = new_encoder.encoder;
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001592 parameters_.config.encoder_settings.payload_name = codec_settings.codec.name;
1593 parameters_.config.encoder_settings.payload_type = codec_settings.codec.id;
1594 parameters_.config.rtp.fec = codec_settings.fec;
1595
1596 // Set RTX payload type if RTX is enabled.
1597 if (!parameters_.config.rtp.rtx.ssrcs.empty()) {
1598 parameters_.config.rtp.rtx.payload_type = codec_settings.rtx_payload_type;
1599 }
1600
1601 if (IsNackEnabled(codec_settings.codec)) {
1602 parameters_.config.rtp.nack.rtp_history_ms = kNackHistoryMs;
1603 }
1604
pbos@webrtc.org5ff71ab2014-07-23 07:28:56 +00001605 options.suspend_below_min_bitrate.Get(
1606 &parameters_.config.suspend_below_min_bitrate);
1607
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001608 parameters_.codec_settings.Set(codec_settings);
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +00001609 parameters_.options = options;
pbos@webrtc.org543e5892014-07-23 07:01:31 +00001610
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001611 RecreateWebRtcStream();
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +00001612 if (allocated_encoder_.encoder != new_encoder.encoder) {
1613 DestroyVideoEncoder(&allocated_encoder_);
1614 allocated_encoder_ = new_encoder;
1615 }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001616}
1617
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001618void WebRtcVideoChannel2::WebRtcVideoSendStream::SetRtpExtensions(
1619 const std::vector<webrtc::RtpExtension>& rtp_extensions) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001620 rtc::CritScope cs(&lock_);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001621 parameters_.config.rtp.extensions = rtp_extensions;
1622 RecreateWebRtcStream();
1623}
1624
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +00001625webrtc::VideoEncoderConfig
1626WebRtcVideoChannel2::WebRtcVideoSendStream::CreateVideoEncoderConfig(
1627 const Dimensions& dimensions,
1628 const VideoCodec& codec) const {
1629 webrtc::VideoEncoderConfig encoder_config;
1630 if (dimensions.is_screencast) {
pbos@webrtc.orgefc82c22014-10-27 13:58:00 +00001631 int screencast_min_bitrate_kbps;
1632 parameters_.options.screencast_min_bitrate.Get(
1633 &screencast_min_bitrate_kbps);
1634 encoder_config.min_transmit_bitrate_bps =
1635 screencast_min_bitrate_kbps * 1000;
1636 encoder_config.content_type = webrtc::VideoEncoderConfig::kScreenshare;
1637 } else {
1638 encoder_config.min_transmit_bitrate_bps = 0;
1639 encoder_config.content_type = webrtc::VideoEncoderConfig::kRealtimeVideo;
1640 }
1641
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +00001642 // Restrict dimensions according to codec max.
1643 int width = dimensions.width;
1644 int height = dimensions.height;
1645 if (!dimensions.is_screencast) {
1646 if (codec.width < width)
1647 width = codec.width;
1648 if (codec.height < height)
1649 height = codec.height;
1650 }
1651
1652 VideoCodec clamped_codec = codec;
1653 clamped_codec.width = width;
1654 clamped_codec.height = height;
pbos@webrtc.orgcddd17c2014-09-16 16:33:13 +00001655
pbos@webrtc.orgf1c8b902015-01-14 17:29:27 +00001656 encoder_config.streams = CreateVideoStreams(
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +00001657 clamped_codec, parameters_.options, parameters_.config.rtp.ssrcs.size());
pbos@webrtc.org6f48f1b2014-07-22 16:29:54 +00001658
pbos@webrtc.orgb7ed7792014-10-31 13:08:10 +00001659 // Conference mode screencast uses 2 temporal layers split at 100kbit.
1660 if (parameters_.options.conference_mode.GetWithDefaultIfUnset(false) &&
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +00001661 dimensions.is_screencast && encoder_config.streams.size() == 1) {
sprang@webrtc.org46d4d292014-12-23 15:19:35 +00001662 ScreenshareLayerConfig config = ScreenshareLayerConfig::GetDefault();
1663
1664 // For screenshare in conference mode, tl0 and tl1 bitrates are piggybacked
1665 // on the VideoCodec struct as target and max bitrates, respectively.
1666 // See eg. webrtc::VP8EncoderImpl::SetRates().
1667 encoder_config.streams[0].target_bitrate_bps =
1668 config.tl0_bitrate_kbps * 1000;
1669 encoder_config.streams[0].max_bitrate_bps = config.tl1_bitrate_kbps * 1000;
pbos@webrtc.orgb7ed7792014-10-31 13:08:10 +00001670 encoder_config.streams[0].temporal_layer_thresholds_bps.clear();
1671 encoder_config.streams[0].temporal_layer_thresholds_bps.push_back(
sprang@webrtc.org46d4d292014-12-23 15:19:35 +00001672 config.tl0_bitrate_kbps * 1000);
pbos@webrtc.orgb7ed7792014-10-31 13:08:10 +00001673 }
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +00001674 return encoder_config;
1675}
1676
1677void WebRtcVideoChannel2::WebRtcVideoSendStream::SetDimensions(
1678 int width,
1679 int height,
1680 bool is_screencast) {
1681 if (last_dimensions_.width == width && last_dimensions_.height == height &&
1682 last_dimensions_.is_screencast == is_screencast) {
1683 // Configured using the same parameters, do not reconfigure.
1684 return;
1685 }
1686 LOG(LS_INFO) << "SetDimensions: " << width << "x" << height
1687 << (is_screencast ? " (screencast)" : " (not screencast)");
1688
1689 last_dimensions_.width = width;
1690 last_dimensions_.height = height;
1691 last_dimensions_.is_screencast = is_screencast;
1692
1693 assert(!parameters_.encoder_config.streams.empty());
1694
1695 VideoCodecSettings codec_settings;
1696 parameters_.codec_settings.Get(&codec_settings);
1697
1698 webrtc::VideoEncoderConfig encoder_config =
1699 CreateVideoEncoderConfig(last_dimensions_, codec_settings.codec);
1700
1701 encoder_config.encoder_specific_settings =
pbos@webrtc.orgf1c8b902015-01-14 17:29:27 +00001702 ConfigureVideoEncoderSettings(codec_settings.codec, parameters_.options);
pbos@webrtc.orgb7ed7792014-10-31 13:08:10 +00001703
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +00001704 bool stream_reconfigured = stream_->ReconfigureVideoEncoder(encoder_config);
1705
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +00001706 encoder_config.encoder_specific_settings = NULL;
pbos@webrtc.org6f48f1b2014-07-22 16:29:54 +00001707
1708 if (!stream_reconfigured) {
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001709 LOG(LS_WARNING) << "Failed to reconfigure video encoder for dimensions: "
1710 << width << "x" << height;
1711 return;
1712 }
pbos@webrtc.orgcddd17c2014-09-16 16:33:13 +00001713
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +00001714 parameters_.encoder_config = encoder_config;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001715}
1716
1717void WebRtcVideoChannel2::WebRtcVideoSendStream::Start() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001718 rtc::CritScope cs(&lock_);
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001719 assert(stream_ != NULL);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001720 stream_->Start();
1721 sending_ = true;
1722}
1723
1724void WebRtcVideoChannel2::WebRtcVideoSendStream::Stop() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001725 rtc::CritScope cs(&lock_);
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001726 if (stream_ != NULL) {
1727 stream_->Stop();
1728 }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001729 sending_ = false;
1730}
1731
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00001732VideoSenderInfo
1733WebRtcVideoChannel2::WebRtcVideoSendStream::GetVideoSenderInfo() {
1734 VideoSenderInfo info;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001735 rtc::CritScope cs(&lock_);
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00001736 for (size_t i = 0; i < parameters_.config.rtp.ssrcs.size(); ++i) {
1737 info.add_ssrc(parameters_.config.rtp.ssrcs[i]);
1738 }
1739
pbos@webrtc.orgc3d2bd22014-08-12 20:55:10 +00001740 if (stream_ == NULL) {
1741 return info;
1742 }
1743
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00001744 webrtc::VideoSendStream::Stats stats = stream_->GetStats();
1745 info.framerate_input = stats.input_frame_rate;
1746 info.framerate_sent = stats.encode_frame_rate;
1747
pbos@webrtc.org273a4142014-12-01 15:23:21 +00001748 info.send_frame_width = 0;
1749 info.send_frame_height = 0;
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001750 for (std::map<uint32_t, webrtc::SsrcStats>::iterator it =
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00001751 stats.substreams.begin();
1752 it != stats.substreams.end();
1753 ++it) {
1754 // TODO(pbos): Wire up additional stats, such as padding bytes.
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001755 webrtc::SsrcStats stream_stats = it->second;
asapersson@webrtc.orgcfd82df2015-01-22 09:39:59 +00001756 info.bytes_sent += stream_stats.rtp_stats.transmitted.payload_bytes +
1757 stream_stats.rtp_stats.transmitted.header_bytes +
1758 stream_stats.rtp_stats.transmitted.padding_bytes;
1759 info.packets_sent += stream_stats.rtp_stats.transmitted.packets;
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00001760 info.packets_lost += stream_stats.rtcp_stats.cumulative_lost;
pbos@webrtc.org273a4142014-12-01 15:23:21 +00001761 if (stream_stats.sent_width > info.send_frame_width)
1762 info.send_frame_width = stream_stats.sent_width;
1763 if (stream_stats.sent_height > info.send_frame_height)
1764 info.send_frame_height = stream_stats.sent_height;
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +00001765 info.firs_rcvd += stream_stats.rtcp_packet_type_counts.fir_packets;
1766 info.nacks_rcvd += stream_stats.rtcp_packet_type_counts.nack_packets;
1767 info.plis_rcvd += stream_stats.rtcp_packet_type_counts.pli_packets;
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00001768 }
1769
1770 if (!stats.substreams.empty()) {
1771 // TODO(pbos): Report fraction lost per SSRC.
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001772 webrtc::SsrcStats first_stream_stats = stats.substreams.begin()->second;
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00001773 info.fraction_lost =
1774 static_cast<float>(first_stream_stats.rtcp_stats.fraction_lost) /
1775 (1 << 8);
1776 }
1777
1778 if (capturer_ != NULL && !capturer_->IsMuted()) {
1779 VideoFormat last_captured_frame_format;
1780 capturer_->GetStats(&info.adapt_frame_drops,
1781 &info.effects_frame_drops,
1782 &info.capturer_frame_time,
1783 &last_captured_frame_format);
1784 info.input_frame_width = last_captured_frame_format.width;
1785 info.input_frame_height = last_captured_frame_format.height;
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00001786 }
1787
pbos@webrtc.org1ed62242015-02-19 13:57:03 +00001788 // TODO(pbos): Support or remove the following stats.
1789 info.packets_cached = -1;
1790
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00001791 return info;
1792}
1793
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001794void WebRtcVideoChannel2::WebRtcVideoSendStream::FillBandwidthEstimationInfo(
1795 BandwidthEstimationInfo* bwe_info) {
1796 rtc::CritScope cs(&lock_);
1797 if (stream_ == NULL) {
1798 return;
1799 }
1800 webrtc::VideoSendStream::Stats stats = stream_->GetStats();
1801 for (std::map<uint32_t, webrtc::SsrcStats>::iterator it =
1802 stats.substreams.begin();
1803 it != stats.substreams.end();
1804 ++it) {
1805 bwe_info->transmit_bitrate += it->second.total_bitrate_bps;
1806 bwe_info->retransmit_bitrate += it->second.retransmit_bitrate_bps;
1807 }
1808 bwe_info->actual_enc_bitrate = stats.media_bitrate_bps;
1809}
1810
pbos@webrtc.org42684be2014-10-03 11:25:45 +00001811void WebRtcVideoChannel2::WebRtcVideoSendStream::OnCpuResolutionRequest(
1812 CoordinatedVideoAdapter::AdaptRequest adapt_request) {
1813 rtc::CritScope cs(&lock_);
1814 bool adapt_cpu;
1815 parameters_.options.cpu_overuse_detection.Get(&adapt_cpu);
1816 if (!adapt_cpu) {
1817 return;
1818 }
1819 if (capturer_ == NULL || capturer_->video_adapter() == NULL) {
1820 return;
1821 }
1822
1823 capturer_->video_adapter()->OnCpuResolutionRequest(adapt_request);
1824}
1825
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001826void WebRtcVideoChannel2::WebRtcVideoSendStream::RecreateWebRtcStream() {
1827 if (stream_ != NULL) {
1828 call_->DestroyVideoSendStream(stream_);
1829 }
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +00001830
pbos@webrtc.org6f48f1b2014-07-22 16:29:54 +00001831 VideoCodecSettings codec_settings;
1832 parameters_.codec_settings.Get(&codec_settings);
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +00001833 parameters_.encoder_config.encoder_specific_settings =
pbos@webrtc.orgf1c8b902015-01-14 17:29:27 +00001834 ConfigureVideoEncoderSettings(codec_settings.codec, parameters_.options);
pbos@webrtc.org6f48f1b2014-07-22 16:29:54 +00001835
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +00001836 stream_ = call_->CreateVideoSendStream(parameters_.config,
1837 parameters_.encoder_config);
pbos@webrtc.org6f48f1b2014-07-22 16:29:54 +00001838
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +00001839 parameters_.encoder_config.encoder_specific_settings = NULL;
pbos@webrtc.org6f48f1b2014-07-22 16:29:54 +00001840
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001841 if (sending_) {
1842 stream_->Start();
1843 }
1844}
1845
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001846WebRtcVideoChannel2::WebRtcVideoReceiveStream::WebRtcVideoReceiveStream(
1847 webrtc::Call* call,
pbos@webrtc.org776e6f22014-10-29 15:28:39 +00001848 WebRtcVideoDecoderFactory* external_decoder_factory,
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001849 const webrtc::VideoReceiveStream::Config& config,
1850 const std::vector<VideoCodecSettings>& recv_codecs)
1851 : call_(call),
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001852 stream_(NULL),
pbos@webrtc.orgb648b9d2014-08-26 11:08:06 +00001853 config_(config),
pbos@webrtc.org776e6f22014-10-29 15:28:39 +00001854 external_decoder_factory_(external_decoder_factory),
pbos@webrtc.orgb648b9d2014-08-26 11:08:06 +00001855 renderer_(NULL),
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001856 last_width_(-1),
magjed@webrtc.orgfc5ad952015-01-27 09:57:01 +00001857 last_height_(-1),
1858 first_frame_timestamp_(-1),
1859 estimated_remote_start_ntp_time_ms_(0) {
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001860 config_.renderer = this;
1861 // SetRecvCodecs will also reset (start) the VideoReceiveStream.
1862 SetRecvCodecs(recv_codecs);
1863}
1864
1865WebRtcVideoChannel2::WebRtcVideoReceiveStream::~WebRtcVideoReceiveStream() {
1866 call_->DestroyVideoReceiveStream(stream_);
pbos@webrtc.org96a93252014-11-03 14:46:44 +00001867 ClearDecoders(&allocated_decoders_);
1868}
1869
1870WebRtcVideoChannel2::WebRtcVideoReceiveStream::AllocatedDecoder
1871WebRtcVideoChannel2::WebRtcVideoReceiveStream::CreateOrReuseVideoDecoder(
1872 std::vector<AllocatedDecoder>* old_decoders,
1873 const VideoCodec& codec) {
1874 webrtc::VideoCodecType type = CodecTypeFromName(codec.name);
1875
1876 for (size_t i = 0; i < old_decoders->size(); ++i) {
1877 if ((*old_decoders)[i].type == type) {
1878 AllocatedDecoder decoder = (*old_decoders)[i];
1879 (*old_decoders)[i] = old_decoders->back();
1880 old_decoders->pop_back();
1881 return decoder;
1882 }
1883 }
1884
1885 if (external_decoder_factory_ != NULL) {
1886 webrtc::VideoDecoder* decoder =
1887 external_decoder_factory_->CreateVideoDecoder(type);
1888 if (decoder != NULL) {
1889 return AllocatedDecoder(decoder, type, true);
1890 }
1891 }
1892
1893 if (type == webrtc::kVideoCodecVP8) {
1894 return AllocatedDecoder(
1895 webrtc::VideoDecoder::Create(webrtc::VideoDecoder::kVp8), type, false);
1896 }
1897
1898 // This shouldn't happen, we should not be trying to create something we don't
1899 // support.
1900 assert(false);
1901 return AllocatedDecoder(NULL, webrtc::kVideoCodecUnknown, false);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001902}
1903
1904void WebRtcVideoChannel2::WebRtcVideoReceiveStream::SetRecvCodecs(
1905 const std::vector<VideoCodecSettings>& recv_codecs) {
pbos@webrtc.org96a93252014-11-03 14:46:44 +00001906 std::vector<AllocatedDecoder> old_decoders = allocated_decoders_;
1907 allocated_decoders_.clear();
1908 config_.decoders.clear();
1909 for (size_t i = 0; i < recv_codecs.size(); ++i) {
1910 AllocatedDecoder allocated_decoder =
1911 CreateOrReuseVideoDecoder(&old_decoders, recv_codecs[i].codec);
1912 allocated_decoders_.push_back(allocated_decoder);
1913
1914 webrtc::VideoReceiveStream::Decoder decoder;
1915 decoder.decoder = allocated_decoder.decoder;
1916 decoder.payload_type = recv_codecs[i].codec.id;
1917 decoder.payload_name = recv_codecs[i].codec.name;
1918 config_.decoders.push_back(decoder);
1919 }
1920
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001921 // TODO(pbos): Reconfigure RTX based on incoming recv_codecs.
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001922 config_.rtp.fec = recv_codecs.front().fec;
pbos@webrtc.org257e1302014-07-25 19:01:32 +00001923 config_.rtp.nack.rtp_history_ms =
1924 IsNackEnabled(recv_codecs.begin()->codec) ? kNackHistoryMs : 0;
1925 config_.rtp.remb = IsRembEnabled(recv_codecs.begin()->codec);
1926
pbos@webrtc.org96a93252014-11-03 14:46:44 +00001927 ClearDecoders(&old_decoders);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001928 RecreateWebRtcStream();
1929}
1930
1931void WebRtcVideoChannel2::WebRtcVideoReceiveStream::SetRtpExtensions(
1932 const std::vector<webrtc::RtpExtension>& extensions) {
1933 config_.rtp.extensions = extensions;
1934 RecreateWebRtcStream();
1935}
1936
1937void WebRtcVideoChannel2::WebRtcVideoReceiveStream::RecreateWebRtcStream() {
1938 if (stream_ != NULL) {
1939 call_->DestroyVideoReceiveStream(stream_);
1940 }
1941 stream_ = call_->CreateVideoReceiveStream(config_);
1942 stream_->Start();
1943}
1944
pbos@webrtc.org96a93252014-11-03 14:46:44 +00001945void WebRtcVideoChannel2::WebRtcVideoReceiveStream::ClearDecoders(
1946 std::vector<AllocatedDecoder>* allocated_decoders) {
1947 for (size_t i = 0; i < allocated_decoders->size(); ++i) {
1948 if ((*allocated_decoders)[i].external) {
pbos@webrtc.org776e6f22014-10-29 15:28:39 +00001949 external_decoder_factory_->DestroyVideoDecoder(
pbos@webrtc.org96a93252014-11-03 14:46:44 +00001950 (*allocated_decoders)[i].decoder);
pbos@webrtc.org776e6f22014-10-29 15:28:39 +00001951 } else {
pbos@webrtc.org96a93252014-11-03 14:46:44 +00001952 delete (*allocated_decoders)[i].decoder;
pbos@webrtc.org776e6f22014-10-29 15:28:39 +00001953 }
1954 }
pbos@webrtc.org96a93252014-11-03 14:46:44 +00001955 allocated_decoders->clear();
pbos@webrtc.org776e6f22014-10-29 15:28:39 +00001956}
1957
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001958void WebRtcVideoChannel2::WebRtcVideoReceiveStream::RenderFrame(
1959 const webrtc::I420VideoFrame& frame,
1960 int time_to_render_ms) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001961 rtc::CritScope crit(&renderer_lock_);
magjed@webrtc.orgfc5ad952015-01-27 09:57:01 +00001962
1963 if (first_frame_timestamp_ < 0)
1964 first_frame_timestamp_ = frame.timestamp();
1965 int64_t rtp_time_elapsed_since_first_frame =
1966 (timestamp_wraparound_handler_.Unwrap(frame.timestamp()) -
1967 first_frame_timestamp_);
1968 int64_t elapsed_time_ms = rtp_time_elapsed_since_first_frame /
1969 (cricket::kVideoCodecClockrate / 1000);
1970 if (frame.ntp_time_ms() > 0)
1971 estimated_remote_start_ntp_time_ms_ = frame.ntp_time_ms() - elapsed_time_ms;
1972
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001973 if (renderer_ == NULL) {
1974 LOG(LS_WARNING) << "VideoReceiveStream not connected to a VideoRenderer.";
1975 return;
1976 }
1977
1978 if (frame.width() != last_width_ || frame.height() != last_height_) {
1979 SetSize(frame.width(), frame.height());
1980 }
1981
1982 LOG(LS_VERBOSE) << "RenderFrame: (" << frame.width() << "x" << frame.height()
1983 << ")";
1984
magjed@webrtc.orgfc5ad952015-01-27 09:57:01 +00001985 const WebRtcVideoRenderFrame render_frame(&frame, elapsed_time_ms);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001986 renderer_->RenderFrame(&render_frame);
1987}
1988
pbos@webrtc.org0d852d52015-02-09 15:14:36 +00001989bool WebRtcVideoChannel2::WebRtcVideoReceiveStream::IsTextureSupported() const {
1990 return true;
1991}
1992
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001993void WebRtcVideoChannel2::WebRtcVideoReceiveStream::SetRenderer(
1994 cricket::VideoRenderer* renderer) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001995 rtc::CritScope crit(&renderer_lock_);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001996 renderer_ = renderer;
1997 if (renderer_ != NULL && last_width_ != -1) {
1998 SetSize(last_width_, last_height_);
1999 }
2000}
2001
2002VideoRenderer* WebRtcVideoChannel2::WebRtcVideoReceiveStream::GetRenderer() {
2003 // TODO(pbos): Remove GetRenderer and all uses of it, it's thread-unsafe by
2004 // design.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002005 rtc::CritScope crit(&renderer_lock_);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00002006 return renderer_;
2007}
2008
2009void WebRtcVideoChannel2::WebRtcVideoReceiveStream::SetSize(int width,
2010 int height) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002011 rtc::CritScope crit(&renderer_lock_);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00002012 if (!renderer_->SetSize(width, height, 0)) {
2013 LOG(LS_ERROR) << "Could not set renderer size.";
2014 }
2015 last_width_ = width;
2016 last_height_ = height;
2017}
2018
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00002019VideoReceiverInfo
2020WebRtcVideoChannel2::WebRtcVideoReceiveStream::GetVideoReceiverInfo() {
2021 VideoReceiverInfo info;
2022 info.add_ssrc(config_.rtp.remote_ssrc);
2023 webrtc::VideoReceiveStream::Stats stats = stream_->GetStats();
asapersson@webrtc.orgcfd82df2015-01-22 09:39:59 +00002024 info.bytes_rcvd = stats.rtp_stats.transmitted.payload_bytes +
2025 stats.rtp_stats.transmitted.header_bytes +
2026 stats.rtp_stats.transmitted.padding_bytes;
2027 info.packets_rcvd = stats.rtp_stats.transmitted.packets;
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00002028
2029 info.framerate_rcvd = stats.network_frame_rate;
2030 info.framerate_decoded = stats.decode_frame_rate;
2031 info.framerate_output = stats.render_frame_rate;
2032
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +00002033 {
2034 rtc::CritScope frame_cs(&renderer_lock_);
2035 info.frame_width = last_width_;
2036 info.frame_height = last_height_;
2037 info.capture_start_ntp_time_ms = estimated_remote_start_ntp_time_ms_;
2038 }
2039
2040 info.firs_sent = stats.rtcp_packet_type_counts.fir_packets;
2041 info.plis_sent = stats.rtcp_packet_type_counts.pli_packets;
2042 info.nacks_sent = stats.rtcp_packet_type_counts.nack_packets;
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00002043
pbos@webrtc.org1ed62242015-02-19 13:57:03 +00002044 // TODO(pbos): Support or remove the following stats.
2045 info.packets_concealed = -1;
2046
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00002047 return info;
2048}
2049
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00002050WebRtcVideoChannel2::VideoCodecSettings::VideoCodecSettings()
2051 : rtx_payload_type(-1) {}
2052
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +00002053bool WebRtcVideoChannel2::VideoCodecSettings::operator==(
2054 const WebRtcVideoChannel2::VideoCodecSettings& other) const {
2055 return codec == other.codec &&
2056 fec.ulpfec_payload_type == other.fec.ulpfec_payload_type &&
2057 fec.red_payload_type == other.fec.red_payload_type &&
2058 rtx_payload_type == other.rtx_payload_type;
2059}
2060
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00002061std::vector<WebRtcVideoChannel2::VideoCodecSettings>
2062WebRtcVideoChannel2::MapCodecs(const std::vector<VideoCodec>& codecs) {
2063 assert(!codecs.empty());
2064
2065 std::vector<VideoCodecSettings> video_codecs;
2066 std::map<int, bool> payload_used;
pbos@webrtc.orge322a172014-06-13 11:47:28 +00002067 std::map<int, VideoCodec::CodecType> payload_codec_type;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00002068 std::map<int, int> rtx_mapping; // video payload type -> rtx payload type.
2069
2070 webrtc::FecConfig fec_settings;
2071
2072 for (size_t i = 0; i < codecs.size(); ++i) {
2073 const VideoCodec& in_codec = codecs[i];
2074 int payload_type = in_codec.id;
2075
2076 if (payload_used[payload_type]) {
2077 LOG(LS_ERROR) << "Payload type already registered: "
2078 << in_codec.ToString();
2079 return std::vector<VideoCodecSettings>();
2080 }
2081 payload_used[payload_type] = true;
pbos@webrtc.orge322a172014-06-13 11:47:28 +00002082 payload_codec_type[payload_type] = in_codec.GetCodecType();
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00002083
2084 switch (in_codec.GetCodecType()) {
2085 case VideoCodec::CODEC_RED: {
2086 // RED payload type, should not have duplicates.
2087 assert(fec_settings.red_payload_type == -1);
2088 fec_settings.red_payload_type = in_codec.id;
2089 continue;
2090 }
2091
2092 case VideoCodec::CODEC_ULPFEC: {
2093 // ULPFEC payload type, should not have duplicates.
2094 assert(fec_settings.ulpfec_payload_type == -1);
2095 fec_settings.ulpfec_payload_type = in_codec.id;
2096 continue;
2097 }
2098
2099 case VideoCodec::CODEC_RTX: {
2100 int associated_payload_type;
2101 if (!in_codec.GetParam(kCodecParamAssociatedPayloadType,
pkasting@chromium.orge9facf82015-02-17 20:36:28 +00002102 &associated_payload_type) ||
2103 !IsValidRtpPayloadType(associated_payload_type)) {
2104 LOG(LS_ERROR)
2105 << "RTX codec with invalid or no associated payload type: "
2106 << in_codec.ToString();
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00002107 return std::vector<VideoCodecSettings>();
2108 }
2109 rtx_mapping[associated_payload_type] = in_codec.id;
2110 continue;
2111 }
2112
2113 case VideoCodec::CODEC_VIDEO:
2114 break;
2115 }
2116
2117 video_codecs.push_back(VideoCodecSettings());
2118 video_codecs.back().codec = in_codec;
2119 }
2120
2121 // One of these codecs should have been a video codec. Only having FEC
2122 // parameters into this code is a logic error.
2123 assert(!video_codecs.empty());
2124
pbos@webrtc.orge322a172014-06-13 11:47:28 +00002125 for (std::map<int, int>::const_iterator it = rtx_mapping.begin();
2126 it != rtx_mapping.end();
2127 ++it) {
2128 if (!payload_used[it->first]) {
2129 LOG(LS_ERROR) << "RTX mapped to payload not in codec list.";
2130 return std::vector<VideoCodecSettings>();
2131 }
andrew@webrtc.org8f27fcc2015-01-09 20:22:46 +00002132 if (payload_codec_type[it->first] != VideoCodec::CODEC_VIDEO) {
2133 LOG(LS_ERROR) << "RTX not mapped to regular video codec.";
pbos@webrtc.orge322a172014-06-13 11:47:28 +00002134 return std::vector<VideoCodecSettings>();
2135 }
2136 }
2137
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00002138 // TODO(pbos): Write tests that figure out that I have not verified that RTX
2139 // codecs aren't mapped to bogus payloads.
2140 for (size_t i = 0; i < video_codecs.size(); ++i) {
2141 video_codecs[i].fec = fec_settings;
andrew@webrtc.org8f27fcc2015-01-09 20:22:46 +00002142 if (rtx_mapping[video_codecs[i].codec.id] != 0) {
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00002143 video_codecs[i].rtx_payload_type = rtx_mapping[video_codecs[i].codec.id];
2144 }
2145 }
2146
2147 return video_codecs;
2148}
2149
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00002150} // namespace cricket
2151
2152#endif // HAVE_WEBRTC_VIDEO