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 | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 668 | } |
| 669 | |
| 670 | WebRtcVideoChannel2::~WebRtcVideoChannel2() { |
| 671 | for (std::map<uint32, WebRtcVideoSendStream*>::iterator it = |
| 672 | send_streams_.begin(); |
| 673 | it != send_streams_.end(); |
| 674 | ++it) { |
| 675 | delete it->second; |
| 676 | } |
| 677 | |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 678 | for (std::map<uint32, WebRtcVideoReceiveStream*>::iterator it = |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 679 | receive_streams_.begin(); |
| 680 | it != receive_streams_.end(); |
| 681 | ++it) { |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 682 | delete it->second; |
| 683 | } |
| 684 | } |
| 685 | |
| 686 | bool WebRtcVideoChannel2::Init() { return true; } |
| 687 | |
| 688 | namespace { |
| 689 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 690 | static std::string CodecVectorToString(const std::vector<VideoCodec>& codecs) { |
| 691 | std::stringstream out; |
| 692 | out << '{'; |
| 693 | for (size_t i = 0; i < codecs.size(); ++i) { |
| 694 | out << codecs[i].ToString(); |
| 695 | if (i != codecs.size() - 1) { |
| 696 | out << ", "; |
| 697 | } |
| 698 | } |
| 699 | out << '}'; |
| 700 | return out.str(); |
| 701 | } |
| 702 | |
pbos@webrtc.org | e322a17 | 2014-06-13 11:47:28 +0000 | [diff] [blame] | 703 | static bool ValidateCodecFormats(const std::vector<VideoCodec>& codecs) { |
| 704 | bool has_video = false; |
| 705 | for (size_t i = 0; i < codecs.size(); ++i) { |
| 706 | if (!codecs[i].ValidateCodecFormat()) { |
| 707 | return false; |
| 708 | } |
| 709 | if (codecs[i].GetCodecType() == VideoCodec::CODEC_VIDEO) { |
| 710 | has_video = true; |
| 711 | } |
| 712 | } |
| 713 | if (!has_video) { |
| 714 | LOG(LS_ERROR) << "Setting codecs without a video codec is invalid: " |
| 715 | << CodecVectorToString(codecs); |
| 716 | return false; |
| 717 | } |
| 718 | return true; |
| 719 | } |
| 720 | |
pbos@webrtc.org | 587ef60 | 2014-06-16 17:32:02 +0000 | [diff] [blame] | 721 | static std::string RtpExtensionsToString( |
| 722 | const std::vector<RtpHeaderExtension>& extensions) { |
| 723 | std::stringstream out; |
| 724 | out << '{'; |
| 725 | for (size_t i = 0; i < extensions.size(); ++i) { |
| 726 | out << "{" << extensions[i].uri << ": " << extensions[i].id << "}"; |
| 727 | if (i != extensions.size() - 1) { |
| 728 | out << ", "; |
| 729 | } |
| 730 | } |
| 731 | out << '}'; |
| 732 | return out.str(); |
| 733 | } |
| 734 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 735 | } // namespace |
| 736 | |
| 737 | bool WebRtcVideoChannel2::SetRecvCodecs(const std::vector<VideoCodec>& codecs) { |
| 738 | // TODO(pbos): Must these receive codecs propagate to existing receive |
| 739 | // streams? |
| 740 | LOG(LS_INFO) << "SetRecvCodecs: " << CodecVectorToString(codecs); |
| 741 | if (!ValidateCodecFormats(codecs)) { |
| 742 | return false; |
| 743 | } |
| 744 | |
| 745 | const std::vector<VideoCodecSettings> mapped_codecs = MapCodecs(codecs); |
| 746 | if (mapped_codecs.empty()) { |
| 747 | LOG(LS_ERROR) << "SetRecvCodecs called without video codec payloads."; |
| 748 | return false; |
| 749 | } |
| 750 | |
| 751 | // TODO(pbos): Add a decoder factory which controls supported codecs. |
| 752 | // Blocked on webrtc:2854. |
| 753 | for (size_t i = 0; i < mapped_codecs.size(); ++i) { |
buildbot@webrtc.org | df9bbbe | 2014-06-19 19:54:33 +0000 | [diff] [blame] | 754 | if (_stricmp(mapped_codecs[i].codec.name.c_str(), kVp8CodecName) != 0) { |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 755 | LOG(LS_ERROR) << "SetRecvCodecs called with unsupported codec: '" |
| 756 | << mapped_codecs[i].codec.name << "'"; |
| 757 | return false; |
| 758 | } |
| 759 | } |
| 760 | |
| 761 | recv_codecs_ = mapped_codecs; |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 762 | |
| 763 | for (std::map<uint32, WebRtcVideoReceiveStream*>::iterator it = |
| 764 | receive_streams_.begin(); |
| 765 | it != receive_streams_.end(); |
| 766 | ++it) { |
| 767 | it->second->SetRecvCodecs(recv_codecs_); |
| 768 | } |
| 769 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 770 | return true; |
| 771 | } |
| 772 | |
| 773 | bool WebRtcVideoChannel2::SetSendCodecs(const std::vector<VideoCodec>& codecs) { |
| 774 | LOG(LS_INFO) << "SetSendCodecs: " << CodecVectorToString(codecs); |
| 775 | if (!ValidateCodecFormats(codecs)) { |
| 776 | return false; |
| 777 | } |
| 778 | |
| 779 | const std::vector<VideoCodecSettings> supported_codecs = |
| 780 | FilterSupportedCodecs(MapCodecs(codecs)); |
| 781 | |
| 782 | if (supported_codecs.empty()) { |
| 783 | LOG(LS_ERROR) << "No video codecs supported by encoder factory."; |
| 784 | return false; |
| 785 | } |
| 786 | |
| 787 | send_codec_.Set(supported_codecs.front()); |
| 788 | LOG(LS_INFO) << "Using codec: " << supported_codecs.front().codec.ToString(); |
| 789 | |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 790 | for (std::map<uint32, WebRtcVideoSendStream*>::iterator it = |
| 791 | send_streams_.begin(); |
| 792 | it != send_streams_.end(); |
| 793 | ++it) { |
| 794 | assert(it->second != NULL); |
| 795 | it->second->SetCodec(supported_codecs.front()); |
| 796 | } |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 797 | |
| 798 | return true; |
| 799 | } |
| 800 | |
| 801 | bool WebRtcVideoChannel2::GetSendCodec(VideoCodec* codec) { |
| 802 | VideoCodecSettings codec_settings; |
| 803 | if (!send_codec_.Get(&codec_settings)) { |
| 804 | LOG(LS_VERBOSE) << "GetSendCodec: No send codec set."; |
| 805 | return false; |
| 806 | } |
| 807 | *codec = codec_settings.codec; |
| 808 | return true; |
| 809 | } |
| 810 | |
| 811 | bool WebRtcVideoChannel2::SetSendStreamFormat(uint32 ssrc, |
| 812 | const VideoFormat& format) { |
| 813 | LOG(LS_VERBOSE) << "SetSendStreamFormat:" << ssrc << " -> " |
| 814 | << format.ToString(); |
| 815 | if (send_streams_.find(ssrc) == send_streams_.end()) { |
| 816 | return false; |
| 817 | } |
| 818 | return send_streams_[ssrc]->SetVideoFormat(format); |
| 819 | } |
| 820 | |
| 821 | bool WebRtcVideoChannel2::SetRender(bool render) { |
| 822 | // TODO(pbos): Implement. Or refactor away as it shouldn't be needed. |
| 823 | LOG(LS_VERBOSE) << "SetRender: " << (render ? "true" : "false"); |
| 824 | return true; |
| 825 | } |
| 826 | |
| 827 | bool WebRtcVideoChannel2::SetSend(bool send) { |
| 828 | LOG(LS_VERBOSE) << "SetSend: " << (send ? "true" : "false"); |
| 829 | if (send && !send_codec_.IsSet()) { |
| 830 | LOG(LS_ERROR) << "SetSend(true) called before setting codec."; |
| 831 | return false; |
| 832 | } |
| 833 | if (send) { |
| 834 | StartAllSendStreams(); |
| 835 | } else { |
| 836 | StopAllSendStreams(); |
| 837 | } |
| 838 | sending_ = send; |
| 839 | return true; |
| 840 | } |
| 841 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 842 | bool WebRtcVideoChannel2::AddSendStream(const StreamParams& sp) { |
| 843 | LOG(LS_INFO) << "AddSendStream: " << sp.ToString(); |
| 844 | if (sp.ssrcs.empty()) { |
| 845 | LOG(LS_ERROR) << "No SSRCs in stream parameters."; |
| 846 | return false; |
| 847 | } |
| 848 | |
| 849 | uint32 ssrc = sp.first_ssrc(); |
| 850 | assert(ssrc != 0); |
| 851 | // TODO(pbos): Make sure none of sp.ssrcs are used, not just the identifying |
| 852 | // ssrc. |
| 853 | if (send_streams_.find(ssrc) != send_streams_.end()) { |
| 854 | LOG(LS_ERROR) << "Send stream with ssrc '" << ssrc << "' already exists."; |
| 855 | return false; |
| 856 | } |
| 857 | |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 858 | std::vector<uint32> primary_ssrcs; |
| 859 | sp.GetPrimarySsrcs(&primary_ssrcs); |
| 860 | std::vector<uint32> rtx_ssrcs; |
| 861 | sp.GetFidSsrcs(primary_ssrcs, &rtx_ssrcs); |
| 862 | if (!rtx_ssrcs.empty() && primary_ssrcs.size() != rtx_ssrcs.size()) { |
| 863 | LOG(LS_ERROR) |
| 864 | << "RTX SSRCs exist, but don't cover all SSRCs (unsupported): " |
| 865 | << sp.ToString(); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 866 | return false; |
| 867 | } |
| 868 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 869 | WebRtcVideoSendStream* stream = |
pbos@webrtc.org | 6ae48c6 | 2014-06-06 10:49:19 +0000 | [diff] [blame] | 870 | new WebRtcVideoSendStream(call_.get(), |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 871 | encoder_factory_, |
pbos@webrtc.org | 6ae48c6 | 2014-06-06 10:49:19 +0000 | [diff] [blame] | 872 | options_, |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 873 | send_codec_, |
| 874 | sp, |
| 875 | send_rtp_extensions_); |
| 876 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 877 | send_streams_[ssrc] = stream; |
| 878 | |
| 879 | if (rtcp_receiver_report_ssrc_ == kDefaultRtcpReceiverReportSsrc) { |
| 880 | rtcp_receiver_report_ssrc_ = ssrc; |
| 881 | } |
| 882 | if (default_send_ssrc_ == 0) { |
| 883 | default_send_ssrc_ = ssrc; |
| 884 | } |
| 885 | if (sending_) { |
| 886 | stream->Start(); |
| 887 | } |
| 888 | |
| 889 | return true; |
| 890 | } |
| 891 | |
| 892 | bool WebRtcVideoChannel2::RemoveSendStream(uint32 ssrc) { |
| 893 | LOG(LS_INFO) << "RemoveSendStream: " << ssrc; |
| 894 | |
| 895 | if (ssrc == 0) { |
| 896 | if (default_send_ssrc_ == 0) { |
| 897 | LOG(LS_ERROR) << "No default send stream active."; |
| 898 | return false; |
| 899 | } |
| 900 | |
| 901 | LOG(LS_VERBOSE) << "Removing default stream: " << default_send_ssrc_; |
| 902 | ssrc = default_send_ssrc_; |
| 903 | } |
| 904 | |
| 905 | std::map<uint32, WebRtcVideoSendStream*>::iterator it = |
| 906 | send_streams_.find(ssrc); |
| 907 | if (it == send_streams_.end()) { |
| 908 | return false; |
| 909 | } |
| 910 | |
| 911 | delete it->second; |
| 912 | send_streams_.erase(it); |
| 913 | |
| 914 | if (ssrc == default_send_ssrc_) { |
| 915 | default_send_ssrc_ = 0; |
| 916 | } |
| 917 | |
| 918 | return true; |
| 919 | } |
| 920 | |
| 921 | bool WebRtcVideoChannel2::AddRecvStream(const StreamParams& sp) { |
| 922 | LOG(LS_INFO) << "AddRecvStream: " << sp.ToString(); |
| 923 | assert(sp.ssrcs.size() > 0); |
| 924 | |
| 925 | uint32 ssrc = sp.first_ssrc(); |
| 926 | assert(ssrc != 0); // TODO(pbos): Is this ever valid? |
| 927 | if (default_recv_ssrc_ == 0) { |
| 928 | default_recv_ssrc_ = ssrc; |
| 929 | } |
| 930 | |
| 931 | // TODO(pbos): Check if any of the SSRCs overlap. |
| 932 | if (receive_streams_.find(ssrc) != receive_streams_.end()) { |
| 933 | LOG(LS_ERROR) << "Receive stream for SSRC " << ssrc << "already exists."; |
| 934 | return false; |
| 935 | } |
| 936 | |
pbos@webrtc.org | bd249bc | 2014-07-07 04:45:15 +0000 | [diff] [blame] | 937 | webrtc::VideoReceiveStream::Config config; |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 938 | ConfigureReceiverRtp(&config, sp); |
| 939 | receive_streams_[ssrc] = |
| 940 | new WebRtcVideoReceiveStream(call_.get(), config, recv_codecs_); |
| 941 | |
| 942 | return true; |
| 943 | } |
| 944 | |
| 945 | void WebRtcVideoChannel2::ConfigureReceiverRtp( |
| 946 | webrtc::VideoReceiveStream::Config* config, |
| 947 | const StreamParams& sp) const { |
| 948 | uint32 ssrc = sp.first_ssrc(); |
| 949 | |
| 950 | config->rtp.remote_ssrc = ssrc; |
| 951 | config->rtp.local_ssrc = rtcp_receiver_report_ssrc_; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 952 | |
pbos@webrtc.org | f99c2f2 | 2014-06-13 12:27:38 +0000 | [diff] [blame] | 953 | if (IsNackEnabled(recv_codecs_.begin()->codec)) { |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 954 | config->rtp.nack.rtp_history_ms = kNackHistoryMs; |
pbos@webrtc.org | f99c2f2 | 2014-06-13 12:27:38 +0000 | [diff] [blame] | 955 | } |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 956 | config->rtp.remb = true; |
| 957 | config->rtp.extensions = recv_rtp_extensions_; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 958 | // TODO(pbos): This protection is against setting the same local ssrc as |
| 959 | // remote which is not permitted by the lower-level API. RTCP requires a |
| 960 | // corresponding sender SSRC. Figure out what to do when we don't have |
| 961 | // (receive-only) or know a good local SSRC. |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 962 | if (config->rtp.remote_ssrc == config->rtp.local_ssrc) { |
| 963 | if (config->rtp.local_ssrc != kDefaultRtcpReceiverReportSsrc) { |
| 964 | config->rtp.local_ssrc = kDefaultRtcpReceiverReportSsrc; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 965 | } else { |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 966 | config->rtp.local_ssrc = kDefaultRtcpReceiverReportSsrc + 1; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 967 | } |
| 968 | } |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 969 | |
| 970 | for (size_t i = 0; i < recv_codecs_.size(); ++i) { |
| 971 | if (recv_codecs_[i].codec.id == kDefaultVideoCodecPref.payload_type) { |
| 972 | config->rtp.fec = recv_codecs_[i].fec; |
| 973 | uint32 rtx_ssrc; |
| 974 | if (recv_codecs_[i].rtx_payload_type != -1 && |
| 975 | sp.GetFidSsrc(ssrc, &rtx_ssrc)) { |
| 976 | config->rtp.rtx[kDefaultVideoCodecPref.payload_type].ssrc = rtx_ssrc; |
| 977 | config->rtp.rtx[kDefaultVideoCodecPref.payload_type].payload_type = |
| 978 | recv_codecs_[i].rtx_payload_type; |
| 979 | } |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 980 | break; |
| 981 | } |
| 982 | } |
| 983 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 984 | } |
| 985 | |
| 986 | bool WebRtcVideoChannel2::RemoveRecvStream(uint32 ssrc) { |
| 987 | LOG(LS_INFO) << "RemoveRecvStream: " << ssrc; |
| 988 | if (ssrc == 0) { |
| 989 | ssrc = default_recv_ssrc_; |
| 990 | } |
| 991 | |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 992 | std::map<uint32, WebRtcVideoReceiveStream*>::iterator stream = |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 993 | receive_streams_.find(ssrc); |
| 994 | if (stream == receive_streams_.end()) { |
| 995 | LOG(LS_ERROR) << "Stream not found for ssrc: " << ssrc; |
| 996 | return false; |
| 997 | } |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 998 | delete stream->second; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 999 | receive_streams_.erase(stream); |
| 1000 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1001 | if (ssrc == default_recv_ssrc_) { |
| 1002 | default_recv_ssrc_ = 0; |
| 1003 | } |
| 1004 | |
| 1005 | return true; |
| 1006 | } |
| 1007 | |
| 1008 | bool WebRtcVideoChannel2::SetRenderer(uint32 ssrc, VideoRenderer* renderer) { |
| 1009 | LOG(LS_INFO) << "SetRenderer: ssrc:" << ssrc << " " |
| 1010 | << (renderer ? "(ptr)" : "NULL"); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1011 | if (ssrc == 0) { |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 1012 | if (default_recv_ssrc_!= 0) { |
| 1013 | receive_streams_[default_recv_ssrc_]->SetRenderer(renderer); |
| 1014 | } |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1015 | ssrc = default_recv_ssrc_; |
| 1016 | default_renderer_ = renderer; |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 1017 | return true; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1018 | } |
| 1019 | |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 1020 | std::map<uint32, WebRtcVideoReceiveStream*>::iterator it = |
| 1021 | receive_streams_.find(ssrc); |
| 1022 | if (it == receive_streams_.end()) { |
| 1023 | return false; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1024 | } |
| 1025 | |
| 1026 | it->second->SetRenderer(renderer); |
| 1027 | return true; |
| 1028 | } |
| 1029 | |
| 1030 | bool WebRtcVideoChannel2::GetRenderer(uint32 ssrc, VideoRenderer** renderer) { |
| 1031 | if (ssrc == 0) { |
| 1032 | if (default_renderer_ == NULL) { |
| 1033 | return false; |
| 1034 | } |
| 1035 | *renderer = default_renderer_; |
| 1036 | return true; |
| 1037 | } |
| 1038 | |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 1039 | std::map<uint32, WebRtcVideoReceiveStream*>::iterator it = |
| 1040 | receive_streams_.find(ssrc); |
| 1041 | if (it == receive_streams_.end()) { |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1042 | return false; |
| 1043 | } |
| 1044 | *renderer = it->second->GetRenderer(); |
| 1045 | return true; |
| 1046 | } |
| 1047 | |
| 1048 | bool WebRtcVideoChannel2::GetStats(const StatsOptions& options, |
| 1049 | VideoMediaInfo* info) { |
pbos@webrtc.org | e6f84ae | 2014-07-18 11:11:55 +0000 | [diff] [blame] | 1050 | info->Clear(); |
| 1051 | FillSenderStats(info); |
| 1052 | FillReceiverStats(info); |
| 1053 | FillBandwidthEstimationStats(info); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1054 | return true; |
| 1055 | } |
| 1056 | |
pbos@webrtc.org | e6f84ae | 2014-07-18 11:11:55 +0000 | [diff] [blame] | 1057 | void WebRtcVideoChannel2::FillSenderStats(VideoMediaInfo* video_media_info) { |
| 1058 | for (std::map<uint32, WebRtcVideoSendStream*>::iterator it = |
| 1059 | send_streams_.begin(); |
| 1060 | it != send_streams_.end(); |
| 1061 | ++it) { |
| 1062 | video_media_info->senders.push_back(it->second->GetVideoSenderInfo()); |
| 1063 | } |
| 1064 | } |
| 1065 | |
| 1066 | void WebRtcVideoChannel2::FillReceiverStats(VideoMediaInfo* video_media_info) { |
| 1067 | for (std::map<uint32, WebRtcVideoReceiveStream*>::iterator it = |
| 1068 | receive_streams_.begin(); |
| 1069 | it != receive_streams_.end(); |
| 1070 | ++it) { |
| 1071 | video_media_info->receivers.push_back(it->second->GetVideoReceiverInfo()); |
| 1072 | } |
| 1073 | } |
| 1074 | |
| 1075 | void WebRtcVideoChannel2::FillBandwidthEstimationStats( |
| 1076 | VideoMediaInfo* video_media_info) { |
| 1077 | // TODO(pbos): Implement. |
| 1078 | } |
| 1079 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1080 | bool WebRtcVideoChannel2::SetCapturer(uint32 ssrc, VideoCapturer* capturer) { |
| 1081 | LOG(LS_INFO) << "SetCapturer: " << ssrc << " -> " |
| 1082 | << (capturer != NULL ? "(capturer)" : "NULL"); |
| 1083 | assert(ssrc != 0); |
| 1084 | if (send_streams_.find(ssrc) == send_streams_.end()) { |
| 1085 | LOG(LS_ERROR) << "No sending stream on ssrc " << ssrc; |
| 1086 | return false; |
| 1087 | } |
| 1088 | return send_streams_[ssrc]->SetCapturer(capturer); |
| 1089 | } |
| 1090 | |
| 1091 | bool WebRtcVideoChannel2::SendIntraFrame() { |
| 1092 | // TODO(pbos): Implement. |
| 1093 | LOG(LS_VERBOSE) << "SendIntraFrame()."; |
| 1094 | return true; |
| 1095 | } |
| 1096 | |
| 1097 | bool WebRtcVideoChannel2::RequestIntraFrame() { |
| 1098 | // TODO(pbos): Implement. |
| 1099 | LOG(LS_VERBOSE) << "SendIntraFrame()."; |
| 1100 | return true; |
| 1101 | } |
| 1102 | |
| 1103 | void WebRtcVideoChannel2::OnPacketReceived( |
| 1104 | talk_base::Buffer* packet, |
| 1105 | const talk_base::PacketTime& packet_time) { |
pbos@webrtc.org | 4e545cc | 2014-05-14 13:58:13 +0000 | [diff] [blame] | 1106 | const webrtc::PacketReceiver::DeliveryStatus delivery_result = |
| 1107 | call_->Receiver()->DeliverPacket( |
| 1108 | reinterpret_cast<const uint8_t*>(packet->data()), packet->length()); |
| 1109 | switch (delivery_result) { |
| 1110 | case webrtc::PacketReceiver::DELIVERY_OK: |
| 1111 | return; |
| 1112 | case webrtc::PacketReceiver::DELIVERY_PACKET_ERROR: |
| 1113 | return; |
| 1114 | case webrtc::PacketReceiver::DELIVERY_UNKNOWN_SSRC: |
| 1115 | break; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1116 | } |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1117 | |
| 1118 | uint32 ssrc = 0; |
| 1119 | if (default_recv_ssrc_ != 0) { // Already one default stream. |
pbos@webrtc.org | 4e545cc | 2014-05-14 13:58:13 +0000 | [diff] [blame] | 1120 | LOG(LS_WARNING) << "Unknown SSRC, but default receive stream already set."; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1121 | return; |
| 1122 | } |
| 1123 | |
| 1124 | if (!GetRtpSsrc(packet->data(), packet->length(), &ssrc)) { |
| 1125 | return; |
| 1126 | } |
| 1127 | |
| 1128 | StreamParams sp; |
| 1129 | sp.ssrcs.push_back(ssrc); |
pbos@webrtc.org | c34bb3a | 2014-05-30 07:38:43 +0000 | [diff] [blame] | 1130 | LOG(LS_INFO) << "Creating default receive stream for SSRC=" << ssrc << "."; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1131 | AddRecvStream(sp); |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 1132 | SetRenderer(0, default_renderer_); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1133 | |
pbos@webrtc.org | 1e019d1 | 2014-05-16 11:38:45 +0000 | [diff] [blame] | 1134 | if (call_->Receiver()->DeliverPacket( |
| 1135 | reinterpret_cast<const uint8_t*>(packet->data()), packet->length()) != |
| 1136 | webrtc::PacketReceiver::DELIVERY_OK) { |
| 1137 | LOG(LS_WARNING) << "Failed to deliver RTP packet after creating default " |
| 1138 | "receiver."; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1139 | return; |
| 1140 | } |
| 1141 | } |
| 1142 | |
| 1143 | void WebRtcVideoChannel2::OnRtcpReceived( |
| 1144 | talk_base::Buffer* packet, |
| 1145 | const talk_base::PacketTime& packet_time) { |
pbos@webrtc.org | 1e019d1 | 2014-05-16 11:38:45 +0000 | [diff] [blame] | 1146 | if (call_->Receiver()->DeliverPacket( |
| 1147 | reinterpret_cast<const uint8_t*>(packet->data()), packet->length()) != |
| 1148 | webrtc::PacketReceiver::DELIVERY_OK) { |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1149 | LOG(LS_WARNING) << "Failed to deliver RTCP packet."; |
| 1150 | } |
| 1151 | } |
| 1152 | |
| 1153 | void WebRtcVideoChannel2::OnReadyToSend(bool ready) { |
| 1154 | LOG(LS_VERBOSE) << "OnReadySend: " << (ready ? "Ready." : "Not ready."); |
| 1155 | } |
| 1156 | |
| 1157 | bool WebRtcVideoChannel2::MuteStream(uint32 ssrc, bool mute) { |
| 1158 | LOG(LS_VERBOSE) << "MuteStream: " << ssrc << " -> " |
| 1159 | << (mute ? "mute" : "unmute"); |
| 1160 | assert(ssrc != 0); |
| 1161 | if (send_streams_.find(ssrc) == send_streams_.end()) { |
| 1162 | LOG(LS_ERROR) << "No sending stream on ssrc " << ssrc; |
| 1163 | return false; |
| 1164 | } |
| 1165 | return send_streams_[ssrc]->MuteStream(mute); |
| 1166 | } |
| 1167 | |
| 1168 | bool WebRtcVideoChannel2::SetRecvRtpHeaderExtensions( |
| 1169 | const std::vector<RtpHeaderExtension>& extensions) { |
pbos@webrtc.org | 587ef60 | 2014-06-16 17:32:02 +0000 | [diff] [blame] | 1170 | LOG(LS_INFO) << "SetRecvRtpHeaderExtensions: " |
| 1171 | << RtpExtensionsToString(extensions); |
pbos@webrtc.org | 3c10758 | 2014-07-20 15:27:35 +0000 | [diff] [blame] | 1172 | if (!ValidateRtpHeaderExtensionIds(extensions)) |
| 1173 | return false; |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 1174 | |
pbos@webrtc.org | 3c10758 | 2014-07-20 15:27:35 +0000 | [diff] [blame] | 1175 | recv_rtp_extensions_ = FilterRtpExtensions(extensions); |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 1176 | for (std::map<uint32, WebRtcVideoReceiveStream*>::iterator it = |
| 1177 | receive_streams_.begin(); |
| 1178 | it != receive_streams_.end(); |
| 1179 | ++it) { |
| 1180 | it->second->SetRtpExtensions(recv_rtp_extensions_); |
| 1181 | } |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1182 | return true; |
| 1183 | } |
| 1184 | |
| 1185 | bool WebRtcVideoChannel2::SetSendRtpHeaderExtensions( |
| 1186 | const std::vector<RtpHeaderExtension>& extensions) { |
pbos@webrtc.org | 587ef60 | 2014-06-16 17:32:02 +0000 | [diff] [blame] | 1187 | LOG(LS_INFO) << "SetSendRtpHeaderExtensions: " |
| 1188 | << RtpExtensionsToString(extensions); |
pbos@webrtc.org | 3c10758 | 2014-07-20 15:27:35 +0000 | [diff] [blame] | 1189 | if (!ValidateRtpHeaderExtensionIds(extensions)) |
| 1190 | return false; |
| 1191 | |
| 1192 | send_rtp_extensions_ = FilterRtpExtensions(extensions); |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 1193 | for (std::map<uint32, WebRtcVideoSendStream*>::iterator it = |
| 1194 | send_streams_.begin(); |
| 1195 | it != send_streams_.end(); |
| 1196 | ++it) { |
| 1197 | it->second->SetRtpExtensions(send_rtp_extensions_); |
| 1198 | } |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1199 | return true; |
| 1200 | } |
| 1201 | |
| 1202 | bool WebRtcVideoChannel2::SetStartSendBandwidth(int bps) { |
| 1203 | // TODO(pbos): Implement. |
| 1204 | LOG(LS_VERBOSE) << "SetStartSendBandwidth: " << bps; |
| 1205 | return true; |
| 1206 | } |
| 1207 | |
| 1208 | bool WebRtcVideoChannel2::SetMaxSendBandwidth(int bps) { |
| 1209 | // TODO(pbos): Implement. |
| 1210 | LOG(LS_VERBOSE) << "SetMaxSendBandwidth: " << bps; |
| 1211 | return true; |
| 1212 | } |
| 1213 | |
| 1214 | bool WebRtcVideoChannel2::SetOptions(const VideoOptions& options) { |
| 1215 | LOG(LS_VERBOSE) << "SetOptions: " << options.ToString(); |
| 1216 | options_.SetAll(options); |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1217 | for (std::map<uint32, WebRtcVideoSendStream*>::iterator it = |
| 1218 | send_streams_.begin(); |
| 1219 | it != send_streams_.end(); |
| 1220 | ++it) { |
| 1221 | it->second->SetOptions(options_); |
| 1222 | } |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1223 | return true; |
| 1224 | } |
| 1225 | |
| 1226 | void WebRtcVideoChannel2::SetInterface(NetworkInterface* iface) { |
| 1227 | MediaChannel::SetInterface(iface); |
| 1228 | // Set the RTP recv/send buffer to a bigger size |
| 1229 | MediaChannel::SetOption(NetworkInterface::ST_RTP, |
| 1230 | talk_base::Socket::OPT_RCVBUF, |
| 1231 | kVideoRtpBufferSize); |
| 1232 | |
| 1233 | // TODO(sriniv): Remove or re-enable this. |
| 1234 | // As part of b/8030474, send-buffer is size now controlled through |
| 1235 | // portallocator flags. |
| 1236 | // network_interface_->SetOption(NetworkInterface::ST_RTP, |
| 1237 | // talk_base::Socket::OPT_SNDBUF, |
| 1238 | // kVideoRtpBufferSize); |
| 1239 | } |
| 1240 | |
| 1241 | void WebRtcVideoChannel2::UpdateAspectRatio(int ratio_w, int ratio_h) { |
| 1242 | // TODO(pbos): Implement. |
| 1243 | } |
| 1244 | |
| 1245 | void WebRtcVideoChannel2::OnMessage(talk_base::Message* msg) { |
| 1246 | // Ignored. |
| 1247 | } |
| 1248 | |
| 1249 | bool WebRtcVideoChannel2::SendRtp(const uint8_t* data, size_t len) { |
| 1250 | talk_base::Buffer packet(data, len, kMaxRtpPacketLen); |
| 1251 | return MediaChannel::SendPacket(&packet); |
| 1252 | } |
| 1253 | |
| 1254 | bool WebRtcVideoChannel2::SendRtcp(const uint8_t* data, size_t len) { |
| 1255 | talk_base::Buffer packet(data, len, kMaxRtpPacketLen); |
| 1256 | return MediaChannel::SendRtcp(&packet); |
| 1257 | } |
| 1258 | |
| 1259 | void WebRtcVideoChannel2::StartAllSendStreams() { |
| 1260 | for (std::map<uint32, WebRtcVideoSendStream*>::iterator it = |
| 1261 | send_streams_.begin(); |
| 1262 | it != send_streams_.end(); |
| 1263 | ++it) { |
| 1264 | it->second->Start(); |
| 1265 | } |
| 1266 | } |
| 1267 | |
| 1268 | void WebRtcVideoChannel2::StopAllSendStreams() { |
| 1269 | for (std::map<uint32, WebRtcVideoSendStream*>::iterator it = |
| 1270 | send_streams_.begin(); |
| 1271 | it != send_streams_.end(); |
| 1272 | ++it) { |
| 1273 | it->second->Stop(); |
| 1274 | } |
| 1275 | } |
| 1276 | |
pbos@webrtc.org | 6ae48c6 | 2014-06-06 10:49:19 +0000 | [diff] [blame] | 1277 | WebRtcVideoChannel2::WebRtcVideoSendStream::VideoSendStreamParameters:: |
| 1278 | VideoSendStreamParameters( |
| 1279 | const webrtc::VideoSendStream::Config& config, |
| 1280 | const VideoOptions& options, |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1281 | const Settable<VideoCodecSettings>& codec_settings) |
| 1282 | : config(config), options(options), codec_settings(codec_settings) { |
pbos@webrtc.org | 6ae48c6 | 2014-06-06 10:49:19 +0000 | [diff] [blame] | 1283 | } |
| 1284 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1285 | WebRtcVideoChannel2::WebRtcVideoSendStream::WebRtcVideoSendStream( |
| 1286 | webrtc::Call* call, |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1287 | WebRtcVideoEncoderFactory2* encoder_factory, |
pbos@webrtc.org | 6ae48c6 | 2014-06-06 10:49:19 +0000 | [diff] [blame] | 1288 | const VideoOptions& options, |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1289 | const Settable<VideoCodecSettings>& codec_settings, |
| 1290 | const StreamParams& sp, |
| 1291 | const std::vector<webrtc::RtpExtension>& rtp_extensions) |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1292 | : call_(call), |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1293 | parameters_(webrtc::VideoSendStream::Config(), options, codec_settings), |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1294 | encoder_factory_(encoder_factory), |
| 1295 | capturer_(NULL), |
| 1296 | stream_(NULL), |
| 1297 | sending_(false), |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1298 | muted_(false) { |
| 1299 | parameters_.config.rtp.max_packet_size = kVideoMtu; |
| 1300 | |
| 1301 | sp.GetPrimarySsrcs(¶meters_.config.rtp.ssrcs); |
| 1302 | sp.GetFidSsrcs(parameters_.config.rtp.ssrcs, |
| 1303 | ¶meters_.config.rtp.rtx.ssrcs); |
| 1304 | parameters_.config.rtp.c_name = sp.cname; |
| 1305 | parameters_.config.rtp.extensions = rtp_extensions; |
| 1306 | |
| 1307 | VideoCodecSettings params; |
| 1308 | if (codec_settings.Get(¶ms)) { |
| 1309 | SetCodec(params); |
| 1310 | } |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1311 | } |
| 1312 | |
| 1313 | WebRtcVideoChannel2::WebRtcVideoSendStream::~WebRtcVideoSendStream() { |
| 1314 | DisconnectCapturer(); |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1315 | if (stream_ != NULL) { |
| 1316 | call_->DestroyVideoSendStream(stream_); |
| 1317 | } |
pbos@webrtc.org | 6ae48c6 | 2014-06-06 10:49:19 +0000 | [diff] [blame] | 1318 | delete parameters_.config.encoder_settings.encoder; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1319 | } |
| 1320 | |
| 1321 | static void SetWebRtcFrameToBlack(webrtc::I420VideoFrame* video_frame) { |
| 1322 | assert(video_frame != NULL); |
| 1323 | memset(video_frame->buffer(webrtc::kYPlane), |
| 1324 | 16, |
| 1325 | video_frame->allocated_size(webrtc::kYPlane)); |
| 1326 | memset(video_frame->buffer(webrtc::kUPlane), |
| 1327 | 128, |
| 1328 | video_frame->allocated_size(webrtc::kUPlane)); |
| 1329 | memset(video_frame->buffer(webrtc::kVPlane), |
| 1330 | 128, |
| 1331 | video_frame->allocated_size(webrtc::kVPlane)); |
| 1332 | } |
| 1333 | |
| 1334 | static void CreateBlackFrame(webrtc::I420VideoFrame* video_frame, |
| 1335 | int width, |
| 1336 | int height) { |
| 1337 | video_frame->CreateEmptyFrame( |
| 1338 | width, height, width, (width + 1) / 2, (width + 1) / 2); |
| 1339 | SetWebRtcFrameToBlack(video_frame); |
| 1340 | } |
| 1341 | |
| 1342 | static void ConvertToI420VideoFrame(const VideoFrame& frame, |
| 1343 | webrtc::I420VideoFrame* i420_frame) { |
| 1344 | i420_frame->CreateFrame( |
| 1345 | static_cast<int>(frame.GetYPitch() * frame.GetHeight()), |
| 1346 | frame.GetYPlane(), |
| 1347 | static_cast<int>(frame.GetUPitch() * ((frame.GetHeight() + 1) / 2)), |
| 1348 | frame.GetUPlane(), |
| 1349 | static_cast<int>(frame.GetVPitch() * ((frame.GetHeight() + 1) / 2)), |
| 1350 | frame.GetVPlane(), |
| 1351 | static_cast<int>(frame.GetWidth()), |
| 1352 | static_cast<int>(frame.GetHeight()), |
| 1353 | static_cast<int>(frame.GetYPitch()), |
| 1354 | static_cast<int>(frame.GetUPitch()), |
| 1355 | static_cast<int>(frame.GetVPitch())); |
| 1356 | } |
| 1357 | |
| 1358 | void WebRtcVideoChannel2::WebRtcVideoSendStream::InputFrame( |
| 1359 | VideoCapturer* capturer, |
| 1360 | const VideoFrame* frame) { |
| 1361 | LOG(LS_VERBOSE) << "InputFrame: " << frame->GetWidth() << "x" |
| 1362 | << frame->GetHeight(); |
| 1363 | bool is_screencast = capturer->IsScreencast(); |
| 1364 | // Lock before copying, can be called concurrently when swapping input source. |
| 1365 | talk_base::CritScope frame_cs(&frame_lock_); |
| 1366 | if (!muted_) { |
| 1367 | ConvertToI420VideoFrame(*frame, &video_frame_); |
| 1368 | } else { |
| 1369 | // Create a tiny black frame to transmit instead. |
| 1370 | CreateBlackFrame(&video_frame_, 1, 1); |
| 1371 | is_screencast = false; |
| 1372 | } |
| 1373 | talk_base::CritScope cs(&lock_); |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1374 | if (stream_ == NULL) { |
| 1375 | LOG(LS_WARNING) << "Capturer inputting frames before send codecs are " |
| 1376 | "configured, dropping."; |
| 1377 | return; |
| 1378 | } |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1379 | if (format_.width == 0) { // Dropping frames. |
| 1380 | assert(format_.height == 0); |
| 1381 | LOG(LS_VERBOSE) << "VideoFormat 0x0 set, Dropping frame."; |
| 1382 | return; |
| 1383 | } |
| 1384 | // Reconfigure codec if necessary. |
| 1385 | if (is_screencast) { |
| 1386 | SetDimensions(video_frame_.width(), video_frame_.height()); |
| 1387 | } |
| 1388 | LOG(LS_VERBOSE) << "SwapFrame: " << video_frame_.width() << "x" |
| 1389 | << video_frame_.height() << " -> (codec) " |
pbos@webrtc.org | 6ae48c6 | 2014-06-06 10:49:19 +0000 | [diff] [blame] | 1390 | << parameters_.video_streams.back().width << "x" |
| 1391 | << parameters_.video_streams.back().height; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1392 | stream_->Input()->SwapFrame(&video_frame_); |
| 1393 | } |
| 1394 | |
| 1395 | bool WebRtcVideoChannel2::WebRtcVideoSendStream::SetCapturer( |
| 1396 | VideoCapturer* capturer) { |
| 1397 | if (!DisconnectCapturer() && capturer == NULL) { |
| 1398 | return false; |
| 1399 | } |
| 1400 | |
| 1401 | { |
| 1402 | talk_base::CritScope cs(&lock_); |
| 1403 | |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1404 | if (capturer == NULL && stream_ != NULL) { |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1405 | LOG(LS_VERBOSE) << "Disabling capturer, sending black frame."; |
| 1406 | webrtc::I420VideoFrame black_frame; |
| 1407 | |
| 1408 | int width = format_.width; |
| 1409 | int height = format_.height; |
| 1410 | int half_width = (width + 1) / 2; |
| 1411 | black_frame.CreateEmptyFrame( |
| 1412 | width, height, width, half_width, half_width); |
| 1413 | SetWebRtcFrameToBlack(&black_frame); |
| 1414 | SetDimensions(width, height); |
| 1415 | stream_->Input()->SwapFrame(&black_frame); |
| 1416 | |
| 1417 | capturer_ = NULL; |
| 1418 | return true; |
| 1419 | } |
| 1420 | |
| 1421 | capturer_ = capturer; |
| 1422 | } |
| 1423 | // Lock cannot be held while connecting the capturer to prevent lock-order |
| 1424 | // violations. |
| 1425 | capturer->SignalVideoFrame.connect(this, &WebRtcVideoSendStream::InputFrame); |
| 1426 | return true; |
| 1427 | } |
| 1428 | |
| 1429 | bool WebRtcVideoChannel2::WebRtcVideoSendStream::SetVideoFormat( |
| 1430 | const VideoFormat& format) { |
| 1431 | if ((format.width == 0 || format.height == 0) && |
| 1432 | format.width != format.height) { |
| 1433 | LOG(LS_ERROR) << "Can't set VideoFormat, width or height is zero (but not " |
| 1434 | "both, 0x0 drops frames)."; |
| 1435 | return false; |
| 1436 | } |
| 1437 | |
| 1438 | talk_base::CritScope cs(&lock_); |
| 1439 | if (format.width == 0 && format.height == 0) { |
| 1440 | LOG(LS_INFO) |
| 1441 | << "0x0 resolution selected. Captured frames will be dropped for ssrc: " |
pbos@webrtc.org | 6ae48c6 | 2014-06-06 10:49:19 +0000 | [diff] [blame] | 1442 | << parameters_.config.rtp.ssrcs[0] << "."; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1443 | } else { |
| 1444 | // TODO(pbos): Fix me, this only affects the last stream! |
pbos@webrtc.org | 6ae48c6 | 2014-06-06 10:49:19 +0000 | [diff] [blame] | 1445 | parameters_.video_streams.back().max_framerate = |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1446 | VideoFormat::IntervalToFps(format.interval); |
| 1447 | SetDimensions(format.width, format.height); |
| 1448 | } |
| 1449 | |
| 1450 | format_ = format; |
| 1451 | return true; |
| 1452 | } |
| 1453 | |
| 1454 | bool WebRtcVideoChannel2::WebRtcVideoSendStream::MuteStream(bool mute) { |
| 1455 | talk_base::CritScope cs(&lock_); |
| 1456 | bool was_muted = muted_; |
| 1457 | muted_ = mute; |
| 1458 | return was_muted != mute; |
| 1459 | } |
| 1460 | |
| 1461 | bool WebRtcVideoChannel2::WebRtcVideoSendStream::DisconnectCapturer() { |
| 1462 | talk_base::CritScope cs(&lock_); |
| 1463 | if (capturer_ == NULL) { |
| 1464 | return false; |
| 1465 | } |
| 1466 | capturer_->SignalVideoFrame.disconnect(this); |
| 1467 | capturer_ = NULL; |
| 1468 | return true; |
| 1469 | } |
| 1470 | |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1471 | void WebRtcVideoChannel2::WebRtcVideoSendStream::SetOptions( |
| 1472 | const VideoOptions& options) { |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1473 | talk_base::CritScope cs(&lock_); |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1474 | VideoCodecSettings codec_settings; |
| 1475 | if (parameters_.codec_settings.Get(&codec_settings)) { |
| 1476 | SetCodecAndOptions(codec_settings, options); |
| 1477 | } else { |
| 1478 | parameters_.options = options; |
| 1479 | } |
| 1480 | } |
| 1481 | void WebRtcVideoChannel2::WebRtcVideoSendStream::SetCodec( |
| 1482 | const VideoCodecSettings& codec_settings) { |
| 1483 | talk_base::CritScope cs(&lock_); |
| 1484 | SetCodecAndOptions(codec_settings, parameters_.options); |
| 1485 | } |
| 1486 | void WebRtcVideoChannel2::WebRtcVideoSendStream::SetCodecAndOptions( |
| 1487 | const VideoCodecSettings& codec_settings, |
| 1488 | const VideoOptions& options) { |
pbos@webrtc.org | 6ae48c6 | 2014-06-06 10:49:19 +0000 | [diff] [blame] | 1489 | std::vector<webrtc::VideoStream> video_streams = |
| 1490 | encoder_factory_->CreateVideoStreams( |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1491 | codec_settings.codec, options, parameters_.config.rtp.ssrcs.size()); |
pbos@webrtc.org | 6ae48c6 | 2014-06-06 10:49:19 +0000 | [diff] [blame] | 1492 | if (video_streams.empty()) { |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1493 | return; |
| 1494 | } |
pbos@webrtc.org | 6ae48c6 | 2014-06-06 10:49:19 +0000 | [diff] [blame] | 1495 | parameters_.video_streams = video_streams; |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1496 | format_ = VideoFormat(codec_settings.codec.width, |
| 1497 | codec_settings.codec.height, |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1498 | VideoFormat::FpsToInterval(30), |
| 1499 | FOURCC_I420); |
pbos@webrtc.org | 6ae48c6 | 2014-06-06 10:49:19 +0000 | [diff] [blame] | 1500 | |
| 1501 | webrtc::VideoEncoder* old_encoder = |
| 1502 | parameters_.config.encoder_settings.encoder; |
| 1503 | parameters_.config.encoder_settings.encoder = |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1504 | encoder_factory_->CreateVideoEncoder(codec_settings.codec, options); |
| 1505 | parameters_.config.encoder_settings.payload_name = codec_settings.codec.name; |
| 1506 | parameters_.config.encoder_settings.payload_type = codec_settings.codec.id; |
| 1507 | parameters_.config.rtp.fec = codec_settings.fec; |
| 1508 | |
| 1509 | // Set RTX payload type if RTX is enabled. |
| 1510 | if (!parameters_.config.rtp.rtx.ssrcs.empty()) { |
| 1511 | parameters_.config.rtp.rtx.payload_type = codec_settings.rtx_payload_type; |
pbos@webrtc.org | 543e589 | 2014-07-23 07:01:31 +0000 | [diff] [blame] | 1512 | |
| 1513 | options.use_payload_padding.Get( |
| 1514 | ¶meters_.config.rtp.rtx.pad_with_redundant_payloads); |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1515 | } |
| 1516 | |
| 1517 | if (IsNackEnabled(codec_settings.codec)) { |
| 1518 | parameters_.config.rtp.nack.rtp_history_ms = kNackHistoryMs; |
| 1519 | } |
| 1520 | |
| 1521 | parameters_.codec_settings.Set(codec_settings); |
pbos@webrtc.org | 6ae48c6 | 2014-06-06 10:49:19 +0000 | [diff] [blame] | 1522 | parameters_.options = options; |
pbos@webrtc.org | 543e589 | 2014-07-23 07:01:31 +0000 | [diff] [blame] | 1523 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1524 | RecreateWebRtcStream(); |
| 1525 | delete old_encoder; |
| 1526 | } |
| 1527 | |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 1528 | void WebRtcVideoChannel2::WebRtcVideoSendStream::SetRtpExtensions( |
| 1529 | const std::vector<webrtc::RtpExtension>& rtp_extensions) { |
| 1530 | talk_base::CritScope cs(&lock_); |
| 1531 | parameters_.config.rtp.extensions = rtp_extensions; |
| 1532 | RecreateWebRtcStream(); |
| 1533 | } |
| 1534 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1535 | void WebRtcVideoChannel2::WebRtcVideoSendStream::SetDimensions(int width, |
pbos@webrtc.org | 6ae48c6 | 2014-06-06 10:49:19 +0000 | [diff] [blame] | 1536 | int height) { |
| 1537 | assert(!parameters_.video_streams.empty()); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1538 | LOG(LS_VERBOSE) << "SetDimensions: " << width << "x" << height; |
pbos@webrtc.org | 6ae48c6 | 2014-06-06 10:49:19 +0000 | [diff] [blame] | 1539 | if (parameters_.video_streams.back().width == width && |
| 1540 | parameters_.video_streams.back().height == height) { |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1541 | return; |
| 1542 | } |
| 1543 | |
| 1544 | // TODO(pbos): Fix me, this only affects the last stream! |
pbos@webrtc.org | 6ae48c6 | 2014-06-06 10:49:19 +0000 | [diff] [blame] | 1545 | parameters_.video_streams.back().width = width; |
| 1546 | parameters_.video_streams.back().height = height; |
| 1547 | |
pbos@webrtc.org | 6f48f1b | 2014-07-22 16:29:54 +0000 | [diff] [blame] | 1548 | VideoCodecSettings codec_settings; |
| 1549 | parameters_.codec_settings.Get(&codec_settings); |
| 1550 | void* encoder_settings = encoder_factory_->CreateVideoEncoderSettings( |
| 1551 | codec_settings.codec, parameters_.options); |
| 1552 | |
| 1553 | bool stream_reconfigured = stream_->ReconfigureVideoEncoder( |
| 1554 | parameters_.video_streams, encoder_settings); |
| 1555 | |
| 1556 | encoder_factory_->DestroyVideoEncoderSettings(codec_settings.codec, |
| 1557 | encoder_settings); |
| 1558 | |
| 1559 | if (!stream_reconfigured) { |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1560 | LOG(LS_WARNING) << "Failed to reconfigure video encoder for dimensions: " |
| 1561 | << width << "x" << height; |
| 1562 | return; |
| 1563 | } |
| 1564 | } |
| 1565 | |
| 1566 | void WebRtcVideoChannel2::WebRtcVideoSendStream::Start() { |
| 1567 | talk_base::CritScope cs(&lock_); |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1568 | assert(stream_ != NULL); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1569 | stream_->Start(); |
| 1570 | sending_ = true; |
| 1571 | } |
| 1572 | |
| 1573 | void WebRtcVideoChannel2::WebRtcVideoSendStream::Stop() { |
| 1574 | talk_base::CritScope cs(&lock_); |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1575 | if (stream_ != NULL) { |
| 1576 | stream_->Stop(); |
| 1577 | } |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1578 | sending_ = false; |
| 1579 | } |
| 1580 | |
pbos@webrtc.org | e6f84ae | 2014-07-18 11:11:55 +0000 | [diff] [blame] | 1581 | VideoSenderInfo |
| 1582 | WebRtcVideoChannel2::WebRtcVideoSendStream::GetVideoSenderInfo() { |
| 1583 | VideoSenderInfo info; |
| 1584 | talk_base::CritScope cs(&lock_); |
| 1585 | for (size_t i = 0; i < parameters_.config.rtp.ssrcs.size(); ++i) { |
| 1586 | info.add_ssrc(parameters_.config.rtp.ssrcs[i]); |
| 1587 | } |
| 1588 | |
| 1589 | webrtc::VideoSendStream::Stats stats = stream_->GetStats(); |
| 1590 | info.framerate_input = stats.input_frame_rate; |
| 1591 | info.framerate_sent = stats.encode_frame_rate; |
| 1592 | |
| 1593 | for (std::map<uint32_t, webrtc::StreamStats>::iterator it = |
| 1594 | stats.substreams.begin(); |
| 1595 | it != stats.substreams.end(); |
| 1596 | ++it) { |
| 1597 | // TODO(pbos): Wire up additional stats, such as padding bytes. |
| 1598 | webrtc::StreamStats stream_stats = it->second; |
| 1599 | info.bytes_sent += stream_stats.rtp_stats.bytes + |
| 1600 | stream_stats.rtp_stats.header_bytes + |
| 1601 | stream_stats.rtp_stats.padding_bytes; |
| 1602 | info.packets_sent += stream_stats.rtp_stats.packets; |
| 1603 | info.packets_lost += stream_stats.rtcp_stats.cumulative_lost; |
| 1604 | } |
| 1605 | |
| 1606 | if (!stats.substreams.empty()) { |
| 1607 | // TODO(pbos): Report fraction lost per SSRC. |
| 1608 | webrtc::StreamStats first_stream_stats = stats.substreams.begin()->second; |
| 1609 | info.fraction_lost = |
| 1610 | static_cast<float>(first_stream_stats.rtcp_stats.fraction_lost) / |
| 1611 | (1 << 8); |
| 1612 | } |
| 1613 | |
| 1614 | if (capturer_ != NULL && !capturer_->IsMuted()) { |
| 1615 | VideoFormat last_captured_frame_format; |
| 1616 | capturer_->GetStats(&info.adapt_frame_drops, |
| 1617 | &info.effects_frame_drops, |
| 1618 | &info.capturer_frame_time, |
| 1619 | &last_captured_frame_format); |
| 1620 | info.input_frame_width = last_captured_frame_format.width; |
| 1621 | info.input_frame_height = last_captured_frame_format.height; |
| 1622 | info.send_frame_width = |
| 1623 | static_cast<int>(parameters_.video_streams.front().width); |
| 1624 | info.send_frame_height = |
| 1625 | static_cast<int>(parameters_.video_streams.front().height); |
| 1626 | } |
| 1627 | |
| 1628 | // TODO(pbos): Support or remove the following stats. |
| 1629 | info.packets_cached = -1; |
| 1630 | info.rtt_ms = -1; |
| 1631 | |
| 1632 | return info; |
| 1633 | } |
| 1634 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1635 | void WebRtcVideoChannel2::WebRtcVideoSendStream::RecreateWebRtcStream() { |
| 1636 | if (stream_ != NULL) { |
| 1637 | call_->DestroyVideoSendStream(stream_); |
| 1638 | } |
pbos@webrtc.org | 6ae48c6 | 2014-06-06 10:49:19 +0000 | [diff] [blame] | 1639 | |
pbos@webrtc.org | 6f48f1b | 2014-07-22 16:29:54 +0000 | [diff] [blame] | 1640 | VideoCodecSettings codec_settings; |
| 1641 | parameters_.codec_settings.Get(&codec_settings); |
| 1642 | void* encoder_settings = encoder_factory_->CreateVideoEncoderSettings( |
| 1643 | codec_settings.codec, parameters_.options); |
| 1644 | |
pbos@webrtc.org | 6ae48c6 | 2014-06-06 10:49:19 +0000 | [diff] [blame] | 1645 | stream_ = call_->CreateVideoSendStream( |
pbos@webrtc.org | 6f48f1b | 2014-07-22 16:29:54 +0000 | [diff] [blame] | 1646 | parameters_.config, parameters_.video_streams, encoder_settings); |
| 1647 | |
| 1648 | encoder_factory_->DestroyVideoEncoderSettings(codec_settings.codec, |
| 1649 | encoder_settings); |
| 1650 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1651 | if (sending_) { |
| 1652 | stream_->Start(); |
| 1653 | } |
| 1654 | } |
| 1655 | |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 1656 | WebRtcVideoChannel2::WebRtcVideoReceiveStream::WebRtcVideoReceiveStream( |
| 1657 | webrtc::Call* call, |
| 1658 | const webrtc::VideoReceiveStream::Config& config, |
| 1659 | const std::vector<VideoCodecSettings>& recv_codecs) |
| 1660 | : call_(call), |
| 1661 | config_(config), |
| 1662 | stream_(NULL), |
| 1663 | last_width_(-1), |
| 1664 | last_height_(-1), |
| 1665 | renderer_(NULL) { |
| 1666 | config_.renderer = this; |
| 1667 | // SetRecvCodecs will also reset (start) the VideoReceiveStream. |
| 1668 | SetRecvCodecs(recv_codecs); |
| 1669 | } |
| 1670 | |
| 1671 | WebRtcVideoChannel2::WebRtcVideoReceiveStream::~WebRtcVideoReceiveStream() { |
| 1672 | call_->DestroyVideoReceiveStream(stream_); |
| 1673 | } |
| 1674 | |
| 1675 | void WebRtcVideoChannel2::WebRtcVideoReceiveStream::SetRecvCodecs( |
| 1676 | const std::vector<VideoCodecSettings>& recv_codecs) { |
| 1677 | // TODO(pbos): Reconfigure RTX based on incoming recv_codecs. |
| 1678 | // TODO(pbos): Base receive codecs off recv_codecs_ and set up using a |
| 1679 | // DecoderFactory similar to send side. Pending webrtc:2854. |
| 1680 | // Also set up default codecs if there's nothing in recv_codecs_. |
| 1681 | webrtc::VideoCodec codec; |
| 1682 | memset(&codec, 0, sizeof(codec)); |
| 1683 | |
| 1684 | codec.plType = kDefaultVideoCodecPref.payload_type; |
| 1685 | strcpy(codec.plName, kDefaultVideoCodecPref.name); |
| 1686 | codec.codecType = webrtc::kVideoCodecVP8; |
| 1687 | codec.codecSpecific.VP8.resilience = webrtc::kResilientStream; |
| 1688 | codec.codecSpecific.VP8.numberOfTemporalLayers = 1; |
| 1689 | codec.codecSpecific.VP8.denoisingOn = true; |
| 1690 | codec.codecSpecific.VP8.errorConcealmentOn = false; |
| 1691 | codec.codecSpecific.VP8.automaticResizeOn = false; |
| 1692 | codec.codecSpecific.VP8.frameDroppingOn = true; |
| 1693 | codec.codecSpecific.VP8.keyFrameInterval = 3000; |
| 1694 | // Bitrates don't matter and are ignored for the receiver. This is put in to |
| 1695 | // have the current underlying implementation accept the VideoCodec. |
| 1696 | codec.minBitrate = codec.startBitrate = codec.maxBitrate = 300; |
| 1697 | config_.codecs.clear(); |
| 1698 | config_.codecs.push_back(codec); |
| 1699 | |
| 1700 | config_.rtp.fec = recv_codecs.front().fec; |
| 1701 | |
| 1702 | RecreateWebRtcStream(); |
| 1703 | } |
| 1704 | |
| 1705 | void WebRtcVideoChannel2::WebRtcVideoReceiveStream::SetRtpExtensions( |
| 1706 | const std::vector<webrtc::RtpExtension>& extensions) { |
| 1707 | config_.rtp.extensions = extensions; |
| 1708 | RecreateWebRtcStream(); |
| 1709 | } |
| 1710 | |
| 1711 | void WebRtcVideoChannel2::WebRtcVideoReceiveStream::RecreateWebRtcStream() { |
| 1712 | if (stream_ != NULL) { |
| 1713 | call_->DestroyVideoReceiveStream(stream_); |
| 1714 | } |
| 1715 | stream_ = call_->CreateVideoReceiveStream(config_); |
| 1716 | stream_->Start(); |
| 1717 | } |
| 1718 | |
| 1719 | void WebRtcVideoChannel2::WebRtcVideoReceiveStream::RenderFrame( |
| 1720 | const webrtc::I420VideoFrame& frame, |
| 1721 | int time_to_render_ms) { |
| 1722 | talk_base::CritScope crit(&renderer_lock_); |
| 1723 | if (renderer_ == NULL) { |
| 1724 | LOG(LS_WARNING) << "VideoReceiveStream not connected to a VideoRenderer."; |
| 1725 | return; |
| 1726 | } |
| 1727 | |
| 1728 | if (frame.width() != last_width_ || frame.height() != last_height_) { |
| 1729 | SetSize(frame.width(), frame.height()); |
| 1730 | } |
| 1731 | |
| 1732 | LOG(LS_VERBOSE) << "RenderFrame: (" << frame.width() << "x" << frame.height() |
| 1733 | << ")"; |
| 1734 | |
| 1735 | const WebRtcVideoRenderFrame render_frame(&frame); |
| 1736 | renderer_->RenderFrame(&render_frame); |
| 1737 | } |
| 1738 | |
| 1739 | void WebRtcVideoChannel2::WebRtcVideoReceiveStream::SetRenderer( |
| 1740 | cricket::VideoRenderer* renderer) { |
| 1741 | talk_base::CritScope crit(&renderer_lock_); |
| 1742 | renderer_ = renderer; |
| 1743 | if (renderer_ != NULL && last_width_ != -1) { |
| 1744 | SetSize(last_width_, last_height_); |
| 1745 | } |
| 1746 | } |
| 1747 | |
| 1748 | VideoRenderer* WebRtcVideoChannel2::WebRtcVideoReceiveStream::GetRenderer() { |
| 1749 | // TODO(pbos): Remove GetRenderer and all uses of it, it's thread-unsafe by |
| 1750 | // design. |
| 1751 | talk_base::CritScope crit(&renderer_lock_); |
| 1752 | return renderer_; |
| 1753 | } |
| 1754 | |
| 1755 | void WebRtcVideoChannel2::WebRtcVideoReceiveStream::SetSize(int width, |
| 1756 | int height) { |
| 1757 | talk_base::CritScope crit(&renderer_lock_); |
| 1758 | if (!renderer_->SetSize(width, height, 0)) { |
| 1759 | LOG(LS_ERROR) << "Could not set renderer size."; |
| 1760 | } |
| 1761 | last_width_ = width; |
| 1762 | last_height_ = height; |
| 1763 | } |
| 1764 | |
pbos@webrtc.org | e6f84ae | 2014-07-18 11:11:55 +0000 | [diff] [blame] | 1765 | VideoReceiverInfo |
| 1766 | WebRtcVideoChannel2::WebRtcVideoReceiveStream::GetVideoReceiverInfo() { |
| 1767 | VideoReceiverInfo info; |
| 1768 | info.add_ssrc(config_.rtp.remote_ssrc); |
| 1769 | webrtc::VideoReceiveStream::Stats stats = stream_->GetStats(); |
| 1770 | info.bytes_rcvd = stats.rtp_stats.bytes + stats.rtp_stats.header_bytes + |
| 1771 | stats.rtp_stats.padding_bytes; |
| 1772 | info.packets_rcvd = stats.rtp_stats.packets; |
| 1773 | |
| 1774 | info.framerate_rcvd = stats.network_frame_rate; |
| 1775 | info.framerate_decoded = stats.decode_frame_rate; |
| 1776 | info.framerate_output = stats.render_frame_rate; |
| 1777 | |
| 1778 | talk_base::CritScope frame_cs(&renderer_lock_); |
| 1779 | info.frame_width = last_width_; |
| 1780 | info.frame_height = last_height_; |
| 1781 | |
| 1782 | // TODO(pbos): Support or remove the following stats. |
| 1783 | info.packets_concealed = -1; |
| 1784 | |
| 1785 | return info; |
| 1786 | } |
| 1787 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1788 | WebRtcVideoChannel2::VideoCodecSettings::VideoCodecSettings() |
| 1789 | : rtx_payload_type(-1) {} |
| 1790 | |
| 1791 | std::vector<WebRtcVideoChannel2::VideoCodecSettings> |
| 1792 | WebRtcVideoChannel2::MapCodecs(const std::vector<VideoCodec>& codecs) { |
| 1793 | assert(!codecs.empty()); |
| 1794 | |
| 1795 | std::vector<VideoCodecSettings> video_codecs; |
| 1796 | std::map<int, bool> payload_used; |
pbos@webrtc.org | e322a17 | 2014-06-13 11:47:28 +0000 | [diff] [blame] | 1797 | std::map<int, VideoCodec::CodecType> payload_codec_type; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1798 | std::map<int, int> rtx_mapping; // video payload type -> rtx payload type. |
| 1799 | |
| 1800 | webrtc::FecConfig fec_settings; |
| 1801 | |
| 1802 | for (size_t i = 0; i < codecs.size(); ++i) { |
| 1803 | const VideoCodec& in_codec = codecs[i]; |
| 1804 | int payload_type = in_codec.id; |
| 1805 | |
| 1806 | if (payload_used[payload_type]) { |
| 1807 | LOG(LS_ERROR) << "Payload type already registered: " |
| 1808 | << in_codec.ToString(); |
| 1809 | return std::vector<VideoCodecSettings>(); |
| 1810 | } |
| 1811 | payload_used[payload_type] = true; |
pbos@webrtc.org | e322a17 | 2014-06-13 11:47:28 +0000 | [diff] [blame] | 1812 | payload_codec_type[payload_type] = in_codec.GetCodecType(); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1813 | |
| 1814 | switch (in_codec.GetCodecType()) { |
| 1815 | case VideoCodec::CODEC_RED: { |
| 1816 | // RED payload type, should not have duplicates. |
| 1817 | assert(fec_settings.red_payload_type == -1); |
| 1818 | fec_settings.red_payload_type = in_codec.id; |
| 1819 | continue; |
| 1820 | } |
| 1821 | |
| 1822 | case VideoCodec::CODEC_ULPFEC: { |
| 1823 | // ULPFEC payload type, should not have duplicates. |
| 1824 | assert(fec_settings.ulpfec_payload_type == -1); |
| 1825 | fec_settings.ulpfec_payload_type = in_codec.id; |
| 1826 | continue; |
| 1827 | } |
| 1828 | |
| 1829 | case VideoCodec::CODEC_RTX: { |
| 1830 | int associated_payload_type; |
| 1831 | if (!in_codec.GetParam(kCodecParamAssociatedPayloadType, |
| 1832 | &associated_payload_type)) { |
| 1833 | LOG(LS_ERROR) << "RTX codec without associated payload type: " |
| 1834 | << in_codec.ToString(); |
| 1835 | return std::vector<VideoCodecSettings>(); |
| 1836 | } |
| 1837 | rtx_mapping[associated_payload_type] = in_codec.id; |
| 1838 | continue; |
| 1839 | } |
| 1840 | |
| 1841 | case VideoCodec::CODEC_VIDEO: |
| 1842 | break; |
| 1843 | } |
| 1844 | |
| 1845 | video_codecs.push_back(VideoCodecSettings()); |
| 1846 | video_codecs.back().codec = in_codec; |
| 1847 | } |
| 1848 | |
| 1849 | // One of these codecs should have been a video codec. Only having FEC |
| 1850 | // parameters into this code is a logic error. |
| 1851 | assert(!video_codecs.empty()); |
| 1852 | |
pbos@webrtc.org | e322a17 | 2014-06-13 11:47:28 +0000 | [diff] [blame] | 1853 | for (std::map<int, int>::const_iterator it = rtx_mapping.begin(); |
| 1854 | it != rtx_mapping.end(); |
| 1855 | ++it) { |
| 1856 | if (!payload_used[it->first]) { |
| 1857 | LOG(LS_ERROR) << "RTX mapped to payload not in codec list."; |
| 1858 | return std::vector<VideoCodecSettings>(); |
| 1859 | } |
| 1860 | if (payload_codec_type[it->first] != VideoCodec::CODEC_VIDEO) { |
| 1861 | LOG(LS_ERROR) << "RTX not mapped to regular video codec."; |
| 1862 | return std::vector<VideoCodecSettings>(); |
| 1863 | } |
| 1864 | } |
| 1865 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1866 | // TODO(pbos): Write tests that figure out that I have not verified that RTX |
| 1867 | // codecs aren't mapped to bogus payloads. |
| 1868 | for (size_t i = 0; i < video_codecs.size(); ++i) { |
| 1869 | video_codecs[i].fec = fec_settings; |
| 1870 | if (rtx_mapping[video_codecs[i].codec.id] != 0) { |
| 1871 | video_codecs[i].rtx_payload_type = rtx_mapping[video_codecs[i].codec.id]; |
| 1872 | } |
| 1873 | } |
| 1874 | |
| 1875 | return video_codecs; |
| 1876 | } |
| 1877 | |
| 1878 | std::vector<WebRtcVideoChannel2::VideoCodecSettings> |
| 1879 | WebRtcVideoChannel2::FilterSupportedCodecs( |
| 1880 | const std::vector<WebRtcVideoChannel2::VideoCodecSettings>& mapped_codecs) { |
| 1881 | std::vector<VideoCodecSettings> supported_codecs; |
| 1882 | for (size_t i = 0; i < mapped_codecs.size(); ++i) { |
| 1883 | if (encoder_factory_->SupportsCodec(mapped_codecs[i].codec)) { |
| 1884 | supported_codecs.push_back(mapped_codecs[i]); |
| 1885 | } |
| 1886 | } |
| 1887 | return supported_codecs; |
| 1888 | } |
| 1889 | |
| 1890 | } // namespace cricket |
| 1891 | |
| 1892 | #endif // HAVE_WEBRTC_VIDEO |