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