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