blob: bf731c4ca5ea653972e429af930760816c58509f [file] [log] [blame]
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001/*
2 * libjingle
3 * Copyright 2014 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/webrtcvideoengine2.h"
30
31#ifdef HAVE_CONFIG_H
32#include <config.h>
33#endif
34
35#include <math.h>
36
37#include <string>
38
39#include "libyuv/convert_from.h"
40#include "talk/base/buffer.h"
41#include "talk/base/logging.h"
42#include "talk/base/stringutils.h"
43#include "talk/media/base/videocapturer.h"
44#include "talk/media/base/videorenderer.h"
45#include "talk/media/webrtc/webrtcvideocapturer.h"
46#include "talk/media/webrtc/webrtcvideoframe.h"
47#include "talk/media/webrtc/webrtcvoiceengine.h"
48#include "webrtc/call.h"
49// TODO(pbos): Move codecs out of modules (webrtc:3070).
50#include "webrtc/modules/video_coding/codecs/vp8/include/vp8.h"
51
52#define UNIMPLEMENTED \
53 LOG(LS_ERROR) << "Call to unimplemented function " << __FUNCTION__; \
54 ASSERT(false)
55
56namespace cricket {
57
58static const int kCpuMonitorPeriodMs = 2000; // 2 seconds.
59
60// This constant is really an on/off, lower-level configurable NACK history
61// duration hasn't been implemented.
62static const int kNackHistoryMs = 1000;
63
64static const int kDefaultFramerate = 30;
65static const int kMinVideoBitrate = 50;
66static const int kMaxVideoBitrate = 2000;
67
68static const int kVideoMtu = 1200;
69static const int kVideoRtpBufferSize = 65536;
70
71static const char kVp8PayloadName[] = "VP8";
72
73static const int kDefaultRtcpReceiverReportSsrc = 1;
74
75struct VideoCodecPref {
76 int payload_type;
77 const char* name;
78 int rtx_payload_type;
79} kDefaultVideoCodecPref = {100, kVp8PayloadName, 96};
80
81VideoCodecPref kRedPref = {116, kRedCodecName, -1};
82VideoCodecPref kUlpfecPref = {117, kUlpfecCodecName, -1};
83
84// The formats are sorted by the descending order of width. We use the order to
85// find the next format for CPU and bandwidth adaptation.
86const VideoFormatPod kDefaultVideoFormat = {
87 640, 400, FPS_TO_INTERVAL(kDefaultFramerate), FOURCC_ANY};
88const VideoFormatPod kVideoFormats[] = {
89 {1280, 800, FPS_TO_INTERVAL(kDefaultFramerate), FOURCC_ANY},
90 {1280, 720, FPS_TO_INTERVAL(kDefaultFramerate), FOURCC_ANY},
91 {960, 600, FPS_TO_INTERVAL(kDefaultFramerate), FOURCC_ANY},
92 {960, 540, FPS_TO_INTERVAL(kDefaultFramerate), FOURCC_ANY},
93 kDefaultVideoFormat,
94 {640, 360, FPS_TO_INTERVAL(kDefaultFramerate), FOURCC_ANY},
95 {640, 480, FPS_TO_INTERVAL(kDefaultFramerate), FOURCC_ANY},
96 {480, 300, FPS_TO_INTERVAL(kDefaultFramerate), FOURCC_ANY},
97 {480, 270, FPS_TO_INTERVAL(kDefaultFramerate), FOURCC_ANY},
98 {480, 360, FPS_TO_INTERVAL(kDefaultFramerate), FOURCC_ANY},
99 {320, 200, FPS_TO_INTERVAL(kDefaultFramerate), FOURCC_ANY},
100 {320, 180, FPS_TO_INTERVAL(kDefaultFramerate), FOURCC_ANY},
101 {320, 240, FPS_TO_INTERVAL(kDefaultFramerate), FOURCC_ANY},
102 {240, 150, FPS_TO_INTERVAL(kDefaultFramerate), FOURCC_ANY},
103 {240, 135, FPS_TO_INTERVAL(kDefaultFramerate), FOURCC_ANY},
104 {240, 180, FPS_TO_INTERVAL(kDefaultFramerate), FOURCC_ANY},
105 {160, 100, FPS_TO_INTERVAL(kDefaultFramerate), FOURCC_ANY},
106 {160, 90, FPS_TO_INTERVAL(kDefaultFramerate), FOURCC_ANY},
107 {160, 120, FPS_TO_INTERVAL(kDefaultFramerate), FOURCC_ANY}, };
108
109static bool FindFirstMatchingCodec(const std::vector<VideoCodec>& codecs,
110 const VideoCodec& requested_codec,
111 VideoCodec* matching_codec) {
112 for (size_t i = 0; i < codecs.size(); ++i) {
113 if (requested_codec.Matches(codecs[i])) {
114 *matching_codec = codecs[i];
115 return true;
116 }
117 }
118 return false;
119}
120static bool FindBestVideoFormat(int max_width,
121 int max_height,
122 int aspect_width,
123 int aspect_height,
124 VideoFormat* video_format) {
125 assert(max_width > 0);
126 assert(max_height > 0);
127 assert(aspect_width > 0);
128 assert(aspect_height > 0);
129 VideoFormat best_format;
130 for (int i = 0; i < ARRAY_SIZE(kVideoFormats); ++i) {
131 const VideoFormat format(kVideoFormats[i]);
132
133 // Skip any format that is larger than the local or remote maximums, or
134 // smaller than the current best match
135 if (format.width > max_width || format.height > max_height ||
136 (format.width < best_format.width &&
137 format.height < best_format.height)) {
138 continue;
139 }
140
141 // If we don't have any matches yet, this is the best so far.
142 if (best_format.width == 0) {
143 best_format = format;
144 continue;
145 }
146
147 // Prefer closer aspect ratios i.e:
148 // |format| aspect - requested aspect <
149 // |best_format| aspect - requested aspect
150 if (abs(format.width * aspect_height * best_format.height -
151 aspect_width * format.height * best_format.height) <
152 abs(best_format.width * aspect_height * format.height -
153 aspect_width * format.height * best_format.height)) {
154 best_format = format;
155 }
156 }
157 if (best_format.width != 0) {
158 *video_format = best_format;
159 return true;
160 }
161 return false;
162}
163
164static VideoCodec DefaultVideoCodec() {
165 VideoCodec default_codec(kDefaultVideoCodecPref.payload_type,
166 kDefaultVideoCodecPref.name,
167 kDefaultVideoFormat.width,
168 kDefaultVideoFormat.height,
169 kDefaultFramerate,
170 0);
171 return default_codec;
172}
173
174static VideoCodec DefaultRedCodec() {
175 return VideoCodec(kRedPref.payload_type, kRedPref.name, 0, 0, 0, 0);
176}
177
178static VideoCodec DefaultUlpfecCodec() {
179 return VideoCodec(kUlpfecPref.payload_type, kUlpfecPref.name, 0, 0, 0, 0);
180}
181
182static std::vector<VideoCodec> DefaultVideoCodecs() {
183 std::vector<VideoCodec> codecs;
184 codecs.push_back(DefaultVideoCodec());
185 codecs.push_back(DefaultRedCodec());
186 codecs.push_back(DefaultUlpfecCodec());
187 if (kDefaultVideoCodecPref.rtx_payload_type != -1) {
188 codecs.push_back(
189 VideoCodec::CreateRtxCodec(kDefaultVideoCodecPref.rtx_payload_type,
190 kDefaultVideoCodecPref.payload_type));
191 }
192 return codecs;
193}
194
pbos@webrtc.org0d523ee2014-06-05 09:10:55 +0000195WebRtcVideoEncoderFactory2::~WebRtcVideoEncoderFactory2() {
196}
197
buildbot@webrtc.orgd41eaeb2014-06-12 07:13:26 +0000198std::vector<webrtc::VideoStream> WebRtcVideoEncoderFactory2::CreateVideoStreams(
199 const VideoCodec& codec,
200 const VideoOptions& options,
201 size_t num_streams) {
202 assert(SupportsCodec(codec));
203 if (num_streams != 1) {
204 LOG(LS_ERROR) << "Unsupported number of streams: " << num_streams;
205 return std::vector<webrtc::VideoStream>();
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000206 }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000207
buildbot@webrtc.orgd41eaeb2014-06-12 07:13:26 +0000208 webrtc::VideoStream stream;
209 stream.width = codec.width;
210 stream.height = codec.height;
211 stream.max_framerate =
212 codec.framerate != 0 ? codec.framerate : kDefaultFramerate;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000213
buildbot@webrtc.orgd41eaeb2014-06-12 07:13:26 +0000214 int min_bitrate = kMinVideoBitrate;
215 codec.GetParam(kCodecParamMinBitrate, &min_bitrate);
216 int max_bitrate = kMaxVideoBitrate;
217 codec.GetParam(kCodecParamMaxBitrate, &max_bitrate);
218 stream.min_bitrate_bps = min_bitrate * 1000;
219 stream.target_bitrate_bps = stream.max_bitrate_bps = max_bitrate * 1000;
220
221 int max_qp = 56;
222 codec.GetParam(kCodecParamMaxQuantization, &max_qp);
223 stream.max_qp = max_qp;
224 std::vector<webrtc::VideoStream> streams;
225 streams.push_back(stream);
226 return streams;
227}
228
229webrtc::VideoEncoder* WebRtcVideoEncoderFactory2::CreateVideoEncoder(
230 const VideoCodec& codec,
231 const VideoOptions& options) {
232 assert(SupportsCodec(codec));
233 return webrtc::VP8Encoder::Create();
234}
235
236bool WebRtcVideoEncoderFactory2::SupportsCodec(const VideoCodec& codec) {
237 return _stricmp(codec.name.c_str(), kVp8PayloadName) == 0;
238}
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000239
240WebRtcVideoEngine2::WebRtcVideoEngine2() {
241 // Construct without a factory or voice engine.
242 Construct(NULL, NULL, new talk_base::CpuMonitor(NULL));
243}
244
245WebRtcVideoEngine2::WebRtcVideoEngine2(
246 WebRtcVideoChannelFactory* channel_factory) {
247 // Construct without a voice engine.
248 Construct(channel_factory, NULL, new talk_base::CpuMonitor(NULL));
249}
250
251void WebRtcVideoEngine2::Construct(WebRtcVideoChannelFactory* channel_factory,
252 WebRtcVoiceEngine* voice_engine,
253 talk_base::CpuMonitor* cpu_monitor) {
254 LOG(LS_INFO) << "WebRtcVideoEngine2::WebRtcVideoEngine2";
255 worker_thread_ = NULL;
256 voice_engine_ = voice_engine;
257 initialized_ = false;
258 capture_started_ = false;
259 cpu_monitor_.reset(cpu_monitor);
260 channel_factory_ = channel_factory;
261
262 video_codecs_ = DefaultVideoCodecs();
263 default_codec_format_ = VideoFormat(kDefaultVideoFormat);
264}
265
266WebRtcVideoEngine2::~WebRtcVideoEngine2() {
267 LOG(LS_INFO) << "WebRtcVideoEngine2::~WebRtcVideoEngine2";
268
269 if (initialized_) {
270 Terminate();
271 }
272}
273
274bool WebRtcVideoEngine2::Init(talk_base::Thread* worker_thread) {
275 LOG(LS_INFO) << "WebRtcVideoEngine2::Init";
276 worker_thread_ = worker_thread;
277 ASSERT(worker_thread_ != NULL);
278
279 cpu_monitor_->set_thread(worker_thread_);
280 if (!cpu_monitor_->Start(kCpuMonitorPeriodMs)) {
281 LOG(LS_ERROR) << "Failed to start CPU monitor.";
282 cpu_monitor_.reset();
283 }
284
285 initialized_ = true;
286 return true;
287}
288
289void WebRtcVideoEngine2::Terminate() {
290 LOG(LS_INFO) << "WebRtcVideoEngine2::Terminate";
291
292 cpu_monitor_->Stop();
293
294 initialized_ = false;
295}
296
297int WebRtcVideoEngine2::GetCapabilities() { return VIDEO_RECV | VIDEO_SEND; }
298
299bool WebRtcVideoEngine2::SetOptions(const VideoOptions& options) {
300 // TODO(pbos): Do we need this? This is a no-op in the existing
301 // WebRtcVideoEngine implementation.
302 LOG(LS_VERBOSE) << "SetOptions: " << options.ToString();
303 // options_ = options;
304 return true;
305}
306
307bool WebRtcVideoEngine2::SetDefaultEncoderConfig(
308 const VideoEncoderConfig& config) {
309 // TODO(pbos): Implement. Should be covered by corresponding unit tests.
310 LOG(LS_VERBOSE) << "SetDefaultEncoderConfig()";
311 return true;
312}
313
314VideoEncoderConfig WebRtcVideoEngine2::GetDefaultEncoderConfig() const {
315 return VideoEncoderConfig(DefaultVideoCodec());
316}
317
318WebRtcVideoChannel2* WebRtcVideoEngine2::CreateChannel(
319 VoiceMediaChannel* voice_channel) {
320 LOG(LS_INFO) << "CreateChannel: "
321 << (voice_channel != NULL ? "With" : "Without")
322 << " voice channel.";
323 WebRtcVideoChannel2* channel =
324 channel_factory_ != NULL
325 ? channel_factory_->Create(this, voice_channel)
326 : new WebRtcVideoChannel2(
pbos@webrtc.org0d523ee2014-06-05 09:10:55 +0000327 this, voice_channel, GetVideoEncoderFactory());
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000328 if (!channel->Init()) {
329 delete channel;
330 return NULL;
331 }
pbos@webrtc.orge322a172014-06-13 11:47:28 +0000332 channel->SetRecvCodecs(video_codecs_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000333 return channel;
334}
335
336const std::vector<VideoCodec>& WebRtcVideoEngine2::codecs() const {
337 return video_codecs_;
338}
339
340const std::vector<RtpHeaderExtension>&
341WebRtcVideoEngine2::rtp_header_extensions() const {
342 return rtp_header_extensions_;
343}
344
345void WebRtcVideoEngine2::SetLogging(int min_sev, const char* filter) {
346 // TODO(pbos): Set up logging.
347 LOG(LS_VERBOSE) << "SetLogging: " << min_sev << '"' << filter << '"';
348 // if min_sev == -1, we keep the current log level.
349 if (min_sev < 0) {
350 assert(min_sev == -1);
351 return;
352 }
353}
354
355bool WebRtcVideoEngine2::EnableTimedRender() {
356 // TODO(pbos): Figure out whether this can be removed.
357 return true;
358}
359
360bool WebRtcVideoEngine2::SetLocalRenderer(VideoRenderer* renderer) {
361 // TODO(pbos): Implement or remove. Unclear which stream should be rendered
362 // locally even.
363 return true;
364}
365
366// Checks to see whether we comprehend and could receive a particular codec
367bool WebRtcVideoEngine2::FindCodec(const VideoCodec& in) {
368 // TODO(pbos): Probe encoder factory to figure out that the codec is supported
369 // if supported by the encoder factory. Add a corresponding test that fails
370 // with this code (that doesn't ask the factory).
371 for (int i = 0; i < ARRAY_SIZE(kVideoFormats); ++i) {
372 const VideoFormat fmt(kVideoFormats[i]);
373 if ((in.width != 0 || in.height != 0) &&
374 (fmt.width != in.width || fmt.height != in.height)) {
375 continue;
376 }
377 for (size_t j = 0; j < video_codecs_.size(); ++j) {
378 VideoCodec codec(video_codecs_[j].id, video_codecs_[j].name, 0, 0, 0, 0);
379 if (codec.Matches(in)) {
380 return true;
381 }
382 }
383 }
384 return false;
385}
386
387// Tells whether the |requested| codec can be transmitted or not. If it can be
388// transmitted |out| is set with the best settings supported. Aspect ratio will
389// be set as close to |current|'s as possible. If not set |requested|'s
390// dimensions will be used for aspect ratio matching.
391bool WebRtcVideoEngine2::CanSendCodec(const VideoCodec& requested,
392 const VideoCodec& current,
393 VideoCodec* out) {
394 assert(out != NULL);
395 // TODO(pbos): Implement.
396
397 if (requested.width != requested.height &&
398 (requested.height == 0 || requested.width == 0)) {
399 // 0xn and nx0 are invalid resolutions.
400 return false;
401 }
402
403 VideoCodec matching_codec;
404 if (!FindFirstMatchingCodec(video_codecs_, requested, &matching_codec)) {
405 // Codec not supported.
406 return false;
407 }
408
409 // Pick the best quality that is within their and our bounds and has the
410 // correct aspect ratio.
411 VideoFormat format;
412 if (requested.width == 0 && requested.height == 0) {
413 // Special case with resolution 0. The channel should not send frames.
414 } else {
415 int max_width = talk_base::_min(requested.width, matching_codec.width);
416 int max_height = talk_base::_min(requested.height, matching_codec.height);
417 int aspect_width = max_width;
418 int aspect_height = max_height;
419 if (current.width > 0 && current.height > 0) {
420 aspect_width = current.width;
421 aspect_height = current.height;
422 }
423 if (!FindBestVideoFormat(
424 max_width, max_height, aspect_width, aspect_height, &format)) {
425 return false;
426 }
427 }
428
429 out->id = requested.id;
430 out->name = requested.name;
431 out->preference = requested.preference;
432 out->params = requested.params;
433 out->framerate =
434 talk_base::_min(requested.framerate, matching_codec.framerate);
435 out->width = format.width;
436 out->height = format.height;
437 out->params = requested.params;
438 out->feedback_params = requested.feedback_params;
439 return true;
440}
441
442bool WebRtcVideoEngine2::SetVoiceEngine(WebRtcVoiceEngine* voice_engine) {
443 if (initialized_) {
444 LOG(LS_WARNING) << "SetVoiceEngine can not be called after Init";
445 return false;
446 }
447 voice_engine_ = voice_engine;
448 return true;
449}
450
451// Ignore spammy trace messages, mostly from the stats API when we haven't
452// gotten RTCP info yet from the remote side.
453bool WebRtcVideoEngine2::ShouldIgnoreTrace(const std::string& trace) {
454 static const char* const kTracesToIgnore[] = {NULL};
455 for (const char* const* p = kTracesToIgnore; *p; ++p) {
456 if (trace.find(*p) == 0) {
457 return true;
458 }
459 }
460 return false;
461}
462
buildbot@webrtc.orgd41eaeb2014-06-12 07:13:26 +0000463WebRtcVideoEncoderFactory2* WebRtcVideoEngine2::GetVideoEncoderFactory() {
464 return &default_video_encoder_factory_;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000465}
466
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000467// Thin map between VideoFrame and an existing webrtc::I420VideoFrame
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000468// to avoid having to copy the rendered VideoFrame prematurely.
469// This implementation is only safe to use in a const context and should never
470// be written to.
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000471class WebRtcVideoRenderFrame : public VideoFrame {
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000472 public:
473 explicit WebRtcVideoRenderFrame(const webrtc::I420VideoFrame* frame)
474 : frame_(frame) {}
475
476 virtual bool InitToBlack(int w,
477 int h,
478 size_t pixel_width,
479 size_t pixel_height,
480 int64 elapsed_time,
481 int64 time_stamp) OVERRIDE {
482 UNIMPLEMENTED;
483 return false;
484 }
485
486 virtual bool Reset(uint32 fourcc,
487 int w,
488 int h,
489 int dw,
490 int dh,
491 uint8* sample,
492 size_t sample_size,
493 size_t pixel_width,
494 size_t pixel_height,
495 int64 elapsed_time,
496 int64 time_stamp,
497 int rotation) OVERRIDE {
498 UNIMPLEMENTED;
499 return false;
500 }
501
502 virtual size_t GetWidth() const OVERRIDE {
503 return static_cast<size_t>(frame_->width());
504 }
505 virtual size_t GetHeight() const OVERRIDE {
506 return static_cast<size_t>(frame_->height());
507 }
508
509 virtual const uint8* GetYPlane() const OVERRIDE {
510 return frame_->buffer(webrtc::kYPlane);
511 }
512 virtual const uint8* GetUPlane() const OVERRIDE {
513 return frame_->buffer(webrtc::kUPlane);
514 }
515 virtual const uint8* GetVPlane() const OVERRIDE {
516 return frame_->buffer(webrtc::kVPlane);
517 }
518
519 virtual uint8* GetYPlane() OVERRIDE {
520 UNIMPLEMENTED;
521 return NULL;
522 }
523 virtual uint8* GetUPlane() OVERRIDE {
524 UNIMPLEMENTED;
525 return NULL;
526 }
527 virtual uint8* GetVPlane() OVERRIDE {
528 UNIMPLEMENTED;
529 return NULL;
530 }
531
532 virtual int32 GetYPitch() const OVERRIDE {
533 return frame_->stride(webrtc::kYPlane);
534 }
535 virtual int32 GetUPitch() const OVERRIDE {
536 return frame_->stride(webrtc::kUPlane);
537 }
538 virtual int32 GetVPitch() const OVERRIDE {
539 return frame_->stride(webrtc::kVPlane);
540 }
541
542 virtual void* GetNativeHandle() const OVERRIDE { return NULL; }
543
544 virtual size_t GetPixelWidth() const OVERRIDE { return 1; }
545 virtual size_t GetPixelHeight() const OVERRIDE { return 1; }
546
547 virtual int64 GetElapsedTime() const OVERRIDE {
548 // Convert millisecond render time to ns timestamp.
549 return frame_->render_time_ms() * talk_base::kNumNanosecsPerMillisec;
550 }
551 virtual int64 GetTimeStamp() const OVERRIDE {
552 // Convert 90K rtp timestamp to ns timestamp.
553 return (frame_->timestamp() / 90) * talk_base::kNumNanosecsPerMillisec;
554 }
555 virtual void SetElapsedTime(int64 elapsed_time) OVERRIDE { UNIMPLEMENTED; }
556 virtual void SetTimeStamp(int64 time_stamp) OVERRIDE { UNIMPLEMENTED; }
557
558 virtual int GetRotation() const OVERRIDE {
559 UNIMPLEMENTED;
560 return ROTATION_0;
561 }
562
563 virtual VideoFrame* Copy() const OVERRIDE {
564 UNIMPLEMENTED;
565 return NULL;
566 }
567
568 virtual bool MakeExclusive() OVERRIDE {
569 UNIMPLEMENTED;
570 return false;
571 }
572
573 virtual size_t CopyToBuffer(uint8* buffer, size_t size) const {
574 UNIMPLEMENTED;
575 return 0;
576 }
577
578 // TODO(fbarchard): Refactor into base class and share with LMI
579 virtual size_t ConvertToRgbBuffer(uint32 to_fourcc,
580 uint8* buffer,
581 size_t size,
582 int stride_rgb) const OVERRIDE {
583 size_t width = GetWidth();
584 size_t height = GetHeight();
585 size_t needed = (stride_rgb >= 0 ? stride_rgb : -stride_rgb) * height;
586 if (size < needed) {
587 LOG(LS_WARNING) << "RGB buffer is not large enough";
588 return needed;
589 }
590
591 if (libyuv::ConvertFromI420(GetYPlane(),
592 GetYPitch(),
593 GetUPlane(),
594 GetUPitch(),
595 GetVPlane(),
596 GetVPitch(),
597 buffer,
598 stride_rgb,
599 static_cast<int>(width),
600 static_cast<int>(height),
601 to_fourcc)) {
602 LOG(LS_ERROR) << "RGB type not supported: " << to_fourcc;
603 return 0; // 0 indicates error
604 }
605 return needed;
606 }
607
608 protected:
609 virtual VideoFrame* CreateEmptyFrame(int w,
610 int h,
611 size_t pixel_width,
612 size_t pixel_height,
613 int64 elapsed_time,
614 int64 time_stamp) const OVERRIDE {
615 // TODO(pbos): Remove WebRtcVideoFrame dependency, and have a non-const
616 // version of I420VideoFrame wrapped.
617 WebRtcVideoFrame* frame = new WebRtcVideoFrame();
618 frame->InitToBlack(
619 w, h, pixel_width, pixel_height, elapsed_time, time_stamp);
620 return frame;
621 }
622
623 private:
624 const webrtc::I420VideoFrame* const frame_;
625};
626
627WebRtcVideoRenderer::WebRtcVideoRenderer()
628 : last_width_(-1), last_height_(-1), renderer_(NULL) {}
629
630void WebRtcVideoRenderer::RenderFrame(const webrtc::I420VideoFrame& frame,
631 int time_to_render_ms) {
632 talk_base::CritScope crit(&lock_);
633 if (renderer_ == NULL) {
634 LOG(LS_WARNING) << "VideoReceiveStream not connected to a VideoRenderer.";
635 return;
636 }
637
638 if (frame.width() != last_width_ || frame.height() != last_height_) {
639 SetSize(frame.width(), frame.height());
640 }
641
642 LOG(LS_VERBOSE) << "RenderFrame: (" << frame.width() << "x" << frame.height()
643 << ")";
644
645 const WebRtcVideoRenderFrame render_frame(&frame);
646 renderer_->RenderFrame(&render_frame);
647}
648
649void WebRtcVideoRenderer::SetRenderer(cricket::VideoRenderer* renderer) {
650 talk_base::CritScope crit(&lock_);
651 renderer_ = renderer;
652 if (renderer_ != NULL && last_width_ != -1) {
653 SetSize(last_width_, last_height_);
654 }
655}
656
657VideoRenderer* WebRtcVideoRenderer::GetRenderer() {
658 talk_base::CritScope crit(&lock_);
659 return renderer_;
660}
661
662void WebRtcVideoRenderer::SetSize(int width, int height) {
663 talk_base::CritScope crit(&lock_);
664 if (!renderer_->SetSize(width, height, 0)) {
665 LOG(LS_ERROR) << "Could not set renderer size.";
666 }
667 last_width_ = width;
668 last_height_ = height;
669}
670
671// WebRtcVideoChannel2
672
673WebRtcVideoChannel2::WebRtcVideoChannel2(
674 WebRtcVideoEngine2* engine,
675 VoiceMediaChannel* voice_channel,
676 WebRtcVideoEncoderFactory2* encoder_factory)
677 : encoder_factory_(encoder_factory) {
678 // TODO(pbos): Connect the video and audio with |voice_channel|.
679 webrtc::Call::Config config(this);
680 Construct(webrtc::Call::Create(config), engine);
681}
682
683WebRtcVideoChannel2::WebRtcVideoChannel2(
684 webrtc::Call* call,
685 WebRtcVideoEngine2* engine,
686 WebRtcVideoEncoderFactory2* encoder_factory)
687 : encoder_factory_(encoder_factory) {
688 Construct(call, engine);
689}
690
691void WebRtcVideoChannel2::Construct(webrtc::Call* call,
692 WebRtcVideoEngine2* engine) {
693 rtcp_receiver_report_ssrc_ = kDefaultRtcpReceiverReportSsrc;
694 sending_ = false;
695 call_.reset(call);
696 default_renderer_ = NULL;
697 default_send_ssrc_ = 0;
698 default_recv_ssrc_ = 0;
699}
700
701WebRtcVideoChannel2::~WebRtcVideoChannel2() {
702 for (std::map<uint32, WebRtcVideoSendStream*>::iterator it =
703 send_streams_.begin();
704 it != send_streams_.end();
705 ++it) {
706 delete it->second;
707 }
708
709 for (std::map<uint32, webrtc::VideoReceiveStream*>::iterator it =
710 receive_streams_.begin();
711 it != receive_streams_.end();
712 ++it) {
713 assert(it->second != NULL);
714 call_->DestroyVideoReceiveStream(it->second);
715 }
716
717 for (std::map<uint32, WebRtcVideoRenderer*>::iterator it = renderers_.begin();
718 it != renderers_.end();
719 ++it) {
720 assert(it->second != NULL);
721 delete it->second;
722 }
723}
724
725bool WebRtcVideoChannel2::Init() { return true; }
726
727namespace {
728
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000729static std::string CodecVectorToString(const std::vector<VideoCodec>& codecs) {
730 std::stringstream out;
731 out << '{';
732 for (size_t i = 0; i < codecs.size(); ++i) {
733 out << codecs[i].ToString();
734 if (i != codecs.size() - 1) {
735 out << ", ";
736 }
737 }
738 out << '}';
739 return out.str();
740}
741
pbos@webrtc.orge322a172014-06-13 11:47:28 +0000742static bool ValidateCodecFormats(const std::vector<VideoCodec>& codecs) {
743 bool has_video = false;
744 for (size_t i = 0; i < codecs.size(); ++i) {
745 if (!codecs[i].ValidateCodecFormat()) {
746 return false;
747 }
748 if (codecs[i].GetCodecType() == VideoCodec::CODEC_VIDEO) {
749 has_video = true;
750 }
751 }
752 if (!has_video) {
753 LOG(LS_ERROR) << "Setting codecs without a video codec is invalid: "
754 << CodecVectorToString(codecs);
755 return false;
756 }
757 return true;
758}
759
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000760} // namespace
761
762bool WebRtcVideoChannel2::SetRecvCodecs(const std::vector<VideoCodec>& codecs) {
763 // TODO(pbos): Must these receive codecs propagate to existing receive
764 // streams?
765 LOG(LS_INFO) << "SetRecvCodecs: " << CodecVectorToString(codecs);
766 if (!ValidateCodecFormats(codecs)) {
767 return false;
768 }
769
770 const std::vector<VideoCodecSettings> mapped_codecs = MapCodecs(codecs);
771 if (mapped_codecs.empty()) {
772 LOG(LS_ERROR) << "SetRecvCodecs called without video codec payloads.";
773 return false;
774 }
775
776 // TODO(pbos): Add a decoder factory which controls supported codecs.
777 // Blocked on webrtc:2854.
778 for (size_t i = 0; i < mapped_codecs.size(); ++i) {
779 if (_stricmp(mapped_codecs[i].codec.name.c_str(), kVp8PayloadName) != 0) {
780 LOG(LS_ERROR) << "SetRecvCodecs called with unsupported codec: '"
781 << mapped_codecs[i].codec.name << "'";
782 return false;
783 }
784 }
785
786 recv_codecs_ = mapped_codecs;
787 return true;
788}
789
790bool WebRtcVideoChannel2::SetSendCodecs(const std::vector<VideoCodec>& codecs) {
791 LOG(LS_INFO) << "SetSendCodecs: " << CodecVectorToString(codecs);
792 if (!ValidateCodecFormats(codecs)) {
793 return false;
794 }
795
796 const std::vector<VideoCodecSettings> supported_codecs =
797 FilterSupportedCodecs(MapCodecs(codecs));
798
799 if (supported_codecs.empty()) {
800 LOG(LS_ERROR) << "No video codecs supported by encoder factory.";
801 return false;
802 }
803
804 send_codec_.Set(supported_codecs.front());
805 LOG(LS_INFO) << "Using codec: " << supported_codecs.front().codec.ToString();
806
807 SetCodecForAllSendStreams(supported_codecs.front());
808
809 return true;
810}
811
812bool WebRtcVideoChannel2::GetSendCodec(VideoCodec* codec) {
813 VideoCodecSettings codec_settings;
814 if (!send_codec_.Get(&codec_settings)) {
815 LOG(LS_VERBOSE) << "GetSendCodec: No send codec set.";
816 return false;
817 }
818 *codec = codec_settings.codec;
819 return true;
820}
821
822bool WebRtcVideoChannel2::SetSendStreamFormat(uint32 ssrc,
823 const VideoFormat& format) {
824 LOG(LS_VERBOSE) << "SetSendStreamFormat:" << ssrc << " -> "
825 << format.ToString();
826 if (send_streams_.find(ssrc) == send_streams_.end()) {
827 return false;
828 }
829 return send_streams_[ssrc]->SetVideoFormat(format);
830}
831
832bool WebRtcVideoChannel2::SetRender(bool render) {
833 // TODO(pbos): Implement. Or refactor away as it shouldn't be needed.
834 LOG(LS_VERBOSE) << "SetRender: " << (render ? "true" : "false");
835 return true;
836}
837
838bool WebRtcVideoChannel2::SetSend(bool send) {
839 LOG(LS_VERBOSE) << "SetSend: " << (send ? "true" : "false");
840 if (send && !send_codec_.IsSet()) {
841 LOG(LS_ERROR) << "SetSend(true) called before setting codec.";
842 return false;
843 }
844 if (send) {
845 StartAllSendStreams();
846 } else {
847 StopAllSendStreams();
848 }
849 sending_ = send;
850 return true;
851}
852
853static bool ConfigureSendSsrcs(webrtc::VideoSendStream::Config* config,
854 const StreamParams& sp) {
855 if (!sp.has_ssrc_groups()) {
856 config->rtp.ssrcs = sp.ssrcs;
857 return true;
858 }
859
860 if (sp.get_ssrc_group(kFecSsrcGroupSemantics) != NULL) {
861 LOG(LS_ERROR) << "Standalone FEC SSRCs not supported.";
862 return false;
863 }
864
pbos@webrtc.orge322a172014-06-13 11:47:28 +0000865 // Map RTX SSRCs.
866 std::vector<uint32_t> ssrcs;
867 std::vector<uint32_t> rtx_ssrcs;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000868 const SsrcGroup* sim_group = sp.get_ssrc_group(kSimSsrcGroupSemantics);
869 if (sim_group == NULL) {
pbos@webrtc.orge322a172014-06-13 11:47:28 +0000870 ssrcs.push_back(sp.first_ssrc());
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000871 uint32_t rtx_ssrc;
pbos@webrtc.orge322a172014-06-13 11:47:28 +0000872 if (!sp.GetFidSsrc(sp.first_ssrc(), &rtx_ssrc)) {
873 LOG(LS_ERROR) << "Could not find FID ssrc for primary SSRC '"
874 << sp.first_ssrc() << "':" << sp.ToString();
875 return false;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000876 }
877 rtx_ssrcs.push_back(rtx_ssrc);
pbos@webrtc.orge322a172014-06-13 11:47:28 +0000878 } else {
879 ssrcs = sim_group->ssrcs;
880 for (size_t i = 0; i < sim_group->ssrcs.size(); ++i) {
881 uint32_t rtx_ssrc;
882 if (!sp.GetFidSsrc(sim_group->ssrcs[i], &rtx_ssrc)) {
883 continue;
884 }
885 rtx_ssrcs.push_back(rtx_ssrc);
886 }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000887 }
pbos@webrtc.orge322a172014-06-13 11:47:28 +0000888 if (!rtx_ssrcs.empty() && ssrcs.size() != rtx_ssrcs.size()) {
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000889 LOG(LS_ERROR)
890 << "RTX SSRCs exist, but don't cover all SSRCs (unsupported): "
891 << sp.ToString();
892 return false;
893 }
894 config->rtp.rtx.ssrcs = rtx_ssrcs;
pbos@webrtc.orge322a172014-06-13 11:47:28 +0000895 config->rtp.ssrcs = ssrcs;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000896 return true;
897}
898
899bool WebRtcVideoChannel2::AddSendStream(const StreamParams& sp) {
900 LOG(LS_INFO) << "AddSendStream: " << sp.ToString();
901 if (sp.ssrcs.empty()) {
902 LOG(LS_ERROR) << "No SSRCs in stream parameters.";
903 return false;
904 }
905
906 uint32 ssrc = sp.first_ssrc();
907 assert(ssrc != 0);
908 // TODO(pbos): Make sure none of sp.ssrcs are used, not just the identifying
909 // ssrc.
910 if (send_streams_.find(ssrc) != send_streams_.end()) {
911 LOG(LS_ERROR) << "Send stream with ssrc '" << ssrc << "' already exists.";
912 return false;
913 }
914
915 webrtc::VideoSendStream::Config config = call_->GetDefaultSendConfig();
916
917 if (!ConfigureSendSsrcs(&config, sp)) {
918 return false;
919 }
920
921 VideoCodecSettings codec_settings;
922 if (!send_codec_.Get(&codec_settings)) {
923 // TODO(pbos): Set up a temporary fake encoder for VideoSendStream instead
924 // of setting default codecs not to break CreateEncoderSettings.
925 SetSendCodecs(DefaultVideoCodecs());
926 assert(send_codec_.IsSet());
927 send_codec_.Get(&codec_settings);
928 // This is only to bring up defaults to make VideoSendStream setup easier
929 // and avoid complexity. We still don't want to allow sending with the
930 // default codec.
931 send_codec_.Clear();
932 }
933
934 // CreateEncoderSettings will allocate a suitable VideoEncoder instance
935 // matching current settings.
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000936 std::vector<webrtc::VideoStream> video_streams =
937 encoder_factory_->CreateVideoStreams(
938 codec_settings.codec, options_, config.rtp.ssrcs.size());
939 if (video_streams.empty()) {
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000940 return false;
941 }
942
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000943 config.encoder_settings.encoder =
944 encoder_factory_->CreateVideoEncoder(codec_settings.codec, options_);
945 config.encoder_settings.payload_name = codec_settings.codec.name;
946 config.encoder_settings.payload_type = codec_settings.codec.id;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000947 config.rtp.c_name = sp.cname;
948 config.rtp.fec = codec_settings.fec;
949 if (!config.rtp.rtx.ssrcs.empty()) {
950 config.rtp.rtx.payload_type = codec_settings.rtx_payload_type;
951 }
952
953 config.rtp.nack.rtp_history_ms = kNackHistoryMs;
954 config.rtp.max_packet_size = kVideoMtu;
955
956 WebRtcVideoSendStream* stream =
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +0000957 new WebRtcVideoSendStream(call_.get(),
958 config,
959 options_,
960 codec_settings.codec,
961 video_streams,
962 encoder_factory_);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000963 send_streams_[ssrc] = stream;
964
965 if (rtcp_receiver_report_ssrc_ == kDefaultRtcpReceiverReportSsrc) {
966 rtcp_receiver_report_ssrc_ = ssrc;
967 }
968 if (default_send_ssrc_ == 0) {
969 default_send_ssrc_ = ssrc;
970 }
971 if (sending_) {
972 stream->Start();
973 }
974
975 return true;
976}
977
978bool WebRtcVideoChannel2::RemoveSendStream(uint32 ssrc) {
979 LOG(LS_INFO) << "RemoveSendStream: " << ssrc;
980
981 if (ssrc == 0) {
982 if (default_send_ssrc_ == 0) {
983 LOG(LS_ERROR) << "No default send stream active.";
984 return false;
985 }
986
987 LOG(LS_VERBOSE) << "Removing default stream: " << default_send_ssrc_;
988 ssrc = default_send_ssrc_;
989 }
990
991 std::map<uint32, WebRtcVideoSendStream*>::iterator it =
992 send_streams_.find(ssrc);
993 if (it == send_streams_.end()) {
994 return false;
995 }
996
997 delete it->second;
998 send_streams_.erase(it);
999
1000 if (ssrc == default_send_ssrc_) {
1001 default_send_ssrc_ = 0;
1002 }
1003
1004 return true;
1005}
1006
1007bool WebRtcVideoChannel2::AddRecvStream(const StreamParams& sp) {
1008 LOG(LS_INFO) << "AddRecvStream: " << sp.ToString();
1009 assert(sp.ssrcs.size() > 0);
1010
1011 uint32 ssrc = sp.first_ssrc();
1012 assert(ssrc != 0); // TODO(pbos): Is this ever valid?
1013 if (default_recv_ssrc_ == 0) {
1014 default_recv_ssrc_ = ssrc;
1015 }
1016
1017 // TODO(pbos): Check if any of the SSRCs overlap.
1018 if (receive_streams_.find(ssrc) != receive_streams_.end()) {
1019 LOG(LS_ERROR) << "Receive stream for SSRC " << ssrc << "already exists.";
1020 return false;
1021 }
1022
1023 webrtc::VideoReceiveStream::Config config = call_->GetDefaultReceiveConfig();
1024 config.rtp.remote_ssrc = ssrc;
1025 config.rtp.local_ssrc = rtcp_receiver_report_ssrc_;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001026
pbos@webrtc.org19864742014-05-30 07:35:47 +00001027 config.rtp.nack.rtp_history_ms = kNackHistoryMs;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001028 config.rtp.remb = true;
1029 // TODO(pbos): This protection is against setting the same local ssrc as
1030 // remote which is not permitted by the lower-level API. RTCP requires a
1031 // corresponding sender SSRC. Figure out what to do when we don't have
1032 // (receive-only) or know a good local SSRC.
1033 if (config.rtp.remote_ssrc == config.rtp.local_ssrc) {
1034 if (config.rtp.local_ssrc != kDefaultRtcpReceiverReportSsrc) {
1035 config.rtp.local_ssrc = kDefaultRtcpReceiverReportSsrc;
1036 } else {
1037 config.rtp.local_ssrc = kDefaultRtcpReceiverReportSsrc + 1;
1038 }
1039 }
1040 bool default_renderer_used = false;
1041 for (std::map<uint32, WebRtcVideoRenderer*>::iterator it = renderers_.begin();
1042 it != renderers_.end();
1043 ++it) {
1044 if (it->second->GetRenderer() == default_renderer_) {
1045 default_renderer_used = true;
1046 break;
1047 }
1048 }
1049
1050 assert(renderers_[ssrc] == NULL);
1051 renderers_[ssrc] = new WebRtcVideoRenderer();
1052 if (!default_renderer_used) {
1053 renderers_[ssrc]->SetRenderer(default_renderer_);
1054 }
1055 config.renderer = renderers_[ssrc];
1056
1057 {
1058 // TODO(pbos): Base receive codecs off recv_codecs_ and set up using a
1059 // DecoderFactory similar to send side. Pending webrtc:2854.
1060 // Also set up default codecs if there's nothing in recv_codecs_.
1061 webrtc::VideoCodec codec;
1062 memset(&codec, 0, sizeof(codec));
1063
1064 codec.plType = kDefaultVideoCodecPref.payload_type;
1065 strcpy(codec.plName, kDefaultVideoCodecPref.name);
1066 codec.codecType = webrtc::kVideoCodecVP8;
1067 codec.codecSpecific.VP8.resilience = webrtc::kResilientStream;
1068 codec.codecSpecific.VP8.numberOfTemporalLayers = 1;
1069 codec.codecSpecific.VP8.denoisingOn = true;
1070 codec.codecSpecific.VP8.errorConcealmentOn = false;
1071 codec.codecSpecific.VP8.automaticResizeOn = false;
1072 codec.codecSpecific.VP8.frameDroppingOn = true;
1073 codec.codecSpecific.VP8.keyFrameInterval = 3000;
1074 // Bitrates don't matter and are ignored for the receiver. This is put in to
1075 // have the current underlying implementation accept the VideoCodec.
1076 codec.minBitrate = codec.startBitrate = codec.maxBitrate = 300;
1077 config.codecs.push_back(codec);
1078 for (size_t i = 0; i < recv_codecs_.size(); ++i) {
1079 if (recv_codecs_[i].codec.id == codec.plType) {
1080 config.rtp.fec = recv_codecs_[i].fec;
pbos@webrtc.orge322a172014-06-13 11:47:28 +00001081 uint32 rtx_ssrc;
1082 if (recv_codecs_[i].rtx_payload_type != -1 &&
1083 sp.GetFidSsrc(ssrc, &rtx_ssrc)) {
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001084 config.rtp.rtx[codec.plType].ssrc = rtx_ssrc;
1085 config.rtp.rtx[codec.plType].payload_type =
1086 recv_codecs_[i].rtx_payload_type;
1087 }
1088 break;
1089 }
1090 }
1091 }
1092
1093 webrtc::VideoReceiveStream* receive_stream =
1094 call_->CreateVideoReceiveStream(config);
1095 assert(receive_stream != NULL);
1096
1097 receive_streams_[ssrc] = receive_stream;
1098 receive_stream->Start();
1099
1100 return true;
1101}
1102
1103bool WebRtcVideoChannel2::RemoveRecvStream(uint32 ssrc) {
1104 LOG(LS_INFO) << "RemoveRecvStream: " << ssrc;
1105 if (ssrc == 0) {
1106 ssrc = default_recv_ssrc_;
1107 }
1108
1109 std::map<uint32, webrtc::VideoReceiveStream*>::iterator stream =
1110 receive_streams_.find(ssrc);
1111 if (stream == receive_streams_.end()) {
1112 LOG(LS_ERROR) << "Stream not found for ssrc: " << ssrc;
1113 return false;
1114 }
1115 call_->DestroyVideoReceiveStream(stream->second);
1116 receive_streams_.erase(stream);
1117
1118 std::map<uint32, WebRtcVideoRenderer*>::iterator renderer =
1119 renderers_.find(ssrc);
1120 assert(renderer != renderers_.end());
1121 delete renderer->second;
1122 renderers_.erase(renderer);
1123
1124 if (ssrc == default_recv_ssrc_) {
1125 default_recv_ssrc_ = 0;
1126 }
1127
1128 return true;
1129}
1130
1131bool WebRtcVideoChannel2::SetRenderer(uint32 ssrc, VideoRenderer* renderer) {
1132 LOG(LS_INFO) << "SetRenderer: ssrc:" << ssrc << " "
1133 << (renderer ? "(ptr)" : "NULL");
1134 bool is_default_ssrc = false;
1135 if (ssrc == 0) {
1136 is_default_ssrc = true;
1137 ssrc = default_recv_ssrc_;
1138 default_renderer_ = renderer;
1139 }
1140
1141 std::map<uint32, WebRtcVideoRenderer*>::iterator it = renderers_.find(ssrc);
1142 if (it == renderers_.end()) {
1143 return is_default_ssrc;
1144 }
1145
1146 it->second->SetRenderer(renderer);
1147 return true;
1148}
1149
1150bool WebRtcVideoChannel2::GetRenderer(uint32 ssrc, VideoRenderer** renderer) {
1151 if (ssrc == 0) {
1152 if (default_renderer_ == NULL) {
1153 return false;
1154 }
1155 *renderer = default_renderer_;
1156 return true;
1157 }
1158
1159 std::map<uint32, WebRtcVideoRenderer*>::iterator it = renderers_.find(ssrc);
1160 if (it == renderers_.end()) {
1161 return false;
1162 }
1163 *renderer = it->second->GetRenderer();
1164 return true;
1165}
1166
1167bool WebRtcVideoChannel2::GetStats(const StatsOptions& options,
1168 VideoMediaInfo* info) {
1169 // TODO(pbos): Implement.
1170 return true;
1171}
1172
1173bool WebRtcVideoChannel2::SetCapturer(uint32 ssrc, VideoCapturer* capturer) {
1174 LOG(LS_INFO) << "SetCapturer: " << ssrc << " -> "
1175 << (capturer != NULL ? "(capturer)" : "NULL");
1176 assert(ssrc != 0);
1177 if (send_streams_.find(ssrc) == send_streams_.end()) {
1178 LOG(LS_ERROR) << "No sending stream on ssrc " << ssrc;
1179 return false;
1180 }
1181 return send_streams_[ssrc]->SetCapturer(capturer);
1182}
1183
1184bool WebRtcVideoChannel2::SendIntraFrame() {
1185 // TODO(pbos): Implement.
1186 LOG(LS_VERBOSE) << "SendIntraFrame().";
1187 return true;
1188}
1189
1190bool WebRtcVideoChannel2::RequestIntraFrame() {
1191 // TODO(pbos): Implement.
1192 LOG(LS_VERBOSE) << "SendIntraFrame().";
1193 return true;
1194}
1195
1196void WebRtcVideoChannel2::OnPacketReceived(
1197 talk_base::Buffer* packet,
1198 const talk_base::PacketTime& packet_time) {
pbos@webrtc.org4e545cc2014-05-14 13:58:13 +00001199 const webrtc::PacketReceiver::DeliveryStatus delivery_result =
1200 call_->Receiver()->DeliverPacket(
1201 reinterpret_cast<const uint8_t*>(packet->data()), packet->length());
1202 switch (delivery_result) {
1203 case webrtc::PacketReceiver::DELIVERY_OK:
1204 return;
1205 case webrtc::PacketReceiver::DELIVERY_PACKET_ERROR:
1206 return;
1207 case webrtc::PacketReceiver::DELIVERY_UNKNOWN_SSRC:
1208 break;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001209 }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001210
1211 uint32 ssrc = 0;
1212 if (default_recv_ssrc_ != 0) { // Already one default stream.
pbos@webrtc.org4e545cc2014-05-14 13:58:13 +00001213 LOG(LS_WARNING) << "Unknown SSRC, but default receive stream already set.";
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001214 return;
1215 }
1216
1217 if (!GetRtpSsrc(packet->data(), packet->length(), &ssrc)) {
1218 return;
1219 }
1220
1221 StreamParams sp;
1222 sp.ssrcs.push_back(ssrc);
pbos@webrtc.orgc34bb3a2014-05-30 07:38:43 +00001223 LOG(LS_INFO) << "Creating default receive stream for SSRC=" << ssrc << ".";
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001224 AddRecvStream(sp);
1225
pbos@webrtc.org1e019d12014-05-16 11:38:45 +00001226 if (call_->Receiver()->DeliverPacket(
1227 reinterpret_cast<const uint8_t*>(packet->data()), packet->length()) !=
1228 webrtc::PacketReceiver::DELIVERY_OK) {
1229 LOG(LS_WARNING) << "Failed to deliver RTP packet after creating default "
1230 "receiver.";
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001231 return;
1232 }
1233}
1234
1235void WebRtcVideoChannel2::OnRtcpReceived(
1236 talk_base::Buffer* packet,
1237 const talk_base::PacketTime& packet_time) {
pbos@webrtc.org1e019d12014-05-16 11:38:45 +00001238 if (call_->Receiver()->DeliverPacket(
1239 reinterpret_cast<const uint8_t*>(packet->data()), packet->length()) !=
1240 webrtc::PacketReceiver::DELIVERY_OK) {
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001241 LOG(LS_WARNING) << "Failed to deliver RTCP packet.";
1242 }
1243}
1244
1245void WebRtcVideoChannel2::OnReadyToSend(bool ready) {
1246 LOG(LS_VERBOSE) << "OnReadySend: " << (ready ? "Ready." : "Not ready.");
1247}
1248
1249bool WebRtcVideoChannel2::MuteStream(uint32 ssrc, bool mute) {
1250 LOG(LS_VERBOSE) << "MuteStream: " << ssrc << " -> "
1251 << (mute ? "mute" : "unmute");
1252 assert(ssrc != 0);
1253 if (send_streams_.find(ssrc) == send_streams_.end()) {
1254 LOG(LS_ERROR) << "No sending stream on ssrc " << ssrc;
1255 return false;
1256 }
1257 return send_streams_[ssrc]->MuteStream(mute);
1258}
1259
1260bool WebRtcVideoChannel2::SetRecvRtpHeaderExtensions(
1261 const std::vector<RtpHeaderExtension>& extensions) {
1262 // TODO(pbos): Implement.
1263 LOG(LS_VERBOSE) << "SetRecvRtpHeaderExtensions()";
1264 return true;
1265}
1266
1267bool WebRtcVideoChannel2::SetSendRtpHeaderExtensions(
1268 const std::vector<RtpHeaderExtension>& extensions) {
1269 // TODO(pbos): Implement.
1270 LOG(LS_VERBOSE) << "SetSendRtpHeaderExtensions()";
1271 return true;
1272}
1273
1274bool WebRtcVideoChannel2::SetStartSendBandwidth(int bps) {
1275 // TODO(pbos): Implement.
1276 LOG(LS_VERBOSE) << "SetStartSendBandwidth: " << bps;
1277 return true;
1278}
1279
1280bool WebRtcVideoChannel2::SetMaxSendBandwidth(int bps) {
1281 // TODO(pbos): Implement.
1282 LOG(LS_VERBOSE) << "SetMaxSendBandwidth: " << bps;
1283 return true;
1284}
1285
1286bool WebRtcVideoChannel2::SetOptions(const VideoOptions& options) {
1287 LOG(LS_VERBOSE) << "SetOptions: " << options.ToString();
1288 options_.SetAll(options);
1289 return true;
1290}
1291
1292void WebRtcVideoChannel2::SetInterface(NetworkInterface* iface) {
1293 MediaChannel::SetInterface(iface);
1294 // Set the RTP recv/send buffer to a bigger size
1295 MediaChannel::SetOption(NetworkInterface::ST_RTP,
1296 talk_base::Socket::OPT_RCVBUF,
1297 kVideoRtpBufferSize);
1298
1299 // TODO(sriniv): Remove or re-enable this.
1300 // As part of b/8030474, send-buffer is size now controlled through
1301 // portallocator flags.
1302 // network_interface_->SetOption(NetworkInterface::ST_RTP,
1303 // talk_base::Socket::OPT_SNDBUF,
1304 // kVideoRtpBufferSize);
1305}
1306
1307void WebRtcVideoChannel2::UpdateAspectRatio(int ratio_w, int ratio_h) {
1308 // TODO(pbos): Implement.
1309}
1310
1311void WebRtcVideoChannel2::OnMessage(talk_base::Message* msg) {
1312 // Ignored.
1313}
1314
1315bool WebRtcVideoChannel2::SendRtp(const uint8_t* data, size_t len) {
1316 talk_base::Buffer packet(data, len, kMaxRtpPacketLen);
1317 return MediaChannel::SendPacket(&packet);
1318}
1319
1320bool WebRtcVideoChannel2::SendRtcp(const uint8_t* data, size_t len) {
1321 talk_base::Buffer packet(data, len, kMaxRtpPacketLen);
1322 return MediaChannel::SendRtcp(&packet);
1323}
1324
1325void WebRtcVideoChannel2::StartAllSendStreams() {
1326 for (std::map<uint32, WebRtcVideoSendStream*>::iterator it =
1327 send_streams_.begin();
1328 it != send_streams_.end();
1329 ++it) {
1330 it->second->Start();
1331 }
1332}
1333
1334void WebRtcVideoChannel2::StopAllSendStreams() {
1335 for (std::map<uint32, WebRtcVideoSendStream*>::iterator it =
1336 send_streams_.begin();
1337 it != send_streams_.end();
1338 ++it) {
1339 it->second->Stop();
1340 }
1341}
1342
1343void WebRtcVideoChannel2::SetCodecForAllSendStreams(
1344 const WebRtcVideoChannel2::VideoCodecSettings& codec) {
1345 for (std::map<uint32, WebRtcVideoSendStream*>::iterator it =
1346 send_streams_.begin();
1347 it != send_streams_.end();
1348 ++it) {
1349 assert(it->second != NULL);
1350 it->second->SetCodec(options_, codec);
1351 }
1352}
1353
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +00001354WebRtcVideoChannel2::WebRtcVideoSendStream::VideoSendStreamParameters::
1355 VideoSendStreamParameters(
1356 const webrtc::VideoSendStream::Config& config,
1357 const VideoOptions& options,
1358 const VideoCodec& codec,
1359 const std::vector<webrtc::VideoStream>& video_streams)
1360 : config(config),
1361 options(options),
1362 codec(codec),
1363 video_streams(video_streams) {
1364}
1365
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001366WebRtcVideoChannel2::WebRtcVideoSendStream::WebRtcVideoSendStream(
1367 webrtc::Call* call,
1368 const webrtc::VideoSendStream::Config& config,
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +00001369 const VideoOptions& options,
1370 const VideoCodec& codec,
1371 const std::vector<webrtc::VideoStream>& video_streams,
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001372 WebRtcVideoEncoderFactory2* encoder_factory)
1373 : call_(call),
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +00001374 parameters_(config, options, codec, video_streams),
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001375 encoder_factory_(encoder_factory),
1376 capturer_(NULL),
1377 stream_(NULL),
1378 sending_(false),
1379 muted_(false),
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +00001380 format_(static_cast<int>(video_streams.back().height),
1381 static_cast<int>(video_streams.back().width),
1382 VideoFormat::FpsToInterval(video_streams.back().max_framerate),
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001383 FOURCC_I420) {
1384 RecreateWebRtcStream();
1385}
1386
1387WebRtcVideoChannel2::WebRtcVideoSendStream::~WebRtcVideoSendStream() {
1388 DisconnectCapturer();
1389 call_->DestroyVideoSendStream(stream_);
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +00001390 delete parameters_.config.encoder_settings.encoder;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001391}
1392
1393static void SetWebRtcFrameToBlack(webrtc::I420VideoFrame* video_frame) {
1394 assert(video_frame != NULL);
1395 memset(video_frame->buffer(webrtc::kYPlane),
1396 16,
1397 video_frame->allocated_size(webrtc::kYPlane));
1398 memset(video_frame->buffer(webrtc::kUPlane),
1399 128,
1400 video_frame->allocated_size(webrtc::kUPlane));
1401 memset(video_frame->buffer(webrtc::kVPlane),
1402 128,
1403 video_frame->allocated_size(webrtc::kVPlane));
1404}
1405
1406static void CreateBlackFrame(webrtc::I420VideoFrame* video_frame,
1407 int width,
1408 int height) {
1409 video_frame->CreateEmptyFrame(
1410 width, height, width, (width + 1) / 2, (width + 1) / 2);
1411 SetWebRtcFrameToBlack(video_frame);
1412}
1413
1414static void ConvertToI420VideoFrame(const VideoFrame& frame,
1415 webrtc::I420VideoFrame* i420_frame) {
1416 i420_frame->CreateFrame(
1417 static_cast<int>(frame.GetYPitch() * frame.GetHeight()),
1418 frame.GetYPlane(),
1419 static_cast<int>(frame.GetUPitch() * ((frame.GetHeight() + 1) / 2)),
1420 frame.GetUPlane(),
1421 static_cast<int>(frame.GetVPitch() * ((frame.GetHeight() + 1) / 2)),
1422 frame.GetVPlane(),
1423 static_cast<int>(frame.GetWidth()),
1424 static_cast<int>(frame.GetHeight()),
1425 static_cast<int>(frame.GetYPitch()),
1426 static_cast<int>(frame.GetUPitch()),
1427 static_cast<int>(frame.GetVPitch()));
1428}
1429
1430void WebRtcVideoChannel2::WebRtcVideoSendStream::InputFrame(
1431 VideoCapturer* capturer,
1432 const VideoFrame* frame) {
1433 LOG(LS_VERBOSE) << "InputFrame: " << frame->GetWidth() << "x"
1434 << frame->GetHeight();
1435 bool is_screencast = capturer->IsScreencast();
1436 // Lock before copying, can be called concurrently when swapping input source.
1437 talk_base::CritScope frame_cs(&frame_lock_);
1438 if (!muted_) {
1439 ConvertToI420VideoFrame(*frame, &video_frame_);
1440 } else {
1441 // Create a tiny black frame to transmit instead.
1442 CreateBlackFrame(&video_frame_, 1, 1);
1443 is_screencast = false;
1444 }
1445 talk_base::CritScope cs(&lock_);
1446 if (format_.width == 0) { // Dropping frames.
1447 assert(format_.height == 0);
1448 LOG(LS_VERBOSE) << "VideoFormat 0x0 set, Dropping frame.";
1449 return;
1450 }
1451 // Reconfigure codec if necessary.
1452 if (is_screencast) {
1453 SetDimensions(video_frame_.width(), video_frame_.height());
1454 }
1455 LOG(LS_VERBOSE) << "SwapFrame: " << video_frame_.width() << "x"
1456 << video_frame_.height() << " -> (codec) "
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +00001457 << parameters_.video_streams.back().width << "x"
1458 << parameters_.video_streams.back().height;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001459 stream_->Input()->SwapFrame(&video_frame_);
1460}
1461
1462bool WebRtcVideoChannel2::WebRtcVideoSendStream::SetCapturer(
1463 VideoCapturer* capturer) {
1464 if (!DisconnectCapturer() && capturer == NULL) {
1465 return false;
1466 }
1467
1468 {
1469 talk_base::CritScope cs(&lock_);
1470
1471 if (capturer == NULL) {
1472 LOG(LS_VERBOSE) << "Disabling capturer, sending black frame.";
1473 webrtc::I420VideoFrame black_frame;
1474
1475 int width = format_.width;
1476 int height = format_.height;
1477 int half_width = (width + 1) / 2;
1478 black_frame.CreateEmptyFrame(
1479 width, height, width, half_width, half_width);
1480 SetWebRtcFrameToBlack(&black_frame);
1481 SetDimensions(width, height);
1482 stream_->Input()->SwapFrame(&black_frame);
1483
1484 capturer_ = NULL;
1485 return true;
1486 }
1487
1488 capturer_ = capturer;
1489 }
1490 // Lock cannot be held while connecting the capturer to prevent lock-order
1491 // violations.
1492 capturer->SignalVideoFrame.connect(this, &WebRtcVideoSendStream::InputFrame);
1493 return true;
1494}
1495
1496bool WebRtcVideoChannel2::WebRtcVideoSendStream::SetVideoFormat(
1497 const VideoFormat& format) {
1498 if ((format.width == 0 || format.height == 0) &&
1499 format.width != format.height) {
1500 LOG(LS_ERROR) << "Can't set VideoFormat, width or height is zero (but not "
1501 "both, 0x0 drops frames).";
1502 return false;
1503 }
1504
1505 talk_base::CritScope cs(&lock_);
1506 if (format.width == 0 && format.height == 0) {
1507 LOG(LS_INFO)
1508 << "0x0 resolution selected. Captured frames will be dropped for ssrc: "
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +00001509 << parameters_.config.rtp.ssrcs[0] << ".";
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001510 } else {
1511 // TODO(pbos): Fix me, this only affects the last stream!
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +00001512 parameters_.video_streams.back().max_framerate =
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001513 VideoFormat::IntervalToFps(format.interval);
1514 SetDimensions(format.width, format.height);
1515 }
1516
1517 format_ = format;
1518 return true;
1519}
1520
1521bool WebRtcVideoChannel2::WebRtcVideoSendStream::MuteStream(bool mute) {
1522 talk_base::CritScope cs(&lock_);
1523 bool was_muted = muted_;
1524 muted_ = mute;
1525 return was_muted != mute;
1526}
1527
1528bool WebRtcVideoChannel2::WebRtcVideoSendStream::DisconnectCapturer() {
1529 talk_base::CritScope cs(&lock_);
1530 if (capturer_ == NULL) {
1531 return false;
1532 }
1533 capturer_->SignalVideoFrame.disconnect(this);
1534 capturer_ = NULL;
1535 return true;
1536}
1537
1538void WebRtcVideoChannel2::WebRtcVideoSendStream::SetCodec(
1539 const VideoOptions& options,
1540 const VideoCodecSettings& codec) {
1541 talk_base::CritScope cs(&lock_);
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +00001542
1543 std::vector<webrtc::VideoStream> video_streams =
1544 encoder_factory_->CreateVideoStreams(
1545 codec.codec, options, parameters_.video_streams.size());
1546 if (video_streams.empty()) {
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001547 return;
1548 }
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +00001549 parameters_.video_streams = video_streams;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001550 format_ = VideoFormat(codec.codec.width,
1551 codec.codec.height,
1552 VideoFormat::FpsToInterval(30),
1553 FOURCC_I420);
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +00001554
1555 webrtc::VideoEncoder* old_encoder =
1556 parameters_.config.encoder_settings.encoder;
1557 parameters_.config.encoder_settings.encoder =
1558 encoder_factory_->CreateVideoEncoder(codec.codec, options);
1559 parameters_.config.rtp.fec = codec.fec;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001560 // TODO(pbos): Should changing RTX payload type be allowed?
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +00001561 parameters_.codec = codec.codec;
1562 parameters_.options = options;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001563 RecreateWebRtcStream();
1564 delete old_encoder;
1565}
1566
1567void WebRtcVideoChannel2::WebRtcVideoSendStream::SetDimensions(int width,
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +00001568 int height) {
1569 assert(!parameters_.video_streams.empty());
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001570 LOG(LS_VERBOSE) << "SetDimensions: " << width << "x" << height;
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +00001571 if (parameters_.video_streams.back().width == width &&
1572 parameters_.video_streams.back().height == height) {
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001573 return;
1574 }
1575
1576 // TODO(pbos): Fix me, this only affects the last stream!
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +00001577 parameters_.video_streams.back().width = width;
1578 parameters_.video_streams.back().height = height;
1579
1580 // TODO(pbos): Wire up encoder_parameters, webrtc:3424.
1581 if (!stream_->ReconfigureVideoEncoder(parameters_.video_streams, NULL)) {
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001582 LOG(LS_WARNING) << "Failed to reconfigure video encoder for dimensions: "
1583 << width << "x" << height;
1584 return;
1585 }
1586}
1587
1588void WebRtcVideoChannel2::WebRtcVideoSendStream::Start() {
1589 talk_base::CritScope cs(&lock_);
1590 stream_->Start();
1591 sending_ = true;
1592}
1593
1594void WebRtcVideoChannel2::WebRtcVideoSendStream::Stop() {
1595 talk_base::CritScope cs(&lock_);
1596 stream_->Stop();
1597 sending_ = false;
1598}
1599
1600void WebRtcVideoChannel2::WebRtcVideoSendStream::RecreateWebRtcStream() {
1601 if (stream_ != NULL) {
1602 call_->DestroyVideoSendStream(stream_);
1603 }
pbos@webrtc.org6ae48c62014-06-06 10:49:19 +00001604
1605 // TODO(pbos): Wire up encoder_parameters, webrtc:3424.
1606 stream_ = call_->CreateVideoSendStream(
1607 parameters_.config, parameters_.video_streams, NULL);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001608 if (sending_) {
1609 stream_->Start();
1610 }
1611}
1612
1613WebRtcVideoChannel2::VideoCodecSettings::VideoCodecSettings()
1614 : rtx_payload_type(-1) {}
1615
1616std::vector<WebRtcVideoChannel2::VideoCodecSettings>
1617WebRtcVideoChannel2::MapCodecs(const std::vector<VideoCodec>& codecs) {
1618 assert(!codecs.empty());
1619
1620 std::vector<VideoCodecSettings> video_codecs;
1621 std::map<int, bool> payload_used;
pbos@webrtc.orge322a172014-06-13 11:47:28 +00001622 std::map<int, VideoCodec::CodecType> payload_codec_type;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001623 std::map<int, int> rtx_mapping; // video payload type -> rtx payload type.
1624
1625 webrtc::FecConfig fec_settings;
1626
1627 for (size_t i = 0; i < codecs.size(); ++i) {
1628 const VideoCodec& in_codec = codecs[i];
1629 int payload_type = in_codec.id;
1630
1631 if (payload_used[payload_type]) {
1632 LOG(LS_ERROR) << "Payload type already registered: "
1633 << in_codec.ToString();
1634 return std::vector<VideoCodecSettings>();
1635 }
1636 payload_used[payload_type] = true;
pbos@webrtc.orge322a172014-06-13 11:47:28 +00001637 payload_codec_type[payload_type] = in_codec.GetCodecType();
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001638
1639 switch (in_codec.GetCodecType()) {
1640 case VideoCodec::CODEC_RED: {
1641 // RED payload type, should not have duplicates.
1642 assert(fec_settings.red_payload_type == -1);
1643 fec_settings.red_payload_type = in_codec.id;
1644 continue;
1645 }
1646
1647 case VideoCodec::CODEC_ULPFEC: {
1648 // ULPFEC payload type, should not have duplicates.
1649 assert(fec_settings.ulpfec_payload_type == -1);
1650 fec_settings.ulpfec_payload_type = in_codec.id;
1651 continue;
1652 }
1653
1654 case VideoCodec::CODEC_RTX: {
1655 int associated_payload_type;
1656 if (!in_codec.GetParam(kCodecParamAssociatedPayloadType,
1657 &associated_payload_type)) {
1658 LOG(LS_ERROR) << "RTX codec without associated payload type: "
1659 << in_codec.ToString();
1660 return std::vector<VideoCodecSettings>();
1661 }
1662 rtx_mapping[associated_payload_type] = in_codec.id;
1663 continue;
1664 }
1665
1666 case VideoCodec::CODEC_VIDEO:
1667 break;
1668 }
1669
1670 video_codecs.push_back(VideoCodecSettings());
1671 video_codecs.back().codec = in_codec;
1672 }
1673
1674 // One of these codecs should have been a video codec. Only having FEC
1675 // parameters into this code is a logic error.
1676 assert(!video_codecs.empty());
1677
pbos@webrtc.orge322a172014-06-13 11:47:28 +00001678 for (std::map<int, int>::const_iterator it = rtx_mapping.begin();
1679 it != rtx_mapping.end();
1680 ++it) {
1681 if (!payload_used[it->first]) {
1682 LOG(LS_ERROR) << "RTX mapped to payload not in codec list.";
1683 return std::vector<VideoCodecSettings>();
1684 }
1685 if (payload_codec_type[it->first] != VideoCodec::CODEC_VIDEO) {
1686 LOG(LS_ERROR) << "RTX not mapped to regular video codec.";
1687 return std::vector<VideoCodecSettings>();
1688 }
1689 }
1690
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +00001691 // TODO(pbos): Write tests that figure out that I have not verified that RTX
1692 // codecs aren't mapped to bogus payloads.
1693 for (size_t i = 0; i < video_codecs.size(); ++i) {
1694 video_codecs[i].fec = fec_settings;
1695 if (rtx_mapping[video_codecs[i].codec.id] != 0) {
1696 video_codecs[i].rtx_payload_type = rtx_mapping[video_codecs[i].codec.id];
1697 }
1698 }
1699
1700 return video_codecs;
1701}
1702
1703std::vector<WebRtcVideoChannel2::VideoCodecSettings>
1704WebRtcVideoChannel2::FilterSupportedCodecs(
1705 const std::vector<WebRtcVideoChannel2::VideoCodecSettings>& mapped_codecs) {
1706 std::vector<VideoCodecSettings> supported_codecs;
1707 for (size_t i = 0; i < mapped_codecs.size(); ++i) {
1708 if (encoder_factory_->SupportsCodec(mapped_codecs[i].codec)) {
1709 supported_codecs.push_back(mapped_codecs[i]);
1710 }
1711 }
1712 return supported_codecs;
1713}
1714
1715} // namespace cricket
1716
1717#endif // HAVE_WEBRTC_VIDEO