blob: f538f80d0f9abbd1f08ed0cc45059c3839ca9114 [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"
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +000063#include "webrtc/modules/remote_bitrate_estimator/include/remote_bitrate_estimator.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000064
65#if !defined(LIBPEERCONNECTION_LIB)
66#ifndef HAVE_WEBRTC_VIDEO
67#error Need webrtc video
68#endif
69#include "talk/media/webrtc/webrtcmediaengine.h"
70
71WRME_EXPORT
72cricket::MediaEngineInterface* CreateWebRtcMediaEngine(
73 webrtc::AudioDeviceModule* adm, webrtc::AudioDeviceModule* adm_sc,
74 cricket::WebRtcVideoEncoderFactory* encoder_factory,
75 cricket::WebRtcVideoDecoderFactory* decoder_factory) {
76 return new cricket::WebRtcMediaEngine(adm, adm_sc, encoder_factory,
77 decoder_factory);
78}
79
80WRME_EXPORT
81void DestroyWebRtcMediaEngine(cricket::MediaEngineInterface* media_engine) {
82 delete static_cast<cricket::WebRtcMediaEngine*>(media_engine);
83}
84#endif
85
86
87namespace cricket {
88
89
90static const int kDefaultLogSeverity = talk_base::LS_WARNING;
91
92static const int kMinVideoBitrate = 50;
93static const int kStartVideoBitrate = 300;
94static const int kMaxVideoBitrate = 2000;
95static const int kDefaultConferenceModeMaxVideoBitrate = 500;
96
wu@webrtc.orgcecfd182013-10-30 05:18:12 +000097// Controlled by exp, try a super low minimum bitrate for poor connections.
98static const int kLowerMinBitrate = 30;
99
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000100static const int kVideoMtu = 1200;
101
102static const int kVideoRtpBufferSize = 65536;
103
104static const char kVp8PayloadName[] = "VP8";
105static const char kRedPayloadName[] = "red";
106static const char kFecPayloadName[] = "ulpfec";
107
108static const int kDefaultNumberOfTemporalLayers = 1; // 1:1
109
110static const int kTimestampDeltaInSecondsForWarning = 2;
111
112static const int kMaxExternalVideoCodecs = 8;
113static const int kExternalVideoPayloadTypeBase = 120;
114
115// Static allocation of payload type values for external video codec.
116static int GetExternalVideoPayloadType(int index) {
117 ASSERT(index >= 0 && index < kMaxExternalVideoCodecs);
118 return kExternalVideoPayloadTypeBase + index;
119}
120
121static void LogMultiline(talk_base::LoggingSeverity sev, char* text) {
122 const char* delim = "\r\n";
123 // TODO(fbarchard): Fix strtok lint warning.
124 for (char* tok = strtok(text, delim); tok; tok = strtok(NULL, delim)) {
125 LOG_V(sev) << tok;
126 }
127}
128
129// Severity is an integer because it comes is assumed to be from command line.
130static int SeverityToFilter(int severity) {
131 int filter = webrtc::kTraceNone;
132 switch (severity) {
133 case talk_base::LS_VERBOSE:
134 filter |= webrtc::kTraceAll;
135 case talk_base::LS_INFO:
136 filter |= (webrtc::kTraceStateInfo | webrtc::kTraceInfo);
137 case talk_base::LS_WARNING:
138 filter |= (webrtc::kTraceTerseInfo | webrtc::kTraceWarning);
139 case talk_base::LS_ERROR:
140 filter |= (webrtc::kTraceError | webrtc::kTraceCritical);
141 }
142 return filter;
143}
144
145static const int kCpuMonitorPeriodMs = 2000; // 2 seconds.
146
147static const bool kNotSending = false;
148
wu@webrtc.orgde305012013-10-31 15:40:38 +0000149// Default video dscp value.
150// See http://tools.ietf.org/html/rfc2474 for details
151// See also http://tools.ietf.org/html/draft-jennings-rtcweb-qos-00
152static const talk_base::DiffServCodePoint kVideoDscpValue =
153 talk_base::DSCP_AF41;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000154
155static bool IsNackEnabled(const VideoCodec& codec) {
156 return codec.HasFeedbackParam(FeedbackParam(kRtcpFbParamNack,
157 kParamValueEmpty));
158}
159
160// Returns true if Receiver Estimated Max Bitrate is enabled.
161static bool IsRembEnabled(const VideoCodec& codec) {
162 return codec.HasFeedbackParam(FeedbackParam(kRtcpFbParamRemb,
163 kParamValueEmpty));
164}
165
166struct FlushBlackFrameData : public talk_base::MessageData {
167 FlushBlackFrameData(uint32 s, int64 t) : ssrc(s), timestamp(t) {
168 }
169 uint32 ssrc;
170 int64 timestamp;
171};
172
173class WebRtcRenderAdapter : public webrtc::ExternalRenderer {
174 public:
175 explicit WebRtcRenderAdapter(VideoRenderer* renderer)
176 : renderer_(renderer), width_(0), height_(0), watermark_enabled_(false) {
177 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000178
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000179 virtual ~WebRtcRenderAdapter() {
180 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000181
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000182 void set_watermark_enabled(bool enable) {
183 talk_base::CritScope cs(&crit_);
184 watermark_enabled_ = enable;
185 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000186
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000187 void SetRenderer(VideoRenderer* renderer) {
188 talk_base::CritScope cs(&crit_);
189 renderer_ = renderer;
190 // FrameSizeChange may have already been called when renderer was not set.
191 // If so we should call SetSize here.
192 // TODO(ronghuawu): Add unit test for this case. Didn't do it now
193 // because the WebRtcRenderAdapter is currently hiding in cc file. No
194 // good way to get access to it from the unit test.
195 if (width_ > 0 && height_ > 0 && renderer_ != NULL) {
196 if (!renderer_->SetSize(width_, height_, 0)) {
197 LOG(LS_ERROR)
198 << "WebRtcRenderAdapter SetRenderer failed to SetSize to: "
199 << width_ << "x" << height_;
200 }
201 }
202 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000203
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000204 // Implementation of webrtc::ExternalRenderer.
205 virtual int FrameSizeChange(unsigned int width, unsigned int height,
206 unsigned int /*number_of_streams*/) {
207 talk_base::CritScope cs(&crit_);
208 width_ = width;
209 height_ = height;
210 LOG(LS_INFO) << "WebRtcRenderAdapter frame size changed to: "
211 << width << "x" << height;
212 if (renderer_ == NULL) {
213 LOG(LS_VERBOSE) << "WebRtcRenderAdapter the renderer has not been set. "
214 << "SetSize will be called later in SetRenderer.";
215 return 0;
216 }
217 return renderer_->SetSize(width_, height_, 0) ? 0 : -1;
218 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000219
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000220 virtual int DeliverFrame(unsigned char* buffer, int buffer_size,
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000221 uint32_t time_stamp, int64_t render_time,
222 void* handle) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000223 talk_base::CritScope cs(&crit_);
224 frame_rate_tracker_.Update(1);
225 if (renderer_ == NULL) {
226 return 0;
227 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000228 // Convert 90K rtp timestamp to ns timestamp.
229 int64 rtp_time_stamp_in_ns = (time_stamp / 90) *
230 talk_base::kNumNanosecsPerMillisec;
231 // Convert milisecond render time to ns timestamp.
232 int64 render_time_stamp_in_ns = render_time *
233 talk_base::kNumNanosecsPerMillisec;
234 // Send the rtp timestamp to renderer as the VideoFrame timestamp.
235 // and the render timestamp as the VideoFrame elapsed_time.
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000236 if (handle == NULL) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000237 return DeliverBufferFrame(buffer, buffer_size, render_time_stamp_in_ns,
238 rtp_time_stamp_in_ns);
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000239 } else {
240 return DeliverTextureFrame(handle, render_time_stamp_in_ns,
241 rtp_time_stamp_in_ns);
242 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000243 }
244
245 virtual bool IsTextureSupported() { return true; }
246
247 int DeliverBufferFrame(unsigned char* buffer, int buffer_size,
248 int64 elapsed_time, int64 time_stamp) {
249 WebRtcVideoFrame video_frame;
wu@webrtc.org16d62542013-11-05 23:45:14 +0000250 video_frame.Alias(buffer, buffer_size, width_, height_,
251 1, 1, elapsed_time, time_stamp, 0);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000252
253
254 // Sanity check on decoded frame size.
255 if (buffer_size != static_cast<int>(VideoFrame::SizeOf(width_, height_))) {
256 LOG(LS_WARNING) << "WebRtcRenderAdapter received a strange frame size: "
257 << buffer_size;
258 }
259
260 int ret = renderer_->RenderFrame(&video_frame) ? 0 : -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000261 return ret;
262 }
263
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000264 int DeliverTextureFrame(void* handle, int64 elapsed_time, int64 time_stamp) {
265 WebRtcTextureVideoFrame video_frame(
266 static_cast<webrtc::NativeHandle*>(handle), width_, height_,
267 elapsed_time, time_stamp);
268 return renderer_->RenderFrame(&video_frame);
269 }
270
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000271 unsigned int width() {
272 talk_base::CritScope cs(&crit_);
273 return width_;
274 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000275
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000276 unsigned int height() {
277 talk_base::CritScope cs(&crit_);
278 return height_;
279 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000280
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000281 int framerate() {
282 talk_base::CritScope cs(&crit_);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000283 return static_cast<int>(frame_rate_tracker_.units_second());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000284 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000285
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000286 VideoRenderer* renderer() {
287 talk_base::CritScope cs(&crit_);
288 return renderer_;
289 }
290
291 private:
292 talk_base::CriticalSection crit_;
293 VideoRenderer* renderer_;
294 unsigned int width_;
295 unsigned int height_;
296 talk_base::RateTracker frame_rate_tracker_;
297 bool watermark_enabled_;
298};
299
300class WebRtcDecoderObserver : public webrtc::ViEDecoderObserver {
301 public:
302 explicit WebRtcDecoderObserver(int video_channel)
303 : video_channel_(video_channel),
304 framerate_(0),
305 bitrate_(0),
wu@webrtc.org97077a32013-10-25 21:18:33 +0000306 decode_ms_(0),
307 max_decode_ms_(0),
308 current_delay_ms_(0),
309 target_delay_ms_(0),
310 jitter_buffer_ms_(0),
311 min_playout_delay_ms_(0),
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +0000312 render_delay_ms_(0) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000313 }
314
315 // virtual functions from VieDecoderObserver.
316 virtual void IncomingCodecChanged(const int videoChannel,
317 const webrtc::VideoCodec& videoCodec) {}
318 virtual void IncomingRate(const int videoChannel,
319 const unsigned int framerate,
320 const unsigned int bitrate) {
wu@webrtc.org78187522013-10-07 23:32:02 +0000321 talk_base::CritScope cs(&crit_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000322 ASSERT(video_channel_ == videoChannel);
323 framerate_ = framerate;
324 bitrate_ = bitrate;
325 }
wu@webrtc.org97077a32013-10-25 21:18:33 +0000326
327 virtual void DecoderTiming(int decode_ms,
328 int max_decode_ms,
329 int current_delay_ms,
330 int target_delay_ms,
331 int jitter_buffer_ms,
332 int min_playout_delay_ms,
333 int render_delay_ms) {
334 talk_base::CritScope cs(&crit_);
335 decode_ms_ = decode_ms;
336 max_decode_ms_ = max_decode_ms;
337 current_delay_ms_ = current_delay_ms;
338 target_delay_ms_ = target_delay_ms;
339 jitter_buffer_ms_ = jitter_buffer_ms;
340 min_playout_delay_ms_ = min_playout_delay_ms;
341 render_delay_ms_ = render_delay_ms;
342 }
343
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +0000344 virtual void RequestNewKeyFrame(const int videoChannel) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000345
wu@webrtc.org97077a32013-10-25 21:18:33 +0000346 // Populate |rinfo| based on previously-set data in |*this|.
347 void ExportTo(VideoReceiverInfo* rinfo) {
wu@webrtc.org78187522013-10-07 23:32:02 +0000348 talk_base::CritScope cs(&crit_);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000349 rinfo->framerate_rcvd = framerate_;
350 rinfo->decode_ms = decode_ms_;
351 rinfo->max_decode_ms = max_decode_ms_;
352 rinfo->current_delay_ms = current_delay_ms_;
353 rinfo->target_delay_ms = target_delay_ms_;
354 rinfo->jitter_buffer_ms = jitter_buffer_ms_;
355 rinfo->min_playout_delay_ms = min_playout_delay_ms_;
356 rinfo->render_delay_ms = render_delay_ms_;
wu@webrtc.org78187522013-10-07 23:32:02 +0000357 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000358
359 private:
wu@webrtc.org78187522013-10-07 23:32:02 +0000360 mutable talk_base::CriticalSection crit_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000361 int video_channel_;
362 int framerate_;
363 int bitrate_;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000364 int decode_ms_;
365 int max_decode_ms_;
366 int current_delay_ms_;
367 int target_delay_ms_;
368 int jitter_buffer_ms_;
369 int min_playout_delay_ms_;
370 int render_delay_ms_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000371};
372
373class WebRtcEncoderObserver : public webrtc::ViEEncoderObserver {
374 public:
375 explicit WebRtcEncoderObserver(int video_channel)
376 : video_channel_(video_channel),
377 framerate_(0),
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000378 bitrate_(0),
379 suspended_(false) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000380 }
381
382 // virtual functions from VieEncoderObserver.
383 virtual void OutgoingRate(const int videoChannel,
384 const unsigned int framerate,
385 const unsigned int bitrate) {
wu@webrtc.org78187522013-10-07 23:32:02 +0000386 talk_base::CritScope cs(&crit_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000387 ASSERT(video_channel_ == videoChannel);
388 framerate_ = framerate;
389 bitrate_ = bitrate;
390 }
391
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000392 virtual void SuspendChange(int video_channel, bool is_suspended) {
393 talk_base::CritScope cs(&crit_);
394 ASSERT(video_channel_ == video_channel);
395 suspended_ = is_suspended;
396 }
397
wu@webrtc.org78187522013-10-07 23:32:02 +0000398 int framerate() const {
399 talk_base::CritScope cs(&crit_);
400 return framerate_;
401 }
402 int bitrate() const {
403 talk_base::CritScope cs(&crit_);
404 return bitrate_;
405 }
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000406 bool suspended() const {
407 talk_base::CritScope cs(&crit_);
408 return suspended_;
409 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000410
411 private:
wu@webrtc.org78187522013-10-07 23:32:02 +0000412 mutable talk_base::CriticalSection crit_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000413 int video_channel_;
414 int framerate_;
415 int bitrate_;
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000416 bool suspended_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000417};
418
419class WebRtcLocalStreamInfo {
420 public:
421 WebRtcLocalStreamInfo()
422 : width_(0), height_(0), elapsed_time_(-1), time_stamp_(-1) {}
423 size_t width() const {
424 talk_base::CritScope cs(&crit_);
425 return width_;
426 }
427 size_t height() const {
428 talk_base::CritScope cs(&crit_);
429 return height_;
430 }
431 int64 elapsed_time() const {
432 talk_base::CritScope cs(&crit_);
433 return elapsed_time_;
434 }
435 int64 time_stamp() const {
436 talk_base::CritScope cs(&crit_);
437 return time_stamp_;
438 }
439 int framerate() {
440 talk_base::CritScope cs(&crit_);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000441 return static_cast<int>(rate_tracker_.units_second());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000442 }
443 void GetLastFrameInfo(
444 size_t* width, size_t* height, int64* elapsed_time) const {
445 talk_base::CritScope cs(&crit_);
446 *width = width_;
447 *height = height_;
448 *elapsed_time = elapsed_time_;
449 }
450
451 void UpdateFrame(const VideoFrame* frame) {
452 talk_base::CritScope cs(&crit_);
453
454 width_ = frame->GetWidth();
455 height_ = frame->GetHeight();
456 elapsed_time_ = frame->GetElapsedTime();
457 time_stamp_ = frame->GetTimeStamp();
458
459 rate_tracker_.Update(1);
460 }
461
462 private:
463 mutable talk_base::CriticalSection crit_;
464 size_t width_;
465 size_t height_;
466 int64 elapsed_time_;
467 int64 time_stamp_;
468 talk_base::RateTracker rate_tracker_;
469
470 DISALLOW_COPY_AND_ASSIGN(WebRtcLocalStreamInfo);
471};
472
473// WebRtcVideoChannelRecvInfo is a container class with members such as renderer
474// and a decoder observer that is used by receive channels.
475// It must exist as long as the receive channel is connected to renderer or a
476// decoder observer in this class and methods in the class should only be called
477// from the worker thread.
478class WebRtcVideoChannelRecvInfo {
479 public:
480 typedef std::map<int, webrtc::VideoDecoder*> DecoderMap; // key: payload type
481 explicit WebRtcVideoChannelRecvInfo(int channel_id)
482 : channel_id_(channel_id),
483 render_adapter_(NULL),
484 decoder_observer_(channel_id) {
485 }
486 int channel_id() { return channel_id_; }
487 void SetRenderer(VideoRenderer* renderer) {
488 render_adapter_.SetRenderer(renderer);
489 }
490 WebRtcRenderAdapter* render_adapter() { return &render_adapter_; }
491 WebRtcDecoderObserver* decoder_observer() { return &decoder_observer_; }
492 void RegisterDecoder(int pl_type, webrtc::VideoDecoder* decoder) {
493 ASSERT(!IsDecoderRegistered(pl_type));
494 registered_decoders_[pl_type] = decoder;
495 }
496 bool IsDecoderRegistered(int pl_type) {
497 return registered_decoders_.count(pl_type) != 0;
498 }
499 const DecoderMap& registered_decoders() {
500 return registered_decoders_;
501 }
502 void ClearRegisteredDecoders() {
503 registered_decoders_.clear();
504 }
505
506 private:
507 int channel_id_; // Webrtc video channel number.
508 // Renderer for this channel.
509 WebRtcRenderAdapter render_adapter_;
510 WebRtcDecoderObserver decoder_observer_;
511 DecoderMap registered_decoders_;
512};
513
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000514class WebRtcOveruseObserver : public webrtc::CpuOveruseObserver {
515 public:
516 explicit WebRtcOveruseObserver(CoordinatedVideoAdapter* video_adapter)
517 : video_adapter_(video_adapter),
518 enabled_(false) {
519 }
520
521 // TODO(mflodman): Consider sending resolution as part of event, to let
522 // adapter know what resolution the request is based on. Helps eliminate stale
523 // data, race conditions.
524 virtual void OveruseDetected() OVERRIDE {
525 talk_base::CritScope cs(&crit_);
526 if (!enabled_) {
527 return;
528 }
529
530 video_adapter_->OnCpuResolutionRequest(CoordinatedVideoAdapter::DOWNGRADE);
531 }
532
533 virtual void NormalUsage() OVERRIDE {
534 talk_base::CritScope cs(&crit_);
535 if (!enabled_) {
536 return;
537 }
538
539 video_adapter_->OnCpuResolutionRequest(CoordinatedVideoAdapter::UPGRADE);
540 }
541
542 void Enable(bool enable) {
543 talk_base::CritScope cs(&crit_);
544 enabled_ = enable;
545 }
546
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000547 bool enabled() const { return enabled_; }
548
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000549 private:
550 CoordinatedVideoAdapter* video_adapter_;
551 bool enabled_;
552 talk_base::CriticalSection crit_;
553};
554
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000555
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000556class WebRtcVideoChannelSendInfo : public sigslot::has_slots<> {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000557 public:
558 typedef std::map<int, webrtc::VideoEncoder*> EncoderMap; // key: payload type
559 WebRtcVideoChannelSendInfo(int channel_id, int capture_id,
560 webrtc::ViEExternalCapture* external_capture,
561 talk_base::CpuMonitor* cpu_monitor)
562 : channel_id_(channel_id),
563 capture_id_(capture_id),
564 sending_(false),
565 muted_(false),
566 video_capturer_(NULL),
567 encoder_observer_(channel_id),
568 external_capture_(external_capture),
569 capturer_updated_(false),
570 interval_(0),
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000571 cpu_monitor_(cpu_monitor),
572 overuse_observer_enabled_(false) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000573 }
574
575 int channel_id() const { return channel_id_; }
576 int capture_id() const { return capture_id_; }
577 void set_sending(bool sending) { sending_ = sending; }
578 bool sending() const { return sending_; }
579 void set_muted(bool on) {
580 // TODO(asapersson): add support.
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000581 // video_adapter_.SetBlackOutput(on);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000582 muted_ = on;
583 }
584 bool muted() {return muted_; }
585
586 WebRtcEncoderObserver* encoder_observer() { return &encoder_observer_; }
587 webrtc::ViEExternalCapture* external_capture() { return external_capture_; }
588 const VideoFormat& video_format() const {
589 return video_format_;
590 }
591 void set_video_format(const VideoFormat& video_format) {
592 video_format_ = video_format;
593 if (video_format_ != cricket::VideoFormat()) {
594 interval_ = video_format_.interval;
595 }
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000596 CoordinatedVideoAdapter* adapter = video_adapter();
597 if (adapter) {
598 adapter->OnOutputFormatRequest(video_format_);
599 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000600 }
601 void set_interval(int64 interval) {
602 if (video_format() == cricket::VideoFormat()) {
603 interval_ = interval;
604 }
605 }
606 int64 interval() { return interval_; }
607
xians@webrtc.orgef221512014-02-21 10:31:29 +0000608 int CurrentAdaptReason() const {
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000609 const CoordinatedVideoAdapter* adapter = video_adapter();
610 if (!adapter) {
611 return CoordinatedVideoAdapter::ADAPTREASON_NONE;
612 }
613 return video_adapter()->adapt_reason();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000614 }
615
616 StreamParams* stream_params() { return stream_params_.get(); }
617 void set_stream_params(const StreamParams& sp) {
618 stream_params_.reset(new StreamParams(sp));
619 }
620 void ClearStreamParams() { stream_params_.reset(); }
621 bool has_ssrc(uint32 local_ssrc) const {
622 return !stream_params_ ? false :
623 stream_params_->has_ssrc(local_ssrc);
624 }
625 WebRtcLocalStreamInfo* local_stream_info() {
626 return &local_stream_info_;
627 }
628 VideoCapturer* video_capturer() {
629 return video_capturer_;
630 }
henrike@webrtc.orgc7bec842014-03-12 19:53:43 +0000631 void set_video_capturer(VideoCapturer* video_capturer,
632 ViEWrapper* vie_wrapper) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000633 if (video_capturer == video_capturer_) {
634 return;
635 }
xians@webrtc.orgef221512014-02-21 10:31:29 +0000636
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000637 CoordinatedVideoAdapter* old_video_adapter = video_adapter();
638 if (old_video_adapter) {
639 // Disconnect signals from old video adapter.
640 SignalCpuAdaptationUnable.disconnect(old_video_adapter);
641 if (cpu_monitor_) {
642 cpu_monitor_->SignalUpdate.disconnect(old_video_adapter);
xians@webrtc.orgef221512014-02-21 10:31:29 +0000643 }
henrike@webrtc.org26438052014-02-20 22:32:53 +0000644 }
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000645
646 capturer_updated_ = true;
647 video_capturer_ = video_capturer;
648
henrike@webrtc.orgc7bec842014-03-12 19:53:43 +0000649 vie_wrapper->base()->RegisterCpuOveruseObserver(channel_id_, NULL);
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000650 if (!video_capturer) {
651 overuse_observer_.reset();
652 return;
653 }
654
655 CoordinatedVideoAdapter* adapter = video_adapter();
656 ASSERT(adapter && "Video adapter should not be null here.");
657
658 UpdateAdapterCpuOptions();
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000659
660 overuse_observer_.reset(new WebRtcOveruseObserver(adapter));
henrike@webrtc.orgc7bec842014-03-12 19:53:43 +0000661 vie_wrapper->base()->RegisterCpuOveruseObserver(channel_id_,
662 overuse_observer_.get());
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000663 // (Dis)connect the video adapter from the cpu monitor as appropriate.
664 SetCpuOveruseDetection(overuse_observer_enabled_);
665
666 SignalCpuAdaptationUnable.repeat(adapter->SignalCpuAdaptationUnable);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000667 }
668
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000669 CoordinatedVideoAdapter* video_adapter() {
670 if (!video_capturer_) {
671 return NULL;
672 }
673 return video_capturer_->video_adapter();
674 }
675 const CoordinatedVideoAdapter* video_adapter() const {
676 if (!video_capturer_) {
677 return NULL;
678 }
679 return video_capturer_->video_adapter();
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000680 }
681
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000682 void ApplyCpuOptions(const VideoOptions& video_options) {
683 // Use video_options_.SetAll() instead of assignment so that unset value in
684 // video_options will not overwrite the previous option value.
685 video_options_.SetAll(video_options);
686 UpdateAdapterCpuOptions();
687 }
688
689 void UpdateAdapterCpuOptions() {
690 if (!video_capturer_) {
691 return;
692 }
693
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000694 bool cpu_adapt, cpu_smoothing, adapt_third;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000695 float low, med, high;
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000696
697 // TODO(thorcarpenter): Have VideoAdapter be responsible for setting
698 // all these video options.
699 CoordinatedVideoAdapter* video_adapter = video_capturer_->video_adapter();
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000700 if (video_options_.adapt_input_to_cpu_usage.Get(&cpu_adapt) ||
701 overuse_observer_enabled_) {
702 video_adapter->set_cpu_adaptation(cpu_adapt || overuse_observer_enabled_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000703 }
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000704 if (video_options_.adapt_cpu_with_smoothing.Get(&cpu_smoothing)) {
705 video_adapter->set_cpu_smoothing(cpu_smoothing);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000706 }
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000707 if (video_options_.process_adaptation_threshhold.Get(&med)) {
708 video_adapter->set_process_threshold(med);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000709 }
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000710 if (video_options_.system_low_adaptation_threshhold.Get(&low)) {
711 video_adapter->set_low_system_threshold(low);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000712 }
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000713 if (video_options_.system_high_adaptation_threshhold.Get(&high)) {
714 video_adapter->set_high_system_threshold(high);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000715 }
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000716 if (video_options_.video_adapt_third.Get(&adapt_third)) {
717 video_adapter->set_scale_third(adapt_third);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000718 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000719 }
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000720
721 void SetCpuOveruseDetection(bool enable) {
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000722 overuse_observer_enabled_ = enable;
723
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000724 if (overuse_observer_) {
725 overuse_observer_->Enable(enable);
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000726 }
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000727
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000728 // The video adapter is signaled by overuse detection if enabled; otherwise
729 // it will be signaled by cpu monitor.
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000730 CoordinatedVideoAdapter* adapter = video_adapter();
731 if (adapter) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000732 bool cpu_adapt = false;
733 video_options_.adapt_input_to_cpu_usage.Get(&cpu_adapt);
734 adapter->set_cpu_adaptation(
735 adapter->cpu_adaptation() || cpu_adapt || enable);
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000736 if (cpu_monitor_) {
737 if (enable) {
738 cpu_monitor_->SignalUpdate.disconnect(adapter);
739 } else {
740 cpu_monitor_->SignalUpdate.connect(
741 adapter, &CoordinatedVideoAdapter::OnCpuLoadUpdated);
742 }
743 }
744 }
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000745 }
746
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000747 void ProcessFrame(const VideoFrame& original_frame, bool mute,
748 VideoFrame** processed_frame) {
749 if (!mute) {
750 *processed_frame = original_frame.Copy();
751 } else {
752 WebRtcVideoFrame* black_frame = new WebRtcVideoFrame();
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000753 black_frame->InitToBlack(static_cast<int>(original_frame.GetWidth()),
754 static_cast<int>(original_frame.GetHeight()),
755 1, 1,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000756 original_frame.GetElapsedTime(),
757 original_frame.GetTimeStamp());
758 *processed_frame = black_frame;
759 }
760 local_stream_info_.UpdateFrame(*processed_frame);
761 }
762 void RegisterEncoder(int pl_type, webrtc::VideoEncoder* encoder) {
763 ASSERT(!IsEncoderRegistered(pl_type));
764 registered_encoders_[pl_type] = encoder;
765 }
766 bool IsEncoderRegistered(int pl_type) {
767 return registered_encoders_.count(pl_type) != 0;
768 }
769 const EncoderMap& registered_encoders() {
770 return registered_encoders_;
771 }
772 void ClearRegisteredEncoders() {
773 registered_encoders_.clear();
774 }
775
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000776 sigslot::repeater0<> SignalCpuAdaptationUnable;
777
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000778 private:
779 int channel_id_;
780 int capture_id_;
781 bool sending_;
782 bool muted_;
783 VideoCapturer* video_capturer_;
784 WebRtcEncoderObserver encoder_observer_;
785 webrtc::ViEExternalCapture* external_capture_;
786 EncoderMap registered_encoders_;
787
788 VideoFormat video_format_;
789
790 talk_base::scoped_ptr<StreamParams> stream_params_;
791
792 WebRtcLocalStreamInfo local_stream_info_;
793
794 bool capturer_updated_;
795
796 int64 interval_;
797
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000798 talk_base::CpuMonitor* cpu_monitor_;
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000799 talk_base::scoped_ptr<WebRtcOveruseObserver> overuse_observer_;
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000800 bool overuse_observer_enabled_;
801
802 VideoOptions video_options_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000803};
804
805const WebRtcVideoEngine::VideoCodecPref
806 WebRtcVideoEngine::kVideoCodecPrefs[] = {
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000807 {kVp8PayloadName, 100, -1, 0},
808 {kRedPayloadName, 116, -1, 1},
809 {kFecPayloadName, 117, -1, 2},
810 {kRtxCodecName, 96, 100, 3},
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000811};
812
813// The formats are sorted by the descending order of width. We use the order to
814// find the next format for CPU and bandwidth adaptation.
815const VideoFormatPod WebRtcVideoEngine::kVideoFormats[] = {
816 {1280, 800, FPS_TO_INTERVAL(30), FOURCC_ANY},
817 {1280, 720, FPS_TO_INTERVAL(30), FOURCC_ANY},
818 {960, 600, FPS_TO_INTERVAL(30), FOURCC_ANY},
819 {960, 540, FPS_TO_INTERVAL(30), FOURCC_ANY},
820 {640, 400, FPS_TO_INTERVAL(30), FOURCC_ANY},
821 {640, 360, FPS_TO_INTERVAL(30), FOURCC_ANY},
822 {640, 480, FPS_TO_INTERVAL(30), FOURCC_ANY},
823 {480, 300, FPS_TO_INTERVAL(30), FOURCC_ANY},
824 {480, 270, FPS_TO_INTERVAL(30), FOURCC_ANY},
825 {480, 360, FPS_TO_INTERVAL(30), FOURCC_ANY},
826 {320, 200, FPS_TO_INTERVAL(30), FOURCC_ANY},
827 {320, 180, FPS_TO_INTERVAL(30), FOURCC_ANY},
828 {320, 240, FPS_TO_INTERVAL(30), FOURCC_ANY},
829 {240, 150, FPS_TO_INTERVAL(30), FOURCC_ANY},
830 {240, 135, FPS_TO_INTERVAL(30), FOURCC_ANY},
831 {240, 180, FPS_TO_INTERVAL(30), FOURCC_ANY},
832 {160, 100, FPS_TO_INTERVAL(30), FOURCC_ANY},
833 {160, 90, FPS_TO_INTERVAL(30), FOURCC_ANY},
834 {160, 120, FPS_TO_INTERVAL(30), FOURCC_ANY},
835};
836
837const VideoFormatPod WebRtcVideoEngine::kDefaultVideoFormat =
838 {640, 400, FPS_TO_INTERVAL(30), FOURCC_ANY};
839
840static void UpdateVideoCodec(const cricket::VideoFormat& video_format,
841 webrtc::VideoCodec* target_codec) {
842 if ((target_codec == NULL) || (video_format == cricket::VideoFormat())) {
843 return;
844 }
845 target_codec->width = video_format.width;
846 target_codec->height = video_format.height;
847 target_codec->maxFramerate = cricket::VideoFormat::IntervalToFps(
848 video_format.interval);
849}
850
851WebRtcVideoEngine::WebRtcVideoEngine() {
852 Construct(new ViEWrapper(), new ViETraceWrapper(), NULL,
853 new talk_base::CpuMonitor(NULL));
854}
855
856WebRtcVideoEngine::WebRtcVideoEngine(WebRtcVoiceEngine* voice_engine,
857 ViEWrapper* vie_wrapper,
858 talk_base::CpuMonitor* cpu_monitor) {
859 Construct(vie_wrapper, new ViETraceWrapper(), voice_engine, cpu_monitor);
860}
861
862WebRtcVideoEngine::WebRtcVideoEngine(WebRtcVoiceEngine* voice_engine,
863 ViEWrapper* vie_wrapper,
864 ViETraceWrapper* tracing,
865 talk_base::CpuMonitor* cpu_monitor) {
866 Construct(vie_wrapper, tracing, voice_engine, cpu_monitor);
867}
868
869void WebRtcVideoEngine::Construct(ViEWrapper* vie_wrapper,
870 ViETraceWrapper* tracing,
871 WebRtcVoiceEngine* voice_engine,
872 talk_base::CpuMonitor* cpu_monitor) {
873 LOG(LS_INFO) << "WebRtcVideoEngine::WebRtcVideoEngine";
874 worker_thread_ = NULL;
875 vie_wrapper_.reset(vie_wrapper);
876 vie_wrapper_base_initialized_ = false;
877 tracing_.reset(tracing);
878 voice_engine_ = voice_engine;
879 initialized_ = false;
880 SetTraceFilter(SeverityToFilter(kDefaultLogSeverity));
881 render_module_.reset(new WebRtcPassthroughRender());
882 local_renderer_w_ = local_renderer_h_ = 0;
883 local_renderer_ = NULL;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000884 capture_started_ = false;
885 decoder_factory_ = NULL;
886 encoder_factory_ = NULL;
887 cpu_monitor_.reset(cpu_monitor);
888
889 SetTraceOptions("");
890 if (tracing_->SetTraceCallback(this) != 0) {
891 LOG_RTCERR1(SetTraceCallback, this);
892 }
893
894 // Set default quality levels for our supported codecs. We override them here
895 // if we know your cpu performance is low, and they can be updated explicitly
896 // by calling SetDefaultCodec. For example by a flute preference setting, or
897 // by the server with a jec in response to our reported system info.
898 VideoCodec max_codec(kVideoCodecPrefs[0].payload_type,
899 kVideoCodecPrefs[0].name,
900 kDefaultVideoFormat.width,
901 kDefaultVideoFormat.height,
902 VideoFormat::IntervalToFps(kDefaultVideoFormat.interval),
903 0);
904 if (!SetDefaultCodec(max_codec)) {
905 LOG(LS_ERROR) << "Failed to initialize list of supported codec types";
906 }
907
908
909 // Load our RTP Header extensions.
910 rtp_header_extensions_.push_back(
911 RtpHeaderExtension(kRtpTimestampOffsetHeaderExtension,
henrike@webrtc.org79047f92014-03-06 23:46:59 +0000912 kRtpTimestampOffsetHeaderExtensionDefaultId));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000913 rtp_header_extensions_.push_back(
henrike@webrtc.org79047f92014-03-06 23:46:59 +0000914 RtpHeaderExtension(kRtpAbsoluteSenderTimeHeaderExtension,
915 kRtpAbsoluteSenderTimeHeaderExtensionDefaultId));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000916}
917
918WebRtcVideoEngine::~WebRtcVideoEngine() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000919 LOG(LS_INFO) << "WebRtcVideoEngine::~WebRtcVideoEngine";
920 if (initialized_) {
921 Terminate();
922 }
923 if (encoder_factory_) {
924 encoder_factory_->RemoveObserver(this);
925 }
926 tracing_->SetTraceCallback(NULL);
927 // Test to see if the media processor was deregistered properly.
928 ASSERT(SignalMediaFrame.is_empty());
929}
930
931bool WebRtcVideoEngine::Init(talk_base::Thread* worker_thread) {
932 LOG(LS_INFO) << "WebRtcVideoEngine::Init";
933 worker_thread_ = worker_thread;
934 ASSERT(worker_thread_ != NULL);
935
936 cpu_monitor_->set_thread(worker_thread_);
937 if (!cpu_monitor_->Start(kCpuMonitorPeriodMs)) {
938 LOG(LS_ERROR) << "Failed to start CPU monitor.";
939 cpu_monitor_.reset();
940 }
941
942 bool result = InitVideoEngine();
943 if (result) {
944 LOG(LS_INFO) << "VideoEngine Init done";
945 } else {
946 LOG(LS_ERROR) << "VideoEngine Init failed, releasing";
947 Terminate();
948 }
949 return result;
950}
951
952bool WebRtcVideoEngine::InitVideoEngine() {
953 LOG(LS_INFO) << "WebRtcVideoEngine::InitVideoEngine";
954
955 // Init WebRTC VideoEngine.
956 if (!vie_wrapper_base_initialized_) {
957 if (vie_wrapper_->base()->Init() != 0) {
958 LOG_RTCERR0(Init);
959 return false;
960 }
961 vie_wrapper_base_initialized_ = true;
962 }
963
964 // Log the VoiceEngine version info.
965 char buffer[1024] = "";
966 if (vie_wrapper_->base()->GetVersion(buffer) != 0) {
967 LOG_RTCERR0(GetVersion);
968 return false;
969 }
970
971 LOG(LS_INFO) << "WebRtc VideoEngine Version:";
972 LogMultiline(talk_base::LS_INFO, buffer);
973
974 // Hook up to VoiceEngine for sync purposes, if supplied.
975 if (!voice_engine_) {
976 LOG(LS_WARNING) << "NULL voice engine";
977 } else if ((vie_wrapper_->base()->SetVoiceEngine(
978 voice_engine_->voe()->engine())) != 0) {
979 LOG_RTCERR0(SetVoiceEngine);
980 return false;
981 }
982
983 // Register our custom render module.
984 if (vie_wrapper_->render()->RegisterVideoRenderModule(
985 *render_module_.get()) != 0) {
986 LOG_RTCERR0(RegisterVideoRenderModule);
987 return false;
988 }
989
990 initialized_ = true;
991 return true;
992}
993
994void WebRtcVideoEngine::Terminate() {
995 LOG(LS_INFO) << "WebRtcVideoEngine::Terminate";
996 initialized_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000997
998 if (vie_wrapper_->render()->DeRegisterVideoRenderModule(
999 *render_module_.get()) != 0) {
1000 LOG_RTCERR0(DeRegisterVideoRenderModule);
1001 }
1002
1003 if (vie_wrapper_->base()->SetVoiceEngine(NULL) != 0) {
1004 LOG_RTCERR0(SetVoiceEngine);
1005 }
1006
1007 cpu_monitor_->Stop();
1008}
1009
1010int WebRtcVideoEngine::GetCapabilities() {
1011 return VIDEO_RECV | VIDEO_SEND;
1012}
1013
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00001014bool WebRtcVideoEngine::SetOptions(const VideoOptions &options) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001015 return true;
1016}
1017
1018bool WebRtcVideoEngine::SetDefaultEncoderConfig(
1019 const VideoEncoderConfig& config) {
1020 return SetDefaultCodec(config.max_codec);
1021}
1022
wu@webrtc.org78187522013-10-07 23:32:02 +00001023VideoEncoderConfig WebRtcVideoEngine::GetDefaultEncoderConfig() const {
1024 ASSERT(!video_codecs_.empty());
1025 VideoCodec max_codec(kVideoCodecPrefs[0].payload_type,
1026 kVideoCodecPrefs[0].name,
1027 video_codecs_[0].width,
1028 video_codecs_[0].height,
1029 video_codecs_[0].framerate,
1030 0);
1031 return VideoEncoderConfig(max_codec);
1032}
1033
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001034// SetDefaultCodec may be called while the capturer is running. For example, a
1035// test call is started in a page with QVGA default codec, and then a real call
1036// is started in another page with VGA default codec. This is the corner case
1037// and happens only when a session is started. We ignore this case currently.
1038bool WebRtcVideoEngine::SetDefaultCodec(const VideoCodec& codec) {
1039 if (!RebuildCodecList(codec)) {
1040 LOG(LS_WARNING) << "Failed to RebuildCodecList";
1041 return false;
1042 }
1043
wu@webrtc.org78187522013-10-07 23:32:02 +00001044 ASSERT(!video_codecs_.empty());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001045 default_codec_format_ = VideoFormat(
1046 video_codecs_[0].width,
1047 video_codecs_[0].height,
1048 VideoFormat::FpsToInterval(video_codecs_[0].framerate),
1049 FOURCC_ANY);
1050 return true;
1051}
1052
1053WebRtcVideoMediaChannel* WebRtcVideoEngine::CreateChannel(
1054 VoiceMediaChannel* voice_channel) {
1055 WebRtcVideoMediaChannel* channel =
1056 new WebRtcVideoMediaChannel(this, voice_channel);
1057 if (!channel->Init()) {
1058 delete channel;
1059 channel = NULL;
1060 }
1061 return channel;
1062}
1063
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001064bool WebRtcVideoEngine::SetLocalRenderer(VideoRenderer* renderer) {
1065 local_renderer_w_ = local_renderer_h_ = 0;
1066 local_renderer_ = renderer;
1067 return true;
1068}
1069
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001070const std::vector<VideoCodec>& WebRtcVideoEngine::codecs() const {
1071 return video_codecs_;
1072}
1073
1074const std::vector<RtpHeaderExtension>&
1075WebRtcVideoEngine::rtp_header_extensions() const {
1076 return rtp_header_extensions_;
1077}
1078
1079void WebRtcVideoEngine::SetLogging(int min_sev, const char* filter) {
1080 // if min_sev == -1, we keep the current log level.
1081 if (min_sev >= 0) {
1082 SetTraceFilter(SeverityToFilter(min_sev));
1083 }
1084 SetTraceOptions(filter);
1085}
1086
1087int WebRtcVideoEngine::GetLastEngineError() {
1088 return vie_wrapper_->error();
1089}
1090
1091// Checks to see whether we comprehend and could receive a particular codec
1092bool WebRtcVideoEngine::FindCodec(const VideoCodec& in) {
1093 for (int i = 0; i < ARRAY_SIZE(kVideoFormats); ++i) {
1094 const VideoFormat fmt(kVideoFormats[i]);
1095 if ((in.width == 0 && in.height == 0) ||
1096 (fmt.width == in.width && fmt.height == in.height)) {
1097 if (encoder_factory_) {
1098 const std::vector<WebRtcVideoEncoderFactory::VideoCodec>& codecs =
1099 encoder_factory_->codecs();
1100 for (size_t j = 0; j < codecs.size(); ++j) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001101 VideoCodec codec(GetExternalVideoPayloadType(static_cast<int>(j)),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001102 codecs[j].name, 0, 0, 0, 0);
1103 if (codec.Matches(in))
1104 return true;
1105 }
1106 }
1107 for (size_t j = 0; j < ARRAY_SIZE(kVideoCodecPrefs); ++j) {
1108 VideoCodec codec(kVideoCodecPrefs[j].payload_type,
1109 kVideoCodecPrefs[j].name, 0, 0, 0, 0);
1110 if (codec.Matches(in)) {
1111 return true;
1112 }
1113 }
1114 }
1115 }
1116 return false;
1117}
1118
1119// Given the requested codec, returns true if we can send that codec type and
1120// updates out with the best quality we could send for that codec. If current is
1121// not empty, we constrain out so that its aspect ratio matches current's.
1122bool WebRtcVideoEngine::CanSendCodec(const VideoCodec& requested,
1123 const VideoCodec& current,
1124 VideoCodec* out) {
1125 if (!out) {
1126 return false;
1127 }
1128
1129 std::vector<VideoCodec>::const_iterator local_max;
1130 for (local_max = video_codecs_.begin();
1131 local_max < video_codecs_.end();
1132 ++local_max) {
1133 // First match codecs by payload type
1134 if (!requested.Matches(*local_max)) {
1135 continue;
1136 }
1137
1138 out->id = requested.id;
1139 out->name = requested.name;
1140 out->preference = requested.preference;
1141 out->params = requested.params;
1142 out->framerate = talk_base::_min(requested.framerate, local_max->framerate);
1143 out->width = 0;
1144 out->height = 0;
1145 out->params = requested.params;
1146 out->feedback_params = requested.feedback_params;
1147
1148 if (0 == requested.width && 0 == requested.height) {
1149 // Special case with resolution 0. The channel should not send frames.
1150 return true;
1151 } else if (0 == requested.width || 0 == requested.height) {
1152 // 0xn and nx0 are invalid resolutions.
1153 return false;
1154 }
1155
1156 // Pick the best quality that is within their and our bounds and has the
1157 // correct aspect ratio.
1158 for (int j = 0; j < ARRAY_SIZE(kVideoFormats); ++j) {
1159 const VideoFormat format(kVideoFormats[j]);
1160
1161 // Skip any format that is larger than the local or remote maximums, or
1162 // smaller than the current best match
1163 if (format.width > requested.width || format.height > requested.height ||
1164 format.width > local_max->width ||
1165 (format.width < out->width && format.height < out->height)) {
1166 continue;
1167 }
1168
1169 bool better = false;
1170
1171 // Check any further constraints on this prospective format
1172 if (!out->width || !out->height) {
1173 // If we don't have any matches yet, this is the best so far.
1174 better = true;
1175 } else if (current.width && current.height) {
1176 // current is set so format must match its ratio exactly.
1177 better =
1178 (format.width * current.height == format.height * current.width);
1179 } else {
1180 // Prefer closer aspect ratios i.e
1181 // format.aspect - requested.aspect < out.aspect - requested.aspect
1182 better = abs(format.width * requested.height * out->height -
1183 requested.width * format.height * out->height) <
1184 abs(out->width * format.height * requested.height -
1185 requested.width * format.height * out->height);
1186 }
1187
1188 if (better) {
1189 out->width = format.width;
1190 out->height = format.height;
1191 }
1192 }
1193 if (out->width > 0) {
1194 return true;
1195 }
1196 }
1197 return false;
1198}
1199
1200static void ConvertToCricketVideoCodec(
1201 const webrtc::VideoCodec& in_codec, VideoCodec* out_codec) {
1202 out_codec->id = in_codec.plType;
1203 out_codec->name = in_codec.plName;
1204 out_codec->width = in_codec.width;
1205 out_codec->height = in_codec.height;
1206 out_codec->framerate = in_codec.maxFramerate;
1207 out_codec->SetParam(kCodecParamMinBitrate, in_codec.minBitrate);
1208 out_codec->SetParam(kCodecParamMaxBitrate, in_codec.maxBitrate);
1209 if (in_codec.qpMax) {
1210 out_codec->SetParam(kCodecParamMaxQuantization, in_codec.qpMax);
1211 }
1212}
1213
1214bool WebRtcVideoEngine::ConvertFromCricketVideoCodec(
1215 const VideoCodec& in_codec, webrtc::VideoCodec* out_codec) {
1216 bool found = false;
1217 int ncodecs = vie_wrapper_->codec()->NumberOfCodecs();
1218 for (int i = 0; i < ncodecs; ++i) {
1219 if (vie_wrapper_->codec()->GetCodec(i, *out_codec) == 0 &&
1220 _stricmp(in_codec.name.c_str(), out_codec->plName) == 0) {
1221 found = true;
1222 break;
1223 }
1224 }
1225
1226 // If not found, check if this is supported by external encoder factory.
1227 if (!found && encoder_factory_) {
1228 const std::vector<WebRtcVideoEncoderFactory::VideoCodec>& codecs =
1229 encoder_factory_->codecs();
1230 for (size_t i = 0; i < codecs.size(); ++i) {
1231 if (_stricmp(in_codec.name.c_str(), codecs[i].name.c_str()) == 0) {
1232 out_codec->codecType = codecs[i].type;
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001233 out_codec->plType = GetExternalVideoPayloadType(static_cast<int>(i));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001234 talk_base::strcpyn(out_codec->plName, sizeof(out_codec->plName),
1235 codecs[i].name.c_str(), codecs[i].name.length());
1236 found = true;
1237 break;
1238 }
1239 }
1240 }
1241
1242 if (!found) {
1243 LOG(LS_ERROR) << "invalid codec type";
1244 return false;
1245 }
1246
1247 if (in_codec.id != 0)
1248 out_codec->plType = in_codec.id;
1249
1250 if (in_codec.width != 0)
1251 out_codec->width = in_codec.width;
1252
1253 if (in_codec.height != 0)
1254 out_codec->height = in_codec.height;
1255
1256 if (in_codec.framerate != 0)
1257 out_codec->maxFramerate = in_codec.framerate;
1258
1259 // Convert bitrate parameters.
1260 int max_bitrate = kMaxVideoBitrate;
1261 int min_bitrate = kMinVideoBitrate;
1262 int start_bitrate = kStartVideoBitrate;
1263
1264 in_codec.GetParam(kCodecParamMinBitrate, &min_bitrate);
1265 in_codec.GetParam(kCodecParamMaxBitrate, &max_bitrate);
1266
1267 if (max_bitrate < min_bitrate) {
1268 return false;
1269 }
1270 start_bitrate = talk_base::_max(start_bitrate, min_bitrate);
1271 start_bitrate = talk_base::_min(start_bitrate, max_bitrate);
1272
1273 out_codec->minBitrate = min_bitrate;
1274 out_codec->startBitrate = start_bitrate;
1275 out_codec->maxBitrate = max_bitrate;
1276
1277 // Convert general codec parameters.
1278 int max_quantization = 0;
1279 if (in_codec.GetParam(kCodecParamMaxQuantization, &max_quantization)) {
1280 if (max_quantization < 0) {
1281 return false;
1282 }
1283 out_codec->qpMax = max_quantization;
1284 }
1285 return true;
1286}
1287
1288void WebRtcVideoEngine::RegisterChannel(WebRtcVideoMediaChannel *channel) {
1289 talk_base::CritScope cs(&channels_crit_);
1290 channels_.push_back(channel);
1291}
1292
1293void WebRtcVideoEngine::UnregisterChannel(WebRtcVideoMediaChannel *channel) {
1294 talk_base::CritScope cs(&channels_crit_);
1295 channels_.erase(std::remove(channels_.begin(), channels_.end(), channel),
1296 channels_.end());
1297}
1298
1299bool WebRtcVideoEngine::SetVoiceEngine(WebRtcVoiceEngine* voice_engine) {
1300 if (initialized_) {
1301 LOG(LS_WARNING) << "SetVoiceEngine can not be called after Init";
1302 return false;
1303 }
1304 voice_engine_ = voice_engine;
1305 return true;
1306}
1307
1308bool WebRtcVideoEngine::EnableTimedRender() {
1309 if (initialized_) {
1310 LOG(LS_WARNING) << "EnableTimedRender can not be called after Init";
1311 return false;
1312 }
1313 render_module_.reset(webrtc::VideoRender::CreateVideoRender(0, NULL,
1314 false, webrtc::kRenderExternal));
1315 return true;
1316}
1317
1318void WebRtcVideoEngine::SetTraceFilter(int filter) {
1319 tracing_->SetTraceFilter(filter);
1320}
1321
1322// See https://sites.google.com/a/google.com/wavelet/
1323// Home/Magic-Flute--RTC-Engine-/Magic-Flute-Command-Line-Parameters
1324// for all supported command line setttings.
1325void WebRtcVideoEngine::SetTraceOptions(const std::string& options) {
1326 // Set WebRTC trace file.
1327 std::vector<std::string> opts;
1328 talk_base::tokenize(options, ' ', '"', '"', &opts);
1329 std::vector<std::string>::iterator tracefile =
1330 std::find(opts.begin(), opts.end(), "tracefile");
1331 if (tracefile != opts.end() && ++tracefile != opts.end()) {
1332 // Write WebRTC debug output (at same loglevel) to file
1333 if (tracing_->SetTraceFile(tracefile->c_str()) == -1) {
1334 LOG_RTCERR1(SetTraceFile, *tracefile);
1335 }
1336 }
1337}
1338
1339static void AddDefaultFeedbackParams(VideoCodec* codec) {
1340 const FeedbackParam kFir(kRtcpFbParamCcm, kRtcpFbCcmParamFir);
1341 codec->AddFeedbackParam(kFir);
1342 const FeedbackParam kNack(kRtcpFbParamNack, kParamValueEmpty);
1343 codec->AddFeedbackParam(kNack);
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001344 const FeedbackParam kPli(kRtcpFbParamNack, kRtcpFbNackParamPli);
1345 codec->AddFeedbackParam(kPli);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001346 const FeedbackParam kRemb(kRtcpFbParamRemb, kParamValueEmpty);
1347 codec->AddFeedbackParam(kRemb);
1348}
1349
1350// Rebuilds the codec list to be only those that are less intensive
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00001351// than the specified codec. Prefers internal codec over external with
1352// higher preference field.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001353bool WebRtcVideoEngine::RebuildCodecList(const VideoCodec& in_codec) {
1354 if (!FindCodec(in_codec))
1355 return false;
1356
1357 video_codecs_.clear();
1358
1359 bool found = false;
mallinath@webrtc.org1112c302013-09-23 20:34:45 +00001360 std::set<std::string> internal_codec_names;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001361 for (size_t i = 0; i < ARRAY_SIZE(kVideoCodecPrefs); ++i) {
1362 const VideoCodecPref& pref(kVideoCodecPrefs[i]);
1363 if (!found)
1364 found = (in_codec.name == pref.name);
mallinath@webrtc.org1112c302013-09-23 20:34:45 +00001365 if (found) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001366 VideoCodec codec(pref.payload_type, pref.name,
1367 in_codec.width, in_codec.height, in_codec.framerate,
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001368 static_cast<int>(ARRAY_SIZE(kVideoCodecPrefs) - i));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001369 if (_stricmp(kVp8PayloadName, codec.name.c_str()) == 0) {
1370 AddDefaultFeedbackParams(&codec);
1371 }
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00001372 if (pref.associated_payload_type != -1) {
1373 codec.SetParam(kCodecParamAssociatedPayloadType,
1374 pref.associated_payload_type);
1375 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001376 video_codecs_.push_back(codec);
mallinath@webrtc.org1112c302013-09-23 20:34:45 +00001377 internal_codec_names.insert(codec.name);
1378 }
1379 }
1380 if (encoder_factory_) {
1381 const std::vector<WebRtcVideoEncoderFactory::VideoCodec>& codecs =
1382 encoder_factory_->codecs();
1383 for (size_t i = 0; i < codecs.size(); ++i) {
1384 bool is_internal_codec = internal_codec_names.find(codecs[i].name) !=
1385 internal_codec_names.end();
1386 if (!is_internal_codec) {
1387 if (!found)
1388 found = (in_codec.name == codecs[i].name);
1389 VideoCodec codec(
1390 GetExternalVideoPayloadType(static_cast<int>(i)),
1391 codecs[i].name,
1392 codecs[i].max_width,
1393 codecs[i].max_height,
1394 codecs[i].max_fps,
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00001395 // Use negative preference on external codec to ensure the internal
1396 // codec is preferred.
1397 static_cast<int>(0 - i));
mallinath@webrtc.org1112c302013-09-23 20:34:45 +00001398 AddDefaultFeedbackParams(&codec);
1399 video_codecs_.push_back(codec);
1400 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001401 }
1402 }
1403 ASSERT(found);
1404 return true;
1405}
1406
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001407// Ignore spammy trace messages, mostly from the stats API when we haven't
1408// gotten RTCP info yet from the remote side.
1409bool WebRtcVideoEngine::ShouldIgnoreTrace(const std::string& trace) {
1410 static const char* const kTracesToIgnore[] = {
1411 NULL
1412 };
1413 for (const char* const* p = kTracesToIgnore; *p; ++p) {
1414 if (trace.find(*p) == 0) {
1415 return true;
1416 }
1417 }
1418 return false;
1419}
1420
1421int WebRtcVideoEngine::GetNumOfChannels() {
1422 talk_base::CritScope cs(&channels_crit_);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001423 return static_cast<int>(channels_.size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001424}
1425
1426void WebRtcVideoEngine::Print(webrtc::TraceLevel level, const char* trace,
1427 int length) {
1428 talk_base::LoggingSeverity sev = talk_base::LS_VERBOSE;
1429 if (level == webrtc::kTraceError || level == webrtc::kTraceCritical)
1430 sev = talk_base::LS_ERROR;
1431 else if (level == webrtc::kTraceWarning)
1432 sev = talk_base::LS_WARNING;
1433 else if (level == webrtc::kTraceStateInfo || level == webrtc::kTraceInfo)
1434 sev = talk_base::LS_INFO;
1435 else if (level == webrtc::kTraceTerseInfo)
1436 sev = talk_base::LS_INFO;
1437
1438 // Skip past boilerplate prefix text
1439 if (length < 72) {
1440 std::string msg(trace, length);
1441 LOG(LS_ERROR) << "Malformed webrtc log message: ";
1442 LOG_V(sev) << msg;
1443 } else {
1444 std::string msg(trace + 71, length - 72);
1445 if (!ShouldIgnoreTrace(msg) &&
1446 (!voice_engine_ || !voice_engine_->ShouldIgnoreTrace(msg))) {
1447 LOG_V(sev) << "webrtc: " << msg;
1448 }
1449 }
1450}
1451
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001452webrtc::VideoDecoder* WebRtcVideoEngine::CreateExternalDecoder(
1453 webrtc::VideoCodecType type) {
1454 if (decoder_factory_ == NULL) {
1455 return NULL;
1456 }
1457 return decoder_factory_->CreateVideoDecoder(type);
1458}
1459
1460void WebRtcVideoEngine::DestroyExternalDecoder(webrtc::VideoDecoder* decoder) {
1461 ASSERT(decoder_factory_ != NULL);
1462 if (decoder_factory_ == NULL)
1463 return;
1464 decoder_factory_->DestroyVideoDecoder(decoder);
1465}
1466
1467webrtc::VideoEncoder* WebRtcVideoEngine::CreateExternalEncoder(
1468 webrtc::VideoCodecType type) {
1469 if (encoder_factory_ == NULL) {
1470 return NULL;
1471 }
1472 return encoder_factory_->CreateVideoEncoder(type);
1473}
1474
1475void WebRtcVideoEngine::DestroyExternalEncoder(webrtc::VideoEncoder* encoder) {
1476 ASSERT(encoder_factory_ != NULL);
1477 if (encoder_factory_ == NULL)
1478 return;
1479 encoder_factory_->DestroyVideoEncoder(encoder);
1480}
1481
1482bool WebRtcVideoEngine::IsExternalEncoderCodecType(
1483 webrtc::VideoCodecType type) const {
1484 if (!encoder_factory_)
1485 return false;
1486 const std::vector<WebRtcVideoEncoderFactory::VideoCodec>& codecs =
1487 encoder_factory_->codecs();
1488 std::vector<WebRtcVideoEncoderFactory::VideoCodec>::const_iterator it;
1489 for (it = codecs.begin(); it != codecs.end(); ++it) {
1490 if (it->type == type)
1491 return true;
1492 }
1493 return false;
1494}
1495
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001496void WebRtcVideoEngine::SetExternalDecoderFactory(
1497 WebRtcVideoDecoderFactory* decoder_factory) {
1498 decoder_factory_ = decoder_factory;
1499}
1500
1501void WebRtcVideoEngine::SetExternalEncoderFactory(
1502 WebRtcVideoEncoderFactory* encoder_factory) {
1503 if (encoder_factory_ == encoder_factory)
1504 return;
1505
1506 if (encoder_factory_) {
1507 encoder_factory_->RemoveObserver(this);
1508 }
1509 encoder_factory_ = encoder_factory;
1510 if (encoder_factory_) {
1511 encoder_factory_->AddObserver(this);
1512 }
1513
1514 // Invoke OnCodecAvailable() here in case the list of codecs is already
1515 // available when the encoder factory is installed. If not the encoder
1516 // factory will invoke the callback later when the codecs become available.
1517 OnCodecsAvailable();
1518}
1519
1520void WebRtcVideoEngine::OnCodecsAvailable() {
1521 // Rebuild codec list while reapplying the current default codec format.
1522 VideoCodec max_codec(kVideoCodecPrefs[0].payload_type,
1523 kVideoCodecPrefs[0].name,
1524 video_codecs_[0].width,
1525 video_codecs_[0].height,
1526 video_codecs_[0].framerate,
1527 0);
1528 if (!RebuildCodecList(max_codec)) {
1529 LOG(LS_ERROR) << "Failed to initialize list of supported codec types";
1530 }
1531}
1532
1533// WebRtcVideoMediaChannel
1534
1535WebRtcVideoMediaChannel::WebRtcVideoMediaChannel(
1536 WebRtcVideoEngine* engine,
1537 VoiceMediaChannel* channel)
1538 : engine_(engine),
1539 voice_channel_(channel),
1540 vie_channel_(-1),
1541 nack_enabled_(true),
1542 remb_enabled_(false),
1543 render_started_(false),
1544 first_receive_ssrc_(0),
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001545 num_unsignalled_recv_channels_(0),
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00001546 send_rtx_type_(-1),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001547 send_red_type_(-1),
1548 send_fec_type_(-1),
1549 send_min_bitrate_(kMinVideoBitrate),
1550 send_start_bitrate_(kStartVideoBitrate),
1551 send_max_bitrate_(kMaxVideoBitrate),
1552 sending_(false),
1553 ratio_w_(0),
1554 ratio_h_(0) {
1555 engine->RegisterChannel(this);
1556}
1557
1558bool WebRtcVideoMediaChannel::Init() {
1559 const uint32 ssrc_key = 0;
1560 return CreateChannel(ssrc_key, MD_SENDRECV, &vie_channel_);
1561}
1562
1563WebRtcVideoMediaChannel::~WebRtcVideoMediaChannel() {
1564 const bool send = false;
1565 SetSend(send);
1566 const bool render = false;
1567 SetRender(render);
1568
1569 while (!send_channels_.empty()) {
1570 if (!DeleteSendChannel(send_channels_.begin()->first)) {
1571 LOG(LS_ERROR) << "Unable to delete channel with ssrc key "
1572 << send_channels_.begin()->first;
1573 ASSERT(false);
1574 break;
1575 }
1576 }
1577
1578 // Remove all receive streams and the default channel.
1579 while (!recv_channels_.empty()) {
1580 RemoveRecvStream(recv_channels_.begin()->first);
1581 }
1582
1583 // Unregister the channel from the engine.
1584 engine()->UnregisterChannel(this);
1585 if (worker_thread()) {
1586 worker_thread()->Clear(this);
1587 }
1588}
1589
1590bool WebRtcVideoMediaChannel::SetRecvCodecs(
1591 const std::vector<VideoCodec>& codecs) {
1592 receive_codecs_.clear();
1593 for (std::vector<VideoCodec>::const_iterator iter = codecs.begin();
1594 iter != codecs.end(); ++iter) {
1595 if (engine()->FindCodec(*iter)) {
1596 webrtc::VideoCodec wcodec;
1597 if (engine()->ConvertFromCricketVideoCodec(*iter, &wcodec)) {
1598 receive_codecs_.push_back(wcodec);
1599 }
1600 } else {
1601 LOG(LS_INFO) << "Unknown codec " << iter->name;
1602 return false;
1603 }
1604 }
1605
1606 for (RecvChannelMap::iterator it = recv_channels_.begin();
1607 it != recv_channels_.end(); ++it) {
1608 if (!SetReceiveCodecs(it->second))
1609 return false;
1610 }
1611 return true;
1612}
1613
1614bool WebRtcVideoMediaChannel::SetSendCodecs(
1615 const std::vector<VideoCodec>& codecs) {
1616 // Match with local video codec list.
1617 std::vector<webrtc::VideoCodec> send_codecs;
1618 VideoCodec checked_codec;
1619 VideoCodec current; // defaults to 0x0
1620 if (sending_) {
1621 ConvertToCricketVideoCodec(*send_codec_, &current);
1622 }
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00001623 std::map<int, int> primary_rtx_pt_mapping;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001624 for (std::vector<VideoCodec>::const_iterator iter = codecs.begin();
1625 iter != codecs.end(); ++iter) {
1626 if (_stricmp(iter->name.c_str(), kRedPayloadName) == 0) {
1627 send_red_type_ = iter->id;
1628 } else if (_stricmp(iter->name.c_str(), kFecPayloadName) == 0) {
1629 send_fec_type_ = iter->id;
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00001630 } else if (_stricmp(iter->name.c_str(), kRtxCodecName) == 0) {
1631 int rtx_type = iter->id;
1632 int rtx_primary_type = -1;
1633 if (iter->GetParam(kCodecParamAssociatedPayloadType, &rtx_primary_type)) {
1634 primary_rtx_pt_mapping[rtx_primary_type] = rtx_type;
1635 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001636 } else if (engine()->CanSendCodec(*iter, current, &checked_codec)) {
1637 webrtc::VideoCodec wcodec;
1638 if (engine()->ConvertFromCricketVideoCodec(checked_codec, &wcodec)) {
1639 if (send_codecs.empty()) {
1640 nack_enabled_ = IsNackEnabled(checked_codec);
1641 remb_enabled_ = IsRembEnabled(checked_codec);
1642 }
1643 send_codecs.push_back(wcodec);
1644 }
1645 } else {
1646 LOG(LS_WARNING) << "Unknown codec " << iter->name;
1647 }
1648 }
1649
1650 // Fail if we don't have a match.
1651 if (send_codecs.empty()) {
1652 LOG(LS_WARNING) << "No matching codecs available";
1653 return false;
1654 }
1655
1656 // Recv protection.
1657 for (RecvChannelMap::iterator it = recv_channels_.begin();
1658 it != recv_channels_.end(); ++it) {
1659 int channel_id = it->second->channel_id();
1660 if (!SetNackFec(channel_id, send_red_type_, send_fec_type_,
1661 nack_enabled_)) {
1662 return false;
1663 }
1664 if (engine_->vie()->rtp()->SetRembStatus(channel_id,
1665 kNotSending,
1666 remb_enabled_) != 0) {
1667 LOG_RTCERR3(SetRembStatus, channel_id, kNotSending, remb_enabled_);
1668 return false;
1669 }
1670 }
1671
1672 // Send settings.
1673 for (SendChannelMap::iterator iter = send_channels_.begin();
1674 iter != send_channels_.end(); ++iter) {
1675 int channel_id = iter->second->channel_id();
1676 if (!SetNackFec(channel_id, send_red_type_, send_fec_type_,
1677 nack_enabled_)) {
1678 return false;
1679 }
1680 if (engine_->vie()->rtp()->SetRembStatus(channel_id,
1681 remb_enabled_,
1682 remb_enabled_) != 0) {
1683 LOG_RTCERR3(SetRembStatus, channel_id, remb_enabled_, remb_enabled_);
1684 return false;
1685 }
1686 }
1687
1688 // Select the first matched codec.
1689 webrtc::VideoCodec& codec(send_codecs[0]);
1690
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00001691 // Set RTX payload type if primary now active. This value will be used in
1692 // SetSendCodec.
1693 std::map<int, int>::const_iterator rtx_it =
1694 primary_rtx_pt_mapping.find(static_cast<int>(codec.plType));
1695 if (rtx_it != primary_rtx_pt_mapping.end()) {
1696 send_rtx_type_ = rtx_it->second;
1697 }
1698
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001699 if (!SetSendCodec(
1700 codec, codec.minBitrate, codec.startBitrate, codec.maxBitrate)) {
1701 return false;
1702 }
1703
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001704 LogSendCodecChange("SetSendCodecs()");
1705
1706 return true;
1707}
1708
1709bool WebRtcVideoMediaChannel::GetSendCodec(VideoCodec* send_codec) {
1710 if (!send_codec_) {
1711 return false;
1712 }
1713 ConvertToCricketVideoCodec(*send_codec_, send_codec);
1714 return true;
1715}
1716
1717bool WebRtcVideoMediaChannel::SetSendStreamFormat(uint32 ssrc,
1718 const VideoFormat& format) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001719 WebRtcVideoChannelSendInfo* send_channel = GetSendChannel(ssrc);
1720 if (!send_channel) {
1721 LOG(LS_ERROR) << "The specified ssrc " << ssrc << " is not in use.";
1722 return false;
1723 }
1724 send_channel->set_video_format(format);
1725 return true;
1726}
1727
1728bool WebRtcVideoMediaChannel::SetRender(bool render) {
1729 if (render == render_started_) {
1730 return true; // no action required
1731 }
1732
1733 bool ret = true;
1734 for (RecvChannelMap::iterator it = recv_channels_.begin();
1735 it != recv_channels_.end(); ++it) {
1736 if (render) {
1737 if (engine()->vie()->render()->StartRender(
1738 it->second->channel_id()) != 0) {
1739 LOG_RTCERR1(StartRender, it->second->channel_id());
1740 ret = false;
1741 }
1742 } else {
1743 if (engine()->vie()->render()->StopRender(
1744 it->second->channel_id()) != 0) {
1745 LOG_RTCERR1(StopRender, it->second->channel_id());
1746 ret = false;
1747 }
1748 }
1749 }
1750 if (ret) {
1751 render_started_ = render;
1752 }
1753
1754 return ret;
1755}
1756
1757bool WebRtcVideoMediaChannel::SetSend(bool send) {
1758 if (!HasReadySendChannels() && send) {
1759 LOG(LS_ERROR) << "No stream added";
1760 return false;
1761 }
1762 if (send == sending()) {
1763 return true; // No action required.
1764 }
1765
1766 if (send) {
1767 // We've been asked to start sending.
1768 // SetSendCodecs must have been called already.
1769 if (!send_codec_) {
1770 return false;
1771 }
1772 // Start send now.
1773 if (!StartSend()) {
1774 return false;
1775 }
1776 } else {
1777 // We've been asked to stop sending.
1778 if (!StopSend()) {
1779 return false;
1780 }
1781 }
1782 sending_ = send;
1783
1784 return true;
1785}
1786
1787bool WebRtcVideoMediaChannel::AddSendStream(const StreamParams& sp) {
1788 LOG(LS_INFO) << "AddSendStream " << sp.ToString();
1789
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00001790 if (!IsOneSsrcStream(sp) && !IsSimulcastStream(sp)) {
1791 LOG(LS_ERROR) << "AddSendStream: bad local stream parameters";
1792 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001793 }
1794
1795 uint32 ssrc_key;
1796 if (!CreateSendChannelKey(sp.first_ssrc(), &ssrc_key)) {
1797 LOG(LS_ERROR) << "Trying to register duplicate ssrc: " << sp.first_ssrc();
1798 return false;
1799 }
1800 // If the default channel is already used for sending create a new channel
1801 // otherwise use the default channel for sending.
1802 int channel_id = -1;
1803 if (send_channels_[0]->stream_params() == NULL) {
1804 channel_id = vie_channel_;
1805 } else {
1806 if (!CreateChannel(ssrc_key, MD_SEND, &channel_id)) {
1807 LOG(LS_ERROR) << "AddSendStream: unable to create channel";
1808 return false;
1809 }
1810 }
1811 WebRtcVideoChannelSendInfo* send_channel = send_channels_[ssrc_key];
1812 // Set the send (local) SSRC.
1813 // If there are multiple send SSRCs, we can only set the first one here, and
1814 // the rest of the SSRC(s) need to be set after SetSendCodec has been called
1815 // (with a codec requires multiple SSRC(s)).
1816 if (engine()->vie()->rtp()->SetLocalSSRC(channel_id,
1817 sp.first_ssrc()) != 0) {
1818 LOG_RTCERR2(SetLocalSSRC, channel_id, sp.first_ssrc());
1819 return false;
1820 }
1821
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00001822 // Set the corresponding RTX SSRC.
1823 if (!SetLocalRtxSsrc(channel_id, sp, sp.first_ssrc(), 0)) {
1824 return false;
1825 }
1826
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001827 // Set RTCP CName.
1828 if (engine()->vie()->rtp()->SetRTCPCName(channel_id,
1829 sp.cname.c_str()) != 0) {
1830 LOG_RTCERR2(SetRTCPCName, channel_id, sp.cname.c_str());
1831 return false;
1832 }
1833
1834 // At this point the channel's local SSRC has been updated. If the channel is
1835 // the default channel make sure that all the receive channels are updated as
1836 // well. Receive channels have to have the same SSRC as the default channel in
1837 // order to send receiver reports with this SSRC.
1838 if (IsDefaultChannel(channel_id)) {
1839 for (RecvChannelMap::const_iterator it = recv_channels_.begin();
1840 it != recv_channels_.end(); ++it) {
1841 WebRtcVideoChannelRecvInfo* info = it->second;
1842 int channel_id = info->channel_id();
1843 if (engine()->vie()->rtp()->SetLocalSSRC(channel_id,
1844 sp.first_ssrc()) != 0) {
1845 LOG_RTCERR1(SetLocalSSRC, it->first);
1846 return false;
1847 }
1848 }
1849 }
1850
1851 send_channel->set_stream_params(sp);
1852
1853 // Reset send codec after stream parameters changed.
1854 if (send_codec_) {
1855 if (!SetSendCodec(send_channel, *send_codec_, send_min_bitrate_,
1856 send_start_bitrate_, send_max_bitrate_)) {
1857 return false;
1858 }
1859 LogSendCodecChange("SetSendStreamFormat()");
1860 }
1861
1862 if (sending_) {
1863 return StartSend(send_channel);
1864 }
1865 return true;
1866}
1867
1868bool WebRtcVideoMediaChannel::RemoveSendStream(uint32 ssrc) {
1869 uint32 ssrc_key;
1870 if (!GetSendChannelKey(ssrc, &ssrc_key)) {
1871 LOG(LS_WARNING) << "Try to remove stream with ssrc " << ssrc
1872 << " which doesn't exist.";
1873 return false;
1874 }
1875 WebRtcVideoChannelSendInfo* send_channel = send_channels_[ssrc_key];
1876 int channel_id = send_channel->channel_id();
1877 if (IsDefaultChannel(channel_id) && (send_channel->stream_params() == NULL)) {
1878 // Default channel will still exist. However, if stream_params() is NULL
1879 // there is no stream to remove.
1880 return false;
1881 }
1882 if (sending_) {
1883 StopSend(send_channel);
1884 }
1885
1886 const WebRtcVideoChannelSendInfo::EncoderMap& encoder_map =
1887 send_channel->registered_encoders();
1888 for (WebRtcVideoChannelSendInfo::EncoderMap::const_iterator it =
1889 encoder_map.begin(); it != encoder_map.end(); ++it) {
1890 if (engine()->vie()->ext_codec()->DeRegisterExternalSendCodec(
1891 channel_id, it->first) != 0) {
1892 LOG_RTCERR1(DeregisterEncoderObserver, channel_id);
1893 }
1894 engine()->DestroyExternalEncoder(it->second);
1895 }
1896 send_channel->ClearRegisteredEncoders();
1897
1898 // The receive channels depend on the default channel, recycle it instead.
1899 if (IsDefaultChannel(channel_id)) {
1900 SetCapturer(GetDefaultChannelSsrc(), NULL);
1901 send_channel->ClearStreamParams();
1902 } else {
1903 return DeleteSendChannel(ssrc_key);
1904 }
1905 return true;
1906}
1907
1908bool WebRtcVideoMediaChannel::AddRecvStream(const StreamParams& sp) {
1909 // TODO(zhurunz) Remove this once BWE works properly across different send
1910 // and receive channels.
1911 // Reuse default channel for recv stream in 1:1 call.
1912 if (!InConferenceMode() && first_receive_ssrc_ == 0) {
1913 LOG(LS_INFO) << "Recv stream " << sp.first_ssrc()
1914 << " reuse default channel #"
1915 << vie_channel_;
1916 first_receive_ssrc_ = sp.first_ssrc();
1917 if (render_started_) {
1918 if (engine()->vie()->render()->StartRender(vie_channel_) !=0) {
1919 LOG_RTCERR1(StartRender, vie_channel_);
1920 }
1921 }
1922 return true;
1923 }
1924
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001925 int channel_id = -1;
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001926 RecvChannelMap::iterator channel_iterator =
1927 recv_channels_.find(sp.first_ssrc());
1928 if (channel_iterator == recv_channels_.end() &&
1929 first_receive_ssrc_ != sp.first_ssrc()) {
1930 // TODO(perkj): Implement recv media from multiple media SSRCs per stream.
1931 // NOTE: We have two SSRCs per stream when RTX is enabled.
1932 if (!IsOneSsrcStream(sp)) {
1933 LOG(LS_ERROR) << "WebRtcVideoMediaChannel supports one primary SSRC per"
1934 << " stream and one FID SSRC per primary SSRC.";
1935 return false;
1936 }
1937
1938 // Create a new channel for receiving video data.
1939 // In order to get the bandwidth estimation work fine for
1940 // receive only channels, we connect all receiving channels
1941 // to our master send channel.
1942 if (!CreateChannel(sp.first_ssrc(), MD_RECV, &channel_id)) {
1943 return false;
1944 }
1945 } else {
1946 // Already exists.
1947 if (first_receive_ssrc_ == sp.first_ssrc()) {
1948 return false;
1949 }
1950 // Early receive added channel.
1951 channel_id = (*channel_iterator).second->channel_id();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001952 }
1953
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00001954 // Set the corresponding RTX SSRC.
1955 uint32 rtx_ssrc;
1956 bool has_rtx = sp.GetFidSsrc(sp.first_ssrc(), &rtx_ssrc);
1957 if (has_rtx && engine()->vie()->rtp()->SetRemoteSSRCType(
1958 channel_id, webrtc::kViEStreamTypeRtx, rtx_ssrc) != 0) {
1959 LOG_RTCERR3(SetRemoteSSRCType, channel_id, webrtc::kViEStreamTypeRtx,
1960 rtx_ssrc);
1961 return false;
1962 }
1963
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001964 // Get the default renderer.
1965 VideoRenderer* default_renderer = NULL;
1966 if (InConferenceMode()) {
1967 // The recv_channels_ size start out being 1, so if it is two here this
1968 // is the first receive channel created (vie_channel_ is not used for
1969 // receiving in a conference call). This means that the renderer stored
1970 // inside vie_channel_ should be used for the just created channel.
1971 if (recv_channels_.size() == 2 &&
1972 recv_channels_.find(0) != recv_channels_.end()) {
1973 GetRenderer(0, &default_renderer);
1974 }
1975 }
1976
1977 // The first recv stream reuses the default renderer (if a default renderer
1978 // has been set).
1979 if (default_renderer) {
1980 SetRenderer(sp.first_ssrc(), default_renderer);
1981 }
1982
1983 LOG(LS_INFO) << "New video stream " << sp.first_ssrc()
1984 << " registered to VideoEngine channel #"
1985 << channel_id << " and connected to channel #" << vie_channel_;
1986
1987 return true;
1988}
1989
1990bool WebRtcVideoMediaChannel::RemoveRecvStream(uint32 ssrc) {
1991 RecvChannelMap::iterator it = recv_channels_.find(ssrc);
1992
1993 if (it == recv_channels_.end()) {
1994 // TODO(perkj): Remove this once BWE works properly across different send
1995 // and receive channels.
1996 // The default channel is reused for recv stream in 1:1 call.
1997 if (first_receive_ssrc_ == ssrc) {
1998 first_receive_ssrc_ = 0;
1999 // Need to stop the renderer and remove it since the render window can be
2000 // deleted after this.
2001 if (render_started_) {
2002 if (engine()->vie()->render()->StopRender(vie_channel_) !=0) {
2003 LOG_RTCERR1(StopRender, it->second->channel_id());
2004 }
2005 }
2006 recv_channels_[0]->SetRenderer(NULL);
2007 return true;
2008 }
2009 return false;
2010 }
2011 WebRtcVideoChannelRecvInfo* info = it->second;
2012 int channel_id = info->channel_id();
2013 if (engine()->vie()->render()->RemoveRenderer(channel_id) != 0) {
2014 LOG_RTCERR1(RemoveRenderer, channel_id);
2015 }
2016
2017 if (engine()->vie()->network()->DeregisterSendTransport(channel_id) !=0) {
2018 LOG_RTCERR1(DeRegisterSendTransport, channel_id);
2019 }
2020
2021 if (engine()->vie()->codec()->DeregisterDecoderObserver(
2022 channel_id) != 0) {
2023 LOG_RTCERR1(DeregisterDecoderObserver, channel_id);
2024 }
2025
2026 const WebRtcVideoChannelRecvInfo::DecoderMap& decoder_map =
2027 info->registered_decoders();
2028 for (WebRtcVideoChannelRecvInfo::DecoderMap::const_iterator it =
2029 decoder_map.begin(); it != decoder_map.end(); ++it) {
2030 if (engine()->vie()->ext_codec()->DeRegisterExternalReceiveCodec(
2031 channel_id, it->first) != 0) {
2032 LOG_RTCERR1(DeregisterDecoderObserver, channel_id);
2033 }
2034 engine()->DestroyExternalDecoder(it->second);
2035 }
2036 info->ClearRegisteredDecoders();
2037
2038 LOG(LS_INFO) << "Removing video stream " << ssrc
2039 << " with VideoEngine channel #"
2040 << channel_id;
wu@webrtc.org9caf2762013-12-11 18:25:07 +00002041 bool ret = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002042 if (engine()->vie()->base()->DeleteChannel(channel_id) == -1) {
2043 LOG_RTCERR1(DeleteChannel, channel_id);
wu@webrtc.org9caf2762013-12-11 18:25:07 +00002044 ret = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002045 }
2046 // Delete the WebRtcVideoChannelRecvInfo pointed to by it->second.
2047 delete info;
2048 recv_channels_.erase(it);
wu@webrtc.org9caf2762013-12-11 18:25:07 +00002049 return ret;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002050}
2051
2052bool WebRtcVideoMediaChannel::StartSend() {
2053 bool success = true;
2054 for (SendChannelMap::iterator iter = send_channels_.begin();
2055 iter != send_channels_.end(); ++iter) {
2056 WebRtcVideoChannelSendInfo* send_channel = iter->second;
2057 if (!StartSend(send_channel)) {
2058 success = false;
2059 }
2060 }
2061 return success;
2062}
2063
2064bool WebRtcVideoMediaChannel::StartSend(
2065 WebRtcVideoChannelSendInfo* send_channel) {
2066 const int channel_id = send_channel->channel_id();
2067 if (engine()->vie()->base()->StartSend(channel_id) != 0) {
2068 LOG_RTCERR1(StartSend, channel_id);
2069 return false;
2070 }
2071
2072 send_channel->set_sending(true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002073 return true;
2074}
2075
2076bool WebRtcVideoMediaChannel::StopSend() {
2077 bool success = true;
2078 for (SendChannelMap::iterator iter = send_channels_.begin();
2079 iter != send_channels_.end(); ++iter) {
2080 WebRtcVideoChannelSendInfo* send_channel = iter->second;
2081 if (!StopSend(send_channel)) {
2082 success = false;
2083 }
2084 }
2085 return success;
2086}
2087
2088bool WebRtcVideoMediaChannel::StopSend(
2089 WebRtcVideoChannelSendInfo* send_channel) {
2090 const int channel_id = send_channel->channel_id();
2091 if (engine()->vie()->base()->StopSend(channel_id) != 0) {
2092 LOG_RTCERR1(StopSend, channel_id);
2093 return false;
2094 }
2095 send_channel->set_sending(false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002096 return true;
2097}
2098
2099bool WebRtcVideoMediaChannel::SendIntraFrame() {
2100 bool success = true;
2101 for (SendChannelMap::iterator iter = send_channels_.begin();
2102 iter != send_channels_.end();
2103 ++iter) {
2104 WebRtcVideoChannelSendInfo* send_channel = iter->second;
2105 const int channel_id = send_channel->channel_id();
2106 if (engine()->vie()->codec()->SendKeyFrame(channel_id) != 0) {
2107 LOG_RTCERR1(SendKeyFrame, channel_id);
2108 success = false;
2109 }
2110 }
2111 return success;
2112}
2113
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002114bool WebRtcVideoMediaChannel::HasReadySendChannels() {
2115 return !send_channels_.empty() &&
2116 ((send_channels_.size() > 1) ||
2117 (send_channels_[0]->stream_params() != NULL));
2118}
2119
2120bool WebRtcVideoMediaChannel::GetSendChannelKey(uint32 local_ssrc,
2121 uint32* key) {
2122 *key = 0;
2123 // If a send channel is not ready to send it will not have local_ssrc
2124 // registered to it.
2125 if (!HasReadySendChannels()) {
2126 return false;
2127 }
2128 // The default channel is stored with key 0. The key therefore does not match
2129 // the SSRC associated with the default channel. Check if the SSRC provided
2130 // corresponds to the default channel's SSRC.
2131 if (local_ssrc == GetDefaultChannelSsrc()) {
2132 return true;
2133 }
2134 if (send_channels_.find(local_ssrc) == send_channels_.end()) {
2135 for (SendChannelMap::iterator iter = send_channels_.begin();
2136 iter != send_channels_.end(); ++iter) {
2137 WebRtcVideoChannelSendInfo* send_channel = iter->second;
2138 if (send_channel->has_ssrc(local_ssrc)) {
2139 *key = iter->first;
2140 return true;
2141 }
2142 }
2143 return false;
2144 }
2145 // The key was found in the above std::map::find call. This means that the
2146 // ssrc is the key.
2147 *key = local_ssrc;
2148 return true;
2149}
2150
2151WebRtcVideoChannelSendInfo* WebRtcVideoMediaChannel::GetSendChannel(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002152 uint32 local_ssrc) {
2153 uint32 key;
2154 if (!GetSendChannelKey(local_ssrc, &key)) {
2155 return NULL;
2156 }
2157 return send_channels_[key];
2158}
2159
2160bool WebRtcVideoMediaChannel::CreateSendChannelKey(uint32 local_ssrc,
2161 uint32* key) {
2162 if (GetSendChannelKey(local_ssrc, key)) {
2163 // If there is a key corresponding to |local_ssrc|, the SSRC is already in
2164 // use. SSRCs need to be unique in a session and at this point a duplicate
2165 // SSRC has been detected.
2166 return false;
2167 }
2168 if (send_channels_[0]->stream_params() == NULL) {
2169 // key should be 0 here as the default channel should be re-used whenever it
2170 // is not used.
2171 *key = 0;
2172 return true;
2173 }
2174 // SSRC is currently not in use and the default channel is already in use. Use
2175 // the SSRC as key since it is supposed to be unique in a session.
2176 *key = local_ssrc;
2177 return true;
2178}
2179
wu@webrtc.org24301a62013-12-13 19:17:43 +00002180int WebRtcVideoMediaChannel::GetSendChannelNum(VideoCapturer* capturer) {
2181 int num = 0;
2182 for (SendChannelMap::iterator iter = send_channels_.begin();
2183 iter != send_channels_.end(); ++iter) {
2184 WebRtcVideoChannelSendInfo* send_channel = iter->second;
2185 if (send_channel->video_capturer() == capturer) {
2186 ++num;
2187 }
2188 }
2189 return num;
2190}
2191
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002192uint32 WebRtcVideoMediaChannel::GetDefaultChannelSsrc() {
2193 WebRtcVideoChannelSendInfo* send_channel = send_channels_[0];
2194 const StreamParams* sp = send_channel->stream_params();
2195 if (sp == NULL) {
2196 // This happens if no send stream is currently registered.
2197 return 0;
2198 }
2199 return sp->first_ssrc();
2200}
2201
2202bool WebRtcVideoMediaChannel::DeleteSendChannel(uint32 ssrc_key) {
2203 if (send_channels_.find(ssrc_key) == send_channels_.end()) {
2204 return false;
2205 }
2206 WebRtcVideoChannelSendInfo* send_channel = send_channels_[ssrc_key];
wu@webrtc.org24301a62013-12-13 19:17:43 +00002207 MaybeDisconnectCapturer(send_channel->video_capturer());
henrike@webrtc.orgc7bec842014-03-12 19:53:43 +00002208 send_channel->set_video_capturer(NULL, engine()->vie());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002209
2210 int channel_id = send_channel->channel_id();
2211 int capture_id = send_channel->capture_id();
2212 if (engine()->vie()->codec()->DeregisterEncoderObserver(
2213 channel_id) != 0) {
2214 LOG_RTCERR1(DeregisterEncoderObserver, channel_id);
2215 }
2216
2217 // Destroy the external capture interface.
2218 if (engine()->vie()->capture()->DisconnectCaptureDevice(
2219 channel_id) != 0) {
2220 LOG_RTCERR1(DisconnectCaptureDevice, channel_id);
2221 }
2222 if (engine()->vie()->capture()->ReleaseCaptureDevice(
2223 capture_id) != 0) {
2224 LOG_RTCERR1(ReleaseCaptureDevice, capture_id);
2225 }
2226
2227 // The default channel is stored in both |send_channels_| and
2228 // |recv_channels_|. To make sure it is only deleted once from vie let the
2229 // delete call happen when tearing down |recv_channels_| and not here.
2230 if (!IsDefaultChannel(channel_id)) {
2231 engine_->vie()->base()->DeleteChannel(channel_id);
2232 }
2233 delete send_channel;
2234 send_channels_.erase(ssrc_key);
2235 return true;
2236}
2237
2238bool WebRtcVideoMediaChannel::RemoveCapturer(uint32 ssrc) {
2239 WebRtcVideoChannelSendInfo* send_channel = GetSendChannel(ssrc);
2240 if (!send_channel) {
2241 return false;
2242 }
2243 VideoCapturer* capturer = send_channel->video_capturer();
2244 if (capturer == NULL) {
2245 return false;
2246 }
wu@webrtc.org24301a62013-12-13 19:17:43 +00002247 MaybeDisconnectCapturer(capturer);
henrike@webrtc.orgc7bec842014-03-12 19:53:43 +00002248 send_channel->set_video_capturer(NULL, engine()->vie());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002249 const int64 timestamp = send_channel->local_stream_info()->time_stamp();
2250 if (send_codec_) {
2251 QueueBlackFrame(ssrc, timestamp, send_codec_->maxFramerate);
2252 }
2253 return true;
2254}
2255
2256bool WebRtcVideoMediaChannel::SetRenderer(uint32 ssrc,
2257 VideoRenderer* renderer) {
2258 if (recv_channels_.find(ssrc) == recv_channels_.end()) {
2259 // TODO(perkj): Remove this once BWE works properly across different send
2260 // and receive channels.
2261 // The default channel is reused for recv stream in 1:1 call.
2262 if (first_receive_ssrc_ == ssrc &&
2263 recv_channels_.find(0) != recv_channels_.end()) {
2264 LOG(LS_INFO) << "SetRenderer " << ssrc
2265 << " reuse default channel #"
2266 << vie_channel_;
2267 recv_channels_[0]->SetRenderer(renderer);
2268 return true;
2269 }
2270 return false;
2271 }
2272
2273 recv_channels_[ssrc]->SetRenderer(renderer);
2274 return true;
2275}
2276
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00002277bool WebRtcVideoMediaChannel::GetStats(const StatsOptions& options,
2278 VideoMediaInfo* info) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002279 // Get sender statistics and build VideoSenderInfo.
2280 unsigned int total_bitrate_sent = 0;
2281 unsigned int video_bitrate_sent = 0;
2282 unsigned int fec_bitrate_sent = 0;
2283 unsigned int nack_bitrate_sent = 0;
2284 unsigned int estimated_send_bandwidth = 0;
2285 unsigned int target_enc_bitrate = 0;
2286 if (send_codec_) {
2287 for (SendChannelMap::const_iterator iter = send_channels_.begin();
2288 iter != send_channels_.end(); ++iter) {
2289 WebRtcVideoChannelSendInfo* send_channel = iter->second;
2290 const int channel_id = send_channel->channel_id();
2291 VideoSenderInfo sinfo;
2292 const StreamParams* send_params = send_channel->stream_params();
2293 if (send_params == NULL) {
2294 // This should only happen if the default vie channel is not in use.
2295 // This can happen if no streams have ever been added or the stream
2296 // corresponding to the default channel has been removed. Note that
2297 // there may be non-default vie channels in use when this happen so
2298 // asserting send_channels_.size() == 1 is not correct and neither is
2299 // breaking out of the loop.
2300 ASSERT(channel_id == vie_channel_);
2301 continue;
2302 }
2303 unsigned int bytes_sent, packets_sent, bytes_recv, packets_recv;
2304 if (engine_->vie()->rtp()->GetRTPStatistics(channel_id, bytes_sent,
2305 packets_sent, bytes_recv,
2306 packets_recv) != 0) {
2307 LOG_RTCERR1(GetRTPStatistics, vie_channel_);
2308 continue;
2309 }
2310 WebRtcLocalStreamInfo* channel_stream_info =
2311 send_channel->local_stream_info();
2312
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002313 for (size_t i = 0; i < send_params->ssrcs.size(); ++i) {
2314 sinfo.add_ssrc(send_params->ssrcs[i]);
2315 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002316 sinfo.codec_name = send_codec_->plName;
2317 sinfo.bytes_sent = bytes_sent;
2318 sinfo.packets_sent = packets_sent;
2319 sinfo.packets_cached = -1;
2320 sinfo.packets_lost = -1;
2321 sinfo.fraction_lost = -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002322 sinfo.rtt_ms = -1;
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00002323 sinfo.input_frame_width = static_cast<int>(channel_stream_info->width());
2324 sinfo.input_frame_height =
2325 static_cast<int>(channel_stream_info->height());
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00002326
2327 VideoCapturer* video_capturer = send_channel->video_capturer();
2328 if (video_capturer) {
2329 video_capturer->GetStats(&sinfo.adapt_frame_drops,
2330 &sinfo.effects_frame_drops,
2331 &sinfo.capturer_frame_time);
2332 }
2333
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00002334 webrtc::VideoCodec vie_codec;
2335 if (engine()->vie()->codec()->GetSendCodec(channel_id, vie_codec) == 0) {
2336 sinfo.send_frame_width = vie_codec.width;
2337 sinfo.send_frame_height = vie_codec.height;
2338 } else {
2339 sinfo.send_frame_width = -1;
2340 sinfo.send_frame_height = -1;
2341 LOG_RTCERR1(GetSendCodec, channel_id);
2342 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002343 sinfo.framerate_input = channel_stream_info->framerate();
2344 sinfo.framerate_sent = send_channel->encoder_observer()->framerate();
2345 sinfo.nominal_bitrate = send_channel->encoder_observer()->bitrate();
2346 sinfo.preferred_bitrate = send_max_bitrate_;
2347 sinfo.adapt_reason = send_channel->CurrentAdaptReason();
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002348 sinfo.capture_jitter_ms = -1;
2349 sinfo.avg_encode_ms = -1;
wu@webrtc.org9caf2762013-12-11 18:25:07 +00002350 sinfo.encode_usage_percent = -1;
2351 sinfo.capture_queue_delay_ms_per_s = -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002352
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002353 int capture_jitter_ms = 0;
2354 int avg_encode_time_ms = 0;
wu@webrtc.org9caf2762013-12-11 18:25:07 +00002355 int encode_usage_percent = 0;
2356 int capture_queue_delay_ms_per_s = 0;
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002357 if (engine()->vie()->base()->CpuOveruseMeasures(
wu@webrtc.org9caf2762013-12-11 18:25:07 +00002358 channel_id,
2359 &capture_jitter_ms,
2360 &avg_encode_time_ms,
2361 &encode_usage_percent,
2362 &capture_queue_delay_ms_per_s) == 0) {
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002363 sinfo.capture_jitter_ms = capture_jitter_ms;
2364 sinfo.avg_encode_ms = avg_encode_time_ms;
wu@webrtc.org9caf2762013-12-11 18:25:07 +00002365 sinfo.encode_usage_percent = encode_usage_percent;
2366 sinfo.capture_queue_delay_ms_per_s = capture_queue_delay_ms_per_s;
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002367 }
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002368
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00002369#ifdef USE_WEBRTC_DEV_BRANCH
2370 webrtc::RtcpPacketTypeCounter rtcp_sent;
2371 webrtc::RtcpPacketTypeCounter rtcp_received;
2372 if (engine()->vie()->rtp()->GetRtcpPacketTypeCounters(
2373 channel_id, &rtcp_sent, &rtcp_received) == 0) {
2374 sinfo.firs_rcvd = rtcp_received.fir_packets;
2375 sinfo.plis_rcvd = rtcp_received.pli_packets;
2376 sinfo.nacks_rcvd = rtcp_received.nack_packets;
2377 } else {
2378 sinfo.firs_rcvd = -1;
2379 sinfo.plis_rcvd = -1;
2380 sinfo.nacks_rcvd = -1;
2381 LOG_RTCERR1(GetRtcpPacketTypeCounters, channel_id);
2382 }
2383#else
2384 sinfo.firs_rcvd = -1;
2385 sinfo.plis_rcvd = -1;
2386 sinfo.nacks_rcvd = -1;
2387#endif
2388
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002389 // Get received RTCP statistics for the sender (reported by the remote
2390 // client in a RTCP packet), if available.
2391 // It's not a fatal error if we can't, since RTCP may not have arrived
2392 // yet.
2393 webrtc::RtcpStatistics outgoing_stream_rtcp_stats;
2394 int outgoing_stream_rtt_ms;
2395
2396 if (engine_->vie()->rtp()->GetSendChannelRtcpStatistics(
2397 channel_id,
2398 outgoing_stream_rtcp_stats,
2399 outgoing_stream_rtt_ms) == 0) {
2400 // Convert Q8 to float.
2401 sinfo.packets_lost = outgoing_stream_rtcp_stats.cumulative_lost;
2402 sinfo.fraction_lost = static_cast<float>(
2403 outgoing_stream_rtcp_stats.fraction_lost) / (1 << 8);
2404 sinfo.rtt_ms = outgoing_stream_rtt_ms;
2405 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002406 info->senders.push_back(sinfo);
2407
2408 unsigned int channel_total_bitrate_sent = 0;
2409 unsigned int channel_video_bitrate_sent = 0;
2410 unsigned int channel_fec_bitrate_sent = 0;
2411 unsigned int channel_nack_bitrate_sent = 0;
2412 if (engine_->vie()->rtp()->GetBandwidthUsage(
2413 channel_id, channel_total_bitrate_sent, channel_video_bitrate_sent,
2414 channel_fec_bitrate_sent, channel_nack_bitrate_sent) == 0) {
2415 total_bitrate_sent += channel_total_bitrate_sent;
2416 video_bitrate_sent += channel_video_bitrate_sent;
2417 fec_bitrate_sent += channel_fec_bitrate_sent;
2418 nack_bitrate_sent += channel_nack_bitrate_sent;
2419 } else {
2420 LOG_RTCERR1(GetBandwidthUsage, channel_id);
2421 }
2422
2423 unsigned int estimated_stream_send_bandwidth = 0;
2424 if (engine_->vie()->rtp()->GetEstimatedSendBandwidth(
2425 channel_id, &estimated_stream_send_bandwidth) == 0) {
2426 estimated_send_bandwidth += estimated_stream_send_bandwidth;
2427 } else {
2428 LOG_RTCERR1(GetEstimatedSendBandwidth, channel_id);
2429 }
2430 unsigned int target_enc_stream_bitrate = 0;
2431 if (engine_->vie()->codec()->GetCodecTargetBitrate(
2432 channel_id, &target_enc_stream_bitrate) == 0) {
2433 target_enc_bitrate += target_enc_stream_bitrate;
2434 } else {
2435 LOG_RTCERR1(GetCodecTargetBitrate, channel_id);
2436 }
2437 }
2438 } else {
2439 LOG(LS_WARNING) << "GetStats: sender information not ready.";
2440 }
2441
2442 // Get the SSRC and stats for each receiver, based on our own calculations.
2443 unsigned int estimated_recv_bandwidth = 0;
2444 for (RecvChannelMap::const_iterator it = recv_channels_.begin();
2445 it != recv_channels_.end(); ++it) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002446 WebRtcVideoChannelRecvInfo* channel = it->second;
2447
2448 unsigned int ssrc;
2449 // Get receiver statistics and build VideoReceiverInfo, if we have data.
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +00002450 // Skip the default channel (ssrc == 0).
2451 if (engine_->vie()->rtp()->GetRemoteSSRC(
2452 channel->channel_id(), ssrc) != 0 ||
2453 ssrc == 0)
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002454 continue;
2455
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002456 webrtc::StreamDataCounters sent;
2457 webrtc::StreamDataCounters received;
2458 if (engine_->vie()->rtp()->GetRtpStatistics(channel->channel_id(),
2459 sent, received) != 0) {
2460 LOG_RTCERR1(GetRTPStatistics, channel->channel_id());
2461 return false;
2462 }
2463 VideoReceiverInfo rinfo;
2464 rinfo.add_ssrc(ssrc);
2465 rinfo.bytes_rcvd = received.bytes;
2466 rinfo.packets_rcvd = received.packets;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002467 rinfo.packets_lost = -1;
2468 rinfo.packets_concealed = -1;
2469 rinfo.fraction_lost = -1; // from SentRTCP
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002470 rinfo.frame_width = channel->render_adapter()->width();
2471 rinfo.frame_height = channel->render_adapter()->height();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002472 int fps = channel->render_adapter()->framerate();
2473 rinfo.framerate_decoded = fps;
2474 rinfo.framerate_output = fps;
wu@webrtc.org97077a32013-10-25 21:18:33 +00002475 channel->decoder_observer()->ExportTo(&rinfo);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002476
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00002477#ifdef USE_WEBRTC_DEV_BRANCH
2478 webrtc::RtcpPacketTypeCounter rtcp_sent;
2479 webrtc::RtcpPacketTypeCounter rtcp_received;
2480 if (engine()->vie()->rtp()->GetRtcpPacketTypeCounters(
2481 channel->channel_id(), &rtcp_sent, &rtcp_received) == 0) {
2482 rinfo.firs_sent = rtcp_sent.fir_packets;
2483 rinfo.plis_sent = rtcp_sent.pli_packets;
2484 rinfo.nacks_sent = rtcp_sent.nack_packets;
2485 } else {
2486 rinfo.firs_sent = -1;
2487 rinfo.plis_sent = -1;
2488 rinfo.nacks_sent = -1;
2489 LOG_RTCERR1(GetRtcpPacketTypeCounters, channel->channel_id());
2490 }
2491#else
2492 rinfo.firs_sent = -1;
2493 rinfo.plis_sent = -1;
2494 rinfo.nacks_sent = -1;
2495#endif
2496
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002497 // Get our locally created statistics of the received RTP stream.
2498 webrtc::RtcpStatistics incoming_stream_rtcp_stats;
2499 int incoming_stream_rtt_ms;
2500 if (engine_->vie()->rtp()->GetReceiveChannelRtcpStatistics(
2501 channel->channel_id(),
2502 incoming_stream_rtcp_stats,
2503 incoming_stream_rtt_ms) == 0) {
2504 // Convert Q8 to float.
2505 rinfo.packets_lost = incoming_stream_rtcp_stats.cumulative_lost;
2506 rinfo.fraction_lost = static_cast<float>(
2507 incoming_stream_rtcp_stats.fraction_lost) / (1 << 8);
2508 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002509 info->receivers.push_back(rinfo);
2510
2511 unsigned int estimated_recv_stream_bandwidth = 0;
2512 if (engine_->vie()->rtp()->GetEstimatedReceiveBandwidth(
2513 channel->channel_id(), &estimated_recv_stream_bandwidth) == 0) {
2514 estimated_recv_bandwidth += estimated_recv_stream_bandwidth;
2515 } else {
2516 LOG_RTCERR1(GetEstimatedReceiveBandwidth, channel->channel_id());
2517 }
2518 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002519 // Build BandwidthEstimationInfo.
2520 // TODO(zhurunz): Add real unittest for this.
2521 BandwidthEstimationInfo bwe;
2522
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00002523 // TODO(jiayl): remove the condition when the necessary changes are available
2524 // outside the dev branch.
2525#ifdef USE_WEBRTC_DEV_BRANCH
2526 if (options.include_received_propagation_stats) {
2527 webrtc::ReceiveBandwidthEstimatorStats additional_stats;
2528 // Only call for the default channel because the returned stats are
2529 // collected for all the channels using the same estimator.
2530 if (engine_->vie()->rtp()->GetReceiveBandwidthEstimatorStats(
mallinath@webrtc.org92fdfeb2014-02-17 18:49:41 +00002531 recv_channels_[0]->channel_id(), &additional_stats) == 0) {
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00002532 bwe.total_received_propagation_delta_ms =
2533 additional_stats.total_propagation_time_delta_ms;
2534 bwe.recent_received_propagation_delta_ms.swap(
2535 additional_stats.recent_propagation_time_delta_ms);
2536 bwe.recent_received_packet_group_arrival_time_ms.swap(
2537 additional_stats.recent_arrival_time_ms);
2538 }
2539 }
henrike@webrtc.orgb8395eb2014-02-28 21:57:22 +00002540
2541 engine_->vie()->rtp()->GetPacerQueuingDelayMs(
2542 recv_channels_[0]->channel_id(), &bwe.bucket_delay);
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00002543#endif
2544
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002545 // Calculations done above per send/receive stream.
2546 bwe.actual_enc_bitrate = video_bitrate_sent;
2547 bwe.transmit_bitrate = total_bitrate_sent;
2548 bwe.retransmit_bitrate = nack_bitrate_sent;
2549 bwe.available_send_bandwidth = estimated_send_bandwidth;
2550 bwe.available_recv_bandwidth = estimated_recv_bandwidth;
2551 bwe.target_enc_bitrate = target_enc_bitrate;
2552
2553 info->bw_estimations.push_back(bwe);
2554
2555 return true;
2556}
2557
2558bool WebRtcVideoMediaChannel::SetCapturer(uint32 ssrc,
2559 VideoCapturer* capturer) {
2560 ASSERT(ssrc != 0);
2561 if (!capturer) {
2562 return RemoveCapturer(ssrc);
2563 }
2564 WebRtcVideoChannelSendInfo* send_channel = GetSendChannel(ssrc);
2565 if (!send_channel) {
2566 return false;
2567 }
2568 VideoCapturer* old_capturer = send_channel->video_capturer();
wu@webrtc.org24301a62013-12-13 19:17:43 +00002569 MaybeDisconnectCapturer(old_capturer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002570
henrike@webrtc.orgc7bec842014-03-12 19:53:43 +00002571 send_channel->set_video_capturer(capturer, engine()->vie());
wu@webrtc.orga8910d22014-01-23 22:12:45 +00002572 MaybeConnectCapturer(capturer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002573 if (!capturer->IsScreencast() && ratio_w_ != 0 && ratio_h_ != 0) {
2574 capturer->UpdateAspectRatio(ratio_w_, ratio_h_);
2575 }
2576 const int64 timestamp = send_channel->local_stream_info()->time_stamp();
2577 if (send_codec_) {
2578 QueueBlackFrame(ssrc, timestamp, send_codec_->maxFramerate);
2579 }
2580 return true;
2581}
2582
2583bool WebRtcVideoMediaChannel::RequestIntraFrame() {
2584 // There is no API exposed to application to request a key frame
2585 // ViE does this internally when there are errors from decoder
2586 return false;
2587}
2588
wu@webrtc.orga9890802013-12-13 00:21:03 +00002589void WebRtcVideoMediaChannel::OnPacketReceived(
2590 talk_base::Buffer* packet, const talk_base::PacketTime& packet_time) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002591 // Pick which channel to send this packet to. If this packet doesn't match
2592 // any multiplexed streams, just send it to the default channel. Otherwise,
2593 // send it to the specific decoder instance for that stream.
2594 uint32 ssrc = 0;
2595 if (!GetRtpSsrc(packet->data(), packet->length(), &ssrc))
2596 return;
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00002597 int processing_channel = GetRecvChannelNum(ssrc);
2598 if (processing_channel == -1) {
2599 // Allocate an unsignalled recv channel for processing in conference mode.
henrike@webrtc.org18e59112014-03-14 17:19:38 +00002600 if (!InConferenceMode()) {
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00002601 // If we cant find or allocate one, use the default.
2602 processing_channel = video_channel();
henrike@webrtc.org18e59112014-03-14 17:19:38 +00002603 } else if (!CreateUnsignalledRecvChannel(ssrc, &processing_channel)) {
2604 // If we cant create an unsignalled recv channel, drop the packet in
2605 // conference mode.
2606 return;
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00002607 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002608 }
2609
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00002610 engine()->vie()->network()->ReceivedRTPPacket(
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00002611 processing_channel,
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00002612 packet->data(),
wu@webrtc.orga9890802013-12-13 00:21:03 +00002613 static_cast<int>(packet->length()),
2614 webrtc::PacketTime(packet_time.timestamp, packet_time.not_before));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002615}
2616
wu@webrtc.orga9890802013-12-13 00:21:03 +00002617void WebRtcVideoMediaChannel::OnRtcpReceived(
2618 talk_base::Buffer* packet, const talk_base::PacketTime& packet_time) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002619// Sending channels need all RTCP packets with feedback information.
2620// Even sender reports can contain attached report blocks.
2621// Receiving channels need sender reports in order to create
2622// correct receiver reports.
2623
2624 uint32 ssrc = 0;
2625 if (!GetRtcpSsrc(packet->data(), packet->length(), &ssrc)) {
2626 LOG(LS_WARNING) << "Failed to parse SSRC from received RTCP packet";
2627 return;
2628 }
2629 int type = 0;
2630 if (!GetRtcpType(packet->data(), packet->length(), &type)) {
2631 LOG(LS_WARNING) << "Failed to parse type from received RTCP packet";
2632 return;
2633 }
2634
2635 // If it is a sender report, find the channel that is listening.
2636 if (type == kRtcpTypeSR) {
2637 int which_channel = GetRecvChannelNum(ssrc);
2638 if (which_channel != -1 && !IsDefaultChannel(which_channel)) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00002639 engine_->vie()->network()->ReceivedRTCPPacket(
2640 which_channel,
2641 packet->data(),
2642 static_cast<int>(packet->length()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002643 }
2644 }
2645 // SR may continue RR and any RR entry may correspond to any one of the send
2646 // channels. So all RTCP packets must be forwarded all send channels. ViE
2647 // will filter out RR internally.
2648 for (SendChannelMap::iterator iter = send_channels_.begin();
2649 iter != send_channels_.end(); ++iter) {
2650 WebRtcVideoChannelSendInfo* send_channel = iter->second;
2651 int channel_id = send_channel->channel_id();
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00002652 engine_->vie()->network()->ReceivedRTCPPacket(
2653 channel_id,
2654 packet->data(),
2655 static_cast<int>(packet->length()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002656 }
2657}
2658
2659void WebRtcVideoMediaChannel::OnReadyToSend(bool ready) {
2660 SetNetworkTransmissionState(ready);
2661}
2662
2663bool WebRtcVideoMediaChannel::MuteStream(uint32 ssrc, bool muted) {
2664 WebRtcVideoChannelSendInfo* send_channel = GetSendChannel(ssrc);
2665 if (!send_channel) {
2666 LOG(LS_ERROR) << "The specified ssrc " << ssrc << " is not in use.";
2667 return false;
2668 }
2669 send_channel->set_muted(muted);
2670 return true;
2671}
2672
2673bool WebRtcVideoMediaChannel::SetRecvRtpHeaderExtensions(
2674 const std::vector<RtpHeaderExtension>& extensions) {
2675 if (receive_extensions_ == extensions) {
2676 return true;
2677 }
2678 receive_extensions_ = extensions;
2679
2680 const RtpHeaderExtension* offset_extension =
2681 FindHeaderExtension(extensions, kRtpTimestampOffsetHeaderExtension);
2682 const RtpHeaderExtension* send_time_extension =
henrike@webrtc.org79047f92014-03-06 23:46:59 +00002683 FindHeaderExtension(extensions, kRtpAbsoluteSenderTimeHeaderExtension);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002684
2685 // Loop through all receive channels and enable/disable the extensions.
2686 for (RecvChannelMap::iterator channel_it = recv_channels_.begin();
2687 channel_it != recv_channels_.end(); ++channel_it) {
2688 int channel_id = channel_it->second->channel_id();
2689 if (!SetHeaderExtension(
2690 &webrtc::ViERTP_RTCP::SetReceiveTimestampOffsetStatus, channel_id,
2691 offset_extension)) {
2692 return false;
2693 }
2694 if (!SetHeaderExtension(
2695 &webrtc::ViERTP_RTCP::SetReceiveAbsoluteSendTimeStatus, channel_id,
2696 send_time_extension)) {
2697 return false;
2698 }
2699 }
2700 return true;
2701}
2702
2703bool WebRtcVideoMediaChannel::SetSendRtpHeaderExtensions(
2704 const std::vector<RtpHeaderExtension>& extensions) {
2705 send_extensions_ = extensions;
2706
2707 const RtpHeaderExtension* offset_extension =
2708 FindHeaderExtension(extensions, kRtpTimestampOffsetHeaderExtension);
2709 const RtpHeaderExtension* send_time_extension =
henrike@webrtc.org79047f92014-03-06 23:46:59 +00002710 FindHeaderExtension(extensions, kRtpAbsoluteSenderTimeHeaderExtension);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002711
2712 // Loop through all send channels and enable/disable the extensions.
2713 for (SendChannelMap::iterator channel_it = send_channels_.begin();
2714 channel_it != send_channels_.end(); ++channel_it) {
2715 int channel_id = channel_it->second->channel_id();
2716 if (!SetHeaderExtension(
2717 &webrtc::ViERTP_RTCP::SetSendTimestampOffsetStatus, channel_id,
2718 offset_extension)) {
2719 return false;
2720 }
2721 if (!SetHeaderExtension(
2722 &webrtc::ViERTP_RTCP::SetSendAbsoluteSendTimeStatus, channel_id,
2723 send_time_extension)) {
2724 return false;
2725 }
2726 }
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00002727
2728 if (send_time_extension) {
2729 // For video RTP packets, we would like to update AbsoluteSendTimeHeader
2730 // Extension closer to the network, @ socket level before sending.
2731 // Pushing the extension id to socket layer.
2732 MediaChannel::SetOption(NetworkInterface::ST_RTP,
2733 talk_base::Socket::OPT_RTP_SENDTIME_EXTN_ID,
2734 send_time_extension->id);
2735 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002736 return true;
2737}
2738
mallinath@webrtc.org92fdfeb2014-02-17 18:49:41 +00002739int WebRtcVideoMediaChannel::GetRtpSendTimeExtnId() const {
2740 const RtpHeaderExtension* send_time_extension = FindHeaderExtension(
henrike@webrtc.org79047f92014-03-06 23:46:59 +00002741 send_extensions_, kRtpAbsoluteSenderTimeHeaderExtension);
mallinath@webrtc.org92fdfeb2014-02-17 18:49:41 +00002742 if (send_time_extension) {
2743 return send_time_extension->id;
2744 }
2745 return -1;
2746}
2747
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002748bool WebRtcVideoMediaChannel::SetStartSendBandwidth(int bps) {
2749 LOG(LS_INFO) << "WebRtcVideoMediaChannel::SetStartSendBandwidth";
2750
2751 if (!send_codec_) {
2752 LOG(LS_INFO) << "The send codec has not been set up yet";
2753 return true;
2754 }
2755
2756 // On success, SetSendCodec() will reset |send_start_bitrate_| to |bps/1000|,
2757 // by calling MaybeChangeStartBitrate. That method will also clamp the
2758 // start bitrate between min and max, consistent with the override behavior
2759 // in SetMaxSendBandwidth.
2760 return SetSendCodec(*send_codec_,
2761 send_min_bitrate_, bps / 1000, send_max_bitrate_);
2762}
2763
2764bool WebRtcVideoMediaChannel::SetMaxSendBandwidth(int bps) {
2765 LOG(LS_INFO) << "WebRtcVideoMediaChannel::SetMaxSendBandwidth";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002766
2767 if (InConferenceMode()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002768 LOG(LS_INFO) << "Conference mode ignores SetMaxSendBandwidth";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002769 return true;
2770 }
2771
2772 if (!send_codec_) {
2773 LOG(LS_INFO) << "The send codec has not been set up yet";
2774 return true;
2775 }
2776
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002777 // Use the default value or the bps for the max
2778 int max_bitrate = (bps <= 0) ? send_max_bitrate_ : (bps / 1000);
2779
2780 // Reduce the current minimum and start bitrates if necessary.
2781 int min_bitrate = talk_base::_min(send_min_bitrate_, max_bitrate);
2782 int start_bitrate = talk_base::_min(send_start_bitrate_, max_bitrate);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002783
2784 if (!SetSendCodec(*send_codec_, min_bitrate, start_bitrate, max_bitrate)) {
2785 return false;
2786 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002787 LogSendCodecChange("SetMaxSendBandwidth()");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002788
2789 return true;
2790}
2791
2792bool WebRtcVideoMediaChannel::SetOptions(const VideoOptions &options) {
2793 // Always accept options that are unchanged.
2794 if (options_ == options) {
2795 return true;
2796 }
2797
2798 // Trigger SetSendCodec to set correct noise reduction state if the option has
2799 // changed.
2800 bool denoiser_changed = options.video_noise_reduction.IsSet() &&
2801 (options_.video_noise_reduction != options.video_noise_reduction);
2802
2803 bool leaky_bucket_changed = options.video_leaky_bucket.IsSet() &&
2804 (options_.video_leaky_bucket != options.video_leaky_bucket);
2805
2806 bool buffer_latency_changed = options.buffered_mode_latency.IsSet() &&
2807 (options_.buffered_mode_latency != options.buffered_mode_latency);
2808
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00002809 bool cpu_overuse_detection_changed = options.cpu_overuse_detection.IsSet() &&
2810 (options_.cpu_overuse_detection != options.cpu_overuse_detection);
2811
wu@webrtc.orgde305012013-10-31 15:40:38 +00002812 bool dscp_option_changed = (options_.dscp != options.dscp);
2813
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002814 bool suspend_below_min_bitrate_changed =
2815 options.suspend_below_min_bitrate.IsSet() &&
2816 (options_.suspend_below_min_bitrate != options.suspend_below_min_bitrate);
2817
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002818 bool conference_mode_turned_off = false;
2819 if (options_.conference_mode.IsSet() && options.conference_mode.IsSet() &&
2820 options_.conference_mode.GetWithDefaultIfUnset(false) &&
2821 !options.conference_mode.GetWithDefaultIfUnset(false)) {
2822 conference_mode_turned_off = true;
2823 }
2824
henrike@webrtc.org10bd88e2014-03-11 21:07:25 +00002825
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002826 // Save the options, to be interpreted where appropriate.
2827 // Use options_.SetAll() instead of assignment so that unset value in options
2828 // will not overwrite the previous option value.
2829 options_.SetAll(options);
2830
2831 // Set CPU options for all send channels.
2832 for (SendChannelMap::iterator iter = send_channels_.begin();
2833 iter != send_channels_.end(); ++iter) {
2834 WebRtcVideoChannelSendInfo* send_channel = iter->second;
2835 send_channel->ApplyCpuOptions(options_);
2836 }
2837
2838 // Adjust send codec bitrate if needed.
2839 int conf_max_bitrate = kDefaultConferenceModeMaxVideoBitrate;
2840
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00002841 // Save altered min_bitrate level and apply if necessary.
2842 bool adjusted_min_bitrate = false;
2843 if (options.lower_min_bitrate.IsSet()) {
2844 bool lower;
2845 options.lower_min_bitrate.Get(&lower);
2846
2847 int new_send_min_bitrate = lower ? kLowerMinBitrate : kMinVideoBitrate;
2848 adjusted_min_bitrate = (new_send_min_bitrate != send_min_bitrate_);
2849 send_min_bitrate_ = new_send_min_bitrate;
2850 }
2851
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002852 int expected_bitrate = send_max_bitrate_;
2853 if (InConferenceMode()) {
2854 expected_bitrate = conf_max_bitrate;
2855 } else if (conference_mode_turned_off) {
2856 // This is a special case for turning conference mode off.
2857 // Max bitrate should go back to the default maximum value instead
2858 // of the current maximum.
2859 expected_bitrate = kMaxVideoBitrate;
2860 }
2861
henrike@webrtc.org10bd88e2014-03-11 21:07:25 +00002862 bool reset_send_codec_needed = send_codec_ &&
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00002863 (send_max_bitrate_ != expected_bitrate || denoiser_changed ||
henrike@webrtc.org10bd88e2014-03-11 21:07:25 +00002864 adjusted_min_bitrate);
2865
2866
2867 if (reset_send_codec_needed) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002868 // On success, SetSendCodec() will reset send_max_bitrate_ to
2869 // expected_bitrate.
2870 if (!SetSendCodec(*send_codec_,
2871 send_min_bitrate_,
2872 send_start_bitrate_,
2873 expected_bitrate)) {
2874 return false;
2875 }
2876 LogSendCodecChange("SetOptions()");
2877 }
henrike@webrtc.org10bd88e2014-03-11 21:07:25 +00002878
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002879 if (leaky_bucket_changed) {
2880 bool enable_leaky_bucket =
2881 options_.video_leaky_bucket.GetWithDefaultIfUnset(false);
2882 for (SendChannelMap::iterator it = send_channels_.begin();
2883 it != send_channels_.end(); ++it) {
2884 if (engine()->vie()->rtp()->SetTransmissionSmoothingStatus(
2885 it->second->channel_id(), enable_leaky_bucket) != 0) {
2886 LOG_RTCERR2(SetTransmissionSmoothingStatus, it->second->channel_id(),
2887 enable_leaky_bucket);
2888 }
2889 }
2890 }
2891 if (buffer_latency_changed) {
2892 int buffer_latency =
2893 options_.buffered_mode_latency.GetWithDefaultIfUnset(
2894 cricket::kBufferedModeDisabled);
2895 for (SendChannelMap::iterator it = send_channels_.begin();
2896 it != send_channels_.end(); ++it) {
2897 if (engine()->vie()->rtp()->SetSenderBufferingMode(
2898 it->second->channel_id(), buffer_latency) != 0) {
2899 LOG_RTCERR2(SetSenderBufferingMode, it->second->channel_id(),
2900 buffer_latency);
2901 }
2902 }
2903 for (RecvChannelMap::iterator it = recv_channels_.begin();
2904 it != recv_channels_.end(); ++it) {
2905 if (engine()->vie()->rtp()->SetReceiverBufferingMode(
2906 it->second->channel_id(), buffer_latency) != 0) {
2907 LOG_RTCERR2(SetReceiverBufferingMode, it->second->channel_id(),
2908 buffer_latency);
2909 }
2910 }
2911 }
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00002912 if (cpu_overuse_detection_changed) {
2913 bool cpu_overuse_detection =
2914 options_.cpu_overuse_detection.GetWithDefaultIfUnset(false);
2915 for (SendChannelMap::iterator iter = send_channels_.begin();
2916 iter != send_channels_.end(); ++iter) {
2917 WebRtcVideoChannelSendInfo* send_channel = iter->second;
2918 send_channel->SetCpuOveruseDetection(cpu_overuse_detection);
2919 }
2920 }
wu@webrtc.orgde305012013-10-31 15:40:38 +00002921 if (dscp_option_changed) {
2922 talk_base::DiffServCodePoint dscp = talk_base::DSCP_DEFAULT;
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00002923 if (options_.dscp.GetWithDefaultIfUnset(false))
wu@webrtc.orgde305012013-10-31 15:40:38 +00002924 dscp = kVideoDscpValue;
2925 if (MediaChannel::SetDscp(dscp) != 0) {
2926 LOG(LS_WARNING) << "Failed to set DSCP settings for video channel";
2927 }
2928 }
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002929 if (suspend_below_min_bitrate_changed) {
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002930 if (options_.suspend_below_min_bitrate.GetWithDefaultIfUnset(false)) {
2931 for (SendChannelMap::iterator it = send_channels_.begin();
2932 it != send_channels_.end(); ++it) {
2933 engine()->vie()->codec()->SuspendBelowMinBitrate(
2934 it->second->channel_id());
2935 }
2936 } else {
2937 LOG(LS_WARNING) << "Cannot disable video suspension once it is enabled";
2938 }
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002939 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002940 return true;
2941}
2942
2943void WebRtcVideoMediaChannel::SetInterface(NetworkInterface* iface) {
2944 MediaChannel::SetInterface(iface);
2945 // Set the RTP recv/send buffer to a bigger size
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002946 MediaChannel::SetOption(NetworkInterface::ST_RTP,
2947 talk_base::Socket::OPT_RCVBUF,
2948 kVideoRtpBufferSize);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002949
2950 // TODO(sriniv): Remove or re-enable this.
2951 // As part of b/8030474, send-buffer is size now controlled through
2952 // portallocator flags.
2953 // network_interface_->SetOption(NetworkInterface::ST_RTP,
2954 // talk_base::Socket::OPT_SNDBUF,
2955 // kVideoRtpBufferSize);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002956}
2957
2958void WebRtcVideoMediaChannel::UpdateAspectRatio(int ratio_w, int ratio_h) {
2959 ASSERT(ratio_w != 0);
2960 ASSERT(ratio_h != 0);
2961 ratio_w_ = ratio_w;
2962 ratio_h_ = ratio_h;
2963 // For now assume that all streams want the same aspect ratio.
2964 // TODO(hellner): remove the need for this assumption.
2965 for (SendChannelMap::iterator iter = send_channels_.begin();
2966 iter != send_channels_.end(); ++iter) {
2967 WebRtcVideoChannelSendInfo* send_channel = iter->second;
2968 VideoCapturer* capturer = send_channel->video_capturer();
2969 if (capturer) {
2970 capturer->UpdateAspectRatio(ratio_w, ratio_h);
2971 }
2972 }
2973}
2974
2975bool WebRtcVideoMediaChannel::GetRenderer(uint32 ssrc,
2976 VideoRenderer** renderer) {
2977 RecvChannelMap::const_iterator it = recv_channels_.find(ssrc);
2978 if (it == recv_channels_.end()) {
2979 if (first_receive_ssrc_ == ssrc &&
2980 recv_channels_.find(0) != recv_channels_.end()) {
2981 LOG(LS_INFO) << " GetRenderer " << ssrc
2982 << " reuse default renderer #"
2983 << vie_channel_;
2984 *renderer = recv_channels_[0]->render_adapter()->renderer();
2985 return true;
2986 }
2987 return false;
2988 }
2989
2990 *renderer = it->second->render_adapter()->renderer();
2991 return true;
2992}
2993
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002994bool WebRtcVideoMediaChannel::GetVideoAdapter(
2995 uint32 ssrc, CoordinatedVideoAdapter** video_adapter) {
2996 SendChannelMap::iterator it = send_channels_.find(ssrc);
2997 if (it == send_channels_.end()) {
2998 return false;
2999 }
3000 *video_adapter = it->second->video_adapter();
3001 return true;
3002}
3003
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003004void WebRtcVideoMediaChannel::SendFrame(VideoCapturer* capturer,
3005 const VideoFrame* frame) {
wu@webrtc.org24301a62013-12-13 19:17:43 +00003006 // If the |capturer| is registered to any send channel, then send the frame
3007 // to those send channels.
3008 bool capturer_is_channel_owned = false;
3009 for (SendChannelMap::iterator iter = send_channels_.begin();
3010 iter != send_channels_.end(); ++iter) {
3011 WebRtcVideoChannelSendInfo* send_channel = iter->second;
3012 if (send_channel->video_capturer() == capturer) {
3013 SendFrame(send_channel, frame, capturer->IsScreencast());
3014 capturer_is_channel_owned = true;
3015 }
3016 }
3017 if (capturer_is_channel_owned) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003018 return;
3019 }
wu@webrtc.org24301a62013-12-13 19:17:43 +00003020
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003021 // TODO(hellner): Remove below for loop once the captured frame no longer
3022 // come from the engine, i.e. the engine no longer owns a capturer.
3023 for (SendChannelMap::iterator iter = send_channels_.begin();
3024 iter != send_channels_.end(); ++iter) {
3025 WebRtcVideoChannelSendInfo* send_channel = iter->second;
3026 if (send_channel->video_capturer() == NULL) {
3027 SendFrame(send_channel, frame, capturer->IsScreencast());
3028 }
3029 }
3030}
3031
3032bool WebRtcVideoMediaChannel::SendFrame(
3033 WebRtcVideoChannelSendInfo* send_channel,
3034 const VideoFrame* frame,
3035 bool is_screencast) {
3036 if (!send_channel) {
3037 return false;
3038 }
3039 if (!send_codec_) {
3040 // Send codec has not been set. No reason to process the frame any further.
3041 return false;
3042 }
3043 const VideoFormat& video_format = send_channel->video_format();
3044 // If the frame should be dropped.
3045 const bool video_format_set = video_format != cricket::VideoFormat();
3046 if (video_format_set &&
3047 (video_format.width == 0 && video_format.height == 0)) {
3048 return true;
3049 }
3050
3051 // Checks if we need to reset vie send codec.
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00003052 if (!MaybeResetVieSendCodec(send_channel,
3053 static_cast<int>(frame->GetWidth()),
3054 static_cast<int>(frame->GetHeight()),
3055 is_screencast, NULL)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003056 LOG(LS_ERROR) << "MaybeResetVieSendCodec failed with "
3057 << frame->GetWidth() << "x" << frame->GetHeight();
3058 return false;
3059 }
3060 const VideoFrame* frame_out = frame;
3061 talk_base::scoped_ptr<VideoFrame> processed_frame;
3062 // Disable muting for screencast.
3063 const bool mute = (send_channel->muted() && !is_screencast);
3064 send_channel->ProcessFrame(*frame_out, mute, processed_frame.use());
3065 if (processed_frame) {
3066 frame_out = processed_frame.get();
3067 }
3068
3069 webrtc::ViEVideoFrameI420 frame_i420;
3070 // TODO(ronghuawu): Update the webrtc::ViEVideoFrameI420
3071 // to use const unsigned char*
3072 frame_i420.y_plane = const_cast<unsigned char*>(frame_out->GetYPlane());
3073 frame_i420.u_plane = const_cast<unsigned char*>(frame_out->GetUPlane());
3074 frame_i420.v_plane = const_cast<unsigned char*>(frame_out->GetVPlane());
3075 frame_i420.y_pitch = frame_out->GetYPitch();
3076 frame_i420.u_pitch = frame_out->GetUPitch();
3077 frame_i420.v_pitch = frame_out->GetVPitch();
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00003078 frame_i420.width = static_cast<uint16>(frame_out->GetWidth());
3079 frame_i420.height = static_cast<uint16>(frame_out->GetHeight());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003080
3081 int64 timestamp_ntp_ms = 0;
3082 // TODO(justinlin): Reenable after Windows issues with clock drift are fixed.
3083 // Currently reverted to old behavior of discarding capture timestamp.
3084#if 0
3085 // If the frame timestamp is 0, we will use the deliver time.
3086 const int64 frame_timestamp = frame->GetTimeStamp();
3087 if (frame_timestamp != 0) {
3088 if (abs(time(NULL) - frame_timestamp / talk_base::kNumNanosecsPerSec) >
3089 kTimestampDeltaInSecondsForWarning) {
3090 LOG(LS_WARNING) << "Frame timestamp differs by more than "
3091 << kTimestampDeltaInSecondsForWarning << " seconds from "
3092 << "current Unix timestamp.";
3093 }
3094
3095 timestamp_ntp_ms =
3096 talk_base::UnixTimestampNanosecsToNtpMillisecs(frame_timestamp);
3097 }
3098#endif
3099
3100 return send_channel->external_capture()->IncomingFrameI420(
3101 frame_i420, timestamp_ntp_ms) == 0;
3102}
3103
3104bool WebRtcVideoMediaChannel::CreateChannel(uint32 ssrc_key,
3105 MediaDirection direction,
3106 int* channel_id) {
3107 // There are 3 types of channels. Sending only, receiving only and
3108 // sending and receiving. The sending and receiving channel is the
3109 // default channel and there is only one. All other channels that are created
3110 // are associated with the default channel which must exist. The default
3111 // channel id is stored in |vie_channel_|. All channels need to know about
3112 // the default channel to properly handle remb which is why there are
3113 // different ViE create channel calls.
3114 // For this channel the local and remote ssrc key is 0. However, it may
3115 // have a non-zero local and/or remote ssrc depending on if it is currently
3116 // sending and/or receiving.
3117 if ((vie_channel_ == -1 || direction == MD_SENDRECV) &&
3118 (!send_channels_.empty() || !recv_channels_.empty())) {
3119 ASSERT(false);
3120 return false;
3121 }
3122
3123 *channel_id = -1;
3124 if (direction == MD_RECV) {
3125 // All rec channels are associated with the default channel |vie_channel_|
3126 if (engine_->vie()->base()->CreateReceiveChannel(*channel_id,
3127 vie_channel_) != 0) {
3128 LOG_RTCERR2(CreateReceiveChannel, *channel_id, vie_channel_);
3129 return false;
3130 }
3131 } else if (direction == MD_SEND) {
3132 if (engine_->vie()->base()->CreateChannel(*channel_id,
3133 vie_channel_) != 0) {
3134 LOG_RTCERR2(CreateChannel, *channel_id, vie_channel_);
3135 return false;
3136 }
3137 } else {
3138 ASSERT(direction == MD_SENDRECV);
3139 if (engine_->vie()->base()->CreateChannel(*channel_id) != 0) {
3140 LOG_RTCERR1(CreateChannel, *channel_id);
3141 return false;
3142 }
3143 }
3144 if (!ConfigureChannel(*channel_id, direction, ssrc_key)) {
3145 engine_->vie()->base()->DeleteChannel(*channel_id);
3146 *channel_id = -1;
3147 return false;
3148 }
3149
3150 return true;
3151}
3152
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00003153bool WebRtcVideoMediaChannel::CreateUnsignalledRecvChannel(
3154 uint32 ssrc_key, int* out_channel_id) {
henrike@webrtc.org18e59112014-03-14 17:19:38 +00003155 int unsignalled_recv_channel_limit =
3156 options_.unsignalled_recv_stream_limit.GetWithDefaultIfUnset(
3157 kNumDefaultUnsignalledVideoRecvStreams);
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00003158 if (num_unsignalled_recv_channels_ >= unsignalled_recv_channel_limit) {
3159 return false;
3160 }
3161 if (!CreateChannel(ssrc_key, MD_RECV, out_channel_id)) {
3162 return false;
3163 }
3164 // TODO(tvsriram): Support dynamic sizing of unsignalled recv channels.
3165 num_unsignalled_recv_channels_++;
3166 return true;
3167}
3168
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003169bool WebRtcVideoMediaChannel::ConfigureChannel(int channel_id,
3170 MediaDirection direction,
3171 uint32 ssrc_key) {
3172 const bool receiving = (direction == MD_RECV) || (direction == MD_SENDRECV);
3173 const bool sending = (direction == MD_SEND) || (direction == MD_SENDRECV);
3174 // Register external transport.
3175 if (engine_->vie()->network()->RegisterSendTransport(
3176 channel_id, *this) != 0) {
3177 LOG_RTCERR1(RegisterSendTransport, channel_id);
3178 return false;
3179 }
3180
3181 // Set MTU.
3182 if (engine_->vie()->network()->SetMTU(channel_id, kVideoMtu) != 0) {
3183 LOG_RTCERR2(SetMTU, channel_id, kVideoMtu);
3184 return false;
3185 }
3186 // Turn on RTCP and loss feedback reporting.
3187 if (engine()->vie()->rtp()->SetRTCPStatus(
3188 channel_id, webrtc::kRtcpCompound_RFC4585) != 0) {
3189 LOG_RTCERR2(SetRTCPStatus, channel_id, webrtc::kRtcpCompound_RFC4585);
3190 return false;
3191 }
3192 // Enable pli as key frame request method.
3193 if (engine_->vie()->rtp()->SetKeyFrameRequestMethod(
3194 channel_id, webrtc::kViEKeyFrameRequestPliRtcp) != 0) {
3195 LOG_RTCERR2(SetKeyFrameRequestMethod,
3196 channel_id, webrtc::kViEKeyFrameRequestPliRtcp);
3197 return false;
3198 }
3199 if (!SetNackFec(channel_id, send_red_type_, send_fec_type_, nack_enabled_)) {
3200 // Logged in SetNackFec. Don't spam the logs.
3201 return false;
3202 }
3203 // Note that receiving must always be configured before sending to ensure
3204 // that send and receive channel is configured correctly (ConfigureReceiving
3205 // assumes no sending).
3206 if (receiving) {
3207 if (!ConfigureReceiving(channel_id, ssrc_key)) {
3208 return false;
3209 }
3210 }
3211 if (sending) {
3212 if (!ConfigureSending(channel_id, ssrc_key)) {
3213 return false;
3214 }
3215 }
3216
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00003217 // Start receiving for both receive and send channels so that we get incoming
3218 // RTP (if receiving) as well as RTCP feedback (if sending).
3219 if (engine()->vie()->base()->StartReceive(channel_id) != 0) {
3220 LOG_RTCERR1(StartReceive, channel_id);
3221 return false;
3222 }
3223
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003224 return true;
3225}
3226
3227bool WebRtcVideoMediaChannel::ConfigureReceiving(int channel_id,
3228 uint32 remote_ssrc_key) {
3229 // Make sure that an SSRC/key isn't registered more than once.
3230 if (recv_channels_.find(remote_ssrc_key) != recv_channels_.end()) {
3231 return false;
3232 }
3233 // Connect the voice channel, if there is one.
3234 // TODO(perkj): The A/V is synched by the receiving channel. So we need to
3235 // know the SSRC of the remote audio channel in order to fetch the correct
3236 // webrtc VoiceEngine channel. For now- only sync the default channel used
3237 // in 1-1 calls.
3238 if (remote_ssrc_key == 0 && voice_channel_) {
3239 WebRtcVoiceMediaChannel* voice_channel =
3240 static_cast<WebRtcVoiceMediaChannel*>(voice_channel_);
3241 if (engine_->vie()->base()->ConnectAudioChannel(
3242 vie_channel_, voice_channel->voe_channel()) != 0) {
3243 LOG_RTCERR2(ConnectAudioChannel, channel_id,
3244 voice_channel->voe_channel());
3245 LOG(LS_WARNING) << "A/V not synchronized";
3246 // Not a fatal error.
3247 }
3248 }
3249
3250 talk_base::scoped_ptr<WebRtcVideoChannelRecvInfo> channel_info(
3251 new WebRtcVideoChannelRecvInfo(channel_id));
3252
3253 // Install a render adapter.
3254 if (engine_->vie()->render()->AddRenderer(channel_id,
3255 webrtc::kVideoI420, channel_info->render_adapter()) != 0) {
3256 LOG_RTCERR3(AddRenderer, channel_id, webrtc::kVideoI420,
3257 channel_info->render_adapter());
3258 return false;
3259 }
3260
3261
3262 if (engine_->vie()->rtp()->SetRembStatus(channel_id,
3263 kNotSending,
3264 remb_enabled_) != 0) {
3265 LOG_RTCERR3(SetRembStatus, channel_id, kNotSending, remb_enabled_);
3266 return false;
3267 }
3268
3269 if (!SetHeaderExtension(&webrtc::ViERTP_RTCP::SetReceiveTimestampOffsetStatus,
3270 channel_id, receive_extensions_, kRtpTimestampOffsetHeaderExtension)) {
3271 return false;
3272 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003273 if (!SetHeaderExtension(
3274 &webrtc::ViERTP_RTCP::SetReceiveAbsoluteSendTimeStatus, channel_id,
henrike@webrtc.org79047f92014-03-06 23:46:59 +00003275 receive_extensions_, kRtpAbsoluteSenderTimeHeaderExtension)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003276 return false;
3277 }
3278
3279 if (remote_ssrc_key != 0) {
3280 // Use the same SSRC as our default channel
3281 // (so the RTCP reports are correct).
3282 unsigned int send_ssrc = 0;
3283 webrtc::ViERTP_RTCP* rtp = engine()->vie()->rtp();
3284 if (rtp->GetLocalSSRC(vie_channel_, send_ssrc) == -1) {
3285 LOG_RTCERR2(GetLocalSSRC, vie_channel_, send_ssrc);
3286 return false;
3287 }
3288 if (rtp->SetLocalSSRC(channel_id, send_ssrc) == -1) {
3289 LOG_RTCERR2(SetLocalSSRC, channel_id, send_ssrc);
3290 return false;
3291 }
3292 } // Else this is the the default channel and we don't change the SSRC.
3293
3294 // Disable color enhancement since it is a bit too aggressive.
3295 if (engine()->vie()->image()->EnableColorEnhancement(channel_id,
3296 false) != 0) {
3297 LOG_RTCERR1(EnableColorEnhancement, channel_id);
3298 return false;
3299 }
3300
3301 if (!SetReceiveCodecs(channel_info.get())) {
3302 return false;
3303 }
3304
3305 int buffer_latency =
3306 options_.buffered_mode_latency.GetWithDefaultIfUnset(
3307 cricket::kBufferedModeDisabled);
3308 if (buffer_latency != cricket::kBufferedModeDisabled) {
3309 if (engine()->vie()->rtp()->SetReceiverBufferingMode(
3310 channel_id, buffer_latency) != 0) {
3311 LOG_RTCERR2(SetReceiverBufferingMode, channel_id, buffer_latency);
3312 }
3313 }
3314
3315 if (render_started_) {
3316 if (engine_->vie()->render()->StartRender(channel_id) != 0) {
3317 LOG_RTCERR1(StartRender, channel_id);
3318 return false;
3319 }
3320 }
3321
3322 // Register decoder observer for incoming framerate and bitrate.
3323 if (engine()->vie()->codec()->RegisterDecoderObserver(
3324 channel_id, *channel_info->decoder_observer()) != 0) {
3325 LOG_RTCERR1(RegisterDecoderObserver, channel_info->decoder_observer());
3326 return false;
3327 }
3328
3329 recv_channels_[remote_ssrc_key] = channel_info.release();
3330 return true;
3331}
3332
3333bool WebRtcVideoMediaChannel::ConfigureSending(int channel_id,
3334 uint32 local_ssrc_key) {
3335 // The ssrc key can be zero or correspond to an SSRC.
3336 // Make sure the default channel isn't configured more than once.
3337 if (local_ssrc_key == 0 && send_channels_.find(0) != send_channels_.end()) {
3338 return false;
3339 }
3340 // Make sure that the SSRC is not already in use.
3341 uint32 dummy_key;
3342 if (GetSendChannelKey(local_ssrc_key, &dummy_key)) {
3343 return false;
3344 }
3345 int vie_capture = 0;
3346 webrtc::ViEExternalCapture* external_capture = NULL;
3347 // Register external capture.
3348 if (engine()->vie()->capture()->AllocateExternalCaptureDevice(
3349 vie_capture, external_capture) != 0) {
3350 LOG_RTCERR0(AllocateExternalCaptureDevice);
3351 return false;
3352 }
3353
3354 // Connect external capture.
3355 if (engine()->vie()->capture()->ConnectCaptureDevice(
3356 vie_capture, channel_id) != 0) {
3357 LOG_RTCERR2(ConnectCaptureDevice, vie_capture, channel_id);
3358 return false;
3359 }
3360 talk_base::scoped_ptr<WebRtcVideoChannelSendInfo> send_channel(
3361 new WebRtcVideoChannelSendInfo(channel_id, vie_capture,
3362 external_capture,
3363 engine()->cpu_monitor()));
3364 send_channel->ApplyCpuOptions(options_);
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00003365 send_channel->SignalCpuAdaptationUnable.connect(this,
3366 &WebRtcVideoMediaChannel::OnCpuAdaptationUnable);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003367
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00003368 if (options_.cpu_overuse_detection.GetWithDefaultIfUnset(false)) {
3369 send_channel->SetCpuOveruseDetection(true);
3370 }
3371
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003372 // Register encoder observer for outgoing framerate and bitrate.
3373 if (engine()->vie()->codec()->RegisterEncoderObserver(
3374 channel_id, *send_channel->encoder_observer()) != 0) {
3375 LOG_RTCERR1(RegisterEncoderObserver, send_channel->encoder_observer());
3376 return false;
3377 }
3378
3379 if (!SetHeaderExtension(&webrtc::ViERTP_RTCP::SetSendTimestampOffsetStatus,
3380 channel_id, send_extensions_, kRtpTimestampOffsetHeaderExtension)) {
3381 return false;
3382 }
3383
3384 if (!SetHeaderExtension(&webrtc::ViERTP_RTCP::SetSendAbsoluteSendTimeStatus,
henrike@webrtc.org79047f92014-03-06 23:46:59 +00003385 channel_id, send_extensions_, kRtpAbsoluteSenderTimeHeaderExtension)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003386 return false;
3387 }
3388
3389 if (options_.video_leaky_bucket.GetWithDefaultIfUnset(false)) {
3390 if (engine()->vie()->rtp()->SetTransmissionSmoothingStatus(channel_id,
3391 true) != 0) {
3392 LOG_RTCERR2(SetTransmissionSmoothingStatus, channel_id, true);
3393 return false;
3394 }
3395 }
3396
3397 int buffer_latency =
3398 options_.buffered_mode_latency.GetWithDefaultIfUnset(
3399 cricket::kBufferedModeDisabled);
3400 if (buffer_latency != cricket::kBufferedModeDisabled) {
3401 if (engine()->vie()->rtp()->SetSenderBufferingMode(
3402 channel_id, buffer_latency) != 0) {
3403 LOG_RTCERR2(SetSenderBufferingMode, channel_id, buffer_latency);
3404 }
3405 }
3406 // The remb status direction correspond to the RTP stream (and not the RTCP
3407 // stream). I.e. if send remb is enabled it means it is receiving remote
3408 // rembs and should use them to estimate bandwidth. Receive remb mean that
3409 // remb packets will be generated and that the channel should be included in
3410 // it. If remb is enabled all channels are allowed to contribute to the remb
3411 // but only receive channels will ever end up actually contributing. This
3412 // keeps the logic simple.
3413 if (engine_->vie()->rtp()->SetRembStatus(channel_id,
3414 remb_enabled_,
3415 remb_enabled_) != 0) {
3416 LOG_RTCERR3(SetRembStatus, channel_id, remb_enabled_, remb_enabled_);
3417 return false;
3418 }
3419 if (!SetNackFec(channel_id, send_red_type_, send_fec_type_, nack_enabled_)) {
3420 // Logged in SetNackFec. Don't spam the logs.
3421 return false;
3422 }
3423
3424 send_channels_[local_ssrc_key] = send_channel.release();
3425
3426 return true;
3427}
3428
3429bool WebRtcVideoMediaChannel::SetNackFec(int channel_id,
3430 int red_payload_type,
3431 int fec_payload_type,
3432 bool nack_enabled) {
3433 bool enable = (red_payload_type != -1 && fec_payload_type != -1 &&
3434 !InConferenceMode());
3435 if (enable) {
3436 if (engine_->vie()->rtp()->SetHybridNACKFECStatus(
3437 channel_id, nack_enabled, red_payload_type, fec_payload_type) != 0) {
3438 LOG_RTCERR4(SetHybridNACKFECStatus,
3439 channel_id, nack_enabled, red_payload_type, fec_payload_type);
3440 return false;
3441 }
3442 LOG(LS_INFO) << "Hybrid NACK/FEC enabled for channel " << channel_id;
3443 } else {
3444 if (engine_->vie()->rtp()->SetNACKStatus(channel_id, nack_enabled) != 0) {
3445 LOG_RTCERR1(SetNACKStatus, channel_id);
3446 return false;
3447 }
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00003448 std::string enabled = nack_enabled ? "enabled" : "disabled";
3449 LOG(LS_INFO) << "NACK " << enabled << " for channel " << channel_id;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003450 }
3451 return true;
3452}
3453
3454bool WebRtcVideoMediaChannel::SetSendCodec(const webrtc::VideoCodec& codec,
3455 int min_bitrate,
3456 int start_bitrate,
3457 int max_bitrate) {
3458 bool ret_val = true;
3459 for (SendChannelMap::iterator iter = send_channels_.begin();
3460 iter != send_channels_.end(); ++iter) {
3461 WebRtcVideoChannelSendInfo* send_channel = iter->second;
3462 ret_val = SetSendCodec(send_channel, codec, min_bitrate, start_bitrate,
3463 max_bitrate) && ret_val;
3464 }
3465 if (ret_val) {
3466 // All SetSendCodec calls were successful. Update the global state
3467 // accordingly.
3468 send_codec_.reset(new webrtc::VideoCodec(codec));
3469 send_min_bitrate_ = min_bitrate;
3470 send_start_bitrate_ = start_bitrate;
3471 send_max_bitrate_ = max_bitrate;
3472 } else {
3473 // At least one SetSendCodec call failed, rollback.
3474 for (SendChannelMap::iterator iter = send_channels_.begin();
3475 iter != send_channels_.end(); ++iter) {
3476 WebRtcVideoChannelSendInfo* send_channel = iter->second;
3477 if (send_codec_) {
3478 SetSendCodec(send_channel, *send_codec_.get(), send_min_bitrate_,
3479 send_start_bitrate_, send_max_bitrate_);
3480 }
3481 }
3482 }
3483 return ret_val;
3484}
3485
3486bool WebRtcVideoMediaChannel::SetSendCodec(
3487 WebRtcVideoChannelSendInfo* send_channel,
3488 const webrtc::VideoCodec& codec,
3489 int min_bitrate,
3490 int start_bitrate,
3491 int max_bitrate) {
3492 if (!send_channel) {
3493 return false;
3494 }
3495 const int channel_id = send_channel->channel_id();
3496 // Make a copy of the codec
3497 webrtc::VideoCodec target_codec = codec;
3498 target_codec.startBitrate = start_bitrate;
3499 target_codec.minBitrate = min_bitrate;
3500 target_codec.maxBitrate = max_bitrate;
3501
3502 // Set the default number of temporal layers for VP8.
3503 if (webrtc::kVideoCodecVP8 == codec.codecType) {
3504 target_codec.codecSpecific.VP8.numberOfTemporalLayers =
3505 kDefaultNumberOfTemporalLayers;
3506
3507 // Turn off the VP8 error resilience
3508 target_codec.codecSpecific.VP8.resilience = webrtc::kResilienceOff;
3509
3510 bool enable_denoising =
3511 options_.video_noise_reduction.GetWithDefaultIfUnset(false);
3512 target_codec.codecSpecific.VP8.denoisingOn = enable_denoising;
3513 }
3514
3515 // Register external encoder if codec type is supported by encoder factory.
3516 if (engine()->IsExternalEncoderCodecType(codec.codecType) &&
3517 !send_channel->IsEncoderRegistered(target_codec.plType)) {
3518 webrtc::VideoEncoder* encoder =
3519 engine()->CreateExternalEncoder(codec.codecType);
3520 if (encoder) {
3521 if (engine()->vie()->ext_codec()->RegisterExternalSendCodec(
3522 channel_id, target_codec.plType, encoder, false) == 0) {
3523 send_channel->RegisterEncoder(target_codec.plType, encoder);
3524 } else {
3525 LOG_RTCERR2(RegisterExternalSendCodec, channel_id, target_codec.plName);
3526 engine()->DestroyExternalEncoder(encoder);
3527 }
3528 }
3529 }
3530
3531 // Resolution and framerate may vary for different send channels.
3532 const VideoFormat& video_format = send_channel->video_format();
3533 UpdateVideoCodec(video_format, &target_codec);
3534
3535 if (target_codec.width == 0 && target_codec.height == 0) {
3536 const uint32 ssrc = send_channel->stream_params()->first_ssrc();
3537 LOG(LS_INFO) << "0x0 resolution selected. Captured frames will be dropped "
3538 << "for ssrc: " << ssrc << ".";
3539 } else {
3540 MaybeChangeStartBitrate(channel_id, &target_codec);
3541 if (0 != engine()->vie()->codec()->SetSendCodec(channel_id, target_codec)) {
3542 LOG_RTCERR2(SetSendCodec, channel_id, target_codec.plName);
3543 return false;
3544 }
3545
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00003546 // NOTE: SetRtxSendPayloadType must be called after all simulcast SSRCs
3547 // are configured. Otherwise ssrc's configured after this point will use
3548 // the primary PT for RTX.
3549 if (send_rtx_type_ != -1 &&
3550 engine()->vie()->rtp()->SetRtxSendPayloadType(channel_id,
3551 send_rtx_type_) != 0) {
3552 LOG_RTCERR2(SetRtxSendPayloadType, channel_id, send_rtx_type_);
3553 return false;
3554 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003555 }
3556 send_channel->set_interval(
3557 cricket::VideoFormat::FpsToInterval(target_codec.maxFramerate));
3558 return true;
3559}
3560
3561
3562static std::string ToString(webrtc::VideoCodecComplexity complexity) {
3563 switch (complexity) {
3564 case webrtc::kComplexityNormal:
3565 return "normal";
3566 case webrtc::kComplexityHigh:
3567 return "high";
3568 case webrtc::kComplexityHigher:
3569 return "higher";
3570 case webrtc::kComplexityMax:
3571 return "max";
3572 default:
3573 return "unknown";
3574 }
3575}
3576
3577static std::string ToString(webrtc::VP8ResilienceMode resilience) {
3578 switch (resilience) {
3579 case webrtc::kResilienceOff:
3580 return "off";
3581 case webrtc::kResilientStream:
3582 return "stream";
3583 case webrtc::kResilientFrames:
3584 return "frames";
3585 default:
3586 return "unknown";
3587 }
3588}
3589
3590void WebRtcVideoMediaChannel::LogSendCodecChange(const std::string& reason) {
3591 webrtc::VideoCodec vie_codec;
3592 if (engine()->vie()->codec()->GetSendCodec(vie_channel_, vie_codec) != 0) {
3593 LOG_RTCERR1(GetSendCodec, vie_channel_);
3594 return;
3595 }
3596
3597 LOG(LS_INFO) << reason << " : selected video codec "
3598 << vie_codec.plName << "/"
3599 << vie_codec.width << "x" << vie_codec.height << "x"
3600 << static_cast<int>(vie_codec.maxFramerate) << "fps"
3601 << "@" << vie_codec.maxBitrate << "kbps"
3602 << " (min=" << vie_codec.minBitrate << "kbps,"
3603 << " start=" << vie_codec.startBitrate << "kbps)";
3604 LOG(LS_INFO) << "Video max quantization: " << vie_codec.qpMax;
3605 if (webrtc::kVideoCodecVP8 == vie_codec.codecType) {
3606 LOG(LS_INFO) << "VP8 number of temporal layers: "
3607 << static_cast<int>(
3608 vie_codec.codecSpecific.VP8.numberOfTemporalLayers);
3609 LOG(LS_INFO) << "VP8 options : "
3610 << "picture loss indication = "
3611 << vie_codec.codecSpecific.VP8.pictureLossIndicationOn
3612 << ", feedback mode = "
3613 << vie_codec.codecSpecific.VP8.feedbackModeOn
3614 << ", complexity = "
3615 << ToString(vie_codec.codecSpecific.VP8.complexity)
3616 << ", resilience = "
3617 << ToString(vie_codec.codecSpecific.VP8.resilience)
3618 << ", denoising = "
3619 << vie_codec.codecSpecific.VP8.denoisingOn
3620 << ", error concealment = "
3621 << vie_codec.codecSpecific.VP8.errorConcealmentOn
3622 << ", automatic resize = "
3623 << vie_codec.codecSpecific.VP8.automaticResizeOn
3624 << ", frame dropping = "
3625 << vie_codec.codecSpecific.VP8.frameDroppingOn
3626 << ", key frame interval = "
3627 << vie_codec.codecSpecific.VP8.keyFrameInterval;
3628 }
3629
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00003630 if (send_rtx_type_ != -1) {
3631 LOG(LS_INFO) << "RTX payload type: " << send_rtx_type_;
3632 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003633}
3634
3635bool WebRtcVideoMediaChannel::SetReceiveCodecs(
3636 WebRtcVideoChannelRecvInfo* info) {
3637 int red_type = -1;
3638 int fec_type = -1;
3639 int channel_id = info->channel_id();
3640 for (std::vector<webrtc::VideoCodec>::iterator it = receive_codecs_.begin();
3641 it != receive_codecs_.end(); ++it) {
3642 if (it->codecType == webrtc::kVideoCodecRED) {
3643 red_type = it->plType;
3644 } else if (it->codecType == webrtc::kVideoCodecULPFEC) {
3645 fec_type = it->plType;
3646 }
3647 if (engine()->vie()->codec()->SetReceiveCodec(channel_id, *it) != 0) {
3648 LOG_RTCERR2(SetReceiveCodec, channel_id, it->plName);
3649 return false;
3650 }
3651 if (!info->IsDecoderRegistered(it->plType) &&
3652 it->codecType != webrtc::kVideoCodecRED &&
3653 it->codecType != webrtc::kVideoCodecULPFEC) {
3654 webrtc::VideoDecoder* decoder =
3655 engine()->CreateExternalDecoder(it->codecType);
3656 if (decoder) {
3657 if (engine()->vie()->ext_codec()->RegisterExternalReceiveCodec(
3658 channel_id, it->plType, decoder) == 0) {
3659 info->RegisterDecoder(it->plType, decoder);
3660 } else {
3661 LOG_RTCERR2(RegisterExternalReceiveCodec, channel_id, it->plName);
3662 engine()->DestroyExternalDecoder(decoder);
3663 }
3664 }
3665 }
3666 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003667 return true;
3668}
3669
3670int WebRtcVideoMediaChannel::GetRecvChannelNum(uint32 ssrc) {
3671 if (ssrc == first_receive_ssrc_) {
3672 return vie_channel_;
3673 }
3674 RecvChannelMap::iterator it = recv_channels_.find(ssrc);
3675 return (it != recv_channels_.end()) ? it->second->channel_id() : -1;
3676}
3677
3678// If the new frame size is different from the send codec size we set on vie,
3679// we need to reset the send codec on vie.
3680// The new send codec size should not exceed send_codec_ which is controlled
3681// only by the 'jec' logic.
3682bool WebRtcVideoMediaChannel::MaybeResetVieSendCodec(
3683 WebRtcVideoChannelSendInfo* send_channel,
3684 int new_width,
3685 int new_height,
3686 bool is_screencast,
3687 bool* reset) {
3688 if (reset) {
3689 *reset = false;
3690 }
3691 ASSERT(send_codec_.get() != NULL);
3692
3693 webrtc::VideoCodec target_codec = *send_codec_.get();
3694 const VideoFormat& video_format = send_channel->video_format();
3695 UpdateVideoCodec(video_format, &target_codec);
3696
3697 // Vie send codec size should not exceed target_codec.
3698 int target_width = new_width;
3699 int target_height = new_height;
3700 if (!is_screencast &&
3701 (new_width > target_codec.width || new_height > target_codec.height)) {
3702 target_width = target_codec.width;
3703 target_height = target_codec.height;
3704 }
3705
3706 // Get current vie codec.
3707 webrtc::VideoCodec vie_codec;
3708 const int channel_id = send_channel->channel_id();
3709 if (engine()->vie()->codec()->GetSendCodec(channel_id, vie_codec) != 0) {
3710 LOG_RTCERR1(GetSendCodec, channel_id);
3711 return false;
3712 }
3713 const int cur_width = vie_codec.width;
3714 const int cur_height = vie_codec.height;
3715
3716 // Only reset send codec when there is a size change. Additionally,
3717 // automatic resize needs to be turned off when screencasting and on when
3718 // not screencasting.
3719 // Don't allow automatic resizing for screencasting.
3720 bool automatic_resize = !is_screencast;
3721 // Turn off VP8 frame dropping when screensharing as the current model does
3722 // not work well at low fps.
3723 bool vp8_frame_dropping = !is_screencast;
3724 // Disable denoising for screencasting.
3725 bool enable_denoising =
3726 options_.video_noise_reduction.GetWithDefaultIfUnset(false);
3727 bool denoising = !is_screencast && enable_denoising;
3728 bool reset_send_codec =
3729 target_width != cur_width || target_height != cur_height ||
3730 automatic_resize != vie_codec.codecSpecific.VP8.automaticResizeOn ||
3731 denoising != vie_codec.codecSpecific.VP8.denoisingOn ||
3732 vp8_frame_dropping != vie_codec.codecSpecific.VP8.frameDroppingOn;
3733
3734 if (reset_send_codec) {
3735 // Set the new codec on vie.
3736 vie_codec.width = target_width;
3737 vie_codec.height = target_height;
3738 vie_codec.maxFramerate = target_codec.maxFramerate;
3739 vie_codec.startBitrate = target_codec.startBitrate;
3740 vie_codec.codecSpecific.VP8.automaticResizeOn = automatic_resize;
3741 vie_codec.codecSpecific.VP8.denoisingOn = denoising;
3742 vie_codec.codecSpecific.VP8.frameDroppingOn = vp8_frame_dropping;
3743 // TODO(mflodman): Remove 'is_screencast' check when screen cast settings
3744 // are treated correctly in WebRTC.
3745 if (!is_screencast)
3746 MaybeChangeStartBitrate(channel_id, &vie_codec);
3747
3748 if (engine()->vie()->codec()->SetSendCodec(channel_id, vie_codec) != 0) {
3749 LOG_RTCERR1(SetSendCodec, channel_id);
3750 return false;
3751 }
3752 if (reset) {
3753 *reset = true;
3754 }
3755 LogSendCodecChange("Capture size changed");
3756 }
3757
3758 return true;
3759}
3760
3761void WebRtcVideoMediaChannel::MaybeChangeStartBitrate(
3762 int channel_id, webrtc::VideoCodec* video_codec) {
3763 if (video_codec->startBitrate < video_codec->minBitrate) {
3764 video_codec->startBitrate = video_codec->minBitrate;
3765 } else if (video_codec->startBitrate > video_codec->maxBitrate) {
3766 video_codec->startBitrate = video_codec->maxBitrate;
3767 }
3768
3769 // Use a previous target bitrate, if there is one.
3770 unsigned int current_target_bitrate = 0;
3771 if (engine()->vie()->codec()->GetCodecTargetBitrate(
3772 channel_id, &current_target_bitrate) == 0) {
3773 // Convert to kbps.
3774 current_target_bitrate /= 1000;
3775 if (current_target_bitrate > video_codec->maxBitrate) {
3776 current_target_bitrate = video_codec->maxBitrate;
3777 }
3778 if (current_target_bitrate > video_codec->startBitrate) {
3779 video_codec->startBitrate = current_target_bitrate;
3780 }
3781 }
3782}
3783
3784void WebRtcVideoMediaChannel::OnMessage(talk_base::Message* msg) {
3785 FlushBlackFrameData* black_frame_data =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00003786 static_cast<FlushBlackFrameData*>(msg->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003787 FlushBlackFrame(black_frame_data->ssrc, black_frame_data->timestamp);
3788 delete black_frame_data;
3789}
3790
3791int WebRtcVideoMediaChannel::SendPacket(int channel, const void* data,
3792 int len) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003793 talk_base::Buffer packet(data, len, kMaxRtpPacketLen);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00003794 return MediaChannel::SendPacket(&packet) ? len : -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003795}
3796
3797int WebRtcVideoMediaChannel::SendRTCPPacket(int channel,
3798 const void* data,
3799 int len) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003800 talk_base::Buffer packet(data, len, kMaxRtpPacketLen);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00003801 return MediaChannel::SendRtcp(&packet) ? len : -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003802}
3803
3804void WebRtcVideoMediaChannel::QueueBlackFrame(uint32 ssrc, int64 timestamp,
3805 int framerate) {
3806 if (timestamp) {
3807 FlushBlackFrameData* black_frame_data = new FlushBlackFrameData(
3808 ssrc,
3809 timestamp);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00003810 const int delay_ms = static_cast<int>(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003811 2 * cricket::VideoFormat::FpsToInterval(framerate) *
3812 talk_base::kNumMillisecsPerSec / talk_base::kNumNanosecsPerSec);
3813 worker_thread()->PostDelayed(delay_ms, this, 0, black_frame_data);
3814 }
3815}
3816
3817void WebRtcVideoMediaChannel::FlushBlackFrame(uint32 ssrc, int64 timestamp) {
3818 WebRtcVideoChannelSendInfo* send_channel = GetSendChannel(ssrc);
3819 if (!send_channel) {
3820 return;
3821 }
3822 talk_base::scoped_ptr<const VideoFrame> black_frame_ptr;
3823
3824 const WebRtcLocalStreamInfo* channel_stream_info =
3825 send_channel->local_stream_info();
3826 int64 last_frame_time_stamp = channel_stream_info->time_stamp();
3827 if (last_frame_time_stamp == timestamp) {
3828 size_t last_frame_width = 0;
3829 size_t last_frame_height = 0;
3830 int64 last_frame_elapsed_time = 0;
3831 channel_stream_info->GetLastFrameInfo(&last_frame_width, &last_frame_height,
3832 &last_frame_elapsed_time);
3833 if (!last_frame_width || !last_frame_height) {
3834 return;
3835 }
3836 WebRtcVideoFrame black_frame;
3837 // Black frame is not screencast.
3838 const bool screencasting = false;
3839 const int64 timestamp_delta = send_channel->interval();
3840 if (!black_frame.InitToBlack(send_codec_->width, send_codec_->height, 1, 1,
3841 last_frame_elapsed_time + timestamp_delta,
3842 last_frame_time_stamp + timestamp_delta) ||
3843 !SendFrame(send_channel, &black_frame, screencasting)) {
3844 LOG(LS_ERROR) << "Failed to send black frame.";
3845 }
3846 }
3847}
3848
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00003849void WebRtcVideoMediaChannel::OnCpuAdaptationUnable() {
3850 // ssrc is hardcoded to 0. This message is based on a system wide issue,
3851 // so finding which ssrc caused it doesn't matter.
3852 SignalMediaError(0, VideoMediaChannel::ERROR_REC_CPU_MAX_CANT_DOWNGRADE);
3853}
3854
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003855void WebRtcVideoMediaChannel::SetNetworkTransmissionState(
3856 bool is_transmitting) {
3857 LOG(LS_INFO) << "SetNetworkTransmissionState: " << is_transmitting;
3858 for (SendChannelMap::iterator iter = send_channels_.begin();
3859 iter != send_channels_.end(); ++iter) {
3860 WebRtcVideoChannelSendInfo* send_channel = iter->second;
3861 int channel_id = send_channel->channel_id();
3862 engine_->vie()->network()->SetNetworkTransmissionState(channel_id,
3863 is_transmitting);
3864 }
3865}
3866
3867bool WebRtcVideoMediaChannel::SetHeaderExtension(ExtensionSetterFunction setter,
3868 int channel_id, const RtpHeaderExtension* extension) {
3869 bool enable = false;
3870 int id = 0;
3871 if (extension) {
3872 enable = true;
3873 id = extension->id;
3874 }
3875 if ((engine_->vie()->rtp()->*setter)(channel_id, enable, id) != 0) {
3876 LOG_RTCERR4(*setter, extension->uri, channel_id, enable, id);
3877 return false;
3878 }
3879 return true;
3880}
3881
3882bool WebRtcVideoMediaChannel::SetHeaderExtension(ExtensionSetterFunction setter,
3883 int channel_id, const std::vector<RtpHeaderExtension>& extensions,
3884 const char header_extension_uri[]) {
3885 const RtpHeaderExtension* extension = FindHeaderExtension(extensions,
3886 header_extension_uri);
3887 return SetHeaderExtension(setter, channel_id, extension);
3888}
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00003889
3890bool WebRtcVideoMediaChannel::SetLocalRtxSsrc(int channel_id,
3891 const StreamParams& send_params,
3892 uint32 primary_ssrc,
3893 int stream_idx) {
3894 uint32 rtx_ssrc = 0;
3895 bool has_rtx = send_params.GetFidSsrc(primary_ssrc, &rtx_ssrc);
3896 if (has_rtx && engine()->vie()->rtp()->SetLocalSSRC(
3897 channel_id, rtx_ssrc, webrtc::kViEStreamTypeRtx, stream_idx) != 0) {
3898 LOG_RTCERR4(SetLocalSSRC, channel_id, rtx_ssrc,
3899 webrtc::kViEStreamTypeRtx, stream_idx);
3900 return false;
3901 }
3902 return true;
3903}
3904
wu@webrtc.org24301a62013-12-13 19:17:43 +00003905void WebRtcVideoMediaChannel::MaybeConnectCapturer(VideoCapturer* capturer) {
3906 if (capturer != NULL && GetSendChannelNum(capturer) == 1) {
3907 capturer->SignalVideoFrame.connect(this,
3908 &WebRtcVideoMediaChannel::SendFrame);
3909 }
3910}
3911
3912void WebRtcVideoMediaChannel::MaybeDisconnectCapturer(VideoCapturer* capturer) {
3913 if (capturer != NULL && GetSendChannelNum(capturer) == 1) {
3914 capturer->SignalVideoFrame.disconnect(this);
3915 }
3916}
3917
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003918} // namespace cricket
3919
3920#endif // HAVE_WEBRTC_VIDEO