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