blob: 5a380cbad6a126116932f0b1fca8f15eb906f4ad [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.org9a4410e2015-02-26 10:03:39 +00001333 muted_(false),
1334 old_adapt_changes_(0) {
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001335 parameters_.config.rtp.max_packet_size = kVideoMtu;
1336
1337 sp.GetPrimarySsrcs(&parameters_.config.rtp.ssrcs);
1338 sp.GetFidSsrcs(parameters_.config.rtp.ssrcs,
1339 &parameters_.config.rtp.rtx.ssrcs);
1340 parameters_.config.rtp.c_name = sp.cname;
1341 parameters_.config.rtp.extensions = rtp_extensions;
1342
1343 VideoCodecSettings params;
1344 if (codec_settings.Get(&params)) {
1345 SetCodec(params);
1346 }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001347}
1348
1349WebRtcVideoChannel2::WebRtcVideoSendStream::~WebRtcVideoSendStream() {
1350 DisconnectCapturer();
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001351 if (stream_ != NULL) {
1352 call_->DestroyVideoSendStream(stream_);
1353 }
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +00001354 DestroyVideoEncoder(&allocated_encoder_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001355}
1356
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001357static void CreateBlackFrame(webrtc::I420VideoFrame* video_frame,
1358 int width,
1359 int height) {
pbos@webrtc.orgb4987bf2015-02-18 10:13:09 +00001360 video_frame->CreateEmptyFrame(width, height, width, (width + 1) / 2,
1361 (width + 1) / 2);
1362 memset(video_frame->buffer(webrtc::kYPlane), 16,
1363 video_frame->allocated_size(webrtc::kYPlane));
1364 memset(video_frame->buffer(webrtc::kUPlane), 128,
1365 video_frame->allocated_size(webrtc::kUPlane));
1366 memset(video_frame->buffer(webrtc::kVPlane), 128,
1367 video_frame->allocated_size(webrtc::kVPlane));
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001368}
1369
1370static void ConvertToI420VideoFrame(const VideoFrame& frame,
1371 webrtc::I420VideoFrame* i420_frame) {
1372 i420_frame->CreateFrame(
1373 static_cast<int>(frame.GetYPitch() * frame.GetHeight()),
1374 frame.GetYPlane(),
1375 static_cast<int>(frame.GetUPitch() * ((frame.GetHeight() + 1) / 2)),
1376 frame.GetUPlane(),
1377 static_cast<int>(frame.GetVPitch() * ((frame.GetHeight() + 1) / 2)),
1378 frame.GetVPlane(),
1379 static_cast<int>(frame.GetWidth()),
1380 static_cast<int>(frame.GetHeight()),
1381 static_cast<int>(frame.GetYPitch()),
1382 static_cast<int>(frame.GetUPitch()),
1383 static_cast<int>(frame.GetVPitch()));
1384}
1385
1386void WebRtcVideoChannel2::WebRtcVideoSendStream::InputFrame(
1387 VideoCapturer* capturer,
1388 const VideoFrame* frame) {
pbos@webrtc.org86196c42015-02-16 21:02:00 +00001389 TRACE_EVENT0("webrtc", "WebRtcVideoSendStream::InputFrame");
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001390 LOG(LS_VERBOSE) << "InputFrame: " << frame->GetWidth() << "x"
1391 << frame->GetHeight();
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001392 // Lock before copying, can be called concurrently when swapping input source.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001393 rtc::CritScope frame_cs(&frame_lock_);
pbos@webrtc.orgd60d79a2014-09-24 07:10:57 +00001394 ConvertToI420VideoFrame(*frame, &video_frame_);
1395
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001396 rtc::CritScope cs(&lock_);
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001397 if (stream_ == NULL) {
1398 LOG(LS_WARNING) << "Capturer inputting frames before send codecs are "
1399 "configured, dropping.";
1400 return;
1401 }
pbos@webrtc.org86196c42015-02-16 21:02:00 +00001402
1403 // Not sending, abort early to prevent expensive reconfigurations while
1404 // setting up codecs etc.
1405 if (!sending_)
1406 return;
1407
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001408 if (format_.width == 0) { // Dropping frames.
1409 assert(format_.height == 0);
1410 LOG(LS_VERBOSE) << "VideoFormat 0x0 set, Dropping frame.";
1411 return;
1412 }
pbos@webrtc.orgd60d79a2014-09-24 07:10:57 +00001413 if (muted_) {
1414 // Create a black frame to transmit instead.
1415 CreateBlackFrame(&video_frame_,
1416 static_cast<int>(frame->GetWidth()),
1417 static_cast<int>(frame->GetHeight()));
1418 }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001419 // Reconfigure codec if necessary.
pbos@webrtc.orgc4175b92014-09-03 15:25:49 +00001420 SetDimensions(
1421 video_frame_.width(), video_frame_.height(), capturer->IsScreencast());
1422
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001423 LOG(LS_VERBOSE) << "SwapFrame: " << video_frame_.width() << "x"
1424 << video_frame_.height() << " -> (codec) "
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +00001425 << parameters_.encoder_config.streams.back().width << "x"
1426 << parameters_.encoder_config.streams.back().height;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001427 stream_->Input()->SwapFrame(&video_frame_);
1428}
1429
1430bool WebRtcVideoChannel2::WebRtcVideoSendStream::SetCapturer(
1431 VideoCapturer* capturer) {
pbos@webrtc.orgb4987bf2015-02-18 10:13:09 +00001432 TRACE_EVENT0("webrtc", "WebRtcVideoSendStream::SetCapturer");
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001433 if (!DisconnectCapturer() && capturer == NULL) {
1434 return false;
1435 }
1436
1437 {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001438 rtc::CritScope cs(&lock_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001439
pbos@webrtc.org9359cb32014-07-23 15:44:48 +00001440 if (capturer == NULL) {
1441 if (stream_ != NULL) {
1442 LOG(LS_VERBOSE) << "Disabling capturer, sending black frame.";
1443 webrtc::I420VideoFrame black_frame;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001444
pbos@webrtc.orgb4987bf2015-02-18 10:13:09 +00001445 CreateBlackFrame(&black_frame, last_dimensions_.width,
1446 last_dimensions_.height);
pbos@webrtc.org9359cb32014-07-23 15:44:48 +00001447 stream_->Input()->SwapFrame(&black_frame);
1448 }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001449
1450 capturer_ = NULL;
1451 return true;
1452 }
1453
1454 capturer_ = capturer;
1455 }
1456 // Lock cannot be held while connecting the capturer to prevent lock-order
1457 // violations.
1458 capturer->SignalVideoFrame.connect(this, &WebRtcVideoSendStream::InputFrame);
1459 return true;
1460}
1461
1462bool WebRtcVideoChannel2::WebRtcVideoSendStream::SetVideoFormat(
1463 const VideoFormat& format) {
1464 if ((format.width == 0 || format.height == 0) &&
1465 format.width != format.height) {
1466 LOG(LS_ERROR) << "Can't set VideoFormat, width or height is zero (but not "
1467 "both, 0x0 drops frames).";
1468 return false;
1469 }
1470
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001471 rtc::CritScope cs(&lock_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001472 if (format.width == 0 && format.height == 0) {
1473 LOG(LS_INFO)
1474 << "0x0 resolution selected. Captured frames will be dropped for ssrc: "
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +00001475 << parameters_.config.rtp.ssrcs[0] << ".";
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001476 } else {
1477 // TODO(pbos): Fix me, this only affects the last stream!
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +00001478 parameters_.encoder_config.streams.back().max_framerate =
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001479 VideoFormat::IntervalToFps(format.interval);
pbos@webrtc.orgc4175b92014-09-03 15:25:49 +00001480 SetDimensions(format.width, format.height, false);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001481 }
1482
1483 format_ = format;
1484 return true;
1485}
1486
pbos@webrtc.orgef8bb8d2014-08-13 21:36:18 +00001487void WebRtcVideoChannel2::WebRtcVideoSendStream::MuteStream(bool mute) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001488 rtc::CritScope cs(&lock_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001489 muted_ = mute;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001490}
1491
1492bool WebRtcVideoChannel2::WebRtcVideoSendStream::DisconnectCapturer() {
pbos@webrtc.org963b9792014-10-07 14:27:27 +00001493 cricket::VideoCapturer* capturer;
1494 {
1495 rtc::CritScope cs(&lock_);
pbos@webrtc.org9a4410e2015-02-26 10:03:39 +00001496 if (capturer_ == NULL)
pbos@webrtc.org963b9792014-10-07 14:27:27 +00001497 return false;
pbos@webrtc.org9a4410e2015-02-26 10:03:39 +00001498
1499 if (capturer_->video_adapter() != nullptr)
1500 old_adapt_changes_ += capturer_->video_adapter()->adaptation_changes();
1501
pbos@webrtc.org963b9792014-10-07 14:27:27 +00001502 capturer = capturer_;
1503 capturer_ = NULL;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001504 }
pbos@webrtc.org963b9792014-10-07 14:27:27 +00001505 capturer->SignalVideoFrame.disconnect(this);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001506 return true;
1507}
1508
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001509void WebRtcVideoChannel2::WebRtcVideoSendStream::SetOptions(
1510 const VideoOptions& options) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001511 rtc::CritScope cs(&lock_);
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001512 VideoCodecSettings codec_settings;
1513 if (parameters_.codec_settings.Get(&codec_settings)) {
1514 SetCodecAndOptions(codec_settings, options);
1515 } else {
1516 parameters_.options = options;
1517 }
1518}
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +00001519
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001520void WebRtcVideoChannel2::WebRtcVideoSendStream::SetCodec(
1521 const VideoCodecSettings& codec_settings) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001522 rtc::CritScope cs(&lock_);
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001523 SetCodecAndOptions(codec_settings, parameters_.options);
1524}
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +00001525
1526webrtc::VideoCodecType CodecTypeFromName(const std::string& name) {
1527 if (CodecNameMatches(name, kVp8CodecName)) {
1528 return webrtc::kVideoCodecVP8;
andresp@webrtc.org188d3b22014-11-07 13:21:04 +00001529 } else if (CodecNameMatches(name, kVp9CodecName)) {
1530 return webrtc::kVideoCodecVP9;
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +00001531 } else if (CodecNameMatches(name, kH264CodecName)) {
1532 return webrtc::kVideoCodecH264;
1533 }
1534 return webrtc::kVideoCodecUnknown;
1535}
1536
1537WebRtcVideoChannel2::WebRtcVideoSendStream::AllocatedEncoder
1538WebRtcVideoChannel2::WebRtcVideoSendStream::CreateVideoEncoder(
1539 const VideoCodec& codec) {
1540 webrtc::VideoCodecType type = CodecTypeFromName(codec.name);
1541
1542 // Do not re-create encoders of the same type.
1543 if (type == allocated_encoder_.type && allocated_encoder_.encoder != NULL) {
1544 return allocated_encoder_;
1545 }
1546
1547 if (external_encoder_factory_ != NULL) {
1548 webrtc::VideoEncoder* encoder =
1549 external_encoder_factory_->CreateVideoEncoder(type);
1550 if (encoder != NULL) {
1551 return AllocatedEncoder(encoder, type, true);
1552 }
1553 }
1554
1555 if (type == webrtc::kVideoCodecVP8) {
1556 return AllocatedEncoder(
1557 webrtc::VideoEncoder::Create(webrtc::VideoEncoder::kVp8), type, false);
andresp@webrtc.org188d3b22014-11-07 13:21:04 +00001558 } else if (type == webrtc::kVideoCodecVP9) {
1559 return AllocatedEncoder(
1560 webrtc::VideoEncoder::Create(webrtc::VideoEncoder::kVp9), type, false);
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +00001561 }
1562
1563 // This shouldn't happen, we should not be trying to create something we don't
1564 // support.
1565 assert(false);
1566 return AllocatedEncoder(NULL, webrtc::kVideoCodecUnknown, false);
1567}
1568
1569void WebRtcVideoChannel2::WebRtcVideoSendStream::DestroyVideoEncoder(
1570 AllocatedEncoder* encoder) {
1571 if (encoder->external) {
1572 external_encoder_factory_->DestroyVideoEncoder(encoder->encoder);
1573 } else {
1574 delete encoder->encoder;
1575 }
1576}
1577
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001578void WebRtcVideoChannel2::WebRtcVideoSendStream::SetCodecAndOptions(
1579 const VideoCodecSettings& codec_settings,
1580 const VideoOptions& options) {
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +00001581 parameters_.encoder_config =
1582 CreateVideoEncoderConfig(last_dimensions_, codec_settings.codec);
pbos@webrtc.org86196c42015-02-16 21:02:00 +00001583 if (parameters_.encoder_config.streams.empty())
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001584 return;
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +00001585
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001586 format_ = VideoFormat(codec_settings.codec.width,
1587 codec_settings.codec.height,
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001588 VideoFormat::FpsToInterval(30),
1589 FOURCC_I420);
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +00001590
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +00001591 AllocatedEncoder new_encoder = CreateVideoEncoder(codec_settings.codec);
1592 parameters_.config.encoder_settings.encoder = new_encoder.encoder;
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001593 parameters_.config.encoder_settings.payload_name = codec_settings.codec.name;
1594 parameters_.config.encoder_settings.payload_type = codec_settings.codec.id;
1595 parameters_.config.rtp.fec = codec_settings.fec;
1596
1597 // Set RTX payload type if RTX is enabled.
1598 if (!parameters_.config.rtp.rtx.ssrcs.empty()) {
1599 parameters_.config.rtp.rtx.payload_type = codec_settings.rtx_payload_type;
1600 }
1601
1602 if (IsNackEnabled(codec_settings.codec)) {
1603 parameters_.config.rtp.nack.rtp_history_ms = kNackHistoryMs;
1604 }
1605
pbos@webrtc.org5ff71ab2014-07-23 07:28:56 +00001606 options.suspend_below_min_bitrate.Get(
1607 &parameters_.config.suspend_below_min_bitrate);
1608
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001609 parameters_.codec_settings.Set(codec_settings);
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +00001610 parameters_.options = options;
pbos@webrtc.org543e5892014-07-23 07:01:31 +00001611
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001612 RecreateWebRtcStream();
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +00001613 if (allocated_encoder_.encoder != new_encoder.encoder) {
1614 DestroyVideoEncoder(&allocated_encoder_);
1615 allocated_encoder_ = new_encoder;
1616 }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001617}
1618
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001619void WebRtcVideoChannel2::WebRtcVideoSendStream::SetRtpExtensions(
1620 const std::vector<webrtc::RtpExtension>& rtp_extensions) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001621 rtc::CritScope cs(&lock_);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001622 parameters_.config.rtp.extensions = rtp_extensions;
1623 RecreateWebRtcStream();
1624}
1625
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +00001626webrtc::VideoEncoderConfig
1627WebRtcVideoChannel2::WebRtcVideoSendStream::CreateVideoEncoderConfig(
1628 const Dimensions& dimensions,
1629 const VideoCodec& codec) const {
1630 webrtc::VideoEncoderConfig encoder_config;
1631 if (dimensions.is_screencast) {
pbos@webrtc.orgefc82c22014-10-27 13:58:00 +00001632 int screencast_min_bitrate_kbps;
1633 parameters_.options.screencast_min_bitrate.Get(
1634 &screencast_min_bitrate_kbps);
1635 encoder_config.min_transmit_bitrate_bps =
1636 screencast_min_bitrate_kbps * 1000;
1637 encoder_config.content_type = webrtc::VideoEncoderConfig::kScreenshare;
1638 } else {
1639 encoder_config.min_transmit_bitrate_bps = 0;
1640 encoder_config.content_type = webrtc::VideoEncoderConfig::kRealtimeVideo;
1641 }
1642
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +00001643 // Restrict dimensions according to codec max.
1644 int width = dimensions.width;
1645 int height = dimensions.height;
1646 if (!dimensions.is_screencast) {
1647 if (codec.width < width)
1648 width = codec.width;
1649 if (codec.height < height)
1650 height = codec.height;
1651 }
1652
1653 VideoCodec clamped_codec = codec;
1654 clamped_codec.width = width;
1655 clamped_codec.height = height;
pbos@webrtc.orgcddd17c2014-09-16 16:33:13 +00001656
pbos@webrtc.orgf1c8b902015-01-14 17:29:27 +00001657 encoder_config.streams = CreateVideoStreams(
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +00001658 clamped_codec, parameters_.options, parameters_.config.rtp.ssrcs.size());
pbos@webrtc.org6f48f1b2014-07-22 16:29:54 +00001659
pbos@webrtc.orgb7ed7792014-10-31 13:08:10 +00001660 // Conference mode screencast uses 2 temporal layers split at 100kbit.
1661 if (parameters_.options.conference_mode.GetWithDefaultIfUnset(false) &&
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +00001662 dimensions.is_screencast && encoder_config.streams.size() == 1) {
sprang@webrtc.org46d4d292014-12-23 15:19:35 +00001663 ScreenshareLayerConfig config = ScreenshareLayerConfig::GetDefault();
1664
1665 // For screenshare in conference mode, tl0 and tl1 bitrates are piggybacked
1666 // on the VideoCodec struct as target and max bitrates, respectively.
1667 // See eg. webrtc::VP8EncoderImpl::SetRates().
1668 encoder_config.streams[0].target_bitrate_bps =
1669 config.tl0_bitrate_kbps * 1000;
1670 encoder_config.streams[0].max_bitrate_bps = config.tl1_bitrate_kbps * 1000;
pbos@webrtc.orgb7ed7792014-10-31 13:08:10 +00001671 encoder_config.streams[0].temporal_layer_thresholds_bps.clear();
1672 encoder_config.streams[0].temporal_layer_thresholds_bps.push_back(
sprang@webrtc.org46d4d292014-12-23 15:19:35 +00001673 config.tl0_bitrate_kbps * 1000);
pbos@webrtc.orgb7ed7792014-10-31 13:08:10 +00001674 }
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +00001675 return encoder_config;
1676}
1677
1678void WebRtcVideoChannel2::WebRtcVideoSendStream::SetDimensions(
1679 int width,
1680 int height,
1681 bool is_screencast) {
1682 if (last_dimensions_.width == width && last_dimensions_.height == height &&
1683 last_dimensions_.is_screencast == is_screencast) {
1684 // Configured using the same parameters, do not reconfigure.
1685 return;
1686 }
1687 LOG(LS_INFO) << "SetDimensions: " << width << "x" << height
1688 << (is_screencast ? " (screencast)" : " (not screencast)");
1689
1690 last_dimensions_.width = width;
1691 last_dimensions_.height = height;
1692 last_dimensions_.is_screencast = is_screencast;
1693
1694 assert(!parameters_.encoder_config.streams.empty());
1695
1696 VideoCodecSettings codec_settings;
1697 parameters_.codec_settings.Get(&codec_settings);
1698
1699 webrtc::VideoEncoderConfig encoder_config =
1700 CreateVideoEncoderConfig(last_dimensions_, codec_settings.codec);
1701
1702 encoder_config.encoder_specific_settings =
pbos@webrtc.orgf1c8b902015-01-14 17:29:27 +00001703 ConfigureVideoEncoderSettings(codec_settings.codec, parameters_.options);
pbos@webrtc.orgb7ed7792014-10-31 13:08:10 +00001704
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +00001705 bool stream_reconfigured = stream_->ReconfigureVideoEncoder(encoder_config);
1706
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +00001707 encoder_config.encoder_specific_settings = NULL;
pbos@webrtc.org6f48f1b2014-07-22 16:29:54 +00001708
1709 if (!stream_reconfigured) {
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001710 LOG(LS_WARNING) << "Failed to reconfigure video encoder for dimensions: "
1711 << width << "x" << height;
1712 return;
1713 }
pbos@webrtc.orgcddd17c2014-09-16 16:33:13 +00001714
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +00001715 parameters_.encoder_config = encoder_config;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001716}
1717
1718void WebRtcVideoChannel2::WebRtcVideoSendStream::Start() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001719 rtc::CritScope cs(&lock_);
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001720 assert(stream_ != NULL);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001721 stream_->Start();
1722 sending_ = true;
1723}
1724
1725void WebRtcVideoChannel2::WebRtcVideoSendStream::Stop() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001726 rtc::CritScope cs(&lock_);
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001727 if (stream_ != NULL) {
1728 stream_->Stop();
1729 }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001730 sending_ = false;
1731}
1732
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00001733VideoSenderInfo
1734WebRtcVideoChannel2::WebRtcVideoSendStream::GetVideoSenderInfo() {
1735 VideoSenderInfo info;
pbos@webrtc.org77e11bb2015-02-23 16:39:07 +00001736 webrtc::VideoSendStream::Stats stats;
1737 {
1738 rtc::CritScope cs(&lock_);
1739 for (uint32_t ssrc : parameters_.config.rtp.ssrcs)
1740 info.add_ssrc(ssrc);
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00001741
pbos@webrtc.org77e11bb2015-02-23 16:39:07 +00001742 for (size_t i = 0; i < parameters_.encoder_config.streams.size(); ++i) {
1743 if (i == parameters_.encoder_config.streams.size() - 1) {
1744 info.preferred_bitrate +=
1745 parameters_.encoder_config.streams[i].max_bitrate_bps;
1746 } else {
1747 info.preferred_bitrate +=
1748 parameters_.encoder_config.streams[i].target_bitrate_bps;
1749 }
1750 }
pbos@webrtc.orgc3d2bd22014-08-12 20:55:10 +00001751
pbos@webrtc.org77e11bb2015-02-23 16:39:07 +00001752 if (stream_ == NULL)
1753 return info;
1754
1755 stats = stream_->GetStats();
1756
pbos@webrtc.org9a4410e2015-02-26 10:03:39 +00001757 info.adapt_changes = old_adapt_changes_;
1758 info.adapt_reason = CoordinatedVideoAdapter::ADAPTREASON_NONE;
1759
1760 if (capturer_ != NULL) {
1761 if (!capturer_->IsMuted()) {
1762 VideoFormat last_captured_frame_format;
1763 capturer_->GetStats(&info.adapt_frame_drops, &info.effects_frame_drops,
1764 &info.capturer_frame_time,
1765 &last_captured_frame_format);
1766 info.input_frame_width = last_captured_frame_format.width;
1767 info.input_frame_height = last_captured_frame_format.height;
1768 }
1769 if (capturer_->video_adapter() != nullptr) {
1770 info.adapt_changes += capturer_->video_adapter()->adaptation_changes();
1771 info.adapt_reason = capturer_->video_adapter()->adapt_reason();
1772 }
pbos@webrtc.org77e11bb2015-02-23 16:39:07 +00001773 }
1774 }
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00001775 info.framerate_input = stats.input_frame_rate;
1776 info.framerate_sent = stats.encode_frame_rate;
pbos@webrtc.org3e6e2712015-02-26 12:19:31 +00001777 info.avg_encode_ms = stats.avg_encode_time_ms;
1778 info.encode_usage_percent = stats.encode_usage_percent;
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00001779
pbos@webrtc.org77e11bb2015-02-23 16:39:07 +00001780 info.nominal_bitrate = stats.media_bitrate_bps;
1781
pbos@webrtc.org273a4142014-12-01 15:23:21 +00001782 info.send_frame_width = 0;
1783 info.send_frame_height = 0;
pbos@webrtc.org09c77b92015-02-25 10:42:16 +00001784 for (std::map<uint32_t, webrtc::VideoSendStream::StreamStats>::iterator it =
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00001785 stats.substreams.begin();
pbos@webrtc.org09c77b92015-02-25 10:42:16 +00001786 it != stats.substreams.end(); ++it) {
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00001787 // TODO(pbos): Wire up additional stats, such as padding bytes.
pbos@webrtc.org09c77b92015-02-25 10:42:16 +00001788 webrtc::VideoSendStream::StreamStats stream_stats = it->second;
asapersson@webrtc.orgcfd82df2015-01-22 09:39:59 +00001789 info.bytes_sent += stream_stats.rtp_stats.transmitted.payload_bytes +
1790 stream_stats.rtp_stats.transmitted.header_bytes +
1791 stream_stats.rtp_stats.transmitted.padding_bytes;
1792 info.packets_sent += stream_stats.rtp_stats.transmitted.packets;
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00001793 info.packets_lost += stream_stats.rtcp_stats.cumulative_lost;
pbos@webrtc.org09c77b92015-02-25 10:42:16 +00001794 if (stream_stats.width > info.send_frame_width)
1795 info.send_frame_width = stream_stats.width;
1796 if (stream_stats.height > info.send_frame_height)
1797 info.send_frame_height = stream_stats.height;
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +00001798 info.firs_rcvd += stream_stats.rtcp_packet_type_counts.fir_packets;
1799 info.nacks_rcvd += stream_stats.rtcp_packet_type_counts.nack_packets;
1800 info.plis_rcvd += stream_stats.rtcp_packet_type_counts.pli_packets;
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00001801 }
1802
1803 if (!stats.substreams.empty()) {
1804 // TODO(pbos): Report fraction lost per SSRC.
pbos@webrtc.org09c77b92015-02-25 10:42:16 +00001805 webrtc::VideoSendStream::StreamStats first_stream_stats =
1806 stats.substreams.begin()->second;
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00001807 info.fraction_lost =
1808 static_cast<float>(first_stream_stats.rtcp_stats.fraction_lost) /
1809 (1 << 8);
1810 }
1811
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00001812 return info;
1813}
1814
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001815void WebRtcVideoChannel2::WebRtcVideoSendStream::FillBandwidthEstimationInfo(
1816 BandwidthEstimationInfo* bwe_info) {
1817 rtc::CritScope cs(&lock_);
1818 if (stream_ == NULL) {
1819 return;
1820 }
1821 webrtc::VideoSendStream::Stats stats = stream_->GetStats();
pbos@webrtc.org09c77b92015-02-25 10:42:16 +00001822 for (std::map<uint32_t, webrtc::VideoSendStream::StreamStats>::iterator it =
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001823 stats.substreams.begin();
pbos@webrtc.org09c77b92015-02-25 10:42:16 +00001824 it != stats.substreams.end(); ++it) {
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001825 bwe_info->transmit_bitrate += it->second.total_bitrate_bps;
1826 bwe_info->retransmit_bitrate += it->second.retransmit_bitrate_bps;
1827 }
pbos@webrtc.org77e11bb2015-02-23 16:39:07 +00001828 bwe_info->actual_enc_bitrate += stats.media_bitrate_bps;
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001829}
1830
pbos@webrtc.org42684be2014-10-03 11:25:45 +00001831void WebRtcVideoChannel2::WebRtcVideoSendStream::OnCpuResolutionRequest(
1832 CoordinatedVideoAdapter::AdaptRequest adapt_request) {
1833 rtc::CritScope cs(&lock_);
1834 bool adapt_cpu;
1835 parameters_.options.cpu_overuse_detection.Get(&adapt_cpu);
pbos@webrtc.org9a4410e2015-02-26 10:03:39 +00001836 if (!adapt_cpu)
pbos@webrtc.org42684be2014-10-03 11:25:45 +00001837 return;
pbos@webrtc.org9a4410e2015-02-26 10:03:39 +00001838 if (capturer_ == NULL || capturer_->video_adapter() == NULL)
pbos@webrtc.org42684be2014-10-03 11:25:45 +00001839 return;
pbos@webrtc.org42684be2014-10-03 11:25:45 +00001840
1841 capturer_->video_adapter()->OnCpuResolutionRequest(adapt_request);
1842}
1843
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001844void WebRtcVideoChannel2::WebRtcVideoSendStream::RecreateWebRtcStream() {
1845 if (stream_ != NULL) {
1846 call_->DestroyVideoSendStream(stream_);
1847 }
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +00001848
pbos@webrtc.org6f48f1b2014-07-22 16:29:54 +00001849 VideoCodecSettings codec_settings;
1850 parameters_.codec_settings.Get(&codec_settings);
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +00001851 parameters_.encoder_config.encoder_specific_settings =
pbos@webrtc.orgf1c8b902015-01-14 17:29:27 +00001852 ConfigureVideoEncoderSettings(codec_settings.codec, parameters_.options);
pbos@webrtc.org6f48f1b2014-07-22 16:29:54 +00001853
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +00001854 stream_ = call_->CreateVideoSendStream(parameters_.config,
1855 parameters_.encoder_config);
pbos@webrtc.org6f48f1b2014-07-22 16:29:54 +00001856
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +00001857 parameters_.encoder_config.encoder_specific_settings = NULL;
pbos@webrtc.org6f48f1b2014-07-22 16:29:54 +00001858
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001859 if (sending_) {
1860 stream_->Start();
1861 }
1862}
1863
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001864WebRtcVideoChannel2::WebRtcVideoReceiveStream::WebRtcVideoReceiveStream(
1865 webrtc::Call* call,
pbos@webrtc.org776e6f22014-10-29 15:28:39 +00001866 WebRtcVideoDecoderFactory* external_decoder_factory,
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001867 const webrtc::VideoReceiveStream::Config& config,
1868 const std::vector<VideoCodecSettings>& recv_codecs)
1869 : call_(call),
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001870 stream_(NULL),
pbos@webrtc.orgb648b9d2014-08-26 11:08:06 +00001871 config_(config),
pbos@webrtc.org776e6f22014-10-29 15:28:39 +00001872 external_decoder_factory_(external_decoder_factory),
pbos@webrtc.orgb648b9d2014-08-26 11:08:06 +00001873 renderer_(NULL),
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001874 last_width_(-1),
magjed@webrtc.orgfc5ad952015-01-27 09:57:01 +00001875 last_height_(-1),
1876 first_frame_timestamp_(-1),
1877 estimated_remote_start_ntp_time_ms_(0) {
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001878 config_.renderer = this;
1879 // SetRecvCodecs will also reset (start) the VideoReceiveStream.
1880 SetRecvCodecs(recv_codecs);
1881}
1882
1883WebRtcVideoChannel2::WebRtcVideoReceiveStream::~WebRtcVideoReceiveStream() {
1884 call_->DestroyVideoReceiveStream(stream_);
pbos@webrtc.org96a93252014-11-03 14:46:44 +00001885 ClearDecoders(&allocated_decoders_);
1886}
1887
1888WebRtcVideoChannel2::WebRtcVideoReceiveStream::AllocatedDecoder
1889WebRtcVideoChannel2::WebRtcVideoReceiveStream::CreateOrReuseVideoDecoder(
1890 std::vector<AllocatedDecoder>* old_decoders,
1891 const VideoCodec& codec) {
1892 webrtc::VideoCodecType type = CodecTypeFromName(codec.name);
1893
1894 for (size_t i = 0; i < old_decoders->size(); ++i) {
1895 if ((*old_decoders)[i].type == type) {
1896 AllocatedDecoder decoder = (*old_decoders)[i];
1897 (*old_decoders)[i] = old_decoders->back();
1898 old_decoders->pop_back();
1899 return decoder;
1900 }
1901 }
1902
1903 if (external_decoder_factory_ != NULL) {
1904 webrtc::VideoDecoder* decoder =
1905 external_decoder_factory_->CreateVideoDecoder(type);
1906 if (decoder != NULL) {
1907 return AllocatedDecoder(decoder, type, true);
1908 }
1909 }
1910
1911 if (type == webrtc::kVideoCodecVP8) {
1912 return AllocatedDecoder(
1913 webrtc::VideoDecoder::Create(webrtc::VideoDecoder::kVp8), type, false);
1914 }
1915
1916 // This shouldn't happen, we should not be trying to create something we don't
1917 // support.
1918 assert(false);
1919 return AllocatedDecoder(NULL, webrtc::kVideoCodecUnknown, false);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001920}
1921
1922void WebRtcVideoChannel2::WebRtcVideoReceiveStream::SetRecvCodecs(
1923 const std::vector<VideoCodecSettings>& recv_codecs) {
pbos@webrtc.org96a93252014-11-03 14:46:44 +00001924 std::vector<AllocatedDecoder> old_decoders = allocated_decoders_;
1925 allocated_decoders_.clear();
1926 config_.decoders.clear();
1927 for (size_t i = 0; i < recv_codecs.size(); ++i) {
1928 AllocatedDecoder allocated_decoder =
1929 CreateOrReuseVideoDecoder(&old_decoders, recv_codecs[i].codec);
1930 allocated_decoders_.push_back(allocated_decoder);
1931
1932 webrtc::VideoReceiveStream::Decoder decoder;
1933 decoder.decoder = allocated_decoder.decoder;
1934 decoder.payload_type = recv_codecs[i].codec.id;
1935 decoder.payload_name = recv_codecs[i].codec.name;
1936 config_.decoders.push_back(decoder);
1937 }
1938
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001939 // TODO(pbos): Reconfigure RTX based on incoming recv_codecs.
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001940 config_.rtp.fec = recv_codecs.front().fec;
pbos@webrtc.org257e1302014-07-25 19:01:32 +00001941 config_.rtp.nack.rtp_history_ms =
1942 IsNackEnabled(recv_codecs.begin()->codec) ? kNackHistoryMs : 0;
1943 config_.rtp.remb = IsRembEnabled(recv_codecs.begin()->codec);
1944
pbos@webrtc.org96a93252014-11-03 14:46:44 +00001945 ClearDecoders(&old_decoders);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001946 RecreateWebRtcStream();
1947}
1948
1949void WebRtcVideoChannel2::WebRtcVideoReceiveStream::SetRtpExtensions(
1950 const std::vector<webrtc::RtpExtension>& extensions) {
1951 config_.rtp.extensions = extensions;
1952 RecreateWebRtcStream();
1953}
1954
1955void WebRtcVideoChannel2::WebRtcVideoReceiveStream::RecreateWebRtcStream() {
1956 if (stream_ != NULL) {
1957 call_->DestroyVideoReceiveStream(stream_);
1958 }
1959 stream_ = call_->CreateVideoReceiveStream(config_);
1960 stream_->Start();
1961}
1962
pbos@webrtc.org96a93252014-11-03 14:46:44 +00001963void WebRtcVideoChannel2::WebRtcVideoReceiveStream::ClearDecoders(
1964 std::vector<AllocatedDecoder>* allocated_decoders) {
1965 for (size_t i = 0; i < allocated_decoders->size(); ++i) {
1966 if ((*allocated_decoders)[i].external) {
pbos@webrtc.org776e6f22014-10-29 15:28:39 +00001967 external_decoder_factory_->DestroyVideoDecoder(
pbos@webrtc.org96a93252014-11-03 14:46:44 +00001968 (*allocated_decoders)[i].decoder);
pbos@webrtc.org776e6f22014-10-29 15:28:39 +00001969 } else {
pbos@webrtc.org96a93252014-11-03 14:46:44 +00001970 delete (*allocated_decoders)[i].decoder;
pbos@webrtc.org776e6f22014-10-29 15:28:39 +00001971 }
1972 }
pbos@webrtc.org96a93252014-11-03 14:46:44 +00001973 allocated_decoders->clear();
pbos@webrtc.org776e6f22014-10-29 15:28:39 +00001974}
1975
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001976void WebRtcVideoChannel2::WebRtcVideoReceiveStream::RenderFrame(
1977 const webrtc::I420VideoFrame& frame,
1978 int time_to_render_ms) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001979 rtc::CritScope crit(&renderer_lock_);
magjed@webrtc.orgfc5ad952015-01-27 09:57:01 +00001980
1981 if (first_frame_timestamp_ < 0)
1982 first_frame_timestamp_ = frame.timestamp();
1983 int64_t rtp_time_elapsed_since_first_frame =
1984 (timestamp_wraparound_handler_.Unwrap(frame.timestamp()) -
1985 first_frame_timestamp_);
1986 int64_t elapsed_time_ms = rtp_time_elapsed_since_first_frame /
1987 (cricket::kVideoCodecClockrate / 1000);
1988 if (frame.ntp_time_ms() > 0)
1989 estimated_remote_start_ntp_time_ms_ = frame.ntp_time_ms() - elapsed_time_ms;
1990
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001991 if (renderer_ == NULL) {
1992 LOG(LS_WARNING) << "VideoReceiveStream not connected to a VideoRenderer.";
1993 return;
1994 }
1995
1996 if (frame.width() != last_width_ || frame.height() != last_height_) {
1997 SetSize(frame.width(), frame.height());
1998 }
1999
2000 LOG(LS_VERBOSE) << "RenderFrame: (" << frame.width() << "x" << frame.height()
2001 << ")";
2002
magjed@webrtc.orgfc5ad952015-01-27 09:57:01 +00002003 const WebRtcVideoRenderFrame render_frame(&frame, elapsed_time_ms);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00002004 renderer_->RenderFrame(&render_frame);
2005}
2006
pbos@webrtc.org0d852d52015-02-09 15:14:36 +00002007bool WebRtcVideoChannel2::WebRtcVideoReceiveStream::IsTextureSupported() const {
2008 return true;
2009}
2010
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00002011void WebRtcVideoChannel2::WebRtcVideoReceiveStream::SetRenderer(
2012 cricket::VideoRenderer* renderer) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002013 rtc::CritScope crit(&renderer_lock_);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00002014 renderer_ = renderer;
2015 if (renderer_ != NULL && last_width_ != -1) {
2016 SetSize(last_width_, last_height_);
2017 }
2018}
2019
2020VideoRenderer* WebRtcVideoChannel2::WebRtcVideoReceiveStream::GetRenderer() {
2021 // TODO(pbos): Remove GetRenderer and all uses of it, it's thread-unsafe by
2022 // design.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002023 rtc::CritScope crit(&renderer_lock_);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00002024 return renderer_;
2025}
2026
2027void WebRtcVideoChannel2::WebRtcVideoReceiveStream::SetSize(int width,
2028 int height) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002029 rtc::CritScope crit(&renderer_lock_);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00002030 if (!renderer_->SetSize(width, height, 0)) {
2031 LOG(LS_ERROR) << "Could not set renderer size.";
2032 }
2033 last_width_ = width;
2034 last_height_ = height;
2035}
2036
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00002037VideoReceiverInfo
2038WebRtcVideoChannel2::WebRtcVideoReceiveStream::GetVideoReceiverInfo() {
2039 VideoReceiverInfo info;
2040 info.add_ssrc(config_.rtp.remote_ssrc);
2041 webrtc::VideoReceiveStream::Stats stats = stream_->GetStats();
asapersson@webrtc.orgcfd82df2015-01-22 09:39:59 +00002042 info.bytes_rcvd = stats.rtp_stats.transmitted.payload_bytes +
2043 stats.rtp_stats.transmitted.header_bytes +
2044 stats.rtp_stats.transmitted.padding_bytes;
2045 info.packets_rcvd = stats.rtp_stats.transmitted.packets;
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00002046
2047 info.framerate_rcvd = stats.network_frame_rate;
2048 info.framerate_decoded = stats.decode_frame_rate;
2049 info.framerate_output = stats.render_frame_rate;
2050
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +00002051 {
2052 rtc::CritScope frame_cs(&renderer_lock_);
2053 info.frame_width = last_width_;
2054 info.frame_height = last_height_;
2055 info.capture_start_ntp_time_ms = estimated_remote_start_ntp_time_ms_;
2056 }
2057
pbos@webrtc.org09c77b92015-02-25 10:42:16 +00002058 info.decode_ms = stats.decode_ms;
2059 info.max_decode_ms = stats.max_decode_ms;
2060 info.current_delay_ms = stats.current_delay_ms;
2061 info.target_delay_ms = stats.target_delay_ms;
2062 info.jitter_buffer_ms = stats.jitter_buffer_ms;
2063 info.min_playout_delay_ms = stats.min_playout_delay_ms;
2064 info.render_delay_ms = stats.render_delay_ms;
2065
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +00002066 info.firs_sent = stats.rtcp_packet_type_counts.fir_packets;
2067 info.plis_sent = stats.rtcp_packet_type_counts.pli_packets;
2068 info.nacks_sent = stats.rtcp_packet_type_counts.nack_packets;
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00002069
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00002070 return info;
2071}
2072
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00002073WebRtcVideoChannel2::VideoCodecSettings::VideoCodecSettings()
2074 : rtx_payload_type(-1) {}
2075
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +00002076bool WebRtcVideoChannel2::VideoCodecSettings::operator==(
2077 const WebRtcVideoChannel2::VideoCodecSettings& other) const {
2078 return codec == other.codec &&
2079 fec.ulpfec_payload_type == other.fec.ulpfec_payload_type &&
2080 fec.red_payload_type == other.fec.red_payload_type &&
2081 rtx_payload_type == other.rtx_payload_type;
2082}
2083
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00002084std::vector<WebRtcVideoChannel2::VideoCodecSettings>
2085WebRtcVideoChannel2::MapCodecs(const std::vector<VideoCodec>& codecs) {
2086 assert(!codecs.empty());
2087
2088 std::vector<VideoCodecSettings> video_codecs;
2089 std::map<int, bool> payload_used;
pbos@webrtc.orge322a172014-06-13 11:47:28 +00002090 std::map<int, VideoCodec::CodecType> payload_codec_type;
pkasting@chromium.orgd3245462015-02-23 21:28:22 +00002091 // |rtx_mapping| maps video payload type to rtx payload type.
2092 std::map<int, int> rtx_mapping;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00002093
2094 webrtc::FecConfig fec_settings;
2095
2096 for (size_t i = 0; i < codecs.size(); ++i) {
2097 const VideoCodec& in_codec = codecs[i];
2098 int payload_type = in_codec.id;
2099
2100 if (payload_used[payload_type]) {
2101 LOG(LS_ERROR) << "Payload type already registered: "
2102 << in_codec.ToString();
2103 return std::vector<VideoCodecSettings>();
2104 }
2105 payload_used[payload_type] = true;
pbos@webrtc.orge322a172014-06-13 11:47:28 +00002106 payload_codec_type[payload_type] = in_codec.GetCodecType();
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00002107
2108 switch (in_codec.GetCodecType()) {
2109 case VideoCodec::CODEC_RED: {
2110 // RED payload type, should not have duplicates.
2111 assert(fec_settings.red_payload_type == -1);
2112 fec_settings.red_payload_type = in_codec.id;
2113 continue;
2114 }
2115
2116 case VideoCodec::CODEC_ULPFEC: {
2117 // ULPFEC payload type, should not have duplicates.
2118 assert(fec_settings.ulpfec_payload_type == -1);
2119 fec_settings.ulpfec_payload_type = in_codec.id;
2120 continue;
2121 }
2122
2123 case VideoCodec::CODEC_RTX: {
2124 int associated_payload_type;
2125 if (!in_codec.GetParam(kCodecParamAssociatedPayloadType,
pkasting@chromium.orge9facf82015-02-17 20:36:28 +00002126 &associated_payload_type) ||
2127 !IsValidRtpPayloadType(associated_payload_type)) {
2128 LOG(LS_ERROR)
2129 << "RTX codec with invalid or no associated payload type: "
2130 << in_codec.ToString();
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00002131 return std::vector<VideoCodecSettings>();
2132 }
2133 rtx_mapping[associated_payload_type] = in_codec.id;
2134 continue;
2135 }
2136
2137 case VideoCodec::CODEC_VIDEO:
2138 break;
2139 }
2140
2141 video_codecs.push_back(VideoCodecSettings());
2142 video_codecs.back().codec = in_codec;
2143 }
2144
2145 // One of these codecs should have been a video codec. Only having FEC
2146 // parameters into this code is a logic error.
2147 assert(!video_codecs.empty());
2148
pbos@webrtc.orge322a172014-06-13 11:47:28 +00002149 for (std::map<int, int>::const_iterator it = rtx_mapping.begin();
2150 it != rtx_mapping.end();
2151 ++it) {
2152 if (!payload_used[it->first]) {
2153 LOG(LS_ERROR) << "RTX mapped to payload not in codec list.";
2154 return std::vector<VideoCodecSettings>();
2155 }
andrew@webrtc.org8f27fcc2015-01-09 20:22:46 +00002156 if (payload_codec_type[it->first] != VideoCodec::CODEC_VIDEO) {
2157 LOG(LS_ERROR) << "RTX not mapped to regular video codec.";
pbos@webrtc.orge322a172014-06-13 11:47:28 +00002158 return std::vector<VideoCodecSettings>();
2159 }
2160 }
2161
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00002162 // TODO(pbos): Write tests that figure out that I have not verified that RTX
2163 // codecs aren't mapped to bogus payloads.
2164 for (size_t i = 0; i < video_codecs.size(); ++i) {
2165 video_codecs[i].fec = fec_settings;
andrew@webrtc.org8f27fcc2015-01-09 20:22:46 +00002166 if (rtx_mapping[video_codecs[i].codec.id] != 0) {
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00002167 video_codecs[i].rtx_payload_type = rtx_mapping[video_codecs[i].codec.id];
2168 }
2169 }
2170
2171 return video_codecs;
2172}
2173
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00002174} // namespace cricket
2175
2176#endif // HAVE_WEBRTC_VIDEO