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