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