henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | /* |
| 2 | * libjingle |
| 3 | * Copyright 2004 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/webrtcvideoengine.h" |
| 30 | |
| 31 | #ifdef HAVE_CONFIG_H |
| 32 | #include <config.h> |
| 33 | #endif |
| 34 | |
| 35 | #include <math.h> |
| 36 | #include <set> |
| 37 | |
| 38 | #include "talk/base/basictypes.h" |
| 39 | #include "talk/base/buffer.h" |
| 40 | #include "talk/base/byteorder.h" |
| 41 | #include "talk/base/common.h" |
| 42 | #include "talk/base/cpumonitor.h" |
| 43 | #include "talk/base/logging.h" |
| 44 | #include "talk/base/stringutils.h" |
| 45 | #include "talk/base/thread.h" |
| 46 | #include "talk/base/timeutils.h" |
| 47 | #include "talk/media/base/constants.h" |
| 48 | #include "talk/media/base/rtputils.h" |
| 49 | #include "talk/media/base/streamparams.h" |
| 50 | #include "talk/media/base/videoadapter.h" |
| 51 | #include "talk/media/base/videocapturer.h" |
| 52 | #include "talk/media/base/videorenderer.h" |
| 53 | #include "talk/media/devices/filevideocapturer.h" |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 54 | #include "talk/media/webrtc/webrtcpassthroughrender.h" |
| 55 | #include "talk/media/webrtc/webrtctexturevideoframe.h" |
| 56 | #include "talk/media/webrtc/webrtcvideocapturer.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 57 | #include "talk/media/webrtc/webrtcvideodecoderfactory.h" |
| 58 | #include "talk/media/webrtc/webrtcvideoencoderfactory.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 59 | #include "talk/media/webrtc/webrtcvideoframe.h" |
| 60 | #include "talk/media/webrtc/webrtcvie.h" |
| 61 | #include "talk/media/webrtc/webrtcvoe.h" |
| 62 | #include "talk/media/webrtc/webrtcvoiceengine.h" |
| 63 | |
| 64 | #if !defined(LIBPEERCONNECTION_LIB) |
| 65 | #ifndef HAVE_WEBRTC_VIDEO |
| 66 | #error Need webrtc video |
| 67 | #endif |
| 68 | #include "talk/media/webrtc/webrtcmediaengine.h" |
| 69 | |
| 70 | WRME_EXPORT |
| 71 | cricket::MediaEngineInterface* CreateWebRtcMediaEngine( |
| 72 | webrtc::AudioDeviceModule* adm, webrtc::AudioDeviceModule* adm_sc, |
| 73 | cricket::WebRtcVideoEncoderFactory* encoder_factory, |
| 74 | cricket::WebRtcVideoDecoderFactory* decoder_factory) { |
| 75 | return new cricket::WebRtcMediaEngine(adm, adm_sc, encoder_factory, |
| 76 | decoder_factory); |
| 77 | } |
| 78 | |
| 79 | WRME_EXPORT |
| 80 | void DestroyWebRtcMediaEngine(cricket::MediaEngineInterface* media_engine) { |
| 81 | delete static_cast<cricket::WebRtcMediaEngine*>(media_engine); |
| 82 | } |
| 83 | #endif |
| 84 | |
| 85 | |
| 86 | namespace cricket { |
| 87 | |
| 88 | |
| 89 | static const int kDefaultLogSeverity = talk_base::LS_WARNING; |
| 90 | |
| 91 | static const int kMinVideoBitrate = 50; |
| 92 | static const int kStartVideoBitrate = 300; |
| 93 | static const int kMaxVideoBitrate = 2000; |
| 94 | static const int kDefaultConferenceModeMaxVideoBitrate = 500; |
| 95 | |
| 96 | static const int kVideoMtu = 1200; |
| 97 | |
| 98 | static const int kVideoRtpBufferSize = 65536; |
| 99 | |
| 100 | static const char kVp8PayloadName[] = "VP8"; |
| 101 | static const char kRedPayloadName[] = "red"; |
| 102 | static const char kFecPayloadName[] = "ulpfec"; |
| 103 | |
| 104 | static const int kDefaultNumberOfTemporalLayers = 1; // 1:1 |
| 105 | |
| 106 | static const int kTimestampDeltaInSecondsForWarning = 2; |
| 107 | |
| 108 | static const int kMaxExternalVideoCodecs = 8; |
| 109 | static const int kExternalVideoPayloadTypeBase = 120; |
| 110 | |
| 111 | // Static allocation of payload type values for external video codec. |
| 112 | static int GetExternalVideoPayloadType(int index) { |
| 113 | ASSERT(index >= 0 && index < kMaxExternalVideoCodecs); |
| 114 | return kExternalVideoPayloadTypeBase + index; |
| 115 | } |
| 116 | |
| 117 | static void LogMultiline(talk_base::LoggingSeverity sev, char* text) { |
| 118 | const char* delim = "\r\n"; |
| 119 | // TODO(fbarchard): Fix strtok lint warning. |
| 120 | for (char* tok = strtok(text, delim); tok; tok = strtok(NULL, delim)) { |
| 121 | LOG_V(sev) << tok; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | // Severity is an integer because it comes is assumed to be from command line. |
| 126 | static int SeverityToFilter(int severity) { |
| 127 | int filter = webrtc::kTraceNone; |
| 128 | switch (severity) { |
| 129 | case talk_base::LS_VERBOSE: |
| 130 | filter |= webrtc::kTraceAll; |
| 131 | case talk_base::LS_INFO: |
| 132 | filter |= (webrtc::kTraceStateInfo | webrtc::kTraceInfo); |
| 133 | case talk_base::LS_WARNING: |
| 134 | filter |= (webrtc::kTraceTerseInfo | webrtc::kTraceWarning); |
| 135 | case talk_base::LS_ERROR: |
| 136 | filter |= (webrtc::kTraceError | webrtc::kTraceCritical); |
| 137 | } |
| 138 | return filter; |
| 139 | } |
| 140 | |
| 141 | static const int kCpuMonitorPeriodMs = 2000; // 2 seconds. |
| 142 | |
| 143 | static const bool kNotSending = false; |
| 144 | |
| 145 | // Extension header for RTP timestamp offset, see RFC 5450 for details: |
| 146 | // http://tools.ietf.org/html/rfc5450 |
| 147 | static const char kRtpTimestampOffsetHeaderExtension[] = |
| 148 | "urn:ietf:params:rtp-hdrext:toffset"; |
| 149 | static const int kRtpTimeOffsetExtensionId = 2; |
| 150 | |
| 151 | // Extension header for absolute send time, see url for details: |
| 152 | // http://www.webrtc.org/experiments/rtp-hdrext/abs-send-time |
| 153 | static const char kRtpAbsoluteSendTimeHeaderExtension[] = |
| 154 | "http://www.webrtc.org/experiments/rtp-hdrext/abs-send-time"; |
| 155 | static const int kRtpAbsoluteSendTimeExtensionId = 3; |
| 156 | |
| 157 | static bool IsNackEnabled(const VideoCodec& codec) { |
| 158 | return codec.HasFeedbackParam(FeedbackParam(kRtcpFbParamNack, |
| 159 | kParamValueEmpty)); |
| 160 | } |
| 161 | |
| 162 | // Returns true if Receiver Estimated Max Bitrate is enabled. |
| 163 | static bool IsRembEnabled(const VideoCodec& codec) { |
| 164 | return codec.HasFeedbackParam(FeedbackParam(kRtcpFbParamRemb, |
| 165 | kParamValueEmpty)); |
| 166 | } |
| 167 | |
| 168 | struct FlushBlackFrameData : public talk_base::MessageData { |
| 169 | FlushBlackFrameData(uint32 s, int64 t) : ssrc(s), timestamp(t) { |
| 170 | } |
| 171 | uint32 ssrc; |
| 172 | int64 timestamp; |
| 173 | }; |
| 174 | |
| 175 | class WebRtcRenderAdapter : public webrtc::ExternalRenderer { |
| 176 | public: |
| 177 | explicit WebRtcRenderAdapter(VideoRenderer* renderer) |
| 178 | : renderer_(renderer), width_(0), height_(0), watermark_enabled_(false) { |
| 179 | } |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 180 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 181 | virtual ~WebRtcRenderAdapter() { |
| 182 | } |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 183 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 184 | void set_watermark_enabled(bool enable) { |
| 185 | talk_base::CritScope cs(&crit_); |
| 186 | watermark_enabled_ = enable; |
| 187 | } |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 188 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 189 | void SetRenderer(VideoRenderer* renderer) { |
| 190 | talk_base::CritScope cs(&crit_); |
| 191 | renderer_ = renderer; |
| 192 | // FrameSizeChange may have already been called when renderer was not set. |
| 193 | // If so we should call SetSize here. |
| 194 | // TODO(ronghuawu): Add unit test for this case. Didn't do it now |
| 195 | // because the WebRtcRenderAdapter is currently hiding in cc file. No |
| 196 | // good way to get access to it from the unit test. |
| 197 | if (width_ > 0 && height_ > 0 && renderer_ != NULL) { |
| 198 | if (!renderer_->SetSize(width_, height_, 0)) { |
| 199 | LOG(LS_ERROR) |
| 200 | << "WebRtcRenderAdapter SetRenderer failed to SetSize to: " |
| 201 | << width_ << "x" << height_; |
| 202 | } |
| 203 | } |
| 204 | } |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 205 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 206 | // Implementation of webrtc::ExternalRenderer. |
| 207 | virtual int FrameSizeChange(unsigned int width, unsigned int height, |
| 208 | unsigned int /*number_of_streams*/) { |
| 209 | talk_base::CritScope cs(&crit_); |
| 210 | width_ = width; |
| 211 | height_ = height; |
| 212 | LOG(LS_INFO) << "WebRtcRenderAdapter frame size changed to: " |
| 213 | << width << "x" << height; |
| 214 | if (renderer_ == NULL) { |
| 215 | LOG(LS_VERBOSE) << "WebRtcRenderAdapter the renderer has not been set. " |
| 216 | << "SetSize will be called later in SetRenderer."; |
| 217 | return 0; |
| 218 | } |
| 219 | return renderer_->SetSize(width_, height_, 0) ? 0 : -1; |
| 220 | } |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 221 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 222 | virtual int DeliverFrame(unsigned char* buffer, int buffer_size, |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 223 | uint32_t time_stamp, int64_t render_time |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 224 | , void* handle |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 225 | ) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 226 | talk_base::CritScope cs(&crit_); |
| 227 | frame_rate_tracker_.Update(1); |
| 228 | if (renderer_ == NULL) { |
| 229 | return 0; |
| 230 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 231 | // Convert 90K rtp timestamp to ns timestamp. |
| 232 | int64 rtp_time_stamp_in_ns = (time_stamp / 90) * |
| 233 | talk_base::kNumNanosecsPerMillisec; |
| 234 | // Convert milisecond render time to ns timestamp. |
| 235 | int64 render_time_stamp_in_ns = render_time * |
| 236 | talk_base::kNumNanosecsPerMillisec; |
| 237 | // Send the rtp timestamp to renderer as the VideoFrame timestamp. |
| 238 | // and the render timestamp as the VideoFrame elapsed_time. |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 239 | if (handle == NULL) { |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 240 | return DeliverBufferFrame(buffer, buffer_size, render_time_stamp_in_ns, |
| 241 | rtp_time_stamp_in_ns); |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 242 | } else { |
| 243 | return DeliverTextureFrame(handle, render_time_stamp_in_ns, |
| 244 | rtp_time_stamp_in_ns); |
| 245 | } |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | virtual bool IsTextureSupported() { return true; } |
| 249 | |
| 250 | int DeliverBufferFrame(unsigned char* buffer, int buffer_size, |
| 251 | int64 elapsed_time, int64 time_stamp) { |
| 252 | WebRtcVideoFrame video_frame; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 253 | video_frame.Attach(buffer, buffer_size, width_, height_, |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 254 | 1, 1, elapsed_time, time_stamp, 0); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 255 | |
| 256 | |
| 257 | // Sanity check on decoded frame size. |
| 258 | if (buffer_size != static_cast<int>(VideoFrame::SizeOf(width_, height_))) { |
| 259 | LOG(LS_WARNING) << "WebRtcRenderAdapter received a strange frame size: " |
| 260 | << buffer_size; |
| 261 | } |
| 262 | |
| 263 | int ret = renderer_->RenderFrame(&video_frame) ? 0 : -1; |
| 264 | uint8* buffer_temp; |
| 265 | size_t buffer_size_temp; |
| 266 | video_frame.Detach(&buffer_temp, &buffer_size_temp); |
| 267 | return ret; |
| 268 | } |
| 269 | |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 270 | int DeliverTextureFrame(void* handle, int64 elapsed_time, int64 time_stamp) { |
| 271 | WebRtcTextureVideoFrame video_frame( |
| 272 | static_cast<webrtc::NativeHandle*>(handle), width_, height_, |
| 273 | elapsed_time, time_stamp); |
| 274 | return renderer_->RenderFrame(&video_frame); |
| 275 | } |
| 276 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 277 | unsigned int width() { |
| 278 | talk_base::CritScope cs(&crit_); |
| 279 | return width_; |
| 280 | } |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 281 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 282 | unsigned int height() { |
| 283 | talk_base::CritScope cs(&crit_); |
| 284 | return height_; |
| 285 | } |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 286 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 287 | int framerate() { |
| 288 | talk_base::CritScope cs(&crit_); |
henrike@webrtc.org | 28654cb | 2013-07-22 21:07:49 +0000 | [diff] [blame] | 289 | return static_cast<int>(frame_rate_tracker_.units_second()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 290 | } |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 291 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 292 | VideoRenderer* renderer() { |
| 293 | talk_base::CritScope cs(&crit_); |
| 294 | return renderer_; |
| 295 | } |
| 296 | |
| 297 | private: |
| 298 | talk_base::CriticalSection crit_; |
| 299 | VideoRenderer* renderer_; |
| 300 | unsigned int width_; |
| 301 | unsigned int height_; |
| 302 | talk_base::RateTracker frame_rate_tracker_; |
| 303 | bool watermark_enabled_; |
| 304 | }; |
| 305 | |
| 306 | class WebRtcDecoderObserver : public webrtc::ViEDecoderObserver { |
| 307 | public: |
| 308 | explicit WebRtcDecoderObserver(int video_channel) |
| 309 | : video_channel_(video_channel), |
| 310 | framerate_(0), |
| 311 | bitrate_(0), |
| 312 | firs_requested_(0) { |
| 313 | } |
| 314 | |
| 315 | // virtual functions from VieDecoderObserver. |
| 316 | virtual void IncomingCodecChanged(const int videoChannel, |
| 317 | const webrtc::VideoCodec& videoCodec) {} |
| 318 | virtual void IncomingRate(const int videoChannel, |
| 319 | const unsigned int framerate, |
| 320 | const unsigned int bitrate) { |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 321 | talk_base::CritScope cs(&crit_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 322 | ASSERT(video_channel_ == videoChannel); |
| 323 | framerate_ = framerate; |
| 324 | bitrate_ = bitrate; |
| 325 | } |
| 326 | virtual void RequestNewKeyFrame(const int videoChannel) { |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 327 | talk_base::CritScope cs(&crit_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 328 | ASSERT(video_channel_ == videoChannel); |
| 329 | ++firs_requested_; |
| 330 | } |
| 331 | |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 332 | int framerate() const { |
| 333 | talk_base::CritScope cs(&crit_); |
| 334 | return framerate_; |
| 335 | } |
| 336 | int bitrate() const { |
| 337 | talk_base::CritScope cs(&crit_); |
| 338 | return bitrate_; |
| 339 | } |
| 340 | int firs_requested() const { |
| 341 | talk_base::CritScope cs(&crit_); |
| 342 | return firs_requested_; |
| 343 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 344 | |
| 345 | private: |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 346 | mutable talk_base::CriticalSection crit_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 347 | int video_channel_; |
| 348 | int framerate_; |
| 349 | int bitrate_; |
| 350 | int firs_requested_; |
| 351 | }; |
| 352 | |
| 353 | class WebRtcEncoderObserver : public webrtc::ViEEncoderObserver { |
| 354 | public: |
| 355 | explicit WebRtcEncoderObserver(int video_channel) |
| 356 | : video_channel_(video_channel), |
| 357 | framerate_(0), |
| 358 | bitrate_(0) { |
| 359 | } |
| 360 | |
| 361 | // virtual functions from VieEncoderObserver. |
| 362 | virtual void OutgoingRate(const int videoChannel, |
| 363 | const unsigned int framerate, |
| 364 | const unsigned int bitrate) { |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 365 | talk_base::CritScope cs(&crit_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 366 | ASSERT(video_channel_ == videoChannel); |
| 367 | framerate_ = framerate; |
| 368 | bitrate_ = bitrate; |
| 369 | } |
| 370 | |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 371 | int framerate() const { |
| 372 | talk_base::CritScope cs(&crit_); |
| 373 | return framerate_; |
| 374 | } |
| 375 | int bitrate() const { |
| 376 | talk_base::CritScope cs(&crit_); |
| 377 | return bitrate_; |
| 378 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 379 | |
| 380 | private: |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 381 | mutable talk_base::CriticalSection crit_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 382 | int video_channel_; |
| 383 | int framerate_; |
| 384 | int bitrate_; |
| 385 | }; |
| 386 | |
| 387 | class WebRtcLocalStreamInfo { |
| 388 | public: |
| 389 | WebRtcLocalStreamInfo() |
| 390 | : width_(0), height_(0), elapsed_time_(-1), time_stamp_(-1) {} |
| 391 | size_t width() const { |
| 392 | talk_base::CritScope cs(&crit_); |
| 393 | return width_; |
| 394 | } |
| 395 | size_t height() const { |
| 396 | talk_base::CritScope cs(&crit_); |
| 397 | return height_; |
| 398 | } |
| 399 | int64 elapsed_time() const { |
| 400 | talk_base::CritScope cs(&crit_); |
| 401 | return elapsed_time_; |
| 402 | } |
| 403 | int64 time_stamp() const { |
| 404 | talk_base::CritScope cs(&crit_); |
| 405 | return time_stamp_; |
| 406 | } |
| 407 | int framerate() { |
| 408 | talk_base::CritScope cs(&crit_); |
henrike@webrtc.org | 28654cb | 2013-07-22 21:07:49 +0000 | [diff] [blame] | 409 | return static_cast<int>(rate_tracker_.units_second()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 410 | } |
| 411 | void GetLastFrameInfo( |
| 412 | size_t* width, size_t* height, int64* elapsed_time) const { |
| 413 | talk_base::CritScope cs(&crit_); |
| 414 | *width = width_; |
| 415 | *height = height_; |
| 416 | *elapsed_time = elapsed_time_; |
| 417 | } |
| 418 | |
| 419 | void UpdateFrame(const VideoFrame* frame) { |
| 420 | talk_base::CritScope cs(&crit_); |
| 421 | |
| 422 | width_ = frame->GetWidth(); |
| 423 | height_ = frame->GetHeight(); |
| 424 | elapsed_time_ = frame->GetElapsedTime(); |
| 425 | time_stamp_ = frame->GetTimeStamp(); |
| 426 | |
| 427 | rate_tracker_.Update(1); |
| 428 | } |
| 429 | |
| 430 | private: |
| 431 | mutable talk_base::CriticalSection crit_; |
| 432 | size_t width_; |
| 433 | size_t height_; |
| 434 | int64 elapsed_time_; |
| 435 | int64 time_stamp_; |
| 436 | talk_base::RateTracker rate_tracker_; |
| 437 | |
| 438 | DISALLOW_COPY_AND_ASSIGN(WebRtcLocalStreamInfo); |
| 439 | }; |
| 440 | |
| 441 | // WebRtcVideoChannelRecvInfo is a container class with members such as renderer |
| 442 | // and a decoder observer that is used by receive channels. |
| 443 | // It must exist as long as the receive channel is connected to renderer or a |
| 444 | // decoder observer in this class and methods in the class should only be called |
| 445 | // from the worker thread. |
| 446 | class WebRtcVideoChannelRecvInfo { |
| 447 | public: |
| 448 | typedef std::map<int, webrtc::VideoDecoder*> DecoderMap; // key: payload type |
| 449 | explicit WebRtcVideoChannelRecvInfo(int channel_id) |
| 450 | : channel_id_(channel_id), |
| 451 | render_adapter_(NULL), |
| 452 | decoder_observer_(channel_id) { |
| 453 | } |
| 454 | int channel_id() { return channel_id_; } |
| 455 | void SetRenderer(VideoRenderer* renderer) { |
| 456 | render_adapter_.SetRenderer(renderer); |
| 457 | } |
| 458 | WebRtcRenderAdapter* render_adapter() { return &render_adapter_; } |
| 459 | WebRtcDecoderObserver* decoder_observer() { return &decoder_observer_; } |
| 460 | void RegisterDecoder(int pl_type, webrtc::VideoDecoder* decoder) { |
| 461 | ASSERT(!IsDecoderRegistered(pl_type)); |
| 462 | registered_decoders_[pl_type] = decoder; |
| 463 | } |
| 464 | bool IsDecoderRegistered(int pl_type) { |
| 465 | return registered_decoders_.count(pl_type) != 0; |
| 466 | } |
| 467 | const DecoderMap& registered_decoders() { |
| 468 | return registered_decoders_; |
| 469 | } |
| 470 | void ClearRegisteredDecoders() { |
| 471 | registered_decoders_.clear(); |
| 472 | } |
| 473 | |
| 474 | private: |
| 475 | int channel_id_; // Webrtc video channel number. |
| 476 | // Renderer for this channel. |
| 477 | WebRtcRenderAdapter render_adapter_; |
| 478 | WebRtcDecoderObserver decoder_observer_; |
| 479 | DecoderMap registered_decoders_; |
| 480 | }; |
| 481 | |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 482 | class WebRtcOveruseObserver : public webrtc::CpuOveruseObserver { |
| 483 | public: |
| 484 | explicit WebRtcOveruseObserver(CoordinatedVideoAdapter* video_adapter) |
| 485 | : video_adapter_(video_adapter), |
| 486 | enabled_(false) { |
| 487 | } |
| 488 | |
| 489 | // TODO(mflodman): Consider sending resolution as part of event, to let |
| 490 | // adapter know what resolution the request is based on. Helps eliminate stale |
| 491 | // data, race conditions. |
| 492 | virtual void OveruseDetected() OVERRIDE { |
| 493 | talk_base::CritScope cs(&crit_); |
| 494 | if (!enabled_) { |
| 495 | return; |
| 496 | } |
| 497 | |
| 498 | video_adapter_->OnCpuResolutionRequest(CoordinatedVideoAdapter::DOWNGRADE); |
| 499 | } |
| 500 | |
| 501 | virtual void NormalUsage() OVERRIDE { |
| 502 | talk_base::CritScope cs(&crit_); |
| 503 | if (!enabled_) { |
| 504 | return; |
| 505 | } |
| 506 | |
| 507 | video_adapter_->OnCpuResolutionRequest(CoordinatedVideoAdapter::UPGRADE); |
| 508 | } |
| 509 | |
| 510 | void Enable(bool enable) { |
| 511 | talk_base::CritScope cs(&crit_); |
| 512 | enabled_ = enable; |
| 513 | } |
| 514 | |
| 515 | private: |
| 516 | CoordinatedVideoAdapter* video_adapter_; |
| 517 | bool enabled_; |
| 518 | talk_base::CriticalSection crit_; |
| 519 | }; |
| 520 | |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 521 | |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 522 | class WebRtcVideoChannelSendInfo : public sigslot::has_slots<> { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 523 | public: |
| 524 | typedef std::map<int, webrtc::VideoEncoder*> EncoderMap; // key: payload type |
| 525 | WebRtcVideoChannelSendInfo(int channel_id, int capture_id, |
| 526 | webrtc::ViEExternalCapture* external_capture, |
| 527 | talk_base::CpuMonitor* cpu_monitor) |
| 528 | : channel_id_(channel_id), |
| 529 | capture_id_(capture_id), |
| 530 | sending_(false), |
| 531 | muted_(false), |
| 532 | video_capturer_(NULL), |
| 533 | encoder_observer_(channel_id), |
| 534 | external_capture_(external_capture), |
| 535 | capturer_updated_(false), |
| 536 | interval_(0), |
mallinath@webrtc.org | a27be8e | 2013-09-27 23:04:10 +0000 | [diff] [blame] | 537 | video_adapter_(new CoordinatedVideoAdapter), |
| 538 | cpu_monitor_(cpu_monitor) { |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 539 | overuse_observer_.reset(new WebRtcOveruseObserver(video_adapter_.get())); |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 540 | SignalCpuAdaptationUnable.repeat(video_adapter_->SignalCpuAdaptationUnable); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 541 | if (cpu_monitor) { |
| 542 | cpu_monitor->SignalUpdate.connect( |
| 543 | video_adapter_.get(), &CoordinatedVideoAdapter::OnCpuLoadUpdated); |
| 544 | } |
| 545 | } |
| 546 | |
| 547 | int channel_id() const { return channel_id_; } |
| 548 | int capture_id() const { return capture_id_; } |
| 549 | void set_sending(bool sending) { sending_ = sending; } |
| 550 | bool sending() const { return sending_; } |
| 551 | void set_muted(bool on) { |
| 552 | // TODO(asapersson): add support. |
| 553 | // video_adapter_->SetBlackOutput(on); |
| 554 | muted_ = on; |
| 555 | } |
| 556 | bool muted() {return muted_; } |
| 557 | |
| 558 | WebRtcEncoderObserver* encoder_observer() { return &encoder_observer_; } |
| 559 | webrtc::ViEExternalCapture* external_capture() { return external_capture_; } |
| 560 | const VideoFormat& video_format() const { |
| 561 | return video_format_; |
| 562 | } |
| 563 | void set_video_format(const VideoFormat& video_format) { |
| 564 | video_format_ = video_format; |
| 565 | if (video_format_ != cricket::VideoFormat()) { |
| 566 | interval_ = video_format_.interval; |
| 567 | } |
| 568 | video_adapter_->OnOutputFormatRequest(video_format_); |
| 569 | } |
| 570 | void set_interval(int64 interval) { |
| 571 | if (video_format() == cricket::VideoFormat()) { |
| 572 | interval_ = interval; |
| 573 | } |
| 574 | } |
| 575 | int64 interval() { return interval_; } |
| 576 | |
| 577 | void InitializeAdapterOutputFormat(const webrtc::VideoCodec& codec) { |
| 578 | VideoFormat format(codec.width, codec.height, |
| 579 | VideoFormat::FpsToInterval(codec.maxFramerate), |
| 580 | FOURCC_I420); |
| 581 | if (video_adapter_->output_format().IsSize0x0()) { |
| 582 | video_adapter_->SetOutputFormat(format); |
| 583 | } |
| 584 | } |
| 585 | |
| 586 | bool AdaptFrame(const VideoFrame* in_frame, const VideoFrame** out_frame) { |
| 587 | *out_frame = NULL; |
| 588 | return video_adapter_->AdaptFrame(in_frame, out_frame); |
| 589 | } |
| 590 | int CurrentAdaptReason() const { |
| 591 | return video_adapter_->adapt_reason(); |
| 592 | } |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 593 | webrtc::CpuOveruseObserver* overuse_observer() { |
| 594 | return overuse_observer_.get(); |
| 595 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 596 | |
| 597 | StreamParams* stream_params() { return stream_params_.get(); } |
| 598 | void set_stream_params(const StreamParams& sp) { |
| 599 | stream_params_.reset(new StreamParams(sp)); |
| 600 | } |
| 601 | void ClearStreamParams() { stream_params_.reset(); } |
| 602 | bool has_ssrc(uint32 local_ssrc) const { |
| 603 | return !stream_params_ ? false : |
| 604 | stream_params_->has_ssrc(local_ssrc); |
| 605 | } |
| 606 | WebRtcLocalStreamInfo* local_stream_info() { |
| 607 | return &local_stream_info_; |
| 608 | } |
| 609 | VideoCapturer* video_capturer() { |
| 610 | return video_capturer_; |
| 611 | } |
| 612 | void set_video_capturer(VideoCapturer* video_capturer) { |
| 613 | if (video_capturer == video_capturer_) { |
| 614 | return; |
| 615 | } |
| 616 | capturer_updated_ = true; |
| 617 | video_capturer_ = video_capturer; |
| 618 | if (video_capturer && !video_capturer->IsScreencast()) { |
| 619 | const VideoFormat* capture_format = video_capturer->GetCaptureFormat(); |
| 620 | if (capture_format) { |
henrike@webrtc.org | 28654cb | 2013-07-22 21:07:49 +0000 | [diff] [blame] | 621 | // TODO(thorcarpenter): This is broken. Video capturer doesn't have |
| 622 | // a capture format until the capturer is started. So, if |
| 623 | // the capturer is started immediately after calling set_video_capturer |
| 624 | // video adapter may not have the input format set, the interval may |
| 625 | // be zero, and all frames may be dropped. |
| 626 | // Consider fixing this by having video_adapter keep a pointer to the |
| 627 | // video capturer. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 628 | video_adapter_->SetInputFormat(*capture_format); |
| 629 | } |
| 630 | } |
| 631 | } |
| 632 | |
| 633 | void ApplyCpuOptions(const VideoOptions& options) { |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 634 | bool cpu_adapt, cpu_smoothing, adapt_third; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 635 | float low, med, high; |
| 636 | if (options.adapt_input_to_cpu_usage.Get(&cpu_adapt)) { |
| 637 | video_adapter_->set_cpu_adaptation(cpu_adapt); |
| 638 | } |
henrike@webrtc.org | 28654cb | 2013-07-22 21:07:49 +0000 | [diff] [blame] | 639 | if (options.adapt_cpu_with_smoothing.Get(&cpu_smoothing)) { |
| 640 | video_adapter_->set_cpu_smoothing(cpu_smoothing); |
| 641 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 642 | if (options.process_adaptation_threshhold.Get(&med)) { |
| 643 | video_adapter_->set_process_threshold(med); |
| 644 | } |
| 645 | if (options.system_low_adaptation_threshhold.Get(&low)) { |
| 646 | video_adapter_->set_low_system_threshold(low); |
| 647 | } |
| 648 | if (options.system_high_adaptation_threshhold.Get(&high)) { |
| 649 | video_adapter_->set_high_system_threshold(high); |
| 650 | } |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 651 | if (options.video_adapt_third.Get(&adapt_third)) { |
| 652 | video_adapter_->set_scale_third(adapt_third); |
| 653 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 654 | } |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 655 | |
| 656 | void SetCpuOveruseDetection(bool enable) { |
mallinath@webrtc.org | a27be8e | 2013-09-27 23:04:10 +0000 | [diff] [blame] | 657 | if (cpu_monitor_ && enable) { |
| 658 | cpu_monitor_->SignalUpdate.disconnect(video_adapter_.get()); |
| 659 | } |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 660 | overuse_observer_->Enable(enable); |
| 661 | video_adapter_->set_cpu_adaptation(enable); |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 662 | } |
| 663 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 664 | void ProcessFrame(const VideoFrame& original_frame, bool mute, |
| 665 | VideoFrame** processed_frame) { |
| 666 | if (!mute) { |
| 667 | *processed_frame = original_frame.Copy(); |
| 668 | } else { |
| 669 | WebRtcVideoFrame* black_frame = new WebRtcVideoFrame(); |
henrike@webrtc.org | 28654cb | 2013-07-22 21:07:49 +0000 | [diff] [blame] | 670 | black_frame->InitToBlack(static_cast<int>(original_frame.GetWidth()), |
| 671 | static_cast<int>(original_frame.GetHeight()), |
| 672 | 1, 1, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 673 | original_frame.GetElapsedTime(), |
| 674 | original_frame.GetTimeStamp()); |
| 675 | *processed_frame = black_frame; |
| 676 | } |
| 677 | local_stream_info_.UpdateFrame(*processed_frame); |
| 678 | } |
| 679 | void RegisterEncoder(int pl_type, webrtc::VideoEncoder* encoder) { |
| 680 | ASSERT(!IsEncoderRegistered(pl_type)); |
| 681 | registered_encoders_[pl_type] = encoder; |
| 682 | } |
| 683 | bool IsEncoderRegistered(int pl_type) { |
| 684 | return registered_encoders_.count(pl_type) != 0; |
| 685 | } |
| 686 | const EncoderMap& registered_encoders() { |
| 687 | return registered_encoders_; |
| 688 | } |
| 689 | void ClearRegisteredEncoders() { |
| 690 | registered_encoders_.clear(); |
| 691 | } |
| 692 | |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 693 | sigslot::repeater0<> SignalCpuAdaptationUnable; |
| 694 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 695 | private: |
| 696 | int channel_id_; |
| 697 | int capture_id_; |
| 698 | bool sending_; |
| 699 | bool muted_; |
| 700 | VideoCapturer* video_capturer_; |
| 701 | WebRtcEncoderObserver encoder_observer_; |
| 702 | webrtc::ViEExternalCapture* external_capture_; |
| 703 | EncoderMap registered_encoders_; |
| 704 | |
| 705 | VideoFormat video_format_; |
| 706 | |
| 707 | talk_base::scoped_ptr<StreamParams> stream_params_; |
| 708 | |
| 709 | WebRtcLocalStreamInfo local_stream_info_; |
| 710 | |
| 711 | bool capturer_updated_; |
| 712 | |
| 713 | int64 interval_; |
| 714 | |
| 715 | talk_base::scoped_ptr<CoordinatedVideoAdapter> video_adapter_; |
mallinath@webrtc.org | a27be8e | 2013-09-27 23:04:10 +0000 | [diff] [blame] | 716 | talk_base::CpuMonitor* cpu_monitor_; |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 717 | talk_base::scoped_ptr<WebRtcOveruseObserver> overuse_observer_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 718 | }; |
| 719 | |
| 720 | const WebRtcVideoEngine::VideoCodecPref |
| 721 | WebRtcVideoEngine::kVideoCodecPrefs[] = { |
| 722 | {kVp8PayloadName, 100, 0}, |
| 723 | {kRedPayloadName, 116, 1}, |
| 724 | {kFecPayloadName, 117, 2}, |
| 725 | }; |
| 726 | |
| 727 | // The formats are sorted by the descending order of width. We use the order to |
| 728 | // find the next format for CPU and bandwidth adaptation. |
| 729 | const VideoFormatPod WebRtcVideoEngine::kVideoFormats[] = { |
| 730 | {1280, 800, FPS_TO_INTERVAL(30), FOURCC_ANY}, |
| 731 | {1280, 720, FPS_TO_INTERVAL(30), FOURCC_ANY}, |
| 732 | {960, 600, FPS_TO_INTERVAL(30), FOURCC_ANY}, |
| 733 | {960, 540, FPS_TO_INTERVAL(30), FOURCC_ANY}, |
| 734 | {640, 400, FPS_TO_INTERVAL(30), FOURCC_ANY}, |
| 735 | {640, 360, FPS_TO_INTERVAL(30), FOURCC_ANY}, |
| 736 | {640, 480, FPS_TO_INTERVAL(30), FOURCC_ANY}, |
| 737 | {480, 300, FPS_TO_INTERVAL(30), FOURCC_ANY}, |
| 738 | {480, 270, FPS_TO_INTERVAL(30), FOURCC_ANY}, |
| 739 | {480, 360, FPS_TO_INTERVAL(30), FOURCC_ANY}, |
| 740 | {320, 200, FPS_TO_INTERVAL(30), FOURCC_ANY}, |
| 741 | {320, 180, FPS_TO_INTERVAL(30), FOURCC_ANY}, |
| 742 | {320, 240, FPS_TO_INTERVAL(30), FOURCC_ANY}, |
| 743 | {240, 150, FPS_TO_INTERVAL(30), FOURCC_ANY}, |
| 744 | {240, 135, FPS_TO_INTERVAL(30), FOURCC_ANY}, |
| 745 | {240, 180, FPS_TO_INTERVAL(30), FOURCC_ANY}, |
| 746 | {160, 100, FPS_TO_INTERVAL(30), FOURCC_ANY}, |
| 747 | {160, 90, FPS_TO_INTERVAL(30), FOURCC_ANY}, |
| 748 | {160, 120, FPS_TO_INTERVAL(30), FOURCC_ANY}, |
| 749 | }; |
| 750 | |
| 751 | const VideoFormatPod WebRtcVideoEngine::kDefaultVideoFormat = |
| 752 | {640, 400, FPS_TO_INTERVAL(30), FOURCC_ANY}; |
| 753 | |
| 754 | static void UpdateVideoCodec(const cricket::VideoFormat& video_format, |
| 755 | webrtc::VideoCodec* target_codec) { |
| 756 | if ((target_codec == NULL) || (video_format == cricket::VideoFormat())) { |
| 757 | return; |
| 758 | } |
| 759 | target_codec->width = video_format.width; |
| 760 | target_codec->height = video_format.height; |
| 761 | target_codec->maxFramerate = cricket::VideoFormat::IntervalToFps( |
| 762 | video_format.interval); |
| 763 | } |
| 764 | |
| 765 | WebRtcVideoEngine::WebRtcVideoEngine() { |
| 766 | Construct(new ViEWrapper(), new ViETraceWrapper(), NULL, |
| 767 | new talk_base::CpuMonitor(NULL)); |
| 768 | } |
| 769 | |
| 770 | WebRtcVideoEngine::WebRtcVideoEngine(WebRtcVoiceEngine* voice_engine, |
| 771 | ViEWrapper* vie_wrapper, |
| 772 | talk_base::CpuMonitor* cpu_monitor) { |
| 773 | Construct(vie_wrapper, new ViETraceWrapper(), voice_engine, cpu_monitor); |
| 774 | } |
| 775 | |
| 776 | WebRtcVideoEngine::WebRtcVideoEngine(WebRtcVoiceEngine* voice_engine, |
| 777 | ViEWrapper* vie_wrapper, |
| 778 | ViETraceWrapper* tracing, |
| 779 | talk_base::CpuMonitor* cpu_monitor) { |
| 780 | Construct(vie_wrapper, tracing, voice_engine, cpu_monitor); |
| 781 | } |
| 782 | |
| 783 | void WebRtcVideoEngine::Construct(ViEWrapper* vie_wrapper, |
| 784 | ViETraceWrapper* tracing, |
| 785 | WebRtcVoiceEngine* voice_engine, |
| 786 | talk_base::CpuMonitor* cpu_monitor) { |
| 787 | LOG(LS_INFO) << "WebRtcVideoEngine::WebRtcVideoEngine"; |
| 788 | worker_thread_ = NULL; |
| 789 | vie_wrapper_.reset(vie_wrapper); |
| 790 | vie_wrapper_base_initialized_ = false; |
| 791 | tracing_.reset(tracing); |
| 792 | voice_engine_ = voice_engine; |
| 793 | initialized_ = false; |
| 794 | SetTraceFilter(SeverityToFilter(kDefaultLogSeverity)); |
| 795 | render_module_.reset(new WebRtcPassthroughRender()); |
| 796 | local_renderer_w_ = local_renderer_h_ = 0; |
| 797 | local_renderer_ = NULL; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 798 | capture_started_ = false; |
| 799 | decoder_factory_ = NULL; |
| 800 | encoder_factory_ = NULL; |
| 801 | cpu_monitor_.reset(cpu_monitor); |
| 802 | |
| 803 | SetTraceOptions(""); |
| 804 | if (tracing_->SetTraceCallback(this) != 0) { |
| 805 | LOG_RTCERR1(SetTraceCallback, this); |
| 806 | } |
| 807 | |
| 808 | // Set default quality levels for our supported codecs. We override them here |
| 809 | // if we know your cpu performance is low, and they can be updated explicitly |
| 810 | // by calling SetDefaultCodec. For example by a flute preference setting, or |
| 811 | // by the server with a jec in response to our reported system info. |
| 812 | VideoCodec max_codec(kVideoCodecPrefs[0].payload_type, |
| 813 | kVideoCodecPrefs[0].name, |
| 814 | kDefaultVideoFormat.width, |
| 815 | kDefaultVideoFormat.height, |
| 816 | VideoFormat::IntervalToFps(kDefaultVideoFormat.interval), |
| 817 | 0); |
| 818 | if (!SetDefaultCodec(max_codec)) { |
| 819 | LOG(LS_ERROR) << "Failed to initialize list of supported codec types"; |
| 820 | } |
| 821 | |
| 822 | |
| 823 | // Load our RTP Header extensions. |
| 824 | rtp_header_extensions_.push_back( |
| 825 | RtpHeaderExtension(kRtpTimestampOffsetHeaderExtension, |
| 826 | kRtpTimeOffsetExtensionId)); |
| 827 | rtp_header_extensions_.push_back( |
| 828 | RtpHeaderExtension(kRtpAbsoluteSendTimeHeaderExtension, |
| 829 | kRtpAbsoluteSendTimeExtensionId)); |
| 830 | } |
| 831 | |
| 832 | WebRtcVideoEngine::~WebRtcVideoEngine() { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 833 | LOG(LS_INFO) << "WebRtcVideoEngine::~WebRtcVideoEngine"; |
| 834 | if (initialized_) { |
| 835 | Terminate(); |
| 836 | } |
| 837 | if (encoder_factory_) { |
| 838 | encoder_factory_->RemoveObserver(this); |
| 839 | } |
| 840 | tracing_->SetTraceCallback(NULL); |
| 841 | // Test to see if the media processor was deregistered properly. |
| 842 | ASSERT(SignalMediaFrame.is_empty()); |
| 843 | } |
| 844 | |
| 845 | bool WebRtcVideoEngine::Init(talk_base::Thread* worker_thread) { |
| 846 | LOG(LS_INFO) << "WebRtcVideoEngine::Init"; |
| 847 | worker_thread_ = worker_thread; |
| 848 | ASSERT(worker_thread_ != NULL); |
| 849 | |
| 850 | cpu_monitor_->set_thread(worker_thread_); |
| 851 | if (!cpu_monitor_->Start(kCpuMonitorPeriodMs)) { |
| 852 | LOG(LS_ERROR) << "Failed to start CPU monitor."; |
| 853 | cpu_monitor_.reset(); |
| 854 | } |
| 855 | |
| 856 | bool result = InitVideoEngine(); |
| 857 | if (result) { |
| 858 | LOG(LS_INFO) << "VideoEngine Init done"; |
| 859 | } else { |
| 860 | LOG(LS_ERROR) << "VideoEngine Init failed, releasing"; |
| 861 | Terminate(); |
| 862 | } |
| 863 | return result; |
| 864 | } |
| 865 | |
| 866 | bool WebRtcVideoEngine::InitVideoEngine() { |
| 867 | LOG(LS_INFO) << "WebRtcVideoEngine::InitVideoEngine"; |
| 868 | |
| 869 | // Init WebRTC VideoEngine. |
| 870 | if (!vie_wrapper_base_initialized_) { |
| 871 | if (vie_wrapper_->base()->Init() != 0) { |
| 872 | LOG_RTCERR0(Init); |
| 873 | return false; |
| 874 | } |
| 875 | vie_wrapper_base_initialized_ = true; |
| 876 | } |
| 877 | |
| 878 | // Log the VoiceEngine version info. |
| 879 | char buffer[1024] = ""; |
| 880 | if (vie_wrapper_->base()->GetVersion(buffer) != 0) { |
| 881 | LOG_RTCERR0(GetVersion); |
| 882 | return false; |
| 883 | } |
| 884 | |
| 885 | LOG(LS_INFO) << "WebRtc VideoEngine Version:"; |
| 886 | LogMultiline(talk_base::LS_INFO, buffer); |
| 887 | |
| 888 | // Hook up to VoiceEngine for sync purposes, if supplied. |
| 889 | if (!voice_engine_) { |
| 890 | LOG(LS_WARNING) << "NULL voice engine"; |
| 891 | } else if ((vie_wrapper_->base()->SetVoiceEngine( |
| 892 | voice_engine_->voe()->engine())) != 0) { |
| 893 | LOG_RTCERR0(SetVoiceEngine); |
| 894 | return false; |
| 895 | } |
| 896 | |
| 897 | // Register our custom render module. |
| 898 | if (vie_wrapper_->render()->RegisterVideoRenderModule( |
| 899 | *render_module_.get()) != 0) { |
| 900 | LOG_RTCERR0(RegisterVideoRenderModule); |
| 901 | return false; |
| 902 | } |
| 903 | |
| 904 | initialized_ = true; |
| 905 | return true; |
| 906 | } |
| 907 | |
| 908 | void WebRtcVideoEngine::Terminate() { |
| 909 | LOG(LS_INFO) << "WebRtcVideoEngine::Terminate"; |
| 910 | initialized_ = false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 911 | |
| 912 | if (vie_wrapper_->render()->DeRegisterVideoRenderModule( |
| 913 | *render_module_.get()) != 0) { |
| 914 | LOG_RTCERR0(DeRegisterVideoRenderModule); |
| 915 | } |
| 916 | |
| 917 | if (vie_wrapper_->base()->SetVoiceEngine(NULL) != 0) { |
| 918 | LOG_RTCERR0(SetVoiceEngine); |
| 919 | } |
| 920 | |
| 921 | cpu_monitor_->Stop(); |
| 922 | } |
| 923 | |
| 924 | int WebRtcVideoEngine::GetCapabilities() { |
| 925 | return VIDEO_RECV | VIDEO_SEND; |
| 926 | } |
| 927 | |
mallinath@webrtc.org | a27be8e | 2013-09-27 23:04:10 +0000 | [diff] [blame] | 928 | bool WebRtcVideoEngine::SetOptions(const VideoOptions &options) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 929 | return true; |
| 930 | } |
| 931 | |
| 932 | bool WebRtcVideoEngine::SetDefaultEncoderConfig( |
| 933 | const VideoEncoderConfig& config) { |
| 934 | return SetDefaultCodec(config.max_codec); |
| 935 | } |
| 936 | |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 937 | VideoEncoderConfig WebRtcVideoEngine::GetDefaultEncoderConfig() const { |
| 938 | ASSERT(!video_codecs_.empty()); |
| 939 | VideoCodec max_codec(kVideoCodecPrefs[0].payload_type, |
| 940 | kVideoCodecPrefs[0].name, |
| 941 | video_codecs_[0].width, |
| 942 | video_codecs_[0].height, |
| 943 | video_codecs_[0].framerate, |
| 944 | 0); |
| 945 | return VideoEncoderConfig(max_codec); |
| 946 | } |
| 947 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 948 | // SetDefaultCodec may be called while the capturer is running. For example, a |
| 949 | // test call is started in a page with QVGA default codec, and then a real call |
| 950 | // is started in another page with VGA default codec. This is the corner case |
| 951 | // and happens only when a session is started. We ignore this case currently. |
| 952 | bool WebRtcVideoEngine::SetDefaultCodec(const VideoCodec& codec) { |
| 953 | if (!RebuildCodecList(codec)) { |
| 954 | LOG(LS_WARNING) << "Failed to RebuildCodecList"; |
| 955 | return false; |
| 956 | } |
| 957 | |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 958 | ASSERT(!video_codecs_.empty()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 959 | default_codec_format_ = VideoFormat( |
| 960 | video_codecs_[0].width, |
| 961 | video_codecs_[0].height, |
| 962 | VideoFormat::FpsToInterval(video_codecs_[0].framerate), |
| 963 | FOURCC_ANY); |
| 964 | return true; |
| 965 | } |
| 966 | |
| 967 | WebRtcVideoMediaChannel* WebRtcVideoEngine::CreateChannel( |
| 968 | VoiceMediaChannel* voice_channel) { |
| 969 | WebRtcVideoMediaChannel* channel = |
| 970 | new WebRtcVideoMediaChannel(this, voice_channel); |
| 971 | if (!channel->Init()) { |
| 972 | delete channel; |
| 973 | channel = NULL; |
| 974 | } |
| 975 | return channel; |
| 976 | } |
| 977 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 978 | bool WebRtcVideoEngine::SetLocalRenderer(VideoRenderer* renderer) { |
| 979 | local_renderer_w_ = local_renderer_h_ = 0; |
| 980 | local_renderer_ = renderer; |
| 981 | return true; |
| 982 | } |
| 983 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 984 | const std::vector<VideoCodec>& WebRtcVideoEngine::codecs() const { |
| 985 | return video_codecs_; |
| 986 | } |
| 987 | |
| 988 | const std::vector<RtpHeaderExtension>& |
| 989 | WebRtcVideoEngine::rtp_header_extensions() const { |
| 990 | return rtp_header_extensions_; |
| 991 | } |
| 992 | |
| 993 | void WebRtcVideoEngine::SetLogging(int min_sev, const char* filter) { |
| 994 | // if min_sev == -1, we keep the current log level. |
| 995 | if (min_sev >= 0) { |
| 996 | SetTraceFilter(SeverityToFilter(min_sev)); |
| 997 | } |
| 998 | SetTraceOptions(filter); |
| 999 | } |
| 1000 | |
| 1001 | int WebRtcVideoEngine::GetLastEngineError() { |
| 1002 | return vie_wrapper_->error(); |
| 1003 | } |
| 1004 | |
| 1005 | // Checks to see whether we comprehend and could receive a particular codec |
| 1006 | bool WebRtcVideoEngine::FindCodec(const VideoCodec& in) { |
| 1007 | for (int i = 0; i < ARRAY_SIZE(kVideoFormats); ++i) { |
| 1008 | const VideoFormat fmt(kVideoFormats[i]); |
| 1009 | if ((in.width == 0 && in.height == 0) || |
| 1010 | (fmt.width == in.width && fmt.height == in.height)) { |
| 1011 | if (encoder_factory_) { |
| 1012 | const std::vector<WebRtcVideoEncoderFactory::VideoCodec>& codecs = |
| 1013 | encoder_factory_->codecs(); |
| 1014 | for (size_t j = 0; j < codecs.size(); ++j) { |
henrike@webrtc.org | 28654cb | 2013-07-22 21:07:49 +0000 | [diff] [blame] | 1015 | VideoCodec codec(GetExternalVideoPayloadType(static_cast<int>(j)), |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1016 | codecs[j].name, 0, 0, 0, 0); |
| 1017 | if (codec.Matches(in)) |
| 1018 | return true; |
| 1019 | } |
| 1020 | } |
| 1021 | for (size_t j = 0; j < ARRAY_SIZE(kVideoCodecPrefs); ++j) { |
| 1022 | VideoCodec codec(kVideoCodecPrefs[j].payload_type, |
| 1023 | kVideoCodecPrefs[j].name, 0, 0, 0, 0); |
| 1024 | if (codec.Matches(in)) { |
| 1025 | return true; |
| 1026 | } |
| 1027 | } |
| 1028 | } |
| 1029 | } |
| 1030 | return false; |
| 1031 | } |
| 1032 | |
| 1033 | // Given the requested codec, returns true if we can send that codec type and |
| 1034 | // updates out with the best quality we could send for that codec. If current is |
| 1035 | // not empty, we constrain out so that its aspect ratio matches current's. |
| 1036 | bool WebRtcVideoEngine::CanSendCodec(const VideoCodec& requested, |
| 1037 | const VideoCodec& current, |
| 1038 | VideoCodec* out) { |
| 1039 | if (!out) { |
| 1040 | return false; |
| 1041 | } |
| 1042 | |
| 1043 | std::vector<VideoCodec>::const_iterator local_max; |
| 1044 | for (local_max = video_codecs_.begin(); |
| 1045 | local_max < video_codecs_.end(); |
| 1046 | ++local_max) { |
| 1047 | // First match codecs by payload type |
| 1048 | if (!requested.Matches(*local_max)) { |
| 1049 | continue; |
| 1050 | } |
| 1051 | |
| 1052 | out->id = requested.id; |
| 1053 | out->name = requested.name; |
| 1054 | out->preference = requested.preference; |
| 1055 | out->params = requested.params; |
| 1056 | out->framerate = talk_base::_min(requested.framerate, local_max->framerate); |
| 1057 | out->width = 0; |
| 1058 | out->height = 0; |
| 1059 | out->params = requested.params; |
| 1060 | out->feedback_params = requested.feedback_params; |
| 1061 | |
| 1062 | if (0 == requested.width && 0 == requested.height) { |
| 1063 | // Special case with resolution 0. The channel should not send frames. |
| 1064 | return true; |
| 1065 | } else if (0 == requested.width || 0 == requested.height) { |
| 1066 | // 0xn and nx0 are invalid resolutions. |
| 1067 | return false; |
| 1068 | } |
| 1069 | |
| 1070 | // Pick the best quality that is within their and our bounds and has the |
| 1071 | // correct aspect ratio. |
| 1072 | for (int j = 0; j < ARRAY_SIZE(kVideoFormats); ++j) { |
| 1073 | const VideoFormat format(kVideoFormats[j]); |
| 1074 | |
| 1075 | // Skip any format that is larger than the local or remote maximums, or |
| 1076 | // smaller than the current best match |
| 1077 | if (format.width > requested.width || format.height > requested.height || |
| 1078 | format.width > local_max->width || |
| 1079 | (format.width < out->width && format.height < out->height)) { |
| 1080 | continue; |
| 1081 | } |
| 1082 | |
| 1083 | bool better = false; |
| 1084 | |
| 1085 | // Check any further constraints on this prospective format |
| 1086 | if (!out->width || !out->height) { |
| 1087 | // If we don't have any matches yet, this is the best so far. |
| 1088 | better = true; |
| 1089 | } else if (current.width && current.height) { |
| 1090 | // current is set so format must match its ratio exactly. |
| 1091 | better = |
| 1092 | (format.width * current.height == format.height * current.width); |
| 1093 | } else { |
| 1094 | // Prefer closer aspect ratios i.e |
| 1095 | // format.aspect - requested.aspect < out.aspect - requested.aspect |
| 1096 | better = abs(format.width * requested.height * out->height - |
| 1097 | requested.width * format.height * out->height) < |
| 1098 | abs(out->width * format.height * requested.height - |
| 1099 | requested.width * format.height * out->height); |
| 1100 | } |
| 1101 | |
| 1102 | if (better) { |
| 1103 | out->width = format.width; |
| 1104 | out->height = format.height; |
| 1105 | } |
| 1106 | } |
| 1107 | if (out->width > 0) { |
| 1108 | return true; |
| 1109 | } |
| 1110 | } |
| 1111 | return false; |
| 1112 | } |
| 1113 | |
| 1114 | static void ConvertToCricketVideoCodec( |
| 1115 | const webrtc::VideoCodec& in_codec, VideoCodec* out_codec) { |
| 1116 | out_codec->id = in_codec.plType; |
| 1117 | out_codec->name = in_codec.plName; |
| 1118 | out_codec->width = in_codec.width; |
| 1119 | out_codec->height = in_codec.height; |
| 1120 | out_codec->framerate = in_codec.maxFramerate; |
| 1121 | out_codec->SetParam(kCodecParamMinBitrate, in_codec.minBitrate); |
| 1122 | out_codec->SetParam(kCodecParamMaxBitrate, in_codec.maxBitrate); |
| 1123 | if (in_codec.qpMax) { |
| 1124 | out_codec->SetParam(kCodecParamMaxQuantization, in_codec.qpMax); |
| 1125 | } |
| 1126 | } |
| 1127 | |
| 1128 | bool WebRtcVideoEngine::ConvertFromCricketVideoCodec( |
| 1129 | const VideoCodec& in_codec, webrtc::VideoCodec* out_codec) { |
| 1130 | bool found = false; |
| 1131 | int ncodecs = vie_wrapper_->codec()->NumberOfCodecs(); |
| 1132 | for (int i = 0; i < ncodecs; ++i) { |
| 1133 | if (vie_wrapper_->codec()->GetCodec(i, *out_codec) == 0 && |
| 1134 | _stricmp(in_codec.name.c_str(), out_codec->plName) == 0) { |
| 1135 | found = true; |
| 1136 | break; |
| 1137 | } |
| 1138 | } |
| 1139 | |
| 1140 | // If not found, check if this is supported by external encoder factory. |
| 1141 | if (!found && encoder_factory_) { |
| 1142 | const std::vector<WebRtcVideoEncoderFactory::VideoCodec>& codecs = |
| 1143 | encoder_factory_->codecs(); |
| 1144 | for (size_t i = 0; i < codecs.size(); ++i) { |
| 1145 | if (_stricmp(in_codec.name.c_str(), codecs[i].name.c_str()) == 0) { |
| 1146 | out_codec->codecType = codecs[i].type; |
henrike@webrtc.org | 28654cb | 2013-07-22 21:07:49 +0000 | [diff] [blame] | 1147 | out_codec->plType = GetExternalVideoPayloadType(static_cast<int>(i)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1148 | talk_base::strcpyn(out_codec->plName, sizeof(out_codec->plName), |
| 1149 | codecs[i].name.c_str(), codecs[i].name.length()); |
| 1150 | found = true; |
| 1151 | break; |
| 1152 | } |
| 1153 | } |
| 1154 | } |
| 1155 | |
| 1156 | if (!found) { |
| 1157 | LOG(LS_ERROR) << "invalid codec type"; |
| 1158 | return false; |
| 1159 | } |
| 1160 | |
| 1161 | if (in_codec.id != 0) |
| 1162 | out_codec->plType = in_codec.id; |
| 1163 | |
| 1164 | if (in_codec.width != 0) |
| 1165 | out_codec->width = in_codec.width; |
| 1166 | |
| 1167 | if (in_codec.height != 0) |
| 1168 | out_codec->height = in_codec.height; |
| 1169 | |
| 1170 | if (in_codec.framerate != 0) |
| 1171 | out_codec->maxFramerate = in_codec.framerate; |
| 1172 | |
| 1173 | // Convert bitrate parameters. |
| 1174 | int max_bitrate = kMaxVideoBitrate; |
| 1175 | int min_bitrate = kMinVideoBitrate; |
| 1176 | int start_bitrate = kStartVideoBitrate; |
| 1177 | |
| 1178 | in_codec.GetParam(kCodecParamMinBitrate, &min_bitrate); |
| 1179 | in_codec.GetParam(kCodecParamMaxBitrate, &max_bitrate); |
| 1180 | |
| 1181 | if (max_bitrate < min_bitrate) { |
| 1182 | return false; |
| 1183 | } |
| 1184 | start_bitrate = talk_base::_max(start_bitrate, min_bitrate); |
| 1185 | start_bitrate = talk_base::_min(start_bitrate, max_bitrate); |
| 1186 | |
| 1187 | out_codec->minBitrate = min_bitrate; |
| 1188 | out_codec->startBitrate = start_bitrate; |
| 1189 | out_codec->maxBitrate = max_bitrate; |
| 1190 | |
| 1191 | // Convert general codec parameters. |
| 1192 | int max_quantization = 0; |
| 1193 | if (in_codec.GetParam(kCodecParamMaxQuantization, &max_quantization)) { |
| 1194 | if (max_quantization < 0) { |
| 1195 | return false; |
| 1196 | } |
| 1197 | out_codec->qpMax = max_quantization; |
| 1198 | } |
| 1199 | return true; |
| 1200 | } |
| 1201 | |
| 1202 | void WebRtcVideoEngine::RegisterChannel(WebRtcVideoMediaChannel *channel) { |
| 1203 | talk_base::CritScope cs(&channels_crit_); |
| 1204 | channels_.push_back(channel); |
| 1205 | } |
| 1206 | |
| 1207 | void WebRtcVideoEngine::UnregisterChannel(WebRtcVideoMediaChannel *channel) { |
| 1208 | talk_base::CritScope cs(&channels_crit_); |
| 1209 | channels_.erase(std::remove(channels_.begin(), channels_.end(), channel), |
| 1210 | channels_.end()); |
| 1211 | } |
| 1212 | |
| 1213 | bool WebRtcVideoEngine::SetVoiceEngine(WebRtcVoiceEngine* voice_engine) { |
| 1214 | if (initialized_) { |
| 1215 | LOG(LS_WARNING) << "SetVoiceEngine can not be called after Init"; |
| 1216 | return false; |
| 1217 | } |
| 1218 | voice_engine_ = voice_engine; |
| 1219 | return true; |
| 1220 | } |
| 1221 | |
| 1222 | bool WebRtcVideoEngine::EnableTimedRender() { |
| 1223 | if (initialized_) { |
| 1224 | LOG(LS_WARNING) << "EnableTimedRender can not be called after Init"; |
| 1225 | return false; |
| 1226 | } |
| 1227 | render_module_.reset(webrtc::VideoRender::CreateVideoRender(0, NULL, |
| 1228 | false, webrtc::kRenderExternal)); |
| 1229 | return true; |
| 1230 | } |
| 1231 | |
| 1232 | void WebRtcVideoEngine::SetTraceFilter(int filter) { |
| 1233 | tracing_->SetTraceFilter(filter); |
| 1234 | } |
| 1235 | |
| 1236 | // See https://sites.google.com/a/google.com/wavelet/ |
| 1237 | // Home/Magic-Flute--RTC-Engine-/Magic-Flute-Command-Line-Parameters |
| 1238 | // for all supported command line setttings. |
| 1239 | void WebRtcVideoEngine::SetTraceOptions(const std::string& options) { |
| 1240 | // Set WebRTC trace file. |
| 1241 | std::vector<std::string> opts; |
| 1242 | talk_base::tokenize(options, ' ', '"', '"', &opts); |
| 1243 | std::vector<std::string>::iterator tracefile = |
| 1244 | std::find(opts.begin(), opts.end(), "tracefile"); |
| 1245 | if (tracefile != opts.end() && ++tracefile != opts.end()) { |
| 1246 | // Write WebRTC debug output (at same loglevel) to file |
| 1247 | if (tracing_->SetTraceFile(tracefile->c_str()) == -1) { |
| 1248 | LOG_RTCERR1(SetTraceFile, *tracefile); |
| 1249 | } |
| 1250 | } |
| 1251 | } |
| 1252 | |
| 1253 | static void AddDefaultFeedbackParams(VideoCodec* codec) { |
| 1254 | const FeedbackParam kFir(kRtcpFbParamCcm, kRtcpFbCcmParamFir); |
| 1255 | codec->AddFeedbackParam(kFir); |
| 1256 | const FeedbackParam kNack(kRtcpFbParamNack, kParamValueEmpty); |
| 1257 | codec->AddFeedbackParam(kNack); |
| 1258 | const FeedbackParam kRemb(kRtcpFbParamRemb, kParamValueEmpty); |
| 1259 | codec->AddFeedbackParam(kRemb); |
| 1260 | } |
| 1261 | |
| 1262 | // Rebuilds the codec list to be only those that are less intensive |
mallinath@webrtc.org | a27be8e | 2013-09-27 23:04:10 +0000 | [diff] [blame] | 1263 | // than the specified codec. Prefers internal codec over external with |
| 1264 | // higher preference field. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1265 | bool WebRtcVideoEngine::RebuildCodecList(const VideoCodec& in_codec) { |
| 1266 | if (!FindCodec(in_codec)) |
| 1267 | return false; |
| 1268 | |
| 1269 | video_codecs_.clear(); |
| 1270 | |
| 1271 | bool found = false; |
mallinath@webrtc.org | 1112c30 | 2013-09-23 20:34:45 +0000 | [diff] [blame] | 1272 | std::set<std::string> internal_codec_names; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1273 | for (size_t i = 0; i < ARRAY_SIZE(kVideoCodecPrefs); ++i) { |
| 1274 | const VideoCodecPref& pref(kVideoCodecPrefs[i]); |
| 1275 | if (!found) |
| 1276 | found = (in_codec.name == pref.name); |
mallinath@webrtc.org | 1112c30 | 2013-09-23 20:34:45 +0000 | [diff] [blame] | 1277 | if (found) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1278 | VideoCodec codec(pref.payload_type, pref.name, |
| 1279 | in_codec.width, in_codec.height, in_codec.framerate, |
henrike@webrtc.org | 28654cb | 2013-07-22 21:07:49 +0000 | [diff] [blame] | 1280 | static_cast<int>(ARRAY_SIZE(kVideoCodecPrefs) - i)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1281 | if (_stricmp(kVp8PayloadName, codec.name.c_str()) == 0) { |
| 1282 | AddDefaultFeedbackParams(&codec); |
| 1283 | } |
| 1284 | video_codecs_.push_back(codec); |
mallinath@webrtc.org | 1112c30 | 2013-09-23 20:34:45 +0000 | [diff] [blame] | 1285 | internal_codec_names.insert(codec.name); |
| 1286 | } |
| 1287 | } |
| 1288 | if (encoder_factory_) { |
| 1289 | const std::vector<WebRtcVideoEncoderFactory::VideoCodec>& codecs = |
| 1290 | encoder_factory_->codecs(); |
| 1291 | for (size_t i = 0; i < codecs.size(); ++i) { |
| 1292 | bool is_internal_codec = internal_codec_names.find(codecs[i].name) != |
| 1293 | internal_codec_names.end(); |
| 1294 | if (!is_internal_codec) { |
| 1295 | if (!found) |
| 1296 | found = (in_codec.name == codecs[i].name); |
| 1297 | VideoCodec codec( |
| 1298 | GetExternalVideoPayloadType(static_cast<int>(i)), |
| 1299 | codecs[i].name, |
| 1300 | codecs[i].max_width, |
| 1301 | codecs[i].max_height, |
| 1302 | codecs[i].max_fps, |
mallinath@webrtc.org | a27be8e | 2013-09-27 23:04:10 +0000 | [diff] [blame] | 1303 | // Use negative preference on external codec to ensure the internal |
| 1304 | // codec is preferred. |
| 1305 | static_cast<int>(0 - i)); |
mallinath@webrtc.org | 1112c30 | 2013-09-23 20:34:45 +0000 | [diff] [blame] | 1306 | AddDefaultFeedbackParams(&codec); |
| 1307 | video_codecs_.push_back(codec); |
| 1308 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1309 | } |
| 1310 | } |
| 1311 | ASSERT(found); |
| 1312 | return true; |
| 1313 | } |
| 1314 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1315 | // Ignore spammy trace messages, mostly from the stats API when we haven't |
| 1316 | // gotten RTCP info yet from the remote side. |
| 1317 | bool WebRtcVideoEngine::ShouldIgnoreTrace(const std::string& trace) { |
| 1318 | static const char* const kTracesToIgnore[] = { |
| 1319 | NULL |
| 1320 | }; |
| 1321 | for (const char* const* p = kTracesToIgnore; *p; ++p) { |
| 1322 | if (trace.find(*p) == 0) { |
| 1323 | return true; |
| 1324 | } |
| 1325 | } |
| 1326 | return false; |
| 1327 | } |
| 1328 | |
| 1329 | int WebRtcVideoEngine::GetNumOfChannels() { |
| 1330 | talk_base::CritScope cs(&channels_crit_); |
henrike@webrtc.org | 28654cb | 2013-07-22 21:07:49 +0000 | [diff] [blame] | 1331 | return static_cast<int>(channels_.size()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1332 | } |
| 1333 | |
| 1334 | void WebRtcVideoEngine::Print(webrtc::TraceLevel level, const char* trace, |
| 1335 | int length) { |
| 1336 | talk_base::LoggingSeverity sev = talk_base::LS_VERBOSE; |
| 1337 | if (level == webrtc::kTraceError || level == webrtc::kTraceCritical) |
| 1338 | sev = talk_base::LS_ERROR; |
| 1339 | else if (level == webrtc::kTraceWarning) |
| 1340 | sev = talk_base::LS_WARNING; |
| 1341 | else if (level == webrtc::kTraceStateInfo || level == webrtc::kTraceInfo) |
| 1342 | sev = talk_base::LS_INFO; |
| 1343 | else if (level == webrtc::kTraceTerseInfo) |
| 1344 | sev = talk_base::LS_INFO; |
| 1345 | |
| 1346 | // Skip past boilerplate prefix text |
| 1347 | if (length < 72) { |
| 1348 | std::string msg(trace, length); |
| 1349 | LOG(LS_ERROR) << "Malformed webrtc log message: "; |
| 1350 | LOG_V(sev) << msg; |
| 1351 | } else { |
| 1352 | std::string msg(trace + 71, length - 72); |
| 1353 | if (!ShouldIgnoreTrace(msg) && |
| 1354 | (!voice_engine_ || !voice_engine_->ShouldIgnoreTrace(msg))) { |
| 1355 | LOG_V(sev) << "webrtc: " << msg; |
| 1356 | } |
| 1357 | } |
| 1358 | } |
| 1359 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1360 | webrtc::VideoDecoder* WebRtcVideoEngine::CreateExternalDecoder( |
| 1361 | webrtc::VideoCodecType type) { |
| 1362 | if (decoder_factory_ == NULL) { |
| 1363 | return NULL; |
| 1364 | } |
| 1365 | return decoder_factory_->CreateVideoDecoder(type); |
| 1366 | } |
| 1367 | |
| 1368 | void WebRtcVideoEngine::DestroyExternalDecoder(webrtc::VideoDecoder* decoder) { |
| 1369 | ASSERT(decoder_factory_ != NULL); |
| 1370 | if (decoder_factory_ == NULL) |
| 1371 | return; |
| 1372 | decoder_factory_->DestroyVideoDecoder(decoder); |
| 1373 | } |
| 1374 | |
| 1375 | webrtc::VideoEncoder* WebRtcVideoEngine::CreateExternalEncoder( |
| 1376 | webrtc::VideoCodecType type) { |
| 1377 | if (encoder_factory_ == NULL) { |
| 1378 | return NULL; |
| 1379 | } |
| 1380 | return encoder_factory_->CreateVideoEncoder(type); |
| 1381 | } |
| 1382 | |
| 1383 | void WebRtcVideoEngine::DestroyExternalEncoder(webrtc::VideoEncoder* encoder) { |
| 1384 | ASSERT(encoder_factory_ != NULL); |
| 1385 | if (encoder_factory_ == NULL) |
| 1386 | return; |
| 1387 | encoder_factory_->DestroyVideoEncoder(encoder); |
| 1388 | } |
| 1389 | |
| 1390 | bool WebRtcVideoEngine::IsExternalEncoderCodecType( |
| 1391 | webrtc::VideoCodecType type) const { |
| 1392 | if (!encoder_factory_) |
| 1393 | return false; |
| 1394 | const std::vector<WebRtcVideoEncoderFactory::VideoCodec>& codecs = |
| 1395 | encoder_factory_->codecs(); |
| 1396 | std::vector<WebRtcVideoEncoderFactory::VideoCodec>::const_iterator it; |
| 1397 | for (it = codecs.begin(); it != codecs.end(); ++it) { |
| 1398 | if (it->type == type) |
| 1399 | return true; |
| 1400 | } |
| 1401 | return false; |
| 1402 | } |
| 1403 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1404 | void WebRtcVideoEngine::SetExternalDecoderFactory( |
| 1405 | WebRtcVideoDecoderFactory* decoder_factory) { |
| 1406 | decoder_factory_ = decoder_factory; |
| 1407 | } |
| 1408 | |
| 1409 | void WebRtcVideoEngine::SetExternalEncoderFactory( |
| 1410 | WebRtcVideoEncoderFactory* encoder_factory) { |
| 1411 | if (encoder_factory_ == encoder_factory) |
| 1412 | return; |
| 1413 | |
| 1414 | if (encoder_factory_) { |
| 1415 | encoder_factory_->RemoveObserver(this); |
| 1416 | } |
| 1417 | encoder_factory_ = encoder_factory; |
| 1418 | if (encoder_factory_) { |
| 1419 | encoder_factory_->AddObserver(this); |
| 1420 | } |
| 1421 | |
| 1422 | // Invoke OnCodecAvailable() here in case the list of codecs is already |
| 1423 | // available when the encoder factory is installed. If not the encoder |
| 1424 | // factory will invoke the callback later when the codecs become available. |
| 1425 | OnCodecsAvailable(); |
| 1426 | } |
| 1427 | |
| 1428 | void WebRtcVideoEngine::OnCodecsAvailable() { |
| 1429 | // Rebuild codec list while reapplying the current default codec format. |
| 1430 | VideoCodec max_codec(kVideoCodecPrefs[0].payload_type, |
| 1431 | kVideoCodecPrefs[0].name, |
| 1432 | video_codecs_[0].width, |
| 1433 | video_codecs_[0].height, |
| 1434 | video_codecs_[0].framerate, |
| 1435 | 0); |
| 1436 | if (!RebuildCodecList(max_codec)) { |
| 1437 | LOG(LS_ERROR) << "Failed to initialize list of supported codec types"; |
| 1438 | } |
| 1439 | } |
| 1440 | |
| 1441 | // WebRtcVideoMediaChannel |
| 1442 | |
| 1443 | WebRtcVideoMediaChannel::WebRtcVideoMediaChannel( |
| 1444 | WebRtcVideoEngine* engine, |
| 1445 | VoiceMediaChannel* channel) |
| 1446 | : engine_(engine), |
| 1447 | voice_channel_(channel), |
| 1448 | vie_channel_(-1), |
| 1449 | nack_enabled_(true), |
| 1450 | remb_enabled_(false), |
| 1451 | render_started_(false), |
| 1452 | first_receive_ssrc_(0), |
| 1453 | send_red_type_(-1), |
| 1454 | send_fec_type_(-1), |
| 1455 | send_min_bitrate_(kMinVideoBitrate), |
| 1456 | send_start_bitrate_(kStartVideoBitrate), |
| 1457 | send_max_bitrate_(kMaxVideoBitrate), |
| 1458 | sending_(false), |
| 1459 | ratio_w_(0), |
| 1460 | ratio_h_(0) { |
| 1461 | engine->RegisterChannel(this); |
| 1462 | } |
| 1463 | |
| 1464 | bool WebRtcVideoMediaChannel::Init() { |
| 1465 | const uint32 ssrc_key = 0; |
| 1466 | return CreateChannel(ssrc_key, MD_SENDRECV, &vie_channel_); |
| 1467 | } |
| 1468 | |
| 1469 | WebRtcVideoMediaChannel::~WebRtcVideoMediaChannel() { |
| 1470 | const bool send = false; |
| 1471 | SetSend(send); |
| 1472 | const bool render = false; |
| 1473 | SetRender(render); |
| 1474 | |
| 1475 | while (!send_channels_.empty()) { |
| 1476 | if (!DeleteSendChannel(send_channels_.begin()->first)) { |
| 1477 | LOG(LS_ERROR) << "Unable to delete channel with ssrc key " |
| 1478 | << send_channels_.begin()->first; |
| 1479 | ASSERT(false); |
| 1480 | break; |
| 1481 | } |
| 1482 | } |
| 1483 | |
| 1484 | // Remove all receive streams and the default channel. |
| 1485 | while (!recv_channels_.empty()) { |
| 1486 | RemoveRecvStream(recv_channels_.begin()->first); |
| 1487 | } |
| 1488 | |
| 1489 | // Unregister the channel from the engine. |
| 1490 | engine()->UnregisterChannel(this); |
| 1491 | if (worker_thread()) { |
| 1492 | worker_thread()->Clear(this); |
| 1493 | } |
| 1494 | } |
| 1495 | |
| 1496 | bool WebRtcVideoMediaChannel::SetRecvCodecs( |
| 1497 | const std::vector<VideoCodec>& codecs) { |
| 1498 | receive_codecs_.clear(); |
| 1499 | for (std::vector<VideoCodec>::const_iterator iter = codecs.begin(); |
| 1500 | iter != codecs.end(); ++iter) { |
| 1501 | if (engine()->FindCodec(*iter)) { |
| 1502 | webrtc::VideoCodec wcodec; |
| 1503 | if (engine()->ConvertFromCricketVideoCodec(*iter, &wcodec)) { |
| 1504 | receive_codecs_.push_back(wcodec); |
| 1505 | } |
| 1506 | } else { |
| 1507 | LOG(LS_INFO) << "Unknown codec " << iter->name; |
| 1508 | return false; |
| 1509 | } |
| 1510 | } |
| 1511 | |
| 1512 | for (RecvChannelMap::iterator it = recv_channels_.begin(); |
| 1513 | it != recv_channels_.end(); ++it) { |
| 1514 | if (!SetReceiveCodecs(it->second)) |
| 1515 | return false; |
| 1516 | } |
| 1517 | return true; |
| 1518 | } |
| 1519 | |
| 1520 | bool WebRtcVideoMediaChannel::SetSendCodecs( |
| 1521 | const std::vector<VideoCodec>& codecs) { |
| 1522 | // Match with local video codec list. |
| 1523 | std::vector<webrtc::VideoCodec> send_codecs; |
| 1524 | VideoCodec checked_codec; |
| 1525 | VideoCodec current; // defaults to 0x0 |
| 1526 | if (sending_) { |
| 1527 | ConvertToCricketVideoCodec(*send_codec_, ¤t); |
| 1528 | } |
| 1529 | for (std::vector<VideoCodec>::const_iterator iter = codecs.begin(); |
| 1530 | iter != codecs.end(); ++iter) { |
| 1531 | if (_stricmp(iter->name.c_str(), kRedPayloadName) == 0) { |
| 1532 | send_red_type_ = iter->id; |
| 1533 | } else if (_stricmp(iter->name.c_str(), kFecPayloadName) == 0) { |
| 1534 | send_fec_type_ = iter->id; |
| 1535 | } else if (engine()->CanSendCodec(*iter, current, &checked_codec)) { |
| 1536 | webrtc::VideoCodec wcodec; |
| 1537 | if (engine()->ConvertFromCricketVideoCodec(checked_codec, &wcodec)) { |
| 1538 | if (send_codecs.empty()) { |
| 1539 | nack_enabled_ = IsNackEnabled(checked_codec); |
| 1540 | remb_enabled_ = IsRembEnabled(checked_codec); |
| 1541 | } |
| 1542 | send_codecs.push_back(wcodec); |
| 1543 | } |
| 1544 | } else { |
| 1545 | LOG(LS_WARNING) << "Unknown codec " << iter->name; |
| 1546 | } |
| 1547 | } |
| 1548 | |
| 1549 | // Fail if we don't have a match. |
| 1550 | if (send_codecs.empty()) { |
| 1551 | LOG(LS_WARNING) << "No matching codecs available"; |
| 1552 | return false; |
| 1553 | } |
| 1554 | |
| 1555 | // Recv protection. |
| 1556 | for (RecvChannelMap::iterator it = recv_channels_.begin(); |
| 1557 | it != recv_channels_.end(); ++it) { |
| 1558 | int channel_id = it->second->channel_id(); |
| 1559 | if (!SetNackFec(channel_id, send_red_type_, send_fec_type_, |
| 1560 | nack_enabled_)) { |
| 1561 | return false; |
| 1562 | } |
| 1563 | if (engine_->vie()->rtp()->SetRembStatus(channel_id, |
| 1564 | kNotSending, |
| 1565 | remb_enabled_) != 0) { |
| 1566 | LOG_RTCERR3(SetRembStatus, channel_id, kNotSending, remb_enabled_); |
| 1567 | return false; |
| 1568 | } |
| 1569 | } |
| 1570 | |
| 1571 | // Send settings. |
| 1572 | for (SendChannelMap::iterator iter = send_channels_.begin(); |
| 1573 | iter != send_channels_.end(); ++iter) { |
| 1574 | int channel_id = iter->second->channel_id(); |
| 1575 | if (!SetNackFec(channel_id, send_red_type_, send_fec_type_, |
| 1576 | nack_enabled_)) { |
| 1577 | return false; |
| 1578 | } |
| 1579 | if (engine_->vie()->rtp()->SetRembStatus(channel_id, |
| 1580 | remb_enabled_, |
| 1581 | remb_enabled_) != 0) { |
| 1582 | LOG_RTCERR3(SetRembStatus, channel_id, remb_enabled_, remb_enabled_); |
| 1583 | return false; |
| 1584 | } |
| 1585 | } |
| 1586 | |
| 1587 | // Select the first matched codec. |
| 1588 | webrtc::VideoCodec& codec(send_codecs[0]); |
| 1589 | |
| 1590 | if (!SetSendCodec( |
| 1591 | codec, codec.minBitrate, codec.startBitrate, codec.maxBitrate)) { |
| 1592 | return false; |
| 1593 | } |
| 1594 | |
| 1595 | for (SendChannelMap::iterator iter = send_channels_.begin(); |
| 1596 | iter != send_channels_.end(); ++iter) { |
| 1597 | WebRtcVideoChannelSendInfo* send_channel = iter->second; |
| 1598 | send_channel->InitializeAdapterOutputFormat(codec); |
| 1599 | } |
| 1600 | |
| 1601 | LogSendCodecChange("SetSendCodecs()"); |
| 1602 | |
| 1603 | return true; |
| 1604 | } |
| 1605 | |
| 1606 | bool WebRtcVideoMediaChannel::GetSendCodec(VideoCodec* send_codec) { |
| 1607 | if (!send_codec_) { |
| 1608 | return false; |
| 1609 | } |
| 1610 | ConvertToCricketVideoCodec(*send_codec_, send_codec); |
| 1611 | return true; |
| 1612 | } |
| 1613 | |
| 1614 | bool WebRtcVideoMediaChannel::SetSendStreamFormat(uint32 ssrc, |
| 1615 | const VideoFormat& format) { |
| 1616 | if (!send_codec_) { |
| 1617 | LOG(LS_ERROR) << "The send codec has not been set yet."; |
| 1618 | return false; |
| 1619 | } |
| 1620 | WebRtcVideoChannelSendInfo* send_channel = GetSendChannel(ssrc); |
| 1621 | if (!send_channel) { |
| 1622 | LOG(LS_ERROR) << "The specified ssrc " << ssrc << " is not in use."; |
| 1623 | return false; |
| 1624 | } |
| 1625 | send_channel->set_video_format(format); |
| 1626 | return true; |
| 1627 | } |
| 1628 | |
| 1629 | bool WebRtcVideoMediaChannel::SetRender(bool render) { |
| 1630 | if (render == render_started_) { |
| 1631 | return true; // no action required |
| 1632 | } |
| 1633 | |
| 1634 | bool ret = true; |
| 1635 | for (RecvChannelMap::iterator it = recv_channels_.begin(); |
| 1636 | it != recv_channels_.end(); ++it) { |
| 1637 | if (render) { |
| 1638 | if (engine()->vie()->render()->StartRender( |
| 1639 | it->second->channel_id()) != 0) { |
| 1640 | LOG_RTCERR1(StartRender, it->second->channel_id()); |
| 1641 | ret = false; |
| 1642 | } |
| 1643 | } else { |
| 1644 | if (engine()->vie()->render()->StopRender( |
| 1645 | it->second->channel_id()) != 0) { |
| 1646 | LOG_RTCERR1(StopRender, it->second->channel_id()); |
| 1647 | ret = false; |
| 1648 | } |
| 1649 | } |
| 1650 | } |
| 1651 | if (ret) { |
| 1652 | render_started_ = render; |
| 1653 | } |
| 1654 | |
| 1655 | return ret; |
| 1656 | } |
| 1657 | |
| 1658 | bool WebRtcVideoMediaChannel::SetSend(bool send) { |
| 1659 | if (!HasReadySendChannels() && send) { |
| 1660 | LOG(LS_ERROR) << "No stream added"; |
| 1661 | return false; |
| 1662 | } |
| 1663 | if (send == sending()) { |
| 1664 | return true; // No action required. |
| 1665 | } |
| 1666 | |
| 1667 | if (send) { |
| 1668 | // We've been asked to start sending. |
| 1669 | // SetSendCodecs must have been called already. |
| 1670 | if (!send_codec_) { |
| 1671 | return false; |
| 1672 | } |
| 1673 | // Start send now. |
| 1674 | if (!StartSend()) { |
| 1675 | return false; |
| 1676 | } |
| 1677 | } else { |
| 1678 | // We've been asked to stop sending. |
| 1679 | if (!StopSend()) { |
| 1680 | return false; |
| 1681 | } |
| 1682 | } |
| 1683 | sending_ = send; |
| 1684 | |
| 1685 | return true; |
| 1686 | } |
| 1687 | |
| 1688 | bool WebRtcVideoMediaChannel::AddSendStream(const StreamParams& sp) { |
| 1689 | LOG(LS_INFO) << "AddSendStream " << sp.ToString(); |
| 1690 | |
| 1691 | if (!IsOneSsrcStream(sp)) { |
| 1692 | LOG(LS_ERROR) << "AddSendStream: bad local stream parameters"; |
| 1693 | return false; |
| 1694 | } |
| 1695 | |
| 1696 | uint32 ssrc_key; |
| 1697 | if (!CreateSendChannelKey(sp.first_ssrc(), &ssrc_key)) { |
| 1698 | LOG(LS_ERROR) << "Trying to register duplicate ssrc: " << sp.first_ssrc(); |
| 1699 | return false; |
| 1700 | } |
| 1701 | // If the default channel is already used for sending create a new channel |
| 1702 | // otherwise use the default channel for sending. |
| 1703 | int channel_id = -1; |
| 1704 | if (send_channels_[0]->stream_params() == NULL) { |
| 1705 | channel_id = vie_channel_; |
| 1706 | } else { |
| 1707 | if (!CreateChannel(ssrc_key, MD_SEND, &channel_id)) { |
| 1708 | LOG(LS_ERROR) << "AddSendStream: unable to create channel"; |
| 1709 | return false; |
| 1710 | } |
| 1711 | } |
| 1712 | WebRtcVideoChannelSendInfo* send_channel = send_channels_[ssrc_key]; |
| 1713 | // Set the send (local) SSRC. |
| 1714 | // If there are multiple send SSRCs, we can only set the first one here, and |
| 1715 | // the rest of the SSRC(s) need to be set after SetSendCodec has been called |
| 1716 | // (with a codec requires multiple SSRC(s)). |
| 1717 | if (engine()->vie()->rtp()->SetLocalSSRC(channel_id, |
| 1718 | sp.first_ssrc()) != 0) { |
| 1719 | LOG_RTCERR2(SetLocalSSRC, channel_id, sp.first_ssrc()); |
| 1720 | return false; |
| 1721 | } |
| 1722 | |
| 1723 | // Set RTCP CName. |
| 1724 | if (engine()->vie()->rtp()->SetRTCPCName(channel_id, |
| 1725 | sp.cname.c_str()) != 0) { |
| 1726 | LOG_RTCERR2(SetRTCPCName, channel_id, sp.cname.c_str()); |
| 1727 | return false; |
| 1728 | } |
| 1729 | |
| 1730 | // At this point the channel's local SSRC has been updated. If the channel is |
| 1731 | // the default channel make sure that all the receive channels are updated as |
| 1732 | // well. Receive channels have to have the same SSRC as the default channel in |
| 1733 | // order to send receiver reports with this SSRC. |
| 1734 | if (IsDefaultChannel(channel_id)) { |
| 1735 | for (RecvChannelMap::const_iterator it = recv_channels_.begin(); |
| 1736 | it != recv_channels_.end(); ++it) { |
| 1737 | WebRtcVideoChannelRecvInfo* info = it->second; |
| 1738 | int channel_id = info->channel_id(); |
| 1739 | if (engine()->vie()->rtp()->SetLocalSSRC(channel_id, |
| 1740 | sp.first_ssrc()) != 0) { |
| 1741 | LOG_RTCERR1(SetLocalSSRC, it->first); |
| 1742 | return false; |
| 1743 | } |
| 1744 | } |
| 1745 | } |
| 1746 | |
| 1747 | send_channel->set_stream_params(sp); |
| 1748 | |
| 1749 | // Reset send codec after stream parameters changed. |
| 1750 | if (send_codec_) { |
| 1751 | if (!SetSendCodec(send_channel, *send_codec_, send_min_bitrate_, |
| 1752 | send_start_bitrate_, send_max_bitrate_)) { |
| 1753 | return false; |
| 1754 | } |
| 1755 | LogSendCodecChange("SetSendStreamFormat()"); |
| 1756 | } |
| 1757 | |
| 1758 | if (sending_) { |
| 1759 | return StartSend(send_channel); |
| 1760 | } |
| 1761 | return true; |
| 1762 | } |
| 1763 | |
| 1764 | bool WebRtcVideoMediaChannel::RemoveSendStream(uint32 ssrc) { |
| 1765 | uint32 ssrc_key; |
| 1766 | if (!GetSendChannelKey(ssrc, &ssrc_key)) { |
| 1767 | LOG(LS_WARNING) << "Try to remove stream with ssrc " << ssrc |
| 1768 | << " which doesn't exist."; |
| 1769 | return false; |
| 1770 | } |
| 1771 | WebRtcVideoChannelSendInfo* send_channel = send_channels_[ssrc_key]; |
| 1772 | int channel_id = send_channel->channel_id(); |
| 1773 | if (IsDefaultChannel(channel_id) && (send_channel->stream_params() == NULL)) { |
| 1774 | // Default channel will still exist. However, if stream_params() is NULL |
| 1775 | // there is no stream to remove. |
| 1776 | return false; |
| 1777 | } |
| 1778 | if (sending_) { |
| 1779 | StopSend(send_channel); |
| 1780 | } |
| 1781 | |
| 1782 | const WebRtcVideoChannelSendInfo::EncoderMap& encoder_map = |
| 1783 | send_channel->registered_encoders(); |
| 1784 | for (WebRtcVideoChannelSendInfo::EncoderMap::const_iterator it = |
| 1785 | encoder_map.begin(); it != encoder_map.end(); ++it) { |
| 1786 | if (engine()->vie()->ext_codec()->DeRegisterExternalSendCodec( |
| 1787 | channel_id, it->first) != 0) { |
| 1788 | LOG_RTCERR1(DeregisterEncoderObserver, channel_id); |
| 1789 | } |
| 1790 | engine()->DestroyExternalEncoder(it->second); |
| 1791 | } |
| 1792 | send_channel->ClearRegisteredEncoders(); |
| 1793 | |
| 1794 | // The receive channels depend on the default channel, recycle it instead. |
| 1795 | if (IsDefaultChannel(channel_id)) { |
| 1796 | SetCapturer(GetDefaultChannelSsrc(), NULL); |
| 1797 | send_channel->ClearStreamParams(); |
| 1798 | } else { |
| 1799 | return DeleteSendChannel(ssrc_key); |
| 1800 | } |
| 1801 | return true; |
| 1802 | } |
| 1803 | |
| 1804 | bool WebRtcVideoMediaChannel::AddRecvStream(const StreamParams& sp) { |
| 1805 | // TODO(zhurunz) Remove this once BWE works properly across different send |
| 1806 | // and receive channels. |
| 1807 | // Reuse default channel for recv stream in 1:1 call. |
| 1808 | if (!InConferenceMode() && first_receive_ssrc_ == 0) { |
| 1809 | LOG(LS_INFO) << "Recv stream " << sp.first_ssrc() |
| 1810 | << " reuse default channel #" |
| 1811 | << vie_channel_; |
| 1812 | first_receive_ssrc_ = sp.first_ssrc(); |
| 1813 | if (render_started_) { |
| 1814 | if (engine()->vie()->render()->StartRender(vie_channel_) !=0) { |
| 1815 | LOG_RTCERR1(StartRender, vie_channel_); |
| 1816 | } |
| 1817 | } |
| 1818 | return true; |
| 1819 | } |
| 1820 | |
| 1821 | if (recv_channels_.find(sp.first_ssrc()) != recv_channels_.end() || |
| 1822 | first_receive_ssrc_ == sp.first_ssrc()) { |
| 1823 | LOG(LS_ERROR) << "Stream already exists"; |
| 1824 | return false; |
| 1825 | } |
| 1826 | |
| 1827 | // TODO(perkj): Implement recv media from multiple SSRCs per stream. |
| 1828 | if (sp.ssrcs.size() != 1) { |
| 1829 | LOG(LS_ERROR) << "WebRtcVideoMediaChannel supports one receiving SSRC per" |
| 1830 | << " stream"; |
| 1831 | return false; |
| 1832 | } |
| 1833 | |
| 1834 | // Create a new channel for receiving video data. |
| 1835 | // In order to get the bandwidth estimation work fine for |
| 1836 | // receive only channels, we connect all receiving channels |
| 1837 | // to our master send channel. |
| 1838 | int channel_id = -1; |
| 1839 | if (!CreateChannel(sp.first_ssrc(), MD_RECV, &channel_id)) { |
| 1840 | return false; |
| 1841 | } |
| 1842 | |
| 1843 | // Get the default renderer. |
| 1844 | VideoRenderer* default_renderer = NULL; |
| 1845 | if (InConferenceMode()) { |
| 1846 | // The recv_channels_ size start out being 1, so if it is two here this |
| 1847 | // is the first receive channel created (vie_channel_ is not used for |
| 1848 | // receiving in a conference call). This means that the renderer stored |
| 1849 | // inside vie_channel_ should be used for the just created channel. |
| 1850 | if (recv_channels_.size() == 2 && |
| 1851 | recv_channels_.find(0) != recv_channels_.end()) { |
| 1852 | GetRenderer(0, &default_renderer); |
| 1853 | } |
| 1854 | } |
| 1855 | |
| 1856 | // The first recv stream reuses the default renderer (if a default renderer |
| 1857 | // has been set). |
| 1858 | if (default_renderer) { |
| 1859 | SetRenderer(sp.first_ssrc(), default_renderer); |
| 1860 | } |
| 1861 | |
| 1862 | LOG(LS_INFO) << "New video stream " << sp.first_ssrc() |
| 1863 | << " registered to VideoEngine channel #" |
| 1864 | << channel_id << " and connected to channel #" << vie_channel_; |
| 1865 | |
| 1866 | return true; |
| 1867 | } |
| 1868 | |
| 1869 | bool WebRtcVideoMediaChannel::RemoveRecvStream(uint32 ssrc) { |
| 1870 | RecvChannelMap::iterator it = recv_channels_.find(ssrc); |
| 1871 | |
| 1872 | if (it == recv_channels_.end()) { |
| 1873 | // TODO(perkj): Remove this once BWE works properly across different send |
| 1874 | // and receive channels. |
| 1875 | // The default channel is reused for recv stream in 1:1 call. |
| 1876 | if (first_receive_ssrc_ == ssrc) { |
| 1877 | first_receive_ssrc_ = 0; |
| 1878 | // Need to stop the renderer and remove it since the render window can be |
| 1879 | // deleted after this. |
| 1880 | if (render_started_) { |
| 1881 | if (engine()->vie()->render()->StopRender(vie_channel_) !=0) { |
| 1882 | LOG_RTCERR1(StopRender, it->second->channel_id()); |
| 1883 | } |
| 1884 | } |
| 1885 | recv_channels_[0]->SetRenderer(NULL); |
| 1886 | return true; |
| 1887 | } |
| 1888 | return false; |
| 1889 | } |
| 1890 | WebRtcVideoChannelRecvInfo* info = it->second; |
| 1891 | int channel_id = info->channel_id(); |
| 1892 | if (engine()->vie()->render()->RemoveRenderer(channel_id) != 0) { |
| 1893 | LOG_RTCERR1(RemoveRenderer, channel_id); |
| 1894 | } |
| 1895 | |
| 1896 | if (engine()->vie()->network()->DeregisterSendTransport(channel_id) !=0) { |
| 1897 | LOG_RTCERR1(DeRegisterSendTransport, channel_id); |
| 1898 | } |
| 1899 | |
| 1900 | if (engine()->vie()->codec()->DeregisterDecoderObserver( |
| 1901 | channel_id) != 0) { |
| 1902 | LOG_RTCERR1(DeregisterDecoderObserver, channel_id); |
| 1903 | } |
| 1904 | |
| 1905 | const WebRtcVideoChannelRecvInfo::DecoderMap& decoder_map = |
| 1906 | info->registered_decoders(); |
| 1907 | for (WebRtcVideoChannelRecvInfo::DecoderMap::const_iterator it = |
| 1908 | decoder_map.begin(); it != decoder_map.end(); ++it) { |
| 1909 | if (engine()->vie()->ext_codec()->DeRegisterExternalReceiveCodec( |
| 1910 | channel_id, it->first) != 0) { |
| 1911 | LOG_RTCERR1(DeregisterDecoderObserver, channel_id); |
| 1912 | } |
| 1913 | engine()->DestroyExternalDecoder(it->second); |
| 1914 | } |
| 1915 | info->ClearRegisteredDecoders(); |
| 1916 | |
| 1917 | LOG(LS_INFO) << "Removing video stream " << ssrc |
| 1918 | << " with VideoEngine channel #" |
| 1919 | << channel_id; |
| 1920 | if (engine()->vie()->base()->DeleteChannel(channel_id) == -1) { |
| 1921 | LOG_RTCERR1(DeleteChannel, channel_id); |
| 1922 | // Leak the WebRtcVideoChannelRecvInfo owned by |it| but remove the channel |
| 1923 | // from recv_channels_. |
| 1924 | recv_channels_.erase(it); |
| 1925 | return false; |
| 1926 | } |
| 1927 | // Delete the WebRtcVideoChannelRecvInfo pointed to by it->second. |
| 1928 | delete info; |
| 1929 | recv_channels_.erase(it); |
| 1930 | return true; |
| 1931 | } |
| 1932 | |
| 1933 | bool WebRtcVideoMediaChannel::StartSend() { |
| 1934 | bool success = true; |
| 1935 | for (SendChannelMap::iterator iter = send_channels_.begin(); |
| 1936 | iter != send_channels_.end(); ++iter) { |
| 1937 | WebRtcVideoChannelSendInfo* send_channel = iter->second; |
| 1938 | if (!StartSend(send_channel)) { |
| 1939 | success = false; |
| 1940 | } |
| 1941 | } |
| 1942 | return success; |
| 1943 | } |
| 1944 | |
| 1945 | bool WebRtcVideoMediaChannel::StartSend( |
| 1946 | WebRtcVideoChannelSendInfo* send_channel) { |
| 1947 | const int channel_id = send_channel->channel_id(); |
| 1948 | if (engine()->vie()->base()->StartSend(channel_id) != 0) { |
| 1949 | LOG_RTCERR1(StartSend, channel_id); |
| 1950 | return false; |
| 1951 | } |
| 1952 | |
| 1953 | send_channel->set_sending(true); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1954 | return true; |
| 1955 | } |
| 1956 | |
| 1957 | bool WebRtcVideoMediaChannel::StopSend() { |
| 1958 | bool success = true; |
| 1959 | for (SendChannelMap::iterator iter = send_channels_.begin(); |
| 1960 | iter != send_channels_.end(); ++iter) { |
| 1961 | WebRtcVideoChannelSendInfo* send_channel = iter->second; |
| 1962 | if (!StopSend(send_channel)) { |
| 1963 | success = false; |
| 1964 | } |
| 1965 | } |
| 1966 | return success; |
| 1967 | } |
| 1968 | |
| 1969 | bool WebRtcVideoMediaChannel::StopSend( |
| 1970 | WebRtcVideoChannelSendInfo* send_channel) { |
| 1971 | const int channel_id = send_channel->channel_id(); |
| 1972 | if (engine()->vie()->base()->StopSend(channel_id) != 0) { |
| 1973 | LOG_RTCERR1(StopSend, channel_id); |
| 1974 | return false; |
| 1975 | } |
| 1976 | send_channel->set_sending(false); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1977 | return true; |
| 1978 | } |
| 1979 | |
| 1980 | bool WebRtcVideoMediaChannel::SendIntraFrame() { |
| 1981 | bool success = true; |
| 1982 | for (SendChannelMap::iterator iter = send_channels_.begin(); |
| 1983 | iter != send_channels_.end(); |
| 1984 | ++iter) { |
| 1985 | WebRtcVideoChannelSendInfo* send_channel = iter->second; |
| 1986 | const int channel_id = send_channel->channel_id(); |
| 1987 | if (engine()->vie()->codec()->SendKeyFrame(channel_id) != 0) { |
| 1988 | LOG_RTCERR1(SendKeyFrame, channel_id); |
| 1989 | success = false; |
| 1990 | } |
| 1991 | } |
| 1992 | return success; |
| 1993 | } |
| 1994 | |
| 1995 | bool WebRtcVideoMediaChannel::IsOneSsrcStream(const StreamParams& sp) { |
| 1996 | return (sp.ssrcs.size() == 1 && sp.ssrc_groups.size() == 0); |
| 1997 | } |
| 1998 | |
| 1999 | bool WebRtcVideoMediaChannel::HasReadySendChannels() { |
| 2000 | return !send_channels_.empty() && |
| 2001 | ((send_channels_.size() > 1) || |
| 2002 | (send_channels_[0]->stream_params() != NULL)); |
| 2003 | } |
| 2004 | |
| 2005 | bool WebRtcVideoMediaChannel::GetSendChannelKey(uint32 local_ssrc, |
| 2006 | uint32* key) { |
| 2007 | *key = 0; |
| 2008 | // If a send channel is not ready to send it will not have local_ssrc |
| 2009 | // registered to it. |
| 2010 | if (!HasReadySendChannels()) { |
| 2011 | return false; |
| 2012 | } |
| 2013 | // The default channel is stored with key 0. The key therefore does not match |
| 2014 | // the SSRC associated with the default channel. Check if the SSRC provided |
| 2015 | // corresponds to the default channel's SSRC. |
| 2016 | if (local_ssrc == GetDefaultChannelSsrc()) { |
| 2017 | return true; |
| 2018 | } |
| 2019 | if (send_channels_.find(local_ssrc) == send_channels_.end()) { |
| 2020 | for (SendChannelMap::iterator iter = send_channels_.begin(); |
| 2021 | iter != send_channels_.end(); ++iter) { |
| 2022 | WebRtcVideoChannelSendInfo* send_channel = iter->second; |
| 2023 | if (send_channel->has_ssrc(local_ssrc)) { |
| 2024 | *key = iter->first; |
| 2025 | return true; |
| 2026 | } |
| 2027 | } |
| 2028 | return false; |
| 2029 | } |
| 2030 | // The key was found in the above std::map::find call. This means that the |
| 2031 | // ssrc is the key. |
| 2032 | *key = local_ssrc; |
| 2033 | return true; |
| 2034 | } |
| 2035 | |
| 2036 | WebRtcVideoChannelSendInfo* WebRtcVideoMediaChannel::GetSendChannel( |
| 2037 | VideoCapturer* video_capturer) { |
| 2038 | for (SendChannelMap::iterator iter = send_channels_.begin(); |
| 2039 | iter != send_channels_.end(); ++iter) { |
| 2040 | WebRtcVideoChannelSendInfo* send_channel = iter->second; |
| 2041 | if (send_channel->video_capturer() == video_capturer) { |
| 2042 | return send_channel; |
| 2043 | } |
| 2044 | } |
| 2045 | return NULL; |
| 2046 | } |
| 2047 | |
| 2048 | WebRtcVideoChannelSendInfo* WebRtcVideoMediaChannel::GetSendChannel( |
| 2049 | uint32 local_ssrc) { |
| 2050 | uint32 key; |
| 2051 | if (!GetSendChannelKey(local_ssrc, &key)) { |
| 2052 | return NULL; |
| 2053 | } |
| 2054 | return send_channels_[key]; |
| 2055 | } |
| 2056 | |
| 2057 | bool WebRtcVideoMediaChannel::CreateSendChannelKey(uint32 local_ssrc, |
| 2058 | uint32* key) { |
| 2059 | if (GetSendChannelKey(local_ssrc, key)) { |
| 2060 | // If there is a key corresponding to |local_ssrc|, the SSRC is already in |
| 2061 | // use. SSRCs need to be unique in a session and at this point a duplicate |
| 2062 | // SSRC has been detected. |
| 2063 | return false; |
| 2064 | } |
| 2065 | if (send_channels_[0]->stream_params() == NULL) { |
| 2066 | // key should be 0 here as the default channel should be re-used whenever it |
| 2067 | // is not used. |
| 2068 | *key = 0; |
| 2069 | return true; |
| 2070 | } |
| 2071 | // SSRC is currently not in use and the default channel is already in use. Use |
| 2072 | // the SSRC as key since it is supposed to be unique in a session. |
| 2073 | *key = local_ssrc; |
| 2074 | return true; |
| 2075 | } |
| 2076 | |
| 2077 | uint32 WebRtcVideoMediaChannel::GetDefaultChannelSsrc() { |
| 2078 | WebRtcVideoChannelSendInfo* send_channel = send_channels_[0]; |
| 2079 | const StreamParams* sp = send_channel->stream_params(); |
| 2080 | if (sp == NULL) { |
| 2081 | // This happens if no send stream is currently registered. |
| 2082 | return 0; |
| 2083 | } |
| 2084 | return sp->first_ssrc(); |
| 2085 | } |
| 2086 | |
| 2087 | bool WebRtcVideoMediaChannel::DeleteSendChannel(uint32 ssrc_key) { |
| 2088 | if (send_channels_.find(ssrc_key) == send_channels_.end()) { |
| 2089 | return false; |
| 2090 | } |
| 2091 | WebRtcVideoChannelSendInfo* send_channel = send_channels_[ssrc_key]; |
| 2092 | VideoCapturer* capturer = send_channel->video_capturer(); |
| 2093 | if (capturer != NULL) { |
| 2094 | capturer->SignalVideoFrame.disconnect(this); |
| 2095 | send_channel->set_video_capturer(NULL); |
| 2096 | } |
| 2097 | |
| 2098 | int channel_id = send_channel->channel_id(); |
| 2099 | int capture_id = send_channel->capture_id(); |
| 2100 | if (engine()->vie()->codec()->DeregisterEncoderObserver( |
| 2101 | channel_id) != 0) { |
| 2102 | LOG_RTCERR1(DeregisterEncoderObserver, channel_id); |
| 2103 | } |
| 2104 | |
| 2105 | // Destroy the external capture interface. |
| 2106 | if (engine()->vie()->capture()->DisconnectCaptureDevice( |
| 2107 | channel_id) != 0) { |
| 2108 | LOG_RTCERR1(DisconnectCaptureDevice, channel_id); |
| 2109 | } |
| 2110 | if (engine()->vie()->capture()->ReleaseCaptureDevice( |
| 2111 | capture_id) != 0) { |
| 2112 | LOG_RTCERR1(ReleaseCaptureDevice, capture_id); |
| 2113 | } |
| 2114 | |
| 2115 | // The default channel is stored in both |send_channels_| and |
| 2116 | // |recv_channels_|. To make sure it is only deleted once from vie let the |
| 2117 | // delete call happen when tearing down |recv_channels_| and not here. |
| 2118 | if (!IsDefaultChannel(channel_id)) { |
| 2119 | engine_->vie()->base()->DeleteChannel(channel_id); |
| 2120 | } |
| 2121 | delete send_channel; |
| 2122 | send_channels_.erase(ssrc_key); |
| 2123 | return true; |
| 2124 | } |
| 2125 | |
| 2126 | bool WebRtcVideoMediaChannel::RemoveCapturer(uint32 ssrc) { |
| 2127 | WebRtcVideoChannelSendInfo* send_channel = GetSendChannel(ssrc); |
| 2128 | if (!send_channel) { |
| 2129 | return false; |
| 2130 | } |
| 2131 | VideoCapturer* capturer = send_channel->video_capturer(); |
| 2132 | if (capturer == NULL) { |
| 2133 | return false; |
| 2134 | } |
| 2135 | capturer->SignalVideoFrame.disconnect(this); |
| 2136 | send_channel->set_video_capturer(NULL); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2137 | const int64 timestamp = send_channel->local_stream_info()->time_stamp(); |
| 2138 | if (send_codec_) { |
| 2139 | QueueBlackFrame(ssrc, timestamp, send_codec_->maxFramerate); |
| 2140 | } |
| 2141 | return true; |
| 2142 | } |
| 2143 | |
| 2144 | bool WebRtcVideoMediaChannel::SetRenderer(uint32 ssrc, |
| 2145 | VideoRenderer* renderer) { |
| 2146 | if (recv_channels_.find(ssrc) == recv_channels_.end()) { |
| 2147 | // TODO(perkj): Remove this once BWE works properly across different send |
| 2148 | // and receive channels. |
| 2149 | // The default channel is reused for recv stream in 1:1 call. |
| 2150 | if (first_receive_ssrc_ == ssrc && |
| 2151 | recv_channels_.find(0) != recv_channels_.end()) { |
| 2152 | LOG(LS_INFO) << "SetRenderer " << ssrc |
| 2153 | << " reuse default channel #" |
| 2154 | << vie_channel_; |
| 2155 | recv_channels_[0]->SetRenderer(renderer); |
| 2156 | return true; |
| 2157 | } |
| 2158 | return false; |
| 2159 | } |
| 2160 | |
| 2161 | recv_channels_[ssrc]->SetRenderer(renderer); |
| 2162 | return true; |
| 2163 | } |
| 2164 | |
| 2165 | bool WebRtcVideoMediaChannel::GetStats(VideoMediaInfo* info) { |
| 2166 | // Get sender statistics and build VideoSenderInfo. |
| 2167 | unsigned int total_bitrate_sent = 0; |
| 2168 | unsigned int video_bitrate_sent = 0; |
| 2169 | unsigned int fec_bitrate_sent = 0; |
| 2170 | unsigned int nack_bitrate_sent = 0; |
| 2171 | unsigned int estimated_send_bandwidth = 0; |
| 2172 | unsigned int target_enc_bitrate = 0; |
| 2173 | if (send_codec_) { |
| 2174 | for (SendChannelMap::const_iterator iter = send_channels_.begin(); |
| 2175 | iter != send_channels_.end(); ++iter) { |
| 2176 | WebRtcVideoChannelSendInfo* send_channel = iter->second; |
| 2177 | const int channel_id = send_channel->channel_id(); |
| 2178 | VideoSenderInfo sinfo; |
| 2179 | const StreamParams* send_params = send_channel->stream_params(); |
| 2180 | if (send_params == NULL) { |
| 2181 | // This should only happen if the default vie channel is not in use. |
| 2182 | // This can happen if no streams have ever been added or the stream |
| 2183 | // corresponding to the default channel has been removed. Note that |
| 2184 | // there may be non-default vie channels in use when this happen so |
| 2185 | // asserting send_channels_.size() == 1 is not correct and neither is |
| 2186 | // breaking out of the loop. |
| 2187 | ASSERT(channel_id == vie_channel_); |
| 2188 | continue; |
| 2189 | } |
| 2190 | unsigned int bytes_sent, packets_sent, bytes_recv, packets_recv; |
| 2191 | if (engine_->vie()->rtp()->GetRTPStatistics(channel_id, bytes_sent, |
| 2192 | packets_sent, bytes_recv, |
| 2193 | packets_recv) != 0) { |
| 2194 | LOG_RTCERR1(GetRTPStatistics, vie_channel_); |
| 2195 | continue; |
| 2196 | } |
| 2197 | WebRtcLocalStreamInfo* channel_stream_info = |
| 2198 | send_channel->local_stream_info(); |
| 2199 | |
| 2200 | sinfo.ssrcs = send_params->ssrcs; |
| 2201 | sinfo.codec_name = send_codec_->plName; |
| 2202 | sinfo.bytes_sent = bytes_sent; |
| 2203 | sinfo.packets_sent = packets_sent; |
| 2204 | sinfo.packets_cached = -1; |
| 2205 | sinfo.packets_lost = -1; |
| 2206 | sinfo.fraction_lost = -1; |
| 2207 | sinfo.firs_rcvd = -1; |
| 2208 | sinfo.nacks_rcvd = -1; |
| 2209 | sinfo.rtt_ms = -1; |
henrike@webrtc.org | 28654cb | 2013-07-22 21:07:49 +0000 | [diff] [blame] | 2210 | sinfo.frame_width = static_cast<int>(channel_stream_info->width()); |
| 2211 | sinfo.frame_height = static_cast<int>(channel_stream_info->height()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2212 | sinfo.framerate_input = channel_stream_info->framerate(); |
| 2213 | sinfo.framerate_sent = send_channel->encoder_observer()->framerate(); |
| 2214 | sinfo.nominal_bitrate = send_channel->encoder_observer()->bitrate(); |
| 2215 | sinfo.preferred_bitrate = send_max_bitrate_; |
| 2216 | sinfo.adapt_reason = send_channel->CurrentAdaptReason(); |
| 2217 | |
| 2218 | // Get received RTCP statistics for the sender, if available. |
| 2219 | // It's not a fatal error if we can't, since RTCP may not have arrived |
| 2220 | // yet. |
| 2221 | uint16 r_fraction_lost; |
| 2222 | unsigned int r_cumulative_lost; |
| 2223 | unsigned int r_extended_max; |
| 2224 | unsigned int r_jitter; |
| 2225 | int r_rtt_ms; |
| 2226 | |
| 2227 | if (engine_->vie()->rtp()->GetSentRTCPStatistics( |
| 2228 | channel_id, |
| 2229 | r_fraction_lost, |
| 2230 | r_cumulative_lost, |
| 2231 | r_extended_max, |
| 2232 | r_jitter, r_rtt_ms) == 0) { |
| 2233 | // Convert Q8 to float. |
| 2234 | sinfo.packets_lost = r_cumulative_lost; |
| 2235 | sinfo.fraction_lost = static_cast<float>(r_fraction_lost) / (1 << 8); |
| 2236 | sinfo.rtt_ms = r_rtt_ms; |
| 2237 | } |
| 2238 | info->senders.push_back(sinfo); |
| 2239 | |
| 2240 | unsigned int channel_total_bitrate_sent = 0; |
| 2241 | unsigned int channel_video_bitrate_sent = 0; |
| 2242 | unsigned int channel_fec_bitrate_sent = 0; |
| 2243 | unsigned int channel_nack_bitrate_sent = 0; |
| 2244 | if (engine_->vie()->rtp()->GetBandwidthUsage( |
| 2245 | channel_id, channel_total_bitrate_sent, channel_video_bitrate_sent, |
| 2246 | channel_fec_bitrate_sent, channel_nack_bitrate_sent) == 0) { |
| 2247 | total_bitrate_sent += channel_total_bitrate_sent; |
| 2248 | video_bitrate_sent += channel_video_bitrate_sent; |
| 2249 | fec_bitrate_sent += channel_fec_bitrate_sent; |
| 2250 | nack_bitrate_sent += channel_nack_bitrate_sent; |
| 2251 | } else { |
| 2252 | LOG_RTCERR1(GetBandwidthUsage, channel_id); |
| 2253 | } |
| 2254 | |
| 2255 | unsigned int estimated_stream_send_bandwidth = 0; |
| 2256 | if (engine_->vie()->rtp()->GetEstimatedSendBandwidth( |
| 2257 | channel_id, &estimated_stream_send_bandwidth) == 0) { |
| 2258 | estimated_send_bandwidth += estimated_stream_send_bandwidth; |
| 2259 | } else { |
| 2260 | LOG_RTCERR1(GetEstimatedSendBandwidth, channel_id); |
| 2261 | } |
| 2262 | unsigned int target_enc_stream_bitrate = 0; |
| 2263 | if (engine_->vie()->codec()->GetCodecTargetBitrate( |
| 2264 | channel_id, &target_enc_stream_bitrate) == 0) { |
| 2265 | target_enc_bitrate += target_enc_stream_bitrate; |
| 2266 | } else { |
| 2267 | LOG_RTCERR1(GetCodecTargetBitrate, channel_id); |
| 2268 | } |
| 2269 | } |
| 2270 | } else { |
| 2271 | LOG(LS_WARNING) << "GetStats: sender information not ready."; |
| 2272 | } |
| 2273 | |
| 2274 | // Get the SSRC and stats for each receiver, based on our own calculations. |
| 2275 | unsigned int estimated_recv_bandwidth = 0; |
| 2276 | for (RecvChannelMap::const_iterator it = recv_channels_.begin(); |
| 2277 | it != recv_channels_.end(); ++it) { |
| 2278 | // Don't report receive statistics from the default channel if we have |
| 2279 | // specified receive channels. |
| 2280 | if (it->first == 0 && recv_channels_.size() > 1) |
| 2281 | continue; |
| 2282 | WebRtcVideoChannelRecvInfo* channel = it->second; |
| 2283 | |
| 2284 | unsigned int ssrc; |
| 2285 | // Get receiver statistics and build VideoReceiverInfo, if we have data. |
sergeyu@chromium.org | a59696b | 2013-09-13 23:48:58 +0000 | [diff] [blame] | 2286 | // Skip the default channel (ssrc == 0). |
| 2287 | if (engine_->vie()->rtp()->GetRemoteSSRC( |
| 2288 | channel->channel_id(), ssrc) != 0 || |
| 2289 | ssrc == 0) |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2290 | continue; |
| 2291 | |
| 2292 | unsigned int bytes_sent, packets_sent, bytes_recv, packets_recv; |
| 2293 | if (engine_->vie()->rtp()->GetRTPStatistics( |
| 2294 | channel->channel_id(), bytes_sent, packets_sent, bytes_recv, |
| 2295 | packets_recv) != 0) { |
| 2296 | LOG_RTCERR1(GetRTPStatistics, channel->channel_id()); |
| 2297 | return false; |
| 2298 | } |
| 2299 | VideoReceiverInfo rinfo; |
| 2300 | rinfo.ssrcs.push_back(ssrc); |
| 2301 | rinfo.bytes_rcvd = bytes_recv; |
| 2302 | rinfo.packets_rcvd = packets_recv; |
| 2303 | rinfo.packets_lost = -1; |
| 2304 | rinfo.packets_concealed = -1; |
| 2305 | rinfo.fraction_lost = -1; // from SentRTCP |
| 2306 | rinfo.firs_sent = channel->decoder_observer()->firs_requested(); |
| 2307 | rinfo.nacks_sent = -1; |
| 2308 | rinfo.frame_width = channel->render_adapter()->width(); |
| 2309 | rinfo.frame_height = channel->render_adapter()->height(); |
| 2310 | rinfo.framerate_rcvd = channel->decoder_observer()->framerate(); |
| 2311 | int fps = channel->render_adapter()->framerate(); |
| 2312 | rinfo.framerate_decoded = fps; |
| 2313 | rinfo.framerate_output = fps; |
| 2314 | |
| 2315 | // Get sent RTCP statistics. |
| 2316 | uint16 s_fraction_lost; |
| 2317 | unsigned int s_cumulative_lost; |
| 2318 | unsigned int s_extended_max; |
| 2319 | unsigned int s_jitter; |
| 2320 | int s_rtt_ms; |
| 2321 | if (engine_->vie()->rtp()->GetReceivedRTCPStatistics(channel->channel_id(), |
| 2322 | s_fraction_lost, s_cumulative_lost, s_extended_max, |
| 2323 | s_jitter, s_rtt_ms) == 0) { |
| 2324 | // Convert Q8 to float. |
| 2325 | rinfo.packets_lost = s_cumulative_lost; |
| 2326 | rinfo.fraction_lost = static_cast<float>(s_fraction_lost) / (1 << 8); |
| 2327 | } |
| 2328 | info->receivers.push_back(rinfo); |
| 2329 | |
| 2330 | unsigned int estimated_recv_stream_bandwidth = 0; |
| 2331 | if (engine_->vie()->rtp()->GetEstimatedReceiveBandwidth( |
| 2332 | channel->channel_id(), &estimated_recv_stream_bandwidth) == 0) { |
| 2333 | estimated_recv_bandwidth += estimated_recv_stream_bandwidth; |
| 2334 | } else { |
| 2335 | LOG_RTCERR1(GetEstimatedReceiveBandwidth, channel->channel_id()); |
| 2336 | } |
| 2337 | } |
| 2338 | |
| 2339 | // Build BandwidthEstimationInfo. |
| 2340 | // TODO(zhurunz): Add real unittest for this. |
| 2341 | BandwidthEstimationInfo bwe; |
| 2342 | |
| 2343 | // Calculations done above per send/receive stream. |
| 2344 | bwe.actual_enc_bitrate = video_bitrate_sent; |
| 2345 | bwe.transmit_bitrate = total_bitrate_sent; |
| 2346 | bwe.retransmit_bitrate = nack_bitrate_sent; |
| 2347 | bwe.available_send_bandwidth = estimated_send_bandwidth; |
| 2348 | bwe.available_recv_bandwidth = estimated_recv_bandwidth; |
| 2349 | bwe.target_enc_bitrate = target_enc_bitrate; |
| 2350 | |
| 2351 | info->bw_estimations.push_back(bwe); |
| 2352 | |
| 2353 | return true; |
| 2354 | } |
| 2355 | |
| 2356 | bool WebRtcVideoMediaChannel::SetCapturer(uint32 ssrc, |
| 2357 | VideoCapturer* capturer) { |
| 2358 | ASSERT(ssrc != 0); |
| 2359 | if (!capturer) { |
| 2360 | return RemoveCapturer(ssrc); |
| 2361 | } |
| 2362 | WebRtcVideoChannelSendInfo* send_channel = GetSendChannel(ssrc); |
| 2363 | if (!send_channel) { |
| 2364 | return false; |
| 2365 | } |
| 2366 | VideoCapturer* old_capturer = send_channel->video_capturer(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2367 | if (old_capturer) { |
| 2368 | old_capturer->SignalVideoFrame.disconnect(this); |
| 2369 | } |
| 2370 | |
| 2371 | send_channel->set_video_capturer(capturer); |
| 2372 | capturer->SignalVideoFrame.connect( |
| 2373 | this, |
| 2374 | &WebRtcVideoMediaChannel::AdaptAndSendFrame); |
| 2375 | if (!capturer->IsScreencast() && ratio_w_ != 0 && ratio_h_ != 0) { |
| 2376 | capturer->UpdateAspectRatio(ratio_w_, ratio_h_); |
| 2377 | } |
| 2378 | const int64 timestamp = send_channel->local_stream_info()->time_stamp(); |
| 2379 | if (send_codec_) { |
| 2380 | QueueBlackFrame(ssrc, timestamp, send_codec_->maxFramerate); |
| 2381 | } |
| 2382 | return true; |
| 2383 | } |
| 2384 | |
| 2385 | bool WebRtcVideoMediaChannel::RequestIntraFrame() { |
| 2386 | // There is no API exposed to application to request a key frame |
| 2387 | // ViE does this internally when there are errors from decoder |
| 2388 | return false; |
| 2389 | } |
| 2390 | |
| 2391 | void WebRtcVideoMediaChannel::OnPacketReceived(talk_base::Buffer* packet) { |
| 2392 | // Pick which channel to send this packet to. If this packet doesn't match |
| 2393 | // any multiplexed streams, just send it to the default channel. Otherwise, |
| 2394 | // send it to the specific decoder instance for that stream. |
| 2395 | uint32 ssrc = 0; |
| 2396 | if (!GetRtpSsrc(packet->data(), packet->length(), &ssrc)) |
| 2397 | return; |
| 2398 | int which_channel = GetRecvChannelNum(ssrc); |
| 2399 | if (which_channel == -1) { |
| 2400 | which_channel = video_channel(); |
| 2401 | } |
| 2402 | |
henrike@webrtc.org | 28654cb | 2013-07-22 21:07:49 +0000 | [diff] [blame] | 2403 | engine()->vie()->network()->ReceivedRTPPacket( |
| 2404 | which_channel, |
| 2405 | packet->data(), |
| 2406 | static_cast<int>(packet->length())); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2407 | } |
| 2408 | |
| 2409 | void WebRtcVideoMediaChannel::OnRtcpReceived(talk_base::Buffer* packet) { |
| 2410 | // Sending channels need all RTCP packets with feedback information. |
| 2411 | // Even sender reports can contain attached report blocks. |
| 2412 | // Receiving channels need sender reports in order to create |
| 2413 | // correct receiver reports. |
| 2414 | |
| 2415 | uint32 ssrc = 0; |
| 2416 | if (!GetRtcpSsrc(packet->data(), packet->length(), &ssrc)) { |
| 2417 | LOG(LS_WARNING) << "Failed to parse SSRC from received RTCP packet"; |
| 2418 | return; |
| 2419 | } |
| 2420 | int type = 0; |
| 2421 | if (!GetRtcpType(packet->data(), packet->length(), &type)) { |
| 2422 | LOG(LS_WARNING) << "Failed to parse type from received RTCP packet"; |
| 2423 | return; |
| 2424 | } |
| 2425 | |
| 2426 | // If it is a sender report, find the channel that is listening. |
| 2427 | if (type == kRtcpTypeSR) { |
| 2428 | int which_channel = GetRecvChannelNum(ssrc); |
| 2429 | if (which_channel != -1 && !IsDefaultChannel(which_channel)) { |
henrike@webrtc.org | 28654cb | 2013-07-22 21:07:49 +0000 | [diff] [blame] | 2430 | engine_->vie()->network()->ReceivedRTCPPacket( |
| 2431 | which_channel, |
| 2432 | packet->data(), |
| 2433 | static_cast<int>(packet->length())); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2434 | } |
| 2435 | } |
| 2436 | // SR may continue RR and any RR entry may correspond to any one of the send |
| 2437 | // channels. So all RTCP packets must be forwarded all send channels. ViE |
| 2438 | // will filter out RR internally. |
| 2439 | for (SendChannelMap::iterator iter = send_channels_.begin(); |
| 2440 | iter != send_channels_.end(); ++iter) { |
| 2441 | WebRtcVideoChannelSendInfo* send_channel = iter->second; |
| 2442 | int channel_id = send_channel->channel_id(); |
henrike@webrtc.org | 28654cb | 2013-07-22 21:07:49 +0000 | [diff] [blame] | 2443 | engine_->vie()->network()->ReceivedRTCPPacket( |
| 2444 | channel_id, |
| 2445 | packet->data(), |
| 2446 | static_cast<int>(packet->length())); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2447 | } |
| 2448 | } |
| 2449 | |
| 2450 | void WebRtcVideoMediaChannel::OnReadyToSend(bool ready) { |
| 2451 | SetNetworkTransmissionState(ready); |
| 2452 | } |
| 2453 | |
| 2454 | bool WebRtcVideoMediaChannel::MuteStream(uint32 ssrc, bool muted) { |
| 2455 | WebRtcVideoChannelSendInfo* send_channel = GetSendChannel(ssrc); |
| 2456 | if (!send_channel) { |
| 2457 | LOG(LS_ERROR) << "The specified ssrc " << ssrc << " is not in use."; |
| 2458 | return false; |
| 2459 | } |
| 2460 | send_channel->set_muted(muted); |
| 2461 | return true; |
| 2462 | } |
| 2463 | |
| 2464 | bool WebRtcVideoMediaChannel::SetRecvRtpHeaderExtensions( |
| 2465 | const std::vector<RtpHeaderExtension>& extensions) { |
| 2466 | if (receive_extensions_ == extensions) { |
| 2467 | return true; |
| 2468 | } |
| 2469 | receive_extensions_ = extensions; |
| 2470 | |
| 2471 | const RtpHeaderExtension* offset_extension = |
| 2472 | FindHeaderExtension(extensions, kRtpTimestampOffsetHeaderExtension); |
| 2473 | const RtpHeaderExtension* send_time_extension = |
| 2474 | FindHeaderExtension(extensions, kRtpAbsoluteSendTimeHeaderExtension); |
| 2475 | |
| 2476 | // Loop through all receive channels and enable/disable the extensions. |
| 2477 | for (RecvChannelMap::iterator channel_it = recv_channels_.begin(); |
| 2478 | channel_it != recv_channels_.end(); ++channel_it) { |
| 2479 | int channel_id = channel_it->second->channel_id(); |
| 2480 | if (!SetHeaderExtension( |
| 2481 | &webrtc::ViERTP_RTCP::SetReceiveTimestampOffsetStatus, channel_id, |
| 2482 | offset_extension)) { |
| 2483 | return false; |
| 2484 | } |
| 2485 | if (!SetHeaderExtension( |
| 2486 | &webrtc::ViERTP_RTCP::SetReceiveAbsoluteSendTimeStatus, channel_id, |
| 2487 | send_time_extension)) { |
| 2488 | return false; |
| 2489 | } |
| 2490 | } |
| 2491 | return true; |
| 2492 | } |
| 2493 | |
| 2494 | bool WebRtcVideoMediaChannel::SetSendRtpHeaderExtensions( |
| 2495 | const std::vector<RtpHeaderExtension>& extensions) { |
| 2496 | send_extensions_ = extensions; |
| 2497 | |
| 2498 | const RtpHeaderExtension* offset_extension = |
| 2499 | FindHeaderExtension(extensions, kRtpTimestampOffsetHeaderExtension); |
| 2500 | const RtpHeaderExtension* send_time_extension = |
| 2501 | FindHeaderExtension(extensions, kRtpAbsoluteSendTimeHeaderExtension); |
| 2502 | |
| 2503 | // Loop through all send channels and enable/disable the extensions. |
| 2504 | for (SendChannelMap::iterator channel_it = send_channels_.begin(); |
| 2505 | channel_it != send_channels_.end(); ++channel_it) { |
| 2506 | int channel_id = channel_it->second->channel_id(); |
| 2507 | if (!SetHeaderExtension( |
| 2508 | &webrtc::ViERTP_RTCP::SetSendTimestampOffsetStatus, channel_id, |
| 2509 | offset_extension)) { |
| 2510 | return false; |
| 2511 | } |
| 2512 | if (!SetHeaderExtension( |
| 2513 | &webrtc::ViERTP_RTCP::SetSendAbsoluteSendTimeStatus, channel_id, |
| 2514 | send_time_extension)) { |
| 2515 | return false; |
| 2516 | } |
| 2517 | } |
| 2518 | return true; |
| 2519 | } |
| 2520 | |
| 2521 | bool WebRtcVideoMediaChannel::SetSendBandwidth(bool autobw, int bps) { |
| 2522 | LOG(LS_INFO) << "WebRtcVideoMediaChanne::SetSendBandwidth"; |
| 2523 | |
| 2524 | if (InConferenceMode()) { |
| 2525 | LOG(LS_INFO) << "Conference mode ignores SetSendBandWidth"; |
| 2526 | return true; |
| 2527 | } |
| 2528 | |
| 2529 | if (!send_codec_) { |
| 2530 | LOG(LS_INFO) << "The send codec has not been set up yet"; |
| 2531 | return true; |
| 2532 | } |
| 2533 | |
| 2534 | int min_bitrate; |
| 2535 | int start_bitrate; |
| 2536 | int max_bitrate; |
| 2537 | if (autobw) { |
| 2538 | // Use the default values for min bitrate. |
| 2539 | min_bitrate = kMinVideoBitrate; |
| 2540 | // Use the default value or the bps for the max |
| 2541 | max_bitrate = (bps <= 0) ? send_max_bitrate_ : (bps / 1000); |
| 2542 | // Maximum start bitrate can be kStartVideoBitrate. |
| 2543 | start_bitrate = talk_base::_min(kStartVideoBitrate, max_bitrate); |
| 2544 | } else { |
| 2545 | // Use the default start or the bps as the target bitrate. |
| 2546 | int target_bitrate = (bps <= 0) ? kStartVideoBitrate : (bps / 1000); |
| 2547 | min_bitrate = target_bitrate; |
| 2548 | start_bitrate = target_bitrate; |
| 2549 | max_bitrate = target_bitrate; |
| 2550 | } |
| 2551 | |
| 2552 | if (!SetSendCodec(*send_codec_, min_bitrate, start_bitrate, max_bitrate)) { |
| 2553 | return false; |
| 2554 | } |
| 2555 | LogSendCodecChange("SetSendBandwidth()"); |
| 2556 | |
| 2557 | return true; |
| 2558 | } |
| 2559 | |
| 2560 | bool WebRtcVideoMediaChannel::SetOptions(const VideoOptions &options) { |
| 2561 | // Always accept options that are unchanged. |
| 2562 | if (options_ == options) { |
| 2563 | return true; |
| 2564 | } |
| 2565 | |
| 2566 | // Trigger SetSendCodec to set correct noise reduction state if the option has |
| 2567 | // changed. |
| 2568 | bool denoiser_changed = options.video_noise_reduction.IsSet() && |
| 2569 | (options_.video_noise_reduction != options.video_noise_reduction); |
| 2570 | |
| 2571 | bool leaky_bucket_changed = options.video_leaky_bucket.IsSet() && |
| 2572 | (options_.video_leaky_bucket != options.video_leaky_bucket); |
| 2573 | |
| 2574 | bool buffer_latency_changed = options.buffered_mode_latency.IsSet() && |
| 2575 | (options_.buffered_mode_latency != options.buffered_mode_latency); |
| 2576 | |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 2577 | bool cpu_overuse_detection_changed = options.cpu_overuse_detection.IsSet() && |
| 2578 | (options_.cpu_overuse_detection != options.cpu_overuse_detection); |
| 2579 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2580 | bool conference_mode_turned_off = false; |
| 2581 | if (options_.conference_mode.IsSet() && options.conference_mode.IsSet() && |
| 2582 | options_.conference_mode.GetWithDefaultIfUnset(false) && |
| 2583 | !options.conference_mode.GetWithDefaultIfUnset(false)) { |
| 2584 | conference_mode_turned_off = true; |
| 2585 | } |
| 2586 | |
| 2587 | // Save the options, to be interpreted where appropriate. |
| 2588 | // Use options_.SetAll() instead of assignment so that unset value in options |
| 2589 | // will not overwrite the previous option value. |
| 2590 | options_.SetAll(options); |
| 2591 | |
| 2592 | // Set CPU options for all send channels. |
| 2593 | for (SendChannelMap::iterator iter = send_channels_.begin(); |
| 2594 | iter != send_channels_.end(); ++iter) { |
| 2595 | WebRtcVideoChannelSendInfo* send_channel = iter->second; |
| 2596 | send_channel->ApplyCpuOptions(options_); |
| 2597 | } |
| 2598 | |
| 2599 | // Adjust send codec bitrate if needed. |
| 2600 | int conf_max_bitrate = kDefaultConferenceModeMaxVideoBitrate; |
| 2601 | |
| 2602 | int expected_bitrate = send_max_bitrate_; |
| 2603 | if (InConferenceMode()) { |
| 2604 | expected_bitrate = conf_max_bitrate; |
| 2605 | } else if (conference_mode_turned_off) { |
| 2606 | // This is a special case for turning conference mode off. |
| 2607 | // Max bitrate should go back to the default maximum value instead |
| 2608 | // of the current maximum. |
| 2609 | expected_bitrate = kMaxVideoBitrate; |
| 2610 | } |
| 2611 | |
| 2612 | if (send_codec_ && |
| 2613 | (send_max_bitrate_ != expected_bitrate || denoiser_changed)) { |
| 2614 | // On success, SetSendCodec() will reset send_max_bitrate_ to |
| 2615 | // expected_bitrate. |
| 2616 | if (!SetSendCodec(*send_codec_, |
| 2617 | send_min_bitrate_, |
| 2618 | send_start_bitrate_, |
| 2619 | expected_bitrate)) { |
| 2620 | return false; |
| 2621 | } |
| 2622 | LogSendCodecChange("SetOptions()"); |
| 2623 | } |
| 2624 | if (leaky_bucket_changed) { |
| 2625 | bool enable_leaky_bucket = |
| 2626 | options_.video_leaky_bucket.GetWithDefaultIfUnset(false); |
| 2627 | for (SendChannelMap::iterator it = send_channels_.begin(); |
| 2628 | it != send_channels_.end(); ++it) { |
| 2629 | if (engine()->vie()->rtp()->SetTransmissionSmoothingStatus( |
| 2630 | it->second->channel_id(), enable_leaky_bucket) != 0) { |
| 2631 | LOG_RTCERR2(SetTransmissionSmoothingStatus, it->second->channel_id(), |
| 2632 | enable_leaky_bucket); |
| 2633 | } |
| 2634 | } |
| 2635 | } |
| 2636 | if (buffer_latency_changed) { |
| 2637 | int buffer_latency = |
| 2638 | options_.buffered_mode_latency.GetWithDefaultIfUnset( |
| 2639 | cricket::kBufferedModeDisabled); |
| 2640 | for (SendChannelMap::iterator it = send_channels_.begin(); |
| 2641 | it != send_channels_.end(); ++it) { |
| 2642 | if (engine()->vie()->rtp()->SetSenderBufferingMode( |
| 2643 | it->second->channel_id(), buffer_latency) != 0) { |
| 2644 | LOG_RTCERR2(SetSenderBufferingMode, it->second->channel_id(), |
| 2645 | buffer_latency); |
| 2646 | } |
| 2647 | } |
| 2648 | for (RecvChannelMap::iterator it = recv_channels_.begin(); |
| 2649 | it != recv_channels_.end(); ++it) { |
| 2650 | if (engine()->vie()->rtp()->SetReceiverBufferingMode( |
| 2651 | it->second->channel_id(), buffer_latency) != 0) { |
| 2652 | LOG_RTCERR2(SetReceiverBufferingMode, it->second->channel_id(), |
| 2653 | buffer_latency); |
| 2654 | } |
| 2655 | } |
| 2656 | } |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 2657 | if (cpu_overuse_detection_changed) { |
| 2658 | bool cpu_overuse_detection = |
| 2659 | options_.cpu_overuse_detection.GetWithDefaultIfUnset(false); |
| 2660 | for (SendChannelMap::iterator iter = send_channels_.begin(); |
| 2661 | iter != send_channels_.end(); ++iter) { |
| 2662 | WebRtcVideoChannelSendInfo* send_channel = iter->second; |
| 2663 | send_channel->SetCpuOveruseDetection(cpu_overuse_detection); |
| 2664 | } |
| 2665 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2666 | return true; |
| 2667 | } |
| 2668 | |
| 2669 | void WebRtcVideoMediaChannel::SetInterface(NetworkInterface* iface) { |
| 2670 | MediaChannel::SetInterface(iface); |
| 2671 | // Set the RTP recv/send buffer to a bigger size |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 2672 | MediaChannel::SetOption(NetworkInterface::ST_RTP, |
| 2673 | talk_base::Socket::OPT_RCVBUF, |
| 2674 | kVideoRtpBufferSize); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2675 | |
| 2676 | // TODO(sriniv): Remove or re-enable this. |
| 2677 | // As part of b/8030474, send-buffer is size now controlled through |
| 2678 | // portallocator flags. |
| 2679 | // network_interface_->SetOption(NetworkInterface::ST_RTP, |
| 2680 | // talk_base::Socket::OPT_SNDBUF, |
| 2681 | // kVideoRtpBufferSize); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2682 | } |
| 2683 | |
| 2684 | void WebRtcVideoMediaChannel::UpdateAspectRatio(int ratio_w, int ratio_h) { |
| 2685 | ASSERT(ratio_w != 0); |
| 2686 | ASSERT(ratio_h != 0); |
| 2687 | ratio_w_ = ratio_w; |
| 2688 | ratio_h_ = ratio_h; |
| 2689 | // For now assume that all streams want the same aspect ratio. |
| 2690 | // TODO(hellner): remove the need for this assumption. |
| 2691 | for (SendChannelMap::iterator iter = send_channels_.begin(); |
| 2692 | iter != send_channels_.end(); ++iter) { |
| 2693 | WebRtcVideoChannelSendInfo* send_channel = iter->second; |
| 2694 | VideoCapturer* capturer = send_channel->video_capturer(); |
| 2695 | if (capturer) { |
| 2696 | capturer->UpdateAspectRatio(ratio_w, ratio_h); |
| 2697 | } |
| 2698 | } |
| 2699 | } |
| 2700 | |
| 2701 | bool WebRtcVideoMediaChannel::GetRenderer(uint32 ssrc, |
| 2702 | VideoRenderer** renderer) { |
| 2703 | RecvChannelMap::const_iterator it = recv_channels_.find(ssrc); |
| 2704 | if (it == recv_channels_.end()) { |
| 2705 | if (first_receive_ssrc_ == ssrc && |
| 2706 | recv_channels_.find(0) != recv_channels_.end()) { |
| 2707 | LOG(LS_INFO) << " GetRenderer " << ssrc |
| 2708 | << " reuse default renderer #" |
| 2709 | << vie_channel_; |
| 2710 | *renderer = recv_channels_[0]->render_adapter()->renderer(); |
| 2711 | return true; |
| 2712 | } |
| 2713 | return false; |
| 2714 | } |
| 2715 | |
| 2716 | *renderer = it->second->render_adapter()->renderer(); |
| 2717 | return true; |
| 2718 | } |
| 2719 | |
| 2720 | void WebRtcVideoMediaChannel::AdaptAndSendFrame(VideoCapturer* capturer, |
| 2721 | const VideoFrame* frame) { |
| 2722 | if (capturer->IsScreencast()) { |
| 2723 | // Do not adapt frames that are screencast. |
| 2724 | SendFrame(capturer, frame); |
| 2725 | return; |
| 2726 | } |
henrike@webrtc.org | 28654cb | 2013-07-22 21:07:49 +0000 | [diff] [blame] | 2727 | // TODO(thorcarpenter): This is broken. One capturer registered on two ssrc |
| 2728 | // will not send any video to the second ssrc send channel. We should remove |
| 2729 | // GetSendChannel(capturer) and pass in an ssrc here. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2730 | WebRtcVideoChannelSendInfo* send_channel = GetSendChannel(capturer); |
| 2731 | if (!send_channel) { |
| 2732 | SendFrame(capturer, frame); |
| 2733 | return; |
| 2734 | } |
| 2735 | const VideoFrame* output_frame = NULL; |
| 2736 | send_channel->AdaptFrame(frame, &output_frame); |
| 2737 | if (output_frame) { |
| 2738 | SendFrame(send_channel, output_frame, capturer->IsScreencast()); |
| 2739 | } |
| 2740 | } |
| 2741 | |
| 2742 | // TODO(zhurunz): Add unittests to test this function. |
| 2743 | void WebRtcVideoMediaChannel::SendFrame(VideoCapturer* capturer, |
| 2744 | const VideoFrame* frame) { |
| 2745 | // If there's send channel registers to the |capturer|, then only send the |
| 2746 | // frame to that channel and return. Otherwise send the frame to the default |
| 2747 | // channel, which currently taking frames from the engine. |
| 2748 | WebRtcVideoChannelSendInfo* send_channel = GetSendChannel(capturer); |
| 2749 | if (send_channel) { |
| 2750 | SendFrame(send_channel, frame, capturer->IsScreencast()); |
| 2751 | return; |
| 2752 | } |
| 2753 | // TODO(hellner): Remove below for loop once the captured frame no longer |
| 2754 | // come from the engine, i.e. the engine no longer owns a capturer. |
| 2755 | for (SendChannelMap::iterator iter = send_channels_.begin(); |
| 2756 | iter != send_channels_.end(); ++iter) { |
| 2757 | WebRtcVideoChannelSendInfo* send_channel = iter->second; |
| 2758 | if (send_channel->video_capturer() == NULL) { |
| 2759 | SendFrame(send_channel, frame, capturer->IsScreencast()); |
| 2760 | } |
| 2761 | } |
| 2762 | } |
| 2763 | |
| 2764 | bool WebRtcVideoMediaChannel::SendFrame( |
| 2765 | WebRtcVideoChannelSendInfo* send_channel, |
| 2766 | const VideoFrame* frame, |
| 2767 | bool is_screencast) { |
| 2768 | if (!send_channel) { |
| 2769 | return false; |
| 2770 | } |
| 2771 | if (!send_codec_) { |
| 2772 | // Send codec has not been set. No reason to process the frame any further. |
| 2773 | return false; |
| 2774 | } |
| 2775 | const VideoFormat& video_format = send_channel->video_format(); |
| 2776 | // If the frame should be dropped. |
| 2777 | const bool video_format_set = video_format != cricket::VideoFormat(); |
| 2778 | if (video_format_set && |
| 2779 | (video_format.width == 0 && video_format.height == 0)) { |
| 2780 | return true; |
| 2781 | } |
| 2782 | |
| 2783 | // Checks if we need to reset vie send codec. |
henrike@webrtc.org | 28654cb | 2013-07-22 21:07:49 +0000 | [diff] [blame] | 2784 | if (!MaybeResetVieSendCodec(send_channel, |
| 2785 | static_cast<int>(frame->GetWidth()), |
| 2786 | static_cast<int>(frame->GetHeight()), |
| 2787 | is_screencast, NULL)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2788 | LOG(LS_ERROR) << "MaybeResetVieSendCodec failed with " |
| 2789 | << frame->GetWidth() << "x" << frame->GetHeight(); |
| 2790 | return false; |
| 2791 | } |
| 2792 | const VideoFrame* frame_out = frame; |
| 2793 | talk_base::scoped_ptr<VideoFrame> processed_frame; |
| 2794 | // Disable muting for screencast. |
| 2795 | const bool mute = (send_channel->muted() && !is_screencast); |
| 2796 | send_channel->ProcessFrame(*frame_out, mute, processed_frame.use()); |
| 2797 | if (processed_frame) { |
| 2798 | frame_out = processed_frame.get(); |
| 2799 | } |
| 2800 | |
| 2801 | webrtc::ViEVideoFrameI420 frame_i420; |
| 2802 | // TODO(ronghuawu): Update the webrtc::ViEVideoFrameI420 |
| 2803 | // to use const unsigned char* |
| 2804 | frame_i420.y_plane = const_cast<unsigned char*>(frame_out->GetYPlane()); |
| 2805 | frame_i420.u_plane = const_cast<unsigned char*>(frame_out->GetUPlane()); |
| 2806 | frame_i420.v_plane = const_cast<unsigned char*>(frame_out->GetVPlane()); |
| 2807 | frame_i420.y_pitch = frame_out->GetYPitch(); |
| 2808 | frame_i420.u_pitch = frame_out->GetUPitch(); |
| 2809 | frame_i420.v_pitch = frame_out->GetVPitch(); |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 2810 | frame_i420.width = static_cast<uint16>(frame_out->GetWidth()); |
| 2811 | frame_i420.height = static_cast<uint16>(frame_out->GetHeight()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2812 | |
| 2813 | int64 timestamp_ntp_ms = 0; |
| 2814 | // TODO(justinlin): Reenable after Windows issues with clock drift are fixed. |
| 2815 | // Currently reverted to old behavior of discarding capture timestamp. |
| 2816 | #if 0 |
| 2817 | // If the frame timestamp is 0, we will use the deliver time. |
| 2818 | const int64 frame_timestamp = frame->GetTimeStamp(); |
| 2819 | if (frame_timestamp != 0) { |
| 2820 | if (abs(time(NULL) - frame_timestamp / talk_base::kNumNanosecsPerSec) > |
| 2821 | kTimestampDeltaInSecondsForWarning) { |
| 2822 | LOG(LS_WARNING) << "Frame timestamp differs by more than " |
| 2823 | << kTimestampDeltaInSecondsForWarning << " seconds from " |
| 2824 | << "current Unix timestamp."; |
| 2825 | } |
| 2826 | |
| 2827 | timestamp_ntp_ms = |
| 2828 | talk_base::UnixTimestampNanosecsToNtpMillisecs(frame_timestamp); |
| 2829 | } |
| 2830 | #endif |
| 2831 | |
| 2832 | return send_channel->external_capture()->IncomingFrameI420( |
| 2833 | frame_i420, timestamp_ntp_ms) == 0; |
| 2834 | } |
| 2835 | |
| 2836 | bool WebRtcVideoMediaChannel::CreateChannel(uint32 ssrc_key, |
| 2837 | MediaDirection direction, |
| 2838 | int* channel_id) { |
| 2839 | // There are 3 types of channels. Sending only, receiving only and |
| 2840 | // sending and receiving. The sending and receiving channel is the |
| 2841 | // default channel and there is only one. All other channels that are created |
| 2842 | // are associated with the default channel which must exist. The default |
| 2843 | // channel id is stored in |vie_channel_|. All channels need to know about |
| 2844 | // the default channel to properly handle remb which is why there are |
| 2845 | // different ViE create channel calls. |
| 2846 | // For this channel the local and remote ssrc key is 0. However, it may |
| 2847 | // have a non-zero local and/or remote ssrc depending on if it is currently |
| 2848 | // sending and/or receiving. |
| 2849 | if ((vie_channel_ == -1 || direction == MD_SENDRECV) && |
| 2850 | (!send_channels_.empty() || !recv_channels_.empty())) { |
| 2851 | ASSERT(false); |
| 2852 | return false; |
| 2853 | } |
| 2854 | |
| 2855 | *channel_id = -1; |
| 2856 | if (direction == MD_RECV) { |
| 2857 | // All rec channels are associated with the default channel |vie_channel_| |
| 2858 | if (engine_->vie()->base()->CreateReceiveChannel(*channel_id, |
| 2859 | vie_channel_) != 0) { |
| 2860 | LOG_RTCERR2(CreateReceiveChannel, *channel_id, vie_channel_); |
| 2861 | return false; |
| 2862 | } |
| 2863 | } else if (direction == MD_SEND) { |
| 2864 | if (engine_->vie()->base()->CreateChannel(*channel_id, |
| 2865 | vie_channel_) != 0) { |
| 2866 | LOG_RTCERR2(CreateChannel, *channel_id, vie_channel_); |
| 2867 | return false; |
| 2868 | } |
| 2869 | } else { |
| 2870 | ASSERT(direction == MD_SENDRECV); |
| 2871 | if (engine_->vie()->base()->CreateChannel(*channel_id) != 0) { |
| 2872 | LOG_RTCERR1(CreateChannel, *channel_id); |
| 2873 | return false; |
| 2874 | } |
| 2875 | } |
| 2876 | if (!ConfigureChannel(*channel_id, direction, ssrc_key)) { |
| 2877 | engine_->vie()->base()->DeleteChannel(*channel_id); |
| 2878 | *channel_id = -1; |
| 2879 | return false; |
| 2880 | } |
| 2881 | |
| 2882 | return true; |
| 2883 | } |
| 2884 | |
| 2885 | bool WebRtcVideoMediaChannel::ConfigureChannel(int channel_id, |
| 2886 | MediaDirection direction, |
| 2887 | uint32 ssrc_key) { |
| 2888 | const bool receiving = (direction == MD_RECV) || (direction == MD_SENDRECV); |
| 2889 | const bool sending = (direction == MD_SEND) || (direction == MD_SENDRECV); |
| 2890 | // Register external transport. |
| 2891 | if (engine_->vie()->network()->RegisterSendTransport( |
| 2892 | channel_id, *this) != 0) { |
| 2893 | LOG_RTCERR1(RegisterSendTransport, channel_id); |
| 2894 | return false; |
| 2895 | } |
| 2896 | |
| 2897 | // Set MTU. |
| 2898 | if (engine_->vie()->network()->SetMTU(channel_id, kVideoMtu) != 0) { |
| 2899 | LOG_RTCERR2(SetMTU, channel_id, kVideoMtu); |
| 2900 | return false; |
| 2901 | } |
| 2902 | // Turn on RTCP and loss feedback reporting. |
| 2903 | if (engine()->vie()->rtp()->SetRTCPStatus( |
| 2904 | channel_id, webrtc::kRtcpCompound_RFC4585) != 0) { |
| 2905 | LOG_RTCERR2(SetRTCPStatus, channel_id, webrtc::kRtcpCompound_RFC4585); |
| 2906 | return false; |
| 2907 | } |
| 2908 | // Enable pli as key frame request method. |
| 2909 | if (engine_->vie()->rtp()->SetKeyFrameRequestMethod( |
| 2910 | channel_id, webrtc::kViEKeyFrameRequestPliRtcp) != 0) { |
| 2911 | LOG_RTCERR2(SetKeyFrameRequestMethod, |
| 2912 | channel_id, webrtc::kViEKeyFrameRequestPliRtcp); |
| 2913 | return false; |
| 2914 | } |
| 2915 | if (!SetNackFec(channel_id, send_red_type_, send_fec_type_, nack_enabled_)) { |
| 2916 | // Logged in SetNackFec. Don't spam the logs. |
| 2917 | return false; |
| 2918 | } |
| 2919 | // Note that receiving must always be configured before sending to ensure |
| 2920 | // that send and receive channel is configured correctly (ConfigureReceiving |
| 2921 | // assumes no sending). |
| 2922 | if (receiving) { |
| 2923 | if (!ConfigureReceiving(channel_id, ssrc_key)) { |
| 2924 | return false; |
| 2925 | } |
| 2926 | } |
| 2927 | if (sending) { |
| 2928 | if (!ConfigureSending(channel_id, ssrc_key)) { |
| 2929 | return false; |
| 2930 | } |
| 2931 | } |
| 2932 | |
| 2933 | return true; |
| 2934 | } |
| 2935 | |
| 2936 | bool WebRtcVideoMediaChannel::ConfigureReceiving(int channel_id, |
| 2937 | uint32 remote_ssrc_key) { |
| 2938 | // Make sure that an SSRC/key isn't registered more than once. |
| 2939 | if (recv_channels_.find(remote_ssrc_key) != recv_channels_.end()) { |
| 2940 | return false; |
| 2941 | } |
| 2942 | // Connect the voice channel, if there is one. |
| 2943 | // TODO(perkj): The A/V is synched by the receiving channel. So we need to |
| 2944 | // know the SSRC of the remote audio channel in order to fetch the correct |
| 2945 | // webrtc VoiceEngine channel. For now- only sync the default channel used |
| 2946 | // in 1-1 calls. |
| 2947 | if (remote_ssrc_key == 0 && voice_channel_) { |
| 2948 | WebRtcVoiceMediaChannel* voice_channel = |
| 2949 | static_cast<WebRtcVoiceMediaChannel*>(voice_channel_); |
| 2950 | if (engine_->vie()->base()->ConnectAudioChannel( |
| 2951 | vie_channel_, voice_channel->voe_channel()) != 0) { |
| 2952 | LOG_RTCERR2(ConnectAudioChannel, channel_id, |
| 2953 | voice_channel->voe_channel()); |
| 2954 | LOG(LS_WARNING) << "A/V not synchronized"; |
| 2955 | // Not a fatal error. |
| 2956 | } |
| 2957 | } |
| 2958 | |
| 2959 | talk_base::scoped_ptr<WebRtcVideoChannelRecvInfo> channel_info( |
| 2960 | new WebRtcVideoChannelRecvInfo(channel_id)); |
| 2961 | |
| 2962 | // Install a render adapter. |
| 2963 | if (engine_->vie()->render()->AddRenderer(channel_id, |
| 2964 | webrtc::kVideoI420, channel_info->render_adapter()) != 0) { |
| 2965 | LOG_RTCERR3(AddRenderer, channel_id, webrtc::kVideoI420, |
| 2966 | channel_info->render_adapter()); |
| 2967 | return false; |
| 2968 | } |
| 2969 | |
| 2970 | |
| 2971 | if (engine_->vie()->rtp()->SetRembStatus(channel_id, |
| 2972 | kNotSending, |
| 2973 | remb_enabled_) != 0) { |
| 2974 | LOG_RTCERR3(SetRembStatus, channel_id, kNotSending, remb_enabled_); |
| 2975 | return false; |
| 2976 | } |
| 2977 | |
| 2978 | if (!SetHeaderExtension(&webrtc::ViERTP_RTCP::SetReceiveTimestampOffsetStatus, |
| 2979 | channel_id, receive_extensions_, kRtpTimestampOffsetHeaderExtension)) { |
| 2980 | return false; |
| 2981 | } |
| 2982 | |
| 2983 | if (!SetHeaderExtension( |
| 2984 | &webrtc::ViERTP_RTCP::SetReceiveAbsoluteSendTimeStatus, channel_id, |
| 2985 | receive_extensions_, kRtpAbsoluteSendTimeHeaderExtension)) { |
| 2986 | return false; |
| 2987 | } |
| 2988 | |
| 2989 | if (remote_ssrc_key != 0) { |
| 2990 | // Use the same SSRC as our default channel |
| 2991 | // (so the RTCP reports are correct). |
| 2992 | unsigned int send_ssrc = 0; |
| 2993 | webrtc::ViERTP_RTCP* rtp = engine()->vie()->rtp(); |
| 2994 | if (rtp->GetLocalSSRC(vie_channel_, send_ssrc) == -1) { |
| 2995 | LOG_RTCERR2(GetLocalSSRC, vie_channel_, send_ssrc); |
| 2996 | return false; |
| 2997 | } |
| 2998 | if (rtp->SetLocalSSRC(channel_id, send_ssrc) == -1) { |
| 2999 | LOG_RTCERR2(SetLocalSSRC, channel_id, send_ssrc); |
| 3000 | return false; |
| 3001 | } |
| 3002 | } // Else this is the the default channel and we don't change the SSRC. |
| 3003 | |
| 3004 | // Disable color enhancement since it is a bit too aggressive. |
| 3005 | if (engine()->vie()->image()->EnableColorEnhancement(channel_id, |
| 3006 | false) != 0) { |
| 3007 | LOG_RTCERR1(EnableColorEnhancement, channel_id); |
| 3008 | return false; |
| 3009 | } |
| 3010 | |
| 3011 | if (!SetReceiveCodecs(channel_info.get())) { |
| 3012 | return false; |
| 3013 | } |
| 3014 | |
| 3015 | int buffer_latency = |
| 3016 | options_.buffered_mode_latency.GetWithDefaultIfUnset( |
| 3017 | cricket::kBufferedModeDisabled); |
| 3018 | if (buffer_latency != cricket::kBufferedModeDisabled) { |
| 3019 | if (engine()->vie()->rtp()->SetReceiverBufferingMode( |
| 3020 | channel_id, buffer_latency) != 0) { |
| 3021 | LOG_RTCERR2(SetReceiverBufferingMode, channel_id, buffer_latency); |
| 3022 | } |
| 3023 | } |
| 3024 | |
| 3025 | if (render_started_) { |
| 3026 | if (engine_->vie()->render()->StartRender(channel_id) != 0) { |
| 3027 | LOG_RTCERR1(StartRender, channel_id); |
| 3028 | return false; |
| 3029 | } |
| 3030 | } |
| 3031 | |
| 3032 | // Register decoder observer for incoming framerate and bitrate. |
| 3033 | if (engine()->vie()->codec()->RegisterDecoderObserver( |
| 3034 | channel_id, *channel_info->decoder_observer()) != 0) { |
| 3035 | LOG_RTCERR1(RegisterDecoderObserver, channel_info->decoder_observer()); |
| 3036 | return false; |
| 3037 | } |
| 3038 | |
| 3039 | recv_channels_[remote_ssrc_key] = channel_info.release(); |
| 3040 | return true; |
| 3041 | } |
| 3042 | |
| 3043 | bool WebRtcVideoMediaChannel::ConfigureSending(int channel_id, |
| 3044 | uint32 local_ssrc_key) { |
| 3045 | // The ssrc key can be zero or correspond to an SSRC. |
| 3046 | // Make sure the default channel isn't configured more than once. |
| 3047 | if (local_ssrc_key == 0 && send_channels_.find(0) != send_channels_.end()) { |
| 3048 | return false; |
| 3049 | } |
| 3050 | // Make sure that the SSRC is not already in use. |
| 3051 | uint32 dummy_key; |
| 3052 | if (GetSendChannelKey(local_ssrc_key, &dummy_key)) { |
| 3053 | return false; |
| 3054 | } |
| 3055 | int vie_capture = 0; |
| 3056 | webrtc::ViEExternalCapture* external_capture = NULL; |
| 3057 | // Register external capture. |
| 3058 | if (engine()->vie()->capture()->AllocateExternalCaptureDevice( |
| 3059 | vie_capture, external_capture) != 0) { |
| 3060 | LOG_RTCERR0(AllocateExternalCaptureDevice); |
| 3061 | return false; |
| 3062 | } |
| 3063 | |
| 3064 | // Connect external capture. |
| 3065 | if (engine()->vie()->capture()->ConnectCaptureDevice( |
| 3066 | vie_capture, channel_id) != 0) { |
| 3067 | LOG_RTCERR2(ConnectCaptureDevice, vie_capture, channel_id); |
| 3068 | return false; |
| 3069 | } |
| 3070 | talk_base::scoped_ptr<WebRtcVideoChannelSendInfo> send_channel( |
| 3071 | new WebRtcVideoChannelSendInfo(channel_id, vie_capture, |
| 3072 | external_capture, |
| 3073 | engine()->cpu_monitor())); |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 3074 | if (engine()->vie()->base()->RegisterCpuOveruseObserver( |
| 3075 | channel_id, send_channel->overuse_observer())) { |
| 3076 | LOG_RTCERR1(RegisterCpuOveruseObserver, channel_id); |
| 3077 | return false; |
| 3078 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3079 | send_channel->ApplyCpuOptions(options_); |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 3080 | send_channel->SignalCpuAdaptationUnable.connect(this, |
| 3081 | &WebRtcVideoMediaChannel::OnCpuAdaptationUnable); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3082 | |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 3083 | if (options_.cpu_overuse_detection.GetWithDefaultIfUnset(false)) { |
| 3084 | send_channel->SetCpuOveruseDetection(true); |
| 3085 | } |
| 3086 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3087 | // Register encoder observer for outgoing framerate and bitrate. |
| 3088 | if (engine()->vie()->codec()->RegisterEncoderObserver( |
| 3089 | channel_id, *send_channel->encoder_observer()) != 0) { |
| 3090 | LOG_RTCERR1(RegisterEncoderObserver, send_channel->encoder_observer()); |
| 3091 | return false; |
| 3092 | } |
| 3093 | |
| 3094 | if (!SetHeaderExtension(&webrtc::ViERTP_RTCP::SetSendTimestampOffsetStatus, |
| 3095 | channel_id, send_extensions_, kRtpTimestampOffsetHeaderExtension)) { |
| 3096 | return false; |
| 3097 | } |
| 3098 | |
| 3099 | if (!SetHeaderExtension(&webrtc::ViERTP_RTCP::SetSendAbsoluteSendTimeStatus, |
| 3100 | channel_id, send_extensions_, kRtpAbsoluteSendTimeHeaderExtension)) { |
| 3101 | return false; |
| 3102 | } |
| 3103 | |
| 3104 | if (options_.video_leaky_bucket.GetWithDefaultIfUnset(false)) { |
| 3105 | if (engine()->vie()->rtp()->SetTransmissionSmoothingStatus(channel_id, |
| 3106 | true) != 0) { |
| 3107 | LOG_RTCERR2(SetTransmissionSmoothingStatus, channel_id, true); |
| 3108 | return false; |
| 3109 | } |
| 3110 | } |
| 3111 | |
| 3112 | int buffer_latency = |
| 3113 | options_.buffered_mode_latency.GetWithDefaultIfUnset( |
| 3114 | cricket::kBufferedModeDisabled); |
| 3115 | if (buffer_latency != cricket::kBufferedModeDisabled) { |
| 3116 | if (engine()->vie()->rtp()->SetSenderBufferingMode( |
| 3117 | channel_id, buffer_latency) != 0) { |
| 3118 | LOG_RTCERR2(SetSenderBufferingMode, channel_id, buffer_latency); |
| 3119 | } |
| 3120 | } |
| 3121 | // The remb status direction correspond to the RTP stream (and not the RTCP |
| 3122 | // stream). I.e. if send remb is enabled it means it is receiving remote |
| 3123 | // rembs and should use them to estimate bandwidth. Receive remb mean that |
| 3124 | // remb packets will be generated and that the channel should be included in |
| 3125 | // it. If remb is enabled all channels are allowed to contribute to the remb |
| 3126 | // but only receive channels will ever end up actually contributing. This |
| 3127 | // keeps the logic simple. |
| 3128 | if (engine_->vie()->rtp()->SetRembStatus(channel_id, |
| 3129 | remb_enabled_, |
| 3130 | remb_enabled_) != 0) { |
| 3131 | LOG_RTCERR3(SetRembStatus, channel_id, remb_enabled_, remb_enabled_); |
| 3132 | return false; |
| 3133 | } |
| 3134 | if (!SetNackFec(channel_id, send_red_type_, send_fec_type_, nack_enabled_)) { |
| 3135 | // Logged in SetNackFec. Don't spam the logs. |
| 3136 | return false; |
| 3137 | } |
| 3138 | |
| 3139 | send_channels_[local_ssrc_key] = send_channel.release(); |
| 3140 | |
| 3141 | return true; |
| 3142 | } |
| 3143 | |
| 3144 | bool WebRtcVideoMediaChannel::SetNackFec(int channel_id, |
| 3145 | int red_payload_type, |
| 3146 | int fec_payload_type, |
| 3147 | bool nack_enabled) { |
| 3148 | bool enable = (red_payload_type != -1 && fec_payload_type != -1 && |
| 3149 | !InConferenceMode()); |
| 3150 | if (enable) { |
| 3151 | if (engine_->vie()->rtp()->SetHybridNACKFECStatus( |
| 3152 | channel_id, nack_enabled, red_payload_type, fec_payload_type) != 0) { |
| 3153 | LOG_RTCERR4(SetHybridNACKFECStatus, |
| 3154 | channel_id, nack_enabled, red_payload_type, fec_payload_type); |
| 3155 | return false; |
| 3156 | } |
| 3157 | LOG(LS_INFO) << "Hybrid NACK/FEC enabled for channel " << channel_id; |
| 3158 | } else { |
| 3159 | if (engine_->vie()->rtp()->SetNACKStatus(channel_id, nack_enabled) != 0) { |
| 3160 | LOG_RTCERR1(SetNACKStatus, channel_id); |
| 3161 | return false; |
| 3162 | } |
| 3163 | LOG(LS_INFO) << "NACK enabled for channel " << channel_id; |
| 3164 | } |
| 3165 | return true; |
| 3166 | } |
| 3167 | |
| 3168 | bool WebRtcVideoMediaChannel::SetSendCodec(const webrtc::VideoCodec& codec, |
| 3169 | int min_bitrate, |
| 3170 | int start_bitrate, |
| 3171 | int max_bitrate) { |
| 3172 | bool ret_val = true; |
| 3173 | for (SendChannelMap::iterator iter = send_channels_.begin(); |
| 3174 | iter != send_channels_.end(); ++iter) { |
| 3175 | WebRtcVideoChannelSendInfo* send_channel = iter->second; |
| 3176 | ret_val = SetSendCodec(send_channel, codec, min_bitrate, start_bitrate, |
| 3177 | max_bitrate) && ret_val; |
| 3178 | } |
| 3179 | if (ret_val) { |
| 3180 | // All SetSendCodec calls were successful. Update the global state |
| 3181 | // accordingly. |
| 3182 | send_codec_.reset(new webrtc::VideoCodec(codec)); |
| 3183 | send_min_bitrate_ = min_bitrate; |
| 3184 | send_start_bitrate_ = start_bitrate; |
| 3185 | send_max_bitrate_ = max_bitrate; |
| 3186 | } else { |
| 3187 | // At least one SetSendCodec call failed, rollback. |
| 3188 | for (SendChannelMap::iterator iter = send_channels_.begin(); |
| 3189 | iter != send_channels_.end(); ++iter) { |
| 3190 | WebRtcVideoChannelSendInfo* send_channel = iter->second; |
| 3191 | if (send_codec_) { |
| 3192 | SetSendCodec(send_channel, *send_codec_.get(), send_min_bitrate_, |
| 3193 | send_start_bitrate_, send_max_bitrate_); |
| 3194 | } |
| 3195 | } |
| 3196 | } |
| 3197 | return ret_val; |
| 3198 | } |
| 3199 | |
| 3200 | bool WebRtcVideoMediaChannel::SetSendCodec( |
| 3201 | WebRtcVideoChannelSendInfo* send_channel, |
| 3202 | const webrtc::VideoCodec& codec, |
| 3203 | int min_bitrate, |
| 3204 | int start_bitrate, |
| 3205 | int max_bitrate) { |
| 3206 | if (!send_channel) { |
| 3207 | return false; |
| 3208 | } |
| 3209 | const int channel_id = send_channel->channel_id(); |
| 3210 | // Make a copy of the codec |
| 3211 | webrtc::VideoCodec target_codec = codec; |
| 3212 | target_codec.startBitrate = start_bitrate; |
| 3213 | target_codec.minBitrate = min_bitrate; |
| 3214 | target_codec.maxBitrate = max_bitrate; |
| 3215 | |
| 3216 | // Set the default number of temporal layers for VP8. |
| 3217 | if (webrtc::kVideoCodecVP8 == codec.codecType) { |
| 3218 | target_codec.codecSpecific.VP8.numberOfTemporalLayers = |
| 3219 | kDefaultNumberOfTemporalLayers; |
| 3220 | |
| 3221 | // Turn off the VP8 error resilience |
| 3222 | target_codec.codecSpecific.VP8.resilience = webrtc::kResilienceOff; |
| 3223 | |
| 3224 | bool enable_denoising = |
| 3225 | options_.video_noise_reduction.GetWithDefaultIfUnset(false); |
| 3226 | target_codec.codecSpecific.VP8.denoisingOn = enable_denoising; |
| 3227 | } |
| 3228 | |
| 3229 | // Register external encoder if codec type is supported by encoder factory. |
| 3230 | if (engine()->IsExternalEncoderCodecType(codec.codecType) && |
| 3231 | !send_channel->IsEncoderRegistered(target_codec.plType)) { |
| 3232 | webrtc::VideoEncoder* encoder = |
| 3233 | engine()->CreateExternalEncoder(codec.codecType); |
| 3234 | if (encoder) { |
| 3235 | if (engine()->vie()->ext_codec()->RegisterExternalSendCodec( |
| 3236 | channel_id, target_codec.plType, encoder, false) == 0) { |
| 3237 | send_channel->RegisterEncoder(target_codec.plType, encoder); |
| 3238 | } else { |
| 3239 | LOG_RTCERR2(RegisterExternalSendCodec, channel_id, target_codec.plName); |
| 3240 | engine()->DestroyExternalEncoder(encoder); |
| 3241 | } |
| 3242 | } |
| 3243 | } |
| 3244 | |
| 3245 | // Resolution and framerate may vary for different send channels. |
| 3246 | const VideoFormat& video_format = send_channel->video_format(); |
| 3247 | UpdateVideoCodec(video_format, &target_codec); |
| 3248 | |
| 3249 | if (target_codec.width == 0 && target_codec.height == 0) { |
| 3250 | const uint32 ssrc = send_channel->stream_params()->first_ssrc(); |
| 3251 | LOG(LS_INFO) << "0x0 resolution selected. Captured frames will be dropped " |
| 3252 | << "for ssrc: " << ssrc << "."; |
| 3253 | } else { |
| 3254 | MaybeChangeStartBitrate(channel_id, &target_codec); |
| 3255 | if (0 != engine()->vie()->codec()->SetSendCodec(channel_id, target_codec)) { |
| 3256 | LOG_RTCERR2(SetSendCodec, channel_id, target_codec.plName); |
| 3257 | return false; |
| 3258 | } |
| 3259 | |
| 3260 | } |
| 3261 | send_channel->set_interval( |
| 3262 | cricket::VideoFormat::FpsToInterval(target_codec.maxFramerate)); |
| 3263 | return true; |
| 3264 | } |
| 3265 | |
| 3266 | |
| 3267 | static std::string ToString(webrtc::VideoCodecComplexity complexity) { |
| 3268 | switch (complexity) { |
| 3269 | case webrtc::kComplexityNormal: |
| 3270 | return "normal"; |
| 3271 | case webrtc::kComplexityHigh: |
| 3272 | return "high"; |
| 3273 | case webrtc::kComplexityHigher: |
| 3274 | return "higher"; |
| 3275 | case webrtc::kComplexityMax: |
| 3276 | return "max"; |
| 3277 | default: |
| 3278 | return "unknown"; |
| 3279 | } |
| 3280 | } |
| 3281 | |
| 3282 | static std::string ToString(webrtc::VP8ResilienceMode resilience) { |
| 3283 | switch (resilience) { |
| 3284 | case webrtc::kResilienceOff: |
| 3285 | return "off"; |
| 3286 | case webrtc::kResilientStream: |
| 3287 | return "stream"; |
| 3288 | case webrtc::kResilientFrames: |
| 3289 | return "frames"; |
| 3290 | default: |
| 3291 | return "unknown"; |
| 3292 | } |
| 3293 | } |
| 3294 | |
| 3295 | void WebRtcVideoMediaChannel::LogSendCodecChange(const std::string& reason) { |
| 3296 | webrtc::VideoCodec vie_codec; |
| 3297 | if (engine()->vie()->codec()->GetSendCodec(vie_channel_, vie_codec) != 0) { |
| 3298 | LOG_RTCERR1(GetSendCodec, vie_channel_); |
| 3299 | return; |
| 3300 | } |
| 3301 | |
| 3302 | LOG(LS_INFO) << reason << " : selected video codec " |
| 3303 | << vie_codec.plName << "/" |
| 3304 | << vie_codec.width << "x" << vie_codec.height << "x" |
| 3305 | << static_cast<int>(vie_codec.maxFramerate) << "fps" |
| 3306 | << "@" << vie_codec.maxBitrate << "kbps" |
| 3307 | << " (min=" << vie_codec.minBitrate << "kbps," |
| 3308 | << " start=" << vie_codec.startBitrate << "kbps)"; |
| 3309 | LOG(LS_INFO) << "Video max quantization: " << vie_codec.qpMax; |
| 3310 | if (webrtc::kVideoCodecVP8 == vie_codec.codecType) { |
| 3311 | LOG(LS_INFO) << "VP8 number of temporal layers: " |
| 3312 | << static_cast<int>( |
| 3313 | vie_codec.codecSpecific.VP8.numberOfTemporalLayers); |
| 3314 | LOG(LS_INFO) << "VP8 options : " |
| 3315 | << "picture loss indication = " |
| 3316 | << vie_codec.codecSpecific.VP8.pictureLossIndicationOn |
| 3317 | << ", feedback mode = " |
| 3318 | << vie_codec.codecSpecific.VP8.feedbackModeOn |
| 3319 | << ", complexity = " |
| 3320 | << ToString(vie_codec.codecSpecific.VP8.complexity) |
| 3321 | << ", resilience = " |
| 3322 | << ToString(vie_codec.codecSpecific.VP8.resilience) |
| 3323 | << ", denoising = " |
| 3324 | << vie_codec.codecSpecific.VP8.denoisingOn |
| 3325 | << ", error concealment = " |
| 3326 | << vie_codec.codecSpecific.VP8.errorConcealmentOn |
| 3327 | << ", automatic resize = " |
| 3328 | << vie_codec.codecSpecific.VP8.automaticResizeOn |
| 3329 | << ", frame dropping = " |
| 3330 | << vie_codec.codecSpecific.VP8.frameDroppingOn |
| 3331 | << ", key frame interval = " |
| 3332 | << vie_codec.codecSpecific.VP8.keyFrameInterval; |
| 3333 | } |
| 3334 | |
| 3335 | } |
| 3336 | |
| 3337 | bool WebRtcVideoMediaChannel::SetReceiveCodecs( |
| 3338 | WebRtcVideoChannelRecvInfo* info) { |
| 3339 | int red_type = -1; |
| 3340 | int fec_type = -1; |
| 3341 | int channel_id = info->channel_id(); |
| 3342 | for (std::vector<webrtc::VideoCodec>::iterator it = receive_codecs_.begin(); |
| 3343 | it != receive_codecs_.end(); ++it) { |
| 3344 | if (it->codecType == webrtc::kVideoCodecRED) { |
| 3345 | red_type = it->plType; |
| 3346 | } else if (it->codecType == webrtc::kVideoCodecULPFEC) { |
| 3347 | fec_type = it->plType; |
| 3348 | } |
| 3349 | if (engine()->vie()->codec()->SetReceiveCodec(channel_id, *it) != 0) { |
| 3350 | LOG_RTCERR2(SetReceiveCodec, channel_id, it->plName); |
| 3351 | return false; |
| 3352 | } |
| 3353 | if (!info->IsDecoderRegistered(it->plType) && |
| 3354 | it->codecType != webrtc::kVideoCodecRED && |
| 3355 | it->codecType != webrtc::kVideoCodecULPFEC) { |
| 3356 | webrtc::VideoDecoder* decoder = |
| 3357 | engine()->CreateExternalDecoder(it->codecType); |
| 3358 | if (decoder) { |
| 3359 | if (engine()->vie()->ext_codec()->RegisterExternalReceiveCodec( |
| 3360 | channel_id, it->plType, decoder) == 0) { |
| 3361 | info->RegisterDecoder(it->plType, decoder); |
| 3362 | } else { |
| 3363 | LOG_RTCERR2(RegisterExternalReceiveCodec, channel_id, it->plName); |
| 3364 | engine()->DestroyExternalDecoder(decoder); |
| 3365 | } |
| 3366 | } |
| 3367 | } |
| 3368 | } |
| 3369 | |
| 3370 | // Start receiving packets if at least one receive codec has been set. |
| 3371 | if (!receive_codecs_.empty()) { |
| 3372 | if (engine()->vie()->base()->StartReceive(channel_id) != 0) { |
| 3373 | LOG_RTCERR1(StartReceive, channel_id); |
| 3374 | return false; |
| 3375 | } |
| 3376 | } |
| 3377 | return true; |
| 3378 | } |
| 3379 | |
| 3380 | int WebRtcVideoMediaChannel::GetRecvChannelNum(uint32 ssrc) { |
| 3381 | if (ssrc == first_receive_ssrc_) { |
| 3382 | return vie_channel_; |
| 3383 | } |
| 3384 | RecvChannelMap::iterator it = recv_channels_.find(ssrc); |
| 3385 | return (it != recv_channels_.end()) ? it->second->channel_id() : -1; |
| 3386 | } |
| 3387 | |
| 3388 | // If the new frame size is different from the send codec size we set on vie, |
| 3389 | // we need to reset the send codec on vie. |
| 3390 | // The new send codec size should not exceed send_codec_ which is controlled |
| 3391 | // only by the 'jec' logic. |
| 3392 | bool WebRtcVideoMediaChannel::MaybeResetVieSendCodec( |
| 3393 | WebRtcVideoChannelSendInfo* send_channel, |
| 3394 | int new_width, |
| 3395 | int new_height, |
| 3396 | bool is_screencast, |
| 3397 | bool* reset) { |
| 3398 | if (reset) { |
| 3399 | *reset = false; |
| 3400 | } |
| 3401 | ASSERT(send_codec_.get() != NULL); |
| 3402 | |
| 3403 | webrtc::VideoCodec target_codec = *send_codec_.get(); |
| 3404 | const VideoFormat& video_format = send_channel->video_format(); |
| 3405 | UpdateVideoCodec(video_format, &target_codec); |
| 3406 | |
| 3407 | // Vie send codec size should not exceed target_codec. |
| 3408 | int target_width = new_width; |
| 3409 | int target_height = new_height; |
| 3410 | if (!is_screencast && |
| 3411 | (new_width > target_codec.width || new_height > target_codec.height)) { |
| 3412 | target_width = target_codec.width; |
| 3413 | target_height = target_codec.height; |
| 3414 | } |
| 3415 | |
| 3416 | // Get current vie codec. |
| 3417 | webrtc::VideoCodec vie_codec; |
| 3418 | const int channel_id = send_channel->channel_id(); |
| 3419 | if (engine()->vie()->codec()->GetSendCodec(channel_id, vie_codec) != 0) { |
| 3420 | LOG_RTCERR1(GetSendCodec, channel_id); |
| 3421 | return false; |
| 3422 | } |
| 3423 | const int cur_width = vie_codec.width; |
| 3424 | const int cur_height = vie_codec.height; |
| 3425 | |
| 3426 | // Only reset send codec when there is a size change. Additionally, |
| 3427 | // automatic resize needs to be turned off when screencasting and on when |
| 3428 | // not screencasting. |
| 3429 | // Don't allow automatic resizing for screencasting. |
| 3430 | bool automatic_resize = !is_screencast; |
| 3431 | // Turn off VP8 frame dropping when screensharing as the current model does |
| 3432 | // not work well at low fps. |
| 3433 | bool vp8_frame_dropping = !is_screencast; |
| 3434 | // Disable denoising for screencasting. |
| 3435 | bool enable_denoising = |
| 3436 | options_.video_noise_reduction.GetWithDefaultIfUnset(false); |
| 3437 | bool denoising = !is_screencast && enable_denoising; |
| 3438 | bool reset_send_codec = |
| 3439 | target_width != cur_width || target_height != cur_height || |
| 3440 | automatic_resize != vie_codec.codecSpecific.VP8.automaticResizeOn || |
| 3441 | denoising != vie_codec.codecSpecific.VP8.denoisingOn || |
| 3442 | vp8_frame_dropping != vie_codec.codecSpecific.VP8.frameDroppingOn; |
| 3443 | |
| 3444 | if (reset_send_codec) { |
| 3445 | // Set the new codec on vie. |
| 3446 | vie_codec.width = target_width; |
| 3447 | vie_codec.height = target_height; |
| 3448 | vie_codec.maxFramerate = target_codec.maxFramerate; |
| 3449 | vie_codec.startBitrate = target_codec.startBitrate; |
| 3450 | vie_codec.codecSpecific.VP8.automaticResizeOn = automatic_resize; |
| 3451 | vie_codec.codecSpecific.VP8.denoisingOn = denoising; |
| 3452 | vie_codec.codecSpecific.VP8.frameDroppingOn = vp8_frame_dropping; |
| 3453 | // TODO(mflodman): Remove 'is_screencast' check when screen cast settings |
| 3454 | // are treated correctly in WebRTC. |
| 3455 | if (!is_screencast) |
| 3456 | MaybeChangeStartBitrate(channel_id, &vie_codec); |
| 3457 | |
| 3458 | if (engine()->vie()->codec()->SetSendCodec(channel_id, vie_codec) != 0) { |
| 3459 | LOG_RTCERR1(SetSendCodec, channel_id); |
| 3460 | return false; |
| 3461 | } |
| 3462 | if (reset) { |
| 3463 | *reset = true; |
| 3464 | } |
| 3465 | LogSendCodecChange("Capture size changed"); |
| 3466 | } |
| 3467 | |
| 3468 | return true; |
| 3469 | } |
| 3470 | |
| 3471 | void WebRtcVideoMediaChannel::MaybeChangeStartBitrate( |
| 3472 | int channel_id, webrtc::VideoCodec* video_codec) { |
| 3473 | if (video_codec->startBitrate < video_codec->minBitrate) { |
| 3474 | video_codec->startBitrate = video_codec->minBitrate; |
| 3475 | } else if (video_codec->startBitrate > video_codec->maxBitrate) { |
| 3476 | video_codec->startBitrate = video_codec->maxBitrate; |
| 3477 | } |
| 3478 | |
| 3479 | // Use a previous target bitrate, if there is one. |
| 3480 | unsigned int current_target_bitrate = 0; |
| 3481 | if (engine()->vie()->codec()->GetCodecTargetBitrate( |
| 3482 | channel_id, ¤t_target_bitrate) == 0) { |
| 3483 | // Convert to kbps. |
| 3484 | current_target_bitrate /= 1000; |
| 3485 | if (current_target_bitrate > video_codec->maxBitrate) { |
| 3486 | current_target_bitrate = video_codec->maxBitrate; |
| 3487 | } |
| 3488 | if (current_target_bitrate > video_codec->startBitrate) { |
| 3489 | video_codec->startBitrate = current_target_bitrate; |
| 3490 | } |
| 3491 | } |
| 3492 | } |
| 3493 | |
| 3494 | void WebRtcVideoMediaChannel::OnMessage(talk_base::Message* msg) { |
| 3495 | FlushBlackFrameData* black_frame_data = |
henrike@webrtc.org | 28654cb | 2013-07-22 21:07:49 +0000 | [diff] [blame] | 3496 | static_cast<FlushBlackFrameData*>(msg->pdata); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3497 | FlushBlackFrame(black_frame_data->ssrc, black_frame_data->timestamp); |
| 3498 | delete black_frame_data; |
| 3499 | } |
| 3500 | |
| 3501 | int WebRtcVideoMediaChannel::SendPacket(int channel, const void* data, |
| 3502 | int len) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3503 | talk_base::Buffer packet(data, len, kMaxRtpPacketLen); |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 3504 | return MediaChannel::SendPacket(&packet) ? len : -1; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3505 | } |
| 3506 | |
| 3507 | int WebRtcVideoMediaChannel::SendRTCPPacket(int channel, |
| 3508 | const void* data, |
| 3509 | int len) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3510 | talk_base::Buffer packet(data, len, kMaxRtpPacketLen); |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 3511 | return MediaChannel::SendRtcp(&packet) ? len : -1; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3512 | } |
| 3513 | |
| 3514 | void WebRtcVideoMediaChannel::QueueBlackFrame(uint32 ssrc, int64 timestamp, |
| 3515 | int framerate) { |
| 3516 | if (timestamp) { |
| 3517 | FlushBlackFrameData* black_frame_data = new FlushBlackFrameData( |
| 3518 | ssrc, |
| 3519 | timestamp); |
henrike@webrtc.org | 28654cb | 2013-07-22 21:07:49 +0000 | [diff] [blame] | 3520 | const int delay_ms = static_cast<int>( |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3521 | 2 * cricket::VideoFormat::FpsToInterval(framerate) * |
| 3522 | talk_base::kNumMillisecsPerSec / talk_base::kNumNanosecsPerSec); |
| 3523 | worker_thread()->PostDelayed(delay_ms, this, 0, black_frame_data); |
| 3524 | } |
| 3525 | } |
| 3526 | |
| 3527 | void WebRtcVideoMediaChannel::FlushBlackFrame(uint32 ssrc, int64 timestamp) { |
| 3528 | WebRtcVideoChannelSendInfo* send_channel = GetSendChannel(ssrc); |
| 3529 | if (!send_channel) { |
| 3530 | return; |
| 3531 | } |
| 3532 | talk_base::scoped_ptr<const VideoFrame> black_frame_ptr; |
| 3533 | |
| 3534 | const WebRtcLocalStreamInfo* channel_stream_info = |
| 3535 | send_channel->local_stream_info(); |
| 3536 | int64 last_frame_time_stamp = channel_stream_info->time_stamp(); |
| 3537 | if (last_frame_time_stamp == timestamp) { |
| 3538 | size_t last_frame_width = 0; |
| 3539 | size_t last_frame_height = 0; |
| 3540 | int64 last_frame_elapsed_time = 0; |
| 3541 | channel_stream_info->GetLastFrameInfo(&last_frame_width, &last_frame_height, |
| 3542 | &last_frame_elapsed_time); |
| 3543 | if (!last_frame_width || !last_frame_height) { |
| 3544 | return; |
| 3545 | } |
| 3546 | WebRtcVideoFrame black_frame; |
| 3547 | // Black frame is not screencast. |
| 3548 | const bool screencasting = false; |
| 3549 | const int64 timestamp_delta = send_channel->interval(); |
| 3550 | if (!black_frame.InitToBlack(send_codec_->width, send_codec_->height, 1, 1, |
| 3551 | last_frame_elapsed_time + timestamp_delta, |
| 3552 | last_frame_time_stamp + timestamp_delta) || |
| 3553 | !SendFrame(send_channel, &black_frame, screencasting)) { |
| 3554 | LOG(LS_ERROR) << "Failed to send black frame."; |
| 3555 | } |
| 3556 | } |
| 3557 | } |
| 3558 | |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 3559 | void WebRtcVideoMediaChannel::OnCpuAdaptationUnable() { |
| 3560 | // ssrc is hardcoded to 0. This message is based on a system wide issue, |
| 3561 | // so finding which ssrc caused it doesn't matter. |
| 3562 | SignalMediaError(0, VideoMediaChannel::ERROR_REC_CPU_MAX_CANT_DOWNGRADE); |
| 3563 | } |
| 3564 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3565 | void WebRtcVideoMediaChannel::SetNetworkTransmissionState( |
| 3566 | bool is_transmitting) { |
| 3567 | LOG(LS_INFO) << "SetNetworkTransmissionState: " << is_transmitting; |
| 3568 | for (SendChannelMap::iterator iter = send_channels_.begin(); |
| 3569 | iter != send_channels_.end(); ++iter) { |
| 3570 | WebRtcVideoChannelSendInfo* send_channel = iter->second; |
| 3571 | int channel_id = send_channel->channel_id(); |
| 3572 | engine_->vie()->network()->SetNetworkTransmissionState(channel_id, |
| 3573 | is_transmitting); |
| 3574 | } |
| 3575 | } |
| 3576 | |
| 3577 | bool WebRtcVideoMediaChannel::SetHeaderExtension(ExtensionSetterFunction setter, |
| 3578 | int channel_id, const RtpHeaderExtension* extension) { |
| 3579 | bool enable = false; |
| 3580 | int id = 0; |
| 3581 | if (extension) { |
| 3582 | enable = true; |
| 3583 | id = extension->id; |
| 3584 | } |
| 3585 | if ((engine_->vie()->rtp()->*setter)(channel_id, enable, id) != 0) { |
| 3586 | LOG_RTCERR4(*setter, extension->uri, channel_id, enable, id); |
| 3587 | return false; |
| 3588 | } |
| 3589 | return true; |
| 3590 | } |
| 3591 | |
| 3592 | bool WebRtcVideoMediaChannel::SetHeaderExtension(ExtensionSetterFunction setter, |
| 3593 | int channel_id, const std::vector<RtpHeaderExtension>& extensions, |
| 3594 | const char header_extension_uri[]) { |
| 3595 | const RtpHeaderExtension* extension = FindHeaderExtension(extensions, |
| 3596 | header_extension_uri); |
| 3597 | return SetHeaderExtension(setter, channel_id, extension); |
| 3598 | } |
| 3599 | } // namespace cricket |
| 3600 | |
| 3601 | #endif // HAVE_WEBRTC_VIDEO |