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