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