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 = |
Fredrik Solenberg | 4b60c73 | 2015-05-07 14:07:48 +0200 | [diff] [blame^] | 613 | new WebRtcVideoChannel2(call_factory_, voice_engine_, |
| 614 | static_cast<WebRtcVoiceMediaChannel*>(voice_channel), options, |
| 615 | external_encoder_factory_, external_decoder_factory_); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 616 | if (!channel->Init()) { |
| 617 | delete channel; |
| 618 | return NULL; |
| 619 | } |
pbos@webrtc.org | e322a17 | 2014-06-13 11:47:28 +0000 | [diff] [blame] | 620 | channel->SetRecvCodecs(video_codecs_); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 621 | return channel; |
| 622 | } |
| 623 | |
| 624 | const std::vector<VideoCodec>& WebRtcVideoEngine2::codecs() const { |
| 625 | return video_codecs_; |
| 626 | } |
| 627 | |
| 628 | const std::vector<RtpHeaderExtension>& |
| 629 | WebRtcVideoEngine2::rtp_header_extensions() const { |
| 630 | return rtp_header_extensions_; |
| 631 | } |
| 632 | |
| 633 | void WebRtcVideoEngine2::SetLogging(int min_sev, const char* filter) { |
| 634 | // TODO(pbos): Set up logging. |
| 635 | LOG(LS_VERBOSE) << "SetLogging: " << min_sev << '"' << filter << '"'; |
| 636 | // if min_sev == -1, we keep the current log level. |
| 637 | if (min_sev < 0) { |
| 638 | assert(min_sev == -1); |
| 639 | return; |
| 640 | } |
| 641 | } |
| 642 | |
pbos@webrtc.org | 0a2087a | 2014-09-23 09:40:22 +0000 | [diff] [blame] | 643 | void WebRtcVideoEngine2::SetExternalDecoderFactory( |
| 644 | WebRtcVideoDecoderFactory* decoder_factory) { |
pbos@webrtc.org | 7fe1e03 | 2014-10-14 04:25:33 +0000 | [diff] [blame] | 645 | assert(!initialized_); |
pbos@webrtc.org | 0a2087a | 2014-09-23 09:40:22 +0000 | [diff] [blame] | 646 | external_decoder_factory_ = decoder_factory; |
| 647 | } |
| 648 | |
| 649 | void WebRtcVideoEngine2::SetExternalEncoderFactory( |
| 650 | WebRtcVideoEncoderFactory* encoder_factory) { |
pbos@webrtc.org | 7fe1e03 | 2014-10-14 04:25:33 +0000 | [diff] [blame] | 651 | assert(!initialized_); |
pbos@webrtc.org | f18fba2 | 2015-01-14 16:26:23 +0000 | [diff] [blame] | 652 | if (external_encoder_factory_ == encoder_factory) |
| 653 | return; |
| 654 | |
| 655 | // No matter what happens we shouldn't hold on to a stale |
| 656 | // WebRtcSimulcastEncoderFactory. |
| 657 | simulcast_encoder_factory_.reset(); |
| 658 | |
| 659 | if (encoder_factory && |
| 660 | WebRtcSimulcastEncoderFactory::UseSimulcastEncoderFactory( |
| 661 | encoder_factory->codecs())) { |
| 662 | simulcast_encoder_factory_.reset( |
| 663 | new WebRtcSimulcastEncoderFactory(encoder_factory)); |
| 664 | encoder_factory = simulcast_encoder_factory_.get(); |
| 665 | } |
pbos@webrtc.org | 0a2087a | 2014-09-23 09:40:22 +0000 | [diff] [blame] | 666 | external_encoder_factory_ = encoder_factory; |
pbos@webrtc.org | 7fe1e03 | 2014-10-14 04:25:33 +0000 | [diff] [blame] | 667 | |
| 668 | video_codecs_ = GetSupportedCodecs(); |
pbos@webrtc.org | 0a2087a | 2014-09-23 09:40:22 +0000 | [diff] [blame] | 669 | } |
| 670 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 671 | bool WebRtcVideoEngine2::EnableTimedRender() { |
| 672 | // TODO(pbos): Figure out whether this can be removed. |
| 673 | return true; |
| 674 | } |
| 675 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 676 | // Checks to see whether we comprehend and could receive a particular codec |
| 677 | bool WebRtcVideoEngine2::FindCodec(const VideoCodec& in) { |
| 678 | // TODO(pbos): Probe encoder factory to figure out that the codec is supported |
| 679 | // if supported by the encoder factory. Add a corresponding test that fails |
| 680 | // with this code (that doesn't ask the factory). |
pbos@webrtc.org | 8fdeee6 | 2014-07-20 14:40:23 +0000 | [diff] [blame] | 681 | for (size_t j = 0; j < video_codecs_.size(); ++j) { |
| 682 | VideoCodec codec(video_codecs_[j].id, video_codecs_[j].name, 0, 0, 0, 0); |
| 683 | if (codec.Matches(in)) { |
| 684 | return true; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 685 | } |
| 686 | } |
| 687 | return false; |
| 688 | } |
| 689 | |
| 690 | // Tells whether the |requested| codec can be transmitted or not. If it can be |
| 691 | // transmitted |out| is set with the best settings supported. Aspect ratio will |
| 692 | // be set as close to |current|'s as possible. If not set |requested|'s |
| 693 | // dimensions will be used for aspect ratio matching. |
| 694 | bool WebRtcVideoEngine2::CanSendCodec(const VideoCodec& requested, |
| 695 | const VideoCodec& current, |
| 696 | VideoCodec* out) { |
| 697 | assert(out != NULL); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 698 | |
| 699 | if (requested.width != requested.height && |
| 700 | (requested.height == 0 || requested.width == 0)) { |
| 701 | // 0xn and nx0 are invalid resolutions. |
| 702 | return false; |
| 703 | } |
| 704 | |
| 705 | VideoCodec matching_codec; |
| 706 | if (!FindFirstMatchingCodec(video_codecs_, requested, &matching_codec)) { |
| 707 | // Codec not supported. |
| 708 | return false; |
| 709 | } |
| 710 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 711 | out->id = requested.id; |
| 712 | out->name = requested.name; |
| 713 | out->preference = requested.preference; |
| 714 | out->params = requested.params; |
andresp@webrtc.org | ff689be | 2015-02-12 11:54:26 +0000 | [diff] [blame] | 715 | out->framerate = std::min(requested.framerate, matching_codec.framerate); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 716 | out->params = requested.params; |
| 717 | out->feedback_params = requested.feedback_params; |
pbos@webrtc.org | 8fdeee6 | 2014-07-20 14:40:23 +0000 | [diff] [blame] | 718 | out->width = requested.width; |
| 719 | out->height = requested.height; |
| 720 | if (requested.width == 0 && requested.height == 0) { |
| 721 | return true; |
| 722 | } |
| 723 | |
| 724 | while (out->width > matching_codec.width) { |
| 725 | out->width /= 2; |
| 726 | out->height /= 2; |
| 727 | } |
| 728 | |
| 729 | return out->width > 0 && out->height > 0; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 730 | } |
| 731 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 732 | // Ignore spammy trace messages, mostly from the stats API when we haven't |
| 733 | // gotten RTCP info yet from the remote side. |
| 734 | bool WebRtcVideoEngine2::ShouldIgnoreTrace(const std::string& trace) { |
| 735 | static const char* const kTracesToIgnore[] = {NULL}; |
| 736 | for (const char* const* p = kTracesToIgnore; *p; ++p) { |
| 737 | if (trace.find(*p) == 0) { |
| 738 | return true; |
| 739 | } |
| 740 | } |
| 741 | return false; |
| 742 | } |
| 743 | |
pbos@webrtc.org | 7fe1e03 | 2014-10-14 04:25:33 +0000 | [diff] [blame] | 744 | std::vector<VideoCodec> WebRtcVideoEngine2::GetSupportedCodecs() const { |
andresp@webrtc.org | 82775b1 | 2014-11-07 09:37:54 +0000 | [diff] [blame] | 745 | std::vector<VideoCodec> supported_codecs = DefaultVideoCodecList(); |
pbos@webrtc.org | 7fe1e03 | 2014-10-14 04:25:33 +0000 | [diff] [blame] | 746 | |
| 747 | if (external_encoder_factory_ == NULL) { |
| 748 | return supported_codecs; |
| 749 | } |
| 750 | |
pbos@webrtc.org | 7fe1e03 | 2014-10-14 04:25:33 +0000 | [diff] [blame] | 751 | const std::vector<WebRtcVideoEncoderFactory::VideoCodec>& codecs = |
| 752 | external_encoder_factory_->codecs(); |
| 753 | for (size_t i = 0; i < codecs.size(); ++i) { |
| 754 | // Don't add internally-supported codecs twice. |
| 755 | if (CodecIsInternallySupported(codecs[i].name)) { |
| 756 | continue; |
| 757 | } |
| 758 | |
pkasting@chromium.org | d324546 | 2015-02-23 21:28:22 +0000 | [diff] [blame] | 759 | // External video encoders are given payloads 120-127. This also means that |
| 760 | // we only support up to 8 external payload types. |
| 761 | const int kExternalVideoPayloadTypeBase = 120; |
| 762 | size_t payload_type = kExternalVideoPayloadTypeBase + i; |
| 763 | assert(payload_type < 128); |
| 764 | VideoCodec codec(static_cast<int>(payload_type), |
pbos@webrtc.org | 7fe1e03 | 2014-10-14 04:25:33 +0000 | [diff] [blame] | 765 | codecs[i].name, |
| 766 | codecs[i].max_width, |
| 767 | codecs[i].max_height, |
| 768 | codecs[i].max_fps, |
| 769 | 0); |
| 770 | |
| 771 | AddDefaultFeedbackParams(&codec); |
| 772 | supported_codecs.push_back(codec); |
| 773 | } |
| 774 | return supported_codecs; |
| 775 | } |
| 776 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 777 | WebRtcVideoChannel2::WebRtcVideoChannel2( |
pbos@webrtc.org | 42684be | 2014-10-03 11:25:45 +0000 | [diff] [blame] | 778 | WebRtcCallFactory* call_factory, |
pbos@webrtc.org | 3bf3d23 | 2014-10-31 12:59:34 +0000 | [diff] [blame] | 779 | WebRtcVoiceEngine* voice_engine, |
Fredrik Solenberg | 4b60c73 | 2015-05-07 14:07:48 +0200 | [diff] [blame^] | 780 | WebRtcVoiceMediaChannel* voice_channel, |
pbos@webrtc.org | fa553ef | 2014-10-20 11:07:07 +0000 | [diff] [blame] | 781 | const VideoOptions& options, |
pbos@webrtc.org | 7fe1e03 | 2014-10-14 04:25:33 +0000 | [diff] [blame] | 782 | WebRtcVideoEncoderFactory* external_encoder_factory, |
pbos@webrtc.org | f1c8b90 | 2015-01-14 17:29:27 +0000 | [diff] [blame] | 783 | WebRtcVideoDecoderFactory* external_decoder_factory) |
pbos@webrtc.org | b648b9d | 2014-08-26 11:08:06 +0000 | [diff] [blame] | 784 | : unsignalled_ssrc_handler_(&default_unsignalled_ssrc_handler_), |
Fredrik Solenberg | 4b60c73 | 2015-05-07 14:07:48 +0200 | [diff] [blame^] | 785 | voice_channel_(voice_channel), |
| 786 | voice_channel_id_(voice_channel ? voice_channel->voe_channel() : -1), |
pbos@webrtc.org | 7fe1e03 | 2014-10-14 04:25:33 +0000 | [diff] [blame] | 787 | external_encoder_factory_(external_encoder_factory), |
pbos@webrtc.org | f1c8b90 | 2015-01-14 17:29:27 +0000 | [diff] [blame] | 788 | external_decoder_factory_(external_decoder_factory) { |
Fredrik Solenberg | 4b60c73 | 2015-05-07 14:07:48 +0200 | [diff] [blame^] | 789 | DCHECK(thread_checker_.CalledOnValidThread()); |
pbos@webrtc.org | fa553ef | 2014-10-20 11:07:07 +0000 | [diff] [blame] | 790 | SetDefaultOptions(); |
| 791 | options_.SetAll(options); |
Peter Boström | e7b221f | 2015-04-13 15:34:32 +0200 | [diff] [blame] | 792 | options_.cpu_overuse_detection.Get(&signal_cpu_adaptation_); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 793 | webrtc::Call::Config config(this); |
pbos@webrtc.org | 42684be | 2014-10-03 11:25:45 +0000 | [diff] [blame] | 794 | config.overuse_callback = this; |
pbos@webrtc.org | 3bf3d23 | 2014-10-31 12:59:34 +0000 | [diff] [blame] | 795 | if (voice_engine != NULL) { |
| 796 | config.voice_engine = voice_engine->voe()->engine(); |
| 797 | } |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 798 | config.bitrate_config.min_bitrate_bps = kMinBandwidthBps; |
| 799 | config.bitrate_config.start_bitrate_bps = kStartBandwidthBps; |
| 800 | config.bitrate_config.max_bitrate_bps = kMaxBandwidthBps; |
pbos@webrtc.org | 42684be | 2014-10-03 11:25:45 +0000 | [diff] [blame] | 801 | call_.reset(call_factory->CreateCall(config)); |
Fredrik Solenberg | 4b60c73 | 2015-05-07 14:07:48 +0200 | [diff] [blame^] | 802 | if (voice_channel_) { |
| 803 | voice_channel_->SetCall(call_.get()); |
| 804 | } |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 805 | rtcp_receiver_report_ssrc_ = kDefaultRtcpReceiverReportSsrc; |
| 806 | sending_ = false; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 807 | default_send_ssrc_ = 0; |
pbos@webrtc.org | 6f48f1b | 2014-07-22 16:29:54 +0000 | [diff] [blame] | 808 | } |
| 809 | |
| 810 | void WebRtcVideoChannel2::SetDefaultOptions() { |
Peter Boström | e432800 | 2015-04-14 22:45:29 +0200 | [diff] [blame] | 811 | options_.cpu_overuse_detection.Set(true); |
pbos@webrtc.org | d819803 | 2014-11-10 14:41:43 +0000 | [diff] [blame] | 812 | options_.dscp.Set(false); |
pbos@webrtc.org | 5ff71ab | 2014-07-23 07:28:56 +0000 | [diff] [blame] | 813 | options_.suspend_below_min_bitrate.Set(false); |
pbos@webrtc.org | 42684be | 2014-10-03 11:25:45 +0000 | [diff] [blame] | 814 | options_.video_noise_reduction.Set(true); |
pbos@webrtc.org | efc82c2 | 2014-10-27 13:58:00 +0000 | [diff] [blame] | 815 | options_.screencast_min_bitrate.Set(0); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 816 | } |
| 817 | |
| 818 | WebRtcVideoChannel2::~WebRtcVideoChannel2() { |
Fredrik Solenberg | 4b60c73 | 2015-05-07 14:07:48 +0200 | [diff] [blame^] | 819 | DetachVoiceChannel(); |
Peter Boström | dfd53fe | 2015-03-27 15:58:11 +0100 | [diff] [blame] | 820 | for (auto& kv : send_streams_) |
| 821 | delete kv.second; |
| 822 | for (auto& kv : receive_streams_) |
| 823 | delete kv.second; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 824 | } |
| 825 | |
| 826 | bool WebRtcVideoChannel2::Init() { return true; } |
| 827 | |
Fredrik Solenberg | 4b60c73 | 2015-05-07 14:07:48 +0200 | [diff] [blame^] | 828 | void WebRtcVideoChannel2::DetachVoiceChannel() { |
| 829 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 830 | if (voice_channel_) { |
| 831 | voice_channel_->SetCall(nullptr); |
| 832 | voice_channel_ = nullptr; |
| 833 | } |
| 834 | } |
| 835 | |
pbos@webrtc.org | 96a9325 | 2014-11-03 14:46:44 +0000 | [diff] [blame] | 836 | bool WebRtcVideoChannel2::CodecIsExternallySupported( |
| 837 | const std::string& name) const { |
| 838 | if (external_encoder_factory_ == NULL) { |
| 839 | return false; |
| 840 | } |
| 841 | |
| 842 | const std::vector<WebRtcVideoEncoderFactory::VideoCodec> external_codecs = |
| 843 | external_encoder_factory_->codecs(); |
| 844 | for (size_t c = 0; c < external_codecs.size(); ++c) { |
Shao Changbin | e62202f | 2015-04-21 20:24:50 +0800 | [diff] [blame] | 845 | if (CodecNamesEq(name, external_codecs[c].name)) { |
pbos@webrtc.org | 96a9325 | 2014-11-03 14:46:44 +0000 | [diff] [blame] | 846 | return true; |
| 847 | } |
| 848 | } |
| 849 | return false; |
| 850 | } |
| 851 | |
| 852 | std::vector<WebRtcVideoChannel2::VideoCodecSettings> |
| 853 | WebRtcVideoChannel2::FilterSupportedCodecs( |
| 854 | const std::vector<WebRtcVideoChannel2::VideoCodecSettings>& mapped_codecs) |
| 855 | const { |
| 856 | std::vector<VideoCodecSettings> supported_codecs; |
| 857 | for (size_t i = 0; i < mapped_codecs.size(); ++i) { |
| 858 | const VideoCodecSettings& codec = mapped_codecs[i]; |
| 859 | if (CodecIsInternallySupported(codec.codec.name) || |
| 860 | CodecIsExternallySupported(codec.codec.name)) { |
| 861 | supported_codecs.push_back(codec); |
| 862 | } |
| 863 | } |
| 864 | return supported_codecs; |
| 865 | } |
| 866 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 867 | bool WebRtcVideoChannel2::SetRecvCodecs(const std::vector<VideoCodec>& codecs) { |
pbos@webrtc.org | 50fe359 | 2015-01-29 12:33:07 +0000 | [diff] [blame] | 868 | TRACE_EVENT0("webrtc", "WebRtcVideoChannel2::SetRecvCodecs"); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 869 | LOG(LS_INFO) << "SetRecvCodecs: " << CodecVectorToString(codecs); |
| 870 | if (!ValidateCodecFormats(codecs)) { |
| 871 | return false; |
| 872 | } |
| 873 | |
| 874 | const std::vector<VideoCodecSettings> mapped_codecs = MapCodecs(codecs); |
| 875 | if (mapped_codecs.empty()) { |
pbos@webrtc.org | 96a9325 | 2014-11-03 14:46:44 +0000 | [diff] [blame] | 876 | LOG(LS_ERROR) << "SetRecvCodecs called without any video codecs."; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 877 | return false; |
| 878 | } |
| 879 | |
pbos@webrtc.org | 96a9325 | 2014-11-03 14:46:44 +0000 | [diff] [blame] | 880 | const std::vector<VideoCodecSettings> supported_codecs = |
| 881 | FilterSupportedCodecs(mapped_codecs); |
| 882 | |
| 883 | if (mapped_codecs.size() != supported_codecs.size()) { |
| 884 | LOG(LS_ERROR) << "SetRecvCodecs called with unsupported video codecs."; |
| 885 | return false; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 886 | } |
| 887 | |
Peter Boström | ee0b00e | 2015-04-22 18:41:14 +0200 | [diff] [blame] | 888 | // Prevent reconfiguration when setting identical receive codecs. |
| 889 | if (recv_codecs_.size() == supported_codecs.size()) { |
| 890 | bool reconfigured = false; |
| 891 | for (size_t i = 0; i < supported_codecs.size(); ++i) { |
| 892 | if (recv_codecs_[i] != supported_codecs[i]) { |
| 893 | reconfigured = true; |
| 894 | break; |
| 895 | } |
| 896 | } |
| 897 | if (!reconfigured) |
| 898 | return true; |
| 899 | } |
| 900 | |
pbos@webrtc.org | 96a9325 | 2014-11-03 14:46:44 +0000 | [diff] [blame] | 901 | recv_codecs_ = supported_codecs; |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 902 | |
pbos@webrtc.org | 575d126 | 2014-10-08 14:48:08 +0000 | [diff] [blame] | 903 | rtc::CritScope stream_lock(&stream_crit_); |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 904 | for (std::map<uint32, WebRtcVideoReceiveStream*>::iterator it = |
| 905 | receive_streams_.begin(); |
| 906 | it != receive_streams_.end(); |
| 907 | ++it) { |
| 908 | it->second->SetRecvCodecs(recv_codecs_); |
| 909 | } |
| 910 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 911 | return true; |
| 912 | } |
| 913 | |
| 914 | bool WebRtcVideoChannel2::SetSendCodecs(const std::vector<VideoCodec>& codecs) { |
pbos@webrtc.org | 50fe359 | 2015-01-29 12:33:07 +0000 | [diff] [blame] | 915 | TRACE_EVENT0("webrtc", "WebRtcVideoChannel2::SetSendCodecs"); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 916 | LOG(LS_INFO) << "SetSendCodecs: " << CodecVectorToString(codecs); |
| 917 | if (!ValidateCodecFormats(codecs)) { |
| 918 | return false; |
| 919 | } |
| 920 | |
| 921 | const std::vector<VideoCodecSettings> supported_codecs = |
| 922 | FilterSupportedCodecs(MapCodecs(codecs)); |
| 923 | |
| 924 | if (supported_codecs.empty()) { |
Peter Boström | 3c3f646 | 2015-04-15 16:27:49 +0200 | [diff] [blame] | 925 | LOG(LS_ERROR) << "No video codecs supported."; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 926 | return false; |
| 927 | } |
| 928 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 929 | LOG(LS_INFO) << "Using codec: " << supported_codecs.front().codec.ToString(); |
| 930 | |
pbos@webrtc.org | a2ef4fe | 2014-11-07 10:54:43 +0000 | [diff] [blame] | 931 | VideoCodecSettings old_codec; |
| 932 | if (send_codec_.Get(&old_codec) && supported_codecs.front() == old_codec) { |
| 933 | // Using same codec, avoid reconfiguring. |
| 934 | return true; |
| 935 | } |
| 936 | |
| 937 | send_codec_.Set(supported_codecs.front()); |
| 938 | |
pbos@webrtc.org | 575d126 | 2014-10-08 14:48:08 +0000 | [diff] [blame] | 939 | rtc::CritScope stream_lock(&stream_crit_); |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 940 | for (std::map<uint32, WebRtcVideoSendStream*>::iterator it = |
| 941 | send_streams_.begin(); |
| 942 | it != send_streams_.end(); |
| 943 | ++it) { |
| 944 | assert(it->second != NULL); |
| 945 | it->second->SetCodec(supported_codecs.front()); |
| 946 | } |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 947 | |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 948 | // TODO(holmer): Changing the codec parameters shouldn't necessarily mean that |
| 949 | // we change the min/max of bandwidth estimation. Reevaluate this. |
pbos@webrtc.org | 0087318 | 2014-11-25 14:03:34 +0000 | [diff] [blame] | 950 | VideoCodec codec = supported_codecs.front().codec; |
| 951 | int bitrate_kbps; |
| 952 | if (codec.GetParam(kCodecParamMinBitrate, &bitrate_kbps) && |
| 953 | bitrate_kbps > 0) { |
| 954 | bitrate_config_.min_bitrate_bps = bitrate_kbps * 1000; |
| 955 | } else { |
| 956 | bitrate_config_.min_bitrate_bps = 0; |
| 957 | } |
| 958 | if (codec.GetParam(kCodecParamStartBitrate, &bitrate_kbps) && |
| 959 | bitrate_kbps > 0) { |
| 960 | bitrate_config_.start_bitrate_bps = bitrate_kbps * 1000; |
| 961 | } else { |
| 962 | // Do not reconfigure start bitrate unless it's specified and positive. |
| 963 | bitrate_config_.start_bitrate_bps = -1; |
| 964 | } |
| 965 | if (codec.GetParam(kCodecParamMaxBitrate, &bitrate_kbps) && |
| 966 | bitrate_kbps > 0) { |
| 967 | bitrate_config_.max_bitrate_bps = bitrate_kbps * 1000; |
| 968 | } else { |
| 969 | bitrate_config_.max_bitrate_bps = -1; |
| 970 | } |
| 971 | call_->SetBitrateConfig(bitrate_config_); |
| 972 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 973 | return true; |
| 974 | } |
| 975 | |
| 976 | bool WebRtcVideoChannel2::GetSendCodec(VideoCodec* codec) { |
| 977 | VideoCodecSettings codec_settings; |
| 978 | if (!send_codec_.Get(&codec_settings)) { |
| 979 | LOG(LS_VERBOSE) << "GetSendCodec: No send codec set."; |
| 980 | return false; |
| 981 | } |
| 982 | *codec = codec_settings.codec; |
| 983 | return true; |
| 984 | } |
| 985 | |
| 986 | bool WebRtcVideoChannel2::SetSendStreamFormat(uint32 ssrc, |
| 987 | const VideoFormat& format) { |
| 988 | LOG(LS_VERBOSE) << "SetSendStreamFormat:" << ssrc << " -> " |
| 989 | << format.ToString(); |
pbos@webrtc.org | 575d126 | 2014-10-08 14:48:08 +0000 | [diff] [blame] | 990 | rtc::CritScope stream_lock(&stream_crit_); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 991 | if (send_streams_.find(ssrc) == send_streams_.end()) { |
| 992 | return false; |
| 993 | } |
| 994 | return send_streams_[ssrc]->SetVideoFormat(format); |
| 995 | } |
| 996 | |
| 997 | bool WebRtcVideoChannel2::SetRender(bool render) { |
| 998 | // TODO(pbos): Implement. Or refactor away as it shouldn't be needed. |
| 999 | LOG(LS_VERBOSE) << "SetRender: " << (render ? "true" : "false"); |
| 1000 | return true; |
| 1001 | } |
| 1002 | |
| 1003 | bool WebRtcVideoChannel2::SetSend(bool send) { |
| 1004 | LOG(LS_VERBOSE) << "SetSend: " << (send ? "true" : "false"); |
| 1005 | if (send && !send_codec_.IsSet()) { |
| 1006 | LOG(LS_ERROR) << "SetSend(true) called before setting codec."; |
| 1007 | return false; |
| 1008 | } |
| 1009 | if (send) { |
| 1010 | StartAllSendStreams(); |
| 1011 | } else { |
| 1012 | StopAllSendStreams(); |
| 1013 | } |
| 1014 | sending_ = send; |
| 1015 | return true; |
| 1016 | } |
| 1017 | |
Peter Boström | d6f4c25 | 2015-03-26 16:23:04 +0100 | [diff] [blame] | 1018 | bool WebRtcVideoChannel2::ValidateSendSsrcAvailability( |
| 1019 | const StreamParams& sp) const { |
| 1020 | for (uint32_t ssrc: sp.ssrcs) { |
| 1021 | if (send_ssrcs_.find(ssrc) != send_ssrcs_.end()) { |
| 1022 | LOG(LS_ERROR) << "Send stream with SSRC '" << ssrc << "' already exists."; |
| 1023 | return false; |
| 1024 | } |
| 1025 | } |
| 1026 | return true; |
| 1027 | } |
| 1028 | |
| 1029 | bool WebRtcVideoChannel2::ValidateReceiveSsrcAvailability( |
| 1030 | const StreamParams& sp) const { |
| 1031 | for (uint32_t ssrc: sp.ssrcs) { |
| 1032 | if (receive_ssrcs_.find(ssrc) != receive_ssrcs_.end()) { |
| 1033 | LOG(LS_ERROR) << "Receive stream with SSRC '" << ssrc |
| 1034 | << "' already exists."; |
| 1035 | return false; |
| 1036 | } |
| 1037 | } |
| 1038 | return true; |
| 1039 | } |
| 1040 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1041 | bool WebRtcVideoChannel2::AddSendStream(const StreamParams& sp) { |
| 1042 | LOG(LS_INFO) << "AddSendStream: " << sp.ToString(); |
Peter Boström | d4362cd | 2015-03-25 14:17:23 +0100 | [diff] [blame] | 1043 | if (!ValidateStreamParams(sp)) |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1044 | return false; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1045 | |
pbos@webrtc.org | 575d126 | 2014-10-08 14:48:08 +0000 | [diff] [blame] | 1046 | rtc::CritScope stream_lock(&stream_crit_); |
Peter Boström | d6f4c25 | 2015-03-26 16:23:04 +0100 | [diff] [blame] | 1047 | |
| 1048 | if (!ValidateSendSsrcAvailability(sp)) |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1049 | return false; |
Peter Boström | d6f4c25 | 2015-03-26 16:23:04 +0100 | [diff] [blame] | 1050 | |
| 1051 | for (uint32 used_ssrc : sp.ssrcs) |
| 1052 | send_ssrcs_.insert(used_ssrc); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1053 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1054 | WebRtcVideoSendStream* stream = |
pbos@webrtc.org | 6ae48c6 | 2014-06-06 10:49:19 +0000 | [diff] [blame] | 1055 | new WebRtcVideoSendStream(call_.get(), |
pbos@webrtc.org | 7fe1e03 | 2014-10-14 04:25:33 +0000 | [diff] [blame] | 1056 | external_encoder_factory_, |
pbos@webrtc.org | 6ae48c6 | 2014-06-06 10:49:19 +0000 | [diff] [blame] | 1057 | options_, |
Peter Boström | dfd53fe | 2015-03-27 15:58:11 +0100 | [diff] [blame] | 1058 | bitrate_config_.max_bitrate_bps, |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1059 | send_codec_, |
| 1060 | sp, |
| 1061 | send_rtp_extensions_); |
| 1062 | |
Peter Boström | d6f4c25 | 2015-03-26 16:23:04 +0100 | [diff] [blame] | 1063 | uint32 ssrc = sp.first_ssrc(); |
| 1064 | assert(ssrc != 0); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1065 | send_streams_[ssrc] = stream; |
| 1066 | |
| 1067 | if (rtcp_receiver_report_ssrc_ == kDefaultRtcpReceiverReportSsrc) { |
| 1068 | rtcp_receiver_report_ssrc_ = ssrc; |
| 1069 | } |
| 1070 | if (default_send_ssrc_ == 0) { |
| 1071 | default_send_ssrc_ = ssrc; |
| 1072 | } |
| 1073 | if (sending_) { |
| 1074 | stream->Start(); |
| 1075 | } |
| 1076 | |
| 1077 | return true; |
| 1078 | } |
| 1079 | |
| 1080 | bool WebRtcVideoChannel2::RemoveSendStream(uint32 ssrc) { |
| 1081 | LOG(LS_INFO) << "RemoveSendStream: " << ssrc; |
| 1082 | |
| 1083 | if (ssrc == 0) { |
| 1084 | if (default_send_ssrc_ == 0) { |
| 1085 | LOG(LS_ERROR) << "No default send stream active."; |
| 1086 | return false; |
| 1087 | } |
| 1088 | |
| 1089 | LOG(LS_VERBOSE) << "Removing default stream: " << default_send_ssrc_; |
| 1090 | ssrc = default_send_ssrc_; |
| 1091 | } |
| 1092 | |
pbos@webrtc.org | 575d126 | 2014-10-08 14:48:08 +0000 | [diff] [blame] | 1093 | WebRtcVideoSendStream* removed_stream; |
| 1094 | { |
| 1095 | rtc::CritScope stream_lock(&stream_crit_); |
| 1096 | std::map<uint32, WebRtcVideoSendStream*>::iterator it = |
| 1097 | send_streams_.find(ssrc); |
| 1098 | if (it == send_streams_.end()) { |
| 1099 | return false; |
| 1100 | } |
| 1101 | |
Peter Boström | d6f4c25 | 2015-03-26 16:23:04 +0100 | [diff] [blame] | 1102 | for (uint32 old_ssrc : it->second->GetSsrcs()) |
| 1103 | send_ssrcs_.erase(old_ssrc); |
| 1104 | |
pbos@webrtc.org | 575d126 | 2014-10-08 14:48:08 +0000 | [diff] [blame] | 1105 | removed_stream = it->second; |
| 1106 | send_streams_.erase(it); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1107 | } |
| 1108 | |
pbos@webrtc.org | 575d126 | 2014-10-08 14:48:08 +0000 | [diff] [blame] | 1109 | delete removed_stream; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1110 | |
| 1111 | if (ssrc == default_send_ssrc_) { |
| 1112 | default_send_ssrc_ = 0; |
| 1113 | } |
| 1114 | |
| 1115 | return true; |
| 1116 | } |
| 1117 | |
Peter Boström | d6f4c25 | 2015-03-26 16:23:04 +0100 | [diff] [blame] | 1118 | void WebRtcVideoChannel2::DeleteReceiveStream( |
| 1119 | WebRtcVideoChannel2::WebRtcVideoReceiveStream* stream) { |
| 1120 | for (uint32 old_ssrc : stream->GetSsrcs()) |
| 1121 | receive_ssrcs_.erase(old_ssrc); |
| 1122 | delete stream; |
| 1123 | } |
| 1124 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1125 | bool WebRtcVideoChannel2::AddRecvStream(const StreamParams& sp) { |
pbos@webrtc.org | a2a6fe6 | 2015-03-06 15:35:19 +0000 | [diff] [blame] | 1126 | return AddRecvStream(sp, false); |
| 1127 | } |
| 1128 | |
| 1129 | bool WebRtcVideoChannel2::AddRecvStream(const StreamParams& sp, |
| 1130 | bool default_stream) { |
Fredrik Solenberg | 4b60c73 | 2015-05-07 14:07:48 +0200 | [diff] [blame^] | 1131 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 1132 | |
Peter Boström | d4362cd | 2015-03-25 14:17:23 +0100 | [diff] [blame] | 1133 | LOG(LS_INFO) << "AddRecvStream" << (default_stream ? " (default stream)" : "") |
| 1134 | << ": " << sp.ToString(); |
| 1135 | if (!ValidateStreamParams(sp)) |
| 1136 | return false; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1137 | |
| 1138 | uint32 ssrc = sp.first_ssrc(); |
| 1139 | assert(ssrc != 0); // TODO(pbos): Is this ever valid? |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1140 | |
pbos@webrtc.org | 575d126 | 2014-10-08 14:48:08 +0000 | [diff] [blame] | 1141 | rtc::CritScope stream_lock(&stream_crit_); |
Peter Boström | d6f4c25 | 2015-03-26 16:23:04 +0100 | [diff] [blame] | 1142 | // Remove running stream if this was a default stream. |
| 1143 | auto prev_stream = receive_streams_.find(ssrc); |
| 1144 | if (prev_stream != receive_streams_.end()) { |
| 1145 | if (default_stream || !prev_stream->second->IsDefaultStream()) { |
| 1146 | LOG(LS_ERROR) << "Receive stream for SSRC '" << ssrc |
| 1147 | << "' already exists."; |
| 1148 | return false; |
pbos@webrtc.org | a2a6fe6 | 2015-03-06 15:35:19 +0000 | [diff] [blame] | 1149 | } |
Peter Boström | d6f4c25 | 2015-03-26 16:23:04 +0100 | [diff] [blame] | 1150 | DeleteReceiveStream(prev_stream->second); |
| 1151 | receive_streams_.erase(prev_stream); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1152 | } |
| 1153 | |
Peter Boström | d6f4c25 | 2015-03-26 16:23:04 +0100 | [diff] [blame] | 1154 | if (!ValidateReceiveSsrcAvailability(sp)) |
| 1155 | return false; |
| 1156 | |
| 1157 | for (uint32 used_ssrc : sp.ssrcs) |
| 1158 | receive_ssrcs_.insert(used_ssrc); |
| 1159 | |
pbos@webrtc.org | bd249bc | 2014-07-07 04:45:15 +0000 | [diff] [blame] | 1160 | webrtc::VideoReceiveStream::Config config; |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 1161 | ConfigureReceiverRtp(&config, sp); |
pbos@webrtc.org | 3bf3d23 | 2014-10-31 12:59:34 +0000 | [diff] [blame] | 1162 | |
| 1163 | // Set up A/V sync if there is a VoiceChannel. |
| 1164 | // TODO(pbos): The A/V is synched by the receiving channel. So we need to know |
| 1165 | // the SSRC of the remote audio channel in order to sync the correct webrtc |
| 1166 | // VoiceEngine channel. For now sync the first channel in non-conference to |
| 1167 | // match existing behavior in WebRtcVideoEngine. |
pbos@webrtc.org | 8296ec5 | 2015-03-20 14:27:49 +0000 | [diff] [blame] | 1168 | if (voice_channel_id_ != -1 && receive_streams_.empty() && |
pbos@webrtc.org | 3bf3d23 | 2014-10-31 12:59:34 +0000 | [diff] [blame] | 1169 | !options_.conference_mode.GetWithDefaultIfUnset(false)) { |
pbos@webrtc.org | 8296ec5 | 2015-03-20 14:27:49 +0000 | [diff] [blame] | 1170 | config.audio_channel_id = voice_channel_id_; |
pbos@webrtc.org | 3bf3d23 | 2014-10-31 12:59:34 +0000 | [diff] [blame] | 1171 | } |
| 1172 | |
Peter Boström | d6f4c25 | 2015-03-26 16:23:04 +0100 | [diff] [blame] | 1173 | receive_streams_[ssrc] = new WebRtcVideoReceiveStream( |
| 1174 | call_.get(), sp.ssrcs, external_decoder_factory_, default_stream, config, |
| 1175 | recv_codecs_); |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 1176 | |
| 1177 | return true; |
| 1178 | } |
| 1179 | |
| 1180 | void WebRtcVideoChannel2::ConfigureReceiverRtp( |
| 1181 | webrtc::VideoReceiveStream::Config* config, |
| 1182 | const StreamParams& sp) const { |
| 1183 | uint32 ssrc = sp.first_ssrc(); |
| 1184 | |
| 1185 | config->rtp.remote_ssrc = ssrc; |
| 1186 | config->rtp.local_ssrc = rtcp_receiver_report_ssrc_; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1187 | |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 1188 | config->rtp.extensions = recv_rtp_extensions_; |
pbos@webrtc.org | 257e130 | 2014-07-25 19:01:32 +0000 | [diff] [blame] | 1189 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1190 | // TODO(pbos): This protection is against setting the same local ssrc as |
| 1191 | // remote which is not permitted by the lower-level API. RTCP requires a |
| 1192 | // corresponding sender SSRC. Figure out what to do when we don't have |
| 1193 | // (receive-only) or know a good local SSRC. |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 1194 | if (config->rtp.remote_ssrc == config->rtp.local_ssrc) { |
| 1195 | if (config->rtp.local_ssrc != kDefaultRtcpReceiverReportSsrc) { |
| 1196 | config->rtp.local_ssrc = kDefaultRtcpReceiverReportSsrc; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1197 | } else { |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 1198 | config->rtp.local_ssrc = kDefaultRtcpReceiverReportSsrc + 1; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1199 | } |
| 1200 | } |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 1201 | |
| 1202 | for (size_t i = 0; i < recv_codecs_.size(); ++i) { |
andresp@webrtc.org | 82775b1 | 2014-11-07 09:37:54 +0000 | [diff] [blame] | 1203 | MergeFecConfig(recv_codecs_[i].fec, &config->rtp.fec); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1204 | } |
| 1205 | |
andresp@webrtc.org | 82775b1 | 2014-11-07 09:37:54 +0000 | [diff] [blame] | 1206 | for (size_t i = 0; i < recv_codecs_.size(); ++i) { |
| 1207 | uint32 rtx_ssrc; |
| 1208 | if (recv_codecs_[i].rtx_payload_type != -1 && |
| 1209 | sp.GetFidSsrc(ssrc, &rtx_ssrc)) { |
| 1210 | webrtc::VideoReceiveStream::Config::Rtp::Rtx& rtx = |
| 1211 | config->rtp.rtx[recv_codecs_[i].codec.id]; |
| 1212 | rtx.ssrc = rtx_ssrc; |
| 1213 | rtx.payload_type = recv_codecs_[i].rtx_payload_type; |
| 1214 | } |
| 1215 | } |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1216 | } |
| 1217 | |
| 1218 | bool WebRtcVideoChannel2::RemoveRecvStream(uint32 ssrc) { |
| 1219 | LOG(LS_INFO) << "RemoveRecvStream: " << ssrc; |
| 1220 | if (ssrc == 0) { |
pbos@webrtc.org | afb554f4 | 2014-08-12 23:17:13 +0000 | [diff] [blame] | 1221 | LOG(LS_ERROR) << "RemoveRecvStream with 0 ssrc is not supported."; |
| 1222 | return false; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1223 | } |
| 1224 | |
pbos@webrtc.org | 575d126 | 2014-10-08 14:48:08 +0000 | [diff] [blame] | 1225 | rtc::CritScope stream_lock(&stream_crit_); |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 1226 | std::map<uint32, WebRtcVideoReceiveStream*>::iterator stream = |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1227 | receive_streams_.find(ssrc); |
| 1228 | if (stream == receive_streams_.end()) { |
| 1229 | LOG(LS_ERROR) << "Stream not found for ssrc: " << ssrc; |
| 1230 | return false; |
| 1231 | } |
Peter Boström | d6f4c25 | 2015-03-26 16:23:04 +0100 | [diff] [blame] | 1232 | DeleteReceiveStream(stream->second); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1233 | receive_streams_.erase(stream); |
| 1234 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1235 | return true; |
| 1236 | } |
| 1237 | |
| 1238 | bool WebRtcVideoChannel2::SetRenderer(uint32 ssrc, VideoRenderer* renderer) { |
| 1239 | LOG(LS_INFO) << "SetRenderer: ssrc:" << ssrc << " " |
| 1240 | << (renderer ? "(ptr)" : "NULL"); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1241 | if (ssrc == 0) { |
pbos@webrtc.org | afb554f4 | 2014-08-12 23:17:13 +0000 | [diff] [blame] | 1242 | default_unsignalled_ssrc_handler_.SetDefaultRenderer(this, renderer); |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 1243 | return true; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1244 | } |
| 1245 | |
pbos@webrtc.org | 575d126 | 2014-10-08 14:48:08 +0000 | [diff] [blame] | 1246 | rtc::CritScope stream_lock(&stream_crit_); |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 1247 | std::map<uint32, WebRtcVideoReceiveStream*>::iterator it = |
| 1248 | receive_streams_.find(ssrc); |
| 1249 | if (it == receive_streams_.end()) { |
| 1250 | return false; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1251 | } |
| 1252 | |
| 1253 | it->second->SetRenderer(renderer); |
| 1254 | return true; |
| 1255 | } |
| 1256 | |
| 1257 | bool WebRtcVideoChannel2::GetRenderer(uint32 ssrc, VideoRenderer** renderer) { |
| 1258 | if (ssrc == 0) { |
pbos@webrtc.org | afb554f4 | 2014-08-12 23:17:13 +0000 | [diff] [blame] | 1259 | *renderer = default_unsignalled_ssrc_handler_.GetDefaultRenderer(); |
| 1260 | return *renderer != NULL; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1261 | } |
| 1262 | |
pbos@webrtc.org | 575d126 | 2014-10-08 14:48:08 +0000 | [diff] [blame] | 1263 | rtc::CritScope stream_lock(&stream_crit_); |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 1264 | std::map<uint32, WebRtcVideoReceiveStream*>::iterator it = |
| 1265 | receive_streams_.find(ssrc); |
| 1266 | if (it == receive_streams_.end()) { |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1267 | return false; |
| 1268 | } |
| 1269 | *renderer = it->second->GetRenderer(); |
| 1270 | return true; |
| 1271 | } |
| 1272 | |
pbos@webrtc.org | 058b1f1 | 2015-03-04 08:54:32 +0000 | [diff] [blame] | 1273 | bool WebRtcVideoChannel2::GetStats(VideoMediaInfo* info) { |
pbos@webrtc.org | e6f84ae | 2014-07-18 11:11:55 +0000 | [diff] [blame] | 1274 | info->Clear(); |
| 1275 | FillSenderStats(info); |
| 1276 | FillReceiverStats(info); |
pbos@webrtc.org | 2b19f06 | 2014-12-11 13:26:09 +0000 | [diff] [blame] | 1277 | webrtc::Call::Stats stats = call_->GetStats(); |
| 1278 | FillBandwidthEstimationStats(stats, info); |
| 1279 | if (stats.rtt_ms != -1) { |
| 1280 | for (size_t i = 0; i < info->senders.size(); ++i) { |
| 1281 | info->senders[i].rtt_ms = stats.rtt_ms; |
| 1282 | } |
| 1283 | } |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1284 | return true; |
| 1285 | } |
| 1286 | |
pbos@webrtc.org | e6f84ae | 2014-07-18 11:11:55 +0000 | [diff] [blame] | 1287 | void WebRtcVideoChannel2::FillSenderStats(VideoMediaInfo* video_media_info) { |
pbos@webrtc.org | 575d126 | 2014-10-08 14:48:08 +0000 | [diff] [blame] | 1288 | rtc::CritScope stream_lock(&stream_crit_); |
pbos@webrtc.org | e6f84ae | 2014-07-18 11:11:55 +0000 | [diff] [blame] | 1289 | for (std::map<uint32, WebRtcVideoSendStream*>::iterator it = |
| 1290 | send_streams_.begin(); |
| 1291 | it != send_streams_.end(); |
| 1292 | ++it) { |
| 1293 | video_media_info->senders.push_back(it->second->GetVideoSenderInfo()); |
| 1294 | } |
| 1295 | } |
| 1296 | |
| 1297 | void WebRtcVideoChannel2::FillReceiverStats(VideoMediaInfo* video_media_info) { |
pbos@webrtc.org | 575d126 | 2014-10-08 14:48:08 +0000 | [diff] [blame] | 1298 | rtc::CritScope stream_lock(&stream_crit_); |
pbos@webrtc.org | e6f84ae | 2014-07-18 11:11:55 +0000 | [diff] [blame] | 1299 | for (std::map<uint32, WebRtcVideoReceiveStream*>::iterator it = |
| 1300 | receive_streams_.begin(); |
| 1301 | it != receive_streams_.end(); |
| 1302 | ++it) { |
| 1303 | video_media_info->receivers.push_back(it->second->GetVideoReceiverInfo()); |
| 1304 | } |
| 1305 | } |
| 1306 | |
| 1307 | void WebRtcVideoChannel2::FillBandwidthEstimationStats( |
pbos@webrtc.org | 2b19f06 | 2014-12-11 13:26:09 +0000 | [diff] [blame] | 1308 | const webrtc::Call::Stats& stats, |
pbos@webrtc.org | e6f84ae | 2014-07-18 11:11:55 +0000 | [diff] [blame] | 1309 | VideoMediaInfo* video_media_info) { |
stefan@webrtc.org | 0bae1fa | 2014-11-05 14:05:29 +0000 | [diff] [blame] | 1310 | BandwidthEstimationInfo bwe_info; |
stefan@webrtc.org | 0bae1fa | 2014-11-05 14:05:29 +0000 | [diff] [blame] | 1311 | bwe_info.available_send_bandwidth = stats.send_bandwidth_bps; |
| 1312 | bwe_info.available_recv_bandwidth = stats.recv_bandwidth_bps; |
| 1313 | bwe_info.bucket_delay = stats.pacer_delay_ms; |
| 1314 | |
| 1315 | // Get send stream bitrate stats. |
| 1316 | rtc::CritScope stream_lock(&stream_crit_); |
| 1317 | for (std::map<uint32, WebRtcVideoSendStream*>::iterator stream = |
| 1318 | send_streams_.begin(); |
| 1319 | stream != send_streams_.end(); |
| 1320 | ++stream) { |
| 1321 | stream->second->FillBandwidthEstimationInfo(&bwe_info); |
| 1322 | } |
| 1323 | video_media_info->bw_estimations.push_back(bwe_info); |
pbos@webrtc.org | e6f84ae | 2014-07-18 11:11:55 +0000 | [diff] [blame] | 1324 | } |
| 1325 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1326 | bool WebRtcVideoChannel2::SetCapturer(uint32 ssrc, VideoCapturer* capturer) { |
| 1327 | LOG(LS_INFO) << "SetCapturer: " << ssrc << " -> " |
| 1328 | << (capturer != NULL ? "(capturer)" : "NULL"); |
| 1329 | assert(ssrc != 0); |
Peter Boström | e7b221f | 2015-04-13 15:34:32 +0200 | [diff] [blame] | 1330 | { |
| 1331 | rtc::CritScope stream_lock(&stream_crit_); |
| 1332 | if (send_streams_.find(ssrc) == send_streams_.end()) { |
| 1333 | LOG(LS_ERROR) << "No sending stream on ssrc " << ssrc; |
| 1334 | return false; |
| 1335 | } |
| 1336 | if (!send_streams_[ssrc]->SetCapturer(capturer)) { |
| 1337 | return false; |
| 1338 | } |
Guo-wei Shieh | 64c1e8c | 2015-04-01 15:33:06 -0700 | [diff] [blame] | 1339 | } |
| 1340 | |
| 1341 | if (capturer) { |
| 1342 | capturer->SetApplyRotation( |
| 1343 | !FindHeaderExtension(send_rtp_extensions_, |
| 1344 | kRtpVideoRotationHeaderExtension)); |
| 1345 | } |
Peter Boström | e7b221f | 2015-04-13 15:34:32 +0200 | [diff] [blame] | 1346 | { |
| 1347 | rtc::CritScope lock(&capturer_crit_); |
| 1348 | capturers_[ssrc] = capturer; |
| 1349 | } |
Guo-wei Shieh | 64c1e8c | 2015-04-01 15:33:06 -0700 | [diff] [blame] | 1350 | return true; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1351 | } |
| 1352 | |
| 1353 | bool WebRtcVideoChannel2::SendIntraFrame() { |
| 1354 | // TODO(pbos): Implement. |
| 1355 | LOG(LS_VERBOSE) << "SendIntraFrame()."; |
| 1356 | return true; |
| 1357 | } |
| 1358 | |
| 1359 | bool WebRtcVideoChannel2::RequestIntraFrame() { |
| 1360 | // TODO(pbos): Implement. |
| 1361 | LOG(LS_VERBOSE) << "SendIntraFrame()."; |
| 1362 | return true; |
| 1363 | } |
| 1364 | |
| 1365 | void WebRtcVideoChannel2::OnPacketReceived( |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1366 | rtc::Buffer* packet, |
| 1367 | const rtc::PacketTime& packet_time) { |
pbos@webrtc.org | 4e545cc | 2014-05-14 13:58:13 +0000 | [diff] [blame] | 1368 | const webrtc::PacketReceiver::DeliveryStatus delivery_result = |
Fredrik Solenberg | 23fba1f | 2015-04-29 15:24:01 +0200 | [diff] [blame] | 1369 | call_->Receiver()->DeliverPacket(webrtc::MediaType::VIDEO, |
kwiberg@webrtc.org | eebcab5 | 2015-03-24 09:19:06 +0000 | [diff] [blame] | 1370 | reinterpret_cast<const uint8_t*>(packet->data()), packet->size()); |
pbos@webrtc.org | 4e545cc | 2014-05-14 13:58:13 +0000 | [diff] [blame] | 1371 | switch (delivery_result) { |
| 1372 | case webrtc::PacketReceiver::DELIVERY_OK: |
| 1373 | return; |
| 1374 | case webrtc::PacketReceiver::DELIVERY_PACKET_ERROR: |
| 1375 | return; |
| 1376 | case webrtc::PacketReceiver::DELIVERY_UNKNOWN_SSRC: |
| 1377 | break; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1378 | } |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1379 | |
| 1380 | uint32 ssrc = 0; |
kwiberg@webrtc.org | eebcab5 | 2015-03-24 09:19:06 +0000 | [diff] [blame] | 1381 | if (!GetRtpSsrc(packet->data(), packet->size(), &ssrc)) { |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1382 | return; |
| 1383 | } |
| 1384 | |
pbos@webrtc.org | a2a6fe6 | 2015-03-06 15:35:19 +0000 | [diff] [blame] | 1385 | // TODO(pbos): Ignore unsignalled packets that don't use the video payload |
| 1386 | // (prevent creating default receivers for RTX configured as if it would |
| 1387 | // receive media payloads on those SSRCs). |
pbos@webrtc.org | afb554f4 | 2014-08-12 23:17:13 +0000 | [diff] [blame] | 1388 | switch (unsignalled_ssrc_handler_->OnUnsignalledSsrc(this, ssrc)) { |
| 1389 | case UnsignalledSsrcHandler::kDropPacket: |
| 1390 | return; |
| 1391 | case UnsignalledSsrcHandler::kDeliverPacket: |
| 1392 | break; |
| 1393 | } |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1394 | |
Fredrik Solenberg | 23fba1f | 2015-04-29 15:24:01 +0200 | [diff] [blame] | 1395 | if (call_->Receiver()->DeliverPacket(webrtc::MediaType::VIDEO, |
kwiberg@webrtc.org | eebcab5 | 2015-03-24 09:19:06 +0000 | [diff] [blame] | 1396 | reinterpret_cast<const uint8_t*>(packet->data()), packet->size()) != |
pbos@webrtc.org | 1e019d1 | 2014-05-16 11:38:45 +0000 | [diff] [blame] | 1397 | webrtc::PacketReceiver::DELIVERY_OK) { |
pbos@webrtc.org | afb554f4 | 2014-08-12 23:17:13 +0000 | [diff] [blame] | 1398 | LOG(LS_WARNING) << "Failed to deliver RTP packet on re-delivery."; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1399 | return; |
| 1400 | } |
| 1401 | } |
| 1402 | |
| 1403 | void WebRtcVideoChannel2::OnRtcpReceived( |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1404 | rtc::Buffer* packet, |
| 1405 | const rtc::PacketTime& packet_time) { |
Fredrik Solenberg | 23fba1f | 2015-04-29 15:24:01 +0200 | [diff] [blame] | 1406 | if (call_->Receiver()->DeliverPacket(webrtc::MediaType::VIDEO, |
kwiberg@webrtc.org | eebcab5 | 2015-03-24 09:19:06 +0000 | [diff] [blame] | 1407 | reinterpret_cast<const uint8_t*>(packet->data()), packet->size()) != |
pbos@webrtc.org | 1e019d1 | 2014-05-16 11:38:45 +0000 | [diff] [blame] | 1408 | webrtc::PacketReceiver::DELIVERY_OK) { |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1409 | LOG(LS_WARNING) << "Failed to deliver RTCP packet."; |
| 1410 | } |
| 1411 | } |
| 1412 | |
| 1413 | void WebRtcVideoChannel2::OnReadyToSend(bool ready) { |
pbos@webrtc.org | 26c0c41 | 2014-09-03 16:17:12 +0000 | [diff] [blame] | 1414 | LOG(LS_VERBOSE) << "OnReadyToSend: " << (ready ? "Ready." : "Not ready."); |
| 1415 | call_->SignalNetworkState(ready ? webrtc::Call::kNetworkUp |
| 1416 | : webrtc::Call::kNetworkDown); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1417 | } |
| 1418 | |
| 1419 | bool WebRtcVideoChannel2::MuteStream(uint32 ssrc, bool mute) { |
| 1420 | LOG(LS_VERBOSE) << "MuteStream: " << ssrc << " -> " |
| 1421 | << (mute ? "mute" : "unmute"); |
| 1422 | assert(ssrc != 0); |
pbos@webrtc.org | 575d126 | 2014-10-08 14:48:08 +0000 | [diff] [blame] | 1423 | rtc::CritScope stream_lock(&stream_crit_); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1424 | if (send_streams_.find(ssrc) == send_streams_.end()) { |
| 1425 | LOG(LS_ERROR) << "No sending stream on ssrc " << ssrc; |
| 1426 | return false; |
| 1427 | } |
pbos@webrtc.org | ef8bb8d | 2014-08-13 21:36:18 +0000 | [diff] [blame] | 1428 | |
| 1429 | send_streams_[ssrc]->MuteStream(mute); |
| 1430 | return true; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1431 | } |
| 1432 | |
| 1433 | bool WebRtcVideoChannel2::SetRecvRtpHeaderExtensions( |
| 1434 | const std::vector<RtpHeaderExtension>& extensions) { |
pbos@webrtc.org | 50fe359 | 2015-01-29 12:33:07 +0000 | [diff] [blame] | 1435 | TRACE_EVENT0("webrtc", "WebRtcVideoChannel2::SetRecvRtpHeaderExtensions"); |
pbos@webrtc.org | 587ef60 | 2014-06-16 17:32:02 +0000 | [diff] [blame] | 1436 | LOG(LS_INFO) << "SetRecvRtpHeaderExtensions: " |
| 1437 | << RtpExtensionsToString(extensions); |
pbos@webrtc.org | 3c10758 | 2014-07-20 15:27:35 +0000 | [diff] [blame] | 1438 | if (!ValidateRtpHeaderExtensionIds(extensions)) |
| 1439 | return false; |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 1440 | |
pbos@webrtc.org | c37e72e | 2015-01-05 18:51:13 +0000 | [diff] [blame] | 1441 | std::vector<webrtc::RtpExtension> filtered_extensions = |
| 1442 | FilterRtpExtensions(extensions); |
| 1443 | if (!RtpExtensionsHaveChanged(recv_rtp_extensions_, filtered_extensions)) |
| 1444 | return true; |
| 1445 | |
| 1446 | recv_rtp_extensions_ = filtered_extensions; |
| 1447 | |
pbos@webrtc.org | 575d126 | 2014-10-08 14:48:08 +0000 | [diff] [blame] | 1448 | rtc::CritScope stream_lock(&stream_crit_); |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 1449 | for (std::map<uint32, WebRtcVideoReceiveStream*>::iterator it = |
| 1450 | receive_streams_.begin(); |
| 1451 | it != receive_streams_.end(); |
| 1452 | ++it) { |
| 1453 | it->second->SetRtpExtensions(recv_rtp_extensions_); |
| 1454 | } |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1455 | return true; |
| 1456 | } |
| 1457 | |
| 1458 | bool WebRtcVideoChannel2::SetSendRtpHeaderExtensions( |
| 1459 | const std::vector<RtpHeaderExtension>& extensions) { |
pbos@webrtc.org | 50fe359 | 2015-01-29 12:33:07 +0000 | [diff] [blame] | 1460 | TRACE_EVENT0("webrtc", "WebRtcVideoChannel2::SetSendRtpHeaderExtensions"); |
pbos@webrtc.org | 587ef60 | 2014-06-16 17:32:02 +0000 | [diff] [blame] | 1461 | LOG(LS_INFO) << "SetSendRtpHeaderExtensions: " |
| 1462 | << RtpExtensionsToString(extensions); |
pbos@webrtc.org | 3c10758 | 2014-07-20 15:27:35 +0000 | [diff] [blame] | 1463 | if (!ValidateRtpHeaderExtensionIds(extensions)) |
| 1464 | return false; |
| 1465 | |
pbos@webrtc.org | c37e72e | 2015-01-05 18:51:13 +0000 | [diff] [blame] | 1466 | std::vector<webrtc::RtpExtension> filtered_extensions = |
| 1467 | FilterRtpExtensions(extensions); |
| 1468 | if (!RtpExtensionsHaveChanged(send_rtp_extensions_, filtered_extensions)) |
| 1469 | return true; |
| 1470 | |
| 1471 | send_rtp_extensions_ = filtered_extensions; |
pbos@webrtc.org | a2ef4fe | 2014-11-07 10:54:43 +0000 | [diff] [blame] | 1472 | |
Guo-wei Shieh | 64c1e8c | 2015-04-01 15:33:06 -0700 | [diff] [blame] | 1473 | const webrtc::RtpExtension* cvo_extension = FindHeaderExtension( |
| 1474 | send_rtp_extensions_, kRtpVideoRotationHeaderExtension); |
| 1475 | |
pbos@webrtc.org | 575d126 | 2014-10-08 14:48:08 +0000 | [diff] [blame] | 1476 | rtc::CritScope stream_lock(&stream_crit_); |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 1477 | for (std::map<uint32, WebRtcVideoSendStream*>::iterator it = |
| 1478 | send_streams_.begin(); |
| 1479 | it != send_streams_.end(); |
| 1480 | ++it) { |
| 1481 | it->second->SetRtpExtensions(send_rtp_extensions_); |
Guo-wei Shieh | 64c1e8c | 2015-04-01 15:33:06 -0700 | [diff] [blame] | 1482 | it->second->SetApplyRotation(!cvo_extension); |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 1483 | } |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1484 | return true; |
| 1485 | } |
| 1486 | |
Peter Boström | dfd53fe | 2015-03-27 15:58:11 +0100 | [diff] [blame] | 1487 | // Counter-intuitively this method doesn't only set global bitrate caps but also |
| 1488 | // per-stream codec max bitrates. This is to permit SetMaxSendBitrate (b=AS) to |
| 1489 | // raise bitrates above the 2000k default bitrate cap. |
pbos@webrtc.org | 0087318 | 2014-11-25 14:03:34 +0000 | [diff] [blame] | 1490 | bool WebRtcVideoChannel2::SetMaxSendBandwidth(int max_bitrate_bps) { |
Peter Boström | dfd53fe | 2015-03-27 15:58:11 +0100 | [diff] [blame] | 1491 | // TODO(pbos): Figure out whether b=AS means max bitrate for this |
| 1492 | // WebRtcVideoChannel2 (in which case we're good), or per sender (SSRC), in |
| 1493 | // which case this should not set a Call::BitrateConfig but rather reconfigure |
| 1494 | // all senders. |
pbos@webrtc.org | 0087318 | 2014-11-25 14:03:34 +0000 | [diff] [blame] | 1495 | LOG(LS_INFO) << "SetMaxSendBandwidth: " << max_bitrate_bps << "bps."; |
Peter Boström | dfd53fe | 2015-03-27 15:58:11 +0100 | [diff] [blame] | 1496 | if (max_bitrate_bps == bitrate_config_.max_bitrate_bps) |
| 1497 | return true; |
| 1498 | |
pbos@webrtc.org | 0087318 | 2014-11-25 14:03:34 +0000 | [diff] [blame] | 1499 | if (max_bitrate_bps <= 0) { |
| 1500 | // Unsetting max bitrate. |
| 1501 | max_bitrate_bps = -1; |
| 1502 | } |
| 1503 | bitrate_config_.start_bitrate_bps = -1; |
| 1504 | bitrate_config_.max_bitrate_bps = max_bitrate_bps; |
| 1505 | if (max_bitrate_bps > 0 && |
| 1506 | bitrate_config_.min_bitrate_bps > max_bitrate_bps) { |
| 1507 | bitrate_config_.min_bitrate_bps = max_bitrate_bps; |
| 1508 | } |
| 1509 | call_->SetBitrateConfig(bitrate_config_); |
Peter Boström | dfd53fe | 2015-03-27 15:58:11 +0100 | [diff] [blame] | 1510 | rtc::CritScope stream_lock(&stream_crit_); |
| 1511 | for (auto& kv : send_streams_) |
| 1512 | kv.second->SetMaxBitrateBps(max_bitrate_bps); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1513 | return true; |
| 1514 | } |
| 1515 | |
| 1516 | bool WebRtcVideoChannel2::SetOptions(const VideoOptions& options) { |
pbos@webrtc.org | 50fe359 | 2015-01-29 12:33:07 +0000 | [diff] [blame] | 1517 | TRACE_EVENT0("webrtc", "WebRtcVideoChannel2::SetOptions"); |
pbos@webrtc.org | a2ef4fe | 2014-11-07 10:54:43 +0000 | [diff] [blame] | 1518 | LOG(LS_INFO) << "SetOptions: " << options.ToString(); |
| 1519 | VideoOptions old_options = options_; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1520 | options_.SetAll(options); |
pbos@webrtc.org | a2ef4fe | 2014-11-07 10:54:43 +0000 | [diff] [blame] | 1521 | if (options_ == old_options) { |
| 1522 | // No new options to set. |
| 1523 | return true; |
| 1524 | } |
Peter Boström | e7b221f | 2015-04-13 15:34:32 +0200 | [diff] [blame] | 1525 | { |
| 1526 | rtc::CritScope lock(&capturer_crit_); |
| 1527 | options_.cpu_overuse_detection.Get(&signal_cpu_adaptation_); |
| 1528 | } |
pbos@webrtc.org | d819803 | 2014-11-10 14:41:43 +0000 | [diff] [blame] | 1529 | rtc::DiffServCodePoint dscp = options_.dscp.GetWithDefaultIfUnset(false) |
| 1530 | ? rtc::DSCP_AF41 |
| 1531 | : rtc::DSCP_DEFAULT; |
| 1532 | MediaChannel::SetDscp(dscp); |
pbos@webrtc.org | 575d126 | 2014-10-08 14:48:08 +0000 | [diff] [blame] | 1533 | rtc::CritScope stream_lock(&stream_crit_); |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1534 | for (std::map<uint32, WebRtcVideoSendStream*>::iterator it = |
| 1535 | send_streams_.begin(); |
| 1536 | it != send_streams_.end(); |
| 1537 | ++it) { |
| 1538 | it->second->SetOptions(options_); |
| 1539 | } |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1540 | return true; |
| 1541 | } |
| 1542 | |
| 1543 | void WebRtcVideoChannel2::SetInterface(NetworkInterface* iface) { |
| 1544 | MediaChannel::SetInterface(iface); |
| 1545 | // Set the RTP recv/send buffer to a bigger size |
| 1546 | MediaChannel::SetOption(NetworkInterface::ST_RTP, |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1547 | rtc::Socket::OPT_RCVBUF, |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1548 | kVideoRtpBufferSize); |
| 1549 | |
buildbot@webrtc.org | ae694ef | 2014-10-28 17:37:17 +0000 | [diff] [blame] | 1550 | // Speculative change to increase the outbound socket buffer size. |
| 1551 | // In b/15152257, we are seeing a significant number of packets discarded |
| 1552 | // due to lack of socket buffer space, although it's not yet clear what the |
| 1553 | // ideal value should be. |
| 1554 | MediaChannel::SetOption(NetworkInterface::ST_RTP, |
| 1555 | rtc::Socket::OPT_SNDBUF, |
| 1556 | kVideoRtpBufferSize); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1557 | } |
| 1558 | |
| 1559 | void WebRtcVideoChannel2::UpdateAspectRatio(int ratio_w, int ratio_h) { |
| 1560 | // TODO(pbos): Implement. |
| 1561 | } |
| 1562 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1563 | void WebRtcVideoChannel2::OnMessage(rtc::Message* msg) { |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1564 | // Ignored. |
| 1565 | } |
| 1566 | |
pbos@webrtc.org | 42684be | 2014-10-03 11:25:45 +0000 | [diff] [blame] | 1567 | void WebRtcVideoChannel2::OnLoadUpdate(Load load) { |
Peter Boström | e7b221f | 2015-04-13 15:34:32 +0200 | [diff] [blame] | 1568 | // OnLoadUpdate can not take any locks that are held while creating streams |
| 1569 | // etc. Doing so establishes lock-order inversions between the webrtc process |
| 1570 | // thread on stream creation and locks such as stream_crit_ while calling out. |
| 1571 | rtc::CritScope stream_lock(&capturer_crit_); |
| 1572 | if (!signal_cpu_adaptation_) |
| 1573 | return; |
Erik Språng | efbde37 | 2015-04-29 16:21:28 +0200 | [diff] [blame] | 1574 | // Do not adapt resolution for screen content as this will likely result in |
| 1575 | // blurry and unreadable text. |
Peter Boström | e7b221f | 2015-04-13 15:34:32 +0200 | [diff] [blame] | 1576 | for (auto& kv : capturers_) { |
Erik Språng | efbde37 | 2015-04-29 16:21:28 +0200 | [diff] [blame] | 1577 | if (kv.second != nullptr |
| 1578 | && !kv.second->IsScreencast() |
| 1579 | && kv.second->video_adapter() != nullptr) { |
Peter Boström | e7b221f | 2015-04-13 15:34:32 +0200 | [diff] [blame] | 1580 | kv.second->video_adapter()->OnCpuResolutionRequest( |
| 1581 | load == kOveruse ? CoordinatedVideoAdapter::DOWNGRADE |
| 1582 | : CoordinatedVideoAdapter::UPGRADE); |
| 1583 | } |
pbos@webrtc.org | 42684be | 2014-10-03 11:25:45 +0000 | [diff] [blame] | 1584 | } |
| 1585 | } |
| 1586 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1587 | bool WebRtcVideoChannel2::SendRtp(const uint8_t* data, size_t len) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1588 | rtc::Buffer packet(data, len, kMaxRtpPacketLen); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1589 | return MediaChannel::SendPacket(&packet); |
| 1590 | } |
| 1591 | |
| 1592 | bool WebRtcVideoChannel2::SendRtcp(const uint8_t* data, size_t len) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1593 | rtc::Buffer packet(data, len, kMaxRtpPacketLen); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1594 | return MediaChannel::SendRtcp(&packet); |
| 1595 | } |
| 1596 | |
| 1597 | void WebRtcVideoChannel2::StartAllSendStreams() { |
pbos@webrtc.org | 575d126 | 2014-10-08 14:48:08 +0000 | [diff] [blame] | 1598 | rtc::CritScope stream_lock(&stream_crit_); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1599 | for (std::map<uint32, WebRtcVideoSendStream*>::iterator it = |
| 1600 | send_streams_.begin(); |
| 1601 | it != send_streams_.end(); |
| 1602 | ++it) { |
| 1603 | it->second->Start(); |
| 1604 | } |
| 1605 | } |
| 1606 | |
| 1607 | void WebRtcVideoChannel2::StopAllSendStreams() { |
pbos@webrtc.org | 575d126 | 2014-10-08 14:48:08 +0000 | [diff] [blame] | 1608 | rtc::CritScope stream_lock(&stream_crit_); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1609 | for (std::map<uint32, WebRtcVideoSendStream*>::iterator it = |
| 1610 | send_streams_.begin(); |
| 1611 | it != send_streams_.end(); |
| 1612 | ++it) { |
| 1613 | it->second->Stop(); |
| 1614 | } |
| 1615 | } |
| 1616 | |
pbos@webrtc.org | 6ae48c6 | 2014-06-06 10:49:19 +0000 | [diff] [blame] | 1617 | WebRtcVideoChannel2::WebRtcVideoSendStream::VideoSendStreamParameters:: |
| 1618 | VideoSendStreamParameters( |
| 1619 | const webrtc::VideoSendStream::Config& config, |
| 1620 | const VideoOptions& options, |
Peter Boström | dfd53fe | 2015-03-27 15:58:11 +0100 | [diff] [blame] | 1621 | int max_bitrate_bps, |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1622 | const Settable<VideoCodecSettings>& codec_settings) |
Peter Boström | dfd53fe | 2015-03-27 15:58:11 +0100 | [diff] [blame] | 1623 | : config(config), |
| 1624 | options(options), |
| 1625 | max_bitrate_bps(max_bitrate_bps), |
| 1626 | codec_settings(codec_settings) { |
pbos@webrtc.org | 6ae48c6 | 2014-06-06 10:49:19 +0000 | [diff] [blame] | 1627 | } |
| 1628 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1629 | WebRtcVideoChannel2::WebRtcVideoSendStream::WebRtcVideoSendStream( |
| 1630 | webrtc::Call* call, |
pbos@webrtc.org | 7fe1e03 | 2014-10-14 04:25:33 +0000 | [diff] [blame] | 1631 | WebRtcVideoEncoderFactory* external_encoder_factory, |
pbos@webrtc.org | 6ae48c6 | 2014-06-06 10:49:19 +0000 | [diff] [blame] | 1632 | const VideoOptions& options, |
Peter Boström | dfd53fe | 2015-03-27 15:58:11 +0100 | [diff] [blame] | 1633 | int max_bitrate_bps, |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1634 | const Settable<VideoCodecSettings>& codec_settings, |
| 1635 | const StreamParams& sp, |
| 1636 | const std::vector<webrtc::RtpExtension>& rtp_extensions) |
Henrik Kjellander | 7c027b6 | 2015-04-22 13:21:30 +0200 | [diff] [blame] | 1637 | : ssrcs_(sp.ssrcs), |
| 1638 | call_(call), |
pbos@webrtc.org | 7fe1e03 | 2014-10-14 04:25:33 +0000 | [diff] [blame] | 1639 | external_encoder_factory_(external_encoder_factory), |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1640 | stream_(NULL), |
Peter Boström | dfd53fe | 2015-03-27 15:58:11 +0100 | [diff] [blame] | 1641 | parameters_(webrtc::VideoSendStream::Config(), |
| 1642 | options, |
| 1643 | max_bitrate_bps, |
| 1644 | codec_settings), |
pbos@webrtc.org | 7fe1e03 | 2014-10-14 04:25:33 +0000 | [diff] [blame] | 1645 | allocated_encoder_(NULL, webrtc::kVideoCodecUnknown, false), |
pbos@webrtc.org | b648b9d | 2014-08-26 11:08:06 +0000 | [diff] [blame] | 1646 | capturer_(NULL), |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1647 | sending_(false), |
pbos@webrtc.org | 9a4410e | 2015-02-26 10:03:39 +0000 | [diff] [blame] | 1648 | muted_(false), |
| 1649 | old_adapt_changes_(0) { |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1650 | parameters_.config.rtp.max_packet_size = kVideoMtu; |
| 1651 | |
| 1652 | sp.GetPrimarySsrcs(¶meters_.config.rtp.ssrcs); |
| 1653 | sp.GetFidSsrcs(parameters_.config.rtp.ssrcs, |
| 1654 | ¶meters_.config.rtp.rtx.ssrcs); |
| 1655 | parameters_.config.rtp.c_name = sp.cname; |
| 1656 | parameters_.config.rtp.extensions = rtp_extensions; |
| 1657 | |
| 1658 | VideoCodecSettings params; |
| 1659 | if (codec_settings.Get(¶ms)) { |
| 1660 | SetCodec(params); |
| 1661 | } |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1662 | } |
| 1663 | |
| 1664 | WebRtcVideoChannel2::WebRtcVideoSendStream::~WebRtcVideoSendStream() { |
| 1665 | DisconnectCapturer(); |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1666 | if (stream_ != NULL) { |
| 1667 | call_->DestroyVideoSendStream(stream_); |
| 1668 | } |
pbos@webrtc.org | 7fe1e03 | 2014-10-14 04:25:33 +0000 | [diff] [blame] | 1669 | DestroyVideoEncoder(&allocated_encoder_); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1670 | } |
| 1671 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1672 | static void CreateBlackFrame(webrtc::I420VideoFrame* video_frame, |
| 1673 | int width, |
| 1674 | int height) { |
pbos@webrtc.org | b4987bf | 2015-02-18 10:13:09 +0000 | [diff] [blame] | 1675 | video_frame->CreateEmptyFrame(width, height, width, (width + 1) / 2, |
| 1676 | (width + 1) / 2); |
| 1677 | memset(video_frame->buffer(webrtc::kYPlane), 16, |
| 1678 | video_frame->allocated_size(webrtc::kYPlane)); |
| 1679 | memset(video_frame->buffer(webrtc::kUPlane), 128, |
| 1680 | video_frame->allocated_size(webrtc::kUPlane)); |
| 1681 | memset(video_frame->buffer(webrtc::kVPlane), 128, |
| 1682 | video_frame->allocated_size(webrtc::kVPlane)); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1683 | } |
| 1684 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1685 | void WebRtcVideoChannel2::WebRtcVideoSendStream::InputFrame( |
| 1686 | VideoCapturer* capturer, |
| 1687 | const VideoFrame* frame) { |
pbos@webrtc.org | 86196c4 | 2015-02-16 21:02:00 +0000 | [diff] [blame] | 1688 | TRACE_EVENT0("webrtc", "WebRtcVideoSendStream::InputFrame"); |
magjed@webrtc.org | afdd5dd | 2015-03-12 13:11:25 +0000 | [diff] [blame] | 1689 | webrtc::I420VideoFrame video_frame(frame->GetVideoFrameBuffer(), 0, 0, |
| 1690 | frame->GetVideoRotation()); |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1691 | rtc::CritScope cs(&lock_); |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1692 | if (stream_ == NULL) { |
Peter Boström | ad1f9b6 | 2015-04-08 14:04:01 +0200 | [diff] [blame] | 1693 | // Frame input before send codecs are configured, dropping frame. |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1694 | return; |
| 1695 | } |
pbos@webrtc.org | 86196c4 | 2015-02-16 21:02:00 +0000 | [diff] [blame] | 1696 | |
| 1697 | // Not sending, abort early to prevent expensive reconfigurations while |
| 1698 | // setting up codecs etc. |
| 1699 | if (!sending_) |
| 1700 | return; |
| 1701 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1702 | if (format_.width == 0) { // Dropping frames. |
| 1703 | assert(format_.height == 0); |
| 1704 | LOG(LS_VERBOSE) << "VideoFormat 0x0 set, Dropping frame."; |
| 1705 | return; |
| 1706 | } |
pbos@webrtc.org | d60d79a | 2014-09-24 07:10:57 +0000 | [diff] [blame] | 1707 | if (muted_) { |
| 1708 | // Create a black frame to transmit instead. |
magjed@webrtc.org | afdd5dd | 2015-03-12 13:11:25 +0000 | [diff] [blame] | 1709 | CreateBlackFrame(&video_frame, |
pbos@webrtc.org | d60d79a | 2014-09-24 07:10:57 +0000 | [diff] [blame] | 1710 | static_cast<int>(frame->GetWidth()), |
| 1711 | static_cast<int>(frame->GetHeight())); |
| 1712 | } |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1713 | // Reconfigure codec if necessary. |
pbos@webrtc.org | c4175b9 | 2014-09-03 15:25:49 +0000 | [diff] [blame] | 1714 | SetDimensions( |
magjed@webrtc.org | afdd5dd | 2015-03-12 13:11:25 +0000 | [diff] [blame] | 1715 | video_frame.width(), video_frame.height(), capturer->IsScreencast()); |
pbos@webrtc.org | c4175b9 | 2014-09-03 15:25:49 +0000 | [diff] [blame] | 1716 | |
Alex Glaznev | e433c0e | 2015-05-01 13:54:19 -0700 | [diff] [blame] | 1717 | LOG(LS_VERBOSE) << "IncomingCapturedFrame: " << video_frame.width() << "x" |
| 1718 | << video_frame.height() << " -> (codec) " |
| 1719 | << parameters_.encoder_config.streams.back().width << "x" |
| 1720 | << parameters_.encoder_config.streams.back().height; |
perkj@webrtc.org | af612d5 | 2015-03-18 09:51:05 +0000 | [diff] [blame] | 1721 | stream_->Input()->IncomingCapturedFrame(video_frame); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1722 | } |
| 1723 | |
| 1724 | bool WebRtcVideoChannel2::WebRtcVideoSendStream::SetCapturer( |
| 1725 | VideoCapturer* capturer) { |
pbos@webrtc.org | b4987bf | 2015-02-18 10:13:09 +0000 | [diff] [blame] | 1726 | TRACE_EVENT0("webrtc", "WebRtcVideoSendStream::SetCapturer"); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1727 | if (!DisconnectCapturer() && capturer == NULL) { |
| 1728 | return false; |
| 1729 | } |
| 1730 | |
| 1731 | { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1732 | rtc::CritScope cs(&lock_); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1733 | |
pbos@webrtc.org | 9359cb3 | 2014-07-23 15:44:48 +0000 | [diff] [blame] | 1734 | if (capturer == NULL) { |
| 1735 | if (stream_ != NULL) { |
| 1736 | LOG(LS_VERBOSE) << "Disabling capturer, sending black frame."; |
| 1737 | webrtc::I420VideoFrame black_frame; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1738 | |
pbos@webrtc.org | b4987bf | 2015-02-18 10:13:09 +0000 | [diff] [blame] | 1739 | CreateBlackFrame(&black_frame, last_dimensions_.width, |
| 1740 | last_dimensions_.height); |
perkj@webrtc.org | af612d5 | 2015-03-18 09:51:05 +0000 | [diff] [blame] | 1741 | stream_->Input()->IncomingCapturedFrame(black_frame); |
pbos@webrtc.org | 9359cb3 | 2014-07-23 15:44:48 +0000 | [diff] [blame] | 1742 | } |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1743 | |
| 1744 | capturer_ = NULL; |
| 1745 | return true; |
| 1746 | } |
| 1747 | |
| 1748 | capturer_ = capturer; |
| 1749 | } |
| 1750 | // Lock cannot be held while connecting the capturer to prevent lock-order |
| 1751 | // violations. |
| 1752 | capturer->SignalVideoFrame.connect(this, &WebRtcVideoSendStream::InputFrame); |
| 1753 | return true; |
| 1754 | } |
| 1755 | |
| 1756 | bool WebRtcVideoChannel2::WebRtcVideoSendStream::SetVideoFormat( |
| 1757 | const VideoFormat& format) { |
| 1758 | if ((format.width == 0 || format.height == 0) && |
| 1759 | format.width != format.height) { |
| 1760 | LOG(LS_ERROR) << "Can't set VideoFormat, width or height is zero (but not " |
| 1761 | "both, 0x0 drops frames)."; |
| 1762 | return false; |
| 1763 | } |
| 1764 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1765 | rtc::CritScope cs(&lock_); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1766 | if (format.width == 0 && format.height == 0) { |
| 1767 | LOG(LS_INFO) |
| 1768 | << "0x0 resolution selected. Captured frames will be dropped for ssrc: " |
pbos@webrtc.org | 6ae48c6 | 2014-06-06 10:49:19 +0000 | [diff] [blame] | 1769 | << parameters_.config.rtp.ssrcs[0] << "."; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1770 | } else { |
| 1771 | // TODO(pbos): Fix me, this only affects the last stream! |
pbos@webrtc.org | bbe0a85 | 2014-09-19 12:30:25 +0000 | [diff] [blame] | 1772 | parameters_.encoder_config.streams.back().max_framerate = |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1773 | VideoFormat::IntervalToFps(format.interval); |
pbos@webrtc.org | c4175b9 | 2014-09-03 15:25:49 +0000 | [diff] [blame] | 1774 | SetDimensions(format.width, format.height, false); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1775 | } |
| 1776 | |
| 1777 | format_ = format; |
| 1778 | return true; |
| 1779 | } |
| 1780 | |
pbos@webrtc.org | ef8bb8d | 2014-08-13 21:36:18 +0000 | [diff] [blame] | 1781 | void WebRtcVideoChannel2::WebRtcVideoSendStream::MuteStream(bool mute) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1782 | rtc::CritScope cs(&lock_); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1783 | muted_ = mute; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1784 | } |
| 1785 | |
| 1786 | bool WebRtcVideoChannel2::WebRtcVideoSendStream::DisconnectCapturer() { |
pbos@webrtc.org | 963b979 | 2014-10-07 14:27:27 +0000 | [diff] [blame] | 1787 | cricket::VideoCapturer* capturer; |
| 1788 | { |
| 1789 | rtc::CritScope cs(&lock_); |
pbos@webrtc.org | 9a4410e | 2015-02-26 10:03:39 +0000 | [diff] [blame] | 1790 | if (capturer_ == NULL) |
pbos@webrtc.org | 963b979 | 2014-10-07 14:27:27 +0000 | [diff] [blame] | 1791 | return false; |
pbos@webrtc.org | 9a4410e | 2015-02-26 10:03:39 +0000 | [diff] [blame] | 1792 | |
| 1793 | if (capturer_->video_adapter() != nullptr) |
| 1794 | old_adapt_changes_ += capturer_->video_adapter()->adaptation_changes(); |
| 1795 | |
pbos@webrtc.org | 963b979 | 2014-10-07 14:27:27 +0000 | [diff] [blame] | 1796 | capturer = capturer_; |
| 1797 | capturer_ = NULL; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1798 | } |
pbos@webrtc.org | 963b979 | 2014-10-07 14:27:27 +0000 | [diff] [blame] | 1799 | capturer->SignalVideoFrame.disconnect(this); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1800 | return true; |
| 1801 | } |
| 1802 | |
Peter Boström | d6f4c25 | 2015-03-26 16:23:04 +0100 | [diff] [blame] | 1803 | const std::vector<uint32>& |
| 1804 | WebRtcVideoChannel2::WebRtcVideoSendStream::GetSsrcs() const { |
| 1805 | return ssrcs_; |
| 1806 | } |
| 1807 | |
Guo-wei Shieh | 64c1e8c | 2015-04-01 15:33:06 -0700 | [diff] [blame] | 1808 | void WebRtcVideoChannel2::WebRtcVideoSendStream::SetApplyRotation( |
| 1809 | bool apply_rotation) { |
| 1810 | rtc::CritScope cs(&lock_); |
| 1811 | if (capturer_ == NULL) |
| 1812 | return; |
| 1813 | |
| 1814 | capturer_->SetApplyRotation(apply_rotation); |
| 1815 | } |
| 1816 | |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1817 | void WebRtcVideoChannel2::WebRtcVideoSendStream::SetOptions( |
| 1818 | const VideoOptions& options) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1819 | rtc::CritScope cs(&lock_); |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1820 | VideoCodecSettings codec_settings; |
| 1821 | if (parameters_.codec_settings.Get(&codec_settings)) { |
| 1822 | SetCodecAndOptions(codec_settings, options); |
| 1823 | } else { |
| 1824 | parameters_.options = options; |
| 1825 | } |
| 1826 | } |
pbos@webrtc.org | 7fe1e03 | 2014-10-14 04:25:33 +0000 | [diff] [blame] | 1827 | |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1828 | void WebRtcVideoChannel2::WebRtcVideoSendStream::SetCodec( |
| 1829 | const VideoCodecSettings& codec_settings) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1830 | rtc::CritScope cs(&lock_); |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1831 | SetCodecAndOptions(codec_settings, parameters_.options); |
| 1832 | } |
pbos@webrtc.org | 7fe1e03 | 2014-10-14 04:25:33 +0000 | [diff] [blame] | 1833 | |
| 1834 | webrtc::VideoCodecType CodecTypeFromName(const std::string& name) { |
Shao Changbin | e62202f | 2015-04-21 20:24:50 +0800 | [diff] [blame] | 1835 | if (CodecNamesEq(name, kVp8CodecName)) { |
pbos@webrtc.org | 7fe1e03 | 2014-10-14 04:25:33 +0000 | [diff] [blame] | 1836 | return webrtc::kVideoCodecVP8; |
Shao Changbin | e62202f | 2015-04-21 20:24:50 +0800 | [diff] [blame] | 1837 | } else if (CodecNamesEq(name, kVp9CodecName)) { |
andresp@webrtc.org | 188d3b2 | 2014-11-07 13:21:04 +0000 | [diff] [blame] | 1838 | return webrtc::kVideoCodecVP9; |
Shao Changbin | e62202f | 2015-04-21 20:24:50 +0800 | [diff] [blame] | 1839 | } else if (CodecNamesEq(name, kH264CodecName)) { |
pbos@webrtc.org | 7fe1e03 | 2014-10-14 04:25:33 +0000 | [diff] [blame] | 1840 | return webrtc::kVideoCodecH264; |
| 1841 | } |
| 1842 | return webrtc::kVideoCodecUnknown; |
| 1843 | } |
| 1844 | |
| 1845 | WebRtcVideoChannel2::WebRtcVideoSendStream::AllocatedEncoder |
| 1846 | WebRtcVideoChannel2::WebRtcVideoSendStream::CreateVideoEncoder( |
| 1847 | const VideoCodec& codec) { |
| 1848 | webrtc::VideoCodecType type = CodecTypeFromName(codec.name); |
| 1849 | |
| 1850 | // Do not re-create encoders of the same type. |
| 1851 | if (type == allocated_encoder_.type && allocated_encoder_.encoder != NULL) { |
| 1852 | return allocated_encoder_; |
| 1853 | } |
| 1854 | |
| 1855 | if (external_encoder_factory_ != NULL) { |
| 1856 | webrtc::VideoEncoder* encoder = |
| 1857 | external_encoder_factory_->CreateVideoEncoder(type); |
| 1858 | if (encoder != NULL) { |
| 1859 | return AllocatedEncoder(encoder, type, true); |
| 1860 | } |
| 1861 | } |
| 1862 | |
| 1863 | if (type == webrtc::kVideoCodecVP8) { |
| 1864 | return AllocatedEncoder( |
| 1865 | webrtc::VideoEncoder::Create(webrtc::VideoEncoder::kVp8), type, false); |
andresp@webrtc.org | 188d3b2 | 2014-11-07 13:21:04 +0000 | [diff] [blame] | 1866 | } else if (type == webrtc::kVideoCodecVP9) { |
| 1867 | return AllocatedEncoder( |
| 1868 | webrtc::VideoEncoder::Create(webrtc::VideoEncoder::kVp9), type, false); |
pbos@webrtc.org | 7fe1e03 | 2014-10-14 04:25:33 +0000 | [diff] [blame] | 1869 | } |
| 1870 | |
| 1871 | // This shouldn't happen, we should not be trying to create something we don't |
| 1872 | // support. |
| 1873 | assert(false); |
| 1874 | return AllocatedEncoder(NULL, webrtc::kVideoCodecUnknown, false); |
| 1875 | } |
| 1876 | |
| 1877 | void WebRtcVideoChannel2::WebRtcVideoSendStream::DestroyVideoEncoder( |
| 1878 | AllocatedEncoder* encoder) { |
| 1879 | if (encoder->external) { |
| 1880 | external_encoder_factory_->DestroyVideoEncoder(encoder->encoder); |
| 1881 | } else { |
| 1882 | delete encoder->encoder; |
| 1883 | } |
| 1884 | } |
| 1885 | |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1886 | void WebRtcVideoChannel2::WebRtcVideoSendStream::SetCodecAndOptions( |
| 1887 | const VideoCodecSettings& codec_settings, |
| 1888 | const VideoOptions& options) { |
pbos@webrtc.org | a2ef4fe | 2014-11-07 10:54:43 +0000 | [diff] [blame] | 1889 | parameters_.encoder_config = |
| 1890 | CreateVideoEncoderConfig(last_dimensions_, codec_settings.codec); |
pbos@webrtc.org | 86196c4 | 2015-02-16 21:02:00 +0000 | [diff] [blame] | 1891 | if (parameters_.encoder_config.streams.empty()) |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1892 | return; |
pbos@webrtc.org | a2ef4fe | 2014-11-07 10:54:43 +0000 | [diff] [blame] | 1893 | |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1894 | format_ = VideoFormat(codec_settings.codec.width, |
| 1895 | codec_settings.codec.height, |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1896 | VideoFormat::FpsToInterval(30), |
| 1897 | FOURCC_I420); |
pbos@webrtc.org | 6ae48c6 | 2014-06-06 10:49:19 +0000 | [diff] [blame] | 1898 | |
pbos@webrtc.org | 7fe1e03 | 2014-10-14 04:25:33 +0000 | [diff] [blame] | 1899 | AllocatedEncoder new_encoder = CreateVideoEncoder(codec_settings.codec); |
| 1900 | parameters_.config.encoder_settings.encoder = new_encoder.encoder; |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1901 | parameters_.config.encoder_settings.payload_name = codec_settings.codec.name; |
| 1902 | parameters_.config.encoder_settings.payload_type = codec_settings.codec.id; |
| 1903 | parameters_.config.rtp.fec = codec_settings.fec; |
| 1904 | |
| 1905 | // Set RTX payload type if RTX is enabled. |
| 1906 | if (!parameters_.config.rtp.rtx.ssrcs.empty()) { |
pbos@webrtc.org | b9557a9 | 2015-03-20 19:52:56 +0000 | [diff] [blame] | 1907 | if (codec_settings.rtx_payload_type == -1) { |
| 1908 | LOG(LS_WARNING) << "RTX SSRCs configured but there's no configured RTX " |
| 1909 | "payload type. Ignoring."; |
| 1910 | parameters_.config.rtp.rtx.ssrcs.clear(); |
| 1911 | } else { |
| 1912 | parameters_.config.rtp.rtx.payload_type = codec_settings.rtx_payload_type; |
| 1913 | } |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1914 | } |
| 1915 | |
Shao Changbin | e62202f | 2015-04-21 20:24:50 +0800 | [diff] [blame] | 1916 | if (HasNack(codec_settings.codec)) { |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1917 | parameters_.config.rtp.nack.rtp_history_ms = kNackHistoryMs; |
| 1918 | } |
| 1919 | |
pbos@webrtc.org | 5ff71ab | 2014-07-23 07:28:56 +0000 | [diff] [blame] | 1920 | options.suspend_below_min_bitrate.Get( |
| 1921 | ¶meters_.config.suspend_below_min_bitrate); |
| 1922 | |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1923 | parameters_.codec_settings.Set(codec_settings); |
pbos@webrtc.org | 6ae48c6 | 2014-06-06 10:49:19 +0000 | [diff] [blame] | 1924 | parameters_.options = options; |
pbos@webrtc.org | 543e589 | 2014-07-23 07:01:31 +0000 | [diff] [blame] | 1925 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1926 | RecreateWebRtcStream(); |
pbos@webrtc.org | 7fe1e03 | 2014-10-14 04:25:33 +0000 | [diff] [blame] | 1927 | if (allocated_encoder_.encoder != new_encoder.encoder) { |
| 1928 | DestroyVideoEncoder(&allocated_encoder_); |
| 1929 | allocated_encoder_ = new_encoder; |
| 1930 | } |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1931 | } |
| 1932 | |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 1933 | void WebRtcVideoChannel2::WebRtcVideoSendStream::SetRtpExtensions( |
| 1934 | const std::vector<webrtc::RtpExtension>& rtp_extensions) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1935 | rtc::CritScope cs(&lock_); |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 1936 | parameters_.config.rtp.extensions = rtp_extensions; |
Peter Boström | 3c3f646 | 2015-04-15 16:27:49 +0200 | [diff] [blame] | 1937 | if (stream_ != nullptr) |
| 1938 | RecreateWebRtcStream(); |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 1939 | } |
| 1940 | |
pbos@webrtc.org | a2ef4fe | 2014-11-07 10:54:43 +0000 | [diff] [blame] | 1941 | webrtc::VideoEncoderConfig |
| 1942 | WebRtcVideoChannel2::WebRtcVideoSendStream::CreateVideoEncoderConfig( |
| 1943 | const Dimensions& dimensions, |
| 1944 | const VideoCodec& codec) const { |
| 1945 | webrtc::VideoEncoderConfig encoder_config; |
| 1946 | if (dimensions.is_screencast) { |
pbos@webrtc.org | efc82c2 | 2014-10-27 13:58:00 +0000 | [diff] [blame] | 1947 | int screencast_min_bitrate_kbps; |
| 1948 | parameters_.options.screencast_min_bitrate.Get( |
| 1949 | &screencast_min_bitrate_kbps); |
| 1950 | encoder_config.min_transmit_bitrate_bps = |
| 1951 | screencast_min_bitrate_kbps * 1000; |
Erik Språng | 143cec1 | 2015-04-28 10:01:41 +0200 | [diff] [blame] | 1952 | encoder_config.content_type = |
| 1953 | webrtc::VideoEncoderConfig::ContentType::kScreen; |
pbos@webrtc.org | efc82c2 | 2014-10-27 13:58:00 +0000 | [diff] [blame] | 1954 | } else { |
| 1955 | encoder_config.min_transmit_bitrate_bps = 0; |
Erik Språng | 143cec1 | 2015-04-28 10:01:41 +0200 | [diff] [blame] | 1956 | encoder_config.content_type = |
| 1957 | webrtc::VideoEncoderConfig::ContentType::kRealtimeVideo; |
pbos@webrtc.org | efc82c2 | 2014-10-27 13:58:00 +0000 | [diff] [blame] | 1958 | } |
| 1959 | |
pbos@webrtc.org | a2ef4fe | 2014-11-07 10:54:43 +0000 | [diff] [blame] | 1960 | // Restrict dimensions according to codec max. |
| 1961 | int width = dimensions.width; |
| 1962 | int height = dimensions.height; |
| 1963 | if (!dimensions.is_screencast) { |
| 1964 | if (codec.width < width) |
| 1965 | width = codec.width; |
| 1966 | if (codec.height < height) |
| 1967 | height = codec.height; |
| 1968 | } |
| 1969 | |
| 1970 | VideoCodec clamped_codec = codec; |
| 1971 | clamped_codec.width = width; |
| 1972 | clamped_codec.height = height; |
pbos@webrtc.org | cddd17c | 2014-09-16 16:33:13 +0000 | [diff] [blame] | 1973 | |
pbos@webrtc.org | f1c8b90 | 2015-01-14 17:29:27 +0000 | [diff] [blame] | 1974 | encoder_config.streams = CreateVideoStreams( |
Peter Boström | dfd53fe | 2015-03-27 15:58:11 +0100 | [diff] [blame] | 1975 | clamped_codec, parameters_.options, parameters_.max_bitrate_bps, |
Erik Språng | 143cec1 | 2015-04-28 10:01:41 +0200 | [diff] [blame] | 1976 | dimensions.is_screencast ? 1 : parameters_.config.rtp.ssrcs.size()); |
pbos@webrtc.org | 6f48f1b | 2014-07-22 16:29:54 +0000 | [diff] [blame] | 1977 | |
pbos@webrtc.org | b7ed779 | 2014-10-31 13:08:10 +0000 | [diff] [blame] | 1978 | // Conference mode screencast uses 2 temporal layers split at 100kbit. |
| 1979 | if (parameters_.options.conference_mode.GetWithDefaultIfUnset(false) && |
pbos@webrtc.org | a2ef4fe | 2014-11-07 10:54:43 +0000 | [diff] [blame] | 1980 | dimensions.is_screencast && encoder_config.streams.size() == 1) { |
sprang@webrtc.org | 46d4d29 | 2014-12-23 15:19:35 +0000 | [diff] [blame] | 1981 | ScreenshareLayerConfig config = ScreenshareLayerConfig::GetDefault(); |
| 1982 | |
| 1983 | // For screenshare in conference mode, tl0 and tl1 bitrates are piggybacked |
| 1984 | // on the VideoCodec struct as target and max bitrates, respectively. |
| 1985 | // See eg. webrtc::VP8EncoderImpl::SetRates(). |
| 1986 | encoder_config.streams[0].target_bitrate_bps = |
| 1987 | config.tl0_bitrate_kbps * 1000; |
| 1988 | 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] | 1989 | encoder_config.streams[0].temporal_layer_thresholds_bps.clear(); |
| 1990 | encoder_config.streams[0].temporal_layer_thresholds_bps.push_back( |
sprang@webrtc.org | 46d4d29 | 2014-12-23 15:19:35 +0000 | [diff] [blame] | 1991 | config.tl0_bitrate_kbps * 1000); |
pbos@webrtc.org | b7ed779 | 2014-10-31 13:08:10 +0000 | [diff] [blame] | 1992 | } |
pbos@webrtc.org | a2ef4fe | 2014-11-07 10:54:43 +0000 | [diff] [blame] | 1993 | return encoder_config; |
| 1994 | } |
| 1995 | |
| 1996 | void WebRtcVideoChannel2::WebRtcVideoSendStream::SetDimensions( |
| 1997 | int width, |
| 1998 | int height, |
| 1999 | bool is_screencast) { |
| 2000 | if (last_dimensions_.width == width && last_dimensions_.height == height && |
| 2001 | last_dimensions_.is_screencast == is_screencast) { |
| 2002 | // Configured using the same parameters, do not reconfigure. |
| 2003 | return; |
| 2004 | } |
| 2005 | LOG(LS_INFO) << "SetDimensions: " << width << "x" << height |
| 2006 | << (is_screencast ? " (screencast)" : " (not screencast)"); |
| 2007 | |
| 2008 | last_dimensions_.width = width; |
| 2009 | last_dimensions_.height = height; |
| 2010 | last_dimensions_.is_screencast = is_screencast; |
| 2011 | |
| 2012 | assert(!parameters_.encoder_config.streams.empty()); |
| 2013 | |
| 2014 | VideoCodecSettings codec_settings; |
| 2015 | parameters_.codec_settings.Get(&codec_settings); |
| 2016 | |
| 2017 | webrtc::VideoEncoderConfig encoder_config = |
| 2018 | CreateVideoEncoderConfig(last_dimensions_, codec_settings.codec); |
| 2019 | |
Erik Språng | 143cec1 | 2015-04-28 10:01:41 +0200 | [diff] [blame] | 2020 | encoder_config.encoder_specific_settings = ConfigureVideoEncoderSettings( |
| 2021 | codec_settings.codec, parameters_.options, is_screencast); |
pbos@webrtc.org | b7ed779 | 2014-10-31 13:08:10 +0000 | [diff] [blame] | 2022 | |
pbos@webrtc.org | bbe0a85 | 2014-09-19 12:30:25 +0000 | [diff] [blame] | 2023 | bool stream_reconfigured = stream_->ReconfigureVideoEncoder(encoder_config); |
| 2024 | |
pbos@webrtc.org | bbe0a85 | 2014-09-19 12:30:25 +0000 | [diff] [blame] | 2025 | encoder_config.encoder_specific_settings = NULL; |
pbos@webrtc.org | 6f48f1b | 2014-07-22 16:29:54 +0000 | [diff] [blame] | 2026 | |
| 2027 | if (!stream_reconfigured) { |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 2028 | LOG(LS_WARNING) << "Failed to reconfigure video encoder for dimensions: " |
| 2029 | << width << "x" << height; |
| 2030 | return; |
| 2031 | } |
pbos@webrtc.org | cddd17c | 2014-09-16 16:33:13 +0000 | [diff] [blame] | 2032 | |
pbos@webrtc.org | bbe0a85 | 2014-09-19 12:30:25 +0000 | [diff] [blame] | 2033 | parameters_.encoder_config = encoder_config; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 2034 | } |
| 2035 | |
| 2036 | void WebRtcVideoChannel2::WebRtcVideoSendStream::Start() { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 2037 | rtc::CritScope cs(&lock_); |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 2038 | assert(stream_ != NULL); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 2039 | stream_->Start(); |
| 2040 | sending_ = true; |
| 2041 | } |
| 2042 | |
| 2043 | void WebRtcVideoChannel2::WebRtcVideoSendStream::Stop() { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 2044 | rtc::CritScope cs(&lock_); |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 2045 | if (stream_ != NULL) { |
| 2046 | stream_->Stop(); |
| 2047 | } |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 2048 | sending_ = false; |
| 2049 | } |
| 2050 | |
pbos@webrtc.org | e6f84ae | 2014-07-18 11:11:55 +0000 | [diff] [blame] | 2051 | VideoSenderInfo |
| 2052 | WebRtcVideoChannel2::WebRtcVideoSendStream::GetVideoSenderInfo() { |
| 2053 | VideoSenderInfo info; |
pbos@webrtc.org | 77e11bb | 2015-02-23 16:39:07 +0000 | [diff] [blame] | 2054 | webrtc::VideoSendStream::Stats stats; |
| 2055 | { |
| 2056 | rtc::CritScope cs(&lock_); |
| 2057 | for (uint32_t ssrc : parameters_.config.rtp.ssrcs) |
| 2058 | info.add_ssrc(ssrc); |
pbos@webrtc.org | e6f84ae | 2014-07-18 11:11:55 +0000 | [diff] [blame] | 2059 | |
Peter Boström | 74d9ed7 | 2015-03-26 16:28:31 +0100 | [diff] [blame] | 2060 | VideoCodecSettings codec_settings; |
| 2061 | if (parameters_.codec_settings.Get(&codec_settings)) |
| 2062 | info.codec_name = codec_settings.codec.name; |
pbos@webrtc.org | 77e11bb | 2015-02-23 16:39:07 +0000 | [diff] [blame] | 2063 | for (size_t i = 0; i < parameters_.encoder_config.streams.size(); ++i) { |
| 2064 | if (i == parameters_.encoder_config.streams.size() - 1) { |
| 2065 | info.preferred_bitrate += |
| 2066 | parameters_.encoder_config.streams[i].max_bitrate_bps; |
| 2067 | } else { |
| 2068 | info.preferred_bitrate += |
| 2069 | parameters_.encoder_config.streams[i].target_bitrate_bps; |
| 2070 | } |
| 2071 | } |
pbos@webrtc.org | c3d2bd2 | 2014-08-12 20:55:10 +0000 | [diff] [blame] | 2072 | |
pbos@webrtc.org | 77e11bb | 2015-02-23 16:39:07 +0000 | [diff] [blame] | 2073 | if (stream_ == NULL) |
| 2074 | return info; |
| 2075 | |
| 2076 | stats = stream_->GetStats(); |
| 2077 | |
pbos@webrtc.org | 9a4410e | 2015-02-26 10:03:39 +0000 | [diff] [blame] | 2078 | info.adapt_changes = old_adapt_changes_; |
| 2079 | info.adapt_reason = CoordinatedVideoAdapter::ADAPTREASON_NONE; |
| 2080 | |
| 2081 | if (capturer_ != NULL) { |
| 2082 | if (!capturer_->IsMuted()) { |
| 2083 | VideoFormat last_captured_frame_format; |
| 2084 | capturer_->GetStats(&info.adapt_frame_drops, &info.effects_frame_drops, |
| 2085 | &info.capturer_frame_time, |
| 2086 | &last_captured_frame_format); |
| 2087 | info.input_frame_width = last_captured_frame_format.width; |
| 2088 | info.input_frame_height = last_captured_frame_format.height; |
| 2089 | } |
| 2090 | if (capturer_->video_adapter() != nullptr) { |
| 2091 | info.adapt_changes += capturer_->video_adapter()->adaptation_changes(); |
| 2092 | info.adapt_reason = capturer_->video_adapter()->adapt_reason(); |
| 2093 | } |
pbos@webrtc.org | 77e11bb | 2015-02-23 16:39:07 +0000 | [diff] [blame] | 2094 | } |
| 2095 | } |
pbos@webrtc.org | e6f84ae | 2014-07-18 11:11:55 +0000 | [diff] [blame] | 2096 | info.framerate_input = stats.input_frame_rate; |
| 2097 | info.framerate_sent = stats.encode_frame_rate; |
pbos@webrtc.org | 3e6e271 | 2015-02-26 12:19:31 +0000 | [diff] [blame] | 2098 | info.avg_encode_ms = stats.avg_encode_time_ms; |
| 2099 | info.encode_usage_percent = stats.encode_usage_percent; |
pbos@webrtc.org | e6f84ae | 2014-07-18 11:11:55 +0000 | [diff] [blame] | 2100 | |
pbos@webrtc.org | 77e11bb | 2015-02-23 16:39:07 +0000 | [diff] [blame] | 2101 | info.nominal_bitrate = stats.media_bitrate_bps; |
| 2102 | |
pbos@webrtc.org | 273a414 | 2014-12-01 15:23:21 +0000 | [diff] [blame] | 2103 | info.send_frame_width = 0; |
| 2104 | info.send_frame_height = 0; |
pbos@webrtc.org | 09c77b9 | 2015-02-25 10:42:16 +0000 | [diff] [blame] | 2105 | for (std::map<uint32_t, webrtc::VideoSendStream::StreamStats>::iterator it = |
pbos@webrtc.org | e6f84ae | 2014-07-18 11:11:55 +0000 | [diff] [blame] | 2106 | stats.substreams.begin(); |
pbos@webrtc.org | 09c77b9 | 2015-02-25 10:42:16 +0000 | [diff] [blame] | 2107 | it != stats.substreams.end(); ++it) { |
pbos@webrtc.org | e6f84ae | 2014-07-18 11:11:55 +0000 | [diff] [blame] | 2108 | // TODO(pbos): Wire up additional stats, such as padding bytes. |
pbos@webrtc.org | 09c77b9 | 2015-02-25 10:42:16 +0000 | [diff] [blame] | 2109 | webrtc::VideoSendStream::StreamStats stream_stats = it->second; |
asapersson@webrtc.org | cfd82df | 2015-01-22 09:39:59 +0000 | [diff] [blame] | 2110 | info.bytes_sent += stream_stats.rtp_stats.transmitted.payload_bytes + |
| 2111 | stream_stats.rtp_stats.transmitted.header_bytes + |
| 2112 | stream_stats.rtp_stats.transmitted.padding_bytes; |
| 2113 | info.packets_sent += stream_stats.rtp_stats.transmitted.packets; |
pbos@webrtc.org | e6f84ae | 2014-07-18 11:11:55 +0000 | [diff] [blame] | 2114 | info.packets_lost += stream_stats.rtcp_stats.cumulative_lost; |
pbos@webrtc.org | 09c77b9 | 2015-02-25 10:42:16 +0000 | [diff] [blame] | 2115 | if (stream_stats.width > info.send_frame_width) |
| 2116 | info.send_frame_width = stream_stats.width; |
| 2117 | if (stream_stats.height > info.send_frame_height) |
| 2118 | info.send_frame_height = stream_stats.height; |
pbos@webrtc.org | 1d0fa5d | 2015-02-19 12:47:00 +0000 | [diff] [blame] | 2119 | info.firs_rcvd += stream_stats.rtcp_packet_type_counts.fir_packets; |
| 2120 | info.nacks_rcvd += stream_stats.rtcp_packet_type_counts.nack_packets; |
| 2121 | info.plis_rcvd += stream_stats.rtcp_packet_type_counts.pli_packets; |
pbos@webrtc.org | e6f84ae | 2014-07-18 11:11:55 +0000 | [diff] [blame] | 2122 | } |
| 2123 | |
| 2124 | if (!stats.substreams.empty()) { |
| 2125 | // TODO(pbos): Report fraction lost per SSRC. |
pbos@webrtc.org | 09c77b9 | 2015-02-25 10:42:16 +0000 | [diff] [blame] | 2126 | webrtc::VideoSendStream::StreamStats first_stream_stats = |
| 2127 | stats.substreams.begin()->second; |
pbos@webrtc.org | e6f84ae | 2014-07-18 11:11:55 +0000 | [diff] [blame] | 2128 | info.fraction_lost = |
| 2129 | static_cast<float>(first_stream_stats.rtcp_stats.fraction_lost) / |
| 2130 | (1 << 8); |
| 2131 | } |
| 2132 | |
pbos@webrtc.org | e6f84ae | 2014-07-18 11:11:55 +0000 | [diff] [blame] | 2133 | return info; |
| 2134 | } |
| 2135 | |
stefan@webrtc.org | 0bae1fa | 2014-11-05 14:05:29 +0000 | [diff] [blame] | 2136 | void WebRtcVideoChannel2::WebRtcVideoSendStream::FillBandwidthEstimationInfo( |
| 2137 | BandwidthEstimationInfo* bwe_info) { |
| 2138 | rtc::CritScope cs(&lock_); |
| 2139 | if (stream_ == NULL) { |
| 2140 | return; |
| 2141 | } |
| 2142 | webrtc::VideoSendStream::Stats stats = stream_->GetStats(); |
pbos@webrtc.org | 09c77b9 | 2015-02-25 10:42:16 +0000 | [diff] [blame] | 2143 | for (std::map<uint32_t, webrtc::VideoSendStream::StreamStats>::iterator it = |
stefan@webrtc.org | 0bae1fa | 2014-11-05 14:05:29 +0000 | [diff] [blame] | 2144 | stats.substreams.begin(); |
pbos@webrtc.org | 09c77b9 | 2015-02-25 10:42:16 +0000 | [diff] [blame] | 2145 | it != stats.substreams.end(); ++it) { |
stefan@webrtc.org | 0bae1fa | 2014-11-05 14:05:29 +0000 | [diff] [blame] | 2146 | bwe_info->transmit_bitrate += it->second.total_bitrate_bps; |
| 2147 | bwe_info->retransmit_bitrate += it->second.retransmit_bitrate_bps; |
| 2148 | } |
pbos@webrtc.org | 891d483 | 2015-02-26 13:15:22 +0000 | [diff] [blame] | 2149 | bwe_info->target_enc_bitrate += stats.target_media_bitrate_bps; |
pbos@webrtc.org | 77e11bb | 2015-02-23 16:39:07 +0000 | [diff] [blame] | 2150 | bwe_info->actual_enc_bitrate += stats.media_bitrate_bps; |
stefan@webrtc.org | 0bae1fa | 2014-11-05 14:05:29 +0000 | [diff] [blame] | 2151 | } |
| 2152 | |
Peter Boström | dfd53fe | 2015-03-27 15:58:11 +0100 | [diff] [blame] | 2153 | void WebRtcVideoChannel2::WebRtcVideoSendStream::SetMaxBitrateBps( |
| 2154 | int max_bitrate_bps) { |
| 2155 | rtc::CritScope cs(&lock_); |
| 2156 | parameters_.max_bitrate_bps = max_bitrate_bps; |
| 2157 | |
| 2158 | // No need to reconfigure if the stream hasn't been configured yet. |
| 2159 | if (parameters_.encoder_config.streams.empty()) |
| 2160 | return; |
| 2161 | |
| 2162 | // Force a stream reconfigure to set the new max bitrate. |
| 2163 | int width = last_dimensions_.width; |
| 2164 | last_dimensions_.width = 0; |
| 2165 | SetDimensions(width, last_dimensions_.height, last_dimensions_.is_screencast); |
| 2166 | } |
pbos@webrtc.org | 42684be | 2014-10-03 11:25:45 +0000 | [diff] [blame] | 2167 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 2168 | void WebRtcVideoChannel2::WebRtcVideoSendStream::RecreateWebRtcStream() { |
| 2169 | if (stream_ != NULL) { |
| 2170 | call_->DestroyVideoSendStream(stream_); |
| 2171 | } |
pbos@webrtc.org | 6ae48c6 | 2014-06-06 10:49:19 +0000 | [diff] [blame] | 2172 | |
pbos@webrtc.org | 6f48f1b | 2014-07-22 16:29:54 +0000 | [diff] [blame] | 2173 | VideoCodecSettings codec_settings; |
| 2174 | parameters_.codec_settings.Get(&codec_settings); |
pbos@webrtc.org | bbe0a85 | 2014-09-19 12:30:25 +0000 | [diff] [blame] | 2175 | parameters_.encoder_config.encoder_specific_settings = |
Erik Språng | 143cec1 | 2015-04-28 10:01:41 +0200 | [diff] [blame] | 2176 | ConfigureVideoEncoderSettings( |
| 2177 | codec_settings.codec, parameters_.options, |
| 2178 | parameters_.encoder_config.content_type == |
| 2179 | webrtc::VideoEncoderConfig::ContentType::kScreen); |
pbos@webrtc.org | 6f48f1b | 2014-07-22 16:29:54 +0000 | [diff] [blame] | 2180 | |
pbos@webrtc.org | b9557a9 | 2015-03-20 19:52:56 +0000 | [diff] [blame] | 2181 | webrtc::VideoSendStream::Config config = parameters_.config; |
| 2182 | if (!config.rtp.rtx.ssrcs.empty() && config.rtp.rtx.payload_type == -1) { |
| 2183 | LOG(LS_WARNING) << "RTX SSRCs configured but there's no configured RTX " |
| 2184 | "payload type the set codec. Ignoring RTX."; |
| 2185 | config.rtp.rtx.ssrcs.clear(); |
| 2186 | } |
| 2187 | stream_ = call_->CreateVideoSendStream(config, parameters_.encoder_config); |
pbos@webrtc.org | 6f48f1b | 2014-07-22 16:29:54 +0000 | [diff] [blame] | 2188 | |
pbos@webrtc.org | bbe0a85 | 2014-09-19 12:30:25 +0000 | [diff] [blame] | 2189 | parameters_.encoder_config.encoder_specific_settings = NULL; |
pbos@webrtc.org | 6f48f1b | 2014-07-22 16:29:54 +0000 | [diff] [blame] | 2190 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 2191 | if (sending_) { |
| 2192 | stream_->Start(); |
| 2193 | } |
| 2194 | } |
| 2195 | |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 2196 | WebRtcVideoChannel2::WebRtcVideoReceiveStream::WebRtcVideoReceiveStream( |
| 2197 | webrtc::Call* call, |
Peter Boström | d6f4c25 | 2015-03-26 16:23:04 +0100 | [diff] [blame] | 2198 | const std::vector<uint32>& ssrcs, |
pbos@webrtc.org | 776e6f2 | 2014-10-29 15:28:39 +0000 | [diff] [blame] | 2199 | WebRtcVideoDecoderFactory* external_decoder_factory, |
pbos@webrtc.org | a2a6fe6 | 2015-03-06 15:35:19 +0000 | [diff] [blame] | 2200 | bool default_stream, |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 2201 | const webrtc::VideoReceiveStream::Config& config, |
| 2202 | const std::vector<VideoCodecSettings>& recv_codecs) |
| 2203 | : call_(call), |
Peter Boström | d6f4c25 | 2015-03-26 16:23:04 +0100 | [diff] [blame] | 2204 | ssrcs_(ssrcs), |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 2205 | stream_(NULL), |
pbos@webrtc.org | a2a6fe6 | 2015-03-06 15:35:19 +0000 | [diff] [blame] | 2206 | default_stream_(default_stream), |
pbos@webrtc.org | b648b9d | 2014-08-26 11:08:06 +0000 | [diff] [blame] | 2207 | config_(config), |
pbos@webrtc.org | 776e6f2 | 2014-10-29 15:28:39 +0000 | [diff] [blame] | 2208 | external_decoder_factory_(external_decoder_factory), |
pbos@webrtc.org | b648b9d | 2014-08-26 11:08:06 +0000 | [diff] [blame] | 2209 | renderer_(NULL), |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 2210 | last_width_(-1), |
magjed@webrtc.org | fc5ad95 | 2015-01-27 09:57:01 +0000 | [diff] [blame] | 2211 | last_height_(-1), |
| 2212 | first_frame_timestamp_(-1), |
| 2213 | estimated_remote_start_ntp_time_ms_(0) { |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 2214 | config_.renderer = this; |
| 2215 | // SetRecvCodecs will also reset (start) the VideoReceiveStream. |
| 2216 | SetRecvCodecs(recv_codecs); |
| 2217 | } |
| 2218 | |
| 2219 | WebRtcVideoChannel2::WebRtcVideoReceiveStream::~WebRtcVideoReceiveStream() { |
| 2220 | call_->DestroyVideoReceiveStream(stream_); |
pbos@webrtc.org | 96a9325 | 2014-11-03 14:46:44 +0000 | [diff] [blame] | 2221 | ClearDecoders(&allocated_decoders_); |
| 2222 | } |
| 2223 | |
Peter Boström | d6f4c25 | 2015-03-26 16:23:04 +0100 | [diff] [blame] | 2224 | const std::vector<uint32>& |
| 2225 | WebRtcVideoChannel2::WebRtcVideoReceiveStream::GetSsrcs() const { |
| 2226 | return ssrcs_; |
| 2227 | } |
| 2228 | |
pbos@webrtc.org | 96a9325 | 2014-11-03 14:46:44 +0000 | [diff] [blame] | 2229 | WebRtcVideoChannel2::WebRtcVideoReceiveStream::AllocatedDecoder |
| 2230 | WebRtcVideoChannel2::WebRtcVideoReceiveStream::CreateOrReuseVideoDecoder( |
| 2231 | std::vector<AllocatedDecoder>* old_decoders, |
| 2232 | const VideoCodec& codec) { |
| 2233 | webrtc::VideoCodecType type = CodecTypeFromName(codec.name); |
| 2234 | |
| 2235 | for (size_t i = 0; i < old_decoders->size(); ++i) { |
| 2236 | if ((*old_decoders)[i].type == type) { |
| 2237 | AllocatedDecoder decoder = (*old_decoders)[i]; |
| 2238 | (*old_decoders)[i] = old_decoders->back(); |
| 2239 | old_decoders->pop_back(); |
| 2240 | return decoder; |
| 2241 | } |
| 2242 | } |
| 2243 | |
| 2244 | if (external_decoder_factory_ != NULL) { |
| 2245 | webrtc::VideoDecoder* decoder = |
| 2246 | external_decoder_factory_->CreateVideoDecoder(type); |
| 2247 | if (decoder != NULL) { |
| 2248 | return AllocatedDecoder(decoder, type, true); |
| 2249 | } |
| 2250 | } |
| 2251 | |
| 2252 | if (type == webrtc::kVideoCodecVP8) { |
| 2253 | return AllocatedDecoder( |
| 2254 | webrtc::VideoDecoder::Create(webrtc::VideoDecoder::kVp8), type, false); |
| 2255 | } |
| 2256 | |
pbos@webrtc.org | b9557a9 | 2015-03-20 19:52:56 +0000 | [diff] [blame] | 2257 | if (type == webrtc::kVideoCodecVP9) { |
| 2258 | return AllocatedDecoder( |
| 2259 | webrtc::VideoDecoder::Create(webrtc::VideoDecoder::kVp9), type, false); |
| 2260 | } |
| 2261 | |
pbos@webrtc.org | 96a9325 | 2014-11-03 14:46:44 +0000 | [diff] [blame] | 2262 | // This shouldn't happen, we should not be trying to create something we don't |
| 2263 | // support. |
| 2264 | assert(false); |
| 2265 | return AllocatedDecoder(NULL, webrtc::kVideoCodecUnknown, false); |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 2266 | } |
| 2267 | |
| 2268 | void WebRtcVideoChannel2::WebRtcVideoReceiveStream::SetRecvCodecs( |
| 2269 | const std::vector<VideoCodecSettings>& recv_codecs) { |
pbos@webrtc.org | 96a9325 | 2014-11-03 14:46:44 +0000 | [diff] [blame] | 2270 | std::vector<AllocatedDecoder> old_decoders = allocated_decoders_; |
| 2271 | allocated_decoders_.clear(); |
| 2272 | config_.decoders.clear(); |
| 2273 | for (size_t i = 0; i < recv_codecs.size(); ++i) { |
| 2274 | AllocatedDecoder allocated_decoder = |
| 2275 | CreateOrReuseVideoDecoder(&old_decoders, recv_codecs[i].codec); |
| 2276 | allocated_decoders_.push_back(allocated_decoder); |
| 2277 | |
| 2278 | webrtc::VideoReceiveStream::Decoder decoder; |
| 2279 | decoder.decoder = allocated_decoder.decoder; |
| 2280 | decoder.payload_type = recv_codecs[i].codec.id; |
| 2281 | decoder.payload_name = recv_codecs[i].codec.name; |
| 2282 | config_.decoders.push_back(decoder); |
| 2283 | } |
| 2284 | |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 2285 | // TODO(pbos): Reconfigure RTX based on incoming recv_codecs. |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 2286 | config_.rtp.fec = recv_codecs.front().fec; |
pbos@webrtc.org | 257e130 | 2014-07-25 19:01:32 +0000 | [diff] [blame] | 2287 | config_.rtp.nack.rtp_history_ms = |
Shao Changbin | e62202f | 2015-04-21 20:24:50 +0800 | [diff] [blame] | 2288 | HasNack(recv_codecs.begin()->codec) ? kNackHistoryMs : 0; |
| 2289 | config_.rtp.remb = HasRemb(recv_codecs.begin()->codec); |
pbos@webrtc.org | 257e130 | 2014-07-25 19:01:32 +0000 | [diff] [blame] | 2290 | |
pbos@webrtc.org | 96a9325 | 2014-11-03 14:46:44 +0000 | [diff] [blame] | 2291 | ClearDecoders(&old_decoders); |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 2292 | RecreateWebRtcStream(); |
| 2293 | } |
| 2294 | |
| 2295 | void WebRtcVideoChannel2::WebRtcVideoReceiveStream::SetRtpExtensions( |
| 2296 | const std::vector<webrtc::RtpExtension>& extensions) { |
| 2297 | config_.rtp.extensions = extensions; |
Peter Boström | 3c3f646 | 2015-04-15 16:27:49 +0200 | [diff] [blame] | 2298 | if (stream_ != nullptr) |
| 2299 | RecreateWebRtcStream(); |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 2300 | } |
| 2301 | |
| 2302 | void WebRtcVideoChannel2::WebRtcVideoReceiveStream::RecreateWebRtcStream() { |
| 2303 | if (stream_ != NULL) { |
| 2304 | call_->DestroyVideoReceiveStream(stream_); |
| 2305 | } |
| 2306 | stream_ = call_->CreateVideoReceiveStream(config_); |
| 2307 | stream_->Start(); |
| 2308 | } |
| 2309 | |
pbos@webrtc.org | 96a9325 | 2014-11-03 14:46:44 +0000 | [diff] [blame] | 2310 | void WebRtcVideoChannel2::WebRtcVideoReceiveStream::ClearDecoders( |
| 2311 | std::vector<AllocatedDecoder>* allocated_decoders) { |
| 2312 | for (size_t i = 0; i < allocated_decoders->size(); ++i) { |
| 2313 | if ((*allocated_decoders)[i].external) { |
pbos@webrtc.org | 776e6f2 | 2014-10-29 15:28:39 +0000 | [diff] [blame] | 2314 | external_decoder_factory_->DestroyVideoDecoder( |
pbos@webrtc.org | 96a9325 | 2014-11-03 14:46:44 +0000 | [diff] [blame] | 2315 | (*allocated_decoders)[i].decoder); |
pbos@webrtc.org | 776e6f2 | 2014-10-29 15:28:39 +0000 | [diff] [blame] | 2316 | } else { |
pbos@webrtc.org | 96a9325 | 2014-11-03 14:46:44 +0000 | [diff] [blame] | 2317 | delete (*allocated_decoders)[i].decoder; |
pbos@webrtc.org | 776e6f2 | 2014-10-29 15:28:39 +0000 | [diff] [blame] | 2318 | } |
| 2319 | } |
pbos@webrtc.org | 96a9325 | 2014-11-03 14:46:44 +0000 | [diff] [blame] | 2320 | allocated_decoders->clear(); |
pbos@webrtc.org | 776e6f2 | 2014-10-29 15:28:39 +0000 | [diff] [blame] | 2321 | } |
| 2322 | |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 2323 | void WebRtcVideoChannel2::WebRtcVideoReceiveStream::RenderFrame( |
| 2324 | const webrtc::I420VideoFrame& frame, |
| 2325 | int time_to_render_ms) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 2326 | rtc::CritScope crit(&renderer_lock_); |
magjed@webrtc.org | fc5ad95 | 2015-01-27 09:57:01 +0000 | [diff] [blame] | 2327 | |
| 2328 | if (first_frame_timestamp_ < 0) |
| 2329 | first_frame_timestamp_ = frame.timestamp(); |
| 2330 | int64_t rtp_time_elapsed_since_first_frame = |
| 2331 | (timestamp_wraparound_handler_.Unwrap(frame.timestamp()) - |
| 2332 | first_frame_timestamp_); |
| 2333 | int64_t elapsed_time_ms = rtp_time_elapsed_since_first_frame / |
| 2334 | (cricket::kVideoCodecClockrate / 1000); |
| 2335 | if (frame.ntp_time_ms() > 0) |
| 2336 | estimated_remote_start_ntp_time_ms_ = frame.ntp_time_ms() - elapsed_time_ms; |
| 2337 | |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 2338 | if (renderer_ == NULL) { |
| 2339 | LOG(LS_WARNING) << "VideoReceiveStream not connected to a VideoRenderer."; |
| 2340 | return; |
| 2341 | } |
| 2342 | |
| 2343 | if (frame.width() != last_width_ || frame.height() != last_height_) { |
| 2344 | SetSize(frame.width(), frame.height()); |
| 2345 | } |
| 2346 | |
magjed@webrtc.org | 2386d6d | 2015-03-05 14:03:08 +0000 | [diff] [blame] | 2347 | const WebRtcVideoFrame render_frame( |
| 2348 | frame.video_frame_buffer(), |
| 2349 | elapsed_time_ms * rtc::kNumNanosecsPerMillisec, |
Guo-wei Shieh | 64c1e8c | 2015-04-01 15:33:06 -0700 | [diff] [blame] | 2350 | frame.render_time_ms() * rtc::kNumNanosecsPerMillisec, frame.rotation()); |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 2351 | renderer_->RenderFrame(&render_frame); |
| 2352 | } |
| 2353 | |
pbos@webrtc.org | 0d852d5 | 2015-02-09 15:14:36 +0000 | [diff] [blame] | 2354 | bool WebRtcVideoChannel2::WebRtcVideoReceiveStream::IsTextureSupported() const { |
| 2355 | return true; |
| 2356 | } |
| 2357 | |
pbos@webrtc.org | a2a6fe6 | 2015-03-06 15:35:19 +0000 | [diff] [blame] | 2358 | bool WebRtcVideoChannel2::WebRtcVideoReceiveStream::IsDefaultStream() const { |
| 2359 | return default_stream_; |
| 2360 | } |
| 2361 | |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 2362 | void WebRtcVideoChannel2::WebRtcVideoReceiveStream::SetRenderer( |
| 2363 | cricket::VideoRenderer* renderer) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 2364 | rtc::CritScope crit(&renderer_lock_); |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 2365 | renderer_ = renderer; |
| 2366 | if (renderer_ != NULL && last_width_ != -1) { |
| 2367 | SetSize(last_width_, last_height_); |
| 2368 | } |
| 2369 | } |
| 2370 | |
| 2371 | VideoRenderer* WebRtcVideoChannel2::WebRtcVideoReceiveStream::GetRenderer() { |
| 2372 | // TODO(pbos): Remove GetRenderer and all uses of it, it's thread-unsafe by |
| 2373 | // design. |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 2374 | rtc::CritScope crit(&renderer_lock_); |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 2375 | return renderer_; |
| 2376 | } |
| 2377 | |
| 2378 | void WebRtcVideoChannel2::WebRtcVideoReceiveStream::SetSize(int width, |
| 2379 | int height) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 2380 | rtc::CritScope crit(&renderer_lock_); |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 2381 | if (!renderer_->SetSize(width, height, 0)) { |
| 2382 | LOG(LS_ERROR) << "Could not set renderer size."; |
| 2383 | } |
| 2384 | last_width_ = width; |
| 2385 | last_height_ = height; |
| 2386 | } |
| 2387 | |
pbos@webrtc.org | e6f84ae | 2014-07-18 11:11:55 +0000 | [diff] [blame] | 2388 | VideoReceiverInfo |
| 2389 | WebRtcVideoChannel2::WebRtcVideoReceiveStream::GetVideoReceiverInfo() { |
| 2390 | VideoReceiverInfo info; |
| 2391 | info.add_ssrc(config_.rtp.remote_ssrc); |
| 2392 | webrtc::VideoReceiveStream::Stats stats = stream_->GetStats(); |
asapersson@webrtc.org | cfd82df | 2015-01-22 09:39:59 +0000 | [diff] [blame] | 2393 | info.bytes_rcvd = stats.rtp_stats.transmitted.payload_bytes + |
| 2394 | stats.rtp_stats.transmitted.header_bytes + |
| 2395 | stats.rtp_stats.transmitted.padding_bytes; |
| 2396 | info.packets_rcvd = stats.rtp_stats.transmitted.packets; |
Peter Boström | 393347f | 2015-04-22 14:52:45 +0200 | [diff] [blame] | 2397 | info.packets_lost = stats.rtcp_stats.cumulative_lost; |
| 2398 | info.fraction_lost = |
| 2399 | static_cast<float>(stats.rtcp_stats.fraction_lost) / (1 << 8); |
pbos@webrtc.org | e6f84ae | 2014-07-18 11:11:55 +0000 | [diff] [blame] | 2400 | |
| 2401 | info.framerate_rcvd = stats.network_frame_rate; |
| 2402 | info.framerate_decoded = stats.decode_frame_rate; |
| 2403 | info.framerate_output = stats.render_frame_rate; |
| 2404 | |
pbos@webrtc.org | 1d0fa5d | 2015-02-19 12:47:00 +0000 | [diff] [blame] | 2405 | { |
| 2406 | rtc::CritScope frame_cs(&renderer_lock_); |
| 2407 | info.frame_width = last_width_; |
| 2408 | info.frame_height = last_height_; |
| 2409 | info.capture_start_ntp_time_ms = estimated_remote_start_ntp_time_ms_; |
| 2410 | } |
| 2411 | |
pbos@webrtc.org | 09c77b9 | 2015-02-25 10:42:16 +0000 | [diff] [blame] | 2412 | info.decode_ms = stats.decode_ms; |
| 2413 | info.max_decode_ms = stats.max_decode_ms; |
| 2414 | info.current_delay_ms = stats.current_delay_ms; |
| 2415 | info.target_delay_ms = stats.target_delay_ms; |
| 2416 | info.jitter_buffer_ms = stats.jitter_buffer_ms; |
| 2417 | info.min_playout_delay_ms = stats.min_playout_delay_ms; |
| 2418 | info.render_delay_ms = stats.render_delay_ms; |
| 2419 | |
pbos@webrtc.org | 1d0fa5d | 2015-02-19 12:47:00 +0000 | [diff] [blame] | 2420 | info.firs_sent = stats.rtcp_packet_type_counts.fir_packets; |
| 2421 | info.plis_sent = stats.rtcp_packet_type_counts.pli_packets; |
| 2422 | info.nacks_sent = stats.rtcp_packet_type_counts.nack_packets; |
pbos@webrtc.org | e6f84ae | 2014-07-18 11:11:55 +0000 | [diff] [blame] | 2423 | |
pbos@webrtc.org | e6f84ae | 2014-07-18 11:11:55 +0000 | [diff] [blame] | 2424 | return info; |
| 2425 | } |
| 2426 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 2427 | WebRtcVideoChannel2::VideoCodecSettings::VideoCodecSettings() |
| 2428 | : rtx_payload_type(-1) {} |
| 2429 | |
pbos@webrtc.org | a2ef4fe | 2014-11-07 10:54:43 +0000 | [diff] [blame] | 2430 | bool WebRtcVideoChannel2::VideoCodecSettings::operator==( |
| 2431 | const WebRtcVideoChannel2::VideoCodecSettings& other) const { |
| 2432 | return codec == other.codec && |
| 2433 | fec.ulpfec_payload_type == other.fec.ulpfec_payload_type && |
| 2434 | fec.red_payload_type == other.fec.red_payload_type && |
Shao Changbin | e62202f | 2015-04-21 20:24:50 +0800 | [diff] [blame] | 2435 | fec.red_rtx_payload_type == other.fec.red_rtx_payload_type && |
pbos@webrtc.org | a2ef4fe | 2014-11-07 10:54:43 +0000 | [diff] [blame] | 2436 | rtx_payload_type == other.rtx_payload_type; |
| 2437 | } |
| 2438 | |
Peter Boström | ee0b00e | 2015-04-22 18:41:14 +0200 | [diff] [blame] | 2439 | bool WebRtcVideoChannel2::VideoCodecSettings::operator!=( |
| 2440 | const WebRtcVideoChannel2::VideoCodecSettings& other) const { |
| 2441 | return !(*this == other); |
| 2442 | } |
| 2443 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 2444 | std::vector<WebRtcVideoChannel2::VideoCodecSettings> |
| 2445 | WebRtcVideoChannel2::MapCodecs(const std::vector<VideoCodec>& codecs) { |
| 2446 | assert(!codecs.empty()); |
| 2447 | |
| 2448 | std::vector<VideoCodecSettings> video_codecs; |
| 2449 | std::map<int, bool> payload_used; |
pbos@webrtc.org | e322a17 | 2014-06-13 11:47:28 +0000 | [diff] [blame] | 2450 | std::map<int, VideoCodec::CodecType> payload_codec_type; |
pkasting@chromium.org | d324546 | 2015-02-23 21:28:22 +0000 | [diff] [blame] | 2451 | // |rtx_mapping| maps video payload type to rtx payload type. |
| 2452 | std::map<int, int> rtx_mapping; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 2453 | |
| 2454 | webrtc::FecConfig fec_settings; |
| 2455 | |
| 2456 | for (size_t i = 0; i < codecs.size(); ++i) { |
| 2457 | const VideoCodec& in_codec = codecs[i]; |
| 2458 | int payload_type = in_codec.id; |
| 2459 | |
| 2460 | if (payload_used[payload_type]) { |
| 2461 | LOG(LS_ERROR) << "Payload type already registered: " |
| 2462 | << in_codec.ToString(); |
| 2463 | return std::vector<VideoCodecSettings>(); |
| 2464 | } |
| 2465 | payload_used[payload_type] = true; |
pbos@webrtc.org | e322a17 | 2014-06-13 11:47:28 +0000 | [diff] [blame] | 2466 | payload_codec_type[payload_type] = in_codec.GetCodecType(); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 2467 | |
| 2468 | switch (in_codec.GetCodecType()) { |
| 2469 | case VideoCodec::CODEC_RED: { |
| 2470 | // RED payload type, should not have duplicates. |
| 2471 | assert(fec_settings.red_payload_type == -1); |
| 2472 | fec_settings.red_payload_type = in_codec.id; |
| 2473 | continue; |
| 2474 | } |
| 2475 | |
| 2476 | case VideoCodec::CODEC_ULPFEC: { |
| 2477 | // ULPFEC payload type, should not have duplicates. |
| 2478 | assert(fec_settings.ulpfec_payload_type == -1); |
| 2479 | fec_settings.ulpfec_payload_type = in_codec.id; |
| 2480 | continue; |
| 2481 | } |
| 2482 | |
| 2483 | case VideoCodec::CODEC_RTX: { |
| 2484 | int associated_payload_type; |
| 2485 | if (!in_codec.GetParam(kCodecParamAssociatedPayloadType, |
pkasting@chromium.org | e9facf8 | 2015-02-17 20:36:28 +0000 | [diff] [blame] | 2486 | &associated_payload_type) || |
| 2487 | !IsValidRtpPayloadType(associated_payload_type)) { |
| 2488 | LOG(LS_ERROR) |
| 2489 | << "RTX codec with invalid or no associated payload type: " |
| 2490 | << in_codec.ToString(); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 2491 | return std::vector<VideoCodecSettings>(); |
| 2492 | } |
| 2493 | rtx_mapping[associated_payload_type] = in_codec.id; |
| 2494 | continue; |
| 2495 | } |
| 2496 | |
| 2497 | case VideoCodec::CODEC_VIDEO: |
| 2498 | break; |
| 2499 | } |
| 2500 | |
| 2501 | video_codecs.push_back(VideoCodecSettings()); |
| 2502 | video_codecs.back().codec = in_codec; |
| 2503 | } |
| 2504 | |
| 2505 | // One of these codecs should have been a video codec. Only having FEC |
| 2506 | // parameters into this code is a logic error. |
| 2507 | assert(!video_codecs.empty()); |
| 2508 | |
pbos@webrtc.org | e322a17 | 2014-06-13 11:47:28 +0000 | [diff] [blame] | 2509 | for (std::map<int, int>::const_iterator it = rtx_mapping.begin(); |
| 2510 | it != rtx_mapping.end(); |
| 2511 | ++it) { |
| 2512 | if (!payload_used[it->first]) { |
| 2513 | LOG(LS_ERROR) << "RTX mapped to payload not in codec list."; |
| 2514 | return std::vector<VideoCodecSettings>(); |
| 2515 | } |
Shao Changbin | e62202f | 2015-04-21 20:24:50 +0800 | [diff] [blame] | 2516 | if (payload_codec_type[it->first] != VideoCodec::CODEC_VIDEO && |
| 2517 | payload_codec_type[it->first] != VideoCodec::CODEC_RED) { |
| 2518 | 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] | 2519 | return std::vector<VideoCodecSettings>(); |
| 2520 | } |
Shao Changbin | e62202f | 2015-04-21 20:24:50 +0800 | [diff] [blame] | 2521 | |
| 2522 | if (it->first == fec_settings.red_payload_type) { |
| 2523 | fec_settings.red_rtx_payload_type = it->second; |
| 2524 | } |
pbos@webrtc.org | e322a17 | 2014-06-13 11:47:28 +0000 | [diff] [blame] | 2525 | } |
| 2526 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 2527 | for (size_t i = 0; i < video_codecs.size(); ++i) { |
| 2528 | video_codecs[i].fec = fec_settings; |
Shao Changbin | e62202f | 2015-04-21 20:24:50 +0800 | [diff] [blame] | 2529 | if (rtx_mapping[video_codecs[i].codec.id] != 0 && |
| 2530 | rtx_mapping[video_codecs[i].codec.id] != |
| 2531 | fec_settings.red_payload_type) { |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 2532 | video_codecs[i].rtx_payload_type = rtx_mapping[video_codecs[i].codec.id]; |
| 2533 | } |
| 2534 | } |
| 2535 | |
| 2536 | return video_codecs; |
| 2537 | } |
| 2538 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 2539 | } // namespace cricket |
| 2540 | |
| 2541 | #endif // HAVE_WEBRTC_VIDEO |