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