blob: d77db235d523e96ecfcb9c72879e891e04fb92df [file] [log] [blame]
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001/*
2 * libjingle
3 * Copyright 2014 Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#ifdef HAVE_WEBRTC_VIDEO
29#include "talk/media/webrtc/webrtcvideoengine2.h"
30
pbos@webrtc.orgc37e72e2015-01-05 18:51:13 +000031#include <algorithm>
pbos@webrtc.org3c107582014-07-20 15:27:35 +000032#include <set>
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +000033#include <string>
34
35#include "libyuv/convert_from.h"
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +000036#include "talk/media/base/videocapturer.h"
37#include "talk/media/base/videorenderer.h"
buildbot@webrtc.orgdf9bbbe2014-06-19 19:54:33 +000038#include "talk/media/webrtc/constants.h"
buildbot@webrtc.orga8530772014-12-10 09:01:18 +000039#include "talk/media/webrtc/simulcast.h"
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +000040#include "talk/media/webrtc/webrtcvideocapturer.h"
andresp@webrtc.org82775b12014-11-07 09:37:54 +000041#include "talk/media/webrtc/webrtcvideoengine.h"
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +000042#include "talk/media/webrtc/webrtcvideoframe.h"
43#include "talk/media/webrtc/webrtcvoiceengine.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000044#include "webrtc/base/buffer.h"
45#include "webrtc/base/logging.h"
46#include "webrtc/base/stringutils.h"
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +000047#include "webrtc/call.h"
pbos@webrtc.org50fe3592015-01-29 12:33:07 +000048#include "webrtc/system_wrappers/interface/trace_event.h"
pbos@webrtc.org776e6f22014-10-29 15:28:39 +000049#include "webrtc/video_decoder.h"
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000050#include "webrtc/video_encoder.h"
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +000051
52#define UNIMPLEMENTED \
53 LOG(LS_ERROR) << "Call to unimplemented function " << __FUNCTION__; \
54 ASSERT(false)
55
56namespace cricket {
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +000057namespace {
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +000058static std::string CodecVectorToString(const std::vector<VideoCodec>& codecs) {
59 std::stringstream out;
60 out << '{';
61 for (size_t i = 0; i < codecs.size(); ++i) {
62 out << codecs[i].ToString();
63 if (i != codecs.size() - 1) {
64 out << ", ";
65 }
66 }
67 out << '}';
68 return out.str();
69}
70
71static bool ValidateCodecFormats(const std::vector<VideoCodec>& codecs) {
72 bool has_video = false;
73 for (size_t i = 0; i < codecs.size(); ++i) {
74 if (!codecs[i].ValidateCodecFormat()) {
75 return false;
76 }
77 if (codecs[i].GetCodecType() == VideoCodec::CODEC_VIDEO) {
78 has_video = true;
79 }
80 }
81 if (!has_video) {
82 LOG(LS_ERROR) << "Setting codecs without a video codec is invalid: "
83 << CodecVectorToString(codecs);
84 return false;
85 }
86 return true;
87}
88
89static std::string RtpExtensionsToString(
90 const std::vector<RtpHeaderExtension>& extensions) {
91 std::stringstream out;
92 out << '{';
93 for (size_t i = 0; i < extensions.size(); ++i) {
94 out << "{" << extensions[i].uri << ": " << extensions[i].id << "}";
95 if (i != extensions.size() - 1) {
96 out << ", ";
97 }
98 }
99 out << '}';
100 return out.str();
101}
102
andresp@webrtc.org82775b12014-11-07 09:37:54 +0000103// Merges two fec configs and logs an error if a conflict arises
104// such that merging in diferent order would trigger a diferent output.
105static void MergeFecConfig(const webrtc::FecConfig& other,
106 webrtc::FecConfig* output) {
107 if (other.ulpfec_payload_type != -1) {
108 if (output->ulpfec_payload_type != -1 &&
109 output->ulpfec_payload_type != other.ulpfec_payload_type) {
110 LOG(LS_WARNING) << "Conflict merging ulpfec_payload_type configs: "
111 << output->ulpfec_payload_type << " and "
112 << other.ulpfec_payload_type;
113 }
114 output->ulpfec_payload_type = other.ulpfec_payload_type;
115 }
116 if (other.red_payload_type != -1) {
117 if (output->red_payload_type != -1 &&
118 output->red_payload_type != other.red_payload_type) {
119 LOG(LS_WARNING) << "Conflict merging red_payload_type configs: "
120 << output->red_payload_type << " and "
121 << other.red_payload_type;
122 }
123 output->red_payload_type = other.red_payload_type;
124 }
125}
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000126} // namespace
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000127
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000128// This constant is really an on/off, lower-level configurable NACK history
129// duration hasn't been implemented.
130static const int kNackHistoryMs = 1000;
131
buildbot@webrtc.org933d88a2014-09-18 20:23:05 +0000132static const int kDefaultQpMax = 56;
133
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000134static const int kDefaultRtcpReceiverReportSsrc = 1;
135
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000136// External video encoders are given payloads 120-127. This also means that we
137// only support up to 8 external payload types.
138static const int kExternalVideoPayloadTypeBase = 120;
139#ifndef NDEBUG
140static const size_t kMaxExternalVideoCodecs = 8;
141#endif
142
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000143const char kH264CodecName[] = "H264";
144
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000145static bool FindFirstMatchingCodec(const std::vector<VideoCodec>& codecs,
146 const VideoCodec& requested_codec,
147 VideoCodec* matching_codec) {
148 for (size_t i = 0; i < codecs.size(); ++i) {
149 if (requested_codec.Matches(codecs[i])) {
150 *matching_codec = codecs[i];
151 return true;
152 }
153 }
154 return false;
155}
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000156
pbos@webrtc.org3c107582014-07-20 15:27:35 +0000157static bool ValidateRtpHeaderExtensionIds(
158 const std::vector<RtpHeaderExtension>& extensions) {
159 std::set<int> extensions_used;
160 for (size_t i = 0; i < extensions.size(); ++i) {
161 if (extensions[i].id < 0 || extensions[i].id >= 15 ||
162 !extensions_used.insert(extensions[i].id).second) {
163 LOG(LS_ERROR) << "RTP extensions are with incorrect or duplicate ids.";
164 return false;
165 }
166 }
167 return true;
168}
169
pbos@webrtc.orgc37e72e2015-01-05 18:51:13 +0000170static bool CompareRtpHeaderExtensionIds(
171 const webrtc::RtpExtension& extension1,
172 const webrtc::RtpExtension& extension2) {
173 // Sorting on ID is sufficient, more than one extension per ID is unsupported.
174 return extension1.id > extension2.id;
175}
176
pbos@webrtc.org3c107582014-07-20 15:27:35 +0000177static std::vector<webrtc::RtpExtension> FilterRtpExtensions(
178 const std::vector<RtpHeaderExtension>& extensions) {
179 std::vector<webrtc::RtpExtension> webrtc_extensions;
180 for (size_t i = 0; i < extensions.size(); ++i) {
181 // Unsupported extensions will be ignored.
182 if (webrtc::RtpExtension::IsSupported(extensions[i].uri)) {
183 webrtc_extensions.push_back(webrtc::RtpExtension(
184 extensions[i].uri, extensions[i].id));
185 } else {
186 LOG(LS_WARNING) << "Unsupported RTP extension: " << extensions[i].uri;
187 }
188 }
pbos@webrtc.orgc37e72e2015-01-05 18:51:13 +0000189
190 // Sort filtered headers to make sure that they can later be compared
191 // regardless of in which order they were entered.
192 std::sort(webrtc_extensions.begin(), webrtc_extensions.end(),
193 CompareRtpHeaderExtensionIds);
pbos@webrtc.org3c107582014-07-20 15:27:35 +0000194 return webrtc_extensions;
195}
196
pbos@webrtc.orgc37e72e2015-01-05 18:51:13 +0000197static bool RtpExtensionsHaveChanged(
198 const std::vector<webrtc::RtpExtension>& before,
199 const std::vector<webrtc::RtpExtension>& after) {
200 if (before.size() != after.size())
201 return true;
202 for (size_t i = 0; i < before.size(); ++i) {
203 if (before[i].id != after[i].id)
204 return true;
205 if (before[i].name != after[i].name)
206 return true;
207 }
208 return false;
209}
210
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000211std::vector<webrtc::VideoStream>
pbos@webrtc.orgf1c8b902015-01-14 17:29:27 +0000212WebRtcVideoChannel2::WebRtcVideoSendStream::CreateSimulcastVideoStreams(
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000213 const VideoCodec& codec,
214 const VideoOptions& options,
215 size_t num_streams) {
216 // Use default factory for non-simulcast.
217 int max_qp = kDefaultQpMax;
218 codec.GetParam(kCodecParamMaxQuantization, &max_qp);
219
220 int min_bitrate_kbps;
221 if (!codec.GetParam(kCodecParamMinBitrate, &min_bitrate_kbps) ||
222 min_bitrate_kbps < kMinVideoBitrate) {
223 min_bitrate_kbps = kMinVideoBitrate;
224 }
225
226 int max_bitrate_kbps;
227 if (!codec.GetParam(kCodecParamMaxBitrate, &max_bitrate_kbps)) {
228 max_bitrate_kbps = 0;
229 }
230
231 return GetSimulcastConfig(
232 num_streams,
233 GetSimulcastBitrateMode(options),
234 codec.width,
235 codec.height,
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000236 max_bitrate_kbps * 1000,
237 max_qp,
238 codec.framerate != 0 ? codec.framerate : kDefaultVideoMaxFramerate);
239}
240
pbos@webrtc.orgf1c8b902015-01-14 17:29:27 +0000241std::vector<webrtc::VideoStream>
242WebRtcVideoChannel2::WebRtcVideoSendStream::CreateVideoStreams(
buildbot@webrtc.orgd41eaeb2014-06-12 07:13:26 +0000243 const VideoCodec& codec,
244 const VideoOptions& options,
245 size_t num_streams) {
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000246 if (num_streams != 1)
247 return CreateSimulcastVideoStreams(codec, options, num_streams);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000248
buildbot@webrtc.orgd41eaeb2014-06-12 07:13:26 +0000249 webrtc::VideoStream stream;
250 stream.width = codec.width;
251 stream.height = codec.height;
252 stream.max_framerate =
andresp@webrtc.org82775b12014-11-07 09:37:54 +0000253 codec.framerate != 0 ? codec.framerate : kDefaultVideoMaxFramerate;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000254
pbos@webrtc.org00873182014-11-25 14:03:34 +0000255 stream.min_bitrate_bps = kMinVideoBitrate * 1000;
256 stream.target_bitrate_bps = stream.max_bitrate_bps = kMaxVideoBitrate * 1000;
buildbot@webrtc.orgd41eaeb2014-06-12 07:13:26 +0000257
buildbot@webrtc.org933d88a2014-09-18 20:23:05 +0000258 int max_qp = kDefaultQpMax;
buildbot@webrtc.orgd41eaeb2014-06-12 07:13:26 +0000259 codec.GetParam(kCodecParamMaxQuantization, &max_qp);
260 stream.max_qp = max_qp;
261 std::vector<webrtc::VideoStream> streams;
262 streams.push_back(stream);
263 return streams;
264}
265
pbos@webrtc.orgf1c8b902015-01-14 17:29:27 +0000266void* WebRtcVideoChannel2::WebRtcVideoSendStream::ConfigureVideoEncoderSettings(
pbos@webrtc.org6f48f1b2014-07-22 16:29:54 +0000267 const VideoCodec& codec,
268 const VideoOptions& options) {
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000269 if (CodecNameMatches(codec.name, kVp8CodecName)) {
pbos@webrtc.orgf1c8b902015-01-14 17:29:27 +0000270 encoder_settings_.vp8 = webrtc::VideoEncoder::GetDefaultVp8Settings();
271 options.video_noise_reduction.Get(&encoder_settings_.vp8.denoisingOn);
272 return &encoder_settings_.vp8;
pbos@webrtc.org6f48f1b2014-07-22 16:29:54 +0000273 }
andresp@webrtc.org188d3b22014-11-07 13:21:04 +0000274 if (CodecNameMatches(codec.name, kVp9CodecName)) {
pbos@webrtc.orgf1c8b902015-01-14 17:29:27 +0000275 encoder_settings_.vp9 = webrtc::VideoEncoder::GetDefaultVp9Settings();
276 options.video_noise_reduction.Get(&encoder_settings_.vp9.denoisingOn);
277 return &encoder_settings_.vp9;
andresp@webrtc.org188d3b22014-11-07 13:21:04 +0000278 }
pbos@webrtc.org6f48f1b2014-07-22 16:29:54 +0000279 return NULL;
280}
281
pbos@webrtc.orgafb554f42014-08-12 23:17:13 +0000282DefaultUnsignalledSsrcHandler::DefaultUnsignalledSsrcHandler()
283 : default_recv_ssrc_(0), default_renderer_(NULL) {}
284
285UnsignalledSsrcHandler::Action DefaultUnsignalledSsrcHandler::OnUnsignalledSsrc(
286 VideoMediaChannel* channel,
287 uint32_t ssrc) {
288 if (default_recv_ssrc_ != 0) { // Already one default stream.
289 LOG(LS_WARNING) << "Unknown SSRC, but default receive stream already set.";
290 return kDropPacket;
291 }
292
293 StreamParams sp;
294 sp.ssrcs.push_back(ssrc);
295 LOG(LS_INFO) << "Creating default receive stream for SSRC=" << ssrc << ".";
296 if (!channel->AddRecvStream(sp)) {
297 LOG(LS_WARNING) << "Could not create default receive stream.";
298 }
299
300 channel->SetRenderer(ssrc, default_renderer_);
301 default_recv_ssrc_ = ssrc;
302 return kDeliverPacket;
303}
304
pbos@webrtc.org42684be2014-10-03 11:25:45 +0000305WebRtcCallFactory::~WebRtcCallFactory() {
306}
307webrtc::Call* WebRtcCallFactory::CreateCall(
308 const webrtc::Call::Config& config) {
309 return webrtc::Call::Create(config);
310}
311
pbos@webrtc.orgafb554f42014-08-12 23:17:13 +0000312VideoRenderer* DefaultUnsignalledSsrcHandler::GetDefaultRenderer() const {
313 return default_renderer_;
314}
315
316void DefaultUnsignalledSsrcHandler::SetDefaultRenderer(
317 VideoMediaChannel* channel,
318 VideoRenderer* renderer) {
319 default_renderer_ = renderer;
320 if (default_recv_ssrc_ != 0) {
321 channel->SetRenderer(default_recv_ssrc_, default_renderer_);
322 }
323}
324
pbos@webrtc.org97fdeb82014-08-22 10:36:23 +0000325WebRtcVideoEngine2::WebRtcVideoEngine2()
pbos@webrtc.orgb648b9d2014-08-26 11:08:06 +0000326 : worker_thread_(NULL),
327 voice_engine_(NULL),
andresp@webrtc.org82775b12014-11-07 09:37:54 +0000328 default_codec_format_(kDefaultVideoMaxWidth,
329 kDefaultVideoMaxHeight,
330 FPS_TO_INTERVAL(kDefaultVideoMaxFramerate),
buildbot@webrtc.org992febb2014-09-05 16:39:08 +0000331 FOURCC_ANY),
pbos@webrtc.orgb648b9d2014-08-26 11:08:06 +0000332 initialized_(false),
333 cpu_monitor_(new rtc::CpuMonitor(NULL)),
pbos@webrtc.org42684be2014-10-03 11:25:45 +0000334 call_factory_(&default_call_factory_),
pbos@webrtc.org0a2087a2014-09-23 09:40:22 +0000335 external_decoder_factory_(NULL),
336 external_encoder_factory_(NULL) {
pbos@webrtc.orgb648b9d2014-08-26 11:08:06 +0000337 LOG(LS_INFO) << "WebRtcVideoEngine2::WebRtcVideoEngine2()";
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000338 video_codecs_ = GetSupportedCodecs();
pbos@webrtc.org587ef602014-06-16 17:32:02 +0000339 rtp_header_extensions_.push_back(
340 RtpHeaderExtension(kRtpTimestampOffsetHeaderExtension,
341 kRtpTimestampOffsetHeaderExtensionDefaultId));
342 rtp_header_extensions_.push_back(
343 RtpHeaderExtension(kRtpAbsoluteSenderTimeHeaderExtension,
344 kRtpAbsoluteSenderTimeHeaderExtensionDefaultId));
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000345}
346
347WebRtcVideoEngine2::~WebRtcVideoEngine2() {
348 LOG(LS_INFO) << "WebRtcVideoEngine2::~WebRtcVideoEngine2";
349
350 if (initialized_) {
351 Terminate();
352 }
353}
354
pbos@webrtc.org42684be2014-10-03 11:25:45 +0000355void WebRtcVideoEngine2::SetCallFactory(WebRtcCallFactory* call_factory) {
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000356 assert(!initialized_);
pbos@webrtc.org42684be2014-10-03 11:25:45 +0000357 call_factory_ = call_factory;
358}
359
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000360bool WebRtcVideoEngine2::Init(rtc::Thread* worker_thread) {
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000361 LOG(LS_INFO) << "WebRtcVideoEngine2::Init";
362 worker_thread_ = worker_thread;
363 ASSERT(worker_thread_ != NULL);
364
365 cpu_monitor_->set_thread(worker_thread_);
366 if (!cpu_monitor_->Start(kCpuMonitorPeriodMs)) {
367 LOG(LS_ERROR) << "Failed to start CPU monitor.";
368 cpu_monitor_.reset();
369 }
370
371 initialized_ = true;
372 return true;
373}
374
375void WebRtcVideoEngine2::Terminate() {
376 LOG(LS_INFO) << "WebRtcVideoEngine2::Terminate";
377
pbos@webrtc.org0fb6ad22014-12-03 13:44:29 +0000378 if (cpu_monitor_.get() != NULL)
379 cpu_monitor_->Stop();
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000380
381 initialized_ = false;
382}
383
384int WebRtcVideoEngine2::GetCapabilities() { return VIDEO_RECV | VIDEO_SEND; }
385
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000386bool WebRtcVideoEngine2::SetDefaultEncoderConfig(
387 const VideoEncoderConfig& config) {
pbos@webrtc.org8fdeee62014-07-20 14:40:23 +0000388 const VideoCodec& codec = config.max_codec;
pbos@webrtc.org957e8022014-11-10 12:36:11 +0000389 bool supports_codec = false;
390 for (size_t i = 0; i < video_codecs_.size(); ++i) {
391 if (CodecNameMatches(video_codecs_[i].name, codec.name)) {
392 video_codecs_[i] = codec;
393 supports_codec = true;
394 break;
395 }
396 }
397
398 if (!supports_codec) {
399 LOG(LS_ERROR) << "SetDefaultEncoderConfig, codec not supported: "
pbos@webrtc.org8fdeee62014-07-20 14:40:23 +0000400 << codec.ToString();
401 return false;
402 }
403
buildbot@webrtc.org992febb2014-09-05 16:39:08 +0000404 default_codec_format_ =
405 VideoFormat(codec.width,
406 codec.height,
407 VideoFormat::FpsToInterval(codec.framerate),
408 FOURCC_ANY);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000409 return true;
410}
411
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000412WebRtcVideoChannel2* WebRtcVideoEngine2::CreateChannel(
buildbot@webrtc.org1ecbe452014-10-14 20:29:28 +0000413 const VideoOptions& options,
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000414 VoiceMediaChannel* voice_channel) {
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000415 assert(initialized_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000416 LOG(LS_INFO) << "CreateChannel: "
417 << (voice_channel != NULL ? "With" : "Without")
pbos@webrtc.orgfa553ef2014-10-20 11:07:07 +0000418 << " voice channel. Options: " << options.ToString();
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000419 WebRtcVideoChannel2* channel =
420 new WebRtcVideoChannel2(call_factory_,
pbos@webrtc.org3bf3d232014-10-31 12:59:34 +0000421 voice_engine_,
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000422 voice_channel,
pbos@webrtc.orgfa553ef2014-10-20 11:07:07 +0000423 options,
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000424 external_encoder_factory_,
pbos@webrtc.orgf1c8b902015-01-14 17:29:27 +0000425 external_decoder_factory_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000426 if (!channel->Init()) {
427 delete channel;
428 return NULL;
429 }
pbos@webrtc.orge322a172014-06-13 11:47:28 +0000430 channel->SetRecvCodecs(video_codecs_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000431 return channel;
432}
433
434const std::vector<VideoCodec>& WebRtcVideoEngine2::codecs() const {
435 return video_codecs_;
436}
437
438const std::vector<RtpHeaderExtension>&
439WebRtcVideoEngine2::rtp_header_extensions() const {
440 return rtp_header_extensions_;
441}
442
443void WebRtcVideoEngine2::SetLogging(int min_sev, const char* filter) {
444 // TODO(pbos): Set up logging.
445 LOG(LS_VERBOSE) << "SetLogging: " << min_sev << '"' << filter << '"';
446 // if min_sev == -1, we keep the current log level.
447 if (min_sev < 0) {
448 assert(min_sev == -1);
449 return;
450 }
451}
452
pbos@webrtc.org0a2087a2014-09-23 09:40:22 +0000453void WebRtcVideoEngine2::SetExternalDecoderFactory(
454 WebRtcVideoDecoderFactory* decoder_factory) {
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000455 assert(!initialized_);
pbos@webrtc.org0a2087a2014-09-23 09:40:22 +0000456 external_decoder_factory_ = decoder_factory;
457}
458
459void WebRtcVideoEngine2::SetExternalEncoderFactory(
460 WebRtcVideoEncoderFactory* encoder_factory) {
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000461 assert(!initialized_);
pbos@webrtc.orgf18fba22015-01-14 16:26:23 +0000462 if (external_encoder_factory_ == encoder_factory)
463 return;
464
465 // No matter what happens we shouldn't hold on to a stale
466 // WebRtcSimulcastEncoderFactory.
467 simulcast_encoder_factory_.reset();
468
469 if (encoder_factory &&
470 WebRtcSimulcastEncoderFactory::UseSimulcastEncoderFactory(
471 encoder_factory->codecs())) {
472 simulcast_encoder_factory_.reset(
473 new WebRtcSimulcastEncoderFactory(encoder_factory));
474 encoder_factory = simulcast_encoder_factory_.get();
475 }
pbos@webrtc.org0a2087a2014-09-23 09:40:22 +0000476 external_encoder_factory_ = encoder_factory;
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000477
478 video_codecs_ = GetSupportedCodecs();
pbos@webrtc.org0a2087a2014-09-23 09:40:22 +0000479}
480
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000481bool WebRtcVideoEngine2::EnableTimedRender() {
482 // TODO(pbos): Figure out whether this can be removed.
483 return true;
484}
485
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000486// Checks to see whether we comprehend and could receive a particular codec
487bool WebRtcVideoEngine2::FindCodec(const VideoCodec& in) {
488 // TODO(pbos): Probe encoder factory to figure out that the codec is supported
489 // if supported by the encoder factory. Add a corresponding test that fails
490 // with this code (that doesn't ask the factory).
pbos@webrtc.org8fdeee62014-07-20 14:40:23 +0000491 for (size_t j = 0; j < video_codecs_.size(); ++j) {
492 VideoCodec codec(video_codecs_[j].id, video_codecs_[j].name, 0, 0, 0, 0);
493 if (codec.Matches(in)) {
494 return true;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000495 }
496 }
497 return false;
498}
499
500// Tells whether the |requested| codec can be transmitted or not. If it can be
501// transmitted |out| is set with the best settings supported. Aspect ratio will
502// be set as close to |current|'s as possible. If not set |requested|'s
503// dimensions will be used for aspect ratio matching.
504bool WebRtcVideoEngine2::CanSendCodec(const VideoCodec& requested,
505 const VideoCodec& current,
506 VideoCodec* out) {
507 assert(out != NULL);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000508
509 if (requested.width != requested.height &&
510 (requested.height == 0 || requested.width == 0)) {
511 // 0xn and nx0 are invalid resolutions.
512 return false;
513 }
514
515 VideoCodec matching_codec;
516 if (!FindFirstMatchingCodec(video_codecs_, requested, &matching_codec)) {
517 // Codec not supported.
518 return false;
519 }
520
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000521 out->id = requested.id;
522 out->name = requested.name;
523 out->preference = requested.preference;
524 out->params = requested.params;
525 out->framerate =
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000526 rtc::_min(requested.framerate, matching_codec.framerate);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000527 out->params = requested.params;
528 out->feedback_params = requested.feedback_params;
pbos@webrtc.org8fdeee62014-07-20 14:40:23 +0000529 out->width = requested.width;
530 out->height = requested.height;
531 if (requested.width == 0 && requested.height == 0) {
532 return true;
533 }
534
535 while (out->width > matching_codec.width) {
536 out->width /= 2;
537 out->height /= 2;
538 }
539
540 return out->width > 0 && out->height > 0;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000541}
542
543bool WebRtcVideoEngine2::SetVoiceEngine(WebRtcVoiceEngine* voice_engine) {
544 if (initialized_) {
545 LOG(LS_WARNING) << "SetVoiceEngine can not be called after Init";
546 return false;
547 }
548 voice_engine_ = voice_engine;
549 return true;
550}
551
552// Ignore spammy trace messages, mostly from the stats API when we haven't
553// gotten RTCP info yet from the remote side.
554bool WebRtcVideoEngine2::ShouldIgnoreTrace(const std::string& trace) {
555 static const char* const kTracesToIgnore[] = {NULL};
556 for (const char* const* p = kTracesToIgnore; *p; ++p) {
557 if (trace.find(*p) == 0) {
558 return true;
559 }
560 }
561 return false;
562}
563
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000564std::vector<VideoCodec> WebRtcVideoEngine2::GetSupportedCodecs() const {
andresp@webrtc.org82775b12014-11-07 09:37:54 +0000565 std::vector<VideoCodec> supported_codecs = DefaultVideoCodecList();
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000566
567 if (external_encoder_factory_ == NULL) {
568 return supported_codecs;
569 }
570
571 assert(external_encoder_factory_->codecs().size() <= kMaxExternalVideoCodecs);
572 const std::vector<WebRtcVideoEncoderFactory::VideoCodec>& codecs =
573 external_encoder_factory_->codecs();
574 for (size_t i = 0; i < codecs.size(); ++i) {
575 // Don't add internally-supported codecs twice.
576 if (CodecIsInternallySupported(codecs[i].name)) {
577 continue;
578 }
579
580 VideoCodec codec(kExternalVideoPayloadTypeBase + static_cast<int>(i),
581 codecs[i].name,
582 codecs[i].max_width,
583 codecs[i].max_height,
584 codecs[i].max_fps,
585 0);
586
587 AddDefaultFeedbackParams(&codec);
588 supported_codecs.push_back(codec);
589 }
590 return supported_codecs;
591}
592
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000593WebRtcVideoChannel2::WebRtcVideoChannel2(
pbos@webrtc.org42684be2014-10-03 11:25:45 +0000594 WebRtcCallFactory* call_factory,
pbos@webrtc.org3bf3d232014-10-31 12:59:34 +0000595 WebRtcVoiceEngine* voice_engine,
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000596 VoiceMediaChannel* voice_channel,
pbos@webrtc.orgfa553ef2014-10-20 11:07:07 +0000597 const VideoOptions& options,
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000598 WebRtcVideoEncoderFactory* external_encoder_factory,
pbos@webrtc.orgf1c8b902015-01-14 17:29:27 +0000599 WebRtcVideoDecoderFactory* external_decoder_factory)
pbos@webrtc.orgb648b9d2014-08-26 11:08:06 +0000600 : unsignalled_ssrc_handler_(&default_unsignalled_ssrc_handler_),
pbos@webrtc.org3bf3d232014-10-31 12:59:34 +0000601 voice_channel_(voice_channel),
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000602 external_encoder_factory_(external_encoder_factory),
pbos@webrtc.orgf1c8b902015-01-14 17:29:27 +0000603 external_decoder_factory_(external_decoder_factory) {
pbos@webrtc.orgfa553ef2014-10-20 11:07:07 +0000604 SetDefaultOptions();
605 options_.SetAll(options);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000606 webrtc::Call::Config config(this);
pbos@webrtc.org42684be2014-10-03 11:25:45 +0000607 config.overuse_callback = this;
pbos@webrtc.org3bf3d232014-10-31 12:59:34 +0000608 if (voice_engine != NULL) {
609 config.voice_engine = voice_engine->voe()->engine();
610 }
pbos@webrtc.orgfa553ef2014-10-20 11:07:07 +0000611
pbos@webrtc.org42684be2014-10-03 11:25:45 +0000612 call_.reset(call_factory->CreateCall(config));
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000613
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000614 rtcp_receiver_report_ssrc_ = kDefaultRtcpReceiverReportSsrc;
615 sending_ = false;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000616 default_send_ssrc_ = 0;
pbos@webrtc.org6f48f1b2014-07-22 16:29:54 +0000617}
618
619void WebRtcVideoChannel2::SetDefaultOptions() {
pbos@webrtc.org42684be2014-10-03 11:25:45 +0000620 options_.cpu_overuse_detection.Set(false);
pbos@webrtc.orgd8198032014-11-10 14:41:43 +0000621 options_.dscp.Set(false);
pbos@webrtc.org5ff71ab2014-07-23 07:28:56 +0000622 options_.suspend_below_min_bitrate.Set(false);
pbos@webrtc.org42684be2014-10-03 11:25:45 +0000623 options_.video_noise_reduction.Set(true);
pbos@webrtc.orgefc82c22014-10-27 13:58:00 +0000624 options_.screencast_min_bitrate.Set(0);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000625}
626
627WebRtcVideoChannel2::~WebRtcVideoChannel2() {
628 for (std::map<uint32, WebRtcVideoSendStream*>::iterator it =
629 send_streams_.begin();
630 it != send_streams_.end();
631 ++it) {
632 delete it->second;
633 }
634
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000635 for (std::map<uint32, WebRtcVideoReceiveStream*>::iterator it =
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000636 receive_streams_.begin();
637 it != receive_streams_.end();
638 ++it) {
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000639 delete it->second;
640 }
641}
642
643bool WebRtcVideoChannel2::Init() { return true; }
644
pbos@webrtc.org96a93252014-11-03 14:46:44 +0000645bool WebRtcVideoChannel2::CodecIsExternallySupported(
646 const std::string& name) const {
647 if (external_encoder_factory_ == NULL) {
648 return false;
649 }
650
651 const std::vector<WebRtcVideoEncoderFactory::VideoCodec> external_codecs =
652 external_encoder_factory_->codecs();
653 for (size_t c = 0; c < external_codecs.size(); ++c) {
654 if (CodecNameMatches(name, external_codecs[c].name)) {
655 return true;
656 }
657 }
658 return false;
659}
660
661std::vector<WebRtcVideoChannel2::VideoCodecSettings>
662WebRtcVideoChannel2::FilterSupportedCodecs(
663 const std::vector<WebRtcVideoChannel2::VideoCodecSettings>& mapped_codecs)
664 const {
665 std::vector<VideoCodecSettings> supported_codecs;
666 for (size_t i = 0; i < mapped_codecs.size(); ++i) {
667 const VideoCodecSettings& codec = mapped_codecs[i];
668 if (CodecIsInternallySupported(codec.codec.name) ||
669 CodecIsExternallySupported(codec.codec.name)) {
670 supported_codecs.push_back(codec);
671 }
672 }
673 return supported_codecs;
674}
675
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000676bool WebRtcVideoChannel2::SetRecvCodecs(const std::vector<VideoCodec>& codecs) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000677 TRACE_EVENT0("webrtc", "WebRtcVideoChannel2::SetRecvCodecs");
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000678 LOG(LS_INFO) << "SetRecvCodecs: " << CodecVectorToString(codecs);
679 if (!ValidateCodecFormats(codecs)) {
680 return false;
681 }
682
683 const std::vector<VideoCodecSettings> mapped_codecs = MapCodecs(codecs);
684 if (mapped_codecs.empty()) {
pbos@webrtc.org96a93252014-11-03 14:46:44 +0000685 LOG(LS_ERROR) << "SetRecvCodecs called without any video codecs.";
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000686 return false;
687 }
688
pbos@webrtc.org96a93252014-11-03 14:46:44 +0000689 const std::vector<VideoCodecSettings> supported_codecs =
690 FilterSupportedCodecs(mapped_codecs);
691
692 if (mapped_codecs.size() != supported_codecs.size()) {
693 LOG(LS_ERROR) << "SetRecvCodecs called with unsupported video codecs.";
694 return false;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000695 }
696
pbos@webrtc.org96a93252014-11-03 14:46:44 +0000697 recv_codecs_ = supported_codecs;
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000698
pbos@webrtc.org575d1262014-10-08 14:48:08 +0000699 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000700 for (std::map<uint32, WebRtcVideoReceiveStream*>::iterator it =
701 receive_streams_.begin();
702 it != receive_streams_.end();
703 ++it) {
704 it->second->SetRecvCodecs(recv_codecs_);
705 }
706
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000707 return true;
708}
709
710bool WebRtcVideoChannel2::SetSendCodecs(const std::vector<VideoCodec>& codecs) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000711 TRACE_EVENT0("webrtc", "WebRtcVideoChannel2::SetSendCodecs");
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000712 LOG(LS_INFO) << "SetSendCodecs: " << CodecVectorToString(codecs);
713 if (!ValidateCodecFormats(codecs)) {
714 return false;
715 }
716
717 const std::vector<VideoCodecSettings> supported_codecs =
718 FilterSupportedCodecs(MapCodecs(codecs));
719
720 if (supported_codecs.empty()) {
721 LOG(LS_ERROR) << "No video codecs supported by encoder factory.";
722 return false;
723 }
724
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000725 LOG(LS_INFO) << "Using codec: " << supported_codecs.front().codec.ToString();
726
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +0000727 VideoCodecSettings old_codec;
728 if (send_codec_.Get(&old_codec) && supported_codecs.front() == old_codec) {
729 // Using same codec, avoid reconfiguring.
730 return true;
731 }
732
733 send_codec_.Set(supported_codecs.front());
734
pbos@webrtc.org575d1262014-10-08 14:48:08 +0000735 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000736 for (std::map<uint32, WebRtcVideoSendStream*>::iterator it =
737 send_streams_.begin();
738 it != send_streams_.end();
739 ++it) {
740 assert(it->second != NULL);
741 it->second->SetCodec(supported_codecs.front());
742 }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000743
pbos@webrtc.org00873182014-11-25 14:03:34 +0000744 VideoCodec codec = supported_codecs.front().codec;
745 int bitrate_kbps;
746 if (codec.GetParam(kCodecParamMinBitrate, &bitrate_kbps) &&
747 bitrate_kbps > 0) {
748 bitrate_config_.min_bitrate_bps = bitrate_kbps * 1000;
749 } else {
750 bitrate_config_.min_bitrate_bps = 0;
751 }
752 if (codec.GetParam(kCodecParamStartBitrate, &bitrate_kbps) &&
753 bitrate_kbps > 0) {
754 bitrate_config_.start_bitrate_bps = bitrate_kbps * 1000;
755 } else {
756 // Do not reconfigure start bitrate unless it's specified and positive.
757 bitrate_config_.start_bitrate_bps = -1;
758 }
759 if (codec.GetParam(kCodecParamMaxBitrate, &bitrate_kbps) &&
760 bitrate_kbps > 0) {
761 bitrate_config_.max_bitrate_bps = bitrate_kbps * 1000;
762 } else {
763 bitrate_config_.max_bitrate_bps = -1;
764 }
765 call_->SetBitrateConfig(bitrate_config_);
766
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000767 return true;
768}
769
770bool WebRtcVideoChannel2::GetSendCodec(VideoCodec* codec) {
771 VideoCodecSettings codec_settings;
772 if (!send_codec_.Get(&codec_settings)) {
773 LOG(LS_VERBOSE) << "GetSendCodec: No send codec set.";
774 return false;
775 }
776 *codec = codec_settings.codec;
777 return true;
778}
779
780bool WebRtcVideoChannel2::SetSendStreamFormat(uint32 ssrc,
781 const VideoFormat& format) {
782 LOG(LS_VERBOSE) << "SetSendStreamFormat:" << ssrc << " -> "
783 << format.ToString();
pbos@webrtc.org575d1262014-10-08 14:48:08 +0000784 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000785 if (send_streams_.find(ssrc) == send_streams_.end()) {
786 return false;
787 }
788 return send_streams_[ssrc]->SetVideoFormat(format);
789}
790
791bool WebRtcVideoChannel2::SetRender(bool render) {
792 // TODO(pbos): Implement. Or refactor away as it shouldn't be needed.
793 LOG(LS_VERBOSE) << "SetRender: " << (render ? "true" : "false");
794 return true;
795}
796
797bool WebRtcVideoChannel2::SetSend(bool send) {
798 LOG(LS_VERBOSE) << "SetSend: " << (send ? "true" : "false");
799 if (send && !send_codec_.IsSet()) {
800 LOG(LS_ERROR) << "SetSend(true) called before setting codec.";
801 return false;
802 }
803 if (send) {
804 StartAllSendStreams();
805 } else {
806 StopAllSendStreams();
807 }
808 sending_ = send;
809 return true;
810}
811
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000812bool WebRtcVideoChannel2::AddSendStream(const StreamParams& sp) {
813 LOG(LS_INFO) << "AddSendStream: " << sp.ToString();
814 if (sp.ssrcs.empty()) {
815 LOG(LS_ERROR) << "No SSRCs in stream parameters.";
816 return false;
817 }
818
819 uint32 ssrc = sp.first_ssrc();
820 assert(ssrc != 0);
821 // TODO(pbos): Make sure none of sp.ssrcs are used, not just the identifying
822 // ssrc.
pbos@webrtc.org575d1262014-10-08 14:48:08 +0000823 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000824 if (send_streams_.find(ssrc) != send_streams_.end()) {
825 LOG(LS_ERROR) << "Send stream with ssrc '" << ssrc << "' already exists.";
826 return false;
827 }
828
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +0000829 std::vector<uint32> primary_ssrcs;
830 sp.GetPrimarySsrcs(&primary_ssrcs);
831 std::vector<uint32> rtx_ssrcs;
832 sp.GetFidSsrcs(primary_ssrcs, &rtx_ssrcs);
833 if (!rtx_ssrcs.empty() && primary_ssrcs.size() != rtx_ssrcs.size()) {
834 LOG(LS_ERROR)
835 << "RTX SSRCs exist, but don't cover all SSRCs (unsupported): "
836 << sp.ToString();
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000837 return false;
838 }
839
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000840 WebRtcVideoSendStream* stream =
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000841 new WebRtcVideoSendStream(call_.get(),
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000842 external_encoder_factory_,
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000843 options_,
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +0000844 send_codec_,
845 sp,
846 send_rtp_extensions_);
847
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000848 send_streams_[ssrc] = stream;
849
850 if (rtcp_receiver_report_ssrc_ == kDefaultRtcpReceiverReportSsrc) {
851 rtcp_receiver_report_ssrc_ = ssrc;
852 }
853 if (default_send_ssrc_ == 0) {
854 default_send_ssrc_ = ssrc;
855 }
856 if (sending_) {
857 stream->Start();
858 }
859
860 return true;
861}
862
863bool WebRtcVideoChannel2::RemoveSendStream(uint32 ssrc) {
864 LOG(LS_INFO) << "RemoveSendStream: " << ssrc;
865
866 if (ssrc == 0) {
867 if (default_send_ssrc_ == 0) {
868 LOG(LS_ERROR) << "No default send stream active.";
869 return false;
870 }
871
872 LOG(LS_VERBOSE) << "Removing default stream: " << default_send_ssrc_;
873 ssrc = default_send_ssrc_;
874 }
875
pbos@webrtc.org575d1262014-10-08 14:48:08 +0000876 WebRtcVideoSendStream* removed_stream;
877 {
878 rtc::CritScope stream_lock(&stream_crit_);
879 std::map<uint32, WebRtcVideoSendStream*>::iterator it =
880 send_streams_.find(ssrc);
881 if (it == send_streams_.end()) {
882 return false;
883 }
884
885 removed_stream = it->second;
886 send_streams_.erase(it);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000887 }
888
pbos@webrtc.org575d1262014-10-08 14:48:08 +0000889 delete removed_stream;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000890
891 if (ssrc == default_send_ssrc_) {
892 default_send_ssrc_ = 0;
893 }
894
895 return true;
896}
897
898bool WebRtcVideoChannel2::AddRecvStream(const StreamParams& sp) {
899 LOG(LS_INFO) << "AddRecvStream: " << sp.ToString();
900 assert(sp.ssrcs.size() > 0);
901
902 uint32 ssrc = sp.first_ssrc();
903 assert(ssrc != 0); // TODO(pbos): Is this ever valid?
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000904
905 // TODO(pbos): Check if any of the SSRCs overlap.
pbos@webrtc.org575d1262014-10-08 14:48:08 +0000906 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000907 if (receive_streams_.find(ssrc) != receive_streams_.end()) {
908 LOG(LS_ERROR) << "Receive stream for SSRC " << ssrc << "already exists.";
909 return false;
910 }
911
pbos@webrtc.orgbd249bc2014-07-07 04:45:15 +0000912 webrtc::VideoReceiveStream::Config config;
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000913 ConfigureReceiverRtp(&config, sp);
pbos@webrtc.org3bf3d232014-10-31 12:59:34 +0000914
915 // Set up A/V sync if there is a VoiceChannel.
916 // TODO(pbos): The A/V is synched by the receiving channel. So we need to know
917 // the SSRC of the remote audio channel in order to sync the correct webrtc
918 // VoiceEngine channel. For now sync the first channel in non-conference to
919 // match existing behavior in WebRtcVideoEngine.
920 if (voice_channel_ != NULL && receive_streams_.empty() &&
921 !options_.conference_mode.GetWithDefaultIfUnset(false)) {
922 config.audio_channel_id =
923 static_cast<WebRtcVoiceMediaChannel*>(voice_channel_)->voe_channel();
924 }
925
pbos@webrtc.org776e6f22014-10-29 15:28:39 +0000926 receive_streams_[ssrc] = new WebRtcVideoReceiveStream(
927 call_.get(), external_decoder_factory_, config, recv_codecs_);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000928
929 return true;
930}
931
932void WebRtcVideoChannel2::ConfigureReceiverRtp(
933 webrtc::VideoReceiveStream::Config* config,
934 const StreamParams& sp) const {
935 uint32 ssrc = sp.first_ssrc();
936
937 config->rtp.remote_ssrc = ssrc;
938 config->rtp.local_ssrc = rtcp_receiver_report_ssrc_;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000939
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000940 config->rtp.extensions = recv_rtp_extensions_;
pbos@webrtc.org257e1302014-07-25 19:01:32 +0000941
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000942 // TODO(pbos): This protection is against setting the same local ssrc as
943 // remote which is not permitted by the lower-level API. RTCP requires a
944 // corresponding sender SSRC. Figure out what to do when we don't have
945 // (receive-only) or know a good local SSRC.
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000946 if (config->rtp.remote_ssrc == config->rtp.local_ssrc) {
947 if (config->rtp.local_ssrc != kDefaultRtcpReceiverReportSsrc) {
948 config->rtp.local_ssrc = kDefaultRtcpReceiverReportSsrc;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000949 } else {
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000950 config->rtp.local_ssrc = kDefaultRtcpReceiverReportSsrc + 1;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000951 }
952 }
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000953
954 for (size_t i = 0; i < recv_codecs_.size(); ++i) {
andresp@webrtc.org82775b12014-11-07 09:37:54 +0000955 MergeFecConfig(recv_codecs_[i].fec, &config->rtp.fec);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000956 }
957
andresp@webrtc.org82775b12014-11-07 09:37:54 +0000958 for (size_t i = 0; i < recv_codecs_.size(); ++i) {
959 uint32 rtx_ssrc;
960 if (recv_codecs_[i].rtx_payload_type != -1 &&
961 sp.GetFidSsrc(ssrc, &rtx_ssrc)) {
962 webrtc::VideoReceiveStream::Config::Rtp::Rtx& rtx =
963 config->rtp.rtx[recv_codecs_[i].codec.id];
964 rtx.ssrc = rtx_ssrc;
965 rtx.payload_type = recv_codecs_[i].rtx_payload_type;
966 }
967 }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000968}
969
970bool WebRtcVideoChannel2::RemoveRecvStream(uint32 ssrc) {
971 LOG(LS_INFO) << "RemoveRecvStream: " << ssrc;
972 if (ssrc == 0) {
pbos@webrtc.orgafb554f42014-08-12 23:17:13 +0000973 LOG(LS_ERROR) << "RemoveRecvStream with 0 ssrc is not supported.";
974 return false;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000975 }
976
pbos@webrtc.org575d1262014-10-08 14:48:08 +0000977 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000978 std::map<uint32, WebRtcVideoReceiveStream*>::iterator stream =
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000979 receive_streams_.find(ssrc);
980 if (stream == receive_streams_.end()) {
981 LOG(LS_ERROR) << "Stream not found for ssrc: " << ssrc;
982 return false;
983 }
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000984 delete stream->second;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000985 receive_streams_.erase(stream);
986
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000987 return true;
988}
989
990bool WebRtcVideoChannel2::SetRenderer(uint32 ssrc, VideoRenderer* renderer) {
991 LOG(LS_INFO) << "SetRenderer: ssrc:" << ssrc << " "
992 << (renderer ? "(ptr)" : "NULL");
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000993 if (ssrc == 0) {
pbos@webrtc.orgafb554f42014-08-12 23:17:13 +0000994 default_unsignalled_ssrc_handler_.SetDefaultRenderer(this, renderer);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000995 return true;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000996 }
997
pbos@webrtc.org575d1262014-10-08 14:48:08 +0000998 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000999 std::map<uint32, WebRtcVideoReceiveStream*>::iterator it =
1000 receive_streams_.find(ssrc);
1001 if (it == receive_streams_.end()) {
1002 return false;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001003 }
1004
1005 it->second->SetRenderer(renderer);
1006 return true;
1007}
1008
1009bool WebRtcVideoChannel2::GetRenderer(uint32 ssrc, VideoRenderer** renderer) {
1010 if (ssrc == 0) {
pbos@webrtc.orgafb554f42014-08-12 23:17:13 +00001011 *renderer = default_unsignalled_ssrc_handler_.GetDefaultRenderer();
1012 return *renderer != NULL;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001013 }
1014
pbos@webrtc.org575d1262014-10-08 14:48:08 +00001015 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001016 std::map<uint32, WebRtcVideoReceiveStream*>::iterator it =
1017 receive_streams_.find(ssrc);
1018 if (it == receive_streams_.end()) {
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001019 return false;
1020 }
1021 *renderer = it->second->GetRenderer();
1022 return true;
1023}
1024
1025bool WebRtcVideoChannel2::GetStats(const StatsOptions& options,
1026 VideoMediaInfo* info) {
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00001027 info->Clear();
1028 FillSenderStats(info);
1029 FillReceiverStats(info);
pbos@webrtc.org2b19f062014-12-11 13:26:09 +00001030 webrtc::Call::Stats stats = call_->GetStats();
1031 FillBandwidthEstimationStats(stats, info);
1032 if (stats.rtt_ms != -1) {
1033 for (size_t i = 0; i < info->senders.size(); ++i) {
1034 info->senders[i].rtt_ms = stats.rtt_ms;
1035 }
1036 }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001037 return true;
1038}
1039
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00001040void WebRtcVideoChannel2::FillSenderStats(VideoMediaInfo* video_media_info) {
pbos@webrtc.org575d1262014-10-08 14:48:08 +00001041 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00001042 for (std::map<uint32, WebRtcVideoSendStream*>::iterator it =
1043 send_streams_.begin();
1044 it != send_streams_.end();
1045 ++it) {
1046 video_media_info->senders.push_back(it->second->GetVideoSenderInfo());
1047 }
1048}
1049
1050void WebRtcVideoChannel2::FillReceiverStats(VideoMediaInfo* video_media_info) {
pbos@webrtc.org575d1262014-10-08 14:48:08 +00001051 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00001052 for (std::map<uint32, WebRtcVideoReceiveStream*>::iterator it =
1053 receive_streams_.begin();
1054 it != receive_streams_.end();
1055 ++it) {
1056 video_media_info->receivers.push_back(it->second->GetVideoReceiverInfo());
1057 }
1058}
1059
1060void WebRtcVideoChannel2::FillBandwidthEstimationStats(
pbos@webrtc.org2b19f062014-12-11 13:26:09 +00001061 const webrtc::Call::Stats& stats,
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00001062 VideoMediaInfo* video_media_info) {
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001063 BandwidthEstimationInfo bwe_info;
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001064 bwe_info.available_send_bandwidth = stats.send_bandwidth_bps;
1065 bwe_info.available_recv_bandwidth = stats.recv_bandwidth_bps;
1066 bwe_info.bucket_delay = stats.pacer_delay_ms;
1067
1068 // Get send stream bitrate stats.
1069 rtc::CritScope stream_lock(&stream_crit_);
1070 for (std::map<uint32, WebRtcVideoSendStream*>::iterator stream =
1071 send_streams_.begin();
1072 stream != send_streams_.end();
1073 ++stream) {
1074 stream->second->FillBandwidthEstimationInfo(&bwe_info);
1075 }
1076 video_media_info->bw_estimations.push_back(bwe_info);
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00001077}
1078
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001079bool WebRtcVideoChannel2::SetCapturer(uint32 ssrc, VideoCapturer* capturer) {
1080 LOG(LS_INFO) << "SetCapturer: " << ssrc << " -> "
1081 << (capturer != NULL ? "(capturer)" : "NULL");
1082 assert(ssrc != 0);
pbos@webrtc.org575d1262014-10-08 14:48:08 +00001083 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001084 if (send_streams_.find(ssrc) == send_streams_.end()) {
1085 LOG(LS_ERROR) << "No sending stream on ssrc " << ssrc;
1086 return false;
1087 }
1088 return send_streams_[ssrc]->SetCapturer(capturer);
1089}
1090
1091bool WebRtcVideoChannel2::SendIntraFrame() {
1092 // TODO(pbos): Implement.
1093 LOG(LS_VERBOSE) << "SendIntraFrame().";
1094 return true;
1095}
1096
1097bool WebRtcVideoChannel2::RequestIntraFrame() {
1098 // TODO(pbos): Implement.
1099 LOG(LS_VERBOSE) << "SendIntraFrame().";
1100 return true;
1101}
1102
1103void WebRtcVideoChannel2::OnPacketReceived(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001104 rtc::Buffer* packet,
1105 const rtc::PacketTime& packet_time) {
pbos@webrtc.org4e545cc2014-05-14 13:58:13 +00001106 const webrtc::PacketReceiver::DeliveryStatus delivery_result =
1107 call_->Receiver()->DeliverPacket(
1108 reinterpret_cast<const uint8_t*>(packet->data()), packet->length());
1109 switch (delivery_result) {
1110 case webrtc::PacketReceiver::DELIVERY_OK:
1111 return;
1112 case webrtc::PacketReceiver::DELIVERY_PACKET_ERROR:
1113 return;
1114 case webrtc::PacketReceiver::DELIVERY_UNKNOWN_SSRC:
1115 break;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001116 }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001117
1118 uint32 ssrc = 0;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001119 if (!GetRtpSsrc(packet->data(), packet->length(), &ssrc)) {
1120 return;
1121 }
1122
pbos@webrtc.orgafb554f42014-08-12 23:17:13 +00001123 // TODO(pbos): Make sure that the unsignalled SSRC uses the video payload.
1124 // Also figure out whether RTX needs to be handled.
1125 switch (unsignalled_ssrc_handler_->OnUnsignalledSsrc(this, ssrc)) {
1126 case UnsignalledSsrcHandler::kDropPacket:
1127 return;
1128 case UnsignalledSsrcHandler::kDeliverPacket:
1129 break;
1130 }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001131
pbos@webrtc.org1e019d12014-05-16 11:38:45 +00001132 if (call_->Receiver()->DeliverPacket(
1133 reinterpret_cast<const uint8_t*>(packet->data()), packet->length()) !=
1134 webrtc::PacketReceiver::DELIVERY_OK) {
pbos@webrtc.orgafb554f42014-08-12 23:17:13 +00001135 LOG(LS_WARNING) << "Failed to deliver RTP packet on re-delivery.";
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001136 return;
1137 }
1138}
1139
1140void WebRtcVideoChannel2::OnRtcpReceived(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001141 rtc::Buffer* packet,
1142 const rtc::PacketTime& packet_time) {
pbos@webrtc.org1e019d12014-05-16 11:38:45 +00001143 if (call_->Receiver()->DeliverPacket(
1144 reinterpret_cast<const uint8_t*>(packet->data()), packet->length()) !=
1145 webrtc::PacketReceiver::DELIVERY_OK) {
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001146 LOG(LS_WARNING) << "Failed to deliver RTCP packet.";
1147 }
1148}
1149
1150void WebRtcVideoChannel2::OnReadyToSend(bool ready) {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +00001151 LOG(LS_VERBOSE) << "OnReadyToSend: " << (ready ? "Ready." : "Not ready.");
1152 call_->SignalNetworkState(ready ? webrtc::Call::kNetworkUp
1153 : webrtc::Call::kNetworkDown);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001154}
1155
1156bool WebRtcVideoChannel2::MuteStream(uint32 ssrc, bool mute) {
1157 LOG(LS_VERBOSE) << "MuteStream: " << ssrc << " -> "
1158 << (mute ? "mute" : "unmute");
1159 assert(ssrc != 0);
pbos@webrtc.org575d1262014-10-08 14:48:08 +00001160 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001161 if (send_streams_.find(ssrc) == send_streams_.end()) {
1162 LOG(LS_ERROR) << "No sending stream on ssrc " << ssrc;
1163 return false;
1164 }
pbos@webrtc.orgef8bb8d2014-08-13 21:36:18 +00001165
1166 send_streams_[ssrc]->MuteStream(mute);
1167 return true;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001168}
1169
1170bool WebRtcVideoChannel2::SetRecvRtpHeaderExtensions(
1171 const std::vector<RtpHeaderExtension>& extensions) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +00001172 TRACE_EVENT0("webrtc", "WebRtcVideoChannel2::SetRecvRtpHeaderExtensions");
pbos@webrtc.org587ef602014-06-16 17:32:02 +00001173 LOG(LS_INFO) << "SetRecvRtpHeaderExtensions: "
1174 << RtpExtensionsToString(extensions);
pbos@webrtc.org3c107582014-07-20 15:27:35 +00001175 if (!ValidateRtpHeaderExtensionIds(extensions))
1176 return false;
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001177
pbos@webrtc.orgc37e72e2015-01-05 18:51:13 +00001178 std::vector<webrtc::RtpExtension> filtered_extensions =
1179 FilterRtpExtensions(extensions);
1180 if (!RtpExtensionsHaveChanged(recv_rtp_extensions_, filtered_extensions))
1181 return true;
1182
1183 recv_rtp_extensions_ = filtered_extensions;
1184
pbos@webrtc.org575d1262014-10-08 14:48:08 +00001185 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001186 for (std::map<uint32, WebRtcVideoReceiveStream*>::iterator it =
1187 receive_streams_.begin();
1188 it != receive_streams_.end();
1189 ++it) {
1190 it->second->SetRtpExtensions(recv_rtp_extensions_);
1191 }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001192 return true;
1193}
1194
1195bool WebRtcVideoChannel2::SetSendRtpHeaderExtensions(
1196 const std::vector<RtpHeaderExtension>& extensions) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +00001197 TRACE_EVENT0("webrtc", "WebRtcVideoChannel2::SetSendRtpHeaderExtensions");
pbos@webrtc.org587ef602014-06-16 17:32:02 +00001198 LOG(LS_INFO) << "SetSendRtpHeaderExtensions: "
1199 << RtpExtensionsToString(extensions);
pbos@webrtc.org3c107582014-07-20 15:27:35 +00001200 if (!ValidateRtpHeaderExtensionIds(extensions))
1201 return false;
1202
pbos@webrtc.orgc37e72e2015-01-05 18:51:13 +00001203 std::vector<webrtc::RtpExtension> filtered_extensions =
1204 FilterRtpExtensions(extensions);
1205 if (!RtpExtensionsHaveChanged(send_rtp_extensions_, filtered_extensions))
1206 return true;
1207
1208 send_rtp_extensions_ = filtered_extensions;
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +00001209
pbos@webrtc.org575d1262014-10-08 14:48:08 +00001210 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001211 for (std::map<uint32, WebRtcVideoSendStream*>::iterator it =
1212 send_streams_.begin();
1213 it != send_streams_.end();
1214 ++it) {
1215 it->second->SetRtpExtensions(send_rtp_extensions_);
1216 }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001217 return true;
1218}
1219
pbos@webrtc.org00873182014-11-25 14:03:34 +00001220bool WebRtcVideoChannel2::SetMaxSendBandwidth(int max_bitrate_bps) {
1221 LOG(LS_INFO) << "SetMaxSendBandwidth: " << max_bitrate_bps << "bps.";
1222 if (max_bitrate_bps <= 0) {
1223 // Unsetting max bitrate.
1224 max_bitrate_bps = -1;
1225 }
1226 bitrate_config_.start_bitrate_bps = -1;
1227 bitrate_config_.max_bitrate_bps = max_bitrate_bps;
1228 if (max_bitrate_bps > 0 &&
1229 bitrate_config_.min_bitrate_bps > max_bitrate_bps) {
1230 bitrate_config_.min_bitrate_bps = max_bitrate_bps;
1231 }
1232 call_->SetBitrateConfig(bitrate_config_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001233 return true;
1234}
1235
1236bool WebRtcVideoChannel2::SetOptions(const VideoOptions& options) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +00001237 TRACE_EVENT0("webrtc", "WebRtcVideoChannel2::SetOptions");
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +00001238 LOG(LS_INFO) << "SetOptions: " << options.ToString();
1239 VideoOptions old_options = options_;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001240 options_.SetAll(options);
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +00001241 if (options_ == old_options) {
1242 // No new options to set.
1243 return true;
1244 }
pbos@webrtc.orgd8198032014-11-10 14:41:43 +00001245 rtc::DiffServCodePoint dscp = options_.dscp.GetWithDefaultIfUnset(false)
1246 ? rtc::DSCP_AF41
1247 : rtc::DSCP_DEFAULT;
1248 MediaChannel::SetDscp(dscp);
pbos@webrtc.org575d1262014-10-08 14:48:08 +00001249 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001250 for (std::map<uint32, WebRtcVideoSendStream*>::iterator it =
1251 send_streams_.begin();
1252 it != send_streams_.end();
1253 ++it) {
1254 it->second->SetOptions(options_);
1255 }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001256 return true;
1257}
1258
1259void WebRtcVideoChannel2::SetInterface(NetworkInterface* iface) {
1260 MediaChannel::SetInterface(iface);
1261 // Set the RTP recv/send buffer to a bigger size
1262 MediaChannel::SetOption(NetworkInterface::ST_RTP,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001263 rtc::Socket::OPT_RCVBUF,
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001264 kVideoRtpBufferSize);
1265
buildbot@webrtc.orgae694ef2014-10-28 17:37:17 +00001266 // Speculative change to increase the outbound socket buffer size.
1267 // In b/15152257, we are seeing a significant number of packets discarded
1268 // due to lack of socket buffer space, although it's not yet clear what the
1269 // ideal value should be.
1270 MediaChannel::SetOption(NetworkInterface::ST_RTP,
1271 rtc::Socket::OPT_SNDBUF,
1272 kVideoRtpBufferSize);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001273}
1274
1275void WebRtcVideoChannel2::UpdateAspectRatio(int ratio_w, int ratio_h) {
1276 // TODO(pbos): Implement.
1277}
1278
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001279void WebRtcVideoChannel2::OnMessage(rtc::Message* msg) {
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001280 // Ignored.
1281}
1282
pbos@webrtc.org42684be2014-10-03 11:25:45 +00001283void WebRtcVideoChannel2::OnLoadUpdate(Load load) {
pbos@webrtc.org575d1262014-10-08 14:48:08 +00001284 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.org42684be2014-10-03 11:25:45 +00001285 for (std::map<uint32, WebRtcVideoSendStream*>::iterator it =
1286 send_streams_.begin();
1287 it != send_streams_.end();
1288 ++it) {
1289 it->second->OnCpuResolutionRequest(load == kOveruse
1290 ? CoordinatedVideoAdapter::DOWNGRADE
1291 : CoordinatedVideoAdapter::UPGRADE);
1292 }
1293}
1294
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001295bool WebRtcVideoChannel2::SendRtp(const uint8_t* data, size_t len) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001296 rtc::Buffer packet(data, len, kMaxRtpPacketLen);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001297 return MediaChannel::SendPacket(&packet);
1298}
1299
1300bool WebRtcVideoChannel2::SendRtcp(const uint8_t* data, size_t len) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001301 rtc::Buffer packet(data, len, kMaxRtpPacketLen);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001302 return MediaChannel::SendRtcp(&packet);
1303}
1304
1305void WebRtcVideoChannel2::StartAllSendStreams() {
pbos@webrtc.org575d1262014-10-08 14:48:08 +00001306 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001307 for (std::map<uint32, WebRtcVideoSendStream*>::iterator it =
1308 send_streams_.begin();
1309 it != send_streams_.end();
1310 ++it) {
1311 it->second->Start();
1312 }
1313}
1314
1315void WebRtcVideoChannel2::StopAllSendStreams() {
pbos@webrtc.org575d1262014-10-08 14:48:08 +00001316 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001317 for (std::map<uint32, WebRtcVideoSendStream*>::iterator it =
1318 send_streams_.begin();
1319 it != send_streams_.end();
1320 ++it) {
1321 it->second->Stop();
1322 }
1323}
1324
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +00001325WebRtcVideoChannel2::WebRtcVideoSendStream::VideoSendStreamParameters::
1326 VideoSendStreamParameters(
1327 const webrtc::VideoSendStream::Config& config,
1328 const VideoOptions& options,
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001329 const Settable<VideoCodecSettings>& codec_settings)
1330 : config(config), options(options), codec_settings(codec_settings) {
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +00001331}
1332
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001333WebRtcVideoChannel2::WebRtcVideoSendStream::WebRtcVideoSendStream(
1334 webrtc::Call* call,
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +00001335 WebRtcVideoEncoderFactory* external_encoder_factory,
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +00001336 const VideoOptions& options,
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001337 const Settable<VideoCodecSettings>& codec_settings,
1338 const StreamParams& sp,
1339 const std::vector<webrtc::RtpExtension>& rtp_extensions)
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001340 : call_(call),
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +00001341 external_encoder_factory_(external_encoder_factory),
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001342 stream_(NULL),
pbos@webrtc.orgb648b9d2014-08-26 11:08:06 +00001343 parameters_(webrtc::VideoSendStream::Config(), options, codec_settings),
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +00001344 allocated_encoder_(NULL, webrtc::kVideoCodecUnknown, false),
pbos@webrtc.orgb648b9d2014-08-26 11:08:06 +00001345 capturer_(NULL),
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001346 sending_(false),
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001347 muted_(false) {
1348 parameters_.config.rtp.max_packet_size = kVideoMtu;
1349
1350 sp.GetPrimarySsrcs(&parameters_.config.rtp.ssrcs);
1351 sp.GetFidSsrcs(parameters_.config.rtp.ssrcs,
1352 &parameters_.config.rtp.rtx.ssrcs);
1353 parameters_.config.rtp.c_name = sp.cname;
1354 parameters_.config.rtp.extensions = rtp_extensions;
1355
1356 VideoCodecSettings params;
1357 if (codec_settings.Get(&params)) {
1358 SetCodec(params);
1359 }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001360}
1361
1362WebRtcVideoChannel2::WebRtcVideoSendStream::~WebRtcVideoSendStream() {
1363 DisconnectCapturer();
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001364 if (stream_ != NULL) {
1365 call_->DestroyVideoSendStream(stream_);
1366 }
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +00001367 DestroyVideoEncoder(&allocated_encoder_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001368}
1369
1370static void SetWebRtcFrameToBlack(webrtc::I420VideoFrame* video_frame) {
1371 assert(video_frame != NULL);
1372 memset(video_frame->buffer(webrtc::kYPlane),
1373 16,
1374 video_frame->allocated_size(webrtc::kYPlane));
1375 memset(video_frame->buffer(webrtc::kUPlane),
1376 128,
1377 video_frame->allocated_size(webrtc::kUPlane));
1378 memset(video_frame->buffer(webrtc::kVPlane),
1379 128,
1380 video_frame->allocated_size(webrtc::kVPlane));
1381}
1382
1383static void CreateBlackFrame(webrtc::I420VideoFrame* video_frame,
1384 int width,
1385 int height) {
1386 video_frame->CreateEmptyFrame(
1387 width, height, width, (width + 1) / 2, (width + 1) / 2);
1388 SetWebRtcFrameToBlack(video_frame);
1389}
1390
1391static void ConvertToI420VideoFrame(const VideoFrame& frame,
1392 webrtc::I420VideoFrame* i420_frame) {
1393 i420_frame->CreateFrame(
1394 static_cast<int>(frame.GetYPitch() * frame.GetHeight()),
1395 frame.GetYPlane(),
1396 static_cast<int>(frame.GetUPitch() * ((frame.GetHeight() + 1) / 2)),
1397 frame.GetUPlane(),
1398 static_cast<int>(frame.GetVPitch() * ((frame.GetHeight() + 1) / 2)),
1399 frame.GetVPlane(),
1400 static_cast<int>(frame.GetWidth()),
1401 static_cast<int>(frame.GetHeight()),
1402 static_cast<int>(frame.GetYPitch()),
1403 static_cast<int>(frame.GetUPitch()),
1404 static_cast<int>(frame.GetVPitch()));
1405}
1406
1407void WebRtcVideoChannel2::WebRtcVideoSendStream::InputFrame(
1408 VideoCapturer* capturer,
1409 const VideoFrame* frame) {
1410 LOG(LS_VERBOSE) << "InputFrame: " << frame->GetWidth() << "x"
1411 << frame->GetHeight();
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001412 // Lock before copying, can be called concurrently when swapping input source.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001413 rtc::CritScope frame_cs(&frame_lock_);
pbos@webrtc.orgd60d79a2014-09-24 07:10:57 +00001414 ConvertToI420VideoFrame(*frame, &video_frame_);
1415
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001416 rtc::CritScope cs(&lock_);
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001417 if (stream_ == NULL) {
1418 LOG(LS_WARNING) << "Capturer inputting frames before send codecs are "
1419 "configured, dropping.";
1420 return;
1421 }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001422 if (format_.width == 0) { // Dropping frames.
1423 assert(format_.height == 0);
1424 LOG(LS_VERBOSE) << "VideoFormat 0x0 set, Dropping frame.";
1425 return;
1426 }
pbos@webrtc.orgd60d79a2014-09-24 07:10:57 +00001427 if (muted_) {
1428 // Create a black frame to transmit instead.
1429 CreateBlackFrame(&video_frame_,
1430 static_cast<int>(frame->GetWidth()),
1431 static_cast<int>(frame->GetHeight()));
1432 }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001433 // Reconfigure codec if necessary.
pbos@webrtc.orgc4175b92014-09-03 15:25:49 +00001434 SetDimensions(
1435 video_frame_.width(), video_frame_.height(), capturer->IsScreencast());
1436
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001437 LOG(LS_VERBOSE) << "SwapFrame: " << video_frame_.width() << "x"
1438 << video_frame_.height() << " -> (codec) "
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +00001439 << parameters_.encoder_config.streams.back().width << "x"
1440 << parameters_.encoder_config.streams.back().height;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001441 stream_->Input()->SwapFrame(&video_frame_);
1442}
1443
1444bool WebRtcVideoChannel2::WebRtcVideoSendStream::SetCapturer(
1445 VideoCapturer* capturer) {
1446 if (!DisconnectCapturer() && capturer == NULL) {
1447 return false;
1448 }
1449
1450 {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001451 rtc::CritScope cs(&lock_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001452
pbos@webrtc.org9359cb32014-07-23 15:44:48 +00001453 if (capturer == NULL) {
1454 if (stream_ != NULL) {
1455 LOG(LS_VERBOSE) << "Disabling capturer, sending black frame.";
1456 webrtc::I420VideoFrame black_frame;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001457
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +00001458 // TODO(pbos): Base width/height on last_dimensions_. This will however
1459 // fail the test AddRemoveCapturer which needs to be fixed to permit
1460 // sending black frames in the same size that was previously sent.
pbos@webrtc.org9359cb32014-07-23 15:44:48 +00001461 int width = format_.width;
1462 int height = format_.height;
1463 int half_width = (width + 1) / 2;
1464 black_frame.CreateEmptyFrame(
1465 width, height, width, half_width, half_width);
1466 SetWebRtcFrameToBlack(&black_frame);
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +00001467 SetDimensions(width, height, last_dimensions_.is_screencast);
pbos@webrtc.org9359cb32014-07-23 15:44:48 +00001468 stream_->Input()->SwapFrame(&black_frame);
1469 }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001470
1471 capturer_ = NULL;
1472 return true;
1473 }
1474
1475 capturer_ = capturer;
1476 }
1477 // Lock cannot be held while connecting the capturer to prevent lock-order
1478 // violations.
1479 capturer->SignalVideoFrame.connect(this, &WebRtcVideoSendStream::InputFrame);
1480 return true;
1481}
1482
1483bool WebRtcVideoChannel2::WebRtcVideoSendStream::SetVideoFormat(
1484 const VideoFormat& format) {
1485 if ((format.width == 0 || format.height == 0) &&
1486 format.width != format.height) {
1487 LOG(LS_ERROR) << "Can't set VideoFormat, width or height is zero (but not "
1488 "both, 0x0 drops frames).";
1489 return false;
1490 }
1491
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001492 rtc::CritScope cs(&lock_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001493 if (format.width == 0 && format.height == 0) {
1494 LOG(LS_INFO)
1495 << "0x0 resolution selected. Captured frames will be dropped for ssrc: "
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +00001496 << parameters_.config.rtp.ssrcs[0] << ".";
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001497 } else {
1498 // TODO(pbos): Fix me, this only affects the last stream!
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +00001499 parameters_.encoder_config.streams.back().max_framerate =
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001500 VideoFormat::IntervalToFps(format.interval);
pbos@webrtc.orgc4175b92014-09-03 15:25:49 +00001501 SetDimensions(format.width, format.height, false);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001502 }
1503
1504 format_ = format;
1505 return true;
1506}
1507
pbos@webrtc.orgef8bb8d2014-08-13 21:36:18 +00001508void WebRtcVideoChannel2::WebRtcVideoSendStream::MuteStream(bool mute) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001509 rtc::CritScope cs(&lock_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001510 muted_ = mute;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001511}
1512
1513bool WebRtcVideoChannel2::WebRtcVideoSendStream::DisconnectCapturer() {
pbos@webrtc.org963b9792014-10-07 14:27:27 +00001514 cricket::VideoCapturer* capturer;
1515 {
1516 rtc::CritScope cs(&lock_);
1517 if (capturer_ == NULL) {
1518 return false;
1519 }
1520 capturer = capturer_;
1521 capturer_ = NULL;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001522 }
pbos@webrtc.org963b9792014-10-07 14:27:27 +00001523 capturer->SignalVideoFrame.disconnect(this);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001524 return true;
1525}
1526
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001527void WebRtcVideoChannel2::WebRtcVideoSendStream::SetOptions(
1528 const VideoOptions& options) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001529 rtc::CritScope cs(&lock_);
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001530 VideoCodecSettings codec_settings;
1531 if (parameters_.codec_settings.Get(&codec_settings)) {
1532 SetCodecAndOptions(codec_settings, options);
1533 } else {
1534 parameters_.options = options;
1535 }
1536}
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +00001537
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001538void WebRtcVideoChannel2::WebRtcVideoSendStream::SetCodec(
1539 const VideoCodecSettings& codec_settings) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001540 rtc::CritScope cs(&lock_);
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001541 SetCodecAndOptions(codec_settings, parameters_.options);
1542}
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +00001543
1544webrtc::VideoCodecType CodecTypeFromName(const std::string& name) {
1545 if (CodecNameMatches(name, kVp8CodecName)) {
1546 return webrtc::kVideoCodecVP8;
andresp@webrtc.org188d3b22014-11-07 13:21:04 +00001547 } else if (CodecNameMatches(name, kVp9CodecName)) {
1548 return webrtc::kVideoCodecVP9;
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +00001549 } else if (CodecNameMatches(name, kH264CodecName)) {
1550 return webrtc::kVideoCodecH264;
1551 }
1552 return webrtc::kVideoCodecUnknown;
1553}
1554
1555WebRtcVideoChannel2::WebRtcVideoSendStream::AllocatedEncoder
1556WebRtcVideoChannel2::WebRtcVideoSendStream::CreateVideoEncoder(
1557 const VideoCodec& codec) {
1558 webrtc::VideoCodecType type = CodecTypeFromName(codec.name);
1559
1560 // Do not re-create encoders of the same type.
1561 if (type == allocated_encoder_.type && allocated_encoder_.encoder != NULL) {
1562 return allocated_encoder_;
1563 }
1564
1565 if (external_encoder_factory_ != NULL) {
1566 webrtc::VideoEncoder* encoder =
1567 external_encoder_factory_->CreateVideoEncoder(type);
1568 if (encoder != NULL) {
1569 return AllocatedEncoder(encoder, type, true);
1570 }
1571 }
1572
1573 if (type == webrtc::kVideoCodecVP8) {
1574 return AllocatedEncoder(
1575 webrtc::VideoEncoder::Create(webrtc::VideoEncoder::kVp8), type, false);
andresp@webrtc.org188d3b22014-11-07 13:21:04 +00001576 } else if (type == webrtc::kVideoCodecVP9) {
1577 return AllocatedEncoder(
1578 webrtc::VideoEncoder::Create(webrtc::VideoEncoder::kVp9), type, false);
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +00001579 }
1580
1581 // This shouldn't happen, we should not be trying to create something we don't
1582 // support.
1583 assert(false);
1584 return AllocatedEncoder(NULL, webrtc::kVideoCodecUnknown, false);
1585}
1586
1587void WebRtcVideoChannel2::WebRtcVideoSendStream::DestroyVideoEncoder(
1588 AllocatedEncoder* encoder) {
1589 if (encoder->external) {
1590 external_encoder_factory_->DestroyVideoEncoder(encoder->encoder);
1591 } else {
1592 delete encoder->encoder;
1593 }
1594}
1595
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001596void WebRtcVideoChannel2::WebRtcVideoSendStream::SetCodecAndOptions(
1597 const VideoCodecSettings& codec_settings,
1598 const VideoOptions& options) {
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +00001599 if (last_dimensions_.width == -1) {
1600 last_dimensions_.width = codec_settings.codec.width;
1601 last_dimensions_.height = codec_settings.codec.height;
1602 last_dimensions_.is_screencast = false;
1603 }
1604 parameters_.encoder_config =
1605 CreateVideoEncoderConfig(last_dimensions_, codec_settings.codec);
1606 if (parameters_.encoder_config.streams.empty()) {
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001607 return;
1608 }
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +00001609
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001610 format_ = VideoFormat(codec_settings.codec.width,
1611 codec_settings.codec.height,
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001612 VideoFormat::FpsToInterval(30),
1613 FOURCC_I420);
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +00001614
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +00001615 AllocatedEncoder new_encoder = CreateVideoEncoder(codec_settings.codec);
1616 parameters_.config.encoder_settings.encoder = new_encoder.encoder;
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001617 parameters_.config.encoder_settings.payload_name = codec_settings.codec.name;
1618 parameters_.config.encoder_settings.payload_type = codec_settings.codec.id;
1619 parameters_.config.rtp.fec = codec_settings.fec;
1620
1621 // Set RTX payload type if RTX is enabled.
1622 if (!parameters_.config.rtp.rtx.ssrcs.empty()) {
1623 parameters_.config.rtp.rtx.payload_type = codec_settings.rtx_payload_type;
1624 }
1625
1626 if (IsNackEnabled(codec_settings.codec)) {
1627 parameters_.config.rtp.nack.rtp_history_ms = kNackHistoryMs;
1628 }
1629
pbos@webrtc.org5ff71ab2014-07-23 07:28:56 +00001630 options.suspend_below_min_bitrate.Get(
1631 &parameters_.config.suspend_below_min_bitrate);
1632
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001633 parameters_.codec_settings.Set(codec_settings);
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +00001634 parameters_.options = options;
pbos@webrtc.org543e5892014-07-23 07:01:31 +00001635
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001636 RecreateWebRtcStream();
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +00001637 if (allocated_encoder_.encoder != new_encoder.encoder) {
1638 DestroyVideoEncoder(&allocated_encoder_);
1639 allocated_encoder_ = new_encoder;
1640 }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001641}
1642
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001643void WebRtcVideoChannel2::WebRtcVideoSendStream::SetRtpExtensions(
1644 const std::vector<webrtc::RtpExtension>& rtp_extensions) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001645 rtc::CritScope cs(&lock_);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001646 parameters_.config.rtp.extensions = rtp_extensions;
1647 RecreateWebRtcStream();
1648}
1649
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +00001650webrtc::VideoEncoderConfig
1651WebRtcVideoChannel2::WebRtcVideoSendStream::CreateVideoEncoderConfig(
1652 const Dimensions& dimensions,
1653 const VideoCodec& codec) const {
1654 webrtc::VideoEncoderConfig encoder_config;
1655 if (dimensions.is_screencast) {
pbos@webrtc.orgefc82c22014-10-27 13:58:00 +00001656 int screencast_min_bitrate_kbps;
1657 parameters_.options.screencast_min_bitrate.Get(
1658 &screencast_min_bitrate_kbps);
1659 encoder_config.min_transmit_bitrate_bps =
1660 screencast_min_bitrate_kbps * 1000;
1661 encoder_config.content_type = webrtc::VideoEncoderConfig::kScreenshare;
1662 } else {
1663 encoder_config.min_transmit_bitrate_bps = 0;
1664 encoder_config.content_type = webrtc::VideoEncoderConfig::kRealtimeVideo;
1665 }
1666
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +00001667 // Restrict dimensions according to codec max.
1668 int width = dimensions.width;
1669 int height = dimensions.height;
1670 if (!dimensions.is_screencast) {
1671 if (codec.width < width)
1672 width = codec.width;
1673 if (codec.height < height)
1674 height = codec.height;
1675 }
1676
1677 VideoCodec clamped_codec = codec;
1678 clamped_codec.width = width;
1679 clamped_codec.height = height;
pbos@webrtc.orgcddd17c2014-09-16 16:33:13 +00001680
pbos@webrtc.orgf1c8b902015-01-14 17:29:27 +00001681 encoder_config.streams = CreateVideoStreams(
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +00001682 clamped_codec, parameters_.options, parameters_.config.rtp.ssrcs.size());
pbos@webrtc.org6f48f1b2014-07-22 16:29:54 +00001683
pbos@webrtc.orgb7ed7792014-10-31 13:08:10 +00001684 // Conference mode screencast uses 2 temporal layers split at 100kbit.
1685 if (parameters_.options.conference_mode.GetWithDefaultIfUnset(false) &&
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +00001686 dimensions.is_screencast && encoder_config.streams.size() == 1) {
sprang@webrtc.org46d4d292014-12-23 15:19:35 +00001687 ScreenshareLayerConfig config = ScreenshareLayerConfig::GetDefault();
1688
1689 // For screenshare in conference mode, tl0 and tl1 bitrates are piggybacked
1690 // on the VideoCodec struct as target and max bitrates, respectively.
1691 // See eg. webrtc::VP8EncoderImpl::SetRates().
1692 encoder_config.streams[0].target_bitrate_bps =
1693 config.tl0_bitrate_kbps * 1000;
1694 encoder_config.streams[0].max_bitrate_bps = config.tl1_bitrate_kbps * 1000;
pbos@webrtc.orgb7ed7792014-10-31 13:08:10 +00001695 encoder_config.streams[0].temporal_layer_thresholds_bps.clear();
1696 encoder_config.streams[0].temporal_layer_thresholds_bps.push_back(
sprang@webrtc.org46d4d292014-12-23 15:19:35 +00001697 config.tl0_bitrate_kbps * 1000);
pbos@webrtc.orgb7ed7792014-10-31 13:08:10 +00001698 }
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +00001699 return encoder_config;
1700}
1701
1702void WebRtcVideoChannel2::WebRtcVideoSendStream::SetDimensions(
1703 int width,
1704 int height,
1705 bool is_screencast) {
1706 if (last_dimensions_.width == width && last_dimensions_.height == height &&
1707 last_dimensions_.is_screencast == is_screencast) {
1708 // Configured using the same parameters, do not reconfigure.
1709 return;
1710 }
1711 LOG(LS_INFO) << "SetDimensions: " << width << "x" << height
1712 << (is_screencast ? " (screencast)" : " (not screencast)");
1713
1714 last_dimensions_.width = width;
1715 last_dimensions_.height = height;
1716 last_dimensions_.is_screencast = is_screencast;
1717
1718 assert(!parameters_.encoder_config.streams.empty());
1719
1720 VideoCodecSettings codec_settings;
1721 parameters_.codec_settings.Get(&codec_settings);
1722
1723 webrtc::VideoEncoderConfig encoder_config =
1724 CreateVideoEncoderConfig(last_dimensions_, codec_settings.codec);
1725
1726 encoder_config.encoder_specific_settings =
pbos@webrtc.orgf1c8b902015-01-14 17:29:27 +00001727 ConfigureVideoEncoderSettings(codec_settings.codec, parameters_.options);
pbos@webrtc.orgb7ed7792014-10-31 13:08:10 +00001728
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +00001729 bool stream_reconfigured = stream_->ReconfigureVideoEncoder(encoder_config);
1730
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +00001731 encoder_config.encoder_specific_settings = NULL;
pbos@webrtc.org6f48f1b2014-07-22 16:29:54 +00001732
1733 if (!stream_reconfigured) {
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001734 LOG(LS_WARNING) << "Failed to reconfigure video encoder for dimensions: "
1735 << width << "x" << height;
1736 return;
1737 }
pbos@webrtc.orgcddd17c2014-09-16 16:33:13 +00001738
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +00001739 parameters_.encoder_config = encoder_config;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001740}
1741
1742void WebRtcVideoChannel2::WebRtcVideoSendStream::Start() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001743 rtc::CritScope cs(&lock_);
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001744 assert(stream_ != NULL);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001745 stream_->Start();
1746 sending_ = true;
1747}
1748
1749void WebRtcVideoChannel2::WebRtcVideoSendStream::Stop() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001750 rtc::CritScope cs(&lock_);
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001751 if (stream_ != NULL) {
1752 stream_->Stop();
1753 }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001754 sending_ = false;
1755}
1756
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00001757VideoSenderInfo
1758WebRtcVideoChannel2::WebRtcVideoSendStream::GetVideoSenderInfo() {
1759 VideoSenderInfo info;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001760 rtc::CritScope cs(&lock_);
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00001761 for (size_t i = 0; i < parameters_.config.rtp.ssrcs.size(); ++i) {
1762 info.add_ssrc(parameters_.config.rtp.ssrcs[i]);
1763 }
1764
pbos@webrtc.orgc3d2bd22014-08-12 20:55:10 +00001765 if (stream_ == NULL) {
1766 return info;
1767 }
1768
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00001769 webrtc::VideoSendStream::Stats stats = stream_->GetStats();
1770 info.framerate_input = stats.input_frame_rate;
1771 info.framerate_sent = stats.encode_frame_rate;
1772
pbos@webrtc.org273a4142014-12-01 15:23:21 +00001773 info.send_frame_width = 0;
1774 info.send_frame_height = 0;
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001775 for (std::map<uint32_t, webrtc::SsrcStats>::iterator it =
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00001776 stats.substreams.begin();
1777 it != stats.substreams.end();
1778 ++it) {
1779 // TODO(pbos): Wire up additional stats, such as padding bytes.
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001780 webrtc::SsrcStats stream_stats = it->second;
asapersson@webrtc.orgcfd82df2015-01-22 09:39:59 +00001781 info.bytes_sent += stream_stats.rtp_stats.transmitted.payload_bytes +
1782 stream_stats.rtp_stats.transmitted.header_bytes +
1783 stream_stats.rtp_stats.transmitted.padding_bytes;
1784 info.packets_sent += stream_stats.rtp_stats.transmitted.packets;
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00001785 info.packets_lost += stream_stats.rtcp_stats.cumulative_lost;
pbos@webrtc.org273a4142014-12-01 15:23:21 +00001786 if (stream_stats.sent_width > info.send_frame_width)
1787 info.send_frame_width = stream_stats.sent_width;
1788 if (stream_stats.sent_height > info.send_frame_height)
1789 info.send_frame_height = stream_stats.sent_height;
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00001790 }
1791
1792 if (!stats.substreams.empty()) {
1793 // TODO(pbos): Report fraction lost per SSRC.
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001794 webrtc::SsrcStats first_stream_stats = stats.substreams.begin()->second;
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00001795 info.fraction_lost =
1796 static_cast<float>(first_stream_stats.rtcp_stats.fraction_lost) /
1797 (1 << 8);
1798 }
1799
1800 if (capturer_ != NULL && !capturer_->IsMuted()) {
1801 VideoFormat last_captured_frame_format;
1802 capturer_->GetStats(&info.adapt_frame_drops,
1803 &info.effects_frame_drops,
1804 &info.capturer_frame_time,
1805 &last_captured_frame_format);
1806 info.input_frame_width = last_captured_frame_format.width;
1807 info.input_frame_height = last_captured_frame_format.height;
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00001808 }
1809
1810 // TODO(pbos): Support or remove the following stats.
1811 info.packets_cached = -1;
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00001812
1813 return info;
1814}
1815
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001816void WebRtcVideoChannel2::WebRtcVideoSendStream::FillBandwidthEstimationInfo(
1817 BandwidthEstimationInfo* bwe_info) {
1818 rtc::CritScope cs(&lock_);
1819 if (stream_ == NULL) {
1820 return;
1821 }
1822 webrtc::VideoSendStream::Stats stats = stream_->GetStats();
1823 for (std::map<uint32_t, webrtc::SsrcStats>::iterator it =
1824 stats.substreams.begin();
1825 it != stats.substreams.end();
1826 ++it) {
1827 bwe_info->transmit_bitrate += it->second.total_bitrate_bps;
1828 bwe_info->retransmit_bitrate += it->second.retransmit_bitrate_bps;
1829 }
1830 bwe_info->actual_enc_bitrate = stats.media_bitrate_bps;
1831}
1832
pbos@webrtc.org42684be2014-10-03 11:25:45 +00001833void WebRtcVideoChannel2::WebRtcVideoSendStream::OnCpuResolutionRequest(
1834 CoordinatedVideoAdapter::AdaptRequest adapt_request) {
1835 rtc::CritScope cs(&lock_);
1836 bool adapt_cpu;
1837 parameters_.options.cpu_overuse_detection.Get(&adapt_cpu);
1838 if (!adapt_cpu) {
1839 return;
1840 }
1841 if (capturer_ == NULL || capturer_->video_adapter() == NULL) {
1842 return;
1843 }
1844
1845 capturer_->video_adapter()->OnCpuResolutionRequest(adapt_request);
1846}
1847
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001848void WebRtcVideoChannel2::WebRtcVideoSendStream::RecreateWebRtcStream() {
1849 if (stream_ != NULL) {
1850 call_->DestroyVideoSendStream(stream_);
1851 }
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +00001852
pbos@webrtc.org6f48f1b2014-07-22 16:29:54 +00001853 VideoCodecSettings codec_settings;
1854 parameters_.codec_settings.Get(&codec_settings);
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +00001855 parameters_.encoder_config.encoder_specific_settings =
pbos@webrtc.orgf1c8b902015-01-14 17:29:27 +00001856 ConfigureVideoEncoderSettings(codec_settings.codec, parameters_.options);
pbos@webrtc.org6f48f1b2014-07-22 16:29:54 +00001857
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +00001858 stream_ = call_->CreateVideoSendStream(parameters_.config,
1859 parameters_.encoder_config);
pbos@webrtc.org6f48f1b2014-07-22 16:29:54 +00001860
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +00001861 parameters_.encoder_config.encoder_specific_settings = NULL;
pbos@webrtc.org6f48f1b2014-07-22 16:29:54 +00001862
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001863 if (sending_) {
1864 stream_->Start();
1865 }
1866}
1867
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001868WebRtcVideoChannel2::WebRtcVideoReceiveStream::WebRtcVideoReceiveStream(
1869 webrtc::Call* call,
pbos@webrtc.org776e6f22014-10-29 15:28:39 +00001870 WebRtcVideoDecoderFactory* external_decoder_factory,
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001871 const webrtc::VideoReceiveStream::Config& config,
1872 const std::vector<VideoCodecSettings>& recv_codecs)
1873 : call_(call),
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001874 stream_(NULL),
pbos@webrtc.orgb648b9d2014-08-26 11:08:06 +00001875 config_(config),
pbos@webrtc.org776e6f22014-10-29 15:28:39 +00001876 external_decoder_factory_(external_decoder_factory),
pbos@webrtc.orgb648b9d2014-08-26 11:08:06 +00001877 renderer_(NULL),
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001878 last_width_(-1),
magjed@webrtc.orgfc5ad952015-01-27 09:57:01 +00001879 last_height_(-1),
1880 first_frame_timestamp_(-1),
1881 estimated_remote_start_ntp_time_ms_(0) {
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001882 config_.renderer = this;
1883 // SetRecvCodecs will also reset (start) the VideoReceiveStream.
1884 SetRecvCodecs(recv_codecs);
1885}
1886
1887WebRtcVideoChannel2::WebRtcVideoReceiveStream::~WebRtcVideoReceiveStream() {
1888 call_->DestroyVideoReceiveStream(stream_);
pbos@webrtc.org96a93252014-11-03 14:46:44 +00001889 ClearDecoders(&allocated_decoders_);
1890}
1891
1892WebRtcVideoChannel2::WebRtcVideoReceiveStream::AllocatedDecoder
1893WebRtcVideoChannel2::WebRtcVideoReceiveStream::CreateOrReuseVideoDecoder(
1894 std::vector<AllocatedDecoder>* old_decoders,
1895 const VideoCodec& codec) {
1896 webrtc::VideoCodecType type = CodecTypeFromName(codec.name);
1897
1898 for (size_t i = 0; i < old_decoders->size(); ++i) {
1899 if ((*old_decoders)[i].type == type) {
1900 AllocatedDecoder decoder = (*old_decoders)[i];
1901 (*old_decoders)[i] = old_decoders->back();
1902 old_decoders->pop_back();
1903 return decoder;
1904 }
1905 }
1906
1907 if (external_decoder_factory_ != NULL) {
1908 webrtc::VideoDecoder* decoder =
1909 external_decoder_factory_->CreateVideoDecoder(type);
1910 if (decoder != NULL) {
1911 return AllocatedDecoder(decoder, type, true);
1912 }
1913 }
1914
1915 if (type == webrtc::kVideoCodecVP8) {
1916 return AllocatedDecoder(
1917 webrtc::VideoDecoder::Create(webrtc::VideoDecoder::kVp8), type, false);
1918 }
1919
1920 // This shouldn't happen, we should not be trying to create something we don't
1921 // support.
1922 assert(false);
1923 return AllocatedDecoder(NULL, webrtc::kVideoCodecUnknown, false);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001924}
1925
1926void WebRtcVideoChannel2::WebRtcVideoReceiveStream::SetRecvCodecs(
1927 const std::vector<VideoCodecSettings>& recv_codecs) {
pbos@webrtc.org96a93252014-11-03 14:46:44 +00001928 std::vector<AllocatedDecoder> old_decoders = allocated_decoders_;
1929 allocated_decoders_.clear();
1930 config_.decoders.clear();
1931 for (size_t i = 0; i < recv_codecs.size(); ++i) {
1932 AllocatedDecoder allocated_decoder =
1933 CreateOrReuseVideoDecoder(&old_decoders, recv_codecs[i].codec);
1934 allocated_decoders_.push_back(allocated_decoder);
1935
1936 webrtc::VideoReceiveStream::Decoder decoder;
1937 decoder.decoder = allocated_decoder.decoder;
1938 decoder.payload_type = recv_codecs[i].codec.id;
1939 decoder.payload_name = recv_codecs[i].codec.name;
1940 config_.decoders.push_back(decoder);
1941 }
1942
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001943 // TODO(pbos): Reconfigure RTX based on incoming recv_codecs.
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001944 config_.rtp.fec = recv_codecs.front().fec;
pbos@webrtc.org257e1302014-07-25 19:01:32 +00001945 config_.rtp.nack.rtp_history_ms =
1946 IsNackEnabled(recv_codecs.begin()->codec) ? kNackHistoryMs : 0;
1947 config_.rtp.remb = IsRembEnabled(recv_codecs.begin()->codec);
1948
pbos@webrtc.org96a93252014-11-03 14:46:44 +00001949 ClearDecoders(&old_decoders);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001950 RecreateWebRtcStream();
1951}
1952
1953void WebRtcVideoChannel2::WebRtcVideoReceiveStream::SetRtpExtensions(
1954 const std::vector<webrtc::RtpExtension>& extensions) {
1955 config_.rtp.extensions = extensions;
1956 RecreateWebRtcStream();
1957}
1958
1959void WebRtcVideoChannel2::WebRtcVideoReceiveStream::RecreateWebRtcStream() {
1960 if (stream_ != NULL) {
1961 call_->DestroyVideoReceiveStream(stream_);
1962 }
1963 stream_ = call_->CreateVideoReceiveStream(config_);
1964 stream_->Start();
1965}
1966
pbos@webrtc.org96a93252014-11-03 14:46:44 +00001967void WebRtcVideoChannel2::WebRtcVideoReceiveStream::ClearDecoders(
1968 std::vector<AllocatedDecoder>* allocated_decoders) {
1969 for (size_t i = 0; i < allocated_decoders->size(); ++i) {
1970 if ((*allocated_decoders)[i].external) {
pbos@webrtc.org776e6f22014-10-29 15:28:39 +00001971 external_decoder_factory_->DestroyVideoDecoder(
pbos@webrtc.org96a93252014-11-03 14:46:44 +00001972 (*allocated_decoders)[i].decoder);
pbos@webrtc.org776e6f22014-10-29 15:28:39 +00001973 } else {
pbos@webrtc.org96a93252014-11-03 14:46:44 +00001974 delete (*allocated_decoders)[i].decoder;
pbos@webrtc.org776e6f22014-10-29 15:28:39 +00001975 }
1976 }
pbos@webrtc.org96a93252014-11-03 14:46:44 +00001977 allocated_decoders->clear();
pbos@webrtc.org776e6f22014-10-29 15:28:39 +00001978}
1979
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001980void WebRtcVideoChannel2::WebRtcVideoReceiveStream::RenderFrame(
1981 const webrtc::I420VideoFrame& frame,
1982 int time_to_render_ms) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001983 rtc::CritScope crit(&renderer_lock_);
magjed@webrtc.orgfc5ad952015-01-27 09:57:01 +00001984
1985 if (first_frame_timestamp_ < 0)
1986 first_frame_timestamp_ = frame.timestamp();
1987 int64_t rtp_time_elapsed_since_first_frame =
1988 (timestamp_wraparound_handler_.Unwrap(frame.timestamp()) -
1989 first_frame_timestamp_);
1990 int64_t elapsed_time_ms = rtp_time_elapsed_since_first_frame /
1991 (cricket::kVideoCodecClockrate / 1000);
1992 if (frame.ntp_time_ms() > 0)
1993 estimated_remote_start_ntp_time_ms_ = frame.ntp_time_ms() - elapsed_time_ms;
1994
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001995 if (renderer_ == NULL) {
1996 LOG(LS_WARNING) << "VideoReceiveStream not connected to a VideoRenderer.";
1997 return;
1998 }
1999
2000 if (frame.width() != last_width_ || frame.height() != last_height_) {
2001 SetSize(frame.width(), frame.height());
2002 }
2003
2004 LOG(LS_VERBOSE) << "RenderFrame: (" << frame.width() << "x" << frame.height()
2005 << ")";
2006
magjed@webrtc.orgfc5ad952015-01-27 09:57:01 +00002007 const WebRtcVideoRenderFrame render_frame(&frame, elapsed_time_ms);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00002008 renderer_->RenderFrame(&render_frame);
2009}
2010
2011void WebRtcVideoChannel2::WebRtcVideoReceiveStream::SetRenderer(
2012 cricket::VideoRenderer* renderer) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002013 rtc::CritScope crit(&renderer_lock_);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00002014 renderer_ = renderer;
2015 if (renderer_ != NULL && last_width_ != -1) {
2016 SetSize(last_width_, last_height_);
2017 }
2018}
2019
2020VideoRenderer* WebRtcVideoChannel2::WebRtcVideoReceiveStream::GetRenderer() {
2021 // TODO(pbos): Remove GetRenderer and all uses of it, it's thread-unsafe by
2022 // design.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002023 rtc::CritScope crit(&renderer_lock_);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00002024 return renderer_;
2025}
2026
2027void WebRtcVideoChannel2::WebRtcVideoReceiveStream::SetSize(int width,
2028 int height) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002029 rtc::CritScope crit(&renderer_lock_);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00002030 if (!renderer_->SetSize(width, height, 0)) {
2031 LOG(LS_ERROR) << "Could not set renderer size.";
2032 }
2033 last_width_ = width;
2034 last_height_ = height;
2035}
2036
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00002037VideoReceiverInfo
2038WebRtcVideoChannel2::WebRtcVideoReceiveStream::GetVideoReceiverInfo() {
2039 VideoReceiverInfo info;
2040 info.add_ssrc(config_.rtp.remote_ssrc);
2041 webrtc::VideoReceiveStream::Stats stats = stream_->GetStats();
asapersson@webrtc.orgcfd82df2015-01-22 09:39:59 +00002042 info.bytes_rcvd = stats.rtp_stats.transmitted.payload_bytes +
2043 stats.rtp_stats.transmitted.header_bytes +
2044 stats.rtp_stats.transmitted.padding_bytes;
2045 info.packets_rcvd = stats.rtp_stats.transmitted.packets;
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00002046
2047 info.framerate_rcvd = stats.network_frame_rate;
2048 info.framerate_decoded = stats.decode_frame_rate;
2049 info.framerate_output = stats.render_frame_rate;
2050
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002051 rtc::CritScope frame_cs(&renderer_lock_);
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00002052 info.frame_width = last_width_;
2053 info.frame_height = last_height_;
magjed@webrtc.orgfc5ad952015-01-27 09:57:01 +00002054 info.capture_start_ntp_time_ms = estimated_remote_start_ntp_time_ms_;
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00002055
2056 // TODO(pbos): Support or remove the following stats.
2057 info.packets_concealed = -1;
2058
2059 return info;
2060}
2061
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00002062WebRtcVideoChannel2::VideoCodecSettings::VideoCodecSettings()
2063 : rtx_payload_type(-1) {}
2064
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +00002065bool WebRtcVideoChannel2::VideoCodecSettings::operator==(
2066 const WebRtcVideoChannel2::VideoCodecSettings& other) const {
2067 return codec == other.codec &&
2068 fec.ulpfec_payload_type == other.fec.ulpfec_payload_type &&
2069 fec.red_payload_type == other.fec.red_payload_type &&
2070 rtx_payload_type == other.rtx_payload_type;
2071}
2072
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00002073std::vector<WebRtcVideoChannel2::VideoCodecSettings>
2074WebRtcVideoChannel2::MapCodecs(const std::vector<VideoCodec>& codecs) {
2075 assert(!codecs.empty());
2076
2077 std::vector<VideoCodecSettings> video_codecs;
2078 std::map<int, bool> payload_used;
pbos@webrtc.orge322a172014-06-13 11:47:28 +00002079 std::map<int, VideoCodec::CodecType> payload_codec_type;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00002080 std::map<int, int> rtx_mapping; // video payload type -> rtx payload type.
2081
2082 webrtc::FecConfig fec_settings;
2083
2084 for (size_t i = 0; i < codecs.size(); ++i) {
2085 const VideoCodec& in_codec = codecs[i];
2086 int payload_type = in_codec.id;
2087
2088 if (payload_used[payload_type]) {
2089 LOG(LS_ERROR) << "Payload type already registered: "
2090 << in_codec.ToString();
2091 return std::vector<VideoCodecSettings>();
2092 }
2093 payload_used[payload_type] = true;
pbos@webrtc.orge322a172014-06-13 11:47:28 +00002094 payload_codec_type[payload_type] = in_codec.GetCodecType();
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00002095
2096 switch (in_codec.GetCodecType()) {
2097 case VideoCodec::CODEC_RED: {
2098 // RED payload type, should not have duplicates.
2099 assert(fec_settings.red_payload_type == -1);
2100 fec_settings.red_payload_type = in_codec.id;
2101 continue;
2102 }
2103
2104 case VideoCodec::CODEC_ULPFEC: {
2105 // ULPFEC payload type, should not have duplicates.
2106 assert(fec_settings.ulpfec_payload_type == -1);
2107 fec_settings.ulpfec_payload_type = in_codec.id;
2108 continue;
2109 }
2110
2111 case VideoCodec::CODEC_RTX: {
2112 int associated_payload_type;
2113 if (!in_codec.GetParam(kCodecParamAssociatedPayloadType,
2114 &associated_payload_type)) {
2115 LOG(LS_ERROR) << "RTX codec without associated payload type: "
2116 << in_codec.ToString();
2117 return std::vector<VideoCodecSettings>();
2118 }
2119 rtx_mapping[associated_payload_type] = in_codec.id;
2120 continue;
2121 }
2122
2123 case VideoCodec::CODEC_VIDEO:
2124 break;
2125 }
2126
2127 video_codecs.push_back(VideoCodecSettings());
2128 video_codecs.back().codec = in_codec;
2129 }
2130
2131 // One of these codecs should have been a video codec. Only having FEC
2132 // parameters into this code is a logic error.
2133 assert(!video_codecs.empty());
2134
pbos@webrtc.orge322a172014-06-13 11:47:28 +00002135 for (std::map<int, int>::const_iterator it = rtx_mapping.begin();
2136 it != rtx_mapping.end();
2137 ++it) {
2138 if (!payload_used[it->first]) {
2139 LOG(LS_ERROR) << "RTX mapped to payload not in codec list.";
2140 return std::vector<VideoCodecSettings>();
2141 }
andrew@webrtc.org8f27fcc2015-01-09 20:22:46 +00002142 if (payload_codec_type[it->first] != VideoCodec::CODEC_VIDEO) {
2143 LOG(LS_ERROR) << "RTX not mapped to regular video codec.";
pbos@webrtc.orge322a172014-06-13 11:47:28 +00002144 return std::vector<VideoCodecSettings>();
2145 }
2146 }
2147
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00002148 // TODO(pbos): Write tests that figure out that I have not verified that RTX
2149 // codecs aren't mapped to bogus payloads.
2150 for (size_t i = 0; i < video_codecs.size(); ++i) {
2151 video_codecs[i].fec = fec_settings;
andrew@webrtc.org8f27fcc2015-01-09 20:22:46 +00002152 if (rtx_mapping[video_codecs[i].codec.id] != 0) {
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00002153 video_codecs[i].rtx_payload_type = rtx_mapping[video_codecs[i].codec.id];
2154 }
2155 }
2156
2157 return video_codecs;
2158}
2159
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00002160} // namespace cricket
2161
2162#endif // HAVE_WEBRTC_VIDEO