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