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