blob: 54a5201e4765e9e107648040ea731761e2c37695 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
2 * libjingle
3 * Copyright 2004 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/webrtcvideoengine.h"
30
31#ifdef HAVE_CONFIG_H
32#include <config.h>
33#endif
34
35#include <math.h>
36#include <set>
37
38#include "talk/base/basictypes.h"
39#include "talk/base/buffer.h"
40#include "talk/base/byteorder.h"
41#include "talk/base/common.h"
42#include "talk/base/cpumonitor.h"
43#include "talk/base/logging.h"
44#include "talk/base/stringutils.h"
45#include "talk/base/thread.h"
46#include "talk/base/timeutils.h"
47#include "talk/media/base/constants.h"
48#include "talk/media/base/rtputils.h"
49#include "talk/media/base/streamparams.h"
50#include "talk/media/base/videoadapter.h"
51#include "talk/media/base/videocapturer.h"
52#include "talk/media/base/videorenderer.h"
53#include "talk/media/devices/filevideocapturer.h"
wu@webrtc.org9dba5252013-08-05 20:36:57 +000054#include "talk/media/webrtc/webrtcpassthroughrender.h"
55#include "talk/media/webrtc/webrtctexturevideoframe.h"
56#include "talk/media/webrtc/webrtcvideocapturer.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000057#include "talk/media/webrtc/webrtcvideodecoderfactory.h"
58#include "talk/media/webrtc/webrtcvideoencoderfactory.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000059#include "talk/media/webrtc/webrtcvideoframe.h"
60#include "talk/media/webrtc/webrtcvie.h"
61#include "talk/media/webrtc/webrtcvoe.h"
62#include "talk/media/webrtc/webrtcvoiceengine.h"
henrike@webrtc.orga92fd742014-03-26 01:46:18 +000063#include "webrtc/experiments.h"
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +000064#include "webrtc/modules/remote_bitrate_estimator/include/remote_bitrate_estimator.h"
buildbot@webrtc.org34a08b42014-06-02 15:48:10 +000065#include "webrtc/system_wrappers/interface/field_trial.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000066
67#if !defined(LIBPEERCONNECTION_LIB)
henrike@webrtc.org28e20752013-07-10 00:45:36 +000068#include "talk/media/webrtc/webrtcmediaengine.h"
69
70WRME_EXPORT
71cricket::MediaEngineInterface* CreateWebRtcMediaEngine(
72 webrtc::AudioDeviceModule* adm, webrtc::AudioDeviceModule* adm_sc,
73 cricket::WebRtcVideoEncoderFactory* encoder_factory,
74 cricket::WebRtcVideoDecoderFactory* decoder_factory) {
buildbot@webrtc.org34a08b42014-06-02 15:48:10 +000075#ifdef WEBRTC_CHROMIUM_BUILD
76 if (webrtc::field_trial::FindFullName("WebRTC-NewVideoAPI") == "Enabled") {
77 return new cricket::WebRtcMediaEngine2(
78 adm, adm_sc, encoder_factory, decoder_factory);
79 } else {
80#endif
81 return new cricket::WebRtcMediaEngine(
82 adm, adm_sc, encoder_factory, decoder_factory);
83#ifdef WEBRTC_CHROMIUM_BUILD
84 }
85#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +000086}
87
88WRME_EXPORT
89void DestroyWebRtcMediaEngine(cricket::MediaEngineInterface* media_engine) {
buildbot@webrtc.org34a08b42014-06-02 15:48:10 +000090#ifdef WEBRTC_CHROMIUM_BUILD
91 if (webrtc::field_trial::FindFullName("WebRTC-NewVideoAPI") == "Enabled") {
92 delete static_cast<cricket::WebRtcMediaEngine2*>(media_engine);
93 } else {
94#endif
95 delete static_cast<cricket::WebRtcMediaEngine*>(media_engine);
96#ifdef WEBRTC_CHROMIUM_BUILD
97 }
98#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +000099}
100#endif
101
102
103namespace cricket {
104
105
106static const int kDefaultLogSeverity = talk_base::LS_WARNING;
107
108static const int kMinVideoBitrate = 50;
109static const int kStartVideoBitrate = 300;
110static const int kMaxVideoBitrate = 2000;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000111
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000112// Controlled by exp, try a super low minimum bitrate for poor connections.
113static const int kLowerMinBitrate = 30;
114
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000115static const int kVideoMtu = 1200;
116
117static const int kVideoRtpBufferSize = 65536;
118
119static const char kVp8PayloadName[] = "VP8";
120static const char kRedPayloadName[] = "red";
121static const char kFecPayloadName[] = "ulpfec";
122
123static const int kDefaultNumberOfTemporalLayers = 1; // 1:1
124
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000125static const int kExternalVideoPayloadTypeBase = 120;
126
buildbot@webrtc.org073dfdd2014-05-08 19:36:21 +0000127static bool BitrateIsSet(int value) {
buildbot@webrtc.orgd1ae89f2014-05-08 19:19:26 +0000128 return value > kAutoBandwidth;
129}
130
buildbot@webrtc.org073dfdd2014-05-08 19:36:21 +0000131static int GetBitrate(int value, int deflt) {
buildbot@webrtc.orgd1ae89f2014-05-08 19:19:26 +0000132 return BitrateIsSet(value) ? value : deflt;
133}
134
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000135// Static allocation of payload type values for external video codec.
136static int GetExternalVideoPayloadType(int index) {
buildbot@webrtc.org34a08b42014-06-02 15:48:10 +0000137#if ENABLE_DEBUG
138 static const int kMaxExternalVideoCodecs = 8;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000139 ASSERT(index >= 0 && index < kMaxExternalVideoCodecs);
buildbot@webrtc.org34a08b42014-06-02 15:48:10 +0000140#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000141 return kExternalVideoPayloadTypeBase + index;
142}
143
144static void LogMultiline(talk_base::LoggingSeverity sev, char* text) {
145 const char* delim = "\r\n";
146 // TODO(fbarchard): Fix strtok lint warning.
147 for (char* tok = strtok(text, delim); tok; tok = strtok(NULL, delim)) {
148 LOG_V(sev) << tok;
149 }
150}
151
152// Severity is an integer because it comes is assumed to be from command line.
153static int SeverityToFilter(int severity) {
154 int filter = webrtc::kTraceNone;
155 switch (severity) {
156 case talk_base::LS_VERBOSE:
157 filter |= webrtc::kTraceAll;
158 case talk_base::LS_INFO:
159 filter |= (webrtc::kTraceStateInfo | webrtc::kTraceInfo);
160 case talk_base::LS_WARNING:
161 filter |= (webrtc::kTraceTerseInfo | webrtc::kTraceWarning);
162 case talk_base::LS_ERROR:
163 filter |= (webrtc::kTraceError | webrtc::kTraceCritical);
164 }
165 return filter;
166}
167
168static const int kCpuMonitorPeriodMs = 2000; // 2 seconds.
169
170static const bool kNotSending = false;
171
wu@webrtc.orgde305012013-10-31 15:40:38 +0000172// Default video dscp value.
173// See http://tools.ietf.org/html/rfc2474 for details
174// See also http://tools.ietf.org/html/draft-jennings-rtcweb-qos-00
175static const talk_base::DiffServCodePoint kVideoDscpValue =
176 talk_base::DSCP_AF41;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000177
178static bool IsNackEnabled(const VideoCodec& codec) {
179 return codec.HasFeedbackParam(FeedbackParam(kRtcpFbParamNack,
180 kParamValueEmpty));
181}
182
183// Returns true if Receiver Estimated Max Bitrate is enabled.
184static bool IsRembEnabled(const VideoCodec& codec) {
185 return codec.HasFeedbackParam(FeedbackParam(kRtcpFbParamRemb,
186 kParamValueEmpty));
187}
188
189struct FlushBlackFrameData : public talk_base::MessageData {
190 FlushBlackFrameData(uint32 s, int64 t) : ssrc(s), timestamp(t) {
191 }
192 uint32 ssrc;
193 int64 timestamp;
194};
195
196class WebRtcRenderAdapter : public webrtc::ExternalRenderer {
197 public:
buildbot@webrtc.orgf875f152014-04-14 16:06:21 +0000198 WebRtcRenderAdapter(VideoRenderer* renderer, int channel_id)
buildbot@webrtc.org0581f0b2014-05-06 21:36:31 +0000199 : renderer_(renderer),
200 channel_id_(channel_id),
201 width_(0),
202 height_(0),
buildbot@webrtc.orgf9f1bfb2014-05-21 17:02:15 +0000203 capture_start_rtp_time_stamp_(-1),
buildbot@webrtc.org0581f0b2014-05-06 21:36:31 +0000204 capture_start_ntp_time_ms_(0) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000205 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000206
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000207 virtual ~WebRtcRenderAdapter() {
208 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000209
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000210 void SetRenderer(VideoRenderer* renderer) {
211 talk_base::CritScope cs(&crit_);
212 renderer_ = renderer;
213 // FrameSizeChange may have already been called when renderer was not set.
214 // If so we should call SetSize here.
215 // TODO(ronghuawu): Add unit test for this case. Didn't do it now
216 // because the WebRtcRenderAdapter is currently hiding in cc file. No
217 // good way to get access to it from the unit test.
218 if (width_ > 0 && height_ > 0 && renderer_ != NULL) {
219 if (!renderer_->SetSize(width_, height_, 0)) {
220 LOG(LS_ERROR)
buildbot@webrtc.orgf875f152014-04-14 16:06:21 +0000221 << "WebRtcRenderAdapter (channel " << channel_id_
222 << ") SetRenderer failed to SetSize to: "
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000223 << width_ << "x" << height_;
224 }
225 }
226 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000227
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000228 // Implementation of webrtc::ExternalRenderer.
229 virtual int FrameSizeChange(unsigned int width, unsigned int height,
230 unsigned int /*number_of_streams*/) {
231 talk_base::CritScope cs(&crit_);
232 width_ = width;
233 height_ = height;
buildbot@webrtc.orgf875f152014-04-14 16:06:21 +0000234 LOG(LS_INFO) << "WebRtcRenderAdapter (channel " << channel_id_
235 << ") frame size changed to: "
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000236 << width << "x" << height;
237 if (renderer_ == NULL) {
buildbot@webrtc.orgf875f152014-04-14 16:06:21 +0000238 LOG(LS_VERBOSE) << "WebRtcRenderAdapter (channel " << channel_id_
239 << ") the renderer has not been set. "
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000240 << "SetSize will be called later in SetRenderer.";
241 return 0;
242 }
243 return renderer_->SetSize(width_, height_, 0) ? 0 : -1;
244 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000245
buildbot@webrtc.org1fd5b452014-04-15 17:39:43 +0000246 virtual int DeliverFrame(unsigned char* buffer,
247 int buffer_size,
buildbot@webrtc.org0581f0b2014-05-06 21:36:31 +0000248 uint32_t rtp_time_stamp,
buildbot@webrtc.org1fd5b452014-04-15 17:39:43 +0000249#ifdef USE_WEBRTC_DEV_BRANCH
250 int64_t ntp_time_ms,
251#endif
252 int64_t render_time,
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000253 void* handle) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000254 talk_base::CritScope cs(&crit_);
buildbot@webrtc.orgf9f1bfb2014-05-21 17:02:15 +0000255 if (capture_start_rtp_time_stamp_ < 0) {
buildbot@webrtc.org0581f0b2014-05-06 21:36:31 +0000256 capture_start_rtp_time_stamp_ = rtp_time_stamp;
257 }
buildbot@webrtc.org22190372014-05-07 17:52:33 +0000258
259 const int kVideoCodecClockratekHz = cricket::kVideoCodecClockrate / 1000;
260
buildbot@webrtc.org0581f0b2014-05-06 21:36:31 +0000261#ifdef USE_WEBRTC_DEV_BRANCH
262 if (ntp_time_ms > 0) {
buildbot@webrtc.orgf9f1bfb2014-05-21 17:02:15 +0000263 int64 elapsed_time_ms =
264 (rtp_ts_wraparound_handler_.Unwrap(rtp_time_stamp) -
265 capture_start_rtp_time_stamp_) / kVideoCodecClockratekHz;
buildbot@webrtc.org0581f0b2014-05-06 21:36:31 +0000266 capture_start_ntp_time_ms_ = ntp_time_ms - elapsed_time_ms;
267 }
268#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000269 frame_rate_tracker_.Update(1);
270 if (renderer_ == NULL) {
271 return 0;
272 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000273 // Convert 90K rtp timestamp to ns timestamp.
buildbot@webrtc.org0581f0b2014-05-06 21:36:31 +0000274 int64 rtp_time_stamp_in_ns = (rtp_time_stamp / kVideoCodecClockratekHz) *
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000275 talk_base::kNumNanosecsPerMillisec;
276 // Convert milisecond render time to ns timestamp.
277 int64 render_time_stamp_in_ns = render_time *
278 talk_base::kNumNanosecsPerMillisec;
279 // Send the rtp timestamp to renderer as the VideoFrame timestamp.
280 // and the render timestamp as the VideoFrame elapsed_time.
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000281 if (handle == NULL) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000282 return DeliverBufferFrame(buffer, buffer_size, render_time_stamp_in_ns,
buildbot@webrtc.org740e6b32014-04-30 15:33:45 +0000283 rtp_time_stamp_in_ns);
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000284 } else {
285 return DeliverTextureFrame(handle, render_time_stamp_in_ns,
buildbot@webrtc.org740e6b32014-04-30 15:33:45 +0000286 rtp_time_stamp_in_ns);
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000287 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000288 }
289
290 virtual bool IsTextureSupported() { return true; }
291
292 int DeliverBufferFrame(unsigned char* buffer, int buffer_size,
buildbot@webrtc.org0581f0b2014-05-06 21:36:31 +0000293 int64 elapsed_time, int64 rtp_time_stamp_in_ns) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000294 WebRtcVideoFrame video_frame;
wu@webrtc.org16d62542013-11-05 23:45:14 +0000295 video_frame.Alias(buffer, buffer_size, width_, height_,
buildbot@webrtc.org0581f0b2014-05-06 21:36:31 +0000296 1, 1, elapsed_time, rtp_time_stamp_in_ns, 0);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000297
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000298 // Sanity check on decoded frame size.
299 if (buffer_size != static_cast<int>(VideoFrame::SizeOf(width_, height_))) {
buildbot@webrtc.orgf875f152014-04-14 16:06:21 +0000300 LOG(LS_WARNING) << "WebRtcRenderAdapter (channel " << channel_id_
301 << ") received a strange frame size: "
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000302 << buffer_size;
303 }
304
305 int ret = renderer_->RenderFrame(&video_frame) ? 0 : -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000306 return ret;
307 }
308
buildbot@webrtc.org0581f0b2014-05-06 21:36:31 +0000309 int DeliverTextureFrame(void* handle,
310 int64 elapsed_time,
311 int64 rtp_time_stamp_in_ns) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000312 WebRtcTextureVideoFrame video_frame(
313 static_cast<webrtc::NativeHandle*>(handle), width_, height_,
buildbot@webrtc.org0581f0b2014-05-06 21:36:31 +0000314 elapsed_time, rtp_time_stamp_in_ns);
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000315 return renderer_->RenderFrame(&video_frame);
316 }
317
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000318 unsigned int width() {
319 talk_base::CritScope cs(&crit_);
320 return width_;
321 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000322
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000323 unsigned int height() {
324 talk_base::CritScope cs(&crit_);
325 return height_;
326 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000327
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000328 int framerate() {
329 talk_base::CritScope cs(&crit_);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000330 return static_cast<int>(frame_rate_tracker_.units_second());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000331 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000332
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000333 VideoRenderer* renderer() {
334 talk_base::CritScope cs(&crit_);
335 return renderer_;
336 }
337
buildbot@webrtc.org0581f0b2014-05-06 21:36:31 +0000338 int64 capture_start_ntp_time_ms() {
339 talk_base::CritScope cs(&crit_);
340 return capture_start_ntp_time_ms_;
341 }
342
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000343 private:
344 talk_base::CriticalSection crit_;
345 VideoRenderer* renderer_;
buildbot@webrtc.orgf875f152014-04-14 16:06:21 +0000346 int channel_id_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000347 unsigned int width_;
348 unsigned int height_;
349 talk_base::RateTracker frame_rate_tracker_;
buildbot@webrtc.orgf9f1bfb2014-05-21 17:02:15 +0000350 talk_base::TimestampWrapAroundHandler rtp_ts_wraparound_handler_;
351 int64 capture_start_rtp_time_stamp_;
buildbot@webrtc.org0581f0b2014-05-06 21:36:31 +0000352 int64 capture_start_ntp_time_ms_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000353};
354
355class WebRtcDecoderObserver : public webrtc::ViEDecoderObserver {
356 public:
357 explicit WebRtcDecoderObserver(int video_channel)
358 : video_channel_(video_channel),
359 framerate_(0),
360 bitrate_(0),
wu@webrtc.org97077a32013-10-25 21:18:33 +0000361 decode_ms_(0),
362 max_decode_ms_(0),
363 current_delay_ms_(0),
364 target_delay_ms_(0),
365 jitter_buffer_ms_(0),
366 min_playout_delay_ms_(0),
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +0000367 render_delay_ms_(0) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000368 }
369
370 // virtual functions from VieDecoderObserver.
371 virtual void IncomingCodecChanged(const int videoChannel,
372 const webrtc::VideoCodec& videoCodec) {}
373 virtual void IncomingRate(const int videoChannel,
374 const unsigned int framerate,
375 const unsigned int bitrate) {
wu@webrtc.org78187522013-10-07 23:32:02 +0000376 talk_base::CritScope cs(&crit_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000377 ASSERT(video_channel_ == videoChannel);
378 framerate_ = framerate;
379 bitrate_ = bitrate;
380 }
wu@webrtc.org97077a32013-10-25 21:18:33 +0000381
382 virtual void DecoderTiming(int decode_ms,
383 int max_decode_ms,
384 int current_delay_ms,
385 int target_delay_ms,
386 int jitter_buffer_ms,
387 int min_playout_delay_ms,
388 int render_delay_ms) {
389 talk_base::CritScope cs(&crit_);
390 decode_ms_ = decode_ms;
391 max_decode_ms_ = max_decode_ms;
392 current_delay_ms_ = current_delay_ms;
393 target_delay_ms_ = target_delay_ms;
394 jitter_buffer_ms_ = jitter_buffer_ms;
395 min_playout_delay_ms_ = min_playout_delay_ms;
396 render_delay_ms_ = render_delay_ms;
397 }
398
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +0000399 virtual void RequestNewKeyFrame(const int videoChannel) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000400
wu@webrtc.org97077a32013-10-25 21:18:33 +0000401 // Populate |rinfo| based on previously-set data in |*this|.
402 void ExportTo(VideoReceiverInfo* rinfo) {
wu@webrtc.org78187522013-10-07 23:32:02 +0000403 talk_base::CritScope cs(&crit_);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000404 rinfo->framerate_rcvd = framerate_;
405 rinfo->decode_ms = decode_ms_;
406 rinfo->max_decode_ms = max_decode_ms_;
407 rinfo->current_delay_ms = current_delay_ms_;
408 rinfo->target_delay_ms = target_delay_ms_;
409 rinfo->jitter_buffer_ms = jitter_buffer_ms_;
410 rinfo->min_playout_delay_ms = min_playout_delay_ms_;
411 rinfo->render_delay_ms = render_delay_ms_;
wu@webrtc.org78187522013-10-07 23:32:02 +0000412 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000413
414 private:
wu@webrtc.org78187522013-10-07 23:32:02 +0000415 mutable talk_base::CriticalSection crit_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000416 int video_channel_;
417 int framerate_;
418 int bitrate_;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000419 int decode_ms_;
420 int max_decode_ms_;
421 int current_delay_ms_;
422 int target_delay_ms_;
423 int jitter_buffer_ms_;
424 int min_playout_delay_ms_;
425 int render_delay_ms_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000426};
427
428class WebRtcEncoderObserver : public webrtc::ViEEncoderObserver {
429 public:
430 explicit WebRtcEncoderObserver(int video_channel)
431 : video_channel_(video_channel),
432 framerate_(0),
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000433 bitrate_(0),
434 suspended_(false) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000435 }
436
437 // virtual functions from VieEncoderObserver.
438 virtual void OutgoingRate(const int videoChannel,
439 const unsigned int framerate,
440 const unsigned int bitrate) {
wu@webrtc.org78187522013-10-07 23:32:02 +0000441 talk_base::CritScope cs(&crit_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000442 ASSERT(video_channel_ == videoChannel);
443 framerate_ = framerate;
444 bitrate_ = bitrate;
445 }
446
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000447 virtual void SuspendChange(int video_channel, bool is_suspended) {
448 talk_base::CritScope cs(&crit_);
449 ASSERT(video_channel_ == video_channel);
450 suspended_ = is_suspended;
451 }
452
wu@webrtc.org78187522013-10-07 23:32:02 +0000453 int framerate() const {
454 talk_base::CritScope cs(&crit_);
455 return framerate_;
456 }
457 int bitrate() const {
458 talk_base::CritScope cs(&crit_);
459 return bitrate_;
460 }
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000461 bool suspended() const {
462 talk_base::CritScope cs(&crit_);
463 return suspended_;
464 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000465
466 private:
wu@webrtc.org78187522013-10-07 23:32:02 +0000467 mutable talk_base::CriticalSection crit_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000468 int video_channel_;
469 int framerate_;
470 int bitrate_;
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000471 bool suspended_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000472};
473
474class WebRtcLocalStreamInfo {
475 public:
476 WebRtcLocalStreamInfo()
477 : width_(0), height_(0), elapsed_time_(-1), time_stamp_(-1) {}
478 size_t width() const {
479 talk_base::CritScope cs(&crit_);
480 return width_;
481 }
482 size_t height() const {
483 talk_base::CritScope cs(&crit_);
484 return height_;
485 }
486 int64 elapsed_time() const {
487 talk_base::CritScope cs(&crit_);
488 return elapsed_time_;
489 }
490 int64 time_stamp() const {
491 talk_base::CritScope cs(&crit_);
492 return time_stamp_;
493 }
494 int framerate() {
495 talk_base::CritScope cs(&crit_);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000496 return static_cast<int>(rate_tracker_.units_second());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000497 }
498 void GetLastFrameInfo(
499 size_t* width, size_t* height, int64* elapsed_time) const {
500 talk_base::CritScope cs(&crit_);
501 *width = width_;
502 *height = height_;
503 *elapsed_time = elapsed_time_;
504 }
505
506 void UpdateFrame(const VideoFrame* frame) {
507 talk_base::CritScope cs(&crit_);
508
509 width_ = frame->GetWidth();
510 height_ = frame->GetHeight();
511 elapsed_time_ = frame->GetElapsedTime();
512 time_stamp_ = frame->GetTimeStamp();
513
514 rate_tracker_.Update(1);
515 }
516
517 private:
518 mutable talk_base::CriticalSection crit_;
519 size_t width_;
520 size_t height_;
521 int64 elapsed_time_;
522 int64 time_stamp_;
523 talk_base::RateTracker rate_tracker_;
524
525 DISALLOW_COPY_AND_ASSIGN(WebRtcLocalStreamInfo);
526};
527
528// WebRtcVideoChannelRecvInfo is a container class with members such as renderer
529// and a decoder observer that is used by receive channels.
530// It must exist as long as the receive channel is connected to renderer or a
531// decoder observer in this class and methods in the class should only be called
532// from the worker thread.
533class WebRtcVideoChannelRecvInfo {
534 public:
535 typedef std::map<int, webrtc::VideoDecoder*> DecoderMap; // key: payload type
536 explicit WebRtcVideoChannelRecvInfo(int channel_id)
537 : channel_id_(channel_id),
buildbot@webrtc.orgf875f152014-04-14 16:06:21 +0000538 render_adapter_(NULL, channel_id),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000539 decoder_observer_(channel_id) {
540 }
541 int channel_id() { return channel_id_; }
542 void SetRenderer(VideoRenderer* renderer) {
543 render_adapter_.SetRenderer(renderer);
544 }
545 WebRtcRenderAdapter* render_adapter() { return &render_adapter_; }
546 WebRtcDecoderObserver* decoder_observer() { return &decoder_observer_; }
547 void RegisterDecoder(int pl_type, webrtc::VideoDecoder* decoder) {
548 ASSERT(!IsDecoderRegistered(pl_type));
549 registered_decoders_[pl_type] = decoder;
550 }
551 bool IsDecoderRegistered(int pl_type) {
552 return registered_decoders_.count(pl_type) != 0;
553 }
554 const DecoderMap& registered_decoders() {
555 return registered_decoders_;
556 }
557 void ClearRegisteredDecoders() {
558 registered_decoders_.clear();
559 }
560
561 private:
562 int channel_id_; // Webrtc video channel number.
563 // Renderer for this channel.
564 WebRtcRenderAdapter render_adapter_;
565 WebRtcDecoderObserver decoder_observer_;
566 DecoderMap registered_decoders_;
567};
568
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000569class WebRtcOveruseObserver : public webrtc::CpuOveruseObserver {
570 public:
571 explicit WebRtcOveruseObserver(CoordinatedVideoAdapter* video_adapter)
572 : video_adapter_(video_adapter),
573 enabled_(false) {
574 }
575
576 // TODO(mflodman): Consider sending resolution as part of event, to let
577 // adapter know what resolution the request is based on. Helps eliminate stale
578 // data, race conditions.
579 virtual void OveruseDetected() OVERRIDE {
580 talk_base::CritScope cs(&crit_);
581 if (!enabled_) {
582 return;
583 }
584
585 video_adapter_->OnCpuResolutionRequest(CoordinatedVideoAdapter::DOWNGRADE);
586 }
587
588 virtual void NormalUsage() OVERRIDE {
589 talk_base::CritScope cs(&crit_);
590 if (!enabled_) {
591 return;
592 }
593
594 video_adapter_->OnCpuResolutionRequest(CoordinatedVideoAdapter::UPGRADE);
595 }
596
597 void Enable(bool enable) {
598 talk_base::CritScope cs(&crit_);
599 enabled_ = enable;
600 }
601
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000602 bool enabled() const { return enabled_; }
603
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000604 private:
605 CoordinatedVideoAdapter* video_adapter_;
606 bool enabled_;
607 talk_base::CriticalSection crit_;
608};
609
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000610
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000611class WebRtcVideoChannelSendInfo : public sigslot::has_slots<> {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000612 public:
613 typedef std::map<int, webrtc::VideoEncoder*> EncoderMap; // key: payload type
614 WebRtcVideoChannelSendInfo(int channel_id, int capture_id,
615 webrtc::ViEExternalCapture* external_capture,
616 talk_base::CpuMonitor* cpu_monitor)
617 : channel_id_(channel_id),
618 capture_id_(capture_id),
619 sending_(false),
620 muted_(false),
621 video_capturer_(NULL),
622 encoder_observer_(channel_id),
623 external_capture_(external_capture),
624 capturer_updated_(false),
625 interval_(0),
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000626 cpu_monitor_(cpu_monitor),
627 overuse_observer_enabled_(false) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000628 }
629
630 int channel_id() const { return channel_id_; }
631 int capture_id() const { return capture_id_; }
632 void set_sending(bool sending) { sending_ = sending; }
633 bool sending() const { return sending_; }
634 void set_muted(bool on) {
635 // TODO(asapersson): add support.
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000636 // video_adapter_.SetBlackOutput(on);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000637 muted_ = on;
638 }
639 bool muted() {return muted_; }
640
641 WebRtcEncoderObserver* encoder_observer() { return &encoder_observer_; }
642 webrtc::ViEExternalCapture* external_capture() { return external_capture_; }
643 const VideoFormat& video_format() const {
644 return video_format_;
645 }
646 void set_video_format(const VideoFormat& video_format) {
647 video_format_ = video_format;
648 if (video_format_ != cricket::VideoFormat()) {
649 interval_ = video_format_.interval;
650 }
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000651 CoordinatedVideoAdapter* adapter = video_adapter();
652 if (adapter) {
653 adapter->OnOutputFormatRequest(video_format_);
654 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000655 }
656 void set_interval(int64 interval) {
657 if (video_format() == cricket::VideoFormat()) {
658 interval_ = interval;
659 }
660 }
661 int64 interval() { return interval_; }
662
xians@webrtc.orgef221512014-02-21 10:31:29 +0000663 int CurrentAdaptReason() const {
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000664 const CoordinatedVideoAdapter* adapter = video_adapter();
665 if (!adapter) {
666 return CoordinatedVideoAdapter::ADAPTREASON_NONE;
667 }
668 return video_adapter()->adapt_reason();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000669 }
670
671 StreamParams* stream_params() { return stream_params_.get(); }
672 void set_stream_params(const StreamParams& sp) {
673 stream_params_.reset(new StreamParams(sp));
674 }
675 void ClearStreamParams() { stream_params_.reset(); }
676 bool has_ssrc(uint32 local_ssrc) const {
677 return !stream_params_ ? false :
678 stream_params_->has_ssrc(local_ssrc);
679 }
680 WebRtcLocalStreamInfo* local_stream_info() {
681 return &local_stream_info_;
682 }
683 VideoCapturer* video_capturer() {
684 return video_capturer_;
685 }
henrike@webrtc.orgc7bec842014-03-12 19:53:43 +0000686 void set_video_capturer(VideoCapturer* video_capturer,
687 ViEWrapper* vie_wrapper) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000688 if (video_capturer == video_capturer_) {
689 return;
690 }
xians@webrtc.orgef221512014-02-21 10:31:29 +0000691
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000692 CoordinatedVideoAdapter* old_video_adapter = video_adapter();
693 if (old_video_adapter) {
694 // Disconnect signals from old video adapter.
695 SignalCpuAdaptationUnable.disconnect(old_video_adapter);
696 if (cpu_monitor_) {
697 cpu_monitor_->SignalUpdate.disconnect(old_video_adapter);
xians@webrtc.orgef221512014-02-21 10:31:29 +0000698 }
henrike@webrtc.org26438052014-02-20 22:32:53 +0000699 }
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000700
701 capturer_updated_ = true;
702 video_capturer_ = video_capturer;
703
henrike@webrtc.orgc7bec842014-03-12 19:53:43 +0000704 vie_wrapper->base()->RegisterCpuOveruseObserver(channel_id_, NULL);
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000705 if (!video_capturer) {
706 overuse_observer_.reset();
707 return;
708 }
709
710 CoordinatedVideoAdapter* adapter = video_adapter();
711 ASSERT(adapter && "Video adapter should not be null here.");
712
713 UpdateAdapterCpuOptions();
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000714
715 overuse_observer_.reset(new WebRtcOveruseObserver(adapter));
henrike@webrtc.orgc7bec842014-03-12 19:53:43 +0000716 vie_wrapper->base()->RegisterCpuOveruseObserver(channel_id_,
717 overuse_observer_.get());
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000718 // (Dis)connect the video adapter from the cpu monitor as appropriate.
719 SetCpuOveruseDetection(overuse_observer_enabled_);
720
721 SignalCpuAdaptationUnable.repeat(adapter->SignalCpuAdaptationUnable);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000722 }
723
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000724 CoordinatedVideoAdapter* video_adapter() {
725 if (!video_capturer_) {
726 return NULL;
727 }
728 return video_capturer_->video_adapter();
729 }
730 const CoordinatedVideoAdapter* video_adapter() const {
731 if (!video_capturer_) {
732 return NULL;
733 }
734 return video_capturer_->video_adapter();
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000735 }
736
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000737 void ApplyCpuOptions(const VideoOptions& video_options) {
738 // Use video_options_.SetAll() instead of assignment so that unset value in
739 // video_options will not overwrite the previous option value.
740 video_options_.SetAll(video_options);
741 UpdateAdapterCpuOptions();
742 }
743
744 void UpdateAdapterCpuOptions() {
745 if (!video_capturer_) {
746 return;
747 }
748
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000749 bool cpu_adapt, cpu_smoothing, adapt_third;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000750 float low, med, high;
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000751
752 // TODO(thorcarpenter): Have VideoAdapter be responsible for setting
753 // all these video options.
754 CoordinatedVideoAdapter* video_adapter = video_capturer_->video_adapter();
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000755 if (video_options_.adapt_input_to_cpu_usage.Get(&cpu_adapt) ||
756 overuse_observer_enabled_) {
757 video_adapter->set_cpu_adaptation(cpu_adapt || overuse_observer_enabled_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000758 }
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000759 if (video_options_.adapt_cpu_with_smoothing.Get(&cpu_smoothing)) {
760 video_adapter->set_cpu_smoothing(cpu_smoothing);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000761 }
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000762 if (video_options_.process_adaptation_threshhold.Get(&med)) {
763 video_adapter->set_process_threshold(med);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000764 }
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000765 if (video_options_.system_low_adaptation_threshhold.Get(&low)) {
766 video_adapter->set_low_system_threshold(low);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000767 }
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000768 if (video_options_.system_high_adaptation_threshhold.Get(&high)) {
769 video_adapter->set_high_system_threshold(high);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000770 }
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000771 if (video_options_.video_adapt_third.Get(&adapt_third)) {
772 video_adapter->set_scale_third(adapt_third);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000773 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000774 }
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000775
776 void SetCpuOveruseDetection(bool enable) {
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000777 overuse_observer_enabled_ = enable;
778
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000779 if (overuse_observer_) {
780 overuse_observer_->Enable(enable);
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000781 }
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000782
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000783 // The video adapter is signaled by overuse detection if enabled; otherwise
784 // it will be signaled by cpu monitor.
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000785 CoordinatedVideoAdapter* adapter = video_adapter();
786 if (adapter) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000787 bool cpu_adapt = false;
788 video_options_.adapt_input_to_cpu_usage.Get(&cpu_adapt);
789 adapter->set_cpu_adaptation(
790 adapter->cpu_adaptation() || cpu_adapt || enable);
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000791 if (cpu_monitor_) {
792 if (enable) {
793 cpu_monitor_->SignalUpdate.disconnect(adapter);
794 } else {
795 cpu_monitor_->SignalUpdate.connect(
796 adapter, &CoordinatedVideoAdapter::OnCpuLoadUpdated);
797 }
798 }
799 }
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000800 }
801
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000802 void ProcessFrame(const VideoFrame& original_frame, bool mute,
803 VideoFrame** processed_frame) {
804 if (!mute) {
805 *processed_frame = original_frame.Copy();
806 } else {
807 WebRtcVideoFrame* black_frame = new WebRtcVideoFrame();
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000808 black_frame->InitToBlack(static_cast<int>(original_frame.GetWidth()),
809 static_cast<int>(original_frame.GetHeight()),
810 1, 1,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000811 original_frame.GetElapsedTime(),
812 original_frame.GetTimeStamp());
813 *processed_frame = black_frame;
814 }
815 local_stream_info_.UpdateFrame(*processed_frame);
816 }
817 void RegisterEncoder(int pl_type, webrtc::VideoEncoder* encoder) {
818 ASSERT(!IsEncoderRegistered(pl_type));
819 registered_encoders_[pl_type] = encoder;
820 }
821 bool IsEncoderRegistered(int pl_type) {
822 return registered_encoders_.count(pl_type) != 0;
823 }
824 const EncoderMap& registered_encoders() {
825 return registered_encoders_;
826 }
827 void ClearRegisteredEncoders() {
828 registered_encoders_.clear();
829 }
830
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000831 sigslot::repeater0<> SignalCpuAdaptationUnable;
832
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000833 private:
834 int channel_id_;
835 int capture_id_;
836 bool sending_;
837 bool muted_;
838 VideoCapturer* video_capturer_;
839 WebRtcEncoderObserver encoder_observer_;
840 webrtc::ViEExternalCapture* external_capture_;
841 EncoderMap registered_encoders_;
842
843 VideoFormat video_format_;
844
845 talk_base::scoped_ptr<StreamParams> stream_params_;
846
847 WebRtcLocalStreamInfo local_stream_info_;
848
849 bool capturer_updated_;
850
851 int64 interval_;
852
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000853 talk_base::CpuMonitor* cpu_monitor_;
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000854 talk_base::scoped_ptr<WebRtcOveruseObserver> overuse_observer_;
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000855 bool overuse_observer_enabled_;
856
857 VideoOptions video_options_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000858};
859
860const WebRtcVideoEngine::VideoCodecPref
861 WebRtcVideoEngine::kVideoCodecPrefs[] = {
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000862 {kVp8PayloadName, 100, -1, 0},
863 {kRedPayloadName, 116, -1, 1},
864 {kFecPayloadName, 117, -1, 2},
865 {kRtxCodecName, 96, 100, 3},
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000866};
867
868// The formats are sorted by the descending order of width. We use the order to
869// find the next format for CPU and bandwidth adaptation.
870const VideoFormatPod WebRtcVideoEngine::kVideoFormats[] = {
871 {1280, 800, FPS_TO_INTERVAL(30), FOURCC_ANY},
872 {1280, 720, FPS_TO_INTERVAL(30), FOURCC_ANY},
873 {960, 600, FPS_TO_INTERVAL(30), FOURCC_ANY},
874 {960, 540, FPS_TO_INTERVAL(30), FOURCC_ANY},
875 {640, 400, FPS_TO_INTERVAL(30), FOURCC_ANY},
876 {640, 360, FPS_TO_INTERVAL(30), FOURCC_ANY},
877 {640, 480, FPS_TO_INTERVAL(30), FOURCC_ANY},
878 {480, 300, FPS_TO_INTERVAL(30), FOURCC_ANY},
879 {480, 270, FPS_TO_INTERVAL(30), FOURCC_ANY},
880 {480, 360, FPS_TO_INTERVAL(30), FOURCC_ANY},
881 {320, 200, FPS_TO_INTERVAL(30), FOURCC_ANY},
882 {320, 180, FPS_TO_INTERVAL(30), FOURCC_ANY},
883 {320, 240, FPS_TO_INTERVAL(30), FOURCC_ANY},
884 {240, 150, FPS_TO_INTERVAL(30), FOURCC_ANY},
885 {240, 135, FPS_TO_INTERVAL(30), FOURCC_ANY},
886 {240, 180, FPS_TO_INTERVAL(30), FOURCC_ANY},
887 {160, 100, FPS_TO_INTERVAL(30), FOURCC_ANY},
888 {160, 90, FPS_TO_INTERVAL(30), FOURCC_ANY},
889 {160, 120, FPS_TO_INTERVAL(30), FOURCC_ANY},
890};
891
892const VideoFormatPod WebRtcVideoEngine::kDefaultVideoFormat =
893 {640, 400, FPS_TO_INTERVAL(30), FOURCC_ANY};
894
895static void UpdateVideoCodec(const cricket::VideoFormat& video_format,
896 webrtc::VideoCodec* target_codec) {
897 if ((target_codec == NULL) || (video_format == cricket::VideoFormat())) {
898 return;
899 }
900 target_codec->width = video_format.width;
901 target_codec->height = video_format.height;
902 target_codec->maxFramerate = cricket::VideoFormat::IntervalToFps(
903 video_format.interval);
904}
905
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000906static bool GetCpuOveruseOptions(const VideoOptions& options,
907 webrtc::CpuOveruseOptions* overuse_options) {
908 int underuse_threshold = 0;
909 int overuse_threshold = 0;
910 if (!options.cpu_underuse_threshold.Get(&underuse_threshold) ||
911 !options.cpu_overuse_threshold.Get(&overuse_threshold)) {
912 return false;
913 }
914 if (underuse_threshold <= 0 || overuse_threshold <= 0) {
915 return false;
916 }
917 // Valid thresholds.
918 bool encode_usage =
919 options.cpu_overuse_encode_usage.GetWithDefaultIfUnset(false);
920 overuse_options->enable_capture_jitter_method = !encode_usage;
921 overuse_options->enable_encode_usage_method = encode_usage;
922 if (encode_usage) {
923 // Use method based on encode usage.
924 overuse_options->low_encode_usage_threshold_percent = underuse_threshold;
925 overuse_options->high_encode_usage_threshold_percent = overuse_threshold;
926 } else {
927 // Use default method based on capture jitter.
928 overuse_options->low_capture_jitter_threshold_ms =
929 static_cast<float>(underuse_threshold);
930 overuse_options->high_capture_jitter_threshold_ms =
931 static_cast<float>(overuse_threshold);
932 }
933 return true;
934}
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000935
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000936WebRtcVideoEngine::WebRtcVideoEngine() {
937 Construct(new ViEWrapper(), new ViETraceWrapper(), NULL,
938 new talk_base::CpuMonitor(NULL));
939}
940
941WebRtcVideoEngine::WebRtcVideoEngine(WebRtcVoiceEngine* voice_engine,
942 ViEWrapper* vie_wrapper,
943 talk_base::CpuMonitor* cpu_monitor) {
944 Construct(vie_wrapper, new ViETraceWrapper(), voice_engine, cpu_monitor);
945}
946
947WebRtcVideoEngine::WebRtcVideoEngine(WebRtcVoiceEngine* voice_engine,
948 ViEWrapper* vie_wrapper,
949 ViETraceWrapper* tracing,
950 talk_base::CpuMonitor* cpu_monitor) {
951 Construct(vie_wrapper, tracing, voice_engine, cpu_monitor);
952}
953
954void WebRtcVideoEngine::Construct(ViEWrapper* vie_wrapper,
955 ViETraceWrapper* tracing,
956 WebRtcVoiceEngine* voice_engine,
957 talk_base::CpuMonitor* cpu_monitor) {
958 LOG(LS_INFO) << "WebRtcVideoEngine::WebRtcVideoEngine";
959 worker_thread_ = NULL;
960 vie_wrapper_.reset(vie_wrapper);
961 vie_wrapper_base_initialized_ = false;
962 tracing_.reset(tracing);
963 voice_engine_ = voice_engine;
964 initialized_ = false;
965 SetTraceFilter(SeverityToFilter(kDefaultLogSeverity));
966 render_module_.reset(new WebRtcPassthroughRender());
967 local_renderer_w_ = local_renderer_h_ = 0;
968 local_renderer_ = NULL;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000969 capture_started_ = false;
970 decoder_factory_ = NULL;
971 encoder_factory_ = NULL;
972 cpu_monitor_.reset(cpu_monitor);
973
974 SetTraceOptions("");
975 if (tracing_->SetTraceCallback(this) != 0) {
976 LOG_RTCERR1(SetTraceCallback, this);
977 }
978
979 // Set default quality levels for our supported codecs. We override them here
980 // if we know your cpu performance is low, and they can be updated explicitly
981 // by calling SetDefaultCodec. For example by a flute preference setting, or
982 // by the server with a jec in response to our reported system info.
983 VideoCodec max_codec(kVideoCodecPrefs[0].payload_type,
984 kVideoCodecPrefs[0].name,
985 kDefaultVideoFormat.width,
986 kDefaultVideoFormat.height,
987 VideoFormat::IntervalToFps(kDefaultVideoFormat.interval),
988 0);
989 if (!SetDefaultCodec(max_codec)) {
990 LOG(LS_ERROR) << "Failed to initialize list of supported codec types";
991 }
992
993
994 // Load our RTP Header extensions.
995 rtp_header_extensions_.push_back(
996 RtpHeaderExtension(kRtpTimestampOffsetHeaderExtension,
henrike@webrtc.org79047f92014-03-06 23:46:59 +0000997 kRtpTimestampOffsetHeaderExtensionDefaultId));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000998 rtp_header_extensions_.push_back(
henrike@webrtc.org79047f92014-03-06 23:46:59 +0000999 RtpHeaderExtension(kRtpAbsoluteSenderTimeHeaderExtension,
1000 kRtpAbsoluteSenderTimeHeaderExtensionDefaultId));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001001}
1002
1003WebRtcVideoEngine::~WebRtcVideoEngine() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001004 LOG(LS_INFO) << "WebRtcVideoEngine::~WebRtcVideoEngine";
1005 if (initialized_) {
1006 Terminate();
1007 }
1008 if (encoder_factory_) {
1009 encoder_factory_->RemoveObserver(this);
1010 }
1011 tracing_->SetTraceCallback(NULL);
1012 // Test to see if the media processor was deregistered properly.
1013 ASSERT(SignalMediaFrame.is_empty());
1014}
1015
1016bool WebRtcVideoEngine::Init(talk_base::Thread* worker_thread) {
1017 LOG(LS_INFO) << "WebRtcVideoEngine::Init";
1018 worker_thread_ = worker_thread;
1019 ASSERT(worker_thread_ != NULL);
1020
1021 cpu_monitor_->set_thread(worker_thread_);
1022 if (!cpu_monitor_->Start(kCpuMonitorPeriodMs)) {
1023 LOG(LS_ERROR) << "Failed to start CPU monitor.";
1024 cpu_monitor_.reset();
1025 }
1026
1027 bool result = InitVideoEngine();
1028 if (result) {
1029 LOG(LS_INFO) << "VideoEngine Init done";
1030 } else {
1031 LOG(LS_ERROR) << "VideoEngine Init failed, releasing";
1032 Terminate();
1033 }
1034 return result;
1035}
1036
1037bool WebRtcVideoEngine::InitVideoEngine() {
1038 LOG(LS_INFO) << "WebRtcVideoEngine::InitVideoEngine";
1039
1040 // Init WebRTC VideoEngine.
1041 if (!vie_wrapper_base_initialized_) {
1042 if (vie_wrapper_->base()->Init() != 0) {
1043 LOG_RTCERR0(Init);
1044 return false;
1045 }
1046 vie_wrapper_base_initialized_ = true;
1047 }
1048
1049 // Log the VoiceEngine version info.
1050 char buffer[1024] = "";
1051 if (vie_wrapper_->base()->GetVersion(buffer) != 0) {
1052 LOG_RTCERR0(GetVersion);
1053 return false;
1054 }
1055
1056 LOG(LS_INFO) << "WebRtc VideoEngine Version:";
1057 LogMultiline(talk_base::LS_INFO, buffer);
1058
1059 // Hook up to VoiceEngine for sync purposes, if supplied.
1060 if (!voice_engine_) {
1061 LOG(LS_WARNING) << "NULL voice engine";
1062 } else if ((vie_wrapper_->base()->SetVoiceEngine(
1063 voice_engine_->voe()->engine())) != 0) {
1064 LOG_RTCERR0(SetVoiceEngine);
1065 return false;
1066 }
1067
1068 // Register our custom render module.
1069 if (vie_wrapper_->render()->RegisterVideoRenderModule(
1070 *render_module_.get()) != 0) {
1071 LOG_RTCERR0(RegisterVideoRenderModule);
1072 return false;
1073 }
1074
1075 initialized_ = true;
1076 return true;
1077}
1078
1079void WebRtcVideoEngine::Terminate() {
1080 LOG(LS_INFO) << "WebRtcVideoEngine::Terminate";
1081 initialized_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001082
1083 if (vie_wrapper_->render()->DeRegisterVideoRenderModule(
1084 *render_module_.get()) != 0) {
1085 LOG_RTCERR0(DeRegisterVideoRenderModule);
1086 }
1087
1088 if (vie_wrapper_->base()->SetVoiceEngine(NULL) != 0) {
1089 LOG_RTCERR0(SetVoiceEngine);
1090 }
1091
1092 cpu_monitor_->Stop();
1093}
1094
1095int WebRtcVideoEngine::GetCapabilities() {
1096 return VIDEO_RECV | VIDEO_SEND;
1097}
1098
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00001099bool WebRtcVideoEngine::SetOptions(const VideoOptions &options) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001100 return true;
1101}
1102
1103bool WebRtcVideoEngine::SetDefaultEncoderConfig(
1104 const VideoEncoderConfig& config) {
1105 return SetDefaultCodec(config.max_codec);
1106}
1107
wu@webrtc.org78187522013-10-07 23:32:02 +00001108VideoEncoderConfig WebRtcVideoEngine::GetDefaultEncoderConfig() const {
1109 ASSERT(!video_codecs_.empty());
1110 VideoCodec max_codec(kVideoCodecPrefs[0].payload_type,
1111 kVideoCodecPrefs[0].name,
1112 video_codecs_[0].width,
1113 video_codecs_[0].height,
1114 video_codecs_[0].framerate,
1115 0);
1116 return VideoEncoderConfig(max_codec);
1117}
1118
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001119// SetDefaultCodec may be called while the capturer is running. For example, a
1120// test call is started in a page with QVGA default codec, and then a real call
1121// is started in another page with VGA default codec. This is the corner case
1122// and happens only when a session is started. We ignore this case currently.
1123bool WebRtcVideoEngine::SetDefaultCodec(const VideoCodec& codec) {
1124 if (!RebuildCodecList(codec)) {
1125 LOG(LS_WARNING) << "Failed to RebuildCodecList";
1126 return false;
1127 }
1128
wu@webrtc.org78187522013-10-07 23:32:02 +00001129 ASSERT(!video_codecs_.empty());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001130 default_codec_format_ = VideoFormat(
1131 video_codecs_[0].width,
1132 video_codecs_[0].height,
1133 VideoFormat::FpsToInterval(video_codecs_[0].framerate),
1134 FOURCC_ANY);
1135 return true;
1136}
1137
1138WebRtcVideoMediaChannel* WebRtcVideoEngine::CreateChannel(
1139 VoiceMediaChannel* voice_channel) {
1140 WebRtcVideoMediaChannel* channel =
1141 new WebRtcVideoMediaChannel(this, voice_channel);
1142 if (!channel->Init()) {
1143 delete channel;
1144 channel = NULL;
1145 }
1146 return channel;
1147}
1148
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001149bool WebRtcVideoEngine::SetLocalRenderer(VideoRenderer* renderer) {
1150 local_renderer_w_ = local_renderer_h_ = 0;
1151 local_renderer_ = renderer;
1152 return true;
1153}
1154
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001155const std::vector<VideoCodec>& WebRtcVideoEngine::codecs() const {
1156 return video_codecs_;
1157}
1158
1159const std::vector<RtpHeaderExtension>&
1160WebRtcVideoEngine::rtp_header_extensions() const {
1161 return rtp_header_extensions_;
1162}
1163
1164void WebRtcVideoEngine::SetLogging(int min_sev, const char* filter) {
1165 // if min_sev == -1, we keep the current log level.
1166 if (min_sev >= 0) {
1167 SetTraceFilter(SeverityToFilter(min_sev));
1168 }
1169 SetTraceOptions(filter);
1170}
1171
1172int WebRtcVideoEngine::GetLastEngineError() {
1173 return vie_wrapper_->error();
1174}
1175
1176// Checks to see whether we comprehend and could receive a particular codec
1177bool WebRtcVideoEngine::FindCodec(const VideoCodec& in) {
1178 for (int i = 0; i < ARRAY_SIZE(kVideoFormats); ++i) {
1179 const VideoFormat fmt(kVideoFormats[i]);
1180 if ((in.width == 0 && in.height == 0) ||
1181 (fmt.width == in.width && fmt.height == in.height)) {
1182 if (encoder_factory_) {
1183 const std::vector<WebRtcVideoEncoderFactory::VideoCodec>& codecs =
1184 encoder_factory_->codecs();
1185 for (size_t j = 0; j < codecs.size(); ++j) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001186 VideoCodec codec(GetExternalVideoPayloadType(static_cast<int>(j)),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001187 codecs[j].name, 0, 0, 0, 0);
1188 if (codec.Matches(in))
1189 return true;
1190 }
1191 }
1192 for (size_t j = 0; j < ARRAY_SIZE(kVideoCodecPrefs); ++j) {
1193 VideoCodec codec(kVideoCodecPrefs[j].payload_type,
1194 kVideoCodecPrefs[j].name, 0, 0, 0, 0);
1195 if (codec.Matches(in)) {
1196 return true;
1197 }
1198 }
1199 }
1200 }
1201 return false;
1202}
1203
1204// Given the requested codec, returns true if we can send that codec type and
1205// updates out with the best quality we could send for that codec. If current is
1206// not empty, we constrain out so that its aspect ratio matches current's.
1207bool WebRtcVideoEngine::CanSendCodec(const VideoCodec& requested,
1208 const VideoCodec& current,
1209 VideoCodec* out) {
1210 if (!out) {
1211 return false;
1212 }
1213
1214 std::vector<VideoCodec>::const_iterator local_max;
1215 for (local_max = video_codecs_.begin();
1216 local_max < video_codecs_.end();
1217 ++local_max) {
1218 // First match codecs by payload type
1219 if (!requested.Matches(*local_max)) {
1220 continue;
1221 }
1222
1223 out->id = requested.id;
1224 out->name = requested.name;
1225 out->preference = requested.preference;
1226 out->params = requested.params;
1227 out->framerate = talk_base::_min(requested.framerate, local_max->framerate);
1228 out->width = 0;
1229 out->height = 0;
1230 out->params = requested.params;
1231 out->feedback_params = requested.feedback_params;
1232
1233 if (0 == requested.width && 0 == requested.height) {
1234 // Special case with resolution 0. The channel should not send frames.
1235 return true;
1236 } else if (0 == requested.width || 0 == requested.height) {
1237 // 0xn and nx0 are invalid resolutions.
1238 return false;
1239 }
1240
1241 // Pick the best quality that is within their and our bounds and has the
1242 // correct aspect ratio.
1243 for (int j = 0; j < ARRAY_SIZE(kVideoFormats); ++j) {
1244 const VideoFormat format(kVideoFormats[j]);
1245
1246 // Skip any format that is larger than the local or remote maximums, or
1247 // smaller than the current best match
1248 if (format.width > requested.width || format.height > requested.height ||
1249 format.width > local_max->width ||
1250 (format.width < out->width && format.height < out->height)) {
1251 continue;
1252 }
1253
1254 bool better = false;
1255
1256 // Check any further constraints on this prospective format
1257 if (!out->width || !out->height) {
1258 // If we don't have any matches yet, this is the best so far.
1259 better = true;
1260 } else if (current.width && current.height) {
1261 // current is set so format must match its ratio exactly.
1262 better =
1263 (format.width * current.height == format.height * current.width);
1264 } else {
1265 // Prefer closer aspect ratios i.e
1266 // format.aspect - requested.aspect < out.aspect - requested.aspect
1267 better = abs(format.width * requested.height * out->height -
1268 requested.width * format.height * out->height) <
1269 abs(out->width * format.height * requested.height -
1270 requested.width * format.height * out->height);
1271 }
1272
1273 if (better) {
1274 out->width = format.width;
1275 out->height = format.height;
1276 }
1277 }
1278 if (out->width > 0) {
1279 return true;
1280 }
1281 }
1282 return false;
1283}
1284
1285static void ConvertToCricketVideoCodec(
1286 const webrtc::VideoCodec& in_codec, VideoCodec* out_codec) {
1287 out_codec->id = in_codec.plType;
1288 out_codec->name = in_codec.plName;
1289 out_codec->width = in_codec.width;
1290 out_codec->height = in_codec.height;
1291 out_codec->framerate = in_codec.maxFramerate;
buildbot@webrtc.orgd1ae89f2014-05-08 19:19:26 +00001292 if (BitrateIsSet(in_codec.minBitrate)) {
1293 out_codec->SetParam(kCodecParamMinBitrate, in_codec.minBitrate);
1294 }
1295 if (BitrateIsSet(in_codec.maxBitrate)) {
1296 out_codec->SetParam(kCodecParamMaxBitrate, in_codec.maxBitrate);
1297 }
1298 if (BitrateIsSet(in_codec.startBitrate)) {
1299 out_codec->SetParam(kCodecParamStartBitrate, in_codec.startBitrate);
1300 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001301 if (in_codec.qpMax) {
1302 out_codec->SetParam(kCodecParamMaxQuantization, in_codec.qpMax);
1303 }
1304}
1305
1306bool WebRtcVideoEngine::ConvertFromCricketVideoCodec(
1307 const VideoCodec& in_codec, webrtc::VideoCodec* out_codec) {
1308 bool found = false;
1309 int ncodecs = vie_wrapper_->codec()->NumberOfCodecs();
1310 for (int i = 0; i < ncodecs; ++i) {
1311 if (vie_wrapper_->codec()->GetCodec(i, *out_codec) == 0 &&
1312 _stricmp(in_codec.name.c_str(), out_codec->plName) == 0) {
1313 found = true;
1314 break;
1315 }
1316 }
1317
1318 // If not found, check if this is supported by external encoder factory.
1319 if (!found && encoder_factory_) {
1320 const std::vector<WebRtcVideoEncoderFactory::VideoCodec>& codecs =
1321 encoder_factory_->codecs();
1322 for (size_t i = 0; i < codecs.size(); ++i) {
1323 if (_stricmp(in_codec.name.c_str(), codecs[i].name.c_str()) == 0) {
1324 out_codec->codecType = codecs[i].type;
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001325 out_codec->plType = GetExternalVideoPayloadType(static_cast<int>(i));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001326 talk_base::strcpyn(out_codec->plName, sizeof(out_codec->plName),
1327 codecs[i].name.c_str(), codecs[i].name.length());
1328 found = true;
1329 break;
1330 }
1331 }
1332 }
1333
buildbot@webrtc.orgdd4742a2014-05-07 14:50:35 +00001334 // Is this an RTX codec? Handled separately here since webrtc doesn't handle
1335 // them as webrtc::VideoCodec internally.
1336 if (!found && _stricmp(in_codec.name.c_str(), kRtxCodecName) == 0) {
1337 talk_base::strcpyn(out_codec->plName, sizeof(out_codec->plName),
1338 in_codec.name.c_str(), in_codec.name.length());
1339 out_codec->plType = in_codec.id;
1340 found = true;
1341 }
1342
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001343 if (!found) {
1344 LOG(LS_ERROR) << "invalid codec type";
1345 return false;
1346 }
1347
1348 if (in_codec.id != 0)
1349 out_codec->plType = in_codec.id;
1350
1351 if (in_codec.width != 0)
1352 out_codec->width = in_codec.width;
1353
1354 if (in_codec.height != 0)
1355 out_codec->height = in_codec.height;
1356
1357 if (in_codec.framerate != 0)
1358 out_codec->maxFramerate = in_codec.framerate;
1359
1360 // Convert bitrate parameters.
buildbot@webrtc.orgd1ae89f2014-05-08 19:19:26 +00001361 int max_bitrate = -1;
1362 int min_bitrate = -1;
1363 int start_bitrate = -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001364
1365 in_codec.GetParam(kCodecParamMinBitrate, &min_bitrate);
1366 in_codec.GetParam(kCodecParamMaxBitrate, &max_bitrate);
buildbot@webrtc.orged97bb02014-05-07 11:15:20 +00001367 in_codec.GetParam(kCodecParamStartBitrate, &start_bitrate);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001368
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001369
1370 out_codec->minBitrate = min_bitrate;
1371 out_codec->startBitrate = start_bitrate;
1372 out_codec->maxBitrate = max_bitrate;
1373
1374 // Convert general codec parameters.
1375 int max_quantization = 0;
1376 if (in_codec.GetParam(kCodecParamMaxQuantization, &max_quantization)) {
1377 if (max_quantization < 0) {
1378 return false;
1379 }
1380 out_codec->qpMax = max_quantization;
1381 }
1382 return true;
1383}
1384
1385void WebRtcVideoEngine::RegisterChannel(WebRtcVideoMediaChannel *channel) {
1386 talk_base::CritScope cs(&channels_crit_);
1387 channels_.push_back(channel);
1388}
1389
1390void WebRtcVideoEngine::UnregisterChannel(WebRtcVideoMediaChannel *channel) {
1391 talk_base::CritScope cs(&channels_crit_);
1392 channels_.erase(std::remove(channels_.begin(), channels_.end(), channel),
1393 channels_.end());
1394}
1395
1396bool WebRtcVideoEngine::SetVoiceEngine(WebRtcVoiceEngine* voice_engine) {
1397 if (initialized_) {
1398 LOG(LS_WARNING) << "SetVoiceEngine can not be called after Init";
1399 return false;
1400 }
1401 voice_engine_ = voice_engine;
1402 return true;
1403}
1404
1405bool WebRtcVideoEngine::EnableTimedRender() {
1406 if (initialized_) {
1407 LOG(LS_WARNING) << "EnableTimedRender can not be called after Init";
1408 return false;
1409 }
1410 render_module_.reset(webrtc::VideoRender::CreateVideoRender(0, NULL,
1411 false, webrtc::kRenderExternal));
1412 return true;
1413}
1414
1415void WebRtcVideoEngine::SetTraceFilter(int filter) {
1416 tracing_->SetTraceFilter(filter);
1417}
1418
1419// See https://sites.google.com/a/google.com/wavelet/
1420// Home/Magic-Flute--RTC-Engine-/Magic-Flute-Command-Line-Parameters
1421// for all supported command line setttings.
1422void WebRtcVideoEngine::SetTraceOptions(const std::string& options) {
1423 // Set WebRTC trace file.
1424 std::vector<std::string> opts;
1425 talk_base::tokenize(options, ' ', '"', '"', &opts);
1426 std::vector<std::string>::iterator tracefile =
1427 std::find(opts.begin(), opts.end(), "tracefile");
1428 if (tracefile != opts.end() && ++tracefile != opts.end()) {
1429 // Write WebRTC debug output (at same loglevel) to file
1430 if (tracing_->SetTraceFile(tracefile->c_str()) == -1) {
1431 LOG_RTCERR1(SetTraceFile, *tracefile);
1432 }
1433 }
1434}
1435
1436static void AddDefaultFeedbackParams(VideoCodec* codec) {
1437 const FeedbackParam kFir(kRtcpFbParamCcm, kRtcpFbCcmParamFir);
1438 codec->AddFeedbackParam(kFir);
1439 const FeedbackParam kNack(kRtcpFbParamNack, kParamValueEmpty);
1440 codec->AddFeedbackParam(kNack);
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001441 const FeedbackParam kPli(kRtcpFbParamNack, kRtcpFbNackParamPli);
1442 codec->AddFeedbackParam(kPli);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001443 const FeedbackParam kRemb(kRtcpFbParamRemb, kParamValueEmpty);
1444 codec->AddFeedbackParam(kRemb);
1445}
1446
1447// Rebuilds the codec list to be only those that are less intensive
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00001448// than the specified codec. Prefers internal codec over external with
1449// higher preference field.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001450bool WebRtcVideoEngine::RebuildCodecList(const VideoCodec& in_codec) {
1451 if (!FindCodec(in_codec))
1452 return false;
1453
1454 video_codecs_.clear();
1455
1456 bool found = false;
mallinath@webrtc.org1112c302013-09-23 20:34:45 +00001457 std::set<std::string> internal_codec_names;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001458 for (size_t i = 0; i < ARRAY_SIZE(kVideoCodecPrefs); ++i) {
1459 const VideoCodecPref& pref(kVideoCodecPrefs[i]);
1460 if (!found)
1461 found = (in_codec.name == pref.name);
mallinath@webrtc.org1112c302013-09-23 20:34:45 +00001462 if (found) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001463 VideoCodec codec(pref.payload_type, pref.name,
1464 in_codec.width, in_codec.height, in_codec.framerate,
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001465 static_cast<int>(ARRAY_SIZE(kVideoCodecPrefs) - i));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001466 if (_stricmp(kVp8PayloadName, codec.name.c_str()) == 0) {
1467 AddDefaultFeedbackParams(&codec);
1468 }
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00001469 if (pref.associated_payload_type != -1) {
1470 codec.SetParam(kCodecParamAssociatedPayloadType,
1471 pref.associated_payload_type);
1472 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001473 video_codecs_.push_back(codec);
mallinath@webrtc.org1112c302013-09-23 20:34:45 +00001474 internal_codec_names.insert(codec.name);
1475 }
1476 }
1477 if (encoder_factory_) {
1478 const std::vector<WebRtcVideoEncoderFactory::VideoCodec>& codecs =
1479 encoder_factory_->codecs();
1480 for (size_t i = 0; i < codecs.size(); ++i) {
1481 bool is_internal_codec = internal_codec_names.find(codecs[i].name) !=
1482 internal_codec_names.end();
1483 if (!is_internal_codec) {
1484 if (!found)
1485 found = (in_codec.name == codecs[i].name);
1486 VideoCodec codec(
1487 GetExternalVideoPayloadType(static_cast<int>(i)),
1488 codecs[i].name,
1489 codecs[i].max_width,
1490 codecs[i].max_height,
1491 codecs[i].max_fps,
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00001492 // Use negative preference on external codec to ensure the internal
1493 // codec is preferred.
1494 static_cast<int>(0 - i));
mallinath@webrtc.org1112c302013-09-23 20:34:45 +00001495 AddDefaultFeedbackParams(&codec);
1496 video_codecs_.push_back(codec);
1497 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001498 }
1499 }
1500 ASSERT(found);
1501 return true;
1502}
1503
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001504// Ignore spammy trace messages, mostly from the stats API when we haven't
1505// gotten RTCP info yet from the remote side.
1506bool WebRtcVideoEngine::ShouldIgnoreTrace(const std::string& trace) {
1507 static const char* const kTracesToIgnore[] = {
1508 NULL
1509 };
1510 for (const char* const* p = kTracesToIgnore; *p; ++p) {
1511 if (trace.find(*p) == 0) {
1512 return true;
1513 }
1514 }
1515 return false;
1516}
1517
1518int WebRtcVideoEngine::GetNumOfChannels() {
1519 talk_base::CritScope cs(&channels_crit_);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001520 return static_cast<int>(channels_.size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001521}
1522
1523void WebRtcVideoEngine::Print(webrtc::TraceLevel level, const char* trace,
1524 int length) {
1525 talk_base::LoggingSeverity sev = talk_base::LS_VERBOSE;
1526 if (level == webrtc::kTraceError || level == webrtc::kTraceCritical)
1527 sev = talk_base::LS_ERROR;
1528 else if (level == webrtc::kTraceWarning)
1529 sev = talk_base::LS_WARNING;
1530 else if (level == webrtc::kTraceStateInfo || level == webrtc::kTraceInfo)
1531 sev = talk_base::LS_INFO;
1532 else if (level == webrtc::kTraceTerseInfo)
1533 sev = talk_base::LS_INFO;
1534
1535 // Skip past boilerplate prefix text
1536 if (length < 72) {
1537 std::string msg(trace, length);
1538 LOG(LS_ERROR) << "Malformed webrtc log message: ";
1539 LOG_V(sev) << msg;
1540 } else {
1541 std::string msg(trace + 71, length - 72);
1542 if (!ShouldIgnoreTrace(msg) &&
1543 (!voice_engine_ || !voice_engine_->ShouldIgnoreTrace(msg))) {
1544 LOG_V(sev) << "webrtc: " << msg;
1545 }
1546 }
1547}
1548
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001549webrtc::VideoDecoder* WebRtcVideoEngine::CreateExternalDecoder(
1550 webrtc::VideoCodecType type) {
1551 if (decoder_factory_ == NULL) {
1552 return NULL;
1553 }
1554 return decoder_factory_->CreateVideoDecoder(type);
1555}
1556
1557void WebRtcVideoEngine::DestroyExternalDecoder(webrtc::VideoDecoder* decoder) {
1558 ASSERT(decoder_factory_ != NULL);
1559 if (decoder_factory_ == NULL)
1560 return;
1561 decoder_factory_->DestroyVideoDecoder(decoder);
1562}
1563
1564webrtc::VideoEncoder* WebRtcVideoEngine::CreateExternalEncoder(
1565 webrtc::VideoCodecType type) {
1566 if (encoder_factory_ == NULL) {
1567 return NULL;
1568 }
1569 return encoder_factory_->CreateVideoEncoder(type);
1570}
1571
1572void WebRtcVideoEngine::DestroyExternalEncoder(webrtc::VideoEncoder* encoder) {
1573 ASSERT(encoder_factory_ != NULL);
1574 if (encoder_factory_ == NULL)
1575 return;
1576 encoder_factory_->DestroyVideoEncoder(encoder);
1577}
1578
1579bool WebRtcVideoEngine::IsExternalEncoderCodecType(
1580 webrtc::VideoCodecType type) const {
1581 if (!encoder_factory_)
1582 return false;
1583 const std::vector<WebRtcVideoEncoderFactory::VideoCodec>& codecs =
1584 encoder_factory_->codecs();
1585 std::vector<WebRtcVideoEncoderFactory::VideoCodec>::const_iterator it;
1586 for (it = codecs.begin(); it != codecs.end(); ++it) {
1587 if (it->type == type)
1588 return true;
1589 }
1590 return false;
1591}
1592
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001593void WebRtcVideoEngine::SetExternalDecoderFactory(
1594 WebRtcVideoDecoderFactory* decoder_factory) {
1595 decoder_factory_ = decoder_factory;
1596}
1597
1598void WebRtcVideoEngine::SetExternalEncoderFactory(
1599 WebRtcVideoEncoderFactory* encoder_factory) {
1600 if (encoder_factory_ == encoder_factory)
1601 return;
1602
1603 if (encoder_factory_) {
1604 encoder_factory_->RemoveObserver(this);
1605 }
1606 encoder_factory_ = encoder_factory;
1607 if (encoder_factory_) {
1608 encoder_factory_->AddObserver(this);
1609 }
1610
1611 // Invoke OnCodecAvailable() here in case the list of codecs is already
1612 // available when the encoder factory is installed. If not the encoder
1613 // factory will invoke the callback later when the codecs become available.
1614 OnCodecsAvailable();
1615}
1616
1617void WebRtcVideoEngine::OnCodecsAvailable() {
1618 // Rebuild codec list while reapplying the current default codec format.
1619 VideoCodec max_codec(kVideoCodecPrefs[0].payload_type,
1620 kVideoCodecPrefs[0].name,
1621 video_codecs_[0].width,
1622 video_codecs_[0].height,
1623 video_codecs_[0].framerate,
1624 0);
1625 if (!RebuildCodecList(max_codec)) {
1626 LOG(LS_ERROR) << "Failed to initialize list of supported codec types";
1627 }
1628}
1629
1630// WebRtcVideoMediaChannel
1631
1632WebRtcVideoMediaChannel::WebRtcVideoMediaChannel(
1633 WebRtcVideoEngine* engine,
1634 VoiceMediaChannel* channel)
1635 : engine_(engine),
1636 voice_channel_(channel),
1637 vie_channel_(-1),
1638 nack_enabled_(true),
1639 remb_enabled_(false),
1640 render_started_(false),
1641 first_receive_ssrc_(0),
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001642 num_unsignalled_recv_channels_(0),
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00001643 send_rtx_type_(-1),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001644 send_red_type_(-1),
1645 send_fec_type_(-1),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001646 sending_(false),
1647 ratio_w_(0),
1648 ratio_h_(0) {
1649 engine->RegisterChannel(this);
1650}
1651
1652bool WebRtcVideoMediaChannel::Init() {
1653 const uint32 ssrc_key = 0;
1654 return CreateChannel(ssrc_key, MD_SENDRECV, &vie_channel_);
1655}
1656
1657WebRtcVideoMediaChannel::~WebRtcVideoMediaChannel() {
1658 const bool send = false;
1659 SetSend(send);
1660 const bool render = false;
1661 SetRender(render);
1662
1663 while (!send_channels_.empty()) {
1664 if (!DeleteSendChannel(send_channels_.begin()->first)) {
1665 LOG(LS_ERROR) << "Unable to delete channel with ssrc key "
1666 << send_channels_.begin()->first;
1667 ASSERT(false);
1668 break;
1669 }
1670 }
1671
1672 // Remove all receive streams and the default channel.
1673 while (!recv_channels_.empty()) {
henrike@webrtc.org6e3dbc22014-03-25 17:09:47 +00001674 RemoveRecvStreamInternal(recv_channels_.begin()->first);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001675 }
1676
1677 // Unregister the channel from the engine.
1678 engine()->UnregisterChannel(this);
1679 if (worker_thread()) {
1680 worker_thread()->Clear(this);
1681 }
1682}
1683
1684bool WebRtcVideoMediaChannel::SetRecvCodecs(
1685 const std::vector<VideoCodec>& codecs) {
1686 receive_codecs_.clear();
buildbot@webrtc.orgdd4742a2014-05-07 14:50:35 +00001687 associated_payload_types_.clear();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001688 for (std::vector<VideoCodec>::const_iterator iter = codecs.begin();
1689 iter != codecs.end(); ++iter) {
1690 if (engine()->FindCodec(*iter)) {
1691 webrtc::VideoCodec wcodec;
1692 if (engine()->ConvertFromCricketVideoCodec(*iter, &wcodec)) {
1693 receive_codecs_.push_back(wcodec);
buildbot@webrtc.orgdd4742a2014-05-07 14:50:35 +00001694 int apt;
1695 if (iter->GetParam(cricket::kCodecParamAssociatedPayloadType, &apt)) {
1696 associated_payload_types_[wcodec.plType] = apt;
1697 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001698 }
1699 } else {
1700 LOG(LS_INFO) << "Unknown codec " << iter->name;
1701 return false;
1702 }
1703 }
1704
1705 for (RecvChannelMap::iterator it = recv_channels_.begin();
1706 it != recv_channels_.end(); ++it) {
1707 if (!SetReceiveCodecs(it->second))
1708 return false;
1709 }
1710 return true;
1711}
1712
1713bool WebRtcVideoMediaChannel::SetSendCodecs(
1714 const std::vector<VideoCodec>& codecs) {
1715 // Match with local video codec list.
1716 std::vector<webrtc::VideoCodec> send_codecs;
1717 VideoCodec checked_codec;
1718 VideoCodec current; // defaults to 0x0
1719 if (sending_) {
1720 ConvertToCricketVideoCodec(*send_codec_, &current);
1721 }
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00001722 std::map<int, int> primary_rtx_pt_mapping;
wu@webrtc.org05e7b442014-04-01 17:44:24 +00001723 bool nack_enabled = nack_enabled_;
1724 bool remb_enabled = remb_enabled_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001725 for (std::vector<VideoCodec>::const_iterator iter = codecs.begin();
1726 iter != codecs.end(); ++iter) {
1727 if (_stricmp(iter->name.c_str(), kRedPayloadName) == 0) {
1728 send_red_type_ = iter->id;
1729 } else if (_stricmp(iter->name.c_str(), kFecPayloadName) == 0) {
1730 send_fec_type_ = iter->id;
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00001731 } else if (_stricmp(iter->name.c_str(), kRtxCodecName) == 0) {
1732 int rtx_type = iter->id;
1733 int rtx_primary_type = -1;
1734 if (iter->GetParam(kCodecParamAssociatedPayloadType, &rtx_primary_type)) {
1735 primary_rtx_pt_mapping[rtx_primary_type] = rtx_type;
1736 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001737 } else if (engine()->CanSendCodec(*iter, current, &checked_codec)) {
1738 webrtc::VideoCodec wcodec;
1739 if (engine()->ConvertFromCricketVideoCodec(checked_codec, &wcodec)) {
1740 if (send_codecs.empty()) {
wu@webrtc.org05e7b442014-04-01 17:44:24 +00001741 nack_enabled = IsNackEnabled(checked_codec);
1742 remb_enabled = IsRembEnabled(checked_codec);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001743 }
1744 send_codecs.push_back(wcodec);
1745 }
1746 } else {
1747 LOG(LS_WARNING) << "Unknown codec " << iter->name;
1748 }
1749 }
1750
1751 // Fail if we don't have a match.
1752 if (send_codecs.empty()) {
1753 LOG(LS_WARNING) << "No matching codecs available";
1754 return false;
1755 }
1756
1757 // Recv protection.
wu@webrtc.org05e7b442014-04-01 17:44:24 +00001758 // Do not update if the status is same as previously configured.
1759 if (nack_enabled_ != nack_enabled) {
1760 for (RecvChannelMap::iterator it = recv_channels_.begin();
1761 it != recv_channels_.end(); ++it) {
1762 int channel_id = it->second->channel_id();
1763 if (!SetNackFec(channel_id, send_red_type_, send_fec_type_,
1764 nack_enabled)) {
1765 return false;
1766 }
1767 if (engine_->vie()->rtp()->SetRembStatus(channel_id,
1768 kNotSending,
1769 remb_enabled_) != 0) {
1770 LOG_RTCERR3(SetRembStatus, channel_id, kNotSending, remb_enabled_);
1771 return false;
1772 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001773 }
wu@webrtc.org05e7b442014-04-01 17:44:24 +00001774 nack_enabled_ = nack_enabled;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001775 }
1776
1777 // Send settings.
wu@webrtc.org05e7b442014-04-01 17:44:24 +00001778 // Do not update if the status is same as previously configured.
1779 if (remb_enabled_ != remb_enabled) {
1780 for (SendChannelMap::iterator iter = send_channels_.begin();
1781 iter != send_channels_.end(); ++iter) {
1782 int channel_id = iter->second->channel_id();
1783 if (!SetNackFec(channel_id, send_red_type_, send_fec_type_,
1784 nack_enabled_)) {
1785 return false;
1786 }
1787 if (engine_->vie()->rtp()->SetRembStatus(channel_id,
1788 remb_enabled,
1789 remb_enabled) != 0) {
1790 LOG_RTCERR3(SetRembStatus, channel_id, remb_enabled, remb_enabled);
1791 return false;
1792 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001793 }
wu@webrtc.org05e7b442014-04-01 17:44:24 +00001794 remb_enabled_ = remb_enabled;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001795 }
1796
1797 // Select the first matched codec.
1798 webrtc::VideoCodec& codec(send_codecs[0]);
1799
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00001800 // Set RTX payload type if primary now active. This value will be used in
1801 // SetSendCodec.
1802 std::map<int, int>::const_iterator rtx_it =
1803 primary_rtx_pt_mapping.find(static_cast<int>(codec.plType));
1804 if (rtx_it != primary_rtx_pt_mapping.end()) {
1805 send_rtx_type_ = rtx_it->second;
1806 }
1807
buildbot@webrtc.orgd1ae89f2014-05-08 19:19:26 +00001808 if (BitrateIsSet(codec.minBitrate) && BitrateIsSet(codec.maxBitrate) &&
1809 codec.minBitrate > codec.maxBitrate) {
1810 // TODO(pthatcher): This behavior contradicts other behavior in
1811 // this file which will cause min > max to push the min down to
1812 // the max. There are unit tests for both behaviors. We should
1813 // pick one and do that.
1814 LOG(LS_INFO) << "Rejecting codec with min bitrate ("
1815 << codec.minBitrate << ") larger than max ("
1816 << codec.maxBitrate << "). ";
1817 return false;
1818 }
1819
1820 if (!SetSendCodec(codec)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001821 return false;
1822 }
1823
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001824 LogSendCodecChange("SetSendCodecs()");
1825
1826 return true;
1827}
1828
1829bool WebRtcVideoMediaChannel::GetSendCodec(VideoCodec* send_codec) {
1830 if (!send_codec_) {
1831 return false;
1832 }
1833 ConvertToCricketVideoCodec(*send_codec_, send_codec);
1834 return true;
1835}
1836
1837bool WebRtcVideoMediaChannel::SetSendStreamFormat(uint32 ssrc,
1838 const VideoFormat& format) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001839 WebRtcVideoChannelSendInfo* send_channel = GetSendChannel(ssrc);
1840 if (!send_channel) {
1841 LOG(LS_ERROR) << "The specified ssrc " << ssrc << " is not in use.";
1842 return false;
1843 }
1844 send_channel->set_video_format(format);
1845 return true;
1846}
1847
1848bool WebRtcVideoMediaChannel::SetRender(bool render) {
1849 if (render == render_started_) {
1850 return true; // no action required
1851 }
1852
1853 bool ret = true;
1854 for (RecvChannelMap::iterator it = recv_channels_.begin();
1855 it != recv_channels_.end(); ++it) {
1856 if (render) {
1857 if (engine()->vie()->render()->StartRender(
1858 it->second->channel_id()) != 0) {
1859 LOG_RTCERR1(StartRender, it->second->channel_id());
1860 ret = false;
1861 }
1862 } else {
1863 if (engine()->vie()->render()->StopRender(
1864 it->second->channel_id()) != 0) {
1865 LOG_RTCERR1(StopRender, it->second->channel_id());
1866 ret = false;
1867 }
1868 }
1869 }
1870 if (ret) {
1871 render_started_ = render;
1872 }
1873
1874 return ret;
1875}
1876
1877bool WebRtcVideoMediaChannel::SetSend(bool send) {
1878 if (!HasReadySendChannels() && send) {
1879 LOG(LS_ERROR) << "No stream added";
1880 return false;
1881 }
1882 if (send == sending()) {
1883 return true; // No action required.
1884 }
1885
1886 if (send) {
1887 // We've been asked to start sending.
1888 // SetSendCodecs must have been called already.
1889 if (!send_codec_) {
1890 return false;
1891 }
1892 // Start send now.
1893 if (!StartSend()) {
1894 return false;
1895 }
1896 } else {
1897 // We've been asked to stop sending.
1898 if (!StopSend()) {
1899 return false;
1900 }
1901 }
1902 sending_ = send;
1903
1904 return true;
1905}
1906
1907bool WebRtcVideoMediaChannel::AddSendStream(const StreamParams& sp) {
henrike@webrtc.org6e3dbc22014-03-25 17:09:47 +00001908 if (sp.first_ssrc() == 0) {
1909 LOG(LS_ERROR) << "AddSendStream with 0 ssrc is not supported.";
1910 return false;
1911 }
1912
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001913 LOG(LS_INFO) << "AddSendStream " << sp.ToString();
1914
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00001915 if (!IsOneSsrcStream(sp) && !IsSimulcastStream(sp)) {
1916 LOG(LS_ERROR) << "AddSendStream: bad local stream parameters";
1917 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001918 }
1919
1920 uint32 ssrc_key;
1921 if (!CreateSendChannelKey(sp.first_ssrc(), &ssrc_key)) {
1922 LOG(LS_ERROR) << "Trying to register duplicate ssrc: " << sp.first_ssrc();
1923 return false;
1924 }
1925 // If the default channel is already used for sending create a new channel
1926 // otherwise use the default channel for sending.
1927 int channel_id = -1;
1928 if (send_channels_[0]->stream_params() == NULL) {
1929 channel_id = vie_channel_;
1930 } else {
1931 if (!CreateChannel(ssrc_key, MD_SEND, &channel_id)) {
1932 LOG(LS_ERROR) << "AddSendStream: unable to create channel";
1933 return false;
1934 }
1935 }
1936 WebRtcVideoChannelSendInfo* send_channel = send_channels_[ssrc_key];
1937 // Set the send (local) SSRC.
1938 // If there are multiple send SSRCs, we can only set the first one here, and
1939 // the rest of the SSRC(s) need to be set after SetSendCodec has been called
1940 // (with a codec requires multiple SSRC(s)).
1941 if (engine()->vie()->rtp()->SetLocalSSRC(channel_id,
1942 sp.first_ssrc()) != 0) {
1943 LOG_RTCERR2(SetLocalSSRC, channel_id, sp.first_ssrc());
1944 return false;
1945 }
1946
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00001947 // Set the corresponding RTX SSRC.
1948 if (!SetLocalRtxSsrc(channel_id, sp, sp.first_ssrc(), 0)) {
1949 return false;
1950 }
1951
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001952 // Set RTCP CName.
1953 if (engine()->vie()->rtp()->SetRTCPCName(channel_id,
1954 sp.cname.c_str()) != 0) {
1955 LOG_RTCERR2(SetRTCPCName, channel_id, sp.cname.c_str());
1956 return false;
1957 }
1958
1959 // At this point the channel's local SSRC has been updated. If the channel is
1960 // the default channel make sure that all the receive channels are updated as
1961 // well. Receive channels have to have the same SSRC as the default channel in
1962 // order to send receiver reports with this SSRC.
1963 if (IsDefaultChannel(channel_id)) {
1964 for (RecvChannelMap::const_iterator it = recv_channels_.begin();
1965 it != recv_channels_.end(); ++it) {
1966 WebRtcVideoChannelRecvInfo* info = it->second;
1967 int channel_id = info->channel_id();
1968 if (engine()->vie()->rtp()->SetLocalSSRC(channel_id,
1969 sp.first_ssrc()) != 0) {
1970 LOG_RTCERR1(SetLocalSSRC, it->first);
1971 return false;
1972 }
1973 }
1974 }
1975
1976 send_channel->set_stream_params(sp);
1977
1978 // Reset send codec after stream parameters changed.
1979 if (send_codec_) {
buildbot@webrtc.orgd1ae89f2014-05-08 19:19:26 +00001980 if (!SetSendCodec(send_channel, *send_codec_)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001981 return false;
1982 }
1983 LogSendCodecChange("SetSendStreamFormat()");
1984 }
1985
1986 if (sending_) {
1987 return StartSend(send_channel);
1988 }
1989 return true;
1990}
1991
1992bool WebRtcVideoMediaChannel::RemoveSendStream(uint32 ssrc) {
henrike@webrtc.org6e3dbc22014-03-25 17:09:47 +00001993 if (ssrc == 0) {
1994 LOG(LS_ERROR) << "RemoveSendStream with 0 ssrc is not supported.";
1995 return false;
1996 }
1997
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001998 uint32 ssrc_key;
1999 if (!GetSendChannelKey(ssrc, &ssrc_key)) {
2000 LOG(LS_WARNING) << "Try to remove stream with ssrc " << ssrc
2001 << " which doesn't exist.";
2002 return false;
2003 }
2004 WebRtcVideoChannelSendInfo* send_channel = send_channels_[ssrc_key];
2005 int channel_id = send_channel->channel_id();
2006 if (IsDefaultChannel(channel_id) && (send_channel->stream_params() == NULL)) {
2007 // Default channel will still exist. However, if stream_params() is NULL
2008 // there is no stream to remove.
2009 return false;
2010 }
2011 if (sending_) {
2012 StopSend(send_channel);
2013 }
2014
2015 const WebRtcVideoChannelSendInfo::EncoderMap& encoder_map =
2016 send_channel->registered_encoders();
2017 for (WebRtcVideoChannelSendInfo::EncoderMap::const_iterator it =
2018 encoder_map.begin(); it != encoder_map.end(); ++it) {
2019 if (engine()->vie()->ext_codec()->DeRegisterExternalSendCodec(
2020 channel_id, it->first) != 0) {
2021 LOG_RTCERR1(DeregisterEncoderObserver, channel_id);
2022 }
2023 engine()->DestroyExternalEncoder(it->second);
2024 }
2025 send_channel->ClearRegisteredEncoders();
2026
2027 // The receive channels depend on the default channel, recycle it instead.
2028 if (IsDefaultChannel(channel_id)) {
2029 SetCapturer(GetDefaultChannelSsrc(), NULL);
2030 send_channel->ClearStreamParams();
2031 } else {
2032 return DeleteSendChannel(ssrc_key);
2033 }
2034 return true;
2035}
2036
2037bool WebRtcVideoMediaChannel::AddRecvStream(const StreamParams& sp) {
henrike@webrtc.org6e3dbc22014-03-25 17:09:47 +00002038 if (sp.first_ssrc() == 0) {
2039 LOG(LS_ERROR) << "AddRecvStream with 0 ssrc is not supported.";
2040 return false;
2041 }
2042
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002043 // TODO(zhurunz) Remove this once BWE works properly across different send
2044 // and receive channels.
2045 // Reuse default channel for recv stream in 1:1 call.
2046 if (!InConferenceMode() && first_receive_ssrc_ == 0) {
2047 LOG(LS_INFO) << "Recv stream " << sp.first_ssrc()
2048 << " reuse default channel #"
2049 << vie_channel_;
2050 first_receive_ssrc_ = sp.first_ssrc();
2051 if (render_started_) {
2052 if (engine()->vie()->render()->StartRender(vie_channel_) !=0) {
2053 LOG_RTCERR1(StartRender, vie_channel_);
2054 }
2055 }
2056 return true;
2057 }
2058
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002059 int channel_id = -1;
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00002060 RecvChannelMap::iterator channel_iterator =
2061 recv_channels_.find(sp.first_ssrc());
2062 if (channel_iterator == recv_channels_.end() &&
2063 first_receive_ssrc_ != sp.first_ssrc()) {
2064 // TODO(perkj): Implement recv media from multiple media SSRCs per stream.
2065 // NOTE: We have two SSRCs per stream when RTX is enabled.
2066 if (!IsOneSsrcStream(sp)) {
2067 LOG(LS_ERROR) << "WebRtcVideoMediaChannel supports one primary SSRC per"
2068 << " stream and one FID SSRC per primary SSRC.";
2069 return false;
2070 }
2071
2072 // Create a new channel for receiving video data.
2073 // In order to get the bandwidth estimation work fine for
2074 // receive only channels, we connect all receiving channels
2075 // to our master send channel.
2076 if (!CreateChannel(sp.first_ssrc(), MD_RECV, &channel_id)) {
2077 return false;
2078 }
2079 } else {
2080 // Already exists.
2081 if (first_receive_ssrc_ == sp.first_ssrc()) {
2082 return false;
2083 }
2084 // Early receive added channel.
2085 channel_id = (*channel_iterator).second->channel_id();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002086 }
buildbot@webrtc.orgdd4742a2014-05-07 14:50:35 +00002087 channel_iterator = recv_channels_.find(sp.first_ssrc());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002088
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002089 // Set the corresponding RTX SSRC.
2090 uint32 rtx_ssrc;
2091 bool has_rtx = sp.GetFidSsrc(sp.first_ssrc(), &rtx_ssrc);
buildbot@webrtc.orgdd4742a2014-05-07 14:50:35 +00002092 if (has_rtx) {
2093 LOG(LS_INFO) << "Setting rtx ssrc " << rtx_ssrc << " for stream "
2094 << sp.first_ssrc();
2095 if (engine()->vie()->rtp()->SetRemoteSSRCType(
2096 channel_id, webrtc::kViEStreamTypeRtx, rtx_ssrc) != 0) {
2097 LOG_RTCERR3(SetRemoteSSRCType, channel_id, webrtc::kViEStreamTypeRtx,
2098 rtx_ssrc);
2099 return false;
2100 }
2101 rtx_to_primary_ssrc_[rtx_ssrc] = sp.first_ssrc();
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002102 }
2103
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002104 // Get the default renderer.
2105 VideoRenderer* default_renderer = NULL;
2106 if (InConferenceMode()) {
2107 // The recv_channels_ size start out being 1, so if it is two here this
2108 // is the first receive channel created (vie_channel_ is not used for
2109 // receiving in a conference call). This means that the renderer stored
2110 // inside vie_channel_ should be used for the just created channel.
2111 if (recv_channels_.size() == 2 &&
2112 recv_channels_.find(0) != recv_channels_.end()) {
2113 GetRenderer(0, &default_renderer);
2114 }
2115 }
2116
2117 // The first recv stream reuses the default renderer (if a default renderer
2118 // has been set).
2119 if (default_renderer) {
2120 SetRenderer(sp.first_ssrc(), default_renderer);
2121 }
2122
2123 LOG(LS_INFO) << "New video stream " << sp.first_ssrc()
2124 << " registered to VideoEngine channel #"
2125 << channel_id << " and connected to channel #" << vie_channel_;
2126
2127 return true;
2128}
2129
2130bool WebRtcVideoMediaChannel::RemoveRecvStream(uint32 ssrc) {
henrike@webrtc.org6e3dbc22014-03-25 17:09:47 +00002131 if (ssrc == 0) {
2132 LOG(LS_ERROR) << "RemoveRecvStream with 0 ssrc is not supported.";
2133 return false;
2134 }
2135 return RemoveRecvStreamInternal(ssrc);
2136}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002137
henrike@webrtc.org6e3dbc22014-03-25 17:09:47 +00002138bool WebRtcVideoMediaChannel::RemoveRecvStreamInternal(uint32 ssrc) {
2139 RecvChannelMap::iterator it = recv_channels_.find(ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002140 if (it == recv_channels_.end()) {
2141 // TODO(perkj): Remove this once BWE works properly across different send
2142 // and receive channels.
2143 // The default channel is reused for recv stream in 1:1 call.
2144 if (first_receive_ssrc_ == ssrc) {
2145 first_receive_ssrc_ = 0;
2146 // Need to stop the renderer and remove it since the render window can be
2147 // deleted after this.
2148 if (render_started_) {
2149 if (engine()->vie()->render()->StopRender(vie_channel_) !=0) {
2150 LOG_RTCERR1(StopRender, it->second->channel_id());
2151 }
2152 }
2153 recv_channels_[0]->SetRenderer(NULL);
2154 return true;
2155 }
2156 return false;
2157 }
2158 WebRtcVideoChannelRecvInfo* info = it->second;
buildbot@webrtc.orgdd4742a2014-05-07 14:50:35 +00002159
2160 // Remove any RTX SSRC mappings to this stream.
2161 SsrcMap::iterator rtx_it = rtx_to_primary_ssrc_.begin();
2162 while (rtx_it != rtx_to_primary_ssrc_.end()) {
2163 if (rtx_it->second == ssrc) {
2164 rtx_to_primary_ssrc_.erase(rtx_it++);
2165 } else {
2166 ++rtx_it;
2167 }
2168 }
2169
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002170 int channel_id = info->channel_id();
2171 if (engine()->vie()->render()->RemoveRenderer(channel_id) != 0) {
2172 LOG_RTCERR1(RemoveRenderer, channel_id);
2173 }
2174
2175 if (engine()->vie()->network()->DeregisterSendTransport(channel_id) !=0) {
2176 LOG_RTCERR1(DeRegisterSendTransport, channel_id);
2177 }
2178
2179 if (engine()->vie()->codec()->DeregisterDecoderObserver(
2180 channel_id) != 0) {
2181 LOG_RTCERR1(DeregisterDecoderObserver, channel_id);
2182 }
2183
2184 const WebRtcVideoChannelRecvInfo::DecoderMap& decoder_map =
2185 info->registered_decoders();
2186 for (WebRtcVideoChannelRecvInfo::DecoderMap::const_iterator it =
2187 decoder_map.begin(); it != decoder_map.end(); ++it) {
2188 if (engine()->vie()->ext_codec()->DeRegisterExternalReceiveCodec(
2189 channel_id, it->first) != 0) {
2190 LOG_RTCERR1(DeregisterDecoderObserver, channel_id);
2191 }
2192 engine()->DestroyExternalDecoder(it->second);
2193 }
2194 info->ClearRegisteredDecoders();
2195
2196 LOG(LS_INFO) << "Removing video stream " << ssrc
2197 << " with VideoEngine channel #"
2198 << channel_id;
wu@webrtc.org9caf2762013-12-11 18:25:07 +00002199 bool ret = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002200 if (engine()->vie()->base()->DeleteChannel(channel_id) == -1) {
2201 LOG_RTCERR1(DeleteChannel, channel_id);
wu@webrtc.org9caf2762013-12-11 18:25:07 +00002202 ret = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002203 }
2204 // Delete the WebRtcVideoChannelRecvInfo pointed to by it->second.
2205 delete info;
2206 recv_channels_.erase(it);
wu@webrtc.org9caf2762013-12-11 18:25:07 +00002207 return ret;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002208}
2209
2210bool WebRtcVideoMediaChannel::StartSend() {
2211 bool success = true;
2212 for (SendChannelMap::iterator iter = send_channels_.begin();
2213 iter != send_channels_.end(); ++iter) {
2214 WebRtcVideoChannelSendInfo* send_channel = iter->second;
2215 if (!StartSend(send_channel)) {
2216 success = false;
2217 }
2218 }
2219 return success;
2220}
2221
2222bool WebRtcVideoMediaChannel::StartSend(
2223 WebRtcVideoChannelSendInfo* send_channel) {
2224 const int channel_id = send_channel->channel_id();
2225 if (engine()->vie()->base()->StartSend(channel_id) != 0) {
2226 LOG_RTCERR1(StartSend, channel_id);
2227 return false;
2228 }
2229
2230 send_channel->set_sending(true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002231 return true;
2232}
2233
2234bool WebRtcVideoMediaChannel::StopSend() {
2235 bool success = true;
2236 for (SendChannelMap::iterator iter = send_channels_.begin();
2237 iter != send_channels_.end(); ++iter) {
2238 WebRtcVideoChannelSendInfo* send_channel = iter->second;
2239 if (!StopSend(send_channel)) {
2240 success = false;
2241 }
2242 }
2243 return success;
2244}
2245
2246bool WebRtcVideoMediaChannel::StopSend(
2247 WebRtcVideoChannelSendInfo* send_channel) {
2248 const int channel_id = send_channel->channel_id();
2249 if (engine()->vie()->base()->StopSend(channel_id) != 0) {
2250 LOG_RTCERR1(StopSend, channel_id);
2251 return false;
2252 }
2253 send_channel->set_sending(false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002254 return true;
2255}
2256
2257bool WebRtcVideoMediaChannel::SendIntraFrame() {
2258 bool success = true;
2259 for (SendChannelMap::iterator iter = send_channels_.begin();
2260 iter != send_channels_.end();
2261 ++iter) {
2262 WebRtcVideoChannelSendInfo* send_channel = iter->second;
2263 const int channel_id = send_channel->channel_id();
2264 if (engine()->vie()->codec()->SendKeyFrame(channel_id) != 0) {
2265 LOG_RTCERR1(SendKeyFrame, channel_id);
2266 success = false;
2267 }
2268 }
2269 return success;
2270}
2271
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002272bool WebRtcVideoMediaChannel::HasReadySendChannels() {
2273 return !send_channels_.empty() &&
2274 ((send_channels_.size() > 1) ||
2275 (send_channels_[0]->stream_params() != NULL));
2276}
2277
2278bool WebRtcVideoMediaChannel::GetSendChannelKey(uint32 local_ssrc,
2279 uint32* key) {
2280 *key = 0;
2281 // If a send channel is not ready to send it will not have local_ssrc
2282 // registered to it.
2283 if (!HasReadySendChannels()) {
2284 return false;
2285 }
2286 // The default channel is stored with key 0. The key therefore does not match
2287 // the SSRC associated with the default channel. Check if the SSRC provided
2288 // corresponds to the default channel's SSRC.
2289 if (local_ssrc == GetDefaultChannelSsrc()) {
2290 return true;
2291 }
2292 if (send_channels_.find(local_ssrc) == send_channels_.end()) {
2293 for (SendChannelMap::iterator iter = send_channels_.begin();
2294 iter != send_channels_.end(); ++iter) {
2295 WebRtcVideoChannelSendInfo* send_channel = iter->second;
2296 if (send_channel->has_ssrc(local_ssrc)) {
2297 *key = iter->first;
2298 return true;
2299 }
2300 }
2301 return false;
2302 }
2303 // The key was found in the above std::map::find call. This means that the
2304 // ssrc is the key.
2305 *key = local_ssrc;
2306 return true;
2307}
2308
2309WebRtcVideoChannelSendInfo* WebRtcVideoMediaChannel::GetSendChannel(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002310 uint32 local_ssrc) {
2311 uint32 key;
2312 if (!GetSendChannelKey(local_ssrc, &key)) {
2313 return NULL;
2314 }
2315 return send_channels_[key];
2316}
2317
2318bool WebRtcVideoMediaChannel::CreateSendChannelKey(uint32 local_ssrc,
2319 uint32* key) {
2320 if (GetSendChannelKey(local_ssrc, key)) {
2321 // If there is a key corresponding to |local_ssrc|, the SSRC is already in
2322 // use. SSRCs need to be unique in a session and at this point a duplicate
2323 // SSRC has been detected.
2324 return false;
2325 }
2326 if (send_channels_[0]->stream_params() == NULL) {
2327 // key should be 0 here as the default channel should be re-used whenever it
2328 // is not used.
2329 *key = 0;
2330 return true;
2331 }
2332 // SSRC is currently not in use and the default channel is already in use. Use
2333 // the SSRC as key since it is supposed to be unique in a session.
2334 *key = local_ssrc;
2335 return true;
2336}
2337
wu@webrtc.org24301a62013-12-13 19:17:43 +00002338int WebRtcVideoMediaChannel::GetSendChannelNum(VideoCapturer* capturer) {
2339 int num = 0;
2340 for (SendChannelMap::iterator iter = send_channels_.begin();
2341 iter != send_channels_.end(); ++iter) {
2342 WebRtcVideoChannelSendInfo* send_channel = iter->second;
2343 if (send_channel->video_capturer() == capturer) {
2344 ++num;
2345 }
2346 }
2347 return num;
2348}
2349
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002350uint32 WebRtcVideoMediaChannel::GetDefaultChannelSsrc() {
2351 WebRtcVideoChannelSendInfo* send_channel = send_channels_[0];
2352 const StreamParams* sp = send_channel->stream_params();
2353 if (sp == NULL) {
2354 // This happens if no send stream is currently registered.
2355 return 0;
2356 }
2357 return sp->first_ssrc();
2358}
2359
2360bool WebRtcVideoMediaChannel::DeleteSendChannel(uint32 ssrc_key) {
2361 if (send_channels_.find(ssrc_key) == send_channels_.end()) {
2362 return false;
2363 }
2364 WebRtcVideoChannelSendInfo* send_channel = send_channels_[ssrc_key];
wu@webrtc.org24301a62013-12-13 19:17:43 +00002365 MaybeDisconnectCapturer(send_channel->video_capturer());
henrike@webrtc.orgc7bec842014-03-12 19:53:43 +00002366 send_channel->set_video_capturer(NULL, engine()->vie());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002367
2368 int channel_id = send_channel->channel_id();
2369 int capture_id = send_channel->capture_id();
2370 if (engine()->vie()->codec()->DeregisterEncoderObserver(
2371 channel_id) != 0) {
2372 LOG_RTCERR1(DeregisterEncoderObserver, channel_id);
2373 }
2374
2375 // Destroy the external capture interface.
2376 if (engine()->vie()->capture()->DisconnectCaptureDevice(
2377 channel_id) != 0) {
2378 LOG_RTCERR1(DisconnectCaptureDevice, channel_id);
2379 }
2380 if (engine()->vie()->capture()->ReleaseCaptureDevice(
2381 capture_id) != 0) {
2382 LOG_RTCERR1(ReleaseCaptureDevice, capture_id);
2383 }
2384
2385 // The default channel is stored in both |send_channels_| and
2386 // |recv_channels_|. To make sure it is only deleted once from vie let the
2387 // delete call happen when tearing down |recv_channels_| and not here.
2388 if (!IsDefaultChannel(channel_id)) {
2389 engine_->vie()->base()->DeleteChannel(channel_id);
2390 }
2391 delete send_channel;
2392 send_channels_.erase(ssrc_key);
2393 return true;
2394}
2395
2396bool WebRtcVideoMediaChannel::RemoveCapturer(uint32 ssrc) {
2397 WebRtcVideoChannelSendInfo* send_channel = GetSendChannel(ssrc);
2398 if (!send_channel) {
2399 return false;
2400 }
2401 VideoCapturer* capturer = send_channel->video_capturer();
2402 if (capturer == NULL) {
2403 return false;
2404 }
wu@webrtc.org24301a62013-12-13 19:17:43 +00002405 MaybeDisconnectCapturer(capturer);
henrike@webrtc.orgc7bec842014-03-12 19:53:43 +00002406 send_channel->set_video_capturer(NULL, engine()->vie());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002407 const int64 timestamp = send_channel->local_stream_info()->time_stamp();
2408 if (send_codec_) {
2409 QueueBlackFrame(ssrc, timestamp, send_codec_->maxFramerate);
2410 }
2411 return true;
2412}
2413
2414bool WebRtcVideoMediaChannel::SetRenderer(uint32 ssrc,
2415 VideoRenderer* renderer) {
2416 if (recv_channels_.find(ssrc) == recv_channels_.end()) {
2417 // TODO(perkj): Remove this once BWE works properly across different send
2418 // and receive channels.
2419 // The default channel is reused for recv stream in 1:1 call.
2420 if (first_receive_ssrc_ == ssrc &&
2421 recv_channels_.find(0) != recv_channels_.end()) {
2422 LOG(LS_INFO) << "SetRenderer " << ssrc
2423 << " reuse default channel #"
2424 << vie_channel_;
2425 recv_channels_[0]->SetRenderer(renderer);
2426 return true;
2427 }
2428 return false;
2429 }
2430
2431 recv_channels_[ssrc]->SetRenderer(renderer);
2432 return true;
2433}
2434
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00002435bool WebRtcVideoMediaChannel::GetStats(const StatsOptions& options,
2436 VideoMediaInfo* info) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002437 // Get sender statistics and build VideoSenderInfo.
2438 unsigned int total_bitrate_sent = 0;
2439 unsigned int video_bitrate_sent = 0;
2440 unsigned int fec_bitrate_sent = 0;
2441 unsigned int nack_bitrate_sent = 0;
2442 unsigned int estimated_send_bandwidth = 0;
2443 unsigned int target_enc_bitrate = 0;
2444 if (send_codec_) {
2445 for (SendChannelMap::const_iterator iter = send_channels_.begin();
2446 iter != send_channels_.end(); ++iter) {
2447 WebRtcVideoChannelSendInfo* send_channel = iter->second;
2448 const int channel_id = send_channel->channel_id();
2449 VideoSenderInfo sinfo;
2450 const StreamParams* send_params = send_channel->stream_params();
2451 if (send_params == NULL) {
2452 // This should only happen if the default vie channel is not in use.
2453 // This can happen if no streams have ever been added or the stream
2454 // corresponding to the default channel has been removed. Note that
2455 // there may be non-default vie channels in use when this happen so
2456 // asserting send_channels_.size() == 1 is not correct and neither is
2457 // breaking out of the loop.
2458 ASSERT(channel_id == vie_channel_);
2459 continue;
2460 }
2461 unsigned int bytes_sent, packets_sent, bytes_recv, packets_recv;
2462 if (engine_->vie()->rtp()->GetRTPStatistics(channel_id, bytes_sent,
2463 packets_sent, bytes_recv,
2464 packets_recv) != 0) {
2465 LOG_RTCERR1(GetRTPStatistics, vie_channel_);
2466 continue;
2467 }
2468 WebRtcLocalStreamInfo* channel_stream_info =
2469 send_channel->local_stream_info();
2470
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002471 for (size_t i = 0; i < send_params->ssrcs.size(); ++i) {
2472 sinfo.add_ssrc(send_params->ssrcs[i]);
2473 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002474 sinfo.codec_name = send_codec_->plName;
2475 sinfo.bytes_sent = bytes_sent;
2476 sinfo.packets_sent = packets_sent;
2477 sinfo.packets_cached = -1;
2478 sinfo.packets_lost = -1;
2479 sinfo.fraction_lost = -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002480 sinfo.rtt_ms = -1;
wu@webrtc.org987f2c92014-03-28 16:22:19 +00002481
2482 VideoCapturer* video_capturer = send_channel->video_capturer();
2483 if (video_capturer) {
buildbot@webrtc.org0b53bd22014-05-06 17:12:36 +00002484 VideoFormat last_captured_frame_format;
wu@webrtc.org987f2c92014-03-28 16:22:19 +00002485 video_capturer->GetStats(&sinfo.adapt_frame_drops,
2486 &sinfo.effects_frame_drops,
buildbot@webrtc.org0b53bd22014-05-06 17:12:36 +00002487 &sinfo.capturer_frame_time,
2488 &last_captured_frame_format);
2489 sinfo.input_frame_width = last_captured_frame_format.width;
2490 sinfo.input_frame_height = last_captured_frame_format.height;
2491 } else {
2492 sinfo.input_frame_width = 0;
2493 sinfo.input_frame_height = 0;
wu@webrtc.org987f2c92014-03-28 16:22:19 +00002494 }
2495
2496 webrtc::VideoCodec vie_codec;
wu@webrtc.org987f2c92014-03-28 16:22:19 +00002497 if (!video_capturer || video_capturer->IsMuted()) {
2498 sinfo.send_frame_width = 0;
2499 sinfo.send_frame_height = 0;
2500 } else if (engine()->vie()->codec()->GetSendCodec(channel_id,
2501 vie_codec) == 0) {
2502 sinfo.send_frame_width = vie_codec.width;
2503 sinfo.send_frame_height = vie_codec.height;
2504 } else {
2505 sinfo.send_frame_width = -1;
2506 sinfo.send_frame_height = -1;
2507 LOG_RTCERR1(GetSendCodec, channel_id);
2508 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002509 sinfo.framerate_input = channel_stream_info->framerate();
2510 sinfo.framerate_sent = send_channel->encoder_observer()->framerate();
2511 sinfo.nominal_bitrate = send_channel->encoder_observer()->bitrate();
buildbot@webrtc.orgd1ae89f2014-05-08 19:19:26 +00002512 if (send_codec_) {
2513 sinfo.preferred_bitrate = GetBitrate(
2514 send_codec_->maxBitrate, kMaxVideoBitrate);
2515 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002516 sinfo.adapt_reason = send_channel->CurrentAdaptReason();
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002517 sinfo.capture_jitter_ms = -1;
2518 sinfo.avg_encode_ms = -1;
wu@webrtc.org9caf2762013-12-11 18:25:07 +00002519 sinfo.encode_usage_percent = -1;
2520 sinfo.capture_queue_delay_ms_per_s = -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002521
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002522 int capture_jitter_ms = 0;
2523 int avg_encode_time_ms = 0;
wu@webrtc.org9caf2762013-12-11 18:25:07 +00002524 int encode_usage_percent = 0;
2525 int capture_queue_delay_ms_per_s = 0;
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002526 if (engine()->vie()->base()->CpuOveruseMeasures(
wu@webrtc.org9caf2762013-12-11 18:25:07 +00002527 channel_id,
2528 &capture_jitter_ms,
2529 &avg_encode_time_ms,
2530 &encode_usage_percent,
2531 &capture_queue_delay_ms_per_s) == 0) {
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002532 sinfo.capture_jitter_ms = capture_jitter_ms;
2533 sinfo.avg_encode_ms = avg_encode_time_ms;
wu@webrtc.org9caf2762013-12-11 18:25:07 +00002534 sinfo.encode_usage_percent = encode_usage_percent;
2535 sinfo.capture_queue_delay_ms_per_s = capture_queue_delay_ms_per_s;
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002536 }
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002537
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00002538 webrtc::RtcpPacketTypeCounter rtcp_sent;
2539 webrtc::RtcpPacketTypeCounter rtcp_received;
2540 if (engine()->vie()->rtp()->GetRtcpPacketTypeCounters(
2541 channel_id, &rtcp_sent, &rtcp_received) == 0) {
2542 sinfo.firs_rcvd = rtcp_received.fir_packets;
2543 sinfo.plis_rcvd = rtcp_received.pli_packets;
2544 sinfo.nacks_rcvd = rtcp_received.nack_packets;
2545 } else {
2546 sinfo.firs_rcvd = -1;
2547 sinfo.plis_rcvd = -1;
2548 sinfo.nacks_rcvd = -1;
2549 LOG_RTCERR1(GetRtcpPacketTypeCounters, channel_id);
2550 }
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00002551
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002552 // Get received RTCP statistics for the sender (reported by the remote
2553 // client in a RTCP packet), if available.
2554 // It's not a fatal error if we can't, since RTCP may not have arrived
2555 // yet.
2556 webrtc::RtcpStatistics outgoing_stream_rtcp_stats;
2557 int outgoing_stream_rtt_ms;
2558
2559 if (engine_->vie()->rtp()->GetSendChannelRtcpStatistics(
2560 channel_id,
2561 outgoing_stream_rtcp_stats,
2562 outgoing_stream_rtt_ms) == 0) {
2563 // Convert Q8 to float.
2564 sinfo.packets_lost = outgoing_stream_rtcp_stats.cumulative_lost;
2565 sinfo.fraction_lost = static_cast<float>(
2566 outgoing_stream_rtcp_stats.fraction_lost) / (1 << 8);
2567 sinfo.rtt_ms = outgoing_stream_rtt_ms;
2568 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002569 info->senders.push_back(sinfo);
2570
2571 unsigned int channel_total_bitrate_sent = 0;
2572 unsigned int channel_video_bitrate_sent = 0;
2573 unsigned int channel_fec_bitrate_sent = 0;
2574 unsigned int channel_nack_bitrate_sent = 0;
2575 if (engine_->vie()->rtp()->GetBandwidthUsage(
2576 channel_id, channel_total_bitrate_sent, channel_video_bitrate_sent,
2577 channel_fec_bitrate_sent, channel_nack_bitrate_sent) == 0) {
2578 total_bitrate_sent += channel_total_bitrate_sent;
2579 video_bitrate_sent += channel_video_bitrate_sent;
2580 fec_bitrate_sent += channel_fec_bitrate_sent;
2581 nack_bitrate_sent += channel_nack_bitrate_sent;
2582 } else {
2583 LOG_RTCERR1(GetBandwidthUsage, channel_id);
2584 }
2585
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002586 unsigned int target_enc_stream_bitrate = 0;
2587 if (engine_->vie()->codec()->GetCodecTargetBitrate(
2588 channel_id, &target_enc_stream_bitrate) == 0) {
2589 target_enc_bitrate += target_enc_stream_bitrate;
2590 } else {
2591 LOG_RTCERR1(GetCodecTargetBitrate, channel_id);
2592 }
2593 }
buildbot@webrtc.orga18b4c92014-05-06 17:48:14 +00002594 if (!send_channels_.empty()) {
2595 // GetEstimatedSendBandwidth returns the estimated bandwidth for all video
2596 // engine channels in a channel group. Any valid channel id will do as it
2597 // is only used to access the right group of channels.
2598 const int channel_id = send_channels_.begin()->second->channel_id();
2599 // Get the send bandwidth available for this MediaChannel.
2600 if (engine_->vie()->rtp()->GetEstimatedSendBandwidth(
2601 channel_id, &estimated_send_bandwidth) != 0) {
2602 LOG_RTCERR1(GetEstimatedSendBandwidth, channel_id);
2603 }
2604 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002605 } else {
2606 LOG(LS_WARNING) << "GetStats: sender information not ready.";
2607 }
2608
2609 // Get the SSRC and stats for each receiver, based on our own calculations.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002610 for (RecvChannelMap::const_iterator it = recv_channels_.begin();
2611 it != recv_channels_.end(); ++it) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002612 WebRtcVideoChannelRecvInfo* channel = it->second;
2613
buildbot@webrtc.orgeaf2bd92014-05-12 23:12:19 +00002614 unsigned int ssrc = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002615 // Get receiver statistics and build VideoReceiverInfo, if we have data.
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +00002616 // Skip the default channel (ssrc == 0).
2617 if (engine_->vie()->rtp()->GetRemoteSSRC(
2618 channel->channel_id(), ssrc) != 0 ||
2619 ssrc == 0)
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002620 continue;
2621
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002622 webrtc::StreamDataCounters sent;
2623 webrtc::StreamDataCounters received;
2624 if (engine_->vie()->rtp()->GetRtpStatistics(channel->channel_id(),
2625 sent, received) != 0) {
2626 LOG_RTCERR1(GetRTPStatistics, channel->channel_id());
2627 return false;
2628 }
2629 VideoReceiverInfo rinfo;
2630 rinfo.add_ssrc(ssrc);
2631 rinfo.bytes_rcvd = received.bytes;
2632 rinfo.packets_rcvd = received.packets;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002633 rinfo.packets_lost = -1;
2634 rinfo.packets_concealed = -1;
2635 rinfo.fraction_lost = -1; // from SentRTCP
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002636 rinfo.frame_width = channel->render_adapter()->width();
2637 rinfo.frame_height = channel->render_adapter()->height();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002638 int fps = channel->render_adapter()->framerate();
2639 rinfo.framerate_decoded = fps;
2640 rinfo.framerate_output = fps;
buildbot@webrtc.org0581f0b2014-05-06 21:36:31 +00002641 rinfo.capture_start_ntp_time_ms =
2642 channel->render_adapter()->capture_start_ntp_time_ms();
wu@webrtc.org97077a32013-10-25 21:18:33 +00002643 channel->decoder_observer()->ExportTo(&rinfo);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002644
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00002645 webrtc::RtcpPacketTypeCounter rtcp_sent;
2646 webrtc::RtcpPacketTypeCounter rtcp_received;
2647 if (engine()->vie()->rtp()->GetRtcpPacketTypeCounters(
2648 channel->channel_id(), &rtcp_sent, &rtcp_received) == 0) {
2649 rinfo.firs_sent = rtcp_sent.fir_packets;
2650 rinfo.plis_sent = rtcp_sent.pli_packets;
2651 rinfo.nacks_sent = rtcp_sent.nack_packets;
2652 } else {
2653 rinfo.firs_sent = -1;
2654 rinfo.plis_sent = -1;
2655 rinfo.nacks_sent = -1;
2656 LOG_RTCERR1(GetRtcpPacketTypeCounters, channel->channel_id());
2657 }
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00002658
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002659 // Get our locally created statistics of the received RTP stream.
2660 webrtc::RtcpStatistics incoming_stream_rtcp_stats;
2661 int incoming_stream_rtt_ms;
2662 if (engine_->vie()->rtp()->GetReceiveChannelRtcpStatistics(
2663 channel->channel_id(),
2664 incoming_stream_rtcp_stats,
2665 incoming_stream_rtt_ms) == 0) {
2666 // Convert Q8 to float.
2667 rinfo.packets_lost = incoming_stream_rtcp_stats.cumulative_lost;
2668 rinfo.fraction_lost = static_cast<float>(
2669 incoming_stream_rtcp_stats.fraction_lost) / (1 << 8);
2670 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002671 info->receivers.push_back(rinfo);
buildbot@webrtc.orga18b4c92014-05-06 17:48:14 +00002672 }
2673 unsigned int estimated_recv_bandwidth = 0;
2674 if (!recv_channels_.empty()) {
2675 // GetEstimatedReceiveBandwidth returns the estimated bandwidth for all
2676 // video engine channels in a channel group. Any valid channel id will do as
2677 // it is only used to access the right group of channels.
2678 const int channel_id = recv_channels_.begin()->second->channel_id();
2679 // Gets the estimated receive bandwidth for the MediaChannel.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002680 if (engine_->vie()->rtp()->GetEstimatedReceiveBandwidth(
buildbot@webrtc.orga18b4c92014-05-06 17:48:14 +00002681 channel_id, &estimated_recv_bandwidth) != 0) {
2682 LOG_RTCERR1(GetEstimatedReceiveBandwidth, channel_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002683 }
2684 }
buildbot@webrtc.orga18b4c92014-05-06 17:48:14 +00002685
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002686 // Build BandwidthEstimationInfo.
2687 // TODO(zhurunz): Add real unittest for this.
2688 BandwidthEstimationInfo bwe;
2689
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00002690 // TODO(jiayl): remove the condition when the necessary changes are available
2691 // outside the dev branch.
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00002692 if (options.include_received_propagation_stats) {
2693 webrtc::ReceiveBandwidthEstimatorStats additional_stats;
2694 // Only call for the default channel because the returned stats are
2695 // collected for all the channels using the same estimator.
2696 if (engine_->vie()->rtp()->GetReceiveBandwidthEstimatorStats(
mallinath@webrtc.org92fdfeb2014-02-17 18:49:41 +00002697 recv_channels_[0]->channel_id(), &additional_stats) == 0) {
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00002698 bwe.total_received_propagation_delta_ms =
2699 additional_stats.total_propagation_time_delta_ms;
2700 bwe.recent_received_propagation_delta_ms.swap(
2701 additional_stats.recent_propagation_time_delta_ms);
2702 bwe.recent_received_packet_group_arrival_time_ms.swap(
2703 additional_stats.recent_arrival_time_ms);
2704 }
2705 }
henrike@webrtc.orgb8395eb2014-02-28 21:57:22 +00002706
2707 engine_->vie()->rtp()->GetPacerQueuingDelayMs(
2708 recv_channels_[0]->channel_id(), &bwe.bucket_delay);
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00002709
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002710 // Calculations done above per send/receive stream.
2711 bwe.actual_enc_bitrate = video_bitrate_sent;
2712 bwe.transmit_bitrate = total_bitrate_sent;
2713 bwe.retransmit_bitrate = nack_bitrate_sent;
2714 bwe.available_send_bandwidth = estimated_send_bandwidth;
2715 bwe.available_recv_bandwidth = estimated_recv_bandwidth;
2716 bwe.target_enc_bitrate = target_enc_bitrate;
2717
2718 info->bw_estimations.push_back(bwe);
2719
2720 return true;
2721}
2722
2723bool WebRtcVideoMediaChannel::SetCapturer(uint32 ssrc,
2724 VideoCapturer* capturer) {
2725 ASSERT(ssrc != 0);
2726 if (!capturer) {
2727 return RemoveCapturer(ssrc);
2728 }
2729 WebRtcVideoChannelSendInfo* send_channel = GetSendChannel(ssrc);
2730 if (!send_channel) {
2731 return false;
2732 }
2733 VideoCapturer* old_capturer = send_channel->video_capturer();
wu@webrtc.org24301a62013-12-13 19:17:43 +00002734 MaybeDisconnectCapturer(old_capturer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002735
henrike@webrtc.orgc7bec842014-03-12 19:53:43 +00002736 send_channel->set_video_capturer(capturer, engine()->vie());
wu@webrtc.orga8910d22014-01-23 22:12:45 +00002737 MaybeConnectCapturer(capturer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002738 if (!capturer->IsScreencast() && ratio_w_ != 0 && ratio_h_ != 0) {
2739 capturer->UpdateAspectRatio(ratio_w_, ratio_h_);
2740 }
2741 const int64 timestamp = send_channel->local_stream_info()->time_stamp();
2742 if (send_codec_) {
2743 QueueBlackFrame(ssrc, timestamp, send_codec_->maxFramerate);
2744 }
2745 return true;
2746}
2747
2748bool WebRtcVideoMediaChannel::RequestIntraFrame() {
2749 // There is no API exposed to application to request a key frame
2750 // ViE does this internally when there are errors from decoder
2751 return false;
2752}
2753
wu@webrtc.orga9890802013-12-13 00:21:03 +00002754void WebRtcVideoMediaChannel::OnPacketReceived(
2755 talk_base::Buffer* packet, const talk_base::PacketTime& packet_time) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002756 // Pick which channel to send this packet to. If this packet doesn't match
2757 // any multiplexed streams, just send it to the default channel. Otherwise,
2758 // send it to the specific decoder instance for that stream.
2759 uint32 ssrc = 0;
2760 if (!GetRtpSsrc(packet->data(), packet->length(), &ssrc))
2761 return;
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00002762 int processing_channel = GetRecvChannelNum(ssrc);
2763 if (processing_channel == -1) {
2764 // Allocate an unsignalled recv channel for processing in conference mode.
henrike@webrtc.org18e59112014-03-14 17:19:38 +00002765 if (!InConferenceMode()) {
buildbot@webrtc.orgdd4742a2014-05-07 14:50:35 +00002766 // If we can't find or allocate one, use the default.
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00002767 processing_channel = video_channel();
henrike@webrtc.org18e59112014-03-14 17:19:38 +00002768 } else if (!CreateUnsignalledRecvChannel(ssrc, &processing_channel)) {
buildbot@webrtc.orgdd4742a2014-05-07 14:50:35 +00002769 // If we can't create an unsignalled recv channel, drop the packet in
henrike@webrtc.org18e59112014-03-14 17:19:38 +00002770 // conference mode.
2771 return;
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00002772 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002773 }
2774
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00002775 engine()->vie()->network()->ReceivedRTPPacket(
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00002776 processing_channel,
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00002777 packet->data(),
wu@webrtc.orga9890802013-12-13 00:21:03 +00002778 static_cast<int>(packet->length()),
2779 webrtc::PacketTime(packet_time.timestamp, packet_time.not_before));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002780}
2781
wu@webrtc.orga9890802013-12-13 00:21:03 +00002782void WebRtcVideoMediaChannel::OnRtcpReceived(
2783 talk_base::Buffer* packet, const talk_base::PacketTime& packet_time) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002784// Sending channels need all RTCP packets with feedback information.
2785// Even sender reports can contain attached report blocks.
2786// Receiving channels need sender reports in order to create
2787// correct receiver reports.
2788
2789 uint32 ssrc = 0;
2790 if (!GetRtcpSsrc(packet->data(), packet->length(), &ssrc)) {
2791 LOG(LS_WARNING) << "Failed to parse SSRC from received RTCP packet";
2792 return;
2793 }
2794 int type = 0;
2795 if (!GetRtcpType(packet->data(), packet->length(), &type)) {
2796 LOG(LS_WARNING) << "Failed to parse type from received RTCP packet";
2797 return;
2798 }
2799
2800 // If it is a sender report, find the channel that is listening.
2801 if (type == kRtcpTypeSR) {
2802 int which_channel = GetRecvChannelNum(ssrc);
2803 if (which_channel != -1 && !IsDefaultChannel(which_channel)) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00002804 engine_->vie()->network()->ReceivedRTCPPacket(
2805 which_channel,
2806 packet->data(),
2807 static_cast<int>(packet->length()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002808 }
2809 }
2810 // SR may continue RR and any RR entry may correspond to any one of the send
2811 // channels. So all RTCP packets must be forwarded all send channels. ViE
2812 // will filter out RR internally.
2813 for (SendChannelMap::iterator iter = send_channels_.begin();
2814 iter != send_channels_.end(); ++iter) {
2815 WebRtcVideoChannelSendInfo* send_channel = iter->second;
2816 int channel_id = send_channel->channel_id();
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00002817 engine_->vie()->network()->ReceivedRTCPPacket(
2818 channel_id,
2819 packet->data(),
2820 static_cast<int>(packet->length()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002821 }
2822}
2823
2824void WebRtcVideoMediaChannel::OnReadyToSend(bool ready) {
2825 SetNetworkTransmissionState(ready);
2826}
2827
2828bool WebRtcVideoMediaChannel::MuteStream(uint32 ssrc, bool muted) {
2829 WebRtcVideoChannelSendInfo* send_channel = GetSendChannel(ssrc);
2830 if (!send_channel) {
2831 LOG(LS_ERROR) << "The specified ssrc " << ssrc << " is not in use.";
2832 return false;
2833 }
2834 send_channel->set_muted(muted);
2835 return true;
2836}
2837
2838bool WebRtcVideoMediaChannel::SetRecvRtpHeaderExtensions(
2839 const std::vector<RtpHeaderExtension>& extensions) {
2840 if (receive_extensions_ == extensions) {
2841 return true;
2842 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002843
2844 const RtpHeaderExtension* offset_extension =
2845 FindHeaderExtension(extensions, kRtpTimestampOffsetHeaderExtension);
2846 const RtpHeaderExtension* send_time_extension =
henrike@webrtc.org79047f92014-03-06 23:46:59 +00002847 FindHeaderExtension(extensions, kRtpAbsoluteSenderTimeHeaderExtension);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002848
2849 // Loop through all receive channels and enable/disable the extensions.
2850 for (RecvChannelMap::iterator channel_it = recv_channels_.begin();
2851 channel_it != recv_channels_.end(); ++channel_it) {
2852 int channel_id = channel_it->second->channel_id();
2853 if (!SetHeaderExtension(
2854 &webrtc::ViERTP_RTCP::SetReceiveTimestampOffsetStatus, channel_id,
2855 offset_extension)) {
2856 return false;
2857 }
2858 if (!SetHeaderExtension(
2859 &webrtc::ViERTP_RTCP::SetReceiveAbsoluteSendTimeStatus, channel_id,
2860 send_time_extension)) {
2861 return false;
2862 }
2863 }
buildbot@webrtc.org150835e2014-05-06 15:54:38 +00002864
2865 receive_extensions_ = extensions;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002866 return true;
2867}
2868
2869bool WebRtcVideoMediaChannel::SetSendRtpHeaderExtensions(
2870 const std::vector<RtpHeaderExtension>& extensions) {
buildbot@webrtc.org150835e2014-05-06 15:54:38 +00002871 if (send_extensions_ == extensions) {
2872 return true;
2873 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002874
2875 const RtpHeaderExtension* offset_extension =
2876 FindHeaderExtension(extensions, kRtpTimestampOffsetHeaderExtension);
2877 const RtpHeaderExtension* send_time_extension =
henrike@webrtc.org79047f92014-03-06 23:46:59 +00002878 FindHeaderExtension(extensions, kRtpAbsoluteSenderTimeHeaderExtension);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002879
2880 // Loop through all send channels and enable/disable the extensions.
2881 for (SendChannelMap::iterator channel_it = send_channels_.begin();
2882 channel_it != send_channels_.end(); ++channel_it) {
2883 int channel_id = channel_it->second->channel_id();
2884 if (!SetHeaderExtension(
2885 &webrtc::ViERTP_RTCP::SetSendTimestampOffsetStatus, channel_id,
2886 offset_extension)) {
2887 return false;
2888 }
2889 if (!SetHeaderExtension(
2890 &webrtc::ViERTP_RTCP::SetSendAbsoluteSendTimeStatus, channel_id,
2891 send_time_extension)) {
2892 return false;
2893 }
2894 }
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00002895
2896 if (send_time_extension) {
2897 // For video RTP packets, we would like to update AbsoluteSendTimeHeader
2898 // Extension closer to the network, @ socket level before sending.
2899 // Pushing the extension id to socket layer.
2900 MediaChannel::SetOption(NetworkInterface::ST_RTP,
2901 talk_base::Socket::OPT_RTP_SENDTIME_EXTN_ID,
2902 send_time_extension->id);
2903 }
buildbot@webrtc.org150835e2014-05-06 15:54:38 +00002904
2905 send_extensions_ = extensions;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002906 return true;
2907}
2908
mallinath@webrtc.org92fdfeb2014-02-17 18:49:41 +00002909int WebRtcVideoMediaChannel::GetRtpSendTimeExtnId() const {
2910 const RtpHeaderExtension* send_time_extension = FindHeaderExtension(
henrike@webrtc.org79047f92014-03-06 23:46:59 +00002911 send_extensions_, kRtpAbsoluteSenderTimeHeaderExtension);
mallinath@webrtc.org92fdfeb2014-02-17 18:49:41 +00002912 if (send_time_extension) {
2913 return send_time_extension->id;
2914 }
2915 return -1;
2916}
2917
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002918bool WebRtcVideoMediaChannel::SetStartSendBandwidth(int bps) {
2919 LOG(LS_INFO) << "WebRtcVideoMediaChannel::SetStartSendBandwidth";
2920
2921 if (!send_codec_) {
2922 LOG(LS_INFO) << "The send codec has not been set up yet";
2923 return true;
2924 }
2925
2926 // On success, SetSendCodec() will reset |send_start_bitrate_| to |bps/1000|,
buildbot@webrtc.orgd1ae89f2014-05-08 19:19:26 +00002927 // by calling MaybeChangeBitrates. That method will also clamp the
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002928 // start bitrate between min and max, consistent with the override behavior
2929 // in SetMaxSendBandwidth.
buildbot@webrtc.orgd1ae89f2014-05-08 19:19:26 +00002930 webrtc::VideoCodec new_codec = *send_codec_;
2931 if (BitrateIsSet(bps)) {
2932 new_codec.startBitrate = bps / 1000;
2933 }
2934 return SetSendCodec(new_codec);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002935}
2936
2937bool WebRtcVideoMediaChannel::SetMaxSendBandwidth(int bps) {
2938 LOG(LS_INFO) << "WebRtcVideoMediaChannel::SetMaxSendBandwidth";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002939
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002940 if (!send_codec_) {
2941 LOG(LS_INFO) << "The send codec has not been set up yet";
2942 return true;
2943 }
2944
buildbot@webrtc.orgd1ae89f2014-05-08 19:19:26 +00002945 webrtc::VideoCodec new_codec = *send_codec_;
2946 if (BitrateIsSet(bps)) {
2947 new_codec.maxBitrate = bps / 1000;
2948 }
2949 if (!SetSendCodec(new_codec)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002950 return false;
2951 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002952 LogSendCodecChange("SetMaxSendBandwidth()");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002953
2954 return true;
2955}
2956
2957bool WebRtcVideoMediaChannel::SetOptions(const VideoOptions &options) {
2958 // Always accept options that are unchanged.
2959 if (options_ == options) {
2960 return true;
2961 }
2962
2963 // Trigger SetSendCodec to set correct noise reduction state if the option has
2964 // changed.
2965 bool denoiser_changed = options.video_noise_reduction.IsSet() &&
2966 (options_.video_noise_reduction != options.video_noise_reduction);
2967
2968 bool leaky_bucket_changed = options.video_leaky_bucket.IsSet() &&
2969 (options_.video_leaky_bucket != options.video_leaky_bucket);
2970
2971 bool buffer_latency_changed = options.buffered_mode_latency.IsSet() &&
2972 (options_.buffered_mode_latency != options.buffered_mode_latency);
2973
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00002974 bool cpu_overuse_detection_changed = options.cpu_overuse_detection.IsSet() &&
2975 (options_.cpu_overuse_detection != options.cpu_overuse_detection);
2976
wu@webrtc.orgde305012013-10-31 15:40:38 +00002977 bool dscp_option_changed = (options_.dscp != options.dscp);
2978
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002979 bool suspend_below_min_bitrate_changed =
2980 options.suspend_below_min_bitrate.IsSet() &&
2981 (options_.suspend_below_min_bitrate != options.suspend_below_min_bitrate);
2982
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002983 bool conference_mode_turned_off = false;
2984 if (options_.conference_mode.IsSet() && options.conference_mode.IsSet() &&
2985 options_.conference_mode.GetWithDefaultIfUnset(false) &&
2986 !options.conference_mode.GetWithDefaultIfUnset(false)) {
2987 conference_mode_turned_off = true;
2988 }
2989
henrike@webrtc.orgdce3feb2014-03-26 01:17:30 +00002990 bool improved_wifi_bwe_changed =
2991 options.use_improved_wifi_bandwidth_estimator.IsSet() &&
2992 options_.use_improved_wifi_bandwidth_estimator !=
2993 options.use_improved_wifi_bandwidth_estimator;
2994
henrike@webrtc.org10bd88e2014-03-11 21:07:25 +00002995
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002996 // Save the options, to be interpreted where appropriate.
2997 // Use options_.SetAll() instead of assignment so that unset value in options
2998 // will not overwrite the previous option value.
2999 options_.SetAll(options);
3000
3001 // Set CPU options for all send channels.
3002 for (SendChannelMap::iterator iter = send_channels_.begin();
3003 iter != send_channels_.end(); ++iter) {
3004 WebRtcVideoChannelSendInfo* send_channel = iter->second;
3005 send_channel->ApplyCpuOptions(options_);
3006 }
3007
buildbot@webrtc.orgd1ae89f2014-05-08 19:19:26 +00003008 if (send_codec_) {
3009 bool reset_send_codec_needed = denoiser_changed;
3010 webrtc::VideoCodec new_codec = *send_codec_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003011
buildbot@webrtc.orgd1ae89f2014-05-08 19:19:26 +00003012 // TODO(pthatcher): Remove this. We don't need 4 ways to set bitrates.
3013 bool lower_min_bitrate;
3014 if (options.lower_min_bitrate.Get(&lower_min_bitrate)) {
3015 new_codec.minBitrate = kLowerMinBitrate;
3016 reset_send_codec_needed = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003017 }
buildbot@webrtc.orgd1ae89f2014-05-08 19:19:26 +00003018
3019 if (conference_mode_turned_off) {
3020 // This is a special case for turning conference mode off.
3021 // Max bitrate should go back to the default maximum value instead
3022 // of the current maximum.
3023 new_codec.maxBitrate = kAutoBandwidth;
3024 reset_send_codec_needed = true;
3025 }
3026
3027 // TODO(pthatcher): Remove this. We don't need 4 ways to set bitrates.
3028 int new_start_bitrate;
3029 if (options.video_start_bitrate.Get(&new_start_bitrate)) {
3030 new_codec.startBitrate = new_start_bitrate;
3031 reset_send_codec_needed = true;
3032 }
3033
3034
3035 LOG(LS_INFO) << "Reset send codec needed is enabled? "
3036 << reset_send_codec_needed;
3037 if (reset_send_codec_needed) {
3038 if (!SetSendCodec(new_codec)) {
3039 return false;
3040 }
3041 LogSendCodecChange("SetOptions()");
3042 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003043 }
henrike@webrtc.org10bd88e2014-03-11 21:07:25 +00003044
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003045 if (leaky_bucket_changed) {
3046 bool enable_leaky_bucket =
3047 options_.video_leaky_bucket.GetWithDefaultIfUnset(false);
buildbot@webrtc.orgf875f152014-04-14 16:06:21 +00003048 LOG(LS_INFO) << "Leaky bucket is enabled? " << enable_leaky_bucket;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003049 for (SendChannelMap::iterator it = send_channels_.begin();
3050 it != send_channels_.end(); ++it) {
3051 if (engine()->vie()->rtp()->SetTransmissionSmoothingStatus(
3052 it->second->channel_id(), enable_leaky_bucket) != 0) {
3053 LOG_RTCERR2(SetTransmissionSmoothingStatus, it->second->channel_id(),
3054 enable_leaky_bucket);
3055 }
3056 }
3057 }
3058 if (buffer_latency_changed) {
3059 int buffer_latency =
3060 options_.buffered_mode_latency.GetWithDefaultIfUnset(
3061 cricket::kBufferedModeDisabled);
buildbot@webrtc.orgf875f152014-04-14 16:06:21 +00003062 LOG(LS_INFO) << "Buffer latency is " << buffer_latency;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003063 for (SendChannelMap::iterator it = send_channels_.begin();
3064 it != send_channels_.end(); ++it) {
3065 if (engine()->vie()->rtp()->SetSenderBufferingMode(
3066 it->second->channel_id(), buffer_latency) != 0) {
3067 LOG_RTCERR2(SetSenderBufferingMode, it->second->channel_id(),
3068 buffer_latency);
3069 }
3070 }
3071 for (RecvChannelMap::iterator it = recv_channels_.begin();
3072 it != recv_channels_.end(); ++it) {
3073 if (engine()->vie()->rtp()->SetReceiverBufferingMode(
3074 it->second->channel_id(), buffer_latency) != 0) {
3075 LOG_RTCERR2(SetReceiverBufferingMode, it->second->channel_id(),
3076 buffer_latency);
3077 }
3078 }
3079 }
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00003080 if (cpu_overuse_detection_changed) {
3081 bool cpu_overuse_detection =
3082 options_.cpu_overuse_detection.GetWithDefaultIfUnset(false);
buildbot@webrtc.orgf875f152014-04-14 16:06:21 +00003083 LOG(LS_INFO) << "CPU overuse detection is enabled? "
3084 << cpu_overuse_detection;
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00003085 for (SendChannelMap::iterator iter = send_channels_.begin();
3086 iter != send_channels_.end(); ++iter) {
3087 WebRtcVideoChannelSendInfo* send_channel = iter->second;
3088 send_channel->SetCpuOveruseDetection(cpu_overuse_detection);
3089 }
3090 }
wu@webrtc.orgde305012013-10-31 15:40:38 +00003091 if (dscp_option_changed) {
3092 talk_base::DiffServCodePoint dscp = talk_base::DSCP_DEFAULT;
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00003093 if (options_.dscp.GetWithDefaultIfUnset(false))
wu@webrtc.orgde305012013-10-31 15:40:38 +00003094 dscp = kVideoDscpValue;
buildbot@webrtc.orgf875f152014-04-14 16:06:21 +00003095 LOG(LS_INFO) << "DSCP is " << dscp;
wu@webrtc.orgde305012013-10-31 15:40:38 +00003096 if (MediaChannel::SetDscp(dscp) != 0) {
3097 LOG(LS_WARNING) << "Failed to set DSCP settings for video channel";
3098 }
3099 }
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00003100 if (suspend_below_min_bitrate_changed) {
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00003101 if (options_.suspend_below_min_bitrate.GetWithDefaultIfUnset(false)) {
buildbot@webrtc.orgf875f152014-04-14 16:06:21 +00003102 LOG(LS_INFO) << "Suspend below min bitrate enabled.";
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00003103 for (SendChannelMap::iterator it = send_channels_.begin();
3104 it != send_channels_.end(); ++it) {
3105 engine()->vie()->codec()->SuspendBelowMinBitrate(
3106 it->second->channel_id());
3107 }
3108 } else {
3109 LOG(LS_WARNING) << "Cannot disable video suspension once it is enabled";
3110 }
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00003111 }
henrike@webrtc.orgdce3feb2014-03-26 01:17:30 +00003112 if (improved_wifi_bwe_changed) {
buildbot@webrtc.orgf875f152014-04-14 16:06:21 +00003113 LOG(LS_INFO) << "Improved WIFI BWE called.";
henrike@webrtc.orgdce3feb2014-03-26 01:17:30 +00003114 webrtc::Config config;
3115 config.Set(new webrtc::AimdRemoteRateControl(
3116 options_.use_improved_wifi_bandwidth_estimator
3117 .GetWithDefaultIfUnset(false)));
3118 for (SendChannelMap::iterator it = send_channels_.begin();
3119 it != send_channels_.end(); ++it) {
3120 engine()->vie()->network()->SetBandwidthEstimationConfig(
3121 it->second->channel_id(), config);
3122 }
3123 }
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +00003124 webrtc::CpuOveruseOptions overuse_options;
3125 if (GetCpuOveruseOptions(options_, &overuse_options)) {
3126 for (SendChannelMap::iterator it = send_channels_.begin();
3127 it != send_channels_.end(); ++it) {
3128 if (engine()->vie()->base()->SetCpuOveruseOptions(
3129 it->second->channel_id(), overuse_options) != 0) {
3130 LOG_RTCERR1(SetCpuOveruseOptions, it->second->channel_id());
3131 }
3132 }
3133 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003134 return true;
3135}
3136
3137void WebRtcVideoMediaChannel::SetInterface(NetworkInterface* iface) {
3138 MediaChannel::SetInterface(iface);
3139 // Set the RTP recv/send buffer to a bigger size
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00003140 MediaChannel::SetOption(NetworkInterface::ST_RTP,
3141 talk_base::Socket::OPT_RCVBUF,
3142 kVideoRtpBufferSize);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003143
3144 // TODO(sriniv): Remove or re-enable this.
3145 // As part of b/8030474, send-buffer is size now controlled through
3146 // portallocator flags.
3147 // network_interface_->SetOption(NetworkInterface::ST_RTP,
3148 // talk_base::Socket::OPT_SNDBUF,
3149 // kVideoRtpBufferSize);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003150}
3151
3152void WebRtcVideoMediaChannel::UpdateAspectRatio(int ratio_w, int ratio_h) {
3153 ASSERT(ratio_w != 0);
3154 ASSERT(ratio_h != 0);
3155 ratio_w_ = ratio_w;
3156 ratio_h_ = ratio_h;
3157 // For now assume that all streams want the same aspect ratio.
3158 // TODO(hellner): remove the need for this assumption.
3159 for (SendChannelMap::iterator iter = send_channels_.begin();
3160 iter != send_channels_.end(); ++iter) {
3161 WebRtcVideoChannelSendInfo* send_channel = iter->second;
3162 VideoCapturer* capturer = send_channel->video_capturer();
3163 if (capturer) {
3164 capturer->UpdateAspectRatio(ratio_w, ratio_h);
3165 }
3166 }
3167}
3168
3169bool WebRtcVideoMediaChannel::GetRenderer(uint32 ssrc,
3170 VideoRenderer** renderer) {
3171 RecvChannelMap::const_iterator it = recv_channels_.find(ssrc);
3172 if (it == recv_channels_.end()) {
3173 if (first_receive_ssrc_ == ssrc &&
3174 recv_channels_.find(0) != recv_channels_.end()) {
3175 LOG(LS_INFO) << " GetRenderer " << ssrc
3176 << " reuse default renderer #"
3177 << vie_channel_;
3178 *renderer = recv_channels_[0]->render_adapter()->renderer();
3179 return true;
3180 }
3181 return false;
3182 }
3183
3184 *renderer = it->second->render_adapter()->renderer();
3185 return true;
3186}
3187
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00003188bool WebRtcVideoMediaChannel::GetVideoAdapter(
3189 uint32 ssrc, CoordinatedVideoAdapter** video_adapter) {
3190 SendChannelMap::iterator it = send_channels_.find(ssrc);
3191 if (it == send_channels_.end()) {
3192 return false;
3193 }
3194 *video_adapter = it->second->video_adapter();
3195 return true;
3196}
3197
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003198void WebRtcVideoMediaChannel::SendFrame(VideoCapturer* capturer,
3199 const VideoFrame* frame) {
wu@webrtc.org24301a62013-12-13 19:17:43 +00003200 // If the |capturer| is registered to any send channel, then send the frame
3201 // to those send channels.
3202 bool capturer_is_channel_owned = false;
3203 for (SendChannelMap::iterator iter = send_channels_.begin();
3204 iter != send_channels_.end(); ++iter) {
3205 WebRtcVideoChannelSendInfo* send_channel = iter->second;
3206 if (send_channel->video_capturer() == capturer) {
3207 SendFrame(send_channel, frame, capturer->IsScreencast());
3208 capturer_is_channel_owned = true;
3209 }
3210 }
3211 if (capturer_is_channel_owned) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003212 return;
3213 }
wu@webrtc.org24301a62013-12-13 19:17:43 +00003214
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003215 // TODO(hellner): Remove below for loop once the captured frame no longer
3216 // come from the engine, i.e. the engine no longer owns a capturer.
3217 for (SendChannelMap::iterator iter = send_channels_.begin();
3218 iter != send_channels_.end(); ++iter) {
3219 WebRtcVideoChannelSendInfo* send_channel = iter->second;
3220 if (send_channel->video_capturer() == NULL) {
3221 SendFrame(send_channel, frame, capturer->IsScreencast());
3222 }
3223 }
3224}
3225
3226bool WebRtcVideoMediaChannel::SendFrame(
3227 WebRtcVideoChannelSendInfo* send_channel,
3228 const VideoFrame* frame,
3229 bool is_screencast) {
3230 if (!send_channel) {
3231 return false;
3232 }
3233 if (!send_codec_) {
3234 // Send codec has not been set. No reason to process the frame any further.
3235 return false;
3236 }
3237 const VideoFormat& video_format = send_channel->video_format();
3238 // If the frame should be dropped.
3239 const bool video_format_set = video_format != cricket::VideoFormat();
3240 if (video_format_set &&
3241 (video_format.width == 0 && video_format.height == 0)) {
3242 return true;
3243 }
3244
3245 // Checks if we need to reset vie send codec.
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00003246 if (!MaybeResetVieSendCodec(send_channel,
3247 static_cast<int>(frame->GetWidth()),
3248 static_cast<int>(frame->GetHeight()),
3249 is_screencast, NULL)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003250 LOG(LS_ERROR) << "MaybeResetVieSendCodec failed with "
3251 << frame->GetWidth() << "x" << frame->GetHeight();
3252 return false;
3253 }
3254 const VideoFrame* frame_out = frame;
3255 talk_base::scoped_ptr<VideoFrame> processed_frame;
3256 // Disable muting for screencast.
3257 const bool mute = (send_channel->muted() && !is_screencast);
3258 send_channel->ProcessFrame(*frame_out, mute, processed_frame.use());
3259 if (processed_frame) {
3260 frame_out = processed_frame.get();
3261 }
3262
3263 webrtc::ViEVideoFrameI420 frame_i420;
3264 // TODO(ronghuawu): Update the webrtc::ViEVideoFrameI420
3265 // to use const unsigned char*
3266 frame_i420.y_plane = const_cast<unsigned char*>(frame_out->GetYPlane());
3267 frame_i420.u_plane = const_cast<unsigned char*>(frame_out->GetUPlane());
3268 frame_i420.v_plane = const_cast<unsigned char*>(frame_out->GetVPlane());
3269 frame_i420.y_pitch = frame_out->GetYPitch();
3270 frame_i420.u_pitch = frame_out->GetUPitch();
3271 frame_i420.v_pitch = frame_out->GetVPitch();
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00003272 frame_i420.width = static_cast<uint16>(frame_out->GetWidth());
3273 frame_i420.height = static_cast<uint16>(frame_out->GetHeight());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003274
3275 int64 timestamp_ntp_ms = 0;
3276 // TODO(justinlin): Reenable after Windows issues with clock drift are fixed.
3277 // Currently reverted to old behavior of discarding capture timestamp.
3278#if 0
henrike@webrtc.orgf5bebd42014-04-04 18:39:07 +00003279 static const int kTimestampDeltaInSecondsForWarning = 2;
3280
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003281 // If the frame timestamp is 0, we will use the deliver time.
3282 const int64 frame_timestamp = frame->GetTimeStamp();
3283 if (frame_timestamp != 0) {
3284 if (abs(time(NULL) - frame_timestamp / talk_base::kNumNanosecsPerSec) >
3285 kTimestampDeltaInSecondsForWarning) {
3286 LOG(LS_WARNING) << "Frame timestamp differs by more than "
3287 << kTimestampDeltaInSecondsForWarning << " seconds from "
3288 << "current Unix timestamp.";
3289 }
3290
3291 timestamp_ntp_ms =
3292 talk_base::UnixTimestampNanosecsToNtpMillisecs(frame_timestamp);
3293 }
3294#endif
3295
3296 return send_channel->external_capture()->IncomingFrameI420(
3297 frame_i420, timestamp_ntp_ms) == 0;
3298}
3299
3300bool WebRtcVideoMediaChannel::CreateChannel(uint32 ssrc_key,
3301 MediaDirection direction,
3302 int* channel_id) {
3303 // There are 3 types of channels. Sending only, receiving only and
3304 // sending and receiving. The sending and receiving channel is the
3305 // default channel and there is only one. All other channels that are created
3306 // are associated with the default channel which must exist. The default
3307 // channel id is stored in |vie_channel_|. All channels need to know about
3308 // the default channel to properly handle remb which is why there are
3309 // different ViE create channel calls.
3310 // For this channel the local and remote ssrc key is 0. However, it may
3311 // have a non-zero local and/or remote ssrc depending on if it is currently
3312 // sending and/or receiving.
3313 if ((vie_channel_ == -1 || direction == MD_SENDRECV) &&
3314 (!send_channels_.empty() || !recv_channels_.empty())) {
3315 ASSERT(false);
3316 return false;
3317 }
3318
3319 *channel_id = -1;
3320 if (direction == MD_RECV) {
3321 // All rec channels are associated with the default channel |vie_channel_|
3322 if (engine_->vie()->base()->CreateReceiveChannel(*channel_id,
3323 vie_channel_) != 0) {
3324 LOG_RTCERR2(CreateReceiveChannel, *channel_id, vie_channel_);
3325 return false;
3326 }
3327 } else if (direction == MD_SEND) {
3328 if (engine_->vie()->base()->CreateChannel(*channel_id,
3329 vie_channel_) != 0) {
3330 LOG_RTCERR2(CreateChannel, *channel_id, vie_channel_);
3331 return false;
3332 }
3333 } else {
3334 ASSERT(direction == MD_SENDRECV);
3335 if (engine_->vie()->base()->CreateChannel(*channel_id) != 0) {
3336 LOG_RTCERR1(CreateChannel, *channel_id);
3337 return false;
3338 }
3339 }
3340 if (!ConfigureChannel(*channel_id, direction, ssrc_key)) {
3341 engine_->vie()->base()->DeleteChannel(*channel_id);
3342 *channel_id = -1;
3343 return false;
3344 }
3345
3346 return true;
3347}
3348
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00003349bool WebRtcVideoMediaChannel::CreateUnsignalledRecvChannel(
3350 uint32 ssrc_key, int* out_channel_id) {
henrike@webrtc.org18e59112014-03-14 17:19:38 +00003351 int unsignalled_recv_channel_limit =
3352 options_.unsignalled_recv_stream_limit.GetWithDefaultIfUnset(
3353 kNumDefaultUnsignalledVideoRecvStreams);
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00003354 if (num_unsignalled_recv_channels_ >= unsignalled_recv_channel_limit) {
3355 return false;
3356 }
3357 if (!CreateChannel(ssrc_key, MD_RECV, out_channel_id)) {
3358 return false;
3359 }
3360 // TODO(tvsriram): Support dynamic sizing of unsignalled recv channels.
3361 num_unsignalled_recv_channels_++;
3362 return true;
3363}
3364
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003365bool WebRtcVideoMediaChannel::ConfigureChannel(int channel_id,
3366 MediaDirection direction,
3367 uint32 ssrc_key) {
3368 const bool receiving = (direction == MD_RECV) || (direction == MD_SENDRECV);
3369 const bool sending = (direction == MD_SEND) || (direction == MD_SENDRECV);
3370 // Register external transport.
3371 if (engine_->vie()->network()->RegisterSendTransport(
3372 channel_id, *this) != 0) {
3373 LOG_RTCERR1(RegisterSendTransport, channel_id);
3374 return false;
3375 }
3376
3377 // Set MTU.
3378 if (engine_->vie()->network()->SetMTU(channel_id, kVideoMtu) != 0) {
3379 LOG_RTCERR2(SetMTU, channel_id, kVideoMtu);
3380 return false;
3381 }
3382 // Turn on RTCP and loss feedback reporting.
3383 if (engine()->vie()->rtp()->SetRTCPStatus(
3384 channel_id, webrtc::kRtcpCompound_RFC4585) != 0) {
3385 LOG_RTCERR2(SetRTCPStatus, channel_id, webrtc::kRtcpCompound_RFC4585);
3386 return false;
3387 }
3388 // Enable pli as key frame request method.
3389 if (engine_->vie()->rtp()->SetKeyFrameRequestMethod(
3390 channel_id, webrtc::kViEKeyFrameRequestPliRtcp) != 0) {
3391 LOG_RTCERR2(SetKeyFrameRequestMethod,
3392 channel_id, webrtc::kViEKeyFrameRequestPliRtcp);
3393 return false;
3394 }
3395 if (!SetNackFec(channel_id, send_red_type_, send_fec_type_, nack_enabled_)) {
3396 // Logged in SetNackFec. Don't spam the logs.
3397 return false;
3398 }
3399 // Note that receiving must always be configured before sending to ensure
3400 // that send and receive channel is configured correctly (ConfigureReceiving
3401 // assumes no sending).
3402 if (receiving) {
3403 if (!ConfigureReceiving(channel_id, ssrc_key)) {
3404 return false;
3405 }
3406 }
3407 if (sending) {
3408 if (!ConfigureSending(channel_id, ssrc_key)) {
3409 return false;
3410 }
3411 }
3412
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00003413 // Start receiving for both receive and send channels so that we get incoming
3414 // RTP (if receiving) as well as RTCP feedback (if sending).
3415 if (engine()->vie()->base()->StartReceive(channel_id) != 0) {
3416 LOG_RTCERR1(StartReceive, channel_id);
3417 return false;
3418 }
3419
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003420 return true;
3421}
3422
3423bool WebRtcVideoMediaChannel::ConfigureReceiving(int channel_id,
3424 uint32 remote_ssrc_key) {
3425 // Make sure that an SSRC/key isn't registered more than once.
3426 if (recv_channels_.find(remote_ssrc_key) != recv_channels_.end()) {
3427 return false;
3428 }
3429 // Connect the voice channel, if there is one.
3430 // TODO(perkj): The A/V is synched by the receiving channel. So we need to
3431 // know the SSRC of the remote audio channel in order to fetch the correct
3432 // webrtc VoiceEngine channel. For now- only sync the default channel used
3433 // in 1-1 calls.
3434 if (remote_ssrc_key == 0 && voice_channel_) {
3435 WebRtcVoiceMediaChannel* voice_channel =
3436 static_cast<WebRtcVoiceMediaChannel*>(voice_channel_);
3437 if (engine_->vie()->base()->ConnectAudioChannel(
3438 vie_channel_, voice_channel->voe_channel()) != 0) {
3439 LOG_RTCERR2(ConnectAudioChannel, channel_id,
3440 voice_channel->voe_channel());
3441 LOG(LS_WARNING) << "A/V not synchronized";
3442 // Not a fatal error.
3443 }
3444 }
3445
3446 talk_base::scoped_ptr<WebRtcVideoChannelRecvInfo> channel_info(
3447 new WebRtcVideoChannelRecvInfo(channel_id));
3448
3449 // Install a render adapter.
3450 if (engine_->vie()->render()->AddRenderer(channel_id,
3451 webrtc::kVideoI420, channel_info->render_adapter()) != 0) {
3452 LOG_RTCERR3(AddRenderer, channel_id, webrtc::kVideoI420,
3453 channel_info->render_adapter());
3454 return false;
3455 }
3456
3457
3458 if (engine_->vie()->rtp()->SetRembStatus(channel_id,
3459 kNotSending,
3460 remb_enabled_) != 0) {
3461 LOG_RTCERR3(SetRembStatus, channel_id, kNotSending, remb_enabled_);
3462 return false;
3463 }
3464
3465 if (!SetHeaderExtension(&webrtc::ViERTP_RTCP::SetReceiveTimestampOffsetStatus,
3466 channel_id, receive_extensions_, kRtpTimestampOffsetHeaderExtension)) {
3467 return false;
3468 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003469 if (!SetHeaderExtension(
3470 &webrtc::ViERTP_RTCP::SetReceiveAbsoluteSendTimeStatus, channel_id,
henrike@webrtc.org79047f92014-03-06 23:46:59 +00003471 receive_extensions_, kRtpAbsoluteSenderTimeHeaderExtension)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003472 return false;
3473 }
3474
3475 if (remote_ssrc_key != 0) {
3476 // Use the same SSRC as our default channel
3477 // (so the RTCP reports are correct).
3478 unsigned int send_ssrc = 0;
3479 webrtc::ViERTP_RTCP* rtp = engine()->vie()->rtp();
3480 if (rtp->GetLocalSSRC(vie_channel_, send_ssrc) == -1) {
3481 LOG_RTCERR2(GetLocalSSRC, vie_channel_, send_ssrc);
3482 return false;
3483 }
3484 if (rtp->SetLocalSSRC(channel_id, send_ssrc) == -1) {
3485 LOG_RTCERR2(SetLocalSSRC, channel_id, send_ssrc);
3486 return false;
3487 }
3488 } // Else this is the the default channel and we don't change the SSRC.
3489
3490 // Disable color enhancement since it is a bit too aggressive.
3491 if (engine()->vie()->image()->EnableColorEnhancement(channel_id,
3492 false) != 0) {
3493 LOG_RTCERR1(EnableColorEnhancement, channel_id);
3494 return false;
3495 }
3496
3497 if (!SetReceiveCodecs(channel_info.get())) {
3498 return false;
3499 }
3500
3501 int buffer_latency =
3502 options_.buffered_mode_latency.GetWithDefaultIfUnset(
3503 cricket::kBufferedModeDisabled);
3504 if (buffer_latency != cricket::kBufferedModeDisabled) {
3505 if (engine()->vie()->rtp()->SetReceiverBufferingMode(
3506 channel_id, buffer_latency) != 0) {
3507 LOG_RTCERR2(SetReceiverBufferingMode, channel_id, buffer_latency);
3508 }
3509 }
3510
3511 if (render_started_) {
3512 if (engine_->vie()->render()->StartRender(channel_id) != 0) {
3513 LOG_RTCERR1(StartRender, channel_id);
3514 return false;
3515 }
3516 }
3517
3518 // Register decoder observer for incoming framerate and bitrate.
3519 if (engine()->vie()->codec()->RegisterDecoderObserver(
3520 channel_id, *channel_info->decoder_observer()) != 0) {
3521 LOG_RTCERR1(RegisterDecoderObserver, channel_info->decoder_observer());
3522 return false;
3523 }
3524
3525 recv_channels_[remote_ssrc_key] = channel_info.release();
3526 return true;
3527}
3528
3529bool WebRtcVideoMediaChannel::ConfigureSending(int channel_id,
3530 uint32 local_ssrc_key) {
3531 // The ssrc key can be zero or correspond to an SSRC.
3532 // Make sure the default channel isn't configured more than once.
3533 if (local_ssrc_key == 0 && send_channels_.find(0) != send_channels_.end()) {
3534 return false;
3535 }
3536 // Make sure that the SSRC is not already in use.
3537 uint32 dummy_key;
3538 if (GetSendChannelKey(local_ssrc_key, &dummy_key)) {
3539 return false;
3540 }
3541 int vie_capture = 0;
3542 webrtc::ViEExternalCapture* external_capture = NULL;
3543 // Register external capture.
3544 if (engine()->vie()->capture()->AllocateExternalCaptureDevice(
3545 vie_capture, external_capture) != 0) {
3546 LOG_RTCERR0(AllocateExternalCaptureDevice);
3547 return false;
3548 }
3549
3550 // Connect external capture.
3551 if (engine()->vie()->capture()->ConnectCaptureDevice(
3552 vie_capture, channel_id) != 0) {
3553 LOG_RTCERR2(ConnectCaptureDevice, vie_capture, channel_id);
3554 return false;
3555 }
3556 talk_base::scoped_ptr<WebRtcVideoChannelSendInfo> send_channel(
3557 new WebRtcVideoChannelSendInfo(channel_id, vie_capture,
3558 external_capture,
3559 engine()->cpu_monitor()));
3560 send_channel->ApplyCpuOptions(options_);
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00003561 send_channel->SignalCpuAdaptationUnable.connect(this,
3562 &WebRtcVideoMediaChannel::OnCpuAdaptationUnable);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003563
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00003564 if (options_.cpu_overuse_detection.GetWithDefaultIfUnset(false)) {
3565 send_channel->SetCpuOveruseDetection(true);
3566 }
3567
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +00003568 webrtc::CpuOveruseOptions overuse_options;
3569 if (GetCpuOveruseOptions(options_, &overuse_options)) {
3570 if (engine()->vie()->base()->SetCpuOveruseOptions(channel_id,
3571 overuse_options) != 0) {
3572 LOG_RTCERR1(SetCpuOveruseOptions, channel_id);
3573 }
3574 }
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +00003575
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003576 // Register encoder observer for outgoing framerate and bitrate.
3577 if (engine()->vie()->codec()->RegisterEncoderObserver(
3578 channel_id, *send_channel->encoder_observer()) != 0) {
3579 LOG_RTCERR1(RegisterEncoderObserver, send_channel->encoder_observer());
3580 return false;
3581 }
3582
3583 if (!SetHeaderExtension(&webrtc::ViERTP_RTCP::SetSendTimestampOffsetStatus,
3584 channel_id, send_extensions_, kRtpTimestampOffsetHeaderExtension)) {
3585 return false;
3586 }
3587
3588 if (!SetHeaderExtension(&webrtc::ViERTP_RTCP::SetSendAbsoluteSendTimeStatus,
henrike@webrtc.org79047f92014-03-06 23:46:59 +00003589 channel_id, send_extensions_, kRtpAbsoluteSenderTimeHeaderExtension)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003590 return false;
3591 }
3592
3593 if (options_.video_leaky_bucket.GetWithDefaultIfUnset(false)) {
3594 if (engine()->vie()->rtp()->SetTransmissionSmoothingStatus(channel_id,
3595 true) != 0) {
3596 LOG_RTCERR2(SetTransmissionSmoothingStatus, channel_id, true);
3597 return false;
3598 }
3599 }
3600
3601 int buffer_latency =
3602 options_.buffered_mode_latency.GetWithDefaultIfUnset(
3603 cricket::kBufferedModeDisabled);
3604 if (buffer_latency != cricket::kBufferedModeDisabled) {
3605 if (engine()->vie()->rtp()->SetSenderBufferingMode(
3606 channel_id, buffer_latency) != 0) {
3607 LOG_RTCERR2(SetSenderBufferingMode, channel_id, buffer_latency);
3608 }
3609 }
henrike@webrtc.org6e3dbc22014-03-25 17:09:47 +00003610
3611 if (options_.suspend_below_min_bitrate.GetWithDefaultIfUnset(false)) {
3612 engine()->vie()->codec()->SuspendBelowMinBitrate(channel_id);
3613 }
3614
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003615 // The remb status direction correspond to the RTP stream (and not the RTCP
3616 // stream). I.e. if send remb is enabled it means it is receiving remote
3617 // rembs and should use them to estimate bandwidth. Receive remb mean that
3618 // remb packets will be generated and that the channel should be included in
3619 // it. If remb is enabled all channels are allowed to contribute to the remb
3620 // but only receive channels will ever end up actually contributing. This
3621 // keeps the logic simple.
3622 if (engine_->vie()->rtp()->SetRembStatus(channel_id,
3623 remb_enabled_,
3624 remb_enabled_) != 0) {
3625 LOG_RTCERR3(SetRembStatus, channel_id, remb_enabled_, remb_enabled_);
3626 return false;
3627 }
3628 if (!SetNackFec(channel_id, send_red_type_, send_fec_type_, nack_enabled_)) {
3629 // Logged in SetNackFec. Don't spam the logs.
3630 return false;
3631 }
3632
3633 send_channels_[local_ssrc_key] = send_channel.release();
3634
3635 return true;
3636}
3637
3638bool WebRtcVideoMediaChannel::SetNackFec(int channel_id,
3639 int red_payload_type,
3640 int fec_payload_type,
3641 bool nack_enabled) {
3642 bool enable = (red_payload_type != -1 && fec_payload_type != -1 &&
3643 !InConferenceMode());
3644 if (enable) {
3645 if (engine_->vie()->rtp()->SetHybridNACKFECStatus(
3646 channel_id, nack_enabled, red_payload_type, fec_payload_type) != 0) {
3647 LOG_RTCERR4(SetHybridNACKFECStatus,
3648 channel_id, nack_enabled, red_payload_type, fec_payload_type);
3649 return false;
3650 }
3651 LOG(LS_INFO) << "Hybrid NACK/FEC enabled for channel " << channel_id;
3652 } else {
3653 if (engine_->vie()->rtp()->SetNACKStatus(channel_id, nack_enabled) != 0) {
3654 LOG_RTCERR1(SetNACKStatus, channel_id);
3655 return false;
3656 }
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00003657 std::string enabled = nack_enabled ? "enabled" : "disabled";
3658 LOG(LS_INFO) << "NACK " << enabled << " for channel " << channel_id;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003659 }
3660 return true;
3661}
3662
buildbot@webrtc.orgd1ae89f2014-05-08 19:19:26 +00003663bool WebRtcVideoMediaChannel::SetSendCodec(const webrtc::VideoCodec& codec) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003664 bool ret_val = true;
3665 for (SendChannelMap::iterator iter = send_channels_.begin();
3666 iter != send_channels_.end(); ++iter) {
3667 WebRtcVideoChannelSendInfo* send_channel = iter->second;
buildbot@webrtc.orgd1ae89f2014-05-08 19:19:26 +00003668 ret_val = SetSendCodec(send_channel, codec) && ret_val;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003669 }
3670 if (ret_val) {
3671 // All SetSendCodec calls were successful. Update the global state
3672 // accordingly.
3673 send_codec_.reset(new webrtc::VideoCodec(codec));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003674 } else {
3675 // At least one SetSendCodec call failed, rollback.
3676 for (SendChannelMap::iterator iter = send_channels_.begin();
3677 iter != send_channels_.end(); ++iter) {
3678 WebRtcVideoChannelSendInfo* send_channel = iter->second;
3679 if (send_codec_) {
buildbot@webrtc.orgd1ae89f2014-05-08 19:19:26 +00003680 SetSendCodec(send_channel, *send_codec_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003681 }
3682 }
3683 }
3684 return ret_val;
3685}
3686
3687bool WebRtcVideoMediaChannel::SetSendCodec(
3688 WebRtcVideoChannelSendInfo* send_channel,
buildbot@webrtc.orgd1ae89f2014-05-08 19:19:26 +00003689 const webrtc::VideoCodec& codec) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003690 if (!send_channel) {
3691 return false;
3692 }
buildbot@webrtc.orgd1ae89f2014-05-08 19:19:26 +00003693
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003694 const int channel_id = send_channel->channel_id();
3695 // Make a copy of the codec
3696 webrtc::VideoCodec target_codec = codec;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003697
3698 // Set the default number of temporal layers for VP8.
3699 if (webrtc::kVideoCodecVP8 == codec.codecType) {
3700 target_codec.codecSpecific.VP8.numberOfTemporalLayers =
3701 kDefaultNumberOfTemporalLayers;
3702
3703 // Turn off the VP8 error resilience
3704 target_codec.codecSpecific.VP8.resilience = webrtc::kResilienceOff;
3705
3706 bool enable_denoising =
3707 options_.video_noise_reduction.GetWithDefaultIfUnset(false);
3708 target_codec.codecSpecific.VP8.denoisingOn = enable_denoising;
3709 }
3710
3711 // Register external encoder if codec type is supported by encoder factory.
3712 if (engine()->IsExternalEncoderCodecType(codec.codecType) &&
3713 !send_channel->IsEncoderRegistered(target_codec.plType)) {
3714 webrtc::VideoEncoder* encoder =
3715 engine()->CreateExternalEncoder(codec.codecType);
3716 if (encoder) {
3717 if (engine()->vie()->ext_codec()->RegisterExternalSendCodec(
3718 channel_id, target_codec.plType, encoder, false) == 0) {
3719 send_channel->RegisterEncoder(target_codec.plType, encoder);
3720 } else {
3721 LOG_RTCERR2(RegisterExternalSendCodec, channel_id, target_codec.plName);
3722 engine()->DestroyExternalEncoder(encoder);
3723 }
3724 }
3725 }
3726
3727 // Resolution and framerate may vary for different send channels.
3728 const VideoFormat& video_format = send_channel->video_format();
3729 UpdateVideoCodec(video_format, &target_codec);
3730
3731 if (target_codec.width == 0 && target_codec.height == 0) {
3732 const uint32 ssrc = send_channel->stream_params()->first_ssrc();
3733 LOG(LS_INFO) << "0x0 resolution selected. Captured frames will be dropped "
3734 << "for ssrc: " << ssrc << ".";
3735 } else {
buildbot@webrtc.orgd1ae89f2014-05-08 19:19:26 +00003736 MaybeChangeBitrates(channel_id, &target_codec);
wu@webrtc.org05e7b442014-04-01 17:44:24 +00003737 webrtc::VideoCodec current_codec;
3738 if (!engine()->vie()->codec()->GetSendCodec(channel_id, current_codec)) {
3739 // Compare against existing configured send codec.
3740 if (current_codec == target_codec) {
3741 // Codec is already configured on channel. no need to apply.
3742 return true;
3743 }
3744 }
3745
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003746 if (0 != engine()->vie()->codec()->SetSendCodec(channel_id, target_codec)) {
3747 LOG_RTCERR2(SetSendCodec, channel_id, target_codec.plName);
3748 return false;
3749 }
3750
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00003751 // NOTE: SetRtxSendPayloadType must be called after all simulcast SSRCs
3752 // are configured. Otherwise ssrc's configured after this point will use
3753 // the primary PT for RTX.
3754 if (send_rtx_type_ != -1 &&
3755 engine()->vie()->rtp()->SetRtxSendPayloadType(channel_id,
3756 send_rtx_type_) != 0) {
3757 LOG_RTCERR2(SetRtxSendPayloadType, channel_id, send_rtx_type_);
3758 return false;
3759 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003760 }
3761 send_channel->set_interval(
3762 cricket::VideoFormat::FpsToInterval(target_codec.maxFramerate));
3763 return true;
3764}
3765
3766
3767static std::string ToString(webrtc::VideoCodecComplexity complexity) {
3768 switch (complexity) {
3769 case webrtc::kComplexityNormal:
3770 return "normal";
3771 case webrtc::kComplexityHigh:
3772 return "high";
3773 case webrtc::kComplexityHigher:
3774 return "higher";
3775 case webrtc::kComplexityMax:
3776 return "max";
3777 default:
3778 return "unknown";
3779 }
3780}
3781
3782static std::string ToString(webrtc::VP8ResilienceMode resilience) {
3783 switch (resilience) {
3784 case webrtc::kResilienceOff:
3785 return "off";
3786 case webrtc::kResilientStream:
3787 return "stream";
3788 case webrtc::kResilientFrames:
3789 return "frames";
3790 default:
3791 return "unknown";
3792 }
3793}
3794
3795void WebRtcVideoMediaChannel::LogSendCodecChange(const std::string& reason) {
3796 webrtc::VideoCodec vie_codec;
3797 if (engine()->vie()->codec()->GetSendCodec(vie_channel_, vie_codec) != 0) {
3798 LOG_RTCERR1(GetSendCodec, vie_channel_);
3799 return;
3800 }
3801
3802 LOG(LS_INFO) << reason << " : selected video codec "
3803 << vie_codec.plName << "/"
3804 << vie_codec.width << "x" << vie_codec.height << "x"
3805 << static_cast<int>(vie_codec.maxFramerate) << "fps"
3806 << "@" << vie_codec.maxBitrate << "kbps"
3807 << " (min=" << vie_codec.minBitrate << "kbps,"
3808 << " start=" << vie_codec.startBitrate << "kbps)";
3809 LOG(LS_INFO) << "Video max quantization: " << vie_codec.qpMax;
3810 if (webrtc::kVideoCodecVP8 == vie_codec.codecType) {
3811 LOG(LS_INFO) << "VP8 number of temporal layers: "
3812 << static_cast<int>(
3813 vie_codec.codecSpecific.VP8.numberOfTemporalLayers);
3814 LOG(LS_INFO) << "VP8 options : "
3815 << "picture loss indication = "
3816 << vie_codec.codecSpecific.VP8.pictureLossIndicationOn
3817 << ", feedback mode = "
3818 << vie_codec.codecSpecific.VP8.feedbackModeOn
3819 << ", complexity = "
3820 << ToString(vie_codec.codecSpecific.VP8.complexity)
3821 << ", resilience = "
3822 << ToString(vie_codec.codecSpecific.VP8.resilience)
3823 << ", denoising = "
3824 << vie_codec.codecSpecific.VP8.denoisingOn
3825 << ", error concealment = "
3826 << vie_codec.codecSpecific.VP8.errorConcealmentOn
3827 << ", automatic resize = "
3828 << vie_codec.codecSpecific.VP8.automaticResizeOn
3829 << ", frame dropping = "
3830 << vie_codec.codecSpecific.VP8.frameDroppingOn
3831 << ", key frame interval = "
3832 << vie_codec.codecSpecific.VP8.keyFrameInterval;
3833 }
3834
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00003835 if (send_rtx_type_ != -1) {
3836 LOG(LS_INFO) << "RTX payload type: " << send_rtx_type_;
3837 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003838}
3839
3840bool WebRtcVideoMediaChannel::SetReceiveCodecs(
3841 WebRtcVideoChannelRecvInfo* info) {
3842 int red_type = -1;
3843 int fec_type = -1;
3844 int channel_id = info->channel_id();
buildbot@webrtc.orgdd4742a2014-05-07 14:50:35 +00003845 // Build a map from payload types to video codecs so that we easily can find
3846 // out if associated payload types are referring to valid codecs.
3847 std::map<int, webrtc::VideoCodec*> pt_to_codec;
3848 for (std::vector<webrtc::VideoCodec>::iterator it = receive_codecs_.begin();
3849 it != receive_codecs_.end(); ++it) {
3850 pt_to_codec[it->plType] = &(*it);
3851 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003852 for (std::vector<webrtc::VideoCodec>::iterator it = receive_codecs_.begin();
3853 it != receive_codecs_.end(); ++it) {
3854 if (it->codecType == webrtc::kVideoCodecRED) {
3855 red_type = it->plType;
3856 } else if (it->codecType == webrtc::kVideoCodecULPFEC) {
3857 fec_type = it->plType;
3858 }
buildbot@webrtc.orgdd4742a2014-05-07 14:50:35 +00003859 // If this is an RTX codec we have to verify that it is associated with
3860 // a valid video codec which we have RTX support for.
3861 if (_stricmp(it->plName, kRtxCodecName) == 0) {
3862 std::map<int, int>::iterator apt_it = associated_payload_types_.find(
3863 it->plType);
3864 bool valid_apt = false;
3865 if (apt_it != associated_payload_types_.end()) {
3866 std::map<int, webrtc::VideoCodec*>::iterator codec_it =
3867 pt_to_codec.find(apt_it->second);
3868 // We currently only support RTX associated with VP8 due to limitations
3869 // in webrtc where only one RTX payload type can be registered.
3870 valid_apt = codec_it != pt_to_codec.end() &&
3871 _stricmp(codec_it->second->plName, kVp8PayloadName) == 0;
3872 }
3873 if (!valid_apt) {
3874 LOG(LS_ERROR) << "The RTX codec isn't associated with a known and "
3875 "supported payload type";
3876 return false;
3877 }
3878 if (engine()->vie()->rtp()->SetRtxReceivePayloadType(
3879 channel_id, it->plType) != 0) {
3880 LOG_RTCERR2(SetRtxReceivePayloadType, channel_id, it->plType);
3881 return false;
3882 }
3883 continue;
3884 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003885 if (engine()->vie()->codec()->SetReceiveCodec(channel_id, *it) != 0) {
3886 LOG_RTCERR2(SetReceiveCodec, channel_id, it->plName);
3887 return false;
3888 }
3889 if (!info->IsDecoderRegistered(it->plType) &&
3890 it->codecType != webrtc::kVideoCodecRED &&
3891 it->codecType != webrtc::kVideoCodecULPFEC) {
3892 webrtc::VideoDecoder* decoder =
3893 engine()->CreateExternalDecoder(it->codecType);
3894 if (decoder) {
3895 if (engine()->vie()->ext_codec()->RegisterExternalReceiveCodec(
3896 channel_id, it->plType, decoder) == 0) {
3897 info->RegisterDecoder(it->plType, decoder);
3898 } else {
3899 LOG_RTCERR2(RegisterExternalReceiveCodec, channel_id, it->plName);
3900 engine()->DestroyExternalDecoder(decoder);
3901 }
3902 }
3903 }
3904 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003905 return true;
3906}
3907
3908int WebRtcVideoMediaChannel::GetRecvChannelNum(uint32 ssrc) {
3909 if (ssrc == first_receive_ssrc_) {
3910 return vie_channel_;
3911 }
buildbot@webrtc.orgdd4742a2014-05-07 14:50:35 +00003912 int recv_channel = -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003913 RecvChannelMap::iterator it = recv_channels_.find(ssrc);
buildbot@webrtc.orgdd4742a2014-05-07 14:50:35 +00003914 if (it == recv_channels_.end()) {
3915 // Check if we have an RTX stream registered on this SSRC.
3916 SsrcMap::iterator rtx_it = rtx_to_primary_ssrc_.find(ssrc);
3917 if (rtx_it != rtx_to_primary_ssrc_.end()) {
3918 it = recv_channels_.find(rtx_it->second);
3919 assert(it != recv_channels_.end());
3920 recv_channel = it->second->channel_id();
3921 }
3922 } else {
3923 recv_channel = it->second->channel_id();
3924 }
3925 return recv_channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003926}
3927
3928// If the new frame size is different from the send codec size we set on vie,
3929// we need to reset the send codec on vie.
3930// The new send codec size should not exceed send_codec_ which is controlled
3931// only by the 'jec' logic.
buildbot@webrtc.orgd1ae89f2014-05-08 19:19:26 +00003932// TODO(pthatcher): Get rid of this function, so we only ever set up
3933// codecs in a single place.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003934bool WebRtcVideoMediaChannel::MaybeResetVieSendCodec(
3935 WebRtcVideoChannelSendInfo* send_channel,
3936 int new_width,
3937 int new_height,
3938 bool is_screencast,
3939 bool* reset) {
3940 if (reset) {
3941 *reset = false;
3942 }
3943 ASSERT(send_codec_.get() != NULL);
3944
buildbot@webrtc.orgd1ae89f2014-05-08 19:19:26 +00003945 webrtc::VideoCodec target_codec = *send_codec_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003946 const VideoFormat& video_format = send_channel->video_format();
3947 UpdateVideoCodec(video_format, &target_codec);
3948
3949 // Vie send codec size should not exceed target_codec.
3950 int target_width = new_width;
3951 int target_height = new_height;
3952 if (!is_screencast &&
3953 (new_width > target_codec.width || new_height > target_codec.height)) {
3954 target_width = target_codec.width;
3955 target_height = target_codec.height;
3956 }
3957
3958 // Get current vie codec.
3959 webrtc::VideoCodec vie_codec;
3960 const int channel_id = send_channel->channel_id();
3961 if (engine()->vie()->codec()->GetSendCodec(channel_id, vie_codec) != 0) {
3962 LOG_RTCERR1(GetSendCodec, channel_id);
3963 return false;
3964 }
3965 const int cur_width = vie_codec.width;
3966 const int cur_height = vie_codec.height;
3967
3968 // Only reset send codec when there is a size change. Additionally,
3969 // automatic resize needs to be turned off when screencasting and on when
3970 // not screencasting.
3971 // Don't allow automatic resizing for screencasting.
3972 bool automatic_resize = !is_screencast;
3973 // Turn off VP8 frame dropping when screensharing as the current model does
3974 // not work well at low fps.
3975 bool vp8_frame_dropping = !is_screencast;
3976 // Disable denoising for screencasting.
3977 bool enable_denoising =
3978 options_.video_noise_reduction.GetWithDefaultIfUnset(false);
henrike@webrtc.orgdce3feb2014-03-26 01:17:30 +00003979 int screencast_min_bitrate =
3980 options_.screencast_min_bitrate.GetWithDefaultIfUnset(0);
3981 bool leaky_bucket = options_.video_leaky_bucket.GetWithDefaultIfUnset(false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003982 bool denoising = !is_screencast && enable_denoising;
3983 bool reset_send_codec =
3984 target_width != cur_width || target_height != cur_height ||
3985 automatic_resize != vie_codec.codecSpecific.VP8.automaticResizeOn ||
3986 denoising != vie_codec.codecSpecific.VP8.denoisingOn ||
3987 vp8_frame_dropping != vie_codec.codecSpecific.VP8.frameDroppingOn;
3988
3989 if (reset_send_codec) {
3990 // Set the new codec on vie.
3991 vie_codec.width = target_width;
3992 vie_codec.height = target_height;
3993 vie_codec.maxFramerate = target_codec.maxFramerate;
3994 vie_codec.startBitrate = target_codec.startBitrate;
buildbot@webrtc.orgd1ae89f2014-05-08 19:19:26 +00003995 vie_codec.minBitrate = target_codec.minBitrate;
3996 vie_codec.maxBitrate = target_codec.maxBitrate;
henrike@webrtc.org6e3dbc22014-03-25 17:09:47 +00003997 vie_codec.targetBitrate = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003998 vie_codec.codecSpecific.VP8.automaticResizeOn = automatic_resize;
3999 vie_codec.codecSpecific.VP8.denoisingOn = denoising;
4000 vie_codec.codecSpecific.VP8.frameDroppingOn = vp8_frame_dropping;
buildbot@webrtc.orgd1ae89f2014-05-08 19:19:26 +00004001 MaybeChangeBitrates(channel_id, &vie_codec);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00004002
4003 if (engine()->vie()->codec()->SetSendCodec(channel_id, vie_codec) != 0) {
4004 LOG_RTCERR1(SetSendCodec, channel_id);
4005 return false;
4006 }
henrike@webrtc.orgdce3feb2014-03-26 01:17:30 +00004007
henrike@webrtc.orgdce3feb2014-03-26 01:17:30 +00004008 if (is_screencast) {
4009 engine()->vie()->rtp()->SetMinTransmitBitrate(channel_id,
4010 screencast_min_bitrate);
4011 // If screencast and min bitrate set, force enable pacer.
4012 if (screencast_min_bitrate > 0) {
4013 engine()->vie()->rtp()->SetTransmissionSmoothingStatus(channel_id,
4014 true);
4015 }
4016 } else {
4017 // In case of switching from screencast to regular capture, set
4018 // min bitrate padding and pacer back to defaults.
4019 engine()->vie()->rtp()->SetMinTransmitBitrate(channel_id, 0);
4020 engine()->vie()->rtp()->SetTransmissionSmoothingStatus(channel_id,
4021 leaky_bucket);
4022 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00004023 if (reset) {
4024 *reset = true;
4025 }
4026 LogSendCodecChange("Capture size changed");
4027 }
4028
4029 return true;
4030}
4031
buildbot@webrtc.orgd1ae89f2014-05-08 19:19:26 +00004032void WebRtcVideoMediaChannel::MaybeChangeBitrates(
4033 int channel_id, webrtc::VideoCodec* codec) {
4034 codec->minBitrate = GetBitrate(codec->minBitrate, kMinVideoBitrate);
4035 codec->startBitrate = GetBitrate(codec->startBitrate, kStartVideoBitrate);
4036 codec->maxBitrate = GetBitrate(codec->maxBitrate, kMaxVideoBitrate);
4037
4038 if (codec->minBitrate > codec->maxBitrate) {
4039 LOG(LS_INFO) << "Decreasing codec min bitrate to the max ("
4040 << codec->maxBitrate << ") because the min ("
4041 << codec->minBitrate << ") exceeds the max.";
4042 codec->minBitrate = codec->maxBitrate;
4043 }
4044 if (codec->startBitrate < codec->minBitrate) {
4045 LOG(LS_INFO) << "Increasing codec start bitrate to the min ("
4046 << codec->minBitrate << ") because the start ("
4047 << codec->startBitrate << ") is less than the min.";
4048 codec->startBitrate = codec->minBitrate;
4049 } else if (codec->startBitrate > codec->maxBitrate) {
4050 LOG(LS_INFO) << "Decreasing codec start bitrate to the max ("
4051 << codec->maxBitrate << ") because the start ("
4052 << codec->startBitrate << ") exceeds the max.";
4053 codec->startBitrate = codec->maxBitrate;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00004054 }
4055
4056 // Use a previous target bitrate, if there is one.
4057 unsigned int current_target_bitrate = 0;
4058 if (engine()->vie()->codec()->GetCodecTargetBitrate(
4059 channel_id, &current_target_bitrate) == 0) {
4060 // Convert to kbps.
4061 current_target_bitrate /= 1000;
buildbot@webrtc.orgd1ae89f2014-05-08 19:19:26 +00004062 if (current_target_bitrate > codec->maxBitrate) {
4063 current_target_bitrate = codec->maxBitrate;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00004064 }
buildbot@webrtc.orgd1ae89f2014-05-08 19:19:26 +00004065 if (current_target_bitrate > codec->startBitrate) {
4066 codec->startBitrate = current_target_bitrate;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00004067 }
4068 }
4069}
4070
4071void WebRtcVideoMediaChannel::OnMessage(talk_base::Message* msg) {
4072 FlushBlackFrameData* black_frame_data =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00004073 static_cast<FlushBlackFrameData*>(msg->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00004074 FlushBlackFrame(black_frame_data->ssrc, black_frame_data->timestamp);
4075 delete black_frame_data;
4076}
4077
4078int WebRtcVideoMediaChannel::SendPacket(int channel, const void* data,
4079 int len) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00004080 talk_base::Buffer packet(data, len, kMaxRtpPacketLen);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00004081 return MediaChannel::SendPacket(&packet) ? len : -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00004082}
4083
4084int WebRtcVideoMediaChannel::SendRTCPPacket(int channel,
4085 const void* data,
4086 int len) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00004087 talk_base::Buffer packet(data, len, kMaxRtpPacketLen);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00004088 return MediaChannel::SendRtcp(&packet) ? len : -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00004089}
4090
4091void WebRtcVideoMediaChannel::QueueBlackFrame(uint32 ssrc, int64 timestamp,
4092 int framerate) {
4093 if (timestamp) {
4094 FlushBlackFrameData* black_frame_data = new FlushBlackFrameData(
4095 ssrc,
4096 timestamp);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00004097 const int delay_ms = static_cast<int>(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00004098 2 * cricket::VideoFormat::FpsToInterval(framerate) *
4099 talk_base::kNumMillisecsPerSec / talk_base::kNumNanosecsPerSec);
4100 worker_thread()->PostDelayed(delay_ms, this, 0, black_frame_data);
4101 }
4102}
4103
4104void WebRtcVideoMediaChannel::FlushBlackFrame(uint32 ssrc, int64 timestamp) {
4105 WebRtcVideoChannelSendInfo* send_channel = GetSendChannel(ssrc);
4106 if (!send_channel) {
4107 return;
4108 }
4109 talk_base::scoped_ptr<const VideoFrame> black_frame_ptr;
4110
4111 const WebRtcLocalStreamInfo* channel_stream_info =
4112 send_channel->local_stream_info();
4113 int64 last_frame_time_stamp = channel_stream_info->time_stamp();
4114 if (last_frame_time_stamp == timestamp) {
4115 size_t last_frame_width = 0;
4116 size_t last_frame_height = 0;
4117 int64 last_frame_elapsed_time = 0;
4118 channel_stream_info->GetLastFrameInfo(&last_frame_width, &last_frame_height,
4119 &last_frame_elapsed_time);
4120 if (!last_frame_width || !last_frame_height) {
4121 return;
4122 }
4123 WebRtcVideoFrame black_frame;
4124 // Black frame is not screencast.
4125 const bool screencasting = false;
4126 const int64 timestamp_delta = send_channel->interval();
4127 if (!black_frame.InitToBlack(send_codec_->width, send_codec_->height, 1, 1,
4128 last_frame_elapsed_time + timestamp_delta,
4129 last_frame_time_stamp + timestamp_delta) ||
4130 !SendFrame(send_channel, &black_frame, screencasting)) {
4131 LOG(LS_ERROR) << "Failed to send black frame.";
4132 }
4133 }
4134}
4135
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00004136void WebRtcVideoMediaChannel::OnCpuAdaptationUnable() {
4137 // ssrc is hardcoded to 0. This message is based on a system wide issue,
4138 // so finding which ssrc caused it doesn't matter.
4139 SignalMediaError(0, VideoMediaChannel::ERROR_REC_CPU_MAX_CANT_DOWNGRADE);
4140}
4141
henrike@webrtc.org28e20752013-07-10 00:45:36 +00004142void WebRtcVideoMediaChannel::SetNetworkTransmissionState(
4143 bool is_transmitting) {
4144 LOG(LS_INFO) << "SetNetworkTransmissionState: " << is_transmitting;
4145 for (SendChannelMap::iterator iter = send_channels_.begin();
4146 iter != send_channels_.end(); ++iter) {
4147 WebRtcVideoChannelSendInfo* send_channel = iter->second;
4148 int channel_id = send_channel->channel_id();
4149 engine_->vie()->network()->SetNetworkTransmissionState(channel_id,
4150 is_transmitting);
4151 }
4152}
4153
4154bool WebRtcVideoMediaChannel::SetHeaderExtension(ExtensionSetterFunction setter,
4155 int channel_id, const RtpHeaderExtension* extension) {
4156 bool enable = false;
4157 int id = 0;
4158 if (extension) {
4159 enable = true;
4160 id = extension->id;
4161 }
4162 if ((engine_->vie()->rtp()->*setter)(channel_id, enable, id) != 0) {
4163 LOG_RTCERR4(*setter, extension->uri, channel_id, enable, id);
4164 return false;
4165 }
4166 return true;
4167}
4168
4169bool WebRtcVideoMediaChannel::SetHeaderExtension(ExtensionSetterFunction setter,
4170 int channel_id, const std::vector<RtpHeaderExtension>& extensions,
4171 const char header_extension_uri[]) {
4172 const RtpHeaderExtension* extension = FindHeaderExtension(extensions,
4173 header_extension_uri);
4174 return SetHeaderExtension(setter, channel_id, extension);
4175}
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00004176
4177bool WebRtcVideoMediaChannel::SetLocalRtxSsrc(int channel_id,
4178 const StreamParams& send_params,
4179 uint32 primary_ssrc,
4180 int stream_idx) {
4181 uint32 rtx_ssrc = 0;
4182 bool has_rtx = send_params.GetFidSsrc(primary_ssrc, &rtx_ssrc);
4183 if (has_rtx && engine()->vie()->rtp()->SetLocalSSRC(
4184 channel_id, rtx_ssrc, webrtc::kViEStreamTypeRtx, stream_idx) != 0) {
4185 LOG_RTCERR4(SetLocalSSRC, channel_id, rtx_ssrc,
4186 webrtc::kViEStreamTypeRtx, stream_idx);
4187 return false;
4188 }
4189 return true;
4190}
4191
wu@webrtc.org24301a62013-12-13 19:17:43 +00004192void WebRtcVideoMediaChannel::MaybeConnectCapturer(VideoCapturer* capturer) {
4193 if (capturer != NULL && GetSendChannelNum(capturer) == 1) {
wu@webrtc.orgf7d501d2014-03-27 23:48:25 +00004194 capturer->SignalVideoFrame.connect(this,
4195 &WebRtcVideoMediaChannel::SendFrame);
wu@webrtc.org24301a62013-12-13 19:17:43 +00004196 }
4197}
4198
4199void WebRtcVideoMediaChannel::MaybeDisconnectCapturer(VideoCapturer* capturer) {
4200 if (capturer != NULL && GetSendChannelNum(capturer) == 1) {
4201 capturer->SignalVideoFrame.disconnect(this);
4202 }
4203}
4204
henrike@webrtc.org28e20752013-07-10 00:45:36 +00004205} // namespace cricket
4206
4207#endif // HAVE_WEBRTC_VIDEO