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