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