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