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