blob: a029ec27e43c5866c6118a5c6decb5f993b6da22 [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.org251fdf62014-06-03 23:43:48 +000065#ifdef WEBRTC_CHROMIUM_BUILD
66#include "webrtc/system_wrappers/interface/field_trial.h"
67#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +000068
69#if !defined(LIBPEERCONNECTION_LIB)
henrike@webrtc.org28e20752013-07-10 00:45:36 +000070#include "talk/media/webrtc/webrtcmediaengine.h"
71
72WRME_EXPORT
73cricket::MediaEngineInterface* CreateWebRtcMediaEngine(
74 webrtc::AudioDeviceModule* adm, webrtc::AudioDeviceModule* adm_sc,
75 cricket::WebRtcVideoEncoderFactory* encoder_factory,
76 cricket::WebRtcVideoDecoderFactory* decoder_factory) {
buildbot@webrtc.org251fdf62014-06-03 23:43:48 +000077#ifdef WEBRTC_CHROMIUM_BUILD
78 if (webrtc::field_trial::FindFullName("WebRTC-NewVideoAPI") == "Enabled") {
79 return new cricket::WebRtcMediaEngine2(
80 adm, adm_sc, encoder_factory, decoder_factory);
81 } else {
82#endif
83 return new cricket::WebRtcMediaEngine(
84 adm, adm_sc, encoder_factory, decoder_factory);
85#ifdef WEBRTC_CHROMIUM_BUILD
86 }
87#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +000088}
89
90WRME_EXPORT
91void DestroyWebRtcMediaEngine(cricket::MediaEngineInterface* media_engine) {
buildbot@webrtc.org251fdf62014-06-03 23:43:48 +000092#ifdef WEBRTC_CHROMIUM_BUILD
93 if (webrtc::field_trial::FindFullName("WebRTC-NewVideoAPI") == "Enabled") {
94 delete static_cast<cricket::WebRtcMediaEngine2*>(media_engine);
95 } else {
96#endif
97 delete static_cast<cricket::WebRtcMediaEngine*>(media_engine);
98#ifdef WEBRTC_CHROMIUM_BUILD
99 }
100#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000101}
102#endif
103
104
105namespace cricket {
106
107
108static const int kDefaultLogSeverity = talk_base::LS_WARNING;
109
110static const int kMinVideoBitrate = 50;
111static const int kStartVideoBitrate = 300;
112static const int kMaxVideoBitrate = 2000;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000113
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000114// Controlled by exp, try a super low minimum bitrate for poor connections.
115static const int kLowerMinBitrate = 30;
116
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000117static const int kVideoMtu = 1200;
118
119static const int kVideoRtpBufferSize = 65536;
120
121static const char kVp8PayloadName[] = "VP8";
122static const char kRedPayloadName[] = "red";
123static const char kFecPayloadName[] = "ulpfec";
124
125static const int kDefaultNumberOfTemporalLayers = 1; // 1:1
126
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000127static const int kExternalVideoPayloadTypeBase = 120;
128
buildbot@webrtc.org073dfdd2014-05-08 19:36:21 +0000129static bool BitrateIsSet(int value) {
buildbot@webrtc.orgd1ae89f2014-05-08 19:19:26 +0000130 return value > kAutoBandwidth;
131}
132
buildbot@webrtc.org073dfdd2014-05-08 19:36:21 +0000133static int GetBitrate(int value, int deflt) {
buildbot@webrtc.orgd1ae89f2014-05-08 19:19:26 +0000134 return BitrateIsSet(value) ? value : deflt;
135}
136
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000137// Static allocation of payload type values for external video codec.
138static int GetExternalVideoPayloadType(int index) {
buildbot@webrtc.org34a08b42014-06-02 15:48:10 +0000139#if ENABLE_DEBUG
140 static const int kMaxExternalVideoCodecs = 8;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000141 ASSERT(index >= 0 && index < kMaxExternalVideoCodecs);
buildbot@webrtc.org34a08b42014-06-02 15:48:10 +0000142#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000143 return kExternalVideoPayloadTypeBase + index;
144}
145
146static void LogMultiline(talk_base::LoggingSeverity sev, char* text) {
147 const char* delim = "\r\n";
buildbot@webrtc.org0cdcd232014-06-04 01:31:14 +0000148 // TODO(fbarchard): Fix strtok lint warning.
149 for (char* tok = strtok(text, delim); tok; tok = strtok(NULL, delim)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000150 LOG_V(sev) << tok;
151 }
152}
153
154// Severity is an integer because it comes is assumed to be from command line.
155static int SeverityToFilter(int severity) {
156 int filter = webrtc::kTraceNone;
157 switch (severity) {
158 case talk_base::LS_VERBOSE:
159 filter |= webrtc::kTraceAll;
160 case talk_base::LS_INFO:
161 filter |= (webrtc::kTraceStateInfo | webrtc::kTraceInfo);
162 case talk_base::LS_WARNING:
163 filter |= (webrtc::kTraceTerseInfo | webrtc::kTraceWarning);
164 case talk_base::LS_ERROR:
165 filter |= (webrtc::kTraceError | webrtc::kTraceCritical);
166 }
167 return filter;
168}
169
170static const int kCpuMonitorPeriodMs = 2000; // 2 seconds.
171
172static const bool kNotSending = false;
173
wu@webrtc.orgde305012013-10-31 15:40:38 +0000174// Default video dscp value.
175// See http://tools.ietf.org/html/rfc2474 for details
176// See also http://tools.ietf.org/html/draft-jennings-rtcweb-qos-00
177static const talk_base::DiffServCodePoint kVideoDscpValue =
178 talk_base::DSCP_AF41;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000179
180static bool IsNackEnabled(const VideoCodec& codec) {
181 return codec.HasFeedbackParam(FeedbackParam(kRtcpFbParamNack,
182 kParamValueEmpty));
183}
184
185// Returns true if Receiver Estimated Max Bitrate is enabled.
186static bool IsRembEnabled(const VideoCodec& codec) {
187 return codec.HasFeedbackParam(FeedbackParam(kRtcpFbParamRemb,
188 kParamValueEmpty));
189}
190
191struct FlushBlackFrameData : public talk_base::MessageData {
192 FlushBlackFrameData(uint32 s, int64 t) : ssrc(s), timestamp(t) {
193 }
194 uint32 ssrc;
195 int64 timestamp;
196};
197
198class WebRtcRenderAdapter : public webrtc::ExternalRenderer {
199 public:
buildbot@webrtc.orgf875f152014-04-14 16:06:21 +0000200 WebRtcRenderAdapter(VideoRenderer* renderer, int channel_id)
buildbot@webrtc.org0581f0b2014-05-06 21:36:31 +0000201 : renderer_(renderer),
202 channel_id_(channel_id),
203 width_(0),
204 height_(0),
buildbot@webrtc.orgf9f1bfb2014-05-21 17:02:15 +0000205 capture_start_rtp_time_stamp_(-1),
buildbot@webrtc.org0581f0b2014-05-06 21:36:31 +0000206 capture_start_ntp_time_ms_(0) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000207 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000208
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000209 virtual ~WebRtcRenderAdapter() {
210 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000211
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000212 void SetRenderer(VideoRenderer* renderer) {
213 talk_base::CritScope cs(&crit_);
214 renderer_ = renderer;
215 // FrameSizeChange may have already been called when renderer was not set.
216 // If so we should call SetSize here.
217 // TODO(ronghuawu): Add unit test for this case. Didn't do it now
218 // because the WebRtcRenderAdapter is currently hiding in cc file. No
219 // good way to get access to it from the unit test.
220 if (width_ > 0 && height_ > 0 && renderer_ != NULL) {
221 if (!renderer_->SetSize(width_, height_, 0)) {
222 LOG(LS_ERROR)
buildbot@webrtc.orgf875f152014-04-14 16:06:21 +0000223 << "WebRtcRenderAdapter (channel " << channel_id_
224 << ") SetRenderer failed to SetSize to: "
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000225 << width_ << "x" << height_;
226 }
227 }
228 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000229
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000230 // Implementation of webrtc::ExternalRenderer.
231 virtual int FrameSizeChange(unsigned int width, unsigned int height,
232 unsigned int /*number_of_streams*/) {
233 talk_base::CritScope cs(&crit_);
234 width_ = width;
235 height_ = height;
buildbot@webrtc.orgf875f152014-04-14 16:06:21 +0000236 LOG(LS_INFO) << "WebRtcRenderAdapter (channel " << channel_id_
237 << ") frame size changed to: "
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000238 << width << "x" << height;
239 if (renderer_ == NULL) {
buildbot@webrtc.orgf875f152014-04-14 16:06:21 +0000240 LOG(LS_VERBOSE) << "WebRtcRenderAdapter (channel " << channel_id_
241 << ") the renderer has not been set. "
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000242 << "SetSize will be called later in SetRenderer.";
243 return 0;
244 }
245 return renderer_->SetSize(width_, height_, 0) ? 0 : -1;
246 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000247
buildbot@webrtc.org1fd5b452014-04-15 17:39:43 +0000248 virtual int DeliverFrame(unsigned char* buffer,
249 int buffer_size,
buildbot@webrtc.org0581f0b2014-05-06 21:36:31 +0000250 uint32_t rtp_time_stamp,
buildbot@webrtc.org1fd5b452014-04-15 17:39:43 +0000251#ifdef USE_WEBRTC_DEV_BRANCH
252 int64_t ntp_time_ms,
253#endif
254 int64_t render_time,
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000255 void* handle) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000256 talk_base::CritScope cs(&crit_);
buildbot@webrtc.orgf9f1bfb2014-05-21 17:02:15 +0000257 if (capture_start_rtp_time_stamp_ < 0) {
buildbot@webrtc.org0581f0b2014-05-06 21:36:31 +0000258 capture_start_rtp_time_stamp_ = rtp_time_stamp;
259 }
buildbot@webrtc.org22190372014-05-07 17:52:33 +0000260
261 const int kVideoCodecClockratekHz = cricket::kVideoCodecClockrate / 1000;
262
buildbot@webrtc.org0581f0b2014-05-06 21:36:31 +0000263#ifdef USE_WEBRTC_DEV_BRANCH
264 if (ntp_time_ms > 0) {
buildbot@webrtc.orgf9f1bfb2014-05-21 17:02:15 +0000265 int64 elapsed_time_ms =
266 (rtp_ts_wraparound_handler_.Unwrap(rtp_time_stamp) -
267 capture_start_rtp_time_stamp_) / kVideoCodecClockratekHz;
buildbot@webrtc.org0581f0b2014-05-06 21:36:31 +0000268 capture_start_ntp_time_ms_ = ntp_time_ms - elapsed_time_ms;
269 }
270#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000271 frame_rate_tracker_.Update(1);
272 if (renderer_ == NULL) {
273 return 0;
274 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000275 // Convert 90K rtp timestamp to ns timestamp.
buildbot@webrtc.org0581f0b2014-05-06 21:36:31 +0000276 int64 rtp_time_stamp_in_ns = (rtp_time_stamp / kVideoCodecClockratekHz) *
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000277 talk_base::kNumNanosecsPerMillisec;
278 // Convert milisecond render time to ns timestamp.
279 int64 render_time_stamp_in_ns = render_time *
280 talk_base::kNumNanosecsPerMillisec;
281 // Send the rtp timestamp to renderer as the VideoFrame timestamp.
282 // and the render timestamp as the VideoFrame elapsed_time.
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000283 if (handle == NULL) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000284 return DeliverBufferFrame(buffer, buffer_size, render_time_stamp_in_ns,
buildbot@webrtc.org740e6b32014-04-30 15:33:45 +0000285 rtp_time_stamp_in_ns);
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000286 } else {
287 return DeliverTextureFrame(handle, render_time_stamp_in_ns,
buildbot@webrtc.org740e6b32014-04-30 15:33:45 +0000288 rtp_time_stamp_in_ns);
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000289 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000290 }
291
292 virtual bool IsTextureSupported() { return true; }
293
294 int DeliverBufferFrame(unsigned char* buffer, int buffer_size,
buildbot@webrtc.org0581f0b2014-05-06 21:36:31 +0000295 int64 elapsed_time, int64 rtp_time_stamp_in_ns) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000296 WebRtcVideoFrame video_frame;
wu@webrtc.org16d62542013-11-05 23:45:14 +0000297 video_frame.Alias(buffer, buffer_size, width_, height_,
buildbot@webrtc.org0581f0b2014-05-06 21:36:31 +0000298 1, 1, elapsed_time, rtp_time_stamp_in_ns, 0);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000299
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000300 // Sanity check on decoded frame size.
301 if (buffer_size != static_cast<int>(VideoFrame::SizeOf(width_, height_))) {
buildbot@webrtc.orgf875f152014-04-14 16:06:21 +0000302 LOG(LS_WARNING) << "WebRtcRenderAdapter (channel " << channel_id_
303 << ") received a strange frame size: "
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000304 << buffer_size;
305 }
306
307 int ret = renderer_->RenderFrame(&video_frame) ? 0 : -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000308 return ret;
309 }
310
buildbot@webrtc.org0581f0b2014-05-06 21:36:31 +0000311 int DeliverTextureFrame(void* handle,
312 int64 elapsed_time,
313 int64 rtp_time_stamp_in_ns) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000314 WebRtcTextureVideoFrame video_frame(
315 static_cast<webrtc::NativeHandle*>(handle), width_, height_,
buildbot@webrtc.org0581f0b2014-05-06 21:36:31 +0000316 elapsed_time, rtp_time_stamp_in_ns);
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000317 return renderer_->RenderFrame(&video_frame);
318 }
319
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000320 unsigned int width() {
321 talk_base::CritScope cs(&crit_);
322 return width_;
323 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000324
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000325 unsigned int height() {
326 talk_base::CritScope cs(&crit_);
327 return height_;
328 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000329
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000330 int framerate() {
331 talk_base::CritScope cs(&crit_);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000332 return static_cast<int>(frame_rate_tracker_.units_second());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000333 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000334
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000335 VideoRenderer* renderer() {
336 talk_base::CritScope cs(&crit_);
337 return renderer_;
338 }
339
buildbot@webrtc.org0581f0b2014-05-06 21:36:31 +0000340 int64 capture_start_ntp_time_ms() {
341 talk_base::CritScope cs(&crit_);
342 return capture_start_ntp_time_ms_;
343 }
344
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000345 private:
346 talk_base::CriticalSection crit_;
347 VideoRenderer* renderer_;
buildbot@webrtc.orgf875f152014-04-14 16:06:21 +0000348 int channel_id_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000349 unsigned int width_;
350 unsigned int height_;
351 talk_base::RateTracker frame_rate_tracker_;
buildbot@webrtc.orgf9f1bfb2014-05-21 17:02:15 +0000352 talk_base::TimestampWrapAroundHandler rtp_ts_wraparound_handler_;
353 int64 capture_start_rtp_time_stamp_;
buildbot@webrtc.org0581f0b2014-05-06 21:36:31 +0000354 int64 capture_start_ntp_time_ms_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000355};
356
357class WebRtcDecoderObserver : public webrtc::ViEDecoderObserver {
358 public:
359 explicit WebRtcDecoderObserver(int video_channel)
360 : video_channel_(video_channel),
361 framerate_(0),
362 bitrate_(0),
wu@webrtc.org97077a32013-10-25 21:18:33 +0000363 decode_ms_(0),
364 max_decode_ms_(0),
365 current_delay_ms_(0),
366 target_delay_ms_(0),
367 jitter_buffer_ms_(0),
368 min_playout_delay_ms_(0),
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +0000369 render_delay_ms_(0) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000370 }
371
372 // virtual functions from VieDecoderObserver.
373 virtual void IncomingCodecChanged(const int videoChannel,
374 const webrtc::VideoCodec& videoCodec) {}
375 virtual void IncomingRate(const int videoChannel,
376 const unsigned int framerate,
377 const unsigned int bitrate) {
wu@webrtc.org78187522013-10-07 23:32:02 +0000378 talk_base::CritScope cs(&crit_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000379 ASSERT(video_channel_ == videoChannel);
380 framerate_ = framerate;
381 bitrate_ = bitrate;
382 }
wu@webrtc.org97077a32013-10-25 21:18:33 +0000383
384 virtual void DecoderTiming(int decode_ms,
385 int max_decode_ms,
386 int current_delay_ms,
387 int target_delay_ms,
388 int jitter_buffer_ms,
389 int min_playout_delay_ms,
390 int render_delay_ms) {
391 talk_base::CritScope cs(&crit_);
392 decode_ms_ = decode_ms;
393 max_decode_ms_ = max_decode_ms;
394 current_delay_ms_ = current_delay_ms;
395 target_delay_ms_ = target_delay_ms;
396 jitter_buffer_ms_ = jitter_buffer_ms;
397 min_playout_delay_ms_ = min_playout_delay_ms;
398 render_delay_ms_ = render_delay_ms;
399 }
400
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +0000401 virtual void RequestNewKeyFrame(const int videoChannel) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000402
wu@webrtc.org97077a32013-10-25 21:18:33 +0000403 // Populate |rinfo| based on previously-set data in |*this|.
404 void ExportTo(VideoReceiverInfo* rinfo) {
wu@webrtc.org78187522013-10-07 23:32:02 +0000405 talk_base::CritScope cs(&crit_);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000406 rinfo->framerate_rcvd = framerate_;
407 rinfo->decode_ms = decode_ms_;
408 rinfo->max_decode_ms = max_decode_ms_;
409 rinfo->current_delay_ms = current_delay_ms_;
410 rinfo->target_delay_ms = target_delay_ms_;
411 rinfo->jitter_buffer_ms = jitter_buffer_ms_;
412 rinfo->min_playout_delay_ms = min_playout_delay_ms_;
413 rinfo->render_delay_ms = render_delay_ms_;
wu@webrtc.org78187522013-10-07 23:32:02 +0000414 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000415
416 private:
wu@webrtc.org78187522013-10-07 23:32:02 +0000417 mutable talk_base::CriticalSection crit_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000418 int video_channel_;
419 int framerate_;
420 int bitrate_;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000421 int decode_ms_;
422 int max_decode_ms_;
423 int current_delay_ms_;
424 int target_delay_ms_;
425 int jitter_buffer_ms_;
426 int min_playout_delay_ms_;
427 int render_delay_ms_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000428};
429
430class WebRtcEncoderObserver : public webrtc::ViEEncoderObserver {
431 public:
432 explicit WebRtcEncoderObserver(int video_channel)
433 : video_channel_(video_channel),
434 framerate_(0),
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000435 bitrate_(0),
436 suspended_(false) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000437 }
438
439 // virtual functions from VieEncoderObserver.
440 virtual void OutgoingRate(const int videoChannel,
441 const unsigned int framerate,
442 const unsigned int bitrate) {
wu@webrtc.org78187522013-10-07 23:32:02 +0000443 talk_base::CritScope cs(&crit_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000444 ASSERT(video_channel_ == videoChannel);
445 framerate_ = framerate;
446 bitrate_ = bitrate;
447 }
448
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000449 virtual void SuspendChange(int video_channel, bool is_suspended) {
450 talk_base::CritScope cs(&crit_);
451 ASSERT(video_channel_ == video_channel);
452 suspended_ = is_suspended;
453 }
454
wu@webrtc.org78187522013-10-07 23:32:02 +0000455 int framerate() const {
456 talk_base::CritScope cs(&crit_);
457 return framerate_;
458 }
459 int bitrate() const {
460 talk_base::CritScope cs(&crit_);
461 return bitrate_;
462 }
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000463 bool suspended() const {
464 talk_base::CritScope cs(&crit_);
465 return suspended_;
466 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000467
468 private:
wu@webrtc.org78187522013-10-07 23:32:02 +0000469 mutable talk_base::CriticalSection crit_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000470 int video_channel_;
471 int framerate_;
472 int bitrate_;
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000473 bool suspended_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000474};
475
476class WebRtcLocalStreamInfo {
477 public:
478 WebRtcLocalStreamInfo()
479 : width_(0), height_(0), elapsed_time_(-1), time_stamp_(-1) {}
480 size_t width() const {
481 talk_base::CritScope cs(&crit_);
482 return width_;
483 }
484 size_t height() const {
485 talk_base::CritScope cs(&crit_);
486 return height_;
487 }
488 int64 elapsed_time() const {
489 talk_base::CritScope cs(&crit_);
490 return elapsed_time_;
491 }
492 int64 time_stamp() const {
493 talk_base::CritScope cs(&crit_);
494 return time_stamp_;
495 }
496 int framerate() {
497 talk_base::CritScope cs(&crit_);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000498 return static_cast<int>(rate_tracker_.units_second());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000499 }
500 void GetLastFrameInfo(
501 size_t* width, size_t* height, int64* elapsed_time) const {
502 talk_base::CritScope cs(&crit_);
503 *width = width_;
504 *height = height_;
505 *elapsed_time = elapsed_time_;
506 }
507
508 void UpdateFrame(const VideoFrame* frame) {
509 talk_base::CritScope cs(&crit_);
510
511 width_ = frame->GetWidth();
512 height_ = frame->GetHeight();
513 elapsed_time_ = frame->GetElapsedTime();
514 time_stamp_ = frame->GetTimeStamp();
515
516 rate_tracker_.Update(1);
517 }
518
519 private:
520 mutable talk_base::CriticalSection crit_;
521 size_t width_;
522 size_t height_;
523 int64 elapsed_time_;
524 int64 time_stamp_;
525 talk_base::RateTracker rate_tracker_;
526
527 DISALLOW_COPY_AND_ASSIGN(WebRtcLocalStreamInfo);
528};
529
530// WebRtcVideoChannelRecvInfo is a container class with members such as renderer
531// and a decoder observer that is used by receive channels.
532// It must exist as long as the receive channel is connected to renderer or a
533// decoder observer in this class and methods in the class should only be called
534// from the worker thread.
535class WebRtcVideoChannelRecvInfo {
536 public:
537 typedef std::map<int, webrtc::VideoDecoder*> DecoderMap; // key: payload type
538 explicit WebRtcVideoChannelRecvInfo(int channel_id)
539 : channel_id_(channel_id),
buildbot@webrtc.orgf875f152014-04-14 16:06:21 +0000540 render_adapter_(NULL, channel_id),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000541 decoder_observer_(channel_id) {
542 }
543 int channel_id() { return channel_id_; }
544 void SetRenderer(VideoRenderer* renderer) {
545 render_adapter_.SetRenderer(renderer);
546 }
547 WebRtcRenderAdapter* render_adapter() { return &render_adapter_; }
548 WebRtcDecoderObserver* decoder_observer() { return &decoder_observer_; }
549 void RegisterDecoder(int pl_type, webrtc::VideoDecoder* decoder) {
550 ASSERT(!IsDecoderRegistered(pl_type));
551 registered_decoders_[pl_type] = decoder;
552 }
553 bool IsDecoderRegistered(int pl_type) {
554 return registered_decoders_.count(pl_type) != 0;
555 }
556 const DecoderMap& registered_decoders() {
557 return registered_decoders_;
558 }
559 void ClearRegisteredDecoders() {
560 registered_decoders_.clear();
561 }
562
563 private:
564 int channel_id_; // Webrtc video channel number.
565 // Renderer for this channel.
566 WebRtcRenderAdapter render_adapter_;
567 WebRtcDecoderObserver decoder_observer_;
568 DecoderMap registered_decoders_;
569};
570
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000571class WebRtcOveruseObserver : public webrtc::CpuOveruseObserver {
572 public:
573 explicit WebRtcOveruseObserver(CoordinatedVideoAdapter* video_adapter)
574 : video_adapter_(video_adapter),
575 enabled_(false) {
576 }
577
578 // TODO(mflodman): Consider sending resolution as part of event, to let
579 // adapter know what resolution the request is based on. Helps eliminate stale
580 // data, race conditions.
581 virtual void OveruseDetected() OVERRIDE {
582 talk_base::CritScope cs(&crit_);
583 if (!enabled_) {
584 return;
585 }
586
587 video_adapter_->OnCpuResolutionRequest(CoordinatedVideoAdapter::DOWNGRADE);
588 }
589
590 virtual void NormalUsage() OVERRIDE {
591 talk_base::CritScope cs(&crit_);
592 if (!enabled_) {
593 return;
594 }
595
596 video_adapter_->OnCpuResolutionRequest(CoordinatedVideoAdapter::UPGRADE);
597 }
598
599 void Enable(bool enable) {
600 talk_base::CritScope cs(&crit_);
601 enabled_ = enable;
602 }
603
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000604 bool enabled() const { return enabled_; }
605
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000606 private:
607 CoordinatedVideoAdapter* video_adapter_;
608 bool enabled_;
609 talk_base::CriticalSection crit_;
610};
611
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000612
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000613class WebRtcVideoChannelSendInfo : public sigslot::has_slots<> {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000614 public:
615 typedef std::map<int, webrtc::VideoEncoder*> EncoderMap; // key: payload type
616 WebRtcVideoChannelSendInfo(int channel_id, int capture_id,
617 webrtc::ViEExternalCapture* external_capture,
618 talk_base::CpuMonitor* cpu_monitor)
619 : channel_id_(channel_id),
620 capture_id_(capture_id),
621 sending_(false),
622 muted_(false),
623 video_capturer_(NULL),
624 encoder_observer_(channel_id),
625 external_capture_(external_capture),
626 capturer_updated_(false),
627 interval_(0),
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000628 cpu_monitor_(cpu_monitor),
629 overuse_observer_enabled_(false) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000630 }
631
632 int channel_id() const { return channel_id_; }
633 int capture_id() const { return capture_id_; }
634 void set_sending(bool sending) { sending_ = sending; }
635 bool sending() const { return sending_; }
636 void set_muted(bool on) {
637 // TODO(asapersson): add support.
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000638 // video_adapter_.SetBlackOutput(on);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000639 muted_ = on;
640 }
641 bool muted() {return muted_; }
642
643 WebRtcEncoderObserver* encoder_observer() { return &encoder_observer_; }
644 webrtc::ViEExternalCapture* external_capture() { return external_capture_; }
645 const VideoFormat& video_format() const {
646 return video_format_;
647 }
648 void set_video_format(const VideoFormat& video_format) {
649 video_format_ = video_format;
650 if (video_format_ != cricket::VideoFormat()) {
651 interval_ = video_format_.interval;
652 }
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000653 CoordinatedVideoAdapter* adapter = video_adapter();
654 if (adapter) {
655 adapter->OnOutputFormatRequest(video_format_);
656 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000657 }
658 void set_interval(int64 interval) {
659 if (video_format() == cricket::VideoFormat()) {
660 interval_ = interval;
661 }
662 }
663 int64 interval() { return interval_; }
664
xians@webrtc.orgef221512014-02-21 10:31:29 +0000665 int CurrentAdaptReason() const {
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000666 const CoordinatedVideoAdapter* adapter = video_adapter();
667 if (!adapter) {
668 return CoordinatedVideoAdapter::ADAPTREASON_NONE;
669 }
670 return video_adapter()->adapt_reason();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000671 }
672
673 StreamParams* stream_params() { return stream_params_.get(); }
674 void set_stream_params(const StreamParams& sp) {
675 stream_params_.reset(new StreamParams(sp));
676 }
677 void ClearStreamParams() { stream_params_.reset(); }
678 bool has_ssrc(uint32 local_ssrc) const {
679 return !stream_params_ ? false :
680 stream_params_->has_ssrc(local_ssrc);
681 }
682 WebRtcLocalStreamInfo* local_stream_info() {
683 return &local_stream_info_;
684 }
685 VideoCapturer* video_capturer() {
686 return video_capturer_;
687 }
henrike@webrtc.orgc7bec842014-03-12 19:53:43 +0000688 void set_video_capturer(VideoCapturer* video_capturer,
689 ViEWrapper* vie_wrapper) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000690 if (video_capturer == video_capturer_) {
691 return;
692 }
xians@webrtc.orgef221512014-02-21 10:31:29 +0000693
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000694 CoordinatedVideoAdapter* old_video_adapter = video_adapter();
695 if (old_video_adapter) {
696 // Disconnect signals from old video adapter.
697 SignalCpuAdaptationUnable.disconnect(old_video_adapter);
698 if (cpu_monitor_) {
699 cpu_monitor_->SignalUpdate.disconnect(old_video_adapter);
xians@webrtc.orgef221512014-02-21 10:31:29 +0000700 }
henrike@webrtc.org26438052014-02-20 22:32:53 +0000701 }
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000702
703 capturer_updated_ = true;
704 video_capturer_ = video_capturer;
705
henrike@webrtc.orgc7bec842014-03-12 19:53:43 +0000706 vie_wrapper->base()->RegisterCpuOveruseObserver(channel_id_, NULL);
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000707 if (!video_capturer) {
708 overuse_observer_.reset();
709 return;
710 }
711
712 CoordinatedVideoAdapter* adapter = video_adapter();
713 ASSERT(adapter && "Video adapter should not be null here.");
714
715 UpdateAdapterCpuOptions();
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000716
717 overuse_observer_.reset(new WebRtcOveruseObserver(adapter));
henrike@webrtc.orgc7bec842014-03-12 19:53:43 +0000718 vie_wrapper->base()->RegisterCpuOveruseObserver(channel_id_,
719 overuse_observer_.get());
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000720 // (Dis)connect the video adapter from the cpu monitor as appropriate.
721 SetCpuOveruseDetection(overuse_observer_enabled_);
722
723 SignalCpuAdaptationUnable.repeat(adapter->SignalCpuAdaptationUnable);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000724 }
725
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000726 CoordinatedVideoAdapter* video_adapter() {
727 if (!video_capturer_) {
728 return NULL;
729 }
730 return video_capturer_->video_adapter();
731 }
732 const CoordinatedVideoAdapter* video_adapter() const {
733 if (!video_capturer_) {
734 return NULL;
735 }
736 return video_capturer_->video_adapter();
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000737 }
738
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000739 void ApplyCpuOptions(const VideoOptions& video_options) {
740 // Use video_options_.SetAll() instead of assignment so that unset value in
741 // video_options will not overwrite the previous option value.
742 video_options_.SetAll(video_options);
743 UpdateAdapterCpuOptions();
744 }
745
746 void UpdateAdapterCpuOptions() {
747 if (!video_capturer_) {
748 return;
749 }
750
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000751 bool cpu_adapt, cpu_smoothing, adapt_third;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000752 float low, med, high;
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000753
754 // TODO(thorcarpenter): Have VideoAdapter be responsible for setting
755 // all these video options.
756 CoordinatedVideoAdapter* video_adapter = video_capturer_->video_adapter();
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000757 if (video_options_.adapt_input_to_cpu_usage.Get(&cpu_adapt) ||
758 overuse_observer_enabled_) {
759 video_adapter->set_cpu_adaptation(cpu_adapt || overuse_observer_enabled_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000760 }
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000761 if (video_options_.adapt_cpu_with_smoothing.Get(&cpu_smoothing)) {
762 video_adapter->set_cpu_smoothing(cpu_smoothing);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000763 }
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000764 if (video_options_.process_adaptation_threshhold.Get(&med)) {
765 video_adapter->set_process_threshold(med);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000766 }
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000767 if (video_options_.system_low_adaptation_threshhold.Get(&low)) {
768 video_adapter->set_low_system_threshold(low);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000769 }
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000770 if (video_options_.system_high_adaptation_threshhold.Get(&high)) {
771 video_adapter->set_high_system_threshold(high);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000772 }
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000773 if (video_options_.video_adapt_third.Get(&adapt_third)) {
774 video_adapter->set_scale_third(adapt_third);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000775 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000776 }
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000777
778 void SetCpuOveruseDetection(bool enable) {
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000779 overuse_observer_enabled_ = enable;
780
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000781 if (overuse_observer_) {
782 overuse_observer_->Enable(enable);
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000783 }
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000784
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000785 // The video adapter is signaled by overuse detection if enabled; otherwise
786 // it will be signaled by cpu monitor.
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000787 CoordinatedVideoAdapter* adapter = video_adapter();
788 if (adapter) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000789 bool cpu_adapt = false;
790 video_options_.adapt_input_to_cpu_usage.Get(&cpu_adapt);
791 adapter->set_cpu_adaptation(
792 adapter->cpu_adaptation() || cpu_adapt || enable);
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000793 if (cpu_monitor_) {
794 if (enable) {
795 cpu_monitor_->SignalUpdate.disconnect(adapter);
796 } else {
797 cpu_monitor_->SignalUpdate.connect(
798 adapter, &CoordinatedVideoAdapter::OnCpuLoadUpdated);
799 }
800 }
801 }
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000802 }
803
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000804 void ProcessFrame(const VideoFrame& original_frame, bool mute,
805 VideoFrame** processed_frame) {
806 if (!mute) {
807 *processed_frame = original_frame.Copy();
808 } else {
809 WebRtcVideoFrame* black_frame = new WebRtcVideoFrame();
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000810 black_frame->InitToBlack(static_cast<int>(original_frame.GetWidth()),
811 static_cast<int>(original_frame.GetHeight()),
812 1, 1,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000813 original_frame.GetElapsedTime(),
814 original_frame.GetTimeStamp());
815 *processed_frame = black_frame;
816 }
817 local_stream_info_.UpdateFrame(*processed_frame);
818 }
819 void RegisterEncoder(int pl_type, webrtc::VideoEncoder* encoder) {
820 ASSERT(!IsEncoderRegistered(pl_type));
821 registered_encoders_[pl_type] = encoder;
822 }
823 bool IsEncoderRegistered(int pl_type) {
824 return registered_encoders_.count(pl_type) != 0;
825 }
826 const EncoderMap& registered_encoders() {
827 return registered_encoders_;
828 }
829 void ClearRegisteredEncoders() {
830 registered_encoders_.clear();
831 }
832
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000833 sigslot::repeater0<> SignalCpuAdaptationUnable;
834
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000835 private:
836 int channel_id_;
837 int capture_id_;
838 bool sending_;
839 bool muted_;
840 VideoCapturer* video_capturer_;
841 WebRtcEncoderObserver encoder_observer_;
842 webrtc::ViEExternalCapture* external_capture_;
843 EncoderMap registered_encoders_;
844
845 VideoFormat video_format_;
846
847 talk_base::scoped_ptr<StreamParams> stream_params_;
848
849 WebRtcLocalStreamInfo local_stream_info_;
850
851 bool capturer_updated_;
852
853 int64 interval_;
854
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000855 talk_base::CpuMonitor* cpu_monitor_;
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000856 talk_base::scoped_ptr<WebRtcOveruseObserver> overuse_observer_;
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000857 bool overuse_observer_enabled_;
858
859 VideoOptions video_options_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000860};
861
862const WebRtcVideoEngine::VideoCodecPref
863 WebRtcVideoEngine::kVideoCodecPrefs[] = {
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000864 {kVp8PayloadName, 100, -1, 0},
865 {kRedPayloadName, 116, -1, 1},
866 {kFecPayloadName, 117, -1, 2},
867 {kRtxCodecName, 96, 100, 3},
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000868};
869
870// The formats are sorted by the descending order of width. We use the order to
871// find the next format for CPU and bandwidth adaptation.
872const VideoFormatPod WebRtcVideoEngine::kVideoFormats[] = {
873 {1280, 800, FPS_TO_INTERVAL(30), FOURCC_ANY},
874 {1280, 720, FPS_TO_INTERVAL(30), FOURCC_ANY},
875 {960, 600, FPS_TO_INTERVAL(30), FOURCC_ANY},
876 {960, 540, FPS_TO_INTERVAL(30), FOURCC_ANY},
877 {640, 400, FPS_TO_INTERVAL(30), FOURCC_ANY},
878 {640, 360, FPS_TO_INTERVAL(30), FOURCC_ANY},
879 {640, 480, FPS_TO_INTERVAL(30), FOURCC_ANY},
880 {480, 300, FPS_TO_INTERVAL(30), FOURCC_ANY},
881 {480, 270, FPS_TO_INTERVAL(30), FOURCC_ANY},
882 {480, 360, FPS_TO_INTERVAL(30), FOURCC_ANY},
883 {320, 200, FPS_TO_INTERVAL(30), FOURCC_ANY},
884 {320, 180, FPS_TO_INTERVAL(30), FOURCC_ANY},
885 {320, 240, FPS_TO_INTERVAL(30), FOURCC_ANY},
886 {240, 150, FPS_TO_INTERVAL(30), FOURCC_ANY},
887 {240, 135, FPS_TO_INTERVAL(30), FOURCC_ANY},
888 {240, 180, FPS_TO_INTERVAL(30), FOURCC_ANY},
889 {160, 100, FPS_TO_INTERVAL(30), FOURCC_ANY},
890 {160, 90, FPS_TO_INTERVAL(30), FOURCC_ANY},
891 {160, 120, FPS_TO_INTERVAL(30), FOURCC_ANY},
892};
893
894const VideoFormatPod WebRtcVideoEngine::kDefaultVideoFormat =
895 {640, 400, FPS_TO_INTERVAL(30), FOURCC_ANY};
896
897static void UpdateVideoCodec(const cricket::VideoFormat& video_format,
898 webrtc::VideoCodec* target_codec) {
899 if ((target_codec == NULL) || (video_format == cricket::VideoFormat())) {
900 return;
901 }
902 target_codec->width = video_format.width;
903 target_codec->height = video_format.height;
904 target_codec->maxFramerate = cricket::VideoFormat::IntervalToFps(
905 video_format.interval);
906}
907
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000908static bool GetCpuOveruseOptions(const VideoOptions& options,
909 webrtc::CpuOveruseOptions* overuse_options) {
910 int underuse_threshold = 0;
911 int overuse_threshold = 0;
912 if (!options.cpu_underuse_threshold.Get(&underuse_threshold) ||
913 !options.cpu_overuse_threshold.Get(&overuse_threshold)) {
914 return false;
915 }
916 if (underuse_threshold <= 0 || overuse_threshold <= 0) {
917 return false;
918 }
919 // Valid thresholds.
920 bool encode_usage =
921 options.cpu_overuse_encode_usage.GetWithDefaultIfUnset(false);
922 overuse_options->enable_capture_jitter_method = !encode_usage;
923 overuse_options->enable_encode_usage_method = encode_usage;
924 if (encode_usage) {
925 // Use method based on encode usage.
926 overuse_options->low_encode_usage_threshold_percent = underuse_threshold;
927 overuse_options->high_encode_usage_threshold_percent = overuse_threshold;
928 } else {
929 // Use default method based on capture jitter.
930 overuse_options->low_capture_jitter_threshold_ms =
931 static_cast<float>(underuse_threshold);
932 overuse_options->high_capture_jitter_threshold_ms =
933 static_cast<float>(overuse_threshold);
934 }
935 return true;
936}
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000937
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000938WebRtcVideoEngine::WebRtcVideoEngine() {
939 Construct(new ViEWrapper(), new ViETraceWrapper(), NULL,
940 new talk_base::CpuMonitor(NULL));
941}
942
943WebRtcVideoEngine::WebRtcVideoEngine(WebRtcVoiceEngine* voice_engine,
944 ViEWrapper* vie_wrapper,
945 talk_base::CpuMonitor* cpu_monitor) {
946 Construct(vie_wrapper, new ViETraceWrapper(), voice_engine, cpu_monitor);
947}
948
949WebRtcVideoEngine::WebRtcVideoEngine(WebRtcVoiceEngine* voice_engine,
950 ViEWrapper* vie_wrapper,
951 ViETraceWrapper* tracing,
952 talk_base::CpuMonitor* cpu_monitor) {
953 Construct(vie_wrapper, tracing, voice_engine, cpu_monitor);
954}
955
956void WebRtcVideoEngine::Construct(ViEWrapper* vie_wrapper,
957 ViETraceWrapper* tracing,
958 WebRtcVoiceEngine* voice_engine,
959 talk_base::CpuMonitor* cpu_monitor) {
960 LOG(LS_INFO) << "WebRtcVideoEngine::WebRtcVideoEngine";
961 worker_thread_ = NULL;
962 vie_wrapper_.reset(vie_wrapper);
963 vie_wrapper_base_initialized_ = false;
964 tracing_.reset(tracing);
965 voice_engine_ = voice_engine;
966 initialized_ = false;
967 SetTraceFilter(SeverityToFilter(kDefaultLogSeverity));
968 render_module_.reset(new WebRtcPassthroughRender());
969 local_renderer_w_ = local_renderer_h_ = 0;
970 local_renderer_ = NULL;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000971 capture_started_ = false;
972 decoder_factory_ = NULL;
973 encoder_factory_ = NULL;
974 cpu_monitor_.reset(cpu_monitor);
975
976 SetTraceOptions("");
977 if (tracing_->SetTraceCallback(this) != 0) {
978 LOG_RTCERR1(SetTraceCallback, this);
979 }
980
981 // Set default quality levels for our supported codecs. We override them here
982 // if we know your cpu performance is low, and they can be updated explicitly
983 // by calling SetDefaultCodec. For example by a flute preference setting, or
984 // by the server with a jec in response to our reported system info.
985 VideoCodec max_codec(kVideoCodecPrefs[0].payload_type,
986 kVideoCodecPrefs[0].name,
987 kDefaultVideoFormat.width,
988 kDefaultVideoFormat.height,
989 VideoFormat::IntervalToFps(kDefaultVideoFormat.interval),
990 0);
991 if (!SetDefaultCodec(max_codec)) {
992 LOG(LS_ERROR) << "Failed to initialize list of supported codec types";
993 }
994
995
996 // Load our RTP Header extensions.
997 rtp_header_extensions_.push_back(
998 RtpHeaderExtension(kRtpTimestampOffsetHeaderExtension,
henrike@webrtc.org79047f92014-03-06 23:46:59 +0000999 kRtpTimestampOffsetHeaderExtensionDefaultId));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001000 rtp_header_extensions_.push_back(
henrike@webrtc.org79047f92014-03-06 23:46:59 +00001001 RtpHeaderExtension(kRtpAbsoluteSenderTimeHeaderExtension,
1002 kRtpAbsoluteSenderTimeHeaderExtensionDefaultId));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001003}
1004
1005WebRtcVideoEngine::~WebRtcVideoEngine() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001006 LOG(LS_INFO) << "WebRtcVideoEngine::~WebRtcVideoEngine";
1007 if (initialized_) {
1008 Terminate();
1009 }
1010 if (encoder_factory_) {
1011 encoder_factory_->RemoveObserver(this);
1012 }
1013 tracing_->SetTraceCallback(NULL);
1014 // Test to see if the media processor was deregistered properly.
1015 ASSERT(SignalMediaFrame.is_empty());
1016}
1017
1018bool WebRtcVideoEngine::Init(talk_base::Thread* worker_thread) {
1019 LOG(LS_INFO) << "WebRtcVideoEngine::Init";
1020 worker_thread_ = worker_thread;
1021 ASSERT(worker_thread_ != NULL);
1022
1023 cpu_monitor_->set_thread(worker_thread_);
1024 if (!cpu_monitor_->Start(kCpuMonitorPeriodMs)) {
1025 LOG(LS_ERROR) << "Failed to start CPU monitor.";
1026 cpu_monitor_.reset();
1027 }
1028
1029 bool result = InitVideoEngine();
1030 if (result) {
1031 LOG(LS_INFO) << "VideoEngine Init done";
1032 } else {
1033 LOG(LS_ERROR) << "VideoEngine Init failed, releasing";
1034 Terminate();
1035 }
1036 return result;
1037}
1038
1039bool WebRtcVideoEngine::InitVideoEngine() {
1040 LOG(LS_INFO) << "WebRtcVideoEngine::InitVideoEngine";
1041
1042 // Init WebRTC VideoEngine.
1043 if (!vie_wrapper_base_initialized_) {
1044 if (vie_wrapper_->base()->Init() != 0) {
1045 LOG_RTCERR0(Init);
1046 return false;
1047 }
1048 vie_wrapper_base_initialized_ = true;
1049 }
1050
1051 // Log the VoiceEngine version info.
1052 char buffer[1024] = "";
1053 if (vie_wrapper_->base()->GetVersion(buffer) != 0) {
1054 LOG_RTCERR0(GetVersion);
1055 return false;
1056 }
1057
1058 LOG(LS_INFO) << "WebRtc VideoEngine Version:";
1059 LogMultiline(talk_base::LS_INFO, buffer);
1060
1061 // Hook up to VoiceEngine for sync purposes, if supplied.
1062 if (!voice_engine_) {
1063 LOG(LS_WARNING) << "NULL voice engine";
1064 } else if ((vie_wrapper_->base()->SetVoiceEngine(
1065 voice_engine_->voe()->engine())) != 0) {
1066 LOG_RTCERR0(SetVoiceEngine);
1067 return false;
1068 }
1069
1070 // Register our custom render module.
1071 if (vie_wrapper_->render()->RegisterVideoRenderModule(
1072 *render_module_.get()) != 0) {
1073 LOG_RTCERR0(RegisterVideoRenderModule);
1074 return false;
1075 }
1076
1077 initialized_ = true;
1078 return true;
1079}
1080
1081void WebRtcVideoEngine::Terminate() {
1082 LOG(LS_INFO) << "WebRtcVideoEngine::Terminate";
1083 initialized_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001084
1085 if (vie_wrapper_->render()->DeRegisterVideoRenderModule(
1086 *render_module_.get()) != 0) {
1087 LOG_RTCERR0(DeRegisterVideoRenderModule);
1088 }
1089
1090 if (vie_wrapper_->base()->SetVoiceEngine(NULL) != 0) {
1091 LOG_RTCERR0(SetVoiceEngine);
1092 }
1093
1094 cpu_monitor_->Stop();
1095}
1096
1097int WebRtcVideoEngine::GetCapabilities() {
1098 return VIDEO_RECV | VIDEO_SEND;
1099}
1100
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00001101bool WebRtcVideoEngine::SetOptions(const VideoOptions &options) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001102 return true;
1103}
1104
1105bool WebRtcVideoEngine::SetDefaultEncoderConfig(
1106 const VideoEncoderConfig& config) {
1107 return SetDefaultCodec(config.max_codec);
1108}
1109
wu@webrtc.org78187522013-10-07 23:32:02 +00001110VideoEncoderConfig WebRtcVideoEngine::GetDefaultEncoderConfig() const {
1111 ASSERT(!video_codecs_.empty());
1112 VideoCodec max_codec(kVideoCodecPrefs[0].payload_type,
1113 kVideoCodecPrefs[0].name,
1114 video_codecs_[0].width,
1115 video_codecs_[0].height,
1116 video_codecs_[0].framerate,
1117 0);
1118 return VideoEncoderConfig(max_codec);
1119}
1120
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001121// SetDefaultCodec may be called while the capturer is running. For example, a
1122// test call is started in a page with QVGA default codec, and then a real call
1123// is started in another page with VGA default codec. This is the corner case
1124// and happens only when a session is started. We ignore this case currently.
1125bool WebRtcVideoEngine::SetDefaultCodec(const VideoCodec& codec) {
1126 if (!RebuildCodecList(codec)) {
1127 LOG(LS_WARNING) << "Failed to RebuildCodecList";
1128 return false;
1129 }
1130
wu@webrtc.org78187522013-10-07 23:32:02 +00001131 ASSERT(!video_codecs_.empty());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001132 default_codec_format_ = VideoFormat(
1133 video_codecs_[0].width,
1134 video_codecs_[0].height,
1135 VideoFormat::FpsToInterval(video_codecs_[0].framerate),
1136 FOURCC_ANY);
1137 return true;
1138}
1139
1140WebRtcVideoMediaChannel* WebRtcVideoEngine::CreateChannel(
1141 VoiceMediaChannel* voice_channel) {
1142 WebRtcVideoMediaChannel* channel =
1143 new WebRtcVideoMediaChannel(this, voice_channel);
1144 if (!channel->Init()) {
1145 delete channel;
1146 channel = NULL;
1147 }
1148 return channel;
1149}
1150
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001151bool WebRtcVideoEngine::SetLocalRenderer(VideoRenderer* renderer) {
1152 local_renderer_w_ = local_renderer_h_ = 0;
1153 local_renderer_ = renderer;
1154 return true;
1155}
1156
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001157const std::vector<VideoCodec>& WebRtcVideoEngine::codecs() const {
1158 return video_codecs_;
1159}
1160
1161const std::vector<RtpHeaderExtension>&
1162WebRtcVideoEngine::rtp_header_extensions() const {
1163 return rtp_header_extensions_;
1164}
1165
1166void WebRtcVideoEngine::SetLogging(int min_sev, const char* filter) {
1167 // if min_sev == -1, we keep the current log level.
1168 if (min_sev >= 0) {
1169 SetTraceFilter(SeverityToFilter(min_sev));
1170 }
1171 SetTraceOptions(filter);
1172}
1173
1174int WebRtcVideoEngine::GetLastEngineError() {
1175 return vie_wrapper_->error();
1176}
1177
1178// Checks to see whether we comprehend and could receive a particular codec
1179bool WebRtcVideoEngine::FindCodec(const VideoCodec& in) {
1180 for (int i = 0; i < ARRAY_SIZE(kVideoFormats); ++i) {
1181 const VideoFormat fmt(kVideoFormats[i]);
1182 if ((in.width == 0 && in.height == 0) ||
1183 (fmt.width == in.width && fmt.height == in.height)) {
1184 if (encoder_factory_) {
1185 const std::vector<WebRtcVideoEncoderFactory::VideoCodec>& codecs =
1186 encoder_factory_->codecs();
1187 for (size_t j = 0; j < codecs.size(); ++j) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001188 VideoCodec codec(GetExternalVideoPayloadType(static_cast<int>(j)),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001189 codecs[j].name, 0, 0, 0, 0);
1190 if (codec.Matches(in))
1191 return true;
1192 }
1193 }
1194 for (size_t j = 0; j < ARRAY_SIZE(kVideoCodecPrefs); ++j) {
1195 VideoCodec codec(kVideoCodecPrefs[j].payload_type,
1196 kVideoCodecPrefs[j].name, 0, 0, 0, 0);
1197 if (codec.Matches(in)) {
1198 return true;
1199 }
1200 }
1201 }
1202 }
1203 return false;
1204}
1205
1206// Given the requested codec, returns true if we can send that codec type and
1207// updates out with the best quality we could send for that codec. If current is
1208// not empty, we constrain out so that its aspect ratio matches current's.
1209bool WebRtcVideoEngine::CanSendCodec(const VideoCodec& requested,
1210 const VideoCodec& current,
1211 VideoCodec* out) {
1212 if (!out) {
1213 return false;
1214 }
1215
1216 std::vector<VideoCodec>::const_iterator local_max;
1217 for (local_max = video_codecs_.begin();
1218 local_max < video_codecs_.end();
1219 ++local_max) {
1220 // First match codecs by payload type
1221 if (!requested.Matches(*local_max)) {
1222 continue;
1223 }
1224
1225 out->id = requested.id;
1226 out->name = requested.name;
1227 out->preference = requested.preference;
1228 out->params = requested.params;
1229 out->framerate = talk_base::_min(requested.framerate, local_max->framerate);
1230 out->width = 0;
1231 out->height = 0;
1232 out->params = requested.params;
1233 out->feedback_params = requested.feedback_params;
1234
1235 if (0 == requested.width && 0 == requested.height) {
1236 // Special case with resolution 0. The channel should not send frames.
1237 return true;
1238 } else if (0 == requested.width || 0 == requested.height) {
1239 // 0xn and nx0 are invalid resolutions.
1240 return false;
1241 }
1242
1243 // Pick the best quality that is within their and our bounds and has the
1244 // correct aspect ratio.
1245 for (int j = 0; j < ARRAY_SIZE(kVideoFormats); ++j) {
1246 const VideoFormat format(kVideoFormats[j]);
1247
1248 // Skip any format that is larger than the local or remote maximums, or
1249 // smaller than the current best match
1250 if (format.width > requested.width || format.height > requested.height ||
1251 format.width > local_max->width ||
1252 (format.width < out->width && format.height < out->height)) {
1253 continue;
1254 }
1255
1256 bool better = false;
1257
1258 // Check any further constraints on this prospective format
1259 if (!out->width || !out->height) {
1260 // If we don't have any matches yet, this is the best so far.
1261 better = true;
1262 } else if (current.width && current.height) {
1263 // current is set so format must match its ratio exactly.
1264 better =
1265 (format.width * current.height == format.height * current.width);
1266 } else {
1267 // Prefer closer aspect ratios i.e
1268 // format.aspect - requested.aspect < out.aspect - requested.aspect
1269 better = abs(format.width * requested.height * out->height -
1270 requested.width * format.height * out->height) <
1271 abs(out->width * format.height * requested.height -
1272 requested.width * format.height * out->height);
1273 }
1274
1275 if (better) {
1276 out->width = format.width;
1277 out->height = format.height;
1278 }
1279 }
1280 if (out->width > 0) {
1281 return true;
1282 }
1283 }
1284 return false;
1285}
1286
1287static void ConvertToCricketVideoCodec(
1288 const webrtc::VideoCodec& in_codec, VideoCodec* out_codec) {
1289 out_codec->id = in_codec.plType;
1290 out_codec->name = in_codec.plName;
1291 out_codec->width = in_codec.width;
1292 out_codec->height = in_codec.height;
1293 out_codec->framerate = in_codec.maxFramerate;
buildbot@webrtc.orgd1ae89f2014-05-08 19:19:26 +00001294 if (BitrateIsSet(in_codec.minBitrate)) {
1295 out_codec->SetParam(kCodecParamMinBitrate, in_codec.minBitrate);
1296 }
1297 if (BitrateIsSet(in_codec.maxBitrate)) {
1298 out_codec->SetParam(kCodecParamMaxBitrate, in_codec.maxBitrate);
1299 }
1300 if (BitrateIsSet(in_codec.startBitrate)) {
1301 out_codec->SetParam(kCodecParamStartBitrate, in_codec.startBitrate);
1302 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001303 if (in_codec.qpMax) {
1304 out_codec->SetParam(kCodecParamMaxQuantization, in_codec.qpMax);
1305 }
1306}
1307
1308bool WebRtcVideoEngine::ConvertFromCricketVideoCodec(
1309 const VideoCodec& in_codec, webrtc::VideoCodec* out_codec) {
1310 bool found = false;
1311 int ncodecs = vie_wrapper_->codec()->NumberOfCodecs();
1312 for (int i = 0; i < ncodecs; ++i) {
1313 if (vie_wrapper_->codec()->GetCodec(i, *out_codec) == 0 &&
1314 _stricmp(in_codec.name.c_str(), out_codec->plName) == 0) {
1315 found = true;
1316 break;
1317 }
1318 }
1319
1320 // If not found, check if this is supported by external encoder factory.
1321 if (!found && encoder_factory_) {
1322 const std::vector<WebRtcVideoEncoderFactory::VideoCodec>& codecs =
1323 encoder_factory_->codecs();
1324 for (size_t i = 0; i < codecs.size(); ++i) {
1325 if (_stricmp(in_codec.name.c_str(), codecs[i].name.c_str()) == 0) {
1326 out_codec->codecType = codecs[i].type;
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001327 out_codec->plType = GetExternalVideoPayloadType(static_cast<int>(i));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001328 talk_base::strcpyn(out_codec->plName, sizeof(out_codec->plName),
1329 codecs[i].name.c_str(), codecs[i].name.length());
1330 found = true;
1331 break;
1332 }
1333 }
1334 }
1335
buildbot@webrtc.orgdd4742a2014-05-07 14:50:35 +00001336 // Is this an RTX codec? Handled separately here since webrtc doesn't handle
1337 // them as webrtc::VideoCodec internally.
1338 if (!found && _stricmp(in_codec.name.c_str(), kRtxCodecName) == 0) {
1339 talk_base::strcpyn(out_codec->plName, sizeof(out_codec->plName),
1340 in_codec.name.c_str(), in_codec.name.length());
1341 out_codec->plType = in_codec.id;
1342 found = true;
1343 }
1344
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001345 if (!found) {
1346 LOG(LS_ERROR) << "invalid codec type";
1347 return false;
1348 }
1349
1350 if (in_codec.id != 0)
1351 out_codec->plType = in_codec.id;
1352
1353 if (in_codec.width != 0)
1354 out_codec->width = in_codec.width;
1355
1356 if (in_codec.height != 0)
1357 out_codec->height = in_codec.height;
1358
1359 if (in_codec.framerate != 0)
1360 out_codec->maxFramerate = in_codec.framerate;
1361
1362 // Convert bitrate parameters.
buildbot@webrtc.orgd1ae89f2014-05-08 19:19:26 +00001363 int max_bitrate = -1;
1364 int min_bitrate = -1;
1365 int start_bitrate = -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001366
1367 in_codec.GetParam(kCodecParamMinBitrate, &min_bitrate);
1368 in_codec.GetParam(kCodecParamMaxBitrate, &max_bitrate);
buildbot@webrtc.orged97bb02014-05-07 11:15:20 +00001369 in_codec.GetParam(kCodecParamStartBitrate, &start_bitrate);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001370
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001371
1372 out_codec->minBitrate = min_bitrate;
1373 out_codec->startBitrate = start_bitrate;
1374 out_codec->maxBitrate = max_bitrate;
1375
1376 // Convert general codec parameters.
1377 int max_quantization = 0;
1378 if (in_codec.GetParam(kCodecParamMaxQuantization, &max_quantization)) {
1379 if (max_quantization < 0) {
1380 return false;
1381 }
1382 out_codec->qpMax = max_quantization;
1383 }
1384 return true;
1385}
1386
1387void WebRtcVideoEngine::RegisterChannel(WebRtcVideoMediaChannel *channel) {
1388 talk_base::CritScope cs(&channels_crit_);
1389 channels_.push_back(channel);
1390}
1391
1392void WebRtcVideoEngine::UnregisterChannel(WebRtcVideoMediaChannel *channel) {
1393 talk_base::CritScope cs(&channels_crit_);
1394 channels_.erase(std::remove(channels_.begin(), channels_.end(), channel),
1395 channels_.end());
1396}
1397
1398bool WebRtcVideoEngine::SetVoiceEngine(WebRtcVoiceEngine* voice_engine) {
1399 if (initialized_) {
1400 LOG(LS_WARNING) << "SetVoiceEngine can not be called after Init";
1401 return false;
1402 }
1403 voice_engine_ = voice_engine;
1404 return true;
1405}
1406
1407bool WebRtcVideoEngine::EnableTimedRender() {
1408 if (initialized_) {
1409 LOG(LS_WARNING) << "EnableTimedRender can not be called after Init";
1410 return false;
1411 }
1412 render_module_.reset(webrtc::VideoRender::CreateVideoRender(0, NULL,
1413 false, webrtc::kRenderExternal));
1414 return true;
1415}
1416
1417void WebRtcVideoEngine::SetTraceFilter(int filter) {
1418 tracing_->SetTraceFilter(filter);
1419}
1420
1421// See https://sites.google.com/a/google.com/wavelet/
1422// Home/Magic-Flute--RTC-Engine-/Magic-Flute-Command-Line-Parameters
1423// for all supported command line setttings.
1424void WebRtcVideoEngine::SetTraceOptions(const std::string& options) {
1425 // Set WebRTC trace file.
1426 std::vector<std::string> opts;
1427 talk_base::tokenize(options, ' ', '"', '"', &opts);
1428 std::vector<std::string>::iterator tracefile =
1429 std::find(opts.begin(), opts.end(), "tracefile");
1430 if (tracefile != opts.end() && ++tracefile != opts.end()) {
1431 // Write WebRTC debug output (at same loglevel) to file
1432 if (tracing_->SetTraceFile(tracefile->c_str()) == -1) {
1433 LOG_RTCERR1(SetTraceFile, *tracefile);
1434 }
1435 }
1436}
1437
1438static void AddDefaultFeedbackParams(VideoCodec* codec) {
1439 const FeedbackParam kFir(kRtcpFbParamCcm, kRtcpFbCcmParamFir);
1440 codec->AddFeedbackParam(kFir);
1441 const FeedbackParam kNack(kRtcpFbParamNack, kParamValueEmpty);
1442 codec->AddFeedbackParam(kNack);
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001443 const FeedbackParam kPli(kRtcpFbParamNack, kRtcpFbNackParamPli);
1444 codec->AddFeedbackParam(kPli);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001445 const FeedbackParam kRemb(kRtcpFbParamRemb, kParamValueEmpty);
1446 codec->AddFeedbackParam(kRemb);
1447}
1448
1449// Rebuilds the codec list to be only those that are less intensive
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00001450// than the specified codec. Prefers internal codec over external with
1451// higher preference field.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001452bool WebRtcVideoEngine::RebuildCodecList(const VideoCodec& in_codec) {
1453 if (!FindCodec(in_codec))
1454 return false;
1455
1456 video_codecs_.clear();
1457
1458 bool found = false;
mallinath@webrtc.org1112c302013-09-23 20:34:45 +00001459 std::set<std::string> internal_codec_names;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001460 for (size_t i = 0; i < ARRAY_SIZE(kVideoCodecPrefs); ++i) {
1461 const VideoCodecPref& pref(kVideoCodecPrefs[i]);
1462 if (!found)
1463 found = (in_codec.name == pref.name);
mallinath@webrtc.org1112c302013-09-23 20:34:45 +00001464 if (found) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001465 VideoCodec codec(pref.payload_type, pref.name,
1466 in_codec.width, in_codec.height, in_codec.framerate,
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001467 static_cast<int>(ARRAY_SIZE(kVideoCodecPrefs) - i));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001468 if (_stricmp(kVp8PayloadName, codec.name.c_str()) == 0) {
1469 AddDefaultFeedbackParams(&codec);
1470 }
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00001471 if (pref.associated_payload_type != -1) {
1472 codec.SetParam(kCodecParamAssociatedPayloadType,
1473 pref.associated_payload_type);
1474 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001475 video_codecs_.push_back(codec);
mallinath@webrtc.org1112c302013-09-23 20:34:45 +00001476 internal_codec_names.insert(codec.name);
1477 }
1478 }
1479 if (encoder_factory_) {
1480 const std::vector<WebRtcVideoEncoderFactory::VideoCodec>& codecs =
1481 encoder_factory_->codecs();
1482 for (size_t i = 0; i < codecs.size(); ++i) {
1483 bool is_internal_codec = internal_codec_names.find(codecs[i].name) !=
1484 internal_codec_names.end();
1485 if (!is_internal_codec) {
1486 if (!found)
1487 found = (in_codec.name == codecs[i].name);
1488 VideoCodec codec(
1489 GetExternalVideoPayloadType(static_cast<int>(i)),
1490 codecs[i].name,
1491 codecs[i].max_width,
1492 codecs[i].max_height,
1493 codecs[i].max_fps,
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00001494 // Use negative preference on external codec to ensure the internal
1495 // codec is preferred.
1496 static_cast<int>(0 - i));
mallinath@webrtc.org1112c302013-09-23 20:34:45 +00001497 AddDefaultFeedbackParams(&codec);
1498 video_codecs_.push_back(codec);
1499 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001500 }
1501 }
1502 ASSERT(found);
1503 return true;
1504}
1505
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001506// Ignore spammy trace messages, mostly from the stats API when we haven't
1507// gotten RTCP info yet from the remote side.
1508bool WebRtcVideoEngine::ShouldIgnoreTrace(const std::string& trace) {
1509 static const char* const kTracesToIgnore[] = {
1510 NULL
1511 };
1512 for (const char* const* p = kTracesToIgnore; *p; ++p) {
1513 if (trace.find(*p) == 0) {
1514 return true;
1515 }
1516 }
1517 return false;
1518}
1519
1520int WebRtcVideoEngine::GetNumOfChannels() {
1521 talk_base::CritScope cs(&channels_crit_);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001522 return static_cast<int>(channels_.size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001523}
1524
1525void WebRtcVideoEngine::Print(webrtc::TraceLevel level, const char* trace,
1526 int length) {
1527 talk_base::LoggingSeverity sev = talk_base::LS_VERBOSE;
1528 if (level == webrtc::kTraceError || level == webrtc::kTraceCritical)
1529 sev = talk_base::LS_ERROR;
1530 else if (level == webrtc::kTraceWarning)
1531 sev = talk_base::LS_WARNING;
1532 else if (level == webrtc::kTraceStateInfo || level == webrtc::kTraceInfo)
1533 sev = talk_base::LS_INFO;
1534 else if (level == webrtc::kTraceTerseInfo)
1535 sev = talk_base::LS_INFO;
1536
1537 // Skip past boilerplate prefix text
1538 if (length < 72) {
1539 std::string msg(trace, length);
1540 LOG(LS_ERROR) << "Malformed webrtc log message: ";
1541 LOG_V(sev) << msg;
1542 } else {
1543 std::string msg(trace + 71, length - 72);
1544 if (!ShouldIgnoreTrace(msg) &&
1545 (!voice_engine_ || !voice_engine_->ShouldIgnoreTrace(msg))) {
1546 LOG_V(sev) << "webrtc: " << msg;
1547 }
1548 }
1549}
1550
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001551webrtc::VideoDecoder* WebRtcVideoEngine::CreateExternalDecoder(
1552 webrtc::VideoCodecType type) {
1553 if (decoder_factory_ == NULL) {
1554 return NULL;
1555 }
1556 return decoder_factory_->CreateVideoDecoder(type);
1557}
1558
1559void WebRtcVideoEngine::DestroyExternalDecoder(webrtc::VideoDecoder* decoder) {
1560 ASSERT(decoder_factory_ != NULL);
1561 if (decoder_factory_ == NULL)
1562 return;
1563 decoder_factory_->DestroyVideoDecoder(decoder);
1564}
1565
1566webrtc::VideoEncoder* WebRtcVideoEngine::CreateExternalEncoder(
1567 webrtc::VideoCodecType type) {
1568 if (encoder_factory_ == NULL) {
1569 return NULL;
1570 }
1571 return encoder_factory_->CreateVideoEncoder(type);
1572}
1573
1574void WebRtcVideoEngine::DestroyExternalEncoder(webrtc::VideoEncoder* encoder) {
1575 ASSERT(encoder_factory_ != NULL);
1576 if (encoder_factory_ == NULL)
1577 return;
1578 encoder_factory_->DestroyVideoEncoder(encoder);
1579}
1580
1581bool WebRtcVideoEngine::IsExternalEncoderCodecType(
1582 webrtc::VideoCodecType type) const {
1583 if (!encoder_factory_)
1584 return false;
1585 const std::vector<WebRtcVideoEncoderFactory::VideoCodec>& codecs =
1586 encoder_factory_->codecs();
1587 std::vector<WebRtcVideoEncoderFactory::VideoCodec>::const_iterator it;
1588 for (it = codecs.begin(); it != codecs.end(); ++it) {
1589 if (it->type == type)
1590 return true;
1591 }
1592 return false;
1593}
1594
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001595void WebRtcVideoEngine::SetExternalDecoderFactory(
1596 WebRtcVideoDecoderFactory* decoder_factory) {
1597 decoder_factory_ = decoder_factory;
1598}
1599
1600void WebRtcVideoEngine::SetExternalEncoderFactory(
1601 WebRtcVideoEncoderFactory* encoder_factory) {
1602 if (encoder_factory_ == encoder_factory)
1603 return;
1604
1605 if (encoder_factory_) {
1606 encoder_factory_->RemoveObserver(this);
1607 }
1608 encoder_factory_ = encoder_factory;
1609 if (encoder_factory_) {
1610 encoder_factory_->AddObserver(this);
1611 }
1612
1613 // Invoke OnCodecAvailable() here in case the list of codecs is already
1614 // available when the encoder factory is installed. If not the encoder
1615 // factory will invoke the callback later when the codecs become available.
1616 OnCodecsAvailable();
1617}
1618
1619void WebRtcVideoEngine::OnCodecsAvailable() {
1620 // Rebuild codec list while reapplying the current default codec format.
1621 VideoCodec max_codec(kVideoCodecPrefs[0].payload_type,
1622 kVideoCodecPrefs[0].name,
1623 video_codecs_[0].width,
1624 video_codecs_[0].height,
1625 video_codecs_[0].framerate,
1626 0);
1627 if (!RebuildCodecList(max_codec)) {
1628 LOG(LS_ERROR) << "Failed to initialize list of supported codec types";
1629 }
1630}
1631
1632// WebRtcVideoMediaChannel
1633
1634WebRtcVideoMediaChannel::WebRtcVideoMediaChannel(
1635 WebRtcVideoEngine* engine,
1636 VoiceMediaChannel* channel)
1637 : engine_(engine),
1638 voice_channel_(channel),
1639 vie_channel_(-1),
1640 nack_enabled_(true),
1641 remb_enabled_(false),
1642 render_started_(false),
1643 first_receive_ssrc_(0),
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001644 num_unsignalled_recv_channels_(0),
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00001645 send_rtx_type_(-1),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001646 send_red_type_(-1),
1647 send_fec_type_(-1),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001648 sending_(false),
1649 ratio_w_(0),
1650 ratio_h_(0) {
1651 engine->RegisterChannel(this);
1652}
1653
1654bool WebRtcVideoMediaChannel::Init() {
1655 const uint32 ssrc_key = 0;
1656 return CreateChannel(ssrc_key, MD_SENDRECV, &vie_channel_);
1657}
1658
1659WebRtcVideoMediaChannel::~WebRtcVideoMediaChannel() {
1660 const bool send = false;
1661 SetSend(send);
1662 const bool render = false;
1663 SetRender(render);
1664
1665 while (!send_channels_.empty()) {
1666 if (!DeleteSendChannel(send_channels_.begin()->first)) {
1667 LOG(LS_ERROR) << "Unable to delete channel with ssrc key "
1668 << send_channels_.begin()->first;
1669 ASSERT(false);
1670 break;
1671 }
1672 }
1673
1674 // Remove all receive streams and the default channel.
1675 while (!recv_channels_.empty()) {
henrike@webrtc.org6e3dbc22014-03-25 17:09:47 +00001676 RemoveRecvStreamInternal(recv_channels_.begin()->first);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001677 }
1678
1679 // Unregister the channel from the engine.
1680 engine()->UnregisterChannel(this);
1681 if (worker_thread()) {
1682 worker_thread()->Clear(this);
1683 }
1684}
1685
1686bool WebRtcVideoMediaChannel::SetRecvCodecs(
1687 const std::vector<VideoCodec>& codecs) {
1688 receive_codecs_.clear();
buildbot@webrtc.orgdd4742a2014-05-07 14:50:35 +00001689 associated_payload_types_.clear();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001690 for (std::vector<VideoCodec>::const_iterator iter = codecs.begin();
1691 iter != codecs.end(); ++iter) {
1692 if (engine()->FindCodec(*iter)) {
1693 webrtc::VideoCodec wcodec;
1694 if (engine()->ConvertFromCricketVideoCodec(*iter, &wcodec)) {
1695 receive_codecs_.push_back(wcodec);
buildbot@webrtc.orgdd4742a2014-05-07 14:50:35 +00001696 int apt;
1697 if (iter->GetParam(cricket::kCodecParamAssociatedPayloadType, &apt)) {
1698 associated_payload_types_[wcodec.plType] = apt;
1699 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001700 }
1701 } else {
1702 LOG(LS_INFO) << "Unknown codec " << iter->name;
1703 return false;
1704 }
1705 }
1706
1707 for (RecvChannelMap::iterator it = recv_channels_.begin();
1708 it != recv_channels_.end(); ++it) {
1709 if (!SetReceiveCodecs(it->second))
1710 return false;
1711 }
1712 return true;
1713}
1714
1715bool WebRtcVideoMediaChannel::SetSendCodecs(
1716 const std::vector<VideoCodec>& codecs) {
1717 // Match with local video codec list.
1718 std::vector<webrtc::VideoCodec> send_codecs;
1719 VideoCodec checked_codec;
1720 VideoCodec current; // defaults to 0x0
1721 if (sending_) {
1722 ConvertToCricketVideoCodec(*send_codec_, &current);
1723 }
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00001724 std::map<int, int> primary_rtx_pt_mapping;
wu@webrtc.org05e7b442014-04-01 17:44:24 +00001725 bool nack_enabled = nack_enabled_;
1726 bool remb_enabled = remb_enabled_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001727 for (std::vector<VideoCodec>::const_iterator iter = codecs.begin();
1728 iter != codecs.end(); ++iter) {
1729 if (_stricmp(iter->name.c_str(), kRedPayloadName) == 0) {
1730 send_red_type_ = iter->id;
1731 } else if (_stricmp(iter->name.c_str(), kFecPayloadName) == 0) {
1732 send_fec_type_ = iter->id;
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00001733 } else if (_stricmp(iter->name.c_str(), kRtxCodecName) == 0) {
1734 int rtx_type = iter->id;
1735 int rtx_primary_type = -1;
1736 if (iter->GetParam(kCodecParamAssociatedPayloadType, &rtx_primary_type)) {
1737 primary_rtx_pt_mapping[rtx_primary_type] = rtx_type;
1738 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001739 } else if (engine()->CanSendCodec(*iter, current, &checked_codec)) {
1740 webrtc::VideoCodec wcodec;
1741 if (engine()->ConvertFromCricketVideoCodec(checked_codec, &wcodec)) {
1742 if (send_codecs.empty()) {
wu@webrtc.org05e7b442014-04-01 17:44:24 +00001743 nack_enabled = IsNackEnabled(checked_codec);
1744 remb_enabled = IsRembEnabled(checked_codec);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001745 }
1746 send_codecs.push_back(wcodec);
1747 }
1748 } else {
1749 LOG(LS_WARNING) << "Unknown codec " << iter->name;
1750 }
1751 }
1752
1753 // Fail if we don't have a match.
1754 if (send_codecs.empty()) {
1755 LOG(LS_WARNING) << "No matching codecs available";
1756 return false;
1757 }
1758
1759 // Recv protection.
wu@webrtc.org05e7b442014-04-01 17:44:24 +00001760 // Do not update if the status is same as previously configured.
1761 if (nack_enabled_ != nack_enabled) {
1762 for (RecvChannelMap::iterator it = recv_channels_.begin();
1763 it != recv_channels_.end(); ++it) {
1764 int channel_id = it->second->channel_id();
1765 if (!SetNackFec(channel_id, send_red_type_, send_fec_type_,
1766 nack_enabled)) {
1767 return false;
1768 }
1769 if (engine_->vie()->rtp()->SetRembStatus(channel_id,
1770 kNotSending,
1771 remb_enabled_) != 0) {
1772 LOG_RTCERR3(SetRembStatus, channel_id, kNotSending, remb_enabled_);
1773 return false;
1774 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001775 }
wu@webrtc.org05e7b442014-04-01 17:44:24 +00001776 nack_enabled_ = nack_enabled;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001777 }
1778
1779 // Send settings.
wu@webrtc.org05e7b442014-04-01 17:44:24 +00001780 // Do not update if the status is same as previously configured.
1781 if (remb_enabled_ != remb_enabled) {
1782 for (SendChannelMap::iterator iter = send_channels_.begin();
1783 iter != send_channels_.end(); ++iter) {
1784 int channel_id = iter->second->channel_id();
1785 if (!SetNackFec(channel_id, send_red_type_, send_fec_type_,
1786 nack_enabled_)) {
1787 return false;
1788 }
1789 if (engine_->vie()->rtp()->SetRembStatus(channel_id,
1790 remb_enabled,
1791 remb_enabled) != 0) {
1792 LOG_RTCERR3(SetRembStatus, channel_id, remb_enabled, remb_enabled);
1793 return false;
1794 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001795 }
wu@webrtc.org05e7b442014-04-01 17:44:24 +00001796 remb_enabled_ = remb_enabled;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001797 }
1798
1799 // Select the first matched codec.
1800 webrtc::VideoCodec& codec(send_codecs[0]);
1801
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00001802 // Set RTX payload type if primary now active. This value will be used in
1803 // SetSendCodec.
1804 std::map<int, int>::const_iterator rtx_it =
1805 primary_rtx_pt_mapping.find(static_cast<int>(codec.plType));
1806 if (rtx_it != primary_rtx_pt_mapping.end()) {
1807 send_rtx_type_ = rtx_it->second;
1808 }
1809
buildbot@webrtc.orgd1ae89f2014-05-08 19:19:26 +00001810 if (BitrateIsSet(codec.minBitrate) && BitrateIsSet(codec.maxBitrate) &&
1811 codec.minBitrate > codec.maxBitrate) {
1812 // TODO(pthatcher): This behavior contradicts other behavior in
1813 // this file which will cause min > max to push the min down to
1814 // the max. There are unit tests for both behaviors. We should
1815 // pick one and do that.
1816 LOG(LS_INFO) << "Rejecting codec with min bitrate ("
1817 << codec.minBitrate << ") larger than max ("
1818 << codec.maxBitrate << "). ";
1819 return false;
1820 }
1821
1822 if (!SetSendCodec(codec)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001823 return false;
1824 }
1825
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001826 LogSendCodecChange("SetSendCodecs()");
1827
1828 return true;
1829}
1830
1831bool WebRtcVideoMediaChannel::GetSendCodec(VideoCodec* send_codec) {
1832 if (!send_codec_) {
1833 return false;
1834 }
1835 ConvertToCricketVideoCodec(*send_codec_, send_codec);
1836 return true;
1837}
1838
1839bool WebRtcVideoMediaChannel::SetSendStreamFormat(uint32 ssrc,
1840 const VideoFormat& format) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001841 WebRtcVideoChannelSendInfo* send_channel = GetSendChannel(ssrc);
1842 if (!send_channel) {
1843 LOG(LS_ERROR) << "The specified ssrc " << ssrc << " is not in use.";
1844 return false;
1845 }
1846 send_channel->set_video_format(format);
1847 return true;
1848}
1849
1850bool WebRtcVideoMediaChannel::SetRender(bool render) {
1851 if (render == render_started_) {
1852 return true; // no action required
1853 }
1854
1855 bool ret = true;
1856 for (RecvChannelMap::iterator it = recv_channels_.begin();
1857 it != recv_channels_.end(); ++it) {
1858 if (render) {
1859 if (engine()->vie()->render()->StartRender(
1860 it->second->channel_id()) != 0) {
1861 LOG_RTCERR1(StartRender, it->second->channel_id());
1862 ret = false;
1863 }
1864 } else {
1865 if (engine()->vie()->render()->StopRender(
1866 it->second->channel_id()) != 0) {
1867 LOG_RTCERR1(StopRender, it->second->channel_id());
1868 ret = false;
1869 }
1870 }
1871 }
1872 if (ret) {
1873 render_started_ = render;
1874 }
1875
1876 return ret;
1877}
1878
1879bool WebRtcVideoMediaChannel::SetSend(bool send) {
1880 if (!HasReadySendChannels() && send) {
1881 LOG(LS_ERROR) << "No stream added";
1882 return false;
1883 }
1884 if (send == sending()) {
1885 return true; // No action required.
1886 }
1887
1888 if (send) {
1889 // We've been asked to start sending.
1890 // SetSendCodecs must have been called already.
1891 if (!send_codec_) {
1892 return false;
1893 }
1894 // Start send now.
1895 if (!StartSend()) {
1896 return false;
1897 }
1898 } else {
1899 // We've been asked to stop sending.
1900 if (!StopSend()) {
1901 return false;
1902 }
1903 }
1904 sending_ = send;
1905
1906 return true;
1907}
1908
1909bool WebRtcVideoMediaChannel::AddSendStream(const StreamParams& sp) {
henrike@webrtc.org6e3dbc22014-03-25 17:09:47 +00001910 if (sp.first_ssrc() == 0) {
1911 LOG(LS_ERROR) << "AddSendStream with 0 ssrc is not supported.";
1912 return false;
1913 }
1914
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001915 LOG(LS_INFO) << "AddSendStream " << sp.ToString();
1916
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00001917 if (!IsOneSsrcStream(sp) && !IsSimulcastStream(sp)) {
1918 LOG(LS_ERROR) << "AddSendStream: bad local stream parameters";
1919 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001920 }
1921
1922 uint32 ssrc_key;
1923 if (!CreateSendChannelKey(sp.first_ssrc(), &ssrc_key)) {
1924 LOG(LS_ERROR) << "Trying to register duplicate ssrc: " << sp.first_ssrc();
1925 return false;
1926 }
1927 // If the default channel is already used for sending create a new channel
1928 // otherwise use the default channel for sending.
1929 int channel_id = -1;
1930 if (send_channels_[0]->stream_params() == NULL) {
1931 channel_id = vie_channel_;
1932 } else {
1933 if (!CreateChannel(ssrc_key, MD_SEND, &channel_id)) {
1934 LOG(LS_ERROR) << "AddSendStream: unable to create channel";
1935 return false;
1936 }
1937 }
1938 WebRtcVideoChannelSendInfo* send_channel = send_channels_[ssrc_key];
1939 // Set the send (local) SSRC.
1940 // If there are multiple send SSRCs, we can only set the first one here, and
1941 // the rest of the SSRC(s) need to be set after SetSendCodec has been called
1942 // (with a codec requires multiple SSRC(s)).
1943 if (engine()->vie()->rtp()->SetLocalSSRC(channel_id,
1944 sp.first_ssrc()) != 0) {
1945 LOG_RTCERR2(SetLocalSSRC, channel_id, sp.first_ssrc());
1946 return false;
1947 }
1948
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00001949 // Set the corresponding RTX SSRC.
1950 if (!SetLocalRtxSsrc(channel_id, sp, sp.first_ssrc(), 0)) {
1951 return false;
1952 }
1953
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001954 // Set RTCP CName.
1955 if (engine()->vie()->rtp()->SetRTCPCName(channel_id,
1956 sp.cname.c_str()) != 0) {
1957 LOG_RTCERR2(SetRTCPCName, channel_id, sp.cname.c_str());
1958 return false;
1959 }
1960
1961 // At this point the channel's local SSRC has been updated. If the channel is
1962 // the default channel make sure that all the receive channels are updated as
1963 // well. Receive channels have to have the same SSRC as the default channel in
1964 // order to send receiver reports with this SSRC.
1965 if (IsDefaultChannel(channel_id)) {
1966 for (RecvChannelMap::const_iterator it = recv_channels_.begin();
1967 it != recv_channels_.end(); ++it) {
1968 WebRtcVideoChannelRecvInfo* info = it->second;
1969 int channel_id = info->channel_id();
1970 if (engine()->vie()->rtp()->SetLocalSSRC(channel_id,
1971 sp.first_ssrc()) != 0) {
1972 LOG_RTCERR1(SetLocalSSRC, it->first);
1973 return false;
1974 }
1975 }
1976 }
1977
1978 send_channel->set_stream_params(sp);
1979
1980 // Reset send codec after stream parameters changed.
1981 if (send_codec_) {
buildbot@webrtc.orgd1ae89f2014-05-08 19:19:26 +00001982 if (!SetSendCodec(send_channel, *send_codec_)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001983 return false;
1984 }
1985 LogSendCodecChange("SetSendStreamFormat()");
1986 }
1987
1988 if (sending_) {
1989 return StartSend(send_channel);
1990 }
1991 return true;
1992}
1993
1994bool WebRtcVideoMediaChannel::RemoveSendStream(uint32 ssrc) {
henrike@webrtc.org6e3dbc22014-03-25 17:09:47 +00001995 if (ssrc == 0) {
1996 LOG(LS_ERROR) << "RemoveSendStream with 0 ssrc is not supported.";
1997 return false;
1998 }
1999
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002000 uint32 ssrc_key;
2001 if (!GetSendChannelKey(ssrc, &ssrc_key)) {
2002 LOG(LS_WARNING) << "Try to remove stream with ssrc " << ssrc
2003 << " which doesn't exist.";
2004 return false;
2005 }
2006 WebRtcVideoChannelSendInfo* send_channel = send_channels_[ssrc_key];
2007 int channel_id = send_channel->channel_id();
2008 if (IsDefaultChannel(channel_id) && (send_channel->stream_params() == NULL)) {
2009 // Default channel will still exist. However, if stream_params() is NULL
2010 // there is no stream to remove.
2011 return false;
2012 }
2013 if (sending_) {
2014 StopSend(send_channel);
2015 }
2016
2017 const WebRtcVideoChannelSendInfo::EncoderMap& encoder_map =
2018 send_channel->registered_encoders();
2019 for (WebRtcVideoChannelSendInfo::EncoderMap::const_iterator it =
2020 encoder_map.begin(); it != encoder_map.end(); ++it) {
2021 if (engine()->vie()->ext_codec()->DeRegisterExternalSendCodec(
2022 channel_id, it->first) != 0) {
2023 LOG_RTCERR1(DeregisterEncoderObserver, channel_id);
2024 }
2025 engine()->DestroyExternalEncoder(it->second);
2026 }
2027 send_channel->ClearRegisteredEncoders();
2028
2029 // The receive channels depend on the default channel, recycle it instead.
2030 if (IsDefaultChannel(channel_id)) {
2031 SetCapturer(GetDefaultChannelSsrc(), NULL);
2032 send_channel->ClearStreamParams();
2033 } else {
2034 return DeleteSendChannel(ssrc_key);
2035 }
2036 return true;
2037}
2038
2039bool WebRtcVideoMediaChannel::AddRecvStream(const StreamParams& sp) {
henrike@webrtc.org6e3dbc22014-03-25 17:09:47 +00002040 if (sp.first_ssrc() == 0) {
2041 LOG(LS_ERROR) << "AddRecvStream with 0 ssrc is not supported.";
2042 return false;
2043 }
2044
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002045 // TODO(zhurunz) Remove this once BWE works properly across different send
2046 // and receive channels.
2047 // Reuse default channel for recv stream in 1:1 call.
2048 if (!InConferenceMode() && first_receive_ssrc_ == 0) {
2049 LOG(LS_INFO) << "Recv stream " << sp.first_ssrc()
2050 << " reuse default channel #"
2051 << vie_channel_;
2052 first_receive_ssrc_ = sp.first_ssrc();
2053 if (render_started_) {
2054 if (engine()->vie()->render()->StartRender(vie_channel_) !=0) {
2055 LOG_RTCERR1(StartRender, vie_channel_);
2056 }
2057 }
2058 return true;
2059 }
2060
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002061 int channel_id = -1;
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00002062 RecvChannelMap::iterator channel_iterator =
2063 recv_channels_.find(sp.first_ssrc());
2064 if (channel_iterator == recv_channels_.end() &&
2065 first_receive_ssrc_ != sp.first_ssrc()) {
2066 // TODO(perkj): Implement recv media from multiple media SSRCs per stream.
2067 // NOTE: We have two SSRCs per stream when RTX is enabled.
2068 if (!IsOneSsrcStream(sp)) {
2069 LOG(LS_ERROR) << "WebRtcVideoMediaChannel supports one primary SSRC per"
2070 << " stream and one FID SSRC per primary SSRC.";
2071 return false;
2072 }
2073
2074 // Create a new channel for receiving video data.
2075 // In order to get the bandwidth estimation work fine for
2076 // receive only channels, we connect all receiving channels
2077 // to our master send channel.
2078 if (!CreateChannel(sp.first_ssrc(), MD_RECV, &channel_id)) {
2079 return false;
2080 }
2081 } else {
2082 // Already exists.
2083 if (first_receive_ssrc_ == sp.first_ssrc()) {
2084 return false;
2085 }
2086 // Early receive added channel.
2087 channel_id = (*channel_iterator).second->channel_id();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002088 }
buildbot@webrtc.orgdd4742a2014-05-07 14:50:35 +00002089 channel_iterator = recv_channels_.find(sp.first_ssrc());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002090
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002091 // Set the corresponding RTX SSRC.
2092 uint32 rtx_ssrc;
2093 bool has_rtx = sp.GetFidSsrc(sp.first_ssrc(), &rtx_ssrc);
buildbot@webrtc.orgdd4742a2014-05-07 14:50:35 +00002094 if (has_rtx) {
2095 LOG(LS_INFO) << "Setting rtx ssrc " << rtx_ssrc << " for stream "
2096 << sp.first_ssrc();
2097 if (engine()->vie()->rtp()->SetRemoteSSRCType(
2098 channel_id, webrtc::kViEStreamTypeRtx, rtx_ssrc) != 0) {
2099 LOG_RTCERR3(SetRemoteSSRCType, channel_id, webrtc::kViEStreamTypeRtx,
2100 rtx_ssrc);
2101 return false;
2102 }
2103 rtx_to_primary_ssrc_[rtx_ssrc] = sp.first_ssrc();
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002104 }
2105
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002106 // Get the default renderer.
2107 VideoRenderer* default_renderer = NULL;
2108 if (InConferenceMode()) {
2109 // The recv_channels_ size start out being 1, so if it is two here this
2110 // is the first receive channel created (vie_channel_ is not used for
2111 // receiving in a conference call). This means that the renderer stored
2112 // inside vie_channel_ should be used for the just created channel.
2113 if (recv_channels_.size() == 2 &&
2114 recv_channels_.find(0) != recv_channels_.end()) {
2115 GetRenderer(0, &default_renderer);
2116 }
2117 }
2118
2119 // The first recv stream reuses the default renderer (if a default renderer
2120 // has been set).
2121 if (default_renderer) {
2122 SetRenderer(sp.first_ssrc(), default_renderer);
2123 }
2124
2125 LOG(LS_INFO) << "New video stream " << sp.first_ssrc()
2126 << " registered to VideoEngine channel #"
2127 << channel_id << " and connected to channel #" << vie_channel_;
2128
2129 return true;
2130}
2131
2132bool WebRtcVideoMediaChannel::RemoveRecvStream(uint32 ssrc) {
henrike@webrtc.org6e3dbc22014-03-25 17:09:47 +00002133 if (ssrc == 0) {
2134 LOG(LS_ERROR) << "RemoveRecvStream with 0 ssrc is not supported.";
2135 return false;
2136 }
2137 return RemoveRecvStreamInternal(ssrc);
2138}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002139
henrike@webrtc.org6e3dbc22014-03-25 17:09:47 +00002140bool WebRtcVideoMediaChannel::RemoveRecvStreamInternal(uint32 ssrc) {
2141 RecvChannelMap::iterator it = recv_channels_.find(ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002142 if (it == recv_channels_.end()) {
2143 // TODO(perkj): Remove this once BWE works properly across different send
2144 // and receive channels.
2145 // The default channel is reused for recv stream in 1:1 call.
2146 if (first_receive_ssrc_ == ssrc) {
2147 first_receive_ssrc_ = 0;
2148 // Need to stop the renderer and remove it since the render window can be
2149 // deleted after this.
2150 if (render_started_) {
2151 if (engine()->vie()->render()->StopRender(vie_channel_) !=0) {
2152 LOG_RTCERR1(StopRender, it->second->channel_id());
2153 }
2154 }
2155 recv_channels_[0]->SetRenderer(NULL);
2156 return true;
2157 }
2158 return false;
2159 }
2160 WebRtcVideoChannelRecvInfo* info = it->second;
buildbot@webrtc.orgdd4742a2014-05-07 14:50:35 +00002161
2162 // Remove any RTX SSRC mappings to this stream.
2163 SsrcMap::iterator rtx_it = rtx_to_primary_ssrc_.begin();
2164 while (rtx_it != rtx_to_primary_ssrc_.end()) {
2165 if (rtx_it->second == ssrc) {
2166 rtx_to_primary_ssrc_.erase(rtx_it++);
2167 } else {
2168 ++rtx_it;
2169 }
2170 }
2171
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002172 int channel_id = info->channel_id();
2173 if (engine()->vie()->render()->RemoveRenderer(channel_id) != 0) {
2174 LOG_RTCERR1(RemoveRenderer, channel_id);
2175 }
2176
2177 if (engine()->vie()->network()->DeregisterSendTransport(channel_id) !=0) {
2178 LOG_RTCERR1(DeRegisterSendTransport, channel_id);
2179 }
2180
2181 if (engine()->vie()->codec()->DeregisterDecoderObserver(
2182 channel_id) != 0) {
2183 LOG_RTCERR1(DeregisterDecoderObserver, channel_id);
2184 }
2185
2186 const WebRtcVideoChannelRecvInfo::DecoderMap& decoder_map =
2187 info->registered_decoders();
2188 for (WebRtcVideoChannelRecvInfo::DecoderMap::const_iterator it =
2189 decoder_map.begin(); it != decoder_map.end(); ++it) {
2190 if (engine()->vie()->ext_codec()->DeRegisterExternalReceiveCodec(
2191 channel_id, it->first) != 0) {
2192 LOG_RTCERR1(DeregisterDecoderObserver, channel_id);
2193 }
2194 engine()->DestroyExternalDecoder(it->second);
2195 }
2196 info->ClearRegisteredDecoders();
2197
2198 LOG(LS_INFO) << "Removing video stream " << ssrc
2199 << " with VideoEngine channel #"
2200 << channel_id;
wu@webrtc.org9caf2762013-12-11 18:25:07 +00002201 bool ret = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002202 if (engine()->vie()->base()->DeleteChannel(channel_id) == -1) {
2203 LOG_RTCERR1(DeleteChannel, channel_id);
wu@webrtc.org9caf2762013-12-11 18:25:07 +00002204 ret = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002205 }
2206 // Delete the WebRtcVideoChannelRecvInfo pointed to by it->second.
2207 delete info;
2208 recv_channels_.erase(it);
wu@webrtc.org9caf2762013-12-11 18:25:07 +00002209 return ret;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002210}
2211
2212bool WebRtcVideoMediaChannel::StartSend() {
2213 bool success = true;
2214 for (SendChannelMap::iterator iter = send_channels_.begin();
2215 iter != send_channels_.end(); ++iter) {
2216 WebRtcVideoChannelSendInfo* send_channel = iter->second;
2217 if (!StartSend(send_channel)) {
2218 success = false;
2219 }
2220 }
2221 return success;
2222}
2223
2224bool WebRtcVideoMediaChannel::StartSend(
2225 WebRtcVideoChannelSendInfo* send_channel) {
2226 const int channel_id = send_channel->channel_id();
2227 if (engine()->vie()->base()->StartSend(channel_id) != 0) {
2228 LOG_RTCERR1(StartSend, channel_id);
2229 return false;
2230 }
2231
2232 send_channel->set_sending(true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002233 return true;
2234}
2235
2236bool WebRtcVideoMediaChannel::StopSend() {
2237 bool success = true;
2238 for (SendChannelMap::iterator iter = send_channels_.begin();
2239 iter != send_channels_.end(); ++iter) {
2240 WebRtcVideoChannelSendInfo* send_channel = iter->second;
2241 if (!StopSend(send_channel)) {
2242 success = false;
2243 }
2244 }
2245 return success;
2246}
2247
2248bool WebRtcVideoMediaChannel::StopSend(
2249 WebRtcVideoChannelSendInfo* send_channel) {
2250 const int channel_id = send_channel->channel_id();
2251 if (engine()->vie()->base()->StopSend(channel_id) != 0) {
2252 LOG_RTCERR1(StopSend, channel_id);
2253 return false;
2254 }
2255 send_channel->set_sending(false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002256 return true;
2257}
2258
2259bool WebRtcVideoMediaChannel::SendIntraFrame() {
2260 bool success = true;
2261 for (SendChannelMap::iterator iter = send_channels_.begin();
2262 iter != send_channels_.end();
2263 ++iter) {
2264 WebRtcVideoChannelSendInfo* send_channel = iter->second;
2265 const int channel_id = send_channel->channel_id();
2266 if (engine()->vie()->codec()->SendKeyFrame(channel_id) != 0) {
2267 LOG_RTCERR1(SendKeyFrame, channel_id);
2268 success = false;
2269 }
2270 }
2271 return success;
2272}
2273
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002274bool WebRtcVideoMediaChannel::HasReadySendChannels() {
2275 return !send_channels_.empty() &&
2276 ((send_channels_.size() > 1) ||
2277 (send_channels_[0]->stream_params() != NULL));
2278}
2279
2280bool WebRtcVideoMediaChannel::GetSendChannelKey(uint32 local_ssrc,
2281 uint32* key) {
2282 *key = 0;
2283 // If a send channel is not ready to send it will not have local_ssrc
2284 // registered to it.
2285 if (!HasReadySendChannels()) {
2286 return false;
2287 }
2288 // The default channel is stored with key 0. The key therefore does not match
2289 // the SSRC associated with the default channel. Check if the SSRC provided
2290 // corresponds to the default channel's SSRC.
2291 if (local_ssrc == GetDefaultChannelSsrc()) {
2292 return true;
2293 }
2294 if (send_channels_.find(local_ssrc) == send_channels_.end()) {
2295 for (SendChannelMap::iterator iter = send_channels_.begin();
2296 iter != send_channels_.end(); ++iter) {
2297 WebRtcVideoChannelSendInfo* send_channel = iter->second;
2298 if (send_channel->has_ssrc(local_ssrc)) {
2299 *key = iter->first;
2300 return true;
2301 }
2302 }
2303 return false;
2304 }
2305 // The key was found in the above std::map::find call. This means that the
2306 // ssrc is the key.
2307 *key = local_ssrc;
2308 return true;
2309}
2310
2311WebRtcVideoChannelSendInfo* WebRtcVideoMediaChannel::GetSendChannel(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002312 uint32 local_ssrc) {
2313 uint32 key;
2314 if (!GetSendChannelKey(local_ssrc, &key)) {
2315 return NULL;
2316 }
2317 return send_channels_[key];
2318}
2319
2320bool WebRtcVideoMediaChannel::CreateSendChannelKey(uint32 local_ssrc,
2321 uint32* key) {
2322 if (GetSendChannelKey(local_ssrc, key)) {
2323 // If there is a key corresponding to |local_ssrc|, the SSRC is already in
2324 // use. SSRCs need to be unique in a session and at this point a duplicate
2325 // SSRC has been detected.
2326 return false;
2327 }
2328 if (send_channels_[0]->stream_params() == NULL) {
2329 // key should be 0 here as the default channel should be re-used whenever it
2330 // is not used.
2331 *key = 0;
2332 return true;
2333 }
2334 // SSRC is currently not in use and the default channel is already in use. Use
2335 // the SSRC as key since it is supposed to be unique in a session.
2336 *key = local_ssrc;
2337 return true;
2338}
2339
wu@webrtc.org24301a62013-12-13 19:17:43 +00002340int WebRtcVideoMediaChannel::GetSendChannelNum(VideoCapturer* capturer) {
2341 int num = 0;
2342 for (SendChannelMap::iterator iter = send_channels_.begin();
2343 iter != send_channels_.end(); ++iter) {
2344 WebRtcVideoChannelSendInfo* send_channel = iter->second;
2345 if (send_channel->video_capturer() == capturer) {
2346 ++num;
2347 }
2348 }
2349 return num;
2350}
2351
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002352uint32 WebRtcVideoMediaChannel::GetDefaultChannelSsrc() {
2353 WebRtcVideoChannelSendInfo* send_channel = send_channels_[0];
2354 const StreamParams* sp = send_channel->stream_params();
2355 if (sp == NULL) {
2356 // This happens if no send stream is currently registered.
2357 return 0;
2358 }
2359 return sp->first_ssrc();
2360}
2361
2362bool WebRtcVideoMediaChannel::DeleteSendChannel(uint32 ssrc_key) {
2363 if (send_channels_.find(ssrc_key) == send_channels_.end()) {
2364 return false;
2365 }
2366 WebRtcVideoChannelSendInfo* send_channel = send_channels_[ssrc_key];
wu@webrtc.org24301a62013-12-13 19:17:43 +00002367 MaybeDisconnectCapturer(send_channel->video_capturer());
henrike@webrtc.orgc7bec842014-03-12 19:53:43 +00002368 send_channel->set_video_capturer(NULL, engine()->vie());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002369
2370 int channel_id = send_channel->channel_id();
2371 int capture_id = send_channel->capture_id();
2372 if (engine()->vie()->codec()->DeregisterEncoderObserver(
2373 channel_id) != 0) {
2374 LOG_RTCERR1(DeregisterEncoderObserver, channel_id);
2375 }
2376
2377 // Destroy the external capture interface.
2378 if (engine()->vie()->capture()->DisconnectCaptureDevice(
2379 channel_id) != 0) {
2380 LOG_RTCERR1(DisconnectCaptureDevice, channel_id);
2381 }
2382 if (engine()->vie()->capture()->ReleaseCaptureDevice(
2383 capture_id) != 0) {
2384 LOG_RTCERR1(ReleaseCaptureDevice, capture_id);
2385 }
2386
2387 // The default channel is stored in both |send_channels_| and
2388 // |recv_channels_|. To make sure it is only deleted once from vie let the
2389 // delete call happen when tearing down |recv_channels_| and not here.
2390 if (!IsDefaultChannel(channel_id)) {
2391 engine_->vie()->base()->DeleteChannel(channel_id);
2392 }
2393 delete send_channel;
2394 send_channels_.erase(ssrc_key);
2395 return true;
2396}
2397
2398bool WebRtcVideoMediaChannel::RemoveCapturer(uint32 ssrc) {
2399 WebRtcVideoChannelSendInfo* send_channel = GetSendChannel(ssrc);
2400 if (!send_channel) {
2401 return false;
2402 }
2403 VideoCapturer* capturer = send_channel->video_capturer();
2404 if (capturer == NULL) {
2405 return false;
2406 }
wu@webrtc.org24301a62013-12-13 19:17:43 +00002407 MaybeDisconnectCapturer(capturer);
henrike@webrtc.orgc7bec842014-03-12 19:53:43 +00002408 send_channel->set_video_capturer(NULL, engine()->vie());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002409 const int64 timestamp = send_channel->local_stream_info()->time_stamp();
2410 if (send_codec_) {
2411 QueueBlackFrame(ssrc, timestamp, send_codec_->maxFramerate);
2412 }
2413 return true;
2414}
2415
2416bool WebRtcVideoMediaChannel::SetRenderer(uint32 ssrc,
2417 VideoRenderer* renderer) {
2418 if (recv_channels_.find(ssrc) == recv_channels_.end()) {
2419 // TODO(perkj): Remove this once BWE works properly across different send
2420 // and receive channels.
2421 // The default channel is reused for recv stream in 1:1 call.
2422 if (first_receive_ssrc_ == ssrc &&
2423 recv_channels_.find(0) != recv_channels_.end()) {
2424 LOG(LS_INFO) << "SetRenderer " << ssrc
2425 << " reuse default channel #"
2426 << vie_channel_;
2427 recv_channels_[0]->SetRenderer(renderer);
2428 return true;
2429 }
2430 return false;
2431 }
2432
2433 recv_channels_[ssrc]->SetRenderer(renderer);
2434 return true;
2435}
2436
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00002437bool WebRtcVideoMediaChannel::GetStats(const StatsOptions& options,
2438 VideoMediaInfo* info) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002439 // Get sender statistics and build VideoSenderInfo.
2440 unsigned int total_bitrate_sent = 0;
2441 unsigned int video_bitrate_sent = 0;
2442 unsigned int fec_bitrate_sent = 0;
2443 unsigned int nack_bitrate_sent = 0;
2444 unsigned int estimated_send_bandwidth = 0;
2445 unsigned int target_enc_bitrate = 0;
2446 if (send_codec_) {
2447 for (SendChannelMap::const_iterator iter = send_channels_.begin();
2448 iter != send_channels_.end(); ++iter) {
2449 WebRtcVideoChannelSendInfo* send_channel = iter->second;
2450 const int channel_id = send_channel->channel_id();
2451 VideoSenderInfo sinfo;
2452 const StreamParams* send_params = send_channel->stream_params();
2453 if (send_params == NULL) {
2454 // This should only happen if the default vie channel is not in use.
2455 // This can happen if no streams have ever been added or the stream
2456 // corresponding to the default channel has been removed. Note that
2457 // there may be non-default vie channels in use when this happen so
2458 // asserting send_channels_.size() == 1 is not correct and neither is
2459 // breaking out of the loop.
2460 ASSERT(channel_id == vie_channel_);
2461 continue;
2462 }
2463 unsigned int bytes_sent, packets_sent, bytes_recv, packets_recv;
2464 if (engine_->vie()->rtp()->GetRTPStatistics(channel_id, bytes_sent,
2465 packets_sent, bytes_recv,
2466 packets_recv) != 0) {
2467 LOG_RTCERR1(GetRTPStatistics, vie_channel_);
2468 continue;
2469 }
2470 WebRtcLocalStreamInfo* channel_stream_info =
2471 send_channel->local_stream_info();
2472
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002473 for (size_t i = 0; i < send_params->ssrcs.size(); ++i) {
2474 sinfo.add_ssrc(send_params->ssrcs[i]);
2475 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002476 sinfo.codec_name = send_codec_->plName;
2477 sinfo.bytes_sent = bytes_sent;
2478 sinfo.packets_sent = packets_sent;
2479 sinfo.packets_cached = -1;
2480 sinfo.packets_lost = -1;
2481 sinfo.fraction_lost = -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002482 sinfo.rtt_ms = -1;
wu@webrtc.org987f2c92014-03-28 16:22:19 +00002483
2484 VideoCapturer* video_capturer = send_channel->video_capturer();
2485 if (video_capturer) {
buildbot@webrtc.org0b53bd22014-05-06 17:12:36 +00002486 VideoFormat last_captured_frame_format;
wu@webrtc.org987f2c92014-03-28 16:22:19 +00002487 video_capturer->GetStats(&sinfo.adapt_frame_drops,
2488 &sinfo.effects_frame_drops,
buildbot@webrtc.org0b53bd22014-05-06 17:12:36 +00002489 &sinfo.capturer_frame_time,
2490 &last_captured_frame_format);
2491 sinfo.input_frame_width = last_captured_frame_format.width;
2492 sinfo.input_frame_height = last_captured_frame_format.height;
2493 } else {
2494 sinfo.input_frame_width = 0;
2495 sinfo.input_frame_height = 0;
wu@webrtc.org987f2c92014-03-28 16:22:19 +00002496 }
2497
2498 webrtc::VideoCodec vie_codec;
wu@webrtc.org987f2c92014-03-28 16:22:19 +00002499 if (!video_capturer || video_capturer->IsMuted()) {
2500 sinfo.send_frame_width = 0;
2501 sinfo.send_frame_height = 0;
2502 } else if (engine()->vie()->codec()->GetSendCodec(channel_id,
2503 vie_codec) == 0) {
2504 sinfo.send_frame_width = vie_codec.width;
2505 sinfo.send_frame_height = vie_codec.height;
2506 } else {
2507 sinfo.send_frame_width = -1;
2508 sinfo.send_frame_height = -1;
2509 LOG_RTCERR1(GetSendCodec, channel_id);
2510 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002511 sinfo.framerate_input = channel_stream_info->framerate();
2512 sinfo.framerate_sent = send_channel->encoder_observer()->framerate();
2513 sinfo.nominal_bitrate = send_channel->encoder_observer()->bitrate();
buildbot@webrtc.orgd1ae89f2014-05-08 19:19:26 +00002514 if (send_codec_) {
2515 sinfo.preferred_bitrate = GetBitrate(
2516 send_codec_->maxBitrate, kMaxVideoBitrate);
2517 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002518 sinfo.adapt_reason = send_channel->CurrentAdaptReason();
buildbot@webrtc.orgb525a9d2014-06-03 09:42:15 +00002519
2520#ifdef USE_WEBRTC_DEV_BRANCH
2521 webrtc::CpuOveruseMetrics metrics;
2522 engine()->vie()->base()->GetCpuOveruseMetrics(channel_id, &metrics);
2523 sinfo.capture_jitter_ms = metrics.capture_jitter_ms;
2524 sinfo.avg_encode_ms = metrics.avg_encode_time_ms;
2525 sinfo.encode_usage_percent = metrics.encode_usage_percent;
2526 sinfo.capture_queue_delay_ms_per_s = metrics.capture_queue_delay_ms_per_s;
2527#else
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002528 sinfo.capture_jitter_ms = -1;
2529 sinfo.avg_encode_ms = -1;
wu@webrtc.org9caf2762013-12-11 18:25:07 +00002530 sinfo.encode_usage_percent = -1;
2531 sinfo.capture_queue_delay_ms_per_s = -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002532
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002533 int capture_jitter_ms = 0;
2534 int avg_encode_time_ms = 0;
wu@webrtc.org9caf2762013-12-11 18:25:07 +00002535 int encode_usage_percent = 0;
2536 int capture_queue_delay_ms_per_s = 0;
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002537 if (engine()->vie()->base()->CpuOveruseMeasures(
wu@webrtc.org9caf2762013-12-11 18:25:07 +00002538 channel_id,
2539 &capture_jitter_ms,
2540 &avg_encode_time_ms,
2541 &encode_usage_percent,
2542 &capture_queue_delay_ms_per_s) == 0) {
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002543 sinfo.capture_jitter_ms = capture_jitter_ms;
2544 sinfo.avg_encode_ms = avg_encode_time_ms;
wu@webrtc.org9caf2762013-12-11 18:25:07 +00002545 sinfo.encode_usage_percent = encode_usage_percent;
2546 sinfo.capture_queue_delay_ms_per_s = capture_queue_delay_ms_per_s;
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002547 }
buildbot@webrtc.orgb525a9d2014-06-03 09:42:15 +00002548#endif
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002549
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00002550 webrtc::RtcpPacketTypeCounter rtcp_sent;
2551 webrtc::RtcpPacketTypeCounter rtcp_received;
2552 if (engine()->vie()->rtp()->GetRtcpPacketTypeCounters(
2553 channel_id, &rtcp_sent, &rtcp_received) == 0) {
2554 sinfo.firs_rcvd = rtcp_received.fir_packets;
2555 sinfo.plis_rcvd = rtcp_received.pli_packets;
2556 sinfo.nacks_rcvd = rtcp_received.nack_packets;
2557 } else {
2558 sinfo.firs_rcvd = -1;
2559 sinfo.plis_rcvd = -1;
2560 sinfo.nacks_rcvd = -1;
2561 LOG_RTCERR1(GetRtcpPacketTypeCounters, channel_id);
2562 }
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00002563
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002564 // Get received RTCP statistics for the sender (reported by the remote
2565 // client in a RTCP packet), if available.
2566 // It's not a fatal error if we can't, since RTCP may not have arrived
2567 // yet.
2568 webrtc::RtcpStatistics outgoing_stream_rtcp_stats;
2569 int outgoing_stream_rtt_ms;
2570
2571 if (engine_->vie()->rtp()->GetSendChannelRtcpStatistics(
2572 channel_id,
2573 outgoing_stream_rtcp_stats,
2574 outgoing_stream_rtt_ms) == 0) {
2575 // Convert Q8 to float.
2576 sinfo.packets_lost = outgoing_stream_rtcp_stats.cumulative_lost;
2577 sinfo.fraction_lost = static_cast<float>(
2578 outgoing_stream_rtcp_stats.fraction_lost) / (1 << 8);
2579 sinfo.rtt_ms = outgoing_stream_rtt_ms;
2580 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002581 info->senders.push_back(sinfo);
2582
2583 unsigned int channel_total_bitrate_sent = 0;
2584 unsigned int channel_video_bitrate_sent = 0;
2585 unsigned int channel_fec_bitrate_sent = 0;
2586 unsigned int channel_nack_bitrate_sent = 0;
2587 if (engine_->vie()->rtp()->GetBandwidthUsage(
2588 channel_id, channel_total_bitrate_sent, channel_video_bitrate_sent,
2589 channel_fec_bitrate_sent, channel_nack_bitrate_sent) == 0) {
2590 total_bitrate_sent += channel_total_bitrate_sent;
2591 video_bitrate_sent += channel_video_bitrate_sent;
2592 fec_bitrate_sent += channel_fec_bitrate_sent;
2593 nack_bitrate_sent += channel_nack_bitrate_sent;
2594 } else {
2595 LOG_RTCERR1(GetBandwidthUsage, channel_id);
2596 }
2597
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002598 unsigned int target_enc_stream_bitrate = 0;
2599 if (engine_->vie()->codec()->GetCodecTargetBitrate(
2600 channel_id, &target_enc_stream_bitrate) == 0) {
2601 target_enc_bitrate += target_enc_stream_bitrate;
2602 } else {
2603 LOG_RTCERR1(GetCodecTargetBitrate, channel_id);
2604 }
2605 }
buildbot@webrtc.orga18b4c92014-05-06 17:48:14 +00002606 if (!send_channels_.empty()) {
2607 // GetEstimatedSendBandwidth returns the estimated bandwidth for all video
2608 // engine channels in a channel group. Any valid channel id will do as it
2609 // is only used to access the right group of channels.
2610 const int channel_id = send_channels_.begin()->second->channel_id();
2611 // Get the send bandwidth available for this MediaChannel.
2612 if (engine_->vie()->rtp()->GetEstimatedSendBandwidth(
2613 channel_id, &estimated_send_bandwidth) != 0) {
2614 LOG_RTCERR1(GetEstimatedSendBandwidth, channel_id);
2615 }
2616 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002617 } else {
2618 LOG(LS_WARNING) << "GetStats: sender information not ready.";
2619 }
2620
2621 // Get the SSRC and stats for each receiver, based on our own calculations.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002622 for (RecvChannelMap::const_iterator it = recv_channels_.begin();
2623 it != recv_channels_.end(); ++it) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002624 WebRtcVideoChannelRecvInfo* channel = it->second;
2625
buildbot@webrtc.orgeaf2bd92014-05-12 23:12:19 +00002626 unsigned int ssrc = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002627 // Get receiver statistics and build VideoReceiverInfo, if we have data.
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +00002628 // Skip the default channel (ssrc == 0).
2629 if (engine_->vie()->rtp()->GetRemoteSSRC(
2630 channel->channel_id(), ssrc) != 0 ||
2631 ssrc == 0)
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002632 continue;
2633
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002634 webrtc::StreamDataCounters sent;
2635 webrtc::StreamDataCounters received;
2636 if (engine_->vie()->rtp()->GetRtpStatistics(channel->channel_id(),
2637 sent, received) != 0) {
2638 LOG_RTCERR1(GetRTPStatistics, channel->channel_id());
2639 return false;
2640 }
2641 VideoReceiverInfo rinfo;
2642 rinfo.add_ssrc(ssrc);
2643 rinfo.bytes_rcvd = received.bytes;
2644 rinfo.packets_rcvd = received.packets;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002645 rinfo.packets_lost = -1;
2646 rinfo.packets_concealed = -1;
2647 rinfo.fraction_lost = -1; // from SentRTCP
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002648 rinfo.frame_width = channel->render_adapter()->width();
2649 rinfo.frame_height = channel->render_adapter()->height();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002650 int fps = channel->render_adapter()->framerate();
2651 rinfo.framerate_decoded = fps;
2652 rinfo.framerate_output = fps;
buildbot@webrtc.org0581f0b2014-05-06 21:36:31 +00002653 rinfo.capture_start_ntp_time_ms =
2654 channel->render_adapter()->capture_start_ntp_time_ms();
wu@webrtc.org97077a32013-10-25 21:18:33 +00002655 channel->decoder_observer()->ExportTo(&rinfo);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002656
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00002657 webrtc::RtcpPacketTypeCounter rtcp_sent;
2658 webrtc::RtcpPacketTypeCounter rtcp_received;
2659 if (engine()->vie()->rtp()->GetRtcpPacketTypeCounters(
2660 channel->channel_id(), &rtcp_sent, &rtcp_received) == 0) {
2661 rinfo.firs_sent = rtcp_sent.fir_packets;
2662 rinfo.plis_sent = rtcp_sent.pli_packets;
2663 rinfo.nacks_sent = rtcp_sent.nack_packets;
2664 } else {
2665 rinfo.firs_sent = -1;
2666 rinfo.plis_sent = -1;
2667 rinfo.nacks_sent = -1;
2668 LOG_RTCERR1(GetRtcpPacketTypeCounters, channel->channel_id());
2669 }
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00002670
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002671 // Get our locally created statistics of the received RTP stream.
2672 webrtc::RtcpStatistics incoming_stream_rtcp_stats;
2673 int incoming_stream_rtt_ms;
2674 if (engine_->vie()->rtp()->GetReceiveChannelRtcpStatistics(
2675 channel->channel_id(),
2676 incoming_stream_rtcp_stats,
2677 incoming_stream_rtt_ms) == 0) {
2678 // Convert Q8 to float.
2679 rinfo.packets_lost = incoming_stream_rtcp_stats.cumulative_lost;
2680 rinfo.fraction_lost = static_cast<float>(
2681 incoming_stream_rtcp_stats.fraction_lost) / (1 << 8);
2682 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002683 info->receivers.push_back(rinfo);
buildbot@webrtc.orga18b4c92014-05-06 17:48:14 +00002684 }
2685 unsigned int estimated_recv_bandwidth = 0;
2686 if (!recv_channels_.empty()) {
2687 // GetEstimatedReceiveBandwidth returns the estimated bandwidth for all
2688 // video engine channels in a channel group. Any valid channel id will do as
2689 // it is only used to access the right group of channels.
2690 const int channel_id = recv_channels_.begin()->second->channel_id();
2691 // Gets the estimated receive bandwidth for the MediaChannel.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002692 if (engine_->vie()->rtp()->GetEstimatedReceiveBandwidth(
buildbot@webrtc.orga18b4c92014-05-06 17:48:14 +00002693 channel_id, &estimated_recv_bandwidth) != 0) {
2694 LOG_RTCERR1(GetEstimatedReceiveBandwidth, channel_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002695 }
2696 }
buildbot@webrtc.orga18b4c92014-05-06 17:48:14 +00002697
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002698 // Build BandwidthEstimationInfo.
2699 // TODO(zhurunz): Add real unittest for this.
2700 BandwidthEstimationInfo bwe;
2701
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00002702 // TODO(jiayl): remove the condition when the necessary changes are available
2703 // outside the dev branch.
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00002704 if (options.include_received_propagation_stats) {
2705 webrtc::ReceiveBandwidthEstimatorStats additional_stats;
2706 // Only call for the default channel because the returned stats are
2707 // collected for all the channels using the same estimator.
2708 if (engine_->vie()->rtp()->GetReceiveBandwidthEstimatorStats(
mallinath@webrtc.org92fdfeb2014-02-17 18:49:41 +00002709 recv_channels_[0]->channel_id(), &additional_stats) == 0) {
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00002710 bwe.total_received_propagation_delta_ms =
2711 additional_stats.total_propagation_time_delta_ms;
2712 bwe.recent_received_propagation_delta_ms.swap(
2713 additional_stats.recent_propagation_time_delta_ms);
2714 bwe.recent_received_packet_group_arrival_time_ms.swap(
2715 additional_stats.recent_arrival_time_ms);
2716 }
2717 }
henrike@webrtc.orgb8395eb2014-02-28 21:57:22 +00002718
2719 engine_->vie()->rtp()->GetPacerQueuingDelayMs(
2720 recv_channels_[0]->channel_id(), &bwe.bucket_delay);
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00002721
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002722 // Calculations done above per send/receive stream.
2723 bwe.actual_enc_bitrate = video_bitrate_sent;
2724 bwe.transmit_bitrate = total_bitrate_sent;
2725 bwe.retransmit_bitrate = nack_bitrate_sent;
2726 bwe.available_send_bandwidth = estimated_send_bandwidth;
2727 bwe.available_recv_bandwidth = estimated_recv_bandwidth;
2728 bwe.target_enc_bitrate = target_enc_bitrate;
2729
2730 info->bw_estimations.push_back(bwe);
2731
2732 return true;
2733}
2734
2735bool WebRtcVideoMediaChannel::SetCapturer(uint32 ssrc,
2736 VideoCapturer* capturer) {
2737 ASSERT(ssrc != 0);
2738 if (!capturer) {
2739 return RemoveCapturer(ssrc);
2740 }
2741 WebRtcVideoChannelSendInfo* send_channel = GetSendChannel(ssrc);
2742 if (!send_channel) {
2743 return false;
2744 }
2745 VideoCapturer* old_capturer = send_channel->video_capturer();
wu@webrtc.org24301a62013-12-13 19:17:43 +00002746 MaybeDisconnectCapturer(old_capturer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002747
henrike@webrtc.orgc7bec842014-03-12 19:53:43 +00002748 send_channel->set_video_capturer(capturer, engine()->vie());
wu@webrtc.orga8910d22014-01-23 22:12:45 +00002749 MaybeConnectCapturer(capturer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002750 if (!capturer->IsScreencast() && ratio_w_ != 0 && ratio_h_ != 0) {
2751 capturer->UpdateAspectRatio(ratio_w_, ratio_h_);
2752 }
2753 const int64 timestamp = send_channel->local_stream_info()->time_stamp();
2754 if (send_codec_) {
2755 QueueBlackFrame(ssrc, timestamp, send_codec_->maxFramerate);
2756 }
2757 return true;
2758}
2759
2760bool WebRtcVideoMediaChannel::RequestIntraFrame() {
2761 // There is no API exposed to application to request a key frame
2762 // ViE does this internally when there are errors from decoder
2763 return false;
2764}
2765
wu@webrtc.orga9890802013-12-13 00:21:03 +00002766void WebRtcVideoMediaChannel::OnPacketReceived(
2767 talk_base::Buffer* packet, const talk_base::PacketTime& packet_time) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002768 // Pick which channel to send this packet to. If this packet doesn't match
2769 // any multiplexed streams, just send it to the default channel. Otherwise,
2770 // send it to the specific decoder instance for that stream.
2771 uint32 ssrc = 0;
2772 if (!GetRtpSsrc(packet->data(), packet->length(), &ssrc))
2773 return;
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00002774 int processing_channel = GetRecvChannelNum(ssrc);
2775 if (processing_channel == -1) {
2776 // Allocate an unsignalled recv channel for processing in conference mode.
henrike@webrtc.org18e59112014-03-14 17:19:38 +00002777 if (!InConferenceMode()) {
buildbot@webrtc.orgdd4742a2014-05-07 14:50:35 +00002778 // If we can't find or allocate one, use the default.
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00002779 processing_channel = video_channel();
henrike@webrtc.org18e59112014-03-14 17:19:38 +00002780 } else if (!CreateUnsignalledRecvChannel(ssrc, &processing_channel)) {
buildbot@webrtc.orgdd4742a2014-05-07 14:50:35 +00002781 // If we can't create an unsignalled recv channel, drop the packet in
henrike@webrtc.org18e59112014-03-14 17:19:38 +00002782 // conference mode.
2783 return;
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00002784 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002785 }
2786
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00002787 engine()->vie()->network()->ReceivedRTPPacket(
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00002788 processing_channel,
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00002789 packet->data(),
wu@webrtc.orga9890802013-12-13 00:21:03 +00002790 static_cast<int>(packet->length()),
2791 webrtc::PacketTime(packet_time.timestamp, packet_time.not_before));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002792}
2793
wu@webrtc.orga9890802013-12-13 00:21:03 +00002794void WebRtcVideoMediaChannel::OnRtcpReceived(
2795 talk_base::Buffer* packet, const talk_base::PacketTime& packet_time) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002796// Sending channels need all RTCP packets with feedback information.
2797// Even sender reports can contain attached report blocks.
2798// Receiving channels need sender reports in order to create
2799// correct receiver reports.
2800
2801 uint32 ssrc = 0;
2802 if (!GetRtcpSsrc(packet->data(), packet->length(), &ssrc)) {
2803 LOG(LS_WARNING) << "Failed to parse SSRC from received RTCP packet";
2804 return;
2805 }
2806 int type = 0;
2807 if (!GetRtcpType(packet->data(), packet->length(), &type)) {
2808 LOG(LS_WARNING) << "Failed to parse type from received RTCP packet";
2809 return;
2810 }
2811
2812 // If it is a sender report, find the channel that is listening.
2813 if (type == kRtcpTypeSR) {
2814 int which_channel = GetRecvChannelNum(ssrc);
2815 if (which_channel != -1 && !IsDefaultChannel(which_channel)) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00002816 engine_->vie()->network()->ReceivedRTCPPacket(
2817 which_channel,
2818 packet->data(),
2819 static_cast<int>(packet->length()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002820 }
2821 }
2822 // SR may continue RR and any RR entry may correspond to any one of the send
2823 // channels. So all RTCP packets must be forwarded all send channels. ViE
2824 // will filter out RR internally.
2825 for (SendChannelMap::iterator iter = send_channels_.begin();
2826 iter != send_channels_.end(); ++iter) {
2827 WebRtcVideoChannelSendInfo* send_channel = iter->second;
2828 int channel_id = send_channel->channel_id();
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00002829 engine_->vie()->network()->ReceivedRTCPPacket(
2830 channel_id,
2831 packet->data(),
2832 static_cast<int>(packet->length()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002833 }
2834}
2835
2836void WebRtcVideoMediaChannel::OnReadyToSend(bool ready) {
2837 SetNetworkTransmissionState(ready);
2838}
2839
2840bool WebRtcVideoMediaChannel::MuteStream(uint32 ssrc, bool muted) {
2841 WebRtcVideoChannelSendInfo* send_channel = GetSendChannel(ssrc);
2842 if (!send_channel) {
2843 LOG(LS_ERROR) << "The specified ssrc " << ssrc << " is not in use.";
2844 return false;
2845 }
2846 send_channel->set_muted(muted);
2847 return true;
2848}
2849
2850bool WebRtcVideoMediaChannel::SetRecvRtpHeaderExtensions(
2851 const std::vector<RtpHeaderExtension>& extensions) {
2852 if (receive_extensions_ == extensions) {
2853 return true;
2854 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002855
2856 const RtpHeaderExtension* offset_extension =
2857 FindHeaderExtension(extensions, kRtpTimestampOffsetHeaderExtension);
2858 const RtpHeaderExtension* send_time_extension =
henrike@webrtc.org79047f92014-03-06 23:46:59 +00002859 FindHeaderExtension(extensions, kRtpAbsoluteSenderTimeHeaderExtension);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002860
2861 // Loop through all receive channels and enable/disable the extensions.
2862 for (RecvChannelMap::iterator channel_it = recv_channels_.begin();
2863 channel_it != recv_channels_.end(); ++channel_it) {
2864 int channel_id = channel_it->second->channel_id();
2865 if (!SetHeaderExtension(
2866 &webrtc::ViERTP_RTCP::SetReceiveTimestampOffsetStatus, channel_id,
2867 offset_extension)) {
2868 return false;
2869 }
2870 if (!SetHeaderExtension(
2871 &webrtc::ViERTP_RTCP::SetReceiveAbsoluteSendTimeStatus, channel_id,
2872 send_time_extension)) {
2873 return false;
2874 }
2875 }
buildbot@webrtc.org150835e2014-05-06 15:54:38 +00002876
2877 receive_extensions_ = extensions;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002878 return true;
2879}
2880
2881bool WebRtcVideoMediaChannel::SetSendRtpHeaderExtensions(
2882 const std::vector<RtpHeaderExtension>& extensions) {
buildbot@webrtc.org150835e2014-05-06 15:54:38 +00002883 if (send_extensions_ == extensions) {
2884 return true;
2885 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002886
2887 const RtpHeaderExtension* offset_extension =
2888 FindHeaderExtension(extensions, kRtpTimestampOffsetHeaderExtension);
2889 const RtpHeaderExtension* send_time_extension =
henrike@webrtc.org79047f92014-03-06 23:46:59 +00002890 FindHeaderExtension(extensions, kRtpAbsoluteSenderTimeHeaderExtension);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002891
2892 // Loop through all send channels and enable/disable the extensions.
2893 for (SendChannelMap::iterator channel_it = send_channels_.begin();
2894 channel_it != send_channels_.end(); ++channel_it) {
2895 int channel_id = channel_it->second->channel_id();
2896 if (!SetHeaderExtension(
2897 &webrtc::ViERTP_RTCP::SetSendTimestampOffsetStatus, channel_id,
2898 offset_extension)) {
2899 return false;
2900 }
2901 if (!SetHeaderExtension(
2902 &webrtc::ViERTP_RTCP::SetSendAbsoluteSendTimeStatus, channel_id,
2903 send_time_extension)) {
2904 return false;
2905 }
2906 }
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00002907
2908 if (send_time_extension) {
2909 // For video RTP packets, we would like to update AbsoluteSendTimeHeader
2910 // Extension closer to the network, @ socket level before sending.
2911 // Pushing the extension id to socket layer.
2912 MediaChannel::SetOption(NetworkInterface::ST_RTP,
2913 talk_base::Socket::OPT_RTP_SENDTIME_EXTN_ID,
2914 send_time_extension->id);
2915 }
buildbot@webrtc.org150835e2014-05-06 15:54:38 +00002916
2917 send_extensions_ = extensions;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002918 return true;
2919}
2920
mallinath@webrtc.org92fdfeb2014-02-17 18:49:41 +00002921int WebRtcVideoMediaChannel::GetRtpSendTimeExtnId() const {
2922 const RtpHeaderExtension* send_time_extension = FindHeaderExtension(
henrike@webrtc.org79047f92014-03-06 23:46:59 +00002923 send_extensions_, kRtpAbsoluteSenderTimeHeaderExtension);
mallinath@webrtc.org92fdfeb2014-02-17 18:49:41 +00002924 if (send_time_extension) {
2925 return send_time_extension->id;
2926 }
2927 return -1;
2928}
2929
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002930bool WebRtcVideoMediaChannel::SetStartSendBandwidth(int bps) {
2931 LOG(LS_INFO) << "WebRtcVideoMediaChannel::SetStartSendBandwidth";
2932
2933 if (!send_codec_) {
2934 LOG(LS_INFO) << "The send codec has not been set up yet";
2935 return true;
2936 }
2937
2938 // On success, SetSendCodec() will reset |send_start_bitrate_| to |bps/1000|,
buildbot@webrtc.orgd1ae89f2014-05-08 19:19:26 +00002939 // by calling MaybeChangeBitrates. That method will also clamp the
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002940 // start bitrate between min and max, consistent with the override behavior
2941 // in SetMaxSendBandwidth.
buildbot@webrtc.orgd1ae89f2014-05-08 19:19:26 +00002942 webrtc::VideoCodec new_codec = *send_codec_;
2943 if (BitrateIsSet(bps)) {
2944 new_codec.startBitrate = bps / 1000;
2945 }
2946 return SetSendCodec(new_codec);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002947}
2948
2949bool WebRtcVideoMediaChannel::SetMaxSendBandwidth(int bps) {
2950 LOG(LS_INFO) << "WebRtcVideoMediaChannel::SetMaxSendBandwidth";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002951
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002952 if (!send_codec_) {
2953 LOG(LS_INFO) << "The send codec has not been set up yet";
2954 return true;
2955 }
2956
buildbot@webrtc.orgd1ae89f2014-05-08 19:19:26 +00002957 webrtc::VideoCodec new_codec = *send_codec_;
2958 if (BitrateIsSet(bps)) {
2959 new_codec.maxBitrate = bps / 1000;
2960 }
2961 if (!SetSendCodec(new_codec)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002962 return false;
2963 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002964 LogSendCodecChange("SetMaxSendBandwidth()");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002965
2966 return true;
2967}
2968
2969bool WebRtcVideoMediaChannel::SetOptions(const VideoOptions &options) {
2970 // Always accept options that are unchanged.
2971 if (options_ == options) {
2972 return true;
2973 }
2974
2975 // Trigger SetSendCodec to set correct noise reduction state if the option has
2976 // changed.
2977 bool denoiser_changed = options.video_noise_reduction.IsSet() &&
2978 (options_.video_noise_reduction != options.video_noise_reduction);
2979
2980 bool leaky_bucket_changed = options.video_leaky_bucket.IsSet() &&
2981 (options_.video_leaky_bucket != options.video_leaky_bucket);
2982
2983 bool buffer_latency_changed = options.buffered_mode_latency.IsSet() &&
2984 (options_.buffered_mode_latency != options.buffered_mode_latency);
2985
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00002986 bool cpu_overuse_detection_changed = options.cpu_overuse_detection.IsSet() &&
2987 (options_.cpu_overuse_detection != options.cpu_overuse_detection);
2988
wu@webrtc.orgde305012013-10-31 15:40:38 +00002989 bool dscp_option_changed = (options_.dscp != options.dscp);
2990
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002991 bool suspend_below_min_bitrate_changed =
2992 options.suspend_below_min_bitrate.IsSet() &&
2993 (options_.suspend_below_min_bitrate != options.suspend_below_min_bitrate);
2994
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002995 bool conference_mode_turned_off = false;
2996 if (options_.conference_mode.IsSet() && options.conference_mode.IsSet() &&
2997 options_.conference_mode.GetWithDefaultIfUnset(false) &&
2998 !options.conference_mode.GetWithDefaultIfUnset(false)) {
2999 conference_mode_turned_off = true;
3000 }
3001
henrike@webrtc.orgdce3feb2014-03-26 01:17:30 +00003002 bool improved_wifi_bwe_changed =
3003 options.use_improved_wifi_bandwidth_estimator.IsSet() &&
3004 options_.use_improved_wifi_bandwidth_estimator !=
3005 options.use_improved_wifi_bandwidth_estimator;
3006
henrike@webrtc.org10bd88e2014-03-11 21:07:25 +00003007
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003008 // Save the options, to be interpreted where appropriate.
3009 // Use options_.SetAll() instead of assignment so that unset value in options
3010 // will not overwrite the previous option value.
3011 options_.SetAll(options);
3012
3013 // Set CPU options for all send channels.
3014 for (SendChannelMap::iterator iter = send_channels_.begin();
3015 iter != send_channels_.end(); ++iter) {
3016 WebRtcVideoChannelSendInfo* send_channel = iter->second;
3017 send_channel->ApplyCpuOptions(options_);
3018 }
3019
buildbot@webrtc.orgd1ae89f2014-05-08 19:19:26 +00003020 if (send_codec_) {
3021 bool reset_send_codec_needed = denoiser_changed;
3022 webrtc::VideoCodec new_codec = *send_codec_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003023
buildbot@webrtc.orgd1ae89f2014-05-08 19:19:26 +00003024 // TODO(pthatcher): Remove this. We don't need 4 ways to set bitrates.
3025 bool lower_min_bitrate;
3026 if (options.lower_min_bitrate.Get(&lower_min_bitrate)) {
3027 new_codec.minBitrate = kLowerMinBitrate;
3028 reset_send_codec_needed = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003029 }
buildbot@webrtc.orgd1ae89f2014-05-08 19:19:26 +00003030
3031 if (conference_mode_turned_off) {
3032 // This is a special case for turning conference mode off.
3033 // Max bitrate should go back to the default maximum value instead
3034 // of the current maximum.
3035 new_codec.maxBitrate = kAutoBandwidth;
3036 reset_send_codec_needed = true;
3037 }
3038
3039 // TODO(pthatcher): Remove this. We don't need 4 ways to set bitrates.
3040 int new_start_bitrate;
3041 if (options.video_start_bitrate.Get(&new_start_bitrate)) {
3042 new_codec.startBitrate = new_start_bitrate;
3043 reset_send_codec_needed = true;
3044 }
3045
3046
3047 LOG(LS_INFO) << "Reset send codec needed is enabled? "
3048 << reset_send_codec_needed;
3049 if (reset_send_codec_needed) {
3050 if (!SetSendCodec(new_codec)) {
3051 return false;
3052 }
3053 LogSendCodecChange("SetOptions()");
3054 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003055 }
henrike@webrtc.org10bd88e2014-03-11 21:07:25 +00003056
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003057 if (leaky_bucket_changed) {
3058 bool enable_leaky_bucket =
3059 options_.video_leaky_bucket.GetWithDefaultIfUnset(false);
buildbot@webrtc.orgf875f152014-04-14 16:06:21 +00003060 LOG(LS_INFO) << "Leaky bucket is enabled? " << enable_leaky_bucket;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003061 for (SendChannelMap::iterator it = send_channels_.begin();
3062 it != send_channels_.end(); ++it) {
3063 if (engine()->vie()->rtp()->SetTransmissionSmoothingStatus(
3064 it->second->channel_id(), enable_leaky_bucket) != 0) {
3065 LOG_RTCERR2(SetTransmissionSmoothingStatus, it->second->channel_id(),
3066 enable_leaky_bucket);
3067 }
3068 }
3069 }
3070 if (buffer_latency_changed) {
3071 int buffer_latency =
3072 options_.buffered_mode_latency.GetWithDefaultIfUnset(
3073 cricket::kBufferedModeDisabled);
buildbot@webrtc.orgf875f152014-04-14 16:06:21 +00003074 LOG(LS_INFO) << "Buffer latency is " << buffer_latency;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003075 for (SendChannelMap::iterator it = send_channels_.begin();
3076 it != send_channels_.end(); ++it) {
3077 if (engine()->vie()->rtp()->SetSenderBufferingMode(
3078 it->second->channel_id(), buffer_latency) != 0) {
3079 LOG_RTCERR2(SetSenderBufferingMode, it->second->channel_id(),
3080 buffer_latency);
3081 }
3082 }
3083 for (RecvChannelMap::iterator it = recv_channels_.begin();
3084 it != recv_channels_.end(); ++it) {
3085 if (engine()->vie()->rtp()->SetReceiverBufferingMode(
3086 it->second->channel_id(), buffer_latency) != 0) {
3087 LOG_RTCERR2(SetReceiverBufferingMode, it->second->channel_id(),
3088 buffer_latency);
3089 }
3090 }
3091 }
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00003092 if (cpu_overuse_detection_changed) {
3093 bool cpu_overuse_detection =
3094 options_.cpu_overuse_detection.GetWithDefaultIfUnset(false);
buildbot@webrtc.orgf875f152014-04-14 16:06:21 +00003095 LOG(LS_INFO) << "CPU overuse detection is enabled? "
3096 << cpu_overuse_detection;
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00003097 for (SendChannelMap::iterator iter = send_channels_.begin();
3098 iter != send_channels_.end(); ++iter) {
3099 WebRtcVideoChannelSendInfo* send_channel = iter->second;
3100 send_channel->SetCpuOveruseDetection(cpu_overuse_detection);
3101 }
3102 }
wu@webrtc.orgde305012013-10-31 15:40:38 +00003103 if (dscp_option_changed) {
3104 talk_base::DiffServCodePoint dscp = talk_base::DSCP_DEFAULT;
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00003105 if (options_.dscp.GetWithDefaultIfUnset(false))
wu@webrtc.orgde305012013-10-31 15:40:38 +00003106 dscp = kVideoDscpValue;
buildbot@webrtc.orgf875f152014-04-14 16:06:21 +00003107 LOG(LS_INFO) << "DSCP is " << dscp;
wu@webrtc.orgde305012013-10-31 15:40:38 +00003108 if (MediaChannel::SetDscp(dscp) != 0) {
3109 LOG(LS_WARNING) << "Failed to set DSCP settings for video channel";
3110 }
3111 }
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00003112 if (suspend_below_min_bitrate_changed) {
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00003113 if (options_.suspend_below_min_bitrate.GetWithDefaultIfUnset(false)) {
buildbot@webrtc.orgf875f152014-04-14 16:06:21 +00003114 LOG(LS_INFO) << "Suspend below min bitrate enabled.";
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00003115 for (SendChannelMap::iterator it = send_channels_.begin();
3116 it != send_channels_.end(); ++it) {
3117 engine()->vie()->codec()->SuspendBelowMinBitrate(
3118 it->second->channel_id());
3119 }
3120 } else {
3121 LOG(LS_WARNING) << "Cannot disable video suspension once it is enabled";
3122 }
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00003123 }
henrike@webrtc.orgdce3feb2014-03-26 01:17:30 +00003124 if (improved_wifi_bwe_changed) {
buildbot@webrtc.orgf875f152014-04-14 16:06:21 +00003125 LOG(LS_INFO) << "Improved WIFI BWE called.";
henrike@webrtc.orgdce3feb2014-03-26 01:17:30 +00003126 webrtc::Config config;
3127 config.Set(new webrtc::AimdRemoteRateControl(
3128 options_.use_improved_wifi_bandwidth_estimator
3129 .GetWithDefaultIfUnset(false)));
3130 for (SendChannelMap::iterator it = send_channels_.begin();
3131 it != send_channels_.end(); ++it) {
3132 engine()->vie()->network()->SetBandwidthEstimationConfig(
3133 it->second->channel_id(), config);
3134 }
3135 }
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +00003136 webrtc::CpuOveruseOptions overuse_options;
3137 if (GetCpuOveruseOptions(options_, &overuse_options)) {
3138 for (SendChannelMap::iterator it = send_channels_.begin();
3139 it != send_channels_.end(); ++it) {
3140 if (engine()->vie()->base()->SetCpuOveruseOptions(
3141 it->second->channel_id(), overuse_options) != 0) {
3142 LOG_RTCERR1(SetCpuOveruseOptions, it->second->channel_id());
3143 }
3144 }
3145 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003146 return true;
3147}
3148
3149void WebRtcVideoMediaChannel::SetInterface(NetworkInterface* iface) {
3150 MediaChannel::SetInterface(iface);
3151 // Set the RTP recv/send buffer to a bigger size
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00003152 MediaChannel::SetOption(NetworkInterface::ST_RTP,
3153 talk_base::Socket::OPT_RCVBUF,
3154 kVideoRtpBufferSize);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003155
3156 // TODO(sriniv): Remove or re-enable this.
3157 // As part of b/8030474, send-buffer is size now controlled through
3158 // portallocator flags.
3159 // network_interface_->SetOption(NetworkInterface::ST_RTP,
3160 // talk_base::Socket::OPT_SNDBUF,
3161 // kVideoRtpBufferSize);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003162}
3163
3164void WebRtcVideoMediaChannel::UpdateAspectRatio(int ratio_w, int ratio_h) {
3165 ASSERT(ratio_w != 0);
3166 ASSERT(ratio_h != 0);
3167 ratio_w_ = ratio_w;
3168 ratio_h_ = ratio_h;
3169 // For now assume that all streams want the same aspect ratio.
3170 // TODO(hellner): remove the need for this assumption.
3171 for (SendChannelMap::iterator iter = send_channels_.begin();
3172 iter != send_channels_.end(); ++iter) {
3173 WebRtcVideoChannelSendInfo* send_channel = iter->second;
3174 VideoCapturer* capturer = send_channel->video_capturer();
3175 if (capturer) {
3176 capturer->UpdateAspectRatio(ratio_w, ratio_h);
3177 }
3178 }
3179}
3180
3181bool WebRtcVideoMediaChannel::GetRenderer(uint32 ssrc,
3182 VideoRenderer** renderer) {
3183 RecvChannelMap::const_iterator it = recv_channels_.find(ssrc);
3184 if (it == recv_channels_.end()) {
3185 if (first_receive_ssrc_ == ssrc &&
3186 recv_channels_.find(0) != recv_channels_.end()) {
3187 LOG(LS_INFO) << " GetRenderer " << ssrc
3188 << " reuse default renderer #"
3189 << vie_channel_;
3190 *renderer = recv_channels_[0]->render_adapter()->renderer();
3191 return true;
3192 }
3193 return false;
3194 }
3195
3196 *renderer = it->second->render_adapter()->renderer();
3197 return true;
3198}
3199
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00003200bool WebRtcVideoMediaChannel::GetVideoAdapter(
3201 uint32 ssrc, CoordinatedVideoAdapter** video_adapter) {
3202 SendChannelMap::iterator it = send_channels_.find(ssrc);
3203 if (it == send_channels_.end()) {
3204 return false;
3205 }
3206 *video_adapter = it->second->video_adapter();
3207 return true;
3208}
3209
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003210void WebRtcVideoMediaChannel::SendFrame(VideoCapturer* capturer,
3211 const VideoFrame* frame) {
wu@webrtc.org24301a62013-12-13 19:17:43 +00003212 // If the |capturer| is registered to any send channel, then send the frame
3213 // to those send channels.
3214 bool capturer_is_channel_owned = false;
3215 for (SendChannelMap::iterator iter = send_channels_.begin();
3216 iter != send_channels_.end(); ++iter) {
3217 WebRtcVideoChannelSendInfo* send_channel = iter->second;
3218 if (send_channel->video_capturer() == capturer) {
3219 SendFrame(send_channel, frame, capturer->IsScreencast());
3220 capturer_is_channel_owned = true;
3221 }
3222 }
3223 if (capturer_is_channel_owned) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003224 return;
3225 }
wu@webrtc.org24301a62013-12-13 19:17:43 +00003226
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003227 // TODO(hellner): Remove below for loop once the captured frame no longer
3228 // come from the engine, i.e. the engine no longer owns a capturer.
3229 for (SendChannelMap::iterator iter = send_channels_.begin();
3230 iter != send_channels_.end(); ++iter) {
3231 WebRtcVideoChannelSendInfo* send_channel = iter->second;
3232 if (send_channel->video_capturer() == NULL) {
3233 SendFrame(send_channel, frame, capturer->IsScreencast());
3234 }
3235 }
3236}
3237
3238bool WebRtcVideoMediaChannel::SendFrame(
3239 WebRtcVideoChannelSendInfo* send_channel,
3240 const VideoFrame* frame,
3241 bool is_screencast) {
3242 if (!send_channel) {
3243 return false;
3244 }
3245 if (!send_codec_) {
3246 // Send codec has not been set. No reason to process the frame any further.
3247 return false;
3248 }
3249 const VideoFormat& video_format = send_channel->video_format();
3250 // If the frame should be dropped.
3251 const bool video_format_set = video_format != cricket::VideoFormat();
3252 if (video_format_set &&
3253 (video_format.width == 0 && video_format.height == 0)) {
3254 return true;
3255 }
3256
3257 // Checks if we need to reset vie send codec.
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00003258 if (!MaybeResetVieSendCodec(send_channel,
3259 static_cast<int>(frame->GetWidth()),
3260 static_cast<int>(frame->GetHeight()),
3261 is_screencast, NULL)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003262 LOG(LS_ERROR) << "MaybeResetVieSendCodec failed with "
3263 << frame->GetWidth() << "x" << frame->GetHeight();
3264 return false;
3265 }
3266 const VideoFrame* frame_out = frame;
3267 talk_base::scoped_ptr<VideoFrame> processed_frame;
3268 // Disable muting for screencast.
3269 const bool mute = (send_channel->muted() && !is_screencast);
3270 send_channel->ProcessFrame(*frame_out, mute, processed_frame.use());
3271 if (processed_frame) {
3272 frame_out = processed_frame.get();
3273 }
3274
3275 webrtc::ViEVideoFrameI420 frame_i420;
3276 // TODO(ronghuawu): Update the webrtc::ViEVideoFrameI420
3277 // to use const unsigned char*
3278 frame_i420.y_plane = const_cast<unsigned char*>(frame_out->GetYPlane());
3279 frame_i420.u_plane = const_cast<unsigned char*>(frame_out->GetUPlane());
3280 frame_i420.v_plane = const_cast<unsigned char*>(frame_out->GetVPlane());
3281 frame_i420.y_pitch = frame_out->GetYPitch();
3282 frame_i420.u_pitch = frame_out->GetUPitch();
3283 frame_i420.v_pitch = frame_out->GetVPitch();
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00003284 frame_i420.width = static_cast<uint16>(frame_out->GetWidth());
3285 frame_i420.height = static_cast<uint16>(frame_out->GetHeight());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003286
3287 int64 timestamp_ntp_ms = 0;
3288 // TODO(justinlin): Reenable after Windows issues with clock drift are fixed.
3289 // Currently reverted to old behavior of discarding capture timestamp.
3290#if 0
henrike@webrtc.orgf5bebd42014-04-04 18:39:07 +00003291 static const int kTimestampDeltaInSecondsForWarning = 2;
3292
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003293 // If the frame timestamp is 0, we will use the deliver time.
3294 const int64 frame_timestamp = frame->GetTimeStamp();
3295 if (frame_timestamp != 0) {
3296 if (abs(time(NULL) - frame_timestamp / talk_base::kNumNanosecsPerSec) >
3297 kTimestampDeltaInSecondsForWarning) {
3298 LOG(LS_WARNING) << "Frame timestamp differs by more than "
3299 << kTimestampDeltaInSecondsForWarning << " seconds from "
3300 << "current Unix timestamp.";
3301 }
3302
3303 timestamp_ntp_ms =
3304 talk_base::UnixTimestampNanosecsToNtpMillisecs(frame_timestamp);
3305 }
3306#endif
3307
3308 return send_channel->external_capture()->IncomingFrameI420(
3309 frame_i420, timestamp_ntp_ms) == 0;
3310}
3311
3312bool WebRtcVideoMediaChannel::CreateChannel(uint32 ssrc_key,
3313 MediaDirection direction,
3314 int* channel_id) {
3315 // There are 3 types of channels. Sending only, receiving only and
3316 // sending and receiving. The sending and receiving channel is the
3317 // default channel and there is only one. All other channels that are created
3318 // are associated with the default channel which must exist. The default
3319 // channel id is stored in |vie_channel_|. All channels need to know about
3320 // the default channel to properly handle remb which is why there are
3321 // different ViE create channel calls.
3322 // For this channel the local and remote ssrc key is 0. However, it may
3323 // have a non-zero local and/or remote ssrc depending on if it is currently
3324 // sending and/or receiving.
3325 if ((vie_channel_ == -1 || direction == MD_SENDRECV) &&
3326 (!send_channels_.empty() || !recv_channels_.empty())) {
3327 ASSERT(false);
3328 return false;
3329 }
3330
3331 *channel_id = -1;
3332 if (direction == MD_RECV) {
3333 // All rec channels are associated with the default channel |vie_channel_|
3334 if (engine_->vie()->base()->CreateReceiveChannel(*channel_id,
3335 vie_channel_) != 0) {
3336 LOG_RTCERR2(CreateReceiveChannel, *channel_id, vie_channel_);
3337 return false;
3338 }
3339 } else if (direction == MD_SEND) {
3340 if (engine_->vie()->base()->CreateChannel(*channel_id,
3341 vie_channel_) != 0) {
3342 LOG_RTCERR2(CreateChannel, *channel_id, vie_channel_);
3343 return false;
3344 }
3345 } else {
3346 ASSERT(direction == MD_SENDRECV);
3347 if (engine_->vie()->base()->CreateChannel(*channel_id) != 0) {
3348 LOG_RTCERR1(CreateChannel, *channel_id);
3349 return false;
3350 }
3351 }
3352 if (!ConfigureChannel(*channel_id, direction, ssrc_key)) {
3353 engine_->vie()->base()->DeleteChannel(*channel_id);
3354 *channel_id = -1;
3355 return false;
3356 }
3357
3358 return true;
3359}
3360
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00003361bool WebRtcVideoMediaChannel::CreateUnsignalledRecvChannel(
3362 uint32 ssrc_key, int* out_channel_id) {
henrike@webrtc.org18e59112014-03-14 17:19:38 +00003363 int unsignalled_recv_channel_limit =
3364 options_.unsignalled_recv_stream_limit.GetWithDefaultIfUnset(
3365 kNumDefaultUnsignalledVideoRecvStreams);
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00003366 if (num_unsignalled_recv_channels_ >= unsignalled_recv_channel_limit) {
3367 return false;
3368 }
3369 if (!CreateChannel(ssrc_key, MD_RECV, out_channel_id)) {
3370 return false;
3371 }
3372 // TODO(tvsriram): Support dynamic sizing of unsignalled recv channels.
3373 num_unsignalled_recv_channels_++;
3374 return true;
3375}
3376
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003377bool WebRtcVideoMediaChannel::ConfigureChannel(int channel_id,
3378 MediaDirection direction,
3379 uint32 ssrc_key) {
3380 const bool receiving = (direction == MD_RECV) || (direction == MD_SENDRECV);
3381 const bool sending = (direction == MD_SEND) || (direction == MD_SENDRECV);
3382 // Register external transport.
3383 if (engine_->vie()->network()->RegisterSendTransport(
3384 channel_id, *this) != 0) {
3385 LOG_RTCERR1(RegisterSendTransport, channel_id);
3386 return false;
3387 }
3388
3389 // Set MTU.
3390 if (engine_->vie()->network()->SetMTU(channel_id, kVideoMtu) != 0) {
3391 LOG_RTCERR2(SetMTU, channel_id, kVideoMtu);
3392 return false;
3393 }
3394 // Turn on RTCP and loss feedback reporting.
3395 if (engine()->vie()->rtp()->SetRTCPStatus(
3396 channel_id, webrtc::kRtcpCompound_RFC4585) != 0) {
3397 LOG_RTCERR2(SetRTCPStatus, channel_id, webrtc::kRtcpCompound_RFC4585);
3398 return false;
3399 }
3400 // Enable pli as key frame request method.
3401 if (engine_->vie()->rtp()->SetKeyFrameRequestMethod(
3402 channel_id, webrtc::kViEKeyFrameRequestPliRtcp) != 0) {
3403 LOG_RTCERR2(SetKeyFrameRequestMethod,
3404 channel_id, webrtc::kViEKeyFrameRequestPliRtcp);
3405 return false;
3406 }
3407 if (!SetNackFec(channel_id, send_red_type_, send_fec_type_, nack_enabled_)) {
3408 // Logged in SetNackFec. Don't spam the logs.
3409 return false;
3410 }
3411 // Note that receiving must always be configured before sending to ensure
3412 // that send and receive channel is configured correctly (ConfigureReceiving
3413 // assumes no sending).
3414 if (receiving) {
3415 if (!ConfigureReceiving(channel_id, ssrc_key)) {
3416 return false;
3417 }
3418 }
3419 if (sending) {
3420 if (!ConfigureSending(channel_id, ssrc_key)) {
3421 return false;
3422 }
3423 }
3424
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00003425 // Start receiving for both receive and send channels so that we get incoming
3426 // RTP (if receiving) as well as RTCP feedback (if sending).
3427 if (engine()->vie()->base()->StartReceive(channel_id) != 0) {
3428 LOG_RTCERR1(StartReceive, channel_id);
3429 return false;
3430 }
3431
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003432 return true;
3433}
3434
3435bool WebRtcVideoMediaChannel::ConfigureReceiving(int channel_id,
3436 uint32 remote_ssrc_key) {
3437 // Make sure that an SSRC/key isn't registered more than once.
3438 if (recv_channels_.find(remote_ssrc_key) != recv_channels_.end()) {
3439 return false;
3440 }
3441 // Connect the voice channel, if there is one.
3442 // TODO(perkj): The A/V is synched by the receiving channel. So we need to
3443 // know the SSRC of the remote audio channel in order to fetch the correct
3444 // webrtc VoiceEngine channel. For now- only sync the default channel used
3445 // in 1-1 calls.
3446 if (remote_ssrc_key == 0 && voice_channel_) {
3447 WebRtcVoiceMediaChannel* voice_channel =
3448 static_cast<WebRtcVoiceMediaChannel*>(voice_channel_);
3449 if (engine_->vie()->base()->ConnectAudioChannel(
3450 vie_channel_, voice_channel->voe_channel()) != 0) {
3451 LOG_RTCERR2(ConnectAudioChannel, channel_id,
3452 voice_channel->voe_channel());
3453 LOG(LS_WARNING) << "A/V not synchronized";
3454 // Not a fatal error.
3455 }
3456 }
3457
3458 talk_base::scoped_ptr<WebRtcVideoChannelRecvInfo> channel_info(
3459 new WebRtcVideoChannelRecvInfo(channel_id));
3460
3461 // Install a render adapter.
3462 if (engine_->vie()->render()->AddRenderer(channel_id,
3463 webrtc::kVideoI420, channel_info->render_adapter()) != 0) {
3464 LOG_RTCERR3(AddRenderer, channel_id, webrtc::kVideoI420,
3465 channel_info->render_adapter());
3466 return false;
3467 }
3468
3469
3470 if (engine_->vie()->rtp()->SetRembStatus(channel_id,
3471 kNotSending,
3472 remb_enabled_) != 0) {
3473 LOG_RTCERR3(SetRembStatus, channel_id, kNotSending, remb_enabled_);
3474 return false;
3475 }
3476
3477 if (!SetHeaderExtension(&webrtc::ViERTP_RTCP::SetReceiveTimestampOffsetStatus,
3478 channel_id, receive_extensions_, kRtpTimestampOffsetHeaderExtension)) {
3479 return false;
3480 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003481 if (!SetHeaderExtension(
3482 &webrtc::ViERTP_RTCP::SetReceiveAbsoluteSendTimeStatus, channel_id,
henrike@webrtc.org79047f92014-03-06 23:46:59 +00003483 receive_extensions_, kRtpAbsoluteSenderTimeHeaderExtension)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003484 return false;
3485 }
3486
3487 if (remote_ssrc_key != 0) {
3488 // Use the same SSRC as our default channel
3489 // (so the RTCP reports are correct).
3490 unsigned int send_ssrc = 0;
3491 webrtc::ViERTP_RTCP* rtp = engine()->vie()->rtp();
3492 if (rtp->GetLocalSSRC(vie_channel_, send_ssrc) == -1) {
3493 LOG_RTCERR2(GetLocalSSRC, vie_channel_, send_ssrc);
3494 return false;
3495 }
3496 if (rtp->SetLocalSSRC(channel_id, send_ssrc) == -1) {
3497 LOG_RTCERR2(SetLocalSSRC, channel_id, send_ssrc);
3498 return false;
3499 }
3500 } // Else this is the the default channel and we don't change the SSRC.
3501
3502 // Disable color enhancement since it is a bit too aggressive.
3503 if (engine()->vie()->image()->EnableColorEnhancement(channel_id,
3504 false) != 0) {
3505 LOG_RTCERR1(EnableColorEnhancement, channel_id);
3506 return false;
3507 }
3508
3509 if (!SetReceiveCodecs(channel_info.get())) {
3510 return false;
3511 }
3512
3513 int buffer_latency =
3514 options_.buffered_mode_latency.GetWithDefaultIfUnset(
3515 cricket::kBufferedModeDisabled);
3516 if (buffer_latency != cricket::kBufferedModeDisabled) {
3517 if (engine()->vie()->rtp()->SetReceiverBufferingMode(
3518 channel_id, buffer_latency) != 0) {
3519 LOG_RTCERR2(SetReceiverBufferingMode, channel_id, buffer_latency);
3520 }
3521 }
3522
3523 if (render_started_) {
3524 if (engine_->vie()->render()->StartRender(channel_id) != 0) {
3525 LOG_RTCERR1(StartRender, channel_id);
3526 return false;
3527 }
3528 }
3529
3530 // Register decoder observer for incoming framerate and bitrate.
3531 if (engine()->vie()->codec()->RegisterDecoderObserver(
3532 channel_id, *channel_info->decoder_observer()) != 0) {
3533 LOG_RTCERR1(RegisterDecoderObserver, channel_info->decoder_observer());
3534 return false;
3535 }
3536
3537 recv_channels_[remote_ssrc_key] = channel_info.release();
3538 return true;
3539}
3540
3541bool WebRtcVideoMediaChannel::ConfigureSending(int channel_id,
3542 uint32 local_ssrc_key) {
3543 // The ssrc key can be zero or correspond to an SSRC.
3544 // Make sure the default channel isn't configured more than once.
3545 if (local_ssrc_key == 0 && send_channels_.find(0) != send_channels_.end()) {
3546 return false;
3547 }
3548 // Make sure that the SSRC is not already in use.
3549 uint32 dummy_key;
3550 if (GetSendChannelKey(local_ssrc_key, &dummy_key)) {
3551 return false;
3552 }
3553 int vie_capture = 0;
3554 webrtc::ViEExternalCapture* external_capture = NULL;
3555 // Register external capture.
3556 if (engine()->vie()->capture()->AllocateExternalCaptureDevice(
3557 vie_capture, external_capture) != 0) {
3558 LOG_RTCERR0(AllocateExternalCaptureDevice);
3559 return false;
3560 }
3561
3562 // Connect external capture.
3563 if (engine()->vie()->capture()->ConnectCaptureDevice(
3564 vie_capture, channel_id) != 0) {
3565 LOG_RTCERR2(ConnectCaptureDevice, vie_capture, channel_id);
3566 return false;
3567 }
3568 talk_base::scoped_ptr<WebRtcVideoChannelSendInfo> send_channel(
3569 new WebRtcVideoChannelSendInfo(channel_id, vie_capture,
3570 external_capture,
3571 engine()->cpu_monitor()));
3572 send_channel->ApplyCpuOptions(options_);
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00003573 send_channel->SignalCpuAdaptationUnable.connect(this,
3574 &WebRtcVideoMediaChannel::OnCpuAdaptationUnable);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003575
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00003576 if (options_.cpu_overuse_detection.GetWithDefaultIfUnset(false)) {
3577 send_channel->SetCpuOveruseDetection(true);
3578 }
3579
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +00003580 webrtc::CpuOveruseOptions overuse_options;
3581 if (GetCpuOveruseOptions(options_, &overuse_options)) {
3582 if (engine()->vie()->base()->SetCpuOveruseOptions(channel_id,
3583 overuse_options) != 0) {
3584 LOG_RTCERR1(SetCpuOveruseOptions, channel_id);
3585 }
3586 }
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +00003587
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003588 // Register encoder observer for outgoing framerate and bitrate.
3589 if (engine()->vie()->codec()->RegisterEncoderObserver(
3590 channel_id, *send_channel->encoder_observer()) != 0) {
3591 LOG_RTCERR1(RegisterEncoderObserver, send_channel->encoder_observer());
3592 return false;
3593 }
3594
3595 if (!SetHeaderExtension(&webrtc::ViERTP_RTCP::SetSendTimestampOffsetStatus,
3596 channel_id, send_extensions_, kRtpTimestampOffsetHeaderExtension)) {
3597 return false;
3598 }
3599
3600 if (!SetHeaderExtension(&webrtc::ViERTP_RTCP::SetSendAbsoluteSendTimeStatus,
henrike@webrtc.org79047f92014-03-06 23:46:59 +00003601 channel_id, send_extensions_, kRtpAbsoluteSenderTimeHeaderExtension)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003602 return false;
3603 }
3604
3605 if (options_.video_leaky_bucket.GetWithDefaultIfUnset(false)) {
3606 if (engine()->vie()->rtp()->SetTransmissionSmoothingStatus(channel_id,
3607 true) != 0) {
3608 LOG_RTCERR2(SetTransmissionSmoothingStatus, channel_id, true);
3609 return false;
3610 }
3611 }
3612
3613 int buffer_latency =
3614 options_.buffered_mode_latency.GetWithDefaultIfUnset(
3615 cricket::kBufferedModeDisabled);
3616 if (buffer_latency != cricket::kBufferedModeDisabled) {
3617 if (engine()->vie()->rtp()->SetSenderBufferingMode(
3618 channel_id, buffer_latency) != 0) {
3619 LOG_RTCERR2(SetSenderBufferingMode, channel_id, buffer_latency);
3620 }
3621 }
henrike@webrtc.org6e3dbc22014-03-25 17:09:47 +00003622
3623 if (options_.suspend_below_min_bitrate.GetWithDefaultIfUnset(false)) {
3624 engine()->vie()->codec()->SuspendBelowMinBitrate(channel_id);
3625 }
3626
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003627 // The remb status direction correspond to the RTP stream (and not the RTCP
3628 // stream). I.e. if send remb is enabled it means it is receiving remote
3629 // rembs and should use them to estimate bandwidth. Receive remb mean that
3630 // remb packets will be generated and that the channel should be included in
3631 // it. If remb is enabled all channels are allowed to contribute to the remb
3632 // but only receive channels will ever end up actually contributing. This
3633 // keeps the logic simple.
3634 if (engine_->vie()->rtp()->SetRembStatus(channel_id,
3635 remb_enabled_,
3636 remb_enabled_) != 0) {
3637 LOG_RTCERR3(SetRembStatus, channel_id, remb_enabled_, remb_enabled_);
3638 return false;
3639 }
3640 if (!SetNackFec(channel_id, send_red_type_, send_fec_type_, nack_enabled_)) {
3641 // Logged in SetNackFec. Don't spam the logs.
3642 return false;
3643 }
3644
3645 send_channels_[local_ssrc_key] = send_channel.release();
3646
3647 return true;
3648}
3649
3650bool WebRtcVideoMediaChannel::SetNackFec(int channel_id,
3651 int red_payload_type,
3652 int fec_payload_type,
3653 bool nack_enabled) {
3654 bool enable = (red_payload_type != -1 && fec_payload_type != -1 &&
3655 !InConferenceMode());
3656 if (enable) {
3657 if (engine_->vie()->rtp()->SetHybridNACKFECStatus(
3658 channel_id, nack_enabled, red_payload_type, fec_payload_type) != 0) {
3659 LOG_RTCERR4(SetHybridNACKFECStatus,
3660 channel_id, nack_enabled, red_payload_type, fec_payload_type);
3661 return false;
3662 }
3663 LOG(LS_INFO) << "Hybrid NACK/FEC enabled for channel " << channel_id;
3664 } else {
3665 if (engine_->vie()->rtp()->SetNACKStatus(channel_id, nack_enabled) != 0) {
3666 LOG_RTCERR1(SetNACKStatus, channel_id);
3667 return false;
3668 }
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00003669 std::string enabled = nack_enabled ? "enabled" : "disabled";
3670 LOG(LS_INFO) << "NACK " << enabled << " for channel " << channel_id;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003671 }
3672 return true;
3673}
3674
buildbot@webrtc.orgd1ae89f2014-05-08 19:19:26 +00003675bool WebRtcVideoMediaChannel::SetSendCodec(const webrtc::VideoCodec& codec) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003676 bool ret_val = true;
3677 for (SendChannelMap::iterator iter = send_channels_.begin();
3678 iter != send_channels_.end(); ++iter) {
3679 WebRtcVideoChannelSendInfo* send_channel = iter->second;
buildbot@webrtc.orgd1ae89f2014-05-08 19:19:26 +00003680 ret_val = SetSendCodec(send_channel, codec) && ret_val;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003681 }
3682 if (ret_val) {
3683 // All SetSendCodec calls were successful. Update the global state
3684 // accordingly.
3685 send_codec_.reset(new webrtc::VideoCodec(codec));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003686 } else {
3687 // At least one SetSendCodec call failed, rollback.
3688 for (SendChannelMap::iterator iter = send_channels_.begin();
3689 iter != send_channels_.end(); ++iter) {
3690 WebRtcVideoChannelSendInfo* send_channel = iter->second;
3691 if (send_codec_) {
buildbot@webrtc.orgd1ae89f2014-05-08 19:19:26 +00003692 SetSendCodec(send_channel, *send_codec_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003693 }
3694 }
3695 }
3696 return ret_val;
3697}
3698
3699bool WebRtcVideoMediaChannel::SetSendCodec(
3700 WebRtcVideoChannelSendInfo* send_channel,
buildbot@webrtc.orgd1ae89f2014-05-08 19:19:26 +00003701 const webrtc::VideoCodec& codec) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003702 if (!send_channel) {
3703 return false;
3704 }
buildbot@webrtc.orgd1ae89f2014-05-08 19:19:26 +00003705
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003706 const int channel_id = send_channel->channel_id();
3707 // Make a copy of the codec
3708 webrtc::VideoCodec target_codec = codec;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003709
3710 // Set the default number of temporal layers for VP8.
3711 if (webrtc::kVideoCodecVP8 == codec.codecType) {
3712 target_codec.codecSpecific.VP8.numberOfTemporalLayers =
3713 kDefaultNumberOfTemporalLayers;
3714
3715 // Turn off the VP8 error resilience
3716 target_codec.codecSpecific.VP8.resilience = webrtc::kResilienceOff;
3717
3718 bool enable_denoising =
3719 options_.video_noise_reduction.GetWithDefaultIfUnset(false);
3720 target_codec.codecSpecific.VP8.denoisingOn = enable_denoising;
3721 }
3722
3723 // Register external encoder if codec type is supported by encoder factory.
3724 if (engine()->IsExternalEncoderCodecType(codec.codecType) &&
3725 !send_channel->IsEncoderRegistered(target_codec.plType)) {
3726 webrtc::VideoEncoder* encoder =
3727 engine()->CreateExternalEncoder(codec.codecType);
3728 if (encoder) {
3729 if (engine()->vie()->ext_codec()->RegisterExternalSendCodec(
3730 channel_id, target_codec.plType, encoder, false) == 0) {
3731 send_channel->RegisterEncoder(target_codec.plType, encoder);
3732 } else {
3733 LOG_RTCERR2(RegisterExternalSendCodec, channel_id, target_codec.plName);
3734 engine()->DestroyExternalEncoder(encoder);
3735 }
3736 }
3737 }
3738
3739 // Resolution and framerate may vary for different send channels.
3740 const VideoFormat& video_format = send_channel->video_format();
3741 UpdateVideoCodec(video_format, &target_codec);
3742
3743 if (target_codec.width == 0 && target_codec.height == 0) {
3744 const uint32 ssrc = send_channel->stream_params()->first_ssrc();
3745 LOG(LS_INFO) << "0x0 resolution selected. Captured frames will be dropped "
3746 << "for ssrc: " << ssrc << ".";
3747 } else {
buildbot@webrtc.orgd1ae89f2014-05-08 19:19:26 +00003748 MaybeChangeBitrates(channel_id, &target_codec);
wu@webrtc.org05e7b442014-04-01 17:44:24 +00003749 webrtc::VideoCodec current_codec;
3750 if (!engine()->vie()->codec()->GetSendCodec(channel_id, current_codec)) {
3751 // Compare against existing configured send codec.
3752 if (current_codec == target_codec) {
3753 // Codec is already configured on channel. no need to apply.
3754 return true;
3755 }
3756 }
3757
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003758 if (0 != engine()->vie()->codec()->SetSendCodec(channel_id, target_codec)) {
3759 LOG_RTCERR2(SetSendCodec, channel_id, target_codec.plName);
3760 return false;
3761 }
3762
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00003763 // NOTE: SetRtxSendPayloadType must be called after all simulcast SSRCs
3764 // are configured. Otherwise ssrc's configured after this point will use
3765 // the primary PT for RTX.
3766 if (send_rtx_type_ != -1 &&
3767 engine()->vie()->rtp()->SetRtxSendPayloadType(channel_id,
3768 send_rtx_type_) != 0) {
3769 LOG_RTCERR2(SetRtxSendPayloadType, channel_id, send_rtx_type_);
3770 return false;
3771 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003772 }
3773 send_channel->set_interval(
3774 cricket::VideoFormat::FpsToInterval(target_codec.maxFramerate));
3775 return true;
3776}
3777
3778
3779static std::string ToString(webrtc::VideoCodecComplexity complexity) {
3780 switch (complexity) {
3781 case webrtc::kComplexityNormal:
3782 return "normal";
3783 case webrtc::kComplexityHigh:
3784 return "high";
3785 case webrtc::kComplexityHigher:
3786 return "higher";
3787 case webrtc::kComplexityMax:
3788 return "max";
3789 default:
3790 return "unknown";
3791 }
3792}
3793
3794static std::string ToString(webrtc::VP8ResilienceMode resilience) {
3795 switch (resilience) {
3796 case webrtc::kResilienceOff:
3797 return "off";
3798 case webrtc::kResilientStream:
3799 return "stream";
3800 case webrtc::kResilientFrames:
3801 return "frames";
3802 default:
3803 return "unknown";
3804 }
3805}
3806
3807void WebRtcVideoMediaChannel::LogSendCodecChange(const std::string& reason) {
3808 webrtc::VideoCodec vie_codec;
3809 if (engine()->vie()->codec()->GetSendCodec(vie_channel_, vie_codec) != 0) {
3810 LOG_RTCERR1(GetSendCodec, vie_channel_);
3811 return;
3812 }
3813
3814 LOG(LS_INFO) << reason << " : selected video codec "
3815 << vie_codec.plName << "/"
3816 << vie_codec.width << "x" << vie_codec.height << "x"
3817 << static_cast<int>(vie_codec.maxFramerate) << "fps"
3818 << "@" << vie_codec.maxBitrate << "kbps"
3819 << " (min=" << vie_codec.minBitrate << "kbps,"
3820 << " start=" << vie_codec.startBitrate << "kbps)";
3821 LOG(LS_INFO) << "Video max quantization: " << vie_codec.qpMax;
3822 if (webrtc::kVideoCodecVP8 == vie_codec.codecType) {
3823 LOG(LS_INFO) << "VP8 number of temporal layers: "
3824 << static_cast<int>(
3825 vie_codec.codecSpecific.VP8.numberOfTemporalLayers);
3826 LOG(LS_INFO) << "VP8 options : "
3827 << "picture loss indication = "
3828 << vie_codec.codecSpecific.VP8.pictureLossIndicationOn
3829 << ", feedback mode = "
3830 << vie_codec.codecSpecific.VP8.feedbackModeOn
3831 << ", complexity = "
3832 << ToString(vie_codec.codecSpecific.VP8.complexity)
3833 << ", resilience = "
3834 << ToString(vie_codec.codecSpecific.VP8.resilience)
3835 << ", denoising = "
3836 << vie_codec.codecSpecific.VP8.denoisingOn
3837 << ", error concealment = "
3838 << vie_codec.codecSpecific.VP8.errorConcealmentOn
3839 << ", automatic resize = "
3840 << vie_codec.codecSpecific.VP8.automaticResizeOn
3841 << ", frame dropping = "
3842 << vie_codec.codecSpecific.VP8.frameDroppingOn
3843 << ", key frame interval = "
3844 << vie_codec.codecSpecific.VP8.keyFrameInterval;
3845 }
3846
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00003847 if (send_rtx_type_ != -1) {
3848 LOG(LS_INFO) << "RTX payload type: " << send_rtx_type_;
3849 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003850}
3851
3852bool WebRtcVideoMediaChannel::SetReceiveCodecs(
3853 WebRtcVideoChannelRecvInfo* info) {
3854 int red_type = -1;
3855 int fec_type = -1;
3856 int channel_id = info->channel_id();
buildbot@webrtc.orgdd4742a2014-05-07 14:50:35 +00003857 // Build a map from payload types to video codecs so that we easily can find
3858 // out if associated payload types are referring to valid codecs.
3859 std::map<int, webrtc::VideoCodec*> pt_to_codec;
3860 for (std::vector<webrtc::VideoCodec>::iterator it = receive_codecs_.begin();
3861 it != receive_codecs_.end(); ++it) {
3862 pt_to_codec[it->plType] = &(*it);
3863 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003864 for (std::vector<webrtc::VideoCodec>::iterator it = receive_codecs_.begin();
3865 it != receive_codecs_.end(); ++it) {
3866 if (it->codecType == webrtc::kVideoCodecRED) {
3867 red_type = it->plType;
3868 } else if (it->codecType == webrtc::kVideoCodecULPFEC) {
3869 fec_type = it->plType;
3870 }
buildbot@webrtc.orgdd4742a2014-05-07 14:50:35 +00003871 // If this is an RTX codec we have to verify that it is associated with
3872 // a valid video codec which we have RTX support for.
3873 if (_stricmp(it->plName, kRtxCodecName) == 0) {
3874 std::map<int, int>::iterator apt_it = associated_payload_types_.find(
3875 it->plType);
3876 bool valid_apt = false;
3877 if (apt_it != associated_payload_types_.end()) {
3878 std::map<int, webrtc::VideoCodec*>::iterator codec_it =
3879 pt_to_codec.find(apt_it->second);
3880 // We currently only support RTX associated with VP8 due to limitations
3881 // in webrtc where only one RTX payload type can be registered.
3882 valid_apt = codec_it != pt_to_codec.end() &&
3883 _stricmp(codec_it->second->plName, kVp8PayloadName) == 0;
3884 }
3885 if (!valid_apt) {
3886 LOG(LS_ERROR) << "The RTX codec isn't associated with a known and "
3887 "supported payload type";
3888 return false;
3889 }
3890 if (engine()->vie()->rtp()->SetRtxReceivePayloadType(
3891 channel_id, it->plType) != 0) {
3892 LOG_RTCERR2(SetRtxReceivePayloadType, channel_id, it->plType);
3893 return false;
3894 }
3895 continue;
3896 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003897 if (engine()->vie()->codec()->SetReceiveCodec(channel_id, *it) != 0) {
3898 LOG_RTCERR2(SetReceiveCodec, channel_id, it->plName);
3899 return false;
3900 }
3901 if (!info->IsDecoderRegistered(it->plType) &&
3902 it->codecType != webrtc::kVideoCodecRED &&
3903 it->codecType != webrtc::kVideoCodecULPFEC) {
3904 webrtc::VideoDecoder* decoder =
3905 engine()->CreateExternalDecoder(it->codecType);
3906 if (decoder) {
3907 if (engine()->vie()->ext_codec()->RegisterExternalReceiveCodec(
3908 channel_id, it->plType, decoder) == 0) {
3909 info->RegisterDecoder(it->plType, decoder);
3910 } else {
3911 LOG_RTCERR2(RegisterExternalReceiveCodec, channel_id, it->plName);
3912 engine()->DestroyExternalDecoder(decoder);
3913 }
3914 }
3915 }
3916 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003917 return true;
3918}
3919
3920int WebRtcVideoMediaChannel::GetRecvChannelNum(uint32 ssrc) {
3921 if (ssrc == first_receive_ssrc_) {
3922 return vie_channel_;
3923 }
buildbot@webrtc.orgdd4742a2014-05-07 14:50:35 +00003924 int recv_channel = -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003925 RecvChannelMap::iterator it = recv_channels_.find(ssrc);
buildbot@webrtc.orgdd4742a2014-05-07 14:50:35 +00003926 if (it == recv_channels_.end()) {
3927 // Check if we have an RTX stream registered on this SSRC.
3928 SsrcMap::iterator rtx_it = rtx_to_primary_ssrc_.find(ssrc);
3929 if (rtx_it != rtx_to_primary_ssrc_.end()) {
3930 it = recv_channels_.find(rtx_it->second);
3931 assert(it != recv_channels_.end());
3932 recv_channel = it->second->channel_id();
3933 }
3934 } else {
3935 recv_channel = it->second->channel_id();
3936 }
3937 return recv_channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003938}
3939
3940// If the new frame size is different from the send codec size we set on vie,
3941// we need to reset the send codec on vie.
3942// The new send codec size should not exceed send_codec_ which is controlled
3943// only by the 'jec' logic.
buildbot@webrtc.orgd1ae89f2014-05-08 19:19:26 +00003944// TODO(pthatcher): Get rid of this function, so we only ever set up
3945// codecs in a single place.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003946bool WebRtcVideoMediaChannel::MaybeResetVieSendCodec(
3947 WebRtcVideoChannelSendInfo* send_channel,
3948 int new_width,
3949 int new_height,
3950 bool is_screencast,
3951 bool* reset) {
3952 if (reset) {
3953 *reset = false;
3954 }
3955 ASSERT(send_codec_.get() != NULL);
3956
buildbot@webrtc.orgd1ae89f2014-05-08 19:19:26 +00003957 webrtc::VideoCodec target_codec = *send_codec_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003958 const VideoFormat& video_format = send_channel->video_format();
3959 UpdateVideoCodec(video_format, &target_codec);
3960
3961 // Vie send codec size should not exceed target_codec.
3962 int target_width = new_width;
3963 int target_height = new_height;
3964 if (!is_screencast &&
3965 (new_width > target_codec.width || new_height > target_codec.height)) {
3966 target_width = target_codec.width;
3967 target_height = target_codec.height;
3968 }
3969
3970 // Get current vie codec.
3971 webrtc::VideoCodec vie_codec;
3972 const int channel_id = send_channel->channel_id();
3973 if (engine()->vie()->codec()->GetSendCodec(channel_id, vie_codec) != 0) {
3974 LOG_RTCERR1(GetSendCodec, channel_id);
3975 return false;
3976 }
3977 const int cur_width = vie_codec.width;
3978 const int cur_height = vie_codec.height;
3979
3980 // Only reset send codec when there is a size change. Additionally,
3981 // automatic resize needs to be turned off when screencasting and on when
3982 // not screencasting.
3983 // Don't allow automatic resizing for screencasting.
3984 bool automatic_resize = !is_screencast;
3985 // Turn off VP8 frame dropping when screensharing as the current model does
3986 // not work well at low fps.
3987 bool vp8_frame_dropping = !is_screencast;
3988 // Disable denoising for screencasting.
3989 bool enable_denoising =
3990 options_.video_noise_reduction.GetWithDefaultIfUnset(false);
henrike@webrtc.orgdce3feb2014-03-26 01:17:30 +00003991 int screencast_min_bitrate =
3992 options_.screencast_min_bitrate.GetWithDefaultIfUnset(0);
3993 bool leaky_bucket = options_.video_leaky_bucket.GetWithDefaultIfUnset(false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003994 bool denoising = !is_screencast && enable_denoising;
3995 bool reset_send_codec =
3996 target_width != cur_width || target_height != cur_height ||
3997 automatic_resize != vie_codec.codecSpecific.VP8.automaticResizeOn ||
3998 denoising != vie_codec.codecSpecific.VP8.denoisingOn ||
3999 vp8_frame_dropping != vie_codec.codecSpecific.VP8.frameDroppingOn;
4000
4001 if (reset_send_codec) {
4002 // Set the new codec on vie.
4003 vie_codec.width = target_width;
4004 vie_codec.height = target_height;
4005 vie_codec.maxFramerate = target_codec.maxFramerate;
4006 vie_codec.startBitrate = target_codec.startBitrate;
buildbot@webrtc.orgd1ae89f2014-05-08 19:19:26 +00004007 vie_codec.minBitrate = target_codec.minBitrate;
4008 vie_codec.maxBitrate = target_codec.maxBitrate;
henrike@webrtc.org6e3dbc22014-03-25 17:09:47 +00004009 vie_codec.targetBitrate = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00004010 vie_codec.codecSpecific.VP8.automaticResizeOn = automatic_resize;
4011 vie_codec.codecSpecific.VP8.denoisingOn = denoising;
4012 vie_codec.codecSpecific.VP8.frameDroppingOn = vp8_frame_dropping;
buildbot@webrtc.orgd1ae89f2014-05-08 19:19:26 +00004013 MaybeChangeBitrates(channel_id, &vie_codec);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00004014
4015 if (engine()->vie()->codec()->SetSendCodec(channel_id, vie_codec) != 0) {
4016 LOG_RTCERR1(SetSendCodec, channel_id);
4017 return false;
4018 }
henrike@webrtc.orgdce3feb2014-03-26 01:17:30 +00004019
henrike@webrtc.orgdce3feb2014-03-26 01:17:30 +00004020 if (is_screencast) {
4021 engine()->vie()->rtp()->SetMinTransmitBitrate(channel_id,
4022 screencast_min_bitrate);
4023 // If screencast and min bitrate set, force enable pacer.
4024 if (screencast_min_bitrate > 0) {
4025 engine()->vie()->rtp()->SetTransmissionSmoothingStatus(channel_id,
4026 true);
4027 }
4028 } else {
4029 // In case of switching from screencast to regular capture, set
4030 // min bitrate padding and pacer back to defaults.
4031 engine()->vie()->rtp()->SetMinTransmitBitrate(channel_id, 0);
4032 engine()->vie()->rtp()->SetTransmissionSmoothingStatus(channel_id,
4033 leaky_bucket);
4034 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00004035 if (reset) {
4036 *reset = true;
4037 }
4038 LogSendCodecChange("Capture size changed");
4039 }
4040
4041 return true;
4042}
4043
buildbot@webrtc.orgd1ae89f2014-05-08 19:19:26 +00004044void WebRtcVideoMediaChannel::MaybeChangeBitrates(
4045 int channel_id, webrtc::VideoCodec* codec) {
4046 codec->minBitrate = GetBitrate(codec->minBitrate, kMinVideoBitrate);
4047 codec->startBitrate = GetBitrate(codec->startBitrate, kStartVideoBitrate);
4048 codec->maxBitrate = GetBitrate(codec->maxBitrate, kMaxVideoBitrate);
4049
4050 if (codec->minBitrate > codec->maxBitrate) {
4051 LOG(LS_INFO) << "Decreasing codec min bitrate to the max ("
4052 << codec->maxBitrate << ") because the min ("
4053 << codec->minBitrate << ") exceeds the max.";
4054 codec->minBitrate = codec->maxBitrate;
4055 }
4056 if (codec->startBitrate < codec->minBitrate) {
4057 LOG(LS_INFO) << "Increasing codec start bitrate to the min ("
4058 << codec->minBitrate << ") because the start ("
4059 << codec->startBitrate << ") is less than the min.";
4060 codec->startBitrate = codec->minBitrate;
4061 } else if (codec->startBitrate > codec->maxBitrate) {
4062 LOG(LS_INFO) << "Decreasing codec start bitrate to the max ("
4063 << codec->maxBitrate << ") because the start ("
4064 << codec->startBitrate << ") exceeds the max.";
4065 codec->startBitrate = codec->maxBitrate;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00004066 }
4067
4068 // Use a previous target bitrate, if there is one.
4069 unsigned int current_target_bitrate = 0;
4070 if (engine()->vie()->codec()->GetCodecTargetBitrate(
4071 channel_id, &current_target_bitrate) == 0) {
4072 // Convert to kbps.
4073 current_target_bitrate /= 1000;
buildbot@webrtc.orgd1ae89f2014-05-08 19:19:26 +00004074 if (current_target_bitrate > codec->maxBitrate) {
4075 current_target_bitrate = codec->maxBitrate;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00004076 }
buildbot@webrtc.orgd1ae89f2014-05-08 19:19:26 +00004077 if (current_target_bitrate > codec->startBitrate) {
4078 codec->startBitrate = current_target_bitrate;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00004079 }
4080 }
4081}
4082
4083void WebRtcVideoMediaChannel::OnMessage(talk_base::Message* msg) {
4084 FlushBlackFrameData* black_frame_data =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00004085 static_cast<FlushBlackFrameData*>(msg->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00004086 FlushBlackFrame(black_frame_data->ssrc, black_frame_data->timestamp);
4087 delete black_frame_data;
4088}
4089
4090int WebRtcVideoMediaChannel::SendPacket(int channel, const void* data,
4091 int len) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00004092 talk_base::Buffer packet(data, len, kMaxRtpPacketLen);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00004093 return MediaChannel::SendPacket(&packet) ? len : -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00004094}
4095
4096int WebRtcVideoMediaChannel::SendRTCPPacket(int channel,
4097 const void* data,
4098 int len) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00004099 talk_base::Buffer packet(data, len, kMaxRtpPacketLen);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00004100 return MediaChannel::SendRtcp(&packet) ? len : -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00004101}
4102
4103void WebRtcVideoMediaChannel::QueueBlackFrame(uint32 ssrc, int64 timestamp,
4104 int framerate) {
4105 if (timestamp) {
4106 FlushBlackFrameData* black_frame_data = new FlushBlackFrameData(
4107 ssrc,
4108 timestamp);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00004109 const int delay_ms = static_cast<int>(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00004110 2 * cricket::VideoFormat::FpsToInterval(framerate) *
4111 talk_base::kNumMillisecsPerSec / talk_base::kNumNanosecsPerSec);
4112 worker_thread()->PostDelayed(delay_ms, this, 0, black_frame_data);
4113 }
4114}
4115
4116void WebRtcVideoMediaChannel::FlushBlackFrame(uint32 ssrc, int64 timestamp) {
4117 WebRtcVideoChannelSendInfo* send_channel = GetSendChannel(ssrc);
4118 if (!send_channel) {
4119 return;
4120 }
4121 talk_base::scoped_ptr<const VideoFrame> black_frame_ptr;
4122
4123 const WebRtcLocalStreamInfo* channel_stream_info =
4124 send_channel->local_stream_info();
4125 int64 last_frame_time_stamp = channel_stream_info->time_stamp();
4126 if (last_frame_time_stamp == timestamp) {
4127 size_t last_frame_width = 0;
4128 size_t last_frame_height = 0;
4129 int64 last_frame_elapsed_time = 0;
4130 channel_stream_info->GetLastFrameInfo(&last_frame_width, &last_frame_height,
4131 &last_frame_elapsed_time);
4132 if (!last_frame_width || !last_frame_height) {
4133 return;
4134 }
4135 WebRtcVideoFrame black_frame;
4136 // Black frame is not screencast.
4137 const bool screencasting = false;
4138 const int64 timestamp_delta = send_channel->interval();
4139 if (!black_frame.InitToBlack(send_codec_->width, send_codec_->height, 1, 1,
4140 last_frame_elapsed_time + timestamp_delta,
4141 last_frame_time_stamp + timestamp_delta) ||
4142 !SendFrame(send_channel, &black_frame, screencasting)) {
4143 LOG(LS_ERROR) << "Failed to send black frame.";
4144 }
4145 }
4146}
4147
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00004148void WebRtcVideoMediaChannel::OnCpuAdaptationUnable() {
4149 // ssrc is hardcoded to 0. This message is based on a system wide issue,
4150 // so finding which ssrc caused it doesn't matter.
4151 SignalMediaError(0, VideoMediaChannel::ERROR_REC_CPU_MAX_CANT_DOWNGRADE);
4152}
4153
henrike@webrtc.org28e20752013-07-10 00:45:36 +00004154void WebRtcVideoMediaChannel::SetNetworkTransmissionState(
4155 bool is_transmitting) {
4156 LOG(LS_INFO) << "SetNetworkTransmissionState: " << is_transmitting;
4157 for (SendChannelMap::iterator iter = send_channels_.begin();
4158 iter != send_channels_.end(); ++iter) {
4159 WebRtcVideoChannelSendInfo* send_channel = iter->second;
4160 int channel_id = send_channel->channel_id();
4161 engine_->vie()->network()->SetNetworkTransmissionState(channel_id,
4162 is_transmitting);
4163 }
4164}
4165
4166bool WebRtcVideoMediaChannel::SetHeaderExtension(ExtensionSetterFunction setter,
4167 int channel_id, const RtpHeaderExtension* extension) {
4168 bool enable = false;
4169 int id = 0;
4170 if (extension) {
4171 enable = true;
4172 id = extension->id;
4173 }
4174 if ((engine_->vie()->rtp()->*setter)(channel_id, enable, id) != 0) {
4175 LOG_RTCERR4(*setter, extension->uri, channel_id, enable, id);
4176 return false;
4177 }
4178 return true;
4179}
4180
4181bool WebRtcVideoMediaChannel::SetHeaderExtension(ExtensionSetterFunction setter,
4182 int channel_id, const std::vector<RtpHeaderExtension>& extensions,
4183 const char header_extension_uri[]) {
4184 const RtpHeaderExtension* extension = FindHeaderExtension(extensions,
4185 header_extension_uri);
4186 return SetHeaderExtension(setter, channel_id, extension);
4187}
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00004188
4189bool WebRtcVideoMediaChannel::SetLocalRtxSsrc(int channel_id,
4190 const StreamParams& send_params,
4191 uint32 primary_ssrc,
4192 int stream_idx) {
4193 uint32 rtx_ssrc = 0;
4194 bool has_rtx = send_params.GetFidSsrc(primary_ssrc, &rtx_ssrc);
4195 if (has_rtx && engine()->vie()->rtp()->SetLocalSSRC(
4196 channel_id, rtx_ssrc, webrtc::kViEStreamTypeRtx, stream_idx) != 0) {
4197 LOG_RTCERR4(SetLocalSSRC, channel_id, rtx_ssrc,
4198 webrtc::kViEStreamTypeRtx, stream_idx);
4199 return false;
4200 }
4201 return true;
4202}
4203
wu@webrtc.org24301a62013-12-13 19:17:43 +00004204void WebRtcVideoMediaChannel::MaybeConnectCapturer(VideoCapturer* capturer) {
4205 if (capturer != NULL && GetSendChannelNum(capturer) == 1) {
wu@webrtc.orgf7d501d2014-03-27 23:48:25 +00004206 capturer->SignalVideoFrame.connect(this,
4207 &WebRtcVideoMediaChannel::SendFrame);
wu@webrtc.org24301a62013-12-13 19:17:43 +00004208 }
4209}
4210
4211void WebRtcVideoMediaChannel::MaybeDisconnectCapturer(VideoCapturer* capturer) {
4212 if (capturer != NULL && GetSendChannelNum(capturer) == 1) {
4213 capturer->SignalVideoFrame.disconnect(this);
4214 }
4215}
4216
henrike@webrtc.org28e20752013-07-10 00:45:36 +00004217} // namespace cricket
4218
4219#endif // HAVE_WEBRTC_VIDEO