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