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