blob: 6067507dfc38b7d109a89824088c17d46d600096 [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"
63
64#if !defined(LIBPEERCONNECTION_LIB)
65#ifndef HAVE_WEBRTC_VIDEO
66#error Need webrtc video
67#endif
68#include "talk/media/webrtc/webrtcmediaengine.h"
69
70WRME_EXPORT
71cricket::MediaEngineInterface* CreateWebRtcMediaEngine(
72 webrtc::AudioDeviceModule* adm, webrtc::AudioDeviceModule* adm_sc,
73 cricket::WebRtcVideoEncoderFactory* encoder_factory,
74 cricket::WebRtcVideoDecoderFactory* decoder_factory) {
75 return new cricket::WebRtcMediaEngine(adm, adm_sc, encoder_factory,
76 decoder_factory);
77}
78
79WRME_EXPORT
80void DestroyWebRtcMediaEngine(cricket::MediaEngineInterface* media_engine) {
81 delete static_cast<cricket::WebRtcMediaEngine*>(media_engine);
82}
83#endif
84
85
86namespace cricket {
87
88
89static const int kDefaultLogSeverity = talk_base::LS_WARNING;
90
91static const int kMinVideoBitrate = 50;
92static const int kStartVideoBitrate = 300;
93static const int kMaxVideoBitrate = 2000;
94static const int kDefaultConferenceModeMaxVideoBitrate = 500;
95
wu@webrtc.orgcecfd182013-10-30 05:18:12 +000096// Controlled by exp, try a super low minimum bitrate for poor connections.
97static const int kLowerMinBitrate = 30;
98
henrike@webrtc.org28e20752013-07-10 00:45:36 +000099static const int kVideoMtu = 1200;
100
101static const int kVideoRtpBufferSize = 65536;
102
103static const char kVp8PayloadName[] = "VP8";
104static const char kRedPayloadName[] = "red";
105static const char kFecPayloadName[] = "ulpfec";
106
107static const int kDefaultNumberOfTemporalLayers = 1; // 1:1
108
109static const int kTimestampDeltaInSecondsForWarning = 2;
110
111static const int kMaxExternalVideoCodecs = 8;
112static const int kExternalVideoPayloadTypeBase = 120;
113
114// Static allocation of payload type values for external video codec.
115static int GetExternalVideoPayloadType(int index) {
116 ASSERT(index >= 0 && index < kMaxExternalVideoCodecs);
117 return kExternalVideoPayloadTypeBase + index;
118}
119
120static void LogMultiline(talk_base::LoggingSeverity sev, char* text) {
121 const char* delim = "\r\n";
122 // TODO(fbarchard): Fix strtok lint warning.
123 for (char* tok = strtok(text, delim); tok; tok = strtok(NULL, delim)) {
124 LOG_V(sev) << tok;
125 }
126}
127
128// Severity is an integer because it comes is assumed to be from command line.
129static int SeverityToFilter(int severity) {
130 int filter = webrtc::kTraceNone;
131 switch (severity) {
132 case talk_base::LS_VERBOSE:
133 filter |= webrtc::kTraceAll;
134 case talk_base::LS_INFO:
135 filter |= (webrtc::kTraceStateInfo | webrtc::kTraceInfo);
136 case talk_base::LS_WARNING:
137 filter |= (webrtc::kTraceTerseInfo | webrtc::kTraceWarning);
138 case talk_base::LS_ERROR:
139 filter |= (webrtc::kTraceError | webrtc::kTraceCritical);
140 }
141 return filter;
142}
143
144static const int kCpuMonitorPeriodMs = 2000; // 2 seconds.
145
146static const bool kNotSending = false;
147
148// Extension header for RTP timestamp offset, see RFC 5450 for details:
149// http://tools.ietf.org/html/rfc5450
150static const char kRtpTimestampOffsetHeaderExtension[] =
151 "urn:ietf:params:rtp-hdrext:toffset";
152static const int kRtpTimeOffsetExtensionId = 2;
153
154// Extension header for absolute send time, see url for details:
155// http://www.webrtc.org/experiments/rtp-hdrext/abs-send-time
156static const char kRtpAbsoluteSendTimeHeaderExtension[] =
157 "http://www.webrtc.org/experiments/rtp-hdrext/abs-send-time";
158static const int kRtpAbsoluteSendTimeExtensionId = 3;
wu@webrtc.orgde305012013-10-31 15:40:38 +0000159// Default video dscp value.
160// See http://tools.ietf.org/html/rfc2474 for details
161// See also http://tools.ietf.org/html/draft-jennings-rtcweb-qos-00
162static const talk_base::DiffServCodePoint kVideoDscpValue =
163 talk_base::DSCP_AF41;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000164
165static bool IsNackEnabled(const VideoCodec& codec) {
166 return codec.HasFeedbackParam(FeedbackParam(kRtcpFbParamNack,
167 kParamValueEmpty));
168}
169
170// Returns true if Receiver Estimated Max Bitrate is enabled.
171static bool IsRembEnabled(const VideoCodec& codec) {
172 return codec.HasFeedbackParam(FeedbackParam(kRtcpFbParamRemb,
173 kParamValueEmpty));
174}
175
176struct FlushBlackFrameData : public talk_base::MessageData {
177 FlushBlackFrameData(uint32 s, int64 t) : ssrc(s), timestamp(t) {
178 }
179 uint32 ssrc;
180 int64 timestamp;
181};
182
183class WebRtcRenderAdapter : public webrtc::ExternalRenderer {
184 public:
185 explicit WebRtcRenderAdapter(VideoRenderer* renderer)
186 : renderer_(renderer), width_(0), height_(0), watermark_enabled_(false) {
187 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000188
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000189 virtual ~WebRtcRenderAdapter() {
190 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000191
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000192 void set_watermark_enabled(bool enable) {
193 talk_base::CritScope cs(&crit_);
194 watermark_enabled_ = enable;
195 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000196
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000197 void SetRenderer(VideoRenderer* renderer) {
198 talk_base::CritScope cs(&crit_);
199 renderer_ = renderer;
200 // FrameSizeChange may have already been called when renderer was not set.
201 // If so we should call SetSize here.
202 // TODO(ronghuawu): Add unit test for this case. Didn't do it now
203 // because the WebRtcRenderAdapter is currently hiding in cc file. No
204 // good way to get access to it from the unit test.
205 if (width_ > 0 && height_ > 0 && renderer_ != NULL) {
206 if (!renderer_->SetSize(width_, height_, 0)) {
207 LOG(LS_ERROR)
208 << "WebRtcRenderAdapter SetRenderer failed to SetSize to: "
209 << width_ << "x" << height_;
210 }
211 }
212 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000213
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000214 // Implementation of webrtc::ExternalRenderer.
215 virtual int FrameSizeChange(unsigned int width, unsigned int height,
216 unsigned int /*number_of_streams*/) {
217 talk_base::CritScope cs(&crit_);
218 width_ = width;
219 height_ = height;
220 LOG(LS_INFO) << "WebRtcRenderAdapter frame size changed to: "
221 << width << "x" << height;
222 if (renderer_ == NULL) {
223 LOG(LS_VERBOSE) << "WebRtcRenderAdapter the renderer has not been set. "
224 << "SetSize will be called later in SetRenderer.";
225 return 0;
226 }
227 return renderer_->SetSize(width_, height_, 0) ? 0 : -1;
228 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000229
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000230 virtual int DeliverFrame(unsigned char* buffer, int buffer_size,
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000231 uint32_t time_stamp, int64_t render_time
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000232 , void* handle
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000233 ) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000234 talk_base::CritScope cs(&crit_);
235 frame_rate_tracker_.Update(1);
236 if (renderer_ == NULL) {
237 return 0;
238 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000239 // Convert 90K rtp timestamp to ns timestamp.
240 int64 rtp_time_stamp_in_ns = (time_stamp / 90) *
241 talk_base::kNumNanosecsPerMillisec;
242 // Convert milisecond render time to ns timestamp.
243 int64 render_time_stamp_in_ns = render_time *
244 talk_base::kNumNanosecsPerMillisec;
245 // Send the rtp timestamp to renderer as the VideoFrame timestamp.
246 // and the render timestamp as the VideoFrame elapsed_time.
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000247 if (handle == NULL) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000248 return DeliverBufferFrame(buffer, buffer_size, render_time_stamp_in_ns,
249 rtp_time_stamp_in_ns);
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000250 } else {
251 return DeliverTextureFrame(handle, render_time_stamp_in_ns,
252 rtp_time_stamp_in_ns);
253 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000254 }
255
256 virtual bool IsTextureSupported() { return true; }
257
258 int DeliverBufferFrame(unsigned char* buffer, int buffer_size,
259 int64 elapsed_time, int64 time_stamp) {
260 WebRtcVideoFrame video_frame;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000261 video_frame.Attach(buffer, buffer_size, width_, height_,
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000262 1, 1, elapsed_time, time_stamp, 0);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000263
264
265 // Sanity check on decoded frame size.
266 if (buffer_size != static_cast<int>(VideoFrame::SizeOf(width_, height_))) {
267 LOG(LS_WARNING) << "WebRtcRenderAdapter received a strange frame size: "
268 << buffer_size;
269 }
270
271 int ret = renderer_->RenderFrame(&video_frame) ? 0 : -1;
272 uint8* buffer_temp;
273 size_t buffer_size_temp;
274 video_frame.Detach(&buffer_temp, &buffer_size_temp);
275 return ret;
276 }
277
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000278 int DeliverTextureFrame(void* handle, int64 elapsed_time, int64 time_stamp) {
279 WebRtcTextureVideoFrame video_frame(
280 static_cast<webrtc::NativeHandle*>(handle), width_, height_,
281 elapsed_time, time_stamp);
282 return renderer_->RenderFrame(&video_frame);
283 }
284
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000285 unsigned int width() {
286 talk_base::CritScope cs(&crit_);
287 return width_;
288 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000289
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000290 unsigned int height() {
291 talk_base::CritScope cs(&crit_);
292 return height_;
293 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000294
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000295 int framerate() {
296 talk_base::CritScope cs(&crit_);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000297 return static_cast<int>(frame_rate_tracker_.units_second());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000298 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000299
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000300 VideoRenderer* renderer() {
301 talk_base::CritScope cs(&crit_);
302 return renderer_;
303 }
304
305 private:
306 talk_base::CriticalSection crit_;
307 VideoRenderer* renderer_;
308 unsigned int width_;
309 unsigned int height_;
310 talk_base::RateTracker frame_rate_tracker_;
311 bool watermark_enabled_;
312};
313
314class WebRtcDecoderObserver : public webrtc::ViEDecoderObserver {
315 public:
316 explicit WebRtcDecoderObserver(int video_channel)
317 : video_channel_(video_channel),
318 framerate_(0),
319 bitrate_(0),
wu@webrtc.org97077a32013-10-25 21:18:33 +0000320 decode_ms_(0),
321 max_decode_ms_(0),
322 current_delay_ms_(0),
323 target_delay_ms_(0),
324 jitter_buffer_ms_(0),
325 min_playout_delay_ms_(0),
326 render_delay_ms_(0),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000327 firs_requested_(0) {
328 }
329
330 // virtual functions from VieDecoderObserver.
331 virtual void IncomingCodecChanged(const int videoChannel,
332 const webrtc::VideoCodec& videoCodec) {}
333 virtual void IncomingRate(const int videoChannel,
334 const unsigned int framerate,
335 const unsigned int bitrate) {
wu@webrtc.org78187522013-10-07 23:32:02 +0000336 talk_base::CritScope cs(&crit_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000337 ASSERT(video_channel_ == videoChannel);
338 framerate_ = framerate;
339 bitrate_ = bitrate;
340 }
wu@webrtc.org97077a32013-10-25 21:18:33 +0000341
342 virtual void DecoderTiming(int decode_ms,
343 int max_decode_ms,
344 int current_delay_ms,
345 int target_delay_ms,
346 int jitter_buffer_ms,
347 int min_playout_delay_ms,
348 int render_delay_ms) {
349 talk_base::CritScope cs(&crit_);
350 decode_ms_ = decode_ms;
351 max_decode_ms_ = max_decode_ms;
352 current_delay_ms_ = current_delay_ms;
353 target_delay_ms_ = target_delay_ms;
354 jitter_buffer_ms_ = jitter_buffer_ms;
355 min_playout_delay_ms_ = min_playout_delay_ms;
356 render_delay_ms_ = render_delay_ms;
357 }
358
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000359 virtual void RequestNewKeyFrame(const int videoChannel) {
wu@webrtc.org78187522013-10-07 23:32:02 +0000360 talk_base::CritScope cs(&crit_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000361 ASSERT(video_channel_ == videoChannel);
362 ++firs_requested_;
363 }
364
wu@webrtc.org97077a32013-10-25 21:18:33 +0000365 // Populate |rinfo| based on previously-set data in |*this|.
366 void ExportTo(VideoReceiverInfo* rinfo) {
wu@webrtc.org78187522013-10-07 23:32:02 +0000367 talk_base::CritScope cs(&crit_);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000368 rinfo->firs_sent = firs_requested_;
369 rinfo->framerate_rcvd = framerate_;
370 rinfo->decode_ms = decode_ms_;
371 rinfo->max_decode_ms = max_decode_ms_;
372 rinfo->current_delay_ms = current_delay_ms_;
373 rinfo->target_delay_ms = target_delay_ms_;
374 rinfo->jitter_buffer_ms = jitter_buffer_ms_;
375 rinfo->min_playout_delay_ms = min_playout_delay_ms_;
376 rinfo->render_delay_ms = render_delay_ms_;
wu@webrtc.org78187522013-10-07 23:32:02 +0000377 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000378
379 private:
wu@webrtc.org78187522013-10-07 23:32:02 +0000380 mutable talk_base::CriticalSection crit_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000381 int video_channel_;
382 int framerate_;
383 int bitrate_;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000384 int decode_ms_;
385 int max_decode_ms_;
386 int current_delay_ms_;
387 int target_delay_ms_;
388 int jitter_buffer_ms_;
389 int min_playout_delay_ms_;
390 int render_delay_ms_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000391 int firs_requested_;
392};
393
394class WebRtcEncoderObserver : public webrtc::ViEEncoderObserver {
395 public:
396 explicit WebRtcEncoderObserver(int video_channel)
397 : video_channel_(video_channel),
398 framerate_(0),
399 bitrate_(0) {
400 }
401
402 // virtual functions from VieEncoderObserver.
403 virtual void OutgoingRate(const int videoChannel,
404 const unsigned int framerate,
405 const unsigned int bitrate) {
wu@webrtc.org78187522013-10-07 23:32:02 +0000406 talk_base::CritScope cs(&crit_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000407 ASSERT(video_channel_ == videoChannel);
408 framerate_ = framerate;
409 bitrate_ = bitrate;
410 }
411
wu@webrtc.org78187522013-10-07 23:32:02 +0000412 int framerate() const {
413 talk_base::CritScope cs(&crit_);
414 return framerate_;
415 }
416 int bitrate() const {
417 talk_base::CritScope cs(&crit_);
418 return bitrate_;
419 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000420
421 private:
wu@webrtc.org78187522013-10-07 23:32:02 +0000422 mutable talk_base::CriticalSection crit_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000423 int video_channel_;
424 int framerate_;
425 int bitrate_;
426};
427
428class WebRtcLocalStreamInfo {
429 public:
430 WebRtcLocalStreamInfo()
431 : width_(0), height_(0), elapsed_time_(-1), time_stamp_(-1) {}
432 size_t width() const {
433 talk_base::CritScope cs(&crit_);
434 return width_;
435 }
436 size_t height() const {
437 talk_base::CritScope cs(&crit_);
438 return height_;
439 }
440 int64 elapsed_time() const {
441 talk_base::CritScope cs(&crit_);
442 return elapsed_time_;
443 }
444 int64 time_stamp() const {
445 talk_base::CritScope cs(&crit_);
446 return time_stamp_;
447 }
448 int framerate() {
449 talk_base::CritScope cs(&crit_);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000450 return static_cast<int>(rate_tracker_.units_second());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000451 }
452 void GetLastFrameInfo(
453 size_t* width, size_t* height, int64* elapsed_time) const {
454 talk_base::CritScope cs(&crit_);
455 *width = width_;
456 *height = height_;
457 *elapsed_time = elapsed_time_;
458 }
459
460 void UpdateFrame(const VideoFrame* frame) {
461 talk_base::CritScope cs(&crit_);
462
463 width_ = frame->GetWidth();
464 height_ = frame->GetHeight();
465 elapsed_time_ = frame->GetElapsedTime();
466 time_stamp_ = frame->GetTimeStamp();
467
468 rate_tracker_.Update(1);
469 }
470
471 private:
472 mutable talk_base::CriticalSection crit_;
473 size_t width_;
474 size_t height_;
475 int64 elapsed_time_;
476 int64 time_stamp_;
477 talk_base::RateTracker rate_tracker_;
478
479 DISALLOW_COPY_AND_ASSIGN(WebRtcLocalStreamInfo);
480};
481
482// WebRtcVideoChannelRecvInfo is a container class with members such as renderer
483// and a decoder observer that is used by receive channels.
484// It must exist as long as the receive channel is connected to renderer or a
485// decoder observer in this class and methods in the class should only be called
486// from the worker thread.
487class WebRtcVideoChannelRecvInfo {
488 public:
489 typedef std::map<int, webrtc::VideoDecoder*> DecoderMap; // key: payload type
490 explicit WebRtcVideoChannelRecvInfo(int channel_id)
491 : channel_id_(channel_id),
492 render_adapter_(NULL),
493 decoder_observer_(channel_id) {
494 }
495 int channel_id() { return channel_id_; }
496 void SetRenderer(VideoRenderer* renderer) {
497 render_adapter_.SetRenderer(renderer);
498 }
499 WebRtcRenderAdapter* render_adapter() { return &render_adapter_; }
500 WebRtcDecoderObserver* decoder_observer() { return &decoder_observer_; }
501 void RegisterDecoder(int pl_type, webrtc::VideoDecoder* decoder) {
502 ASSERT(!IsDecoderRegistered(pl_type));
503 registered_decoders_[pl_type] = decoder;
504 }
505 bool IsDecoderRegistered(int pl_type) {
506 return registered_decoders_.count(pl_type) != 0;
507 }
508 const DecoderMap& registered_decoders() {
509 return registered_decoders_;
510 }
511 void ClearRegisteredDecoders() {
512 registered_decoders_.clear();
513 }
514
515 private:
516 int channel_id_; // Webrtc video channel number.
517 // Renderer for this channel.
518 WebRtcRenderAdapter render_adapter_;
519 WebRtcDecoderObserver decoder_observer_;
520 DecoderMap registered_decoders_;
521};
522
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000523class WebRtcOveruseObserver : public webrtc::CpuOveruseObserver {
524 public:
525 explicit WebRtcOveruseObserver(CoordinatedVideoAdapter* video_adapter)
526 : video_adapter_(video_adapter),
527 enabled_(false) {
528 }
529
530 // TODO(mflodman): Consider sending resolution as part of event, to let
531 // adapter know what resolution the request is based on. Helps eliminate stale
532 // data, race conditions.
533 virtual void OveruseDetected() OVERRIDE {
534 talk_base::CritScope cs(&crit_);
535 if (!enabled_) {
536 return;
537 }
538
539 video_adapter_->OnCpuResolutionRequest(CoordinatedVideoAdapter::DOWNGRADE);
540 }
541
542 virtual void NormalUsage() OVERRIDE {
543 talk_base::CritScope cs(&crit_);
544 if (!enabled_) {
545 return;
546 }
547
548 video_adapter_->OnCpuResolutionRequest(CoordinatedVideoAdapter::UPGRADE);
549 }
550
551 void Enable(bool enable) {
552 talk_base::CritScope cs(&crit_);
553 enabled_ = enable;
554 }
555
556 private:
557 CoordinatedVideoAdapter* video_adapter_;
558 bool enabled_;
559 talk_base::CriticalSection crit_;
560};
561
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000562
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000563class WebRtcVideoChannelSendInfo : public sigslot::has_slots<> {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000564 public:
565 typedef std::map<int, webrtc::VideoEncoder*> EncoderMap; // key: payload type
566 WebRtcVideoChannelSendInfo(int channel_id, int capture_id,
567 webrtc::ViEExternalCapture* external_capture,
568 talk_base::CpuMonitor* cpu_monitor)
569 : channel_id_(channel_id),
570 capture_id_(capture_id),
571 sending_(false),
572 muted_(false),
573 video_capturer_(NULL),
574 encoder_observer_(channel_id),
575 external_capture_(external_capture),
576 capturer_updated_(false),
577 interval_(0),
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000578 video_adapter_(new CoordinatedVideoAdapter),
579 cpu_monitor_(cpu_monitor) {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000580 overuse_observer_.reset(new WebRtcOveruseObserver(video_adapter_.get()));
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000581 SignalCpuAdaptationUnable.repeat(video_adapter_->SignalCpuAdaptationUnable);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000582 if (cpu_monitor) {
583 cpu_monitor->SignalUpdate.connect(
584 video_adapter_.get(), &CoordinatedVideoAdapter::OnCpuLoadUpdated);
585 }
586 }
587
588 int channel_id() const { return channel_id_; }
589 int capture_id() const { return capture_id_; }
590 void set_sending(bool sending) { sending_ = sending; }
591 bool sending() const { return sending_; }
592 void set_muted(bool on) {
593 // TODO(asapersson): add support.
594 // video_adapter_->SetBlackOutput(on);
595 muted_ = on;
596 }
597 bool muted() {return muted_; }
598
599 WebRtcEncoderObserver* encoder_observer() { return &encoder_observer_; }
600 webrtc::ViEExternalCapture* external_capture() { return external_capture_; }
601 const VideoFormat& video_format() const {
602 return video_format_;
603 }
604 void set_video_format(const VideoFormat& video_format) {
605 video_format_ = video_format;
606 if (video_format_ != cricket::VideoFormat()) {
607 interval_ = video_format_.interval;
608 }
609 video_adapter_->OnOutputFormatRequest(video_format_);
610 }
611 void set_interval(int64 interval) {
612 if (video_format() == cricket::VideoFormat()) {
613 interval_ = interval;
614 }
615 }
616 int64 interval() { return interval_; }
617
618 void InitializeAdapterOutputFormat(const webrtc::VideoCodec& codec) {
619 VideoFormat format(codec.width, codec.height,
620 VideoFormat::FpsToInterval(codec.maxFramerate),
621 FOURCC_I420);
622 if (video_adapter_->output_format().IsSize0x0()) {
623 video_adapter_->SetOutputFormat(format);
624 }
625 }
626
627 bool AdaptFrame(const VideoFrame* in_frame, const VideoFrame** out_frame) {
628 *out_frame = NULL;
629 return video_adapter_->AdaptFrame(in_frame, out_frame);
630 }
631 int CurrentAdaptReason() const {
632 return video_adapter_->adapt_reason();
633 }
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000634 webrtc::CpuOveruseObserver* overuse_observer() {
635 return overuse_observer_.get();
636 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000637
638 StreamParams* stream_params() { return stream_params_.get(); }
639 void set_stream_params(const StreamParams& sp) {
640 stream_params_.reset(new StreamParams(sp));
641 }
642 void ClearStreamParams() { stream_params_.reset(); }
643 bool has_ssrc(uint32 local_ssrc) const {
644 return !stream_params_ ? false :
645 stream_params_->has_ssrc(local_ssrc);
646 }
647 WebRtcLocalStreamInfo* local_stream_info() {
648 return &local_stream_info_;
649 }
650 VideoCapturer* video_capturer() {
651 return video_capturer_;
652 }
653 void set_video_capturer(VideoCapturer* video_capturer) {
654 if (video_capturer == video_capturer_) {
655 return;
656 }
657 capturer_updated_ = true;
658 video_capturer_ = video_capturer;
659 if (video_capturer && !video_capturer->IsScreencast()) {
660 const VideoFormat* capture_format = video_capturer->GetCaptureFormat();
661 if (capture_format) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000662 // TODO(thorcarpenter): This is broken. Video capturer doesn't have
663 // a capture format until the capturer is started. So, if
664 // the capturer is started immediately after calling set_video_capturer
665 // video adapter may not have the input format set, the interval may
666 // be zero, and all frames may be dropped.
667 // Consider fixing this by having video_adapter keep a pointer to the
668 // video capturer.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000669 video_adapter_->SetInputFormat(*capture_format);
670 }
671 }
672 }
673
674 void ApplyCpuOptions(const VideoOptions& options) {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000675 bool cpu_adapt, cpu_smoothing, adapt_third;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000676 float low, med, high;
677 if (options.adapt_input_to_cpu_usage.Get(&cpu_adapt)) {
678 video_adapter_->set_cpu_adaptation(cpu_adapt);
679 }
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000680 if (options.adapt_cpu_with_smoothing.Get(&cpu_smoothing)) {
681 video_adapter_->set_cpu_smoothing(cpu_smoothing);
682 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000683 if (options.process_adaptation_threshhold.Get(&med)) {
684 video_adapter_->set_process_threshold(med);
685 }
686 if (options.system_low_adaptation_threshhold.Get(&low)) {
687 video_adapter_->set_low_system_threshold(low);
688 }
689 if (options.system_high_adaptation_threshhold.Get(&high)) {
690 video_adapter_->set_high_system_threshold(high);
691 }
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000692 if (options.video_adapt_third.Get(&adapt_third)) {
693 video_adapter_->set_scale_third(adapt_third);
694 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000695 }
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000696
697 void SetCpuOveruseDetection(bool enable) {
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000698 if (cpu_monitor_ && enable) {
699 cpu_monitor_->SignalUpdate.disconnect(video_adapter_.get());
700 }
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000701 overuse_observer_->Enable(enable);
702 video_adapter_->set_cpu_adaptation(enable);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000703 }
704
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000705 void ProcessFrame(const VideoFrame& original_frame, bool mute,
706 VideoFrame** processed_frame) {
707 if (!mute) {
708 *processed_frame = original_frame.Copy();
709 } else {
710 WebRtcVideoFrame* black_frame = new WebRtcVideoFrame();
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000711 black_frame->InitToBlack(static_cast<int>(original_frame.GetWidth()),
712 static_cast<int>(original_frame.GetHeight()),
713 1, 1,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000714 original_frame.GetElapsedTime(),
715 original_frame.GetTimeStamp());
716 *processed_frame = black_frame;
717 }
718 local_stream_info_.UpdateFrame(*processed_frame);
719 }
720 void RegisterEncoder(int pl_type, webrtc::VideoEncoder* encoder) {
721 ASSERT(!IsEncoderRegistered(pl_type));
722 registered_encoders_[pl_type] = encoder;
723 }
724 bool IsEncoderRegistered(int pl_type) {
725 return registered_encoders_.count(pl_type) != 0;
726 }
727 const EncoderMap& registered_encoders() {
728 return registered_encoders_;
729 }
730 void ClearRegisteredEncoders() {
731 registered_encoders_.clear();
732 }
733
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000734 sigslot::repeater0<> SignalCpuAdaptationUnable;
735
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000736 private:
737 int channel_id_;
738 int capture_id_;
739 bool sending_;
740 bool muted_;
741 VideoCapturer* video_capturer_;
742 WebRtcEncoderObserver encoder_observer_;
743 webrtc::ViEExternalCapture* external_capture_;
744 EncoderMap registered_encoders_;
745
746 VideoFormat video_format_;
747
748 talk_base::scoped_ptr<StreamParams> stream_params_;
749
750 WebRtcLocalStreamInfo local_stream_info_;
751
752 bool capturer_updated_;
753
754 int64 interval_;
755
756 talk_base::scoped_ptr<CoordinatedVideoAdapter> video_adapter_;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000757 talk_base::CpuMonitor* cpu_monitor_;
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000758 talk_base::scoped_ptr<WebRtcOveruseObserver> overuse_observer_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000759};
760
761const WebRtcVideoEngine::VideoCodecPref
762 WebRtcVideoEngine::kVideoCodecPrefs[] = {
763 {kVp8PayloadName, 100, 0},
764 {kRedPayloadName, 116, 1},
765 {kFecPayloadName, 117, 2},
766};
767
768// The formats are sorted by the descending order of width. We use the order to
769// find the next format for CPU and bandwidth adaptation.
770const VideoFormatPod WebRtcVideoEngine::kVideoFormats[] = {
771 {1280, 800, FPS_TO_INTERVAL(30), FOURCC_ANY},
772 {1280, 720, FPS_TO_INTERVAL(30), FOURCC_ANY},
773 {960, 600, FPS_TO_INTERVAL(30), FOURCC_ANY},
774 {960, 540, FPS_TO_INTERVAL(30), FOURCC_ANY},
775 {640, 400, FPS_TO_INTERVAL(30), FOURCC_ANY},
776 {640, 360, FPS_TO_INTERVAL(30), FOURCC_ANY},
777 {640, 480, FPS_TO_INTERVAL(30), FOURCC_ANY},
778 {480, 300, FPS_TO_INTERVAL(30), FOURCC_ANY},
779 {480, 270, FPS_TO_INTERVAL(30), FOURCC_ANY},
780 {480, 360, FPS_TO_INTERVAL(30), FOURCC_ANY},
781 {320, 200, FPS_TO_INTERVAL(30), FOURCC_ANY},
782 {320, 180, FPS_TO_INTERVAL(30), FOURCC_ANY},
783 {320, 240, FPS_TO_INTERVAL(30), FOURCC_ANY},
784 {240, 150, FPS_TO_INTERVAL(30), FOURCC_ANY},
785 {240, 135, FPS_TO_INTERVAL(30), FOURCC_ANY},
786 {240, 180, FPS_TO_INTERVAL(30), FOURCC_ANY},
787 {160, 100, FPS_TO_INTERVAL(30), FOURCC_ANY},
788 {160, 90, FPS_TO_INTERVAL(30), FOURCC_ANY},
789 {160, 120, FPS_TO_INTERVAL(30), FOURCC_ANY},
790};
791
792const VideoFormatPod WebRtcVideoEngine::kDefaultVideoFormat =
793 {640, 400, FPS_TO_INTERVAL(30), FOURCC_ANY};
794
795static void UpdateVideoCodec(const cricket::VideoFormat& video_format,
796 webrtc::VideoCodec* target_codec) {
797 if ((target_codec == NULL) || (video_format == cricket::VideoFormat())) {
798 return;
799 }
800 target_codec->width = video_format.width;
801 target_codec->height = video_format.height;
802 target_codec->maxFramerate = cricket::VideoFormat::IntervalToFps(
803 video_format.interval);
804}
805
806WebRtcVideoEngine::WebRtcVideoEngine() {
807 Construct(new ViEWrapper(), new ViETraceWrapper(), NULL,
808 new talk_base::CpuMonitor(NULL));
809}
810
811WebRtcVideoEngine::WebRtcVideoEngine(WebRtcVoiceEngine* voice_engine,
812 ViEWrapper* vie_wrapper,
813 talk_base::CpuMonitor* cpu_monitor) {
814 Construct(vie_wrapper, new ViETraceWrapper(), voice_engine, cpu_monitor);
815}
816
817WebRtcVideoEngine::WebRtcVideoEngine(WebRtcVoiceEngine* voice_engine,
818 ViEWrapper* vie_wrapper,
819 ViETraceWrapper* tracing,
820 talk_base::CpuMonitor* cpu_monitor) {
821 Construct(vie_wrapper, tracing, voice_engine, cpu_monitor);
822}
823
824void WebRtcVideoEngine::Construct(ViEWrapper* vie_wrapper,
825 ViETraceWrapper* tracing,
826 WebRtcVoiceEngine* voice_engine,
827 talk_base::CpuMonitor* cpu_monitor) {
828 LOG(LS_INFO) << "WebRtcVideoEngine::WebRtcVideoEngine";
829 worker_thread_ = NULL;
830 vie_wrapper_.reset(vie_wrapper);
831 vie_wrapper_base_initialized_ = false;
832 tracing_.reset(tracing);
833 voice_engine_ = voice_engine;
834 initialized_ = false;
835 SetTraceFilter(SeverityToFilter(kDefaultLogSeverity));
836 render_module_.reset(new WebRtcPassthroughRender());
837 local_renderer_w_ = local_renderer_h_ = 0;
838 local_renderer_ = NULL;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000839 capture_started_ = false;
840 decoder_factory_ = NULL;
841 encoder_factory_ = NULL;
842 cpu_monitor_.reset(cpu_monitor);
843
844 SetTraceOptions("");
845 if (tracing_->SetTraceCallback(this) != 0) {
846 LOG_RTCERR1(SetTraceCallback, this);
847 }
848
849 // Set default quality levels for our supported codecs. We override them here
850 // if we know your cpu performance is low, and they can be updated explicitly
851 // by calling SetDefaultCodec. For example by a flute preference setting, or
852 // by the server with a jec in response to our reported system info.
853 VideoCodec max_codec(kVideoCodecPrefs[0].payload_type,
854 kVideoCodecPrefs[0].name,
855 kDefaultVideoFormat.width,
856 kDefaultVideoFormat.height,
857 VideoFormat::IntervalToFps(kDefaultVideoFormat.interval),
858 0);
859 if (!SetDefaultCodec(max_codec)) {
860 LOG(LS_ERROR) << "Failed to initialize list of supported codec types";
861 }
862
863
864 // Load our RTP Header extensions.
865 rtp_header_extensions_.push_back(
866 RtpHeaderExtension(kRtpTimestampOffsetHeaderExtension,
867 kRtpTimeOffsetExtensionId));
868 rtp_header_extensions_.push_back(
869 RtpHeaderExtension(kRtpAbsoluteSendTimeHeaderExtension,
870 kRtpAbsoluteSendTimeExtensionId));
871}
872
873WebRtcVideoEngine::~WebRtcVideoEngine() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000874 LOG(LS_INFO) << "WebRtcVideoEngine::~WebRtcVideoEngine";
875 if (initialized_) {
876 Terminate();
877 }
878 if (encoder_factory_) {
879 encoder_factory_->RemoveObserver(this);
880 }
881 tracing_->SetTraceCallback(NULL);
882 // Test to see if the media processor was deregistered properly.
883 ASSERT(SignalMediaFrame.is_empty());
884}
885
886bool WebRtcVideoEngine::Init(talk_base::Thread* worker_thread) {
887 LOG(LS_INFO) << "WebRtcVideoEngine::Init";
888 worker_thread_ = worker_thread;
889 ASSERT(worker_thread_ != NULL);
890
891 cpu_monitor_->set_thread(worker_thread_);
892 if (!cpu_monitor_->Start(kCpuMonitorPeriodMs)) {
893 LOG(LS_ERROR) << "Failed to start CPU monitor.";
894 cpu_monitor_.reset();
895 }
896
897 bool result = InitVideoEngine();
898 if (result) {
899 LOG(LS_INFO) << "VideoEngine Init done";
900 } else {
901 LOG(LS_ERROR) << "VideoEngine Init failed, releasing";
902 Terminate();
903 }
904 return result;
905}
906
907bool WebRtcVideoEngine::InitVideoEngine() {
908 LOG(LS_INFO) << "WebRtcVideoEngine::InitVideoEngine";
909
910 // Init WebRTC VideoEngine.
911 if (!vie_wrapper_base_initialized_) {
912 if (vie_wrapper_->base()->Init() != 0) {
913 LOG_RTCERR0(Init);
914 return false;
915 }
916 vie_wrapper_base_initialized_ = true;
917 }
918
919 // Log the VoiceEngine version info.
920 char buffer[1024] = "";
921 if (vie_wrapper_->base()->GetVersion(buffer) != 0) {
922 LOG_RTCERR0(GetVersion);
923 return false;
924 }
925
926 LOG(LS_INFO) << "WebRtc VideoEngine Version:";
927 LogMultiline(talk_base::LS_INFO, buffer);
928
929 // Hook up to VoiceEngine for sync purposes, if supplied.
930 if (!voice_engine_) {
931 LOG(LS_WARNING) << "NULL voice engine";
932 } else if ((vie_wrapper_->base()->SetVoiceEngine(
933 voice_engine_->voe()->engine())) != 0) {
934 LOG_RTCERR0(SetVoiceEngine);
935 return false;
936 }
937
938 // Register our custom render module.
939 if (vie_wrapper_->render()->RegisterVideoRenderModule(
940 *render_module_.get()) != 0) {
941 LOG_RTCERR0(RegisterVideoRenderModule);
942 return false;
943 }
944
945 initialized_ = true;
946 return true;
947}
948
949void WebRtcVideoEngine::Terminate() {
950 LOG(LS_INFO) << "WebRtcVideoEngine::Terminate";
951 initialized_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000952
953 if (vie_wrapper_->render()->DeRegisterVideoRenderModule(
954 *render_module_.get()) != 0) {
955 LOG_RTCERR0(DeRegisterVideoRenderModule);
956 }
957
958 if (vie_wrapper_->base()->SetVoiceEngine(NULL) != 0) {
959 LOG_RTCERR0(SetVoiceEngine);
960 }
961
962 cpu_monitor_->Stop();
963}
964
965int WebRtcVideoEngine::GetCapabilities() {
966 return VIDEO_RECV | VIDEO_SEND;
967}
968
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000969bool WebRtcVideoEngine::SetOptions(const VideoOptions &options) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000970 return true;
971}
972
973bool WebRtcVideoEngine::SetDefaultEncoderConfig(
974 const VideoEncoderConfig& config) {
975 return SetDefaultCodec(config.max_codec);
976}
977
wu@webrtc.org78187522013-10-07 23:32:02 +0000978VideoEncoderConfig WebRtcVideoEngine::GetDefaultEncoderConfig() const {
979 ASSERT(!video_codecs_.empty());
980 VideoCodec max_codec(kVideoCodecPrefs[0].payload_type,
981 kVideoCodecPrefs[0].name,
982 video_codecs_[0].width,
983 video_codecs_[0].height,
984 video_codecs_[0].framerate,
985 0);
986 return VideoEncoderConfig(max_codec);
987}
988
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000989// SetDefaultCodec may be called while the capturer is running. For example, a
990// test call is started in a page with QVGA default codec, and then a real call
991// is started in another page with VGA default codec. This is the corner case
992// and happens only when a session is started. We ignore this case currently.
993bool WebRtcVideoEngine::SetDefaultCodec(const VideoCodec& codec) {
994 if (!RebuildCodecList(codec)) {
995 LOG(LS_WARNING) << "Failed to RebuildCodecList";
996 return false;
997 }
998
wu@webrtc.org78187522013-10-07 23:32:02 +0000999 ASSERT(!video_codecs_.empty());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001000 default_codec_format_ = VideoFormat(
1001 video_codecs_[0].width,
1002 video_codecs_[0].height,
1003 VideoFormat::FpsToInterval(video_codecs_[0].framerate),
1004 FOURCC_ANY);
1005 return true;
1006}
1007
1008WebRtcVideoMediaChannel* WebRtcVideoEngine::CreateChannel(
1009 VoiceMediaChannel* voice_channel) {
1010 WebRtcVideoMediaChannel* channel =
1011 new WebRtcVideoMediaChannel(this, voice_channel);
1012 if (!channel->Init()) {
1013 delete channel;
1014 channel = NULL;
1015 }
1016 return channel;
1017}
1018
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001019bool WebRtcVideoEngine::SetLocalRenderer(VideoRenderer* renderer) {
1020 local_renderer_w_ = local_renderer_h_ = 0;
1021 local_renderer_ = renderer;
1022 return true;
1023}
1024
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001025const std::vector<VideoCodec>& WebRtcVideoEngine::codecs() const {
1026 return video_codecs_;
1027}
1028
1029const std::vector<RtpHeaderExtension>&
1030WebRtcVideoEngine::rtp_header_extensions() const {
1031 return rtp_header_extensions_;
1032}
1033
1034void WebRtcVideoEngine::SetLogging(int min_sev, const char* filter) {
1035 // if min_sev == -1, we keep the current log level.
1036 if (min_sev >= 0) {
1037 SetTraceFilter(SeverityToFilter(min_sev));
1038 }
1039 SetTraceOptions(filter);
1040}
1041
1042int WebRtcVideoEngine::GetLastEngineError() {
1043 return vie_wrapper_->error();
1044}
1045
1046// Checks to see whether we comprehend and could receive a particular codec
1047bool WebRtcVideoEngine::FindCodec(const VideoCodec& in) {
1048 for (int i = 0; i < ARRAY_SIZE(kVideoFormats); ++i) {
1049 const VideoFormat fmt(kVideoFormats[i]);
1050 if ((in.width == 0 && in.height == 0) ||
1051 (fmt.width == in.width && fmt.height == in.height)) {
1052 if (encoder_factory_) {
1053 const std::vector<WebRtcVideoEncoderFactory::VideoCodec>& codecs =
1054 encoder_factory_->codecs();
1055 for (size_t j = 0; j < codecs.size(); ++j) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001056 VideoCodec codec(GetExternalVideoPayloadType(static_cast<int>(j)),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001057 codecs[j].name, 0, 0, 0, 0);
1058 if (codec.Matches(in))
1059 return true;
1060 }
1061 }
1062 for (size_t j = 0; j < ARRAY_SIZE(kVideoCodecPrefs); ++j) {
1063 VideoCodec codec(kVideoCodecPrefs[j].payload_type,
1064 kVideoCodecPrefs[j].name, 0, 0, 0, 0);
1065 if (codec.Matches(in)) {
1066 return true;
1067 }
1068 }
1069 }
1070 }
1071 return false;
1072}
1073
1074// Given the requested codec, returns true if we can send that codec type and
1075// updates out with the best quality we could send for that codec. If current is
1076// not empty, we constrain out so that its aspect ratio matches current's.
1077bool WebRtcVideoEngine::CanSendCodec(const VideoCodec& requested,
1078 const VideoCodec& current,
1079 VideoCodec* out) {
1080 if (!out) {
1081 return false;
1082 }
1083
1084 std::vector<VideoCodec>::const_iterator local_max;
1085 for (local_max = video_codecs_.begin();
1086 local_max < video_codecs_.end();
1087 ++local_max) {
1088 // First match codecs by payload type
1089 if (!requested.Matches(*local_max)) {
1090 continue;
1091 }
1092
1093 out->id = requested.id;
1094 out->name = requested.name;
1095 out->preference = requested.preference;
1096 out->params = requested.params;
1097 out->framerate = talk_base::_min(requested.framerate, local_max->framerate);
1098 out->width = 0;
1099 out->height = 0;
1100 out->params = requested.params;
1101 out->feedback_params = requested.feedback_params;
1102
1103 if (0 == requested.width && 0 == requested.height) {
1104 // Special case with resolution 0. The channel should not send frames.
1105 return true;
1106 } else if (0 == requested.width || 0 == requested.height) {
1107 // 0xn and nx0 are invalid resolutions.
1108 return false;
1109 }
1110
1111 // Pick the best quality that is within their and our bounds and has the
1112 // correct aspect ratio.
1113 for (int j = 0; j < ARRAY_SIZE(kVideoFormats); ++j) {
1114 const VideoFormat format(kVideoFormats[j]);
1115
1116 // Skip any format that is larger than the local or remote maximums, or
1117 // smaller than the current best match
1118 if (format.width > requested.width || format.height > requested.height ||
1119 format.width > local_max->width ||
1120 (format.width < out->width && format.height < out->height)) {
1121 continue;
1122 }
1123
1124 bool better = false;
1125
1126 // Check any further constraints on this prospective format
1127 if (!out->width || !out->height) {
1128 // If we don't have any matches yet, this is the best so far.
1129 better = true;
1130 } else if (current.width && current.height) {
1131 // current is set so format must match its ratio exactly.
1132 better =
1133 (format.width * current.height == format.height * current.width);
1134 } else {
1135 // Prefer closer aspect ratios i.e
1136 // format.aspect - requested.aspect < out.aspect - requested.aspect
1137 better = abs(format.width * requested.height * out->height -
1138 requested.width * format.height * out->height) <
1139 abs(out->width * format.height * requested.height -
1140 requested.width * format.height * out->height);
1141 }
1142
1143 if (better) {
1144 out->width = format.width;
1145 out->height = format.height;
1146 }
1147 }
1148 if (out->width > 0) {
1149 return true;
1150 }
1151 }
1152 return false;
1153}
1154
1155static void ConvertToCricketVideoCodec(
1156 const webrtc::VideoCodec& in_codec, VideoCodec* out_codec) {
1157 out_codec->id = in_codec.plType;
1158 out_codec->name = in_codec.plName;
1159 out_codec->width = in_codec.width;
1160 out_codec->height = in_codec.height;
1161 out_codec->framerate = in_codec.maxFramerate;
1162 out_codec->SetParam(kCodecParamMinBitrate, in_codec.minBitrate);
1163 out_codec->SetParam(kCodecParamMaxBitrate, in_codec.maxBitrate);
1164 if (in_codec.qpMax) {
1165 out_codec->SetParam(kCodecParamMaxQuantization, in_codec.qpMax);
1166 }
1167}
1168
1169bool WebRtcVideoEngine::ConvertFromCricketVideoCodec(
1170 const VideoCodec& in_codec, webrtc::VideoCodec* out_codec) {
1171 bool found = false;
1172 int ncodecs = vie_wrapper_->codec()->NumberOfCodecs();
1173 for (int i = 0; i < ncodecs; ++i) {
1174 if (vie_wrapper_->codec()->GetCodec(i, *out_codec) == 0 &&
1175 _stricmp(in_codec.name.c_str(), out_codec->plName) == 0) {
1176 found = true;
1177 break;
1178 }
1179 }
1180
1181 // If not found, check if this is supported by external encoder factory.
1182 if (!found && encoder_factory_) {
1183 const std::vector<WebRtcVideoEncoderFactory::VideoCodec>& codecs =
1184 encoder_factory_->codecs();
1185 for (size_t i = 0; i < codecs.size(); ++i) {
1186 if (_stricmp(in_codec.name.c_str(), codecs[i].name.c_str()) == 0) {
1187 out_codec->codecType = codecs[i].type;
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001188 out_codec->plType = GetExternalVideoPayloadType(static_cast<int>(i));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001189 talk_base::strcpyn(out_codec->plName, sizeof(out_codec->plName),
1190 codecs[i].name.c_str(), codecs[i].name.length());
1191 found = true;
1192 break;
1193 }
1194 }
1195 }
1196
1197 if (!found) {
1198 LOG(LS_ERROR) << "invalid codec type";
1199 return false;
1200 }
1201
1202 if (in_codec.id != 0)
1203 out_codec->plType = in_codec.id;
1204
1205 if (in_codec.width != 0)
1206 out_codec->width = in_codec.width;
1207
1208 if (in_codec.height != 0)
1209 out_codec->height = in_codec.height;
1210
1211 if (in_codec.framerate != 0)
1212 out_codec->maxFramerate = in_codec.framerate;
1213
1214 // Convert bitrate parameters.
1215 int max_bitrate = kMaxVideoBitrate;
1216 int min_bitrate = kMinVideoBitrate;
1217 int start_bitrate = kStartVideoBitrate;
1218
1219 in_codec.GetParam(kCodecParamMinBitrate, &min_bitrate);
1220 in_codec.GetParam(kCodecParamMaxBitrate, &max_bitrate);
1221
1222 if (max_bitrate < min_bitrate) {
1223 return false;
1224 }
1225 start_bitrate = talk_base::_max(start_bitrate, min_bitrate);
1226 start_bitrate = talk_base::_min(start_bitrate, max_bitrate);
1227
1228 out_codec->minBitrate = min_bitrate;
1229 out_codec->startBitrate = start_bitrate;
1230 out_codec->maxBitrate = max_bitrate;
1231
1232 // Convert general codec parameters.
1233 int max_quantization = 0;
1234 if (in_codec.GetParam(kCodecParamMaxQuantization, &max_quantization)) {
1235 if (max_quantization < 0) {
1236 return false;
1237 }
1238 out_codec->qpMax = max_quantization;
1239 }
1240 return true;
1241}
1242
1243void WebRtcVideoEngine::RegisterChannel(WebRtcVideoMediaChannel *channel) {
1244 talk_base::CritScope cs(&channels_crit_);
1245 channels_.push_back(channel);
1246}
1247
1248void WebRtcVideoEngine::UnregisterChannel(WebRtcVideoMediaChannel *channel) {
1249 talk_base::CritScope cs(&channels_crit_);
1250 channels_.erase(std::remove(channels_.begin(), channels_.end(), channel),
1251 channels_.end());
1252}
1253
1254bool WebRtcVideoEngine::SetVoiceEngine(WebRtcVoiceEngine* voice_engine) {
1255 if (initialized_) {
1256 LOG(LS_WARNING) << "SetVoiceEngine can not be called after Init";
1257 return false;
1258 }
1259 voice_engine_ = voice_engine;
1260 return true;
1261}
1262
1263bool WebRtcVideoEngine::EnableTimedRender() {
1264 if (initialized_) {
1265 LOG(LS_WARNING) << "EnableTimedRender can not be called after Init";
1266 return false;
1267 }
1268 render_module_.reset(webrtc::VideoRender::CreateVideoRender(0, NULL,
1269 false, webrtc::kRenderExternal));
1270 return true;
1271}
1272
1273void WebRtcVideoEngine::SetTraceFilter(int filter) {
1274 tracing_->SetTraceFilter(filter);
1275}
1276
1277// See https://sites.google.com/a/google.com/wavelet/
1278// Home/Magic-Flute--RTC-Engine-/Magic-Flute-Command-Line-Parameters
1279// for all supported command line setttings.
1280void WebRtcVideoEngine::SetTraceOptions(const std::string& options) {
1281 // Set WebRTC trace file.
1282 std::vector<std::string> opts;
1283 talk_base::tokenize(options, ' ', '"', '"', &opts);
1284 std::vector<std::string>::iterator tracefile =
1285 std::find(opts.begin(), opts.end(), "tracefile");
1286 if (tracefile != opts.end() && ++tracefile != opts.end()) {
1287 // Write WebRTC debug output (at same loglevel) to file
1288 if (tracing_->SetTraceFile(tracefile->c_str()) == -1) {
1289 LOG_RTCERR1(SetTraceFile, *tracefile);
1290 }
1291 }
1292}
1293
1294static void AddDefaultFeedbackParams(VideoCodec* codec) {
1295 const FeedbackParam kFir(kRtcpFbParamCcm, kRtcpFbCcmParamFir);
1296 codec->AddFeedbackParam(kFir);
1297 const FeedbackParam kNack(kRtcpFbParamNack, kParamValueEmpty);
1298 codec->AddFeedbackParam(kNack);
1299 const FeedbackParam kRemb(kRtcpFbParamRemb, kParamValueEmpty);
1300 codec->AddFeedbackParam(kRemb);
1301}
1302
1303// Rebuilds the codec list to be only those that are less intensive
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00001304// than the specified codec. Prefers internal codec over external with
1305// higher preference field.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001306bool WebRtcVideoEngine::RebuildCodecList(const VideoCodec& in_codec) {
1307 if (!FindCodec(in_codec))
1308 return false;
1309
1310 video_codecs_.clear();
1311
1312 bool found = false;
mallinath@webrtc.org1112c302013-09-23 20:34:45 +00001313 std::set<std::string> internal_codec_names;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001314 for (size_t i = 0; i < ARRAY_SIZE(kVideoCodecPrefs); ++i) {
1315 const VideoCodecPref& pref(kVideoCodecPrefs[i]);
1316 if (!found)
1317 found = (in_codec.name == pref.name);
mallinath@webrtc.org1112c302013-09-23 20:34:45 +00001318 if (found) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001319 VideoCodec codec(pref.payload_type, pref.name,
1320 in_codec.width, in_codec.height, in_codec.framerate,
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001321 static_cast<int>(ARRAY_SIZE(kVideoCodecPrefs) - i));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001322 if (_stricmp(kVp8PayloadName, codec.name.c_str()) == 0) {
1323 AddDefaultFeedbackParams(&codec);
1324 }
1325 video_codecs_.push_back(codec);
mallinath@webrtc.org1112c302013-09-23 20:34:45 +00001326 internal_codec_names.insert(codec.name);
1327 }
1328 }
1329 if (encoder_factory_) {
1330 const std::vector<WebRtcVideoEncoderFactory::VideoCodec>& codecs =
1331 encoder_factory_->codecs();
1332 for (size_t i = 0; i < codecs.size(); ++i) {
1333 bool is_internal_codec = internal_codec_names.find(codecs[i].name) !=
1334 internal_codec_names.end();
1335 if (!is_internal_codec) {
1336 if (!found)
1337 found = (in_codec.name == codecs[i].name);
1338 VideoCodec codec(
1339 GetExternalVideoPayloadType(static_cast<int>(i)),
1340 codecs[i].name,
1341 codecs[i].max_width,
1342 codecs[i].max_height,
1343 codecs[i].max_fps,
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00001344 // Use negative preference on external codec to ensure the internal
1345 // codec is preferred.
1346 static_cast<int>(0 - i));
mallinath@webrtc.org1112c302013-09-23 20:34:45 +00001347 AddDefaultFeedbackParams(&codec);
1348 video_codecs_.push_back(codec);
1349 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001350 }
1351 }
1352 ASSERT(found);
1353 return true;
1354}
1355
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001356// Ignore spammy trace messages, mostly from the stats API when we haven't
1357// gotten RTCP info yet from the remote side.
1358bool WebRtcVideoEngine::ShouldIgnoreTrace(const std::string& trace) {
1359 static const char* const kTracesToIgnore[] = {
1360 NULL
1361 };
1362 for (const char* const* p = kTracesToIgnore; *p; ++p) {
1363 if (trace.find(*p) == 0) {
1364 return true;
1365 }
1366 }
1367 return false;
1368}
1369
1370int WebRtcVideoEngine::GetNumOfChannels() {
1371 talk_base::CritScope cs(&channels_crit_);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001372 return static_cast<int>(channels_.size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001373}
1374
1375void WebRtcVideoEngine::Print(webrtc::TraceLevel level, const char* trace,
1376 int length) {
1377 talk_base::LoggingSeverity sev = talk_base::LS_VERBOSE;
1378 if (level == webrtc::kTraceError || level == webrtc::kTraceCritical)
1379 sev = talk_base::LS_ERROR;
1380 else if (level == webrtc::kTraceWarning)
1381 sev = talk_base::LS_WARNING;
1382 else if (level == webrtc::kTraceStateInfo || level == webrtc::kTraceInfo)
1383 sev = talk_base::LS_INFO;
1384 else if (level == webrtc::kTraceTerseInfo)
1385 sev = talk_base::LS_INFO;
1386
1387 // Skip past boilerplate prefix text
1388 if (length < 72) {
1389 std::string msg(trace, length);
1390 LOG(LS_ERROR) << "Malformed webrtc log message: ";
1391 LOG_V(sev) << msg;
1392 } else {
1393 std::string msg(trace + 71, length - 72);
1394 if (!ShouldIgnoreTrace(msg) &&
1395 (!voice_engine_ || !voice_engine_->ShouldIgnoreTrace(msg))) {
1396 LOG_V(sev) << "webrtc: " << msg;
1397 }
1398 }
1399}
1400
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001401webrtc::VideoDecoder* WebRtcVideoEngine::CreateExternalDecoder(
1402 webrtc::VideoCodecType type) {
1403 if (decoder_factory_ == NULL) {
1404 return NULL;
1405 }
1406 return decoder_factory_->CreateVideoDecoder(type);
1407}
1408
1409void WebRtcVideoEngine::DestroyExternalDecoder(webrtc::VideoDecoder* decoder) {
1410 ASSERT(decoder_factory_ != NULL);
1411 if (decoder_factory_ == NULL)
1412 return;
1413 decoder_factory_->DestroyVideoDecoder(decoder);
1414}
1415
1416webrtc::VideoEncoder* WebRtcVideoEngine::CreateExternalEncoder(
1417 webrtc::VideoCodecType type) {
1418 if (encoder_factory_ == NULL) {
1419 return NULL;
1420 }
1421 return encoder_factory_->CreateVideoEncoder(type);
1422}
1423
1424void WebRtcVideoEngine::DestroyExternalEncoder(webrtc::VideoEncoder* encoder) {
1425 ASSERT(encoder_factory_ != NULL);
1426 if (encoder_factory_ == NULL)
1427 return;
1428 encoder_factory_->DestroyVideoEncoder(encoder);
1429}
1430
1431bool WebRtcVideoEngine::IsExternalEncoderCodecType(
1432 webrtc::VideoCodecType type) const {
1433 if (!encoder_factory_)
1434 return false;
1435 const std::vector<WebRtcVideoEncoderFactory::VideoCodec>& codecs =
1436 encoder_factory_->codecs();
1437 std::vector<WebRtcVideoEncoderFactory::VideoCodec>::const_iterator it;
1438 for (it = codecs.begin(); it != codecs.end(); ++it) {
1439 if (it->type == type)
1440 return true;
1441 }
1442 return false;
1443}
1444
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001445void WebRtcVideoEngine::SetExternalDecoderFactory(
1446 WebRtcVideoDecoderFactory* decoder_factory) {
1447 decoder_factory_ = decoder_factory;
1448}
1449
1450void WebRtcVideoEngine::SetExternalEncoderFactory(
1451 WebRtcVideoEncoderFactory* encoder_factory) {
1452 if (encoder_factory_ == encoder_factory)
1453 return;
1454
1455 if (encoder_factory_) {
1456 encoder_factory_->RemoveObserver(this);
1457 }
1458 encoder_factory_ = encoder_factory;
1459 if (encoder_factory_) {
1460 encoder_factory_->AddObserver(this);
1461 }
1462
1463 // Invoke OnCodecAvailable() here in case the list of codecs is already
1464 // available when the encoder factory is installed. If not the encoder
1465 // factory will invoke the callback later when the codecs become available.
1466 OnCodecsAvailable();
1467}
1468
1469void WebRtcVideoEngine::OnCodecsAvailable() {
1470 // Rebuild codec list while reapplying the current default codec format.
1471 VideoCodec max_codec(kVideoCodecPrefs[0].payload_type,
1472 kVideoCodecPrefs[0].name,
1473 video_codecs_[0].width,
1474 video_codecs_[0].height,
1475 video_codecs_[0].framerate,
1476 0);
1477 if (!RebuildCodecList(max_codec)) {
1478 LOG(LS_ERROR) << "Failed to initialize list of supported codec types";
1479 }
1480}
1481
1482// WebRtcVideoMediaChannel
1483
1484WebRtcVideoMediaChannel::WebRtcVideoMediaChannel(
1485 WebRtcVideoEngine* engine,
1486 VoiceMediaChannel* channel)
1487 : engine_(engine),
1488 voice_channel_(channel),
1489 vie_channel_(-1),
1490 nack_enabled_(true),
1491 remb_enabled_(false),
1492 render_started_(false),
1493 first_receive_ssrc_(0),
1494 send_red_type_(-1),
1495 send_fec_type_(-1),
1496 send_min_bitrate_(kMinVideoBitrate),
1497 send_start_bitrate_(kStartVideoBitrate),
1498 send_max_bitrate_(kMaxVideoBitrate),
1499 sending_(false),
1500 ratio_w_(0),
1501 ratio_h_(0) {
1502 engine->RegisterChannel(this);
1503}
1504
1505bool WebRtcVideoMediaChannel::Init() {
1506 const uint32 ssrc_key = 0;
1507 return CreateChannel(ssrc_key, MD_SENDRECV, &vie_channel_);
1508}
1509
1510WebRtcVideoMediaChannel::~WebRtcVideoMediaChannel() {
1511 const bool send = false;
1512 SetSend(send);
1513 const bool render = false;
1514 SetRender(render);
1515
1516 while (!send_channels_.empty()) {
1517 if (!DeleteSendChannel(send_channels_.begin()->first)) {
1518 LOG(LS_ERROR) << "Unable to delete channel with ssrc key "
1519 << send_channels_.begin()->first;
1520 ASSERT(false);
1521 break;
1522 }
1523 }
1524
1525 // Remove all receive streams and the default channel.
1526 while (!recv_channels_.empty()) {
1527 RemoveRecvStream(recv_channels_.begin()->first);
1528 }
1529
1530 // Unregister the channel from the engine.
1531 engine()->UnregisterChannel(this);
1532 if (worker_thread()) {
1533 worker_thread()->Clear(this);
1534 }
1535}
1536
1537bool WebRtcVideoMediaChannel::SetRecvCodecs(
1538 const std::vector<VideoCodec>& codecs) {
1539 receive_codecs_.clear();
1540 for (std::vector<VideoCodec>::const_iterator iter = codecs.begin();
1541 iter != codecs.end(); ++iter) {
1542 if (engine()->FindCodec(*iter)) {
1543 webrtc::VideoCodec wcodec;
1544 if (engine()->ConvertFromCricketVideoCodec(*iter, &wcodec)) {
1545 receive_codecs_.push_back(wcodec);
1546 }
1547 } else {
1548 LOG(LS_INFO) << "Unknown codec " << iter->name;
1549 return false;
1550 }
1551 }
1552
1553 for (RecvChannelMap::iterator it = recv_channels_.begin();
1554 it != recv_channels_.end(); ++it) {
1555 if (!SetReceiveCodecs(it->second))
1556 return false;
1557 }
1558 return true;
1559}
1560
1561bool WebRtcVideoMediaChannel::SetSendCodecs(
1562 const std::vector<VideoCodec>& codecs) {
1563 // Match with local video codec list.
1564 std::vector<webrtc::VideoCodec> send_codecs;
1565 VideoCodec checked_codec;
1566 VideoCodec current; // defaults to 0x0
1567 if (sending_) {
1568 ConvertToCricketVideoCodec(*send_codec_, &current);
1569 }
1570 for (std::vector<VideoCodec>::const_iterator iter = codecs.begin();
1571 iter != codecs.end(); ++iter) {
1572 if (_stricmp(iter->name.c_str(), kRedPayloadName) == 0) {
1573 send_red_type_ = iter->id;
1574 } else if (_stricmp(iter->name.c_str(), kFecPayloadName) == 0) {
1575 send_fec_type_ = iter->id;
1576 } else if (engine()->CanSendCodec(*iter, current, &checked_codec)) {
1577 webrtc::VideoCodec wcodec;
1578 if (engine()->ConvertFromCricketVideoCodec(checked_codec, &wcodec)) {
1579 if (send_codecs.empty()) {
1580 nack_enabled_ = IsNackEnabled(checked_codec);
1581 remb_enabled_ = IsRembEnabled(checked_codec);
1582 }
1583 send_codecs.push_back(wcodec);
1584 }
1585 } else {
1586 LOG(LS_WARNING) << "Unknown codec " << iter->name;
1587 }
1588 }
1589
1590 // Fail if we don't have a match.
1591 if (send_codecs.empty()) {
1592 LOG(LS_WARNING) << "No matching codecs available";
1593 return false;
1594 }
1595
1596 // Recv protection.
1597 for (RecvChannelMap::iterator it = recv_channels_.begin();
1598 it != recv_channels_.end(); ++it) {
1599 int channel_id = it->second->channel_id();
1600 if (!SetNackFec(channel_id, send_red_type_, send_fec_type_,
1601 nack_enabled_)) {
1602 return false;
1603 }
1604 if (engine_->vie()->rtp()->SetRembStatus(channel_id,
1605 kNotSending,
1606 remb_enabled_) != 0) {
1607 LOG_RTCERR3(SetRembStatus, channel_id, kNotSending, remb_enabled_);
1608 return false;
1609 }
1610 }
1611
1612 // Send settings.
1613 for (SendChannelMap::iterator iter = send_channels_.begin();
1614 iter != send_channels_.end(); ++iter) {
1615 int channel_id = iter->second->channel_id();
1616 if (!SetNackFec(channel_id, send_red_type_, send_fec_type_,
1617 nack_enabled_)) {
1618 return false;
1619 }
1620 if (engine_->vie()->rtp()->SetRembStatus(channel_id,
1621 remb_enabled_,
1622 remb_enabled_) != 0) {
1623 LOG_RTCERR3(SetRembStatus, channel_id, remb_enabled_, remb_enabled_);
1624 return false;
1625 }
1626 }
1627
1628 // Select the first matched codec.
1629 webrtc::VideoCodec& codec(send_codecs[0]);
1630
1631 if (!SetSendCodec(
1632 codec, codec.minBitrate, codec.startBitrate, codec.maxBitrate)) {
1633 return false;
1634 }
1635
1636 for (SendChannelMap::iterator iter = send_channels_.begin();
1637 iter != send_channels_.end(); ++iter) {
1638 WebRtcVideoChannelSendInfo* send_channel = iter->second;
1639 send_channel->InitializeAdapterOutputFormat(codec);
1640 }
1641
1642 LogSendCodecChange("SetSendCodecs()");
1643
1644 return true;
1645}
1646
1647bool WebRtcVideoMediaChannel::GetSendCodec(VideoCodec* send_codec) {
1648 if (!send_codec_) {
1649 return false;
1650 }
1651 ConvertToCricketVideoCodec(*send_codec_, send_codec);
1652 return true;
1653}
1654
1655bool WebRtcVideoMediaChannel::SetSendStreamFormat(uint32 ssrc,
1656 const VideoFormat& format) {
1657 if (!send_codec_) {
1658 LOG(LS_ERROR) << "The send codec has not been set yet.";
1659 return false;
1660 }
1661 WebRtcVideoChannelSendInfo* send_channel = GetSendChannel(ssrc);
1662 if (!send_channel) {
1663 LOG(LS_ERROR) << "The specified ssrc " << ssrc << " is not in use.";
1664 return false;
1665 }
1666 send_channel->set_video_format(format);
1667 return true;
1668}
1669
1670bool WebRtcVideoMediaChannel::SetRender(bool render) {
1671 if (render == render_started_) {
1672 return true; // no action required
1673 }
1674
1675 bool ret = true;
1676 for (RecvChannelMap::iterator it = recv_channels_.begin();
1677 it != recv_channels_.end(); ++it) {
1678 if (render) {
1679 if (engine()->vie()->render()->StartRender(
1680 it->second->channel_id()) != 0) {
1681 LOG_RTCERR1(StartRender, it->second->channel_id());
1682 ret = false;
1683 }
1684 } else {
1685 if (engine()->vie()->render()->StopRender(
1686 it->second->channel_id()) != 0) {
1687 LOG_RTCERR1(StopRender, it->second->channel_id());
1688 ret = false;
1689 }
1690 }
1691 }
1692 if (ret) {
1693 render_started_ = render;
1694 }
1695
1696 return ret;
1697}
1698
1699bool WebRtcVideoMediaChannel::SetSend(bool send) {
1700 if (!HasReadySendChannels() && send) {
1701 LOG(LS_ERROR) << "No stream added";
1702 return false;
1703 }
1704 if (send == sending()) {
1705 return true; // No action required.
1706 }
1707
1708 if (send) {
1709 // We've been asked to start sending.
1710 // SetSendCodecs must have been called already.
1711 if (!send_codec_) {
1712 return false;
1713 }
1714 // Start send now.
1715 if (!StartSend()) {
1716 return false;
1717 }
1718 } else {
1719 // We've been asked to stop sending.
1720 if (!StopSend()) {
1721 return false;
1722 }
1723 }
1724 sending_ = send;
1725
1726 return true;
1727}
1728
1729bool WebRtcVideoMediaChannel::AddSendStream(const StreamParams& sp) {
1730 LOG(LS_INFO) << "AddSendStream " << sp.ToString();
1731
1732 if (!IsOneSsrcStream(sp)) {
1733 LOG(LS_ERROR) << "AddSendStream: bad local stream parameters";
1734 return false;
1735 }
1736
1737 uint32 ssrc_key;
1738 if (!CreateSendChannelKey(sp.first_ssrc(), &ssrc_key)) {
1739 LOG(LS_ERROR) << "Trying to register duplicate ssrc: " << sp.first_ssrc();
1740 return false;
1741 }
1742 // If the default channel is already used for sending create a new channel
1743 // otherwise use the default channel for sending.
1744 int channel_id = -1;
1745 if (send_channels_[0]->stream_params() == NULL) {
1746 channel_id = vie_channel_;
1747 } else {
1748 if (!CreateChannel(ssrc_key, MD_SEND, &channel_id)) {
1749 LOG(LS_ERROR) << "AddSendStream: unable to create channel";
1750 return false;
1751 }
1752 }
1753 WebRtcVideoChannelSendInfo* send_channel = send_channels_[ssrc_key];
1754 // Set the send (local) SSRC.
1755 // If there are multiple send SSRCs, we can only set the first one here, and
1756 // the rest of the SSRC(s) need to be set after SetSendCodec has been called
1757 // (with a codec requires multiple SSRC(s)).
1758 if (engine()->vie()->rtp()->SetLocalSSRC(channel_id,
1759 sp.first_ssrc()) != 0) {
1760 LOG_RTCERR2(SetLocalSSRC, channel_id, sp.first_ssrc());
1761 return false;
1762 }
1763
1764 // Set RTCP CName.
1765 if (engine()->vie()->rtp()->SetRTCPCName(channel_id,
1766 sp.cname.c_str()) != 0) {
1767 LOG_RTCERR2(SetRTCPCName, channel_id, sp.cname.c_str());
1768 return false;
1769 }
1770
1771 // At this point the channel's local SSRC has been updated. If the channel is
1772 // the default channel make sure that all the receive channels are updated as
1773 // well. Receive channels have to have the same SSRC as the default channel in
1774 // order to send receiver reports with this SSRC.
1775 if (IsDefaultChannel(channel_id)) {
1776 for (RecvChannelMap::const_iterator it = recv_channels_.begin();
1777 it != recv_channels_.end(); ++it) {
1778 WebRtcVideoChannelRecvInfo* info = it->second;
1779 int channel_id = info->channel_id();
1780 if (engine()->vie()->rtp()->SetLocalSSRC(channel_id,
1781 sp.first_ssrc()) != 0) {
1782 LOG_RTCERR1(SetLocalSSRC, it->first);
1783 return false;
1784 }
1785 }
1786 }
1787
1788 send_channel->set_stream_params(sp);
1789
1790 // Reset send codec after stream parameters changed.
1791 if (send_codec_) {
1792 if (!SetSendCodec(send_channel, *send_codec_, send_min_bitrate_,
1793 send_start_bitrate_, send_max_bitrate_)) {
1794 return false;
1795 }
1796 LogSendCodecChange("SetSendStreamFormat()");
1797 }
1798
1799 if (sending_) {
1800 return StartSend(send_channel);
1801 }
1802 return true;
1803}
1804
1805bool WebRtcVideoMediaChannel::RemoveSendStream(uint32 ssrc) {
1806 uint32 ssrc_key;
1807 if (!GetSendChannelKey(ssrc, &ssrc_key)) {
1808 LOG(LS_WARNING) << "Try to remove stream with ssrc " << ssrc
1809 << " which doesn't exist.";
1810 return false;
1811 }
1812 WebRtcVideoChannelSendInfo* send_channel = send_channels_[ssrc_key];
1813 int channel_id = send_channel->channel_id();
1814 if (IsDefaultChannel(channel_id) && (send_channel->stream_params() == NULL)) {
1815 // Default channel will still exist. However, if stream_params() is NULL
1816 // there is no stream to remove.
1817 return false;
1818 }
1819 if (sending_) {
1820 StopSend(send_channel);
1821 }
1822
1823 const WebRtcVideoChannelSendInfo::EncoderMap& encoder_map =
1824 send_channel->registered_encoders();
1825 for (WebRtcVideoChannelSendInfo::EncoderMap::const_iterator it =
1826 encoder_map.begin(); it != encoder_map.end(); ++it) {
1827 if (engine()->vie()->ext_codec()->DeRegisterExternalSendCodec(
1828 channel_id, it->first) != 0) {
1829 LOG_RTCERR1(DeregisterEncoderObserver, channel_id);
1830 }
1831 engine()->DestroyExternalEncoder(it->second);
1832 }
1833 send_channel->ClearRegisteredEncoders();
1834
1835 // The receive channels depend on the default channel, recycle it instead.
1836 if (IsDefaultChannel(channel_id)) {
1837 SetCapturer(GetDefaultChannelSsrc(), NULL);
1838 send_channel->ClearStreamParams();
1839 } else {
1840 return DeleteSendChannel(ssrc_key);
1841 }
1842 return true;
1843}
1844
1845bool WebRtcVideoMediaChannel::AddRecvStream(const StreamParams& sp) {
1846 // TODO(zhurunz) Remove this once BWE works properly across different send
1847 // and receive channels.
1848 // Reuse default channel for recv stream in 1:1 call.
1849 if (!InConferenceMode() && first_receive_ssrc_ == 0) {
1850 LOG(LS_INFO) << "Recv stream " << sp.first_ssrc()
1851 << " reuse default channel #"
1852 << vie_channel_;
1853 first_receive_ssrc_ = sp.first_ssrc();
1854 if (render_started_) {
1855 if (engine()->vie()->render()->StartRender(vie_channel_) !=0) {
1856 LOG_RTCERR1(StartRender, vie_channel_);
1857 }
1858 }
1859 return true;
1860 }
1861
1862 if (recv_channels_.find(sp.first_ssrc()) != recv_channels_.end() ||
1863 first_receive_ssrc_ == sp.first_ssrc()) {
1864 LOG(LS_ERROR) << "Stream already exists";
1865 return false;
1866 }
1867
1868 // TODO(perkj): Implement recv media from multiple SSRCs per stream.
1869 if (sp.ssrcs.size() != 1) {
1870 LOG(LS_ERROR) << "WebRtcVideoMediaChannel supports one receiving SSRC per"
1871 << " stream";
1872 return false;
1873 }
1874
1875 // Create a new channel for receiving video data.
1876 // In order to get the bandwidth estimation work fine for
1877 // receive only channels, we connect all receiving channels
1878 // to our master send channel.
1879 int channel_id = -1;
1880 if (!CreateChannel(sp.first_ssrc(), MD_RECV, &channel_id)) {
1881 return false;
1882 }
1883
1884 // Get the default renderer.
1885 VideoRenderer* default_renderer = NULL;
1886 if (InConferenceMode()) {
1887 // The recv_channels_ size start out being 1, so if it is two here this
1888 // is the first receive channel created (vie_channel_ is not used for
1889 // receiving in a conference call). This means that the renderer stored
1890 // inside vie_channel_ should be used for the just created channel.
1891 if (recv_channels_.size() == 2 &&
1892 recv_channels_.find(0) != recv_channels_.end()) {
1893 GetRenderer(0, &default_renderer);
1894 }
1895 }
1896
1897 // The first recv stream reuses the default renderer (if a default renderer
1898 // has been set).
1899 if (default_renderer) {
1900 SetRenderer(sp.first_ssrc(), default_renderer);
1901 }
1902
1903 LOG(LS_INFO) << "New video stream " << sp.first_ssrc()
1904 << " registered to VideoEngine channel #"
1905 << channel_id << " and connected to channel #" << vie_channel_;
1906
1907 return true;
1908}
1909
1910bool WebRtcVideoMediaChannel::RemoveRecvStream(uint32 ssrc) {
1911 RecvChannelMap::iterator it = recv_channels_.find(ssrc);
1912
1913 if (it == recv_channels_.end()) {
1914 // TODO(perkj): Remove this once BWE works properly across different send
1915 // and receive channels.
1916 // The default channel is reused for recv stream in 1:1 call.
1917 if (first_receive_ssrc_ == ssrc) {
1918 first_receive_ssrc_ = 0;
1919 // Need to stop the renderer and remove it since the render window can be
1920 // deleted after this.
1921 if (render_started_) {
1922 if (engine()->vie()->render()->StopRender(vie_channel_) !=0) {
1923 LOG_RTCERR1(StopRender, it->second->channel_id());
1924 }
1925 }
1926 recv_channels_[0]->SetRenderer(NULL);
1927 return true;
1928 }
1929 return false;
1930 }
1931 WebRtcVideoChannelRecvInfo* info = it->second;
1932 int channel_id = info->channel_id();
1933 if (engine()->vie()->render()->RemoveRenderer(channel_id) != 0) {
1934 LOG_RTCERR1(RemoveRenderer, channel_id);
1935 }
1936
1937 if (engine()->vie()->network()->DeregisterSendTransport(channel_id) !=0) {
1938 LOG_RTCERR1(DeRegisterSendTransport, channel_id);
1939 }
1940
1941 if (engine()->vie()->codec()->DeregisterDecoderObserver(
1942 channel_id) != 0) {
1943 LOG_RTCERR1(DeregisterDecoderObserver, channel_id);
1944 }
1945
1946 const WebRtcVideoChannelRecvInfo::DecoderMap& decoder_map =
1947 info->registered_decoders();
1948 for (WebRtcVideoChannelRecvInfo::DecoderMap::const_iterator it =
1949 decoder_map.begin(); it != decoder_map.end(); ++it) {
1950 if (engine()->vie()->ext_codec()->DeRegisterExternalReceiveCodec(
1951 channel_id, it->first) != 0) {
1952 LOG_RTCERR1(DeregisterDecoderObserver, channel_id);
1953 }
1954 engine()->DestroyExternalDecoder(it->second);
1955 }
1956 info->ClearRegisteredDecoders();
1957
1958 LOG(LS_INFO) << "Removing video stream " << ssrc
1959 << " with VideoEngine channel #"
1960 << channel_id;
1961 if (engine()->vie()->base()->DeleteChannel(channel_id) == -1) {
1962 LOG_RTCERR1(DeleteChannel, channel_id);
1963 // Leak the WebRtcVideoChannelRecvInfo owned by |it| but remove the channel
1964 // from recv_channels_.
1965 recv_channels_.erase(it);
1966 return false;
1967 }
1968 // Delete the WebRtcVideoChannelRecvInfo pointed to by it->second.
1969 delete info;
1970 recv_channels_.erase(it);
1971 return true;
1972}
1973
1974bool WebRtcVideoMediaChannel::StartSend() {
1975 bool success = true;
1976 for (SendChannelMap::iterator iter = send_channels_.begin();
1977 iter != send_channels_.end(); ++iter) {
1978 WebRtcVideoChannelSendInfo* send_channel = iter->second;
1979 if (!StartSend(send_channel)) {
1980 success = false;
1981 }
1982 }
1983 return success;
1984}
1985
1986bool WebRtcVideoMediaChannel::StartSend(
1987 WebRtcVideoChannelSendInfo* send_channel) {
1988 const int channel_id = send_channel->channel_id();
1989 if (engine()->vie()->base()->StartSend(channel_id) != 0) {
1990 LOG_RTCERR1(StartSend, channel_id);
1991 return false;
1992 }
1993
1994 send_channel->set_sending(true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001995 return true;
1996}
1997
1998bool WebRtcVideoMediaChannel::StopSend() {
1999 bool success = true;
2000 for (SendChannelMap::iterator iter = send_channels_.begin();
2001 iter != send_channels_.end(); ++iter) {
2002 WebRtcVideoChannelSendInfo* send_channel = iter->second;
2003 if (!StopSend(send_channel)) {
2004 success = false;
2005 }
2006 }
2007 return success;
2008}
2009
2010bool WebRtcVideoMediaChannel::StopSend(
2011 WebRtcVideoChannelSendInfo* send_channel) {
2012 const int channel_id = send_channel->channel_id();
2013 if (engine()->vie()->base()->StopSend(channel_id) != 0) {
2014 LOG_RTCERR1(StopSend, channel_id);
2015 return false;
2016 }
2017 send_channel->set_sending(false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002018 return true;
2019}
2020
2021bool WebRtcVideoMediaChannel::SendIntraFrame() {
2022 bool success = true;
2023 for (SendChannelMap::iterator iter = send_channels_.begin();
2024 iter != send_channels_.end();
2025 ++iter) {
2026 WebRtcVideoChannelSendInfo* send_channel = iter->second;
2027 const int channel_id = send_channel->channel_id();
2028 if (engine()->vie()->codec()->SendKeyFrame(channel_id) != 0) {
2029 LOG_RTCERR1(SendKeyFrame, channel_id);
2030 success = false;
2031 }
2032 }
2033 return success;
2034}
2035
2036bool WebRtcVideoMediaChannel::IsOneSsrcStream(const StreamParams& sp) {
2037 return (sp.ssrcs.size() == 1 && sp.ssrc_groups.size() == 0);
2038}
2039
2040bool WebRtcVideoMediaChannel::HasReadySendChannels() {
2041 return !send_channels_.empty() &&
2042 ((send_channels_.size() > 1) ||
2043 (send_channels_[0]->stream_params() != NULL));
2044}
2045
2046bool WebRtcVideoMediaChannel::GetSendChannelKey(uint32 local_ssrc,
2047 uint32* key) {
2048 *key = 0;
2049 // If a send channel is not ready to send it will not have local_ssrc
2050 // registered to it.
2051 if (!HasReadySendChannels()) {
2052 return false;
2053 }
2054 // The default channel is stored with key 0. The key therefore does not match
2055 // the SSRC associated with the default channel. Check if the SSRC provided
2056 // corresponds to the default channel's SSRC.
2057 if (local_ssrc == GetDefaultChannelSsrc()) {
2058 return true;
2059 }
2060 if (send_channels_.find(local_ssrc) == send_channels_.end()) {
2061 for (SendChannelMap::iterator iter = send_channels_.begin();
2062 iter != send_channels_.end(); ++iter) {
2063 WebRtcVideoChannelSendInfo* send_channel = iter->second;
2064 if (send_channel->has_ssrc(local_ssrc)) {
2065 *key = iter->first;
2066 return true;
2067 }
2068 }
2069 return false;
2070 }
2071 // The key was found in the above std::map::find call. This means that the
2072 // ssrc is the key.
2073 *key = local_ssrc;
2074 return true;
2075}
2076
2077WebRtcVideoChannelSendInfo* WebRtcVideoMediaChannel::GetSendChannel(
2078 VideoCapturer* video_capturer) {
2079 for (SendChannelMap::iterator iter = send_channels_.begin();
2080 iter != send_channels_.end(); ++iter) {
2081 WebRtcVideoChannelSendInfo* send_channel = iter->second;
2082 if (send_channel->video_capturer() == video_capturer) {
2083 return send_channel;
2084 }
2085 }
2086 return NULL;
2087}
2088
2089WebRtcVideoChannelSendInfo* WebRtcVideoMediaChannel::GetSendChannel(
2090 uint32 local_ssrc) {
2091 uint32 key;
2092 if (!GetSendChannelKey(local_ssrc, &key)) {
2093 return NULL;
2094 }
2095 return send_channels_[key];
2096}
2097
2098bool WebRtcVideoMediaChannel::CreateSendChannelKey(uint32 local_ssrc,
2099 uint32* key) {
2100 if (GetSendChannelKey(local_ssrc, key)) {
2101 // If there is a key corresponding to |local_ssrc|, the SSRC is already in
2102 // use. SSRCs need to be unique in a session and at this point a duplicate
2103 // SSRC has been detected.
2104 return false;
2105 }
2106 if (send_channels_[0]->stream_params() == NULL) {
2107 // key should be 0 here as the default channel should be re-used whenever it
2108 // is not used.
2109 *key = 0;
2110 return true;
2111 }
2112 // SSRC is currently not in use and the default channel is already in use. Use
2113 // the SSRC as key since it is supposed to be unique in a session.
2114 *key = local_ssrc;
2115 return true;
2116}
2117
2118uint32 WebRtcVideoMediaChannel::GetDefaultChannelSsrc() {
2119 WebRtcVideoChannelSendInfo* send_channel = send_channels_[0];
2120 const StreamParams* sp = send_channel->stream_params();
2121 if (sp == NULL) {
2122 // This happens if no send stream is currently registered.
2123 return 0;
2124 }
2125 return sp->first_ssrc();
2126}
2127
2128bool WebRtcVideoMediaChannel::DeleteSendChannel(uint32 ssrc_key) {
2129 if (send_channels_.find(ssrc_key) == send_channels_.end()) {
2130 return false;
2131 }
2132 WebRtcVideoChannelSendInfo* send_channel = send_channels_[ssrc_key];
2133 VideoCapturer* capturer = send_channel->video_capturer();
2134 if (capturer != NULL) {
2135 capturer->SignalVideoFrame.disconnect(this);
2136 send_channel->set_video_capturer(NULL);
2137 }
2138
2139 int channel_id = send_channel->channel_id();
2140 int capture_id = send_channel->capture_id();
2141 if (engine()->vie()->codec()->DeregisterEncoderObserver(
2142 channel_id) != 0) {
2143 LOG_RTCERR1(DeregisterEncoderObserver, channel_id);
2144 }
2145
2146 // Destroy the external capture interface.
2147 if (engine()->vie()->capture()->DisconnectCaptureDevice(
2148 channel_id) != 0) {
2149 LOG_RTCERR1(DisconnectCaptureDevice, channel_id);
2150 }
2151 if (engine()->vie()->capture()->ReleaseCaptureDevice(
2152 capture_id) != 0) {
2153 LOG_RTCERR1(ReleaseCaptureDevice, capture_id);
2154 }
2155
2156 // The default channel is stored in both |send_channels_| and
2157 // |recv_channels_|. To make sure it is only deleted once from vie let the
2158 // delete call happen when tearing down |recv_channels_| and not here.
2159 if (!IsDefaultChannel(channel_id)) {
2160 engine_->vie()->base()->DeleteChannel(channel_id);
2161 }
2162 delete send_channel;
2163 send_channels_.erase(ssrc_key);
2164 return true;
2165}
2166
2167bool WebRtcVideoMediaChannel::RemoveCapturer(uint32 ssrc) {
2168 WebRtcVideoChannelSendInfo* send_channel = GetSendChannel(ssrc);
2169 if (!send_channel) {
2170 return false;
2171 }
2172 VideoCapturer* capturer = send_channel->video_capturer();
2173 if (capturer == NULL) {
2174 return false;
2175 }
2176 capturer->SignalVideoFrame.disconnect(this);
2177 send_channel->set_video_capturer(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002178 const int64 timestamp = send_channel->local_stream_info()->time_stamp();
2179 if (send_codec_) {
2180 QueueBlackFrame(ssrc, timestamp, send_codec_->maxFramerate);
2181 }
2182 return true;
2183}
2184
2185bool WebRtcVideoMediaChannel::SetRenderer(uint32 ssrc,
2186 VideoRenderer* renderer) {
2187 if (recv_channels_.find(ssrc) == recv_channels_.end()) {
2188 // TODO(perkj): Remove this once BWE works properly across different send
2189 // and receive channels.
2190 // The default channel is reused for recv stream in 1:1 call.
2191 if (first_receive_ssrc_ == ssrc &&
2192 recv_channels_.find(0) != recv_channels_.end()) {
2193 LOG(LS_INFO) << "SetRenderer " << ssrc
2194 << " reuse default channel #"
2195 << vie_channel_;
2196 recv_channels_[0]->SetRenderer(renderer);
2197 return true;
2198 }
2199 return false;
2200 }
2201
2202 recv_channels_[ssrc]->SetRenderer(renderer);
2203 return true;
2204}
2205
2206bool WebRtcVideoMediaChannel::GetStats(VideoMediaInfo* info) {
2207 // Get sender statistics and build VideoSenderInfo.
2208 unsigned int total_bitrate_sent = 0;
2209 unsigned int video_bitrate_sent = 0;
2210 unsigned int fec_bitrate_sent = 0;
2211 unsigned int nack_bitrate_sent = 0;
2212 unsigned int estimated_send_bandwidth = 0;
2213 unsigned int target_enc_bitrate = 0;
2214 if (send_codec_) {
2215 for (SendChannelMap::const_iterator iter = send_channels_.begin();
2216 iter != send_channels_.end(); ++iter) {
2217 WebRtcVideoChannelSendInfo* send_channel = iter->second;
2218 const int channel_id = send_channel->channel_id();
2219 VideoSenderInfo sinfo;
2220 const StreamParams* send_params = send_channel->stream_params();
2221 if (send_params == NULL) {
2222 // This should only happen if the default vie channel is not in use.
2223 // This can happen if no streams have ever been added or the stream
2224 // corresponding to the default channel has been removed. Note that
2225 // there may be non-default vie channels in use when this happen so
2226 // asserting send_channels_.size() == 1 is not correct and neither is
2227 // breaking out of the loop.
2228 ASSERT(channel_id == vie_channel_);
2229 continue;
2230 }
2231 unsigned int bytes_sent, packets_sent, bytes_recv, packets_recv;
2232 if (engine_->vie()->rtp()->GetRTPStatistics(channel_id, bytes_sent,
2233 packets_sent, bytes_recv,
2234 packets_recv) != 0) {
2235 LOG_RTCERR1(GetRTPStatistics, vie_channel_);
2236 continue;
2237 }
2238 WebRtcLocalStreamInfo* channel_stream_info =
2239 send_channel->local_stream_info();
2240
2241 sinfo.ssrcs = send_params->ssrcs;
2242 sinfo.codec_name = send_codec_->plName;
2243 sinfo.bytes_sent = bytes_sent;
2244 sinfo.packets_sent = packets_sent;
2245 sinfo.packets_cached = -1;
2246 sinfo.packets_lost = -1;
2247 sinfo.fraction_lost = -1;
2248 sinfo.firs_rcvd = -1;
2249 sinfo.nacks_rcvd = -1;
2250 sinfo.rtt_ms = -1;
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00002251 sinfo.frame_width = static_cast<int>(channel_stream_info->width());
2252 sinfo.frame_height = static_cast<int>(channel_stream_info->height());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002253 sinfo.framerate_input = channel_stream_info->framerate();
2254 sinfo.framerate_sent = send_channel->encoder_observer()->framerate();
2255 sinfo.nominal_bitrate = send_channel->encoder_observer()->bitrate();
2256 sinfo.preferred_bitrate = send_max_bitrate_;
2257 sinfo.adapt_reason = send_channel->CurrentAdaptReason();
2258
2259 // Get received RTCP statistics for the sender, if available.
2260 // It's not a fatal error if we can't, since RTCP may not have arrived
2261 // yet.
2262 uint16 r_fraction_lost;
2263 unsigned int r_cumulative_lost;
2264 unsigned int r_extended_max;
2265 unsigned int r_jitter;
2266 int r_rtt_ms;
2267
2268 if (engine_->vie()->rtp()->GetSentRTCPStatistics(
2269 channel_id,
2270 r_fraction_lost,
2271 r_cumulative_lost,
2272 r_extended_max,
2273 r_jitter, r_rtt_ms) == 0) {
2274 // Convert Q8 to float.
2275 sinfo.packets_lost = r_cumulative_lost;
2276 sinfo.fraction_lost = static_cast<float>(r_fraction_lost) / (1 << 8);
2277 sinfo.rtt_ms = r_rtt_ms;
2278 }
2279 info->senders.push_back(sinfo);
2280
2281 unsigned int channel_total_bitrate_sent = 0;
2282 unsigned int channel_video_bitrate_sent = 0;
2283 unsigned int channel_fec_bitrate_sent = 0;
2284 unsigned int channel_nack_bitrate_sent = 0;
2285 if (engine_->vie()->rtp()->GetBandwidthUsage(
2286 channel_id, channel_total_bitrate_sent, channel_video_bitrate_sent,
2287 channel_fec_bitrate_sent, channel_nack_bitrate_sent) == 0) {
2288 total_bitrate_sent += channel_total_bitrate_sent;
2289 video_bitrate_sent += channel_video_bitrate_sent;
2290 fec_bitrate_sent += channel_fec_bitrate_sent;
2291 nack_bitrate_sent += channel_nack_bitrate_sent;
2292 } else {
2293 LOG_RTCERR1(GetBandwidthUsage, channel_id);
2294 }
2295
2296 unsigned int estimated_stream_send_bandwidth = 0;
2297 if (engine_->vie()->rtp()->GetEstimatedSendBandwidth(
2298 channel_id, &estimated_stream_send_bandwidth) == 0) {
2299 estimated_send_bandwidth += estimated_stream_send_bandwidth;
2300 } else {
2301 LOG_RTCERR1(GetEstimatedSendBandwidth, channel_id);
2302 }
2303 unsigned int target_enc_stream_bitrate = 0;
2304 if (engine_->vie()->codec()->GetCodecTargetBitrate(
2305 channel_id, &target_enc_stream_bitrate) == 0) {
2306 target_enc_bitrate += target_enc_stream_bitrate;
2307 } else {
2308 LOG_RTCERR1(GetCodecTargetBitrate, channel_id);
2309 }
2310 }
2311 } else {
2312 LOG(LS_WARNING) << "GetStats: sender information not ready.";
2313 }
2314
2315 // Get the SSRC and stats for each receiver, based on our own calculations.
2316 unsigned int estimated_recv_bandwidth = 0;
2317 for (RecvChannelMap::const_iterator it = recv_channels_.begin();
2318 it != recv_channels_.end(); ++it) {
2319 // Don't report receive statistics from the default channel if we have
2320 // specified receive channels.
2321 if (it->first == 0 && recv_channels_.size() > 1)
2322 continue;
2323 WebRtcVideoChannelRecvInfo* channel = it->second;
2324
2325 unsigned int ssrc;
2326 // Get receiver statistics and build VideoReceiverInfo, if we have data.
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +00002327 // Skip the default channel (ssrc == 0).
2328 if (engine_->vie()->rtp()->GetRemoteSSRC(
2329 channel->channel_id(), ssrc) != 0 ||
2330 ssrc == 0)
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002331 continue;
2332
2333 unsigned int bytes_sent, packets_sent, bytes_recv, packets_recv;
2334 if (engine_->vie()->rtp()->GetRTPStatistics(
2335 channel->channel_id(), bytes_sent, packets_sent, bytes_recv,
2336 packets_recv) != 0) {
2337 LOG_RTCERR1(GetRTPStatistics, channel->channel_id());
2338 return false;
2339 }
2340 VideoReceiverInfo rinfo;
2341 rinfo.ssrcs.push_back(ssrc);
2342 rinfo.bytes_rcvd = bytes_recv;
2343 rinfo.packets_rcvd = packets_recv;
2344 rinfo.packets_lost = -1;
2345 rinfo.packets_concealed = -1;
2346 rinfo.fraction_lost = -1; // from SentRTCP
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002347 rinfo.nacks_sent = -1;
2348 rinfo.frame_width = channel->render_adapter()->width();
2349 rinfo.frame_height = channel->render_adapter()->height();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002350 int fps = channel->render_adapter()->framerate();
2351 rinfo.framerate_decoded = fps;
2352 rinfo.framerate_output = fps;
wu@webrtc.org97077a32013-10-25 21:18:33 +00002353 channel->decoder_observer()->ExportTo(&rinfo);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002354
2355 // Get sent RTCP statistics.
2356 uint16 s_fraction_lost;
2357 unsigned int s_cumulative_lost;
2358 unsigned int s_extended_max;
2359 unsigned int s_jitter;
2360 int s_rtt_ms;
2361 if (engine_->vie()->rtp()->GetReceivedRTCPStatistics(channel->channel_id(),
2362 s_fraction_lost, s_cumulative_lost, s_extended_max,
2363 s_jitter, s_rtt_ms) == 0) {
2364 // Convert Q8 to float.
2365 rinfo.packets_lost = s_cumulative_lost;
2366 rinfo.fraction_lost = static_cast<float>(s_fraction_lost) / (1 << 8);
2367 }
2368 info->receivers.push_back(rinfo);
2369
2370 unsigned int estimated_recv_stream_bandwidth = 0;
2371 if (engine_->vie()->rtp()->GetEstimatedReceiveBandwidth(
2372 channel->channel_id(), &estimated_recv_stream_bandwidth) == 0) {
2373 estimated_recv_bandwidth += estimated_recv_stream_bandwidth;
2374 } else {
2375 LOG_RTCERR1(GetEstimatedReceiveBandwidth, channel->channel_id());
2376 }
2377 }
2378
2379 // Build BandwidthEstimationInfo.
2380 // TODO(zhurunz): Add real unittest for this.
2381 BandwidthEstimationInfo bwe;
2382
2383 // Calculations done above per send/receive stream.
2384 bwe.actual_enc_bitrate = video_bitrate_sent;
2385 bwe.transmit_bitrate = total_bitrate_sent;
2386 bwe.retransmit_bitrate = nack_bitrate_sent;
2387 bwe.available_send_bandwidth = estimated_send_bandwidth;
2388 bwe.available_recv_bandwidth = estimated_recv_bandwidth;
2389 bwe.target_enc_bitrate = target_enc_bitrate;
2390
2391 info->bw_estimations.push_back(bwe);
2392
2393 return true;
2394}
2395
2396bool WebRtcVideoMediaChannel::SetCapturer(uint32 ssrc,
2397 VideoCapturer* capturer) {
2398 ASSERT(ssrc != 0);
2399 if (!capturer) {
2400 return RemoveCapturer(ssrc);
2401 }
2402 WebRtcVideoChannelSendInfo* send_channel = GetSendChannel(ssrc);
2403 if (!send_channel) {
2404 return false;
2405 }
2406 VideoCapturer* old_capturer = send_channel->video_capturer();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002407 if (old_capturer) {
2408 old_capturer->SignalVideoFrame.disconnect(this);
2409 }
2410
2411 send_channel->set_video_capturer(capturer);
2412 capturer->SignalVideoFrame.connect(
2413 this,
2414 &WebRtcVideoMediaChannel::AdaptAndSendFrame);
2415 if (!capturer->IsScreencast() && ratio_w_ != 0 && ratio_h_ != 0) {
2416 capturer->UpdateAspectRatio(ratio_w_, ratio_h_);
2417 }
2418 const int64 timestamp = send_channel->local_stream_info()->time_stamp();
2419 if (send_codec_) {
2420 QueueBlackFrame(ssrc, timestamp, send_codec_->maxFramerate);
2421 }
2422 return true;
2423}
2424
2425bool WebRtcVideoMediaChannel::RequestIntraFrame() {
2426 // There is no API exposed to application to request a key frame
2427 // ViE does this internally when there are errors from decoder
2428 return false;
2429}
2430
2431void WebRtcVideoMediaChannel::OnPacketReceived(talk_base::Buffer* packet) {
2432 // Pick which channel to send this packet to. If this packet doesn't match
2433 // any multiplexed streams, just send it to the default channel. Otherwise,
2434 // send it to the specific decoder instance for that stream.
2435 uint32 ssrc = 0;
2436 if (!GetRtpSsrc(packet->data(), packet->length(), &ssrc))
2437 return;
2438 int which_channel = GetRecvChannelNum(ssrc);
2439 if (which_channel == -1) {
2440 which_channel = video_channel();
2441 }
2442
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00002443 engine()->vie()->network()->ReceivedRTPPacket(
2444 which_channel,
2445 packet->data(),
2446 static_cast<int>(packet->length()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002447}
2448
2449void WebRtcVideoMediaChannel::OnRtcpReceived(talk_base::Buffer* packet) {
2450// Sending channels need all RTCP packets with feedback information.
2451// Even sender reports can contain attached report blocks.
2452// Receiving channels need sender reports in order to create
2453// correct receiver reports.
2454
2455 uint32 ssrc = 0;
2456 if (!GetRtcpSsrc(packet->data(), packet->length(), &ssrc)) {
2457 LOG(LS_WARNING) << "Failed to parse SSRC from received RTCP packet";
2458 return;
2459 }
2460 int type = 0;
2461 if (!GetRtcpType(packet->data(), packet->length(), &type)) {
2462 LOG(LS_WARNING) << "Failed to parse type from received RTCP packet";
2463 return;
2464 }
2465
2466 // If it is a sender report, find the channel that is listening.
2467 if (type == kRtcpTypeSR) {
2468 int which_channel = GetRecvChannelNum(ssrc);
2469 if (which_channel != -1 && !IsDefaultChannel(which_channel)) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00002470 engine_->vie()->network()->ReceivedRTCPPacket(
2471 which_channel,
2472 packet->data(),
2473 static_cast<int>(packet->length()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002474 }
2475 }
2476 // SR may continue RR and any RR entry may correspond to any one of the send
2477 // channels. So all RTCP packets must be forwarded all send channels. ViE
2478 // will filter out RR internally.
2479 for (SendChannelMap::iterator iter = send_channels_.begin();
2480 iter != send_channels_.end(); ++iter) {
2481 WebRtcVideoChannelSendInfo* send_channel = iter->second;
2482 int channel_id = send_channel->channel_id();
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00002483 engine_->vie()->network()->ReceivedRTCPPacket(
2484 channel_id,
2485 packet->data(),
2486 static_cast<int>(packet->length()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002487 }
2488}
2489
2490void WebRtcVideoMediaChannel::OnReadyToSend(bool ready) {
2491 SetNetworkTransmissionState(ready);
2492}
2493
2494bool WebRtcVideoMediaChannel::MuteStream(uint32 ssrc, bool muted) {
2495 WebRtcVideoChannelSendInfo* send_channel = GetSendChannel(ssrc);
2496 if (!send_channel) {
2497 LOG(LS_ERROR) << "The specified ssrc " << ssrc << " is not in use.";
2498 return false;
2499 }
2500 send_channel->set_muted(muted);
2501 return true;
2502}
2503
2504bool WebRtcVideoMediaChannel::SetRecvRtpHeaderExtensions(
2505 const std::vector<RtpHeaderExtension>& extensions) {
2506 if (receive_extensions_ == extensions) {
2507 return true;
2508 }
2509 receive_extensions_ = extensions;
2510
2511 const RtpHeaderExtension* offset_extension =
2512 FindHeaderExtension(extensions, kRtpTimestampOffsetHeaderExtension);
2513 const RtpHeaderExtension* send_time_extension =
2514 FindHeaderExtension(extensions, kRtpAbsoluteSendTimeHeaderExtension);
2515
2516 // Loop through all receive channels and enable/disable the extensions.
2517 for (RecvChannelMap::iterator channel_it = recv_channels_.begin();
2518 channel_it != recv_channels_.end(); ++channel_it) {
2519 int channel_id = channel_it->second->channel_id();
2520 if (!SetHeaderExtension(
2521 &webrtc::ViERTP_RTCP::SetReceiveTimestampOffsetStatus, channel_id,
2522 offset_extension)) {
2523 return false;
2524 }
2525 if (!SetHeaderExtension(
2526 &webrtc::ViERTP_RTCP::SetReceiveAbsoluteSendTimeStatus, channel_id,
2527 send_time_extension)) {
2528 return false;
2529 }
2530 }
2531 return true;
2532}
2533
2534bool WebRtcVideoMediaChannel::SetSendRtpHeaderExtensions(
2535 const std::vector<RtpHeaderExtension>& extensions) {
2536 send_extensions_ = extensions;
2537
2538 const RtpHeaderExtension* offset_extension =
2539 FindHeaderExtension(extensions, kRtpTimestampOffsetHeaderExtension);
2540 const RtpHeaderExtension* send_time_extension =
2541 FindHeaderExtension(extensions, kRtpAbsoluteSendTimeHeaderExtension);
2542
2543 // Loop through all send channels and enable/disable the extensions.
2544 for (SendChannelMap::iterator channel_it = send_channels_.begin();
2545 channel_it != send_channels_.end(); ++channel_it) {
2546 int channel_id = channel_it->second->channel_id();
2547 if (!SetHeaderExtension(
2548 &webrtc::ViERTP_RTCP::SetSendTimestampOffsetStatus, channel_id,
2549 offset_extension)) {
2550 return false;
2551 }
2552 if (!SetHeaderExtension(
2553 &webrtc::ViERTP_RTCP::SetSendAbsoluteSendTimeStatus, channel_id,
2554 send_time_extension)) {
2555 return false;
2556 }
2557 }
2558 return true;
2559}
2560
2561bool WebRtcVideoMediaChannel::SetSendBandwidth(bool autobw, int bps) {
2562 LOG(LS_INFO) << "WebRtcVideoMediaChanne::SetSendBandwidth";
2563
2564 if (InConferenceMode()) {
2565 LOG(LS_INFO) << "Conference mode ignores SetSendBandWidth";
2566 return true;
2567 }
2568
2569 if (!send_codec_) {
2570 LOG(LS_INFO) << "The send codec has not been set up yet";
2571 return true;
2572 }
2573
2574 int min_bitrate;
2575 int start_bitrate;
2576 int max_bitrate;
2577 if (autobw) {
2578 // Use the default values for min bitrate.
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00002579 min_bitrate = send_min_bitrate_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002580 // Use the default value or the bps for the max
2581 max_bitrate = (bps <= 0) ? send_max_bitrate_ : (bps / 1000);
2582 // Maximum start bitrate can be kStartVideoBitrate.
2583 start_bitrate = talk_base::_min(kStartVideoBitrate, max_bitrate);
2584 } else {
2585 // Use the default start or the bps as the target bitrate.
2586 int target_bitrate = (bps <= 0) ? kStartVideoBitrate : (bps / 1000);
2587 min_bitrate = target_bitrate;
2588 start_bitrate = target_bitrate;
2589 max_bitrate = target_bitrate;
2590 }
2591
2592 if (!SetSendCodec(*send_codec_, min_bitrate, start_bitrate, max_bitrate)) {
2593 return false;
2594 }
2595 LogSendCodecChange("SetSendBandwidth()");
2596
2597 return true;
2598}
2599
2600bool WebRtcVideoMediaChannel::SetOptions(const VideoOptions &options) {
2601 // Always accept options that are unchanged.
2602 if (options_ == options) {
2603 return true;
2604 }
2605
2606 // Trigger SetSendCodec to set correct noise reduction state if the option has
2607 // changed.
2608 bool denoiser_changed = options.video_noise_reduction.IsSet() &&
2609 (options_.video_noise_reduction != options.video_noise_reduction);
2610
2611 bool leaky_bucket_changed = options.video_leaky_bucket.IsSet() &&
2612 (options_.video_leaky_bucket != options.video_leaky_bucket);
2613
2614 bool buffer_latency_changed = options.buffered_mode_latency.IsSet() &&
2615 (options_.buffered_mode_latency != options.buffered_mode_latency);
2616
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00002617 bool cpu_overuse_detection_changed = options.cpu_overuse_detection.IsSet() &&
2618 (options_.cpu_overuse_detection != options.cpu_overuse_detection);
2619
wu@webrtc.orgde305012013-10-31 15:40:38 +00002620 bool dscp_option_changed = (options_.dscp != options.dscp);
2621
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002622 bool conference_mode_turned_off = false;
2623 if (options_.conference_mode.IsSet() && options.conference_mode.IsSet() &&
2624 options_.conference_mode.GetWithDefaultIfUnset(false) &&
2625 !options.conference_mode.GetWithDefaultIfUnset(false)) {
2626 conference_mode_turned_off = true;
2627 }
2628
2629 // Save the options, to be interpreted where appropriate.
2630 // Use options_.SetAll() instead of assignment so that unset value in options
2631 // will not overwrite the previous option value.
2632 options_.SetAll(options);
2633
2634 // Set CPU options for all send channels.
2635 for (SendChannelMap::iterator iter = send_channels_.begin();
2636 iter != send_channels_.end(); ++iter) {
2637 WebRtcVideoChannelSendInfo* send_channel = iter->second;
2638 send_channel->ApplyCpuOptions(options_);
2639 }
2640
2641 // Adjust send codec bitrate if needed.
2642 int conf_max_bitrate = kDefaultConferenceModeMaxVideoBitrate;
2643
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00002644 // Save altered min_bitrate level and apply if necessary.
2645 bool adjusted_min_bitrate = false;
2646 if (options.lower_min_bitrate.IsSet()) {
2647 bool lower;
2648 options.lower_min_bitrate.Get(&lower);
2649
2650 int new_send_min_bitrate = lower ? kLowerMinBitrate : kMinVideoBitrate;
2651 adjusted_min_bitrate = (new_send_min_bitrate != send_min_bitrate_);
2652 send_min_bitrate_ = new_send_min_bitrate;
2653 }
2654
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002655 int expected_bitrate = send_max_bitrate_;
2656 if (InConferenceMode()) {
2657 expected_bitrate = conf_max_bitrate;
2658 } else if (conference_mode_turned_off) {
2659 // This is a special case for turning conference mode off.
2660 // Max bitrate should go back to the default maximum value instead
2661 // of the current maximum.
2662 expected_bitrate = kMaxVideoBitrate;
2663 }
2664
2665 if (send_codec_ &&
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00002666 (send_max_bitrate_ != expected_bitrate || denoiser_changed ||
2667 adjusted_min_bitrate)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002668 // On success, SetSendCodec() will reset send_max_bitrate_ to
2669 // expected_bitrate.
2670 if (!SetSendCodec(*send_codec_,
2671 send_min_bitrate_,
2672 send_start_bitrate_,
2673 expected_bitrate)) {
2674 return false;
2675 }
2676 LogSendCodecChange("SetOptions()");
2677 }
2678 if (leaky_bucket_changed) {
2679 bool enable_leaky_bucket =
2680 options_.video_leaky_bucket.GetWithDefaultIfUnset(false);
2681 for (SendChannelMap::iterator it = send_channels_.begin();
2682 it != send_channels_.end(); ++it) {
2683 if (engine()->vie()->rtp()->SetTransmissionSmoothingStatus(
2684 it->second->channel_id(), enable_leaky_bucket) != 0) {
2685 LOG_RTCERR2(SetTransmissionSmoothingStatus, it->second->channel_id(),
2686 enable_leaky_bucket);
2687 }
2688 }
2689 }
2690 if (buffer_latency_changed) {
2691 int buffer_latency =
2692 options_.buffered_mode_latency.GetWithDefaultIfUnset(
2693 cricket::kBufferedModeDisabled);
2694 for (SendChannelMap::iterator it = send_channels_.begin();
2695 it != send_channels_.end(); ++it) {
2696 if (engine()->vie()->rtp()->SetSenderBufferingMode(
2697 it->second->channel_id(), buffer_latency) != 0) {
2698 LOG_RTCERR2(SetSenderBufferingMode, it->second->channel_id(),
2699 buffer_latency);
2700 }
2701 }
2702 for (RecvChannelMap::iterator it = recv_channels_.begin();
2703 it != recv_channels_.end(); ++it) {
2704 if (engine()->vie()->rtp()->SetReceiverBufferingMode(
2705 it->second->channel_id(), buffer_latency) != 0) {
2706 LOG_RTCERR2(SetReceiverBufferingMode, it->second->channel_id(),
2707 buffer_latency);
2708 }
2709 }
2710 }
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00002711 if (cpu_overuse_detection_changed) {
2712 bool cpu_overuse_detection =
2713 options_.cpu_overuse_detection.GetWithDefaultIfUnset(false);
2714 for (SendChannelMap::iterator iter = send_channels_.begin();
2715 iter != send_channels_.end(); ++iter) {
2716 WebRtcVideoChannelSendInfo* send_channel = iter->second;
2717 send_channel->SetCpuOveruseDetection(cpu_overuse_detection);
2718 }
2719 }
wu@webrtc.orgde305012013-10-31 15:40:38 +00002720 if (dscp_option_changed) {
2721 talk_base::DiffServCodePoint dscp = talk_base::DSCP_DEFAULT;
2722 if (options.dscp.GetWithDefaultIfUnset(false))
2723 dscp = kVideoDscpValue;
2724 if (MediaChannel::SetDscp(dscp) != 0) {
2725 LOG(LS_WARNING) << "Failed to set DSCP settings for video channel";
2726 }
2727 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002728 return true;
2729}
2730
2731void WebRtcVideoMediaChannel::SetInterface(NetworkInterface* iface) {
2732 MediaChannel::SetInterface(iface);
2733 // Set the RTP recv/send buffer to a bigger size
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002734 MediaChannel::SetOption(NetworkInterface::ST_RTP,
2735 talk_base::Socket::OPT_RCVBUF,
2736 kVideoRtpBufferSize);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002737
2738 // TODO(sriniv): Remove or re-enable this.
2739 // As part of b/8030474, send-buffer is size now controlled through
2740 // portallocator flags.
2741 // network_interface_->SetOption(NetworkInterface::ST_RTP,
2742 // talk_base::Socket::OPT_SNDBUF,
2743 // kVideoRtpBufferSize);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002744}
2745
2746void WebRtcVideoMediaChannel::UpdateAspectRatio(int ratio_w, int ratio_h) {
2747 ASSERT(ratio_w != 0);
2748 ASSERT(ratio_h != 0);
2749 ratio_w_ = ratio_w;
2750 ratio_h_ = ratio_h;
2751 // For now assume that all streams want the same aspect ratio.
2752 // TODO(hellner): remove the need for this assumption.
2753 for (SendChannelMap::iterator iter = send_channels_.begin();
2754 iter != send_channels_.end(); ++iter) {
2755 WebRtcVideoChannelSendInfo* send_channel = iter->second;
2756 VideoCapturer* capturer = send_channel->video_capturer();
2757 if (capturer) {
2758 capturer->UpdateAspectRatio(ratio_w, ratio_h);
2759 }
2760 }
2761}
2762
2763bool WebRtcVideoMediaChannel::GetRenderer(uint32 ssrc,
2764 VideoRenderer** renderer) {
2765 RecvChannelMap::const_iterator it = recv_channels_.find(ssrc);
2766 if (it == recv_channels_.end()) {
2767 if (first_receive_ssrc_ == ssrc &&
2768 recv_channels_.find(0) != recv_channels_.end()) {
2769 LOG(LS_INFO) << " GetRenderer " << ssrc
2770 << " reuse default renderer #"
2771 << vie_channel_;
2772 *renderer = recv_channels_[0]->render_adapter()->renderer();
2773 return true;
2774 }
2775 return false;
2776 }
2777
2778 *renderer = it->second->render_adapter()->renderer();
2779 return true;
2780}
2781
2782void WebRtcVideoMediaChannel::AdaptAndSendFrame(VideoCapturer* capturer,
2783 const VideoFrame* frame) {
2784 if (capturer->IsScreencast()) {
2785 // Do not adapt frames that are screencast.
2786 SendFrame(capturer, frame);
2787 return;
2788 }
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00002789 // TODO(thorcarpenter): This is broken. One capturer registered on two ssrc
2790 // will not send any video to the second ssrc send channel. We should remove
2791 // GetSendChannel(capturer) and pass in an ssrc here.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002792 WebRtcVideoChannelSendInfo* send_channel = GetSendChannel(capturer);
2793 if (!send_channel) {
2794 SendFrame(capturer, frame);
2795 return;
2796 }
2797 const VideoFrame* output_frame = NULL;
2798 send_channel->AdaptFrame(frame, &output_frame);
2799 if (output_frame) {
2800 SendFrame(send_channel, output_frame, capturer->IsScreencast());
2801 }
2802}
2803
2804// TODO(zhurunz): Add unittests to test this function.
2805void WebRtcVideoMediaChannel::SendFrame(VideoCapturer* capturer,
2806 const VideoFrame* frame) {
2807 // If there's send channel registers to the |capturer|, then only send the
2808 // frame to that channel and return. Otherwise send the frame to the default
2809 // channel, which currently taking frames from the engine.
2810 WebRtcVideoChannelSendInfo* send_channel = GetSendChannel(capturer);
2811 if (send_channel) {
2812 SendFrame(send_channel, frame, capturer->IsScreencast());
2813 return;
2814 }
2815 // TODO(hellner): Remove below for loop once the captured frame no longer
2816 // come from the engine, i.e. the engine no longer owns a capturer.
2817 for (SendChannelMap::iterator iter = send_channels_.begin();
2818 iter != send_channels_.end(); ++iter) {
2819 WebRtcVideoChannelSendInfo* send_channel = iter->second;
2820 if (send_channel->video_capturer() == NULL) {
2821 SendFrame(send_channel, frame, capturer->IsScreencast());
2822 }
2823 }
2824}
2825
2826bool WebRtcVideoMediaChannel::SendFrame(
2827 WebRtcVideoChannelSendInfo* send_channel,
2828 const VideoFrame* frame,
2829 bool is_screencast) {
2830 if (!send_channel) {
2831 return false;
2832 }
2833 if (!send_codec_) {
2834 // Send codec has not been set. No reason to process the frame any further.
2835 return false;
2836 }
2837 const VideoFormat& video_format = send_channel->video_format();
2838 // If the frame should be dropped.
2839 const bool video_format_set = video_format != cricket::VideoFormat();
2840 if (video_format_set &&
2841 (video_format.width == 0 && video_format.height == 0)) {
2842 return true;
2843 }
2844
2845 // Checks if we need to reset vie send codec.
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00002846 if (!MaybeResetVieSendCodec(send_channel,
2847 static_cast<int>(frame->GetWidth()),
2848 static_cast<int>(frame->GetHeight()),
2849 is_screencast, NULL)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002850 LOG(LS_ERROR) << "MaybeResetVieSendCodec failed with "
2851 << frame->GetWidth() << "x" << frame->GetHeight();
2852 return false;
2853 }
2854 const VideoFrame* frame_out = frame;
2855 talk_base::scoped_ptr<VideoFrame> processed_frame;
2856 // Disable muting for screencast.
2857 const bool mute = (send_channel->muted() && !is_screencast);
2858 send_channel->ProcessFrame(*frame_out, mute, processed_frame.use());
2859 if (processed_frame) {
2860 frame_out = processed_frame.get();
2861 }
2862
2863 webrtc::ViEVideoFrameI420 frame_i420;
2864 // TODO(ronghuawu): Update the webrtc::ViEVideoFrameI420
2865 // to use const unsigned char*
2866 frame_i420.y_plane = const_cast<unsigned char*>(frame_out->GetYPlane());
2867 frame_i420.u_plane = const_cast<unsigned char*>(frame_out->GetUPlane());
2868 frame_i420.v_plane = const_cast<unsigned char*>(frame_out->GetVPlane());
2869 frame_i420.y_pitch = frame_out->GetYPitch();
2870 frame_i420.u_pitch = frame_out->GetUPitch();
2871 frame_i420.v_pitch = frame_out->GetVPitch();
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00002872 frame_i420.width = static_cast<uint16>(frame_out->GetWidth());
2873 frame_i420.height = static_cast<uint16>(frame_out->GetHeight());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002874
2875 int64 timestamp_ntp_ms = 0;
2876 // TODO(justinlin): Reenable after Windows issues with clock drift are fixed.
2877 // Currently reverted to old behavior of discarding capture timestamp.
2878#if 0
2879 // If the frame timestamp is 0, we will use the deliver time.
2880 const int64 frame_timestamp = frame->GetTimeStamp();
2881 if (frame_timestamp != 0) {
2882 if (abs(time(NULL) - frame_timestamp / talk_base::kNumNanosecsPerSec) >
2883 kTimestampDeltaInSecondsForWarning) {
2884 LOG(LS_WARNING) << "Frame timestamp differs by more than "
2885 << kTimestampDeltaInSecondsForWarning << " seconds from "
2886 << "current Unix timestamp.";
2887 }
2888
2889 timestamp_ntp_ms =
2890 talk_base::UnixTimestampNanosecsToNtpMillisecs(frame_timestamp);
2891 }
2892#endif
2893
2894 return send_channel->external_capture()->IncomingFrameI420(
2895 frame_i420, timestamp_ntp_ms) == 0;
2896}
2897
2898bool WebRtcVideoMediaChannel::CreateChannel(uint32 ssrc_key,
2899 MediaDirection direction,
2900 int* channel_id) {
2901 // There are 3 types of channels. Sending only, receiving only and
2902 // sending and receiving. The sending and receiving channel is the
2903 // default channel and there is only one. All other channels that are created
2904 // are associated with the default channel which must exist. The default
2905 // channel id is stored in |vie_channel_|. All channels need to know about
2906 // the default channel to properly handle remb which is why there are
2907 // different ViE create channel calls.
2908 // For this channel the local and remote ssrc key is 0. However, it may
2909 // have a non-zero local and/or remote ssrc depending on if it is currently
2910 // sending and/or receiving.
2911 if ((vie_channel_ == -1 || direction == MD_SENDRECV) &&
2912 (!send_channels_.empty() || !recv_channels_.empty())) {
2913 ASSERT(false);
2914 return false;
2915 }
2916
2917 *channel_id = -1;
2918 if (direction == MD_RECV) {
2919 // All rec channels are associated with the default channel |vie_channel_|
2920 if (engine_->vie()->base()->CreateReceiveChannel(*channel_id,
2921 vie_channel_) != 0) {
2922 LOG_RTCERR2(CreateReceiveChannel, *channel_id, vie_channel_);
2923 return false;
2924 }
2925 } else if (direction == MD_SEND) {
2926 if (engine_->vie()->base()->CreateChannel(*channel_id,
2927 vie_channel_) != 0) {
2928 LOG_RTCERR2(CreateChannel, *channel_id, vie_channel_);
2929 return false;
2930 }
2931 } else {
2932 ASSERT(direction == MD_SENDRECV);
2933 if (engine_->vie()->base()->CreateChannel(*channel_id) != 0) {
2934 LOG_RTCERR1(CreateChannel, *channel_id);
2935 return false;
2936 }
2937 }
2938 if (!ConfigureChannel(*channel_id, direction, ssrc_key)) {
2939 engine_->vie()->base()->DeleteChannel(*channel_id);
2940 *channel_id = -1;
2941 return false;
2942 }
2943
2944 return true;
2945}
2946
2947bool WebRtcVideoMediaChannel::ConfigureChannel(int channel_id,
2948 MediaDirection direction,
2949 uint32 ssrc_key) {
2950 const bool receiving = (direction == MD_RECV) || (direction == MD_SENDRECV);
2951 const bool sending = (direction == MD_SEND) || (direction == MD_SENDRECV);
2952 // Register external transport.
2953 if (engine_->vie()->network()->RegisterSendTransport(
2954 channel_id, *this) != 0) {
2955 LOG_RTCERR1(RegisterSendTransport, channel_id);
2956 return false;
2957 }
2958
2959 // Set MTU.
2960 if (engine_->vie()->network()->SetMTU(channel_id, kVideoMtu) != 0) {
2961 LOG_RTCERR2(SetMTU, channel_id, kVideoMtu);
2962 return false;
2963 }
2964 // Turn on RTCP and loss feedback reporting.
2965 if (engine()->vie()->rtp()->SetRTCPStatus(
2966 channel_id, webrtc::kRtcpCompound_RFC4585) != 0) {
2967 LOG_RTCERR2(SetRTCPStatus, channel_id, webrtc::kRtcpCompound_RFC4585);
2968 return false;
2969 }
2970 // Enable pli as key frame request method.
2971 if (engine_->vie()->rtp()->SetKeyFrameRequestMethod(
2972 channel_id, webrtc::kViEKeyFrameRequestPliRtcp) != 0) {
2973 LOG_RTCERR2(SetKeyFrameRequestMethod,
2974 channel_id, webrtc::kViEKeyFrameRequestPliRtcp);
2975 return false;
2976 }
2977 if (!SetNackFec(channel_id, send_red_type_, send_fec_type_, nack_enabled_)) {
2978 // Logged in SetNackFec. Don't spam the logs.
2979 return false;
2980 }
2981 // Note that receiving must always be configured before sending to ensure
2982 // that send and receive channel is configured correctly (ConfigureReceiving
2983 // assumes no sending).
2984 if (receiving) {
2985 if (!ConfigureReceiving(channel_id, ssrc_key)) {
2986 return false;
2987 }
2988 }
2989 if (sending) {
2990 if (!ConfigureSending(channel_id, ssrc_key)) {
2991 return false;
2992 }
2993 }
2994
2995 return true;
2996}
2997
2998bool WebRtcVideoMediaChannel::ConfigureReceiving(int channel_id,
2999 uint32 remote_ssrc_key) {
3000 // Make sure that an SSRC/key isn't registered more than once.
3001 if (recv_channels_.find(remote_ssrc_key) != recv_channels_.end()) {
3002 return false;
3003 }
3004 // Connect the voice channel, if there is one.
3005 // TODO(perkj): The A/V is synched by the receiving channel. So we need to
3006 // know the SSRC of the remote audio channel in order to fetch the correct
3007 // webrtc VoiceEngine channel. For now- only sync the default channel used
3008 // in 1-1 calls.
3009 if (remote_ssrc_key == 0 && voice_channel_) {
3010 WebRtcVoiceMediaChannel* voice_channel =
3011 static_cast<WebRtcVoiceMediaChannel*>(voice_channel_);
3012 if (engine_->vie()->base()->ConnectAudioChannel(
3013 vie_channel_, voice_channel->voe_channel()) != 0) {
3014 LOG_RTCERR2(ConnectAudioChannel, channel_id,
3015 voice_channel->voe_channel());
3016 LOG(LS_WARNING) << "A/V not synchronized";
3017 // Not a fatal error.
3018 }
3019 }
3020
3021 talk_base::scoped_ptr<WebRtcVideoChannelRecvInfo> channel_info(
3022 new WebRtcVideoChannelRecvInfo(channel_id));
3023
3024 // Install a render adapter.
3025 if (engine_->vie()->render()->AddRenderer(channel_id,
3026 webrtc::kVideoI420, channel_info->render_adapter()) != 0) {
3027 LOG_RTCERR3(AddRenderer, channel_id, webrtc::kVideoI420,
3028 channel_info->render_adapter());
3029 return false;
3030 }
3031
3032
3033 if (engine_->vie()->rtp()->SetRembStatus(channel_id,
3034 kNotSending,
3035 remb_enabled_) != 0) {
3036 LOG_RTCERR3(SetRembStatus, channel_id, kNotSending, remb_enabled_);
3037 return false;
3038 }
3039
3040 if (!SetHeaderExtension(&webrtc::ViERTP_RTCP::SetReceiveTimestampOffsetStatus,
3041 channel_id, receive_extensions_, kRtpTimestampOffsetHeaderExtension)) {
3042 return false;
3043 }
3044
3045 if (!SetHeaderExtension(
3046 &webrtc::ViERTP_RTCP::SetReceiveAbsoluteSendTimeStatus, channel_id,
3047 receive_extensions_, kRtpAbsoluteSendTimeHeaderExtension)) {
3048 return false;
3049 }
3050
3051 if (remote_ssrc_key != 0) {
3052 // Use the same SSRC as our default channel
3053 // (so the RTCP reports are correct).
3054 unsigned int send_ssrc = 0;
3055 webrtc::ViERTP_RTCP* rtp = engine()->vie()->rtp();
3056 if (rtp->GetLocalSSRC(vie_channel_, send_ssrc) == -1) {
3057 LOG_RTCERR2(GetLocalSSRC, vie_channel_, send_ssrc);
3058 return false;
3059 }
3060 if (rtp->SetLocalSSRC(channel_id, send_ssrc) == -1) {
3061 LOG_RTCERR2(SetLocalSSRC, channel_id, send_ssrc);
3062 return false;
3063 }
3064 } // Else this is the the default channel and we don't change the SSRC.
3065
3066 // Disable color enhancement since it is a bit too aggressive.
3067 if (engine()->vie()->image()->EnableColorEnhancement(channel_id,
3068 false) != 0) {
3069 LOG_RTCERR1(EnableColorEnhancement, channel_id);
3070 return false;
3071 }
3072
3073 if (!SetReceiveCodecs(channel_info.get())) {
3074 return false;
3075 }
3076
3077 int buffer_latency =
3078 options_.buffered_mode_latency.GetWithDefaultIfUnset(
3079 cricket::kBufferedModeDisabled);
3080 if (buffer_latency != cricket::kBufferedModeDisabled) {
3081 if (engine()->vie()->rtp()->SetReceiverBufferingMode(
3082 channel_id, buffer_latency) != 0) {
3083 LOG_RTCERR2(SetReceiverBufferingMode, channel_id, buffer_latency);
3084 }
3085 }
3086
3087 if (render_started_) {
3088 if (engine_->vie()->render()->StartRender(channel_id) != 0) {
3089 LOG_RTCERR1(StartRender, channel_id);
3090 return false;
3091 }
3092 }
3093
3094 // Register decoder observer for incoming framerate and bitrate.
3095 if (engine()->vie()->codec()->RegisterDecoderObserver(
3096 channel_id, *channel_info->decoder_observer()) != 0) {
3097 LOG_RTCERR1(RegisterDecoderObserver, channel_info->decoder_observer());
3098 return false;
3099 }
3100
3101 recv_channels_[remote_ssrc_key] = channel_info.release();
3102 return true;
3103}
3104
3105bool WebRtcVideoMediaChannel::ConfigureSending(int channel_id,
3106 uint32 local_ssrc_key) {
3107 // The ssrc key can be zero or correspond to an SSRC.
3108 // Make sure the default channel isn't configured more than once.
3109 if (local_ssrc_key == 0 && send_channels_.find(0) != send_channels_.end()) {
3110 return false;
3111 }
3112 // Make sure that the SSRC is not already in use.
3113 uint32 dummy_key;
3114 if (GetSendChannelKey(local_ssrc_key, &dummy_key)) {
3115 return false;
3116 }
3117 int vie_capture = 0;
3118 webrtc::ViEExternalCapture* external_capture = NULL;
3119 // Register external capture.
3120 if (engine()->vie()->capture()->AllocateExternalCaptureDevice(
3121 vie_capture, external_capture) != 0) {
3122 LOG_RTCERR0(AllocateExternalCaptureDevice);
3123 return false;
3124 }
3125
3126 // Connect external capture.
3127 if (engine()->vie()->capture()->ConnectCaptureDevice(
3128 vie_capture, channel_id) != 0) {
3129 LOG_RTCERR2(ConnectCaptureDevice, vie_capture, channel_id);
3130 return false;
3131 }
3132 talk_base::scoped_ptr<WebRtcVideoChannelSendInfo> send_channel(
3133 new WebRtcVideoChannelSendInfo(channel_id, vie_capture,
3134 external_capture,
3135 engine()->cpu_monitor()));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00003136 if (engine()->vie()->base()->RegisterCpuOveruseObserver(
3137 channel_id, send_channel->overuse_observer())) {
3138 LOG_RTCERR1(RegisterCpuOveruseObserver, channel_id);
3139 return false;
3140 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003141 send_channel->ApplyCpuOptions(options_);
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00003142 send_channel->SignalCpuAdaptationUnable.connect(this,
3143 &WebRtcVideoMediaChannel::OnCpuAdaptationUnable);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003144
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00003145 if (options_.cpu_overuse_detection.GetWithDefaultIfUnset(false)) {
3146 send_channel->SetCpuOveruseDetection(true);
3147 }
3148
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003149 // Register encoder observer for outgoing framerate and bitrate.
3150 if (engine()->vie()->codec()->RegisterEncoderObserver(
3151 channel_id, *send_channel->encoder_observer()) != 0) {
3152 LOG_RTCERR1(RegisterEncoderObserver, send_channel->encoder_observer());
3153 return false;
3154 }
3155
3156 if (!SetHeaderExtension(&webrtc::ViERTP_RTCP::SetSendTimestampOffsetStatus,
3157 channel_id, send_extensions_, kRtpTimestampOffsetHeaderExtension)) {
3158 return false;
3159 }
3160
3161 if (!SetHeaderExtension(&webrtc::ViERTP_RTCP::SetSendAbsoluteSendTimeStatus,
3162 channel_id, send_extensions_, kRtpAbsoluteSendTimeHeaderExtension)) {
3163 return false;
3164 }
3165
3166 if (options_.video_leaky_bucket.GetWithDefaultIfUnset(false)) {
3167 if (engine()->vie()->rtp()->SetTransmissionSmoothingStatus(channel_id,
3168 true) != 0) {
3169 LOG_RTCERR2(SetTransmissionSmoothingStatus, channel_id, true);
3170 return false;
3171 }
3172 }
3173
3174 int buffer_latency =
3175 options_.buffered_mode_latency.GetWithDefaultIfUnset(
3176 cricket::kBufferedModeDisabled);
3177 if (buffer_latency != cricket::kBufferedModeDisabled) {
3178 if (engine()->vie()->rtp()->SetSenderBufferingMode(
3179 channel_id, buffer_latency) != 0) {
3180 LOG_RTCERR2(SetSenderBufferingMode, channel_id, buffer_latency);
3181 }
3182 }
3183 // The remb status direction correspond to the RTP stream (and not the RTCP
3184 // stream). I.e. if send remb is enabled it means it is receiving remote
3185 // rembs and should use them to estimate bandwidth. Receive remb mean that
3186 // remb packets will be generated and that the channel should be included in
3187 // it. If remb is enabled all channels are allowed to contribute to the remb
3188 // but only receive channels will ever end up actually contributing. This
3189 // keeps the logic simple.
3190 if (engine_->vie()->rtp()->SetRembStatus(channel_id,
3191 remb_enabled_,
3192 remb_enabled_) != 0) {
3193 LOG_RTCERR3(SetRembStatus, channel_id, remb_enabled_, remb_enabled_);
3194 return false;
3195 }
3196 if (!SetNackFec(channel_id, send_red_type_, send_fec_type_, nack_enabled_)) {
3197 // Logged in SetNackFec. Don't spam the logs.
3198 return false;
3199 }
3200
3201 send_channels_[local_ssrc_key] = send_channel.release();
3202
3203 return true;
3204}
3205
3206bool WebRtcVideoMediaChannel::SetNackFec(int channel_id,
3207 int red_payload_type,
3208 int fec_payload_type,
3209 bool nack_enabled) {
3210 bool enable = (red_payload_type != -1 && fec_payload_type != -1 &&
3211 !InConferenceMode());
3212 if (enable) {
3213 if (engine_->vie()->rtp()->SetHybridNACKFECStatus(
3214 channel_id, nack_enabled, red_payload_type, fec_payload_type) != 0) {
3215 LOG_RTCERR4(SetHybridNACKFECStatus,
3216 channel_id, nack_enabled, red_payload_type, fec_payload_type);
3217 return false;
3218 }
3219 LOG(LS_INFO) << "Hybrid NACK/FEC enabled for channel " << channel_id;
3220 } else {
3221 if (engine_->vie()->rtp()->SetNACKStatus(channel_id, nack_enabled) != 0) {
3222 LOG_RTCERR1(SetNACKStatus, channel_id);
3223 return false;
3224 }
3225 LOG(LS_INFO) << "NACK enabled for channel " << channel_id;
3226 }
3227 return true;
3228}
3229
3230bool WebRtcVideoMediaChannel::SetSendCodec(const webrtc::VideoCodec& codec,
3231 int min_bitrate,
3232 int start_bitrate,
3233 int max_bitrate) {
3234 bool ret_val = true;
3235 for (SendChannelMap::iterator iter = send_channels_.begin();
3236 iter != send_channels_.end(); ++iter) {
3237 WebRtcVideoChannelSendInfo* send_channel = iter->second;
3238 ret_val = SetSendCodec(send_channel, codec, min_bitrate, start_bitrate,
3239 max_bitrate) && ret_val;
3240 }
3241 if (ret_val) {
3242 // All SetSendCodec calls were successful. Update the global state
3243 // accordingly.
3244 send_codec_.reset(new webrtc::VideoCodec(codec));
3245 send_min_bitrate_ = min_bitrate;
3246 send_start_bitrate_ = start_bitrate;
3247 send_max_bitrate_ = max_bitrate;
3248 } else {
3249 // At least one SetSendCodec call failed, rollback.
3250 for (SendChannelMap::iterator iter = send_channels_.begin();
3251 iter != send_channels_.end(); ++iter) {
3252 WebRtcVideoChannelSendInfo* send_channel = iter->second;
3253 if (send_codec_) {
3254 SetSendCodec(send_channel, *send_codec_.get(), send_min_bitrate_,
3255 send_start_bitrate_, send_max_bitrate_);
3256 }
3257 }
3258 }
3259 return ret_val;
3260}
3261
3262bool WebRtcVideoMediaChannel::SetSendCodec(
3263 WebRtcVideoChannelSendInfo* send_channel,
3264 const webrtc::VideoCodec& codec,
3265 int min_bitrate,
3266 int start_bitrate,
3267 int max_bitrate) {
3268 if (!send_channel) {
3269 return false;
3270 }
3271 const int channel_id = send_channel->channel_id();
3272 // Make a copy of the codec
3273 webrtc::VideoCodec target_codec = codec;
3274 target_codec.startBitrate = start_bitrate;
3275 target_codec.minBitrate = min_bitrate;
3276 target_codec.maxBitrate = max_bitrate;
3277
3278 // Set the default number of temporal layers for VP8.
3279 if (webrtc::kVideoCodecVP8 == codec.codecType) {
3280 target_codec.codecSpecific.VP8.numberOfTemporalLayers =
3281 kDefaultNumberOfTemporalLayers;
3282
3283 // Turn off the VP8 error resilience
3284 target_codec.codecSpecific.VP8.resilience = webrtc::kResilienceOff;
3285
3286 bool enable_denoising =
3287 options_.video_noise_reduction.GetWithDefaultIfUnset(false);
3288 target_codec.codecSpecific.VP8.denoisingOn = enable_denoising;
3289 }
3290
3291 // Register external encoder if codec type is supported by encoder factory.
3292 if (engine()->IsExternalEncoderCodecType(codec.codecType) &&
3293 !send_channel->IsEncoderRegistered(target_codec.plType)) {
3294 webrtc::VideoEncoder* encoder =
3295 engine()->CreateExternalEncoder(codec.codecType);
3296 if (encoder) {
3297 if (engine()->vie()->ext_codec()->RegisterExternalSendCodec(
3298 channel_id, target_codec.plType, encoder, false) == 0) {
3299 send_channel->RegisterEncoder(target_codec.plType, encoder);
3300 } else {
3301 LOG_RTCERR2(RegisterExternalSendCodec, channel_id, target_codec.plName);
3302 engine()->DestroyExternalEncoder(encoder);
3303 }
3304 }
3305 }
3306
3307 // Resolution and framerate may vary for different send channels.
3308 const VideoFormat& video_format = send_channel->video_format();
3309 UpdateVideoCodec(video_format, &target_codec);
3310
3311 if (target_codec.width == 0 && target_codec.height == 0) {
3312 const uint32 ssrc = send_channel->stream_params()->first_ssrc();
3313 LOG(LS_INFO) << "0x0 resolution selected. Captured frames will be dropped "
3314 << "for ssrc: " << ssrc << ".";
3315 } else {
3316 MaybeChangeStartBitrate(channel_id, &target_codec);
3317 if (0 != engine()->vie()->codec()->SetSendCodec(channel_id, target_codec)) {
3318 LOG_RTCERR2(SetSendCodec, channel_id, target_codec.plName);
3319 return false;
3320 }
3321
3322 }
3323 send_channel->set_interval(
3324 cricket::VideoFormat::FpsToInterval(target_codec.maxFramerate));
3325 return true;
3326}
3327
3328
3329static std::string ToString(webrtc::VideoCodecComplexity complexity) {
3330 switch (complexity) {
3331 case webrtc::kComplexityNormal:
3332 return "normal";
3333 case webrtc::kComplexityHigh:
3334 return "high";
3335 case webrtc::kComplexityHigher:
3336 return "higher";
3337 case webrtc::kComplexityMax:
3338 return "max";
3339 default:
3340 return "unknown";
3341 }
3342}
3343
3344static std::string ToString(webrtc::VP8ResilienceMode resilience) {
3345 switch (resilience) {
3346 case webrtc::kResilienceOff:
3347 return "off";
3348 case webrtc::kResilientStream:
3349 return "stream";
3350 case webrtc::kResilientFrames:
3351 return "frames";
3352 default:
3353 return "unknown";
3354 }
3355}
3356
3357void WebRtcVideoMediaChannel::LogSendCodecChange(const std::string& reason) {
3358 webrtc::VideoCodec vie_codec;
3359 if (engine()->vie()->codec()->GetSendCodec(vie_channel_, vie_codec) != 0) {
3360 LOG_RTCERR1(GetSendCodec, vie_channel_);
3361 return;
3362 }
3363
3364 LOG(LS_INFO) << reason << " : selected video codec "
3365 << vie_codec.plName << "/"
3366 << vie_codec.width << "x" << vie_codec.height << "x"
3367 << static_cast<int>(vie_codec.maxFramerate) << "fps"
3368 << "@" << vie_codec.maxBitrate << "kbps"
3369 << " (min=" << vie_codec.minBitrate << "kbps,"
3370 << " start=" << vie_codec.startBitrate << "kbps)";
3371 LOG(LS_INFO) << "Video max quantization: " << vie_codec.qpMax;
3372 if (webrtc::kVideoCodecVP8 == vie_codec.codecType) {
3373 LOG(LS_INFO) << "VP8 number of temporal layers: "
3374 << static_cast<int>(
3375 vie_codec.codecSpecific.VP8.numberOfTemporalLayers);
3376 LOG(LS_INFO) << "VP8 options : "
3377 << "picture loss indication = "
3378 << vie_codec.codecSpecific.VP8.pictureLossIndicationOn
3379 << ", feedback mode = "
3380 << vie_codec.codecSpecific.VP8.feedbackModeOn
3381 << ", complexity = "
3382 << ToString(vie_codec.codecSpecific.VP8.complexity)
3383 << ", resilience = "
3384 << ToString(vie_codec.codecSpecific.VP8.resilience)
3385 << ", denoising = "
3386 << vie_codec.codecSpecific.VP8.denoisingOn
3387 << ", error concealment = "
3388 << vie_codec.codecSpecific.VP8.errorConcealmentOn
3389 << ", automatic resize = "
3390 << vie_codec.codecSpecific.VP8.automaticResizeOn
3391 << ", frame dropping = "
3392 << vie_codec.codecSpecific.VP8.frameDroppingOn
3393 << ", key frame interval = "
3394 << vie_codec.codecSpecific.VP8.keyFrameInterval;
3395 }
3396
3397}
3398
3399bool WebRtcVideoMediaChannel::SetReceiveCodecs(
3400 WebRtcVideoChannelRecvInfo* info) {
3401 int red_type = -1;
3402 int fec_type = -1;
3403 int channel_id = info->channel_id();
3404 for (std::vector<webrtc::VideoCodec>::iterator it = receive_codecs_.begin();
3405 it != receive_codecs_.end(); ++it) {
3406 if (it->codecType == webrtc::kVideoCodecRED) {
3407 red_type = it->plType;
3408 } else if (it->codecType == webrtc::kVideoCodecULPFEC) {
3409 fec_type = it->plType;
3410 }
3411 if (engine()->vie()->codec()->SetReceiveCodec(channel_id, *it) != 0) {
3412 LOG_RTCERR2(SetReceiveCodec, channel_id, it->plName);
3413 return false;
3414 }
3415 if (!info->IsDecoderRegistered(it->plType) &&
3416 it->codecType != webrtc::kVideoCodecRED &&
3417 it->codecType != webrtc::kVideoCodecULPFEC) {
3418 webrtc::VideoDecoder* decoder =
3419 engine()->CreateExternalDecoder(it->codecType);
3420 if (decoder) {
3421 if (engine()->vie()->ext_codec()->RegisterExternalReceiveCodec(
3422 channel_id, it->plType, decoder) == 0) {
3423 info->RegisterDecoder(it->plType, decoder);
3424 } else {
3425 LOG_RTCERR2(RegisterExternalReceiveCodec, channel_id, it->plName);
3426 engine()->DestroyExternalDecoder(decoder);
3427 }
3428 }
3429 }
3430 }
3431
3432 // Start receiving packets if at least one receive codec has been set.
3433 if (!receive_codecs_.empty()) {
3434 if (engine()->vie()->base()->StartReceive(channel_id) != 0) {
3435 LOG_RTCERR1(StartReceive, channel_id);
3436 return false;
3437 }
3438 }
3439 return true;
3440}
3441
3442int WebRtcVideoMediaChannel::GetRecvChannelNum(uint32 ssrc) {
3443 if (ssrc == first_receive_ssrc_) {
3444 return vie_channel_;
3445 }
3446 RecvChannelMap::iterator it = recv_channels_.find(ssrc);
3447 return (it != recv_channels_.end()) ? it->second->channel_id() : -1;
3448}
3449
3450// If the new frame size is different from the send codec size we set on vie,
3451// we need to reset the send codec on vie.
3452// The new send codec size should not exceed send_codec_ which is controlled
3453// only by the 'jec' logic.
3454bool WebRtcVideoMediaChannel::MaybeResetVieSendCodec(
3455 WebRtcVideoChannelSendInfo* send_channel,
3456 int new_width,
3457 int new_height,
3458 bool is_screencast,
3459 bool* reset) {
3460 if (reset) {
3461 *reset = false;
3462 }
3463 ASSERT(send_codec_.get() != NULL);
3464
3465 webrtc::VideoCodec target_codec = *send_codec_.get();
3466 const VideoFormat& video_format = send_channel->video_format();
3467 UpdateVideoCodec(video_format, &target_codec);
3468
3469 // Vie send codec size should not exceed target_codec.
3470 int target_width = new_width;
3471 int target_height = new_height;
3472 if (!is_screencast &&
3473 (new_width > target_codec.width || new_height > target_codec.height)) {
3474 target_width = target_codec.width;
3475 target_height = target_codec.height;
3476 }
3477
3478 // Get current vie codec.
3479 webrtc::VideoCodec vie_codec;
3480 const int channel_id = send_channel->channel_id();
3481 if (engine()->vie()->codec()->GetSendCodec(channel_id, vie_codec) != 0) {
3482 LOG_RTCERR1(GetSendCodec, channel_id);
3483 return false;
3484 }
3485 const int cur_width = vie_codec.width;
3486 const int cur_height = vie_codec.height;
3487
3488 // Only reset send codec when there is a size change. Additionally,
3489 // automatic resize needs to be turned off when screencasting and on when
3490 // not screencasting.
3491 // Don't allow automatic resizing for screencasting.
3492 bool automatic_resize = !is_screencast;
3493 // Turn off VP8 frame dropping when screensharing as the current model does
3494 // not work well at low fps.
3495 bool vp8_frame_dropping = !is_screencast;
3496 // Disable denoising for screencasting.
3497 bool enable_denoising =
3498 options_.video_noise_reduction.GetWithDefaultIfUnset(false);
3499 bool denoising = !is_screencast && enable_denoising;
3500 bool reset_send_codec =
3501 target_width != cur_width || target_height != cur_height ||
3502 automatic_resize != vie_codec.codecSpecific.VP8.automaticResizeOn ||
3503 denoising != vie_codec.codecSpecific.VP8.denoisingOn ||
3504 vp8_frame_dropping != vie_codec.codecSpecific.VP8.frameDroppingOn;
3505
3506 if (reset_send_codec) {
3507 // Set the new codec on vie.
3508 vie_codec.width = target_width;
3509 vie_codec.height = target_height;
3510 vie_codec.maxFramerate = target_codec.maxFramerate;
3511 vie_codec.startBitrate = target_codec.startBitrate;
3512 vie_codec.codecSpecific.VP8.automaticResizeOn = automatic_resize;
3513 vie_codec.codecSpecific.VP8.denoisingOn = denoising;
3514 vie_codec.codecSpecific.VP8.frameDroppingOn = vp8_frame_dropping;
3515 // TODO(mflodman): Remove 'is_screencast' check when screen cast settings
3516 // are treated correctly in WebRTC.
3517 if (!is_screencast)
3518 MaybeChangeStartBitrate(channel_id, &vie_codec);
3519
3520 if (engine()->vie()->codec()->SetSendCodec(channel_id, vie_codec) != 0) {
3521 LOG_RTCERR1(SetSendCodec, channel_id);
3522 return false;
3523 }
3524 if (reset) {
3525 *reset = true;
3526 }
3527 LogSendCodecChange("Capture size changed");
3528 }
3529
3530 return true;
3531}
3532
3533void WebRtcVideoMediaChannel::MaybeChangeStartBitrate(
3534 int channel_id, webrtc::VideoCodec* video_codec) {
3535 if (video_codec->startBitrate < video_codec->minBitrate) {
3536 video_codec->startBitrate = video_codec->minBitrate;
3537 } else if (video_codec->startBitrate > video_codec->maxBitrate) {
3538 video_codec->startBitrate = video_codec->maxBitrate;
3539 }
3540
3541 // Use a previous target bitrate, if there is one.
3542 unsigned int current_target_bitrate = 0;
3543 if (engine()->vie()->codec()->GetCodecTargetBitrate(
3544 channel_id, &current_target_bitrate) == 0) {
3545 // Convert to kbps.
3546 current_target_bitrate /= 1000;
3547 if (current_target_bitrate > video_codec->maxBitrate) {
3548 current_target_bitrate = video_codec->maxBitrate;
3549 }
3550 if (current_target_bitrate > video_codec->startBitrate) {
3551 video_codec->startBitrate = current_target_bitrate;
3552 }
3553 }
3554}
3555
3556void WebRtcVideoMediaChannel::OnMessage(talk_base::Message* msg) {
3557 FlushBlackFrameData* black_frame_data =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00003558 static_cast<FlushBlackFrameData*>(msg->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003559 FlushBlackFrame(black_frame_data->ssrc, black_frame_data->timestamp);
3560 delete black_frame_data;
3561}
3562
3563int WebRtcVideoMediaChannel::SendPacket(int channel, const void* data,
3564 int len) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003565 talk_base::Buffer packet(data, len, kMaxRtpPacketLen);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00003566 return MediaChannel::SendPacket(&packet) ? len : -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003567}
3568
3569int WebRtcVideoMediaChannel::SendRTCPPacket(int channel,
3570 const void* data,
3571 int len) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003572 talk_base::Buffer packet(data, len, kMaxRtpPacketLen);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00003573 return MediaChannel::SendRtcp(&packet) ? len : -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003574}
3575
3576void WebRtcVideoMediaChannel::QueueBlackFrame(uint32 ssrc, int64 timestamp,
3577 int framerate) {
3578 if (timestamp) {
3579 FlushBlackFrameData* black_frame_data = new FlushBlackFrameData(
3580 ssrc,
3581 timestamp);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00003582 const int delay_ms = static_cast<int>(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003583 2 * cricket::VideoFormat::FpsToInterval(framerate) *
3584 talk_base::kNumMillisecsPerSec / talk_base::kNumNanosecsPerSec);
3585 worker_thread()->PostDelayed(delay_ms, this, 0, black_frame_data);
3586 }
3587}
3588
3589void WebRtcVideoMediaChannel::FlushBlackFrame(uint32 ssrc, int64 timestamp) {
3590 WebRtcVideoChannelSendInfo* send_channel = GetSendChannel(ssrc);
3591 if (!send_channel) {
3592 return;
3593 }
3594 talk_base::scoped_ptr<const VideoFrame> black_frame_ptr;
3595
3596 const WebRtcLocalStreamInfo* channel_stream_info =
3597 send_channel->local_stream_info();
3598 int64 last_frame_time_stamp = channel_stream_info->time_stamp();
3599 if (last_frame_time_stamp == timestamp) {
3600 size_t last_frame_width = 0;
3601 size_t last_frame_height = 0;
3602 int64 last_frame_elapsed_time = 0;
3603 channel_stream_info->GetLastFrameInfo(&last_frame_width, &last_frame_height,
3604 &last_frame_elapsed_time);
3605 if (!last_frame_width || !last_frame_height) {
3606 return;
3607 }
3608 WebRtcVideoFrame black_frame;
3609 // Black frame is not screencast.
3610 const bool screencasting = false;
3611 const int64 timestamp_delta = send_channel->interval();
3612 if (!black_frame.InitToBlack(send_codec_->width, send_codec_->height, 1, 1,
3613 last_frame_elapsed_time + timestamp_delta,
3614 last_frame_time_stamp + timestamp_delta) ||
3615 !SendFrame(send_channel, &black_frame, screencasting)) {
3616 LOG(LS_ERROR) << "Failed to send black frame.";
3617 }
3618 }
3619}
3620
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00003621void WebRtcVideoMediaChannel::OnCpuAdaptationUnable() {
3622 // ssrc is hardcoded to 0. This message is based on a system wide issue,
3623 // so finding which ssrc caused it doesn't matter.
3624 SignalMediaError(0, VideoMediaChannel::ERROR_REC_CPU_MAX_CANT_DOWNGRADE);
3625}
3626
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003627void WebRtcVideoMediaChannel::SetNetworkTransmissionState(
3628 bool is_transmitting) {
3629 LOG(LS_INFO) << "SetNetworkTransmissionState: " << is_transmitting;
3630 for (SendChannelMap::iterator iter = send_channels_.begin();
3631 iter != send_channels_.end(); ++iter) {
3632 WebRtcVideoChannelSendInfo* send_channel = iter->second;
3633 int channel_id = send_channel->channel_id();
3634 engine_->vie()->network()->SetNetworkTransmissionState(channel_id,
3635 is_transmitting);
3636 }
3637}
3638
3639bool WebRtcVideoMediaChannel::SetHeaderExtension(ExtensionSetterFunction setter,
3640 int channel_id, const RtpHeaderExtension* extension) {
3641 bool enable = false;
3642 int id = 0;
3643 if (extension) {
3644 enable = true;
3645 id = extension->id;
3646 }
3647 if ((engine_->vie()->rtp()->*setter)(channel_id, enable, id) != 0) {
3648 LOG_RTCERR4(*setter, extension->uri, channel_id, enable, id);
3649 return false;
3650 }
3651 return true;
3652}
3653
3654bool WebRtcVideoMediaChannel::SetHeaderExtension(ExtensionSetterFunction setter,
3655 int channel_id, const std::vector<RtpHeaderExtension>& extensions,
3656 const char header_extension_uri[]) {
3657 const RtpHeaderExtension* extension = FindHeaderExtension(extensions,
3658 header_extension_uri);
3659 return SetHeaderExtension(setter, channel_id, extension);
3660}
3661} // namespace cricket
3662
3663#endif // HAVE_WEBRTC_VIDEO