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