blob: f33d6f06fe1e0836346b73703e665184aca55a8d [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.org9caf2762013-12-11 18:25:07 +0000231 uint32_t time_stamp, int64_t render_time,
232 void* handle) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000233 talk_base::CritScope cs(&crit_);
234 frame_rate_tracker_.Update(1);
235 if (renderer_ == NULL) {
236 return 0;
237 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000238 // Convert 90K rtp timestamp to ns timestamp.
239 int64 rtp_time_stamp_in_ns = (time_stamp / 90) *
240 talk_base::kNumNanosecsPerMillisec;
241 // Convert milisecond render time to ns timestamp.
242 int64 render_time_stamp_in_ns = render_time *
243 talk_base::kNumNanosecsPerMillisec;
244 // Send the rtp timestamp to renderer as the VideoFrame timestamp.
245 // and the render timestamp as the VideoFrame elapsed_time.
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000246 if (handle == NULL) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000247 return DeliverBufferFrame(buffer, buffer_size, render_time_stamp_in_ns,
248 rtp_time_stamp_in_ns);
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000249 } else {
250 return DeliverTextureFrame(handle, render_time_stamp_in_ns,
251 rtp_time_stamp_in_ns);
252 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000253 }
254
255 virtual bool IsTextureSupported() { return true; }
256
257 int DeliverBufferFrame(unsigned char* buffer, int buffer_size,
258 int64 elapsed_time, int64 time_stamp) {
259 WebRtcVideoFrame video_frame;
wu@webrtc.org16d62542013-11-05 23:45:14 +0000260 video_frame.Alias(buffer, buffer_size, width_, height_,
261 1, 1, elapsed_time, time_stamp, 0);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000262
263
264 // Sanity check on decoded frame size.
265 if (buffer_size != static_cast<int>(VideoFrame::SizeOf(width_, height_))) {
266 LOG(LS_WARNING) << "WebRtcRenderAdapter received a strange frame size: "
267 << buffer_size;
268 }
269
270 int ret = renderer_->RenderFrame(&video_frame) ? 0 : -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000271 return ret;
272 }
273
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000274 int DeliverTextureFrame(void* handle, int64 elapsed_time, int64 time_stamp) {
275 WebRtcTextureVideoFrame video_frame(
276 static_cast<webrtc::NativeHandle*>(handle), width_, height_,
277 elapsed_time, time_stamp);
278 return renderer_->RenderFrame(&video_frame);
279 }
280
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000281 unsigned int width() {
282 talk_base::CritScope cs(&crit_);
283 return width_;
284 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000285
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000286 unsigned int height() {
287 talk_base::CritScope cs(&crit_);
288 return height_;
289 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000290
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000291 int framerate() {
292 talk_base::CritScope cs(&crit_);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000293 return static_cast<int>(frame_rate_tracker_.units_second());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000294 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000295
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000296 VideoRenderer* renderer() {
297 talk_base::CritScope cs(&crit_);
298 return renderer_;
299 }
300
301 private:
302 talk_base::CriticalSection crit_;
303 VideoRenderer* renderer_;
304 unsigned int width_;
305 unsigned int height_;
306 talk_base::RateTracker frame_rate_tracker_;
307 bool watermark_enabled_;
308};
309
310class WebRtcDecoderObserver : public webrtc::ViEDecoderObserver {
311 public:
312 explicit WebRtcDecoderObserver(int video_channel)
313 : video_channel_(video_channel),
314 framerate_(0),
315 bitrate_(0),
wu@webrtc.org97077a32013-10-25 21:18:33 +0000316 decode_ms_(0),
317 max_decode_ms_(0),
318 current_delay_ms_(0),
319 target_delay_ms_(0),
320 jitter_buffer_ms_(0),
321 min_playout_delay_ms_(0),
322 render_delay_ms_(0),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000323 firs_requested_(0) {
324 }
325
326 // virtual functions from VieDecoderObserver.
327 virtual void IncomingCodecChanged(const int videoChannel,
328 const webrtc::VideoCodec& videoCodec) {}
329 virtual void IncomingRate(const int videoChannel,
330 const unsigned int framerate,
331 const unsigned int bitrate) {
wu@webrtc.org78187522013-10-07 23:32:02 +0000332 talk_base::CritScope cs(&crit_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000333 ASSERT(video_channel_ == videoChannel);
334 framerate_ = framerate;
335 bitrate_ = bitrate;
336 }
wu@webrtc.org97077a32013-10-25 21:18:33 +0000337
338 virtual void DecoderTiming(int decode_ms,
339 int max_decode_ms,
340 int current_delay_ms,
341 int target_delay_ms,
342 int jitter_buffer_ms,
343 int min_playout_delay_ms,
344 int render_delay_ms) {
345 talk_base::CritScope cs(&crit_);
346 decode_ms_ = decode_ms;
347 max_decode_ms_ = max_decode_ms;
348 current_delay_ms_ = current_delay_ms;
349 target_delay_ms_ = target_delay_ms;
350 jitter_buffer_ms_ = jitter_buffer_ms;
351 min_playout_delay_ms_ = min_playout_delay_ms;
352 render_delay_ms_ = render_delay_ms;
353 }
354
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000355 virtual void RequestNewKeyFrame(const int videoChannel) {
wu@webrtc.org78187522013-10-07 23:32:02 +0000356 talk_base::CritScope cs(&crit_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000357 ASSERT(video_channel_ == videoChannel);
358 ++firs_requested_;
359 }
360
wu@webrtc.org97077a32013-10-25 21:18:33 +0000361 // Populate |rinfo| based on previously-set data in |*this|.
362 void ExportTo(VideoReceiverInfo* rinfo) {
wu@webrtc.org78187522013-10-07 23:32:02 +0000363 talk_base::CritScope cs(&crit_);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000364 rinfo->firs_sent = firs_requested_;
365 rinfo->framerate_rcvd = framerate_;
366 rinfo->decode_ms = decode_ms_;
367 rinfo->max_decode_ms = max_decode_ms_;
368 rinfo->current_delay_ms = current_delay_ms_;
369 rinfo->target_delay_ms = target_delay_ms_;
370 rinfo->jitter_buffer_ms = jitter_buffer_ms_;
371 rinfo->min_playout_delay_ms = min_playout_delay_ms_;
372 rinfo->render_delay_ms = render_delay_ms_;
wu@webrtc.org78187522013-10-07 23:32:02 +0000373 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000374
375 private:
wu@webrtc.org78187522013-10-07 23:32:02 +0000376 mutable talk_base::CriticalSection crit_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000377 int video_channel_;
378 int framerate_;
379 int bitrate_;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000380 int decode_ms_;
381 int max_decode_ms_;
382 int current_delay_ms_;
383 int target_delay_ms_;
384 int jitter_buffer_ms_;
385 int min_playout_delay_ms_;
386 int render_delay_ms_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000387 int firs_requested_;
388};
389
390class WebRtcEncoderObserver : public webrtc::ViEEncoderObserver {
391 public:
392 explicit WebRtcEncoderObserver(int video_channel)
393 : video_channel_(video_channel),
394 framerate_(0),
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000395 bitrate_(0),
396 suspended_(false) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000397 }
398
399 // virtual functions from VieEncoderObserver.
400 virtual void OutgoingRate(const int videoChannel,
401 const unsigned int framerate,
402 const unsigned int bitrate) {
wu@webrtc.org78187522013-10-07 23:32:02 +0000403 talk_base::CritScope cs(&crit_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000404 ASSERT(video_channel_ == videoChannel);
405 framerate_ = framerate;
406 bitrate_ = bitrate;
407 }
408
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000409 virtual void SuspendChange(int video_channel, bool is_suspended) {
410 talk_base::CritScope cs(&crit_);
411 ASSERT(video_channel_ == video_channel);
412 suspended_ = is_suspended;
413 }
414
wu@webrtc.org78187522013-10-07 23:32:02 +0000415 int framerate() const {
416 talk_base::CritScope cs(&crit_);
417 return framerate_;
418 }
419 int bitrate() const {
420 talk_base::CritScope cs(&crit_);
421 return bitrate_;
422 }
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000423 bool suspended() const {
424 talk_base::CritScope cs(&crit_);
425 return suspended_;
426 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000427
428 private:
wu@webrtc.org78187522013-10-07 23:32:02 +0000429 mutable talk_base::CriticalSection crit_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000430 int video_channel_;
431 int framerate_;
432 int bitrate_;
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000433 bool suspended_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000434};
435
436class WebRtcLocalStreamInfo {
437 public:
438 WebRtcLocalStreamInfo()
439 : width_(0), height_(0), elapsed_time_(-1), time_stamp_(-1) {}
440 size_t width() const {
441 talk_base::CritScope cs(&crit_);
442 return width_;
443 }
444 size_t height() const {
445 talk_base::CritScope cs(&crit_);
446 return height_;
447 }
448 int64 elapsed_time() const {
449 talk_base::CritScope cs(&crit_);
450 return elapsed_time_;
451 }
452 int64 time_stamp() const {
453 talk_base::CritScope cs(&crit_);
454 return time_stamp_;
455 }
456 int framerate() {
457 talk_base::CritScope cs(&crit_);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000458 return static_cast<int>(rate_tracker_.units_second());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000459 }
460 void GetLastFrameInfo(
461 size_t* width, size_t* height, int64* elapsed_time) const {
462 talk_base::CritScope cs(&crit_);
463 *width = width_;
464 *height = height_;
465 *elapsed_time = elapsed_time_;
466 }
467
468 void UpdateFrame(const VideoFrame* frame) {
469 talk_base::CritScope cs(&crit_);
470
471 width_ = frame->GetWidth();
472 height_ = frame->GetHeight();
473 elapsed_time_ = frame->GetElapsedTime();
474 time_stamp_ = frame->GetTimeStamp();
475
476 rate_tracker_.Update(1);
477 }
478
479 private:
480 mutable talk_base::CriticalSection crit_;
481 size_t width_;
482 size_t height_;
483 int64 elapsed_time_;
484 int64 time_stamp_;
485 talk_base::RateTracker rate_tracker_;
486
487 DISALLOW_COPY_AND_ASSIGN(WebRtcLocalStreamInfo);
488};
489
490// WebRtcVideoChannelRecvInfo is a container class with members such as renderer
491// and a decoder observer that is used by receive channels.
492// It must exist as long as the receive channel is connected to renderer or a
493// decoder observer in this class and methods in the class should only be called
494// from the worker thread.
495class WebRtcVideoChannelRecvInfo {
496 public:
497 typedef std::map<int, webrtc::VideoDecoder*> DecoderMap; // key: payload type
498 explicit WebRtcVideoChannelRecvInfo(int channel_id)
499 : channel_id_(channel_id),
500 render_adapter_(NULL),
501 decoder_observer_(channel_id) {
502 }
503 int channel_id() { return channel_id_; }
504 void SetRenderer(VideoRenderer* renderer) {
505 render_adapter_.SetRenderer(renderer);
506 }
507 WebRtcRenderAdapter* render_adapter() { return &render_adapter_; }
508 WebRtcDecoderObserver* decoder_observer() { return &decoder_observer_; }
509 void RegisterDecoder(int pl_type, webrtc::VideoDecoder* decoder) {
510 ASSERT(!IsDecoderRegistered(pl_type));
511 registered_decoders_[pl_type] = decoder;
512 }
513 bool IsDecoderRegistered(int pl_type) {
514 return registered_decoders_.count(pl_type) != 0;
515 }
516 const DecoderMap& registered_decoders() {
517 return registered_decoders_;
518 }
519 void ClearRegisteredDecoders() {
520 registered_decoders_.clear();
521 }
522
523 private:
524 int channel_id_; // Webrtc video channel number.
525 // Renderer for this channel.
526 WebRtcRenderAdapter render_adapter_;
527 WebRtcDecoderObserver decoder_observer_;
528 DecoderMap registered_decoders_;
529};
530
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000531class WebRtcOveruseObserver : public webrtc::CpuOveruseObserver {
532 public:
533 explicit WebRtcOveruseObserver(CoordinatedVideoAdapter* video_adapter)
534 : video_adapter_(video_adapter),
535 enabled_(false) {
536 }
537
538 // TODO(mflodman): Consider sending resolution as part of event, to let
539 // adapter know what resolution the request is based on. Helps eliminate stale
540 // data, race conditions.
541 virtual void OveruseDetected() OVERRIDE {
542 talk_base::CritScope cs(&crit_);
543 if (!enabled_) {
544 return;
545 }
546
547 video_adapter_->OnCpuResolutionRequest(CoordinatedVideoAdapter::DOWNGRADE);
548 }
549
550 virtual void NormalUsage() OVERRIDE {
551 talk_base::CritScope cs(&crit_);
552 if (!enabled_) {
553 return;
554 }
555
556 video_adapter_->OnCpuResolutionRequest(CoordinatedVideoAdapter::UPGRADE);
557 }
558
559 void Enable(bool enable) {
560 talk_base::CritScope cs(&crit_);
561 enabled_ = enable;
562 }
563
564 private:
565 CoordinatedVideoAdapter* video_adapter_;
566 bool enabled_;
567 talk_base::CriticalSection crit_;
568};
569
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000570
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000571class WebRtcVideoChannelSendInfo : public sigslot::has_slots<> {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000572 public:
573 typedef std::map<int, webrtc::VideoEncoder*> EncoderMap; // key: payload type
574 WebRtcVideoChannelSendInfo(int channel_id, int capture_id,
575 webrtc::ViEExternalCapture* external_capture,
576 talk_base::CpuMonitor* cpu_monitor)
577 : channel_id_(channel_id),
578 capture_id_(capture_id),
579 sending_(false),
580 muted_(false),
581 video_capturer_(NULL),
582 encoder_observer_(channel_id),
583 external_capture_(external_capture),
584 capturer_updated_(false),
585 interval_(0),
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000586 video_adapter_(new CoordinatedVideoAdapter),
587 cpu_monitor_(cpu_monitor) {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000588 overuse_observer_.reset(new WebRtcOveruseObserver(video_adapter_.get()));
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000589 SignalCpuAdaptationUnable.repeat(video_adapter_->SignalCpuAdaptationUnable);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000590 if (cpu_monitor) {
591 cpu_monitor->SignalUpdate.connect(
592 video_adapter_.get(), &CoordinatedVideoAdapter::OnCpuLoadUpdated);
593 }
594 }
595
596 int channel_id() const { return channel_id_; }
597 int capture_id() const { return capture_id_; }
598 void set_sending(bool sending) { sending_ = sending; }
599 bool sending() const { return sending_; }
600 void set_muted(bool on) {
601 // TODO(asapersson): add support.
602 // video_adapter_->SetBlackOutput(on);
603 muted_ = on;
604 }
605 bool muted() {return muted_; }
606
607 WebRtcEncoderObserver* encoder_observer() { return &encoder_observer_; }
608 webrtc::ViEExternalCapture* external_capture() { return external_capture_; }
609 const VideoFormat& video_format() const {
610 return video_format_;
611 }
612 void set_video_format(const VideoFormat& video_format) {
613 video_format_ = video_format;
614 if (video_format_ != cricket::VideoFormat()) {
615 interval_ = video_format_.interval;
616 }
617 video_adapter_->OnOutputFormatRequest(video_format_);
618 }
619 void set_interval(int64 interval) {
620 if (video_format() == cricket::VideoFormat()) {
621 interval_ = interval;
622 }
623 }
624 int64 interval() { return interval_; }
625
626 void InitializeAdapterOutputFormat(const webrtc::VideoCodec& codec) {
627 VideoFormat format(codec.width, codec.height,
628 VideoFormat::FpsToInterval(codec.maxFramerate),
629 FOURCC_I420);
630 if (video_adapter_->output_format().IsSize0x0()) {
631 video_adapter_->SetOutputFormat(format);
632 }
633 }
634
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000635 int CurrentAdaptReason() const {
636 return video_adapter_->adapt_reason();
637 }
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000638 webrtc::CpuOveruseObserver* overuse_observer() {
639 return overuse_observer_.get();
640 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000641
642 StreamParams* stream_params() { return stream_params_.get(); }
643 void set_stream_params(const StreamParams& sp) {
644 stream_params_.reset(new StreamParams(sp));
645 }
646 void ClearStreamParams() { stream_params_.reset(); }
647 bool has_ssrc(uint32 local_ssrc) const {
648 return !stream_params_ ? false :
649 stream_params_->has_ssrc(local_ssrc);
650 }
651 WebRtcLocalStreamInfo* local_stream_info() {
652 return &local_stream_info_;
653 }
654 VideoCapturer* video_capturer() {
655 return video_capturer_;
656 }
657 void set_video_capturer(VideoCapturer* video_capturer) {
658 if (video_capturer == video_capturer_) {
659 return;
660 }
661 capturer_updated_ = true;
662 video_capturer_ = video_capturer;
663 if (video_capturer && !video_capturer->IsScreencast()) {
664 const VideoFormat* capture_format = video_capturer->GetCaptureFormat();
665 if (capture_format) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000666 // TODO(thorcarpenter): This is broken. Video capturer doesn't have
667 // a capture format until the capturer is started. So, if
668 // the capturer is started immediately after calling set_video_capturer
669 // video adapter may not have the input format set, the interval may
670 // be zero, and all frames may be dropped.
671 // Consider fixing this by having video_adapter keep a pointer to the
672 // video capturer.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000673 video_adapter_->SetInputFormat(*capture_format);
674 }
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000675 // TODO(thorcarpenter): When the adapter supports "only frame dropping"
676 // mode, also hook it up to screencast capturers.
677 video_capturer->SignalAdaptFrame.connect(
678 this, &WebRtcVideoChannelSendInfo::AdaptFrame);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000679 }
680 }
681
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000682 void AdaptFrame(VideoCapturer* capturer, const VideoFrame* input,
683 VideoFrame** adapted) {
684 video_adapter_->AdaptFrame(input, adapted);
685 }
686
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000687 void ApplyCpuOptions(const VideoOptions& options) {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000688 bool cpu_adapt, cpu_smoothing, adapt_third;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000689 float low, med, high;
690 if (options.adapt_input_to_cpu_usage.Get(&cpu_adapt)) {
691 video_adapter_->set_cpu_adaptation(cpu_adapt);
692 }
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000693 if (options.adapt_cpu_with_smoothing.Get(&cpu_smoothing)) {
694 video_adapter_->set_cpu_smoothing(cpu_smoothing);
695 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000696 if (options.process_adaptation_threshhold.Get(&med)) {
697 video_adapter_->set_process_threshold(med);
698 }
699 if (options.system_low_adaptation_threshhold.Get(&low)) {
700 video_adapter_->set_low_system_threshold(low);
701 }
702 if (options.system_high_adaptation_threshhold.Get(&high)) {
703 video_adapter_->set_high_system_threshold(high);
704 }
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000705 if (options.video_adapt_third.Get(&adapt_third)) {
706 video_adapter_->set_scale_third(adapt_third);
707 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000708 }
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000709
710 void SetCpuOveruseDetection(bool enable) {
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000711 if (cpu_monitor_ && enable) {
712 cpu_monitor_->SignalUpdate.disconnect(video_adapter_.get());
713 }
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000714 overuse_observer_->Enable(enable);
715 video_adapter_->set_cpu_adaptation(enable);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000716 }
717
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000718 void ProcessFrame(const VideoFrame& original_frame, bool mute,
719 VideoFrame** processed_frame) {
720 if (!mute) {
721 *processed_frame = original_frame.Copy();
722 } else {
723 WebRtcVideoFrame* black_frame = new WebRtcVideoFrame();
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000724 black_frame->InitToBlack(static_cast<int>(original_frame.GetWidth()),
725 static_cast<int>(original_frame.GetHeight()),
726 1, 1,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000727 original_frame.GetElapsedTime(),
728 original_frame.GetTimeStamp());
729 *processed_frame = black_frame;
730 }
731 local_stream_info_.UpdateFrame(*processed_frame);
732 }
733 void RegisterEncoder(int pl_type, webrtc::VideoEncoder* encoder) {
734 ASSERT(!IsEncoderRegistered(pl_type));
735 registered_encoders_[pl_type] = encoder;
736 }
737 bool IsEncoderRegistered(int pl_type) {
738 return registered_encoders_.count(pl_type) != 0;
739 }
740 const EncoderMap& registered_encoders() {
741 return registered_encoders_;
742 }
743 void ClearRegisteredEncoders() {
744 registered_encoders_.clear();
745 }
746
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000747 sigslot::repeater0<> SignalCpuAdaptationUnable;
748
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000749 private:
750 int channel_id_;
751 int capture_id_;
752 bool sending_;
753 bool muted_;
754 VideoCapturer* video_capturer_;
755 WebRtcEncoderObserver encoder_observer_;
756 webrtc::ViEExternalCapture* external_capture_;
757 EncoderMap registered_encoders_;
758
759 VideoFormat video_format_;
760
761 talk_base::scoped_ptr<StreamParams> stream_params_;
762
763 WebRtcLocalStreamInfo local_stream_info_;
764
765 bool capturer_updated_;
766
767 int64 interval_;
768
769 talk_base::scoped_ptr<CoordinatedVideoAdapter> video_adapter_;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000770 talk_base::CpuMonitor* cpu_monitor_;
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000771 talk_base::scoped_ptr<WebRtcOveruseObserver> overuse_observer_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000772};
773
774const WebRtcVideoEngine::VideoCodecPref
775 WebRtcVideoEngine::kVideoCodecPrefs[] = {
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000776 {kVp8PayloadName, 100, -1, 0},
777 {kRedPayloadName, 116, -1, 1},
778 {kFecPayloadName, 117, -1, 2},
779 {kRtxCodecName, 96, 100, 3},
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000780};
781
782// The formats are sorted by the descending order of width. We use the order to
783// find the next format for CPU and bandwidth adaptation.
784const VideoFormatPod WebRtcVideoEngine::kVideoFormats[] = {
785 {1280, 800, FPS_TO_INTERVAL(30), FOURCC_ANY},
786 {1280, 720, FPS_TO_INTERVAL(30), FOURCC_ANY},
787 {960, 600, FPS_TO_INTERVAL(30), FOURCC_ANY},
788 {960, 540, FPS_TO_INTERVAL(30), FOURCC_ANY},
789 {640, 400, FPS_TO_INTERVAL(30), FOURCC_ANY},
790 {640, 360, FPS_TO_INTERVAL(30), FOURCC_ANY},
791 {640, 480, FPS_TO_INTERVAL(30), FOURCC_ANY},
792 {480, 300, FPS_TO_INTERVAL(30), FOURCC_ANY},
793 {480, 270, FPS_TO_INTERVAL(30), FOURCC_ANY},
794 {480, 360, FPS_TO_INTERVAL(30), FOURCC_ANY},
795 {320, 200, FPS_TO_INTERVAL(30), FOURCC_ANY},
796 {320, 180, FPS_TO_INTERVAL(30), FOURCC_ANY},
797 {320, 240, FPS_TO_INTERVAL(30), FOURCC_ANY},
798 {240, 150, FPS_TO_INTERVAL(30), FOURCC_ANY},
799 {240, 135, FPS_TO_INTERVAL(30), FOURCC_ANY},
800 {240, 180, FPS_TO_INTERVAL(30), FOURCC_ANY},
801 {160, 100, FPS_TO_INTERVAL(30), FOURCC_ANY},
802 {160, 90, FPS_TO_INTERVAL(30), FOURCC_ANY},
803 {160, 120, FPS_TO_INTERVAL(30), FOURCC_ANY},
804};
805
806const VideoFormatPod WebRtcVideoEngine::kDefaultVideoFormat =
807 {640, 400, FPS_TO_INTERVAL(30), FOURCC_ANY};
808
809static void UpdateVideoCodec(const cricket::VideoFormat& video_format,
810 webrtc::VideoCodec* target_codec) {
811 if ((target_codec == NULL) || (video_format == cricket::VideoFormat())) {
812 return;
813 }
814 target_codec->width = video_format.width;
815 target_codec->height = video_format.height;
816 target_codec->maxFramerate = cricket::VideoFormat::IntervalToFps(
817 video_format.interval);
818}
819
820WebRtcVideoEngine::WebRtcVideoEngine() {
821 Construct(new ViEWrapper(), new ViETraceWrapper(), NULL,
822 new talk_base::CpuMonitor(NULL));
823}
824
825WebRtcVideoEngine::WebRtcVideoEngine(WebRtcVoiceEngine* voice_engine,
826 ViEWrapper* vie_wrapper,
827 talk_base::CpuMonitor* cpu_monitor) {
828 Construct(vie_wrapper, new ViETraceWrapper(), voice_engine, cpu_monitor);
829}
830
831WebRtcVideoEngine::WebRtcVideoEngine(WebRtcVoiceEngine* voice_engine,
832 ViEWrapper* vie_wrapper,
833 ViETraceWrapper* tracing,
834 talk_base::CpuMonitor* cpu_monitor) {
835 Construct(vie_wrapper, tracing, voice_engine, cpu_monitor);
836}
837
838void WebRtcVideoEngine::Construct(ViEWrapper* vie_wrapper,
839 ViETraceWrapper* tracing,
840 WebRtcVoiceEngine* voice_engine,
841 talk_base::CpuMonitor* cpu_monitor) {
842 LOG(LS_INFO) << "WebRtcVideoEngine::WebRtcVideoEngine";
843 worker_thread_ = NULL;
844 vie_wrapper_.reset(vie_wrapper);
845 vie_wrapper_base_initialized_ = false;
846 tracing_.reset(tracing);
847 voice_engine_ = voice_engine;
848 initialized_ = false;
849 SetTraceFilter(SeverityToFilter(kDefaultLogSeverity));
850 render_module_.reset(new WebRtcPassthroughRender());
851 local_renderer_w_ = local_renderer_h_ = 0;
852 local_renderer_ = NULL;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000853 capture_started_ = false;
854 decoder_factory_ = NULL;
855 encoder_factory_ = NULL;
856 cpu_monitor_.reset(cpu_monitor);
857
858 SetTraceOptions("");
859 if (tracing_->SetTraceCallback(this) != 0) {
860 LOG_RTCERR1(SetTraceCallback, this);
861 }
862
863 // Set default quality levels for our supported codecs. We override them here
864 // if we know your cpu performance is low, and they can be updated explicitly
865 // by calling SetDefaultCodec. For example by a flute preference setting, or
866 // by the server with a jec in response to our reported system info.
867 VideoCodec max_codec(kVideoCodecPrefs[0].payload_type,
868 kVideoCodecPrefs[0].name,
869 kDefaultVideoFormat.width,
870 kDefaultVideoFormat.height,
871 VideoFormat::IntervalToFps(kDefaultVideoFormat.interval),
872 0);
873 if (!SetDefaultCodec(max_codec)) {
874 LOG(LS_ERROR) << "Failed to initialize list of supported codec types";
875 }
876
877
878 // Load our RTP Header extensions.
879 rtp_header_extensions_.push_back(
880 RtpHeaderExtension(kRtpTimestampOffsetHeaderExtension,
881 kRtpTimeOffsetExtensionId));
882 rtp_header_extensions_.push_back(
883 RtpHeaderExtension(kRtpAbsoluteSendTimeHeaderExtension,
884 kRtpAbsoluteSendTimeExtensionId));
885}
886
887WebRtcVideoEngine::~WebRtcVideoEngine() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000888 LOG(LS_INFO) << "WebRtcVideoEngine::~WebRtcVideoEngine";
889 if (initialized_) {
890 Terminate();
891 }
892 if (encoder_factory_) {
893 encoder_factory_->RemoveObserver(this);
894 }
895 tracing_->SetTraceCallback(NULL);
896 // Test to see if the media processor was deregistered properly.
897 ASSERT(SignalMediaFrame.is_empty());
898}
899
900bool WebRtcVideoEngine::Init(talk_base::Thread* worker_thread) {
901 LOG(LS_INFO) << "WebRtcVideoEngine::Init";
902 worker_thread_ = worker_thread;
903 ASSERT(worker_thread_ != NULL);
904
905 cpu_monitor_->set_thread(worker_thread_);
906 if (!cpu_monitor_->Start(kCpuMonitorPeriodMs)) {
907 LOG(LS_ERROR) << "Failed to start CPU monitor.";
908 cpu_monitor_.reset();
909 }
910
911 bool result = InitVideoEngine();
912 if (result) {
913 LOG(LS_INFO) << "VideoEngine Init done";
914 } else {
915 LOG(LS_ERROR) << "VideoEngine Init failed, releasing";
916 Terminate();
917 }
918 return result;
919}
920
921bool WebRtcVideoEngine::InitVideoEngine() {
922 LOG(LS_INFO) << "WebRtcVideoEngine::InitVideoEngine";
923
924 // Init WebRTC VideoEngine.
925 if (!vie_wrapper_base_initialized_) {
926 if (vie_wrapper_->base()->Init() != 0) {
927 LOG_RTCERR0(Init);
928 return false;
929 }
930 vie_wrapper_base_initialized_ = true;
931 }
932
933 // Log the VoiceEngine version info.
934 char buffer[1024] = "";
935 if (vie_wrapper_->base()->GetVersion(buffer) != 0) {
936 LOG_RTCERR0(GetVersion);
937 return false;
938 }
939
940 LOG(LS_INFO) << "WebRtc VideoEngine Version:";
941 LogMultiline(talk_base::LS_INFO, buffer);
942
943 // Hook up to VoiceEngine for sync purposes, if supplied.
944 if (!voice_engine_) {
945 LOG(LS_WARNING) << "NULL voice engine";
946 } else if ((vie_wrapper_->base()->SetVoiceEngine(
947 voice_engine_->voe()->engine())) != 0) {
948 LOG_RTCERR0(SetVoiceEngine);
949 return false;
950 }
951
952 // Register our custom render module.
953 if (vie_wrapper_->render()->RegisterVideoRenderModule(
954 *render_module_.get()) != 0) {
955 LOG_RTCERR0(RegisterVideoRenderModule);
956 return false;
957 }
958
959 initialized_ = true;
960 return true;
961}
962
963void WebRtcVideoEngine::Terminate() {
964 LOG(LS_INFO) << "WebRtcVideoEngine::Terminate";
965 initialized_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000966
967 if (vie_wrapper_->render()->DeRegisterVideoRenderModule(
968 *render_module_.get()) != 0) {
969 LOG_RTCERR0(DeRegisterVideoRenderModule);
970 }
971
972 if (vie_wrapper_->base()->SetVoiceEngine(NULL) != 0) {
973 LOG_RTCERR0(SetVoiceEngine);
974 }
975
976 cpu_monitor_->Stop();
977}
978
979int WebRtcVideoEngine::GetCapabilities() {
980 return VIDEO_RECV | VIDEO_SEND;
981}
982
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000983bool WebRtcVideoEngine::SetOptions(const VideoOptions &options) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000984 return true;
985}
986
987bool WebRtcVideoEngine::SetDefaultEncoderConfig(
988 const VideoEncoderConfig& config) {
989 return SetDefaultCodec(config.max_codec);
990}
991
wu@webrtc.org78187522013-10-07 23:32:02 +0000992VideoEncoderConfig WebRtcVideoEngine::GetDefaultEncoderConfig() const {
993 ASSERT(!video_codecs_.empty());
994 VideoCodec max_codec(kVideoCodecPrefs[0].payload_type,
995 kVideoCodecPrefs[0].name,
996 video_codecs_[0].width,
997 video_codecs_[0].height,
998 video_codecs_[0].framerate,
999 0);
1000 return VideoEncoderConfig(max_codec);
1001}
1002
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001003// SetDefaultCodec may be called while the capturer is running. For example, a
1004// test call is started in a page with QVGA default codec, and then a real call
1005// is started in another page with VGA default codec. This is the corner case
1006// and happens only when a session is started. We ignore this case currently.
1007bool WebRtcVideoEngine::SetDefaultCodec(const VideoCodec& codec) {
1008 if (!RebuildCodecList(codec)) {
1009 LOG(LS_WARNING) << "Failed to RebuildCodecList";
1010 return false;
1011 }
1012
wu@webrtc.org78187522013-10-07 23:32:02 +00001013 ASSERT(!video_codecs_.empty());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001014 default_codec_format_ = VideoFormat(
1015 video_codecs_[0].width,
1016 video_codecs_[0].height,
1017 VideoFormat::FpsToInterval(video_codecs_[0].framerate),
1018 FOURCC_ANY);
1019 return true;
1020}
1021
1022WebRtcVideoMediaChannel* WebRtcVideoEngine::CreateChannel(
1023 VoiceMediaChannel* voice_channel) {
1024 WebRtcVideoMediaChannel* channel =
1025 new WebRtcVideoMediaChannel(this, voice_channel);
1026 if (!channel->Init()) {
1027 delete channel;
1028 channel = NULL;
1029 }
1030 return channel;
1031}
1032
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001033bool WebRtcVideoEngine::SetLocalRenderer(VideoRenderer* renderer) {
1034 local_renderer_w_ = local_renderer_h_ = 0;
1035 local_renderer_ = renderer;
1036 return true;
1037}
1038
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001039const std::vector<VideoCodec>& WebRtcVideoEngine::codecs() const {
1040 return video_codecs_;
1041}
1042
1043const std::vector<RtpHeaderExtension>&
1044WebRtcVideoEngine::rtp_header_extensions() const {
1045 return rtp_header_extensions_;
1046}
1047
1048void WebRtcVideoEngine::SetLogging(int min_sev, const char* filter) {
1049 // if min_sev == -1, we keep the current log level.
1050 if (min_sev >= 0) {
1051 SetTraceFilter(SeverityToFilter(min_sev));
1052 }
1053 SetTraceOptions(filter);
1054}
1055
1056int WebRtcVideoEngine::GetLastEngineError() {
1057 return vie_wrapper_->error();
1058}
1059
1060// Checks to see whether we comprehend and could receive a particular codec
1061bool WebRtcVideoEngine::FindCodec(const VideoCodec& in) {
1062 for (int i = 0; i < ARRAY_SIZE(kVideoFormats); ++i) {
1063 const VideoFormat fmt(kVideoFormats[i]);
1064 if ((in.width == 0 && in.height == 0) ||
1065 (fmt.width == in.width && fmt.height == in.height)) {
1066 if (encoder_factory_) {
1067 const std::vector<WebRtcVideoEncoderFactory::VideoCodec>& codecs =
1068 encoder_factory_->codecs();
1069 for (size_t j = 0; j < codecs.size(); ++j) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001070 VideoCodec codec(GetExternalVideoPayloadType(static_cast<int>(j)),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001071 codecs[j].name, 0, 0, 0, 0);
1072 if (codec.Matches(in))
1073 return true;
1074 }
1075 }
1076 for (size_t j = 0; j < ARRAY_SIZE(kVideoCodecPrefs); ++j) {
1077 VideoCodec codec(kVideoCodecPrefs[j].payload_type,
1078 kVideoCodecPrefs[j].name, 0, 0, 0, 0);
1079 if (codec.Matches(in)) {
1080 return true;
1081 }
1082 }
1083 }
1084 }
1085 return false;
1086}
1087
1088// Given the requested codec, returns true if we can send that codec type and
1089// updates out with the best quality we could send for that codec. If current is
1090// not empty, we constrain out so that its aspect ratio matches current's.
1091bool WebRtcVideoEngine::CanSendCodec(const VideoCodec& requested,
1092 const VideoCodec& current,
1093 VideoCodec* out) {
1094 if (!out) {
1095 return false;
1096 }
1097
1098 std::vector<VideoCodec>::const_iterator local_max;
1099 for (local_max = video_codecs_.begin();
1100 local_max < video_codecs_.end();
1101 ++local_max) {
1102 // First match codecs by payload type
1103 if (!requested.Matches(*local_max)) {
1104 continue;
1105 }
1106
1107 out->id = requested.id;
1108 out->name = requested.name;
1109 out->preference = requested.preference;
1110 out->params = requested.params;
1111 out->framerate = talk_base::_min(requested.framerate, local_max->framerate);
1112 out->width = 0;
1113 out->height = 0;
1114 out->params = requested.params;
1115 out->feedback_params = requested.feedback_params;
1116
1117 if (0 == requested.width && 0 == requested.height) {
1118 // Special case with resolution 0. The channel should not send frames.
1119 return true;
1120 } else if (0 == requested.width || 0 == requested.height) {
1121 // 0xn and nx0 are invalid resolutions.
1122 return false;
1123 }
1124
1125 // Pick the best quality that is within their and our bounds and has the
1126 // correct aspect ratio.
1127 for (int j = 0; j < ARRAY_SIZE(kVideoFormats); ++j) {
1128 const VideoFormat format(kVideoFormats[j]);
1129
1130 // Skip any format that is larger than the local or remote maximums, or
1131 // smaller than the current best match
1132 if (format.width > requested.width || format.height > requested.height ||
1133 format.width > local_max->width ||
1134 (format.width < out->width && format.height < out->height)) {
1135 continue;
1136 }
1137
1138 bool better = false;
1139
1140 // Check any further constraints on this prospective format
1141 if (!out->width || !out->height) {
1142 // If we don't have any matches yet, this is the best so far.
1143 better = true;
1144 } else if (current.width && current.height) {
1145 // current is set so format must match its ratio exactly.
1146 better =
1147 (format.width * current.height == format.height * current.width);
1148 } else {
1149 // Prefer closer aspect ratios i.e
1150 // format.aspect - requested.aspect < out.aspect - requested.aspect
1151 better = abs(format.width * requested.height * out->height -
1152 requested.width * format.height * out->height) <
1153 abs(out->width * format.height * requested.height -
1154 requested.width * format.height * out->height);
1155 }
1156
1157 if (better) {
1158 out->width = format.width;
1159 out->height = format.height;
1160 }
1161 }
1162 if (out->width > 0) {
1163 return true;
1164 }
1165 }
1166 return false;
1167}
1168
1169static void ConvertToCricketVideoCodec(
1170 const webrtc::VideoCodec& in_codec, VideoCodec* out_codec) {
1171 out_codec->id = in_codec.plType;
1172 out_codec->name = in_codec.plName;
1173 out_codec->width = in_codec.width;
1174 out_codec->height = in_codec.height;
1175 out_codec->framerate = in_codec.maxFramerate;
1176 out_codec->SetParam(kCodecParamMinBitrate, in_codec.minBitrate);
1177 out_codec->SetParam(kCodecParamMaxBitrate, in_codec.maxBitrate);
1178 if (in_codec.qpMax) {
1179 out_codec->SetParam(kCodecParamMaxQuantization, in_codec.qpMax);
1180 }
1181}
1182
1183bool WebRtcVideoEngine::ConvertFromCricketVideoCodec(
1184 const VideoCodec& in_codec, webrtc::VideoCodec* out_codec) {
1185 bool found = false;
1186 int ncodecs = vie_wrapper_->codec()->NumberOfCodecs();
1187 for (int i = 0; i < ncodecs; ++i) {
1188 if (vie_wrapper_->codec()->GetCodec(i, *out_codec) == 0 &&
1189 _stricmp(in_codec.name.c_str(), out_codec->plName) == 0) {
1190 found = true;
1191 break;
1192 }
1193 }
1194
1195 // If not found, check if this is supported by external encoder factory.
1196 if (!found && encoder_factory_) {
1197 const std::vector<WebRtcVideoEncoderFactory::VideoCodec>& codecs =
1198 encoder_factory_->codecs();
1199 for (size_t i = 0; i < codecs.size(); ++i) {
1200 if (_stricmp(in_codec.name.c_str(), codecs[i].name.c_str()) == 0) {
1201 out_codec->codecType = codecs[i].type;
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001202 out_codec->plType = GetExternalVideoPayloadType(static_cast<int>(i));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001203 talk_base::strcpyn(out_codec->plName, sizeof(out_codec->plName),
1204 codecs[i].name.c_str(), codecs[i].name.length());
1205 found = true;
1206 break;
1207 }
1208 }
1209 }
1210
1211 if (!found) {
1212 LOG(LS_ERROR) << "invalid codec type";
1213 return false;
1214 }
1215
1216 if (in_codec.id != 0)
1217 out_codec->plType = in_codec.id;
1218
1219 if (in_codec.width != 0)
1220 out_codec->width = in_codec.width;
1221
1222 if (in_codec.height != 0)
1223 out_codec->height = in_codec.height;
1224
1225 if (in_codec.framerate != 0)
1226 out_codec->maxFramerate = in_codec.framerate;
1227
1228 // Convert bitrate parameters.
1229 int max_bitrate = kMaxVideoBitrate;
1230 int min_bitrate = kMinVideoBitrate;
1231 int start_bitrate = kStartVideoBitrate;
1232
1233 in_codec.GetParam(kCodecParamMinBitrate, &min_bitrate);
1234 in_codec.GetParam(kCodecParamMaxBitrate, &max_bitrate);
1235
1236 if (max_bitrate < min_bitrate) {
1237 return false;
1238 }
1239 start_bitrate = talk_base::_max(start_bitrate, min_bitrate);
1240 start_bitrate = talk_base::_min(start_bitrate, max_bitrate);
1241
1242 out_codec->minBitrate = min_bitrate;
1243 out_codec->startBitrate = start_bitrate;
1244 out_codec->maxBitrate = max_bitrate;
1245
1246 // Convert general codec parameters.
1247 int max_quantization = 0;
1248 if (in_codec.GetParam(kCodecParamMaxQuantization, &max_quantization)) {
1249 if (max_quantization < 0) {
1250 return false;
1251 }
1252 out_codec->qpMax = max_quantization;
1253 }
1254 return true;
1255}
1256
1257void WebRtcVideoEngine::RegisterChannel(WebRtcVideoMediaChannel *channel) {
1258 talk_base::CritScope cs(&channels_crit_);
1259 channels_.push_back(channel);
1260}
1261
1262void WebRtcVideoEngine::UnregisterChannel(WebRtcVideoMediaChannel *channel) {
1263 talk_base::CritScope cs(&channels_crit_);
1264 channels_.erase(std::remove(channels_.begin(), channels_.end(), channel),
1265 channels_.end());
1266}
1267
1268bool WebRtcVideoEngine::SetVoiceEngine(WebRtcVoiceEngine* voice_engine) {
1269 if (initialized_) {
1270 LOG(LS_WARNING) << "SetVoiceEngine can not be called after Init";
1271 return false;
1272 }
1273 voice_engine_ = voice_engine;
1274 return true;
1275}
1276
1277bool WebRtcVideoEngine::EnableTimedRender() {
1278 if (initialized_) {
1279 LOG(LS_WARNING) << "EnableTimedRender can not be called after Init";
1280 return false;
1281 }
1282 render_module_.reset(webrtc::VideoRender::CreateVideoRender(0, NULL,
1283 false, webrtc::kRenderExternal));
1284 return true;
1285}
1286
1287void WebRtcVideoEngine::SetTraceFilter(int filter) {
1288 tracing_->SetTraceFilter(filter);
1289}
1290
1291// See https://sites.google.com/a/google.com/wavelet/
1292// Home/Magic-Flute--RTC-Engine-/Magic-Flute-Command-Line-Parameters
1293// for all supported command line setttings.
1294void WebRtcVideoEngine::SetTraceOptions(const std::string& options) {
1295 // Set WebRTC trace file.
1296 std::vector<std::string> opts;
1297 talk_base::tokenize(options, ' ', '"', '"', &opts);
1298 std::vector<std::string>::iterator tracefile =
1299 std::find(opts.begin(), opts.end(), "tracefile");
1300 if (tracefile != opts.end() && ++tracefile != opts.end()) {
1301 // Write WebRTC debug output (at same loglevel) to file
1302 if (tracing_->SetTraceFile(tracefile->c_str()) == -1) {
1303 LOG_RTCERR1(SetTraceFile, *tracefile);
1304 }
1305 }
1306}
1307
1308static void AddDefaultFeedbackParams(VideoCodec* codec) {
1309 const FeedbackParam kFir(kRtcpFbParamCcm, kRtcpFbCcmParamFir);
1310 codec->AddFeedbackParam(kFir);
1311 const FeedbackParam kNack(kRtcpFbParamNack, kParamValueEmpty);
1312 codec->AddFeedbackParam(kNack);
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001313 const FeedbackParam kPli(kRtcpFbParamNack, kRtcpFbNackParamPli);
1314 codec->AddFeedbackParam(kPli);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001315 const FeedbackParam kRemb(kRtcpFbParamRemb, kParamValueEmpty);
1316 codec->AddFeedbackParam(kRemb);
1317}
1318
1319// Rebuilds the codec list to be only those that are less intensive
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00001320// than the specified codec. Prefers internal codec over external with
1321// higher preference field.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001322bool WebRtcVideoEngine::RebuildCodecList(const VideoCodec& in_codec) {
1323 if (!FindCodec(in_codec))
1324 return false;
1325
1326 video_codecs_.clear();
1327
1328 bool found = false;
mallinath@webrtc.org1112c302013-09-23 20:34:45 +00001329 std::set<std::string> internal_codec_names;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001330 for (size_t i = 0; i < ARRAY_SIZE(kVideoCodecPrefs); ++i) {
1331 const VideoCodecPref& pref(kVideoCodecPrefs[i]);
1332 if (!found)
1333 found = (in_codec.name == pref.name);
mallinath@webrtc.org1112c302013-09-23 20:34:45 +00001334 if (found) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001335 VideoCodec codec(pref.payload_type, pref.name,
1336 in_codec.width, in_codec.height, in_codec.framerate,
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001337 static_cast<int>(ARRAY_SIZE(kVideoCodecPrefs) - i));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001338 if (_stricmp(kVp8PayloadName, codec.name.c_str()) == 0) {
1339 AddDefaultFeedbackParams(&codec);
1340 }
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00001341 if (pref.associated_payload_type != -1) {
1342 codec.SetParam(kCodecParamAssociatedPayloadType,
1343 pref.associated_payload_type);
1344 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001345 video_codecs_.push_back(codec);
mallinath@webrtc.org1112c302013-09-23 20:34:45 +00001346 internal_codec_names.insert(codec.name);
1347 }
1348 }
1349 if (encoder_factory_) {
1350 const std::vector<WebRtcVideoEncoderFactory::VideoCodec>& codecs =
1351 encoder_factory_->codecs();
1352 for (size_t i = 0; i < codecs.size(); ++i) {
1353 bool is_internal_codec = internal_codec_names.find(codecs[i].name) !=
1354 internal_codec_names.end();
1355 if (!is_internal_codec) {
1356 if (!found)
1357 found = (in_codec.name == codecs[i].name);
1358 VideoCodec codec(
1359 GetExternalVideoPayloadType(static_cast<int>(i)),
1360 codecs[i].name,
1361 codecs[i].max_width,
1362 codecs[i].max_height,
1363 codecs[i].max_fps,
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00001364 // Use negative preference on external codec to ensure the internal
1365 // codec is preferred.
1366 static_cast<int>(0 - i));
mallinath@webrtc.org1112c302013-09-23 20:34:45 +00001367 AddDefaultFeedbackParams(&codec);
1368 video_codecs_.push_back(codec);
1369 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001370 }
1371 }
1372 ASSERT(found);
1373 return true;
1374}
1375
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001376// Ignore spammy trace messages, mostly from the stats API when we haven't
1377// gotten RTCP info yet from the remote side.
1378bool WebRtcVideoEngine::ShouldIgnoreTrace(const std::string& trace) {
1379 static const char* const kTracesToIgnore[] = {
1380 NULL
1381 };
1382 for (const char* const* p = kTracesToIgnore; *p; ++p) {
1383 if (trace.find(*p) == 0) {
1384 return true;
1385 }
1386 }
1387 return false;
1388}
1389
1390int WebRtcVideoEngine::GetNumOfChannels() {
1391 talk_base::CritScope cs(&channels_crit_);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001392 return static_cast<int>(channels_.size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001393}
1394
1395void WebRtcVideoEngine::Print(webrtc::TraceLevel level, const char* trace,
1396 int length) {
1397 talk_base::LoggingSeverity sev = talk_base::LS_VERBOSE;
1398 if (level == webrtc::kTraceError || level == webrtc::kTraceCritical)
1399 sev = talk_base::LS_ERROR;
1400 else if (level == webrtc::kTraceWarning)
1401 sev = talk_base::LS_WARNING;
1402 else if (level == webrtc::kTraceStateInfo || level == webrtc::kTraceInfo)
1403 sev = talk_base::LS_INFO;
1404 else if (level == webrtc::kTraceTerseInfo)
1405 sev = talk_base::LS_INFO;
1406
1407 // Skip past boilerplate prefix text
1408 if (length < 72) {
1409 std::string msg(trace, length);
1410 LOG(LS_ERROR) << "Malformed webrtc log message: ";
1411 LOG_V(sev) << msg;
1412 } else {
1413 std::string msg(trace + 71, length - 72);
1414 if (!ShouldIgnoreTrace(msg) &&
1415 (!voice_engine_ || !voice_engine_->ShouldIgnoreTrace(msg))) {
1416 LOG_V(sev) << "webrtc: " << msg;
1417 }
1418 }
1419}
1420
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001421webrtc::VideoDecoder* WebRtcVideoEngine::CreateExternalDecoder(
1422 webrtc::VideoCodecType type) {
1423 if (decoder_factory_ == NULL) {
1424 return NULL;
1425 }
1426 return decoder_factory_->CreateVideoDecoder(type);
1427}
1428
1429void WebRtcVideoEngine::DestroyExternalDecoder(webrtc::VideoDecoder* decoder) {
1430 ASSERT(decoder_factory_ != NULL);
1431 if (decoder_factory_ == NULL)
1432 return;
1433 decoder_factory_->DestroyVideoDecoder(decoder);
1434}
1435
1436webrtc::VideoEncoder* WebRtcVideoEngine::CreateExternalEncoder(
1437 webrtc::VideoCodecType type) {
1438 if (encoder_factory_ == NULL) {
1439 return NULL;
1440 }
1441 return encoder_factory_->CreateVideoEncoder(type);
1442}
1443
1444void WebRtcVideoEngine::DestroyExternalEncoder(webrtc::VideoEncoder* encoder) {
1445 ASSERT(encoder_factory_ != NULL);
1446 if (encoder_factory_ == NULL)
1447 return;
1448 encoder_factory_->DestroyVideoEncoder(encoder);
1449}
1450
1451bool WebRtcVideoEngine::IsExternalEncoderCodecType(
1452 webrtc::VideoCodecType type) const {
1453 if (!encoder_factory_)
1454 return false;
1455 const std::vector<WebRtcVideoEncoderFactory::VideoCodec>& codecs =
1456 encoder_factory_->codecs();
1457 std::vector<WebRtcVideoEncoderFactory::VideoCodec>::const_iterator it;
1458 for (it = codecs.begin(); it != codecs.end(); ++it) {
1459 if (it->type == type)
1460 return true;
1461 }
1462 return false;
1463}
1464
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001465void WebRtcVideoEngine::SetExternalDecoderFactory(
1466 WebRtcVideoDecoderFactory* decoder_factory) {
1467 decoder_factory_ = decoder_factory;
1468}
1469
1470void WebRtcVideoEngine::SetExternalEncoderFactory(
1471 WebRtcVideoEncoderFactory* encoder_factory) {
1472 if (encoder_factory_ == encoder_factory)
1473 return;
1474
1475 if (encoder_factory_) {
1476 encoder_factory_->RemoveObserver(this);
1477 }
1478 encoder_factory_ = encoder_factory;
1479 if (encoder_factory_) {
1480 encoder_factory_->AddObserver(this);
1481 }
1482
1483 // Invoke OnCodecAvailable() here in case the list of codecs is already
1484 // available when the encoder factory is installed. If not the encoder
1485 // factory will invoke the callback later when the codecs become available.
1486 OnCodecsAvailable();
1487}
1488
1489void WebRtcVideoEngine::OnCodecsAvailable() {
1490 // Rebuild codec list while reapplying the current default codec format.
1491 VideoCodec max_codec(kVideoCodecPrefs[0].payload_type,
1492 kVideoCodecPrefs[0].name,
1493 video_codecs_[0].width,
1494 video_codecs_[0].height,
1495 video_codecs_[0].framerate,
1496 0);
1497 if (!RebuildCodecList(max_codec)) {
1498 LOG(LS_ERROR) << "Failed to initialize list of supported codec types";
1499 }
1500}
1501
1502// WebRtcVideoMediaChannel
1503
1504WebRtcVideoMediaChannel::WebRtcVideoMediaChannel(
1505 WebRtcVideoEngine* engine,
1506 VoiceMediaChannel* channel)
1507 : engine_(engine),
1508 voice_channel_(channel),
1509 vie_channel_(-1),
1510 nack_enabled_(true),
1511 remb_enabled_(false),
1512 render_started_(false),
1513 first_receive_ssrc_(0),
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00001514 send_rtx_type_(-1),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001515 send_red_type_(-1),
1516 send_fec_type_(-1),
1517 send_min_bitrate_(kMinVideoBitrate),
1518 send_start_bitrate_(kStartVideoBitrate),
1519 send_max_bitrate_(kMaxVideoBitrate),
1520 sending_(false),
1521 ratio_w_(0),
1522 ratio_h_(0) {
1523 engine->RegisterChannel(this);
1524}
1525
1526bool WebRtcVideoMediaChannel::Init() {
1527 const uint32 ssrc_key = 0;
1528 return CreateChannel(ssrc_key, MD_SENDRECV, &vie_channel_);
1529}
1530
1531WebRtcVideoMediaChannel::~WebRtcVideoMediaChannel() {
1532 const bool send = false;
1533 SetSend(send);
1534 const bool render = false;
1535 SetRender(render);
1536
1537 while (!send_channels_.empty()) {
1538 if (!DeleteSendChannel(send_channels_.begin()->first)) {
1539 LOG(LS_ERROR) << "Unable to delete channel with ssrc key "
1540 << send_channels_.begin()->first;
1541 ASSERT(false);
1542 break;
1543 }
1544 }
1545
1546 // Remove all receive streams and the default channel.
1547 while (!recv_channels_.empty()) {
1548 RemoveRecvStream(recv_channels_.begin()->first);
1549 }
1550
1551 // Unregister the channel from the engine.
1552 engine()->UnregisterChannel(this);
1553 if (worker_thread()) {
1554 worker_thread()->Clear(this);
1555 }
1556}
1557
1558bool WebRtcVideoMediaChannel::SetRecvCodecs(
1559 const std::vector<VideoCodec>& codecs) {
1560 receive_codecs_.clear();
1561 for (std::vector<VideoCodec>::const_iterator iter = codecs.begin();
1562 iter != codecs.end(); ++iter) {
1563 if (engine()->FindCodec(*iter)) {
1564 webrtc::VideoCodec wcodec;
1565 if (engine()->ConvertFromCricketVideoCodec(*iter, &wcodec)) {
1566 receive_codecs_.push_back(wcodec);
1567 }
1568 } else {
1569 LOG(LS_INFO) << "Unknown codec " << iter->name;
1570 return false;
1571 }
1572 }
1573
1574 for (RecvChannelMap::iterator it = recv_channels_.begin();
1575 it != recv_channels_.end(); ++it) {
1576 if (!SetReceiveCodecs(it->second))
1577 return false;
1578 }
1579 return true;
1580}
1581
1582bool WebRtcVideoMediaChannel::SetSendCodecs(
1583 const std::vector<VideoCodec>& codecs) {
1584 // Match with local video codec list.
1585 std::vector<webrtc::VideoCodec> send_codecs;
1586 VideoCodec checked_codec;
1587 VideoCodec current; // defaults to 0x0
1588 if (sending_) {
1589 ConvertToCricketVideoCodec(*send_codec_, &current);
1590 }
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00001591 std::map<int, int> primary_rtx_pt_mapping;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001592 for (std::vector<VideoCodec>::const_iterator iter = codecs.begin();
1593 iter != codecs.end(); ++iter) {
1594 if (_stricmp(iter->name.c_str(), kRedPayloadName) == 0) {
1595 send_red_type_ = iter->id;
1596 } else if (_stricmp(iter->name.c_str(), kFecPayloadName) == 0) {
1597 send_fec_type_ = iter->id;
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00001598 } else if (_stricmp(iter->name.c_str(), kRtxCodecName) == 0) {
1599 int rtx_type = iter->id;
1600 int rtx_primary_type = -1;
1601 if (iter->GetParam(kCodecParamAssociatedPayloadType, &rtx_primary_type)) {
1602 primary_rtx_pt_mapping[rtx_primary_type] = rtx_type;
1603 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001604 } else if (engine()->CanSendCodec(*iter, current, &checked_codec)) {
1605 webrtc::VideoCodec wcodec;
1606 if (engine()->ConvertFromCricketVideoCodec(checked_codec, &wcodec)) {
1607 if (send_codecs.empty()) {
1608 nack_enabled_ = IsNackEnabled(checked_codec);
1609 remb_enabled_ = IsRembEnabled(checked_codec);
1610 }
1611 send_codecs.push_back(wcodec);
1612 }
1613 } else {
1614 LOG(LS_WARNING) << "Unknown codec " << iter->name;
1615 }
1616 }
1617
1618 // Fail if we don't have a match.
1619 if (send_codecs.empty()) {
1620 LOG(LS_WARNING) << "No matching codecs available";
1621 return false;
1622 }
1623
1624 // Recv protection.
1625 for (RecvChannelMap::iterator it = recv_channels_.begin();
1626 it != recv_channels_.end(); ++it) {
1627 int channel_id = it->second->channel_id();
1628 if (!SetNackFec(channel_id, send_red_type_, send_fec_type_,
1629 nack_enabled_)) {
1630 return false;
1631 }
1632 if (engine_->vie()->rtp()->SetRembStatus(channel_id,
1633 kNotSending,
1634 remb_enabled_) != 0) {
1635 LOG_RTCERR3(SetRembStatus, channel_id, kNotSending, remb_enabled_);
1636 return false;
1637 }
1638 }
1639
1640 // Send settings.
1641 for (SendChannelMap::iterator iter = send_channels_.begin();
1642 iter != send_channels_.end(); ++iter) {
1643 int channel_id = iter->second->channel_id();
1644 if (!SetNackFec(channel_id, send_red_type_, send_fec_type_,
1645 nack_enabled_)) {
1646 return false;
1647 }
1648 if (engine_->vie()->rtp()->SetRembStatus(channel_id,
1649 remb_enabled_,
1650 remb_enabled_) != 0) {
1651 LOG_RTCERR3(SetRembStatus, channel_id, remb_enabled_, remb_enabled_);
1652 return false;
1653 }
1654 }
1655
1656 // Select the first matched codec.
1657 webrtc::VideoCodec& codec(send_codecs[0]);
1658
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00001659 // Set RTX payload type if primary now active. This value will be used in
1660 // SetSendCodec.
1661 std::map<int, int>::const_iterator rtx_it =
1662 primary_rtx_pt_mapping.find(static_cast<int>(codec.plType));
1663 if (rtx_it != primary_rtx_pt_mapping.end()) {
1664 send_rtx_type_ = rtx_it->second;
1665 }
1666
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001667 if (!SetSendCodec(
1668 codec, codec.minBitrate, codec.startBitrate, codec.maxBitrate)) {
1669 return false;
1670 }
1671
1672 for (SendChannelMap::iterator iter = send_channels_.begin();
1673 iter != send_channels_.end(); ++iter) {
1674 WebRtcVideoChannelSendInfo* send_channel = iter->second;
1675 send_channel->InitializeAdapterOutputFormat(codec);
1676 }
1677
1678 LogSendCodecChange("SetSendCodecs()");
1679
1680 return true;
1681}
1682
1683bool WebRtcVideoMediaChannel::GetSendCodec(VideoCodec* send_codec) {
1684 if (!send_codec_) {
1685 return false;
1686 }
1687 ConvertToCricketVideoCodec(*send_codec_, send_codec);
1688 return true;
1689}
1690
1691bool WebRtcVideoMediaChannel::SetSendStreamFormat(uint32 ssrc,
1692 const VideoFormat& format) {
1693 if (!send_codec_) {
1694 LOG(LS_ERROR) << "The send codec has not been set yet.";
1695 return false;
1696 }
1697 WebRtcVideoChannelSendInfo* send_channel = GetSendChannel(ssrc);
1698 if (!send_channel) {
1699 LOG(LS_ERROR) << "The specified ssrc " << ssrc << " is not in use.";
1700 return false;
1701 }
1702 send_channel->set_video_format(format);
1703 return true;
1704}
1705
1706bool WebRtcVideoMediaChannel::SetRender(bool render) {
1707 if (render == render_started_) {
1708 return true; // no action required
1709 }
1710
1711 bool ret = true;
1712 for (RecvChannelMap::iterator it = recv_channels_.begin();
1713 it != recv_channels_.end(); ++it) {
1714 if (render) {
1715 if (engine()->vie()->render()->StartRender(
1716 it->second->channel_id()) != 0) {
1717 LOG_RTCERR1(StartRender, it->second->channel_id());
1718 ret = false;
1719 }
1720 } else {
1721 if (engine()->vie()->render()->StopRender(
1722 it->second->channel_id()) != 0) {
1723 LOG_RTCERR1(StopRender, it->second->channel_id());
1724 ret = false;
1725 }
1726 }
1727 }
1728 if (ret) {
1729 render_started_ = render;
1730 }
1731
1732 return ret;
1733}
1734
1735bool WebRtcVideoMediaChannel::SetSend(bool send) {
1736 if (!HasReadySendChannels() && send) {
1737 LOG(LS_ERROR) << "No stream added";
1738 return false;
1739 }
1740 if (send == sending()) {
1741 return true; // No action required.
1742 }
1743
1744 if (send) {
1745 // We've been asked to start sending.
1746 // SetSendCodecs must have been called already.
1747 if (!send_codec_) {
1748 return false;
1749 }
1750 // Start send now.
1751 if (!StartSend()) {
1752 return false;
1753 }
1754 } else {
1755 // We've been asked to stop sending.
1756 if (!StopSend()) {
1757 return false;
1758 }
1759 }
1760 sending_ = send;
1761
1762 return true;
1763}
1764
1765bool WebRtcVideoMediaChannel::AddSendStream(const StreamParams& sp) {
1766 LOG(LS_INFO) << "AddSendStream " << sp.ToString();
1767
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00001768 if (!IsOneSsrcStream(sp) && !IsSimulcastStream(sp)) {
1769 LOG(LS_ERROR) << "AddSendStream: bad local stream parameters";
1770 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001771 }
1772
1773 uint32 ssrc_key;
1774 if (!CreateSendChannelKey(sp.first_ssrc(), &ssrc_key)) {
1775 LOG(LS_ERROR) << "Trying to register duplicate ssrc: " << sp.first_ssrc();
1776 return false;
1777 }
1778 // If the default channel is already used for sending create a new channel
1779 // otherwise use the default channel for sending.
1780 int channel_id = -1;
1781 if (send_channels_[0]->stream_params() == NULL) {
1782 channel_id = vie_channel_;
1783 } else {
1784 if (!CreateChannel(ssrc_key, MD_SEND, &channel_id)) {
1785 LOG(LS_ERROR) << "AddSendStream: unable to create channel";
1786 return false;
1787 }
1788 }
1789 WebRtcVideoChannelSendInfo* send_channel = send_channels_[ssrc_key];
1790 // Set the send (local) SSRC.
1791 // If there are multiple send SSRCs, we can only set the first one here, and
1792 // the rest of the SSRC(s) need to be set after SetSendCodec has been called
1793 // (with a codec requires multiple SSRC(s)).
1794 if (engine()->vie()->rtp()->SetLocalSSRC(channel_id,
1795 sp.first_ssrc()) != 0) {
1796 LOG_RTCERR2(SetLocalSSRC, channel_id, sp.first_ssrc());
1797 return false;
1798 }
1799
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00001800 // Set the corresponding RTX SSRC.
1801 if (!SetLocalRtxSsrc(channel_id, sp, sp.first_ssrc(), 0)) {
1802 return false;
1803 }
1804
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001805 // Set RTCP CName.
1806 if (engine()->vie()->rtp()->SetRTCPCName(channel_id,
1807 sp.cname.c_str()) != 0) {
1808 LOG_RTCERR2(SetRTCPCName, channel_id, sp.cname.c_str());
1809 return false;
1810 }
1811
1812 // At this point the channel's local SSRC has been updated. If the channel is
1813 // the default channel make sure that all the receive channels are updated as
1814 // well. Receive channels have to have the same SSRC as the default channel in
1815 // order to send receiver reports with this SSRC.
1816 if (IsDefaultChannel(channel_id)) {
1817 for (RecvChannelMap::const_iterator it = recv_channels_.begin();
1818 it != recv_channels_.end(); ++it) {
1819 WebRtcVideoChannelRecvInfo* info = it->second;
1820 int channel_id = info->channel_id();
1821 if (engine()->vie()->rtp()->SetLocalSSRC(channel_id,
1822 sp.first_ssrc()) != 0) {
1823 LOG_RTCERR1(SetLocalSSRC, it->first);
1824 return false;
1825 }
1826 }
1827 }
1828
1829 send_channel->set_stream_params(sp);
1830
1831 // Reset send codec after stream parameters changed.
1832 if (send_codec_) {
1833 if (!SetSendCodec(send_channel, *send_codec_, send_min_bitrate_,
1834 send_start_bitrate_, send_max_bitrate_)) {
1835 return false;
1836 }
1837 LogSendCodecChange("SetSendStreamFormat()");
1838 }
1839
1840 if (sending_) {
1841 return StartSend(send_channel);
1842 }
1843 return true;
1844}
1845
1846bool WebRtcVideoMediaChannel::RemoveSendStream(uint32 ssrc) {
1847 uint32 ssrc_key;
1848 if (!GetSendChannelKey(ssrc, &ssrc_key)) {
1849 LOG(LS_WARNING) << "Try to remove stream with ssrc " << ssrc
1850 << " which doesn't exist.";
1851 return false;
1852 }
1853 WebRtcVideoChannelSendInfo* send_channel = send_channels_[ssrc_key];
1854 int channel_id = send_channel->channel_id();
1855 if (IsDefaultChannel(channel_id) && (send_channel->stream_params() == NULL)) {
1856 // Default channel will still exist. However, if stream_params() is NULL
1857 // there is no stream to remove.
1858 return false;
1859 }
1860 if (sending_) {
1861 StopSend(send_channel);
1862 }
1863
1864 const WebRtcVideoChannelSendInfo::EncoderMap& encoder_map =
1865 send_channel->registered_encoders();
1866 for (WebRtcVideoChannelSendInfo::EncoderMap::const_iterator it =
1867 encoder_map.begin(); it != encoder_map.end(); ++it) {
1868 if (engine()->vie()->ext_codec()->DeRegisterExternalSendCodec(
1869 channel_id, it->first) != 0) {
1870 LOG_RTCERR1(DeregisterEncoderObserver, channel_id);
1871 }
1872 engine()->DestroyExternalEncoder(it->second);
1873 }
1874 send_channel->ClearRegisteredEncoders();
1875
1876 // The receive channels depend on the default channel, recycle it instead.
1877 if (IsDefaultChannel(channel_id)) {
1878 SetCapturer(GetDefaultChannelSsrc(), NULL);
1879 send_channel->ClearStreamParams();
1880 } else {
1881 return DeleteSendChannel(ssrc_key);
1882 }
1883 return true;
1884}
1885
1886bool WebRtcVideoMediaChannel::AddRecvStream(const StreamParams& sp) {
1887 // TODO(zhurunz) Remove this once BWE works properly across different send
1888 // and receive channels.
1889 // Reuse default channel for recv stream in 1:1 call.
1890 if (!InConferenceMode() && first_receive_ssrc_ == 0) {
1891 LOG(LS_INFO) << "Recv stream " << sp.first_ssrc()
1892 << " reuse default channel #"
1893 << vie_channel_;
1894 first_receive_ssrc_ = sp.first_ssrc();
1895 if (render_started_) {
1896 if (engine()->vie()->render()->StartRender(vie_channel_) !=0) {
1897 LOG_RTCERR1(StartRender, vie_channel_);
1898 }
1899 }
1900 return true;
1901 }
1902
1903 if (recv_channels_.find(sp.first_ssrc()) != recv_channels_.end() ||
1904 first_receive_ssrc_ == sp.first_ssrc()) {
1905 LOG(LS_ERROR) << "Stream already exists";
1906 return false;
1907 }
1908
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00001909 // TODO(perkj): Implement recv media from multiple media SSRCs per stream.
1910 // NOTE: We have two SSRCs per stream when RTX is enabled.
1911 if (!IsOneSsrcStream(sp)) {
1912 LOG(LS_ERROR) << "WebRtcVideoMediaChannel supports one primary SSRC per"
1913 << " stream and one FID SSRC per primary SSRC.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001914 return false;
1915 }
1916
1917 // Create a new channel for receiving video data.
1918 // In order to get the bandwidth estimation work fine for
1919 // receive only channels, we connect all receiving channels
1920 // to our master send channel.
1921 int channel_id = -1;
1922 if (!CreateChannel(sp.first_ssrc(), MD_RECV, &channel_id)) {
1923 return false;
1924 }
1925
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00001926 // Set the corresponding RTX SSRC.
1927 uint32 rtx_ssrc;
1928 bool has_rtx = sp.GetFidSsrc(sp.first_ssrc(), &rtx_ssrc);
1929 if (has_rtx && engine()->vie()->rtp()->SetRemoteSSRCType(
1930 channel_id, webrtc::kViEStreamTypeRtx, rtx_ssrc) != 0) {
1931 LOG_RTCERR3(SetRemoteSSRCType, channel_id, webrtc::kViEStreamTypeRtx,
1932 rtx_ssrc);
1933 return false;
1934 }
1935
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001936 // Get the default renderer.
1937 VideoRenderer* default_renderer = NULL;
1938 if (InConferenceMode()) {
1939 // The recv_channels_ size start out being 1, so if it is two here this
1940 // is the first receive channel created (vie_channel_ is not used for
1941 // receiving in a conference call). This means that the renderer stored
1942 // inside vie_channel_ should be used for the just created channel.
1943 if (recv_channels_.size() == 2 &&
1944 recv_channels_.find(0) != recv_channels_.end()) {
1945 GetRenderer(0, &default_renderer);
1946 }
1947 }
1948
1949 // The first recv stream reuses the default renderer (if a default renderer
1950 // has been set).
1951 if (default_renderer) {
1952 SetRenderer(sp.first_ssrc(), default_renderer);
1953 }
1954
1955 LOG(LS_INFO) << "New video stream " << sp.first_ssrc()
1956 << " registered to VideoEngine channel #"
1957 << channel_id << " and connected to channel #" << vie_channel_;
1958
1959 return true;
1960}
1961
1962bool WebRtcVideoMediaChannel::RemoveRecvStream(uint32 ssrc) {
1963 RecvChannelMap::iterator it = recv_channels_.find(ssrc);
1964
1965 if (it == recv_channels_.end()) {
1966 // TODO(perkj): Remove this once BWE works properly across different send
1967 // and receive channels.
1968 // The default channel is reused for recv stream in 1:1 call.
1969 if (first_receive_ssrc_ == ssrc) {
1970 first_receive_ssrc_ = 0;
1971 // Need to stop the renderer and remove it since the render window can be
1972 // deleted after this.
1973 if (render_started_) {
1974 if (engine()->vie()->render()->StopRender(vie_channel_) !=0) {
1975 LOG_RTCERR1(StopRender, it->second->channel_id());
1976 }
1977 }
1978 recv_channels_[0]->SetRenderer(NULL);
1979 return true;
1980 }
1981 return false;
1982 }
1983 WebRtcVideoChannelRecvInfo* info = it->second;
1984 int channel_id = info->channel_id();
1985 if (engine()->vie()->render()->RemoveRenderer(channel_id) != 0) {
1986 LOG_RTCERR1(RemoveRenderer, channel_id);
1987 }
1988
1989 if (engine()->vie()->network()->DeregisterSendTransport(channel_id) !=0) {
1990 LOG_RTCERR1(DeRegisterSendTransport, channel_id);
1991 }
1992
1993 if (engine()->vie()->codec()->DeregisterDecoderObserver(
1994 channel_id) != 0) {
1995 LOG_RTCERR1(DeregisterDecoderObserver, channel_id);
1996 }
1997
1998 const WebRtcVideoChannelRecvInfo::DecoderMap& decoder_map =
1999 info->registered_decoders();
2000 for (WebRtcVideoChannelRecvInfo::DecoderMap::const_iterator it =
2001 decoder_map.begin(); it != decoder_map.end(); ++it) {
2002 if (engine()->vie()->ext_codec()->DeRegisterExternalReceiveCodec(
2003 channel_id, it->first) != 0) {
2004 LOG_RTCERR1(DeregisterDecoderObserver, channel_id);
2005 }
2006 engine()->DestroyExternalDecoder(it->second);
2007 }
2008 info->ClearRegisteredDecoders();
2009
2010 LOG(LS_INFO) << "Removing video stream " << ssrc
2011 << " with VideoEngine channel #"
2012 << channel_id;
wu@webrtc.org9caf2762013-12-11 18:25:07 +00002013 bool ret = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002014 if (engine()->vie()->base()->DeleteChannel(channel_id) == -1) {
2015 LOG_RTCERR1(DeleteChannel, channel_id);
wu@webrtc.org9caf2762013-12-11 18:25:07 +00002016 ret = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002017 }
2018 // Delete the WebRtcVideoChannelRecvInfo pointed to by it->second.
2019 delete info;
2020 recv_channels_.erase(it);
wu@webrtc.org9caf2762013-12-11 18:25:07 +00002021 return ret;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002022}
2023
2024bool WebRtcVideoMediaChannel::StartSend() {
2025 bool success = true;
2026 for (SendChannelMap::iterator iter = send_channels_.begin();
2027 iter != send_channels_.end(); ++iter) {
2028 WebRtcVideoChannelSendInfo* send_channel = iter->second;
2029 if (!StartSend(send_channel)) {
2030 success = false;
2031 }
2032 }
2033 return success;
2034}
2035
2036bool WebRtcVideoMediaChannel::StartSend(
2037 WebRtcVideoChannelSendInfo* send_channel) {
2038 const int channel_id = send_channel->channel_id();
2039 if (engine()->vie()->base()->StartSend(channel_id) != 0) {
2040 LOG_RTCERR1(StartSend, channel_id);
2041 return false;
2042 }
2043
2044 send_channel->set_sending(true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002045 return true;
2046}
2047
2048bool WebRtcVideoMediaChannel::StopSend() {
2049 bool success = true;
2050 for (SendChannelMap::iterator iter = send_channels_.begin();
2051 iter != send_channels_.end(); ++iter) {
2052 WebRtcVideoChannelSendInfo* send_channel = iter->second;
2053 if (!StopSend(send_channel)) {
2054 success = false;
2055 }
2056 }
2057 return success;
2058}
2059
2060bool WebRtcVideoMediaChannel::StopSend(
2061 WebRtcVideoChannelSendInfo* send_channel) {
2062 const int channel_id = send_channel->channel_id();
2063 if (engine()->vie()->base()->StopSend(channel_id) != 0) {
2064 LOG_RTCERR1(StopSend, channel_id);
2065 return false;
2066 }
2067 send_channel->set_sending(false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002068 return true;
2069}
2070
2071bool WebRtcVideoMediaChannel::SendIntraFrame() {
2072 bool success = true;
2073 for (SendChannelMap::iterator iter = send_channels_.begin();
2074 iter != send_channels_.end();
2075 ++iter) {
2076 WebRtcVideoChannelSendInfo* send_channel = iter->second;
2077 const int channel_id = send_channel->channel_id();
2078 if (engine()->vie()->codec()->SendKeyFrame(channel_id) != 0) {
2079 LOG_RTCERR1(SendKeyFrame, channel_id);
2080 success = false;
2081 }
2082 }
2083 return success;
2084}
2085
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002086bool WebRtcVideoMediaChannel::HasReadySendChannels() {
2087 return !send_channels_.empty() &&
2088 ((send_channels_.size() > 1) ||
2089 (send_channels_[0]->stream_params() != NULL));
2090}
2091
2092bool WebRtcVideoMediaChannel::GetSendChannelKey(uint32 local_ssrc,
2093 uint32* key) {
2094 *key = 0;
2095 // If a send channel is not ready to send it will not have local_ssrc
2096 // registered to it.
2097 if (!HasReadySendChannels()) {
2098 return false;
2099 }
2100 // The default channel is stored with key 0. The key therefore does not match
2101 // the SSRC associated with the default channel. Check if the SSRC provided
2102 // corresponds to the default channel's SSRC.
2103 if (local_ssrc == GetDefaultChannelSsrc()) {
2104 return true;
2105 }
2106 if (send_channels_.find(local_ssrc) == send_channels_.end()) {
2107 for (SendChannelMap::iterator iter = send_channels_.begin();
2108 iter != send_channels_.end(); ++iter) {
2109 WebRtcVideoChannelSendInfo* send_channel = iter->second;
2110 if (send_channel->has_ssrc(local_ssrc)) {
2111 *key = iter->first;
2112 return true;
2113 }
2114 }
2115 return false;
2116 }
2117 // The key was found in the above std::map::find call. This means that the
2118 // ssrc is the key.
2119 *key = local_ssrc;
2120 return true;
2121}
2122
2123WebRtcVideoChannelSendInfo* WebRtcVideoMediaChannel::GetSendChannel(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002124 uint32 local_ssrc) {
2125 uint32 key;
2126 if (!GetSendChannelKey(local_ssrc, &key)) {
2127 return NULL;
2128 }
2129 return send_channels_[key];
2130}
2131
2132bool WebRtcVideoMediaChannel::CreateSendChannelKey(uint32 local_ssrc,
2133 uint32* key) {
2134 if (GetSendChannelKey(local_ssrc, key)) {
2135 // If there is a key corresponding to |local_ssrc|, the SSRC is already in
2136 // use. SSRCs need to be unique in a session and at this point a duplicate
2137 // SSRC has been detected.
2138 return false;
2139 }
2140 if (send_channels_[0]->stream_params() == NULL) {
2141 // key should be 0 here as the default channel should be re-used whenever it
2142 // is not used.
2143 *key = 0;
2144 return true;
2145 }
2146 // SSRC is currently not in use and the default channel is already in use. Use
2147 // the SSRC as key since it is supposed to be unique in a session.
2148 *key = local_ssrc;
2149 return true;
2150}
2151
wu@webrtc.org24301a62013-12-13 19:17:43 +00002152int WebRtcVideoMediaChannel::GetSendChannelNum(VideoCapturer* capturer) {
2153 int num = 0;
2154 for (SendChannelMap::iterator iter = send_channels_.begin();
2155 iter != send_channels_.end(); ++iter) {
2156 WebRtcVideoChannelSendInfo* send_channel = iter->second;
2157 if (send_channel->video_capturer() == capturer) {
2158 ++num;
2159 }
2160 }
2161 return num;
2162}
2163
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002164uint32 WebRtcVideoMediaChannel::GetDefaultChannelSsrc() {
2165 WebRtcVideoChannelSendInfo* send_channel = send_channels_[0];
2166 const StreamParams* sp = send_channel->stream_params();
2167 if (sp == NULL) {
2168 // This happens if no send stream is currently registered.
2169 return 0;
2170 }
2171 return sp->first_ssrc();
2172}
2173
2174bool WebRtcVideoMediaChannel::DeleteSendChannel(uint32 ssrc_key) {
2175 if (send_channels_.find(ssrc_key) == send_channels_.end()) {
2176 return false;
2177 }
2178 WebRtcVideoChannelSendInfo* send_channel = send_channels_[ssrc_key];
wu@webrtc.org24301a62013-12-13 19:17:43 +00002179 MaybeDisconnectCapturer(send_channel->video_capturer());
2180 send_channel->set_video_capturer(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002181
2182 int channel_id = send_channel->channel_id();
2183 int capture_id = send_channel->capture_id();
2184 if (engine()->vie()->codec()->DeregisterEncoderObserver(
2185 channel_id) != 0) {
2186 LOG_RTCERR1(DeregisterEncoderObserver, channel_id);
2187 }
2188
2189 // Destroy the external capture interface.
2190 if (engine()->vie()->capture()->DisconnectCaptureDevice(
2191 channel_id) != 0) {
2192 LOG_RTCERR1(DisconnectCaptureDevice, channel_id);
2193 }
2194 if (engine()->vie()->capture()->ReleaseCaptureDevice(
2195 capture_id) != 0) {
2196 LOG_RTCERR1(ReleaseCaptureDevice, capture_id);
2197 }
2198
2199 // The default channel is stored in both |send_channels_| and
2200 // |recv_channels_|. To make sure it is only deleted once from vie let the
2201 // delete call happen when tearing down |recv_channels_| and not here.
2202 if (!IsDefaultChannel(channel_id)) {
2203 engine_->vie()->base()->DeleteChannel(channel_id);
2204 }
2205 delete send_channel;
2206 send_channels_.erase(ssrc_key);
2207 return true;
2208}
2209
2210bool WebRtcVideoMediaChannel::RemoveCapturer(uint32 ssrc) {
2211 WebRtcVideoChannelSendInfo* send_channel = GetSendChannel(ssrc);
2212 if (!send_channel) {
2213 return false;
2214 }
2215 VideoCapturer* capturer = send_channel->video_capturer();
2216 if (capturer == NULL) {
2217 return false;
2218 }
wu@webrtc.org24301a62013-12-13 19:17:43 +00002219 MaybeDisconnectCapturer(capturer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002220 send_channel->set_video_capturer(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002221 const int64 timestamp = send_channel->local_stream_info()->time_stamp();
2222 if (send_codec_) {
2223 QueueBlackFrame(ssrc, timestamp, send_codec_->maxFramerate);
2224 }
2225 return true;
2226}
2227
2228bool WebRtcVideoMediaChannel::SetRenderer(uint32 ssrc,
2229 VideoRenderer* renderer) {
2230 if (recv_channels_.find(ssrc) == recv_channels_.end()) {
2231 // TODO(perkj): Remove this once BWE works properly across different send
2232 // and receive channels.
2233 // The default channel is reused for recv stream in 1:1 call.
2234 if (first_receive_ssrc_ == ssrc &&
2235 recv_channels_.find(0) != recv_channels_.end()) {
2236 LOG(LS_INFO) << "SetRenderer " << ssrc
2237 << " reuse default channel #"
2238 << vie_channel_;
2239 recv_channels_[0]->SetRenderer(renderer);
2240 return true;
2241 }
2242 return false;
2243 }
2244
2245 recv_channels_[ssrc]->SetRenderer(renderer);
2246 return true;
2247}
2248
2249bool WebRtcVideoMediaChannel::GetStats(VideoMediaInfo* info) {
2250 // Get sender statistics and build VideoSenderInfo.
2251 unsigned int total_bitrate_sent = 0;
2252 unsigned int video_bitrate_sent = 0;
2253 unsigned int fec_bitrate_sent = 0;
2254 unsigned int nack_bitrate_sent = 0;
2255 unsigned int estimated_send_bandwidth = 0;
2256 unsigned int target_enc_bitrate = 0;
2257 if (send_codec_) {
2258 for (SendChannelMap::const_iterator iter = send_channels_.begin();
2259 iter != send_channels_.end(); ++iter) {
2260 WebRtcVideoChannelSendInfo* send_channel = iter->second;
2261 const int channel_id = send_channel->channel_id();
2262 VideoSenderInfo sinfo;
2263 const StreamParams* send_params = send_channel->stream_params();
2264 if (send_params == NULL) {
2265 // This should only happen if the default vie channel is not in use.
2266 // This can happen if no streams have ever been added or the stream
2267 // corresponding to the default channel has been removed. Note that
2268 // there may be non-default vie channels in use when this happen so
2269 // asserting send_channels_.size() == 1 is not correct and neither is
2270 // breaking out of the loop.
2271 ASSERT(channel_id == vie_channel_);
2272 continue;
2273 }
2274 unsigned int bytes_sent, packets_sent, bytes_recv, packets_recv;
2275 if (engine_->vie()->rtp()->GetRTPStatistics(channel_id, bytes_sent,
2276 packets_sent, bytes_recv,
2277 packets_recv) != 0) {
2278 LOG_RTCERR1(GetRTPStatistics, vie_channel_);
2279 continue;
2280 }
2281 WebRtcLocalStreamInfo* channel_stream_info =
2282 send_channel->local_stream_info();
2283
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002284 for (size_t i = 0; i < send_params->ssrcs.size(); ++i) {
2285 sinfo.add_ssrc(send_params->ssrcs[i]);
2286 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002287 sinfo.codec_name = send_codec_->plName;
2288 sinfo.bytes_sent = bytes_sent;
2289 sinfo.packets_sent = packets_sent;
2290 sinfo.packets_cached = -1;
2291 sinfo.packets_lost = -1;
2292 sinfo.fraction_lost = -1;
2293 sinfo.firs_rcvd = -1;
2294 sinfo.nacks_rcvd = -1;
2295 sinfo.rtt_ms = -1;
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00002296 sinfo.frame_width = static_cast<int>(channel_stream_info->width());
2297 sinfo.frame_height = static_cast<int>(channel_stream_info->height());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002298 sinfo.framerate_input = channel_stream_info->framerate();
2299 sinfo.framerate_sent = send_channel->encoder_observer()->framerate();
2300 sinfo.nominal_bitrate = send_channel->encoder_observer()->bitrate();
2301 sinfo.preferred_bitrate = send_max_bitrate_;
2302 sinfo.adapt_reason = send_channel->CurrentAdaptReason();
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002303 sinfo.capture_jitter_ms = -1;
2304 sinfo.avg_encode_ms = -1;
wu@webrtc.org9caf2762013-12-11 18:25:07 +00002305 sinfo.encode_usage_percent = -1;
2306 sinfo.capture_queue_delay_ms_per_s = -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002307
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002308#ifdef USE_WEBRTC_DEV_BRANCH
2309 int capture_jitter_ms = 0;
2310 int avg_encode_time_ms = 0;
wu@webrtc.org9caf2762013-12-11 18:25:07 +00002311 int encode_usage_percent = 0;
2312 int capture_queue_delay_ms_per_s = 0;
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002313 if (engine()->vie()->base()->CpuOveruseMeasures(
wu@webrtc.org9caf2762013-12-11 18:25:07 +00002314 channel_id,
2315 &capture_jitter_ms,
2316 &avg_encode_time_ms,
2317 &encode_usage_percent,
2318 &capture_queue_delay_ms_per_s) == 0) {
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002319 sinfo.capture_jitter_ms = capture_jitter_ms;
2320 sinfo.avg_encode_ms = avg_encode_time_ms;
wu@webrtc.org9caf2762013-12-11 18:25:07 +00002321 sinfo.encode_usage_percent = encode_usage_percent;
2322 sinfo.capture_queue_delay_ms_per_s = capture_queue_delay_ms_per_s;
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002323 }
2324#endif
2325
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002326 // Get received RTCP statistics for the sender (reported by the remote
2327 // client in a RTCP packet), if available.
2328 // It's not a fatal error if we can't, since RTCP may not have arrived
2329 // yet.
2330 webrtc::RtcpStatistics outgoing_stream_rtcp_stats;
2331 int outgoing_stream_rtt_ms;
2332
2333 if (engine_->vie()->rtp()->GetSendChannelRtcpStatistics(
2334 channel_id,
2335 outgoing_stream_rtcp_stats,
2336 outgoing_stream_rtt_ms) == 0) {
2337 // Convert Q8 to float.
2338 sinfo.packets_lost = outgoing_stream_rtcp_stats.cumulative_lost;
2339 sinfo.fraction_lost = static_cast<float>(
2340 outgoing_stream_rtcp_stats.fraction_lost) / (1 << 8);
2341 sinfo.rtt_ms = outgoing_stream_rtt_ms;
2342 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002343 info->senders.push_back(sinfo);
2344
2345 unsigned int channel_total_bitrate_sent = 0;
2346 unsigned int channel_video_bitrate_sent = 0;
2347 unsigned int channel_fec_bitrate_sent = 0;
2348 unsigned int channel_nack_bitrate_sent = 0;
2349 if (engine_->vie()->rtp()->GetBandwidthUsage(
2350 channel_id, channel_total_bitrate_sent, channel_video_bitrate_sent,
2351 channel_fec_bitrate_sent, channel_nack_bitrate_sent) == 0) {
2352 total_bitrate_sent += channel_total_bitrate_sent;
2353 video_bitrate_sent += channel_video_bitrate_sent;
2354 fec_bitrate_sent += channel_fec_bitrate_sent;
2355 nack_bitrate_sent += channel_nack_bitrate_sent;
2356 } else {
2357 LOG_RTCERR1(GetBandwidthUsage, channel_id);
2358 }
2359
2360 unsigned int estimated_stream_send_bandwidth = 0;
2361 if (engine_->vie()->rtp()->GetEstimatedSendBandwidth(
2362 channel_id, &estimated_stream_send_bandwidth) == 0) {
2363 estimated_send_bandwidth += estimated_stream_send_bandwidth;
2364 } else {
2365 LOG_RTCERR1(GetEstimatedSendBandwidth, channel_id);
2366 }
2367 unsigned int target_enc_stream_bitrate = 0;
2368 if (engine_->vie()->codec()->GetCodecTargetBitrate(
2369 channel_id, &target_enc_stream_bitrate) == 0) {
2370 target_enc_bitrate += target_enc_stream_bitrate;
2371 } else {
2372 LOG_RTCERR1(GetCodecTargetBitrate, channel_id);
2373 }
2374 }
2375 } else {
2376 LOG(LS_WARNING) << "GetStats: sender information not ready.";
2377 }
2378
2379 // Get the SSRC and stats for each receiver, based on our own calculations.
2380 unsigned int estimated_recv_bandwidth = 0;
2381 for (RecvChannelMap::const_iterator it = recv_channels_.begin();
2382 it != recv_channels_.end(); ++it) {
2383 // Don't report receive statistics from the default channel if we have
2384 // specified receive channels.
2385 if (it->first == 0 && recv_channels_.size() > 1)
2386 continue;
2387 WebRtcVideoChannelRecvInfo* channel = it->second;
2388
2389 unsigned int ssrc;
2390 // Get receiver statistics and build VideoReceiverInfo, if we have data.
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +00002391 // Skip the default channel (ssrc == 0).
2392 if (engine_->vie()->rtp()->GetRemoteSSRC(
2393 channel->channel_id(), ssrc) != 0 ||
2394 ssrc == 0)
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002395 continue;
2396
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002397 webrtc::StreamDataCounters sent;
2398 webrtc::StreamDataCounters received;
2399 if (engine_->vie()->rtp()->GetRtpStatistics(channel->channel_id(),
2400 sent, received) != 0) {
2401 LOG_RTCERR1(GetRTPStatistics, channel->channel_id());
2402 return false;
2403 }
2404 VideoReceiverInfo rinfo;
2405 rinfo.add_ssrc(ssrc);
2406 rinfo.bytes_rcvd = received.bytes;
2407 rinfo.packets_rcvd = received.packets;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002408 rinfo.packets_lost = -1;
2409 rinfo.packets_concealed = -1;
2410 rinfo.fraction_lost = -1; // from SentRTCP
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002411 rinfo.nacks_sent = -1;
2412 rinfo.frame_width = channel->render_adapter()->width();
2413 rinfo.frame_height = channel->render_adapter()->height();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002414 int fps = channel->render_adapter()->framerate();
2415 rinfo.framerate_decoded = fps;
2416 rinfo.framerate_output = fps;
wu@webrtc.org97077a32013-10-25 21:18:33 +00002417 channel->decoder_observer()->ExportTo(&rinfo);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002418
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002419 // Get our locally created statistics of the received RTP stream.
2420 webrtc::RtcpStatistics incoming_stream_rtcp_stats;
2421 int incoming_stream_rtt_ms;
2422 if (engine_->vie()->rtp()->GetReceiveChannelRtcpStatistics(
2423 channel->channel_id(),
2424 incoming_stream_rtcp_stats,
2425 incoming_stream_rtt_ms) == 0) {
2426 // Convert Q8 to float.
2427 rinfo.packets_lost = incoming_stream_rtcp_stats.cumulative_lost;
2428 rinfo.fraction_lost = static_cast<float>(
2429 incoming_stream_rtcp_stats.fraction_lost) / (1 << 8);
2430 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002431 info->receivers.push_back(rinfo);
2432
2433 unsigned int estimated_recv_stream_bandwidth = 0;
2434 if (engine_->vie()->rtp()->GetEstimatedReceiveBandwidth(
2435 channel->channel_id(), &estimated_recv_stream_bandwidth) == 0) {
2436 estimated_recv_bandwidth += estimated_recv_stream_bandwidth;
2437 } else {
2438 LOG_RTCERR1(GetEstimatedReceiveBandwidth, channel->channel_id());
2439 }
2440 }
2441
2442 // Build BandwidthEstimationInfo.
2443 // TODO(zhurunz): Add real unittest for this.
2444 BandwidthEstimationInfo bwe;
2445
2446 // Calculations done above per send/receive stream.
2447 bwe.actual_enc_bitrate = video_bitrate_sent;
2448 bwe.transmit_bitrate = total_bitrate_sent;
2449 bwe.retransmit_bitrate = nack_bitrate_sent;
2450 bwe.available_send_bandwidth = estimated_send_bandwidth;
2451 bwe.available_recv_bandwidth = estimated_recv_bandwidth;
2452 bwe.target_enc_bitrate = target_enc_bitrate;
2453
2454 info->bw_estimations.push_back(bwe);
2455
2456 return true;
2457}
2458
2459bool WebRtcVideoMediaChannel::SetCapturer(uint32 ssrc,
2460 VideoCapturer* capturer) {
2461 ASSERT(ssrc != 0);
2462 if (!capturer) {
2463 return RemoveCapturer(ssrc);
2464 }
2465 WebRtcVideoChannelSendInfo* send_channel = GetSendChannel(ssrc);
2466 if (!send_channel) {
2467 return false;
2468 }
2469 VideoCapturer* old_capturer = send_channel->video_capturer();
wu@webrtc.org24301a62013-12-13 19:17:43 +00002470 MaybeDisconnectCapturer(old_capturer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002471
2472 send_channel->set_video_capturer(capturer);
wu@webrtc.org24301a62013-12-13 19:17:43 +00002473 MaybeConnectCapturer(capturer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002474 if (!capturer->IsScreencast() && ratio_w_ != 0 && ratio_h_ != 0) {
2475 capturer->UpdateAspectRatio(ratio_w_, ratio_h_);
2476 }
2477 const int64 timestamp = send_channel->local_stream_info()->time_stamp();
2478 if (send_codec_) {
2479 QueueBlackFrame(ssrc, timestamp, send_codec_->maxFramerate);
2480 }
2481 return true;
2482}
2483
2484bool WebRtcVideoMediaChannel::RequestIntraFrame() {
2485 // There is no API exposed to application to request a key frame
2486 // ViE does this internally when there are errors from decoder
2487 return false;
2488}
2489
wu@webrtc.orga9890802013-12-13 00:21:03 +00002490void WebRtcVideoMediaChannel::OnPacketReceived(
2491 talk_base::Buffer* packet, const talk_base::PacketTime& packet_time) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002492 // Pick which channel to send this packet to. If this packet doesn't match
2493 // any multiplexed streams, just send it to the default channel. Otherwise,
2494 // send it to the specific decoder instance for that stream.
2495 uint32 ssrc = 0;
2496 if (!GetRtpSsrc(packet->data(), packet->length(), &ssrc))
2497 return;
2498 int which_channel = GetRecvChannelNum(ssrc);
2499 if (which_channel == -1) {
2500 which_channel = video_channel();
2501 }
2502
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00002503 engine()->vie()->network()->ReceivedRTPPacket(
2504 which_channel,
2505 packet->data(),
wu@webrtc.orga9890802013-12-13 00:21:03 +00002506#ifdef USE_WEBRTC_DEV_BRANCH
2507 static_cast<int>(packet->length()),
2508 webrtc::PacketTime(packet_time.timestamp, packet_time.not_before));
2509#else
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00002510 static_cast<int>(packet->length()));
wu@webrtc.orga9890802013-12-13 00:21:03 +00002511#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002512}
2513
wu@webrtc.orga9890802013-12-13 00:21:03 +00002514void WebRtcVideoMediaChannel::OnRtcpReceived(
2515 talk_base::Buffer* packet, const talk_base::PacketTime& packet_time) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002516// Sending channels need all RTCP packets with feedback information.
2517// Even sender reports can contain attached report blocks.
2518// Receiving channels need sender reports in order to create
2519// correct receiver reports.
2520
2521 uint32 ssrc = 0;
2522 if (!GetRtcpSsrc(packet->data(), packet->length(), &ssrc)) {
2523 LOG(LS_WARNING) << "Failed to parse SSRC from received RTCP packet";
2524 return;
2525 }
2526 int type = 0;
2527 if (!GetRtcpType(packet->data(), packet->length(), &type)) {
2528 LOG(LS_WARNING) << "Failed to parse type from received RTCP packet";
2529 return;
2530 }
2531
2532 // If it is a sender report, find the channel that is listening.
2533 if (type == kRtcpTypeSR) {
2534 int which_channel = GetRecvChannelNum(ssrc);
2535 if (which_channel != -1 && !IsDefaultChannel(which_channel)) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00002536 engine_->vie()->network()->ReceivedRTCPPacket(
2537 which_channel,
2538 packet->data(),
2539 static_cast<int>(packet->length()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002540 }
2541 }
2542 // SR may continue RR and any RR entry may correspond to any one of the send
2543 // channels. So all RTCP packets must be forwarded all send channels. ViE
2544 // will filter out RR internally.
2545 for (SendChannelMap::iterator iter = send_channels_.begin();
2546 iter != send_channels_.end(); ++iter) {
2547 WebRtcVideoChannelSendInfo* send_channel = iter->second;
2548 int channel_id = send_channel->channel_id();
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00002549 engine_->vie()->network()->ReceivedRTCPPacket(
2550 channel_id,
2551 packet->data(),
2552 static_cast<int>(packet->length()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002553 }
2554}
2555
2556void WebRtcVideoMediaChannel::OnReadyToSend(bool ready) {
2557 SetNetworkTransmissionState(ready);
2558}
2559
2560bool WebRtcVideoMediaChannel::MuteStream(uint32 ssrc, bool muted) {
2561 WebRtcVideoChannelSendInfo* send_channel = GetSendChannel(ssrc);
2562 if (!send_channel) {
2563 LOG(LS_ERROR) << "The specified ssrc " << ssrc << " is not in use.";
2564 return false;
2565 }
2566 send_channel->set_muted(muted);
2567 return true;
2568}
2569
2570bool WebRtcVideoMediaChannel::SetRecvRtpHeaderExtensions(
2571 const std::vector<RtpHeaderExtension>& extensions) {
2572 if (receive_extensions_ == extensions) {
2573 return true;
2574 }
2575 receive_extensions_ = extensions;
2576
2577 const RtpHeaderExtension* offset_extension =
2578 FindHeaderExtension(extensions, kRtpTimestampOffsetHeaderExtension);
2579 const RtpHeaderExtension* send_time_extension =
2580 FindHeaderExtension(extensions, kRtpAbsoluteSendTimeHeaderExtension);
2581
2582 // Loop through all receive channels and enable/disable the extensions.
2583 for (RecvChannelMap::iterator channel_it = recv_channels_.begin();
2584 channel_it != recv_channels_.end(); ++channel_it) {
2585 int channel_id = channel_it->second->channel_id();
2586 if (!SetHeaderExtension(
2587 &webrtc::ViERTP_RTCP::SetReceiveTimestampOffsetStatus, channel_id,
2588 offset_extension)) {
2589 return false;
2590 }
2591 if (!SetHeaderExtension(
2592 &webrtc::ViERTP_RTCP::SetReceiveAbsoluteSendTimeStatus, channel_id,
2593 send_time_extension)) {
2594 return false;
2595 }
2596 }
2597 return true;
2598}
2599
2600bool WebRtcVideoMediaChannel::SetSendRtpHeaderExtensions(
2601 const std::vector<RtpHeaderExtension>& extensions) {
2602 send_extensions_ = extensions;
2603
2604 const RtpHeaderExtension* offset_extension =
2605 FindHeaderExtension(extensions, kRtpTimestampOffsetHeaderExtension);
2606 const RtpHeaderExtension* send_time_extension =
2607 FindHeaderExtension(extensions, kRtpAbsoluteSendTimeHeaderExtension);
2608
2609 // Loop through all send channels and enable/disable the extensions.
2610 for (SendChannelMap::iterator channel_it = send_channels_.begin();
2611 channel_it != send_channels_.end(); ++channel_it) {
2612 int channel_id = channel_it->second->channel_id();
2613 if (!SetHeaderExtension(
2614 &webrtc::ViERTP_RTCP::SetSendTimestampOffsetStatus, channel_id,
2615 offset_extension)) {
2616 return false;
2617 }
2618 if (!SetHeaderExtension(
2619 &webrtc::ViERTP_RTCP::SetSendAbsoluteSendTimeStatus, channel_id,
2620 send_time_extension)) {
2621 return false;
2622 }
2623 }
2624 return true;
2625}
2626
2627bool WebRtcVideoMediaChannel::SetSendBandwidth(bool autobw, int bps) {
2628 LOG(LS_INFO) << "WebRtcVideoMediaChanne::SetSendBandwidth";
2629
2630 if (InConferenceMode()) {
2631 LOG(LS_INFO) << "Conference mode ignores SetSendBandWidth";
2632 return true;
2633 }
2634
2635 if (!send_codec_) {
2636 LOG(LS_INFO) << "The send codec has not been set up yet";
2637 return true;
2638 }
2639
2640 int min_bitrate;
2641 int start_bitrate;
2642 int max_bitrate;
2643 if (autobw) {
2644 // Use the default values for min bitrate.
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00002645 min_bitrate = send_min_bitrate_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002646 // Use the default value or the bps for the max
2647 max_bitrate = (bps <= 0) ? send_max_bitrate_ : (bps / 1000);
2648 // Maximum start bitrate can be kStartVideoBitrate.
2649 start_bitrate = talk_base::_min(kStartVideoBitrate, max_bitrate);
2650 } else {
2651 // Use the default start or the bps as the target bitrate.
2652 int target_bitrate = (bps <= 0) ? kStartVideoBitrate : (bps / 1000);
2653 min_bitrate = target_bitrate;
2654 start_bitrate = target_bitrate;
2655 max_bitrate = target_bitrate;
2656 }
2657
2658 if (!SetSendCodec(*send_codec_, min_bitrate, start_bitrate, max_bitrate)) {
2659 return false;
2660 }
2661 LogSendCodecChange("SetSendBandwidth()");
2662
2663 return true;
2664}
2665
2666bool WebRtcVideoMediaChannel::SetOptions(const VideoOptions &options) {
2667 // Always accept options that are unchanged.
2668 if (options_ == options) {
2669 return true;
2670 }
2671
2672 // Trigger SetSendCodec to set correct noise reduction state if the option has
2673 // changed.
2674 bool denoiser_changed = options.video_noise_reduction.IsSet() &&
2675 (options_.video_noise_reduction != options.video_noise_reduction);
2676
2677 bool leaky_bucket_changed = options.video_leaky_bucket.IsSet() &&
2678 (options_.video_leaky_bucket != options.video_leaky_bucket);
2679
2680 bool buffer_latency_changed = options.buffered_mode_latency.IsSet() &&
2681 (options_.buffered_mode_latency != options.buffered_mode_latency);
2682
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00002683 bool cpu_overuse_detection_changed = options.cpu_overuse_detection.IsSet() &&
2684 (options_.cpu_overuse_detection != options.cpu_overuse_detection);
2685
wu@webrtc.orgde305012013-10-31 15:40:38 +00002686 bool dscp_option_changed = (options_.dscp != options.dscp);
2687
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002688 bool suspend_below_min_bitrate_changed =
2689 options.suspend_below_min_bitrate.IsSet() &&
2690 (options_.suspend_below_min_bitrate != options.suspend_below_min_bitrate);
2691
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002692 bool conference_mode_turned_off = false;
2693 if (options_.conference_mode.IsSet() && options.conference_mode.IsSet() &&
2694 options_.conference_mode.GetWithDefaultIfUnset(false) &&
2695 !options.conference_mode.GetWithDefaultIfUnset(false)) {
2696 conference_mode_turned_off = true;
2697 }
2698
2699 // Save the options, to be interpreted where appropriate.
2700 // Use options_.SetAll() instead of assignment so that unset value in options
2701 // will not overwrite the previous option value.
2702 options_.SetAll(options);
2703
2704 // Set CPU options for all send channels.
2705 for (SendChannelMap::iterator iter = send_channels_.begin();
2706 iter != send_channels_.end(); ++iter) {
2707 WebRtcVideoChannelSendInfo* send_channel = iter->second;
2708 send_channel->ApplyCpuOptions(options_);
2709 }
2710
2711 // Adjust send codec bitrate if needed.
2712 int conf_max_bitrate = kDefaultConferenceModeMaxVideoBitrate;
2713
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00002714 // Save altered min_bitrate level and apply if necessary.
2715 bool adjusted_min_bitrate = false;
2716 if (options.lower_min_bitrate.IsSet()) {
2717 bool lower;
2718 options.lower_min_bitrate.Get(&lower);
2719
2720 int new_send_min_bitrate = lower ? kLowerMinBitrate : kMinVideoBitrate;
2721 adjusted_min_bitrate = (new_send_min_bitrate != send_min_bitrate_);
2722 send_min_bitrate_ = new_send_min_bitrate;
2723 }
2724
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002725 int expected_bitrate = send_max_bitrate_;
2726 if (InConferenceMode()) {
2727 expected_bitrate = conf_max_bitrate;
2728 } else if (conference_mode_turned_off) {
2729 // This is a special case for turning conference mode off.
2730 // Max bitrate should go back to the default maximum value instead
2731 // of the current maximum.
2732 expected_bitrate = kMaxVideoBitrate;
2733 }
2734
2735 if (send_codec_ &&
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00002736 (send_max_bitrate_ != expected_bitrate || denoiser_changed ||
2737 adjusted_min_bitrate)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002738 // On success, SetSendCodec() will reset send_max_bitrate_ to
2739 // expected_bitrate.
2740 if (!SetSendCodec(*send_codec_,
2741 send_min_bitrate_,
2742 send_start_bitrate_,
2743 expected_bitrate)) {
2744 return false;
2745 }
2746 LogSendCodecChange("SetOptions()");
2747 }
2748 if (leaky_bucket_changed) {
2749 bool enable_leaky_bucket =
2750 options_.video_leaky_bucket.GetWithDefaultIfUnset(false);
2751 for (SendChannelMap::iterator it = send_channels_.begin();
2752 it != send_channels_.end(); ++it) {
2753 if (engine()->vie()->rtp()->SetTransmissionSmoothingStatus(
2754 it->second->channel_id(), enable_leaky_bucket) != 0) {
2755 LOG_RTCERR2(SetTransmissionSmoothingStatus, it->second->channel_id(),
2756 enable_leaky_bucket);
2757 }
2758 }
2759 }
2760 if (buffer_latency_changed) {
2761 int buffer_latency =
2762 options_.buffered_mode_latency.GetWithDefaultIfUnset(
2763 cricket::kBufferedModeDisabled);
2764 for (SendChannelMap::iterator it = send_channels_.begin();
2765 it != send_channels_.end(); ++it) {
2766 if (engine()->vie()->rtp()->SetSenderBufferingMode(
2767 it->second->channel_id(), buffer_latency) != 0) {
2768 LOG_RTCERR2(SetSenderBufferingMode, it->second->channel_id(),
2769 buffer_latency);
2770 }
2771 }
2772 for (RecvChannelMap::iterator it = recv_channels_.begin();
2773 it != recv_channels_.end(); ++it) {
2774 if (engine()->vie()->rtp()->SetReceiverBufferingMode(
2775 it->second->channel_id(), buffer_latency) != 0) {
2776 LOG_RTCERR2(SetReceiverBufferingMode, it->second->channel_id(),
2777 buffer_latency);
2778 }
2779 }
2780 }
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00002781 if (cpu_overuse_detection_changed) {
2782 bool cpu_overuse_detection =
2783 options_.cpu_overuse_detection.GetWithDefaultIfUnset(false);
2784 for (SendChannelMap::iterator iter = send_channels_.begin();
2785 iter != send_channels_.end(); ++iter) {
2786 WebRtcVideoChannelSendInfo* send_channel = iter->second;
2787 send_channel->SetCpuOveruseDetection(cpu_overuse_detection);
2788 }
2789 }
wu@webrtc.orgde305012013-10-31 15:40:38 +00002790 if (dscp_option_changed) {
2791 talk_base::DiffServCodePoint dscp = talk_base::DSCP_DEFAULT;
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00002792 if (options_.dscp.GetWithDefaultIfUnset(false))
wu@webrtc.orgde305012013-10-31 15:40:38 +00002793 dscp = kVideoDscpValue;
2794 if (MediaChannel::SetDscp(dscp) != 0) {
2795 LOG(LS_WARNING) << "Failed to set DSCP settings for video channel";
2796 }
2797 }
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002798 if (suspend_below_min_bitrate_changed) {
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002799 if (options_.suspend_below_min_bitrate.GetWithDefaultIfUnset(false)) {
2800 for (SendChannelMap::iterator it = send_channels_.begin();
2801 it != send_channels_.end(); ++it) {
2802 engine()->vie()->codec()->SuspendBelowMinBitrate(
2803 it->second->channel_id());
2804 }
2805 } else {
2806 LOG(LS_WARNING) << "Cannot disable video suspension once it is enabled";
2807 }
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002808 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002809 return true;
2810}
2811
2812void WebRtcVideoMediaChannel::SetInterface(NetworkInterface* iface) {
2813 MediaChannel::SetInterface(iface);
2814 // Set the RTP recv/send buffer to a bigger size
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002815 MediaChannel::SetOption(NetworkInterface::ST_RTP,
2816 talk_base::Socket::OPT_RCVBUF,
2817 kVideoRtpBufferSize);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002818
2819 // TODO(sriniv): Remove or re-enable this.
2820 // As part of b/8030474, send-buffer is size now controlled through
2821 // portallocator flags.
2822 // network_interface_->SetOption(NetworkInterface::ST_RTP,
2823 // talk_base::Socket::OPT_SNDBUF,
2824 // kVideoRtpBufferSize);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002825}
2826
2827void WebRtcVideoMediaChannel::UpdateAspectRatio(int ratio_w, int ratio_h) {
2828 ASSERT(ratio_w != 0);
2829 ASSERT(ratio_h != 0);
2830 ratio_w_ = ratio_w;
2831 ratio_h_ = ratio_h;
2832 // For now assume that all streams want the same aspect ratio.
2833 // TODO(hellner): remove the need for this assumption.
2834 for (SendChannelMap::iterator iter = send_channels_.begin();
2835 iter != send_channels_.end(); ++iter) {
2836 WebRtcVideoChannelSendInfo* send_channel = iter->second;
2837 VideoCapturer* capturer = send_channel->video_capturer();
2838 if (capturer) {
2839 capturer->UpdateAspectRatio(ratio_w, ratio_h);
2840 }
2841 }
2842}
2843
2844bool WebRtcVideoMediaChannel::GetRenderer(uint32 ssrc,
2845 VideoRenderer** renderer) {
2846 RecvChannelMap::const_iterator it = recv_channels_.find(ssrc);
2847 if (it == recv_channels_.end()) {
2848 if (first_receive_ssrc_ == ssrc &&
2849 recv_channels_.find(0) != recv_channels_.end()) {
2850 LOG(LS_INFO) << " GetRenderer " << ssrc
2851 << " reuse default renderer #"
2852 << vie_channel_;
2853 *renderer = recv_channels_[0]->render_adapter()->renderer();
2854 return true;
2855 }
2856 return false;
2857 }
2858
2859 *renderer = it->second->render_adapter()->renderer();
2860 return true;
2861}
2862
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002863void WebRtcVideoMediaChannel::SendFrame(VideoCapturer* capturer,
2864 const VideoFrame* frame) {
wu@webrtc.org24301a62013-12-13 19:17:43 +00002865 // If the |capturer| is registered to any send channel, then send the frame
2866 // to those send channels.
2867 bool capturer_is_channel_owned = false;
2868 for (SendChannelMap::iterator iter = send_channels_.begin();
2869 iter != send_channels_.end(); ++iter) {
2870 WebRtcVideoChannelSendInfo* send_channel = iter->second;
2871 if (send_channel->video_capturer() == capturer) {
2872 SendFrame(send_channel, frame, capturer->IsScreencast());
2873 capturer_is_channel_owned = true;
2874 }
2875 }
2876 if (capturer_is_channel_owned) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002877 return;
2878 }
wu@webrtc.org24301a62013-12-13 19:17:43 +00002879
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002880 // TODO(hellner): Remove below for loop once the captured frame no longer
2881 // come from the engine, i.e. the engine no longer owns a capturer.
2882 for (SendChannelMap::iterator iter = send_channels_.begin();
2883 iter != send_channels_.end(); ++iter) {
2884 WebRtcVideoChannelSendInfo* send_channel = iter->second;
2885 if (send_channel->video_capturer() == NULL) {
2886 SendFrame(send_channel, frame, capturer->IsScreencast());
2887 }
2888 }
2889}
2890
2891bool WebRtcVideoMediaChannel::SendFrame(
2892 WebRtcVideoChannelSendInfo* send_channel,
2893 const VideoFrame* frame,
2894 bool is_screencast) {
2895 if (!send_channel) {
2896 return false;
2897 }
2898 if (!send_codec_) {
2899 // Send codec has not been set. No reason to process the frame any further.
2900 return false;
2901 }
2902 const VideoFormat& video_format = send_channel->video_format();
2903 // If the frame should be dropped.
2904 const bool video_format_set = video_format != cricket::VideoFormat();
2905 if (video_format_set &&
2906 (video_format.width == 0 && video_format.height == 0)) {
2907 return true;
2908 }
2909
2910 // Checks if we need to reset vie send codec.
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00002911 if (!MaybeResetVieSendCodec(send_channel,
2912 static_cast<int>(frame->GetWidth()),
2913 static_cast<int>(frame->GetHeight()),
2914 is_screencast, NULL)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002915 LOG(LS_ERROR) << "MaybeResetVieSendCodec failed with "
2916 << frame->GetWidth() << "x" << frame->GetHeight();
2917 return false;
2918 }
2919 const VideoFrame* frame_out = frame;
2920 talk_base::scoped_ptr<VideoFrame> processed_frame;
2921 // Disable muting for screencast.
2922 const bool mute = (send_channel->muted() && !is_screencast);
2923 send_channel->ProcessFrame(*frame_out, mute, processed_frame.use());
2924 if (processed_frame) {
2925 frame_out = processed_frame.get();
2926 }
2927
2928 webrtc::ViEVideoFrameI420 frame_i420;
2929 // TODO(ronghuawu): Update the webrtc::ViEVideoFrameI420
2930 // to use const unsigned char*
2931 frame_i420.y_plane = const_cast<unsigned char*>(frame_out->GetYPlane());
2932 frame_i420.u_plane = const_cast<unsigned char*>(frame_out->GetUPlane());
2933 frame_i420.v_plane = const_cast<unsigned char*>(frame_out->GetVPlane());
2934 frame_i420.y_pitch = frame_out->GetYPitch();
2935 frame_i420.u_pitch = frame_out->GetUPitch();
2936 frame_i420.v_pitch = frame_out->GetVPitch();
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00002937 frame_i420.width = static_cast<uint16>(frame_out->GetWidth());
2938 frame_i420.height = static_cast<uint16>(frame_out->GetHeight());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002939
2940 int64 timestamp_ntp_ms = 0;
2941 // TODO(justinlin): Reenable after Windows issues with clock drift are fixed.
2942 // Currently reverted to old behavior of discarding capture timestamp.
2943#if 0
2944 // If the frame timestamp is 0, we will use the deliver time.
2945 const int64 frame_timestamp = frame->GetTimeStamp();
2946 if (frame_timestamp != 0) {
2947 if (abs(time(NULL) - frame_timestamp / talk_base::kNumNanosecsPerSec) >
2948 kTimestampDeltaInSecondsForWarning) {
2949 LOG(LS_WARNING) << "Frame timestamp differs by more than "
2950 << kTimestampDeltaInSecondsForWarning << " seconds from "
2951 << "current Unix timestamp.";
2952 }
2953
2954 timestamp_ntp_ms =
2955 talk_base::UnixTimestampNanosecsToNtpMillisecs(frame_timestamp);
2956 }
2957#endif
2958
2959 return send_channel->external_capture()->IncomingFrameI420(
2960 frame_i420, timestamp_ntp_ms) == 0;
2961}
2962
2963bool WebRtcVideoMediaChannel::CreateChannel(uint32 ssrc_key,
2964 MediaDirection direction,
2965 int* channel_id) {
2966 // There are 3 types of channels. Sending only, receiving only and
2967 // sending and receiving. The sending and receiving channel is the
2968 // default channel and there is only one. All other channels that are created
2969 // are associated with the default channel which must exist. The default
2970 // channel id is stored in |vie_channel_|. All channels need to know about
2971 // the default channel to properly handle remb which is why there are
2972 // different ViE create channel calls.
2973 // For this channel the local and remote ssrc key is 0. However, it may
2974 // have a non-zero local and/or remote ssrc depending on if it is currently
2975 // sending and/or receiving.
2976 if ((vie_channel_ == -1 || direction == MD_SENDRECV) &&
2977 (!send_channels_.empty() || !recv_channels_.empty())) {
2978 ASSERT(false);
2979 return false;
2980 }
2981
2982 *channel_id = -1;
2983 if (direction == MD_RECV) {
2984 // All rec channels are associated with the default channel |vie_channel_|
2985 if (engine_->vie()->base()->CreateReceiveChannel(*channel_id,
2986 vie_channel_) != 0) {
2987 LOG_RTCERR2(CreateReceiveChannel, *channel_id, vie_channel_);
2988 return false;
2989 }
2990 } else if (direction == MD_SEND) {
2991 if (engine_->vie()->base()->CreateChannel(*channel_id,
2992 vie_channel_) != 0) {
2993 LOG_RTCERR2(CreateChannel, *channel_id, vie_channel_);
2994 return false;
2995 }
2996 } else {
2997 ASSERT(direction == MD_SENDRECV);
2998 if (engine_->vie()->base()->CreateChannel(*channel_id) != 0) {
2999 LOG_RTCERR1(CreateChannel, *channel_id);
3000 return false;
3001 }
3002 }
3003 if (!ConfigureChannel(*channel_id, direction, ssrc_key)) {
3004 engine_->vie()->base()->DeleteChannel(*channel_id);
3005 *channel_id = -1;
3006 return false;
3007 }
3008
3009 return true;
3010}
3011
3012bool WebRtcVideoMediaChannel::ConfigureChannel(int channel_id,
3013 MediaDirection direction,
3014 uint32 ssrc_key) {
3015 const bool receiving = (direction == MD_RECV) || (direction == MD_SENDRECV);
3016 const bool sending = (direction == MD_SEND) || (direction == MD_SENDRECV);
3017 // Register external transport.
3018 if (engine_->vie()->network()->RegisterSendTransport(
3019 channel_id, *this) != 0) {
3020 LOG_RTCERR1(RegisterSendTransport, channel_id);
3021 return false;
3022 }
3023
3024 // Set MTU.
3025 if (engine_->vie()->network()->SetMTU(channel_id, kVideoMtu) != 0) {
3026 LOG_RTCERR2(SetMTU, channel_id, kVideoMtu);
3027 return false;
3028 }
3029 // Turn on RTCP and loss feedback reporting.
3030 if (engine()->vie()->rtp()->SetRTCPStatus(
3031 channel_id, webrtc::kRtcpCompound_RFC4585) != 0) {
3032 LOG_RTCERR2(SetRTCPStatus, channel_id, webrtc::kRtcpCompound_RFC4585);
3033 return false;
3034 }
3035 // Enable pli as key frame request method.
3036 if (engine_->vie()->rtp()->SetKeyFrameRequestMethod(
3037 channel_id, webrtc::kViEKeyFrameRequestPliRtcp) != 0) {
3038 LOG_RTCERR2(SetKeyFrameRequestMethod,
3039 channel_id, webrtc::kViEKeyFrameRequestPliRtcp);
3040 return false;
3041 }
3042 if (!SetNackFec(channel_id, send_red_type_, send_fec_type_, nack_enabled_)) {
3043 // Logged in SetNackFec. Don't spam the logs.
3044 return false;
3045 }
3046 // Note that receiving must always be configured before sending to ensure
3047 // that send and receive channel is configured correctly (ConfigureReceiving
3048 // assumes no sending).
3049 if (receiving) {
3050 if (!ConfigureReceiving(channel_id, ssrc_key)) {
3051 return false;
3052 }
3053 }
3054 if (sending) {
3055 if (!ConfigureSending(channel_id, ssrc_key)) {
3056 return false;
3057 }
3058 }
3059
3060 return true;
3061}
3062
3063bool WebRtcVideoMediaChannel::ConfigureReceiving(int channel_id,
3064 uint32 remote_ssrc_key) {
3065 // Make sure that an SSRC/key isn't registered more than once.
3066 if (recv_channels_.find(remote_ssrc_key) != recv_channels_.end()) {
3067 return false;
3068 }
3069 // Connect the voice channel, if there is one.
3070 // TODO(perkj): The A/V is synched by the receiving channel. So we need to
3071 // know the SSRC of the remote audio channel in order to fetch the correct
3072 // webrtc VoiceEngine channel. For now- only sync the default channel used
3073 // in 1-1 calls.
3074 if (remote_ssrc_key == 0 && voice_channel_) {
3075 WebRtcVoiceMediaChannel* voice_channel =
3076 static_cast<WebRtcVoiceMediaChannel*>(voice_channel_);
3077 if (engine_->vie()->base()->ConnectAudioChannel(
3078 vie_channel_, voice_channel->voe_channel()) != 0) {
3079 LOG_RTCERR2(ConnectAudioChannel, channel_id,
3080 voice_channel->voe_channel());
3081 LOG(LS_WARNING) << "A/V not synchronized";
3082 // Not a fatal error.
3083 }
3084 }
3085
3086 talk_base::scoped_ptr<WebRtcVideoChannelRecvInfo> channel_info(
3087 new WebRtcVideoChannelRecvInfo(channel_id));
3088
3089 // Install a render adapter.
3090 if (engine_->vie()->render()->AddRenderer(channel_id,
3091 webrtc::kVideoI420, channel_info->render_adapter()) != 0) {
3092 LOG_RTCERR3(AddRenderer, channel_id, webrtc::kVideoI420,
3093 channel_info->render_adapter());
3094 return false;
3095 }
3096
3097
3098 if (engine_->vie()->rtp()->SetRembStatus(channel_id,
3099 kNotSending,
3100 remb_enabled_) != 0) {
3101 LOG_RTCERR3(SetRembStatus, channel_id, kNotSending, remb_enabled_);
3102 return false;
3103 }
3104
3105 if (!SetHeaderExtension(&webrtc::ViERTP_RTCP::SetReceiveTimestampOffsetStatus,
3106 channel_id, receive_extensions_, kRtpTimestampOffsetHeaderExtension)) {
3107 return false;
3108 }
3109
3110 if (!SetHeaderExtension(
3111 &webrtc::ViERTP_RTCP::SetReceiveAbsoluteSendTimeStatus, channel_id,
3112 receive_extensions_, kRtpAbsoluteSendTimeHeaderExtension)) {
3113 return false;
3114 }
3115
3116 if (remote_ssrc_key != 0) {
3117 // Use the same SSRC as our default channel
3118 // (so the RTCP reports are correct).
3119 unsigned int send_ssrc = 0;
3120 webrtc::ViERTP_RTCP* rtp = engine()->vie()->rtp();
3121 if (rtp->GetLocalSSRC(vie_channel_, send_ssrc) == -1) {
3122 LOG_RTCERR2(GetLocalSSRC, vie_channel_, send_ssrc);
3123 return false;
3124 }
3125 if (rtp->SetLocalSSRC(channel_id, send_ssrc) == -1) {
3126 LOG_RTCERR2(SetLocalSSRC, channel_id, send_ssrc);
3127 return false;
3128 }
3129 } // Else this is the the default channel and we don't change the SSRC.
3130
3131 // Disable color enhancement since it is a bit too aggressive.
3132 if (engine()->vie()->image()->EnableColorEnhancement(channel_id,
3133 false) != 0) {
3134 LOG_RTCERR1(EnableColorEnhancement, channel_id);
3135 return false;
3136 }
3137
3138 if (!SetReceiveCodecs(channel_info.get())) {
3139 return false;
3140 }
3141
3142 int buffer_latency =
3143 options_.buffered_mode_latency.GetWithDefaultIfUnset(
3144 cricket::kBufferedModeDisabled);
3145 if (buffer_latency != cricket::kBufferedModeDisabled) {
3146 if (engine()->vie()->rtp()->SetReceiverBufferingMode(
3147 channel_id, buffer_latency) != 0) {
3148 LOG_RTCERR2(SetReceiverBufferingMode, channel_id, buffer_latency);
3149 }
3150 }
3151
3152 if (render_started_) {
3153 if (engine_->vie()->render()->StartRender(channel_id) != 0) {
3154 LOG_RTCERR1(StartRender, channel_id);
3155 return false;
3156 }
3157 }
3158
3159 // Register decoder observer for incoming framerate and bitrate.
3160 if (engine()->vie()->codec()->RegisterDecoderObserver(
3161 channel_id, *channel_info->decoder_observer()) != 0) {
3162 LOG_RTCERR1(RegisterDecoderObserver, channel_info->decoder_observer());
3163 return false;
3164 }
3165
3166 recv_channels_[remote_ssrc_key] = channel_info.release();
3167 return true;
3168}
3169
3170bool WebRtcVideoMediaChannel::ConfigureSending(int channel_id,
3171 uint32 local_ssrc_key) {
3172 // The ssrc key can be zero or correspond to an SSRC.
3173 // Make sure the default channel isn't configured more than once.
3174 if (local_ssrc_key == 0 && send_channels_.find(0) != send_channels_.end()) {
3175 return false;
3176 }
3177 // Make sure that the SSRC is not already in use.
3178 uint32 dummy_key;
3179 if (GetSendChannelKey(local_ssrc_key, &dummy_key)) {
3180 return false;
3181 }
3182 int vie_capture = 0;
3183 webrtc::ViEExternalCapture* external_capture = NULL;
3184 // Register external capture.
3185 if (engine()->vie()->capture()->AllocateExternalCaptureDevice(
3186 vie_capture, external_capture) != 0) {
3187 LOG_RTCERR0(AllocateExternalCaptureDevice);
3188 return false;
3189 }
3190
3191 // Connect external capture.
3192 if (engine()->vie()->capture()->ConnectCaptureDevice(
3193 vie_capture, channel_id) != 0) {
3194 LOG_RTCERR2(ConnectCaptureDevice, vie_capture, channel_id);
3195 return false;
3196 }
3197 talk_base::scoped_ptr<WebRtcVideoChannelSendInfo> send_channel(
3198 new WebRtcVideoChannelSendInfo(channel_id, vie_capture,
3199 external_capture,
3200 engine()->cpu_monitor()));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00003201 if (engine()->vie()->base()->RegisterCpuOveruseObserver(
3202 channel_id, send_channel->overuse_observer())) {
3203 LOG_RTCERR1(RegisterCpuOveruseObserver, channel_id);
3204 return false;
3205 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003206 send_channel->ApplyCpuOptions(options_);
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00003207 send_channel->SignalCpuAdaptationUnable.connect(this,
3208 &WebRtcVideoMediaChannel::OnCpuAdaptationUnable);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003209
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00003210 if (options_.cpu_overuse_detection.GetWithDefaultIfUnset(false)) {
3211 send_channel->SetCpuOveruseDetection(true);
3212 }
3213
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003214 // Register encoder observer for outgoing framerate and bitrate.
3215 if (engine()->vie()->codec()->RegisterEncoderObserver(
3216 channel_id, *send_channel->encoder_observer()) != 0) {
3217 LOG_RTCERR1(RegisterEncoderObserver, send_channel->encoder_observer());
3218 return false;
3219 }
3220
3221 if (!SetHeaderExtension(&webrtc::ViERTP_RTCP::SetSendTimestampOffsetStatus,
3222 channel_id, send_extensions_, kRtpTimestampOffsetHeaderExtension)) {
3223 return false;
3224 }
3225
3226 if (!SetHeaderExtension(&webrtc::ViERTP_RTCP::SetSendAbsoluteSendTimeStatus,
3227 channel_id, send_extensions_, kRtpAbsoluteSendTimeHeaderExtension)) {
3228 return false;
3229 }
3230
3231 if (options_.video_leaky_bucket.GetWithDefaultIfUnset(false)) {
3232 if (engine()->vie()->rtp()->SetTransmissionSmoothingStatus(channel_id,
3233 true) != 0) {
3234 LOG_RTCERR2(SetTransmissionSmoothingStatus, channel_id, true);
3235 return false;
3236 }
3237 }
3238
3239 int buffer_latency =
3240 options_.buffered_mode_latency.GetWithDefaultIfUnset(
3241 cricket::kBufferedModeDisabled);
3242 if (buffer_latency != cricket::kBufferedModeDisabled) {
3243 if (engine()->vie()->rtp()->SetSenderBufferingMode(
3244 channel_id, buffer_latency) != 0) {
3245 LOG_RTCERR2(SetSenderBufferingMode, channel_id, buffer_latency);
3246 }
3247 }
3248 // The remb status direction correspond to the RTP stream (and not the RTCP
3249 // stream). I.e. if send remb is enabled it means it is receiving remote
3250 // rembs and should use them to estimate bandwidth. Receive remb mean that
3251 // remb packets will be generated and that the channel should be included in
3252 // it. If remb is enabled all channels are allowed to contribute to the remb
3253 // but only receive channels will ever end up actually contributing. This
3254 // keeps the logic simple.
3255 if (engine_->vie()->rtp()->SetRembStatus(channel_id,
3256 remb_enabled_,
3257 remb_enabled_) != 0) {
3258 LOG_RTCERR3(SetRembStatus, channel_id, remb_enabled_, remb_enabled_);
3259 return false;
3260 }
3261 if (!SetNackFec(channel_id, send_red_type_, send_fec_type_, nack_enabled_)) {
3262 // Logged in SetNackFec. Don't spam the logs.
3263 return false;
3264 }
3265
3266 send_channels_[local_ssrc_key] = send_channel.release();
3267
3268 return true;
3269}
3270
3271bool WebRtcVideoMediaChannel::SetNackFec(int channel_id,
3272 int red_payload_type,
3273 int fec_payload_type,
3274 bool nack_enabled) {
3275 bool enable = (red_payload_type != -1 && fec_payload_type != -1 &&
3276 !InConferenceMode());
3277 if (enable) {
3278 if (engine_->vie()->rtp()->SetHybridNACKFECStatus(
3279 channel_id, nack_enabled, red_payload_type, fec_payload_type) != 0) {
3280 LOG_RTCERR4(SetHybridNACKFECStatus,
3281 channel_id, nack_enabled, red_payload_type, fec_payload_type);
3282 return false;
3283 }
3284 LOG(LS_INFO) << "Hybrid NACK/FEC enabled for channel " << channel_id;
3285 } else {
3286 if (engine_->vie()->rtp()->SetNACKStatus(channel_id, nack_enabled) != 0) {
3287 LOG_RTCERR1(SetNACKStatus, channel_id);
3288 return false;
3289 }
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00003290 std::string enabled = nack_enabled ? "enabled" : "disabled";
3291 LOG(LS_INFO) << "NACK " << enabled << " for channel " << channel_id;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003292 }
3293 return true;
3294}
3295
3296bool WebRtcVideoMediaChannel::SetSendCodec(const webrtc::VideoCodec& codec,
3297 int min_bitrate,
3298 int start_bitrate,
3299 int max_bitrate) {
3300 bool ret_val = true;
3301 for (SendChannelMap::iterator iter = send_channels_.begin();
3302 iter != send_channels_.end(); ++iter) {
3303 WebRtcVideoChannelSendInfo* send_channel = iter->second;
3304 ret_val = SetSendCodec(send_channel, codec, min_bitrate, start_bitrate,
3305 max_bitrate) && ret_val;
3306 }
3307 if (ret_val) {
3308 // All SetSendCodec calls were successful. Update the global state
3309 // accordingly.
3310 send_codec_.reset(new webrtc::VideoCodec(codec));
3311 send_min_bitrate_ = min_bitrate;
3312 send_start_bitrate_ = start_bitrate;
3313 send_max_bitrate_ = max_bitrate;
3314 } else {
3315 // At least one SetSendCodec call failed, rollback.
3316 for (SendChannelMap::iterator iter = send_channels_.begin();
3317 iter != send_channels_.end(); ++iter) {
3318 WebRtcVideoChannelSendInfo* send_channel = iter->second;
3319 if (send_codec_) {
3320 SetSendCodec(send_channel, *send_codec_.get(), send_min_bitrate_,
3321 send_start_bitrate_, send_max_bitrate_);
3322 }
3323 }
3324 }
3325 return ret_val;
3326}
3327
3328bool WebRtcVideoMediaChannel::SetSendCodec(
3329 WebRtcVideoChannelSendInfo* send_channel,
3330 const webrtc::VideoCodec& codec,
3331 int min_bitrate,
3332 int start_bitrate,
3333 int max_bitrate) {
3334 if (!send_channel) {
3335 return false;
3336 }
3337 const int channel_id = send_channel->channel_id();
3338 // Make a copy of the codec
3339 webrtc::VideoCodec target_codec = codec;
3340 target_codec.startBitrate = start_bitrate;
3341 target_codec.minBitrate = min_bitrate;
3342 target_codec.maxBitrate = max_bitrate;
3343
3344 // Set the default number of temporal layers for VP8.
3345 if (webrtc::kVideoCodecVP8 == codec.codecType) {
3346 target_codec.codecSpecific.VP8.numberOfTemporalLayers =
3347 kDefaultNumberOfTemporalLayers;
3348
3349 // Turn off the VP8 error resilience
3350 target_codec.codecSpecific.VP8.resilience = webrtc::kResilienceOff;
3351
3352 bool enable_denoising =
3353 options_.video_noise_reduction.GetWithDefaultIfUnset(false);
3354 target_codec.codecSpecific.VP8.denoisingOn = enable_denoising;
3355 }
3356
3357 // Register external encoder if codec type is supported by encoder factory.
3358 if (engine()->IsExternalEncoderCodecType(codec.codecType) &&
3359 !send_channel->IsEncoderRegistered(target_codec.plType)) {
3360 webrtc::VideoEncoder* encoder =
3361 engine()->CreateExternalEncoder(codec.codecType);
3362 if (encoder) {
3363 if (engine()->vie()->ext_codec()->RegisterExternalSendCodec(
3364 channel_id, target_codec.plType, encoder, false) == 0) {
3365 send_channel->RegisterEncoder(target_codec.plType, encoder);
3366 } else {
3367 LOG_RTCERR2(RegisterExternalSendCodec, channel_id, target_codec.plName);
3368 engine()->DestroyExternalEncoder(encoder);
3369 }
3370 }
3371 }
3372
3373 // Resolution and framerate may vary for different send channels.
3374 const VideoFormat& video_format = send_channel->video_format();
3375 UpdateVideoCodec(video_format, &target_codec);
3376
3377 if (target_codec.width == 0 && target_codec.height == 0) {
3378 const uint32 ssrc = send_channel->stream_params()->first_ssrc();
3379 LOG(LS_INFO) << "0x0 resolution selected. Captured frames will be dropped "
3380 << "for ssrc: " << ssrc << ".";
3381 } else {
3382 MaybeChangeStartBitrate(channel_id, &target_codec);
3383 if (0 != engine()->vie()->codec()->SetSendCodec(channel_id, target_codec)) {
3384 LOG_RTCERR2(SetSendCodec, channel_id, target_codec.plName);
3385 return false;
3386 }
3387
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00003388 // NOTE: SetRtxSendPayloadType must be called after all simulcast SSRCs
3389 // are configured. Otherwise ssrc's configured after this point will use
3390 // the primary PT for RTX.
3391 if (send_rtx_type_ != -1 &&
3392 engine()->vie()->rtp()->SetRtxSendPayloadType(channel_id,
3393 send_rtx_type_) != 0) {
3394 LOG_RTCERR2(SetRtxSendPayloadType, channel_id, send_rtx_type_);
3395 return false;
3396 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003397 }
3398 send_channel->set_interval(
3399 cricket::VideoFormat::FpsToInterval(target_codec.maxFramerate));
3400 return true;
3401}
3402
3403
3404static std::string ToString(webrtc::VideoCodecComplexity complexity) {
3405 switch (complexity) {
3406 case webrtc::kComplexityNormal:
3407 return "normal";
3408 case webrtc::kComplexityHigh:
3409 return "high";
3410 case webrtc::kComplexityHigher:
3411 return "higher";
3412 case webrtc::kComplexityMax:
3413 return "max";
3414 default:
3415 return "unknown";
3416 }
3417}
3418
3419static std::string ToString(webrtc::VP8ResilienceMode resilience) {
3420 switch (resilience) {
3421 case webrtc::kResilienceOff:
3422 return "off";
3423 case webrtc::kResilientStream:
3424 return "stream";
3425 case webrtc::kResilientFrames:
3426 return "frames";
3427 default:
3428 return "unknown";
3429 }
3430}
3431
3432void WebRtcVideoMediaChannel::LogSendCodecChange(const std::string& reason) {
3433 webrtc::VideoCodec vie_codec;
3434 if (engine()->vie()->codec()->GetSendCodec(vie_channel_, vie_codec) != 0) {
3435 LOG_RTCERR1(GetSendCodec, vie_channel_);
3436 return;
3437 }
3438
3439 LOG(LS_INFO) << reason << " : selected video codec "
3440 << vie_codec.plName << "/"
3441 << vie_codec.width << "x" << vie_codec.height << "x"
3442 << static_cast<int>(vie_codec.maxFramerate) << "fps"
3443 << "@" << vie_codec.maxBitrate << "kbps"
3444 << " (min=" << vie_codec.minBitrate << "kbps,"
3445 << " start=" << vie_codec.startBitrate << "kbps)";
3446 LOG(LS_INFO) << "Video max quantization: " << vie_codec.qpMax;
3447 if (webrtc::kVideoCodecVP8 == vie_codec.codecType) {
3448 LOG(LS_INFO) << "VP8 number of temporal layers: "
3449 << static_cast<int>(
3450 vie_codec.codecSpecific.VP8.numberOfTemporalLayers);
3451 LOG(LS_INFO) << "VP8 options : "
3452 << "picture loss indication = "
3453 << vie_codec.codecSpecific.VP8.pictureLossIndicationOn
3454 << ", feedback mode = "
3455 << vie_codec.codecSpecific.VP8.feedbackModeOn
3456 << ", complexity = "
3457 << ToString(vie_codec.codecSpecific.VP8.complexity)
3458 << ", resilience = "
3459 << ToString(vie_codec.codecSpecific.VP8.resilience)
3460 << ", denoising = "
3461 << vie_codec.codecSpecific.VP8.denoisingOn
3462 << ", error concealment = "
3463 << vie_codec.codecSpecific.VP8.errorConcealmentOn
3464 << ", automatic resize = "
3465 << vie_codec.codecSpecific.VP8.automaticResizeOn
3466 << ", frame dropping = "
3467 << vie_codec.codecSpecific.VP8.frameDroppingOn
3468 << ", key frame interval = "
3469 << vie_codec.codecSpecific.VP8.keyFrameInterval;
3470 }
3471
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00003472 if (send_rtx_type_ != -1) {
3473 LOG(LS_INFO) << "RTX payload type: " << send_rtx_type_;
3474 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003475}
3476
3477bool WebRtcVideoMediaChannel::SetReceiveCodecs(
3478 WebRtcVideoChannelRecvInfo* info) {
3479 int red_type = -1;
3480 int fec_type = -1;
3481 int channel_id = info->channel_id();
3482 for (std::vector<webrtc::VideoCodec>::iterator it = receive_codecs_.begin();
3483 it != receive_codecs_.end(); ++it) {
3484 if (it->codecType == webrtc::kVideoCodecRED) {
3485 red_type = it->plType;
3486 } else if (it->codecType == webrtc::kVideoCodecULPFEC) {
3487 fec_type = it->plType;
3488 }
3489 if (engine()->vie()->codec()->SetReceiveCodec(channel_id, *it) != 0) {
3490 LOG_RTCERR2(SetReceiveCodec, channel_id, it->plName);
3491 return false;
3492 }
3493 if (!info->IsDecoderRegistered(it->plType) &&
3494 it->codecType != webrtc::kVideoCodecRED &&
3495 it->codecType != webrtc::kVideoCodecULPFEC) {
3496 webrtc::VideoDecoder* decoder =
3497 engine()->CreateExternalDecoder(it->codecType);
3498 if (decoder) {
3499 if (engine()->vie()->ext_codec()->RegisterExternalReceiveCodec(
3500 channel_id, it->plType, decoder) == 0) {
3501 info->RegisterDecoder(it->plType, decoder);
3502 } else {
3503 LOG_RTCERR2(RegisterExternalReceiveCodec, channel_id, it->plName);
3504 engine()->DestroyExternalDecoder(decoder);
3505 }
3506 }
3507 }
3508 }
3509
3510 // Start receiving packets if at least one receive codec has been set.
3511 if (!receive_codecs_.empty()) {
3512 if (engine()->vie()->base()->StartReceive(channel_id) != 0) {
3513 LOG_RTCERR1(StartReceive, channel_id);
3514 return false;
3515 }
3516 }
3517 return true;
3518}
3519
3520int WebRtcVideoMediaChannel::GetRecvChannelNum(uint32 ssrc) {
3521 if (ssrc == first_receive_ssrc_) {
3522 return vie_channel_;
3523 }
3524 RecvChannelMap::iterator it = recv_channels_.find(ssrc);
3525 return (it != recv_channels_.end()) ? it->second->channel_id() : -1;
3526}
3527
3528// If the new frame size is different from the send codec size we set on vie,
3529// we need to reset the send codec on vie.
3530// The new send codec size should not exceed send_codec_ which is controlled
3531// only by the 'jec' logic.
3532bool WebRtcVideoMediaChannel::MaybeResetVieSendCodec(
3533 WebRtcVideoChannelSendInfo* send_channel,
3534 int new_width,
3535 int new_height,
3536 bool is_screencast,
3537 bool* reset) {
3538 if (reset) {
3539 *reset = false;
3540 }
3541 ASSERT(send_codec_.get() != NULL);
3542
3543 webrtc::VideoCodec target_codec = *send_codec_.get();
3544 const VideoFormat& video_format = send_channel->video_format();
3545 UpdateVideoCodec(video_format, &target_codec);
3546
3547 // Vie send codec size should not exceed target_codec.
3548 int target_width = new_width;
3549 int target_height = new_height;
3550 if (!is_screencast &&
3551 (new_width > target_codec.width || new_height > target_codec.height)) {
3552 target_width = target_codec.width;
3553 target_height = target_codec.height;
3554 }
3555
3556 // Get current vie codec.
3557 webrtc::VideoCodec vie_codec;
3558 const int channel_id = send_channel->channel_id();
3559 if (engine()->vie()->codec()->GetSendCodec(channel_id, vie_codec) != 0) {
3560 LOG_RTCERR1(GetSendCodec, channel_id);
3561 return false;
3562 }
3563 const int cur_width = vie_codec.width;
3564 const int cur_height = vie_codec.height;
3565
3566 // Only reset send codec when there is a size change. Additionally,
3567 // automatic resize needs to be turned off when screencasting and on when
3568 // not screencasting.
3569 // Don't allow automatic resizing for screencasting.
3570 bool automatic_resize = !is_screencast;
3571 // Turn off VP8 frame dropping when screensharing as the current model does
3572 // not work well at low fps.
3573 bool vp8_frame_dropping = !is_screencast;
3574 // Disable denoising for screencasting.
3575 bool enable_denoising =
3576 options_.video_noise_reduction.GetWithDefaultIfUnset(false);
3577 bool denoising = !is_screencast && enable_denoising;
3578 bool reset_send_codec =
3579 target_width != cur_width || target_height != cur_height ||
3580 automatic_resize != vie_codec.codecSpecific.VP8.automaticResizeOn ||
3581 denoising != vie_codec.codecSpecific.VP8.denoisingOn ||
3582 vp8_frame_dropping != vie_codec.codecSpecific.VP8.frameDroppingOn;
3583
3584 if (reset_send_codec) {
3585 // Set the new codec on vie.
3586 vie_codec.width = target_width;
3587 vie_codec.height = target_height;
3588 vie_codec.maxFramerate = target_codec.maxFramerate;
3589 vie_codec.startBitrate = target_codec.startBitrate;
3590 vie_codec.codecSpecific.VP8.automaticResizeOn = automatic_resize;
3591 vie_codec.codecSpecific.VP8.denoisingOn = denoising;
3592 vie_codec.codecSpecific.VP8.frameDroppingOn = vp8_frame_dropping;
3593 // TODO(mflodman): Remove 'is_screencast' check when screen cast settings
3594 // are treated correctly in WebRTC.
3595 if (!is_screencast)
3596 MaybeChangeStartBitrate(channel_id, &vie_codec);
3597
3598 if (engine()->vie()->codec()->SetSendCodec(channel_id, vie_codec) != 0) {
3599 LOG_RTCERR1(SetSendCodec, channel_id);
3600 return false;
3601 }
3602 if (reset) {
3603 *reset = true;
3604 }
3605 LogSendCodecChange("Capture size changed");
3606 }
3607
3608 return true;
3609}
3610
3611void WebRtcVideoMediaChannel::MaybeChangeStartBitrate(
3612 int channel_id, webrtc::VideoCodec* video_codec) {
3613 if (video_codec->startBitrate < video_codec->minBitrate) {
3614 video_codec->startBitrate = video_codec->minBitrate;
3615 } else if (video_codec->startBitrate > video_codec->maxBitrate) {
3616 video_codec->startBitrate = video_codec->maxBitrate;
3617 }
3618
3619 // Use a previous target bitrate, if there is one.
3620 unsigned int current_target_bitrate = 0;
3621 if (engine()->vie()->codec()->GetCodecTargetBitrate(
3622 channel_id, &current_target_bitrate) == 0) {
3623 // Convert to kbps.
3624 current_target_bitrate /= 1000;
3625 if (current_target_bitrate > video_codec->maxBitrate) {
3626 current_target_bitrate = video_codec->maxBitrate;
3627 }
3628 if (current_target_bitrate > video_codec->startBitrate) {
3629 video_codec->startBitrate = current_target_bitrate;
3630 }
3631 }
3632}
3633
3634void WebRtcVideoMediaChannel::OnMessage(talk_base::Message* msg) {
3635 FlushBlackFrameData* black_frame_data =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00003636 static_cast<FlushBlackFrameData*>(msg->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003637 FlushBlackFrame(black_frame_data->ssrc, black_frame_data->timestamp);
3638 delete black_frame_data;
3639}
3640
3641int WebRtcVideoMediaChannel::SendPacket(int channel, const void* data,
3642 int len) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003643 talk_base::Buffer packet(data, len, kMaxRtpPacketLen);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00003644 return MediaChannel::SendPacket(&packet) ? len : -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003645}
3646
3647int WebRtcVideoMediaChannel::SendRTCPPacket(int channel,
3648 const void* data,
3649 int len) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003650 talk_base::Buffer packet(data, len, kMaxRtpPacketLen);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00003651 return MediaChannel::SendRtcp(&packet) ? len : -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003652}
3653
3654void WebRtcVideoMediaChannel::QueueBlackFrame(uint32 ssrc, int64 timestamp,
3655 int framerate) {
3656 if (timestamp) {
3657 FlushBlackFrameData* black_frame_data = new FlushBlackFrameData(
3658 ssrc,
3659 timestamp);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00003660 const int delay_ms = static_cast<int>(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003661 2 * cricket::VideoFormat::FpsToInterval(framerate) *
3662 talk_base::kNumMillisecsPerSec / talk_base::kNumNanosecsPerSec);
3663 worker_thread()->PostDelayed(delay_ms, this, 0, black_frame_data);
3664 }
3665}
3666
3667void WebRtcVideoMediaChannel::FlushBlackFrame(uint32 ssrc, int64 timestamp) {
3668 WebRtcVideoChannelSendInfo* send_channel = GetSendChannel(ssrc);
3669 if (!send_channel) {
3670 return;
3671 }
3672 talk_base::scoped_ptr<const VideoFrame> black_frame_ptr;
3673
3674 const WebRtcLocalStreamInfo* channel_stream_info =
3675 send_channel->local_stream_info();
3676 int64 last_frame_time_stamp = channel_stream_info->time_stamp();
3677 if (last_frame_time_stamp == timestamp) {
3678 size_t last_frame_width = 0;
3679 size_t last_frame_height = 0;
3680 int64 last_frame_elapsed_time = 0;
3681 channel_stream_info->GetLastFrameInfo(&last_frame_width, &last_frame_height,
3682 &last_frame_elapsed_time);
3683 if (!last_frame_width || !last_frame_height) {
3684 return;
3685 }
3686 WebRtcVideoFrame black_frame;
3687 // Black frame is not screencast.
3688 const bool screencasting = false;
3689 const int64 timestamp_delta = send_channel->interval();
3690 if (!black_frame.InitToBlack(send_codec_->width, send_codec_->height, 1, 1,
3691 last_frame_elapsed_time + timestamp_delta,
3692 last_frame_time_stamp + timestamp_delta) ||
3693 !SendFrame(send_channel, &black_frame, screencasting)) {
3694 LOG(LS_ERROR) << "Failed to send black frame.";
3695 }
3696 }
3697}
3698
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00003699void WebRtcVideoMediaChannel::OnCpuAdaptationUnable() {
3700 // ssrc is hardcoded to 0. This message is based on a system wide issue,
3701 // so finding which ssrc caused it doesn't matter.
3702 SignalMediaError(0, VideoMediaChannel::ERROR_REC_CPU_MAX_CANT_DOWNGRADE);
3703}
3704
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003705void WebRtcVideoMediaChannel::SetNetworkTransmissionState(
3706 bool is_transmitting) {
3707 LOG(LS_INFO) << "SetNetworkTransmissionState: " << is_transmitting;
3708 for (SendChannelMap::iterator iter = send_channels_.begin();
3709 iter != send_channels_.end(); ++iter) {
3710 WebRtcVideoChannelSendInfo* send_channel = iter->second;
3711 int channel_id = send_channel->channel_id();
3712 engine_->vie()->network()->SetNetworkTransmissionState(channel_id,
3713 is_transmitting);
3714 }
3715}
3716
3717bool WebRtcVideoMediaChannel::SetHeaderExtension(ExtensionSetterFunction setter,
3718 int channel_id, const RtpHeaderExtension* extension) {
3719 bool enable = false;
3720 int id = 0;
3721 if (extension) {
3722 enable = true;
3723 id = extension->id;
3724 }
3725 if ((engine_->vie()->rtp()->*setter)(channel_id, enable, id) != 0) {
3726 LOG_RTCERR4(*setter, extension->uri, channel_id, enable, id);
3727 return false;
3728 }
3729 return true;
3730}
3731
3732bool WebRtcVideoMediaChannel::SetHeaderExtension(ExtensionSetterFunction setter,
3733 int channel_id, const std::vector<RtpHeaderExtension>& extensions,
3734 const char header_extension_uri[]) {
3735 const RtpHeaderExtension* extension = FindHeaderExtension(extensions,
3736 header_extension_uri);
3737 return SetHeaderExtension(setter, channel_id, extension);
3738}
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00003739
3740bool WebRtcVideoMediaChannel::SetLocalRtxSsrc(int channel_id,
3741 const StreamParams& send_params,
3742 uint32 primary_ssrc,
3743 int stream_idx) {
3744 uint32 rtx_ssrc = 0;
3745 bool has_rtx = send_params.GetFidSsrc(primary_ssrc, &rtx_ssrc);
3746 if (has_rtx && engine()->vie()->rtp()->SetLocalSSRC(
3747 channel_id, rtx_ssrc, webrtc::kViEStreamTypeRtx, stream_idx) != 0) {
3748 LOG_RTCERR4(SetLocalSSRC, channel_id, rtx_ssrc,
3749 webrtc::kViEStreamTypeRtx, stream_idx);
3750 return false;
3751 }
3752 return true;
3753}
3754
wu@webrtc.org24301a62013-12-13 19:17:43 +00003755void WebRtcVideoMediaChannel::MaybeConnectCapturer(VideoCapturer* capturer) {
3756 if (capturer != NULL && GetSendChannelNum(capturer) == 1) {
3757 capturer->SignalVideoFrame.connect(this,
3758 &WebRtcVideoMediaChannel::SendFrame);
3759 }
3760}
3761
3762void WebRtcVideoMediaChannel::MaybeDisconnectCapturer(VideoCapturer* capturer) {
3763 if (capturer != NULL && GetSendChannelNum(capturer) == 1) {
3764 capturer->SignalVideoFrame.disconnect(this);
3765 }
3766}
3767
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003768} // namespace cricket
3769
3770#endif // HAVE_WEBRTC_VIDEO