pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
pbos@webrtc.org | 3c10758 | 2014-07-20 15:27:35 +0000 | [diff] [blame] | 31 | #include <set> |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 32 | #include <string> |
| 33 | |
| 34 | #include "libyuv/convert_from.h" |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 35 | #include "talk/media/base/videocapturer.h" |
| 36 | #include "talk/media/base/videorenderer.h" |
buildbot@webrtc.org | df9bbbe | 2014-06-19 19:54:33 +0000 | [diff] [blame] | 37 | #include "talk/media/webrtc/constants.h" |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 38 | #include "talk/media/webrtc/webrtcvideocapturer.h" |
| 39 | #include "talk/media/webrtc/webrtcvideoframe.h" |
| 40 | #include "talk/media/webrtc/webrtcvoiceengine.h" |
buildbot@webrtc.org | a09a999 | 2014-08-13 17:26:08 +0000 | [diff] [blame] | 41 | #include "webrtc/base/buffer.h" |
| 42 | #include "webrtc/base/logging.h" |
| 43 | #include "webrtc/base/stringutils.h" |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 44 | #include "webrtc/call.h" |
| 45 | // TODO(pbos): Move codecs out of modules (webrtc:3070). |
| 46 | #include "webrtc/modules/video_coding/codecs/vp8/include/vp8.h" |
| 47 | |
| 48 | #define UNIMPLEMENTED \ |
| 49 | LOG(LS_ERROR) << "Call to unimplemented function " << __FUNCTION__; \ |
| 50 | ASSERT(false) |
| 51 | |
| 52 | namespace cricket { |
| 53 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 54 | // This constant is really an on/off, lower-level configurable NACK history |
| 55 | // duration hasn't been implemented. |
| 56 | static const int kNackHistoryMs = 1000; |
| 57 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 58 | static const int kDefaultRtcpReceiverReportSsrc = 1; |
| 59 | |
| 60 | struct VideoCodecPref { |
| 61 | int payload_type; |
| 62 | const char* name; |
| 63 | int rtx_payload_type; |
buildbot@webrtc.org | df9bbbe | 2014-06-19 19:54:33 +0000 | [diff] [blame] | 64 | } kDefaultVideoCodecPref = {100, kVp8CodecName, 96}; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 65 | |
| 66 | VideoCodecPref kRedPref = {116, kRedCodecName, -1}; |
| 67 | VideoCodecPref kUlpfecPref = {117, kUlpfecCodecName, -1}; |
| 68 | |
| 69 | // The formats are sorted by the descending order of width. We use the order to |
| 70 | // find the next format for CPU and bandwidth adaptation. |
pbos@webrtc.org | 8fdeee6 | 2014-07-20 14:40:23 +0000 | [diff] [blame] | 71 | const VideoFormatPod kDefaultMaxVideoFormat = { |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 72 | 640, 400, FPS_TO_INTERVAL(kDefaultFramerate), FOURCC_ANY}; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 73 | |
| 74 | static bool FindFirstMatchingCodec(const std::vector<VideoCodec>& codecs, |
| 75 | const VideoCodec& requested_codec, |
| 76 | VideoCodec* matching_codec) { |
| 77 | for (size_t i = 0; i < codecs.size(); ++i) { |
| 78 | if (requested_codec.Matches(codecs[i])) { |
| 79 | *matching_codec = codecs[i]; |
| 80 | return true; |
| 81 | } |
| 82 | } |
| 83 | return false; |
| 84 | } |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 85 | |
pbos@webrtc.org | f99c2f2 | 2014-06-13 12:27:38 +0000 | [diff] [blame] | 86 | static void AddDefaultFeedbackParams(VideoCodec* codec) { |
| 87 | const FeedbackParam kFir(kRtcpFbParamCcm, kRtcpFbCcmParamFir); |
| 88 | codec->AddFeedbackParam(kFir); |
| 89 | const FeedbackParam kNack(kRtcpFbParamNack, kParamValueEmpty); |
| 90 | codec->AddFeedbackParam(kNack); |
| 91 | const FeedbackParam kPli(kRtcpFbParamNack, kRtcpFbNackParamPli); |
| 92 | codec->AddFeedbackParam(kPli); |
| 93 | const FeedbackParam kRemb(kRtcpFbParamRemb, kParamValueEmpty); |
| 94 | codec->AddFeedbackParam(kRemb); |
| 95 | } |
| 96 | |
| 97 | static bool IsNackEnabled(const VideoCodec& codec) { |
| 98 | return codec.HasFeedbackParam( |
| 99 | FeedbackParam(kRtcpFbParamNack, kParamValueEmpty)); |
| 100 | } |
| 101 | |
pbos@webrtc.org | 257e130 | 2014-07-25 19:01:32 +0000 | [diff] [blame] | 102 | static bool IsRembEnabled(const VideoCodec& codec) { |
| 103 | return codec.HasFeedbackParam( |
| 104 | FeedbackParam(kRtcpFbParamRemb, kParamValueEmpty)); |
| 105 | } |
| 106 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 107 | static VideoCodec DefaultVideoCodec() { |
| 108 | VideoCodec default_codec(kDefaultVideoCodecPref.payload_type, |
| 109 | kDefaultVideoCodecPref.name, |
pbos@webrtc.org | 8fdeee6 | 2014-07-20 14:40:23 +0000 | [diff] [blame] | 110 | kDefaultMaxVideoFormat.width, |
| 111 | kDefaultMaxVideoFormat.height, |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 112 | kDefaultFramerate, |
| 113 | 0); |
pbos@webrtc.org | f99c2f2 | 2014-06-13 12:27:38 +0000 | [diff] [blame] | 114 | AddDefaultFeedbackParams(&default_codec); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 115 | return default_codec; |
| 116 | } |
| 117 | |
| 118 | static VideoCodec DefaultRedCodec() { |
| 119 | return VideoCodec(kRedPref.payload_type, kRedPref.name, 0, 0, 0, 0); |
| 120 | } |
| 121 | |
| 122 | static VideoCodec DefaultUlpfecCodec() { |
| 123 | return VideoCodec(kUlpfecPref.payload_type, kUlpfecPref.name, 0, 0, 0, 0); |
| 124 | } |
| 125 | |
| 126 | static std::vector<VideoCodec> DefaultVideoCodecs() { |
| 127 | std::vector<VideoCodec> codecs; |
| 128 | codecs.push_back(DefaultVideoCodec()); |
| 129 | codecs.push_back(DefaultRedCodec()); |
| 130 | codecs.push_back(DefaultUlpfecCodec()); |
| 131 | if (kDefaultVideoCodecPref.rtx_payload_type != -1) { |
| 132 | codecs.push_back( |
| 133 | VideoCodec::CreateRtxCodec(kDefaultVideoCodecPref.rtx_payload_type, |
| 134 | kDefaultVideoCodecPref.payload_type)); |
| 135 | } |
| 136 | return codecs; |
| 137 | } |
| 138 | |
pbos@webrtc.org | 3c10758 | 2014-07-20 15:27:35 +0000 | [diff] [blame] | 139 | static bool ValidateRtpHeaderExtensionIds( |
| 140 | const std::vector<RtpHeaderExtension>& extensions) { |
| 141 | std::set<int> extensions_used; |
| 142 | for (size_t i = 0; i < extensions.size(); ++i) { |
| 143 | if (extensions[i].id < 0 || extensions[i].id >= 15 || |
| 144 | !extensions_used.insert(extensions[i].id).second) { |
| 145 | LOG(LS_ERROR) << "RTP extensions are with incorrect or duplicate ids."; |
| 146 | return false; |
| 147 | } |
| 148 | } |
| 149 | return true; |
| 150 | } |
| 151 | |
| 152 | static std::vector<webrtc::RtpExtension> FilterRtpExtensions( |
| 153 | const std::vector<RtpHeaderExtension>& extensions) { |
| 154 | std::vector<webrtc::RtpExtension> webrtc_extensions; |
| 155 | for (size_t i = 0; i < extensions.size(); ++i) { |
| 156 | // Unsupported extensions will be ignored. |
| 157 | if (webrtc::RtpExtension::IsSupported(extensions[i].uri)) { |
| 158 | webrtc_extensions.push_back(webrtc::RtpExtension( |
| 159 | extensions[i].uri, extensions[i].id)); |
| 160 | } else { |
| 161 | LOG(LS_WARNING) << "Unsupported RTP extension: " << extensions[i].uri; |
| 162 | } |
| 163 | } |
| 164 | return webrtc_extensions; |
| 165 | } |
| 166 | |
pbos@webrtc.org | 0d523ee | 2014-06-05 09:10:55 +0000 | [diff] [blame] | 167 | WebRtcVideoEncoderFactory2::~WebRtcVideoEncoderFactory2() { |
| 168 | } |
| 169 | |
buildbot@webrtc.org | d41eaeb | 2014-06-12 07:13:26 +0000 | [diff] [blame] | 170 | std::vector<webrtc::VideoStream> WebRtcVideoEncoderFactory2::CreateVideoStreams( |
| 171 | const VideoCodec& codec, |
| 172 | const VideoOptions& options, |
| 173 | size_t num_streams) { |
| 174 | assert(SupportsCodec(codec)); |
| 175 | if (num_streams != 1) { |
| 176 | LOG(LS_ERROR) << "Unsupported number of streams: " << num_streams; |
| 177 | return std::vector<webrtc::VideoStream>(); |
pbos@webrtc.org | 6ae48c6 | 2014-06-06 10:49:19 +0000 | [diff] [blame] | 178 | } |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 179 | |
buildbot@webrtc.org | d41eaeb | 2014-06-12 07:13:26 +0000 | [diff] [blame] | 180 | webrtc::VideoStream stream; |
| 181 | stream.width = codec.width; |
| 182 | stream.height = codec.height; |
| 183 | stream.max_framerate = |
| 184 | codec.framerate != 0 ? codec.framerate : kDefaultFramerate; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 185 | |
buildbot@webrtc.org | d41eaeb | 2014-06-12 07:13:26 +0000 | [diff] [blame] | 186 | int min_bitrate = kMinVideoBitrate; |
| 187 | codec.GetParam(kCodecParamMinBitrate, &min_bitrate); |
| 188 | int max_bitrate = kMaxVideoBitrate; |
| 189 | codec.GetParam(kCodecParamMaxBitrate, &max_bitrate); |
| 190 | stream.min_bitrate_bps = min_bitrate * 1000; |
| 191 | stream.target_bitrate_bps = stream.max_bitrate_bps = max_bitrate * 1000; |
| 192 | |
| 193 | int max_qp = 56; |
| 194 | codec.GetParam(kCodecParamMaxQuantization, &max_qp); |
| 195 | stream.max_qp = max_qp; |
| 196 | std::vector<webrtc::VideoStream> streams; |
| 197 | streams.push_back(stream); |
| 198 | return streams; |
| 199 | } |
| 200 | |
| 201 | webrtc::VideoEncoder* WebRtcVideoEncoderFactory2::CreateVideoEncoder( |
| 202 | const VideoCodec& codec, |
| 203 | const VideoOptions& options) { |
| 204 | assert(SupportsCodec(codec)); |
pbos@webrtc.org | 6f48f1b | 2014-07-22 16:29:54 +0000 | [diff] [blame] | 205 | if (_stricmp(codec.name.c_str(), kVp8CodecName) == 0) { |
| 206 | return webrtc::VP8Encoder::Create(); |
| 207 | } |
| 208 | // This shouldn't happen, we should be able to create encoders for all codecs |
| 209 | // we support. |
| 210 | assert(false); |
| 211 | return NULL; |
| 212 | } |
| 213 | |
| 214 | void* WebRtcVideoEncoderFactory2::CreateVideoEncoderSettings( |
| 215 | const VideoCodec& codec, |
| 216 | const VideoOptions& options) { |
| 217 | assert(SupportsCodec(codec)); |
| 218 | if (_stricmp(codec.name.c_str(), kVp8CodecName) == 0) { |
| 219 | webrtc::VideoCodecVP8* settings = new webrtc::VideoCodecVP8(); |
| 220 | settings->resilience = webrtc::kResilientStream; |
| 221 | settings->numberOfTemporalLayers = 1; |
| 222 | options.video_noise_reduction.Get(&settings->denoisingOn); |
| 223 | settings->errorConcealmentOn = false; |
| 224 | settings->automaticResizeOn = false; |
| 225 | settings->frameDroppingOn = true; |
| 226 | settings->keyFrameInterval = 3000; |
| 227 | return settings; |
| 228 | } |
| 229 | return NULL; |
| 230 | } |
| 231 | |
| 232 | void WebRtcVideoEncoderFactory2::DestroyVideoEncoderSettings( |
| 233 | const VideoCodec& codec, |
| 234 | void* encoder_settings) { |
| 235 | assert(SupportsCodec(codec)); |
| 236 | if (encoder_settings == NULL) { |
| 237 | return; |
| 238 | } |
| 239 | |
| 240 | if (_stricmp(codec.name.c_str(), kVp8CodecName) == 0) { |
| 241 | delete reinterpret_cast<webrtc::VideoCodecVP8*>(encoder_settings); |
| 242 | return; |
| 243 | } |
| 244 | // We should be able to destroy all encoder settings we've allocated. |
| 245 | assert(false); |
buildbot@webrtc.org | d41eaeb | 2014-06-12 07:13:26 +0000 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | bool WebRtcVideoEncoderFactory2::SupportsCodec(const VideoCodec& codec) { |
buildbot@webrtc.org | df9bbbe | 2014-06-19 19:54:33 +0000 | [diff] [blame] | 249 | return _stricmp(codec.name.c_str(), kVp8CodecName) == 0; |
buildbot@webrtc.org | d41eaeb | 2014-06-12 07:13:26 +0000 | [diff] [blame] | 250 | } |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 251 | |
pbos@webrtc.org | afb554f4 | 2014-08-12 23:17:13 +0000 | [diff] [blame] | 252 | DefaultUnsignalledSsrcHandler::DefaultUnsignalledSsrcHandler() |
| 253 | : default_recv_ssrc_(0), default_renderer_(NULL) {} |
| 254 | |
| 255 | UnsignalledSsrcHandler::Action DefaultUnsignalledSsrcHandler::OnUnsignalledSsrc( |
| 256 | VideoMediaChannel* channel, |
| 257 | uint32_t ssrc) { |
| 258 | if (default_recv_ssrc_ != 0) { // Already one default stream. |
| 259 | LOG(LS_WARNING) << "Unknown SSRC, but default receive stream already set."; |
| 260 | return kDropPacket; |
| 261 | } |
| 262 | |
| 263 | StreamParams sp; |
| 264 | sp.ssrcs.push_back(ssrc); |
| 265 | LOG(LS_INFO) << "Creating default receive stream for SSRC=" << ssrc << "."; |
| 266 | if (!channel->AddRecvStream(sp)) { |
| 267 | LOG(LS_WARNING) << "Could not create default receive stream."; |
| 268 | } |
| 269 | |
| 270 | channel->SetRenderer(ssrc, default_renderer_); |
| 271 | default_recv_ssrc_ = ssrc; |
| 272 | return kDeliverPacket; |
| 273 | } |
| 274 | |
| 275 | VideoRenderer* DefaultUnsignalledSsrcHandler::GetDefaultRenderer() const { |
| 276 | return default_renderer_; |
| 277 | } |
| 278 | |
| 279 | void DefaultUnsignalledSsrcHandler::SetDefaultRenderer( |
| 280 | VideoMediaChannel* channel, |
| 281 | VideoRenderer* renderer) { |
| 282 | default_renderer_ = renderer; |
| 283 | if (default_recv_ssrc_ != 0) { |
| 284 | channel->SetRenderer(default_recv_ssrc_, default_renderer_); |
| 285 | } |
| 286 | } |
| 287 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 288 | WebRtcVideoEngine2::WebRtcVideoEngine2() { |
| 289 | // Construct without a factory or voice engine. |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 290 | Construct(NULL, NULL, new rtc::CpuMonitor(NULL)); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | WebRtcVideoEngine2::WebRtcVideoEngine2( |
| 294 | WebRtcVideoChannelFactory* channel_factory) { |
| 295 | // Construct without a voice engine. |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 296 | Construct(channel_factory, NULL, new rtc::CpuMonitor(NULL)); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | void WebRtcVideoEngine2::Construct(WebRtcVideoChannelFactory* channel_factory, |
| 300 | WebRtcVoiceEngine* voice_engine, |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 301 | rtc::CpuMonitor* cpu_monitor) { |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 302 | LOG(LS_INFO) << "WebRtcVideoEngine2::WebRtcVideoEngine2"; |
| 303 | worker_thread_ = NULL; |
| 304 | voice_engine_ = voice_engine; |
| 305 | initialized_ = false; |
| 306 | capture_started_ = false; |
| 307 | cpu_monitor_.reset(cpu_monitor); |
| 308 | channel_factory_ = channel_factory; |
| 309 | |
| 310 | video_codecs_ = DefaultVideoCodecs(); |
pbos@webrtc.org | 8fdeee6 | 2014-07-20 14:40:23 +0000 | [diff] [blame] | 311 | default_codec_format_ = VideoFormat(kDefaultMaxVideoFormat); |
pbos@webrtc.org | 587ef60 | 2014-06-16 17:32:02 +0000 | [diff] [blame] | 312 | |
| 313 | rtp_header_extensions_.push_back( |
| 314 | RtpHeaderExtension(kRtpTimestampOffsetHeaderExtension, |
| 315 | kRtpTimestampOffsetHeaderExtensionDefaultId)); |
| 316 | rtp_header_extensions_.push_back( |
| 317 | RtpHeaderExtension(kRtpAbsoluteSenderTimeHeaderExtension, |
| 318 | kRtpAbsoluteSenderTimeHeaderExtensionDefaultId)); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | WebRtcVideoEngine2::~WebRtcVideoEngine2() { |
| 322 | LOG(LS_INFO) << "WebRtcVideoEngine2::~WebRtcVideoEngine2"; |
| 323 | |
| 324 | if (initialized_) { |
| 325 | Terminate(); |
| 326 | } |
| 327 | } |
| 328 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 329 | bool WebRtcVideoEngine2::Init(rtc::Thread* worker_thread) { |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 330 | LOG(LS_INFO) << "WebRtcVideoEngine2::Init"; |
| 331 | worker_thread_ = worker_thread; |
| 332 | ASSERT(worker_thread_ != NULL); |
| 333 | |
| 334 | cpu_monitor_->set_thread(worker_thread_); |
| 335 | if (!cpu_monitor_->Start(kCpuMonitorPeriodMs)) { |
| 336 | LOG(LS_ERROR) << "Failed to start CPU monitor."; |
| 337 | cpu_monitor_.reset(); |
| 338 | } |
| 339 | |
| 340 | initialized_ = true; |
| 341 | return true; |
| 342 | } |
| 343 | |
| 344 | void WebRtcVideoEngine2::Terminate() { |
| 345 | LOG(LS_INFO) << "WebRtcVideoEngine2::Terminate"; |
| 346 | |
| 347 | cpu_monitor_->Stop(); |
| 348 | |
| 349 | initialized_ = false; |
| 350 | } |
| 351 | |
| 352 | int WebRtcVideoEngine2::GetCapabilities() { return VIDEO_RECV | VIDEO_SEND; } |
| 353 | |
| 354 | bool WebRtcVideoEngine2::SetOptions(const VideoOptions& options) { |
| 355 | // TODO(pbos): Do we need this? This is a no-op in the existing |
| 356 | // WebRtcVideoEngine implementation. |
| 357 | LOG(LS_VERBOSE) << "SetOptions: " << options.ToString(); |
| 358 | // options_ = options; |
| 359 | return true; |
| 360 | } |
| 361 | |
| 362 | bool WebRtcVideoEngine2::SetDefaultEncoderConfig( |
| 363 | const VideoEncoderConfig& config) { |
pbos@webrtc.org | 8fdeee6 | 2014-07-20 14:40:23 +0000 | [diff] [blame] | 364 | const VideoCodec& codec = config.max_codec; |
| 365 | // TODO(pbos): Make use of external encoder factory. |
| 366 | if (!GetVideoEncoderFactory()->SupportsCodec(codec)) { |
| 367 | LOG(LS_ERROR) << "SetDefaultEncoderConfig, codec not supported:" |
| 368 | << codec.ToString(); |
| 369 | return false; |
| 370 | } |
| 371 | |
| 372 | default_codec_format_ = |
| 373 | VideoFormat(codec.width, |
| 374 | codec.height, |
| 375 | VideoFormat::FpsToInterval(codec.framerate), |
| 376 | FOURCC_ANY); |
| 377 | video_codecs_.clear(); |
| 378 | video_codecs_.push_back(codec); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 379 | return true; |
| 380 | } |
| 381 | |
| 382 | VideoEncoderConfig WebRtcVideoEngine2::GetDefaultEncoderConfig() const { |
| 383 | return VideoEncoderConfig(DefaultVideoCodec()); |
| 384 | } |
| 385 | |
| 386 | WebRtcVideoChannel2* WebRtcVideoEngine2::CreateChannel( |
| 387 | VoiceMediaChannel* voice_channel) { |
| 388 | LOG(LS_INFO) << "CreateChannel: " |
| 389 | << (voice_channel != NULL ? "With" : "Without") |
| 390 | << " voice channel."; |
| 391 | WebRtcVideoChannel2* channel = |
| 392 | channel_factory_ != NULL |
| 393 | ? channel_factory_->Create(this, voice_channel) |
| 394 | : new WebRtcVideoChannel2( |
pbos@webrtc.org | 0d523ee | 2014-06-05 09:10:55 +0000 | [diff] [blame] | 395 | this, voice_channel, GetVideoEncoderFactory()); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 396 | if (!channel->Init()) { |
| 397 | delete channel; |
| 398 | return NULL; |
| 399 | } |
pbos@webrtc.org | e322a17 | 2014-06-13 11:47:28 +0000 | [diff] [blame] | 400 | channel->SetRecvCodecs(video_codecs_); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 401 | return channel; |
| 402 | } |
| 403 | |
| 404 | const std::vector<VideoCodec>& WebRtcVideoEngine2::codecs() const { |
| 405 | return video_codecs_; |
| 406 | } |
| 407 | |
| 408 | const std::vector<RtpHeaderExtension>& |
| 409 | WebRtcVideoEngine2::rtp_header_extensions() const { |
| 410 | return rtp_header_extensions_; |
| 411 | } |
| 412 | |
| 413 | void WebRtcVideoEngine2::SetLogging(int min_sev, const char* filter) { |
| 414 | // TODO(pbos): Set up logging. |
| 415 | LOG(LS_VERBOSE) << "SetLogging: " << min_sev << '"' << filter << '"'; |
| 416 | // if min_sev == -1, we keep the current log level. |
| 417 | if (min_sev < 0) { |
| 418 | assert(min_sev == -1); |
| 419 | return; |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | bool WebRtcVideoEngine2::EnableTimedRender() { |
| 424 | // TODO(pbos): Figure out whether this can be removed. |
| 425 | return true; |
| 426 | } |
| 427 | |
| 428 | bool WebRtcVideoEngine2::SetLocalRenderer(VideoRenderer* renderer) { |
| 429 | // TODO(pbos): Implement or remove. Unclear which stream should be rendered |
| 430 | // locally even. |
| 431 | return true; |
| 432 | } |
| 433 | |
| 434 | // Checks to see whether we comprehend and could receive a particular codec |
| 435 | bool WebRtcVideoEngine2::FindCodec(const VideoCodec& in) { |
| 436 | // TODO(pbos): Probe encoder factory to figure out that the codec is supported |
| 437 | // if supported by the encoder factory. Add a corresponding test that fails |
| 438 | // with this code (that doesn't ask the factory). |
pbos@webrtc.org | 8fdeee6 | 2014-07-20 14:40:23 +0000 | [diff] [blame] | 439 | for (size_t j = 0; j < video_codecs_.size(); ++j) { |
| 440 | VideoCodec codec(video_codecs_[j].id, video_codecs_[j].name, 0, 0, 0, 0); |
| 441 | if (codec.Matches(in)) { |
| 442 | return true; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 443 | } |
| 444 | } |
| 445 | return false; |
| 446 | } |
| 447 | |
| 448 | // Tells whether the |requested| codec can be transmitted or not. If it can be |
| 449 | // transmitted |out| is set with the best settings supported. Aspect ratio will |
| 450 | // be set as close to |current|'s as possible. If not set |requested|'s |
| 451 | // dimensions will be used for aspect ratio matching. |
| 452 | bool WebRtcVideoEngine2::CanSendCodec(const VideoCodec& requested, |
| 453 | const VideoCodec& current, |
| 454 | VideoCodec* out) { |
| 455 | assert(out != NULL); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 456 | |
| 457 | if (requested.width != requested.height && |
| 458 | (requested.height == 0 || requested.width == 0)) { |
| 459 | // 0xn and nx0 are invalid resolutions. |
| 460 | return false; |
| 461 | } |
| 462 | |
| 463 | VideoCodec matching_codec; |
| 464 | if (!FindFirstMatchingCodec(video_codecs_, requested, &matching_codec)) { |
| 465 | // Codec not supported. |
| 466 | return false; |
| 467 | } |
| 468 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 469 | out->id = requested.id; |
| 470 | out->name = requested.name; |
| 471 | out->preference = requested.preference; |
| 472 | out->params = requested.params; |
| 473 | out->framerate = |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 474 | rtc::_min(requested.framerate, matching_codec.framerate); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 475 | out->params = requested.params; |
| 476 | out->feedback_params = requested.feedback_params; |
pbos@webrtc.org | 8fdeee6 | 2014-07-20 14:40:23 +0000 | [diff] [blame] | 477 | out->width = requested.width; |
| 478 | out->height = requested.height; |
| 479 | if (requested.width == 0 && requested.height == 0) { |
| 480 | return true; |
| 481 | } |
| 482 | |
| 483 | while (out->width > matching_codec.width) { |
| 484 | out->width /= 2; |
| 485 | out->height /= 2; |
| 486 | } |
| 487 | |
| 488 | return out->width > 0 && out->height > 0; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | bool WebRtcVideoEngine2::SetVoiceEngine(WebRtcVoiceEngine* voice_engine) { |
| 492 | if (initialized_) { |
| 493 | LOG(LS_WARNING) << "SetVoiceEngine can not be called after Init"; |
| 494 | return false; |
| 495 | } |
| 496 | voice_engine_ = voice_engine; |
| 497 | return true; |
| 498 | } |
| 499 | |
| 500 | // Ignore spammy trace messages, mostly from the stats API when we haven't |
| 501 | // gotten RTCP info yet from the remote side. |
| 502 | bool WebRtcVideoEngine2::ShouldIgnoreTrace(const std::string& trace) { |
| 503 | static const char* const kTracesToIgnore[] = {NULL}; |
| 504 | for (const char* const* p = kTracesToIgnore; *p; ++p) { |
| 505 | if (trace.find(*p) == 0) { |
| 506 | return true; |
| 507 | } |
| 508 | } |
| 509 | return false; |
| 510 | } |
| 511 | |
buildbot@webrtc.org | d41eaeb | 2014-06-12 07:13:26 +0000 | [diff] [blame] | 512 | WebRtcVideoEncoderFactory2* WebRtcVideoEngine2::GetVideoEncoderFactory() { |
| 513 | return &default_video_encoder_factory_; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 514 | } |
| 515 | |
pbos@webrtc.org | 6ae48c6 | 2014-06-06 10:49:19 +0000 | [diff] [blame] | 516 | // Thin map between VideoFrame and an existing webrtc::I420VideoFrame |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 517 | // to avoid having to copy the rendered VideoFrame prematurely. |
| 518 | // This implementation is only safe to use in a const context and should never |
| 519 | // be written to. |
pbos@webrtc.org | 6ae48c6 | 2014-06-06 10:49:19 +0000 | [diff] [blame] | 520 | class WebRtcVideoRenderFrame : public VideoFrame { |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 521 | public: |
| 522 | explicit WebRtcVideoRenderFrame(const webrtc::I420VideoFrame* frame) |
| 523 | : frame_(frame) {} |
| 524 | |
| 525 | virtual bool InitToBlack(int w, |
| 526 | int h, |
| 527 | size_t pixel_width, |
| 528 | size_t pixel_height, |
| 529 | int64 elapsed_time, |
| 530 | int64 time_stamp) OVERRIDE { |
| 531 | UNIMPLEMENTED; |
| 532 | return false; |
| 533 | } |
| 534 | |
| 535 | virtual bool Reset(uint32 fourcc, |
| 536 | int w, |
| 537 | int h, |
| 538 | int dw, |
| 539 | int dh, |
| 540 | uint8* sample, |
| 541 | size_t sample_size, |
| 542 | size_t pixel_width, |
| 543 | size_t pixel_height, |
| 544 | int64 elapsed_time, |
| 545 | int64 time_stamp, |
| 546 | int rotation) OVERRIDE { |
| 547 | UNIMPLEMENTED; |
| 548 | return false; |
| 549 | } |
| 550 | |
| 551 | virtual size_t GetWidth() const OVERRIDE { |
| 552 | return static_cast<size_t>(frame_->width()); |
| 553 | } |
| 554 | virtual size_t GetHeight() const OVERRIDE { |
| 555 | return static_cast<size_t>(frame_->height()); |
| 556 | } |
| 557 | |
| 558 | virtual const uint8* GetYPlane() const OVERRIDE { |
| 559 | return frame_->buffer(webrtc::kYPlane); |
| 560 | } |
| 561 | virtual const uint8* GetUPlane() const OVERRIDE { |
| 562 | return frame_->buffer(webrtc::kUPlane); |
| 563 | } |
| 564 | virtual const uint8* GetVPlane() const OVERRIDE { |
| 565 | return frame_->buffer(webrtc::kVPlane); |
| 566 | } |
| 567 | |
| 568 | virtual uint8* GetYPlane() OVERRIDE { |
| 569 | UNIMPLEMENTED; |
| 570 | return NULL; |
| 571 | } |
| 572 | virtual uint8* GetUPlane() OVERRIDE { |
| 573 | UNIMPLEMENTED; |
| 574 | return NULL; |
| 575 | } |
| 576 | virtual uint8* GetVPlane() OVERRIDE { |
| 577 | UNIMPLEMENTED; |
| 578 | return NULL; |
| 579 | } |
| 580 | |
| 581 | virtual int32 GetYPitch() const OVERRIDE { |
| 582 | return frame_->stride(webrtc::kYPlane); |
| 583 | } |
| 584 | virtual int32 GetUPitch() const OVERRIDE { |
| 585 | return frame_->stride(webrtc::kUPlane); |
| 586 | } |
| 587 | virtual int32 GetVPitch() const OVERRIDE { |
| 588 | return frame_->stride(webrtc::kVPlane); |
| 589 | } |
| 590 | |
| 591 | virtual void* GetNativeHandle() const OVERRIDE { return NULL; } |
| 592 | |
| 593 | virtual size_t GetPixelWidth() const OVERRIDE { return 1; } |
| 594 | virtual size_t GetPixelHeight() const OVERRIDE { return 1; } |
| 595 | |
| 596 | virtual int64 GetElapsedTime() const OVERRIDE { |
| 597 | // Convert millisecond render time to ns timestamp. |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 598 | return frame_->render_time_ms() * rtc::kNumNanosecsPerMillisec; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 599 | } |
| 600 | virtual int64 GetTimeStamp() const OVERRIDE { |
| 601 | // Convert 90K rtp timestamp to ns timestamp. |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 602 | return (frame_->timestamp() / 90) * rtc::kNumNanosecsPerMillisec; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 603 | } |
| 604 | virtual void SetElapsedTime(int64 elapsed_time) OVERRIDE { UNIMPLEMENTED; } |
| 605 | virtual void SetTimeStamp(int64 time_stamp) OVERRIDE { UNIMPLEMENTED; } |
| 606 | |
| 607 | virtual int GetRotation() const OVERRIDE { |
| 608 | UNIMPLEMENTED; |
| 609 | return ROTATION_0; |
| 610 | } |
| 611 | |
| 612 | virtual VideoFrame* Copy() const OVERRIDE { |
| 613 | UNIMPLEMENTED; |
| 614 | return NULL; |
| 615 | } |
| 616 | |
| 617 | virtual bool MakeExclusive() OVERRIDE { |
| 618 | UNIMPLEMENTED; |
| 619 | return false; |
| 620 | } |
| 621 | |
| 622 | virtual size_t CopyToBuffer(uint8* buffer, size_t size) const { |
| 623 | UNIMPLEMENTED; |
| 624 | return 0; |
| 625 | } |
| 626 | |
| 627 | // TODO(fbarchard): Refactor into base class and share with LMI |
| 628 | virtual size_t ConvertToRgbBuffer(uint32 to_fourcc, |
| 629 | uint8* buffer, |
| 630 | size_t size, |
| 631 | int stride_rgb) const OVERRIDE { |
| 632 | size_t width = GetWidth(); |
| 633 | size_t height = GetHeight(); |
| 634 | size_t needed = (stride_rgb >= 0 ? stride_rgb : -stride_rgb) * height; |
| 635 | if (size < needed) { |
| 636 | LOG(LS_WARNING) << "RGB buffer is not large enough"; |
| 637 | return needed; |
| 638 | } |
| 639 | |
| 640 | if (libyuv::ConvertFromI420(GetYPlane(), |
| 641 | GetYPitch(), |
| 642 | GetUPlane(), |
| 643 | GetUPitch(), |
| 644 | GetVPlane(), |
| 645 | GetVPitch(), |
| 646 | buffer, |
| 647 | stride_rgb, |
| 648 | static_cast<int>(width), |
| 649 | static_cast<int>(height), |
| 650 | to_fourcc)) { |
| 651 | LOG(LS_ERROR) << "RGB type not supported: " << to_fourcc; |
| 652 | return 0; // 0 indicates error |
| 653 | } |
| 654 | return needed; |
| 655 | } |
| 656 | |
| 657 | protected: |
| 658 | virtual VideoFrame* CreateEmptyFrame(int w, |
| 659 | int h, |
| 660 | size_t pixel_width, |
| 661 | size_t pixel_height, |
| 662 | int64 elapsed_time, |
| 663 | int64 time_stamp) const OVERRIDE { |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 664 | WebRtcVideoFrame* frame = new WebRtcVideoFrame(); |
| 665 | frame->InitToBlack( |
| 666 | w, h, pixel_width, pixel_height, elapsed_time, time_stamp); |
| 667 | return frame; |
| 668 | } |
| 669 | |
| 670 | private: |
| 671 | const webrtc::I420VideoFrame* const frame_; |
| 672 | }; |
| 673 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 674 | WebRtcVideoChannel2::WebRtcVideoChannel2( |
| 675 | WebRtcVideoEngine2* engine, |
| 676 | VoiceMediaChannel* voice_channel, |
| 677 | WebRtcVideoEncoderFactory2* encoder_factory) |
pbos@webrtc.org | afb554f4 | 2014-08-12 23:17:13 +0000 | [diff] [blame] | 678 | : encoder_factory_(encoder_factory), |
| 679 | unsignalled_ssrc_handler_(&default_unsignalled_ssrc_handler_) { |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 680 | // TODO(pbos): Connect the video and audio with |voice_channel|. |
| 681 | webrtc::Call::Config config(this); |
| 682 | Construct(webrtc::Call::Create(config), engine); |
| 683 | } |
| 684 | |
| 685 | WebRtcVideoChannel2::WebRtcVideoChannel2( |
| 686 | webrtc::Call* call, |
| 687 | WebRtcVideoEngine2* engine, |
| 688 | WebRtcVideoEncoderFactory2* encoder_factory) |
pbos@webrtc.org | afb554f4 | 2014-08-12 23:17:13 +0000 | [diff] [blame] | 689 | : encoder_factory_(encoder_factory), |
| 690 | unsignalled_ssrc_handler_(&default_unsignalled_ssrc_handler_) { |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 691 | Construct(call, engine); |
| 692 | } |
| 693 | |
| 694 | void WebRtcVideoChannel2::Construct(webrtc::Call* call, |
| 695 | WebRtcVideoEngine2* engine) { |
| 696 | rtcp_receiver_report_ssrc_ = kDefaultRtcpReceiverReportSsrc; |
| 697 | sending_ = false; |
| 698 | call_.reset(call); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 699 | default_send_ssrc_ = 0; |
pbos@webrtc.org | 6f48f1b | 2014-07-22 16:29:54 +0000 | [diff] [blame] | 700 | |
| 701 | SetDefaultOptions(); |
| 702 | } |
| 703 | |
| 704 | void WebRtcVideoChannel2::SetDefaultOptions() { |
| 705 | options_.video_noise_reduction.Set(true); |
pbos@webrtc.org | 543e589 | 2014-07-23 07:01:31 +0000 | [diff] [blame] | 706 | options_.use_payload_padding.Set(false); |
pbos@webrtc.org | 5ff71ab | 2014-07-23 07:28:56 +0000 | [diff] [blame] | 707 | options_.suspend_below_min_bitrate.Set(false); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 708 | } |
| 709 | |
| 710 | WebRtcVideoChannel2::~WebRtcVideoChannel2() { |
| 711 | for (std::map<uint32, WebRtcVideoSendStream*>::iterator it = |
| 712 | send_streams_.begin(); |
| 713 | it != send_streams_.end(); |
| 714 | ++it) { |
| 715 | delete it->second; |
| 716 | } |
| 717 | |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 718 | for (std::map<uint32, WebRtcVideoReceiveStream*>::iterator it = |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 719 | receive_streams_.begin(); |
| 720 | it != receive_streams_.end(); |
| 721 | ++it) { |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 722 | delete it->second; |
| 723 | } |
| 724 | } |
| 725 | |
| 726 | bool WebRtcVideoChannel2::Init() { return true; } |
| 727 | |
| 728 | namespace { |
| 729 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 730 | static std::string CodecVectorToString(const std::vector<VideoCodec>& codecs) { |
| 731 | std::stringstream out; |
| 732 | out << '{'; |
| 733 | for (size_t i = 0; i < codecs.size(); ++i) { |
| 734 | out << codecs[i].ToString(); |
| 735 | if (i != codecs.size() - 1) { |
| 736 | out << ", "; |
| 737 | } |
| 738 | } |
| 739 | out << '}'; |
| 740 | return out.str(); |
| 741 | } |
| 742 | |
pbos@webrtc.org | e322a17 | 2014-06-13 11:47:28 +0000 | [diff] [blame] | 743 | static bool ValidateCodecFormats(const std::vector<VideoCodec>& codecs) { |
| 744 | bool has_video = false; |
| 745 | for (size_t i = 0; i < codecs.size(); ++i) { |
| 746 | if (!codecs[i].ValidateCodecFormat()) { |
| 747 | return false; |
| 748 | } |
| 749 | if (codecs[i].GetCodecType() == VideoCodec::CODEC_VIDEO) { |
| 750 | has_video = true; |
| 751 | } |
| 752 | } |
| 753 | if (!has_video) { |
| 754 | LOG(LS_ERROR) << "Setting codecs without a video codec is invalid: " |
| 755 | << CodecVectorToString(codecs); |
| 756 | return false; |
| 757 | } |
| 758 | return true; |
| 759 | } |
| 760 | |
pbos@webrtc.org | 587ef60 | 2014-06-16 17:32:02 +0000 | [diff] [blame] | 761 | static std::string RtpExtensionsToString( |
| 762 | const std::vector<RtpHeaderExtension>& extensions) { |
| 763 | std::stringstream out; |
| 764 | out << '{'; |
| 765 | for (size_t i = 0; i < extensions.size(); ++i) { |
| 766 | out << "{" << extensions[i].uri << ": " << extensions[i].id << "}"; |
| 767 | if (i != extensions.size() - 1) { |
| 768 | out << ", "; |
| 769 | } |
| 770 | } |
| 771 | out << '}'; |
| 772 | return out.str(); |
| 773 | } |
| 774 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 775 | } // namespace |
| 776 | |
| 777 | bool WebRtcVideoChannel2::SetRecvCodecs(const std::vector<VideoCodec>& codecs) { |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 778 | LOG(LS_INFO) << "SetRecvCodecs: " << CodecVectorToString(codecs); |
| 779 | if (!ValidateCodecFormats(codecs)) { |
| 780 | return false; |
| 781 | } |
| 782 | |
| 783 | const std::vector<VideoCodecSettings> mapped_codecs = MapCodecs(codecs); |
| 784 | if (mapped_codecs.empty()) { |
| 785 | LOG(LS_ERROR) << "SetRecvCodecs called without video codec payloads."; |
| 786 | return false; |
| 787 | } |
| 788 | |
| 789 | // TODO(pbos): Add a decoder factory which controls supported codecs. |
| 790 | // Blocked on webrtc:2854. |
| 791 | for (size_t i = 0; i < mapped_codecs.size(); ++i) { |
buildbot@webrtc.org | df9bbbe | 2014-06-19 19:54:33 +0000 | [diff] [blame] | 792 | if (_stricmp(mapped_codecs[i].codec.name.c_str(), kVp8CodecName) != 0) { |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 793 | LOG(LS_ERROR) << "SetRecvCodecs called with unsupported codec: '" |
| 794 | << mapped_codecs[i].codec.name << "'"; |
| 795 | return false; |
| 796 | } |
| 797 | } |
| 798 | |
| 799 | recv_codecs_ = mapped_codecs; |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 800 | |
| 801 | for (std::map<uint32, WebRtcVideoReceiveStream*>::iterator it = |
| 802 | receive_streams_.begin(); |
| 803 | it != receive_streams_.end(); |
| 804 | ++it) { |
| 805 | it->second->SetRecvCodecs(recv_codecs_); |
| 806 | } |
| 807 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 808 | return true; |
| 809 | } |
| 810 | |
| 811 | bool WebRtcVideoChannel2::SetSendCodecs(const std::vector<VideoCodec>& codecs) { |
| 812 | LOG(LS_INFO) << "SetSendCodecs: " << CodecVectorToString(codecs); |
| 813 | if (!ValidateCodecFormats(codecs)) { |
| 814 | return false; |
| 815 | } |
| 816 | |
| 817 | const std::vector<VideoCodecSettings> supported_codecs = |
| 818 | FilterSupportedCodecs(MapCodecs(codecs)); |
| 819 | |
| 820 | if (supported_codecs.empty()) { |
| 821 | LOG(LS_ERROR) << "No video codecs supported by encoder factory."; |
| 822 | return false; |
| 823 | } |
| 824 | |
| 825 | send_codec_.Set(supported_codecs.front()); |
| 826 | LOG(LS_INFO) << "Using codec: " << supported_codecs.front().codec.ToString(); |
| 827 | |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 828 | for (std::map<uint32, WebRtcVideoSendStream*>::iterator it = |
| 829 | send_streams_.begin(); |
| 830 | it != send_streams_.end(); |
| 831 | ++it) { |
| 832 | assert(it->second != NULL); |
| 833 | it->second->SetCodec(supported_codecs.front()); |
| 834 | } |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 835 | |
| 836 | return true; |
| 837 | } |
| 838 | |
| 839 | bool WebRtcVideoChannel2::GetSendCodec(VideoCodec* codec) { |
| 840 | VideoCodecSettings codec_settings; |
| 841 | if (!send_codec_.Get(&codec_settings)) { |
| 842 | LOG(LS_VERBOSE) << "GetSendCodec: No send codec set."; |
| 843 | return false; |
| 844 | } |
| 845 | *codec = codec_settings.codec; |
| 846 | return true; |
| 847 | } |
| 848 | |
| 849 | bool WebRtcVideoChannel2::SetSendStreamFormat(uint32 ssrc, |
| 850 | const VideoFormat& format) { |
| 851 | LOG(LS_VERBOSE) << "SetSendStreamFormat:" << ssrc << " -> " |
| 852 | << format.ToString(); |
| 853 | if (send_streams_.find(ssrc) == send_streams_.end()) { |
| 854 | return false; |
| 855 | } |
| 856 | return send_streams_[ssrc]->SetVideoFormat(format); |
| 857 | } |
| 858 | |
| 859 | bool WebRtcVideoChannel2::SetRender(bool render) { |
| 860 | // TODO(pbos): Implement. Or refactor away as it shouldn't be needed. |
| 861 | LOG(LS_VERBOSE) << "SetRender: " << (render ? "true" : "false"); |
| 862 | return true; |
| 863 | } |
| 864 | |
| 865 | bool WebRtcVideoChannel2::SetSend(bool send) { |
| 866 | LOG(LS_VERBOSE) << "SetSend: " << (send ? "true" : "false"); |
| 867 | if (send && !send_codec_.IsSet()) { |
| 868 | LOG(LS_ERROR) << "SetSend(true) called before setting codec."; |
| 869 | return false; |
| 870 | } |
| 871 | if (send) { |
| 872 | StartAllSendStreams(); |
| 873 | } else { |
| 874 | StopAllSendStreams(); |
| 875 | } |
| 876 | sending_ = send; |
| 877 | return true; |
| 878 | } |
| 879 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 880 | bool WebRtcVideoChannel2::AddSendStream(const StreamParams& sp) { |
| 881 | LOG(LS_INFO) << "AddSendStream: " << sp.ToString(); |
| 882 | if (sp.ssrcs.empty()) { |
| 883 | LOG(LS_ERROR) << "No SSRCs in stream parameters."; |
| 884 | return false; |
| 885 | } |
| 886 | |
| 887 | uint32 ssrc = sp.first_ssrc(); |
| 888 | assert(ssrc != 0); |
| 889 | // TODO(pbos): Make sure none of sp.ssrcs are used, not just the identifying |
| 890 | // ssrc. |
| 891 | if (send_streams_.find(ssrc) != send_streams_.end()) { |
| 892 | LOG(LS_ERROR) << "Send stream with ssrc '" << ssrc << "' already exists."; |
| 893 | return false; |
| 894 | } |
| 895 | |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 896 | std::vector<uint32> primary_ssrcs; |
| 897 | sp.GetPrimarySsrcs(&primary_ssrcs); |
| 898 | std::vector<uint32> rtx_ssrcs; |
| 899 | sp.GetFidSsrcs(primary_ssrcs, &rtx_ssrcs); |
| 900 | if (!rtx_ssrcs.empty() && primary_ssrcs.size() != rtx_ssrcs.size()) { |
| 901 | LOG(LS_ERROR) |
| 902 | << "RTX SSRCs exist, but don't cover all SSRCs (unsupported): " |
| 903 | << sp.ToString(); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 904 | return false; |
| 905 | } |
| 906 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 907 | WebRtcVideoSendStream* stream = |
pbos@webrtc.org | 6ae48c6 | 2014-06-06 10:49:19 +0000 | [diff] [blame] | 908 | new WebRtcVideoSendStream(call_.get(), |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 909 | encoder_factory_, |
pbos@webrtc.org | 6ae48c6 | 2014-06-06 10:49:19 +0000 | [diff] [blame] | 910 | options_, |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 911 | send_codec_, |
| 912 | sp, |
| 913 | send_rtp_extensions_); |
| 914 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 915 | send_streams_[ssrc] = stream; |
| 916 | |
| 917 | if (rtcp_receiver_report_ssrc_ == kDefaultRtcpReceiverReportSsrc) { |
| 918 | rtcp_receiver_report_ssrc_ = ssrc; |
| 919 | } |
| 920 | if (default_send_ssrc_ == 0) { |
| 921 | default_send_ssrc_ = ssrc; |
| 922 | } |
| 923 | if (sending_) { |
| 924 | stream->Start(); |
| 925 | } |
| 926 | |
| 927 | return true; |
| 928 | } |
| 929 | |
| 930 | bool WebRtcVideoChannel2::RemoveSendStream(uint32 ssrc) { |
| 931 | LOG(LS_INFO) << "RemoveSendStream: " << ssrc; |
| 932 | |
| 933 | if (ssrc == 0) { |
| 934 | if (default_send_ssrc_ == 0) { |
| 935 | LOG(LS_ERROR) << "No default send stream active."; |
| 936 | return false; |
| 937 | } |
| 938 | |
| 939 | LOG(LS_VERBOSE) << "Removing default stream: " << default_send_ssrc_; |
| 940 | ssrc = default_send_ssrc_; |
| 941 | } |
| 942 | |
| 943 | std::map<uint32, WebRtcVideoSendStream*>::iterator it = |
| 944 | send_streams_.find(ssrc); |
| 945 | if (it == send_streams_.end()) { |
| 946 | return false; |
| 947 | } |
| 948 | |
| 949 | delete it->second; |
| 950 | send_streams_.erase(it); |
| 951 | |
| 952 | if (ssrc == default_send_ssrc_) { |
| 953 | default_send_ssrc_ = 0; |
| 954 | } |
| 955 | |
| 956 | return true; |
| 957 | } |
| 958 | |
| 959 | bool WebRtcVideoChannel2::AddRecvStream(const StreamParams& sp) { |
| 960 | LOG(LS_INFO) << "AddRecvStream: " << sp.ToString(); |
| 961 | assert(sp.ssrcs.size() > 0); |
| 962 | |
| 963 | uint32 ssrc = sp.first_ssrc(); |
| 964 | assert(ssrc != 0); // TODO(pbos): Is this ever valid? |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 965 | |
| 966 | // TODO(pbos): Check if any of the SSRCs overlap. |
| 967 | if (receive_streams_.find(ssrc) != receive_streams_.end()) { |
| 968 | LOG(LS_ERROR) << "Receive stream for SSRC " << ssrc << "already exists."; |
| 969 | return false; |
| 970 | } |
| 971 | |
pbos@webrtc.org | bd249bc | 2014-07-07 04:45:15 +0000 | [diff] [blame] | 972 | webrtc::VideoReceiveStream::Config config; |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 973 | ConfigureReceiverRtp(&config, sp); |
| 974 | receive_streams_[ssrc] = |
| 975 | new WebRtcVideoReceiveStream(call_.get(), config, recv_codecs_); |
| 976 | |
| 977 | return true; |
| 978 | } |
| 979 | |
| 980 | void WebRtcVideoChannel2::ConfigureReceiverRtp( |
| 981 | webrtc::VideoReceiveStream::Config* config, |
| 982 | const StreamParams& sp) const { |
| 983 | uint32 ssrc = sp.first_ssrc(); |
| 984 | |
| 985 | config->rtp.remote_ssrc = ssrc; |
| 986 | config->rtp.local_ssrc = rtcp_receiver_report_ssrc_; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 987 | |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 988 | config->rtp.extensions = recv_rtp_extensions_; |
pbos@webrtc.org | 257e130 | 2014-07-25 19:01:32 +0000 | [diff] [blame] | 989 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 990 | // TODO(pbos): This protection is against setting the same local ssrc as |
| 991 | // remote which is not permitted by the lower-level API. RTCP requires a |
| 992 | // corresponding sender SSRC. Figure out what to do when we don't have |
| 993 | // (receive-only) or know a good local SSRC. |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 994 | if (config->rtp.remote_ssrc == config->rtp.local_ssrc) { |
| 995 | if (config->rtp.local_ssrc != kDefaultRtcpReceiverReportSsrc) { |
| 996 | config->rtp.local_ssrc = kDefaultRtcpReceiverReportSsrc; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 997 | } else { |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 998 | config->rtp.local_ssrc = kDefaultRtcpReceiverReportSsrc + 1; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 999 | } |
| 1000 | } |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 1001 | |
| 1002 | for (size_t i = 0; i < recv_codecs_.size(); ++i) { |
| 1003 | if (recv_codecs_[i].codec.id == kDefaultVideoCodecPref.payload_type) { |
| 1004 | config->rtp.fec = recv_codecs_[i].fec; |
| 1005 | uint32 rtx_ssrc; |
| 1006 | if (recv_codecs_[i].rtx_payload_type != -1 && |
| 1007 | sp.GetFidSsrc(ssrc, &rtx_ssrc)) { |
| 1008 | config->rtp.rtx[kDefaultVideoCodecPref.payload_type].ssrc = rtx_ssrc; |
| 1009 | config->rtp.rtx[kDefaultVideoCodecPref.payload_type].payload_type = |
| 1010 | recv_codecs_[i].rtx_payload_type; |
| 1011 | } |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1012 | break; |
| 1013 | } |
| 1014 | } |
| 1015 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1016 | } |
| 1017 | |
| 1018 | bool WebRtcVideoChannel2::RemoveRecvStream(uint32 ssrc) { |
| 1019 | LOG(LS_INFO) << "RemoveRecvStream: " << ssrc; |
| 1020 | if (ssrc == 0) { |
pbos@webrtc.org | afb554f4 | 2014-08-12 23:17:13 +0000 | [diff] [blame] | 1021 | LOG(LS_ERROR) << "RemoveRecvStream with 0 ssrc is not supported."; |
| 1022 | return false; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1023 | } |
| 1024 | |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 1025 | std::map<uint32, WebRtcVideoReceiveStream*>::iterator stream = |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1026 | receive_streams_.find(ssrc); |
| 1027 | if (stream == receive_streams_.end()) { |
| 1028 | LOG(LS_ERROR) << "Stream not found for ssrc: " << ssrc; |
| 1029 | return false; |
| 1030 | } |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 1031 | delete stream->second; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1032 | receive_streams_.erase(stream); |
| 1033 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1034 | return true; |
| 1035 | } |
| 1036 | |
| 1037 | bool WebRtcVideoChannel2::SetRenderer(uint32 ssrc, VideoRenderer* renderer) { |
| 1038 | LOG(LS_INFO) << "SetRenderer: ssrc:" << ssrc << " " |
| 1039 | << (renderer ? "(ptr)" : "NULL"); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1040 | if (ssrc == 0) { |
pbos@webrtc.org | afb554f4 | 2014-08-12 23:17:13 +0000 | [diff] [blame] | 1041 | default_unsignalled_ssrc_handler_.SetDefaultRenderer(this, renderer); |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 1042 | return true; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1043 | } |
| 1044 | |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 1045 | std::map<uint32, WebRtcVideoReceiveStream*>::iterator it = |
| 1046 | receive_streams_.find(ssrc); |
| 1047 | if (it == receive_streams_.end()) { |
| 1048 | return false; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1049 | } |
| 1050 | |
| 1051 | it->second->SetRenderer(renderer); |
| 1052 | return true; |
| 1053 | } |
| 1054 | |
| 1055 | bool WebRtcVideoChannel2::GetRenderer(uint32 ssrc, VideoRenderer** renderer) { |
| 1056 | if (ssrc == 0) { |
pbos@webrtc.org | afb554f4 | 2014-08-12 23:17:13 +0000 | [diff] [blame] | 1057 | *renderer = default_unsignalled_ssrc_handler_.GetDefaultRenderer(); |
| 1058 | return *renderer != NULL; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1059 | } |
| 1060 | |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 1061 | std::map<uint32, WebRtcVideoReceiveStream*>::iterator it = |
| 1062 | receive_streams_.find(ssrc); |
| 1063 | if (it == receive_streams_.end()) { |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1064 | return false; |
| 1065 | } |
| 1066 | *renderer = it->second->GetRenderer(); |
| 1067 | return true; |
| 1068 | } |
| 1069 | |
| 1070 | bool WebRtcVideoChannel2::GetStats(const StatsOptions& options, |
| 1071 | VideoMediaInfo* info) { |
pbos@webrtc.org | e6f84ae | 2014-07-18 11:11:55 +0000 | [diff] [blame] | 1072 | info->Clear(); |
| 1073 | FillSenderStats(info); |
| 1074 | FillReceiverStats(info); |
| 1075 | FillBandwidthEstimationStats(info); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1076 | return true; |
| 1077 | } |
| 1078 | |
pbos@webrtc.org | e6f84ae | 2014-07-18 11:11:55 +0000 | [diff] [blame] | 1079 | void WebRtcVideoChannel2::FillSenderStats(VideoMediaInfo* video_media_info) { |
| 1080 | for (std::map<uint32, WebRtcVideoSendStream*>::iterator it = |
| 1081 | send_streams_.begin(); |
| 1082 | it != send_streams_.end(); |
| 1083 | ++it) { |
| 1084 | video_media_info->senders.push_back(it->second->GetVideoSenderInfo()); |
| 1085 | } |
| 1086 | } |
| 1087 | |
| 1088 | void WebRtcVideoChannel2::FillReceiverStats(VideoMediaInfo* video_media_info) { |
| 1089 | for (std::map<uint32, WebRtcVideoReceiveStream*>::iterator it = |
| 1090 | receive_streams_.begin(); |
| 1091 | it != receive_streams_.end(); |
| 1092 | ++it) { |
| 1093 | video_media_info->receivers.push_back(it->second->GetVideoReceiverInfo()); |
| 1094 | } |
| 1095 | } |
| 1096 | |
| 1097 | void WebRtcVideoChannel2::FillBandwidthEstimationStats( |
| 1098 | VideoMediaInfo* video_media_info) { |
| 1099 | // TODO(pbos): Implement. |
| 1100 | } |
| 1101 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1102 | bool WebRtcVideoChannel2::SetCapturer(uint32 ssrc, VideoCapturer* capturer) { |
| 1103 | LOG(LS_INFO) << "SetCapturer: " << ssrc << " -> " |
| 1104 | << (capturer != NULL ? "(capturer)" : "NULL"); |
| 1105 | assert(ssrc != 0); |
| 1106 | if (send_streams_.find(ssrc) == send_streams_.end()) { |
| 1107 | LOG(LS_ERROR) << "No sending stream on ssrc " << ssrc; |
| 1108 | return false; |
| 1109 | } |
| 1110 | return send_streams_[ssrc]->SetCapturer(capturer); |
| 1111 | } |
| 1112 | |
| 1113 | bool WebRtcVideoChannel2::SendIntraFrame() { |
| 1114 | // TODO(pbos): Implement. |
| 1115 | LOG(LS_VERBOSE) << "SendIntraFrame()."; |
| 1116 | return true; |
| 1117 | } |
| 1118 | |
| 1119 | bool WebRtcVideoChannel2::RequestIntraFrame() { |
| 1120 | // TODO(pbos): Implement. |
| 1121 | LOG(LS_VERBOSE) << "SendIntraFrame()."; |
| 1122 | return true; |
| 1123 | } |
| 1124 | |
| 1125 | void WebRtcVideoChannel2::OnPacketReceived( |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1126 | rtc::Buffer* packet, |
| 1127 | const rtc::PacketTime& packet_time) { |
pbos@webrtc.org | 4e545cc | 2014-05-14 13:58:13 +0000 | [diff] [blame] | 1128 | const webrtc::PacketReceiver::DeliveryStatus delivery_result = |
| 1129 | call_->Receiver()->DeliverPacket( |
| 1130 | reinterpret_cast<const uint8_t*>(packet->data()), packet->length()); |
| 1131 | switch (delivery_result) { |
| 1132 | case webrtc::PacketReceiver::DELIVERY_OK: |
| 1133 | return; |
| 1134 | case webrtc::PacketReceiver::DELIVERY_PACKET_ERROR: |
| 1135 | return; |
| 1136 | case webrtc::PacketReceiver::DELIVERY_UNKNOWN_SSRC: |
| 1137 | break; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1138 | } |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1139 | |
| 1140 | uint32 ssrc = 0; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1141 | if (!GetRtpSsrc(packet->data(), packet->length(), &ssrc)) { |
| 1142 | return; |
| 1143 | } |
| 1144 | |
pbos@webrtc.org | afb554f4 | 2014-08-12 23:17:13 +0000 | [diff] [blame] | 1145 | // TODO(pbos): Make sure that the unsignalled SSRC uses the video payload. |
| 1146 | // Also figure out whether RTX needs to be handled. |
| 1147 | switch (unsignalled_ssrc_handler_->OnUnsignalledSsrc(this, ssrc)) { |
| 1148 | case UnsignalledSsrcHandler::kDropPacket: |
| 1149 | return; |
| 1150 | case UnsignalledSsrcHandler::kDeliverPacket: |
| 1151 | break; |
| 1152 | } |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1153 | |
pbos@webrtc.org | 1e019d1 | 2014-05-16 11:38:45 +0000 | [diff] [blame] | 1154 | if (call_->Receiver()->DeliverPacket( |
| 1155 | reinterpret_cast<const uint8_t*>(packet->data()), packet->length()) != |
| 1156 | webrtc::PacketReceiver::DELIVERY_OK) { |
pbos@webrtc.org | afb554f4 | 2014-08-12 23:17:13 +0000 | [diff] [blame] | 1157 | LOG(LS_WARNING) << "Failed to deliver RTP packet on re-delivery."; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1158 | return; |
| 1159 | } |
| 1160 | } |
| 1161 | |
| 1162 | void WebRtcVideoChannel2::OnRtcpReceived( |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1163 | rtc::Buffer* packet, |
| 1164 | const rtc::PacketTime& packet_time) { |
pbos@webrtc.org | 1e019d1 | 2014-05-16 11:38:45 +0000 | [diff] [blame] | 1165 | if (call_->Receiver()->DeliverPacket( |
| 1166 | reinterpret_cast<const uint8_t*>(packet->data()), packet->length()) != |
| 1167 | webrtc::PacketReceiver::DELIVERY_OK) { |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1168 | LOG(LS_WARNING) << "Failed to deliver RTCP packet."; |
| 1169 | } |
| 1170 | } |
| 1171 | |
| 1172 | void WebRtcVideoChannel2::OnReadyToSend(bool ready) { |
| 1173 | LOG(LS_VERBOSE) << "OnReadySend: " << (ready ? "Ready." : "Not ready."); |
| 1174 | } |
| 1175 | |
| 1176 | bool WebRtcVideoChannel2::MuteStream(uint32 ssrc, bool mute) { |
| 1177 | LOG(LS_VERBOSE) << "MuteStream: " << ssrc << " -> " |
| 1178 | << (mute ? "mute" : "unmute"); |
| 1179 | assert(ssrc != 0); |
| 1180 | if (send_streams_.find(ssrc) == send_streams_.end()) { |
| 1181 | LOG(LS_ERROR) << "No sending stream on ssrc " << ssrc; |
| 1182 | return false; |
| 1183 | } |
| 1184 | return send_streams_[ssrc]->MuteStream(mute); |
| 1185 | } |
| 1186 | |
| 1187 | bool WebRtcVideoChannel2::SetRecvRtpHeaderExtensions( |
| 1188 | const std::vector<RtpHeaderExtension>& extensions) { |
pbos@webrtc.org | 587ef60 | 2014-06-16 17:32:02 +0000 | [diff] [blame] | 1189 | LOG(LS_INFO) << "SetRecvRtpHeaderExtensions: " |
| 1190 | << RtpExtensionsToString(extensions); |
pbos@webrtc.org | 3c10758 | 2014-07-20 15:27:35 +0000 | [diff] [blame] | 1191 | if (!ValidateRtpHeaderExtensionIds(extensions)) |
| 1192 | return false; |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 1193 | |
pbos@webrtc.org | 3c10758 | 2014-07-20 15:27:35 +0000 | [diff] [blame] | 1194 | recv_rtp_extensions_ = FilterRtpExtensions(extensions); |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 1195 | for (std::map<uint32, WebRtcVideoReceiveStream*>::iterator it = |
| 1196 | receive_streams_.begin(); |
| 1197 | it != receive_streams_.end(); |
| 1198 | ++it) { |
| 1199 | it->second->SetRtpExtensions(recv_rtp_extensions_); |
| 1200 | } |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1201 | return true; |
| 1202 | } |
| 1203 | |
| 1204 | bool WebRtcVideoChannel2::SetSendRtpHeaderExtensions( |
| 1205 | const std::vector<RtpHeaderExtension>& extensions) { |
pbos@webrtc.org | 587ef60 | 2014-06-16 17:32:02 +0000 | [diff] [blame] | 1206 | LOG(LS_INFO) << "SetSendRtpHeaderExtensions: " |
| 1207 | << RtpExtensionsToString(extensions); |
pbos@webrtc.org | 3c10758 | 2014-07-20 15:27:35 +0000 | [diff] [blame] | 1208 | if (!ValidateRtpHeaderExtensionIds(extensions)) |
| 1209 | return false; |
| 1210 | |
| 1211 | send_rtp_extensions_ = FilterRtpExtensions(extensions); |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 1212 | for (std::map<uint32, WebRtcVideoSendStream*>::iterator it = |
| 1213 | send_streams_.begin(); |
| 1214 | it != send_streams_.end(); |
| 1215 | ++it) { |
| 1216 | it->second->SetRtpExtensions(send_rtp_extensions_); |
| 1217 | } |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1218 | return true; |
| 1219 | } |
| 1220 | |
| 1221 | bool WebRtcVideoChannel2::SetStartSendBandwidth(int bps) { |
| 1222 | // TODO(pbos): Implement. |
| 1223 | LOG(LS_VERBOSE) << "SetStartSendBandwidth: " << bps; |
| 1224 | return true; |
| 1225 | } |
| 1226 | |
| 1227 | bool WebRtcVideoChannel2::SetMaxSendBandwidth(int bps) { |
| 1228 | // TODO(pbos): Implement. |
| 1229 | LOG(LS_VERBOSE) << "SetMaxSendBandwidth: " << bps; |
| 1230 | return true; |
| 1231 | } |
| 1232 | |
| 1233 | bool WebRtcVideoChannel2::SetOptions(const VideoOptions& options) { |
| 1234 | LOG(LS_VERBOSE) << "SetOptions: " << options.ToString(); |
| 1235 | options_.SetAll(options); |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1236 | for (std::map<uint32, WebRtcVideoSendStream*>::iterator it = |
| 1237 | send_streams_.begin(); |
| 1238 | it != send_streams_.end(); |
| 1239 | ++it) { |
| 1240 | it->second->SetOptions(options_); |
| 1241 | } |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1242 | return true; |
| 1243 | } |
| 1244 | |
| 1245 | void WebRtcVideoChannel2::SetInterface(NetworkInterface* iface) { |
| 1246 | MediaChannel::SetInterface(iface); |
| 1247 | // Set the RTP recv/send buffer to a bigger size |
| 1248 | MediaChannel::SetOption(NetworkInterface::ST_RTP, |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1249 | rtc::Socket::OPT_RCVBUF, |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1250 | kVideoRtpBufferSize); |
| 1251 | |
| 1252 | // TODO(sriniv): Remove or re-enable this. |
| 1253 | // As part of b/8030474, send-buffer is size now controlled through |
| 1254 | // portallocator flags. |
| 1255 | // network_interface_->SetOption(NetworkInterface::ST_RTP, |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1256 | // rtc::Socket::OPT_SNDBUF, |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1257 | // kVideoRtpBufferSize); |
| 1258 | } |
| 1259 | |
| 1260 | void WebRtcVideoChannel2::UpdateAspectRatio(int ratio_w, int ratio_h) { |
| 1261 | // TODO(pbos): Implement. |
| 1262 | } |
| 1263 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1264 | void WebRtcVideoChannel2::OnMessage(rtc::Message* msg) { |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1265 | // Ignored. |
| 1266 | } |
| 1267 | |
| 1268 | bool WebRtcVideoChannel2::SendRtp(const uint8_t* data, size_t len) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1269 | rtc::Buffer packet(data, len, kMaxRtpPacketLen); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1270 | return MediaChannel::SendPacket(&packet); |
| 1271 | } |
| 1272 | |
| 1273 | bool WebRtcVideoChannel2::SendRtcp(const uint8_t* data, size_t len) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1274 | rtc::Buffer packet(data, len, kMaxRtpPacketLen); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1275 | return MediaChannel::SendRtcp(&packet); |
| 1276 | } |
| 1277 | |
| 1278 | void WebRtcVideoChannel2::StartAllSendStreams() { |
| 1279 | for (std::map<uint32, WebRtcVideoSendStream*>::iterator it = |
| 1280 | send_streams_.begin(); |
| 1281 | it != send_streams_.end(); |
| 1282 | ++it) { |
| 1283 | it->second->Start(); |
| 1284 | } |
| 1285 | } |
| 1286 | |
| 1287 | void WebRtcVideoChannel2::StopAllSendStreams() { |
| 1288 | for (std::map<uint32, WebRtcVideoSendStream*>::iterator it = |
| 1289 | send_streams_.begin(); |
| 1290 | it != send_streams_.end(); |
| 1291 | ++it) { |
| 1292 | it->second->Stop(); |
| 1293 | } |
| 1294 | } |
| 1295 | |
pbos@webrtc.org | 6ae48c6 | 2014-06-06 10:49:19 +0000 | [diff] [blame] | 1296 | WebRtcVideoChannel2::WebRtcVideoSendStream::VideoSendStreamParameters:: |
| 1297 | VideoSendStreamParameters( |
| 1298 | const webrtc::VideoSendStream::Config& config, |
| 1299 | const VideoOptions& options, |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1300 | const Settable<VideoCodecSettings>& codec_settings) |
| 1301 | : config(config), options(options), codec_settings(codec_settings) { |
pbos@webrtc.org | 6ae48c6 | 2014-06-06 10:49:19 +0000 | [diff] [blame] | 1302 | } |
| 1303 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1304 | WebRtcVideoChannel2::WebRtcVideoSendStream::WebRtcVideoSendStream( |
| 1305 | webrtc::Call* call, |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1306 | WebRtcVideoEncoderFactory2* encoder_factory, |
pbos@webrtc.org | 6ae48c6 | 2014-06-06 10:49:19 +0000 | [diff] [blame] | 1307 | const VideoOptions& options, |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1308 | const Settable<VideoCodecSettings>& codec_settings, |
| 1309 | const StreamParams& sp, |
| 1310 | const std::vector<webrtc::RtpExtension>& rtp_extensions) |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1311 | : call_(call), |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1312 | parameters_(webrtc::VideoSendStream::Config(), options, codec_settings), |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1313 | encoder_factory_(encoder_factory), |
| 1314 | capturer_(NULL), |
| 1315 | stream_(NULL), |
| 1316 | sending_(false), |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1317 | muted_(false) { |
| 1318 | parameters_.config.rtp.max_packet_size = kVideoMtu; |
| 1319 | |
| 1320 | sp.GetPrimarySsrcs(¶meters_.config.rtp.ssrcs); |
| 1321 | sp.GetFidSsrcs(parameters_.config.rtp.ssrcs, |
| 1322 | ¶meters_.config.rtp.rtx.ssrcs); |
| 1323 | parameters_.config.rtp.c_name = sp.cname; |
| 1324 | parameters_.config.rtp.extensions = rtp_extensions; |
| 1325 | |
| 1326 | VideoCodecSettings params; |
| 1327 | if (codec_settings.Get(¶ms)) { |
| 1328 | SetCodec(params); |
| 1329 | } |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1330 | } |
| 1331 | |
| 1332 | WebRtcVideoChannel2::WebRtcVideoSendStream::~WebRtcVideoSendStream() { |
| 1333 | DisconnectCapturer(); |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1334 | if (stream_ != NULL) { |
| 1335 | call_->DestroyVideoSendStream(stream_); |
| 1336 | } |
pbos@webrtc.org | 6ae48c6 | 2014-06-06 10:49:19 +0000 | [diff] [blame] | 1337 | delete parameters_.config.encoder_settings.encoder; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1338 | } |
| 1339 | |
| 1340 | static void SetWebRtcFrameToBlack(webrtc::I420VideoFrame* video_frame) { |
| 1341 | assert(video_frame != NULL); |
| 1342 | memset(video_frame->buffer(webrtc::kYPlane), |
| 1343 | 16, |
| 1344 | video_frame->allocated_size(webrtc::kYPlane)); |
| 1345 | memset(video_frame->buffer(webrtc::kUPlane), |
| 1346 | 128, |
| 1347 | video_frame->allocated_size(webrtc::kUPlane)); |
| 1348 | memset(video_frame->buffer(webrtc::kVPlane), |
| 1349 | 128, |
| 1350 | video_frame->allocated_size(webrtc::kVPlane)); |
| 1351 | } |
| 1352 | |
| 1353 | static void CreateBlackFrame(webrtc::I420VideoFrame* video_frame, |
| 1354 | int width, |
| 1355 | int height) { |
| 1356 | video_frame->CreateEmptyFrame( |
| 1357 | width, height, width, (width + 1) / 2, (width + 1) / 2); |
| 1358 | SetWebRtcFrameToBlack(video_frame); |
| 1359 | } |
| 1360 | |
| 1361 | static void ConvertToI420VideoFrame(const VideoFrame& frame, |
| 1362 | webrtc::I420VideoFrame* i420_frame) { |
| 1363 | i420_frame->CreateFrame( |
| 1364 | static_cast<int>(frame.GetYPitch() * frame.GetHeight()), |
| 1365 | frame.GetYPlane(), |
| 1366 | static_cast<int>(frame.GetUPitch() * ((frame.GetHeight() + 1) / 2)), |
| 1367 | frame.GetUPlane(), |
| 1368 | static_cast<int>(frame.GetVPitch() * ((frame.GetHeight() + 1) / 2)), |
| 1369 | frame.GetVPlane(), |
| 1370 | static_cast<int>(frame.GetWidth()), |
| 1371 | static_cast<int>(frame.GetHeight()), |
| 1372 | static_cast<int>(frame.GetYPitch()), |
| 1373 | static_cast<int>(frame.GetUPitch()), |
| 1374 | static_cast<int>(frame.GetVPitch())); |
| 1375 | } |
| 1376 | |
| 1377 | void WebRtcVideoChannel2::WebRtcVideoSendStream::InputFrame( |
| 1378 | VideoCapturer* capturer, |
| 1379 | const VideoFrame* frame) { |
| 1380 | LOG(LS_VERBOSE) << "InputFrame: " << frame->GetWidth() << "x" |
| 1381 | << frame->GetHeight(); |
| 1382 | bool is_screencast = capturer->IsScreencast(); |
| 1383 | // Lock before copying, can be called concurrently when swapping input source. |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1384 | rtc::CritScope frame_cs(&frame_lock_); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1385 | if (!muted_) { |
| 1386 | ConvertToI420VideoFrame(*frame, &video_frame_); |
| 1387 | } else { |
| 1388 | // Create a tiny black frame to transmit instead. |
| 1389 | CreateBlackFrame(&video_frame_, 1, 1); |
| 1390 | is_screencast = false; |
| 1391 | } |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1392 | rtc::CritScope cs(&lock_); |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1393 | if (stream_ == NULL) { |
| 1394 | LOG(LS_WARNING) << "Capturer inputting frames before send codecs are " |
| 1395 | "configured, dropping."; |
| 1396 | return; |
| 1397 | } |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1398 | if (format_.width == 0) { // Dropping frames. |
| 1399 | assert(format_.height == 0); |
| 1400 | LOG(LS_VERBOSE) << "VideoFormat 0x0 set, Dropping frame."; |
| 1401 | return; |
| 1402 | } |
| 1403 | // Reconfigure codec if necessary. |
| 1404 | if (is_screencast) { |
| 1405 | SetDimensions(video_frame_.width(), video_frame_.height()); |
| 1406 | } |
| 1407 | LOG(LS_VERBOSE) << "SwapFrame: " << video_frame_.width() << "x" |
| 1408 | << video_frame_.height() << " -> (codec) " |
pbos@webrtc.org | 6ae48c6 | 2014-06-06 10:49:19 +0000 | [diff] [blame] | 1409 | << parameters_.video_streams.back().width << "x" |
| 1410 | << parameters_.video_streams.back().height; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1411 | stream_->Input()->SwapFrame(&video_frame_); |
| 1412 | } |
| 1413 | |
| 1414 | bool WebRtcVideoChannel2::WebRtcVideoSendStream::SetCapturer( |
| 1415 | VideoCapturer* capturer) { |
| 1416 | if (!DisconnectCapturer() && capturer == NULL) { |
| 1417 | return false; |
| 1418 | } |
| 1419 | |
| 1420 | { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1421 | rtc::CritScope cs(&lock_); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1422 | |
pbos@webrtc.org | 9359cb3 | 2014-07-23 15:44:48 +0000 | [diff] [blame] | 1423 | if (capturer == NULL) { |
| 1424 | if (stream_ != NULL) { |
| 1425 | LOG(LS_VERBOSE) << "Disabling capturer, sending black frame."; |
| 1426 | webrtc::I420VideoFrame black_frame; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1427 | |
pbos@webrtc.org | 9359cb3 | 2014-07-23 15:44:48 +0000 | [diff] [blame] | 1428 | int width = format_.width; |
| 1429 | int height = format_.height; |
| 1430 | int half_width = (width + 1) / 2; |
| 1431 | black_frame.CreateEmptyFrame( |
| 1432 | width, height, width, half_width, half_width); |
| 1433 | SetWebRtcFrameToBlack(&black_frame); |
| 1434 | SetDimensions(width, height); |
| 1435 | stream_->Input()->SwapFrame(&black_frame); |
| 1436 | } |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1437 | |
| 1438 | capturer_ = NULL; |
| 1439 | return true; |
| 1440 | } |
| 1441 | |
| 1442 | capturer_ = capturer; |
| 1443 | } |
| 1444 | // Lock cannot be held while connecting the capturer to prevent lock-order |
| 1445 | // violations. |
| 1446 | capturer->SignalVideoFrame.connect(this, &WebRtcVideoSendStream::InputFrame); |
| 1447 | return true; |
| 1448 | } |
| 1449 | |
| 1450 | bool WebRtcVideoChannel2::WebRtcVideoSendStream::SetVideoFormat( |
| 1451 | const VideoFormat& format) { |
| 1452 | if ((format.width == 0 || format.height == 0) && |
| 1453 | format.width != format.height) { |
| 1454 | LOG(LS_ERROR) << "Can't set VideoFormat, width or height is zero (but not " |
| 1455 | "both, 0x0 drops frames)."; |
| 1456 | return false; |
| 1457 | } |
| 1458 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1459 | rtc::CritScope cs(&lock_); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1460 | if (format.width == 0 && format.height == 0) { |
| 1461 | LOG(LS_INFO) |
| 1462 | << "0x0 resolution selected. Captured frames will be dropped for ssrc: " |
pbos@webrtc.org | 6ae48c6 | 2014-06-06 10:49:19 +0000 | [diff] [blame] | 1463 | << parameters_.config.rtp.ssrcs[0] << "."; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1464 | } else { |
| 1465 | // TODO(pbos): Fix me, this only affects the last stream! |
pbos@webrtc.org | 6ae48c6 | 2014-06-06 10:49:19 +0000 | [diff] [blame] | 1466 | parameters_.video_streams.back().max_framerate = |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1467 | VideoFormat::IntervalToFps(format.interval); |
| 1468 | SetDimensions(format.width, format.height); |
| 1469 | } |
| 1470 | |
| 1471 | format_ = format; |
| 1472 | return true; |
| 1473 | } |
| 1474 | |
| 1475 | bool WebRtcVideoChannel2::WebRtcVideoSendStream::MuteStream(bool mute) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1476 | rtc::CritScope cs(&lock_); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1477 | bool was_muted = muted_; |
| 1478 | muted_ = mute; |
| 1479 | return was_muted != mute; |
| 1480 | } |
| 1481 | |
| 1482 | bool WebRtcVideoChannel2::WebRtcVideoSendStream::DisconnectCapturer() { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1483 | rtc::CritScope cs(&lock_); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1484 | if (capturer_ == NULL) { |
| 1485 | return false; |
| 1486 | } |
| 1487 | capturer_->SignalVideoFrame.disconnect(this); |
| 1488 | capturer_ = NULL; |
| 1489 | return true; |
| 1490 | } |
| 1491 | |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1492 | void WebRtcVideoChannel2::WebRtcVideoSendStream::SetOptions( |
| 1493 | const VideoOptions& options) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1494 | rtc::CritScope cs(&lock_); |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1495 | VideoCodecSettings codec_settings; |
| 1496 | if (parameters_.codec_settings.Get(&codec_settings)) { |
| 1497 | SetCodecAndOptions(codec_settings, options); |
| 1498 | } else { |
| 1499 | parameters_.options = options; |
| 1500 | } |
| 1501 | } |
| 1502 | void WebRtcVideoChannel2::WebRtcVideoSendStream::SetCodec( |
| 1503 | const VideoCodecSettings& codec_settings) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1504 | rtc::CritScope cs(&lock_); |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1505 | SetCodecAndOptions(codec_settings, parameters_.options); |
| 1506 | } |
| 1507 | void WebRtcVideoChannel2::WebRtcVideoSendStream::SetCodecAndOptions( |
| 1508 | const VideoCodecSettings& codec_settings, |
| 1509 | const VideoOptions& options) { |
pbos@webrtc.org | 6ae48c6 | 2014-06-06 10:49:19 +0000 | [diff] [blame] | 1510 | std::vector<webrtc::VideoStream> video_streams = |
| 1511 | encoder_factory_->CreateVideoStreams( |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1512 | codec_settings.codec, options, parameters_.config.rtp.ssrcs.size()); |
pbos@webrtc.org | 6ae48c6 | 2014-06-06 10:49:19 +0000 | [diff] [blame] | 1513 | if (video_streams.empty()) { |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1514 | return; |
| 1515 | } |
pbos@webrtc.org | 6ae48c6 | 2014-06-06 10:49:19 +0000 | [diff] [blame] | 1516 | parameters_.video_streams = video_streams; |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1517 | format_ = VideoFormat(codec_settings.codec.width, |
| 1518 | codec_settings.codec.height, |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1519 | VideoFormat::FpsToInterval(30), |
| 1520 | FOURCC_I420); |
pbos@webrtc.org | 6ae48c6 | 2014-06-06 10:49:19 +0000 | [diff] [blame] | 1521 | |
| 1522 | webrtc::VideoEncoder* old_encoder = |
| 1523 | parameters_.config.encoder_settings.encoder; |
| 1524 | parameters_.config.encoder_settings.encoder = |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1525 | encoder_factory_->CreateVideoEncoder(codec_settings.codec, options); |
| 1526 | parameters_.config.encoder_settings.payload_name = codec_settings.codec.name; |
| 1527 | parameters_.config.encoder_settings.payload_type = codec_settings.codec.id; |
| 1528 | parameters_.config.rtp.fec = codec_settings.fec; |
| 1529 | |
| 1530 | // Set RTX payload type if RTX is enabled. |
| 1531 | if (!parameters_.config.rtp.rtx.ssrcs.empty()) { |
| 1532 | parameters_.config.rtp.rtx.payload_type = codec_settings.rtx_payload_type; |
pbos@webrtc.org | 543e589 | 2014-07-23 07:01:31 +0000 | [diff] [blame] | 1533 | |
| 1534 | options.use_payload_padding.Get( |
| 1535 | ¶meters_.config.rtp.rtx.pad_with_redundant_payloads); |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1536 | } |
| 1537 | |
| 1538 | if (IsNackEnabled(codec_settings.codec)) { |
| 1539 | parameters_.config.rtp.nack.rtp_history_ms = kNackHistoryMs; |
| 1540 | } |
| 1541 | |
pbos@webrtc.org | 5ff71ab | 2014-07-23 07:28:56 +0000 | [diff] [blame] | 1542 | options.suspend_below_min_bitrate.Get( |
| 1543 | ¶meters_.config.suspend_below_min_bitrate); |
| 1544 | |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1545 | parameters_.codec_settings.Set(codec_settings); |
pbos@webrtc.org | 6ae48c6 | 2014-06-06 10:49:19 +0000 | [diff] [blame] | 1546 | parameters_.options = options; |
pbos@webrtc.org | 543e589 | 2014-07-23 07:01:31 +0000 | [diff] [blame] | 1547 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1548 | RecreateWebRtcStream(); |
| 1549 | delete old_encoder; |
| 1550 | } |
| 1551 | |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 1552 | void WebRtcVideoChannel2::WebRtcVideoSendStream::SetRtpExtensions( |
| 1553 | const std::vector<webrtc::RtpExtension>& rtp_extensions) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1554 | rtc::CritScope cs(&lock_); |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 1555 | parameters_.config.rtp.extensions = rtp_extensions; |
| 1556 | RecreateWebRtcStream(); |
| 1557 | } |
| 1558 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1559 | void WebRtcVideoChannel2::WebRtcVideoSendStream::SetDimensions(int width, |
pbos@webrtc.org | 6ae48c6 | 2014-06-06 10:49:19 +0000 | [diff] [blame] | 1560 | int height) { |
| 1561 | assert(!parameters_.video_streams.empty()); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1562 | LOG(LS_VERBOSE) << "SetDimensions: " << width << "x" << height; |
pbos@webrtc.org | 6ae48c6 | 2014-06-06 10:49:19 +0000 | [diff] [blame] | 1563 | if (parameters_.video_streams.back().width == width && |
| 1564 | parameters_.video_streams.back().height == height) { |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1565 | return; |
| 1566 | } |
| 1567 | |
| 1568 | // TODO(pbos): Fix me, this only affects the last stream! |
pbos@webrtc.org | 6ae48c6 | 2014-06-06 10:49:19 +0000 | [diff] [blame] | 1569 | parameters_.video_streams.back().width = width; |
| 1570 | parameters_.video_streams.back().height = height; |
| 1571 | |
pbos@webrtc.org | 6f48f1b | 2014-07-22 16:29:54 +0000 | [diff] [blame] | 1572 | VideoCodecSettings codec_settings; |
| 1573 | parameters_.codec_settings.Get(&codec_settings); |
| 1574 | void* encoder_settings = encoder_factory_->CreateVideoEncoderSettings( |
| 1575 | codec_settings.codec, parameters_.options); |
| 1576 | |
| 1577 | bool stream_reconfigured = stream_->ReconfigureVideoEncoder( |
| 1578 | parameters_.video_streams, encoder_settings); |
| 1579 | |
| 1580 | encoder_factory_->DestroyVideoEncoderSettings(codec_settings.codec, |
| 1581 | encoder_settings); |
| 1582 | |
| 1583 | if (!stream_reconfigured) { |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1584 | LOG(LS_WARNING) << "Failed to reconfigure video encoder for dimensions: " |
| 1585 | << width << "x" << height; |
| 1586 | return; |
| 1587 | } |
| 1588 | } |
| 1589 | |
| 1590 | void WebRtcVideoChannel2::WebRtcVideoSendStream::Start() { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1591 | rtc::CritScope cs(&lock_); |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1592 | assert(stream_ != NULL); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1593 | stream_->Start(); |
| 1594 | sending_ = true; |
| 1595 | } |
| 1596 | |
| 1597 | void WebRtcVideoChannel2::WebRtcVideoSendStream::Stop() { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1598 | rtc::CritScope cs(&lock_); |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1599 | if (stream_ != NULL) { |
| 1600 | stream_->Stop(); |
| 1601 | } |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1602 | sending_ = false; |
| 1603 | } |
| 1604 | |
pbos@webrtc.org | e6f84ae | 2014-07-18 11:11:55 +0000 | [diff] [blame] | 1605 | VideoSenderInfo |
| 1606 | WebRtcVideoChannel2::WebRtcVideoSendStream::GetVideoSenderInfo() { |
| 1607 | VideoSenderInfo info; |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1608 | rtc::CritScope cs(&lock_); |
pbos@webrtc.org | e6f84ae | 2014-07-18 11:11:55 +0000 | [diff] [blame] | 1609 | for (size_t i = 0; i < parameters_.config.rtp.ssrcs.size(); ++i) { |
| 1610 | info.add_ssrc(parameters_.config.rtp.ssrcs[i]); |
| 1611 | } |
| 1612 | |
pbos@webrtc.org | c3d2bd2 | 2014-08-12 20:55:10 +0000 | [diff] [blame] | 1613 | if (stream_ == NULL) { |
| 1614 | return info; |
| 1615 | } |
| 1616 | |
pbos@webrtc.org | e6f84ae | 2014-07-18 11:11:55 +0000 | [diff] [blame] | 1617 | webrtc::VideoSendStream::Stats stats = stream_->GetStats(); |
| 1618 | info.framerate_input = stats.input_frame_rate; |
| 1619 | info.framerate_sent = stats.encode_frame_rate; |
| 1620 | |
| 1621 | for (std::map<uint32_t, webrtc::StreamStats>::iterator it = |
| 1622 | stats.substreams.begin(); |
| 1623 | it != stats.substreams.end(); |
| 1624 | ++it) { |
| 1625 | // TODO(pbos): Wire up additional stats, such as padding bytes. |
| 1626 | webrtc::StreamStats stream_stats = it->second; |
| 1627 | info.bytes_sent += stream_stats.rtp_stats.bytes + |
| 1628 | stream_stats.rtp_stats.header_bytes + |
| 1629 | stream_stats.rtp_stats.padding_bytes; |
| 1630 | info.packets_sent += stream_stats.rtp_stats.packets; |
| 1631 | info.packets_lost += stream_stats.rtcp_stats.cumulative_lost; |
| 1632 | } |
| 1633 | |
| 1634 | if (!stats.substreams.empty()) { |
| 1635 | // TODO(pbos): Report fraction lost per SSRC. |
| 1636 | webrtc::StreamStats first_stream_stats = stats.substreams.begin()->second; |
| 1637 | info.fraction_lost = |
| 1638 | static_cast<float>(first_stream_stats.rtcp_stats.fraction_lost) / |
| 1639 | (1 << 8); |
| 1640 | } |
| 1641 | |
| 1642 | if (capturer_ != NULL && !capturer_->IsMuted()) { |
| 1643 | VideoFormat last_captured_frame_format; |
| 1644 | capturer_->GetStats(&info.adapt_frame_drops, |
| 1645 | &info.effects_frame_drops, |
| 1646 | &info.capturer_frame_time, |
| 1647 | &last_captured_frame_format); |
| 1648 | info.input_frame_width = last_captured_frame_format.width; |
| 1649 | info.input_frame_height = last_captured_frame_format.height; |
| 1650 | info.send_frame_width = |
| 1651 | static_cast<int>(parameters_.video_streams.front().width); |
| 1652 | info.send_frame_height = |
| 1653 | static_cast<int>(parameters_.video_streams.front().height); |
| 1654 | } |
| 1655 | |
| 1656 | // TODO(pbos): Support or remove the following stats. |
| 1657 | info.packets_cached = -1; |
| 1658 | info.rtt_ms = -1; |
| 1659 | |
| 1660 | return info; |
| 1661 | } |
| 1662 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1663 | void WebRtcVideoChannel2::WebRtcVideoSendStream::RecreateWebRtcStream() { |
| 1664 | if (stream_ != NULL) { |
| 1665 | call_->DestroyVideoSendStream(stream_); |
| 1666 | } |
pbos@webrtc.org | 6ae48c6 | 2014-06-06 10:49:19 +0000 | [diff] [blame] | 1667 | |
pbos@webrtc.org | 6f48f1b | 2014-07-22 16:29:54 +0000 | [diff] [blame] | 1668 | VideoCodecSettings codec_settings; |
| 1669 | parameters_.codec_settings.Get(&codec_settings); |
| 1670 | void* encoder_settings = encoder_factory_->CreateVideoEncoderSettings( |
| 1671 | codec_settings.codec, parameters_.options); |
| 1672 | |
pbos@webrtc.org | 6ae48c6 | 2014-06-06 10:49:19 +0000 | [diff] [blame] | 1673 | stream_ = call_->CreateVideoSendStream( |
pbos@webrtc.org | 6f48f1b | 2014-07-22 16:29:54 +0000 | [diff] [blame] | 1674 | parameters_.config, parameters_.video_streams, encoder_settings); |
| 1675 | |
| 1676 | encoder_factory_->DestroyVideoEncoderSettings(codec_settings.codec, |
| 1677 | encoder_settings); |
| 1678 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1679 | if (sending_) { |
| 1680 | stream_->Start(); |
| 1681 | } |
| 1682 | } |
| 1683 | |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 1684 | WebRtcVideoChannel2::WebRtcVideoReceiveStream::WebRtcVideoReceiveStream( |
| 1685 | webrtc::Call* call, |
| 1686 | const webrtc::VideoReceiveStream::Config& config, |
| 1687 | const std::vector<VideoCodecSettings>& recv_codecs) |
| 1688 | : call_(call), |
| 1689 | config_(config), |
| 1690 | stream_(NULL), |
| 1691 | last_width_(-1), |
| 1692 | last_height_(-1), |
| 1693 | renderer_(NULL) { |
| 1694 | config_.renderer = this; |
| 1695 | // SetRecvCodecs will also reset (start) the VideoReceiveStream. |
| 1696 | SetRecvCodecs(recv_codecs); |
| 1697 | } |
| 1698 | |
| 1699 | WebRtcVideoChannel2::WebRtcVideoReceiveStream::~WebRtcVideoReceiveStream() { |
| 1700 | call_->DestroyVideoReceiveStream(stream_); |
| 1701 | } |
| 1702 | |
| 1703 | void WebRtcVideoChannel2::WebRtcVideoReceiveStream::SetRecvCodecs( |
| 1704 | const std::vector<VideoCodecSettings>& recv_codecs) { |
| 1705 | // TODO(pbos): Reconfigure RTX based on incoming recv_codecs. |
| 1706 | // TODO(pbos): Base receive codecs off recv_codecs_ and set up using a |
| 1707 | // DecoderFactory similar to send side. Pending webrtc:2854. |
| 1708 | // Also set up default codecs if there's nothing in recv_codecs_. |
| 1709 | webrtc::VideoCodec codec; |
| 1710 | memset(&codec, 0, sizeof(codec)); |
| 1711 | |
| 1712 | codec.plType = kDefaultVideoCodecPref.payload_type; |
| 1713 | strcpy(codec.plName, kDefaultVideoCodecPref.name); |
| 1714 | codec.codecType = webrtc::kVideoCodecVP8; |
| 1715 | codec.codecSpecific.VP8.resilience = webrtc::kResilientStream; |
| 1716 | codec.codecSpecific.VP8.numberOfTemporalLayers = 1; |
| 1717 | codec.codecSpecific.VP8.denoisingOn = true; |
| 1718 | codec.codecSpecific.VP8.errorConcealmentOn = false; |
| 1719 | codec.codecSpecific.VP8.automaticResizeOn = false; |
| 1720 | codec.codecSpecific.VP8.frameDroppingOn = true; |
| 1721 | codec.codecSpecific.VP8.keyFrameInterval = 3000; |
| 1722 | // Bitrates don't matter and are ignored for the receiver. This is put in to |
| 1723 | // have the current underlying implementation accept the VideoCodec. |
| 1724 | codec.minBitrate = codec.startBitrate = codec.maxBitrate = 300; |
| 1725 | config_.codecs.clear(); |
| 1726 | config_.codecs.push_back(codec); |
| 1727 | |
| 1728 | config_.rtp.fec = recv_codecs.front().fec; |
| 1729 | |
pbos@webrtc.org | 257e130 | 2014-07-25 19:01:32 +0000 | [diff] [blame] | 1730 | config_.rtp.nack.rtp_history_ms = |
| 1731 | IsNackEnabled(recv_codecs.begin()->codec) ? kNackHistoryMs : 0; |
| 1732 | config_.rtp.remb = IsRembEnabled(recv_codecs.begin()->codec); |
| 1733 | |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 1734 | RecreateWebRtcStream(); |
| 1735 | } |
| 1736 | |
| 1737 | void WebRtcVideoChannel2::WebRtcVideoReceiveStream::SetRtpExtensions( |
| 1738 | const std::vector<webrtc::RtpExtension>& extensions) { |
| 1739 | config_.rtp.extensions = extensions; |
| 1740 | RecreateWebRtcStream(); |
| 1741 | } |
| 1742 | |
| 1743 | void WebRtcVideoChannel2::WebRtcVideoReceiveStream::RecreateWebRtcStream() { |
| 1744 | if (stream_ != NULL) { |
| 1745 | call_->DestroyVideoReceiveStream(stream_); |
| 1746 | } |
| 1747 | stream_ = call_->CreateVideoReceiveStream(config_); |
| 1748 | stream_->Start(); |
| 1749 | } |
| 1750 | |
| 1751 | void WebRtcVideoChannel2::WebRtcVideoReceiveStream::RenderFrame( |
| 1752 | const webrtc::I420VideoFrame& frame, |
| 1753 | int time_to_render_ms) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1754 | rtc::CritScope crit(&renderer_lock_); |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 1755 | if (renderer_ == NULL) { |
| 1756 | LOG(LS_WARNING) << "VideoReceiveStream not connected to a VideoRenderer."; |
| 1757 | return; |
| 1758 | } |
| 1759 | |
| 1760 | if (frame.width() != last_width_ || frame.height() != last_height_) { |
| 1761 | SetSize(frame.width(), frame.height()); |
| 1762 | } |
| 1763 | |
| 1764 | LOG(LS_VERBOSE) << "RenderFrame: (" << frame.width() << "x" << frame.height() |
| 1765 | << ")"; |
| 1766 | |
| 1767 | const WebRtcVideoRenderFrame render_frame(&frame); |
| 1768 | renderer_->RenderFrame(&render_frame); |
| 1769 | } |
| 1770 | |
| 1771 | void WebRtcVideoChannel2::WebRtcVideoReceiveStream::SetRenderer( |
| 1772 | cricket::VideoRenderer* renderer) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1773 | rtc::CritScope crit(&renderer_lock_); |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 1774 | renderer_ = renderer; |
| 1775 | if (renderer_ != NULL && last_width_ != -1) { |
| 1776 | SetSize(last_width_, last_height_); |
| 1777 | } |
| 1778 | } |
| 1779 | |
| 1780 | VideoRenderer* WebRtcVideoChannel2::WebRtcVideoReceiveStream::GetRenderer() { |
| 1781 | // TODO(pbos): Remove GetRenderer and all uses of it, it's thread-unsafe by |
| 1782 | // design. |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1783 | rtc::CritScope crit(&renderer_lock_); |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 1784 | return renderer_; |
| 1785 | } |
| 1786 | |
| 1787 | void WebRtcVideoChannel2::WebRtcVideoReceiveStream::SetSize(int width, |
| 1788 | int height) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1789 | rtc::CritScope crit(&renderer_lock_); |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 1790 | if (!renderer_->SetSize(width, height, 0)) { |
| 1791 | LOG(LS_ERROR) << "Could not set renderer size."; |
| 1792 | } |
| 1793 | last_width_ = width; |
| 1794 | last_height_ = height; |
| 1795 | } |
| 1796 | |
pbos@webrtc.org | e6f84ae | 2014-07-18 11:11:55 +0000 | [diff] [blame] | 1797 | VideoReceiverInfo |
| 1798 | WebRtcVideoChannel2::WebRtcVideoReceiveStream::GetVideoReceiverInfo() { |
| 1799 | VideoReceiverInfo info; |
| 1800 | info.add_ssrc(config_.rtp.remote_ssrc); |
| 1801 | webrtc::VideoReceiveStream::Stats stats = stream_->GetStats(); |
| 1802 | info.bytes_rcvd = stats.rtp_stats.bytes + stats.rtp_stats.header_bytes + |
| 1803 | stats.rtp_stats.padding_bytes; |
| 1804 | info.packets_rcvd = stats.rtp_stats.packets; |
| 1805 | |
| 1806 | info.framerate_rcvd = stats.network_frame_rate; |
| 1807 | info.framerate_decoded = stats.decode_frame_rate; |
| 1808 | info.framerate_output = stats.render_frame_rate; |
| 1809 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1810 | rtc::CritScope frame_cs(&renderer_lock_); |
pbos@webrtc.org | e6f84ae | 2014-07-18 11:11:55 +0000 | [diff] [blame] | 1811 | info.frame_width = last_width_; |
| 1812 | info.frame_height = last_height_; |
| 1813 | |
| 1814 | // TODO(pbos): Support or remove the following stats. |
| 1815 | info.packets_concealed = -1; |
| 1816 | |
| 1817 | return info; |
| 1818 | } |
| 1819 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1820 | WebRtcVideoChannel2::VideoCodecSettings::VideoCodecSettings() |
| 1821 | : rtx_payload_type(-1) {} |
| 1822 | |
| 1823 | std::vector<WebRtcVideoChannel2::VideoCodecSettings> |
| 1824 | WebRtcVideoChannel2::MapCodecs(const std::vector<VideoCodec>& codecs) { |
| 1825 | assert(!codecs.empty()); |
| 1826 | |
| 1827 | std::vector<VideoCodecSettings> video_codecs; |
| 1828 | std::map<int, bool> payload_used; |
pbos@webrtc.org | e322a17 | 2014-06-13 11:47:28 +0000 | [diff] [blame] | 1829 | std::map<int, VideoCodec::CodecType> payload_codec_type; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1830 | std::map<int, int> rtx_mapping; // video payload type -> rtx payload type. |
| 1831 | |
| 1832 | webrtc::FecConfig fec_settings; |
| 1833 | |
| 1834 | for (size_t i = 0; i < codecs.size(); ++i) { |
| 1835 | const VideoCodec& in_codec = codecs[i]; |
| 1836 | int payload_type = in_codec.id; |
| 1837 | |
| 1838 | if (payload_used[payload_type]) { |
| 1839 | LOG(LS_ERROR) << "Payload type already registered: " |
| 1840 | << in_codec.ToString(); |
| 1841 | return std::vector<VideoCodecSettings>(); |
| 1842 | } |
| 1843 | payload_used[payload_type] = true; |
pbos@webrtc.org | e322a17 | 2014-06-13 11:47:28 +0000 | [diff] [blame] | 1844 | payload_codec_type[payload_type] = in_codec.GetCodecType(); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1845 | |
| 1846 | switch (in_codec.GetCodecType()) { |
| 1847 | case VideoCodec::CODEC_RED: { |
| 1848 | // RED payload type, should not have duplicates. |
| 1849 | assert(fec_settings.red_payload_type == -1); |
| 1850 | fec_settings.red_payload_type = in_codec.id; |
| 1851 | continue; |
| 1852 | } |
| 1853 | |
| 1854 | case VideoCodec::CODEC_ULPFEC: { |
| 1855 | // ULPFEC payload type, should not have duplicates. |
| 1856 | assert(fec_settings.ulpfec_payload_type == -1); |
| 1857 | fec_settings.ulpfec_payload_type = in_codec.id; |
| 1858 | continue; |
| 1859 | } |
| 1860 | |
| 1861 | case VideoCodec::CODEC_RTX: { |
| 1862 | int associated_payload_type; |
| 1863 | if (!in_codec.GetParam(kCodecParamAssociatedPayloadType, |
| 1864 | &associated_payload_type)) { |
| 1865 | LOG(LS_ERROR) << "RTX codec without associated payload type: " |
| 1866 | << in_codec.ToString(); |
| 1867 | return std::vector<VideoCodecSettings>(); |
| 1868 | } |
| 1869 | rtx_mapping[associated_payload_type] = in_codec.id; |
| 1870 | continue; |
| 1871 | } |
| 1872 | |
| 1873 | case VideoCodec::CODEC_VIDEO: |
| 1874 | break; |
| 1875 | } |
| 1876 | |
| 1877 | video_codecs.push_back(VideoCodecSettings()); |
| 1878 | video_codecs.back().codec = in_codec; |
| 1879 | } |
| 1880 | |
| 1881 | // One of these codecs should have been a video codec. Only having FEC |
| 1882 | // parameters into this code is a logic error. |
| 1883 | assert(!video_codecs.empty()); |
| 1884 | |
pbos@webrtc.org | e322a17 | 2014-06-13 11:47:28 +0000 | [diff] [blame] | 1885 | for (std::map<int, int>::const_iterator it = rtx_mapping.begin(); |
| 1886 | it != rtx_mapping.end(); |
| 1887 | ++it) { |
| 1888 | if (!payload_used[it->first]) { |
| 1889 | LOG(LS_ERROR) << "RTX mapped to payload not in codec list."; |
| 1890 | return std::vector<VideoCodecSettings>(); |
| 1891 | } |
| 1892 | if (payload_codec_type[it->first] != VideoCodec::CODEC_VIDEO) { |
| 1893 | LOG(LS_ERROR) << "RTX not mapped to regular video codec."; |
| 1894 | return std::vector<VideoCodecSettings>(); |
| 1895 | } |
| 1896 | } |
| 1897 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1898 | // TODO(pbos): Write tests that figure out that I have not verified that RTX |
| 1899 | // codecs aren't mapped to bogus payloads. |
| 1900 | for (size_t i = 0; i < video_codecs.size(); ++i) { |
| 1901 | video_codecs[i].fec = fec_settings; |
| 1902 | if (rtx_mapping[video_codecs[i].codec.id] != 0) { |
| 1903 | video_codecs[i].rtx_payload_type = rtx_mapping[video_codecs[i].codec.id]; |
| 1904 | } |
| 1905 | } |
| 1906 | |
| 1907 | return video_codecs; |
| 1908 | } |
| 1909 | |
| 1910 | std::vector<WebRtcVideoChannel2::VideoCodecSettings> |
| 1911 | WebRtcVideoChannel2::FilterSupportedCodecs( |
| 1912 | const std::vector<WebRtcVideoChannel2::VideoCodecSettings>& mapped_codecs) { |
| 1913 | std::vector<VideoCodecSettings> supported_codecs; |
| 1914 | for (size_t i = 0; i < mapped_codecs.size(); ++i) { |
| 1915 | if (encoder_factory_->SupportsCodec(mapped_codecs[i].codec)) { |
| 1916 | supported_codecs.push_back(mapped_codecs[i]); |
| 1917 | } |
| 1918 | } |
| 1919 | return supported_codecs; |
| 1920 | } |
| 1921 | |
| 1922 | } // namespace cricket |
| 1923 | |
| 1924 | #endif // HAVE_WEBRTC_VIDEO |