blob: 567a719c9b21139c2a324a103e203290698ee4b1 [file] [log] [blame]
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001/*
2 * libjingle
3 * Copyright 2014 Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#ifdef HAVE_WEBRTC_VIDEO
29#include "talk/media/webrtc/webrtcvideoengine2.h"
30
pbos@webrtc.orgc37e72e2015-01-05 18:51:13 +000031#include <algorithm>
pbos@webrtc.org3c107582014-07-20 15:27:35 +000032#include <set>
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +000033#include <string>
34
35#include "libyuv/convert_from.h"
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +000036#include "talk/media/base/videocapturer.h"
37#include "talk/media/base/videorenderer.h"
buildbot@webrtc.orgdf9bbbe2014-06-19 19:54:33 +000038#include "talk/media/webrtc/constants.h"
buildbot@webrtc.orga8530772014-12-10 09:01:18 +000039#include "talk/media/webrtc/simulcast.h"
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +000040#include "talk/media/webrtc/webrtcvideocapturer.h"
andresp@webrtc.org82775b12014-11-07 09:37:54 +000041#include "talk/media/webrtc/webrtcvideoengine.h"
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +000042#include "talk/media/webrtc/webrtcvideoframe.h"
43#include "talk/media/webrtc/webrtcvoiceengine.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000044#include "webrtc/base/buffer.h"
45#include "webrtc/base/logging.h"
46#include "webrtc/base/stringutils.h"
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +000047#include "webrtc/call.h"
pbos@webrtc.org50fe3592015-01-29 12:33:07 +000048#include "webrtc/system_wrappers/interface/trace_event.h"
pbos@webrtc.org776e6f22014-10-29 15:28:39 +000049#include "webrtc/video_decoder.h"
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000050#include "webrtc/video_encoder.h"
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +000051
52#define UNIMPLEMENTED \
53 LOG(LS_ERROR) << "Call to unimplemented function " << __FUNCTION__; \
54 ASSERT(false)
55
56namespace cricket {
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +000057namespace {
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +000058static std::string CodecVectorToString(const std::vector<VideoCodec>& codecs) {
59 std::stringstream out;
60 out << '{';
61 for (size_t i = 0; i < codecs.size(); ++i) {
62 out << codecs[i].ToString();
63 if (i != codecs.size() - 1) {
64 out << ", ";
65 }
66 }
67 out << '}';
68 return out.str();
69}
70
71static bool ValidateCodecFormats(const std::vector<VideoCodec>& codecs) {
72 bool has_video = false;
73 for (size_t i = 0; i < codecs.size(); ++i) {
74 if (!codecs[i].ValidateCodecFormat()) {
75 return false;
76 }
77 if (codecs[i].GetCodecType() == VideoCodec::CODEC_VIDEO) {
78 has_video = true;
79 }
80 }
81 if (!has_video) {
82 LOG(LS_ERROR) << "Setting codecs without a video codec is invalid: "
83 << CodecVectorToString(codecs);
84 return false;
85 }
86 return true;
87}
88
89static std::string RtpExtensionsToString(
90 const std::vector<RtpHeaderExtension>& extensions) {
91 std::stringstream out;
92 out << '{';
93 for (size_t i = 0; i < extensions.size(); ++i) {
94 out << "{" << extensions[i].uri << ": " << extensions[i].id << "}";
95 if (i != extensions.size() - 1) {
96 out << ", ";
97 }
98 }
99 out << '}';
100 return out.str();
101}
102
andresp@webrtc.org82775b12014-11-07 09:37:54 +0000103// Merges two fec configs and logs an error if a conflict arises
104// such that merging in diferent order would trigger a diferent output.
105static void MergeFecConfig(const webrtc::FecConfig& other,
106 webrtc::FecConfig* output) {
107 if (other.ulpfec_payload_type != -1) {
108 if (output->ulpfec_payload_type != -1 &&
109 output->ulpfec_payload_type != other.ulpfec_payload_type) {
110 LOG(LS_WARNING) << "Conflict merging ulpfec_payload_type configs: "
111 << output->ulpfec_payload_type << " and "
112 << other.ulpfec_payload_type;
113 }
114 output->ulpfec_payload_type = other.ulpfec_payload_type;
115 }
116 if (other.red_payload_type != -1) {
117 if (output->red_payload_type != -1 &&
118 output->red_payload_type != other.red_payload_type) {
119 LOG(LS_WARNING) << "Conflict merging red_payload_type configs: "
120 << output->red_payload_type << " and "
121 << other.red_payload_type;
122 }
123 output->red_payload_type = other.red_payload_type;
124 }
125}
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000126} // namespace
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000127
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000128// This constant is really an on/off, lower-level configurable NACK history
129// duration hasn't been implemented.
130static const int kNackHistoryMs = 1000;
131
buildbot@webrtc.org933d88a2014-09-18 20:23:05 +0000132static const int kDefaultQpMax = 56;
133
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000134static const int kDefaultRtcpReceiverReportSsrc = 1;
135
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000136// External video encoders are given payloads 120-127. This also means that we
137// only support up to 8 external payload types.
138static const int kExternalVideoPayloadTypeBase = 120;
139#ifndef NDEBUG
140static const size_t kMaxExternalVideoCodecs = 8;
141#endif
142
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000143const char kH264CodecName[] = "H264";
144
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000145static bool FindFirstMatchingCodec(const std::vector<VideoCodec>& codecs,
146 const VideoCodec& requested_codec,
147 VideoCodec* matching_codec) {
148 for (size_t i = 0; i < codecs.size(); ++i) {
149 if (requested_codec.Matches(codecs[i])) {
150 *matching_codec = codecs[i];
151 return true;
152 }
153 }
154 return false;
155}
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000156
pbos@webrtc.org3c107582014-07-20 15:27:35 +0000157static bool ValidateRtpHeaderExtensionIds(
158 const std::vector<RtpHeaderExtension>& extensions) {
159 std::set<int> extensions_used;
160 for (size_t i = 0; i < extensions.size(); ++i) {
161 if (extensions[i].id < 0 || extensions[i].id >= 15 ||
162 !extensions_used.insert(extensions[i].id).second) {
163 LOG(LS_ERROR) << "RTP extensions are with incorrect or duplicate ids.";
164 return false;
165 }
166 }
167 return true;
168}
169
pbos@webrtc.orgc37e72e2015-01-05 18:51:13 +0000170static bool CompareRtpHeaderExtensionIds(
171 const webrtc::RtpExtension& extension1,
172 const webrtc::RtpExtension& extension2) {
173 // Sorting on ID is sufficient, more than one extension per ID is unsupported.
174 return extension1.id > extension2.id;
175}
176
pbos@webrtc.org3c107582014-07-20 15:27:35 +0000177static std::vector<webrtc::RtpExtension> FilterRtpExtensions(
178 const std::vector<RtpHeaderExtension>& extensions) {
179 std::vector<webrtc::RtpExtension> webrtc_extensions;
180 for (size_t i = 0; i < extensions.size(); ++i) {
181 // Unsupported extensions will be ignored.
182 if (webrtc::RtpExtension::IsSupported(extensions[i].uri)) {
183 webrtc_extensions.push_back(webrtc::RtpExtension(
184 extensions[i].uri, extensions[i].id));
185 } else {
186 LOG(LS_WARNING) << "Unsupported RTP extension: " << extensions[i].uri;
187 }
188 }
pbos@webrtc.orgc37e72e2015-01-05 18:51:13 +0000189
190 // Sort filtered headers to make sure that they can later be compared
191 // regardless of in which order they were entered.
192 std::sort(webrtc_extensions.begin(), webrtc_extensions.end(),
193 CompareRtpHeaderExtensionIds);
pbos@webrtc.org3c107582014-07-20 15:27:35 +0000194 return webrtc_extensions;
195}
196
pbos@webrtc.orgc37e72e2015-01-05 18:51:13 +0000197static bool RtpExtensionsHaveChanged(
198 const std::vector<webrtc::RtpExtension>& before,
199 const std::vector<webrtc::RtpExtension>& after) {
200 if (before.size() != after.size())
201 return true;
202 for (size_t i = 0; i < before.size(); ++i) {
203 if (before[i].id != after[i].id)
204 return true;
205 if (before[i].name != after[i].name)
206 return true;
207 }
208 return false;
209}
210
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000211std::vector<webrtc::VideoStream>
pbos@webrtc.orgf1c8b902015-01-14 17:29:27 +0000212WebRtcVideoChannel2::WebRtcVideoSendStream::CreateSimulcastVideoStreams(
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000213 const VideoCodec& codec,
214 const VideoOptions& options,
215 size_t num_streams) {
216 // Use default factory for non-simulcast.
217 int max_qp = kDefaultQpMax;
218 codec.GetParam(kCodecParamMaxQuantization, &max_qp);
219
220 int min_bitrate_kbps;
221 if (!codec.GetParam(kCodecParamMinBitrate, &min_bitrate_kbps) ||
222 min_bitrate_kbps < kMinVideoBitrate) {
223 min_bitrate_kbps = kMinVideoBitrate;
224 }
225
226 int max_bitrate_kbps;
227 if (!codec.GetParam(kCodecParamMaxBitrate, &max_bitrate_kbps)) {
228 max_bitrate_kbps = 0;
229 }
230
231 return GetSimulcastConfig(
232 num_streams,
233 GetSimulcastBitrateMode(options),
234 codec.width,
235 codec.height,
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000236 max_bitrate_kbps * 1000,
237 max_qp,
238 codec.framerate != 0 ? codec.framerate : kDefaultVideoMaxFramerate);
239}
240
pbos@webrtc.orgf1c8b902015-01-14 17:29:27 +0000241std::vector<webrtc::VideoStream>
242WebRtcVideoChannel2::WebRtcVideoSendStream::CreateVideoStreams(
buildbot@webrtc.orgd41eaeb2014-06-12 07:13:26 +0000243 const VideoCodec& codec,
244 const VideoOptions& options,
245 size_t num_streams) {
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000246 if (num_streams != 1)
247 return CreateSimulcastVideoStreams(codec, options, num_streams);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000248
buildbot@webrtc.orgd41eaeb2014-06-12 07:13:26 +0000249 webrtc::VideoStream stream;
250 stream.width = codec.width;
251 stream.height = codec.height;
252 stream.max_framerate =
andresp@webrtc.org82775b12014-11-07 09:37:54 +0000253 codec.framerate != 0 ? codec.framerate : kDefaultVideoMaxFramerate;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000254
pbos@webrtc.org00873182014-11-25 14:03:34 +0000255 stream.min_bitrate_bps = kMinVideoBitrate * 1000;
256 stream.target_bitrate_bps = stream.max_bitrate_bps = kMaxVideoBitrate * 1000;
buildbot@webrtc.orgd41eaeb2014-06-12 07:13:26 +0000257
buildbot@webrtc.org933d88a2014-09-18 20:23:05 +0000258 int max_qp = kDefaultQpMax;
buildbot@webrtc.orgd41eaeb2014-06-12 07:13:26 +0000259 codec.GetParam(kCodecParamMaxQuantization, &max_qp);
260 stream.max_qp = max_qp;
261 std::vector<webrtc::VideoStream> streams;
262 streams.push_back(stream);
263 return streams;
264}
265
pbos@webrtc.orgf1c8b902015-01-14 17:29:27 +0000266void* WebRtcVideoChannel2::WebRtcVideoSendStream::ConfigureVideoEncoderSettings(
pbos@webrtc.org6f48f1b2014-07-22 16:29:54 +0000267 const VideoCodec& codec,
268 const VideoOptions& options) {
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000269 if (CodecNameMatches(codec.name, kVp8CodecName)) {
pbos@webrtc.orgf1c8b902015-01-14 17:29:27 +0000270 encoder_settings_.vp8 = webrtc::VideoEncoder::GetDefaultVp8Settings();
271 options.video_noise_reduction.Get(&encoder_settings_.vp8.denoisingOn);
272 return &encoder_settings_.vp8;
pbos@webrtc.org6f48f1b2014-07-22 16:29:54 +0000273 }
andresp@webrtc.org188d3b22014-11-07 13:21:04 +0000274 if (CodecNameMatches(codec.name, kVp9CodecName)) {
pbos@webrtc.orgf1c8b902015-01-14 17:29:27 +0000275 encoder_settings_.vp9 = webrtc::VideoEncoder::GetDefaultVp9Settings();
276 options.video_noise_reduction.Get(&encoder_settings_.vp9.denoisingOn);
277 return &encoder_settings_.vp9;
andresp@webrtc.org188d3b22014-11-07 13:21:04 +0000278 }
pbos@webrtc.org6f48f1b2014-07-22 16:29:54 +0000279 return NULL;
280}
281
pbos@webrtc.orgafb554f42014-08-12 23:17:13 +0000282DefaultUnsignalledSsrcHandler::DefaultUnsignalledSsrcHandler()
283 : default_recv_ssrc_(0), default_renderer_(NULL) {}
284
285UnsignalledSsrcHandler::Action DefaultUnsignalledSsrcHandler::OnUnsignalledSsrc(
286 VideoMediaChannel* channel,
287 uint32_t ssrc) {
288 if (default_recv_ssrc_ != 0) { // Already one default stream.
289 LOG(LS_WARNING) << "Unknown SSRC, but default receive stream already set.";
290 return kDropPacket;
291 }
292
293 StreamParams sp;
294 sp.ssrcs.push_back(ssrc);
295 LOG(LS_INFO) << "Creating default receive stream for SSRC=" << ssrc << ".";
296 if (!channel->AddRecvStream(sp)) {
297 LOG(LS_WARNING) << "Could not create default receive stream.";
298 }
299
300 channel->SetRenderer(ssrc, default_renderer_);
301 default_recv_ssrc_ = ssrc;
302 return kDeliverPacket;
303}
304
pbos@webrtc.org42684be2014-10-03 11:25:45 +0000305WebRtcCallFactory::~WebRtcCallFactory() {
306}
307webrtc::Call* WebRtcCallFactory::CreateCall(
308 const webrtc::Call::Config& config) {
309 return webrtc::Call::Create(config);
310}
311
pbos@webrtc.orgafb554f42014-08-12 23:17:13 +0000312VideoRenderer* DefaultUnsignalledSsrcHandler::GetDefaultRenderer() const {
313 return default_renderer_;
314}
315
316void DefaultUnsignalledSsrcHandler::SetDefaultRenderer(
317 VideoMediaChannel* channel,
318 VideoRenderer* renderer) {
319 default_renderer_ = renderer;
320 if (default_recv_ssrc_ != 0) {
321 channel->SetRenderer(default_recv_ssrc_, default_renderer_);
322 }
323}
324
pbos@webrtc.org97fdeb82014-08-22 10:36:23 +0000325WebRtcVideoEngine2::WebRtcVideoEngine2()
pbos@webrtc.orgb648b9d2014-08-26 11:08:06 +0000326 : worker_thread_(NULL),
327 voice_engine_(NULL),
andresp@webrtc.org82775b12014-11-07 09:37:54 +0000328 default_codec_format_(kDefaultVideoMaxWidth,
329 kDefaultVideoMaxHeight,
330 FPS_TO_INTERVAL(kDefaultVideoMaxFramerate),
buildbot@webrtc.org992febb2014-09-05 16:39:08 +0000331 FOURCC_ANY),
pbos@webrtc.orgb648b9d2014-08-26 11:08:06 +0000332 initialized_(false),
pbos@webrtc.org42684be2014-10-03 11:25:45 +0000333 call_factory_(&default_call_factory_),
pbos@webrtc.org0a2087a2014-09-23 09:40:22 +0000334 external_decoder_factory_(NULL),
335 external_encoder_factory_(NULL) {
pbos@webrtc.orgb648b9d2014-08-26 11:08:06 +0000336 LOG(LS_INFO) << "WebRtcVideoEngine2::WebRtcVideoEngine2()";
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000337 video_codecs_ = GetSupportedCodecs();
pbos@webrtc.org587ef602014-06-16 17:32:02 +0000338 rtp_header_extensions_.push_back(
339 RtpHeaderExtension(kRtpTimestampOffsetHeaderExtension,
340 kRtpTimestampOffsetHeaderExtensionDefaultId));
341 rtp_header_extensions_.push_back(
342 RtpHeaderExtension(kRtpAbsoluteSenderTimeHeaderExtension,
343 kRtpAbsoluteSenderTimeHeaderExtensionDefaultId));
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000344}
345
346WebRtcVideoEngine2::~WebRtcVideoEngine2() {
347 LOG(LS_INFO) << "WebRtcVideoEngine2::~WebRtcVideoEngine2";
348
349 if (initialized_) {
350 Terminate();
351 }
352}
353
pbos@webrtc.org42684be2014-10-03 11:25:45 +0000354void WebRtcVideoEngine2::SetCallFactory(WebRtcCallFactory* call_factory) {
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000355 assert(!initialized_);
pbos@webrtc.org42684be2014-10-03 11:25:45 +0000356 call_factory_ = call_factory;
357}
358
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000359bool WebRtcVideoEngine2::Init(rtc::Thread* worker_thread) {
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000360 LOG(LS_INFO) << "WebRtcVideoEngine2::Init";
361 worker_thread_ = worker_thread;
362 ASSERT(worker_thread_ != NULL);
363
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000364 initialized_ = true;
365 return true;
366}
367
368void WebRtcVideoEngine2::Terminate() {
369 LOG(LS_INFO) << "WebRtcVideoEngine2::Terminate";
370
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000371 initialized_ = false;
372}
373
374int WebRtcVideoEngine2::GetCapabilities() { return VIDEO_RECV | VIDEO_SEND; }
375
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000376bool WebRtcVideoEngine2::SetDefaultEncoderConfig(
377 const VideoEncoderConfig& config) {
pbos@webrtc.org8fdeee62014-07-20 14:40:23 +0000378 const VideoCodec& codec = config.max_codec;
pbos@webrtc.org957e8022014-11-10 12:36:11 +0000379 bool supports_codec = false;
380 for (size_t i = 0; i < video_codecs_.size(); ++i) {
381 if (CodecNameMatches(video_codecs_[i].name, codec.name)) {
382 video_codecs_[i] = codec;
383 supports_codec = true;
384 break;
385 }
386 }
387
388 if (!supports_codec) {
389 LOG(LS_ERROR) << "SetDefaultEncoderConfig, codec not supported: "
pbos@webrtc.org8fdeee62014-07-20 14:40:23 +0000390 << codec.ToString();
391 return false;
392 }
393
buildbot@webrtc.org992febb2014-09-05 16:39:08 +0000394 default_codec_format_ =
395 VideoFormat(codec.width,
396 codec.height,
397 VideoFormat::FpsToInterval(codec.framerate),
398 FOURCC_ANY);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000399 return true;
400}
401
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000402WebRtcVideoChannel2* WebRtcVideoEngine2::CreateChannel(
buildbot@webrtc.org1ecbe452014-10-14 20:29:28 +0000403 const VideoOptions& options,
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000404 VoiceMediaChannel* voice_channel) {
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000405 assert(initialized_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000406 LOG(LS_INFO) << "CreateChannel: "
407 << (voice_channel != NULL ? "With" : "Without")
pbos@webrtc.orgfa553ef2014-10-20 11:07:07 +0000408 << " voice channel. Options: " << options.ToString();
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000409 WebRtcVideoChannel2* channel =
410 new WebRtcVideoChannel2(call_factory_,
pbos@webrtc.org3bf3d232014-10-31 12:59:34 +0000411 voice_engine_,
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000412 voice_channel,
pbos@webrtc.orgfa553ef2014-10-20 11:07:07 +0000413 options,
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000414 external_encoder_factory_,
pbos@webrtc.orgf1c8b902015-01-14 17:29:27 +0000415 external_decoder_factory_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000416 if (!channel->Init()) {
417 delete channel;
418 return NULL;
419 }
pbos@webrtc.orge322a172014-06-13 11:47:28 +0000420 channel->SetRecvCodecs(video_codecs_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000421 return channel;
422}
423
424const std::vector<VideoCodec>& WebRtcVideoEngine2::codecs() const {
425 return video_codecs_;
426}
427
428const std::vector<RtpHeaderExtension>&
429WebRtcVideoEngine2::rtp_header_extensions() const {
430 return rtp_header_extensions_;
431}
432
433void WebRtcVideoEngine2::SetLogging(int min_sev, const char* filter) {
434 // TODO(pbos): Set up logging.
435 LOG(LS_VERBOSE) << "SetLogging: " << min_sev << '"' << filter << '"';
436 // if min_sev == -1, we keep the current log level.
437 if (min_sev < 0) {
438 assert(min_sev == -1);
439 return;
440 }
441}
442
pbos@webrtc.org0a2087a2014-09-23 09:40:22 +0000443void WebRtcVideoEngine2::SetExternalDecoderFactory(
444 WebRtcVideoDecoderFactory* decoder_factory) {
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000445 assert(!initialized_);
pbos@webrtc.org0a2087a2014-09-23 09:40:22 +0000446 external_decoder_factory_ = decoder_factory;
447}
448
449void WebRtcVideoEngine2::SetExternalEncoderFactory(
450 WebRtcVideoEncoderFactory* encoder_factory) {
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000451 assert(!initialized_);
pbos@webrtc.orgf18fba22015-01-14 16:26:23 +0000452 if (external_encoder_factory_ == encoder_factory)
453 return;
454
455 // No matter what happens we shouldn't hold on to a stale
456 // WebRtcSimulcastEncoderFactory.
457 simulcast_encoder_factory_.reset();
458
459 if (encoder_factory &&
460 WebRtcSimulcastEncoderFactory::UseSimulcastEncoderFactory(
461 encoder_factory->codecs())) {
462 simulcast_encoder_factory_.reset(
463 new WebRtcSimulcastEncoderFactory(encoder_factory));
464 encoder_factory = simulcast_encoder_factory_.get();
465 }
pbos@webrtc.org0a2087a2014-09-23 09:40:22 +0000466 external_encoder_factory_ = encoder_factory;
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000467
468 video_codecs_ = GetSupportedCodecs();
pbos@webrtc.org0a2087a2014-09-23 09:40:22 +0000469}
470
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000471bool WebRtcVideoEngine2::EnableTimedRender() {
472 // TODO(pbos): Figure out whether this can be removed.
473 return true;
474}
475
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000476// Checks to see whether we comprehend and could receive a particular codec
477bool WebRtcVideoEngine2::FindCodec(const VideoCodec& in) {
478 // TODO(pbos): Probe encoder factory to figure out that the codec is supported
479 // if supported by the encoder factory. Add a corresponding test that fails
480 // with this code (that doesn't ask the factory).
pbos@webrtc.org8fdeee62014-07-20 14:40:23 +0000481 for (size_t j = 0; j < video_codecs_.size(); ++j) {
482 VideoCodec codec(video_codecs_[j].id, video_codecs_[j].name, 0, 0, 0, 0);
483 if (codec.Matches(in)) {
484 return true;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000485 }
486 }
487 return false;
488}
489
490// Tells whether the |requested| codec can be transmitted or not. If it can be
491// transmitted |out| is set with the best settings supported. Aspect ratio will
492// be set as close to |current|'s as possible. If not set |requested|'s
493// dimensions will be used for aspect ratio matching.
494bool WebRtcVideoEngine2::CanSendCodec(const VideoCodec& requested,
495 const VideoCodec& current,
496 VideoCodec* out) {
497 assert(out != NULL);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000498
499 if (requested.width != requested.height &&
500 (requested.height == 0 || requested.width == 0)) {
501 // 0xn and nx0 are invalid resolutions.
502 return false;
503 }
504
505 VideoCodec matching_codec;
506 if (!FindFirstMatchingCodec(video_codecs_, requested, &matching_codec)) {
507 // Codec not supported.
508 return false;
509 }
510
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000511 out->id = requested.id;
512 out->name = requested.name;
513 out->preference = requested.preference;
514 out->params = requested.params;
andresp@webrtc.orgff689be2015-02-12 11:54:26 +0000515 out->framerate = std::min(requested.framerate, matching_codec.framerate);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000516 out->params = requested.params;
517 out->feedback_params = requested.feedback_params;
pbos@webrtc.org8fdeee62014-07-20 14:40:23 +0000518 out->width = requested.width;
519 out->height = requested.height;
520 if (requested.width == 0 && requested.height == 0) {
521 return true;
522 }
523
524 while (out->width > matching_codec.width) {
525 out->width /= 2;
526 out->height /= 2;
527 }
528
529 return out->width > 0 && out->height > 0;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000530}
531
532bool WebRtcVideoEngine2::SetVoiceEngine(WebRtcVoiceEngine* voice_engine) {
533 if (initialized_) {
534 LOG(LS_WARNING) << "SetVoiceEngine can not be called after Init";
535 return false;
536 }
537 voice_engine_ = voice_engine;
538 return true;
539}
540
541// Ignore spammy trace messages, mostly from the stats API when we haven't
542// gotten RTCP info yet from the remote side.
543bool WebRtcVideoEngine2::ShouldIgnoreTrace(const std::string& trace) {
544 static const char* const kTracesToIgnore[] = {NULL};
545 for (const char* const* p = kTracesToIgnore; *p; ++p) {
546 if (trace.find(*p) == 0) {
547 return true;
548 }
549 }
550 return false;
551}
552
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000553std::vector<VideoCodec> WebRtcVideoEngine2::GetSupportedCodecs() const {
andresp@webrtc.org82775b12014-11-07 09:37:54 +0000554 std::vector<VideoCodec> supported_codecs = DefaultVideoCodecList();
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000555
556 if (external_encoder_factory_ == NULL) {
557 return supported_codecs;
558 }
559
560 assert(external_encoder_factory_->codecs().size() <= kMaxExternalVideoCodecs);
561 const std::vector<WebRtcVideoEncoderFactory::VideoCodec>& codecs =
562 external_encoder_factory_->codecs();
563 for (size_t i = 0; i < codecs.size(); ++i) {
564 // Don't add internally-supported codecs twice.
565 if (CodecIsInternallySupported(codecs[i].name)) {
566 continue;
567 }
568
569 VideoCodec codec(kExternalVideoPayloadTypeBase + static_cast<int>(i),
570 codecs[i].name,
571 codecs[i].max_width,
572 codecs[i].max_height,
573 codecs[i].max_fps,
574 0);
575
576 AddDefaultFeedbackParams(&codec);
577 supported_codecs.push_back(codec);
578 }
579 return supported_codecs;
580}
581
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000582WebRtcVideoChannel2::WebRtcVideoChannel2(
pbos@webrtc.org42684be2014-10-03 11:25:45 +0000583 WebRtcCallFactory* call_factory,
pbos@webrtc.org3bf3d232014-10-31 12:59:34 +0000584 WebRtcVoiceEngine* voice_engine,
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000585 VoiceMediaChannel* voice_channel,
pbos@webrtc.orgfa553ef2014-10-20 11:07:07 +0000586 const VideoOptions& options,
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000587 WebRtcVideoEncoderFactory* external_encoder_factory,
pbos@webrtc.orgf1c8b902015-01-14 17:29:27 +0000588 WebRtcVideoDecoderFactory* external_decoder_factory)
pbos@webrtc.orgb648b9d2014-08-26 11:08:06 +0000589 : unsignalled_ssrc_handler_(&default_unsignalled_ssrc_handler_),
pbos@webrtc.org3bf3d232014-10-31 12:59:34 +0000590 voice_channel_(voice_channel),
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000591 external_encoder_factory_(external_encoder_factory),
pbos@webrtc.orgf1c8b902015-01-14 17:29:27 +0000592 external_decoder_factory_(external_decoder_factory) {
pbos@webrtc.orgfa553ef2014-10-20 11:07:07 +0000593 SetDefaultOptions();
594 options_.SetAll(options);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000595 webrtc::Call::Config config(this);
pbos@webrtc.org42684be2014-10-03 11:25:45 +0000596 config.overuse_callback = this;
pbos@webrtc.org3bf3d232014-10-31 12:59:34 +0000597 if (voice_engine != NULL) {
598 config.voice_engine = voice_engine->voe()->engine();
599 }
pbos@webrtc.orgfa553ef2014-10-20 11:07:07 +0000600
pbos@webrtc.org42684be2014-10-03 11:25:45 +0000601 call_.reset(call_factory->CreateCall(config));
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000602
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000603 rtcp_receiver_report_ssrc_ = kDefaultRtcpReceiverReportSsrc;
604 sending_ = false;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000605 default_send_ssrc_ = 0;
pbos@webrtc.org6f48f1b2014-07-22 16:29:54 +0000606}
607
608void WebRtcVideoChannel2::SetDefaultOptions() {
pbos@webrtc.org42684be2014-10-03 11:25:45 +0000609 options_.cpu_overuse_detection.Set(false);
pbos@webrtc.orgd8198032014-11-10 14:41:43 +0000610 options_.dscp.Set(false);
pbos@webrtc.org5ff71ab2014-07-23 07:28:56 +0000611 options_.suspend_below_min_bitrate.Set(false);
pbos@webrtc.org42684be2014-10-03 11:25:45 +0000612 options_.video_noise_reduction.Set(true);
pbos@webrtc.orgefc82c22014-10-27 13:58:00 +0000613 options_.screencast_min_bitrate.Set(0);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000614}
615
616WebRtcVideoChannel2::~WebRtcVideoChannel2() {
617 for (std::map<uint32, WebRtcVideoSendStream*>::iterator it =
618 send_streams_.begin();
619 it != send_streams_.end();
620 ++it) {
621 delete it->second;
622 }
623
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000624 for (std::map<uint32, WebRtcVideoReceiveStream*>::iterator it =
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000625 receive_streams_.begin();
626 it != receive_streams_.end();
627 ++it) {
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000628 delete it->second;
629 }
630}
631
632bool WebRtcVideoChannel2::Init() { return true; }
633
pbos@webrtc.org96a93252014-11-03 14:46:44 +0000634bool WebRtcVideoChannel2::CodecIsExternallySupported(
635 const std::string& name) const {
636 if (external_encoder_factory_ == NULL) {
637 return false;
638 }
639
640 const std::vector<WebRtcVideoEncoderFactory::VideoCodec> external_codecs =
641 external_encoder_factory_->codecs();
642 for (size_t c = 0; c < external_codecs.size(); ++c) {
643 if (CodecNameMatches(name, external_codecs[c].name)) {
644 return true;
645 }
646 }
647 return false;
648}
649
650std::vector<WebRtcVideoChannel2::VideoCodecSettings>
651WebRtcVideoChannel2::FilterSupportedCodecs(
652 const std::vector<WebRtcVideoChannel2::VideoCodecSettings>& mapped_codecs)
653 const {
654 std::vector<VideoCodecSettings> supported_codecs;
655 for (size_t i = 0; i < mapped_codecs.size(); ++i) {
656 const VideoCodecSettings& codec = mapped_codecs[i];
657 if (CodecIsInternallySupported(codec.codec.name) ||
658 CodecIsExternallySupported(codec.codec.name)) {
659 supported_codecs.push_back(codec);
660 }
661 }
662 return supported_codecs;
663}
664
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000665bool WebRtcVideoChannel2::SetRecvCodecs(const std::vector<VideoCodec>& codecs) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000666 TRACE_EVENT0("webrtc", "WebRtcVideoChannel2::SetRecvCodecs");
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000667 LOG(LS_INFO) << "SetRecvCodecs: " << CodecVectorToString(codecs);
668 if (!ValidateCodecFormats(codecs)) {
669 return false;
670 }
671
672 const std::vector<VideoCodecSettings> mapped_codecs = MapCodecs(codecs);
673 if (mapped_codecs.empty()) {
pbos@webrtc.org96a93252014-11-03 14:46:44 +0000674 LOG(LS_ERROR) << "SetRecvCodecs called without any video codecs.";
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000675 return false;
676 }
677
pbos@webrtc.org96a93252014-11-03 14:46:44 +0000678 const std::vector<VideoCodecSettings> supported_codecs =
679 FilterSupportedCodecs(mapped_codecs);
680
681 if (mapped_codecs.size() != supported_codecs.size()) {
682 LOG(LS_ERROR) << "SetRecvCodecs called with unsupported video codecs.";
683 return false;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000684 }
685
pbos@webrtc.org96a93252014-11-03 14:46:44 +0000686 recv_codecs_ = supported_codecs;
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000687
pbos@webrtc.org575d1262014-10-08 14:48:08 +0000688 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000689 for (std::map<uint32, WebRtcVideoReceiveStream*>::iterator it =
690 receive_streams_.begin();
691 it != receive_streams_.end();
692 ++it) {
693 it->second->SetRecvCodecs(recv_codecs_);
694 }
695
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000696 return true;
697}
698
699bool WebRtcVideoChannel2::SetSendCodecs(const std::vector<VideoCodec>& codecs) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000700 TRACE_EVENT0("webrtc", "WebRtcVideoChannel2::SetSendCodecs");
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000701 LOG(LS_INFO) << "SetSendCodecs: " << CodecVectorToString(codecs);
702 if (!ValidateCodecFormats(codecs)) {
703 return false;
704 }
705
706 const std::vector<VideoCodecSettings> supported_codecs =
707 FilterSupportedCodecs(MapCodecs(codecs));
708
709 if (supported_codecs.empty()) {
710 LOG(LS_ERROR) << "No video codecs supported by encoder factory.";
711 return false;
712 }
713
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000714 LOG(LS_INFO) << "Using codec: " << supported_codecs.front().codec.ToString();
715
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +0000716 VideoCodecSettings old_codec;
717 if (send_codec_.Get(&old_codec) && supported_codecs.front() == old_codec) {
718 // Using same codec, avoid reconfiguring.
719 return true;
720 }
721
722 send_codec_.Set(supported_codecs.front());
723
pbos@webrtc.org575d1262014-10-08 14:48:08 +0000724 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000725 for (std::map<uint32, WebRtcVideoSendStream*>::iterator it =
726 send_streams_.begin();
727 it != send_streams_.end();
728 ++it) {
729 assert(it->second != NULL);
730 it->second->SetCodec(supported_codecs.front());
731 }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000732
pbos@webrtc.org00873182014-11-25 14:03:34 +0000733 VideoCodec codec = supported_codecs.front().codec;
734 int bitrate_kbps;
735 if (codec.GetParam(kCodecParamMinBitrate, &bitrate_kbps) &&
736 bitrate_kbps > 0) {
737 bitrate_config_.min_bitrate_bps = bitrate_kbps * 1000;
738 } else {
739 bitrate_config_.min_bitrate_bps = 0;
740 }
741 if (codec.GetParam(kCodecParamStartBitrate, &bitrate_kbps) &&
742 bitrate_kbps > 0) {
743 bitrate_config_.start_bitrate_bps = bitrate_kbps * 1000;
744 } else {
745 // Do not reconfigure start bitrate unless it's specified and positive.
746 bitrate_config_.start_bitrate_bps = -1;
747 }
748 if (codec.GetParam(kCodecParamMaxBitrate, &bitrate_kbps) &&
749 bitrate_kbps > 0) {
750 bitrate_config_.max_bitrate_bps = bitrate_kbps * 1000;
751 } else {
752 bitrate_config_.max_bitrate_bps = -1;
753 }
754 call_->SetBitrateConfig(bitrate_config_);
755
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000756 return true;
757}
758
759bool WebRtcVideoChannel2::GetSendCodec(VideoCodec* codec) {
760 VideoCodecSettings codec_settings;
761 if (!send_codec_.Get(&codec_settings)) {
762 LOG(LS_VERBOSE) << "GetSendCodec: No send codec set.";
763 return false;
764 }
765 *codec = codec_settings.codec;
766 return true;
767}
768
769bool WebRtcVideoChannel2::SetSendStreamFormat(uint32 ssrc,
770 const VideoFormat& format) {
771 LOG(LS_VERBOSE) << "SetSendStreamFormat:" << ssrc << " -> "
772 << format.ToString();
pbos@webrtc.org575d1262014-10-08 14:48:08 +0000773 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000774 if (send_streams_.find(ssrc) == send_streams_.end()) {
775 return false;
776 }
777 return send_streams_[ssrc]->SetVideoFormat(format);
778}
779
780bool WebRtcVideoChannel2::SetRender(bool render) {
781 // TODO(pbos): Implement. Or refactor away as it shouldn't be needed.
782 LOG(LS_VERBOSE) << "SetRender: " << (render ? "true" : "false");
783 return true;
784}
785
786bool WebRtcVideoChannel2::SetSend(bool send) {
787 LOG(LS_VERBOSE) << "SetSend: " << (send ? "true" : "false");
788 if (send && !send_codec_.IsSet()) {
789 LOG(LS_ERROR) << "SetSend(true) called before setting codec.";
790 return false;
791 }
792 if (send) {
793 StartAllSendStreams();
794 } else {
795 StopAllSendStreams();
796 }
797 sending_ = send;
798 return true;
799}
800
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000801bool WebRtcVideoChannel2::AddSendStream(const StreamParams& sp) {
802 LOG(LS_INFO) << "AddSendStream: " << sp.ToString();
803 if (sp.ssrcs.empty()) {
804 LOG(LS_ERROR) << "No SSRCs in stream parameters.";
805 return false;
806 }
807
808 uint32 ssrc = sp.first_ssrc();
809 assert(ssrc != 0);
810 // TODO(pbos): Make sure none of sp.ssrcs are used, not just the identifying
811 // ssrc.
pbos@webrtc.org575d1262014-10-08 14:48:08 +0000812 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000813 if (send_streams_.find(ssrc) != send_streams_.end()) {
814 LOG(LS_ERROR) << "Send stream with ssrc '" << ssrc << "' already exists.";
815 return false;
816 }
817
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +0000818 std::vector<uint32> primary_ssrcs;
819 sp.GetPrimarySsrcs(&primary_ssrcs);
820 std::vector<uint32> rtx_ssrcs;
821 sp.GetFidSsrcs(primary_ssrcs, &rtx_ssrcs);
822 if (!rtx_ssrcs.empty() && primary_ssrcs.size() != rtx_ssrcs.size()) {
823 LOG(LS_ERROR)
824 << "RTX SSRCs exist, but don't cover all SSRCs (unsupported): "
825 << sp.ToString();
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000826 return false;
827 }
828
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000829 WebRtcVideoSendStream* stream =
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000830 new WebRtcVideoSendStream(call_.get(),
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000831 external_encoder_factory_,
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000832 options_,
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +0000833 send_codec_,
834 sp,
835 send_rtp_extensions_);
836
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000837 send_streams_[ssrc] = stream;
838
839 if (rtcp_receiver_report_ssrc_ == kDefaultRtcpReceiverReportSsrc) {
840 rtcp_receiver_report_ssrc_ = ssrc;
841 }
842 if (default_send_ssrc_ == 0) {
843 default_send_ssrc_ = ssrc;
844 }
845 if (sending_) {
846 stream->Start();
847 }
848
849 return true;
850}
851
852bool WebRtcVideoChannel2::RemoveSendStream(uint32 ssrc) {
853 LOG(LS_INFO) << "RemoveSendStream: " << ssrc;
854
855 if (ssrc == 0) {
856 if (default_send_ssrc_ == 0) {
857 LOG(LS_ERROR) << "No default send stream active.";
858 return false;
859 }
860
861 LOG(LS_VERBOSE) << "Removing default stream: " << default_send_ssrc_;
862 ssrc = default_send_ssrc_;
863 }
864
pbos@webrtc.org575d1262014-10-08 14:48:08 +0000865 WebRtcVideoSendStream* removed_stream;
866 {
867 rtc::CritScope stream_lock(&stream_crit_);
868 std::map<uint32, WebRtcVideoSendStream*>::iterator it =
869 send_streams_.find(ssrc);
870 if (it == send_streams_.end()) {
871 return false;
872 }
873
874 removed_stream = it->second;
875 send_streams_.erase(it);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000876 }
877
pbos@webrtc.org575d1262014-10-08 14:48:08 +0000878 delete removed_stream;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000879
880 if (ssrc == default_send_ssrc_) {
881 default_send_ssrc_ = 0;
882 }
883
884 return true;
885}
886
887bool WebRtcVideoChannel2::AddRecvStream(const StreamParams& sp) {
888 LOG(LS_INFO) << "AddRecvStream: " << sp.ToString();
889 assert(sp.ssrcs.size() > 0);
890
891 uint32 ssrc = sp.first_ssrc();
892 assert(ssrc != 0); // TODO(pbos): Is this ever valid?
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000893
894 // TODO(pbos): Check if any of the SSRCs overlap.
pbos@webrtc.org575d1262014-10-08 14:48:08 +0000895 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000896 if (receive_streams_.find(ssrc) != receive_streams_.end()) {
897 LOG(LS_ERROR) << "Receive stream for SSRC " << ssrc << "already exists.";
898 return false;
899 }
900
pbos@webrtc.orgbd249bc2014-07-07 04:45:15 +0000901 webrtc::VideoReceiveStream::Config config;
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000902 ConfigureReceiverRtp(&config, sp);
pbos@webrtc.org3bf3d232014-10-31 12:59:34 +0000903
904 // Set up A/V sync if there is a VoiceChannel.
905 // TODO(pbos): The A/V is synched by the receiving channel. So we need to know
906 // the SSRC of the remote audio channel in order to sync the correct webrtc
907 // VoiceEngine channel. For now sync the first channel in non-conference to
908 // match existing behavior in WebRtcVideoEngine.
909 if (voice_channel_ != NULL && receive_streams_.empty() &&
910 !options_.conference_mode.GetWithDefaultIfUnset(false)) {
911 config.audio_channel_id =
912 static_cast<WebRtcVoiceMediaChannel*>(voice_channel_)->voe_channel();
913 }
914
pbos@webrtc.org776e6f22014-10-29 15:28:39 +0000915 receive_streams_[ssrc] = new WebRtcVideoReceiveStream(
916 call_.get(), external_decoder_factory_, config, recv_codecs_);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000917
918 return true;
919}
920
921void WebRtcVideoChannel2::ConfigureReceiverRtp(
922 webrtc::VideoReceiveStream::Config* config,
923 const StreamParams& sp) const {
924 uint32 ssrc = sp.first_ssrc();
925
926 config->rtp.remote_ssrc = ssrc;
927 config->rtp.local_ssrc = rtcp_receiver_report_ssrc_;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000928
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000929 config->rtp.extensions = recv_rtp_extensions_;
pbos@webrtc.org257e1302014-07-25 19:01:32 +0000930
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000931 // TODO(pbos): This protection is against setting the same local ssrc as
932 // remote which is not permitted by the lower-level API. RTCP requires a
933 // corresponding sender SSRC. Figure out what to do when we don't have
934 // (receive-only) or know a good local SSRC.
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000935 if (config->rtp.remote_ssrc == config->rtp.local_ssrc) {
936 if (config->rtp.local_ssrc != kDefaultRtcpReceiverReportSsrc) {
937 config->rtp.local_ssrc = kDefaultRtcpReceiverReportSsrc;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000938 } else {
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000939 config->rtp.local_ssrc = kDefaultRtcpReceiverReportSsrc + 1;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000940 }
941 }
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000942
943 for (size_t i = 0; i < recv_codecs_.size(); ++i) {
andresp@webrtc.org82775b12014-11-07 09:37:54 +0000944 MergeFecConfig(recv_codecs_[i].fec, &config->rtp.fec);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000945 }
946
andresp@webrtc.org82775b12014-11-07 09:37:54 +0000947 for (size_t i = 0; i < recv_codecs_.size(); ++i) {
948 uint32 rtx_ssrc;
949 if (recv_codecs_[i].rtx_payload_type != -1 &&
950 sp.GetFidSsrc(ssrc, &rtx_ssrc)) {
951 webrtc::VideoReceiveStream::Config::Rtp::Rtx& rtx =
952 config->rtp.rtx[recv_codecs_[i].codec.id];
953 rtx.ssrc = rtx_ssrc;
954 rtx.payload_type = recv_codecs_[i].rtx_payload_type;
955 }
956 }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000957}
958
959bool WebRtcVideoChannel2::RemoveRecvStream(uint32 ssrc) {
960 LOG(LS_INFO) << "RemoveRecvStream: " << ssrc;
961 if (ssrc == 0) {
pbos@webrtc.orgafb554f42014-08-12 23:17:13 +0000962 LOG(LS_ERROR) << "RemoveRecvStream with 0 ssrc is not supported.";
963 return false;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000964 }
965
pbos@webrtc.org575d1262014-10-08 14:48:08 +0000966 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000967 std::map<uint32, WebRtcVideoReceiveStream*>::iterator stream =
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000968 receive_streams_.find(ssrc);
969 if (stream == receive_streams_.end()) {
970 LOG(LS_ERROR) << "Stream not found for ssrc: " << ssrc;
971 return false;
972 }
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000973 delete stream->second;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000974 receive_streams_.erase(stream);
975
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000976 return true;
977}
978
979bool WebRtcVideoChannel2::SetRenderer(uint32 ssrc, VideoRenderer* renderer) {
980 LOG(LS_INFO) << "SetRenderer: ssrc:" << ssrc << " "
981 << (renderer ? "(ptr)" : "NULL");
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000982 if (ssrc == 0) {
pbos@webrtc.orgafb554f42014-08-12 23:17:13 +0000983 default_unsignalled_ssrc_handler_.SetDefaultRenderer(this, renderer);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000984 return true;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000985 }
986
pbos@webrtc.org575d1262014-10-08 14:48:08 +0000987 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000988 std::map<uint32, WebRtcVideoReceiveStream*>::iterator it =
989 receive_streams_.find(ssrc);
990 if (it == receive_streams_.end()) {
991 return false;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000992 }
993
994 it->second->SetRenderer(renderer);
995 return true;
996}
997
998bool WebRtcVideoChannel2::GetRenderer(uint32 ssrc, VideoRenderer** renderer) {
999 if (ssrc == 0) {
pbos@webrtc.orgafb554f42014-08-12 23:17:13 +00001000 *renderer = default_unsignalled_ssrc_handler_.GetDefaultRenderer();
1001 return *renderer != NULL;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001002 }
1003
pbos@webrtc.org575d1262014-10-08 14:48:08 +00001004 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001005 std::map<uint32, WebRtcVideoReceiveStream*>::iterator it =
1006 receive_streams_.find(ssrc);
1007 if (it == receive_streams_.end()) {
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001008 return false;
1009 }
1010 *renderer = it->second->GetRenderer();
1011 return true;
1012}
1013
1014bool WebRtcVideoChannel2::GetStats(const StatsOptions& options,
1015 VideoMediaInfo* info) {
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00001016 info->Clear();
1017 FillSenderStats(info);
1018 FillReceiverStats(info);
pbos@webrtc.org2b19f062014-12-11 13:26:09 +00001019 webrtc::Call::Stats stats = call_->GetStats();
1020 FillBandwidthEstimationStats(stats, info);
1021 if (stats.rtt_ms != -1) {
1022 for (size_t i = 0; i < info->senders.size(); ++i) {
1023 info->senders[i].rtt_ms = stats.rtt_ms;
1024 }
1025 }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001026 return true;
1027}
1028
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00001029void WebRtcVideoChannel2::FillSenderStats(VideoMediaInfo* video_media_info) {
pbos@webrtc.org575d1262014-10-08 14:48:08 +00001030 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00001031 for (std::map<uint32, WebRtcVideoSendStream*>::iterator it =
1032 send_streams_.begin();
1033 it != send_streams_.end();
1034 ++it) {
1035 video_media_info->senders.push_back(it->second->GetVideoSenderInfo());
1036 }
1037}
1038
1039void WebRtcVideoChannel2::FillReceiverStats(VideoMediaInfo* video_media_info) {
pbos@webrtc.org575d1262014-10-08 14:48:08 +00001040 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00001041 for (std::map<uint32, WebRtcVideoReceiveStream*>::iterator it =
1042 receive_streams_.begin();
1043 it != receive_streams_.end();
1044 ++it) {
1045 video_media_info->receivers.push_back(it->second->GetVideoReceiverInfo());
1046 }
1047}
1048
1049void WebRtcVideoChannel2::FillBandwidthEstimationStats(
pbos@webrtc.org2b19f062014-12-11 13:26:09 +00001050 const webrtc::Call::Stats& stats,
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00001051 VideoMediaInfo* video_media_info) {
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001052 BandwidthEstimationInfo bwe_info;
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001053 bwe_info.available_send_bandwidth = stats.send_bandwidth_bps;
1054 bwe_info.available_recv_bandwidth = stats.recv_bandwidth_bps;
1055 bwe_info.bucket_delay = stats.pacer_delay_ms;
1056
1057 // Get send stream bitrate stats.
1058 rtc::CritScope stream_lock(&stream_crit_);
1059 for (std::map<uint32, WebRtcVideoSendStream*>::iterator stream =
1060 send_streams_.begin();
1061 stream != send_streams_.end();
1062 ++stream) {
1063 stream->second->FillBandwidthEstimationInfo(&bwe_info);
1064 }
1065 video_media_info->bw_estimations.push_back(bwe_info);
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00001066}
1067
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001068bool WebRtcVideoChannel2::SetCapturer(uint32 ssrc, VideoCapturer* capturer) {
1069 LOG(LS_INFO) << "SetCapturer: " << ssrc << " -> "
1070 << (capturer != NULL ? "(capturer)" : "NULL");
1071 assert(ssrc != 0);
pbos@webrtc.org575d1262014-10-08 14:48:08 +00001072 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001073 if (send_streams_.find(ssrc) == send_streams_.end()) {
1074 LOG(LS_ERROR) << "No sending stream on ssrc " << ssrc;
1075 return false;
1076 }
1077 return send_streams_[ssrc]->SetCapturer(capturer);
1078}
1079
1080bool WebRtcVideoChannel2::SendIntraFrame() {
1081 // TODO(pbos): Implement.
1082 LOG(LS_VERBOSE) << "SendIntraFrame().";
1083 return true;
1084}
1085
1086bool WebRtcVideoChannel2::RequestIntraFrame() {
1087 // TODO(pbos): Implement.
1088 LOG(LS_VERBOSE) << "SendIntraFrame().";
1089 return true;
1090}
1091
1092void WebRtcVideoChannel2::OnPacketReceived(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001093 rtc::Buffer* packet,
1094 const rtc::PacketTime& packet_time) {
pbos@webrtc.org4e545cc2014-05-14 13:58:13 +00001095 const webrtc::PacketReceiver::DeliveryStatus delivery_result =
1096 call_->Receiver()->DeliverPacket(
1097 reinterpret_cast<const uint8_t*>(packet->data()), packet->length());
1098 switch (delivery_result) {
1099 case webrtc::PacketReceiver::DELIVERY_OK:
1100 return;
1101 case webrtc::PacketReceiver::DELIVERY_PACKET_ERROR:
1102 return;
1103 case webrtc::PacketReceiver::DELIVERY_UNKNOWN_SSRC:
1104 break;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001105 }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001106
1107 uint32 ssrc = 0;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001108 if (!GetRtpSsrc(packet->data(), packet->length(), &ssrc)) {
1109 return;
1110 }
1111
pbos@webrtc.orgafb554f42014-08-12 23:17:13 +00001112 // TODO(pbos): Make sure that the unsignalled SSRC uses the video payload.
1113 // Also figure out whether RTX needs to be handled.
1114 switch (unsignalled_ssrc_handler_->OnUnsignalledSsrc(this, ssrc)) {
1115 case UnsignalledSsrcHandler::kDropPacket:
1116 return;
1117 case UnsignalledSsrcHandler::kDeliverPacket:
1118 break;
1119 }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001120
pbos@webrtc.org1e019d12014-05-16 11:38:45 +00001121 if (call_->Receiver()->DeliverPacket(
1122 reinterpret_cast<const uint8_t*>(packet->data()), packet->length()) !=
1123 webrtc::PacketReceiver::DELIVERY_OK) {
pbos@webrtc.orgafb554f42014-08-12 23:17:13 +00001124 LOG(LS_WARNING) << "Failed to deliver RTP packet on re-delivery.";
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001125 return;
1126 }
1127}
1128
1129void WebRtcVideoChannel2::OnRtcpReceived(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001130 rtc::Buffer* packet,
1131 const rtc::PacketTime& packet_time) {
pbos@webrtc.org1e019d12014-05-16 11:38:45 +00001132 if (call_->Receiver()->DeliverPacket(
1133 reinterpret_cast<const uint8_t*>(packet->data()), packet->length()) !=
1134 webrtc::PacketReceiver::DELIVERY_OK) {
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001135 LOG(LS_WARNING) << "Failed to deliver RTCP packet.";
1136 }
1137}
1138
1139void WebRtcVideoChannel2::OnReadyToSend(bool ready) {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +00001140 LOG(LS_VERBOSE) << "OnReadyToSend: " << (ready ? "Ready." : "Not ready.");
1141 call_->SignalNetworkState(ready ? webrtc::Call::kNetworkUp
1142 : webrtc::Call::kNetworkDown);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001143}
1144
1145bool WebRtcVideoChannel2::MuteStream(uint32 ssrc, bool mute) {
1146 LOG(LS_VERBOSE) << "MuteStream: " << ssrc << " -> "
1147 << (mute ? "mute" : "unmute");
1148 assert(ssrc != 0);
pbos@webrtc.org575d1262014-10-08 14:48:08 +00001149 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001150 if (send_streams_.find(ssrc) == send_streams_.end()) {
1151 LOG(LS_ERROR) << "No sending stream on ssrc " << ssrc;
1152 return false;
1153 }
pbos@webrtc.orgef8bb8d2014-08-13 21:36:18 +00001154
1155 send_streams_[ssrc]->MuteStream(mute);
1156 return true;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001157}
1158
1159bool WebRtcVideoChannel2::SetRecvRtpHeaderExtensions(
1160 const std::vector<RtpHeaderExtension>& extensions) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +00001161 TRACE_EVENT0("webrtc", "WebRtcVideoChannel2::SetRecvRtpHeaderExtensions");
pbos@webrtc.org587ef602014-06-16 17:32:02 +00001162 LOG(LS_INFO) << "SetRecvRtpHeaderExtensions: "
1163 << RtpExtensionsToString(extensions);
pbos@webrtc.org3c107582014-07-20 15:27:35 +00001164 if (!ValidateRtpHeaderExtensionIds(extensions))
1165 return false;
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001166
pbos@webrtc.orgc37e72e2015-01-05 18:51:13 +00001167 std::vector<webrtc::RtpExtension> filtered_extensions =
1168 FilterRtpExtensions(extensions);
1169 if (!RtpExtensionsHaveChanged(recv_rtp_extensions_, filtered_extensions))
1170 return true;
1171
1172 recv_rtp_extensions_ = filtered_extensions;
1173
pbos@webrtc.org575d1262014-10-08 14:48:08 +00001174 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001175 for (std::map<uint32, WebRtcVideoReceiveStream*>::iterator it =
1176 receive_streams_.begin();
1177 it != receive_streams_.end();
1178 ++it) {
1179 it->second->SetRtpExtensions(recv_rtp_extensions_);
1180 }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001181 return true;
1182}
1183
1184bool WebRtcVideoChannel2::SetSendRtpHeaderExtensions(
1185 const std::vector<RtpHeaderExtension>& extensions) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +00001186 TRACE_EVENT0("webrtc", "WebRtcVideoChannel2::SetSendRtpHeaderExtensions");
pbos@webrtc.org587ef602014-06-16 17:32:02 +00001187 LOG(LS_INFO) << "SetSendRtpHeaderExtensions: "
1188 << RtpExtensionsToString(extensions);
pbos@webrtc.org3c107582014-07-20 15:27:35 +00001189 if (!ValidateRtpHeaderExtensionIds(extensions))
1190 return false;
1191
pbos@webrtc.orgc37e72e2015-01-05 18:51:13 +00001192 std::vector<webrtc::RtpExtension> filtered_extensions =
1193 FilterRtpExtensions(extensions);
1194 if (!RtpExtensionsHaveChanged(send_rtp_extensions_, filtered_extensions))
1195 return true;
1196
1197 send_rtp_extensions_ = filtered_extensions;
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +00001198
pbos@webrtc.org575d1262014-10-08 14:48:08 +00001199 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001200 for (std::map<uint32, WebRtcVideoSendStream*>::iterator it =
1201 send_streams_.begin();
1202 it != send_streams_.end();
1203 ++it) {
1204 it->second->SetRtpExtensions(send_rtp_extensions_);
1205 }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001206 return true;
1207}
1208
pbos@webrtc.org00873182014-11-25 14:03:34 +00001209bool WebRtcVideoChannel2::SetMaxSendBandwidth(int max_bitrate_bps) {
1210 LOG(LS_INFO) << "SetMaxSendBandwidth: " << max_bitrate_bps << "bps.";
1211 if (max_bitrate_bps <= 0) {
1212 // Unsetting max bitrate.
1213 max_bitrate_bps = -1;
1214 }
1215 bitrate_config_.start_bitrate_bps = -1;
1216 bitrate_config_.max_bitrate_bps = max_bitrate_bps;
1217 if (max_bitrate_bps > 0 &&
1218 bitrate_config_.min_bitrate_bps > max_bitrate_bps) {
1219 bitrate_config_.min_bitrate_bps = max_bitrate_bps;
1220 }
1221 call_->SetBitrateConfig(bitrate_config_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001222 return true;
1223}
1224
1225bool WebRtcVideoChannel2::SetOptions(const VideoOptions& options) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +00001226 TRACE_EVENT0("webrtc", "WebRtcVideoChannel2::SetOptions");
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +00001227 LOG(LS_INFO) << "SetOptions: " << options.ToString();
1228 VideoOptions old_options = options_;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001229 options_.SetAll(options);
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +00001230 if (options_ == old_options) {
1231 // No new options to set.
1232 return true;
1233 }
pbos@webrtc.orgd8198032014-11-10 14:41:43 +00001234 rtc::DiffServCodePoint dscp = options_.dscp.GetWithDefaultIfUnset(false)
1235 ? rtc::DSCP_AF41
1236 : rtc::DSCP_DEFAULT;
1237 MediaChannel::SetDscp(dscp);
pbos@webrtc.org575d1262014-10-08 14:48:08 +00001238 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001239 for (std::map<uint32, WebRtcVideoSendStream*>::iterator it =
1240 send_streams_.begin();
1241 it != send_streams_.end();
1242 ++it) {
1243 it->second->SetOptions(options_);
1244 }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001245 return true;
1246}
1247
1248void WebRtcVideoChannel2::SetInterface(NetworkInterface* iface) {
1249 MediaChannel::SetInterface(iface);
1250 // Set the RTP recv/send buffer to a bigger size
1251 MediaChannel::SetOption(NetworkInterface::ST_RTP,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001252 rtc::Socket::OPT_RCVBUF,
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001253 kVideoRtpBufferSize);
1254
buildbot@webrtc.orgae694ef2014-10-28 17:37:17 +00001255 // Speculative change to increase the outbound socket buffer size.
1256 // In b/15152257, we are seeing a significant number of packets discarded
1257 // due to lack of socket buffer space, although it's not yet clear what the
1258 // ideal value should be.
1259 MediaChannel::SetOption(NetworkInterface::ST_RTP,
1260 rtc::Socket::OPT_SNDBUF,
1261 kVideoRtpBufferSize);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001262}
1263
1264void WebRtcVideoChannel2::UpdateAspectRatio(int ratio_w, int ratio_h) {
1265 // TODO(pbos): Implement.
1266}
1267
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001268void WebRtcVideoChannel2::OnMessage(rtc::Message* msg) {
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001269 // Ignored.
1270}
1271
pbos@webrtc.org42684be2014-10-03 11:25:45 +00001272void WebRtcVideoChannel2::OnLoadUpdate(Load load) {
pbos@webrtc.org575d1262014-10-08 14:48:08 +00001273 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.org42684be2014-10-03 11:25:45 +00001274 for (std::map<uint32, WebRtcVideoSendStream*>::iterator it =
1275 send_streams_.begin();
1276 it != send_streams_.end();
1277 ++it) {
1278 it->second->OnCpuResolutionRequest(load == kOveruse
1279 ? CoordinatedVideoAdapter::DOWNGRADE
1280 : CoordinatedVideoAdapter::UPGRADE);
1281 }
1282}
1283
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001284bool WebRtcVideoChannel2::SendRtp(const uint8_t* data, size_t len) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001285 rtc::Buffer packet(data, len, kMaxRtpPacketLen);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001286 return MediaChannel::SendPacket(&packet);
1287}
1288
1289bool WebRtcVideoChannel2::SendRtcp(const uint8_t* data, size_t len) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001290 rtc::Buffer packet(data, len, kMaxRtpPacketLen);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001291 return MediaChannel::SendRtcp(&packet);
1292}
1293
1294void WebRtcVideoChannel2::StartAllSendStreams() {
pbos@webrtc.org575d1262014-10-08 14:48:08 +00001295 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001296 for (std::map<uint32, WebRtcVideoSendStream*>::iterator it =
1297 send_streams_.begin();
1298 it != send_streams_.end();
1299 ++it) {
1300 it->second->Start();
1301 }
1302}
1303
1304void WebRtcVideoChannel2::StopAllSendStreams() {
pbos@webrtc.org575d1262014-10-08 14:48:08 +00001305 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001306 for (std::map<uint32, WebRtcVideoSendStream*>::iterator it =
1307 send_streams_.begin();
1308 it != send_streams_.end();
1309 ++it) {
1310 it->second->Stop();
1311 }
1312}
1313
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +00001314WebRtcVideoChannel2::WebRtcVideoSendStream::VideoSendStreamParameters::
1315 VideoSendStreamParameters(
1316 const webrtc::VideoSendStream::Config& config,
1317 const VideoOptions& options,
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001318 const Settable<VideoCodecSettings>& codec_settings)
1319 : config(config), options(options), codec_settings(codec_settings) {
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +00001320}
1321
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001322WebRtcVideoChannel2::WebRtcVideoSendStream::WebRtcVideoSendStream(
1323 webrtc::Call* call,
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +00001324 WebRtcVideoEncoderFactory* external_encoder_factory,
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +00001325 const VideoOptions& options,
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001326 const Settable<VideoCodecSettings>& codec_settings,
1327 const StreamParams& sp,
1328 const std::vector<webrtc::RtpExtension>& rtp_extensions)
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001329 : call_(call),
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +00001330 external_encoder_factory_(external_encoder_factory),
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001331 stream_(NULL),
pbos@webrtc.orgb648b9d2014-08-26 11:08:06 +00001332 parameters_(webrtc::VideoSendStream::Config(), options, codec_settings),
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +00001333 allocated_encoder_(NULL, webrtc::kVideoCodecUnknown, false),
pbos@webrtc.orgb648b9d2014-08-26 11:08:06 +00001334 capturer_(NULL),
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001335 sending_(false),
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001336 muted_(false) {
1337 parameters_.config.rtp.max_packet_size = kVideoMtu;
1338
1339 sp.GetPrimarySsrcs(&parameters_.config.rtp.ssrcs);
1340 sp.GetFidSsrcs(parameters_.config.rtp.ssrcs,
1341 &parameters_.config.rtp.rtx.ssrcs);
1342 parameters_.config.rtp.c_name = sp.cname;
1343 parameters_.config.rtp.extensions = rtp_extensions;
1344
1345 VideoCodecSettings params;
1346 if (codec_settings.Get(&params)) {
1347 SetCodec(params);
1348 }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001349}
1350
1351WebRtcVideoChannel2::WebRtcVideoSendStream::~WebRtcVideoSendStream() {
1352 DisconnectCapturer();
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001353 if (stream_ != NULL) {
1354 call_->DestroyVideoSendStream(stream_);
1355 }
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +00001356 DestroyVideoEncoder(&allocated_encoder_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001357}
1358
1359static void SetWebRtcFrameToBlack(webrtc::I420VideoFrame* video_frame) {
1360 assert(video_frame != NULL);
1361 memset(video_frame->buffer(webrtc::kYPlane),
1362 16,
1363 video_frame->allocated_size(webrtc::kYPlane));
1364 memset(video_frame->buffer(webrtc::kUPlane),
1365 128,
1366 video_frame->allocated_size(webrtc::kUPlane));
1367 memset(video_frame->buffer(webrtc::kVPlane),
1368 128,
1369 video_frame->allocated_size(webrtc::kVPlane));
1370}
1371
1372static void CreateBlackFrame(webrtc::I420VideoFrame* video_frame,
1373 int width,
1374 int height) {
1375 video_frame->CreateEmptyFrame(
1376 width, height, width, (width + 1) / 2, (width + 1) / 2);
1377 SetWebRtcFrameToBlack(video_frame);
1378}
1379
1380static void ConvertToI420VideoFrame(const VideoFrame& frame,
1381 webrtc::I420VideoFrame* i420_frame) {
1382 i420_frame->CreateFrame(
1383 static_cast<int>(frame.GetYPitch() * frame.GetHeight()),
1384 frame.GetYPlane(),
1385 static_cast<int>(frame.GetUPitch() * ((frame.GetHeight() + 1) / 2)),
1386 frame.GetUPlane(),
1387 static_cast<int>(frame.GetVPitch() * ((frame.GetHeight() + 1) / 2)),
1388 frame.GetVPlane(),
1389 static_cast<int>(frame.GetWidth()),
1390 static_cast<int>(frame.GetHeight()),
1391 static_cast<int>(frame.GetYPitch()),
1392 static_cast<int>(frame.GetUPitch()),
1393 static_cast<int>(frame.GetVPitch()));
1394}
1395
1396void WebRtcVideoChannel2::WebRtcVideoSendStream::InputFrame(
1397 VideoCapturer* capturer,
1398 const VideoFrame* frame) {
pbos@webrtc.org86196c42015-02-16 21:02:00 +00001399 TRACE_EVENT0("webrtc", "WebRtcVideoSendStream::InputFrame");
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001400 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.org86196c42015-02-16 21:02:00 +00001412
1413 // Not sending, abort early to prevent expensive reconfigurations while
1414 // setting up codecs etc.
1415 if (!sending_)
1416 return;
1417
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001418 if (format_.width == 0) { // Dropping frames.
1419 assert(format_.height == 0);
1420 LOG(LS_VERBOSE) << "VideoFormat 0x0 set, Dropping frame.";
1421 return;
1422 }
pbos@webrtc.orgd60d79a2014-09-24 07:10:57 +00001423 if (muted_) {
1424 // Create a black frame to transmit instead.
1425 CreateBlackFrame(&video_frame_,
1426 static_cast<int>(frame->GetWidth()),
1427 static_cast<int>(frame->GetHeight()));
1428 }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001429 // Reconfigure codec if necessary.
pbos@webrtc.orgc4175b92014-09-03 15:25:49 +00001430 SetDimensions(
1431 video_frame_.width(), video_frame_.height(), capturer->IsScreencast());
1432
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001433 LOG(LS_VERBOSE) << "SwapFrame: " << video_frame_.width() << "x"
1434 << video_frame_.height() << " -> (codec) "
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +00001435 << parameters_.encoder_config.streams.back().width << "x"
1436 << parameters_.encoder_config.streams.back().height;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001437 stream_->Input()->SwapFrame(&video_frame_);
1438}
1439
1440bool WebRtcVideoChannel2::WebRtcVideoSendStream::SetCapturer(
1441 VideoCapturer* capturer) {
1442 if (!DisconnectCapturer() && capturer == NULL) {
1443 return false;
1444 }
1445
1446 {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001447 rtc::CritScope cs(&lock_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001448
pbos@webrtc.org9359cb32014-07-23 15:44:48 +00001449 if (capturer == NULL) {
1450 if (stream_ != NULL) {
1451 LOG(LS_VERBOSE) << "Disabling capturer, sending black frame.";
1452 webrtc::I420VideoFrame black_frame;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001453
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +00001454 // TODO(pbos): Base width/height on last_dimensions_. This will however
1455 // fail the test AddRemoveCapturer which needs to be fixed to permit
1456 // sending black frames in the same size that was previously sent.
pbos@webrtc.org9359cb32014-07-23 15:44:48 +00001457 int width = format_.width;
1458 int height = format_.height;
1459 int half_width = (width + 1) / 2;
1460 black_frame.CreateEmptyFrame(
1461 width, height, width, half_width, half_width);
1462 SetWebRtcFrameToBlack(&black_frame);
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +00001463 SetDimensions(width, height, last_dimensions_.is_screencast);
pbos@webrtc.org9359cb32014-07-23 15:44:48 +00001464 stream_->Input()->SwapFrame(&black_frame);
1465 }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001466
1467 capturer_ = NULL;
1468 return true;
1469 }
1470
1471 capturer_ = capturer;
1472 }
1473 // Lock cannot be held while connecting the capturer to prevent lock-order
1474 // violations.
1475 capturer->SignalVideoFrame.connect(this, &WebRtcVideoSendStream::InputFrame);
1476 return true;
1477}
1478
1479bool WebRtcVideoChannel2::WebRtcVideoSendStream::SetVideoFormat(
1480 const VideoFormat& format) {
1481 if ((format.width == 0 || format.height == 0) &&
1482 format.width != format.height) {
1483 LOG(LS_ERROR) << "Can't set VideoFormat, width or height is zero (but not "
1484 "both, 0x0 drops frames).";
1485 return false;
1486 }
1487
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001488 rtc::CritScope cs(&lock_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001489 if (format.width == 0 && format.height == 0) {
1490 LOG(LS_INFO)
1491 << "0x0 resolution selected. Captured frames will be dropped for ssrc: "
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +00001492 << parameters_.config.rtp.ssrcs[0] << ".";
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001493 } else {
1494 // TODO(pbos): Fix me, this only affects the last stream!
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +00001495 parameters_.encoder_config.streams.back().max_framerate =
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001496 VideoFormat::IntervalToFps(format.interval);
pbos@webrtc.orgc4175b92014-09-03 15:25:49 +00001497 SetDimensions(format.width, format.height, false);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001498 }
1499
1500 format_ = format;
1501 return true;
1502}
1503
pbos@webrtc.orgef8bb8d2014-08-13 21:36:18 +00001504void WebRtcVideoChannel2::WebRtcVideoSendStream::MuteStream(bool mute) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001505 rtc::CritScope cs(&lock_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001506 muted_ = mute;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001507}
1508
1509bool WebRtcVideoChannel2::WebRtcVideoSendStream::DisconnectCapturer() {
pbos@webrtc.org963b9792014-10-07 14:27:27 +00001510 cricket::VideoCapturer* capturer;
1511 {
1512 rtc::CritScope cs(&lock_);
1513 if (capturer_ == NULL) {
1514 return false;
1515 }
1516 capturer = capturer_;
1517 capturer_ = NULL;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001518 }
pbos@webrtc.org963b9792014-10-07 14:27:27 +00001519 capturer->SignalVideoFrame.disconnect(this);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001520 return true;
1521}
1522
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001523void WebRtcVideoChannel2::WebRtcVideoSendStream::SetOptions(
1524 const VideoOptions& options) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001525 rtc::CritScope cs(&lock_);
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001526 VideoCodecSettings codec_settings;
1527 if (parameters_.codec_settings.Get(&codec_settings)) {
1528 SetCodecAndOptions(codec_settings, options);
1529 } else {
1530 parameters_.options = options;
1531 }
1532}
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +00001533
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001534void WebRtcVideoChannel2::WebRtcVideoSendStream::SetCodec(
1535 const VideoCodecSettings& codec_settings) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001536 rtc::CritScope cs(&lock_);
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001537 SetCodecAndOptions(codec_settings, parameters_.options);
1538}
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +00001539
1540webrtc::VideoCodecType CodecTypeFromName(const std::string& name) {
1541 if (CodecNameMatches(name, kVp8CodecName)) {
1542 return webrtc::kVideoCodecVP8;
andresp@webrtc.org188d3b22014-11-07 13:21:04 +00001543 } else if (CodecNameMatches(name, kVp9CodecName)) {
1544 return webrtc::kVideoCodecVP9;
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +00001545 } else if (CodecNameMatches(name, kH264CodecName)) {
1546 return webrtc::kVideoCodecH264;
1547 }
1548 return webrtc::kVideoCodecUnknown;
1549}
1550
1551WebRtcVideoChannel2::WebRtcVideoSendStream::AllocatedEncoder
1552WebRtcVideoChannel2::WebRtcVideoSendStream::CreateVideoEncoder(
1553 const VideoCodec& codec) {
1554 webrtc::VideoCodecType type = CodecTypeFromName(codec.name);
1555
1556 // Do not re-create encoders of the same type.
1557 if (type == allocated_encoder_.type && allocated_encoder_.encoder != NULL) {
1558 return allocated_encoder_;
1559 }
1560
1561 if (external_encoder_factory_ != NULL) {
1562 webrtc::VideoEncoder* encoder =
1563 external_encoder_factory_->CreateVideoEncoder(type);
1564 if (encoder != NULL) {
1565 return AllocatedEncoder(encoder, type, true);
1566 }
1567 }
1568
1569 if (type == webrtc::kVideoCodecVP8) {
1570 return AllocatedEncoder(
1571 webrtc::VideoEncoder::Create(webrtc::VideoEncoder::kVp8), type, false);
andresp@webrtc.org188d3b22014-11-07 13:21:04 +00001572 } else if (type == webrtc::kVideoCodecVP9) {
1573 return AllocatedEncoder(
1574 webrtc::VideoEncoder::Create(webrtc::VideoEncoder::kVp9), type, false);
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +00001575 }
1576
1577 // This shouldn't happen, we should not be trying to create something we don't
1578 // support.
1579 assert(false);
1580 return AllocatedEncoder(NULL, webrtc::kVideoCodecUnknown, false);
1581}
1582
1583void WebRtcVideoChannel2::WebRtcVideoSendStream::DestroyVideoEncoder(
1584 AllocatedEncoder* encoder) {
1585 if (encoder->external) {
1586 external_encoder_factory_->DestroyVideoEncoder(encoder->encoder);
1587 } else {
1588 delete encoder->encoder;
1589 }
1590}
1591
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001592void WebRtcVideoChannel2::WebRtcVideoSendStream::SetCodecAndOptions(
1593 const VideoCodecSettings& codec_settings,
1594 const VideoOptions& options) {
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +00001595 parameters_.encoder_config =
1596 CreateVideoEncoderConfig(last_dimensions_, codec_settings.codec);
pbos@webrtc.org86196c42015-02-16 21:02:00 +00001597 if (parameters_.encoder_config.streams.empty())
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001598 return;
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
pbos@webrtc.org0d852d52015-02-09 15:14:36 +00002001bool WebRtcVideoChannel2::WebRtcVideoReceiveStream::IsTextureSupported() const {
2002 return true;
2003}
2004
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00002005void WebRtcVideoChannel2::WebRtcVideoReceiveStream::SetRenderer(
2006 cricket::VideoRenderer* renderer) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002007 rtc::CritScope crit(&renderer_lock_);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00002008 renderer_ = renderer;
2009 if (renderer_ != NULL && last_width_ != -1) {
2010 SetSize(last_width_, last_height_);
2011 }
2012}
2013
2014VideoRenderer* WebRtcVideoChannel2::WebRtcVideoReceiveStream::GetRenderer() {
2015 // TODO(pbos): Remove GetRenderer and all uses of it, it's thread-unsafe by
2016 // design.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002017 rtc::CritScope crit(&renderer_lock_);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00002018 return renderer_;
2019}
2020
2021void WebRtcVideoChannel2::WebRtcVideoReceiveStream::SetSize(int width,
2022 int height) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002023 rtc::CritScope crit(&renderer_lock_);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00002024 if (!renderer_->SetSize(width, height, 0)) {
2025 LOG(LS_ERROR) << "Could not set renderer size.";
2026 }
2027 last_width_ = width;
2028 last_height_ = height;
2029}
2030
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00002031VideoReceiverInfo
2032WebRtcVideoChannel2::WebRtcVideoReceiveStream::GetVideoReceiverInfo() {
2033 VideoReceiverInfo info;
2034 info.add_ssrc(config_.rtp.remote_ssrc);
2035 webrtc::VideoReceiveStream::Stats stats = stream_->GetStats();
asapersson@webrtc.orgcfd82df2015-01-22 09:39:59 +00002036 info.bytes_rcvd = stats.rtp_stats.transmitted.payload_bytes +
2037 stats.rtp_stats.transmitted.header_bytes +
2038 stats.rtp_stats.transmitted.padding_bytes;
2039 info.packets_rcvd = stats.rtp_stats.transmitted.packets;
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00002040
2041 info.framerate_rcvd = stats.network_frame_rate;
2042 info.framerate_decoded = stats.decode_frame_rate;
2043 info.framerate_output = stats.render_frame_rate;
2044
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002045 rtc::CritScope frame_cs(&renderer_lock_);
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00002046 info.frame_width = last_width_;
2047 info.frame_height = last_height_;
magjed@webrtc.orgfc5ad952015-01-27 09:57:01 +00002048 info.capture_start_ntp_time_ms = estimated_remote_start_ntp_time_ms_;
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00002049
2050 // TODO(pbos): Support or remove the following stats.
2051 info.packets_concealed = -1;
2052
2053 return info;
2054}
2055
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00002056WebRtcVideoChannel2::VideoCodecSettings::VideoCodecSettings()
2057 : rtx_payload_type(-1) {}
2058
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +00002059bool WebRtcVideoChannel2::VideoCodecSettings::operator==(
2060 const WebRtcVideoChannel2::VideoCodecSettings& other) const {
2061 return codec == other.codec &&
2062 fec.ulpfec_payload_type == other.fec.ulpfec_payload_type &&
2063 fec.red_payload_type == other.fec.red_payload_type &&
2064 rtx_payload_type == other.rtx_payload_type;
2065}
2066
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00002067std::vector<WebRtcVideoChannel2::VideoCodecSettings>
2068WebRtcVideoChannel2::MapCodecs(const std::vector<VideoCodec>& codecs) {
2069 assert(!codecs.empty());
2070
2071 std::vector<VideoCodecSettings> video_codecs;
2072 std::map<int, bool> payload_used;
pbos@webrtc.orge322a172014-06-13 11:47:28 +00002073 std::map<int, VideoCodec::CodecType> payload_codec_type;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00002074 std::map<int, int> rtx_mapping; // video payload type -> rtx payload type.
2075
2076 webrtc::FecConfig fec_settings;
2077
2078 for (size_t i = 0; i < codecs.size(); ++i) {
2079 const VideoCodec& in_codec = codecs[i];
2080 int payload_type = in_codec.id;
2081
2082 if (payload_used[payload_type]) {
2083 LOG(LS_ERROR) << "Payload type already registered: "
2084 << in_codec.ToString();
2085 return std::vector<VideoCodecSettings>();
2086 }
2087 payload_used[payload_type] = true;
pbos@webrtc.orge322a172014-06-13 11:47:28 +00002088 payload_codec_type[payload_type] = in_codec.GetCodecType();
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00002089
2090 switch (in_codec.GetCodecType()) {
2091 case VideoCodec::CODEC_RED: {
2092 // RED payload type, should not have duplicates.
2093 assert(fec_settings.red_payload_type == -1);
2094 fec_settings.red_payload_type = in_codec.id;
2095 continue;
2096 }
2097
2098 case VideoCodec::CODEC_ULPFEC: {
2099 // ULPFEC payload type, should not have duplicates.
2100 assert(fec_settings.ulpfec_payload_type == -1);
2101 fec_settings.ulpfec_payload_type = in_codec.id;
2102 continue;
2103 }
2104
2105 case VideoCodec::CODEC_RTX: {
2106 int associated_payload_type;
2107 if (!in_codec.GetParam(kCodecParamAssociatedPayloadType,
pkasting@chromium.orge9facf82015-02-17 20:36:28 +00002108 &associated_payload_type) ||
2109 !IsValidRtpPayloadType(associated_payload_type)) {
2110 LOG(LS_ERROR)
2111 << "RTX codec with invalid or no associated payload type: "
2112 << in_codec.ToString();
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00002113 return std::vector<VideoCodecSettings>();
2114 }
2115 rtx_mapping[associated_payload_type] = in_codec.id;
2116 continue;
2117 }
2118
2119 case VideoCodec::CODEC_VIDEO:
2120 break;
2121 }
2122
2123 video_codecs.push_back(VideoCodecSettings());
2124 video_codecs.back().codec = in_codec;
2125 }
2126
2127 // One of these codecs should have been a video codec. Only having FEC
2128 // parameters into this code is a logic error.
2129 assert(!video_codecs.empty());
2130
pbos@webrtc.orge322a172014-06-13 11:47:28 +00002131 for (std::map<int, int>::const_iterator it = rtx_mapping.begin();
2132 it != rtx_mapping.end();
2133 ++it) {
2134 if (!payload_used[it->first]) {
2135 LOG(LS_ERROR) << "RTX mapped to payload not in codec list.";
2136 return std::vector<VideoCodecSettings>();
2137 }
andrew@webrtc.org8f27fcc2015-01-09 20:22:46 +00002138 if (payload_codec_type[it->first] != VideoCodec::CODEC_VIDEO) {
2139 LOG(LS_ERROR) << "RTX not mapped to regular video codec.";
pbos@webrtc.orge322a172014-06-13 11:47:28 +00002140 return std::vector<VideoCodecSettings>();
2141 }
2142 }
2143
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00002144 // TODO(pbos): Write tests that figure out that I have not verified that RTX
2145 // codecs aren't mapped to bogus payloads.
2146 for (size_t i = 0; i < video_codecs.size(); ++i) {
2147 video_codecs[i].fec = fec_settings;
andrew@webrtc.org8f27fcc2015-01-09 20:22:46 +00002148 if (rtx_mapping[video_codecs[i].codec.id] != 0) {
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00002149 video_codecs[i].rtx_payload_type = rtx_mapping[video_codecs[i].codec.id];
2150 }
2151 }
2152
2153 return video_codecs;
2154}
2155
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00002156} // namespace cricket
2157
2158#endif // HAVE_WEBRTC_VIDEO