blob: 25ee95c22177e93b66edf97dbe757f28d3047d00 [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
149// Extension header for RTP timestamp offset, see RFC 5450 for details:
150// http://tools.ietf.org/html/rfc5450
151static const char kRtpTimestampOffsetHeaderExtension[] =
152 "urn:ietf:params:rtp-hdrext:toffset";
153static const int kRtpTimeOffsetExtensionId = 2;
154
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000155// Extension header ID for absolute send time. Url defined in constants.cc
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000156static const int kRtpAbsoluteSendTimeExtensionId = 3;
wu@webrtc.orgde305012013-10-31 15:40:38 +0000157// Default video dscp value.
158// See http://tools.ietf.org/html/rfc2474 for details
159// See also http://tools.ietf.org/html/draft-jennings-rtcweb-qos-00
160static const talk_base::DiffServCodePoint kVideoDscpValue =
161 talk_base::DSCP_AF41;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000162
163static bool IsNackEnabled(const VideoCodec& codec) {
164 return codec.HasFeedbackParam(FeedbackParam(kRtcpFbParamNack,
165 kParamValueEmpty));
166}
167
168// Returns true if Receiver Estimated Max Bitrate is enabled.
169static bool IsRembEnabled(const VideoCodec& codec) {
170 return codec.HasFeedbackParam(FeedbackParam(kRtcpFbParamRemb,
171 kParamValueEmpty));
172}
173
174struct FlushBlackFrameData : public talk_base::MessageData {
175 FlushBlackFrameData(uint32 s, int64 t) : ssrc(s), timestamp(t) {
176 }
177 uint32 ssrc;
178 int64 timestamp;
179};
180
181class WebRtcRenderAdapter : public webrtc::ExternalRenderer {
182 public:
183 explicit WebRtcRenderAdapter(VideoRenderer* renderer)
184 : renderer_(renderer), width_(0), height_(0), watermark_enabled_(false) {
185 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000186
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000187 virtual ~WebRtcRenderAdapter() {
188 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000189
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000190 void set_watermark_enabled(bool enable) {
191 talk_base::CritScope cs(&crit_);
192 watermark_enabled_ = enable;
193 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000194
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000195 void SetRenderer(VideoRenderer* renderer) {
196 talk_base::CritScope cs(&crit_);
197 renderer_ = renderer;
198 // FrameSizeChange may have already been called when renderer was not set.
199 // If so we should call SetSize here.
200 // TODO(ronghuawu): Add unit test for this case. Didn't do it now
201 // because the WebRtcRenderAdapter is currently hiding in cc file. No
202 // good way to get access to it from the unit test.
203 if (width_ > 0 && height_ > 0 && renderer_ != NULL) {
204 if (!renderer_->SetSize(width_, height_, 0)) {
205 LOG(LS_ERROR)
206 << "WebRtcRenderAdapter SetRenderer failed to SetSize to: "
207 << width_ << "x" << height_;
208 }
209 }
210 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000211
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000212 // Implementation of webrtc::ExternalRenderer.
213 virtual int FrameSizeChange(unsigned int width, unsigned int height,
214 unsigned int /*number_of_streams*/) {
215 talk_base::CritScope cs(&crit_);
216 width_ = width;
217 height_ = height;
218 LOG(LS_INFO) << "WebRtcRenderAdapter frame size changed to: "
219 << width << "x" << height;
220 if (renderer_ == NULL) {
221 LOG(LS_VERBOSE) << "WebRtcRenderAdapter the renderer has not been set. "
222 << "SetSize will be called later in SetRenderer.";
223 return 0;
224 }
225 return renderer_->SetSize(width_, height_, 0) ? 0 : -1;
226 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000227
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000228 virtual int DeliverFrame(unsigned char* buffer, int buffer_size,
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000229 uint32_t time_stamp, int64_t render_time,
230 void* handle) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000231 talk_base::CritScope cs(&crit_);
232 frame_rate_tracker_.Update(1);
233 if (renderer_ == NULL) {
234 return 0;
235 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000236 // Convert 90K rtp timestamp to ns timestamp.
237 int64 rtp_time_stamp_in_ns = (time_stamp / 90) *
238 talk_base::kNumNanosecsPerMillisec;
239 // Convert milisecond render time to ns timestamp.
240 int64 render_time_stamp_in_ns = render_time *
241 talk_base::kNumNanosecsPerMillisec;
242 // Send the rtp timestamp to renderer as the VideoFrame timestamp.
243 // and the render timestamp as the VideoFrame elapsed_time.
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000244 if (handle == NULL) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000245 return DeliverBufferFrame(buffer, buffer_size, render_time_stamp_in_ns,
246 rtp_time_stamp_in_ns);
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000247 } else {
248 return DeliverTextureFrame(handle, render_time_stamp_in_ns,
249 rtp_time_stamp_in_ns);
250 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000251 }
252
253 virtual bool IsTextureSupported() { return true; }
254
255 int DeliverBufferFrame(unsigned char* buffer, int buffer_size,
256 int64 elapsed_time, int64 time_stamp) {
257 WebRtcVideoFrame video_frame;
wu@webrtc.org16d62542013-11-05 23:45:14 +0000258 video_frame.Alias(buffer, buffer_size, width_, height_,
259 1, 1, elapsed_time, time_stamp, 0);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000260
261
262 // Sanity check on decoded frame size.
263 if (buffer_size != static_cast<int>(VideoFrame::SizeOf(width_, height_))) {
264 LOG(LS_WARNING) << "WebRtcRenderAdapter received a strange frame size: "
265 << buffer_size;
266 }
267
268 int ret = renderer_->RenderFrame(&video_frame) ? 0 : -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000269 return ret;
270 }
271
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000272 int DeliverTextureFrame(void* handle, int64 elapsed_time, int64 time_stamp) {
273 WebRtcTextureVideoFrame video_frame(
274 static_cast<webrtc::NativeHandle*>(handle), width_, height_,
275 elapsed_time, time_stamp);
276 return renderer_->RenderFrame(&video_frame);
277 }
278
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000279 unsigned int width() {
280 talk_base::CritScope cs(&crit_);
281 return width_;
282 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000283
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000284 unsigned int height() {
285 talk_base::CritScope cs(&crit_);
286 return height_;
287 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000288
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000289 int framerate() {
290 talk_base::CritScope cs(&crit_);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000291 return static_cast<int>(frame_rate_tracker_.units_second());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000292 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000293
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000294 VideoRenderer* renderer() {
295 talk_base::CritScope cs(&crit_);
296 return renderer_;
297 }
298
299 private:
300 talk_base::CriticalSection crit_;
301 VideoRenderer* renderer_;
302 unsigned int width_;
303 unsigned int height_;
304 talk_base::RateTracker frame_rate_tracker_;
305 bool watermark_enabled_;
306};
307
308class WebRtcDecoderObserver : public webrtc::ViEDecoderObserver {
309 public:
310 explicit WebRtcDecoderObserver(int video_channel)
311 : video_channel_(video_channel),
312 framerate_(0),
313 bitrate_(0),
wu@webrtc.org97077a32013-10-25 21:18:33 +0000314 decode_ms_(0),
315 max_decode_ms_(0),
316 current_delay_ms_(0),
317 target_delay_ms_(0),
318 jitter_buffer_ms_(0),
319 min_playout_delay_ms_(0),
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +0000320 render_delay_ms_(0) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000321 }
322
323 // virtual functions from VieDecoderObserver.
324 virtual void IncomingCodecChanged(const int videoChannel,
325 const webrtc::VideoCodec& videoCodec) {}
326 virtual void IncomingRate(const int videoChannel,
327 const unsigned int framerate,
328 const unsigned int bitrate) {
wu@webrtc.org78187522013-10-07 23:32:02 +0000329 talk_base::CritScope cs(&crit_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000330 ASSERT(video_channel_ == videoChannel);
331 framerate_ = framerate;
332 bitrate_ = bitrate;
333 }
wu@webrtc.org97077a32013-10-25 21:18:33 +0000334
335 virtual void DecoderTiming(int decode_ms,
336 int max_decode_ms,
337 int current_delay_ms,
338 int target_delay_ms,
339 int jitter_buffer_ms,
340 int min_playout_delay_ms,
341 int render_delay_ms) {
342 talk_base::CritScope cs(&crit_);
343 decode_ms_ = decode_ms;
344 max_decode_ms_ = max_decode_ms;
345 current_delay_ms_ = current_delay_ms;
346 target_delay_ms_ = target_delay_ms;
347 jitter_buffer_ms_ = jitter_buffer_ms;
348 min_playout_delay_ms_ = min_playout_delay_ms;
349 render_delay_ms_ = render_delay_ms;
350 }
351
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +0000352 virtual void RequestNewKeyFrame(const int videoChannel) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000353
wu@webrtc.org97077a32013-10-25 21:18:33 +0000354 // Populate |rinfo| based on previously-set data in |*this|.
355 void ExportTo(VideoReceiverInfo* rinfo) {
wu@webrtc.org78187522013-10-07 23:32:02 +0000356 talk_base::CritScope cs(&crit_);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000357 rinfo->framerate_rcvd = framerate_;
358 rinfo->decode_ms = decode_ms_;
359 rinfo->max_decode_ms = max_decode_ms_;
360 rinfo->current_delay_ms = current_delay_ms_;
361 rinfo->target_delay_ms = target_delay_ms_;
362 rinfo->jitter_buffer_ms = jitter_buffer_ms_;
363 rinfo->min_playout_delay_ms = min_playout_delay_ms_;
364 rinfo->render_delay_ms = render_delay_ms_;
wu@webrtc.org78187522013-10-07 23:32:02 +0000365 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000366
367 private:
wu@webrtc.org78187522013-10-07 23:32:02 +0000368 mutable talk_base::CriticalSection crit_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000369 int video_channel_;
370 int framerate_;
371 int bitrate_;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000372 int decode_ms_;
373 int max_decode_ms_;
374 int current_delay_ms_;
375 int target_delay_ms_;
376 int jitter_buffer_ms_;
377 int min_playout_delay_ms_;
378 int render_delay_ms_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000379};
380
381class WebRtcEncoderObserver : public webrtc::ViEEncoderObserver {
382 public:
383 explicit WebRtcEncoderObserver(int video_channel)
384 : video_channel_(video_channel),
385 framerate_(0),
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000386 bitrate_(0),
387 suspended_(false) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000388 }
389
390 // virtual functions from VieEncoderObserver.
391 virtual void OutgoingRate(const int videoChannel,
392 const unsigned int framerate,
393 const unsigned int bitrate) {
wu@webrtc.org78187522013-10-07 23:32:02 +0000394 talk_base::CritScope cs(&crit_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000395 ASSERT(video_channel_ == videoChannel);
396 framerate_ = framerate;
397 bitrate_ = bitrate;
398 }
399
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000400 virtual void SuspendChange(int video_channel, bool is_suspended) {
401 talk_base::CritScope cs(&crit_);
402 ASSERT(video_channel_ == video_channel);
403 suspended_ = is_suspended;
404 }
405
wu@webrtc.org78187522013-10-07 23:32:02 +0000406 int framerate() const {
407 talk_base::CritScope cs(&crit_);
408 return framerate_;
409 }
410 int bitrate() const {
411 talk_base::CritScope cs(&crit_);
412 return bitrate_;
413 }
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000414 bool suspended() const {
415 talk_base::CritScope cs(&crit_);
416 return suspended_;
417 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000418
419 private:
wu@webrtc.org78187522013-10-07 23:32:02 +0000420 mutable talk_base::CriticalSection crit_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000421 int video_channel_;
422 int framerate_;
423 int bitrate_;
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000424 bool suspended_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000425};
426
427class WebRtcLocalStreamInfo {
428 public:
429 WebRtcLocalStreamInfo()
430 : width_(0), height_(0), elapsed_time_(-1), time_stamp_(-1) {}
431 size_t width() const {
432 talk_base::CritScope cs(&crit_);
433 return width_;
434 }
435 size_t height() const {
436 talk_base::CritScope cs(&crit_);
437 return height_;
438 }
439 int64 elapsed_time() const {
440 talk_base::CritScope cs(&crit_);
441 return elapsed_time_;
442 }
443 int64 time_stamp() const {
444 talk_base::CritScope cs(&crit_);
445 return time_stamp_;
446 }
447 int framerate() {
448 talk_base::CritScope cs(&crit_);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000449 return static_cast<int>(rate_tracker_.units_second());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000450 }
451 void GetLastFrameInfo(
452 size_t* width, size_t* height, int64* elapsed_time) const {
453 talk_base::CritScope cs(&crit_);
454 *width = width_;
455 *height = height_;
456 *elapsed_time = elapsed_time_;
457 }
458
459 void UpdateFrame(const VideoFrame* frame) {
460 talk_base::CritScope cs(&crit_);
461
462 width_ = frame->GetWidth();
463 height_ = frame->GetHeight();
464 elapsed_time_ = frame->GetElapsedTime();
465 time_stamp_ = frame->GetTimeStamp();
466
467 rate_tracker_.Update(1);
468 }
469
470 private:
471 mutable talk_base::CriticalSection crit_;
472 size_t width_;
473 size_t height_;
474 int64 elapsed_time_;
475 int64 time_stamp_;
476 talk_base::RateTracker rate_tracker_;
477
478 DISALLOW_COPY_AND_ASSIGN(WebRtcLocalStreamInfo);
479};
480
481// WebRtcVideoChannelRecvInfo is a container class with members such as renderer
482// and a decoder observer that is used by receive channels.
483// It must exist as long as the receive channel is connected to renderer or a
484// decoder observer in this class and methods in the class should only be called
485// from the worker thread.
486class WebRtcVideoChannelRecvInfo {
487 public:
488 typedef std::map<int, webrtc::VideoDecoder*> DecoderMap; // key: payload type
489 explicit WebRtcVideoChannelRecvInfo(int channel_id)
490 : channel_id_(channel_id),
491 render_adapter_(NULL),
492 decoder_observer_(channel_id) {
493 }
494 int channel_id() { return channel_id_; }
495 void SetRenderer(VideoRenderer* renderer) {
496 render_adapter_.SetRenderer(renderer);
497 }
498 WebRtcRenderAdapter* render_adapter() { return &render_adapter_; }
499 WebRtcDecoderObserver* decoder_observer() { return &decoder_observer_; }
500 void RegisterDecoder(int pl_type, webrtc::VideoDecoder* decoder) {
501 ASSERT(!IsDecoderRegistered(pl_type));
502 registered_decoders_[pl_type] = decoder;
503 }
504 bool IsDecoderRegistered(int pl_type) {
505 return registered_decoders_.count(pl_type) != 0;
506 }
507 const DecoderMap& registered_decoders() {
508 return registered_decoders_;
509 }
510 void ClearRegisteredDecoders() {
511 registered_decoders_.clear();
512 }
513
514 private:
515 int channel_id_; // Webrtc video channel number.
516 // Renderer for this channel.
517 WebRtcRenderAdapter render_adapter_;
518 WebRtcDecoderObserver decoder_observer_;
519 DecoderMap registered_decoders_;
520};
521
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000522class WebRtcOveruseObserver : public webrtc::CpuOveruseObserver {
523 public:
524 explicit WebRtcOveruseObserver(CoordinatedVideoAdapter* video_adapter)
525 : video_adapter_(video_adapter),
526 enabled_(false) {
527 }
528
529 // TODO(mflodman): Consider sending resolution as part of event, to let
530 // adapter know what resolution the request is based on. Helps eliminate stale
531 // data, race conditions.
532 virtual void OveruseDetected() OVERRIDE {
533 talk_base::CritScope cs(&crit_);
534 if (!enabled_) {
535 return;
536 }
537
538 video_adapter_->OnCpuResolutionRequest(CoordinatedVideoAdapter::DOWNGRADE);
539 }
540
541 virtual void NormalUsage() OVERRIDE {
542 talk_base::CritScope cs(&crit_);
543 if (!enabled_) {
544 return;
545 }
546
547 video_adapter_->OnCpuResolutionRequest(CoordinatedVideoAdapter::UPGRADE);
548 }
549
550 void Enable(bool enable) {
551 talk_base::CritScope cs(&crit_);
552 enabled_ = enable;
553 }
554
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000555 bool enabled() const { return enabled_; }
556
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000557 private:
558 CoordinatedVideoAdapter* video_adapter_;
559 bool enabled_;
560 talk_base::CriticalSection crit_;
561};
562
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000563
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000564class WebRtcVideoChannelSendInfo : public sigslot::has_slots<> {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000565 public:
566 typedef std::map<int, webrtc::VideoEncoder*> EncoderMap; // key: payload type
567 WebRtcVideoChannelSendInfo(int channel_id, int capture_id,
568 webrtc::ViEExternalCapture* external_capture,
569 talk_base::CpuMonitor* cpu_monitor)
570 : channel_id_(channel_id),
571 capture_id_(capture_id),
572 sending_(false),
573 muted_(false),
574 video_capturer_(NULL),
575 encoder_observer_(channel_id),
576 external_capture_(external_capture),
577 capturer_updated_(false),
578 interval_(0),
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000579 cpu_monitor_(cpu_monitor),
580 overuse_observer_enabled_(false) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000581 }
582
583 int channel_id() const { return channel_id_; }
584 int capture_id() const { return capture_id_; }
585 void set_sending(bool sending) { sending_ = sending; }
586 bool sending() const { return sending_; }
587 void set_muted(bool on) {
588 // TODO(asapersson): add support.
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000589 // video_adapter_.SetBlackOutput(on);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000590 muted_ = on;
591 }
592 bool muted() {return muted_; }
593
594 WebRtcEncoderObserver* encoder_observer() { return &encoder_observer_; }
595 webrtc::ViEExternalCapture* external_capture() { return external_capture_; }
596 const VideoFormat& video_format() const {
597 return video_format_;
598 }
599 void set_video_format(const VideoFormat& video_format) {
600 video_format_ = video_format;
601 if (video_format_ != cricket::VideoFormat()) {
602 interval_ = video_format_.interval;
603 }
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000604 CoordinatedVideoAdapter* adapter = video_adapter();
605 if (adapter) {
606 adapter->OnOutputFormatRequest(video_format_);
607 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000608 }
609 void set_interval(int64 interval) {
610 if (video_format() == cricket::VideoFormat()) {
611 interval_ = interval;
612 }
613 }
614 int64 interval() { return interval_; }
615
xians@webrtc.orgef221512014-02-21 10:31:29 +0000616 int CurrentAdaptReason() const {
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000617 const CoordinatedVideoAdapter* adapter = video_adapter();
618 if (!adapter) {
619 return CoordinatedVideoAdapter::ADAPTREASON_NONE;
620 }
621 return video_adapter()->adapt_reason();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000622 }
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000623 webrtc::CpuOveruseObserver* overuse_observer() {
624 return overuse_observer_.get();
625 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000626
627 StreamParams* stream_params() { return stream_params_.get(); }
628 void set_stream_params(const StreamParams& sp) {
629 stream_params_.reset(new StreamParams(sp));
630 }
631 void ClearStreamParams() { stream_params_.reset(); }
632 bool has_ssrc(uint32 local_ssrc) const {
633 return !stream_params_ ? false :
634 stream_params_->has_ssrc(local_ssrc);
635 }
636 WebRtcLocalStreamInfo* local_stream_info() {
637 return &local_stream_info_;
638 }
639 VideoCapturer* video_capturer() {
640 return video_capturer_;
641 }
642 void set_video_capturer(VideoCapturer* video_capturer) {
643 if (video_capturer == video_capturer_) {
644 return;
645 }
xians@webrtc.orgef221512014-02-21 10:31:29 +0000646
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000647 CoordinatedVideoAdapter* old_video_adapter = video_adapter();
648 if (old_video_adapter) {
649 // Disconnect signals from old video adapter.
650 SignalCpuAdaptationUnable.disconnect(old_video_adapter);
651 if (cpu_monitor_) {
652 cpu_monitor_->SignalUpdate.disconnect(old_video_adapter);
xians@webrtc.orgef221512014-02-21 10:31:29 +0000653 }
henrike@webrtc.org26438052014-02-20 22:32:53 +0000654 }
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000655
656 capturer_updated_ = true;
657 video_capturer_ = video_capturer;
658
659 if (!video_capturer) {
660 overuse_observer_.reset();
661 return;
662 }
663
664 CoordinatedVideoAdapter* adapter = video_adapter();
665 ASSERT(adapter && "Video adapter should not be null here.");
666
667 UpdateAdapterCpuOptions();
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000668
669 overuse_observer_.reset(new WebRtcOveruseObserver(adapter));
670 // (Dis)connect the video adapter from the cpu monitor as appropriate.
671 SetCpuOveruseDetection(overuse_observer_enabled_);
672
673 SignalCpuAdaptationUnable.repeat(adapter->SignalCpuAdaptationUnable);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000674 }
675
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000676 CoordinatedVideoAdapter* video_adapter() {
677 if (!video_capturer_) {
678 return NULL;
679 }
680 return video_capturer_->video_adapter();
681 }
682 const CoordinatedVideoAdapter* video_adapter() const {
683 if (!video_capturer_) {
684 return NULL;
685 }
686 return video_capturer_->video_adapter();
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000687 }
688
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000689 void ApplyCpuOptions(const VideoOptions& video_options) {
690 // Use video_options_.SetAll() instead of assignment so that unset value in
691 // video_options will not overwrite the previous option value.
692 video_options_.SetAll(video_options);
693 UpdateAdapterCpuOptions();
694 }
695
696 void UpdateAdapterCpuOptions() {
697 if (!video_capturer_) {
698 return;
699 }
700
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000701 bool cpu_adapt, cpu_smoothing, adapt_third;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000702 float low, med, high;
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000703
704 // TODO(thorcarpenter): Have VideoAdapter be responsible for setting
705 // all these video options.
706 CoordinatedVideoAdapter* video_adapter = video_capturer_->video_adapter();
707 if (video_options_.adapt_input_to_cpu_usage.Get(&cpu_adapt)) {
708 video_adapter->set_cpu_adaptation(cpu_adapt);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000709 }
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000710 if (video_options_.adapt_cpu_with_smoothing.Get(&cpu_smoothing)) {
711 video_adapter->set_cpu_smoothing(cpu_smoothing);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000712 }
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000713 if (video_options_.process_adaptation_threshhold.Get(&med)) {
714 video_adapter->set_process_threshold(med);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000715 }
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000716 if (video_options_.system_low_adaptation_threshhold.Get(&low)) {
717 video_adapter->set_low_system_threshold(low);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000718 }
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000719 if (video_options_.system_high_adaptation_threshhold.Get(&high)) {
720 video_adapter->set_high_system_threshold(high);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000721 }
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000722 if (video_options_.video_adapt_third.Get(&adapt_third)) {
723 video_adapter->set_scale_third(adapt_third);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000724 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000725 }
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000726
727 void SetCpuOveruseDetection(bool enable) {
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000728 overuse_observer_enabled_ = enable;
729
730 if (!overuse_observer_) {
731 // Cannot actually use the overuse detector until it is initialized
732 // with a video adapter.
733 return;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000734 }
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000735 overuse_observer_->Enable(enable);
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000736
737 // If overuse detection is enabled, it will signal the video adapter
738 // instead of the cpu monitor. If disabled, connect the adapter to the
739 // cpu monitor.
740 CoordinatedVideoAdapter* adapter = video_adapter();
741 if (adapter) {
742 adapter->set_cpu_adaptation(enable);
743 if (cpu_monitor_) {
744 if (enable) {
745 cpu_monitor_->SignalUpdate.disconnect(adapter);
746 } else {
747 cpu_monitor_->SignalUpdate.connect(
748 adapter, &CoordinatedVideoAdapter::OnCpuLoadUpdated);
749 }
750 }
751 }
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000752 }
753
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000754 void ProcessFrame(const VideoFrame& original_frame, bool mute,
755 VideoFrame** processed_frame) {
756 if (!mute) {
757 *processed_frame = original_frame.Copy();
758 } else {
759 WebRtcVideoFrame* black_frame = new WebRtcVideoFrame();
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000760 black_frame->InitToBlack(static_cast<int>(original_frame.GetWidth()),
761 static_cast<int>(original_frame.GetHeight()),
762 1, 1,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000763 original_frame.GetElapsedTime(),
764 original_frame.GetTimeStamp());
765 *processed_frame = black_frame;
766 }
767 local_stream_info_.UpdateFrame(*processed_frame);
768 }
769 void RegisterEncoder(int pl_type, webrtc::VideoEncoder* encoder) {
770 ASSERT(!IsEncoderRegistered(pl_type));
771 registered_encoders_[pl_type] = encoder;
772 }
773 bool IsEncoderRegistered(int pl_type) {
774 return registered_encoders_.count(pl_type) != 0;
775 }
776 const EncoderMap& registered_encoders() {
777 return registered_encoders_;
778 }
779 void ClearRegisteredEncoders() {
780 registered_encoders_.clear();
781 }
782
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000783 sigslot::repeater0<> SignalCpuAdaptationUnable;
784
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000785 private:
786 int channel_id_;
787 int capture_id_;
788 bool sending_;
789 bool muted_;
790 VideoCapturer* video_capturer_;
791 WebRtcEncoderObserver encoder_observer_;
792 webrtc::ViEExternalCapture* external_capture_;
793 EncoderMap registered_encoders_;
794
795 VideoFormat video_format_;
796
797 talk_base::scoped_ptr<StreamParams> stream_params_;
798
799 WebRtcLocalStreamInfo local_stream_info_;
800
801 bool capturer_updated_;
802
803 int64 interval_;
804
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000805 talk_base::CpuMonitor* cpu_monitor_;
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000806 talk_base::scoped_ptr<WebRtcOveruseObserver> overuse_observer_;
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000807 bool overuse_observer_enabled_;
808
809 VideoOptions video_options_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000810};
811
812const WebRtcVideoEngine::VideoCodecPref
813 WebRtcVideoEngine::kVideoCodecPrefs[] = {
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000814 {kVp8PayloadName, 100, -1, 0},
815 {kRedPayloadName, 116, -1, 1},
816 {kFecPayloadName, 117, -1, 2},
817 {kRtxCodecName, 96, 100, 3},
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000818};
819
820// The formats are sorted by the descending order of width. We use the order to
821// find the next format for CPU and bandwidth adaptation.
822const VideoFormatPod WebRtcVideoEngine::kVideoFormats[] = {
823 {1280, 800, FPS_TO_INTERVAL(30), FOURCC_ANY},
824 {1280, 720, FPS_TO_INTERVAL(30), FOURCC_ANY},
825 {960, 600, FPS_TO_INTERVAL(30), FOURCC_ANY},
826 {960, 540, FPS_TO_INTERVAL(30), FOURCC_ANY},
827 {640, 400, FPS_TO_INTERVAL(30), FOURCC_ANY},
828 {640, 360, FPS_TO_INTERVAL(30), FOURCC_ANY},
829 {640, 480, FPS_TO_INTERVAL(30), FOURCC_ANY},
830 {480, 300, FPS_TO_INTERVAL(30), FOURCC_ANY},
831 {480, 270, FPS_TO_INTERVAL(30), FOURCC_ANY},
832 {480, 360, FPS_TO_INTERVAL(30), FOURCC_ANY},
833 {320, 200, FPS_TO_INTERVAL(30), FOURCC_ANY},
834 {320, 180, FPS_TO_INTERVAL(30), FOURCC_ANY},
835 {320, 240, FPS_TO_INTERVAL(30), FOURCC_ANY},
836 {240, 150, FPS_TO_INTERVAL(30), FOURCC_ANY},
837 {240, 135, FPS_TO_INTERVAL(30), FOURCC_ANY},
838 {240, 180, FPS_TO_INTERVAL(30), FOURCC_ANY},
839 {160, 100, FPS_TO_INTERVAL(30), FOURCC_ANY},
840 {160, 90, FPS_TO_INTERVAL(30), FOURCC_ANY},
841 {160, 120, FPS_TO_INTERVAL(30), FOURCC_ANY},
842};
843
844const VideoFormatPod WebRtcVideoEngine::kDefaultVideoFormat =
845 {640, 400, FPS_TO_INTERVAL(30), FOURCC_ANY};
846
847static void UpdateVideoCodec(const cricket::VideoFormat& video_format,
848 webrtc::VideoCodec* target_codec) {
849 if ((target_codec == NULL) || (video_format == cricket::VideoFormat())) {
850 return;
851 }
852 target_codec->width = video_format.width;
853 target_codec->height = video_format.height;
854 target_codec->maxFramerate = cricket::VideoFormat::IntervalToFps(
855 video_format.interval);
856}
857
858WebRtcVideoEngine::WebRtcVideoEngine() {
859 Construct(new ViEWrapper(), new ViETraceWrapper(), NULL,
860 new talk_base::CpuMonitor(NULL));
861}
862
863WebRtcVideoEngine::WebRtcVideoEngine(WebRtcVoiceEngine* voice_engine,
864 ViEWrapper* vie_wrapper,
865 talk_base::CpuMonitor* cpu_monitor) {
866 Construct(vie_wrapper, new ViETraceWrapper(), voice_engine, cpu_monitor);
867}
868
869WebRtcVideoEngine::WebRtcVideoEngine(WebRtcVoiceEngine* voice_engine,
870 ViEWrapper* vie_wrapper,
871 ViETraceWrapper* tracing,
872 talk_base::CpuMonitor* cpu_monitor) {
873 Construct(vie_wrapper, tracing, voice_engine, cpu_monitor);
874}
875
876void WebRtcVideoEngine::Construct(ViEWrapper* vie_wrapper,
877 ViETraceWrapper* tracing,
878 WebRtcVoiceEngine* voice_engine,
879 talk_base::CpuMonitor* cpu_monitor) {
880 LOG(LS_INFO) << "WebRtcVideoEngine::WebRtcVideoEngine";
881 worker_thread_ = NULL;
882 vie_wrapper_.reset(vie_wrapper);
883 vie_wrapper_base_initialized_ = false;
884 tracing_.reset(tracing);
885 voice_engine_ = voice_engine;
886 initialized_ = false;
887 SetTraceFilter(SeverityToFilter(kDefaultLogSeverity));
888 render_module_.reset(new WebRtcPassthroughRender());
889 local_renderer_w_ = local_renderer_h_ = 0;
890 local_renderer_ = NULL;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000891 capture_started_ = false;
892 decoder_factory_ = NULL;
893 encoder_factory_ = NULL;
894 cpu_monitor_.reset(cpu_monitor);
895
896 SetTraceOptions("");
897 if (tracing_->SetTraceCallback(this) != 0) {
898 LOG_RTCERR1(SetTraceCallback, this);
899 }
900
901 // Set default quality levels for our supported codecs. We override them here
902 // if we know your cpu performance is low, and they can be updated explicitly
903 // by calling SetDefaultCodec. For example by a flute preference setting, or
904 // by the server with a jec in response to our reported system info.
905 VideoCodec max_codec(kVideoCodecPrefs[0].payload_type,
906 kVideoCodecPrefs[0].name,
907 kDefaultVideoFormat.width,
908 kDefaultVideoFormat.height,
909 VideoFormat::IntervalToFps(kDefaultVideoFormat.interval),
910 0);
911 if (!SetDefaultCodec(max_codec)) {
912 LOG(LS_ERROR) << "Failed to initialize list of supported codec types";
913 }
914
915
916 // Load our RTP Header extensions.
917 rtp_header_extensions_.push_back(
918 RtpHeaderExtension(kRtpTimestampOffsetHeaderExtension,
919 kRtpTimeOffsetExtensionId));
920 rtp_header_extensions_.push_back(
921 RtpHeaderExtension(kRtpAbsoluteSendTimeHeaderExtension,
922 kRtpAbsoluteSendTimeExtensionId));
923}
924
925WebRtcVideoEngine::~WebRtcVideoEngine() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000926 LOG(LS_INFO) << "WebRtcVideoEngine::~WebRtcVideoEngine";
927 if (initialized_) {
928 Terminate();
929 }
930 if (encoder_factory_) {
931 encoder_factory_->RemoveObserver(this);
932 }
933 tracing_->SetTraceCallback(NULL);
934 // Test to see if the media processor was deregistered properly.
935 ASSERT(SignalMediaFrame.is_empty());
936}
937
938bool WebRtcVideoEngine::Init(talk_base::Thread* worker_thread) {
939 LOG(LS_INFO) << "WebRtcVideoEngine::Init";
940 worker_thread_ = worker_thread;
941 ASSERT(worker_thread_ != NULL);
942
943 cpu_monitor_->set_thread(worker_thread_);
944 if (!cpu_monitor_->Start(kCpuMonitorPeriodMs)) {
945 LOG(LS_ERROR) << "Failed to start CPU monitor.";
946 cpu_monitor_.reset();
947 }
948
949 bool result = InitVideoEngine();
950 if (result) {
951 LOG(LS_INFO) << "VideoEngine Init done";
952 } else {
953 LOG(LS_ERROR) << "VideoEngine Init failed, releasing";
954 Terminate();
955 }
956 return result;
957}
958
959bool WebRtcVideoEngine::InitVideoEngine() {
960 LOG(LS_INFO) << "WebRtcVideoEngine::InitVideoEngine";
961
962 // Init WebRTC VideoEngine.
963 if (!vie_wrapper_base_initialized_) {
964 if (vie_wrapper_->base()->Init() != 0) {
965 LOG_RTCERR0(Init);
966 return false;
967 }
968 vie_wrapper_base_initialized_ = true;
969 }
970
971 // Log the VoiceEngine version info.
972 char buffer[1024] = "";
973 if (vie_wrapper_->base()->GetVersion(buffer) != 0) {
974 LOG_RTCERR0(GetVersion);
975 return false;
976 }
977
978 LOG(LS_INFO) << "WebRtc VideoEngine Version:";
979 LogMultiline(talk_base::LS_INFO, buffer);
980
981 // Hook up to VoiceEngine for sync purposes, if supplied.
982 if (!voice_engine_) {
983 LOG(LS_WARNING) << "NULL voice engine";
984 } else if ((vie_wrapper_->base()->SetVoiceEngine(
985 voice_engine_->voe()->engine())) != 0) {
986 LOG_RTCERR0(SetVoiceEngine);
987 return false;
988 }
989
990 // Register our custom render module.
991 if (vie_wrapper_->render()->RegisterVideoRenderModule(
992 *render_module_.get()) != 0) {
993 LOG_RTCERR0(RegisterVideoRenderModule);
994 return false;
995 }
996
997 initialized_ = true;
998 return true;
999}
1000
1001void WebRtcVideoEngine::Terminate() {
1002 LOG(LS_INFO) << "WebRtcVideoEngine::Terminate";
1003 initialized_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001004
1005 if (vie_wrapper_->render()->DeRegisterVideoRenderModule(
1006 *render_module_.get()) != 0) {
1007 LOG_RTCERR0(DeRegisterVideoRenderModule);
1008 }
1009
1010 if (vie_wrapper_->base()->SetVoiceEngine(NULL) != 0) {
1011 LOG_RTCERR0(SetVoiceEngine);
1012 }
1013
1014 cpu_monitor_->Stop();
1015}
1016
1017int WebRtcVideoEngine::GetCapabilities() {
1018 return VIDEO_RECV | VIDEO_SEND;
1019}
1020
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00001021bool WebRtcVideoEngine::SetOptions(const VideoOptions &options) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001022 return true;
1023}
1024
1025bool WebRtcVideoEngine::SetDefaultEncoderConfig(
1026 const VideoEncoderConfig& config) {
1027 return SetDefaultCodec(config.max_codec);
1028}
1029
wu@webrtc.org78187522013-10-07 23:32:02 +00001030VideoEncoderConfig WebRtcVideoEngine::GetDefaultEncoderConfig() const {
1031 ASSERT(!video_codecs_.empty());
1032 VideoCodec max_codec(kVideoCodecPrefs[0].payload_type,
1033 kVideoCodecPrefs[0].name,
1034 video_codecs_[0].width,
1035 video_codecs_[0].height,
1036 video_codecs_[0].framerate,
1037 0);
1038 return VideoEncoderConfig(max_codec);
1039}
1040
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001041// SetDefaultCodec may be called while the capturer is running. For example, a
1042// test call is started in a page with QVGA default codec, and then a real call
1043// is started in another page with VGA default codec. This is the corner case
1044// and happens only when a session is started. We ignore this case currently.
1045bool WebRtcVideoEngine::SetDefaultCodec(const VideoCodec& codec) {
1046 if (!RebuildCodecList(codec)) {
1047 LOG(LS_WARNING) << "Failed to RebuildCodecList";
1048 return false;
1049 }
1050
wu@webrtc.org78187522013-10-07 23:32:02 +00001051 ASSERT(!video_codecs_.empty());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001052 default_codec_format_ = VideoFormat(
1053 video_codecs_[0].width,
1054 video_codecs_[0].height,
1055 VideoFormat::FpsToInterval(video_codecs_[0].framerate),
1056 FOURCC_ANY);
1057 return true;
1058}
1059
1060WebRtcVideoMediaChannel* WebRtcVideoEngine::CreateChannel(
1061 VoiceMediaChannel* voice_channel) {
1062 WebRtcVideoMediaChannel* channel =
1063 new WebRtcVideoMediaChannel(this, voice_channel);
1064 if (!channel->Init()) {
1065 delete channel;
1066 channel = NULL;
1067 }
1068 return channel;
1069}
1070
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001071bool WebRtcVideoEngine::SetLocalRenderer(VideoRenderer* renderer) {
1072 local_renderer_w_ = local_renderer_h_ = 0;
1073 local_renderer_ = renderer;
1074 return true;
1075}
1076
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001077const std::vector<VideoCodec>& WebRtcVideoEngine::codecs() const {
1078 return video_codecs_;
1079}
1080
1081const std::vector<RtpHeaderExtension>&
1082WebRtcVideoEngine::rtp_header_extensions() const {
1083 return rtp_header_extensions_;
1084}
1085
1086void WebRtcVideoEngine::SetLogging(int min_sev, const char* filter) {
1087 // if min_sev == -1, we keep the current log level.
1088 if (min_sev >= 0) {
1089 SetTraceFilter(SeverityToFilter(min_sev));
1090 }
1091 SetTraceOptions(filter);
1092}
1093
1094int WebRtcVideoEngine::GetLastEngineError() {
1095 return vie_wrapper_->error();
1096}
1097
1098// Checks to see whether we comprehend and could receive a particular codec
1099bool WebRtcVideoEngine::FindCodec(const VideoCodec& in) {
1100 for (int i = 0; i < ARRAY_SIZE(kVideoFormats); ++i) {
1101 const VideoFormat fmt(kVideoFormats[i]);
1102 if ((in.width == 0 && in.height == 0) ||
1103 (fmt.width == in.width && fmt.height == in.height)) {
1104 if (encoder_factory_) {
1105 const std::vector<WebRtcVideoEncoderFactory::VideoCodec>& codecs =
1106 encoder_factory_->codecs();
1107 for (size_t j = 0; j < codecs.size(); ++j) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001108 VideoCodec codec(GetExternalVideoPayloadType(static_cast<int>(j)),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001109 codecs[j].name, 0, 0, 0, 0);
1110 if (codec.Matches(in))
1111 return true;
1112 }
1113 }
1114 for (size_t j = 0; j < ARRAY_SIZE(kVideoCodecPrefs); ++j) {
1115 VideoCodec codec(kVideoCodecPrefs[j].payload_type,
1116 kVideoCodecPrefs[j].name, 0, 0, 0, 0);
1117 if (codec.Matches(in)) {
1118 return true;
1119 }
1120 }
1121 }
1122 }
1123 return false;
1124}
1125
1126// Given the requested codec, returns true if we can send that codec type and
1127// updates out with the best quality we could send for that codec. If current is
1128// not empty, we constrain out so that its aspect ratio matches current's.
1129bool WebRtcVideoEngine::CanSendCodec(const VideoCodec& requested,
1130 const VideoCodec& current,
1131 VideoCodec* out) {
1132 if (!out) {
1133 return false;
1134 }
1135
1136 std::vector<VideoCodec>::const_iterator local_max;
1137 for (local_max = video_codecs_.begin();
1138 local_max < video_codecs_.end();
1139 ++local_max) {
1140 // First match codecs by payload type
1141 if (!requested.Matches(*local_max)) {
1142 continue;
1143 }
1144
1145 out->id = requested.id;
1146 out->name = requested.name;
1147 out->preference = requested.preference;
1148 out->params = requested.params;
1149 out->framerate = talk_base::_min(requested.framerate, local_max->framerate);
1150 out->width = 0;
1151 out->height = 0;
1152 out->params = requested.params;
1153 out->feedback_params = requested.feedback_params;
1154
1155 if (0 == requested.width && 0 == requested.height) {
1156 // Special case with resolution 0. The channel should not send frames.
1157 return true;
1158 } else if (0 == requested.width || 0 == requested.height) {
1159 // 0xn and nx0 are invalid resolutions.
1160 return false;
1161 }
1162
1163 // Pick the best quality that is within their and our bounds and has the
1164 // correct aspect ratio.
1165 for (int j = 0; j < ARRAY_SIZE(kVideoFormats); ++j) {
1166 const VideoFormat format(kVideoFormats[j]);
1167
1168 // Skip any format that is larger than the local or remote maximums, or
1169 // smaller than the current best match
1170 if (format.width > requested.width || format.height > requested.height ||
1171 format.width > local_max->width ||
1172 (format.width < out->width && format.height < out->height)) {
1173 continue;
1174 }
1175
1176 bool better = false;
1177
1178 // Check any further constraints on this prospective format
1179 if (!out->width || !out->height) {
1180 // If we don't have any matches yet, this is the best so far.
1181 better = true;
1182 } else if (current.width && current.height) {
1183 // current is set so format must match its ratio exactly.
1184 better =
1185 (format.width * current.height == format.height * current.width);
1186 } else {
1187 // Prefer closer aspect ratios i.e
1188 // format.aspect - requested.aspect < out.aspect - requested.aspect
1189 better = abs(format.width * requested.height * out->height -
1190 requested.width * format.height * out->height) <
1191 abs(out->width * format.height * requested.height -
1192 requested.width * format.height * out->height);
1193 }
1194
1195 if (better) {
1196 out->width = format.width;
1197 out->height = format.height;
1198 }
1199 }
1200 if (out->width > 0) {
1201 return true;
1202 }
1203 }
1204 return false;
1205}
1206
1207static void ConvertToCricketVideoCodec(
1208 const webrtc::VideoCodec& in_codec, VideoCodec* out_codec) {
1209 out_codec->id = in_codec.plType;
1210 out_codec->name = in_codec.plName;
1211 out_codec->width = in_codec.width;
1212 out_codec->height = in_codec.height;
1213 out_codec->framerate = in_codec.maxFramerate;
1214 out_codec->SetParam(kCodecParamMinBitrate, in_codec.minBitrate);
1215 out_codec->SetParam(kCodecParamMaxBitrate, in_codec.maxBitrate);
1216 if (in_codec.qpMax) {
1217 out_codec->SetParam(kCodecParamMaxQuantization, in_codec.qpMax);
1218 }
1219}
1220
1221bool WebRtcVideoEngine::ConvertFromCricketVideoCodec(
1222 const VideoCodec& in_codec, webrtc::VideoCodec* out_codec) {
1223 bool found = false;
1224 int ncodecs = vie_wrapper_->codec()->NumberOfCodecs();
1225 for (int i = 0; i < ncodecs; ++i) {
1226 if (vie_wrapper_->codec()->GetCodec(i, *out_codec) == 0 &&
1227 _stricmp(in_codec.name.c_str(), out_codec->plName) == 0) {
1228 found = true;
1229 break;
1230 }
1231 }
1232
1233 // If not found, check if this is supported by external encoder factory.
1234 if (!found && encoder_factory_) {
1235 const std::vector<WebRtcVideoEncoderFactory::VideoCodec>& codecs =
1236 encoder_factory_->codecs();
1237 for (size_t i = 0; i < codecs.size(); ++i) {
1238 if (_stricmp(in_codec.name.c_str(), codecs[i].name.c_str()) == 0) {
1239 out_codec->codecType = codecs[i].type;
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001240 out_codec->plType = GetExternalVideoPayloadType(static_cast<int>(i));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001241 talk_base::strcpyn(out_codec->plName, sizeof(out_codec->plName),
1242 codecs[i].name.c_str(), codecs[i].name.length());
1243 found = true;
1244 break;
1245 }
1246 }
1247 }
1248
1249 if (!found) {
1250 LOG(LS_ERROR) << "invalid codec type";
1251 return false;
1252 }
1253
1254 if (in_codec.id != 0)
1255 out_codec->plType = in_codec.id;
1256
1257 if (in_codec.width != 0)
1258 out_codec->width = in_codec.width;
1259
1260 if (in_codec.height != 0)
1261 out_codec->height = in_codec.height;
1262
1263 if (in_codec.framerate != 0)
1264 out_codec->maxFramerate = in_codec.framerate;
1265
1266 // Convert bitrate parameters.
1267 int max_bitrate = kMaxVideoBitrate;
1268 int min_bitrate = kMinVideoBitrate;
1269 int start_bitrate = kStartVideoBitrate;
1270
1271 in_codec.GetParam(kCodecParamMinBitrate, &min_bitrate);
1272 in_codec.GetParam(kCodecParamMaxBitrate, &max_bitrate);
1273
1274 if (max_bitrate < min_bitrate) {
1275 return false;
1276 }
1277 start_bitrate = talk_base::_max(start_bitrate, min_bitrate);
1278 start_bitrate = talk_base::_min(start_bitrate, max_bitrate);
1279
1280 out_codec->minBitrate = min_bitrate;
1281 out_codec->startBitrate = start_bitrate;
1282 out_codec->maxBitrate = max_bitrate;
1283
1284 // Convert general codec parameters.
1285 int max_quantization = 0;
1286 if (in_codec.GetParam(kCodecParamMaxQuantization, &max_quantization)) {
1287 if (max_quantization < 0) {
1288 return false;
1289 }
1290 out_codec->qpMax = max_quantization;
1291 }
1292 return true;
1293}
1294
1295void WebRtcVideoEngine::RegisterChannel(WebRtcVideoMediaChannel *channel) {
1296 talk_base::CritScope cs(&channels_crit_);
1297 channels_.push_back(channel);
1298}
1299
1300void WebRtcVideoEngine::UnregisterChannel(WebRtcVideoMediaChannel *channel) {
1301 talk_base::CritScope cs(&channels_crit_);
1302 channels_.erase(std::remove(channels_.begin(), channels_.end(), channel),
1303 channels_.end());
1304}
1305
1306bool WebRtcVideoEngine::SetVoiceEngine(WebRtcVoiceEngine* voice_engine) {
1307 if (initialized_) {
1308 LOG(LS_WARNING) << "SetVoiceEngine can not be called after Init";
1309 return false;
1310 }
1311 voice_engine_ = voice_engine;
1312 return true;
1313}
1314
1315bool WebRtcVideoEngine::EnableTimedRender() {
1316 if (initialized_) {
1317 LOG(LS_WARNING) << "EnableTimedRender can not be called after Init";
1318 return false;
1319 }
1320 render_module_.reset(webrtc::VideoRender::CreateVideoRender(0, NULL,
1321 false, webrtc::kRenderExternal));
1322 return true;
1323}
1324
1325void WebRtcVideoEngine::SetTraceFilter(int filter) {
1326 tracing_->SetTraceFilter(filter);
1327}
1328
1329// See https://sites.google.com/a/google.com/wavelet/
1330// Home/Magic-Flute--RTC-Engine-/Magic-Flute-Command-Line-Parameters
1331// for all supported command line setttings.
1332void WebRtcVideoEngine::SetTraceOptions(const std::string& options) {
1333 // Set WebRTC trace file.
1334 std::vector<std::string> opts;
1335 talk_base::tokenize(options, ' ', '"', '"', &opts);
1336 std::vector<std::string>::iterator tracefile =
1337 std::find(opts.begin(), opts.end(), "tracefile");
1338 if (tracefile != opts.end() && ++tracefile != opts.end()) {
1339 // Write WebRTC debug output (at same loglevel) to file
1340 if (tracing_->SetTraceFile(tracefile->c_str()) == -1) {
1341 LOG_RTCERR1(SetTraceFile, *tracefile);
1342 }
1343 }
1344}
1345
1346static void AddDefaultFeedbackParams(VideoCodec* codec) {
1347 const FeedbackParam kFir(kRtcpFbParamCcm, kRtcpFbCcmParamFir);
1348 codec->AddFeedbackParam(kFir);
1349 const FeedbackParam kNack(kRtcpFbParamNack, kParamValueEmpty);
1350 codec->AddFeedbackParam(kNack);
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001351 const FeedbackParam kPli(kRtcpFbParamNack, kRtcpFbNackParamPli);
1352 codec->AddFeedbackParam(kPli);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001353 const FeedbackParam kRemb(kRtcpFbParamRemb, kParamValueEmpty);
1354 codec->AddFeedbackParam(kRemb);
1355}
1356
1357// Rebuilds the codec list to be only those that are less intensive
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00001358// than the specified codec. Prefers internal codec over external with
1359// higher preference field.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001360bool WebRtcVideoEngine::RebuildCodecList(const VideoCodec& in_codec) {
1361 if (!FindCodec(in_codec))
1362 return false;
1363
1364 video_codecs_.clear();
1365
1366 bool found = false;
mallinath@webrtc.org1112c302013-09-23 20:34:45 +00001367 std::set<std::string> internal_codec_names;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001368 for (size_t i = 0; i < ARRAY_SIZE(kVideoCodecPrefs); ++i) {
1369 const VideoCodecPref& pref(kVideoCodecPrefs[i]);
1370 if (!found)
1371 found = (in_codec.name == pref.name);
mallinath@webrtc.org1112c302013-09-23 20:34:45 +00001372 if (found) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001373 VideoCodec codec(pref.payload_type, pref.name,
1374 in_codec.width, in_codec.height, in_codec.framerate,
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001375 static_cast<int>(ARRAY_SIZE(kVideoCodecPrefs) - i));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001376 if (_stricmp(kVp8PayloadName, codec.name.c_str()) == 0) {
1377 AddDefaultFeedbackParams(&codec);
1378 }
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00001379 if (pref.associated_payload_type != -1) {
1380 codec.SetParam(kCodecParamAssociatedPayloadType,
1381 pref.associated_payload_type);
1382 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001383 video_codecs_.push_back(codec);
mallinath@webrtc.org1112c302013-09-23 20:34:45 +00001384 internal_codec_names.insert(codec.name);
1385 }
1386 }
1387 if (encoder_factory_) {
1388 const std::vector<WebRtcVideoEncoderFactory::VideoCodec>& codecs =
1389 encoder_factory_->codecs();
1390 for (size_t i = 0; i < codecs.size(); ++i) {
1391 bool is_internal_codec = internal_codec_names.find(codecs[i].name) !=
1392 internal_codec_names.end();
1393 if (!is_internal_codec) {
1394 if (!found)
1395 found = (in_codec.name == codecs[i].name);
1396 VideoCodec codec(
1397 GetExternalVideoPayloadType(static_cast<int>(i)),
1398 codecs[i].name,
1399 codecs[i].max_width,
1400 codecs[i].max_height,
1401 codecs[i].max_fps,
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00001402 // Use negative preference on external codec to ensure the internal
1403 // codec is preferred.
1404 static_cast<int>(0 - i));
mallinath@webrtc.org1112c302013-09-23 20:34:45 +00001405 AddDefaultFeedbackParams(&codec);
1406 video_codecs_.push_back(codec);
1407 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001408 }
1409 }
1410 ASSERT(found);
1411 return true;
1412}
1413
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001414// Ignore spammy trace messages, mostly from the stats API when we haven't
1415// gotten RTCP info yet from the remote side.
1416bool WebRtcVideoEngine::ShouldIgnoreTrace(const std::string& trace) {
1417 static const char* const kTracesToIgnore[] = {
1418 NULL
1419 };
1420 for (const char* const* p = kTracesToIgnore; *p; ++p) {
1421 if (trace.find(*p) == 0) {
1422 return true;
1423 }
1424 }
1425 return false;
1426}
1427
1428int WebRtcVideoEngine::GetNumOfChannels() {
1429 talk_base::CritScope cs(&channels_crit_);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001430 return static_cast<int>(channels_.size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001431}
1432
1433void WebRtcVideoEngine::Print(webrtc::TraceLevel level, const char* trace,
1434 int length) {
1435 talk_base::LoggingSeverity sev = talk_base::LS_VERBOSE;
1436 if (level == webrtc::kTraceError || level == webrtc::kTraceCritical)
1437 sev = talk_base::LS_ERROR;
1438 else if (level == webrtc::kTraceWarning)
1439 sev = talk_base::LS_WARNING;
1440 else if (level == webrtc::kTraceStateInfo || level == webrtc::kTraceInfo)
1441 sev = talk_base::LS_INFO;
1442 else if (level == webrtc::kTraceTerseInfo)
1443 sev = talk_base::LS_INFO;
1444
1445 // Skip past boilerplate prefix text
1446 if (length < 72) {
1447 std::string msg(trace, length);
1448 LOG(LS_ERROR) << "Malformed webrtc log message: ";
1449 LOG_V(sev) << msg;
1450 } else {
1451 std::string msg(trace + 71, length - 72);
1452 if (!ShouldIgnoreTrace(msg) &&
1453 (!voice_engine_ || !voice_engine_->ShouldIgnoreTrace(msg))) {
1454 LOG_V(sev) << "webrtc: " << msg;
1455 }
1456 }
1457}
1458
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001459webrtc::VideoDecoder* WebRtcVideoEngine::CreateExternalDecoder(
1460 webrtc::VideoCodecType type) {
1461 if (decoder_factory_ == NULL) {
1462 return NULL;
1463 }
1464 return decoder_factory_->CreateVideoDecoder(type);
1465}
1466
1467void WebRtcVideoEngine::DestroyExternalDecoder(webrtc::VideoDecoder* decoder) {
1468 ASSERT(decoder_factory_ != NULL);
1469 if (decoder_factory_ == NULL)
1470 return;
1471 decoder_factory_->DestroyVideoDecoder(decoder);
1472}
1473
1474webrtc::VideoEncoder* WebRtcVideoEngine::CreateExternalEncoder(
1475 webrtc::VideoCodecType type) {
1476 if (encoder_factory_ == NULL) {
1477 return NULL;
1478 }
1479 return encoder_factory_->CreateVideoEncoder(type);
1480}
1481
1482void WebRtcVideoEngine::DestroyExternalEncoder(webrtc::VideoEncoder* encoder) {
1483 ASSERT(encoder_factory_ != NULL);
1484 if (encoder_factory_ == NULL)
1485 return;
1486 encoder_factory_->DestroyVideoEncoder(encoder);
1487}
1488
1489bool WebRtcVideoEngine::IsExternalEncoderCodecType(
1490 webrtc::VideoCodecType type) const {
1491 if (!encoder_factory_)
1492 return false;
1493 const std::vector<WebRtcVideoEncoderFactory::VideoCodec>& codecs =
1494 encoder_factory_->codecs();
1495 std::vector<WebRtcVideoEncoderFactory::VideoCodec>::const_iterator it;
1496 for (it = codecs.begin(); it != codecs.end(); ++it) {
1497 if (it->type == type)
1498 return true;
1499 }
1500 return false;
1501}
1502
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001503void WebRtcVideoEngine::SetExternalDecoderFactory(
1504 WebRtcVideoDecoderFactory* decoder_factory) {
1505 decoder_factory_ = decoder_factory;
1506}
1507
1508void WebRtcVideoEngine::SetExternalEncoderFactory(
1509 WebRtcVideoEncoderFactory* encoder_factory) {
1510 if (encoder_factory_ == encoder_factory)
1511 return;
1512
1513 if (encoder_factory_) {
1514 encoder_factory_->RemoveObserver(this);
1515 }
1516 encoder_factory_ = encoder_factory;
1517 if (encoder_factory_) {
1518 encoder_factory_->AddObserver(this);
1519 }
1520
1521 // Invoke OnCodecAvailable() here in case the list of codecs is already
1522 // available when the encoder factory is installed. If not the encoder
1523 // factory will invoke the callback later when the codecs become available.
1524 OnCodecsAvailable();
1525}
1526
1527void WebRtcVideoEngine::OnCodecsAvailable() {
1528 // Rebuild codec list while reapplying the current default codec format.
1529 VideoCodec max_codec(kVideoCodecPrefs[0].payload_type,
1530 kVideoCodecPrefs[0].name,
1531 video_codecs_[0].width,
1532 video_codecs_[0].height,
1533 video_codecs_[0].framerate,
1534 0);
1535 if (!RebuildCodecList(max_codec)) {
1536 LOG(LS_ERROR) << "Failed to initialize list of supported codec types";
1537 }
1538}
1539
1540// WebRtcVideoMediaChannel
1541
1542WebRtcVideoMediaChannel::WebRtcVideoMediaChannel(
1543 WebRtcVideoEngine* engine,
1544 VoiceMediaChannel* channel)
1545 : engine_(engine),
1546 voice_channel_(channel),
1547 vie_channel_(-1),
1548 nack_enabled_(true),
1549 remb_enabled_(false),
1550 render_started_(false),
1551 first_receive_ssrc_(0),
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001552 num_unsignalled_recv_channels_(0),
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00001553 send_rtx_type_(-1),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001554 send_red_type_(-1),
1555 send_fec_type_(-1),
1556 send_min_bitrate_(kMinVideoBitrate),
1557 send_start_bitrate_(kStartVideoBitrate),
1558 send_max_bitrate_(kMaxVideoBitrate),
1559 sending_(false),
1560 ratio_w_(0),
1561 ratio_h_(0) {
1562 engine->RegisterChannel(this);
1563}
1564
1565bool WebRtcVideoMediaChannel::Init() {
1566 const uint32 ssrc_key = 0;
1567 return CreateChannel(ssrc_key, MD_SENDRECV, &vie_channel_);
1568}
1569
1570WebRtcVideoMediaChannel::~WebRtcVideoMediaChannel() {
1571 const bool send = false;
1572 SetSend(send);
1573 const bool render = false;
1574 SetRender(render);
1575
1576 while (!send_channels_.empty()) {
1577 if (!DeleteSendChannel(send_channels_.begin()->first)) {
1578 LOG(LS_ERROR) << "Unable to delete channel with ssrc key "
1579 << send_channels_.begin()->first;
1580 ASSERT(false);
1581 break;
1582 }
1583 }
1584
1585 // Remove all receive streams and the default channel.
1586 while (!recv_channels_.empty()) {
1587 RemoveRecvStream(recv_channels_.begin()->first);
1588 }
1589
1590 // Unregister the channel from the engine.
1591 engine()->UnregisterChannel(this);
1592 if (worker_thread()) {
1593 worker_thread()->Clear(this);
1594 }
1595}
1596
1597bool WebRtcVideoMediaChannel::SetRecvCodecs(
1598 const std::vector<VideoCodec>& codecs) {
1599 receive_codecs_.clear();
1600 for (std::vector<VideoCodec>::const_iterator iter = codecs.begin();
1601 iter != codecs.end(); ++iter) {
1602 if (engine()->FindCodec(*iter)) {
1603 webrtc::VideoCodec wcodec;
1604 if (engine()->ConvertFromCricketVideoCodec(*iter, &wcodec)) {
1605 receive_codecs_.push_back(wcodec);
1606 }
1607 } else {
1608 LOG(LS_INFO) << "Unknown codec " << iter->name;
1609 return false;
1610 }
1611 }
1612
1613 for (RecvChannelMap::iterator it = recv_channels_.begin();
1614 it != recv_channels_.end(); ++it) {
1615 if (!SetReceiveCodecs(it->second))
1616 return false;
1617 }
1618 return true;
1619}
1620
1621bool WebRtcVideoMediaChannel::SetSendCodecs(
1622 const std::vector<VideoCodec>& codecs) {
1623 // Match with local video codec list.
1624 std::vector<webrtc::VideoCodec> send_codecs;
1625 VideoCodec checked_codec;
1626 VideoCodec current; // defaults to 0x0
1627 if (sending_) {
1628 ConvertToCricketVideoCodec(*send_codec_, &current);
1629 }
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00001630 std::map<int, int> primary_rtx_pt_mapping;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001631 for (std::vector<VideoCodec>::const_iterator iter = codecs.begin();
1632 iter != codecs.end(); ++iter) {
1633 if (_stricmp(iter->name.c_str(), kRedPayloadName) == 0) {
1634 send_red_type_ = iter->id;
1635 } else if (_stricmp(iter->name.c_str(), kFecPayloadName) == 0) {
1636 send_fec_type_ = iter->id;
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00001637 } else if (_stricmp(iter->name.c_str(), kRtxCodecName) == 0) {
1638 int rtx_type = iter->id;
1639 int rtx_primary_type = -1;
1640 if (iter->GetParam(kCodecParamAssociatedPayloadType, &rtx_primary_type)) {
1641 primary_rtx_pt_mapping[rtx_primary_type] = rtx_type;
1642 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001643 } else if (engine()->CanSendCodec(*iter, current, &checked_codec)) {
1644 webrtc::VideoCodec wcodec;
1645 if (engine()->ConvertFromCricketVideoCodec(checked_codec, &wcodec)) {
1646 if (send_codecs.empty()) {
1647 nack_enabled_ = IsNackEnabled(checked_codec);
1648 remb_enabled_ = IsRembEnabled(checked_codec);
1649 }
1650 send_codecs.push_back(wcodec);
1651 }
1652 } else {
1653 LOG(LS_WARNING) << "Unknown codec " << iter->name;
1654 }
1655 }
1656
1657 // Fail if we don't have a match.
1658 if (send_codecs.empty()) {
1659 LOG(LS_WARNING) << "No matching codecs available";
1660 return false;
1661 }
1662
1663 // Recv protection.
1664 for (RecvChannelMap::iterator it = recv_channels_.begin();
1665 it != recv_channels_.end(); ++it) {
1666 int channel_id = it->second->channel_id();
1667 if (!SetNackFec(channel_id, send_red_type_, send_fec_type_,
1668 nack_enabled_)) {
1669 return false;
1670 }
1671 if (engine_->vie()->rtp()->SetRembStatus(channel_id,
1672 kNotSending,
1673 remb_enabled_) != 0) {
1674 LOG_RTCERR3(SetRembStatus, channel_id, kNotSending, remb_enabled_);
1675 return false;
1676 }
1677 }
1678
1679 // Send settings.
1680 for (SendChannelMap::iterator iter = send_channels_.begin();
1681 iter != send_channels_.end(); ++iter) {
1682 int channel_id = iter->second->channel_id();
1683 if (!SetNackFec(channel_id, send_red_type_, send_fec_type_,
1684 nack_enabled_)) {
1685 return false;
1686 }
1687 if (engine_->vie()->rtp()->SetRembStatus(channel_id,
1688 remb_enabled_,
1689 remb_enabled_) != 0) {
1690 LOG_RTCERR3(SetRembStatus, channel_id, remb_enabled_, remb_enabled_);
1691 return false;
1692 }
1693 }
1694
1695 // Select the first matched codec.
1696 webrtc::VideoCodec& codec(send_codecs[0]);
1697
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00001698 // Set RTX payload type if primary now active. This value will be used in
1699 // SetSendCodec.
1700 std::map<int, int>::const_iterator rtx_it =
1701 primary_rtx_pt_mapping.find(static_cast<int>(codec.plType));
1702 if (rtx_it != primary_rtx_pt_mapping.end()) {
1703 send_rtx_type_ = rtx_it->second;
1704 }
1705
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001706 if (!SetSendCodec(
1707 codec, codec.minBitrate, codec.startBitrate, codec.maxBitrate)) {
1708 return false;
1709 }
1710
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001711 LogSendCodecChange("SetSendCodecs()");
1712
1713 return true;
1714}
1715
1716bool WebRtcVideoMediaChannel::GetSendCodec(VideoCodec* send_codec) {
1717 if (!send_codec_) {
1718 return false;
1719 }
1720 ConvertToCricketVideoCodec(*send_codec_, send_codec);
1721 return true;
1722}
1723
1724bool WebRtcVideoMediaChannel::SetSendStreamFormat(uint32 ssrc,
1725 const VideoFormat& format) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001726 WebRtcVideoChannelSendInfo* send_channel = GetSendChannel(ssrc);
1727 if (!send_channel) {
1728 LOG(LS_ERROR) << "The specified ssrc " << ssrc << " is not in use.";
1729 return false;
1730 }
1731 send_channel->set_video_format(format);
1732 return true;
1733}
1734
1735bool WebRtcVideoMediaChannel::SetRender(bool render) {
1736 if (render == render_started_) {
1737 return true; // no action required
1738 }
1739
1740 bool ret = true;
1741 for (RecvChannelMap::iterator it = recv_channels_.begin();
1742 it != recv_channels_.end(); ++it) {
1743 if (render) {
1744 if (engine()->vie()->render()->StartRender(
1745 it->second->channel_id()) != 0) {
1746 LOG_RTCERR1(StartRender, it->second->channel_id());
1747 ret = false;
1748 }
1749 } else {
1750 if (engine()->vie()->render()->StopRender(
1751 it->second->channel_id()) != 0) {
1752 LOG_RTCERR1(StopRender, it->second->channel_id());
1753 ret = false;
1754 }
1755 }
1756 }
1757 if (ret) {
1758 render_started_ = render;
1759 }
1760
1761 return ret;
1762}
1763
1764bool WebRtcVideoMediaChannel::SetSend(bool send) {
1765 if (!HasReadySendChannels() && send) {
1766 LOG(LS_ERROR) << "No stream added";
1767 return false;
1768 }
1769 if (send == sending()) {
1770 return true; // No action required.
1771 }
1772
1773 if (send) {
1774 // We've been asked to start sending.
1775 // SetSendCodecs must have been called already.
1776 if (!send_codec_) {
1777 return false;
1778 }
1779 // Start send now.
1780 if (!StartSend()) {
1781 return false;
1782 }
1783 } else {
1784 // We've been asked to stop sending.
1785 if (!StopSend()) {
1786 return false;
1787 }
1788 }
1789 sending_ = send;
1790
1791 return true;
1792}
1793
1794bool WebRtcVideoMediaChannel::AddSendStream(const StreamParams& sp) {
1795 LOG(LS_INFO) << "AddSendStream " << sp.ToString();
1796
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00001797 if (!IsOneSsrcStream(sp) && !IsSimulcastStream(sp)) {
1798 LOG(LS_ERROR) << "AddSendStream: bad local stream parameters";
1799 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001800 }
1801
1802 uint32 ssrc_key;
1803 if (!CreateSendChannelKey(sp.first_ssrc(), &ssrc_key)) {
1804 LOG(LS_ERROR) << "Trying to register duplicate ssrc: " << sp.first_ssrc();
1805 return false;
1806 }
1807 // If the default channel is already used for sending create a new channel
1808 // otherwise use the default channel for sending.
1809 int channel_id = -1;
1810 if (send_channels_[0]->stream_params() == NULL) {
1811 channel_id = vie_channel_;
1812 } else {
1813 if (!CreateChannel(ssrc_key, MD_SEND, &channel_id)) {
1814 LOG(LS_ERROR) << "AddSendStream: unable to create channel";
1815 return false;
1816 }
1817 }
1818 WebRtcVideoChannelSendInfo* send_channel = send_channels_[ssrc_key];
1819 // Set the send (local) SSRC.
1820 // If there are multiple send SSRCs, we can only set the first one here, and
1821 // the rest of the SSRC(s) need to be set after SetSendCodec has been called
1822 // (with a codec requires multiple SSRC(s)).
1823 if (engine()->vie()->rtp()->SetLocalSSRC(channel_id,
1824 sp.first_ssrc()) != 0) {
1825 LOG_RTCERR2(SetLocalSSRC, channel_id, sp.first_ssrc());
1826 return false;
1827 }
1828
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00001829 // Set the corresponding RTX SSRC.
1830 if (!SetLocalRtxSsrc(channel_id, sp, sp.first_ssrc(), 0)) {
1831 return false;
1832 }
1833
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001834 // Set RTCP CName.
1835 if (engine()->vie()->rtp()->SetRTCPCName(channel_id,
1836 sp.cname.c_str()) != 0) {
1837 LOG_RTCERR2(SetRTCPCName, channel_id, sp.cname.c_str());
1838 return false;
1839 }
1840
1841 // At this point the channel's local SSRC has been updated. If the channel is
1842 // the default channel make sure that all the receive channels are updated as
1843 // well. Receive channels have to have the same SSRC as the default channel in
1844 // order to send receiver reports with this SSRC.
1845 if (IsDefaultChannel(channel_id)) {
1846 for (RecvChannelMap::const_iterator it = recv_channels_.begin();
1847 it != recv_channels_.end(); ++it) {
1848 WebRtcVideoChannelRecvInfo* info = it->second;
1849 int channel_id = info->channel_id();
1850 if (engine()->vie()->rtp()->SetLocalSSRC(channel_id,
1851 sp.first_ssrc()) != 0) {
1852 LOG_RTCERR1(SetLocalSSRC, it->first);
1853 return false;
1854 }
1855 }
1856 }
1857
1858 send_channel->set_stream_params(sp);
1859
1860 // Reset send codec after stream parameters changed.
1861 if (send_codec_) {
1862 if (!SetSendCodec(send_channel, *send_codec_, send_min_bitrate_,
1863 send_start_bitrate_, send_max_bitrate_)) {
1864 return false;
1865 }
1866 LogSendCodecChange("SetSendStreamFormat()");
1867 }
1868
1869 if (sending_) {
1870 return StartSend(send_channel);
1871 }
1872 return true;
1873}
1874
1875bool WebRtcVideoMediaChannel::RemoveSendStream(uint32 ssrc) {
1876 uint32 ssrc_key;
1877 if (!GetSendChannelKey(ssrc, &ssrc_key)) {
1878 LOG(LS_WARNING) << "Try to remove stream with ssrc " << ssrc
1879 << " which doesn't exist.";
1880 return false;
1881 }
1882 WebRtcVideoChannelSendInfo* send_channel = send_channels_[ssrc_key];
1883 int channel_id = send_channel->channel_id();
1884 if (IsDefaultChannel(channel_id) && (send_channel->stream_params() == NULL)) {
1885 // Default channel will still exist. However, if stream_params() is NULL
1886 // there is no stream to remove.
1887 return false;
1888 }
1889 if (sending_) {
1890 StopSend(send_channel);
1891 }
1892
1893 const WebRtcVideoChannelSendInfo::EncoderMap& encoder_map =
1894 send_channel->registered_encoders();
1895 for (WebRtcVideoChannelSendInfo::EncoderMap::const_iterator it =
1896 encoder_map.begin(); it != encoder_map.end(); ++it) {
1897 if (engine()->vie()->ext_codec()->DeRegisterExternalSendCodec(
1898 channel_id, it->first) != 0) {
1899 LOG_RTCERR1(DeregisterEncoderObserver, channel_id);
1900 }
1901 engine()->DestroyExternalEncoder(it->second);
1902 }
1903 send_channel->ClearRegisteredEncoders();
1904
1905 // The receive channels depend on the default channel, recycle it instead.
1906 if (IsDefaultChannel(channel_id)) {
1907 SetCapturer(GetDefaultChannelSsrc(), NULL);
1908 send_channel->ClearStreamParams();
1909 } else {
1910 return DeleteSendChannel(ssrc_key);
1911 }
1912 return true;
1913}
1914
1915bool WebRtcVideoMediaChannel::AddRecvStream(const StreamParams& sp) {
1916 // TODO(zhurunz) Remove this once BWE works properly across different send
1917 // and receive channels.
1918 // Reuse default channel for recv stream in 1:1 call.
1919 if (!InConferenceMode() && first_receive_ssrc_ == 0) {
1920 LOG(LS_INFO) << "Recv stream " << sp.first_ssrc()
1921 << " reuse default channel #"
1922 << vie_channel_;
1923 first_receive_ssrc_ = sp.first_ssrc();
1924 if (render_started_) {
1925 if (engine()->vie()->render()->StartRender(vie_channel_) !=0) {
1926 LOG_RTCERR1(StartRender, vie_channel_);
1927 }
1928 }
1929 return true;
1930 }
1931
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001932 int channel_id = -1;
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001933 RecvChannelMap::iterator channel_iterator =
1934 recv_channels_.find(sp.first_ssrc());
1935 if (channel_iterator == recv_channels_.end() &&
1936 first_receive_ssrc_ != sp.first_ssrc()) {
1937 // TODO(perkj): Implement recv media from multiple media SSRCs per stream.
1938 // NOTE: We have two SSRCs per stream when RTX is enabled.
1939 if (!IsOneSsrcStream(sp)) {
1940 LOG(LS_ERROR) << "WebRtcVideoMediaChannel supports one primary SSRC per"
1941 << " stream and one FID SSRC per primary SSRC.";
1942 return false;
1943 }
1944
1945 // Create a new channel for receiving video data.
1946 // In order to get the bandwidth estimation work fine for
1947 // receive only channels, we connect all receiving channels
1948 // to our master send channel.
1949 if (!CreateChannel(sp.first_ssrc(), MD_RECV, &channel_id)) {
1950 return false;
1951 }
1952 } else {
1953 // Already exists.
1954 if (first_receive_ssrc_ == sp.first_ssrc()) {
1955 return false;
1956 }
1957 // Early receive added channel.
1958 channel_id = (*channel_iterator).second->channel_id();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001959 }
1960
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00001961 // Set the corresponding RTX SSRC.
1962 uint32 rtx_ssrc;
1963 bool has_rtx = sp.GetFidSsrc(sp.first_ssrc(), &rtx_ssrc);
1964 if (has_rtx && engine()->vie()->rtp()->SetRemoteSSRCType(
1965 channel_id, webrtc::kViEStreamTypeRtx, rtx_ssrc) != 0) {
1966 LOG_RTCERR3(SetRemoteSSRCType, channel_id, webrtc::kViEStreamTypeRtx,
1967 rtx_ssrc);
1968 return false;
1969 }
1970
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001971 // Get the default renderer.
1972 VideoRenderer* default_renderer = NULL;
1973 if (InConferenceMode()) {
1974 // The recv_channels_ size start out being 1, so if it is two here this
1975 // is the first receive channel created (vie_channel_ is not used for
1976 // receiving in a conference call). This means that the renderer stored
1977 // inside vie_channel_ should be used for the just created channel.
1978 if (recv_channels_.size() == 2 &&
1979 recv_channels_.find(0) != recv_channels_.end()) {
1980 GetRenderer(0, &default_renderer);
1981 }
1982 }
1983
1984 // The first recv stream reuses the default renderer (if a default renderer
1985 // has been set).
1986 if (default_renderer) {
1987 SetRenderer(sp.first_ssrc(), default_renderer);
1988 }
1989
1990 LOG(LS_INFO) << "New video stream " << sp.first_ssrc()
1991 << " registered to VideoEngine channel #"
1992 << channel_id << " and connected to channel #" << vie_channel_;
1993
1994 return true;
1995}
1996
1997bool WebRtcVideoMediaChannel::RemoveRecvStream(uint32 ssrc) {
1998 RecvChannelMap::iterator it = recv_channels_.find(ssrc);
1999
2000 if (it == recv_channels_.end()) {
2001 // TODO(perkj): Remove this once BWE works properly across different send
2002 // and receive channels.
2003 // The default channel is reused for recv stream in 1:1 call.
2004 if (first_receive_ssrc_ == ssrc) {
2005 first_receive_ssrc_ = 0;
2006 // Need to stop the renderer and remove it since the render window can be
2007 // deleted after this.
2008 if (render_started_) {
2009 if (engine()->vie()->render()->StopRender(vie_channel_) !=0) {
2010 LOG_RTCERR1(StopRender, it->second->channel_id());
2011 }
2012 }
2013 recv_channels_[0]->SetRenderer(NULL);
2014 return true;
2015 }
2016 return false;
2017 }
2018 WebRtcVideoChannelRecvInfo* info = it->second;
2019 int channel_id = info->channel_id();
2020 if (engine()->vie()->render()->RemoveRenderer(channel_id) != 0) {
2021 LOG_RTCERR1(RemoveRenderer, channel_id);
2022 }
2023
2024 if (engine()->vie()->network()->DeregisterSendTransport(channel_id) !=0) {
2025 LOG_RTCERR1(DeRegisterSendTransport, channel_id);
2026 }
2027
2028 if (engine()->vie()->codec()->DeregisterDecoderObserver(
2029 channel_id) != 0) {
2030 LOG_RTCERR1(DeregisterDecoderObserver, channel_id);
2031 }
2032
2033 const WebRtcVideoChannelRecvInfo::DecoderMap& decoder_map =
2034 info->registered_decoders();
2035 for (WebRtcVideoChannelRecvInfo::DecoderMap::const_iterator it =
2036 decoder_map.begin(); it != decoder_map.end(); ++it) {
2037 if (engine()->vie()->ext_codec()->DeRegisterExternalReceiveCodec(
2038 channel_id, it->first) != 0) {
2039 LOG_RTCERR1(DeregisterDecoderObserver, channel_id);
2040 }
2041 engine()->DestroyExternalDecoder(it->second);
2042 }
2043 info->ClearRegisteredDecoders();
2044
2045 LOG(LS_INFO) << "Removing video stream " << ssrc
2046 << " with VideoEngine channel #"
2047 << channel_id;
wu@webrtc.org9caf2762013-12-11 18:25:07 +00002048 bool ret = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002049 if (engine()->vie()->base()->DeleteChannel(channel_id) == -1) {
2050 LOG_RTCERR1(DeleteChannel, channel_id);
wu@webrtc.org9caf2762013-12-11 18:25:07 +00002051 ret = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002052 }
2053 // Delete the WebRtcVideoChannelRecvInfo pointed to by it->second.
2054 delete info;
2055 recv_channels_.erase(it);
wu@webrtc.org9caf2762013-12-11 18:25:07 +00002056 return ret;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002057}
2058
2059bool WebRtcVideoMediaChannel::StartSend() {
2060 bool success = true;
2061 for (SendChannelMap::iterator iter = send_channels_.begin();
2062 iter != send_channels_.end(); ++iter) {
2063 WebRtcVideoChannelSendInfo* send_channel = iter->second;
2064 if (!StartSend(send_channel)) {
2065 success = false;
2066 }
2067 }
2068 return success;
2069}
2070
2071bool WebRtcVideoMediaChannel::StartSend(
2072 WebRtcVideoChannelSendInfo* send_channel) {
2073 const int channel_id = send_channel->channel_id();
2074 if (engine()->vie()->base()->StartSend(channel_id) != 0) {
2075 LOG_RTCERR1(StartSend, channel_id);
2076 return false;
2077 }
2078
2079 send_channel->set_sending(true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002080 return true;
2081}
2082
2083bool WebRtcVideoMediaChannel::StopSend() {
2084 bool success = true;
2085 for (SendChannelMap::iterator iter = send_channels_.begin();
2086 iter != send_channels_.end(); ++iter) {
2087 WebRtcVideoChannelSendInfo* send_channel = iter->second;
2088 if (!StopSend(send_channel)) {
2089 success = false;
2090 }
2091 }
2092 return success;
2093}
2094
2095bool WebRtcVideoMediaChannel::StopSend(
2096 WebRtcVideoChannelSendInfo* send_channel) {
2097 const int channel_id = send_channel->channel_id();
2098 if (engine()->vie()->base()->StopSend(channel_id) != 0) {
2099 LOG_RTCERR1(StopSend, channel_id);
2100 return false;
2101 }
2102 send_channel->set_sending(false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002103 return true;
2104}
2105
2106bool WebRtcVideoMediaChannel::SendIntraFrame() {
2107 bool success = true;
2108 for (SendChannelMap::iterator iter = send_channels_.begin();
2109 iter != send_channels_.end();
2110 ++iter) {
2111 WebRtcVideoChannelSendInfo* send_channel = iter->second;
2112 const int channel_id = send_channel->channel_id();
2113 if (engine()->vie()->codec()->SendKeyFrame(channel_id) != 0) {
2114 LOG_RTCERR1(SendKeyFrame, channel_id);
2115 success = false;
2116 }
2117 }
2118 return success;
2119}
2120
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002121bool WebRtcVideoMediaChannel::HasReadySendChannels() {
2122 return !send_channels_.empty() &&
2123 ((send_channels_.size() > 1) ||
2124 (send_channels_[0]->stream_params() != NULL));
2125}
2126
2127bool WebRtcVideoMediaChannel::GetSendChannelKey(uint32 local_ssrc,
2128 uint32* key) {
2129 *key = 0;
2130 // If a send channel is not ready to send it will not have local_ssrc
2131 // registered to it.
2132 if (!HasReadySendChannels()) {
2133 return false;
2134 }
2135 // The default channel is stored with key 0. The key therefore does not match
2136 // the SSRC associated with the default channel. Check if the SSRC provided
2137 // corresponds to the default channel's SSRC.
2138 if (local_ssrc == GetDefaultChannelSsrc()) {
2139 return true;
2140 }
2141 if (send_channels_.find(local_ssrc) == send_channels_.end()) {
2142 for (SendChannelMap::iterator iter = send_channels_.begin();
2143 iter != send_channels_.end(); ++iter) {
2144 WebRtcVideoChannelSendInfo* send_channel = iter->second;
2145 if (send_channel->has_ssrc(local_ssrc)) {
2146 *key = iter->first;
2147 return true;
2148 }
2149 }
2150 return false;
2151 }
2152 // The key was found in the above std::map::find call. This means that the
2153 // ssrc is the key.
2154 *key = local_ssrc;
2155 return true;
2156}
2157
2158WebRtcVideoChannelSendInfo* WebRtcVideoMediaChannel::GetSendChannel(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002159 uint32 local_ssrc) {
2160 uint32 key;
2161 if (!GetSendChannelKey(local_ssrc, &key)) {
2162 return NULL;
2163 }
2164 return send_channels_[key];
2165}
2166
2167bool WebRtcVideoMediaChannel::CreateSendChannelKey(uint32 local_ssrc,
2168 uint32* key) {
2169 if (GetSendChannelKey(local_ssrc, key)) {
2170 // If there is a key corresponding to |local_ssrc|, the SSRC is already in
2171 // use. SSRCs need to be unique in a session and at this point a duplicate
2172 // SSRC has been detected.
2173 return false;
2174 }
2175 if (send_channels_[0]->stream_params() == NULL) {
2176 // key should be 0 here as the default channel should be re-used whenever it
2177 // is not used.
2178 *key = 0;
2179 return true;
2180 }
2181 // SSRC is currently not in use and the default channel is already in use. Use
2182 // the SSRC as key since it is supposed to be unique in a session.
2183 *key = local_ssrc;
2184 return true;
2185}
2186
wu@webrtc.org24301a62013-12-13 19:17:43 +00002187int WebRtcVideoMediaChannel::GetSendChannelNum(VideoCapturer* capturer) {
2188 int num = 0;
2189 for (SendChannelMap::iterator iter = send_channels_.begin();
2190 iter != send_channels_.end(); ++iter) {
2191 WebRtcVideoChannelSendInfo* send_channel = iter->second;
2192 if (send_channel->video_capturer() == capturer) {
2193 ++num;
2194 }
2195 }
2196 return num;
2197}
2198
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002199uint32 WebRtcVideoMediaChannel::GetDefaultChannelSsrc() {
2200 WebRtcVideoChannelSendInfo* send_channel = send_channels_[0];
2201 const StreamParams* sp = send_channel->stream_params();
2202 if (sp == NULL) {
2203 // This happens if no send stream is currently registered.
2204 return 0;
2205 }
2206 return sp->first_ssrc();
2207}
2208
2209bool WebRtcVideoMediaChannel::DeleteSendChannel(uint32 ssrc_key) {
2210 if (send_channels_.find(ssrc_key) == send_channels_.end()) {
2211 return false;
2212 }
2213 WebRtcVideoChannelSendInfo* send_channel = send_channels_[ssrc_key];
wu@webrtc.org24301a62013-12-13 19:17:43 +00002214 MaybeDisconnectCapturer(send_channel->video_capturer());
2215 send_channel->set_video_capturer(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002216
2217 int channel_id = send_channel->channel_id();
2218 int capture_id = send_channel->capture_id();
2219 if (engine()->vie()->codec()->DeregisterEncoderObserver(
2220 channel_id) != 0) {
2221 LOG_RTCERR1(DeregisterEncoderObserver, channel_id);
2222 }
2223
2224 // Destroy the external capture interface.
2225 if (engine()->vie()->capture()->DisconnectCaptureDevice(
2226 channel_id) != 0) {
2227 LOG_RTCERR1(DisconnectCaptureDevice, channel_id);
2228 }
2229 if (engine()->vie()->capture()->ReleaseCaptureDevice(
2230 capture_id) != 0) {
2231 LOG_RTCERR1(ReleaseCaptureDevice, capture_id);
2232 }
2233
2234 // The default channel is stored in both |send_channels_| and
2235 // |recv_channels_|. To make sure it is only deleted once from vie let the
2236 // delete call happen when tearing down |recv_channels_| and not here.
2237 if (!IsDefaultChannel(channel_id)) {
2238 engine_->vie()->base()->DeleteChannel(channel_id);
2239 }
2240 delete send_channel;
2241 send_channels_.erase(ssrc_key);
2242 return true;
2243}
2244
2245bool WebRtcVideoMediaChannel::RemoveCapturer(uint32 ssrc) {
2246 WebRtcVideoChannelSendInfo* send_channel = GetSendChannel(ssrc);
2247 if (!send_channel) {
2248 return false;
2249 }
2250 VideoCapturer* capturer = send_channel->video_capturer();
2251 if (capturer == NULL) {
2252 return false;
2253 }
wu@webrtc.org24301a62013-12-13 19:17:43 +00002254 MaybeDisconnectCapturer(capturer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002255 send_channel->set_video_capturer(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002256 const int64 timestamp = send_channel->local_stream_info()->time_stamp();
2257 if (send_codec_) {
2258 QueueBlackFrame(ssrc, timestamp, send_codec_->maxFramerate);
2259 }
2260 return true;
2261}
2262
2263bool WebRtcVideoMediaChannel::SetRenderer(uint32 ssrc,
2264 VideoRenderer* renderer) {
2265 if (recv_channels_.find(ssrc) == recv_channels_.end()) {
2266 // TODO(perkj): Remove this once BWE works properly across different send
2267 // and receive channels.
2268 // The default channel is reused for recv stream in 1:1 call.
2269 if (first_receive_ssrc_ == ssrc &&
2270 recv_channels_.find(0) != recv_channels_.end()) {
2271 LOG(LS_INFO) << "SetRenderer " << ssrc
2272 << " reuse default channel #"
2273 << vie_channel_;
2274 recv_channels_[0]->SetRenderer(renderer);
2275 return true;
2276 }
2277 return false;
2278 }
2279
2280 recv_channels_[ssrc]->SetRenderer(renderer);
2281 return true;
2282}
2283
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00002284bool WebRtcVideoMediaChannel::GetStats(const StatsOptions& options,
2285 VideoMediaInfo* info) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002286 // Get sender statistics and build VideoSenderInfo.
2287 unsigned int total_bitrate_sent = 0;
2288 unsigned int video_bitrate_sent = 0;
2289 unsigned int fec_bitrate_sent = 0;
2290 unsigned int nack_bitrate_sent = 0;
2291 unsigned int estimated_send_bandwidth = 0;
2292 unsigned int target_enc_bitrate = 0;
2293 if (send_codec_) {
2294 for (SendChannelMap::const_iterator iter = send_channels_.begin();
2295 iter != send_channels_.end(); ++iter) {
2296 WebRtcVideoChannelSendInfo* send_channel = iter->second;
2297 const int channel_id = send_channel->channel_id();
2298 VideoSenderInfo sinfo;
2299 const StreamParams* send_params = send_channel->stream_params();
2300 if (send_params == NULL) {
2301 // This should only happen if the default vie channel is not in use.
2302 // This can happen if no streams have ever been added or the stream
2303 // corresponding to the default channel has been removed. Note that
2304 // there may be non-default vie channels in use when this happen so
2305 // asserting send_channels_.size() == 1 is not correct and neither is
2306 // breaking out of the loop.
2307 ASSERT(channel_id == vie_channel_);
2308 continue;
2309 }
2310 unsigned int bytes_sent, packets_sent, bytes_recv, packets_recv;
2311 if (engine_->vie()->rtp()->GetRTPStatistics(channel_id, bytes_sent,
2312 packets_sent, bytes_recv,
2313 packets_recv) != 0) {
2314 LOG_RTCERR1(GetRTPStatistics, vie_channel_);
2315 continue;
2316 }
2317 WebRtcLocalStreamInfo* channel_stream_info =
2318 send_channel->local_stream_info();
2319
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002320 for (size_t i = 0; i < send_params->ssrcs.size(); ++i) {
2321 sinfo.add_ssrc(send_params->ssrcs[i]);
2322 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002323 sinfo.codec_name = send_codec_->plName;
2324 sinfo.bytes_sent = bytes_sent;
2325 sinfo.packets_sent = packets_sent;
2326 sinfo.packets_cached = -1;
2327 sinfo.packets_lost = -1;
2328 sinfo.fraction_lost = -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002329 sinfo.rtt_ms = -1;
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00002330 sinfo.input_frame_width = static_cast<int>(channel_stream_info->width());
2331 sinfo.input_frame_height =
2332 static_cast<int>(channel_stream_info->height());
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00002333
2334 VideoCapturer* video_capturer = send_channel->video_capturer();
2335 if (video_capturer) {
2336 video_capturer->GetStats(&sinfo.adapt_frame_drops,
2337 &sinfo.effects_frame_drops,
2338 &sinfo.capturer_frame_time);
2339 }
2340
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00002341 webrtc::VideoCodec vie_codec;
2342 if (engine()->vie()->codec()->GetSendCodec(channel_id, vie_codec) == 0) {
2343 sinfo.send_frame_width = vie_codec.width;
2344 sinfo.send_frame_height = vie_codec.height;
2345 } else {
2346 sinfo.send_frame_width = -1;
2347 sinfo.send_frame_height = -1;
2348 LOG_RTCERR1(GetSendCodec, channel_id);
2349 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002350 sinfo.framerate_input = channel_stream_info->framerate();
2351 sinfo.framerate_sent = send_channel->encoder_observer()->framerate();
2352 sinfo.nominal_bitrate = send_channel->encoder_observer()->bitrate();
2353 sinfo.preferred_bitrate = send_max_bitrate_;
2354 sinfo.adapt_reason = send_channel->CurrentAdaptReason();
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002355 sinfo.capture_jitter_ms = -1;
2356 sinfo.avg_encode_ms = -1;
wu@webrtc.org9caf2762013-12-11 18:25:07 +00002357 sinfo.encode_usage_percent = -1;
2358 sinfo.capture_queue_delay_ms_per_s = -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002359
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002360 int capture_jitter_ms = 0;
2361 int avg_encode_time_ms = 0;
wu@webrtc.org9caf2762013-12-11 18:25:07 +00002362 int encode_usage_percent = 0;
2363 int capture_queue_delay_ms_per_s = 0;
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002364 if (engine()->vie()->base()->CpuOveruseMeasures(
wu@webrtc.org9caf2762013-12-11 18:25:07 +00002365 channel_id,
2366 &capture_jitter_ms,
2367 &avg_encode_time_ms,
2368 &encode_usage_percent,
2369 &capture_queue_delay_ms_per_s) == 0) {
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002370 sinfo.capture_jitter_ms = capture_jitter_ms;
2371 sinfo.avg_encode_ms = avg_encode_time_ms;
wu@webrtc.org9caf2762013-12-11 18:25:07 +00002372 sinfo.encode_usage_percent = encode_usage_percent;
2373 sinfo.capture_queue_delay_ms_per_s = capture_queue_delay_ms_per_s;
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002374 }
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002375
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00002376#ifdef USE_WEBRTC_DEV_BRANCH
2377 webrtc::RtcpPacketTypeCounter rtcp_sent;
2378 webrtc::RtcpPacketTypeCounter rtcp_received;
2379 if (engine()->vie()->rtp()->GetRtcpPacketTypeCounters(
2380 channel_id, &rtcp_sent, &rtcp_received) == 0) {
2381 sinfo.firs_rcvd = rtcp_received.fir_packets;
2382 sinfo.plis_rcvd = rtcp_received.pli_packets;
2383 sinfo.nacks_rcvd = rtcp_received.nack_packets;
2384 } else {
2385 sinfo.firs_rcvd = -1;
2386 sinfo.plis_rcvd = -1;
2387 sinfo.nacks_rcvd = -1;
2388 LOG_RTCERR1(GetRtcpPacketTypeCounters, channel_id);
2389 }
2390#else
2391 sinfo.firs_rcvd = -1;
2392 sinfo.plis_rcvd = -1;
2393 sinfo.nacks_rcvd = -1;
2394#endif
2395
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002396 // Get received RTCP statistics for the sender (reported by the remote
2397 // client in a RTCP packet), if available.
2398 // It's not a fatal error if we can't, since RTCP may not have arrived
2399 // yet.
2400 webrtc::RtcpStatistics outgoing_stream_rtcp_stats;
2401 int outgoing_stream_rtt_ms;
2402
2403 if (engine_->vie()->rtp()->GetSendChannelRtcpStatistics(
2404 channel_id,
2405 outgoing_stream_rtcp_stats,
2406 outgoing_stream_rtt_ms) == 0) {
2407 // Convert Q8 to float.
2408 sinfo.packets_lost = outgoing_stream_rtcp_stats.cumulative_lost;
2409 sinfo.fraction_lost = static_cast<float>(
2410 outgoing_stream_rtcp_stats.fraction_lost) / (1 << 8);
2411 sinfo.rtt_ms = outgoing_stream_rtt_ms;
2412 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002413 info->senders.push_back(sinfo);
2414
2415 unsigned int channel_total_bitrate_sent = 0;
2416 unsigned int channel_video_bitrate_sent = 0;
2417 unsigned int channel_fec_bitrate_sent = 0;
2418 unsigned int channel_nack_bitrate_sent = 0;
2419 if (engine_->vie()->rtp()->GetBandwidthUsage(
2420 channel_id, channel_total_bitrate_sent, channel_video_bitrate_sent,
2421 channel_fec_bitrate_sent, channel_nack_bitrate_sent) == 0) {
2422 total_bitrate_sent += channel_total_bitrate_sent;
2423 video_bitrate_sent += channel_video_bitrate_sent;
2424 fec_bitrate_sent += channel_fec_bitrate_sent;
2425 nack_bitrate_sent += channel_nack_bitrate_sent;
2426 } else {
2427 LOG_RTCERR1(GetBandwidthUsage, channel_id);
2428 }
2429
2430 unsigned int estimated_stream_send_bandwidth = 0;
2431 if (engine_->vie()->rtp()->GetEstimatedSendBandwidth(
2432 channel_id, &estimated_stream_send_bandwidth) == 0) {
2433 estimated_send_bandwidth += estimated_stream_send_bandwidth;
2434 } else {
2435 LOG_RTCERR1(GetEstimatedSendBandwidth, channel_id);
2436 }
2437 unsigned int target_enc_stream_bitrate = 0;
2438 if (engine_->vie()->codec()->GetCodecTargetBitrate(
2439 channel_id, &target_enc_stream_bitrate) == 0) {
2440 target_enc_bitrate += target_enc_stream_bitrate;
2441 } else {
2442 LOG_RTCERR1(GetCodecTargetBitrate, channel_id);
2443 }
2444 }
2445 } else {
2446 LOG(LS_WARNING) << "GetStats: sender information not ready.";
2447 }
2448
2449 // Get the SSRC and stats for each receiver, based on our own calculations.
2450 unsigned int estimated_recv_bandwidth = 0;
2451 for (RecvChannelMap::const_iterator it = recv_channels_.begin();
2452 it != recv_channels_.end(); ++it) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002453 WebRtcVideoChannelRecvInfo* channel = it->second;
2454
2455 unsigned int ssrc;
2456 // Get receiver statistics and build VideoReceiverInfo, if we have data.
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +00002457 // Skip the default channel (ssrc == 0).
2458 if (engine_->vie()->rtp()->GetRemoteSSRC(
2459 channel->channel_id(), ssrc) != 0 ||
2460 ssrc == 0)
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002461 continue;
2462
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002463 webrtc::StreamDataCounters sent;
2464 webrtc::StreamDataCounters received;
2465 if (engine_->vie()->rtp()->GetRtpStatistics(channel->channel_id(),
2466 sent, received) != 0) {
2467 LOG_RTCERR1(GetRTPStatistics, channel->channel_id());
2468 return false;
2469 }
2470 VideoReceiverInfo rinfo;
2471 rinfo.add_ssrc(ssrc);
2472 rinfo.bytes_rcvd = received.bytes;
2473 rinfo.packets_rcvd = received.packets;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002474 rinfo.packets_lost = -1;
2475 rinfo.packets_concealed = -1;
2476 rinfo.fraction_lost = -1; // from SentRTCP
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002477 rinfo.frame_width = channel->render_adapter()->width();
2478 rinfo.frame_height = channel->render_adapter()->height();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002479 int fps = channel->render_adapter()->framerate();
2480 rinfo.framerate_decoded = fps;
2481 rinfo.framerate_output = fps;
wu@webrtc.org97077a32013-10-25 21:18:33 +00002482 channel->decoder_observer()->ExportTo(&rinfo);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002483
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00002484#ifdef USE_WEBRTC_DEV_BRANCH
2485 webrtc::RtcpPacketTypeCounter rtcp_sent;
2486 webrtc::RtcpPacketTypeCounter rtcp_received;
2487 if (engine()->vie()->rtp()->GetRtcpPacketTypeCounters(
2488 channel->channel_id(), &rtcp_sent, &rtcp_received) == 0) {
2489 rinfo.firs_sent = rtcp_sent.fir_packets;
2490 rinfo.plis_sent = rtcp_sent.pli_packets;
2491 rinfo.nacks_sent = rtcp_sent.nack_packets;
2492 } else {
2493 rinfo.firs_sent = -1;
2494 rinfo.plis_sent = -1;
2495 rinfo.nacks_sent = -1;
2496 LOG_RTCERR1(GetRtcpPacketTypeCounters, channel->channel_id());
2497 }
2498#else
2499 rinfo.firs_sent = -1;
2500 rinfo.plis_sent = -1;
2501 rinfo.nacks_sent = -1;
2502#endif
2503
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002504 // Get our locally created statistics of the received RTP stream.
2505 webrtc::RtcpStatistics incoming_stream_rtcp_stats;
2506 int incoming_stream_rtt_ms;
2507 if (engine_->vie()->rtp()->GetReceiveChannelRtcpStatistics(
2508 channel->channel_id(),
2509 incoming_stream_rtcp_stats,
2510 incoming_stream_rtt_ms) == 0) {
2511 // Convert Q8 to float.
2512 rinfo.packets_lost = incoming_stream_rtcp_stats.cumulative_lost;
2513 rinfo.fraction_lost = static_cast<float>(
2514 incoming_stream_rtcp_stats.fraction_lost) / (1 << 8);
2515 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002516 info->receivers.push_back(rinfo);
2517
2518 unsigned int estimated_recv_stream_bandwidth = 0;
2519 if (engine_->vie()->rtp()->GetEstimatedReceiveBandwidth(
2520 channel->channel_id(), &estimated_recv_stream_bandwidth) == 0) {
2521 estimated_recv_bandwidth += estimated_recv_stream_bandwidth;
2522 } else {
2523 LOG_RTCERR1(GetEstimatedReceiveBandwidth, channel->channel_id());
2524 }
2525 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002526 // Build BandwidthEstimationInfo.
2527 // TODO(zhurunz): Add real unittest for this.
2528 BandwidthEstimationInfo bwe;
2529
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00002530 // TODO(jiayl): remove the condition when the necessary changes are available
2531 // outside the dev branch.
2532#ifdef USE_WEBRTC_DEV_BRANCH
2533 if (options.include_received_propagation_stats) {
2534 webrtc::ReceiveBandwidthEstimatorStats additional_stats;
2535 // Only call for the default channel because the returned stats are
2536 // collected for all the channels using the same estimator.
2537 if (engine_->vie()->rtp()->GetReceiveBandwidthEstimatorStats(
mallinath@webrtc.org92fdfeb2014-02-17 18:49:41 +00002538 recv_channels_[0]->channel_id(), &additional_stats) == 0) {
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00002539 bwe.total_received_propagation_delta_ms =
2540 additional_stats.total_propagation_time_delta_ms;
2541 bwe.recent_received_propagation_delta_ms.swap(
2542 additional_stats.recent_propagation_time_delta_ms);
2543 bwe.recent_received_packet_group_arrival_time_ms.swap(
2544 additional_stats.recent_arrival_time_ms);
2545 }
2546 }
henrike@webrtc.orgb8395eb2014-02-28 21:57:22 +00002547
2548 engine_->vie()->rtp()->GetPacerQueuingDelayMs(
2549 recv_channels_[0]->channel_id(), &bwe.bucket_delay);
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00002550#endif
2551
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002552 // Calculations done above per send/receive stream.
2553 bwe.actual_enc_bitrate = video_bitrate_sent;
2554 bwe.transmit_bitrate = total_bitrate_sent;
2555 bwe.retransmit_bitrate = nack_bitrate_sent;
2556 bwe.available_send_bandwidth = estimated_send_bandwidth;
2557 bwe.available_recv_bandwidth = estimated_recv_bandwidth;
2558 bwe.target_enc_bitrate = target_enc_bitrate;
2559
2560 info->bw_estimations.push_back(bwe);
2561
2562 return true;
2563}
2564
2565bool WebRtcVideoMediaChannel::SetCapturer(uint32 ssrc,
2566 VideoCapturer* capturer) {
2567 ASSERT(ssrc != 0);
2568 if (!capturer) {
2569 return RemoveCapturer(ssrc);
2570 }
2571 WebRtcVideoChannelSendInfo* send_channel = GetSendChannel(ssrc);
2572 if (!send_channel) {
2573 return false;
2574 }
2575 VideoCapturer* old_capturer = send_channel->video_capturer();
wu@webrtc.org24301a62013-12-13 19:17:43 +00002576 MaybeDisconnectCapturer(old_capturer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002577
2578 send_channel->set_video_capturer(capturer);
wu@webrtc.orga8910d22014-01-23 22:12:45 +00002579 MaybeConnectCapturer(capturer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002580 if (!capturer->IsScreencast() && ratio_w_ != 0 && ratio_h_ != 0) {
2581 capturer->UpdateAspectRatio(ratio_w_, ratio_h_);
2582 }
2583 const int64 timestamp = send_channel->local_stream_info()->time_stamp();
2584 if (send_codec_) {
2585 QueueBlackFrame(ssrc, timestamp, send_codec_->maxFramerate);
2586 }
2587 return true;
2588}
2589
2590bool WebRtcVideoMediaChannel::RequestIntraFrame() {
2591 // There is no API exposed to application to request a key frame
2592 // ViE does this internally when there are errors from decoder
2593 return false;
2594}
2595
wu@webrtc.orga9890802013-12-13 00:21:03 +00002596void WebRtcVideoMediaChannel::OnPacketReceived(
2597 talk_base::Buffer* packet, const talk_base::PacketTime& packet_time) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002598 // Pick which channel to send this packet to. If this packet doesn't match
2599 // any multiplexed streams, just send it to the default channel. Otherwise,
2600 // send it to the specific decoder instance for that stream.
2601 uint32 ssrc = 0;
2602 if (!GetRtpSsrc(packet->data(), packet->length(), &ssrc))
2603 return;
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00002604 int processing_channel = GetRecvChannelNum(ssrc);
2605 if (processing_channel == -1) {
2606 // Allocate an unsignalled recv channel for processing in conference mode.
2607 if (!InConferenceMode() ||
2608 !CreateUnsignalledRecvChannel(ssrc, &processing_channel)) {
2609 // If we cant find or allocate one, use the default.
2610 processing_channel = video_channel();
2611 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002612 }
2613
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00002614 engine()->vie()->network()->ReceivedRTPPacket(
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00002615 processing_channel,
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00002616 packet->data(),
wu@webrtc.orga9890802013-12-13 00:21:03 +00002617 static_cast<int>(packet->length()),
2618 webrtc::PacketTime(packet_time.timestamp, packet_time.not_before));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002619}
2620
wu@webrtc.orga9890802013-12-13 00:21:03 +00002621void WebRtcVideoMediaChannel::OnRtcpReceived(
2622 talk_base::Buffer* packet, const talk_base::PacketTime& packet_time) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002623// Sending channels need all RTCP packets with feedback information.
2624// Even sender reports can contain attached report blocks.
2625// Receiving channels need sender reports in order to create
2626// correct receiver reports.
2627
2628 uint32 ssrc = 0;
2629 if (!GetRtcpSsrc(packet->data(), packet->length(), &ssrc)) {
2630 LOG(LS_WARNING) << "Failed to parse SSRC from received RTCP packet";
2631 return;
2632 }
2633 int type = 0;
2634 if (!GetRtcpType(packet->data(), packet->length(), &type)) {
2635 LOG(LS_WARNING) << "Failed to parse type from received RTCP packet";
2636 return;
2637 }
2638
2639 // If it is a sender report, find the channel that is listening.
2640 if (type == kRtcpTypeSR) {
2641 int which_channel = GetRecvChannelNum(ssrc);
2642 if (which_channel != -1 && !IsDefaultChannel(which_channel)) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00002643 engine_->vie()->network()->ReceivedRTCPPacket(
2644 which_channel,
2645 packet->data(),
2646 static_cast<int>(packet->length()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002647 }
2648 }
2649 // SR may continue RR and any RR entry may correspond to any one of the send
2650 // channels. So all RTCP packets must be forwarded all send channels. ViE
2651 // will filter out RR internally.
2652 for (SendChannelMap::iterator iter = send_channels_.begin();
2653 iter != send_channels_.end(); ++iter) {
2654 WebRtcVideoChannelSendInfo* send_channel = iter->second;
2655 int channel_id = send_channel->channel_id();
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00002656 engine_->vie()->network()->ReceivedRTCPPacket(
2657 channel_id,
2658 packet->data(),
2659 static_cast<int>(packet->length()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002660 }
2661}
2662
2663void WebRtcVideoMediaChannel::OnReadyToSend(bool ready) {
2664 SetNetworkTransmissionState(ready);
2665}
2666
2667bool WebRtcVideoMediaChannel::MuteStream(uint32 ssrc, bool muted) {
2668 WebRtcVideoChannelSendInfo* send_channel = GetSendChannel(ssrc);
2669 if (!send_channel) {
2670 LOG(LS_ERROR) << "The specified ssrc " << ssrc << " is not in use.";
2671 return false;
2672 }
2673 send_channel->set_muted(muted);
2674 return true;
2675}
2676
2677bool WebRtcVideoMediaChannel::SetRecvRtpHeaderExtensions(
2678 const std::vector<RtpHeaderExtension>& extensions) {
2679 if (receive_extensions_ == extensions) {
2680 return true;
2681 }
2682 receive_extensions_ = extensions;
2683
2684 const RtpHeaderExtension* offset_extension =
2685 FindHeaderExtension(extensions, kRtpTimestampOffsetHeaderExtension);
2686 const RtpHeaderExtension* send_time_extension =
2687 FindHeaderExtension(extensions, kRtpAbsoluteSendTimeHeaderExtension);
2688
2689 // Loop through all receive channels and enable/disable the extensions.
2690 for (RecvChannelMap::iterator channel_it = recv_channels_.begin();
2691 channel_it != recv_channels_.end(); ++channel_it) {
2692 int channel_id = channel_it->second->channel_id();
2693 if (!SetHeaderExtension(
2694 &webrtc::ViERTP_RTCP::SetReceiveTimestampOffsetStatus, channel_id,
2695 offset_extension)) {
2696 return false;
2697 }
2698 if (!SetHeaderExtension(
2699 &webrtc::ViERTP_RTCP::SetReceiveAbsoluteSendTimeStatus, channel_id,
2700 send_time_extension)) {
2701 return false;
2702 }
2703 }
2704 return true;
2705}
2706
2707bool WebRtcVideoMediaChannel::SetSendRtpHeaderExtensions(
2708 const std::vector<RtpHeaderExtension>& extensions) {
2709 send_extensions_ = extensions;
2710
2711 const RtpHeaderExtension* offset_extension =
2712 FindHeaderExtension(extensions, kRtpTimestampOffsetHeaderExtension);
2713 const RtpHeaderExtension* send_time_extension =
2714 FindHeaderExtension(extensions, kRtpAbsoluteSendTimeHeaderExtension);
2715
2716 // Loop through all send channels and enable/disable the extensions.
2717 for (SendChannelMap::iterator channel_it = send_channels_.begin();
2718 channel_it != send_channels_.end(); ++channel_it) {
2719 int channel_id = channel_it->second->channel_id();
2720 if (!SetHeaderExtension(
2721 &webrtc::ViERTP_RTCP::SetSendTimestampOffsetStatus, channel_id,
2722 offset_extension)) {
2723 return false;
2724 }
2725 if (!SetHeaderExtension(
2726 &webrtc::ViERTP_RTCP::SetSendAbsoluteSendTimeStatus, channel_id,
2727 send_time_extension)) {
2728 return false;
2729 }
2730 }
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00002731
2732 if (send_time_extension) {
2733 // For video RTP packets, we would like to update AbsoluteSendTimeHeader
2734 // Extension closer to the network, @ socket level before sending.
2735 // Pushing the extension id to socket layer.
2736 MediaChannel::SetOption(NetworkInterface::ST_RTP,
2737 talk_base::Socket::OPT_RTP_SENDTIME_EXTN_ID,
2738 send_time_extension->id);
2739 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002740 return true;
2741}
2742
mallinath@webrtc.org92fdfeb2014-02-17 18:49:41 +00002743int WebRtcVideoMediaChannel::GetRtpSendTimeExtnId() const {
2744 const RtpHeaderExtension* send_time_extension = FindHeaderExtension(
2745 send_extensions_, kRtpAbsoluteSendTimeHeaderExtension);
2746 if (send_time_extension) {
2747 return send_time_extension->id;
2748 }
2749 return -1;
2750}
2751
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002752bool WebRtcVideoMediaChannel::SetStartSendBandwidth(int bps) {
2753 LOG(LS_INFO) << "WebRtcVideoMediaChannel::SetStartSendBandwidth";
2754
2755 if (!send_codec_) {
2756 LOG(LS_INFO) << "The send codec has not been set up yet";
2757 return true;
2758 }
2759
2760 // On success, SetSendCodec() will reset |send_start_bitrate_| to |bps/1000|,
2761 // by calling MaybeChangeStartBitrate. That method will also clamp the
2762 // start bitrate between min and max, consistent with the override behavior
2763 // in SetMaxSendBandwidth.
2764 return SetSendCodec(*send_codec_,
2765 send_min_bitrate_, bps / 1000, send_max_bitrate_);
2766}
2767
2768bool WebRtcVideoMediaChannel::SetMaxSendBandwidth(int bps) {
2769 LOG(LS_INFO) << "WebRtcVideoMediaChannel::SetMaxSendBandwidth";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002770
2771 if (InConferenceMode()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002772 LOG(LS_INFO) << "Conference mode ignores SetMaxSendBandwidth";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002773 return true;
2774 }
2775
2776 if (!send_codec_) {
2777 LOG(LS_INFO) << "The send codec has not been set up yet";
2778 return true;
2779 }
2780
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002781 // Use the default value or the bps for the max
2782 int max_bitrate = (bps <= 0) ? send_max_bitrate_ : (bps / 1000);
2783
2784 // Reduce the current minimum and start bitrates if necessary.
2785 int min_bitrate = talk_base::_min(send_min_bitrate_, max_bitrate);
2786 int start_bitrate = talk_base::_min(send_start_bitrate_, max_bitrate);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002787
2788 if (!SetSendCodec(*send_codec_, min_bitrate, start_bitrate, max_bitrate)) {
2789 return false;
2790 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002791 LogSendCodecChange("SetMaxSendBandwidth()");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002792
2793 return true;
2794}
2795
2796bool WebRtcVideoMediaChannel::SetOptions(const VideoOptions &options) {
2797 // Always accept options that are unchanged.
2798 if (options_ == options) {
2799 return true;
2800 }
2801
2802 // Trigger SetSendCodec to set correct noise reduction state if the option has
2803 // changed.
2804 bool denoiser_changed = options.video_noise_reduction.IsSet() &&
2805 (options_.video_noise_reduction != options.video_noise_reduction);
2806
2807 bool leaky_bucket_changed = options.video_leaky_bucket.IsSet() &&
2808 (options_.video_leaky_bucket != options.video_leaky_bucket);
2809
2810 bool buffer_latency_changed = options.buffered_mode_latency.IsSet() &&
2811 (options_.buffered_mode_latency != options.buffered_mode_latency);
2812
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00002813 bool cpu_overuse_detection_changed = options.cpu_overuse_detection.IsSet() &&
2814 (options_.cpu_overuse_detection != options.cpu_overuse_detection);
2815
wu@webrtc.orgde305012013-10-31 15:40:38 +00002816 bool dscp_option_changed = (options_.dscp != options.dscp);
2817
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002818 bool suspend_below_min_bitrate_changed =
2819 options.suspend_below_min_bitrate.IsSet() &&
2820 (options_.suspend_below_min_bitrate != options.suspend_below_min_bitrate);
2821
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002822 bool conference_mode_turned_off = false;
2823 if (options_.conference_mode.IsSet() && options.conference_mode.IsSet() &&
2824 options_.conference_mode.GetWithDefaultIfUnset(false) &&
2825 !options.conference_mode.GetWithDefaultIfUnset(false)) {
2826 conference_mode_turned_off = true;
2827 }
2828
2829 // Save the options, to be interpreted where appropriate.
2830 // Use options_.SetAll() instead of assignment so that unset value in options
2831 // will not overwrite the previous option value.
2832 options_.SetAll(options);
2833
2834 // Set CPU options for all send channels.
2835 for (SendChannelMap::iterator iter = send_channels_.begin();
2836 iter != send_channels_.end(); ++iter) {
2837 WebRtcVideoChannelSendInfo* send_channel = iter->second;
2838 send_channel->ApplyCpuOptions(options_);
2839 }
2840
2841 // Adjust send codec bitrate if needed.
2842 int conf_max_bitrate = kDefaultConferenceModeMaxVideoBitrate;
2843
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00002844 // Save altered min_bitrate level and apply if necessary.
2845 bool adjusted_min_bitrate = false;
2846 if (options.lower_min_bitrate.IsSet()) {
2847 bool lower;
2848 options.lower_min_bitrate.Get(&lower);
2849
2850 int new_send_min_bitrate = lower ? kLowerMinBitrate : kMinVideoBitrate;
2851 adjusted_min_bitrate = (new_send_min_bitrate != send_min_bitrate_);
2852 send_min_bitrate_ = new_send_min_bitrate;
2853 }
2854
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002855 int expected_bitrate = send_max_bitrate_;
2856 if (InConferenceMode()) {
2857 expected_bitrate = conf_max_bitrate;
2858 } else if (conference_mode_turned_off) {
2859 // This is a special case for turning conference mode off.
2860 // Max bitrate should go back to the default maximum value instead
2861 // of the current maximum.
2862 expected_bitrate = kMaxVideoBitrate;
2863 }
2864
2865 if (send_codec_ &&
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00002866 (send_max_bitrate_ != expected_bitrate || denoiser_changed ||
2867 adjusted_min_bitrate)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002868 // On success, SetSendCodec() will reset send_max_bitrate_ to
2869 // expected_bitrate.
2870 if (!SetSendCodec(*send_codec_,
2871 send_min_bitrate_,
2872 send_start_bitrate_,
2873 expected_bitrate)) {
2874 return false;
2875 }
2876 LogSendCodecChange("SetOptions()");
2877 }
2878 if (leaky_bucket_changed) {
2879 bool enable_leaky_bucket =
2880 options_.video_leaky_bucket.GetWithDefaultIfUnset(false);
2881 for (SendChannelMap::iterator it = send_channels_.begin();
2882 it != send_channels_.end(); ++it) {
2883 if (engine()->vie()->rtp()->SetTransmissionSmoothingStatus(
2884 it->second->channel_id(), enable_leaky_bucket) != 0) {
2885 LOG_RTCERR2(SetTransmissionSmoothingStatus, it->second->channel_id(),
2886 enable_leaky_bucket);
2887 }
2888 }
2889 }
2890 if (buffer_latency_changed) {
2891 int buffer_latency =
2892 options_.buffered_mode_latency.GetWithDefaultIfUnset(
2893 cricket::kBufferedModeDisabled);
2894 for (SendChannelMap::iterator it = send_channels_.begin();
2895 it != send_channels_.end(); ++it) {
2896 if (engine()->vie()->rtp()->SetSenderBufferingMode(
2897 it->second->channel_id(), buffer_latency) != 0) {
2898 LOG_RTCERR2(SetSenderBufferingMode, it->second->channel_id(),
2899 buffer_latency);
2900 }
2901 }
2902 for (RecvChannelMap::iterator it = recv_channels_.begin();
2903 it != recv_channels_.end(); ++it) {
2904 if (engine()->vie()->rtp()->SetReceiverBufferingMode(
2905 it->second->channel_id(), buffer_latency) != 0) {
2906 LOG_RTCERR2(SetReceiverBufferingMode, it->second->channel_id(),
2907 buffer_latency);
2908 }
2909 }
2910 }
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00002911 if (cpu_overuse_detection_changed) {
2912 bool cpu_overuse_detection =
2913 options_.cpu_overuse_detection.GetWithDefaultIfUnset(false);
2914 for (SendChannelMap::iterator iter = send_channels_.begin();
2915 iter != send_channels_.end(); ++iter) {
2916 WebRtcVideoChannelSendInfo* send_channel = iter->second;
2917 send_channel->SetCpuOveruseDetection(cpu_overuse_detection);
2918 }
2919 }
wu@webrtc.orgde305012013-10-31 15:40:38 +00002920 if (dscp_option_changed) {
2921 talk_base::DiffServCodePoint dscp = talk_base::DSCP_DEFAULT;
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00002922 if (options_.dscp.GetWithDefaultIfUnset(false))
wu@webrtc.orgde305012013-10-31 15:40:38 +00002923 dscp = kVideoDscpValue;
2924 if (MediaChannel::SetDscp(dscp) != 0) {
2925 LOG(LS_WARNING) << "Failed to set DSCP settings for video channel";
2926 }
2927 }
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002928 if (suspend_below_min_bitrate_changed) {
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002929 if (options_.suspend_below_min_bitrate.GetWithDefaultIfUnset(false)) {
2930 for (SendChannelMap::iterator it = send_channels_.begin();
2931 it != send_channels_.end(); ++it) {
2932 engine()->vie()->codec()->SuspendBelowMinBitrate(
2933 it->second->channel_id());
2934 }
2935 } else {
2936 LOG(LS_WARNING) << "Cannot disable video suspension once it is enabled";
2937 }
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002938 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002939 return true;
2940}
2941
2942void WebRtcVideoMediaChannel::SetInterface(NetworkInterface* iface) {
2943 MediaChannel::SetInterface(iface);
2944 // Set the RTP recv/send buffer to a bigger size
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002945 MediaChannel::SetOption(NetworkInterface::ST_RTP,
2946 talk_base::Socket::OPT_RCVBUF,
2947 kVideoRtpBufferSize);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002948
2949 // TODO(sriniv): Remove or re-enable this.
2950 // As part of b/8030474, send-buffer is size now controlled through
2951 // portallocator flags.
2952 // network_interface_->SetOption(NetworkInterface::ST_RTP,
2953 // talk_base::Socket::OPT_SNDBUF,
2954 // kVideoRtpBufferSize);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002955}
2956
2957void WebRtcVideoMediaChannel::UpdateAspectRatio(int ratio_w, int ratio_h) {
2958 ASSERT(ratio_w != 0);
2959 ASSERT(ratio_h != 0);
2960 ratio_w_ = ratio_w;
2961 ratio_h_ = ratio_h;
2962 // For now assume that all streams want the same aspect ratio.
2963 // TODO(hellner): remove the need for this assumption.
2964 for (SendChannelMap::iterator iter = send_channels_.begin();
2965 iter != send_channels_.end(); ++iter) {
2966 WebRtcVideoChannelSendInfo* send_channel = iter->second;
2967 VideoCapturer* capturer = send_channel->video_capturer();
2968 if (capturer) {
2969 capturer->UpdateAspectRatio(ratio_w, ratio_h);
2970 }
2971 }
2972}
2973
2974bool WebRtcVideoMediaChannel::GetRenderer(uint32 ssrc,
2975 VideoRenderer** renderer) {
2976 RecvChannelMap::const_iterator it = recv_channels_.find(ssrc);
2977 if (it == recv_channels_.end()) {
2978 if (first_receive_ssrc_ == ssrc &&
2979 recv_channels_.find(0) != recv_channels_.end()) {
2980 LOG(LS_INFO) << " GetRenderer " << ssrc
2981 << " reuse default renderer #"
2982 << vie_channel_;
2983 *renderer = recv_channels_[0]->render_adapter()->renderer();
2984 return true;
2985 }
2986 return false;
2987 }
2988
2989 *renderer = it->second->render_adapter()->renderer();
2990 return true;
2991}
2992
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002993bool WebRtcVideoMediaChannel::GetVideoAdapter(
2994 uint32 ssrc, CoordinatedVideoAdapter** video_adapter) {
2995 SendChannelMap::iterator it = send_channels_.find(ssrc);
2996 if (it == send_channels_.end()) {
2997 return false;
2998 }
2999 *video_adapter = it->second->video_adapter();
3000 return true;
3001}
3002
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003003void WebRtcVideoMediaChannel::SendFrame(VideoCapturer* capturer,
3004 const VideoFrame* frame) {
wu@webrtc.org24301a62013-12-13 19:17:43 +00003005 // If the |capturer| is registered to any send channel, then send the frame
3006 // to those send channels.
3007 bool capturer_is_channel_owned = false;
3008 for (SendChannelMap::iterator iter = send_channels_.begin();
3009 iter != send_channels_.end(); ++iter) {
3010 WebRtcVideoChannelSendInfo* send_channel = iter->second;
3011 if (send_channel->video_capturer() == capturer) {
3012 SendFrame(send_channel, frame, capturer->IsScreencast());
3013 capturer_is_channel_owned = true;
3014 }
3015 }
3016 if (capturer_is_channel_owned) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003017 return;
3018 }
wu@webrtc.org24301a62013-12-13 19:17:43 +00003019
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003020 // TODO(hellner): Remove below for loop once the captured frame no longer
3021 // come from the engine, i.e. the engine no longer owns a capturer.
3022 for (SendChannelMap::iterator iter = send_channels_.begin();
3023 iter != send_channels_.end(); ++iter) {
3024 WebRtcVideoChannelSendInfo* send_channel = iter->second;
3025 if (send_channel->video_capturer() == NULL) {
3026 SendFrame(send_channel, frame, capturer->IsScreencast());
3027 }
3028 }
3029}
3030
3031bool WebRtcVideoMediaChannel::SendFrame(
3032 WebRtcVideoChannelSendInfo* send_channel,
3033 const VideoFrame* frame,
3034 bool is_screencast) {
3035 if (!send_channel) {
3036 return false;
3037 }
3038 if (!send_codec_) {
3039 // Send codec has not been set. No reason to process the frame any further.
3040 return false;
3041 }
3042 const VideoFormat& video_format = send_channel->video_format();
3043 // If the frame should be dropped.
3044 const bool video_format_set = video_format != cricket::VideoFormat();
3045 if (video_format_set &&
3046 (video_format.width == 0 && video_format.height == 0)) {
3047 return true;
3048 }
3049
3050 // Checks if we need to reset vie send codec.
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00003051 if (!MaybeResetVieSendCodec(send_channel,
3052 static_cast<int>(frame->GetWidth()),
3053 static_cast<int>(frame->GetHeight()),
3054 is_screencast, NULL)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003055 LOG(LS_ERROR) << "MaybeResetVieSendCodec failed with "
3056 << frame->GetWidth() << "x" << frame->GetHeight();
3057 return false;
3058 }
3059 const VideoFrame* frame_out = frame;
3060 talk_base::scoped_ptr<VideoFrame> processed_frame;
3061 // Disable muting for screencast.
3062 const bool mute = (send_channel->muted() && !is_screencast);
3063 send_channel->ProcessFrame(*frame_out, mute, processed_frame.use());
3064 if (processed_frame) {
3065 frame_out = processed_frame.get();
3066 }
3067
3068 webrtc::ViEVideoFrameI420 frame_i420;
3069 // TODO(ronghuawu): Update the webrtc::ViEVideoFrameI420
3070 // to use const unsigned char*
3071 frame_i420.y_plane = const_cast<unsigned char*>(frame_out->GetYPlane());
3072 frame_i420.u_plane = const_cast<unsigned char*>(frame_out->GetUPlane());
3073 frame_i420.v_plane = const_cast<unsigned char*>(frame_out->GetVPlane());
3074 frame_i420.y_pitch = frame_out->GetYPitch();
3075 frame_i420.u_pitch = frame_out->GetUPitch();
3076 frame_i420.v_pitch = frame_out->GetVPitch();
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00003077 frame_i420.width = static_cast<uint16>(frame_out->GetWidth());
3078 frame_i420.height = static_cast<uint16>(frame_out->GetHeight());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003079
3080 int64 timestamp_ntp_ms = 0;
3081 // TODO(justinlin): Reenable after Windows issues with clock drift are fixed.
3082 // Currently reverted to old behavior of discarding capture timestamp.
3083#if 0
3084 // If the frame timestamp is 0, we will use the deliver time.
3085 const int64 frame_timestamp = frame->GetTimeStamp();
3086 if (frame_timestamp != 0) {
3087 if (abs(time(NULL) - frame_timestamp / talk_base::kNumNanosecsPerSec) >
3088 kTimestampDeltaInSecondsForWarning) {
3089 LOG(LS_WARNING) << "Frame timestamp differs by more than "
3090 << kTimestampDeltaInSecondsForWarning << " seconds from "
3091 << "current Unix timestamp.";
3092 }
3093
3094 timestamp_ntp_ms =
3095 talk_base::UnixTimestampNanosecsToNtpMillisecs(frame_timestamp);
3096 }
3097#endif
3098
3099 return send_channel->external_capture()->IncomingFrameI420(
3100 frame_i420, timestamp_ntp_ms) == 0;
3101}
3102
3103bool WebRtcVideoMediaChannel::CreateChannel(uint32 ssrc_key,
3104 MediaDirection direction,
3105 int* channel_id) {
3106 // There are 3 types of channels. Sending only, receiving only and
3107 // sending and receiving. The sending and receiving channel is the
3108 // default channel and there is only one. All other channels that are created
3109 // are associated with the default channel which must exist. The default
3110 // channel id is stored in |vie_channel_|. All channels need to know about
3111 // the default channel to properly handle remb which is why there are
3112 // different ViE create channel calls.
3113 // For this channel the local and remote ssrc key is 0. However, it may
3114 // have a non-zero local and/or remote ssrc depending on if it is currently
3115 // sending and/or receiving.
3116 if ((vie_channel_ == -1 || direction == MD_SENDRECV) &&
3117 (!send_channels_.empty() || !recv_channels_.empty())) {
3118 ASSERT(false);
3119 return false;
3120 }
3121
3122 *channel_id = -1;
3123 if (direction == MD_RECV) {
3124 // All rec channels are associated with the default channel |vie_channel_|
3125 if (engine_->vie()->base()->CreateReceiveChannel(*channel_id,
3126 vie_channel_) != 0) {
3127 LOG_RTCERR2(CreateReceiveChannel, *channel_id, vie_channel_);
3128 return false;
3129 }
3130 } else if (direction == MD_SEND) {
3131 if (engine_->vie()->base()->CreateChannel(*channel_id,
3132 vie_channel_) != 0) {
3133 LOG_RTCERR2(CreateChannel, *channel_id, vie_channel_);
3134 return false;
3135 }
3136 } else {
3137 ASSERT(direction == MD_SENDRECV);
3138 if (engine_->vie()->base()->CreateChannel(*channel_id) != 0) {
3139 LOG_RTCERR1(CreateChannel, *channel_id);
3140 return false;
3141 }
3142 }
3143 if (!ConfigureChannel(*channel_id, direction, ssrc_key)) {
3144 engine_->vie()->base()->DeleteChannel(*channel_id);
3145 *channel_id = -1;
3146 return false;
3147 }
3148
3149 return true;
3150}
3151
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00003152bool WebRtcVideoMediaChannel::CreateUnsignalledRecvChannel(
3153 uint32 ssrc_key, int* out_channel_id) {
henrike@webrtc.org806768a2014-02-27 21:03:09 +00003154 int unsignalled_recv_channel_limit = 0;
3155 // TODO(tvsriram): Enable this once we fix handling packets
3156 // in default channel with unsignalled recv.
3157 // options_.unsignalled_recv_stream_limit.GetWithDefaultIfUnset(
3158 // kNumDefaultUnsignalledVideoRecvStreams);
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00003159 if (num_unsignalled_recv_channels_ >= unsignalled_recv_channel_limit) {
3160 return false;
3161 }
3162 if (!CreateChannel(ssrc_key, MD_RECV, out_channel_id)) {
3163 return false;
3164 }
3165 // TODO(tvsriram): Support dynamic sizing of unsignalled recv channels.
3166 num_unsignalled_recv_channels_++;
3167 return true;
3168}
3169
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003170bool WebRtcVideoMediaChannel::ConfigureChannel(int channel_id,
3171 MediaDirection direction,
3172 uint32 ssrc_key) {
3173 const bool receiving = (direction == MD_RECV) || (direction == MD_SENDRECV);
3174 const bool sending = (direction == MD_SEND) || (direction == MD_SENDRECV);
3175 // Register external transport.
3176 if (engine_->vie()->network()->RegisterSendTransport(
3177 channel_id, *this) != 0) {
3178 LOG_RTCERR1(RegisterSendTransport, channel_id);
3179 return false;
3180 }
3181
3182 // Set MTU.
3183 if (engine_->vie()->network()->SetMTU(channel_id, kVideoMtu) != 0) {
3184 LOG_RTCERR2(SetMTU, channel_id, kVideoMtu);
3185 return false;
3186 }
3187 // Turn on RTCP and loss feedback reporting.
3188 if (engine()->vie()->rtp()->SetRTCPStatus(
3189 channel_id, webrtc::kRtcpCompound_RFC4585) != 0) {
3190 LOG_RTCERR2(SetRTCPStatus, channel_id, webrtc::kRtcpCompound_RFC4585);
3191 return false;
3192 }
3193 // Enable pli as key frame request method.
3194 if (engine_->vie()->rtp()->SetKeyFrameRequestMethod(
3195 channel_id, webrtc::kViEKeyFrameRequestPliRtcp) != 0) {
3196 LOG_RTCERR2(SetKeyFrameRequestMethod,
3197 channel_id, webrtc::kViEKeyFrameRequestPliRtcp);
3198 return false;
3199 }
3200 if (!SetNackFec(channel_id, send_red_type_, send_fec_type_, nack_enabled_)) {
3201 // Logged in SetNackFec. Don't spam the logs.
3202 return false;
3203 }
3204 // Note that receiving must always be configured before sending to ensure
3205 // that send and receive channel is configured correctly (ConfigureReceiving
3206 // assumes no sending).
3207 if (receiving) {
3208 if (!ConfigureReceiving(channel_id, ssrc_key)) {
3209 return false;
3210 }
3211 }
3212 if (sending) {
3213 if (!ConfigureSending(channel_id, ssrc_key)) {
3214 return false;
3215 }
3216 }
3217
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00003218 // Start receiving for both receive and send channels so that we get incoming
3219 // RTP (if receiving) as well as RTCP feedback (if sending).
3220 if (engine()->vie()->base()->StartReceive(channel_id) != 0) {
3221 LOG_RTCERR1(StartReceive, channel_id);
3222 return false;
3223 }
3224
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003225 return true;
3226}
3227
3228bool WebRtcVideoMediaChannel::ConfigureReceiving(int channel_id,
3229 uint32 remote_ssrc_key) {
3230 // Make sure that an SSRC/key isn't registered more than once.
3231 if (recv_channels_.find(remote_ssrc_key) != recv_channels_.end()) {
3232 return false;
3233 }
3234 // Connect the voice channel, if there is one.
3235 // TODO(perkj): The A/V is synched by the receiving channel. So we need to
3236 // know the SSRC of the remote audio channel in order to fetch the correct
3237 // webrtc VoiceEngine channel. For now- only sync the default channel used
3238 // in 1-1 calls.
3239 if (remote_ssrc_key == 0 && voice_channel_) {
3240 WebRtcVoiceMediaChannel* voice_channel =
3241 static_cast<WebRtcVoiceMediaChannel*>(voice_channel_);
3242 if (engine_->vie()->base()->ConnectAudioChannel(
3243 vie_channel_, voice_channel->voe_channel()) != 0) {
3244 LOG_RTCERR2(ConnectAudioChannel, channel_id,
3245 voice_channel->voe_channel());
3246 LOG(LS_WARNING) << "A/V not synchronized";
3247 // Not a fatal error.
3248 }
3249 }
3250
3251 talk_base::scoped_ptr<WebRtcVideoChannelRecvInfo> channel_info(
3252 new WebRtcVideoChannelRecvInfo(channel_id));
3253
3254 // Install a render adapter.
3255 if (engine_->vie()->render()->AddRenderer(channel_id,
3256 webrtc::kVideoI420, channel_info->render_adapter()) != 0) {
3257 LOG_RTCERR3(AddRenderer, channel_id, webrtc::kVideoI420,
3258 channel_info->render_adapter());
3259 return false;
3260 }
3261
3262
3263 if (engine_->vie()->rtp()->SetRembStatus(channel_id,
3264 kNotSending,
3265 remb_enabled_) != 0) {
3266 LOG_RTCERR3(SetRembStatus, channel_id, kNotSending, remb_enabled_);
3267 return false;
3268 }
3269
3270 if (!SetHeaderExtension(&webrtc::ViERTP_RTCP::SetReceiveTimestampOffsetStatus,
3271 channel_id, receive_extensions_, kRtpTimestampOffsetHeaderExtension)) {
3272 return false;
3273 }
3274
3275 if (!SetHeaderExtension(
3276 &webrtc::ViERTP_RTCP::SetReceiveAbsoluteSendTimeStatus, channel_id,
3277 receive_extensions_, kRtpAbsoluteSendTimeHeaderExtension)) {
3278 return false;
3279 }
3280
3281 if (remote_ssrc_key != 0) {
3282 // Use the same SSRC as our default channel
3283 // (so the RTCP reports are correct).
3284 unsigned int send_ssrc = 0;
3285 webrtc::ViERTP_RTCP* rtp = engine()->vie()->rtp();
3286 if (rtp->GetLocalSSRC(vie_channel_, send_ssrc) == -1) {
3287 LOG_RTCERR2(GetLocalSSRC, vie_channel_, send_ssrc);
3288 return false;
3289 }
3290 if (rtp->SetLocalSSRC(channel_id, send_ssrc) == -1) {
3291 LOG_RTCERR2(SetLocalSSRC, channel_id, send_ssrc);
3292 return false;
3293 }
3294 } // Else this is the the default channel and we don't change the SSRC.
3295
3296 // Disable color enhancement since it is a bit too aggressive.
3297 if (engine()->vie()->image()->EnableColorEnhancement(channel_id,
3298 false) != 0) {
3299 LOG_RTCERR1(EnableColorEnhancement, channel_id);
3300 return false;
3301 }
3302
3303 if (!SetReceiveCodecs(channel_info.get())) {
3304 return false;
3305 }
3306
3307 int buffer_latency =
3308 options_.buffered_mode_latency.GetWithDefaultIfUnset(
3309 cricket::kBufferedModeDisabled);
3310 if (buffer_latency != cricket::kBufferedModeDisabled) {
3311 if (engine()->vie()->rtp()->SetReceiverBufferingMode(
3312 channel_id, buffer_latency) != 0) {
3313 LOG_RTCERR2(SetReceiverBufferingMode, channel_id, buffer_latency);
3314 }
3315 }
3316
3317 if (render_started_) {
3318 if (engine_->vie()->render()->StartRender(channel_id) != 0) {
3319 LOG_RTCERR1(StartRender, channel_id);
3320 return false;
3321 }
3322 }
3323
3324 // Register decoder observer for incoming framerate and bitrate.
3325 if (engine()->vie()->codec()->RegisterDecoderObserver(
3326 channel_id, *channel_info->decoder_observer()) != 0) {
3327 LOG_RTCERR1(RegisterDecoderObserver, channel_info->decoder_observer());
3328 return false;
3329 }
3330
3331 recv_channels_[remote_ssrc_key] = channel_info.release();
3332 return true;
3333}
3334
3335bool WebRtcVideoMediaChannel::ConfigureSending(int channel_id,
3336 uint32 local_ssrc_key) {
3337 // The ssrc key can be zero or correspond to an SSRC.
3338 // Make sure the default channel isn't configured more than once.
3339 if (local_ssrc_key == 0 && send_channels_.find(0) != send_channels_.end()) {
3340 return false;
3341 }
3342 // Make sure that the SSRC is not already in use.
3343 uint32 dummy_key;
3344 if (GetSendChannelKey(local_ssrc_key, &dummy_key)) {
3345 return false;
3346 }
3347 int vie_capture = 0;
3348 webrtc::ViEExternalCapture* external_capture = NULL;
3349 // Register external capture.
3350 if (engine()->vie()->capture()->AllocateExternalCaptureDevice(
3351 vie_capture, external_capture) != 0) {
3352 LOG_RTCERR0(AllocateExternalCaptureDevice);
3353 return false;
3354 }
3355
3356 // Connect external capture.
3357 if (engine()->vie()->capture()->ConnectCaptureDevice(
3358 vie_capture, channel_id) != 0) {
3359 LOG_RTCERR2(ConnectCaptureDevice, vie_capture, channel_id);
3360 return false;
3361 }
3362 talk_base::scoped_ptr<WebRtcVideoChannelSendInfo> send_channel(
3363 new WebRtcVideoChannelSendInfo(channel_id, vie_capture,
3364 external_capture,
3365 engine()->cpu_monitor()));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00003366 if (engine()->vie()->base()->RegisterCpuOveruseObserver(
3367 channel_id, send_channel->overuse_observer())) {
3368 LOG_RTCERR1(RegisterCpuOveruseObserver, channel_id);
3369 return false;
3370 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003371 send_channel->ApplyCpuOptions(options_);
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00003372 send_channel->SignalCpuAdaptationUnable.connect(this,
3373 &WebRtcVideoMediaChannel::OnCpuAdaptationUnable);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003374
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00003375 if (options_.cpu_overuse_detection.GetWithDefaultIfUnset(false)) {
3376 send_channel->SetCpuOveruseDetection(true);
3377 }
3378
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003379 // Register encoder observer for outgoing framerate and bitrate.
3380 if (engine()->vie()->codec()->RegisterEncoderObserver(
3381 channel_id, *send_channel->encoder_observer()) != 0) {
3382 LOG_RTCERR1(RegisterEncoderObserver, send_channel->encoder_observer());
3383 return false;
3384 }
3385
3386 if (!SetHeaderExtension(&webrtc::ViERTP_RTCP::SetSendTimestampOffsetStatus,
3387 channel_id, send_extensions_, kRtpTimestampOffsetHeaderExtension)) {
3388 return false;
3389 }
3390
3391 if (!SetHeaderExtension(&webrtc::ViERTP_RTCP::SetSendAbsoluteSendTimeStatus,
3392 channel_id, send_extensions_, kRtpAbsoluteSendTimeHeaderExtension)) {
3393 return false;
3394 }
3395
3396 if (options_.video_leaky_bucket.GetWithDefaultIfUnset(false)) {
3397 if (engine()->vie()->rtp()->SetTransmissionSmoothingStatus(channel_id,
3398 true) != 0) {
3399 LOG_RTCERR2(SetTransmissionSmoothingStatus, channel_id, true);
3400 return false;
3401 }
3402 }
3403
3404 int buffer_latency =
3405 options_.buffered_mode_latency.GetWithDefaultIfUnset(
3406 cricket::kBufferedModeDisabled);
3407 if (buffer_latency != cricket::kBufferedModeDisabled) {
3408 if (engine()->vie()->rtp()->SetSenderBufferingMode(
3409 channel_id, buffer_latency) != 0) {
3410 LOG_RTCERR2(SetSenderBufferingMode, channel_id, buffer_latency);
3411 }
3412 }
3413 // The remb status direction correspond to the RTP stream (and not the RTCP
3414 // stream). I.e. if send remb is enabled it means it is receiving remote
3415 // rembs and should use them to estimate bandwidth. Receive remb mean that
3416 // remb packets will be generated and that the channel should be included in
3417 // it. If remb is enabled all channels are allowed to contribute to the remb
3418 // but only receive channels will ever end up actually contributing. This
3419 // keeps the logic simple.
3420 if (engine_->vie()->rtp()->SetRembStatus(channel_id,
3421 remb_enabled_,
3422 remb_enabled_) != 0) {
3423 LOG_RTCERR3(SetRembStatus, channel_id, remb_enabled_, remb_enabled_);
3424 return false;
3425 }
3426 if (!SetNackFec(channel_id, send_red_type_, send_fec_type_, nack_enabled_)) {
3427 // Logged in SetNackFec. Don't spam the logs.
3428 return false;
3429 }
3430
3431 send_channels_[local_ssrc_key] = send_channel.release();
3432
3433 return true;
3434}
3435
3436bool WebRtcVideoMediaChannel::SetNackFec(int channel_id,
3437 int red_payload_type,
3438 int fec_payload_type,
3439 bool nack_enabled) {
3440 bool enable = (red_payload_type != -1 && fec_payload_type != -1 &&
3441 !InConferenceMode());
3442 if (enable) {
3443 if (engine_->vie()->rtp()->SetHybridNACKFECStatus(
3444 channel_id, nack_enabled, red_payload_type, fec_payload_type) != 0) {
3445 LOG_RTCERR4(SetHybridNACKFECStatus,
3446 channel_id, nack_enabled, red_payload_type, fec_payload_type);
3447 return false;
3448 }
3449 LOG(LS_INFO) << "Hybrid NACK/FEC enabled for channel " << channel_id;
3450 } else {
3451 if (engine_->vie()->rtp()->SetNACKStatus(channel_id, nack_enabled) != 0) {
3452 LOG_RTCERR1(SetNACKStatus, channel_id);
3453 return false;
3454 }
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00003455 std::string enabled = nack_enabled ? "enabled" : "disabled";
3456 LOG(LS_INFO) << "NACK " << enabled << " for channel " << channel_id;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003457 }
3458 return true;
3459}
3460
3461bool WebRtcVideoMediaChannel::SetSendCodec(const webrtc::VideoCodec& codec,
3462 int min_bitrate,
3463 int start_bitrate,
3464 int max_bitrate) {
3465 bool ret_val = true;
3466 for (SendChannelMap::iterator iter = send_channels_.begin();
3467 iter != send_channels_.end(); ++iter) {
3468 WebRtcVideoChannelSendInfo* send_channel = iter->second;
3469 ret_val = SetSendCodec(send_channel, codec, min_bitrate, start_bitrate,
3470 max_bitrate) && ret_val;
3471 }
3472 if (ret_val) {
3473 // All SetSendCodec calls were successful. Update the global state
3474 // accordingly.
3475 send_codec_.reset(new webrtc::VideoCodec(codec));
3476 send_min_bitrate_ = min_bitrate;
3477 send_start_bitrate_ = start_bitrate;
3478 send_max_bitrate_ = max_bitrate;
3479 } else {
3480 // At least one SetSendCodec call failed, rollback.
3481 for (SendChannelMap::iterator iter = send_channels_.begin();
3482 iter != send_channels_.end(); ++iter) {
3483 WebRtcVideoChannelSendInfo* send_channel = iter->second;
3484 if (send_codec_) {
3485 SetSendCodec(send_channel, *send_codec_.get(), send_min_bitrate_,
3486 send_start_bitrate_, send_max_bitrate_);
3487 }
3488 }
3489 }
3490 return ret_val;
3491}
3492
3493bool WebRtcVideoMediaChannel::SetSendCodec(
3494 WebRtcVideoChannelSendInfo* send_channel,
3495 const webrtc::VideoCodec& codec,
3496 int min_bitrate,
3497 int start_bitrate,
3498 int max_bitrate) {
3499 if (!send_channel) {
3500 return false;
3501 }
3502 const int channel_id = send_channel->channel_id();
3503 // Make a copy of the codec
3504 webrtc::VideoCodec target_codec = codec;
3505 target_codec.startBitrate = start_bitrate;
3506 target_codec.minBitrate = min_bitrate;
3507 target_codec.maxBitrate = max_bitrate;
3508
3509 // Set the default number of temporal layers for VP8.
3510 if (webrtc::kVideoCodecVP8 == codec.codecType) {
3511 target_codec.codecSpecific.VP8.numberOfTemporalLayers =
3512 kDefaultNumberOfTemporalLayers;
3513
3514 // Turn off the VP8 error resilience
3515 target_codec.codecSpecific.VP8.resilience = webrtc::kResilienceOff;
3516
3517 bool enable_denoising =
3518 options_.video_noise_reduction.GetWithDefaultIfUnset(false);
3519 target_codec.codecSpecific.VP8.denoisingOn = enable_denoising;
3520 }
3521
3522 // Register external encoder if codec type is supported by encoder factory.
3523 if (engine()->IsExternalEncoderCodecType(codec.codecType) &&
3524 !send_channel->IsEncoderRegistered(target_codec.plType)) {
3525 webrtc::VideoEncoder* encoder =
3526 engine()->CreateExternalEncoder(codec.codecType);
3527 if (encoder) {
3528 if (engine()->vie()->ext_codec()->RegisterExternalSendCodec(
3529 channel_id, target_codec.plType, encoder, false) == 0) {
3530 send_channel->RegisterEncoder(target_codec.plType, encoder);
3531 } else {
3532 LOG_RTCERR2(RegisterExternalSendCodec, channel_id, target_codec.plName);
3533 engine()->DestroyExternalEncoder(encoder);
3534 }
3535 }
3536 }
3537
3538 // Resolution and framerate may vary for different send channels.
3539 const VideoFormat& video_format = send_channel->video_format();
3540 UpdateVideoCodec(video_format, &target_codec);
3541
3542 if (target_codec.width == 0 && target_codec.height == 0) {
3543 const uint32 ssrc = send_channel->stream_params()->first_ssrc();
3544 LOG(LS_INFO) << "0x0 resolution selected. Captured frames will be dropped "
3545 << "for ssrc: " << ssrc << ".";
3546 } else {
3547 MaybeChangeStartBitrate(channel_id, &target_codec);
3548 if (0 != engine()->vie()->codec()->SetSendCodec(channel_id, target_codec)) {
3549 LOG_RTCERR2(SetSendCodec, channel_id, target_codec.plName);
3550 return false;
3551 }
3552
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00003553 // NOTE: SetRtxSendPayloadType must be called after all simulcast SSRCs
3554 // are configured. Otherwise ssrc's configured after this point will use
3555 // the primary PT for RTX.
3556 if (send_rtx_type_ != -1 &&
3557 engine()->vie()->rtp()->SetRtxSendPayloadType(channel_id,
3558 send_rtx_type_) != 0) {
3559 LOG_RTCERR2(SetRtxSendPayloadType, channel_id, send_rtx_type_);
3560 return false;
3561 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003562 }
3563 send_channel->set_interval(
3564 cricket::VideoFormat::FpsToInterval(target_codec.maxFramerate));
3565 return true;
3566}
3567
3568
3569static std::string ToString(webrtc::VideoCodecComplexity complexity) {
3570 switch (complexity) {
3571 case webrtc::kComplexityNormal:
3572 return "normal";
3573 case webrtc::kComplexityHigh:
3574 return "high";
3575 case webrtc::kComplexityHigher:
3576 return "higher";
3577 case webrtc::kComplexityMax:
3578 return "max";
3579 default:
3580 return "unknown";
3581 }
3582}
3583
3584static std::string ToString(webrtc::VP8ResilienceMode resilience) {
3585 switch (resilience) {
3586 case webrtc::kResilienceOff:
3587 return "off";
3588 case webrtc::kResilientStream:
3589 return "stream";
3590 case webrtc::kResilientFrames:
3591 return "frames";
3592 default:
3593 return "unknown";
3594 }
3595}
3596
3597void WebRtcVideoMediaChannel::LogSendCodecChange(const std::string& reason) {
3598 webrtc::VideoCodec vie_codec;
3599 if (engine()->vie()->codec()->GetSendCodec(vie_channel_, vie_codec) != 0) {
3600 LOG_RTCERR1(GetSendCodec, vie_channel_);
3601 return;
3602 }
3603
3604 LOG(LS_INFO) << reason << " : selected video codec "
3605 << vie_codec.plName << "/"
3606 << vie_codec.width << "x" << vie_codec.height << "x"
3607 << static_cast<int>(vie_codec.maxFramerate) << "fps"
3608 << "@" << vie_codec.maxBitrate << "kbps"
3609 << " (min=" << vie_codec.minBitrate << "kbps,"
3610 << " start=" << vie_codec.startBitrate << "kbps)";
3611 LOG(LS_INFO) << "Video max quantization: " << vie_codec.qpMax;
3612 if (webrtc::kVideoCodecVP8 == vie_codec.codecType) {
3613 LOG(LS_INFO) << "VP8 number of temporal layers: "
3614 << static_cast<int>(
3615 vie_codec.codecSpecific.VP8.numberOfTemporalLayers);
3616 LOG(LS_INFO) << "VP8 options : "
3617 << "picture loss indication = "
3618 << vie_codec.codecSpecific.VP8.pictureLossIndicationOn
3619 << ", feedback mode = "
3620 << vie_codec.codecSpecific.VP8.feedbackModeOn
3621 << ", complexity = "
3622 << ToString(vie_codec.codecSpecific.VP8.complexity)
3623 << ", resilience = "
3624 << ToString(vie_codec.codecSpecific.VP8.resilience)
3625 << ", denoising = "
3626 << vie_codec.codecSpecific.VP8.denoisingOn
3627 << ", error concealment = "
3628 << vie_codec.codecSpecific.VP8.errorConcealmentOn
3629 << ", automatic resize = "
3630 << vie_codec.codecSpecific.VP8.automaticResizeOn
3631 << ", frame dropping = "
3632 << vie_codec.codecSpecific.VP8.frameDroppingOn
3633 << ", key frame interval = "
3634 << vie_codec.codecSpecific.VP8.keyFrameInterval;
3635 }
3636
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00003637 if (send_rtx_type_ != -1) {
3638 LOG(LS_INFO) << "RTX payload type: " << send_rtx_type_;
3639 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003640}
3641
3642bool WebRtcVideoMediaChannel::SetReceiveCodecs(
3643 WebRtcVideoChannelRecvInfo* info) {
3644 int red_type = -1;
3645 int fec_type = -1;
3646 int channel_id = info->channel_id();
3647 for (std::vector<webrtc::VideoCodec>::iterator it = receive_codecs_.begin();
3648 it != receive_codecs_.end(); ++it) {
3649 if (it->codecType == webrtc::kVideoCodecRED) {
3650 red_type = it->plType;
3651 } else if (it->codecType == webrtc::kVideoCodecULPFEC) {
3652 fec_type = it->plType;
3653 }
3654 if (engine()->vie()->codec()->SetReceiveCodec(channel_id, *it) != 0) {
3655 LOG_RTCERR2(SetReceiveCodec, channel_id, it->plName);
3656 return false;
3657 }
3658 if (!info->IsDecoderRegistered(it->plType) &&
3659 it->codecType != webrtc::kVideoCodecRED &&
3660 it->codecType != webrtc::kVideoCodecULPFEC) {
3661 webrtc::VideoDecoder* decoder =
3662 engine()->CreateExternalDecoder(it->codecType);
3663 if (decoder) {
3664 if (engine()->vie()->ext_codec()->RegisterExternalReceiveCodec(
3665 channel_id, it->plType, decoder) == 0) {
3666 info->RegisterDecoder(it->plType, decoder);
3667 } else {
3668 LOG_RTCERR2(RegisterExternalReceiveCodec, channel_id, it->plName);
3669 engine()->DestroyExternalDecoder(decoder);
3670 }
3671 }
3672 }
3673 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003674 return true;
3675}
3676
3677int WebRtcVideoMediaChannel::GetRecvChannelNum(uint32 ssrc) {
3678 if (ssrc == first_receive_ssrc_) {
3679 return vie_channel_;
3680 }
3681 RecvChannelMap::iterator it = recv_channels_.find(ssrc);
3682 return (it != recv_channels_.end()) ? it->second->channel_id() : -1;
3683}
3684
3685// If the new frame size is different from the send codec size we set on vie,
3686// we need to reset the send codec on vie.
3687// The new send codec size should not exceed send_codec_ which is controlled
3688// only by the 'jec' logic.
3689bool WebRtcVideoMediaChannel::MaybeResetVieSendCodec(
3690 WebRtcVideoChannelSendInfo* send_channel,
3691 int new_width,
3692 int new_height,
3693 bool is_screencast,
3694 bool* reset) {
3695 if (reset) {
3696 *reset = false;
3697 }
3698 ASSERT(send_codec_.get() != NULL);
3699
3700 webrtc::VideoCodec target_codec = *send_codec_.get();
3701 const VideoFormat& video_format = send_channel->video_format();
3702 UpdateVideoCodec(video_format, &target_codec);
3703
3704 // Vie send codec size should not exceed target_codec.
3705 int target_width = new_width;
3706 int target_height = new_height;
3707 if (!is_screencast &&
3708 (new_width > target_codec.width || new_height > target_codec.height)) {
3709 target_width = target_codec.width;
3710 target_height = target_codec.height;
3711 }
3712
3713 // Get current vie codec.
3714 webrtc::VideoCodec vie_codec;
3715 const int channel_id = send_channel->channel_id();
3716 if (engine()->vie()->codec()->GetSendCodec(channel_id, vie_codec) != 0) {
3717 LOG_RTCERR1(GetSendCodec, channel_id);
3718 return false;
3719 }
3720 const int cur_width = vie_codec.width;
3721 const int cur_height = vie_codec.height;
3722
3723 // Only reset send codec when there is a size change. Additionally,
3724 // automatic resize needs to be turned off when screencasting and on when
3725 // not screencasting.
3726 // Don't allow automatic resizing for screencasting.
3727 bool automatic_resize = !is_screencast;
3728 // Turn off VP8 frame dropping when screensharing as the current model does
3729 // not work well at low fps.
3730 bool vp8_frame_dropping = !is_screencast;
3731 // Disable denoising for screencasting.
3732 bool enable_denoising =
3733 options_.video_noise_reduction.GetWithDefaultIfUnset(false);
3734 bool denoising = !is_screencast && enable_denoising;
3735 bool reset_send_codec =
3736 target_width != cur_width || target_height != cur_height ||
3737 automatic_resize != vie_codec.codecSpecific.VP8.automaticResizeOn ||
3738 denoising != vie_codec.codecSpecific.VP8.denoisingOn ||
3739 vp8_frame_dropping != vie_codec.codecSpecific.VP8.frameDroppingOn;
3740
3741 if (reset_send_codec) {
3742 // Set the new codec on vie.
3743 vie_codec.width = target_width;
3744 vie_codec.height = target_height;
3745 vie_codec.maxFramerate = target_codec.maxFramerate;
3746 vie_codec.startBitrate = target_codec.startBitrate;
3747 vie_codec.codecSpecific.VP8.automaticResizeOn = automatic_resize;
3748 vie_codec.codecSpecific.VP8.denoisingOn = denoising;
3749 vie_codec.codecSpecific.VP8.frameDroppingOn = vp8_frame_dropping;
3750 // TODO(mflodman): Remove 'is_screencast' check when screen cast settings
3751 // are treated correctly in WebRTC.
3752 if (!is_screencast)
3753 MaybeChangeStartBitrate(channel_id, &vie_codec);
3754
3755 if (engine()->vie()->codec()->SetSendCodec(channel_id, vie_codec) != 0) {
3756 LOG_RTCERR1(SetSendCodec, channel_id);
3757 return false;
3758 }
3759 if (reset) {
3760 *reset = true;
3761 }
3762 LogSendCodecChange("Capture size changed");
3763 }
3764
3765 return true;
3766}
3767
3768void WebRtcVideoMediaChannel::MaybeChangeStartBitrate(
3769 int channel_id, webrtc::VideoCodec* video_codec) {
3770 if (video_codec->startBitrate < video_codec->minBitrate) {
3771 video_codec->startBitrate = video_codec->minBitrate;
3772 } else if (video_codec->startBitrate > video_codec->maxBitrate) {
3773 video_codec->startBitrate = video_codec->maxBitrate;
3774 }
3775
3776 // Use a previous target bitrate, if there is one.
3777 unsigned int current_target_bitrate = 0;
3778 if (engine()->vie()->codec()->GetCodecTargetBitrate(
3779 channel_id, &current_target_bitrate) == 0) {
3780 // Convert to kbps.
3781 current_target_bitrate /= 1000;
3782 if (current_target_bitrate > video_codec->maxBitrate) {
3783 current_target_bitrate = video_codec->maxBitrate;
3784 }
3785 if (current_target_bitrate > video_codec->startBitrate) {
3786 video_codec->startBitrate = current_target_bitrate;
3787 }
3788 }
3789}
3790
3791void WebRtcVideoMediaChannel::OnMessage(talk_base::Message* msg) {
3792 FlushBlackFrameData* black_frame_data =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00003793 static_cast<FlushBlackFrameData*>(msg->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003794 FlushBlackFrame(black_frame_data->ssrc, black_frame_data->timestamp);
3795 delete black_frame_data;
3796}
3797
3798int WebRtcVideoMediaChannel::SendPacket(int channel, const void* data,
3799 int len) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003800 talk_base::Buffer packet(data, len, kMaxRtpPacketLen);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00003801 return MediaChannel::SendPacket(&packet) ? len : -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003802}
3803
3804int WebRtcVideoMediaChannel::SendRTCPPacket(int channel,
3805 const void* data,
3806 int len) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003807 talk_base::Buffer packet(data, len, kMaxRtpPacketLen);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00003808 return MediaChannel::SendRtcp(&packet) ? len : -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003809}
3810
3811void WebRtcVideoMediaChannel::QueueBlackFrame(uint32 ssrc, int64 timestamp,
3812 int framerate) {
3813 if (timestamp) {
3814 FlushBlackFrameData* black_frame_data = new FlushBlackFrameData(
3815 ssrc,
3816 timestamp);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00003817 const int delay_ms = static_cast<int>(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003818 2 * cricket::VideoFormat::FpsToInterval(framerate) *
3819 talk_base::kNumMillisecsPerSec / talk_base::kNumNanosecsPerSec);
3820 worker_thread()->PostDelayed(delay_ms, this, 0, black_frame_data);
3821 }
3822}
3823
3824void WebRtcVideoMediaChannel::FlushBlackFrame(uint32 ssrc, int64 timestamp) {
3825 WebRtcVideoChannelSendInfo* send_channel = GetSendChannel(ssrc);
3826 if (!send_channel) {
3827 return;
3828 }
3829 talk_base::scoped_ptr<const VideoFrame> black_frame_ptr;
3830
3831 const WebRtcLocalStreamInfo* channel_stream_info =
3832 send_channel->local_stream_info();
3833 int64 last_frame_time_stamp = channel_stream_info->time_stamp();
3834 if (last_frame_time_stamp == timestamp) {
3835 size_t last_frame_width = 0;
3836 size_t last_frame_height = 0;
3837 int64 last_frame_elapsed_time = 0;
3838 channel_stream_info->GetLastFrameInfo(&last_frame_width, &last_frame_height,
3839 &last_frame_elapsed_time);
3840 if (!last_frame_width || !last_frame_height) {
3841 return;
3842 }
3843 WebRtcVideoFrame black_frame;
3844 // Black frame is not screencast.
3845 const bool screencasting = false;
3846 const int64 timestamp_delta = send_channel->interval();
3847 if (!black_frame.InitToBlack(send_codec_->width, send_codec_->height, 1, 1,
3848 last_frame_elapsed_time + timestamp_delta,
3849 last_frame_time_stamp + timestamp_delta) ||
3850 !SendFrame(send_channel, &black_frame, screencasting)) {
3851 LOG(LS_ERROR) << "Failed to send black frame.";
3852 }
3853 }
3854}
3855
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00003856void WebRtcVideoMediaChannel::OnCpuAdaptationUnable() {
3857 // ssrc is hardcoded to 0. This message is based on a system wide issue,
3858 // so finding which ssrc caused it doesn't matter.
3859 SignalMediaError(0, VideoMediaChannel::ERROR_REC_CPU_MAX_CANT_DOWNGRADE);
3860}
3861
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003862void WebRtcVideoMediaChannel::SetNetworkTransmissionState(
3863 bool is_transmitting) {
3864 LOG(LS_INFO) << "SetNetworkTransmissionState: " << is_transmitting;
3865 for (SendChannelMap::iterator iter = send_channels_.begin();
3866 iter != send_channels_.end(); ++iter) {
3867 WebRtcVideoChannelSendInfo* send_channel = iter->second;
3868 int channel_id = send_channel->channel_id();
3869 engine_->vie()->network()->SetNetworkTransmissionState(channel_id,
3870 is_transmitting);
3871 }
3872}
3873
3874bool WebRtcVideoMediaChannel::SetHeaderExtension(ExtensionSetterFunction setter,
3875 int channel_id, const RtpHeaderExtension* extension) {
3876 bool enable = false;
3877 int id = 0;
3878 if (extension) {
3879 enable = true;
3880 id = extension->id;
3881 }
3882 if ((engine_->vie()->rtp()->*setter)(channel_id, enable, id) != 0) {
3883 LOG_RTCERR4(*setter, extension->uri, channel_id, enable, id);
3884 return false;
3885 }
3886 return true;
3887}
3888
3889bool WebRtcVideoMediaChannel::SetHeaderExtension(ExtensionSetterFunction setter,
3890 int channel_id, const std::vector<RtpHeaderExtension>& extensions,
3891 const char header_extension_uri[]) {
3892 const RtpHeaderExtension* extension = FindHeaderExtension(extensions,
3893 header_extension_uri);
3894 return SetHeaderExtension(setter, channel_id, extension);
3895}
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00003896
3897bool WebRtcVideoMediaChannel::SetLocalRtxSsrc(int channel_id,
3898 const StreamParams& send_params,
3899 uint32 primary_ssrc,
3900 int stream_idx) {
3901 uint32 rtx_ssrc = 0;
3902 bool has_rtx = send_params.GetFidSsrc(primary_ssrc, &rtx_ssrc);
3903 if (has_rtx && engine()->vie()->rtp()->SetLocalSSRC(
3904 channel_id, rtx_ssrc, webrtc::kViEStreamTypeRtx, stream_idx) != 0) {
3905 LOG_RTCERR4(SetLocalSSRC, channel_id, rtx_ssrc,
3906 webrtc::kViEStreamTypeRtx, stream_idx);
3907 return false;
3908 }
3909 return true;
3910}
3911
wu@webrtc.org24301a62013-12-13 19:17:43 +00003912void WebRtcVideoMediaChannel::MaybeConnectCapturer(VideoCapturer* capturer) {
3913 if (capturer != NULL && GetSendChannelNum(capturer) == 1) {
3914 capturer->SignalVideoFrame.connect(this,
3915 &WebRtcVideoMediaChannel::SendFrame);
3916 }
3917}
3918
3919void WebRtcVideoMediaChannel::MaybeDisconnectCapturer(VideoCapturer* capturer) {
3920 if (capturer != NULL && GetSendChannelNum(capturer) == 1) {
3921 capturer->SignalVideoFrame.disconnect(this);
3922 }
3923}
3924
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003925} // namespace cricket
3926
3927#endif // HAVE_WEBRTC_VIDEO