pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1 | /* |
| 2 | * libjingle |
| 3 | * Copyright 2014 Google Inc. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions are met: |
| 7 | * |
| 8 | * 1. Redistributions of source code must retain the above copyright notice, |
| 9 | * this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 11 | * this list of conditions and the following disclaimer in the documentation |
| 12 | * and/or other materials provided with the distribution. |
| 13 | * 3. The name of the author may not be used to endorse or promote products |
| 14 | * derived from this software without specific prior written permission. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
| 17 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 18 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
| 19 | * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 20 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 21 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
| 22 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 23 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 24 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
| 25 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 | |
| 28 | #ifdef HAVE_WEBRTC_VIDEO |
| 29 | #include "talk/media/webrtc/webrtcvideoengine2.h" |
| 30 | |
pbos@webrtc.org | 3c10758 | 2014-07-20 15:27:35 +0000 | [diff] [blame] | 31 | #include <set> |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 32 | #include <string> |
| 33 | |
| 34 | #include "libyuv/convert_from.h" |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 35 | #include "talk/media/base/videocapturer.h" |
| 36 | #include "talk/media/base/videorenderer.h" |
buildbot@webrtc.org | df9bbbe | 2014-06-19 19:54:33 +0000 | [diff] [blame] | 37 | #include "talk/media/webrtc/constants.h" |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 38 | #include "talk/media/webrtc/webrtcvideocapturer.h" |
| 39 | #include "talk/media/webrtc/webrtcvideoframe.h" |
| 40 | #include "talk/media/webrtc/webrtcvoiceengine.h" |
buildbot@webrtc.org | a09a999 | 2014-08-13 17:26:08 +0000 | [diff] [blame] | 41 | #include "webrtc/base/buffer.h" |
| 42 | #include "webrtc/base/logging.h" |
| 43 | #include "webrtc/base/stringutils.h" |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 44 | #include "webrtc/call.h" |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 45 | #include "webrtc/video_encoder.h" |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 46 | |
| 47 | #define UNIMPLEMENTED \ |
| 48 | LOG(LS_ERROR) << "Call to unimplemented function " << __FUNCTION__; \ |
| 49 | ASSERT(false) |
| 50 | |
| 51 | namespace cricket { |
| 52 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 53 | // This constant is really an on/off, lower-level configurable NACK history |
| 54 | // duration hasn't been implemented. |
| 55 | static const int kNackHistoryMs = 1000; |
| 56 | |
buildbot@webrtc.org | 933d88a | 2014-09-18 20:23:05 +0000 | [diff] [blame] | 57 | static const int kDefaultQpMax = 56; |
| 58 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 59 | static const int kDefaultRtcpReceiverReportSsrc = 1; |
| 60 | |
| 61 | struct VideoCodecPref { |
| 62 | int payload_type; |
pbos@webrtc.org | 97fdeb8 | 2014-08-22 10:36:23 +0000 | [diff] [blame] | 63 | int width; |
| 64 | int height; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 65 | const char* name; |
| 66 | int rtx_payload_type; |
pbos@webrtc.org | 97fdeb8 | 2014-08-22 10:36:23 +0000 | [diff] [blame] | 67 | } kDefaultVideoCodecPref = {100, 640, 400, kVp8CodecName, 96}; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 68 | |
pbos@webrtc.org | 97fdeb8 | 2014-08-22 10:36:23 +0000 | [diff] [blame] | 69 | VideoCodecPref kRedPref = {116, -1, -1, kRedCodecName, -1}; |
| 70 | VideoCodecPref kUlpfecPref = {117, -1, -1, kUlpfecCodecName, -1}; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 71 | |
| 72 | static bool FindFirstMatchingCodec(const std::vector<VideoCodec>& codecs, |
| 73 | const VideoCodec& requested_codec, |
| 74 | VideoCodec* matching_codec) { |
| 75 | for (size_t i = 0; i < codecs.size(); ++i) { |
| 76 | if (requested_codec.Matches(codecs[i])) { |
| 77 | *matching_codec = codecs[i]; |
| 78 | return true; |
| 79 | } |
| 80 | } |
| 81 | return false; |
| 82 | } |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 83 | |
pbos@webrtc.org | f99c2f2 | 2014-06-13 12:27:38 +0000 | [diff] [blame] | 84 | static void AddDefaultFeedbackParams(VideoCodec* codec) { |
| 85 | const FeedbackParam kFir(kRtcpFbParamCcm, kRtcpFbCcmParamFir); |
| 86 | codec->AddFeedbackParam(kFir); |
| 87 | const FeedbackParam kNack(kRtcpFbParamNack, kParamValueEmpty); |
| 88 | codec->AddFeedbackParam(kNack); |
| 89 | const FeedbackParam kPli(kRtcpFbParamNack, kRtcpFbNackParamPli); |
| 90 | codec->AddFeedbackParam(kPli); |
| 91 | const FeedbackParam kRemb(kRtcpFbParamRemb, kParamValueEmpty); |
| 92 | codec->AddFeedbackParam(kRemb); |
| 93 | } |
| 94 | |
| 95 | static bool IsNackEnabled(const VideoCodec& codec) { |
| 96 | return codec.HasFeedbackParam( |
| 97 | FeedbackParam(kRtcpFbParamNack, kParamValueEmpty)); |
| 98 | } |
| 99 | |
pbos@webrtc.org | 257e130 | 2014-07-25 19:01:32 +0000 | [diff] [blame] | 100 | static bool IsRembEnabled(const VideoCodec& codec) { |
| 101 | return codec.HasFeedbackParam( |
| 102 | FeedbackParam(kRtcpFbParamRemb, kParamValueEmpty)); |
| 103 | } |
| 104 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 105 | static VideoCodec DefaultVideoCodec() { |
| 106 | VideoCodec default_codec(kDefaultVideoCodecPref.payload_type, |
| 107 | kDefaultVideoCodecPref.name, |
pbos@webrtc.org | 97fdeb8 | 2014-08-22 10:36:23 +0000 | [diff] [blame] | 108 | kDefaultVideoCodecPref.width, |
| 109 | kDefaultVideoCodecPref.height, |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 110 | kDefaultFramerate, |
| 111 | 0); |
pbos@webrtc.org | f99c2f2 | 2014-06-13 12:27:38 +0000 | [diff] [blame] | 112 | AddDefaultFeedbackParams(&default_codec); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 113 | return default_codec; |
| 114 | } |
| 115 | |
| 116 | static VideoCodec DefaultRedCodec() { |
| 117 | return VideoCodec(kRedPref.payload_type, kRedPref.name, 0, 0, 0, 0); |
| 118 | } |
| 119 | |
| 120 | static VideoCodec DefaultUlpfecCodec() { |
| 121 | return VideoCodec(kUlpfecPref.payload_type, kUlpfecPref.name, 0, 0, 0, 0); |
| 122 | } |
| 123 | |
| 124 | static std::vector<VideoCodec> DefaultVideoCodecs() { |
| 125 | std::vector<VideoCodec> codecs; |
| 126 | codecs.push_back(DefaultVideoCodec()); |
| 127 | codecs.push_back(DefaultRedCodec()); |
| 128 | codecs.push_back(DefaultUlpfecCodec()); |
| 129 | if (kDefaultVideoCodecPref.rtx_payload_type != -1) { |
| 130 | codecs.push_back( |
| 131 | VideoCodec::CreateRtxCodec(kDefaultVideoCodecPref.rtx_payload_type, |
| 132 | kDefaultVideoCodecPref.payload_type)); |
| 133 | } |
| 134 | return codecs; |
| 135 | } |
| 136 | |
pbos@webrtc.org | 3c10758 | 2014-07-20 15:27:35 +0000 | [diff] [blame] | 137 | static bool ValidateRtpHeaderExtensionIds( |
| 138 | const std::vector<RtpHeaderExtension>& extensions) { |
| 139 | std::set<int> extensions_used; |
| 140 | for (size_t i = 0; i < extensions.size(); ++i) { |
| 141 | if (extensions[i].id < 0 || extensions[i].id >= 15 || |
| 142 | !extensions_used.insert(extensions[i].id).second) { |
| 143 | LOG(LS_ERROR) << "RTP extensions are with incorrect or duplicate ids."; |
| 144 | return false; |
| 145 | } |
| 146 | } |
| 147 | return true; |
| 148 | } |
| 149 | |
| 150 | static std::vector<webrtc::RtpExtension> FilterRtpExtensions( |
| 151 | const std::vector<RtpHeaderExtension>& extensions) { |
| 152 | std::vector<webrtc::RtpExtension> webrtc_extensions; |
| 153 | for (size_t i = 0; i < extensions.size(); ++i) { |
| 154 | // Unsupported extensions will be ignored. |
| 155 | if (webrtc::RtpExtension::IsSupported(extensions[i].uri)) { |
| 156 | webrtc_extensions.push_back(webrtc::RtpExtension( |
| 157 | extensions[i].uri, extensions[i].id)); |
| 158 | } else { |
| 159 | LOG(LS_WARNING) << "Unsupported RTP extension: " << extensions[i].uri; |
| 160 | } |
| 161 | } |
| 162 | return webrtc_extensions; |
| 163 | } |
| 164 | |
pbos@webrtc.org | 0d523ee | 2014-06-05 09:10:55 +0000 | [diff] [blame] | 165 | WebRtcVideoEncoderFactory2::~WebRtcVideoEncoderFactory2() { |
| 166 | } |
| 167 | |
buildbot@webrtc.org | d41eaeb | 2014-06-12 07:13:26 +0000 | [diff] [blame] | 168 | std::vector<webrtc::VideoStream> WebRtcVideoEncoderFactory2::CreateVideoStreams( |
| 169 | const VideoCodec& codec, |
| 170 | const VideoOptions& options, |
| 171 | size_t num_streams) { |
| 172 | assert(SupportsCodec(codec)); |
| 173 | if (num_streams != 1) { |
| 174 | LOG(LS_ERROR) << "Unsupported number of streams: " << num_streams; |
| 175 | return std::vector<webrtc::VideoStream>(); |
pbos@webrtc.org | 6ae48c6 | 2014-06-06 10:49:19 +0000 | [diff] [blame] | 176 | } |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 177 | |
buildbot@webrtc.org | d41eaeb | 2014-06-12 07:13:26 +0000 | [diff] [blame] | 178 | webrtc::VideoStream stream; |
| 179 | stream.width = codec.width; |
| 180 | stream.height = codec.height; |
| 181 | stream.max_framerate = |
| 182 | codec.framerate != 0 ? codec.framerate : kDefaultFramerate; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 183 | |
buildbot@webrtc.org | d41eaeb | 2014-06-12 07:13:26 +0000 | [diff] [blame] | 184 | int min_bitrate = kMinVideoBitrate; |
| 185 | codec.GetParam(kCodecParamMinBitrate, &min_bitrate); |
| 186 | int max_bitrate = kMaxVideoBitrate; |
| 187 | codec.GetParam(kCodecParamMaxBitrate, &max_bitrate); |
| 188 | stream.min_bitrate_bps = min_bitrate * 1000; |
| 189 | stream.target_bitrate_bps = stream.max_bitrate_bps = max_bitrate * 1000; |
| 190 | |
buildbot@webrtc.org | 933d88a | 2014-09-18 20:23:05 +0000 | [diff] [blame] | 191 | int max_qp = kDefaultQpMax; |
buildbot@webrtc.org | d41eaeb | 2014-06-12 07:13:26 +0000 | [diff] [blame] | 192 | codec.GetParam(kCodecParamMaxQuantization, &max_qp); |
| 193 | stream.max_qp = max_qp; |
| 194 | std::vector<webrtc::VideoStream> streams; |
| 195 | streams.push_back(stream); |
| 196 | return streams; |
| 197 | } |
| 198 | |
| 199 | webrtc::VideoEncoder* WebRtcVideoEncoderFactory2::CreateVideoEncoder( |
| 200 | const VideoCodec& codec, |
| 201 | const VideoOptions& options) { |
| 202 | assert(SupportsCodec(codec)); |
pbos@webrtc.org | 6f48f1b | 2014-07-22 16:29:54 +0000 | [diff] [blame] | 203 | if (_stricmp(codec.name.c_str(), kVp8CodecName) == 0) { |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 204 | return webrtc::VideoEncoder::Create(webrtc::VideoEncoder::kVp8); |
pbos@webrtc.org | 6f48f1b | 2014-07-22 16:29:54 +0000 | [diff] [blame] | 205 | } |
| 206 | // This shouldn't happen, we should be able to create encoders for all codecs |
| 207 | // we support. |
| 208 | assert(false); |
| 209 | return NULL; |
| 210 | } |
| 211 | |
| 212 | void* WebRtcVideoEncoderFactory2::CreateVideoEncoderSettings( |
| 213 | const VideoCodec& codec, |
| 214 | const VideoOptions& options) { |
| 215 | assert(SupportsCodec(codec)); |
| 216 | if (_stricmp(codec.name.c_str(), kVp8CodecName) == 0) { |
pbos@webrtc.org | 6cd6ba8 | 2014-09-18 12:42:28 +0000 | [diff] [blame] | 217 | webrtc::VideoCodecVP8* settings = new webrtc::VideoCodecVP8( |
| 218 | webrtc::VideoEncoder::GetDefaultVp8Settings()); |
pbos@webrtc.org | 6f48f1b | 2014-07-22 16:29:54 +0000 | [diff] [blame] | 219 | options.video_noise_reduction.Get(&settings->denoisingOn); |
pbos@webrtc.org | 6f48f1b | 2014-07-22 16:29:54 +0000 | [diff] [blame] | 220 | return settings; |
| 221 | } |
| 222 | return NULL; |
| 223 | } |
| 224 | |
| 225 | void WebRtcVideoEncoderFactory2::DestroyVideoEncoderSettings( |
| 226 | const VideoCodec& codec, |
| 227 | void* encoder_settings) { |
| 228 | assert(SupportsCodec(codec)); |
| 229 | if (encoder_settings == NULL) { |
| 230 | return; |
| 231 | } |
pbos@webrtc.org | 6f48f1b | 2014-07-22 16:29:54 +0000 | [diff] [blame] | 232 | if (_stricmp(codec.name.c_str(), kVp8CodecName) == 0) { |
| 233 | delete reinterpret_cast<webrtc::VideoCodecVP8*>(encoder_settings); |
pbos@webrtc.org | 6f48f1b | 2014-07-22 16:29:54 +0000 | [diff] [blame] | 234 | } |
buildbot@webrtc.org | d41eaeb | 2014-06-12 07:13:26 +0000 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | bool WebRtcVideoEncoderFactory2::SupportsCodec(const VideoCodec& codec) { |
buildbot@webrtc.org | df9bbbe | 2014-06-19 19:54:33 +0000 | [diff] [blame] | 238 | return _stricmp(codec.name.c_str(), kVp8CodecName) == 0; |
buildbot@webrtc.org | d41eaeb | 2014-06-12 07:13:26 +0000 | [diff] [blame] | 239 | } |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 240 | |
pbos@webrtc.org | afb554f4 | 2014-08-12 23:17:13 +0000 | [diff] [blame] | 241 | DefaultUnsignalledSsrcHandler::DefaultUnsignalledSsrcHandler() |
| 242 | : default_recv_ssrc_(0), default_renderer_(NULL) {} |
| 243 | |
| 244 | UnsignalledSsrcHandler::Action DefaultUnsignalledSsrcHandler::OnUnsignalledSsrc( |
| 245 | VideoMediaChannel* channel, |
| 246 | uint32_t ssrc) { |
| 247 | if (default_recv_ssrc_ != 0) { // Already one default stream. |
| 248 | LOG(LS_WARNING) << "Unknown SSRC, but default receive stream already set."; |
| 249 | return kDropPacket; |
| 250 | } |
| 251 | |
| 252 | StreamParams sp; |
| 253 | sp.ssrcs.push_back(ssrc); |
| 254 | LOG(LS_INFO) << "Creating default receive stream for SSRC=" << ssrc << "."; |
| 255 | if (!channel->AddRecvStream(sp)) { |
| 256 | LOG(LS_WARNING) << "Could not create default receive stream."; |
| 257 | } |
| 258 | |
| 259 | channel->SetRenderer(ssrc, default_renderer_); |
| 260 | default_recv_ssrc_ = ssrc; |
| 261 | return kDeliverPacket; |
| 262 | } |
| 263 | |
| 264 | VideoRenderer* DefaultUnsignalledSsrcHandler::GetDefaultRenderer() const { |
| 265 | return default_renderer_; |
| 266 | } |
| 267 | |
| 268 | void DefaultUnsignalledSsrcHandler::SetDefaultRenderer( |
| 269 | VideoMediaChannel* channel, |
| 270 | VideoRenderer* renderer) { |
| 271 | default_renderer_ = renderer; |
| 272 | if (default_recv_ssrc_ != 0) { |
| 273 | channel->SetRenderer(default_recv_ssrc_, default_renderer_); |
| 274 | } |
| 275 | } |
| 276 | |
pbos@webrtc.org | 97fdeb8 | 2014-08-22 10:36:23 +0000 | [diff] [blame] | 277 | WebRtcVideoEngine2::WebRtcVideoEngine2() |
pbos@webrtc.org | b648b9d | 2014-08-26 11:08:06 +0000 | [diff] [blame] | 278 | : worker_thread_(NULL), |
| 279 | voice_engine_(NULL), |
| 280 | video_codecs_(DefaultVideoCodecs()), |
buildbot@webrtc.org | 992febb | 2014-09-05 16:39:08 +0000 | [diff] [blame] | 281 | default_codec_format_(kDefaultVideoCodecPref.width, |
| 282 | kDefaultVideoCodecPref.height, |
| 283 | FPS_TO_INTERVAL(kDefaultFramerate), |
| 284 | FOURCC_ANY), |
pbos@webrtc.org | b648b9d | 2014-08-26 11:08:06 +0000 | [diff] [blame] | 285 | initialized_(false), |
| 286 | cpu_monitor_(new rtc::CpuMonitor(NULL)), |
| 287 | channel_factory_(NULL) { |
| 288 | LOG(LS_INFO) << "WebRtcVideoEngine2::WebRtcVideoEngine2()"; |
pbos@webrtc.org | 587ef60 | 2014-06-16 17:32:02 +0000 | [diff] [blame] | 289 | rtp_header_extensions_.push_back( |
| 290 | RtpHeaderExtension(kRtpTimestampOffsetHeaderExtension, |
| 291 | kRtpTimestampOffsetHeaderExtensionDefaultId)); |
| 292 | rtp_header_extensions_.push_back( |
| 293 | RtpHeaderExtension(kRtpAbsoluteSenderTimeHeaderExtension, |
| 294 | kRtpAbsoluteSenderTimeHeaderExtensionDefaultId)); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 295 | } |
| 296 | |
pbos@webrtc.org | b648b9d | 2014-08-26 11:08:06 +0000 | [diff] [blame] | 297 | void WebRtcVideoEngine2::SetChannelFactory( |
| 298 | WebRtcVideoChannelFactory* channel_factory) { |
| 299 | channel_factory_ = channel_factory; |
| 300 | } |
| 301 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 302 | WebRtcVideoEngine2::~WebRtcVideoEngine2() { |
| 303 | LOG(LS_INFO) << "WebRtcVideoEngine2::~WebRtcVideoEngine2"; |
| 304 | |
| 305 | if (initialized_) { |
| 306 | Terminate(); |
| 307 | } |
| 308 | } |
| 309 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 310 | bool WebRtcVideoEngine2::Init(rtc::Thread* worker_thread) { |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 311 | LOG(LS_INFO) << "WebRtcVideoEngine2::Init"; |
| 312 | worker_thread_ = worker_thread; |
| 313 | ASSERT(worker_thread_ != NULL); |
| 314 | |
| 315 | cpu_monitor_->set_thread(worker_thread_); |
| 316 | if (!cpu_monitor_->Start(kCpuMonitorPeriodMs)) { |
| 317 | LOG(LS_ERROR) << "Failed to start CPU monitor."; |
| 318 | cpu_monitor_.reset(); |
| 319 | } |
| 320 | |
| 321 | initialized_ = true; |
| 322 | return true; |
| 323 | } |
| 324 | |
| 325 | void WebRtcVideoEngine2::Terminate() { |
| 326 | LOG(LS_INFO) << "WebRtcVideoEngine2::Terminate"; |
| 327 | |
| 328 | cpu_monitor_->Stop(); |
| 329 | |
| 330 | initialized_ = false; |
| 331 | } |
| 332 | |
| 333 | int WebRtcVideoEngine2::GetCapabilities() { return VIDEO_RECV | VIDEO_SEND; } |
| 334 | |
| 335 | bool WebRtcVideoEngine2::SetOptions(const VideoOptions& options) { |
| 336 | // TODO(pbos): Do we need this? This is a no-op in the existing |
| 337 | // WebRtcVideoEngine implementation. |
| 338 | LOG(LS_VERBOSE) << "SetOptions: " << options.ToString(); |
| 339 | // options_ = options; |
| 340 | return true; |
| 341 | } |
| 342 | |
| 343 | bool WebRtcVideoEngine2::SetDefaultEncoderConfig( |
| 344 | const VideoEncoderConfig& config) { |
pbos@webrtc.org | 8fdeee6 | 2014-07-20 14:40:23 +0000 | [diff] [blame] | 345 | const VideoCodec& codec = config.max_codec; |
| 346 | // TODO(pbos): Make use of external encoder factory. |
| 347 | if (!GetVideoEncoderFactory()->SupportsCodec(codec)) { |
| 348 | LOG(LS_ERROR) << "SetDefaultEncoderConfig, codec not supported:" |
| 349 | << codec.ToString(); |
| 350 | return false; |
| 351 | } |
| 352 | |
buildbot@webrtc.org | 992febb | 2014-09-05 16:39:08 +0000 | [diff] [blame] | 353 | default_codec_format_ = |
| 354 | VideoFormat(codec.width, |
| 355 | codec.height, |
| 356 | VideoFormat::FpsToInterval(codec.framerate), |
| 357 | FOURCC_ANY); |
pbos@webrtc.org | 8fdeee6 | 2014-07-20 14:40:23 +0000 | [diff] [blame] | 358 | video_codecs_.clear(); |
| 359 | video_codecs_.push_back(codec); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 360 | return true; |
| 361 | } |
| 362 | |
| 363 | VideoEncoderConfig WebRtcVideoEngine2::GetDefaultEncoderConfig() const { |
| 364 | return VideoEncoderConfig(DefaultVideoCodec()); |
| 365 | } |
| 366 | |
| 367 | WebRtcVideoChannel2* WebRtcVideoEngine2::CreateChannel( |
| 368 | VoiceMediaChannel* voice_channel) { |
| 369 | LOG(LS_INFO) << "CreateChannel: " |
| 370 | << (voice_channel != NULL ? "With" : "Without") |
| 371 | << " voice channel."; |
| 372 | WebRtcVideoChannel2* channel = |
| 373 | channel_factory_ != NULL |
| 374 | ? channel_factory_->Create(this, voice_channel) |
| 375 | : new WebRtcVideoChannel2( |
pbos@webrtc.org | 0d523ee | 2014-06-05 09:10:55 +0000 | [diff] [blame] | 376 | this, voice_channel, GetVideoEncoderFactory()); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 377 | if (!channel->Init()) { |
| 378 | delete channel; |
| 379 | return NULL; |
| 380 | } |
pbos@webrtc.org | e322a17 | 2014-06-13 11:47:28 +0000 | [diff] [blame] | 381 | channel->SetRecvCodecs(video_codecs_); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 382 | return channel; |
| 383 | } |
| 384 | |
| 385 | const std::vector<VideoCodec>& WebRtcVideoEngine2::codecs() const { |
| 386 | return video_codecs_; |
| 387 | } |
| 388 | |
| 389 | const std::vector<RtpHeaderExtension>& |
| 390 | WebRtcVideoEngine2::rtp_header_extensions() const { |
| 391 | return rtp_header_extensions_; |
| 392 | } |
| 393 | |
| 394 | void WebRtcVideoEngine2::SetLogging(int min_sev, const char* filter) { |
| 395 | // TODO(pbos): Set up logging. |
| 396 | LOG(LS_VERBOSE) << "SetLogging: " << min_sev << '"' << filter << '"'; |
| 397 | // if min_sev == -1, we keep the current log level. |
| 398 | if (min_sev < 0) { |
| 399 | assert(min_sev == -1); |
| 400 | return; |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | bool WebRtcVideoEngine2::EnableTimedRender() { |
| 405 | // TODO(pbos): Figure out whether this can be removed. |
| 406 | return true; |
| 407 | } |
| 408 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 409 | // Checks to see whether we comprehend and could receive a particular codec |
| 410 | bool WebRtcVideoEngine2::FindCodec(const VideoCodec& in) { |
| 411 | // TODO(pbos): Probe encoder factory to figure out that the codec is supported |
| 412 | // if supported by the encoder factory. Add a corresponding test that fails |
| 413 | // with this code (that doesn't ask the factory). |
pbos@webrtc.org | 8fdeee6 | 2014-07-20 14:40:23 +0000 | [diff] [blame] | 414 | for (size_t j = 0; j < video_codecs_.size(); ++j) { |
| 415 | VideoCodec codec(video_codecs_[j].id, video_codecs_[j].name, 0, 0, 0, 0); |
| 416 | if (codec.Matches(in)) { |
| 417 | return true; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 418 | } |
| 419 | } |
| 420 | return false; |
| 421 | } |
| 422 | |
| 423 | // Tells whether the |requested| codec can be transmitted or not. If it can be |
| 424 | // transmitted |out| is set with the best settings supported. Aspect ratio will |
| 425 | // be set as close to |current|'s as possible. If not set |requested|'s |
| 426 | // dimensions will be used for aspect ratio matching. |
| 427 | bool WebRtcVideoEngine2::CanSendCodec(const VideoCodec& requested, |
| 428 | const VideoCodec& current, |
| 429 | VideoCodec* out) { |
| 430 | assert(out != NULL); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 431 | |
| 432 | if (requested.width != requested.height && |
| 433 | (requested.height == 0 || requested.width == 0)) { |
| 434 | // 0xn and nx0 are invalid resolutions. |
| 435 | return false; |
| 436 | } |
| 437 | |
| 438 | VideoCodec matching_codec; |
| 439 | if (!FindFirstMatchingCodec(video_codecs_, requested, &matching_codec)) { |
| 440 | // Codec not supported. |
| 441 | return false; |
| 442 | } |
| 443 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 444 | out->id = requested.id; |
| 445 | out->name = requested.name; |
| 446 | out->preference = requested.preference; |
| 447 | out->params = requested.params; |
| 448 | out->framerate = |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 449 | rtc::_min(requested.framerate, matching_codec.framerate); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 450 | out->params = requested.params; |
| 451 | out->feedback_params = requested.feedback_params; |
pbos@webrtc.org | 8fdeee6 | 2014-07-20 14:40:23 +0000 | [diff] [blame] | 452 | out->width = requested.width; |
| 453 | out->height = requested.height; |
| 454 | if (requested.width == 0 && requested.height == 0) { |
| 455 | return true; |
| 456 | } |
| 457 | |
| 458 | while (out->width > matching_codec.width) { |
| 459 | out->width /= 2; |
| 460 | out->height /= 2; |
| 461 | } |
| 462 | |
| 463 | return out->width > 0 && out->height > 0; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | bool WebRtcVideoEngine2::SetVoiceEngine(WebRtcVoiceEngine* voice_engine) { |
| 467 | if (initialized_) { |
| 468 | LOG(LS_WARNING) << "SetVoiceEngine can not be called after Init"; |
| 469 | return false; |
| 470 | } |
| 471 | voice_engine_ = voice_engine; |
| 472 | return true; |
| 473 | } |
| 474 | |
| 475 | // Ignore spammy trace messages, mostly from the stats API when we haven't |
| 476 | // gotten RTCP info yet from the remote side. |
| 477 | bool WebRtcVideoEngine2::ShouldIgnoreTrace(const std::string& trace) { |
| 478 | static const char* const kTracesToIgnore[] = {NULL}; |
| 479 | for (const char* const* p = kTracesToIgnore; *p; ++p) { |
| 480 | if (trace.find(*p) == 0) { |
| 481 | return true; |
| 482 | } |
| 483 | } |
| 484 | return false; |
| 485 | } |
| 486 | |
buildbot@webrtc.org | d41eaeb | 2014-06-12 07:13:26 +0000 | [diff] [blame] | 487 | WebRtcVideoEncoderFactory2* WebRtcVideoEngine2::GetVideoEncoderFactory() { |
| 488 | return &default_video_encoder_factory_; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 489 | } |
| 490 | |
pbos@webrtc.org | 6ae48c6 | 2014-06-06 10:49:19 +0000 | [diff] [blame] | 491 | // Thin map between VideoFrame and an existing webrtc::I420VideoFrame |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 492 | // to avoid having to copy the rendered VideoFrame prematurely. |
| 493 | // This implementation is only safe to use in a const context and should never |
| 494 | // be written to. |
pbos@webrtc.org | 6ae48c6 | 2014-06-06 10:49:19 +0000 | [diff] [blame] | 495 | class WebRtcVideoRenderFrame : public VideoFrame { |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 496 | public: |
| 497 | explicit WebRtcVideoRenderFrame(const webrtc::I420VideoFrame* frame) |
| 498 | : frame_(frame) {} |
| 499 | |
| 500 | virtual bool InitToBlack(int w, |
| 501 | int h, |
| 502 | size_t pixel_width, |
| 503 | size_t pixel_height, |
| 504 | int64 elapsed_time, |
| 505 | int64 time_stamp) OVERRIDE { |
| 506 | UNIMPLEMENTED; |
| 507 | return false; |
| 508 | } |
| 509 | |
| 510 | virtual bool Reset(uint32 fourcc, |
| 511 | int w, |
| 512 | int h, |
| 513 | int dw, |
| 514 | int dh, |
| 515 | uint8* sample, |
| 516 | size_t sample_size, |
| 517 | size_t pixel_width, |
| 518 | size_t pixel_height, |
| 519 | int64 elapsed_time, |
| 520 | int64 time_stamp, |
| 521 | int rotation) OVERRIDE { |
| 522 | UNIMPLEMENTED; |
| 523 | return false; |
| 524 | } |
| 525 | |
| 526 | virtual size_t GetWidth() const OVERRIDE { |
| 527 | return static_cast<size_t>(frame_->width()); |
| 528 | } |
| 529 | virtual size_t GetHeight() const OVERRIDE { |
| 530 | return static_cast<size_t>(frame_->height()); |
| 531 | } |
| 532 | |
| 533 | virtual const uint8* GetYPlane() const OVERRIDE { |
| 534 | return frame_->buffer(webrtc::kYPlane); |
| 535 | } |
| 536 | virtual const uint8* GetUPlane() const OVERRIDE { |
| 537 | return frame_->buffer(webrtc::kUPlane); |
| 538 | } |
| 539 | virtual const uint8* GetVPlane() const OVERRIDE { |
| 540 | return frame_->buffer(webrtc::kVPlane); |
| 541 | } |
| 542 | |
| 543 | virtual uint8* GetYPlane() OVERRIDE { |
| 544 | UNIMPLEMENTED; |
| 545 | return NULL; |
| 546 | } |
| 547 | virtual uint8* GetUPlane() OVERRIDE { |
| 548 | UNIMPLEMENTED; |
| 549 | return NULL; |
| 550 | } |
| 551 | virtual uint8* GetVPlane() OVERRIDE { |
| 552 | UNIMPLEMENTED; |
| 553 | return NULL; |
| 554 | } |
| 555 | |
| 556 | virtual int32 GetYPitch() const OVERRIDE { |
| 557 | return frame_->stride(webrtc::kYPlane); |
| 558 | } |
| 559 | virtual int32 GetUPitch() const OVERRIDE { |
| 560 | return frame_->stride(webrtc::kUPlane); |
| 561 | } |
| 562 | virtual int32 GetVPitch() const OVERRIDE { |
| 563 | return frame_->stride(webrtc::kVPlane); |
| 564 | } |
| 565 | |
| 566 | virtual void* GetNativeHandle() const OVERRIDE { return NULL; } |
| 567 | |
| 568 | virtual size_t GetPixelWidth() const OVERRIDE { return 1; } |
| 569 | virtual size_t GetPixelHeight() const OVERRIDE { return 1; } |
| 570 | |
| 571 | virtual int64 GetElapsedTime() const OVERRIDE { |
| 572 | // Convert millisecond render time to ns timestamp. |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 573 | return frame_->render_time_ms() * rtc::kNumNanosecsPerMillisec; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 574 | } |
| 575 | virtual int64 GetTimeStamp() const OVERRIDE { |
| 576 | // Convert 90K rtp timestamp to ns timestamp. |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 577 | return (frame_->timestamp() / 90) * rtc::kNumNanosecsPerMillisec; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 578 | } |
| 579 | virtual void SetElapsedTime(int64 elapsed_time) OVERRIDE { UNIMPLEMENTED; } |
| 580 | virtual void SetTimeStamp(int64 time_stamp) OVERRIDE { UNIMPLEMENTED; } |
| 581 | |
| 582 | virtual int GetRotation() const OVERRIDE { |
| 583 | UNIMPLEMENTED; |
| 584 | return ROTATION_0; |
| 585 | } |
| 586 | |
| 587 | virtual VideoFrame* Copy() const OVERRIDE { |
| 588 | UNIMPLEMENTED; |
| 589 | return NULL; |
| 590 | } |
| 591 | |
| 592 | virtual bool MakeExclusive() OVERRIDE { |
| 593 | UNIMPLEMENTED; |
| 594 | return false; |
| 595 | } |
| 596 | |
| 597 | virtual size_t CopyToBuffer(uint8* buffer, size_t size) const { |
| 598 | UNIMPLEMENTED; |
| 599 | return 0; |
| 600 | } |
| 601 | |
| 602 | // TODO(fbarchard): Refactor into base class and share with LMI |
| 603 | virtual size_t ConvertToRgbBuffer(uint32 to_fourcc, |
| 604 | uint8* buffer, |
| 605 | size_t size, |
| 606 | int stride_rgb) const OVERRIDE { |
| 607 | size_t width = GetWidth(); |
| 608 | size_t height = GetHeight(); |
| 609 | size_t needed = (stride_rgb >= 0 ? stride_rgb : -stride_rgb) * height; |
| 610 | if (size < needed) { |
| 611 | LOG(LS_WARNING) << "RGB buffer is not large enough"; |
| 612 | return needed; |
| 613 | } |
| 614 | |
| 615 | if (libyuv::ConvertFromI420(GetYPlane(), |
| 616 | GetYPitch(), |
| 617 | GetUPlane(), |
| 618 | GetUPitch(), |
| 619 | GetVPlane(), |
| 620 | GetVPitch(), |
| 621 | buffer, |
| 622 | stride_rgb, |
| 623 | static_cast<int>(width), |
| 624 | static_cast<int>(height), |
| 625 | to_fourcc)) { |
| 626 | LOG(LS_ERROR) << "RGB type not supported: " << to_fourcc; |
| 627 | return 0; // 0 indicates error |
| 628 | } |
| 629 | return needed; |
| 630 | } |
| 631 | |
| 632 | protected: |
| 633 | virtual VideoFrame* CreateEmptyFrame(int w, |
| 634 | int h, |
| 635 | size_t pixel_width, |
| 636 | size_t pixel_height, |
| 637 | int64 elapsed_time, |
| 638 | int64 time_stamp) const OVERRIDE { |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 639 | WebRtcVideoFrame* frame = new WebRtcVideoFrame(); |
| 640 | frame->InitToBlack( |
| 641 | w, h, pixel_width, pixel_height, elapsed_time, time_stamp); |
| 642 | return frame; |
| 643 | } |
| 644 | |
| 645 | private: |
| 646 | const webrtc::I420VideoFrame* const frame_; |
| 647 | }; |
| 648 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 649 | WebRtcVideoChannel2::WebRtcVideoChannel2( |
| 650 | WebRtcVideoEngine2* engine, |
| 651 | VoiceMediaChannel* voice_channel, |
| 652 | WebRtcVideoEncoderFactory2* encoder_factory) |
pbos@webrtc.org | b648b9d | 2014-08-26 11:08:06 +0000 | [diff] [blame] | 653 | : unsignalled_ssrc_handler_(&default_unsignalled_ssrc_handler_), |
| 654 | encoder_factory_(encoder_factory) { |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 655 | // TODO(pbos): Connect the video and audio with |voice_channel|. |
| 656 | webrtc::Call::Config config(this); |
| 657 | Construct(webrtc::Call::Create(config), engine); |
| 658 | } |
| 659 | |
| 660 | WebRtcVideoChannel2::WebRtcVideoChannel2( |
| 661 | webrtc::Call* call, |
| 662 | WebRtcVideoEngine2* engine, |
| 663 | WebRtcVideoEncoderFactory2* encoder_factory) |
pbos@webrtc.org | b648b9d | 2014-08-26 11:08:06 +0000 | [diff] [blame] | 664 | : unsignalled_ssrc_handler_(&default_unsignalled_ssrc_handler_), |
| 665 | encoder_factory_(encoder_factory) { |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 666 | Construct(call, engine); |
| 667 | } |
| 668 | |
| 669 | void WebRtcVideoChannel2::Construct(webrtc::Call* call, |
| 670 | WebRtcVideoEngine2* engine) { |
| 671 | rtcp_receiver_report_ssrc_ = kDefaultRtcpReceiverReportSsrc; |
| 672 | sending_ = false; |
| 673 | call_.reset(call); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 674 | default_send_ssrc_ = 0; |
pbos@webrtc.org | 6f48f1b | 2014-07-22 16:29:54 +0000 | [diff] [blame] | 675 | |
| 676 | SetDefaultOptions(); |
| 677 | } |
| 678 | |
| 679 | void WebRtcVideoChannel2::SetDefaultOptions() { |
| 680 | options_.video_noise_reduction.Set(true); |
pbos@webrtc.org | 543e589 | 2014-07-23 07:01:31 +0000 | [diff] [blame] | 681 | options_.use_payload_padding.Set(false); |
pbos@webrtc.org | 5ff71ab | 2014-07-23 07:28:56 +0000 | [diff] [blame] | 682 | options_.suspend_below_min_bitrate.Set(false); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 683 | } |
| 684 | |
| 685 | WebRtcVideoChannel2::~WebRtcVideoChannel2() { |
| 686 | for (std::map<uint32, WebRtcVideoSendStream*>::iterator it = |
| 687 | send_streams_.begin(); |
| 688 | it != send_streams_.end(); |
| 689 | ++it) { |
| 690 | delete it->second; |
| 691 | } |
| 692 | |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 693 | for (std::map<uint32, WebRtcVideoReceiveStream*>::iterator it = |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 694 | receive_streams_.begin(); |
| 695 | it != receive_streams_.end(); |
| 696 | ++it) { |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 697 | delete it->second; |
| 698 | } |
| 699 | } |
| 700 | |
| 701 | bool WebRtcVideoChannel2::Init() { return true; } |
| 702 | |
| 703 | namespace { |
| 704 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 705 | static std::string CodecVectorToString(const std::vector<VideoCodec>& codecs) { |
| 706 | std::stringstream out; |
| 707 | out << '{'; |
| 708 | for (size_t i = 0; i < codecs.size(); ++i) { |
| 709 | out << codecs[i].ToString(); |
| 710 | if (i != codecs.size() - 1) { |
| 711 | out << ", "; |
| 712 | } |
| 713 | } |
| 714 | out << '}'; |
| 715 | return out.str(); |
| 716 | } |
| 717 | |
pbos@webrtc.org | e322a17 | 2014-06-13 11:47:28 +0000 | [diff] [blame] | 718 | static bool ValidateCodecFormats(const std::vector<VideoCodec>& codecs) { |
| 719 | bool has_video = false; |
| 720 | for (size_t i = 0; i < codecs.size(); ++i) { |
| 721 | if (!codecs[i].ValidateCodecFormat()) { |
| 722 | return false; |
| 723 | } |
| 724 | if (codecs[i].GetCodecType() == VideoCodec::CODEC_VIDEO) { |
| 725 | has_video = true; |
| 726 | } |
| 727 | } |
| 728 | if (!has_video) { |
| 729 | LOG(LS_ERROR) << "Setting codecs without a video codec is invalid: " |
| 730 | << CodecVectorToString(codecs); |
| 731 | return false; |
| 732 | } |
| 733 | return true; |
| 734 | } |
| 735 | |
pbos@webrtc.org | 587ef60 | 2014-06-16 17:32:02 +0000 | [diff] [blame] | 736 | static std::string RtpExtensionsToString( |
| 737 | const std::vector<RtpHeaderExtension>& extensions) { |
| 738 | std::stringstream out; |
| 739 | out << '{'; |
| 740 | for (size_t i = 0; i < extensions.size(); ++i) { |
| 741 | out << "{" << extensions[i].uri << ": " << extensions[i].id << "}"; |
| 742 | if (i != extensions.size() - 1) { |
| 743 | out << ", "; |
| 744 | } |
| 745 | } |
| 746 | out << '}'; |
| 747 | return out.str(); |
| 748 | } |
| 749 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 750 | } // namespace |
| 751 | |
| 752 | bool WebRtcVideoChannel2::SetRecvCodecs(const std::vector<VideoCodec>& codecs) { |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 753 | LOG(LS_INFO) << "SetRecvCodecs: " << CodecVectorToString(codecs); |
| 754 | if (!ValidateCodecFormats(codecs)) { |
| 755 | return false; |
| 756 | } |
| 757 | |
| 758 | const std::vector<VideoCodecSettings> mapped_codecs = MapCodecs(codecs); |
| 759 | if (mapped_codecs.empty()) { |
| 760 | LOG(LS_ERROR) << "SetRecvCodecs called without video codec payloads."; |
| 761 | return false; |
| 762 | } |
| 763 | |
| 764 | // TODO(pbos): Add a decoder factory which controls supported codecs. |
| 765 | // Blocked on webrtc:2854. |
| 766 | for (size_t i = 0; i < mapped_codecs.size(); ++i) { |
buildbot@webrtc.org | df9bbbe | 2014-06-19 19:54:33 +0000 | [diff] [blame] | 767 | if (_stricmp(mapped_codecs[i].codec.name.c_str(), kVp8CodecName) != 0) { |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 768 | LOG(LS_ERROR) << "SetRecvCodecs called with unsupported codec: '" |
| 769 | << mapped_codecs[i].codec.name << "'"; |
| 770 | return false; |
| 771 | } |
| 772 | } |
| 773 | |
| 774 | recv_codecs_ = mapped_codecs; |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 775 | |
| 776 | for (std::map<uint32, WebRtcVideoReceiveStream*>::iterator it = |
| 777 | receive_streams_.begin(); |
| 778 | it != receive_streams_.end(); |
| 779 | ++it) { |
| 780 | it->second->SetRecvCodecs(recv_codecs_); |
| 781 | } |
| 782 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 783 | return true; |
| 784 | } |
| 785 | |
| 786 | bool WebRtcVideoChannel2::SetSendCodecs(const std::vector<VideoCodec>& codecs) { |
| 787 | LOG(LS_INFO) << "SetSendCodecs: " << CodecVectorToString(codecs); |
| 788 | if (!ValidateCodecFormats(codecs)) { |
| 789 | return false; |
| 790 | } |
| 791 | |
| 792 | const std::vector<VideoCodecSettings> supported_codecs = |
| 793 | FilterSupportedCodecs(MapCodecs(codecs)); |
| 794 | |
| 795 | if (supported_codecs.empty()) { |
| 796 | LOG(LS_ERROR) << "No video codecs supported by encoder factory."; |
| 797 | return false; |
| 798 | } |
| 799 | |
| 800 | send_codec_.Set(supported_codecs.front()); |
| 801 | LOG(LS_INFO) << "Using codec: " << supported_codecs.front().codec.ToString(); |
| 802 | |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 803 | for (std::map<uint32, WebRtcVideoSendStream*>::iterator it = |
| 804 | send_streams_.begin(); |
| 805 | it != send_streams_.end(); |
| 806 | ++it) { |
| 807 | assert(it->second != NULL); |
| 808 | it->second->SetCodec(supported_codecs.front()); |
| 809 | } |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 810 | |
| 811 | return true; |
| 812 | } |
| 813 | |
| 814 | bool WebRtcVideoChannel2::GetSendCodec(VideoCodec* codec) { |
| 815 | VideoCodecSettings codec_settings; |
| 816 | if (!send_codec_.Get(&codec_settings)) { |
| 817 | LOG(LS_VERBOSE) << "GetSendCodec: No send codec set."; |
| 818 | return false; |
| 819 | } |
| 820 | *codec = codec_settings.codec; |
| 821 | return true; |
| 822 | } |
| 823 | |
| 824 | bool WebRtcVideoChannel2::SetSendStreamFormat(uint32 ssrc, |
| 825 | const VideoFormat& format) { |
| 826 | LOG(LS_VERBOSE) << "SetSendStreamFormat:" << ssrc << " -> " |
| 827 | << format.ToString(); |
| 828 | if (send_streams_.find(ssrc) == send_streams_.end()) { |
| 829 | return false; |
| 830 | } |
| 831 | return send_streams_[ssrc]->SetVideoFormat(format); |
| 832 | } |
| 833 | |
| 834 | bool WebRtcVideoChannel2::SetRender(bool render) { |
| 835 | // TODO(pbos): Implement. Or refactor away as it shouldn't be needed. |
| 836 | LOG(LS_VERBOSE) << "SetRender: " << (render ? "true" : "false"); |
| 837 | return true; |
| 838 | } |
| 839 | |
| 840 | bool WebRtcVideoChannel2::SetSend(bool send) { |
| 841 | LOG(LS_VERBOSE) << "SetSend: " << (send ? "true" : "false"); |
| 842 | if (send && !send_codec_.IsSet()) { |
| 843 | LOG(LS_ERROR) << "SetSend(true) called before setting codec."; |
| 844 | return false; |
| 845 | } |
| 846 | if (send) { |
| 847 | StartAllSendStreams(); |
| 848 | } else { |
| 849 | StopAllSendStreams(); |
| 850 | } |
| 851 | sending_ = send; |
| 852 | return true; |
| 853 | } |
| 854 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 855 | bool WebRtcVideoChannel2::AddSendStream(const StreamParams& sp) { |
| 856 | LOG(LS_INFO) << "AddSendStream: " << sp.ToString(); |
| 857 | if (sp.ssrcs.empty()) { |
| 858 | LOG(LS_ERROR) << "No SSRCs in stream parameters."; |
| 859 | return false; |
| 860 | } |
| 861 | |
| 862 | uint32 ssrc = sp.first_ssrc(); |
| 863 | assert(ssrc != 0); |
| 864 | // TODO(pbos): Make sure none of sp.ssrcs are used, not just the identifying |
| 865 | // ssrc. |
| 866 | if (send_streams_.find(ssrc) != send_streams_.end()) { |
| 867 | LOG(LS_ERROR) << "Send stream with ssrc '" << ssrc << "' already exists."; |
| 868 | return false; |
| 869 | } |
| 870 | |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 871 | std::vector<uint32> primary_ssrcs; |
| 872 | sp.GetPrimarySsrcs(&primary_ssrcs); |
| 873 | std::vector<uint32> rtx_ssrcs; |
| 874 | sp.GetFidSsrcs(primary_ssrcs, &rtx_ssrcs); |
| 875 | if (!rtx_ssrcs.empty() && primary_ssrcs.size() != rtx_ssrcs.size()) { |
| 876 | LOG(LS_ERROR) |
| 877 | << "RTX SSRCs exist, but don't cover all SSRCs (unsupported): " |
| 878 | << sp.ToString(); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 879 | return false; |
| 880 | } |
| 881 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 882 | WebRtcVideoSendStream* stream = |
pbos@webrtc.org | 6ae48c6 | 2014-06-06 10:49:19 +0000 | [diff] [blame] | 883 | new WebRtcVideoSendStream(call_.get(), |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 884 | encoder_factory_, |
pbos@webrtc.org | 6ae48c6 | 2014-06-06 10:49:19 +0000 | [diff] [blame] | 885 | options_, |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 886 | send_codec_, |
| 887 | sp, |
| 888 | send_rtp_extensions_); |
| 889 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 890 | send_streams_[ssrc] = stream; |
| 891 | |
| 892 | if (rtcp_receiver_report_ssrc_ == kDefaultRtcpReceiverReportSsrc) { |
| 893 | rtcp_receiver_report_ssrc_ = ssrc; |
| 894 | } |
| 895 | if (default_send_ssrc_ == 0) { |
| 896 | default_send_ssrc_ = ssrc; |
| 897 | } |
| 898 | if (sending_) { |
| 899 | stream->Start(); |
| 900 | } |
| 901 | |
| 902 | return true; |
| 903 | } |
| 904 | |
| 905 | bool WebRtcVideoChannel2::RemoveSendStream(uint32 ssrc) { |
| 906 | LOG(LS_INFO) << "RemoveSendStream: " << ssrc; |
| 907 | |
| 908 | if (ssrc == 0) { |
| 909 | if (default_send_ssrc_ == 0) { |
| 910 | LOG(LS_ERROR) << "No default send stream active."; |
| 911 | return false; |
| 912 | } |
| 913 | |
| 914 | LOG(LS_VERBOSE) << "Removing default stream: " << default_send_ssrc_; |
| 915 | ssrc = default_send_ssrc_; |
| 916 | } |
| 917 | |
| 918 | std::map<uint32, WebRtcVideoSendStream*>::iterator it = |
| 919 | send_streams_.find(ssrc); |
| 920 | if (it == send_streams_.end()) { |
| 921 | return false; |
| 922 | } |
| 923 | |
| 924 | delete it->second; |
| 925 | send_streams_.erase(it); |
| 926 | |
| 927 | if (ssrc == default_send_ssrc_) { |
| 928 | default_send_ssrc_ = 0; |
| 929 | } |
| 930 | |
| 931 | return true; |
| 932 | } |
| 933 | |
| 934 | bool WebRtcVideoChannel2::AddRecvStream(const StreamParams& sp) { |
| 935 | LOG(LS_INFO) << "AddRecvStream: " << sp.ToString(); |
| 936 | assert(sp.ssrcs.size() > 0); |
| 937 | |
| 938 | uint32 ssrc = sp.first_ssrc(); |
| 939 | assert(ssrc != 0); // TODO(pbos): Is this ever valid? |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 940 | |
| 941 | // TODO(pbos): Check if any of the SSRCs overlap. |
| 942 | if (receive_streams_.find(ssrc) != receive_streams_.end()) { |
| 943 | LOG(LS_ERROR) << "Receive stream for SSRC " << ssrc << "already exists."; |
| 944 | return false; |
| 945 | } |
| 946 | |
pbos@webrtc.org | bd249bc | 2014-07-07 04:45:15 +0000 | [diff] [blame] | 947 | webrtc::VideoReceiveStream::Config config; |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 948 | ConfigureReceiverRtp(&config, sp); |
| 949 | receive_streams_[ssrc] = |
| 950 | new WebRtcVideoReceiveStream(call_.get(), config, recv_codecs_); |
| 951 | |
| 952 | return true; |
| 953 | } |
| 954 | |
| 955 | void WebRtcVideoChannel2::ConfigureReceiverRtp( |
| 956 | webrtc::VideoReceiveStream::Config* config, |
| 957 | const StreamParams& sp) const { |
| 958 | uint32 ssrc = sp.first_ssrc(); |
| 959 | |
| 960 | config->rtp.remote_ssrc = ssrc; |
| 961 | config->rtp.local_ssrc = rtcp_receiver_report_ssrc_; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 962 | |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 963 | config->rtp.extensions = recv_rtp_extensions_; |
pbos@webrtc.org | 257e130 | 2014-07-25 19:01:32 +0000 | [diff] [blame] | 964 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 965 | // TODO(pbos): This protection is against setting the same local ssrc as |
| 966 | // remote which is not permitted by the lower-level API. RTCP requires a |
| 967 | // corresponding sender SSRC. Figure out what to do when we don't have |
| 968 | // (receive-only) or know a good local SSRC. |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 969 | if (config->rtp.remote_ssrc == config->rtp.local_ssrc) { |
| 970 | if (config->rtp.local_ssrc != kDefaultRtcpReceiverReportSsrc) { |
| 971 | config->rtp.local_ssrc = kDefaultRtcpReceiverReportSsrc; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 972 | } else { |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 973 | config->rtp.local_ssrc = kDefaultRtcpReceiverReportSsrc + 1; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 974 | } |
| 975 | } |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 976 | |
| 977 | for (size_t i = 0; i < recv_codecs_.size(); ++i) { |
| 978 | if (recv_codecs_[i].codec.id == kDefaultVideoCodecPref.payload_type) { |
| 979 | config->rtp.fec = recv_codecs_[i].fec; |
| 980 | uint32 rtx_ssrc; |
| 981 | if (recv_codecs_[i].rtx_payload_type != -1 && |
| 982 | sp.GetFidSsrc(ssrc, &rtx_ssrc)) { |
| 983 | config->rtp.rtx[kDefaultVideoCodecPref.payload_type].ssrc = rtx_ssrc; |
| 984 | config->rtp.rtx[kDefaultVideoCodecPref.payload_type].payload_type = |
| 985 | recv_codecs_[i].rtx_payload_type; |
| 986 | } |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 987 | break; |
| 988 | } |
| 989 | } |
| 990 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 991 | } |
| 992 | |
| 993 | bool WebRtcVideoChannel2::RemoveRecvStream(uint32 ssrc) { |
| 994 | LOG(LS_INFO) << "RemoveRecvStream: " << ssrc; |
| 995 | if (ssrc == 0) { |
pbos@webrtc.org | afb554f4 | 2014-08-12 23:17:13 +0000 | [diff] [blame] | 996 | LOG(LS_ERROR) << "RemoveRecvStream with 0 ssrc is not supported."; |
| 997 | return false; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 998 | } |
| 999 | |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 1000 | std::map<uint32, WebRtcVideoReceiveStream*>::iterator stream = |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1001 | receive_streams_.find(ssrc); |
| 1002 | if (stream == receive_streams_.end()) { |
| 1003 | LOG(LS_ERROR) << "Stream not found for ssrc: " << ssrc; |
| 1004 | return false; |
| 1005 | } |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 1006 | delete stream->second; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1007 | receive_streams_.erase(stream); |
| 1008 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1009 | return true; |
| 1010 | } |
| 1011 | |
| 1012 | bool WebRtcVideoChannel2::SetRenderer(uint32 ssrc, VideoRenderer* renderer) { |
| 1013 | LOG(LS_INFO) << "SetRenderer: ssrc:" << ssrc << " " |
| 1014 | << (renderer ? "(ptr)" : "NULL"); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1015 | if (ssrc == 0) { |
pbos@webrtc.org | afb554f4 | 2014-08-12 23:17:13 +0000 | [diff] [blame] | 1016 | default_unsignalled_ssrc_handler_.SetDefaultRenderer(this, renderer); |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 1017 | return true; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1018 | } |
| 1019 | |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 1020 | std::map<uint32, WebRtcVideoReceiveStream*>::iterator it = |
| 1021 | receive_streams_.find(ssrc); |
| 1022 | if (it == receive_streams_.end()) { |
| 1023 | return false; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1024 | } |
| 1025 | |
| 1026 | it->second->SetRenderer(renderer); |
| 1027 | return true; |
| 1028 | } |
| 1029 | |
| 1030 | bool WebRtcVideoChannel2::GetRenderer(uint32 ssrc, VideoRenderer** renderer) { |
| 1031 | if (ssrc == 0) { |
pbos@webrtc.org | afb554f4 | 2014-08-12 23:17:13 +0000 | [diff] [blame] | 1032 | *renderer = default_unsignalled_ssrc_handler_.GetDefaultRenderer(); |
| 1033 | return *renderer != NULL; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1034 | } |
| 1035 | |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 1036 | std::map<uint32, WebRtcVideoReceiveStream*>::iterator it = |
| 1037 | receive_streams_.find(ssrc); |
| 1038 | if (it == receive_streams_.end()) { |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1039 | return false; |
| 1040 | } |
| 1041 | *renderer = it->second->GetRenderer(); |
| 1042 | return true; |
| 1043 | } |
| 1044 | |
| 1045 | bool WebRtcVideoChannel2::GetStats(const StatsOptions& options, |
| 1046 | VideoMediaInfo* info) { |
pbos@webrtc.org | e6f84ae | 2014-07-18 11:11:55 +0000 | [diff] [blame] | 1047 | info->Clear(); |
| 1048 | FillSenderStats(info); |
| 1049 | FillReceiverStats(info); |
| 1050 | FillBandwidthEstimationStats(info); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1051 | return true; |
| 1052 | } |
| 1053 | |
pbos@webrtc.org | e6f84ae | 2014-07-18 11:11:55 +0000 | [diff] [blame] | 1054 | void WebRtcVideoChannel2::FillSenderStats(VideoMediaInfo* video_media_info) { |
| 1055 | for (std::map<uint32, WebRtcVideoSendStream*>::iterator it = |
| 1056 | send_streams_.begin(); |
| 1057 | it != send_streams_.end(); |
| 1058 | ++it) { |
| 1059 | video_media_info->senders.push_back(it->second->GetVideoSenderInfo()); |
| 1060 | } |
| 1061 | } |
| 1062 | |
| 1063 | void WebRtcVideoChannel2::FillReceiverStats(VideoMediaInfo* video_media_info) { |
| 1064 | for (std::map<uint32, WebRtcVideoReceiveStream*>::iterator it = |
| 1065 | receive_streams_.begin(); |
| 1066 | it != receive_streams_.end(); |
| 1067 | ++it) { |
| 1068 | video_media_info->receivers.push_back(it->second->GetVideoReceiverInfo()); |
| 1069 | } |
| 1070 | } |
| 1071 | |
| 1072 | void WebRtcVideoChannel2::FillBandwidthEstimationStats( |
| 1073 | VideoMediaInfo* video_media_info) { |
| 1074 | // TODO(pbos): Implement. |
| 1075 | } |
| 1076 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1077 | bool WebRtcVideoChannel2::SetCapturer(uint32 ssrc, VideoCapturer* capturer) { |
| 1078 | LOG(LS_INFO) << "SetCapturer: " << ssrc << " -> " |
| 1079 | << (capturer != NULL ? "(capturer)" : "NULL"); |
| 1080 | assert(ssrc != 0); |
| 1081 | if (send_streams_.find(ssrc) == send_streams_.end()) { |
| 1082 | LOG(LS_ERROR) << "No sending stream on ssrc " << ssrc; |
| 1083 | return false; |
| 1084 | } |
| 1085 | return send_streams_[ssrc]->SetCapturer(capturer); |
| 1086 | } |
| 1087 | |
| 1088 | bool WebRtcVideoChannel2::SendIntraFrame() { |
| 1089 | // TODO(pbos): Implement. |
| 1090 | LOG(LS_VERBOSE) << "SendIntraFrame()."; |
| 1091 | return true; |
| 1092 | } |
| 1093 | |
| 1094 | bool WebRtcVideoChannel2::RequestIntraFrame() { |
| 1095 | // TODO(pbos): Implement. |
| 1096 | LOG(LS_VERBOSE) << "SendIntraFrame()."; |
| 1097 | return true; |
| 1098 | } |
| 1099 | |
| 1100 | void WebRtcVideoChannel2::OnPacketReceived( |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1101 | rtc::Buffer* packet, |
| 1102 | const rtc::PacketTime& packet_time) { |
pbos@webrtc.org | 4e545cc | 2014-05-14 13:58:13 +0000 | [diff] [blame] | 1103 | const webrtc::PacketReceiver::DeliveryStatus delivery_result = |
| 1104 | call_->Receiver()->DeliverPacket( |
| 1105 | reinterpret_cast<const uint8_t*>(packet->data()), packet->length()); |
| 1106 | switch (delivery_result) { |
| 1107 | case webrtc::PacketReceiver::DELIVERY_OK: |
| 1108 | return; |
| 1109 | case webrtc::PacketReceiver::DELIVERY_PACKET_ERROR: |
| 1110 | return; |
| 1111 | case webrtc::PacketReceiver::DELIVERY_UNKNOWN_SSRC: |
| 1112 | break; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1113 | } |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1114 | |
| 1115 | uint32 ssrc = 0; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1116 | if (!GetRtpSsrc(packet->data(), packet->length(), &ssrc)) { |
| 1117 | return; |
| 1118 | } |
| 1119 | |
pbos@webrtc.org | afb554f4 | 2014-08-12 23:17:13 +0000 | [diff] [blame] | 1120 | // TODO(pbos): Make sure that the unsignalled SSRC uses the video payload. |
| 1121 | // Also figure out whether RTX needs to be handled. |
| 1122 | switch (unsignalled_ssrc_handler_->OnUnsignalledSsrc(this, ssrc)) { |
| 1123 | case UnsignalledSsrcHandler::kDropPacket: |
| 1124 | return; |
| 1125 | case UnsignalledSsrcHandler::kDeliverPacket: |
| 1126 | break; |
| 1127 | } |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1128 | |
pbos@webrtc.org | 1e019d1 | 2014-05-16 11:38:45 +0000 | [diff] [blame] | 1129 | if (call_->Receiver()->DeliverPacket( |
| 1130 | reinterpret_cast<const uint8_t*>(packet->data()), packet->length()) != |
| 1131 | webrtc::PacketReceiver::DELIVERY_OK) { |
pbos@webrtc.org | afb554f4 | 2014-08-12 23:17:13 +0000 | [diff] [blame] | 1132 | LOG(LS_WARNING) << "Failed to deliver RTP packet on re-delivery."; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1133 | return; |
| 1134 | } |
| 1135 | } |
| 1136 | |
| 1137 | void WebRtcVideoChannel2::OnRtcpReceived( |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1138 | rtc::Buffer* packet, |
| 1139 | const rtc::PacketTime& packet_time) { |
pbos@webrtc.org | 1e019d1 | 2014-05-16 11:38:45 +0000 | [diff] [blame] | 1140 | if (call_->Receiver()->DeliverPacket( |
| 1141 | reinterpret_cast<const uint8_t*>(packet->data()), packet->length()) != |
| 1142 | webrtc::PacketReceiver::DELIVERY_OK) { |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1143 | LOG(LS_WARNING) << "Failed to deliver RTCP packet."; |
| 1144 | } |
| 1145 | } |
| 1146 | |
| 1147 | void WebRtcVideoChannel2::OnReadyToSend(bool ready) { |
pbos@webrtc.org | 26c0c41 | 2014-09-03 16:17:12 +0000 | [diff] [blame] | 1148 | LOG(LS_VERBOSE) << "OnReadyToSend: " << (ready ? "Ready." : "Not ready."); |
| 1149 | call_->SignalNetworkState(ready ? webrtc::Call::kNetworkUp |
| 1150 | : webrtc::Call::kNetworkDown); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1151 | } |
| 1152 | |
| 1153 | bool WebRtcVideoChannel2::MuteStream(uint32 ssrc, bool mute) { |
| 1154 | LOG(LS_VERBOSE) << "MuteStream: " << ssrc << " -> " |
| 1155 | << (mute ? "mute" : "unmute"); |
| 1156 | assert(ssrc != 0); |
| 1157 | if (send_streams_.find(ssrc) == send_streams_.end()) { |
| 1158 | LOG(LS_ERROR) << "No sending stream on ssrc " << ssrc; |
| 1159 | return false; |
| 1160 | } |
pbos@webrtc.org | ef8bb8d | 2014-08-13 21:36:18 +0000 | [diff] [blame] | 1161 | |
| 1162 | send_streams_[ssrc]->MuteStream(mute); |
| 1163 | return true; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1164 | } |
| 1165 | |
| 1166 | bool WebRtcVideoChannel2::SetRecvRtpHeaderExtensions( |
| 1167 | const std::vector<RtpHeaderExtension>& extensions) { |
pbos@webrtc.org | 587ef60 | 2014-06-16 17:32:02 +0000 | [diff] [blame] | 1168 | LOG(LS_INFO) << "SetRecvRtpHeaderExtensions: " |
| 1169 | << RtpExtensionsToString(extensions); |
pbos@webrtc.org | 3c10758 | 2014-07-20 15:27:35 +0000 | [diff] [blame] | 1170 | if (!ValidateRtpHeaderExtensionIds(extensions)) |
| 1171 | return false; |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 1172 | |
pbos@webrtc.org | 3c10758 | 2014-07-20 15:27:35 +0000 | [diff] [blame] | 1173 | recv_rtp_extensions_ = FilterRtpExtensions(extensions); |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 1174 | for (std::map<uint32, WebRtcVideoReceiveStream*>::iterator it = |
| 1175 | receive_streams_.begin(); |
| 1176 | it != receive_streams_.end(); |
| 1177 | ++it) { |
| 1178 | it->second->SetRtpExtensions(recv_rtp_extensions_); |
| 1179 | } |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1180 | return true; |
| 1181 | } |
| 1182 | |
| 1183 | bool WebRtcVideoChannel2::SetSendRtpHeaderExtensions( |
| 1184 | const std::vector<RtpHeaderExtension>& extensions) { |
pbos@webrtc.org | 587ef60 | 2014-06-16 17:32:02 +0000 | [diff] [blame] | 1185 | LOG(LS_INFO) << "SetSendRtpHeaderExtensions: " |
| 1186 | << RtpExtensionsToString(extensions); |
pbos@webrtc.org | 3c10758 | 2014-07-20 15:27:35 +0000 | [diff] [blame] | 1187 | if (!ValidateRtpHeaderExtensionIds(extensions)) |
| 1188 | return false; |
| 1189 | |
| 1190 | send_rtp_extensions_ = FilterRtpExtensions(extensions); |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 1191 | for (std::map<uint32, WebRtcVideoSendStream*>::iterator it = |
| 1192 | send_streams_.begin(); |
| 1193 | it != send_streams_.end(); |
| 1194 | ++it) { |
| 1195 | it->second->SetRtpExtensions(send_rtp_extensions_); |
| 1196 | } |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1197 | return true; |
| 1198 | } |
| 1199 | |
| 1200 | bool WebRtcVideoChannel2::SetStartSendBandwidth(int bps) { |
| 1201 | // TODO(pbos): Implement. |
| 1202 | LOG(LS_VERBOSE) << "SetStartSendBandwidth: " << bps; |
| 1203 | return true; |
| 1204 | } |
| 1205 | |
| 1206 | bool WebRtcVideoChannel2::SetMaxSendBandwidth(int bps) { |
| 1207 | // TODO(pbos): Implement. |
| 1208 | LOG(LS_VERBOSE) << "SetMaxSendBandwidth: " << bps; |
| 1209 | return true; |
| 1210 | } |
| 1211 | |
| 1212 | bool WebRtcVideoChannel2::SetOptions(const VideoOptions& options) { |
| 1213 | LOG(LS_VERBOSE) << "SetOptions: " << options.ToString(); |
| 1214 | options_.SetAll(options); |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1215 | for (std::map<uint32, WebRtcVideoSendStream*>::iterator it = |
| 1216 | send_streams_.begin(); |
| 1217 | it != send_streams_.end(); |
| 1218 | ++it) { |
| 1219 | it->second->SetOptions(options_); |
| 1220 | } |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1221 | return true; |
| 1222 | } |
| 1223 | |
| 1224 | void WebRtcVideoChannel2::SetInterface(NetworkInterface* iface) { |
| 1225 | MediaChannel::SetInterface(iface); |
| 1226 | // Set the RTP recv/send buffer to a bigger size |
| 1227 | MediaChannel::SetOption(NetworkInterface::ST_RTP, |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1228 | rtc::Socket::OPT_RCVBUF, |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1229 | kVideoRtpBufferSize); |
| 1230 | |
| 1231 | // TODO(sriniv): Remove or re-enable this. |
| 1232 | // As part of b/8030474, send-buffer is size now controlled through |
| 1233 | // portallocator flags. |
| 1234 | // network_interface_->SetOption(NetworkInterface::ST_RTP, |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1235 | // rtc::Socket::OPT_SNDBUF, |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1236 | // kVideoRtpBufferSize); |
| 1237 | } |
| 1238 | |
| 1239 | void WebRtcVideoChannel2::UpdateAspectRatio(int ratio_w, int ratio_h) { |
| 1240 | // TODO(pbos): Implement. |
| 1241 | } |
| 1242 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1243 | void WebRtcVideoChannel2::OnMessage(rtc::Message* msg) { |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1244 | // Ignored. |
| 1245 | } |
| 1246 | |
| 1247 | bool WebRtcVideoChannel2::SendRtp(const uint8_t* data, size_t len) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1248 | rtc::Buffer packet(data, len, kMaxRtpPacketLen); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1249 | return MediaChannel::SendPacket(&packet); |
| 1250 | } |
| 1251 | |
| 1252 | bool WebRtcVideoChannel2::SendRtcp(const uint8_t* data, size_t len) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1253 | rtc::Buffer packet(data, len, kMaxRtpPacketLen); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1254 | return MediaChannel::SendRtcp(&packet); |
| 1255 | } |
| 1256 | |
| 1257 | void WebRtcVideoChannel2::StartAllSendStreams() { |
| 1258 | for (std::map<uint32, WebRtcVideoSendStream*>::iterator it = |
| 1259 | send_streams_.begin(); |
| 1260 | it != send_streams_.end(); |
| 1261 | ++it) { |
| 1262 | it->second->Start(); |
| 1263 | } |
| 1264 | } |
| 1265 | |
| 1266 | void WebRtcVideoChannel2::StopAllSendStreams() { |
| 1267 | for (std::map<uint32, WebRtcVideoSendStream*>::iterator it = |
| 1268 | send_streams_.begin(); |
| 1269 | it != send_streams_.end(); |
| 1270 | ++it) { |
| 1271 | it->second->Stop(); |
| 1272 | } |
| 1273 | } |
| 1274 | |
pbos@webrtc.org | 6ae48c6 | 2014-06-06 10:49:19 +0000 | [diff] [blame] | 1275 | WebRtcVideoChannel2::WebRtcVideoSendStream::VideoSendStreamParameters:: |
| 1276 | VideoSendStreamParameters( |
| 1277 | const webrtc::VideoSendStream::Config& config, |
| 1278 | const VideoOptions& options, |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1279 | const Settable<VideoCodecSettings>& codec_settings) |
| 1280 | : config(config), options(options), codec_settings(codec_settings) { |
pbos@webrtc.org | 6ae48c6 | 2014-06-06 10:49:19 +0000 | [diff] [blame] | 1281 | } |
| 1282 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1283 | WebRtcVideoChannel2::WebRtcVideoSendStream::WebRtcVideoSendStream( |
| 1284 | webrtc::Call* call, |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1285 | WebRtcVideoEncoderFactory2* encoder_factory, |
pbos@webrtc.org | 6ae48c6 | 2014-06-06 10:49:19 +0000 | [diff] [blame] | 1286 | const VideoOptions& options, |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1287 | const Settable<VideoCodecSettings>& codec_settings, |
| 1288 | const StreamParams& sp, |
| 1289 | const std::vector<webrtc::RtpExtension>& rtp_extensions) |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1290 | : call_(call), |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1291 | encoder_factory_(encoder_factory), |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1292 | stream_(NULL), |
pbos@webrtc.org | b648b9d | 2014-08-26 11:08:06 +0000 | [diff] [blame] | 1293 | parameters_(webrtc::VideoSendStream::Config(), options, codec_settings), |
| 1294 | capturer_(NULL), |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1295 | sending_(false), |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1296 | muted_(false) { |
| 1297 | parameters_.config.rtp.max_packet_size = kVideoMtu; |
| 1298 | |
| 1299 | sp.GetPrimarySsrcs(¶meters_.config.rtp.ssrcs); |
| 1300 | sp.GetFidSsrcs(parameters_.config.rtp.ssrcs, |
| 1301 | ¶meters_.config.rtp.rtx.ssrcs); |
| 1302 | parameters_.config.rtp.c_name = sp.cname; |
| 1303 | parameters_.config.rtp.extensions = rtp_extensions; |
| 1304 | |
| 1305 | VideoCodecSettings params; |
| 1306 | if (codec_settings.Get(¶ms)) { |
| 1307 | SetCodec(params); |
| 1308 | } |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1309 | } |
| 1310 | |
| 1311 | WebRtcVideoChannel2::WebRtcVideoSendStream::~WebRtcVideoSendStream() { |
| 1312 | DisconnectCapturer(); |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1313 | if (stream_ != NULL) { |
| 1314 | call_->DestroyVideoSendStream(stream_); |
| 1315 | } |
pbos@webrtc.org | 6ae48c6 | 2014-06-06 10:49:19 +0000 | [diff] [blame] | 1316 | delete parameters_.config.encoder_settings.encoder; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1317 | } |
| 1318 | |
| 1319 | static void SetWebRtcFrameToBlack(webrtc::I420VideoFrame* video_frame) { |
| 1320 | assert(video_frame != NULL); |
| 1321 | memset(video_frame->buffer(webrtc::kYPlane), |
| 1322 | 16, |
| 1323 | video_frame->allocated_size(webrtc::kYPlane)); |
| 1324 | memset(video_frame->buffer(webrtc::kUPlane), |
| 1325 | 128, |
| 1326 | video_frame->allocated_size(webrtc::kUPlane)); |
| 1327 | memset(video_frame->buffer(webrtc::kVPlane), |
| 1328 | 128, |
| 1329 | video_frame->allocated_size(webrtc::kVPlane)); |
| 1330 | } |
| 1331 | |
| 1332 | static void CreateBlackFrame(webrtc::I420VideoFrame* video_frame, |
| 1333 | int width, |
| 1334 | int height) { |
| 1335 | video_frame->CreateEmptyFrame( |
| 1336 | width, height, width, (width + 1) / 2, (width + 1) / 2); |
| 1337 | SetWebRtcFrameToBlack(video_frame); |
| 1338 | } |
| 1339 | |
| 1340 | static void ConvertToI420VideoFrame(const VideoFrame& frame, |
| 1341 | webrtc::I420VideoFrame* i420_frame) { |
| 1342 | i420_frame->CreateFrame( |
| 1343 | static_cast<int>(frame.GetYPitch() * frame.GetHeight()), |
| 1344 | frame.GetYPlane(), |
| 1345 | static_cast<int>(frame.GetUPitch() * ((frame.GetHeight() + 1) / 2)), |
| 1346 | frame.GetUPlane(), |
| 1347 | static_cast<int>(frame.GetVPitch() * ((frame.GetHeight() + 1) / 2)), |
| 1348 | frame.GetVPlane(), |
| 1349 | static_cast<int>(frame.GetWidth()), |
| 1350 | static_cast<int>(frame.GetHeight()), |
| 1351 | static_cast<int>(frame.GetYPitch()), |
| 1352 | static_cast<int>(frame.GetUPitch()), |
| 1353 | static_cast<int>(frame.GetVPitch())); |
| 1354 | } |
| 1355 | |
| 1356 | void WebRtcVideoChannel2::WebRtcVideoSendStream::InputFrame( |
| 1357 | VideoCapturer* capturer, |
| 1358 | const VideoFrame* frame) { |
| 1359 | LOG(LS_VERBOSE) << "InputFrame: " << frame->GetWidth() << "x" |
| 1360 | << frame->GetHeight(); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1361 | // Lock before copying, can be called concurrently when swapping input source. |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1362 | rtc::CritScope frame_cs(&frame_lock_); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1363 | if (!muted_) { |
| 1364 | ConvertToI420VideoFrame(*frame, &video_frame_); |
| 1365 | } else { |
pbos@webrtc.org | c4175b9 | 2014-09-03 15:25:49 +0000 | [diff] [blame] | 1366 | // Create a black frame to transmit instead. |
| 1367 | CreateBlackFrame(&video_frame_, |
| 1368 | static_cast<int>(frame->GetWidth()), |
| 1369 | static_cast<int>(frame->GetHeight())); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1370 | } |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1371 | rtc::CritScope cs(&lock_); |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1372 | if (stream_ == NULL) { |
| 1373 | LOG(LS_WARNING) << "Capturer inputting frames before send codecs are " |
| 1374 | "configured, dropping."; |
| 1375 | return; |
| 1376 | } |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1377 | if (format_.width == 0) { // Dropping frames. |
| 1378 | assert(format_.height == 0); |
| 1379 | LOG(LS_VERBOSE) << "VideoFormat 0x0 set, Dropping frame."; |
| 1380 | return; |
| 1381 | } |
| 1382 | // Reconfigure codec if necessary. |
pbos@webrtc.org | c4175b9 | 2014-09-03 15:25:49 +0000 | [diff] [blame] | 1383 | SetDimensions( |
| 1384 | video_frame_.width(), video_frame_.height(), capturer->IsScreencast()); |
| 1385 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1386 | LOG(LS_VERBOSE) << "SwapFrame: " << video_frame_.width() << "x" |
| 1387 | << video_frame_.height() << " -> (codec) " |
pbos@webrtc.org | bbe0a85 | 2014-09-19 12:30:25 +0000 | [diff] [blame^] | 1388 | << parameters_.encoder_config.streams.back().width << "x" |
| 1389 | << parameters_.encoder_config.streams.back().height; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1390 | stream_->Input()->SwapFrame(&video_frame_); |
| 1391 | } |
| 1392 | |
| 1393 | bool WebRtcVideoChannel2::WebRtcVideoSendStream::SetCapturer( |
| 1394 | VideoCapturer* capturer) { |
| 1395 | if (!DisconnectCapturer() && capturer == NULL) { |
| 1396 | return false; |
| 1397 | } |
| 1398 | |
| 1399 | { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1400 | rtc::CritScope cs(&lock_); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1401 | |
pbos@webrtc.org | 9359cb3 | 2014-07-23 15:44:48 +0000 | [diff] [blame] | 1402 | if (capturer == NULL) { |
| 1403 | if (stream_ != NULL) { |
| 1404 | LOG(LS_VERBOSE) << "Disabling capturer, sending black frame."; |
| 1405 | webrtc::I420VideoFrame black_frame; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1406 | |
pbos@webrtc.org | 9359cb3 | 2014-07-23 15:44:48 +0000 | [diff] [blame] | 1407 | int width = format_.width; |
| 1408 | int height = format_.height; |
| 1409 | int half_width = (width + 1) / 2; |
| 1410 | black_frame.CreateEmptyFrame( |
| 1411 | width, height, width, half_width, half_width); |
| 1412 | SetWebRtcFrameToBlack(&black_frame); |
pbos@webrtc.org | c4175b9 | 2014-09-03 15:25:49 +0000 | [diff] [blame] | 1413 | SetDimensions(width, height, false); |
pbos@webrtc.org | 9359cb3 | 2014-07-23 15:44:48 +0000 | [diff] [blame] | 1414 | stream_->Input()->SwapFrame(&black_frame); |
| 1415 | } |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1416 | |
| 1417 | capturer_ = NULL; |
| 1418 | return true; |
| 1419 | } |
| 1420 | |
| 1421 | capturer_ = capturer; |
| 1422 | } |
| 1423 | // Lock cannot be held while connecting the capturer to prevent lock-order |
| 1424 | // violations. |
| 1425 | capturer->SignalVideoFrame.connect(this, &WebRtcVideoSendStream::InputFrame); |
| 1426 | return true; |
| 1427 | } |
| 1428 | |
| 1429 | bool WebRtcVideoChannel2::WebRtcVideoSendStream::SetVideoFormat( |
| 1430 | const VideoFormat& format) { |
| 1431 | if ((format.width == 0 || format.height == 0) && |
| 1432 | format.width != format.height) { |
| 1433 | LOG(LS_ERROR) << "Can't set VideoFormat, width or height is zero (but not " |
| 1434 | "both, 0x0 drops frames)."; |
| 1435 | return false; |
| 1436 | } |
| 1437 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1438 | rtc::CritScope cs(&lock_); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1439 | if (format.width == 0 && format.height == 0) { |
| 1440 | LOG(LS_INFO) |
| 1441 | << "0x0 resolution selected. Captured frames will be dropped for ssrc: " |
pbos@webrtc.org | 6ae48c6 | 2014-06-06 10:49:19 +0000 | [diff] [blame] | 1442 | << parameters_.config.rtp.ssrcs[0] << "."; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1443 | } else { |
| 1444 | // TODO(pbos): Fix me, this only affects the last stream! |
pbos@webrtc.org | bbe0a85 | 2014-09-19 12:30:25 +0000 | [diff] [blame^] | 1445 | parameters_.encoder_config.streams.back().max_framerate = |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1446 | VideoFormat::IntervalToFps(format.interval); |
pbos@webrtc.org | c4175b9 | 2014-09-03 15:25:49 +0000 | [diff] [blame] | 1447 | SetDimensions(format.width, format.height, false); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1448 | } |
| 1449 | |
| 1450 | format_ = format; |
| 1451 | return true; |
| 1452 | } |
| 1453 | |
pbos@webrtc.org | ef8bb8d | 2014-08-13 21:36:18 +0000 | [diff] [blame] | 1454 | void WebRtcVideoChannel2::WebRtcVideoSendStream::MuteStream(bool mute) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1455 | rtc::CritScope cs(&lock_); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1456 | muted_ = mute; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1457 | } |
| 1458 | |
| 1459 | bool WebRtcVideoChannel2::WebRtcVideoSendStream::DisconnectCapturer() { |
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 (capturer_ == NULL) { |
| 1462 | return false; |
| 1463 | } |
| 1464 | capturer_->SignalVideoFrame.disconnect(this); |
| 1465 | capturer_ = NULL; |
| 1466 | return true; |
| 1467 | } |
| 1468 | |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1469 | void WebRtcVideoChannel2::WebRtcVideoSendStream::SetOptions( |
| 1470 | const VideoOptions& options) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1471 | rtc::CritScope cs(&lock_); |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1472 | VideoCodecSettings codec_settings; |
| 1473 | if (parameters_.codec_settings.Get(&codec_settings)) { |
| 1474 | SetCodecAndOptions(codec_settings, options); |
| 1475 | } else { |
| 1476 | parameters_.options = options; |
| 1477 | } |
| 1478 | } |
| 1479 | void WebRtcVideoChannel2::WebRtcVideoSendStream::SetCodec( |
| 1480 | const VideoCodecSettings& codec_settings) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1481 | rtc::CritScope cs(&lock_); |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1482 | SetCodecAndOptions(codec_settings, parameters_.options); |
| 1483 | } |
| 1484 | void WebRtcVideoChannel2::WebRtcVideoSendStream::SetCodecAndOptions( |
| 1485 | const VideoCodecSettings& codec_settings, |
| 1486 | const VideoOptions& options) { |
pbos@webrtc.org | 6ae48c6 | 2014-06-06 10:49:19 +0000 | [diff] [blame] | 1487 | std::vector<webrtc::VideoStream> video_streams = |
| 1488 | encoder_factory_->CreateVideoStreams( |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1489 | codec_settings.codec, options, parameters_.config.rtp.ssrcs.size()); |
pbos@webrtc.org | 6ae48c6 | 2014-06-06 10:49:19 +0000 | [diff] [blame] | 1490 | if (video_streams.empty()) { |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1491 | return; |
| 1492 | } |
pbos@webrtc.org | bbe0a85 | 2014-09-19 12:30:25 +0000 | [diff] [blame^] | 1493 | parameters_.encoder_config.streams = video_streams; |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1494 | format_ = VideoFormat(codec_settings.codec.width, |
| 1495 | codec_settings.codec.height, |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1496 | VideoFormat::FpsToInterval(30), |
| 1497 | FOURCC_I420); |
pbos@webrtc.org | 6ae48c6 | 2014-06-06 10:49:19 +0000 | [diff] [blame] | 1498 | |
| 1499 | webrtc::VideoEncoder* old_encoder = |
| 1500 | parameters_.config.encoder_settings.encoder; |
| 1501 | parameters_.config.encoder_settings.encoder = |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1502 | encoder_factory_->CreateVideoEncoder(codec_settings.codec, options); |
| 1503 | parameters_.config.encoder_settings.payload_name = codec_settings.codec.name; |
| 1504 | parameters_.config.encoder_settings.payload_type = codec_settings.codec.id; |
| 1505 | parameters_.config.rtp.fec = codec_settings.fec; |
| 1506 | |
| 1507 | // Set RTX payload type if RTX is enabled. |
| 1508 | if (!parameters_.config.rtp.rtx.ssrcs.empty()) { |
| 1509 | parameters_.config.rtp.rtx.payload_type = codec_settings.rtx_payload_type; |
pbos@webrtc.org | 543e589 | 2014-07-23 07:01:31 +0000 | [diff] [blame] | 1510 | |
| 1511 | options.use_payload_padding.Get( |
| 1512 | ¶meters_.config.rtp.rtx.pad_with_redundant_payloads); |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1513 | } |
| 1514 | |
| 1515 | if (IsNackEnabled(codec_settings.codec)) { |
| 1516 | parameters_.config.rtp.nack.rtp_history_ms = kNackHistoryMs; |
| 1517 | } |
| 1518 | |
pbos@webrtc.org | 5ff71ab | 2014-07-23 07:28:56 +0000 | [diff] [blame] | 1519 | options.suspend_below_min_bitrate.Get( |
| 1520 | ¶meters_.config.suspend_below_min_bitrate); |
| 1521 | |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1522 | parameters_.codec_settings.Set(codec_settings); |
pbos@webrtc.org | 6ae48c6 | 2014-06-06 10:49:19 +0000 | [diff] [blame] | 1523 | parameters_.options = options; |
pbos@webrtc.org | 543e589 | 2014-07-23 07:01:31 +0000 | [diff] [blame] | 1524 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1525 | RecreateWebRtcStream(); |
| 1526 | delete old_encoder; |
| 1527 | } |
| 1528 | |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 1529 | void WebRtcVideoChannel2::WebRtcVideoSendStream::SetRtpExtensions( |
| 1530 | const std::vector<webrtc::RtpExtension>& rtp_extensions) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1531 | rtc::CritScope cs(&lock_); |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 1532 | parameters_.config.rtp.extensions = rtp_extensions; |
| 1533 | RecreateWebRtcStream(); |
| 1534 | } |
| 1535 | |
pbos@webrtc.org | c4175b9 | 2014-09-03 15:25:49 +0000 | [diff] [blame] | 1536 | void WebRtcVideoChannel2::WebRtcVideoSendStream::SetDimensions( |
| 1537 | int width, |
| 1538 | int height, |
| 1539 | bool override_max) { |
pbos@webrtc.org | bbe0a85 | 2014-09-19 12:30:25 +0000 | [diff] [blame^] | 1540 | assert(!parameters_.encoder_config.streams.empty()); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1541 | LOG(LS_VERBOSE) << "SetDimensions: " << width << "x" << height; |
pbos@webrtc.org | c4175b9 | 2014-09-03 15:25:49 +0000 | [diff] [blame] | 1542 | |
| 1543 | VideoCodecSettings codec_settings; |
| 1544 | parameters_.codec_settings.Get(&codec_settings); |
| 1545 | // Restrict dimensions according to codec max. |
| 1546 | if (!override_max) { |
| 1547 | if (codec_settings.codec.width < width) |
| 1548 | width = codec_settings.codec.width; |
| 1549 | if (codec_settings.codec.height < height) |
| 1550 | height = codec_settings.codec.height; |
| 1551 | } |
| 1552 | |
pbos@webrtc.org | bbe0a85 | 2014-09-19 12:30:25 +0000 | [diff] [blame^] | 1553 | if (parameters_.encoder_config.streams.back().width == width && |
| 1554 | parameters_.encoder_config.streams.back().height == height) { |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1555 | return; |
| 1556 | } |
| 1557 | |
pbos@webrtc.org | bbe0a85 | 2014-09-19 12:30:25 +0000 | [diff] [blame^] | 1558 | webrtc::VideoEncoderConfig encoder_config = parameters_.encoder_config; |
| 1559 | encoder_config.encoder_specific_settings = |
| 1560 | encoder_factory_->CreateVideoEncoderSettings(codec_settings.codec, |
| 1561 | parameters_.options); |
pbos@webrtc.org | 6f48f1b | 2014-07-22 16:29:54 +0000 | [diff] [blame] | 1562 | |
pbos@webrtc.org | cddd17c | 2014-09-16 16:33:13 +0000 | [diff] [blame] | 1563 | VideoCodec codec = codec_settings.codec; |
| 1564 | codec.width = width; |
| 1565 | codec.height = height; |
pbos@webrtc.org | cddd17c | 2014-09-16 16:33:13 +0000 | [diff] [blame] | 1566 | |
pbos@webrtc.org | bbe0a85 | 2014-09-19 12:30:25 +0000 | [diff] [blame^] | 1567 | encoder_config.streams = encoder_factory_->CreateVideoStreams( |
| 1568 | codec, parameters_.options, parameters_.config.rtp.ssrcs.size()); |
pbos@webrtc.org | 6f48f1b | 2014-07-22 16:29:54 +0000 | [diff] [blame] | 1569 | |
pbos@webrtc.org | bbe0a85 | 2014-09-19 12:30:25 +0000 | [diff] [blame^] | 1570 | bool stream_reconfigured = stream_->ReconfigureVideoEncoder(encoder_config); |
| 1571 | |
| 1572 | encoder_factory_->DestroyVideoEncoderSettings( |
| 1573 | codec_settings.codec, |
| 1574 | encoder_config.encoder_specific_settings); |
| 1575 | |
| 1576 | encoder_config.encoder_specific_settings = NULL; |
pbos@webrtc.org | 6f48f1b | 2014-07-22 16:29:54 +0000 | [diff] [blame] | 1577 | |
| 1578 | if (!stream_reconfigured) { |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1579 | LOG(LS_WARNING) << "Failed to reconfigure video encoder for dimensions: " |
| 1580 | << width << "x" << height; |
| 1581 | return; |
| 1582 | } |
pbos@webrtc.org | cddd17c | 2014-09-16 16:33:13 +0000 | [diff] [blame] | 1583 | |
pbos@webrtc.org | bbe0a85 | 2014-09-19 12:30:25 +0000 | [diff] [blame^] | 1584 | parameters_.encoder_config = encoder_config; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1585 | } |
| 1586 | |
| 1587 | void WebRtcVideoChannel2::WebRtcVideoSendStream::Start() { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1588 | rtc::CritScope cs(&lock_); |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1589 | assert(stream_ != NULL); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1590 | stream_->Start(); |
| 1591 | sending_ = true; |
| 1592 | } |
| 1593 | |
| 1594 | void WebRtcVideoChannel2::WebRtcVideoSendStream::Stop() { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1595 | rtc::CritScope cs(&lock_); |
pbos@webrtc.org | 5301b0f | 2014-07-17 08:51:46 +0000 | [diff] [blame] | 1596 | if (stream_ != NULL) { |
| 1597 | stream_->Stop(); |
| 1598 | } |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1599 | sending_ = false; |
| 1600 | } |
| 1601 | |
pbos@webrtc.org | e6f84ae | 2014-07-18 11:11:55 +0000 | [diff] [blame] | 1602 | VideoSenderInfo |
| 1603 | WebRtcVideoChannel2::WebRtcVideoSendStream::GetVideoSenderInfo() { |
| 1604 | VideoSenderInfo info; |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1605 | rtc::CritScope cs(&lock_); |
pbos@webrtc.org | e6f84ae | 2014-07-18 11:11:55 +0000 | [diff] [blame] | 1606 | for (size_t i = 0; i < parameters_.config.rtp.ssrcs.size(); ++i) { |
| 1607 | info.add_ssrc(parameters_.config.rtp.ssrcs[i]); |
| 1608 | } |
| 1609 | |
pbos@webrtc.org | c3d2bd2 | 2014-08-12 20:55:10 +0000 | [diff] [blame] | 1610 | if (stream_ == NULL) { |
| 1611 | return info; |
| 1612 | } |
| 1613 | |
pbos@webrtc.org | e6f84ae | 2014-07-18 11:11:55 +0000 | [diff] [blame] | 1614 | webrtc::VideoSendStream::Stats stats = stream_->GetStats(); |
| 1615 | info.framerate_input = stats.input_frame_rate; |
| 1616 | info.framerate_sent = stats.encode_frame_rate; |
| 1617 | |
| 1618 | for (std::map<uint32_t, webrtc::StreamStats>::iterator it = |
| 1619 | stats.substreams.begin(); |
| 1620 | it != stats.substreams.end(); |
| 1621 | ++it) { |
| 1622 | // TODO(pbos): Wire up additional stats, such as padding bytes. |
| 1623 | webrtc::StreamStats stream_stats = it->second; |
| 1624 | info.bytes_sent += stream_stats.rtp_stats.bytes + |
| 1625 | stream_stats.rtp_stats.header_bytes + |
| 1626 | stream_stats.rtp_stats.padding_bytes; |
| 1627 | info.packets_sent += stream_stats.rtp_stats.packets; |
| 1628 | info.packets_lost += stream_stats.rtcp_stats.cumulative_lost; |
| 1629 | } |
| 1630 | |
| 1631 | if (!stats.substreams.empty()) { |
| 1632 | // TODO(pbos): Report fraction lost per SSRC. |
| 1633 | webrtc::StreamStats first_stream_stats = stats.substreams.begin()->second; |
| 1634 | info.fraction_lost = |
| 1635 | static_cast<float>(first_stream_stats.rtcp_stats.fraction_lost) / |
| 1636 | (1 << 8); |
| 1637 | } |
| 1638 | |
| 1639 | if (capturer_ != NULL && !capturer_->IsMuted()) { |
| 1640 | VideoFormat last_captured_frame_format; |
| 1641 | capturer_->GetStats(&info.adapt_frame_drops, |
| 1642 | &info.effects_frame_drops, |
| 1643 | &info.capturer_frame_time, |
| 1644 | &last_captured_frame_format); |
| 1645 | info.input_frame_width = last_captured_frame_format.width; |
| 1646 | info.input_frame_height = last_captured_frame_format.height; |
| 1647 | info.send_frame_width = |
pbos@webrtc.org | bbe0a85 | 2014-09-19 12:30:25 +0000 | [diff] [blame^] | 1648 | static_cast<int>(parameters_.encoder_config.streams.front().width); |
pbos@webrtc.org | e6f84ae | 2014-07-18 11:11:55 +0000 | [diff] [blame] | 1649 | info.send_frame_height = |
pbos@webrtc.org | bbe0a85 | 2014-09-19 12:30:25 +0000 | [diff] [blame^] | 1650 | static_cast<int>(parameters_.encoder_config.streams.front().height); |
pbos@webrtc.org | e6f84ae | 2014-07-18 11:11:55 +0000 | [diff] [blame] | 1651 | } |
| 1652 | |
| 1653 | // TODO(pbos): Support or remove the following stats. |
| 1654 | info.packets_cached = -1; |
| 1655 | info.rtt_ms = -1; |
| 1656 | |
| 1657 | return info; |
| 1658 | } |
| 1659 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1660 | void WebRtcVideoChannel2::WebRtcVideoSendStream::RecreateWebRtcStream() { |
| 1661 | if (stream_ != NULL) { |
| 1662 | call_->DestroyVideoSendStream(stream_); |
| 1663 | } |
pbos@webrtc.org | 6ae48c6 | 2014-06-06 10:49:19 +0000 | [diff] [blame] | 1664 | |
pbos@webrtc.org | 6f48f1b | 2014-07-22 16:29:54 +0000 | [diff] [blame] | 1665 | VideoCodecSettings codec_settings; |
| 1666 | parameters_.codec_settings.Get(&codec_settings); |
pbos@webrtc.org | bbe0a85 | 2014-09-19 12:30:25 +0000 | [diff] [blame^] | 1667 | parameters_.encoder_config.encoder_specific_settings = |
| 1668 | encoder_factory_->CreateVideoEncoderSettings(codec_settings.codec, |
| 1669 | parameters_.options); |
pbos@webrtc.org | 6f48f1b | 2014-07-22 16:29:54 +0000 | [diff] [blame] | 1670 | |
pbos@webrtc.org | bbe0a85 | 2014-09-19 12:30:25 +0000 | [diff] [blame^] | 1671 | stream_ = call_->CreateVideoSendStream(parameters_.config, |
| 1672 | parameters_.encoder_config); |
pbos@webrtc.org | 6f48f1b | 2014-07-22 16:29:54 +0000 | [diff] [blame] | 1673 | |
pbos@webrtc.org | bbe0a85 | 2014-09-19 12:30:25 +0000 | [diff] [blame^] | 1674 | encoder_factory_->DestroyVideoEncoderSettings( |
| 1675 | codec_settings.codec, |
| 1676 | parameters_.encoder_config.encoder_specific_settings); |
| 1677 | |
| 1678 | parameters_.encoder_config.encoder_specific_settings = NULL; |
pbos@webrtc.org | 6f48f1b | 2014-07-22 16:29:54 +0000 | [diff] [blame] | 1679 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1680 | if (sending_) { |
| 1681 | stream_->Start(); |
| 1682 | } |
| 1683 | } |
| 1684 | |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 1685 | WebRtcVideoChannel2::WebRtcVideoReceiveStream::WebRtcVideoReceiveStream( |
| 1686 | webrtc::Call* call, |
| 1687 | const webrtc::VideoReceiveStream::Config& config, |
| 1688 | const std::vector<VideoCodecSettings>& recv_codecs) |
| 1689 | : call_(call), |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 1690 | stream_(NULL), |
pbos@webrtc.org | b648b9d | 2014-08-26 11:08:06 +0000 | [diff] [blame] | 1691 | config_(config), |
| 1692 | renderer_(NULL), |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 1693 | last_width_(-1), |
pbos@webrtc.org | b648b9d | 2014-08-26 11:08:06 +0000 | [diff] [blame] | 1694 | last_height_(-1) { |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 1695 | config_.renderer = this; |
| 1696 | // SetRecvCodecs will also reset (start) the VideoReceiveStream. |
| 1697 | SetRecvCodecs(recv_codecs); |
| 1698 | } |
| 1699 | |
| 1700 | WebRtcVideoChannel2::WebRtcVideoReceiveStream::~WebRtcVideoReceiveStream() { |
| 1701 | call_->DestroyVideoReceiveStream(stream_); |
| 1702 | } |
| 1703 | |
| 1704 | void WebRtcVideoChannel2::WebRtcVideoReceiveStream::SetRecvCodecs( |
| 1705 | const std::vector<VideoCodecSettings>& recv_codecs) { |
| 1706 | // TODO(pbos): Reconfigure RTX based on incoming recv_codecs. |
| 1707 | // TODO(pbos): Base receive codecs off recv_codecs_ and set up using a |
| 1708 | // DecoderFactory similar to send side. Pending webrtc:2854. |
| 1709 | // Also set up default codecs if there's nothing in recv_codecs_. |
| 1710 | webrtc::VideoCodec codec; |
| 1711 | memset(&codec, 0, sizeof(codec)); |
| 1712 | |
| 1713 | codec.plType = kDefaultVideoCodecPref.payload_type; |
| 1714 | strcpy(codec.plName, kDefaultVideoCodecPref.name); |
| 1715 | codec.codecType = webrtc::kVideoCodecVP8; |
| 1716 | codec.codecSpecific.VP8.resilience = webrtc::kResilientStream; |
| 1717 | codec.codecSpecific.VP8.numberOfTemporalLayers = 1; |
| 1718 | codec.codecSpecific.VP8.denoisingOn = true; |
| 1719 | codec.codecSpecific.VP8.errorConcealmentOn = false; |
| 1720 | codec.codecSpecific.VP8.automaticResizeOn = false; |
| 1721 | codec.codecSpecific.VP8.frameDroppingOn = true; |
| 1722 | codec.codecSpecific.VP8.keyFrameInterval = 3000; |
| 1723 | // Bitrates don't matter and are ignored for the receiver. This is put in to |
| 1724 | // have the current underlying implementation accept the VideoCodec. |
| 1725 | codec.minBitrate = codec.startBitrate = codec.maxBitrate = 300; |
| 1726 | config_.codecs.clear(); |
| 1727 | config_.codecs.push_back(codec); |
| 1728 | |
| 1729 | config_.rtp.fec = recv_codecs.front().fec; |
| 1730 | |
pbos@webrtc.org | 257e130 | 2014-07-25 19:01:32 +0000 | [diff] [blame] | 1731 | config_.rtp.nack.rtp_history_ms = |
| 1732 | IsNackEnabled(recv_codecs.begin()->codec) ? kNackHistoryMs : 0; |
| 1733 | config_.rtp.remb = IsRembEnabled(recv_codecs.begin()->codec); |
| 1734 | |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 1735 | RecreateWebRtcStream(); |
| 1736 | } |
| 1737 | |
| 1738 | void WebRtcVideoChannel2::WebRtcVideoReceiveStream::SetRtpExtensions( |
| 1739 | const std::vector<webrtc::RtpExtension>& extensions) { |
| 1740 | config_.rtp.extensions = extensions; |
| 1741 | RecreateWebRtcStream(); |
| 1742 | } |
| 1743 | |
| 1744 | void WebRtcVideoChannel2::WebRtcVideoReceiveStream::RecreateWebRtcStream() { |
| 1745 | if (stream_ != NULL) { |
| 1746 | call_->DestroyVideoReceiveStream(stream_); |
| 1747 | } |
| 1748 | stream_ = call_->CreateVideoReceiveStream(config_); |
| 1749 | stream_->Start(); |
| 1750 | } |
| 1751 | |
| 1752 | void WebRtcVideoChannel2::WebRtcVideoReceiveStream::RenderFrame( |
| 1753 | const webrtc::I420VideoFrame& frame, |
| 1754 | int time_to_render_ms) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1755 | rtc::CritScope crit(&renderer_lock_); |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 1756 | if (renderer_ == NULL) { |
| 1757 | LOG(LS_WARNING) << "VideoReceiveStream not connected to a VideoRenderer."; |
| 1758 | return; |
| 1759 | } |
| 1760 | |
| 1761 | if (frame.width() != last_width_ || frame.height() != last_height_) { |
| 1762 | SetSize(frame.width(), frame.height()); |
| 1763 | } |
| 1764 | |
| 1765 | LOG(LS_VERBOSE) << "RenderFrame: (" << frame.width() << "x" << frame.height() |
| 1766 | << ")"; |
| 1767 | |
| 1768 | const WebRtcVideoRenderFrame render_frame(&frame); |
| 1769 | renderer_->RenderFrame(&render_frame); |
| 1770 | } |
| 1771 | |
| 1772 | void WebRtcVideoChannel2::WebRtcVideoReceiveStream::SetRenderer( |
| 1773 | cricket::VideoRenderer* renderer) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1774 | rtc::CritScope crit(&renderer_lock_); |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 1775 | renderer_ = renderer; |
| 1776 | if (renderer_ != NULL && last_width_ != -1) { |
| 1777 | SetSize(last_width_, last_height_); |
| 1778 | } |
| 1779 | } |
| 1780 | |
| 1781 | VideoRenderer* WebRtcVideoChannel2::WebRtcVideoReceiveStream::GetRenderer() { |
| 1782 | // TODO(pbos): Remove GetRenderer and all uses of it, it's thread-unsafe by |
| 1783 | // design. |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1784 | rtc::CritScope crit(&renderer_lock_); |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 1785 | return renderer_; |
| 1786 | } |
| 1787 | |
| 1788 | void WebRtcVideoChannel2::WebRtcVideoReceiveStream::SetSize(int width, |
| 1789 | int height) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1790 | rtc::CritScope crit(&renderer_lock_); |
pbos@webrtc.org | d1ea06b | 2014-07-18 09:35:58 +0000 | [diff] [blame] | 1791 | if (!renderer_->SetSize(width, height, 0)) { |
| 1792 | LOG(LS_ERROR) << "Could not set renderer size."; |
| 1793 | } |
| 1794 | last_width_ = width; |
| 1795 | last_height_ = height; |
| 1796 | } |
| 1797 | |
pbos@webrtc.org | e6f84ae | 2014-07-18 11:11:55 +0000 | [diff] [blame] | 1798 | VideoReceiverInfo |
| 1799 | WebRtcVideoChannel2::WebRtcVideoReceiveStream::GetVideoReceiverInfo() { |
| 1800 | VideoReceiverInfo info; |
| 1801 | info.add_ssrc(config_.rtp.remote_ssrc); |
| 1802 | webrtc::VideoReceiveStream::Stats stats = stream_->GetStats(); |
| 1803 | info.bytes_rcvd = stats.rtp_stats.bytes + stats.rtp_stats.header_bytes + |
| 1804 | stats.rtp_stats.padding_bytes; |
| 1805 | info.packets_rcvd = stats.rtp_stats.packets; |
| 1806 | |
| 1807 | info.framerate_rcvd = stats.network_frame_rate; |
| 1808 | info.framerate_decoded = stats.decode_frame_rate; |
| 1809 | info.framerate_output = stats.render_frame_rate; |
| 1810 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1811 | rtc::CritScope frame_cs(&renderer_lock_); |
pbos@webrtc.org | e6f84ae | 2014-07-18 11:11:55 +0000 | [diff] [blame] | 1812 | info.frame_width = last_width_; |
| 1813 | info.frame_height = last_height_; |
| 1814 | |
| 1815 | // TODO(pbos): Support or remove the following stats. |
| 1816 | info.packets_concealed = -1; |
| 1817 | |
| 1818 | return info; |
| 1819 | } |
| 1820 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1821 | WebRtcVideoChannel2::VideoCodecSettings::VideoCodecSettings() |
| 1822 | : rtx_payload_type(-1) {} |
| 1823 | |
| 1824 | std::vector<WebRtcVideoChannel2::VideoCodecSettings> |
| 1825 | WebRtcVideoChannel2::MapCodecs(const std::vector<VideoCodec>& codecs) { |
| 1826 | assert(!codecs.empty()); |
| 1827 | |
| 1828 | std::vector<VideoCodecSettings> video_codecs; |
| 1829 | std::map<int, bool> payload_used; |
pbos@webrtc.org | e322a17 | 2014-06-13 11:47:28 +0000 | [diff] [blame] | 1830 | std::map<int, VideoCodec::CodecType> payload_codec_type; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1831 | std::map<int, int> rtx_mapping; // video payload type -> rtx payload type. |
| 1832 | |
| 1833 | webrtc::FecConfig fec_settings; |
| 1834 | |
| 1835 | for (size_t i = 0; i < codecs.size(); ++i) { |
| 1836 | const VideoCodec& in_codec = codecs[i]; |
| 1837 | int payload_type = in_codec.id; |
| 1838 | |
| 1839 | if (payload_used[payload_type]) { |
| 1840 | LOG(LS_ERROR) << "Payload type already registered: " |
| 1841 | << in_codec.ToString(); |
| 1842 | return std::vector<VideoCodecSettings>(); |
| 1843 | } |
| 1844 | payload_used[payload_type] = true; |
pbos@webrtc.org | e322a17 | 2014-06-13 11:47:28 +0000 | [diff] [blame] | 1845 | payload_codec_type[payload_type] = in_codec.GetCodecType(); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1846 | |
| 1847 | switch (in_codec.GetCodecType()) { |
| 1848 | case VideoCodec::CODEC_RED: { |
| 1849 | // RED payload type, should not have duplicates. |
| 1850 | assert(fec_settings.red_payload_type == -1); |
| 1851 | fec_settings.red_payload_type = in_codec.id; |
| 1852 | continue; |
| 1853 | } |
| 1854 | |
| 1855 | case VideoCodec::CODEC_ULPFEC: { |
| 1856 | // ULPFEC payload type, should not have duplicates. |
| 1857 | assert(fec_settings.ulpfec_payload_type == -1); |
| 1858 | fec_settings.ulpfec_payload_type = in_codec.id; |
| 1859 | continue; |
| 1860 | } |
| 1861 | |
| 1862 | case VideoCodec::CODEC_RTX: { |
| 1863 | int associated_payload_type; |
| 1864 | if (!in_codec.GetParam(kCodecParamAssociatedPayloadType, |
| 1865 | &associated_payload_type)) { |
| 1866 | LOG(LS_ERROR) << "RTX codec without associated payload type: " |
| 1867 | << in_codec.ToString(); |
| 1868 | return std::vector<VideoCodecSettings>(); |
| 1869 | } |
| 1870 | rtx_mapping[associated_payload_type] = in_codec.id; |
| 1871 | continue; |
| 1872 | } |
| 1873 | |
| 1874 | case VideoCodec::CODEC_VIDEO: |
| 1875 | break; |
| 1876 | } |
| 1877 | |
| 1878 | video_codecs.push_back(VideoCodecSettings()); |
| 1879 | video_codecs.back().codec = in_codec; |
| 1880 | } |
| 1881 | |
| 1882 | // One of these codecs should have been a video codec. Only having FEC |
| 1883 | // parameters into this code is a logic error. |
| 1884 | assert(!video_codecs.empty()); |
| 1885 | |
pbos@webrtc.org | e322a17 | 2014-06-13 11:47:28 +0000 | [diff] [blame] | 1886 | for (std::map<int, int>::const_iterator it = rtx_mapping.begin(); |
| 1887 | it != rtx_mapping.end(); |
| 1888 | ++it) { |
| 1889 | if (!payload_used[it->first]) { |
| 1890 | LOG(LS_ERROR) << "RTX mapped to payload not in codec list."; |
| 1891 | return std::vector<VideoCodecSettings>(); |
| 1892 | } |
| 1893 | if (payload_codec_type[it->first] != VideoCodec::CODEC_VIDEO) { |
| 1894 | LOG(LS_ERROR) << "RTX not mapped to regular video codec."; |
| 1895 | return std::vector<VideoCodecSettings>(); |
| 1896 | } |
| 1897 | } |
| 1898 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 1899 | // TODO(pbos): Write tests that figure out that I have not verified that RTX |
| 1900 | // codecs aren't mapped to bogus payloads. |
| 1901 | for (size_t i = 0; i < video_codecs.size(); ++i) { |
| 1902 | video_codecs[i].fec = fec_settings; |
| 1903 | if (rtx_mapping[video_codecs[i].codec.id] != 0) { |
| 1904 | video_codecs[i].rtx_payload_type = rtx_mapping[video_codecs[i].codec.id]; |
| 1905 | } |
| 1906 | } |
| 1907 | |
| 1908 | return video_codecs; |
| 1909 | } |
| 1910 | |
| 1911 | std::vector<WebRtcVideoChannel2::VideoCodecSettings> |
| 1912 | WebRtcVideoChannel2::FilterSupportedCodecs( |
| 1913 | const std::vector<WebRtcVideoChannel2::VideoCodecSettings>& mapped_codecs) { |
| 1914 | std::vector<VideoCodecSettings> supported_codecs; |
| 1915 | for (size_t i = 0; i < mapped_codecs.size(); ++i) { |
| 1916 | if (encoder_factory_->SupportsCodec(mapped_codecs[i].codec)) { |
| 1917 | supported_codecs.push_back(mapped_codecs[i]); |
| 1918 | } |
| 1919 | } |
| 1920 | return supported_codecs; |
| 1921 | } |
| 1922 | |
| 1923 | } // namespace cricket |
| 1924 | |
| 1925 | #endif // HAVE_WEBRTC_VIDEO |