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