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 | c37e72e | 2015-01-05 18:51:13 +0000 | [diff] [blame] | 31 | #include <algorithm> |
pbos@webrtc.org | 3c10758 | 2014-07-20 15:27:35 +0000 | [diff] [blame] | 32 | #include <set> |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 33 | #include <string> |
| 34 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 35 | #include "talk/media/base/videocapturer.h" |
| 36 | #include "talk/media/base/videorenderer.h" |
buildbot@webrtc.org | df9bbbe | 2014-06-19 19:54:33 +0000 | [diff] [blame] | 37 | #include "talk/media/webrtc/constants.h" |
buildbot@webrtc.org | a853077 | 2014-12-10 09:01:18 +0000 | [diff] [blame] | 38 | #include "talk/media/webrtc/simulcast.h" |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 39 | #include "talk/media/webrtc/webrtcvideocapturer.h" |
Peter Boström | 81ea54e | 2015-05-07 11:41:09 +0200 | [diff] [blame^] | 40 | #include "talk/media/webrtc/webrtcvideoencoderfactory.h" |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 41 | #include "talk/media/webrtc/webrtcvideoframe.h" |
| 42 | #include "talk/media/webrtc/webrtcvoiceengine.h" |
buildbot@webrtc.org | a09a999 | 2014-08-13 17:26:08 +0000 | [diff] [blame] | 43 | #include "webrtc/base/buffer.h" |
| 44 | #include "webrtc/base/logging.h" |
| 45 | #include "webrtc/base/stringutils.h" |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 46 | #include "webrtc/call.h" |
Peter Boström | 81ea54e | 2015-05-07 11:41:09 +0200 | [diff] [blame^] | 47 | #include "webrtc/modules/video_coding/codecs/vp8/simulcast_encoder_adapter.h" |
| 48 | #include "webrtc/system_wrappers/interface/field_trial.h" |
pbos@webrtc.org | 50fe359 | 2015-01-29 12:33:07 +0000 | [diff] [blame] | 49 | #include "webrtc/system_wrappers/interface/trace_event.h" |
pbos@webrtc.org | 776e6f2 | 2014-10-29 15:28:39 +0000 | [diff] [blame] | 50 | #include "webrtc/video_decoder.h" |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 51 | #include "webrtc/video_encoder.h" |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 52 | |
| 53 | #define UNIMPLEMENTED \ |
| 54 | LOG(LS_ERROR) << "Call to unimplemented function " << __FUNCTION__; \ |
| 55 | ASSERT(false) |
| 56 | |
| 57 | namespace cricket { |
pbos@webrtc.org | 7fe1e03 | 2014-10-14 04:25:33 +0000 | [diff] [blame] | 58 | namespace { |
Peter Boström | 81ea54e | 2015-05-07 11:41:09 +0200 | [diff] [blame^] | 59 | |
| 60 | // Wrap cricket::WebRtcVideoEncoderFactory as a webrtc::VideoEncoderFactory. |
| 61 | class EncoderFactoryAdapter : public webrtc::VideoEncoderFactory { |
| 62 | public: |
| 63 | // EncoderFactoryAdapter doesn't take ownership of |factory|, which is owned |
| 64 | // by e.g. PeerConnectionFactory. |
| 65 | explicit EncoderFactoryAdapter(cricket::WebRtcVideoEncoderFactory* factory) |
| 66 | : factory_(factory) {} |
| 67 | virtual ~EncoderFactoryAdapter() {} |
| 68 | |
| 69 | // Implement webrtc::VideoEncoderFactory. |
| 70 | webrtc::VideoEncoder* Create() override { |
| 71 | return factory_->CreateVideoEncoder(webrtc::kVideoCodecVP8); |
| 72 | } |
| 73 | |
| 74 | void Destroy(webrtc::VideoEncoder* encoder) override { |
| 75 | return factory_->DestroyVideoEncoder(encoder); |
| 76 | } |
| 77 | |
| 78 | private: |
| 79 | cricket::WebRtcVideoEncoderFactory* const factory_; |
| 80 | }; |
| 81 | |
| 82 | // An encoder factory that wraps Create requests for simulcastable codec types |
| 83 | // with a webrtc::SimulcastEncoderAdapter. Non simulcastable codec type |
| 84 | // requests are just passed through to the contained encoder factory. |
| 85 | class WebRtcSimulcastEncoderFactory |
| 86 | : public cricket::WebRtcVideoEncoderFactory { |
| 87 | public: |
| 88 | // WebRtcSimulcastEncoderFactory doesn't take ownership of |factory|, which is |
| 89 | // owned by e.g. PeerConnectionFactory. |
| 90 | explicit WebRtcSimulcastEncoderFactory( |
| 91 | cricket::WebRtcVideoEncoderFactory* factory) |
| 92 | : factory_(factory) {} |
| 93 | |
| 94 | static bool UseSimulcastEncoderFactory( |
| 95 | const std::vector<VideoCodec>& codecs) { |
| 96 | // If any codec is VP8, use the simulcast factory. If asked to create a |
| 97 | // non-VP8 codec, we'll just return a contained factory encoder directly. |
| 98 | for (const auto& codec : codecs) { |
| 99 | if (codec.type == webrtc::kVideoCodecVP8) { |
| 100 | return true; |
| 101 | } |
| 102 | } |
| 103 | return false; |
| 104 | } |
| 105 | |
| 106 | webrtc::VideoEncoder* CreateVideoEncoder( |
| 107 | webrtc::VideoCodecType type) override { |
| 108 | ASSERT(factory_ != NULL); |
| 109 | // If it's a codec type we can simulcast, create a wrapped encoder. |
| 110 | if (type == webrtc::kVideoCodecVP8) { |
| 111 | return new webrtc::SimulcastEncoderAdapter( |
| 112 | new EncoderFactoryAdapter(factory_)); |
| 113 | } |
| 114 | webrtc::VideoEncoder* encoder = factory_->CreateVideoEncoder(type); |
| 115 | if (encoder) { |
| 116 | non_simulcast_encoders_.push_back(encoder); |
| 117 | } |
| 118 | return encoder; |
| 119 | } |
| 120 | |
| 121 | const std::vector<VideoCodec>& codecs() const override { |
| 122 | return factory_->codecs(); |
| 123 | } |
| 124 | |
| 125 | bool EncoderTypeHasInternalSource( |
| 126 | webrtc::VideoCodecType type) const override { |
| 127 | return factory_->EncoderTypeHasInternalSource(type); |
| 128 | } |
| 129 | |
| 130 | void DestroyVideoEncoder(webrtc::VideoEncoder* encoder) override { |
| 131 | // Check first to see if the encoder wasn't wrapped in a |
| 132 | // SimulcastEncoderAdapter. In that case, ask the factory to destroy it. |
| 133 | if (std::remove(non_simulcast_encoders_.begin(), |
| 134 | non_simulcast_encoders_.end(), |
| 135 | encoder) != non_simulcast_encoders_.end()) { |
| 136 | factory_->DestroyVideoEncoder(encoder); |
| 137 | return; |
| 138 | } |
| 139 | |
| 140 | // Otherwise, SimulcastEncoderAdapter can be deleted directly, and will call |
| 141 | // DestroyVideoEncoder on the factory for individual encoder instances. |
| 142 | delete encoder; |
| 143 | } |
| 144 | |
| 145 | private: |
| 146 | cricket::WebRtcVideoEncoderFactory* factory_; |
| 147 | // A list of encoders that were created without being wrapped in a |
| 148 | // SimulcastEncoderAdapter. |
| 149 | std::vector<webrtc::VideoEncoder*> non_simulcast_encoders_; |
| 150 | }; |
| 151 | |
| 152 | bool CodecIsInternallySupported(const std::string& codec_name) { |
| 153 | if (CodecNamesEq(codec_name, kVp8CodecName)) { |
| 154 | return true; |
| 155 | } |
| 156 | if (CodecNamesEq(codec_name, kVp9CodecName)) { |
| 157 | const std::string group_name = |
| 158 | webrtc::field_trial::FindFullName("WebRTC-SupportVP9"); |
| 159 | return group_name == "Enabled" || group_name == "EnabledByFlag"; |
| 160 | } |
| 161 | return false; |
| 162 | } |
| 163 | |
| 164 | void AddDefaultFeedbackParams(VideoCodec* codec) { |
| 165 | codec->AddFeedbackParam(FeedbackParam(kRtcpFbParamCcm, kRtcpFbCcmParamFir)); |
| 166 | codec->AddFeedbackParam(FeedbackParam(kRtcpFbParamNack, kParamValueEmpty)); |
| 167 | codec->AddFeedbackParam(FeedbackParam(kRtcpFbParamNack, kRtcpFbNackParamPli)); |
| 168 | codec->AddFeedbackParam(FeedbackParam(kRtcpFbParamRemb, kParamValueEmpty)); |
| 169 | } |
| 170 | |
| 171 | static VideoCodec MakeVideoCodecWithDefaultFeedbackParams(int payload_type, |
| 172 | const char* name) { |
| 173 | VideoCodec codec(payload_type, name, kDefaultVideoMaxWidth, |
| 174 | kDefaultVideoMaxHeight, kDefaultVideoMaxFramerate, 0); |
| 175 | AddDefaultFeedbackParams(&codec); |
| 176 | return codec; |
| 177 | } |
| 178 | |
pbos@webrtc.org | 7fe1e03 | 2014-10-14 04:25:33 +0000 | [diff] [blame] | 179 | static std::string CodecVectorToString(const std::vector<VideoCodec>& codecs) { |
| 180 | std::stringstream out; |
| 181 | out << '{'; |
| 182 | for (size_t i = 0; i < codecs.size(); ++i) { |
| 183 | out << codecs[i].ToString(); |
| 184 | if (i != codecs.size() - 1) { |
| 185 | out << ", "; |
| 186 | } |
| 187 | } |
| 188 | out << '}'; |
| 189 | return out.str(); |
| 190 | } |
| 191 | |
| 192 | static bool ValidateCodecFormats(const std::vector<VideoCodec>& codecs) { |
| 193 | bool has_video = false; |
| 194 | for (size_t i = 0; i < codecs.size(); ++i) { |
| 195 | if (!codecs[i].ValidateCodecFormat()) { |
| 196 | return false; |
| 197 | } |
| 198 | if (codecs[i].GetCodecType() == VideoCodec::CODEC_VIDEO) { |
| 199 | has_video = true; |
| 200 | } |
| 201 | } |
| 202 | if (!has_video) { |
| 203 | LOG(LS_ERROR) << "Setting codecs without a video codec is invalid: " |
| 204 | << CodecVectorToString(codecs); |
| 205 | return false; |
| 206 | } |
| 207 | return true; |
| 208 | } |
| 209 | |
Peter Boström | d4362cd | 2015-03-25 14:17:23 +0100 | [diff] [blame] | 210 | static bool ValidateStreamParams(const StreamParams& sp) { |
| 211 | if (sp.ssrcs.empty()) { |
| 212 | LOG(LS_ERROR) << "No SSRCs in stream parameters: " << sp.ToString(); |
| 213 | return false; |
| 214 | } |
| 215 | |
| 216 | std::vector<uint32> primary_ssrcs; |
| 217 | sp.GetPrimarySsrcs(&primary_ssrcs); |
| 218 | std::vector<uint32> rtx_ssrcs; |
| 219 | sp.GetFidSsrcs(primary_ssrcs, &rtx_ssrcs); |
| 220 | for (uint32_t rtx_ssrc : rtx_ssrcs) { |
| 221 | bool rtx_ssrc_present = false; |
| 222 | for (uint32_t sp_ssrc : sp.ssrcs) { |
| 223 | if (sp_ssrc == rtx_ssrc) { |
| 224 | rtx_ssrc_present = true; |
| 225 | break; |
| 226 | } |
| 227 | } |
| 228 | if (!rtx_ssrc_present) { |
| 229 | LOG(LS_ERROR) << "RTX SSRC '" << rtx_ssrc |
| 230 | << "' missing from StreamParams ssrcs: " << sp.ToString(); |
| 231 | return false; |
| 232 | } |
| 233 | } |
| 234 | if (!rtx_ssrcs.empty() && primary_ssrcs.size() != rtx_ssrcs.size()) { |
| 235 | LOG(LS_ERROR) |
| 236 | << "RTX SSRCs exist, but don't cover all SSRCs (unsupported): " |
| 237 | << sp.ToString(); |
| 238 | return false; |
| 239 | } |
| 240 | |
| 241 | return true; |
| 242 | } |
| 243 | |
pbos@webrtc.org | 7fe1e03 | 2014-10-14 04:25:33 +0000 | [diff] [blame] | 244 | static std::string RtpExtensionsToString( |
| 245 | const std::vector<RtpHeaderExtension>& extensions) { |
| 246 | std::stringstream out; |
| 247 | out << '{'; |
| 248 | for (size_t i = 0; i < extensions.size(); ++i) { |
| 249 | out << "{" << extensions[i].uri << ": " << extensions[i].id << "}"; |
| 250 | if (i != extensions.size() - 1) { |
| 251 | out << ", "; |
| 252 | } |
| 253 | } |
| 254 | out << '}'; |
| 255 | return out.str(); |
| 256 | } |
| 257 | |
Guo-wei Shieh | 64c1e8c | 2015-04-01 15:33:06 -0700 | [diff] [blame] | 258 | inline const webrtc::RtpExtension* FindHeaderExtension( |
| 259 | const std::vector<webrtc::RtpExtension>& extensions, |
| 260 | const std::string& name) { |
| 261 | for (const auto& kv : extensions) { |
| 262 | if (kv.name == name) { |
| 263 | return &kv; |
| 264 | } |
| 265 | } |
| 266 | return NULL; |
| 267 | } |
| 268 | |
andresp@webrtc.org | 82775b1 | 2014-11-07 09:37:54 +0000 | [diff] [blame] | 269 | // Merges two fec configs and logs an error if a conflict arises |
Shao Changbin | e62202f | 2015-04-21 20:24:50 +0800 | [diff] [blame] | 270 | // such that merging in different order would trigger a different output. |
andresp@webrtc.org | 82775b1 | 2014-11-07 09:37:54 +0000 | [diff] [blame] | 271 | static void MergeFecConfig(const webrtc::FecConfig& other, |
| 272 | webrtc::FecConfig* output) { |
| 273 | if (other.ulpfec_payload_type != -1) { |
| 274 | if (output->ulpfec_payload_type != -1 && |
| 275 | output->ulpfec_payload_type != other.ulpfec_payload_type) { |
| 276 | LOG(LS_WARNING) << "Conflict merging ulpfec_payload_type configs: " |
| 277 | << output->ulpfec_payload_type << " and " |
| 278 | << other.ulpfec_payload_type; |
| 279 | } |
| 280 | output->ulpfec_payload_type = other.ulpfec_payload_type; |
| 281 | } |
| 282 | if (other.red_payload_type != -1) { |
| 283 | if (output->red_payload_type != -1 && |
| 284 | output->red_payload_type != other.red_payload_type) { |
| 285 | LOG(LS_WARNING) << "Conflict merging red_payload_type configs: " |
| 286 | << output->red_payload_type << " and " |
| 287 | << other.red_payload_type; |
| 288 | } |
| 289 | output->red_payload_type = other.red_payload_type; |
| 290 | } |
Shao Changbin | e62202f | 2015-04-21 20:24:50 +0800 | [diff] [blame] | 291 | if (other.red_rtx_payload_type != -1) { |
| 292 | if (output->red_rtx_payload_type != -1 && |
| 293 | output->red_rtx_payload_type != other.red_rtx_payload_type) { |
| 294 | LOG(LS_WARNING) << "Conflict merging red_rtx_payload_type configs: " |
| 295 | << output->red_rtx_payload_type << " and " |
| 296 | << other.red_rtx_payload_type; |
| 297 | } |
| 298 | output->red_rtx_payload_type = other.red_rtx_payload_type; |
| 299 | } |
andresp@webrtc.org | 82775b1 | 2014-11-07 09:37:54 +0000 | [diff] [blame] | 300 | } |
pbos@webrtc.org | 7fe1e03 | 2014-10-14 04:25:33 +0000 | [diff] [blame] | 301 | } // namespace |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 302 | |
Peter Boström | 81ea54e | 2015-05-07 11:41:09 +0200 | [diff] [blame^] | 303 | // Constants defined in talk/media/webrtc/constants.h |
| 304 | // TODO(pbos): Move these to a separate constants.cc file. |
| 305 | const int kMinVideoBitrate = 30; |
| 306 | const int kStartVideoBitrate = 300; |
| 307 | const int kMaxVideoBitrate = 2000; |
| 308 | |
| 309 | const int kVideoMtu = 1200; |
| 310 | const int kVideoRtpBufferSize = 65536; |
| 311 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 312 | // This constant is really an on/off, lower-level configurable NACK history |
| 313 | // duration hasn't been implemented. |
| 314 | static const int kNackHistoryMs = 1000; |
| 315 | |
buildbot@webrtc.org | 933d88a | 2014-09-18 20:23:05 +0000 | [diff] [blame] | 316 | static const int kDefaultQpMax = 56; |
| 317 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 318 | static const int kDefaultRtcpReceiverReportSsrc = 1; |
| 319 | |
pbos@webrtc.org | 7fe1e03 | 2014-10-14 04:25:33 +0000 | [diff] [blame] | 320 | const char kH264CodecName[] = "H264"; |
| 321 | |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 322 | const int kMinBandwidthBps = 30000; |
| 323 | const int kStartBandwidthBps = 300000; |
| 324 | const int kMaxBandwidthBps = 2000000; |
| 325 | |
Peter Boström | 81ea54e | 2015-05-07 11:41:09 +0200 | [diff] [blame^] | 326 | std::vector<VideoCodec> DefaultVideoCodecList() { |
| 327 | std::vector<VideoCodec> codecs; |
| 328 | if (CodecIsInternallySupported(kVp9CodecName)) { |
| 329 | codecs.push_back(MakeVideoCodecWithDefaultFeedbackParams(kDefaultVp9PlType, |
| 330 | kVp9CodecName)); |
| 331 | // TODO(andresp): Add rtx codec for vp9 and verify it works. |
| 332 | } |
| 333 | codecs.push_back(MakeVideoCodecWithDefaultFeedbackParams(kDefaultVp8PlType, |
| 334 | kVp8CodecName)); |
| 335 | codecs.push_back( |
| 336 | VideoCodec::CreateRtxCodec(kDefaultRtxVp8PlType, kDefaultVp8PlType)); |
| 337 | codecs.push_back(VideoCodec(kDefaultRedPlType, kRedCodecName)); |
| 338 | codecs.push_back(VideoCodec(kDefaultUlpfecType, kUlpfecCodecName)); |
| 339 | return codecs; |
| 340 | } |
| 341 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 342 | static bool FindFirstMatchingCodec(const std::vector<VideoCodec>& codecs, |
| 343 | const VideoCodec& requested_codec, |
| 344 | VideoCodec* matching_codec) { |
| 345 | for (size_t i = 0; i < codecs.size(); ++i) { |
| 346 | if (requested_codec.Matches(codecs[i])) { |
| 347 | *matching_codec = codecs[i]; |
| 348 | return true; |
| 349 | } |
| 350 | } |
| 351 | return false; |
| 352 | } |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 353 | |
pbos@webrtc.org | 3c10758 | 2014-07-20 15:27:35 +0000 | [diff] [blame] | 354 | static bool ValidateRtpHeaderExtensionIds( |
| 355 | const std::vector<RtpHeaderExtension>& extensions) { |
| 356 | std::set<int> extensions_used; |
| 357 | for (size_t i = 0; i < extensions.size(); ++i) { |
Peter Boström | 23914fe | 2015-03-31 15:08:04 +0200 | [diff] [blame] | 358 | if (extensions[i].id <= 0 || extensions[i].id >= 15 || |
pbos@webrtc.org | 3c10758 | 2014-07-20 15:27:35 +0000 | [diff] [blame] | 359 | !extensions_used.insert(extensions[i].id).second) { |
| 360 | LOG(LS_ERROR) << "RTP extensions are with incorrect or duplicate ids."; |
| 361 | return false; |
| 362 | } |
| 363 | } |
| 364 | return true; |
| 365 | } |
| 366 | |
pbos@webrtc.org | c37e72e | 2015-01-05 18:51:13 +0000 | [diff] [blame] | 367 | static bool CompareRtpHeaderExtensionIds( |
| 368 | const webrtc::RtpExtension& extension1, |
| 369 | const webrtc::RtpExtension& extension2) { |
| 370 | // Sorting on ID is sufficient, more than one extension per ID is unsupported. |
| 371 | return extension1.id > extension2.id; |
| 372 | } |
| 373 | |
pbos@webrtc.org | 3c10758 | 2014-07-20 15:27:35 +0000 | [diff] [blame] | 374 | static std::vector<webrtc::RtpExtension> FilterRtpExtensions( |
| 375 | const std::vector<RtpHeaderExtension>& extensions) { |
| 376 | std::vector<webrtc::RtpExtension> webrtc_extensions; |
| 377 | for (size_t i = 0; i < extensions.size(); ++i) { |
| 378 | // Unsupported extensions will be ignored. |
Fredrik Solenberg | 23fba1f | 2015-04-29 15:24:01 +0200 | [diff] [blame] | 379 | if (webrtc::RtpExtension::IsSupportedForVideo(extensions[i].uri)) { |
pbos@webrtc.org | 3c10758 | 2014-07-20 15:27:35 +0000 | [diff] [blame] | 380 | webrtc_extensions.push_back(webrtc::RtpExtension( |
| 381 | extensions[i].uri, extensions[i].id)); |
| 382 | } else { |
| 383 | LOG(LS_WARNING) << "Unsupported RTP extension: " << extensions[i].uri; |
| 384 | } |
| 385 | } |
pbos@webrtc.org | c37e72e | 2015-01-05 18:51:13 +0000 | [diff] [blame] | 386 | |
| 387 | // Sort filtered headers to make sure that they can later be compared |
| 388 | // regardless of in which order they were entered. |
| 389 | std::sort(webrtc_extensions.begin(), webrtc_extensions.end(), |
| 390 | CompareRtpHeaderExtensionIds); |
pbos@webrtc.org | 3c10758 | 2014-07-20 15:27:35 +0000 | [diff] [blame] | 391 | return webrtc_extensions; |
| 392 | } |
| 393 | |
pbos@webrtc.org | c37e72e | 2015-01-05 18:51:13 +0000 | [diff] [blame] | 394 | static bool RtpExtensionsHaveChanged( |
| 395 | const std::vector<webrtc::RtpExtension>& before, |
| 396 | const std::vector<webrtc::RtpExtension>& after) { |
| 397 | if (before.size() != after.size()) |
| 398 | return true; |
| 399 | for (size_t i = 0; i < before.size(); ++i) { |
| 400 | if (before[i].id != after[i].id) |
| 401 | return true; |
| 402 | if (before[i].name != after[i].name) |
| 403 | return true; |
| 404 | } |
| 405 | return false; |
| 406 | } |
| 407 | |
buildbot@webrtc.org | a853077 | 2014-12-10 09:01:18 +0000 | [diff] [blame] | 408 | std::vector<webrtc::VideoStream> |
pbos@webrtc.org | f1c8b90 | 2015-01-14 17:29:27 +0000 | [diff] [blame] | 409 | WebRtcVideoChannel2::WebRtcVideoSendStream::CreateSimulcastVideoStreams( |
buildbot@webrtc.org | a853077 | 2014-12-10 09:01:18 +0000 | [diff] [blame] | 410 | const VideoCodec& codec, |
| 411 | const VideoOptions& options, |
Peter Boström | dfd53fe | 2015-03-27 15:58:11 +0100 | [diff] [blame] | 412 | int max_bitrate_bps, |
buildbot@webrtc.org | a853077 | 2014-12-10 09:01:18 +0000 | [diff] [blame] | 413 | size_t num_streams) { |
buildbot@webrtc.org | a853077 | 2014-12-10 09:01:18 +0000 | [diff] [blame] | 414 | int max_qp = kDefaultQpMax; |
| 415 | codec.GetParam(kCodecParamMaxQuantization, &max_qp); |
| 416 | |
buildbot@webrtc.org | a853077 | 2014-12-10 09:01:18 +0000 | [diff] [blame] | 417 | return GetSimulcastConfig( |
Peter Boström | dfd53fe | 2015-03-27 15:58:11 +0100 | [diff] [blame] | 418 | num_streams, GetSimulcastBitrateMode(options), codec.width, codec.height, |
| 419 | max_bitrate_bps, max_qp, |
buildbot@webrtc.org | a853077 | 2014-12-10 09:01:18 +0000 | [diff] [blame] | 420 | codec.framerate != 0 ? codec.framerate : kDefaultVideoMaxFramerate); |
| 421 | } |
| 422 | |
pbos@webrtc.org | f1c8b90 | 2015-01-14 17:29:27 +0000 | [diff] [blame] | 423 | std::vector<webrtc::VideoStream> |
| 424 | WebRtcVideoChannel2::WebRtcVideoSendStream::CreateVideoStreams( |
buildbot@webrtc.org | d41eaeb | 2014-06-12 07:13:26 +0000 | [diff] [blame] | 425 | const VideoCodec& codec, |
| 426 | const VideoOptions& options, |
Peter Boström | dfd53fe | 2015-03-27 15:58:11 +0100 | [diff] [blame] | 427 | int max_bitrate_bps, |
buildbot@webrtc.org | d41eaeb | 2014-06-12 07:13:26 +0000 | [diff] [blame] | 428 | size_t num_streams) { |
Peter Boström | dfd53fe | 2015-03-27 15:58:11 +0100 | [diff] [blame] | 429 | int codec_max_bitrate_kbps; |
| 430 | if (codec.GetParam(kCodecParamMaxBitrate, &codec_max_bitrate_kbps)) { |
| 431 | max_bitrate_bps = codec_max_bitrate_kbps * 1000; |
| 432 | } |
| 433 | if (num_streams != 1) { |
| 434 | return CreateSimulcastVideoStreams(codec, options, max_bitrate_bps, |
| 435 | num_streams); |
| 436 | } |
| 437 | |
| 438 | // For unset max bitrates set default bitrate for non-simulcast. |
| 439 | if (max_bitrate_bps <= 0) |
| 440 | max_bitrate_bps = kMaxVideoBitrate * 1000; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 441 | |
buildbot@webrtc.org | d41eaeb | 2014-06-12 07:13:26 +0000 | [diff] [blame] | 442 | webrtc::VideoStream stream; |
| 443 | stream.width = codec.width; |
| 444 | stream.height = codec.height; |
| 445 | stream.max_framerate = |
andresp@webrtc.org | 82775b1 | 2014-11-07 09:37:54 +0000 | [diff] [blame] | 446 | codec.framerate != 0 ? codec.framerate : kDefaultVideoMaxFramerate; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 447 | |
pbos@webrtc.org | 0087318 | 2014-11-25 14:03:34 +0000 | [diff] [blame] | 448 | stream.min_bitrate_bps = kMinVideoBitrate * 1000; |
Peter Boström | dfd53fe | 2015-03-27 15:58:11 +0100 | [diff] [blame] | 449 | stream.target_bitrate_bps = stream.max_bitrate_bps = max_bitrate_bps; |
buildbot@webrtc.org | d41eaeb | 2014-06-12 07:13:26 +0000 | [diff] [blame] | 450 | |
buildbot@webrtc.org | 933d88a | 2014-09-18 20:23:05 +0000 | [diff] [blame] | 451 | int max_qp = kDefaultQpMax; |
buildbot@webrtc.org | d41eaeb | 2014-06-12 07:13:26 +0000 | [diff] [blame] | 452 | codec.GetParam(kCodecParamMaxQuantization, &max_qp); |
| 453 | stream.max_qp = max_qp; |
| 454 | std::vector<webrtc::VideoStream> streams; |
| 455 | streams.push_back(stream); |
| 456 | return streams; |
| 457 | } |
| 458 | |
pbos@webrtc.org | f1c8b90 | 2015-01-14 17:29:27 +0000 | [diff] [blame] | 459 | void* WebRtcVideoChannel2::WebRtcVideoSendStream::ConfigureVideoEncoderSettings( |
pbos@webrtc.org | 6f48f1b | 2014-07-22 16:29:54 +0000 | [diff] [blame] | 460 | const VideoCodec& codec, |
Erik Språng | 143cec1 | 2015-04-28 10:01:41 +0200 | [diff] [blame] | 461 | const VideoOptions& options, |
| 462 | bool is_screencast) { |
| 463 | // No automatic resizing when using simulcast. |
| 464 | bool automatic_resize = !is_screencast && ssrcs_.size() == 1; |
| 465 | bool frame_dropping = !is_screencast; |
| 466 | bool denoising; |
| 467 | if (is_screencast) { |
| 468 | denoising = false; |
| 469 | } else { |
| 470 | options.video_noise_reduction.Get(&denoising); |
| 471 | } |
| 472 | |
Shao Changbin | e62202f | 2015-04-21 20:24:50 +0800 | [diff] [blame] | 473 | if (CodecNamesEq(codec.name, kVp8CodecName)) { |
pbos@webrtc.org | f1c8b90 | 2015-01-14 17:29:27 +0000 | [diff] [blame] | 474 | encoder_settings_.vp8 = webrtc::VideoEncoder::GetDefaultVp8Settings(); |
Erik Språng | 143cec1 | 2015-04-28 10:01:41 +0200 | [diff] [blame] | 475 | encoder_settings_.vp8.automaticResizeOn = automatic_resize; |
| 476 | encoder_settings_.vp8.denoisingOn = denoising; |
| 477 | encoder_settings_.vp8.frameDroppingOn = frame_dropping; |
pbos@webrtc.org | f1c8b90 | 2015-01-14 17:29:27 +0000 | [diff] [blame] | 478 | return &encoder_settings_.vp8; |
pbos@webrtc.org | 6f48f1b | 2014-07-22 16:29:54 +0000 | [diff] [blame] | 479 | } |
Shao Changbin | e62202f | 2015-04-21 20:24:50 +0800 | [diff] [blame] | 480 | if (CodecNamesEq(codec.name, kVp9CodecName)) { |
pbos@webrtc.org | f1c8b90 | 2015-01-14 17:29:27 +0000 | [diff] [blame] | 481 | encoder_settings_.vp9 = webrtc::VideoEncoder::GetDefaultVp9Settings(); |
Erik Språng | 143cec1 | 2015-04-28 10:01:41 +0200 | [diff] [blame] | 482 | encoder_settings_.vp9.denoisingOn = denoising; |
| 483 | encoder_settings_.vp9.frameDroppingOn = frame_dropping; |
pbos@webrtc.org | f1c8b90 | 2015-01-14 17:29:27 +0000 | [diff] [blame] | 484 | return &encoder_settings_.vp9; |
andresp@webrtc.org | 188d3b2 | 2014-11-07 13:21:04 +0000 | [diff] [blame] | 485 | } |
pbos@webrtc.org | 6f48f1b | 2014-07-22 16:29:54 +0000 | [diff] [blame] | 486 | return NULL; |
| 487 | } |
| 488 | |
pbos@webrtc.org | afb554f4 | 2014-08-12 23:17:13 +0000 | [diff] [blame] | 489 | DefaultUnsignalledSsrcHandler::DefaultUnsignalledSsrcHandler() |
| 490 | : default_recv_ssrc_(0), default_renderer_(NULL) {} |
| 491 | |
| 492 | UnsignalledSsrcHandler::Action DefaultUnsignalledSsrcHandler::OnUnsignalledSsrc( |
pbos@webrtc.org | a2a6fe6 | 2015-03-06 15:35:19 +0000 | [diff] [blame] | 493 | WebRtcVideoChannel2* channel, |
pbos@webrtc.org | afb554f4 | 2014-08-12 23:17:13 +0000 | [diff] [blame] | 494 | uint32_t ssrc) { |
| 495 | if (default_recv_ssrc_ != 0) { // Already one default stream. |
| 496 | LOG(LS_WARNING) << "Unknown SSRC, but default receive stream already set."; |
| 497 | return kDropPacket; |
| 498 | } |
| 499 | |
| 500 | StreamParams sp; |
| 501 | sp.ssrcs.push_back(ssrc); |
| 502 | LOG(LS_INFO) << "Creating default receive stream for SSRC=" << ssrc << "."; |
pbos@webrtc.org | a2a6fe6 | 2015-03-06 15:35:19 +0000 | [diff] [blame] | 503 | if (!channel->AddRecvStream(sp, true)) { |
pbos@webrtc.org | afb554f4 | 2014-08-12 23:17:13 +0000 | [diff] [blame] | 504 | LOG(LS_WARNING) << "Could not create default receive stream."; |
| 505 | } |
| 506 | |
| 507 | channel->SetRenderer(ssrc, default_renderer_); |
| 508 | default_recv_ssrc_ = ssrc; |
| 509 | return kDeliverPacket; |
| 510 | } |
| 511 | |
pbos@webrtc.org | 42684be | 2014-10-03 11:25:45 +0000 | [diff] [blame] | 512 | WebRtcCallFactory::~WebRtcCallFactory() { |
| 513 | } |
| 514 | webrtc::Call* WebRtcCallFactory::CreateCall( |
| 515 | const webrtc::Call::Config& config) { |
| 516 | return webrtc::Call::Create(config); |
| 517 | } |
| 518 | |
pbos@webrtc.org | afb554f4 | 2014-08-12 23:17:13 +0000 | [diff] [blame] | 519 | VideoRenderer* DefaultUnsignalledSsrcHandler::GetDefaultRenderer() const { |
| 520 | return default_renderer_; |
| 521 | } |
| 522 | |
| 523 | void DefaultUnsignalledSsrcHandler::SetDefaultRenderer( |
| 524 | VideoMediaChannel* channel, |
| 525 | VideoRenderer* renderer) { |
| 526 | default_renderer_ = renderer; |
| 527 | if (default_recv_ssrc_ != 0) { |
| 528 | channel->SetRenderer(default_recv_ssrc_, default_renderer_); |
| 529 | } |
| 530 | } |
| 531 | |
pbos@webrtc.org | f1f0d9a | 2015-03-02 13:30:15 +0000 | [diff] [blame] | 532 | WebRtcVideoEngine2::WebRtcVideoEngine2(WebRtcVoiceEngine* voice_engine) |
pbos@webrtc.org | b648b9d | 2014-08-26 11:08:06 +0000 | [diff] [blame] | 533 | : worker_thread_(NULL), |
pbos@webrtc.org | f1f0d9a | 2015-03-02 13:30:15 +0000 | [diff] [blame] | 534 | voice_engine_(voice_engine), |
pbos@webrtc.org | b648b9d | 2014-08-26 11:08:06 +0000 | [diff] [blame] | 535 | initialized_(false), |
pbos@webrtc.org | 42684be | 2014-10-03 11:25:45 +0000 | [diff] [blame] | 536 | call_factory_(&default_call_factory_), |
pbos@webrtc.org | 0a2087a | 2014-09-23 09:40:22 +0000 | [diff] [blame] | 537 | external_decoder_factory_(NULL), |
| 538 | external_encoder_factory_(NULL) { |
pbos@webrtc.org | b648b9d | 2014-08-26 11:08:06 +0000 | [diff] [blame] | 539 | LOG(LS_INFO) << "WebRtcVideoEngine2::WebRtcVideoEngine2()"; |
pbos@webrtc.org | 7fe1e03 | 2014-10-14 04:25:33 +0000 | [diff] [blame] | 540 | video_codecs_ = GetSupportedCodecs(); |
pbos@webrtc.org | 587ef60 | 2014-06-16 17:32:02 +0000 | [diff] [blame] | 541 | rtp_header_extensions_.push_back( |
| 542 | RtpHeaderExtension(kRtpTimestampOffsetHeaderExtension, |
| 543 | kRtpTimestampOffsetHeaderExtensionDefaultId)); |
| 544 | rtp_header_extensions_.push_back( |
| 545 | RtpHeaderExtension(kRtpAbsoluteSenderTimeHeaderExtension, |
| 546 | kRtpAbsoluteSenderTimeHeaderExtensionDefaultId)); |
Guo-wei Shieh | 64c1e8c | 2015-04-01 15:33:06 -0700 | [diff] [blame] | 547 | rtp_header_extensions_.push_back( |
| 548 | RtpHeaderExtension(kRtpVideoRotationHeaderExtension, |
| 549 | kRtpVideoRotationHeaderExtensionDefaultId)); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 550 | } |
| 551 | |
| 552 | WebRtcVideoEngine2::~WebRtcVideoEngine2() { |
| 553 | LOG(LS_INFO) << "WebRtcVideoEngine2::~WebRtcVideoEngine2"; |
| 554 | |
| 555 | if (initialized_) { |
| 556 | Terminate(); |
| 557 | } |
| 558 | } |
| 559 | |
pbos@webrtc.org | 42684be | 2014-10-03 11:25:45 +0000 | [diff] [blame] | 560 | void WebRtcVideoEngine2::SetCallFactory(WebRtcCallFactory* call_factory) { |
pbos@webrtc.org | 7fe1e03 | 2014-10-14 04:25:33 +0000 | [diff] [blame] | 561 | assert(!initialized_); |
pbos@webrtc.org | 42684be | 2014-10-03 11:25:45 +0000 | [diff] [blame] | 562 | call_factory_ = call_factory; |
| 563 | } |
| 564 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 565 | bool WebRtcVideoEngine2::Init(rtc::Thread* worker_thread) { |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 566 | LOG(LS_INFO) << "WebRtcVideoEngine2::Init"; |
| 567 | worker_thread_ = worker_thread; |
| 568 | ASSERT(worker_thread_ != NULL); |
| 569 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 570 | initialized_ = true; |
| 571 | return true; |
| 572 | } |
| 573 | |
| 574 | void WebRtcVideoEngine2::Terminate() { |
| 575 | LOG(LS_INFO) << "WebRtcVideoEngine2::Terminate"; |
| 576 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 577 | initialized_ = false; |
| 578 | } |
| 579 | |
| 580 | int WebRtcVideoEngine2::GetCapabilities() { return VIDEO_RECV | VIDEO_SEND; } |
| 581 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 582 | bool WebRtcVideoEngine2::SetDefaultEncoderConfig( |
| 583 | const VideoEncoderConfig& config) { |
pbos@webrtc.org | 8fdeee6 | 2014-07-20 14:40:23 +0000 | [diff] [blame] | 584 | const VideoCodec& codec = config.max_codec; |
pbos@webrtc.org | 957e802 | 2014-11-10 12:36:11 +0000 | [diff] [blame] | 585 | bool supports_codec = false; |
| 586 | for (size_t i = 0; i < video_codecs_.size(); ++i) { |
Shao Changbin | e62202f | 2015-04-21 20:24:50 +0800 | [diff] [blame] | 587 | if (CodecNamesEq(video_codecs_[i].name, codec.name)) { |
pbos@webrtc.org | 2a72c65 | 2015-02-26 16:01:24 +0000 | [diff] [blame] | 588 | video_codecs_[i].width = codec.width; |
| 589 | video_codecs_[i].height = codec.height; |
| 590 | video_codecs_[i].framerate = codec.framerate; |
pbos@webrtc.org | 957e802 | 2014-11-10 12:36:11 +0000 | [diff] [blame] | 591 | supports_codec = true; |
| 592 | break; |
| 593 | } |
| 594 | } |
| 595 | |
| 596 | if (!supports_codec) { |
| 597 | LOG(LS_ERROR) << "SetDefaultEncoderConfig, codec not supported: " |
pbos@webrtc.org | 8fdeee6 | 2014-07-20 14:40:23 +0000 | [diff] [blame] | 598 | << codec.ToString(); |
| 599 | return false; |
| 600 | } |
| 601 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 602 | return true; |
| 603 | } |
| 604 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 605 | WebRtcVideoChannel2* WebRtcVideoEngine2::CreateChannel( |
buildbot@webrtc.org | 1ecbe45 | 2014-10-14 20:29:28 +0000 | [diff] [blame] | 606 | const VideoOptions& options, |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 607 | VoiceMediaChannel* voice_channel) { |
pbos@webrtc.org | 7fe1e03 | 2014-10-14 04:25:33 +0000 | [diff] [blame] | 608 | assert(initialized_); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 609 | LOG(LS_INFO) << "CreateChannel: " |
| 610 | << (voice_channel != NULL ? "With" : "Without") |
pbos@webrtc.org | fa553ef | 2014-10-20 11:07:07 +0000 | [diff] [blame] | 611 | << " voice channel. Options: " << options.ToString(); |
pbos@webrtc.org | 7fe1e03 | 2014-10-14 04:25:33 +0000 | [diff] [blame] | 612 | WebRtcVideoChannel2* channel = |
| 613 | new WebRtcVideoChannel2(call_factory_, |
pbos@webrtc.org | 3bf3d23 | 2014-10-31 12:59:34 +0000 | [diff] [blame] | 614 | voice_engine_, |
pbos@webrtc.org | 7fe1e03 | 2014-10-14 04:25:33 +0000 | [diff] [blame] | 615 | voice_channel, |
pbos@webrtc.org | fa553ef | 2014-10-20 11:07:07 +0000 | [diff] [blame] | 616 | options, |
pbos@webrtc.org | 7fe1e03 | 2014-10-14 04:25:33 +0000 | [diff] [blame] | 617 | external_encoder_factory_, |
pbos@webrtc.org | f1c8b90 | 2015-01-14 17:29:27 +0000 | [diff] [blame] | 618 | external_decoder_factory_); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 619 | if (!channel->Init()) { |
| 620 | delete channel; |
| 621 | return NULL; |
| 622 | } |
pbos@webrtc.org | e322a17 | 2014-06-13 11:47:28 +0000 | [diff] [blame] | 623 | channel->SetRecvCodecs(video_codecs_); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 624 | return channel; |
| 625 | } |
| 626 | |
| 627 | const std::vector<VideoCodec>& WebRtcVideoEngine2::codecs() const { |
| 628 | return video_codecs_; |
| 629 | } |
| 630 | |
| 631 | const std::vector<RtpHeaderExtension>& |
| 632 | WebRtcVideoEngine2::rtp_header_extensions() const { |
| 633 | return rtp_header_extensions_; |
| 634 | } |
| 635 | |
| 636 | void WebRtcVideoEngine2::SetLogging(int min_sev, const char* filter) { |
| 637 | // TODO(pbos): Set up logging. |
| 638 | LOG(LS_VERBOSE) << "SetLogging: " << min_sev << '"' << filter << '"'; |
| 639 | // if min_sev == -1, we keep the current log level. |
| 640 | if (min_sev < 0) { |
| 641 | assert(min_sev == -1); |
| 642 | return; |
| 643 | } |
| 644 | } |
| 645 | |
pbos@webrtc.org | 0a2087a | 2014-09-23 09:40:22 +0000 | [diff] [blame] | 646 | void WebRtcVideoEngine2::SetExternalDecoderFactory( |
| 647 | WebRtcVideoDecoderFactory* decoder_factory) { |
pbos@webrtc.org | 7fe1e03 | 2014-10-14 04:25:33 +0000 | [diff] [blame] | 648 | assert(!initialized_); |
pbos@webrtc.org | 0a2087a | 2014-09-23 09:40:22 +0000 | [diff] [blame] | 649 | external_decoder_factory_ = decoder_factory; |
| 650 | } |
| 651 | |
| 652 | void WebRtcVideoEngine2::SetExternalEncoderFactory( |
| 653 | WebRtcVideoEncoderFactory* encoder_factory) { |
pbos@webrtc.org | 7fe1e03 | 2014-10-14 04:25:33 +0000 | [diff] [blame] | 654 | assert(!initialized_); |
pbos@webrtc.org | f18fba2 | 2015-01-14 16:26:23 +0000 | [diff] [blame] | 655 | if (external_encoder_factory_ == encoder_factory) |
| 656 | return; |
| 657 | |
| 658 | // No matter what happens we shouldn't hold on to a stale |
| 659 | // WebRtcSimulcastEncoderFactory. |
| 660 | simulcast_encoder_factory_.reset(); |
| 661 | |
| 662 | if (encoder_factory && |
| 663 | WebRtcSimulcastEncoderFactory::UseSimulcastEncoderFactory( |
| 664 | encoder_factory->codecs())) { |
| 665 | simulcast_encoder_factory_.reset( |
| 666 | new WebRtcSimulcastEncoderFactory(encoder_factory)); |
| 667 | encoder_factory = simulcast_encoder_factory_.get(); |
| 668 | } |
pbos@webrtc.org | 0a2087a | 2014-09-23 09:40:22 +0000 | [diff] [blame] | 669 | external_encoder_factory_ = encoder_factory; |
pbos@webrtc.org | 7fe1e03 | 2014-10-14 04:25:33 +0000 | [diff] [blame] | 670 | |
| 671 | video_codecs_ = GetSupportedCodecs(); |
pbos@webrtc.org | 0a2087a | 2014-09-23 09:40:22 +0000 | [diff] [blame] | 672 | } |
| 673 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 674 | bool WebRtcVideoEngine2::EnableTimedRender() { |
| 675 | // TODO(pbos): Figure out whether this can be removed. |
| 676 | return true; |
| 677 | } |
| 678 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 679 | // Checks to see whether we comprehend and could receive a particular codec |
| 680 | bool WebRtcVideoEngine2::FindCodec(const VideoCodec& in) { |
| 681 | // TODO(pbos): Probe encoder factory to figure out that the codec is supported |
| 682 | // if supported by the encoder factory. Add a corresponding test that fails |
| 683 | // with this code (that doesn't ask the factory). |
pbos@webrtc.org | 8fdeee6 | 2014-07-20 14:40:23 +0000 | [diff] [blame] | 684 | for (size_t j = 0; j < video_codecs_.size(); ++j) { |
| 685 | VideoCodec codec(video_codecs_[j].id, video_codecs_[j].name, 0, 0, 0, 0); |
| 686 | if (codec.Matches(in)) { |
| 687 | return true; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 688 | } |
| 689 | } |
| 690 | return false; |
| 691 | } |
| 692 | |
| 693 | // Tells whether the |requested| codec can be transmitted or not. If it can be |
| 694 | // transmitted |out| is set with the best settings supported. Aspect ratio will |
| 695 | // be set as close to |current|'s as possible. If not set |requested|'s |
| 696 | // dimensions will be used for aspect ratio matching. |
| 697 | bool WebRtcVideoEngine2::CanSendCodec(const VideoCodec& requested, |
| 698 | const VideoCodec& current, |
| 699 | VideoCodec* out) { |
| 700 | assert(out != NULL); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 701 | |
| 702 | if (requested.width != requested.height && |
| 703 | (requested.height == 0 || requested.width == 0)) { |
| 704 | // 0xn and nx0 are invalid resolutions. |
| 705 | return false; |
| 706 | } |
| 707 | |
| 708 | VideoCodec matching_codec; |
| 709 | if (!FindFirstMatchingCodec(video_codecs_, requested, &matching_codec)) { |
| 710 | // Codec not supported. |
| 711 | return false; |
| 712 | } |
| 713 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 714 | out->id = requested.id; |
| 715 | out->name = requested.name; |
| 716 | out->preference = requested.preference; |
| 717 | out->params = requested.params; |
andresp@webrtc.org | ff689be | 2015-02-12 11:54:26 +0000 | [diff] [blame] | 718 | out->framerate = std::min(requested.framerate, matching_codec.framerate); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 719 | out->params = requested.params; |
| 720 | out->feedback_params = requested.feedback_params; |
pbos@webrtc.org | 8fdeee6 | 2014-07-20 14:40:23 +0000 | [diff] [blame] | 721 | out->width = requested.width; |
| 722 | out->height = requested.height; |
| 723 | if (requested.width == 0 && requested.height == 0) { |
| 724 | return true; |
| 725 | } |
| 726 | |
| 727 | while (out->width > matching_codec.width) { |
| 728 | out->width /= 2; |
| 729 | out->height /= 2; |
| 730 | } |
| 731 | |
| 732 | return out->width > 0 && out->height > 0; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 733 | } |
| 734 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 735 | // Ignore spammy trace messages, mostly from the stats API when we haven't |
| 736 | // gotten RTCP info yet from the remote side. |
| 737 | bool WebRtcVideoEngine2::ShouldIgnoreTrace(const std::string& trace) { |
| 738 | static const char* const kTracesToIgnore[] = {NULL}; |
| 739 | for (const char* const* p = kTracesToIgnore; *p; ++p) { |
| 740 | if (trace.find(*p) == 0) { |
| 741 | return true; |
| 742 | } |
| 743 | } |
| 744 | return false; |
| 745 | } |
| 746 | |
pbos@webrtc.org | 7fe1e03 | 2014-10-14 04:25:33 +0000 | [diff] [blame] | 747 | std::vector<VideoCodec> WebRtcVideoEngine2::GetSupportedCodecs() const { |
andresp@webrtc.org | 82775b1 | 2014-11-07 09:37:54 +0000 | [diff] [blame] | 748 | std::vector<VideoCodec> supported_codecs = DefaultVideoCodecList(); |
pbos@webrtc.org | 7fe1e03 | 2014-10-14 04:25:33 +0000 | [diff] [blame] | 749 | |
| 750 | if (external_encoder_factory_ == NULL) { |
| 751 | return supported_codecs; |
| 752 | } |
| 753 | |
pbos@webrtc.org | 7fe1e03 | 2014-10-14 04:25:33 +0000 | [diff] [blame] | 754 | const std::vector<WebRtcVideoEncoderFactory::VideoCodec>& codecs = |
| 755 | external_encoder_factory_->codecs(); |
| 756 | for (size_t i = 0; i < codecs.size(); ++i) { |
| 757 | // Don't add internally-supported codecs twice. |
| 758 | if (CodecIsInternallySupported(codecs[i].name)) { |
| 759 | continue; |
| 760 | } |
| 761 | |
pkasting@chromium.org | d324546 | 2015-02-23 21:28:22 +0000 | [diff] [blame] | 762 | // External video encoders are given payloads 120-127. This also means that |
| 763 | // we only support up to 8 external payload types. |
| 764 | const int kExternalVideoPayloadTypeBase = 120; |
| 765 | size_t payload_type = kExternalVideoPayloadTypeBase + i; |
| 766 | assert(payload_type < 128); |
| 767 | VideoCodec codec(static_cast<int>(payload_type), |
pbos@webrtc.org | 7fe1e03 | 2014-10-14 04:25:33 +0000 | [diff] [blame] | 768 | codecs[i].name, |
| 769 | codecs[i].max_width, |
| 770 | codecs[i].max_height, |
| 771 | codecs[i].max_fps, |
| 772 | 0); |
| 773 | |
| 774 | AddDefaultFeedbackParams(&codec); |
| 775 | supported_codecs.push_back(codec); |
| 776 | } |
| 777 | return supported_codecs; |
| 778 | } |
| 779 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 780 | WebRtcVideoChannel2::WebRtcVideoChannel2( |
pbos@webrtc.org | 42684be | 2014-10-03 11:25:45 +0000 | [diff] [blame] | 781 | WebRtcCallFactory* call_factory, |
pbos@webrtc.org | 3bf3d23 | 2014-10-31 12:59:34 +0000 | [diff] [blame] | 782 | WebRtcVoiceEngine* voice_engine, |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 783 | VoiceMediaChannel* voice_channel, |
pbos@webrtc.org | fa553ef | 2014-10-20 11:07:07 +0000 | [diff] [blame] | 784 | const VideoOptions& options, |
pbos@webrtc.org | 7fe1e03 | 2014-10-14 04:25:33 +0000 | [diff] [blame] | 785 | WebRtcVideoEncoderFactory* external_encoder_factory, |
pbos@webrtc.org | f1c8b90 | 2015-01-14 17:29:27 +0000 | [diff] [blame] | 786 | WebRtcVideoDecoderFactory* external_decoder_factory) |
pbos@webrtc.org | b648b9d | 2014-08-26 11:08:06 +0000 | [diff] [blame] | 787 | : unsignalled_ssrc_handler_(&default_unsignalled_ssrc_handler_), |
pbos@webrtc.org | 8296ec5 | 2015-03-20 14:27:49 +0000 | [diff] [blame] | 788 | voice_channel_id_(voice_channel != nullptr |
| 789 | ? static_cast<WebRtcVoiceMediaChannel*>( |
| 790 | voice_channel)->voe_channel() |
| 791 | : -1), |
pbos@webrtc.org | 7fe1e03 | 2014-10-14 04:25:33 +0000 | [diff] [blame] | 792 | external_encoder_factory_(external_encoder_factory), |
pbos@webrtc.org | f1c8b90 | 2015-01-14 17:29:27 +0000 | [diff] [blame] | 793 | external_decoder_factory_(external_decoder_factory) { |
pbos@webrtc.org | fa553ef | 2014-10-20 11:07:07 +0000 | [diff] [blame] | 794 | SetDefaultOptions(); |
| 795 | options_.SetAll(options); |
Peter Boström | e7b221f | 2015-04-13 15:34:32 +0200 | [diff] [blame] | 796 | options_.cpu_overuse_detection.Get(&signal_cpu_adaptation_); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 797 | webrtc::Call::Config config(this); |
pbos@webrtc.org | 42684be | 2014-10-03 11:25:45 +0000 | [diff] [blame] | 798 | config.overuse_callback = this; |
pbos@webrtc.org | 3bf3d23 | 2014-10-31 12:59:34 +0000 | [diff] [blame] | 799 | if (voice_engine != NULL) { |
| 800 | config.voice_engine = voice_engine->voe()->engine(); |
| 801 | } |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 802 | config.bitrate_config.min_bitrate_bps = kMinBandwidthBps; |
| 803 | config.bitrate_config.start_bitrate_bps = kStartBandwidthBps; |
| 804 | config.bitrate_config.max_bitrate_bps = kMaxBandwidthBps; |
pbos@webrtc.org | 42684be | 2014-10-03 11:25:45 +0000 | [diff] [blame] | 805 | call_.reset(call_factory->CreateCall(config)); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 806 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 807 | rtcp_receiver_report_ssrc_ = kDefaultRtcpReceiverReportSsrc; |
| 808 | sending_ = false; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 809 | default_send_ssrc_ = 0; |
pbos@webrtc.org | 6f48f1b | 2014-07-22 16:29:54 +0000 | [diff] [blame] | 810 | } |
| 811 | |
| 812 | void WebRtcVideoChannel2::SetDefaultOptions() { |
Peter Boström | e432800 | 2015-04-14 22:45:29 +0200 | [diff] [blame] | 813 | options_.cpu_overuse_detection.Set(true); |
pbos@webrtc.org | d819803 | 2014-11-10 14:41:43 +0000 | [diff] [blame] | 814 | options_.dscp.Set(false); |
pbos@webrtc.org | 5ff71ab | 2014-07-23 07:28:56 +0000 | [diff] [blame] | 815 | options_.suspend_below_min_bitrate.Set(false); |
pbos@webrtc.org | 42684be | 2014-10-03 11:25:45 +0000 | [diff] [blame] | 816 | options_.video_noise_reduction.Set(true); |
pbos@webrtc.org | efc82c2 | 2014-10-27 13:58:00 +0000 | [diff] [blame] | 817 | options_.screencast_min_bitrate.Set(0); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 818 | } |
| 819 | |
| 820 | WebRtcVideoChannel2::~WebRtcVideoChannel2() { |
Peter Boström | dfd53fe | 2015-03-27 15:58:11 +0100 | [diff] [blame] | 821 | for (auto& kv : send_streams_) |
| 822 | delete kv.second; |
| 823 | for (auto& kv : receive_streams_) |
| 824 | delete kv.second; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 825 | } |
| 826 | |
| 827 | bool WebRtcVideoChannel2::Init() { return true; } |
| 828 | |
pbos@webrtc.org | 96a9325 | 2014-11-03 14:46:44 +0000 | [diff] [blame] | 829 | bool WebRtcVideoChannel2::CodecIsExternallySupported( |
| 830 | const std::string& name) const { |
| 831 | if (external_encoder_factory_ == NULL) { |
| 832 | return false; |
| 833 | } |
| 834 | |
| 835 | const std::vector<WebRtcVideoEncoderFactory::VideoCodec> external_codecs = |
| 836 | external_encoder_factory_->codecs(); |
| 837 | for (size_t c = 0; c < external_codecs.size(); ++c) { |
Shao Changbin | e62202f | 2015-04-21 20:24:50 +0800 | [diff] [blame] | 838 | if (CodecNamesEq(name, external_codecs[c].name)) { |
pbos@webrtc.org | 96a9325 | 2014-11-03 14:46:44 +0000 | [diff] [blame] | 839 | return true; |
| 840 | } |
| 841 | } |
| 842 | return false; |
| 843 | } |
| 844 | |
| 845 | std::vector<WebRtcVideoChannel2::VideoCodecSettings> |
| 846 | WebRtcVideoChannel2::FilterSupportedCodecs( |
| 847 | const std::vector<WebRtcVideoChannel2::VideoCodecSettings>& mapped_codecs) |
| 848 | const { |
| 849 | std::vector<VideoCodecSettings> supported_codecs; |
| 850 | for (size_t i = 0; i < mapped_codecs.size(); ++i) { |
| 851 | const VideoCodecSettings& codec = mapped_codecs[i]; |
| 852 | if (CodecIsInternallySupported(codec.codec.name) || |
| 853 | CodecIsExternallySupported(codec.codec.name)) { |
| 854 | supported_codecs.push_back(codec); |
| 855 | } |
| 856 | } |
| 857 | return supported_codecs; |
| 858 | } |
| 859 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 860 | bool WebRtcVideoChannel2::SetRecvCodecs(const std::vector<VideoCodec>& codecs) { |
pbos@webrtc.org | 50fe359 | 2015-01-29 12:33:07 +0000 | [diff] [blame] | 861 | TRACE_EVENT0("webrtc", "WebRtcVideoChannel2::SetRecvCodecs"); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 862 | LOG(LS_INFO) << "SetRecvCodecs: " << CodecVectorToString(codecs); |
| 863 | if (!ValidateCodecFormats(codecs)) { |
| 864 | return false; |
| 865 | } |
| 866 | |
| 867 | const std::vector<VideoCodecSettings> mapped_codecs = MapCodecs(codecs); |
| 868 | if (mapped_codecs.empty()) { |
pbos@webrtc.org | 96a9325 | 2014-11-03 14:46:44 +0000 | [diff] [blame] | 869 | LOG(LS_ERROR) << "SetRecvCodecs called without any video codecs."; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 870 | return false; |
| 871 | } |
| 872 | |
pbos@webrtc.org | 96a9325 | 2014-11-03 14:46:44 +0000 | [diff] [blame] | 873 | const std::vector<VideoCodecSettings> supported_codecs = |
| 874 | FilterSupportedCodecs(mapped_codecs); |
| 875 | |
| 876 | if (mapped_codecs.size() != supported_codecs.size()) { |
| 877 | LOG(LS_ERROR) << "SetRecvCodecs called with unsupported video codecs."; |
| 878 | return false; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 879 | } |
| 880 | |
Peter Boström | ee0b00e | 2015-04-22 18:41:14 +0200 | [diff] [blame] | 881 | // Prevent reconfiguration when setting identical receive codecs. |
| 882 | if (recv_codecs_.size() == supported_codecs.size()) { |
| 883 | bool reconfigured = false; |
| 884 | for (size_t i = 0; i < supported_codecs.size(); ++i) { |
| 885 | if (recv_codecs_[i] != supported_codecs[i]) { |
| 886 | reconfigured = true; |
| 887 | break; |
| 888 | } |
| 889 | } |
| 890 | if (!reconfigured) |
| 891 | return true; |
| 892 | } |
| 893 | |
pbos@webrtc.org | 96a9325 | 2014-11-03 14:46:44 +0000 | [diff] [blame] | 894 | recv_codecs_ = supported_codecs; |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 895 | |
pbos@webrtc.org | 575d126 | 2014-10-08 14:48:08 +0000 | [diff] [blame] | 896 | rtc::CritScope stream_lock(&stream_crit_); |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 897 | for (std::map<uint32, WebRtcVideoReceiveStream*>::iterator it = |
| 898 | receive_streams_.begin(); |
| 899 | it != receive_streams_.end(); |
| 900 | ++it) { |
| 901 | it->second->SetRecvCodecs(recv_codecs_); |
| 902 | } |
| 903 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 904 | return true; |
| 905 | } |
| 906 | |
| 907 | bool WebRtcVideoChannel2::SetSendCodecs(const std::vector<VideoCodec>& codecs) { |
pbos@webrtc.org | 50fe359 | 2015-01-29 12:33:07 +0000 | [diff] [blame] | 908 | TRACE_EVENT0("webrtc", "WebRtcVideoChannel2::SetSendCodecs"); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 909 | LOG(LS_INFO) << "SetSendCodecs: " << CodecVectorToString(codecs); |
| 910 | if (!ValidateCodecFormats(codecs)) { |
| 911 | return false; |
| 912 | } |
| 913 | |
| 914 | const std::vector<VideoCodecSettings> supported_codecs = |
| 915 | FilterSupportedCodecs(MapCodecs(codecs)); |
| 916 | |
| 917 | if (supported_codecs.empty()) { |
Peter Boström | 3c3f646 | 2015-04-15 16:27:49 +0200 | [diff] [blame] | 918 | LOG(LS_ERROR) << "No video codecs supported."; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 919 | return false; |
| 920 | } |
| 921 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 922 | LOG(LS_INFO) << "Using codec: " << supported_codecs.front().codec.ToString(); |
| 923 | |
pbos@webrtc.org | a2ef4fe | 2014-11-07 10:54:43 +0000 | [diff] [blame] | 924 | VideoCodecSettings old_codec; |
| 925 | if (send_codec_.Get(&old_codec) && supported_codecs.front() == old_codec) { |
| 926 | // Using same codec, avoid reconfiguring. |
| 927 | return true; |
| 928 | } |
| 929 | |
| 930 | send_codec_.Set(supported_codecs.front()); |
| 931 | |
pbos@webrtc.org | 575d126 | 2014-10-08 14:48:08 +0000 | [diff] [blame] | 932 | rtc::CritScope stream_lock(&stream_crit_); |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 933 | for (std::map<uint32, WebRtcVideoSendStream*>::iterator it = |
| 934 | send_streams_.begin(); |
| 935 | it != send_streams_.end(); |
| 936 | ++it) { |
| 937 | assert(it->second != NULL); |
| 938 | it->second->SetCodec(supported_codecs.front()); |
| 939 | } |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 940 | |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 941 | // TODO(holmer): Changing the codec parameters shouldn't necessarily mean that |
| 942 | // we change the min/max of bandwidth estimation. Reevaluate this. |
pbos@webrtc.org | 0087318 | 2014-11-25 14:03:34 +0000 | [diff] [blame] | 943 | VideoCodec codec = supported_codecs.front().codec; |
| 944 | int bitrate_kbps; |
| 945 | if (codec.GetParam(kCodecParamMinBitrate, &bitrate_kbps) && |
| 946 | bitrate_kbps > 0) { |
| 947 | bitrate_config_.min_bitrate_bps = bitrate_kbps * 1000; |
| 948 | } else { |
| 949 | bitrate_config_.min_bitrate_bps = 0; |
| 950 | } |
| 951 | if (codec.GetParam(kCodecParamStartBitrate, &bitrate_kbps) && |
| 952 | bitrate_kbps > 0) { |
| 953 | bitrate_config_.start_bitrate_bps = bitrate_kbps * 1000; |
| 954 | } else { |
| 955 | // Do not reconfigure start bitrate unless it's specified and positive. |
| 956 | bitrate_config_.start_bitrate_bps = -1; |
| 957 | } |
| 958 | if (codec.GetParam(kCodecParamMaxBitrate, &bitrate_kbps) && |
| 959 | bitrate_kbps > 0) { |
| 960 | bitrate_config_.max_bitrate_bps = bitrate_kbps * 1000; |
| 961 | } else { |
| 962 | bitrate_config_.max_bitrate_bps = -1; |
| 963 | } |
| 964 | call_->SetBitrateConfig(bitrate_config_); |
| 965 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 966 | return true; |
| 967 | } |
| 968 | |
| 969 | bool WebRtcVideoChannel2::GetSendCodec(VideoCodec* codec) { |
| 970 | VideoCodecSettings codec_settings; |
| 971 | if (!send_codec_.Get(&codec_settings)) { |
| 972 | LOG(LS_VERBOSE) << "GetSendCodec: No send codec set."; |
| 973 | return false; |
| 974 | } |
| 975 | *codec = codec_settings.codec; |
| 976 | return true; |
| 977 | } |
| 978 | |
| 979 | bool WebRtcVideoChannel2::SetSendStreamFormat(uint32 ssrc, |
| 980 | const VideoFormat& format) { |
| 981 | LOG(LS_VERBOSE) << "SetSendStreamFormat:" << ssrc << " -> " |
| 982 | << format.ToString(); |
pbos@webrtc.org | 575d126 | 2014-10-08 14:48:08 +0000 | [diff] [blame] | 983 | rtc::CritScope stream_lock(&stream_crit_); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 984 | if (send_streams_.find(ssrc) == send_streams_.end()) { |
| 985 | return false; |
| 986 | } |
| 987 | return send_streams_[ssrc]->SetVideoFormat(format); |
| 988 | } |
| 989 | |
| 990 | bool WebRtcVideoChannel2::SetRender(bool render) { |
| 991 | // TODO(pbos): Implement. Or refactor away as it shouldn't be needed. |
| 992 | LOG(LS_VERBOSE) << "SetRender: " << (render ? "true" : "false"); |
| 993 | return true; |
| 994 | } |
| 995 | |
| 996 | bool WebRtcVideoChannel2::SetSend(bool send) { |
| 997 | LOG(LS_VERBOSE) << "SetSend: " << (send ? "true" : "false"); |
| 998 | if (send && !send_codec_.IsSet()) { |
| 999 | LOG(LS_ERROR) << "SetSend(true) called before setting codec."; |
| 1000 | return false; |
| 1001 | } |
| 1002 | if (send) { |
| 1003 | StartAllSendStreams(); |
| 1004 | } else { |
| 1005 | StopAllSendStreams(); |
| 1006 | } |
| 1007 | sending_ = send; |
| 1008 | return true; |
| 1009 | } |
| 1010 | |
Peter Boström | d6f4c25 | 2015-03-26 16:23:04 +0100 | [diff] [blame] | 1011 | bool WebRtcVideoChannel2::ValidateSendSsrcAvailability( |
| 1012 | const StreamParams& sp) const { |
| 1013 | for (uint32_t ssrc: sp.ssrcs) { |
| 1014 | if (send_ssrcs_.find(ssrc) != send_ssrcs_.end()) { |
| 1015 | LOG(LS_ERROR) << "Send stream with SSRC '" << ssrc << "' already exists."; |
| 1016 | return false; |
| 1017 | } |
| 1018 | } |
| 1019 | return true; |
| 1020 | } |
| 1021 | |
| 1022 | bool WebRtcVideoChannel2::ValidateReceiveSsrcAvailability( |
| 1023 | const StreamParams& sp) const { |
| 1024 | for (uint32_t ssrc: sp.ssrcs) { |
| 1025 | if (receive_ssrcs_.find(ssrc) != receive_ssrcs_.end()) { |
| 1026 | LOG(LS_ERROR) << "Receive stream with SSRC '" << ssrc |
| 1027 | << "' already exists."; |
| 1028 | return false; |
| 1029 | } |
| 1030 | } |
| 1031 | return true; |
| 1032 | } |
| 1033 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1034 | bool WebRtcVideoChannel2::AddSendStream(const StreamParams& sp) { |
| 1035 | LOG(LS_INFO) << "AddSendStream: " << sp.ToString(); |
Peter Boström | d4362cd | 2015-03-25 14:17:23 +0100 | [diff] [blame] | 1036 | if (!ValidateStreamParams(sp)) |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1037 | return false; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1038 | |
pbos@webrtc.org | 575d126 | 2014-10-08 14:48:08 +0000 | [diff] [blame] | 1039 | rtc::CritScope stream_lock(&stream_crit_); |
Peter Boström | d6f4c25 | 2015-03-26 16:23:04 +0100 | [diff] [blame] | 1040 | |
| 1041 | if (!ValidateSendSsrcAvailability(sp)) |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1042 | return false; |
Peter Boström | d6f4c25 | 2015-03-26 16:23:04 +0100 | [diff] [blame] | 1043 | |
| 1044 | for (uint32 used_ssrc : sp.ssrcs) |
| 1045 | send_ssrcs_.insert(used_ssrc); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1046 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1047 | WebRtcVideoSendStream* stream = |
pbos@webrtc.org | 6ae48c6 | 2014-06-06 10:49:19 +0000 | [diff] [blame] | 1048 | new WebRtcVideoSendStream(call_.get(), |
pbos@webrtc.org | 7fe1e03 | 2014-10-14 04:25:33 +0000 | [diff] [blame] | 1049 | external_encoder_factory_, |
pbos@webrtc.org | 6ae48c6 | 2014-06-06 10:49:19 +0000 | [diff] [blame] | 1050 | options_, |
Peter Boström | dfd53fe | 2015-03-27 15:58:11 +0100 | [diff] [blame] | 1051 | bitrate_config_.max_bitrate_bps, |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1052 | send_codec_, |
| 1053 | sp, |
| 1054 | send_rtp_extensions_); |
| 1055 | |
Peter Boström | d6f4c25 | 2015-03-26 16:23:04 +0100 | [diff] [blame] | 1056 | uint32 ssrc = sp.first_ssrc(); |
| 1057 | assert(ssrc != 0); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1058 | send_streams_[ssrc] = stream; |
| 1059 | |
| 1060 | if (rtcp_receiver_report_ssrc_ == kDefaultRtcpReceiverReportSsrc) { |
| 1061 | rtcp_receiver_report_ssrc_ = ssrc; |
| 1062 | } |
| 1063 | if (default_send_ssrc_ == 0) { |
| 1064 | default_send_ssrc_ = ssrc; |
| 1065 | } |
| 1066 | if (sending_) { |
| 1067 | stream->Start(); |
| 1068 | } |
| 1069 | |
| 1070 | return true; |
| 1071 | } |
| 1072 | |
| 1073 | bool WebRtcVideoChannel2::RemoveSendStream(uint32 ssrc) { |
| 1074 | LOG(LS_INFO) << "RemoveSendStream: " << ssrc; |
| 1075 | |
| 1076 | if (ssrc == 0) { |
| 1077 | if (default_send_ssrc_ == 0) { |
| 1078 | LOG(LS_ERROR) << "No default send stream active."; |
| 1079 | return false; |
| 1080 | } |
| 1081 | |
| 1082 | LOG(LS_VERBOSE) << "Removing default stream: " << default_send_ssrc_; |
| 1083 | ssrc = default_send_ssrc_; |
| 1084 | } |
| 1085 | |
pbos@webrtc.org | 575d126 | 2014-10-08 14:48:08 +0000 | [diff] [blame] | 1086 | WebRtcVideoSendStream* removed_stream; |
| 1087 | { |
| 1088 | rtc::CritScope stream_lock(&stream_crit_); |
| 1089 | std::map<uint32, WebRtcVideoSendStream*>::iterator it = |
| 1090 | send_streams_.find(ssrc); |
| 1091 | if (it == send_streams_.end()) { |
| 1092 | return false; |
| 1093 | } |
| 1094 | |
Peter Boström | d6f4c25 | 2015-03-26 16:23:04 +0100 | [diff] [blame] | 1095 | for (uint32 old_ssrc : it->second->GetSsrcs()) |
| 1096 | send_ssrcs_.erase(old_ssrc); |
| 1097 | |
pbos@webrtc.org | 575d126 | 2014-10-08 14:48:08 +0000 | [diff] [blame] | 1098 | removed_stream = it->second; |
| 1099 | send_streams_.erase(it); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1100 | } |
| 1101 | |
pbos@webrtc.org | 575d126 | 2014-10-08 14:48:08 +0000 | [diff] [blame] | 1102 | delete removed_stream; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1103 | |
| 1104 | if (ssrc == default_send_ssrc_) { |
| 1105 | default_send_ssrc_ = 0; |
| 1106 | } |
| 1107 | |
| 1108 | return true; |
| 1109 | } |
| 1110 | |
Peter Boström | d6f4c25 | 2015-03-26 16:23:04 +0100 | [diff] [blame] | 1111 | void WebRtcVideoChannel2::DeleteReceiveStream( |
| 1112 | WebRtcVideoChannel2::WebRtcVideoReceiveStream* stream) { |
| 1113 | for (uint32 old_ssrc : stream->GetSsrcs()) |
| 1114 | receive_ssrcs_.erase(old_ssrc); |
| 1115 | delete stream; |
| 1116 | } |
| 1117 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1118 | bool WebRtcVideoChannel2::AddRecvStream(const StreamParams& sp) { |
pbos@webrtc.org | a2a6fe6 | 2015-03-06 15:35:19 +0000 | [diff] [blame] | 1119 | return AddRecvStream(sp, false); |
| 1120 | } |
| 1121 | |
| 1122 | bool WebRtcVideoChannel2::AddRecvStream(const StreamParams& sp, |
| 1123 | bool default_stream) { |
Peter Boström | d4362cd | 2015-03-25 14:17:23 +0100 | [diff] [blame] | 1124 | LOG(LS_INFO) << "AddRecvStream" << (default_stream ? " (default stream)" : "") |
| 1125 | << ": " << sp.ToString(); |
| 1126 | if (!ValidateStreamParams(sp)) |
| 1127 | return false; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1128 | |
| 1129 | uint32 ssrc = sp.first_ssrc(); |
| 1130 | assert(ssrc != 0); // TODO(pbos): Is this ever valid? |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1131 | |
pbos@webrtc.org | 575d126 | 2014-10-08 14:48:08 +0000 | [diff] [blame] | 1132 | rtc::CritScope stream_lock(&stream_crit_); |
Peter Boström | d6f4c25 | 2015-03-26 16:23:04 +0100 | [diff] [blame] | 1133 | // Remove running stream if this was a default stream. |
| 1134 | auto prev_stream = receive_streams_.find(ssrc); |
| 1135 | if (prev_stream != receive_streams_.end()) { |
| 1136 | if (default_stream || !prev_stream->second->IsDefaultStream()) { |
| 1137 | LOG(LS_ERROR) << "Receive stream for SSRC '" << ssrc |
| 1138 | << "' already exists."; |
| 1139 | return false; |
pbos@webrtc.org | a2a6fe6 | 2015-03-06 15:35:19 +0000 | [diff] [blame] | 1140 | } |
Peter Boström | d6f4c25 | 2015-03-26 16:23:04 +0100 | [diff] [blame] | 1141 | DeleteReceiveStream(prev_stream->second); |
| 1142 | receive_streams_.erase(prev_stream); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1143 | } |
| 1144 | |
Peter Boström | d6f4c25 | 2015-03-26 16:23:04 +0100 | [diff] [blame] | 1145 | if (!ValidateReceiveSsrcAvailability(sp)) |
| 1146 | return false; |
| 1147 | |
| 1148 | for (uint32 used_ssrc : sp.ssrcs) |
| 1149 | receive_ssrcs_.insert(used_ssrc); |
| 1150 | |
pbos@webrtc.org | bd249bc | 2014-07-07 04:45:15 +0000 | [diff] [blame] | 1151 | webrtc::VideoReceiveStream::Config config; |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 1152 | ConfigureReceiverRtp(&config, sp); |
pbos@webrtc.org | 3bf3d23 | 2014-10-31 12:59:34 +0000 | [diff] [blame] | 1153 | |
| 1154 | // Set up A/V sync if there is a VoiceChannel. |
| 1155 | // TODO(pbos): The A/V is synched by the receiving channel. So we need to know |
| 1156 | // the SSRC of the remote audio channel in order to sync the correct webrtc |
| 1157 | // VoiceEngine channel. For now sync the first channel in non-conference to |
| 1158 | // match existing behavior in WebRtcVideoEngine. |
pbos@webrtc.org | 8296ec5 | 2015-03-20 14:27:49 +0000 | [diff] [blame] | 1159 | if (voice_channel_id_ != -1 && receive_streams_.empty() && |
pbos@webrtc.org | 3bf3d23 | 2014-10-31 12:59:34 +0000 | [diff] [blame] | 1160 | !options_.conference_mode.GetWithDefaultIfUnset(false)) { |
pbos@webrtc.org | 8296ec5 | 2015-03-20 14:27:49 +0000 | [diff] [blame] | 1161 | config.audio_channel_id = voice_channel_id_; |
pbos@webrtc.org | 3bf3d23 | 2014-10-31 12:59:34 +0000 | [diff] [blame] | 1162 | } |
| 1163 | |
Peter Boström | d6f4c25 | 2015-03-26 16:23:04 +0100 | [diff] [blame] | 1164 | receive_streams_[ssrc] = new WebRtcVideoReceiveStream( |
| 1165 | call_.get(), sp.ssrcs, external_decoder_factory_, default_stream, config, |
| 1166 | recv_codecs_); |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 1167 | |
| 1168 | return true; |
| 1169 | } |
| 1170 | |
| 1171 | void WebRtcVideoChannel2::ConfigureReceiverRtp( |
| 1172 | webrtc::VideoReceiveStream::Config* config, |
| 1173 | const StreamParams& sp) const { |
| 1174 | uint32 ssrc = sp.first_ssrc(); |
| 1175 | |
| 1176 | config->rtp.remote_ssrc = ssrc; |
| 1177 | config->rtp.local_ssrc = rtcp_receiver_report_ssrc_; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1178 | |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 1179 | config->rtp.extensions = recv_rtp_extensions_; |
pbos@webrtc.org | 257e130 | 2014-07-25 19:01:32 +0000 | [diff] [blame] | 1180 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1181 | // TODO(pbos): This protection is against setting the same local ssrc as |
| 1182 | // remote which is not permitted by the lower-level API. RTCP requires a |
| 1183 | // corresponding sender SSRC. Figure out what to do when we don't have |
| 1184 | // (receive-only) or know a good local SSRC. |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 1185 | if (config->rtp.remote_ssrc == config->rtp.local_ssrc) { |
| 1186 | if (config->rtp.local_ssrc != kDefaultRtcpReceiverReportSsrc) { |
| 1187 | config->rtp.local_ssrc = kDefaultRtcpReceiverReportSsrc; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1188 | } else { |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 1189 | config->rtp.local_ssrc = kDefaultRtcpReceiverReportSsrc + 1; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1190 | } |
| 1191 | } |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 1192 | |
| 1193 | for (size_t i = 0; i < recv_codecs_.size(); ++i) { |
andresp@webrtc.org | 82775b1 | 2014-11-07 09:37:54 +0000 | [diff] [blame] | 1194 | MergeFecConfig(recv_codecs_[i].fec, &config->rtp.fec); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1195 | } |
| 1196 | |
andresp@webrtc.org | 82775b1 | 2014-11-07 09:37:54 +0000 | [diff] [blame] | 1197 | for (size_t i = 0; i < recv_codecs_.size(); ++i) { |
| 1198 | uint32 rtx_ssrc; |
| 1199 | if (recv_codecs_[i].rtx_payload_type != -1 && |
| 1200 | sp.GetFidSsrc(ssrc, &rtx_ssrc)) { |
| 1201 | webrtc::VideoReceiveStream::Config::Rtp::Rtx& rtx = |
| 1202 | config->rtp.rtx[recv_codecs_[i].codec.id]; |
| 1203 | rtx.ssrc = rtx_ssrc; |
| 1204 | rtx.payload_type = recv_codecs_[i].rtx_payload_type; |
| 1205 | } |
| 1206 | } |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1207 | } |
| 1208 | |
| 1209 | bool WebRtcVideoChannel2::RemoveRecvStream(uint32 ssrc) { |
| 1210 | LOG(LS_INFO) << "RemoveRecvStream: " << ssrc; |
| 1211 | if (ssrc == 0) { |
pbos@webrtc.org | afb554f4 | 2014-08-12 23:17:13 +0000 | [diff] [blame] | 1212 | LOG(LS_ERROR) << "RemoveRecvStream with 0 ssrc is not supported."; |
| 1213 | return false; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1214 | } |
| 1215 | |
pbos@webrtc.org | 575d126 | 2014-10-08 14:48:08 +0000 | [diff] [blame] | 1216 | rtc::CritScope stream_lock(&stream_crit_); |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 1217 | std::map<uint32, WebRtcVideoReceiveStream*>::iterator stream = |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1218 | receive_streams_.find(ssrc); |
| 1219 | if (stream == receive_streams_.end()) { |
| 1220 | LOG(LS_ERROR) << "Stream not found for ssrc: " << ssrc; |
| 1221 | return false; |
| 1222 | } |
Peter Boström | d6f4c25 | 2015-03-26 16:23:04 +0100 | [diff] [blame] | 1223 | DeleteReceiveStream(stream->second); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1224 | receive_streams_.erase(stream); |
| 1225 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1226 | return true; |
| 1227 | } |
| 1228 | |
| 1229 | bool WebRtcVideoChannel2::SetRenderer(uint32 ssrc, VideoRenderer* renderer) { |
| 1230 | LOG(LS_INFO) << "SetRenderer: ssrc:" << ssrc << " " |
| 1231 | << (renderer ? "(ptr)" : "NULL"); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1232 | if (ssrc == 0) { |
pbos@webrtc.org | afb554f4 | 2014-08-12 23:17:13 +0000 | [diff] [blame] | 1233 | default_unsignalled_ssrc_handler_.SetDefaultRenderer(this, renderer); |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 1234 | return true; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1235 | } |
| 1236 | |
pbos@webrtc.org | 575d126 | 2014-10-08 14:48:08 +0000 | [diff] [blame] | 1237 | rtc::CritScope stream_lock(&stream_crit_); |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 1238 | std::map<uint32, WebRtcVideoReceiveStream*>::iterator it = |
| 1239 | receive_streams_.find(ssrc); |
| 1240 | if (it == receive_streams_.end()) { |
| 1241 | return false; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1242 | } |
| 1243 | |
| 1244 | it->second->SetRenderer(renderer); |
| 1245 | return true; |
| 1246 | } |
| 1247 | |
| 1248 | bool WebRtcVideoChannel2::GetRenderer(uint32 ssrc, VideoRenderer** renderer) { |
| 1249 | if (ssrc == 0) { |
pbos@webrtc.org | afb554f4 | 2014-08-12 23:17:13 +0000 | [diff] [blame] | 1250 | *renderer = default_unsignalled_ssrc_handler_.GetDefaultRenderer(); |
| 1251 | return *renderer != NULL; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1252 | } |
| 1253 | |
pbos@webrtc.org | 575d126 | 2014-10-08 14:48:08 +0000 | [diff] [blame] | 1254 | rtc::CritScope stream_lock(&stream_crit_); |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 1255 | std::map<uint32, WebRtcVideoReceiveStream*>::iterator it = |
| 1256 | receive_streams_.find(ssrc); |
| 1257 | if (it == receive_streams_.end()) { |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1258 | return false; |
| 1259 | } |
| 1260 | *renderer = it->second->GetRenderer(); |
| 1261 | return true; |
| 1262 | } |
| 1263 | |
pbos@webrtc.org | 058b1f1 | 2015-03-04 08:54:32 +0000 | [diff] [blame] | 1264 | bool WebRtcVideoChannel2::GetStats(VideoMediaInfo* info) { |
pbos@webrtc.org | e6f84ae | 2014-07-18 11:11:55 +0000 | [diff] [blame] | 1265 | info->Clear(); |
| 1266 | FillSenderStats(info); |
| 1267 | FillReceiverStats(info); |
pbos@webrtc.org | 2b19f06 | 2014-12-11 13:26:09 +0000 | [diff] [blame] | 1268 | webrtc::Call::Stats stats = call_->GetStats(); |
| 1269 | FillBandwidthEstimationStats(stats, info); |
| 1270 | if (stats.rtt_ms != -1) { |
| 1271 | for (size_t i = 0; i < info->senders.size(); ++i) { |
| 1272 | info->senders[i].rtt_ms = stats.rtt_ms; |
| 1273 | } |
| 1274 | } |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1275 | return true; |
| 1276 | } |
| 1277 | |
pbos@webrtc.org | e6f84ae | 2014-07-18 11:11:55 +0000 | [diff] [blame] | 1278 | void WebRtcVideoChannel2::FillSenderStats(VideoMediaInfo* video_media_info) { |
pbos@webrtc.org | 575d126 | 2014-10-08 14:48:08 +0000 | [diff] [blame] | 1279 | rtc::CritScope stream_lock(&stream_crit_); |
pbos@webrtc.org | e6f84ae | 2014-07-18 11:11:55 +0000 | [diff] [blame] | 1280 | for (std::map<uint32, WebRtcVideoSendStream*>::iterator it = |
| 1281 | send_streams_.begin(); |
| 1282 | it != send_streams_.end(); |
| 1283 | ++it) { |
| 1284 | video_media_info->senders.push_back(it->second->GetVideoSenderInfo()); |
| 1285 | } |
| 1286 | } |
| 1287 | |
| 1288 | void WebRtcVideoChannel2::FillReceiverStats(VideoMediaInfo* video_media_info) { |
pbos@webrtc.org | 575d126 | 2014-10-08 14:48:08 +0000 | [diff] [blame] | 1289 | rtc::CritScope stream_lock(&stream_crit_); |
pbos@webrtc.org | e6f84ae | 2014-07-18 11:11:55 +0000 | [diff] [blame] | 1290 | for (std::map<uint32, WebRtcVideoReceiveStream*>::iterator it = |
| 1291 | receive_streams_.begin(); |
| 1292 | it != receive_streams_.end(); |
| 1293 | ++it) { |
| 1294 | video_media_info->receivers.push_back(it->second->GetVideoReceiverInfo()); |
| 1295 | } |
| 1296 | } |
| 1297 | |
| 1298 | void WebRtcVideoChannel2::FillBandwidthEstimationStats( |
pbos@webrtc.org | 2b19f06 | 2014-12-11 13:26:09 +0000 | [diff] [blame] | 1299 | const webrtc::Call::Stats& stats, |
pbos@webrtc.org | e6f84ae | 2014-07-18 11:11:55 +0000 | [diff] [blame] | 1300 | VideoMediaInfo* video_media_info) { |
stefan@webrtc.org | 0bae1fa | 2014-11-05 14:05:29 +0000 | [diff] [blame] | 1301 | BandwidthEstimationInfo bwe_info; |
stefan@webrtc.org | 0bae1fa | 2014-11-05 14:05:29 +0000 | [diff] [blame] | 1302 | bwe_info.available_send_bandwidth = stats.send_bandwidth_bps; |
| 1303 | bwe_info.available_recv_bandwidth = stats.recv_bandwidth_bps; |
| 1304 | bwe_info.bucket_delay = stats.pacer_delay_ms; |
| 1305 | |
| 1306 | // Get send stream bitrate stats. |
| 1307 | rtc::CritScope stream_lock(&stream_crit_); |
| 1308 | for (std::map<uint32, WebRtcVideoSendStream*>::iterator stream = |
| 1309 | send_streams_.begin(); |
| 1310 | stream != send_streams_.end(); |
| 1311 | ++stream) { |
| 1312 | stream->second->FillBandwidthEstimationInfo(&bwe_info); |
| 1313 | } |
| 1314 | video_media_info->bw_estimations.push_back(bwe_info); |
pbos@webrtc.org | e6f84ae | 2014-07-18 11:11:55 +0000 | [diff] [blame] | 1315 | } |
| 1316 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1317 | bool WebRtcVideoChannel2::SetCapturer(uint32 ssrc, VideoCapturer* capturer) { |
| 1318 | LOG(LS_INFO) << "SetCapturer: " << ssrc << " -> " |
| 1319 | << (capturer != NULL ? "(capturer)" : "NULL"); |
| 1320 | assert(ssrc != 0); |
Peter Boström | e7b221f | 2015-04-13 15:34:32 +0200 | [diff] [blame] | 1321 | { |
| 1322 | rtc::CritScope stream_lock(&stream_crit_); |
| 1323 | if (send_streams_.find(ssrc) == send_streams_.end()) { |
| 1324 | LOG(LS_ERROR) << "No sending stream on ssrc " << ssrc; |
| 1325 | return false; |
| 1326 | } |
| 1327 | if (!send_streams_[ssrc]->SetCapturer(capturer)) { |
| 1328 | return false; |
| 1329 | } |
Guo-wei Shieh | 64c1e8c | 2015-04-01 15:33:06 -0700 | [diff] [blame] | 1330 | } |
| 1331 | |
| 1332 | if (capturer) { |
| 1333 | capturer->SetApplyRotation( |
| 1334 | !FindHeaderExtension(send_rtp_extensions_, |
| 1335 | kRtpVideoRotationHeaderExtension)); |
| 1336 | } |
Peter Boström | e7b221f | 2015-04-13 15:34:32 +0200 | [diff] [blame] | 1337 | { |
| 1338 | rtc::CritScope lock(&capturer_crit_); |
| 1339 | capturers_[ssrc] = capturer; |
| 1340 | } |
Guo-wei Shieh | 64c1e8c | 2015-04-01 15:33:06 -0700 | [diff] [blame] | 1341 | return true; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1342 | } |
| 1343 | |
| 1344 | bool WebRtcVideoChannel2::SendIntraFrame() { |
| 1345 | // TODO(pbos): Implement. |
| 1346 | LOG(LS_VERBOSE) << "SendIntraFrame()."; |
| 1347 | return true; |
| 1348 | } |
| 1349 | |
| 1350 | bool WebRtcVideoChannel2::RequestIntraFrame() { |
| 1351 | // TODO(pbos): Implement. |
| 1352 | LOG(LS_VERBOSE) << "SendIntraFrame()."; |
| 1353 | return true; |
| 1354 | } |
| 1355 | |
| 1356 | void WebRtcVideoChannel2::OnPacketReceived( |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1357 | rtc::Buffer* packet, |
| 1358 | const rtc::PacketTime& packet_time) { |
pbos@webrtc.org | 4e545cc | 2014-05-14 13:58:13 +0000 | [diff] [blame] | 1359 | const webrtc::PacketReceiver::DeliveryStatus delivery_result = |
Fredrik Solenberg | 23fba1f | 2015-04-29 15:24:01 +0200 | [diff] [blame] | 1360 | call_->Receiver()->DeliverPacket(webrtc::MediaType::VIDEO, |
kwiberg@webrtc.org | eebcab5 | 2015-03-24 09:19:06 +0000 | [diff] [blame] | 1361 | reinterpret_cast<const uint8_t*>(packet->data()), packet->size()); |
pbos@webrtc.org | 4e545cc | 2014-05-14 13:58:13 +0000 | [diff] [blame] | 1362 | switch (delivery_result) { |
| 1363 | case webrtc::PacketReceiver::DELIVERY_OK: |
| 1364 | return; |
| 1365 | case webrtc::PacketReceiver::DELIVERY_PACKET_ERROR: |
| 1366 | return; |
| 1367 | case webrtc::PacketReceiver::DELIVERY_UNKNOWN_SSRC: |
| 1368 | break; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1369 | } |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1370 | |
| 1371 | uint32 ssrc = 0; |
kwiberg@webrtc.org | eebcab5 | 2015-03-24 09:19:06 +0000 | [diff] [blame] | 1372 | if (!GetRtpSsrc(packet->data(), packet->size(), &ssrc)) { |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1373 | return; |
| 1374 | } |
| 1375 | |
pbos@webrtc.org | a2a6fe6 | 2015-03-06 15:35:19 +0000 | [diff] [blame] | 1376 | // TODO(pbos): Ignore unsignalled packets that don't use the video payload |
| 1377 | // (prevent creating default receivers for RTX configured as if it would |
| 1378 | // receive media payloads on those SSRCs). |
pbos@webrtc.org | afb554f4 | 2014-08-12 23:17:13 +0000 | [diff] [blame] | 1379 | switch (unsignalled_ssrc_handler_->OnUnsignalledSsrc(this, ssrc)) { |
| 1380 | case UnsignalledSsrcHandler::kDropPacket: |
| 1381 | return; |
| 1382 | case UnsignalledSsrcHandler::kDeliverPacket: |
| 1383 | break; |
| 1384 | } |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1385 | |
Fredrik Solenberg | 23fba1f | 2015-04-29 15:24:01 +0200 | [diff] [blame] | 1386 | if (call_->Receiver()->DeliverPacket(webrtc::MediaType::VIDEO, |
kwiberg@webrtc.org | eebcab5 | 2015-03-24 09:19:06 +0000 | [diff] [blame] | 1387 | reinterpret_cast<const uint8_t*>(packet->data()), packet->size()) != |
pbos@webrtc.org | 1e019d1 | 2014-05-16 11:38:45 +0000 | [diff] [blame] | 1388 | webrtc::PacketReceiver::DELIVERY_OK) { |
pbos@webrtc.org | afb554f4 | 2014-08-12 23:17:13 +0000 | [diff] [blame] | 1389 | LOG(LS_WARNING) << "Failed to deliver RTP packet on re-delivery."; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1390 | return; |
| 1391 | } |
| 1392 | } |
| 1393 | |
| 1394 | void WebRtcVideoChannel2::OnRtcpReceived( |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1395 | rtc::Buffer* packet, |
| 1396 | const rtc::PacketTime& packet_time) { |
Fredrik Solenberg | 23fba1f | 2015-04-29 15:24:01 +0200 | [diff] [blame] | 1397 | if (call_->Receiver()->DeliverPacket(webrtc::MediaType::VIDEO, |
kwiberg@webrtc.org | eebcab5 | 2015-03-24 09:19:06 +0000 | [diff] [blame] | 1398 | reinterpret_cast<const uint8_t*>(packet->data()), packet->size()) != |
pbos@webrtc.org | 1e019d1 | 2014-05-16 11:38:45 +0000 | [diff] [blame] | 1399 | webrtc::PacketReceiver::DELIVERY_OK) { |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1400 | LOG(LS_WARNING) << "Failed to deliver RTCP packet."; |
| 1401 | } |
| 1402 | } |
| 1403 | |
| 1404 | void WebRtcVideoChannel2::OnReadyToSend(bool ready) { |
pbos@webrtc.org | 26c0c41 | 2014-09-03 16:17:12 +0000 | [diff] [blame] | 1405 | LOG(LS_VERBOSE) << "OnReadyToSend: " << (ready ? "Ready." : "Not ready."); |
| 1406 | call_->SignalNetworkState(ready ? webrtc::Call::kNetworkUp |
| 1407 | : webrtc::Call::kNetworkDown); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1408 | } |
| 1409 | |
| 1410 | bool WebRtcVideoChannel2::MuteStream(uint32 ssrc, bool mute) { |
| 1411 | LOG(LS_VERBOSE) << "MuteStream: " << ssrc << " -> " |
| 1412 | << (mute ? "mute" : "unmute"); |
| 1413 | assert(ssrc != 0); |
pbos@webrtc.org | 575d126 | 2014-10-08 14:48:08 +0000 | [diff] [blame] | 1414 | rtc::CritScope stream_lock(&stream_crit_); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1415 | if (send_streams_.find(ssrc) == send_streams_.end()) { |
| 1416 | LOG(LS_ERROR) << "No sending stream on ssrc " << ssrc; |
| 1417 | return false; |
| 1418 | } |
pbos@webrtc.org | ef8bb8d | 2014-08-13 21:36:18 +0000 | [diff] [blame] | 1419 | |
| 1420 | send_streams_[ssrc]->MuteStream(mute); |
| 1421 | return true; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1422 | } |
| 1423 | |
| 1424 | bool WebRtcVideoChannel2::SetRecvRtpHeaderExtensions( |
| 1425 | const std::vector<RtpHeaderExtension>& extensions) { |
pbos@webrtc.org | 50fe359 | 2015-01-29 12:33:07 +0000 | [diff] [blame] | 1426 | TRACE_EVENT0("webrtc", "WebRtcVideoChannel2::SetRecvRtpHeaderExtensions"); |
pbos@webrtc.org | 587ef60 | 2014-06-16 17:32:02 +0000 | [diff] [blame] | 1427 | LOG(LS_INFO) << "SetRecvRtpHeaderExtensions: " |
| 1428 | << RtpExtensionsToString(extensions); |
pbos@webrtc.org | 3c10758 | 2014-07-20 15:27:35 +0000 | [diff] [blame] | 1429 | if (!ValidateRtpHeaderExtensionIds(extensions)) |
| 1430 | return false; |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 1431 | |
pbos@webrtc.org | c37e72e | 2015-01-05 18:51:13 +0000 | [diff] [blame] | 1432 | std::vector<webrtc::RtpExtension> filtered_extensions = |
| 1433 | FilterRtpExtensions(extensions); |
| 1434 | if (!RtpExtensionsHaveChanged(recv_rtp_extensions_, filtered_extensions)) |
| 1435 | return true; |
| 1436 | |
| 1437 | recv_rtp_extensions_ = filtered_extensions; |
| 1438 | |
pbos@webrtc.org | 575d126 | 2014-10-08 14:48:08 +0000 | [diff] [blame] | 1439 | rtc::CritScope stream_lock(&stream_crit_); |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 1440 | for (std::map<uint32, WebRtcVideoReceiveStream*>::iterator it = |
| 1441 | receive_streams_.begin(); |
| 1442 | it != receive_streams_.end(); |
| 1443 | ++it) { |
| 1444 | it->second->SetRtpExtensions(recv_rtp_extensions_); |
| 1445 | } |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1446 | return true; |
| 1447 | } |
| 1448 | |
| 1449 | bool WebRtcVideoChannel2::SetSendRtpHeaderExtensions( |
| 1450 | const std::vector<RtpHeaderExtension>& extensions) { |
pbos@webrtc.org | 50fe359 | 2015-01-29 12:33:07 +0000 | [diff] [blame] | 1451 | TRACE_EVENT0("webrtc", "WebRtcVideoChannel2::SetSendRtpHeaderExtensions"); |
pbos@webrtc.org | 587ef60 | 2014-06-16 17:32:02 +0000 | [diff] [blame] | 1452 | LOG(LS_INFO) << "SetSendRtpHeaderExtensions: " |
| 1453 | << RtpExtensionsToString(extensions); |
pbos@webrtc.org | 3c10758 | 2014-07-20 15:27:35 +0000 | [diff] [blame] | 1454 | if (!ValidateRtpHeaderExtensionIds(extensions)) |
| 1455 | return false; |
| 1456 | |
pbos@webrtc.org | c37e72e | 2015-01-05 18:51:13 +0000 | [diff] [blame] | 1457 | std::vector<webrtc::RtpExtension> filtered_extensions = |
| 1458 | FilterRtpExtensions(extensions); |
| 1459 | if (!RtpExtensionsHaveChanged(send_rtp_extensions_, filtered_extensions)) |
| 1460 | return true; |
| 1461 | |
| 1462 | send_rtp_extensions_ = filtered_extensions; |
pbos@webrtc.org | a2ef4fe | 2014-11-07 10:54:43 +0000 | [diff] [blame] | 1463 | |
Guo-wei Shieh | 64c1e8c | 2015-04-01 15:33:06 -0700 | [diff] [blame] | 1464 | const webrtc::RtpExtension* cvo_extension = FindHeaderExtension( |
| 1465 | send_rtp_extensions_, kRtpVideoRotationHeaderExtension); |
| 1466 | |
pbos@webrtc.org | 575d126 | 2014-10-08 14:48:08 +0000 | [diff] [blame] | 1467 | rtc::CritScope stream_lock(&stream_crit_); |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 1468 | for (std::map<uint32, WebRtcVideoSendStream*>::iterator it = |
| 1469 | send_streams_.begin(); |
| 1470 | it != send_streams_.end(); |
| 1471 | ++it) { |
| 1472 | it->second->SetRtpExtensions(send_rtp_extensions_); |
Guo-wei Shieh | 64c1e8c | 2015-04-01 15:33:06 -0700 | [diff] [blame] | 1473 | it->second->SetApplyRotation(!cvo_extension); |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 1474 | } |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1475 | return true; |
| 1476 | } |
| 1477 | |
Peter Boström | dfd53fe | 2015-03-27 15:58:11 +0100 | [diff] [blame] | 1478 | // Counter-intuitively this method doesn't only set global bitrate caps but also |
| 1479 | // per-stream codec max bitrates. This is to permit SetMaxSendBitrate (b=AS) to |
| 1480 | // raise bitrates above the 2000k default bitrate cap. |
pbos@webrtc.org | 0087318 | 2014-11-25 14:03:34 +0000 | [diff] [blame] | 1481 | bool WebRtcVideoChannel2::SetMaxSendBandwidth(int max_bitrate_bps) { |
Peter Boström | dfd53fe | 2015-03-27 15:58:11 +0100 | [diff] [blame] | 1482 | // TODO(pbos): Figure out whether b=AS means max bitrate for this |
| 1483 | // WebRtcVideoChannel2 (in which case we're good), or per sender (SSRC), in |
| 1484 | // which case this should not set a Call::BitrateConfig but rather reconfigure |
| 1485 | // all senders. |
pbos@webrtc.org | 0087318 | 2014-11-25 14:03:34 +0000 | [diff] [blame] | 1486 | LOG(LS_INFO) << "SetMaxSendBandwidth: " << max_bitrate_bps << "bps."; |
Peter Boström | dfd53fe | 2015-03-27 15:58:11 +0100 | [diff] [blame] | 1487 | if (max_bitrate_bps == bitrate_config_.max_bitrate_bps) |
| 1488 | return true; |
| 1489 | |
pbos@webrtc.org | 0087318 | 2014-11-25 14:03:34 +0000 | [diff] [blame] | 1490 | if (max_bitrate_bps <= 0) { |
| 1491 | // Unsetting max bitrate. |
| 1492 | max_bitrate_bps = -1; |
| 1493 | } |
| 1494 | bitrate_config_.start_bitrate_bps = -1; |
| 1495 | bitrate_config_.max_bitrate_bps = max_bitrate_bps; |
| 1496 | if (max_bitrate_bps > 0 && |
| 1497 | bitrate_config_.min_bitrate_bps > max_bitrate_bps) { |
| 1498 | bitrate_config_.min_bitrate_bps = max_bitrate_bps; |
| 1499 | } |
| 1500 | call_->SetBitrateConfig(bitrate_config_); |
Peter Boström | dfd53fe | 2015-03-27 15:58:11 +0100 | [diff] [blame] | 1501 | rtc::CritScope stream_lock(&stream_crit_); |
| 1502 | for (auto& kv : send_streams_) |
| 1503 | kv.second->SetMaxBitrateBps(max_bitrate_bps); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1504 | return true; |
| 1505 | } |
| 1506 | |
| 1507 | bool WebRtcVideoChannel2::SetOptions(const VideoOptions& options) { |
pbos@webrtc.org | 50fe359 | 2015-01-29 12:33:07 +0000 | [diff] [blame] | 1508 | TRACE_EVENT0("webrtc", "WebRtcVideoChannel2::SetOptions"); |
pbos@webrtc.org | a2ef4fe | 2014-11-07 10:54:43 +0000 | [diff] [blame] | 1509 | LOG(LS_INFO) << "SetOptions: " << options.ToString(); |
| 1510 | VideoOptions old_options = options_; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1511 | options_.SetAll(options); |
pbos@webrtc.org | a2ef4fe | 2014-11-07 10:54:43 +0000 | [diff] [blame] | 1512 | if (options_ == old_options) { |
| 1513 | // No new options to set. |
| 1514 | return true; |
| 1515 | } |
Peter Boström | e7b221f | 2015-04-13 15:34:32 +0200 | [diff] [blame] | 1516 | { |
| 1517 | rtc::CritScope lock(&capturer_crit_); |
| 1518 | options_.cpu_overuse_detection.Get(&signal_cpu_adaptation_); |
| 1519 | } |
pbos@webrtc.org | d819803 | 2014-11-10 14:41:43 +0000 | [diff] [blame] | 1520 | rtc::DiffServCodePoint dscp = options_.dscp.GetWithDefaultIfUnset(false) |
| 1521 | ? rtc::DSCP_AF41 |
| 1522 | : rtc::DSCP_DEFAULT; |
| 1523 | MediaChannel::SetDscp(dscp); |
pbos@webrtc.org | 575d126 | 2014-10-08 14:48:08 +0000 | [diff] [blame] | 1524 | rtc::CritScope stream_lock(&stream_crit_); |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1525 | for (std::map<uint32, WebRtcVideoSendStream*>::iterator it = |
| 1526 | send_streams_.begin(); |
| 1527 | it != send_streams_.end(); |
| 1528 | ++it) { |
| 1529 | it->second->SetOptions(options_); |
| 1530 | } |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1531 | return true; |
| 1532 | } |
| 1533 | |
| 1534 | void WebRtcVideoChannel2::SetInterface(NetworkInterface* iface) { |
| 1535 | MediaChannel::SetInterface(iface); |
| 1536 | // Set the RTP recv/send buffer to a bigger size |
| 1537 | MediaChannel::SetOption(NetworkInterface::ST_RTP, |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1538 | rtc::Socket::OPT_RCVBUF, |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1539 | kVideoRtpBufferSize); |
| 1540 | |
buildbot@webrtc.org | ae694ef | 2014-10-28 17:37:17 +0000 | [diff] [blame] | 1541 | // Speculative change to increase the outbound socket buffer size. |
| 1542 | // In b/15152257, we are seeing a significant number of packets discarded |
| 1543 | // due to lack of socket buffer space, although it's not yet clear what the |
| 1544 | // ideal value should be. |
| 1545 | MediaChannel::SetOption(NetworkInterface::ST_RTP, |
| 1546 | rtc::Socket::OPT_SNDBUF, |
| 1547 | kVideoRtpBufferSize); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1548 | } |
| 1549 | |
| 1550 | void WebRtcVideoChannel2::UpdateAspectRatio(int ratio_w, int ratio_h) { |
| 1551 | // TODO(pbos): Implement. |
| 1552 | } |
| 1553 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1554 | void WebRtcVideoChannel2::OnMessage(rtc::Message* msg) { |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1555 | // Ignored. |
| 1556 | } |
| 1557 | |
pbos@webrtc.org | 42684be | 2014-10-03 11:25:45 +0000 | [diff] [blame] | 1558 | void WebRtcVideoChannel2::OnLoadUpdate(Load load) { |
Peter Boström | e7b221f | 2015-04-13 15:34:32 +0200 | [diff] [blame] | 1559 | // OnLoadUpdate can not take any locks that are held while creating streams |
| 1560 | // etc. Doing so establishes lock-order inversions between the webrtc process |
| 1561 | // thread on stream creation and locks such as stream_crit_ while calling out. |
| 1562 | rtc::CritScope stream_lock(&capturer_crit_); |
| 1563 | if (!signal_cpu_adaptation_) |
| 1564 | return; |
Erik Språng | efbde37 | 2015-04-29 16:21:28 +0200 | [diff] [blame] | 1565 | // Do not adapt resolution for screen content as this will likely result in |
| 1566 | // blurry and unreadable text. |
Peter Boström | e7b221f | 2015-04-13 15:34:32 +0200 | [diff] [blame] | 1567 | for (auto& kv : capturers_) { |
Erik Språng | efbde37 | 2015-04-29 16:21:28 +0200 | [diff] [blame] | 1568 | if (kv.second != nullptr |
| 1569 | && !kv.second->IsScreencast() |
| 1570 | && kv.second->video_adapter() != nullptr) { |
Peter Boström | e7b221f | 2015-04-13 15:34:32 +0200 | [diff] [blame] | 1571 | kv.second->video_adapter()->OnCpuResolutionRequest( |
| 1572 | load == kOveruse ? CoordinatedVideoAdapter::DOWNGRADE |
| 1573 | : CoordinatedVideoAdapter::UPGRADE); |
| 1574 | } |
pbos@webrtc.org | 42684be | 2014-10-03 11:25:45 +0000 | [diff] [blame] | 1575 | } |
| 1576 | } |
| 1577 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1578 | bool WebRtcVideoChannel2::SendRtp(const uint8_t* data, size_t len) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1579 | rtc::Buffer packet(data, len, kMaxRtpPacketLen); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1580 | return MediaChannel::SendPacket(&packet); |
| 1581 | } |
| 1582 | |
| 1583 | bool WebRtcVideoChannel2::SendRtcp(const uint8_t* data, size_t len) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1584 | rtc::Buffer packet(data, len, kMaxRtpPacketLen); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1585 | return MediaChannel::SendRtcp(&packet); |
| 1586 | } |
| 1587 | |
| 1588 | void WebRtcVideoChannel2::StartAllSendStreams() { |
pbos@webrtc.org | 575d126 | 2014-10-08 14:48:08 +0000 | [diff] [blame] | 1589 | rtc::CritScope stream_lock(&stream_crit_); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1590 | for (std::map<uint32, WebRtcVideoSendStream*>::iterator it = |
| 1591 | send_streams_.begin(); |
| 1592 | it != send_streams_.end(); |
| 1593 | ++it) { |
| 1594 | it->second->Start(); |
| 1595 | } |
| 1596 | } |
| 1597 | |
| 1598 | void WebRtcVideoChannel2::StopAllSendStreams() { |
pbos@webrtc.org | 575d126 | 2014-10-08 14:48:08 +0000 | [diff] [blame] | 1599 | rtc::CritScope stream_lock(&stream_crit_); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1600 | for (std::map<uint32, WebRtcVideoSendStream*>::iterator it = |
| 1601 | send_streams_.begin(); |
| 1602 | it != send_streams_.end(); |
| 1603 | ++it) { |
| 1604 | it->second->Stop(); |
| 1605 | } |
| 1606 | } |
| 1607 | |
pbos@webrtc.org | 6ae48c6 | 2014-06-06 10:49:19 +0000 | [diff] [blame] | 1608 | WebRtcVideoChannel2::WebRtcVideoSendStream::VideoSendStreamParameters:: |
| 1609 | VideoSendStreamParameters( |
| 1610 | const webrtc::VideoSendStream::Config& config, |
| 1611 | const VideoOptions& options, |
Peter Boström | dfd53fe | 2015-03-27 15:58:11 +0100 | [diff] [blame] | 1612 | int max_bitrate_bps, |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1613 | const Settable<VideoCodecSettings>& codec_settings) |
Peter Boström | dfd53fe | 2015-03-27 15:58:11 +0100 | [diff] [blame] | 1614 | : config(config), |
| 1615 | options(options), |
| 1616 | max_bitrate_bps(max_bitrate_bps), |
| 1617 | codec_settings(codec_settings) { |
pbos@webrtc.org | 6ae48c6 | 2014-06-06 10:49:19 +0000 | [diff] [blame] | 1618 | } |
| 1619 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1620 | WebRtcVideoChannel2::WebRtcVideoSendStream::WebRtcVideoSendStream( |
| 1621 | webrtc::Call* call, |
pbos@webrtc.org | 7fe1e03 | 2014-10-14 04:25:33 +0000 | [diff] [blame] | 1622 | WebRtcVideoEncoderFactory* external_encoder_factory, |
pbos@webrtc.org | 6ae48c6 | 2014-06-06 10:49:19 +0000 | [diff] [blame] | 1623 | const VideoOptions& options, |
Peter Boström | dfd53fe | 2015-03-27 15:58:11 +0100 | [diff] [blame] | 1624 | int max_bitrate_bps, |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1625 | const Settable<VideoCodecSettings>& codec_settings, |
| 1626 | const StreamParams& sp, |
| 1627 | const std::vector<webrtc::RtpExtension>& rtp_extensions) |
Henrik Kjellander | 7c027b6 | 2015-04-22 13:21:30 +0200 | [diff] [blame] | 1628 | : ssrcs_(sp.ssrcs), |
| 1629 | call_(call), |
pbos@webrtc.org | 7fe1e03 | 2014-10-14 04:25:33 +0000 | [diff] [blame] | 1630 | external_encoder_factory_(external_encoder_factory), |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1631 | stream_(NULL), |
Peter Boström | dfd53fe | 2015-03-27 15:58:11 +0100 | [diff] [blame] | 1632 | parameters_(webrtc::VideoSendStream::Config(), |
| 1633 | options, |
| 1634 | max_bitrate_bps, |
| 1635 | codec_settings), |
pbos@webrtc.org | 7fe1e03 | 2014-10-14 04:25:33 +0000 | [diff] [blame] | 1636 | allocated_encoder_(NULL, webrtc::kVideoCodecUnknown, false), |
pbos@webrtc.org | b648b9d | 2014-08-26 11:08:06 +0000 | [diff] [blame] | 1637 | capturer_(NULL), |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1638 | sending_(false), |
pbos@webrtc.org | 9a4410e | 2015-02-26 10:03:39 +0000 | [diff] [blame] | 1639 | muted_(false), |
| 1640 | old_adapt_changes_(0) { |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1641 | parameters_.config.rtp.max_packet_size = kVideoMtu; |
| 1642 | |
| 1643 | sp.GetPrimarySsrcs(¶meters_.config.rtp.ssrcs); |
| 1644 | sp.GetFidSsrcs(parameters_.config.rtp.ssrcs, |
| 1645 | ¶meters_.config.rtp.rtx.ssrcs); |
| 1646 | parameters_.config.rtp.c_name = sp.cname; |
| 1647 | parameters_.config.rtp.extensions = rtp_extensions; |
| 1648 | |
| 1649 | VideoCodecSettings params; |
| 1650 | if (codec_settings.Get(¶ms)) { |
| 1651 | SetCodec(params); |
| 1652 | } |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1653 | } |
| 1654 | |
| 1655 | WebRtcVideoChannel2::WebRtcVideoSendStream::~WebRtcVideoSendStream() { |
| 1656 | DisconnectCapturer(); |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1657 | if (stream_ != NULL) { |
| 1658 | call_->DestroyVideoSendStream(stream_); |
| 1659 | } |
pbos@webrtc.org | 7fe1e03 | 2014-10-14 04:25:33 +0000 | [diff] [blame] | 1660 | DestroyVideoEncoder(&allocated_encoder_); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1661 | } |
| 1662 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1663 | static void CreateBlackFrame(webrtc::I420VideoFrame* video_frame, |
| 1664 | int width, |
| 1665 | int height) { |
pbos@webrtc.org | b4987bf | 2015-02-18 10:13:09 +0000 | [diff] [blame] | 1666 | video_frame->CreateEmptyFrame(width, height, width, (width + 1) / 2, |
| 1667 | (width + 1) / 2); |
| 1668 | memset(video_frame->buffer(webrtc::kYPlane), 16, |
| 1669 | video_frame->allocated_size(webrtc::kYPlane)); |
| 1670 | memset(video_frame->buffer(webrtc::kUPlane), 128, |
| 1671 | video_frame->allocated_size(webrtc::kUPlane)); |
| 1672 | memset(video_frame->buffer(webrtc::kVPlane), 128, |
| 1673 | video_frame->allocated_size(webrtc::kVPlane)); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1674 | } |
| 1675 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1676 | void WebRtcVideoChannel2::WebRtcVideoSendStream::InputFrame( |
| 1677 | VideoCapturer* capturer, |
| 1678 | const VideoFrame* frame) { |
pbos@webrtc.org | 86196c4 | 2015-02-16 21:02:00 +0000 | [diff] [blame] | 1679 | TRACE_EVENT0("webrtc", "WebRtcVideoSendStream::InputFrame"); |
magjed@webrtc.org | afdd5dd | 2015-03-12 13:11:25 +0000 | [diff] [blame] | 1680 | webrtc::I420VideoFrame video_frame(frame->GetVideoFrameBuffer(), 0, 0, |
| 1681 | frame->GetVideoRotation()); |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1682 | rtc::CritScope cs(&lock_); |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1683 | if (stream_ == NULL) { |
Peter Boström | ad1f9b6 | 2015-04-08 14:04:01 +0200 | [diff] [blame] | 1684 | // Frame input before send codecs are configured, dropping frame. |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1685 | return; |
| 1686 | } |
pbos@webrtc.org | 86196c4 | 2015-02-16 21:02:00 +0000 | [diff] [blame] | 1687 | |
| 1688 | // Not sending, abort early to prevent expensive reconfigurations while |
| 1689 | // setting up codecs etc. |
| 1690 | if (!sending_) |
| 1691 | return; |
| 1692 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1693 | if (format_.width == 0) { // Dropping frames. |
| 1694 | assert(format_.height == 0); |
| 1695 | LOG(LS_VERBOSE) << "VideoFormat 0x0 set, Dropping frame."; |
| 1696 | return; |
| 1697 | } |
pbos@webrtc.org | d60d79a | 2014-09-24 07:10:57 +0000 | [diff] [blame] | 1698 | if (muted_) { |
| 1699 | // Create a black frame to transmit instead. |
magjed@webrtc.org | afdd5dd | 2015-03-12 13:11:25 +0000 | [diff] [blame] | 1700 | CreateBlackFrame(&video_frame, |
pbos@webrtc.org | d60d79a | 2014-09-24 07:10:57 +0000 | [diff] [blame] | 1701 | static_cast<int>(frame->GetWidth()), |
| 1702 | static_cast<int>(frame->GetHeight())); |
| 1703 | } |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1704 | // Reconfigure codec if necessary. |
pbos@webrtc.org | c4175b9 | 2014-09-03 15:25:49 +0000 | [diff] [blame] | 1705 | SetDimensions( |
magjed@webrtc.org | afdd5dd | 2015-03-12 13:11:25 +0000 | [diff] [blame] | 1706 | video_frame.width(), video_frame.height(), capturer->IsScreencast()); |
pbos@webrtc.org | c4175b9 | 2014-09-03 15:25:49 +0000 | [diff] [blame] | 1707 | |
Alex Glaznev | e433c0e | 2015-05-01 13:54:19 -0700 | [diff] [blame] | 1708 | LOG(LS_VERBOSE) << "IncomingCapturedFrame: " << video_frame.width() << "x" |
| 1709 | << video_frame.height() << " -> (codec) " |
| 1710 | << parameters_.encoder_config.streams.back().width << "x" |
| 1711 | << parameters_.encoder_config.streams.back().height; |
perkj@webrtc.org | af612d5 | 2015-03-18 09:51:05 +0000 | [diff] [blame] | 1712 | stream_->Input()->IncomingCapturedFrame(video_frame); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1713 | } |
| 1714 | |
| 1715 | bool WebRtcVideoChannel2::WebRtcVideoSendStream::SetCapturer( |
| 1716 | VideoCapturer* capturer) { |
pbos@webrtc.org | b4987bf | 2015-02-18 10:13:09 +0000 | [diff] [blame] | 1717 | TRACE_EVENT0("webrtc", "WebRtcVideoSendStream::SetCapturer"); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1718 | if (!DisconnectCapturer() && capturer == NULL) { |
| 1719 | return false; |
| 1720 | } |
| 1721 | |
| 1722 | { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1723 | rtc::CritScope cs(&lock_); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1724 | |
pbos@webrtc.org | 9359cb3 | 2014-07-23 15:44:48 +0000 | [diff] [blame] | 1725 | if (capturer == NULL) { |
| 1726 | if (stream_ != NULL) { |
| 1727 | LOG(LS_VERBOSE) << "Disabling capturer, sending black frame."; |
| 1728 | webrtc::I420VideoFrame black_frame; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1729 | |
pbos@webrtc.org | b4987bf | 2015-02-18 10:13:09 +0000 | [diff] [blame] | 1730 | CreateBlackFrame(&black_frame, last_dimensions_.width, |
| 1731 | last_dimensions_.height); |
perkj@webrtc.org | af612d5 | 2015-03-18 09:51:05 +0000 | [diff] [blame] | 1732 | stream_->Input()->IncomingCapturedFrame(black_frame); |
pbos@webrtc.org | 9359cb3 | 2014-07-23 15:44:48 +0000 | [diff] [blame] | 1733 | } |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1734 | |
| 1735 | capturer_ = NULL; |
| 1736 | return true; |
| 1737 | } |
| 1738 | |
| 1739 | capturer_ = capturer; |
| 1740 | } |
| 1741 | // Lock cannot be held while connecting the capturer to prevent lock-order |
| 1742 | // violations. |
| 1743 | capturer->SignalVideoFrame.connect(this, &WebRtcVideoSendStream::InputFrame); |
| 1744 | return true; |
| 1745 | } |
| 1746 | |
| 1747 | bool WebRtcVideoChannel2::WebRtcVideoSendStream::SetVideoFormat( |
| 1748 | const VideoFormat& format) { |
| 1749 | if ((format.width == 0 || format.height == 0) && |
| 1750 | format.width != format.height) { |
| 1751 | LOG(LS_ERROR) << "Can't set VideoFormat, width or height is zero (but not " |
| 1752 | "both, 0x0 drops frames)."; |
| 1753 | return false; |
| 1754 | } |
| 1755 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1756 | rtc::CritScope cs(&lock_); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1757 | if (format.width == 0 && format.height == 0) { |
| 1758 | LOG(LS_INFO) |
| 1759 | << "0x0 resolution selected. Captured frames will be dropped for ssrc: " |
pbos@webrtc.org | 6ae48c6 | 2014-06-06 10:49:19 +0000 | [diff] [blame] | 1760 | << parameters_.config.rtp.ssrcs[0] << "."; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1761 | } else { |
| 1762 | // TODO(pbos): Fix me, this only affects the last stream! |
pbos@webrtc.org | bbe0a85 | 2014-09-19 12:30:25 +0000 | [diff] [blame] | 1763 | parameters_.encoder_config.streams.back().max_framerate = |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1764 | VideoFormat::IntervalToFps(format.interval); |
pbos@webrtc.org | c4175b9 | 2014-09-03 15:25:49 +0000 | [diff] [blame] | 1765 | SetDimensions(format.width, format.height, false); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1766 | } |
| 1767 | |
| 1768 | format_ = format; |
| 1769 | return true; |
| 1770 | } |
| 1771 | |
pbos@webrtc.org | ef8bb8d | 2014-08-13 21:36:18 +0000 | [diff] [blame] | 1772 | void WebRtcVideoChannel2::WebRtcVideoSendStream::MuteStream(bool mute) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1773 | rtc::CritScope cs(&lock_); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1774 | muted_ = mute; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1775 | } |
| 1776 | |
| 1777 | bool WebRtcVideoChannel2::WebRtcVideoSendStream::DisconnectCapturer() { |
pbos@webrtc.org | 963b979 | 2014-10-07 14:27:27 +0000 | [diff] [blame] | 1778 | cricket::VideoCapturer* capturer; |
| 1779 | { |
| 1780 | rtc::CritScope cs(&lock_); |
pbos@webrtc.org | 9a4410e | 2015-02-26 10:03:39 +0000 | [diff] [blame] | 1781 | if (capturer_ == NULL) |
pbos@webrtc.org | 963b979 | 2014-10-07 14:27:27 +0000 | [diff] [blame] | 1782 | return false; |
pbos@webrtc.org | 9a4410e | 2015-02-26 10:03:39 +0000 | [diff] [blame] | 1783 | |
| 1784 | if (capturer_->video_adapter() != nullptr) |
| 1785 | old_adapt_changes_ += capturer_->video_adapter()->adaptation_changes(); |
| 1786 | |
pbos@webrtc.org | 963b979 | 2014-10-07 14:27:27 +0000 | [diff] [blame] | 1787 | capturer = capturer_; |
| 1788 | capturer_ = NULL; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1789 | } |
pbos@webrtc.org | 963b979 | 2014-10-07 14:27:27 +0000 | [diff] [blame] | 1790 | capturer->SignalVideoFrame.disconnect(this); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1791 | return true; |
| 1792 | } |
| 1793 | |
Peter Boström | d6f4c25 | 2015-03-26 16:23:04 +0100 | [diff] [blame] | 1794 | const std::vector<uint32>& |
| 1795 | WebRtcVideoChannel2::WebRtcVideoSendStream::GetSsrcs() const { |
| 1796 | return ssrcs_; |
| 1797 | } |
| 1798 | |
Guo-wei Shieh | 64c1e8c | 2015-04-01 15:33:06 -0700 | [diff] [blame] | 1799 | void WebRtcVideoChannel2::WebRtcVideoSendStream::SetApplyRotation( |
| 1800 | bool apply_rotation) { |
| 1801 | rtc::CritScope cs(&lock_); |
| 1802 | if (capturer_ == NULL) |
| 1803 | return; |
| 1804 | |
| 1805 | capturer_->SetApplyRotation(apply_rotation); |
| 1806 | } |
| 1807 | |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1808 | void WebRtcVideoChannel2::WebRtcVideoSendStream::SetOptions( |
| 1809 | const VideoOptions& options) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1810 | rtc::CritScope cs(&lock_); |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1811 | VideoCodecSettings codec_settings; |
| 1812 | if (parameters_.codec_settings.Get(&codec_settings)) { |
| 1813 | SetCodecAndOptions(codec_settings, options); |
| 1814 | } else { |
| 1815 | parameters_.options = options; |
| 1816 | } |
| 1817 | } |
pbos@webrtc.org | 7fe1e03 | 2014-10-14 04:25:33 +0000 | [diff] [blame] | 1818 | |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1819 | void WebRtcVideoChannel2::WebRtcVideoSendStream::SetCodec( |
| 1820 | const VideoCodecSettings& codec_settings) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1821 | rtc::CritScope cs(&lock_); |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1822 | SetCodecAndOptions(codec_settings, parameters_.options); |
| 1823 | } |
pbos@webrtc.org | 7fe1e03 | 2014-10-14 04:25:33 +0000 | [diff] [blame] | 1824 | |
| 1825 | webrtc::VideoCodecType CodecTypeFromName(const std::string& name) { |
Shao Changbin | e62202f | 2015-04-21 20:24:50 +0800 | [diff] [blame] | 1826 | if (CodecNamesEq(name, kVp8CodecName)) { |
pbos@webrtc.org | 7fe1e03 | 2014-10-14 04:25:33 +0000 | [diff] [blame] | 1827 | return webrtc::kVideoCodecVP8; |
Shao Changbin | e62202f | 2015-04-21 20:24:50 +0800 | [diff] [blame] | 1828 | } else if (CodecNamesEq(name, kVp9CodecName)) { |
andresp@webrtc.org | 188d3b2 | 2014-11-07 13:21:04 +0000 | [diff] [blame] | 1829 | return webrtc::kVideoCodecVP9; |
Shao Changbin | e62202f | 2015-04-21 20:24:50 +0800 | [diff] [blame] | 1830 | } else if (CodecNamesEq(name, kH264CodecName)) { |
pbos@webrtc.org | 7fe1e03 | 2014-10-14 04:25:33 +0000 | [diff] [blame] | 1831 | return webrtc::kVideoCodecH264; |
| 1832 | } |
| 1833 | return webrtc::kVideoCodecUnknown; |
| 1834 | } |
| 1835 | |
| 1836 | WebRtcVideoChannel2::WebRtcVideoSendStream::AllocatedEncoder |
| 1837 | WebRtcVideoChannel2::WebRtcVideoSendStream::CreateVideoEncoder( |
| 1838 | const VideoCodec& codec) { |
| 1839 | webrtc::VideoCodecType type = CodecTypeFromName(codec.name); |
| 1840 | |
| 1841 | // Do not re-create encoders of the same type. |
| 1842 | if (type == allocated_encoder_.type && allocated_encoder_.encoder != NULL) { |
| 1843 | return allocated_encoder_; |
| 1844 | } |
| 1845 | |
| 1846 | if (external_encoder_factory_ != NULL) { |
| 1847 | webrtc::VideoEncoder* encoder = |
| 1848 | external_encoder_factory_->CreateVideoEncoder(type); |
| 1849 | if (encoder != NULL) { |
| 1850 | return AllocatedEncoder(encoder, type, true); |
| 1851 | } |
| 1852 | } |
| 1853 | |
| 1854 | if (type == webrtc::kVideoCodecVP8) { |
| 1855 | return AllocatedEncoder( |
| 1856 | webrtc::VideoEncoder::Create(webrtc::VideoEncoder::kVp8), type, false); |
andresp@webrtc.org | 188d3b2 | 2014-11-07 13:21:04 +0000 | [diff] [blame] | 1857 | } else if (type == webrtc::kVideoCodecVP9) { |
| 1858 | return AllocatedEncoder( |
| 1859 | webrtc::VideoEncoder::Create(webrtc::VideoEncoder::kVp9), type, false); |
pbos@webrtc.org | 7fe1e03 | 2014-10-14 04:25:33 +0000 | [diff] [blame] | 1860 | } |
| 1861 | |
| 1862 | // This shouldn't happen, we should not be trying to create something we don't |
| 1863 | // support. |
| 1864 | assert(false); |
| 1865 | return AllocatedEncoder(NULL, webrtc::kVideoCodecUnknown, false); |
| 1866 | } |
| 1867 | |
| 1868 | void WebRtcVideoChannel2::WebRtcVideoSendStream::DestroyVideoEncoder( |
| 1869 | AllocatedEncoder* encoder) { |
| 1870 | if (encoder->external) { |
| 1871 | external_encoder_factory_->DestroyVideoEncoder(encoder->encoder); |
| 1872 | } else { |
| 1873 | delete encoder->encoder; |
| 1874 | } |
| 1875 | } |
| 1876 | |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1877 | void WebRtcVideoChannel2::WebRtcVideoSendStream::SetCodecAndOptions( |
| 1878 | const VideoCodecSettings& codec_settings, |
| 1879 | const VideoOptions& options) { |
pbos@webrtc.org | a2ef4fe | 2014-11-07 10:54:43 +0000 | [diff] [blame] | 1880 | parameters_.encoder_config = |
| 1881 | CreateVideoEncoderConfig(last_dimensions_, codec_settings.codec); |
pbos@webrtc.org | 86196c4 | 2015-02-16 21:02:00 +0000 | [diff] [blame] | 1882 | if (parameters_.encoder_config.streams.empty()) |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1883 | return; |
pbos@webrtc.org | a2ef4fe | 2014-11-07 10:54:43 +0000 | [diff] [blame] | 1884 | |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1885 | format_ = VideoFormat(codec_settings.codec.width, |
| 1886 | codec_settings.codec.height, |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1887 | VideoFormat::FpsToInterval(30), |
| 1888 | FOURCC_I420); |
pbos@webrtc.org | 6ae48c6 | 2014-06-06 10:49:19 +0000 | [diff] [blame] | 1889 | |
pbos@webrtc.org | 7fe1e03 | 2014-10-14 04:25:33 +0000 | [diff] [blame] | 1890 | AllocatedEncoder new_encoder = CreateVideoEncoder(codec_settings.codec); |
| 1891 | parameters_.config.encoder_settings.encoder = new_encoder.encoder; |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1892 | parameters_.config.encoder_settings.payload_name = codec_settings.codec.name; |
| 1893 | parameters_.config.encoder_settings.payload_type = codec_settings.codec.id; |
| 1894 | parameters_.config.rtp.fec = codec_settings.fec; |
| 1895 | |
| 1896 | // Set RTX payload type if RTX is enabled. |
| 1897 | if (!parameters_.config.rtp.rtx.ssrcs.empty()) { |
pbos@webrtc.org | b9557a9 | 2015-03-20 19:52:56 +0000 | [diff] [blame] | 1898 | if (codec_settings.rtx_payload_type == -1) { |
| 1899 | LOG(LS_WARNING) << "RTX SSRCs configured but there's no configured RTX " |
| 1900 | "payload type. Ignoring."; |
| 1901 | parameters_.config.rtp.rtx.ssrcs.clear(); |
| 1902 | } else { |
| 1903 | parameters_.config.rtp.rtx.payload_type = codec_settings.rtx_payload_type; |
| 1904 | } |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1905 | } |
| 1906 | |
Shao Changbin | e62202f | 2015-04-21 20:24:50 +0800 | [diff] [blame] | 1907 | if (HasNack(codec_settings.codec)) { |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1908 | parameters_.config.rtp.nack.rtp_history_ms = kNackHistoryMs; |
| 1909 | } |
| 1910 | |
pbos@webrtc.org | 5ff71ab | 2014-07-23 07:28:56 +0000 | [diff] [blame] | 1911 | options.suspend_below_min_bitrate.Get( |
| 1912 | ¶meters_.config.suspend_below_min_bitrate); |
| 1913 | |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1914 | parameters_.codec_settings.Set(codec_settings); |
pbos@webrtc.org | 6ae48c6 | 2014-06-06 10:49:19 +0000 | [diff] [blame] | 1915 | parameters_.options = options; |
pbos@webrtc.org | 543e589 | 2014-07-23 07:01:31 +0000 | [diff] [blame] | 1916 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1917 | RecreateWebRtcStream(); |
pbos@webrtc.org | 7fe1e03 | 2014-10-14 04:25:33 +0000 | [diff] [blame] | 1918 | if (allocated_encoder_.encoder != new_encoder.encoder) { |
| 1919 | DestroyVideoEncoder(&allocated_encoder_); |
| 1920 | allocated_encoder_ = new_encoder; |
| 1921 | } |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1922 | } |
| 1923 | |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 1924 | void WebRtcVideoChannel2::WebRtcVideoSendStream::SetRtpExtensions( |
| 1925 | const std::vector<webrtc::RtpExtension>& rtp_extensions) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1926 | rtc::CritScope cs(&lock_); |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 1927 | parameters_.config.rtp.extensions = rtp_extensions; |
Peter Boström | 3c3f646 | 2015-04-15 16:27:49 +0200 | [diff] [blame] | 1928 | if (stream_ != nullptr) |
| 1929 | RecreateWebRtcStream(); |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 1930 | } |
| 1931 | |
pbos@webrtc.org | a2ef4fe | 2014-11-07 10:54:43 +0000 | [diff] [blame] | 1932 | webrtc::VideoEncoderConfig |
| 1933 | WebRtcVideoChannel2::WebRtcVideoSendStream::CreateVideoEncoderConfig( |
| 1934 | const Dimensions& dimensions, |
| 1935 | const VideoCodec& codec) const { |
| 1936 | webrtc::VideoEncoderConfig encoder_config; |
| 1937 | if (dimensions.is_screencast) { |
pbos@webrtc.org | efc82c2 | 2014-10-27 13:58:00 +0000 | [diff] [blame] | 1938 | int screencast_min_bitrate_kbps; |
| 1939 | parameters_.options.screencast_min_bitrate.Get( |
| 1940 | &screencast_min_bitrate_kbps); |
| 1941 | encoder_config.min_transmit_bitrate_bps = |
| 1942 | screencast_min_bitrate_kbps * 1000; |
Erik Språng | 143cec1 | 2015-04-28 10:01:41 +0200 | [diff] [blame] | 1943 | encoder_config.content_type = |
| 1944 | webrtc::VideoEncoderConfig::ContentType::kScreen; |
pbos@webrtc.org | efc82c2 | 2014-10-27 13:58:00 +0000 | [diff] [blame] | 1945 | } else { |
| 1946 | encoder_config.min_transmit_bitrate_bps = 0; |
Erik Språng | 143cec1 | 2015-04-28 10:01:41 +0200 | [diff] [blame] | 1947 | encoder_config.content_type = |
| 1948 | webrtc::VideoEncoderConfig::ContentType::kRealtimeVideo; |
pbos@webrtc.org | efc82c2 | 2014-10-27 13:58:00 +0000 | [diff] [blame] | 1949 | } |
| 1950 | |
pbos@webrtc.org | a2ef4fe | 2014-11-07 10:54:43 +0000 | [diff] [blame] | 1951 | // Restrict dimensions according to codec max. |
| 1952 | int width = dimensions.width; |
| 1953 | int height = dimensions.height; |
| 1954 | if (!dimensions.is_screencast) { |
| 1955 | if (codec.width < width) |
| 1956 | width = codec.width; |
| 1957 | if (codec.height < height) |
| 1958 | height = codec.height; |
| 1959 | } |
| 1960 | |
| 1961 | VideoCodec clamped_codec = codec; |
| 1962 | clamped_codec.width = width; |
| 1963 | clamped_codec.height = height; |
pbos@webrtc.org | cddd17c | 2014-09-16 16:33:13 +0000 | [diff] [blame] | 1964 | |
pbos@webrtc.org | f1c8b90 | 2015-01-14 17:29:27 +0000 | [diff] [blame] | 1965 | encoder_config.streams = CreateVideoStreams( |
Peter Boström | dfd53fe | 2015-03-27 15:58:11 +0100 | [diff] [blame] | 1966 | clamped_codec, parameters_.options, parameters_.max_bitrate_bps, |
Erik Språng | 143cec1 | 2015-04-28 10:01:41 +0200 | [diff] [blame] | 1967 | dimensions.is_screencast ? 1 : parameters_.config.rtp.ssrcs.size()); |
pbos@webrtc.org | 6f48f1b | 2014-07-22 16:29:54 +0000 | [diff] [blame] | 1968 | |
pbos@webrtc.org | b7ed779 | 2014-10-31 13:08:10 +0000 | [diff] [blame] | 1969 | // Conference mode screencast uses 2 temporal layers split at 100kbit. |
| 1970 | if (parameters_.options.conference_mode.GetWithDefaultIfUnset(false) && |
pbos@webrtc.org | a2ef4fe | 2014-11-07 10:54:43 +0000 | [diff] [blame] | 1971 | dimensions.is_screencast && encoder_config.streams.size() == 1) { |
sprang@webrtc.org | 46d4d29 | 2014-12-23 15:19:35 +0000 | [diff] [blame] | 1972 | ScreenshareLayerConfig config = ScreenshareLayerConfig::GetDefault(); |
| 1973 | |
| 1974 | // For screenshare in conference mode, tl0 and tl1 bitrates are piggybacked |
| 1975 | // on the VideoCodec struct as target and max bitrates, respectively. |
| 1976 | // See eg. webrtc::VP8EncoderImpl::SetRates(). |
| 1977 | encoder_config.streams[0].target_bitrate_bps = |
| 1978 | config.tl0_bitrate_kbps * 1000; |
| 1979 | encoder_config.streams[0].max_bitrate_bps = config.tl1_bitrate_kbps * 1000; |
pbos@webrtc.org | b7ed779 | 2014-10-31 13:08:10 +0000 | [diff] [blame] | 1980 | encoder_config.streams[0].temporal_layer_thresholds_bps.clear(); |
| 1981 | encoder_config.streams[0].temporal_layer_thresholds_bps.push_back( |
sprang@webrtc.org | 46d4d29 | 2014-12-23 15:19:35 +0000 | [diff] [blame] | 1982 | config.tl0_bitrate_kbps * 1000); |
pbos@webrtc.org | b7ed779 | 2014-10-31 13:08:10 +0000 | [diff] [blame] | 1983 | } |
pbos@webrtc.org | a2ef4fe | 2014-11-07 10:54:43 +0000 | [diff] [blame] | 1984 | return encoder_config; |
| 1985 | } |
| 1986 | |
| 1987 | void WebRtcVideoChannel2::WebRtcVideoSendStream::SetDimensions( |
| 1988 | int width, |
| 1989 | int height, |
| 1990 | bool is_screencast) { |
| 1991 | if (last_dimensions_.width == width && last_dimensions_.height == height && |
| 1992 | last_dimensions_.is_screencast == is_screencast) { |
| 1993 | // Configured using the same parameters, do not reconfigure. |
| 1994 | return; |
| 1995 | } |
| 1996 | LOG(LS_INFO) << "SetDimensions: " << width << "x" << height |
| 1997 | << (is_screencast ? " (screencast)" : " (not screencast)"); |
| 1998 | |
| 1999 | last_dimensions_.width = width; |
| 2000 | last_dimensions_.height = height; |
| 2001 | last_dimensions_.is_screencast = is_screencast; |
| 2002 | |
| 2003 | assert(!parameters_.encoder_config.streams.empty()); |
| 2004 | |
| 2005 | VideoCodecSettings codec_settings; |
| 2006 | parameters_.codec_settings.Get(&codec_settings); |
| 2007 | |
| 2008 | webrtc::VideoEncoderConfig encoder_config = |
| 2009 | CreateVideoEncoderConfig(last_dimensions_, codec_settings.codec); |
| 2010 | |
Erik Språng | 143cec1 | 2015-04-28 10:01:41 +0200 | [diff] [blame] | 2011 | encoder_config.encoder_specific_settings = ConfigureVideoEncoderSettings( |
| 2012 | codec_settings.codec, parameters_.options, is_screencast); |
pbos@webrtc.org | b7ed779 | 2014-10-31 13:08:10 +0000 | [diff] [blame] | 2013 | |
pbos@webrtc.org | bbe0a85 | 2014-09-19 12:30:25 +0000 | [diff] [blame] | 2014 | bool stream_reconfigured = stream_->ReconfigureVideoEncoder(encoder_config); |
| 2015 | |
pbos@webrtc.org | bbe0a85 | 2014-09-19 12:30:25 +0000 | [diff] [blame] | 2016 | encoder_config.encoder_specific_settings = NULL; |
pbos@webrtc.org | 6f48f1b | 2014-07-22 16:29:54 +0000 | [diff] [blame] | 2017 | |
| 2018 | if (!stream_reconfigured) { |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 2019 | LOG(LS_WARNING) << "Failed to reconfigure video encoder for dimensions: " |
| 2020 | << width << "x" << height; |
| 2021 | return; |
| 2022 | } |
pbos@webrtc.org | cddd17c | 2014-09-16 16:33:13 +0000 | [diff] [blame] | 2023 | |
pbos@webrtc.org | bbe0a85 | 2014-09-19 12:30:25 +0000 | [diff] [blame] | 2024 | parameters_.encoder_config = encoder_config; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 2025 | } |
| 2026 | |
| 2027 | void WebRtcVideoChannel2::WebRtcVideoSendStream::Start() { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 2028 | rtc::CritScope cs(&lock_); |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 2029 | assert(stream_ != NULL); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 2030 | stream_->Start(); |
| 2031 | sending_ = true; |
| 2032 | } |
| 2033 | |
| 2034 | void WebRtcVideoChannel2::WebRtcVideoSendStream::Stop() { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 2035 | rtc::CritScope cs(&lock_); |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 2036 | if (stream_ != NULL) { |
| 2037 | stream_->Stop(); |
| 2038 | } |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 2039 | sending_ = false; |
| 2040 | } |
| 2041 | |
pbos@webrtc.org | e6f84ae | 2014-07-18 11:11:55 +0000 | [diff] [blame] | 2042 | VideoSenderInfo |
| 2043 | WebRtcVideoChannel2::WebRtcVideoSendStream::GetVideoSenderInfo() { |
| 2044 | VideoSenderInfo info; |
pbos@webrtc.org | 77e11bb | 2015-02-23 16:39:07 +0000 | [diff] [blame] | 2045 | webrtc::VideoSendStream::Stats stats; |
| 2046 | { |
| 2047 | rtc::CritScope cs(&lock_); |
| 2048 | for (uint32_t ssrc : parameters_.config.rtp.ssrcs) |
| 2049 | info.add_ssrc(ssrc); |
pbos@webrtc.org | e6f84ae | 2014-07-18 11:11:55 +0000 | [diff] [blame] | 2050 | |
Peter Boström | 74d9ed7 | 2015-03-26 16:28:31 +0100 | [diff] [blame] | 2051 | VideoCodecSettings codec_settings; |
| 2052 | if (parameters_.codec_settings.Get(&codec_settings)) |
| 2053 | info.codec_name = codec_settings.codec.name; |
pbos@webrtc.org | 77e11bb | 2015-02-23 16:39:07 +0000 | [diff] [blame] | 2054 | for (size_t i = 0; i < parameters_.encoder_config.streams.size(); ++i) { |
| 2055 | if (i == parameters_.encoder_config.streams.size() - 1) { |
| 2056 | info.preferred_bitrate += |
| 2057 | parameters_.encoder_config.streams[i].max_bitrate_bps; |
| 2058 | } else { |
| 2059 | info.preferred_bitrate += |
| 2060 | parameters_.encoder_config.streams[i].target_bitrate_bps; |
| 2061 | } |
| 2062 | } |
pbos@webrtc.org | c3d2bd2 | 2014-08-12 20:55:10 +0000 | [diff] [blame] | 2063 | |
pbos@webrtc.org | 77e11bb | 2015-02-23 16:39:07 +0000 | [diff] [blame] | 2064 | if (stream_ == NULL) |
| 2065 | return info; |
| 2066 | |
| 2067 | stats = stream_->GetStats(); |
| 2068 | |
pbos@webrtc.org | 9a4410e | 2015-02-26 10:03:39 +0000 | [diff] [blame] | 2069 | info.adapt_changes = old_adapt_changes_; |
| 2070 | info.adapt_reason = CoordinatedVideoAdapter::ADAPTREASON_NONE; |
| 2071 | |
| 2072 | if (capturer_ != NULL) { |
| 2073 | if (!capturer_->IsMuted()) { |
| 2074 | VideoFormat last_captured_frame_format; |
| 2075 | capturer_->GetStats(&info.adapt_frame_drops, &info.effects_frame_drops, |
| 2076 | &info.capturer_frame_time, |
| 2077 | &last_captured_frame_format); |
| 2078 | info.input_frame_width = last_captured_frame_format.width; |
| 2079 | info.input_frame_height = last_captured_frame_format.height; |
| 2080 | } |
| 2081 | if (capturer_->video_adapter() != nullptr) { |
| 2082 | info.adapt_changes += capturer_->video_adapter()->adaptation_changes(); |
| 2083 | info.adapt_reason = capturer_->video_adapter()->adapt_reason(); |
| 2084 | } |
pbos@webrtc.org | 77e11bb | 2015-02-23 16:39:07 +0000 | [diff] [blame] | 2085 | } |
| 2086 | } |
pbos@webrtc.org | e6f84ae | 2014-07-18 11:11:55 +0000 | [diff] [blame] | 2087 | info.framerate_input = stats.input_frame_rate; |
| 2088 | info.framerate_sent = stats.encode_frame_rate; |
pbos@webrtc.org | 3e6e271 | 2015-02-26 12:19:31 +0000 | [diff] [blame] | 2089 | info.avg_encode_ms = stats.avg_encode_time_ms; |
| 2090 | info.encode_usage_percent = stats.encode_usage_percent; |
pbos@webrtc.org | e6f84ae | 2014-07-18 11:11:55 +0000 | [diff] [blame] | 2091 | |
pbos@webrtc.org | 77e11bb | 2015-02-23 16:39:07 +0000 | [diff] [blame] | 2092 | info.nominal_bitrate = stats.media_bitrate_bps; |
| 2093 | |
pbos@webrtc.org | 273a414 | 2014-12-01 15:23:21 +0000 | [diff] [blame] | 2094 | info.send_frame_width = 0; |
| 2095 | info.send_frame_height = 0; |
pbos@webrtc.org | 09c77b9 | 2015-02-25 10:42:16 +0000 | [diff] [blame] | 2096 | for (std::map<uint32_t, webrtc::VideoSendStream::StreamStats>::iterator it = |
pbos@webrtc.org | e6f84ae | 2014-07-18 11:11:55 +0000 | [diff] [blame] | 2097 | stats.substreams.begin(); |
pbos@webrtc.org | 09c77b9 | 2015-02-25 10:42:16 +0000 | [diff] [blame] | 2098 | it != stats.substreams.end(); ++it) { |
pbos@webrtc.org | e6f84ae | 2014-07-18 11:11:55 +0000 | [diff] [blame] | 2099 | // TODO(pbos): Wire up additional stats, such as padding bytes. |
pbos@webrtc.org | 09c77b9 | 2015-02-25 10:42:16 +0000 | [diff] [blame] | 2100 | webrtc::VideoSendStream::StreamStats stream_stats = it->second; |
asapersson@webrtc.org | cfd82df | 2015-01-22 09:39:59 +0000 | [diff] [blame] | 2101 | info.bytes_sent += stream_stats.rtp_stats.transmitted.payload_bytes + |
| 2102 | stream_stats.rtp_stats.transmitted.header_bytes + |
| 2103 | stream_stats.rtp_stats.transmitted.padding_bytes; |
| 2104 | info.packets_sent += stream_stats.rtp_stats.transmitted.packets; |
pbos@webrtc.org | e6f84ae | 2014-07-18 11:11:55 +0000 | [diff] [blame] | 2105 | info.packets_lost += stream_stats.rtcp_stats.cumulative_lost; |
pbos@webrtc.org | 09c77b9 | 2015-02-25 10:42:16 +0000 | [diff] [blame] | 2106 | if (stream_stats.width > info.send_frame_width) |
| 2107 | info.send_frame_width = stream_stats.width; |
| 2108 | if (stream_stats.height > info.send_frame_height) |
| 2109 | info.send_frame_height = stream_stats.height; |
pbos@webrtc.org | 1d0fa5d | 2015-02-19 12:47:00 +0000 | [diff] [blame] | 2110 | info.firs_rcvd += stream_stats.rtcp_packet_type_counts.fir_packets; |
| 2111 | info.nacks_rcvd += stream_stats.rtcp_packet_type_counts.nack_packets; |
| 2112 | info.plis_rcvd += stream_stats.rtcp_packet_type_counts.pli_packets; |
pbos@webrtc.org | e6f84ae | 2014-07-18 11:11:55 +0000 | [diff] [blame] | 2113 | } |
| 2114 | |
| 2115 | if (!stats.substreams.empty()) { |
| 2116 | // TODO(pbos): Report fraction lost per SSRC. |
pbos@webrtc.org | 09c77b9 | 2015-02-25 10:42:16 +0000 | [diff] [blame] | 2117 | webrtc::VideoSendStream::StreamStats first_stream_stats = |
| 2118 | stats.substreams.begin()->second; |
pbos@webrtc.org | e6f84ae | 2014-07-18 11:11:55 +0000 | [diff] [blame] | 2119 | info.fraction_lost = |
| 2120 | static_cast<float>(first_stream_stats.rtcp_stats.fraction_lost) / |
| 2121 | (1 << 8); |
| 2122 | } |
| 2123 | |
pbos@webrtc.org | e6f84ae | 2014-07-18 11:11:55 +0000 | [diff] [blame] | 2124 | return info; |
| 2125 | } |
| 2126 | |
stefan@webrtc.org | 0bae1fa | 2014-11-05 14:05:29 +0000 | [diff] [blame] | 2127 | void WebRtcVideoChannel2::WebRtcVideoSendStream::FillBandwidthEstimationInfo( |
| 2128 | BandwidthEstimationInfo* bwe_info) { |
| 2129 | rtc::CritScope cs(&lock_); |
| 2130 | if (stream_ == NULL) { |
| 2131 | return; |
| 2132 | } |
| 2133 | webrtc::VideoSendStream::Stats stats = stream_->GetStats(); |
pbos@webrtc.org | 09c77b9 | 2015-02-25 10:42:16 +0000 | [diff] [blame] | 2134 | for (std::map<uint32_t, webrtc::VideoSendStream::StreamStats>::iterator it = |
stefan@webrtc.org | 0bae1fa | 2014-11-05 14:05:29 +0000 | [diff] [blame] | 2135 | stats.substreams.begin(); |
pbos@webrtc.org | 09c77b9 | 2015-02-25 10:42:16 +0000 | [diff] [blame] | 2136 | it != stats.substreams.end(); ++it) { |
stefan@webrtc.org | 0bae1fa | 2014-11-05 14:05:29 +0000 | [diff] [blame] | 2137 | bwe_info->transmit_bitrate += it->second.total_bitrate_bps; |
| 2138 | bwe_info->retransmit_bitrate += it->second.retransmit_bitrate_bps; |
| 2139 | } |
pbos@webrtc.org | 891d483 | 2015-02-26 13:15:22 +0000 | [diff] [blame] | 2140 | bwe_info->target_enc_bitrate += stats.target_media_bitrate_bps; |
pbos@webrtc.org | 77e11bb | 2015-02-23 16:39:07 +0000 | [diff] [blame] | 2141 | bwe_info->actual_enc_bitrate += stats.media_bitrate_bps; |
stefan@webrtc.org | 0bae1fa | 2014-11-05 14:05:29 +0000 | [diff] [blame] | 2142 | } |
| 2143 | |
Peter Boström | dfd53fe | 2015-03-27 15:58:11 +0100 | [diff] [blame] | 2144 | void WebRtcVideoChannel2::WebRtcVideoSendStream::SetMaxBitrateBps( |
| 2145 | int max_bitrate_bps) { |
| 2146 | rtc::CritScope cs(&lock_); |
| 2147 | parameters_.max_bitrate_bps = max_bitrate_bps; |
| 2148 | |
| 2149 | // No need to reconfigure if the stream hasn't been configured yet. |
| 2150 | if (parameters_.encoder_config.streams.empty()) |
| 2151 | return; |
| 2152 | |
| 2153 | // Force a stream reconfigure to set the new max bitrate. |
| 2154 | int width = last_dimensions_.width; |
| 2155 | last_dimensions_.width = 0; |
| 2156 | SetDimensions(width, last_dimensions_.height, last_dimensions_.is_screencast); |
| 2157 | } |
pbos@webrtc.org | 42684be | 2014-10-03 11:25:45 +0000 | [diff] [blame] | 2158 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 2159 | void WebRtcVideoChannel2::WebRtcVideoSendStream::RecreateWebRtcStream() { |
| 2160 | if (stream_ != NULL) { |
| 2161 | call_->DestroyVideoSendStream(stream_); |
| 2162 | } |
pbos@webrtc.org | 6ae48c6 | 2014-06-06 10:49:19 +0000 | [diff] [blame] | 2163 | |
pbos@webrtc.org | 6f48f1b | 2014-07-22 16:29:54 +0000 | [diff] [blame] | 2164 | VideoCodecSettings codec_settings; |
| 2165 | parameters_.codec_settings.Get(&codec_settings); |
pbos@webrtc.org | bbe0a85 | 2014-09-19 12:30:25 +0000 | [diff] [blame] | 2166 | parameters_.encoder_config.encoder_specific_settings = |
Erik Språng | 143cec1 | 2015-04-28 10:01:41 +0200 | [diff] [blame] | 2167 | ConfigureVideoEncoderSettings( |
| 2168 | codec_settings.codec, parameters_.options, |
| 2169 | parameters_.encoder_config.content_type == |
| 2170 | webrtc::VideoEncoderConfig::ContentType::kScreen); |
pbos@webrtc.org | 6f48f1b | 2014-07-22 16:29:54 +0000 | [diff] [blame] | 2171 | |
pbos@webrtc.org | b9557a9 | 2015-03-20 19:52:56 +0000 | [diff] [blame] | 2172 | webrtc::VideoSendStream::Config config = parameters_.config; |
| 2173 | if (!config.rtp.rtx.ssrcs.empty() && config.rtp.rtx.payload_type == -1) { |
| 2174 | LOG(LS_WARNING) << "RTX SSRCs configured but there's no configured RTX " |
| 2175 | "payload type the set codec. Ignoring RTX."; |
| 2176 | config.rtp.rtx.ssrcs.clear(); |
| 2177 | } |
| 2178 | stream_ = call_->CreateVideoSendStream(config, parameters_.encoder_config); |
pbos@webrtc.org | 6f48f1b | 2014-07-22 16:29:54 +0000 | [diff] [blame] | 2179 | |
pbos@webrtc.org | bbe0a85 | 2014-09-19 12:30:25 +0000 | [diff] [blame] | 2180 | parameters_.encoder_config.encoder_specific_settings = NULL; |
pbos@webrtc.org | 6f48f1b | 2014-07-22 16:29:54 +0000 | [diff] [blame] | 2181 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 2182 | if (sending_) { |
| 2183 | stream_->Start(); |
| 2184 | } |
| 2185 | } |
| 2186 | |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 2187 | WebRtcVideoChannel2::WebRtcVideoReceiveStream::WebRtcVideoReceiveStream( |
| 2188 | webrtc::Call* call, |
Peter Boström | d6f4c25 | 2015-03-26 16:23:04 +0100 | [diff] [blame] | 2189 | const std::vector<uint32>& ssrcs, |
pbos@webrtc.org | 776e6f2 | 2014-10-29 15:28:39 +0000 | [diff] [blame] | 2190 | WebRtcVideoDecoderFactory* external_decoder_factory, |
pbos@webrtc.org | a2a6fe6 | 2015-03-06 15:35:19 +0000 | [diff] [blame] | 2191 | bool default_stream, |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 2192 | const webrtc::VideoReceiveStream::Config& config, |
| 2193 | const std::vector<VideoCodecSettings>& recv_codecs) |
| 2194 | : call_(call), |
Peter Boström | d6f4c25 | 2015-03-26 16:23:04 +0100 | [diff] [blame] | 2195 | ssrcs_(ssrcs), |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 2196 | stream_(NULL), |
pbos@webrtc.org | a2a6fe6 | 2015-03-06 15:35:19 +0000 | [diff] [blame] | 2197 | default_stream_(default_stream), |
pbos@webrtc.org | b648b9d | 2014-08-26 11:08:06 +0000 | [diff] [blame] | 2198 | config_(config), |
pbos@webrtc.org | 776e6f2 | 2014-10-29 15:28:39 +0000 | [diff] [blame] | 2199 | external_decoder_factory_(external_decoder_factory), |
pbos@webrtc.org | b648b9d | 2014-08-26 11:08:06 +0000 | [diff] [blame] | 2200 | renderer_(NULL), |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 2201 | last_width_(-1), |
magjed@webrtc.org | fc5ad95 | 2015-01-27 09:57:01 +0000 | [diff] [blame] | 2202 | last_height_(-1), |
| 2203 | first_frame_timestamp_(-1), |
| 2204 | estimated_remote_start_ntp_time_ms_(0) { |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 2205 | config_.renderer = this; |
| 2206 | // SetRecvCodecs will also reset (start) the VideoReceiveStream. |
| 2207 | SetRecvCodecs(recv_codecs); |
| 2208 | } |
| 2209 | |
| 2210 | WebRtcVideoChannel2::WebRtcVideoReceiveStream::~WebRtcVideoReceiveStream() { |
| 2211 | call_->DestroyVideoReceiveStream(stream_); |
pbos@webrtc.org | 96a9325 | 2014-11-03 14:46:44 +0000 | [diff] [blame] | 2212 | ClearDecoders(&allocated_decoders_); |
| 2213 | } |
| 2214 | |
Peter Boström | d6f4c25 | 2015-03-26 16:23:04 +0100 | [diff] [blame] | 2215 | const std::vector<uint32>& |
| 2216 | WebRtcVideoChannel2::WebRtcVideoReceiveStream::GetSsrcs() const { |
| 2217 | return ssrcs_; |
| 2218 | } |
| 2219 | |
pbos@webrtc.org | 96a9325 | 2014-11-03 14:46:44 +0000 | [diff] [blame] | 2220 | WebRtcVideoChannel2::WebRtcVideoReceiveStream::AllocatedDecoder |
| 2221 | WebRtcVideoChannel2::WebRtcVideoReceiveStream::CreateOrReuseVideoDecoder( |
| 2222 | std::vector<AllocatedDecoder>* old_decoders, |
| 2223 | const VideoCodec& codec) { |
| 2224 | webrtc::VideoCodecType type = CodecTypeFromName(codec.name); |
| 2225 | |
| 2226 | for (size_t i = 0; i < old_decoders->size(); ++i) { |
| 2227 | if ((*old_decoders)[i].type == type) { |
| 2228 | AllocatedDecoder decoder = (*old_decoders)[i]; |
| 2229 | (*old_decoders)[i] = old_decoders->back(); |
| 2230 | old_decoders->pop_back(); |
| 2231 | return decoder; |
| 2232 | } |
| 2233 | } |
| 2234 | |
| 2235 | if (external_decoder_factory_ != NULL) { |
| 2236 | webrtc::VideoDecoder* decoder = |
| 2237 | external_decoder_factory_->CreateVideoDecoder(type); |
| 2238 | if (decoder != NULL) { |
| 2239 | return AllocatedDecoder(decoder, type, true); |
| 2240 | } |
| 2241 | } |
| 2242 | |
| 2243 | if (type == webrtc::kVideoCodecVP8) { |
| 2244 | return AllocatedDecoder( |
| 2245 | webrtc::VideoDecoder::Create(webrtc::VideoDecoder::kVp8), type, false); |
| 2246 | } |
| 2247 | |
pbos@webrtc.org | b9557a9 | 2015-03-20 19:52:56 +0000 | [diff] [blame] | 2248 | if (type == webrtc::kVideoCodecVP9) { |
| 2249 | return AllocatedDecoder( |
| 2250 | webrtc::VideoDecoder::Create(webrtc::VideoDecoder::kVp9), type, false); |
| 2251 | } |
| 2252 | |
pbos@webrtc.org | 96a9325 | 2014-11-03 14:46:44 +0000 | [diff] [blame] | 2253 | // This shouldn't happen, we should not be trying to create something we don't |
| 2254 | // support. |
| 2255 | assert(false); |
| 2256 | return AllocatedDecoder(NULL, webrtc::kVideoCodecUnknown, false); |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 2257 | } |
| 2258 | |
| 2259 | void WebRtcVideoChannel2::WebRtcVideoReceiveStream::SetRecvCodecs( |
| 2260 | const std::vector<VideoCodecSettings>& recv_codecs) { |
pbos@webrtc.org | 96a9325 | 2014-11-03 14:46:44 +0000 | [diff] [blame] | 2261 | std::vector<AllocatedDecoder> old_decoders = allocated_decoders_; |
| 2262 | allocated_decoders_.clear(); |
| 2263 | config_.decoders.clear(); |
| 2264 | for (size_t i = 0; i < recv_codecs.size(); ++i) { |
| 2265 | AllocatedDecoder allocated_decoder = |
| 2266 | CreateOrReuseVideoDecoder(&old_decoders, recv_codecs[i].codec); |
| 2267 | allocated_decoders_.push_back(allocated_decoder); |
| 2268 | |
| 2269 | webrtc::VideoReceiveStream::Decoder decoder; |
| 2270 | decoder.decoder = allocated_decoder.decoder; |
| 2271 | decoder.payload_type = recv_codecs[i].codec.id; |
| 2272 | decoder.payload_name = recv_codecs[i].codec.name; |
| 2273 | config_.decoders.push_back(decoder); |
| 2274 | } |
| 2275 | |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 2276 | // TODO(pbos): Reconfigure RTX based on incoming recv_codecs. |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 2277 | config_.rtp.fec = recv_codecs.front().fec; |
pbos@webrtc.org | 257e130 | 2014-07-25 19:01:32 +0000 | [diff] [blame] | 2278 | config_.rtp.nack.rtp_history_ms = |
Shao Changbin | e62202f | 2015-04-21 20:24:50 +0800 | [diff] [blame] | 2279 | HasNack(recv_codecs.begin()->codec) ? kNackHistoryMs : 0; |
| 2280 | config_.rtp.remb = HasRemb(recv_codecs.begin()->codec); |
pbos@webrtc.org | 257e130 | 2014-07-25 19:01:32 +0000 | [diff] [blame] | 2281 | |
pbos@webrtc.org | 96a9325 | 2014-11-03 14:46:44 +0000 | [diff] [blame] | 2282 | ClearDecoders(&old_decoders); |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 2283 | RecreateWebRtcStream(); |
| 2284 | } |
| 2285 | |
| 2286 | void WebRtcVideoChannel2::WebRtcVideoReceiveStream::SetRtpExtensions( |
| 2287 | const std::vector<webrtc::RtpExtension>& extensions) { |
| 2288 | config_.rtp.extensions = extensions; |
Peter Boström | 3c3f646 | 2015-04-15 16:27:49 +0200 | [diff] [blame] | 2289 | if (stream_ != nullptr) |
| 2290 | RecreateWebRtcStream(); |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 2291 | } |
| 2292 | |
| 2293 | void WebRtcVideoChannel2::WebRtcVideoReceiveStream::RecreateWebRtcStream() { |
| 2294 | if (stream_ != NULL) { |
| 2295 | call_->DestroyVideoReceiveStream(stream_); |
| 2296 | } |
| 2297 | stream_ = call_->CreateVideoReceiveStream(config_); |
| 2298 | stream_->Start(); |
| 2299 | } |
| 2300 | |
pbos@webrtc.org | 96a9325 | 2014-11-03 14:46:44 +0000 | [diff] [blame] | 2301 | void WebRtcVideoChannel2::WebRtcVideoReceiveStream::ClearDecoders( |
| 2302 | std::vector<AllocatedDecoder>* allocated_decoders) { |
| 2303 | for (size_t i = 0; i < allocated_decoders->size(); ++i) { |
| 2304 | if ((*allocated_decoders)[i].external) { |
pbos@webrtc.org | 776e6f2 | 2014-10-29 15:28:39 +0000 | [diff] [blame] | 2305 | external_decoder_factory_->DestroyVideoDecoder( |
pbos@webrtc.org | 96a9325 | 2014-11-03 14:46:44 +0000 | [diff] [blame] | 2306 | (*allocated_decoders)[i].decoder); |
pbos@webrtc.org | 776e6f2 | 2014-10-29 15:28:39 +0000 | [diff] [blame] | 2307 | } else { |
pbos@webrtc.org | 96a9325 | 2014-11-03 14:46:44 +0000 | [diff] [blame] | 2308 | delete (*allocated_decoders)[i].decoder; |
pbos@webrtc.org | 776e6f2 | 2014-10-29 15:28:39 +0000 | [diff] [blame] | 2309 | } |
| 2310 | } |
pbos@webrtc.org | 96a9325 | 2014-11-03 14:46:44 +0000 | [diff] [blame] | 2311 | allocated_decoders->clear(); |
pbos@webrtc.org | 776e6f2 | 2014-10-29 15:28:39 +0000 | [diff] [blame] | 2312 | } |
| 2313 | |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 2314 | void WebRtcVideoChannel2::WebRtcVideoReceiveStream::RenderFrame( |
| 2315 | const webrtc::I420VideoFrame& frame, |
| 2316 | int time_to_render_ms) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 2317 | rtc::CritScope crit(&renderer_lock_); |
magjed@webrtc.org | fc5ad95 | 2015-01-27 09:57:01 +0000 | [diff] [blame] | 2318 | |
| 2319 | if (first_frame_timestamp_ < 0) |
| 2320 | first_frame_timestamp_ = frame.timestamp(); |
| 2321 | int64_t rtp_time_elapsed_since_first_frame = |
| 2322 | (timestamp_wraparound_handler_.Unwrap(frame.timestamp()) - |
| 2323 | first_frame_timestamp_); |
| 2324 | int64_t elapsed_time_ms = rtp_time_elapsed_since_first_frame / |
| 2325 | (cricket::kVideoCodecClockrate / 1000); |
| 2326 | if (frame.ntp_time_ms() > 0) |
| 2327 | estimated_remote_start_ntp_time_ms_ = frame.ntp_time_ms() - elapsed_time_ms; |
| 2328 | |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 2329 | if (renderer_ == NULL) { |
| 2330 | LOG(LS_WARNING) << "VideoReceiveStream not connected to a VideoRenderer."; |
| 2331 | return; |
| 2332 | } |
| 2333 | |
| 2334 | if (frame.width() != last_width_ || frame.height() != last_height_) { |
| 2335 | SetSize(frame.width(), frame.height()); |
| 2336 | } |
| 2337 | |
magjed@webrtc.org | 2386d6d | 2015-03-05 14:03:08 +0000 | [diff] [blame] | 2338 | const WebRtcVideoFrame render_frame( |
| 2339 | frame.video_frame_buffer(), |
| 2340 | elapsed_time_ms * rtc::kNumNanosecsPerMillisec, |
Guo-wei Shieh | 64c1e8c | 2015-04-01 15:33:06 -0700 | [diff] [blame] | 2341 | frame.render_time_ms() * rtc::kNumNanosecsPerMillisec, frame.rotation()); |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 2342 | renderer_->RenderFrame(&render_frame); |
| 2343 | } |
| 2344 | |
pbos@webrtc.org | 0d852d5 | 2015-02-09 15:14:36 +0000 | [diff] [blame] | 2345 | bool WebRtcVideoChannel2::WebRtcVideoReceiveStream::IsTextureSupported() const { |
| 2346 | return true; |
| 2347 | } |
| 2348 | |
pbos@webrtc.org | a2a6fe6 | 2015-03-06 15:35:19 +0000 | [diff] [blame] | 2349 | bool WebRtcVideoChannel2::WebRtcVideoReceiveStream::IsDefaultStream() const { |
| 2350 | return default_stream_; |
| 2351 | } |
| 2352 | |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 2353 | void WebRtcVideoChannel2::WebRtcVideoReceiveStream::SetRenderer( |
| 2354 | cricket::VideoRenderer* renderer) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 2355 | rtc::CritScope crit(&renderer_lock_); |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 2356 | renderer_ = renderer; |
| 2357 | if (renderer_ != NULL && last_width_ != -1) { |
| 2358 | SetSize(last_width_, last_height_); |
| 2359 | } |
| 2360 | } |
| 2361 | |
| 2362 | VideoRenderer* WebRtcVideoChannel2::WebRtcVideoReceiveStream::GetRenderer() { |
| 2363 | // TODO(pbos): Remove GetRenderer and all uses of it, it's thread-unsafe by |
| 2364 | // design. |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 2365 | rtc::CritScope crit(&renderer_lock_); |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 2366 | return renderer_; |
| 2367 | } |
| 2368 | |
| 2369 | void WebRtcVideoChannel2::WebRtcVideoReceiveStream::SetSize(int width, |
| 2370 | int height) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 2371 | rtc::CritScope crit(&renderer_lock_); |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 2372 | if (!renderer_->SetSize(width, height, 0)) { |
| 2373 | LOG(LS_ERROR) << "Could not set renderer size."; |
| 2374 | } |
| 2375 | last_width_ = width; |
| 2376 | last_height_ = height; |
| 2377 | } |
| 2378 | |
pbos@webrtc.org | e6f84ae | 2014-07-18 11:11:55 +0000 | [diff] [blame] | 2379 | VideoReceiverInfo |
| 2380 | WebRtcVideoChannel2::WebRtcVideoReceiveStream::GetVideoReceiverInfo() { |
| 2381 | VideoReceiverInfo info; |
| 2382 | info.add_ssrc(config_.rtp.remote_ssrc); |
| 2383 | webrtc::VideoReceiveStream::Stats stats = stream_->GetStats(); |
asapersson@webrtc.org | cfd82df | 2015-01-22 09:39:59 +0000 | [diff] [blame] | 2384 | info.bytes_rcvd = stats.rtp_stats.transmitted.payload_bytes + |
| 2385 | stats.rtp_stats.transmitted.header_bytes + |
| 2386 | stats.rtp_stats.transmitted.padding_bytes; |
| 2387 | info.packets_rcvd = stats.rtp_stats.transmitted.packets; |
Peter Boström | 393347f | 2015-04-22 14:52:45 +0200 | [diff] [blame] | 2388 | info.packets_lost = stats.rtcp_stats.cumulative_lost; |
| 2389 | info.fraction_lost = |
| 2390 | static_cast<float>(stats.rtcp_stats.fraction_lost) / (1 << 8); |
pbos@webrtc.org | e6f84ae | 2014-07-18 11:11:55 +0000 | [diff] [blame] | 2391 | |
| 2392 | info.framerate_rcvd = stats.network_frame_rate; |
| 2393 | info.framerate_decoded = stats.decode_frame_rate; |
| 2394 | info.framerate_output = stats.render_frame_rate; |
| 2395 | |
pbos@webrtc.org | 1d0fa5d | 2015-02-19 12:47:00 +0000 | [diff] [blame] | 2396 | { |
| 2397 | rtc::CritScope frame_cs(&renderer_lock_); |
| 2398 | info.frame_width = last_width_; |
| 2399 | info.frame_height = last_height_; |
| 2400 | info.capture_start_ntp_time_ms = estimated_remote_start_ntp_time_ms_; |
| 2401 | } |
| 2402 | |
pbos@webrtc.org | 09c77b9 | 2015-02-25 10:42:16 +0000 | [diff] [blame] | 2403 | info.decode_ms = stats.decode_ms; |
| 2404 | info.max_decode_ms = stats.max_decode_ms; |
| 2405 | info.current_delay_ms = stats.current_delay_ms; |
| 2406 | info.target_delay_ms = stats.target_delay_ms; |
| 2407 | info.jitter_buffer_ms = stats.jitter_buffer_ms; |
| 2408 | info.min_playout_delay_ms = stats.min_playout_delay_ms; |
| 2409 | info.render_delay_ms = stats.render_delay_ms; |
| 2410 | |
pbos@webrtc.org | 1d0fa5d | 2015-02-19 12:47:00 +0000 | [diff] [blame] | 2411 | info.firs_sent = stats.rtcp_packet_type_counts.fir_packets; |
| 2412 | info.plis_sent = stats.rtcp_packet_type_counts.pli_packets; |
| 2413 | info.nacks_sent = stats.rtcp_packet_type_counts.nack_packets; |
pbos@webrtc.org | e6f84ae | 2014-07-18 11:11:55 +0000 | [diff] [blame] | 2414 | |
pbos@webrtc.org | e6f84ae | 2014-07-18 11:11:55 +0000 | [diff] [blame] | 2415 | return info; |
| 2416 | } |
| 2417 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 2418 | WebRtcVideoChannel2::VideoCodecSettings::VideoCodecSettings() |
| 2419 | : rtx_payload_type(-1) {} |
| 2420 | |
pbos@webrtc.org | a2ef4fe | 2014-11-07 10:54:43 +0000 | [diff] [blame] | 2421 | bool WebRtcVideoChannel2::VideoCodecSettings::operator==( |
| 2422 | const WebRtcVideoChannel2::VideoCodecSettings& other) const { |
| 2423 | return codec == other.codec && |
| 2424 | fec.ulpfec_payload_type == other.fec.ulpfec_payload_type && |
| 2425 | fec.red_payload_type == other.fec.red_payload_type && |
Shao Changbin | e62202f | 2015-04-21 20:24:50 +0800 | [diff] [blame] | 2426 | fec.red_rtx_payload_type == other.fec.red_rtx_payload_type && |
pbos@webrtc.org | a2ef4fe | 2014-11-07 10:54:43 +0000 | [diff] [blame] | 2427 | rtx_payload_type == other.rtx_payload_type; |
| 2428 | } |
| 2429 | |
Peter Boström | ee0b00e | 2015-04-22 18:41:14 +0200 | [diff] [blame] | 2430 | bool WebRtcVideoChannel2::VideoCodecSettings::operator!=( |
| 2431 | const WebRtcVideoChannel2::VideoCodecSettings& other) const { |
| 2432 | return !(*this == other); |
| 2433 | } |
| 2434 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 2435 | std::vector<WebRtcVideoChannel2::VideoCodecSettings> |
| 2436 | WebRtcVideoChannel2::MapCodecs(const std::vector<VideoCodec>& codecs) { |
| 2437 | assert(!codecs.empty()); |
| 2438 | |
| 2439 | std::vector<VideoCodecSettings> video_codecs; |
| 2440 | std::map<int, bool> payload_used; |
pbos@webrtc.org | e322a17 | 2014-06-13 11:47:28 +0000 | [diff] [blame] | 2441 | std::map<int, VideoCodec::CodecType> payload_codec_type; |
pkasting@chromium.org | d324546 | 2015-02-23 21:28:22 +0000 | [diff] [blame] | 2442 | // |rtx_mapping| maps video payload type to rtx payload type. |
| 2443 | std::map<int, int> rtx_mapping; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 2444 | |
| 2445 | webrtc::FecConfig fec_settings; |
| 2446 | |
| 2447 | for (size_t i = 0; i < codecs.size(); ++i) { |
| 2448 | const VideoCodec& in_codec = codecs[i]; |
| 2449 | int payload_type = in_codec.id; |
| 2450 | |
| 2451 | if (payload_used[payload_type]) { |
| 2452 | LOG(LS_ERROR) << "Payload type already registered: " |
| 2453 | << in_codec.ToString(); |
| 2454 | return std::vector<VideoCodecSettings>(); |
| 2455 | } |
| 2456 | payload_used[payload_type] = true; |
pbos@webrtc.org | e322a17 | 2014-06-13 11:47:28 +0000 | [diff] [blame] | 2457 | payload_codec_type[payload_type] = in_codec.GetCodecType(); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 2458 | |
| 2459 | switch (in_codec.GetCodecType()) { |
| 2460 | case VideoCodec::CODEC_RED: { |
| 2461 | // RED payload type, should not have duplicates. |
| 2462 | assert(fec_settings.red_payload_type == -1); |
| 2463 | fec_settings.red_payload_type = in_codec.id; |
| 2464 | continue; |
| 2465 | } |
| 2466 | |
| 2467 | case VideoCodec::CODEC_ULPFEC: { |
| 2468 | // ULPFEC payload type, should not have duplicates. |
| 2469 | assert(fec_settings.ulpfec_payload_type == -1); |
| 2470 | fec_settings.ulpfec_payload_type = in_codec.id; |
| 2471 | continue; |
| 2472 | } |
| 2473 | |
| 2474 | case VideoCodec::CODEC_RTX: { |
| 2475 | int associated_payload_type; |
| 2476 | if (!in_codec.GetParam(kCodecParamAssociatedPayloadType, |
pkasting@chromium.org | e9facf8 | 2015-02-17 20:36:28 +0000 | [diff] [blame] | 2477 | &associated_payload_type) || |
| 2478 | !IsValidRtpPayloadType(associated_payload_type)) { |
| 2479 | LOG(LS_ERROR) |
| 2480 | << "RTX codec with invalid or no associated payload type: " |
| 2481 | << in_codec.ToString(); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 2482 | return std::vector<VideoCodecSettings>(); |
| 2483 | } |
| 2484 | rtx_mapping[associated_payload_type] = in_codec.id; |
| 2485 | continue; |
| 2486 | } |
| 2487 | |
| 2488 | case VideoCodec::CODEC_VIDEO: |
| 2489 | break; |
| 2490 | } |
| 2491 | |
| 2492 | video_codecs.push_back(VideoCodecSettings()); |
| 2493 | video_codecs.back().codec = in_codec; |
| 2494 | } |
| 2495 | |
| 2496 | // One of these codecs should have been a video codec. Only having FEC |
| 2497 | // parameters into this code is a logic error. |
| 2498 | assert(!video_codecs.empty()); |
| 2499 | |
pbos@webrtc.org | e322a17 | 2014-06-13 11:47:28 +0000 | [diff] [blame] | 2500 | for (std::map<int, int>::const_iterator it = rtx_mapping.begin(); |
| 2501 | it != rtx_mapping.end(); |
| 2502 | ++it) { |
| 2503 | if (!payload_used[it->first]) { |
| 2504 | LOG(LS_ERROR) << "RTX mapped to payload not in codec list."; |
| 2505 | return std::vector<VideoCodecSettings>(); |
| 2506 | } |
Shao Changbin | e62202f | 2015-04-21 20:24:50 +0800 | [diff] [blame] | 2507 | if (payload_codec_type[it->first] != VideoCodec::CODEC_VIDEO && |
| 2508 | payload_codec_type[it->first] != VideoCodec::CODEC_RED) { |
| 2509 | LOG(LS_ERROR) << "RTX not mapped to regular video codec or RED codec."; |
pbos@webrtc.org | e322a17 | 2014-06-13 11:47:28 +0000 | [diff] [blame] | 2510 | return std::vector<VideoCodecSettings>(); |
| 2511 | } |
Shao Changbin | e62202f | 2015-04-21 20:24:50 +0800 | [diff] [blame] | 2512 | |
| 2513 | if (it->first == fec_settings.red_payload_type) { |
| 2514 | fec_settings.red_rtx_payload_type = it->second; |
| 2515 | } |
pbos@webrtc.org | e322a17 | 2014-06-13 11:47:28 +0000 | [diff] [blame] | 2516 | } |
| 2517 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 2518 | for (size_t i = 0; i < video_codecs.size(); ++i) { |
| 2519 | video_codecs[i].fec = fec_settings; |
Shao Changbin | e62202f | 2015-04-21 20:24:50 +0800 | [diff] [blame] | 2520 | if (rtx_mapping[video_codecs[i].codec.id] != 0 && |
| 2521 | rtx_mapping[video_codecs[i].codec.id] != |
| 2522 | fec_settings.red_payload_type) { |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 2523 | video_codecs[i].rtx_payload_type = rtx_mapping[video_codecs[i].codec.id]; |
| 2524 | } |
| 2525 | } |
| 2526 | |
| 2527 | return video_codecs; |
| 2528 | } |
| 2529 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 2530 | } // namespace cricket |
| 2531 | |
| 2532 | #endif // HAVE_WEBRTC_VIDEO |