blob: 869e5244498c0596208275a619ebbb0da353ba13 [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"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000065
66#if !defined(LIBPEERCONNECTION_LIB)
henrike@webrtc.org28e20752013-07-10 00:45:36 +000067#include "talk/media/webrtc/webrtcmediaengine.h"
68
69WRME_EXPORT
70cricket::MediaEngineInterface* CreateWebRtcMediaEngine(
71 webrtc::AudioDeviceModule* adm, webrtc::AudioDeviceModule* adm_sc,
72 cricket::WebRtcVideoEncoderFactory* encoder_factory,
73 cricket::WebRtcVideoDecoderFactory* decoder_factory) {
74 return new cricket::WebRtcMediaEngine(adm, adm_sc, encoder_factory,
75 decoder_factory);
76}
77
78WRME_EXPORT
79void DestroyWebRtcMediaEngine(cricket::MediaEngineInterface* media_engine) {
80 delete static_cast<cricket::WebRtcMediaEngine*>(media_engine);
81}
82#endif
83
84
85namespace cricket {
86
87
88static const int kDefaultLogSeverity = talk_base::LS_WARNING;
89
90static const int kMinVideoBitrate = 50;
91static const int kStartVideoBitrate = 300;
92static const int kMaxVideoBitrate = 2000;
93static const int kDefaultConferenceModeMaxVideoBitrate = 500;
94
wu@webrtc.orgcecfd182013-10-30 05:18:12 +000095// Controlled by exp, try a super low minimum bitrate for poor connections.
96static const int kLowerMinBitrate = 30;
97
henrike@webrtc.org28e20752013-07-10 00:45:36 +000098static const int kVideoMtu = 1200;
99
100static const int kVideoRtpBufferSize = 65536;
101
102static const char kVp8PayloadName[] = "VP8";
103static const char kRedPayloadName[] = "red";
104static const char kFecPayloadName[] = "ulpfec";
105
106static const int kDefaultNumberOfTemporalLayers = 1; // 1:1
107
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000108static const int kMaxExternalVideoCodecs = 8;
109static const int kExternalVideoPayloadTypeBase = 120;
110
111// Static allocation of payload type values for external video codec.
112static int GetExternalVideoPayloadType(int index) {
113 ASSERT(index >= 0 && index < kMaxExternalVideoCodecs);
114 return kExternalVideoPayloadTypeBase + index;
115}
116
117static void LogMultiline(talk_base::LoggingSeverity sev, char* text) {
118 const char* delim = "\r\n";
119 // TODO(fbarchard): Fix strtok lint warning.
120 for (char* tok = strtok(text, delim); tok; tok = strtok(NULL, delim)) {
121 LOG_V(sev) << tok;
122 }
123}
124
125// Severity is an integer because it comes is assumed to be from command line.
126static int SeverityToFilter(int severity) {
127 int filter = webrtc::kTraceNone;
128 switch (severity) {
129 case talk_base::LS_VERBOSE:
130 filter |= webrtc::kTraceAll;
131 case talk_base::LS_INFO:
132 filter |= (webrtc::kTraceStateInfo | webrtc::kTraceInfo);
133 case talk_base::LS_WARNING:
134 filter |= (webrtc::kTraceTerseInfo | webrtc::kTraceWarning);
135 case talk_base::LS_ERROR:
136 filter |= (webrtc::kTraceError | webrtc::kTraceCritical);
137 }
138 return filter;
139}
140
141static const int kCpuMonitorPeriodMs = 2000; // 2 seconds.
142
143static const bool kNotSending = false;
144
wu@webrtc.orgde305012013-10-31 15:40:38 +0000145// Default video dscp value.
146// See http://tools.ietf.org/html/rfc2474 for details
147// See also http://tools.ietf.org/html/draft-jennings-rtcweb-qos-00
148static const talk_base::DiffServCodePoint kVideoDscpValue =
149 talk_base::DSCP_AF41;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000150
151static bool IsNackEnabled(const VideoCodec& codec) {
152 return codec.HasFeedbackParam(FeedbackParam(kRtcpFbParamNack,
153 kParamValueEmpty));
154}
155
156// Returns true if Receiver Estimated Max Bitrate is enabled.
157static bool IsRembEnabled(const VideoCodec& codec) {
158 return codec.HasFeedbackParam(FeedbackParam(kRtcpFbParamRemb,
159 kParamValueEmpty));
160}
161
162struct FlushBlackFrameData : public talk_base::MessageData {
163 FlushBlackFrameData(uint32 s, int64 t) : ssrc(s), timestamp(t) {
164 }
165 uint32 ssrc;
166 int64 timestamp;
167};
168
169class WebRtcRenderAdapter : public webrtc::ExternalRenderer {
170 public:
buildbot@webrtc.orgf875f152014-04-14 16:06:21 +0000171 WebRtcRenderAdapter(VideoRenderer* renderer, int channel_id)
buildbot@webrtc.org0581f0b2014-05-06 21:36:31 +0000172 : renderer_(renderer),
173 channel_id_(channel_id),
174 width_(0),
175 height_(0),
176 first_frame_arrived_(false),
177 capture_start_rtp_time_stamp_(0),
178 capture_start_ntp_time_ms_(0) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000179 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000180
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000181 virtual ~WebRtcRenderAdapter() {
182 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000183
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000184 void SetRenderer(VideoRenderer* renderer) {
185 talk_base::CritScope cs(&crit_);
186 renderer_ = renderer;
187 // FrameSizeChange may have already been called when renderer was not set.
188 // If so we should call SetSize here.
189 // TODO(ronghuawu): Add unit test for this case. Didn't do it now
190 // because the WebRtcRenderAdapter is currently hiding in cc file. No
191 // good way to get access to it from the unit test.
192 if (width_ > 0 && height_ > 0 && renderer_ != NULL) {
193 if (!renderer_->SetSize(width_, height_, 0)) {
194 LOG(LS_ERROR)
buildbot@webrtc.orgf875f152014-04-14 16:06:21 +0000195 << "WebRtcRenderAdapter (channel " << channel_id_
196 << ") SetRenderer failed to SetSize to: "
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000197 << width_ << "x" << height_;
198 }
199 }
200 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000201
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000202 // Implementation of webrtc::ExternalRenderer.
203 virtual int FrameSizeChange(unsigned int width, unsigned int height,
204 unsigned int /*number_of_streams*/) {
205 talk_base::CritScope cs(&crit_);
206 width_ = width;
207 height_ = height;
buildbot@webrtc.orgf875f152014-04-14 16:06:21 +0000208 LOG(LS_INFO) << "WebRtcRenderAdapter (channel " << channel_id_
209 << ") frame size changed to: "
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000210 << width << "x" << height;
211 if (renderer_ == NULL) {
buildbot@webrtc.orgf875f152014-04-14 16:06:21 +0000212 LOG(LS_VERBOSE) << "WebRtcRenderAdapter (channel " << channel_id_
213 << ") the renderer has not been set. "
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000214 << "SetSize will be called later in SetRenderer.";
215 return 0;
216 }
217 return renderer_->SetSize(width_, height_, 0) ? 0 : -1;
218 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000219
buildbot@webrtc.org1fd5b452014-04-15 17:39:43 +0000220 virtual int DeliverFrame(unsigned char* buffer,
221 int buffer_size,
buildbot@webrtc.org0581f0b2014-05-06 21:36:31 +0000222 uint32_t rtp_time_stamp,
buildbot@webrtc.org1fd5b452014-04-15 17:39:43 +0000223#ifdef USE_WEBRTC_DEV_BRANCH
224 int64_t ntp_time_ms,
225#endif
226 int64_t render_time,
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000227 void* handle) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000228 talk_base::CritScope cs(&crit_);
buildbot@webrtc.org0581f0b2014-05-06 21:36:31 +0000229 if (!first_frame_arrived_) {
230 first_frame_arrived_ = true;
231 capture_start_rtp_time_stamp_ = rtp_time_stamp;
232 }
buildbot@webrtc.org22190372014-05-07 17:52:33 +0000233
234 const int kVideoCodecClockratekHz = cricket::kVideoCodecClockrate / 1000;
235
buildbot@webrtc.org0581f0b2014-05-06 21:36:31 +0000236#ifdef USE_WEBRTC_DEV_BRANCH
237 if (ntp_time_ms > 0) {
238 uint32 elapsed_time_ms =
239 (rtp_time_stamp - capture_start_rtp_time_stamp_) /
240 kVideoCodecClockratekHz;
241 capture_start_ntp_time_ms_ = ntp_time_ms - elapsed_time_ms;
242 }
243#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000244 frame_rate_tracker_.Update(1);
245 if (renderer_ == NULL) {
246 return 0;
247 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000248 // Convert 90K rtp timestamp to ns timestamp.
buildbot@webrtc.org0581f0b2014-05-06 21:36:31 +0000249 int64 rtp_time_stamp_in_ns = (rtp_time_stamp / kVideoCodecClockratekHz) *
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000250 talk_base::kNumNanosecsPerMillisec;
251 // Convert milisecond render time to ns timestamp.
252 int64 render_time_stamp_in_ns = render_time *
253 talk_base::kNumNanosecsPerMillisec;
254 // Send the rtp timestamp to renderer as the VideoFrame timestamp.
255 // and the render timestamp as the VideoFrame elapsed_time.
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000256 if (handle == NULL) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000257 return DeliverBufferFrame(buffer, buffer_size, render_time_stamp_in_ns,
buildbot@webrtc.org740e6b32014-04-30 15:33:45 +0000258 rtp_time_stamp_in_ns);
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000259 } else {
260 return DeliverTextureFrame(handle, render_time_stamp_in_ns,
buildbot@webrtc.org740e6b32014-04-30 15:33:45 +0000261 rtp_time_stamp_in_ns);
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000262 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000263 }
264
265 virtual bool IsTextureSupported() { return true; }
266
267 int DeliverBufferFrame(unsigned char* buffer, int buffer_size,
buildbot@webrtc.org0581f0b2014-05-06 21:36:31 +0000268 int64 elapsed_time, int64 rtp_time_stamp_in_ns) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000269 WebRtcVideoFrame video_frame;
wu@webrtc.org16d62542013-11-05 23:45:14 +0000270 video_frame.Alias(buffer, buffer_size, width_, height_,
buildbot@webrtc.org0581f0b2014-05-06 21:36:31 +0000271 1, 1, elapsed_time, rtp_time_stamp_in_ns, 0);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000272
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000273 // Sanity check on decoded frame size.
274 if (buffer_size != static_cast<int>(VideoFrame::SizeOf(width_, height_))) {
buildbot@webrtc.orgf875f152014-04-14 16:06:21 +0000275 LOG(LS_WARNING) << "WebRtcRenderAdapter (channel " << channel_id_
276 << ") received a strange frame size: "
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000277 << buffer_size;
278 }
279
280 int ret = renderer_->RenderFrame(&video_frame) ? 0 : -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000281 return ret;
282 }
283
buildbot@webrtc.org0581f0b2014-05-06 21:36:31 +0000284 int DeliverTextureFrame(void* handle,
285 int64 elapsed_time,
286 int64 rtp_time_stamp_in_ns) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000287 WebRtcTextureVideoFrame video_frame(
288 static_cast<webrtc::NativeHandle*>(handle), width_, height_,
buildbot@webrtc.org0581f0b2014-05-06 21:36:31 +0000289 elapsed_time, rtp_time_stamp_in_ns);
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000290 return renderer_->RenderFrame(&video_frame);
291 }
292
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000293 unsigned int width() {
294 talk_base::CritScope cs(&crit_);
295 return width_;
296 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000297
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000298 unsigned int height() {
299 talk_base::CritScope cs(&crit_);
300 return height_;
301 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000302
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000303 int framerate() {
304 talk_base::CritScope cs(&crit_);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000305 return static_cast<int>(frame_rate_tracker_.units_second());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000306 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000307
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000308 VideoRenderer* renderer() {
309 talk_base::CritScope cs(&crit_);
310 return renderer_;
311 }
312
buildbot@webrtc.org0581f0b2014-05-06 21:36:31 +0000313 int64 capture_start_ntp_time_ms() {
314 talk_base::CritScope cs(&crit_);
315 return capture_start_ntp_time_ms_;
316 }
317
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000318 private:
319 talk_base::CriticalSection crit_;
320 VideoRenderer* renderer_;
buildbot@webrtc.orgf875f152014-04-14 16:06:21 +0000321 int channel_id_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000322 unsigned int width_;
323 unsigned int height_;
324 talk_base::RateTracker frame_rate_tracker_;
buildbot@webrtc.org0581f0b2014-05-06 21:36:31 +0000325 bool first_frame_arrived_;
326 uint32 capture_start_rtp_time_stamp_;
327 int64 capture_start_ntp_time_ms_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000328};
329
330class WebRtcDecoderObserver : public webrtc::ViEDecoderObserver {
331 public:
332 explicit WebRtcDecoderObserver(int video_channel)
333 : video_channel_(video_channel),
334 framerate_(0),
335 bitrate_(0),
wu@webrtc.org97077a32013-10-25 21:18:33 +0000336 decode_ms_(0),
337 max_decode_ms_(0),
338 current_delay_ms_(0),
339 target_delay_ms_(0),
340 jitter_buffer_ms_(0),
341 min_playout_delay_ms_(0),
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +0000342 render_delay_ms_(0) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000343 }
344
345 // virtual functions from VieDecoderObserver.
346 virtual void IncomingCodecChanged(const int videoChannel,
347 const webrtc::VideoCodec& videoCodec) {}
348 virtual void IncomingRate(const int videoChannel,
349 const unsigned int framerate,
350 const unsigned int bitrate) {
wu@webrtc.org78187522013-10-07 23:32:02 +0000351 talk_base::CritScope cs(&crit_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000352 ASSERT(video_channel_ == videoChannel);
353 framerate_ = framerate;
354 bitrate_ = bitrate;
355 }
wu@webrtc.org97077a32013-10-25 21:18:33 +0000356
357 virtual void DecoderTiming(int decode_ms,
358 int max_decode_ms,
359 int current_delay_ms,
360 int target_delay_ms,
361 int jitter_buffer_ms,
362 int min_playout_delay_ms,
363 int render_delay_ms) {
364 talk_base::CritScope cs(&crit_);
365 decode_ms_ = decode_ms;
366 max_decode_ms_ = max_decode_ms;
367 current_delay_ms_ = current_delay_ms;
368 target_delay_ms_ = target_delay_ms;
369 jitter_buffer_ms_ = jitter_buffer_ms;
370 min_playout_delay_ms_ = min_playout_delay_ms;
371 render_delay_ms_ = render_delay_ms;
372 }
373
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +0000374 virtual void RequestNewKeyFrame(const int videoChannel) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000375
wu@webrtc.org97077a32013-10-25 21:18:33 +0000376 // Populate |rinfo| based on previously-set data in |*this|.
377 void ExportTo(VideoReceiverInfo* rinfo) {
wu@webrtc.org78187522013-10-07 23:32:02 +0000378 talk_base::CritScope cs(&crit_);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000379 rinfo->framerate_rcvd = framerate_;
380 rinfo->decode_ms = decode_ms_;
381 rinfo->max_decode_ms = max_decode_ms_;
382 rinfo->current_delay_ms = current_delay_ms_;
383 rinfo->target_delay_ms = target_delay_ms_;
384 rinfo->jitter_buffer_ms = jitter_buffer_ms_;
385 rinfo->min_playout_delay_ms = min_playout_delay_ms_;
386 rinfo->render_delay_ms = render_delay_ms_;
wu@webrtc.org78187522013-10-07 23:32:02 +0000387 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000388
389 private:
wu@webrtc.org78187522013-10-07 23:32:02 +0000390 mutable talk_base::CriticalSection crit_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000391 int video_channel_;
392 int framerate_;
393 int bitrate_;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000394 int decode_ms_;
395 int max_decode_ms_;
396 int current_delay_ms_;
397 int target_delay_ms_;
398 int jitter_buffer_ms_;
399 int min_playout_delay_ms_;
400 int render_delay_ms_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000401};
402
403class WebRtcEncoderObserver : public webrtc::ViEEncoderObserver {
404 public:
405 explicit WebRtcEncoderObserver(int video_channel)
406 : video_channel_(video_channel),
407 framerate_(0),
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000408 bitrate_(0),
409 suspended_(false) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000410 }
411
412 // virtual functions from VieEncoderObserver.
413 virtual void OutgoingRate(const int videoChannel,
414 const unsigned int framerate,
415 const unsigned int bitrate) {
wu@webrtc.org78187522013-10-07 23:32:02 +0000416 talk_base::CritScope cs(&crit_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000417 ASSERT(video_channel_ == videoChannel);
418 framerate_ = framerate;
419 bitrate_ = bitrate;
420 }
421
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000422 virtual void SuspendChange(int video_channel, bool is_suspended) {
423 talk_base::CritScope cs(&crit_);
424 ASSERT(video_channel_ == video_channel);
425 suspended_ = is_suspended;
426 }
427
wu@webrtc.org78187522013-10-07 23:32:02 +0000428 int framerate() const {
429 talk_base::CritScope cs(&crit_);
430 return framerate_;
431 }
432 int bitrate() const {
433 talk_base::CritScope cs(&crit_);
434 return bitrate_;
435 }
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000436 bool suspended() const {
437 talk_base::CritScope cs(&crit_);
438 return suspended_;
439 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000440
441 private:
wu@webrtc.org78187522013-10-07 23:32:02 +0000442 mutable talk_base::CriticalSection crit_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000443 int video_channel_;
444 int framerate_;
445 int bitrate_;
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000446 bool suspended_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000447};
448
449class WebRtcLocalStreamInfo {
450 public:
451 WebRtcLocalStreamInfo()
452 : width_(0), height_(0), elapsed_time_(-1), time_stamp_(-1) {}
453 size_t width() const {
454 talk_base::CritScope cs(&crit_);
455 return width_;
456 }
457 size_t height() const {
458 talk_base::CritScope cs(&crit_);
459 return height_;
460 }
461 int64 elapsed_time() const {
462 talk_base::CritScope cs(&crit_);
463 return elapsed_time_;
464 }
465 int64 time_stamp() const {
466 talk_base::CritScope cs(&crit_);
467 return time_stamp_;
468 }
469 int framerate() {
470 talk_base::CritScope cs(&crit_);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000471 return static_cast<int>(rate_tracker_.units_second());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000472 }
473 void GetLastFrameInfo(
474 size_t* width, size_t* height, int64* elapsed_time) const {
475 talk_base::CritScope cs(&crit_);
476 *width = width_;
477 *height = height_;
478 *elapsed_time = elapsed_time_;
479 }
480
481 void UpdateFrame(const VideoFrame* frame) {
482 talk_base::CritScope cs(&crit_);
483
484 width_ = frame->GetWidth();
485 height_ = frame->GetHeight();
486 elapsed_time_ = frame->GetElapsedTime();
487 time_stamp_ = frame->GetTimeStamp();
488
489 rate_tracker_.Update(1);
490 }
491
492 private:
493 mutable talk_base::CriticalSection crit_;
494 size_t width_;
495 size_t height_;
496 int64 elapsed_time_;
497 int64 time_stamp_;
498 talk_base::RateTracker rate_tracker_;
499
500 DISALLOW_COPY_AND_ASSIGN(WebRtcLocalStreamInfo);
501};
502
503// WebRtcVideoChannelRecvInfo is a container class with members such as renderer
504// and a decoder observer that is used by receive channels.
505// It must exist as long as the receive channel is connected to renderer or a
506// decoder observer in this class and methods in the class should only be called
507// from the worker thread.
508class WebRtcVideoChannelRecvInfo {
509 public:
510 typedef std::map<int, webrtc::VideoDecoder*> DecoderMap; // key: payload type
511 explicit WebRtcVideoChannelRecvInfo(int channel_id)
512 : channel_id_(channel_id),
buildbot@webrtc.orgf875f152014-04-14 16:06:21 +0000513 render_adapter_(NULL, channel_id),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000514 decoder_observer_(channel_id) {
515 }
516 int channel_id() { return channel_id_; }
517 void SetRenderer(VideoRenderer* renderer) {
518 render_adapter_.SetRenderer(renderer);
519 }
520 WebRtcRenderAdapter* render_adapter() { return &render_adapter_; }
521 WebRtcDecoderObserver* decoder_observer() { return &decoder_observer_; }
522 void RegisterDecoder(int pl_type, webrtc::VideoDecoder* decoder) {
523 ASSERT(!IsDecoderRegistered(pl_type));
524 registered_decoders_[pl_type] = decoder;
525 }
526 bool IsDecoderRegistered(int pl_type) {
527 return registered_decoders_.count(pl_type) != 0;
528 }
529 const DecoderMap& registered_decoders() {
530 return registered_decoders_;
531 }
532 void ClearRegisteredDecoders() {
533 registered_decoders_.clear();
534 }
535
536 private:
537 int channel_id_; // Webrtc video channel number.
538 // Renderer for this channel.
539 WebRtcRenderAdapter render_adapter_;
540 WebRtcDecoderObserver decoder_observer_;
541 DecoderMap registered_decoders_;
542};
543
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000544class WebRtcOveruseObserver : public webrtc::CpuOveruseObserver {
545 public:
546 explicit WebRtcOveruseObserver(CoordinatedVideoAdapter* video_adapter)
547 : video_adapter_(video_adapter),
548 enabled_(false) {
549 }
550
551 // TODO(mflodman): Consider sending resolution as part of event, to let
552 // adapter know what resolution the request is based on. Helps eliminate stale
553 // data, race conditions.
554 virtual void OveruseDetected() OVERRIDE {
555 talk_base::CritScope cs(&crit_);
556 if (!enabled_) {
557 return;
558 }
559
560 video_adapter_->OnCpuResolutionRequest(CoordinatedVideoAdapter::DOWNGRADE);
561 }
562
563 virtual void NormalUsage() OVERRIDE {
564 talk_base::CritScope cs(&crit_);
565 if (!enabled_) {
566 return;
567 }
568
569 video_adapter_->OnCpuResolutionRequest(CoordinatedVideoAdapter::UPGRADE);
570 }
571
572 void Enable(bool enable) {
573 talk_base::CritScope cs(&crit_);
574 enabled_ = enable;
575 }
576
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000577 bool enabled() const { return enabled_; }
578
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000579 private:
580 CoordinatedVideoAdapter* video_adapter_;
581 bool enabled_;
582 talk_base::CriticalSection crit_;
583};
584
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000585
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000586class WebRtcVideoChannelSendInfo : public sigslot::has_slots<> {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000587 public:
588 typedef std::map<int, webrtc::VideoEncoder*> EncoderMap; // key: payload type
589 WebRtcVideoChannelSendInfo(int channel_id, int capture_id,
590 webrtc::ViEExternalCapture* external_capture,
591 talk_base::CpuMonitor* cpu_monitor)
592 : channel_id_(channel_id),
593 capture_id_(capture_id),
594 sending_(false),
595 muted_(false),
596 video_capturer_(NULL),
597 encoder_observer_(channel_id),
598 external_capture_(external_capture),
599 capturer_updated_(false),
600 interval_(0),
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000601 cpu_monitor_(cpu_monitor),
602 overuse_observer_enabled_(false) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000603 }
604
605 int channel_id() const { return channel_id_; }
606 int capture_id() const { return capture_id_; }
607 void set_sending(bool sending) { sending_ = sending; }
608 bool sending() const { return sending_; }
609 void set_muted(bool on) {
610 // TODO(asapersson): add support.
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000611 // video_adapter_.SetBlackOutput(on);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000612 muted_ = on;
613 }
614 bool muted() {return muted_; }
615
616 WebRtcEncoderObserver* encoder_observer() { return &encoder_observer_; }
617 webrtc::ViEExternalCapture* external_capture() { return external_capture_; }
618 const VideoFormat& video_format() const {
619 return video_format_;
620 }
621 void set_video_format(const VideoFormat& video_format) {
622 video_format_ = video_format;
623 if (video_format_ != cricket::VideoFormat()) {
624 interval_ = video_format_.interval;
625 }
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000626 CoordinatedVideoAdapter* adapter = video_adapter();
627 if (adapter) {
628 adapter->OnOutputFormatRequest(video_format_);
629 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000630 }
631 void set_interval(int64 interval) {
632 if (video_format() == cricket::VideoFormat()) {
633 interval_ = interval;
634 }
635 }
636 int64 interval() { return interval_; }
637
xians@webrtc.orgef221512014-02-21 10:31:29 +0000638 int CurrentAdaptReason() const {
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000639 const CoordinatedVideoAdapter* adapter = video_adapter();
640 if (!adapter) {
641 return CoordinatedVideoAdapter::ADAPTREASON_NONE;
642 }
643 return video_adapter()->adapt_reason();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000644 }
645
646 StreamParams* stream_params() { return stream_params_.get(); }
647 void set_stream_params(const StreamParams& sp) {
648 stream_params_.reset(new StreamParams(sp));
649 }
650 void ClearStreamParams() { stream_params_.reset(); }
651 bool has_ssrc(uint32 local_ssrc) const {
652 return !stream_params_ ? false :
653 stream_params_->has_ssrc(local_ssrc);
654 }
655 WebRtcLocalStreamInfo* local_stream_info() {
656 return &local_stream_info_;
657 }
658 VideoCapturer* video_capturer() {
659 return video_capturer_;
660 }
henrike@webrtc.orgc7bec842014-03-12 19:53:43 +0000661 void set_video_capturer(VideoCapturer* video_capturer,
662 ViEWrapper* vie_wrapper) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000663 if (video_capturer == video_capturer_) {
664 return;
665 }
xians@webrtc.orgef221512014-02-21 10:31:29 +0000666
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000667 CoordinatedVideoAdapter* old_video_adapter = video_adapter();
668 if (old_video_adapter) {
669 // Disconnect signals from old video adapter.
670 SignalCpuAdaptationUnable.disconnect(old_video_adapter);
671 if (cpu_monitor_) {
672 cpu_monitor_->SignalUpdate.disconnect(old_video_adapter);
xians@webrtc.orgef221512014-02-21 10:31:29 +0000673 }
henrike@webrtc.org26438052014-02-20 22:32:53 +0000674 }
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000675
676 capturer_updated_ = true;
677 video_capturer_ = video_capturer;
678
henrike@webrtc.orgc7bec842014-03-12 19:53:43 +0000679 vie_wrapper->base()->RegisterCpuOveruseObserver(channel_id_, NULL);
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000680 if (!video_capturer) {
681 overuse_observer_.reset();
682 return;
683 }
684
685 CoordinatedVideoAdapter* adapter = video_adapter();
686 ASSERT(adapter && "Video adapter should not be null here.");
687
688 UpdateAdapterCpuOptions();
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000689
690 overuse_observer_.reset(new WebRtcOveruseObserver(adapter));
henrike@webrtc.orgc7bec842014-03-12 19:53:43 +0000691 vie_wrapper->base()->RegisterCpuOveruseObserver(channel_id_,
692 overuse_observer_.get());
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000693 // (Dis)connect the video adapter from the cpu monitor as appropriate.
694 SetCpuOveruseDetection(overuse_observer_enabled_);
695
696 SignalCpuAdaptationUnable.repeat(adapter->SignalCpuAdaptationUnable);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000697 }
698
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000699 CoordinatedVideoAdapter* video_adapter() {
700 if (!video_capturer_) {
701 return NULL;
702 }
703 return video_capturer_->video_adapter();
704 }
705 const CoordinatedVideoAdapter* video_adapter() const {
706 if (!video_capturer_) {
707 return NULL;
708 }
709 return video_capturer_->video_adapter();
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000710 }
711
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000712 void ApplyCpuOptions(const VideoOptions& video_options) {
713 // Use video_options_.SetAll() instead of assignment so that unset value in
714 // video_options will not overwrite the previous option value.
715 video_options_.SetAll(video_options);
716 UpdateAdapterCpuOptions();
717 }
718
719 void UpdateAdapterCpuOptions() {
720 if (!video_capturer_) {
721 return;
722 }
723
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000724 bool cpu_adapt, cpu_smoothing, adapt_third;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000725 float low, med, high;
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000726
727 // TODO(thorcarpenter): Have VideoAdapter be responsible for setting
728 // all these video options.
729 CoordinatedVideoAdapter* video_adapter = video_capturer_->video_adapter();
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000730 if (video_options_.adapt_input_to_cpu_usage.Get(&cpu_adapt) ||
731 overuse_observer_enabled_) {
732 video_adapter->set_cpu_adaptation(cpu_adapt || overuse_observer_enabled_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000733 }
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000734 if (video_options_.adapt_cpu_with_smoothing.Get(&cpu_smoothing)) {
735 video_adapter->set_cpu_smoothing(cpu_smoothing);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000736 }
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000737 if (video_options_.process_adaptation_threshhold.Get(&med)) {
738 video_adapter->set_process_threshold(med);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000739 }
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000740 if (video_options_.system_low_adaptation_threshhold.Get(&low)) {
741 video_adapter->set_low_system_threshold(low);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000742 }
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000743 if (video_options_.system_high_adaptation_threshhold.Get(&high)) {
744 video_adapter->set_high_system_threshold(high);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000745 }
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000746 if (video_options_.video_adapt_third.Get(&adapt_third)) {
747 video_adapter->set_scale_third(adapt_third);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000748 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000749 }
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000750
751 void SetCpuOveruseDetection(bool enable) {
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000752 overuse_observer_enabled_ = enable;
753
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000754 if (overuse_observer_) {
755 overuse_observer_->Enable(enable);
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000756 }
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000757
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000758 // The video adapter is signaled by overuse detection if enabled; otherwise
759 // it will be signaled by cpu monitor.
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000760 CoordinatedVideoAdapter* adapter = video_adapter();
761 if (adapter) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000762 bool cpu_adapt = false;
763 video_options_.adapt_input_to_cpu_usage.Get(&cpu_adapt);
764 adapter->set_cpu_adaptation(
765 adapter->cpu_adaptation() || cpu_adapt || enable);
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000766 if (cpu_monitor_) {
767 if (enable) {
768 cpu_monitor_->SignalUpdate.disconnect(adapter);
769 } else {
770 cpu_monitor_->SignalUpdate.connect(
771 adapter, &CoordinatedVideoAdapter::OnCpuLoadUpdated);
772 }
773 }
774 }
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000775 }
776
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000777 void ProcessFrame(const VideoFrame& original_frame, bool mute,
778 VideoFrame** processed_frame) {
779 if (!mute) {
780 *processed_frame = original_frame.Copy();
781 } else {
782 WebRtcVideoFrame* black_frame = new WebRtcVideoFrame();
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000783 black_frame->InitToBlack(static_cast<int>(original_frame.GetWidth()),
784 static_cast<int>(original_frame.GetHeight()),
785 1, 1,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000786 original_frame.GetElapsedTime(),
787 original_frame.GetTimeStamp());
788 *processed_frame = black_frame;
789 }
790 local_stream_info_.UpdateFrame(*processed_frame);
791 }
792 void RegisterEncoder(int pl_type, webrtc::VideoEncoder* encoder) {
793 ASSERT(!IsEncoderRegistered(pl_type));
794 registered_encoders_[pl_type] = encoder;
795 }
796 bool IsEncoderRegistered(int pl_type) {
797 return registered_encoders_.count(pl_type) != 0;
798 }
799 const EncoderMap& registered_encoders() {
800 return registered_encoders_;
801 }
802 void ClearRegisteredEncoders() {
803 registered_encoders_.clear();
804 }
805
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000806 sigslot::repeater0<> SignalCpuAdaptationUnable;
807
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000808 private:
809 int channel_id_;
810 int capture_id_;
811 bool sending_;
812 bool muted_;
813 VideoCapturer* video_capturer_;
814 WebRtcEncoderObserver encoder_observer_;
815 webrtc::ViEExternalCapture* external_capture_;
816 EncoderMap registered_encoders_;
817
818 VideoFormat video_format_;
819
820 talk_base::scoped_ptr<StreamParams> stream_params_;
821
822 WebRtcLocalStreamInfo local_stream_info_;
823
824 bool capturer_updated_;
825
826 int64 interval_;
827
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000828 talk_base::CpuMonitor* cpu_monitor_;
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000829 talk_base::scoped_ptr<WebRtcOveruseObserver> overuse_observer_;
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000830 bool overuse_observer_enabled_;
831
832 VideoOptions video_options_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000833};
834
835const WebRtcVideoEngine::VideoCodecPref
836 WebRtcVideoEngine::kVideoCodecPrefs[] = {
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000837 {kVp8PayloadName, 100, -1, 0},
838 {kRedPayloadName, 116, -1, 1},
839 {kFecPayloadName, 117, -1, 2},
840 {kRtxCodecName, 96, 100, 3},
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000841};
842
843// The formats are sorted by the descending order of width. We use the order to
844// find the next format for CPU and bandwidth adaptation.
845const VideoFormatPod WebRtcVideoEngine::kVideoFormats[] = {
846 {1280, 800, FPS_TO_INTERVAL(30), FOURCC_ANY},
847 {1280, 720, FPS_TO_INTERVAL(30), FOURCC_ANY},
848 {960, 600, FPS_TO_INTERVAL(30), FOURCC_ANY},
849 {960, 540, FPS_TO_INTERVAL(30), FOURCC_ANY},
850 {640, 400, FPS_TO_INTERVAL(30), FOURCC_ANY},
851 {640, 360, FPS_TO_INTERVAL(30), FOURCC_ANY},
852 {640, 480, FPS_TO_INTERVAL(30), FOURCC_ANY},
853 {480, 300, FPS_TO_INTERVAL(30), FOURCC_ANY},
854 {480, 270, FPS_TO_INTERVAL(30), FOURCC_ANY},
855 {480, 360, FPS_TO_INTERVAL(30), FOURCC_ANY},
856 {320, 200, FPS_TO_INTERVAL(30), FOURCC_ANY},
857 {320, 180, FPS_TO_INTERVAL(30), FOURCC_ANY},
858 {320, 240, FPS_TO_INTERVAL(30), FOURCC_ANY},
859 {240, 150, FPS_TO_INTERVAL(30), FOURCC_ANY},
860 {240, 135, FPS_TO_INTERVAL(30), FOURCC_ANY},
861 {240, 180, FPS_TO_INTERVAL(30), FOURCC_ANY},
862 {160, 100, FPS_TO_INTERVAL(30), FOURCC_ANY},
863 {160, 90, FPS_TO_INTERVAL(30), FOURCC_ANY},
864 {160, 120, FPS_TO_INTERVAL(30), FOURCC_ANY},
865};
866
867const VideoFormatPod WebRtcVideoEngine::kDefaultVideoFormat =
868 {640, 400, FPS_TO_INTERVAL(30), FOURCC_ANY};
869
870static void UpdateVideoCodec(const cricket::VideoFormat& video_format,
871 webrtc::VideoCodec* target_codec) {
872 if ((target_codec == NULL) || (video_format == cricket::VideoFormat())) {
873 return;
874 }
875 target_codec->width = video_format.width;
876 target_codec->height = video_format.height;
877 target_codec->maxFramerate = cricket::VideoFormat::IntervalToFps(
878 video_format.interval);
879}
880
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000881static bool GetCpuOveruseOptions(const VideoOptions& options,
882 webrtc::CpuOveruseOptions* overuse_options) {
883 int underuse_threshold = 0;
884 int overuse_threshold = 0;
885 if (!options.cpu_underuse_threshold.Get(&underuse_threshold) ||
886 !options.cpu_overuse_threshold.Get(&overuse_threshold)) {
887 return false;
888 }
889 if (underuse_threshold <= 0 || overuse_threshold <= 0) {
890 return false;
891 }
892 // Valid thresholds.
893 bool encode_usage =
894 options.cpu_overuse_encode_usage.GetWithDefaultIfUnset(false);
895 overuse_options->enable_capture_jitter_method = !encode_usage;
896 overuse_options->enable_encode_usage_method = encode_usage;
897 if (encode_usage) {
898 // Use method based on encode usage.
899 overuse_options->low_encode_usage_threshold_percent = underuse_threshold;
900 overuse_options->high_encode_usage_threshold_percent = overuse_threshold;
901 } else {
902 // Use default method based on capture jitter.
903 overuse_options->low_capture_jitter_threshold_ms =
904 static_cast<float>(underuse_threshold);
905 overuse_options->high_capture_jitter_threshold_ms =
906 static_cast<float>(overuse_threshold);
907 }
908 return true;
909}
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000910
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000911WebRtcVideoEngine::WebRtcVideoEngine() {
912 Construct(new ViEWrapper(), new ViETraceWrapper(), NULL,
913 new talk_base::CpuMonitor(NULL));
914}
915
916WebRtcVideoEngine::WebRtcVideoEngine(WebRtcVoiceEngine* voice_engine,
917 ViEWrapper* vie_wrapper,
918 talk_base::CpuMonitor* cpu_monitor) {
919 Construct(vie_wrapper, new ViETraceWrapper(), voice_engine, cpu_monitor);
920}
921
922WebRtcVideoEngine::WebRtcVideoEngine(WebRtcVoiceEngine* voice_engine,
923 ViEWrapper* vie_wrapper,
924 ViETraceWrapper* tracing,
925 talk_base::CpuMonitor* cpu_monitor) {
926 Construct(vie_wrapper, tracing, voice_engine, cpu_monitor);
927}
928
929void WebRtcVideoEngine::Construct(ViEWrapper* vie_wrapper,
930 ViETraceWrapper* tracing,
931 WebRtcVoiceEngine* voice_engine,
932 talk_base::CpuMonitor* cpu_monitor) {
933 LOG(LS_INFO) << "WebRtcVideoEngine::WebRtcVideoEngine";
934 worker_thread_ = NULL;
935 vie_wrapper_.reset(vie_wrapper);
936 vie_wrapper_base_initialized_ = false;
937 tracing_.reset(tracing);
938 voice_engine_ = voice_engine;
939 initialized_ = false;
940 SetTraceFilter(SeverityToFilter(kDefaultLogSeverity));
941 render_module_.reset(new WebRtcPassthroughRender());
942 local_renderer_w_ = local_renderer_h_ = 0;
943 local_renderer_ = NULL;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000944 capture_started_ = false;
945 decoder_factory_ = NULL;
946 encoder_factory_ = NULL;
947 cpu_monitor_.reset(cpu_monitor);
948
949 SetTraceOptions("");
950 if (tracing_->SetTraceCallback(this) != 0) {
951 LOG_RTCERR1(SetTraceCallback, this);
952 }
953
954 // Set default quality levels for our supported codecs. We override them here
955 // if we know your cpu performance is low, and they can be updated explicitly
956 // by calling SetDefaultCodec. For example by a flute preference setting, or
957 // by the server with a jec in response to our reported system info.
958 VideoCodec max_codec(kVideoCodecPrefs[0].payload_type,
959 kVideoCodecPrefs[0].name,
960 kDefaultVideoFormat.width,
961 kDefaultVideoFormat.height,
962 VideoFormat::IntervalToFps(kDefaultVideoFormat.interval),
963 0);
964 if (!SetDefaultCodec(max_codec)) {
965 LOG(LS_ERROR) << "Failed to initialize list of supported codec types";
966 }
967
968
969 // Load our RTP Header extensions.
970 rtp_header_extensions_.push_back(
971 RtpHeaderExtension(kRtpTimestampOffsetHeaderExtension,
henrike@webrtc.org79047f92014-03-06 23:46:59 +0000972 kRtpTimestampOffsetHeaderExtensionDefaultId));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000973 rtp_header_extensions_.push_back(
henrike@webrtc.org79047f92014-03-06 23:46:59 +0000974 RtpHeaderExtension(kRtpAbsoluteSenderTimeHeaderExtension,
975 kRtpAbsoluteSenderTimeHeaderExtensionDefaultId));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000976}
977
978WebRtcVideoEngine::~WebRtcVideoEngine() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000979 LOG(LS_INFO) << "WebRtcVideoEngine::~WebRtcVideoEngine";
980 if (initialized_) {
981 Terminate();
982 }
983 if (encoder_factory_) {
984 encoder_factory_->RemoveObserver(this);
985 }
986 tracing_->SetTraceCallback(NULL);
987 // Test to see if the media processor was deregistered properly.
988 ASSERT(SignalMediaFrame.is_empty());
989}
990
991bool WebRtcVideoEngine::Init(talk_base::Thread* worker_thread) {
992 LOG(LS_INFO) << "WebRtcVideoEngine::Init";
993 worker_thread_ = worker_thread;
994 ASSERT(worker_thread_ != NULL);
995
996 cpu_monitor_->set_thread(worker_thread_);
997 if (!cpu_monitor_->Start(kCpuMonitorPeriodMs)) {
998 LOG(LS_ERROR) << "Failed to start CPU monitor.";
999 cpu_monitor_.reset();
1000 }
1001
1002 bool result = InitVideoEngine();
1003 if (result) {
1004 LOG(LS_INFO) << "VideoEngine Init done";
1005 } else {
1006 LOG(LS_ERROR) << "VideoEngine Init failed, releasing";
1007 Terminate();
1008 }
1009 return result;
1010}
1011
1012bool WebRtcVideoEngine::InitVideoEngine() {
1013 LOG(LS_INFO) << "WebRtcVideoEngine::InitVideoEngine";
1014
1015 // Init WebRTC VideoEngine.
1016 if (!vie_wrapper_base_initialized_) {
1017 if (vie_wrapper_->base()->Init() != 0) {
1018 LOG_RTCERR0(Init);
1019 return false;
1020 }
1021 vie_wrapper_base_initialized_ = true;
1022 }
1023
1024 // Log the VoiceEngine version info.
1025 char buffer[1024] = "";
1026 if (vie_wrapper_->base()->GetVersion(buffer) != 0) {
1027 LOG_RTCERR0(GetVersion);
1028 return false;
1029 }
1030
1031 LOG(LS_INFO) << "WebRtc VideoEngine Version:";
1032 LogMultiline(talk_base::LS_INFO, buffer);
1033
1034 // Hook up to VoiceEngine for sync purposes, if supplied.
1035 if (!voice_engine_) {
1036 LOG(LS_WARNING) << "NULL voice engine";
1037 } else if ((vie_wrapper_->base()->SetVoiceEngine(
1038 voice_engine_->voe()->engine())) != 0) {
1039 LOG_RTCERR0(SetVoiceEngine);
1040 return false;
1041 }
1042
1043 // Register our custom render module.
1044 if (vie_wrapper_->render()->RegisterVideoRenderModule(
1045 *render_module_.get()) != 0) {
1046 LOG_RTCERR0(RegisterVideoRenderModule);
1047 return false;
1048 }
1049
1050 initialized_ = true;
1051 return true;
1052}
1053
1054void WebRtcVideoEngine::Terminate() {
1055 LOG(LS_INFO) << "WebRtcVideoEngine::Terminate";
1056 initialized_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001057
1058 if (vie_wrapper_->render()->DeRegisterVideoRenderModule(
1059 *render_module_.get()) != 0) {
1060 LOG_RTCERR0(DeRegisterVideoRenderModule);
1061 }
1062
1063 if (vie_wrapper_->base()->SetVoiceEngine(NULL) != 0) {
1064 LOG_RTCERR0(SetVoiceEngine);
1065 }
1066
1067 cpu_monitor_->Stop();
1068}
1069
1070int WebRtcVideoEngine::GetCapabilities() {
1071 return VIDEO_RECV | VIDEO_SEND;
1072}
1073
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00001074bool WebRtcVideoEngine::SetOptions(const VideoOptions &options) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001075 return true;
1076}
1077
1078bool WebRtcVideoEngine::SetDefaultEncoderConfig(
1079 const VideoEncoderConfig& config) {
1080 return SetDefaultCodec(config.max_codec);
1081}
1082
wu@webrtc.org78187522013-10-07 23:32:02 +00001083VideoEncoderConfig WebRtcVideoEngine::GetDefaultEncoderConfig() const {
1084 ASSERT(!video_codecs_.empty());
1085 VideoCodec max_codec(kVideoCodecPrefs[0].payload_type,
1086 kVideoCodecPrefs[0].name,
1087 video_codecs_[0].width,
1088 video_codecs_[0].height,
1089 video_codecs_[0].framerate,
1090 0);
1091 return VideoEncoderConfig(max_codec);
1092}
1093
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001094// SetDefaultCodec may be called while the capturer is running. For example, a
1095// test call is started in a page with QVGA default codec, and then a real call
1096// is started in another page with VGA default codec. This is the corner case
1097// and happens only when a session is started. We ignore this case currently.
1098bool WebRtcVideoEngine::SetDefaultCodec(const VideoCodec& codec) {
1099 if (!RebuildCodecList(codec)) {
1100 LOG(LS_WARNING) << "Failed to RebuildCodecList";
1101 return false;
1102 }
1103
wu@webrtc.org78187522013-10-07 23:32:02 +00001104 ASSERT(!video_codecs_.empty());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001105 default_codec_format_ = VideoFormat(
1106 video_codecs_[0].width,
1107 video_codecs_[0].height,
1108 VideoFormat::FpsToInterval(video_codecs_[0].framerate),
1109 FOURCC_ANY);
1110 return true;
1111}
1112
1113WebRtcVideoMediaChannel* WebRtcVideoEngine::CreateChannel(
1114 VoiceMediaChannel* voice_channel) {
1115 WebRtcVideoMediaChannel* channel =
1116 new WebRtcVideoMediaChannel(this, voice_channel);
1117 if (!channel->Init()) {
1118 delete channel;
1119 channel = NULL;
1120 }
1121 return channel;
1122}
1123
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001124bool WebRtcVideoEngine::SetLocalRenderer(VideoRenderer* renderer) {
1125 local_renderer_w_ = local_renderer_h_ = 0;
1126 local_renderer_ = renderer;
1127 return true;
1128}
1129
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001130const std::vector<VideoCodec>& WebRtcVideoEngine::codecs() const {
1131 return video_codecs_;
1132}
1133
1134const std::vector<RtpHeaderExtension>&
1135WebRtcVideoEngine::rtp_header_extensions() const {
1136 return rtp_header_extensions_;
1137}
1138
1139void WebRtcVideoEngine::SetLogging(int min_sev, const char* filter) {
1140 // if min_sev == -1, we keep the current log level.
1141 if (min_sev >= 0) {
1142 SetTraceFilter(SeverityToFilter(min_sev));
1143 }
1144 SetTraceOptions(filter);
1145}
1146
1147int WebRtcVideoEngine::GetLastEngineError() {
1148 return vie_wrapper_->error();
1149}
1150
1151// Checks to see whether we comprehend and could receive a particular codec
1152bool WebRtcVideoEngine::FindCodec(const VideoCodec& in) {
1153 for (int i = 0; i < ARRAY_SIZE(kVideoFormats); ++i) {
1154 const VideoFormat fmt(kVideoFormats[i]);
1155 if ((in.width == 0 && in.height == 0) ||
1156 (fmt.width == in.width && fmt.height == in.height)) {
1157 if (encoder_factory_) {
1158 const std::vector<WebRtcVideoEncoderFactory::VideoCodec>& codecs =
1159 encoder_factory_->codecs();
1160 for (size_t j = 0; j < codecs.size(); ++j) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001161 VideoCodec codec(GetExternalVideoPayloadType(static_cast<int>(j)),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001162 codecs[j].name, 0, 0, 0, 0);
1163 if (codec.Matches(in))
1164 return true;
1165 }
1166 }
1167 for (size_t j = 0; j < ARRAY_SIZE(kVideoCodecPrefs); ++j) {
1168 VideoCodec codec(kVideoCodecPrefs[j].payload_type,
1169 kVideoCodecPrefs[j].name, 0, 0, 0, 0);
1170 if (codec.Matches(in)) {
1171 return true;
1172 }
1173 }
1174 }
1175 }
1176 return false;
1177}
1178
1179// Given the requested codec, returns true if we can send that codec type and
1180// updates out with the best quality we could send for that codec. If current is
1181// not empty, we constrain out so that its aspect ratio matches current's.
1182bool WebRtcVideoEngine::CanSendCodec(const VideoCodec& requested,
1183 const VideoCodec& current,
1184 VideoCodec* out) {
1185 if (!out) {
1186 return false;
1187 }
1188
1189 std::vector<VideoCodec>::const_iterator local_max;
1190 for (local_max = video_codecs_.begin();
1191 local_max < video_codecs_.end();
1192 ++local_max) {
1193 // First match codecs by payload type
1194 if (!requested.Matches(*local_max)) {
1195 continue;
1196 }
1197
1198 out->id = requested.id;
1199 out->name = requested.name;
1200 out->preference = requested.preference;
1201 out->params = requested.params;
1202 out->framerate = talk_base::_min(requested.framerate, local_max->framerate);
1203 out->width = 0;
1204 out->height = 0;
1205 out->params = requested.params;
1206 out->feedback_params = requested.feedback_params;
1207
1208 if (0 == requested.width && 0 == requested.height) {
1209 // Special case with resolution 0. The channel should not send frames.
1210 return true;
1211 } else if (0 == requested.width || 0 == requested.height) {
1212 // 0xn and nx0 are invalid resolutions.
1213 return false;
1214 }
1215
1216 // Pick the best quality that is within their and our bounds and has the
1217 // correct aspect ratio.
1218 for (int j = 0; j < ARRAY_SIZE(kVideoFormats); ++j) {
1219 const VideoFormat format(kVideoFormats[j]);
1220
1221 // Skip any format that is larger than the local or remote maximums, or
1222 // smaller than the current best match
1223 if (format.width > requested.width || format.height > requested.height ||
1224 format.width > local_max->width ||
1225 (format.width < out->width && format.height < out->height)) {
1226 continue;
1227 }
1228
1229 bool better = false;
1230
1231 // Check any further constraints on this prospective format
1232 if (!out->width || !out->height) {
1233 // If we don't have any matches yet, this is the best so far.
1234 better = true;
1235 } else if (current.width && current.height) {
1236 // current is set so format must match its ratio exactly.
1237 better =
1238 (format.width * current.height == format.height * current.width);
1239 } else {
1240 // Prefer closer aspect ratios i.e
1241 // format.aspect - requested.aspect < out.aspect - requested.aspect
1242 better = abs(format.width * requested.height * out->height -
1243 requested.width * format.height * out->height) <
1244 abs(out->width * format.height * requested.height -
1245 requested.width * format.height * out->height);
1246 }
1247
1248 if (better) {
1249 out->width = format.width;
1250 out->height = format.height;
1251 }
1252 }
1253 if (out->width > 0) {
1254 return true;
1255 }
1256 }
1257 return false;
1258}
1259
1260static void ConvertToCricketVideoCodec(
1261 const webrtc::VideoCodec& in_codec, VideoCodec* out_codec) {
1262 out_codec->id = in_codec.plType;
1263 out_codec->name = in_codec.plName;
1264 out_codec->width = in_codec.width;
1265 out_codec->height = in_codec.height;
1266 out_codec->framerate = in_codec.maxFramerate;
1267 out_codec->SetParam(kCodecParamMinBitrate, in_codec.minBitrate);
1268 out_codec->SetParam(kCodecParamMaxBitrate, in_codec.maxBitrate);
buildbot@webrtc.orged97bb02014-05-07 11:15:20 +00001269 out_codec->SetParam(kCodecParamStartBitrate, in_codec.startBitrate);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001270 if (in_codec.qpMax) {
1271 out_codec->SetParam(kCodecParamMaxQuantization, in_codec.qpMax);
1272 }
1273}
1274
1275bool WebRtcVideoEngine::ConvertFromCricketVideoCodec(
1276 const VideoCodec& in_codec, webrtc::VideoCodec* out_codec) {
1277 bool found = false;
1278 int ncodecs = vie_wrapper_->codec()->NumberOfCodecs();
1279 for (int i = 0; i < ncodecs; ++i) {
1280 if (vie_wrapper_->codec()->GetCodec(i, *out_codec) == 0 &&
1281 _stricmp(in_codec.name.c_str(), out_codec->plName) == 0) {
1282 found = true;
1283 break;
1284 }
1285 }
1286
1287 // If not found, check if this is supported by external encoder factory.
1288 if (!found && encoder_factory_) {
1289 const std::vector<WebRtcVideoEncoderFactory::VideoCodec>& codecs =
1290 encoder_factory_->codecs();
1291 for (size_t i = 0; i < codecs.size(); ++i) {
1292 if (_stricmp(in_codec.name.c_str(), codecs[i].name.c_str()) == 0) {
1293 out_codec->codecType = codecs[i].type;
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001294 out_codec->plType = GetExternalVideoPayloadType(static_cast<int>(i));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001295 talk_base::strcpyn(out_codec->plName, sizeof(out_codec->plName),
1296 codecs[i].name.c_str(), codecs[i].name.length());
1297 found = true;
1298 break;
1299 }
1300 }
1301 }
1302
buildbot@webrtc.orgdd4742a2014-05-07 14:50:35 +00001303 // Is this an RTX codec? Handled separately here since webrtc doesn't handle
1304 // them as webrtc::VideoCodec internally.
1305 if (!found && _stricmp(in_codec.name.c_str(), kRtxCodecName) == 0) {
1306 talk_base::strcpyn(out_codec->plName, sizeof(out_codec->plName),
1307 in_codec.name.c_str(), in_codec.name.length());
1308 out_codec->plType = in_codec.id;
1309 found = true;
1310 }
1311
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001312 if (!found) {
1313 LOG(LS_ERROR) << "invalid codec type";
1314 return false;
1315 }
1316
1317 if (in_codec.id != 0)
1318 out_codec->plType = in_codec.id;
1319
1320 if (in_codec.width != 0)
1321 out_codec->width = in_codec.width;
1322
1323 if (in_codec.height != 0)
1324 out_codec->height = in_codec.height;
1325
1326 if (in_codec.framerate != 0)
1327 out_codec->maxFramerate = in_codec.framerate;
1328
1329 // Convert bitrate parameters.
1330 int max_bitrate = kMaxVideoBitrate;
1331 int min_bitrate = kMinVideoBitrate;
1332 int start_bitrate = kStartVideoBitrate;
1333
1334 in_codec.GetParam(kCodecParamMinBitrate, &min_bitrate);
1335 in_codec.GetParam(kCodecParamMaxBitrate, &max_bitrate);
buildbot@webrtc.orged97bb02014-05-07 11:15:20 +00001336 in_codec.GetParam(kCodecParamStartBitrate, &start_bitrate);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001337
1338 if (max_bitrate < min_bitrate) {
1339 return false;
1340 }
1341 start_bitrate = talk_base::_max(start_bitrate, min_bitrate);
1342 start_bitrate = talk_base::_min(start_bitrate, max_bitrate);
1343
1344 out_codec->minBitrate = min_bitrate;
1345 out_codec->startBitrate = start_bitrate;
1346 out_codec->maxBitrate = max_bitrate;
1347
1348 // Convert general codec parameters.
1349 int max_quantization = 0;
1350 if (in_codec.GetParam(kCodecParamMaxQuantization, &max_quantization)) {
1351 if (max_quantization < 0) {
1352 return false;
1353 }
1354 out_codec->qpMax = max_quantization;
1355 }
1356 return true;
1357}
1358
1359void WebRtcVideoEngine::RegisterChannel(WebRtcVideoMediaChannel *channel) {
1360 talk_base::CritScope cs(&channels_crit_);
1361 channels_.push_back(channel);
1362}
1363
1364void WebRtcVideoEngine::UnregisterChannel(WebRtcVideoMediaChannel *channel) {
1365 talk_base::CritScope cs(&channels_crit_);
1366 channels_.erase(std::remove(channels_.begin(), channels_.end(), channel),
1367 channels_.end());
1368}
1369
1370bool WebRtcVideoEngine::SetVoiceEngine(WebRtcVoiceEngine* voice_engine) {
1371 if (initialized_) {
1372 LOG(LS_WARNING) << "SetVoiceEngine can not be called after Init";
1373 return false;
1374 }
1375 voice_engine_ = voice_engine;
1376 return true;
1377}
1378
1379bool WebRtcVideoEngine::EnableTimedRender() {
1380 if (initialized_) {
1381 LOG(LS_WARNING) << "EnableTimedRender can not be called after Init";
1382 return false;
1383 }
1384 render_module_.reset(webrtc::VideoRender::CreateVideoRender(0, NULL,
1385 false, webrtc::kRenderExternal));
1386 return true;
1387}
1388
1389void WebRtcVideoEngine::SetTraceFilter(int filter) {
1390 tracing_->SetTraceFilter(filter);
1391}
1392
1393// See https://sites.google.com/a/google.com/wavelet/
1394// Home/Magic-Flute--RTC-Engine-/Magic-Flute-Command-Line-Parameters
1395// for all supported command line setttings.
1396void WebRtcVideoEngine::SetTraceOptions(const std::string& options) {
1397 // Set WebRTC trace file.
1398 std::vector<std::string> opts;
1399 talk_base::tokenize(options, ' ', '"', '"', &opts);
1400 std::vector<std::string>::iterator tracefile =
1401 std::find(opts.begin(), opts.end(), "tracefile");
1402 if (tracefile != opts.end() && ++tracefile != opts.end()) {
1403 // Write WebRTC debug output (at same loglevel) to file
1404 if (tracing_->SetTraceFile(tracefile->c_str()) == -1) {
1405 LOG_RTCERR1(SetTraceFile, *tracefile);
1406 }
1407 }
1408}
1409
1410static void AddDefaultFeedbackParams(VideoCodec* codec) {
1411 const FeedbackParam kFir(kRtcpFbParamCcm, kRtcpFbCcmParamFir);
1412 codec->AddFeedbackParam(kFir);
1413 const FeedbackParam kNack(kRtcpFbParamNack, kParamValueEmpty);
1414 codec->AddFeedbackParam(kNack);
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001415 const FeedbackParam kPli(kRtcpFbParamNack, kRtcpFbNackParamPli);
1416 codec->AddFeedbackParam(kPli);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001417 const FeedbackParam kRemb(kRtcpFbParamRemb, kParamValueEmpty);
1418 codec->AddFeedbackParam(kRemb);
1419}
1420
1421// Rebuilds the codec list to be only those that are less intensive
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00001422// than the specified codec. Prefers internal codec over external with
1423// higher preference field.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001424bool WebRtcVideoEngine::RebuildCodecList(const VideoCodec& in_codec) {
1425 if (!FindCodec(in_codec))
1426 return false;
1427
1428 video_codecs_.clear();
1429
1430 bool found = false;
mallinath@webrtc.org1112c302013-09-23 20:34:45 +00001431 std::set<std::string> internal_codec_names;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001432 for (size_t i = 0; i < ARRAY_SIZE(kVideoCodecPrefs); ++i) {
1433 const VideoCodecPref& pref(kVideoCodecPrefs[i]);
1434 if (!found)
1435 found = (in_codec.name == pref.name);
mallinath@webrtc.org1112c302013-09-23 20:34:45 +00001436 if (found) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001437 VideoCodec codec(pref.payload_type, pref.name,
1438 in_codec.width, in_codec.height, in_codec.framerate,
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001439 static_cast<int>(ARRAY_SIZE(kVideoCodecPrefs) - i));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001440 if (_stricmp(kVp8PayloadName, codec.name.c_str()) == 0) {
1441 AddDefaultFeedbackParams(&codec);
1442 }
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00001443 if (pref.associated_payload_type != -1) {
1444 codec.SetParam(kCodecParamAssociatedPayloadType,
1445 pref.associated_payload_type);
1446 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001447 video_codecs_.push_back(codec);
mallinath@webrtc.org1112c302013-09-23 20:34:45 +00001448 internal_codec_names.insert(codec.name);
1449 }
1450 }
1451 if (encoder_factory_) {
1452 const std::vector<WebRtcVideoEncoderFactory::VideoCodec>& codecs =
1453 encoder_factory_->codecs();
1454 for (size_t i = 0; i < codecs.size(); ++i) {
1455 bool is_internal_codec = internal_codec_names.find(codecs[i].name) !=
1456 internal_codec_names.end();
1457 if (!is_internal_codec) {
1458 if (!found)
1459 found = (in_codec.name == codecs[i].name);
1460 VideoCodec codec(
1461 GetExternalVideoPayloadType(static_cast<int>(i)),
1462 codecs[i].name,
1463 codecs[i].max_width,
1464 codecs[i].max_height,
1465 codecs[i].max_fps,
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00001466 // Use negative preference on external codec to ensure the internal
1467 // codec is preferred.
1468 static_cast<int>(0 - i));
mallinath@webrtc.org1112c302013-09-23 20:34:45 +00001469 AddDefaultFeedbackParams(&codec);
1470 video_codecs_.push_back(codec);
1471 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001472 }
1473 }
1474 ASSERT(found);
1475 return true;
1476}
1477
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001478// Ignore spammy trace messages, mostly from the stats API when we haven't
1479// gotten RTCP info yet from the remote side.
1480bool WebRtcVideoEngine::ShouldIgnoreTrace(const std::string& trace) {
1481 static const char* const kTracesToIgnore[] = {
1482 NULL
1483 };
1484 for (const char* const* p = kTracesToIgnore; *p; ++p) {
1485 if (trace.find(*p) == 0) {
1486 return true;
1487 }
1488 }
1489 return false;
1490}
1491
1492int WebRtcVideoEngine::GetNumOfChannels() {
1493 talk_base::CritScope cs(&channels_crit_);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001494 return static_cast<int>(channels_.size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001495}
1496
1497void WebRtcVideoEngine::Print(webrtc::TraceLevel level, const char* trace,
1498 int length) {
1499 talk_base::LoggingSeverity sev = talk_base::LS_VERBOSE;
1500 if (level == webrtc::kTraceError || level == webrtc::kTraceCritical)
1501 sev = talk_base::LS_ERROR;
1502 else if (level == webrtc::kTraceWarning)
1503 sev = talk_base::LS_WARNING;
1504 else if (level == webrtc::kTraceStateInfo || level == webrtc::kTraceInfo)
1505 sev = talk_base::LS_INFO;
1506 else if (level == webrtc::kTraceTerseInfo)
1507 sev = talk_base::LS_INFO;
1508
1509 // Skip past boilerplate prefix text
1510 if (length < 72) {
1511 std::string msg(trace, length);
1512 LOG(LS_ERROR) << "Malformed webrtc log message: ";
1513 LOG_V(sev) << msg;
1514 } else {
1515 std::string msg(trace + 71, length - 72);
1516 if (!ShouldIgnoreTrace(msg) &&
1517 (!voice_engine_ || !voice_engine_->ShouldIgnoreTrace(msg))) {
1518 LOG_V(sev) << "webrtc: " << msg;
1519 }
1520 }
1521}
1522
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001523webrtc::VideoDecoder* WebRtcVideoEngine::CreateExternalDecoder(
1524 webrtc::VideoCodecType type) {
1525 if (decoder_factory_ == NULL) {
1526 return NULL;
1527 }
1528 return decoder_factory_->CreateVideoDecoder(type);
1529}
1530
1531void WebRtcVideoEngine::DestroyExternalDecoder(webrtc::VideoDecoder* decoder) {
1532 ASSERT(decoder_factory_ != NULL);
1533 if (decoder_factory_ == NULL)
1534 return;
1535 decoder_factory_->DestroyVideoDecoder(decoder);
1536}
1537
1538webrtc::VideoEncoder* WebRtcVideoEngine::CreateExternalEncoder(
1539 webrtc::VideoCodecType type) {
1540 if (encoder_factory_ == NULL) {
1541 return NULL;
1542 }
1543 return encoder_factory_->CreateVideoEncoder(type);
1544}
1545
1546void WebRtcVideoEngine::DestroyExternalEncoder(webrtc::VideoEncoder* encoder) {
1547 ASSERT(encoder_factory_ != NULL);
1548 if (encoder_factory_ == NULL)
1549 return;
1550 encoder_factory_->DestroyVideoEncoder(encoder);
1551}
1552
1553bool WebRtcVideoEngine::IsExternalEncoderCodecType(
1554 webrtc::VideoCodecType type) const {
1555 if (!encoder_factory_)
1556 return false;
1557 const std::vector<WebRtcVideoEncoderFactory::VideoCodec>& codecs =
1558 encoder_factory_->codecs();
1559 std::vector<WebRtcVideoEncoderFactory::VideoCodec>::const_iterator it;
1560 for (it = codecs.begin(); it != codecs.end(); ++it) {
1561 if (it->type == type)
1562 return true;
1563 }
1564 return false;
1565}
1566
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001567void WebRtcVideoEngine::SetExternalDecoderFactory(
1568 WebRtcVideoDecoderFactory* decoder_factory) {
1569 decoder_factory_ = decoder_factory;
1570}
1571
1572void WebRtcVideoEngine::SetExternalEncoderFactory(
1573 WebRtcVideoEncoderFactory* encoder_factory) {
1574 if (encoder_factory_ == encoder_factory)
1575 return;
1576
1577 if (encoder_factory_) {
1578 encoder_factory_->RemoveObserver(this);
1579 }
1580 encoder_factory_ = encoder_factory;
1581 if (encoder_factory_) {
1582 encoder_factory_->AddObserver(this);
1583 }
1584
1585 // Invoke OnCodecAvailable() here in case the list of codecs is already
1586 // available when the encoder factory is installed. If not the encoder
1587 // factory will invoke the callback later when the codecs become available.
1588 OnCodecsAvailable();
1589}
1590
1591void WebRtcVideoEngine::OnCodecsAvailable() {
1592 // Rebuild codec list while reapplying the current default codec format.
1593 VideoCodec max_codec(kVideoCodecPrefs[0].payload_type,
1594 kVideoCodecPrefs[0].name,
1595 video_codecs_[0].width,
1596 video_codecs_[0].height,
1597 video_codecs_[0].framerate,
1598 0);
1599 if (!RebuildCodecList(max_codec)) {
1600 LOG(LS_ERROR) << "Failed to initialize list of supported codec types";
1601 }
1602}
1603
1604// WebRtcVideoMediaChannel
1605
1606WebRtcVideoMediaChannel::WebRtcVideoMediaChannel(
1607 WebRtcVideoEngine* engine,
1608 VoiceMediaChannel* channel)
1609 : engine_(engine),
1610 voice_channel_(channel),
1611 vie_channel_(-1),
1612 nack_enabled_(true),
1613 remb_enabled_(false),
1614 render_started_(false),
1615 first_receive_ssrc_(0),
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001616 num_unsignalled_recv_channels_(0),
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00001617 send_rtx_type_(-1),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001618 send_red_type_(-1),
1619 send_fec_type_(-1),
1620 send_min_bitrate_(kMinVideoBitrate),
1621 send_start_bitrate_(kStartVideoBitrate),
1622 send_max_bitrate_(kMaxVideoBitrate),
1623 sending_(false),
1624 ratio_w_(0),
1625 ratio_h_(0) {
1626 engine->RegisterChannel(this);
1627}
1628
1629bool WebRtcVideoMediaChannel::Init() {
1630 const uint32 ssrc_key = 0;
1631 return CreateChannel(ssrc_key, MD_SENDRECV, &vie_channel_);
1632}
1633
1634WebRtcVideoMediaChannel::~WebRtcVideoMediaChannel() {
1635 const bool send = false;
1636 SetSend(send);
1637 const bool render = false;
1638 SetRender(render);
1639
1640 while (!send_channels_.empty()) {
1641 if (!DeleteSendChannel(send_channels_.begin()->first)) {
1642 LOG(LS_ERROR) << "Unable to delete channel with ssrc key "
1643 << send_channels_.begin()->first;
1644 ASSERT(false);
1645 break;
1646 }
1647 }
1648
1649 // Remove all receive streams and the default channel.
1650 while (!recv_channels_.empty()) {
henrike@webrtc.org6e3dbc22014-03-25 17:09:47 +00001651 RemoveRecvStreamInternal(recv_channels_.begin()->first);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001652 }
1653
1654 // Unregister the channel from the engine.
1655 engine()->UnregisterChannel(this);
1656 if (worker_thread()) {
1657 worker_thread()->Clear(this);
1658 }
1659}
1660
1661bool WebRtcVideoMediaChannel::SetRecvCodecs(
1662 const std::vector<VideoCodec>& codecs) {
1663 receive_codecs_.clear();
buildbot@webrtc.orgdd4742a2014-05-07 14:50:35 +00001664 associated_payload_types_.clear();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001665 for (std::vector<VideoCodec>::const_iterator iter = codecs.begin();
1666 iter != codecs.end(); ++iter) {
1667 if (engine()->FindCodec(*iter)) {
1668 webrtc::VideoCodec wcodec;
1669 if (engine()->ConvertFromCricketVideoCodec(*iter, &wcodec)) {
1670 receive_codecs_.push_back(wcodec);
buildbot@webrtc.orgdd4742a2014-05-07 14:50:35 +00001671 int apt;
1672 if (iter->GetParam(cricket::kCodecParamAssociatedPayloadType, &apt)) {
1673 associated_payload_types_[wcodec.plType] = apt;
1674 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001675 }
1676 } else {
1677 LOG(LS_INFO) << "Unknown codec " << iter->name;
1678 return false;
1679 }
1680 }
1681
1682 for (RecvChannelMap::iterator it = recv_channels_.begin();
1683 it != recv_channels_.end(); ++it) {
1684 if (!SetReceiveCodecs(it->second))
1685 return false;
1686 }
1687 return true;
1688}
1689
1690bool WebRtcVideoMediaChannel::SetSendCodecs(
1691 const std::vector<VideoCodec>& codecs) {
1692 // Match with local video codec list.
1693 std::vector<webrtc::VideoCodec> send_codecs;
1694 VideoCodec checked_codec;
1695 VideoCodec current; // defaults to 0x0
1696 if (sending_) {
1697 ConvertToCricketVideoCodec(*send_codec_, &current);
1698 }
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00001699 std::map<int, int> primary_rtx_pt_mapping;
wu@webrtc.org05e7b442014-04-01 17:44:24 +00001700 bool nack_enabled = nack_enabled_;
1701 bool remb_enabled = remb_enabled_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001702 for (std::vector<VideoCodec>::const_iterator iter = codecs.begin();
1703 iter != codecs.end(); ++iter) {
1704 if (_stricmp(iter->name.c_str(), kRedPayloadName) == 0) {
1705 send_red_type_ = iter->id;
1706 } else if (_stricmp(iter->name.c_str(), kFecPayloadName) == 0) {
1707 send_fec_type_ = iter->id;
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00001708 } else if (_stricmp(iter->name.c_str(), kRtxCodecName) == 0) {
1709 int rtx_type = iter->id;
1710 int rtx_primary_type = -1;
1711 if (iter->GetParam(kCodecParamAssociatedPayloadType, &rtx_primary_type)) {
1712 primary_rtx_pt_mapping[rtx_primary_type] = rtx_type;
1713 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001714 } else if (engine()->CanSendCodec(*iter, current, &checked_codec)) {
1715 webrtc::VideoCodec wcodec;
1716 if (engine()->ConvertFromCricketVideoCodec(checked_codec, &wcodec)) {
1717 if (send_codecs.empty()) {
wu@webrtc.org05e7b442014-04-01 17:44:24 +00001718 nack_enabled = IsNackEnabled(checked_codec);
1719 remb_enabled = IsRembEnabled(checked_codec);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001720 }
1721 send_codecs.push_back(wcodec);
1722 }
1723 } else {
1724 LOG(LS_WARNING) << "Unknown codec " << iter->name;
1725 }
1726 }
1727
1728 // Fail if we don't have a match.
1729 if (send_codecs.empty()) {
1730 LOG(LS_WARNING) << "No matching codecs available";
1731 return false;
1732 }
1733
1734 // Recv protection.
wu@webrtc.org05e7b442014-04-01 17:44:24 +00001735 // Do not update if the status is same as previously configured.
1736 if (nack_enabled_ != nack_enabled) {
1737 for (RecvChannelMap::iterator it = recv_channels_.begin();
1738 it != recv_channels_.end(); ++it) {
1739 int channel_id = it->second->channel_id();
1740 if (!SetNackFec(channel_id, send_red_type_, send_fec_type_,
1741 nack_enabled)) {
1742 return false;
1743 }
1744 if (engine_->vie()->rtp()->SetRembStatus(channel_id,
1745 kNotSending,
1746 remb_enabled_) != 0) {
1747 LOG_RTCERR3(SetRembStatus, channel_id, kNotSending, remb_enabled_);
1748 return false;
1749 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001750 }
wu@webrtc.org05e7b442014-04-01 17:44:24 +00001751 nack_enabled_ = nack_enabled;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001752 }
1753
1754 // Send settings.
wu@webrtc.org05e7b442014-04-01 17:44:24 +00001755 // Do not update if the status is same as previously configured.
1756 if (remb_enabled_ != remb_enabled) {
1757 for (SendChannelMap::iterator iter = send_channels_.begin();
1758 iter != send_channels_.end(); ++iter) {
1759 int channel_id = iter->second->channel_id();
1760 if (!SetNackFec(channel_id, send_red_type_, send_fec_type_,
1761 nack_enabled_)) {
1762 return false;
1763 }
1764 if (engine_->vie()->rtp()->SetRembStatus(channel_id,
1765 remb_enabled,
1766 remb_enabled) != 0) {
1767 LOG_RTCERR3(SetRembStatus, channel_id, remb_enabled, remb_enabled);
1768 return false;
1769 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001770 }
wu@webrtc.org05e7b442014-04-01 17:44:24 +00001771 remb_enabled_ = remb_enabled;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001772 }
1773
1774 // Select the first matched codec.
1775 webrtc::VideoCodec& codec(send_codecs[0]);
1776
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00001777 // Set RTX payload type if primary now active. This value will be used in
1778 // SetSendCodec.
1779 std::map<int, int>::const_iterator rtx_it =
1780 primary_rtx_pt_mapping.find(static_cast<int>(codec.plType));
1781 if (rtx_it != primary_rtx_pt_mapping.end()) {
1782 send_rtx_type_ = rtx_it->second;
1783 }
1784
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001785 if (!SetSendCodec(
1786 codec, codec.minBitrate, codec.startBitrate, codec.maxBitrate)) {
1787 return false;
1788 }
1789
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001790 LogSendCodecChange("SetSendCodecs()");
1791
1792 return true;
1793}
1794
1795bool WebRtcVideoMediaChannel::GetSendCodec(VideoCodec* send_codec) {
1796 if (!send_codec_) {
1797 return false;
1798 }
1799 ConvertToCricketVideoCodec(*send_codec_, send_codec);
1800 return true;
1801}
1802
1803bool WebRtcVideoMediaChannel::SetSendStreamFormat(uint32 ssrc,
1804 const VideoFormat& format) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001805 WebRtcVideoChannelSendInfo* send_channel = GetSendChannel(ssrc);
1806 if (!send_channel) {
1807 LOG(LS_ERROR) << "The specified ssrc " << ssrc << " is not in use.";
1808 return false;
1809 }
1810 send_channel->set_video_format(format);
1811 return true;
1812}
1813
1814bool WebRtcVideoMediaChannel::SetRender(bool render) {
1815 if (render == render_started_) {
1816 return true; // no action required
1817 }
1818
1819 bool ret = true;
1820 for (RecvChannelMap::iterator it = recv_channels_.begin();
1821 it != recv_channels_.end(); ++it) {
1822 if (render) {
1823 if (engine()->vie()->render()->StartRender(
1824 it->second->channel_id()) != 0) {
1825 LOG_RTCERR1(StartRender, it->second->channel_id());
1826 ret = false;
1827 }
1828 } else {
1829 if (engine()->vie()->render()->StopRender(
1830 it->second->channel_id()) != 0) {
1831 LOG_RTCERR1(StopRender, it->second->channel_id());
1832 ret = false;
1833 }
1834 }
1835 }
1836 if (ret) {
1837 render_started_ = render;
1838 }
1839
1840 return ret;
1841}
1842
1843bool WebRtcVideoMediaChannel::SetSend(bool send) {
1844 if (!HasReadySendChannels() && send) {
1845 LOG(LS_ERROR) << "No stream added";
1846 return false;
1847 }
1848 if (send == sending()) {
1849 return true; // No action required.
1850 }
1851
1852 if (send) {
1853 // We've been asked to start sending.
1854 // SetSendCodecs must have been called already.
1855 if (!send_codec_) {
1856 return false;
1857 }
1858 // Start send now.
1859 if (!StartSend()) {
1860 return false;
1861 }
1862 } else {
1863 // We've been asked to stop sending.
1864 if (!StopSend()) {
1865 return false;
1866 }
1867 }
1868 sending_ = send;
1869
1870 return true;
1871}
1872
1873bool WebRtcVideoMediaChannel::AddSendStream(const StreamParams& sp) {
henrike@webrtc.org6e3dbc22014-03-25 17:09:47 +00001874 if (sp.first_ssrc() == 0) {
1875 LOG(LS_ERROR) << "AddSendStream with 0 ssrc is not supported.";
1876 return false;
1877 }
1878
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001879 LOG(LS_INFO) << "AddSendStream " << sp.ToString();
1880
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00001881 if (!IsOneSsrcStream(sp) && !IsSimulcastStream(sp)) {
1882 LOG(LS_ERROR) << "AddSendStream: bad local stream parameters";
1883 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001884 }
1885
1886 uint32 ssrc_key;
1887 if (!CreateSendChannelKey(sp.first_ssrc(), &ssrc_key)) {
1888 LOG(LS_ERROR) << "Trying to register duplicate ssrc: " << sp.first_ssrc();
1889 return false;
1890 }
1891 // If the default channel is already used for sending create a new channel
1892 // otherwise use the default channel for sending.
1893 int channel_id = -1;
1894 if (send_channels_[0]->stream_params() == NULL) {
1895 channel_id = vie_channel_;
1896 } else {
1897 if (!CreateChannel(ssrc_key, MD_SEND, &channel_id)) {
1898 LOG(LS_ERROR) << "AddSendStream: unable to create channel";
1899 return false;
1900 }
1901 }
1902 WebRtcVideoChannelSendInfo* send_channel = send_channels_[ssrc_key];
1903 // Set the send (local) SSRC.
1904 // If there are multiple send SSRCs, we can only set the first one here, and
1905 // the rest of the SSRC(s) need to be set after SetSendCodec has been called
1906 // (with a codec requires multiple SSRC(s)).
1907 if (engine()->vie()->rtp()->SetLocalSSRC(channel_id,
1908 sp.first_ssrc()) != 0) {
1909 LOG_RTCERR2(SetLocalSSRC, channel_id, sp.first_ssrc());
1910 return false;
1911 }
1912
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00001913 // Set the corresponding RTX SSRC.
1914 if (!SetLocalRtxSsrc(channel_id, sp, sp.first_ssrc(), 0)) {
1915 return false;
1916 }
1917
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001918 // Set RTCP CName.
1919 if (engine()->vie()->rtp()->SetRTCPCName(channel_id,
1920 sp.cname.c_str()) != 0) {
1921 LOG_RTCERR2(SetRTCPCName, channel_id, sp.cname.c_str());
1922 return false;
1923 }
1924
1925 // At this point the channel's local SSRC has been updated. If the channel is
1926 // the default channel make sure that all the receive channels are updated as
1927 // well. Receive channels have to have the same SSRC as the default channel in
1928 // order to send receiver reports with this SSRC.
1929 if (IsDefaultChannel(channel_id)) {
1930 for (RecvChannelMap::const_iterator it = recv_channels_.begin();
1931 it != recv_channels_.end(); ++it) {
1932 WebRtcVideoChannelRecvInfo* info = it->second;
1933 int channel_id = info->channel_id();
1934 if (engine()->vie()->rtp()->SetLocalSSRC(channel_id,
1935 sp.first_ssrc()) != 0) {
1936 LOG_RTCERR1(SetLocalSSRC, it->first);
1937 return false;
1938 }
1939 }
1940 }
1941
1942 send_channel->set_stream_params(sp);
1943
1944 // Reset send codec after stream parameters changed.
1945 if (send_codec_) {
1946 if (!SetSendCodec(send_channel, *send_codec_, send_min_bitrate_,
1947 send_start_bitrate_, send_max_bitrate_)) {
1948 return false;
1949 }
1950 LogSendCodecChange("SetSendStreamFormat()");
1951 }
1952
1953 if (sending_) {
1954 return StartSend(send_channel);
1955 }
1956 return true;
1957}
1958
1959bool WebRtcVideoMediaChannel::RemoveSendStream(uint32 ssrc) {
henrike@webrtc.org6e3dbc22014-03-25 17:09:47 +00001960 if (ssrc == 0) {
1961 LOG(LS_ERROR) << "RemoveSendStream with 0 ssrc is not supported.";
1962 return false;
1963 }
1964
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001965 uint32 ssrc_key;
1966 if (!GetSendChannelKey(ssrc, &ssrc_key)) {
1967 LOG(LS_WARNING) << "Try to remove stream with ssrc " << ssrc
1968 << " which doesn't exist.";
1969 return false;
1970 }
1971 WebRtcVideoChannelSendInfo* send_channel = send_channels_[ssrc_key];
1972 int channel_id = send_channel->channel_id();
1973 if (IsDefaultChannel(channel_id) && (send_channel->stream_params() == NULL)) {
1974 // Default channel will still exist. However, if stream_params() is NULL
1975 // there is no stream to remove.
1976 return false;
1977 }
1978 if (sending_) {
1979 StopSend(send_channel);
1980 }
1981
1982 const WebRtcVideoChannelSendInfo::EncoderMap& encoder_map =
1983 send_channel->registered_encoders();
1984 for (WebRtcVideoChannelSendInfo::EncoderMap::const_iterator it =
1985 encoder_map.begin(); it != encoder_map.end(); ++it) {
1986 if (engine()->vie()->ext_codec()->DeRegisterExternalSendCodec(
1987 channel_id, it->first) != 0) {
1988 LOG_RTCERR1(DeregisterEncoderObserver, channel_id);
1989 }
1990 engine()->DestroyExternalEncoder(it->second);
1991 }
1992 send_channel->ClearRegisteredEncoders();
1993
1994 // The receive channels depend on the default channel, recycle it instead.
1995 if (IsDefaultChannel(channel_id)) {
1996 SetCapturer(GetDefaultChannelSsrc(), NULL);
1997 send_channel->ClearStreamParams();
1998 } else {
1999 return DeleteSendChannel(ssrc_key);
2000 }
2001 return true;
2002}
2003
2004bool WebRtcVideoMediaChannel::AddRecvStream(const StreamParams& sp) {
henrike@webrtc.org6e3dbc22014-03-25 17:09:47 +00002005 if (sp.first_ssrc() == 0) {
2006 LOG(LS_ERROR) << "AddRecvStream with 0 ssrc is not supported.";
2007 return false;
2008 }
2009
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002010 // TODO(zhurunz) Remove this once BWE works properly across different send
2011 // and receive channels.
2012 // Reuse default channel for recv stream in 1:1 call.
2013 if (!InConferenceMode() && first_receive_ssrc_ == 0) {
2014 LOG(LS_INFO) << "Recv stream " << sp.first_ssrc()
2015 << " reuse default channel #"
2016 << vie_channel_;
2017 first_receive_ssrc_ = sp.first_ssrc();
2018 if (render_started_) {
2019 if (engine()->vie()->render()->StartRender(vie_channel_) !=0) {
2020 LOG_RTCERR1(StartRender, vie_channel_);
2021 }
2022 }
2023 return true;
2024 }
2025
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002026 int channel_id = -1;
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00002027 RecvChannelMap::iterator channel_iterator =
2028 recv_channels_.find(sp.first_ssrc());
2029 if (channel_iterator == recv_channels_.end() &&
2030 first_receive_ssrc_ != sp.first_ssrc()) {
2031 // TODO(perkj): Implement recv media from multiple media SSRCs per stream.
2032 // NOTE: We have two SSRCs per stream when RTX is enabled.
2033 if (!IsOneSsrcStream(sp)) {
2034 LOG(LS_ERROR) << "WebRtcVideoMediaChannel supports one primary SSRC per"
2035 << " stream and one FID SSRC per primary SSRC.";
2036 return false;
2037 }
2038
2039 // Create a new channel for receiving video data.
2040 // In order to get the bandwidth estimation work fine for
2041 // receive only channels, we connect all receiving channels
2042 // to our master send channel.
2043 if (!CreateChannel(sp.first_ssrc(), MD_RECV, &channel_id)) {
2044 return false;
2045 }
2046 } else {
2047 // Already exists.
2048 if (first_receive_ssrc_ == sp.first_ssrc()) {
2049 return false;
2050 }
2051 // Early receive added channel.
2052 channel_id = (*channel_iterator).second->channel_id();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002053 }
buildbot@webrtc.orgdd4742a2014-05-07 14:50:35 +00002054 channel_iterator = recv_channels_.find(sp.first_ssrc());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002055
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002056 // Set the corresponding RTX SSRC.
2057 uint32 rtx_ssrc;
2058 bool has_rtx = sp.GetFidSsrc(sp.first_ssrc(), &rtx_ssrc);
buildbot@webrtc.orgdd4742a2014-05-07 14:50:35 +00002059 if (has_rtx) {
2060 LOG(LS_INFO) << "Setting rtx ssrc " << rtx_ssrc << " for stream "
2061 << sp.first_ssrc();
2062 if (engine()->vie()->rtp()->SetRemoteSSRCType(
2063 channel_id, webrtc::kViEStreamTypeRtx, rtx_ssrc) != 0) {
2064 LOG_RTCERR3(SetRemoteSSRCType, channel_id, webrtc::kViEStreamTypeRtx,
2065 rtx_ssrc);
2066 return false;
2067 }
2068 rtx_to_primary_ssrc_[rtx_ssrc] = sp.first_ssrc();
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002069 }
2070
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002071 // Get the default renderer.
2072 VideoRenderer* default_renderer = NULL;
2073 if (InConferenceMode()) {
2074 // The recv_channels_ size start out being 1, so if it is two here this
2075 // is the first receive channel created (vie_channel_ is not used for
2076 // receiving in a conference call). This means that the renderer stored
2077 // inside vie_channel_ should be used for the just created channel.
2078 if (recv_channels_.size() == 2 &&
2079 recv_channels_.find(0) != recv_channels_.end()) {
2080 GetRenderer(0, &default_renderer);
2081 }
2082 }
2083
2084 // The first recv stream reuses the default renderer (if a default renderer
2085 // has been set).
2086 if (default_renderer) {
2087 SetRenderer(sp.first_ssrc(), default_renderer);
2088 }
2089
2090 LOG(LS_INFO) << "New video stream " << sp.first_ssrc()
2091 << " registered to VideoEngine channel #"
2092 << channel_id << " and connected to channel #" << vie_channel_;
2093
2094 return true;
2095}
2096
2097bool WebRtcVideoMediaChannel::RemoveRecvStream(uint32 ssrc) {
henrike@webrtc.org6e3dbc22014-03-25 17:09:47 +00002098 if (ssrc == 0) {
2099 LOG(LS_ERROR) << "RemoveRecvStream with 0 ssrc is not supported.";
2100 return false;
2101 }
2102 return RemoveRecvStreamInternal(ssrc);
2103}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002104
henrike@webrtc.org6e3dbc22014-03-25 17:09:47 +00002105bool WebRtcVideoMediaChannel::RemoveRecvStreamInternal(uint32 ssrc) {
2106 RecvChannelMap::iterator it = recv_channels_.find(ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002107 if (it == recv_channels_.end()) {
2108 // TODO(perkj): Remove this once BWE works properly across different send
2109 // and receive channels.
2110 // The default channel is reused for recv stream in 1:1 call.
2111 if (first_receive_ssrc_ == ssrc) {
2112 first_receive_ssrc_ = 0;
2113 // Need to stop the renderer and remove it since the render window can be
2114 // deleted after this.
2115 if (render_started_) {
2116 if (engine()->vie()->render()->StopRender(vie_channel_) !=0) {
2117 LOG_RTCERR1(StopRender, it->second->channel_id());
2118 }
2119 }
2120 recv_channels_[0]->SetRenderer(NULL);
2121 return true;
2122 }
2123 return false;
2124 }
2125 WebRtcVideoChannelRecvInfo* info = it->second;
buildbot@webrtc.orgdd4742a2014-05-07 14:50:35 +00002126
2127 // Remove any RTX SSRC mappings to this stream.
2128 SsrcMap::iterator rtx_it = rtx_to_primary_ssrc_.begin();
2129 while (rtx_it != rtx_to_primary_ssrc_.end()) {
2130 if (rtx_it->second == ssrc) {
2131 rtx_to_primary_ssrc_.erase(rtx_it++);
2132 } else {
2133 ++rtx_it;
2134 }
2135 }
2136
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002137 int channel_id = info->channel_id();
2138 if (engine()->vie()->render()->RemoveRenderer(channel_id) != 0) {
2139 LOG_RTCERR1(RemoveRenderer, channel_id);
2140 }
2141
2142 if (engine()->vie()->network()->DeregisterSendTransport(channel_id) !=0) {
2143 LOG_RTCERR1(DeRegisterSendTransport, channel_id);
2144 }
2145
2146 if (engine()->vie()->codec()->DeregisterDecoderObserver(
2147 channel_id) != 0) {
2148 LOG_RTCERR1(DeregisterDecoderObserver, channel_id);
2149 }
2150
2151 const WebRtcVideoChannelRecvInfo::DecoderMap& decoder_map =
2152 info->registered_decoders();
2153 for (WebRtcVideoChannelRecvInfo::DecoderMap::const_iterator it =
2154 decoder_map.begin(); it != decoder_map.end(); ++it) {
2155 if (engine()->vie()->ext_codec()->DeRegisterExternalReceiveCodec(
2156 channel_id, it->first) != 0) {
2157 LOG_RTCERR1(DeregisterDecoderObserver, channel_id);
2158 }
2159 engine()->DestroyExternalDecoder(it->second);
2160 }
2161 info->ClearRegisteredDecoders();
2162
2163 LOG(LS_INFO) << "Removing video stream " << ssrc
2164 << " with VideoEngine channel #"
2165 << channel_id;
wu@webrtc.org9caf2762013-12-11 18:25:07 +00002166 bool ret = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002167 if (engine()->vie()->base()->DeleteChannel(channel_id) == -1) {
2168 LOG_RTCERR1(DeleteChannel, channel_id);
wu@webrtc.org9caf2762013-12-11 18:25:07 +00002169 ret = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002170 }
2171 // Delete the WebRtcVideoChannelRecvInfo pointed to by it->second.
2172 delete info;
2173 recv_channels_.erase(it);
wu@webrtc.org9caf2762013-12-11 18:25:07 +00002174 return ret;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002175}
2176
2177bool WebRtcVideoMediaChannel::StartSend() {
2178 bool success = true;
2179 for (SendChannelMap::iterator iter = send_channels_.begin();
2180 iter != send_channels_.end(); ++iter) {
2181 WebRtcVideoChannelSendInfo* send_channel = iter->second;
2182 if (!StartSend(send_channel)) {
2183 success = false;
2184 }
2185 }
2186 return success;
2187}
2188
2189bool WebRtcVideoMediaChannel::StartSend(
2190 WebRtcVideoChannelSendInfo* send_channel) {
2191 const int channel_id = send_channel->channel_id();
2192 if (engine()->vie()->base()->StartSend(channel_id) != 0) {
2193 LOG_RTCERR1(StartSend, channel_id);
2194 return false;
2195 }
2196
2197 send_channel->set_sending(true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002198 return true;
2199}
2200
2201bool WebRtcVideoMediaChannel::StopSend() {
2202 bool success = true;
2203 for (SendChannelMap::iterator iter = send_channels_.begin();
2204 iter != send_channels_.end(); ++iter) {
2205 WebRtcVideoChannelSendInfo* send_channel = iter->second;
2206 if (!StopSend(send_channel)) {
2207 success = false;
2208 }
2209 }
2210 return success;
2211}
2212
2213bool WebRtcVideoMediaChannel::StopSend(
2214 WebRtcVideoChannelSendInfo* send_channel) {
2215 const int channel_id = send_channel->channel_id();
2216 if (engine()->vie()->base()->StopSend(channel_id) != 0) {
2217 LOG_RTCERR1(StopSend, channel_id);
2218 return false;
2219 }
2220 send_channel->set_sending(false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002221 return true;
2222}
2223
2224bool WebRtcVideoMediaChannel::SendIntraFrame() {
2225 bool success = true;
2226 for (SendChannelMap::iterator iter = send_channels_.begin();
2227 iter != send_channels_.end();
2228 ++iter) {
2229 WebRtcVideoChannelSendInfo* send_channel = iter->second;
2230 const int channel_id = send_channel->channel_id();
2231 if (engine()->vie()->codec()->SendKeyFrame(channel_id) != 0) {
2232 LOG_RTCERR1(SendKeyFrame, channel_id);
2233 success = false;
2234 }
2235 }
2236 return success;
2237}
2238
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002239bool WebRtcVideoMediaChannel::HasReadySendChannels() {
2240 return !send_channels_.empty() &&
2241 ((send_channels_.size() > 1) ||
2242 (send_channels_[0]->stream_params() != NULL));
2243}
2244
2245bool WebRtcVideoMediaChannel::GetSendChannelKey(uint32 local_ssrc,
2246 uint32* key) {
2247 *key = 0;
2248 // If a send channel is not ready to send it will not have local_ssrc
2249 // registered to it.
2250 if (!HasReadySendChannels()) {
2251 return false;
2252 }
2253 // The default channel is stored with key 0. The key therefore does not match
2254 // the SSRC associated with the default channel. Check if the SSRC provided
2255 // corresponds to the default channel's SSRC.
2256 if (local_ssrc == GetDefaultChannelSsrc()) {
2257 return true;
2258 }
2259 if (send_channels_.find(local_ssrc) == send_channels_.end()) {
2260 for (SendChannelMap::iterator iter = send_channels_.begin();
2261 iter != send_channels_.end(); ++iter) {
2262 WebRtcVideoChannelSendInfo* send_channel = iter->second;
2263 if (send_channel->has_ssrc(local_ssrc)) {
2264 *key = iter->first;
2265 return true;
2266 }
2267 }
2268 return false;
2269 }
2270 // The key was found in the above std::map::find call. This means that the
2271 // ssrc is the key.
2272 *key = local_ssrc;
2273 return true;
2274}
2275
2276WebRtcVideoChannelSendInfo* WebRtcVideoMediaChannel::GetSendChannel(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002277 uint32 local_ssrc) {
2278 uint32 key;
2279 if (!GetSendChannelKey(local_ssrc, &key)) {
2280 return NULL;
2281 }
2282 return send_channels_[key];
2283}
2284
2285bool WebRtcVideoMediaChannel::CreateSendChannelKey(uint32 local_ssrc,
2286 uint32* key) {
2287 if (GetSendChannelKey(local_ssrc, key)) {
2288 // If there is a key corresponding to |local_ssrc|, the SSRC is already in
2289 // use. SSRCs need to be unique in a session and at this point a duplicate
2290 // SSRC has been detected.
2291 return false;
2292 }
2293 if (send_channels_[0]->stream_params() == NULL) {
2294 // key should be 0 here as the default channel should be re-used whenever it
2295 // is not used.
2296 *key = 0;
2297 return true;
2298 }
2299 // SSRC is currently not in use and the default channel is already in use. Use
2300 // the SSRC as key since it is supposed to be unique in a session.
2301 *key = local_ssrc;
2302 return true;
2303}
2304
wu@webrtc.org24301a62013-12-13 19:17:43 +00002305int WebRtcVideoMediaChannel::GetSendChannelNum(VideoCapturer* capturer) {
2306 int num = 0;
2307 for (SendChannelMap::iterator iter = send_channels_.begin();
2308 iter != send_channels_.end(); ++iter) {
2309 WebRtcVideoChannelSendInfo* send_channel = iter->second;
2310 if (send_channel->video_capturer() == capturer) {
2311 ++num;
2312 }
2313 }
2314 return num;
2315}
2316
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002317uint32 WebRtcVideoMediaChannel::GetDefaultChannelSsrc() {
2318 WebRtcVideoChannelSendInfo* send_channel = send_channels_[0];
2319 const StreamParams* sp = send_channel->stream_params();
2320 if (sp == NULL) {
2321 // This happens if no send stream is currently registered.
2322 return 0;
2323 }
2324 return sp->first_ssrc();
2325}
2326
2327bool WebRtcVideoMediaChannel::DeleteSendChannel(uint32 ssrc_key) {
2328 if (send_channels_.find(ssrc_key) == send_channels_.end()) {
2329 return false;
2330 }
2331 WebRtcVideoChannelSendInfo* send_channel = send_channels_[ssrc_key];
wu@webrtc.org24301a62013-12-13 19:17:43 +00002332 MaybeDisconnectCapturer(send_channel->video_capturer());
henrike@webrtc.orgc7bec842014-03-12 19:53:43 +00002333 send_channel->set_video_capturer(NULL, engine()->vie());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002334
2335 int channel_id = send_channel->channel_id();
2336 int capture_id = send_channel->capture_id();
2337 if (engine()->vie()->codec()->DeregisterEncoderObserver(
2338 channel_id) != 0) {
2339 LOG_RTCERR1(DeregisterEncoderObserver, channel_id);
2340 }
2341
2342 // Destroy the external capture interface.
2343 if (engine()->vie()->capture()->DisconnectCaptureDevice(
2344 channel_id) != 0) {
2345 LOG_RTCERR1(DisconnectCaptureDevice, channel_id);
2346 }
2347 if (engine()->vie()->capture()->ReleaseCaptureDevice(
2348 capture_id) != 0) {
2349 LOG_RTCERR1(ReleaseCaptureDevice, capture_id);
2350 }
2351
2352 // The default channel is stored in both |send_channels_| and
2353 // |recv_channels_|. To make sure it is only deleted once from vie let the
2354 // delete call happen when tearing down |recv_channels_| and not here.
2355 if (!IsDefaultChannel(channel_id)) {
2356 engine_->vie()->base()->DeleteChannel(channel_id);
2357 }
2358 delete send_channel;
2359 send_channels_.erase(ssrc_key);
2360 return true;
2361}
2362
2363bool WebRtcVideoMediaChannel::RemoveCapturer(uint32 ssrc) {
2364 WebRtcVideoChannelSendInfo* send_channel = GetSendChannel(ssrc);
2365 if (!send_channel) {
2366 return false;
2367 }
2368 VideoCapturer* capturer = send_channel->video_capturer();
2369 if (capturer == NULL) {
2370 return false;
2371 }
wu@webrtc.org24301a62013-12-13 19:17:43 +00002372 MaybeDisconnectCapturer(capturer);
henrike@webrtc.orgc7bec842014-03-12 19:53:43 +00002373 send_channel->set_video_capturer(NULL, engine()->vie());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002374 const int64 timestamp = send_channel->local_stream_info()->time_stamp();
2375 if (send_codec_) {
2376 QueueBlackFrame(ssrc, timestamp, send_codec_->maxFramerate);
2377 }
2378 return true;
2379}
2380
2381bool WebRtcVideoMediaChannel::SetRenderer(uint32 ssrc,
2382 VideoRenderer* renderer) {
2383 if (recv_channels_.find(ssrc) == recv_channels_.end()) {
2384 // TODO(perkj): Remove this once BWE works properly across different send
2385 // and receive channels.
2386 // The default channel is reused for recv stream in 1:1 call.
2387 if (first_receive_ssrc_ == ssrc &&
2388 recv_channels_.find(0) != recv_channels_.end()) {
2389 LOG(LS_INFO) << "SetRenderer " << ssrc
2390 << " reuse default channel #"
2391 << vie_channel_;
2392 recv_channels_[0]->SetRenderer(renderer);
2393 return true;
2394 }
2395 return false;
2396 }
2397
2398 recv_channels_[ssrc]->SetRenderer(renderer);
2399 return true;
2400}
2401
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00002402bool WebRtcVideoMediaChannel::GetStats(const StatsOptions& options,
2403 VideoMediaInfo* info) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002404 // Get sender statistics and build VideoSenderInfo.
2405 unsigned int total_bitrate_sent = 0;
2406 unsigned int video_bitrate_sent = 0;
2407 unsigned int fec_bitrate_sent = 0;
2408 unsigned int nack_bitrate_sent = 0;
2409 unsigned int estimated_send_bandwidth = 0;
2410 unsigned int target_enc_bitrate = 0;
2411 if (send_codec_) {
2412 for (SendChannelMap::const_iterator iter = send_channels_.begin();
2413 iter != send_channels_.end(); ++iter) {
2414 WebRtcVideoChannelSendInfo* send_channel = iter->second;
2415 const int channel_id = send_channel->channel_id();
2416 VideoSenderInfo sinfo;
2417 const StreamParams* send_params = send_channel->stream_params();
2418 if (send_params == NULL) {
2419 // This should only happen if the default vie channel is not in use.
2420 // This can happen if no streams have ever been added or the stream
2421 // corresponding to the default channel has been removed. Note that
2422 // there may be non-default vie channels in use when this happen so
2423 // asserting send_channels_.size() == 1 is not correct and neither is
2424 // breaking out of the loop.
2425 ASSERT(channel_id == vie_channel_);
2426 continue;
2427 }
2428 unsigned int bytes_sent, packets_sent, bytes_recv, packets_recv;
2429 if (engine_->vie()->rtp()->GetRTPStatistics(channel_id, bytes_sent,
2430 packets_sent, bytes_recv,
2431 packets_recv) != 0) {
2432 LOG_RTCERR1(GetRTPStatistics, vie_channel_);
2433 continue;
2434 }
2435 WebRtcLocalStreamInfo* channel_stream_info =
2436 send_channel->local_stream_info();
2437
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002438 for (size_t i = 0; i < send_params->ssrcs.size(); ++i) {
2439 sinfo.add_ssrc(send_params->ssrcs[i]);
2440 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002441 sinfo.codec_name = send_codec_->plName;
2442 sinfo.bytes_sent = bytes_sent;
2443 sinfo.packets_sent = packets_sent;
2444 sinfo.packets_cached = -1;
2445 sinfo.packets_lost = -1;
2446 sinfo.fraction_lost = -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002447 sinfo.rtt_ms = -1;
wu@webrtc.org987f2c92014-03-28 16:22:19 +00002448
2449 VideoCapturer* video_capturer = send_channel->video_capturer();
2450 if (video_capturer) {
buildbot@webrtc.org0b53bd22014-05-06 17:12:36 +00002451 VideoFormat last_captured_frame_format;
wu@webrtc.org987f2c92014-03-28 16:22:19 +00002452 video_capturer->GetStats(&sinfo.adapt_frame_drops,
2453 &sinfo.effects_frame_drops,
buildbot@webrtc.org0b53bd22014-05-06 17:12:36 +00002454 &sinfo.capturer_frame_time,
2455 &last_captured_frame_format);
2456 sinfo.input_frame_width = last_captured_frame_format.width;
2457 sinfo.input_frame_height = last_captured_frame_format.height;
2458 } else {
2459 sinfo.input_frame_width = 0;
2460 sinfo.input_frame_height = 0;
wu@webrtc.org987f2c92014-03-28 16:22:19 +00002461 }
2462
2463 webrtc::VideoCodec vie_codec;
wu@webrtc.org987f2c92014-03-28 16:22:19 +00002464 if (!video_capturer || video_capturer->IsMuted()) {
2465 sinfo.send_frame_width = 0;
2466 sinfo.send_frame_height = 0;
2467 } else if (engine()->vie()->codec()->GetSendCodec(channel_id,
2468 vie_codec) == 0) {
2469 sinfo.send_frame_width = vie_codec.width;
2470 sinfo.send_frame_height = vie_codec.height;
2471 } else {
2472 sinfo.send_frame_width = -1;
2473 sinfo.send_frame_height = -1;
2474 LOG_RTCERR1(GetSendCodec, channel_id);
2475 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002476 sinfo.framerate_input = channel_stream_info->framerate();
2477 sinfo.framerate_sent = send_channel->encoder_observer()->framerate();
2478 sinfo.nominal_bitrate = send_channel->encoder_observer()->bitrate();
2479 sinfo.preferred_bitrate = send_max_bitrate_;
2480 sinfo.adapt_reason = send_channel->CurrentAdaptReason();
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002481 sinfo.capture_jitter_ms = -1;
2482 sinfo.avg_encode_ms = -1;
wu@webrtc.org9caf2762013-12-11 18:25:07 +00002483 sinfo.encode_usage_percent = -1;
2484 sinfo.capture_queue_delay_ms_per_s = -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002485
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002486 int capture_jitter_ms = 0;
2487 int avg_encode_time_ms = 0;
wu@webrtc.org9caf2762013-12-11 18:25:07 +00002488 int encode_usage_percent = 0;
2489 int capture_queue_delay_ms_per_s = 0;
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002490 if (engine()->vie()->base()->CpuOveruseMeasures(
wu@webrtc.org9caf2762013-12-11 18:25:07 +00002491 channel_id,
2492 &capture_jitter_ms,
2493 &avg_encode_time_ms,
2494 &encode_usage_percent,
2495 &capture_queue_delay_ms_per_s) == 0) {
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002496 sinfo.capture_jitter_ms = capture_jitter_ms;
2497 sinfo.avg_encode_ms = avg_encode_time_ms;
wu@webrtc.org9caf2762013-12-11 18:25:07 +00002498 sinfo.encode_usage_percent = encode_usage_percent;
2499 sinfo.capture_queue_delay_ms_per_s = capture_queue_delay_ms_per_s;
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002500 }
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002501
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00002502 webrtc::RtcpPacketTypeCounter rtcp_sent;
2503 webrtc::RtcpPacketTypeCounter rtcp_received;
2504 if (engine()->vie()->rtp()->GetRtcpPacketTypeCounters(
2505 channel_id, &rtcp_sent, &rtcp_received) == 0) {
2506 sinfo.firs_rcvd = rtcp_received.fir_packets;
2507 sinfo.plis_rcvd = rtcp_received.pli_packets;
2508 sinfo.nacks_rcvd = rtcp_received.nack_packets;
2509 } else {
2510 sinfo.firs_rcvd = -1;
2511 sinfo.plis_rcvd = -1;
2512 sinfo.nacks_rcvd = -1;
2513 LOG_RTCERR1(GetRtcpPacketTypeCounters, channel_id);
2514 }
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00002515
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002516 // Get received RTCP statistics for the sender (reported by the remote
2517 // client in a RTCP packet), if available.
2518 // It's not a fatal error if we can't, since RTCP may not have arrived
2519 // yet.
2520 webrtc::RtcpStatistics outgoing_stream_rtcp_stats;
2521 int outgoing_stream_rtt_ms;
2522
2523 if (engine_->vie()->rtp()->GetSendChannelRtcpStatistics(
2524 channel_id,
2525 outgoing_stream_rtcp_stats,
2526 outgoing_stream_rtt_ms) == 0) {
2527 // Convert Q8 to float.
2528 sinfo.packets_lost = outgoing_stream_rtcp_stats.cumulative_lost;
2529 sinfo.fraction_lost = static_cast<float>(
2530 outgoing_stream_rtcp_stats.fraction_lost) / (1 << 8);
2531 sinfo.rtt_ms = outgoing_stream_rtt_ms;
2532 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002533 info->senders.push_back(sinfo);
2534
2535 unsigned int channel_total_bitrate_sent = 0;
2536 unsigned int channel_video_bitrate_sent = 0;
2537 unsigned int channel_fec_bitrate_sent = 0;
2538 unsigned int channel_nack_bitrate_sent = 0;
2539 if (engine_->vie()->rtp()->GetBandwidthUsage(
2540 channel_id, channel_total_bitrate_sent, channel_video_bitrate_sent,
2541 channel_fec_bitrate_sent, channel_nack_bitrate_sent) == 0) {
2542 total_bitrate_sent += channel_total_bitrate_sent;
2543 video_bitrate_sent += channel_video_bitrate_sent;
2544 fec_bitrate_sent += channel_fec_bitrate_sent;
2545 nack_bitrate_sent += channel_nack_bitrate_sent;
2546 } else {
2547 LOG_RTCERR1(GetBandwidthUsage, channel_id);
2548 }
2549
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002550 unsigned int target_enc_stream_bitrate = 0;
2551 if (engine_->vie()->codec()->GetCodecTargetBitrate(
2552 channel_id, &target_enc_stream_bitrate) == 0) {
2553 target_enc_bitrate += target_enc_stream_bitrate;
2554 } else {
2555 LOG_RTCERR1(GetCodecTargetBitrate, channel_id);
2556 }
2557 }
buildbot@webrtc.orga18b4c92014-05-06 17:48:14 +00002558 if (!send_channels_.empty()) {
2559 // GetEstimatedSendBandwidth returns the estimated bandwidth for all video
2560 // engine channels in a channel group. Any valid channel id will do as it
2561 // is only used to access the right group of channels.
2562 const int channel_id = send_channels_.begin()->second->channel_id();
2563 // Get the send bandwidth available for this MediaChannel.
2564 if (engine_->vie()->rtp()->GetEstimatedSendBandwidth(
2565 channel_id, &estimated_send_bandwidth) != 0) {
2566 LOG_RTCERR1(GetEstimatedSendBandwidth, channel_id);
2567 }
2568 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002569 } else {
2570 LOG(LS_WARNING) << "GetStats: sender information not ready.";
2571 }
2572
2573 // Get the SSRC and stats for each receiver, based on our own calculations.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002574 for (RecvChannelMap::const_iterator it = recv_channels_.begin();
2575 it != recv_channels_.end(); ++it) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002576 WebRtcVideoChannelRecvInfo* channel = it->second;
2577
2578 unsigned int ssrc;
2579 // Get receiver statistics and build VideoReceiverInfo, if we have data.
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +00002580 // Skip the default channel (ssrc == 0).
2581 if (engine_->vie()->rtp()->GetRemoteSSRC(
2582 channel->channel_id(), ssrc) != 0 ||
2583 ssrc == 0)
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002584 continue;
2585
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002586 webrtc::StreamDataCounters sent;
2587 webrtc::StreamDataCounters received;
2588 if (engine_->vie()->rtp()->GetRtpStatistics(channel->channel_id(),
2589 sent, received) != 0) {
2590 LOG_RTCERR1(GetRTPStatistics, channel->channel_id());
2591 return false;
2592 }
2593 VideoReceiverInfo rinfo;
2594 rinfo.add_ssrc(ssrc);
2595 rinfo.bytes_rcvd = received.bytes;
2596 rinfo.packets_rcvd = received.packets;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002597 rinfo.packets_lost = -1;
2598 rinfo.packets_concealed = -1;
2599 rinfo.fraction_lost = -1; // from SentRTCP
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002600 rinfo.frame_width = channel->render_adapter()->width();
2601 rinfo.frame_height = channel->render_adapter()->height();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002602 int fps = channel->render_adapter()->framerate();
2603 rinfo.framerate_decoded = fps;
2604 rinfo.framerate_output = fps;
buildbot@webrtc.org0581f0b2014-05-06 21:36:31 +00002605 rinfo.capture_start_ntp_time_ms =
2606 channel->render_adapter()->capture_start_ntp_time_ms();
wu@webrtc.org97077a32013-10-25 21:18:33 +00002607 channel->decoder_observer()->ExportTo(&rinfo);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002608
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00002609 webrtc::RtcpPacketTypeCounter rtcp_sent;
2610 webrtc::RtcpPacketTypeCounter rtcp_received;
2611 if (engine()->vie()->rtp()->GetRtcpPacketTypeCounters(
2612 channel->channel_id(), &rtcp_sent, &rtcp_received) == 0) {
2613 rinfo.firs_sent = rtcp_sent.fir_packets;
2614 rinfo.plis_sent = rtcp_sent.pli_packets;
2615 rinfo.nacks_sent = rtcp_sent.nack_packets;
2616 } else {
2617 rinfo.firs_sent = -1;
2618 rinfo.plis_sent = -1;
2619 rinfo.nacks_sent = -1;
2620 LOG_RTCERR1(GetRtcpPacketTypeCounters, channel->channel_id());
2621 }
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00002622
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002623 // Get our locally created statistics of the received RTP stream.
2624 webrtc::RtcpStatistics incoming_stream_rtcp_stats;
2625 int incoming_stream_rtt_ms;
2626 if (engine_->vie()->rtp()->GetReceiveChannelRtcpStatistics(
2627 channel->channel_id(),
2628 incoming_stream_rtcp_stats,
2629 incoming_stream_rtt_ms) == 0) {
2630 // Convert Q8 to float.
2631 rinfo.packets_lost = incoming_stream_rtcp_stats.cumulative_lost;
2632 rinfo.fraction_lost = static_cast<float>(
2633 incoming_stream_rtcp_stats.fraction_lost) / (1 << 8);
2634 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002635 info->receivers.push_back(rinfo);
buildbot@webrtc.orga18b4c92014-05-06 17:48:14 +00002636 }
2637 unsigned int estimated_recv_bandwidth = 0;
2638 if (!recv_channels_.empty()) {
2639 // GetEstimatedReceiveBandwidth returns the estimated bandwidth for all
2640 // video engine channels in a channel group. Any valid channel id will do as
2641 // it is only used to access the right group of channels.
2642 const int channel_id = recv_channels_.begin()->second->channel_id();
2643 // Gets the estimated receive bandwidth for the MediaChannel.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002644 if (engine_->vie()->rtp()->GetEstimatedReceiveBandwidth(
buildbot@webrtc.orga18b4c92014-05-06 17:48:14 +00002645 channel_id, &estimated_recv_bandwidth) != 0) {
2646 LOG_RTCERR1(GetEstimatedReceiveBandwidth, channel_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002647 }
2648 }
buildbot@webrtc.orga18b4c92014-05-06 17:48:14 +00002649
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002650 // Build BandwidthEstimationInfo.
2651 // TODO(zhurunz): Add real unittest for this.
2652 BandwidthEstimationInfo bwe;
2653
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00002654 // TODO(jiayl): remove the condition when the necessary changes are available
2655 // outside the dev branch.
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00002656 if (options.include_received_propagation_stats) {
2657 webrtc::ReceiveBandwidthEstimatorStats additional_stats;
2658 // Only call for the default channel because the returned stats are
2659 // collected for all the channels using the same estimator.
2660 if (engine_->vie()->rtp()->GetReceiveBandwidthEstimatorStats(
mallinath@webrtc.org92fdfeb2014-02-17 18:49:41 +00002661 recv_channels_[0]->channel_id(), &additional_stats) == 0) {
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00002662 bwe.total_received_propagation_delta_ms =
2663 additional_stats.total_propagation_time_delta_ms;
2664 bwe.recent_received_propagation_delta_ms.swap(
2665 additional_stats.recent_propagation_time_delta_ms);
2666 bwe.recent_received_packet_group_arrival_time_ms.swap(
2667 additional_stats.recent_arrival_time_ms);
2668 }
2669 }
henrike@webrtc.orgb8395eb2014-02-28 21:57:22 +00002670
2671 engine_->vie()->rtp()->GetPacerQueuingDelayMs(
2672 recv_channels_[0]->channel_id(), &bwe.bucket_delay);
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00002673
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002674 // Calculations done above per send/receive stream.
2675 bwe.actual_enc_bitrate = video_bitrate_sent;
2676 bwe.transmit_bitrate = total_bitrate_sent;
2677 bwe.retransmit_bitrate = nack_bitrate_sent;
2678 bwe.available_send_bandwidth = estimated_send_bandwidth;
2679 bwe.available_recv_bandwidth = estimated_recv_bandwidth;
2680 bwe.target_enc_bitrate = target_enc_bitrate;
2681
2682 info->bw_estimations.push_back(bwe);
2683
2684 return true;
2685}
2686
2687bool WebRtcVideoMediaChannel::SetCapturer(uint32 ssrc,
2688 VideoCapturer* capturer) {
2689 ASSERT(ssrc != 0);
2690 if (!capturer) {
2691 return RemoveCapturer(ssrc);
2692 }
2693 WebRtcVideoChannelSendInfo* send_channel = GetSendChannel(ssrc);
2694 if (!send_channel) {
2695 return false;
2696 }
2697 VideoCapturer* old_capturer = send_channel->video_capturer();
wu@webrtc.org24301a62013-12-13 19:17:43 +00002698 MaybeDisconnectCapturer(old_capturer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002699
henrike@webrtc.orgc7bec842014-03-12 19:53:43 +00002700 send_channel->set_video_capturer(capturer, engine()->vie());
wu@webrtc.orga8910d22014-01-23 22:12:45 +00002701 MaybeConnectCapturer(capturer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002702 if (!capturer->IsScreencast() && ratio_w_ != 0 && ratio_h_ != 0) {
2703 capturer->UpdateAspectRatio(ratio_w_, ratio_h_);
2704 }
2705 const int64 timestamp = send_channel->local_stream_info()->time_stamp();
2706 if (send_codec_) {
2707 QueueBlackFrame(ssrc, timestamp, send_codec_->maxFramerate);
2708 }
2709 return true;
2710}
2711
2712bool WebRtcVideoMediaChannel::RequestIntraFrame() {
2713 // There is no API exposed to application to request a key frame
2714 // ViE does this internally when there are errors from decoder
2715 return false;
2716}
2717
wu@webrtc.orga9890802013-12-13 00:21:03 +00002718void WebRtcVideoMediaChannel::OnPacketReceived(
2719 talk_base::Buffer* packet, const talk_base::PacketTime& packet_time) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002720 // Pick which channel to send this packet to. If this packet doesn't match
2721 // any multiplexed streams, just send it to the default channel. Otherwise,
2722 // send it to the specific decoder instance for that stream.
2723 uint32 ssrc = 0;
2724 if (!GetRtpSsrc(packet->data(), packet->length(), &ssrc))
2725 return;
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00002726 int processing_channel = GetRecvChannelNum(ssrc);
2727 if (processing_channel == -1) {
2728 // Allocate an unsignalled recv channel for processing in conference mode.
henrike@webrtc.org18e59112014-03-14 17:19:38 +00002729 if (!InConferenceMode()) {
buildbot@webrtc.orgdd4742a2014-05-07 14:50:35 +00002730 // If we can't find or allocate one, use the default.
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00002731 processing_channel = video_channel();
henrike@webrtc.org18e59112014-03-14 17:19:38 +00002732 } else if (!CreateUnsignalledRecvChannel(ssrc, &processing_channel)) {
buildbot@webrtc.orgdd4742a2014-05-07 14:50:35 +00002733 // If we can't create an unsignalled recv channel, drop the packet in
henrike@webrtc.org18e59112014-03-14 17:19:38 +00002734 // conference mode.
2735 return;
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00002736 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002737 }
2738
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00002739 engine()->vie()->network()->ReceivedRTPPacket(
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00002740 processing_channel,
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00002741 packet->data(),
wu@webrtc.orga9890802013-12-13 00:21:03 +00002742 static_cast<int>(packet->length()),
2743 webrtc::PacketTime(packet_time.timestamp, packet_time.not_before));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002744}
2745
wu@webrtc.orga9890802013-12-13 00:21:03 +00002746void WebRtcVideoMediaChannel::OnRtcpReceived(
2747 talk_base::Buffer* packet, const talk_base::PacketTime& packet_time) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002748// Sending channels need all RTCP packets with feedback information.
2749// Even sender reports can contain attached report blocks.
2750// Receiving channels need sender reports in order to create
2751// correct receiver reports.
2752
2753 uint32 ssrc = 0;
2754 if (!GetRtcpSsrc(packet->data(), packet->length(), &ssrc)) {
2755 LOG(LS_WARNING) << "Failed to parse SSRC from received RTCP packet";
2756 return;
2757 }
2758 int type = 0;
2759 if (!GetRtcpType(packet->data(), packet->length(), &type)) {
2760 LOG(LS_WARNING) << "Failed to parse type from received RTCP packet";
2761 return;
2762 }
2763
2764 // If it is a sender report, find the channel that is listening.
2765 if (type == kRtcpTypeSR) {
2766 int which_channel = GetRecvChannelNum(ssrc);
2767 if (which_channel != -1 && !IsDefaultChannel(which_channel)) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00002768 engine_->vie()->network()->ReceivedRTCPPacket(
2769 which_channel,
2770 packet->data(),
2771 static_cast<int>(packet->length()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002772 }
2773 }
2774 // SR may continue RR and any RR entry may correspond to any one of the send
2775 // channels. So all RTCP packets must be forwarded all send channels. ViE
2776 // will filter out RR internally.
2777 for (SendChannelMap::iterator iter = send_channels_.begin();
2778 iter != send_channels_.end(); ++iter) {
2779 WebRtcVideoChannelSendInfo* send_channel = iter->second;
2780 int channel_id = send_channel->channel_id();
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00002781 engine_->vie()->network()->ReceivedRTCPPacket(
2782 channel_id,
2783 packet->data(),
2784 static_cast<int>(packet->length()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002785 }
2786}
2787
2788void WebRtcVideoMediaChannel::OnReadyToSend(bool ready) {
2789 SetNetworkTransmissionState(ready);
2790}
2791
2792bool WebRtcVideoMediaChannel::MuteStream(uint32 ssrc, bool muted) {
2793 WebRtcVideoChannelSendInfo* send_channel = GetSendChannel(ssrc);
2794 if (!send_channel) {
2795 LOG(LS_ERROR) << "The specified ssrc " << ssrc << " is not in use.";
2796 return false;
2797 }
2798 send_channel->set_muted(muted);
2799 return true;
2800}
2801
2802bool WebRtcVideoMediaChannel::SetRecvRtpHeaderExtensions(
2803 const std::vector<RtpHeaderExtension>& extensions) {
2804 if (receive_extensions_ == extensions) {
2805 return true;
2806 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002807
2808 const RtpHeaderExtension* offset_extension =
2809 FindHeaderExtension(extensions, kRtpTimestampOffsetHeaderExtension);
2810 const RtpHeaderExtension* send_time_extension =
henrike@webrtc.org79047f92014-03-06 23:46:59 +00002811 FindHeaderExtension(extensions, kRtpAbsoluteSenderTimeHeaderExtension);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002812
2813 // Loop through all receive channels and enable/disable the extensions.
2814 for (RecvChannelMap::iterator channel_it = recv_channels_.begin();
2815 channel_it != recv_channels_.end(); ++channel_it) {
2816 int channel_id = channel_it->second->channel_id();
2817 if (!SetHeaderExtension(
2818 &webrtc::ViERTP_RTCP::SetReceiveTimestampOffsetStatus, channel_id,
2819 offset_extension)) {
2820 return false;
2821 }
2822 if (!SetHeaderExtension(
2823 &webrtc::ViERTP_RTCP::SetReceiveAbsoluteSendTimeStatus, channel_id,
2824 send_time_extension)) {
2825 return false;
2826 }
2827 }
buildbot@webrtc.org150835e2014-05-06 15:54:38 +00002828
2829 receive_extensions_ = extensions;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002830 return true;
2831}
2832
2833bool WebRtcVideoMediaChannel::SetSendRtpHeaderExtensions(
2834 const std::vector<RtpHeaderExtension>& extensions) {
buildbot@webrtc.org150835e2014-05-06 15:54:38 +00002835 if (send_extensions_ == extensions) {
2836 return true;
2837 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002838
2839 const RtpHeaderExtension* offset_extension =
2840 FindHeaderExtension(extensions, kRtpTimestampOffsetHeaderExtension);
2841 const RtpHeaderExtension* send_time_extension =
henrike@webrtc.org79047f92014-03-06 23:46:59 +00002842 FindHeaderExtension(extensions, kRtpAbsoluteSenderTimeHeaderExtension);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002843
2844 // Loop through all send channels and enable/disable the extensions.
2845 for (SendChannelMap::iterator channel_it = send_channels_.begin();
2846 channel_it != send_channels_.end(); ++channel_it) {
2847 int channel_id = channel_it->second->channel_id();
2848 if (!SetHeaderExtension(
2849 &webrtc::ViERTP_RTCP::SetSendTimestampOffsetStatus, channel_id,
2850 offset_extension)) {
2851 return false;
2852 }
2853 if (!SetHeaderExtension(
2854 &webrtc::ViERTP_RTCP::SetSendAbsoluteSendTimeStatus, channel_id,
2855 send_time_extension)) {
2856 return false;
2857 }
2858 }
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00002859
2860 if (send_time_extension) {
2861 // For video RTP packets, we would like to update AbsoluteSendTimeHeader
2862 // Extension closer to the network, @ socket level before sending.
2863 // Pushing the extension id to socket layer.
2864 MediaChannel::SetOption(NetworkInterface::ST_RTP,
2865 talk_base::Socket::OPT_RTP_SENDTIME_EXTN_ID,
2866 send_time_extension->id);
2867 }
buildbot@webrtc.org150835e2014-05-06 15:54:38 +00002868
2869 send_extensions_ = extensions;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002870 return true;
2871}
2872
mallinath@webrtc.org92fdfeb2014-02-17 18:49:41 +00002873int WebRtcVideoMediaChannel::GetRtpSendTimeExtnId() const {
2874 const RtpHeaderExtension* send_time_extension = FindHeaderExtension(
henrike@webrtc.org79047f92014-03-06 23:46:59 +00002875 send_extensions_, kRtpAbsoluteSenderTimeHeaderExtension);
mallinath@webrtc.org92fdfeb2014-02-17 18:49:41 +00002876 if (send_time_extension) {
2877 return send_time_extension->id;
2878 }
2879 return -1;
2880}
2881
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002882bool WebRtcVideoMediaChannel::SetStartSendBandwidth(int bps) {
2883 LOG(LS_INFO) << "WebRtcVideoMediaChannel::SetStartSendBandwidth";
2884
2885 if (!send_codec_) {
2886 LOG(LS_INFO) << "The send codec has not been set up yet";
2887 return true;
2888 }
2889
2890 // On success, SetSendCodec() will reset |send_start_bitrate_| to |bps/1000|,
2891 // by calling MaybeChangeStartBitrate. That method will also clamp the
2892 // start bitrate between min and max, consistent with the override behavior
2893 // in SetMaxSendBandwidth.
2894 return SetSendCodec(*send_codec_,
2895 send_min_bitrate_, bps / 1000, send_max_bitrate_);
2896}
2897
2898bool WebRtcVideoMediaChannel::SetMaxSendBandwidth(int bps) {
2899 LOG(LS_INFO) << "WebRtcVideoMediaChannel::SetMaxSendBandwidth";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002900
2901 if (InConferenceMode()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002902 LOG(LS_INFO) << "Conference mode ignores SetMaxSendBandwidth";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002903 return true;
2904 }
2905
2906 if (!send_codec_) {
2907 LOG(LS_INFO) << "The send codec has not been set up yet";
2908 return true;
2909 }
2910
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002911 // Use the default value or the bps for the max
2912 int max_bitrate = (bps <= 0) ? send_max_bitrate_ : (bps / 1000);
2913
2914 // Reduce the current minimum and start bitrates if necessary.
2915 int min_bitrate = talk_base::_min(send_min_bitrate_, max_bitrate);
2916 int start_bitrate = talk_base::_min(send_start_bitrate_, max_bitrate);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002917
2918 if (!SetSendCodec(*send_codec_, min_bitrate, start_bitrate, max_bitrate)) {
2919 return false;
2920 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002921 LogSendCodecChange("SetMaxSendBandwidth()");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002922
2923 return true;
2924}
2925
2926bool WebRtcVideoMediaChannel::SetOptions(const VideoOptions &options) {
2927 // Always accept options that are unchanged.
2928 if (options_ == options) {
2929 return true;
2930 }
2931
2932 // Trigger SetSendCodec to set correct noise reduction state if the option has
2933 // changed.
2934 bool denoiser_changed = options.video_noise_reduction.IsSet() &&
2935 (options_.video_noise_reduction != options.video_noise_reduction);
2936
2937 bool leaky_bucket_changed = options.video_leaky_bucket.IsSet() &&
2938 (options_.video_leaky_bucket != options.video_leaky_bucket);
2939
2940 bool buffer_latency_changed = options.buffered_mode_latency.IsSet() &&
2941 (options_.buffered_mode_latency != options.buffered_mode_latency);
2942
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00002943 bool cpu_overuse_detection_changed = options.cpu_overuse_detection.IsSet() &&
2944 (options_.cpu_overuse_detection != options.cpu_overuse_detection);
2945
wu@webrtc.orgde305012013-10-31 15:40:38 +00002946 bool dscp_option_changed = (options_.dscp != options.dscp);
2947
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002948 bool suspend_below_min_bitrate_changed =
2949 options.suspend_below_min_bitrate.IsSet() &&
2950 (options_.suspend_below_min_bitrate != options.suspend_below_min_bitrate);
2951
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002952 bool conference_mode_turned_off = false;
2953 if (options_.conference_mode.IsSet() && options.conference_mode.IsSet() &&
2954 options_.conference_mode.GetWithDefaultIfUnset(false) &&
2955 !options.conference_mode.GetWithDefaultIfUnset(false)) {
2956 conference_mode_turned_off = true;
2957 }
2958
henrike@webrtc.orgdce3feb2014-03-26 01:17:30 +00002959 bool improved_wifi_bwe_changed =
2960 options.use_improved_wifi_bandwidth_estimator.IsSet() &&
2961 options_.use_improved_wifi_bandwidth_estimator !=
2962 options.use_improved_wifi_bandwidth_estimator;
2963
henrike@webrtc.org10bd88e2014-03-11 21:07:25 +00002964
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002965 // Save the options, to be interpreted where appropriate.
2966 // Use options_.SetAll() instead of assignment so that unset value in options
2967 // will not overwrite the previous option value.
2968 options_.SetAll(options);
2969
2970 // Set CPU options for all send channels.
2971 for (SendChannelMap::iterator iter = send_channels_.begin();
2972 iter != send_channels_.end(); ++iter) {
2973 WebRtcVideoChannelSendInfo* send_channel = iter->second;
2974 send_channel->ApplyCpuOptions(options_);
2975 }
2976
2977 // Adjust send codec bitrate if needed.
2978 int conf_max_bitrate = kDefaultConferenceModeMaxVideoBitrate;
2979
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00002980 // Save altered min_bitrate level and apply if necessary.
2981 bool adjusted_min_bitrate = false;
2982 if (options.lower_min_bitrate.IsSet()) {
2983 bool lower;
2984 options.lower_min_bitrate.Get(&lower);
2985
2986 int new_send_min_bitrate = lower ? kLowerMinBitrate : kMinVideoBitrate;
2987 adjusted_min_bitrate = (new_send_min_bitrate != send_min_bitrate_);
2988 send_min_bitrate_ = new_send_min_bitrate;
2989 }
2990
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002991 int expected_bitrate = send_max_bitrate_;
2992 if (InConferenceMode()) {
2993 expected_bitrate = conf_max_bitrate;
2994 } else if (conference_mode_turned_off) {
2995 // This is a special case for turning conference mode off.
2996 // Max bitrate should go back to the default maximum value instead
2997 // of the current maximum.
2998 expected_bitrate = kMaxVideoBitrate;
2999 }
3000
wu@webrtc.org1e6cb2c2014-03-24 17:01:50 +00003001 int options_start_bitrate;
3002 bool start_bitrate_changed = false;
3003 if (options.video_start_bitrate.Get(&options_start_bitrate) &&
3004 options_start_bitrate != send_start_bitrate_) {
3005 send_start_bitrate_ = options_start_bitrate;
3006 start_bitrate_changed = true;
3007 }
3008
henrike@webrtc.org10bd88e2014-03-11 21:07:25 +00003009 bool reset_send_codec_needed = send_codec_ &&
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00003010 (send_max_bitrate_ != expected_bitrate || denoiser_changed ||
wu@webrtc.org1e6cb2c2014-03-24 17:01:50 +00003011 adjusted_min_bitrate || start_bitrate_changed);
henrike@webrtc.org10bd88e2014-03-11 21:07:25 +00003012
3013
buildbot@webrtc.orgf875f152014-04-14 16:06:21 +00003014 LOG(LS_INFO) << "Reset send codec needed is enabled? "
3015 << reset_send_codec_needed;
henrike@webrtc.org10bd88e2014-03-11 21:07:25 +00003016 if (reset_send_codec_needed) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003017 // On success, SetSendCodec() will reset send_max_bitrate_ to
3018 // expected_bitrate.
3019 if (!SetSendCodec(*send_codec_,
3020 send_min_bitrate_,
3021 send_start_bitrate_,
3022 expected_bitrate)) {
3023 return false;
3024 }
3025 LogSendCodecChange("SetOptions()");
3026 }
henrike@webrtc.org10bd88e2014-03-11 21:07:25 +00003027
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003028 if (leaky_bucket_changed) {
3029 bool enable_leaky_bucket =
3030 options_.video_leaky_bucket.GetWithDefaultIfUnset(false);
buildbot@webrtc.orgf875f152014-04-14 16:06:21 +00003031 LOG(LS_INFO) << "Leaky bucket is enabled? " << enable_leaky_bucket;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003032 for (SendChannelMap::iterator it = send_channels_.begin();
3033 it != send_channels_.end(); ++it) {
3034 if (engine()->vie()->rtp()->SetTransmissionSmoothingStatus(
3035 it->second->channel_id(), enable_leaky_bucket) != 0) {
3036 LOG_RTCERR2(SetTransmissionSmoothingStatus, it->second->channel_id(),
3037 enable_leaky_bucket);
3038 }
3039 }
3040 }
3041 if (buffer_latency_changed) {
3042 int buffer_latency =
3043 options_.buffered_mode_latency.GetWithDefaultIfUnset(
3044 cricket::kBufferedModeDisabled);
buildbot@webrtc.orgf875f152014-04-14 16:06:21 +00003045 LOG(LS_INFO) << "Buffer latency is " << buffer_latency;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003046 for (SendChannelMap::iterator it = send_channels_.begin();
3047 it != send_channels_.end(); ++it) {
3048 if (engine()->vie()->rtp()->SetSenderBufferingMode(
3049 it->second->channel_id(), buffer_latency) != 0) {
3050 LOG_RTCERR2(SetSenderBufferingMode, it->second->channel_id(),
3051 buffer_latency);
3052 }
3053 }
3054 for (RecvChannelMap::iterator it = recv_channels_.begin();
3055 it != recv_channels_.end(); ++it) {
3056 if (engine()->vie()->rtp()->SetReceiverBufferingMode(
3057 it->second->channel_id(), buffer_latency) != 0) {
3058 LOG_RTCERR2(SetReceiverBufferingMode, it->second->channel_id(),
3059 buffer_latency);
3060 }
3061 }
3062 }
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00003063 if (cpu_overuse_detection_changed) {
3064 bool cpu_overuse_detection =
3065 options_.cpu_overuse_detection.GetWithDefaultIfUnset(false);
buildbot@webrtc.orgf875f152014-04-14 16:06:21 +00003066 LOG(LS_INFO) << "CPU overuse detection is enabled? "
3067 << cpu_overuse_detection;
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00003068 for (SendChannelMap::iterator iter = send_channels_.begin();
3069 iter != send_channels_.end(); ++iter) {
3070 WebRtcVideoChannelSendInfo* send_channel = iter->second;
3071 send_channel->SetCpuOveruseDetection(cpu_overuse_detection);
3072 }
3073 }
wu@webrtc.orgde305012013-10-31 15:40:38 +00003074 if (dscp_option_changed) {
3075 talk_base::DiffServCodePoint dscp = talk_base::DSCP_DEFAULT;
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00003076 if (options_.dscp.GetWithDefaultIfUnset(false))
wu@webrtc.orgde305012013-10-31 15:40:38 +00003077 dscp = kVideoDscpValue;
buildbot@webrtc.orgf875f152014-04-14 16:06:21 +00003078 LOG(LS_INFO) << "DSCP is " << dscp;
wu@webrtc.orgde305012013-10-31 15:40:38 +00003079 if (MediaChannel::SetDscp(dscp) != 0) {
3080 LOG(LS_WARNING) << "Failed to set DSCP settings for video channel";
3081 }
3082 }
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00003083 if (suspend_below_min_bitrate_changed) {
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00003084 if (options_.suspend_below_min_bitrate.GetWithDefaultIfUnset(false)) {
buildbot@webrtc.orgf875f152014-04-14 16:06:21 +00003085 LOG(LS_INFO) << "Suspend below min bitrate enabled.";
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00003086 for (SendChannelMap::iterator it = send_channels_.begin();
3087 it != send_channels_.end(); ++it) {
3088 engine()->vie()->codec()->SuspendBelowMinBitrate(
3089 it->second->channel_id());
3090 }
3091 } else {
3092 LOG(LS_WARNING) << "Cannot disable video suspension once it is enabled";
3093 }
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00003094 }
henrike@webrtc.orgdce3feb2014-03-26 01:17:30 +00003095 if (improved_wifi_bwe_changed) {
buildbot@webrtc.orgf875f152014-04-14 16:06:21 +00003096 LOG(LS_INFO) << "Improved WIFI BWE called.";
henrike@webrtc.orgdce3feb2014-03-26 01:17:30 +00003097 webrtc::Config config;
3098 config.Set(new webrtc::AimdRemoteRateControl(
3099 options_.use_improved_wifi_bandwidth_estimator
3100 .GetWithDefaultIfUnset(false)));
3101 for (SendChannelMap::iterator it = send_channels_.begin();
3102 it != send_channels_.end(); ++it) {
3103 engine()->vie()->network()->SetBandwidthEstimationConfig(
3104 it->second->channel_id(), config);
3105 }
3106 }
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +00003107 webrtc::CpuOveruseOptions overuse_options;
3108 if (GetCpuOveruseOptions(options_, &overuse_options)) {
3109 for (SendChannelMap::iterator it = send_channels_.begin();
3110 it != send_channels_.end(); ++it) {
3111 if (engine()->vie()->base()->SetCpuOveruseOptions(
3112 it->second->channel_id(), overuse_options) != 0) {
3113 LOG_RTCERR1(SetCpuOveruseOptions, it->second->channel_id());
3114 }
3115 }
3116 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003117 return true;
3118}
3119
3120void WebRtcVideoMediaChannel::SetInterface(NetworkInterface* iface) {
3121 MediaChannel::SetInterface(iface);
3122 // Set the RTP recv/send buffer to a bigger size
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00003123 MediaChannel::SetOption(NetworkInterface::ST_RTP,
3124 talk_base::Socket::OPT_RCVBUF,
3125 kVideoRtpBufferSize);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003126
3127 // TODO(sriniv): Remove or re-enable this.
3128 // As part of b/8030474, send-buffer is size now controlled through
3129 // portallocator flags.
3130 // network_interface_->SetOption(NetworkInterface::ST_RTP,
3131 // talk_base::Socket::OPT_SNDBUF,
3132 // kVideoRtpBufferSize);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003133}
3134
3135void WebRtcVideoMediaChannel::UpdateAspectRatio(int ratio_w, int ratio_h) {
3136 ASSERT(ratio_w != 0);
3137 ASSERT(ratio_h != 0);
3138 ratio_w_ = ratio_w;
3139 ratio_h_ = ratio_h;
3140 // For now assume that all streams want the same aspect ratio.
3141 // TODO(hellner): remove the need for this assumption.
3142 for (SendChannelMap::iterator iter = send_channels_.begin();
3143 iter != send_channels_.end(); ++iter) {
3144 WebRtcVideoChannelSendInfo* send_channel = iter->second;
3145 VideoCapturer* capturer = send_channel->video_capturer();
3146 if (capturer) {
3147 capturer->UpdateAspectRatio(ratio_w, ratio_h);
3148 }
3149 }
3150}
3151
3152bool WebRtcVideoMediaChannel::GetRenderer(uint32 ssrc,
3153 VideoRenderer** renderer) {
3154 RecvChannelMap::const_iterator it = recv_channels_.find(ssrc);
3155 if (it == recv_channels_.end()) {
3156 if (first_receive_ssrc_ == ssrc &&
3157 recv_channels_.find(0) != recv_channels_.end()) {
3158 LOG(LS_INFO) << " GetRenderer " << ssrc
3159 << " reuse default renderer #"
3160 << vie_channel_;
3161 *renderer = recv_channels_[0]->render_adapter()->renderer();
3162 return true;
3163 }
3164 return false;
3165 }
3166
3167 *renderer = it->second->render_adapter()->renderer();
3168 return true;
3169}
3170
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00003171bool WebRtcVideoMediaChannel::GetVideoAdapter(
3172 uint32 ssrc, CoordinatedVideoAdapter** video_adapter) {
3173 SendChannelMap::iterator it = send_channels_.find(ssrc);
3174 if (it == send_channels_.end()) {
3175 return false;
3176 }
3177 *video_adapter = it->second->video_adapter();
3178 return true;
3179}
3180
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003181void WebRtcVideoMediaChannel::SendFrame(VideoCapturer* capturer,
3182 const VideoFrame* frame) {
wu@webrtc.org24301a62013-12-13 19:17:43 +00003183 // If the |capturer| is registered to any send channel, then send the frame
3184 // to those send channels.
3185 bool capturer_is_channel_owned = false;
3186 for (SendChannelMap::iterator iter = send_channels_.begin();
3187 iter != send_channels_.end(); ++iter) {
3188 WebRtcVideoChannelSendInfo* send_channel = iter->second;
3189 if (send_channel->video_capturer() == capturer) {
3190 SendFrame(send_channel, frame, capturer->IsScreencast());
3191 capturer_is_channel_owned = true;
3192 }
3193 }
3194 if (capturer_is_channel_owned) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003195 return;
3196 }
wu@webrtc.org24301a62013-12-13 19:17:43 +00003197
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003198 // TODO(hellner): Remove below for loop once the captured frame no longer
3199 // come from the engine, i.e. the engine no longer owns a capturer.
3200 for (SendChannelMap::iterator iter = send_channels_.begin();
3201 iter != send_channels_.end(); ++iter) {
3202 WebRtcVideoChannelSendInfo* send_channel = iter->second;
3203 if (send_channel->video_capturer() == NULL) {
3204 SendFrame(send_channel, frame, capturer->IsScreencast());
3205 }
3206 }
3207}
3208
3209bool WebRtcVideoMediaChannel::SendFrame(
3210 WebRtcVideoChannelSendInfo* send_channel,
3211 const VideoFrame* frame,
3212 bool is_screencast) {
3213 if (!send_channel) {
3214 return false;
3215 }
3216 if (!send_codec_) {
3217 // Send codec has not been set. No reason to process the frame any further.
3218 return false;
3219 }
3220 const VideoFormat& video_format = send_channel->video_format();
3221 // If the frame should be dropped.
3222 const bool video_format_set = video_format != cricket::VideoFormat();
3223 if (video_format_set &&
3224 (video_format.width == 0 && video_format.height == 0)) {
3225 return true;
3226 }
3227
3228 // Checks if we need to reset vie send codec.
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00003229 if (!MaybeResetVieSendCodec(send_channel,
3230 static_cast<int>(frame->GetWidth()),
3231 static_cast<int>(frame->GetHeight()),
3232 is_screencast, NULL)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003233 LOG(LS_ERROR) << "MaybeResetVieSendCodec failed with "
3234 << frame->GetWidth() << "x" << frame->GetHeight();
3235 return false;
3236 }
3237 const VideoFrame* frame_out = frame;
3238 talk_base::scoped_ptr<VideoFrame> processed_frame;
3239 // Disable muting for screencast.
3240 const bool mute = (send_channel->muted() && !is_screencast);
3241 send_channel->ProcessFrame(*frame_out, mute, processed_frame.use());
3242 if (processed_frame) {
3243 frame_out = processed_frame.get();
3244 }
3245
3246 webrtc::ViEVideoFrameI420 frame_i420;
3247 // TODO(ronghuawu): Update the webrtc::ViEVideoFrameI420
3248 // to use const unsigned char*
3249 frame_i420.y_plane = const_cast<unsigned char*>(frame_out->GetYPlane());
3250 frame_i420.u_plane = const_cast<unsigned char*>(frame_out->GetUPlane());
3251 frame_i420.v_plane = const_cast<unsigned char*>(frame_out->GetVPlane());
3252 frame_i420.y_pitch = frame_out->GetYPitch();
3253 frame_i420.u_pitch = frame_out->GetUPitch();
3254 frame_i420.v_pitch = frame_out->GetVPitch();
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00003255 frame_i420.width = static_cast<uint16>(frame_out->GetWidth());
3256 frame_i420.height = static_cast<uint16>(frame_out->GetHeight());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003257
3258 int64 timestamp_ntp_ms = 0;
3259 // TODO(justinlin): Reenable after Windows issues with clock drift are fixed.
3260 // Currently reverted to old behavior of discarding capture timestamp.
3261#if 0
henrike@webrtc.orgf5bebd42014-04-04 18:39:07 +00003262 static const int kTimestampDeltaInSecondsForWarning = 2;
3263
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003264 // If the frame timestamp is 0, we will use the deliver time.
3265 const int64 frame_timestamp = frame->GetTimeStamp();
3266 if (frame_timestamp != 0) {
3267 if (abs(time(NULL) - frame_timestamp / talk_base::kNumNanosecsPerSec) >
3268 kTimestampDeltaInSecondsForWarning) {
3269 LOG(LS_WARNING) << "Frame timestamp differs by more than "
3270 << kTimestampDeltaInSecondsForWarning << " seconds from "
3271 << "current Unix timestamp.";
3272 }
3273
3274 timestamp_ntp_ms =
3275 talk_base::UnixTimestampNanosecsToNtpMillisecs(frame_timestamp);
3276 }
3277#endif
3278
3279 return send_channel->external_capture()->IncomingFrameI420(
3280 frame_i420, timestamp_ntp_ms) == 0;
3281}
3282
3283bool WebRtcVideoMediaChannel::CreateChannel(uint32 ssrc_key,
3284 MediaDirection direction,
3285 int* channel_id) {
3286 // There are 3 types of channels. Sending only, receiving only and
3287 // sending and receiving. The sending and receiving channel is the
3288 // default channel and there is only one. All other channels that are created
3289 // are associated with the default channel which must exist. The default
3290 // channel id is stored in |vie_channel_|. All channels need to know about
3291 // the default channel to properly handle remb which is why there are
3292 // different ViE create channel calls.
3293 // For this channel the local and remote ssrc key is 0. However, it may
3294 // have a non-zero local and/or remote ssrc depending on if it is currently
3295 // sending and/or receiving.
3296 if ((vie_channel_ == -1 || direction == MD_SENDRECV) &&
3297 (!send_channels_.empty() || !recv_channels_.empty())) {
3298 ASSERT(false);
3299 return false;
3300 }
3301
3302 *channel_id = -1;
3303 if (direction == MD_RECV) {
3304 // All rec channels are associated with the default channel |vie_channel_|
3305 if (engine_->vie()->base()->CreateReceiveChannel(*channel_id,
3306 vie_channel_) != 0) {
3307 LOG_RTCERR2(CreateReceiveChannel, *channel_id, vie_channel_);
3308 return false;
3309 }
3310 } else if (direction == MD_SEND) {
3311 if (engine_->vie()->base()->CreateChannel(*channel_id,
3312 vie_channel_) != 0) {
3313 LOG_RTCERR2(CreateChannel, *channel_id, vie_channel_);
3314 return false;
3315 }
3316 } else {
3317 ASSERT(direction == MD_SENDRECV);
3318 if (engine_->vie()->base()->CreateChannel(*channel_id) != 0) {
3319 LOG_RTCERR1(CreateChannel, *channel_id);
3320 return false;
3321 }
3322 }
3323 if (!ConfigureChannel(*channel_id, direction, ssrc_key)) {
3324 engine_->vie()->base()->DeleteChannel(*channel_id);
3325 *channel_id = -1;
3326 return false;
3327 }
3328
3329 return true;
3330}
3331
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00003332bool WebRtcVideoMediaChannel::CreateUnsignalledRecvChannel(
3333 uint32 ssrc_key, int* out_channel_id) {
henrike@webrtc.org18e59112014-03-14 17:19:38 +00003334 int unsignalled_recv_channel_limit =
3335 options_.unsignalled_recv_stream_limit.GetWithDefaultIfUnset(
3336 kNumDefaultUnsignalledVideoRecvStreams);
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00003337 if (num_unsignalled_recv_channels_ >= unsignalled_recv_channel_limit) {
3338 return false;
3339 }
3340 if (!CreateChannel(ssrc_key, MD_RECV, out_channel_id)) {
3341 return false;
3342 }
3343 // TODO(tvsriram): Support dynamic sizing of unsignalled recv channels.
3344 num_unsignalled_recv_channels_++;
3345 return true;
3346}
3347
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003348bool WebRtcVideoMediaChannel::ConfigureChannel(int channel_id,
3349 MediaDirection direction,
3350 uint32 ssrc_key) {
3351 const bool receiving = (direction == MD_RECV) || (direction == MD_SENDRECV);
3352 const bool sending = (direction == MD_SEND) || (direction == MD_SENDRECV);
3353 // Register external transport.
3354 if (engine_->vie()->network()->RegisterSendTransport(
3355 channel_id, *this) != 0) {
3356 LOG_RTCERR1(RegisterSendTransport, channel_id);
3357 return false;
3358 }
3359
3360 // Set MTU.
3361 if (engine_->vie()->network()->SetMTU(channel_id, kVideoMtu) != 0) {
3362 LOG_RTCERR2(SetMTU, channel_id, kVideoMtu);
3363 return false;
3364 }
3365 // Turn on RTCP and loss feedback reporting.
3366 if (engine()->vie()->rtp()->SetRTCPStatus(
3367 channel_id, webrtc::kRtcpCompound_RFC4585) != 0) {
3368 LOG_RTCERR2(SetRTCPStatus, channel_id, webrtc::kRtcpCompound_RFC4585);
3369 return false;
3370 }
3371 // Enable pli as key frame request method.
3372 if (engine_->vie()->rtp()->SetKeyFrameRequestMethod(
3373 channel_id, webrtc::kViEKeyFrameRequestPliRtcp) != 0) {
3374 LOG_RTCERR2(SetKeyFrameRequestMethod,
3375 channel_id, webrtc::kViEKeyFrameRequestPliRtcp);
3376 return false;
3377 }
3378 if (!SetNackFec(channel_id, send_red_type_, send_fec_type_, nack_enabled_)) {
3379 // Logged in SetNackFec. Don't spam the logs.
3380 return false;
3381 }
3382 // Note that receiving must always be configured before sending to ensure
3383 // that send and receive channel is configured correctly (ConfigureReceiving
3384 // assumes no sending).
3385 if (receiving) {
3386 if (!ConfigureReceiving(channel_id, ssrc_key)) {
3387 return false;
3388 }
3389 }
3390 if (sending) {
3391 if (!ConfigureSending(channel_id, ssrc_key)) {
3392 return false;
3393 }
3394 }
3395
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00003396 // Start receiving for both receive and send channels so that we get incoming
3397 // RTP (if receiving) as well as RTCP feedback (if sending).
3398 if (engine()->vie()->base()->StartReceive(channel_id) != 0) {
3399 LOG_RTCERR1(StartReceive, channel_id);
3400 return false;
3401 }
3402
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003403 return true;
3404}
3405
3406bool WebRtcVideoMediaChannel::ConfigureReceiving(int channel_id,
3407 uint32 remote_ssrc_key) {
3408 // Make sure that an SSRC/key isn't registered more than once.
3409 if (recv_channels_.find(remote_ssrc_key) != recv_channels_.end()) {
3410 return false;
3411 }
3412 // Connect the voice channel, if there is one.
3413 // TODO(perkj): The A/V is synched by the receiving channel. So we need to
3414 // know the SSRC of the remote audio channel in order to fetch the correct
3415 // webrtc VoiceEngine channel. For now- only sync the default channel used
3416 // in 1-1 calls.
3417 if (remote_ssrc_key == 0 && voice_channel_) {
3418 WebRtcVoiceMediaChannel* voice_channel =
3419 static_cast<WebRtcVoiceMediaChannel*>(voice_channel_);
3420 if (engine_->vie()->base()->ConnectAudioChannel(
3421 vie_channel_, voice_channel->voe_channel()) != 0) {
3422 LOG_RTCERR2(ConnectAudioChannel, channel_id,
3423 voice_channel->voe_channel());
3424 LOG(LS_WARNING) << "A/V not synchronized";
3425 // Not a fatal error.
3426 }
3427 }
3428
3429 talk_base::scoped_ptr<WebRtcVideoChannelRecvInfo> channel_info(
3430 new WebRtcVideoChannelRecvInfo(channel_id));
3431
3432 // Install a render adapter.
3433 if (engine_->vie()->render()->AddRenderer(channel_id,
3434 webrtc::kVideoI420, channel_info->render_adapter()) != 0) {
3435 LOG_RTCERR3(AddRenderer, channel_id, webrtc::kVideoI420,
3436 channel_info->render_adapter());
3437 return false;
3438 }
3439
3440
3441 if (engine_->vie()->rtp()->SetRembStatus(channel_id,
3442 kNotSending,
3443 remb_enabled_) != 0) {
3444 LOG_RTCERR3(SetRembStatus, channel_id, kNotSending, remb_enabled_);
3445 return false;
3446 }
3447
3448 if (!SetHeaderExtension(&webrtc::ViERTP_RTCP::SetReceiveTimestampOffsetStatus,
3449 channel_id, receive_extensions_, kRtpTimestampOffsetHeaderExtension)) {
3450 return false;
3451 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003452 if (!SetHeaderExtension(
3453 &webrtc::ViERTP_RTCP::SetReceiveAbsoluteSendTimeStatus, channel_id,
henrike@webrtc.org79047f92014-03-06 23:46:59 +00003454 receive_extensions_, kRtpAbsoluteSenderTimeHeaderExtension)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003455 return false;
3456 }
3457
3458 if (remote_ssrc_key != 0) {
3459 // Use the same SSRC as our default channel
3460 // (so the RTCP reports are correct).
3461 unsigned int send_ssrc = 0;
3462 webrtc::ViERTP_RTCP* rtp = engine()->vie()->rtp();
3463 if (rtp->GetLocalSSRC(vie_channel_, send_ssrc) == -1) {
3464 LOG_RTCERR2(GetLocalSSRC, vie_channel_, send_ssrc);
3465 return false;
3466 }
3467 if (rtp->SetLocalSSRC(channel_id, send_ssrc) == -1) {
3468 LOG_RTCERR2(SetLocalSSRC, channel_id, send_ssrc);
3469 return false;
3470 }
3471 } // Else this is the the default channel and we don't change the SSRC.
3472
3473 // Disable color enhancement since it is a bit too aggressive.
3474 if (engine()->vie()->image()->EnableColorEnhancement(channel_id,
3475 false) != 0) {
3476 LOG_RTCERR1(EnableColorEnhancement, channel_id);
3477 return false;
3478 }
3479
3480 if (!SetReceiveCodecs(channel_info.get())) {
3481 return false;
3482 }
3483
3484 int buffer_latency =
3485 options_.buffered_mode_latency.GetWithDefaultIfUnset(
3486 cricket::kBufferedModeDisabled);
3487 if (buffer_latency != cricket::kBufferedModeDisabled) {
3488 if (engine()->vie()->rtp()->SetReceiverBufferingMode(
3489 channel_id, buffer_latency) != 0) {
3490 LOG_RTCERR2(SetReceiverBufferingMode, channel_id, buffer_latency);
3491 }
3492 }
3493
3494 if (render_started_) {
3495 if (engine_->vie()->render()->StartRender(channel_id) != 0) {
3496 LOG_RTCERR1(StartRender, channel_id);
3497 return false;
3498 }
3499 }
3500
3501 // Register decoder observer for incoming framerate and bitrate.
3502 if (engine()->vie()->codec()->RegisterDecoderObserver(
3503 channel_id, *channel_info->decoder_observer()) != 0) {
3504 LOG_RTCERR1(RegisterDecoderObserver, channel_info->decoder_observer());
3505 return false;
3506 }
3507
3508 recv_channels_[remote_ssrc_key] = channel_info.release();
3509 return true;
3510}
3511
3512bool WebRtcVideoMediaChannel::ConfigureSending(int channel_id,
3513 uint32 local_ssrc_key) {
3514 // The ssrc key can be zero or correspond to an SSRC.
3515 // Make sure the default channel isn't configured more than once.
3516 if (local_ssrc_key == 0 && send_channels_.find(0) != send_channels_.end()) {
3517 return false;
3518 }
3519 // Make sure that the SSRC is not already in use.
3520 uint32 dummy_key;
3521 if (GetSendChannelKey(local_ssrc_key, &dummy_key)) {
3522 return false;
3523 }
3524 int vie_capture = 0;
3525 webrtc::ViEExternalCapture* external_capture = NULL;
3526 // Register external capture.
3527 if (engine()->vie()->capture()->AllocateExternalCaptureDevice(
3528 vie_capture, external_capture) != 0) {
3529 LOG_RTCERR0(AllocateExternalCaptureDevice);
3530 return false;
3531 }
3532
3533 // Connect external capture.
3534 if (engine()->vie()->capture()->ConnectCaptureDevice(
3535 vie_capture, channel_id) != 0) {
3536 LOG_RTCERR2(ConnectCaptureDevice, vie_capture, channel_id);
3537 return false;
3538 }
3539 talk_base::scoped_ptr<WebRtcVideoChannelSendInfo> send_channel(
3540 new WebRtcVideoChannelSendInfo(channel_id, vie_capture,
3541 external_capture,
3542 engine()->cpu_monitor()));
3543 send_channel->ApplyCpuOptions(options_);
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00003544 send_channel->SignalCpuAdaptationUnable.connect(this,
3545 &WebRtcVideoMediaChannel::OnCpuAdaptationUnable);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003546
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00003547 if (options_.cpu_overuse_detection.GetWithDefaultIfUnset(false)) {
3548 send_channel->SetCpuOveruseDetection(true);
3549 }
3550
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +00003551 webrtc::CpuOveruseOptions overuse_options;
3552 if (GetCpuOveruseOptions(options_, &overuse_options)) {
3553 if (engine()->vie()->base()->SetCpuOveruseOptions(channel_id,
3554 overuse_options) != 0) {
3555 LOG_RTCERR1(SetCpuOveruseOptions, channel_id);
3556 }
3557 }
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +00003558
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003559 // Register encoder observer for outgoing framerate and bitrate.
3560 if (engine()->vie()->codec()->RegisterEncoderObserver(
3561 channel_id, *send_channel->encoder_observer()) != 0) {
3562 LOG_RTCERR1(RegisterEncoderObserver, send_channel->encoder_observer());
3563 return false;
3564 }
3565
3566 if (!SetHeaderExtension(&webrtc::ViERTP_RTCP::SetSendTimestampOffsetStatus,
3567 channel_id, send_extensions_, kRtpTimestampOffsetHeaderExtension)) {
3568 return false;
3569 }
3570
3571 if (!SetHeaderExtension(&webrtc::ViERTP_RTCP::SetSendAbsoluteSendTimeStatus,
henrike@webrtc.org79047f92014-03-06 23:46:59 +00003572 channel_id, send_extensions_, kRtpAbsoluteSenderTimeHeaderExtension)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003573 return false;
3574 }
3575
3576 if (options_.video_leaky_bucket.GetWithDefaultIfUnset(false)) {
3577 if (engine()->vie()->rtp()->SetTransmissionSmoothingStatus(channel_id,
3578 true) != 0) {
3579 LOG_RTCERR2(SetTransmissionSmoothingStatus, channel_id, true);
3580 return false;
3581 }
3582 }
3583
3584 int buffer_latency =
3585 options_.buffered_mode_latency.GetWithDefaultIfUnset(
3586 cricket::kBufferedModeDisabled);
3587 if (buffer_latency != cricket::kBufferedModeDisabled) {
3588 if (engine()->vie()->rtp()->SetSenderBufferingMode(
3589 channel_id, buffer_latency) != 0) {
3590 LOG_RTCERR2(SetSenderBufferingMode, channel_id, buffer_latency);
3591 }
3592 }
henrike@webrtc.org6e3dbc22014-03-25 17:09:47 +00003593
3594 if (options_.suspend_below_min_bitrate.GetWithDefaultIfUnset(false)) {
3595 engine()->vie()->codec()->SuspendBelowMinBitrate(channel_id);
3596 }
3597
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003598 // The remb status direction correspond to the RTP stream (and not the RTCP
3599 // stream). I.e. if send remb is enabled it means it is receiving remote
3600 // rembs and should use them to estimate bandwidth. Receive remb mean that
3601 // remb packets will be generated and that the channel should be included in
3602 // it. If remb is enabled all channels are allowed to contribute to the remb
3603 // but only receive channels will ever end up actually contributing. This
3604 // keeps the logic simple.
3605 if (engine_->vie()->rtp()->SetRembStatus(channel_id,
3606 remb_enabled_,
3607 remb_enabled_) != 0) {
3608 LOG_RTCERR3(SetRembStatus, channel_id, remb_enabled_, remb_enabled_);
3609 return false;
3610 }
3611 if (!SetNackFec(channel_id, send_red_type_, send_fec_type_, nack_enabled_)) {
3612 // Logged in SetNackFec. Don't spam the logs.
3613 return false;
3614 }
3615
3616 send_channels_[local_ssrc_key] = send_channel.release();
3617
3618 return true;
3619}
3620
3621bool WebRtcVideoMediaChannel::SetNackFec(int channel_id,
3622 int red_payload_type,
3623 int fec_payload_type,
3624 bool nack_enabled) {
3625 bool enable = (red_payload_type != -1 && fec_payload_type != -1 &&
3626 !InConferenceMode());
3627 if (enable) {
3628 if (engine_->vie()->rtp()->SetHybridNACKFECStatus(
3629 channel_id, nack_enabled, red_payload_type, fec_payload_type) != 0) {
3630 LOG_RTCERR4(SetHybridNACKFECStatus,
3631 channel_id, nack_enabled, red_payload_type, fec_payload_type);
3632 return false;
3633 }
3634 LOG(LS_INFO) << "Hybrid NACK/FEC enabled for channel " << channel_id;
3635 } else {
3636 if (engine_->vie()->rtp()->SetNACKStatus(channel_id, nack_enabled) != 0) {
3637 LOG_RTCERR1(SetNACKStatus, channel_id);
3638 return false;
3639 }
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00003640 std::string enabled = nack_enabled ? "enabled" : "disabled";
3641 LOG(LS_INFO) << "NACK " << enabled << " for channel " << channel_id;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003642 }
3643 return true;
3644}
3645
3646bool WebRtcVideoMediaChannel::SetSendCodec(const webrtc::VideoCodec& codec,
3647 int min_bitrate,
3648 int start_bitrate,
3649 int max_bitrate) {
3650 bool ret_val = true;
3651 for (SendChannelMap::iterator iter = send_channels_.begin();
3652 iter != send_channels_.end(); ++iter) {
3653 WebRtcVideoChannelSendInfo* send_channel = iter->second;
3654 ret_val = SetSendCodec(send_channel, codec, min_bitrate, start_bitrate,
3655 max_bitrate) && ret_val;
3656 }
3657 if (ret_val) {
3658 // All SetSendCodec calls were successful. Update the global state
3659 // accordingly.
3660 send_codec_.reset(new webrtc::VideoCodec(codec));
3661 send_min_bitrate_ = min_bitrate;
3662 send_start_bitrate_ = start_bitrate;
3663 send_max_bitrate_ = max_bitrate;
3664 } else {
3665 // At least one SetSendCodec call failed, rollback.
3666 for (SendChannelMap::iterator iter = send_channels_.begin();
3667 iter != send_channels_.end(); ++iter) {
3668 WebRtcVideoChannelSendInfo* send_channel = iter->second;
3669 if (send_codec_) {
3670 SetSendCodec(send_channel, *send_codec_.get(), send_min_bitrate_,
3671 send_start_bitrate_, send_max_bitrate_);
3672 }
3673 }
3674 }
3675 return ret_val;
3676}
3677
3678bool WebRtcVideoMediaChannel::SetSendCodec(
3679 WebRtcVideoChannelSendInfo* send_channel,
3680 const webrtc::VideoCodec& codec,
3681 int min_bitrate,
3682 int start_bitrate,
3683 int max_bitrate) {
3684 if (!send_channel) {
3685 return false;
3686 }
3687 const int channel_id = send_channel->channel_id();
3688 // Make a copy of the codec
3689 webrtc::VideoCodec target_codec = codec;
3690 target_codec.startBitrate = start_bitrate;
3691 target_codec.minBitrate = min_bitrate;
3692 target_codec.maxBitrate = max_bitrate;
3693
3694 // Set the default number of temporal layers for VP8.
3695 if (webrtc::kVideoCodecVP8 == codec.codecType) {
3696 target_codec.codecSpecific.VP8.numberOfTemporalLayers =
3697 kDefaultNumberOfTemporalLayers;
3698
3699 // Turn off the VP8 error resilience
3700 target_codec.codecSpecific.VP8.resilience = webrtc::kResilienceOff;
3701
3702 bool enable_denoising =
3703 options_.video_noise_reduction.GetWithDefaultIfUnset(false);
3704 target_codec.codecSpecific.VP8.denoisingOn = enable_denoising;
3705 }
3706
3707 // Register external encoder if codec type is supported by encoder factory.
3708 if (engine()->IsExternalEncoderCodecType(codec.codecType) &&
3709 !send_channel->IsEncoderRegistered(target_codec.plType)) {
3710 webrtc::VideoEncoder* encoder =
3711 engine()->CreateExternalEncoder(codec.codecType);
3712 if (encoder) {
3713 if (engine()->vie()->ext_codec()->RegisterExternalSendCodec(
3714 channel_id, target_codec.plType, encoder, false) == 0) {
3715 send_channel->RegisterEncoder(target_codec.plType, encoder);
3716 } else {
3717 LOG_RTCERR2(RegisterExternalSendCodec, channel_id, target_codec.plName);
3718 engine()->DestroyExternalEncoder(encoder);
3719 }
3720 }
3721 }
3722
3723 // Resolution and framerate may vary for different send channels.
3724 const VideoFormat& video_format = send_channel->video_format();
3725 UpdateVideoCodec(video_format, &target_codec);
3726
3727 if (target_codec.width == 0 && target_codec.height == 0) {
3728 const uint32 ssrc = send_channel->stream_params()->first_ssrc();
3729 LOG(LS_INFO) << "0x0 resolution selected. Captured frames will be dropped "
3730 << "for ssrc: " << ssrc << ".";
3731 } else {
3732 MaybeChangeStartBitrate(channel_id, &target_codec);
wu@webrtc.org05e7b442014-04-01 17:44:24 +00003733 webrtc::VideoCodec current_codec;
3734 if (!engine()->vie()->codec()->GetSendCodec(channel_id, current_codec)) {
3735 // Compare against existing configured send codec.
3736 if (current_codec == target_codec) {
3737 // Codec is already configured on channel. no need to apply.
3738 return true;
3739 }
3740 }
3741
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003742 if (0 != engine()->vie()->codec()->SetSendCodec(channel_id, target_codec)) {
3743 LOG_RTCERR2(SetSendCodec, channel_id, target_codec.plName);
3744 return false;
3745 }
3746
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00003747 // NOTE: SetRtxSendPayloadType must be called after all simulcast SSRCs
3748 // are configured. Otherwise ssrc's configured after this point will use
3749 // the primary PT for RTX.
3750 if (send_rtx_type_ != -1 &&
3751 engine()->vie()->rtp()->SetRtxSendPayloadType(channel_id,
3752 send_rtx_type_) != 0) {
3753 LOG_RTCERR2(SetRtxSendPayloadType, channel_id, send_rtx_type_);
3754 return false;
3755 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003756 }
3757 send_channel->set_interval(
3758 cricket::VideoFormat::FpsToInterval(target_codec.maxFramerate));
3759 return true;
3760}
3761
3762
3763static std::string ToString(webrtc::VideoCodecComplexity complexity) {
3764 switch (complexity) {
3765 case webrtc::kComplexityNormal:
3766 return "normal";
3767 case webrtc::kComplexityHigh:
3768 return "high";
3769 case webrtc::kComplexityHigher:
3770 return "higher";
3771 case webrtc::kComplexityMax:
3772 return "max";
3773 default:
3774 return "unknown";
3775 }
3776}
3777
3778static std::string ToString(webrtc::VP8ResilienceMode resilience) {
3779 switch (resilience) {
3780 case webrtc::kResilienceOff:
3781 return "off";
3782 case webrtc::kResilientStream:
3783 return "stream";
3784 case webrtc::kResilientFrames:
3785 return "frames";
3786 default:
3787 return "unknown";
3788 }
3789}
3790
3791void WebRtcVideoMediaChannel::LogSendCodecChange(const std::string& reason) {
3792 webrtc::VideoCodec vie_codec;
3793 if (engine()->vie()->codec()->GetSendCodec(vie_channel_, vie_codec) != 0) {
3794 LOG_RTCERR1(GetSendCodec, vie_channel_);
3795 return;
3796 }
3797
3798 LOG(LS_INFO) << reason << " : selected video codec "
3799 << vie_codec.plName << "/"
3800 << vie_codec.width << "x" << vie_codec.height << "x"
3801 << static_cast<int>(vie_codec.maxFramerate) << "fps"
3802 << "@" << vie_codec.maxBitrate << "kbps"
3803 << " (min=" << vie_codec.minBitrate << "kbps,"
3804 << " start=" << vie_codec.startBitrate << "kbps)";
3805 LOG(LS_INFO) << "Video max quantization: " << vie_codec.qpMax;
3806 if (webrtc::kVideoCodecVP8 == vie_codec.codecType) {
3807 LOG(LS_INFO) << "VP8 number of temporal layers: "
3808 << static_cast<int>(
3809 vie_codec.codecSpecific.VP8.numberOfTemporalLayers);
3810 LOG(LS_INFO) << "VP8 options : "
3811 << "picture loss indication = "
3812 << vie_codec.codecSpecific.VP8.pictureLossIndicationOn
3813 << ", feedback mode = "
3814 << vie_codec.codecSpecific.VP8.feedbackModeOn
3815 << ", complexity = "
3816 << ToString(vie_codec.codecSpecific.VP8.complexity)
3817 << ", resilience = "
3818 << ToString(vie_codec.codecSpecific.VP8.resilience)
3819 << ", denoising = "
3820 << vie_codec.codecSpecific.VP8.denoisingOn
3821 << ", error concealment = "
3822 << vie_codec.codecSpecific.VP8.errorConcealmentOn
3823 << ", automatic resize = "
3824 << vie_codec.codecSpecific.VP8.automaticResizeOn
3825 << ", frame dropping = "
3826 << vie_codec.codecSpecific.VP8.frameDroppingOn
3827 << ", key frame interval = "
3828 << vie_codec.codecSpecific.VP8.keyFrameInterval;
3829 }
3830
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00003831 if (send_rtx_type_ != -1) {
3832 LOG(LS_INFO) << "RTX payload type: " << send_rtx_type_;
3833 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003834}
3835
3836bool WebRtcVideoMediaChannel::SetReceiveCodecs(
3837 WebRtcVideoChannelRecvInfo* info) {
3838 int red_type = -1;
3839 int fec_type = -1;
3840 int channel_id = info->channel_id();
buildbot@webrtc.orgdd4742a2014-05-07 14:50:35 +00003841 // Build a map from payload types to video codecs so that we easily can find
3842 // out if associated payload types are referring to valid codecs.
3843 std::map<int, webrtc::VideoCodec*> pt_to_codec;
3844 for (std::vector<webrtc::VideoCodec>::iterator it = receive_codecs_.begin();
3845 it != receive_codecs_.end(); ++it) {
3846 pt_to_codec[it->plType] = &(*it);
3847 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003848 for (std::vector<webrtc::VideoCodec>::iterator it = receive_codecs_.begin();
3849 it != receive_codecs_.end(); ++it) {
3850 if (it->codecType == webrtc::kVideoCodecRED) {
3851 red_type = it->plType;
3852 } else if (it->codecType == webrtc::kVideoCodecULPFEC) {
3853 fec_type = it->plType;
3854 }
buildbot@webrtc.orgdd4742a2014-05-07 14:50:35 +00003855 // If this is an RTX codec we have to verify that it is associated with
3856 // a valid video codec which we have RTX support for.
3857 if (_stricmp(it->plName, kRtxCodecName) == 0) {
3858 std::map<int, int>::iterator apt_it = associated_payload_types_.find(
3859 it->plType);
3860 bool valid_apt = false;
3861 if (apt_it != associated_payload_types_.end()) {
3862 std::map<int, webrtc::VideoCodec*>::iterator codec_it =
3863 pt_to_codec.find(apt_it->second);
3864 // We currently only support RTX associated with VP8 due to limitations
3865 // in webrtc where only one RTX payload type can be registered.
3866 valid_apt = codec_it != pt_to_codec.end() &&
3867 _stricmp(codec_it->second->plName, kVp8PayloadName) == 0;
3868 }
3869 if (!valid_apt) {
3870 LOG(LS_ERROR) << "The RTX codec isn't associated with a known and "
3871 "supported payload type";
3872 return false;
3873 }
3874 if (engine()->vie()->rtp()->SetRtxReceivePayloadType(
3875 channel_id, it->plType) != 0) {
3876 LOG_RTCERR2(SetRtxReceivePayloadType, channel_id, it->plType);
3877 return false;
3878 }
3879 continue;
3880 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003881 if (engine()->vie()->codec()->SetReceiveCodec(channel_id, *it) != 0) {
3882 LOG_RTCERR2(SetReceiveCodec, channel_id, it->plName);
3883 return false;
3884 }
3885 if (!info->IsDecoderRegistered(it->plType) &&
3886 it->codecType != webrtc::kVideoCodecRED &&
3887 it->codecType != webrtc::kVideoCodecULPFEC) {
3888 webrtc::VideoDecoder* decoder =
3889 engine()->CreateExternalDecoder(it->codecType);
3890 if (decoder) {
3891 if (engine()->vie()->ext_codec()->RegisterExternalReceiveCodec(
3892 channel_id, it->plType, decoder) == 0) {
3893 info->RegisterDecoder(it->plType, decoder);
3894 } else {
3895 LOG_RTCERR2(RegisterExternalReceiveCodec, channel_id, it->plName);
3896 engine()->DestroyExternalDecoder(decoder);
3897 }
3898 }
3899 }
3900 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003901 return true;
3902}
3903
3904int WebRtcVideoMediaChannel::GetRecvChannelNum(uint32 ssrc) {
3905 if (ssrc == first_receive_ssrc_) {
3906 return vie_channel_;
3907 }
buildbot@webrtc.orgdd4742a2014-05-07 14:50:35 +00003908 int recv_channel = -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003909 RecvChannelMap::iterator it = recv_channels_.find(ssrc);
buildbot@webrtc.orgdd4742a2014-05-07 14:50:35 +00003910 if (it == recv_channels_.end()) {
3911 // Check if we have an RTX stream registered on this SSRC.
3912 SsrcMap::iterator rtx_it = rtx_to_primary_ssrc_.find(ssrc);
3913 if (rtx_it != rtx_to_primary_ssrc_.end()) {
3914 it = recv_channels_.find(rtx_it->second);
3915 assert(it != recv_channels_.end());
3916 recv_channel = it->second->channel_id();
3917 }
3918 } else {
3919 recv_channel = it->second->channel_id();
3920 }
3921 return recv_channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003922}
3923
3924// If the new frame size is different from the send codec size we set on vie,
3925// we need to reset the send codec on vie.
3926// The new send codec size should not exceed send_codec_ which is controlled
3927// only by the 'jec' logic.
3928bool WebRtcVideoMediaChannel::MaybeResetVieSendCodec(
3929 WebRtcVideoChannelSendInfo* send_channel,
3930 int new_width,
3931 int new_height,
3932 bool is_screencast,
3933 bool* reset) {
3934 if (reset) {
3935 *reset = false;
3936 }
3937 ASSERT(send_codec_.get() != NULL);
3938
3939 webrtc::VideoCodec target_codec = *send_codec_.get();
3940 const VideoFormat& video_format = send_channel->video_format();
3941 UpdateVideoCodec(video_format, &target_codec);
3942
3943 // Vie send codec size should not exceed target_codec.
3944 int target_width = new_width;
3945 int target_height = new_height;
3946 if (!is_screencast &&
3947 (new_width > target_codec.width || new_height > target_codec.height)) {
3948 target_width = target_codec.width;
3949 target_height = target_codec.height;
3950 }
3951
3952 // Get current vie codec.
3953 webrtc::VideoCodec vie_codec;
3954 const int channel_id = send_channel->channel_id();
3955 if (engine()->vie()->codec()->GetSendCodec(channel_id, vie_codec) != 0) {
3956 LOG_RTCERR1(GetSendCodec, channel_id);
3957 return false;
3958 }
3959 const int cur_width = vie_codec.width;
3960 const int cur_height = vie_codec.height;
3961
3962 // Only reset send codec when there is a size change. Additionally,
3963 // automatic resize needs to be turned off when screencasting and on when
3964 // not screencasting.
3965 // Don't allow automatic resizing for screencasting.
3966 bool automatic_resize = !is_screencast;
3967 // Turn off VP8 frame dropping when screensharing as the current model does
3968 // not work well at low fps.
3969 bool vp8_frame_dropping = !is_screencast;
3970 // Disable denoising for screencasting.
3971 bool enable_denoising =
3972 options_.video_noise_reduction.GetWithDefaultIfUnset(false);
henrike@webrtc.orgdce3feb2014-03-26 01:17:30 +00003973 int screencast_min_bitrate =
3974 options_.screencast_min_bitrate.GetWithDefaultIfUnset(0);
3975 bool leaky_bucket = options_.video_leaky_bucket.GetWithDefaultIfUnset(false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003976 bool denoising = !is_screencast && enable_denoising;
3977 bool reset_send_codec =
3978 target_width != cur_width || target_height != cur_height ||
3979 automatic_resize != vie_codec.codecSpecific.VP8.automaticResizeOn ||
3980 denoising != vie_codec.codecSpecific.VP8.denoisingOn ||
3981 vp8_frame_dropping != vie_codec.codecSpecific.VP8.frameDroppingOn;
3982
3983 if (reset_send_codec) {
3984 // Set the new codec on vie.
3985 vie_codec.width = target_width;
3986 vie_codec.height = target_height;
3987 vie_codec.maxFramerate = target_codec.maxFramerate;
3988 vie_codec.startBitrate = target_codec.startBitrate;
henrike@webrtc.org6e3dbc22014-03-25 17:09:47 +00003989 vie_codec.targetBitrate = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003990 vie_codec.codecSpecific.VP8.automaticResizeOn = automatic_resize;
3991 vie_codec.codecSpecific.VP8.denoisingOn = denoising;
3992 vie_codec.codecSpecific.VP8.frameDroppingOn = vp8_frame_dropping;
buildbot@webrtc.org0d34f142014-05-02 16:54:25 +00003993 MaybeChangeStartBitrate(channel_id, &vie_codec);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003994
3995 if (engine()->vie()->codec()->SetSendCodec(channel_id, vie_codec) != 0) {
3996 LOG_RTCERR1(SetSendCodec, channel_id);
3997 return false;
3998 }
henrike@webrtc.orgdce3feb2014-03-26 01:17:30 +00003999
henrike@webrtc.orgdce3feb2014-03-26 01:17:30 +00004000 if (is_screencast) {
4001 engine()->vie()->rtp()->SetMinTransmitBitrate(channel_id,
4002 screencast_min_bitrate);
4003 // If screencast and min bitrate set, force enable pacer.
4004 if (screencast_min_bitrate > 0) {
4005 engine()->vie()->rtp()->SetTransmissionSmoothingStatus(channel_id,
4006 true);
4007 }
4008 } else {
4009 // In case of switching from screencast to regular capture, set
4010 // min bitrate padding and pacer back to defaults.
4011 engine()->vie()->rtp()->SetMinTransmitBitrate(channel_id, 0);
4012 engine()->vie()->rtp()->SetTransmissionSmoothingStatus(channel_id,
4013 leaky_bucket);
4014 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00004015 if (reset) {
4016 *reset = true;
4017 }
4018 LogSendCodecChange("Capture size changed");
4019 }
4020
4021 return true;
4022}
4023
4024void WebRtcVideoMediaChannel::MaybeChangeStartBitrate(
4025 int channel_id, webrtc::VideoCodec* video_codec) {
4026 if (video_codec->startBitrate < video_codec->minBitrate) {
4027 video_codec->startBitrate = video_codec->minBitrate;
4028 } else if (video_codec->startBitrate > video_codec->maxBitrate) {
4029 video_codec->startBitrate = video_codec->maxBitrate;
4030 }
4031
4032 // Use a previous target bitrate, if there is one.
4033 unsigned int current_target_bitrate = 0;
4034 if (engine()->vie()->codec()->GetCodecTargetBitrate(
4035 channel_id, &current_target_bitrate) == 0) {
4036 // Convert to kbps.
4037 current_target_bitrate /= 1000;
4038 if (current_target_bitrate > video_codec->maxBitrate) {
4039 current_target_bitrate = video_codec->maxBitrate;
4040 }
4041 if (current_target_bitrate > video_codec->startBitrate) {
4042 video_codec->startBitrate = current_target_bitrate;
4043 }
4044 }
4045}
4046
4047void WebRtcVideoMediaChannel::OnMessage(talk_base::Message* msg) {
4048 FlushBlackFrameData* black_frame_data =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00004049 static_cast<FlushBlackFrameData*>(msg->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00004050 FlushBlackFrame(black_frame_data->ssrc, black_frame_data->timestamp);
4051 delete black_frame_data;
4052}
4053
4054int WebRtcVideoMediaChannel::SendPacket(int channel, const void* data,
4055 int len) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00004056 talk_base::Buffer packet(data, len, kMaxRtpPacketLen);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00004057 return MediaChannel::SendPacket(&packet) ? len : -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00004058}
4059
4060int WebRtcVideoMediaChannel::SendRTCPPacket(int channel,
4061 const void* data,
4062 int len) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00004063 talk_base::Buffer packet(data, len, kMaxRtpPacketLen);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00004064 return MediaChannel::SendRtcp(&packet) ? len : -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00004065}
4066
4067void WebRtcVideoMediaChannel::QueueBlackFrame(uint32 ssrc, int64 timestamp,
4068 int framerate) {
4069 if (timestamp) {
4070 FlushBlackFrameData* black_frame_data = new FlushBlackFrameData(
4071 ssrc,
4072 timestamp);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00004073 const int delay_ms = static_cast<int>(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00004074 2 * cricket::VideoFormat::FpsToInterval(framerate) *
4075 talk_base::kNumMillisecsPerSec / talk_base::kNumNanosecsPerSec);
4076 worker_thread()->PostDelayed(delay_ms, this, 0, black_frame_data);
4077 }
4078}
4079
4080void WebRtcVideoMediaChannel::FlushBlackFrame(uint32 ssrc, int64 timestamp) {
4081 WebRtcVideoChannelSendInfo* send_channel = GetSendChannel(ssrc);
4082 if (!send_channel) {
4083 return;
4084 }
4085 talk_base::scoped_ptr<const VideoFrame> black_frame_ptr;
4086
4087 const WebRtcLocalStreamInfo* channel_stream_info =
4088 send_channel->local_stream_info();
4089 int64 last_frame_time_stamp = channel_stream_info->time_stamp();
4090 if (last_frame_time_stamp == timestamp) {
4091 size_t last_frame_width = 0;
4092 size_t last_frame_height = 0;
4093 int64 last_frame_elapsed_time = 0;
4094 channel_stream_info->GetLastFrameInfo(&last_frame_width, &last_frame_height,
4095 &last_frame_elapsed_time);
4096 if (!last_frame_width || !last_frame_height) {
4097 return;
4098 }
4099 WebRtcVideoFrame black_frame;
4100 // Black frame is not screencast.
4101 const bool screencasting = false;
4102 const int64 timestamp_delta = send_channel->interval();
4103 if (!black_frame.InitToBlack(send_codec_->width, send_codec_->height, 1, 1,
4104 last_frame_elapsed_time + timestamp_delta,
4105 last_frame_time_stamp + timestamp_delta) ||
4106 !SendFrame(send_channel, &black_frame, screencasting)) {
4107 LOG(LS_ERROR) << "Failed to send black frame.";
4108 }
4109 }
4110}
4111
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00004112void WebRtcVideoMediaChannel::OnCpuAdaptationUnable() {
4113 // ssrc is hardcoded to 0. This message is based on a system wide issue,
4114 // so finding which ssrc caused it doesn't matter.
4115 SignalMediaError(0, VideoMediaChannel::ERROR_REC_CPU_MAX_CANT_DOWNGRADE);
4116}
4117
henrike@webrtc.org28e20752013-07-10 00:45:36 +00004118void WebRtcVideoMediaChannel::SetNetworkTransmissionState(
4119 bool is_transmitting) {
4120 LOG(LS_INFO) << "SetNetworkTransmissionState: " << is_transmitting;
4121 for (SendChannelMap::iterator iter = send_channels_.begin();
4122 iter != send_channels_.end(); ++iter) {
4123 WebRtcVideoChannelSendInfo* send_channel = iter->second;
4124 int channel_id = send_channel->channel_id();
4125 engine_->vie()->network()->SetNetworkTransmissionState(channel_id,
4126 is_transmitting);
4127 }
4128}
4129
4130bool WebRtcVideoMediaChannel::SetHeaderExtension(ExtensionSetterFunction setter,
4131 int channel_id, const RtpHeaderExtension* extension) {
4132 bool enable = false;
4133 int id = 0;
4134 if (extension) {
4135 enable = true;
4136 id = extension->id;
4137 }
4138 if ((engine_->vie()->rtp()->*setter)(channel_id, enable, id) != 0) {
4139 LOG_RTCERR4(*setter, extension->uri, channel_id, enable, id);
4140 return false;
4141 }
4142 return true;
4143}
4144
4145bool WebRtcVideoMediaChannel::SetHeaderExtension(ExtensionSetterFunction setter,
4146 int channel_id, const std::vector<RtpHeaderExtension>& extensions,
4147 const char header_extension_uri[]) {
4148 const RtpHeaderExtension* extension = FindHeaderExtension(extensions,
4149 header_extension_uri);
4150 return SetHeaderExtension(setter, channel_id, extension);
4151}
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00004152
4153bool WebRtcVideoMediaChannel::SetLocalRtxSsrc(int channel_id,
4154 const StreamParams& send_params,
4155 uint32 primary_ssrc,
4156 int stream_idx) {
4157 uint32 rtx_ssrc = 0;
4158 bool has_rtx = send_params.GetFidSsrc(primary_ssrc, &rtx_ssrc);
4159 if (has_rtx && engine()->vie()->rtp()->SetLocalSSRC(
4160 channel_id, rtx_ssrc, webrtc::kViEStreamTypeRtx, stream_idx) != 0) {
4161 LOG_RTCERR4(SetLocalSSRC, channel_id, rtx_ssrc,
4162 webrtc::kViEStreamTypeRtx, stream_idx);
4163 return false;
4164 }
4165 return true;
4166}
4167
wu@webrtc.org24301a62013-12-13 19:17:43 +00004168void WebRtcVideoMediaChannel::MaybeConnectCapturer(VideoCapturer* capturer) {
4169 if (capturer != NULL && GetSendChannelNum(capturer) == 1) {
wu@webrtc.orgf7d501d2014-03-27 23:48:25 +00004170 capturer->SignalVideoFrame.connect(this,
4171 &WebRtcVideoMediaChannel::SendFrame);
wu@webrtc.org24301a62013-12-13 19:17:43 +00004172 }
4173}
4174
4175void WebRtcVideoMediaChannel::MaybeDisconnectCapturer(VideoCapturer* capturer) {
4176 if (capturer != NULL && GetSendChannelNum(capturer) == 1) {
4177 capturer->SignalVideoFrame.disconnect(this);
4178 }
4179}
4180
henrike@webrtc.org28e20752013-07-10 00:45:36 +00004181} // namespace cricket
4182
4183#endif // HAVE_WEBRTC_VIDEO