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