blob: f1cffa31dcc18a5637aea92c3d3117783a02aab5 [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.
2600 if (!InConferenceMode() ||
2601 !CreateUnsignalledRecvChannel(ssrc, &processing_channel)) {
2602 // If we cant find or allocate one, use the default.
2603 processing_channel = video_channel();
2604 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002605 }
2606
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00002607 engine()->vie()->network()->ReceivedRTPPacket(
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00002608 processing_channel,
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00002609 packet->data(),
wu@webrtc.orga9890802013-12-13 00:21:03 +00002610 static_cast<int>(packet->length()),
2611 webrtc::PacketTime(packet_time.timestamp, packet_time.not_before));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002612}
2613
wu@webrtc.orga9890802013-12-13 00:21:03 +00002614void WebRtcVideoMediaChannel::OnRtcpReceived(
2615 talk_base::Buffer* packet, const talk_base::PacketTime& packet_time) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002616// Sending channels need all RTCP packets with feedback information.
2617// Even sender reports can contain attached report blocks.
2618// Receiving channels need sender reports in order to create
2619// correct receiver reports.
2620
2621 uint32 ssrc = 0;
2622 if (!GetRtcpSsrc(packet->data(), packet->length(), &ssrc)) {
2623 LOG(LS_WARNING) << "Failed to parse SSRC from received RTCP packet";
2624 return;
2625 }
2626 int type = 0;
2627 if (!GetRtcpType(packet->data(), packet->length(), &type)) {
2628 LOG(LS_WARNING) << "Failed to parse type from received RTCP packet";
2629 return;
2630 }
2631
2632 // If it is a sender report, find the channel that is listening.
2633 if (type == kRtcpTypeSR) {
2634 int which_channel = GetRecvChannelNum(ssrc);
2635 if (which_channel != -1 && !IsDefaultChannel(which_channel)) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00002636 engine_->vie()->network()->ReceivedRTCPPacket(
2637 which_channel,
2638 packet->data(),
2639 static_cast<int>(packet->length()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002640 }
2641 }
2642 // SR may continue RR and any RR entry may correspond to any one of the send
2643 // channels. So all RTCP packets must be forwarded all send channels. ViE
2644 // will filter out RR internally.
2645 for (SendChannelMap::iterator iter = send_channels_.begin();
2646 iter != send_channels_.end(); ++iter) {
2647 WebRtcVideoChannelSendInfo* send_channel = iter->second;
2648 int channel_id = send_channel->channel_id();
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00002649 engine_->vie()->network()->ReceivedRTCPPacket(
2650 channel_id,
2651 packet->data(),
2652 static_cast<int>(packet->length()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002653 }
2654}
2655
2656void WebRtcVideoMediaChannel::OnReadyToSend(bool ready) {
2657 SetNetworkTransmissionState(ready);
2658}
2659
2660bool WebRtcVideoMediaChannel::MuteStream(uint32 ssrc, bool muted) {
2661 WebRtcVideoChannelSendInfo* send_channel = GetSendChannel(ssrc);
2662 if (!send_channel) {
2663 LOG(LS_ERROR) << "The specified ssrc " << ssrc << " is not in use.";
2664 return false;
2665 }
2666 send_channel->set_muted(muted);
2667 return true;
2668}
2669
2670bool WebRtcVideoMediaChannel::SetRecvRtpHeaderExtensions(
2671 const std::vector<RtpHeaderExtension>& extensions) {
2672 if (receive_extensions_ == extensions) {
2673 return true;
2674 }
2675 receive_extensions_ = extensions;
2676
2677 const RtpHeaderExtension* offset_extension =
2678 FindHeaderExtension(extensions, kRtpTimestampOffsetHeaderExtension);
2679 const RtpHeaderExtension* send_time_extension =
henrike@webrtc.org79047f92014-03-06 23:46:59 +00002680 FindHeaderExtension(extensions, kRtpAbsoluteSenderTimeHeaderExtension);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002681
2682 // Loop through all receive channels and enable/disable the extensions.
2683 for (RecvChannelMap::iterator channel_it = recv_channels_.begin();
2684 channel_it != recv_channels_.end(); ++channel_it) {
2685 int channel_id = channel_it->second->channel_id();
2686 if (!SetHeaderExtension(
2687 &webrtc::ViERTP_RTCP::SetReceiveTimestampOffsetStatus, channel_id,
2688 offset_extension)) {
2689 return false;
2690 }
2691 if (!SetHeaderExtension(
2692 &webrtc::ViERTP_RTCP::SetReceiveAbsoluteSendTimeStatus, channel_id,
2693 send_time_extension)) {
2694 return false;
2695 }
2696 }
2697 return true;
2698}
2699
2700bool WebRtcVideoMediaChannel::SetSendRtpHeaderExtensions(
2701 const std::vector<RtpHeaderExtension>& extensions) {
2702 send_extensions_ = extensions;
2703
2704 const RtpHeaderExtension* offset_extension =
2705 FindHeaderExtension(extensions, kRtpTimestampOffsetHeaderExtension);
2706 const RtpHeaderExtension* send_time_extension =
henrike@webrtc.org79047f92014-03-06 23:46:59 +00002707 FindHeaderExtension(extensions, kRtpAbsoluteSenderTimeHeaderExtension);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002708
2709 // Loop through all send channels and enable/disable the extensions.
2710 for (SendChannelMap::iterator channel_it = send_channels_.begin();
2711 channel_it != send_channels_.end(); ++channel_it) {
2712 int channel_id = channel_it->second->channel_id();
2713 if (!SetHeaderExtension(
2714 &webrtc::ViERTP_RTCP::SetSendTimestampOffsetStatus, channel_id,
2715 offset_extension)) {
2716 return false;
2717 }
2718 if (!SetHeaderExtension(
2719 &webrtc::ViERTP_RTCP::SetSendAbsoluteSendTimeStatus, channel_id,
2720 send_time_extension)) {
2721 return false;
2722 }
2723 }
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00002724
2725 if (send_time_extension) {
2726 // For video RTP packets, we would like to update AbsoluteSendTimeHeader
2727 // Extension closer to the network, @ socket level before sending.
2728 // Pushing the extension id to socket layer.
2729 MediaChannel::SetOption(NetworkInterface::ST_RTP,
2730 talk_base::Socket::OPT_RTP_SENDTIME_EXTN_ID,
2731 send_time_extension->id);
2732 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002733 return true;
2734}
2735
mallinath@webrtc.org92fdfeb2014-02-17 18:49:41 +00002736int WebRtcVideoMediaChannel::GetRtpSendTimeExtnId() const {
2737 const RtpHeaderExtension* send_time_extension = FindHeaderExtension(
henrike@webrtc.org79047f92014-03-06 23:46:59 +00002738 send_extensions_, kRtpAbsoluteSenderTimeHeaderExtension);
mallinath@webrtc.org92fdfeb2014-02-17 18:49:41 +00002739 if (send_time_extension) {
2740 return send_time_extension->id;
2741 }
2742 return -1;
2743}
2744
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002745bool WebRtcVideoMediaChannel::SetStartSendBandwidth(int bps) {
2746 LOG(LS_INFO) << "WebRtcVideoMediaChannel::SetStartSendBandwidth";
2747
2748 if (!send_codec_) {
2749 LOG(LS_INFO) << "The send codec has not been set up yet";
2750 return true;
2751 }
2752
2753 // On success, SetSendCodec() will reset |send_start_bitrate_| to |bps/1000|,
2754 // by calling MaybeChangeStartBitrate. That method will also clamp the
2755 // start bitrate between min and max, consistent with the override behavior
2756 // in SetMaxSendBandwidth.
2757 return SetSendCodec(*send_codec_,
2758 send_min_bitrate_, bps / 1000, send_max_bitrate_);
2759}
2760
2761bool WebRtcVideoMediaChannel::SetMaxSendBandwidth(int bps) {
2762 LOG(LS_INFO) << "WebRtcVideoMediaChannel::SetMaxSendBandwidth";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002763
2764 if (InConferenceMode()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002765 LOG(LS_INFO) << "Conference mode ignores SetMaxSendBandwidth";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002766 return true;
2767 }
2768
2769 if (!send_codec_) {
2770 LOG(LS_INFO) << "The send codec has not been set up yet";
2771 return true;
2772 }
2773
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002774 // Use the default value or the bps for the max
2775 int max_bitrate = (bps <= 0) ? send_max_bitrate_ : (bps / 1000);
2776
2777 // Reduce the current minimum and start bitrates if necessary.
2778 int min_bitrate = talk_base::_min(send_min_bitrate_, max_bitrate);
2779 int start_bitrate = talk_base::_min(send_start_bitrate_, max_bitrate);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002780
2781 if (!SetSendCodec(*send_codec_, min_bitrate, start_bitrate, max_bitrate)) {
2782 return false;
2783 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002784 LogSendCodecChange("SetMaxSendBandwidth()");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002785
2786 return true;
2787}
2788
2789bool WebRtcVideoMediaChannel::SetOptions(const VideoOptions &options) {
2790 // Always accept options that are unchanged.
2791 if (options_ == options) {
2792 return true;
2793 }
2794
2795 // Trigger SetSendCodec to set correct noise reduction state if the option has
2796 // changed.
2797 bool denoiser_changed = options.video_noise_reduction.IsSet() &&
2798 (options_.video_noise_reduction != options.video_noise_reduction);
2799
2800 bool leaky_bucket_changed = options.video_leaky_bucket.IsSet() &&
2801 (options_.video_leaky_bucket != options.video_leaky_bucket);
2802
2803 bool buffer_latency_changed = options.buffered_mode_latency.IsSet() &&
2804 (options_.buffered_mode_latency != options.buffered_mode_latency);
2805
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00002806 bool cpu_overuse_detection_changed = options.cpu_overuse_detection.IsSet() &&
2807 (options_.cpu_overuse_detection != options.cpu_overuse_detection);
2808
wu@webrtc.orgde305012013-10-31 15:40:38 +00002809 bool dscp_option_changed = (options_.dscp != options.dscp);
2810
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002811 bool suspend_below_min_bitrate_changed =
2812 options.suspend_below_min_bitrate.IsSet() &&
2813 (options_.suspend_below_min_bitrate != options.suspend_below_min_bitrate);
2814
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002815 bool conference_mode_turned_off = false;
2816 if (options_.conference_mode.IsSet() && options.conference_mode.IsSet() &&
2817 options_.conference_mode.GetWithDefaultIfUnset(false) &&
2818 !options.conference_mode.GetWithDefaultIfUnset(false)) {
2819 conference_mode_turned_off = true;
2820 }
2821
henrike@webrtc.org10bd88e2014-03-11 21:07:25 +00002822
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002823 // Save the options, to be interpreted where appropriate.
2824 // Use options_.SetAll() instead of assignment so that unset value in options
2825 // will not overwrite the previous option value.
2826 options_.SetAll(options);
2827
2828 // Set CPU options for all send channels.
2829 for (SendChannelMap::iterator iter = send_channels_.begin();
2830 iter != send_channels_.end(); ++iter) {
2831 WebRtcVideoChannelSendInfo* send_channel = iter->second;
2832 send_channel->ApplyCpuOptions(options_);
2833 }
2834
2835 // Adjust send codec bitrate if needed.
2836 int conf_max_bitrate = kDefaultConferenceModeMaxVideoBitrate;
2837
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00002838 // Save altered min_bitrate level and apply if necessary.
2839 bool adjusted_min_bitrate = false;
2840 if (options.lower_min_bitrate.IsSet()) {
2841 bool lower;
2842 options.lower_min_bitrate.Get(&lower);
2843
2844 int new_send_min_bitrate = lower ? kLowerMinBitrate : kMinVideoBitrate;
2845 adjusted_min_bitrate = (new_send_min_bitrate != send_min_bitrate_);
2846 send_min_bitrate_ = new_send_min_bitrate;
2847 }
2848
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002849 int expected_bitrate = send_max_bitrate_;
2850 if (InConferenceMode()) {
2851 expected_bitrate = conf_max_bitrate;
2852 } else if (conference_mode_turned_off) {
2853 // This is a special case for turning conference mode off.
2854 // Max bitrate should go back to the default maximum value instead
2855 // of the current maximum.
2856 expected_bitrate = kMaxVideoBitrate;
2857 }
2858
henrike@webrtc.org10bd88e2014-03-11 21:07:25 +00002859 bool reset_send_codec_needed = send_codec_ &&
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00002860 (send_max_bitrate_ != expected_bitrate || denoiser_changed ||
henrike@webrtc.org10bd88e2014-03-11 21:07:25 +00002861 adjusted_min_bitrate);
2862
2863
2864 if (reset_send_codec_needed) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002865 // On success, SetSendCodec() will reset send_max_bitrate_ to
2866 // expected_bitrate.
2867 if (!SetSendCodec(*send_codec_,
2868 send_min_bitrate_,
2869 send_start_bitrate_,
2870 expected_bitrate)) {
2871 return false;
2872 }
2873 LogSendCodecChange("SetOptions()");
2874 }
henrike@webrtc.org10bd88e2014-03-11 21:07:25 +00002875
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002876 if (leaky_bucket_changed) {
2877 bool enable_leaky_bucket =
2878 options_.video_leaky_bucket.GetWithDefaultIfUnset(false);
2879 for (SendChannelMap::iterator it = send_channels_.begin();
2880 it != send_channels_.end(); ++it) {
2881 if (engine()->vie()->rtp()->SetTransmissionSmoothingStatus(
2882 it->second->channel_id(), enable_leaky_bucket) != 0) {
2883 LOG_RTCERR2(SetTransmissionSmoothingStatus, it->second->channel_id(),
2884 enable_leaky_bucket);
2885 }
2886 }
2887 }
2888 if (buffer_latency_changed) {
2889 int buffer_latency =
2890 options_.buffered_mode_latency.GetWithDefaultIfUnset(
2891 cricket::kBufferedModeDisabled);
2892 for (SendChannelMap::iterator it = send_channels_.begin();
2893 it != send_channels_.end(); ++it) {
2894 if (engine()->vie()->rtp()->SetSenderBufferingMode(
2895 it->second->channel_id(), buffer_latency) != 0) {
2896 LOG_RTCERR2(SetSenderBufferingMode, it->second->channel_id(),
2897 buffer_latency);
2898 }
2899 }
2900 for (RecvChannelMap::iterator it = recv_channels_.begin();
2901 it != recv_channels_.end(); ++it) {
2902 if (engine()->vie()->rtp()->SetReceiverBufferingMode(
2903 it->second->channel_id(), buffer_latency) != 0) {
2904 LOG_RTCERR2(SetReceiverBufferingMode, it->second->channel_id(),
2905 buffer_latency);
2906 }
2907 }
2908 }
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00002909 if (cpu_overuse_detection_changed) {
2910 bool cpu_overuse_detection =
2911 options_.cpu_overuse_detection.GetWithDefaultIfUnset(false);
2912 for (SendChannelMap::iterator iter = send_channels_.begin();
2913 iter != send_channels_.end(); ++iter) {
2914 WebRtcVideoChannelSendInfo* send_channel = iter->second;
2915 send_channel->SetCpuOveruseDetection(cpu_overuse_detection);
2916 }
2917 }
wu@webrtc.orgde305012013-10-31 15:40:38 +00002918 if (dscp_option_changed) {
2919 talk_base::DiffServCodePoint dscp = talk_base::DSCP_DEFAULT;
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00002920 if (options_.dscp.GetWithDefaultIfUnset(false))
wu@webrtc.orgde305012013-10-31 15:40:38 +00002921 dscp = kVideoDscpValue;
2922 if (MediaChannel::SetDscp(dscp) != 0) {
2923 LOG(LS_WARNING) << "Failed to set DSCP settings for video channel";
2924 }
2925 }
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002926 if (suspend_below_min_bitrate_changed) {
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002927 if (options_.suspend_below_min_bitrate.GetWithDefaultIfUnset(false)) {
2928 for (SendChannelMap::iterator it = send_channels_.begin();
2929 it != send_channels_.end(); ++it) {
2930 engine()->vie()->codec()->SuspendBelowMinBitrate(
2931 it->second->channel_id());
2932 }
2933 } else {
2934 LOG(LS_WARNING) << "Cannot disable video suspension once it is enabled";
2935 }
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002936 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002937 return true;
2938}
2939
2940void WebRtcVideoMediaChannel::SetInterface(NetworkInterface* iface) {
2941 MediaChannel::SetInterface(iface);
2942 // Set the RTP recv/send buffer to a bigger size
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002943 MediaChannel::SetOption(NetworkInterface::ST_RTP,
2944 talk_base::Socket::OPT_RCVBUF,
2945 kVideoRtpBufferSize);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002946
2947 // TODO(sriniv): Remove or re-enable this.
2948 // As part of b/8030474, send-buffer is size now controlled through
2949 // portallocator flags.
2950 // network_interface_->SetOption(NetworkInterface::ST_RTP,
2951 // talk_base::Socket::OPT_SNDBUF,
2952 // kVideoRtpBufferSize);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002953}
2954
2955void WebRtcVideoMediaChannel::UpdateAspectRatio(int ratio_w, int ratio_h) {
2956 ASSERT(ratio_w != 0);
2957 ASSERT(ratio_h != 0);
2958 ratio_w_ = ratio_w;
2959 ratio_h_ = ratio_h;
2960 // For now assume that all streams want the same aspect ratio.
2961 // TODO(hellner): remove the need for this assumption.
2962 for (SendChannelMap::iterator iter = send_channels_.begin();
2963 iter != send_channels_.end(); ++iter) {
2964 WebRtcVideoChannelSendInfo* send_channel = iter->second;
2965 VideoCapturer* capturer = send_channel->video_capturer();
2966 if (capturer) {
2967 capturer->UpdateAspectRatio(ratio_w, ratio_h);
2968 }
2969 }
2970}
2971
2972bool WebRtcVideoMediaChannel::GetRenderer(uint32 ssrc,
2973 VideoRenderer** renderer) {
2974 RecvChannelMap::const_iterator it = recv_channels_.find(ssrc);
2975 if (it == recv_channels_.end()) {
2976 if (first_receive_ssrc_ == ssrc &&
2977 recv_channels_.find(0) != recv_channels_.end()) {
2978 LOG(LS_INFO) << " GetRenderer " << ssrc
2979 << " reuse default renderer #"
2980 << vie_channel_;
2981 *renderer = recv_channels_[0]->render_adapter()->renderer();
2982 return true;
2983 }
2984 return false;
2985 }
2986
2987 *renderer = it->second->render_adapter()->renderer();
2988 return true;
2989}
2990
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002991bool WebRtcVideoMediaChannel::GetVideoAdapter(
2992 uint32 ssrc, CoordinatedVideoAdapter** video_adapter) {
2993 SendChannelMap::iterator it = send_channels_.find(ssrc);
2994 if (it == send_channels_.end()) {
2995 return false;
2996 }
2997 *video_adapter = it->second->video_adapter();
2998 return true;
2999}
3000
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003001void WebRtcVideoMediaChannel::SendFrame(VideoCapturer* capturer,
3002 const VideoFrame* frame) {
wu@webrtc.org24301a62013-12-13 19:17:43 +00003003 // If the |capturer| is registered to any send channel, then send the frame
3004 // to those send channels.
3005 bool capturer_is_channel_owned = false;
3006 for (SendChannelMap::iterator iter = send_channels_.begin();
3007 iter != send_channels_.end(); ++iter) {
3008 WebRtcVideoChannelSendInfo* send_channel = iter->second;
3009 if (send_channel->video_capturer() == capturer) {
3010 SendFrame(send_channel, frame, capturer->IsScreencast());
3011 capturer_is_channel_owned = true;
3012 }
3013 }
3014 if (capturer_is_channel_owned) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003015 return;
3016 }
wu@webrtc.org24301a62013-12-13 19:17:43 +00003017
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003018 // TODO(hellner): Remove below for loop once the captured frame no longer
3019 // come from the engine, i.e. the engine no longer owns a capturer.
3020 for (SendChannelMap::iterator iter = send_channels_.begin();
3021 iter != send_channels_.end(); ++iter) {
3022 WebRtcVideoChannelSendInfo* send_channel = iter->second;
3023 if (send_channel->video_capturer() == NULL) {
3024 SendFrame(send_channel, frame, capturer->IsScreencast());
3025 }
3026 }
3027}
3028
3029bool WebRtcVideoMediaChannel::SendFrame(
3030 WebRtcVideoChannelSendInfo* send_channel,
3031 const VideoFrame* frame,
3032 bool is_screencast) {
3033 if (!send_channel) {
3034 return false;
3035 }
3036 if (!send_codec_) {
3037 // Send codec has not been set. No reason to process the frame any further.
3038 return false;
3039 }
3040 const VideoFormat& video_format = send_channel->video_format();
3041 // If the frame should be dropped.
3042 const bool video_format_set = video_format != cricket::VideoFormat();
3043 if (video_format_set &&
3044 (video_format.width == 0 && video_format.height == 0)) {
3045 return true;
3046 }
3047
3048 // Checks if we need to reset vie send codec.
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00003049 if (!MaybeResetVieSendCodec(send_channel,
3050 static_cast<int>(frame->GetWidth()),
3051 static_cast<int>(frame->GetHeight()),
3052 is_screencast, NULL)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003053 LOG(LS_ERROR) << "MaybeResetVieSendCodec failed with "
3054 << frame->GetWidth() << "x" << frame->GetHeight();
3055 return false;
3056 }
3057 const VideoFrame* frame_out = frame;
3058 talk_base::scoped_ptr<VideoFrame> processed_frame;
3059 // Disable muting for screencast.
3060 const bool mute = (send_channel->muted() && !is_screencast);
3061 send_channel->ProcessFrame(*frame_out, mute, processed_frame.use());
3062 if (processed_frame) {
3063 frame_out = processed_frame.get();
3064 }
3065
3066 webrtc::ViEVideoFrameI420 frame_i420;
3067 // TODO(ronghuawu): Update the webrtc::ViEVideoFrameI420
3068 // to use const unsigned char*
3069 frame_i420.y_plane = const_cast<unsigned char*>(frame_out->GetYPlane());
3070 frame_i420.u_plane = const_cast<unsigned char*>(frame_out->GetUPlane());
3071 frame_i420.v_plane = const_cast<unsigned char*>(frame_out->GetVPlane());
3072 frame_i420.y_pitch = frame_out->GetYPitch();
3073 frame_i420.u_pitch = frame_out->GetUPitch();
3074 frame_i420.v_pitch = frame_out->GetVPitch();
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00003075 frame_i420.width = static_cast<uint16>(frame_out->GetWidth());
3076 frame_i420.height = static_cast<uint16>(frame_out->GetHeight());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003077
3078 int64 timestamp_ntp_ms = 0;
3079 // TODO(justinlin): Reenable after Windows issues with clock drift are fixed.
3080 // Currently reverted to old behavior of discarding capture timestamp.
3081#if 0
3082 // If the frame timestamp is 0, we will use the deliver time.
3083 const int64 frame_timestamp = frame->GetTimeStamp();
3084 if (frame_timestamp != 0) {
3085 if (abs(time(NULL) - frame_timestamp / talk_base::kNumNanosecsPerSec) >
3086 kTimestampDeltaInSecondsForWarning) {
3087 LOG(LS_WARNING) << "Frame timestamp differs by more than "
3088 << kTimestampDeltaInSecondsForWarning << " seconds from "
3089 << "current Unix timestamp.";
3090 }
3091
3092 timestamp_ntp_ms =
3093 talk_base::UnixTimestampNanosecsToNtpMillisecs(frame_timestamp);
3094 }
3095#endif
3096
3097 return send_channel->external_capture()->IncomingFrameI420(
3098 frame_i420, timestamp_ntp_ms) == 0;
3099}
3100
3101bool WebRtcVideoMediaChannel::CreateChannel(uint32 ssrc_key,
3102 MediaDirection direction,
3103 int* channel_id) {
3104 // There are 3 types of channels. Sending only, receiving only and
3105 // sending and receiving. The sending and receiving channel is the
3106 // default channel and there is only one. All other channels that are created
3107 // are associated with the default channel which must exist. The default
3108 // channel id is stored in |vie_channel_|. All channels need to know about
3109 // the default channel to properly handle remb which is why there are
3110 // different ViE create channel calls.
3111 // For this channel the local and remote ssrc key is 0. However, it may
3112 // have a non-zero local and/or remote ssrc depending on if it is currently
3113 // sending and/or receiving.
3114 if ((vie_channel_ == -1 || direction == MD_SENDRECV) &&
3115 (!send_channels_.empty() || !recv_channels_.empty())) {
3116 ASSERT(false);
3117 return false;
3118 }
3119
3120 *channel_id = -1;
3121 if (direction == MD_RECV) {
3122 // All rec channels are associated with the default channel |vie_channel_|
3123 if (engine_->vie()->base()->CreateReceiveChannel(*channel_id,
3124 vie_channel_) != 0) {
3125 LOG_RTCERR2(CreateReceiveChannel, *channel_id, vie_channel_);
3126 return false;
3127 }
3128 } else if (direction == MD_SEND) {
3129 if (engine_->vie()->base()->CreateChannel(*channel_id,
3130 vie_channel_) != 0) {
3131 LOG_RTCERR2(CreateChannel, *channel_id, vie_channel_);
3132 return false;
3133 }
3134 } else {
3135 ASSERT(direction == MD_SENDRECV);
3136 if (engine_->vie()->base()->CreateChannel(*channel_id) != 0) {
3137 LOG_RTCERR1(CreateChannel, *channel_id);
3138 return false;
3139 }
3140 }
3141 if (!ConfigureChannel(*channel_id, direction, ssrc_key)) {
3142 engine_->vie()->base()->DeleteChannel(*channel_id);
3143 *channel_id = -1;
3144 return false;
3145 }
3146
3147 return true;
3148}
3149
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00003150bool WebRtcVideoMediaChannel::CreateUnsignalledRecvChannel(
3151 uint32 ssrc_key, int* out_channel_id) {
henrike@webrtc.org806768a2014-02-27 21:03:09 +00003152 int unsignalled_recv_channel_limit = 0;
3153 // TODO(tvsriram): Enable this once we fix handling packets
3154 // in default channel with unsignalled recv.
3155 // options_.unsignalled_recv_stream_limit.GetWithDefaultIfUnset(
3156 // kNumDefaultUnsignalledVideoRecvStreams);
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00003157 if (num_unsignalled_recv_channels_ >= unsignalled_recv_channel_limit) {
3158 return false;
3159 }
3160 if (!CreateChannel(ssrc_key, MD_RECV, out_channel_id)) {
3161 return false;
3162 }
3163 // TODO(tvsriram): Support dynamic sizing of unsignalled recv channels.
3164 num_unsignalled_recv_channels_++;
3165 return true;
3166}
3167
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003168bool WebRtcVideoMediaChannel::ConfigureChannel(int channel_id,
3169 MediaDirection direction,
3170 uint32 ssrc_key) {
3171 const bool receiving = (direction == MD_RECV) || (direction == MD_SENDRECV);
3172 const bool sending = (direction == MD_SEND) || (direction == MD_SENDRECV);
3173 // Register external transport.
3174 if (engine_->vie()->network()->RegisterSendTransport(
3175 channel_id, *this) != 0) {
3176 LOG_RTCERR1(RegisterSendTransport, channel_id);
3177 return false;
3178 }
3179
3180 // Set MTU.
3181 if (engine_->vie()->network()->SetMTU(channel_id, kVideoMtu) != 0) {
3182 LOG_RTCERR2(SetMTU, channel_id, kVideoMtu);
3183 return false;
3184 }
3185 // Turn on RTCP and loss feedback reporting.
3186 if (engine()->vie()->rtp()->SetRTCPStatus(
3187 channel_id, webrtc::kRtcpCompound_RFC4585) != 0) {
3188 LOG_RTCERR2(SetRTCPStatus, channel_id, webrtc::kRtcpCompound_RFC4585);
3189 return false;
3190 }
3191 // Enable pli as key frame request method.
3192 if (engine_->vie()->rtp()->SetKeyFrameRequestMethod(
3193 channel_id, webrtc::kViEKeyFrameRequestPliRtcp) != 0) {
3194 LOG_RTCERR2(SetKeyFrameRequestMethod,
3195 channel_id, webrtc::kViEKeyFrameRequestPliRtcp);
3196 return false;
3197 }
3198 if (!SetNackFec(channel_id, send_red_type_, send_fec_type_, nack_enabled_)) {
3199 // Logged in SetNackFec. Don't spam the logs.
3200 return false;
3201 }
3202 // Note that receiving must always be configured before sending to ensure
3203 // that send and receive channel is configured correctly (ConfigureReceiving
3204 // assumes no sending).
3205 if (receiving) {
3206 if (!ConfigureReceiving(channel_id, ssrc_key)) {
3207 return false;
3208 }
3209 }
3210 if (sending) {
3211 if (!ConfigureSending(channel_id, ssrc_key)) {
3212 return false;
3213 }
3214 }
3215
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00003216 // Start receiving for both receive and send channels so that we get incoming
3217 // RTP (if receiving) as well as RTCP feedback (if sending).
3218 if (engine()->vie()->base()->StartReceive(channel_id) != 0) {
3219 LOG_RTCERR1(StartReceive, channel_id);
3220 return false;
3221 }
3222
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003223 return true;
3224}
3225
3226bool WebRtcVideoMediaChannel::ConfigureReceiving(int channel_id,
3227 uint32 remote_ssrc_key) {
3228 // Make sure that an SSRC/key isn't registered more than once.
3229 if (recv_channels_.find(remote_ssrc_key) != recv_channels_.end()) {
3230 return false;
3231 }
3232 // Connect the voice channel, if there is one.
3233 // TODO(perkj): The A/V is synched by the receiving channel. So we need to
3234 // know the SSRC of the remote audio channel in order to fetch the correct
3235 // webrtc VoiceEngine channel. For now- only sync the default channel used
3236 // in 1-1 calls.
3237 if (remote_ssrc_key == 0 && voice_channel_) {
3238 WebRtcVoiceMediaChannel* voice_channel =
3239 static_cast<WebRtcVoiceMediaChannel*>(voice_channel_);
3240 if (engine_->vie()->base()->ConnectAudioChannel(
3241 vie_channel_, voice_channel->voe_channel()) != 0) {
3242 LOG_RTCERR2(ConnectAudioChannel, channel_id,
3243 voice_channel->voe_channel());
3244 LOG(LS_WARNING) << "A/V not synchronized";
3245 // Not a fatal error.
3246 }
3247 }
3248
3249 talk_base::scoped_ptr<WebRtcVideoChannelRecvInfo> channel_info(
3250 new WebRtcVideoChannelRecvInfo(channel_id));
3251
3252 // Install a render adapter.
3253 if (engine_->vie()->render()->AddRenderer(channel_id,
3254 webrtc::kVideoI420, channel_info->render_adapter()) != 0) {
3255 LOG_RTCERR3(AddRenderer, channel_id, webrtc::kVideoI420,
3256 channel_info->render_adapter());
3257 return false;
3258 }
3259
3260
3261 if (engine_->vie()->rtp()->SetRembStatus(channel_id,
3262 kNotSending,
3263 remb_enabled_) != 0) {
3264 LOG_RTCERR3(SetRembStatus, channel_id, kNotSending, remb_enabled_);
3265 return false;
3266 }
3267
3268 if (!SetHeaderExtension(&webrtc::ViERTP_RTCP::SetReceiveTimestampOffsetStatus,
3269 channel_id, receive_extensions_, kRtpTimestampOffsetHeaderExtension)) {
3270 return false;
3271 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003272 if (!SetHeaderExtension(
3273 &webrtc::ViERTP_RTCP::SetReceiveAbsoluteSendTimeStatus, channel_id,
henrike@webrtc.org79047f92014-03-06 23:46:59 +00003274 receive_extensions_, kRtpAbsoluteSenderTimeHeaderExtension)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003275 return false;
3276 }
3277
3278 if (remote_ssrc_key != 0) {
3279 // Use the same SSRC as our default channel
3280 // (so the RTCP reports are correct).
3281 unsigned int send_ssrc = 0;
3282 webrtc::ViERTP_RTCP* rtp = engine()->vie()->rtp();
3283 if (rtp->GetLocalSSRC(vie_channel_, send_ssrc) == -1) {
3284 LOG_RTCERR2(GetLocalSSRC, vie_channel_, send_ssrc);
3285 return false;
3286 }
3287 if (rtp->SetLocalSSRC(channel_id, send_ssrc) == -1) {
3288 LOG_RTCERR2(SetLocalSSRC, channel_id, send_ssrc);
3289 return false;
3290 }
3291 } // Else this is the the default channel and we don't change the SSRC.
3292
3293 // Disable color enhancement since it is a bit too aggressive.
3294 if (engine()->vie()->image()->EnableColorEnhancement(channel_id,
3295 false) != 0) {
3296 LOG_RTCERR1(EnableColorEnhancement, channel_id);
3297 return false;
3298 }
3299
3300 if (!SetReceiveCodecs(channel_info.get())) {
3301 return false;
3302 }
3303
3304 int buffer_latency =
3305 options_.buffered_mode_latency.GetWithDefaultIfUnset(
3306 cricket::kBufferedModeDisabled);
3307 if (buffer_latency != cricket::kBufferedModeDisabled) {
3308 if (engine()->vie()->rtp()->SetReceiverBufferingMode(
3309 channel_id, buffer_latency) != 0) {
3310 LOG_RTCERR2(SetReceiverBufferingMode, channel_id, buffer_latency);
3311 }
3312 }
3313
3314 if (render_started_) {
3315 if (engine_->vie()->render()->StartRender(channel_id) != 0) {
3316 LOG_RTCERR1(StartRender, channel_id);
3317 return false;
3318 }
3319 }
3320
3321 // Register decoder observer for incoming framerate and bitrate.
3322 if (engine()->vie()->codec()->RegisterDecoderObserver(
3323 channel_id, *channel_info->decoder_observer()) != 0) {
3324 LOG_RTCERR1(RegisterDecoderObserver, channel_info->decoder_observer());
3325 return false;
3326 }
3327
3328 recv_channels_[remote_ssrc_key] = channel_info.release();
3329 return true;
3330}
3331
3332bool WebRtcVideoMediaChannel::ConfigureSending(int channel_id,
3333 uint32 local_ssrc_key) {
3334 // The ssrc key can be zero or correspond to an SSRC.
3335 // Make sure the default channel isn't configured more than once.
3336 if (local_ssrc_key == 0 && send_channels_.find(0) != send_channels_.end()) {
3337 return false;
3338 }
3339 // Make sure that the SSRC is not already in use.
3340 uint32 dummy_key;
3341 if (GetSendChannelKey(local_ssrc_key, &dummy_key)) {
3342 return false;
3343 }
3344 int vie_capture = 0;
3345 webrtc::ViEExternalCapture* external_capture = NULL;
3346 // Register external capture.
3347 if (engine()->vie()->capture()->AllocateExternalCaptureDevice(
3348 vie_capture, external_capture) != 0) {
3349 LOG_RTCERR0(AllocateExternalCaptureDevice);
3350 return false;
3351 }
3352
3353 // Connect external capture.
3354 if (engine()->vie()->capture()->ConnectCaptureDevice(
3355 vie_capture, channel_id) != 0) {
3356 LOG_RTCERR2(ConnectCaptureDevice, vie_capture, channel_id);
3357 return false;
3358 }
3359 talk_base::scoped_ptr<WebRtcVideoChannelSendInfo> send_channel(
3360 new WebRtcVideoChannelSendInfo(channel_id, vie_capture,
3361 external_capture,
3362 engine()->cpu_monitor()));
3363 send_channel->ApplyCpuOptions(options_);
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00003364 send_channel->SignalCpuAdaptationUnable.connect(this,
3365 &WebRtcVideoMediaChannel::OnCpuAdaptationUnable);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003366
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00003367 if (options_.cpu_overuse_detection.GetWithDefaultIfUnset(false)) {
3368 send_channel->SetCpuOveruseDetection(true);
3369 }
3370
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003371 // Register encoder observer for outgoing framerate and bitrate.
3372 if (engine()->vie()->codec()->RegisterEncoderObserver(
3373 channel_id, *send_channel->encoder_observer()) != 0) {
3374 LOG_RTCERR1(RegisterEncoderObserver, send_channel->encoder_observer());
3375 return false;
3376 }
3377
3378 if (!SetHeaderExtension(&webrtc::ViERTP_RTCP::SetSendTimestampOffsetStatus,
3379 channel_id, send_extensions_, kRtpTimestampOffsetHeaderExtension)) {
3380 return false;
3381 }
3382
3383 if (!SetHeaderExtension(&webrtc::ViERTP_RTCP::SetSendAbsoluteSendTimeStatus,
henrike@webrtc.org79047f92014-03-06 23:46:59 +00003384 channel_id, send_extensions_, kRtpAbsoluteSenderTimeHeaderExtension)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003385 return false;
3386 }
3387
3388 if (options_.video_leaky_bucket.GetWithDefaultIfUnset(false)) {
3389 if (engine()->vie()->rtp()->SetTransmissionSmoothingStatus(channel_id,
3390 true) != 0) {
3391 LOG_RTCERR2(SetTransmissionSmoothingStatus, channel_id, true);
3392 return false;
3393 }
3394 }
3395
3396 int buffer_latency =
3397 options_.buffered_mode_latency.GetWithDefaultIfUnset(
3398 cricket::kBufferedModeDisabled);
3399 if (buffer_latency != cricket::kBufferedModeDisabled) {
3400 if (engine()->vie()->rtp()->SetSenderBufferingMode(
3401 channel_id, buffer_latency) != 0) {
3402 LOG_RTCERR2(SetSenderBufferingMode, channel_id, buffer_latency);
3403 }
3404 }
3405 // The remb status direction correspond to the RTP stream (and not the RTCP
3406 // stream). I.e. if send remb is enabled it means it is receiving remote
3407 // rembs and should use them to estimate bandwidth. Receive remb mean that
3408 // remb packets will be generated and that the channel should be included in
3409 // it. If remb is enabled all channels are allowed to contribute to the remb
3410 // but only receive channels will ever end up actually contributing. This
3411 // keeps the logic simple.
3412 if (engine_->vie()->rtp()->SetRembStatus(channel_id,
3413 remb_enabled_,
3414 remb_enabled_) != 0) {
3415 LOG_RTCERR3(SetRembStatus, channel_id, remb_enabled_, remb_enabled_);
3416 return false;
3417 }
3418 if (!SetNackFec(channel_id, send_red_type_, send_fec_type_, nack_enabled_)) {
3419 // Logged in SetNackFec. Don't spam the logs.
3420 return false;
3421 }
3422
3423 send_channels_[local_ssrc_key] = send_channel.release();
3424
3425 return true;
3426}
3427
3428bool WebRtcVideoMediaChannel::SetNackFec(int channel_id,
3429 int red_payload_type,
3430 int fec_payload_type,
3431 bool nack_enabled) {
3432 bool enable = (red_payload_type != -1 && fec_payload_type != -1 &&
3433 !InConferenceMode());
3434 if (enable) {
3435 if (engine_->vie()->rtp()->SetHybridNACKFECStatus(
3436 channel_id, nack_enabled, red_payload_type, fec_payload_type) != 0) {
3437 LOG_RTCERR4(SetHybridNACKFECStatus,
3438 channel_id, nack_enabled, red_payload_type, fec_payload_type);
3439 return false;
3440 }
3441 LOG(LS_INFO) << "Hybrid NACK/FEC enabled for channel " << channel_id;
3442 } else {
3443 if (engine_->vie()->rtp()->SetNACKStatus(channel_id, nack_enabled) != 0) {
3444 LOG_RTCERR1(SetNACKStatus, channel_id);
3445 return false;
3446 }
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00003447 std::string enabled = nack_enabled ? "enabled" : "disabled";
3448 LOG(LS_INFO) << "NACK " << enabled << " for channel " << channel_id;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003449 }
3450 return true;
3451}
3452
3453bool WebRtcVideoMediaChannel::SetSendCodec(const webrtc::VideoCodec& codec,
3454 int min_bitrate,
3455 int start_bitrate,
3456 int max_bitrate) {
3457 bool ret_val = true;
3458 for (SendChannelMap::iterator iter = send_channels_.begin();
3459 iter != send_channels_.end(); ++iter) {
3460 WebRtcVideoChannelSendInfo* send_channel = iter->second;
3461 ret_val = SetSendCodec(send_channel, codec, min_bitrate, start_bitrate,
3462 max_bitrate) && ret_val;
3463 }
3464 if (ret_val) {
3465 // All SetSendCodec calls were successful. Update the global state
3466 // accordingly.
3467 send_codec_.reset(new webrtc::VideoCodec(codec));
3468 send_min_bitrate_ = min_bitrate;
3469 send_start_bitrate_ = start_bitrate;
3470 send_max_bitrate_ = max_bitrate;
3471 } else {
3472 // At least one SetSendCodec call failed, rollback.
3473 for (SendChannelMap::iterator iter = send_channels_.begin();
3474 iter != send_channels_.end(); ++iter) {
3475 WebRtcVideoChannelSendInfo* send_channel = iter->second;
3476 if (send_codec_) {
3477 SetSendCodec(send_channel, *send_codec_.get(), send_min_bitrate_,
3478 send_start_bitrate_, send_max_bitrate_);
3479 }
3480 }
3481 }
3482 return ret_val;
3483}
3484
3485bool WebRtcVideoMediaChannel::SetSendCodec(
3486 WebRtcVideoChannelSendInfo* send_channel,
3487 const webrtc::VideoCodec& codec,
3488 int min_bitrate,
3489 int start_bitrate,
3490 int max_bitrate) {
3491 if (!send_channel) {
3492 return false;
3493 }
3494 const int channel_id = send_channel->channel_id();
3495 // Make a copy of the codec
3496 webrtc::VideoCodec target_codec = codec;
3497 target_codec.startBitrate = start_bitrate;
3498 target_codec.minBitrate = min_bitrate;
3499 target_codec.maxBitrate = max_bitrate;
3500
3501 // Set the default number of temporal layers for VP8.
3502 if (webrtc::kVideoCodecVP8 == codec.codecType) {
3503 target_codec.codecSpecific.VP8.numberOfTemporalLayers =
3504 kDefaultNumberOfTemporalLayers;
3505
3506 // Turn off the VP8 error resilience
3507 target_codec.codecSpecific.VP8.resilience = webrtc::kResilienceOff;
3508
3509 bool enable_denoising =
3510 options_.video_noise_reduction.GetWithDefaultIfUnset(false);
3511 target_codec.codecSpecific.VP8.denoisingOn = enable_denoising;
3512 }
3513
3514 // Register external encoder if codec type is supported by encoder factory.
3515 if (engine()->IsExternalEncoderCodecType(codec.codecType) &&
3516 !send_channel->IsEncoderRegistered(target_codec.plType)) {
3517 webrtc::VideoEncoder* encoder =
3518 engine()->CreateExternalEncoder(codec.codecType);
3519 if (encoder) {
3520 if (engine()->vie()->ext_codec()->RegisterExternalSendCodec(
3521 channel_id, target_codec.plType, encoder, false) == 0) {
3522 send_channel->RegisterEncoder(target_codec.plType, encoder);
3523 } else {
3524 LOG_RTCERR2(RegisterExternalSendCodec, channel_id, target_codec.plName);
3525 engine()->DestroyExternalEncoder(encoder);
3526 }
3527 }
3528 }
3529
3530 // Resolution and framerate may vary for different send channels.
3531 const VideoFormat& video_format = send_channel->video_format();
3532 UpdateVideoCodec(video_format, &target_codec);
3533
3534 if (target_codec.width == 0 && target_codec.height == 0) {
3535 const uint32 ssrc = send_channel->stream_params()->first_ssrc();
3536 LOG(LS_INFO) << "0x0 resolution selected. Captured frames will be dropped "
3537 << "for ssrc: " << ssrc << ".";
3538 } else {
3539 MaybeChangeStartBitrate(channel_id, &target_codec);
3540 if (0 != engine()->vie()->codec()->SetSendCodec(channel_id, target_codec)) {
3541 LOG_RTCERR2(SetSendCodec, channel_id, target_codec.plName);
3542 return false;
3543 }
3544
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00003545 // NOTE: SetRtxSendPayloadType must be called after all simulcast SSRCs
3546 // are configured. Otherwise ssrc's configured after this point will use
3547 // the primary PT for RTX.
3548 if (send_rtx_type_ != -1 &&
3549 engine()->vie()->rtp()->SetRtxSendPayloadType(channel_id,
3550 send_rtx_type_) != 0) {
3551 LOG_RTCERR2(SetRtxSendPayloadType, channel_id, send_rtx_type_);
3552 return false;
3553 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003554 }
3555 send_channel->set_interval(
3556 cricket::VideoFormat::FpsToInterval(target_codec.maxFramerate));
3557 return true;
3558}
3559
3560
3561static std::string ToString(webrtc::VideoCodecComplexity complexity) {
3562 switch (complexity) {
3563 case webrtc::kComplexityNormal:
3564 return "normal";
3565 case webrtc::kComplexityHigh:
3566 return "high";
3567 case webrtc::kComplexityHigher:
3568 return "higher";
3569 case webrtc::kComplexityMax:
3570 return "max";
3571 default:
3572 return "unknown";
3573 }
3574}
3575
3576static std::string ToString(webrtc::VP8ResilienceMode resilience) {
3577 switch (resilience) {
3578 case webrtc::kResilienceOff:
3579 return "off";
3580 case webrtc::kResilientStream:
3581 return "stream";
3582 case webrtc::kResilientFrames:
3583 return "frames";
3584 default:
3585 return "unknown";
3586 }
3587}
3588
3589void WebRtcVideoMediaChannel::LogSendCodecChange(const std::string& reason) {
3590 webrtc::VideoCodec vie_codec;
3591 if (engine()->vie()->codec()->GetSendCodec(vie_channel_, vie_codec) != 0) {
3592 LOG_RTCERR1(GetSendCodec, vie_channel_);
3593 return;
3594 }
3595
3596 LOG(LS_INFO) << reason << " : selected video codec "
3597 << vie_codec.plName << "/"
3598 << vie_codec.width << "x" << vie_codec.height << "x"
3599 << static_cast<int>(vie_codec.maxFramerate) << "fps"
3600 << "@" << vie_codec.maxBitrate << "kbps"
3601 << " (min=" << vie_codec.minBitrate << "kbps,"
3602 << " start=" << vie_codec.startBitrate << "kbps)";
3603 LOG(LS_INFO) << "Video max quantization: " << vie_codec.qpMax;
3604 if (webrtc::kVideoCodecVP8 == vie_codec.codecType) {
3605 LOG(LS_INFO) << "VP8 number of temporal layers: "
3606 << static_cast<int>(
3607 vie_codec.codecSpecific.VP8.numberOfTemporalLayers);
3608 LOG(LS_INFO) << "VP8 options : "
3609 << "picture loss indication = "
3610 << vie_codec.codecSpecific.VP8.pictureLossIndicationOn
3611 << ", feedback mode = "
3612 << vie_codec.codecSpecific.VP8.feedbackModeOn
3613 << ", complexity = "
3614 << ToString(vie_codec.codecSpecific.VP8.complexity)
3615 << ", resilience = "
3616 << ToString(vie_codec.codecSpecific.VP8.resilience)
3617 << ", denoising = "
3618 << vie_codec.codecSpecific.VP8.denoisingOn
3619 << ", error concealment = "
3620 << vie_codec.codecSpecific.VP8.errorConcealmentOn
3621 << ", automatic resize = "
3622 << vie_codec.codecSpecific.VP8.automaticResizeOn
3623 << ", frame dropping = "
3624 << vie_codec.codecSpecific.VP8.frameDroppingOn
3625 << ", key frame interval = "
3626 << vie_codec.codecSpecific.VP8.keyFrameInterval;
3627 }
3628
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00003629 if (send_rtx_type_ != -1) {
3630 LOG(LS_INFO) << "RTX payload type: " << send_rtx_type_;
3631 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003632}
3633
3634bool WebRtcVideoMediaChannel::SetReceiveCodecs(
3635 WebRtcVideoChannelRecvInfo* info) {
3636 int red_type = -1;
3637 int fec_type = -1;
3638 int channel_id = info->channel_id();
3639 for (std::vector<webrtc::VideoCodec>::iterator it = receive_codecs_.begin();
3640 it != receive_codecs_.end(); ++it) {
3641 if (it->codecType == webrtc::kVideoCodecRED) {
3642 red_type = it->plType;
3643 } else if (it->codecType == webrtc::kVideoCodecULPFEC) {
3644 fec_type = it->plType;
3645 }
3646 if (engine()->vie()->codec()->SetReceiveCodec(channel_id, *it) != 0) {
3647 LOG_RTCERR2(SetReceiveCodec, channel_id, it->plName);
3648 return false;
3649 }
3650 if (!info->IsDecoderRegistered(it->plType) &&
3651 it->codecType != webrtc::kVideoCodecRED &&
3652 it->codecType != webrtc::kVideoCodecULPFEC) {
3653 webrtc::VideoDecoder* decoder =
3654 engine()->CreateExternalDecoder(it->codecType);
3655 if (decoder) {
3656 if (engine()->vie()->ext_codec()->RegisterExternalReceiveCodec(
3657 channel_id, it->plType, decoder) == 0) {
3658 info->RegisterDecoder(it->plType, decoder);
3659 } else {
3660 LOG_RTCERR2(RegisterExternalReceiveCodec, channel_id, it->plName);
3661 engine()->DestroyExternalDecoder(decoder);
3662 }
3663 }
3664 }
3665 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003666 return true;
3667}
3668
3669int WebRtcVideoMediaChannel::GetRecvChannelNum(uint32 ssrc) {
3670 if (ssrc == first_receive_ssrc_) {
3671 return vie_channel_;
3672 }
3673 RecvChannelMap::iterator it = recv_channels_.find(ssrc);
3674 return (it != recv_channels_.end()) ? it->second->channel_id() : -1;
3675}
3676
3677// If the new frame size is different from the send codec size we set on vie,
3678// we need to reset the send codec on vie.
3679// The new send codec size should not exceed send_codec_ which is controlled
3680// only by the 'jec' logic.
3681bool WebRtcVideoMediaChannel::MaybeResetVieSendCodec(
3682 WebRtcVideoChannelSendInfo* send_channel,
3683 int new_width,
3684 int new_height,
3685 bool is_screencast,
3686 bool* reset) {
3687 if (reset) {
3688 *reset = false;
3689 }
3690 ASSERT(send_codec_.get() != NULL);
3691
3692 webrtc::VideoCodec target_codec = *send_codec_.get();
3693 const VideoFormat& video_format = send_channel->video_format();
3694 UpdateVideoCodec(video_format, &target_codec);
3695
3696 // Vie send codec size should not exceed target_codec.
3697 int target_width = new_width;
3698 int target_height = new_height;
3699 if (!is_screencast &&
3700 (new_width > target_codec.width || new_height > target_codec.height)) {
3701 target_width = target_codec.width;
3702 target_height = target_codec.height;
3703 }
3704
3705 // Get current vie codec.
3706 webrtc::VideoCodec vie_codec;
3707 const int channel_id = send_channel->channel_id();
3708 if (engine()->vie()->codec()->GetSendCodec(channel_id, vie_codec) != 0) {
3709 LOG_RTCERR1(GetSendCodec, channel_id);
3710 return false;
3711 }
3712 const int cur_width = vie_codec.width;
3713 const int cur_height = vie_codec.height;
3714
3715 // Only reset send codec when there is a size change. Additionally,
3716 // automatic resize needs to be turned off when screencasting and on when
3717 // not screencasting.
3718 // Don't allow automatic resizing for screencasting.
3719 bool automatic_resize = !is_screencast;
3720 // Turn off VP8 frame dropping when screensharing as the current model does
3721 // not work well at low fps.
3722 bool vp8_frame_dropping = !is_screencast;
3723 // Disable denoising for screencasting.
3724 bool enable_denoising =
3725 options_.video_noise_reduction.GetWithDefaultIfUnset(false);
3726 bool denoising = !is_screencast && enable_denoising;
3727 bool reset_send_codec =
3728 target_width != cur_width || target_height != cur_height ||
3729 automatic_resize != vie_codec.codecSpecific.VP8.automaticResizeOn ||
3730 denoising != vie_codec.codecSpecific.VP8.denoisingOn ||
3731 vp8_frame_dropping != vie_codec.codecSpecific.VP8.frameDroppingOn;
3732
3733 if (reset_send_codec) {
3734 // Set the new codec on vie.
3735 vie_codec.width = target_width;
3736 vie_codec.height = target_height;
3737 vie_codec.maxFramerate = target_codec.maxFramerate;
3738 vie_codec.startBitrate = target_codec.startBitrate;
3739 vie_codec.codecSpecific.VP8.automaticResizeOn = automatic_resize;
3740 vie_codec.codecSpecific.VP8.denoisingOn = denoising;
3741 vie_codec.codecSpecific.VP8.frameDroppingOn = vp8_frame_dropping;
3742 // TODO(mflodman): Remove 'is_screencast' check when screen cast settings
3743 // are treated correctly in WebRTC.
3744 if (!is_screencast)
3745 MaybeChangeStartBitrate(channel_id, &vie_codec);
3746
3747 if (engine()->vie()->codec()->SetSendCodec(channel_id, vie_codec) != 0) {
3748 LOG_RTCERR1(SetSendCodec, channel_id);
3749 return false;
3750 }
3751 if (reset) {
3752 *reset = true;
3753 }
3754 LogSendCodecChange("Capture size changed");
3755 }
3756
3757 return true;
3758}
3759
3760void WebRtcVideoMediaChannel::MaybeChangeStartBitrate(
3761 int channel_id, webrtc::VideoCodec* video_codec) {
3762 if (video_codec->startBitrate < video_codec->minBitrate) {
3763 video_codec->startBitrate = video_codec->minBitrate;
3764 } else if (video_codec->startBitrate > video_codec->maxBitrate) {
3765 video_codec->startBitrate = video_codec->maxBitrate;
3766 }
3767
3768 // Use a previous target bitrate, if there is one.
3769 unsigned int current_target_bitrate = 0;
3770 if (engine()->vie()->codec()->GetCodecTargetBitrate(
3771 channel_id, &current_target_bitrate) == 0) {
3772 // Convert to kbps.
3773 current_target_bitrate /= 1000;
3774 if (current_target_bitrate > video_codec->maxBitrate) {
3775 current_target_bitrate = video_codec->maxBitrate;
3776 }
3777 if (current_target_bitrate > video_codec->startBitrate) {
3778 video_codec->startBitrate = current_target_bitrate;
3779 }
3780 }
3781}
3782
3783void WebRtcVideoMediaChannel::OnMessage(talk_base::Message* msg) {
3784 FlushBlackFrameData* black_frame_data =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00003785 static_cast<FlushBlackFrameData*>(msg->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003786 FlushBlackFrame(black_frame_data->ssrc, black_frame_data->timestamp);
3787 delete black_frame_data;
3788}
3789
3790int WebRtcVideoMediaChannel::SendPacket(int channel, const void* data,
3791 int len) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003792 talk_base::Buffer packet(data, len, kMaxRtpPacketLen);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00003793 return MediaChannel::SendPacket(&packet) ? len : -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003794}
3795
3796int WebRtcVideoMediaChannel::SendRTCPPacket(int channel,
3797 const void* data,
3798 int len) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003799 talk_base::Buffer packet(data, len, kMaxRtpPacketLen);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00003800 return MediaChannel::SendRtcp(&packet) ? len : -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003801}
3802
3803void WebRtcVideoMediaChannel::QueueBlackFrame(uint32 ssrc, int64 timestamp,
3804 int framerate) {
3805 if (timestamp) {
3806 FlushBlackFrameData* black_frame_data = new FlushBlackFrameData(
3807 ssrc,
3808 timestamp);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00003809 const int delay_ms = static_cast<int>(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003810 2 * cricket::VideoFormat::FpsToInterval(framerate) *
3811 talk_base::kNumMillisecsPerSec / talk_base::kNumNanosecsPerSec);
3812 worker_thread()->PostDelayed(delay_ms, this, 0, black_frame_data);
3813 }
3814}
3815
3816void WebRtcVideoMediaChannel::FlushBlackFrame(uint32 ssrc, int64 timestamp) {
3817 WebRtcVideoChannelSendInfo* send_channel = GetSendChannel(ssrc);
3818 if (!send_channel) {
3819 return;
3820 }
3821 talk_base::scoped_ptr<const VideoFrame> black_frame_ptr;
3822
3823 const WebRtcLocalStreamInfo* channel_stream_info =
3824 send_channel->local_stream_info();
3825 int64 last_frame_time_stamp = channel_stream_info->time_stamp();
3826 if (last_frame_time_stamp == timestamp) {
3827 size_t last_frame_width = 0;
3828 size_t last_frame_height = 0;
3829 int64 last_frame_elapsed_time = 0;
3830 channel_stream_info->GetLastFrameInfo(&last_frame_width, &last_frame_height,
3831 &last_frame_elapsed_time);
3832 if (!last_frame_width || !last_frame_height) {
3833 return;
3834 }
3835 WebRtcVideoFrame black_frame;
3836 // Black frame is not screencast.
3837 const bool screencasting = false;
3838 const int64 timestamp_delta = send_channel->interval();
3839 if (!black_frame.InitToBlack(send_codec_->width, send_codec_->height, 1, 1,
3840 last_frame_elapsed_time + timestamp_delta,
3841 last_frame_time_stamp + timestamp_delta) ||
3842 !SendFrame(send_channel, &black_frame, screencasting)) {
3843 LOG(LS_ERROR) << "Failed to send black frame.";
3844 }
3845 }
3846}
3847
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00003848void WebRtcVideoMediaChannel::OnCpuAdaptationUnable() {
3849 // ssrc is hardcoded to 0. This message is based on a system wide issue,
3850 // so finding which ssrc caused it doesn't matter.
3851 SignalMediaError(0, VideoMediaChannel::ERROR_REC_CPU_MAX_CANT_DOWNGRADE);
3852}
3853
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003854void WebRtcVideoMediaChannel::SetNetworkTransmissionState(
3855 bool is_transmitting) {
3856 LOG(LS_INFO) << "SetNetworkTransmissionState: " << is_transmitting;
3857 for (SendChannelMap::iterator iter = send_channels_.begin();
3858 iter != send_channels_.end(); ++iter) {
3859 WebRtcVideoChannelSendInfo* send_channel = iter->second;
3860 int channel_id = send_channel->channel_id();
3861 engine_->vie()->network()->SetNetworkTransmissionState(channel_id,
3862 is_transmitting);
3863 }
3864}
3865
3866bool WebRtcVideoMediaChannel::SetHeaderExtension(ExtensionSetterFunction setter,
3867 int channel_id, const RtpHeaderExtension* extension) {
3868 bool enable = false;
3869 int id = 0;
3870 if (extension) {
3871 enable = true;
3872 id = extension->id;
3873 }
3874 if ((engine_->vie()->rtp()->*setter)(channel_id, enable, id) != 0) {
3875 LOG_RTCERR4(*setter, extension->uri, channel_id, enable, id);
3876 return false;
3877 }
3878 return true;
3879}
3880
3881bool WebRtcVideoMediaChannel::SetHeaderExtension(ExtensionSetterFunction setter,
3882 int channel_id, const std::vector<RtpHeaderExtension>& extensions,
3883 const char header_extension_uri[]) {
3884 const RtpHeaderExtension* extension = FindHeaderExtension(extensions,
3885 header_extension_uri);
3886 return SetHeaderExtension(setter, channel_id, extension);
3887}
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00003888
3889bool WebRtcVideoMediaChannel::SetLocalRtxSsrc(int channel_id,
3890 const StreamParams& send_params,
3891 uint32 primary_ssrc,
3892 int stream_idx) {
3893 uint32 rtx_ssrc = 0;
3894 bool has_rtx = send_params.GetFidSsrc(primary_ssrc, &rtx_ssrc);
3895 if (has_rtx && engine()->vie()->rtp()->SetLocalSSRC(
3896 channel_id, rtx_ssrc, webrtc::kViEStreamTypeRtx, stream_idx) != 0) {
3897 LOG_RTCERR4(SetLocalSSRC, channel_id, rtx_ssrc,
3898 webrtc::kViEStreamTypeRtx, stream_idx);
3899 return false;
3900 }
3901 return true;
3902}
3903
wu@webrtc.org24301a62013-12-13 19:17:43 +00003904void WebRtcVideoMediaChannel::MaybeConnectCapturer(VideoCapturer* capturer) {
3905 if (capturer != NULL && GetSendChannelNum(capturer) == 1) {
3906 capturer->SignalVideoFrame.connect(this,
3907 &WebRtcVideoMediaChannel::SendFrame);
3908 }
3909}
3910
3911void WebRtcVideoMediaChannel::MaybeDisconnectCapturer(VideoCapturer* capturer) {
3912 if (capturer != NULL && GetSendChannelNum(capturer) == 1) {
3913 capturer->SignalVideoFrame.disconnect(this);
3914 }
3915}
3916
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003917} // namespace cricket
3918
3919#endif // HAVE_WEBRTC_VIDEO