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( |
pbos@webrtc.org | a2a6fe6 | 2015-03-06 15:35:19 +0000 | [diff] [blame] | 279 | WebRtcVideoChannel2* channel, |
pbos@webrtc.org | afb554f4 | 2014-08-12 23:17:13 +0000 | [diff] [blame] | 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 << "."; |
pbos@webrtc.org | a2a6fe6 | 2015-03-06 15:35:19 +0000 | [diff] [blame] | 289 | if (!channel->AddRecvStream(sp, true)) { |
pbos@webrtc.org | afb554f4 | 2014-08-12 23:17:13 +0000 | [diff] [blame] | 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()) { |
pbos@webrtc.org | a2a6fe6 | 2015-03-06 15:35:19 +0000 | [diff] [blame] | 804 | LOG(LS_ERROR) << "Send stream with SSRC '" << ssrc << "' already exists."; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 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) { |
pbos@webrtc.org | a2a6fe6 | 2015-03-06 15:35:19 +0000 | [diff] [blame] | 878 | return AddRecvStream(sp, false); |
| 879 | } |
| 880 | |
| 881 | bool WebRtcVideoChannel2::AddRecvStream(const StreamParams& sp, |
| 882 | bool default_stream) { |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 883 | LOG(LS_INFO) << "AddRecvStream: " << sp.ToString(); |
| 884 | assert(sp.ssrcs.size() > 0); |
| 885 | |
| 886 | uint32 ssrc = sp.first_ssrc(); |
| 887 | assert(ssrc != 0); // TODO(pbos): Is this ever valid? |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 888 | |
| 889 | // TODO(pbos): Check if any of the SSRCs overlap. |
pbos@webrtc.org | 575d126 | 2014-10-08 14:48:08 +0000 | [diff] [blame] | 890 | rtc::CritScope stream_lock(&stream_crit_); |
pbos@webrtc.org | a2a6fe6 | 2015-03-06 15:35:19 +0000 | [diff] [blame] | 891 | { |
| 892 | auto it = receive_streams_.find(ssrc); |
| 893 | if (it != receive_streams_.end()) { |
| 894 | if (default_stream || !it->second->IsDefaultStream()) { |
| 895 | LOG(LS_ERROR) << "Receive stream for SSRC '" << ssrc |
| 896 | << "' already exists."; |
| 897 | return false; |
| 898 | } |
| 899 | delete it->second; |
| 900 | receive_streams_.erase(it); |
| 901 | } |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 902 | } |
| 903 | |
pbos@webrtc.org | bd249bc | 2014-07-07 04:45:15 +0000 | [diff] [blame] | 904 | webrtc::VideoReceiveStream::Config config; |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 905 | ConfigureReceiverRtp(&config, sp); |
pbos@webrtc.org | 3bf3d23 | 2014-10-31 12:59:34 +0000 | [diff] [blame] | 906 | |
| 907 | // Set up A/V sync if there is a VoiceChannel. |
| 908 | // TODO(pbos): The A/V is synched by the receiving channel. So we need to know |
| 909 | // the SSRC of the remote audio channel in order to sync the correct webrtc |
| 910 | // VoiceEngine channel. For now sync the first channel in non-conference to |
| 911 | // match existing behavior in WebRtcVideoEngine. |
| 912 | if (voice_channel_ != NULL && receive_streams_.empty() && |
| 913 | !options_.conference_mode.GetWithDefaultIfUnset(false)) { |
| 914 | config.audio_channel_id = |
| 915 | static_cast<WebRtcVoiceMediaChannel*>(voice_channel_)->voe_channel(); |
| 916 | } |
| 917 | |
pbos@webrtc.org | a2a6fe6 | 2015-03-06 15:35:19 +0000 | [diff] [blame] | 918 | receive_streams_[ssrc] = |
| 919 | new WebRtcVideoReceiveStream(call_.get(), external_decoder_factory_, |
| 920 | default_stream, config, recv_codecs_); |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 921 | |
| 922 | return true; |
| 923 | } |
| 924 | |
| 925 | void WebRtcVideoChannel2::ConfigureReceiverRtp( |
| 926 | webrtc::VideoReceiveStream::Config* config, |
| 927 | const StreamParams& sp) const { |
| 928 | uint32 ssrc = sp.first_ssrc(); |
| 929 | |
| 930 | config->rtp.remote_ssrc = ssrc; |
| 931 | config->rtp.local_ssrc = rtcp_receiver_report_ssrc_; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 932 | |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 933 | config->rtp.extensions = recv_rtp_extensions_; |
pbos@webrtc.org | 257e130 | 2014-07-25 19:01:32 +0000 | [diff] [blame] | 934 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 935 | // TODO(pbos): This protection is against setting the same local ssrc as |
| 936 | // remote which is not permitted by the lower-level API. RTCP requires a |
| 937 | // corresponding sender SSRC. Figure out what to do when we don't have |
| 938 | // (receive-only) or know a good local SSRC. |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 939 | if (config->rtp.remote_ssrc == config->rtp.local_ssrc) { |
| 940 | if (config->rtp.local_ssrc != kDefaultRtcpReceiverReportSsrc) { |
| 941 | config->rtp.local_ssrc = kDefaultRtcpReceiverReportSsrc; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 942 | } else { |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 943 | config->rtp.local_ssrc = kDefaultRtcpReceiverReportSsrc + 1; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 944 | } |
| 945 | } |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 946 | |
| 947 | for (size_t i = 0; i < recv_codecs_.size(); ++i) { |
andresp@webrtc.org | 82775b1 | 2014-11-07 09:37:54 +0000 | [diff] [blame] | 948 | MergeFecConfig(recv_codecs_[i].fec, &config->rtp.fec); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 949 | } |
| 950 | |
andresp@webrtc.org | 82775b1 | 2014-11-07 09:37:54 +0000 | [diff] [blame] | 951 | for (size_t i = 0; i < recv_codecs_.size(); ++i) { |
| 952 | uint32 rtx_ssrc; |
| 953 | if (recv_codecs_[i].rtx_payload_type != -1 && |
| 954 | sp.GetFidSsrc(ssrc, &rtx_ssrc)) { |
| 955 | webrtc::VideoReceiveStream::Config::Rtp::Rtx& rtx = |
| 956 | config->rtp.rtx[recv_codecs_[i].codec.id]; |
| 957 | rtx.ssrc = rtx_ssrc; |
| 958 | rtx.payload_type = recv_codecs_[i].rtx_payload_type; |
| 959 | } |
| 960 | } |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 961 | } |
| 962 | |
| 963 | bool WebRtcVideoChannel2::RemoveRecvStream(uint32 ssrc) { |
| 964 | LOG(LS_INFO) << "RemoveRecvStream: " << ssrc; |
| 965 | if (ssrc == 0) { |
pbos@webrtc.org | afb554f4 | 2014-08-12 23:17:13 +0000 | [diff] [blame] | 966 | LOG(LS_ERROR) << "RemoveRecvStream with 0 ssrc is not supported."; |
| 967 | return false; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 968 | } |
| 969 | |
pbos@webrtc.org | 575d126 | 2014-10-08 14:48:08 +0000 | [diff] [blame] | 970 | rtc::CritScope stream_lock(&stream_crit_); |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 971 | std::map<uint32, WebRtcVideoReceiveStream*>::iterator stream = |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 972 | receive_streams_.find(ssrc); |
| 973 | if (stream == receive_streams_.end()) { |
| 974 | LOG(LS_ERROR) << "Stream not found for ssrc: " << ssrc; |
| 975 | return false; |
| 976 | } |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 977 | delete stream->second; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 978 | receive_streams_.erase(stream); |
| 979 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 980 | return true; |
| 981 | } |
| 982 | |
| 983 | bool WebRtcVideoChannel2::SetRenderer(uint32 ssrc, VideoRenderer* renderer) { |
| 984 | LOG(LS_INFO) << "SetRenderer: ssrc:" << ssrc << " " |
| 985 | << (renderer ? "(ptr)" : "NULL"); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 986 | if (ssrc == 0) { |
pbos@webrtc.org | afb554f4 | 2014-08-12 23:17:13 +0000 | [diff] [blame] | 987 | default_unsignalled_ssrc_handler_.SetDefaultRenderer(this, renderer); |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 988 | return true; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 989 | } |
| 990 | |
pbos@webrtc.org | 575d126 | 2014-10-08 14:48:08 +0000 | [diff] [blame] | 991 | rtc::CritScope stream_lock(&stream_crit_); |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 992 | std::map<uint32, WebRtcVideoReceiveStream*>::iterator it = |
| 993 | receive_streams_.find(ssrc); |
| 994 | if (it == receive_streams_.end()) { |
| 995 | return false; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 996 | } |
| 997 | |
| 998 | it->second->SetRenderer(renderer); |
| 999 | return true; |
| 1000 | } |
| 1001 | |
| 1002 | bool WebRtcVideoChannel2::GetRenderer(uint32 ssrc, VideoRenderer** renderer) { |
| 1003 | if (ssrc == 0) { |
pbos@webrtc.org | afb554f4 | 2014-08-12 23:17:13 +0000 | [diff] [blame] | 1004 | *renderer = default_unsignalled_ssrc_handler_.GetDefaultRenderer(); |
| 1005 | return *renderer != NULL; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1006 | } |
| 1007 | |
pbos@webrtc.org | 575d126 | 2014-10-08 14:48:08 +0000 | [diff] [blame] | 1008 | rtc::CritScope stream_lock(&stream_crit_); |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 1009 | std::map<uint32, WebRtcVideoReceiveStream*>::iterator it = |
| 1010 | receive_streams_.find(ssrc); |
| 1011 | if (it == receive_streams_.end()) { |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1012 | return false; |
| 1013 | } |
| 1014 | *renderer = it->second->GetRenderer(); |
| 1015 | return true; |
| 1016 | } |
| 1017 | |
pbos@webrtc.org | 058b1f1 | 2015-03-04 08:54:32 +0000 | [diff] [blame] | 1018 | bool WebRtcVideoChannel2::GetStats(VideoMediaInfo* info) { |
pbos@webrtc.org | e6f84ae | 2014-07-18 11:11:55 +0000 | [diff] [blame] | 1019 | info->Clear(); |
| 1020 | FillSenderStats(info); |
| 1021 | FillReceiverStats(info); |
pbos@webrtc.org | 2b19f06 | 2014-12-11 13:26:09 +0000 | [diff] [blame] | 1022 | webrtc::Call::Stats stats = call_->GetStats(); |
| 1023 | FillBandwidthEstimationStats(stats, info); |
| 1024 | if (stats.rtt_ms != -1) { |
| 1025 | for (size_t i = 0; i < info->senders.size(); ++i) { |
| 1026 | info->senders[i].rtt_ms = stats.rtt_ms; |
| 1027 | } |
| 1028 | } |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1029 | return true; |
| 1030 | } |
| 1031 | |
pbos@webrtc.org | e6f84ae | 2014-07-18 11:11:55 +0000 | [diff] [blame] | 1032 | void WebRtcVideoChannel2::FillSenderStats(VideoMediaInfo* video_media_info) { |
pbos@webrtc.org | 575d126 | 2014-10-08 14:48:08 +0000 | [diff] [blame] | 1033 | rtc::CritScope stream_lock(&stream_crit_); |
pbos@webrtc.org | e6f84ae | 2014-07-18 11:11:55 +0000 | [diff] [blame] | 1034 | for (std::map<uint32, WebRtcVideoSendStream*>::iterator it = |
| 1035 | send_streams_.begin(); |
| 1036 | it != send_streams_.end(); |
| 1037 | ++it) { |
| 1038 | video_media_info->senders.push_back(it->second->GetVideoSenderInfo()); |
| 1039 | } |
| 1040 | } |
| 1041 | |
| 1042 | void WebRtcVideoChannel2::FillReceiverStats(VideoMediaInfo* video_media_info) { |
pbos@webrtc.org | 575d126 | 2014-10-08 14:48:08 +0000 | [diff] [blame] | 1043 | rtc::CritScope stream_lock(&stream_crit_); |
pbos@webrtc.org | e6f84ae | 2014-07-18 11:11:55 +0000 | [diff] [blame] | 1044 | for (std::map<uint32, WebRtcVideoReceiveStream*>::iterator it = |
| 1045 | receive_streams_.begin(); |
| 1046 | it != receive_streams_.end(); |
| 1047 | ++it) { |
| 1048 | video_media_info->receivers.push_back(it->second->GetVideoReceiverInfo()); |
| 1049 | } |
| 1050 | } |
| 1051 | |
| 1052 | void WebRtcVideoChannel2::FillBandwidthEstimationStats( |
pbos@webrtc.org | 2b19f06 | 2014-12-11 13:26:09 +0000 | [diff] [blame] | 1053 | const webrtc::Call::Stats& stats, |
pbos@webrtc.org | e6f84ae | 2014-07-18 11:11:55 +0000 | [diff] [blame] | 1054 | VideoMediaInfo* video_media_info) { |
stefan@webrtc.org | 0bae1fa | 2014-11-05 14:05:29 +0000 | [diff] [blame] | 1055 | BandwidthEstimationInfo bwe_info; |
stefan@webrtc.org | 0bae1fa | 2014-11-05 14:05:29 +0000 | [diff] [blame] | 1056 | bwe_info.available_send_bandwidth = stats.send_bandwidth_bps; |
| 1057 | bwe_info.available_recv_bandwidth = stats.recv_bandwidth_bps; |
| 1058 | bwe_info.bucket_delay = stats.pacer_delay_ms; |
| 1059 | |
| 1060 | // Get send stream bitrate stats. |
| 1061 | rtc::CritScope stream_lock(&stream_crit_); |
| 1062 | for (std::map<uint32, WebRtcVideoSendStream*>::iterator stream = |
| 1063 | send_streams_.begin(); |
| 1064 | stream != send_streams_.end(); |
| 1065 | ++stream) { |
| 1066 | stream->second->FillBandwidthEstimationInfo(&bwe_info); |
| 1067 | } |
| 1068 | video_media_info->bw_estimations.push_back(bwe_info); |
pbos@webrtc.org | e6f84ae | 2014-07-18 11:11:55 +0000 | [diff] [blame] | 1069 | } |
| 1070 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1071 | bool WebRtcVideoChannel2::SetCapturer(uint32 ssrc, VideoCapturer* capturer) { |
| 1072 | LOG(LS_INFO) << "SetCapturer: " << ssrc << " -> " |
| 1073 | << (capturer != NULL ? "(capturer)" : "NULL"); |
| 1074 | assert(ssrc != 0); |
pbos@webrtc.org | 575d126 | 2014-10-08 14:48:08 +0000 | [diff] [blame] | 1075 | rtc::CritScope stream_lock(&stream_crit_); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1076 | if (send_streams_.find(ssrc) == send_streams_.end()) { |
| 1077 | LOG(LS_ERROR) << "No sending stream on ssrc " << ssrc; |
| 1078 | return false; |
| 1079 | } |
| 1080 | return send_streams_[ssrc]->SetCapturer(capturer); |
| 1081 | } |
| 1082 | |
| 1083 | bool WebRtcVideoChannel2::SendIntraFrame() { |
| 1084 | // TODO(pbos): Implement. |
| 1085 | LOG(LS_VERBOSE) << "SendIntraFrame()."; |
| 1086 | return true; |
| 1087 | } |
| 1088 | |
| 1089 | bool WebRtcVideoChannel2::RequestIntraFrame() { |
| 1090 | // TODO(pbos): Implement. |
| 1091 | LOG(LS_VERBOSE) << "SendIntraFrame()."; |
| 1092 | return true; |
| 1093 | } |
| 1094 | |
| 1095 | void WebRtcVideoChannel2::OnPacketReceived( |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1096 | rtc::Buffer* packet, |
| 1097 | const rtc::PacketTime& packet_time) { |
pbos@webrtc.org | 4e545cc | 2014-05-14 13:58:13 +0000 | [diff] [blame] | 1098 | const webrtc::PacketReceiver::DeliveryStatus delivery_result = |
| 1099 | call_->Receiver()->DeliverPacket( |
| 1100 | reinterpret_cast<const uint8_t*>(packet->data()), packet->length()); |
| 1101 | switch (delivery_result) { |
| 1102 | case webrtc::PacketReceiver::DELIVERY_OK: |
| 1103 | return; |
| 1104 | case webrtc::PacketReceiver::DELIVERY_PACKET_ERROR: |
| 1105 | return; |
| 1106 | case webrtc::PacketReceiver::DELIVERY_UNKNOWN_SSRC: |
| 1107 | break; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1108 | } |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1109 | |
| 1110 | uint32 ssrc = 0; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1111 | if (!GetRtpSsrc(packet->data(), packet->length(), &ssrc)) { |
| 1112 | return; |
| 1113 | } |
| 1114 | |
pbos@webrtc.org | a2a6fe6 | 2015-03-06 15:35:19 +0000 | [diff] [blame] | 1115 | // TODO(pbos): Ignore unsignalled packets that don't use the video payload |
| 1116 | // (prevent creating default receivers for RTX configured as if it would |
| 1117 | // receive media payloads on those SSRCs). |
pbos@webrtc.org | afb554f4 | 2014-08-12 23:17:13 +0000 | [diff] [blame] | 1118 | switch (unsignalled_ssrc_handler_->OnUnsignalledSsrc(this, ssrc)) { |
| 1119 | case UnsignalledSsrcHandler::kDropPacket: |
| 1120 | return; |
| 1121 | case UnsignalledSsrcHandler::kDeliverPacket: |
| 1122 | break; |
| 1123 | } |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1124 | |
pbos@webrtc.org | 1e019d1 | 2014-05-16 11:38:45 +0000 | [diff] [blame] | 1125 | if (call_->Receiver()->DeliverPacket( |
| 1126 | reinterpret_cast<const uint8_t*>(packet->data()), packet->length()) != |
| 1127 | webrtc::PacketReceiver::DELIVERY_OK) { |
pbos@webrtc.org | afb554f4 | 2014-08-12 23:17:13 +0000 | [diff] [blame] | 1128 | LOG(LS_WARNING) << "Failed to deliver RTP packet on re-delivery."; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1129 | return; |
| 1130 | } |
| 1131 | } |
| 1132 | |
| 1133 | void WebRtcVideoChannel2::OnRtcpReceived( |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1134 | rtc::Buffer* packet, |
| 1135 | const rtc::PacketTime& packet_time) { |
pbos@webrtc.org | 1e019d1 | 2014-05-16 11:38:45 +0000 | [diff] [blame] | 1136 | if (call_->Receiver()->DeliverPacket( |
| 1137 | reinterpret_cast<const uint8_t*>(packet->data()), packet->length()) != |
| 1138 | webrtc::PacketReceiver::DELIVERY_OK) { |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1139 | LOG(LS_WARNING) << "Failed to deliver RTCP packet."; |
| 1140 | } |
| 1141 | } |
| 1142 | |
| 1143 | void WebRtcVideoChannel2::OnReadyToSend(bool ready) { |
pbos@webrtc.org | 26c0c41 | 2014-09-03 16:17:12 +0000 | [diff] [blame] | 1144 | LOG(LS_VERBOSE) << "OnReadyToSend: " << (ready ? "Ready." : "Not ready."); |
| 1145 | call_->SignalNetworkState(ready ? webrtc::Call::kNetworkUp |
| 1146 | : webrtc::Call::kNetworkDown); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1147 | } |
| 1148 | |
| 1149 | bool WebRtcVideoChannel2::MuteStream(uint32 ssrc, bool mute) { |
| 1150 | LOG(LS_VERBOSE) << "MuteStream: " << ssrc << " -> " |
| 1151 | << (mute ? "mute" : "unmute"); |
| 1152 | assert(ssrc != 0); |
pbos@webrtc.org | 575d126 | 2014-10-08 14:48:08 +0000 | [diff] [blame] | 1153 | rtc::CritScope stream_lock(&stream_crit_); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1154 | if (send_streams_.find(ssrc) == send_streams_.end()) { |
| 1155 | LOG(LS_ERROR) << "No sending stream on ssrc " << ssrc; |
| 1156 | return false; |
| 1157 | } |
pbos@webrtc.org | ef8bb8d | 2014-08-13 21:36:18 +0000 | [diff] [blame] | 1158 | |
| 1159 | send_streams_[ssrc]->MuteStream(mute); |
| 1160 | return true; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1161 | } |
| 1162 | |
| 1163 | bool WebRtcVideoChannel2::SetRecvRtpHeaderExtensions( |
| 1164 | const std::vector<RtpHeaderExtension>& extensions) { |
pbos@webrtc.org | 50fe359 | 2015-01-29 12:33:07 +0000 | [diff] [blame] | 1165 | TRACE_EVENT0("webrtc", "WebRtcVideoChannel2::SetRecvRtpHeaderExtensions"); |
pbos@webrtc.org | 587ef60 | 2014-06-16 17:32:02 +0000 | [diff] [blame] | 1166 | LOG(LS_INFO) << "SetRecvRtpHeaderExtensions: " |
| 1167 | << RtpExtensionsToString(extensions); |
pbos@webrtc.org | 3c10758 | 2014-07-20 15:27:35 +0000 | [diff] [blame] | 1168 | if (!ValidateRtpHeaderExtensionIds(extensions)) |
| 1169 | return false; |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 1170 | |
pbos@webrtc.org | c37e72e | 2015-01-05 18:51:13 +0000 | [diff] [blame] | 1171 | std::vector<webrtc::RtpExtension> filtered_extensions = |
| 1172 | FilterRtpExtensions(extensions); |
| 1173 | if (!RtpExtensionsHaveChanged(recv_rtp_extensions_, filtered_extensions)) |
| 1174 | return true; |
| 1175 | |
| 1176 | recv_rtp_extensions_ = filtered_extensions; |
| 1177 | |
pbos@webrtc.org | 575d126 | 2014-10-08 14:48:08 +0000 | [diff] [blame] | 1178 | rtc::CritScope stream_lock(&stream_crit_); |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 1179 | for (std::map<uint32, WebRtcVideoReceiveStream*>::iterator it = |
| 1180 | receive_streams_.begin(); |
| 1181 | it != receive_streams_.end(); |
| 1182 | ++it) { |
| 1183 | it->second->SetRtpExtensions(recv_rtp_extensions_); |
| 1184 | } |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1185 | return true; |
| 1186 | } |
| 1187 | |
| 1188 | bool WebRtcVideoChannel2::SetSendRtpHeaderExtensions( |
| 1189 | const std::vector<RtpHeaderExtension>& extensions) { |
pbos@webrtc.org | 50fe359 | 2015-01-29 12:33:07 +0000 | [diff] [blame] | 1190 | TRACE_EVENT0("webrtc", "WebRtcVideoChannel2::SetSendRtpHeaderExtensions"); |
pbos@webrtc.org | 587ef60 | 2014-06-16 17:32:02 +0000 | [diff] [blame] | 1191 | LOG(LS_INFO) << "SetSendRtpHeaderExtensions: " |
| 1192 | << RtpExtensionsToString(extensions); |
pbos@webrtc.org | 3c10758 | 2014-07-20 15:27:35 +0000 | [diff] [blame] | 1193 | if (!ValidateRtpHeaderExtensionIds(extensions)) |
| 1194 | return false; |
| 1195 | |
pbos@webrtc.org | c37e72e | 2015-01-05 18:51:13 +0000 | [diff] [blame] | 1196 | std::vector<webrtc::RtpExtension> filtered_extensions = |
| 1197 | FilterRtpExtensions(extensions); |
| 1198 | if (!RtpExtensionsHaveChanged(send_rtp_extensions_, filtered_extensions)) |
| 1199 | return true; |
| 1200 | |
| 1201 | send_rtp_extensions_ = filtered_extensions; |
pbos@webrtc.org | a2ef4fe | 2014-11-07 10:54:43 +0000 | [diff] [blame] | 1202 | |
pbos@webrtc.org | 575d126 | 2014-10-08 14:48:08 +0000 | [diff] [blame] | 1203 | rtc::CritScope stream_lock(&stream_crit_); |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 1204 | for (std::map<uint32, WebRtcVideoSendStream*>::iterator it = |
| 1205 | send_streams_.begin(); |
| 1206 | it != send_streams_.end(); |
| 1207 | ++it) { |
| 1208 | it->second->SetRtpExtensions(send_rtp_extensions_); |
| 1209 | } |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1210 | return true; |
| 1211 | } |
| 1212 | |
pbos@webrtc.org | 0087318 | 2014-11-25 14:03:34 +0000 | [diff] [blame] | 1213 | bool WebRtcVideoChannel2::SetMaxSendBandwidth(int max_bitrate_bps) { |
| 1214 | LOG(LS_INFO) << "SetMaxSendBandwidth: " << max_bitrate_bps << "bps."; |
| 1215 | if (max_bitrate_bps <= 0) { |
| 1216 | // Unsetting max bitrate. |
| 1217 | max_bitrate_bps = -1; |
| 1218 | } |
| 1219 | bitrate_config_.start_bitrate_bps = -1; |
| 1220 | bitrate_config_.max_bitrate_bps = max_bitrate_bps; |
| 1221 | if (max_bitrate_bps > 0 && |
| 1222 | bitrate_config_.min_bitrate_bps > max_bitrate_bps) { |
| 1223 | bitrate_config_.min_bitrate_bps = max_bitrate_bps; |
| 1224 | } |
| 1225 | call_->SetBitrateConfig(bitrate_config_); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1226 | return true; |
| 1227 | } |
| 1228 | |
| 1229 | bool WebRtcVideoChannel2::SetOptions(const VideoOptions& options) { |
pbos@webrtc.org | 50fe359 | 2015-01-29 12:33:07 +0000 | [diff] [blame] | 1230 | TRACE_EVENT0("webrtc", "WebRtcVideoChannel2::SetOptions"); |
pbos@webrtc.org | a2ef4fe | 2014-11-07 10:54:43 +0000 | [diff] [blame] | 1231 | LOG(LS_INFO) << "SetOptions: " << options.ToString(); |
| 1232 | VideoOptions old_options = options_; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1233 | options_.SetAll(options); |
pbos@webrtc.org | a2ef4fe | 2014-11-07 10:54:43 +0000 | [diff] [blame] | 1234 | if (options_ == old_options) { |
| 1235 | // No new options to set. |
| 1236 | return true; |
| 1237 | } |
pbos@webrtc.org | d819803 | 2014-11-10 14:41:43 +0000 | [diff] [blame] | 1238 | rtc::DiffServCodePoint dscp = options_.dscp.GetWithDefaultIfUnset(false) |
| 1239 | ? rtc::DSCP_AF41 |
| 1240 | : rtc::DSCP_DEFAULT; |
| 1241 | MediaChannel::SetDscp(dscp); |
pbos@webrtc.org | 575d126 | 2014-10-08 14:48:08 +0000 | [diff] [blame] | 1242 | rtc::CritScope stream_lock(&stream_crit_); |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1243 | for (std::map<uint32, WebRtcVideoSendStream*>::iterator it = |
| 1244 | send_streams_.begin(); |
| 1245 | it != send_streams_.end(); |
| 1246 | ++it) { |
| 1247 | it->second->SetOptions(options_); |
| 1248 | } |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1249 | return true; |
| 1250 | } |
| 1251 | |
| 1252 | void WebRtcVideoChannel2::SetInterface(NetworkInterface* iface) { |
| 1253 | MediaChannel::SetInterface(iface); |
| 1254 | // Set the RTP recv/send buffer to a bigger size |
| 1255 | MediaChannel::SetOption(NetworkInterface::ST_RTP, |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1256 | rtc::Socket::OPT_RCVBUF, |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1257 | kVideoRtpBufferSize); |
| 1258 | |
buildbot@webrtc.org | ae694ef | 2014-10-28 17:37:17 +0000 | [diff] [blame] | 1259 | // Speculative change to increase the outbound socket buffer size. |
| 1260 | // In b/15152257, we are seeing a significant number of packets discarded |
| 1261 | // due to lack of socket buffer space, although it's not yet clear what the |
| 1262 | // ideal value should be. |
| 1263 | MediaChannel::SetOption(NetworkInterface::ST_RTP, |
| 1264 | rtc::Socket::OPT_SNDBUF, |
| 1265 | kVideoRtpBufferSize); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1266 | } |
| 1267 | |
| 1268 | void WebRtcVideoChannel2::UpdateAspectRatio(int ratio_w, int ratio_h) { |
| 1269 | // TODO(pbos): Implement. |
| 1270 | } |
| 1271 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1272 | void WebRtcVideoChannel2::OnMessage(rtc::Message* msg) { |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1273 | // Ignored. |
| 1274 | } |
| 1275 | |
pbos@webrtc.org | 42684be | 2014-10-03 11:25:45 +0000 | [diff] [blame] | 1276 | void WebRtcVideoChannel2::OnLoadUpdate(Load load) { |
pbos@webrtc.org | 575d126 | 2014-10-08 14:48:08 +0000 | [diff] [blame] | 1277 | rtc::CritScope stream_lock(&stream_crit_); |
pbos@webrtc.org | 42684be | 2014-10-03 11:25:45 +0000 | [diff] [blame] | 1278 | for (std::map<uint32, WebRtcVideoSendStream*>::iterator it = |
| 1279 | send_streams_.begin(); |
| 1280 | it != send_streams_.end(); |
| 1281 | ++it) { |
| 1282 | it->second->OnCpuResolutionRequest(load == kOveruse |
| 1283 | ? CoordinatedVideoAdapter::DOWNGRADE |
| 1284 | : CoordinatedVideoAdapter::UPGRADE); |
| 1285 | } |
| 1286 | } |
| 1287 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1288 | bool WebRtcVideoChannel2::SendRtp(const uint8_t* data, size_t len) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1289 | rtc::Buffer packet(data, len, kMaxRtpPacketLen); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1290 | return MediaChannel::SendPacket(&packet); |
| 1291 | } |
| 1292 | |
| 1293 | bool WebRtcVideoChannel2::SendRtcp(const uint8_t* data, size_t len) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1294 | rtc::Buffer packet(data, len, kMaxRtpPacketLen); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1295 | return MediaChannel::SendRtcp(&packet); |
| 1296 | } |
| 1297 | |
| 1298 | void WebRtcVideoChannel2::StartAllSendStreams() { |
pbos@webrtc.org | 575d126 | 2014-10-08 14:48:08 +0000 | [diff] [blame] | 1299 | rtc::CritScope stream_lock(&stream_crit_); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1300 | for (std::map<uint32, WebRtcVideoSendStream*>::iterator it = |
| 1301 | send_streams_.begin(); |
| 1302 | it != send_streams_.end(); |
| 1303 | ++it) { |
| 1304 | it->second->Start(); |
| 1305 | } |
| 1306 | } |
| 1307 | |
| 1308 | void WebRtcVideoChannel2::StopAllSendStreams() { |
pbos@webrtc.org | 575d126 | 2014-10-08 14:48:08 +0000 | [diff] [blame] | 1309 | rtc::CritScope stream_lock(&stream_crit_); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1310 | for (std::map<uint32, WebRtcVideoSendStream*>::iterator it = |
| 1311 | send_streams_.begin(); |
| 1312 | it != send_streams_.end(); |
| 1313 | ++it) { |
| 1314 | it->second->Stop(); |
| 1315 | } |
| 1316 | } |
| 1317 | |
pbos@webrtc.org | 6ae48c6 | 2014-06-06 10:49:19 +0000 | [diff] [blame] | 1318 | WebRtcVideoChannel2::WebRtcVideoSendStream::VideoSendStreamParameters:: |
| 1319 | VideoSendStreamParameters( |
| 1320 | const webrtc::VideoSendStream::Config& config, |
| 1321 | const VideoOptions& options, |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1322 | const Settable<VideoCodecSettings>& codec_settings) |
| 1323 | : config(config), options(options), codec_settings(codec_settings) { |
pbos@webrtc.org | 6ae48c6 | 2014-06-06 10:49:19 +0000 | [diff] [blame] | 1324 | } |
| 1325 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1326 | WebRtcVideoChannel2::WebRtcVideoSendStream::WebRtcVideoSendStream( |
| 1327 | webrtc::Call* call, |
pbos@webrtc.org | 7fe1e03 | 2014-10-14 04:25:33 +0000 | [diff] [blame] | 1328 | WebRtcVideoEncoderFactory* external_encoder_factory, |
pbos@webrtc.org | 6ae48c6 | 2014-06-06 10:49:19 +0000 | [diff] [blame] | 1329 | const VideoOptions& options, |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1330 | const Settable<VideoCodecSettings>& codec_settings, |
| 1331 | const StreamParams& sp, |
| 1332 | const std::vector<webrtc::RtpExtension>& rtp_extensions) |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1333 | : call_(call), |
pbos@webrtc.org | 7fe1e03 | 2014-10-14 04:25:33 +0000 | [diff] [blame] | 1334 | external_encoder_factory_(external_encoder_factory), |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1335 | stream_(NULL), |
pbos@webrtc.org | b648b9d | 2014-08-26 11:08:06 +0000 | [diff] [blame] | 1336 | parameters_(webrtc::VideoSendStream::Config(), options, codec_settings), |
pbos@webrtc.org | 7fe1e03 | 2014-10-14 04:25:33 +0000 | [diff] [blame] | 1337 | allocated_encoder_(NULL, webrtc::kVideoCodecUnknown, false), |
pbos@webrtc.org | b648b9d | 2014-08-26 11:08:06 +0000 | [diff] [blame] | 1338 | capturer_(NULL), |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1339 | sending_(false), |
pbos@webrtc.org | 9a4410e | 2015-02-26 10:03:39 +0000 | [diff] [blame] | 1340 | muted_(false), |
| 1341 | old_adapt_changes_(0) { |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1342 | parameters_.config.rtp.max_packet_size = kVideoMtu; |
| 1343 | |
| 1344 | sp.GetPrimarySsrcs(¶meters_.config.rtp.ssrcs); |
| 1345 | sp.GetFidSsrcs(parameters_.config.rtp.ssrcs, |
| 1346 | ¶meters_.config.rtp.rtx.ssrcs); |
| 1347 | parameters_.config.rtp.c_name = sp.cname; |
| 1348 | parameters_.config.rtp.extensions = rtp_extensions; |
| 1349 | |
| 1350 | VideoCodecSettings params; |
| 1351 | if (codec_settings.Get(¶ms)) { |
| 1352 | SetCodec(params); |
| 1353 | } |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1354 | } |
| 1355 | |
| 1356 | WebRtcVideoChannel2::WebRtcVideoSendStream::~WebRtcVideoSendStream() { |
| 1357 | DisconnectCapturer(); |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1358 | if (stream_ != NULL) { |
| 1359 | call_->DestroyVideoSendStream(stream_); |
| 1360 | } |
pbos@webrtc.org | 7fe1e03 | 2014-10-14 04:25:33 +0000 | [diff] [blame] | 1361 | DestroyVideoEncoder(&allocated_encoder_); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1362 | } |
| 1363 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1364 | static void CreateBlackFrame(webrtc::I420VideoFrame* video_frame, |
| 1365 | int width, |
| 1366 | int height) { |
pbos@webrtc.org | b4987bf | 2015-02-18 10:13:09 +0000 | [diff] [blame] | 1367 | video_frame->CreateEmptyFrame(width, height, width, (width + 1) / 2, |
| 1368 | (width + 1) / 2); |
| 1369 | memset(video_frame->buffer(webrtc::kYPlane), 16, |
| 1370 | video_frame->allocated_size(webrtc::kYPlane)); |
| 1371 | memset(video_frame->buffer(webrtc::kUPlane), 128, |
| 1372 | video_frame->allocated_size(webrtc::kUPlane)); |
| 1373 | memset(video_frame->buffer(webrtc::kVPlane), 128, |
| 1374 | video_frame->allocated_size(webrtc::kVPlane)); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1375 | } |
| 1376 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1377 | void WebRtcVideoChannel2::WebRtcVideoSendStream::InputFrame( |
| 1378 | VideoCapturer* capturer, |
| 1379 | const VideoFrame* frame) { |
pbos@webrtc.org | 86196c4 | 2015-02-16 21:02:00 +0000 | [diff] [blame] | 1380 | TRACE_EVENT0("webrtc", "WebRtcVideoSendStream::InputFrame"); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1381 | LOG(LS_VERBOSE) << "InputFrame: " << frame->GetWidth() << "x" |
| 1382 | << frame->GetHeight(); |
magjed@webrtc.org | 370a72c | 2015-03-11 14:15:44 +0000 | [diff] [blame^] | 1383 | webrtc::I420VideoFrame video_frame(frame->GetVideoFrameBuffer(), 0, 0, |
| 1384 | frame->GetVideoRotation()); |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1385 | rtc::CritScope cs(&lock_); |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1386 | if (stream_ == NULL) { |
| 1387 | LOG(LS_WARNING) << "Capturer inputting frames before send codecs are " |
| 1388 | "configured, dropping."; |
| 1389 | return; |
| 1390 | } |
pbos@webrtc.org | 86196c4 | 2015-02-16 21:02:00 +0000 | [diff] [blame] | 1391 | |
| 1392 | // Not sending, abort early to prevent expensive reconfigurations while |
| 1393 | // setting up codecs etc. |
| 1394 | if (!sending_) |
| 1395 | return; |
| 1396 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1397 | if (format_.width == 0) { // Dropping frames. |
| 1398 | assert(format_.height == 0); |
| 1399 | LOG(LS_VERBOSE) << "VideoFormat 0x0 set, Dropping frame."; |
| 1400 | return; |
| 1401 | } |
pbos@webrtc.org | d60d79a | 2014-09-24 07:10:57 +0000 | [diff] [blame] | 1402 | if (muted_) { |
| 1403 | // Create a black frame to transmit instead. |
magjed@webrtc.org | 370a72c | 2015-03-11 14:15:44 +0000 | [diff] [blame^] | 1404 | CreateBlackFrame(&video_frame, |
pbos@webrtc.org | d60d79a | 2014-09-24 07:10:57 +0000 | [diff] [blame] | 1405 | static_cast<int>(frame->GetWidth()), |
| 1406 | static_cast<int>(frame->GetHeight())); |
| 1407 | } |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1408 | // Reconfigure codec if necessary. |
pbos@webrtc.org | c4175b9 | 2014-09-03 15:25:49 +0000 | [diff] [blame] | 1409 | SetDimensions( |
magjed@webrtc.org | 370a72c | 2015-03-11 14:15:44 +0000 | [diff] [blame^] | 1410 | video_frame.width(), video_frame.height(), capturer->IsScreencast()); |
pbos@webrtc.org | c4175b9 | 2014-09-03 15:25:49 +0000 | [diff] [blame] | 1411 | |
magjed@webrtc.org | 370a72c | 2015-03-11 14:15:44 +0000 | [diff] [blame^] | 1412 | LOG(LS_VERBOSE) << "SwapFrame: " << video_frame.width() << "x" |
| 1413 | << video_frame.height() << " -> (codec) " |
pbos@webrtc.org | bbe0a85 | 2014-09-19 12:30:25 +0000 | [diff] [blame] | 1414 | << parameters_.encoder_config.streams.back().width << "x" |
| 1415 | << parameters_.encoder_config.streams.back().height; |
magjed@webrtc.org | 370a72c | 2015-03-11 14:15:44 +0000 | [diff] [blame^] | 1416 | stream_->Input()->SwapFrame(&video_frame); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1417 | } |
| 1418 | |
| 1419 | bool WebRtcVideoChannel2::WebRtcVideoSendStream::SetCapturer( |
| 1420 | VideoCapturer* capturer) { |
pbos@webrtc.org | b4987bf | 2015-02-18 10:13:09 +0000 | [diff] [blame] | 1421 | TRACE_EVENT0("webrtc", "WebRtcVideoSendStream::SetCapturer"); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1422 | if (!DisconnectCapturer() && capturer == NULL) { |
| 1423 | return false; |
| 1424 | } |
| 1425 | |
| 1426 | { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1427 | rtc::CritScope cs(&lock_); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1428 | |
pbos@webrtc.org | 9359cb3 | 2014-07-23 15:44:48 +0000 | [diff] [blame] | 1429 | if (capturer == NULL) { |
| 1430 | if (stream_ != NULL) { |
| 1431 | LOG(LS_VERBOSE) << "Disabling capturer, sending black frame."; |
| 1432 | webrtc::I420VideoFrame black_frame; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1433 | |
pbos@webrtc.org | b4987bf | 2015-02-18 10:13:09 +0000 | [diff] [blame] | 1434 | CreateBlackFrame(&black_frame, last_dimensions_.width, |
| 1435 | last_dimensions_.height); |
magjed@webrtc.org | d7452a0 | 2015-03-10 15:12:26 +0000 | [diff] [blame] | 1436 | stream_->Input()->SwapFrame(&black_frame); |
pbos@webrtc.org | 9359cb3 | 2014-07-23 15:44:48 +0000 | [diff] [blame] | 1437 | } |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1438 | |
| 1439 | capturer_ = NULL; |
| 1440 | return true; |
| 1441 | } |
| 1442 | |
| 1443 | capturer_ = capturer; |
| 1444 | } |
| 1445 | // Lock cannot be held while connecting the capturer to prevent lock-order |
| 1446 | // violations. |
| 1447 | capturer->SignalVideoFrame.connect(this, &WebRtcVideoSendStream::InputFrame); |
| 1448 | return true; |
| 1449 | } |
| 1450 | |
| 1451 | bool WebRtcVideoChannel2::WebRtcVideoSendStream::SetVideoFormat( |
| 1452 | const VideoFormat& format) { |
| 1453 | if ((format.width == 0 || format.height == 0) && |
| 1454 | format.width != format.height) { |
| 1455 | LOG(LS_ERROR) << "Can't set VideoFormat, width or height is zero (but not " |
| 1456 | "both, 0x0 drops frames)."; |
| 1457 | return false; |
| 1458 | } |
| 1459 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1460 | rtc::CritScope cs(&lock_); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1461 | if (format.width == 0 && format.height == 0) { |
| 1462 | LOG(LS_INFO) |
| 1463 | << "0x0 resolution selected. Captured frames will be dropped for ssrc: " |
pbos@webrtc.org | 6ae48c6 | 2014-06-06 10:49:19 +0000 | [diff] [blame] | 1464 | << parameters_.config.rtp.ssrcs[0] << "."; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1465 | } else { |
| 1466 | // TODO(pbos): Fix me, this only affects the last stream! |
pbos@webrtc.org | bbe0a85 | 2014-09-19 12:30:25 +0000 | [diff] [blame] | 1467 | parameters_.encoder_config.streams.back().max_framerate = |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1468 | VideoFormat::IntervalToFps(format.interval); |
pbos@webrtc.org | c4175b9 | 2014-09-03 15:25:49 +0000 | [diff] [blame] | 1469 | SetDimensions(format.width, format.height, false); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1470 | } |
| 1471 | |
| 1472 | format_ = format; |
| 1473 | return true; |
| 1474 | } |
| 1475 | |
pbos@webrtc.org | ef8bb8d | 2014-08-13 21:36:18 +0000 | [diff] [blame] | 1476 | void WebRtcVideoChannel2::WebRtcVideoSendStream::MuteStream(bool mute) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1477 | rtc::CritScope cs(&lock_); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1478 | muted_ = mute; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1479 | } |
| 1480 | |
| 1481 | bool WebRtcVideoChannel2::WebRtcVideoSendStream::DisconnectCapturer() { |
pbos@webrtc.org | 963b979 | 2014-10-07 14:27:27 +0000 | [diff] [blame] | 1482 | cricket::VideoCapturer* capturer; |
| 1483 | { |
| 1484 | rtc::CritScope cs(&lock_); |
pbos@webrtc.org | 9a4410e | 2015-02-26 10:03:39 +0000 | [diff] [blame] | 1485 | if (capturer_ == NULL) |
pbos@webrtc.org | 963b979 | 2014-10-07 14:27:27 +0000 | [diff] [blame] | 1486 | return false; |
pbos@webrtc.org | 9a4410e | 2015-02-26 10:03:39 +0000 | [diff] [blame] | 1487 | |
| 1488 | if (capturer_->video_adapter() != nullptr) |
| 1489 | old_adapt_changes_ += capturer_->video_adapter()->adaptation_changes(); |
| 1490 | |
pbos@webrtc.org | 963b979 | 2014-10-07 14:27:27 +0000 | [diff] [blame] | 1491 | capturer = capturer_; |
| 1492 | capturer_ = NULL; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1493 | } |
pbos@webrtc.org | 963b979 | 2014-10-07 14:27:27 +0000 | [diff] [blame] | 1494 | capturer->SignalVideoFrame.disconnect(this); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1495 | return true; |
| 1496 | } |
| 1497 | |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1498 | void WebRtcVideoChannel2::WebRtcVideoSendStream::SetOptions( |
| 1499 | const VideoOptions& options) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1500 | rtc::CritScope cs(&lock_); |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1501 | VideoCodecSettings codec_settings; |
| 1502 | if (parameters_.codec_settings.Get(&codec_settings)) { |
| 1503 | SetCodecAndOptions(codec_settings, options); |
| 1504 | } else { |
| 1505 | parameters_.options = options; |
| 1506 | } |
| 1507 | } |
pbos@webrtc.org | 7fe1e03 | 2014-10-14 04:25:33 +0000 | [diff] [blame] | 1508 | |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1509 | void WebRtcVideoChannel2::WebRtcVideoSendStream::SetCodec( |
| 1510 | const VideoCodecSettings& codec_settings) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1511 | rtc::CritScope cs(&lock_); |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1512 | SetCodecAndOptions(codec_settings, parameters_.options); |
| 1513 | } |
pbos@webrtc.org | 7fe1e03 | 2014-10-14 04:25:33 +0000 | [diff] [blame] | 1514 | |
| 1515 | webrtc::VideoCodecType CodecTypeFromName(const std::string& name) { |
| 1516 | if (CodecNameMatches(name, kVp8CodecName)) { |
| 1517 | return webrtc::kVideoCodecVP8; |
andresp@webrtc.org | 188d3b2 | 2014-11-07 13:21:04 +0000 | [diff] [blame] | 1518 | } else if (CodecNameMatches(name, kVp9CodecName)) { |
| 1519 | return webrtc::kVideoCodecVP9; |
pbos@webrtc.org | 7fe1e03 | 2014-10-14 04:25:33 +0000 | [diff] [blame] | 1520 | } else if (CodecNameMatches(name, kH264CodecName)) { |
| 1521 | return webrtc::kVideoCodecH264; |
| 1522 | } |
| 1523 | return webrtc::kVideoCodecUnknown; |
| 1524 | } |
| 1525 | |
| 1526 | WebRtcVideoChannel2::WebRtcVideoSendStream::AllocatedEncoder |
| 1527 | WebRtcVideoChannel2::WebRtcVideoSendStream::CreateVideoEncoder( |
| 1528 | const VideoCodec& codec) { |
| 1529 | webrtc::VideoCodecType type = CodecTypeFromName(codec.name); |
| 1530 | |
| 1531 | // Do not re-create encoders of the same type. |
| 1532 | if (type == allocated_encoder_.type && allocated_encoder_.encoder != NULL) { |
| 1533 | return allocated_encoder_; |
| 1534 | } |
| 1535 | |
| 1536 | if (external_encoder_factory_ != NULL) { |
| 1537 | webrtc::VideoEncoder* encoder = |
| 1538 | external_encoder_factory_->CreateVideoEncoder(type); |
| 1539 | if (encoder != NULL) { |
| 1540 | return AllocatedEncoder(encoder, type, true); |
| 1541 | } |
| 1542 | } |
| 1543 | |
| 1544 | if (type == webrtc::kVideoCodecVP8) { |
| 1545 | return AllocatedEncoder( |
| 1546 | webrtc::VideoEncoder::Create(webrtc::VideoEncoder::kVp8), type, false); |
andresp@webrtc.org | 188d3b2 | 2014-11-07 13:21:04 +0000 | [diff] [blame] | 1547 | } else if (type == webrtc::kVideoCodecVP9) { |
| 1548 | return AllocatedEncoder( |
| 1549 | webrtc::VideoEncoder::Create(webrtc::VideoEncoder::kVp9), type, false); |
pbos@webrtc.org | 7fe1e03 | 2014-10-14 04:25:33 +0000 | [diff] [blame] | 1550 | } |
| 1551 | |
| 1552 | // This shouldn't happen, we should not be trying to create something we don't |
| 1553 | // support. |
| 1554 | assert(false); |
| 1555 | return AllocatedEncoder(NULL, webrtc::kVideoCodecUnknown, false); |
| 1556 | } |
| 1557 | |
| 1558 | void WebRtcVideoChannel2::WebRtcVideoSendStream::DestroyVideoEncoder( |
| 1559 | AllocatedEncoder* encoder) { |
| 1560 | if (encoder->external) { |
| 1561 | external_encoder_factory_->DestroyVideoEncoder(encoder->encoder); |
| 1562 | } else { |
| 1563 | delete encoder->encoder; |
| 1564 | } |
| 1565 | } |
| 1566 | |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1567 | void WebRtcVideoChannel2::WebRtcVideoSendStream::SetCodecAndOptions( |
| 1568 | const VideoCodecSettings& codec_settings, |
| 1569 | const VideoOptions& options) { |
pbos@webrtc.org | a2ef4fe | 2014-11-07 10:54:43 +0000 | [diff] [blame] | 1570 | parameters_.encoder_config = |
| 1571 | CreateVideoEncoderConfig(last_dimensions_, codec_settings.codec); |
pbos@webrtc.org | 86196c4 | 2015-02-16 21:02:00 +0000 | [diff] [blame] | 1572 | if (parameters_.encoder_config.streams.empty()) |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1573 | return; |
pbos@webrtc.org | a2ef4fe | 2014-11-07 10:54:43 +0000 | [diff] [blame] | 1574 | |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1575 | format_ = VideoFormat(codec_settings.codec.width, |
| 1576 | codec_settings.codec.height, |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1577 | VideoFormat::FpsToInterval(30), |
| 1578 | FOURCC_I420); |
pbos@webrtc.org | 6ae48c6 | 2014-06-06 10:49:19 +0000 | [diff] [blame] | 1579 | |
pbos@webrtc.org | 7fe1e03 | 2014-10-14 04:25:33 +0000 | [diff] [blame] | 1580 | AllocatedEncoder new_encoder = CreateVideoEncoder(codec_settings.codec); |
| 1581 | parameters_.config.encoder_settings.encoder = new_encoder.encoder; |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1582 | parameters_.config.encoder_settings.payload_name = codec_settings.codec.name; |
| 1583 | parameters_.config.encoder_settings.payload_type = codec_settings.codec.id; |
| 1584 | parameters_.config.rtp.fec = codec_settings.fec; |
| 1585 | |
| 1586 | // Set RTX payload type if RTX is enabled. |
| 1587 | if (!parameters_.config.rtp.rtx.ssrcs.empty()) { |
| 1588 | parameters_.config.rtp.rtx.payload_type = codec_settings.rtx_payload_type; |
| 1589 | } |
| 1590 | |
| 1591 | if (IsNackEnabled(codec_settings.codec)) { |
| 1592 | parameters_.config.rtp.nack.rtp_history_ms = kNackHistoryMs; |
| 1593 | } |
| 1594 | |
pbos@webrtc.org | 5ff71ab | 2014-07-23 07:28:56 +0000 | [diff] [blame] | 1595 | options.suspend_below_min_bitrate.Get( |
| 1596 | ¶meters_.config.suspend_below_min_bitrate); |
| 1597 | |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1598 | parameters_.codec_settings.Set(codec_settings); |
pbos@webrtc.org | 6ae48c6 | 2014-06-06 10:49:19 +0000 | [diff] [blame] | 1599 | parameters_.options = options; |
pbos@webrtc.org | 543e589 | 2014-07-23 07:01:31 +0000 | [diff] [blame] | 1600 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1601 | RecreateWebRtcStream(); |
pbos@webrtc.org | 7fe1e03 | 2014-10-14 04:25:33 +0000 | [diff] [blame] | 1602 | if (allocated_encoder_.encoder != new_encoder.encoder) { |
| 1603 | DestroyVideoEncoder(&allocated_encoder_); |
| 1604 | allocated_encoder_ = new_encoder; |
| 1605 | } |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1606 | } |
| 1607 | |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 1608 | void WebRtcVideoChannel2::WebRtcVideoSendStream::SetRtpExtensions( |
| 1609 | const std::vector<webrtc::RtpExtension>& rtp_extensions) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1610 | rtc::CritScope cs(&lock_); |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 1611 | parameters_.config.rtp.extensions = rtp_extensions; |
| 1612 | RecreateWebRtcStream(); |
| 1613 | } |
| 1614 | |
pbos@webrtc.org | a2ef4fe | 2014-11-07 10:54:43 +0000 | [diff] [blame] | 1615 | webrtc::VideoEncoderConfig |
| 1616 | WebRtcVideoChannel2::WebRtcVideoSendStream::CreateVideoEncoderConfig( |
| 1617 | const Dimensions& dimensions, |
| 1618 | const VideoCodec& codec) const { |
| 1619 | webrtc::VideoEncoderConfig encoder_config; |
| 1620 | if (dimensions.is_screencast) { |
pbos@webrtc.org | efc82c2 | 2014-10-27 13:58:00 +0000 | [diff] [blame] | 1621 | int screencast_min_bitrate_kbps; |
| 1622 | parameters_.options.screencast_min_bitrate.Get( |
| 1623 | &screencast_min_bitrate_kbps); |
| 1624 | encoder_config.min_transmit_bitrate_bps = |
| 1625 | screencast_min_bitrate_kbps * 1000; |
| 1626 | encoder_config.content_type = webrtc::VideoEncoderConfig::kScreenshare; |
| 1627 | } else { |
| 1628 | encoder_config.min_transmit_bitrate_bps = 0; |
| 1629 | encoder_config.content_type = webrtc::VideoEncoderConfig::kRealtimeVideo; |
| 1630 | } |
| 1631 | |
pbos@webrtc.org | a2ef4fe | 2014-11-07 10:54:43 +0000 | [diff] [blame] | 1632 | // Restrict dimensions according to codec max. |
| 1633 | int width = dimensions.width; |
| 1634 | int height = dimensions.height; |
| 1635 | if (!dimensions.is_screencast) { |
| 1636 | if (codec.width < width) |
| 1637 | width = codec.width; |
| 1638 | if (codec.height < height) |
| 1639 | height = codec.height; |
| 1640 | } |
| 1641 | |
| 1642 | VideoCodec clamped_codec = codec; |
| 1643 | clamped_codec.width = width; |
| 1644 | clamped_codec.height = height; |
pbos@webrtc.org | cddd17c | 2014-09-16 16:33:13 +0000 | [diff] [blame] | 1645 | |
pbos@webrtc.org | f1c8b90 | 2015-01-14 17:29:27 +0000 | [diff] [blame] | 1646 | encoder_config.streams = CreateVideoStreams( |
pbos@webrtc.org | a2ef4fe | 2014-11-07 10:54:43 +0000 | [diff] [blame] | 1647 | clamped_codec, parameters_.options, parameters_.config.rtp.ssrcs.size()); |
pbos@webrtc.org | 6f48f1b | 2014-07-22 16:29:54 +0000 | [diff] [blame] | 1648 | |
pbos@webrtc.org | b7ed779 | 2014-10-31 13:08:10 +0000 | [diff] [blame] | 1649 | // Conference mode screencast uses 2 temporal layers split at 100kbit. |
| 1650 | if (parameters_.options.conference_mode.GetWithDefaultIfUnset(false) && |
pbos@webrtc.org | a2ef4fe | 2014-11-07 10:54:43 +0000 | [diff] [blame] | 1651 | dimensions.is_screencast && encoder_config.streams.size() == 1) { |
sprang@webrtc.org | 46d4d29 | 2014-12-23 15:19:35 +0000 | [diff] [blame] | 1652 | ScreenshareLayerConfig config = ScreenshareLayerConfig::GetDefault(); |
| 1653 | |
| 1654 | // For screenshare in conference mode, tl0 and tl1 bitrates are piggybacked |
| 1655 | // on the VideoCodec struct as target and max bitrates, respectively. |
| 1656 | // See eg. webrtc::VP8EncoderImpl::SetRates(). |
| 1657 | encoder_config.streams[0].target_bitrate_bps = |
| 1658 | config.tl0_bitrate_kbps * 1000; |
| 1659 | 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] | 1660 | encoder_config.streams[0].temporal_layer_thresholds_bps.clear(); |
| 1661 | encoder_config.streams[0].temporal_layer_thresholds_bps.push_back( |
sprang@webrtc.org | 46d4d29 | 2014-12-23 15:19:35 +0000 | [diff] [blame] | 1662 | config.tl0_bitrate_kbps * 1000); |
pbos@webrtc.org | b7ed779 | 2014-10-31 13:08:10 +0000 | [diff] [blame] | 1663 | } |
pbos@webrtc.org | a2ef4fe | 2014-11-07 10:54:43 +0000 | [diff] [blame] | 1664 | return encoder_config; |
| 1665 | } |
| 1666 | |
| 1667 | void WebRtcVideoChannel2::WebRtcVideoSendStream::SetDimensions( |
| 1668 | int width, |
| 1669 | int height, |
| 1670 | bool is_screencast) { |
| 1671 | if (last_dimensions_.width == width && last_dimensions_.height == height && |
| 1672 | last_dimensions_.is_screencast == is_screencast) { |
| 1673 | // Configured using the same parameters, do not reconfigure. |
| 1674 | return; |
| 1675 | } |
| 1676 | LOG(LS_INFO) << "SetDimensions: " << width << "x" << height |
| 1677 | << (is_screencast ? " (screencast)" : " (not screencast)"); |
| 1678 | |
| 1679 | last_dimensions_.width = width; |
| 1680 | last_dimensions_.height = height; |
| 1681 | last_dimensions_.is_screencast = is_screencast; |
| 1682 | |
| 1683 | assert(!parameters_.encoder_config.streams.empty()); |
| 1684 | |
| 1685 | VideoCodecSettings codec_settings; |
| 1686 | parameters_.codec_settings.Get(&codec_settings); |
| 1687 | |
| 1688 | webrtc::VideoEncoderConfig encoder_config = |
| 1689 | CreateVideoEncoderConfig(last_dimensions_, codec_settings.codec); |
| 1690 | |
| 1691 | encoder_config.encoder_specific_settings = |
pbos@webrtc.org | f1c8b90 | 2015-01-14 17:29:27 +0000 | [diff] [blame] | 1692 | ConfigureVideoEncoderSettings(codec_settings.codec, parameters_.options); |
pbos@webrtc.org | b7ed779 | 2014-10-31 13:08:10 +0000 | [diff] [blame] | 1693 | |
pbos@webrtc.org | bbe0a85 | 2014-09-19 12:30:25 +0000 | [diff] [blame] | 1694 | bool stream_reconfigured = stream_->ReconfigureVideoEncoder(encoder_config); |
| 1695 | |
pbos@webrtc.org | bbe0a85 | 2014-09-19 12:30:25 +0000 | [diff] [blame] | 1696 | encoder_config.encoder_specific_settings = NULL; |
pbos@webrtc.org | 6f48f1b | 2014-07-22 16:29:54 +0000 | [diff] [blame] | 1697 | |
| 1698 | if (!stream_reconfigured) { |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1699 | LOG(LS_WARNING) << "Failed to reconfigure video encoder for dimensions: " |
| 1700 | << width << "x" << height; |
| 1701 | return; |
| 1702 | } |
pbos@webrtc.org | cddd17c | 2014-09-16 16:33:13 +0000 | [diff] [blame] | 1703 | |
pbos@webrtc.org | bbe0a85 | 2014-09-19 12:30:25 +0000 | [diff] [blame] | 1704 | parameters_.encoder_config = encoder_config; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1705 | } |
| 1706 | |
| 1707 | void WebRtcVideoChannel2::WebRtcVideoSendStream::Start() { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1708 | rtc::CritScope cs(&lock_); |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1709 | assert(stream_ != NULL); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1710 | stream_->Start(); |
| 1711 | sending_ = true; |
| 1712 | } |
| 1713 | |
| 1714 | void WebRtcVideoChannel2::WebRtcVideoSendStream::Stop() { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1715 | rtc::CritScope cs(&lock_); |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1716 | if (stream_ != NULL) { |
| 1717 | stream_->Stop(); |
| 1718 | } |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1719 | sending_ = false; |
| 1720 | } |
| 1721 | |
pbos@webrtc.org | e6f84ae | 2014-07-18 11:11:55 +0000 | [diff] [blame] | 1722 | VideoSenderInfo |
| 1723 | WebRtcVideoChannel2::WebRtcVideoSendStream::GetVideoSenderInfo() { |
| 1724 | VideoSenderInfo info; |
pbos@webrtc.org | 77e11bb | 2015-02-23 16:39:07 +0000 | [diff] [blame] | 1725 | webrtc::VideoSendStream::Stats stats; |
| 1726 | { |
| 1727 | rtc::CritScope cs(&lock_); |
| 1728 | for (uint32_t ssrc : parameters_.config.rtp.ssrcs) |
| 1729 | info.add_ssrc(ssrc); |
pbos@webrtc.org | e6f84ae | 2014-07-18 11:11:55 +0000 | [diff] [blame] | 1730 | |
pbos@webrtc.org | 77e11bb | 2015-02-23 16:39:07 +0000 | [diff] [blame] | 1731 | for (size_t i = 0; i < parameters_.encoder_config.streams.size(); ++i) { |
| 1732 | if (i == parameters_.encoder_config.streams.size() - 1) { |
| 1733 | info.preferred_bitrate += |
| 1734 | parameters_.encoder_config.streams[i].max_bitrate_bps; |
| 1735 | } else { |
| 1736 | info.preferred_bitrate += |
| 1737 | parameters_.encoder_config.streams[i].target_bitrate_bps; |
| 1738 | } |
| 1739 | } |
pbos@webrtc.org | c3d2bd2 | 2014-08-12 20:55:10 +0000 | [diff] [blame] | 1740 | |
pbos@webrtc.org | 77e11bb | 2015-02-23 16:39:07 +0000 | [diff] [blame] | 1741 | if (stream_ == NULL) |
| 1742 | return info; |
| 1743 | |
| 1744 | stats = stream_->GetStats(); |
| 1745 | |
pbos@webrtc.org | 9a4410e | 2015-02-26 10:03:39 +0000 | [diff] [blame] | 1746 | info.adapt_changes = old_adapt_changes_; |
| 1747 | info.adapt_reason = CoordinatedVideoAdapter::ADAPTREASON_NONE; |
| 1748 | |
| 1749 | if (capturer_ != NULL) { |
| 1750 | if (!capturer_->IsMuted()) { |
| 1751 | VideoFormat last_captured_frame_format; |
| 1752 | capturer_->GetStats(&info.adapt_frame_drops, &info.effects_frame_drops, |
| 1753 | &info.capturer_frame_time, |
| 1754 | &last_captured_frame_format); |
| 1755 | info.input_frame_width = last_captured_frame_format.width; |
| 1756 | info.input_frame_height = last_captured_frame_format.height; |
| 1757 | } |
| 1758 | if (capturer_->video_adapter() != nullptr) { |
| 1759 | info.adapt_changes += capturer_->video_adapter()->adaptation_changes(); |
| 1760 | info.adapt_reason = capturer_->video_adapter()->adapt_reason(); |
| 1761 | } |
pbos@webrtc.org | 77e11bb | 2015-02-23 16:39:07 +0000 | [diff] [blame] | 1762 | } |
| 1763 | } |
pbos@webrtc.org | e6f84ae | 2014-07-18 11:11:55 +0000 | [diff] [blame] | 1764 | info.framerate_input = stats.input_frame_rate; |
| 1765 | info.framerate_sent = stats.encode_frame_rate; |
pbos@webrtc.org | 3e6e271 | 2015-02-26 12:19:31 +0000 | [diff] [blame] | 1766 | info.avg_encode_ms = stats.avg_encode_time_ms; |
| 1767 | info.encode_usage_percent = stats.encode_usage_percent; |
pbos@webrtc.org | e6f84ae | 2014-07-18 11:11:55 +0000 | [diff] [blame] | 1768 | |
pbos@webrtc.org | 77e11bb | 2015-02-23 16:39:07 +0000 | [diff] [blame] | 1769 | info.nominal_bitrate = stats.media_bitrate_bps; |
| 1770 | |
pbos@webrtc.org | 273a414 | 2014-12-01 15:23:21 +0000 | [diff] [blame] | 1771 | info.send_frame_width = 0; |
| 1772 | info.send_frame_height = 0; |
pbos@webrtc.org | 09c77b9 | 2015-02-25 10:42:16 +0000 | [diff] [blame] | 1773 | for (std::map<uint32_t, webrtc::VideoSendStream::StreamStats>::iterator it = |
pbos@webrtc.org | e6f84ae | 2014-07-18 11:11:55 +0000 | [diff] [blame] | 1774 | stats.substreams.begin(); |
pbos@webrtc.org | 09c77b9 | 2015-02-25 10:42:16 +0000 | [diff] [blame] | 1775 | it != stats.substreams.end(); ++it) { |
pbos@webrtc.org | e6f84ae | 2014-07-18 11:11:55 +0000 | [diff] [blame] | 1776 | // TODO(pbos): Wire up additional stats, such as padding bytes. |
pbos@webrtc.org | 09c77b9 | 2015-02-25 10:42:16 +0000 | [diff] [blame] | 1777 | webrtc::VideoSendStream::StreamStats stream_stats = it->second; |
asapersson@webrtc.org | cfd82df | 2015-01-22 09:39:59 +0000 | [diff] [blame] | 1778 | info.bytes_sent += stream_stats.rtp_stats.transmitted.payload_bytes + |
| 1779 | stream_stats.rtp_stats.transmitted.header_bytes + |
| 1780 | stream_stats.rtp_stats.transmitted.padding_bytes; |
| 1781 | info.packets_sent += stream_stats.rtp_stats.transmitted.packets; |
pbos@webrtc.org | e6f84ae | 2014-07-18 11:11:55 +0000 | [diff] [blame] | 1782 | info.packets_lost += stream_stats.rtcp_stats.cumulative_lost; |
pbos@webrtc.org | 09c77b9 | 2015-02-25 10:42:16 +0000 | [diff] [blame] | 1783 | if (stream_stats.width > info.send_frame_width) |
| 1784 | info.send_frame_width = stream_stats.width; |
| 1785 | if (stream_stats.height > info.send_frame_height) |
| 1786 | info.send_frame_height = stream_stats.height; |
pbos@webrtc.org | 1d0fa5d | 2015-02-19 12:47:00 +0000 | [diff] [blame] | 1787 | info.firs_rcvd += stream_stats.rtcp_packet_type_counts.fir_packets; |
| 1788 | info.nacks_rcvd += stream_stats.rtcp_packet_type_counts.nack_packets; |
| 1789 | info.plis_rcvd += stream_stats.rtcp_packet_type_counts.pli_packets; |
pbos@webrtc.org | e6f84ae | 2014-07-18 11:11:55 +0000 | [diff] [blame] | 1790 | } |
| 1791 | |
| 1792 | if (!stats.substreams.empty()) { |
| 1793 | // TODO(pbos): Report fraction lost per SSRC. |
pbos@webrtc.org | 09c77b9 | 2015-02-25 10:42:16 +0000 | [diff] [blame] | 1794 | webrtc::VideoSendStream::StreamStats first_stream_stats = |
| 1795 | stats.substreams.begin()->second; |
pbos@webrtc.org | e6f84ae | 2014-07-18 11:11:55 +0000 | [diff] [blame] | 1796 | info.fraction_lost = |
| 1797 | static_cast<float>(first_stream_stats.rtcp_stats.fraction_lost) / |
| 1798 | (1 << 8); |
| 1799 | } |
| 1800 | |
pbos@webrtc.org | e6f84ae | 2014-07-18 11:11:55 +0000 | [diff] [blame] | 1801 | return info; |
| 1802 | } |
| 1803 | |
stefan@webrtc.org | 0bae1fa | 2014-11-05 14:05:29 +0000 | [diff] [blame] | 1804 | void WebRtcVideoChannel2::WebRtcVideoSendStream::FillBandwidthEstimationInfo( |
| 1805 | BandwidthEstimationInfo* bwe_info) { |
| 1806 | rtc::CritScope cs(&lock_); |
| 1807 | if (stream_ == NULL) { |
| 1808 | return; |
| 1809 | } |
| 1810 | webrtc::VideoSendStream::Stats stats = stream_->GetStats(); |
pbos@webrtc.org | 09c77b9 | 2015-02-25 10:42:16 +0000 | [diff] [blame] | 1811 | for (std::map<uint32_t, webrtc::VideoSendStream::StreamStats>::iterator it = |
stefan@webrtc.org | 0bae1fa | 2014-11-05 14:05:29 +0000 | [diff] [blame] | 1812 | stats.substreams.begin(); |
pbos@webrtc.org | 09c77b9 | 2015-02-25 10:42:16 +0000 | [diff] [blame] | 1813 | it != stats.substreams.end(); ++it) { |
stefan@webrtc.org | 0bae1fa | 2014-11-05 14:05:29 +0000 | [diff] [blame] | 1814 | bwe_info->transmit_bitrate += it->second.total_bitrate_bps; |
| 1815 | bwe_info->retransmit_bitrate += it->second.retransmit_bitrate_bps; |
| 1816 | } |
pbos@webrtc.org | 891d483 | 2015-02-26 13:15:22 +0000 | [diff] [blame] | 1817 | bwe_info->target_enc_bitrate += stats.target_media_bitrate_bps; |
pbos@webrtc.org | 77e11bb | 2015-02-23 16:39:07 +0000 | [diff] [blame] | 1818 | bwe_info->actual_enc_bitrate += stats.media_bitrate_bps; |
stefan@webrtc.org | 0bae1fa | 2014-11-05 14:05:29 +0000 | [diff] [blame] | 1819 | } |
| 1820 | |
pbos@webrtc.org | 42684be | 2014-10-03 11:25:45 +0000 | [diff] [blame] | 1821 | void WebRtcVideoChannel2::WebRtcVideoSendStream::OnCpuResolutionRequest( |
| 1822 | CoordinatedVideoAdapter::AdaptRequest adapt_request) { |
| 1823 | rtc::CritScope cs(&lock_); |
| 1824 | bool adapt_cpu; |
| 1825 | parameters_.options.cpu_overuse_detection.Get(&adapt_cpu); |
pbos@webrtc.org | 9a4410e | 2015-02-26 10:03:39 +0000 | [diff] [blame] | 1826 | if (!adapt_cpu) |
pbos@webrtc.org | 42684be | 2014-10-03 11:25:45 +0000 | [diff] [blame] | 1827 | return; |
pbos@webrtc.org | 9a4410e | 2015-02-26 10:03:39 +0000 | [diff] [blame] | 1828 | if (capturer_ == NULL || capturer_->video_adapter() == NULL) |
pbos@webrtc.org | 42684be | 2014-10-03 11:25:45 +0000 | [diff] [blame] | 1829 | return; |
pbos@webrtc.org | 42684be | 2014-10-03 11:25:45 +0000 | [diff] [blame] | 1830 | |
| 1831 | capturer_->video_adapter()->OnCpuResolutionRequest(adapt_request); |
| 1832 | } |
| 1833 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1834 | void WebRtcVideoChannel2::WebRtcVideoSendStream::RecreateWebRtcStream() { |
| 1835 | if (stream_ != NULL) { |
| 1836 | call_->DestroyVideoSendStream(stream_); |
| 1837 | } |
pbos@webrtc.org | 6ae48c6 | 2014-06-06 10:49:19 +0000 | [diff] [blame] | 1838 | |
pbos@webrtc.org | 6f48f1b | 2014-07-22 16:29:54 +0000 | [diff] [blame] | 1839 | VideoCodecSettings codec_settings; |
| 1840 | parameters_.codec_settings.Get(&codec_settings); |
pbos@webrtc.org | bbe0a85 | 2014-09-19 12:30:25 +0000 | [diff] [blame] | 1841 | parameters_.encoder_config.encoder_specific_settings = |
pbos@webrtc.org | f1c8b90 | 2015-01-14 17:29:27 +0000 | [diff] [blame] | 1842 | ConfigureVideoEncoderSettings(codec_settings.codec, parameters_.options); |
pbos@webrtc.org | 6f48f1b | 2014-07-22 16:29:54 +0000 | [diff] [blame] | 1843 | |
pbos@webrtc.org | bbe0a85 | 2014-09-19 12:30:25 +0000 | [diff] [blame] | 1844 | stream_ = call_->CreateVideoSendStream(parameters_.config, |
| 1845 | parameters_.encoder_config); |
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 | parameters_.encoder_config.encoder_specific_settings = NULL; |
pbos@webrtc.org | 6f48f1b | 2014-07-22 16:29:54 +0000 | [diff] [blame] | 1848 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1849 | if (sending_) { |
| 1850 | stream_->Start(); |
| 1851 | } |
| 1852 | } |
| 1853 | |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 1854 | WebRtcVideoChannel2::WebRtcVideoReceiveStream::WebRtcVideoReceiveStream( |
| 1855 | webrtc::Call* call, |
pbos@webrtc.org | 776e6f2 | 2014-10-29 15:28:39 +0000 | [diff] [blame] | 1856 | WebRtcVideoDecoderFactory* external_decoder_factory, |
pbos@webrtc.org | a2a6fe6 | 2015-03-06 15:35:19 +0000 | [diff] [blame] | 1857 | bool default_stream, |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 1858 | const webrtc::VideoReceiveStream::Config& config, |
| 1859 | const std::vector<VideoCodecSettings>& recv_codecs) |
| 1860 | : call_(call), |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 1861 | stream_(NULL), |
pbos@webrtc.org | a2a6fe6 | 2015-03-06 15:35:19 +0000 | [diff] [blame] | 1862 | default_stream_(default_stream), |
pbos@webrtc.org | b648b9d | 2014-08-26 11:08:06 +0000 | [diff] [blame] | 1863 | config_(config), |
pbos@webrtc.org | 776e6f2 | 2014-10-29 15:28:39 +0000 | [diff] [blame] | 1864 | external_decoder_factory_(external_decoder_factory), |
pbos@webrtc.org | b648b9d | 2014-08-26 11:08:06 +0000 | [diff] [blame] | 1865 | renderer_(NULL), |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 1866 | last_width_(-1), |
magjed@webrtc.org | fc5ad95 | 2015-01-27 09:57:01 +0000 | [diff] [blame] | 1867 | last_height_(-1), |
| 1868 | first_frame_timestamp_(-1), |
| 1869 | estimated_remote_start_ntp_time_ms_(0) { |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 1870 | config_.renderer = this; |
| 1871 | // SetRecvCodecs will also reset (start) the VideoReceiveStream. |
| 1872 | SetRecvCodecs(recv_codecs); |
| 1873 | } |
| 1874 | |
| 1875 | WebRtcVideoChannel2::WebRtcVideoReceiveStream::~WebRtcVideoReceiveStream() { |
| 1876 | call_->DestroyVideoReceiveStream(stream_); |
pbos@webrtc.org | 96a9325 | 2014-11-03 14:46:44 +0000 | [diff] [blame] | 1877 | ClearDecoders(&allocated_decoders_); |
| 1878 | } |
| 1879 | |
| 1880 | WebRtcVideoChannel2::WebRtcVideoReceiveStream::AllocatedDecoder |
| 1881 | WebRtcVideoChannel2::WebRtcVideoReceiveStream::CreateOrReuseVideoDecoder( |
| 1882 | std::vector<AllocatedDecoder>* old_decoders, |
| 1883 | const VideoCodec& codec) { |
| 1884 | webrtc::VideoCodecType type = CodecTypeFromName(codec.name); |
| 1885 | |
| 1886 | for (size_t i = 0; i < old_decoders->size(); ++i) { |
| 1887 | if ((*old_decoders)[i].type == type) { |
| 1888 | AllocatedDecoder decoder = (*old_decoders)[i]; |
| 1889 | (*old_decoders)[i] = old_decoders->back(); |
| 1890 | old_decoders->pop_back(); |
| 1891 | return decoder; |
| 1892 | } |
| 1893 | } |
| 1894 | |
| 1895 | if (external_decoder_factory_ != NULL) { |
| 1896 | webrtc::VideoDecoder* decoder = |
| 1897 | external_decoder_factory_->CreateVideoDecoder(type); |
| 1898 | if (decoder != NULL) { |
| 1899 | return AllocatedDecoder(decoder, type, true); |
| 1900 | } |
| 1901 | } |
| 1902 | |
| 1903 | if (type == webrtc::kVideoCodecVP8) { |
| 1904 | return AllocatedDecoder( |
| 1905 | webrtc::VideoDecoder::Create(webrtc::VideoDecoder::kVp8), type, false); |
| 1906 | } |
| 1907 | |
| 1908 | // This shouldn't happen, we should not be trying to create something we don't |
| 1909 | // support. |
| 1910 | assert(false); |
| 1911 | return AllocatedDecoder(NULL, webrtc::kVideoCodecUnknown, false); |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 1912 | } |
| 1913 | |
| 1914 | void WebRtcVideoChannel2::WebRtcVideoReceiveStream::SetRecvCodecs( |
| 1915 | const std::vector<VideoCodecSettings>& recv_codecs) { |
pbos@webrtc.org | 96a9325 | 2014-11-03 14:46:44 +0000 | [diff] [blame] | 1916 | std::vector<AllocatedDecoder> old_decoders = allocated_decoders_; |
| 1917 | allocated_decoders_.clear(); |
| 1918 | config_.decoders.clear(); |
| 1919 | for (size_t i = 0; i < recv_codecs.size(); ++i) { |
| 1920 | AllocatedDecoder allocated_decoder = |
| 1921 | CreateOrReuseVideoDecoder(&old_decoders, recv_codecs[i].codec); |
| 1922 | allocated_decoders_.push_back(allocated_decoder); |
| 1923 | |
| 1924 | webrtc::VideoReceiveStream::Decoder decoder; |
| 1925 | decoder.decoder = allocated_decoder.decoder; |
| 1926 | decoder.payload_type = recv_codecs[i].codec.id; |
| 1927 | decoder.payload_name = recv_codecs[i].codec.name; |
| 1928 | config_.decoders.push_back(decoder); |
| 1929 | } |
| 1930 | |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 1931 | // TODO(pbos): Reconfigure RTX based on incoming recv_codecs. |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 1932 | config_.rtp.fec = recv_codecs.front().fec; |
pbos@webrtc.org | 257e130 | 2014-07-25 19:01:32 +0000 | [diff] [blame] | 1933 | config_.rtp.nack.rtp_history_ms = |
| 1934 | IsNackEnabled(recv_codecs.begin()->codec) ? kNackHistoryMs : 0; |
| 1935 | config_.rtp.remb = IsRembEnabled(recv_codecs.begin()->codec); |
| 1936 | |
pbos@webrtc.org | 96a9325 | 2014-11-03 14:46:44 +0000 | [diff] [blame] | 1937 | ClearDecoders(&old_decoders); |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 1938 | RecreateWebRtcStream(); |
| 1939 | } |
| 1940 | |
| 1941 | void WebRtcVideoChannel2::WebRtcVideoReceiveStream::SetRtpExtensions( |
| 1942 | const std::vector<webrtc::RtpExtension>& extensions) { |
| 1943 | config_.rtp.extensions = extensions; |
| 1944 | RecreateWebRtcStream(); |
| 1945 | } |
| 1946 | |
| 1947 | void WebRtcVideoChannel2::WebRtcVideoReceiveStream::RecreateWebRtcStream() { |
| 1948 | if (stream_ != NULL) { |
| 1949 | call_->DestroyVideoReceiveStream(stream_); |
| 1950 | } |
| 1951 | stream_ = call_->CreateVideoReceiveStream(config_); |
| 1952 | stream_->Start(); |
| 1953 | } |
| 1954 | |
pbos@webrtc.org | 96a9325 | 2014-11-03 14:46:44 +0000 | [diff] [blame] | 1955 | void WebRtcVideoChannel2::WebRtcVideoReceiveStream::ClearDecoders( |
| 1956 | std::vector<AllocatedDecoder>* allocated_decoders) { |
| 1957 | for (size_t i = 0; i < allocated_decoders->size(); ++i) { |
| 1958 | if ((*allocated_decoders)[i].external) { |
pbos@webrtc.org | 776e6f2 | 2014-10-29 15:28:39 +0000 | [diff] [blame] | 1959 | external_decoder_factory_->DestroyVideoDecoder( |
pbos@webrtc.org | 96a9325 | 2014-11-03 14:46:44 +0000 | [diff] [blame] | 1960 | (*allocated_decoders)[i].decoder); |
pbos@webrtc.org | 776e6f2 | 2014-10-29 15:28:39 +0000 | [diff] [blame] | 1961 | } else { |
pbos@webrtc.org | 96a9325 | 2014-11-03 14:46:44 +0000 | [diff] [blame] | 1962 | delete (*allocated_decoders)[i].decoder; |
pbos@webrtc.org | 776e6f2 | 2014-10-29 15:28:39 +0000 | [diff] [blame] | 1963 | } |
| 1964 | } |
pbos@webrtc.org | 96a9325 | 2014-11-03 14:46:44 +0000 | [diff] [blame] | 1965 | allocated_decoders->clear(); |
pbos@webrtc.org | 776e6f2 | 2014-10-29 15:28:39 +0000 | [diff] [blame] | 1966 | } |
| 1967 | |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 1968 | void WebRtcVideoChannel2::WebRtcVideoReceiveStream::RenderFrame( |
| 1969 | const webrtc::I420VideoFrame& frame, |
| 1970 | int time_to_render_ms) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1971 | rtc::CritScope crit(&renderer_lock_); |
magjed@webrtc.org | fc5ad95 | 2015-01-27 09:57:01 +0000 | [diff] [blame] | 1972 | |
| 1973 | if (first_frame_timestamp_ < 0) |
| 1974 | first_frame_timestamp_ = frame.timestamp(); |
| 1975 | int64_t rtp_time_elapsed_since_first_frame = |
| 1976 | (timestamp_wraparound_handler_.Unwrap(frame.timestamp()) - |
| 1977 | first_frame_timestamp_); |
| 1978 | int64_t elapsed_time_ms = rtp_time_elapsed_since_first_frame / |
| 1979 | (cricket::kVideoCodecClockrate / 1000); |
| 1980 | if (frame.ntp_time_ms() > 0) |
| 1981 | estimated_remote_start_ntp_time_ms_ = frame.ntp_time_ms() - elapsed_time_ms; |
| 1982 | |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 1983 | if (renderer_ == NULL) { |
| 1984 | LOG(LS_WARNING) << "VideoReceiveStream not connected to a VideoRenderer."; |
| 1985 | return; |
| 1986 | } |
| 1987 | |
| 1988 | if (frame.width() != last_width_ || frame.height() != last_height_) { |
| 1989 | SetSize(frame.width(), frame.height()); |
| 1990 | } |
| 1991 | |
| 1992 | LOG(LS_VERBOSE) << "RenderFrame: (" << frame.width() << "x" << frame.height() |
| 1993 | << ")"; |
| 1994 | |
magjed@webrtc.org | 2386d6d | 2015-03-05 14:03:08 +0000 | [diff] [blame] | 1995 | const WebRtcVideoFrame render_frame( |
| 1996 | frame.video_frame_buffer(), |
| 1997 | elapsed_time_ms * rtc::kNumNanosecsPerMillisec, |
| 1998 | frame.render_time_ms() * rtc::kNumNanosecsPerMillisec); |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 1999 | renderer_->RenderFrame(&render_frame); |
| 2000 | } |
| 2001 | |
pbos@webrtc.org | 0d852d5 | 2015-02-09 15:14:36 +0000 | [diff] [blame] | 2002 | bool WebRtcVideoChannel2::WebRtcVideoReceiveStream::IsTextureSupported() const { |
| 2003 | return true; |
| 2004 | } |
| 2005 | |
pbos@webrtc.org | a2a6fe6 | 2015-03-06 15:35:19 +0000 | [diff] [blame] | 2006 | bool WebRtcVideoChannel2::WebRtcVideoReceiveStream::IsDefaultStream() const { |
| 2007 | return default_stream_; |
| 2008 | } |
| 2009 | |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 2010 | void WebRtcVideoChannel2::WebRtcVideoReceiveStream::SetRenderer( |
| 2011 | cricket::VideoRenderer* renderer) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 2012 | rtc::CritScope crit(&renderer_lock_); |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 2013 | renderer_ = renderer; |
| 2014 | if (renderer_ != NULL && last_width_ != -1) { |
| 2015 | SetSize(last_width_, last_height_); |
| 2016 | } |
| 2017 | } |
| 2018 | |
| 2019 | VideoRenderer* WebRtcVideoChannel2::WebRtcVideoReceiveStream::GetRenderer() { |
| 2020 | // TODO(pbos): Remove GetRenderer and all uses of it, it's thread-unsafe by |
| 2021 | // design. |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 2022 | rtc::CritScope crit(&renderer_lock_); |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 2023 | return renderer_; |
| 2024 | } |
| 2025 | |
| 2026 | void WebRtcVideoChannel2::WebRtcVideoReceiveStream::SetSize(int width, |
| 2027 | int height) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 2028 | rtc::CritScope crit(&renderer_lock_); |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 2029 | if (!renderer_->SetSize(width, height, 0)) { |
| 2030 | LOG(LS_ERROR) << "Could not set renderer size."; |
| 2031 | } |
| 2032 | last_width_ = width; |
| 2033 | last_height_ = height; |
| 2034 | } |
| 2035 | |
pbos@webrtc.org | e6f84ae | 2014-07-18 11:11:55 +0000 | [diff] [blame] | 2036 | VideoReceiverInfo |
| 2037 | WebRtcVideoChannel2::WebRtcVideoReceiveStream::GetVideoReceiverInfo() { |
| 2038 | VideoReceiverInfo info; |
| 2039 | info.add_ssrc(config_.rtp.remote_ssrc); |
| 2040 | webrtc::VideoReceiveStream::Stats stats = stream_->GetStats(); |
asapersson@webrtc.org | cfd82df | 2015-01-22 09:39:59 +0000 | [diff] [blame] | 2041 | info.bytes_rcvd = stats.rtp_stats.transmitted.payload_bytes + |
| 2042 | stats.rtp_stats.transmitted.header_bytes + |
| 2043 | stats.rtp_stats.transmitted.padding_bytes; |
| 2044 | info.packets_rcvd = stats.rtp_stats.transmitted.packets; |
pbos@webrtc.org | e6f84ae | 2014-07-18 11:11:55 +0000 | [diff] [blame] | 2045 | |
| 2046 | info.framerate_rcvd = stats.network_frame_rate; |
| 2047 | info.framerate_decoded = stats.decode_frame_rate; |
| 2048 | info.framerate_output = stats.render_frame_rate; |
| 2049 | |
pbos@webrtc.org | 1d0fa5d | 2015-02-19 12:47:00 +0000 | [diff] [blame] | 2050 | { |
| 2051 | rtc::CritScope frame_cs(&renderer_lock_); |
| 2052 | info.frame_width = last_width_; |
| 2053 | info.frame_height = last_height_; |
| 2054 | info.capture_start_ntp_time_ms = estimated_remote_start_ntp_time_ms_; |
| 2055 | } |
| 2056 | |
pbos@webrtc.org | 09c77b9 | 2015-02-25 10:42:16 +0000 | [diff] [blame] | 2057 | info.decode_ms = stats.decode_ms; |
| 2058 | info.max_decode_ms = stats.max_decode_ms; |
| 2059 | info.current_delay_ms = stats.current_delay_ms; |
| 2060 | info.target_delay_ms = stats.target_delay_ms; |
| 2061 | info.jitter_buffer_ms = stats.jitter_buffer_ms; |
| 2062 | info.min_playout_delay_ms = stats.min_playout_delay_ms; |
| 2063 | info.render_delay_ms = stats.render_delay_ms; |
| 2064 | |
pbos@webrtc.org | 1d0fa5d | 2015-02-19 12:47:00 +0000 | [diff] [blame] | 2065 | info.firs_sent = stats.rtcp_packet_type_counts.fir_packets; |
| 2066 | info.plis_sent = stats.rtcp_packet_type_counts.pli_packets; |
| 2067 | info.nacks_sent = stats.rtcp_packet_type_counts.nack_packets; |
pbos@webrtc.org | e6f84ae | 2014-07-18 11:11:55 +0000 | [diff] [blame] | 2068 | |
pbos@webrtc.org | e6f84ae | 2014-07-18 11:11:55 +0000 | [diff] [blame] | 2069 | return info; |
| 2070 | } |
| 2071 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 2072 | WebRtcVideoChannel2::VideoCodecSettings::VideoCodecSettings() |
| 2073 | : rtx_payload_type(-1) {} |
| 2074 | |
pbos@webrtc.org | a2ef4fe | 2014-11-07 10:54:43 +0000 | [diff] [blame] | 2075 | bool WebRtcVideoChannel2::VideoCodecSettings::operator==( |
| 2076 | const WebRtcVideoChannel2::VideoCodecSettings& other) const { |
| 2077 | return codec == other.codec && |
| 2078 | fec.ulpfec_payload_type == other.fec.ulpfec_payload_type && |
| 2079 | fec.red_payload_type == other.fec.red_payload_type && |
| 2080 | rtx_payload_type == other.rtx_payload_type; |
| 2081 | } |
| 2082 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 2083 | std::vector<WebRtcVideoChannel2::VideoCodecSettings> |
| 2084 | WebRtcVideoChannel2::MapCodecs(const std::vector<VideoCodec>& codecs) { |
| 2085 | assert(!codecs.empty()); |
| 2086 | |
| 2087 | std::vector<VideoCodecSettings> video_codecs; |
| 2088 | std::map<int, bool> payload_used; |
pbos@webrtc.org | e322a17 | 2014-06-13 11:47:28 +0000 | [diff] [blame] | 2089 | std::map<int, VideoCodec::CodecType> payload_codec_type; |
pkasting@chromium.org | d324546 | 2015-02-23 21:28:22 +0000 | [diff] [blame] | 2090 | // |rtx_mapping| maps video payload type to rtx payload type. |
| 2091 | std::map<int, int> rtx_mapping; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 2092 | |
| 2093 | webrtc::FecConfig fec_settings; |
| 2094 | |
| 2095 | for (size_t i = 0; i < codecs.size(); ++i) { |
| 2096 | const VideoCodec& in_codec = codecs[i]; |
| 2097 | int payload_type = in_codec.id; |
| 2098 | |
| 2099 | if (payload_used[payload_type]) { |
| 2100 | LOG(LS_ERROR) << "Payload type already registered: " |
| 2101 | << in_codec.ToString(); |
| 2102 | return std::vector<VideoCodecSettings>(); |
| 2103 | } |
| 2104 | payload_used[payload_type] = true; |
pbos@webrtc.org | e322a17 | 2014-06-13 11:47:28 +0000 | [diff] [blame] | 2105 | payload_codec_type[payload_type] = in_codec.GetCodecType(); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 2106 | |
| 2107 | switch (in_codec.GetCodecType()) { |
| 2108 | case VideoCodec::CODEC_RED: { |
| 2109 | // RED payload type, should not have duplicates. |
| 2110 | assert(fec_settings.red_payload_type == -1); |
| 2111 | fec_settings.red_payload_type = in_codec.id; |
| 2112 | continue; |
| 2113 | } |
| 2114 | |
| 2115 | case VideoCodec::CODEC_ULPFEC: { |
| 2116 | // ULPFEC payload type, should not have duplicates. |
| 2117 | assert(fec_settings.ulpfec_payload_type == -1); |
| 2118 | fec_settings.ulpfec_payload_type = in_codec.id; |
| 2119 | continue; |
| 2120 | } |
| 2121 | |
| 2122 | case VideoCodec::CODEC_RTX: { |
| 2123 | int associated_payload_type; |
| 2124 | if (!in_codec.GetParam(kCodecParamAssociatedPayloadType, |
pkasting@chromium.org | e9facf8 | 2015-02-17 20:36:28 +0000 | [diff] [blame] | 2125 | &associated_payload_type) || |
| 2126 | !IsValidRtpPayloadType(associated_payload_type)) { |
| 2127 | LOG(LS_ERROR) |
| 2128 | << "RTX codec with invalid or no associated payload type: " |
| 2129 | << in_codec.ToString(); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 2130 | return std::vector<VideoCodecSettings>(); |
| 2131 | } |
| 2132 | rtx_mapping[associated_payload_type] = in_codec.id; |
| 2133 | continue; |
| 2134 | } |
| 2135 | |
| 2136 | case VideoCodec::CODEC_VIDEO: |
| 2137 | break; |
| 2138 | } |
| 2139 | |
| 2140 | video_codecs.push_back(VideoCodecSettings()); |
| 2141 | video_codecs.back().codec = in_codec; |
| 2142 | } |
| 2143 | |
| 2144 | // One of these codecs should have been a video codec. Only having FEC |
| 2145 | // parameters into this code is a logic error. |
| 2146 | assert(!video_codecs.empty()); |
| 2147 | |
pbos@webrtc.org | e322a17 | 2014-06-13 11:47:28 +0000 | [diff] [blame] | 2148 | for (std::map<int, int>::const_iterator it = rtx_mapping.begin(); |
| 2149 | it != rtx_mapping.end(); |
| 2150 | ++it) { |
| 2151 | if (!payload_used[it->first]) { |
| 2152 | LOG(LS_ERROR) << "RTX mapped to payload not in codec list."; |
| 2153 | return std::vector<VideoCodecSettings>(); |
| 2154 | } |
andrew@webrtc.org | 8f27fcc | 2015-01-09 20:22:46 +0000 | [diff] [blame] | 2155 | if (payload_codec_type[it->first] != VideoCodec::CODEC_VIDEO) { |
| 2156 | LOG(LS_ERROR) << "RTX not mapped to regular video codec."; |
pbos@webrtc.org | e322a17 | 2014-06-13 11:47:28 +0000 | [diff] [blame] | 2157 | return std::vector<VideoCodecSettings>(); |
| 2158 | } |
| 2159 | } |
| 2160 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 2161 | // TODO(pbos): Write tests that figure out that I have not verified that RTX |
| 2162 | // codecs aren't mapped to bogus payloads. |
| 2163 | for (size_t i = 0; i < video_codecs.size(); ++i) { |
| 2164 | video_codecs[i].fec = fec_settings; |
andrew@webrtc.org | 8f27fcc | 2015-01-09 20:22:46 +0000 | [diff] [blame] | 2165 | if (rtx_mapping[video_codecs[i].codec.id] != 0) { |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 2166 | video_codecs[i].rtx_payload_type = rtx_mapping[video_codecs[i].codec.id]; |
| 2167 | } |
| 2168 | } |
| 2169 | |
| 2170 | return video_codecs; |
| 2171 | } |
| 2172 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 2173 | } // namespace cricket |
| 2174 | |
| 2175 | #endif // HAVE_WEBRTC_VIDEO |