blob: ee1bbc5dfe1f4237f2722be73d41bc95a61b3939 [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"
henrike@webrtc.orga92fd742014-03-26 01:46:18 +000063#include "webrtc/experiments.h"
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +000064#include "webrtc/modules/remote_bitrate_estimator/include/remote_bitrate_estimator.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000065
66#if !defined(LIBPEERCONNECTION_LIB)
67#ifndef HAVE_WEBRTC_VIDEO
68#error Need webrtc video
69#endif
70#include "talk/media/webrtc/webrtcmediaengine.h"
71
72WRME_EXPORT
73cricket::MediaEngineInterface* CreateWebRtcMediaEngine(
74 webrtc::AudioDeviceModule* adm, webrtc::AudioDeviceModule* adm_sc,
75 cricket::WebRtcVideoEncoderFactory* encoder_factory,
76 cricket::WebRtcVideoDecoderFactory* decoder_factory) {
77 return new cricket::WebRtcMediaEngine(adm, adm_sc, encoder_factory,
78 decoder_factory);
79}
80
81WRME_EXPORT
82void DestroyWebRtcMediaEngine(cricket::MediaEngineInterface* media_engine) {
83 delete static_cast<cricket::WebRtcMediaEngine*>(media_engine);
84}
85#endif
86
87
88namespace cricket {
89
90
91static const int kDefaultLogSeverity = talk_base::LS_WARNING;
92
93static const int kMinVideoBitrate = 50;
94static const int kStartVideoBitrate = 300;
95static const int kMaxVideoBitrate = 2000;
96static const int kDefaultConferenceModeMaxVideoBitrate = 500;
97
wu@webrtc.orgcecfd182013-10-30 05:18:12 +000098// Controlled by exp, try a super low minimum bitrate for poor connections.
99static const int kLowerMinBitrate = 30;
100
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000101static const int kVideoMtu = 1200;
102
103static const int kVideoRtpBufferSize = 65536;
104
105static const char kVp8PayloadName[] = "VP8";
106static const char kRedPayloadName[] = "red";
107static const char kFecPayloadName[] = "ulpfec";
108
109static const int kDefaultNumberOfTemporalLayers = 1; // 1:1
110
111static const int kTimestampDeltaInSecondsForWarning = 2;
112
113static const int kMaxExternalVideoCodecs = 8;
114static const int kExternalVideoPayloadTypeBase = 120;
115
116// Static allocation of payload type values for external video codec.
117static int GetExternalVideoPayloadType(int index) {
118 ASSERT(index >= 0 && index < kMaxExternalVideoCodecs);
119 return kExternalVideoPayloadTypeBase + index;
120}
121
122static void LogMultiline(talk_base::LoggingSeverity sev, char* text) {
123 const char* delim = "\r\n";
124 // TODO(fbarchard): Fix strtok lint warning.
125 for (char* tok = strtok(text, delim); tok; tok = strtok(NULL, delim)) {
126 LOG_V(sev) << tok;
127 }
128}
129
130// Severity is an integer because it comes is assumed to be from command line.
131static int SeverityToFilter(int severity) {
132 int filter = webrtc::kTraceNone;
133 switch (severity) {
134 case talk_base::LS_VERBOSE:
135 filter |= webrtc::kTraceAll;
136 case talk_base::LS_INFO:
137 filter |= (webrtc::kTraceStateInfo | webrtc::kTraceInfo);
138 case talk_base::LS_WARNING:
139 filter |= (webrtc::kTraceTerseInfo | webrtc::kTraceWarning);
140 case talk_base::LS_ERROR:
141 filter |= (webrtc::kTraceError | webrtc::kTraceCritical);
142 }
143 return filter;
144}
145
146static const int kCpuMonitorPeriodMs = 2000; // 2 seconds.
147
148static const bool kNotSending = false;
149
wu@webrtc.orgde305012013-10-31 15:40:38 +0000150// Default video dscp value.
151// See http://tools.ietf.org/html/rfc2474 for details
152// See also http://tools.ietf.org/html/draft-jennings-rtcweb-qos-00
153static const talk_base::DiffServCodePoint kVideoDscpValue =
154 talk_base::DSCP_AF41;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000155
156static bool IsNackEnabled(const VideoCodec& codec) {
157 return codec.HasFeedbackParam(FeedbackParam(kRtcpFbParamNack,
158 kParamValueEmpty));
159}
160
161// Returns true if Receiver Estimated Max Bitrate is enabled.
162static bool IsRembEnabled(const VideoCodec& codec) {
163 return codec.HasFeedbackParam(FeedbackParam(kRtcpFbParamRemb,
164 kParamValueEmpty));
165}
166
167struct FlushBlackFrameData : public talk_base::MessageData {
168 FlushBlackFrameData(uint32 s, int64 t) : ssrc(s), timestamp(t) {
169 }
170 uint32 ssrc;
171 int64 timestamp;
172};
173
174class WebRtcRenderAdapter : public webrtc::ExternalRenderer {
175 public:
176 explicit WebRtcRenderAdapter(VideoRenderer* renderer)
henrike@webrtc.orge9793ab2014-03-18 14:36:23 +0000177 : renderer_(renderer), width_(0), height_(0) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000178 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000179
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000180 virtual ~WebRtcRenderAdapter() {
181 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000182
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000183 void SetRenderer(VideoRenderer* renderer) {
184 talk_base::CritScope cs(&crit_);
185 renderer_ = renderer;
186 // FrameSizeChange may have already been called when renderer was not set.
187 // If so we should call SetSize here.
188 // TODO(ronghuawu): Add unit test for this case. Didn't do it now
189 // because the WebRtcRenderAdapter is currently hiding in cc file. No
190 // good way to get access to it from the unit test.
191 if (width_ > 0 && height_ > 0 && renderer_ != NULL) {
192 if (!renderer_->SetSize(width_, height_, 0)) {
193 LOG(LS_ERROR)
194 << "WebRtcRenderAdapter SetRenderer failed to SetSize to: "
195 << width_ << "x" << height_;
196 }
197 }
198 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000199
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000200 // Implementation of webrtc::ExternalRenderer.
201 virtual int FrameSizeChange(unsigned int width, unsigned int height,
202 unsigned int /*number_of_streams*/) {
203 talk_base::CritScope cs(&crit_);
204 width_ = width;
205 height_ = height;
206 LOG(LS_INFO) << "WebRtcRenderAdapter frame size changed to: "
207 << width << "x" << height;
208 if (renderer_ == NULL) {
209 LOG(LS_VERBOSE) << "WebRtcRenderAdapter the renderer has not been set. "
210 << "SetSize will be called later in SetRenderer.";
211 return 0;
212 }
213 return renderer_->SetSize(width_, height_, 0) ? 0 : -1;
214 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000215
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000216 virtual int DeliverFrame(unsigned char* buffer, int buffer_size,
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000217 uint32_t time_stamp, int64_t render_time,
218 void* handle) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000219 talk_base::CritScope cs(&crit_);
220 frame_rate_tracker_.Update(1);
221 if (renderer_ == NULL) {
222 return 0;
223 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000224 // Convert 90K rtp timestamp to ns timestamp.
225 int64 rtp_time_stamp_in_ns = (time_stamp / 90) *
226 talk_base::kNumNanosecsPerMillisec;
227 // Convert milisecond render time to ns timestamp.
228 int64 render_time_stamp_in_ns = render_time *
229 talk_base::kNumNanosecsPerMillisec;
230 // Send the rtp timestamp to renderer as the VideoFrame timestamp.
231 // and the render timestamp as the VideoFrame elapsed_time.
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000232 if (handle == NULL) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000233 return DeliverBufferFrame(buffer, buffer_size, render_time_stamp_in_ns,
234 rtp_time_stamp_in_ns);
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000235 } else {
236 return DeliverTextureFrame(handle, render_time_stamp_in_ns,
237 rtp_time_stamp_in_ns);
238 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000239 }
240
241 virtual bool IsTextureSupported() { return true; }
242
243 int DeliverBufferFrame(unsigned char* buffer, int buffer_size,
244 int64 elapsed_time, int64 time_stamp) {
245 WebRtcVideoFrame video_frame;
wu@webrtc.org16d62542013-11-05 23:45:14 +0000246 video_frame.Alias(buffer, buffer_size, width_, height_,
247 1, 1, elapsed_time, time_stamp, 0);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000248
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000249 // Sanity check on decoded frame size.
250 if (buffer_size != static_cast<int>(VideoFrame::SizeOf(width_, height_))) {
251 LOG(LS_WARNING) << "WebRtcRenderAdapter received a strange frame size: "
252 << buffer_size;
253 }
254
255 int ret = renderer_->RenderFrame(&video_frame) ? 0 : -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000256 return ret;
257 }
258
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000259 int DeliverTextureFrame(void* handle, int64 elapsed_time, int64 time_stamp) {
260 WebRtcTextureVideoFrame video_frame(
261 static_cast<webrtc::NativeHandle*>(handle), width_, height_,
262 elapsed_time, time_stamp);
263 return renderer_->RenderFrame(&video_frame);
264 }
265
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000266 unsigned int width() {
267 talk_base::CritScope cs(&crit_);
268 return width_;
269 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000270
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000271 unsigned int height() {
272 talk_base::CritScope cs(&crit_);
273 return height_;
274 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000275
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000276 int framerate() {
277 talk_base::CritScope cs(&crit_);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000278 return static_cast<int>(frame_rate_tracker_.units_second());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000279 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000280
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000281 VideoRenderer* renderer() {
282 talk_base::CritScope cs(&crit_);
283 return renderer_;
284 }
285
286 private:
287 talk_base::CriticalSection crit_;
288 VideoRenderer* renderer_;
289 unsigned int width_;
290 unsigned int height_;
291 talk_base::RateTracker frame_rate_tracker_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000292};
293
294class WebRtcDecoderObserver : public webrtc::ViEDecoderObserver {
295 public:
296 explicit WebRtcDecoderObserver(int video_channel)
297 : video_channel_(video_channel),
298 framerate_(0),
299 bitrate_(0),
wu@webrtc.org97077a32013-10-25 21:18:33 +0000300 decode_ms_(0),
301 max_decode_ms_(0),
302 current_delay_ms_(0),
303 target_delay_ms_(0),
304 jitter_buffer_ms_(0),
305 min_playout_delay_ms_(0),
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +0000306 render_delay_ms_(0) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000307 }
308
309 // virtual functions from VieDecoderObserver.
310 virtual void IncomingCodecChanged(const int videoChannel,
311 const webrtc::VideoCodec& videoCodec) {}
312 virtual void IncomingRate(const int videoChannel,
313 const unsigned int framerate,
314 const unsigned int bitrate) {
wu@webrtc.org78187522013-10-07 23:32:02 +0000315 talk_base::CritScope cs(&crit_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000316 ASSERT(video_channel_ == videoChannel);
317 framerate_ = framerate;
318 bitrate_ = bitrate;
319 }
wu@webrtc.org97077a32013-10-25 21:18:33 +0000320
321 virtual void DecoderTiming(int decode_ms,
322 int max_decode_ms,
323 int current_delay_ms,
324 int target_delay_ms,
325 int jitter_buffer_ms,
326 int min_playout_delay_ms,
327 int render_delay_ms) {
328 talk_base::CritScope cs(&crit_);
329 decode_ms_ = decode_ms;
330 max_decode_ms_ = max_decode_ms;
331 current_delay_ms_ = current_delay_ms;
332 target_delay_ms_ = target_delay_ms;
333 jitter_buffer_ms_ = jitter_buffer_ms;
334 min_playout_delay_ms_ = min_playout_delay_ms;
335 render_delay_ms_ = render_delay_ms;
336 }
337
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +0000338 virtual void RequestNewKeyFrame(const int videoChannel) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000339
wu@webrtc.org97077a32013-10-25 21:18:33 +0000340 // Populate |rinfo| based on previously-set data in |*this|.
341 void ExportTo(VideoReceiverInfo* rinfo) {
wu@webrtc.org78187522013-10-07 23:32:02 +0000342 talk_base::CritScope cs(&crit_);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000343 rinfo->framerate_rcvd = framerate_;
344 rinfo->decode_ms = decode_ms_;
345 rinfo->max_decode_ms = max_decode_ms_;
346 rinfo->current_delay_ms = current_delay_ms_;
347 rinfo->target_delay_ms = target_delay_ms_;
348 rinfo->jitter_buffer_ms = jitter_buffer_ms_;
349 rinfo->min_playout_delay_ms = min_playout_delay_ms_;
350 rinfo->render_delay_ms = render_delay_ms_;
wu@webrtc.org78187522013-10-07 23:32:02 +0000351 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000352
353 private:
wu@webrtc.org78187522013-10-07 23:32:02 +0000354 mutable talk_base::CriticalSection crit_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000355 int video_channel_;
356 int framerate_;
357 int bitrate_;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000358 int decode_ms_;
359 int max_decode_ms_;
360 int current_delay_ms_;
361 int target_delay_ms_;
362 int jitter_buffer_ms_;
363 int min_playout_delay_ms_;
364 int render_delay_ms_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000365};
366
367class WebRtcEncoderObserver : public webrtc::ViEEncoderObserver {
368 public:
369 explicit WebRtcEncoderObserver(int video_channel)
370 : video_channel_(video_channel),
371 framerate_(0),
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000372 bitrate_(0),
373 suspended_(false) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000374 }
375
376 // virtual functions from VieEncoderObserver.
377 virtual void OutgoingRate(const int videoChannel,
378 const unsigned int framerate,
379 const unsigned int bitrate) {
wu@webrtc.org78187522013-10-07 23:32:02 +0000380 talk_base::CritScope cs(&crit_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000381 ASSERT(video_channel_ == videoChannel);
382 framerate_ = framerate;
383 bitrate_ = bitrate;
384 }
385
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000386 virtual void SuspendChange(int video_channel, bool is_suspended) {
387 talk_base::CritScope cs(&crit_);
388 ASSERT(video_channel_ == video_channel);
389 suspended_ = is_suspended;
390 }
391
wu@webrtc.org78187522013-10-07 23:32:02 +0000392 int framerate() const {
393 talk_base::CritScope cs(&crit_);
394 return framerate_;
395 }
396 int bitrate() const {
397 talk_base::CritScope cs(&crit_);
398 return bitrate_;
399 }
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000400 bool suspended() const {
401 talk_base::CritScope cs(&crit_);
402 return suspended_;
403 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000404
405 private:
wu@webrtc.org78187522013-10-07 23:32:02 +0000406 mutable talk_base::CriticalSection crit_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000407 int video_channel_;
408 int framerate_;
409 int bitrate_;
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000410 bool suspended_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000411};
412
413class WebRtcLocalStreamInfo {
414 public:
415 WebRtcLocalStreamInfo()
416 : width_(0), height_(0), elapsed_time_(-1), time_stamp_(-1) {}
417 size_t width() const {
418 talk_base::CritScope cs(&crit_);
419 return width_;
420 }
421 size_t height() const {
422 talk_base::CritScope cs(&crit_);
423 return height_;
424 }
425 int64 elapsed_time() const {
426 talk_base::CritScope cs(&crit_);
427 return elapsed_time_;
428 }
429 int64 time_stamp() const {
430 talk_base::CritScope cs(&crit_);
431 return time_stamp_;
432 }
433 int framerate() {
434 talk_base::CritScope cs(&crit_);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000435 return static_cast<int>(rate_tracker_.units_second());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000436 }
437 void GetLastFrameInfo(
438 size_t* width, size_t* height, int64* elapsed_time) const {
439 talk_base::CritScope cs(&crit_);
440 *width = width_;
441 *height = height_;
442 *elapsed_time = elapsed_time_;
443 }
444
445 void UpdateFrame(const VideoFrame* frame) {
446 talk_base::CritScope cs(&crit_);
447
448 width_ = frame->GetWidth();
449 height_ = frame->GetHeight();
450 elapsed_time_ = frame->GetElapsedTime();
451 time_stamp_ = frame->GetTimeStamp();
452
453 rate_tracker_.Update(1);
454 }
455
456 private:
457 mutable talk_base::CriticalSection crit_;
458 size_t width_;
459 size_t height_;
460 int64 elapsed_time_;
461 int64 time_stamp_;
462 talk_base::RateTracker rate_tracker_;
463
464 DISALLOW_COPY_AND_ASSIGN(WebRtcLocalStreamInfo);
465};
466
467// WebRtcVideoChannelRecvInfo is a container class with members such as renderer
468// and a decoder observer that is used by receive channels.
469// It must exist as long as the receive channel is connected to renderer or a
470// decoder observer in this class and methods in the class should only be called
471// from the worker thread.
472class WebRtcVideoChannelRecvInfo {
473 public:
474 typedef std::map<int, webrtc::VideoDecoder*> DecoderMap; // key: payload type
475 explicit WebRtcVideoChannelRecvInfo(int channel_id)
476 : channel_id_(channel_id),
477 render_adapter_(NULL),
478 decoder_observer_(channel_id) {
479 }
480 int channel_id() { return channel_id_; }
481 void SetRenderer(VideoRenderer* renderer) {
482 render_adapter_.SetRenderer(renderer);
483 }
484 WebRtcRenderAdapter* render_adapter() { return &render_adapter_; }
485 WebRtcDecoderObserver* decoder_observer() { return &decoder_observer_; }
486 void RegisterDecoder(int pl_type, webrtc::VideoDecoder* decoder) {
487 ASSERT(!IsDecoderRegistered(pl_type));
488 registered_decoders_[pl_type] = decoder;
489 }
490 bool IsDecoderRegistered(int pl_type) {
491 return registered_decoders_.count(pl_type) != 0;
492 }
493 const DecoderMap& registered_decoders() {
494 return registered_decoders_;
495 }
496 void ClearRegisteredDecoders() {
497 registered_decoders_.clear();
498 }
499
500 private:
501 int channel_id_; // Webrtc video channel number.
502 // Renderer for this channel.
503 WebRtcRenderAdapter render_adapter_;
504 WebRtcDecoderObserver decoder_observer_;
505 DecoderMap registered_decoders_;
506};
507
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000508class WebRtcOveruseObserver : public webrtc::CpuOveruseObserver {
509 public:
510 explicit WebRtcOveruseObserver(CoordinatedVideoAdapter* video_adapter)
511 : video_adapter_(video_adapter),
512 enabled_(false) {
513 }
514
515 // TODO(mflodman): Consider sending resolution as part of event, to let
516 // adapter know what resolution the request is based on. Helps eliminate stale
517 // data, race conditions.
518 virtual void OveruseDetected() OVERRIDE {
519 talk_base::CritScope cs(&crit_);
520 if (!enabled_) {
521 return;
522 }
523
524 video_adapter_->OnCpuResolutionRequest(CoordinatedVideoAdapter::DOWNGRADE);
525 }
526
527 virtual void NormalUsage() OVERRIDE {
528 talk_base::CritScope cs(&crit_);
529 if (!enabled_) {
530 return;
531 }
532
533 video_adapter_->OnCpuResolutionRequest(CoordinatedVideoAdapter::UPGRADE);
534 }
535
536 void Enable(bool enable) {
537 talk_base::CritScope cs(&crit_);
538 enabled_ = enable;
539 }
540
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000541 bool enabled() const { return enabled_; }
542
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000543 private:
544 CoordinatedVideoAdapter* video_adapter_;
545 bool enabled_;
546 talk_base::CriticalSection crit_;
547};
548
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000549
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000550class WebRtcVideoChannelSendInfo : public sigslot::has_slots<> {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000551 public:
552 typedef std::map<int, webrtc::VideoEncoder*> EncoderMap; // key: payload type
553 WebRtcVideoChannelSendInfo(int channel_id, int capture_id,
554 webrtc::ViEExternalCapture* external_capture,
555 talk_base::CpuMonitor* cpu_monitor)
556 : channel_id_(channel_id),
557 capture_id_(capture_id),
558 sending_(false),
559 muted_(false),
560 video_capturer_(NULL),
561 encoder_observer_(channel_id),
562 external_capture_(external_capture),
563 capturer_updated_(false),
564 interval_(0),
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000565 cpu_monitor_(cpu_monitor),
566 overuse_observer_enabled_(false) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000567 }
568
569 int channel_id() const { return channel_id_; }
570 int capture_id() const { return capture_id_; }
571 void set_sending(bool sending) { sending_ = sending; }
572 bool sending() const { return sending_; }
573 void set_muted(bool on) {
574 // TODO(asapersson): add support.
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000575 // video_adapter_.SetBlackOutput(on);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000576 muted_ = on;
577 }
578 bool muted() {return muted_; }
579
580 WebRtcEncoderObserver* encoder_observer() { return &encoder_observer_; }
581 webrtc::ViEExternalCapture* external_capture() { return external_capture_; }
582 const VideoFormat& video_format() const {
583 return video_format_;
584 }
585 void set_video_format(const VideoFormat& video_format) {
586 video_format_ = video_format;
587 if (video_format_ != cricket::VideoFormat()) {
588 interval_ = video_format_.interval;
589 }
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000590 CoordinatedVideoAdapter* adapter = video_adapter();
591 if (adapter) {
592 adapter->OnOutputFormatRequest(video_format_);
593 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000594 }
595 void set_interval(int64 interval) {
596 if (video_format() == cricket::VideoFormat()) {
597 interval_ = interval;
598 }
599 }
600 int64 interval() { return interval_; }
601
xians@webrtc.orgef221512014-02-21 10:31:29 +0000602 int CurrentAdaptReason() const {
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000603 const CoordinatedVideoAdapter* adapter = video_adapter();
604 if (!adapter) {
605 return CoordinatedVideoAdapter::ADAPTREASON_NONE;
606 }
607 return video_adapter()->adapt_reason();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000608 }
609
610 StreamParams* stream_params() { return stream_params_.get(); }
611 void set_stream_params(const StreamParams& sp) {
612 stream_params_.reset(new StreamParams(sp));
613 }
614 void ClearStreamParams() { stream_params_.reset(); }
615 bool has_ssrc(uint32 local_ssrc) const {
616 return !stream_params_ ? false :
617 stream_params_->has_ssrc(local_ssrc);
618 }
619 WebRtcLocalStreamInfo* local_stream_info() {
620 return &local_stream_info_;
621 }
622 VideoCapturer* video_capturer() {
623 return video_capturer_;
624 }
henrike@webrtc.orgc7bec842014-03-12 19:53:43 +0000625 void set_video_capturer(VideoCapturer* video_capturer,
626 ViEWrapper* vie_wrapper) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000627 if (video_capturer == video_capturer_) {
628 return;
629 }
xians@webrtc.orgef221512014-02-21 10:31:29 +0000630
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000631 CoordinatedVideoAdapter* old_video_adapter = video_adapter();
632 if (old_video_adapter) {
633 // Disconnect signals from old video adapter.
634 SignalCpuAdaptationUnable.disconnect(old_video_adapter);
635 if (cpu_monitor_) {
636 cpu_monitor_->SignalUpdate.disconnect(old_video_adapter);
xians@webrtc.orgef221512014-02-21 10:31:29 +0000637 }
henrike@webrtc.org26438052014-02-20 22:32:53 +0000638 }
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000639
640 capturer_updated_ = true;
641 video_capturer_ = video_capturer;
642
henrike@webrtc.orgc7bec842014-03-12 19:53:43 +0000643 vie_wrapper->base()->RegisterCpuOveruseObserver(channel_id_, NULL);
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000644 if (!video_capturer) {
645 overuse_observer_.reset();
646 return;
647 }
648
649 CoordinatedVideoAdapter* adapter = video_adapter();
650 ASSERT(adapter && "Video adapter should not be null here.");
651
652 UpdateAdapterCpuOptions();
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000653
654 overuse_observer_.reset(new WebRtcOveruseObserver(adapter));
henrike@webrtc.orgc7bec842014-03-12 19:53:43 +0000655 vie_wrapper->base()->RegisterCpuOveruseObserver(channel_id_,
656 overuse_observer_.get());
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000657 // (Dis)connect the video adapter from the cpu monitor as appropriate.
658 SetCpuOveruseDetection(overuse_observer_enabled_);
659
660 SignalCpuAdaptationUnable.repeat(adapter->SignalCpuAdaptationUnable);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000661 }
662
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000663 CoordinatedVideoAdapter* video_adapter() {
664 if (!video_capturer_) {
665 return NULL;
666 }
667 return video_capturer_->video_adapter();
668 }
669 const CoordinatedVideoAdapter* video_adapter() const {
670 if (!video_capturer_) {
671 return NULL;
672 }
673 return video_capturer_->video_adapter();
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000674 }
675
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000676 void ApplyCpuOptions(const VideoOptions& video_options) {
677 // Use video_options_.SetAll() instead of assignment so that unset value in
678 // video_options will not overwrite the previous option value.
679 video_options_.SetAll(video_options);
680 UpdateAdapterCpuOptions();
681 }
682
683 void UpdateAdapterCpuOptions() {
684 if (!video_capturer_) {
685 return;
686 }
687
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;
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000690
691 // TODO(thorcarpenter): Have VideoAdapter be responsible for setting
692 // all these video options.
693 CoordinatedVideoAdapter* video_adapter = video_capturer_->video_adapter();
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000694 if (video_options_.adapt_input_to_cpu_usage.Get(&cpu_adapt) ||
695 overuse_observer_enabled_) {
696 video_adapter->set_cpu_adaptation(cpu_adapt || overuse_observer_enabled_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000697 }
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000698 if (video_options_.adapt_cpu_with_smoothing.Get(&cpu_smoothing)) {
699 video_adapter->set_cpu_smoothing(cpu_smoothing);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000700 }
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000701 if (video_options_.process_adaptation_threshhold.Get(&med)) {
702 video_adapter->set_process_threshold(med);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000703 }
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000704 if (video_options_.system_low_adaptation_threshhold.Get(&low)) {
705 video_adapter->set_low_system_threshold(low);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000706 }
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000707 if (video_options_.system_high_adaptation_threshhold.Get(&high)) {
708 video_adapter->set_high_system_threshold(high);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000709 }
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000710 if (video_options_.video_adapt_third.Get(&adapt_third)) {
711 video_adapter->set_scale_third(adapt_third);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000712 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000713 }
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000714
715 void SetCpuOveruseDetection(bool enable) {
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000716 overuse_observer_enabled_ = enable;
717
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000718 if (overuse_observer_) {
719 overuse_observer_->Enable(enable);
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000720 }
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000721
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000722 // The video adapter is signaled by overuse detection if enabled; otherwise
723 // it will be signaled by cpu monitor.
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000724 CoordinatedVideoAdapter* adapter = video_adapter();
725 if (adapter) {
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000726 bool cpu_adapt = false;
727 video_options_.adapt_input_to_cpu_usage.Get(&cpu_adapt);
728 adapter->set_cpu_adaptation(
729 adapter->cpu_adaptation() || cpu_adapt || enable);
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000730 if (cpu_monitor_) {
731 if (enable) {
732 cpu_monitor_->SignalUpdate.disconnect(adapter);
733 } else {
734 cpu_monitor_->SignalUpdate.connect(
735 adapter, &CoordinatedVideoAdapter::OnCpuLoadUpdated);
736 }
737 }
738 }
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000739 }
740
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000741 void ProcessFrame(const VideoFrame& original_frame, bool mute,
742 VideoFrame** processed_frame) {
743 if (!mute) {
744 *processed_frame = original_frame.Copy();
745 } else {
746 WebRtcVideoFrame* black_frame = new WebRtcVideoFrame();
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000747 black_frame->InitToBlack(static_cast<int>(original_frame.GetWidth()),
748 static_cast<int>(original_frame.GetHeight()),
749 1, 1,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000750 original_frame.GetElapsedTime(),
751 original_frame.GetTimeStamp());
752 *processed_frame = black_frame;
753 }
754 local_stream_info_.UpdateFrame(*processed_frame);
755 }
756 void RegisterEncoder(int pl_type, webrtc::VideoEncoder* encoder) {
757 ASSERT(!IsEncoderRegistered(pl_type));
758 registered_encoders_[pl_type] = encoder;
759 }
760 bool IsEncoderRegistered(int pl_type) {
761 return registered_encoders_.count(pl_type) != 0;
762 }
763 const EncoderMap& registered_encoders() {
764 return registered_encoders_;
765 }
766 void ClearRegisteredEncoders() {
767 registered_encoders_.clear();
768 }
769
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000770 sigslot::repeater0<> SignalCpuAdaptationUnable;
771
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000772 private:
773 int channel_id_;
774 int capture_id_;
775 bool sending_;
776 bool muted_;
777 VideoCapturer* video_capturer_;
778 WebRtcEncoderObserver encoder_observer_;
779 webrtc::ViEExternalCapture* external_capture_;
780 EncoderMap registered_encoders_;
781
782 VideoFormat video_format_;
783
784 talk_base::scoped_ptr<StreamParams> stream_params_;
785
786 WebRtcLocalStreamInfo local_stream_info_;
787
788 bool capturer_updated_;
789
790 int64 interval_;
791
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000792 talk_base::CpuMonitor* cpu_monitor_;
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000793 talk_base::scoped_ptr<WebRtcOveruseObserver> overuse_observer_;
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000794 bool overuse_observer_enabled_;
795
796 VideoOptions video_options_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000797};
798
799const WebRtcVideoEngine::VideoCodecPref
800 WebRtcVideoEngine::kVideoCodecPrefs[] = {
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000801 {kVp8PayloadName, 100, -1, 0},
802 {kRedPayloadName, 116, -1, 1},
803 {kFecPayloadName, 117, -1, 2},
804 {kRtxCodecName, 96, 100, 3},
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000805};
806
807// The formats are sorted by the descending order of width. We use the order to
808// find the next format for CPU and bandwidth adaptation.
809const VideoFormatPod WebRtcVideoEngine::kVideoFormats[] = {
810 {1280, 800, FPS_TO_INTERVAL(30), FOURCC_ANY},
811 {1280, 720, FPS_TO_INTERVAL(30), FOURCC_ANY},
812 {960, 600, FPS_TO_INTERVAL(30), FOURCC_ANY},
813 {960, 540, FPS_TO_INTERVAL(30), FOURCC_ANY},
814 {640, 400, FPS_TO_INTERVAL(30), FOURCC_ANY},
815 {640, 360, FPS_TO_INTERVAL(30), FOURCC_ANY},
816 {640, 480, FPS_TO_INTERVAL(30), FOURCC_ANY},
817 {480, 300, FPS_TO_INTERVAL(30), FOURCC_ANY},
818 {480, 270, FPS_TO_INTERVAL(30), FOURCC_ANY},
819 {480, 360, FPS_TO_INTERVAL(30), FOURCC_ANY},
820 {320, 200, FPS_TO_INTERVAL(30), FOURCC_ANY},
821 {320, 180, FPS_TO_INTERVAL(30), FOURCC_ANY},
822 {320, 240, FPS_TO_INTERVAL(30), FOURCC_ANY},
823 {240, 150, FPS_TO_INTERVAL(30), FOURCC_ANY},
824 {240, 135, FPS_TO_INTERVAL(30), FOURCC_ANY},
825 {240, 180, FPS_TO_INTERVAL(30), FOURCC_ANY},
826 {160, 100, FPS_TO_INTERVAL(30), FOURCC_ANY},
827 {160, 90, FPS_TO_INTERVAL(30), FOURCC_ANY},
828 {160, 120, FPS_TO_INTERVAL(30), FOURCC_ANY},
829};
830
831const VideoFormatPod WebRtcVideoEngine::kDefaultVideoFormat =
832 {640, 400, FPS_TO_INTERVAL(30), FOURCC_ANY};
833
834static void UpdateVideoCodec(const cricket::VideoFormat& video_format,
835 webrtc::VideoCodec* target_codec) {
836 if ((target_codec == NULL) || (video_format == cricket::VideoFormat())) {
837 return;
838 }
839 target_codec->width = video_format.width;
840 target_codec->height = video_format.height;
841 target_codec->maxFramerate = cricket::VideoFormat::IntervalToFps(
842 video_format.interval);
843}
844
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +0000845#ifdef USE_WEBRTC_DEV_BRANCH
846static bool GetCpuOveruseOptions(const VideoOptions& options,
847 webrtc::CpuOveruseOptions* overuse_options) {
848 int underuse_threshold = 0;
849 int overuse_threshold = 0;
850 if (!options.cpu_underuse_threshold.Get(&underuse_threshold) ||
851 !options.cpu_overuse_threshold.Get(&overuse_threshold)) {
852 return false;
853 }
854 if (underuse_threshold <= 0 || overuse_threshold <= 0) {
855 return false;
856 }
857 // Valid thresholds.
858 bool encode_usage =
859 options.cpu_overuse_encode_usage.GetWithDefaultIfUnset(false);
860 overuse_options->enable_capture_jitter_method = !encode_usage;
861 overuse_options->enable_encode_usage_method = encode_usage;
862 if (encode_usage) {
863 // Use method based on encode usage.
864 overuse_options->low_encode_usage_threshold_percent = underuse_threshold;
865 overuse_options->high_encode_usage_threshold_percent = overuse_threshold;
866 } else {
867 // Use default method based on capture jitter.
868 overuse_options->low_capture_jitter_threshold_ms =
869 static_cast<float>(underuse_threshold);
870 overuse_options->high_capture_jitter_threshold_ms =
871 static_cast<float>(overuse_threshold);
872 }
873 return true;
874}
875#endif
876
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000877WebRtcVideoEngine::WebRtcVideoEngine() {
878 Construct(new ViEWrapper(), new ViETraceWrapper(), NULL,
879 new talk_base::CpuMonitor(NULL));
880}
881
882WebRtcVideoEngine::WebRtcVideoEngine(WebRtcVoiceEngine* voice_engine,
883 ViEWrapper* vie_wrapper,
884 talk_base::CpuMonitor* cpu_monitor) {
885 Construct(vie_wrapper, new ViETraceWrapper(), voice_engine, cpu_monitor);
886}
887
888WebRtcVideoEngine::WebRtcVideoEngine(WebRtcVoiceEngine* voice_engine,
889 ViEWrapper* vie_wrapper,
890 ViETraceWrapper* tracing,
891 talk_base::CpuMonitor* cpu_monitor) {
892 Construct(vie_wrapper, tracing, voice_engine, cpu_monitor);
893}
894
895void WebRtcVideoEngine::Construct(ViEWrapper* vie_wrapper,
896 ViETraceWrapper* tracing,
897 WebRtcVoiceEngine* voice_engine,
898 talk_base::CpuMonitor* cpu_monitor) {
899 LOG(LS_INFO) << "WebRtcVideoEngine::WebRtcVideoEngine";
900 worker_thread_ = NULL;
901 vie_wrapper_.reset(vie_wrapper);
902 vie_wrapper_base_initialized_ = false;
903 tracing_.reset(tracing);
904 voice_engine_ = voice_engine;
905 initialized_ = false;
906 SetTraceFilter(SeverityToFilter(kDefaultLogSeverity));
907 render_module_.reset(new WebRtcPassthroughRender());
908 local_renderer_w_ = local_renderer_h_ = 0;
909 local_renderer_ = NULL;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000910 capture_started_ = false;
911 decoder_factory_ = NULL;
912 encoder_factory_ = NULL;
913 cpu_monitor_.reset(cpu_monitor);
914
915 SetTraceOptions("");
916 if (tracing_->SetTraceCallback(this) != 0) {
917 LOG_RTCERR1(SetTraceCallback, this);
918 }
919
920 // Set default quality levels for our supported codecs. We override them here
921 // if we know your cpu performance is low, and they can be updated explicitly
922 // by calling SetDefaultCodec. For example by a flute preference setting, or
923 // by the server with a jec in response to our reported system info.
924 VideoCodec max_codec(kVideoCodecPrefs[0].payload_type,
925 kVideoCodecPrefs[0].name,
926 kDefaultVideoFormat.width,
927 kDefaultVideoFormat.height,
928 VideoFormat::IntervalToFps(kDefaultVideoFormat.interval),
929 0);
930 if (!SetDefaultCodec(max_codec)) {
931 LOG(LS_ERROR) << "Failed to initialize list of supported codec types";
932 }
933
934
935 // Load our RTP Header extensions.
936 rtp_header_extensions_.push_back(
937 RtpHeaderExtension(kRtpTimestampOffsetHeaderExtension,
henrike@webrtc.org79047f92014-03-06 23:46:59 +0000938 kRtpTimestampOffsetHeaderExtensionDefaultId));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000939 rtp_header_extensions_.push_back(
henrike@webrtc.org79047f92014-03-06 23:46:59 +0000940 RtpHeaderExtension(kRtpAbsoluteSenderTimeHeaderExtension,
941 kRtpAbsoluteSenderTimeHeaderExtensionDefaultId));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000942}
943
944WebRtcVideoEngine::~WebRtcVideoEngine() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000945 LOG(LS_INFO) << "WebRtcVideoEngine::~WebRtcVideoEngine";
946 if (initialized_) {
947 Terminate();
948 }
949 if (encoder_factory_) {
950 encoder_factory_->RemoveObserver(this);
951 }
952 tracing_->SetTraceCallback(NULL);
953 // Test to see if the media processor was deregistered properly.
954 ASSERT(SignalMediaFrame.is_empty());
955}
956
957bool WebRtcVideoEngine::Init(talk_base::Thread* worker_thread) {
958 LOG(LS_INFO) << "WebRtcVideoEngine::Init";
959 worker_thread_ = worker_thread;
960 ASSERT(worker_thread_ != NULL);
961
962 cpu_monitor_->set_thread(worker_thread_);
963 if (!cpu_monitor_->Start(kCpuMonitorPeriodMs)) {
964 LOG(LS_ERROR) << "Failed to start CPU monitor.";
965 cpu_monitor_.reset();
966 }
967
968 bool result = InitVideoEngine();
969 if (result) {
970 LOG(LS_INFO) << "VideoEngine Init done";
971 } else {
972 LOG(LS_ERROR) << "VideoEngine Init failed, releasing";
973 Terminate();
974 }
975 return result;
976}
977
978bool WebRtcVideoEngine::InitVideoEngine() {
979 LOG(LS_INFO) << "WebRtcVideoEngine::InitVideoEngine";
980
981 // Init WebRTC VideoEngine.
982 if (!vie_wrapper_base_initialized_) {
983 if (vie_wrapper_->base()->Init() != 0) {
984 LOG_RTCERR0(Init);
985 return false;
986 }
987 vie_wrapper_base_initialized_ = true;
988 }
989
990 // Log the VoiceEngine version info.
991 char buffer[1024] = "";
992 if (vie_wrapper_->base()->GetVersion(buffer) != 0) {
993 LOG_RTCERR0(GetVersion);
994 return false;
995 }
996
997 LOG(LS_INFO) << "WebRtc VideoEngine Version:";
998 LogMultiline(talk_base::LS_INFO, buffer);
999
1000 // Hook up to VoiceEngine for sync purposes, if supplied.
1001 if (!voice_engine_) {
1002 LOG(LS_WARNING) << "NULL voice engine";
1003 } else if ((vie_wrapper_->base()->SetVoiceEngine(
1004 voice_engine_->voe()->engine())) != 0) {
1005 LOG_RTCERR0(SetVoiceEngine);
1006 return false;
1007 }
1008
1009 // Register our custom render module.
1010 if (vie_wrapper_->render()->RegisterVideoRenderModule(
1011 *render_module_.get()) != 0) {
1012 LOG_RTCERR0(RegisterVideoRenderModule);
1013 return false;
1014 }
1015
1016 initialized_ = true;
1017 return true;
1018}
1019
1020void WebRtcVideoEngine::Terminate() {
1021 LOG(LS_INFO) << "WebRtcVideoEngine::Terminate";
1022 initialized_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001023
1024 if (vie_wrapper_->render()->DeRegisterVideoRenderModule(
1025 *render_module_.get()) != 0) {
1026 LOG_RTCERR0(DeRegisterVideoRenderModule);
1027 }
1028
1029 if (vie_wrapper_->base()->SetVoiceEngine(NULL) != 0) {
1030 LOG_RTCERR0(SetVoiceEngine);
1031 }
1032
1033 cpu_monitor_->Stop();
1034}
1035
1036int WebRtcVideoEngine::GetCapabilities() {
1037 return VIDEO_RECV | VIDEO_SEND;
1038}
1039
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00001040bool WebRtcVideoEngine::SetOptions(const VideoOptions &options) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001041 return true;
1042}
1043
1044bool WebRtcVideoEngine::SetDefaultEncoderConfig(
1045 const VideoEncoderConfig& config) {
1046 return SetDefaultCodec(config.max_codec);
1047}
1048
wu@webrtc.org78187522013-10-07 23:32:02 +00001049VideoEncoderConfig WebRtcVideoEngine::GetDefaultEncoderConfig() const {
1050 ASSERT(!video_codecs_.empty());
1051 VideoCodec max_codec(kVideoCodecPrefs[0].payload_type,
1052 kVideoCodecPrefs[0].name,
1053 video_codecs_[0].width,
1054 video_codecs_[0].height,
1055 video_codecs_[0].framerate,
1056 0);
1057 return VideoEncoderConfig(max_codec);
1058}
1059
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001060// SetDefaultCodec may be called while the capturer is running. For example, a
1061// test call is started in a page with QVGA default codec, and then a real call
1062// is started in another page with VGA default codec. This is the corner case
1063// and happens only when a session is started. We ignore this case currently.
1064bool WebRtcVideoEngine::SetDefaultCodec(const VideoCodec& codec) {
1065 if (!RebuildCodecList(codec)) {
1066 LOG(LS_WARNING) << "Failed to RebuildCodecList";
1067 return false;
1068 }
1069
wu@webrtc.org78187522013-10-07 23:32:02 +00001070 ASSERT(!video_codecs_.empty());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001071 default_codec_format_ = VideoFormat(
1072 video_codecs_[0].width,
1073 video_codecs_[0].height,
1074 VideoFormat::FpsToInterval(video_codecs_[0].framerate),
1075 FOURCC_ANY);
1076 return true;
1077}
1078
1079WebRtcVideoMediaChannel* WebRtcVideoEngine::CreateChannel(
1080 VoiceMediaChannel* voice_channel) {
1081 WebRtcVideoMediaChannel* channel =
1082 new WebRtcVideoMediaChannel(this, voice_channel);
1083 if (!channel->Init()) {
1084 delete channel;
1085 channel = NULL;
1086 }
1087 return channel;
1088}
1089
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001090bool WebRtcVideoEngine::SetLocalRenderer(VideoRenderer* renderer) {
1091 local_renderer_w_ = local_renderer_h_ = 0;
1092 local_renderer_ = renderer;
1093 return true;
1094}
1095
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001096const std::vector<VideoCodec>& WebRtcVideoEngine::codecs() const {
1097 return video_codecs_;
1098}
1099
1100const std::vector<RtpHeaderExtension>&
1101WebRtcVideoEngine::rtp_header_extensions() const {
1102 return rtp_header_extensions_;
1103}
1104
1105void WebRtcVideoEngine::SetLogging(int min_sev, const char* filter) {
1106 // if min_sev == -1, we keep the current log level.
1107 if (min_sev >= 0) {
1108 SetTraceFilter(SeverityToFilter(min_sev));
1109 }
1110 SetTraceOptions(filter);
1111}
1112
1113int WebRtcVideoEngine::GetLastEngineError() {
1114 return vie_wrapper_->error();
1115}
1116
1117// Checks to see whether we comprehend and could receive a particular codec
1118bool WebRtcVideoEngine::FindCodec(const VideoCodec& in) {
1119 for (int i = 0; i < ARRAY_SIZE(kVideoFormats); ++i) {
1120 const VideoFormat fmt(kVideoFormats[i]);
1121 if ((in.width == 0 && in.height == 0) ||
1122 (fmt.width == in.width && fmt.height == in.height)) {
1123 if (encoder_factory_) {
1124 const std::vector<WebRtcVideoEncoderFactory::VideoCodec>& codecs =
1125 encoder_factory_->codecs();
1126 for (size_t j = 0; j < codecs.size(); ++j) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001127 VideoCodec codec(GetExternalVideoPayloadType(static_cast<int>(j)),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001128 codecs[j].name, 0, 0, 0, 0);
1129 if (codec.Matches(in))
1130 return true;
1131 }
1132 }
1133 for (size_t j = 0; j < ARRAY_SIZE(kVideoCodecPrefs); ++j) {
1134 VideoCodec codec(kVideoCodecPrefs[j].payload_type,
1135 kVideoCodecPrefs[j].name, 0, 0, 0, 0);
1136 if (codec.Matches(in)) {
1137 return true;
1138 }
1139 }
1140 }
1141 }
1142 return false;
1143}
1144
1145// Given the requested codec, returns true if we can send that codec type and
1146// updates out with the best quality we could send for that codec. If current is
1147// not empty, we constrain out so that its aspect ratio matches current's.
1148bool WebRtcVideoEngine::CanSendCodec(const VideoCodec& requested,
1149 const VideoCodec& current,
1150 VideoCodec* out) {
1151 if (!out) {
1152 return false;
1153 }
1154
1155 std::vector<VideoCodec>::const_iterator local_max;
1156 for (local_max = video_codecs_.begin();
1157 local_max < video_codecs_.end();
1158 ++local_max) {
1159 // First match codecs by payload type
1160 if (!requested.Matches(*local_max)) {
1161 continue;
1162 }
1163
1164 out->id = requested.id;
1165 out->name = requested.name;
1166 out->preference = requested.preference;
1167 out->params = requested.params;
1168 out->framerate = talk_base::_min(requested.framerate, local_max->framerate);
1169 out->width = 0;
1170 out->height = 0;
1171 out->params = requested.params;
1172 out->feedback_params = requested.feedback_params;
1173
1174 if (0 == requested.width && 0 == requested.height) {
1175 // Special case with resolution 0. The channel should not send frames.
1176 return true;
1177 } else if (0 == requested.width || 0 == requested.height) {
1178 // 0xn and nx0 are invalid resolutions.
1179 return false;
1180 }
1181
1182 // Pick the best quality that is within their and our bounds and has the
1183 // correct aspect ratio.
1184 for (int j = 0; j < ARRAY_SIZE(kVideoFormats); ++j) {
1185 const VideoFormat format(kVideoFormats[j]);
1186
1187 // Skip any format that is larger than the local or remote maximums, or
1188 // smaller than the current best match
1189 if (format.width > requested.width || format.height > requested.height ||
1190 format.width > local_max->width ||
1191 (format.width < out->width && format.height < out->height)) {
1192 continue;
1193 }
1194
1195 bool better = false;
1196
1197 // Check any further constraints on this prospective format
1198 if (!out->width || !out->height) {
1199 // If we don't have any matches yet, this is the best so far.
1200 better = true;
1201 } else if (current.width && current.height) {
1202 // current is set so format must match its ratio exactly.
1203 better =
1204 (format.width * current.height == format.height * current.width);
1205 } else {
1206 // Prefer closer aspect ratios i.e
1207 // format.aspect - requested.aspect < out.aspect - requested.aspect
1208 better = abs(format.width * requested.height * out->height -
1209 requested.width * format.height * out->height) <
1210 abs(out->width * format.height * requested.height -
1211 requested.width * format.height * out->height);
1212 }
1213
1214 if (better) {
1215 out->width = format.width;
1216 out->height = format.height;
1217 }
1218 }
1219 if (out->width > 0) {
1220 return true;
1221 }
1222 }
1223 return false;
1224}
1225
1226static void ConvertToCricketVideoCodec(
1227 const webrtc::VideoCodec& in_codec, VideoCodec* out_codec) {
1228 out_codec->id = in_codec.plType;
1229 out_codec->name = in_codec.plName;
1230 out_codec->width = in_codec.width;
1231 out_codec->height = in_codec.height;
1232 out_codec->framerate = in_codec.maxFramerate;
1233 out_codec->SetParam(kCodecParamMinBitrate, in_codec.minBitrate);
1234 out_codec->SetParam(kCodecParamMaxBitrate, in_codec.maxBitrate);
1235 if (in_codec.qpMax) {
1236 out_codec->SetParam(kCodecParamMaxQuantization, in_codec.qpMax);
1237 }
1238}
1239
1240bool WebRtcVideoEngine::ConvertFromCricketVideoCodec(
1241 const VideoCodec& in_codec, webrtc::VideoCodec* out_codec) {
1242 bool found = false;
1243 int ncodecs = vie_wrapper_->codec()->NumberOfCodecs();
1244 for (int i = 0; i < ncodecs; ++i) {
1245 if (vie_wrapper_->codec()->GetCodec(i, *out_codec) == 0 &&
1246 _stricmp(in_codec.name.c_str(), out_codec->plName) == 0) {
1247 found = true;
1248 break;
1249 }
1250 }
1251
1252 // If not found, check if this is supported by external encoder factory.
1253 if (!found && encoder_factory_) {
1254 const std::vector<WebRtcVideoEncoderFactory::VideoCodec>& codecs =
1255 encoder_factory_->codecs();
1256 for (size_t i = 0; i < codecs.size(); ++i) {
1257 if (_stricmp(in_codec.name.c_str(), codecs[i].name.c_str()) == 0) {
1258 out_codec->codecType = codecs[i].type;
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001259 out_codec->plType = GetExternalVideoPayloadType(static_cast<int>(i));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001260 talk_base::strcpyn(out_codec->plName, sizeof(out_codec->plName),
1261 codecs[i].name.c_str(), codecs[i].name.length());
1262 found = true;
1263 break;
1264 }
1265 }
1266 }
1267
1268 if (!found) {
1269 LOG(LS_ERROR) << "invalid codec type";
1270 return false;
1271 }
1272
1273 if (in_codec.id != 0)
1274 out_codec->plType = in_codec.id;
1275
1276 if (in_codec.width != 0)
1277 out_codec->width = in_codec.width;
1278
1279 if (in_codec.height != 0)
1280 out_codec->height = in_codec.height;
1281
1282 if (in_codec.framerate != 0)
1283 out_codec->maxFramerate = in_codec.framerate;
1284
1285 // Convert bitrate parameters.
1286 int max_bitrate = kMaxVideoBitrate;
1287 int min_bitrate = kMinVideoBitrate;
1288 int start_bitrate = kStartVideoBitrate;
1289
1290 in_codec.GetParam(kCodecParamMinBitrate, &min_bitrate);
1291 in_codec.GetParam(kCodecParamMaxBitrate, &max_bitrate);
1292
1293 if (max_bitrate < min_bitrate) {
1294 return false;
1295 }
1296 start_bitrate = talk_base::_max(start_bitrate, min_bitrate);
1297 start_bitrate = talk_base::_min(start_bitrate, max_bitrate);
1298
1299 out_codec->minBitrate = min_bitrate;
1300 out_codec->startBitrate = start_bitrate;
1301 out_codec->maxBitrate = max_bitrate;
1302
1303 // Convert general codec parameters.
1304 int max_quantization = 0;
1305 if (in_codec.GetParam(kCodecParamMaxQuantization, &max_quantization)) {
1306 if (max_quantization < 0) {
1307 return false;
1308 }
1309 out_codec->qpMax = max_quantization;
1310 }
1311 return true;
1312}
1313
1314void WebRtcVideoEngine::RegisterChannel(WebRtcVideoMediaChannel *channel) {
1315 talk_base::CritScope cs(&channels_crit_);
1316 channels_.push_back(channel);
1317}
1318
1319void WebRtcVideoEngine::UnregisterChannel(WebRtcVideoMediaChannel *channel) {
1320 talk_base::CritScope cs(&channels_crit_);
1321 channels_.erase(std::remove(channels_.begin(), channels_.end(), channel),
1322 channels_.end());
1323}
1324
1325bool WebRtcVideoEngine::SetVoiceEngine(WebRtcVoiceEngine* voice_engine) {
1326 if (initialized_) {
1327 LOG(LS_WARNING) << "SetVoiceEngine can not be called after Init";
1328 return false;
1329 }
1330 voice_engine_ = voice_engine;
1331 return true;
1332}
1333
1334bool WebRtcVideoEngine::EnableTimedRender() {
1335 if (initialized_) {
1336 LOG(LS_WARNING) << "EnableTimedRender can not be called after Init";
1337 return false;
1338 }
1339 render_module_.reset(webrtc::VideoRender::CreateVideoRender(0, NULL,
1340 false, webrtc::kRenderExternal));
1341 return true;
1342}
1343
1344void WebRtcVideoEngine::SetTraceFilter(int filter) {
1345 tracing_->SetTraceFilter(filter);
1346}
1347
1348// See https://sites.google.com/a/google.com/wavelet/
1349// Home/Magic-Flute--RTC-Engine-/Magic-Flute-Command-Line-Parameters
1350// for all supported command line setttings.
1351void WebRtcVideoEngine::SetTraceOptions(const std::string& options) {
1352 // Set WebRTC trace file.
1353 std::vector<std::string> opts;
1354 talk_base::tokenize(options, ' ', '"', '"', &opts);
1355 std::vector<std::string>::iterator tracefile =
1356 std::find(opts.begin(), opts.end(), "tracefile");
1357 if (tracefile != opts.end() && ++tracefile != opts.end()) {
1358 // Write WebRTC debug output (at same loglevel) to file
1359 if (tracing_->SetTraceFile(tracefile->c_str()) == -1) {
1360 LOG_RTCERR1(SetTraceFile, *tracefile);
1361 }
1362 }
1363}
1364
1365static void AddDefaultFeedbackParams(VideoCodec* codec) {
1366 const FeedbackParam kFir(kRtcpFbParamCcm, kRtcpFbCcmParamFir);
1367 codec->AddFeedbackParam(kFir);
1368 const FeedbackParam kNack(kRtcpFbParamNack, kParamValueEmpty);
1369 codec->AddFeedbackParam(kNack);
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001370 const FeedbackParam kPli(kRtcpFbParamNack, kRtcpFbNackParamPli);
1371 codec->AddFeedbackParam(kPli);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001372 const FeedbackParam kRemb(kRtcpFbParamRemb, kParamValueEmpty);
1373 codec->AddFeedbackParam(kRemb);
1374}
1375
1376// Rebuilds the codec list to be only those that are less intensive
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00001377// than the specified codec. Prefers internal codec over external with
1378// higher preference field.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001379bool WebRtcVideoEngine::RebuildCodecList(const VideoCodec& in_codec) {
1380 if (!FindCodec(in_codec))
1381 return false;
1382
1383 video_codecs_.clear();
1384
1385 bool found = false;
mallinath@webrtc.org1112c302013-09-23 20:34:45 +00001386 std::set<std::string> internal_codec_names;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001387 for (size_t i = 0; i < ARRAY_SIZE(kVideoCodecPrefs); ++i) {
1388 const VideoCodecPref& pref(kVideoCodecPrefs[i]);
1389 if (!found)
1390 found = (in_codec.name == pref.name);
mallinath@webrtc.org1112c302013-09-23 20:34:45 +00001391 if (found) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001392 VideoCodec codec(pref.payload_type, pref.name,
1393 in_codec.width, in_codec.height, in_codec.framerate,
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001394 static_cast<int>(ARRAY_SIZE(kVideoCodecPrefs) - i));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001395 if (_stricmp(kVp8PayloadName, codec.name.c_str()) == 0) {
1396 AddDefaultFeedbackParams(&codec);
1397 }
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00001398 if (pref.associated_payload_type != -1) {
1399 codec.SetParam(kCodecParamAssociatedPayloadType,
1400 pref.associated_payload_type);
1401 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001402 video_codecs_.push_back(codec);
mallinath@webrtc.org1112c302013-09-23 20:34:45 +00001403 internal_codec_names.insert(codec.name);
1404 }
1405 }
1406 if (encoder_factory_) {
1407 const std::vector<WebRtcVideoEncoderFactory::VideoCodec>& codecs =
1408 encoder_factory_->codecs();
1409 for (size_t i = 0; i < codecs.size(); ++i) {
1410 bool is_internal_codec = internal_codec_names.find(codecs[i].name) !=
1411 internal_codec_names.end();
1412 if (!is_internal_codec) {
1413 if (!found)
1414 found = (in_codec.name == codecs[i].name);
1415 VideoCodec codec(
1416 GetExternalVideoPayloadType(static_cast<int>(i)),
1417 codecs[i].name,
1418 codecs[i].max_width,
1419 codecs[i].max_height,
1420 codecs[i].max_fps,
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00001421 // Use negative preference on external codec to ensure the internal
1422 // codec is preferred.
1423 static_cast<int>(0 - i));
mallinath@webrtc.org1112c302013-09-23 20:34:45 +00001424 AddDefaultFeedbackParams(&codec);
1425 video_codecs_.push_back(codec);
1426 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001427 }
1428 }
1429 ASSERT(found);
1430 return true;
1431}
1432
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001433// Ignore spammy trace messages, mostly from the stats API when we haven't
1434// gotten RTCP info yet from the remote side.
1435bool WebRtcVideoEngine::ShouldIgnoreTrace(const std::string& trace) {
1436 static const char* const kTracesToIgnore[] = {
1437 NULL
1438 };
1439 for (const char* const* p = kTracesToIgnore; *p; ++p) {
1440 if (trace.find(*p) == 0) {
1441 return true;
1442 }
1443 }
1444 return false;
1445}
1446
1447int WebRtcVideoEngine::GetNumOfChannels() {
1448 talk_base::CritScope cs(&channels_crit_);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00001449 return static_cast<int>(channels_.size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001450}
1451
1452void WebRtcVideoEngine::Print(webrtc::TraceLevel level, const char* trace,
1453 int length) {
1454 talk_base::LoggingSeverity sev = talk_base::LS_VERBOSE;
1455 if (level == webrtc::kTraceError || level == webrtc::kTraceCritical)
1456 sev = talk_base::LS_ERROR;
1457 else if (level == webrtc::kTraceWarning)
1458 sev = talk_base::LS_WARNING;
1459 else if (level == webrtc::kTraceStateInfo || level == webrtc::kTraceInfo)
1460 sev = talk_base::LS_INFO;
1461 else if (level == webrtc::kTraceTerseInfo)
1462 sev = talk_base::LS_INFO;
1463
1464 // Skip past boilerplate prefix text
1465 if (length < 72) {
1466 std::string msg(trace, length);
1467 LOG(LS_ERROR) << "Malformed webrtc log message: ";
1468 LOG_V(sev) << msg;
1469 } else {
1470 std::string msg(trace + 71, length - 72);
1471 if (!ShouldIgnoreTrace(msg) &&
1472 (!voice_engine_ || !voice_engine_->ShouldIgnoreTrace(msg))) {
1473 LOG_V(sev) << "webrtc: " << msg;
1474 }
1475 }
1476}
1477
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001478webrtc::VideoDecoder* WebRtcVideoEngine::CreateExternalDecoder(
1479 webrtc::VideoCodecType type) {
1480 if (decoder_factory_ == NULL) {
1481 return NULL;
1482 }
1483 return decoder_factory_->CreateVideoDecoder(type);
1484}
1485
1486void WebRtcVideoEngine::DestroyExternalDecoder(webrtc::VideoDecoder* decoder) {
1487 ASSERT(decoder_factory_ != NULL);
1488 if (decoder_factory_ == NULL)
1489 return;
1490 decoder_factory_->DestroyVideoDecoder(decoder);
1491}
1492
1493webrtc::VideoEncoder* WebRtcVideoEngine::CreateExternalEncoder(
1494 webrtc::VideoCodecType type) {
1495 if (encoder_factory_ == NULL) {
1496 return NULL;
1497 }
1498 return encoder_factory_->CreateVideoEncoder(type);
1499}
1500
1501void WebRtcVideoEngine::DestroyExternalEncoder(webrtc::VideoEncoder* encoder) {
1502 ASSERT(encoder_factory_ != NULL);
1503 if (encoder_factory_ == NULL)
1504 return;
1505 encoder_factory_->DestroyVideoEncoder(encoder);
1506}
1507
1508bool WebRtcVideoEngine::IsExternalEncoderCodecType(
1509 webrtc::VideoCodecType type) const {
1510 if (!encoder_factory_)
1511 return false;
1512 const std::vector<WebRtcVideoEncoderFactory::VideoCodec>& codecs =
1513 encoder_factory_->codecs();
1514 std::vector<WebRtcVideoEncoderFactory::VideoCodec>::const_iterator it;
1515 for (it = codecs.begin(); it != codecs.end(); ++it) {
1516 if (it->type == type)
1517 return true;
1518 }
1519 return false;
1520}
1521
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001522void WebRtcVideoEngine::SetExternalDecoderFactory(
1523 WebRtcVideoDecoderFactory* decoder_factory) {
1524 decoder_factory_ = decoder_factory;
1525}
1526
1527void WebRtcVideoEngine::SetExternalEncoderFactory(
1528 WebRtcVideoEncoderFactory* encoder_factory) {
1529 if (encoder_factory_ == encoder_factory)
1530 return;
1531
1532 if (encoder_factory_) {
1533 encoder_factory_->RemoveObserver(this);
1534 }
1535 encoder_factory_ = encoder_factory;
1536 if (encoder_factory_) {
1537 encoder_factory_->AddObserver(this);
1538 }
1539
1540 // Invoke OnCodecAvailable() here in case the list of codecs is already
1541 // available when the encoder factory is installed. If not the encoder
1542 // factory will invoke the callback later when the codecs become available.
1543 OnCodecsAvailable();
1544}
1545
1546void WebRtcVideoEngine::OnCodecsAvailable() {
1547 // Rebuild codec list while reapplying the current default codec format.
1548 VideoCodec max_codec(kVideoCodecPrefs[0].payload_type,
1549 kVideoCodecPrefs[0].name,
1550 video_codecs_[0].width,
1551 video_codecs_[0].height,
1552 video_codecs_[0].framerate,
1553 0);
1554 if (!RebuildCodecList(max_codec)) {
1555 LOG(LS_ERROR) << "Failed to initialize list of supported codec types";
1556 }
1557}
1558
1559// WebRtcVideoMediaChannel
1560
1561WebRtcVideoMediaChannel::WebRtcVideoMediaChannel(
1562 WebRtcVideoEngine* engine,
1563 VoiceMediaChannel* channel)
1564 : engine_(engine),
1565 voice_channel_(channel),
1566 vie_channel_(-1),
1567 nack_enabled_(true),
1568 remb_enabled_(false),
1569 render_started_(false),
1570 first_receive_ssrc_(0),
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001571 num_unsignalled_recv_channels_(0),
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00001572 send_rtx_type_(-1),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001573 send_red_type_(-1),
1574 send_fec_type_(-1),
1575 send_min_bitrate_(kMinVideoBitrate),
1576 send_start_bitrate_(kStartVideoBitrate),
1577 send_max_bitrate_(kMaxVideoBitrate),
1578 sending_(false),
1579 ratio_w_(0),
1580 ratio_h_(0) {
1581 engine->RegisterChannel(this);
1582}
1583
1584bool WebRtcVideoMediaChannel::Init() {
1585 const uint32 ssrc_key = 0;
1586 return CreateChannel(ssrc_key, MD_SENDRECV, &vie_channel_);
1587}
1588
1589WebRtcVideoMediaChannel::~WebRtcVideoMediaChannel() {
1590 const bool send = false;
1591 SetSend(send);
1592 const bool render = false;
1593 SetRender(render);
1594
1595 while (!send_channels_.empty()) {
1596 if (!DeleteSendChannel(send_channels_.begin()->first)) {
1597 LOG(LS_ERROR) << "Unable to delete channel with ssrc key "
1598 << send_channels_.begin()->first;
1599 ASSERT(false);
1600 break;
1601 }
1602 }
1603
1604 // Remove all receive streams and the default channel.
1605 while (!recv_channels_.empty()) {
henrike@webrtc.org6e3dbc22014-03-25 17:09:47 +00001606 RemoveRecvStreamInternal(recv_channels_.begin()->first);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001607 }
1608
1609 // Unregister the channel from the engine.
1610 engine()->UnregisterChannel(this);
1611 if (worker_thread()) {
1612 worker_thread()->Clear(this);
1613 }
1614}
1615
1616bool WebRtcVideoMediaChannel::SetRecvCodecs(
1617 const std::vector<VideoCodec>& codecs) {
1618 receive_codecs_.clear();
1619 for (std::vector<VideoCodec>::const_iterator iter = codecs.begin();
1620 iter != codecs.end(); ++iter) {
1621 if (engine()->FindCodec(*iter)) {
1622 webrtc::VideoCodec wcodec;
1623 if (engine()->ConvertFromCricketVideoCodec(*iter, &wcodec)) {
1624 receive_codecs_.push_back(wcodec);
1625 }
1626 } else {
1627 LOG(LS_INFO) << "Unknown codec " << iter->name;
1628 return false;
1629 }
1630 }
1631
1632 for (RecvChannelMap::iterator it = recv_channels_.begin();
1633 it != recv_channels_.end(); ++it) {
1634 if (!SetReceiveCodecs(it->second))
1635 return false;
1636 }
1637 return true;
1638}
1639
1640bool WebRtcVideoMediaChannel::SetSendCodecs(
1641 const std::vector<VideoCodec>& codecs) {
1642 // Match with local video codec list.
1643 std::vector<webrtc::VideoCodec> send_codecs;
1644 VideoCodec checked_codec;
1645 VideoCodec current; // defaults to 0x0
1646 if (sending_) {
1647 ConvertToCricketVideoCodec(*send_codec_, &current);
1648 }
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00001649 std::map<int, int> primary_rtx_pt_mapping;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001650 for (std::vector<VideoCodec>::const_iterator iter = codecs.begin();
1651 iter != codecs.end(); ++iter) {
1652 if (_stricmp(iter->name.c_str(), kRedPayloadName) == 0) {
1653 send_red_type_ = iter->id;
1654 } else if (_stricmp(iter->name.c_str(), kFecPayloadName) == 0) {
1655 send_fec_type_ = iter->id;
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00001656 } else if (_stricmp(iter->name.c_str(), kRtxCodecName) == 0) {
1657 int rtx_type = iter->id;
1658 int rtx_primary_type = -1;
1659 if (iter->GetParam(kCodecParamAssociatedPayloadType, &rtx_primary_type)) {
1660 primary_rtx_pt_mapping[rtx_primary_type] = rtx_type;
1661 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001662 } else if (engine()->CanSendCodec(*iter, current, &checked_codec)) {
1663 webrtc::VideoCodec wcodec;
1664 if (engine()->ConvertFromCricketVideoCodec(checked_codec, &wcodec)) {
1665 if (send_codecs.empty()) {
1666 nack_enabled_ = IsNackEnabled(checked_codec);
1667 remb_enabled_ = IsRembEnabled(checked_codec);
1668 }
1669 send_codecs.push_back(wcodec);
1670 }
1671 } else {
1672 LOG(LS_WARNING) << "Unknown codec " << iter->name;
1673 }
1674 }
1675
1676 // Fail if we don't have a match.
1677 if (send_codecs.empty()) {
1678 LOG(LS_WARNING) << "No matching codecs available";
1679 return false;
1680 }
1681
1682 // Recv protection.
1683 for (RecvChannelMap::iterator it = recv_channels_.begin();
1684 it != recv_channels_.end(); ++it) {
1685 int channel_id = it->second->channel_id();
1686 if (!SetNackFec(channel_id, send_red_type_, send_fec_type_,
1687 nack_enabled_)) {
1688 return false;
1689 }
1690 if (engine_->vie()->rtp()->SetRembStatus(channel_id,
1691 kNotSending,
1692 remb_enabled_) != 0) {
1693 LOG_RTCERR3(SetRembStatus, channel_id, kNotSending, remb_enabled_);
1694 return false;
1695 }
1696 }
1697
1698 // Send settings.
1699 for (SendChannelMap::iterator iter = send_channels_.begin();
1700 iter != send_channels_.end(); ++iter) {
1701 int channel_id = iter->second->channel_id();
1702 if (!SetNackFec(channel_id, send_red_type_, send_fec_type_,
1703 nack_enabled_)) {
1704 return false;
1705 }
1706 if (engine_->vie()->rtp()->SetRembStatus(channel_id,
1707 remb_enabled_,
1708 remb_enabled_) != 0) {
1709 LOG_RTCERR3(SetRembStatus, channel_id, remb_enabled_, remb_enabled_);
1710 return false;
1711 }
1712 }
1713
1714 // Select the first matched codec.
1715 webrtc::VideoCodec& codec(send_codecs[0]);
1716
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00001717 // Set RTX payload type if primary now active. This value will be used in
1718 // SetSendCodec.
1719 std::map<int, int>::const_iterator rtx_it =
1720 primary_rtx_pt_mapping.find(static_cast<int>(codec.plType));
1721 if (rtx_it != primary_rtx_pt_mapping.end()) {
1722 send_rtx_type_ = rtx_it->second;
1723 }
1724
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001725 if (!SetSendCodec(
1726 codec, codec.minBitrate, codec.startBitrate, codec.maxBitrate)) {
1727 return false;
1728 }
1729
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001730 LogSendCodecChange("SetSendCodecs()");
1731
1732 return true;
1733}
1734
1735bool WebRtcVideoMediaChannel::GetSendCodec(VideoCodec* send_codec) {
1736 if (!send_codec_) {
1737 return false;
1738 }
1739 ConvertToCricketVideoCodec(*send_codec_, send_codec);
1740 return true;
1741}
1742
1743bool WebRtcVideoMediaChannel::SetSendStreamFormat(uint32 ssrc,
1744 const VideoFormat& format) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001745 WebRtcVideoChannelSendInfo* send_channel = GetSendChannel(ssrc);
1746 if (!send_channel) {
1747 LOG(LS_ERROR) << "The specified ssrc " << ssrc << " is not in use.";
1748 return false;
1749 }
1750 send_channel->set_video_format(format);
1751 return true;
1752}
1753
1754bool WebRtcVideoMediaChannel::SetRender(bool render) {
1755 if (render == render_started_) {
1756 return true; // no action required
1757 }
1758
1759 bool ret = true;
1760 for (RecvChannelMap::iterator it = recv_channels_.begin();
1761 it != recv_channels_.end(); ++it) {
1762 if (render) {
1763 if (engine()->vie()->render()->StartRender(
1764 it->second->channel_id()) != 0) {
1765 LOG_RTCERR1(StartRender, it->second->channel_id());
1766 ret = false;
1767 }
1768 } else {
1769 if (engine()->vie()->render()->StopRender(
1770 it->second->channel_id()) != 0) {
1771 LOG_RTCERR1(StopRender, it->second->channel_id());
1772 ret = false;
1773 }
1774 }
1775 }
1776 if (ret) {
1777 render_started_ = render;
1778 }
1779
1780 return ret;
1781}
1782
1783bool WebRtcVideoMediaChannel::SetSend(bool send) {
1784 if (!HasReadySendChannels() && send) {
1785 LOG(LS_ERROR) << "No stream added";
1786 return false;
1787 }
1788 if (send == sending()) {
1789 return true; // No action required.
1790 }
1791
1792 if (send) {
1793 // We've been asked to start sending.
1794 // SetSendCodecs must have been called already.
1795 if (!send_codec_) {
1796 return false;
1797 }
1798 // Start send now.
1799 if (!StartSend()) {
1800 return false;
1801 }
1802 } else {
1803 // We've been asked to stop sending.
1804 if (!StopSend()) {
1805 return false;
1806 }
1807 }
1808 sending_ = send;
1809
1810 return true;
1811}
1812
1813bool WebRtcVideoMediaChannel::AddSendStream(const StreamParams& sp) {
henrike@webrtc.org6e3dbc22014-03-25 17:09:47 +00001814 if (sp.first_ssrc() == 0) {
1815 LOG(LS_ERROR) << "AddSendStream with 0 ssrc is not supported.";
1816 return false;
1817 }
1818
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001819 LOG(LS_INFO) << "AddSendStream " << sp.ToString();
1820
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00001821 if (!IsOneSsrcStream(sp) && !IsSimulcastStream(sp)) {
1822 LOG(LS_ERROR) << "AddSendStream: bad local stream parameters";
1823 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001824 }
1825
1826 uint32 ssrc_key;
1827 if (!CreateSendChannelKey(sp.first_ssrc(), &ssrc_key)) {
1828 LOG(LS_ERROR) << "Trying to register duplicate ssrc: " << sp.first_ssrc();
1829 return false;
1830 }
1831 // If the default channel is already used for sending create a new channel
1832 // otherwise use the default channel for sending.
1833 int channel_id = -1;
1834 if (send_channels_[0]->stream_params() == NULL) {
1835 channel_id = vie_channel_;
1836 } else {
1837 if (!CreateChannel(ssrc_key, MD_SEND, &channel_id)) {
1838 LOG(LS_ERROR) << "AddSendStream: unable to create channel";
1839 return false;
1840 }
1841 }
1842 WebRtcVideoChannelSendInfo* send_channel = send_channels_[ssrc_key];
1843 // Set the send (local) SSRC.
1844 // If there are multiple send SSRCs, we can only set the first one here, and
1845 // the rest of the SSRC(s) need to be set after SetSendCodec has been called
1846 // (with a codec requires multiple SSRC(s)).
1847 if (engine()->vie()->rtp()->SetLocalSSRC(channel_id,
1848 sp.first_ssrc()) != 0) {
1849 LOG_RTCERR2(SetLocalSSRC, channel_id, sp.first_ssrc());
1850 return false;
1851 }
1852
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00001853 // Set the corresponding RTX SSRC.
1854 if (!SetLocalRtxSsrc(channel_id, sp, sp.first_ssrc(), 0)) {
1855 return false;
1856 }
1857
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001858 // Set RTCP CName.
1859 if (engine()->vie()->rtp()->SetRTCPCName(channel_id,
1860 sp.cname.c_str()) != 0) {
1861 LOG_RTCERR2(SetRTCPCName, channel_id, sp.cname.c_str());
1862 return false;
1863 }
1864
1865 // At this point the channel's local SSRC has been updated. If the channel is
1866 // the default channel make sure that all the receive channels are updated as
1867 // well. Receive channels have to have the same SSRC as the default channel in
1868 // order to send receiver reports with this SSRC.
1869 if (IsDefaultChannel(channel_id)) {
1870 for (RecvChannelMap::const_iterator it = recv_channels_.begin();
1871 it != recv_channels_.end(); ++it) {
1872 WebRtcVideoChannelRecvInfo* info = it->second;
1873 int channel_id = info->channel_id();
1874 if (engine()->vie()->rtp()->SetLocalSSRC(channel_id,
1875 sp.first_ssrc()) != 0) {
1876 LOG_RTCERR1(SetLocalSSRC, it->first);
1877 return false;
1878 }
1879 }
1880 }
1881
1882 send_channel->set_stream_params(sp);
1883
1884 // Reset send codec after stream parameters changed.
1885 if (send_codec_) {
1886 if (!SetSendCodec(send_channel, *send_codec_, send_min_bitrate_,
1887 send_start_bitrate_, send_max_bitrate_)) {
1888 return false;
1889 }
1890 LogSendCodecChange("SetSendStreamFormat()");
1891 }
1892
1893 if (sending_) {
1894 return StartSend(send_channel);
1895 }
1896 return true;
1897}
1898
1899bool WebRtcVideoMediaChannel::RemoveSendStream(uint32 ssrc) {
henrike@webrtc.org6e3dbc22014-03-25 17:09:47 +00001900 if (ssrc == 0) {
1901 LOG(LS_ERROR) << "RemoveSendStream with 0 ssrc is not supported.";
1902 return false;
1903 }
1904
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001905 uint32 ssrc_key;
1906 if (!GetSendChannelKey(ssrc, &ssrc_key)) {
1907 LOG(LS_WARNING) << "Try to remove stream with ssrc " << ssrc
1908 << " which doesn't exist.";
1909 return false;
1910 }
1911 WebRtcVideoChannelSendInfo* send_channel = send_channels_[ssrc_key];
1912 int channel_id = send_channel->channel_id();
1913 if (IsDefaultChannel(channel_id) && (send_channel->stream_params() == NULL)) {
1914 // Default channel will still exist. However, if stream_params() is NULL
1915 // there is no stream to remove.
1916 return false;
1917 }
1918 if (sending_) {
1919 StopSend(send_channel);
1920 }
1921
1922 const WebRtcVideoChannelSendInfo::EncoderMap& encoder_map =
1923 send_channel->registered_encoders();
1924 for (WebRtcVideoChannelSendInfo::EncoderMap::const_iterator it =
1925 encoder_map.begin(); it != encoder_map.end(); ++it) {
1926 if (engine()->vie()->ext_codec()->DeRegisterExternalSendCodec(
1927 channel_id, it->first) != 0) {
1928 LOG_RTCERR1(DeregisterEncoderObserver, channel_id);
1929 }
1930 engine()->DestroyExternalEncoder(it->second);
1931 }
1932 send_channel->ClearRegisteredEncoders();
1933
1934 // The receive channels depend on the default channel, recycle it instead.
1935 if (IsDefaultChannel(channel_id)) {
1936 SetCapturer(GetDefaultChannelSsrc(), NULL);
1937 send_channel->ClearStreamParams();
1938 } else {
1939 return DeleteSendChannel(ssrc_key);
1940 }
1941 return true;
1942}
1943
1944bool WebRtcVideoMediaChannel::AddRecvStream(const StreamParams& sp) {
henrike@webrtc.org6e3dbc22014-03-25 17:09:47 +00001945 if (sp.first_ssrc() == 0) {
1946 LOG(LS_ERROR) << "AddRecvStream with 0 ssrc is not supported.";
1947 return false;
1948 }
1949
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001950 // TODO(zhurunz) Remove this once BWE works properly across different send
1951 // and receive channels.
1952 // Reuse default channel for recv stream in 1:1 call.
1953 if (!InConferenceMode() && first_receive_ssrc_ == 0) {
1954 LOG(LS_INFO) << "Recv stream " << sp.first_ssrc()
1955 << " reuse default channel #"
1956 << vie_channel_;
1957 first_receive_ssrc_ = sp.first_ssrc();
1958 if (render_started_) {
1959 if (engine()->vie()->render()->StartRender(vie_channel_) !=0) {
1960 LOG_RTCERR1(StartRender, vie_channel_);
1961 }
1962 }
1963 return true;
1964 }
1965
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001966 int channel_id = -1;
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001967 RecvChannelMap::iterator channel_iterator =
1968 recv_channels_.find(sp.first_ssrc());
1969 if (channel_iterator == recv_channels_.end() &&
1970 first_receive_ssrc_ != sp.first_ssrc()) {
1971 // TODO(perkj): Implement recv media from multiple media SSRCs per stream.
1972 // NOTE: We have two SSRCs per stream when RTX is enabled.
1973 if (!IsOneSsrcStream(sp)) {
1974 LOG(LS_ERROR) << "WebRtcVideoMediaChannel supports one primary SSRC per"
1975 << " stream and one FID SSRC per primary SSRC.";
1976 return false;
1977 }
1978
1979 // Create a new channel for receiving video data.
1980 // In order to get the bandwidth estimation work fine for
1981 // receive only channels, we connect all receiving channels
1982 // to our master send channel.
1983 if (!CreateChannel(sp.first_ssrc(), MD_RECV, &channel_id)) {
1984 return false;
1985 }
1986 } else {
1987 // Already exists.
1988 if (first_receive_ssrc_ == sp.first_ssrc()) {
1989 return false;
1990 }
1991 // Early receive added channel.
1992 channel_id = (*channel_iterator).second->channel_id();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001993 }
1994
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00001995 // Set the corresponding RTX SSRC.
1996 uint32 rtx_ssrc;
1997 bool has_rtx = sp.GetFidSsrc(sp.first_ssrc(), &rtx_ssrc);
1998 if (has_rtx && engine()->vie()->rtp()->SetRemoteSSRCType(
1999 channel_id, webrtc::kViEStreamTypeRtx, rtx_ssrc) != 0) {
2000 LOG_RTCERR3(SetRemoteSSRCType, channel_id, webrtc::kViEStreamTypeRtx,
2001 rtx_ssrc);
2002 return false;
2003 }
2004
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002005 // Get the default renderer.
2006 VideoRenderer* default_renderer = NULL;
2007 if (InConferenceMode()) {
2008 // The recv_channels_ size start out being 1, so if it is two here this
2009 // is the first receive channel created (vie_channel_ is not used for
2010 // receiving in a conference call). This means that the renderer stored
2011 // inside vie_channel_ should be used for the just created channel.
2012 if (recv_channels_.size() == 2 &&
2013 recv_channels_.find(0) != recv_channels_.end()) {
2014 GetRenderer(0, &default_renderer);
2015 }
2016 }
2017
2018 // The first recv stream reuses the default renderer (if a default renderer
2019 // has been set).
2020 if (default_renderer) {
2021 SetRenderer(sp.first_ssrc(), default_renderer);
2022 }
2023
2024 LOG(LS_INFO) << "New video stream " << sp.first_ssrc()
2025 << " registered to VideoEngine channel #"
2026 << channel_id << " and connected to channel #" << vie_channel_;
2027
2028 return true;
2029}
2030
2031bool WebRtcVideoMediaChannel::RemoveRecvStream(uint32 ssrc) {
henrike@webrtc.org6e3dbc22014-03-25 17:09:47 +00002032 if (ssrc == 0) {
2033 LOG(LS_ERROR) << "RemoveRecvStream with 0 ssrc is not supported.";
2034 return false;
2035 }
2036 return RemoveRecvStreamInternal(ssrc);
2037}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002038
henrike@webrtc.org6e3dbc22014-03-25 17:09:47 +00002039bool WebRtcVideoMediaChannel::RemoveRecvStreamInternal(uint32 ssrc) {
2040 RecvChannelMap::iterator it = recv_channels_.find(ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002041 if (it == recv_channels_.end()) {
2042 // TODO(perkj): Remove this once BWE works properly across different send
2043 // and receive channels.
2044 // The default channel is reused for recv stream in 1:1 call.
2045 if (first_receive_ssrc_ == ssrc) {
2046 first_receive_ssrc_ = 0;
2047 // Need to stop the renderer and remove it since the render window can be
2048 // deleted after this.
2049 if (render_started_) {
2050 if (engine()->vie()->render()->StopRender(vie_channel_) !=0) {
2051 LOG_RTCERR1(StopRender, it->second->channel_id());
2052 }
2053 }
2054 recv_channels_[0]->SetRenderer(NULL);
2055 return true;
2056 }
2057 return false;
2058 }
2059 WebRtcVideoChannelRecvInfo* info = it->second;
2060 int channel_id = info->channel_id();
2061 if (engine()->vie()->render()->RemoveRenderer(channel_id) != 0) {
2062 LOG_RTCERR1(RemoveRenderer, channel_id);
2063 }
2064
2065 if (engine()->vie()->network()->DeregisterSendTransport(channel_id) !=0) {
2066 LOG_RTCERR1(DeRegisterSendTransport, channel_id);
2067 }
2068
2069 if (engine()->vie()->codec()->DeregisterDecoderObserver(
2070 channel_id) != 0) {
2071 LOG_RTCERR1(DeregisterDecoderObserver, channel_id);
2072 }
2073
2074 const WebRtcVideoChannelRecvInfo::DecoderMap& decoder_map =
2075 info->registered_decoders();
2076 for (WebRtcVideoChannelRecvInfo::DecoderMap::const_iterator it =
2077 decoder_map.begin(); it != decoder_map.end(); ++it) {
2078 if (engine()->vie()->ext_codec()->DeRegisterExternalReceiveCodec(
2079 channel_id, it->first) != 0) {
2080 LOG_RTCERR1(DeregisterDecoderObserver, channel_id);
2081 }
2082 engine()->DestroyExternalDecoder(it->second);
2083 }
2084 info->ClearRegisteredDecoders();
2085
2086 LOG(LS_INFO) << "Removing video stream " << ssrc
2087 << " with VideoEngine channel #"
2088 << channel_id;
wu@webrtc.org9caf2762013-12-11 18:25:07 +00002089 bool ret = true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002090 if (engine()->vie()->base()->DeleteChannel(channel_id) == -1) {
2091 LOG_RTCERR1(DeleteChannel, channel_id);
wu@webrtc.org9caf2762013-12-11 18:25:07 +00002092 ret = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002093 }
2094 // Delete the WebRtcVideoChannelRecvInfo pointed to by it->second.
2095 delete info;
2096 recv_channels_.erase(it);
wu@webrtc.org9caf2762013-12-11 18:25:07 +00002097 return ret;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002098}
2099
2100bool WebRtcVideoMediaChannel::StartSend() {
2101 bool success = true;
2102 for (SendChannelMap::iterator iter = send_channels_.begin();
2103 iter != send_channels_.end(); ++iter) {
2104 WebRtcVideoChannelSendInfo* send_channel = iter->second;
2105 if (!StartSend(send_channel)) {
2106 success = false;
2107 }
2108 }
2109 return success;
2110}
2111
2112bool WebRtcVideoMediaChannel::StartSend(
2113 WebRtcVideoChannelSendInfo* send_channel) {
2114 const int channel_id = send_channel->channel_id();
2115 if (engine()->vie()->base()->StartSend(channel_id) != 0) {
2116 LOG_RTCERR1(StartSend, channel_id);
2117 return false;
2118 }
2119
2120 send_channel->set_sending(true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002121 return true;
2122}
2123
2124bool WebRtcVideoMediaChannel::StopSend() {
2125 bool success = true;
2126 for (SendChannelMap::iterator iter = send_channels_.begin();
2127 iter != send_channels_.end(); ++iter) {
2128 WebRtcVideoChannelSendInfo* send_channel = iter->second;
2129 if (!StopSend(send_channel)) {
2130 success = false;
2131 }
2132 }
2133 return success;
2134}
2135
2136bool WebRtcVideoMediaChannel::StopSend(
2137 WebRtcVideoChannelSendInfo* send_channel) {
2138 const int channel_id = send_channel->channel_id();
2139 if (engine()->vie()->base()->StopSend(channel_id) != 0) {
2140 LOG_RTCERR1(StopSend, channel_id);
2141 return false;
2142 }
2143 send_channel->set_sending(false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002144 return true;
2145}
2146
2147bool WebRtcVideoMediaChannel::SendIntraFrame() {
2148 bool success = true;
2149 for (SendChannelMap::iterator iter = send_channels_.begin();
2150 iter != send_channels_.end();
2151 ++iter) {
2152 WebRtcVideoChannelSendInfo* send_channel = iter->second;
2153 const int channel_id = send_channel->channel_id();
2154 if (engine()->vie()->codec()->SendKeyFrame(channel_id) != 0) {
2155 LOG_RTCERR1(SendKeyFrame, channel_id);
2156 success = false;
2157 }
2158 }
2159 return success;
2160}
2161
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002162bool WebRtcVideoMediaChannel::HasReadySendChannels() {
2163 return !send_channels_.empty() &&
2164 ((send_channels_.size() > 1) ||
2165 (send_channels_[0]->stream_params() != NULL));
2166}
2167
2168bool WebRtcVideoMediaChannel::GetSendChannelKey(uint32 local_ssrc,
2169 uint32* key) {
2170 *key = 0;
2171 // If a send channel is not ready to send it will not have local_ssrc
2172 // registered to it.
2173 if (!HasReadySendChannels()) {
2174 return false;
2175 }
2176 // The default channel is stored with key 0. The key therefore does not match
2177 // the SSRC associated with the default channel. Check if the SSRC provided
2178 // corresponds to the default channel's SSRC.
2179 if (local_ssrc == GetDefaultChannelSsrc()) {
2180 return true;
2181 }
2182 if (send_channels_.find(local_ssrc) == send_channels_.end()) {
2183 for (SendChannelMap::iterator iter = send_channels_.begin();
2184 iter != send_channels_.end(); ++iter) {
2185 WebRtcVideoChannelSendInfo* send_channel = iter->second;
2186 if (send_channel->has_ssrc(local_ssrc)) {
2187 *key = iter->first;
2188 return true;
2189 }
2190 }
2191 return false;
2192 }
2193 // The key was found in the above std::map::find call. This means that the
2194 // ssrc is the key.
2195 *key = local_ssrc;
2196 return true;
2197}
2198
2199WebRtcVideoChannelSendInfo* WebRtcVideoMediaChannel::GetSendChannel(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002200 uint32 local_ssrc) {
2201 uint32 key;
2202 if (!GetSendChannelKey(local_ssrc, &key)) {
2203 return NULL;
2204 }
2205 return send_channels_[key];
2206}
2207
2208bool WebRtcVideoMediaChannel::CreateSendChannelKey(uint32 local_ssrc,
2209 uint32* key) {
2210 if (GetSendChannelKey(local_ssrc, key)) {
2211 // If there is a key corresponding to |local_ssrc|, the SSRC is already in
2212 // use. SSRCs need to be unique in a session and at this point a duplicate
2213 // SSRC has been detected.
2214 return false;
2215 }
2216 if (send_channels_[0]->stream_params() == NULL) {
2217 // key should be 0 here as the default channel should be re-used whenever it
2218 // is not used.
2219 *key = 0;
2220 return true;
2221 }
2222 // SSRC is currently not in use and the default channel is already in use. Use
2223 // the SSRC as key since it is supposed to be unique in a session.
2224 *key = local_ssrc;
2225 return true;
2226}
2227
wu@webrtc.org24301a62013-12-13 19:17:43 +00002228int WebRtcVideoMediaChannel::GetSendChannelNum(VideoCapturer* capturer) {
2229 int num = 0;
2230 for (SendChannelMap::iterator iter = send_channels_.begin();
2231 iter != send_channels_.end(); ++iter) {
2232 WebRtcVideoChannelSendInfo* send_channel = iter->second;
2233 if (send_channel->video_capturer() == capturer) {
2234 ++num;
2235 }
2236 }
2237 return num;
2238}
2239
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002240uint32 WebRtcVideoMediaChannel::GetDefaultChannelSsrc() {
2241 WebRtcVideoChannelSendInfo* send_channel = send_channels_[0];
2242 const StreamParams* sp = send_channel->stream_params();
2243 if (sp == NULL) {
2244 // This happens if no send stream is currently registered.
2245 return 0;
2246 }
2247 return sp->first_ssrc();
2248}
2249
2250bool WebRtcVideoMediaChannel::DeleteSendChannel(uint32 ssrc_key) {
2251 if (send_channels_.find(ssrc_key) == send_channels_.end()) {
2252 return false;
2253 }
2254 WebRtcVideoChannelSendInfo* send_channel = send_channels_[ssrc_key];
wu@webrtc.org24301a62013-12-13 19:17:43 +00002255 MaybeDisconnectCapturer(send_channel->video_capturer());
henrike@webrtc.orgc7bec842014-03-12 19:53:43 +00002256 send_channel->set_video_capturer(NULL, engine()->vie());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002257
2258 int channel_id = send_channel->channel_id();
2259 int capture_id = send_channel->capture_id();
2260 if (engine()->vie()->codec()->DeregisterEncoderObserver(
2261 channel_id) != 0) {
2262 LOG_RTCERR1(DeregisterEncoderObserver, channel_id);
2263 }
2264
2265 // Destroy the external capture interface.
2266 if (engine()->vie()->capture()->DisconnectCaptureDevice(
2267 channel_id) != 0) {
2268 LOG_RTCERR1(DisconnectCaptureDevice, channel_id);
2269 }
2270 if (engine()->vie()->capture()->ReleaseCaptureDevice(
2271 capture_id) != 0) {
2272 LOG_RTCERR1(ReleaseCaptureDevice, capture_id);
2273 }
2274
2275 // The default channel is stored in both |send_channels_| and
2276 // |recv_channels_|. To make sure it is only deleted once from vie let the
2277 // delete call happen when tearing down |recv_channels_| and not here.
2278 if (!IsDefaultChannel(channel_id)) {
2279 engine_->vie()->base()->DeleteChannel(channel_id);
2280 }
2281 delete send_channel;
2282 send_channels_.erase(ssrc_key);
2283 return true;
2284}
2285
2286bool WebRtcVideoMediaChannel::RemoveCapturer(uint32 ssrc) {
2287 WebRtcVideoChannelSendInfo* send_channel = GetSendChannel(ssrc);
2288 if (!send_channel) {
2289 return false;
2290 }
2291 VideoCapturer* capturer = send_channel->video_capturer();
2292 if (capturer == NULL) {
2293 return false;
2294 }
wu@webrtc.org24301a62013-12-13 19:17:43 +00002295 MaybeDisconnectCapturer(capturer);
henrike@webrtc.orgc7bec842014-03-12 19:53:43 +00002296 send_channel->set_video_capturer(NULL, engine()->vie());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002297 const int64 timestamp = send_channel->local_stream_info()->time_stamp();
2298 if (send_codec_) {
2299 QueueBlackFrame(ssrc, timestamp, send_codec_->maxFramerate);
2300 }
2301 return true;
2302}
2303
2304bool WebRtcVideoMediaChannel::SetRenderer(uint32 ssrc,
2305 VideoRenderer* renderer) {
2306 if (recv_channels_.find(ssrc) == recv_channels_.end()) {
2307 // TODO(perkj): Remove this once BWE works properly across different send
2308 // and receive channels.
2309 // The default channel is reused for recv stream in 1:1 call.
2310 if (first_receive_ssrc_ == ssrc &&
2311 recv_channels_.find(0) != recv_channels_.end()) {
2312 LOG(LS_INFO) << "SetRenderer " << ssrc
2313 << " reuse default channel #"
2314 << vie_channel_;
2315 recv_channels_[0]->SetRenderer(renderer);
2316 return true;
2317 }
2318 return false;
2319 }
2320
2321 recv_channels_[ssrc]->SetRenderer(renderer);
2322 return true;
2323}
2324
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00002325bool WebRtcVideoMediaChannel::GetStats(const StatsOptions& options,
2326 VideoMediaInfo* info) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002327 // Get sender statistics and build VideoSenderInfo.
2328 unsigned int total_bitrate_sent = 0;
2329 unsigned int video_bitrate_sent = 0;
2330 unsigned int fec_bitrate_sent = 0;
2331 unsigned int nack_bitrate_sent = 0;
2332 unsigned int estimated_send_bandwidth = 0;
2333 unsigned int target_enc_bitrate = 0;
2334 if (send_codec_) {
2335 for (SendChannelMap::const_iterator iter = send_channels_.begin();
2336 iter != send_channels_.end(); ++iter) {
2337 WebRtcVideoChannelSendInfo* send_channel = iter->second;
2338 const int channel_id = send_channel->channel_id();
2339 VideoSenderInfo sinfo;
2340 const StreamParams* send_params = send_channel->stream_params();
2341 if (send_params == NULL) {
2342 // This should only happen if the default vie channel is not in use.
2343 // This can happen if no streams have ever been added or the stream
2344 // corresponding to the default channel has been removed. Note that
2345 // there may be non-default vie channels in use when this happen so
2346 // asserting send_channels_.size() == 1 is not correct and neither is
2347 // breaking out of the loop.
2348 ASSERT(channel_id == vie_channel_);
2349 continue;
2350 }
2351 unsigned int bytes_sent, packets_sent, bytes_recv, packets_recv;
2352 if (engine_->vie()->rtp()->GetRTPStatistics(channel_id, bytes_sent,
2353 packets_sent, bytes_recv,
2354 packets_recv) != 0) {
2355 LOG_RTCERR1(GetRTPStatistics, vie_channel_);
2356 continue;
2357 }
2358 WebRtcLocalStreamInfo* channel_stream_info =
2359 send_channel->local_stream_info();
2360
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002361 for (size_t i = 0; i < send_params->ssrcs.size(); ++i) {
2362 sinfo.add_ssrc(send_params->ssrcs[i]);
2363 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002364 sinfo.codec_name = send_codec_->plName;
2365 sinfo.bytes_sent = bytes_sent;
2366 sinfo.packets_sent = packets_sent;
2367 sinfo.packets_cached = -1;
2368 sinfo.packets_lost = -1;
2369 sinfo.fraction_lost = -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002370 sinfo.rtt_ms = -1;
wu@webrtc.orgf7d501d2014-03-27 23:48:25 +00002371 sinfo.frame_width = static_cast<int>(channel_stream_info->width());
2372 sinfo.frame_height = static_cast<int>(channel_stream_info->height());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002373 sinfo.framerate_input = channel_stream_info->framerate();
2374 sinfo.framerate_sent = send_channel->encoder_observer()->framerate();
2375 sinfo.nominal_bitrate = send_channel->encoder_observer()->bitrate();
2376 sinfo.preferred_bitrate = send_max_bitrate_;
2377 sinfo.adapt_reason = send_channel->CurrentAdaptReason();
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002378 sinfo.capture_jitter_ms = -1;
2379 sinfo.avg_encode_ms = -1;
wu@webrtc.org9caf2762013-12-11 18:25:07 +00002380 sinfo.encode_usage_percent = -1;
2381 sinfo.capture_queue_delay_ms_per_s = -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002382
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002383 int capture_jitter_ms = 0;
2384 int avg_encode_time_ms = 0;
wu@webrtc.org9caf2762013-12-11 18:25:07 +00002385 int encode_usage_percent = 0;
2386 int capture_queue_delay_ms_per_s = 0;
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002387 if (engine()->vie()->base()->CpuOveruseMeasures(
wu@webrtc.org9caf2762013-12-11 18:25:07 +00002388 channel_id,
2389 &capture_jitter_ms,
2390 &avg_encode_time_ms,
2391 &encode_usage_percent,
2392 &capture_queue_delay_ms_per_s) == 0) {
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002393 sinfo.capture_jitter_ms = capture_jitter_ms;
2394 sinfo.avg_encode_ms = avg_encode_time_ms;
wu@webrtc.org9caf2762013-12-11 18:25:07 +00002395 sinfo.encode_usage_percent = encode_usage_percent;
2396 sinfo.capture_queue_delay_ms_per_s = capture_queue_delay_ms_per_s;
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002397 }
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002398
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00002399#ifdef USE_WEBRTC_DEV_BRANCH
2400 webrtc::RtcpPacketTypeCounter rtcp_sent;
2401 webrtc::RtcpPacketTypeCounter rtcp_received;
2402 if (engine()->vie()->rtp()->GetRtcpPacketTypeCounters(
2403 channel_id, &rtcp_sent, &rtcp_received) == 0) {
2404 sinfo.firs_rcvd = rtcp_received.fir_packets;
2405 sinfo.plis_rcvd = rtcp_received.pli_packets;
2406 sinfo.nacks_rcvd = rtcp_received.nack_packets;
2407 } else {
2408 sinfo.firs_rcvd = -1;
2409 sinfo.plis_rcvd = -1;
2410 sinfo.nacks_rcvd = -1;
2411 LOG_RTCERR1(GetRtcpPacketTypeCounters, channel_id);
2412 }
2413#else
2414 sinfo.firs_rcvd = -1;
2415 sinfo.plis_rcvd = -1;
2416 sinfo.nacks_rcvd = -1;
2417#endif
2418
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002419 // Get received RTCP statistics for the sender (reported by the remote
2420 // client in a RTCP packet), if available.
2421 // It's not a fatal error if we can't, since RTCP may not have arrived
2422 // yet.
2423 webrtc::RtcpStatistics outgoing_stream_rtcp_stats;
2424 int outgoing_stream_rtt_ms;
2425
2426 if (engine_->vie()->rtp()->GetSendChannelRtcpStatistics(
2427 channel_id,
2428 outgoing_stream_rtcp_stats,
2429 outgoing_stream_rtt_ms) == 0) {
2430 // Convert Q8 to float.
2431 sinfo.packets_lost = outgoing_stream_rtcp_stats.cumulative_lost;
2432 sinfo.fraction_lost = static_cast<float>(
2433 outgoing_stream_rtcp_stats.fraction_lost) / (1 << 8);
2434 sinfo.rtt_ms = outgoing_stream_rtt_ms;
2435 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002436 info->senders.push_back(sinfo);
2437
2438 unsigned int channel_total_bitrate_sent = 0;
2439 unsigned int channel_video_bitrate_sent = 0;
2440 unsigned int channel_fec_bitrate_sent = 0;
2441 unsigned int channel_nack_bitrate_sent = 0;
2442 if (engine_->vie()->rtp()->GetBandwidthUsage(
2443 channel_id, channel_total_bitrate_sent, channel_video_bitrate_sent,
2444 channel_fec_bitrate_sent, channel_nack_bitrate_sent) == 0) {
2445 total_bitrate_sent += channel_total_bitrate_sent;
2446 video_bitrate_sent += channel_video_bitrate_sent;
2447 fec_bitrate_sent += channel_fec_bitrate_sent;
2448 nack_bitrate_sent += channel_nack_bitrate_sent;
2449 } else {
2450 LOG_RTCERR1(GetBandwidthUsage, channel_id);
2451 }
2452
2453 unsigned int estimated_stream_send_bandwidth = 0;
2454 if (engine_->vie()->rtp()->GetEstimatedSendBandwidth(
2455 channel_id, &estimated_stream_send_bandwidth) == 0) {
2456 estimated_send_bandwidth += estimated_stream_send_bandwidth;
2457 } else {
2458 LOG_RTCERR1(GetEstimatedSendBandwidth, channel_id);
2459 }
2460 unsigned int target_enc_stream_bitrate = 0;
2461 if (engine_->vie()->codec()->GetCodecTargetBitrate(
2462 channel_id, &target_enc_stream_bitrate) == 0) {
2463 target_enc_bitrate += target_enc_stream_bitrate;
2464 } else {
2465 LOG_RTCERR1(GetCodecTargetBitrate, channel_id);
2466 }
2467 }
2468 } else {
2469 LOG(LS_WARNING) << "GetStats: sender information not ready.";
2470 }
2471
2472 // Get the SSRC and stats for each receiver, based on our own calculations.
2473 unsigned int estimated_recv_bandwidth = 0;
2474 for (RecvChannelMap::const_iterator it = recv_channels_.begin();
2475 it != recv_channels_.end(); ++it) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002476 WebRtcVideoChannelRecvInfo* channel = it->second;
2477
2478 unsigned int ssrc;
2479 // Get receiver statistics and build VideoReceiverInfo, if we have data.
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +00002480 // Skip the default channel (ssrc == 0).
2481 if (engine_->vie()->rtp()->GetRemoteSSRC(
2482 channel->channel_id(), ssrc) != 0 ||
2483 ssrc == 0)
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002484 continue;
2485
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002486 webrtc::StreamDataCounters sent;
2487 webrtc::StreamDataCounters received;
2488 if (engine_->vie()->rtp()->GetRtpStatistics(channel->channel_id(),
2489 sent, received) != 0) {
2490 LOG_RTCERR1(GetRTPStatistics, channel->channel_id());
2491 return false;
2492 }
2493 VideoReceiverInfo rinfo;
2494 rinfo.add_ssrc(ssrc);
2495 rinfo.bytes_rcvd = received.bytes;
2496 rinfo.packets_rcvd = received.packets;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002497 rinfo.packets_lost = -1;
2498 rinfo.packets_concealed = -1;
2499 rinfo.fraction_lost = -1; // from SentRTCP
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002500 rinfo.frame_width = channel->render_adapter()->width();
2501 rinfo.frame_height = channel->render_adapter()->height();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002502 int fps = channel->render_adapter()->framerate();
2503 rinfo.framerate_decoded = fps;
2504 rinfo.framerate_output = fps;
wu@webrtc.org97077a32013-10-25 21:18:33 +00002505 channel->decoder_observer()->ExportTo(&rinfo);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002506
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00002507#ifdef USE_WEBRTC_DEV_BRANCH
2508 webrtc::RtcpPacketTypeCounter rtcp_sent;
2509 webrtc::RtcpPacketTypeCounter rtcp_received;
2510 if (engine()->vie()->rtp()->GetRtcpPacketTypeCounters(
2511 channel->channel_id(), &rtcp_sent, &rtcp_received) == 0) {
2512 rinfo.firs_sent = rtcp_sent.fir_packets;
2513 rinfo.plis_sent = rtcp_sent.pli_packets;
2514 rinfo.nacks_sent = rtcp_sent.nack_packets;
2515 } else {
2516 rinfo.firs_sent = -1;
2517 rinfo.plis_sent = -1;
2518 rinfo.nacks_sent = -1;
2519 LOG_RTCERR1(GetRtcpPacketTypeCounters, channel->channel_id());
2520 }
2521#else
2522 rinfo.firs_sent = -1;
2523 rinfo.plis_sent = -1;
2524 rinfo.nacks_sent = -1;
2525#endif
2526
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002527 // Get our locally created statistics of the received RTP stream.
2528 webrtc::RtcpStatistics incoming_stream_rtcp_stats;
2529 int incoming_stream_rtt_ms;
2530 if (engine_->vie()->rtp()->GetReceiveChannelRtcpStatistics(
2531 channel->channel_id(),
2532 incoming_stream_rtcp_stats,
2533 incoming_stream_rtt_ms) == 0) {
2534 // Convert Q8 to float.
2535 rinfo.packets_lost = incoming_stream_rtcp_stats.cumulative_lost;
2536 rinfo.fraction_lost = static_cast<float>(
2537 incoming_stream_rtcp_stats.fraction_lost) / (1 << 8);
2538 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002539 info->receivers.push_back(rinfo);
2540
2541 unsigned int estimated_recv_stream_bandwidth = 0;
2542 if (engine_->vie()->rtp()->GetEstimatedReceiveBandwidth(
2543 channel->channel_id(), &estimated_recv_stream_bandwidth) == 0) {
2544 estimated_recv_bandwidth += estimated_recv_stream_bandwidth;
2545 } else {
2546 LOG_RTCERR1(GetEstimatedReceiveBandwidth, channel->channel_id());
2547 }
2548 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002549 // Build BandwidthEstimationInfo.
2550 // TODO(zhurunz): Add real unittest for this.
2551 BandwidthEstimationInfo bwe;
2552
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00002553 // TODO(jiayl): remove the condition when the necessary changes are available
2554 // outside the dev branch.
2555#ifdef USE_WEBRTC_DEV_BRANCH
2556 if (options.include_received_propagation_stats) {
2557 webrtc::ReceiveBandwidthEstimatorStats additional_stats;
2558 // Only call for the default channel because the returned stats are
2559 // collected for all the channels using the same estimator.
2560 if (engine_->vie()->rtp()->GetReceiveBandwidthEstimatorStats(
mallinath@webrtc.org92fdfeb2014-02-17 18:49:41 +00002561 recv_channels_[0]->channel_id(), &additional_stats) == 0) {
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00002562 bwe.total_received_propagation_delta_ms =
2563 additional_stats.total_propagation_time_delta_ms;
2564 bwe.recent_received_propagation_delta_ms.swap(
2565 additional_stats.recent_propagation_time_delta_ms);
2566 bwe.recent_received_packet_group_arrival_time_ms.swap(
2567 additional_stats.recent_arrival_time_ms);
2568 }
2569 }
henrike@webrtc.orgb8395eb2014-02-28 21:57:22 +00002570
2571 engine_->vie()->rtp()->GetPacerQueuingDelayMs(
2572 recv_channels_[0]->channel_id(), &bwe.bucket_delay);
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00002573#endif
2574
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002575 // Calculations done above per send/receive stream.
2576 bwe.actual_enc_bitrate = video_bitrate_sent;
2577 bwe.transmit_bitrate = total_bitrate_sent;
2578 bwe.retransmit_bitrate = nack_bitrate_sent;
2579 bwe.available_send_bandwidth = estimated_send_bandwidth;
2580 bwe.available_recv_bandwidth = estimated_recv_bandwidth;
2581 bwe.target_enc_bitrate = target_enc_bitrate;
2582
2583 info->bw_estimations.push_back(bwe);
2584
2585 return true;
2586}
2587
2588bool WebRtcVideoMediaChannel::SetCapturer(uint32 ssrc,
2589 VideoCapturer* capturer) {
2590 ASSERT(ssrc != 0);
2591 if (!capturer) {
2592 return RemoveCapturer(ssrc);
2593 }
2594 WebRtcVideoChannelSendInfo* send_channel = GetSendChannel(ssrc);
2595 if (!send_channel) {
2596 return false;
2597 }
2598 VideoCapturer* old_capturer = send_channel->video_capturer();
wu@webrtc.org24301a62013-12-13 19:17:43 +00002599 MaybeDisconnectCapturer(old_capturer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002600
henrike@webrtc.orgc7bec842014-03-12 19:53:43 +00002601 send_channel->set_video_capturer(capturer, engine()->vie());
wu@webrtc.orga8910d22014-01-23 22:12:45 +00002602 MaybeConnectCapturer(capturer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002603 if (!capturer->IsScreencast() && ratio_w_ != 0 && ratio_h_ != 0) {
2604 capturer->UpdateAspectRatio(ratio_w_, ratio_h_);
2605 }
2606 const int64 timestamp = send_channel->local_stream_info()->time_stamp();
2607 if (send_codec_) {
2608 QueueBlackFrame(ssrc, timestamp, send_codec_->maxFramerate);
2609 }
2610 return true;
2611}
2612
2613bool WebRtcVideoMediaChannel::RequestIntraFrame() {
2614 // There is no API exposed to application to request a key frame
2615 // ViE does this internally when there are errors from decoder
2616 return false;
2617}
2618
wu@webrtc.orga9890802013-12-13 00:21:03 +00002619void WebRtcVideoMediaChannel::OnPacketReceived(
2620 talk_base::Buffer* packet, const talk_base::PacketTime& packet_time) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002621 // Pick which channel to send this packet to. If this packet doesn't match
2622 // any multiplexed streams, just send it to the default channel. Otherwise,
2623 // send it to the specific decoder instance for that stream.
2624 uint32 ssrc = 0;
2625 if (!GetRtpSsrc(packet->data(), packet->length(), &ssrc))
2626 return;
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00002627 int processing_channel = GetRecvChannelNum(ssrc);
2628 if (processing_channel == -1) {
2629 // Allocate an unsignalled recv channel for processing in conference mode.
henrike@webrtc.org18e59112014-03-14 17:19:38 +00002630 if (!InConferenceMode()) {
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00002631 // If we cant find or allocate one, use the default.
2632 processing_channel = video_channel();
henrike@webrtc.org18e59112014-03-14 17:19:38 +00002633 } else if (!CreateUnsignalledRecvChannel(ssrc, &processing_channel)) {
2634 // If we cant create an unsignalled recv channel, drop the packet in
2635 // conference mode.
2636 return;
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00002637 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002638 }
2639
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00002640 engine()->vie()->network()->ReceivedRTPPacket(
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00002641 processing_channel,
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00002642 packet->data(),
wu@webrtc.orga9890802013-12-13 00:21:03 +00002643 static_cast<int>(packet->length()),
2644 webrtc::PacketTime(packet_time.timestamp, packet_time.not_before));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002645}
2646
wu@webrtc.orga9890802013-12-13 00:21:03 +00002647void WebRtcVideoMediaChannel::OnRtcpReceived(
2648 talk_base::Buffer* packet, const talk_base::PacketTime& packet_time) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002649// Sending channels need all RTCP packets with feedback information.
2650// Even sender reports can contain attached report blocks.
2651// Receiving channels need sender reports in order to create
2652// correct receiver reports.
2653
2654 uint32 ssrc = 0;
2655 if (!GetRtcpSsrc(packet->data(), packet->length(), &ssrc)) {
2656 LOG(LS_WARNING) << "Failed to parse SSRC from received RTCP packet";
2657 return;
2658 }
2659 int type = 0;
2660 if (!GetRtcpType(packet->data(), packet->length(), &type)) {
2661 LOG(LS_WARNING) << "Failed to parse type from received RTCP packet";
2662 return;
2663 }
2664
2665 // If it is a sender report, find the channel that is listening.
2666 if (type == kRtcpTypeSR) {
2667 int which_channel = GetRecvChannelNum(ssrc);
2668 if (which_channel != -1 && !IsDefaultChannel(which_channel)) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00002669 engine_->vie()->network()->ReceivedRTCPPacket(
2670 which_channel,
2671 packet->data(),
2672 static_cast<int>(packet->length()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002673 }
2674 }
2675 // SR may continue RR and any RR entry may correspond to any one of the send
2676 // channels. So all RTCP packets must be forwarded all send channels. ViE
2677 // will filter out RR internally.
2678 for (SendChannelMap::iterator iter = send_channels_.begin();
2679 iter != send_channels_.end(); ++iter) {
2680 WebRtcVideoChannelSendInfo* send_channel = iter->second;
2681 int channel_id = send_channel->channel_id();
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00002682 engine_->vie()->network()->ReceivedRTCPPacket(
2683 channel_id,
2684 packet->data(),
2685 static_cast<int>(packet->length()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002686 }
2687}
2688
2689void WebRtcVideoMediaChannel::OnReadyToSend(bool ready) {
2690 SetNetworkTransmissionState(ready);
2691}
2692
2693bool WebRtcVideoMediaChannel::MuteStream(uint32 ssrc, bool muted) {
2694 WebRtcVideoChannelSendInfo* send_channel = GetSendChannel(ssrc);
2695 if (!send_channel) {
2696 LOG(LS_ERROR) << "The specified ssrc " << ssrc << " is not in use.";
2697 return false;
2698 }
2699 send_channel->set_muted(muted);
2700 return true;
2701}
2702
2703bool WebRtcVideoMediaChannel::SetRecvRtpHeaderExtensions(
2704 const std::vector<RtpHeaderExtension>& extensions) {
2705 if (receive_extensions_ == extensions) {
2706 return true;
2707 }
2708 receive_extensions_ = extensions;
2709
2710 const RtpHeaderExtension* offset_extension =
2711 FindHeaderExtension(extensions, kRtpTimestampOffsetHeaderExtension);
2712 const RtpHeaderExtension* send_time_extension =
henrike@webrtc.org79047f92014-03-06 23:46:59 +00002713 FindHeaderExtension(extensions, kRtpAbsoluteSenderTimeHeaderExtension);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002714
2715 // Loop through all receive channels and enable/disable the extensions.
2716 for (RecvChannelMap::iterator channel_it = recv_channels_.begin();
2717 channel_it != recv_channels_.end(); ++channel_it) {
2718 int channel_id = channel_it->second->channel_id();
2719 if (!SetHeaderExtension(
2720 &webrtc::ViERTP_RTCP::SetReceiveTimestampOffsetStatus, channel_id,
2721 offset_extension)) {
2722 return false;
2723 }
2724 if (!SetHeaderExtension(
2725 &webrtc::ViERTP_RTCP::SetReceiveAbsoluteSendTimeStatus, channel_id,
2726 send_time_extension)) {
2727 return false;
2728 }
2729 }
2730 return true;
2731}
2732
2733bool WebRtcVideoMediaChannel::SetSendRtpHeaderExtensions(
2734 const std::vector<RtpHeaderExtension>& extensions) {
2735 send_extensions_ = extensions;
2736
2737 const RtpHeaderExtension* offset_extension =
2738 FindHeaderExtension(extensions, kRtpTimestampOffsetHeaderExtension);
2739 const RtpHeaderExtension* send_time_extension =
henrike@webrtc.org79047f92014-03-06 23:46:59 +00002740 FindHeaderExtension(extensions, kRtpAbsoluteSenderTimeHeaderExtension);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002741
2742 // Loop through all send channels and enable/disable the extensions.
2743 for (SendChannelMap::iterator channel_it = send_channels_.begin();
2744 channel_it != send_channels_.end(); ++channel_it) {
2745 int channel_id = channel_it->second->channel_id();
2746 if (!SetHeaderExtension(
2747 &webrtc::ViERTP_RTCP::SetSendTimestampOffsetStatus, channel_id,
2748 offset_extension)) {
2749 return false;
2750 }
2751 if (!SetHeaderExtension(
2752 &webrtc::ViERTP_RTCP::SetSendAbsoluteSendTimeStatus, channel_id,
2753 send_time_extension)) {
2754 return false;
2755 }
2756 }
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00002757
2758 if (send_time_extension) {
2759 // For video RTP packets, we would like to update AbsoluteSendTimeHeader
2760 // Extension closer to the network, @ socket level before sending.
2761 // Pushing the extension id to socket layer.
2762 MediaChannel::SetOption(NetworkInterface::ST_RTP,
2763 talk_base::Socket::OPT_RTP_SENDTIME_EXTN_ID,
2764 send_time_extension->id);
2765 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002766 return true;
2767}
2768
mallinath@webrtc.org92fdfeb2014-02-17 18:49:41 +00002769int WebRtcVideoMediaChannel::GetRtpSendTimeExtnId() const {
2770 const RtpHeaderExtension* send_time_extension = FindHeaderExtension(
henrike@webrtc.org79047f92014-03-06 23:46:59 +00002771 send_extensions_, kRtpAbsoluteSenderTimeHeaderExtension);
mallinath@webrtc.org92fdfeb2014-02-17 18:49:41 +00002772 if (send_time_extension) {
2773 return send_time_extension->id;
2774 }
2775 return -1;
2776}
2777
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002778bool WebRtcVideoMediaChannel::SetStartSendBandwidth(int bps) {
2779 LOG(LS_INFO) << "WebRtcVideoMediaChannel::SetStartSendBandwidth";
2780
2781 if (!send_codec_) {
2782 LOG(LS_INFO) << "The send codec has not been set up yet";
2783 return true;
2784 }
2785
2786 // On success, SetSendCodec() will reset |send_start_bitrate_| to |bps/1000|,
2787 // by calling MaybeChangeStartBitrate. That method will also clamp the
2788 // start bitrate between min and max, consistent with the override behavior
2789 // in SetMaxSendBandwidth.
2790 return SetSendCodec(*send_codec_,
2791 send_min_bitrate_, bps / 1000, send_max_bitrate_);
2792}
2793
2794bool WebRtcVideoMediaChannel::SetMaxSendBandwidth(int bps) {
2795 LOG(LS_INFO) << "WebRtcVideoMediaChannel::SetMaxSendBandwidth";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002796
2797 if (InConferenceMode()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002798 LOG(LS_INFO) << "Conference mode ignores SetMaxSendBandwidth";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002799 return true;
2800 }
2801
2802 if (!send_codec_) {
2803 LOG(LS_INFO) << "The send codec has not been set up yet";
2804 return true;
2805 }
2806
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002807 // Use the default value or the bps for the max
2808 int max_bitrate = (bps <= 0) ? send_max_bitrate_ : (bps / 1000);
2809
2810 // Reduce the current minimum and start bitrates if necessary.
2811 int min_bitrate = talk_base::_min(send_min_bitrate_, max_bitrate);
2812 int start_bitrate = talk_base::_min(send_start_bitrate_, max_bitrate);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002813
2814 if (!SetSendCodec(*send_codec_, min_bitrate, start_bitrate, max_bitrate)) {
2815 return false;
2816 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002817 LogSendCodecChange("SetMaxSendBandwidth()");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002818
2819 return true;
2820}
2821
2822bool WebRtcVideoMediaChannel::SetOptions(const VideoOptions &options) {
2823 // Always accept options that are unchanged.
2824 if (options_ == options) {
2825 return true;
2826 }
2827
2828 // Trigger SetSendCodec to set correct noise reduction state if the option has
2829 // changed.
2830 bool denoiser_changed = options.video_noise_reduction.IsSet() &&
2831 (options_.video_noise_reduction != options.video_noise_reduction);
2832
2833 bool leaky_bucket_changed = options.video_leaky_bucket.IsSet() &&
2834 (options_.video_leaky_bucket != options.video_leaky_bucket);
2835
2836 bool buffer_latency_changed = options.buffered_mode_latency.IsSet() &&
2837 (options_.buffered_mode_latency != options.buffered_mode_latency);
2838
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00002839 bool cpu_overuse_detection_changed = options.cpu_overuse_detection.IsSet() &&
2840 (options_.cpu_overuse_detection != options.cpu_overuse_detection);
2841
wu@webrtc.orgde305012013-10-31 15:40:38 +00002842 bool dscp_option_changed = (options_.dscp != options.dscp);
2843
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002844 bool suspend_below_min_bitrate_changed =
2845 options.suspend_below_min_bitrate.IsSet() &&
2846 (options_.suspend_below_min_bitrate != options.suspend_below_min_bitrate);
2847
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002848 bool conference_mode_turned_off = false;
2849 if (options_.conference_mode.IsSet() && options.conference_mode.IsSet() &&
2850 options_.conference_mode.GetWithDefaultIfUnset(false) &&
2851 !options.conference_mode.GetWithDefaultIfUnset(false)) {
2852 conference_mode_turned_off = true;
2853 }
2854
henrike@webrtc.orgdce3feb2014-03-26 01:17:30 +00002855#ifdef USE_WEBRTC_DEV_BRANCH
2856 bool improved_wifi_bwe_changed =
2857 options.use_improved_wifi_bandwidth_estimator.IsSet() &&
2858 options_.use_improved_wifi_bandwidth_estimator !=
2859 options.use_improved_wifi_bandwidth_estimator;
2860
2861#endif
henrike@webrtc.org10bd88e2014-03-11 21:07:25 +00002862
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002863 // Save the options, to be interpreted where appropriate.
2864 // Use options_.SetAll() instead of assignment so that unset value in options
2865 // will not overwrite the previous option value.
2866 options_.SetAll(options);
2867
2868 // Set CPU options for all send channels.
2869 for (SendChannelMap::iterator iter = send_channels_.begin();
2870 iter != send_channels_.end(); ++iter) {
2871 WebRtcVideoChannelSendInfo* send_channel = iter->second;
2872 send_channel->ApplyCpuOptions(options_);
2873 }
2874
2875 // Adjust send codec bitrate if needed.
2876 int conf_max_bitrate = kDefaultConferenceModeMaxVideoBitrate;
2877
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00002878 // Save altered min_bitrate level and apply if necessary.
2879 bool adjusted_min_bitrate = false;
2880 if (options.lower_min_bitrate.IsSet()) {
2881 bool lower;
2882 options.lower_min_bitrate.Get(&lower);
2883
2884 int new_send_min_bitrate = lower ? kLowerMinBitrate : kMinVideoBitrate;
2885 adjusted_min_bitrate = (new_send_min_bitrate != send_min_bitrate_);
2886 send_min_bitrate_ = new_send_min_bitrate;
2887 }
2888
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002889 int expected_bitrate = send_max_bitrate_;
2890 if (InConferenceMode()) {
2891 expected_bitrate = conf_max_bitrate;
2892 } else if (conference_mode_turned_off) {
2893 // This is a special case for turning conference mode off.
2894 // Max bitrate should go back to the default maximum value instead
2895 // of the current maximum.
2896 expected_bitrate = kMaxVideoBitrate;
2897 }
2898
wu@webrtc.org1e6cb2c2014-03-24 17:01:50 +00002899 int options_start_bitrate;
2900 bool start_bitrate_changed = false;
2901 if (options.video_start_bitrate.Get(&options_start_bitrate) &&
2902 options_start_bitrate != send_start_bitrate_) {
2903 send_start_bitrate_ = options_start_bitrate;
2904 start_bitrate_changed = true;
2905 }
2906
henrike@webrtc.org10bd88e2014-03-11 21:07:25 +00002907 bool reset_send_codec_needed = send_codec_ &&
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00002908 (send_max_bitrate_ != expected_bitrate || denoiser_changed ||
wu@webrtc.org1e6cb2c2014-03-24 17:01:50 +00002909 adjusted_min_bitrate || start_bitrate_changed);
henrike@webrtc.org10bd88e2014-03-11 21:07:25 +00002910
2911
2912 if (reset_send_codec_needed) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002913 // On success, SetSendCodec() will reset send_max_bitrate_ to
2914 // expected_bitrate.
2915 if (!SetSendCodec(*send_codec_,
2916 send_min_bitrate_,
2917 send_start_bitrate_,
2918 expected_bitrate)) {
2919 return false;
2920 }
2921 LogSendCodecChange("SetOptions()");
2922 }
henrike@webrtc.org10bd88e2014-03-11 21:07:25 +00002923
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002924 if (leaky_bucket_changed) {
2925 bool enable_leaky_bucket =
2926 options_.video_leaky_bucket.GetWithDefaultIfUnset(false);
henrike@webrtc.org152208a2014-03-21 21:43:26 +00002927 LOG(LS_INFO) << "Leaky bucket is enabled : " << enable_leaky_bucket;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002928 for (SendChannelMap::iterator it = send_channels_.begin();
2929 it != send_channels_.end(); ++it) {
2930 if (engine()->vie()->rtp()->SetTransmissionSmoothingStatus(
2931 it->second->channel_id(), enable_leaky_bucket) != 0) {
2932 LOG_RTCERR2(SetTransmissionSmoothingStatus, it->second->channel_id(),
2933 enable_leaky_bucket);
2934 }
2935 }
2936 }
2937 if (buffer_latency_changed) {
2938 int buffer_latency =
2939 options_.buffered_mode_latency.GetWithDefaultIfUnset(
2940 cricket::kBufferedModeDisabled);
2941 for (SendChannelMap::iterator it = send_channels_.begin();
2942 it != send_channels_.end(); ++it) {
2943 if (engine()->vie()->rtp()->SetSenderBufferingMode(
2944 it->second->channel_id(), buffer_latency) != 0) {
2945 LOG_RTCERR2(SetSenderBufferingMode, it->second->channel_id(),
2946 buffer_latency);
2947 }
2948 }
2949 for (RecvChannelMap::iterator it = recv_channels_.begin();
2950 it != recv_channels_.end(); ++it) {
2951 if (engine()->vie()->rtp()->SetReceiverBufferingMode(
2952 it->second->channel_id(), buffer_latency) != 0) {
2953 LOG_RTCERR2(SetReceiverBufferingMode, it->second->channel_id(),
2954 buffer_latency);
2955 }
2956 }
2957 }
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00002958 if (cpu_overuse_detection_changed) {
2959 bool cpu_overuse_detection =
2960 options_.cpu_overuse_detection.GetWithDefaultIfUnset(false);
2961 for (SendChannelMap::iterator iter = send_channels_.begin();
2962 iter != send_channels_.end(); ++iter) {
2963 WebRtcVideoChannelSendInfo* send_channel = iter->second;
2964 send_channel->SetCpuOveruseDetection(cpu_overuse_detection);
2965 }
2966 }
wu@webrtc.orgde305012013-10-31 15:40:38 +00002967 if (dscp_option_changed) {
2968 talk_base::DiffServCodePoint dscp = talk_base::DSCP_DEFAULT;
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00002969 if (options_.dscp.GetWithDefaultIfUnset(false))
wu@webrtc.orgde305012013-10-31 15:40:38 +00002970 dscp = kVideoDscpValue;
2971 if (MediaChannel::SetDscp(dscp) != 0) {
2972 LOG(LS_WARNING) << "Failed to set DSCP settings for video channel";
2973 }
2974 }
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002975 if (suspend_below_min_bitrate_changed) {
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002976 if (options_.suspend_below_min_bitrate.GetWithDefaultIfUnset(false)) {
2977 for (SendChannelMap::iterator it = send_channels_.begin();
2978 it != send_channels_.end(); ++it) {
2979 engine()->vie()->codec()->SuspendBelowMinBitrate(
2980 it->second->channel_id());
2981 }
2982 } else {
2983 LOG(LS_WARNING) << "Cannot disable video suspension once it is enabled";
2984 }
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002985 }
henrike@webrtc.orgdce3feb2014-03-26 01:17:30 +00002986#ifdef USE_WEBRTC_DEV_BRANCH
2987 if (improved_wifi_bwe_changed) {
2988 webrtc::Config config;
2989 config.Set(new webrtc::AimdRemoteRateControl(
2990 options_.use_improved_wifi_bandwidth_estimator
2991 .GetWithDefaultIfUnset(false)));
2992 for (SendChannelMap::iterator it = send_channels_.begin();
2993 it != send_channels_.end(); ++it) {
2994 engine()->vie()->network()->SetBandwidthEstimationConfig(
2995 it->second->channel_id(), config);
2996 }
2997 }
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +00002998 webrtc::CpuOveruseOptions overuse_options;
2999 if (GetCpuOveruseOptions(options_, &overuse_options)) {
3000 for (SendChannelMap::iterator it = send_channels_.begin();
3001 it != send_channels_.end(); ++it) {
3002 if (engine()->vie()->base()->SetCpuOveruseOptions(
3003 it->second->channel_id(), overuse_options) != 0) {
3004 LOG_RTCERR1(SetCpuOveruseOptions, it->second->channel_id());
3005 }
3006 }
3007 }
henrike@webrtc.orgdce3feb2014-03-26 01:17:30 +00003008#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003009 return true;
3010}
3011
3012void WebRtcVideoMediaChannel::SetInterface(NetworkInterface* iface) {
3013 MediaChannel::SetInterface(iface);
3014 // Set the RTP recv/send buffer to a bigger size
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00003015 MediaChannel::SetOption(NetworkInterface::ST_RTP,
3016 talk_base::Socket::OPT_RCVBUF,
3017 kVideoRtpBufferSize);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003018
3019 // TODO(sriniv): Remove or re-enable this.
3020 // As part of b/8030474, send-buffer is size now controlled through
3021 // portallocator flags.
3022 // network_interface_->SetOption(NetworkInterface::ST_RTP,
3023 // talk_base::Socket::OPT_SNDBUF,
3024 // kVideoRtpBufferSize);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003025}
3026
3027void WebRtcVideoMediaChannel::UpdateAspectRatio(int ratio_w, int ratio_h) {
3028 ASSERT(ratio_w != 0);
3029 ASSERT(ratio_h != 0);
3030 ratio_w_ = ratio_w;
3031 ratio_h_ = ratio_h;
3032 // For now assume that all streams want the same aspect ratio.
3033 // TODO(hellner): remove the need for this assumption.
3034 for (SendChannelMap::iterator iter = send_channels_.begin();
3035 iter != send_channels_.end(); ++iter) {
3036 WebRtcVideoChannelSendInfo* send_channel = iter->second;
3037 VideoCapturer* capturer = send_channel->video_capturer();
3038 if (capturer) {
3039 capturer->UpdateAspectRatio(ratio_w, ratio_h);
3040 }
3041 }
3042}
3043
3044bool WebRtcVideoMediaChannel::GetRenderer(uint32 ssrc,
3045 VideoRenderer** renderer) {
3046 RecvChannelMap::const_iterator it = recv_channels_.find(ssrc);
3047 if (it == recv_channels_.end()) {
3048 if (first_receive_ssrc_ == ssrc &&
3049 recv_channels_.find(0) != recv_channels_.end()) {
3050 LOG(LS_INFO) << " GetRenderer " << ssrc
3051 << " reuse default renderer #"
3052 << vie_channel_;
3053 *renderer = recv_channels_[0]->render_adapter()->renderer();
3054 return true;
3055 }
3056 return false;
3057 }
3058
3059 *renderer = it->second->render_adapter()->renderer();
3060 return true;
3061}
3062
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00003063bool WebRtcVideoMediaChannel::GetVideoAdapter(
3064 uint32 ssrc, CoordinatedVideoAdapter** video_adapter) {
3065 SendChannelMap::iterator it = send_channels_.find(ssrc);
3066 if (it == send_channels_.end()) {
3067 return false;
3068 }
3069 *video_adapter = it->second->video_adapter();
3070 return true;
3071}
3072
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003073void WebRtcVideoMediaChannel::SendFrame(VideoCapturer* capturer,
3074 const VideoFrame* frame) {
wu@webrtc.org24301a62013-12-13 19:17:43 +00003075 // If the |capturer| is registered to any send channel, then send the frame
3076 // to those send channels.
3077 bool capturer_is_channel_owned = false;
3078 for (SendChannelMap::iterator iter = send_channels_.begin();
3079 iter != send_channels_.end(); ++iter) {
3080 WebRtcVideoChannelSendInfo* send_channel = iter->second;
3081 if (send_channel->video_capturer() == capturer) {
3082 SendFrame(send_channel, frame, capturer->IsScreencast());
3083 capturer_is_channel_owned = true;
3084 }
3085 }
3086 if (capturer_is_channel_owned) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003087 return;
3088 }
wu@webrtc.org24301a62013-12-13 19:17:43 +00003089
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003090 // TODO(hellner): Remove below for loop once the captured frame no longer
3091 // come from the engine, i.e. the engine no longer owns a capturer.
3092 for (SendChannelMap::iterator iter = send_channels_.begin();
3093 iter != send_channels_.end(); ++iter) {
3094 WebRtcVideoChannelSendInfo* send_channel = iter->second;
3095 if (send_channel->video_capturer() == NULL) {
3096 SendFrame(send_channel, frame, capturer->IsScreencast());
3097 }
3098 }
3099}
3100
3101bool WebRtcVideoMediaChannel::SendFrame(
3102 WebRtcVideoChannelSendInfo* send_channel,
3103 const VideoFrame* frame,
3104 bool is_screencast) {
3105 if (!send_channel) {
3106 return false;
3107 }
3108 if (!send_codec_) {
3109 // Send codec has not been set. No reason to process the frame any further.
3110 return false;
3111 }
3112 const VideoFormat& video_format = send_channel->video_format();
3113 // If the frame should be dropped.
3114 const bool video_format_set = video_format != cricket::VideoFormat();
3115 if (video_format_set &&
3116 (video_format.width == 0 && video_format.height == 0)) {
3117 return true;
3118 }
3119
3120 // Checks if we need to reset vie send codec.
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00003121 if (!MaybeResetVieSendCodec(send_channel,
3122 static_cast<int>(frame->GetWidth()),
3123 static_cast<int>(frame->GetHeight()),
3124 is_screencast, NULL)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003125 LOG(LS_ERROR) << "MaybeResetVieSendCodec failed with "
3126 << frame->GetWidth() << "x" << frame->GetHeight();
3127 return false;
3128 }
3129 const VideoFrame* frame_out = frame;
3130 talk_base::scoped_ptr<VideoFrame> processed_frame;
3131 // Disable muting for screencast.
3132 const bool mute = (send_channel->muted() && !is_screencast);
3133 send_channel->ProcessFrame(*frame_out, mute, processed_frame.use());
3134 if (processed_frame) {
3135 frame_out = processed_frame.get();
3136 }
3137
3138 webrtc::ViEVideoFrameI420 frame_i420;
3139 // TODO(ronghuawu): Update the webrtc::ViEVideoFrameI420
3140 // to use const unsigned char*
3141 frame_i420.y_plane = const_cast<unsigned char*>(frame_out->GetYPlane());
3142 frame_i420.u_plane = const_cast<unsigned char*>(frame_out->GetUPlane());
3143 frame_i420.v_plane = const_cast<unsigned char*>(frame_out->GetVPlane());
3144 frame_i420.y_pitch = frame_out->GetYPitch();
3145 frame_i420.u_pitch = frame_out->GetUPitch();
3146 frame_i420.v_pitch = frame_out->GetVPitch();
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00003147 frame_i420.width = static_cast<uint16>(frame_out->GetWidth());
3148 frame_i420.height = static_cast<uint16>(frame_out->GetHeight());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003149
3150 int64 timestamp_ntp_ms = 0;
3151 // TODO(justinlin): Reenable after Windows issues with clock drift are fixed.
3152 // Currently reverted to old behavior of discarding capture timestamp.
3153#if 0
3154 // If the frame timestamp is 0, we will use the deliver time.
3155 const int64 frame_timestamp = frame->GetTimeStamp();
3156 if (frame_timestamp != 0) {
3157 if (abs(time(NULL) - frame_timestamp / talk_base::kNumNanosecsPerSec) >
3158 kTimestampDeltaInSecondsForWarning) {
3159 LOG(LS_WARNING) << "Frame timestamp differs by more than "
3160 << kTimestampDeltaInSecondsForWarning << " seconds from "
3161 << "current Unix timestamp.";
3162 }
3163
3164 timestamp_ntp_ms =
3165 talk_base::UnixTimestampNanosecsToNtpMillisecs(frame_timestamp);
3166 }
3167#endif
3168
3169 return send_channel->external_capture()->IncomingFrameI420(
3170 frame_i420, timestamp_ntp_ms) == 0;
3171}
3172
3173bool WebRtcVideoMediaChannel::CreateChannel(uint32 ssrc_key,
3174 MediaDirection direction,
3175 int* channel_id) {
3176 // There are 3 types of channels. Sending only, receiving only and
3177 // sending and receiving. The sending and receiving channel is the
3178 // default channel and there is only one. All other channels that are created
3179 // are associated with the default channel which must exist. The default
3180 // channel id is stored in |vie_channel_|. All channels need to know about
3181 // the default channel to properly handle remb which is why there are
3182 // different ViE create channel calls.
3183 // For this channel the local and remote ssrc key is 0. However, it may
3184 // have a non-zero local and/or remote ssrc depending on if it is currently
3185 // sending and/or receiving.
3186 if ((vie_channel_ == -1 || direction == MD_SENDRECV) &&
3187 (!send_channels_.empty() || !recv_channels_.empty())) {
3188 ASSERT(false);
3189 return false;
3190 }
3191
3192 *channel_id = -1;
3193 if (direction == MD_RECV) {
3194 // All rec channels are associated with the default channel |vie_channel_|
3195 if (engine_->vie()->base()->CreateReceiveChannel(*channel_id,
3196 vie_channel_) != 0) {
3197 LOG_RTCERR2(CreateReceiveChannel, *channel_id, vie_channel_);
3198 return false;
3199 }
3200 } else if (direction == MD_SEND) {
3201 if (engine_->vie()->base()->CreateChannel(*channel_id,
3202 vie_channel_) != 0) {
3203 LOG_RTCERR2(CreateChannel, *channel_id, vie_channel_);
3204 return false;
3205 }
3206 } else {
3207 ASSERT(direction == MD_SENDRECV);
3208 if (engine_->vie()->base()->CreateChannel(*channel_id) != 0) {
3209 LOG_RTCERR1(CreateChannel, *channel_id);
3210 return false;
3211 }
3212 }
3213 if (!ConfigureChannel(*channel_id, direction, ssrc_key)) {
3214 engine_->vie()->base()->DeleteChannel(*channel_id);
3215 *channel_id = -1;
3216 return false;
3217 }
3218
3219 return true;
3220}
3221
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00003222bool WebRtcVideoMediaChannel::CreateUnsignalledRecvChannel(
3223 uint32 ssrc_key, int* out_channel_id) {
henrike@webrtc.org18e59112014-03-14 17:19:38 +00003224 int unsignalled_recv_channel_limit =
3225 options_.unsignalled_recv_stream_limit.GetWithDefaultIfUnset(
3226 kNumDefaultUnsignalledVideoRecvStreams);
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00003227 if (num_unsignalled_recv_channels_ >= unsignalled_recv_channel_limit) {
3228 return false;
3229 }
3230 if (!CreateChannel(ssrc_key, MD_RECV, out_channel_id)) {
3231 return false;
3232 }
3233 // TODO(tvsriram): Support dynamic sizing of unsignalled recv channels.
3234 num_unsignalled_recv_channels_++;
3235 return true;
3236}
3237
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003238bool WebRtcVideoMediaChannel::ConfigureChannel(int channel_id,
3239 MediaDirection direction,
3240 uint32 ssrc_key) {
3241 const bool receiving = (direction == MD_RECV) || (direction == MD_SENDRECV);
3242 const bool sending = (direction == MD_SEND) || (direction == MD_SENDRECV);
3243 // Register external transport.
3244 if (engine_->vie()->network()->RegisterSendTransport(
3245 channel_id, *this) != 0) {
3246 LOG_RTCERR1(RegisterSendTransport, channel_id);
3247 return false;
3248 }
3249
3250 // Set MTU.
3251 if (engine_->vie()->network()->SetMTU(channel_id, kVideoMtu) != 0) {
3252 LOG_RTCERR2(SetMTU, channel_id, kVideoMtu);
3253 return false;
3254 }
3255 // Turn on RTCP and loss feedback reporting.
3256 if (engine()->vie()->rtp()->SetRTCPStatus(
3257 channel_id, webrtc::kRtcpCompound_RFC4585) != 0) {
3258 LOG_RTCERR2(SetRTCPStatus, channel_id, webrtc::kRtcpCompound_RFC4585);
3259 return false;
3260 }
3261 // Enable pli as key frame request method.
3262 if (engine_->vie()->rtp()->SetKeyFrameRequestMethod(
3263 channel_id, webrtc::kViEKeyFrameRequestPliRtcp) != 0) {
3264 LOG_RTCERR2(SetKeyFrameRequestMethod,
3265 channel_id, webrtc::kViEKeyFrameRequestPliRtcp);
3266 return false;
3267 }
3268 if (!SetNackFec(channel_id, send_red_type_, send_fec_type_, nack_enabled_)) {
3269 // Logged in SetNackFec. Don't spam the logs.
3270 return false;
3271 }
3272 // Note that receiving must always be configured before sending to ensure
3273 // that send and receive channel is configured correctly (ConfigureReceiving
3274 // assumes no sending).
3275 if (receiving) {
3276 if (!ConfigureReceiving(channel_id, ssrc_key)) {
3277 return false;
3278 }
3279 }
3280 if (sending) {
3281 if (!ConfigureSending(channel_id, ssrc_key)) {
3282 return false;
3283 }
3284 }
3285
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00003286 // Start receiving for both receive and send channels so that we get incoming
3287 // RTP (if receiving) as well as RTCP feedback (if sending).
3288 if (engine()->vie()->base()->StartReceive(channel_id) != 0) {
3289 LOG_RTCERR1(StartReceive, channel_id);
3290 return false;
3291 }
3292
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003293 return true;
3294}
3295
3296bool WebRtcVideoMediaChannel::ConfigureReceiving(int channel_id,
3297 uint32 remote_ssrc_key) {
3298 // Make sure that an SSRC/key isn't registered more than once.
3299 if (recv_channels_.find(remote_ssrc_key) != recv_channels_.end()) {
3300 return false;
3301 }
3302 // Connect the voice channel, if there is one.
3303 // TODO(perkj): The A/V is synched by the receiving channel. So we need to
3304 // know the SSRC of the remote audio channel in order to fetch the correct
3305 // webrtc VoiceEngine channel. For now- only sync the default channel used
3306 // in 1-1 calls.
3307 if (remote_ssrc_key == 0 && voice_channel_) {
3308 WebRtcVoiceMediaChannel* voice_channel =
3309 static_cast<WebRtcVoiceMediaChannel*>(voice_channel_);
3310 if (engine_->vie()->base()->ConnectAudioChannel(
3311 vie_channel_, voice_channel->voe_channel()) != 0) {
3312 LOG_RTCERR2(ConnectAudioChannel, channel_id,
3313 voice_channel->voe_channel());
3314 LOG(LS_WARNING) << "A/V not synchronized";
3315 // Not a fatal error.
3316 }
3317 }
3318
3319 talk_base::scoped_ptr<WebRtcVideoChannelRecvInfo> channel_info(
3320 new WebRtcVideoChannelRecvInfo(channel_id));
3321
3322 // Install a render adapter.
3323 if (engine_->vie()->render()->AddRenderer(channel_id,
3324 webrtc::kVideoI420, channel_info->render_adapter()) != 0) {
3325 LOG_RTCERR3(AddRenderer, channel_id, webrtc::kVideoI420,
3326 channel_info->render_adapter());
3327 return false;
3328 }
3329
3330
3331 if (engine_->vie()->rtp()->SetRembStatus(channel_id,
3332 kNotSending,
3333 remb_enabled_) != 0) {
3334 LOG_RTCERR3(SetRembStatus, channel_id, kNotSending, remb_enabled_);
3335 return false;
3336 }
3337
3338 if (!SetHeaderExtension(&webrtc::ViERTP_RTCP::SetReceiveTimestampOffsetStatus,
3339 channel_id, receive_extensions_, kRtpTimestampOffsetHeaderExtension)) {
3340 return false;
3341 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003342 if (!SetHeaderExtension(
3343 &webrtc::ViERTP_RTCP::SetReceiveAbsoluteSendTimeStatus, channel_id,
henrike@webrtc.org79047f92014-03-06 23:46:59 +00003344 receive_extensions_, kRtpAbsoluteSenderTimeHeaderExtension)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003345 return false;
3346 }
3347
3348 if (remote_ssrc_key != 0) {
3349 // Use the same SSRC as our default channel
3350 // (so the RTCP reports are correct).
3351 unsigned int send_ssrc = 0;
3352 webrtc::ViERTP_RTCP* rtp = engine()->vie()->rtp();
3353 if (rtp->GetLocalSSRC(vie_channel_, send_ssrc) == -1) {
3354 LOG_RTCERR2(GetLocalSSRC, vie_channel_, send_ssrc);
3355 return false;
3356 }
3357 if (rtp->SetLocalSSRC(channel_id, send_ssrc) == -1) {
3358 LOG_RTCERR2(SetLocalSSRC, channel_id, send_ssrc);
3359 return false;
3360 }
3361 } // Else this is the the default channel and we don't change the SSRC.
3362
3363 // Disable color enhancement since it is a bit too aggressive.
3364 if (engine()->vie()->image()->EnableColorEnhancement(channel_id,
3365 false) != 0) {
3366 LOG_RTCERR1(EnableColorEnhancement, channel_id);
3367 return false;
3368 }
3369
3370 if (!SetReceiveCodecs(channel_info.get())) {
3371 return false;
3372 }
3373
3374 int buffer_latency =
3375 options_.buffered_mode_latency.GetWithDefaultIfUnset(
3376 cricket::kBufferedModeDisabled);
3377 if (buffer_latency != cricket::kBufferedModeDisabled) {
3378 if (engine()->vie()->rtp()->SetReceiverBufferingMode(
3379 channel_id, buffer_latency) != 0) {
3380 LOG_RTCERR2(SetReceiverBufferingMode, channel_id, buffer_latency);
3381 }
3382 }
3383
3384 if (render_started_) {
3385 if (engine_->vie()->render()->StartRender(channel_id) != 0) {
3386 LOG_RTCERR1(StartRender, channel_id);
3387 return false;
3388 }
3389 }
3390
3391 // Register decoder observer for incoming framerate and bitrate.
3392 if (engine()->vie()->codec()->RegisterDecoderObserver(
3393 channel_id, *channel_info->decoder_observer()) != 0) {
3394 LOG_RTCERR1(RegisterDecoderObserver, channel_info->decoder_observer());
3395 return false;
3396 }
3397
3398 recv_channels_[remote_ssrc_key] = channel_info.release();
3399 return true;
3400}
3401
3402bool WebRtcVideoMediaChannel::ConfigureSending(int channel_id,
3403 uint32 local_ssrc_key) {
3404 // The ssrc key can be zero or correspond to an SSRC.
3405 // Make sure the default channel isn't configured more than once.
3406 if (local_ssrc_key == 0 && send_channels_.find(0) != send_channels_.end()) {
3407 return false;
3408 }
3409 // Make sure that the SSRC is not already in use.
3410 uint32 dummy_key;
3411 if (GetSendChannelKey(local_ssrc_key, &dummy_key)) {
3412 return false;
3413 }
3414 int vie_capture = 0;
3415 webrtc::ViEExternalCapture* external_capture = NULL;
3416 // Register external capture.
3417 if (engine()->vie()->capture()->AllocateExternalCaptureDevice(
3418 vie_capture, external_capture) != 0) {
3419 LOG_RTCERR0(AllocateExternalCaptureDevice);
3420 return false;
3421 }
3422
3423 // Connect external capture.
3424 if (engine()->vie()->capture()->ConnectCaptureDevice(
3425 vie_capture, channel_id) != 0) {
3426 LOG_RTCERR2(ConnectCaptureDevice, vie_capture, channel_id);
3427 return false;
3428 }
3429 talk_base::scoped_ptr<WebRtcVideoChannelSendInfo> send_channel(
3430 new WebRtcVideoChannelSendInfo(channel_id, vie_capture,
3431 external_capture,
3432 engine()->cpu_monitor()));
3433 send_channel->ApplyCpuOptions(options_);
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00003434 send_channel->SignalCpuAdaptationUnable.connect(this,
3435 &WebRtcVideoMediaChannel::OnCpuAdaptationUnable);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003436
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00003437 if (options_.cpu_overuse_detection.GetWithDefaultIfUnset(false)) {
3438 send_channel->SetCpuOveruseDetection(true);
3439 }
3440
henrike@webrtc.orgb0ecc1c2014-03-26 22:44:28 +00003441#ifdef USE_WEBRTC_DEV_BRANCH
3442 webrtc::CpuOveruseOptions overuse_options;
3443 if (GetCpuOveruseOptions(options_, &overuse_options)) {
3444 if (engine()->vie()->base()->SetCpuOveruseOptions(channel_id,
3445 overuse_options) != 0) {
3446 LOG_RTCERR1(SetCpuOveruseOptions, channel_id);
3447 }
3448 }
3449#endif
3450
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003451 // Register encoder observer for outgoing framerate and bitrate.
3452 if (engine()->vie()->codec()->RegisterEncoderObserver(
3453 channel_id, *send_channel->encoder_observer()) != 0) {
3454 LOG_RTCERR1(RegisterEncoderObserver, send_channel->encoder_observer());
3455 return false;
3456 }
3457
3458 if (!SetHeaderExtension(&webrtc::ViERTP_RTCP::SetSendTimestampOffsetStatus,
3459 channel_id, send_extensions_, kRtpTimestampOffsetHeaderExtension)) {
3460 return false;
3461 }
3462
3463 if (!SetHeaderExtension(&webrtc::ViERTP_RTCP::SetSendAbsoluteSendTimeStatus,
henrike@webrtc.org79047f92014-03-06 23:46:59 +00003464 channel_id, send_extensions_, kRtpAbsoluteSenderTimeHeaderExtension)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003465 return false;
3466 }
3467
3468 if (options_.video_leaky_bucket.GetWithDefaultIfUnset(false)) {
3469 if (engine()->vie()->rtp()->SetTransmissionSmoothingStatus(channel_id,
3470 true) != 0) {
3471 LOG_RTCERR2(SetTransmissionSmoothingStatus, channel_id, true);
3472 return false;
3473 }
3474 }
3475
3476 int buffer_latency =
3477 options_.buffered_mode_latency.GetWithDefaultIfUnset(
3478 cricket::kBufferedModeDisabled);
3479 if (buffer_latency != cricket::kBufferedModeDisabled) {
3480 if (engine()->vie()->rtp()->SetSenderBufferingMode(
3481 channel_id, buffer_latency) != 0) {
3482 LOG_RTCERR2(SetSenderBufferingMode, channel_id, buffer_latency);
3483 }
3484 }
henrike@webrtc.org6e3dbc22014-03-25 17:09:47 +00003485
3486 if (options_.suspend_below_min_bitrate.GetWithDefaultIfUnset(false)) {
3487 engine()->vie()->codec()->SuspendBelowMinBitrate(channel_id);
3488 }
3489
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003490 // The remb status direction correspond to the RTP stream (and not the RTCP
3491 // stream). I.e. if send remb is enabled it means it is receiving remote
3492 // rembs and should use them to estimate bandwidth. Receive remb mean that
3493 // remb packets will be generated and that the channel should be included in
3494 // it. If remb is enabled all channels are allowed to contribute to the remb
3495 // but only receive channels will ever end up actually contributing. This
3496 // keeps the logic simple.
3497 if (engine_->vie()->rtp()->SetRembStatus(channel_id,
3498 remb_enabled_,
3499 remb_enabled_) != 0) {
3500 LOG_RTCERR3(SetRembStatus, channel_id, remb_enabled_, remb_enabled_);
3501 return false;
3502 }
3503 if (!SetNackFec(channel_id, send_red_type_, send_fec_type_, nack_enabled_)) {
3504 // Logged in SetNackFec. Don't spam the logs.
3505 return false;
3506 }
3507
3508 send_channels_[local_ssrc_key] = send_channel.release();
3509
3510 return true;
3511}
3512
3513bool WebRtcVideoMediaChannel::SetNackFec(int channel_id,
3514 int red_payload_type,
3515 int fec_payload_type,
3516 bool nack_enabled) {
3517 bool enable = (red_payload_type != -1 && fec_payload_type != -1 &&
3518 !InConferenceMode());
3519 if (enable) {
3520 if (engine_->vie()->rtp()->SetHybridNACKFECStatus(
3521 channel_id, nack_enabled, red_payload_type, fec_payload_type) != 0) {
3522 LOG_RTCERR4(SetHybridNACKFECStatus,
3523 channel_id, nack_enabled, red_payload_type, fec_payload_type);
3524 return false;
3525 }
3526 LOG(LS_INFO) << "Hybrid NACK/FEC enabled for channel " << channel_id;
3527 } else {
3528 if (engine_->vie()->rtp()->SetNACKStatus(channel_id, nack_enabled) != 0) {
3529 LOG_RTCERR1(SetNACKStatus, channel_id);
3530 return false;
3531 }
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +00003532 std::string enabled = nack_enabled ? "enabled" : "disabled";
3533 LOG(LS_INFO) << "NACK " << enabled << " for channel " << channel_id;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003534 }
3535 return true;
3536}
3537
3538bool WebRtcVideoMediaChannel::SetSendCodec(const webrtc::VideoCodec& codec,
3539 int min_bitrate,
3540 int start_bitrate,
3541 int max_bitrate) {
3542 bool ret_val = true;
3543 for (SendChannelMap::iterator iter = send_channels_.begin();
3544 iter != send_channels_.end(); ++iter) {
3545 WebRtcVideoChannelSendInfo* send_channel = iter->second;
3546 ret_val = SetSendCodec(send_channel, codec, min_bitrate, start_bitrate,
3547 max_bitrate) && ret_val;
3548 }
3549 if (ret_val) {
3550 // All SetSendCodec calls were successful. Update the global state
3551 // accordingly.
3552 send_codec_.reset(new webrtc::VideoCodec(codec));
3553 send_min_bitrate_ = min_bitrate;
3554 send_start_bitrate_ = start_bitrate;
3555 send_max_bitrate_ = max_bitrate;
3556 } else {
3557 // At least one SetSendCodec call failed, rollback.
3558 for (SendChannelMap::iterator iter = send_channels_.begin();
3559 iter != send_channels_.end(); ++iter) {
3560 WebRtcVideoChannelSendInfo* send_channel = iter->second;
3561 if (send_codec_) {
3562 SetSendCodec(send_channel, *send_codec_.get(), send_min_bitrate_,
3563 send_start_bitrate_, send_max_bitrate_);
3564 }
3565 }
3566 }
3567 return ret_val;
3568}
3569
3570bool WebRtcVideoMediaChannel::SetSendCodec(
3571 WebRtcVideoChannelSendInfo* send_channel,
3572 const webrtc::VideoCodec& codec,
3573 int min_bitrate,
3574 int start_bitrate,
3575 int max_bitrate) {
3576 if (!send_channel) {
3577 return false;
3578 }
3579 const int channel_id = send_channel->channel_id();
3580 // Make a copy of the codec
3581 webrtc::VideoCodec target_codec = codec;
3582 target_codec.startBitrate = start_bitrate;
3583 target_codec.minBitrate = min_bitrate;
3584 target_codec.maxBitrate = max_bitrate;
3585
3586 // Set the default number of temporal layers for VP8.
3587 if (webrtc::kVideoCodecVP8 == codec.codecType) {
3588 target_codec.codecSpecific.VP8.numberOfTemporalLayers =
3589 kDefaultNumberOfTemporalLayers;
3590
3591 // Turn off the VP8 error resilience
3592 target_codec.codecSpecific.VP8.resilience = webrtc::kResilienceOff;
3593
3594 bool enable_denoising =
3595 options_.video_noise_reduction.GetWithDefaultIfUnset(false);
3596 target_codec.codecSpecific.VP8.denoisingOn = enable_denoising;
3597 }
3598
3599 // Register external encoder if codec type is supported by encoder factory.
3600 if (engine()->IsExternalEncoderCodecType(codec.codecType) &&
3601 !send_channel->IsEncoderRegistered(target_codec.plType)) {
3602 webrtc::VideoEncoder* encoder =
3603 engine()->CreateExternalEncoder(codec.codecType);
3604 if (encoder) {
3605 if (engine()->vie()->ext_codec()->RegisterExternalSendCodec(
3606 channel_id, target_codec.plType, encoder, false) == 0) {
3607 send_channel->RegisterEncoder(target_codec.plType, encoder);
3608 } else {
3609 LOG_RTCERR2(RegisterExternalSendCodec, channel_id, target_codec.plName);
3610 engine()->DestroyExternalEncoder(encoder);
3611 }
3612 }
3613 }
3614
3615 // Resolution and framerate may vary for different send channels.
3616 const VideoFormat& video_format = send_channel->video_format();
3617 UpdateVideoCodec(video_format, &target_codec);
3618
3619 if (target_codec.width == 0 && target_codec.height == 0) {
3620 const uint32 ssrc = send_channel->stream_params()->first_ssrc();
3621 LOG(LS_INFO) << "0x0 resolution selected. Captured frames will be dropped "
3622 << "for ssrc: " << ssrc << ".";
3623 } else {
3624 MaybeChangeStartBitrate(channel_id, &target_codec);
3625 if (0 != engine()->vie()->codec()->SetSendCodec(channel_id, target_codec)) {
3626 LOG_RTCERR2(SetSendCodec, channel_id, target_codec.plName);
3627 return false;
3628 }
3629
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00003630 // NOTE: SetRtxSendPayloadType must be called after all simulcast SSRCs
3631 // are configured. Otherwise ssrc's configured after this point will use
3632 // the primary PT for RTX.
3633 if (send_rtx_type_ != -1 &&
3634 engine()->vie()->rtp()->SetRtxSendPayloadType(channel_id,
3635 send_rtx_type_) != 0) {
3636 LOG_RTCERR2(SetRtxSendPayloadType, channel_id, send_rtx_type_);
3637 return false;
3638 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003639 }
3640 send_channel->set_interval(
3641 cricket::VideoFormat::FpsToInterval(target_codec.maxFramerate));
3642 return true;
3643}
3644
3645
3646static std::string ToString(webrtc::VideoCodecComplexity complexity) {
3647 switch (complexity) {
3648 case webrtc::kComplexityNormal:
3649 return "normal";
3650 case webrtc::kComplexityHigh:
3651 return "high";
3652 case webrtc::kComplexityHigher:
3653 return "higher";
3654 case webrtc::kComplexityMax:
3655 return "max";
3656 default:
3657 return "unknown";
3658 }
3659}
3660
3661static std::string ToString(webrtc::VP8ResilienceMode resilience) {
3662 switch (resilience) {
3663 case webrtc::kResilienceOff:
3664 return "off";
3665 case webrtc::kResilientStream:
3666 return "stream";
3667 case webrtc::kResilientFrames:
3668 return "frames";
3669 default:
3670 return "unknown";
3671 }
3672}
3673
3674void WebRtcVideoMediaChannel::LogSendCodecChange(const std::string& reason) {
3675 webrtc::VideoCodec vie_codec;
3676 if (engine()->vie()->codec()->GetSendCodec(vie_channel_, vie_codec) != 0) {
3677 LOG_RTCERR1(GetSendCodec, vie_channel_);
3678 return;
3679 }
3680
3681 LOG(LS_INFO) << reason << " : selected video codec "
3682 << vie_codec.plName << "/"
3683 << vie_codec.width << "x" << vie_codec.height << "x"
3684 << static_cast<int>(vie_codec.maxFramerate) << "fps"
3685 << "@" << vie_codec.maxBitrate << "kbps"
3686 << " (min=" << vie_codec.minBitrate << "kbps,"
3687 << " start=" << vie_codec.startBitrate << "kbps)";
3688 LOG(LS_INFO) << "Video max quantization: " << vie_codec.qpMax;
3689 if (webrtc::kVideoCodecVP8 == vie_codec.codecType) {
3690 LOG(LS_INFO) << "VP8 number of temporal layers: "
3691 << static_cast<int>(
3692 vie_codec.codecSpecific.VP8.numberOfTemporalLayers);
3693 LOG(LS_INFO) << "VP8 options : "
3694 << "picture loss indication = "
3695 << vie_codec.codecSpecific.VP8.pictureLossIndicationOn
3696 << ", feedback mode = "
3697 << vie_codec.codecSpecific.VP8.feedbackModeOn
3698 << ", complexity = "
3699 << ToString(vie_codec.codecSpecific.VP8.complexity)
3700 << ", resilience = "
3701 << ToString(vie_codec.codecSpecific.VP8.resilience)
3702 << ", denoising = "
3703 << vie_codec.codecSpecific.VP8.denoisingOn
3704 << ", error concealment = "
3705 << vie_codec.codecSpecific.VP8.errorConcealmentOn
3706 << ", automatic resize = "
3707 << vie_codec.codecSpecific.VP8.automaticResizeOn
3708 << ", frame dropping = "
3709 << vie_codec.codecSpecific.VP8.frameDroppingOn
3710 << ", key frame interval = "
3711 << vie_codec.codecSpecific.VP8.keyFrameInterval;
3712 }
3713
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00003714 if (send_rtx_type_ != -1) {
3715 LOG(LS_INFO) << "RTX payload type: " << send_rtx_type_;
3716 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003717}
3718
3719bool WebRtcVideoMediaChannel::SetReceiveCodecs(
3720 WebRtcVideoChannelRecvInfo* info) {
3721 int red_type = -1;
3722 int fec_type = -1;
3723 int channel_id = info->channel_id();
3724 for (std::vector<webrtc::VideoCodec>::iterator it = receive_codecs_.begin();
3725 it != receive_codecs_.end(); ++it) {
3726 if (it->codecType == webrtc::kVideoCodecRED) {
3727 red_type = it->plType;
3728 } else if (it->codecType == webrtc::kVideoCodecULPFEC) {
3729 fec_type = it->plType;
3730 }
3731 if (engine()->vie()->codec()->SetReceiveCodec(channel_id, *it) != 0) {
3732 LOG_RTCERR2(SetReceiveCodec, channel_id, it->plName);
3733 return false;
3734 }
3735 if (!info->IsDecoderRegistered(it->plType) &&
3736 it->codecType != webrtc::kVideoCodecRED &&
3737 it->codecType != webrtc::kVideoCodecULPFEC) {
3738 webrtc::VideoDecoder* decoder =
3739 engine()->CreateExternalDecoder(it->codecType);
3740 if (decoder) {
3741 if (engine()->vie()->ext_codec()->RegisterExternalReceiveCodec(
3742 channel_id, it->plType, decoder) == 0) {
3743 info->RegisterDecoder(it->plType, decoder);
3744 } else {
3745 LOG_RTCERR2(RegisterExternalReceiveCodec, channel_id, it->plName);
3746 engine()->DestroyExternalDecoder(decoder);
3747 }
3748 }
3749 }
3750 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003751 return true;
3752}
3753
3754int WebRtcVideoMediaChannel::GetRecvChannelNum(uint32 ssrc) {
3755 if (ssrc == first_receive_ssrc_) {
3756 return vie_channel_;
3757 }
3758 RecvChannelMap::iterator it = recv_channels_.find(ssrc);
3759 return (it != recv_channels_.end()) ? it->second->channel_id() : -1;
3760}
3761
3762// If the new frame size is different from the send codec size we set on vie,
3763// we need to reset the send codec on vie.
3764// The new send codec size should not exceed send_codec_ which is controlled
3765// only by the 'jec' logic.
3766bool WebRtcVideoMediaChannel::MaybeResetVieSendCodec(
3767 WebRtcVideoChannelSendInfo* send_channel,
3768 int new_width,
3769 int new_height,
3770 bool is_screencast,
3771 bool* reset) {
3772 if (reset) {
3773 *reset = false;
3774 }
3775 ASSERT(send_codec_.get() != NULL);
3776
3777 webrtc::VideoCodec target_codec = *send_codec_.get();
3778 const VideoFormat& video_format = send_channel->video_format();
3779 UpdateVideoCodec(video_format, &target_codec);
3780
3781 // Vie send codec size should not exceed target_codec.
3782 int target_width = new_width;
3783 int target_height = new_height;
3784 if (!is_screencast &&
3785 (new_width > target_codec.width || new_height > target_codec.height)) {
3786 target_width = target_codec.width;
3787 target_height = target_codec.height;
3788 }
3789
3790 // Get current vie codec.
3791 webrtc::VideoCodec vie_codec;
3792 const int channel_id = send_channel->channel_id();
3793 if (engine()->vie()->codec()->GetSendCodec(channel_id, vie_codec) != 0) {
3794 LOG_RTCERR1(GetSendCodec, channel_id);
3795 return false;
3796 }
3797 const int cur_width = vie_codec.width;
3798 const int cur_height = vie_codec.height;
3799
3800 // Only reset send codec when there is a size change. Additionally,
3801 // automatic resize needs to be turned off when screencasting and on when
3802 // not screencasting.
3803 // Don't allow automatic resizing for screencasting.
3804 bool automatic_resize = !is_screencast;
3805 // Turn off VP8 frame dropping when screensharing as the current model does
3806 // not work well at low fps.
3807 bool vp8_frame_dropping = !is_screencast;
3808 // Disable denoising for screencasting.
3809 bool enable_denoising =
3810 options_.video_noise_reduction.GetWithDefaultIfUnset(false);
henrike@webrtc.orgdce3feb2014-03-26 01:17:30 +00003811#ifdef USE_WEBRTC_DEV_BRANCH
3812 int screencast_min_bitrate =
3813 options_.screencast_min_bitrate.GetWithDefaultIfUnset(0);
3814 bool leaky_bucket = options_.video_leaky_bucket.GetWithDefaultIfUnset(false);
3815#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003816 bool denoising = !is_screencast && enable_denoising;
3817 bool reset_send_codec =
3818 target_width != cur_width || target_height != cur_height ||
3819 automatic_resize != vie_codec.codecSpecific.VP8.automaticResizeOn ||
3820 denoising != vie_codec.codecSpecific.VP8.denoisingOn ||
3821 vp8_frame_dropping != vie_codec.codecSpecific.VP8.frameDroppingOn;
3822
3823 if (reset_send_codec) {
3824 // Set the new codec on vie.
3825 vie_codec.width = target_width;
3826 vie_codec.height = target_height;
3827 vie_codec.maxFramerate = target_codec.maxFramerate;
3828 vie_codec.startBitrate = target_codec.startBitrate;
henrike@webrtc.org6e3dbc22014-03-25 17:09:47 +00003829#ifdef USE_WEBRTC_DEV_BRANCH
3830 vie_codec.targetBitrate = 0;
3831#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003832 vie_codec.codecSpecific.VP8.automaticResizeOn = automatic_resize;
3833 vie_codec.codecSpecific.VP8.denoisingOn = denoising;
3834 vie_codec.codecSpecific.VP8.frameDroppingOn = vp8_frame_dropping;
henrike@webrtc.org6e3dbc22014-03-25 17:09:47 +00003835 bool maybe_change_start_bitrate = !is_screencast;
3836#ifdef USE_WEBRTC_DEV_BRANCH
3837 // TODO(pbos): When USE_WEBRTC_DEV_BRANCH is removed, remove
3838 // maybe_change_start_bitrate as well. MaybeChangeStartBitrate should be
3839 // called for all content.
3840 maybe_change_start_bitrate = true;
3841#endif
3842 if (maybe_change_start_bitrate)
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003843 MaybeChangeStartBitrate(channel_id, &vie_codec);
3844
3845 if (engine()->vie()->codec()->SetSendCodec(channel_id, vie_codec) != 0) {
3846 LOG_RTCERR1(SetSendCodec, channel_id);
3847 return false;
3848 }
henrike@webrtc.orgdce3feb2014-03-26 01:17:30 +00003849
3850#ifdef USE_WEBRTC_DEV_BRANCH
3851 if (is_screencast) {
3852 engine()->vie()->rtp()->SetMinTransmitBitrate(channel_id,
3853 screencast_min_bitrate);
3854 // If screencast and min bitrate set, force enable pacer.
3855 if (screencast_min_bitrate > 0) {
3856 engine()->vie()->rtp()->SetTransmissionSmoothingStatus(channel_id,
3857 true);
3858 }
3859 } else {
3860 // In case of switching from screencast to regular capture, set
3861 // min bitrate padding and pacer back to defaults.
3862 engine()->vie()->rtp()->SetMinTransmitBitrate(channel_id, 0);
3863 engine()->vie()->rtp()->SetTransmissionSmoothingStatus(channel_id,
3864 leaky_bucket);
3865 }
3866#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003867 if (reset) {
3868 *reset = true;
3869 }
3870 LogSendCodecChange("Capture size changed");
3871 }
3872
3873 return true;
3874}
3875
3876void WebRtcVideoMediaChannel::MaybeChangeStartBitrate(
3877 int channel_id, webrtc::VideoCodec* video_codec) {
3878 if (video_codec->startBitrate < video_codec->minBitrate) {
3879 video_codec->startBitrate = video_codec->minBitrate;
3880 } else if (video_codec->startBitrate > video_codec->maxBitrate) {
3881 video_codec->startBitrate = video_codec->maxBitrate;
3882 }
3883
3884 // Use a previous target bitrate, if there is one.
3885 unsigned int current_target_bitrate = 0;
3886 if (engine()->vie()->codec()->GetCodecTargetBitrate(
3887 channel_id, &current_target_bitrate) == 0) {
3888 // Convert to kbps.
3889 current_target_bitrate /= 1000;
3890 if (current_target_bitrate > video_codec->maxBitrate) {
3891 current_target_bitrate = video_codec->maxBitrate;
3892 }
3893 if (current_target_bitrate > video_codec->startBitrate) {
3894 video_codec->startBitrate = current_target_bitrate;
3895 }
3896 }
3897}
3898
3899void WebRtcVideoMediaChannel::OnMessage(talk_base::Message* msg) {
3900 FlushBlackFrameData* black_frame_data =
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00003901 static_cast<FlushBlackFrameData*>(msg->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003902 FlushBlackFrame(black_frame_data->ssrc, black_frame_data->timestamp);
3903 delete black_frame_data;
3904}
3905
3906int WebRtcVideoMediaChannel::SendPacket(int channel, const void* data,
3907 int len) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003908 talk_base::Buffer packet(data, len, kMaxRtpPacketLen);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00003909 return MediaChannel::SendPacket(&packet) ? len : -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003910}
3911
3912int WebRtcVideoMediaChannel::SendRTCPPacket(int channel,
3913 const void* data,
3914 int len) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003915 talk_base::Buffer packet(data, len, kMaxRtpPacketLen);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00003916 return MediaChannel::SendRtcp(&packet) ? len : -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003917}
3918
3919void WebRtcVideoMediaChannel::QueueBlackFrame(uint32 ssrc, int64 timestamp,
3920 int framerate) {
3921 if (timestamp) {
3922 FlushBlackFrameData* black_frame_data = new FlushBlackFrameData(
3923 ssrc,
3924 timestamp);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00003925 const int delay_ms = static_cast<int>(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003926 2 * cricket::VideoFormat::FpsToInterval(framerate) *
3927 talk_base::kNumMillisecsPerSec / talk_base::kNumNanosecsPerSec);
3928 worker_thread()->PostDelayed(delay_ms, this, 0, black_frame_data);
3929 }
3930}
3931
3932void WebRtcVideoMediaChannel::FlushBlackFrame(uint32 ssrc, int64 timestamp) {
3933 WebRtcVideoChannelSendInfo* send_channel = GetSendChannel(ssrc);
3934 if (!send_channel) {
3935 return;
3936 }
3937 talk_base::scoped_ptr<const VideoFrame> black_frame_ptr;
3938
3939 const WebRtcLocalStreamInfo* channel_stream_info =
3940 send_channel->local_stream_info();
3941 int64 last_frame_time_stamp = channel_stream_info->time_stamp();
3942 if (last_frame_time_stamp == timestamp) {
3943 size_t last_frame_width = 0;
3944 size_t last_frame_height = 0;
3945 int64 last_frame_elapsed_time = 0;
3946 channel_stream_info->GetLastFrameInfo(&last_frame_width, &last_frame_height,
3947 &last_frame_elapsed_time);
3948 if (!last_frame_width || !last_frame_height) {
3949 return;
3950 }
3951 WebRtcVideoFrame black_frame;
3952 // Black frame is not screencast.
3953 const bool screencasting = false;
3954 const int64 timestamp_delta = send_channel->interval();
3955 if (!black_frame.InitToBlack(send_codec_->width, send_codec_->height, 1, 1,
3956 last_frame_elapsed_time + timestamp_delta,
3957 last_frame_time_stamp + timestamp_delta) ||
3958 !SendFrame(send_channel, &black_frame, screencasting)) {
3959 LOG(LS_ERROR) << "Failed to send black frame.";
3960 }
3961 }
3962}
3963
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00003964void WebRtcVideoMediaChannel::OnCpuAdaptationUnable() {
3965 // ssrc is hardcoded to 0. This message is based on a system wide issue,
3966 // so finding which ssrc caused it doesn't matter.
3967 SignalMediaError(0, VideoMediaChannel::ERROR_REC_CPU_MAX_CANT_DOWNGRADE);
3968}
3969
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003970void WebRtcVideoMediaChannel::SetNetworkTransmissionState(
3971 bool is_transmitting) {
3972 LOG(LS_INFO) << "SetNetworkTransmissionState: " << is_transmitting;
3973 for (SendChannelMap::iterator iter = send_channels_.begin();
3974 iter != send_channels_.end(); ++iter) {
3975 WebRtcVideoChannelSendInfo* send_channel = iter->second;
3976 int channel_id = send_channel->channel_id();
3977 engine_->vie()->network()->SetNetworkTransmissionState(channel_id,
3978 is_transmitting);
3979 }
3980}
3981
3982bool WebRtcVideoMediaChannel::SetHeaderExtension(ExtensionSetterFunction setter,
3983 int channel_id, const RtpHeaderExtension* extension) {
3984 bool enable = false;
3985 int id = 0;
3986 if (extension) {
3987 enable = true;
3988 id = extension->id;
3989 }
3990 if ((engine_->vie()->rtp()->*setter)(channel_id, enable, id) != 0) {
3991 LOG_RTCERR4(*setter, extension->uri, channel_id, enable, id);
3992 return false;
3993 }
3994 return true;
3995}
3996
3997bool WebRtcVideoMediaChannel::SetHeaderExtension(ExtensionSetterFunction setter,
3998 int channel_id, const std::vector<RtpHeaderExtension>& extensions,
3999 const char header_extension_uri[]) {
4000 const RtpHeaderExtension* extension = FindHeaderExtension(extensions,
4001 header_extension_uri);
4002 return SetHeaderExtension(setter, channel_id, extension);
4003}
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00004004
4005bool WebRtcVideoMediaChannel::SetLocalRtxSsrc(int channel_id,
4006 const StreamParams& send_params,
4007 uint32 primary_ssrc,
4008 int stream_idx) {
4009 uint32 rtx_ssrc = 0;
4010 bool has_rtx = send_params.GetFidSsrc(primary_ssrc, &rtx_ssrc);
4011 if (has_rtx && engine()->vie()->rtp()->SetLocalSSRC(
4012 channel_id, rtx_ssrc, webrtc::kViEStreamTypeRtx, stream_idx) != 0) {
4013 LOG_RTCERR4(SetLocalSSRC, channel_id, rtx_ssrc,
4014 webrtc::kViEStreamTypeRtx, stream_idx);
4015 return false;
4016 }
4017 return true;
4018}
4019
wu@webrtc.org24301a62013-12-13 19:17:43 +00004020void WebRtcVideoMediaChannel::MaybeConnectCapturer(VideoCapturer* capturer) {
4021 if (capturer != NULL && GetSendChannelNum(capturer) == 1) {
wu@webrtc.orgf7d501d2014-03-27 23:48:25 +00004022 capturer->SignalVideoFrame.connect(this,
4023 &WebRtcVideoMediaChannel::SendFrame);
wu@webrtc.org24301a62013-12-13 19:17:43 +00004024 }
4025}
4026
4027void WebRtcVideoMediaChannel::MaybeDisconnectCapturer(VideoCapturer* capturer) {
4028 if (capturer != NULL && GetSendChannelNum(capturer) == 1) {
4029 capturer->SignalVideoFrame.disconnect(this);
4030 }
4031}
4032
henrike@webrtc.org28e20752013-07-10 00:45:36 +00004033} // namespace cricket
4034
4035#endif // HAVE_WEBRTC_VIDEO