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