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