blob: 4410e5997ac42b3befa63a2fde4f04ed8aa4dc96 [file] [log] [blame]
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001/*
2 * libjingle
3 * Copyright 2014 Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#ifdef HAVE_WEBRTC_VIDEO
29#include "talk/media/webrtc/webrtcvideoengine2.h"
30
pbos@webrtc.orgc37e72e2015-01-05 18:51:13 +000031#include <algorithm>
pbos@webrtc.org3c107582014-07-20 15:27:35 +000032#include <set>
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +000033#include <string>
34
35#include "libyuv/convert_from.h"
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +000036#include "talk/media/base/videocapturer.h"
37#include "talk/media/base/videorenderer.h"
buildbot@webrtc.orgdf9bbbe2014-06-19 19:54:33 +000038#include "talk/media/webrtc/constants.h"
buildbot@webrtc.orga8530772014-12-10 09:01:18 +000039#include "talk/media/webrtc/simulcast.h"
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +000040#include "talk/media/webrtc/webrtcvideocapturer.h"
andresp@webrtc.org82775b12014-11-07 09:37:54 +000041#include "talk/media/webrtc/webrtcvideoengine.h"
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +000042#include "talk/media/webrtc/webrtcvideoframe.h"
43#include "talk/media/webrtc/webrtcvoiceengine.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000044#include "webrtc/base/buffer.h"
45#include "webrtc/base/logging.h"
46#include "webrtc/base/stringutils.h"
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +000047#include "webrtc/call.h"
pbos@webrtc.org50fe3592015-01-29 12:33:07 +000048#include "webrtc/system_wrappers/interface/trace_event.h"
pbos@webrtc.org776e6f22014-10-29 15:28:39 +000049#include "webrtc/video_decoder.h"
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000050#include "webrtc/video_encoder.h"
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +000051
52#define UNIMPLEMENTED \
53 LOG(LS_ERROR) << "Call to unimplemented function " << __FUNCTION__; \
54 ASSERT(false)
55
56namespace cricket {
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +000057namespace {
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +000058static std::string CodecVectorToString(const std::vector<VideoCodec>& codecs) {
59 std::stringstream out;
60 out << '{';
61 for (size_t i = 0; i < codecs.size(); ++i) {
62 out << codecs[i].ToString();
63 if (i != codecs.size() - 1) {
64 out << ", ";
65 }
66 }
67 out << '}';
68 return out.str();
69}
70
71static bool ValidateCodecFormats(const std::vector<VideoCodec>& codecs) {
72 bool has_video = false;
73 for (size_t i = 0; i < codecs.size(); ++i) {
74 if (!codecs[i].ValidateCodecFormat()) {
75 return false;
76 }
77 if (codecs[i].GetCodecType() == VideoCodec::CODEC_VIDEO) {
78 has_video = true;
79 }
80 }
81 if (!has_video) {
82 LOG(LS_ERROR) << "Setting codecs without a video codec is invalid: "
83 << CodecVectorToString(codecs);
84 return false;
85 }
86 return true;
87}
88
89static std::string RtpExtensionsToString(
90 const std::vector<RtpHeaderExtension>& extensions) {
91 std::stringstream out;
92 out << '{';
93 for (size_t i = 0; i < extensions.size(); ++i) {
94 out << "{" << extensions[i].uri << ": " << extensions[i].id << "}";
95 if (i != extensions.size() - 1) {
96 out << ", ";
97 }
98 }
99 out << '}';
100 return out.str();
101}
102
andresp@webrtc.org82775b12014-11-07 09:37:54 +0000103// Merges two fec configs and logs an error if a conflict arises
104// such that merging in diferent order would trigger a diferent output.
105static void MergeFecConfig(const webrtc::FecConfig& other,
106 webrtc::FecConfig* output) {
107 if (other.ulpfec_payload_type != -1) {
108 if (output->ulpfec_payload_type != -1 &&
109 output->ulpfec_payload_type != other.ulpfec_payload_type) {
110 LOG(LS_WARNING) << "Conflict merging ulpfec_payload_type configs: "
111 << output->ulpfec_payload_type << " and "
112 << other.ulpfec_payload_type;
113 }
114 output->ulpfec_payload_type = other.ulpfec_payload_type;
115 }
116 if (other.red_payload_type != -1) {
117 if (output->red_payload_type != -1 &&
118 output->red_payload_type != other.red_payload_type) {
119 LOG(LS_WARNING) << "Conflict merging red_payload_type configs: "
120 << output->red_payload_type << " and "
121 << other.red_payload_type;
122 }
123 output->red_payload_type = other.red_payload_type;
124 }
125}
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000126} // namespace
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000127
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000128// This constant is really an on/off, lower-level configurable NACK history
129// duration hasn't been implemented.
130static const int kNackHistoryMs = 1000;
131
buildbot@webrtc.org933d88a2014-09-18 20:23:05 +0000132static const int kDefaultQpMax = 56;
133
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000134static const int kDefaultRtcpReceiverReportSsrc = 1;
135
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000136const char kH264CodecName[] = "H264";
137
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000138static bool FindFirstMatchingCodec(const std::vector<VideoCodec>& codecs,
139 const VideoCodec& requested_codec,
140 VideoCodec* matching_codec) {
141 for (size_t i = 0; i < codecs.size(); ++i) {
142 if (requested_codec.Matches(codecs[i])) {
143 *matching_codec = codecs[i];
144 return true;
145 }
146 }
147 return false;
148}
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000149
pbos@webrtc.org3c107582014-07-20 15:27:35 +0000150static bool ValidateRtpHeaderExtensionIds(
151 const std::vector<RtpHeaderExtension>& extensions) {
152 std::set<int> extensions_used;
153 for (size_t i = 0; i < extensions.size(); ++i) {
154 if (extensions[i].id < 0 || extensions[i].id >= 15 ||
155 !extensions_used.insert(extensions[i].id).second) {
156 LOG(LS_ERROR) << "RTP extensions are with incorrect or duplicate ids.";
157 return false;
158 }
159 }
160 return true;
161}
162
pbos@webrtc.orgc37e72e2015-01-05 18:51:13 +0000163static bool CompareRtpHeaderExtensionIds(
164 const webrtc::RtpExtension& extension1,
165 const webrtc::RtpExtension& extension2) {
166 // Sorting on ID is sufficient, more than one extension per ID is unsupported.
167 return extension1.id > extension2.id;
168}
169
pbos@webrtc.org3c107582014-07-20 15:27:35 +0000170static std::vector<webrtc::RtpExtension> FilterRtpExtensions(
171 const std::vector<RtpHeaderExtension>& extensions) {
172 std::vector<webrtc::RtpExtension> webrtc_extensions;
173 for (size_t i = 0; i < extensions.size(); ++i) {
174 // Unsupported extensions will be ignored.
175 if (webrtc::RtpExtension::IsSupported(extensions[i].uri)) {
176 webrtc_extensions.push_back(webrtc::RtpExtension(
177 extensions[i].uri, extensions[i].id));
178 } else {
179 LOG(LS_WARNING) << "Unsupported RTP extension: " << extensions[i].uri;
180 }
181 }
pbos@webrtc.orgc37e72e2015-01-05 18:51:13 +0000182
183 // Sort filtered headers to make sure that they can later be compared
184 // regardless of in which order they were entered.
185 std::sort(webrtc_extensions.begin(), webrtc_extensions.end(),
186 CompareRtpHeaderExtensionIds);
pbos@webrtc.org3c107582014-07-20 15:27:35 +0000187 return webrtc_extensions;
188}
189
pbos@webrtc.orgc37e72e2015-01-05 18:51:13 +0000190static bool RtpExtensionsHaveChanged(
191 const std::vector<webrtc::RtpExtension>& before,
192 const std::vector<webrtc::RtpExtension>& after) {
193 if (before.size() != after.size())
194 return true;
195 for (size_t i = 0; i < before.size(); ++i) {
196 if (before[i].id != after[i].id)
197 return true;
198 if (before[i].name != after[i].name)
199 return true;
200 }
201 return false;
202}
203
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000204std::vector<webrtc::VideoStream>
pbos@webrtc.orgf1c8b902015-01-14 17:29:27 +0000205WebRtcVideoChannel2::WebRtcVideoSendStream::CreateSimulcastVideoStreams(
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000206 const VideoCodec& codec,
207 const VideoOptions& options,
208 size_t num_streams) {
209 // Use default factory for non-simulcast.
210 int max_qp = kDefaultQpMax;
211 codec.GetParam(kCodecParamMaxQuantization, &max_qp);
212
213 int min_bitrate_kbps;
214 if (!codec.GetParam(kCodecParamMinBitrate, &min_bitrate_kbps) ||
215 min_bitrate_kbps < kMinVideoBitrate) {
216 min_bitrate_kbps = kMinVideoBitrate;
217 }
218
219 int max_bitrate_kbps;
220 if (!codec.GetParam(kCodecParamMaxBitrate, &max_bitrate_kbps)) {
221 max_bitrate_kbps = 0;
222 }
223
224 return GetSimulcastConfig(
225 num_streams,
226 GetSimulcastBitrateMode(options),
227 codec.width,
228 codec.height,
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000229 max_bitrate_kbps * 1000,
230 max_qp,
231 codec.framerate != 0 ? codec.framerate : kDefaultVideoMaxFramerate);
232}
233
pbos@webrtc.orgf1c8b902015-01-14 17:29:27 +0000234std::vector<webrtc::VideoStream>
235WebRtcVideoChannel2::WebRtcVideoSendStream::CreateVideoStreams(
buildbot@webrtc.orgd41eaeb2014-06-12 07:13:26 +0000236 const VideoCodec& codec,
237 const VideoOptions& options,
238 size_t num_streams) {
buildbot@webrtc.orga8530772014-12-10 09:01:18 +0000239 if (num_streams != 1)
240 return CreateSimulcastVideoStreams(codec, options, num_streams);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000241
buildbot@webrtc.orgd41eaeb2014-06-12 07:13:26 +0000242 webrtc::VideoStream stream;
243 stream.width = codec.width;
244 stream.height = codec.height;
245 stream.max_framerate =
andresp@webrtc.org82775b12014-11-07 09:37:54 +0000246 codec.framerate != 0 ? codec.framerate : kDefaultVideoMaxFramerate;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000247
pbos@webrtc.org00873182014-11-25 14:03:34 +0000248 stream.min_bitrate_bps = kMinVideoBitrate * 1000;
pbos@webrtc.orga5f6fb52015-03-23 22:29:39 +0000249 int max_bitrate_kbps;
250 if (!codec.GetParam(kCodecParamMaxBitrate, &max_bitrate_kbps) ||
251 max_bitrate_kbps < kMaxVideoBitrate) {
252 max_bitrate_kbps = kMaxVideoBitrate;
253 }
254
255 stream.target_bitrate_bps = stream.max_bitrate_bps = max_bitrate_kbps * 1000;
buildbot@webrtc.orgd41eaeb2014-06-12 07:13:26 +0000256
buildbot@webrtc.org933d88a2014-09-18 20:23:05 +0000257 int max_qp = kDefaultQpMax;
buildbot@webrtc.orgd41eaeb2014-06-12 07:13:26 +0000258 codec.GetParam(kCodecParamMaxQuantization, &max_qp);
259 stream.max_qp = max_qp;
260 std::vector<webrtc::VideoStream> streams;
261 streams.push_back(stream);
262 return streams;
263}
264
pbos@webrtc.orgf1c8b902015-01-14 17:29:27 +0000265void* WebRtcVideoChannel2::WebRtcVideoSendStream::ConfigureVideoEncoderSettings(
pbos@webrtc.org6f48f1b2014-07-22 16:29:54 +0000266 const VideoCodec& codec,
267 const VideoOptions& options) {
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000268 if (CodecNameMatches(codec.name, kVp8CodecName)) {
pbos@webrtc.orgf1c8b902015-01-14 17:29:27 +0000269 encoder_settings_.vp8 = webrtc::VideoEncoder::GetDefaultVp8Settings();
270 options.video_noise_reduction.Get(&encoder_settings_.vp8.denoisingOn);
271 return &encoder_settings_.vp8;
pbos@webrtc.org6f48f1b2014-07-22 16:29:54 +0000272 }
andresp@webrtc.org188d3b22014-11-07 13:21:04 +0000273 if (CodecNameMatches(codec.name, kVp9CodecName)) {
pbos@webrtc.orgf1c8b902015-01-14 17:29:27 +0000274 encoder_settings_.vp9 = webrtc::VideoEncoder::GetDefaultVp9Settings();
275 options.video_noise_reduction.Get(&encoder_settings_.vp9.denoisingOn);
276 return &encoder_settings_.vp9;
andresp@webrtc.org188d3b22014-11-07 13:21:04 +0000277 }
pbos@webrtc.org6f48f1b2014-07-22 16:29:54 +0000278 return NULL;
279}
280
pbos@webrtc.orgafb554f42014-08-12 23:17:13 +0000281DefaultUnsignalledSsrcHandler::DefaultUnsignalledSsrcHandler()
282 : default_recv_ssrc_(0), default_renderer_(NULL) {}
283
284UnsignalledSsrcHandler::Action DefaultUnsignalledSsrcHandler::OnUnsignalledSsrc(
pbos@webrtc.orga2a6fe62015-03-06 15:35:19 +0000285 WebRtcVideoChannel2* channel,
pbos@webrtc.orgafb554f42014-08-12 23:17:13 +0000286 uint32_t ssrc) {
287 if (default_recv_ssrc_ != 0) { // Already one default stream.
288 LOG(LS_WARNING) << "Unknown SSRC, but default receive stream already set.";
289 return kDropPacket;
290 }
291
292 StreamParams sp;
293 sp.ssrcs.push_back(ssrc);
294 LOG(LS_INFO) << "Creating default receive stream for SSRC=" << ssrc << ".";
pbos@webrtc.orga2a6fe62015-03-06 15:35:19 +0000295 if (!channel->AddRecvStream(sp, true)) {
pbos@webrtc.orgafb554f42014-08-12 23:17:13 +0000296 LOG(LS_WARNING) << "Could not create default receive stream.";
297 }
298
299 channel->SetRenderer(ssrc, default_renderer_);
300 default_recv_ssrc_ = ssrc;
301 return kDeliverPacket;
302}
303
pbos@webrtc.org42684be2014-10-03 11:25:45 +0000304WebRtcCallFactory::~WebRtcCallFactory() {
305}
306webrtc::Call* WebRtcCallFactory::CreateCall(
307 const webrtc::Call::Config& config) {
308 return webrtc::Call::Create(config);
309}
310
pbos@webrtc.orgafb554f42014-08-12 23:17:13 +0000311VideoRenderer* DefaultUnsignalledSsrcHandler::GetDefaultRenderer() const {
312 return default_renderer_;
313}
314
315void DefaultUnsignalledSsrcHandler::SetDefaultRenderer(
316 VideoMediaChannel* channel,
317 VideoRenderer* renderer) {
318 default_renderer_ = renderer;
319 if (default_recv_ssrc_ != 0) {
320 channel->SetRenderer(default_recv_ssrc_, default_renderer_);
321 }
322}
323
pbos@webrtc.orgf1f0d9a2015-03-02 13:30:15 +0000324WebRtcVideoEngine2::WebRtcVideoEngine2(WebRtcVoiceEngine* voice_engine)
pbos@webrtc.orgb648b9d2014-08-26 11:08:06 +0000325 : worker_thread_(NULL),
pbos@webrtc.orgf1f0d9a2015-03-02 13:30:15 +0000326 voice_engine_(voice_engine),
andresp@webrtc.org82775b12014-11-07 09:37:54 +0000327 default_codec_format_(kDefaultVideoMaxWidth,
328 kDefaultVideoMaxHeight,
329 FPS_TO_INTERVAL(kDefaultVideoMaxFramerate),
buildbot@webrtc.org992febb2014-09-05 16:39:08 +0000330 FOURCC_ANY),
pbos@webrtc.orgb648b9d2014-08-26 11:08:06 +0000331 initialized_(false),
pbos@webrtc.org42684be2014-10-03 11:25:45 +0000332 call_factory_(&default_call_factory_),
pbos@webrtc.org0a2087a2014-09-23 09:40:22 +0000333 external_decoder_factory_(NULL),
334 external_encoder_factory_(NULL) {
pbos@webrtc.orgb648b9d2014-08-26 11:08:06 +0000335 LOG(LS_INFO) << "WebRtcVideoEngine2::WebRtcVideoEngine2()";
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000336 video_codecs_ = GetSupportedCodecs();
pbos@webrtc.org587ef602014-06-16 17:32:02 +0000337 rtp_header_extensions_.push_back(
338 RtpHeaderExtension(kRtpTimestampOffsetHeaderExtension,
339 kRtpTimestampOffsetHeaderExtensionDefaultId));
340 rtp_header_extensions_.push_back(
341 RtpHeaderExtension(kRtpAbsoluteSenderTimeHeaderExtension,
342 kRtpAbsoluteSenderTimeHeaderExtensionDefaultId));
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000343}
344
345WebRtcVideoEngine2::~WebRtcVideoEngine2() {
346 LOG(LS_INFO) << "WebRtcVideoEngine2::~WebRtcVideoEngine2";
347
348 if (initialized_) {
349 Terminate();
350 }
351}
352
pbos@webrtc.org42684be2014-10-03 11:25:45 +0000353void WebRtcVideoEngine2::SetCallFactory(WebRtcCallFactory* call_factory) {
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000354 assert(!initialized_);
pbos@webrtc.org42684be2014-10-03 11:25:45 +0000355 call_factory_ = call_factory;
356}
357
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000358bool WebRtcVideoEngine2::Init(rtc::Thread* worker_thread) {
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000359 LOG(LS_INFO) << "WebRtcVideoEngine2::Init";
360 worker_thread_ = worker_thread;
361 ASSERT(worker_thread_ != NULL);
362
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000363 initialized_ = true;
364 return true;
365}
366
367void WebRtcVideoEngine2::Terminate() {
368 LOG(LS_INFO) << "WebRtcVideoEngine2::Terminate";
369
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000370 initialized_ = false;
371}
372
373int WebRtcVideoEngine2::GetCapabilities() { return VIDEO_RECV | VIDEO_SEND; }
374
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000375bool WebRtcVideoEngine2::SetDefaultEncoderConfig(
376 const VideoEncoderConfig& config) {
pbos@webrtc.org8fdeee62014-07-20 14:40:23 +0000377 const VideoCodec& codec = config.max_codec;
pbos@webrtc.org957e8022014-11-10 12:36:11 +0000378 bool supports_codec = false;
379 for (size_t i = 0; i < video_codecs_.size(); ++i) {
380 if (CodecNameMatches(video_codecs_[i].name, codec.name)) {
pbos@webrtc.org2a72c652015-02-26 16:01:24 +0000381 video_codecs_[i].width = codec.width;
382 video_codecs_[i].height = codec.height;
383 video_codecs_[i].framerate = codec.framerate;
pbos@webrtc.org957e8022014-11-10 12:36:11 +0000384 supports_codec = true;
385 break;
386 }
387 }
388
389 if (!supports_codec) {
390 LOG(LS_ERROR) << "SetDefaultEncoderConfig, codec not supported: "
pbos@webrtc.org8fdeee62014-07-20 14:40:23 +0000391 << codec.ToString();
392 return false;
393 }
394
buildbot@webrtc.org992febb2014-09-05 16:39:08 +0000395 default_codec_format_ =
396 VideoFormat(codec.width,
397 codec.height,
398 VideoFormat::FpsToInterval(codec.framerate),
399 FOURCC_ANY);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000400 return true;
401}
402
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000403WebRtcVideoChannel2* WebRtcVideoEngine2::CreateChannel(
buildbot@webrtc.org1ecbe452014-10-14 20:29:28 +0000404 const VideoOptions& options,
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000405 VoiceMediaChannel* voice_channel) {
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000406 assert(initialized_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000407 LOG(LS_INFO) << "CreateChannel: "
408 << (voice_channel != NULL ? "With" : "Without")
pbos@webrtc.orgfa553ef2014-10-20 11:07:07 +0000409 << " voice channel. Options: " << options.ToString();
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000410 WebRtcVideoChannel2* channel =
411 new WebRtcVideoChannel2(call_factory_,
pbos@webrtc.org3bf3d232014-10-31 12:59:34 +0000412 voice_engine_,
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000413 voice_channel,
pbos@webrtc.orgfa553ef2014-10-20 11:07:07 +0000414 options,
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000415 external_encoder_factory_,
pbos@webrtc.orgf1c8b902015-01-14 17:29:27 +0000416 external_decoder_factory_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000417 if (!channel->Init()) {
418 delete channel;
419 return NULL;
420 }
pbos@webrtc.orge322a172014-06-13 11:47:28 +0000421 channel->SetRecvCodecs(video_codecs_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000422 return channel;
423}
424
425const std::vector<VideoCodec>& WebRtcVideoEngine2::codecs() const {
426 return video_codecs_;
427}
428
429const std::vector<RtpHeaderExtension>&
430WebRtcVideoEngine2::rtp_header_extensions() const {
431 return rtp_header_extensions_;
432}
433
434void WebRtcVideoEngine2::SetLogging(int min_sev, const char* filter) {
435 // TODO(pbos): Set up logging.
436 LOG(LS_VERBOSE) << "SetLogging: " << min_sev << '"' << filter << '"';
437 // if min_sev == -1, we keep the current log level.
438 if (min_sev < 0) {
439 assert(min_sev == -1);
440 return;
441 }
442}
443
pbos@webrtc.org0a2087a2014-09-23 09:40:22 +0000444void WebRtcVideoEngine2::SetExternalDecoderFactory(
445 WebRtcVideoDecoderFactory* decoder_factory) {
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000446 assert(!initialized_);
pbos@webrtc.org0a2087a2014-09-23 09:40:22 +0000447 external_decoder_factory_ = decoder_factory;
448}
449
450void WebRtcVideoEngine2::SetExternalEncoderFactory(
451 WebRtcVideoEncoderFactory* encoder_factory) {
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000452 assert(!initialized_);
pbos@webrtc.orgf18fba22015-01-14 16:26:23 +0000453 if (external_encoder_factory_ == encoder_factory)
454 return;
455
456 // No matter what happens we shouldn't hold on to a stale
457 // WebRtcSimulcastEncoderFactory.
458 simulcast_encoder_factory_.reset();
459
460 if (encoder_factory &&
461 WebRtcSimulcastEncoderFactory::UseSimulcastEncoderFactory(
462 encoder_factory->codecs())) {
463 simulcast_encoder_factory_.reset(
464 new WebRtcSimulcastEncoderFactory(encoder_factory));
465 encoder_factory = simulcast_encoder_factory_.get();
466 }
pbos@webrtc.org0a2087a2014-09-23 09:40:22 +0000467 external_encoder_factory_ = encoder_factory;
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000468
469 video_codecs_ = GetSupportedCodecs();
pbos@webrtc.org0a2087a2014-09-23 09:40:22 +0000470}
471
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000472bool WebRtcVideoEngine2::EnableTimedRender() {
473 // TODO(pbos): Figure out whether this can be removed.
474 return true;
475}
476
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000477// Checks to see whether we comprehend and could receive a particular codec
478bool WebRtcVideoEngine2::FindCodec(const VideoCodec& in) {
479 // TODO(pbos): Probe encoder factory to figure out that the codec is supported
480 // if supported by the encoder factory. Add a corresponding test that fails
481 // with this code (that doesn't ask the factory).
pbos@webrtc.org8fdeee62014-07-20 14:40:23 +0000482 for (size_t j = 0; j < video_codecs_.size(); ++j) {
483 VideoCodec codec(video_codecs_[j].id, video_codecs_[j].name, 0, 0, 0, 0);
484 if (codec.Matches(in)) {
485 return true;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000486 }
487 }
488 return false;
489}
490
491// Tells whether the |requested| codec can be transmitted or not. If it can be
492// transmitted |out| is set with the best settings supported. Aspect ratio will
493// be set as close to |current|'s as possible. If not set |requested|'s
494// dimensions will be used for aspect ratio matching.
495bool WebRtcVideoEngine2::CanSendCodec(const VideoCodec& requested,
496 const VideoCodec& current,
497 VideoCodec* out) {
498 assert(out != NULL);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000499
500 if (requested.width != requested.height &&
501 (requested.height == 0 || requested.width == 0)) {
502 // 0xn and nx0 are invalid resolutions.
503 return false;
504 }
505
506 VideoCodec matching_codec;
507 if (!FindFirstMatchingCodec(video_codecs_, requested, &matching_codec)) {
508 // Codec not supported.
509 return false;
510 }
511
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000512 out->id = requested.id;
513 out->name = requested.name;
514 out->preference = requested.preference;
515 out->params = requested.params;
andresp@webrtc.orgff689be2015-02-12 11:54:26 +0000516 out->framerate = std::min(requested.framerate, matching_codec.framerate);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000517 out->params = requested.params;
518 out->feedback_params = requested.feedback_params;
pbos@webrtc.org8fdeee62014-07-20 14:40:23 +0000519 out->width = requested.width;
520 out->height = requested.height;
521 if (requested.width == 0 && requested.height == 0) {
522 return true;
523 }
524
525 while (out->width > matching_codec.width) {
526 out->width /= 2;
527 out->height /= 2;
528 }
529
530 return out->width > 0 && out->height > 0;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000531}
532
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000533// Ignore spammy trace messages, mostly from the stats API when we haven't
534// gotten RTCP info yet from the remote side.
535bool WebRtcVideoEngine2::ShouldIgnoreTrace(const std::string& trace) {
536 static const char* const kTracesToIgnore[] = {NULL};
537 for (const char* const* p = kTracesToIgnore; *p; ++p) {
538 if (trace.find(*p) == 0) {
539 return true;
540 }
541 }
542 return false;
543}
544
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000545std::vector<VideoCodec> WebRtcVideoEngine2::GetSupportedCodecs() const {
andresp@webrtc.org82775b12014-11-07 09:37:54 +0000546 std::vector<VideoCodec> supported_codecs = DefaultVideoCodecList();
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000547
548 if (external_encoder_factory_ == NULL) {
549 return supported_codecs;
550 }
551
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000552 const std::vector<WebRtcVideoEncoderFactory::VideoCodec>& codecs =
553 external_encoder_factory_->codecs();
554 for (size_t i = 0; i < codecs.size(); ++i) {
555 // Don't add internally-supported codecs twice.
556 if (CodecIsInternallySupported(codecs[i].name)) {
557 continue;
558 }
559
pkasting@chromium.orgd3245462015-02-23 21:28:22 +0000560 // External video encoders are given payloads 120-127. This also means that
561 // we only support up to 8 external payload types.
562 const int kExternalVideoPayloadTypeBase = 120;
563 size_t payload_type = kExternalVideoPayloadTypeBase + i;
564 assert(payload_type < 128);
565 VideoCodec codec(static_cast<int>(payload_type),
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000566 codecs[i].name,
567 codecs[i].max_width,
568 codecs[i].max_height,
569 codecs[i].max_fps,
570 0);
571
572 AddDefaultFeedbackParams(&codec);
573 supported_codecs.push_back(codec);
574 }
575 return supported_codecs;
576}
577
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000578WebRtcVideoChannel2::WebRtcVideoChannel2(
pbos@webrtc.org42684be2014-10-03 11:25:45 +0000579 WebRtcCallFactory* call_factory,
pbos@webrtc.org3bf3d232014-10-31 12:59:34 +0000580 WebRtcVoiceEngine* voice_engine,
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000581 VoiceMediaChannel* voice_channel,
pbos@webrtc.orgfa553ef2014-10-20 11:07:07 +0000582 const VideoOptions& options,
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000583 WebRtcVideoEncoderFactory* external_encoder_factory,
pbos@webrtc.orgf1c8b902015-01-14 17:29:27 +0000584 WebRtcVideoDecoderFactory* external_decoder_factory)
pbos@webrtc.orgb648b9d2014-08-26 11:08:06 +0000585 : unsignalled_ssrc_handler_(&default_unsignalled_ssrc_handler_),
pbos@webrtc.org8296ec52015-03-20 14:27:49 +0000586 voice_channel_id_(voice_channel != nullptr
587 ? static_cast<WebRtcVoiceMediaChannel*>(
588 voice_channel)->voe_channel()
589 : -1),
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000590 external_encoder_factory_(external_encoder_factory),
pbos@webrtc.orgf1c8b902015-01-14 17:29:27 +0000591 external_decoder_factory_(external_decoder_factory) {
pbos@webrtc.orgfa553ef2014-10-20 11:07:07 +0000592 SetDefaultOptions();
593 options_.SetAll(options);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000594 webrtc::Call::Config config(this);
pbos@webrtc.org42684be2014-10-03 11:25:45 +0000595 config.overuse_callback = this;
pbos@webrtc.org3bf3d232014-10-31 12:59:34 +0000596 if (voice_engine != NULL) {
597 config.voice_engine = voice_engine->voe()->engine();
598 }
pbos@webrtc.orgfa553ef2014-10-20 11:07:07 +0000599
pbos@webrtc.org42684be2014-10-03 11:25:45 +0000600 call_.reset(call_factory->CreateCall(config));
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000601
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000602 rtcp_receiver_report_ssrc_ = kDefaultRtcpReceiverReportSsrc;
603 sending_ = false;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000604 default_send_ssrc_ = 0;
pbos@webrtc.org6f48f1b2014-07-22 16:29:54 +0000605}
606
607void WebRtcVideoChannel2::SetDefaultOptions() {
pbos@webrtc.org42684be2014-10-03 11:25:45 +0000608 options_.cpu_overuse_detection.Set(false);
pbos@webrtc.orgd8198032014-11-10 14:41:43 +0000609 options_.dscp.Set(false);
pbos@webrtc.org5ff71ab2014-07-23 07:28:56 +0000610 options_.suspend_below_min_bitrate.Set(false);
pbos@webrtc.org42684be2014-10-03 11:25:45 +0000611 options_.video_noise_reduction.Set(true);
pbos@webrtc.orgefc82c22014-10-27 13:58:00 +0000612 options_.screencast_min_bitrate.Set(0);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000613}
614
615WebRtcVideoChannel2::~WebRtcVideoChannel2() {
616 for (std::map<uint32, WebRtcVideoSendStream*>::iterator it =
617 send_streams_.begin();
618 it != send_streams_.end();
619 ++it) {
620 delete it->second;
621 }
622
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000623 for (std::map<uint32, WebRtcVideoReceiveStream*>::iterator it =
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000624 receive_streams_.begin();
625 it != receive_streams_.end();
626 ++it) {
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000627 delete it->second;
628 }
629}
630
631bool WebRtcVideoChannel2::Init() { return true; }
632
pbos@webrtc.org96a93252014-11-03 14:46:44 +0000633bool WebRtcVideoChannel2::CodecIsExternallySupported(
634 const std::string& name) const {
635 if (external_encoder_factory_ == NULL) {
636 return false;
637 }
638
639 const std::vector<WebRtcVideoEncoderFactory::VideoCodec> external_codecs =
640 external_encoder_factory_->codecs();
641 for (size_t c = 0; c < external_codecs.size(); ++c) {
642 if (CodecNameMatches(name, external_codecs[c].name)) {
643 return true;
644 }
645 }
646 return false;
647}
648
649std::vector<WebRtcVideoChannel2::VideoCodecSettings>
650WebRtcVideoChannel2::FilterSupportedCodecs(
651 const std::vector<WebRtcVideoChannel2::VideoCodecSettings>& mapped_codecs)
652 const {
653 std::vector<VideoCodecSettings> supported_codecs;
654 for (size_t i = 0; i < mapped_codecs.size(); ++i) {
655 const VideoCodecSettings& codec = mapped_codecs[i];
656 if (CodecIsInternallySupported(codec.codec.name) ||
657 CodecIsExternallySupported(codec.codec.name)) {
658 supported_codecs.push_back(codec);
659 }
660 }
661 return supported_codecs;
662}
663
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000664bool WebRtcVideoChannel2::SetRecvCodecs(const std::vector<VideoCodec>& codecs) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000665 TRACE_EVENT0("webrtc", "WebRtcVideoChannel2::SetRecvCodecs");
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000666 LOG(LS_INFO) << "SetRecvCodecs: " << CodecVectorToString(codecs);
667 if (!ValidateCodecFormats(codecs)) {
668 return false;
669 }
670
671 const std::vector<VideoCodecSettings> mapped_codecs = MapCodecs(codecs);
672 if (mapped_codecs.empty()) {
pbos@webrtc.org96a93252014-11-03 14:46:44 +0000673 LOG(LS_ERROR) << "SetRecvCodecs called without any video codecs.";
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000674 return false;
675 }
676
pbos@webrtc.org96a93252014-11-03 14:46:44 +0000677 const std::vector<VideoCodecSettings> supported_codecs =
678 FilterSupportedCodecs(mapped_codecs);
679
680 if (mapped_codecs.size() != supported_codecs.size()) {
681 LOG(LS_ERROR) << "SetRecvCodecs called with unsupported video codecs.";
682 return false;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000683 }
684
pbos@webrtc.org96a93252014-11-03 14:46:44 +0000685 recv_codecs_ = supported_codecs;
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000686
pbos@webrtc.org575d1262014-10-08 14:48:08 +0000687 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000688 for (std::map<uint32, WebRtcVideoReceiveStream*>::iterator it =
689 receive_streams_.begin();
690 it != receive_streams_.end();
691 ++it) {
692 it->second->SetRecvCodecs(recv_codecs_);
693 }
694
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000695 return true;
696}
697
698bool WebRtcVideoChannel2::SetSendCodecs(const std::vector<VideoCodec>& codecs) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +0000699 TRACE_EVENT0("webrtc", "WebRtcVideoChannel2::SetSendCodecs");
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000700 LOG(LS_INFO) << "SetSendCodecs: " << CodecVectorToString(codecs);
701 if (!ValidateCodecFormats(codecs)) {
702 return false;
703 }
704
705 const std::vector<VideoCodecSettings> supported_codecs =
706 FilterSupportedCodecs(MapCodecs(codecs));
707
708 if (supported_codecs.empty()) {
709 LOG(LS_ERROR) << "No video codecs supported by encoder factory.";
710 return false;
711 }
712
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000713 LOG(LS_INFO) << "Using codec: " << supported_codecs.front().codec.ToString();
714
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +0000715 VideoCodecSettings old_codec;
716 if (send_codec_.Get(&old_codec) && supported_codecs.front() == old_codec) {
717 // Using same codec, avoid reconfiguring.
718 return true;
719 }
720
721 send_codec_.Set(supported_codecs.front());
722
pbos@webrtc.org575d1262014-10-08 14:48:08 +0000723 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000724 for (std::map<uint32, WebRtcVideoSendStream*>::iterator it =
725 send_streams_.begin();
726 it != send_streams_.end();
727 ++it) {
728 assert(it->second != NULL);
729 it->second->SetCodec(supported_codecs.front());
730 }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000731
pbos@webrtc.org00873182014-11-25 14:03:34 +0000732 VideoCodec codec = supported_codecs.front().codec;
733 int bitrate_kbps;
734 if (codec.GetParam(kCodecParamMinBitrate, &bitrate_kbps) &&
735 bitrate_kbps > 0) {
736 bitrate_config_.min_bitrate_bps = bitrate_kbps * 1000;
737 } else {
738 bitrate_config_.min_bitrate_bps = 0;
739 }
740 if (codec.GetParam(kCodecParamStartBitrate, &bitrate_kbps) &&
741 bitrate_kbps > 0) {
742 bitrate_config_.start_bitrate_bps = bitrate_kbps * 1000;
743 } else {
744 // Do not reconfigure start bitrate unless it's specified and positive.
745 bitrate_config_.start_bitrate_bps = -1;
746 }
747 if (codec.GetParam(kCodecParamMaxBitrate, &bitrate_kbps) &&
748 bitrate_kbps > 0) {
749 bitrate_config_.max_bitrate_bps = bitrate_kbps * 1000;
750 } else {
751 bitrate_config_.max_bitrate_bps = -1;
752 }
753 call_->SetBitrateConfig(bitrate_config_);
754
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000755 return true;
756}
757
758bool WebRtcVideoChannel2::GetSendCodec(VideoCodec* codec) {
759 VideoCodecSettings codec_settings;
760 if (!send_codec_.Get(&codec_settings)) {
761 LOG(LS_VERBOSE) << "GetSendCodec: No send codec set.";
762 return false;
763 }
764 *codec = codec_settings.codec;
765 return true;
766}
767
768bool WebRtcVideoChannel2::SetSendStreamFormat(uint32 ssrc,
769 const VideoFormat& format) {
770 LOG(LS_VERBOSE) << "SetSendStreamFormat:" << ssrc << " -> "
771 << format.ToString();
pbos@webrtc.org575d1262014-10-08 14:48:08 +0000772 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000773 if (send_streams_.find(ssrc) == send_streams_.end()) {
774 return false;
775 }
776 return send_streams_[ssrc]->SetVideoFormat(format);
777}
778
779bool WebRtcVideoChannel2::SetRender(bool render) {
780 // TODO(pbos): Implement. Or refactor away as it shouldn't be needed.
781 LOG(LS_VERBOSE) << "SetRender: " << (render ? "true" : "false");
782 return true;
783}
784
785bool WebRtcVideoChannel2::SetSend(bool send) {
786 LOG(LS_VERBOSE) << "SetSend: " << (send ? "true" : "false");
787 if (send && !send_codec_.IsSet()) {
788 LOG(LS_ERROR) << "SetSend(true) called before setting codec.";
789 return false;
790 }
791 if (send) {
792 StartAllSendStreams();
793 } else {
794 StopAllSendStreams();
795 }
796 sending_ = send;
797 return true;
798}
799
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000800bool WebRtcVideoChannel2::AddSendStream(const StreamParams& sp) {
801 LOG(LS_INFO) << "AddSendStream: " << sp.ToString();
802 if (sp.ssrcs.empty()) {
803 LOG(LS_ERROR) << "No SSRCs in stream parameters.";
804 return false;
805 }
806
807 uint32 ssrc = sp.first_ssrc();
808 assert(ssrc != 0);
809 // TODO(pbos): Make sure none of sp.ssrcs are used, not just the identifying
810 // ssrc.
pbos@webrtc.org575d1262014-10-08 14:48:08 +0000811 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000812 if (send_streams_.find(ssrc) != send_streams_.end()) {
pbos@webrtc.orga2a6fe62015-03-06 15:35:19 +0000813 LOG(LS_ERROR) << "Send stream with SSRC '" << ssrc << "' already exists.";
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000814 return false;
815 }
816
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +0000817 std::vector<uint32> primary_ssrcs;
818 sp.GetPrimarySsrcs(&primary_ssrcs);
819 std::vector<uint32> rtx_ssrcs;
820 sp.GetFidSsrcs(primary_ssrcs, &rtx_ssrcs);
821 if (!rtx_ssrcs.empty() && primary_ssrcs.size() != rtx_ssrcs.size()) {
822 LOG(LS_ERROR)
823 << "RTX SSRCs exist, but don't cover all SSRCs (unsupported): "
824 << sp.ToString();
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000825 return false;
826 }
827
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000828 WebRtcVideoSendStream* stream =
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000829 new WebRtcVideoSendStream(call_.get(),
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +0000830 external_encoder_factory_,
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000831 options_,
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +0000832 send_codec_,
833 sp,
834 send_rtp_extensions_);
835
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000836 send_streams_[ssrc] = stream;
837
838 if (rtcp_receiver_report_ssrc_ == kDefaultRtcpReceiverReportSsrc) {
839 rtcp_receiver_report_ssrc_ = ssrc;
840 }
841 if (default_send_ssrc_ == 0) {
842 default_send_ssrc_ = ssrc;
843 }
844 if (sending_) {
845 stream->Start();
846 }
847
848 return true;
849}
850
851bool WebRtcVideoChannel2::RemoveSendStream(uint32 ssrc) {
852 LOG(LS_INFO) << "RemoveSendStream: " << ssrc;
853
854 if (ssrc == 0) {
855 if (default_send_ssrc_ == 0) {
856 LOG(LS_ERROR) << "No default send stream active.";
857 return false;
858 }
859
860 LOG(LS_VERBOSE) << "Removing default stream: " << default_send_ssrc_;
861 ssrc = default_send_ssrc_;
862 }
863
pbos@webrtc.org575d1262014-10-08 14:48:08 +0000864 WebRtcVideoSendStream* removed_stream;
865 {
866 rtc::CritScope stream_lock(&stream_crit_);
867 std::map<uint32, WebRtcVideoSendStream*>::iterator it =
868 send_streams_.find(ssrc);
869 if (it == send_streams_.end()) {
870 return false;
871 }
872
873 removed_stream = it->second;
874 send_streams_.erase(it);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000875 }
876
pbos@webrtc.org575d1262014-10-08 14:48:08 +0000877 delete removed_stream;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000878
879 if (ssrc == default_send_ssrc_) {
880 default_send_ssrc_ = 0;
881 }
882
883 return true;
884}
885
886bool WebRtcVideoChannel2::AddRecvStream(const StreamParams& sp) {
pbos@webrtc.orga2a6fe62015-03-06 15:35:19 +0000887 return AddRecvStream(sp, false);
888}
889
890bool WebRtcVideoChannel2::AddRecvStream(const StreamParams& sp,
891 bool default_stream) {
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000892 LOG(LS_INFO) << "AddRecvStream: " << sp.ToString();
893 assert(sp.ssrcs.size() > 0);
894
895 uint32 ssrc = sp.first_ssrc();
896 assert(ssrc != 0); // TODO(pbos): Is this ever valid?
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000897
898 // TODO(pbos): Check if any of the SSRCs overlap.
pbos@webrtc.org575d1262014-10-08 14:48:08 +0000899 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.orga2a6fe62015-03-06 15:35:19 +0000900 {
901 auto it = receive_streams_.find(ssrc);
902 if (it != receive_streams_.end()) {
903 if (default_stream || !it->second->IsDefaultStream()) {
904 LOG(LS_ERROR) << "Receive stream for SSRC '" << ssrc
905 << "' already exists.";
906 return false;
907 }
908 delete it->second;
909 receive_streams_.erase(it);
910 }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000911 }
912
pbos@webrtc.orgbd249bc2014-07-07 04:45:15 +0000913 webrtc::VideoReceiveStream::Config config;
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000914 ConfigureReceiverRtp(&config, sp);
pbos@webrtc.org3bf3d232014-10-31 12:59:34 +0000915
916 // Set up A/V sync if there is a VoiceChannel.
917 // TODO(pbos): The A/V is synched by the receiving channel. So we need to know
918 // the SSRC of the remote audio channel in order to sync the correct webrtc
919 // VoiceEngine channel. For now sync the first channel in non-conference to
920 // match existing behavior in WebRtcVideoEngine.
pbos@webrtc.org8296ec52015-03-20 14:27:49 +0000921 if (voice_channel_id_ != -1 && receive_streams_.empty() &&
pbos@webrtc.org3bf3d232014-10-31 12:59:34 +0000922 !options_.conference_mode.GetWithDefaultIfUnset(false)) {
pbos@webrtc.org8296ec52015-03-20 14:27:49 +0000923 config.audio_channel_id = voice_channel_id_;
pbos@webrtc.org3bf3d232014-10-31 12:59:34 +0000924 }
925
pbos@webrtc.orga2a6fe62015-03-06 15:35:19 +0000926 receive_streams_[ssrc] =
927 new WebRtcVideoReceiveStream(call_.get(), external_decoder_factory_,
928 default_stream, config, recv_codecs_);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000929
930 return true;
931}
932
933void WebRtcVideoChannel2::ConfigureReceiverRtp(
934 webrtc::VideoReceiveStream::Config* config,
935 const StreamParams& sp) const {
936 uint32 ssrc = sp.first_ssrc();
937
938 config->rtp.remote_ssrc = ssrc;
939 config->rtp.local_ssrc = rtcp_receiver_report_ssrc_;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000940
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000941 config->rtp.extensions = recv_rtp_extensions_;
pbos@webrtc.org257e1302014-07-25 19:01:32 +0000942
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000943 // TODO(pbos): This protection is against setting the same local ssrc as
944 // remote which is not permitted by the lower-level API. RTCP requires a
945 // corresponding sender SSRC. Figure out what to do when we don't have
946 // (receive-only) or know a good local SSRC.
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000947 if (config->rtp.remote_ssrc == config->rtp.local_ssrc) {
948 if (config->rtp.local_ssrc != kDefaultRtcpReceiverReportSsrc) {
949 config->rtp.local_ssrc = kDefaultRtcpReceiverReportSsrc;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000950 } else {
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000951 config->rtp.local_ssrc = kDefaultRtcpReceiverReportSsrc + 1;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000952 }
953 }
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000954
955 for (size_t i = 0; i < recv_codecs_.size(); ++i) {
andresp@webrtc.org82775b12014-11-07 09:37:54 +0000956 MergeFecConfig(recv_codecs_[i].fec, &config->rtp.fec);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000957 }
958
andresp@webrtc.org82775b12014-11-07 09:37:54 +0000959 for (size_t i = 0; i < recv_codecs_.size(); ++i) {
960 uint32 rtx_ssrc;
961 if (recv_codecs_[i].rtx_payload_type != -1 &&
962 sp.GetFidSsrc(ssrc, &rtx_ssrc)) {
963 webrtc::VideoReceiveStream::Config::Rtp::Rtx& rtx =
964 config->rtp.rtx[recv_codecs_[i].codec.id];
965 rtx.ssrc = rtx_ssrc;
966 rtx.payload_type = recv_codecs_[i].rtx_payload_type;
967 }
968 }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000969}
970
971bool WebRtcVideoChannel2::RemoveRecvStream(uint32 ssrc) {
972 LOG(LS_INFO) << "RemoveRecvStream: " << ssrc;
973 if (ssrc == 0) {
pbos@webrtc.orgafb554f42014-08-12 23:17:13 +0000974 LOG(LS_ERROR) << "RemoveRecvStream with 0 ssrc is not supported.";
975 return false;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000976 }
977
pbos@webrtc.org575d1262014-10-08 14:48:08 +0000978 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000979 std::map<uint32, WebRtcVideoReceiveStream*>::iterator stream =
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000980 receive_streams_.find(ssrc);
981 if (stream == receive_streams_.end()) {
982 LOG(LS_ERROR) << "Stream not found for ssrc: " << ssrc;
983 return false;
984 }
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000985 delete stream->second;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000986 receive_streams_.erase(stream);
987
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000988 return true;
989}
990
991bool WebRtcVideoChannel2::SetRenderer(uint32 ssrc, VideoRenderer* renderer) {
992 LOG(LS_INFO) << "SetRenderer: ssrc:" << ssrc << " "
993 << (renderer ? "(ptr)" : "NULL");
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000994 if (ssrc == 0) {
pbos@webrtc.orgafb554f42014-08-12 23:17:13 +0000995 default_unsignalled_ssrc_handler_.SetDefaultRenderer(this, renderer);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +0000996 return true;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000997 }
998
pbos@webrtc.org575d1262014-10-08 14:48:08 +0000999 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001000 std::map<uint32, WebRtcVideoReceiveStream*>::iterator it =
1001 receive_streams_.find(ssrc);
1002 if (it == receive_streams_.end()) {
1003 return false;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001004 }
1005
1006 it->second->SetRenderer(renderer);
1007 return true;
1008}
1009
1010bool WebRtcVideoChannel2::GetRenderer(uint32 ssrc, VideoRenderer** renderer) {
1011 if (ssrc == 0) {
pbos@webrtc.orgafb554f42014-08-12 23:17:13 +00001012 *renderer = default_unsignalled_ssrc_handler_.GetDefaultRenderer();
1013 return *renderer != NULL;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001014 }
1015
pbos@webrtc.org575d1262014-10-08 14:48:08 +00001016 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001017 std::map<uint32, WebRtcVideoReceiveStream*>::iterator it =
1018 receive_streams_.find(ssrc);
1019 if (it == receive_streams_.end()) {
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001020 return false;
1021 }
1022 *renderer = it->second->GetRenderer();
1023 return true;
1024}
1025
pbos@webrtc.org058b1f12015-03-04 08:54:32 +00001026bool WebRtcVideoChannel2::GetStats(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.orga2a6fe62015-03-06 15:35:19 +00001123 // TODO(pbos): Ignore unsignalled packets that don't use the video payload
1124 // (prevent creating default receivers for RTX configured as if it would
1125 // receive media payloads on those SSRCs).
pbos@webrtc.orgafb554f42014-08-12 23:17:13 +00001126 switch (unsignalled_ssrc_handler_->OnUnsignalledSsrc(this, ssrc)) {
1127 case UnsignalledSsrcHandler::kDropPacket:
1128 return;
1129 case UnsignalledSsrcHandler::kDeliverPacket:
1130 break;
1131 }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001132
pbos@webrtc.org1e019d12014-05-16 11:38:45 +00001133 if (call_->Receiver()->DeliverPacket(
1134 reinterpret_cast<const uint8_t*>(packet->data()), packet->length()) !=
1135 webrtc::PacketReceiver::DELIVERY_OK) {
pbos@webrtc.orgafb554f42014-08-12 23:17:13 +00001136 LOG(LS_WARNING) << "Failed to deliver RTP packet on re-delivery.";
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001137 return;
1138 }
1139}
1140
1141void WebRtcVideoChannel2::OnRtcpReceived(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001142 rtc::Buffer* packet,
1143 const rtc::PacketTime& packet_time) {
pbos@webrtc.org1e019d12014-05-16 11:38:45 +00001144 if (call_->Receiver()->DeliverPacket(
1145 reinterpret_cast<const uint8_t*>(packet->data()), packet->length()) !=
1146 webrtc::PacketReceiver::DELIVERY_OK) {
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001147 LOG(LS_WARNING) << "Failed to deliver RTCP packet.";
1148 }
1149}
1150
1151void WebRtcVideoChannel2::OnReadyToSend(bool ready) {
pbos@webrtc.org26c0c412014-09-03 16:17:12 +00001152 LOG(LS_VERBOSE) << "OnReadyToSend: " << (ready ? "Ready." : "Not ready.");
1153 call_->SignalNetworkState(ready ? webrtc::Call::kNetworkUp
1154 : webrtc::Call::kNetworkDown);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001155}
1156
1157bool WebRtcVideoChannel2::MuteStream(uint32 ssrc, bool mute) {
1158 LOG(LS_VERBOSE) << "MuteStream: " << ssrc << " -> "
1159 << (mute ? "mute" : "unmute");
1160 assert(ssrc != 0);
pbos@webrtc.org575d1262014-10-08 14:48:08 +00001161 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001162 if (send_streams_.find(ssrc) == send_streams_.end()) {
1163 LOG(LS_ERROR) << "No sending stream on ssrc " << ssrc;
1164 return false;
1165 }
pbos@webrtc.orgef8bb8d2014-08-13 21:36:18 +00001166
1167 send_streams_[ssrc]->MuteStream(mute);
1168 return true;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001169}
1170
1171bool WebRtcVideoChannel2::SetRecvRtpHeaderExtensions(
1172 const std::vector<RtpHeaderExtension>& extensions) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +00001173 TRACE_EVENT0("webrtc", "WebRtcVideoChannel2::SetRecvRtpHeaderExtensions");
pbos@webrtc.org587ef602014-06-16 17:32:02 +00001174 LOG(LS_INFO) << "SetRecvRtpHeaderExtensions: "
1175 << RtpExtensionsToString(extensions);
pbos@webrtc.org3c107582014-07-20 15:27:35 +00001176 if (!ValidateRtpHeaderExtensionIds(extensions))
1177 return false;
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001178
pbos@webrtc.orgc37e72e2015-01-05 18:51:13 +00001179 std::vector<webrtc::RtpExtension> filtered_extensions =
1180 FilterRtpExtensions(extensions);
1181 if (!RtpExtensionsHaveChanged(recv_rtp_extensions_, filtered_extensions))
1182 return true;
1183
1184 recv_rtp_extensions_ = filtered_extensions;
1185
pbos@webrtc.org575d1262014-10-08 14:48:08 +00001186 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001187 for (std::map<uint32, WebRtcVideoReceiveStream*>::iterator it =
1188 receive_streams_.begin();
1189 it != receive_streams_.end();
1190 ++it) {
1191 it->second->SetRtpExtensions(recv_rtp_extensions_);
1192 }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001193 return true;
1194}
1195
1196bool WebRtcVideoChannel2::SetSendRtpHeaderExtensions(
1197 const std::vector<RtpHeaderExtension>& extensions) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +00001198 TRACE_EVENT0("webrtc", "WebRtcVideoChannel2::SetSendRtpHeaderExtensions");
pbos@webrtc.org587ef602014-06-16 17:32:02 +00001199 LOG(LS_INFO) << "SetSendRtpHeaderExtensions: "
1200 << RtpExtensionsToString(extensions);
pbos@webrtc.org3c107582014-07-20 15:27:35 +00001201 if (!ValidateRtpHeaderExtensionIds(extensions))
1202 return false;
1203
pbos@webrtc.orgc37e72e2015-01-05 18:51:13 +00001204 std::vector<webrtc::RtpExtension> filtered_extensions =
1205 FilterRtpExtensions(extensions);
1206 if (!RtpExtensionsHaveChanged(send_rtp_extensions_, filtered_extensions))
1207 return true;
1208
1209 send_rtp_extensions_ = filtered_extensions;
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +00001210
pbos@webrtc.org575d1262014-10-08 14:48:08 +00001211 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001212 for (std::map<uint32, WebRtcVideoSendStream*>::iterator it =
1213 send_streams_.begin();
1214 it != send_streams_.end();
1215 ++it) {
1216 it->second->SetRtpExtensions(send_rtp_extensions_);
1217 }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001218 return true;
1219}
1220
pbos@webrtc.org00873182014-11-25 14:03:34 +00001221bool WebRtcVideoChannel2::SetMaxSendBandwidth(int max_bitrate_bps) {
1222 LOG(LS_INFO) << "SetMaxSendBandwidth: " << max_bitrate_bps << "bps.";
1223 if (max_bitrate_bps <= 0) {
1224 // Unsetting max bitrate.
1225 max_bitrate_bps = -1;
1226 }
1227 bitrate_config_.start_bitrate_bps = -1;
1228 bitrate_config_.max_bitrate_bps = max_bitrate_bps;
1229 if (max_bitrate_bps > 0 &&
1230 bitrate_config_.min_bitrate_bps > max_bitrate_bps) {
1231 bitrate_config_.min_bitrate_bps = max_bitrate_bps;
1232 }
1233 call_->SetBitrateConfig(bitrate_config_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001234 return true;
1235}
1236
1237bool WebRtcVideoChannel2::SetOptions(const VideoOptions& options) {
pbos@webrtc.org50fe3592015-01-29 12:33:07 +00001238 TRACE_EVENT0("webrtc", "WebRtcVideoChannel2::SetOptions");
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +00001239 LOG(LS_INFO) << "SetOptions: " << options.ToString();
1240 VideoOptions old_options = options_;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001241 options_.SetAll(options);
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +00001242 if (options_ == old_options) {
1243 // No new options to set.
1244 return true;
1245 }
pbos@webrtc.orgd8198032014-11-10 14:41:43 +00001246 rtc::DiffServCodePoint dscp = options_.dscp.GetWithDefaultIfUnset(false)
1247 ? rtc::DSCP_AF41
1248 : rtc::DSCP_DEFAULT;
1249 MediaChannel::SetDscp(dscp);
pbos@webrtc.org575d1262014-10-08 14:48:08 +00001250 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001251 for (std::map<uint32, WebRtcVideoSendStream*>::iterator it =
1252 send_streams_.begin();
1253 it != send_streams_.end();
1254 ++it) {
1255 it->second->SetOptions(options_);
1256 }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001257 return true;
1258}
1259
1260void WebRtcVideoChannel2::SetInterface(NetworkInterface* iface) {
1261 MediaChannel::SetInterface(iface);
1262 // Set the RTP recv/send buffer to a bigger size
1263 MediaChannel::SetOption(NetworkInterface::ST_RTP,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001264 rtc::Socket::OPT_RCVBUF,
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001265 kVideoRtpBufferSize);
1266
buildbot@webrtc.orgae694ef2014-10-28 17:37:17 +00001267 // Speculative change to increase the outbound socket buffer size.
1268 // In b/15152257, we are seeing a significant number of packets discarded
1269 // due to lack of socket buffer space, although it's not yet clear what the
1270 // ideal value should be.
1271 MediaChannel::SetOption(NetworkInterface::ST_RTP,
1272 rtc::Socket::OPT_SNDBUF,
1273 kVideoRtpBufferSize);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001274}
1275
1276void WebRtcVideoChannel2::UpdateAspectRatio(int ratio_w, int ratio_h) {
1277 // TODO(pbos): Implement.
1278}
1279
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001280void WebRtcVideoChannel2::OnMessage(rtc::Message* msg) {
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001281 // Ignored.
1282}
1283
pbos@webrtc.org42684be2014-10-03 11:25:45 +00001284void WebRtcVideoChannel2::OnLoadUpdate(Load load) {
pbos@webrtc.org575d1262014-10-08 14:48:08 +00001285 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.org42684be2014-10-03 11:25:45 +00001286 for (std::map<uint32, WebRtcVideoSendStream*>::iterator it =
1287 send_streams_.begin();
1288 it != send_streams_.end();
1289 ++it) {
1290 it->second->OnCpuResolutionRequest(load == kOveruse
1291 ? CoordinatedVideoAdapter::DOWNGRADE
1292 : CoordinatedVideoAdapter::UPGRADE);
1293 }
1294}
1295
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001296bool WebRtcVideoChannel2::SendRtp(const uint8_t* data, size_t len) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001297 rtc::Buffer packet(data, len, kMaxRtpPacketLen);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001298 return MediaChannel::SendPacket(&packet);
1299}
1300
1301bool WebRtcVideoChannel2::SendRtcp(const uint8_t* data, size_t len) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001302 rtc::Buffer packet(data, len, kMaxRtpPacketLen);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001303 return MediaChannel::SendRtcp(&packet);
1304}
1305
1306void WebRtcVideoChannel2::StartAllSendStreams() {
pbos@webrtc.org575d1262014-10-08 14:48:08 +00001307 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001308 for (std::map<uint32, WebRtcVideoSendStream*>::iterator it =
1309 send_streams_.begin();
1310 it != send_streams_.end();
1311 ++it) {
1312 it->second->Start();
1313 }
1314}
1315
1316void WebRtcVideoChannel2::StopAllSendStreams() {
pbos@webrtc.org575d1262014-10-08 14:48:08 +00001317 rtc::CritScope stream_lock(&stream_crit_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001318 for (std::map<uint32, WebRtcVideoSendStream*>::iterator it =
1319 send_streams_.begin();
1320 it != send_streams_.end();
1321 ++it) {
1322 it->second->Stop();
1323 }
1324}
1325
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +00001326WebRtcVideoChannel2::WebRtcVideoSendStream::VideoSendStreamParameters::
1327 VideoSendStreamParameters(
1328 const webrtc::VideoSendStream::Config& config,
1329 const VideoOptions& options,
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001330 const Settable<VideoCodecSettings>& codec_settings)
1331 : config(config), options(options), codec_settings(codec_settings) {
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +00001332}
1333
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001334WebRtcVideoChannel2::WebRtcVideoSendStream::WebRtcVideoSendStream(
1335 webrtc::Call* call,
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +00001336 WebRtcVideoEncoderFactory* external_encoder_factory,
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +00001337 const VideoOptions& options,
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001338 const Settable<VideoCodecSettings>& codec_settings,
1339 const StreamParams& sp,
1340 const std::vector<webrtc::RtpExtension>& rtp_extensions)
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001341 : call_(call),
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +00001342 external_encoder_factory_(external_encoder_factory),
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001343 stream_(NULL),
pbos@webrtc.orgb648b9d2014-08-26 11:08:06 +00001344 parameters_(webrtc::VideoSendStream::Config(), options, codec_settings),
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +00001345 allocated_encoder_(NULL, webrtc::kVideoCodecUnknown, false),
pbos@webrtc.orgb648b9d2014-08-26 11:08:06 +00001346 capturer_(NULL),
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001347 sending_(false),
pbos@webrtc.org9a4410e2015-02-26 10:03:39 +00001348 muted_(false),
1349 old_adapt_changes_(0) {
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001350 parameters_.config.rtp.max_packet_size = kVideoMtu;
1351
1352 sp.GetPrimarySsrcs(&parameters_.config.rtp.ssrcs);
1353 sp.GetFidSsrcs(parameters_.config.rtp.ssrcs,
1354 &parameters_.config.rtp.rtx.ssrcs);
1355 parameters_.config.rtp.c_name = sp.cname;
1356 parameters_.config.rtp.extensions = rtp_extensions;
1357
1358 VideoCodecSettings params;
1359 if (codec_settings.Get(&params)) {
1360 SetCodec(params);
1361 }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001362}
1363
1364WebRtcVideoChannel2::WebRtcVideoSendStream::~WebRtcVideoSendStream() {
1365 DisconnectCapturer();
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001366 if (stream_ != NULL) {
1367 call_->DestroyVideoSendStream(stream_);
1368 }
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +00001369 DestroyVideoEncoder(&allocated_encoder_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001370}
1371
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001372static void CreateBlackFrame(webrtc::I420VideoFrame* video_frame,
1373 int width,
1374 int height) {
pbos@webrtc.orgb4987bf2015-02-18 10:13:09 +00001375 video_frame->CreateEmptyFrame(width, height, width, (width + 1) / 2,
1376 (width + 1) / 2);
1377 memset(video_frame->buffer(webrtc::kYPlane), 16,
1378 video_frame->allocated_size(webrtc::kYPlane));
1379 memset(video_frame->buffer(webrtc::kUPlane), 128,
1380 video_frame->allocated_size(webrtc::kUPlane));
1381 memset(video_frame->buffer(webrtc::kVPlane), 128,
1382 video_frame->allocated_size(webrtc::kVPlane));
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001383}
1384
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001385void WebRtcVideoChannel2::WebRtcVideoSendStream::InputFrame(
1386 VideoCapturer* capturer,
1387 const VideoFrame* frame) {
pbos@webrtc.org86196c42015-02-16 21:02:00 +00001388 TRACE_EVENT0("webrtc", "WebRtcVideoSendStream::InputFrame");
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001389 LOG(LS_VERBOSE) << "InputFrame: " << frame->GetWidth() << "x"
1390 << frame->GetHeight();
magjed@webrtc.orgafdd5dd2015-03-12 13:11:25 +00001391 webrtc::I420VideoFrame video_frame(frame->GetVideoFrameBuffer(), 0, 0,
1392 frame->GetVideoRotation());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001393 rtc::CritScope cs(&lock_);
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001394 if (stream_ == NULL) {
1395 LOG(LS_WARNING) << "Capturer inputting frames before send codecs are "
1396 "configured, dropping.";
1397 return;
1398 }
pbos@webrtc.org86196c42015-02-16 21:02:00 +00001399
1400 // Not sending, abort early to prevent expensive reconfigurations while
1401 // setting up codecs etc.
1402 if (!sending_)
1403 return;
1404
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001405 if (format_.width == 0) { // Dropping frames.
1406 assert(format_.height == 0);
1407 LOG(LS_VERBOSE) << "VideoFormat 0x0 set, Dropping frame.";
1408 return;
1409 }
pbos@webrtc.orgd60d79a2014-09-24 07:10:57 +00001410 if (muted_) {
1411 // Create a black frame to transmit instead.
magjed@webrtc.orgafdd5dd2015-03-12 13:11:25 +00001412 CreateBlackFrame(&video_frame,
pbos@webrtc.orgd60d79a2014-09-24 07:10:57 +00001413 static_cast<int>(frame->GetWidth()),
1414 static_cast<int>(frame->GetHeight()));
1415 }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001416 // Reconfigure codec if necessary.
pbos@webrtc.orgc4175b92014-09-03 15:25:49 +00001417 SetDimensions(
magjed@webrtc.orgafdd5dd2015-03-12 13:11:25 +00001418 video_frame.width(), video_frame.height(), capturer->IsScreencast());
pbos@webrtc.orgc4175b92014-09-03 15:25:49 +00001419
perkj@webrtc.orgaf612d52015-03-18 09:51:05 +00001420 LOG(LS_VERBOSE) << "IncomingCapturedFrame: " << video_frame.width() << "x"
magjed@webrtc.orgafdd5dd2015-03-12 13:11:25 +00001421 << video_frame.height() << " -> (codec) "
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +00001422 << parameters_.encoder_config.streams.back().width << "x"
1423 << parameters_.encoder_config.streams.back().height;
perkj@webrtc.orgaf612d52015-03-18 09:51:05 +00001424 stream_->Input()->IncomingCapturedFrame(video_frame);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001425}
1426
1427bool WebRtcVideoChannel2::WebRtcVideoSendStream::SetCapturer(
1428 VideoCapturer* capturer) {
pbos@webrtc.orgb4987bf2015-02-18 10:13:09 +00001429 TRACE_EVENT0("webrtc", "WebRtcVideoSendStream::SetCapturer");
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001430 if (!DisconnectCapturer() && capturer == NULL) {
1431 return false;
1432 }
1433
1434 {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001435 rtc::CritScope cs(&lock_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001436
pbos@webrtc.org9359cb32014-07-23 15:44:48 +00001437 if (capturer == NULL) {
1438 if (stream_ != NULL) {
1439 LOG(LS_VERBOSE) << "Disabling capturer, sending black frame.";
1440 webrtc::I420VideoFrame black_frame;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001441
pbos@webrtc.orgb4987bf2015-02-18 10:13:09 +00001442 CreateBlackFrame(&black_frame, last_dimensions_.width,
1443 last_dimensions_.height);
perkj@webrtc.orgaf612d52015-03-18 09:51:05 +00001444 stream_->Input()->IncomingCapturedFrame(black_frame);
pbos@webrtc.org9359cb32014-07-23 15:44:48 +00001445 }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001446
1447 capturer_ = NULL;
1448 return true;
1449 }
1450
1451 capturer_ = capturer;
1452 }
1453 // Lock cannot be held while connecting the capturer to prevent lock-order
1454 // violations.
1455 capturer->SignalVideoFrame.connect(this, &WebRtcVideoSendStream::InputFrame);
1456 return true;
1457}
1458
1459bool WebRtcVideoChannel2::WebRtcVideoSendStream::SetVideoFormat(
1460 const VideoFormat& format) {
1461 if ((format.width == 0 || format.height == 0) &&
1462 format.width != format.height) {
1463 LOG(LS_ERROR) << "Can't set VideoFormat, width or height is zero (but not "
1464 "both, 0x0 drops frames).";
1465 return false;
1466 }
1467
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001468 rtc::CritScope cs(&lock_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001469 if (format.width == 0 && format.height == 0) {
1470 LOG(LS_INFO)
1471 << "0x0 resolution selected. Captured frames will be dropped for ssrc: "
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +00001472 << parameters_.config.rtp.ssrcs[0] << ".";
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001473 } else {
1474 // TODO(pbos): Fix me, this only affects the last stream!
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +00001475 parameters_.encoder_config.streams.back().max_framerate =
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001476 VideoFormat::IntervalToFps(format.interval);
pbos@webrtc.orgc4175b92014-09-03 15:25:49 +00001477 SetDimensions(format.width, format.height, false);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001478 }
1479
1480 format_ = format;
1481 return true;
1482}
1483
pbos@webrtc.orgef8bb8d2014-08-13 21:36:18 +00001484void WebRtcVideoChannel2::WebRtcVideoSendStream::MuteStream(bool mute) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001485 rtc::CritScope cs(&lock_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001486 muted_ = mute;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001487}
1488
1489bool WebRtcVideoChannel2::WebRtcVideoSendStream::DisconnectCapturer() {
pbos@webrtc.org963b9792014-10-07 14:27:27 +00001490 cricket::VideoCapturer* capturer;
1491 {
1492 rtc::CritScope cs(&lock_);
pbos@webrtc.org9a4410e2015-02-26 10:03:39 +00001493 if (capturer_ == NULL)
pbos@webrtc.org963b9792014-10-07 14:27:27 +00001494 return false;
pbos@webrtc.org9a4410e2015-02-26 10:03:39 +00001495
1496 if (capturer_->video_adapter() != nullptr)
1497 old_adapt_changes_ += capturer_->video_adapter()->adaptation_changes();
1498
pbos@webrtc.org963b9792014-10-07 14:27:27 +00001499 capturer = capturer_;
1500 capturer_ = NULL;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001501 }
pbos@webrtc.org963b9792014-10-07 14:27:27 +00001502 capturer->SignalVideoFrame.disconnect(this);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001503 return true;
1504}
1505
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001506void WebRtcVideoChannel2::WebRtcVideoSendStream::SetOptions(
1507 const VideoOptions& options) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001508 rtc::CritScope cs(&lock_);
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001509 VideoCodecSettings codec_settings;
1510 if (parameters_.codec_settings.Get(&codec_settings)) {
1511 SetCodecAndOptions(codec_settings, options);
1512 } else {
1513 parameters_.options = options;
1514 }
1515}
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +00001516
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001517void WebRtcVideoChannel2::WebRtcVideoSendStream::SetCodec(
1518 const VideoCodecSettings& codec_settings) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001519 rtc::CritScope cs(&lock_);
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001520 SetCodecAndOptions(codec_settings, parameters_.options);
1521}
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +00001522
1523webrtc::VideoCodecType CodecTypeFromName(const std::string& name) {
1524 if (CodecNameMatches(name, kVp8CodecName)) {
1525 return webrtc::kVideoCodecVP8;
andresp@webrtc.org188d3b22014-11-07 13:21:04 +00001526 } else if (CodecNameMatches(name, kVp9CodecName)) {
1527 return webrtc::kVideoCodecVP9;
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +00001528 } else if (CodecNameMatches(name, kH264CodecName)) {
1529 return webrtc::kVideoCodecH264;
1530 }
1531 return webrtc::kVideoCodecUnknown;
1532}
1533
1534WebRtcVideoChannel2::WebRtcVideoSendStream::AllocatedEncoder
1535WebRtcVideoChannel2::WebRtcVideoSendStream::CreateVideoEncoder(
1536 const VideoCodec& codec) {
1537 webrtc::VideoCodecType type = CodecTypeFromName(codec.name);
1538
1539 // Do not re-create encoders of the same type.
1540 if (type == allocated_encoder_.type && allocated_encoder_.encoder != NULL) {
1541 return allocated_encoder_;
1542 }
1543
1544 if (external_encoder_factory_ != NULL) {
1545 webrtc::VideoEncoder* encoder =
1546 external_encoder_factory_->CreateVideoEncoder(type);
1547 if (encoder != NULL) {
1548 return AllocatedEncoder(encoder, type, true);
1549 }
1550 }
1551
1552 if (type == webrtc::kVideoCodecVP8) {
1553 return AllocatedEncoder(
1554 webrtc::VideoEncoder::Create(webrtc::VideoEncoder::kVp8), type, false);
andresp@webrtc.org188d3b22014-11-07 13:21:04 +00001555 } else if (type == webrtc::kVideoCodecVP9) {
1556 return AllocatedEncoder(
1557 webrtc::VideoEncoder::Create(webrtc::VideoEncoder::kVp9), type, false);
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +00001558 }
1559
1560 // This shouldn't happen, we should not be trying to create something we don't
1561 // support.
1562 assert(false);
1563 return AllocatedEncoder(NULL, webrtc::kVideoCodecUnknown, false);
1564}
1565
1566void WebRtcVideoChannel2::WebRtcVideoSendStream::DestroyVideoEncoder(
1567 AllocatedEncoder* encoder) {
1568 if (encoder->external) {
1569 external_encoder_factory_->DestroyVideoEncoder(encoder->encoder);
1570 } else {
1571 delete encoder->encoder;
1572 }
1573}
1574
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001575void WebRtcVideoChannel2::WebRtcVideoSendStream::SetCodecAndOptions(
1576 const VideoCodecSettings& codec_settings,
1577 const VideoOptions& options) {
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +00001578 parameters_.encoder_config =
1579 CreateVideoEncoderConfig(last_dimensions_, codec_settings.codec);
pbos@webrtc.org86196c42015-02-16 21:02:00 +00001580 if (parameters_.encoder_config.streams.empty())
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001581 return;
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +00001582
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001583 format_ = VideoFormat(codec_settings.codec.width,
1584 codec_settings.codec.height,
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001585 VideoFormat::FpsToInterval(30),
1586 FOURCC_I420);
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +00001587
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +00001588 AllocatedEncoder new_encoder = CreateVideoEncoder(codec_settings.codec);
1589 parameters_.config.encoder_settings.encoder = new_encoder.encoder;
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001590 parameters_.config.encoder_settings.payload_name = codec_settings.codec.name;
1591 parameters_.config.encoder_settings.payload_type = codec_settings.codec.id;
1592 parameters_.config.rtp.fec = codec_settings.fec;
1593
1594 // Set RTX payload type if RTX is enabled.
1595 if (!parameters_.config.rtp.rtx.ssrcs.empty()) {
pbos@webrtc.orgb9557a92015-03-20 19:52:56 +00001596 if (codec_settings.rtx_payload_type == -1) {
1597 LOG(LS_WARNING) << "RTX SSRCs configured but there's no configured RTX "
1598 "payload type. Ignoring.";
1599 parameters_.config.rtp.rtx.ssrcs.clear();
1600 } else {
1601 parameters_.config.rtp.rtx.payload_type = codec_settings.rtx_payload_type;
1602 }
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001603 }
1604
1605 if (IsNackEnabled(codec_settings.codec)) {
1606 parameters_.config.rtp.nack.rtp_history_ms = kNackHistoryMs;
1607 }
1608
pbos@webrtc.org5ff71ab2014-07-23 07:28:56 +00001609 options.suspend_below_min_bitrate.Get(
1610 &parameters_.config.suspend_below_min_bitrate);
1611
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001612 parameters_.codec_settings.Set(codec_settings);
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +00001613 parameters_.options = options;
pbos@webrtc.org543e5892014-07-23 07:01:31 +00001614
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001615 RecreateWebRtcStream();
pbos@webrtc.org7fe1e032014-10-14 04:25:33 +00001616 if (allocated_encoder_.encoder != new_encoder.encoder) {
1617 DestroyVideoEncoder(&allocated_encoder_);
1618 allocated_encoder_ = new_encoder;
1619 }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001620}
1621
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001622void WebRtcVideoChannel2::WebRtcVideoSendStream::SetRtpExtensions(
1623 const std::vector<webrtc::RtpExtension>& rtp_extensions) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001624 rtc::CritScope cs(&lock_);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001625 parameters_.config.rtp.extensions = rtp_extensions;
1626 RecreateWebRtcStream();
1627}
1628
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +00001629webrtc::VideoEncoderConfig
1630WebRtcVideoChannel2::WebRtcVideoSendStream::CreateVideoEncoderConfig(
1631 const Dimensions& dimensions,
1632 const VideoCodec& codec) const {
1633 webrtc::VideoEncoderConfig encoder_config;
1634 if (dimensions.is_screencast) {
pbos@webrtc.orgefc82c22014-10-27 13:58:00 +00001635 int screencast_min_bitrate_kbps;
1636 parameters_.options.screencast_min_bitrate.Get(
1637 &screencast_min_bitrate_kbps);
1638 encoder_config.min_transmit_bitrate_bps =
1639 screencast_min_bitrate_kbps * 1000;
1640 encoder_config.content_type = webrtc::VideoEncoderConfig::kScreenshare;
1641 } else {
1642 encoder_config.min_transmit_bitrate_bps = 0;
1643 encoder_config.content_type = webrtc::VideoEncoderConfig::kRealtimeVideo;
1644 }
1645
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +00001646 // Restrict dimensions according to codec max.
1647 int width = dimensions.width;
1648 int height = dimensions.height;
1649 if (!dimensions.is_screencast) {
1650 if (codec.width < width)
1651 width = codec.width;
1652 if (codec.height < height)
1653 height = codec.height;
1654 }
1655
1656 VideoCodec clamped_codec = codec;
1657 clamped_codec.width = width;
1658 clamped_codec.height = height;
pbos@webrtc.orgcddd17c2014-09-16 16:33:13 +00001659
pbos@webrtc.orgf1c8b902015-01-14 17:29:27 +00001660 encoder_config.streams = CreateVideoStreams(
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +00001661 clamped_codec, parameters_.options, parameters_.config.rtp.ssrcs.size());
pbos@webrtc.org6f48f1b2014-07-22 16:29:54 +00001662
pbos@webrtc.orgb7ed7792014-10-31 13:08:10 +00001663 // Conference mode screencast uses 2 temporal layers split at 100kbit.
1664 if (parameters_.options.conference_mode.GetWithDefaultIfUnset(false) &&
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +00001665 dimensions.is_screencast && encoder_config.streams.size() == 1) {
sprang@webrtc.org46d4d292014-12-23 15:19:35 +00001666 ScreenshareLayerConfig config = ScreenshareLayerConfig::GetDefault();
1667
1668 // For screenshare in conference mode, tl0 and tl1 bitrates are piggybacked
1669 // on the VideoCodec struct as target and max bitrates, respectively.
1670 // See eg. webrtc::VP8EncoderImpl::SetRates().
1671 encoder_config.streams[0].target_bitrate_bps =
1672 config.tl0_bitrate_kbps * 1000;
1673 encoder_config.streams[0].max_bitrate_bps = config.tl1_bitrate_kbps * 1000;
pbos@webrtc.orgb7ed7792014-10-31 13:08:10 +00001674 encoder_config.streams[0].temporal_layer_thresholds_bps.clear();
1675 encoder_config.streams[0].temporal_layer_thresholds_bps.push_back(
sprang@webrtc.org46d4d292014-12-23 15:19:35 +00001676 config.tl0_bitrate_kbps * 1000);
pbos@webrtc.orgb7ed7792014-10-31 13:08:10 +00001677 }
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +00001678 return encoder_config;
1679}
1680
1681void WebRtcVideoChannel2::WebRtcVideoSendStream::SetDimensions(
1682 int width,
1683 int height,
1684 bool is_screencast) {
1685 if (last_dimensions_.width == width && last_dimensions_.height == height &&
1686 last_dimensions_.is_screencast == is_screencast) {
1687 // Configured using the same parameters, do not reconfigure.
1688 return;
1689 }
1690 LOG(LS_INFO) << "SetDimensions: " << width << "x" << height
1691 << (is_screencast ? " (screencast)" : " (not screencast)");
1692
1693 last_dimensions_.width = width;
1694 last_dimensions_.height = height;
1695 last_dimensions_.is_screencast = is_screencast;
1696
1697 assert(!parameters_.encoder_config.streams.empty());
1698
1699 VideoCodecSettings codec_settings;
1700 parameters_.codec_settings.Get(&codec_settings);
1701
1702 webrtc::VideoEncoderConfig encoder_config =
1703 CreateVideoEncoderConfig(last_dimensions_, codec_settings.codec);
1704
1705 encoder_config.encoder_specific_settings =
pbos@webrtc.orgf1c8b902015-01-14 17:29:27 +00001706 ConfigureVideoEncoderSettings(codec_settings.codec, parameters_.options);
pbos@webrtc.orgb7ed7792014-10-31 13:08:10 +00001707
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +00001708 bool stream_reconfigured = stream_->ReconfigureVideoEncoder(encoder_config);
1709
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +00001710 encoder_config.encoder_specific_settings = NULL;
pbos@webrtc.org6f48f1b2014-07-22 16:29:54 +00001711
1712 if (!stream_reconfigured) {
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001713 LOG(LS_WARNING) << "Failed to reconfigure video encoder for dimensions: "
1714 << width << "x" << height;
1715 return;
1716 }
pbos@webrtc.orgcddd17c2014-09-16 16:33:13 +00001717
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +00001718 parameters_.encoder_config = encoder_config;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001719}
1720
1721void WebRtcVideoChannel2::WebRtcVideoSendStream::Start() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001722 rtc::CritScope cs(&lock_);
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001723 assert(stream_ != NULL);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001724 stream_->Start();
1725 sending_ = true;
1726}
1727
1728void WebRtcVideoChannel2::WebRtcVideoSendStream::Stop() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001729 rtc::CritScope cs(&lock_);
pbos@webrtc.org5301b0f2014-07-17 08:51:46 +00001730 if (stream_ != NULL) {
1731 stream_->Stop();
1732 }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001733 sending_ = false;
1734}
1735
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00001736VideoSenderInfo
1737WebRtcVideoChannel2::WebRtcVideoSendStream::GetVideoSenderInfo() {
1738 VideoSenderInfo info;
pbos@webrtc.org77e11bb2015-02-23 16:39:07 +00001739 webrtc::VideoSendStream::Stats stats;
1740 {
1741 rtc::CritScope cs(&lock_);
1742 for (uint32_t ssrc : parameters_.config.rtp.ssrcs)
1743 info.add_ssrc(ssrc);
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00001744
pbos@webrtc.org77e11bb2015-02-23 16:39:07 +00001745 for (size_t i = 0; i < parameters_.encoder_config.streams.size(); ++i) {
1746 if (i == parameters_.encoder_config.streams.size() - 1) {
1747 info.preferred_bitrate +=
1748 parameters_.encoder_config.streams[i].max_bitrate_bps;
1749 } else {
1750 info.preferred_bitrate +=
1751 parameters_.encoder_config.streams[i].target_bitrate_bps;
1752 }
1753 }
pbos@webrtc.orgc3d2bd22014-08-12 20:55:10 +00001754
pbos@webrtc.org77e11bb2015-02-23 16:39:07 +00001755 if (stream_ == NULL)
1756 return info;
1757
1758 stats = stream_->GetStats();
1759
pbos@webrtc.org9a4410e2015-02-26 10:03:39 +00001760 info.adapt_changes = old_adapt_changes_;
1761 info.adapt_reason = CoordinatedVideoAdapter::ADAPTREASON_NONE;
1762
1763 if (capturer_ != NULL) {
1764 if (!capturer_->IsMuted()) {
1765 VideoFormat last_captured_frame_format;
1766 capturer_->GetStats(&info.adapt_frame_drops, &info.effects_frame_drops,
1767 &info.capturer_frame_time,
1768 &last_captured_frame_format);
1769 info.input_frame_width = last_captured_frame_format.width;
1770 info.input_frame_height = last_captured_frame_format.height;
1771 }
1772 if (capturer_->video_adapter() != nullptr) {
1773 info.adapt_changes += capturer_->video_adapter()->adaptation_changes();
1774 info.adapt_reason = capturer_->video_adapter()->adapt_reason();
1775 }
pbos@webrtc.org77e11bb2015-02-23 16:39:07 +00001776 }
1777 }
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00001778 info.framerate_input = stats.input_frame_rate;
1779 info.framerate_sent = stats.encode_frame_rate;
pbos@webrtc.org3e6e2712015-02-26 12:19:31 +00001780 info.avg_encode_ms = stats.avg_encode_time_ms;
1781 info.encode_usage_percent = stats.encode_usage_percent;
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00001782
pbos@webrtc.org77e11bb2015-02-23 16:39:07 +00001783 info.nominal_bitrate = stats.media_bitrate_bps;
1784
pbos@webrtc.org273a4142014-12-01 15:23:21 +00001785 info.send_frame_width = 0;
1786 info.send_frame_height = 0;
pbos@webrtc.org09c77b92015-02-25 10:42:16 +00001787 for (std::map<uint32_t, webrtc::VideoSendStream::StreamStats>::iterator it =
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00001788 stats.substreams.begin();
pbos@webrtc.org09c77b92015-02-25 10:42:16 +00001789 it != stats.substreams.end(); ++it) {
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00001790 // TODO(pbos): Wire up additional stats, such as padding bytes.
pbos@webrtc.org09c77b92015-02-25 10:42:16 +00001791 webrtc::VideoSendStream::StreamStats stream_stats = it->second;
asapersson@webrtc.orgcfd82df2015-01-22 09:39:59 +00001792 info.bytes_sent += stream_stats.rtp_stats.transmitted.payload_bytes +
1793 stream_stats.rtp_stats.transmitted.header_bytes +
1794 stream_stats.rtp_stats.transmitted.padding_bytes;
1795 info.packets_sent += stream_stats.rtp_stats.transmitted.packets;
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00001796 info.packets_lost += stream_stats.rtcp_stats.cumulative_lost;
pbos@webrtc.org09c77b92015-02-25 10:42:16 +00001797 if (stream_stats.width > info.send_frame_width)
1798 info.send_frame_width = stream_stats.width;
1799 if (stream_stats.height > info.send_frame_height)
1800 info.send_frame_height = stream_stats.height;
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +00001801 info.firs_rcvd += stream_stats.rtcp_packet_type_counts.fir_packets;
1802 info.nacks_rcvd += stream_stats.rtcp_packet_type_counts.nack_packets;
1803 info.plis_rcvd += stream_stats.rtcp_packet_type_counts.pli_packets;
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00001804 }
1805
1806 if (!stats.substreams.empty()) {
1807 // TODO(pbos): Report fraction lost per SSRC.
pbos@webrtc.org09c77b92015-02-25 10:42:16 +00001808 webrtc::VideoSendStream::StreamStats first_stream_stats =
1809 stats.substreams.begin()->second;
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00001810 info.fraction_lost =
1811 static_cast<float>(first_stream_stats.rtcp_stats.fraction_lost) /
1812 (1 << 8);
1813 }
1814
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00001815 return info;
1816}
1817
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001818void WebRtcVideoChannel2::WebRtcVideoSendStream::FillBandwidthEstimationInfo(
1819 BandwidthEstimationInfo* bwe_info) {
1820 rtc::CritScope cs(&lock_);
1821 if (stream_ == NULL) {
1822 return;
1823 }
1824 webrtc::VideoSendStream::Stats stats = stream_->GetStats();
pbos@webrtc.org09c77b92015-02-25 10:42:16 +00001825 for (std::map<uint32_t, webrtc::VideoSendStream::StreamStats>::iterator it =
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001826 stats.substreams.begin();
pbos@webrtc.org09c77b92015-02-25 10:42:16 +00001827 it != stats.substreams.end(); ++it) {
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001828 bwe_info->transmit_bitrate += it->second.total_bitrate_bps;
1829 bwe_info->retransmit_bitrate += it->second.retransmit_bitrate_bps;
1830 }
pbos@webrtc.org891d4832015-02-26 13:15:22 +00001831 bwe_info->target_enc_bitrate += stats.target_media_bitrate_bps;
pbos@webrtc.org77e11bb2015-02-23 16:39:07 +00001832 bwe_info->actual_enc_bitrate += stats.media_bitrate_bps;
stefan@webrtc.org0bae1fa2014-11-05 14:05:29 +00001833}
1834
pbos@webrtc.org42684be2014-10-03 11:25:45 +00001835void WebRtcVideoChannel2::WebRtcVideoSendStream::OnCpuResolutionRequest(
1836 CoordinatedVideoAdapter::AdaptRequest adapt_request) {
1837 rtc::CritScope cs(&lock_);
1838 bool adapt_cpu;
1839 parameters_.options.cpu_overuse_detection.Get(&adapt_cpu);
pbos@webrtc.org9a4410e2015-02-26 10:03:39 +00001840 if (!adapt_cpu)
pbos@webrtc.org42684be2014-10-03 11:25:45 +00001841 return;
pbos@webrtc.org9a4410e2015-02-26 10:03:39 +00001842 if (capturer_ == NULL || capturer_->video_adapter() == NULL)
pbos@webrtc.org42684be2014-10-03 11:25:45 +00001843 return;
pbos@webrtc.org42684be2014-10-03 11:25:45 +00001844
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.orgb9557a92015-03-20 19:52:56 +00001858 webrtc::VideoSendStream::Config config = parameters_.config;
1859 if (!config.rtp.rtx.ssrcs.empty() && config.rtp.rtx.payload_type == -1) {
1860 LOG(LS_WARNING) << "RTX SSRCs configured but there's no configured RTX "
1861 "payload type the set codec. Ignoring RTX.";
1862 config.rtp.rtx.ssrcs.clear();
1863 }
1864 stream_ = call_->CreateVideoSendStream(config, parameters_.encoder_config);
pbos@webrtc.org6f48f1b2014-07-22 16:29:54 +00001865
pbos@webrtc.orgbbe0a852014-09-19 12:30:25 +00001866 parameters_.encoder_config.encoder_specific_settings = NULL;
pbos@webrtc.org6f48f1b2014-07-22 16:29:54 +00001867
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001868 if (sending_) {
1869 stream_->Start();
1870 }
1871}
1872
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001873WebRtcVideoChannel2::WebRtcVideoReceiveStream::WebRtcVideoReceiveStream(
1874 webrtc::Call* call,
pbos@webrtc.org776e6f22014-10-29 15:28:39 +00001875 WebRtcVideoDecoderFactory* external_decoder_factory,
pbos@webrtc.orga2a6fe62015-03-06 15:35:19 +00001876 bool default_stream,
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001877 const webrtc::VideoReceiveStream::Config& config,
1878 const std::vector<VideoCodecSettings>& recv_codecs)
1879 : call_(call),
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001880 stream_(NULL),
pbos@webrtc.orga2a6fe62015-03-06 15:35:19 +00001881 default_stream_(default_stream),
pbos@webrtc.orgb648b9d2014-08-26 11:08:06 +00001882 config_(config),
pbos@webrtc.org776e6f22014-10-29 15:28:39 +00001883 external_decoder_factory_(external_decoder_factory),
pbos@webrtc.orgb648b9d2014-08-26 11:08:06 +00001884 renderer_(NULL),
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001885 last_width_(-1),
magjed@webrtc.orgfc5ad952015-01-27 09:57:01 +00001886 last_height_(-1),
1887 first_frame_timestamp_(-1),
1888 estimated_remote_start_ntp_time_ms_(0) {
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001889 config_.renderer = this;
1890 // SetRecvCodecs will also reset (start) the VideoReceiveStream.
1891 SetRecvCodecs(recv_codecs);
1892}
1893
1894WebRtcVideoChannel2::WebRtcVideoReceiveStream::~WebRtcVideoReceiveStream() {
1895 call_->DestroyVideoReceiveStream(stream_);
pbos@webrtc.org96a93252014-11-03 14:46:44 +00001896 ClearDecoders(&allocated_decoders_);
1897}
1898
1899WebRtcVideoChannel2::WebRtcVideoReceiveStream::AllocatedDecoder
1900WebRtcVideoChannel2::WebRtcVideoReceiveStream::CreateOrReuseVideoDecoder(
1901 std::vector<AllocatedDecoder>* old_decoders,
1902 const VideoCodec& codec) {
1903 webrtc::VideoCodecType type = CodecTypeFromName(codec.name);
1904
1905 for (size_t i = 0; i < old_decoders->size(); ++i) {
1906 if ((*old_decoders)[i].type == type) {
1907 AllocatedDecoder decoder = (*old_decoders)[i];
1908 (*old_decoders)[i] = old_decoders->back();
1909 old_decoders->pop_back();
1910 return decoder;
1911 }
1912 }
1913
1914 if (external_decoder_factory_ != NULL) {
1915 webrtc::VideoDecoder* decoder =
1916 external_decoder_factory_->CreateVideoDecoder(type);
1917 if (decoder != NULL) {
1918 return AllocatedDecoder(decoder, type, true);
1919 }
1920 }
1921
1922 if (type == webrtc::kVideoCodecVP8) {
1923 return AllocatedDecoder(
1924 webrtc::VideoDecoder::Create(webrtc::VideoDecoder::kVp8), type, false);
1925 }
1926
pbos@webrtc.orgb9557a92015-03-20 19:52:56 +00001927 if (type == webrtc::kVideoCodecVP9) {
1928 return AllocatedDecoder(
1929 webrtc::VideoDecoder::Create(webrtc::VideoDecoder::kVp9), type, false);
1930 }
1931
pbos@webrtc.org96a93252014-11-03 14:46:44 +00001932 // This shouldn't happen, we should not be trying to create something we don't
1933 // support.
1934 assert(false);
1935 return AllocatedDecoder(NULL, webrtc::kVideoCodecUnknown, false);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001936}
1937
1938void WebRtcVideoChannel2::WebRtcVideoReceiveStream::SetRecvCodecs(
1939 const std::vector<VideoCodecSettings>& recv_codecs) {
pbos@webrtc.org96a93252014-11-03 14:46:44 +00001940 std::vector<AllocatedDecoder> old_decoders = allocated_decoders_;
1941 allocated_decoders_.clear();
1942 config_.decoders.clear();
1943 for (size_t i = 0; i < recv_codecs.size(); ++i) {
1944 AllocatedDecoder allocated_decoder =
1945 CreateOrReuseVideoDecoder(&old_decoders, recv_codecs[i].codec);
1946 allocated_decoders_.push_back(allocated_decoder);
1947
1948 webrtc::VideoReceiveStream::Decoder decoder;
1949 decoder.decoder = allocated_decoder.decoder;
1950 decoder.payload_type = recv_codecs[i].codec.id;
1951 decoder.payload_name = recv_codecs[i].codec.name;
1952 config_.decoders.push_back(decoder);
1953 }
1954
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001955 // TODO(pbos): Reconfigure RTX based on incoming recv_codecs.
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001956 config_.rtp.fec = recv_codecs.front().fec;
pbos@webrtc.org257e1302014-07-25 19:01:32 +00001957 config_.rtp.nack.rtp_history_ms =
1958 IsNackEnabled(recv_codecs.begin()->codec) ? kNackHistoryMs : 0;
1959 config_.rtp.remb = IsRembEnabled(recv_codecs.begin()->codec);
1960
pbos@webrtc.org96a93252014-11-03 14:46:44 +00001961 ClearDecoders(&old_decoders);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001962 RecreateWebRtcStream();
1963}
1964
1965void WebRtcVideoChannel2::WebRtcVideoReceiveStream::SetRtpExtensions(
1966 const std::vector<webrtc::RtpExtension>& extensions) {
1967 config_.rtp.extensions = extensions;
1968 RecreateWebRtcStream();
1969}
1970
1971void WebRtcVideoChannel2::WebRtcVideoReceiveStream::RecreateWebRtcStream() {
1972 if (stream_ != NULL) {
1973 call_->DestroyVideoReceiveStream(stream_);
1974 }
1975 stream_ = call_->CreateVideoReceiveStream(config_);
1976 stream_->Start();
1977}
1978
pbos@webrtc.org96a93252014-11-03 14:46:44 +00001979void WebRtcVideoChannel2::WebRtcVideoReceiveStream::ClearDecoders(
1980 std::vector<AllocatedDecoder>* allocated_decoders) {
1981 for (size_t i = 0; i < allocated_decoders->size(); ++i) {
1982 if ((*allocated_decoders)[i].external) {
pbos@webrtc.org776e6f22014-10-29 15:28:39 +00001983 external_decoder_factory_->DestroyVideoDecoder(
pbos@webrtc.org96a93252014-11-03 14:46:44 +00001984 (*allocated_decoders)[i].decoder);
pbos@webrtc.org776e6f22014-10-29 15:28:39 +00001985 } else {
pbos@webrtc.org96a93252014-11-03 14:46:44 +00001986 delete (*allocated_decoders)[i].decoder;
pbos@webrtc.org776e6f22014-10-29 15:28:39 +00001987 }
1988 }
pbos@webrtc.org96a93252014-11-03 14:46:44 +00001989 allocated_decoders->clear();
pbos@webrtc.org776e6f22014-10-29 15:28:39 +00001990}
1991
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00001992void WebRtcVideoChannel2::WebRtcVideoReceiveStream::RenderFrame(
1993 const webrtc::I420VideoFrame& frame,
1994 int time_to_render_ms) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001995 rtc::CritScope crit(&renderer_lock_);
magjed@webrtc.orgfc5ad952015-01-27 09:57:01 +00001996
1997 if (first_frame_timestamp_ < 0)
1998 first_frame_timestamp_ = frame.timestamp();
1999 int64_t rtp_time_elapsed_since_first_frame =
2000 (timestamp_wraparound_handler_.Unwrap(frame.timestamp()) -
2001 first_frame_timestamp_);
2002 int64_t elapsed_time_ms = rtp_time_elapsed_since_first_frame /
2003 (cricket::kVideoCodecClockrate / 1000);
2004 if (frame.ntp_time_ms() > 0)
2005 estimated_remote_start_ntp_time_ms_ = frame.ntp_time_ms() - elapsed_time_ms;
2006
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00002007 if (renderer_ == NULL) {
2008 LOG(LS_WARNING) << "VideoReceiveStream not connected to a VideoRenderer.";
2009 return;
2010 }
2011
2012 if (frame.width() != last_width_ || frame.height() != last_height_) {
2013 SetSize(frame.width(), frame.height());
2014 }
2015
2016 LOG(LS_VERBOSE) << "RenderFrame: (" << frame.width() << "x" << frame.height()
2017 << ")";
2018
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +00002019 const WebRtcVideoFrame render_frame(
2020 frame.video_frame_buffer(),
2021 elapsed_time_ms * rtc::kNumNanosecsPerMillisec,
2022 frame.render_time_ms() * rtc::kNumNanosecsPerMillisec);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00002023 renderer_->RenderFrame(&render_frame);
2024}
2025
pbos@webrtc.org0d852d52015-02-09 15:14:36 +00002026bool WebRtcVideoChannel2::WebRtcVideoReceiveStream::IsTextureSupported() const {
2027 return true;
2028}
2029
pbos@webrtc.orga2a6fe62015-03-06 15:35:19 +00002030bool WebRtcVideoChannel2::WebRtcVideoReceiveStream::IsDefaultStream() const {
2031 return default_stream_;
2032}
2033
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00002034void WebRtcVideoChannel2::WebRtcVideoReceiveStream::SetRenderer(
2035 cricket::VideoRenderer* renderer) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002036 rtc::CritScope crit(&renderer_lock_);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00002037 renderer_ = renderer;
2038 if (renderer_ != NULL && last_width_ != -1) {
2039 SetSize(last_width_, last_height_);
2040 }
2041}
2042
2043VideoRenderer* WebRtcVideoChannel2::WebRtcVideoReceiveStream::GetRenderer() {
2044 // TODO(pbos): Remove GetRenderer and all uses of it, it's thread-unsafe by
2045 // design.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002046 rtc::CritScope crit(&renderer_lock_);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00002047 return renderer_;
2048}
2049
2050void WebRtcVideoChannel2::WebRtcVideoReceiveStream::SetSize(int width,
2051 int height) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002052 rtc::CritScope crit(&renderer_lock_);
pbos@webrtc.orgd1ea06b2014-07-18 09:35:58 +00002053 if (!renderer_->SetSize(width, height, 0)) {
2054 LOG(LS_ERROR) << "Could not set renderer size.";
2055 }
2056 last_width_ = width;
2057 last_height_ = height;
2058}
2059
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00002060VideoReceiverInfo
2061WebRtcVideoChannel2::WebRtcVideoReceiveStream::GetVideoReceiverInfo() {
2062 VideoReceiverInfo info;
2063 info.add_ssrc(config_.rtp.remote_ssrc);
2064 webrtc::VideoReceiveStream::Stats stats = stream_->GetStats();
asapersson@webrtc.orgcfd82df2015-01-22 09:39:59 +00002065 info.bytes_rcvd = stats.rtp_stats.transmitted.payload_bytes +
2066 stats.rtp_stats.transmitted.header_bytes +
2067 stats.rtp_stats.transmitted.padding_bytes;
2068 info.packets_rcvd = stats.rtp_stats.transmitted.packets;
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00002069
2070 info.framerate_rcvd = stats.network_frame_rate;
2071 info.framerate_decoded = stats.decode_frame_rate;
2072 info.framerate_output = stats.render_frame_rate;
2073
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +00002074 {
2075 rtc::CritScope frame_cs(&renderer_lock_);
2076 info.frame_width = last_width_;
2077 info.frame_height = last_height_;
2078 info.capture_start_ntp_time_ms = estimated_remote_start_ntp_time_ms_;
2079 }
2080
pbos@webrtc.org09c77b92015-02-25 10:42:16 +00002081 info.decode_ms = stats.decode_ms;
2082 info.max_decode_ms = stats.max_decode_ms;
2083 info.current_delay_ms = stats.current_delay_ms;
2084 info.target_delay_ms = stats.target_delay_ms;
2085 info.jitter_buffer_ms = stats.jitter_buffer_ms;
2086 info.min_playout_delay_ms = stats.min_playout_delay_ms;
2087 info.render_delay_ms = stats.render_delay_ms;
2088
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +00002089 info.firs_sent = stats.rtcp_packet_type_counts.fir_packets;
2090 info.plis_sent = stats.rtcp_packet_type_counts.pli_packets;
2091 info.nacks_sent = stats.rtcp_packet_type_counts.nack_packets;
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00002092
pbos@webrtc.orge6f84ae2014-07-18 11:11:55 +00002093 return info;
2094}
2095
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00002096WebRtcVideoChannel2::VideoCodecSettings::VideoCodecSettings()
2097 : rtx_payload_type(-1) {}
2098
pbos@webrtc.orga2ef4fe2014-11-07 10:54:43 +00002099bool WebRtcVideoChannel2::VideoCodecSettings::operator==(
2100 const WebRtcVideoChannel2::VideoCodecSettings& other) const {
2101 return codec == other.codec &&
2102 fec.ulpfec_payload_type == other.fec.ulpfec_payload_type &&
2103 fec.red_payload_type == other.fec.red_payload_type &&
2104 rtx_payload_type == other.rtx_payload_type;
2105}
2106
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00002107std::vector<WebRtcVideoChannel2::VideoCodecSettings>
2108WebRtcVideoChannel2::MapCodecs(const std::vector<VideoCodec>& codecs) {
2109 assert(!codecs.empty());
2110
2111 std::vector<VideoCodecSettings> video_codecs;
2112 std::map<int, bool> payload_used;
pbos@webrtc.orge322a172014-06-13 11:47:28 +00002113 std::map<int, VideoCodec::CodecType> payload_codec_type;
pkasting@chromium.orgd3245462015-02-23 21:28:22 +00002114 // |rtx_mapping| maps video payload type to rtx payload type.
2115 std::map<int, int> rtx_mapping;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00002116
2117 webrtc::FecConfig fec_settings;
2118
2119 for (size_t i = 0; i < codecs.size(); ++i) {
2120 const VideoCodec& in_codec = codecs[i];
2121 int payload_type = in_codec.id;
2122
2123 if (payload_used[payload_type]) {
2124 LOG(LS_ERROR) << "Payload type already registered: "
2125 << in_codec.ToString();
2126 return std::vector<VideoCodecSettings>();
2127 }
2128 payload_used[payload_type] = true;
pbos@webrtc.orge322a172014-06-13 11:47:28 +00002129 payload_codec_type[payload_type] = in_codec.GetCodecType();
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00002130
2131 switch (in_codec.GetCodecType()) {
2132 case VideoCodec::CODEC_RED: {
2133 // RED payload type, should not have duplicates.
2134 assert(fec_settings.red_payload_type == -1);
2135 fec_settings.red_payload_type = in_codec.id;
2136 continue;
2137 }
2138
2139 case VideoCodec::CODEC_ULPFEC: {
2140 // ULPFEC payload type, should not have duplicates.
2141 assert(fec_settings.ulpfec_payload_type == -1);
2142 fec_settings.ulpfec_payload_type = in_codec.id;
2143 continue;
2144 }
2145
2146 case VideoCodec::CODEC_RTX: {
2147 int associated_payload_type;
2148 if (!in_codec.GetParam(kCodecParamAssociatedPayloadType,
pkasting@chromium.orge9facf82015-02-17 20:36:28 +00002149 &associated_payload_type) ||
2150 !IsValidRtpPayloadType(associated_payload_type)) {
2151 LOG(LS_ERROR)
2152 << "RTX codec with invalid or no associated payload type: "
2153 << in_codec.ToString();
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00002154 return std::vector<VideoCodecSettings>();
2155 }
2156 rtx_mapping[associated_payload_type] = in_codec.id;
2157 continue;
2158 }
2159
2160 case VideoCodec::CODEC_VIDEO:
2161 break;
2162 }
2163
2164 video_codecs.push_back(VideoCodecSettings());
2165 video_codecs.back().codec = in_codec;
2166 }
2167
2168 // One of these codecs should have been a video codec. Only having FEC
2169 // parameters into this code is a logic error.
2170 assert(!video_codecs.empty());
2171
pbos@webrtc.orge322a172014-06-13 11:47:28 +00002172 for (std::map<int, int>::const_iterator it = rtx_mapping.begin();
2173 it != rtx_mapping.end();
2174 ++it) {
2175 if (!payload_used[it->first]) {
2176 LOG(LS_ERROR) << "RTX mapped to payload not in codec list.";
2177 return std::vector<VideoCodecSettings>();
2178 }
andrew@webrtc.org8f27fcc2015-01-09 20:22:46 +00002179 if (payload_codec_type[it->first] != VideoCodec::CODEC_VIDEO) {
2180 LOG(LS_ERROR) << "RTX not mapped to regular video codec.";
pbos@webrtc.orge322a172014-06-13 11:47:28 +00002181 return std::vector<VideoCodecSettings>();
2182 }
2183 }
2184
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00002185 // TODO(pbos): Write tests that figure out that I have not verified that RTX
2186 // codecs aren't mapped to bogus payloads.
2187 for (size_t i = 0; i < video_codecs.size(); ++i) {
2188 video_codecs[i].fec = fec_settings;
andrew@webrtc.org8f27fcc2015-01-09 20:22:46 +00002189 if (rtx_mapping[video_codecs[i].codec.id] != 0) {
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00002190 video_codecs[i].rtx_payload_type = rtx_mapping[video_codecs[i].codec.id];
2191 }
2192 }
2193
2194 return video_codecs;
2195}
2196
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00002197} // namespace cricket
2198
2199#endif // HAVE_WEBRTC_VIDEO