blob: f2643c38b7090739fcd2c647f383583add37432b [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 +0000136const char kH264CodecName[] = "H264";
137
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000138static bool FindFirstMatchingCodec(const std::vector<VideoCodec>& codecs,
139 const VideoCodec& requested_codec,
140 VideoCodec* matching_codec) {
141 for (size_t i = 0; i < codecs.size(); ++i) {
142 if (requested_codec.Matches(codecs[i])) {
143 *matching_codec = codecs[i];
144 return true;
145 }
146 }
147 return false;
148}
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000149
pbos@webrtc.org3c107582014-07-20 15:27:35 +0000150static bool ValidateRtpHeaderExtensionIds(
151 const std::vector<RtpHeaderExtension>& extensions) {
152 std::set<int> extensions_used;
153 for (size_t i = 0; i < extensions.size(); ++i) {
154 if (extensions[i].id < 0 || extensions[i].id >= 15 ||
155 !extensions_used.insert(extensions[i].id).second) {
156 LOG(LS_ERROR) << "RTP extensions are with incorrect or duplicate ids.";
157 return false;
158 }
159 }
160 return true;
161}
162
pbos@webrtc.orgc37e72e2015-01-05 18:51:13 +0000163static bool CompareRtpHeaderExtensionIds(
164 const webrtc::RtpExtension& extension1,
165 const webrtc::RtpExtension& extension2) {
166 // Sorting on ID is sufficient, more than one extension per ID is unsupported.
167 return extension1.id > extension2.id;
168}
169
pbos@webrtc.org3c107582014-07-20 15:27:35 +0000170static std::vector<webrtc::RtpExtension> FilterRtpExtensions(
171 const std::vector<RtpHeaderExtension>& extensions) {
172 std::vector<webrtc::RtpExtension> webrtc_extensions;
173 for (size_t i = 0; i < extensions.size(); ++i) {
174 // Unsupported extensions will be ignored.
175 if (webrtc::RtpExtension::IsSupported(extensions[i].uri)) {
176 webrtc_extensions.push_back(webrtc::RtpExtension(
177 extensions[i].uri, extensions[i].id));
178 } else {
179 LOG(LS_WARNING) << "Unsupported RTP extension: " << extensions[i].uri;
180 }
181 }
pbos@webrtc.orgc37e72e2015-01-05 18:51:13 +0000182
183 // Sort filtered headers to make sure that they can later be compared
184 // regardless of in which order they were entered.
185 std::sort(webrtc_extensions.begin(), webrtc_extensions.end(),
186 CompareRtpHeaderExtensionIds);
pbos@webrtc.org3c107582014-07-20 15:27:35 +0000187 return webrtc_extensions;
188}
189
pbos@webrtc.orgc37e72e2015-01-05 18:51:13 +0000190static bool RtpExtensionsHaveChanged(
191 const std::vector<webrtc::RtpExtension>& before,
192 const std::vector<webrtc::RtpExtension>& after) {
193 if (before.size() != after.size())
194 return true;
195 for (size_t i = 0; i < before.size(); ++i) {
196 if (before[i].id != after[i].id)
197 return true;
198 if (before[i].name != after[i].name)
199 return true;
200 }
201 return false;
202}
203
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000204std::vector<webrtc::VideoStream>
pbos@webrtc.orgf1c8b902015-01-14 17:29:27 +0000205WebRtcVideoChannel2::WebRtcVideoSendStream::CreateSimulcastVideoStreams(
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000206 const VideoCodec& codec,
207 const VideoOptions& options,
208 size_t num_streams) {
209 // Use default factory for non-simulcast.
210 int max_qp = kDefaultQpMax;
211 codec.GetParam(kCodecParamMaxQuantization, &max_qp);
212
213 int min_bitrate_kbps;
214 if (!codec.GetParam(kCodecParamMinBitrate, &min_bitrate_kbps) ||
215 min_bitrate_kbps < kMinVideoBitrate) {
216 min_bitrate_kbps = kMinVideoBitrate;
217 }
218
219 int max_bitrate_kbps;
220 if (!codec.GetParam(kCodecParamMaxBitrate, &max_bitrate_kbps)) {
221 max_bitrate_kbps = 0;
222 }
223
224 return GetSimulcastConfig(
225 num_streams,
226 GetSimulcastBitrateMode(options),
227 codec.width,
228 codec.height,
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000229 max_bitrate_kbps * 1000,
230 max_qp,
231 codec.framerate != 0 ? codec.framerate : kDefaultVideoMaxFramerate);
232}
233
pbos@webrtc.orgf1c8b902015-01-14 17:29:27 +0000234std::vector<webrtc::VideoStream>
235WebRtcVideoChannel2::WebRtcVideoSendStream::CreateVideoStreams(
buildbot@webrtc.orgd41eaeb2014-06-12 07:13:26 +0000236 const VideoCodec& codec,
237 const VideoOptions& options,
238 size_t num_streams) {
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000239 if (num_streams != 1)
240 return CreateSimulcastVideoStreams(codec, options, num_streams);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000241
buildbot@webrtc.orgd41eaeb2014-06-12 07:13:26 +0000242 webrtc::VideoStream stream;
243 stream.width = codec.width;
244 stream.height = codec.height;
245 stream.max_framerate =
andresp@webrtc.org82775b12014-11-07 09:37:54 +0000246 codec.framerate != 0 ? codec.framerate : kDefaultVideoMaxFramerate;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000247
pbos@webrtc.org00873182014-11-25 14:03:34 +0000248 stream.min_bitrate_bps = kMinVideoBitrate * 1000;
249 stream.target_bitrate_bps = stream.max_bitrate_bps = kMaxVideoBitrate * 1000;
buildbot@webrtc.orgd41eaeb2014-06-12 07:13:26 +0000250
buildbot@webrtc.org933d88a2014-09-18 20:23:05 +0000251 int max_qp = kDefaultQpMax;
buildbot@webrtc.orgd41eaeb2014-06-12 07:13:26 +0000252 codec.GetParam(kCodecParamMaxQuantization, &max_qp);
253 stream.max_qp = max_qp;
254 std::vector<webrtc::VideoStream> streams;
255 streams.push_back(stream);
256 return streams;
257}
258
pbos@webrtc.orgf1c8b902015-01-14 17:29:27 +0000259void* WebRtcVideoChannel2::WebRtcVideoSendStream::ConfigureVideoEncoderSettings(
pbos@webrtc.org6f48f1b2014-07-22 16:29:54 +0000260 const VideoCodec& codec,
261 const VideoOptions& options) {
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000262 if (CodecNameMatches(codec.name, kVp8CodecName)) {
pbos@webrtc.orgf1c8b902015-01-14 17:29:27 +0000263 encoder_settings_.vp8 = webrtc::VideoEncoder::GetDefaultVp8Settings();
264 options.video_noise_reduction.Get(&encoder_settings_.vp8.denoisingOn);
265 return &encoder_settings_.vp8;
pbos@webrtc.org6f48f1b2014-07-22 16:29:54 +0000266 }
andresp@webrtc.org188d3b22014-11-07 13:21:04 +0000267 if (CodecNameMatches(codec.name, kVp9CodecName)) {
pbos@webrtc.orgf1c8b902015-01-14 17:29:27 +0000268 encoder_settings_.vp9 = webrtc::VideoEncoder::GetDefaultVp9Settings();
269 options.video_noise_reduction.Get(&encoder_settings_.vp9.denoisingOn);
270 return &encoder_settings_.vp9;
andresp@webrtc.org188d3b22014-11-07 13:21:04 +0000271 }
pbos@webrtc.org6f48f1b2014-07-22 16:29:54 +0000272 return NULL;
273}
274
pbos@webrtc.orgafb554f42014-08-12 23:17:13 +0000275DefaultUnsignalledSsrcHandler::DefaultUnsignalledSsrcHandler()
276 : default_recv_ssrc_(0), default_renderer_(NULL) {}
277
278UnsignalledSsrcHandler::Action DefaultUnsignalledSsrcHandler::OnUnsignalledSsrc(
279 VideoMediaChannel* channel,
280 uint32_t ssrc) {
281 if (default_recv_ssrc_ != 0) { // Already one default stream.
282 LOG(LS_WARNING) << "Unknown SSRC, but default receive stream already set.";
283 return kDropPacket;
284 }
285
286 StreamParams sp;
287 sp.ssrcs.push_back(ssrc);
288 LOG(LS_INFO) << "Creating default receive stream for SSRC=" << ssrc << ".";
289 if (!channel->AddRecvStream(sp)) {
290 LOG(LS_WARNING) << "Could not create default receive stream.";
291 }
292
293 channel->SetRenderer(ssrc, default_renderer_);
294 default_recv_ssrc_ = ssrc;
295 return kDeliverPacket;
296}
297
pbos@webrtc.org42684be2014-10-03 11:25:45 +0000298WebRtcCallFactory::~WebRtcCallFactory() {
299}
300webrtc::Call* WebRtcCallFactory::CreateCall(
301 const webrtc::Call::Config& config) {
302 return webrtc::Call::Create(config);
303}
304
pbos@webrtc.orgafb554f42014-08-12 23:17:13 +0000305VideoRenderer* DefaultUnsignalledSsrcHandler::GetDefaultRenderer() const {
306 return default_renderer_;
307}
308
309void DefaultUnsignalledSsrcHandler::SetDefaultRenderer(
310 VideoMediaChannel* channel,
311 VideoRenderer* renderer) {
312 default_renderer_ = renderer;
313 if (default_recv_ssrc_ != 0) {
314 channel->SetRenderer(default_recv_ssrc_, default_renderer_);
315 }
316}
317
pbos@webrtc.org97fdeb82014-08-22 10:36:23 +0000318WebRtcVideoEngine2::WebRtcVideoEngine2()
pbos@webrtc.orgb648b9d2014-08-26 11:08:06 +0000319 : worker_thread_(NULL),
320 voice_engine_(NULL),
andresp@webrtc.org82775b12014-11-07 09:37:54 +0000321 default_codec_format_(kDefaultVideoMaxWidth,
322 kDefaultVideoMaxHeight,
323 FPS_TO_INTERVAL(kDefaultVideoMaxFramerate),
buildbot@webrtc.org992febb2014-09-05 16:39:08 +0000324 FOURCC_ANY),
pbos@webrtc.orgb648b9d2014-08-26 11:08:06 +0000325 initialized_(false),
pbos@webrtc.org42684be2014-10-03 11:25:45 +0000326 call_factory_(&default_call_factory_),
pbos@webrtc.org0a2087a2014-09-23 09:40:22 +0000327 external_decoder_factory_(NULL),
328 external_encoder_factory_(NULL) {
pbos@webrtc.orgb648b9d2014-08-26 11:08:06 +0000329 LOG(LS_INFO) << "WebRtcVideoEngine2::WebRtcVideoEngine2()";
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000330 video_codecs_ = GetSupportedCodecs();
pbos@webrtc.org587ef602014-06-16 17:32:02 +0000331 rtp_header_extensions_.push_back(
332 RtpHeaderExtension(kRtpTimestampOffsetHeaderExtension,
333 kRtpTimestampOffsetHeaderExtensionDefaultId));
334 rtp_header_extensions_.push_back(
335 RtpHeaderExtension(kRtpAbsoluteSenderTimeHeaderExtension,
336 kRtpAbsoluteSenderTimeHeaderExtensionDefaultId));
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000337}
338
339WebRtcVideoEngine2::~WebRtcVideoEngine2() {
340 LOG(LS_INFO) << "WebRtcVideoEngine2::~WebRtcVideoEngine2";
341
342 if (initialized_) {
343 Terminate();
344 }
345}
346
pbos@webrtc.org42684be2014-10-03 11:25:45 +0000347void WebRtcVideoEngine2::SetCallFactory(WebRtcCallFactory* call_factory) {
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000348 assert(!initialized_);
pbos@webrtc.org42684be2014-10-03 11:25:45 +0000349 call_factory_ = call_factory;
350}
351
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000352bool WebRtcVideoEngine2::Init(rtc::Thread* worker_thread) {
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000353 LOG(LS_INFO) << "WebRtcVideoEngine2::Init";
354 worker_thread_ = worker_thread;
355 ASSERT(worker_thread_ != NULL);
356
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000357 initialized_ = true;
358 return true;
359}
360
361void WebRtcVideoEngine2::Terminate() {
362 LOG(LS_INFO) << "WebRtcVideoEngine2::Terminate";
363
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000364 initialized_ = false;
365}
366
367int WebRtcVideoEngine2::GetCapabilities() { return VIDEO_RECV | VIDEO_SEND; }
368
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000369bool WebRtcVideoEngine2::SetDefaultEncoderConfig(
370 const VideoEncoderConfig& config) {
pbos@webrtc.org8fdeee62014-07-20 14:40:23 +0000371 const VideoCodec& codec = config.max_codec;
pbos@webrtc.org957e8022014-11-10 12:36:11 +0000372 bool supports_codec = false;
373 for (size_t i = 0; i < video_codecs_.size(); ++i) {
374 if (CodecNameMatches(video_codecs_[i].name, codec.name)) {
375 video_codecs_[i] = codec;
376 supports_codec = true;
377 break;
378 }
379 }
380
381 if (!supports_codec) {
382 LOG(LS_ERROR) << "SetDefaultEncoderConfig, codec not supported: "
pbos@webrtc.org8fdeee62014-07-20 14:40:23 +0000383 << codec.ToString();
384 return false;
385 }
386
buildbot@webrtc.org992febb2014-09-05 16:39:08 +0000387 default_codec_format_ =
388 VideoFormat(codec.width,
389 codec.height,
390 VideoFormat::FpsToInterval(codec.framerate),
391 FOURCC_ANY);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000392 return true;
393}
394
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000395WebRtcVideoChannel2* WebRtcVideoEngine2::CreateChannel(
buildbot@webrtc.org1ecbe452014-10-14 20:29:28 +0000396 const VideoOptions& options,
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000397 VoiceMediaChannel* voice_channel) {
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000398 assert(initialized_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000399 LOG(LS_INFO) << "CreateChannel: "
400 << (voice_channel != NULL ? "With" : "Without")
pbos@webrtc.orgfa553ef2014-10-20 11:07:07 +0000401 << " voice channel. Options: " << options.ToString();
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000402 WebRtcVideoChannel2* channel =
403 new WebRtcVideoChannel2(call_factory_,
pbos@webrtc.org3bf3d232014-10-31 12:59:34 +0000404 voice_engine_,
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000405 voice_channel,
pbos@webrtc.orgfa553ef2014-10-20 11:07:07 +0000406 options,
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000407 external_encoder_factory_,
pbos@webrtc.orgf1c8b902015-01-14 17:29:27 +0000408 external_decoder_factory_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000409 if (!channel->Init()) {
410 delete channel;
411 return NULL;
412 }
pbos@webrtc.orge322a172014-06-13 11:47:28 +0000413 channel->SetRecvCodecs(video_codecs_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000414 return channel;
415}
416
417const std::vector<VideoCodec>& WebRtcVideoEngine2::codecs() const {
418 return video_codecs_;
419}
420
421const std::vector<RtpHeaderExtension>&
422WebRtcVideoEngine2::rtp_header_extensions() const {
423 return rtp_header_extensions_;
424}
425
426void WebRtcVideoEngine2::SetLogging(int min_sev, const char* filter) {
427 // TODO(pbos): Set up logging.
428 LOG(LS_VERBOSE) << "SetLogging: " << min_sev << '"' << filter << '"';
429 // if min_sev == -1, we keep the current log level.
430 if (min_sev < 0) {
431 assert(min_sev == -1);
432 return;
433 }
434}
435
pbos@webrtc.org0a2087a2014-09-23 09:40:22 +0000436void WebRtcVideoEngine2::SetExternalDecoderFactory(
437 WebRtcVideoDecoderFactory* decoder_factory) {
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000438 assert(!initialized_);
pbos@webrtc.org0a2087a2014-09-23 09:40:22 +0000439 external_decoder_factory_ = decoder_factory;
440}
441
442void WebRtcVideoEngine2::SetExternalEncoderFactory(
443 WebRtcVideoEncoderFactory* encoder_factory) {
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000444 assert(!initialized_);
pbos@webrtc.orgf18fba22015-01-14 16:26:23 +0000445 if (external_encoder_factory_ == encoder_factory)
446 return;
447
448 // No matter what happens we shouldn't hold on to a stale
449 // WebRtcSimulcastEncoderFactory.
450 simulcast_encoder_factory_.reset();
451
452 if (encoder_factory &&
453 WebRtcSimulcastEncoderFactory::UseSimulcastEncoderFactory(
454 encoder_factory->codecs())) {
455 simulcast_encoder_factory_.reset(
456 new WebRtcSimulcastEncoderFactory(encoder_factory));
457 encoder_factory = simulcast_encoder_factory_.get();
458 }
pbos@webrtc.org0a2087a2014-09-23 09:40:22 +0000459 external_encoder_factory_ = encoder_factory;
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000460
461 video_codecs_ = GetSupportedCodecs();
pbos@webrtc.org0a2087a2014-09-23 09:40:22 +0000462}
463
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000464bool WebRtcVideoEngine2::EnableTimedRender() {
465 // TODO(pbos): Figure out whether this can be removed.
466 return true;
467}
468
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000469// Checks to see whether we comprehend and could receive a particular codec
470bool WebRtcVideoEngine2::FindCodec(const VideoCodec& in) {
471 // TODO(pbos): Probe encoder factory to figure out that the codec is supported
472 // if supported by the encoder factory. Add a corresponding test that fails
473 // with this code (that doesn't ask the factory).
pbos@webrtc.org8fdeee62014-07-20 14:40:23 +0000474 for (size_t j = 0; j < video_codecs_.size(); ++j) {
475 VideoCodec codec(video_codecs_[j].id, video_codecs_[j].name, 0, 0, 0, 0);
476 if (codec.Matches(in)) {
477 return true;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000478 }
479 }
480 return false;
481}
482
483// Tells whether the |requested| codec can be transmitted or not. If it can be
484// transmitted |out| is set with the best settings supported. Aspect ratio will
485// be set as close to |current|'s as possible. If not set |requested|'s
486// dimensions will be used for aspect ratio matching.
487bool WebRtcVideoEngine2::CanSendCodec(const VideoCodec& requested,
488 const VideoCodec& current,
489 VideoCodec* out) {
490 assert(out != NULL);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000491
492 if (requested.width != requested.height &&
493 (requested.height == 0 || requested.width == 0)) {
494 // 0xn and nx0 are invalid resolutions.
495 return false;
496 }
497
498 VideoCodec matching_codec;
499 if (!FindFirstMatchingCodec(video_codecs_, requested, &matching_codec)) {
500 // Codec not supported.
501 return false;
502 }
503
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000504 out->id = requested.id;
505 out->name = requested.name;
506 out->preference = requested.preference;
507 out->params = requested.params;
andresp@webrtc.orgff689be2015-02-12 11:54:26 +0000508 out->framerate = std::min(requested.framerate, matching_codec.framerate);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000509 out->params = requested.params;
510 out->feedback_params = requested.feedback_params;
pbos@webrtc.org8fdeee62014-07-20 14:40:23 +0000511 out->width = requested.width;
512 out->height = requested.height;
513 if (requested.width == 0 && requested.height == 0) {
514 return true;
515 }
516
517 while (out->width > matching_codec.width) {
518 out->width /= 2;
519 out->height /= 2;
520 }
521
522 return out->width > 0 && out->height > 0;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000523}
524
525bool WebRtcVideoEngine2::SetVoiceEngine(WebRtcVoiceEngine* voice_engine) {
526 if (initialized_) {
527 LOG(LS_WARNING) << "SetVoiceEngine can not be called after Init";
528 return false;
529 }
530 voice_engine_ = voice_engine;
531 return true;
532}
533
534// Ignore spammy trace messages, mostly from the stats API when we haven't
535// gotten RTCP info yet from the remote side.
536bool WebRtcVideoEngine2::ShouldIgnoreTrace(const std::string& trace) {
537 static const char* const kTracesToIgnore[] = {NULL};
538 for (const char* const* p = kTracesToIgnore; *p; ++p) {
539 if (trace.find(*p) == 0) {
540 return true;
541 }
542 }
543 return false;
544}
545
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000546std::vector<VideoCodec> WebRtcVideoEngine2::GetSupportedCodecs() const {
andresp@webrtc.org82775b12014-11-07 09:37:54 +0000547 std::vector<VideoCodec> supported_codecs = DefaultVideoCodecList();
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000548
549 if (external_encoder_factory_ == NULL) {
550 return supported_codecs;
551 }
552
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000553 const std::vector<WebRtcVideoEncoderFactory::VideoCodec>& codecs =
554 external_encoder_factory_->codecs();
555 for (size_t i = 0; i < codecs.size(); ++i) {
556 // Don't add internally-supported codecs twice.
557 if (CodecIsInternallySupported(codecs[i].name)) {
558 continue;
559 }
560
pkasting@chromium.orgd3245462015-02-23 21:28:22 +0000561 // External video encoders are given payloads 120-127. This also means that
562 // we only support up to 8 external payload types.
563 const int kExternalVideoPayloadTypeBase = 120;
564 size_t payload_type = kExternalVideoPayloadTypeBase + i;
565 assert(payload_type < 128);
566 VideoCodec codec(static_cast<int>(payload_type),
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000567 codecs[i].name,
568 codecs[i].max_width,
569 codecs[i].max_height,
570 codecs[i].max_fps,
571 0);
572
573 AddDefaultFeedbackParams(&codec);
574 supported_codecs.push_back(codec);
575 }
576 return supported_codecs;
577}
578
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000579WebRtcVideoChannel2::WebRtcVideoChannel2(
pbos@webrtc.org42684be2014-10-03 11:25:45 +0000580 WebRtcCallFactory* call_factory,
pbos@webrtc.org3bf3d232014-10-31 12:59:34 +0000581 WebRtcVoiceEngine* voice_engine,
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000582 VoiceMediaChannel* voice_channel,
pbos@webrtc.orgfa553ef2014-10-20 11:07:07 +0000583 const VideoOptions& options,
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000584 WebRtcVideoEncoderFactory* external_encoder_factory,
pbos@webrtc.orgf1c8b902015-01-14 17:29:27 +0000585 WebRtcVideoDecoderFactory* external_decoder_factory)
pbos@webrtc.orgb648b9d2014-08-26 11:08:06 +0000586 : unsignalled_ssrc_handler_(&default_unsignalled_ssrc_handler_),
pbos@webrtc.org3bf3d232014-10-31 12:59:34 +0000587 voice_channel_(voice_channel),
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000588 external_encoder_factory_(external_encoder_factory),
pbos@webrtc.orgf1c8b902015-01-14 17:29:27 +0000589 external_decoder_factory_(external_decoder_factory) {
pbos@webrtc.orgfa553ef2014-10-20 11:07:07 +0000590 SetDefaultOptions();
591 options_.SetAll(options);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000592 webrtc::Call::Config config(this);
pbos@webrtc.org42684be2014-10-03 11:25:45 +0000593 config.overuse_callback = this;
pbos@webrtc.org3bf3d232014-10-31 12:59:34 +0000594 if (voice_engine != NULL) {
595 config.voice_engine = voice_engine->voe()->engine();
596 }
pbos@webrtc.orgfa553ef2014-10-20 11:07:07 +0000597
pbos@webrtc.org42684be2014-10-03 11:25:45 +0000598 call_.reset(call_factory->CreateCall(config));
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000599
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000600 rtcp_receiver_report_ssrc_ = kDefaultRtcpReceiverReportSsrc;
601 sending_ = false;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000602 default_send_ssrc_ = 0;
pbos@webrtc.org6f48f1b2014-07-22 16:29:54 +0000603}
604
605void WebRtcVideoChannel2::SetDefaultOptions() {
pbos@webrtc.org42684be2014-10-03 11:25:45 +0000606 options_.cpu_overuse_detection.Set(false);
pbos@webrtc.orgd8198032014-11-10 14:41:43 +0000607 options_.dscp.Set(false);
pbos@webrtc.org5ff71ab2014-07-23 07:28:56 +0000608 options_.suspend_below_min_bitrate.Set(false);
pbos@webrtc.org42684be2014-10-03 11:25:45 +0000609 options_.video_noise_reduction.Set(true);
pbos@webrtc.orgefc82c22014-10-27 13:58:00 +0000610 options_.screencast_min_bitrate.Set(0);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000611}
612
613WebRtcVideoChannel2::~WebRtcVideoChannel2() {
614 for (std::map<uint32, WebRtcVideoSendStream*>::iterator it =
615 send_streams_.begin();
616 it != send_streams_.end();
617 ++it) {
618 delete it->second;
619 }
620
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000621 for (std::map<uint32, WebRtcVideoReceiveStream*>::iterator it =
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000622 receive_streams_.begin();
623 it != receive_streams_.end();
624 ++it) {
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000625 delete it->second;
626 }
627}
628
629bool WebRtcVideoChannel2::Init() { return true; }
630
pbos@webrtc.org96a93252014-11-03 14:46:44 +0000631bool WebRtcVideoChannel2::CodecIsExternallySupported(
632 const std::string& name) const {
633 if (external_encoder_factory_ == NULL) {
634 return false;
635 }
636
637 const std::vector<WebRtcVideoEncoderFactory::VideoCodec> external_codecs =
638 external_encoder_factory_->codecs();
639 for (size_t c = 0; c < external_codecs.size(); ++c) {
640 if (CodecNameMatches(name, external_codecs[c].name)) {
641 return true;
642 }
643 }
644 return false;
645}
646
647std::vector<WebRtcVideoChannel2::VideoCodecSettings>
648WebRtcVideoChannel2::FilterSupportedCodecs(
649 const std::vector<WebRtcVideoChannel2::VideoCodecSettings>& mapped_codecs)
650 const {
651 std::vector<VideoCodecSettings> supported_codecs;
652 for (size_t i = 0; i < mapped_codecs.size(); ++i) {
653 const VideoCodecSettings& codec = mapped_codecs[i];
654 if (CodecIsInternallySupported(codec.codec.name) ||
655 CodecIsExternallySupported(codec.codec.name)) {
656 supported_codecs.push_back(codec);
657 }
658 }
659 return supported_codecs;
660}
661
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000662bool WebRtcVideoChannel2::SetRecvCodecs(const std::vector<VideoCodec>& codecs) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000663 TRACE_EVENT0("webrtc", "WebRtcVideoChannel2::SetRecvCodecs");
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000664 LOG(LS_INFO) << "SetRecvCodecs: " << CodecVectorToString(codecs);
665 if (!ValidateCodecFormats(codecs)) {
666 return false;
667 }
668
669 const std::vector<VideoCodecSettings> mapped_codecs = MapCodecs(codecs);
670 if (mapped_codecs.empty()) {
pbos@webrtc.org96a93252014-11-03 14:46:44 +0000671 LOG(LS_ERROR) << "SetRecvCodecs called without any video codecs.";
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000672 return false;
673 }
674
pbos@webrtc.org96a93252014-11-03 14:46:44 +0000675 const std::vector<VideoCodecSettings> supported_codecs =
676 FilterSupportedCodecs(mapped_codecs);
677
678 if (mapped_codecs.size() != supported_codecs.size()) {
679 LOG(LS_ERROR) << "SetRecvCodecs called with unsupported video codecs.";
680 return false;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000681 }
682
pbos@webrtc.org96a93252014-11-03 14:46:44 +0000683 recv_codecs_ = supported_codecs;
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000684
pbos@webrtc.org575d1262014-10-08 14:48:08 +0000685 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000686 for (std::map<uint32, WebRtcVideoReceiveStream*>::iterator it =
687 receive_streams_.begin();
688 it != receive_streams_.end();
689 ++it) {
690 it->second->SetRecvCodecs(recv_codecs_);
691 }
692
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000693 return true;
694}
695
696bool WebRtcVideoChannel2::SetSendCodecs(const std::vector<VideoCodec>& codecs) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000697 TRACE_EVENT0("webrtc", "WebRtcVideoChannel2::SetSendCodecs");
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000698 LOG(LS_INFO) << "SetSendCodecs: " << CodecVectorToString(codecs);
699 if (!ValidateCodecFormats(codecs)) {
700 return false;
701 }
702
703 const std::vector<VideoCodecSettings> supported_codecs =
704 FilterSupportedCodecs(MapCodecs(codecs));
705
706 if (supported_codecs.empty()) {
707 LOG(LS_ERROR) << "No video codecs supported by encoder factory.";
708 return false;
709 }
710
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000711 LOG(LS_INFO) << "Using codec: " << supported_codecs.front().codec.ToString();
712
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +0000713 VideoCodecSettings old_codec;
714 if (send_codec_.Get(&old_codec) && supported_codecs.front() == old_codec) {
715 // Using same codec, avoid reconfiguring.
716 return true;
717 }
718
719 send_codec_.Set(supported_codecs.front());
720
pbos@webrtc.org575d1262014-10-08 14:48:08 +0000721 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000722 for (std::map<uint32, WebRtcVideoSendStream*>::iterator it =
723 send_streams_.begin();
724 it != send_streams_.end();
725 ++it) {
726 assert(it->second != NULL);
727 it->second->SetCodec(supported_codecs.front());
728 }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000729
pbos@webrtc.org00873182014-11-25 14:03:34 +0000730 VideoCodec codec = supported_codecs.front().codec;
731 int bitrate_kbps;
732 if (codec.GetParam(kCodecParamMinBitrate, &bitrate_kbps) &&
733 bitrate_kbps > 0) {
734 bitrate_config_.min_bitrate_bps = bitrate_kbps * 1000;
735 } else {
736 bitrate_config_.min_bitrate_bps = 0;
737 }
738 if (codec.GetParam(kCodecParamStartBitrate, &bitrate_kbps) &&
739 bitrate_kbps > 0) {
740 bitrate_config_.start_bitrate_bps = bitrate_kbps * 1000;
741 } else {
742 // Do not reconfigure start bitrate unless it's specified and positive.
743 bitrate_config_.start_bitrate_bps = -1;
744 }
745 if (codec.GetParam(kCodecParamMaxBitrate, &bitrate_kbps) &&
746 bitrate_kbps > 0) {
747 bitrate_config_.max_bitrate_bps = bitrate_kbps * 1000;
748 } else {
749 bitrate_config_.max_bitrate_bps = -1;
750 }
751 call_->SetBitrateConfig(bitrate_config_);
752
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000753 return true;
754}
755
756bool WebRtcVideoChannel2::GetSendCodec(VideoCodec* codec) {
757 VideoCodecSettings codec_settings;
758 if (!send_codec_.Get(&codec_settings)) {
759 LOG(LS_VERBOSE) << "GetSendCodec: No send codec set.";
760 return false;
761 }
762 *codec = codec_settings.codec;
763 return true;
764}
765
766bool WebRtcVideoChannel2::SetSendStreamFormat(uint32 ssrc,
767 const VideoFormat& format) {
768 LOG(LS_VERBOSE) << "SetSendStreamFormat:" << ssrc << " -> "
769 << format.ToString();
pbos@webrtc.org575d1262014-10-08 14:48:08 +0000770 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000771 if (send_streams_.find(ssrc) == send_streams_.end()) {
772 return false;
773 }
774 return send_streams_[ssrc]->SetVideoFormat(format);
775}
776
777bool WebRtcVideoChannel2::SetRender(bool render) {
778 // TODO(pbos): Implement. Or refactor away as it shouldn't be needed.
779 LOG(LS_VERBOSE) << "SetRender: " << (render ? "true" : "false");
780 return true;
781}
782
783bool WebRtcVideoChannel2::SetSend(bool send) {
784 LOG(LS_VERBOSE) << "SetSend: " << (send ? "true" : "false");
785 if (send && !send_codec_.IsSet()) {
786 LOG(LS_ERROR) << "SetSend(true) called before setting codec.";
787 return false;
788 }
789 if (send) {
790 StartAllSendStreams();
791 } else {
792 StopAllSendStreams();
793 }
794 sending_ = send;
795 return true;
796}
797
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000798bool WebRtcVideoChannel2::AddSendStream(const StreamParams& sp) {
799 LOG(LS_INFO) << "AddSendStream: " << sp.ToString();
800 if (sp.ssrcs.empty()) {
801 LOG(LS_ERROR) << "No SSRCs in stream parameters.";
802 return false;
803 }
804
805 uint32 ssrc = sp.first_ssrc();
806 assert(ssrc != 0);
807 // TODO(pbos): Make sure none of sp.ssrcs are used, not just the identifying
808 // ssrc.
pbos@webrtc.org575d1262014-10-08 14:48:08 +0000809 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000810 if (send_streams_.find(ssrc) != send_streams_.end()) {
811 LOG(LS_ERROR) << "Send stream with ssrc '" << ssrc << "' already exists.";
812 return false;
813 }
814
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +0000815 std::vector<uint32> primary_ssrcs;
816 sp.GetPrimarySsrcs(&primary_ssrcs);
817 std::vector<uint32> rtx_ssrcs;
818 sp.GetFidSsrcs(primary_ssrcs, &rtx_ssrcs);
819 if (!rtx_ssrcs.empty() && primary_ssrcs.size() != rtx_ssrcs.size()) {
820 LOG(LS_ERROR)
821 << "RTX SSRCs exist, but don't cover all SSRCs (unsupported): "
822 << sp.ToString();
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000823 return false;
824 }
825
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000826 WebRtcVideoSendStream* stream =
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000827 new WebRtcVideoSendStream(call_.get(),
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000828 external_encoder_factory_,
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000829 options_,
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +0000830 send_codec_,
831 sp,
832 send_rtp_extensions_);
833
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000834 send_streams_[ssrc] = stream;
835
836 if (rtcp_receiver_report_ssrc_ == kDefaultRtcpReceiverReportSsrc) {
837 rtcp_receiver_report_ssrc_ = ssrc;
838 }
839 if (default_send_ssrc_ == 0) {
840 default_send_ssrc_ = ssrc;
841 }
842 if (sending_) {
843 stream->Start();
844 }
845
846 return true;
847}
848
849bool WebRtcVideoChannel2::RemoveSendStream(uint32 ssrc) {
850 LOG(LS_INFO) << "RemoveSendStream: " << ssrc;
851
852 if (ssrc == 0) {
853 if (default_send_ssrc_ == 0) {
854 LOG(LS_ERROR) << "No default send stream active.";
855 return false;
856 }
857
858 LOG(LS_VERBOSE) << "Removing default stream: " << default_send_ssrc_;
859 ssrc = default_send_ssrc_;
860 }
861
pbos@webrtc.org575d1262014-10-08 14:48:08 +0000862 WebRtcVideoSendStream* removed_stream;
863 {
864 rtc::CritScope stream_lock(&stream_crit_);
865 std::map<uint32, WebRtcVideoSendStream*>::iterator it =
866 send_streams_.find(ssrc);
867 if (it == send_streams_.end()) {
868 return false;
869 }
870
871 removed_stream = it->second;
872 send_streams_.erase(it);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000873 }
874
pbos@webrtc.org575d1262014-10-08 14:48:08 +0000875 delete removed_stream;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000876
877 if (ssrc == default_send_ssrc_) {
878 default_send_ssrc_ = 0;
879 }
880
881 return true;
882}
883
884bool WebRtcVideoChannel2::AddRecvStream(const StreamParams& sp) {
885 LOG(LS_INFO) << "AddRecvStream: " << sp.ToString();
886 assert(sp.ssrcs.size() > 0);
887
888 uint32 ssrc = sp.first_ssrc();
889 assert(ssrc != 0); // TODO(pbos): Is this ever valid?
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000890
891 // TODO(pbos): Check if any of the SSRCs overlap.
pbos@webrtc.org575d1262014-10-08 14:48:08 +0000892 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000893 if (receive_streams_.find(ssrc) != receive_streams_.end()) {
894 LOG(LS_ERROR) << "Receive stream for SSRC " << ssrc << "already exists.";
895 return false;
896 }
897
pbos@webrtc.orgbd249bc2014-07-07 04:45:15 +0000898 webrtc::VideoReceiveStream::Config config;
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000899 ConfigureReceiverRtp(&config, sp);
pbos@webrtc.org3bf3d232014-10-31 12:59:34 +0000900
901 // Set up A/V sync if there is a VoiceChannel.
902 // TODO(pbos): The A/V is synched by the receiving channel. So we need to know
903 // the SSRC of the remote audio channel in order to sync the correct webrtc
904 // VoiceEngine channel. For now sync the first channel in non-conference to
905 // match existing behavior in WebRtcVideoEngine.
906 if (voice_channel_ != NULL && receive_streams_.empty() &&
907 !options_.conference_mode.GetWithDefaultIfUnset(false)) {
908 config.audio_channel_id =
909 static_cast<WebRtcVoiceMediaChannel*>(voice_channel_)->voe_channel();
910 }
911
pbos@webrtc.org776e6f22014-10-29 15:28:39 +0000912 receive_streams_[ssrc] = new WebRtcVideoReceiveStream(
913 call_.get(), external_decoder_factory_, config, recv_codecs_);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000914
915 return true;
916}
917
918void WebRtcVideoChannel2::ConfigureReceiverRtp(
919 webrtc::VideoReceiveStream::Config* config,
920 const StreamParams& sp) const {
921 uint32 ssrc = sp.first_ssrc();
922
923 config->rtp.remote_ssrc = ssrc;
924 config->rtp.local_ssrc = rtcp_receiver_report_ssrc_;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000925
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000926 config->rtp.extensions = recv_rtp_extensions_;
pbos@webrtc.org257e1302014-07-25 19:01:32 +0000927
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000928 // TODO(pbos): This protection is against setting the same local ssrc as
929 // remote which is not permitted by the lower-level API. RTCP requires a
930 // corresponding sender SSRC. Figure out what to do when we don't have
931 // (receive-only) or know a good local SSRC.
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000932 if (config->rtp.remote_ssrc == config->rtp.local_ssrc) {
933 if (config->rtp.local_ssrc != kDefaultRtcpReceiverReportSsrc) {
934 config->rtp.local_ssrc = kDefaultRtcpReceiverReportSsrc;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000935 } else {
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000936 config->rtp.local_ssrc = kDefaultRtcpReceiverReportSsrc + 1;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000937 }
938 }
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000939
940 for (size_t i = 0; i < recv_codecs_.size(); ++i) {
andresp@webrtc.org82775b12014-11-07 09:37:54 +0000941 MergeFecConfig(recv_codecs_[i].fec, &config->rtp.fec);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000942 }
943
andresp@webrtc.org82775b12014-11-07 09:37:54 +0000944 for (size_t i = 0; i < recv_codecs_.size(); ++i) {
945 uint32 rtx_ssrc;
946 if (recv_codecs_[i].rtx_payload_type != -1 &&
947 sp.GetFidSsrc(ssrc, &rtx_ssrc)) {
948 webrtc::VideoReceiveStream::Config::Rtp::Rtx& rtx =
949 config->rtp.rtx[recv_codecs_[i].codec.id];
950 rtx.ssrc = rtx_ssrc;
951 rtx.payload_type = recv_codecs_[i].rtx_payload_type;
952 }
953 }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000954}
955
956bool WebRtcVideoChannel2::RemoveRecvStream(uint32 ssrc) {
957 LOG(LS_INFO) << "RemoveRecvStream: " << ssrc;
958 if (ssrc == 0) {
pbos@webrtc.orgafb554f42014-08-12 23:17:13 +0000959 LOG(LS_ERROR) << "RemoveRecvStream with 0 ssrc is not supported.";
960 return false;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000961 }
962
pbos@webrtc.org575d1262014-10-08 14:48:08 +0000963 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000964 std::map<uint32, WebRtcVideoReceiveStream*>::iterator stream =
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000965 receive_streams_.find(ssrc);
966 if (stream == receive_streams_.end()) {
967 LOG(LS_ERROR) << "Stream not found for ssrc: " << ssrc;
968 return false;
969 }
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000970 delete stream->second;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000971 receive_streams_.erase(stream);
972
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000973 return true;
974}
975
976bool WebRtcVideoChannel2::SetRenderer(uint32 ssrc, VideoRenderer* renderer) {
977 LOG(LS_INFO) << "SetRenderer: ssrc:" << ssrc << " "
978 << (renderer ? "(ptr)" : "NULL");
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000979 if (ssrc == 0) {
pbos@webrtc.orgafb554f42014-08-12 23:17:13 +0000980 default_unsignalled_ssrc_handler_.SetDefaultRenderer(this, renderer);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000981 return true;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000982 }
983
pbos@webrtc.org575d1262014-10-08 14:48:08 +0000984 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000985 std::map<uint32, WebRtcVideoReceiveStream*>::iterator it =
986 receive_streams_.find(ssrc);
987 if (it == receive_streams_.end()) {
988 return false;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000989 }
990
991 it->second->SetRenderer(renderer);
992 return true;
993}
994
995bool WebRtcVideoChannel2::GetRenderer(uint32 ssrc, VideoRenderer** renderer) {
996 if (ssrc == 0) {
pbos@webrtc.orgafb554f42014-08-12 23:17:13 +0000997 *renderer = default_unsignalled_ssrc_handler_.GetDefaultRenderer();
998 return *renderer != NULL;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000999 }
1000
pbos@webrtc.org575d1262014-10-08 14:48:08 +00001001 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001002 std::map<uint32, WebRtcVideoReceiveStream*>::iterator it =
1003 receive_streams_.find(ssrc);
1004 if (it == receive_streams_.end()) {
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001005 return false;
1006 }
1007 *renderer = it->second->GetRenderer();
1008 return true;
1009}
1010
1011bool WebRtcVideoChannel2::GetStats(const StatsOptions& options,
1012 VideoMediaInfo* info) {
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00001013 info->Clear();
1014 FillSenderStats(info);
1015 FillReceiverStats(info);
pbos@webrtc.org2b19f062014-12-11 13:26:09 +00001016 webrtc::Call::Stats stats = call_->GetStats();
1017 FillBandwidthEstimationStats(stats, info);
1018 if (stats.rtt_ms != -1) {
1019 for (size_t i = 0; i < info->senders.size(); ++i) {
1020 info->senders[i].rtt_ms = stats.rtt_ms;
1021 }
1022 }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001023 return true;
1024}
1025
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00001026void WebRtcVideoChannel2::FillSenderStats(VideoMediaInfo* video_media_info) {
pbos@webrtc.org575d1262014-10-08 14:48:08 +00001027 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00001028 for (std::map<uint32, WebRtcVideoSendStream*>::iterator it =
1029 send_streams_.begin();
1030 it != send_streams_.end();
1031 ++it) {
1032 video_media_info->senders.push_back(it->second->GetVideoSenderInfo());
1033 }
1034}
1035
1036void WebRtcVideoChannel2::FillReceiverStats(VideoMediaInfo* video_media_info) {
pbos@webrtc.org575d1262014-10-08 14:48:08 +00001037 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00001038 for (std::map<uint32, WebRtcVideoReceiveStream*>::iterator it =
1039 receive_streams_.begin();
1040 it != receive_streams_.end();
1041 ++it) {
1042 video_media_info->receivers.push_back(it->second->GetVideoReceiverInfo());
1043 }
1044}
1045
1046void WebRtcVideoChannel2::FillBandwidthEstimationStats(
pbos@webrtc.org2b19f062014-12-11 13:26:09 +00001047 const webrtc::Call::Stats& stats,
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00001048 VideoMediaInfo* video_media_info) {
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001049 BandwidthEstimationInfo bwe_info;
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001050 bwe_info.available_send_bandwidth = stats.send_bandwidth_bps;
1051 bwe_info.available_recv_bandwidth = stats.recv_bandwidth_bps;
1052 bwe_info.bucket_delay = stats.pacer_delay_ms;
1053
1054 // Get send stream bitrate stats.
1055 rtc::CritScope stream_lock(&stream_crit_);
1056 for (std::map<uint32, WebRtcVideoSendStream*>::iterator stream =
1057 send_streams_.begin();
1058 stream != send_streams_.end();
1059 ++stream) {
1060 stream->second->FillBandwidthEstimationInfo(&bwe_info);
1061 }
1062 video_media_info->bw_estimations.push_back(bwe_info);
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00001063}
1064
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001065bool WebRtcVideoChannel2::SetCapturer(uint32 ssrc, VideoCapturer* capturer) {
1066 LOG(LS_INFO) << "SetCapturer: " << ssrc << " -> "
1067 << (capturer != NULL ? "(capturer)" : "NULL");
1068 assert(ssrc != 0);
pbos@webrtc.org575d1262014-10-08 14:48:08 +00001069 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001070 if (send_streams_.find(ssrc) == send_streams_.end()) {
1071 LOG(LS_ERROR) << "No sending stream on ssrc " << ssrc;
1072 return false;
1073 }
1074 return send_streams_[ssrc]->SetCapturer(capturer);
1075}
1076
1077bool WebRtcVideoChannel2::SendIntraFrame() {
1078 // TODO(pbos): Implement.
1079 LOG(LS_VERBOSE) << "SendIntraFrame().";
1080 return true;
1081}
1082
1083bool WebRtcVideoChannel2::RequestIntraFrame() {
1084 // TODO(pbos): Implement.
1085 LOG(LS_VERBOSE) << "SendIntraFrame().";
1086 return true;
1087}
1088
1089void WebRtcVideoChannel2::OnPacketReceived(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001090 rtc::Buffer* packet,
1091 const rtc::PacketTime& packet_time) {
pbos@webrtc.org4e545cc2014-05-14 13:58:13 +00001092 const webrtc::PacketReceiver::DeliveryStatus delivery_result =
1093 call_->Receiver()->DeliverPacket(
1094 reinterpret_cast<const uint8_t*>(packet->data()), packet->length());
1095 switch (delivery_result) {
1096 case webrtc::PacketReceiver::DELIVERY_OK:
1097 return;
1098 case webrtc::PacketReceiver::DELIVERY_PACKET_ERROR:
1099 return;
1100 case webrtc::PacketReceiver::DELIVERY_UNKNOWN_SSRC:
1101 break;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001102 }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001103
1104 uint32 ssrc = 0;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001105 if (!GetRtpSsrc(packet->data(), packet->length(), &ssrc)) {
1106 return;
1107 }
1108
pbos@webrtc.orgafb554f42014-08-12 23:17:13 +00001109 // TODO(pbos): Make sure that the unsignalled SSRC uses the video payload.
1110 // Also figure out whether RTX needs to be handled.
1111 switch (unsignalled_ssrc_handler_->OnUnsignalledSsrc(this, ssrc)) {
1112 case UnsignalledSsrcHandler::kDropPacket:
1113 return;
1114 case UnsignalledSsrcHandler::kDeliverPacket:
1115 break;
1116 }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001117
pbos@webrtc.org1e019d12014-05-16 11:38:45 +00001118 if (call_->Receiver()->DeliverPacket(
1119 reinterpret_cast<const uint8_t*>(packet->data()), packet->length()) !=
1120 webrtc::PacketReceiver::DELIVERY_OK) {
pbos@webrtc.orgafb554f42014-08-12 23:17:13 +00001121 LOG(LS_WARNING) << "Failed to deliver RTP packet on re-delivery.";
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001122 return;
1123 }
1124}
1125
1126void WebRtcVideoChannel2::OnRtcpReceived(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001127 rtc::Buffer* packet,
1128 const rtc::PacketTime& packet_time) {
pbos@webrtc.org1e019d12014-05-16 11:38:45 +00001129 if (call_->Receiver()->DeliverPacket(
1130 reinterpret_cast<const uint8_t*>(packet->data()), packet->length()) !=
1131 webrtc::PacketReceiver::DELIVERY_OK) {
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001132 LOG(LS_WARNING) << "Failed to deliver RTCP packet.";
1133 }
1134}
1135
1136void WebRtcVideoChannel2::OnReadyToSend(bool ready) {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +00001137 LOG(LS_VERBOSE) << "OnReadyToSend: " << (ready ? "Ready." : "Not ready.");
1138 call_->SignalNetworkState(ready ? webrtc::Call::kNetworkUp
1139 : webrtc::Call::kNetworkDown);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001140}
1141
1142bool WebRtcVideoChannel2::MuteStream(uint32 ssrc, bool mute) {
1143 LOG(LS_VERBOSE) << "MuteStream: " << ssrc << " -> "
1144 << (mute ? "mute" : "unmute");
1145 assert(ssrc != 0);
pbos@webrtc.org575d1262014-10-08 14:48:08 +00001146 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001147 if (send_streams_.find(ssrc) == send_streams_.end()) {
1148 LOG(LS_ERROR) << "No sending stream on ssrc " << ssrc;
1149 return false;
1150 }
pbos@webrtc.orgef8bb8d2014-08-13 21:36:18 +00001151
1152 send_streams_[ssrc]->MuteStream(mute);
1153 return true;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001154}
1155
1156bool WebRtcVideoChannel2::SetRecvRtpHeaderExtensions(
1157 const std::vector<RtpHeaderExtension>& extensions) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +00001158 TRACE_EVENT0("webrtc", "WebRtcVideoChannel2::SetRecvRtpHeaderExtensions");
pbos@webrtc.org587ef602014-06-16 17:32:02 +00001159 LOG(LS_INFO) << "SetRecvRtpHeaderExtensions: "
1160 << RtpExtensionsToString(extensions);
pbos@webrtc.org3c107582014-07-20 15:27:35 +00001161 if (!ValidateRtpHeaderExtensionIds(extensions))
1162 return false;
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001163
pbos@webrtc.orgc37e72e2015-01-05 18:51:13 +00001164 std::vector<webrtc::RtpExtension> filtered_extensions =
1165 FilterRtpExtensions(extensions);
1166 if (!RtpExtensionsHaveChanged(recv_rtp_extensions_, filtered_extensions))
1167 return true;
1168
1169 recv_rtp_extensions_ = filtered_extensions;
1170
pbos@webrtc.org575d1262014-10-08 14:48:08 +00001171 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001172 for (std::map<uint32, WebRtcVideoReceiveStream*>::iterator it =
1173 receive_streams_.begin();
1174 it != receive_streams_.end();
1175 ++it) {
1176 it->second->SetRtpExtensions(recv_rtp_extensions_);
1177 }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001178 return true;
1179}
1180
1181bool WebRtcVideoChannel2::SetSendRtpHeaderExtensions(
1182 const std::vector<RtpHeaderExtension>& extensions) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +00001183 TRACE_EVENT0("webrtc", "WebRtcVideoChannel2::SetSendRtpHeaderExtensions");
pbos@webrtc.org587ef602014-06-16 17:32:02 +00001184 LOG(LS_INFO) << "SetSendRtpHeaderExtensions: "
1185 << RtpExtensionsToString(extensions);
pbos@webrtc.org3c107582014-07-20 15:27:35 +00001186 if (!ValidateRtpHeaderExtensionIds(extensions))
1187 return false;
1188
pbos@webrtc.orgc37e72e2015-01-05 18:51:13 +00001189 std::vector<webrtc::RtpExtension> filtered_extensions =
1190 FilterRtpExtensions(extensions);
1191 if (!RtpExtensionsHaveChanged(send_rtp_extensions_, filtered_extensions))
1192 return true;
1193
1194 send_rtp_extensions_ = filtered_extensions;
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +00001195
pbos@webrtc.org575d1262014-10-08 14:48:08 +00001196 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001197 for (std::map<uint32, WebRtcVideoSendStream*>::iterator it =
1198 send_streams_.begin();
1199 it != send_streams_.end();
1200 ++it) {
1201 it->second->SetRtpExtensions(send_rtp_extensions_);
1202 }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001203 return true;
1204}
1205
pbos@webrtc.org00873182014-11-25 14:03:34 +00001206bool WebRtcVideoChannel2::SetMaxSendBandwidth(int max_bitrate_bps) {
1207 LOG(LS_INFO) << "SetMaxSendBandwidth: " << max_bitrate_bps << "bps.";
1208 if (max_bitrate_bps <= 0) {
1209 // Unsetting max bitrate.
1210 max_bitrate_bps = -1;
1211 }
1212 bitrate_config_.start_bitrate_bps = -1;
1213 bitrate_config_.max_bitrate_bps = max_bitrate_bps;
1214 if (max_bitrate_bps > 0 &&
1215 bitrate_config_.min_bitrate_bps > max_bitrate_bps) {
1216 bitrate_config_.min_bitrate_bps = max_bitrate_bps;
1217 }
1218 call_->SetBitrateConfig(bitrate_config_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001219 return true;
1220}
1221
1222bool WebRtcVideoChannel2::SetOptions(const VideoOptions& options) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +00001223 TRACE_EVENT0("webrtc", "WebRtcVideoChannel2::SetOptions");
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +00001224 LOG(LS_INFO) << "SetOptions: " << options.ToString();
1225 VideoOptions old_options = options_;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001226 options_.SetAll(options);
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +00001227 if (options_ == old_options) {
1228 // No new options to set.
1229 return true;
1230 }
pbos@webrtc.orgd8198032014-11-10 14:41:43 +00001231 rtc::DiffServCodePoint dscp = options_.dscp.GetWithDefaultIfUnset(false)
1232 ? rtc::DSCP_AF41
1233 : rtc::DSCP_DEFAULT;
1234 MediaChannel::SetDscp(dscp);
pbos@webrtc.org575d1262014-10-08 14:48:08 +00001235 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001236 for (std::map<uint32, WebRtcVideoSendStream*>::iterator it =
1237 send_streams_.begin();
1238 it != send_streams_.end();
1239 ++it) {
1240 it->second->SetOptions(options_);
1241 }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001242 return true;
1243}
1244
1245void WebRtcVideoChannel2::SetInterface(NetworkInterface* iface) {
1246 MediaChannel::SetInterface(iface);
1247 // Set the RTP recv/send buffer to a bigger size
1248 MediaChannel::SetOption(NetworkInterface::ST_RTP,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001249 rtc::Socket::OPT_RCVBUF,
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001250 kVideoRtpBufferSize);
1251
buildbot@webrtc.orgae694ef2014-10-28 17:37:17 +00001252 // Speculative change to increase the outbound socket buffer size.
1253 // In b/15152257, we are seeing a significant number of packets discarded
1254 // due to lack of socket buffer space, although it's not yet clear what the
1255 // ideal value should be.
1256 MediaChannel::SetOption(NetworkInterface::ST_RTP,
1257 rtc::Socket::OPT_SNDBUF,
1258 kVideoRtpBufferSize);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001259}
1260
1261void WebRtcVideoChannel2::UpdateAspectRatio(int ratio_w, int ratio_h) {
1262 // TODO(pbos): Implement.
1263}
1264
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001265void WebRtcVideoChannel2::OnMessage(rtc::Message* msg) {
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001266 // Ignored.
1267}
1268
pbos@webrtc.org42684be2014-10-03 11:25:45 +00001269void WebRtcVideoChannel2::OnLoadUpdate(Load load) {
pbos@webrtc.org575d1262014-10-08 14:48:08 +00001270 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.org42684be2014-10-03 11:25:45 +00001271 for (std::map<uint32, WebRtcVideoSendStream*>::iterator it =
1272 send_streams_.begin();
1273 it != send_streams_.end();
1274 ++it) {
1275 it->second->OnCpuResolutionRequest(load == kOveruse
1276 ? CoordinatedVideoAdapter::DOWNGRADE
1277 : CoordinatedVideoAdapter::UPGRADE);
1278 }
1279}
1280
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001281bool WebRtcVideoChannel2::SendRtp(const uint8_t* data, size_t len) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001282 rtc::Buffer packet(data, len, kMaxRtpPacketLen);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001283 return MediaChannel::SendPacket(&packet);
1284}
1285
1286bool WebRtcVideoChannel2::SendRtcp(const uint8_t* data, size_t len) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001287 rtc::Buffer packet(data, len, kMaxRtpPacketLen);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001288 return MediaChannel::SendRtcp(&packet);
1289}
1290
1291void WebRtcVideoChannel2::StartAllSendStreams() {
pbos@webrtc.org575d1262014-10-08 14:48:08 +00001292 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001293 for (std::map<uint32, WebRtcVideoSendStream*>::iterator it =
1294 send_streams_.begin();
1295 it != send_streams_.end();
1296 ++it) {
1297 it->second->Start();
1298 }
1299}
1300
1301void WebRtcVideoChannel2::StopAllSendStreams() {
pbos@webrtc.org575d1262014-10-08 14:48:08 +00001302 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001303 for (std::map<uint32, WebRtcVideoSendStream*>::iterator it =
1304 send_streams_.begin();
1305 it != send_streams_.end();
1306 ++it) {
1307 it->second->Stop();
1308 }
1309}
1310
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +00001311WebRtcVideoChannel2::WebRtcVideoSendStream::VideoSendStreamParameters::
1312 VideoSendStreamParameters(
1313 const webrtc::VideoSendStream::Config& config,
1314 const VideoOptions& options,
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001315 const Settable<VideoCodecSettings>& codec_settings)
1316 : config(config), options(options), codec_settings(codec_settings) {
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +00001317}
1318
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001319WebRtcVideoChannel2::WebRtcVideoSendStream::WebRtcVideoSendStream(
1320 webrtc::Call* call,
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +00001321 WebRtcVideoEncoderFactory* external_encoder_factory,
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +00001322 const VideoOptions& options,
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001323 const Settable<VideoCodecSettings>& codec_settings,
1324 const StreamParams& sp,
1325 const std::vector<webrtc::RtpExtension>& rtp_extensions)
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001326 : call_(call),
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +00001327 external_encoder_factory_(external_encoder_factory),
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001328 stream_(NULL),
pbos@webrtc.orgb648b9d2014-08-26 11:08:06 +00001329 parameters_(webrtc::VideoSendStream::Config(), options, codec_settings),
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +00001330 allocated_encoder_(NULL, webrtc::kVideoCodecUnknown, false),
pbos@webrtc.orgb648b9d2014-08-26 11:08:06 +00001331 capturer_(NULL),
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001332 sending_(false),
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001333 muted_(false) {
1334 parameters_.config.rtp.max_packet_size = kVideoMtu;
1335
1336 sp.GetPrimarySsrcs(&parameters_.config.rtp.ssrcs);
1337 sp.GetFidSsrcs(parameters_.config.rtp.ssrcs,
1338 &parameters_.config.rtp.rtx.ssrcs);
1339 parameters_.config.rtp.c_name = sp.cname;
1340 parameters_.config.rtp.extensions = rtp_extensions;
1341
1342 VideoCodecSettings params;
1343 if (codec_settings.Get(&params)) {
1344 SetCodec(params);
1345 }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001346}
1347
1348WebRtcVideoChannel2::WebRtcVideoSendStream::~WebRtcVideoSendStream() {
1349 DisconnectCapturer();
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001350 if (stream_ != NULL) {
1351 call_->DestroyVideoSendStream(stream_);
1352 }
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +00001353 DestroyVideoEncoder(&allocated_encoder_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001354}
1355
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001356static void CreateBlackFrame(webrtc::I420VideoFrame* video_frame,
1357 int width,
1358 int height) {
pbos@webrtc.orgb4987bf2015-02-18 10:13:09 +00001359 video_frame->CreateEmptyFrame(width, height, width, (width + 1) / 2,
1360 (width + 1) / 2);
1361 memset(video_frame->buffer(webrtc::kYPlane), 16,
1362 video_frame->allocated_size(webrtc::kYPlane));
1363 memset(video_frame->buffer(webrtc::kUPlane), 128,
1364 video_frame->allocated_size(webrtc::kUPlane));
1365 memset(video_frame->buffer(webrtc::kVPlane), 128,
1366 video_frame->allocated_size(webrtc::kVPlane));
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001367}
1368
1369static void ConvertToI420VideoFrame(const VideoFrame& frame,
1370 webrtc::I420VideoFrame* i420_frame) {
1371 i420_frame->CreateFrame(
1372 static_cast<int>(frame.GetYPitch() * frame.GetHeight()),
1373 frame.GetYPlane(),
1374 static_cast<int>(frame.GetUPitch() * ((frame.GetHeight() + 1) / 2)),
1375 frame.GetUPlane(),
1376 static_cast<int>(frame.GetVPitch() * ((frame.GetHeight() + 1) / 2)),
1377 frame.GetVPlane(),
1378 static_cast<int>(frame.GetWidth()),
1379 static_cast<int>(frame.GetHeight()),
1380 static_cast<int>(frame.GetYPitch()),
1381 static_cast<int>(frame.GetUPitch()),
1382 static_cast<int>(frame.GetVPitch()));
1383}
1384
1385void WebRtcVideoChannel2::WebRtcVideoSendStream::InputFrame(
1386 VideoCapturer* capturer,
1387 const VideoFrame* frame) {
pbos@webrtc.org86196c42015-02-16 21:02:00 +00001388 TRACE_EVENT0("webrtc", "WebRtcVideoSendStream::InputFrame");
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001389 LOG(LS_VERBOSE) << "InputFrame: " << frame->GetWidth() << "x"
1390 << frame->GetHeight();
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001391 // Lock before copying, can be called concurrently when swapping input source.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001392 rtc::CritScope frame_cs(&frame_lock_);
pbos@webrtc.orgd60d79a2014-09-24 07:10:57 +00001393 ConvertToI420VideoFrame(*frame, &video_frame_);
1394
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001395 rtc::CritScope cs(&lock_);
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001396 if (stream_ == NULL) {
1397 LOG(LS_WARNING) << "Capturer inputting frames before send codecs are "
1398 "configured, dropping.";
1399 return;
1400 }
pbos@webrtc.org86196c42015-02-16 21:02:00 +00001401
1402 // Not sending, abort early to prevent expensive reconfigurations while
1403 // setting up codecs etc.
1404 if (!sending_)
1405 return;
1406
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001407 if (format_.width == 0) { // Dropping frames.
1408 assert(format_.height == 0);
1409 LOG(LS_VERBOSE) << "VideoFormat 0x0 set, Dropping frame.";
1410 return;
1411 }
pbos@webrtc.orgd60d79a2014-09-24 07:10:57 +00001412 if (muted_) {
1413 // Create a black frame to transmit instead.
1414 CreateBlackFrame(&video_frame_,
1415 static_cast<int>(frame->GetWidth()),
1416 static_cast<int>(frame->GetHeight()));
1417 }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001418 // Reconfigure codec if necessary.
pbos@webrtc.orgc4175b92014-09-03 15:25:49 +00001419 SetDimensions(
1420 video_frame_.width(), video_frame_.height(), capturer->IsScreencast());
1421
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001422 LOG(LS_VERBOSE) << "SwapFrame: " << video_frame_.width() << "x"
1423 << video_frame_.height() << " -> (codec) "
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +00001424 << parameters_.encoder_config.streams.back().width << "x"
1425 << parameters_.encoder_config.streams.back().height;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001426 stream_->Input()->SwapFrame(&video_frame_);
1427}
1428
1429bool WebRtcVideoChannel2::WebRtcVideoSendStream::SetCapturer(
1430 VideoCapturer* capturer) {
pbos@webrtc.orgb4987bf2015-02-18 10:13:09 +00001431 TRACE_EVENT0("webrtc", "WebRtcVideoSendStream::SetCapturer");
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001432 if (!DisconnectCapturer() && capturer == NULL) {
1433 return false;
1434 }
1435
1436 {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001437 rtc::CritScope cs(&lock_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001438
pbos@webrtc.org9359cb32014-07-23 15:44:48 +00001439 if (capturer == NULL) {
1440 if (stream_ != NULL) {
1441 LOG(LS_VERBOSE) << "Disabling capturer, sending black frame.";
1442 webrtc::I420VideoFrame black_frame;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001443
pbos@webrtc.orgb4987bf2015-02-18 10:13:09 +00001444 CreateBlackFrame(&black_frame, last_dimensions_.width,
1445 last_dimensions_.height);
pbos@webrtc.org9359cb32014-07-23 15:44:48 +00001446 stream_->Input()->SwapFrame(&black_frame);
1447 }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001448
1449 capturer_ = NULL;
1450 return true;
1451 }
1452
1453 capturer_ = capturer;
1454 }
1455 // Lock cannot be held while connecting the capturer to prevent lock-order
1456 // violations.
1457 capturer->SignalVideoFrame.connect(this, &WebRtcVideoSendStream::InputFrame);
1458 return true;
1459}
1460
1461bool WebRtcVideoChannel2::WebRtcVideoSendStream::SetVideoFormat(
1462 const VideoFormat& format) {
1463 if ((format.width == 0 || format.height == 0) &&
1464 format.width != format.height) {
1465 LOG(LS_ERROR) << "Can't set VideoFormat, width or height is zero (but not "
1466 "both, 0x0 drops frames).";
1467 return false;
1468 }
1469
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001470 rtc::CritScope cs(&lock_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001471 if (format.width == 0 && format.height == 0) {
1472 LOG(LS_INFO)
1473 << "0x0 resolution selected. Captured frames will be dropped for ssrc: "
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +00001474 << parameters_.config.rtp.ssrcs[0] << ".";
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001475 } else {
1476 // TODO(pbos): Fix me, this only affects the last stream!
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +00001477 parameters_.encoder_config.streams.back().max_framerate =
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001478 VideoFormat::IntervalToFps(format.interval);
pbos@webrtc.orgc4175b92014-09-03 15:25:49 +00001479 SetDimensions(format.width, format.height, false);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001480 }
1481
1482 format_ = format;
1483 return true;
1484}
1485
pbos@webrtc.orgef8bb8d2014-08-13 21:36:18 +00001486void WebRtcVideoChannel2::WebRtcVideoSendStream::MuteStream(bool mute) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001487 rtc::CritScope cs(&lock_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001488 muted_ = mute;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001489}
1490
1491bool WebRtcVideoChannel2::WebRtcVideoSendStream::DisconnectCapturer() {
pbos@webrtc.org963b9792014-10-07 14:27:27 +00001492 cricket::VideoCapturer* capturer;
1493 {
1494 rtc::CritScope cs(&lock_);
1495 if (capturer_ == NULL) {
1496 return false;
1497 }
1498 capturer = capturer_;
1499 capturer_ = NULL;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001500 }
pbos@webrtc.org963b9792014-10-07 14:27:27 +00001501 capturer->SignalVideoFrame.disconnect(this);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001502 return true;
1503}
1504
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001505void WebRtcVideoChannel2::WebRtcVideoSendStream::SetOptions(
1506 const VideoOptions& options) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001507 rtc::CritScope cs(&lock_);
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001508 VideoCodecSettings codec_settings;
1509 if (parameters_.codec_settings.Get(&codec_settings)) {
1510 SetCodecAndOptions(codec_settings, options);
1511 } else {
1512 parameters_.options = options;
1513 }
1514}
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +00001515
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001516void WebRtcVideoChannel2::WebRtcVideoSendStream::SetCodec(
1517 const VideoCodecSettings& codec_settings) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001518 rtc::CritScope cs(&lock_);
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001519 SetCodecAndOptions(codec_settings, parameters_.options);
1520}
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +00001521
1522webrtc::VideoCodecType CodecTypeFromName(const std::string& name) {
1523 if (CodecNameMatches(name, kVp8CodecName)) {
1524 return webrtc::kVideoCodecVP8;
andresp@webrtc.org188d3b22014-11-07 13:21:04 +00001525 } else if (CodecNameMatches(name, kVp9CodecName)) {
1526 return webrtc::kVideoCodecVP9;
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +00001527 } else if (CodecNameMatches(name, kH264CodecName)) {
1528 return webrtc::kVideoCodecH264;
1529 }
1530 return webrtc::kVideoCodecUnknown;
1531}
1532
1533WebRtcVideoChannel2::WebRtcVideoSendStream::AllocatedEncoder
1534WebRtcVideoChannel2::WebRtcVideoSendStream::CreateVideoEncoder(
1535 const VideoCodec& codec) {
1536 webrtc::VideoCodecType type = CodecTypeFromName(codec.name);
1537
1538 // Do not re-create encoders of the same type.
1539 if (type == allocated_encoder_.type && allocated_encoder_.encoder != NULL) {
1540 return allocated_encoder_;
1541 }
1542
1543 if (external_encoder_factory_ != NULL) {
1544 webrtc::VideoEncoder* encoder =
1545 external_encoder_factory_->CreateVideoEncoder(type);
1546 if (encoder != NULL) {
1547 return AllocatedEncoder(encoder, type, true);
1548 }
1549 }
1550
1551 if (type == webrtc::kVideoCodecVP8) {
1552 return AllocatedEncoder(
1553 webrtc::VideoEncoder::Create(webrtc::VideoEncoder::kVp8), type, false);
andresp@webrtc.org188d3b22014-11-07 13:21:04 +00001554 } else if (type == webrtc::kVideoCodecVP9) {
1555 return AllocatedEncoder(
1556 webrtc::VideoEncoder::Create(webrtc::VideoEncoder::kVp9), type, false);
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +00001557 }
1558
1559 // This shouldn't happen, we should not be trying to create something we don't
1560 // support.
1561 assert(false);
1562 return AllocatedEncoder(NULL, webrtc::kVideoCodecUnknown, false);
1563}
1564
1565void WebRtcVideoChannel2::WebRtcVideoSendStream::DestroyVideoEncoder(
1566 AllocatedEncoder* encoder) {
1567 if (encoder->external) {
1568 external_encoder_factory_->DestroyVideoEncoder(encoder->encoder);
1569 } else {
1570 delete encoder->encoder;
1571 }
1572}
1573
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001574void WebRtcVideoChannel2::WebRtcVideoSendStream::SetCodecAndOptions(
1575 const VideoCodecSettings& codec_settings,
1576 const VideoOptions& options) {
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +00001577 parameters_.encoder_config =
1578 CreateVideoEncoderConfig(last_dimensions_, codec_settings.codec);
pbos@webrtc.org86196c42015-02-16 21:02:00 +00001579 if (parameters_.encoder_config.streams.empty())
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001580 return;
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +00001581
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001582 format_ = VideoFormat(codec_settings.codec.width,
1583 codec_settings.codec.height,
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001584 VideoFormat::FpsToInterval(30),
1585 FOURCC_I420);
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +00001586
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +00001587 AllocatedEncoder new_encoder = CreateVideoEncoder(codec_settings.codec);
1588 parameters_.config.encoder_settings.encoder = new_encoder.encoder;
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001589 parameters_.config.encoder_settings.payload_name = codec_settings.codec.name;
1590 parameters_.config.encoder_settings.payload_type = codec_settings.codec.id;
1591 parameters_.config.rtp.fec = codec_settings.fec;
1592
1593 // Set RTX payload type if RTX is enabled.
1594 if (!parameters_.config.rtp.rtx.ssrcs.empty()) {
1595 parameters_.config.rtp.rtx.payload_type = codec_settings.rtx_payload_type;
1596 }
1597
1598 if (IsNackEnabled(codec_settings.codec)) {
1599 parameters_.config.rtp.nack.rtp_history_ms = kNackHistoryMs;
1600 }
1601
pbos@webrtc.org5ff71ab2014-07-23 07:28:56 +00001602 options.suspend_below_min_bitrate.Get(
1603 &parameters_.config.suspend_below_min_bitrate);
1604
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001605 parameters_.codec_settings.Set(codec_settings);
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +00001606 parameters_.options = options;
pbos@webrtc.org543e5892014-07-23 07:01:31 +00001607
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001608 RecreateWebRtcStream();
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +00001609 if (allocated_encoder_.encoder != new_encoder.encoder) {
1610 DestroyVideoEncoder(&allocated_encoder_);
1611 allocated_encoder_ = new_encoder;
1612 }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001613}
1614
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001615void WebRtcVideoChannel2::WebRtcVideoSendStream::SetRtpExtensions(
1616 const std::vector<webrtc::RtpExtension>& rtp_extensions) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001617 rtc::CritScope cs(&lock_);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001618 parameters_.config.rtp.extensions = rtp_extensions;
1619 RecreateWebRtcStream();
1620}
1621
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +00001622webrtc::VideoEncoderConfig
1623WebRtcVideoChannel2::WebRtcVideoSendStream::CreateVideoEncoderConfig(
1624 const Dimensions& dimensions,
1625 const VideoCodec& codec) const {
1626 webrtc::VideoEncoderConfig encoder_config;
1627 if (dimensions.is_screencast) {
pbos@webrtc.orgefc82c22014-10-27 13:58:00 +00001628 int screencast_min_bitrate_kbps;
1629 parameters_.options.screencast_min_bitrate.Get(
1630 &screencast_min_bitrate_kbps);
1631 encoder_config.min_transmit_bitrate_bps =
1632 screencast_min_bitrate_kbps * 1000;
1633 encoder_config.content_type = webrtc::VideoEncoderConfig::kScreenshare;
1634 } else {
1635 encoder_config.min_transmit_bitrate_bps = 0;
1636 encoder_config.content_type = webrtc::VideoEncoderConfig::kRealtimeVideo;
1637 }
1638
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +00001639 // Restrict dimensions according to codec max.
1640 int width = dimensions.width;
1641 int height = dimensions.height;
1642 if (!dimensions.is_screencast) {
1643 if (codec.width < width)
1644 width = codec.width;
1645 if (codec.height < height)
1646 height = codec.height;
1647 }
1648
1649 VideoCodec clamped_codec = codec;
1650 clamped_codec.width = width;
1651 clamped_codec.height = height;
pbos@webrtc.orgcddd17c2014-09-16 16:33:13 +00001652
pbos@webrtc.orgf1c8b902015-01-14 17:29:27 +00001653 encoder_config.streams = CreateVideoStreams(
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +00001654 clamped_codec, parameters_.options, parameters_.config.rtp.ssrcs.size());
pbos@webrtc.org6f48f1b2014-07-22 16:29:54 +00001655
pbos@webrtc.orgb7ed7792014-10-31 13:08:10 +00001656 // Conference mode screencast uses 2 temporal layers split at 100kbit.
1657 if (parameters_.options.conference_mode.GetWithDefaultIfUnset(false) &&
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +00001658 dimensions.is_screencast && encoder_config.streams.size() == 1) {
sprang@webrtc.org46d4d292014-12-23 15:19:35 +00001659 ScreenshareLayerConfig config = ScreenshareLayerConfig::GetDefault();
1660
1661 // For screenshare in conference mode, tl0 and tl1 bitrates are piggybacked
1662 // on the VideoCodec struct as target and max bitrates, respectively.
1663 // See eg. webrtc::VP8EncoderImpl::SetRates().
1664 encoder_config.streams[0].target_bitrate_bps =
1665 config.tl0_bitrate_kbps * 1000;
1666 encoder_config.streams[0].max_bitrate_bps = config.tl1_bitrate_kbps * 1000;
pbos@webrtc.orgb7ed7792014-10-31 13:08:10 +00001667 encoder_config.streams[0].temporal_layer_thresholds_bps.clear();
1668 encoder_config.streams[0].temporal_layer_thresholds_bps.push_back(
sprang@webrtc.org46d4d292014-12-23 15:19:35 +00001669 config.tl0_bitrate_kbps * 1000);
pbos@webrtc.orgb7ed7792014-10-31 13:08:10 +00001670 }
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +00001671 return encoder_config;
1672}
1673
1674void WebRtcVideoChannel2::WebRtcVideoSendStream::SetDimensions(
1675 int width,
1676 int height,
1677 bool is_screencast) {
1678 if (last_dimensions_.width == width && last_dimensions_.height == height &&
1679 last_dimensions_.is_screencast == is_screencast) {
1680 // Configured using the same parameters, do not reconfigure.
1681 return;
1682 }
1683 LOG(LS_INFO) << "SetDimensions: " << width << "x" << height
1684 << (is_screencast ? " (screencast)" : " (not screencast)");
1685
1686 last_dimensions_.width = width;
1687 last_dimensions_.height = height;
1688 last_dimensions_.is_screencast = is_screencast;
1689
1690 assert(!parameters_.encoder_config.streams.empty());
1691
1692 VideoCodecSettings codec_settings;
1693 parameters_.codec_settings.Get(&codec_settings);
1694
1695 webrtc::VideoEncoderConfig encoder_config =
1696 CreateVideoEncoderConfig(last_dimensions_, codec_settings.codec);
1697
1698 encoder_config.encoder_specific_settings =
pbos@webrtc.orgf1c8b902015-01-14 17:29:27 +00001699 ConfigureVideoEncoderSettings(codec_settings.codec, parameters_.options);
pbos@webrtc.orgb7ed7792014-10-31 13:08:10 +00001700
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +00001701 bool stream_reconfigured = stream_->ReconfigureVideoEncoder(encoder_config);
1702
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +00001703 encoder_config.encoder_specific_settings = NULL;
pbos@webrtc.org6f48f1b2014-07-22 16:29:54 +00001704
1705 if (!stream_reconfigured) {
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001706 LOG(LS_WARNING) << "Failed to reconfigure video encoder for dimensions: "
1707 << width << "x" << height;
1708 return;
1709 }
pbos@webrtc.orgcddd17c2014-09-16 16:33:13 +00001710
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +00001711 parameters_.encoder_config = encoder_config;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001712}
1713
1714void WebRtcVideoChannel2::WebRtcVideoSendStream::Start() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001715 rtc::CritScope cs(&lock_);
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001716 assert(stream_ != NULL);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001717 stream_->Start();
1718 sending_ = true;
1719}
1720
1721void WebRtcVideoChannel2::WebRtcVideoSendStream::Stop() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001722 rtc::CritScope cs(&lock_);
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001723 if (stream_ != NULL) {
1724 stream_->Stop();
1725 }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001726 sending_ = false;
1727}
1728
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00001729VideoSenderInfo
1730WebRtcVideoChannel2::WebRtcVideoSendStream::GetVideoSenderInfo() {
1731 VideoSenderInfo info;
pbos@webrtc.org77e11bb2015-02-23 16:39:07 +00001732 webrtc::VideoSendStream::Stats stats;
1733 {
1734 rtc::CritScope cs(&lock_);
1735 for (uint32_t ssrc : parameters_.config.rtp.ssrcs)
1736 info.add_ssrc(ssrc);
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00001737
pbos@webrtc.org77e11bb2015-02-23 16:39:07 +00001738 for (size_t i = 0; i < parameters_.encoder_config.streams.size(); ++i) {
1739 if (i == parameters_.encoder_config.streams.size() - 1) {
1740 info.preferred_bitrate +=
1741 parameters_.encoder_config.streams[i].max_bitrate_bps;
1742 } else {
1743 info.preferred_bitrate +=
1744 parameters_.encoder_config.streams[i].target_bitrate_bps;
1745 }
1746 }
pbos@webrtc.orgc3d2bd22014-08-12 20:55:10 +00001747
pbos@webrtc.org77e11bb2015-02-23 16:39:07 +00001748 if (stream_ == NULL)
1749 return info;
1750
1751 stats = stream_->GetStats();
1752
1753 if (capturer_ != NULL && !capturer_->IsMuted()) {
1754 VideoFormat last_captured_frame_format;
1755 capturer_->GetStats(&info.adapt_frame_drops, &info.effects_frame_drops,
1756 &info.capturer_frame_time,
1757 &last_captured_frame_format);
1758 info.input_frame_width = last_captured_frame_format.width;
1759 info.input_frame_height = last_captured_frame_format.height;
1760 }
1761 }
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00001762 info.framerate_input = stats.input_frame_rate;
1763 info.framerate_sent = stats.encode_frame_rate;
1764
pbos@webrtc.org77e11bb2015-02-23 16:39:07 +00001765 info.nominal_bitrate = stats.media_bitrate_bps;
1766
pbos@webrtc.org273a4142014-12-01 15:23:21 +00001767 info.send_frame_width = 0;
1768 info.send_frame_height = 0;
pbos@webrtc.org09c77b92015-02-25 10:42:16 +00001769 for (std::map<uint32_t, webrtc::VideoSendStream::StreamStats>::iterator it =
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00001770 stats.substreams.begin();
pbos@webrtc.org09c77b92015-02-25 10:42:16 +00001771 it != stats.substreams.end(); ++it) {
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00001772 // TODO(pbos): Wire up additional stats, such as padding bytes.
pbos@webrtc.org09c77b92015-02-25 10:42:16 +00001773 webrtc::VideoSendStream::StreamStats stream_stats = it->second;
asapersson@webrtc.orgcfd82df2015-01-22 09:39:59 +00001774 info.bytes_sent += stream_stats.rtp_stats.transmitted.payload_bytes +
1775 stream_stats.rtp_stats.transmitted.header_bytes +
1776 stream_stats.rtp_stats.transmitted.padding_bytes;
1777 info.packets_sent += stream_stats.rtp_stats.transmitted.packets;
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00001778 info.packets_lost += stream_stats.rtcp_stats.cumulative_lost;
pbos@webrtc.org09c77b92015-02-25 10:42:16 +00001779 if (stream_stats.width > info.send_frame_width)
1780 info.send_frame_width = stream_stats.width;
1781 if (stream_stats.height > info.send_frame_height)
1782 info.send_frame_height = stream_stats.height;
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +00001783 info.firs_rcvd += stream_stats.rtcp_packet_type_counts.fir_packets;
1784 info.nacks_rcvd += stream_stats.rtcp_packet_type_counts.nack_packets;
1785 info.plis_rcvd += stream_stats.rtcp_packet_type_counts.pli_packets;
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00001786 }
1787
1788 if (!stats.substreams.empty()) {
1789 // TODO(pbos): Report fraction lost per SSRC.
pbos@webrtc.org09c77b92015-02-25 10:42:16 +00001790 webrtc::VideoSendStream::StreamStats first_stream_stats =
1791 stats.substreams.begin()->second;
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00001792 info.fraction_lost =
1793 static_cast<float>(first_stream_stats.rtcp_stats.fraction_lost) /
1794 (1 << 8);
1795 }
1796
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00001797 return info;
1798}
1799
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001800void WebRtcVideoChannel2::WebRtcVideoSendStream::FillBandwidthEstimationInfo(
1801 BandwidthEstimationInfo* bwe_info) {
1802 rtc::CritScope cs(&lock_);
1803 if (stream_ == NULL) {
1804 return;
1805 }
1806 webrtc::VideoSendStream::Stats stats = stream_->GetStats();
pbos@webrtc.org09c77b92015-02-25 10:42:16 +00001807 for (std::map<uint32_t, webrtc::VideoSendStream::StreamStats>::iterator it =
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001808 stats.substreams.begin();
pbos@webrtc.org09c77b92015-02-25 10:42:16 +00001809 it != stats.substreams.end(); ++it) {
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001810 bwe_info->transmit_bitrate += it->second.total_bitrate_bps;
1811 bwe_info->retransmit_bitrate += it->second.retransmit_bitrate_bps;
1812 }
pbos@webrtc.org77e11bb2015-02-23 16:39:07 +00001813 bwe_info->actual_enc_bitrate += stats.media_bitrate_bps;
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001814}
1815
pbos@webrtc.org42684be2014-10-03 11:25:45 +00001816void WebRtcVideoChannel2::WebRtcVideoSendStream::OnCpuResolutionRequest(
1817 CoordinatedVideoAdapter::AdaptRequest adapt_request) {
1818 rtc::CritScope cs(&lock_);
1819 bool adapt_cpu;
1820 parameters_.options.cpu_overuse_detection.Get(&adapt_cpu);
1821 if (!adapt_cpu) {
1822 return;
1823 }
1824 if (capturer_ == NULL || capturer_->video_adapter() == NULL) {
1825 return;
1826 }
1827
1828 capturer_->video_adapter()->OnCpuResolutionRequest(adapt_request);
1829}
1830
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001831void WebRtcVideoChannel2::WebRtcVideoSendStream::RecreateWebRtcStream() {
1832 if (stream_ != NULL) {
1833 call_->DestroyVideoSendStream(stream_);
1834 }
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +00001835
pbos@webrtc.org6f48f1b2014-07-22 16:29:54 +00001836 VideoCodecSettings codec_settings;
1837 parameters_.codec_settings.Get(&codec_settings);
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +00001838 parameters_.encoder_config.encoder_specific_settings =
pbos@webrtc.orgf1c8b902015-01-14 17:29:27 +00001839 ConfigureVideoEncoderSettings(codec_settings.codec, parameters_.options);
pbos@webrtc.org6f48f1b2014-07-22 16:29:54 +00001840
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +00001841 stream_ = call_->CreateVideoSendStream(parameters_.config,
1842 parameters_.encoder_config);
pbos@webrtc.org6f48f1b2014-07-22 16:29:54 +00001843
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +00001844 parameters_.encoder_config.encoder_specific_settings = NULL;
pbos@webrtc.org6f48f1b2014-07-22 16:29:54 +00001845
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001846 if (sending_) {
1847 stream_->Start();
1848 }
1849}
1850
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001851WebRtcVideoChannel2::WebRtcVideoReceiveStream::WebRtcVideoReceiveStream(
1852 webrtc::Call* call,
pbos@webrtc.org776e6f22014-10-29 15:28:39 +00001853 WebRtcVideoDecoderFactory* external_decoder_factory,
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001854 const webrtc::VideoReceiveStream::Config& config,
1855 const std::vector<VideoCodecSettings>& recv_codecs)
1856 : call_(call),
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001857 stream_(NULL),
pbos@webrtc.orgb648b9d2014-08-26 11:08:06 +00001858 config_(config),
pbos@webrtc.org776e6f22014-10-29 15:28:39 +00001859 external_decoder_factory_(external_decoder_factory),
pbos@webrtc.orgb648b9d2014-08-26 11:08:06 +00001860 renderer_(NULL),
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001861 last_width_(-1),
magjed@webrtc.orgfc5ad952015-01-27 09:57:01 +00001862 last_height_(-1),
1863 first_frame_timestamp_(-1),
1864 estimated_remote_start_ntp_time_ms_(0) {
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001865 config_.renderer = this;
1866 // SetRecvCodecs will also reset (start) the VideoReceiveStream.
1867 SetRecvCodecs(recv_codecs);
1868}
1869
1870WebRtcVideoChannel2::WebRtcVideoReceiveStream::~WebRtcVideoReceiveStream() {
1871 call_->DestroyVideoReceiveStream(stream_);
pbos@webrtc.org96a93252014-11-03 14:46:44 +00001872 ClearDecoders(&allocated_decoders_);
1873}
1874
1875WebRtcVideoChannel2::WebRtcVideoReceiveStream::AllocatedDecoder
1876WebRtcVideoChannel2::WebRtcVideoReceiveStream::CreateOrReuseVideoDecoder(
1877 std::vector<AllocatedDecoder>* old_decoders,
1878 const VideoCodec& codec) {
1879 webrtc::VideoCodecType type = CodecTypeFromName(codec.name);
1880
1881 for (size_t i = 0; i < old_decoders->size(); ++i) {
1882 if ((*old_decoders)[i].type == type) {
1883 AllocatedDecoder decoder = (*old_decoders)[i];
1884 (*old_decoders)[i] = old_decoders->back();
1885 old_decoders->pop_back();
1886 return decoder;
1887 }
1888 }
1889
1890 if (external_decoder_factory_ != NULL) {
1891 webrtc::VideoDecoder* decoder =
1892 external_decoder_factory_->CreateVideoDecoder(type);
1893 if (decoder != NULL) {
1894 return AllocatedDecoder(decoder, type, true);
1895 }
1896 }
1897
1898 if (type == webrtc::kVideoCodecVP8) {
1899 return AllocatedDecoder(
1900 webrtc::VideoDecoder::Create(webrtc::VideoDecoder::kVp8), type, false);
1901 }
1902
1903 // This shouldn't happen, we should not be trying to create something we don't
1904 // support.
1905 assert(false);
1906 return AllocatedDecoder(NULL, webrtc::kVideoCodecUnknown, false);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001907}
1908
1909void WebRtcVideoChannel2::WebRtcVideoReceiveStream::SetRecvCodecs(
1910 const std::vector<VideoCodecSettings>& recv_codecs) {
pbos@webrtc.org96a93252014-11-03 14:46:44 +00001911 std::vector<AllocatedDecoder> old_decoders = allocated_decoders_;
1912 allocated_decoders_.clear();
1913 config_.decoders.clear();
1914 for (size_t i = 0; i < recv_codecs.size(); ++i) {
1915 AllocatedDecoder allocated_decoder =
1916 CreateOrReuseVideoDecoder(&old_decoders, recv_codecs[i].codec);
1917 allocated_decoders_.push_back(allocated_decoder);
1918
1919 webrtc::VideoReceiveStream::Decoder decoder;
1920 decoder.decoder = allocated_decoder.decoder;
1921 decoder.payload_type = recv_codecs[i].codec.id;
1922 decoder.payload_name = recv_codecs[i].codec.name;
1923 config_.decoders.push_back(decoder);
1924 }
1925
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001926 // TODO(pbos): Reconfigure RTX based on incoming recv_codecs.
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001927 config_.rtp.fec = recv_codecs.front().fec;
pbos@webrtc.org257e1302014-07-25 19:01:32 +00001928 config_.rtp.nack.rtp_history_ms =
1929 IsNackEnabled(recv_codecs.begin()->codec) ? kNackHistoryMs : 0;
1930 config_.rtp.remb = IsRembEnabled(recv_codecs.begin()->codec);
1931
pbos@webrtc.org96a93252014-11-03 14:46:44 +00001932 ClearDecoders(&old_decoders);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001933 RecreateWebRtcStream();
1934}
1935
1936void WebRtcVideoChannel2::WebRtcVideoReceiveStream::SetRtpExtensions(
1937 const std::vector<webrtc::RtpExtension>& extensions) {
1938 config_.rtp.extensions = extensions;
1939 RecreateWebRtcStream();
1940}
1941
1942void WebRtcVideoChannel2::WebRtcVideoReceiveStream::RecreateWebRtcStream() {
1943 if (stream_ != NULL) {
1944 call_->DestroyVideoReceiveStream(stream_);
1945 }
1946 stream_ = call_->CreateVideoReceiveStream(config_);
1947 stream_->Start();
1948}
1949
pbos@webrtc.org96a93252014-11-03 14:46:44 +00001950void WebRtcVideoChannel2::WebRtcVideoReceiveStream::ClearDecoders(
1951 std::vector<AllocatedDecoder>* allocated_decoders) {
1952 for (size_t i = 0; i < allocated_decoders->size(); ++i) {
1953 if ((*allocated_decoders)[i].external) {
pbos@webrtc.org776e6f22014-10-29 15:28:39 +00001954 external_decoder_factory_->DestroyVideoDecoder(
pbos@webrtc.org96a93252014-11-03 14:46:44 +00001955 (*allocated_decoders)[i].decoder);
pbos@webrtc.org776e6f22014-10-29 15:28:39 +00001956 } else {
pbos@webrtc.org96a93252014-11-03 14:46:44 +00001957 delete (*allocated_decoders)[i].decoder;
pbos@webrtc.org776e6f22014-10-29 15:28:39 +00001958 }
1959 }
pbos@webrtc.org96a93252014-11-03 14:46:44 +00001960 allocated_decoders->clear();
pbos@webrtc.org776e6f22014-10-29 15:28:39 +00001961}
1962
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001963void WebRtcVideoChannel2::WebRtcVideoReceiveStream::RenderFrame(
1964 const webrtc::I420VideoFrame& frame,
1965 int time_to_render_ms) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001966 rtc::CritScope crit(&renderer_lock_);
magjed@webrtc.orgfc5ad952015-01-27 09:57:01 +00001967
1968 if (first_frame_timestamp_ < 0)
1969 first_frame_timestamp_ = frame.timestamp();
1970 int64_t rtp_time_elapsed_since_first_frame =
1971 (timestamp_wraparound_handler_.Unwrap(frame.timestamp()) -
1972 first_frame_timestamp_);
1973 int64_t elapsed_time_ms = rtp_time_elapsed_since_first_frame /
1974 (cricket::kVideoCodecClockrate / 1000);
1975 if (frame.ntp_time_ms() > 0)
1976 estimated_remote_start_ntp_time_ms_ = frame.ntp_time_ms() - elapsed_time_ms;
1977
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001978 if (renderer_ == NULL) {
1979 LOG(LS_WARNING) << "VideoReceiveStream not connected to a VideoRenderer.";
1980 return;
1981 }
1982
1983 if (frame.width() != last_width_ || frame.height() != last_height_) {
1984 SetSize(frame.width(), frame.height());
1985 }
1986
1987 LOG(LS_VERBOSE) << "RenderFrame: (" << frame.width() << "x" << frame.height()
1988 << ")";
1989
magjed@webrtc.orgfc5ad952015-01-27 09:57:01 +00001990 const WebRtcVideoRenderFrame render_frame(&frame, elapsed_time_ms);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001991 renderer_->RenderFrame(&render_frame);
1992}
1993
pbos@webrtc.org0d852d52015-02-09 15:14:36 +00001994bool WebRtcVideoChannel2::WebRtcVideoReceiveStream::IsTextureSupported() const {
1995 return true;
1996}
1997
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001998void WebRtcVideoChannel2::WebRtcVideoReceiveStream::SetRenderer(
1999 cricket::VideoRenderer* renderer) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002000 rtc::CritScope crit(&renderer_lock_);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00002001 renderer_ = renderer;
2002 if (renderer_ != NULL && last_width_ != -1) {
2003 SetSize(last_width_, last_height_);
2004 }
2005}
2006
2007VideoRenderer* WebRtcVideoChannel2::WebRtcVideoReceiveStream::GetRenderer() {
2008 // TODO(pbos): Remove GetRenderer and all uses of it, it's thread-unsafe by
2009 // design.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002010 rtc::CritScope crit(&renderer_lock_);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00002011 return renderer_;
2012}
2013
2014void WebRtcVideoChannel2::WebRtcVideoReceiveStream::SetSize(int width,
2015 int height) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002016 rtc::CritScope crit(&renderer_lock_);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00002017 if (!renderer_->SetSize(width, height, 0)) {
2018 LOG(LS_ERROR) << "Could not set renderer size.";
2019 }
2020 last_width_ = width;
2021 last_height_ = height;
2022}
2023
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00002024VideoReceiverInfo
2025WebRtcVideoChannel2::WebRtcVideoReceiveStream::GetVideoReceiverInfo() {
2026 VideoReceiverInfo info;
2027 info.add_ssrc(config_.rtp.remote_ssrc);
2028 webrtc::VideoReceiveStream::Stats stats = stream_->GetStats();
asapersson@webrtc.orgcfd82df2015-01-22 09:39:59 +00002029 info.bytes_rcvd = stats.rtp_stats.transmitted.payload_bytes +
2030 stats.rtp_stats.transmitted.header_bytes +
2031 stats.rtp_stats.transmitted.padding_bytes;
2032 info.packets_rcvd = stats.rtp_stats.transmitted.packets;
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00002033
2034 info.framerate_rcvd = stats.network_frame_rate;
2035 info.framerate_decoded = stats.decode_frame_rate;
2036 info.framerate_output = stats.render_frame_rate;
2037
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +00002038 {
2039 rtc::CritScope frame_cs(&renderer_lock_);
2040 info.frame_width = last_width_;
2041 info.frame_height = last_height_;
2042 info.capture_start_ntp_time_ms = estimated_remote_start_ntp_time_ms_;
2043 }
2044
pbos@webrtc.org09c77b92015-02-25 10:42:16 +00002045 info.decode_ms = stats.decode_ms;
2046 info.max_decode_ms = stats.max_decode_ms;
2047 info.current_delay_ms = stats.current_delay_ms;
2048 info.target_delay_ms = stats.target_delay_ms;
2049 info.jitter_buffer_ms = stats.jitter_buffer_ms;
2050 info.min_playout_delay_ms = stats.min_playout_delay_ms;
2051 info.render_delay_ms = stats.render_delay_ms;
2052
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +00002053 info.firs_sent = stats.rtcp_packet_type_counts.fir_packets;
2054 info.plis_sent = stats.rtcp_packet_type_counts.pli_packets;
2055 info.nacks_sent = stats.rtcp_packet_type_counts.nack_packets;
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00002056
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00002057 return info;
2058}
2059
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00002060WebRtcVideoChannel2::VideoCodecSettings::VideoCodecSettings()
2061 : rtx_payload_type(-1) {}
2062
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +00002063bool WebRtcVideoChannel2::VideoCodecSettings::operator==(
2064 const WebRtcVideoChannel2::VideoCodecSettings& other) const {
2065 return codec == other.codec &&
2066 fec.ulpfec_payload_type == other.fec.ulpfec_payload_type &&
2067 fec.red_payload_type == other.fec.red_payload_type &&
2068 rtx_payload_type == other.rtx_payload_type;
2069}
2070
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00002071std::vector<WebRtcVideoChannel2::VideoCodecSettings>
2072WebRtcVideoChannel2::MapCodecs(const std::vector<VideoCodec>& codecs) {
2073 assert(!codecs.empty());
2074
2075 std::vector<VideoCodecSettings> video_codecs;
2076 std::map<int, bool> payload_used;
pbos@webrtc.orge322a172014-06-13 11:47:28 +00002077 std::map<int, VideoCodec::CodecType> payload_codec_type;
pkasting@chromium.orgd3245462015-02-23 21:28:22 +00002078 // |rtx_mapping| maps video payload type to rtx payload type.
2079 std::map<int, int> rtx_mapping;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00002080
2081 webrtc::FecConfig fec_settings;
2082
2083 for (size_t i = 0; i < codecs.size(); ++i) {
2084 const VideoCodec& in_codec = codecs[i];
2085 int payload_type = in_codec.id;
2086
2087 if (payload_used[payload_type]) {
2088 LOG(LS_ERROR) << "Payload type already registered: "
2089 << in_codec.ToString();
2090 return std::vector<VideoCodecSettings>();
2091 }
2092 payload_used[payload_type] = true;
pbos@webrtc.orge322a172014-06-13 11:47:28 +00002093 payload_codec_type[payload_type] = in_codec.GetCodecType();
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00002094
2095 switch (in_codec.GetCodecType()) {
2096 case VideoCodec::CODEC_RED: {
2097 // RED payload type, should not have duplicates.
2098 assert(fec_settings.red_payload_type == -1);
2099 fec_settings.red_payload_type = in_codec.id;
2100 continue;
2101 }
2102
2103 case VideoCodec::CODEC_ULPFEC: {
2104 // ULPFEC payload type, should not have duplicates.
2105 assert(fec_settings.ulpfec_payload_type == -1);
2106 fec_settings.ulpfec_payload_type = in_codec.id;
2107 continue;
2108 }
2109
2110 case VideoCodec::CODEC_RTX: {
2111 int associated_payload_type;
2112 if (!in_codec.GetParam(kCodecParamAssociatedPayloadType,
pkasting@chromium.orge9facf82015-02-17 20:36:28 +00002113 &associated_payload_type) ||
2114 !IsValidRtpPayloadType(associated_payload_type)) {
2115 LOG(LS_ERROR)
2116 << "RTX codec with invalid or no associated payload type: "
2117 << in_codec.ToString();
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00002118 return std::vector<VideoCodecSettings>();
2119 }
2120 rtx_mapping[associated_payload_type] = in_codec.id;
2121 continue;
2122 }
2123
2124 case VideoCodec::CODEC_VIDEO:
2125 break;
2126 }
2127
2128 video_codecs.push_back(VideoCodecSettings());
2129 video_codecs.back().codec = in_codec;
2130 }
2131
2132 // One of these codecs should have been a video codec. Only having FEC
2133 // parameters into this code is a logic error.
2134 assert(!video_codecs.empty());
2135
pbos@webrtc.orge322a172014-06-13 11:47:28 +00002136 for (std::map<int, int>::const_iterator it = rtx_mapping.begin();
2137 it != rtx_mapping.end();
2138 ++it) {
2139 if (!payload_used[it->first]) {
2140 LOG(LS_ERROR) << "RTX mapped to payload not in codec list.";
2141 return std::vector<VideoCodecSettings>();
2142 }
andrew@webrtc.org8f27fcc2015-01-09 20:22:46 +00002143 if (payload_codec_type[it->first] != VideoCodec::CODEC_VIDEO) {
2144 LOG(LS_ERROR) << "RTX not mapped to regular video codec.";
pbos@webrtc.orge322a172014-06-13 11:47:28 +00002145 return std::vector<VideoCodecSettings>();
2146 }
2147 }
2148
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00002149 // TODO(pbos): Write tests that figure out that I have not verified that RTX
2150 // codecs aren't mapped to bogus payloads.
2151 for (size_t i = 0; i < video_codecs.size(); ++i) {
2152 video_codecs[i].fec = fec_settings;
andrew@webrtc.org8f27fcc2015-01-09 20:22:46 +00002153 if (rtx_mapping[video_codecs[i].codec.id] != 0) {
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00002154 video_codecs[i].rtx_payload_type = rtx_mapping[video_codecs[i].codec.id];
2155 }
2156 }
2157
2158 return video_codecs;
2159}
2160
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00002161} // namespace cricket
2162
2163#endif // HAVE_WEBRTC_VIDEO