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