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