blob: 8d53f17080b30a994b1755dc8288bc4b6bf2e668 [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.org3c107582014-07-20 15:27:35 +000031#include <set>
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +000032#include <string>
33
34#include "libyuv/convert_from.h"
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +000035#include "talk/media/base/videocapturer.h"
36#include "talk/media/base/videorenderer.h"
buildbot@webrtc.orgdf9bbbe2014-06-19 19:54:33 +000037#include "talk/media/webrtc/constants.h"
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +000038#include "talk/media/webrtc/webrtcvideocapturer.h"
andresp@webrtc.org82775b12014-11-07 09:37:54 +000039#include "talk/media/webrtc/webrtcvideoengine.h"
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +000040#include "talk/media/webrtc/webrtcvideoframe.h"
41#include "talk/media/webrtc/webrtcvoiceengine.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000042#include "webrtc/base/buffer.h"
43#include "webrtc/base/logging.h"
44#include "webrtc/base/stringutils.h"
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +000045#include "webrtc/call.h"
pbos@webrtc.org776e6f22014-10-29 15:28:39 +000046#include "webrtc/video_decoder.h"
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000047#include "webrtc/video_encoder.h"
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +000048
49#define UNIMPLEMENTED \
50 LOG(LS_ERROR) << "Call to unimplemented function " << __FUNCTION__; \
51 ASSERT(false)
52
53namespace cricket {
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +000054namespace {
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +000055static std::string CodecVectorToString(const std::vector<VideoCodec>& codecs) {
56 std::stringstream out;
57 out << '{';
58 for (size_t i = 0; i < codecs.size(); ++i) {
59 out << codecs[i].ToString();
60 if (i != codecs.size() - 1) {
61 out << ", ";
62 }
63 }
64 out << '}';
65 return out.str();
66}
67
68static bool ValidateCodecFormats(const std::vector<VideoCodec>& codecs) {
69 bool has_video = false;
70 for (size_t i = 0; i < codecs.size(); ++i) {
71 if (!codecs[i].ValidateCodecFormat()) {
72 return false;
73 }
74 if (codecs[i].GetCodecType() == VideoCodec::CODEC_VIDEO) {
75 has_video = true;
76 }
77 }
78 if (!has_video) {
79 LOG(LS_ERROR) << "Setting codecs without a video codec is invalid: "
80 << CodecVectorToString(codecs);
81 return false;
82 }
83 return true;
84}
85
86static std::string RtpExtensionsToString(
87 const std::vector<RtpHeaderExtension>& extensions) {
88 std::stringstream out;
89 out << '{';
90 for (size_t i = 0; i < extensions.size(); ++i) {
91 out << "{" << extensions[i].uri << ": " << extensions[i].id << "}";
92 if (i != extensions.size() - 1) {
93 out << ", ";
94 }
95 }
96 out << '}';
97 return out.str();
98}
99
andresp@webrtc.org82775b12014-11-07 09:37:54 +0000100// Merges two fec configs and logs an error if a conflict arises
101// such that merging in diferent order would trigger a diferent output.
102static void MergeFecConfig(const webrtc::FecConfig& other,
103 webrtc::FecConfig* output) {
104 if (other.ulpfec_payload_type != -1) {
105 if (output->ulpfec_payload_type != -1 &&
106 output->ulpfec_payload_type != other.ulpfec_payload_type) {
107 LOG(LS_WARNING) << "Conflict merging ulpfec_payload_type configs: "
108 << output->ulpfec_payload_type << " and "
109 << other.ulpfec_payload_type;
110 }
111 output->ulpfec_payload_type = other.ulpfec_payload_type;
112 }
113 if (other.red_payload_type != -1) {
114 if (output->red_payload_type != -1 &&
115 output->red_payload_type != other.red_payload_type) {
116 LOG(LS_WARNING) << "Conflict merging red_payload_type configs: "
117 << output->red_payload_type << " and "
118 << other.red_payload_type;
119 }
120 output->red_payload_type = other.red_payload_type;
121 }
122}
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000123} // namespace
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000124
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000125// This constant is really an on/off, lower-level configurable NACK history
126// duration hasn't been implemented.
127static const int kNackHistoryMs = 1000;
128
buildbot@webrtc.org933d88a2014-09-18 20:23:05 +0000129static const int kDefaultQpMax = 56;
130
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000131static const int kDefaultRtcpReceiverReportSsrc = 1;
132
pbos@webrtc.orgb7ed7792014-10-31 13:08:10 +0000133static const int kConferenceModeTemporalLayerBitrateBps = 100000;
134
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000135// External video encoders are given payloads 120-127. This also means that we
136// only support up to 8 external payload types.
137static const int kExternalVideoPayloadTypeBase = 120;
138#ifndef NDEBUG
139static const size_t kMaxExternalVideoCodecs = 8;
140#endif
141
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000142const char kH264CodecName[] = "H264";
143
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000144static bool FindFirstMatchingCodec(const std::vector<VideoCodec>& codecs,
145 const VideoCodec& requested_codec,
146 VideoCodec* matching_codec) {
147 for (size_t i = 0; i < codecs.size(); ++i) {
148 if (requested_codec.Matches(codecs[i])) {
149 *matching_codec = codecs[i];
150 return true;
151 }
152 }
153 return false;
154}
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000155
pbos@webrtc.org3c107582014-07-20 15:27:35 +0000156static bool ValidateRtpHeaderExtensionIds(
157 const std::vector<RtpHeaderExtension>& extensions) {
158 std::set<int> extensions_used;
159 for (size_t i = 0; i < extensions.size(); ++i) {
160 if (extensions[i].id < 0 || extensions[i].id >= 15 ||
161 !extensions_used.insert(extensions[i].id).second) {
162 LOG(LS_ERROR) << "RTP extensions are with incorrect or duplicate ids.";
163 return false;
164 }
165 }
166 return true;
167}
168
169static std::vector<webrtc::RtpExtension> FilterRtpExtensions(
170 const std::vector<RtpHeaderExtension>& extensions) {
171 std::vector<webrtc::RtpExtension> webrtc_extensions;
172 for (size_t i = 0; i < extensions.size(); ++i) {
173 // Unsupported extensions will be ignored.
174 if (webrtc::RtpExtension::IsSupported(extensions[i].uri)) {
175 webrtc_extensions.push_back(webrtc::RtpExtension(
176 extensions[i].uri, extensions[i].id));
177 } else {
178 LOG(LS_WARNING) << "Unsupported RTP extension: " << extensions[i].uri;
179 }
180 }
181 return webrtc_extensions;
182}
183
pbos@webrtc.org0d523ee2014-06-05 09:10:55 +0000184WebRtcVideoEncoderFactory2::~WebRtcVideoEncoderFactory2() {
185}
186
buildbot@webrtc.orgd41eaeb2014-06-12 07:13:26 +0000187std::vector<webrtc::VideoStream> WebRtcVideoEncoderFactory2::CreateVideoStreams(
188 const VideoCodec& codec,
189 const VideoOptions& options,
190 size_t num_streams) {
buildbot@webrtc.orgd41eaeb2014-06-12 07:13:26 +0000191 if (num_streams != 1) {
pbos@webrtc.org88ef6322014-11-04 15:29:29 +0000192 LOG(LS_WARNING) << "Unsupported number of streams (" << num_streams
193 << "), falling back to one.";
194 num_streams = 1;
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000195 }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000196
buildbot@webrtc.orgd41eaeb2014-06-12 07:13:26 +0000197 webrtc::VideoStream stream;
198 stream.width = codec.width;
199 stream.height = codec.height;
200 stream.max_framerate =
andresp@webrtc.org82775b12014-11-07 09:37:54 +0000201 codec.framerate != 0 ? codec.framerate : kDefaultVideoMaxFramerate;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000202
buildbot@webrtc.orgd41eaeb2014-06-12 07:13:26 +0000203 int min_bitrate = kMinVideoBitrate;
204 codec.GetParam(kCodecParamMinBitrate, &min_bitrate);
pbos@webrtc.org88ef6322014-11-04 15:29:29 +0000205 // Clamp the min video bitrate, this is set from JavaScript directly and needs
206 // to be sanitized.
207 if (min_bitrate < kMinVideoBitrate) {
208 min_bitrate = kMinVideoBitrate;
209 }
210
buildbot@webrtc.orgd41eaeb2014-06-12 07:13:26 +0000211 int max_bitrate = kMaxVideoBitrate;
212 codec.GetParam(kCodecParamMaxBitrate, &max_bitrate);
213 stream.min_bitrate_bps = min_bitrate * 1000;
214 stream.target_bitrate_bps = stream.max_bitrate_bps = max_bitrate * 1000;
215
buildbot@webrtc.org933d88a2014-09-18 20:23:05 +0000216 int max_qp = kDefaultQpMax;
buildbot@webrtc.orgd41eaeb2014-06-12 07:13:26 +0000217 codec.GetParam(kCodecParamMaxQuantization, &max_qp);
218 stream.max_qp = max_qp;
219 std::vector<webrtc::VideoStream> streams;
220 streams.push_back(stream);
221 return streams;
222}
223
pbos@webrtc.org6f48f1b2014-07-22 16:29:54 +0000224void* WebRtcVideoEncoderFactory2::CreateVideoEncoderSettings(
225 const VideoCodec& codec,
226 const VideoOptions& options) {
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000227 if (CodecNameMatches(codec.name, kVp8CodecName)) {
pbos@webrtc.org6cd6ba82014-09-18 12:42:28 +0000228 webrtc::VideoCodecVP8* settings = new webrtc::VideoCodecVP8(
229 webrtc::VideoEncoder::GetDefaultVp8Settings());
pbos@webrtc.org6f48f1b2014-07-22 16:29:54 +0000230 options.video_noise_reduction.Get(&settings->denoisingOn);
pbos@webrtc.org6f48f1b2014-07-22 16:29:54 +0000231 return settings;
232 }
233 return NULL;
234}
235
236void WebRtcVideoEncoderFactory2::DestroyVideoEncoderSettings(
237 const VideoCodec& codec,
238 void* encoder_settings) {
pbos@webrtc.org6f48f1b2014-07-22 16:29:54 +0000239 if (encoder_settings == NULL) {
240 return;
241 }
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000242 if (CodecNameMatches(codec.name, kVp8CodecName)) {
pbos@webrtc.org6f48f1b2014-07-22 16:29:54 +0000243 delete reinterpret_cast<webrtc::VideoCodecVP8*>(encoder_settings);
pbos@webrtc.org6f48f1b2014-07-22 16:29:54 +0000244 }
buildbot@webrtc.orgd41eaeb2014-06-12 07:13:26 +0000245}
246
pbos@webrtc.orgafb554f42014-08-12 23:17:13 +0000247DefaultUnsignalledSsrcHandler::DefaultUnsignalledSsrcHandler()
248 : default_recv_ssrc_(0), default_renderer_(NULL) {}
249
250UnsignalledSsrcHandler::Action DefaultUnsignalledSsrcHandler::OnUnsignalledSsrc(
251 VideoMediaChannel* channel,
252 uint32_t ssrc) {
253 if (default_recv_ssrc_ != 0) { // Already one default stream.
254 LOG(LS_WARNING) << "Unknown SSRC, but default receive stream already set.";
255 return kDropPacket;
256 }
257
258 StreamParams sp;
259 sp.ssrcs.push_back(ssrc);
260 LOG(LS_INFO) << "Creating default receive stream for SSRC=" << ssrc << ".";
261 if (!channel->AddRecvStream(sp)) {
262 LOG(LS_WARNING) << "Could not create default receive stream.";
263 }
264
265 channel->SetRenderer(ssrc, default_renderer_);
266 default_recv_ssrc_ = ssrc;
267 return kDeliverPacket;
268}
269
pbos@webrtc.org42684be2014-10-03 11:25:45 +0000270WebRtcCallFactory::~WebRtcCallFactory() {
271}
272webrtc::Call* WebRtcCallFactory::CreateCall(
273 const webrtc::Call::Config& config) {
274 return webrtc::Call::Create(config);
275}
276
pbos@webrtc.orgafb554f42014-08-12 23:17:13 +0000277VideoRenderer* DefaultUnsignalledSsrcHandler::GetDefaultRenderer() const {
278 return default_renderer_;
279}
280
281void DefaultUnsignalledSsrcHandler::SetDefaultRenderer(
282 VideoMediaChannel* channel,
283 VideoRenderer* renderer) {
284 default_renderer_ = renderer;
285 if (default_recv_ssrc_ != 0) {
286 channel->SetRenderer(default_recv_ssrc_, default_renderer_);
287 }
288}
289
pbos@webrtc.org97fdeb82014-08-22 10:36:23 +0000290WebRtcVideoEngine2::WebRtcVideoEngine2()
pbos@webrtc.orgb648b9d2014-08-26 11:08:06 +0000291 : worker_thread_(NULL),
292 voice_engine_(NULL),
andresp@webrtc.org82775b12014-11-07 09:37:54 +0000293 default_codec_format_(kDefaultVideoMaxWidth,
294 kDefaultVideoMaxHeight,
295 FPS_TO_INTERVAL(kDefaultVideoMaxFramerate),
buildbot@webrtc.org992febb2014-09-05 16:39:08 +0000296 FOURCC_ANY),
pbos@webrtc.orgb648b9d2014-08-26 11:08:06 +0000297 initialized_(false),
298 cpu_monitor_(new rtc::CpuMonitor(NULL)),
pbos@webrtc.org42684be2014-10-03 11:25:45 +0000299 call_factory_(&default_call_factory_),
pbos@webrtc.org0a2087a2014-09-23 09:40:22 +0000300 external_decoder_factory_(NULL),
301 external_encoder_factory_(NULL) {
pbos@webrtc.orgb648b9d2014-08-26 11:08:06 +0000302 LOG(LS_INFO) << "WebRtcVideoEngine2::WebRtcVideoEngine2()";
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000303 video_codecs_ = GetSupportedCodecs();
pbos@webrtc.org587ef602014-06-16 17:32:02 +0000304 rtp_header_extensions_.push_back(
305 RtpHeaderExtension(kRtpTimestampOffsetHeaderExtension,
306 kRtpTimestampOffsetHeaderExtensionDefaultId));
307 rtp_header_extensions_.push_back(
308 RtpHeaderExtension(kRtpAbsoluteSenderTimeHeaderExtension,
309 kRtpAbsoluteSenderTimeHeaderExtensionDefaultId));
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000310}
311
312WebRtcVideoEngine2::~WebRtcVideoEngine2() {
313 LOG(LS_INFO) << "WebRtcVideoEngine2::~WebRtcVideoEngine2";
314
315 if (initialized_) {
316 Terminate();
317 }
318}
319
pbos@webrtc.org42684be2014-10-03 11:25:45 +0000320void WebRtcVideoEngine2::SetCallFactory(WebRtcCallFactory* call_factory) {
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000321 assert(!initialized_);
pbos@webrtc.org42684be2014-10-03 11:25:45 +0000322 call_factory_ = call_factory;
323}
324
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000325bool WebRtcVideoEngine2::Init(rtc::Thread* worker_thread) {
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000326 LOG(LS_INFO) << "WebRtcVideoEngine2::Init";
327 worker_thread_ = worker_thread;
328 ASSERT(worker_thread_ != NULL);
329
330 cpu_monitor_->set_thread(worker_thread_);
331 if (!cpu_monitor_->Start(kCpuMonitorPeriodMs)) {
332 LOG(LS_ERROR) << "Failed to start CPU monitor.";
333 cpu_monitor_.reset();
334 }
335
336 initialized_ = true;
337 return true;
338}
339
340void WebRtcVideoEngine2::Terminate() {
341 LOG(LS_INFO) << "WebRtcVideoEngine2::Terminate";
342
343 cpu_monitor_->Stop();
344
345 initialized_ = false;
346}
347
348int WebRtcVideoEngine2::GetCapabilities() { return VIDEO_RECV | VIDEO_SEND; }
349
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000350bool WebRtcVideoEngine2::SetDefaultEncoderConfig(
351 const VideoEncoderConfig& config) {
pbos@webrtc.org8fdeee62014-07-20 14:40:23 +0000352 const VideoCodec& codec = config.max_codec;
353 // TODO(pbos): Make use of external encoder factory.
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000354 if (!CodecIsInternallySupported(codec.name)) {
pbos@webrtc.org8fdeee62014-07-20 14:40:23 +0000355 LOG(LS_ERROR) << "SetDefaultEncoderConfig, codec not supported:"
356 << codec.ToString();
357 return false;
358 }
359
buildbot@webrtc.org992febb2014-09-05 16:39:08 +0000360 default_codec_format_ =
361 VideoFormat(codec.width,
362 codec.height,
363 VideoFormat::FpsToInterval(codec.framerate),
364 FOURCC_ANY);
pbos@webrtc.org8fdeee62014-07-20 14:40:23 +0000365 video_codecs_.clear();
366 video_codecs_.push_back(codec);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000367 return true;
368}
369
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000370WebRtcVideoChannel2* WebRtcVideoEngine2::CreateChannel(
buildbot@webrtc.org1ecbe452014-10-14 20:29:28 +0000371 const VideoOptions& options,
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000372 VoiceMediaChannel* voice_channel) {
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000373 assert(initialized_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000374 LOG(LS_INFO) << "CreateChannel: "
375 << (voice_channel != NULL ? "With" : "Without")
pbos@webrtc.orgfa553ef2014-10-20 11:07:07 +0000376 << " voice channel. Options: " << options.ToString();
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000377 WebRtcVideoChannel2* channel =
378 new WebRtcVideoChannel2(call_factory_,
pbos@webrtc.org3bf3d232014-10-31 12:59:34 +0000379 voice_engine_,
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000380 voice_channel,
pbos@webrtc.orgfa553ef2014-10-20 11:07:07 +0000381 options,
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000382 external_encoder_factory_,
383 external_decoder_factory_,
384 GetVideoEncoderFactory());
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000385 if (!channel->Init()) {
386 delete channel;
387 return NULL;
388 }
pbos@webrtc.orge322a172014-06-13 11:47:28 +0000389 channel->SetRecvCodecs(video_codecs_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000390 return channel;
391}
392
393const std::vector<VideoCodec>& WebRtcVideoEngine2::codecs() const {
394 return video_codecs_;
395}
396
397const std::vector<RtpHeaderExtension>&
398WebRtcVideoEngine2::rtp_header_extensions() const {
399 return rtp_header_extensions_;
400}
401
402void WebRtcVideoEngine2::SetLogging(int min_sev, const char* filter) {
403 // TODO(pbos): Set up logging.
404 LOG(LS_VERBOSE) << "SetLogging: " << min_sev << '"' << filter << '"';
405 // if min_sev == -1, we keep the current log level.
406 if (min_sev < 0) {
407 assert(min_sev == -1);
408 return;
409 }
410}
411
pbos@webrtc.org0a2087a2014-09-23 09:40:22 +0000412void WebRtcVideoEngine2::SetExternalDecoderFactory(
413 WebRtcVideoDecoderFactory* decoder_factory) {
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000414 assert(!initialized_);
pbos@webrtc.org0a2087a2014-09-23 09:40:22 +0000415 external_decoder_factory_ = decoder_factory;
416}
417
418void WebRtcVideoEngine2::SetExternalEncoderFactory(
419 WebRtcVideoEncoderFactory* encoder_factory) {
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000420 assert(!initialized_);
pbos@webrtc.org0a2087a2014-09-23 09:40:22 +0000421 external_encoder_factory_ = encoder_factory;
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000422
423 video_codecs_ = GetSupportedCodecs();
pbos@webrtc.org0a2087a2014-09-23 09:40:22 +0000424}
425
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000426bool WebRtcVideoEngine2::EnableTimedRender() {
427 // TODO(pbos): Figure out whether this can be removed.
428 return true;
429}
430
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000431// Checks to see whether we comprehend and could receive a particular codec
432bool WebRtcVideoEngine2::FindCodec(const VideoCodec& in) {
433 // TODO(pbos): Probe encoder factory to figure out that the codec is supported
434 // if supported by the encoder factory. Add a corresponding test that fails
435 // with this code (that doesn't ask the factory).
pbos@webrtc.org8fdeee62014-07-20 14:40:23 +0000436 for (size_t j = 0; j < video_codecs_.size(); ++j) {
437 VideoCodec codec(video_codecs_[j].id, video_codecs_[j].name, 0, 0, 0, 0);
438 if (codec.Matches(in)) {
439 return true;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000440 }
441 }
442 return false;
443}
444
445// Tells whether the |requested| codec can be transmitted or not. If it can be
446// transmitted |out| is set with the best settings supported. Aspect ratio will
447// be set as close to |current|'s as possible. If not set |requested|'s
448// dimensions will be used for aspect ratio matching.
449bool WebRtcVideoEngine2::CanSendCodec(const VideoCodec& requested,
450 const VideoCodec& current,
451 VideoCodec* out) {
452 assert(out != NULL);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000453
454 if (requested.width != requested.height &&
455 (requested.height == 0 || requested.width == 0)) {
456 // 0xn and nx0 are invalid resolutions.
457 return false;
458 }
459
460 VideoCodec matching_codec;
461 if (!FindFirstMatchingCodec(video_codecs_, requested, &matching_codec)) {
462 // Codec not supported.
463 return false;
464 }
465
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000466 out->id = requested.id;
467 out->name = requested.name;
468 out->preference = requested.preference;
469 out->params = requested.params;
470 out->framerate =
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000471 rtc::_min(requested.framerate, matching_codec.framerate);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000472 out->params = requested.params;
473 out->feedback_params = requested.feedback_params;
pbos@webrtc.org8fdeee62014-07-20 14:40:23 +0000474 out->width = requested.width;
475 out->height = requested.height;
476 if (requested.width == 0 && requested.height == 0) {
477 return true;
478 }
479
480 while (out->width > matching_codec.width) {
481 out->width /= 2;
482 out->height /= 2;
483 }
484
485 return out->width > 0 && out->height > 0;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000486}
487
488bool WebRtcVideoEngine2::SetVoiceEngine(WebRtcVoiceEngine* voice_engine) {
489 if (initialized_) {
490 LOG(LS_WARNING) << "SetVoiceEngine can not be called after Init";
491 return false;
492 }
493 voice_engine_ = voice_engine;
494 return true;
495}
496
497// Ignore spammy trace messages, mostly from the stats API when we haven't
498// gotten RTCP info yet from the remote side.
499bool WebRtcVideoEngine2::ShouldIgnoreTrace(const std::string& trace) {
500 static const char* const kTracesToIgnore[] = {NULL};
501 for (const char* const* p = kTracesToIgnore; *p; ++p) {
502 if (trace.find(*p) == 0) {
503 return true;
504 }
505 }
506 return false;
507}
508
buildbot@webrtc.orgd41eaeb2014-06-12 07:13:26 +0000509WebRtcVideoEncoderFactory2* WebRtcVideoEngine2::GetVideoEncoderFactory() {
510 return &default_video_encoder_factory_;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000511}
512
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000513std::vector<VideoCodec> WebRtcVideoEngine2::GetSupportedCodecs() const {
andresp@webrtc.org82775b12014-11-07 09:37:54 +0000514 std::vector<VideoCodec> supported_codecs = DefaultVideoCodecList();
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000515
516 if (external_encoder_factory_ == NULL) {
517 return supported_codecs;
518 }
519
520 assert(external_encoder_factory_->codecs().size() <= kMaxExternalVideoCodecs);
521 const std::vector<WebRtcVideoEncoderFactory::VideoCodec>& codecs =
522 external_encoder_factory_->codecs();
523 for (size_t i = 0; i < codecs.size(); ++i) {
524 // Don't add internally-supported codecs twice.
525 if (CodecIsInternallySupported(codecs[i].name)) {
526 continue;
527 }
528
529 VideoCodec codec(kExternalVideoPayloadTypeBase + static_cast<int>(i),
530 codecs[i].name,
531 codecs[i].max_width,
532 codecs[i].max_height,
533 codecs[i].max_fps,
534 0);
535
536 AddDefaultFeedbackParams(&codec);
537 supported_codecs.push_back(codec);
538 }
539 return supported_codecs;
540}
541
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000542// Thin map between VideoFrame and an existing webrtc::I420VideoFrame
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000543// to avoid having to copy the rendered VideoFrame prematurely.
544// This implementation is only safe to use in a const context and should never
545// be written to.
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000546class WebRtcVideoRenderFrame : public VideoFrame {
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000547 public:
548 explicit WebRtcVideoRenderFrame(const webrtc::I420VideoFrame* frame)
549 : frame_(frame) {}
550
551 virtual bool InitToBlack(int w,
552 int h,
553 size_t pixel_width,
554 size_t pixel_height,
555 int64 elapsed_time,
556 int64 time_stamp) OVERRIDE {
557 UNIMPLEMENTED;
558 return false;
559 }
560
561 virtual bool Reset(uint32 fourcc,
562 int w,
563 int h,
564 int dw,
565 int dh,
566 uint8* sample,
567 size_t sample_size,
568 size_t pixel_width,
569 size_t pixel_height,
570 int64 elapsed_time,
571 int64 time_stamp,
572 int rotation) OVERRIDE {
573 UNIMPLEMENTED;
574 return false;
575 }
576
577 virtual size_t GetWidth() const OVERRIDE {
578 return static_cast<size_t>(frame_->width());
579 }
580 virtual size_t GetHeight() const OVERRIDE {
581 return static_cast<size_t>(frame_->height());
582 }
583
584 virtual const uint8* GetYPlane() const OVERRIDE {
585 return frame_->buffer(webrtc::kYPlane);
586 }
587 virtual const uint8* GetUPlane() const OVERRIDE {
588 return frame_->buffer(webrtc::kUPlane);
589 }
590 virtual const uint8* GetVPlane() const OVERRIDE {
591 return frame_->buffer(webrtc::kVPlane);
592 }
593
594 virtual uint8* GetYPlane() OVERRIDE {
595 UNIMPLEMENTED;
596 return NULL;
597 }
598 virtual uint8* GetUPlane() OVERRIDE {
599 UNIMPLEMENTED;
600 return NULL;
601 }
602 virtual uint8* GetVPlane() OVERRIDE {
603 UNIMPLEMENTED;
604 return NULL;
605 }
606
607 virtual int32 GetYPitch() const OVERRIDE {
608 return frame_->stride(webrtc::kYPlane);
609 }
610 virtual int32 GetUPitch() const OVERRIDE {
611 return frame_->stride(webrtc::kUPlane);
612 }
613 virtual int32 GetVPitch() const OVERRIDE {
614 return frame_->stride(webrtc::kVPlane);
615 }
616
617 virtual void* GetNativeHandle() const OVERRIDE { return NULL; }
618
619 virtual size_t GetPixelWidth() const OVERRIDE { return 1; }
620 virtual size_t GetPixelHeight() const OVERRIDE { return 1; }
621
622 virtual int64 GetElapsedTime() const OVERRIDE {
623 // Convert millisecond render time to ns timestamp.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000624 return frame_->render_time_ms() * rtc::kNumNanosecsPerMillisec;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000625 }
626 virtual int64 GetTimeStamp() const OVERRIDE {
627 // Convert 90K rtp timestamp to ns timestamp.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000628 return (frame_->timestamp() / 90) * rtc::kNumNanosecsPerMillisec;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000629 }
630 virtual void SetElapsedTime(int64 elapsed_time) OVERRIDE { UNIMPLEMENTED; }
631 virtual void SetTimeStamp(int64 time_stamp) OVERRIDE { UNIMPLEMENTED; }
632
633 virtual int GetRotation() const OVERRIDE {
634 UNIMPLEMENTED;
635 return ROTATION_0;
636 }
637
638 virtual VideoFrame* Copy() const OVERRIDE {
639 UNIMPLEMENTED;
640 return NULL;
641 }
642
643 virtual bool MakeExclusive() OVERRIDE {
644 UNIMPLEMENTED;
645 return false;
646 }
647
648 virtual size_t CopyToBuffer(uint8* buffer, size_t size) const {
649 UNIMPLEMENTED;
650 return 0;
651 }
652
653 // TODO(fbarchard): Refactor into base class and share with LMI
654 virtual size_t ConvertToRgbBuffer(uint32 to_fourcc,
655 uint8* buffer,
656 size_t size,
657 int stride_rgb) const OVERRIDE {
658 size_t width = GetWidth();
659 size_t height = GetHeight();
660 size_t needed = (stride_rgb >= 0 ? stride_rgb : -stride_rgb) * height;
661 if (size < needed) {
662 LOG(LS_WARNING) << "RGB buffer is not large enough";
663 return needed;
664 }
665
666 if (libyuv::ConvertFromI420(GetYPlane(),
667 GetYPitch(),
668 GetUPlane(),
669 GetUPitch(),
670 GetVPlane(),
671 GetVPitch(),
672 buffer,
673 stride_rgb,
674 static_cast<int>(width),
675 static_cast<int>(height),
676 to_fourcc)) {
677 LOG(LS_ERROR) << "RGB type not supported: " << to_fourcc;
678 return 0; // 0 indicates error
679 }
680 return needed;
681 }
682
683 protected:
684 virtual VideoFrame* CreateEmptyFrame(int w,
685 int h,
686 size_t pixel_width,
687 size_t pixel_height,
688 int64 elapsed_time,
689 int64 time_stamp) const OVERRIDE {
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000690 WebRtcVideoFrame* frame = new WebRtcVideoFrame();
691 frame->InitToBlack(
692 w, h, pixel_width, pixel_height, elapsed_time, time_stamp);
693 return frame;
694 }
695
696 private:
697 const webrtc::I420VideoFrame* const frame_;
698};
699
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000700WebRtcVideoChannel2::WebRtcVideoChannel2(
pbos@webrtc.org42684be2014-10-03 11:25:45 +0000701 WebRtcCallFactory* call_factory,
pbos@webrtc.org3bf3d232014-10-31 12:59:34 +0000702 WebRtcVoiceEngine* voice_engine,
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000703 VoiceMediaChannel* voice_channel,
pbos@webrtc.orgfa553ef2014-10-20 11:07:07 +0000704 const VideoOptions& options,
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000705 WebRtcVideoEncoderFactory* external_encoder_factory,
706 WebRtcVideoDecoderFactory* external_decoder_factory,
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000707 WebRtcVideoEncoderFactory2* encoder_factory)
pbos@webrtc.orgb648b9d2014-08-26 11:08:06 +0000708 : unsignalled_ssrc_handler_(&default_unsignalled_ssrc_handler_),
pbos@webrtc.org3bf3d232014-10-31 12:59:34 +0000709 voice_channel_(voice_channel),
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000710 external_encoder_factory_(external_encoder_factory),
711 external_decoder_factory_(external_decoder_factory),
pbos@webrtc.orgb648b9d2014-08-26 11:08:06 +0000712 encoder_factory_(encoder_factory) {
pbos@webrtc.orgfa553ef2014-10-20 11:07:07 +0000713 SetDefaultOptions();
714 options_.SetAll(options);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000715 webrtc::Call::Config config(this);
pbos@webrtc.org42684be2014-10-03 11:25:45 +0000716 config.overuse_callback = this;
pbos@webrtc.org3bf3d232014-10-31 12:59:34 +0000717 if (voice_engine != NULL) {
718 config.voice_engine = voice_engine->voe()->engine();
719 }
pbos@webrtc.orgfa553ef2014-10-20 11:07:07 +0000720
721 // Set start bitrate for the call. A default is provided by SetDefaultOptions.
722 int start_bitrate_kbps;
723 options_.video_start_bitrate.Get(&start_bitrate_kbps);
724 config.stream_start_bitrate_bps = start_bitrate_kbps * 1000;
725
pbos@webrtc.org42684be2014-10-03 11:25:45 +0000726 call_.reset(call_factory->CreateCall(config));
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000727
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000728 rtcp_receiver_report_ssrc_ = kDefaultRtcpReceiverReportSsrc;
729 sending_ = false;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000730 default_send_ssrc_ = 0;
pbos@webrtc.org6f48f1b2014-07-22 16:29:54 +0000731}
732
733void WebRtcVideoChannel2::SetDefaultOptions() {
pbos@webrtc.org42684be2014-10-03 11:25:45 +0000734 options_.cpu_overuse_detection.Set(false);
pbos@webrtc.org5ff71ab2014-07-23 07:28:56 +0000735 options_.suspend_below_min_bitrate.Set(false);
pbos@webrtc.org42684be2014-10-03 11:25:45 +0000736 options_.use_payload_padding.Set(false);
737 options_.video_noise_reduction.Set(true);
pbos@webrtc.orgfa553ef2014-10-20 11:07:07 +0000738 options_.video_start_bitrate.Set(
739 webrtc::Call::Config::kDefaultStartBitrateBps / 1000);
pbos@webrtc.orgefc82c22014-10-27 13:58:00 +0000740 options_.screencast_min_bitrate.Set(0);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000741}
742
743WebRtcVideoChannel2::~WebRtcVideoChannel2() {
744 for (std::map<uint32, WebRtcVideoSendStream*>::iterator it =
745 send_streams_.begin();
746 it != send_streams_.end();
747 ++it) {
748 delete it->second;
749 }
750
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000751 for (std::map<uint32, WebRtcVideoReceiveStream*>::iterator it =
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000752 receive_streams_.begin();
753 it != receive_streams_.end();
754 ++it) {
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000755 delete it->second;
756 }
757}
758
759bool WebRtcVideoChannel2::Init() { return true; }
760
pbos@webrtc.org96a93252014-11-03 14:46:44 +0000761bool WebRtcVideoChannel2::CodecIsExternallySupported(
762 const std::string& name) const {
763 if (external_encoder_factory_ == NULL) {
764 return false;
765 }
766
767 const std::vector<WebRtcVideoEncoderFactory::VideoCodec> external_codecs =
768 external_encoder_factory_->codecs();
769 for (size_t c = 0; c < external_codecs.size(); ++c) {
770 if (CodecNameMatches(name, external_codecs[c].name)) {
771 return true;
772 }
773 }
774 return false;
775}
776
777std::vector<WebRtcVideoChannel2::VideoCodecSettings>
778WebRtcVideoChannel2::FilterSupportedCodecs(
779 const std::vector<WebRtcVideoChannel2::VideoCodecSettings>& mapped_codecs)
780 const {
781 std::vector<VideoCodecSettings> supported_codecs;
782 for (size_t i = 0; i < mapped_codecs.size(); ++i) {
783 const VideoCodecSettings& codec = mapped_codecs[i];
784 if (CodecIsInternallySupported(codec.codec.name) ||
785 CodecIsExternallySupported(codec.codec.name)) {
786 supported_codecs.push_back(codec);
787 }
788 }
789 return supported_codecs;
790}
791
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000792bool WebRtcVideoChannel2::SetRecvCodecs(const std::vector<VideoCodec>& codecs) {
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000793 LOG(LS_INFO) << "SetRecvCodecs: " << CodecVectorToString(codecs);
794 if (!ValidateCodecFormats(codecs)) {
795 return false;
796 }
797
798 const std::vector<VideoCodecSettings> mapped_codecs = MapCodecs(codecs);
799 if (mapped_codecs.empty()) {
pbos@webrtc.org96a93252014-11-03 14:46:44 +0000800 LOG(LS_ERROR) << "SetRecvCodecs called without any video codecs.";
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000801 return false;
802 }
803
pbos@webrtc.org96a93252014-11-03 14:46:44 +0000804 const std::vector<VideoCodecSettings> supported_codecs =
805 FilterSupportedCodecs(mapped_codecs);
806
807 if (mapped_codecs.size() != supported_codecs.size()) {
808 LOG(LS_ERROR) << "SetRecvCodecs called with unsupported video codecs.";
809 return false;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000810 }
811
pbos@webrtc.org96a93252014-11-03 14:46:44 +0000812 recv_codecs_ = supported_codecs;
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000813
pbos@webrtc.org575d1262014-10-08 14:48:08 +0000814 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000815 for (std::map<uint32, WebRtcVideoReceiveStream*>::iterator it =
816 receive_streams_.begin();
817 it != receive_streams_.end();
818 ++it) {
819 it->second->SetRecvCodecs(recv_codecs_);
820 }
821
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000822 return true;
823}
824
825bool WebRtcVideoChannel2::SetSendCodecs(const std::vector<VideoCodec>& codecs) {
826 LOG(LS_INFO) << "SetSendCodecs: " << CodecVectorToString(codecs);
827 if (!ValidateCodecFormats(codecs)) {
828 return false;
829 }
830
831 const std::vector<VideoCodecSettings> supported_codecs =
832 FilterSupportedCodecs(MapCodecs(codecs));
833
834 if (supported_codecs.empty()) {
835 LOG(LS_ERROR) << "No video codecs supported by encoder factory.";
836 return false;
837 }
838
839 send_codec_.Set(supported_codecs.front());
840 LOG(LS_INFO) << "Using codec: " << supported_codecs.front().codec.ToString();
841
pbos@webrtc.org575d1262014-10-08 14:48:08 +0000842 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000843 for (std::map<uint32, WebRtcVideoSendStream*>::iterator it =
844 send_streams_.begin();
845 it != send_streams_.end();
846 ++it) {
847 assert(it->second != NULL);
848 it->second->SetCodec(supported_codecs.front());
849 }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000850
851 return true;
852}
853
854bool WebRtcVideoChannel2::GetSendCodec(VideoCodec* codec) {
855 VideoCodecSettings codec_settings;
856 if (!send_codec_.Get(&codec_settings)) {
857 LOG(LS_VERBOSE) << "GetSendCodec: No send codec set.";
858 return false;
859 }
860 *codec = codec_settings.codec;
861 return true;
862}
863
864bool WebRtcVideoChannel2::SetSendStreamFormat(uint32 ssrc,
865 const VideoFormat& format) {
866 LOG(LS_VERBOSE) << "SetSendStreamFormat:" << ssrc << " -> "
867 << format.ToString();
pbos@webrtc.org575d1262014-10-08 14:48:08 +0000868 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000869 if (send_streams_.find(ssrc) == send_streams_.end()) {
870 return false;
871 }
872 return send_streams_[ssrc]->SetVideoFormat(format);
873}
874
875bool WebRtcVideoChannel2::SetRender(bool render) {
876 // TODO(pbos): Implement. Or refactor away as it shouldn't be needed.
877 LOG(LS_VERBOSE) << "SetRender: " << (render ? "true" : "false");
878 return true;
879}
880
881bool WebRtcVideoChannel2::SetSend(bool send) {
882 LOG(LS_VERBOSE) << "SetSend: " << (send ? "true" : "false");
883 if (send && !send_codec_.IsSet()) {
884 LOG(LS_ERROR) << "SetSend(true) called before setting codec.";
885 return false;
886 }
887 if (send) {
888 StartAllSendStreams();
889 } else {
890 StopAllSendStreams();
891 }
892 sending_ = send;
893 return true;
894}
895
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000896bool WebRtcVideoChannel2::AddSendStream(const StreamParams& sp) {
897 LOG(LS_INFO) << "AddSendStream: " << sp.ToString();
898 if (sp.ssrcs.empty()) {
899 LOG(LS_ERROR) << "No SSRCs in stream parameters.";
900 return false;
901 }
902
903 uint32 ssrc = sp.first_ssrc();
904 assert(ssrc != 0);
905 // TODO(pbos): Make sure none of sp.ssrcs are used, not just the identifying
906 // ssrc.
pbos@webrtc.org575d1262014-10-08 14:48:08 +0000907 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000908 if (send_streams_.find(ssrc) != send_streams_.end()) {
909 LOG(LS_ERROR) << "Send stream with ssrc '" << ssrc << "' already exists.";
910 return false;
911 }
912
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +0000913 std::vector<uint32> primary_ssrcs;
914 sp.GetPrimarySsrcs(&primary_ssrcs);
915 std::vector<uint32> rtx_ssrcs;
916 sp.GetFidSsrcs(primary_ssrcs, &rtx_ssrcs);
917 if (!rtx_ssrcs.empty() && primary_ssrcs.size() != rtx_ssrcs.size()) {
918 LOG(LS_ERROR)
919 << "RTX SSRCs exist, but don't cover all SSRCs (unsupported): "
920 << sp.ToString();
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000921 return false;
922 }
923
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000924 WebRtcVideoSendStream* stream =
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000925 new WebRtcVideoSendStream(call_.get(),
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000926 external_encoder_factory_,
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +0000927 encoder_factory_,
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000928 options_,
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +0000929 send_codec_,
930 sp,
931 send_rtp_extensions_);
932
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000933 send_streams_[ssrc] = stream;
934
935 if (rtcp_receiver_report_ssrc_ == kDefaultRtcpReceiverReportSsrc) {
936 rtcp_receiver_report_ssrc_ = ssrc;
937 }
938 if (default_send_ssrc_ == 0) {
939 default_send_ssrc_ = ssrc;
940 }
941 if (sending_) {
942 stream->Start();
943 }
944
945 return true;
946}
947
948bool WebRtcVideoChannel2::RemoveSendStream(uint32 ssrc) {
949 LOG(LS_INFO) << "RemoveSendStream: " << ssrc;
950
951 if (ssrc == 0) {
952 if (default_send_ssrc_ == 0) {
953 LOG(LS_ERROR) << "No default send stream active.";
954 return false;
955 }
956
957 LOG(LS_VERBOSE) << "Removing default stream: " << default_send_ssrc_;
958 ssrc = default_send_ssrc_;
959 }
960
pbos@webrtc.org575d1262014-10-08 14:48:08 +0000961 WebRtcVideoSendStream* removed_stream;
962 {
963 rtc::CritScope stream_lock(&stream_crit_);
964 std::map<uint32, WebRtcVideoSendStream*>::iterator it =
965 send_streams_.find(ssrc);
966 if (it == send_streams_.end()) {
967 return false;
968 }
969
970 removed_stream = it->second;
971 send_streams_.erase(it);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000972 }
973
pbos@webrtc.org575d1262014-10-08 14:48:08 +0000974 delete removed_stream;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000975
976 if (ssrc == default_send_ssrc_) {
977 default_send_ssrc_ = 0;
978 }
979
980 return true;
981}
982
983bool WebRtcVideoChannel2::AddRecvStream(const StreamParams& sp) {
984 LOG(LS_INFO) << "AddRecvStream: " << sp.ToString();
985 assert(sp.ssrcs.size() > 0);
986
987 uint32 ssrc = sp.first_ssrc();
988 assert(ssrc != 0); // TODO(pbos): Is this ever valid?
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000989
990 // TODO(pbos): Check if any of the SSRCs overlap.
pbos@webrtc.org575d1262014-10-08 14:48:08 +0000991 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000992 if (receive_streams_.find(ssrc) != receive_streams_.end()) {
993 LOG(LS_ERROR) << "Receive stream for SSRC " << ssrc << "already exists.";
994 return false;
995 }
996
pbos@webrtc.orgbd249bc2014-07-07 04:45:15 +0000997 webrtc::VideoReceiveStream::Config config;
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000998 ConfigureReceiverRtp(&config, sp);
pbos@webrtc.org3bf3d232014-10-31 12:59:34 +0000999
1000 // Set up A/V sync if there is a VoiceChannel.
1001 // TODO(pbos): The A/V is synched by the receiving channel. So we need to know
1002 // the SSRC of the remote audio channel in order to sync the correct webrtc
1003 // VoiceEngine channel. For now sync the first channel in non-conference to
1004 // match existing behavior in WebRtcVideoEngine.
1005 if (voice_channel_ != NULL && receive_streams_.empty() &&
1006 !options_.conference_mode.GetWithDefaultIfUnset(false)) {
1007 config.audio_channel_id =
1008 static_cast<WebRtcVoiceMediaChannel*>(voice_channel_)->voe_channel();
1009 }
1010
pbos@webrtc.org776e6f22014-10-29 15:28:39 +00001011 receive_streams_[ssrc] = new WebRtcVideoReceiveStream(
1012 call_.get(), external_decoder_factory_, config, recv_codecs_);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001013
1014 return true;
1015}
1016
1017void WebRtcVideoChannel2::ConfigureReceiverRtp(
1018 webrtc::VideoReceiveStream::Config* config,
1019 const StreamParams& sp) const {
1020 uint32 ssrc = sp.first_ssrc();
1021
1022 config->rtp.remote_ssrc = ssrc;
1023 config->rtp.local_ssrc = rtcp_receiver_report_ssrc_;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001024
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001025 config->rtp.extensions = recv_rtp_extensions_;
pbos@webrtc.org257e1302014-07-25 19:01:32 +00001026
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001027 // TODO(pbos): This protection is against setting the same local ssrc as
1028 // remote which is not permitted by the lower-level API. RTCP requires a
1029 // corresponding sender SSRC. Figure out what to do when we don't have
1030 // (receive-only) or know a good local SSRC.
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001031 if (config->rtp.remote_ssrc == config->rtp.local_ssrc) {
1032 if (config->rtp.local_ssrc != kDefaultRtcpReceiverReportSsrc) {
1033 config->rtp.local_ssrc = kDefaultRtcpReceiverReportSsrc;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001034 } else {
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001035 config->rtp.local_ssrc = kDefaultRtcpReceiverReportSsrc + 1;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001036 }
1037 }
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001038
1039 for (size_t i = 0; i < recv_codecs_.size(); ++i) {
andresp@webrtc.org82775b12014-11-07 09:37:54 +00001040 MergeFecConfig(recv_codecs_[i].fec, &config->rtp.fec);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001041 }
1042
andresp@webrtc.org82775b12014-11-07 09:37:54 +00001043 for (size_t i = 0; i < recv_codecs_.size(); ++i) {
1044 uint32 rtx_ssrc;
1045 if (recv_codecs_[i].rtx_payload_type != -1 &&
1046 sp.GetFidSsrc(ssrc, &rtx_ssrc)) {
1047 webrtc::VideoReceiveStream::Config::Rtp::Rtx& rtx =
1048 config->rtp.rtx[recv_codecs_[i].codec.id];
1049 rtx.ssrc = rtx_ssrc;
1050 rtx.payload_type = recv_codecs_[i].rtx_payload_type;
1051 }
1052 }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001053}
1054
1055bool WebRtcVideoChannel2::RemoveRecvStream(uint32 ssrc) {
1056 LOG(LS_INFO) << "RemoveRecvStream: " << ssrc;
1057 if (ssrc == 0) {
pbos@webrtc.orgafb554f42014-08-12 23:17:13 +00001058 LOG(LS_ERROR) << "RemoveRecvStream with 0 ssrc is not supported.";
1059 return false;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001060 }
1061
pbos@webrtc.org575d1262014-10-08 14:48:08 +00001062 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001063 std::map<uint32, WebRtcVideoReceiveStream*>::iterator stream =
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001064 receive_streams_.find(ssrc);
1065 if (stream == receive_streams_.end()) {
1066 LOG(LS_ERROR) << "Stream not found for ssrc: " << ssrc;
1067 return false;
1068 }
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001069 delete stream->second;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001070 receive_streams_.erase(stream);
1071
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001072 return true;
1073}
1074
1075bool WebRtcVideoChannel2::SetRenderer(uint32 ssrc, VideoRenderer* renderer) {
1076 LOG(LS_INFO) << "SetRenderer: ssrc:" << ssrc << " "
1077 << (renderer ? "(ptr)" : "NULL");
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001078 if (ssrc == 0) {
pbos@webrtc.orgafb554f42014-08-12 23:17:13 +00001079 default_unsignalled_ssrc_handler_.SetDefaultRenderer(this, renderer);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001080 return true;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001081 }
1082
pbos@webrtc.org575d1262014-10-08 14:48:08 +00001083 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001084 std::map<uint32, WebRtcVideoReceiveStream*>::iterator it =
1085 receive_streams_.find(ssrc);
1086 if (it == receive_streams_.end()) {
1087 return false;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001088 }
1089
1090 it->second->SetRenderer(renderer);
1091 return true;
1092}
1093
1094bool WebRtcVideoChannel2::GetRenderer(uint32 ssrc, VideoRenderer** renderer) {
1095 if (ssrc == 0) {
pbos@webrtc.orgafb554f42014-08-12 23:17:13 +00001096 *renderer = default_unsignalled_ssrc_handler_.GetDefaultRenderer();
1097 return *renderer != NULL;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001098 }
1099
pbos@webrtc.org575d1262014-10-08 14:48:08 +00001100 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001101 std::map<uint32, WebRtcVideoReceiveStream*>::iterator it =
1102 receive_streams_.find(ssrc);
1103 if (it == receive_streams_.end()) {
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001104 return false;
1105 }
1106 *renderer = it->second->GetRenderer();
1107 return true;
1108}
1109
1110bool WebRtcVideoChannel2::GetStats(const StatsOptions& options,
1111 VideoMediaInfo* info) {
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00001112 info->Clear();
1113 FillSenderStats(info);
1114 FillReceiverStats(info);
1115 FillBandwidthEstimationStats(info);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001116 return true;
1117}
1118
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00001119void WebRtcVideoChannel2::FillSenderStats(VideoMediaInfo* video_media_info) {
pbos@webrtc.org575d1262014-10-08 14:48:08 +00001120 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00001121 for (std::map<uint32, WebRtcVideoSendStream*>::iterator it =
1122 send_streams_.begin();
1123 it != send_streams_.end();
1124 ++it) {
1125 video_media_info->senders.push_back(it->second->GetVideoSenderInfo());
1126 }
1127}
1128
1129void WebRtcVideoChannel2::FillReceiverStats(VideoMediaInfo* video_media_info) {
pbos@webrtc.org575d1262014-10-08 14:48:08 +00001130 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00001131 for (std::map<uint32, WebRtcVideoReceiveStream*>::iterator it =
1132 receive_streams_.begin();
1133 it != receive_streams_.end();
1134 ++it) {
1135 video_media_info->receivers.push_back(it->second->GetVideoReceiverInfo());
1136 }
1137}
1138
1139void WebRtcVideoChannel2::FillBandwidthEstimationStats(
1140 VideoMediaInfo* video_media_info) {
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001141 BandwidthEstimationInfo bwe_info;
1142 webrtc::Call::Stats stats = call_->GetStats();
1143 bwe_info.available_send_bandwidth = stats.send_bandwidth_bps;
1144 bwe_info.available_recv_bandwidth = stats.recv_bandwidth_bps;
1145 bwe_info.bucket_delay = stats.pacer_delay_ms;
1146
1147 // Get send stream bitrate stats.
1148 rtc::CritScope stream_lock(&stream_crit_);
1149 for (std::map<uint32, WebRtcVideoSendStream*>::iterator stream =
1150 send_streams_.begin();
1151 stream != send_streams_.end();
1152 ++stream) {
1153 stream->second->FillBandwidthEstimationInfo(&bwe_info);
1154 }
1155 video_media_info->bw_estimations.push_back(bwe_info);
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00001156}
1157
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001158bool WebRtcVideoChannel2::SetCapturer(uint32 ssrc, VideoCapturer* capturer) {
1159 LOG(LS_INFO) << "SetCapturer: " << ssrc << " -> "
1160 << (capturer != NULL ? "(capturer)" : "NULL");
1161 assert(ssrc != 0);
pbos@webrtc.org575d1262014-10-08 14:48:08 +00001162 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001163 if (send_streams_.find(ssrc) == send_streams_.end()) {
1164 LOG(LS_ERROR) << "No sending stream on ssrc " << ssrc;
1165 return false;
1166 }
1167 return send_streams_[ssrc]->SetCapturer(capturer);
1168}
1169
1170bool WebRtcVideoChannel2::SendIntraFrame() {
1171 // TODO(pbos): Implement.
1172 LOG(LS_VERBOSE) << "SendIntraFrame().";
1173 return true;
1174}
1175
1176bool WebRtcVideoChannel2::RequestIntraFrame() {
1177 // TODO(pbos): Implement.
1178 LOG(LS_VERBOSE) << "SendIntraFrame().";
1179 return true;
1180}
1181
1182void WebRtcVideoChannel2::OnPacketReceived(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001183 rtc::Buffer* packet,
1184 const rtc::PacketTime& packet_time) {
pbos@webrtc.org4e545cc2014-05-14 13:58:13 +00001185 const webrtc::PacketReceiver::DeliveryStatus delivery_result =
1186 call_->Receiver()->DeliverPacket(
1187 reinterpret_cast<const uint8_t*>(packet->data()), packet->length());
1188 switch (delivery_result) {
1189 case webrtc::PacketReceiver::DELIVERY_OK:
1190 return;
1191 case webrtc::PacketReceiver::DELIVERY_PACKET_ERROR:
1192 return;
1193 case webrtc::PacketReceiver::DELIVERY_UNKNOWN_SSRC:
1194 break;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001195 }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001196
1197 uint32 ssrc = 0;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001198 if (!GetRtpSsrc(packet->data(), packet->length(), &ssrc)) {
1199 return;
1200 }
1201
pbos@webrtc.orgafb554f42014-08-12 23:17:13 +00001202 // TODO(pbos): Make sure that the unsignalled SSRC uses the video payload.
1203 // Also figure out whether RTX needs to be handled.
1204 switch (unsignalled_ssrc_handler_->OnUnsignalledSsrc(this, ssrc)) {
1205 case UnsignalledSsrcHandler::kDropPacket:
1206 return;
1207 case UnsignalledSsrcHandler::kDeliverPacket:
1208 break;
1209 }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001210
pbos@webrtc.org1e019d12014-05-16 11:38:45 +00001211 if (call_->Receiver()->DeliverPacket(
1212 reinterpret_cast<const uint8_t*>(packet->data()), packet->length()) !=
1213 webrtc::PacketReceiver::DELIVERY_OK) {
pbos@webrtc.orgafb554f42014-08-12 23:17:13 +00001214 LOG(LS_WARNING) << "Failed to deliver RTP packet on re-delivery.";
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001215 return;
1216 }
1217}
1218
1219void WebRtcVideoChannel2::OnRtcpReceived(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001220 rtc::Buffer* packet,
1221 const rtc::PacketTime& packet_time) {
pbos@webrtc.org1e019d12014-05-16 11:38:45 +00001222 if (call_->Receiver()->DeliverPacket(
1223 reinterpret_cast<const uint8_t*>(packet->data()), packet->length()) !=
1224 webrtc::PacketReceiver::DELIVERY_OK) {
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001225 LOG(LS_WARNING) << "Failed to deliver RTCP packet.";
1226 }
1227}
1228
1229void WebRtcVideoChannel2::OnReadyToSend(bool ready) {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +00001230 LOG(LS_VERBOSE) << "OnReadyToSend: " << (ready ? "Ready." : "Not ready.");
1231 call_->SignalNetworkState(ready ? webrtc::Call::kNetworkUp
1232 : webrtc::Call::kNetworkDown);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001233}
1234
1235bool WebRtcVideoChannel2::MuteStream(uint32 ssrc, bool mute) {
1236 LOG(LS_VERBOSE) << "MuteStream: " << ssrc << " -> "
1237 << (mute ? "mute" : "unmute");
1238 assert(ssrc != 0);
pbos@webrtc.org575d1262014-10-08 14:48:08 +00001239 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001240 if (send_streams_.find(ssrc) == send_streams_.end()) {
1241 LOG(LS_ERROR) << "No sending stream on ssrc " << ssrc;
1242 return false;
1243 }
pbos@webrtc.orgef8bb8d2014-08-13 21:36:18 +00001244
1245 send_streams_[ssrc]->MuteStream(mute);
1246 return true;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001247}
1248
1249bool WebRtcVideoChannel2::SetRecvRtpHeaderExtensions(
1250 const std::vector<RtpHeaderExtension>& extensions) {
pbos@webrtc.org587ef602014-06-16 17:32:02 +00001251 LOG(LS_INFO) << "SetRecvRtpHeaderExtensions: "
1252 << RtpExtensionsToString(extensions);
pbos@webrtc.org3c107582014-07-20 15:27:35 +00001253 if (!ValidateRtpHeaderExtensionIds(extensions))
1254 return false;
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001255
pbos@webrtc.org3c107582014-07-20 15:27:35 +00001256 recv_rtp_extensions_ = FilterRtpExtensions(extensions);
pbos@webrtc.org575d1262014-10-08 14:48:08 +00001257 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001258 for (std::map<uint32, WebRtcVideoReceiveStream*>::iterator it =
1259 receive_streams_.begin();
1260 it != receive_streams_.end();
1261 ++it) {
1262 it->second->SetRtpExtensions(recv_rtp_extensions_);
1263 }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001264 return true;
1265}
1266
1267bool WebRtcVideoChannel2::SetSendRtpHeaderExtensions(
1268 const std::vector<RtpHeaderExtension>& extensions) {
pbos@webrtc.org587ef602014-06-16 17:32:02 +00001269 LOG(LS_INFO) << "SetSendRtpHeaderExtensions: "
1270 << RtpExtensionsToString(extensions);
pbos@webrtc.org3c107582014-07-20 15:27:35 +00001271 if (!ValidateRtpHeaderExtensionIds(extensions))
1272 return false;
1273
1274 send_rtp_extensions_ = FilterRtpExtensions(extensions);
pbos@webrtc.org575d1262014-10-08 14:48:08 +00001275 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001276 for (std::map<uint32, WebRtcVideoSendStream*>::iterator it =
1277 send_streams_.begin();
1278 it != send_streams_.end();
1279 ++it) {
1280 it->second->SetRtpExtensions(send_rtp_extensions_);
1281 }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001282 return true;
1283}
1284
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001285bool WebRtcVideoChannel2::SetMaxSendBandwidth(int bps) {
1286 // TODO(pbos): Implement.
1287 LOG(LS_VERBOSE) << "SetMaxSendBandwidth: " << bps;
1288 return true;
1289}
1290
1291bool WebRtcVideoChannel2::SetOptions(const VideoOptions& options) {
1292 LOG(LS_VERBOSE) << "SetOptions: " << options.ToString();
1293 options_.SetAll(options);
pbos@webrtc.org575d1262014-10-08 14:48:08 +00001294 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001295 for (std::map<uint32, WebRtcVideoSendStream*>::iterator it =
1296 send_streams_.begin();
1297 it != send_streams_.end();
1298 ++it) {
1299 it->second->SetOptions(options_);
1300 }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001301 return true;
1302}
1303
1304void WebRtcVideoChannel2::SetInterface(NetworkInterface* iface) {
1305 MediaChannel::SetInterface(iface);
1306 // Set the RTP recv/send buffer to a bigger size
1307 MediaChannel::SetOption(NetworkInterface::ST_RTP,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001308 rtc::Socket::OPT_RCVBUF,
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001309 kVideoRtpBufferSize);
1310
buildbot@webrtc.orgae694ef2014-10-28 17:37:17 +00001311 // Speculative change to increase the outbound socket buffer size.
1312 // In b/15152257, we are seeing a significant number of packets discarded
1313 // due to lack of socket buffer space, although it's not yet clear what the
1314 // ideal value should be.
1315 MediaChannel::SetOption(NetworkInterface::ST_RTP,
1316 rtc::Socket::OPT_SNDBUF,
1317 kVideoRtpBufferSize);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001318}
1319
1320void WebRtcVideoChannel2::UpdateAspectRatio(int ratio_w, int ratio_h) {
1321 // TODO(pbos): Implement.
1322}
1323
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001324void WebRtcVideoChannel2::OnMessage(rtc::Message* msg) {
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001325 // Ignored.
1326}
1327
pbos@webrtc.org42684be2014-10-03 11:25:45 +00001328void WebRtcVideoChannel2::OnLoadUpdate(Load load) {
pbos@webrtc.org575d1262014-10-08 14:48:08 +00001329 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.org42684be2014-10-03 11:25:45 +00001330 for (std::map<uint32, WebRtcVideoSendStream*>::iterator it =
1331 send_streams_.begin();
1332 it != send_streams_.end();
1333 ++it) {
1334 it->second->OnCpuResolutionRequest(load == kOveruse
1335 ? CoordinatedVideoAdapter::DOWNGRADE
1336 : CoordinatedVideoAdapter::UPGRADE);
1337 }
1338}
1339
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001340bool WebRtcVideoChannel2::SendRtp(const uint8_t* data, size_t len) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001341 rtc::Buffer packet(data, len, kMaxRtpPacketLen);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001342 return MediaChannel::SendPacket(&packet);
1343}
1344
1345bool WebRtcVideoChannel2::SendRtcp(const uint8_t* data, size_t len) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001346 rtc::Buffer packet(data, len, kMaxRtpPacketLen);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001347 return MediaChannel::SendRtcp(&packet);
1348}
1349
1350void WebRtcVideoChannel2::StartAllSendStreams() {
pbos@webrtc.org575d1262014-10-08 14:48:08 +00001351 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001352 for (std::map<uint32, WebRtcVideoSendStream*>::iterator it =
1353 send_streams_.begin();
1354 it != send_streams_.end();
1355 ++it) {
1356 it->second->Start();
1357 }
1358}
1359
1360void WebRtcVideoChannel2::StopAllSendStreams() {
pbos@webrtc.org575d1262014-10-08 14:48:08 +00001361 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001362 for (std::map<uint32, WebRtcVideoSendStream*>::iterator it =
1363 send_streams_.begin();
1364 it != send_streams_.end();
1365 ++it) {
1366 it->second->Stop();
1367 }
1368}
1369
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +00001370WebRtcVideoChannel2::WebRtcVideoSendStream::VideoSendStreamParameters::
1371 VideoSendStreamParameters(
1372 const webrtc::VideoSendStream::Config& config,
1373 const VideoOptions& options,
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001374 const Settable<VideoCodecSettings>& codec_settings)
1375 : config(config), options(options), codec_settings(codec_settings) {
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +00001376}
1377
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001378WebRtcVideoChannel2::WebRtcVideoSendStream::WebRtcVideoSendStream(
1379 webrtc::Call* call,
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +00001380 WebRtcVideoEncoderFactory* external_encoder_factory,
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001381 WebRtcVideoEncoderFactory2* encoder_factory,
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +00001382 const VideoOptions& options,
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001383 const Settable<VideoCodecSettings>& codec_settings,
1384 const StreamParams& sp,
1385 const std::vector<webrtc::RtpExtension>& rtp_extensions)
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001386 : call_(call),
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +00001387 external_encoder_factory_(external_encoder_factory),
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001388 encoder_factory_(encoder_factory),
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001389 stream_(NULL),
pbos@webrtc.orgb648b9d2014-08-26 11:08:06 +00001390 parameters_(webrtc::VideoSendStream::Config(), options, codec_settings),
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +00001391 allocated_encoder_(NULL, webrtc::kVideoCodecUnknown, false),
pbos@webrtc.orgb648b9d2014-08-26 11:08:06 +00001392 capturer_(NULL),
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001393 sending_(false),
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001394 muted_(false) {
1395 parameters_.config.rtp.max_packet_size = kVideoMtu;
1396
1397 sp.GetPrimarySsrcs(&parameters_.config.rtp.ssrcs);
1398 sp.GetFidSsrcs(parameters_.config.rtp.ssrcs,
1399 &parameters_.config.rtp.rtx.ssrcs);
1400 parameters_.config.rtp.c_name = sp.cname;
1401 parameters_.config.rtp.extensions = rtp_extensions;
1402
1403 VideoCodecSettings params;
1404 if (codec_settings.Get(&params)) {
1405 SetCodec(params);
1406 }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001407}
1408
1409WebRtcVideoChannel2::WebRtcVideoSendStream::~WebRtcVideoSendStream() {
1410 DisconnectCapturer();
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001411 if (stream_ != NULL) {
1412 call_->DestroyVideoSendStream(stream_);
1413 }
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +00001414 DestroyVideoEncoder(&allocated_encoder_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001415}
1416
1417static void SetWebRtcFrameToBlack(webrtc::I420VideoFrame* video_frame) {
1418 assert(video_frame != NULL);
1419 memset(video_frame->buffer(webrtc::kYPlane),
1420 16,
1421 video_frame->allocated_size(webrtc::kYPlane));
1422 memset(video_frame->buffer(webrtc::kUPlane),
1423 128,
1424 video_frame->allocated_size(webrtc::kUPlane));
1425 memset(video_frame->buffer(webrtc::kVPlane),
1426 128,
1427 video_frame->allocated_size(webrtc::kVPlane));
1428}
1429
1430static void CreateBlackFrame(webrtc::I420VideoFrame* video_frame,
1431 int width,
1432 int height) {
1433 video_frame->CreateEmptyFrame(
1434 width, height, width, (width + 1) / 2, (width + 1) / 2);
1435 SetWebRtcFrameToBlack(video_frame);
1436}
1437
1438static void ConvertToI420VideoFrame(const VideoFrame& frame,
1439 webrtc::I420VideoFrame* i420_frame) {
1440 i420_frame->CreateFrame(
1441 static_cast<int>(frame.GetYPitch() * frame.GetHeight()),
1442 frame.GetYPlane(),
1443 static_cast<int>(frame.GetUPitch() * ((frame.GetHeight() + 1) / 2)),
1444 frame.GetUPlane(),
1445 static_cast<int>(frame.GetVPitch() * ((frame.GetHeight() + 1) / 2)),
1446 frame.GetVPlane(),
1447 static_cast<int>(frame.GetWidth()),
1448 static_cast<int>(frame.GetHeight()),
1449 static_cast<int>(frame.GetYPitch()),
1450 static_cast<int>(frame.GetUPitch()),
1451 static_cast<int>(frame.GetVPitch()));
1452}
1453
1454void WebRtcVideoChannel2::WebRtcVideoSendStream::InputFrame(
1455 VideoCapturer* capturer,
1456 const VideoFrame* frame) {
1457 LOG(LS_VERBOSE) << "InputFrame: " << frame->GetWidth() << "x"
1458 << frame->GetHeight();
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001459 // Lock before copying, can be called concurrently when swapping input source.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001460 rtc::CritScope frame_cs(&frame_lock_);
pbos@webrtc.orgd60d79a2014-09-24 07:10:57 +00001461 ConvertToI420VideoFrame(*frame, &video_frame_);
1462
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001463 rtc::CritScope cs(&lock_);
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001464 if (stream_ == NULL) {
1465 LOG(LS_WARNING) << "Capturer inputting frames before send codecs are "
1466 "configured, dropping.";
1467 return;
1468 }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001469 if (format_.width == 0) { // Dropping frames.
1470 assert(format_.height == 0);
1471 LOG(LS_VERBOSE) << "VideoFormat 0x0 set, Dropping frame.";
1472 return;
1473 }
pbos@webrtc.orgd60d79a2014-09-24 07:10:57 +00001474 if (muted_) {
1475 // Create a black frame to transmit instead.
1476 CreateBlackFrame(&video_frame_,
1477 static_cast<int>(frame->GetWidth()),
1478 static_cast<int>(frame->GetHeight()));
1479 }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001480 // Reconfigure codec if necessary.
pbos@webrtc.orgc4175b92014-09-03 15:25:49 +00001481 SetDimensions(
1482 video_frame_.width(), video_frame_.height(), capturer->IsScreencast());
1483
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001484 LOG(LS_VERBOSE) << "SwapFrame: " << video_frame_.width() << "x"
1485 << video_frame_.height() << " -> (codec) "
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +00001486 << parameters_.encoder_config.streams.back().width << "x"
1487 << parameters_.encoder_config.streams.back().height;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001488 stream_->Input()->SwapFrame(&video_frame_);
1489}
1490
1491bool WebRtcVideoChannel2::WebRtcVideoSendStream::SetCapturer(
1492 VideoCapturer* capturer) {
1493 if (!DisconnectCapturer() && capturer == NULL) {
1494 return false;
1495 }
1496
1497 {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001498 rtc::CritScope cs(&lock_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001499
pbos@webrtc.org9359cb32014-07-23 15:44:48 +00001500 if (capturer == NULL) {
1501 if (stream_ != NULL) {
1502 LOG(LS_VERBOSE) << "Disabling capturer, sending black frame.";
1503 webrtc::I420VideoFrame black_frame;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001504
pbos@webrtc.org9359cb32014-07-23 15:44:48 +00001505 int width = format_.width;
1506 int height = format_.height;
1507 int half_width = (width + 1) / 2;
1508 black_frame.CreateEmptyFrame(
1509 width, height, width, half_width, half_width);
1510 SetWebRtcFrameToBlack(&black_frame);
pbos@webrtc.orgc4175b92014-09-03 15:25:49 +00001511 SetDimensions(width, height, false);
pbos@webrtc.org9359cb32014-07-23 15:44:48 +00001512 stream_->Input()->SwapFrame(&black_frame);
1513 }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001514
1515 capturer_ = NULL;
1516 return true;
1517 }
1518
1519 capturer_ = capturer;
1520 }
1521 // Lock cannot be held while connecting the capturer to prevent lock-order
1522 // violations.
1523 capturer->SignalVideoFrame.connect(this, &WebRtcVideoSendStream::InputFrame);
1524 return true;
1525}
1526
1527bool WebRtcVideoChannel2::WebRtcVideoSendStream::SetVideoFormat(
1528 const VideoFormat& format) {
1529 if ((format.width == 0 || format.height == 0) &&
1530 format.width != format.height) {
1531 LOG(LS_ERROR) << "Can't set VideoFormat, width or height is zero (but not "
1532 "both, 0x0 drops frames).";
1533 return false;
1534 }
1535
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001536 rtc::CritScope cs(&lock_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001537 if (format.width == 0 && format.height == 0) {
1538 LOG(LS_INFO)
1539 << "0x0 resolution selected. Captured frames will be dropped for ssrc: "
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +00001540 << parameters_.config.rtp.ssrcs[0] << ".";
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001541 } else {
1542 // TODO(pbos): Fix me, this only affects the last stream!
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +00001543 parameters_.encoder_config.streams.back().max_framerate =
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001544 VideoFormat::IntervalToFps(format.interval);
pbos@webrtc.orgc4175b92014-09-03 15:25:49 +00001545 SetDimensions(format.width, format.height, false);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001546 }
1547
1548 format_ = format;
1549 return true;
1550}
1551
pbos@webrtc.orgef8bb8d2014-08-13 21:36:18 +00001552void WebRtcVideoChannel2::WebRtcVideoSendStream::MuteStream(bool mute) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001553 rtc::CritScope cs(&lock_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001554 muted_ = mute;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001555}
1556
1557bool WebRtcVideoChannel2::WebRtcVideoSendStream::DisconnectCapturer() {
pbos@webrtc.org963b9792014-10-07 14:27:27 +00001558 cricket::VideoCapturer* capturer;
1559 {
1560 rtc::CritScope cs(&lock_);
1561 if (capturer_ == NULL) {
1562 return false;
1563 }
1564 capturer = capturer_;
1565 capturer_ = NULL;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001566 }
pbos@webrtc.org963b9792014-10-07 14:27:27 +00001567 capturer->SignalVideoFrame.disconnect(this);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001568 return true;
1569}
1570
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001571void WebRtcVideoChannel2::WebRtcVideoSendStream::SetOptions(
1572 const VideoOptions& options) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001573 rtc::CritScope cs(&lock_);
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001574 VideoCodecSettings codec_settings;
1575 if (parameters_.codec_settings.Get(&codec_settings)) {
1576 SetCodecAndOptions(codec_settings, options);
1577 } else {
1578 parameters_.options = options;
1579 }
1580}
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +00001581
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001582void WebRtcVideoChannel2::WebRtcVideoSendStream::SetCodec(
1583 const VideoCodecSettings& codec_settings) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001584 rtc::CritScope cs(&lock_);
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001585 SetCodecAndOptions(codec_settings, parameters_.options);
1586}
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +00001587
1588webrtc::VideoCodecType CodecTypeFromName(const std::string& name) {
1589 if (CodecNameMatches(name, kVp8CodecName)) {
1590 return webrtc::kVideoCodecVP8;
1591 } else if (CodecNameMatches(name, kH264CodecName)) {
1592 return webrtc::kVideoCodecH264;
1593 }
1594 return webrtc::kVideoCodecUnknown;
1595}
1596
1597WebRtcVideoChannel2::WebRtcVideoSendStream::AllocatedEncoder
1598WebRtcVideoChannel2::WebRtcVideoSendStream::CreateVideoEncoder(
1599 const VideoCodec& codec) {
1600 webrtc::VideoCodecType type = CodecTypeFromName(codec.name);
1601
1602 // Do not re-create encoders of the same type.
1603 if (type == allocated_encoder_.type && allocated_encoder_.encoder != NULL) {
1604 return allocated_encoder_;
1605 }
1606
1607 if (external_encoder_factory_ != NULL) {
1608 webrtc::VideoEncoder* encoder =
1609 external_encoder_factory_->CreateVideoEncoder(type);
1610 if (encoder != NULL) {
1611 return AllocatedEncoder(encoder, type, true);
1612 }
1613 }
1614
1615 if (type == webrtc::kVideoCodecVP8) {
1616 return AllocatedEncoder(
1617 webrtc::VideoEncoder::Create(webrtc::VideoEncoder::kVp8), type, false);
1618 }
1619
1620 // This shouldn't happen, we should not be trying to create something we don't
1621 // support.
1622 assert(false);
1623 return AllocatedEncoder(NULL, webrtc::kVideoCodecUnknown, false);
1624}
1625
1626void WebRtcVideoChannel2::WebRtcVideoSendStream::DestroyVideoEncoder(
1627 AllocatedEncoder* encoder) {
1628 if (encoder->external) {
1629 external_encoder_factory_->DestroyVideoEncoder(encoder->encoder);
1630 } else {
1631 delete encoder->encoder;
1632 }
1633}
1634
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001635void WebRtcVideoChannel2::WebRtcVideoSendStream::SetCodecAndOptions(
1636 const VideoCodecSettings& codec_settings,
1637 const VideoOptions& options) {
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +00001638 std::vector<webrtc::VideoStream> video_streams =
1639 encoder_factory_->CreateVideoStreams(
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001640 codec_settings.codec, options, parameters_.config.rtp.ssrcs.size());
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +00001641 if (video_streams.empty()) {
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001642 return;
1643 }
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +00001644 parameters_.encoder_config.streams = video_streams;
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001645 format_ = VideoFormat(codec_settings.codec.width,
1646 codec_settings.codec.height,
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001647 VideoFormat::FpsToInterval(30),
1648 FOURCC_I420);
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +00001649
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +00001650 AllocatedEncoder new_encoder = CreateVideoEncoder(codec_settings.codec);
1651 parameters_.config.encoder_settings.encoder = new_encoder.encoder;
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001652 parameters_.config.encoder_settings.payload_name = codec_settings.codec.name;
1653 parameters_.config.encoder_settings.payload_type = codec_settings.codec.id;
1654 parameters_.config.rtp.fec = codec_settings.fec;
1655
1656 // Set RTX payload type if RTX is enabled.
1657 if (!parameters_.config.rtp.rtx.ssrcs.empty()) {
1658 parameters_.config.rtp.rtx.payload_type = codec_settings.rtx_payload_type;
pbos@webrtc.org543e5892014-07-23 07:01:31 +00001659
1660 options.use_payload_padding.Get(
1661 &parameters_.config.rtp.rtx.pad_with_redundant_payloads);
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001662 }
1663
1664 if (IsNackEnabled(codec_settings.codec)) {
1665 parameters_.config.rtp.nack.rtp_history_ms = kNackHistoryMs;
1666 }
1667
pbos@webrtc.org5ff71ab2014-07-23 07:28:56 +00001668 options.suspend_below_min_bitrate.Get(
1669 &parameters_.config.suspend_below_min_bitrate);
1670
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001671 parameters_.codec_settings.Set(codec_settings);
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +00001672 parameters_.options = options;
pbos@webrtc.org543e5892014-07-23 07:01:31 +00001673
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001674 RecreateWebRtcStream();
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +00001675 if (allocated_encoder_.encoder != new_encoder.encoder) {
1676 DestroyVideoEncoder(&allocated_encoder_);
1677 allocated_encoder_ = new_encoder;
1678 }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001679}
1680
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001681void WebRtcVideoChannel2::WebRtcVideoSendStream::SetRtpExtensions(
1682 const std::vector<webrtc::RtpExtension>& rtp_extensions) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001683 rtc::CritScope cs(&lock_);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001684 parameters_.config.rtp.extensions = rtp_extensions;
1685 RecreateWebRtcStream();
1686}
1687
pbos@webrtc.orgc4175b92014-09-03 15:25:49 +00001688void WebRtcVideoChannel2::WebRtcVideoSendStream::SetDimensions(
1689 int width,
1690 int height,
pbos@webrtc.orgefc82c22014-10-27 13:58:00 +00001691 bool is_screencast) {
1692 if (last_dimensions_.width == width && last_dimensions_.height == height &&
1693 last_dimensions_.is_screencast == is_screencast) {
1694 // Configured using the same parameters, do not reconfigure.
1695 return;
1696 }
1697
1698 last_dimensions_.width = width;
1699 last_dimensions_.height = height;
1700 last_dimensions_.is_screencast = is_screencast;
1701
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +00001702 assert(!parameters_.encoder_config.streams.empty());
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001703 LOG(LS_VERBOSE) << "SetDimensions: " << width << "x" << height;
pbos@webrtc.orgc4175b92014-09-03 15:25:49 +00001704
1705 VideoCodecSettings codec_settings;
1706 parameters_.codec_settings.Get(&codec_settings);
1707 // Restrict dimensions according to codec max.
pbos@webrtc.orgefc82c22014-10-27 13:58:00 +00001708 if (!is_screencast) {
pbos@webrtc.orgc4175b92014-09-03 15:25:49 +00001709 if (codec_settings.codec.width < width)
1710 width = codec_settings.codec.width;
1711 if (codec_settings.codec.height < height)
1712 height = codec_settings.codec.height;
1713 }
1714
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +00001715 webrtc::VideoEncoderConfig encoder_config = parameters_.encoder_config;
1716 encoder_config.encoder_specific_settings =
1717 encoder_factory_->CreateVideoEncoderSettings(codec_settings.codec,
1718 parameters_.options);
pbos@webrtc.org6f48f1b2014-07-22 16:29:54 +00001719
pbos@webrtc.orgefc82c22014-10-27 13:58:00 +00001720 if (is_screencast) {
1721 int screencast_min_bitrate_kbps;
1722 parameters_.options.screencast_min_bitrate.Get(
1723 &screencast_min_bitrate_kbps);
1724 encoder_config.min_transmit_bitrate_bps =
1725 screencast_min_bitrate_kbps * 1000;
1726 encoder_config.content_type = webrtc::VideoEncoderConfig::kScreenshare;
1727 } else {
1728 encoder_config.min_transmit_bitrate_bps = 0;
1729 encoder_config.content_type = webrtc::VideoEncoderConfig::kRealtimeVideo;
1730 }
1731
pbos@webrtc.orgcddd17c2014-09-16 16:33:13 +00001732 VideoCodec codec = codec_settings.codec;
1733 codec.width = width;
1734 codec.height = height;
pbos@webrtc.orgcddd17c2014-09-16 16:33:13 +00001735
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +00001736 encoder_config.streams = encoder_factory_->CreateVideoStreams(
1737 codec, parameters_.options, parameters_.config.rtp.ssrcs.size());
pbos@webrtc.org6f48f1b2014-07-22 16:29:54 +00001738
pbos@webrtc.orgb7ed7792014-10-31 13:08:10 +00001739 // Conference mode screencast uses 2 temporal layers split at 100kbit.
1740 if (parameters_.options.conference_mode.GetWithDefaultIfUnset(false) &&
1741 is_screencast && encoder_config.streams.size() == 1) {
1742 encoder_config.streams[0].temporal_layer_thresholds_bps.clear();
1743 encoder_config.streams[0].temporal_layer_thresholds_bps.push_back(
1744 kConferenceModeTemporalLayerBitrateBps);
1745 }
1746
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +00001747 bool stream_reconfigured = stream_->ReconfigureVideoEncoder(encoder_config);
1748
1749 encoder_factory_->DestroyVideoEncoderSettings(
1750 codec_settings.codec,
1751 encoder_config.encoder_specific_settings);
1752
1753 encoder_config.encoder_specific_settings = NULL;
pbos@webrtc.org6f48f1b2014-07-22 16:29:54 +00001754
1755 if (!stream_reconfigured) {
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001756 LOG(LS_WARNING) << "Failed to reconfigure video encoder for dimensions: "
1757 << width << "x" << height;
1758 return;
1759 }
pbos@webrtc.orgcddd17c2014-09-16 16:33:13 +00001760
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +00001761 parameters_.encoder_config = encoder_config;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001762}
1763
1764void WebRtcVideoChannel2::WebRtcVideoSendStream::Start() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001765 rtc::CritScope cs(&lock_);
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001766 assert(stream_ != NULL);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001767 stream_->Start();
1768 sending_ = true;
1769}
1770
1771void WebRtcVideoChannel2::WebRtcVideoSendStream::Stop() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001772 rtc::CritScope cs(&lock_);
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001773 if (stream_ != NULL) {
1774 stream_->Stop();
1775 }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001776 sending_ = false;
1777}
1778
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00001779VideoSenderInfo
1780WebRtcVideoChannel2::WebRtcVideoSendStream::GetVideoSenderInfo() {
1781 VideoSenderInfo info;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001782 rtc::CritScope cs(&lock_);
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00001783 for (size_t i = 0; i < parameters_.config.rtp.ssrcs.size(); ++i) {
1784 info.add_ssrc(parameters_.config.rtp.ssrcs[i]);
1785 }
1786
pbos@webrtc.orgc3d2bd22014-08-12 20:55:10 +00001787 if (stream_ == NULL) {
1788 return info;
1789 }
1790
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00001791 webrtc::VideoSendStream::Stats stats = stream_->GetStats();
1792 info.framerate_input = stats.input_frame_rate;
1793 info.framerate_sent = stats.encode_frame_rate;
1794
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001795 for (std::map<uint32_t, webrtc::SsrcStats>::iterator it =
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00001796 stats.substreams.begin();
1797 it != stats.substreams.end();
1798 ++it) {
1799 // TODO(pbos): Wire up additional stats, such as padding bytes.
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001800 webrtc::SsrcStats stream_stats = it->second;
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00001801 info.bytes_sent += stream_stats.rtp_stats.bytes +
1802 stream_stats.rtp_stats.header_bytes +
1803 stream_stats.rtp_stats.padding_bytes;
1804 info.packets_sent += stream_stats.rtp_stats.packets;
1805 info.packets_lost += stream_stats.rtcp_stats.cumulative_lost;
1806 }
1807
1808 if (!stats.substreams.empty()) {
1809 // TODO(pbos): Report fraction lost per SSRC.
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001810 webrtc::SsrcStats first_stream_stats = stats.substreams.begin()->second;
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00001811 info.fraction_lost =
1812 static_cast<float>(first_stream_stats.rtcp_stats.fraction_lost) /
1813 (1 << 8);
1814 }
1815
1816 if (capturer_ != NULL && !capturer_->IsMuted()) {
1817 VideoFormat last_captured_frame_format;
1818 capturer_->GetStats(&info.adapt_frame_drops,
1819 &info.effects_frame_drops,
1820 &info.capturer_frame_time,
1821 &last_captured_frame_format);
1822 info.input_frame_width = last_captured_frame_format.width;
1823 info.input_frame_height = last_captured_frame_format.height;
1824 info.send_frame_width =
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +00001825 static_cast<int>(parameters_.encoder_config.streams.front().width);
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00001826 info.send_frame_height =
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +00001827 static_cast<int>(parameters_.encoder_config.streams.front().height);
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00001828 }
1829
1830 // TODO(pbos): Support or remove the following stats.
1831 info.packets_cached = -1;
1832 info.rtt_ms = -1;
1833
1834 return info;
1835}
1836
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001837void WebRtcVideoChannel2::WebRtcVideoSendStream::FillBandwidthEstimationInfo(
1838 BandwidthEstimationInfo* bwe_info) {
1839 rtc::CritScope cs(&lock_);
1840 if (stream_ == NULL) {
1841 return;
1842 }
1843 webrtc::VideoSendStream::Stats stats = stream_->GetStats();
1844 for (std::map<uint32_t, webrtc::SsrcStats>::iterator it =
1845 stats.substreams.begin();
1846 it != stats.substreams.end();
1847 ++it) {
1848 bwe_info->transmit_bitrate += it->second.total_bitrate_bps;
1849 bwe_info->retransmit_bitrate += it->second.retransmit_bitrate_bps;
1850 }
1851 bwe_info->actual_enc_bitrate = stats.media_bitrate_bps;
1852}
1853
pbos@webrtc.org42684be2014-10-03 11:25:45 +00001854void WebRtcVideoChannel2::WebRtcVideoSendStream::OnCpuResolutionRequest(
1855 CoordinatedVideoAdapter::AdaptRequest adapt_request) {
1856 rtc::CritScope cs(&lock_);
1857 bool adapt_cpu;
1858 parameters_.options.cpu_overuse_detection.Get(&adapt_cpu);
1859 if (!adapt_cpu) {
1860 return;
1861 }
1862 if (capturer_ == NULL || capturer_->video_adapter() == NULL) {
1863 return;
1864 }
1865
1866 capturer_->video_adapter()->OnCpuResolutionRequest(adapt_request);
1867}
1868
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001869void WebRtcVideoChannel2::WebRtcVideoSendStream::RecreateWebRtcStream() {
1870 if (stream_ != NULL) {
1871 call_->DestroyVideoSendStream(stream_);
1872 }
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +00001873
pbos@webrtc.org6f48f1b2014-07-22 16:29:54 +00001874 VideoCodecSettings codec_settings;
1875 parameters_.codec_settings.Get(&codec_settings);
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +00001876 parameters_.encoder_config.encoder_specific_settings =
1877 encoder_factory_->CreateVideoEncoderSettings(codec_settings.codec,
1878 parameters_.options);
pbos@webrtc.org6f48f1b2014-07-22 16:29:54 +00001879
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +00001880 stream_ = call_->CreateVideoSendStream(parameters_.config,
1881 parameters_.encoder_config);
pbos@webrtc.org6f48f1b2014-07-22 16:29:54 +00001882
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +00001883 encoder_factory_->DestroyVideoEncoderSettings(
1884 codec_settings.codec,
1885 parameters_.encoder_config.encoder_specific_settings);
1886
1887 parameters_.encoder_config.encoder_specific_settings = NULL;
pbos@webrtc.org6f48f1b2014-07-22 16:29:54 +00001888
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001889 if (sending_) {
1890 stream_->Start();
1891 }
1892}
1893
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001894WebRtcVideoChannel2::WebRtcVideoReceiveStream::WebRtcVideoReceiveStream(
1895 webrtc::Call* call,
pbos@webrtc.org776e6f22014-10-29 15:28:39 +00001896 WebRtcVideoDecoderFactory* external_decoder_factory,
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001897 const webrtc::VideoReceiveStream::Config& config,
1898 const std::vector<VideoCodecSettings>& recv_codecs)
1899 : call_(call),
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001900 stream_(NULL),
pbos@webrtc.orgb648b9d2014-08-26 11:08:06 +00001901 config_(config),
pbos@webrtc.org776e6f22014-10-29 15:28:39 +00001902 external_decoder_factory_(external_decoder_factory),
pbos@webrtc.orgb648b9d2014-08-26 11:08:06 +00001903 renderer_(NULL),
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001904 last_width_(-1),
pbos@webrtc.orgb648b9d2014-08-26 11:08:06 +00001905 last_height_(-1) {
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001906 config_.renderer = this;
1907 // SetRecvCodecs will also reset (start) the VideoReceiveStream.
1908 SetRecvCodecs(recv_codecs);
1909}
1910
1911WebRtcVideoChannel2::WebRtcVideoReceiveStream::~WebRtcVideoReceiveStream() {
1912 call_->DestroyVideoReceiveStream(stream_);
pbos@webrtc.org96a93252014-11-03 14:46:44 +00001913 ClearDecoders(&allocated_decoders_);
1914}
1915
1916WebRtcVideoChannel2::WebRtcVideoReceiveStream::AllocatedDecoder
1917WebRtcVideoChannel2::WebRtcVideoReceiveStream::CreateOrReuseVideoDecoder(
1918 std::vector<AllocatedDecoder>* old_decoders,
1919 const VideoCodec& codec) {
1920 webrtc::VideoCodecType type = CodecTypeFromName(codec.name);
1921
1922 for (size_t i = 0; i < old_decoders->size(); ++i) {
1923 if ((*old_decoders)[i].type == type) {
1924 AllocatedDecoder decoder = (*old_decoders)[i];
1925 (*old_decoders)[i] = old_decoders->back();
1926 old_decoders->pop_back();
1927 return decoder;
1928 }
1929 }
1930
1931 if (external_decoder_factory_ != NULL) {
1932 webrtc::VideoDecoder* decoder =
1933 external_decoder_factory_->CreateVideoDecoder(type);
1934 if (decoder != NULL) {
1935 return AllocatedDecoder(decoder, type, true);
1936 }
1937 }
1938
1939 if (type == webrtc::kVideoCodecVP8) {
1940 return AllocatedDecoder(
1941 webrtc::VideoDecoder::Create(webrtc::VideoDecoder::kVp8), type, false);
1942 }
1943
1944 // This shouldn't happen, we should not be trying to create something we don't
1945 // support.
1946 assert(false);
1947 return AllocatedDecoder(NULL, webrtc::kVideoCodecUnknown, false);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001948}
1949
1950void WebRtcVideoChannel2::WebRtcVideoReceiveStream::SetRecvCodecs(
1951 const std::vector<VideoCodecSettings>& recv_codecs) {
pbos@webrtc.org96a93252014-11-03 14:46:44 +00001952 std::vector<AllocatedDecoder> old_decoders = allocated_decoders_;
1953 allocated_decoders_.clear();
1954 config_.decoders.clear();
1955 for (size_t i = 0; i < recv_codecs.size(); ++i) {
1956 AllocatedDecoder allocated_decoder =
1957 CreateOrReuseVideoDecoder(&old_decoders, recv_codecs[i].codec);
1958 allocated_decoders_.push_back(allocated_decoder);
1959
1960 webrtc::VideoReceiveStream::Decoder decoder;
1961 decoder.decoder = allocated_decoder.decoder;
1962 decoder.payload_type = recv_codecs[i].codec.id;
1963 decoder.payload_name = recv_codecs[i].codec.name;
1964 config_.decoders.push_back(decoder);
1965 }
1966
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001967 // TODO(pbos): Reconfigure RTX based on incoming recv_codecs.
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001968 config_.rtp.fec = recv_codecs.front().fec;
pbos@webrtc.org257e1302014-07-25 19:01:32 +00001969 config_.rtp.nack.rtp_history_ms =
1970 IsNackEnabled(recv_codecs.begin()->codec) ? kNackHistoryMs : 0;
1971 config_.rtp.remb = IsRembEnabled(recv_codecs.begin()->codec);
1972
pbos@webrtc.org96a93252014-11-03 14:46:44 +00001973 ClearDecoders(&old_decoders);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001974 RecreateWebRtcStream();
1975}
1976
1977void WebRtcVideoChannel2::WebRtcVideoReceiveStream::SetRtpExtensions(
1978 const std::vector<webrtc::RtpExtension>& extensions) {
1979 config_.rtp.extensions = extensions;
1980 RecreateWebRtcStream();
1981}
1982
1983void WebRtcVideoChannel2::WebRtcVideoReceiveStream::RecreateWebRtcStream() {
1984 if (stream_ != NULL) {
1985 call_->DestroyVideoReceiveStream(stream_);
1986 }
1987 stream_ = call_->CreateVideoReceiveStream(config_);
1988 stream_->Start();
1989}
1990
pbos@webrtc.org96a93252014-11-03 14:46:44 +00001991void WebRtcVideoChannel2::WebRtcVideoReceiveStream::ClearDecoders(
1992 std::vector<AllocatedDecoder>* allocated_decoders) {
1993 for (size_t i = 0; i < allocated_decoders->size(); ++i) {
1994 if ((*allocated_decoders)[i].external) {
pbos@webrtc.org776e6f22014-10-29 15:28:39 +00001995 external_decoder_factory_->DestroyVideoDecoder(
pbos@webrtc.org96a93252014-11-03 14:46:44 +00001996 (*allocated_decoders)[i].decoder);
pbos@webrtc.org776e6f22014-10-29 15:28:39 +00001997 } else {
pbos@webrtc.org96a93252014-11-03 14:46:44 +00001998 delete (*allocated_decoders)[i].decoder;
pbos@webrtc.org776e6f22014-10-29 15:28:39 +00001999 }
2000 }
pbos@webrtc.org96a93252014-11-03 14:46:44 +00002001 allocated_decoders->clear();
pbos@webrtc.org776e6f22014-10-29 15:28:39 +00002002}
2003
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00002004void WebRtcVideoChannel2::WebRtcVideoReceiveStream::RenderFrame(
2005 const webrtc::I420VideoFrame& frame,
2006 int time_to_render_ms) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002007 rtc::CritScope crit(&renderer_lock_);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00002008 if (renderer_ == NULL) {
2009 LOG(LS_WARNING) << "VideoReceiveStream not connected to a VideoRenderer.";
2010 return;
2011 }
2012
2013 if (frame.width() != last_width_ || frame.height() != last_height_) {
2014 SetSize(frame.width(), frame.height());
2015 }
2016
2017 LOG(LS_VERBOSE) << "RenderFrame: (" << frame.width() << "x" << frame.height()
2018 << ")";
2019
2020 const WebRtcVideoRenderFrame render_frame(&frame);
2021 renderer_->RenderFrame(&render_frame);
2022}
2023
2024void WebRtcVideoChannel2::WebRtcVideoReceiveStream::SetRenderer(
2025 cricket::VideoRenderer* renderer) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002026 rtc::CritScope crit(&renderer_lock_);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00002027 renderer_ = renderer;
2028 if (renderer_ != NULL && last_width_ != -1) {
2029 SetSize(last_width_, last_height_);
2030 }
2031}
2032
2033VideoRenderer* WebRtcVideoChannel2::WebRtcVideoReceiveStream::GetRenderer() {
2034 // TODO(pbos): Remove GetRenderer and all uses of it, it's thread-unsafe by
2035 // design.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002036 rtc::CritScope crit(&renderer_lock_);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00002037 return renderer_;
2038}
2039
2040void WebRtcVideoChannel2::WebRtcVideoReceiveStream::SetSize(int width,
2041 int height) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002042 rtc::CritScope crit(&renderer_lock_);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00002043 if (!renderer_->SetSize(width, height, 0)) {
2044 LOG(LS_ERROR) << "Could not set renderer size.";
2045 }
2046 last_width_ = width;
2047 last_height_ = height;
2048}
2049
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00002050VideoReceiverInfo
2051WebRtcVideoChannel2::WebRtcVideoReceiveStream::GetVideoReceiverInfo() {
2052 VideoReceiverInfo info;
2053 info.add_ssrc(config_.rtp.remote_ssrc);
2054 webrtc::VideoReceiveStream::Stats stats = stream_->GetStats();
2055 info.bytes_rcvd = stats.rtp_stats.bytes + stats.rtp_stats.header_bytes +
2056 stats.rtp_stats.padding_bytes;
2057 info.packets_rcvd = stats.rtp_stats.packets;
2058
2059 info.framerate_rcvd = stats.network_frame_rate;
2060 info.framerate_decoded = stats.decode_frame_rate;
2061 info.framerate_output = stats.render_frame_rate;
2062
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002063 rtc::CritScope frame_cs(&renderer_lock_);
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00002064 info.frame_width = last_width_;
2065 info.frame_height = last_height_;
2066
2067 // TODO(pbos): Support or remove the following stats.
2068 info.packets_concealed = -1;
2069
2070 return info;
2071}
2072
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00002073WebRtcVideoChannel2::VideoCodecSettings::VideoCodecSettings()
2074 : rtx_payload_type(-1) {}
2075
2076std::vector<WebRtcVideoChannel2::VideoCodecSettings>
2077WebRtcVideoChannel2::MapCodecs(const std::vector<VideoCodec>& codecs) {
2078 assert(!codecs.empty());
2079
2080 std::vector<VideoCodecSettings> video_codecs;
2081 std::map<int, bool> payload_used;
pbos@webrtc.orge322a172014-06-13 11:47:28 +00002082 std::map<int, VideoCodec::CodecType> payload_codec_type;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00002083 std::map<int, int> rtx_mapping; // video payload type -> rtx payload type.
2084
2085 webrtc::FecConfig fec_settings;
2086
2087 for (size_t i = 0; i < codecs.size(); ++i) {
2088 const VideoCodec& in_codec = codecs[i];
2089 int payload_type = in_codec.id;
2090
2091 if (payload_used[payload_type]) {
2092 LOG(LS_ERROR) << "Payload type already registered: "
2093 << in_codec.ToString();
2094 return std::vector<VideoCodecSettings>();
2095 }
2096 payload_used[payload_type] = true;
pbos@webrtc.orge322a172014-06-13 11:47:28 +00002097 payload_codec_type[payload_type] = in_codec.GetCodecType();
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00002098
2099 switch (in_codec.GetCodecType()) {
2100 case VideoCodec::CODEC_RED: {
2101 // RED payload type, should not have duplicates.
2102 assert(fec_settings.red_payload_type == -1);
2103 fec_settings.red_payload_type = in_codec.id;
2104 continue;
2105 }
2106
2107 case VideoCodec::CODEC_ULPFEC: {
2108 // ULPFEC payload type, should not have duplicates.
2109 assert(fec_settings.ulpfec_payload_type == -1);
2110 fec_settings.ulpfec_payload_type = in_codec.id;
2111 continue;
2112 }
2113
2114 case VideoCodec::CODEC_RTX: {
2115 int associated_payload_type;
2116 if (!in_codec.GetParam(kCodecParamAssociatedPayloadType,
2117 &associated_payload_type)) {
2118 LOG(LS_ERROR) << "RTX codec without associated payload type: "
2119 << in_codec.ToString();
2120 return std::vector<VideoCodecSettings>();
2121 }
2122 rtx_mapping[associated_payload_type] = in_codec.id;
2123 continue;
2124 }
2125
2126 case VideoCodec::CODEC_VIDEO:
2127 break;
2128 }
2129
2130 video_codecs.push_back(VideoCodecSettings());
2131 video_codecs.back().codec = in_codec;
2132 }
2133
2134 // One of these codecs should have been a video codec. Only having FEC
2135 // parameters into this code is a logic error.
2136 assert(!video_codecs.empty());
2137
pbos@webrtc.orge322a172014-06-13 11:47:28 +00002138 for (std::map<int, int>::const_iterator it = rtx_mapping.begin();
2139 it != rtx_mapping.end();
2140 ++it) {
2141 if (!payload_used[it->first]) {
2142 LOG(LS_ERROR) << "RTX mapped to payload not in codec list.";
2143 return std::vector<VideoCodecSettings>();
2144 }
2145 if (payload_codec_type[it->first] != VideoCodec::CODEC_VIDEO) {
2146 LOG(LS_ERROR) << "RTX not mapped to regular video codec.";
2147 return std::vector<VideoCodecSettings>();
2148 }
2149 }
2150
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00002151 // TODO(pbos): Write tests that figure out that I have not verified that RTX
2152 // codecs aren't mapped to bogus payloads.
2153 for (size_t i = 0; i < video_codecs.size(); ++i) {
2154 video_codecs[i].fec = fec_settings;
2155 if (rtx_mapping[video_codecs[i].codec.id] != 0) {
2156 video_codecs[i].rtx_payload_type = rtx_mapping[video_codecs[i].codec.id];
2157 }
2158 }
2159
2160 return video_codecs;
2161}
2162
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00002163} // namespace cricket
2164
2165#endif // HAVE_WEBRTC_VIDEO