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