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