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