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