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