blob: 0283f575dae4a73b1f92d4dc36683e02968944b5 [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;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000093
wu@webrtc.orgcecfd182013-10-30 05:18:12 +000094// Controlled by exp, try a super low minimum bitrate for poor connections.
95static const int kLowerMinBitrate = 30;
96
henrike@webrtc.org28e20752013-07-10 00:45:36 +000097static const int kVideoMtu = 1200;
98
99static const int kVideoRtpBufferSize = 65536;
100
101static const char kVp8PayloadName[] = "VP8";
102static const char kRedPayloadName[] = "red";
103static const char kFecPayloadName[] = "ulpfec";
104
105static const int kDefaultNumberOfTemporalLayers = 1; // 1:1
106
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000107static const int kMaxExternalVideoCodecs = 8;
108static const int kExternalVideoPayloadTypeBase = 120;
109
buildbot@webrtc.org073dfdd2014-05-08 19:36:21 +0000110static bool BitrateIsSet(int value) {
buildbot@webrtc.orgd1ae89f2014-05-08 19:19:26 +0000111 return value > kAutoBandwidth;
112}
113
buildbot@webrtc.org073dfdd2014-05-08 19:36:21 +0000114static int GetBitrate(int value, int deflt) {
buildbot@webrtc.orgd1ae89f2014-05-08 19:19:26 +0000115 return BitrateIsSet(value) ? value : deflt;
116}
117
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000118// Static allocation of payload type values for external video codec.
119static int GetExternalVideoPayloadType(int index) {
120 ASSERT(index >= 0 && index < kMaxExternalVideoCodecs);
121 return kExternalVideoPayloadTypeBase + index;
122}
123
124static void LogMultiline(talk_base::LoggingSeverity sev, char* text) {
125 const char* delim = "\r\n";
126 // TODO(fbarchard): Fix strtok lint warning.
127 for (char* tok = strtok(text, delim); tok; tok = strtok(NULL, delim)) {
128 LOG_V(sev) << tok;
129 }
130}
131
132// Severity is an integer because it comes is assumed to be from command line.
133static int SeverityToFilter(int severity) {
134 int filter = webrtc::kTraceNone;
135 switch (severity) {
136 case talk_base::LS_VERBOSE:
137 filter |= webrtc::kTraceAll;
138 case talk_base::LS_INFO:
139 filter |= (webrtc::kTraceStateInfo | webrtc::kTraceInfo);
140 case talk_base::LS_WARNING:
141 filter |= (webrtc::kTraceTerseInfo | webrtc::kTraceWarning);
142 case talk_base::LS_ERROR:
143 filter |= (webrtc::kTraceError | webrtc::kTraceCritical);
144 }
145 return filter;
146}
147
148static const int kCpuMonitorPeriodMs = 2000; // 2 seconds.
149
150static const bool kNotSending = false;
151
wu@webrtc.orgde305012013-10-31 15:40:38 +0000152// Default video dscp value.
153// See http://tools.ietf.org/html/rfc2474 for details
154// See also http://tools.ietf.org/html/draft-jennings-rtcweb-qos-00
155static const talk_base::DiffServCodePoint kVideoDscpValue =
156 talk_base::DSCP_AF41;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000157
158static bool IsNackEnabled(const VideoCodec& codec) {
159 return codec.HasFeedbackParam(FeedbackParam(kRtcpFbParamNack,
160 kParamValueEmpty));
161}
162
163// Returns true if Receiver Estimated Max Bitrate is enabled.
164static bool IsRembEnabled(const VideoCodec& codec) {
165 return codec.HasFeedbackParam(FeedbackParam(kRtcpFbParamRemb,
166 kParamValueEmpty));
167}
168
169struct FlushBlackFrameData : public talk_base::MessageData {
170 FlushBlackFrameData(uint32 s, int64 t) : ssrc(s), timestamp(t) {
171 }
172 uint32 ssrc;
173 int64 timestamp;
174};
175
176class WebRtcRenderAdapter : public webrtc::ExternalRenderer {
177 public:
buildbot@webrtc.orgf875f152014-04-14 16:06:21 +0000178 WebRtcRenderAdapter(VideoRenderer* renderer, int channel_id)
buildbot@webrtc.org0581f0b2014-05-06 21:36:31 +0000179 : renderer_(renderer),
180 channel_id_(channel_id),
181 width_(0),
182 height_(0),
buildbot@webrtc.orgf9f1bfb2014-05-21 17:02:15 +0000183 capture_start_rtp_time_stamp_(-1),
buildbot@webrtc.org0581f0b2014-05-06 21:36:31 +0000184 capture_start_ntp_time_ms_(0) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000185 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000186
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000187 virtual ~WebRtcRenderAdapter() {
188 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000189
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000190 void SetRenderer(VideoRenderer* renderer) {
191 talk_base::CritScope cs(&crit_);
192 renderer_ = renderer;
193 // FrameSizeChange may have already been called when renderer was not set.
194 // If so we should call SetSize here.
195 // TODO(ronghuawu): Add unit test for this case. Didn't do it now
196 // because the WebRtcRenderAdapter is currently hiding in cc file. No
197 // good way to get access to it from the unit test.
198 if (width_ > 0 && height_ > 0 && renderer_ != NULL) {
199 if (!renderer_->SetSize(width_, height_, 0)) {
200 LOG(LS_ERROR)
buildbot@webrtc.orgf875f152014-04-14 16:06:21 +0000201 << "WebRtcRenderAdapter (channel " << channel_id_
202 << ") SetRenderer failed to SetSize to: "
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000203 << width_ << "x" << height_;
204 }
205 }
206 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000207
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000208 // Implementation of webrtc::ExternalRenderer.
209 virtual int FrameSizeChange(unsigned int width, unsigned int height,
210 unsigned int /*number_of_streams*/) {
211 talk_base::CritScope cs(&crit_);
212 width_ = width;
213 height_ = height;
buildbot@webrtc.orgf875f152014-04-14 16:06:21 +0000214 LOG(LS_INFO) << "WebRtcRenderAdapter (channel " << channel_id_
215 << ") frame size changed to: "
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000216 << width << "x" << height;
217 if (renderer_ == NULL) {
buildbot@webrtc.orgf875f152014-04-14 16:06:21 +0000218 LOG(LS_VERBOSE) << "WebRtcRenderAdapter (channel " << channel_id_
219 << ") the renderer has not been set. "
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000220 << "SetSize will be called later in SetRenderer.";
221 return 0;
222 }
223 return renderer_->SetSize(width_, height_, 0) ? 0 : -1;
224 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000225
buildbot@webrtc.org1fd5b452014-04-15 17:39:43 +0000226 virtual int DeliverFrame(unsigned char* buffer,
227 int buffer_size,
buildbot@webrtc.org0581f0b2014-05-06 21:36:31 +0000228 uint32_t rtp_time_stamp,
buildbot@webrtc.org1fd5b452014-04-15 17:39:43 +0000229#ifdef USE_WEBRTC_DEV_BRANCH
230 int64_t ntp_time_ms,
231#endif
232 int64_t render_time,
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000233 void* handle) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000234 talk_base::CritScope cs(&crit_);
buildbot@webrtc.orgf9f1bfb2014-05-21 17:02:15 +0000235 if (capture_start_rtp_time_stamp_ < 0) {
buildbot@webrtc.org0581f0b2014-05-06 21:36:31 +0000236 capture_start_rtp_time_stamp_ = rtp_time_stamp;
237 }
buildbot@webrtc.org22190372014-05-07 17:52:33 +0000238
239 const int kVideoCodecClockratekHz = cricket::kVideoCodecClockrate / 1000;
240
buildbot@webrtc.org0581f0b2014-05-06 21:36:31 +0000241#ifdef USE_WEBRTC_DEV_BRANCH
242 if (ntp_time_ms > 0) {
buildbot@webrtc.orgf9f1bfb2014-05-21 17:02:15 +0000243 int64 elapsed_time_ms =
244 (rtp_ts_wraparound_handler_.Unwrap(rtp_time_stamp) -
245 capture_start_rtp_time_stamp_) / kVideoCodecClockratekHz;
buildbot@webrtc.org0581f0b2014-05-06 21:36:31 +0000246 capture_start_ntp_time_ms_ = ntp_time_ms - elapsed_time_ms;
247 }
248#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000249 frame_rate_tracker_.Update(1);
250 if (renderer_ == NULL) {
251 return 0;
252 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000253 // Convert 90K rtp timestamp to ns timestamp.
buildbot@webrtc.org0581f0b2014-05-06 21:36:31 +0000254 int64 rtp_time_stamp_in_ns = (rtp_time_stamp / kVideoCodecClockratekHz) *
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000255 talk_base::kNumNanosecsPerMillisec;
256 // Convert milisecond render time to ns timestamp.
257 int64 render_time_stamp_in_ns = render_time *
258 talk_base::kNumNanosecsPerMillisec;
259 // Send the rtp timestamp to renderer as the VideoFrame timestamp.
260 // and the render timestamp as the VideoFrame elapsed_time.
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000261 if (handle == NULL) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000262 return DeliverBufferFrame(buffer, buffer_size, render_time_stamp_in_ns,
buildbot@webrtc.org740e6b32014-04-30 15:33:45 +0000263 rtp_time_stamp_in_ns);
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000264 } else {
265 return DeliverTextureFrame(handle, render_time_stamp_in_ns,
buildbot@webrtc.org740e6b32014-04-30 15:33:45 +0000266 rtp_time_stamp_in_ns);
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000267 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000268 }
269
270 virtual bool IsTextureSupported() { return true; }
271
272 int DeliverBufferFrame(unsigned char* buffer, int buffer_size,
buildbot@webrtc.org0581f0b2014-05-06 21:36:31 +0000273 int64 elapsed_time, int64 rtp_time_stamp_in_ns) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000274 WebRtcVideoFrame video_frame;
wu@webrtc.org16d62542013-11-05 23:45:14 +0000275 video_frame.Alias(buffer, buffer_size, width_, height_,
buildbot@webrtc.org0581f0b2014-05-06 21:36:31 +0000276 1, 1, elapsed_time, rtp_time_stamp_in_ns, 0);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000277
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000278 // Sanity check on decoded frame size.
279 if (buffer_size != static_cast<int>(VideoFrame::SizeOf(width_, height_))) {
buildbot@webrtc.orgf875f152014-04-14 16:06:21 +0000280 LOG(LS_WARNING) << "WebRtcRenderAdapter (channel " << channel_id_
281 << ") received a strange frame size: "
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000282 << buffer_size;
283 }
284
285 int ret = renderer_->RenderFrame(&video_frame) ? 0 : -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000286 return ret;
287 }
288
buildbot@webrtc.org0581f0b2014-05-06 21:36:31 +0000289 int DeliverTextureFrame(void* handle,
290 int64 elapsed_time,
291 int64 rtp_time_stamp_in_ns) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000292 WebRtcTextureVideoFrame video_frame(
293 static_cast<webrtc::NativeHandle*>(handle), width_, height_,
buildbot@webrtc.org0581f0b2014-05-06 21:36:31 +0000294 elapsed_time, rtp_time_stamp_in_ns);
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000295 return renderer_->RenderFrame(&video_frame);
296 }
297
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000298 unsigned int width() {
299 talk_base::CritScope cs(&crit_);
300 return width_;
301 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000302
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000303 unsigned int height() {
304 talk_base::CritScope cs(&crit_);
305 return height_;
306 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000307
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000308 int framerate() {
309 talk_base::CritScope cs(&crit_);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000310 return static_cast<int>(frame_rate_tracker_.units_second());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000311 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000312
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000313 VideoRenderer* renderer() {
314 talk_base::CritScope cs(&crit_);
315 return renderer_;
316 }
317
buildbot@webrtc.org0581f0b2014-05-06 21:36:31 +0000318 int64 capture_start_ntp_time_ms() {
319 talk_base::CritScope cs(&crit_);
320 return capture_start_ntp_time_ms_;
321 }
322
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000323 private:
324 talk_base::CriticalSection crit_;
325 VideoRenderer* renderer_;
buildbot@webrtc.orgf875f152014-04-14 16:06:21 +0000326 int channel_id_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000327 unsigned int width_;
328 unsigned int height_;
329 talk_base::RateTracker frame_rate_tracker_;
buildbot@webrtc.orgf9f1bfb2014-05-21 17:02:15 +0000330 talk_base::TimestampWrapAroundHandler rtp_ts_wraparound_handler_;
331 int64 capture_start_rtp_time_stamp_;
buildbot@webrtc.org0581f0b2014-05-06 21:36:31 +0000332 int64 capture_start_ntp_time_ms_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000333};
334
335class WebRtcDecoderObserver : public webrtc::ViEDecoderObserver {
336 public:
337 explicit WebRtcDecoderObserver(int video_channel)
338 : video_channel_(video_channel),
339 framerate_(0),
340 bitrate_(0),
wu@webrtc.org97077a32013-10-25 21:18:33 +0000341 decode_ms_(0),
342 max_decode_ms_(0),
343 current_delay_ms_(0),
344 target_delay_ms_(0),
345 jitter_buffer_ms_(0),
346 min_playout_delay_ms_(0),
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +0000347 render_delay_ms_(0) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000348 }
349
350 // virtual functions from VieDecoderObserver.
351 virtual void IncomingCodecChanged(const int videoChannel,
352 const webrtc::VideoCodec& videoCodec) {}
353 virtual void IncomingRate(const int videoChannel,
354 const unsigned int framerate,
355 const unsigned int bitrate) {
wu@webrtc.org78187522013-10-07 23:32:02 +0000356 talk_base::CritScope cs(&crit_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000357 ASSERT(video_channel_ == videoChannel);
358 framerate_ = framerate;
359 bitrate_ = bitrate;
360 }
wu@webrtc.org97077a32013-10-25 21:18:33 +0000361
362 virtual void DecoderTiming(int decode_ms,
363 int max_decode_ms,
364 int current_delay_ms,
365 int target_delay_ms,
366 int jitter_buffer_ms,
367 int min_playout_delay_ms,
368 int render_delay_ms) {
369 talk_base::CritScope cs(&crit_);
370 decode_ms_ = decode_ms;
371 max_decode_ms_ = max_decode_ms;
372 current_delay_ms_ = current_delay_ms;
373 target_delay_ms_ = target_delay_ms;
374 jitter_buffer_ms_ = jitter_buffer_ms;
375 min_playout_delay_ms_ = min_playout_delay_ms;
376 render_delay_ms_ = render_delay_ms;
377 }
378
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +0000379 virtual void RequestNewKeyFrame(const int videoChannel) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000380
wu@webrtc.org97077a32013-10-25 21:18:33 +0000381 // Populate |rinfo| based on previously-set data in |*this|.
382 void ExportTo(VideoReceiverInfo* rinfo) {
wu@webrtc.org78187522013-10-07 23:32:02 +0000383 talk_base::CritScope cs(&crit_);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000384 rinfo->framerate_rcvd = framerate_;
385 rinfo->decode_ms = decode_ms_;
386 rinfo->max_decode_ms = max_decode_ms_;
387 rinfo->current_delay_ms = current_delay_ms_;
388 rinfo->target_delay_ms = target_delay_ms_;
389 rinfo->jitter_buffer_ms = jitter_buffer_ms_;
390 rinfo->min_playout_delay_ms = min_playout_delay_ms_;
391 rinfo->render_delay_ms = render_delay_ms_;
wu@webrtc.org78187522013-10-07 23:32:02 +0000392 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000393
394 private:
wu@webrtc.org78187522013-10-07 23:32:02 +0000395 mutable talk_base::CriticalSection crit_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000396 int video_channel_;
397 int framerate_;
398 int bitrate_;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000399 int decode_ms_;
400 int max_decode_ms_;
401 int current_delay_ms_;
402 int target_delay_ms_;
403 int jitter_buffer_ms_;
404 int min_playout_delay_ms_;
405 int render_delay_ms_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000406};
407
408class WebRtcEncoderObserver : public webrtc::ViEEncoderObserver {
409 public:
410 explicit WebRtcEncoderObserver(int video_channel)
411 : video_channel_(video_channel),
412 framerate_(0),
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000413 bitrate_(0),
414 suspended_(false) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000415 }
416
417 // virtual functions from VieEncoderObserver.
418 virtual void OutgoingRate(const int videoChannel,
419 const unsigned int framerate,
420 const unsigned int bitrate) {
wu@webrtc.org78187522013-10-07 23:32:02 +0000421 talk_base::CritScope cs(&crit_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000422 ASSERT(video_channel_ == videoChannel);
423 framerate_ = framerate;
424 bitrate_ = bitrate;
425 }
426
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000427 virtual void SuspendChange(int video_channel, bool is_suspended) {
428 talk_base::CritScope cs(&crit_);
429 ASSERT(video_channel_ == video_channel);
430 suspended_ = is_suspended;
431 }
432
wu@webrtc.org78187522013-10-07 23:32:02 +0000433 int framerate() const {
434 talk_base::CritScope cs(&crit_);
435 return framerate_;
436 }
437 int bitrate() const {
438 talk_base::CritScope cs(&crit_);
439 return bitrate_;
440 }
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000441 bool suspended() const {
442 talk_base::CritScope cs(&crit_);
443 return suspended_;
444 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000445
446 private:
wu@webrtc.org78187522013-10-07 23:32:02 +0000447 mutable talk_base::CriticalSection crit_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000448 int video_channel_;
449 int framerate_;
450 int bitrate_;
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000451 bool suspended_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000452};
453
454class WebRtcLocalStreamInfo {
455 public:
456 WebRtcLocalStreamInfo()
457 : width_(0), height_(0), elapsed_time_(-1), time_stamp_(-1) {}
458 size_t width() const {
459 talk_base::CritScope cs(&crit_);
460 return width_;
461 }
462 size_t height() const {
463 talk_base::CritScope cs(&crit_);
464 return height_;
465 }
466 int64 elapsed_time() const {
467 talk_base::CritScope cs(&crit_);
468 return elapsed_time_;
469 }
470 int64 time_stamp() const {
471 talk_base::CritScope cs(&crit_);
472 return time_stamp_;
473 }
474 int framerate() {
475 talk_base::CritScope cs(&crit_);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000476 return static_cast<int>(rate_tracker_.units_second());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000477 }
478 void GetLastFrameInfo(
479 size_t* width, size_t* height, int64* elapsed_time) const {
480 talk_base::CritScope cs(&crit_);
481 *width = width_;
482 *height = height_;
483 *elapsed_time = elapsed_time_;
484 }
485
486 void UpdateFrame(const VideoFrame* frame) {
487 talk_base::CritScope cs(&crit_);
488
489 width_ = frame->GetWidth();
490 height_ = frame->GetHeight();
491 elapsed_time_ = frame->GetElapsedTime();
492 time_stamp_ = frame->GetTimeStamp();
493
494 rate_tracker_.Update(1);
495 }
496
497 private:
498 mutable talk_base::CriticalSection crit_;
499 size_t width_;
500 size_t height_;
501 int64 elapsed_time_;
502 int64 time_stamp_;
503 talk_base::RateTracker rate_tracker_;
504
505 DISALLOW_COPY_AND_ASSIGN(WebRtcLocalStreamInfo);
506};
507
508// WebRtcVideoChannelRecvInfo is a container class with members such as renderer
509// and a decoder observer that is used by receive channels.
510// It must exist as long as the receive channel is connected to renderer or a
511// decoder observer in this class and methods in the class should only be called
512// from the worker thread.
513class WebRtcVideoChannelRecvInfo {
514 public:
515 typedef std::map<int, webrtc::VideoDecoder*> DecoderMap; // key: payload type
516 explicit WebRtcVideoChannelRecvInfo(int channel_id)
517 : channel_id_(channel_id),
buildbot@webrtc.orgf875f152014-04-14 16:06:21 +0000518 render_adapter_(NULL, channel_id),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000519 decoder_observer_(channel_id) {
520 }
521 int channel_id() { return channel_id_; }
522 void SetRenderer(VideoRenderer* renderer) {
523 render_adapter_.SetRenderer(renderer);
524 }
525 WebRtcRenderAdapter* render_adapter() { return &render_adapter_; }
526 WebRtcDecoderObserver* decoder_observer() { return &decoder_observer_; }
527 void RegisterDecoder(int pl_type, webrtc::VideoDecoder* decoder) {
528 ASSERT(!IsDecoderRegistered(pl_type));
529 registered_decoders_[pl_type] = decoder;
530 }
531 bool IsDecoderRegistered(int pl_type) {
532 return registered_decoders_.count(pl_type) != 0;
533 }
534 const DecoderMap& registered_decoders() {
535 return registered_decoders_;
536 }
537 void ClearRegisteredDecoders() {
538 registered_decoders_.clear();
539 }
540
541 private:
542 int channel_id_; // Webrtc video channel number.
543 // Renderer for this channel.
544 WebRtcRenderAdapter render_adapter_;
545 WebRtcDecoderObserver decoder_observer_;
546 DecoderMap registered_decoders_;
547};
548
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000549class WebRtcOveruseObserver : public webrtc::CpuOveruseObserver {
550 public:
551 explicit WebRtcOveruseObserver(CoordinatedVideoAdapter* video_adapter)
552 : video_adapter_(video_adapter),
553 enabled_(false) {
554 }
555
556 // TODO(mflodman): Consider sending resolution as part of event, to let
557 // adapter know what resolution the request is based on. Helps eliminate stale
558 // data, race conditions.
559 virtual void OveruseDetected() OVERRIDE {
560 talk_base::CritScope cs(&crit_);
561 if (!enabled_) {
562 return;
563 }
564
565 video_adapter_->OnCpuResolutionRequest(CoordinatedVideoAdapter::DOWNGRADE);
566 }
567
568 virtual void NormalUsage() OVERRIDE {
569 talk_base::CritScope cs(&crit_);
570 if (!enabled_) {
571 return;
572 }
573
574 video_adapter_->OnCpuResolutionRequest(CoordinatedVideoAdapter::UPGRADE);
575 }
576
577 void Enable(bool enable) {
578 talk_base::CritScope cs(&crit_);
579 enabled_ = enable;
580 }
581
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000582 bool enabled() const { return enabled_; }
583
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000584 private:
585 CoordinatedVideoAdapter* video_adapter_;
586 bool enabled_;
587 talk_base::CriticalSection crit_;
588};
589
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000590
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000591class WebRtcVideoChannelSendInfo : public sigslot::has_slots<> {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000592 public:
593 typedef std::map<int, webrtc::VideoEncoder*> EncoderMap; // key: payload type
594 WebRtcVideoChannelSendInfo(int channel_id, int capture_id,
595 webrtc::ViEExternalCapture* external_capture,
596 talk_base::CpuMonitor* cpu_monitor)
597 : channel_id_(channel_id),
598 capture_id_(capture_id),
599 sending_(false),
600 muted_(false),
601 video_capturer_(NULL),
602 encoder_observer_(channel_id),
603 external_capture_(external_capture),
604 capturer_updated_(false),
605 interval_(0),
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000606 cpu_monitor_(cpu_monitor),
607 overuse_observer_enabled_(false) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000608 }
609
610 int channel_id() const { return channel_id_; }
611 int capture_id() const { return capture_id_; }
612 void set_sending(bool sending) { sending_ = sending; }
613 bool sending() const { return sending_; }
614 void set_muted(bool on) {
615 // TODO(asapersson): add support.
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000616 // video_adapter_.SetBlackOutput(on);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000617 muted_ = on;
618 }
619 bool muted() {return muted_; }
620
621 WebRtcEncoderObserver* encoder_observer() { return &encoder_observer_; }
622 webrtc::ViEExternalCapture* external_capture() { return external_capture_; }
623 const VideoFormat& video_format() const {
624 return video_format_;
625 }
626 void set_video_format(const VideoFormat& video_format) {
627 video_format_ = video_format;
628 if (video_format_ != cricket::VideoFormat()) {
629 interval_ = video_format_.interval;
630 }
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000631 CoordinatedVideoAdapter* adapter = video_adapter();
632 if (adapter) {
633 adapter->OnOutputFormatRequest(video_format_);
634 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000635 }
636 void set_interval(int64 interval) {
637 if (video_format() == cricket::VideoFormat()) {
638 interval_ = interval;
639 }
640 }
641 int64 interval() { return interval_; }
642
xians@webrtc.orgef221512014-02-21 10:31:29 +0000643 int CurrentAdaptReason() const {
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000644 const CoordinatedVideoAdapter* adapter = video_adapter();
645 if (!adapter) {
646 return CoordinatedVideoAdapter::ADAPTREASON_NONE;
647 }
648 return video_adapter()->adapt_reason();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000649 }
650
651 StreamParams* stream_params() { return stream_params_.get(); }
652 void set_stream_params(const StreamParams& sp) {
653 stream_params_.reset(new StreamParams(sp));
654 }
655 void ClearStreamParams() { stream_params_.reset(); }
656 bool has_ssrc(uint32 local_ssrc) const {
657 return !stream_params_ ? false :
658 stream_params_->has_ssrc(local_ssrc);
659 }
660 WebRtcLocalStreamInfo* local_stream_info() {
661 return &local_stream_info_;
662 }
663 VideoCapturer* video_capturer() {
664 return video_capturer_;
665 }
henrike@webrtc.orgc7bec842014-03-12 19:53:43 +0000666 void set_video_capturer(VideoCapturer* video_capturer,
667 ViEWrapper* vie_wrapper) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000668 if (video_capturer == video_capturer_) {
669 return;
670 }
xians@webrtc.orgef221512014-02-21 10:31:29 +0000671
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000672 CoordinatedVideoAdapter* old_video_adapter = video_adapter();
673 if (old_video_adapter) {
674 // Disconnect signals from old video adapter.
675 SignalCpuAdaptationUnable.disconnect(old_video_adapter);
676 if (cpu_monitor_) {
677 cpu_monitor_->SignalUpdate.disconnect(old_video_adapter);
xians@webrtc.orgef221512014-02-21 10:31:29 +0000678 }
henrike@webrtc.org26438052014-02-20 22:32:53 +0000679 }
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000680
681 capturer_updated_ = true;
682 video_capturer_ = video_capturer;
683
henrike@webrtc.orgc7bec842014-03-12 19:53:43 +0000684 vie_wrapper->base()->RegisterCpuOveruseObserver(channel_id_, NULL);
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000685 if (!video_capturer) {
686 overuse_observer_.reset();
687 return;
688 }
689
690 CoordinatedVideoAdapter* adapter = video_adapter();
691 ASSERT(adapter && "Video adapter should not be null here.");
692
693 UpdateAdapterCpuOptions();
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000694
695 overuse_observer_.reset(new WebRtcOveruseObserver(adapter));
henrike@webrtc.orgc7bec842014-03-12 19:53:43 +0000696 vie_wrapper->base()->RegisterCpuOveruseObserver(channel_id_,
697 overuse_observer_.get());
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000698 // (Dis)connect the video adapter from the cpu monitor as appropriate.
699 SetCpuOveruseDetection(overuse_observer_enabled_);
700
701 SignalCpuAdaptationUnable.repeat(adapter->SignalCpuAdaptationUnable);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000702 }
703
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000704 CoordinatedVideoAdapter* video_adapter() {
705 if (!video_capturer_) {
706 return NULL;
707 }
708 return video_capturer_->video_adapter();
709 }
710 const CoordinatedVideoAdapter* video_adapter() const {
711 if (!video_capturer_) {
712 return NULL;
713 }
714 return video_capturer_->video_adapter();
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000715 }
716
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000717 void ApplyCpuOptions(const VideoOptions& video_options) {
718 // Use video_options_.SetAll() instead of assignment so that unset value in
719 // video_options will not overwrite the previous option value.
720 video_options_.SetAll(video_options);
721 UpdateAdapterCpuOptions();
722 }
723
724 void UpdateAdapterCpuOptions() {
725 if (!video_capturer_) {
726 return;
727 }
728
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000729 bool cpu_adapt, cpu_smoothing, adapt_third;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000730 float low, med, high;
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000731
732 // TODO(thorcarpenter): Have VideoAdapter be responsible for setting
733 // all these video options.
734 CoordinatedVideoAdapter* video_adapter = video_capturer_->video_adapter();
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000735 if (video_options_.adapt_input_to_cpu_usage.Get(&cpu_adapt) ||
736 overuse_observer_enabled_) {
737 video_adapter->set_cpu_adaptation(cpu_adapt || overuse_observer_enabled_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000738 }
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000739 if (video_options_.adapt_cpu_with_smoothing.Get(&cpu_smoothing)) {
740 video_adapter->set_cpu_smoothing(cpu_smoothing);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000741 }
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000742 if (video_options_.process_adaptation_threshhold.Get(&med)) {
743 video_adapter->set_process_threshold(med);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000744 }
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000745 if (video_options_.system_low_adaptation_threshhold.Get(&low)) {
746 video_adapter->set_low_system_threshold(low);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000747 }
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000748 if (video_options_.system_high_adaptation_threshhold.Get(&high)) {
749 video_adapter->set_high_system_threshold(high);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000750 }
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000751 if (video_options_.video_adapt_third.Get(&adapt_third)) {
752 video_adapter->set_scale_third(adapt_third);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000753 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000754 }
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000755
756 void SetCpuOveruseDetection(bool enable) {
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000757 overuse_observer_enabled_ = enable;
758
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000759 if (overuse_observer_) {
760 overuse_observer_->Enable(enable);
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000761 }
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000762
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000763 // The video adapter is signaled by overuse detection if enabled; otherwise
764 // it will be signaled by cpu monitor.
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000765 CoordinatedVideoAdapter* adapter = video_adapter();
766 if (adapter) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000767 bool cpu_adapt = false;
768 video_options_.adapt_input_to_cpu_usage.Get(&cpu_adapt);
769 adapter->set_cpu_adaptation(
770 adapter->cpu_adaptation() || cpu_adapt || enable);
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000771 if (cpu_monitor_) {
772 if (enable) {
773 cpu_monitor_->SignalUpdate.disconnect(adapter);
774 } else {
775 cpu_monitor_->SignalUpdate.connect(
776 adapter, &CoordinatedVideoAdapter::OnCpuLoadUpdated);
777 }
778 }
779 }
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000780 }
781
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000782 void ProcessFrame(const VideoFrame& original_frame, bool mute,
783 VideoFrame** processed_frame) {
784 if (!mute) {
785 *processed_frame = original_frame.Copy();
786 } else {
787 WebRtcVideoFrame* black_frame = new WebRtcVideoFrame();
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000788 black_frame->InitToBlack(static_cast<int>(original_frame.GetWidth()),
789 static_cast<int>(original_frame.GetHeight()),
790 1, 1,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000791 original_frame.GetElapsedTime(),
792 original_frame.GetTimeStamp());
793 *processed_frame = black_frame;
794 }
795 local_stream_info_.UpdateFrame(*processed_frame);
796 }
797 void RegisterEncoder(int pl_type, webrtc::VideoEncoder* encoder) {
798 ASSERT(!IsEncoderRegistered(pl_type));
799 registered_encoders_[pl_type] = encoder;
800 }
801 bool IsEncoderRegistered(int pl_type) {
802 return registered_encoders_.count(pl_type) != 0;
803 }
804 const EncoderMap& registered_encoders() {
805 return registered_encoders_;
806 }
807 void ClearRegisteredEncoders() {
808 registered_encoders_.clear();
809 }
810
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000811 sigslot::repeater0<> SignalCpuAdaptationUnable;
812
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000813 private:
814 int channel_id_;
815 int capture_id_;
816 bool sending_;
817 bool muted_;
818 VideoCapturer* video_capturer_;
819 WebRtcEncoderObserver encoder_observer_;
820 webrtc::ViEExternalCapture* external_capture_;
821 EncoderMap registered_encoders_;
822
823 VideoFormat video_format_;
824
825 talk_base::scoped_ptr<StreamParams> stream_params_;
826
827 WebRtcLocalStreamInfo local_stream_info_;
828
829 bool capturer_updated_;
830
831 int64 interval_;
832
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000833 talk_base::CpuMonitor* cpu_monitor_;
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000834 talk_base::scoped_ptr<WebRtcOveruseObserver> overuse_observer_;
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000835 bool overuse_observer_enabled_;
836
837 VideoOptions video_options_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000838};
839
840const WebRtcVideoEngine::VideoCodecPref
841 WebRtcVideoEngine::kVideoCodecPrefs[] = {
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000842 {kVp8PayloadName, 100, -1, 0},
843 {kRedPayloadName, 116, -1, 1},
844 {kFecPayloadName, 117, -1, 2},
845 {kRtxCodecName, 96, 100, 3},
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000846};
847
848// The formats are sorted by the descending order of width. We use the order to
849// find the next format for CPU and bandwidth adaptation.
850const VideoFormatPod WebRtcVideoEngine::kVideoFormats[] = {
851 {1280, 800, FPS_TO_INTERVAL(30), FOURCC_ANY},
852 {1280, 720, FPS_TO_INTERVAL(30), FOURCC_ANY},
853 {960, 600, FPS_TO_INTERVAL(30), FOURCC_ANY},
854 {960, 540, FPS_TO_INTERVAL(30), FOURCC_ANY},
855 {640, 400, FPS_TO_INTERVAL(30), FOURCC_ANY},
856 {640, 360, FPS_TO_INTERVAL(30), FOURCC_ANY},
857 {640, 480, FPS_TO_INTERVAL(30), FOURCC_ANY},
858 {480, 300, FPS_TO_INTERVAL(30), FOURCC_ANY},
859 {480, 270, FPS_TO_INTERVAL(30), FOURCC_ANY},
860 {480, 360, FPS_TO_INTERVAL(30), FOURCC_ANY},
861 {320, 200, FPS_TO_INTERVAL(30), FOURCC_ANY},
862 {320, 180, FPS_TO_INTERVAL(30), FOURCC_ANY},
863 {320, 240, FPS_TO_INTERVAL(30), FOURCC_ANY},
864 {240, 150, FPS_TO_INTERVAL(30), FOURCC_ANY},
865 {240, 135, FPS_TO_INTERVAL(30), FOURCC_ANY},
866 {240, 180, FPS_TO_INTERVAL(30), FOURCC_ANY},
867 {160, 100, FPS_TO_INTERVAL(30), FOURCC_ANY},
868 {160, 90, FPS_TO_INTERVAL(30), FOURCC_ANY},
869 {160, 120, FPS_TO_INTERVAL(30), FOURCC_ANY},
870};
871
872const VideoFormatPod WebRtcVideoEngine::kDefaultVideoFormat =
873 {640, 400, FPS_TO_INTERVAL(30), FOURCC_ANY};
874
875static void UpdateVideoCodec(const cricket::VideoFormat& video_format,
876 webrtc::VideoCodec* target_codec) {
877 if ((target_codec == NULL) || (video_format == cricket::VideoFormat())) {
878 return;
879 }
880 target_codec->width = video_format.width;
881 target_codec->height = video_format.height;
882 target_codec->maxFramerate = cricket::VideoFormat::IntervalToFps(
883 video_format.interval);
884}
885
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000886static bool GetCpuOveruseOptions(const VideoOptions& options,
887 webrtc::CpuOveruseOptions* overuse_options) {
888 int underuse_threshold = 0;
889 int overuse_threshold = 0;
890 if (!options.cpu_underuse_threshold.Get(&underuse_threshold) ||
891 !options.cpu_overuse_threshold.Get(&overuse_threshold)) {
892 return false;
893 }
894 if (underuse_threshold <= 0 || overuse_threshold <= 0) {
895 return false;
896 }
897 // Valid thresholds.
898 bool encode_usage =
899 options.cpu_overuse_encode_usage.GetWithDefaultIfUnset(false);
900 overuse_options->enable_capture_jitter_method = !encode_usage;
901 overuse_options->enable_encode_usage_method = encode_usage;
902 if (encode_usage) {
903 // Use method based on encode usage.
904 overuse_options->low_encode_usage_threshold_percent = underuse_threshold;
905 overuse_options->high_encode_usage_threshold_percent = overuse_threshold;
906 } else {
907 // Use default method based on capture jitter.
908 overuse_options->low_capture_jitter_threshold_ms =
909 static_cast<float>(underuse_threshold);
910 overuse_options->high_capture_jitter_threshold_ms =
911 static_cast<float>(overuse_threshold);
912 }
913 return true;
914}
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000915
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000916WebRtcVideoEngine::WebRtcVideoEngine() {
917 Construct(new ViEWrapper(), new ViETraceWrapper(), NULL,
918 new talk_base::CpuMonitor(NULL));
919}
920
921WebRtcVideoEngine::WebRtcVideoEngine(WebRtcVoiceEngine* voice_engine,
922 ViEWrapper* vie_wrapper,
923 talk_base::CpuMonitor* cpu_monitor) {
924 Construct(vie_wrapper, new ViETraceWrapper(), voice_engine, cpu_monitor);
925}
926
927WebRtcVideoEngine::WebRtcVideoEngine(WebRtcVoiceEngine* voice_engine,
928 ViEWrapper* vie_wrapper,
929 ViETraceWrapper* tracing,
930 talk_base::CpuMonitor* cpu_monitor) {
931 Construct(vie_wrapper, tracing, voice_engine, cpu_monitor);
932}
933
934void WebRtcVideoEngine::Construct(ViEWrapper* vie_wrapper,
935 ViETraceWrapper* tracing,
936 WebRtcVoiceEngine* voice_engine,
937 talk_base::CpuMonitor* cpu_monitor) {
938 LOG(LS_INFO) << "WebRtcVideoEngine::WebRtcVideoEngine";
939 worker_thread_ = NULL;
940 vie_wrapper_.reset(vie_wrapper);
941 vie_wrapper_base_initialized_ = false;
942 tracing_.reset(tracing);
943 voice_engine_ = voice_engine;
944 initialized_ = false;
945 SetTraceFilter(SeverityToFilter(kDefaultLogSeverity));
946 render_module_.reset(new WebRtcPassthroughRender());
947 local_renderer_w_ = local_renderer_h_ = 0;
948 local_renderer_ = NULL;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000949 capture_started_ = false;
950 decoder_factory_ = NULL;
951 encoder_factory_ = NULL;
952 cpu_monitor_.reset(cpu_monitor);
953
954 SetTraceOptions("");
955 if (tracing_->SetTraceCallback(this) != 0) {
956 LOG_RTCERR1(SetTraceCallback, this);
957 }
958
959 // Set default quality levels for our supported codecs. We override them here
960 // if we know your cpu performance is low, and they can be updated explicitly
961 // by calling SetDefaultCodec. For example by a flute preference setting, or
962 // by the server with a jec in response to our reported system info.
963 VideoCodec max_codec(kVideoCodecPrefs[0].payload_type,
964 kVideoCodecPrefs[0].name,
965 kDefaultVideoFormat.width,
966 kDefaultVideoFormat.height,
967 VideoFormat::IntervalToFps(kDefaultVideoFormat.interval),
968 0);
969 if (!SetDefaultCodec(max_codec)) {
970 LOG(LS_ERROR) << "Failed to initialize list of supported codec types";
971 }
972
973
974 // Load our RTP Header extensions.
975 rtp_header_extensions_.push_back(
976 RtpHeaderExtension(kRtpTimestampOffsetHeaderExtension,
henrike@webrtc.org79047f92014-03-06 23:46:59 +0000977 kRtpTimestampOffsetHeaderExtensionDefaultId));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000978 rtp_header_extensions_.push_back(
henrike@webrtc.org79047f92014-03-06 23:46:59 +0000979 RtpHeaderExtension(kRtpAbsoluteSenderTimeHeaderExtension,
980 kRtpAbsoluteSenderTimeHeaderExtensionDefaultId));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000981}
982
983WebRtcVideoEngine::~WebRtcVideoEngine() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000984 LOG(LS_INFO) << "WebRtcVideoEngine::~WebRtcVideoEngine";
985 if (initialized_) {
986 Terminate();
987 }
988 if (encoder_factory_) {
989 encoder_factory_->RemoveObserver(this);
990 }
991 tracing_->SetTraceCallback(NULL);
992 // Test to see if the media processor was deregistered properly.
993 ASSERT(SignalMediaFrame.is_empty());
994}
995
996bool WebRtcVideoEngine::Init(talk_base::Thread* worker_thread) {
997 LOG(LS_INFO) << "WebRtcVideoEngine::Init";
998 worker_thread_ = worker_thread;
999 ASSERT(worker_thread_ != NULL);
1000
1001 cpu_monitor_->set_thread(worker_thread_);
1002 if (!cpu_monitor_->Start(kCpuMonitorPeriodMs)) {
1003 LOG(LS_ERROR) << "Failed to start CPU monitor.";
1004 cpu_monitor_.reset();
1005 }
1006
1007 bool result = InitVideoEngine();
1008 if (result) {
1009 LOG(LS_INFO) << "VideoEngine Init done";
1010 } else {
1011 LOG(LS_ERROR) << "VideoEngine Init failed, releasing";
1012 Terminate();
1013 }
1014 return result;
1015}
1016
1017bool WebRtcVideoEngine::InitVideoEngine() {
1018 LOG(LS_INFO) << "WebRtcVideoEngine::InitVideoEngine";
1019
1020 // Init WebRTC VideoEngine.
1021 if (!vie_wrapper_base_initialized_) {
1022 if (vie_wrapper_->base()->Init() != 0) {
1023 LOG_RTCERR0(Init);
1024 return false;
1025 }
1026 vie_wrapper_base_initialized_ = true;
1027 }
1028
1029 // Log the VoiceEngine version info.
1030 char buffer[1024] = "";
1031 if (vie_wrapper_->base()->GetVersion(buffer) != 0) {
1032 LOG_RTCERR0(GetVersion);
1033 return false;
1034 }
1035
1036 LOG(LS_INFO) << "WebRtc VideoEngine Version:";
1037 LogMultiline(talk_base::LS_INFO, buffer);
1038
1039 // Hook up to VoiceEngine for sync purposes, if supplied.
1040 if (!voice_engine_) {
1041 LOG(LS_WARNING) << "NULL voice engine";
1042 } else if ((vie_wrapper_->base()->SetVoiceEngine(
1043 voice_engine_->voe()->engine())) != 0) {
1044 LOG_RTCERR0(SetVoiceEngine);
1045 return false;
1046 }
1047
1048 // Register our custom render module.
1049 if (vie_wrapper_->render()->RegisterVideoRenderModule(
1050 *render_module_.get()) != 0) {
1051 LOG_RTCERR0(RegisterVideoRenderModule);
1052 return false;
1053 }
1054
1055 initialized_ = true;
1056 return true;
1057}
1058
1059void WebRtcVideoEngine::Terminate() {
1060 LOG(LS_INFO) << "WebRtcVideoEngine::Terminate";
1061 initialized_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001062
1063 if (vie_wrapper_->render()->DeRegisterVideoRenderModule(
1064 *render_module_.get()) != 0) {
1065 LOG_RTCERR0(DeRegisterVideoRenderModule);
1066 }
1067
1068 if (vie_wrapper_->base()->SetVoiceEngine(NULL) != 0) {
1069 LOG_RTCERR0(SetVoiceEngine);
1070 }
1071
1072 cpu_monitor_->Stop();
1073}
1074
1075int WebRtcVideoEngine::GetCapabilities() {
1076 return VIDEO_RECV | VIDEO_SEND;
1077}
1078
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00001079bool WebRtcVideoEngine::SetOptions(const VideoOptions &options) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001080 return true;
1081}
1082
1083bool WebRtcVideoEngine::SetDefaultEncoderConfig(
1084 const VideoEncoderConfig& config) {
1085 return SetDefaultCodec(config.max_codec);
1086}
1087
wu@webrtc.org78187522013-10-07 23:32:02 +00001088VideoEncoderConfig WebRtcVideoEngine::GetDefaultEncoderConfig() const {
1089 ASSERT(!video_codecs_.empty());
1090 VideoCodec max_codec(kVideoCodecPrefs[0].payload_type,
1091 kVideoCodecPrefs[0].name,
1092 video_codecs_[0].width,
1093 video_codecs_[0].height,
1094 video_codecs_[0].framerate,
1095 0);
1096 return VideoEncoderConfig(max_codec);
1097}
1098
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001099// SetDefaultCodec may be called while the capturer is running. For example, a
1100// test call is started in a page with QVGA default codec, and then a real call
1101// is started in another page with VGA default codec. This is the corner case
1102// and happens only when a session is started. We ignore this case currently.
1103bool WebRtcVideoEngine::SetDefaultCodec(const VideoCodec& codec) {
1104 if (!RebuildCodecList(codec)) {
1105 LOG(LS_WARNING) << "Failed to RebuildCodecList";
1106 return false;
1107 }
1108
wu@webrtc.org78187522013-10-07 23:32:02 +00001109 ASSERT(!video_codecs_.empty());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001110 default_codec_format_ = VideoFormat(
1111 video_codecs_[0].width,
1112 video_codecs_[0].height,
1113 VideoFormat::FpsToInterval(video_codecs_[0].framerate),
1114 FOURCC_ANY);
1115 return true;
1116}
1117
1118WebRtcVideoMediaChannel* WebRtcVideoEngine::CreateChannel(
1119 VoiceMediaChannel* voice_channel) {
1120 WebRtcVideoMediaChannel* channel =
1121 new WebRtcVideoMediaChannel(this, voice_channel);
1122 if (!channel->Init()) {
1123 delete channel;
1124 channel = NULL;
1125 }
1126 return channel;
1127}
1128
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001129bool WebRtcVideoEngine::SetLocalRenderer(VideoRenderer* renderer) {
1130 local_renderer_w_ = local_renderer_h_ = 0;
1131 local_renderer_ = renderer;
1132 return true;
1133}
1134
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001135const std::vector<VideoCodec>& WebRtcVideoEngine::codecs() const {
1136 return video_codecs_;
1137}
1138
1139const std::vector<RtpHeaderExtension>&
1140WebRtcVideoEngine::rtp_header_extensions() const {
1141 return rtp_header_extensions_;
1142}
1143
1144void WebRtcVideoEngine::SetLogging(int min_sev, const char* filter) {
1145 // if min_sev == -1, we keep the current log level.
1146 if (min_sev >= 0) {
1147 SetTraceFilter(SeverityToFilter(min_sev));
1148 }
1149 SetTraceOptions(filter);
1150}
1151
1152int WebRtcVideoEngine::GetLastEngineError() {
1153 return vie_wrapper_->error();
1154}
1155
1156// Checks to see whether we comprehend and could receive a particular codec
1157bool WebRtcVideoEngine::FindCodec(const VideoCodec& in) {
1158 for (int i = 0; i < ARRAY_SIZE(kVideoFormats); ++i) {
1159 const VideoFormat fmt(kVideoFormats[i]);
1160 if ((in.width == 0 && in.height == 0) ||
1161 (fmt.width == in.width && fmt.height == in.height)) {
1162 if (encoder_factory_) {
1163 const std::vector<WebRtcVideoEncoderFactory::VideoCodec>& codecs =
1164 encoder_factory_->codecs();
1165 for (size_t j = 0; j < codecs.size(); ++j) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001166 VideoCodec codec(GetExternalVideoPayloadType(static_cast<int>(j)),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001167 codecs[j].name, 0, 0, 0, 0);
1168 if (codec.Matches(in))
1169 return true;
1170 }
1171 }
1172 for (size_t j = 0; j < ARRAY_SIZE(kVideoCodecPrefs); ++j) {
1173 VideoCodec codec(kVideoCodecPrefs[j].payload_type,
1174 kVideoCodecPrefs[j].name, 0, 0, 0, 0);
1175 if (codec.Matches(in)) {
1176 return true;
1177 }
1178 }
1179 }
1180 }
1181 return false;
1182}
1183
1184// Given the requested codec, returns true if we can send that codec type and
1185// updates out with the best quality we could send for that codec. If current is
1186// not empty, we constrain out so that its aspect ratio matches current's.
1187bool WebRtcVideoEngine::CanSendCodec(const VideoCodec& requested,
1188 const VideoCodec& current,
1189 VideoCodec* out) {
1190 if (!out) {
1191 return false;
1192 }
1193
1194 std::vector<VideoCodec>::const_iterator local_max;
1195 for (local_max = video_codecs_.begin();
1196 local_max < video_codecs_.end();
1197 ++local_max) {
1198 // First match codecs by payload type
1199 if (!requested.Matches(*local_max)) {
1200 continue;
1201 }
1202
1203 out->id = requested.id;
1204 out->name = requested.name;
1205 out->preference = requested.preference;
1206 out->params = requested.params;
1207 out->framerate = talk_base::_min(requested.framerate, local_max->framerate);
1208 out->width = 0;
1209 out->height = 0;
1210 out->params = requested.params;
1211 out->feedback_params = requested.feedback_params;
1212
1213 if (0 == requested.width && 0 == requested.height) {
1214 // Special case with resolution 0. The channel should not send frames.
1215 return true;
1216 } else if (0 == requested.width || 0 == requested.height) {
1217 // 0xn and nx0 are invalid resolutions.
1218 return false;
1219 }
1220
1221 // Pick the best quality that is within their and our bounds and has the
1222 // correct aspect ratio.
1223 for (int j = 0; j < ARRAY_SIZE(kVideoFormats); ++j) {
1224 const VideoFormat format(kVideoFormats[j]);
1225
1226 // Skip any format that is larger than the local or remote maximums, or
1227 // smaller than the current best match
1228 if (format.width > requested.width || format.height > requested.height ||
1229 format.width > local_max->width ||
1230 (format.width < out->width && format.height < out->height)) {
1231 continue;
1232 }
1233
1234 bool better = false;
1235
1236 // Check any further constraints on this prospective format
1237 if (!out->width || !out->height) {
1238 // If we don't have any matches yet, this is the best so far.
1239 better = true;
1240 } else if (current.width && current.height) {
1241 // current is set so format must match its ratio exactly.
1242 better =
1243 (format.width * current.height == format.height * current.width);
1244 } else {
1245 // Prefer closer aspect ratios i.e
1246 // format.aspect - requested.aspect < out.aspect - requested.aspect
1247 better = abs(format.width * requested.height * out->height -
1248 requested.width * format.height * out->height) <
1249 abs(out->width * format.height * requested.height -
1250 requested.width * format.height * out->height);
1251 }
1252
1253 if (better) {
1254 out->width = format.width;
1255 out->height = format.height;
1256 }
1257 }
1258 if (out->width > 0) {
1259 return true;
1260 }
1261 }
1262 return false;
1263}
1264
1265static void ConvertToCricketVideoCodec(
1266 const webrtc::VideoCodec& in_codec, VideoCodec* out_codec) {
1267 out_codec->id = in_codec.plType;
1268 out_codec->name = in_codec.plName;
1269 out_codec->width = in_codec.width;
1270 out_codec->height = in_codec.height;
1271 out_codec->framerate = in_codec.maxFramerate;
buildbot@webrtc.orgd1ae89f2014-05-08 19:19:26 +00001272 if (BitrateIsSet(in_codec.minBitrate)) {
1273 out_codec->SetParam(kCodecParamMinBitrate, in_codec.minBitrate);
1274 }
1275 if (BitrateIsSet(in_codec.maxBitrate)) {
1276 out_codec->SetParam(kCodecParamMaxBitrate, in_codec.maxBitrate);
1277 }
1278 if (BitrateIsSet(in_codec.startBitrate)) {
1279 out_codec->SetParam(kCodecParamStartBitrate, in_codec.startBitrate);
1280 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001281 if (in_codec.qpMax) {
1282 out_codec->SetParam(kCodecParamMaxQuantization, in_codec.qpMax);
1283 }
1284}
1285
1286bool WebRtcVideoEngine::ConvertFromCricketVideoCodec(
1287 const VideoCodec& in_codec, webrtc::VideoCodec* out_codec) {
1288 bool found = false;
1289 int ncodecs = vie_wrapper_->codec()->NumberOfCodecs();
1290 for (int i = 0; i < ncodecs; ++i) {
1291 if (vie_wrapper_->codec()->GetCodec(i, *out_codec) == 0 &&
1292 _stricmp(in_codec.name.c_str(), out_codec->plName) == 0) {
1293 found = true;
1294 break;
1295 }
1296 }
1297
1298 // If not found, check if this is supported by external encoder factory.
1299 if (!found && encoder_factory_) {
1300 const std::vector<WebRtcVideoEncoderFactory::VideoCodec>& codecs =
1301 encoder_factory_->codecs();
1302 for (size_t i = 0; i < codecs.size(); ++i) {
1303 if (_stricmp(in_codec.name.c_str(), codecs[i].name.c_str()) == 0) {
1304 out_codec->codecType = codecs[i].type;
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001305 out_codec->plType = GetExternalVideoPayloadType(static_cast<int>(i));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001306 talk_base::strcpyn(out_codec->plName, sizeof(out_codec->plName),
1307 codecs[i].name.c_str(), codecs[i].name.length());
1308 found = true;
1309 break;
1310 }
1311 }
1312 }
1313
buildbot@webrtc.orgdd4742a2014-05-07 14:50:35 +00001314 // Is this an RTX codec? Handled separately here since webrtc doesn't handle
1315 // them as webrtc::VideoCodec internally.
1316 if (!found && _stricmp(in_codec.name.c_str(), kRtxCodecName) == 0) {
1317 talk_base::strcpyn(out_codec->plName, sizeof(out_codec->plName),
1318 in_codec.name.c_str(), in_codec.name.length());
1319 out_codec->plType = in_codec.id;
1320 found = true;
1321 }
1322
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001323 if (!found) {
1324 LOG(LS_ERROR) << "invalid codec type";
1325 return false;
1326 }
1327
1328 if (in_codec.id != 0)
1329 out_codec->plType = in_codec.id;
1330
1331 if (in_codec.width != 0)
1332 out_codec->width = in_codec.width;
1333
1334 if (in_codec.height != 0)
1335 out_codec->height = in_codec.height;
1336
1337 if (in_codec.framerate != 0)
1338 out_codec->maxFramerate = in_codec.framerate;
1339
1340 // Convert bitrate parameters.
buildbot@webrtc.orgd1ae89f2014-05-08 19:19:26 +00001341 int max_bitrate = -1;
1342 int min_bitrate = -1;
1343 int start_bitrate = -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001344
1345 in_codec.GetParam(kCodecParamMinBitrate, &min_bitrate);
1346 in_codec.GetParam(kCodecParamMaxBitrate, &max_bitrate);
buildbot@webrtc.orged97bb02014-05-07 11:15:20 +00001347 in_codec.GetParam(kCodecParamStartBitrate, &start_bitrate);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001348
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001349
1350 out_codec->minBitrate = min_bitrate;
1351 out_codec->startBitrate = start_bitrate;
1352 out_codec->maxBitrate = max_bitrate;
1353
1354 // Convert general codec parameters.
1355 int max_quantization = 0;
1356 if (in_codec.GetParam(kCodecParamMaxQuantization, &max_quantization)) {
1357 if (max_quantization < 0) {
1358 return false;
1359 }
1360 out_codec->qpMax = max_quantization;
1361 }
1362 return true;
1363}
1364
1365void WebRtcVideoEngine::RegisterChannel(WebRtcVideoMediaChannel *channel) {
1366 talk_base::CritScope cs(&channels_crit_);
1367 channels_.push_back(channel);
1368}
1369
1370void WebRtcVideoEngine::UnregisterChannel(WebRtcVideoMediaChannel *channel) {
1371 talk_base::CritScope cs(&channels_crit_);
1372 channels_.erase(std::remove(channels_.begin(), channels_.end(), channel),
1373 channels_.end());
1374}
1375
1376bool WebRtcVideoEngine::SetVoiceEngine(WebRtcVoiceEngine* voice_engine) {
1377 if (initialized_) {
1378 LOG(LS_WARNING) << "SetVoiceEngine can not be called after Init";
1379 return false;
1380 }
1381 voice_engine_ = voice_engine;
1382 return true;
1383}
1384
1385bool WebRtcVideoEngine::EnableTimedRender() {
1386 if (initialized_) {
1387 LOG(LS_WARNING) << "EnableTimedRender can not be called after Init";
1388 return false;
1389 }
1390 render_module_.reset(webrtc::VideoRender::CreateVideoRender(0, NULL,
1391 false, webrtc::kRenderExternal));
1392 return true;
1393}
1394
1395void WebRtcVideoEngine::SetTraceFilter(int filter) {
1396 tracing_->SetTraceFilter(filter);
1397}
1398
1399// See https://sites.google.com/a/google.com/wavelet/
1400// Home/Magic-Flute--RTC-Engine-/Magic-Flute-Command-Line-Parameters
1401// for all supported command line setttings.
1402void WebRtcVideoEngine::SetTraceOptions(const std::string& options) {
1403 // Set WebRTC trace file.
1404 std::vector<std::string> opts;
1405 talk_base::tokenize(options, ' ', '"', '"', &opts);
1406 std::vector<std::string>::iterator tracefile =
1407 std::find(opts.begin(), opts.end(), "tracefile");
1408 if (tracefile != opts.end() && ++tracefile != opts.end()) {
1409 // Write WebRTC debug output (at same loglevel) to file
1410 if (tracing_->SetTraceFile(tracefile->c_str()) == -1) {
1411 LOG_RTCERR1(SetTraceFile, *tracefile);
1412 }
1413 }
1414}
1415
1416static void AddDefaultFeedbackParams(VideoCodec* codec) {
1417 const FeedbackParam kFir(kRtcpFbParamCcm, kRtcpFbCcmParamFir);
1418 codec->AddFeedbackParam(kFir);
1419 const FeedbackParam kNack(kRtcpFbParamNack, kParamValueEmpty);
1420 codec->AddFeedbackParam(kNack);
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001421 const FeedbackParam kPli(kRtcpFbParamNack, kRtcpFbNackParamPli);
1422 codec->AddFeedbackParam(kPli);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001423 const FeedbackParam kRemb(kRtcpFbParamRemb, kParamValueEmpty);
1424 codec->AddFeedbackParam(kRemb);
1425}
1426
1427// Rebuilds the codec list to be only those that are less intensive
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00001428// than the specified codec. Prefers internal codec over external with
1429// higher preference field.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001430bool WebRtcVideoEngine::RebuildCodecList(const VideoCodec& in_codec) {
1431 if (!FindCodec(in_codec))
1432 return false;
1433
1434 video_codecs_.clear();
1435
1436 bool found = false;
mallinath@webrtc.org1112c302013-09-23 20:34:45 +00001437 std::set<std::string> internal_codec_names;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001438 for (size_t i = 0; i < ARRAY_SIZE(kVideoCodecPrefs); ++i) {
1439 const VideoCodecPref& pref(kVideoCodecPrefs[i]);
1440 if (!found)
1441 found = (in_codec.name == pref.name);
mallinath@webrtc.org1112c302013-09-23 20:34:45 +00001442 if (found) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001443 VideoCodec codec(pref.payload_type, pref.name,
1444 in_codec.width, in_codec.height, in_codec.framerate,
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001445 static_cast<int>(ARRAY_SIZE(kVideoCodecPrefs) - i));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001446 if (_stricmp(kVp8PayloadName, codec.name.c_str()) == 0) {
1447 AddDefaultFeedbackParams(&codec);
1448 }
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00001449 if (pref.associated_payload_type != -1) {
1450 codec.SetParam(kCodecParamAssociatedPayloadType,
1451 pref.associated_payload_type);
1452 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001453 video_codecs_.push_back(codec);
mallinath@webrtc.org1112c302013-09-23 20:34:45 +00001454 internal_codec_names.insert(codec.name);
1455 }
1456 }
1457 if (encoder_factory_) {
1458 const std::vector<WebRtcVideoEncoderFactory::VideoCodec>& codecs =
1459 encoder_factory_->codecs();
1460 for (size_t i = 0; i < codecs.size(); ++i) {
1461 bool is_internal_codec = internal_codec_names.find(codecs[i].name) !=
1462 internal_codec_names.end();
1463 if (!is_internal_codec) {
1464 if (!found)
1465 found = (in_codec.name == codecs[i].name);
1466 VideoCodec codec(
1467 GetExternalVideoPayloadType(static_cast<int>(i)),
1468 codecs[i].name,
1469 codecs[i].max_width,
1470 codecs[i].max_height,
1471 codecs[i].max_fps,
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00001472 // Use negative preference on external codec to ensure the internal
1473 // codec is preferred.
1474 static_cast<int>(0 - i));
mallinath@webrtc.org1112c302013-09-23 20:34:45 +00001475 AddDefaultFeedbackParams(&codec);
1476 video_codecs_.push_back(codec);
1477 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001478 }
1479 }
1480 ASSERT(found);
1481 return true;
1482}
1483
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001484// Ignore spammy trace messages, mostly from the stats API when we haven't
1485// gotten RTCP info yet from the remote side.
1486bool WebRtcVideoEngine::ShouldIgnoreTrace(const std::string& trace) {
1487 static const char* const kTracesToIgnore[] = {
1488 NULL
1489 };
1490 for (const char* const* p = kTracesToIgnore; *p; ++p) {
1491 if (trace.find(*p) == 0) {
1492 return true;
1493 }
1494 }
1495 return false;
1496}
1497
1498int WebRtcVideoEngine::GetNumOfChannels() {
1499 talk_base::CritScope cs(&channels_crit_);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001500 return static_cast<int>(channels_.size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001501}
1502
1503void WebRtcVideoEngine::Print(webrtc::TraceLevel level, const char* trace,
1504 int length) {
1505 talk_base::LoggingSeverity sev = talk_base::LS_VERBOSE;
1506 if (level == webrtc::kTraceError || level == webrtc::kTraceCritical)
1507 sev = talk_base::LS_ERROR;
1508 else if (level == webrtc::kTraceWarning)
1509 sev = talk_base::LS_WARNING;
1510 else if (level == webrtc::kTraceStateInfo || level == webrtc::kTraceInfo)
1511 sev = talk_base::LS_INFO;
1512 else if (level == webrtc::kTraceTerseInfo)
1513 sev = talk_base::LS_INFO;
1514
1515 // Skip past boilerplate prefix text
1516 if (length < 72) {
1517 std::string msg(trace, length);
1518 LOG(LS_ERROR) << "Malformed webrtc log message: ";
1519 LOG_V(sev) << msg;
1520 } else {
1521 std::string msg(trace + 71, length - 72);
1522 if (!ShouldIgnoreTrace(msg) &&
1523 (!voice_engine_ || !voice_engine_->ShouldIgnoreTrace(msg))) {
1524 LOG_V(sev) << "webrtc: " << msg;
1525 }
1526 }
1527}
1528
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001529webrtc::VideoDecoder* WebRtcVideoEngine::CreateExternalDecoder(
1530 webrtc::VideoCodecType type) {
1531 if (decoder_factory_ == NULL) {
1532 return NULL;
1533 }
1534 return decoder_factory_->CreateVideoDecoder(type);
1535}
1536
1537void WebRtcVideoEngine::DestroyExternalDecoder(webrtc::VideoDecoder* decoder) {
1538 ASSERT(decoder_factory_ != NULL);
1539 if (decoder_factory_ == NULL)
1540 return;
1541 decoder_factory_->DestroyVideoDecoder(decoder);
1542}
1543
1544webrtc::VideoEncoder* WebRtcVideoEngine::CreateExternalEncoder(
1545 webrtc::VideoCodecType type) {
1546 if (encoder_factory_ == NULL) {
1547 return NULL;
1548 }
1549 return encoder_factory_->CreateVideoEncoder(type);
1550}
1551
1552void WebRtcVideoEngine::DestroyExternalEncoder(webrtc::VideoEncoder* encoder) {
1553 ASSERT(encoder_factory_ != NULL);
1554 if (encoder_factory_ == NULL)
1555 return;
1556 encoder_factory_->DestroyVideoEncoder(encoder);
1557}
1558
1559bool WebRtcVideoEngine::IsExternalEncoderCodecType(
1560 webrtc::VideoCodecType type) const {
1561 if (!encoder_factory_)
1562 return false;
1563 const std::vector<WebRtcVideoEncoderFactory::VideoCodec>& codecs =
1564 encoder_factory_->codecs();
1565 std::vector<WebRtcVideoEncoderFactory::VideoCodec>::const_iterator it;
1566 for (it = codecs.begin(); it != codecs.end(); ++it) {
1567 if (it->type == type)
1568 return true;
1569 }
1570 return false;
1571}
1572
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001573void WebRtcVideoEngine::SetExternalDecoderFactory(
1574 WebRtcVideoDecoderFactory* decoder_factory) {
1575 decoder_factory_ = decoder_factory;
1576}
1577
1578void WebRtcVideoEngine::SetExternalEncoderFactory(
1579 WebRtcVideoEncoderFactory* encoder_factory) {
1580 if (encoder_factory_ == encoder_factory)
1581 return;
1582
1583 if (encoder_factory_) {
1584 encoder_factory_->RemoveObserver(this);
1585 }
1586 encoder_factory_ = encoder_factory;
1587 if (encoder_factory_) {
1588 encoder_factory_->AddObserver(this);
1589 }
1590
1591 // Invoke OnCodecAvailable() here in case the list of codecs is already
1592 // available when the encoder factory is installed. If not the encoder
1593 // factory will invoke the callback later when the codecs become available.
1594 OnCodecsAvailable();
1595}
1596
1597void WebRtcVideoEngine::OnCodecsAvailable() {
1598 // Rebuild codec list while reapplying the current default codec format.
1599 VideoCodec max_codec(kVideoCodecPrefs[0].payload_type,
1600 kVideoCodecPrefs[0].name,
1601 video_codecs_[0].width,
1602 video_codecs_[0].height,
1603 video_codecs_[0].framerate,
1604 0);
1605 if (!RebuildCodecList(max_codec)) {
1606 LOG(LS_ERROR) << "Failed to initialize list of supported codec types";
1607 }
1608}
1609
1610// WebRtcVideoMediaChannel
1611
1612WebRtcVideoMediaChannel::WebRtcVideoMediaChannel(
1613 WebRtcVideoEngine* engine,
1614 VoiceMediaChannel* channel)
1615 : engine_(engine),
1616 voice_channel_(channel),
1617 vie_channel_(-1),
1618 nack_enabled_(true),
1619 remb_enabled_(false),
1620 render_started_(false),
1621 first_receive_ssrc_(0),
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001622 num_unsignalled_recv_channels_(0),
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00001623 send_rtx_type_(-1),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001624 send_red_type_(-1),
1625 send_fec_type_(-1),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001626 sending_(false),
1627 ratio_w_(0),
1628 ratio_h_(0) {
1629 engine->RegisterChannel(this);
1630}
1631
1632bool WebRtcVideoMediaChannel::Init() {
1633 const uint32 ssrc_key = 0;
1634 return CreateChannel(ssrc_key, MD_SENDRECV, &vie_channel_);
1635}
1636
1637WebRtcVideoMediaChannel::~WebRtcVideoMediaChannel() {
1638 const bool send = false;
1639 SetSend(send);
1640 const bool render = false;
1641 SetRender(render);
1642
1643 while (!send_channels_.empty()) {
1644 if (!DeleteSendChannel(send_channels_.begin()->first)) {
1645 LOG(LS_ERROR) << "Unable to delete channel with ssrc key "
1646 << send_channels_.begin()->first;
1647 ASSERT(false);
1648 break;
1649 }
1650 }
1651
1652 // Remove all receive streams and the default channel.
1653 while (!recv_channels_.empty()) {
henrike@webrtc.org6e3dbc22014-03-25 17:09:47 +00001654 RemoveRecvStreamInternal(recv_channels_.begin()->first);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001655 }
1656
1657 // Unregister the channel from the engine.
1658 engine()->UnregisterChannel(this);
1659 if (worker_thread()) {
1660 worker_thread()->Clear(this);
1661 }
1662}
1663
1664bool WebRtcVideoMediaChannel::SetRecvCodecs(
1665 const std::vector<VideoCodec>& codecs) {
1666 receive_codecs_.clear();
buildbot@webrtc.orgdd4742a2014-05-07 14:50:35 +00001667 associated_payload_types_.clear();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001668 for (std::vector<VideoCodec>::const_iterator iter = codecs.begin();
1669 iter != codecs.end(); ++iter) {
1670 if (engine()->FindCodec(*iter)) {
1671 webrtc::VideoCodec wcodec;
1672 if (engine()->ConvertFromCricketVideoCodec(*iter, &wcodec)) {
1673 receive_codecs_.push_back(wcodec);
buildbot@webrtc.orgdd4742a2014-05-07 14:50:35 +00001674 int apt;
1675 if (iter->GetParam(cricket::kCodecParamAssociatedPayloadType, &apt)) {
1676 associated_payload_types_[wcodec.plType] = apt;
1677 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001678 }
1679 } else {
1680 LOG(LS_INFO) << "Unknown codec " << iter->name;
1681 return false;
1682 }
1683 }
1684
1685 for (RecvChannelMap::iterator it = recv_channels_.begin();
1686 it != recv_channels_.end(); ++it) {
1687 if (!SetReceiveCodecs(it->second))
1688 return false;
1689 }
1690 return true;
1691}
1692
1693bool WebRtcVideoMediaChannel::SetSendCodecs(
1694 const std::vector<VideoCodec>& codecs) {
1695 // Match with local video codec list.
1696 std::vector<webrtc::VideoCodec> send_codecs;
1697 VideoCodec checked_codec;
1698 VideoCodec current; // defaults to 0x0
1699 if (sending_) {
1700 ConvertToCricketVideoCodec(*send_codec_, &current);
1701 }
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00001702 std::map<int, int> primary_rtx_pt_mapping;
wu@webrtc.org05e7b442014-04-01 17:44:24 +00001703 bool nack_enabled = nack_enabled_;
1704 bool remb_enabled = remb_enabled_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001705 for (std::vector<VideoCodec>::const_iterator iter = codecs.begin();
1706 iter != codecs.end(); ++iter) {
1707 if (_stricmp(iter->name.c_str(), kRedPayloadName) == 0) {
1708 send_red_type_ = iter->id;
1709 } else if (_stricmp(iter->name.c_str(), kFecPayloadName) == 0) {
1710 send_fec_type_ = iter->id;
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00001711 } else if (_stricmp(iter->name.c_str(), kRtxCodecName) == 0) {
1712 int rtx_type = iter->id;
1713 int rtx_primary_type = -1;
1714 if (iter->GetParam(kCodecParamAssociatedPayloadType, &rtx_primary_type)) {
1715 primary_rtx_pt_mapping[rtx_primary_type] = rtx_type;
1716 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001717 } else if (engine()->CanSendCodec(*iter, current, &checked_codec)) {
1718 webrtc::VideoCodec wcodec;
1719 if (engine()->ConvertFromCricketVideoCodec(checked_codec, &wcodec)) {
1720 if (send_codecs.empty()) {
wu@webrtc.org05e7b442014-04-01 17:44:24 +00001721 nack_enabled = IsNackEnabled(checked_codec);
1722 remb_enabled = IsRembEnabled(checked_codec);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001723 }
1724 send_codecs.push_back(wcodec);
1725 }
1726 } else {
1727 LOG(LS_WARNING) << "Unknown codec " << iter->name;
1728 }
1729 }
1730
1731 // Fail if we don't have a match.
1732 if (send_codecs.empty()) {
1733 LOG(LS_WARNING) << "No matching codecs available";
1734 return false;
1735 }
1736
1737 // Recv protection.
wu@webrtc.org05e7b442014-04-01 17:44:24 +00001738 // Do not update if the status is same as previously configured.
1739 if (nack_enabled_ != nack_enabled) {
1740 for (RecvChannelMap::iterator it = recv_channels_.begin();
1741 it != recv_channels_.end(); ++it) {
1742 int channel_id = it->second->channel_id();
1743 if (!SetNackFec(channel_id, send_red_type_, send_fec_type_,
1744 nack_enabled)) {
1745 return false;
1746 }
1747 if (engine_->vie()->rtp()->SetRembStatus(channel_id,
1748 kNotSending,
1749 remb_enabled_) != 0) {
1750 LOG_RTCERR3(SetRembStatus, channel_id, kNotSending, remb_enabled_);
1751 return false;
1752 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001753 }
wu@webrtc.org05e7b442014-04-01 17:44:24 +00001754 nack_enabled_ = nack_enabled;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001755 }
1756
1757 // Send settings.
wu@webrtc.org05e7b442014-04-01 17:44:24 +00001758 // Do not update if the status is same as previously configured.
1759 if (remb_enabled_ != remb_enabled) {
1760 for (SendChannelMap::iterator iter = send_channels_.begin();
1761 iter != send_channels_.end(); ++iter) {
1762 int channel_id = iter->second->channel_id();
1763 if (!SetNackFec(channel_id, send_red_type_, send_fec_type_,
1764 nack_enabled_)) {
1765 return false;
1766 }
1767 if (engine_->vie()->rtp()->SetRembStatus(channel_id,
1768 remb_enabled,
1769 remb_enabled) != 0) {
1770 LOG_RTCERR3(SetRembStatus, channel_id, remb_enabled, remb_enabled);
1771 return false;
1772 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001773 }
wu@webrtc.org05e7b442014-04-01 17:44:24 +00001774 remb_enabled_ = remb_enabled;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001775 }
1776
1777 // Select the first matched codec.
1778 webrtc::VideoCodec& codec(send_codecs[0]);
1779
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00001780 // Set RTX payload type if primary now active. This value will be used in
1781 // SetSendCodec.
1782 std::map<int, int>::const_iterator rtx_it =
1783 primary_rtx_pt_mapping.find(static_cast<int>(codec.plType));
1784 if (rtx_it != primary_rtx_pt_mapping.end()) {
1785 send_rtx_type_ = rtx_it->second;
1786 }
1787
buildbot@webrtc.orgd1ae89f2014-05-08 19:19:26 +00001788 if (BitrateIsSet(codec.minBitrate) && BitrateIsSet(codec.maxBitrate) &&
1789 codec.minBitrate > codec.maxBitrate) {
1790 // TODO(pthatcher): This behavior contradicts other behavior in
1791 // this file which will cause min > max to push the min down to
1792 // the max. There are unit tests for both behaviors. We should
1793 // pick one and do that.
1794 LOG(LS_INFO) << "Rejecting codec with min bitrate ("
1795 << codec.minBitrate << ") larger than max ("
1796 << codec.maxBitrate << "). ";
1797 return false;
1798 }
1799
1800 if (!SetSendCodec(codec)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001801 return false;
1802 }
1803
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001804 LogSendCodecChange("SetSendCodecs()");
1805
1806 return true;
1807}
1808
1809bool WebRtcVideoMediaChannel::GetSendCodec(VideoCodec* send_codec) {
1810 if (!send_codec_) {
1811 return false;
1812 }
1813 ConvertToCricketVideoCodec(*send_codec_, send_codec);
1814 return true;
1815}
1816
1817bool WebRtcVideoMediaChannel::SetSendStreamFormat(uint32 ssrc,
1818 const VideoFormat& format) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001819 WebRtcVideoChannelSendInfo* send_channel = GetSendChannel(ssrc);
1820 if (!send_channel) {
1821 LOG(LS_ERROR) << "The specified ssrc " << ssrc << " is not in use.";
1822 return false;
1823 }
1824 send_channel->set_video_format(format);
1825 return true;
1826}
1827
1828bool WebRtcVideoMediaChannel::SetRender(bool render) {
1829 if (render == render_started_) {
1830 return true; // no action required
1831 }
1832
1833 bool ret = true;
1834 for (RecvChannelMap::iterator it = recv_channels_.begin();
1835 it != recv_channels_.end(); ++it) {
1836 if (render) {
1837 if (engine()->vie()->render()->StartRender(
1838 it->second->channel_id()) != 0) {
1839 LOG_RTCERR1(StartRender, it->second->channel_id());
1840 ret = false;
1841 }
1842 } else {
1843 if (engine()->vie()->render()->StopRender(
1844 it->second->channel_id()) != 0) {
1845 LOG_RTCERR1(StopRender, it->second->channel_id());
1846 ret = false;
1847 }
1848 }
1849 }
1850 if (ret) {
1851 render_started_ = render;
1852 }
1853
1854 return ret;
1855}
1856
1857bool WebRtcVideoMediaChannel::SetSend(bool send) {
1858 if (!HasReadySendChannels() && send) {
1859 LOG(LS_ERROR) << "No stream added";
1860 return false;
1861 }
1862 if (send == sending()) {
1863 return true; // No action required.
1864 }
1865
1866 if (send) {
1867 // We've been asked to start sending.
1868 // SetSendCodecs must have been called already.
1869 if (!send_codec_) {
1870 return false;
1871 }
1872 // Start send now.
1873 if (!StartSend()) {
1874 return false;
1875 }
1876 } else {
1877 // We've been asked to stop sending.
1878 if (!StopSend()) {
1879 return false;
1880 }
1881 }
1882 sending_ = send;
1883
1884 return true;
1885}
1886
1887bool WebRtcVideoMediaChannel::AddSendStream(const StreamParams& sp) {
henrike@webrtc.org6e3dbc22014-03-25 17:09:47 +00001888 if (sp.first_ssrc() == 0) {
1889 LOG(LS_ERROR) << "AddSendStream with 0 ssrc is not supported.";
1890 return false;
1891 }
1892
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001893 LOG(LS_INFO) << "AddSendStream " << sp.ToString();
1894
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00001895 if (!IsOneSsrcStream(sp) && !IsSimulcastStream(sp)) {
1896 LOG(LS_ERROR) << "AddSendStream: bad local stream parameters";
1897 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001898 }
1899
1900 uint32 ssrc_key;
1901 if (!CreateSendChannelKey(sp.first_ssrc(), &ssrc_key)) {
1902 LOG(LS_ERROR) << "Trying to register duplicate ssrc: " << sp.first_ssrc();
1903 return false;
1904 }
1905 // If the default channel is already used for sending create a new channel
1906 // otherwise use the default channel for sending.
1907 int channel_id = -1;
1908 if (send_channels_[0]->stream_params() == NULL) {
1909 channel_id = vie_channel_;
1910 } else {
1911 if (!CreateChannel(ssrc_key, MD_SEND, &channel_id)) {
1912 LOG(LS_ERROR) << "AddSendStream: unable to create channel";
1913 return false;
1914 }
1915 }
1916 WebRtcVideoChannelSendInfo* send_channel = send_channels_[ssrc_key];
1917 // Set the send (local) SSRC.
1918 // If there are multiple send SSRCs, we can only set the first one here, and
1919 // the rest of the SSRC(s) need to be set after SetSendCodec has been called
1920 // (with a codec requires multiple SSRC(s)).
1921 if (engine()->vie()->rtp()->SetLocalSSRC(channel_id,
1922 sp.first_ssrc()) != 0) {
1923 LOG_RTCERR2(SetLocalSSRC, channel_id, sp.first_ssrc());
1924 return false;
1925 }
1926
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00001927 // Set the corresponding RTX SSRC.
1928 if (!SetLocalRtxSsrc(channel_id, sp, sp.first_ssrc(), 0)) {
1929 return false;
1930 }
1931
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001932 // Set RTCP CName.
1933 if (engine()->vie()->rtp()->SetRTCPCName(channel_id,
1934 sp.cname.c_str()) != 0) {
1935 LOG_RTCERR2(SetRTCPCName, channel_id, sp.cname.c_str());
1936 return false;
1937 }
1938
1939 // At this point the channel's local SSRC has been updated. If the channel is
1940 // the default channel make sure that all the receive channels are updated as
1941 // well. Receive channels have to have the same SSRC as the default channel in
1942 // order to send receiver reports with this SSRC.
1943 if (IsDefaultChannel(channel_id)) {
1944 for (RecvChannelMap::const_iterator it = recv_channels_.begin();
1945 it != recv_channels_.end(); ++it) {
1946 WebRtcVideoChannelRecvInfo* info = it->second;
1947 int channel_id = info->channel_id();
1948 if (engine()->vie()->rtp()->SetLocalSSRC(channel_id,
1949 sp.first_ssrc()) != 0) {
1950 LOG_RTCERR1(SetLocalSSRC, it->first);
1951 return false;
1952 }
1953 }
1954 }
1955
1956 send_channel->set_stream_params(sp);
1957
1958 // Reset send codec after stream parameters changed.
1959 if (send_codec_) {
buildbot@webrtc.orgd1ae89f2014-05-08 19:19:26 +00001960 if (!SetSendCodec(send_channel, *send_codec_)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001961 return false;
1962 }
1963 LogSendCodecChange("SetSendStreamFormat()");
1964 }
1965
1966 if (sending_) {
1967 return StartSend(send_channel);
1968 }
1969 return true;
1970}
1971
1972bool WebRtcVideoMediaChannel::RemoveSendStream(uint32 ssrc) {
henrike@webrtc.org6e3dbc22014-03-25 17:09:47 +00001973 if (ssrc == 0) {
1974 LOG(LS_ERROR) << "RemoveSendStream with 0 ssrc is not supported.";
1975 return false;
1976 }
1977
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001978 uint32 ssrc_key;
1979 if (!GetSendChannelKey(ssrc, &ssrc_key)) {
1980 LOG(LS_WARNING) << "Try to remove stream with ssrc " << ssrc
1981 << " which doesn't exist.";
1982 return false;
1983 }
1984 WebRtcVideoChannelSendInfo* send_channel = send_channels_[ssrc_key];
1985 int channel_id = send_channel->channel_id();
1986 if (IsDefaultChannel(channel_id) && (send_channel->stream_params() == NULL)) {
1987 // Default channel will still exist. However, if stream_params() is NULL
1988 // there is no stream to remove.
1989 return false;
1990 }
1991 if (sending_) {
1992 StopSend(send_channel);
1993 }
1994
1995 const WebRtcVideoChannelSendInfo::EncoderMap& encoder_map =
1996 send_channel->registered_encoders();
1997 for (WebRtcVideoChannelSendInfo::EncoderMap::const_iterator it =
1998 encoder_map.begin(); it != encoder_map.end(); ++it) {
1999 if (engine()->vie()->ext_codec()->DeRegisterExternalSendCodec(
2000 channel_id, it->first) != 0) {
2001 LOG_RTCERR1(DeregisterEncoderObserver, channel_id);
2002 }
2003 engine()->DestroyExternalEncoder(it->second);
2004 }
2005 send_channel->ClearRegisteredEncoders();
2006
2007 // The receive channels depend on the default channel, recycle it instead.
2008 if (IsDefaultChannel(channel_id)) {
2009 SetCapturer(GetDefaultChannelSsrc(), NULL);
2010 send_channel->ClearStreamParams();
2011 } else {
2012 return DeleteSendChannel(ssrc_key);
2013 }
2014 return true;
2015}
2016
2017bool WebRtcVideoMediaChannel::AddRecvStream(const StreamParams& sp) {
henrike@webrtc.org6e3dbc22014-03-25 17:09:47 +00002018 if (sp.first_ssrc() == 0) {
2019 LOG(LS_ERROR) << "AddRecvStream with 0 ssrc is not supported.";
2020 return false;
2021 }
2022
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002023 // TODO(zhurunz) Remove this once BWE works properly across different send
2024 // and receive channels.
2025 // Reuse default channel for recv stream in 1:1 call.
2026 if (!InConferenceMode() && first_receive_ssrc_ == 0) {
2027 LOG(LS_INFO) << "Recv stream " << sp.first_ssrc()
2028 << " reuse default channel #"
2029 << vie_channel_;
2030 first_receive_ssrc_ = sp.first_ssrc();
2031 if (render_started_) {
2032 if (engine()->vie()->render()->StartRender(vie_channel_) !=0) {
2033 LOG_RTCERR1(StartRender, vie_channel_);
2034 }
2035 }
2036 return true;
2037 }
2038
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002039 int channel_id = -1;
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00002040 RecvChannelMap::iterator channel_iterator =
2041 recv_channels_.find(sp.first_ssrc());
2042 if (channel_iterator == recv_channels_.end() &&
2043 first_receive_ssrc_ != sp.first_ssrc()) {
2044 // TODO(perkj): Implement recv media from multiple media SSRCs per stream.
2045 // NOTE: We have two SSRCs per stream when RTX is enabled.
2046 if (!IsOneSsrcStream(sp)) {
2047 LOG(LS_ERROR) << "WebRtcVideoMediaChannel supports one primary SSRC per"
2048 << " stream and one FID SSRC per primary SSRC.";
2049 return false;
2050 }
2051
2052 // Create a new channel for receiving video data.
2053 // In order to get the bandwidth estimation work fine for
2054 // receive only channels, we connect all receiving channels
2055 // to our master send channel.
2056 if (!CreateChannel(sp.first_ssrc(), MD_RECV, &channel_id)) {
2057 return false;
2058 }
2059 } else {
2060 // Already exists.
2061 if (first_receive_ssrc_ == sp.first_ssrc()) {
2062 return false;
2063 }
2064 // Early receive added channel.
2065 channel_id = (*channel_iterator).second->channel_id();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002066 }
buildbot@webrtc.orgdd4742a2014-05-07 14:50:35 +00002067 channel_iterator = recv_channels_.find(sp.first_ssrc());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002068
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002069 // Set the corresponding RTX SSRC.
2070 uint32 rtx_ssrc;
2071 bool has_rtx = sp.GetFidSsrc(sp.first_ssrc(), &rtx_ssrc);
buildbot@webrtc.orgdd4742a2014-05-07 14:50:35 +00002072 if (has_rtx) {
2073 LOG(LS_INFO) << "Setting rtx ssrc " << rtx_ssrc << " for stream "
2074 << sp.first_ssrc();
2075 if (engine()->vie()->rtp()->SetRemoteSSRCType(
2076 channel_id, webrtc::kViEStreamTypeRtx, rtx_ssrc) != 0) {
2077 LOG_RTCERR3(SetRemoteSSRCType, channel_id, webrtc::kViEStreamTypeRtx,
2078 rtx_ssrc);
2079 return false;
2080 }
2081 rtx_to_primary_ssrc_[rtx_ssrc] = sp.first_ssrc();
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002082 }
2083
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002084 // Get the default renderer.
2085 VideoRenderer* default_renderer = NULL;
2086 if (InConferenceMode()) {
2087 // The recv_channels_ size start out being 1, so if it is two here this
2088 // is the first receive channel created (vie_channel_ is not used for
2089 // receiving in a conference call). This means that the renderer stored
2090 // inside vie_channel_ should be used for the just created channel.
2091 if (recv_channels_.size() == 2 &&
2092 recv_channels_.find(0) != recv_channels_.end()) {
2093 GetRenderer(0, &default_renderer);
2094 }
2095 }
2096
2097 // The first recv stream reuses the default renderer (if a default renderer
2098 // has been set).
2099 if (default_renderer) {
2100 SetRenderer(sp.first_ssrc(), default_renderer);
2101 }
2102
2103 LOG(LS_INFO) << "New video stream " << sp.first_ssrc()
2104 << " registered to VideoEngine channel #"
2105 << channel_id << " and connected to channel #" << vie_channel_;
2106
2107 return true;
2108}
2109
2110bool WebRtcVideoMediaChannel::RemoveRecvStream(uint32 ssrc) {
henrike@webrtc.org6e3dbc22014-03-25 17:09:47 +00002111 if (ssrc == 0) {
2112 LOG(LS_ERROR) << "RemoveRecvStream with 0 ssrc is not supported.";
2113 return false;
2114 }
2115 return RemoveRecvStreamInternal(ssrc);
2116}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002117
henrike@webrtc.org6e3dbc22014-03-25 17:09:47 +00002118bool WebRtcVideoMediaChannel::RemoveRecvStreamInternal(uint32 ssrc) {
2119 RecvChannelMap::iterator it = recv_channels_.find(ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002120 if (it == recv_channels_.end()) {
2121 // TODO(perkj): Remove this once BWE works properly across different send
2122 // and receive channels.
2123 // The default channel is reused for recv stream in 1:1 call.
2124 if (first_receive_ssrc_ == ssrc) {
2125 first_receive_ssrc_ = 0;
2126 // Need to stop the renderer and remove it since the render window can be
2127 // deleted after this.
2128 if (render_started_) {
2129 if (engine()->vie()->render()->StopRender(vie_channel_) !=0) {
2130 LOG_RTCERR1(StopRender, it->second->channel_id());
2131 }
2132 }
2133 recv_channels_[0]->SetRenderer(NULL);
2134 return true;
2135 }
2136 return false;
2137 }
2138 WebRtcVideoChannelRecvInfo* info = it->second;
buildbot@webrtc.orgdd4742a2014-05-07 14:50:35 +00002139
2140 // Remove any RTX SSRC mappings to this stream.
2141 SsrcMap::iterator rtx_it = rtx_to_primary_ssrc_.begin();
2142 while (rtx_it != rtx_to_primary_ssrc_.end()) {
2143 if (rtx_it->second == ssrc) {
2144 rtx_to_primary_ssrc_.erase(rtx_it++);
2145 } else {
2146 ++rtx_it;
2147 }
2148 }
2149
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002150 int channel_id = info->channel_id();
2151 if (engine()->vie()->render()->RemoveRenderer(channel_id) != 0) {
2152 LOG_RTCERR1(RemoveRenderer, channel_id);
2153 }
2154
2155 if (engine()->vie()->network()->DeregisterSendTransport(channel_id) !=0) {
2156 LOG_RTCERR1(DeRegisterSendTransport, channel_id);
2157 }
2158
2159 if (engine()->vie()->codec()->DeregisterDecoderObserver(
2160 channel_id) != 0) {
2161 LOG_RTCERR1(DeregisterDecoderObserver, channel_id);
2162 }
2163
2164 const WebRtcVideoChannelRecvInfo::DecoderMap& decoder_map =
2165 info->registered_decoders();
2166 for (WebRtcVideoChannelRecvInfo::DecoderMap::const_iterator it =
2167 decoder_map.begin(); it != decoder_map.end(); ++it) {
2168 if (engine()->vie()->ext_codec()->DeRegisterExternalReceiveCodec(
2169 channel_id, it->first) != 0) {
2170 LOG_RTCERR1(DeregisterDecoderObserver, channel_id);
2171 }
2172 engine()->DestroyExternalDecoder(it->second);
2173 }
2174 info->ClearRegisteredDecoders();
2175
2176 LOG(LS_INFO) << "Removing video stream " << ssrc
2177 << " with VideoEngine channel #"
2178 << channel_id;
wu@webrtc.org9caf2762013-12-11 18:25:07 +00002179 bool ret = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002180 if (engine()->vie()->base()->DeleteChannel(channel_id) == -1) {
2181 LOG_RTCERR1(DeleteChannel, channel_id);
wu@webrtc.org9caf2762013-12-11 18:25:07 +00002182 ret = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002183 }
2184 // Delete the WebRtcVideoChannelRecvInfo pointed to by it->second.
2185 delete info;
2186 recv_channels_.erase(it);
wu@webrtc.org9caf2762013-12-11 18:25:07 +00002187 return ret;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002188}
2189
2190bool WebRtcVideoMediaChannel::StartSend() {
2191 bool success = true;
2192 for (SendChannelMap::iterator iter = send_channels_.begin();
2193 iter != send_channels_.end(); ++iter) {
2194 WebRtcVideoChannelSendInfo* send_channel = iter->second;
2195 if (!StartSend(send_channel)) {
2196 success = false;
2197 }
2198 }
2199 return success;
2200}
2201
2202bool WebRtcVideoMediaChannel::StartSend(
2203 WebRtcVideoChannelSendInfo* send_channel) {
2204 const int channel_id = send_channel->channel_id();
2205 if (engine()->vie()->base()->StartSend(channel_id) != 0) {
2206 LOG_RTCERR1(StartSend, channel_id);
2207 return false;
2208 }
2209
2210 send_channel->set_sending(true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002211 return true;
2212}
2213
2214bool WebRtcVideoMediaChannel::StopSend() {
2215 bool success = true;
2216 for (SendChannelMap::iterator iter = send_channels_.begin();
2217 iter != send_channels_.end(); ++iter) {
2218 WebRtcVideoChannelSendInfo* send_channel = iter->second;
2219 if (!StopSend(send_channel)) {
2220 success = false;
2221 }
2222 }
2223 return success;
2224}
2225
2226bool WebRtcVideoMediaChannel::StopSend(
2227 WebRtcVideoChannelSendInfo* send_channel) {
2228 const int channel_id = send_channel->channel_id();
2229 if (engine()->vie()->base()->StopSend(channel_id) != 0) {
2230 LOG_RTCERR1(StopSend, channel_id);
2231 return false;
2232 }
2233 send_channel->set_sending(false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002234 return true;
2235}
2236
2237bool WebRtcVideoMediaChannel::SendIntraFrame() {
2238 bool success = true;
2239 for (SendChannelMap::iterator iter = send_channels_.begin();
2240 iter != send_channels_.end();
2241 ++iter) {
2242 WebRtcVideoChannelSendInfo* send_channel = iter->second;
2243 const int channel_id = send_channel->channel_id();
2244 if (engine()->vie()->codec()->SendKeyFrame(channel_id) != 0) {
2245 LOG_RTCERR1(SendKeyFrame, channel_id);
2246 success = false;
2247 }
2248 }
2249 return success;
2250}
2251
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002252bool WebRtcVideoMediaChannel::HasReadySendChannels() {
2253 return !send_channels_.empty() &&
2254 ((send_channels_.size() > 1) ||
2255 (send_channels_[0]->stream_params() != NULL));
2256}
2257
2258bool WebRtcVideoMediaChannel::GetSendChannelKey(uint32 local_ssrc,
2259 uint32* key) {
2260 *key = 0;
2261 // If a send channel is not ready to send it will not have local_ssrc
2262 // registered to it.
2263 if (!HasReadySendChannels()) {
2264 return false;
2265 }
2266 // The default channel is stored with key 0. The key therefore does not match
2267 // the SSRC associated with the default channel. Check if the SSRC provided
2268 // corresponds to the default channel's SSRC.
2269 if (local_ssrc == GetDefaultChannelSsrc()) {
2270 return true;
2271 }
2272 if (send_channels_.find(local_ssrc) == send_channels_.end()) {
2273 for (SendChannelMap::iterator iter = send_channels_.begin();
2274 iter != send_channels_.end(); ++iter) {
2275 WebRtcVideoChannelSendInfo* send_channel = iter->second;
2276 if (send_channel->has_ssrc(local_ssrc)) {
2277 *key = iter->first;
2278 return true;
2279 }
2280 }
2281 return false;
2282 }
2283 // The key was found in the above std::map::find call. This means that the
2284 // ssrc is the key.
2285 *key = local_ssrc;
2286 return true;
2287}
2288
2289WebRtcVideoChannelSendInfo* WebRtcVideoMediaChannel::GetSendChannel(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002290 uint32 local_ssrc) {
2291 uint32 key;
2292 if (!GetSendChannelKey(local_ssrc, &key)) {
2293 return NULL;
2294 }
2295 return send_channels_[key];
2296}
2297
2298bool WebRtcVideoMediaChannel::CreateSendChannelKey(uint32 local_ssrc,
2299 uint32* key) {
2300 if (GetSendChannelKey(local_ssrc, key)) {
2301 // If there is a key corresponding to |local_ssrc|, the SSRC is already in
2302 // use. SSRCs need to be unique in a session and at this point a duplicate
2303 // SSRC has been detected.
2304 return false;
2305 }
2306 if (send_channels_[0]->stream_params() == NULL) {
2307 // key should be 0 here as the default channel should be re-used whenever it
2308 // is not used.
2309 *key = 0;
2310 return true;
2311 }
2312 // SSRC is currently not in use and the default channel is already in use. Use
2313 // the SSRC as key since it is supposed to be unique in a session.
2314 *key = local_ssrc;
2315 return true;
2316}
2317
wu@webrtc.org24301a62013-12-13 19:17:43 +00002318int WebRtcVideoMediaChannel::GetSendChannelNum(VideoCapturer* capturer) {
2319 int num = 0;
2320 for (SendChannelMap::iterator iter = send_channels_.begin();
2321 iter != send_channels_.end(); ++iter) {
2322 WebRtcVideoChannelSendInfo* send_channel = iter->second;
2323 if (send_channel->video_capturer() == capturer) {
2324 ++num;
2325 }
2326 }
2327 return num;
2328}
2329
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002330uint32 WebRtcVideoMediaChannel::GetDefaultChannelSsrc() {
2331 WebRtcVideoChannelSendInfo* send_channel = send_channels_[0];
2332 const StreamParams* sp = send_channel->stream_params();
2333 if (sp == NULL) {
2334 // This happens if no send stream is currently registered.
2335 return 0;
2336 }
2337 return sp->first_ssrc();
2338}
2339
2340bool WebRtcVideoMediaChannel::DeleteSendChannel(uint32 ssrc_key) {
2341 if (send_channels_.find(ssrc_key) == send_channels_.end()) {
2342 return false;
2343 }
2344 WebRtcVideoChannelSendInfo* send_channel = send_channels_[ssrc_key];
wu@webrtc.org24301a62013-12-13 19:17:43 +00002345 MaybeDisconnectCapturer(send_channel->video_capturer());
henrike@webrtc.orgc7bec842014-03-12 19:53:43 +00002346 send_channel->set_video_capturer(NULL, engine()->vie());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002347
2348 int channel_id = send_channel->channel_id();
2349 int capture_id = send_channel->capture_id();
2350 if (engine()->vie()->codec()->DeregisterEncoderObserver(
2351 channel_id) != 0) {
2352 LOG_RTCERR1(DeregisterEncoderObserver, channel_id);
2353 }
2354
2355 // Destroy the external capture interface.
2356 if (engine()->vie()->capture()->DisconnectCaptureDevice(
2357 channel_id) != 0) {
2358 LOG_RTCERR1(DisconnectCaptureDevice, channel_id);
2359 }
2360 if (engine()->vie()->capture()->ReleaseCaptureDevice(
2361 capture_id) != 0) {
2362 LOG_RTCERR1(ReleaseCaptureDevice, capture_id);
2363 }
2364
2365 // The default channel is stored in both |send_channels_| and
2366 // |recv_channels_|. To make sure it is only deleted once from vie let the
2367 // delete call happen when tearing down |recv_channels_| and not here.
2368 if (!IsDefaultChannel(channel_id)) {
2369 engine_->vie()->base()->DeleteChannel(channel_id);
2370 }
2371 delete send_channel;
2372 send_channels_.erase(ssrc_key);
2373 return true;
2374}
2375
2376bool WebRtcVideoMediaChannel::RemoveCapturer(uint32 ssrc) {
2377 WebRtcVideoChannelSendInfo* send_channel = GetSendChannel(ssrc);
2378 if (!send_channel) {
2379 return false;
2380 }
2381 VideoCapturer* capturer = send_channel->video_capturer();
2382 if (capturer == NULL) {
2383 return false;
2384 }
wu@webrtc.org24301a62013-12-13 19:17:43 +00002385 MaybeDisconnectCapturer(capturer);
henrike@webrtc.orgc7bec842014-03-12 19:53:43 +00002386 send_channel->set_video_capturer(NULL, engine()->vie());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002387 const int64 timestamp = send_channel->local_stream_info()->time_stamp();
2388 if (send_codec_) {
2389 QueueBlackFrame(ssrc, timestamp, send_codec_->maxFramerate);
2390 }
2391 return true;
2392}
2393
2394bool WebRtcVideoMediaChannel::SetRenderer(uint32 ssrc,
2395 VideoRenderer* renderer) {
2396 if (recv_channels_.find(ssrc) == recv_channels_.end()) {
2397 // TODO(perkj): Remove this once BWE works properly across different send
2398 // and receive channels.
2399 // The default channel is reused for recv stream in 1:1 call.
2400 if (first_receive_ssrc_ == ssrc &&
2401 recv_channels_.find(0) != recv_channels_.end()) {
2402 LOG(LS_INFO) << "SetRenderer " << ssrc
2403 << " reuse default channel #"
2404 << vie_channel_;
2405 recv_channels_[0]->SetRenderer(renderer);
2406 return true;
2407 }
2408 return false;
2409 }
2410
2411 recv_channels_[ssrc]->SetRenderer(renderer);
2412 return true;
2413}
2414
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00002415bool WebRtcVideoMediaChannel::GetStats(const StatsOptions& options,
2416 VideoMediaInfo* info) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002417 // Get sender statistics and build VideoSenderInfo.
2418 unsigned int total_bitrate_sent = 0;
2419 unsigned int video_bitrate_sent = 0;
2420 unsigned int fec_bitrate_sent = 0;
2421 unsigned int nack_bitrate_sent = 0;
2422 unsigned int estimated_send_bandwidth = 0;
2423 unsigned int target_enc_bitrate = 0;
2424 if (send_codec_) {
2425 for (SendChannelMap::const_iterator iter = send_channels_.begin();
2426 iter != send_channels_.end(); ++iter) {
2427 WebRtcVideoChannelSendInfo* send_channel = iter->second;
2428 const int channel_id = send_channel->channel_id();
2429 VideoSenderInfo sinfo;
2430 const StreamParams* send_params = send_channel->stream_params();
2431 if (send_params == NULL) {
2432 // This should only happen if the default vie channel is not in use.
2433 // This can happen if no streams have ever been added or the stream
2434 // corresponding to the default channel has been removed. Note that
2435 // there may be non-default vie channels in use when this happen so
2436 // asserting send_channels_.size() == 1 is not correct and neither is
2437 // breaking out of the loop.
2438 ASSERT(channel_id == vie_channel_);
2439 continue;
2440 }
2441 unsigned int bytes_sent, packets_sent, bytes_recv, packets_recv;
2442 if (engine_->vie()->rtp()->GetRTPStatistics(channel_id, bytes_sent,
2443 packets_sent, bytes_recv,
2444 packets_recv) != 0) {
2445 LOG_RTCERR1(GetRTPStatistics, vie_channel_);
2446 continue;
2447 }
2448 WebRtcLocalStreamInfo* channel_stream_info =
2449 send_channel->local_stream_info();
2450
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002451 for (size_t i = 0; i < send_params->ssrcs.size(); ++i) {
2452 sinfo.add_ssrc(send_params->ssrcs[i]);
2453 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002454 sinfo.codec_name = send_codec_->plName;
2455 sinfo.bytes_sent = bytes_sent;
2456 sinfo.packets_sent = packets_sent;
2457 sinfo.packets_cached = -1;
2458 sinfo.packets_lost = -1;
2459 sinfo.fraction_lost = -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002460 sinfo.rtt_ms = -1;
wu@webrtc.org987f2c92014-03-28 16:22:19 +00002461
2462 VideoCapturer* video_capturer = send_channel->video_capturer();
2463 if (video_capturer) {
buildbot@webrtc.org0b53bd22014-05-06 17:12:36 +00002464 VideoFormat last_captured_frame_format;
wu@webrtc.org987f2c92014-03-28 16:22:19 +00002465 video_capturer->GetStats(&sinfo.adapt_frame_drops,
2466 &sinfo.effects_frame_drops,
buildbot@webrtc.org0b53bd22014-05-06 17:12:36 +00002467 &sinfo.capturer_frame_time,
2468 &last_captured_frame_format);
2469 sinfo.input_frame_width = last_captured_frame_format.width;
2470 sinfo.input_frame_height = last_captured_frame_format.height;
2471 } else {
2472 sinfo.input_frame_width = 0;
2473 sinfo.input_frame_height = 0;
wu@webrtc.org987f2c92014-03-28 16:22:19 +00002474 }
2475
2476 webrtc::VideoCodec vie_codec;
wu@webrtc.org987f2c92014-03-28 16:22:19 +00002477 if (!video_capturer || video_capturer->IsMuted()) {
2478 sinfo.send_frame_width = 0;
2479 sinfo.send_frame_height = 0;
2480 } else if (engine()->vie()->codec()->GetSendCodec(channel_id,
2481 vie_codec) == 0) {
2482 sinfo.send_frame_width = vie_codec.width;
2483 sinfo.send_frame_height = vie_codec.height;
2484 } else {
2485 sinfo.send_frame_width = -1;
2486 sinfo.send_frame_height = -1;
2487 LOG_RTCERR1(GetSendCodec, channel_id);
2488 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002489 sinfo.framerate_input = channel_stream_info->framerate();
2490 sinfo.framerate_sent = send_channel->encoder_observer()->framerate();
2491 sinfo.nominal_bitrate = send_channel->encoder_observer()->bitrate();
buildbot@webrtc.orgd1ae89f2014-05-08 19:19:26 +00002492 if (send_codec_) {
2493 sinfo.preferred_bitrate = GetBitrate(
2494 send_codec_->maxBitrate, kMaxVideoBitrate);
2495 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002496 sinfo.adapt_reason = send_channel->CurrentAdaptReason();
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002497 sinfo.capture_jitter_ms = -1;
2498 sinfo.avg_encode_ms = -1;
wu@webrtc.org9caf2762013-12-11 18:25:07 +00002499 sinfo.encode_usage_percent = -1;
2500 sinfo.capture_queue_delay_ms_per_s = -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002501
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002502 int capture_jitter_ms = 0;
2503 int avg_encode_time_ms = 0;
wu@webrtc.org9caf2762013-12-11 18:25:07 +00002504 int encode_usage_percent = 0;
2505 int capture_queue_delay_ms_per_s = 0;
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002506 if (engine()->vie()->base()->CpuOveruseMeasures(
wu@webrtc.org9caf2762013-12-11 18:25:07 +00002507 channel_id,
2508 &capture_jitter_ms,
2509 &avg_encode_time_ms,
2510 &encode_usage_percent,
2511 &capture_queue_delay_ms_per_s) == 0) {
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002512 sinfo.capture_jitter_ms = capture_jitter_ms;
2513 sinfo.avg_encode_ms = avg_encode_time_ms;
wu@webrtc.org9caf2762013-12-11 18:25:07 +00002514 sinfo.encode_usage_percent = encode_usage_percent;
2515 sinfo.capture_queue_delay_ms_per_s = capture_queue_delay_ms_per_s;
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002516 }
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002517
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00002518 webrtc::RtcpPacketTypeCounter rtcp_sent;
2519 webrtc::RtcpPacketTypeCounter rtcp_received;
2520 if (engine()->vie()->rtp()->GetRtcpPacketTypeCounters(
2521 channel_id, &rtcp_sent, &rtcp_received) == 0) {
2522 sinfo.firs_rcvd = rtcp_received.fir_packets;
2523 sinfo.plis_rcvd = rtcp_received.pli_packets;
2524 sinfo.nacks_rcvd = rtcp_received.nack_packets;
2525 } else {
2526 sinfo.firs_rcvd = -1;
2527 sinfo.plis_rcvd = -1;
2528 sinfo.nacks_rcvd = -1;
2529 LOG_RTCERR1(GetRtcpPacketTypeCounters, channel_id);
2530 }
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00002531
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002532 // Get received RTCP statistics for the sender (reported by the remote
2533 // client in a RTCP packet), if available.
2534 // It's not a fatal error if we can't, since RTCP may not have arrived
2535 // yet.
2536 webrtc::RtcpStatistics outgoing_stream_rtcp_stats;
2537 int outgoing_stream_rtt_ms;
2538
2539 if (engine_->vie()->rtp()->GetSendChannelRtcpStatistics(
2540 channel_id,
2541 outgoing_stream_rtcp_stats,
2542 outgoing_stream_rtt_ms) == 0) {
2543 // Convert Q8 to float.
2544 sinfo.packets_lost = outgoing_stream_rtcp_stats.cumulative_lost;
2545 sinfo.fraction_lost = static_cast<float>(
2546 outgoing_stream_rtcp_stats.fraction_lost) / (1 << 8);
2547 sinfo.rtt_ms = outgoing_stream_rtt_ms;
2548 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002549 info->senders.push_back(sinfo);
2550
2551 unsigned int channel_total_bitrate_sent = 0;
2552 unsigned int channel_video_bitrate_sent = 0;
2553 unsigned int channel_fec_bitrate_sent = 0;
2554 unsigned int channel_nack_bitrate_sent = 0;
2555 if (engine_->vie()->rtp()->GetBandwidthUsage(
2556 channel_id, channel_total_bitrate_sent, channel_video_bitrate_sent,
2557 channel_fec_bitrate_sent, channel_nack_bitrate_sent) == 0) {
2558 total_bitrate_sent += channel_total_bitrate_sent;
2559 video_bitrate_sent += channel_video_bitrate_sent;
2560 fec_bitrate_sent += channel_fec_bitrate_sent;
2561 nack_bitrate_sent += channel_nack_bitrate_sent;
2562 } else {
2563 LOG_RTCERR1(GetBandwidthUsage, channel_id);
2564 }
2565
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002566 unsigned int target_enc_stream_bitrate = 0;
2567 if (engine_->vie()->codec()->GetCodecTargetBitrate(
2568 channel_id, &target_enc_stream_bitrate) == 0) {
2569 target_enc_bitrate += target_enc_stream_bitrate;
2570 } else {
2571 LOG_RTCERR1(GetCodecTargetBitrate, channel_id);
2572 }
2573 }
buildbot@webrtc.orga18b4c92014-05-06 17:48:14 +00002574 if (!send_channels_.empty()) {
2575 // GetEstimatedSendBandwidth returns the estimated bandwidth for all video
2576 // engine channels in a channel group. Any valid channel id will do as it
2577 // is only used to access the right group of channels.
2578 const int channel_id = send_channels_.begin()->second->channel_id();
2579 // Get the send bandwidth available for this MediaChannel.
2580 if (engine_->vie()->rtp()->GetEstimatedSendBandwidth(
2581 channel_id, &estimated_send_bandwidth) != 0) {
2582 LOG_RTCERR1(GetEstimatedSendBandwidth, channel_id);
2583 }
2584 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002585 } else {
2586 LOG(LS_WARNING) << "GetStats: sender information not ready.";
2587 }
2588
2589 // Get the SSRC and stats for each receiver, based on our own calculations.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002590 for (RecvChannelMap::const_iterator it = recv_channels_.begin();
2591 it != recv_channels_.end(); ++it) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002592 WebRtcVideoChannelRecvInfo* channel = it->second;
2593
buildbot@webrtc.orgeaf2bd92014-05-12 23:12:19 +00002594 unsigned int ssrc = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002595 // Get receiver statistics and build VideoReceiverInfo, if we have data.
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +00002596 // Skip the default channel (ssrc == 0).
2597 if (engine_->vie()->rtp()->GetRemoteSSRC(
2598 channel->channel_id(), ssrc) != 0 ||
2599 ssrc == 0)
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002600 continue;
2601
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002602 webrtc::StreamDataCounters sent;
2603 webrtc::StreamDataCounters received;
2604 if (engine_->vie()->rtp()->GetRtpStatistics(channel->channel_id(),
2605 sent, received) != 0) {
2606 LOG_RTCERR1(GetRTPStatistics, channel->channel_id());
2607 return false;
2608 }
2609 VideoReceiverInfo rinfo;
2610 rinfo.add_ssrc(ssrc);
2611 rinfo.bytes_rcvd = received.bytes;
2612 rinfo.packets_rcvd = received.packets;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002613 rinfo.packets_lost = -1;
2614 rinfo.packets_concealed = -1;
2615 rinfo.fraction_lost = -1; // from SentRTCP
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002616 rinfo.frame_width = channel->render_adapter()->width();
2617 rinfo.frame_height = channel->render_adapter()->height();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002618 int fps = channel->render_adapter()->framerate();
2619 rinfo.framerate_decoded = fps;
2620 rinfo.framerate_output = fps;
buildbot@webrtc.org0581f0b2014-05-06 21:36:31 +00002621 rinfo.capture_start_ntp_time_ms =
2622 channel->render_adapter()->capture_start_ntp_time_ms();
wu@webrtc.org97077a32013-10-25 21:18:33 +00002623 channel->decoder_observer()->ExportTo(&rinfo);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002624
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00002625 webrtc::RtcpPacketTypeCounter rtcp_sent;
2626 webrtc::RtcpPacketTypeCounter rtcp_received;
2627 if (engine()->vie()->rtp()->GetRtcpPacketTypeCounters(
2628 channel->channel_id(), &rtcp_sent, &rtcp_received) == 0) {
2629 rinfo.firs_sent = rtcp_sent.fir_packets;
2630 rinfo.plis_sent = rtcp_sent.pli_packets;
2631 rinfo.nacks_sent = rtcp_sent.nack_packets;
2632 } else {
2633 rinfo.firs_sent = -1;
2634 rinfo.plis_sent = -1;
2635 rinfo.nacks_sent = -1;
2636 LOG_RTCERR1(GetRtcpPacketTypeCounters, channel->channel_id());
2637 }
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00002638
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002639 // Get our locally created statistics of the received RTP stream.
2640 webrtc::RtcpStatistics incoming_stream_rtcp_stats;
2641 int incoming_stream_rtt_ms;
2642 if (engine_->vie()->rtp()->GetReceiveChannelRtcpStatistics(
2643 channel->channel_id(),
2644 incoming_stream_rtcp_stats,
2645 incoming_stream_rtt_ms) == 0) {
2646 // Convert Q8 to float.
2647 rinfo.packets_lost = incoming_stream_rtcp_stats.cumulative_lost;
2648 rinfo.fraction_lost = static_cast<float>(
2649 incoming_stream_rtcp_stats.fraction_lost) / (1 << 8);
2650 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002651 info->receivers.push_back(rinfo);
buildbot@webrtc.orga18b4c92014-05-06 17:48:14 +00002652 }
2653 unsigned int estimated_recv_bandwidth = 0;
2654 if (!recv_channels_.empty()) {
2655 // GetEstimatedReceiveBandwidth returns the estimated bandwidth for all
2656 // video engine channels in a channel group. Any valid channel id will do as
2657 // it is only used to access the right group of channels.
2658 const int channel_id = recv_channels_.begin()->second->channel_id();
2659 // Gets the estimated receive bandwidth for the MediaChannel.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002660 if (engine_->vie()->rtp()->GetEstimatedReceiveBandwidth(
buildbot@webrtc.orga18b4c92014-05-06 17:48:14 +00002661 channel_id, &estimated_recv_bandwidth) != 0) {
2662 LOG_RTCERR1(GetEstimatedReceiveBandwidth, channel_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002663 }
2664 }
buildbot@webrtc.orga18b4c92014-05-06 17:48:14 +00002665
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002666 // Build BandwidthEstimationInfo.
2667 // TODO(zhurunz): Add real unittest for this.
2668 BandwidthEstimationInfo bwe;
2669
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00002670 // TODO(jiayl): remove the condition when the necessary changes are available
2671 // outside the dev branch.
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00002672 if (options.include_received_propagation_stats) {
2673 webrtc::ReceiveBandwidthEstimatorStats additional_stats;
2674 // Only call for the default channel because the returned stats are
2675 // collected for all the channels using the same estimator.
2676 if (engine_->vie()->rtp()->GetReceiveBandwidthEstimatorStats(
mallinath@webrtc.org92fdfeb2014-02-17 18:49:41 +00002677 recv_channels_[0]->channel_id(), &additional_stats) == 0) {
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00002678 bwe.total_received_propagation_delta_ms =
2679 additional_stats.total_propagation_time_delta_ms;
2680 bwe.recent_received_propagation_delta_ms.swap(
2681 additional_stats.recent_propagation_time_delta_ms);
2682 bwe.recent_received_packet_group_arrival_time_ms.swap(
2683 additional_stats.recent_arrival_time_ms);
2684 }
2685 }
henrike@webrtc.orgb8395eb2014-02-28 21:57:22 +00002686
2687 engine_->vie()->rtp()->GetPacerQueuingDelayMs(
2688 recv_channels_[0]->channel_id(), &bwe.bucket_delay);
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00002689
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002690 // Calculations done above per send/receive stream.
2691 bwe.actual_enc_bitrate = video_bitrate_sent;
2692 bwe.transmit_bitrate = total_bitrate_sent;
2693 bwe.retransmit_bitrate = nack_bitrate_sent;
2694 bwe.available_send_bandwidth = estimated_send_bandwidth;
2695 bwe.available_recv_bandwidth = estimated_recv_bandwidth;
2696 bwe.target_enc_bitrate = target_enc_bitrate;
2697
2698 info->bw_estimations.push_back(bwe);
2699
2700 return true;
2701}
2702
2703bool WebRtcVideoMediaChannel::SetCapturer(uint32 ssrc,
2704 VideoCapturer* capturer) {
2705 ASSERT(ssrc != 0);
2706 if (!capturer) {
2707 return RemoveCapturer(ssrc);
2708 }
2709 WebRtcVideoChannelSendInfo* send_channel = GetSendChannel(ssrc);
2710 if (!send_channel) {
2711 return false;
2712 }
2713 VideoCapturer* old_capturer = send_channel->video_capturer();
wu@webrtc.org24301a62013-12-13 19:17:43 +00002714 MaybeDisconnectCapturer(old_capturer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002715
henrike@webrtc.orgc7bec842014-03-12 19:53:43 +00002716 send_channel->set_video_capturer(capturer, engine()->vie());
wu@webrtc.orga8910d22014-01-23 22:12:45 +00002717 MaybeConnectCapturer(capturer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002718 if (!capturer->IsScreencast() && ratio_w_ != 0 && ratio_h_ != 0) {
2719 capturer->UpdateAspectRatio(ratio_w_, ratio_h_);
2720 }
2721 const int64 timestamp = send_channel->local_stream_info()->time_stamp();
2722 if (send_codec_) {
2723 QueueBlackFrame(ssrc, timestamp, send_codec_->maxFramerate);
2724 }
2725 return true;
2726}
2727
2728bool WebRtcVideoMediaChannel::RequestIntraFrame() {
2729 // There is no API exposed to application to request a key frame
2730 // ViE does this internally when there are errors from decoder
2731 return false;
2732}
2733
wu@webrtc.orga9890802013-12-13 00:21:03 +00002734void WebRtcVideoMediaChannel::OnPacketReceived(
2735 talk_base::Buffer* packet, const talk_base::PacketTime& packet_time) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002736 // Pick which channel to send this packet to. If this packet doesn't match
2737 // any multiplexed streams, just send it to the default channel. Otherwise,
2738 // send it to the specific decoder instance for that stream.
2739 uint32 ssrc = 0;
2740 if (!GetRtpSsrc(packet->data(), packet->length(), &ssrc))
2741 return;
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00002742 int processing_channel = GetRecvChannelNum(ssrc);
2743 if (processing_channel == -1) {
2744 // Allocate an unsignalled recv channel for processing in conference mode.
henrike@webrtc.org18e59112014-03-14 17:19:38 +00002745 if (!InConferenceMode()) {
buildbot@webrtc.orgdd4742a2014-05-07 14:50:35 +00002746 // If we can't find or allocate one, use the default.
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00002747 processing_channel = video_channel();
henrike@webrtc.org18e59112014-03-14 17:19:38 +00002748 } else if (!CreateUnsignalledRecvChannel(ssrc, &processing_channel)) {
buildbot@webrtc.orgdd4742a2014-05-07 14:50:35 +00002749 // If we can't create an unsignalled recv channel, drop the packet in
henrike@webrtc.org18e59112014-03-14 17:19:38 +00002750 // conference mode.
2751 return;
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00002752 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002753 }
2754
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00002755 engine()->vie()->network()->ReceivedRTPPacket(
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00002756 processing_channel,
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00002757 packet->data(),
wu@webrtc.orga9890802013-12-13 00:21:03 +00002758 static_cast<int>(packet->length()),
2759 webrtc::PacketTime(packet_time.timestamp, packet_time.not_before));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002760}
2761
wu@webrtc.orga9890802013-12-13 00:21:03 +00002762void WebRtcVideoMediaChannel::OnRtcpReceived(
2763 talk_base::Buffer* packet, const talk_base::PacketTime& packet_time) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002764// Sending channels need all RTCP packets with feedback information.
2765// Even sender reports can contain attached report blocks.
2766// Receiving channels need sender reports in order to create
2767// correct receiver reports.
2768
2769 uint32 ssrc = 0;
2770 if (!GetRtcpSsrc(packet->data(), packet->length(), &ssrc)) {
2771 LOG(LS_WARNING) << "Failed to parse SSRC from received RTCP packet";
2772 return;
2773 }
2774 int type = 0;
2775 if (!GetRtcpType(packet->data(), packet->length(), &type)) {
2776 LOG(LS_WARNING) << "Failed to parse type from received RTCP packet";
2777 return;
2778 }
2779
2780 // If it is a sender report, find the channel that is listening.
2781 if (type == kRtcpTypeSR) {
2782 int which_channel = GetRecvChannelNum(ssrc);
2783 if (which_channel != -1 && !IsDefaultChannel(which_channel)) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00002784 engine_->vie()->network()->ReceivedRTCPPacket(
2785 which_channel,
2786 packet->data(),
2787 static_cast<int>(packet->length()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002788 }
2789 }
2790 // SR may continue RR and any RR entry may correspond to any one of the send
2791 // channels. So all RTCP packets must be forwarded all send channels. ViE
2792 // will filter out RR internally.
2793 for (SendChannelMap::iterator iter = send_channels_.begin();
2794 iter != send_channels_.end(); ++iter) {
2795 WebRtcVideoChannelSendInfo* send_channel = iter->second;
2796 int channel_id = send_channel->channel_id();
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00002797 engine_->vie()->network()->ReceivedRTCPPacket(
2798 channel_id,
2799 packet->data(),
2800 static_cast<int>(packet->length()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002801 }
2802}
2803
2804void WebRtcVideoMediaChannel::OnReadyToSend(bool ready) {
2805 SetNetworkTransmissionState(ready);
2806}
2807
2808bool WebRtcVideoMediaChannel::MuteStream(uint32 ssrc, bool muted) {
2809 WebRtcVideoChannelSendInfo* send_channel = GetSendChannel(ssrc);
2810 if (!send_channel) {
2811 LOG(LS_ERROR) << "The specified ssrc " << ssrc << " is not in use.";
2812 return false;
2813 }
2814 send_channel->set_muted(muted);
2815 return true;
2816}
2817
2818bool WebRtcVideoMediaChannel::SetRecvRtpHeaderExtensions(
2819 const std::vector<RtpHeaderExtension>& extensions) {
2820 if (receive_extensions_ == extensions) {
2821 return true;
2822 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002823
2824 const RtpHeaderExtension* offset_extension =
2825 FindHeaderExtension(extensions, kRtpTimestampOffsetHeaderExtension);
2826 const RtpHeaderExtension* send_time_extension =
henrike@webrtc.org79047f92014-03-06 23:46:59 +00002827 FindHeaderExtension(extensions, kRtpAbsoluteSenderTimeHeaderExtension);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002828
2829 // Loop through all receive channels and enable/disable the extensions.
2830 for (RecvChannelMap::iterator channel_it = recv_channels_.begin();
2831 channel_it != recv_channels_.end(); ++channel_it) {
2832 int channel_id = channel_it->second->channel_id();
2833 if (!SetHeaderExtension(
2834 &webrtc::ViERTP_RTCP::SetReceiveTimestampOffsetStatus, channel_id,
2835 offset_extension)) {
2836 return false;
2837 }
2838 if (!SetHeaderExtension(
2839 &webrtc::ViERTP_RTCP::SetReceiveAbsoluteSendTimeStatus, channel_id,
2840 send_time_extension)) {
2841 return false;
2842 }
2843 }
buildbot@webrtc.org150835e2014-05-06 15:54:38 +00002844
2845 receive_extensions_ = extensions;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002846 return true;
2847}
2848
2849bool WebRtcVideoMediaChannel::SetSendRtpHeaderExtensions(
2850 const std::vector<RtpHeaderExtension>& extensions) {
buildbot@webrtc.org150835e2014-05-06 15:54:38 +00002851 if (send_extensions_ == extensions) {
2852 return true;
2853 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002854
2855 const RtpHeaderExtension* offset_extension =
2856 FindHeaderExtension(extensions, kRtpTimestampOffsetHeaderExtension);
2857 const RtpHeaderExtension* send_time_extension =
henrike@webrtc.org79047f92014-03-06 23:46:59 +00002858 FindHeaderExtension(extensions, kRtpAbsoluteSenderTimeHeaderExtension);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002859
2860 // Loop through all send channels and enable/disable the extensions.
2861 for (SendChannelMap::iterator channel_it = send_channels_.begin();
2862 channel_it != send_channels_.end(); ++channel_it) {
2863 int channel_id = channel_it->second->channel_id();
2864 if (!SetHeaderExtension(
2865 &webrtc::ViERTP_RTCP::SetSendTimestampOffsetStatus, channel_id,
2866 offset_extension)) {
2867 return false;
2868 }
2869 if (!SetHeaderExtension(
2870 &webrtc::ViERTP_RTCP::SetSendAbsoluteSendTimeStatus, channel_id,
2871 send_time_extension)) {
2872 return false;
2873 }
2874 }
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00002875
2876 if (send_time_extension) {
2877 // For video RTP packets, we would like to update AbsoluteSendTimeHeader
2878 // Extension closer to the network, @ socket level before sending.
2879 // Pushing the extension id to socket layer.
2880 MediaChannel::SetOption(NetworkInterface::ST_RTP,
2881 talk_base::Socket::OPT_RTP_SENDTIME_EXTN_ID,
2882 send_time_extension->id);
2883 }
buildbot@webrtc.org150835e2014-05-06 15:54:38 +00002884
2885 send_extensions_ = extensions;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002886 return true;
2887}
2888
mallinath@webrtc.org92fdfeb2014-02-17 18:49:41 +00002889int WebRtcVideoMediaChannel::GetRtpSendTimeExtnId() const {
2890 const RtpHeaderExtension* send_time_extension = FindHeaderExtension(
henrike@webrtc.org79047f92014-03-06 23:46:59 +00002891 send_extensions_, kRtpAbsoluteSenderTimeHeaderExtension);
mallinath@webrtc.org92fdfeb2014-02-17 18:49:41 +00002892 if (send_time_extension) {
2893 return send_time_extension->id;
2894 }
2895 return -1;
2896}
2897
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002898bool WebRtcVideoMediaChannel::SetStartSendBandwidth(int bps) {
2899 LOG(LS_INFO) << "WebRtcVideoMediaChannel::SetStartSendBandwidth";
2900
2901 if (!send_codec_) {
2902 LOG(LS_INFO) << "The send codec has not been set up yet";
2903 return true;
2904 }
2905
2906 // On success, SetSendCodec() will reset |send_start_bitrate_| to |bps/1000|,
buildbot@webrtc.orgd1ae89f2014-05-08 19:19:26 +00002907 // by calling MaybeChangeBitrates. That method will also clamp the
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002908 // start bitrate between min and max, consistent with the override behavior
2909 // in SetMaxSendBandwidth.
buildbot@webrtc.orgd1ae89f2014-05-08 19:19:26 +00002910 webrtc::VideoCodec new_codec = *send_codec_;
2911 if (BitrateIsSet(bps)) {
2912 new_codec.startBitrate = bps / 1000;
2913 }
2914 return SetSendCodec(new_codec);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002915}
2916
2917bool WebRtcVideoMediaChannel::SetMaxSendBandwidth(int bps) {
2918 LOG(LS_INFO) << "WebRtcVideoMediaChannel::SetMaxSendBandwidth";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002919
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002920 if (!send_codec_) {
2921 LOG(LS_INFO) << "The send codec has not been set up yet";
2922 return true;
2923 }
2924
buildbot@webrtc.orgd1ae89f2014-05-08 19:19:26 +00002925 webrtc::VideoCodec new_codec = *send_codec_;
2926 if (BitrateIsSet(bps)) {
2927 new_codec.maxBitrate = bps / 1000;
2928 }
2929 if (!SetSendCodec(new_codec)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002930 return false;
2931 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002932 LogSendCodecChange("SetMaxSendBandwidth()");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002933
2934 return true;
2935}
2936
2937bool WebRtcVideoMediaChannel::SetOptions(const VideoOptions &options) {
2938 // Always accept options that are unchanged.
2939 if (options_ == options) {
2940 return true;
2941 }
2942
2943 // Trigger SetSendCodec to set correct noise reduction state if the option has
2944 // changed.
2945 bool denoiser_changed = options.video_noise_reduction.IsSet() &&
2946 (options_.video_noise_reduction != options.video_noise_reduction);
2947
2948 bool leaky_bucket_changed = options.video_leaky_bucket.IsSet() &&
2949 (options_.video_leaky_bucket != options.video_leaky_bucket);
2950
2951 bool buffer_latency_changed = options.buffered_mode_latency.IsSet() &&
2952 (options_.buffered_mode_latency != options.buffered_mode_latency);
2953
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00002954 bool cpu_overuse_detection_changed = options.cpu_overuse_detection.IsSet() &&
2955 (options_.cpu_overuse_detection != options.cpu_overuse_detection);
2956
wu@webrtc.orgde305012013-10-31 15:40:38 +00002957 bool dscp_option_changed = (options_.dscp != options.dscp);
2958
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002959 bool suspend_below_min_bitrate_changed =
2960 options.suspend_below_min_bitrate.IsSet() &&
2961 (options_.suspend_below_min_bitrate != options.suspend_below_min_bitrate);
2962
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002963 bool conference_mode_turned_off = false;
2964 if (options_.conference_mode.IsSet() && options.conference_mode.IsSet() &&
2965 options_.conference_mode.GetWithDefaultIfUnset(false) &&
2966 !options.conference_mode.GetWithDefaultIfUnset(false)) {
2967 conference_mode_turned_off = true;
2968 }
2969
henrike@webrtc.orgdce3feb2014-03-26 01:17:30 +00002970 bool improved_wifi_bwe_changed =
2971 options.use_improved_wifi_bandwidth_estimator.IsSet() &&
2972 options_.use_improved_wifi_bandwidth_estimator !=
2973 options.use_improved_wifi_bandwidth_estimator;
2974
henrike@webrtc.org10bd88e2014-03-11 21:07:25 +00002975
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002976 // Save the options, to be interpreted where appropriate.
2977 // Use options_.SetAll() instead of assignment so that unset value in options
2978 // will not overwrite the previous option value.
2979 options_.SetAll(options);
2980
2981 // Set CPU options for all send channels.
2982 for (SendChannelMap::iterator iter = send_channels_.begin();
2983 iter != send_channels_.end(); ++iter) {
2984 WebRtcVideoChannelSendInfo* send_channel = iter->second;
2985 send_channel->ApplyCpuOptions(options_);
2986 }
2987
buildbot@webrtc.orgd1ae89f2014-05-08 19:19:26 +00002988 if (send_codec_) {
2989 bool reset_send_codec_needed = denoiser_changed;
2990 webrtc::VideoCodec new_codec = *send_codec_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002991
buildbot@webrtc.orgd1ae89f2014-05-08 19:19:26 +00002992 // TODO(pthatcher): Remove this. We don't need 4 ways to set bitrates.
2993 bool lower_min_bitrate;
2994 if (options.lower_min_bitrate.Get(&lower_min_bitrate)) {
2995 new_codec.minBitrate = kLowerMinBitrate;
2996 reset_send_codec_needed = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002997 }
buildbot@webrtc.orgd1ae89f2014-05-08 19:19:26 +00002998
2999 if (conference_mode_turned_off) {
3000 // This is a special case for turning conference mode off.
3001 // Max bitrate should go back to the default maximum value instead
3002 // of the current maximum.
3003 new_codec.maxBitrate = kAutoBandwidth;
3004 reset_send_codec_needed = true;
3005 }
3006
3007 // TODO(pthatcher): Remove this. We don't need 4 ways to set bitrates.
3008 int new_start_bitrate;
3009 if (options.video_start_bitrate.Get(&new_start_bitrate)) {
3010 new_codec.startBitrate = new_start_bitrate;
3011 reset_send_codec_needed = true;
3012 }
3013
3014
3015 LOG(LS_INFO) << "Reset send codec needed is enabled? "
3016 << reset_send_codec_needed;
3017 if (reset_send_codec_needed) {
3018 if (!SetSendCodec(new_codec)) {
3019 return false;
3020 }
3021 LogSendCodecChange("SetOptions()");
3022 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003023 }
henrike@webrtc.org10bd88e2014-03-11 21:07:25 +00003024
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003025 if (leaky_bucket_changed) {
3026 bool enable_leaky_bucket =
3027 options_.video_leaky_bucket.GetWithDefaultIfUnset(false);
buildbot@webrtc.orgf875f152014-04-14 16:06:21 +00003028 LOG(LS_INFO) << "Leaky bucket is enabled? " << enable_leaky_bucket;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003029 for (SendChannelMap::iterator it = send_channels_.begin();
3030 it != send_channels_.end(); ++it) {
3031 if (engine()->vie()->rtp()->SetTransmissionSmoothingStatus(
3032 it->second->channel_id(), enable_leaky_bucket) != 0) {
3033 LOG_RTCERR2(SetTransmissionSmoothingStatus, it->second->channel_id(),
3034 enable_leaky_bucket);
3035 }
3036 }
3037 }
3038 if (buffer_latency_changed) {
3039 int buffer_latency =
3040 options_.buffered_mode_latency.GetWithDefaultIfUnset(
3041 cricket::kBufferedModeDisabled);
buildbot@webrtc.orgf875f152014-04-14 16:06:21 +00003042 LOG(LS_INFO) << "Buffer latency is " << buffer_latency;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003043 for (SendChannelMap::iterator it = send_channels_.begin();
3044 it != send_channels_.end(); ++it) {
3045 if (engine()->vie()->rtp()->SetSenderBufferingMode(
3046 it->second->channel_id(), buffer_latency) != 0) {
3047 LOG_RTCERR2(SetSenderBufferingMode, it->second->channel_id(),
3048 buffer_latency);
3049 }
3050 }
3051 for (RecvChannelMap::iterator it = recv_channels_.begin();
3052 it != recv_channels_.end(); ++it) {
3053 if (engine()->vie()->rtp()->SetReceiverBufferingMode(
3054 it->second->channel_id(), buffer_latency) != 0) {
3055 LOG_RTCERR2(SetReceiverBufferingMode, it->second->channel_id(),
3056 buffer_latency);
3057 }
3058 }
3059 }
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00003060 if (cpu_overuse_detection_changed) {
3061 bool cpu_overuse_detection =
3062 options_.cpu_overuse_detection.GetWithDefaultIfUnset(false);
buildbot@webrtc.orgf875f152014-04-14 16:06:21 +00003063 LOG(LS_INFO) << "CPU overuse detection is enabled? "
3064 << cpu_overuse_detection;
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00003065 for (SendChannelMap::iterator iter = send_channels_.begin();
3066 iter != send_channels_.end(); ++iter) {
3067 WebRtcVideoChannelSendInfo* send_channel = iter->second;
3068 send_channel->SetCpuOveruseDetection(cpu_overuse_detection);
3069 }
3070 }
wu@webrtc.orgde305012013-10-31 15:40:38 +00003071 if (dscp_option_changed) {
3072 talk_base::DiffServCodePoint dscp = talk_base::DSCP_DEFAULT;
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00003073 if (options_.dscp.GetWithDefaultIfUnset(false))
wu@webrtc.orgde305012013-10-31 15:40:38 +00003074 dscp = kVideoDscpValue;
buildbot@webrtc.orgf875f152014-04-14 16:06:21 +00003075 LOG(LS_INFO) << "DSCP is " << dscp;
wu@webrtc.orgde305012013-10-31 15:40:38 +00003076 if (MediaChannel::SetDscp(dscp) != 0) {
3077 LOG(LS_WARNING) << "Failed to set DSCP settings for video channel";
3078 }
3079 }
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00003080 if (suspend_below_min_bitrate_changed) {
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00003081 if (options_.suspend_below_min_bitrate.GetWithDefaultIfUnset(false)) {
buildbot@webrtc.orgf875f152014-04-14 16:06:21 +00003082 LOG(LS_INFO) << "Suspend below min bitrate enabled.";
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00003083 for (SendChannelMap::iterator it = send_channels_.begin();
3084 it != send_channels_.end(); ++it) {
3085 engine()->vie()->codec()->SuspendBelowMinBitrate(
3086 it->second->channel_id());
3087 }
3088 } else {
3089 LOG(LS_WARNING) << "Cannot disable video suspension once it is enabled";
3090 }
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00003091 }
henrike@webrtc.orgdce3feb2014-03-26 01:17:30 +00003092 if (improved_wifi_bwe_changed) {
buildbot@webrtc.orgf875f152014-04-14 16:06:21 +00003093 LOG(LS_INFO) << "Improved WIFI BWE called.";
henrike@webrtc.orgdce3feb2014-03-26 01:17:30 +00003094 webrtc::Config config;
3095 config.Set(new webrtc::AimdRemoteRateControl(
3096 options_.use_improved_wifi_bandwidth_estimator
3097 .GetWithDefaultIfUnset(false)));
3098 for (SendChannelMap::iterator it = send_channels_.begin();
3099 it != send_channels_.end(); ++it) {
3100 engine()->vie()->network()->SetBandwidthEstimationConfig(
3101 it->second->channel_id(), config);
3102 }
3103 }
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +00003104 webrtc::CpuOveruseOptions overuse_options;
3105 if (GetCpuOveruseOptions(options_, &overuse_options)) {
3106 for (SendChannelMap::iterator it = send_channels_.begin();
3107 it != send_channels_.end(); ++it) {
3108 if (engine()->vie()->base()->SetCpuOveruseOptions(
3109 it->second->channel_id(), overuse_options) != 0) {
3110 LOG_RTCERR1(SetCpuOveruseOptions, it->second->channel_id());
3111 }
3112 }
3113 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003114 return true;
3115}
3116
3117void WebRtcVideoMediaChannel::SetInterface(NetworkInterface* iface) {
3118 MediaChannel::SetInterface(iface);
3119 // Set the RTP recv/send buffer to a bigger size
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00003120 MediaChannel::SetOption(NetworkInterface::ST_RTP,
3121 talk_base::Socket::OPT_RCVBUF,
3122 kVideoRtpBufferSize);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003123
3124 // TODO(sriniv): Remove or re-enable this.
3125 // As part of b/8030474, send-buffer is size now controlled through
3126 // portallocator flags.
3127 // network_interface_->SetOption(NetworkInterface::ST_RTP,
3128 // talk_base::Socket::OPT_SNDBUF,
3129 // kVideoRtpBufferSize);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003130}
3131
3132void WebRtcVideoMediaChannel::UpdateAspectRatio(int ratio_w, int ratio_h) {
3133 ASSERT(ratio_w != 0);
3134 ASSERT(ratio_h != 0);
3135 ratio_w_ = ratio_w;
3136 ratio_h_ = ratio_h;
3137 // For now assume that all streams want the same aspect ratio.
3138 // TODO(hellner): remove the need for this assumption.
3139 for (SendChannelMap::iterator iter = send_channels_.begin();
3140 iter != send_channels_.end(); ++iter) {
3141 WebRtcVideoChannelSendInfo* send_channel = iter->second;
3142 VideoCapturer* capturer = send_channel->video_capturer();
3143 if (capturer) {
3144 capturer->UpdateAspectRatio(ratio_w, ratio_h);
3145 }
3146 }
3147}
3148
3149bool WebRtcVideoMediaChannel::GetRenderer(uint32 ssrc,
3150 VideoRenderer** renderer) {
3151 RecvChannelMap::const_iterator it = recv_channels_.find(ssrc);
3152 if (it == recv_channels_.end()) {
3153 if (first_receive_ssrc_ == ssrc &&
3154 recv_channels_.find(0) != recv_channels_.end()) {
3155 LOG(LS_INFO) << " GetRenderer " << ssrc
3156 << " reuse default renderer #"
3157 << vie_channel_;
3158 *renderer = recv_channels_[0]->render_adapter()->renderer();
3159 return true;
3160 }
3161 return false;
3162 }
3163
3164 *renderer = it->second->render_adapter()->renderer();
3165 return true;
3166}
3167
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00003168bool WebRtcVideoMediaChannel::GetVideoAdapter(
3169 uint32 ssrc, CoordinatedVideoAdapter** video_adapter) {
3170 SendChannelMap::iterator it = send_channels_.find(ssrc);
3171 if (it == send_channels_.end()) {
3172 return false;
3173 }
3174 *video_adapter = it->second->video_adapter();
3175 return true;
3176}
3177
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003178void WebRtcVideoMediaChannel::SendFrame(VideoCapturer* capturer,
3179 const VideoFrame* frame) {
wu@webrtc.org24301a62013-12-13 19:17:43 +00003180 // If the |capturer| is registered to any send channel, then send the frame
3181 // to those send channels.
3182 bool capturer_is_channel_owned = false;
3183 for (SendChannelMap::iterator iter = send_channels_.begin();
3184 iter != send_channels_.end(); ++iter) {
3185 WebRtcVideoChannelSendInfo* send_channel = iter->second;
3186 if (send_channel->video_capturer() == capturer) {
3187 SendFrame(send_channel, frame, capturer->IsScreencast());
3188 capturer_is_channel_owned = true;
3189 }
3190 }
3191 if (capturer_is_channel_owned) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003192 return;
3193 }
wu@webrtc.org24301a62013-12-13 19:17:43 +00003194
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003195 // TODO(hellner): Remove below for loop once the captured frame no longer
3196 // come from the engine, i.e. the engine no longer owns a capturer.
3197 for (SendChannelMap::iterator iter = send_channels_.begin();
3198 iter != send_channels_.end(); ++iter) {
3199 WebRtcVideoChannelSendInfo* send_channel = iter->second;
3200 if (send_channel->video_capturer() == NULL) {
3201 SendFrame(send_channel, frame, capturer->IsScreencast());
3202 }
3203 }
3204}
3205
3206bool WebRtcVideoMediaChannel::SendFrame(
3207 WebRtcVideoChannelSendInfo* send_channel,
3208 const VideoFrame* frame,
3209 bool is_screencast) {
3210 if (!send_channel) {
3211 return false;
3212 }
3213 if (!send_codec_) {
3214 // Send codec has not been set. No reason to process the frame any further.
3215 return false;
3216 }
3217 const VideoFormat& video_format = send_channel->video_format();
3218 // If the frame should be dropped.
3219 const bool video_format_set = video_format != cricket::VideoFormat();
3220 if (video_format_set &&
3221 (video_format.width == 0 && video_format.height == 0)) {
3222 return true;
3223 }
3224
3225 // Checks if we need to reset vie send codec.
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00003226 if (!MaybeResetVieSendCodec(send_channel,
3227 static_cast<int>(frame->GetWidth()),
3228 static_cast<int>(frame->GetHeight()),
3229 is_screencast, NULL)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003230 LOG(LS_ERROR) << "MaybeResetVieSendCodec failed with "
3231 << frame->GetWidth() << "x" << frame->GetHeight();
3232 return false;
3233 }
3234 const VideoFrame* frame_out = frame;
3235 talk_base::scoped_ptr<VideoFrame> processed_frame;
3236 // Disable muting for screencast.
3237 const bool mute = (send_channel->muted() && !is_screencast);
3238 send_channel->ProcessFrame(*frame_out, mute, processed_frame.use());
3239 if (processed_frame) {
3240 frame_out = processed_frame.get();
3241 }
3242
3243 webrtc::ViEVideoFrameI420 frame_i420;
3244 // TODO(ronghuawu): Update the webrtc::ViEVideoFrameI420
3245 // to use const unsigned char*
3246 frame_i420.y_plane = const_cast<unsigned char*>(frame_out->GetYPlane());
3247 frame_i420.u_plane = const_cast<unsigned char*>(frame_out->GetUPlane());
3248 frame_i420.v_plane = const_cast<unsigned char*>(frame_out->GetVPlane());
3249 frame_i420.y_pitch = frame_out->GetYPitch();
3250 frame_i420.u_pitch = frame_out->GetUPitch();
3251 frame_i420.v_pitch = frame_out->GetVPitch();
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00003252 frame_i420.width = static_cast<uint16>(frame_out->GetWidth());
3253 frame_i420.height = static_cast<uint16>(frame_out->GetHeight());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003254
3255 int64 timestamp_ntp_ms = 0;
3256 // TODO(justinlin): Reenable after Windows issues with clock drift are fixed.
3257 // Currently reverted to old behavior of discarding capture timestamp.
3258#if 0
henrike@webrtc.orgf5bebd42014-04-04 18:39:07 +00003259 static const int kTimestampDeltaInSecondsForWarning = 2;
3260
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003261 // If the frame timestamp is 0, we will use the deliver time.
3262 const int64 frame_timestamp = frame->GetTimeStamp();
3263 if (frame_timestamp != 0) {
3264 if (abs(time(NULL) - frame_timestamp / talk_base::kNumNanosecsPerSec) >
3265 kTimestampDeltaInSecondsForWarning) {
3266 LOG(LS_WARNING) << "Frame timestamp differs by more than "
3267 << kTimestampDeltaInSecondsForWarning << " seconds from "
3268 << "current Unix timestamp.";
3269 }
3270
3271 timestamp_ntp_ms =
3272 talk_base::UnixTimestampNanosecsToNtpMillisecs(frame_timestamp);
3273 }
3274#endif
3275
3276 return send_channel->external_capture()->IncomingFrameI420(
3277 frame_i420, timestamp_ntp_ms) == 0;
3278}
3279
3280bool WebRtcVideoMediaChannel::CreateChannel(uint32 ssrc_key,
3281 MediaDirection direction,
3282 int* channel_id) {
3283 // There are 3 types of channels. Sending only, receiving only and
3284 // sending and receiving. The sending and receiving channel is the
3285 // default channel and there is only one. All other channels that are created
3286 // are associated with the default channel which must exist. The default
3287 // channel id is stored in |vie_channel_|. All channels need to know about
3288 // the default channel to properly handle remb which is why there are
3289 // different ViE create channel calls.
3290 // For this channel the local and remote ssrc key is 0. However, it may
3291 // have a non-zero local and/or remote ssrc depending on if it is currently
3292 // sending and/or receiving.
3293 if ((vie_channel_ == -1 || direction == MD_SENDRECV) &&
3294 (!send_channels_.empty() || !recv_channels_.empty())) {
3295 ASSERT(false);
3296 return false;
3297 }
3298
3299 *channel_id = -1;
3300 if (direction == MD_RECV) {
3301 // All rec channels are associated with the default channel |vie_channel_|
3302 if (engine_->vie()->base()->CreateReceiveChannel(*channel_id,
3303 vie_channel_) != 0) {
3304 LOG_RTCERR2(CreateReceiveChannel, *channel_id, vie_channel_);
3305 return false;
3306 }
3307 } else if (direction == MD_SEND) {
3308 if (engine_->vie()->base()->CreateChannel(*channel_id,
3309 vie_channel_) != 0) {
3310 LOG_RTCERR2(CreateChannel, *channel_id, vie_channel_);
3311 return false;
3312 }
3313 } else {
3314 ASSERT(direction == MD_SENDRECV);
3315 if (engine_->vie()->base()->CreateChannel(*channel_id) != 0) {
3316 LOG_RTCERR1(CreateChannel, *channel_id);
3317 return false;
3318 }
3319 }
3320 if (!ConfigureChannel(*channel_id, direction, ssrc_key)) {
3321 engine_->vie()->base()->DeleteChannel(*channel_id);
3322 *channel_id = -1;
3323 return false;
3324 }
3325
3326 return true;
3327}
3328
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00003329bool WebRtcVideoMediaChannel::CreateUnsignalledRecvChannel(
3330 uint32 ssrc_key, int* out_channel_id) {
henrike@webrtc.org18e59112014-03-14 17:19:38 +00003331 int unsignalled_recv_channel_limit =
3332 options_.unsignalled_recv_stream_limit.GetWithDefaultIfUnset(
3333 kNumDefaultUnsignalledVideoRecvStreams);
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00003334 if (num_unsignalled_recv_channels_ >= unsignalled_recv_channel_limit) {
3335 return false;
3336 }
3337 if (!CreateChannel(ssrc_key, MD_RECV, out_channel_id)) {
3338 return false;
3339 }
3340 // TODO(tvsriram): Support dynamic sizing of unsignalled recv channels.
3341 num_unsignalled_recv_channels_++;
3342 return true;
3343}
3344
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003345bool WebRtcVideoMediaChannel::ConfigureChannel(int channel_id,
3346 MediaDirection direction,
3347 uint32 ssrc_key) {
3348 const bool receiving = (direction == MD_RECV) || (direction == MD_SENDRECV);
3349 const bool sending = (direction == MD_SEND) || (direction == MD_SENDRECV);
3350 // Register external transport.
3351 if (engine_->vie()->network()->RegisterSendTransport(
3352 channel_id, *this) != 0) {
3353 LOG_RTCERR1(RegisterSendTransport, channel_id);
3354 return false;
3355 }
3356
3357 // Set MTU.
3358 if (engine_->vie()->network()->SetMTU(channel_id, kVideoMtu) != 0) {
3359 LOG_RTCERR2(SetMTU, channel_id, kVideoMtu);
3360 return false;
3361 }
3362 // Turn on RTCP and loss feedback reporting.
3363 if (engine()->vie()->rtp()->SetRTCPStatus(
3364 channel_id, webrtc::kRtcpCompound_RFC4585) != 0) {
3365 LOG_RTCERR2(SetRTCPStatus, channel_id, webrtc::kRtcpCompound_RFC4585);
3366 return false;
3367 }
3368 // Enable pli as key frame request method.
3369 if (engine_->vie()->rtp()->SetKeyFrameRequestMethod(
3370 channel_id, webrtc::kViEKeyFrameRequestPliRtcp) != 0) {
3371 LOG_RTCERR2(SetKeyFrameRequestMethod,
3372 channel_id, webrtc::kViEKeyFrameRequestPliRtcp);
3373 return false;
3374 }
3375 if (!SetNackFec(channel_id, send_red_type_, send_fec_type_, nack_enabled_)) {
3376 // Logged in SetNackFec. Don't spam the logs.
3377 return false;
3378 }
3379 // Note that receiving must always be configured before sending to ensure
3380 // that send and receive channel is configured correctly (ConfigureReceiving
3381 // assumes no sending).
3382 if (receiving) {
3383 if (!ConfigureReceiving(channel_id, ssrc_key)) {
3384 return false;
3385 }
3386 }
3387 if (sending) {
3388 if (!ConfigureSending(channel_id, ssrc_key)) {
3389 return false;
3390 }
3391 }
3392
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00003393 // Start receiving for both receive and send channels so that we get incoming
3394 // RTP (if receiving) as well as RTCP feedback (if sending).
3395 if (engine()->vie()->base()->StartReceive(channel_id) != 0) {
3396 LOG_RTCERR1(StartReceive, channel_id);
3397 return false;
3398 }
3399
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003400 return true;
3401}
3402
3403bool WebRtcVideoMediaChannel::ConfigureReceiving(int channel_id,
3404 uint32 remote_ssrc_key) {
3405 // Make sure that an SSRC/key isn't registered more than once.
3406 if (recv_channels_.find(remote_ssrc_key) != recv_channels_.end()) {
3407 return false;
3408 }
3409 // Connect the voice channel, if there is one.
3410 // TODO(perkj): The A/V is synched by the receiving channel. So we need to
3411 // know the SSRC of the remote audio channel in order to fetch the correct
3412 // webrtc VoiceEngine channel. For now- only sync the default channel used
3413 // in 1-1 calls.
3414 if (remote_ssrc_key == 0 && voice_channel_) {
3415 WebRtcVoiceMediaChannel* voice_channel =
3416 static_cast<WebRtcVoiceMediaChannel*>(voice_channel_);
3417 if (engine_->vie()->base()->ConnectAudioChannel(
3418 vie_channel_, voice_channel->voe_channel()) != 0) {
3419 LOG_RTCERR2(ConnectAudioChannel, channel_id,
3420 voice_channel->voe_channel());
3421 LOG(LS_WARNING) << "A/V not synchronized";
3422 // Not a fatal error.
3423 }
3424 }
3425
3426 talk_base::scoped_ptr<WebRtcVideoChannelRecvInfo> channel_info(
3427 new WebRtcVideoChannelRecvInfo(channel_id));
3428
3429 // Install a render adapter.
3430 if (engine_->vie()->render()->AddRenderer(channel_id,
3431 webrtc::kVideoI420, channel_info->render_adapter()) != 0) {
3432 LOG_RTCERR3(AddRenderer, channel_id, webrtc::kVideoI420,
3433 channel_info->render_adapter());
3434 return false;
3435 }
3436
3437
3438 if (engine_->vie()->rtp()->SetRembStatus(channel_id,
3439 kNotSending,
3440 remb_enabled_) != 0) {
3441 LOG_RTCERR3(SetRembStatus, channel_id, kNotSending, remb_enabled_);
3442 return false;
3443 }
3444
3445 if (!SetHeaderExtension(&webrtc::ViERTP_RTCP::SetReceiveTimestampOffsetStatus,
3446 channel_id, receive_extensions_, kRtpTimestampOffsetHeaderExtension)) {
3447 return false;
3448 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003449 if (!SetHeaderExtension(
3450 &webrtc::ViERTP_RTCP::SetReceiveAbsoluteSendTimeStatus, channel_id,
henrike@webrtc.org79047f92014-03-06 23:46:59 +00003451 receive_extensions_, kRtpAbsoluteSenderTimeHeaderExtension)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003452 return false;
3453 }
3454
3455 if (remote_ssrc_key != 0) {
3456 // Use the same SSRC as our default channel
3457 // (so the RTCP reports are correct).
3458 unsigned int send_ssrc = 0;
3459 webrtc::ViERTP_RTCP* rtp = engine()->vie()->rtp();
3460 if (rtp->GetLocalSSRC(vie_channel_, send_ssrc) == -1) {
3461 LOG_RTCERR2(GetLocalSSRC, vie_channel_, send_ssrc);
3462 return false;
3463 }
3464 if (rtp->SetLocalSSRC(channel_id, send_ssrc) == -1) {
3465 LOG_RTCERR2(SetLocalSSRC, channel_id, send_ssrc);
3466 return false;
3467 }
3468 } // Else this is the the default channel and we don't change the SSRC.
3469
3470 // Disable color enhancement since it is a bit too aggressive.
3471 if (engine()->vie()->image()->EnableColorEnhancement(channel_id,
3472 false) != 0) {
3473 LOG_RTCERR1(EnableColorEnhancement, channel_id);
3474 return false;
3475 }
3476
3477 if (!SetReceiveCodecs(channel_info.get())) {
3478 return false;
3479 }
3480
3481 int buffer_latency =
3482 options_.buffered_mode_latency.GetWithDefaultIfUnset(
3483 cricket::kBufferedModeDisabled);
3484 if (buffer_latency != cricket::kBufferedModeDisabled) {
3485 if (engine()->vie()->rtp()->SetReceiverBufferingMode(
3486 channel_id, buffer_latency) != 0) {
3487 LOG_RTCERR2(SetReceiverBufferingMode, channel_id, buffer_latency);
3488 }
3489 }
3490
3491 if (render_started_) {
3492 if (engine_->vie()->render()->StartRender(channel_id) != 0) {
3493 LOG_RTCERR1(StartRender, channel_id);
3494 return false;
3495 }
3496 }
3497
3498 // Register decoder observer for incoming framerate and bitrate.
3499 if (engine()->vie()->codec()->RegisterDecoderObserver(
3500 channel_id, *channel_info->decoder_observer()) != 0) {
3501 LOG_RTCERR1(RegisterDecoderObserver, channel_info->decoder_observer());
3502 return false;
3503 }
3504
3505 recv_channels_[remote_ssrc_key] = channel_info.release();
3506 return true;
3507}
3508
3509bool WebRtcVideoMediaChannel::ConfigureSending(int channel_id,
3510 uint32 local_ssrc_key) {
3511 // The ssrc key can be zero or correspond to an SSRC.
3512 // Make sure the default channel isn't configured more than once.
3513 if (local_ssrc_key == 0 && send_channels_.find(0) != send_channels_.end()) {
3514 return false;
3515 }
3516 // Make sure that the SSRC is not already in use.
3517 uint32 dummy_key;
3518 if (GetSendChannelKey(local_ssrc_key, &dummy_key)) {
3519 return false;
3520 }
3521 int vie_capture = 0;
3522 webrtc::ViEExternalCapture* external_capture = NULL;
3523 // Register external capture.
3524 if (engine()->vie()->capture()->AllocateExternalCaptureDevice(
3525 vie_capture, external_capture) != 0) {
3526 LOG_RTCERR0(AllocateExternalCaptureDevice);
3527 return false;
3528 }
3529
3530 // Connect external capture.
3531 if (engine()->vie()->capture()->ConnectCaptureDevice(
3532 vie_capture, channel_id) != 0) {
3533 LOG_RTCERR2(ConnectCaptureDevice, vie_capture, channel_id);
3534 return false;
3535 }
3536 talk_base::scoped_ptr<WebRtcVideoChannelSendInfo> send_channel(
3537 new WebRtcVideoChannelSendInfo(channel_id, vie_capture,
3538 external_capture,
3539 engine()->cpu_monitor()));
3540 send_channel->ApplyCpuOptions(options_);
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00003541 send_channel->SignalCpuAdaptationUnable.connect(this,
3542 &WebRtcVideoMediaChannel::OnCpuAdaptationUnable);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003543
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00003544 if (options_.cpu_overuse_detection.GetWithDefaultIfUnset(false)) {
3545 send_channel->SetCpuOveruseDetection(true);
3546 }
3547
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +00003548 webrtc::CpuOveruseOptions overuse_options;
3549 if (GetCpuOveruseOptions(options_, &overuse_options)) {
3550 if (engine()->vie()->base()->SetCpuOveruseOptions(channel_id,
3551 overuse_options) != 0) {
3552 LOG_RTCERR1(SetCpuOveruseOptions, channel_id);
3553 }
3554 }
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +00003555
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003556 // Register encoder observer for outgoing framerate and bitrate.
3557 if (engine()->vie()->codec()->RegisterEncoderObserver(
3558 channel_id, *send_channel->encoder_observer()) != 0) {
3559 LOG_RTCERR1(RegisterEncoderObserver, send_channel->encoder_observer());
3560 return false;
3561 }
3562
3563 if (!SetHeaderExtension(&webrtc::ViERTP_RTCP::SetSendTimestampOffsetStatus,
3564 channel_id, send_extensions_, kRtpTimestampOffsetHeaderExtension)) {
3565 return false;
3566 }
3567
3568 if (!SetHeaderExtension(&webrtc::ViERTP_RTCP::SetSendAbsoluteSendTimeStatus,
henrike@webrtc.org79047f92014-03-06 23:46:59 +00003569 channel_id, send_extensions_, kRtpAbsoluteSenderTimeHeaderExtension)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003570 return false;
3571 }
3572
3573 if (options_.video_leaky_bucket.GetWithDefaultIfUnset(false)) {
3574 if (engine()->vie()->rtp()->SetTransmissionSmoothingStatus(channel_id,
3575 true) != 0) {
3576 LOG_RTCERR2(SetTransmissionSmoothingStatus, channel_id, true);
3577 return false;
3578 }
3579 }
3580
3581 int buffer_latency =
3582 options_.buffered_mode_latency.GetWithDefaultIfUnset(
3583 cricket::kBufferedModeDisabled);
3584 if (buffer_latency != cricket::kBufferedModeDisabled) {
3585 if (engine()->vie()->rtp()->SetSenderBufferingMode(
3586 channel_id, buffer_latency) != 0) {
3587 LOG_RTCERR2(SetSenderBufferingMode, channel_id, buffer_latency);
3588 }
3589 }
henrike@webrtc.org6e3dbc22014-03-25 17:09:47 +00003590
3591 if (options_.suspend_below_min_bitrate.GetWithDefaultIfUnset(false)) {
3592 engine()->vie()->codec()->SuspendBelowMinBitrate(channel_id);
3593 }
3594
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003595 // The remb status direction correspond to the RTP stream (and not the RTCP
3596 // stream). I.e. if send remb is enabled it means it is receiving remote
3597 // rembs and should use them to estimate bandwidth. Receive remb mean that
3598 // remb packets will be generated and that the channel should be included in
3599 // it. If remb is enabled all channels are allowed to contribute to the remb
3600 // but only receive channels will ever end up actually contributing. This
3601 // keeps the logic simple.
3602 if (engine_->vie()->rtp()->SetRembStatus(channel_id,
3603 remb_enabled_,
3604 remb_enabled_) != 0) {
3605 LOG_RTCERR3(SetRembStatus, channel_id, remb_enabled_, remb_enabled_);
3606 return false;
3607 }
3608 if (!SetNackFec(channel_id, send_red_type_, send_fec_type_, nack_enabled_)) {
3609 // Logged in SetNackFec. Don't spam the logs.
3610 return false;
3611 }
3612
3613 send_channels_[local_ssrc_key] = send_channel.release();
3614
3615 return true;
3616}
3617
3618bool WebRtcVideoMediaChannel::SetNackFec(int channel_id,
3619 int red_payload_type,
3620 int fec_payload_type,
3621 bool nack_enabled) {
3622 bool enable = (red_payload_type != -1 && fec_payload_type != -1 &&
3623 !InConferenceMode());
3624 if (enable) {
3625 if (engine_->vie()->rtp()->SetHybridNACKFECStatus(
3626 channel_id, nack_enabled, red_payload_type, fec_payload_type) != 0) {
3627 LOG_RTCERR4(SetHybridNACKFECStatus,
3628 channel_id, nack_enabled, red_payload_type, fec_payload_type);
3629 return false;
3630 }
3631 LOG(LS_INFO) << "Hybrid NACK/FEC enabled for channel " << channel_id;
3632 } else {
3633 if (engine_->vie()->rtp()->SetNACKStatus(channel_id, nack_enabled) != 0) {
3634 LOG_RTCERR1(SetNACKStatus, channel_id);
3635 return false;
3636 }
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00003637 std::string enabled = nack_enabled ? "enabled" : "disabled";
3638 LOG(LS_INFO) << "NACK " << enabled << " for channel " << channel_id;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003639 }
3640 return true;
3641}
3642
buildbot@webrtc.orgd1ae89f2014-05-08 19:19:26 +00003643bool WebRtcVideoMediaChannel::SetSendCodec(const webrtc::VideoCodec& codec) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003644 bool ret_val = true;
3645 for (SendChannelMap::iterator iter = send_channels_.begin();
3646 iter != send_channels_.end(); ++iter) {
3647 WebRtcVideoChannelSendInfo* send_channel = iter->second;
buildbot@webrtc.orgd1ae89f2014-05-08 19:19:26 +00003648 ret_val = SetSendCodec(send_channel, codec) && ret_val;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003649 }
3650 if (ret_val) {
3651 // All SetSendCodec calls were successful. Update the global state
3652 // accordingly.
3653 send_codec_.reset(new webrtc::VideoCodec(codec));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003654 } else {
3655 // At least one SetSendCodec call failed, rollback.
3656 for (SendChannelMap::iterator iter = send_channels_.begin();
3657 iter != send_channels_.end(); ++iter) {
3658 WebRtcVideoChannelSendInfo* send_channel = iter->second;
3659 if (send_codec_) {
buildbot@webrtc.orgd1ae89f2014-05-08 19:19:26 +00003660 SetSendCodec(send_channel, *send_codec_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003661 }
3662 }
3663 }
3664 return ret_val;
3665}
3666
3667bool WebRtcVideoMediaChannel::SetSendCodec(
3668 WebRtcVideoChannelSendInfo* send_channel,
buildbot@webrtc.orgd1ae89f2014-05-08 19:19:26 +00003669 const webrtc::VideoCodec& codec) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003670 if (!send_channel) {
3671 return false;
3672 }
buildbot@webrtc.orgd1ae89f2014-05-08 19:19:26 +00003673
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003674 const int channel_id = send_channel->channel_id();
3675 // Make a copy of the codec
3676 webrtc::VideoCodec target_codec = codec;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003677
3678 // Set the default number of temporal layers for VP8.
3679 if (webrtc::kVideoCodecVP8 == codec.codecType) {
3680 target_codec.codecSpecific.VP8.numberOfTemporalLayers =
3681 kDefaultNumberOfTemporalLayers;
3682
3683 // Turn off the VP8 error resilience
3684 target_codec.codecSpecific.VP8.resilience = webrtc::kResilienceOff;
3685
3686 bool enable_denoising =
3687 options_.video_noise_reduction.GetWithDefaultIfUnset(false);
3688 target_codec.codecSpecific.VP8.denoisingOn = enable_denoising;
3689 }
3690
3691 // Register external encoder if codec type is supported by encoder factory.
3692 if (engine()->IsExternalEncoderCodecType(codec.codecType) &&
3693 !send_channel->IsEncoderRegistered(target_codec.plType)) {
3694 webrtc::VideoEncoder* encoder =
3695 engine()->CreateExternalEncoder(codec.codecType);
3696 if (encoder) {
3697 if (engine()->vie()->ext_codec()->RegisterExternalSendCodec(
3698 channel_id, target_codec.plType, encoder, false) == 0) {
3699 send_channel->RegisterEncoder(target_codec.plType, encoder);
3700 } else {
3701 LOG_RTCERR2(RegisterExternalSendCodec, channel_id, target_codec.plName);
3702 engine()->DestroyExternalEncoder(encoder);
3703 }
3704 }
3705 }
3706
3707 // Resolution and framerate may vary for different send channels.
3708 const VideoFormat& video_format = send_channel->video_format();
3709 UpdateVideoCodec(video_format, &target_codec);
3710
3711 if (target_codec.width == 0 && target_codec.height == 0) {
3712 const uint32 ssrc = send_channel->stream_params()->first_ssrc();
3713 LOG(LS_INFO) << "0x0 resolution selected. Captured frames will be dropped "
3714 << "for ssrc: " << ssrc << ".";
3715 } else {
buildbot@webrtc.orgd1ae89f2014-05-08 19:19:26 +00003716 MaybeChangeBitrates(channel_id, &target_codec);
wu@webrtc.org05e7b442014-04-01 17:44:24 +00003717 webrtc::VideoCodec current_codec;
3718 if (!engine()->vie()->codec()->GetSendCodec(channel_id, current_codec)) {
3719 // Compare against existing configured send codec.
3720 if (current_codec == target_codec) {
3721 // Codec is already configured on channel. no need to apply.
3722 return true;
3723 }
3724 }
3725
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003726 if (0 != engine()->vie()->codec()->SetSendCodec(channel_id, target_codec)) {
3727 LOG_RTCERR2(SetSendCodec, channel_id, target_codec.plName);
3728 return false;
3729 }
3730
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00003731 // NOTE: SetRtxSendPayloadType must be called after all simulcast SSRCs
3732 // are configured. Otherwise ssrc's configured after this point will use
3733 // the primary PT for RTX.
3734 if (send_rtx_type_ != -1 &&
3735 engine()->vie()->rtp()->SetRtxSendPayloadType(channel_id,
3736 send_rtx_type_) != 0) {
3737 LOG_RTCERR2(SetRtxSendPayloadType, channel_id, send_rtx_type_);
3738 return false;
3739 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003740 }
3741 send_channel->set_interval(
3742 cricket::VideoFormat::FpsToInterval(target_codec.maxFramerate));
3743 return true;
3744}
3745
3746
3747static std::string ToString(webrtc::VideoCodecComplexity complexity) {
3748 switch (complexity) {
3749 case webrtc::kComplexityNormal:
3750 return "normal";
3751 case webrtc::kComplexityHigh:
3752 return "high";
3753 case webrtc::kComplexityHigher:
3754 return "higher";
3755 case webrtc::kComplexityMax:
3756 return "max";
3757 default:
3758 return "unknown";
3759 }
3760}
3761
3762static std::string ToString(webrtc::VP8ResilienceMode resilience) {
3763 switch (resilience) {
3764 case webrtc::kResilienceOff:
3765 return "off";
3766 case webrtc::kResilientStream:
3767 return "stream";
3768 case webrtc::kResilientFrames:
3769 return "frames";
3770 default:
3771 return "unknown";
3772 }
3773}
3774
3775void WebRtcVideoMediaChannel::LogSendCodecChange(const std::string& reason) {
3776 webrtc::VideoCodec vie_codec;
3777 if (engine()->vie()->codec()->GetSendCodec(vie_channel_, vie_codec) != 0) {
3778 LOG_RTCERR1(GetSendCodec, vie_channel_);
3779 return;
3780 }
3781
3782 LOG(LS_INFO) << reason << " : selected video codec "
3783 << vie_codec.plName << "/"
3784 << vie_codec.width << "x" << vie_codec.height << "x"
3785 << static_cast<int>(vie_codec.maxFramerate) << "fps"
3786 << "@" << vie_codec.maxBitrate << "kbps"
3787 << " (min=" << vie_codec.minBitrate << "kbps,"
3788 << " start=" << vie_codec.startBitrate << "kbps)";
3789 LOG(LS_INFO) << "Video max quantization: " << vie_codec.qpMax;
3790 if (webrtc::kVideoCodecVP8 == vie_codec.codecType) {
3791 LOG(LS_INFO) << "VP8 number of temporal layers: "
3792 << static_cast<int>(
3793 vie_codec.codecSpecific.VP8.numberOfTemporalLayers);
3794 LOG(LS_INFO) << "VP8 options : "
3795 << "picture loss indication = "
3796 << vie_codec.codecSpecific.VP8.pictureLossIndicationOn
3797 << ", feedback mode = "
3798 << vie_codec.codecSpecific.VP8.feedbackModeOn
3799 << ", complexity = "
3800 << ToString(vie_codec.codecSpecific.VP8.complexity)
3801 << ", resilience = "
3802 << ToString(vie_codec.codecSpecific.VP8.resilience)
3803 << ", denoising = "
3804 << vie_codec.codecSpecific.VP8.denoisingOn
3805 << ", error concealment = "
3806 << vie_codec.codecSpecific.VP8.errorConcealmentOn
3807 << ", automatic resize = "
3808 << vie_codec.codecSpecific.VP8.automaticResizeOn
3809 << ", frame dropping = "
3810 << vie_codec.codecSpecific.VP8.frameDroppingOn
3811 << ", key frame interval = "
3812 << vie_codec.codecSpecific.VP8.keyFrameInterval;
3813 }
3814
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00003815 if (send_rtx_type_ != -1) {
3816 LOG(LS_INFO) << "RTX payload type: " << send_rtx_type_;
3817 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003818}
3819
3820bool WebRtcVideoMediaChannel::SetReceiveCodecs(
3821 WebRtcVideoChannelRecvInfo* info) {
3822 int red_type = -1;
3823 int fec_type = -1;
3824 int channel_id = info->channel_id();
buildbot@webrtc.orgdd4742a2014-05-07 14:50:35 +00003825 // Build a map from payload types to video codecs so that we easily can find
3826 // out if associated payload types are referring to valid codecs.
3827 std::map<int, webrtc::VideoCodec*> pt_to_codec;
3828 for (std::vector<webrtc::VideoCodec>::iterator it = receive_codecs_.begin();
3829 it != receive_codecs_.end(); ++it) {
3830 pt_to_codec[it->plType] = &(*it);
3831 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003832 for (std::vector<webrtc::VideoCodec>::iterator it = receive_codecs_.begin();
3833 it != receive_codecs_.end(); ++it) {
3834 if (it->codecType == webrtc::kVideoCodecRED) {
3835 red_type = it->plType;
3836 } else if (it->codecType == webrtc::kVideoCodecULPFEC) {
3837 fec_type = it->plType;
3838 }
buildbot@webrtc.orgdd4742a2014-05-07 14:50:35 +00003839 // If this is an RTX codec we have to verify that it is associated with
3840 // a valid video codec which we have RTX support for.
3841 if (_stricmp(it->plName, kRtxCodecName) == 0) {
3842 std::map<int, int>::iterator apt_it = associated_payload_types_.find(
3843 it->plType);
3844 bool valid_apt = false;
3845 if (apt_it != associated_payload_types_.end()) {
3846 std::map<int, webrtc::VideoCodec*>::iterator codec_it =
3847 pt_to_codec.find(apt_it->second);
3848 // We currently only support RTX associated with VP8 due to limitations
3849 // in webrtc where only one RTX payload type can be registered.
3850 valid_apt = codec_it != pt_to_codec.end() &&
3851 _stricmp(codec_it->second->plName, kVp8PayloadName) == 0;
3852 }
3853 if (!valid_apt) {
3854 LOG(LS_ERROR) << "The RTX codec isn't associated with a known and "
3855 "supported payload type";
3856 return false;
3857 }
3858 if (engine()->vie()->rtp()->SetRtxReceivePayloadType(
3859 channel_id, it->plType) != 0) {
3860 LOG_RTCERR2(SetRtxReceivePayloadType, channel_id, it->plType);
3861 return false;
3862 }
3863 continue;
3864 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003865 if (engine()->vie()->codec()->SetReceiveCodec(channel_id, *it) != 0) {
3866 LOG_RTCERR2(SetReceiveCodec, channel_id, it->plName);
3867 return false;
3868 }
3869 if (!info->IsDecoderRegistered(it->plType) &&
3870 it->codecType != webrtc::kVideoCodecRED &&
3871 it->codecType != webrtc::kVideoCodecULPFEC) {
3872 webrtc::VideoDecoder* decoder =
3873 engine()->CreateExternalDecoder(it->codecType);
3874 if (decoder) {
3875 if (engine()->vie()->ext_codec()->RegisterExternalReceiveCodec(
3876 channel_id, it->plType, decoder) == 0) {
3877 info->RegisterDecoder(it->plType, decoder);
3878 } else {
3879 LOG_RTCERR2(RegisterExternalReceiveCodec, channel_id, it->plName);
3880 engine()->DestroyExternalDecoder(decoder);
3881 }
3882 }
3883 }
3884 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003885 return true;
3886}
3887
3888int WebRtcVideoMediaChannel::GetRecvChannelNum(uint32 ssrc) {
3889 if (ssrc == first_receive_ssrc_) {
3890 return vie_channel_;
3891 }
buildbot@webrtc.orgdd4742a2014-05-07 14:50:35 +00003892 int recv_channel = -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003893 RecvChannelMap::iterator it = recv_channels_.find(ssrc);
buildbot@webrtc.orgdd4742a2014-05-07 14:50:35 +00003894 if (it == recv_channels_.end()) {
3895 // Check if we have an RTX stream registered on this SSRC.
3896 SsrcMap::iterator rtx_it = rtx_to_primary_ssrc_.find(ssrc);
3897 if (rtx_it != rtx_to_primary_ssrc_.end()) {
3898 it = recv_channels_.find(rtx_it->second);
3899 assert(it != recv_channels_.end());
3900 recv_channel = it->second->channel_id();
3901 }
3902 } else {
3903 recv_channel = it->second->channel_id();
3904 }
3905 return recv_channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003906}
3907
3908// If the new frame size is different from the send codec size we set on vie,
3909// we need to reset the send codec on vie.
3910// The new send codec size should not exceed send_codec_ which is controlled
3911// only by the 'jec' logic.
buildbot@webrtc.orgd1ae89f2014-05-08 19:19:26 +00003912// TODO(pthatcher): Get rid of this function, so we only ever set up
3913// codecs in a single place.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003914bool WebRtcVideoMediaChannel::MaybeResetVieSendCodec(
3915 WebRtcVideoChannelSendInfo* send_channel,
3916 int new_width,
3917 int new_height,
3918 bool is_screencast,
3919 bool* reset) {
3920 if (reset) {
3921 *reset = false;
3922 }
3923 ASSERT(send_codec_.get() != NULL);
3924
buildbot@webrtc.orgd1ae89f2014-05-08 19:19:26 +00003925 webrtc::VideoCodec target_codec = *send_codec_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003926 const VideoFormat& video_format = send_channel->video_format();
3927 UpdateVideoCodec(video_format, &target_codec);
3928
3929 // Vie send codec size should not exceed target_codec.
3930 int target_width = new_width;
3931 int target_height = new_height;
3932 if (!is_screencast &&
3933 (new_width > target_codec.width || new_height > target_codec.height)) {
3934 target_width = target_codec.width;
3935 target_height = target_codec.height;
3936 }
3937
3938 // Get current vie codec.
3939 webrtc::VideoCodec vie_codec;
3940 const int channel_id = send_channel->channel_id();
3941 if (engine()->vie()->codec()->GetSendCodec(channel_id, vie_codec) != 0) {
3942 LOG_RTCERR1(GetSendCodec, channel_id);
3943 return false;
3944 }
3945 const int cur_width = vie_codec.width;
3946 const int cur_height = vie_codec.height;
3947
3948 // Only reset send codec when there is a size change. Additionally,
3949 // automatic resize needs to be turned off when screencasting and on when
3950 // not screencasting.
3951 // Don't allow automatic resizing for screencasting.
3952 bool automatic_resize = !is_screencast;
3953 // Turn off VP8 frame dropping when screensharing as the current model does
3954 // not work well at low fps.
3955 bool vp8_frame_dropping = !is_screencast;
3956 // Disable denoising for screencasting.
3957 bool enable_denoising =
3958 options_.video_noise_reduction.GetWithDefaultIfUnset(false);
henrike@webrtc.orgdce3feb2014-03-26 01:17:30 +00003959 int screencast_min_bitrate =
3960 options_.screencast_min_bitrate.GetWithDefaultIfUnset(0);
3961 bool leaky_bucket = options_.video_leaky_bucket.GetWithDefaultIfUnset(false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003962 bool denoising = !is_screencast && enable_denoising;
3963 bool reset_send_codec =
3964 target_width != cur_width || target_height != cur_height ||
3965 automatic_resize != vie_codec.codecSpecific.VP8.automaticResizeOn ||
3966 denoising != vie_codec.codecSpecific.VP8.denoisingOn ||
3967 vp8_frame_dropping != vie_codec.codecSpecific.VP8.frameDroppingOn;
3968
3969 if (reset_send_codec) {
3970 // Set the new codec on vie.
3971 vie_codec.width = target_width;
3972 vie_codec.height = target_height;
3973 vie_codec.maxFramerate = target_codec.maxFramerate;
3974 vie_codec.startBitrate = target_codec.startBitrate;
buildbot@webrtc.orgd1ae89f2014-05-08 19:19:26 +00003975 vie_codec.minBitrate = target_codec.minBitrate;
3976 vie_codec.maxBitrate = target_codec.maxBitrate;
henrike@webrtc.org6e3dbc22014-03-25 17:09:47 +00003977 vie_codec.targetBitrate = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003978 vie_codec.codecSpecific.VP8.automaticResizeOn = automatic_resize;
3979 vie_codec.codecSpecific.VP8.denoisingOn = denoising;
3980 vie_codec.codecSpecific.VP8.frameDroppingOn = vp8_frame_dropping;
buildbot@webrtc.orgd1ae89f2014-05-08 19:19:26 +00003981 MaybeChangeBitrates(channel_id, &vie_codec);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003982
3983 if (engine()->vie()->codec()->SetSendCodec(channel_id, vie_codec) != 0) {
3984 LOG_RTCERR1(SetSendCodec, channel_id);
3985 return false;
3986 }
henrike@webrtc.orgdce3feb2014-03-26 01:17:30 +00003987
henrike@webrtc.orgdce3feb2014-03-26 01:17:30 +00003988 if (is_screencast) {
3989 engine()->vie()->rtp()->SetMinTransmitBitrate(channel_id,
3990 screencast_min_bitrate);
3991 // If screencast and min bitrate set, force enable pacer.
3992 if (screencast_min_bitrate > 0) {
3993 engine()->vie()->rtp()->SetTransmissionSmoothingStatus(channel_id,
3994 true);
3995 }
3996 } else {
3997 // In case of switching from screencast to regular capture, set
3998 // min bitrate padding and pacer back to defaults.
3999 engine()->vie()->rtp()->SetMinTransmitBitrate(channel_id, 0);
4000 engine()->vie()->rtp()->SetTransmissionSmoothingStatus(channel_id,
4001 leaky_bucket);
4002 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00004003 if (reset) {
4004 *reset = true;
4005 }
4006 LogSendCodecChange("Capture size changed");
4007 }
4008
4009 return true;
4010}
4011
buildbot@webrtc.orgd1ae89f2014-05-08 19:19:26 +00004012void WebRtcVideoMediaChannel::MaybeChangeBitrates(
4013 int channel_id, webrtc::VideoCodec* codec) {
4014 codec->minBitrate = GetBitrate(codec->minBitrate, kMinVideoBitrate);
4015 codec->startBitrate = GetBitrate(codec->startBitrate, kStartVideoBitrate);
4016 codec->maxBitrate = GetBitrate(codec->maxBitrate, kMaxVideoBitrate);
4017
4018 if (codec->minBitrate > codec->maxBitrate) {
4019 LOG(LS_INFO) << "Decreasing codec min bitrate to the max ("
4020 << codec->maxBitrate << ") because the min ("
4021 << codec->minBitrate << ") exceeds the max.";
4022 codec->minBitrate = codec->maxBitrate;
4023 }
4024 if (codec->startBitrate < codec->minBitrate) {
4025 LOG(LS_INFO) << "Increasing codec start bitrate to the min ("
4026 << codec->minBitrate << ") because the start ("
4027 << codec->startBitrate << ") is less than the min.";
4028 codec->startBitrate = codec->minBitrate;
4029 } else if (codec->startBitrate > codec->maxBitrate) {
4030 LOG(LS_INFO) << "Decreasing codec start bitrate to the max ("
4031 << codec->maxBitrate << ") because the start ("
4032 << codec->startBitrate << ") exceeds the max.";
4033 codec->startBitrate = codec->maxBitrate;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00004034 }
4035
4036 // Use a previous target bitrate, if there is one.
4037 unsigned int current_target_bitrate = 0;
4038 if (engine()->vie()->codec()->GetCodecTargetBitrate(
4039 channel_id, &current_target_bitrate) == 0) {
4040 // Convert to kbps.
4041 current_target_bitrate /= 1000;
buildbot@webrtc.orgd1ae89f2014-05-08 19:19:26 +00004042 if (current_target_bitrate > codec->maxBitrate) {
4043 current_target_bitrate = codec->maxBitrate;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00004044 }
buildbot@webrtc.orgd1ae89f2014-05-08 19:19:26 +00004045 if (current_target_bitrate > codec->startBitrate) {
4046 codec->startBitrate = current_target_bitrate;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00004047 }
4048 }
4049}
4050
4051void WebRtcVideoMediaChannel::OnMessage(talk_base::Message* msg) {
4052 FlushBlackFrameData* black_frame_data =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00004053 static_cast<FlushBlackFrameData*>(msg->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00004054 FlushBlackFrame(black_frame_data->ssrc, black_frame_data->timestamp);
4055 delete black_frame_data;
4056}
4057
4058int WebRtcVideoMediaChannel::SendPacket(int channel, const void* data,
4059 int len) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00004060 talk_base::Buffer packet(data, len, kMaxRtpPacketLen);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00004061 return MediaChannel::SendPacket(&packet) ? len : -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00004062}
4063
4064int WebRtcVideoMediaChannel::SendRTCPPacket(int channel,
4065 const void* data,
4066 int len) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00004067 talk_base::Buffer packet(data, len, kMaxRtpPacketLen);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00004068 return MediaChannel::SendRtcp(&packet) ? len : -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00004069}
4070
4071void WebRtcVideoMediaChannel::QueueBlackFrame(uint32 ssrc, int64 timestamp,
4072 int framerate) {
4073 if (timestamp) {
4074 FlushBlackFrameData* black_frame_data = new FlushBlackFrameData(
4075 ssrc,
4076 timestamp);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00004077 const int delay_ms = static_cast<int>(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00004078 2 * cricket::VideoFormat::FpsToInterval(framerate) *
4079 talk_base::kNumMillisecsPerSec / talk_base::kNumNanosecsPerSec);
4080 worker_thread()->PostDelayed(delay_ms, this, 0, black_frame_data);
4081 }
4082}
4083
4084void WebRtcVideoMediaChannel::FlushBlackFrame(uint32 ssrc, int64 timestamp) {
4085 WebRtcVideoChannelSendInfo* send_channel = GetSendChannel(ssrc);
4086 if (!send_channel) {
4087 return;
4088 }
4089 talk_base::scoped_ptr<const VideoFrame> black_frame_ptr;
4090
4091 const WebRtcLocalStreamInfo* channel_stream_info =
4092 send_channel->local_stream_info();
4093 int64 last_frame_time_stamp = channel_stream_info->time_stamp();
4094 if (last_frame_time_stamp == timestamp) {
4095 size_t last_frame_width = 0;
4096 size_t last_frame_height = 0;
4097 int64 last_frame_elapsed_time = 0;
4098 channel_stream_info->GetLastFrameInfo(&last_frame_width, &last_frame_height,
4099 &last_frame_elapsed_time);
4100 if (!last_frame_width || !last_frame_height) {
4101 return;
4102 }
4103 WebRtcVideoFrame black_frame;
4104 // Black frame is not screencast.
4105 const bool screencasting = false;
4106 const int64 timestamp_delta = send_channel->interval();
4107 if (!black_frame.InitToBlack(send_codec_->width, send_codec_->height, 1, 1,
4108 last_frame_elapsed_time + timestamp_delta,
4109 last_frame_time_stamp + timestamp_delta) ||
4110 !SendFrame(send_channel, &black_frame, screencasting)) {
4111 LOG(LS_ERROR) << "Failed to send black frame.";
4112 }
4113 }
4114}
4115
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00004116void WebRtcVideoMediaChannel::OnCpuAdaptationUnable() {
4117 // ssrc is hardcoded to 0. This message is based on a system wide issue,
4118 // so finding which ssrc caused it doesn't matter.
4119 SignalMediaError(0, VideoMediaChannel::ERROR_REC_CPU_MAX_CANT_DOWNGRADE);
4120}
4121
henrike@webrtc.org28e20752013-07-10 00:45:36 +00004122void WebRtcVideoMediaChannel::SetNetworkTransmissionState(
4123 bool is_transmitting) {
4124 LOG(LS_INFO) << "SetNetworkTransmissionState: " << is_transmitting;
4125 for (SendChannelMap::iterator iter = send_channels_.begin();
4126 iter != send_channels_.end(); ++iter) {
4127 WebRtcVideoChannelSendInfo* send_channel = iter->second;
4128 int channel_id = send_channel->channel_id();
4129 engine_->vie()->network()->SetNetworkTransmissionState(channel_id,
4130 is_transmitting);
4131 }
4132}
4133
4134bool WebRtcVideoMediaChannel::SetHeaderExtension(ExtensionSetterFunction setter,
4135 int channel_id, const RtpHeaderExtension* extension) {
4136 bool enable = false;
4137 int id = 0;
4138 if (extension) {
4139 enable = true;
4140 id = extension->id;
4141 }
4142 if ((engine_->vie()->rtp()->*setter)(channel_id, enable, id) != 0) {
4143 LOG_RTCERR4(*setter, extension->uri, channel_id, enable, id);
4144 return false;
4145 }
4146 return true;
4147}
4148
4149bool WebRtcVideoMediaChannel::SetHeaderExtension(ExtensionSetterFunction setter,
4150 int channel_id, const std::vector<RtpHeaderExtension>& extensions,
4151 const char header_extension_uri[]) {
4152 const RtpHeaderExtension* extension = FindHeaderExtension(extensions,
4153 header_extension_uri);
4154 return SetHeaderExtension(setter, channel_id, extension);
4155}
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00004156
4157bool WebRtcVideoMediaChannel::SetLocalRtxSsrc(int channel_id,
4158 const StreamParams& send_params,
4159 uint32 primary_ssrc,
4160 int stream_idx) {
4161 uint32 rtx_ssrc = 0;
4162 bool has_rtx = send_params.GetFidSsrc(primary_ssrc, &rtx_ssrc);
4163 if (has_rtx && engine()->vie()->rtp()->SetLocalSSRC(
4164 channel_id, rtx_ssrc, webrtc::kViEStreamTypeRtx, stream_idx) != 0) {
4165 LOG_RTCERR4(SetLocalSSRC, channel_id, rtx_ssrc,
4166 webrtc::kViEStreamTypeRtx, stream_idx);
4167 return false;
4168 }
4169 return true;
4170}
4171
wu@webrtc.org24301a62013-12-13 19:17:43 +00004172void WebRtcVideoMediaChannel::MaybeConnectCapturer(VideoCapturer* capturer) {
4173 if (capturer != NULL && GetSendChannelNum(capturer) == 1) {
wu@webrtc.orgf7d501d2014-03-27 23:48:25 +00004174 capturer->SignalVideoFrame.connect(this,
4175 &WebRtcVideoMediaChannel::SendFrame);
wu@webrtc.org24301a62013-12-13 19:17:43 +00004176 }
4177}
4178
4179void WebRtcVideoMediaChannel::MaybeDisconnectCapturer(VideoCapturer* capturer) {
4180 if (capturer != NULL && GetSendChannelNum(capturer) == 1) {
4181 capturer->SignalVideoFrame.disconnect(this);
4182 }
4183}
4184
henrike@webrtc.org28e20752013-07-10 00:45:36 +00004185} // namespace cricket
4186
4187#endif // HAVE_WEBRTC_VIDEO