blob: 22285d72622050144ec1088bcd3a941abc7a38cb [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;
515 out->framerate =
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000516 rtc::_min(requested.framerate, matching_codec.framerate);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000517 out->params = requested.params;
518 out->feedback_params = requested.feedback_params;
pbos@webrtc.org8fdeee62014-07-20 14:40:23 +0000519 out->width = requested.width;
520 out->height = requested.height;
521 if (requested.width == 0 && requested.height == 0) {
522 return true;
523 }
524
525 while (out->width > matching_codec.width) {
526 out->width /= 2;
527 out->height /= 2;
528 }
529
530 return out->width > 0 && out->height > 0;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000531}
532
533bool WebRtcVideoEngine2::SetVoiceEngine(WebRtcVoiceEngine* voice_engine) {
534 if (initialized_) {
535 LOG(LS_WARNING) << "SetVoiceEngine can not be called after Init";
536 return false;
537 }
538 voice_engine_ = voice_engine;
539 return true;
540}
541
542// Ignore spammy trace messages, mostly from the stats API when we haven't
543// gotten RTCP info yet from the remote side.
544bool WebRtcVideoEngine2::ShouldIgnoreTrace(const std::string& trace) {
545 static const char* const kTracesToIgnore[] = {NULL};
546 for (const char* const* p = kTracesToIgnore; *p; ++p) {
547 if (trace.find(*p) == 0) {
548 return true;
549 }
550 }
551 return false;
552}
553
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000554std::vector<VideoCodec> WebRtcVideoEngine2::GetSupportedCodecs() const {
andresp@webrtc.org82775b12014-11-07 09:37:54 +0000555 std::vector<VideoCodec> supported_codecs = DefaultVideoCodecList();
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000556
557 if (external_encoder_factory_ == NULL) {
558 return supported_codecs;
559 }
560
561 assert(external_encoder_factory_->codecs().size() <= kMaxExternalVideoCodecs);
562 const std::vector<WebRtcVideoEncoderFactory::VideoCodec>& codecs =
563 external_encoder_factory_->codecs();
564 for (size_t i = 0; i < codecs.size(); ++i) {
565 // Don't add internally-supported codecs twice.
566 if (CodecIsInternallySupported(codecs[i].name)) {
567 continue;
568 }
569
570 VideoCodec codec(kExternalVideoPayloadTypeBase + static_cast<int>(i),
571 codecs[i].name,
572 codecs[i].max_width,
573 codecs[i].max_height,
574 codecs[i].max_fps,
575 0);
576
577 AddDefaultFeedbackParams(&codec);
578 supported_codecs.push_back(codec);
579 }
580 return supported_codecs;
581}
582
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000583WebRtcVideoChannel2::WebRtcVideoChannel2(
pbos@webrtc.org42684be2014-10-03 11:25:45 +0000584 WebRtcCallFactory* call_factory,
pbos@webrtc.org3bf3d232014-10-31 12:59:34 +0000585 WebRtcVoiceEngine* voice_engine,
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000586 VoiceMediaChannel* voice_channel,
pbos@webrtc.orgfa553ef2014-10-20 11:07:07 +0000587 const VideoOptions& options,
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000588 WebRtcVideoEncoderFactory* external_encoder_factory,
pbos@webrtc.orgf1c8b902015-01-14 17:29:27 +0000589 WebRtcVideoDecoderFactory* external_decoder_factory)
pbos@webrtc.orgb648b9d2014-08-26 11:08:06 +0000590 : unsignalled_ssrc_handler_(&default_unsignalled_ssrc_handler_),
pbos@webrtc.org3bf3d232014-10-31 12:59:34 +0000591 voice_channel_(voice_channel),
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000592 external_encoder_factory_(external_encoder_factory),
pbos@webrtc.orgf1c8b902015-01-14 17:29:27 +0000593 external_decoder_factory_(external_decoder_factory) {
pbos@webrtc.orgfa553ef2014-10-20 11:07:07 +0000594 SetDefaultOptions();
595 options_.SetAll(options);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000596 webrtc::Call::Config config(this);
pbos@webrtc.org42684be2014-10-03 11:25:45 +0000597 config.overuse_callback = this;
pbos@webrtc.org3bf3d232014-10-31 12:59:34 +0000598 if (voice_engine != NULL) {
599 config.voice_engine = voice_engine->voe()->engine();
600 }
pbos@webrtc.orgfa553ef2014-10-20 11:07:07 +0000601
pbos@webrtc.org42684be2014-10-03 11:25:45 +0000602 call_.reset(call_factory->CreateCall(config));
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000603
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000604 rtcp_receiver_report_ssrc_ = kDefaultRtcpReceiverReportSsrc;
605 sending_ = false;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000606 default_send_ssrc_ = 0;
pbos@webrtc.org6f48f1b2014-07-22 16:29:54 +0000607}
608
609void WebRtcVideoChannel2::SetDefaultOptions() {
pbos@webrtc.org42684be2014-10-03 11:25:45 +0000610 options_.cpu_overuse_detection.Set(false);
pbos@webrtc.orgd8198032014-11-10 14:41:43 +0000611 options_.dscp.Set(false);
pbos@webrtc.org5ff71ab2014-07-23 07:28:56 +0000612 options_.suspend_below_min_bitrate.Set(false);
pbos@webrtc.org42684be2014-10-03 11:25:45 +0000613 options_.video_noise_reduction.Set(true);
pbos@webrtc.orgefc82c22014-10-27 13:58:00 +0000614 options_.screencast_min_bitrate.Set(0);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000615}
616
617WebRtcVideoChannel2::~WebRtcVideoChannel2() {
618 for (std::map<uint32, WebRtcVideoSendStream*>::iterator it =
619 send_streams_.begin();
620 it != send_streams_.end();
621 ++it) {
622 delete it->second;
623 }
624
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000625 for (std::map<uint32, WebRtcVideoReceiveStream*>::iterator it =
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000626 receive_streams_.begin();
627 it != receive_streams_.end();
628 ++it) {
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000629 delete it->second;
630 }
631}
632
633bool WebRtcVideoChannel2::Init() { return true; }
634
pbos@webrtc.org96a93252014-11-03 14:46:44 +0000635bool WebRtcVideoChannel2::CodecIsExternallySupported(
636 const std::string& name) const {
637 if (external_encoder_factory_ == NULL) {
638 return false;
639 }
640
641 const std::vector<WebRtcVideoEncoderFactory::VideoCodec> external_codecs =
642 external_encoder_factory_->codecs();
643 for (size_t c = 0; c < external_codecs.size(); ++c) {
644 if (CodecNameMatches(name, external_codecs[c].name)) {
645 return true;
646 }
647 }
648 return false;
649}
650
651std::vector<WebRtcVideoChannel2::VideoCodecSettings>
652WebRtcVideoChannel2::FilterSupportedCodecs(
653 const std::vector<WebRtcVideoChannel2::VideoCodecSettings>& mapped_codecs)
654 const {
655 std::vector<VideoCodecSettings> supported_codecs;
656 for (size_t i = 0; i < mapped_codecs.size(); ++i) {
657 const VideoCodecSettings& codec = mapped_codecs[i];
658 if (CodecIsInternallySupported(codec.codec.name) ||
659 CodecIsExternallySupported(codec.codec.name)) {
660 supported_codecs.push_back(codec);
661 }
662 }
663 return supported_codecs;
664}
665
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000666bool WebRtcVideoChannel2::SetRecvCodecs(const std::vector<VideoCodec>& codecs) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000667 TRACE_EVENT0("webrtc", "WebRtcVideoChannel2::SetRecvCodecs");
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000668 LOG(LS_INFO) << "SetRecvCodecs: " << CodecVectorToString(codecs);
669 if (!ValidateCodecFormats(codecs)) {
670 return false;
671 }
672
673 const std::vector<VideoCodecSettings> mapped_codecs = MapCodecs(codecs);
674 if (mapped_codecs.empty()) {
pbos@webrtc.org96a93252014-11-03 14:46:44 +0000675 LOG(LS_ERROR) << "SetRecvCodecs called without any video codecs.";
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000676 return false;
677 }
678
pbos@webrtc.org96a93252014-11-03 14:46:44 +0000679 const std::vector<VideoCodecSettings> supported_codecs =
680 FilterSupportedCodecs(mapped_codecs);
681
682 if (mapped_codecs.size() != supported_codecs.size()) {
683 LOG(LS_ERROR) << "SetRecvCodecs called with unsupported video codecs.";
684 return false;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000685 }
686
pbos@webrtc.org96a93252014-11-03 14:46:44 +0000687 recv_codecs_ = supported_codecs;
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000688
pbos@webrtc.org575d1262014-10-08 14:48:08 +0000689 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000690 for (std::map<uint32, WebRtcVideoReceiveStream*>::iterator it =
691 receive_streams_.begin();
692 it != receive_streams_.end();
693 ++it) {
694 it->second->SetRecvCodecs(recv_codecs_);
695 }
696
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000697 return true;
698}
699
700bool WebRtcVideoChannel2::SetSendCodecs(const std::vector<VideoCodec>& codecs) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000701 TRACE_EVENT0("webrtc", "WebRtcVideoChannel2::SetSendCodecs");
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000702 LOG(LS_INFO) << "SetSendCodecs: " << CodecVectorToString(codecs);
703 if (!ValidateCodecFormats(codecs)) {
704 return false;
705 }
706
707 const std::vector<VideoCodecSettings> supported_codecs =
708 FilterSupportedCodecs(MapCodecs(codecs));
709
710 if (supported_codecs.empty()) {
711 LOG(LS_ERROR) << "No video codecs supported by encoder factory.";
712 return false;
713 }
714
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000715 LOG(LS_INFO) << "Using codec: " << supported_codecs.front().codec.ToString();
716
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +0000717 VideoCodecSettings old_codec;
718 if (send_codec_.Get(&old_codec) && supported_codecs.front() == old_codec) {
719 // Using same codec, avoid reconfiguring.
720 return true;
721 }
722
723 send_codec_.Set(supported_codecs.front());
724
pbos@webrtc.org575d1262014-10-08 14:48:08 +0000725 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000726 for (std::map<uint32, WebRtcVideoSendStream*>::iterator it =
727 send_streams_.begin();
728 it != send_streams_.end();
729 ++it) {
730 assert(it->second != NULL);
731 it->second->SetCodec(supported_codecs.front());
732 }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000733
pbos@webrtc.org00873182014-11-25 14:03:34 +0000734 VideoCodec codec = supported_codecs.front().codec;
735 int bitrate_kbps;
736 if (codec.GetParam(kCodecParamMinBitrate, &bitrate_kbps) &&
737 bitrate_kbps > 0) {
738 bitrate_config_.min_bitrate_bps = bitrate_kbps * 1000;
739 } else {
740 bitrate_config_.min_bitrate_bps = 0;
741 }
742 if (codec.GetParam(kCodecParamStartBitrate, &bitrate_kbps) &&
743 bitrate_kbps > 0) {
744 bitrate_config_.start_bitrate_bps = bitrate_kbps * 1000;
745 } else {
746 // Do not reconfigure start bitrate unless it's specified and positive.
747 bitrate_config_.start_bitrate_bps = -1;
748 }
749 if (codec.GetParam(kCodecParamMaxBitrate, &bitrate_kbps) &&
750 bitrate_kbps > 0) {
751 bitrate_config_.max_bitrate_bps = bitrate_kbps * 1000;
752 } else {
753 bitrate_config_.max_bitrate_bps = -1;
754 }
755 call_->SetBitrateConfig(bitrate_config_);
756
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000757 return true;
758}
759
760bool WebRtcVideoChannel2::GetSendCodec(VideoCodec* codec) {
761 VideoCodecSettings codec_settings;
762 if (!send_codec_.Get(&codec_settings)) {
763 LOG(LS_VERBOSE) << "GetSendCodec: No send codec set.";
764 return false;
765 }
766 *codec = codec_settings.codec;
767 return true;
768}
769
770bool WebRtcVideoChannel2::SetSendStreamFormat(uint32 ssrc,
771 const VideoFormat& format) {
772 LOG(LS_VERBOSE) << "SetSendStreamFormat:" << ssrc << " -> "
773 << format.ToString();
pbos@webrtc.org575d1262014-10-08 14:48:08 +0000774 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000775 if (send_streams_.find(ssrc) == send_streams_.end()) {
776 return false;
777 }
778 return send_streams_[ssrc]->SetVideoFormat(format);
779}
780
781bool WebRtcVideoChannel2::SetRender(bool render) {
782 // TODO(pbos): Implement. Or refactor away as it shouldn't be needed.
783 LOG(LS_VERBOSE) << "SetRender: " << (render ? "true" : "false");
784 return true;
785}
786
787bool WebRtcVideoChannel2::SetSend(bool send) {
788 LOG(LS_VERBOSE) << "SetSend: " << (send ? "true" : "false");
789 if (send && !send_codec_.IsSet()) {
790 LOG(LS_ERROR) << "SetSend(true) called before setting codec.";
791 return false;
792 }
793 if (send) {
794 StartAllSendStreams();
795 } else {
796 StopAllSendStreams();
797 }
798 sending_ = send;
799 return true;
800}
801
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000802bool WebRtcVideoChannel2::AddSendStream(const StreamParams& sp) {
803 LOG(LS_INFO) << "AddSendStream: " << sp.ToString();
804 if (sp.ssrcs.empty()) {
805 LOG(LS_ERROR) << "No SSRCs in stream parameters.";
806 return false;
807 }
808
809 uint32 ssrc = sp.first_ssrc();
810 assert(ssrc != 0);
811 // TODO(pbos): Make sure none of sp.ssrcs are used, not just the identifying
812 // ssrc.
pbos@webrtc.org575d1262014-10-08 14:48:08 +0000813 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000814 if (send_streams_.find(ssrc) != send_streams_.end()) {
815 LOG(LS_ERROR) << "Send stream with ssrc '" << ssrc << "' already exists.";
816 return false;
817 }
818
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +0000819 std::vector<uint32> primary_ssrcs;
820 sp.GetPrimarySsrcs(&primary_ssrcs);
821 std::vector<uint32> rtx_ssrcs;
822 sp.GetFidSsrcs(primary_ssrcs, &rtx_ssrcs);
823 if (!rtx_ssrcs.empty() && primary_ssrcs.size() != rtx_ssrcs.size()) {
824 LOG(LS_ERROR)
825 << "RTX SSRCs exist, but don't cover all SSRCs (unsupported): "
826 << sp.ToString();
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000827 return false;
828 }
829
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000830 WebRtcVideoSendStream* stream =
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000831 new WebRtcVideoSendStream(call_.get(),
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000832 external_encoder_factory_,
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000833 options_,
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +0000834 send_codec_,
835 sp,
836 send_rtp_extensions_);
837
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000838 send_streams_[ssrc] = stream;
839
840 if (rtcp_receiver_report_ssrc_ == kDefaultRtcpReceiverReportSsrc) {
841 rtcp_receiver_report_ssrc_ = ssrc;
842 }
843 if (default_send_ssrc_ == 0) {
844 default_send_ssrc_ = ssrc;
845 }
846 if (sending_) {
847 stream->Start();
848 }
849
850 return true;
851}
852
853bool WebRtcVideoChannel2::RemoveSendStream(uint32 ssrc) {
854 LOG(LS_INFO) << "RemoveSendStream: " << ssrc;
855
856 if (ssrc == 0) {
857 if (default_send_ssrc_ == 0) {
858 LOG(LS_ERROR) << "No default send stream active.";
859 return false;
860 }
861
862 LOG(LS_VERBOSE) << "Removing default stream: " << default_send_ssrc_;
863 ssrc = default_send_ssrc_;
864 }
865
pbos@webrtc.org575d1262014-10-08 14:48:08 +0000866 WebRtcVideoSendStream* removed_stream;
867 {
868 rtc::CritScope stream_lock(&stream_crit_);
869 std::map<uint32, WebRtcVideoSendStream*>::iterator it =
870 send_streams_.find(ssrc);
871 if (it == send_streams_.end()) {
872 return false;
873 }
874
875 removed_stream = it->second;
876 send_streams_.erase(it);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000877 }
878
pbos@webrtc.org575d1262014-10-08 14:48:08 +0000879 delete removed_stream;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000880
881 if (ssrc == default_send_ssrc_) {
882 default_send_ssrc_ = 0;
883 }
884
885 return true;
886}
887
888bool WebRtcVideoChannel2::AddRecvStream(const StreamParams& sp) {
889 LOG(LS_INFO) << "AddRecvStream: " << sp.ToString();
890 assert(sp.ssrcs.size() > 0);
891
892 uint32 ssrc = sp.first_ssrc();
893 assert(ssrc != 0); // TODO(pbos): Is this ever valid?
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000894
895 // TODO(pbos): Check if any of the SSRCs overlap.
pbos@webrtc.org575d1262014-10-08 14:48:08 +0000896 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000897 if (receive_streams_.find(ssrc) != receive_streams_.end()) {
898 LOG(LS_ERROR) << "Receive stream for SSRC " << ssrc << "already exists.";
899 return false;
900 }
901
pbos@webrtc.orgbd249bc2014-07-07 04:45:15 +0000902 webrtc::VideoReceiveStream::Config config;
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000903 ConfigureReceiverRtp(&config, sp);
pbos@webrtc.org3bf3d232014-10-31 12:59:34 +0000904
905 // Set up A/V sync if there is a VoiceChannel.
906 // TODO(pbos): The A/V is synched by the receiving channel. So we need to know
907 // the SSRC of the remote audio channel in order to sync the correct webrtc
908 // VoiceEngine channel. For now sync the first channel in non-conference to
909 // match existing behavior in WebRtcVideoEngine.
910 if (voice_channel_ != NULL && receive_streams_.empty() &&
911 !options_.conference_mode.GetWithDefaultIfUnset(false)) {
912 config.audio_channel_id =
913 static_cast<WebRtcVoiceMediaChannel*>(voice_channel_)->voe_channel();
914 }
915
pbos@webrtc.org776e6f22014-10-29 15:28:39 +0000916 receive_streams_[ssrc] = new WebRtcVideoReceiveStream(
917 call_.get(), external_decoder_factory_, config, recv_codecs_);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000918
919 return true;
920}
921
922void WebRtcVideoChannel2::ConfigureReceiverRtp(
923 webrtc::VideoReceiveStream::Config* config,
924 const StreamParams& sp) const {
925 uint32 ssrc = sp.first_ssrc();
926
927 config->rtp.remote_ssrc = ssrc;
928 config->rtp.local_ssrc = rtcp_receiver_report_ssrc_;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000929
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000930 config->rtp.extensions = recv_rtp_extensions_;
pbos@webrtc.org257e1302014-07-25 19:01:32 +0000931
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000932 // TODO(pbos): This protection is against setting the same local ssrc as
933 // remote which is not permitted by the lower-level API. RTCP requires a
934 // corresponding sender SSRC. Figure out what to do when we don't have
935 // (receive-only) or know a good local SSRC.
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000936 if (config->rtp.remote_ssrc == config->rtp.local_ssrc) {
937 if (config->rtp.local_ssrc != kDefaultRtcpReceiverReportSsrc) {
938 config->rtp.local_ssrc = kDefaultRtcpReceiverReportSsrc;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000939 } else {
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000940 config->rtp.local_ssrc = kDefaultRtcpReceiverReportSsrc + 1;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000941 }
942 }
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000943
944 for (size_t i = 0; i < recv_codecs_.size(); ++i) {
andresp@webrtc.org82775b12014-11-07 09:37:54 +0000945 MergeFecConfig(recv_codecs_[i].fec, &config->rtp.fec);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000946 }
947
andresp@webrtc.org82775b12014-11-07 09:37:54 +0000948 for (size_t i = 0; i < recv_codecs_.size(); ++i) {
949 uint32 rtx_ssrc;
950 if (recv_codecs_[i].rtx_payload_type != -1 &&
951 sp.GetFidSsrc(ssrc, &rtx_ssrc)) {
952 webrtc::VideoReceiveStream::Config::Rtp::Rtx& rtx =
953 config->rtp.rtx[recv_codecs_[i].codec.id];
954 rtx.ssrc = rtx_ssrc;
955 rtx.payload_type = recv_codecs_[i].rtx_payload_type;
956 }
957 }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000958}
959
960bool WebRtcVideoChannel2::RemoveRecvStream(uint32 ssrc) {
961 LOG(LS_INFO) << "RemoveRecvStream: " << ssrc;
962 if (ssrc == 0) {
pbos@webrtc.orgafb554f42014-08-12 23:17:13 +0000963 LOG(LS_ERROR) << "RemoveRecvStream with 0 ssrc is not supported.";
964 return false;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000965 }
966
pbos@webrtc.org575d1262014-10-08 14:48:08 +0000967 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000968 std::map<uint32, WebRtcVideoReceiveStream*>::iterator stream =
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000969 receive_streams_.find(ssrc);
970 if (stream == receive_streams_.end()) {
971 LOG(LS_ERROR) << "Stream not found for ssrc: " << ssrc;
972 return false;
973 }
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000974 delete stream->second;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000975 receive_streams_.erase(stream);
976
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000977 return true;
978}
979
980bool WebRtcVideoChannel2::SetRenderer(uint32 ssrc, VideoRenderer* renderer) {
981 LOG(LS_INFO) << "SetRenderer: ssrc:" << ssrc << " "
982 << (renderer ? "(ptr)" : "NULL");
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000983 if (ssrc == 0) {
pbos@webrtc.orgafb554f42014-08-12 23:17:13 +0000984 default_unsignalled_ssrc_handler_.SetDefaultRenderer(this, renderer);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000985 return true;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000986 }
987
pbos@webrtc.org575d1262014-10-08 14:48:08 +0000988 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000989 std::map<uint32, WebRtcVideoReceiveStream*>::iterator it =
990 receive_streams_.find(ssrc);
991 if (it == receive_streams_.end()) {
992 return false;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000993 }
994
995 it->second->SetRenderer(renderer);
996 return true;
997}
998
999bool WebRtcVideoChannel2::GetRenderer(uint32 ssrc, VideoRenderer** renderer) {
1000 if (ssrc == 0) {
pbos@webrtc.orgafb554f42014-08-12 23:17:13 +00001001 *renderer = default_unsignalled_ssrc_handler_.GetDefaultRenderer();
1002 return *renderer != NULL;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001003 }
1004
pbos@webrtc.org575d1262014-10-08 14:48:08 +00001005 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001006 std::map<uint32, WebRtcVideoReceiveStream*>::iterator it =
1007 receive_streams_.find(ssrc);
1008 if (it == receive_streams_.end()) {
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001009 return false;
1010 }
1011 *renderer = it->second->GetRenderer();
1012 return true;
1013}
1014
1015bool WebRtcVideoChannel2::GetStats(const StatsOptions& options,
1016 VideoMediaInfo* info) {
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00001017 info->Clear();
1018 FillSenderStats(info);
1019 FillReceiverStats(info);
pbos@webrtc.org2b19f062014-12-11 13:26:09 +00001020 webrtc::Call::Stats stats = call_->GetStats();
1021 FillBandwidthEstimationStats(stats, info);
1022 if (stats.rtt_ms != -1) {
1023 for (size_t i = 0; i < info->senders.size(); ++i) {
1024 info->senders[i].rtt_ms = stats.rtt_ms;
1025 }
1026 }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001027 return true;
1028}
1029
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00001030void WebRtcVideoChannel2::FillSenderStats(VideoMediaInfo* video_media_info) {
pbos@webrtc.org575d1262014-10-08 14:48:08 +00001031 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00001032 for (std::map<uint32, WebRtcVideoSendStream*>::iterator it =
1033 send_streams_.begin();
1034 it != send_streams_.end();
1035 ++it) {
1036 video_media_info->senders.push_back(it->second->GetVideoSenderInfo());
1037 }
1038}
1039
1040void WebRtcVideoChannel2::FillReceiverStats(VideoMediaInfo* video_media_info) {
pbos@webrtc.org575d1262014-10-08 14:48:08 +00001041 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00001042 for (std::map<uint32, WebRtcVideoReceiveStream*>::iterator it =
1043 receive_streams_.begin();
1044 it != receive_streams_.end();
1045 ++it) {
1046 video_media_info->receivers.push_back(it->second->GetVideoReceiverInfo());
1047 }
1048}
1049
1050void WebRtcVideoChannel2::FillBandwidthEstimationStats(
pbos@webrtc.org2b19f062014-12-11 13:26:09 +00001051 const webrtc::Call::Stats& stats,
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00001052 VideoMediaInfo* video_media_info) {
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001053 BandwidthEstimationInfo bwe_info;
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001054 bwe_info.available_send_bandwidth = stats.send_bandwidth_bps;
1055 bwe_info.available_recv_bandwidth = stats.recv_bandwidth_bps;
1056 bwe_info.bucket_delay = stats.pacer_delay_ms;
1057
1058 // Get send stream bitrate stats.
1059 rtc::CritScope stream_lock(&stream_crit_);
1060 for (std::map<uint32, WebRtcVideoSendStream*>::iterator stream =
1061 send_streams_.begin();
1062 stream != send_streams_.end();
1063 ++stream) {
1064 stream->second->FillBandwidthEstimationInfo(&bwe_info);
1065 }
1066 video_media_info->bw_estimations.push_back(bwe_info);
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00001067}
1068
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001069bool WebRtcVideoChannel2::SetCapturer(uint32 ssrc, VideoCapturer* capturer) {
1070 LOG(LS_INFO) << "SetCapturer: " << ssrc << " -> "
1071 << (capturer != NULL ? "(capturer)" : "NULL");
1072 assert(ssrc != 0);
pbos@webrtc.org575d1262014-10-08 14:48:08 +00001073 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001074 if (send_streams_.find(ssrc) == send_streams_.end()) {
1075 LOG(LS_ERROR) << "No sending stream on ssrc " << ssrc;
1076 return false;
1077 }
1078 return send_streams_[ssrc]->SetCapturer(capturer);
1079}
1080
1081bool WebRtcVideoChannel2::SendIntraFrame() {
1082 // TODO(pbos): Implement.
1083 LOG(LS_VERBOSE) << "SendIntraFrame().";
1084 return true;
1085}
1086
1087bool WebRtcVideoChannel2::RequestIntraFrame() {
1088 // TODO(pbos): Implement.
1089 LOG(LS_VERBOSE) << "SendIntraFrame().";
1090 return true;
1091}
1092
1093void WebRtcVideoChannel2::OnPacketReceived(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001094 rtc::Buffer* packet,
1095 const rtc::PacketTime& packet_time) {
pbos@webrtc.org4e545cc2014-05-14 13:58:13 +00001096 const webrtc::PacketReceiver::DeliveryStatus delivery_result =
1097 call_->Receiver()->DeliverPacket(
1098 reinterpret_cast<const uint8_t*>(packet->data()), packet->length());
1099 switch (delivery_result) {
1100 case webrtc::PacketReceiver::DELIVERY_OK:
1101 return;
1102 case webrtc::PacketReceiver::DELIVERY_PACKET_ERROR:
1103 return;
1104 case webrtc::PacketReceiver::DELIVERY_UNKNOWN_SSRC:
1105 break;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001106 }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001107
1108 uint32 ssrc = 0;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001109 if (!GetRtpSsrc(packet->data(), packet->length(), &ssrc)) {
1110 return;
1111 }
1112
pbos@webrtc.orgafb554f42014-08-12 23:17:13 +00001113 // TODO(pbos): Make sure that the unsignalled SSRC uses the video payload.
1114 // Also figure out whether RTX needs to be handled.
1115 switch (unsignalled_ssrc_handler_->OnUnsignalledSsrc(this, ssrc)) {
1116 case UnsignalledSsrcHandler::kDropPacket:
1117 return;
1118 case UnsignalledSsrcHandler::kDeliverPacket:
1119 break;
1120 }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001121
pbos@webrtc.org1e019d12014-05-16 11:38:45 +00001122 if (call_->Receiver()->DeliverPacket(
1123 reinterpret_cast<const uint8_t*>(packet->data()), packet->length()) !=
1124 webrtc::PacketReceiver::DELIVERY_OK) {
pbos@webrtc.orgafb554f42014-08-12 23:17:13 +00001125 LOG(LS_WARNING) << "Failed to deliver RTP packet on re-delivery.";
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001126 return;
1127 }
1128}
1129
1130void WebRtcVideoChannel2::OnRtcpReceived(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001131 rtc::Buffer* packet,
1132 const rtc::PacketTime& packet_time) {
pbos@webrtc.org1e019d12014-05-16 11:38:45 +00001133 if (call_->Receiver()->DeliverPacket(
1134 reinterpret_cast<const uint8_t*>(packet->data()), packet->length()) !=
1135 webrtc::PacketReceiver::DELIVERY_OK) {
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001136 LOG(LS_WARNING) << "Failed to deliver RTCP packet.";
1137 }
1138}
1139
1140void WebRtcVideoChannel2::OnReadyToSend(bool ready) {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +00001141 LOG(LS_VERBOSE) << "OnReadyToSend: " << (ready ? "Ready." : "Not ready.");
1142 call_->SignalNetworkState(ready ? webrtc::Call::kNetworkUp
1143 : webrtc::Call::kNetworkDown);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001144}
1145
1146bool WebRtcVideoChannel2::MuteStream(uint32 ssrc, bool mute) {
1147 LOG(LS_VERBOSE) << "MuteStream: " << ssrc << " -> "
1148 << (mute ? "mute" : "unmute");
1149 assert(ssrc != 0);
pbos@webrtc.org575d1262014-10-08 14:48:08 +00001150 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001151 if (send_streams_.find(ssrc) == send_streams_.end()) {
1152 LOG(LS_ERROR) << "No sending stream on ssrc " << ssrc;
1153 return false;
1154 }
pbos@webrtc.orgef8bb8d2014-08-13 21:36:18 +00001155
1156 send_streams_[ssrc]->MuteStream(mute);
1157 return true;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001158}
1159
1160bool WebRtcVideoChannel2::SetRecvRtpHeaderExtensions(
1161 const std::vector<RtpHeaderExtension>& extensions) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +00001162 TRACE_EVENT0("webrtc", "WebRtcVideoChannel2::SetRecvRtpHeaderExtensions");
pbos@webrtc.org587ef602014-06-16 17:32:02 +00001163 LOG(LS_INFO) << "SetRecvRtpHeaderExtensions: "
1164 << RtpExtensionsToString(extensions);
pbos@webrtc.org3c107582014-07-20 15:27:35 +00001165 if (!ValidateRtpHeaderExtensionIds(extensions))
1166 return false;
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001167
pbos@webrtc.orgc37e72e2015-01-05 18:51:13 +00001168 std::vector<webrtc::RtpExtension> filtered_extensions =
1169 FilterRtpExtensions(extensions);
1170 if (!RtpExtensionsHaveChanged(recv_rtp_extensions_, filtered_extensions))
1171 return true;
1172
1173 recv_rtp_extensions_ = filtered_extensions;
1174
pbos@webrtc.org575d1262014-10-08 14:48:08 +00001175 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001176 for (std::map<uint32, WebRtcVideoReceiveStream*>::iterator it =
1177 receive_streams_.begin();
1178 it != receive_streams_.end();
1179 ++it) {
1180 it->second->SetRtpExtensions(recv_rtp_extensions_);
1181 }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001182 return true;
1183}
1184
1185bool WebRtcVideoChannel2::SetSendRtpHeaderExtensions(
1186 const std::vector<RtpHeaderExtension>& extensions) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +00001187 TRACE_EVENT0("webrtc", "WebRtcVideoChannel2::SetSendRtpHeaderExtensions");
pbos@webrtc.org587ef602014-06-16 17:32:02 +00001188 LOG(LS_INFO) << "SetSendRtpHeaderExtensions: "
1189 << RtpExtensionsToString(extensions);
pbos@webrtc.org3c107582014-07-20 15:27:35 +00001190 if (!ValidateRtpHeaderExtensionIds(extensions))
1191 return false;
1192
pbos@webrtc.orgc37e72e2015-01-05 18:51:13 +00001193 std::vector<webrtc::RtpExtension> filtered_extensions =
1194 FilterRtpExtensions(extensions);
1195 if (!RtpExtensionsHaveChanged(send_rtp_extensions_, filtered_extensions))
1196 return true;
1197
1198 send_rtp_extensions_ = filtered_extensions;
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +00001199
pbos@webrtc.org575d1262014-10-08 14:48:08 +00001200 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001201 for (std::map<uint32, WebRtcVideoSendStream*>::iterator it =
1202 send_streams_.begin();
1203 it != send_streams_.end();
1204 ++it) {
1205 it->second->SetRtpExtensions(send_rtp_extensions_);
1206 }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001207 return true;
1208}
1209
pbos@webrtc.org00873182014-11-25 14:03:34 +00001210bool WebRtcVideoChannel2::SetMaxSendBandwidth(int max_bitrate_bps) {
1211 LOG(LS_INFO) << "SetMaxSendBandwidth: " << max_bitrate_bps << "bps.";
1212 if (max_bitrate_bps <= 0) {
1213 // Unsetting max bitrate.
1214 max_bitrate_bps = -1;
1215 }
1216 bitrate_config_.start_bitrate_bps = -1;
1217 bitrate_config_.max_bitrate_bps = max_bitrate_bps;
1218 if (max_bitrate_bps > 0 &&
1219 bitrate_config_.min_bitrate_bps > max_bitrate_bps) {
1220 bitrate_config_.min_bitrate_bps = max_bitrate_bps;
1221 }
1222 call_->SetBitrateConfig(bitrate_config_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001223 return true;
1224}
1225
1226bool WebRtcVideoChannel2::SetOptions(const VideoOptions& options) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +00001227 TRACE_EVENT0("webrtc", "WebRtcVideoChannel2::SetOptions");
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +00001228 LOG(LS_INFO) << "SetOptions: " << options.ToString();
1229 VideoOptions old_options = options_;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001230 options_.SetAll(options);
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +00001231 if (options_ == old_options) {
1232 // No new options to set.
1233 return true;
1234 }
pbos@webrtc.orgd8198032014-11-10 14:41:43 +00001235 rtc::DiffServCodePoint dscp = options_.dscp.GetWithDefaultIfUnset(false)
1236 ? rtc::DSCP_AF41
1237 : rtc::DSCP_DEFAULT;
1238 MediaChannel::SetDscp(dscp);
pbos@webrtc.org575d1262014-10-08 14:48:08 +00001239 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001240 for (std::map<uint32, WebRtcVideoSendStream*>::iterator it =
1241 send_streams_.begin();
1242 it != send_streams_.end();
1243 ++it) {
1244 it->second->SetOptions(options_);
1245 }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001246 return true;
1247}
1248
1249void WebRtcVideoChannel2::SetInterface(NetworkInterface* iface) {
1250 MediaChannel::SetInterface(iface);
1251 // Set the RTP recv/send buffer to a bigger size
1252 MediaChannel::SetOption(NetworkInterface::ST_RTP,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001253 rtc::Socket::OPT_RCVBUF,
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001254 kVideoRtpBufferSize);
1255
buildbot@webrtc.orgae694ef2014-10-28 17:37:17 +00001256 // Speculative change to increase the outbound socket buffer size.
1257 // In b/15152257, we are seeing a significant number of packets discarded
1258 // due to lack of socket buffer space, although it's not yet clear what the
1259 // ideal value should be.
1260 MediaChannel::SetOption(NetworkInterface::ST_RTP,
1261 rtc::Socket::OPT_SNDBUF,
1262 kVideoRtpBufferSize);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001263}
1264
1265void WebRtcVideoChannel2::UpdateAspectRatio(int ratio_w, int ratio_h) {
1266 // TODO(pbos): Implement.
1267}
1268
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001269void WebRtcVideoChannel2::OnMessage(rtc::Message* msg) {
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001270 // Ignored.
1271}
1272
pbos@webrtc.org42684be2014-10-03 11:25:45 +00001273void WebRtcVideoChannel2::OnLoadUpdate(Load load) {
pbos@webrtc.org575d1262014-10-08 14:48:08 +00001274 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.org42684be2014-10-03 11:25:45 +00001275 for (std::map<uint32, WebRtcVideoSendStream*>::iterator it =
1276 send_streams_.begin();
1277 it != send_streams_.end();
1278 ++it) {
1279 it->second->OnCpuResolutionRequest(load == kOveruse
1280 ? CoordinatedVideoAdapter::DOWNGRADE
1281 : CoordinatedVideoAdapter::UPGRADE);
1282 }
1283}
1284
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001285bool WebRtcVideoChannel2::SendRtp(const uint8_t* data, size_t len) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001286 rtc::Buffer packet(data, len, kMaxRtpPacketLen);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001287 return MediaChannel::SendPacket(&packet);
1288}
1289
1290bool WebRtcVideoChannel2::SendRtcp(const uint8_t* data, size_t len) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001291 rtc::Buffer packet(data, len, kMaxRtpPacketLen);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001292 return MediaChannel::SendRtcp(&packet);
1293}
1294
1295void WebRtcVideoChannel2::StartAllSendStreams() {
pbos@webrtc.org575d1262014-10-08 14:48:08 +00001296 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001297 for (std::map<uint32, WebRtcVideoSendStream*>::iterator it =
1298 send_streams_.begin();
1299 it != send_streams_.end();
1300 ++it) {
1301 it->second->Start();
1302 }
1303}
1304
1305void WebRtcVideoChannel2::StopAllSendStreams() {
pbos@webrtc.org575d1262014-10-08 14:48:08 +00001306 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001307 for (std::map<uint32, WebRtcVideoSendStream*>::iterator it =
1308 send_streams_.begin();
1309 it != send_streams_.end();
1310 ++it) {
1311 it->second->Stop();
1312 }
1313}
1314
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +00001315WebRtcVideoChannel2::WebRtcVideoSendStream::VideoSendStreamParameters::
1316 VideoSendStreamParameters(
1317 const webrtc::VideoSendStream::Config& config,
1318 const VideoOptions& options,
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001319 const Settable<VideoCodecSettings>& codec_settings)
1320 : config(config), options(options), codec_settings(codec_settings) {
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +00001321}
1322
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001323WebRtcVideoChannel2::WebRtcVideoSendStream::WebRtcVideoSendStream(
1324 webrtc::Call* call,
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +00001325 WebRtcVideoEncoderFactory* external_encoder_factory,
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +00001326 const VideoOptions& options,
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001327 const Settable<VideoCodecSettings>& codec_settings,
1328 const StreamParams& sp,
1329 const std::vector<webrtc::RtpExtension>& rtp_extensions)
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001330 : call_(call),
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +00001331 external_encoder_factory_(external_encoder_factory),
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001332 stream_(NULL),
pbos@webrtc.orgb648b9d2014-08-26 11:08:06 +00001333 parameters_(webrtc::VideoSendStream::Config(), options, codec_settings),
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +00001334 allocated_encoder_(NULL, webrtc::kVideoCodecUnknown, false),
pbos@webrtc.orgb648b9d2014-08-26 11:08:06 +00001335 capturer_(NULL),
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001336 sending_(false),
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001337 muted_(false) {
1338 parameters_.config.rtp.max_packet_size = kVideoMtu;
1339
1340 sp.GetPrimarySsrcs(&parameters_.config.rtp.ssrcs);
1341 sp.GetFidSsrcs(parameters_.config.rtp.ssrcs,
1342 &parameters_.config.rtp.rtx.ssrcs);
1343 parameters_.config.rtp.c_name = sp.cname;
1344 parameters_.config.rtp.extensions = rtp_extensions;
1345
1346 VideoCodecSettings params;
1347 if (codec_settings.Get(&params)) {
1348 SetCodec(params);
1349 }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001350}
1351
1352WebRtcVideoChannel2::WebRtcVideoSendStream::~WebRtcVideoSendStream() {
1353 DisconnectCapturer();
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001354 if (stream_ != NULL) {
1355 call_->DestroyVideoSendStream(stream_);
1356 }
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +00001357 DestroyVideoEncoder(&allocated_encoder_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001358}
1359
1360static void SetWebRtcFrameToBlack(webrtc::I420VideoFrame* video_frame) {
1361 assert(video_frame != NULL);
1362 memset(video_frame->buffer(webrtc::kYPlane),
1363 16,
1364 video_frame->allocated_size(webrtc::kYPlane));
1365 memset(video_frame->buffer(webrtc::kUPlane),
1366 128,
1367 video_frame->allocated_size(webrtc::kUPlane));
1368 memset(video_frame->buffer(webrtc::kVPlane),
1369 128,
1370 video_frame->allocated_size(webrtc::kVPlane));
1371}
1372
1373static void CreateBlackFrame(webrtc::I420VideoFrame* video_frame,
1374 int width,
1375 int height) {
1376 video_frame->CreateEmptyFrame(
1377 width, height, width, (width + 1) / 2, (width + 1) / 2);
1378 SetWebRtcFrameToBlack(video_frame);
1379}
1380
1381static void ConvertToI420VideoFrame(const VideoFrame& frame,
1382 webrtc::I420VideoFrame* i420_frame) {
1383 i420_frame->CreateFrame(
1384 static_cast<int>(frame.GetYPitch() * frame.GetHeight()),
1385 frame.GetYPlane(),
1386 static_cast<int>(frame.GetUPitch() * ((frame.GetHeight() + 1) / 2)),
1387 frame.GetUPlane(),
1388 static_cast<int>(frame.GetVPitch() * ((frame.GetHeight() + 1) / 2)),
1389 frame.GetVPlane(),
1390 static_cast<int>(frame.GetWidth()),
1391 static_cast<int>(frame.GetHeight()),
1392 static_cast<int>(frame.GetYPitch()),
1393 static_cast<int>(frame.GetUPitch()),
1394 static_cast<int>(frame.GetVPitch()));
1395}
1396
1397void WebRtcVideoChannel2::WebRtcVideoSendStream::InputFrame(
1398 VideoCapturer* capturer,
1399 const VideoFrame* frame) {
1400 LOG(LS_VERBOSE) << "InputFrame: " << frame->GetWidth() << "x"
1401 << frame->GetHeight();
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001402 // Lock before copying, can be called concurrently when swapping input source.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001403 rtc::CritScope frame_cs(&frame_lock_);
pbos@webrtc.orgd60d79a2014-09-24 07:10:57 +00001404 ConvertToI420VideoFrame(*frame, &video_frame_);
1405
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001406 rtc::CritScope cs(&lock_);
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001407 if (stream_ == NULL) {
1408 LOG(LS_WARNING) << "Capturer inputting frames before send codecs are "
1409 "configured, dropping.";
1410 return;
1411 }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001412 if (format_.width == 0) { // Dropping frames.
1413 assert(format_.height == 0);
1414 LOG(LS_VERBOSE) << "VideoFormat 0x0 set, Dropping frame.";
1415 return;
1416 }
pbos@webrtc.orgd60d79a2014-09-24 07:10:57 +00001417 if (muted_) {
1418 // Create a black frame to transmit instead.
1419 CreateBlackFrame(&video_frame_,
1420 static_cast<int>(frame->GetWidth()),
1421 static_cast<int>(frame->GetHeight()));
1422 }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001423 // Reconfigure codec if necessary.
pbos@webrtc.orgc4175b92014-09-03 15:25:49 +00001424 SetDimensions(
1425 video_frame_.width(), video_frame_.height(), capturer->IsScreencast());
1426
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001427 LOG(LS_VERBOSE) << "SwapFrame: " << video_frame_.width() << "x"
1428 << video_frame_.height() << " -> (codec) "
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +00001429 << parameters_.encoder_config.streams.back().width << "x"
1430 << parameters_.encoder_config.streams.back().height;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001431 stream_->Input()->SwapFrame(&video_frame_);
1432}
1433
1434bool WebRtcVideoChannel2::WebRtcVideoSendStream::SetCapturer(
1435 VideoCapturer* capturer) {
1436 if (!DisconnectCapturer() && capturer == NULL) {
1437 return false;
1438 }
1439
1440 {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001441 rtc::CritScope cs(&lock_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001442
pbos@webrtc.org9359cb32014-07-23 15:44:48 +00001443 if (capturer == NULL) {
1444 if (stream_ != NULL) {
1445 LOG(LS_VERBOSE) << "Disabling capturer, sending black frame.";
1446 webrtc::I420VideoFrame black_frame;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001447
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +00001448 // TODO(pbos): Base width/height on last_dimensions_. This will however
1449 // fail the test AddRemoveCapturer which needs to be fixed to permit
1450 // sending black frames in the same size that was previously sent.
pbos@webrtc.org9359cb32014-07-23 15:44:48 +00001451 int width = format_.width;
1452 int height = format_.height;
1453 int half_width = (width + 1) / 2;
1454 black_frame.CreateEmptyFrame(
1455 width, height, width, half_width, half_width);
1456 SetWebRtcFrameToBlack(&black_frame);
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +00001457 SetDimensions(width, height, last_dimensions_.is_screencast);
pbos@webrtc.org9359cb32014-07-23 15:44:48 +00001458 stream_->Input()->SwapFrame(&black_frame);
1459 }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001460
1461 capturer_ = NULL;
1462 return true;
1463 }
1464
1465 capturer_ = capturer;
1466 }
1467 // Lock cannot be held while connecting the capturer to prevent lock-order
1468 // violations.
1469 capturer->SignalVideoFrame.connect(this, &WebRtcVideoSendStream::InputFrame);
1470 return true;
1471}
1472
1473bool WebRtcVideoChannel2::WebRtcVideoSendStream::SetVideoFormat(
1474 const VideoFormat& format) {
1475 if ((format.width == 0 || format.height == 0) &&
1476 format.width != format.height) {
1477 LOG(LS_ERROR) << "Can't set VideoFormat, width or height is zero (but not "
1478 "both, 0x0 drops frames).";
1479 return false;
1480 }
1481
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001482 rtc::CritScope cs(&lock_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001483 if (format.width == 0 && format.height == 0) {
1484 LOG(LS_INFO)
1485 << "0x0 resolution selected. Captured frames will be dropped for ssrc: "
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +00001486 << parameters_.config.rtp.ssrcs[0] << ".";
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001487 } else {
1488 // TODO(pbos): Fix me, this only affects the last stream!
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +00001489 parameters_.encoder_config.streams.back().max_framerate =
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001490 VideoFormat::IntervalToFps(format.interval);
pbos@webrtc.orgc4175b92014-09-03 15:25:49 +00001491 SetDimensions(format.width, format.height, false);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001492 }
1493
1494 format_ = format;
1495 return true;
1496}
1497
pbos@webrtc.orgef8bb8d2014-08-13 21:36:18 +00001498void WebRtcVideoChannel2::WebRtcVideoSendStream::MuteStream(bool mute) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001499 rtc::CritScope cs(&lock_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001500 muted_ = mute;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001501}
1502
1503bool WebRtcVideoChannel2::WebRtcVideoSendStream::DisconnectCapturer() {
pbos@webrtc.org963b9792014-10-07 14:27:27 +00001504 cricket::VideoCapturer* capturer;
1505 {
1506 rtc::CritScope cs(&lock_);
1507 if (capturer_ == NULL) {
1508 return false;
1509 }
1510 capturer = capturer_;
1511 capturer_ = NULL;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001512 }
pbos@webrtc.org963b9792014-10-07 14:27:27 +00001513 capturer->SignalVideoFrame.disconnect(this);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001514 return true;
1515}
1516
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001517void WebRtcVideoChannel2::WebRtcVideoSendStream::SetOptions(
1518 const VideoOptions& options) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001519 rtc::CritScope cs(&lock_);
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001520 VideoCodecSettings codec_settings;
1521 if (parameters_.codec_settings.Get(&codec_settings)) {
1522 SetCodecAndOptions(codec_settings, options);
1523 } else {
1524 parameters_.options = options;
1525 }
1526}
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +00001527
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001528void WebRtcVideoChannel2::WebRtcVideoSendStream::SetCodec(
1529 const VideoCodecSettings& codec_settings) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001530 rtc::CritScope cs(&lock_);
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001531 SetCodecAndOptions(codec_settings, parameters_.options);
1532}
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +00001533
1534webrtc::VideoCodecType CodecTypeFromName(const std::string& name) {
1535 if (CodecNameMatches(name, kVp8CodecName)) {
1536 return webrtc::kVideoCodecVP8;
andresp@webrtc.org188d3b22014-11-07 13:21:04 +00001537 } else if (CodecNameMatches(name, kVp9CodecName)) {
1538 return webrtc::kVideoCodecVP9;
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +00001539 } else if (CodecNameMatches(name, kH264CodecName)) {
1540 return webrtc::kVideoCodecH264;
1541 }
1542 return webrtc::kVideoCodecUnknown;
1543}
1544
1545WebRtcVideoChannel2::WebRtcVideoSendStream::AllocatedEncoder
1546WebRtcVideoChannel2::WebRtcVideoSendStream::CreateVideoEncoder(
1547 const VideoCodec& codec) {
1548 webrtc::VideoCodecType type = CodecTypeFromName(codec.name);
1549
1550 // Do not re-create encoders of the same type.
1551 if (type == allocated_encoder_.type && allocated_encoder_.encoder != NULL) {
1552 return allocated_encoder_;
1553 }
1554
1555 if (external_encoder_factory_ != NULL) {
1556 webrtc::VideoEncoder* encoder =
1557 external_encoder_factory_->CreateVideoEncoder(type);
1558 if (encoder != NULL) {
1559 return AllocatedEncoder(encoder, type, true);
1560 }
1561 }
1562
1563 if (type == webrtc::kVideoCodecVP8) {
1564 return AllocatedEncoder(
1565 webrtc::VideoEncoder::Create(webrtc::VideoEncoder::kVp8), type, false);
andresp@webrtc.org188d3b22014-11-07 13:21:04 +00001566 } else if (type == webrtc::kVideoCodecVP9) {
1567 return AllocatedEncoder(
1568 webrtc::VideoEncoder::Create(webrtc::VideoEncoder::kVp9), type, false);
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +00001569 }
1570
1571 // This shouldn't happen, we should not be trying to create something we don't
1572 // support.
1573 assert(false);
1574 return AllocatedEncoder(NULL, webrtc::kVideoCodecUnknown, false);
1575}
1576
1577void WebRtcVideoChannel2::WebRtcVideoSendStream::DestroyVideoEncoder(
1578 AllocatedEncoder* encoder) {
1579 if (encoder->external) {
1580 external_encoder_factory_->DestroyVideoEncoder(encoder->encoder);
1581 } else {
1582 delete encoder->encoder;
1583 }
1584}
1585
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001586void WebRtcVideoChannel2::WebRtcVideoSendStream::SetCodecAndOptions(
1587 const VideoCodecSettings& codec_settings,
1588 const VideoOptions& options) {
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +00001589 if (last_dimensions_.width == -1) {
1590 last_dimensions_.width = codec_settings.codec.width;
1591 last_dimensions_.height = codec_settings.codec.height;
1592 last_dimensions_.is_screencast = false;
1593 }
1594 parameters_.encoder_config =
1595 CreateVideoEncoderConfig(last_dimensions_, codec_settings.codec);
1596 if (parameters_.encoder_config.streams.empty()) {
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001597 return;
1598 }
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +00001599
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001600 format_ = VideoFormat(codec_settings.codec.width,
1601 codec_settings.codec.height,
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001602 VideoFormat::FpsToInterval(30),
1603 FOURCC_I420);
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +00001604
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +00001605 AllocatedEncoder new_encoder = CreateVideoEncoder(codec_settings.codec);
1606 parameters_.config.encoder_settings.encoder = new_encoder.encoder;
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001607 parameters_.config.encoder_settings.payload_name = codec_settings.codec.name;
1608 parameters_.config.encoder_settings.payload_type = codec_settings.codec.id;
1609 parameters_.config.rtp.fec = codec_settings.fec;
1610
1611 // Set RTX payload type if RTX is enabled.
1612 if (!parameters_.config.rtp.rtx.ssrcs.empty()) {
1613 parameters_.config.rtp.rtx.payload_type = codec_settings.rtx_payload_type;
1614 }
1615
1616 if (IsNackEnabled(codec_settings.codec)) {
1617 parameters_.config.rtp.nack.rtp_history_ms = kNackHistoryMs;
1618 }
1619
pbos@webrtc.org5ff71ab2014-07-23 07:28:56 +00001620 options.suspend_below_min_bitrate.Get(
1621 &parameters_.config.suspend_below_min_bitrate);
1622
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001623 parameters_.codec_settings.Set(codec_settings);
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +00001624 parameters_.options = options;
pbos@webrtc.org543e5892014-07-23 07:01:31 +00001625
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001626 RecreateWebRtcStream();
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +00001627 if (allocated_encoder_.encoder != new_encoder.encoder) {
1628 DestroyVideoEncoder(&allocated_encoder_);
1629 allocated_encoder_ = new_encoder;
1630 }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001631}
1632
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001633void WebRtcVideoChannel2::WebRtcVideoSendStream::SetRtpExtensions(
1634 const std::vector<webrtc::RtpExtension>& rtp_extensions) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001635 rtc::CritScope cs(&lock_);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001636 parameters_.config.rtp.extensions = rtp_extensions;
1637 RecreateWebRtcStream();
1638}
1639
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +00001640webrtc::VideoEncoderConfig
1641WebRtcVideoChannel2::WebRtcVideoSendStream::CreateVideoEncoderConfig(
1642 const Dimensions& dimensions,
1643 const VideoCodec& codec) const {
1644 webrtc::VideoEncoderConfig encoder_config;
1645 if (dimensions.is_screencast) {
pbos@webrtc.orgefc82c22014-10-27 13:58:00 +00001646 int screencast_min_bitrate_kbps;
1647 parameters_.options.screencast_min_bitrate.Get(
1648 &screencast_min_bitrate_kbps);
1649 encoder_config.min_transmit_bitrate_bps =
1650 screencast_min_bitrate_kbps * 1000;
1651 encoder_config.content_type = webrtc::VideoEncoderConfig::kScreenshare;
1652 } else {
1653 encoder_config.min_transmit_bitrate_bps = 0;
1654 encoder_config.content_type = webrtc::VideoEncoderConfig::kRealtimeVideo;
1655 }
1656
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +00001657 // Restrict dimensions according to codec max.
1658 int width = dimensions.width;
1659 int height = dimensions.height;
1660 if (!dimensions.is_screencast) {
1661 if (codec.width < width)
1662 width = codec.width;
1663 if (codec.height < height)
1664 height = codec.height;
1665 }
1666
1667 VideoCodec clamped_codec = codec;
1668 clamped_codec.width = width;
1669 clamped_codec.height = height;
pbos@webrtc.orgcddd17c2014-09-16 16:33:13 +00001670
pbos@webrtc.orgf1c8b902015-01-14 17:29:27 +00001671 encoder_config.streams = CreateVideoStreams(
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +00001672 clamped_codec, parameters_.options, parameters_.config.rtp.ssrcs.size());
pbos@webrtc.org6f48f1b2014-07-22 16:29:54 +00001673
pbos@webrtc.orgb7ed7792014-10-31 13:08:10 +00001674 // Conference mode screencast uses 2 temporal layers split at 100kbit.
1675 if (parameters_.options.conference_mode.GetWithDefaultIfUnset(false) &&
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +00001676 dimensions.is_screencast && encoder_config.streams.size() == 1) {
sprang@webrtc.org46d4d292014-12-23 15:19:35 +00001677 ScreenshareLayerConfig config = ScreenshareLayerConfig::GetDefault();
1678
1679 // For screenshare in conference mode, tl0 and tl1 bitrates are piggybacked
1680 // on the VideoCodec struct as target and max bitrates, respectively.
1681 // See eg. webrtc::VP8EncoderImpl::SetRates().
1682 encoder_config.streams[0].target_bitrate_bps =
1683 config.tl0_bitrate_kbps * 1000;
1684 encoder_config.streams[0].max_bitrate_bps = config.tl1_bitrate_kbps * 1000;
pbos@webrtc.orgb7ed7792014-10-31 13:08:10 +00001685 encoder_config.streams[0].temporal_layer_thresholds_bps.clear();
1686 encoder_config.streams[0].temporal_layer_thresholds_bps.push_back(
sprang@webrtc.org46d4d292014-12-23 15:19:35 +00001687 config.tl0_bitrate_kbps * 1000);
pbos@webrtc.orgb7ed7792014-10-31 13:08:10 +00001688 }
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +00001689 return encoder_config;
1690}
1691
1692void WebRtcVideoChannel2::WebRtcVideoSendStream::SetDimensions(
1693 int width,
1694 int height,
1695 bool is_screencast) {
1696 if (last_dimensions_.width == width && last_dimensions_.height == height &&
1697 last_dimensions_.is_screencast == is_screencast) {
1698 // Configured using the same parameters, do not reconfigure.
1699 return;
1700 }
1701 LOG(LS_INFO) << "SetDimensions: " << width << "x" << height
1702 << (is_screencast ? " (screencast)" : " (not screencast)");
1703
1704 last_dimensions_.width = width;
1705 last_dimensions_.height = height;
1706 last_dimensions_.is_screencast = is_screencast;
1707
1708 assert(!parameters_.encoder_config.streams.empty());
1709
1710 VideoCodecSettings codec_settings;
1711 parameters_.codec_settings.Get(&codec_settings);
1712
1713 webrtc::VideoEncoderConfig encoder_config =
1714 CreateVideoEncoderConfig(last_dimensions_, codec_settings.codec);
1715
1716 encoder_config.encoder_specific_settings =
pbos@webrtc.orgf1c8b902015-01-14 17:29:27 +00001717 ConfigureVideoEncoderSettings(codec_settings.codec, parameters_.options);
pbos@webrtc.orgb7ed7792014-10-31 13:08:10 +00001718
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +00001719 bool stream_reconfigured = stream_->ReconfigureVideoEncoder(encoder_config);
1720
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +00001721 encoder_config.encoder_specific_settings = NULL;
pbos@webrtc.org6f48f1b2014-07-22 16:29:54 +00001722
1723 if (!stream_reconfigured) {
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001724 LOG(LS_WARNING) << "Failed to reconfigure video encoder for dimensions: "
1725 << width << "x" << height;
1726 return;
1727 }
pbos@webrtc.orgcddd17c2014-09-16 16:33:13 +00001728
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +00001729 parameters_.encoder_config = encoder_config;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001730}
1731
1732void WebRtcVideoChannel2::WebRtcVideoSendStream::Start() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001733 rtc::CritScope cs(&lock_);
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001734 assert(stream_ != NULL);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001735 stream_->Start();
1736 sending_ = true;
1737}
1738
1739void WebRtcVideoChannel2::WebRtcVideoSendStream::Stop() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001740 rtc::CritScope cs(&lock_);
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001741 if (stream_ != NULL) {
1742 stream_->Stop();
1743 }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001744 sending_ = false;
1745}
1746
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00001747VideoSenderInfo
1748WebRtcVideoChannel2::WebRtcVideoSendStream::GetVideoSenderInfo() {
1749 VideoSenderInfo info;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001750 rtc::CritScope cs(&lock_);
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00001751 for (size_t i = 0; i < parameters_.config.rtp.ssrcs.size(); ++i) {
1752 info.add_ssrc(parameters_.config.rtp.ssrcs[i]);
1753 }
1754
pbos@webrtc.orgc3d2bd22014-08-12 20:55:10 +00001755 if (stream_ == NULL) {
1756 return info;
1757 }
1758
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00001759 webrtc::VideoSendStream::Stats stats = stream_->GetStats();
1760 info.framerate_input = stats.input_frame_rate;
1761 info.framerate_sent = stats.encode_frame_rate;
1762
pbos@webrtc.org273a4142014-12-01 15:23:21 +00001763 info.send_frame_width = 0;
1764 info.send_frame_height = 0;
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001765 for (std::map<uint32_t, webrtc::SsrcStats>::iterator it =
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00001766 stats.substreams.begin();
1767 it != stats.substreams.end();
1768 ++it) {
1769 // TODO(pbos): Wire up additional stats, such as padding bytes.
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001770 webrtc::SsrcStats stream_stats = it->second;
asapersson@webrtc.orgcfd82df2015-01-22 09:39:59 +00001771 info.bytes_sent += stream_stats.rtp_stats.transmitted.payload_bytes +
1772 stream_stats.rtp_stats.transmitted.header_bytes +
1773 stream_stats.rtp_stats.transmitted.padding_bytes;
1774 info.packets_sent += stream_stats.rtp_stats.transmitted.packets;
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00001775 info.packets_lost += stream_stats.rtcp_stats.cumulative_lost;
pbos@webrtc.org273a4142014-12-01 15:23:21 +00001776 if (stream_stats.sent_width > info.send_frame_width)
1777 info.send_frame_width = stream_stats.sent_width;
1778 if (stream_stats.sent_height > info.send_frame_height)
1779 info.send_frame_height = stream_stats.sent_height;
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00001780 }
1781
1782 if (!stats.substreams.empty()) {
1783 // TODO(pbos): Report fraction lost per SSRC.
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001784 webrtc::SsrcStats first_stream_stats = stats.substreams.begin()->second;
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00001785 info.fraction_lost =
1786 static_cast<float>(first_stream_stats.rtcp_stats.fraction_lost) /
1787 (1 << 8);
1788 }
1789
1790 if (capturer_ != NULL && !capturer_->IsMuted()) {
1791 VideoFormat last_captured_frame_format;
1792 capturer_->GetStats(&info.adapt_frame_drops,
1793 &info.effects_frame_drops,
1794 &info.capturer_frame_time,
1795 &last_captured_frame_format);
1796 info.input_frame_width = last_captured_frame_format.width;
1797 info.input_frame_height = last_captured_frame_format.height;
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00001798 }
1799
1800 // TODO(pbos): Support or remove the following stats.
1801 info.packets_cached = -1;
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00001802
1803 return info;
1804}
1805
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001806void WebRtcVideoChannel2::WebRtcVideoSendStream::FillBandwidthEstimationInfo(
1807 BandwidthEstimationInfo* bwe_info) {
1808 rtc::CritScope cs(&lock_);
1809 if (stream_ == NULL) {
1810 return;
1811 }
1812 webrtc::VideoSendStream::Stats stats = stream_->GetStats();
1813 for (std::map<uint32_t, webrtc::SsrcStats>::iterator it =
1814 stats.substreams.begin();
1815 it != stats.substreams.end();
1816 ++it) {
1817 bwe_info->transmit_bitrate += it->second.total_bitrate_bps;
1818 bwe_info->retransmit_bitrate += it->second.retransmit_bitrate_bps;
1819 }
1820 bwe_info->actual_enc_bitrate = stats.media_bitrate_bps;
1821}
1822
pbos@webrtc.org42684be2014-10-03 11:25:45 +00001823void WebRtcVideoChannel2::WebRtcVideoSendStream::OnCpuResolutionRequest(
1824 CoordinatedVideoAdapter::AdaptRequest adapt_request) {
1825 rtc::CritScope cs(&lock_);
1826 bool adapt_cpu;
1827 parameters_.options.cpu_overuse_detection.Get(&adapt_cpu);
1828 if (!adapt_cpu) {
1829 return;
1830 }
1831 if (capturer_ == NULL || capturer_->video_adapter() == NULL) {
1832 return;
1833 }
1834
1835 capturer_->video_adapter()->OnCpuResolutionRequest(adapt_request);
1836}
1837
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001838void WebRtcVideoChannel2::WebRtcVideoSendStream::RecreateWebRtcStream() {
1839 if (stream_ != NULL) {
1840 call_->DestroyVideoSendStream(stream_);
1841 }
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +00001842
pbos@webrtc.org6f48f1b2014-07-22 16:29:54 +00001843 VideoCodecSettings codec_settings;
1844 parameters_.codec_settings.Get(&codec_settings);
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +00001845 parameters_.encoder_config.encoder_specific_settings =
pbos@webrtc.orgf1c8b902015-01-14 17:29:27 +00001846 ConfigureVideoEncoderSettings(codec_settings.codec, parameters_.options);
pbos@webrtc.org6f48f1b2014-07-22 16:29:54 +00001847
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +00001848 stream_ = call_->CreateVideoSendStream(parameters_.config,
1849 parameters_.encoder_config);
pbos@webrtc.org6f48f1b2014-07-22 16:29:54 +00001850
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +00001851 parameters_.encoder_config.encoder_specific_settings = NULL;
pbos@webrtc.org6f48f1b2014-07-22 16:29:54 +00001852
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001853 if (sending_) {
1854 stream_->Start();
1855 }
1856}
1857
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001858WebRtcVideoChannel2::WebRtcVideoReceiveStream::WebRtcVideoReceiveStream(
1859 webrtc::Call* call,
pbos@webrtc.org776e6f22014-10-29 15:28:39 +00001860 WebRtcVideoDecoderFactory* external_decoder_factory,
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001861 const webrtc::VideoReceiveStream::Config& config,
1862 const std::vector<VideoCodecSettings>& recv_codecs)
1863 : call_(call),
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001864 stream_(NULL),
pbos@webrtc.orgb648b9d2014-08-26 11:08:06 +00001865 config_(config),
pbos@webrtc.org776e6f22014-10-29 15:28:39 +00001866 external_decoder_factory_(external_decoder_factory),
pbos@webrtc.orgb648b9d2014-08-26 11:08:06 +00001867 renderer_(NULL),
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001868 last_width_(-1),
magjed@webrtc.orgfc5ad952015-01-27 09:57:01 +00001869 last_height_(-1),
1870 first_frame_timestamp_(-1),
1871 estimated_remote_start_ntp_time_ms_(0) {
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001872 config_.renderer = this;
1873 // SetRecvCodecs will also reset (start) the VideoReceiveStream.
1874 SetRecvCodecs(recv_codecs);
1875}
1876
1877WebRtcVideoChannel2::WebRtcVideoReceiveStream::~WebRtcVideoReceiveStream() {
1878 call_->DestroyVideoReceiveStream(stream_);
pbos@webrtc.org96a93252014-11-03 14:46:44 +00001879 ClearDecoders(&allocated_decoders_);
1880}
1881
1882WebRtcVideoChannel2::WebRtcVideoReceiveStream::AllocatedDecoder
1883WebRtcVideoChannel2::WebRtcVideoReceiveStream::CreateOrReuseVideoDecoder(
1884 std::vector<AllocatedDecoder>* old_decoders,
1885 const VideoCodec& codec) {
1886 webrtc::VideoCodecType type = CodecTypeFromName(codec.name);
1887
1888 for (size_t i = 0; i < old_decoders->size(); ++i) {
1889 if ((*old_decoders)[i].type == type) {
1890 AllocatedDecoder decoder = (*old_decoders)[i];
1891 (*old_decoders)[i] = old_decoders->back();
1892 old_decoders->pop_back();
1893 return decoder;
1894 }
1895 }
1896
1897 if (external_decoder_factory_ != NULL) {
1898 webrtc::VideoDecoder* decoder =
1899 external_decoder_factory_->CreateVideoDecoder(type);
1900 if (decoder != NULL) {
1901 return AllocatedDecoder(decoder, type, true);
1902 }
1903 }
1904
1905 if (type == webrtc::kVideoCodecVP8) {
1906 return AllocatedDecoder(
1907 webrtc::VideoDecoder::Create(webrtc::VideoDecoder::kVp8), type, false);
1908 }
1909
1910 // This shouldn't happen, we should not be trying to create something we don't
1911 // support.
1912 assert(false);
1913 return AllocatedDecoder(NULL, webrtc::kVideoCodecUnknown, false);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001914}
1915
1916void WebRtcVideoChannel2::WebRtcVideoReceiveStream::SetRecvCodecs(
1917 const std::vector<VideoCodecSettings>& recv_codecs) {
pbos@webrtc.org96a93252014-11-03 14:46:44 +00001918 std::vector<AllocatedDecoder> old_decoders = allocated_decoders_;
1919 allocated_decoders_.clear();
1920 config_.decoders.clear();
1921 for (size_t i = 0; i < recv_codecs.size(); ++i) {
1922 AllocatedDecoder allocated_decoder =
1923 CreateOrReuseVideoDecoder(&old_decoders, recv_codecs[i].codec);
1924 allocated_decoders_.push_back(allocated_decoder);
1925
1926 webrtc::VideoReceiveStream::Decoder decoder;
1927 decoder.decoder = allocated_decoder.decoder;
1928 decoder.payload_type = recv_codecs[i].codec.id;
1929 decoder.payload_name = recv_codecs[i].codec.name;
1930 config_.decoders.push_back(decoder);
1931 }
1932
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001933 // TODO(pbos): Reconfigure RTX based on incoming recv_codecs.
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001934 config_.rtp.fec = recv_codecs.front().fec;
pbos@webrtc.org257e1302014-07-25 19:01:32 +00001935 config_.rtp.nack.rtp_history_ms =
1936 IsNackEnabled(recv_codecs.begin()->codec) ? kNackHistoryMs : 0;
1937 config_.rtp.remb = IsRembEnabled(recv_codecs.begin()->codec);
1938
pbos@webrtc.org96a93252014-11-03 14:46:44 +00001939 ClearDecoders(&old_decoders);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001940 RecreateWebRtcStream();
1941}
1942
1943void WebRtcVideoChannel2::WebRtcVideoReceiveStream::SetRtpExtensions(
1944 const std::vector<webrtc::RtpExtension>& extensions) {
1945 config_.rtp.extensions = extensions;
1946 RecreateWebRtcStream();
1947}
1948
1949void WebRtcVideoChannel2::WebRtcVideoReceiveStream::RecreateWebRtcStream() {
1950 if (stream_ != NULL) {
1951 call_->DestroyVideoReceiveStream(stream_);
1952 }
1953 stream_ = call_->CreateVideoReceiveStream(config_);
1954 stream_->Start();
1955}
1956
pbos@webrtc.org96a93252014-11-03 14:46:44 +00001957void WebRtcVideoChannel2::WebRtcVideoReceiveStream::ClearDecoders(
1958 std::vector<AllocatedDecoder>* allocated_decoders) {
1959 for (size_t i = 0; i < allocated_decoders->size(); ++i) {
1960 if ((*allocated_decoders)[i].external) {
pbos@webrtc.org776e6f22014-10-29 15:28:39 +00001961 external_decoder_factory_->DestroyVideoDecoder(
pbos@webrtc.org96a93252014-11-03 14:46:44 +00001962 (*allocated_decoders)[i].decoder);
pbos@webrtc.org776e6f22014-10-29 15:28:39 +00001963 } else {
pbos@webrtc.org96a93252014-11-03 14:46:44 +00001964 delete (*allocated_decoders)[i].decoder;
pbos@webrtc.org776e6f22014-10-29 15:28:39 +00001965 }
1966 }
pbos@webrtc.org96a93252014-11-03 14:46:44 +00001967 allocated_decoders->clear();
pbos@webrtc.org776e6f22014-10-29 15:28:39 +00001968}
1969
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001970void WebRtcVideoChannel2::WebRtcVideoReceiveStream::RenderFrame(
1971 const webrtc::I420VideoFrame& frame,
1972 int time_to_render_ms) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001973 rtc::CritScope crit(&renderer_lock_);
magjed@webrtc.orgfc5ad952015-01-27 09:57:01 +00001974
1975 if (first_frame_timestamp_ < 0)
1976 first_frame_timestamp_ = frame.timestamp();
1977 int64_t rtp_time_elapsed_since_first_frame =
1978 (timestamp_wraparound_handler_.Unwrap(frame.timestamp()) -
1979 first_frame_timestamp_);
1980 int64_t elapsed_time_ms = rtp_time_elapsed_since_first_frame /
1981 (cricket::kVideoCodecClockrate / 1000);
1982 if (frame.ntp_time_ms() > 0)
1983 estimated_remote_start_ntp_time_ms_ = frame.ntp_time_ms() - elapsed_time_ms;
1984
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001985 if (renderer_ == NULL) {
1986 LOG(LS_WARNING) << "VideoReceiveStream not connected to a VideoRenderer.";
1987 return;
1988 }
1989
1990 if (frame.width() != last_width_ || frame.height() != last_height_) {
1991 SetSize(frame.width(), frame.height());
1992 }
1993
1994 LOG(LS_VERBOSE) << "RenderFrame: (" << frame.width() << "x" << frame.height()
1995 << ")";
1996
magjed@webrtc.orgfc5ad952015-01-27 09:57:01 +00001997 const WebRtcVideoRenderFrame render_frame(&frame, elapsed_time_ms);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001998 renderer_->RenderFrame(&render_frame);
1999}
2000
2001void WebRtcVideoChannel2::WebRtcVideoReceiveStream::SetRenderer(
2002 cricket::VideoRenderer* renderer) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002003 rtc::CritScope crit(&renderer_lock_);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00002004 renderer_ = renderer;
2005 if (renderer_ != NULL && last_width_ != -1) {
2006 SetSize(last_width_, last_height_);
2007 }
2008}
2009
2010VideoRenderer* WebRtcVideoChannel2::WebRtcVideoReceiveStream::GetRenderer() {
2011 // TODO(pbos): Remove GetRenderer and all uses of it, it's thread-unsafe by
2012 // design.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002013 rtc::CritScope crit(&renderer_lock_);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00002014 return renderer_;
2015}
2016
2017void WebRtcVideoChannel2::WebRtcVideoReceiveStream::SetSize(int width,
2018 int height) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002019 rtc::CritScope crit(&renderer_lock_);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00002020 if (!renderer_->SetSize(width, height, 0)) {
2021 LOG(LS_ERROR) << "Could not set renderer size.";
2022 }
2023 last_width_ = width;
2024 last_height_ = height;
2025}
2026
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00002027VideoReceiverInfo
2028WebRtcVideoChannel2::WebRtcVideoReceiveStream::GetVideoReceiverInfo() {
2029 VideoReceiverInfo info;
2030 info.add_ssrc(config_.rtp.remote_ssrc);
2031 webrtc::VideoReceiveStream::Stats stats = stream_->GetStats();
asapersson@webrtc.orgcfd82df2015-01-22 09:39:59 +00002032 info.bytes_rcvd = stats.rtp_stats.transmitted.payload_bytes +
2033 stats.rtp_stats.transmitted.header_bytes +
2034 stats.rtp_stats.transmitted.padding_bytes;
2035 info.packets_rcvd = stats.rtp_stats.transmitted.packets;
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00002036
2037 info.framerate_rcvd = stats.network_frame_rate;
2038 info.framerate_decoded = stats.decode_frame_rate;
2039 info.framerate_output = stats.render_frame_rate;
2040
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002041 rtc::CritScope frame_cs(&renderer_lock_);
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00002042 info.frame_width = last_width_;
2043 info.frame_height = last_height_;
magjed@webrtc.orgfc5ad952015-01-27 09:57:01 +00002044 info.capture_start_ntp_time_ms = estimated_remote_start_ntp_time_ms_;
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00002045
2046 // TODO(pbos): Support or remove the following stats.
2047 info.packets_concealed = -1;
2048
2049 return info;
2050}
2051
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00002052WebRtcVideoChannel2::VideoCodecSettings::VideoCodecSettings()
2053 : rtx_payload_type(-1) {}
2054
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +00002055bool WebRtcVideoChannel2::VideoCodecSettings::operator==(
2056 const WebRtcVideoChannel2::VideoCodecSettings& other) const {
2057 return codec == other.codec &&
2058 fec.ulpfec_payload_type == other.fec.ulpfec_payload_type &&
2059 fec.red_payload_type == other.fec.red_payload_type &&
2060 rtx_payload_type == other.rtx_payload_type;
2061}
2062
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00002063std::vector<WebRtcVideoChannel2::VideoCodecSettings>
2064WebRtcVideoChannel2::MapCodecs(const std::vector<VideoCodec>& codecs) {
2065 assert(!codecs.empty());
2066
2067 std::vector<VideoCodecSettings> video_codecs;
2068 std::map<int, bool> payload_used;
pbos@webrtc.orge322a172014-06-13 11:47:28 +00002069 std::map<int, VideoCodec::CodecType> payload_codec_type;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00002070 std::map<int, int> rtx_mapping; // video payload type -> rtx payload type.
2071
2072 webrtc::FecConfig fec_settings;
2073
2074 for (size_t i = 0; i < codecs.size(); ++i) {
2075 const VideoCodec& in_codec = codecs[i];
2076 int payload_type = in_codec.id;
2077
2078 if (payload_used[payload_type]) {
2079 LOG(LS_ERROR) << "Payload type already registered: "
2080 << in_codec.ToString();
2081 return std::vector<VideoCodecSettings>();
2082 }
2083 payload_used[payload_type] = true;
pbos@webrtc.orge322a172014-06-13 11:47:28 +00002084 payload_codec_type[payload_type] = in_codec.GetCodecType();
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00002085
2086 switch (in_codec.GetCodecType()) {
2087 case VideoCodec::CODEC_RED: {
2088 // RED payload type, should not have duplicates.
2089 assert(fec_settings.red_payload_type == -1);
2090 fec_settings.red_payload_type = in_codec.id;
2091 continue;
2092 }
2093
2094 case VideoCodec::CODEC_ULPFEC: {
2095 // ULPFEC payload type, should not have duplicates.
2096 assert(fec_settings.ulpfec_payload_type == -1);
2097 fec_settings.ulpfec_payload_type = in_codec.id;
2098 continue;
2099 }
2100
2101 case VideoCodec::CODEC_RTX: {
2102 int associated_payload_type;
2103 if (!in_codec.GetParam(kCodecParamAssociatedPayloadType,
2104 &associated_payload_type)) {
2105 LOG(LS_ERROR) << "RTX codec without associated payload type: "
2106 << in_codec.ToString();
2107 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