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