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