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 | 5ff71ab | 2014-07-23 07:28:56 +0000 | [diff] [blame] | 750 | options_.suspend_below_min_bitrate.Set(false); |
pbos@webrtc.org | 42684be | 2014-10-03 11:25:45 +0000 | [diff] [blame] | 751 | options_.use_payload_padding.Set(false); |
| 752 | options_.video_noise_reduction.Set(true); |
pbos@webrtc.org | fa553ef | 2014-10-20 11:07:07 +0000 | [diff] [blame] | 753 | options_.video_start_bitrate.Set( |
| 754 | webrtc::Call::Config::kDefaultStartBitrateBps / 1000); |
pbos@webrtc.org | efc82c2 | 2014-10-27 13:58:00 +0000 | [diff] [blame] | 755 | options_.screencast_min_bitrate.Set(0); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 756 | } |
| 757 | |
| 758 | WebRtcVideoChannel2::~WebRtcVideoChannel2() { |
| 759 | for (std::map<uint32, WebRtcVideoSendStream*>::iterator it = |
| 760 | send_streams_.begin(); |
| 761 | it != send_streams_.end(); |
| 762 | ++it) { |
| 763 | delete it->second; |
| 764 | } |
| 765 | |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 766 | for (std::map<uint32, WebRtcVideoReceiveStream*>::iterator it = |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 767 | receive_streams_.begin(); |
| 768 | it != receive_streams_.end(); |
| 769 | ++it) { |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 770 | delete it->second; |
| 771 | } |
| 772 | } |
| 773 | |
| 774 | bool WebRtcVideoChannel2::Init() { return true; } |
| 775 | |
pbos@webrtc.org | 96a9325 | 2014-11-03 14:46:44 +0000 | [diff] [blame] | 776 | bool WebRtcVideoChannel2::CodecIsExternallySupported( |
| 777 | const std::string& name) const { |
| 778 | if (external_encoder_factory_ == NULL) { |
| 779 | return false; |
| 780 | } |
| 781 | |
| 782 | const std::vector<WebRtcVideoEncoderFactory::VideoCodec> external_codecs = |
| 783 | external_encoder_factory_->codecs(); |
| 784 | for (size_t c = 0; c < external_codecs.size(); ++c) { |
| 785 | if (CodecNameMatches(name, external_codecs[c].name)) { |
| 786 | return true; |
| 787 | } |
| 788 | } |
| 789 | return false; |
| 790 | } |
| 791 | |
| 792 | std::vector<WebRtcVideoChannel2::VideoCodecSettings> |
| 793 | WebRtcVideoChannel2::FilterSupportedCodecs( |
| 794 | const std::vector<WebRtcVideoChannel2::VideoCodecSettings>& mapped_codecs) |
| 795 | const { |
| 796 | std::vector<VideoCodecSettings> supported_codecs; |
| 797 | for (size_t i = 0; i < mapped_codecs.size(); ++i) { |
| 798 | const VideoCodecSettings& codec = mapped_codecs[i]; |
| 799 | if (CodecIsInternallySupported(codec.codec.name) || |
| 800 | CodecIsExternallySupported(codec.codec.name)) { |
| 801 | supported_codecs.push_back(codec); |
| 802 | } |
| 803 | } |
| 804 | return supported_codecs; |
| 805 | } |
| 806 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 807 | bool WebRtcVideoChannel2::SetRecvCodecs(const std::vector<VideoCodec>& codecs) { |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 808 | LOG(LS_INFO) << "SetRecvCodecs: " << CodecVectorToString(codecs); |
| 809 | if (!ValidateCodecFormats(codecs)) { |
| 810 | return false; |
| 811 | } |
| 812 | |
| 813 | const std::vector<VideoCodecSettings> mapped_codecs = MapCodecs(codecs); |
| 814 | if (mapped_codecs.empty()) { |
pbos@webrtc.org | 96a9325 | 2014-11-03 14:46:44 +0000 | [diff] [blame] | 815 | LOG(LS_ERROR) << "SetRecvCodecs called without any video codecs."; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 816 | return false; |
| 817 | } |
| 818 | |
pbos@webrtc.org | 96a9325 | 2014-11-03 14:46:44 +0000 | [diff] [blame] | 819 | const std::vector<VideoCodecSettings> supported_codecs = |
| 820 | FilterSupportedCodecs(mapped_codecs); |
| 821 | |
| 822 | if (mapped_codecs.size() != supported_codecs.size()) { |
| 823 | LOG(LS_ERROR) << "SetRecvCodecs called with unsupported video codecs."; |
| 824 | return false; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 825 | } |
| 826 | |
pbos@webrtc.org | 96a9325 | 2014-11-03 14:46:44 +0000 | [diff] [blame] | 827 | recv_codecs_ = supported_codecs; |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 828 | |
pbos@webrtc.org | 575d126 | 2014-10-08 14:48:08 +0000 | [diff] [blame] | 829 | rtc::CritScope stream_lock(&stream_crit_); |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 830 | for (std::map<uint32, WebRtcVideoReceiveStream*>::iterator it = |
| 831 | receive_streams_.begin(); |
| 832 | it != receive_streams_.end(); |
| 833 | ++it) { |
| 834 | it->second->SetRecvCodecs(recv_codecs_); |
| 835 | } |
| 836 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 837 | return true; |
| 838 | } |
| 839 | |
| 840 | bool WebRtcVideoChannel2::SetSendCodecs(const std::vector<VideoCodec>& codecs) { |
| 841 | LOG(LS_INFO) << "SetSendCodecs: " << CodecVectorToString(codecs); |
| 842 | if (!ValidateCodecFormats(codecs)) { |
| 843 | return false; |
| 844 | } |
| 845 | |
| 846 | const std::vector<VideoCodecSettings> supported_codecs = |
| 847 | FilterSupportedCodecs(MapCodecs(codecs)); |
| 848 | |
| 849 | if (supported_codecs.empty()) { |
| 850 | LOG(LS_ERROR) << "No video codecs supported by encoder factory."; |
| 851 | return false; |
| 852 | } |
| 853 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 854 | LOG(LS_INFO) << "Using codec: " << supported_codecs.front().codec.ToString(); |
| 855 | |
pbos@webrtc.org | a2ef4fe | 2014-11-07 10:54:43 +0000 | [diff] [blame] | 856 | VideoCodecSettings old_codec; |
| 857 | if (send_codec_.Get(&old_codec) && supported_codecs.front() == old_codec) { |
| 858 | // Using same codec, avoid reconfiguring. |
| 859 | return true; |
| 860 | } |
| 861 | |
| 862 | send_codec_.Set(supported_codecs.front()); |
| 863 | |
pbos@webrtc.org | 575d126 | 2014-10-08 14:48:08 +0000 | [diff] [blame] | 864 | rtc::CritScope stream_lock(&stream_crit_); |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 865 | for (std::map<uint32, WebRtcVideoSendStream*>::iterator it = |
| 866 | send_streams_.begin(); |
| 867 | it != send_streams_.end(); |
| 868 | ++it) { |
| 869 | assert(it->second != NULL); |
| 870 | it->second->SetCodec(supported_codecs.front()); |
| 871 | } |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 872 | |
| 873 | return true; |
| 874 | } |
| 875 | |
| 876 | bool WebRtcVideoChannel2::GetSendCodec(VideoCodec* codec) { |
| 877 | VideoCodecSettings codec_settings; |
| 878 | if (!send_codec_.Get(&codec_settings)) { |
| 879 | LOG(LS_VERBOSE) << "GetSendCodec: No send codec set."; |
| 880 | return false; |
| 881 | } |
| 882 | *codec = codec_settings.codec; |
| 883 | return true; |
| 884 | } |
| 885 | |
| 886 | bool WebRtcVideoChannel2::SetSendStreamFormat(uint32 ssrc, |
| 887 | const VideoFormat& format) { |
| 888 | LOG(LS_VERBOSE) << "SetSendStreamFormat:" << ssrc << " -> " |
| 889 | << format.ToString(); |
pbos@webrtc.org | 575d126 | 2014-10-08 14:48:08 +0000 | [diff] [blame] | 890 | rtc::CritScope stream_lock(&stream_crit_); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 891 | if (send_streams_.find(ssrc) == send_streams_.end()) { |
| 892 | return false; |
| 893 | } |
| 894 | return send_streams_[ssrc]->SetVideoFormat(format); |
| 895 | } |
| 896 | |
| 897 | bool WebRtcVideoChannel2::SetRender(bool render) { |
| 898 | // TODO(pbos): Implement. Or refactor away as it shouldn't be needed. |
| 899 | LOG(LS_VERBOSE) << "SetRender: " << (render ? "true" : "false"); |
| 900 | return true; |
| 901 | } |
| 902 | |
| 903 | bool WebRtcVideoChannel2::SetSend(bool send) { |
| 904 | LOG(LS_VERBOSE) << "SetSend: " << (send ? "true" : "false"); |
| 905 | if (send && !send_codec_.IsSet()) { |
| 906 | LOG(LS_ERROR) << "SetSend(true) called before setting codec."; |
| 907 | return false; |
| 908 | } |
| 909 | if (send) { |
| 910 | StartAllSendStreams(); |
| 911 | } else { |
| 912 | StopAllSendStreams(); |
| 913 | } |
| 914 | sending_ = send; |
| 915 | return true; |
| 916 | } |
| 917 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 918 | bool WebRtcVideoChannel2::AddSendStream(const StreamParams& sp) { |
| 919 | LOG(LS_INFO) << "AddSendStream: " << sp.ToString(); |
| 920 | if (sp.ssrcs.empty()) { |
| 921 | LOG(LS_ERROR) << "No SSRCs in stream parameters."; |
| 922 | return false; |
| 923 | } |
| 924 | |
| 925 | uint32 ssrc = sp.first_ssrc(); |
| 926 | assert(ssrc != 0); |
| 927 | // TODO(pbos): Make sure none of sp.ssrcs are used, not just the identifying |
| 928 | // ssrc. |
pbos@webrtc.org | 575d126 | 2014-10-08 14:48:08 +0000 | [diff] [blame] | 929 | rtc::CritScope stream_lock(&stream_crit_); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 930 | if (send_streams_.find(ssrc) != send_streams_.end()) { |
| 931 | LOG(LS_ERROR) << "Send stream with ssrc '" << ssrc << "' already exists."; |
| 932 | return false; |
| 933 | } |
| 934 | |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 935 | std::vector<uint32> primary_ssrcs; |
| 936 | sp.GetPrimarySsrcs(&primary_ssrcs); |
| 937 | std::vector<uint32> rtx_ssrcs; |
| 938 | sp.GetFidSsrcs(primary_ssrcs, &rtx_ssrcs); |
| 939 | if (!rtx_ssrcs.empty() && primary_ssrcs.size() != rtx_ssrcs.size()) { |
| 940 | LOG(LS_ERROR) |
| 941 | << "RTX SSRCs exist, but don't cover all SSRCs (unsupported): " |
| 942 | << sp.ToString(); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 943 | return false; |
| 944 | } |
| 945 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 946 | WebRtcVideoSendStream* stream = |
pbos@webrtc.org | 6ae48c6 | 2014-06-06 10:49:19 +0000 | [diff] [blame] | 947 | new WebRtcVideoSendStream(call_.get(), |
pbos@webrtc.org | 7fe1e03 | 2014-10-14 04:25:33 +0000 | [diff] [blame] | 948 | external_encoder_factory_, |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 949 | encoder_factory_, |
pbos@webrtc.org | 6ae48c6 | 2014-06-06 10:49:19 +0000 | [diff] [blame] | 950 | options_, |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 951 | send_codec_, |
| 952 | sp, |
| 953 | send_rtp_extensions_); |
| 954 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 955 | send_streams_[ssrc] = stream; |
| 956 | |
| 957 | if (rtcp_receiver_report_ssrc_ == kDefaultRtcpReceiverReportSsrc) { |
| 958 | rtcp_receiver_report_ssrc_ = ssrc; |
| 959 | } |
| 960 | if (default_send_ssrc_ == 0) { |
| 961 | default_send_ssrc_ = ssrc; |
| 962 | } |
| 963 | if (sending_) { |
| 964 | stream->Start(); |
| 965 | } |
| 966 | |
| 967 | return true; |
| 968 | } |
| 969 | |
| 970 | bool WebRtcVideoChannel2::RemoveSendStream(uint32 ssrc) { |
| 971 | LOG(LS_INFO) << "RemoveSendStream: " << ssrc; |
| 972 | |
| 973 | if (ssrc == 0) { |
| 974 | if (default_send_ssrc_ == 0) { |
| 975 | LOG(LS_ERROR) << "No default send stream active."; |
| 976 | return false; |
| 977 | } |
| 978 | |
| 979 | LOG(LS_VERBOSE) << "Removing default stream: " << default_send_ssrc_; |
| 980 | ssrc = default_send_ssrc_; |
| 981 | } |
| 982 | |
pbos@webrtc.org | 575d126 | 2014-10-08 14:48:08 +0000 | [diff] [blame] | 983 | WebRtcVideoSendStream* removed_stream; |
| 984 | { |
| 985 | rtc::CritScope stream_lock(&stream_crit_); |
| 986 | std::map<uint32, WebRtcVideoSendStream*>::iterator it = |
| 987 | send_streams_.find(ssrc); |
| 988 | if (it == send_streams_.end()) { |
| 989 | return false; |
| 990 | } |
| 991 | |
| 992 | removed_stream = it->second; |
| 993 | send_streams_.erase(it); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 994 | } |
| 995 | |
pbos@webrtc.org | 575d126 | 2014-10-08 14:48:08 +0000 | [diff] [blame] | 996 | delete removed_stream; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 997 | |
| 998 | if (ssrc == default_send_ssrc_) { |
| 999 | default_send_ssrc_ = 0; |
| 1000 | } |
| 1001 | |
| 1002 | return true; |
| 1003 | } |
| 1004 | |
| 1005 | bool WebRtcVideoChannel2::AddRecvStream(const StreamParams& sp) { |
| 1006 | LOG(LS_INFO) << "AddRecvStream: " << sp.ToString(); |
| 1007 | assert(sp.ssrcs.size() > 0); |
| 1008 | |
| 1009 | uint32 ssrc = sp.first_ssrc(); |
| 1010 | assert(ssrc != 0); // TODO(pbos): Is this ever valid? |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1011 | |
| 1012 | // TODO(pbos): Check if any of the SSRCs overlap. |
pbos@webrtc.org | 575d126 | 2014-10-08 14:48:08 +0000 | [diff] [blame] | 1013 | rtc::CritScope stream_lock(&stream_crit_); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1014 | if (receive_streams_.find(ssrc) != receive_streams_.end()) { |
| 1015 | LOG(LS_ERROR) << "Receive stream for SSRC " << ssrc << "already exists."; |
| 1016 | return false; |
| 1017 | } |
| 1018 | |
pbos@webrtc.org | bd249bc | 2014-07-07 04:45:15 +0000 | [diff] [blame] | 1019 | webrtc::VideoReceiveStream::Config config; |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 1020 | ConfigureReceiverRtp(&config, sp); |
pbos@webrtc.org | 3bf3d23 | 2014-10-31 12:59:34 +0000 | [diff] [blame] | 1021 | |
| 1022 | // Set up A/V sync if there is a VoiceChannel. |
| 1023 | // TODO(pbos): The A/V is synched by the receiving channel. So we need to know |
| 1024 | // the SSRC of the remote audio channel in order to sync the correct webrtc |
| 1025 | // VoiceEngine channel. For now sync the first channel in non-conference to |
| 1026 | // match existing behavior in WebRtcVideoEngine. |
| 1027 | if (voice_channel_ != NULL && receive_streams_.empty() && |
| 1028 | !options_.conference_mode.GetWithDefaultIfUnset(false)) { |
| 1029 | config.audio_channel_id = |
| 1030 | static_cast<WebRtcVoiceMediaChannel*>(voice_channel_)->voe_channel(); |
| 1031 | } |
| 1032 | |
pbos@webrtc.org | 776e6f2 | 2014-10-29 15:28:39 +0000 | [diff] [blame] | 1033 | receive_streams_[ssrc] = new WebRtcVideoReceiveStream( |
| 1034 | call_.get(), external_decoder_factory_, config, recv_codecs_); |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 1035 | |
| 1036 | return true; |
| 1037 | } |
| 1038 | |
| 1039 | void WebRtcVideoChannel2::ConfigureReceiverRtp( |
| 1040 | webrtc::VideoReceiveStream::Config* config, |
| 1041 | const StreamParams& sp) const { |
| 1042 | uint32 ssrc = sp.first_ssrc(); |
| 1043 | |
| 1044 | config->rtp.remote_ssrc = ssrc; |
| 1045 | config->rtp.local_ssrc = rtcp_receiver_report_ssrc_; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1046 | |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 1047 | config->rtp.extensions = recv_rtp_extensions_; |
pbos@webrtc.org | 257e130 | 2014-07-25 19:01:32 +0000 | [diff] [blame] | 1048 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1049 | // TODO(pbos): This protection is against setting the same local ssrc as |
| 1050 | // remote which is not permitted by the lower-level API. RTCP requires a |
| 1051 | // corresponding sender SSRC. Figure out what to do when we don't have |
| 1052 | // (receive-only) or know a good local SSRC. |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 1053 | if (config->rtp.remote_ssrc == config->rtp.local_ssrc) { |
| 1054 | if (config->rtp.local_ssrc != kDefaultRtcpReceiverReportSsrc) { |
| 1055 | config->rtp.local_ssrc = kDefaultRtcpReceiverReportSsrc; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1056 | } else { |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 1057 | config->rtp.local_ssrc = kDefaultRtcpReceiverReportSsrc + 1; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1058 | } |
| 1059 | } |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 1060 | |
| 1061 | for (size_t i = 0; i < recv_codecs_.size(); ++i) { |
andresp@webrtc.org | 82775b1 | 2014-11-07 09:37:54 +0000 | [diff] [blame] | 1062 | MergeFecConfig(recv_codecs_[i].fec, &config->rtp.fec); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1063 | } |
| 1064 | |
andresp@webrtc.org | 82775b1 | 2014-11-07 09:37:54 +0000 | [diff] [blame] | 1065 | for (size_t i = 0; i < recv_codecs_.size(); ++i) { |
| 1066 | uint32 rtx_ssrc; |
| 1067 | if (recv_codecs_[i].rtx_payload_type != -1 && |
| 1068 | sp.GetFidSsrc(ssrc, &rtx_ssrc)) { |
| 1069 | webrtc::VideoReceiveStream::Config::Rtp::Rtx& rtx = |
| 1070 | config->rtp.rtx[recv_codecs_[i].codec.id]; |
| 1071 | rtx.ssrc = rtx_ssrc; |
| 1072 | rtx.payload_type = recv_codecs_[i].rtx_payload_type; |
| 1073 | } |
| 1074 | } |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1075 | } |
| 1076 | |
| 1077 | bool WebRtcVideoChannel2::RemoveRecvStream(uint32 ssrc) { |
| 1078 | LOG(LS_INFO) << "RemoveRecvStream: " << ssrc; |
| 1079 | if (ssrc == 0) { |
pbos@webrtc.org | afb554f4 | 2014-08-12 23:17:13 +0000 | [diff] [blame] | 1080 | LOG(LS_ERROR) << "RemoveRecvStream with 0 ssrc is not supported."; |
| 1081 | return false; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1082 | } |
| 1083 | |
pbos@webrtc.org | 575d126 | 2014-10-08 14:48:08 +0000 | [diff] [blame] | 1084 | rtc::CritScope stream_lock(&stream_crit_); |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 1085 | std::map<uint32, WebRtcVideoReceiveStream*>::iterator stream = |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1086 | receive_streams_.find(ssrc); |
| 1087 | if (stream == receive_streams_.end()) { |
| 1088 | LOG(LS_ERROR) << "Stream not found for ssrc: " << ssrc; |
| 1089 | return false; |
| 1090 | } |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 1091 | delete stream->second; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1092 | receive_streams_.erase(stream); |
| 1093 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1094 | return true; |
| 1095 | } |
| 1096 | |
| 1097 | bool WebRtcVideoChannel2::SetRenderer(uint32 ssrc, VideoRenderer* renderer) { |
| 1098 | LOG(LS_INFO) << "SetRenderer: ssrc:" << ssrc << " " |
| 1099 | << (renderer ? "(ptr)" : "NULL"); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1100 | if (ssrc == 0) { |
pbos@webrtc.org | afb554f4 | 2014-08-12 23:17:13 +0000 | [diff] [blame] | 1101 | default_unsignalled_ssrc_handler_.SetDefaultRenderer(this, renderer); |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 1102 | return true; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1103 | } |
| 1104 | |
pbos@webrtc.org | 575d126 | 2014-10-08 14:48:08 +0000 | [diff] [blame] | 1105 | rtc::CritScope stream_lock(&stream_crit_); |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 1106 | std::map<uint32, WebRtcVideoReceiveStream*>::iterator it = |
| 1107 | receive_streams_.find(ssrc); |
| 1108 | if (it == receive_streams_.end()) { |
| 1109 | return false; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1110 | } |
| 1111 | |
| 1112 | it->second->SetRenderer(renderer); |
| 1113 | return true; |
| 1114 | } |
| 1115 | |
| 1116 | bool WebRtcVideoChannel2::GetRenderer(uint32 ssrc, VideoRenderer** renderer) { |
| 1117 | if (ssrc == 0) { |
pbos@webrtc.org | afb554f4 | 2014-08-12 23:17:13 +0000 | [diff] [blame] | 1118 | *renderer = default_unsignalled_ssrc_handler_.GetDefaultRenderer(); |
| 1119 | return *renderer != NULL; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1120 | } |
| 1121 | |
pbos@webrtc.org | 575d126 | 2014-10-08 14:48:08 +0000 | [diff] [blame] | 1122 | rtc::CritScope stream_lock(&stream_crit_); |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 1123 | std::map<uint32, WebRtcVideoReceiveStream*>::iterator it = |
| 1124 | receive_streams_.find(ssrc); |
| 1125 | if (it == receive_streams_.end()) { |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1126 | return false; |
| 1127 | } |
| 1128 | *renderer = it->second->GetRenderer(); |
| 1129 | return true; |
| 1130 | } |
| 1131 | |
| 1132 | bool WebRtcVideoChannel2::GetStats(const StatsOptions& options, |
| 1133 | VideoMediaInfo* info) { |
pbos@webrtc.org | e6f84ae | 2014-07-18 11:11:55 +0000 | [diff] [blame] | 1134 | info->Clear(); |
| 1135 | FillSenderStats(info); |
| 1136 | FillReceiverStats(info); |
| 1137 | FillBandwidthEstimationStats(info); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1138 | return true; |
| 1139 | } |
| 1140 | |
pbos@webrtc.org | e6f84ae | 2014-07-18 11:11:55 +0000 | [diff] [blame] | 1141 | void WebRtcVideoChannel2::FillSenderStats(VideoMediaInfo* video_media_info) { |
pbos@webrtc.org | 575d126 | 2014-10-08 14:48:08 +0000 | [diff] [blame] | 1142 | rtc::CritScope stream_lock(&stream_crit_); |
pbos@webrtc.org | e6f84ae | 2014-07-18 11:11:55 +0000 | [diff] [blame] | 1143 | for (std::map<uint32, WebRtcVideoSendStream*>::iterator it = |
| 1144 | send_streams_.begin(); |
| 1145 | it != send_streams_.end(); |
| 1146 | ++it) { |
| 1147 | video_media_info->senders.push_back(it->second->GetVideoSenderInfo()); |
| 1148 | } |
| 1149 | } |
| 1150 | |
| 1151 | void WebRtcVideoChannel2::FillReceiverStats(VideoMediaInfo* video_media_info) { |
pbos@webrtc.org | 575d126 | 2014-10-08 14:48:08 +0000 | [diff] [blame] | 1152 | rtc::CritScope stream_lock(&stream_crit_); |
pbos@webrtc.org | e6f84ae | 2014-07-18 11:11:55 +0000 | [diff] [blame] | 1153 | for (std::map<uint32, WebRtcVideoReceiveStream*>::iterator it = |
| 1154 | receive_streams_.begin(); |
| 1155 | it != receive_streams_.end(); |
| 1156 | ++it) { |
| 1157 | video_media_info->receivers.push_back(it->second->GetVideoReceiverInfo()); |
| 1158 | } |
| 1159 | } |
| 1160 | |
| 1161 | void WebRtcVideoChannel2::FillBandwidthEstimationStats( |
| 1162 | VideoMediaInfo* video_media_info) { |
stefan@webrtc.org | 0bae1fa | 2014-11-05 14:05:29 +0000 | [diff] [blame] | 1163 | BandwidthEstimationInfo bwe_info; |
| 1164 | webrtc::Call::Stats stats = call_->GetStats(); |
| 1165 | bwe_info.available_send_bandwidth = stats.send_bandwidth_bps; |
| 1166 | bwe_info.available_recv_bandwidth = stats.recv_bandwidth_bps; |
| 1167 | bwe_info.bucket_delay = stats.pacer_delay_ms; |
| 1168 | |
| 1169 | // Get send stream bitrate stats. |
| 1170 | rtc::CritScope stream_lock(&stream_crit_); |
| 1171 | for (std::map<uint32, WebRtcVideoSendStream*>::iterator stream = |
| 1172 | send_streams_.begin(); |
| 1173 | stream != send_streams_.end(); |
| 1174 | ++stream) { |
| 1175 | stream->second->FillBandwidthEstimationInfo(&bwe_info); |
| 1176 | } |
| 1177 | video_media_info->bw_estimations.push_back(bwe_info); |
pbos@webrtc.org | e6f84ae | 2014-07-18 11:11:55 +0000 | [diff] [blame] | 1178 | } |
| 1179 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1180 | bool WebRtcVideoChannel2::SetCapturer(uint32 ssrc, VideoCapturer* capturer) { |
| 1181 | LOG(LS_INFO) << "SetCapturer: " << ssrc << " -> " |
| 1182 | << (capturer != NULL ? "(capturer)" : "NULL"); |
| 1183 | assert(ssrc != 0); |
pbos@webrtc.org | 575d126 | 2014-10-08 14:48:08 +0000 | [diff] [blame] | 1184 | rtc::CritScope stream_lock(&stream_crit_); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1185 | if (send_streams_.find(ssrc) == send_streams_.end()) { |
| 1186 | LOG(LS_ERROR) << "No sending stream on ssrc " << ssrc; |
| 1187 | return false; |
| 1188 | } |
| 1189 | return send_streams_[ssrc]->SetCapturer(capturer); |
| 1190 | } |
| 1191 | |
| 1192 | bool WebRtcVideoChannel2::SendIntraFrame() { |
| 1193 | // TODO(pbos): Implement. |
| 1194 | LOG(LS_VERBOSE) << "SendIntraFrame()."; |
| 1195 | return true; |
| 1196 | } |
| 1197 | |
| 1198 | bool WebRtcVideoChannel2::RequestIntraFrame() { |
| 1199 | // TODO(pbos): Implement. |
| 1200 | LOG(LS_VERBOSE) << "SendIntraFrame()."; |
| 1201 | return true; |
| 1202 | } |
| 1203 | |
| 1204 | void WebRtcVideoChannel2::OnPacketReceived( |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1205 | rtc::Buffer* packet, |
| 1206 | const rtc::PacketTime& packet_time) { |
pbos@webrtc.org | 4e545cc | 2014-05-14 13:58:13 +0000 | [diff] [blame] | 1207 | const webrtc::PacketReceiver::DeliveryStatus delivery_result = |
| 1208 | call_->Receiver()->DeliverPacket( |
| 1209 | reinterpret_cast<const uint8_t*>(packet->data()), packet->length()); |
| 1210 | switch (delivery_result) { |
| 1211 | case webrtc::PacketReceiver::DELIVERY_OK: |
| 1212 | return; |
| 1213 | case webrtc::PacketReceiver::DELIVERY_PACKET_ERROR: |
| 1214 | return; |
| 1215 | case webrtc::PacketReceiver::DELIVERY_UNKNOWN_SSRC: |
| 1216 | break; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1217 | } |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1218 | |
| 1219 | uint32 ssrc = 0; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1220 | if (!GetRtpSsrc(packet->data(), packet->length(), &ssrc)) { |
| 1221 | return; |
| 1222 | } |
| 1223 | |
pbos@webrtc.org | afb554f4 | 2014-08-12 23:17:13 +0000 | [diff] [blame] | 1224 | // TODO(pbos): Make sure that the unsignalled SSRC uses the video payload. |
| 1225 | // Also figure out whether RTX needs to be handled. |
| 1226 | switch (unsignalled_ssrc_handler_->OnUnsignalledSsrc(this, ssrc)) { |
| 1227 | case UnsignalledSsrcHandler::kDropPacket: |
| 1228 | return; |
| 1229 | case UnsignalledSsrcHandler::kDeliverPacket: |
| 1230 | break; |
| 1231 | } |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1232 | |
pbos@webrtc.org | 1e019d1 | 2014-05-16 11:38:45 +0000 | [diff] [blame] | 1233 | if (call_->Receiver()->DeliverPacket( |
| 1234 | reinterpret_cast<const uint8_t*>(packet->data()), packet->length()) != |
| 1235 | webrtc::PacketReceiver::DELIVERY_OK) { |
pbos@webrtc.org | afb554f4 | 2014-08-12 23:17:13 +0000 | [diff] [blame] | 1236 | LOG(LS_WARNING) << "Failed to deliver RTP packet on re-delivery."; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1237 | return; |
| 1238 | } |
| 1239 | } |
| 1240 | |
| 1241 | void WebRtcVideoChannel2::OnRtcpReceived( |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1242 | rtc::Buffer* packet, |
| 1243 | const rtc::PacketTime& packet_time) { |
pbos@webrtc.org | 1e019d1 | 2014-05-16 11:38:45 +0000 | [diff] [blame] | 1244 | if (call_->Receiver()->DeliverPacket( |
| 1245 | reinterpret_cast<const uint8_t*>(packet->data()), packet->length()) != |
| 1246 | webrtc::PacketReceiver::DELIVERY_OK) { |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1247 | LOG(LS_WARNING) << "Failed to deliver RTCP packet."; |
| 1248 | } |
| 1249 | } |
| 1250 | |
| 1251 | void WebRtcVideoChannel2::OnReadyToSend(bool ready) { |
pbos@webrtc.org | 26c0c41 | 2014-09-03 16:17:12 +0000 | [diff] [blame] | 1252 | LOG(LS_VERBOSE) << "OnReadyToSend: " << (ready ? "Ready." : "Not ready."); |
| 1253 | call_->SignalNetworkState(ready ? webrtc::Call::kNetworkUp |
| 1254 | : webrtc::Call::kNetworkDown); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1255 | } |
| 1256 | |
| 1257 | bool WebRtcVideoChannel2::MuteStream(uint32 ssrc, bool mute) { |
| 1258 | LOG(LS_VERBOSE) << "MuteStream: " << ssrc << " -> " |
| 1259 | << (mute ? "mute" : "unmute"); |
| 1260 | assert(ssrc != 0); |
pbos@webrtc.org | 575d126 | 2014-10-08 14:48:08 +0000 | [diff] [blame] | 1261 | rtc::CritScope stream_lock(&stream_crit_); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1262 | if (send_streams_.find(ssrc) == send_streams_.end()) { |
| 1263 | LOG(LS_ERROR) << "No sending stream on ssrc " << ssrc; |
| 1264 | return false; |
| 1265 | } |
pbos@webrtc.org | ef8bb8d | 2014-08-13 21:36:18 +0000 | [diff] [blame] | 1266 | |
| 1267 | send_streams_[ssrc]->MuteStream(mute); |
| 1268 | return true; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1269 | } |
| 1270 | |
| 1271 | bool WebRtcVideoChannel2::SetRecvRtpHeaderExtensions( |
| 1272 | const std::vector<RtpHeaderExtension>& extensions) { |
pbos@webrtc.org | 587ef60 | 2014-06-16 17:32:02 +0000 | [diff] [blame] | 1273 | LOG(LS_INFO) << "SetRecvRtpHeaderExtensions: " |
| 1274 | << RtpExtensionsToString(extensions); |
pbos@webrtc.org | 3c10758 | 2014-07-20 15:27:35 +0000 | [diff] [blame] | 1275 | if (!ValidateRtpHeaderExtensionIds(extensions)) |
| 1276 | return false; |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 1277 | |
pbos@webrtc.org | 3c10758 | 2014-07-20 15:27:35 +0000 | [diff] [blame] | 1278 | recv_rtp_extensions_ = FilterRtpExtensions(extensions); |
pbos@webrtc.org | 575d126 | 2014-10-08 14:48:08 +0000 | [diff] [blame] | 1279 | rtc::CritScope stream_lock(&stream_crit_); |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 1280 | for (std::map<uint32, WebRtcVideoReceiveStream*>::iterator it = |
| 1281 | receive_streams_.begin(); |
| 1282 | it != receive_streams_.end(); |
| 1283 | ++it) { |
| 1284 | it->second->SetRtpExtensions(recv_rtp_extensions_); |
| 1285 | } |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1286 | return true; |
| 1287 | } |
| 1288 | |
| 1289 | bool WebRtcVideoChannel2::SetSendRtpHeaderExtensions( |
| 1290 | const std::vector<RtpHeaderExtension>& extensions) { |
pbos@webrtc.org | 587ef60 | 2014-06-16 17:32:02 +0000 | [diff] [blame] | 1291 | LOG(LS_INFO) << "SetSendRtpHeaderExtensions: " |
| 1292 | << RtpExtensionsToString(extensions); |
pbos@webrtc.org | 3c10758 | 2014-07-20 15:27:35 +0000 | [diff] [blame] | 1293 | if (!ValidateRtpHeaderExtensionIds(extensions)) |
| 1294 | return false; |
| 1295 | |
| 1296 | send_rtp_extensions_ = FilterRtpExtensions(extensions); |
pbos@webrtc.org | a2ef4fe | 2014-11-07 10:54:43 +0000 | [diff] [blame] | 1297 | |
pbos@webrtc.org | 575d126 | 2014-10-08 14:48:08 +0000 | [diff] [blame] | 1298 | rtc::CritScope stream_lock(&stream_crit_); |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 1299 | for (std::map<uint32, WebRtcVideoSendStream*>::iterator it = |
| 1300 | send_streams_.begin(); |
| 1301 | it != send_streams_.end(); |
| 1302 | ++it) { |
| 1303 | it->second->SetRtpExtensions(send_rtp_extensions_); |
| 1304 | } |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1305 | return true; |
| 1306 | } |
| 1307 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1308 | bool WebRtcVideoChannel2::SetMaxSendBandwidth(int bps) { |
| 1309 | // TODO(pbos): Implement. |
| 1310 | LOG(LS_VERBOSE) << "SetMaxSendBandwidth: " << bps; |
| 1311 | return true; |
| 1312 | } |
| 1313 | |
| 1314 | bool WebRtcVideoChannel2::SetOptions(const VideoOptions& options) { |
pbos@webrtc.org | a2ef4fe | 2014-11-07 10:54:43 +0000 | [diff] [blame] | 1315 | LOG(LS_INFO) << "SetOptions: " << options.ToString(); |
| 1316 | VideoOptions old_options = options_; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1317 | options_.SetAll(options); |
pbos@webrtc.org | a2ef4fe | 2014-11-07 10:54:43 +0000 | [diff] [blame] | 1318 | if (options_ == old_options) { |
| 1319 | // No new options to set. |
| 1320 | return true; |
| 1321 | } |
pbos@webrtc.org | 575d126 | 2014-10-08 14:48:08 +0000 | [diff] [blame] | 1322 | rtc::CritScope stream_lock(&stream_crit_); |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1323 | for (std::map<uint32, WebRtcVideoSendStream*>::iterator it = |
| 1324 | send_streams_.begin(); |
| 1325 | it != send_streams_.end(); |
| 1326 | ++it) { |
| 1327 | it->second->SetOptions(options_); |
| 1328 | } |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1329 | return true; |
| 1330 | } |
| 1331 | |
| 1332 | void WebRtcVideoChannel2::SetInterface(NetworkInterface* iface) { |
| 1333 | MediaChannel::SetInterface(iface); |
| 1334 | // Set the RTP recv/send buffer to a bigger size |
| 1335 | MediaChannel::SetOption(NetworkInterface::ST_RTP, |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1336 | rtc::Socket::OPT_RCVBUF, |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1337 | kVideoRtpBufferSize); |
| 1338 | |
buildbot@webrtc.org | ae694ef | 2014-10-28 17:37:17 +0000 | [diff] [blame] | 1339 | // Speculative change to increase the outbound socket buffer size. |
| 1340 | // In b/15152257, we are seeing a significant number of packets discarded |
| 1341 | // due to lack of socket buffer space, although it's not yet clear what the |
| 1342 | // ideal value should be. |
| 1343 | MediaChannel::SetOption(NetworkInterface::ST_RTP, |
| 1344 | rtc::Socket::OPT_SNDBUF, |
| 1345 | kVideoRtpBufferSize); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1346 | } |
| 1347 | |
| 1348 | void WebRtcVideoChannel2::UpdateAspectRatio(int ratio_w, int ratio_h) { |
| 1349 | // TODO(pbos): Implement. |
| 1350 | } |
| 1351 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1352 | void WebRtcVideoChannel2::OnMessage(rtc::Message* msg) { |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1353 | // Ignored. |
| 1354 | } |
| 1355 | |
pbos@webrtc.org | 42684be | 2014-10-03 11:25:45 +0000 | [diff] [blame] | 1356 | void WebRtcVideoChannel2::OnLoadUpdate(Load load) { |
pbos@webrtc.org | 575d126 | 2014-10-08 14:48:08 +0000 | [diff] [blame] | 1357 | rtc::CritScope stream_lock(&stream_crit_); |
pbos@webrtc.org | 42684be | 2014-10-03 11:25:45 +0000 | [diff] [blame] | 1358 | for (std::map<uint32, WebRtcVideoSendStream*>::iterator it = |
| 1359 | send_streams_.begin(); |
| 1360 | it != send_streams_.end(); |
| 1361 | ++it) { |
| 1362 | it->second->OnCpuResolutionRequest(load == kOveruse |
| 1363 | ? CoordinatedVideoAdapter::DOWNGRADE |
| 1364 | : CoordinatedVideoAdapter::UPGRADE); |
| 1365 | } |
| 1366 | } |
| 1367 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1368 | bool WebRtcVideoChannel2::SendRtp(const uint8_t* data, size_t len) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1369 | rtc::Buffer packet(data, len, kMaxRtpPacketLen); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1370 | return MediaChannel::SendPacket(&packet); |
| 1371 | } |
| 1372 | |
| 1373 | bool WebRtcVideoChannel2::SendRtcp(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::SendRtcp(&packet); |
| 1376 | } |
| 1377 | |
| 1378 | void WebRtcVideoChannel2::StartAllSendStreams() { |
pbos@webrtc.org | 575d126 | 2014-10-08 14:48:08 +0000 | [diff] [blame] | 1379 | rtc::CritScope stream_lock(&stream_crit_); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1380 | for (std::map<uint32, WebRtcVideoSendStream*>::iterator it = |
| 1381 | send_streams_.begin(); |
| 1382 | it != send_streams_.end(); |
| 1383 | ++it) { |
| 1384 | it->second->Start(); |
| 1385 | } |
| 1386 | } |
| 1387 | |
| 1388 | void WebRtcVideoChannel2::StopAllSendStreams() { |
pbos@webrtc.org | 575d126 | 2014-10-08 14:48:08 +0000 | [diff] [blame] | 1389 | rtc::CritScope stream_lock(&stream_crit_); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1390 | for (std::map<uint32, WebRtcVideoSendStream*>::iterator it = |
| 1391 | send_streams_.begin(); |
| 1392 | it != send_streams_.end(); |
| 1393 | ++it) { |
| 1394 | it->second->Stop(); |
| 1395 | } |
| 1396 | } |
| 1397 | |
pbos@webrtc.org | 6ae48c6 | 2014-06-06 10:49:19 +0000 | [diff] [blame] | 1398 | WebRtcVideoChannel2::WebRtcVideoSendStream::VideoSendStreamParameters:: |
| 1399 | VideoSendStreamParameters( |
| 1400 | const webrtc::VideoSendStream::Config& config, |
| 1401 | const VideoOptions& options, |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1402 | const Settable<VideoCodecSettings>& codec_settings) |
| 1403 | : config(config), options(options), codec_settings(codec_settings) { |
pbos@webrtc.org | 6ae48c6 | 2014-06-06 10:49:19 +0000 | [diff] [blame] | 1404 | } |
| 1405 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1406 | WebRtcVideoChannel2::WebRtcVideoSendStream::WebRtcVideoSendStream( |
| 1407 | webrtc::Call* call, |
pbos@webrtc.org | 7fe1e03 | 2014-10-14 04:25:33 +0000 | [diff] [blame] | 1408 | WebRtcVideoEncoderFactory* external_encoder_factory, |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1409 | WebRtcVideoEncoderFactory2* encoder_factory, |
pbos@webrtc.org | 6ae48c6 | 2014-06-06 10:49:19 +0000 | [diff] [blame] | 1410 | const VideoOptions& options, |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1411 | const Settable<VideoCodecSettings>& codec_settings, |
| 1412 | const StreamParams& sp, |
| 1413 | const std::vector<webrtc::RtpExtension>& rtp_extensions) |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1414 | : call_(call), |
pbos@webrtc.org | 7fe1e03 | 2014-10-14 04:25:33 +0000 | [diff] [blame] | 1415 | external_encoder_factory_(external_encoder_factory), |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1416 | encoder_factory_(encoder_factory), |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1417 | stream_(NULL), |
pbos@webrtc.org | b648b9d | 2014-08-26 11:08:06 +0000 | [diff] [blame] | 1418 | parameters_(webrtc::VideoSendStream::Config(), options, codec_settings), |
pbos@webrtc.org | 7fe1e03 | 2014-10-14 04:25:33 +0000 | [diff] [blame] | 1419 | allocated_encoder_(NULL, webrtc::kVideoCodecUnknown, false), |
pbos@webrtc.org | b648b9d | 2014-08-26 11:08:06 +0000 | [diff] [blame] | 1420 | capturer_(NULL), |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1421 | sending_(false), |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1422 | muted_(false) { |
| 1423 | parameters_.config.rtp.max_packet_size = kVideoMtu; |
| 1424 | |
| 1425 | sp.GetPrimarySsrcs(¶meters_.config.rtp.ssrcs); |
| 1426 | sp.GetFidSsrcs(parameters_.config.rtp.ssrcs, |
| 1427 | ¶meters_.config.rtp.rtx.ssrcs); |
| 1428 | parameters_.config.rtp.c_name = sp.cname; |
| 1429 | parameters_.config.rtp.extensions = rtp_extensions; |
| 1430 | |
| 1431 | VideoCodecSettings params; |
| 1432 | if (codec_settings.Get(¶ms)) { |
| 1433 | SetCodec(params); |
| 1434 | } |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1435 | } |
| 1436 | |
| 1437 | WebRtcVideoChannel2::WebRtcVideoSendStream::~WebRtcVideoSendStream() { |
| 1438 | DisconnectCapturer(); |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1439 | if (stream_ != NULL) { |
| 1440 | call_->DestroyVideoSendStream(stream_); |
| 1441 | } |
pbos@webrtc.org | 7fe1e03 | 2014-10-14 04:25:33 +0000 | [diff] [blame] | 1442 | DestroyVideoEncoder(&allocated_encoder_); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1443 | } |
| 1444 | |
| 1445 | static void SetWebRtcFrameToBlack(webrtc::I420VideoFrame* video_frame) { |
| 1446 | assert(video_frame != NULL); |
| 1447 | memset(video_frame->buffer(webrtc::kYPlane), |
| 1448 | 16, |
| 1449 | video_frame->allocated_size(webrtc::kYPlane)); |
| 1450 | memset(video_frame->buffer(webrtc::kUPlane), |
| 1451 | 128, |
| 1452 | video_frame->allocated_size(webrtc::kUPlane)); |
| 1453 | memset(video_frame->buffer(webrtc::kVPlane), |
| 1454 | 128, |
| 1455 | video_frame->allocated_size(webrtc::kVPlane)); |
| 1456 | } |
| 1457 | |
| 1458 | static void CreateBlackFrame(webrtc::I420VideoFrame* video_frame, |
| 1459 | int width, |
| 1460 | int height) { |
| 1461 | video_frame->CreateEmptyFrame( |
| 1462 | width, height, width, (width + 1) / 2, (width + 1) / 2); |
| 1463 | SetWebRtcFrameToBlack(video_frame); |
| 1464 | } |
| 1465 | |
| 1466 | static void ConvertToI420VideoFrame(const VideoFrame& frame, |
| 1467 | webrtc::I420VideoFrame* i420_frame) { |
| 1468 | i420_frame->CreateFrame( |
| 1469 | static_cast<int>(frame.GetYPitch() * frame.GetHeight()), |
| 1470 | frame.GetYPlane(), |
| 1471 | static_cast<int>(frame.GetUPitch() * ((frame.GetHeight() + 1) / 2)), |
| 1472 | frame.GetUPlane(), |
| 1473 | static_cast<int>(frame.GetVPitch() * ((frame.GetHeight() + 1) / 2)), |
| 1474 | frame.GetVPlane(), |
| 1475 | static_cast<int>(frame.GetWidth()), |
| 1476 | static_cast<int>(frame.GetHeight()), |
| 1477 | static_cast<int>(frame.GetYPitch()), |
| 1478 | static_cast<int>(frame.GetUPitch()), |
| 1479 | static_cast<int>(frame.GetVPitch())); |
| 1480 | } |
| 1481 | |
| 1482 | void WebRtcVideoChannel2::WebRtcVideoSendStream::InputFrame( |
| 1483 | VideoCapturer* capturer, |
| 1484 | const VideoFrame* frame) { |
| 1485 | LOG(LS_VERBOSE) << "InputFrame: " << frame->GetWidth() << "x" |
| 1486 | << frame->GetHeight(); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1487 | // Lock before copying, can be called concurrently when swapping input source. |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1488 | rtc::CritScope frame_cs(&frame_lock_); |
pbos@webrtc.org | d60d79a | 2014-09-24 07:10:57 +0000 | [diff] [blame] | 1489 | ConvertToI420VideoFrame(*frame, &video_frame_); |
| 1490 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1491 | rtc::CritScope cs(&lock_); |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1492 | if (stream_ == NULL) { |
| 1493 | LOG(LS_WARNING) << "Capturer inputting frames before send codecs are " |
| 1494 | "configured, dropping."; |
| 1495 | return; |
| 1496 | } |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1497 | if (format_.width == 0) { // Dropping frames. |
| 1498 | assert(format_.height == 0); |
| 1499 | LOG(LS_VERBOSE) << "VideoFormat 0x0 set, Dropping frame."; |
| 1500 | return; |
| 1501 | } |
pbos@webrtc.org | d60d79a | 2014-09-24 07:10:57 +0000 | [diff] [blame] | 1502 | if (muted_) { |
| 1503 | // Create a black frame to transmit instead. |
| 1504 | CreateBlackFrame(&video_frame_, |
| 1505 | static_cast<int>(frame->GetWidth()), |
| 1506 | static_cast<int>(frame->GetHeight())); |
| 1507 | } |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1508 | // Reconfigure codec if necessary. |
pbos@webrtc.org | c4175b9 | 2014-09-03 15:25:49 +0000 | [diff] [blame] | 1509 | SetDimensions( |
| 1510 | video_frame_.width(), video_frame_.height(), capturer->IsScreencast()); |
| 1511 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1512 | LOG(LS_VERBOSE) << "SwapFrame: " << video_frame_.width() << "x" |
| 1513 | << video_frame_.height() << " -> (codec) " |
pbos@webrtc.org | bbe0a85 | 2014-09-19 12:30:25 +0000 | [diff] [blame] | 1514 | << parameters_.encoder_config.streams.back().width << "x" |
| 1515 | << parameters_.encoder_config.streams.back().height; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1516 | stream_->Input()->SwapFrame(&video_frame_); |
| 1517 | } |
| 1518 | |
| 1519 | bool WebRtcVideoChannel2::WebRtcVideoSendStream::SetCapturer( |
| 1520 | VideoCapturer* capturer) { |
| 1521 | if (!DisconnectCapturer() && capturer == NULL) { |
| 1522 | return false; |
| 1523 | } |
| 1524 | |
| 1525 | { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1526 | rtc::CritScope cs(&lock_); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1527 | |
pbos@webrtc.org | 9359cb3 | 2014-07-23 15:44:48 +0000 | [diff] [blame] | 1528 | if (capturer == NULL) { |
| 1529 | if (stream_ != NULL) { |
| 1530 | LOG(LS_VERBOSE) << "Disabling capturer, sending black frame."; |
| 1531 | webrtc::I420VideoFrame black_frame; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1532 | |
pbos@webrtc.org | a2ef4fe | 2014-11-07 10:54:43 +0000 | [diff] [blame] | 1533 | // TODO(pbos): Base width/height on last_dimensions_. This will however |
| 1534 | // fail the test AddRemoveCapturer which needs to be fixed to permit |
| 1535 | // sending black frames in the same size that was previously sent. |
pbos@webrtc.org | 9359cb3 | 2014-07-23 15:44:48 +0000 | [diff] [blame] | 1536 | int width = format_.width; |
| 1537 | int height = format_.height; |
| 1538 | int half_width = (width + 1) / 2; |
| 1539 | black_frame.CreateEmptyFrame( |
| 1540 | width, height, width, half_width, half_width); |
| 1541 | SetWebRtcFrameToBlack(&black_frame); |
pbos@webrtc.org | a2ef4fe | 2014-11-07 10:54:43 +0000 | [diff] [blame] | 1542 | SetDimensions(width, height, last_dimensions_.is_screencast); |
pbos@webrtc.org | 9359cb3 | 2014-07-23 15:44:48 +0000 | [diff] [blame] | 1543 | stream_->Input()->SwapFrame(&black_frame); |
| 1544 | } |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1545 | |
| 1546 | capturer_ = NULL; |
| 1547 | return true; |
| 1548 | } |
| 1549 | |
| 1550 | capturer_ = capturer; |
| 1551 | } |
| 1552 | // Lock cannot be held while connecting the capturer to prevent lock-order |
| 1553 | // violations. |
| 1554 | capturer->SignalVideoFrame.connect(this, &WebRtcVideoSendStream::InputFrame); |
| 1555 | return true; |
| 1556 | } |
| 1557 | |
| 1558 | bool WebRtcVideoChannel2::WebRtcVideoSendStream::SetVideoFormat( |
| 1559 | const VideoFormat& format) { |
| 1560 | if ((format.width == 0 || format.height == 0) && |
| 1561 | format.width != format.height) { |
| 1562 | LOG(LS_ERROR) << "Can't set VideoFormat, width or height is zero (but not " |
| 1563 | "both, 0x0 drops frames)."; |
| 1564 | return false; |
| 1565 | } |
| 1566 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1567 | rtc::CritScope cs(&lock_); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1568 | if (format.width == 0 && format.height == 0) { |
| 1569 | LOG(LS_INFO) |
| 1570 | << "0x0 resolution selected. Captured frames will be dropped for ssrc: " |
pbos@webrtc.org | 6ae48c6 | 2014-06-06 10:49:19 +0000 | [diff] [blame] | 1571 | << parameters_.config.rtp.ssrcs[0] << "."; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1572 | } else { |
| 1573 | // TODO(pbos): Fix me, this only affects the last stream! |
pbos@webrtc.org | bbe0a85 | 2014-09-19 12:30:25 +0000 | [diff] [blame] | 1574 | parameters_.encoder_config.streams.back().max_framerate = |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1575 | VideoFormat::IntervalToFps(format.interval); |
pbos@webrtc.org | c4175b9 | 2014-09-03 15:25:49 +0000 | [diff] [blame] | 1576 | SetDimensions(format.width, format.height, false); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1577 | } |
| 1578 | |
| 1579 | format_ = format; |
| 1580 | return true; |
| 1581 | } |
| 1582 | |
pbos@webrtc.org | ef8bb8d | 2014-08-13 21:36:18 +0000 | [diff] [blame] | 1583 | void WebRtcVideoChannel2::WebRtcVideoSendStream::MuteStream(bool mute) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1584 | rtc::CritScope cs(&lock_); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1585 | muted_ = mute; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1586 | } |
| 1587 | |
| 1588 | bool WebRtcVideoChannel2::WebRtcVideoSendStream::DisconnectCapturer() { |
pbos@webrtc.org | 963b979 | 2014-10-07 14:27:27 +0000 | [diff] [blame] | 1589 | cricket::VideoCapturer* capturer; |
| 1590 | { |
| 1591 | rtc::CritScope cs(&lock_); |
| 1592 | if (capturer_ == NULL) { |
| 1593 | return false; |
| 1594 | } |
| 1595 | capturer = capturer_; |
| 1596 | capturer_ = NULL; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1597 | } |
pbos@webrtc.org | 963b979 | 2014-10-07 14:27:27 +0000 | [diff] [blame] | 1598 | capturer->SignalVideoFrame.disconnect(this); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1599 | return true; |
| 1600 | } |
| 1601 | |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1602 | void WebRtcVideoChannel2::WebRtcVideoSendStream::SetOptions( |
| 1603 | const VideoOptions& options) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1604 | rtc::CritScope cs(&lock_); |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1605 | VideoCodecSettings codec_settings; |
| 1606 | if (parameters_.codec_settings.Get(&codec_settings)) { |
| 1607 | SetCodecAndOptions(codec_settings, options); |
| 1608 | } else { |
| 1609 | parameters_.options = options; |
| 1610 | } |
| 1611 | } |
pbos@webrtc.org | 7fe1e03 | 2014-10-14 04:25:33 +0000 | [diff] [blame] | 1612 | |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1613 | void WebRtcVideoChannel2::WebRtcVideoSendStream::SetCodec( |
| 1614 | const VideoCodecSettings& codec_settings) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1615 | rtc::CritScope cs(&lock_); |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1616 | SetCodecAndOptions(codec_settings, parameters_.options); |
| 1617 | } |
pbos@webrtc.org | 7fe1e03 | 2014-10-14 04:25:33 +0000 | [diff] [blame] | 1618 | |
| 1619 | webrtc::VideoCodecType CodecTypeFromName(const std::string& name) { |
| 1620 | if (CodecNameMatches(name, kVp8CodecName)) { |
| 1621 | return webrtc::kVideoCodecVP8; |
andresp@webrtc.org | 188d3b2 | 2014-11-07 13:21:04 +0000 | [diff] [blame] | 1622 | } else if (CodecNameMatches(name, kVp9CodecName)) { |
| 1623 | return webrtc::kVideoCodecVP9; |
pbos@webrtc.org | 7fe1e03 | 2014-10-14 04:25:33 +0000 | [diff] [blame] | 1624 | } else if (CodecNameMatches(name, kH264CodecName)) { |
| 1625 | return webrtc::kVideoCodecH264; |
| 1626 | } |
| 1627 | return webrtc::kVideoCodecUnknown; |
| 1628 | } |
| 1629 | |
| 1630 | WebRtcVideoChannel2::WebRtcVideoSendStream::AllocatedEncoder |
| 1631 | WebRtcVideoChannel2::WebRtcVideoSendStream::CreateVideoEncoder( |
| 1632 | const VideoCodec& codec) { |
| 1633 | webrtc::VideoCodecType type = CodecTypeFromName(codec.name); |
| 1634 | |
| 1635 | // Do not re-create encoders of the same type. |
| 1636 | if (type == allocated_encoder_.type && allocated_encoder_.encoder != NULL) { |
| 1637 | return allocated_encoder_; |
| 1638 | } |
| 1639 | |
| 1640 | if (external_encoder_factory_ != NULL) { |
| 1641 | webrtc::VideoEncoder* encoder = |
| 1642 | external_encoder_factory_->CreateVideoEncoder(type); |
| 1643 | if (encoder != NULL) { |
| 1644 | return AllocatedEncoder(encoder, type, true); |
| 1645 | } |
| 1646 | } |
| 1647 | |
| 1648 | if (type == webrtc::kVideoCodecVP8) { |
| 1649 | return AllocatedEncoder( |
| 1650 | webrtc::VideoEncoder::Create(webrtc::VideoEncoder::kVp8), type, false); |
andresp@webrtc.org | 188d3b2 | 2014-11-07 13:21:04 +0000 | [diff] [blame] | 1651 | } else if (type == webrtc::kVideoCodecVP9) { |
| 1652 | return AllocatedEncoder( |
| 1653 | webrtc::VideoEncoder::Create(webrtc::VideoEncoder::kVp9), type, false); |
pbos@webrtc.org | 7fe1e03 | 2014-10-14 04:25:33 +0000 | [diff] [blame] | 1654 | } |
| 1655 | |
| 1656 | // This shouldn't happen, we should not be trying to create something we don't |
| 1657 | // support. |
| 1658 | assert(false); |
| 1659 | return AllocatedEncoder(NULL, webrtc::kVideoCodecUnknown, false); |
| 1660 | } |
| 1661 | |
| 1662 | void WebRtcVideoChannel2::WebRtcVideoSendStream::DestroyVideoEncoder( |
| 1663 | AllocatedEncoder* encoder) { |
| 1664 | if (encoder->external) { |
| 1665 | external_encoder_factory_->DestroyVideoEncoder(encoder->encoder); |
| 1666 | } else { |
| 1667 | delete encoder->encoder; |
| 1668 | } |
| 1669 | } |
| 1670 | |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1671 | void WebRtcVideoChannel2::WebRtcVideoSendStream::SetCodecAndOptions( |
| 1672 | const VideoCodecSettings& codec_settings, |
| 1673 | const VideoOptions& options) { |
pbos@webrtc.org | a2ef4fe | 2014-11-07 10:54:43 +0000 | [diff] [blame] | 1674 | if (last_dimensions_.width == -1) { |
| 1675 | last_dimensions_.width = codec_settings.codec.width; |
| 1676 | last_dimensions_.height = codec_settings.codec.height; |
| 1677 | last_dimensions_.is_screencast = false; |
| 1678 | } |
| 1679 | parameters_.encoder_config = |
| 1680 | CreateVideoEncoderConfig(last_dimensions_, codec_settings.codec); |
| 1681 | if (parameters_.encoder_config.streams.empty()) { |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1682 | return; |
| 1683 | } |
pbos@webrtc.org | a2ef4fe | 2014-11-07 10:54:43 +0000 | [diff] [blame] | 1684 | |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1685 | format_ = VideoFormat(codec_settings.codec.width, |
| 1686 | codec_settings.codec.height, |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1687 | VideoFormat::FpsToInterval(30), |
| 1688 | FOURCC_I420); |
pbos@webrtc.org | 6ae48c6 | 2014-06-06 10:49:19 +0000 | [diff] [blame] | 1689 | |
pbos@webrtc.org | 7fe1e03 | 2014-10-14 04:25:33 +0000 | [diff] [blame] | 1690 | AllocatedEncoder new_encoder = CreateVideoEncoder(codec_settings.codec); |
| 1691 | parameters_.config.encoder_settings.encoder = new_encoder.encoder; |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1692 | parameters_.config.encoder_settings.payload_name = codec_settings.codec.name; |
| 1693 | parameters_.config.encoder_settings.payload_type = codec_settings.codec.id; |
| 1694 | parameters_.config.rtp.fec = codec_settings.fec; |
| 1695 | |
| 1696 | // Set RTX payload type if RTX is enabled. |
| 1697 | if (!parameters_.config.rtp.rtx.ssrcs.empty()) { |
| 1698 | parameters_.config.rtp.rtx.payload_type = codec_settings.rtx_payload_type; |
pbos@webrtc.org | 543e589 | 2014-07-23 07:01:31 +0000 | [diff] [blame] | 1699 | |
| 1700 | options.use_payload_padding.Get( |
| 1701 | ¶meters_.config.rtp.rtx.pad_with_redundant_payloads); |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1702 | } |
| 1703 | |
| 1704 | if (IsNackEnabled(codec_settings.codec)) { |
| 1705 | parameters_.config.rtp.nack.rtp_history_ms = kNackHistoryMs; |
| 1706 | } |
| 1707 | |
pbos@webrtc.org | 5ff71ab | 2014-07-23 07:28:56 +0000 | [diff] [blame] | 1708 | options.suspend_below_min_bitrate.Get( |
| 1709 | ¶meters_.config.suspend_below_min_bitrate); |
| 1710 | |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1711 | parameters_.codec_settings.Set(codec_settings); |
pbos@webrtc.org | 6ae48c6 | 2014-06-06 10:49:19 +0000 | [diff] [blame] | 1712 | parameters_.options = options; |
pbos@webrtc.org | 543e589 | 2014-07-23 07:01:31 +0000 | [diff] [blame] | 1713 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1714 | RecreateWebRtcStream(); |
pbos@webrtc.org | 7fe1e03 | 2014-10-14 04:25:33 +0000 | [diff] [blame] | 1715 | if (allocated_encoder_.encoder != new_encoder.encoder) { |
| 1716 | DestroyVideoEncoder(&allocated_encoder_); |
| 1717 | allocated_encoder_ = new_encoder; |
| 1718 | } |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1719 | } |
| 1720 | |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 1721 | void WebRtcVideoChannel2::WebRtcVideoSendStream::SetRtpExtensions( |
| 1722 | const std::vector<webrtc::RtpExtension>& rtp_extensions) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1723 | rtc::CritScope cs(&lock_); |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 1724 | parameters_.config.rtp.extensions = rtp_extensions; |
| 1725 | RecreateWebRtcStream(); |
| 1726 | } |
| 1727 | |
pbos@webrtc.org | a2ef4fe | 2014-11-07 10:54:43 +0000 | [diff] [blame] | 1728 | webrtc::VideoEncoderConfig |
| 1729 | WebRtcVideoChannel2::WebRtcVideoSendStream::CreateVideoEncoderConfig( |
| 1730 | const Dimensions& dimensions, |
| 1731 | const VideoCodec& codec) const { |
| 1732 | webrtc::VideoEncoderConfig encoder_config; |
| 1733 | if (dimensions.is_screencast) { |
pbos@webrtc.org | efc82c2 | 2014-10-27 13:58:00 +0000 | [diff] [blame] | 1734 | int screencast_min_bitrate_kbps; |
| 1735 | parameters_.options.screencast_min_bitrate.Get( |
| 1736 | &screencast_min_bitrate_kbps); |
| 1737 | encoder_config.min_transmit_bitrate_bps = |
| 1738 | screencast_min_bitrate_kbps * 1000; |
| 1739 | encoder_config.content_type = webrtc::VideoEncoderConfig::kScreenshare; |
| 1740 | } else { |
| 1741 | encoder_config.min_transmit_bitrate_bps = 0; |
| 1742 | encoder_config.content_type = webrtc::VideoEncoderConfig::kRealtimeVideo; |
| 1743 | } |
| 1744 | |
pbos@webrtc.org | a2ef4fe | 2014-11-07 10:54:43 +0000 | [diff] [blame] | 1745 | // Restrict dimensions according to codec max. |
| 1746 | int width = dimensions.width; |
| 1747 | int height = dimensions.height; |
| 1748 | if (!dimensions.is_screencast) { |
| 1749 | if (codec.width < width) |
| 1750 | width = codec.width; |
| 1751 | if (codec.height < height) |
| 1752 | height = codec.height; |
| 1753 | } |
| 1754 | |
| 1755 | VideoCodec clamped_codec = codec; |
| 1756 | clamped_codec.width = width; |
| 1757 | clamped_codec.height = height; |
pbos@webrtc.org | cddd17c | 2014-09-16 16:33:13 +0000 | [diff] [blame] | 1758 | |
pbos@webrtc.org | bbe0a85 | 2014-09-19 12:30:25 +0000 | [diff] [blame] | 1759 | encoder_config.streams = encoder_factory_->CreateVideoStreams( |
pbos@webrtc.org | a2ef4fe | 2014-11-07 10:54:43 +0000 | [diff] [blame] | 1760 | clamped_codec, parameters_.options, parameters_.config.rtp.ssrcs.size()); |
pbos@webrtc.org | 6f48f1b | 2014-07-22 16:29:54 +0000 | [diff] [blame] | 1761 | |
pbos@webrtc.org | b7ed779 | 2014-10-31 13:08:10 +0000 | [diff] [blame] | 1762 | // Conference mode screencast uses 2 temporal layers split at 100kbit. |
| 1763 | if (parameters_.options.conference_mode.GetWithDefaultIfUnset(false) && |
pbos@webrtc.org | a2ef4fe | 2014-11-07 10:54:43 +0000 | [diff] [blame] | 1764 | dimensions.is_screencast && encoder_config.streams.size() == 1) { |
pbos@webrtc.org | b7ed779 | 2014-10-31 13:08:10 +0000 | [diff] [blame] | 1765 | encoder_config.streams[0].temporal_layer_thresholds_bps.clear(); |
| 1766 | encoder_config.streams[0].temporal_layer_thresholds_bps.push_back( |
| 1767 | kConferenceModeTemporalLayerBitrateBps); |
| 1768 | } |
pbos@webrtc.org | a2ef4fe | 2014-11-07 10:54:43 +0000 | [diff] [blame] | 1769 | return encoder_config; |
| 1770 | } |
| 1771 | |
| 1772 | void WebRtcVideoChannel2::WebRtcVideoSendStream::SetDimensions( |
| 1773 | int width, |
| 1774 | int height, |
| 1775 | bool is_screencast) { |
| 1776 | if (last_dimensions_.width == width && last_dimensions_.height == height && |
| 1777 | last_dimensions_.is_screencast == is_screencast) { |
| 1778 | // Configured using the same parameters, do not reconfigure. |
| 1779 | return; |
| 1780 | } |
| 1781 | LOG(LS_INFO) << "SetDimensions: " << width << "x" << height |
| 1782 | << (is_screencast ? " (screencast)" : " (not screencast)"); |
| 1783 | |
| 1784 | last_dimensions_.width = width; |
| 1785 | last_dimensions_.height = height; |
| 1786 | last_dimensions_.is_screencast = is_screencast; |
| 1787 | |
| 1788 | assert(!parameters_.encoder_config.streams.empty()); |
| 1789 | |
| 1790 | VideoCodecSettings codec_settings; |
| 1791 | parameters_.codec_settings.Get(&codec_settings); |
| 1792 | |
| 1793 | webrtc::VideoEncoderConfig encoder_config = |
| 1794 | CreateVideoEncoderConfig(last_dimensions_, codec_settings.codec); |
| 1795 | |
| 1796 | encoder_config.encoder_specific_settings = |
| 1797 | encoder_factory_->CreateVideoEncoderSettings(codec_settings.codec, |
| 1798 | parameters_.options); |
pbos@webrtc.org | b7ed779 | 2014-10-31 13:08:10 +0000 | [diff] [blame] | 1799 | |
pbos@webrtc.org | bbe0a85 | 2014-09-19 12:30:25 +0000 | [diff] [blame] | 1800 | bool stream_reconfigured = stream_->ReconfigureVideoEncoder(encoder_config); |
| 1801 | |
| 1802 | encoder_factory_->DestroyVideoEncoderSettings( |
| 1803 | codec_settings.codec, |
| 1804 | encoder_config.encoder_specific_settings); |
| 1805 | |
| 1806 | encoder_config.encoder_specific_settings = NULL; |
pbos@webrtc.org | 6f48f1b | 2014-07-22 16:29:54 +0000 | [diff] [blame] | 1807 | |
| 1808 | if (!stream_reconfigured) { |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1809 | LOG(LS_WARNING) << "Failed to reconfigure video encoder for dimensions: " |
| 1810 | << width << "x" << height; |
| 1811 | return; |
| 1812 | } |
pbos@webrtc.org | cddd17c | 2014-09-16 16:33:13 +0000 | [diff] [blame] | 1813 | |
pbos@webrtc.org | bbe0a85 | 2014-09-19 12:30:25 +0000 | [diff] [blame] | 1814 | parameters_.encoder_config = encoder_config; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1815 | } |
| 1816 | |
| 1817 | void WebRtcVideoChannel2::WebRtcVideoSendStream::Start() { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1818 | rtc::CritScope cs(&lock_); |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1819 | assert(stream_ != NULL); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1820 | stream_->Start(); |
| 1821 | sending_ = true; |
| 1822 | } |
| 1823 | |
| 1824 | void WebRtcVideoChannel2::WebRtcVideoSendStream::Stop() { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1825 | rtc::CritScope cs(&lock_); |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1826 | if (stream_ != NULL) { |
| 1827 | stream_->Stop(); |
| 1828 | } |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1829 | sending_ = false; |
| 1830 | } |
| 1831 | |
pbos@webrtc.org | e6f84ae | 2014-07-18 11:11:55 +0000 | [diff] [blame] | 1832 | VideoSenderInfo |
| 1833 | WebRtcVideoChannel2::WebRtcVideoSendStream::GetVideoSenderInfo() { |
| 1834 | VideoSenderInfo info; |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1835 | rtc::CritScope cs(&lock_); |
pbos@webrtc.org | e6f84ae | 2014-07-18 11:11:55 +0000 | [diff] [blame] | 1836 | for (size_t i = 0; i < parameters_.config.rtp.ssrcs.size(); ++i) { |
| 1837 | info.add_ssrc(parameters_.config.rtp.ssrcs[i]); |
| 1838 | } |
| 1839 | |
pbos@webrtc.org | c3d2bd2 | 2014-08-12 20:55:10 +0000 | [diff] [blame] | 1840 | if (stream_ == NULL) { |
| 1841 | return info; |
| 1842 | } |
| 1843 | |
pbos@webrtc.org | e6f84ae | 2014-07-18 11:11:55 +0000 | [diff] [blame] | 1844 | webrtc::VideoSendStream::Stats stats = stream_->GetStats(); |
| 1845 | info.framerate_input = stats.input_frame_rate; |
| 1846 | info.framerate_sent = stats.encode_frame_rate; |
| 1847 | |
stefan@webrtc.org | 0bae1fa | 2014-11-05 14:05:29 +0000 | [diff] [blame] | 1848 | for (std::map<uint32_t, webrtc::SsrcStats>::iterator it = |
pbos@webrtc.org | e6f84ae | 2014-07-18 11:11:55 +0000 | [diff] [blame] | 1849 | stats.substreams.begin(); |
| 1850 | it != stats.substreams.end(); |
| 1851 | ++it) { |
| 1852 | // TODO(pbos): Wire up additional stats, such as padding bytes. |
stefan@webrtc.org | 0bae1fa | 2014-11-05 14:05:29 +0000 | [diff] [blame] | 1853 | webrtc::SsrcStats stream_stats = it->second; |
pbos@webrtc.org | e6f84ae | 2014-07-18 11:11:55 +0000 | [diff] [blame] | 1854 | info.bytes_sent += stream_stats.rtp_stats.bytes + |
| 1855 | stream_stats.rtp_stats.header_bytes + |
| 1856 | stream_stats.rtp_stats.padding_bytes; |
| 1857 | info.packets_sent += stream_stats.rtp_stats.packets; |
| 1858 | info.packets_lost += stream_stats.rtcp_stats.cumulative_lost; |
| 1859 | } |
| 1860 | |
| 1861 | if (!stats.substreams.empty()) { |
| 1862 | // TODO(pbos): Report fraction lost per SSRC. |
stefan@webrtc.org | 0bae1fa | 2014-11-05 14:05:29 +0000 | [diff] [blame] | 1863 | webrtc::SsrcStats first_stream_stats = stats.substreams.begin()->second; |
pbos@webrtc.org | e6f84ae | 2014-07-18 11:11:55 +0000 | [diff] [blame] | 1864 | info.fraction_lost = |
| 1865 | static_cast<float>(first_stream_stats.rtcp_stats.fraction_lost) / |
| 1866 | (1 << 8); |
| 1867 | } |
| 1868 | |
| 1869 | if (capturer_ != NULL && !capturer_->IsMuted()) { |
| 1870 | VideoFormat last_captured_frame_format; |
| 1871 | capturer_->GetStats(&info.adapt_frame_drops, |
| 1872 | &info.effects_frame_drops, |
| 1873 | &info.capturer_frame_time, |
| 1874 | &last_captured_frame_format); |
| 1875 | info.input_frame_width = last_captured_frame_format.width; |
| 1876 | info.input_frame_height = last_captured_frame_format.height; |
| 1877 | info.send_frame_width = |
pbos@webrtc.org | bbe0a85 | 2014-09-19 12:30:25 +0000 | [diff] [blame] | 1878 | static_cast<int>(parameters_.encoder_config.streams.front().width); |
pbos@webrtc.org | e6f84ae | 2014-07-18 11:11:55 +0000 | [diff] [blame] | 1879 | info.send_frame_height = |
pbos@webrtc.org | bbe0a85 | 2014-09-19 12:30:25 +0000 | [diff] [blame] | 1880 | static_cast<int>(parameters_.encoder_config.streams.front().height); |
pbos@webrtc.org | e6f84ae | 2014-07-18 11:11:55 +0000 | [diff] [blame] | 1881 | } |
| 1882 | |
| 1883 | // TODO(pbos): Support or remove the following stats. |
| 1884 | info.packets_cached = -1; |
| 1885 | info.rtt_ms = -1; |
| 1886 | |
| 1887 | return info; |
| 1888 | } |
| 1889 | |
stefan@webrtc.org | 0bae1fa | 2014-11-05 14:05:29 +0000 | [diff] [blame] | 1890 | void WebRtcVideoChannel2::WebRtcVideoSendStream::FillBandwidthEstimationInfo( |
| 1891 | BandwidthEstimationInfo* bwe_info) { |
| 1892 | rtc::CritScope cs(&lock_); |
| 1893 | if (stream_ == NULL) { |
| 1894 | return; |
| 1895 | } |
| 1896 | webrtc::VideoSendStream::Stats stats = stream_->GetStats(); |
| 1897 | for (std::map<uint32_t, webrtc::SsrcStats>::iterator it = |
| 1898 | stats.substreams.begin(); |
| 1899 | it != stats.substreams.end(); |
| 1900 | ++it) { |
| 1901 | bwe_info->transmit_bitrate += it->second.total_bitrate_bps; |
| 1902 | bwe_info->retransmit_bitrate += it->second.retransmit_bitrate_bps; |
| 1903 | } |
| 1904 | bwe_info->actual_enc_bitrate = stats.media_bitrate_bps; |
| 1905 | } |
| 1906 | |
pbos@webrtc.org | 42684be | 2014-10-03 11:25:45 +0000 | [diff] [blame] | 1907 | void WebRtcVideoChannel2::WebRtcVideoSendStream::OnCpuResolutionRequest( |
| 1908 | CoordinatedVideoAdapter::AdaptRequest adapt_request) { |
| 1909 | rtc::CritScope cs(&lock_); |
| 1910 | bool adapt_cpu; |
| 1911 | parameters_.options.cpu_overuse_detection.Get(&adapt_cpu); |
| 1912 | if (!adapt_cpu) { |
| 1913 | return; |
| 1914 | } |
| 1915 | if (capturer_ == NULL || capturer_->video_adapter() == NULL) { |
| 1916 | return; |
| 1917 | } |
| 1918 | |
| 1919 | capturer_->video_adapter()->OnCpuResolutionRequest(adapt_request); |
| 1920 | } |
| 1921 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1922 | void WebRtcVideoChannel2::WebRtcVideoSendStream::RecreateWebRtcStream() { |
| 1923 | if (stream_ != NULL) { |
| 1924 | call_->DestroyVideoSendStream(stream_); |
| 1925 | } |
pbos@webrtc.org | 6ae48c6 | 2014-06-06 10:49:19 +0000 | [diff] [blame] | 1926 | |
pbos@webrtc.org | 6f48f1b | 2014-07-22 16:29:54 +0000 | [diff] [blame] | 1927 | VideoCodecSettings codec_settings; |
| 1928 | parameters_.codec_settings.Get(&codec_settings); |
pbos@webrtc.org | bbe0a85 | 2014-09-19 12:30:25 +0000 | [diff] [blame] | 1929 | parameters_.encoder_config.encoder_specific_settings = |
| 1930 | encoder_factory_->CreateVideoEncoderSettings(codec_settings.codec, |
| 1931 | parameters_.options); |
pbos@webrtc.org | 6f48f1b | 2014-07-22 16:29:54 +0000 | [diff] [blame] | 1932 | |
pbos@webrtc.org | bbe0a85 | 2014-09-19 12:30:25 +0000 | [diff] [blame] | 1933 | stream_ = call_->CreateVideoSendStream(parameters_.config, |
| 1934 | parameters_.encoder_config); |
pbos@webrtc.org | 6f48f1b | 2014-07-22 16:29:54 +0000 | [diff] [blame] | 1935 | |
pbos@webrtc.org | bbe0a85 | 2014-09-19 12:30:25 +0000 | [diff] [blame] | 1936 | encoder_factory_->DestroyVideoEncoderSettings( |
| 1937 | codec_settings.codec, |
| 1938 | parameters_.encoder_config.encoder_specific_settings); |
| 1939 | |
| 1940 | parameters_.encoder_config.encoder_specific_settings = NULL; |
pbos@webrtc.org | 6f48f1b | 2014-07-22 16:29:54 +0000 | [diff] [blame] | 1941 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1942 | if (sending_) { |
| 1943 | stream_->Start(); |
| 1944 | } |
| 1945 | } |
| 1946 | |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 1947 | WebRtcVideoChannel2::WebRtcVideoReceiveStream::WebRtcVideoReceiveStream( |
| 1948 | webrtc::Call* call, |
pbos@webrtc.org | 776e6f2 | 2014-10-29 15:28:39 +0000 | [diff] [blame] | 1949 | WebRtcVideoDecoderFactory* external_decoder_factory, |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 1950 | const webrtc::VideoReceiveStream::Config& config, |
| 1951 | const std::vector<VideoCodecSettings>& recv_codecs) |
| 1952 | : call_(call), |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 1953 | stream_(NULL), |
pbos@webrtc.org | b648b9d | 2014-08-26 11:08:06 +0000 | [diff] [blame] | 1954 | config_(config), |
pbos@webrtc.org | 776e6f2 | 2014-10-29 15:28:39 +0000 | [diff] [blame] | 1955 | external_decoder_factory_(external_decoder_factory), |
pbos@webrtc.org | b648b9d | 2014-08-26 11:08:06 +0000 | [diff] [blame] | 1956 | renderer_(NULL), |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 1957 | last_width_(-1), |
pbos@webrtc.org | b648b9d | 2014-08-26 11:08:06 +0000 | [diff] [blame] | 1958 | last_height_(-1) { |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 1959 | config_.renderer = this; |
| 1960 | // SetRecvCodecs will also reset (start) the VideoReceiveStream. |
| 1961 | SetRecvCodecs(recv_codecs); |
| 1962 | } |
| 1963 | |
| 1964 | WebRtcVideoChannel2::WebRtcVideoReceiveStream::~WebRtcVideoReceiveStream() { |
| 1965 | call_->DestroyVideoReceiveStream(stream_); |
pbos@webrtc.org | 96a9325 | 2014-11-03 14:46:44 +0000 | [diff] [blame] | 1966 | ClearDecoders(&allocated_decoders_); |
| 1967 | } |
| 1968 | |
| 1969 | WebRtcVideoChannel2::WebRtcVideoReceiveStream::AllocatedDecoder |
| 1970 | WebRtcVideoChannel2::WebRtcVideoReceiveStream::CreateOrReuseVideoDecoder( |
| 1971 | std::vector<AllocatedDecoder>* old_decoders, |
| 1972 | const VideoCodec& codec) { |
| 1973 | webrtc::VideoCodecType type = CodecTypeFromName(codec.name); |
| 1974 | |
| 1975 | for (size_t i = 0; i < old_decoders->size(); ++i) { |
| 1976 | if ((*old_decoders)[i].type == type) { |
| 1977 | AllocatedDecoder decoder = (*old_decoders)[i]; |
| 1978 | (*old_decoders)[i] = old_decoders->back(); |
| 1979 | old_decoders->pop_back(); |
| 1980 | return decoder; |
| 1981 | } |
| 1982 | } |
| 1983 | |
| 1984 | if (external_decoder_factory_ != NULL) { |
| 1985 | webrtc::VideoDecoder* decoder = |
| 1986 | external_decoder_factory_->CreateVideoDecoder(type); |
| 1987 | if (decoder != NULL) { |
| 1988 | return AllocatedDecoder(decoder, type, true); |
| 1989 | } |
| 1990 | } |
| 1991 | |
| 1992 | if (type == webrtc::kVideoCodecVP8) { |
| 1993 | return AllocatedDecoder( |
| 1994 | webrtc::VideoDecoder::Create(webrtc::VideoDecoder::kVp8), type, false); |
| 1995 | } |
| 1996 | |
| 1997 | // This shouldn't happen, we should not be trying to create something we don't |
| 1998 | // support. |
| 1999 | assert(false); |
| 2000 | return AllocatedDecoder(NULL, webrtc::kVideoCodecUnknown, false); |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 2001 | } |
| 2002 | |
| 2003 | void WebRtcVideoChannel2::WebRtcVideoReceiveStream::SetRecvCodecs( |
| 2004 | const std::vector<VideoCodecSettings>& recv_codecs) { |
pbos@webrtc.org | 96a9325 | 2014-11-03 14:46:44 +0000 | [diff] [blame] | 2005 | std::vector<AllocatedDecoder> old_decoders = allocated_decoders_; |
| 2006 | allocated_decoders_.clear(); |
| 2007 | config_.decoders.clear(); |
| 2008 | for (size_t i = 0; i < recv_codecs.size(); ++i) { |
| 2009 | AllocatedDecoder allocated_decoder = |
| 2010 | CreateOrReuseVideoDecoder(&old_decoders, recv_codecs[i].codec); |
| 2011 | allocated_decoders_.push_back(allocated_decoder); |
| 2012 | |
| 2013 | webrtc::VideoReceiveStream::Decoder decoder; |
| 2014 | decoder.decoder = allocated_decoder.decoder; |
| 2015 | decoder.payload_type = recv_codecs[i].codec.id; |
| 2016 | decoder.payload_name = recv_codecs[i].codec.name; |
| 2017 | config_.decoders.push_back(decoder); |
| 2018 | } |
| 2019 | |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 2020 | // TODO(pbos): Reconfigure RTX based on incoming recv_codecs. |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 2021 | config_.rtp.fec = recv_codecs.front().fec; |
pbos@webrtc.org | 257e130 | 2014-07-25 19:01:32 +0000 | [diff] [blame] | 2022 | config_.rtp.nack.rtp_history_ms = |
| 2023 | IsNackEnabled(recv_codecs.begin()->codec) ? kNackHistoryMs : 0; |
| 2024 | config_.rtp.remb = IsRembEnabled(recv_codecs.begin()->codec); |
| 2025 | |
pbos@webrtc.org | 96a9325 | 2014-11-03 14:46:44 +0000 | [diff] [blame] | 2026 | ClearDecoders(&old_decoders); |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 2027 | RecreateWebRtcStream(); |
| 2028 | } |
| 2029 | |
| 2030 | void WebRtcVideoChannel2::WebRtcVideoReceiveStream::SetRtpExtensions( |
| 2031 | const std::vector<webrtc::RtpExtension>& extensions) { |
| 2032 | config_.rtp.extensions = extensions; |
| 2033 | RecreateWebRtcStream(); |
| 2034 | } |
| 2035 | |
| 2036 | void WebRtcVideoChannel2::WebRtcVideoReceiveStream::RecreateWebRtcStream() { |
| 2037 | if (stream_ != NULL) { |
| 2038 | call_->DestroyVideoReceiveStream(stream_); |
| 2039 | } |
| 2040 | stream_ = call_->CreateVideoReceiveStream(config_); |
| 2041 | stream_->Start(); |
| 2042 | } |
| 2043 | |
pbos@webrtc.org | 96a9325 | 2014-11-03 14:46:44 +0000 | [diff] [blame] | 2044 | void WebRtcVideoChannel2::WebRtcVideoReceiveStream::ClearDecoders( |
| 2045 | std::vector<AllocatedDecoder>* allocated_decoders) { |
| 2046 | for (size_t i = 0; i < allocated_decoders->size(); ++i) { |
| 2047 | if ((*allocated_decoders)[i].external) { |
pbos@webrtc.org | 776e6f2 | 2014-10-29 15:28:39 +0000 | [diff] [blame] | 2048 | external_decoder_factory_->DestroyVideoDecoder( |
pbos@webrtc.org | 96a9325 | 2014-11-03 14:46:44 +0000 | [diff] [blame] | 2049 | (*allocated_decoders)[i].decoder); |
pbos@webrtc.org | 776e6f2 | 2014-10-29 15:28:39 +0000 | [diff] [blame] | 2050 | } else { |
pbos@webrtc.org | 96a9325 | 2014-11-03 14:46:44 +0000 | [diff] [blame] | 2051 | delete (*allocated_decoders)[i].decoder; |
pbos@webrtc.org | 776e6f2 | 2014-10-29 15:28:39 +0000 | [diff] [blame] | 2052 | } |
| 2053 | } |
pbos@webrtc.org | 96a9325 | 2014-11-03 14:46:44 +0000 | [diff] [blame] | 2054 | allocated_decoders->clear(); |
pbos@webrtc.org | 776e6f2 | 2014-10-29 15:28:39 +0000 | [diff] [blame] | 2055 | } |
| 2056 | |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 2057 | void WebRtcVideoChannel2::WebRtcVideoReceiveStream::RenderFrame( |
| 2058 | const webrtc::I420VideoFrame& frame, |
| 2059 | int time_to_render_ms) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 2060 | rtc::CritScope crit(&renderer_lock_); |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 2061 | if (renderer_ == NULL) { |
| 2062 | LOG(LS_WARNING) << "VideoReceiveStream not connected to a VideoRenderer."; |
| 2063 | return; |
| 2064 | } |
| 2065 | |
| 2066 | if (frame.width() != last_width_ || frame.height() != last_height_) { |
| 2067 | SetSize(frame.width(), frame.height()); |
| 2068 | } |
| 2069 | |
| 2070 | LOG(LS_VERBOSE) << "RenderFrame: (" << frame.width() << "x" << frame.height() |
| 2071 | << ")"; |
| 2072 | |
| 2073 | const WebRtcVideoRenderFrame render_frame(&frame); |
| 2074 | renderer_->RenderFrame(&render_frame); |
| 2075 | } |
| 2076 | |
| 2077 | void WebRtcVideoChannel2::WebRtcVideoReceiveStream::SetRenderer( |
| 2078 | cricket::VideoRenderer* renderer) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 2079 | rtc::CritScope crit(&renderer_lock_); |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 2080 | renderer_ = renderer; |
| 2081 | if (renderer_ != NULL && last_width_ != -1) { |
| 2082 | SetSize(last_width_, last_height_); |
| 2083 | } |
| 2084 | } |
| 2085 | |
| 2086 | VideoRenderer* WebRtcVideoChannel2::WebRtcVideoReceiveStream::GetRenderer() { |
| 2087 | // TODO(pbos): Remove GetRenderer and all uses of it, it's thread-unsafe by |
| 2088 | // design. |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 2089 | rtc::CritScope crit(&renderer_lock_); |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 2090 | return renderer_; |
| 2091 | } |
| 2092 | |
| 2093 | void WebRtcVideoChannel2::WebRtcVideoReceiveStream::SetSize(int width, |
| 2094 | int height) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 2095 | rtc::CritScope crit(&renderer_lock_); |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 2096 | if (!renderer_->SetSize(width, height, 0)) { |
| 2097 | LOG(LS_ERROR) << "Could not set renderer size."; |
| 2098 | } |
| 2099 | last_width_ = width; |
| 2100 | last_height_ = height; |
| 2101 | } |
| 2102 | |
pbos@webrtc.org | e6f84ae | 2014-07-18 11:11:55 +0000 | [diff] [blame] | 2103 | VideoReceiverInfo |
| 2104 | WebRtcVideoChannel2::WebRtcVideoReceiveStream::GetVideoReceiverInfo() { |
| 2105 | VideoReceiverInfo info; |
| 2106 | info.add_ssrc(config_.rtp.remote_ssrc); |
| 2107 | webrtc::VideoReceiveStream::Stats stats = stream_->GetStats(); |
| 2108 | info.bytes_rcvd = stats.rtp_stats.bytes + stats.rtp_stats.header_bytes + |
| 2109 | stats.rtp_stats.padding_bytes; |
| 2110 | info.packets_rcvd = stats.rtp_stats.packets; |
| 2111 | |
| 2112 | info.framerate_rcvd = stats.network_frame_rate; |
| 2113 | info.framerate_decoded = stats.decode_frame_rate; |
| 2114 | info.framerate_output = stats.render_frame_rate; |
| 2115 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 2116 | rtc::CritScope frame_cs(&renderer_lock_); |
pbos@webrtc.org | e6f84ae | 2014-07-18 11:11:55 +0000 | [diff] [blame] | 2117 | info.frame_width = last_width_; |
| 2118 | info.frame_height = last_height_; |
| 2119 | |
| 2120 | // TODO(pbos): Support or remove the following stats. |
| 2121 | info.packets_concealed = -1; |
| 2122 | |
| 2123 | return info; |
| 2124 | } |
| 2125 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 2126 | WebRtcVideoChannel2::VideoCodecSettings::VideoCodecSettings() |
| 2127 | : rtx_payload_type(-1) {} |
| 2128 | |
pbos@webrtc.org | a2ef4fe | 2014-11-07 10:54:43 +0000 | [diff] [blame] | 2129 | bool WebRtcVideoChannel2::VideoCodecSettings::operator==( |
| 2130 | const WebRtcVideoChannel2::VideoCodecSettings& other) const { |
| 2131 | return codec == other.codec && |
| 2132 | fec.ulpfec_payload_type == other.fec.ulpfec_payload_type && |
| 2133 | fec.red_payload_type == other.fec.red_payload_type && |
| 2134 | rtx_payload_type == other.rtx_payload_type; |
| 2135 | } |
| 2136 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 2137 | std::vector<WebRtcVideoChannel2::VideoCodecSettings> |
| 2138 | WebRtcVideoChannel2::MapCodecs(const std::vector<VideoCodec>& codecs) { |
| 2139 | assert(!codecs.empty()); |
| 2140 | |
| 2141 | std::vector<VideoCodecSettings> video_codecs; |
| 2142 | std::map<int, bool> payload_used; |
pbos@webrtc.org | e322a17 | 2014-06-13 11:47:28 +0000 | [diff] [blame] | 2143 | std::map<int, VideoCodec::CodecType> payload_codec_type; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 2144 | std::map<int, int> rtx_mapping; // video payload type -> rtx payload type. |
| 2145 | |
| 2146 | webrtc::FecConfig fec_settings; |
| 2147 | |
| 2148 | for (size_t i = 0; i < codecs.size(); ++i) { |
| 2149 | const VideoCodec& in_codec = codecs[i]; |
| 2150 | int payload_type = in_codec.id; |
| 2151 | |
| 2152 | if (payload_used[payload_type]) { |
| 2153 | LOG(LS_ERROR) << "Payload type already registered: " |
| 2154 | << in_codec.ToString(); |
| 2155 | return std::vector<VideoCodecSettings>(); |
| 2156 | } |
| 2157 | payload_used[payload_type] = true; |
pbos@webrtc.org | e322a17 | 2014-06-13 11:47:28 +0000 | [diff] [blame] | 2158 | payload_codec_type[payload_type] = in_codec.GetCodecType(); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 2159 | |
| 2160 | switch (in_codec.GetCodecType()) { |
| 2161 | case VideoCodec::CODEC_RED: { |
| 2162 | // RED payload type, should not have duplicates. |
| 2163 | assert(fec_settings.red_payload_type == -1); |
| 2164 | fec_settings.red_payload_type = in_codec.id; |
| 2165 | continue; |
| 2166 | } |
| 2167 | |
| 2168 | case VideoCodec::CODEC_ULPFEC: { |
| 2169 | // ULPFEC payload type, should not have duplicates. |
| 2170 | assert(fec_settings.ulpfec_payload_type == -1); |
| 2171 | fec_settings.ulpfec_payload_type = in_codec.id; |
| 2172 | continue; |
| 2173 | } |
| 2174 | |
| 2175 | case VideoCodec::CODEC_RTX: { |
| 2176 | int associated_payload_type; |
| 2177 | if (!in_codec.GetParam(kCodecParamAssociatedPayloadType, |
| 2178 | &associated_payload_type)) { |
| 2179 | LOG(LS_ERROR) << "RTX codec without associated payload type: " |
| 2180 | << in_codec.ToString(); |
| 2181 | return std::vector<VideoCodecSettings>(); |
| 2182 | } |
| 2183 | rtx_mapping[associated_payload_type] = in_codec.id; |
| 2184 | continue; |
| 2185 | } |
| 2186 | |
| 2187 | case VideoCodec::CODEC_VIDEO: |
| 2188 | break; |
| 2189 | } |
| 2190 | |
| 2191 | video_codecs.push_back(VideoCodecSettings()); |
| 2192 | video_codecs.back().codec = in_codec; |
| 2193 | } |
| 2194 | |
| 2195 | // One of these codecs should have been a video codec. Only having FEC |
| 2196 | // parameters into this code is a logic error. |
| 2197 | assert(!video_codecs.empty()); |
| 2198 | |
pbos@webrtc.org | e322a17 | 2014-06-13 11:47:28 +0000 | [diff] [blame] | 2199 | for (std::map<int, int>::const_iterator it = rtx_mapping.begin(); |
| 2200 | it != rtx_mapping.end(); |
| 2201 | ++it) { |
| 2202 | if (!payload_used[it->first]) { |
| 2203 | LOG(LS_ERROR) << "RTX mapped to payload not in codec list."; |
| 2204 | return std::vector<VideoCodecSettings>(); |
| 2205 | } |
| 2206 | if (payload_codec_type[it->first] != VideoCodec::CODEC_VIDEO) { |
| 2207 | LOG(LS_ERROR) << "RTX not mapped to regular video codec."; |
| 2208 | return std::vector<VideoCodecSettings>(); |
| 2209 | } |
| 2210 | } |
| 2211 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 2212 | // TODO(pbos): Write tests that figure out that I have not verified that RTX |
| 2213 | // codecs aren't mapped to bogus payloads. |
| 2214 | for (size_t i = 0; i < video_codecs.size(); ++i) { |
| 2215 | video_codecs[i].fec = fec_settings; |
| 2216 | if (rtx_mapping[video_codecs[i].codec.id] != 0) { |
| 2217 | video_codecs[i].rtx_payload_type = rtx_mapping[video_codecs[i].codec.id]; |
| 2218 | } |
| 2219 | } |
| 2220 | |
| 2221 | return video_codecs; |
| 2222 | } |
| 2223 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 2224 | } // namespace cricket |
| 2225 | |
| 2226 | #endif // HAVE_WEBRTC_VIDEO |