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