aleloi | 440b6d9 | 2017-08-22 12:43:23 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 11 | #ifndef CALL_VIDEO_SEND_STREAM_H_ |
| 12 | #define CALL_VIDEO_SEND_STREAM_H_ |
aleloi | 440b6d9 | 2017-08-22 12:43:23 | [diff] [blame] | 13 | |
| 14 | #include <map> |
| 15 | #include <string> |
| 16 | #include <utility> |
| 17 | #include <vector> |
| 18 | |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 19 | #include "api/call/transport.h" |
| 20 | #include "api/rtpparameters.h" |
Patrik Höglund | 3e11343 | 2017-12-15 13:40:10 | [diff] [blame] | 21 | #include "api/rtp_headers.h" |
Patrik Höglund | be214a2 | 2018-01-04 11:14:35 | [diff] [blame] | 22 | #include "api/videosinkinterface.h" |
Patrik Höglund | 9e19403 | 2018-01-04 14:58:20 | [diff] [blame] | 23 | #include "api/videosourceinterface.h" |
Niels Möller | 88614b0 | 2018-03-27 14:39:01 | [diff] [blame] | 24 | #include "api/video_codecs/video_encoder_factory.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 25 | #include "call/rtp_config.h" |
| 26 | #include "call/video_config.h" |
Mirko Bonadei | 7120742 | 2017-09-15 11:58:09 | [diff] [blame] | 27 | #include "common_types.h" // NOLINT(build/include) |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 28 | #include "common_video/include/frame_callback.h" |
Patrik Höglund | 3e11343 | 2017-12-15 13:40:10 | [diff] [blame] | 29 | #include "modules/rtp_rtcp/include/rtp_rtcp_defines.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 30 | #include "rtc_base/platform_file.h" |
aleloi | 440b6d9 | 2017-08-22 12:43:23 | [diff] [blame] | 31 | |
| 32 | namespace webrtc { |
| 33 | |
| 34 | class VideoEncoder; |
| 35 | |
| 36 | class VideoSendStream { |
| 37 | public: |
| 38 | struct StreamStats { |
| 39 | StreamStats(); |
| 40 | ~StreamStats(); |
| 41 | |
| 42 | std::string ToString() const; |
| 43 | |
| 44 | FrameCounts frame_counts; |
| 45 | bool is_rtx = false; |
| 46 | bool is_flexfec = false; |
| 47 | int width = 0; |
| 48 | int height = 0; |
| 49 | // TODO(holmer): Move bitrate_bps out to the webrtc::Call layer. |
| 50 | int total_bitrate_bps = 0; |
| 51 | int retransmit_bitrate_bps = 0; |
| 52 | int avg_delay_ms = 0; |
| 53 | int max_delay_ms = 0; |
| 54 | StreamDataCounters rtp_stats; |
| 55 | RtcpPacketTypeCounter rtcp_packet_type_counts; |
| 56 | RtcpStatistics rtcp_stats; |
| 57 | }; |
| 58 | |
| 59 | struct Stats { |
| 60 | Stats(); |
| 61 | ~Stats(); |
| 62 | std::string ToString(int64_t time_ms) const; |
| 63 | std::string encoder_implementation_name = "unknown"; |
| 64 | int input_frame_rate = 0; |
| 65 | int encode_frame_rate = 0; |
| 66 | int avg_encode_time_ms = 0; |
| 67 | int encode_usage_percent = 0; |
| 68 | uint32_t frames_encoded = 0; |
Ilya Nikolaevskiy | d79314f | 2017-10-23 08:45:37 | [diff] [blame] | 69 | uint32_t frames_dropped_by_capturer = 0; |
| 70 | uint32_t frames_dropped_by_encoder_queue = 0; |
| 71 | uint32_t frames_dropped_by_rate_limiter = 0; |
| 72 | uint32_t frames_dropped_by_encoder = 0; |
aleloi | 440b6d9 | 2017-08-22 12:43:23 | [diff] [blame] | 73 | rtc::Optional<uint64_t> qp_sum; |
| 74 | // Bitrate the encoder is currently configured to use due to bandwidth |
| 75 | // limitations. |
| 76 | int target_media_bitrate_bps = 0; |
| 77 | // Bitrate the encoder is actually producing. |
| 78 | int media_bitrate_bps = 0; |
| 79 | // Media bitrate this VideoSendStream is configured to prefer if there are |
| 80 | // no bandwidth limitations. |
| 81 | int preferred_media_bitrate_bps = 0; |
| 82 | bool suspended = false; |
| 83 | bool bw_limited_resolution = false; |
| 84 | bool cpu_limited_resolution = false; |
| 85 | bool bw_limited_framerate = false; |
| 86 | bool cpu_limited_framerate = false; |
| 87 | // Total number of times resolution as been requested to be changed due to |
| 88 | // CPU/quality adaptation. |
| 89 | int number_of_cpu_adapt_changes = 0; |
| 90 | int number_of_quality_adapt_changes = 0; |
Åsa Persson | c3ed630 | 2017-11-16 13:04:52 | [diff] [blame] | 91 | bool has_entered_low_resolution = false; |
aleloi | 440b6d9 | 2017-08-22 12:43:23 | [diff] [blame] | 92 | std::map<uint32_t, StreamStats> substreams; |
ilnik | 50864a8 | 2017-09-06 19:32:35 | [diff] [blame] | 93 | webrtc::VideoContentType content_type = |
| 94 | webrtc::VideoContentType::UNSPECIFIED; |
Ilya Nikolaevskiy | 70473fc | 2018-02-28 15:35:03 | [diff] [blame] | 95 | uint32_t huge_frames_sent = 0; |
aleloi | 440b6d9 | 2017-08-22 12:43:23 | [diff] [blame] | 96 | }; |
| 97 | |
| 98 | struct Config { |
| 99 | public: |
| 100 | Config() = delete; |
| 101 | Config(Config&&); |
| 102 | explicit Config(Transport* send_transport); |
| 103 | |
| 104 | Config& operator=(Config&&); |
| 105 | Config& operator=(const Config&) = delete; |
| 106 | |
| 107 | ~Config(); |
| 108 | |
| 109 | // Mostly used by tests. Avoid creating copies if you can. |
| 110 | Config Copy() const { return Config(*this); } |
| 111 | |
| 112 | std::string ToString() const; |
| 113 | |
| 114 | struct EncoderSettings { |
| 115 | EncoderSettings() = default; |
aleloi | 440b6d9 | 2017-08-22 12:43:23 | [diff] [blame] | 116 | std::string ToString() const; |
| 117 | |
Niels Möller | 6539f69 | 2018-01-18 07:58:50 | [diff] [blame] | 118 | // Enables the new method to estimate the cpu load from encoding, used for |
| 119 | // cpu adaptation. |
| 120 | bool experiment_cpu_load_estimator = false; |
| 121 | |
Niels Möller | 88614b0 | 2018-03-27 14:39:01 | [diff] [blame] | 122 | // Ownership stays with WebrtcVideoEngine (delegated from PeerConnection). |
| 123 | VideoEncoderFactory* encoder_factory = nullptr; |
aleloi | 440b6d9 | 2017-08-22 12:43:23 | [diff] [blame] | 124 | } encoder_settings; |
| 125 | |
| 126 | static const size_t kDefaultMaxPacketSize = 1500 - 40; // TCP over IPv4. |
| 127 | struct Rtp { |
| 128 | Rtp(); |
| 129 | Rtp(const Rtp&); |
| 130 | ~Rtp(); |
| 131 | std::string ToString() const; |
| 132 | |
| 133 | std::vector<uint32_t> ssrcs; |
| 134 | |
Steve Anton | bb50ce5 | 2018-03-26 17:24:32 | [diff] [blame] | 135 | // The value to send in the MID RTP header extension if the extension is |
| 136 | // included in the list of extensions. |
| 137 | std::string mid; |
| 138 | |
aleloi | 440b6d9 | 2017-08-22 12:43:23 | [diff] [blame] | 139 | // See RtcpMode for description. |
| 140 | RtcpMode rtcp_mode = RtcpMode::kCompound; |
| 141 | |
| 142 | // Max RTP packet size delivered to send transport from VideoEngine. |
| 143 | size_t max_packet_size = kDefaultMaxPacketSize; |
| 144 | |
| 145 | // RTP header extensions to use for this send stream. |
| 146 | std::vector<RtpExtension> extensions; |
| 147 | |
Niels Möller | 12d6a49 | 2018-03-22 11:41:48 | [diff] [blame] | 148 | // TODO(nisse): For now, these are fixed, but we'd like to support |
| 149 | // changing codec without recreating the VideoSendStream. Then these |
| 150 | // fields must be removed, and association between payload type and codec |
| 151 | // must move above the per-stream level. Ownership could be with |
| 152 | // RtpTransportControllerSend, with a reference from PayloadRouter, where |
| 153 | // the latter would be responsible for mapping the codec type of encoded |
| 154 | // images to the right payload type. |
| 155 | std::string payload_name; |
| 156 | int payload_type = -1; |
| 157 | |
aleloi | 440b6d9 | 2017-08-22 12:43:23 | [diff] [blame] | 158 | // See NackConfig for description. |
| 159 | NackConfig nack; |
| 160 | |
| 161 | // See UlpfecConfig for description. |
| 162 | UlpfecConfig ulpfec; |
| 163 | |
| 164 | struct Flexfec { |
| 165 | Flexfec(); |
| 166 | Flexfec(const Flexfec&); |
| 167 | ~Flexfec(); |
| 168 | // Payload type of FlexFEC. Set to -1 to disable sending FlexFEC. |
| 169 | int payload_type = -1; |
| 170 | |
| 171 | // SSRC of FlexFEC stream. |
| 172 | uint32_t ssrc = 0; |
| 173 | |
| 174 | // Vector containing a single element, corresponding to the SSRC of the |
| 175 | // media stream being protected by this FlexFEC stream. |
| 176 | // The vector MUST have size 1. |
| 177 | // |
| 178 | // TODO(brandtr): Update comment above when we support |
| 179 | // multistream protection. |
| 180 | std::vector<uint32_t> protected_media_ssrcs; |
| 181 | } flexfec; |
| 182 | |
| 183 | // Settings for RTP retransmission payload format, see RFC 4588 for |
| 184 | // details. |
| 185 | struct Rtx { |
| 186 | Rtx(); |
| 187 | Rtx(const Rtx&); |
| 188 | ~Rtx(); |
| 189 | std::string ToString() const; |
| 190 | // SSRCs to use for the RTX streams. |
| 191 | std::vector<uint32_t> ssrcs; |
| 192 | |
| 193 | // Payload type to use for the RTX stream. |
| 194 | int payload_type = -1; |
| 195 | } rtx; |
| 196 | |
| 197 | // RTCP CNAME, see RFC 3550. |
| 198 | std::string c_name; |
| 199 | } rtp; |
| 200 | |
Jiawei Ou | 3587b83 | 2018-02-01 06:08:26 | [diff] [blame] | 201 | struct Rtcp { |
| 202 | Rtcp(); |
| 203 | Rtcp(const Rtcp&); |
| 204 | ~Rtcp(); |
| 205 | std::string ToString() const; |
| 206 | |
| 207 | // Time interval between RTCP report for video |
| 208 | int64_t video_report_interval_ms = 1000; |
| 209 | // Time interval between RTCP report for audio |
| 210 | int64_t audio_report_interval_ms = 5000; |
| 211 | } rtcp; |
| 212 | |
aleloi | 440b6d9 | 2017-08-22 12:43:23 | [diff] [blame] | 213 | // Transport for outgoing packets. |
| 214 | Transport* send_transport = nullptr; |
| 215 | |
| 216 | // Called for each I420 frame before encoding the frame. Can be used for |
| 217 | // effects, snapshots etc. 'nullptr' disables the callback. |
| 218 | rtc::VideoSinkInterface<VideoFrame>* pre_encode_callback = nullptr; |
| 219 | |
| 220 | // Called for each encoded frame, e.g. used for file storage. 'nullptr' |
| 221 | // disables the callback. Also measures timing and passes the time |
| 222 | // spent on encoding. This timing will not fire if encoding takes longer |
| 223 | // than the measuring window, since the sample data will have been dropped. |
| 224 | EncodedFrameObserver* post_encode_callback = nullptr; |
| 225 | |
| 226 | // Expected delay needed by the renderer, i.e. the frame will be delivered |
| 227 | // this many milliseconds, if possible, earlier than expected render time. |
| 228 | // Only valid if |local_renderer| is set. |
| 229 | int render_delay_ms = 0; |
| 230 | |
| 231 | // Target delay in milliseconds. A positive value indicates this stream is |
| 232 | // used for streaming instead of a real-time call. |
| 233 | int target_delay_ms = 0; |
| 234 | |
| 235 | // True if the stream should be suspended when the available bitrate fall |
| 236 | // below the minimum configured bitrate. If this variable is false, the |
| 237 | // stream may send at a rate higher than the estimated available bitrate. |
| 238 | bool suspend_below_min_bitrate = false; |
| 239 | |
| 240 | // Enables periodic bandwidth probing in application-limited region. |
| 241 | bool periodic_alr_bandwidth_probing = false; |
| 242 | |
Alex Narest | b3944f0 | 2017-10-13 12:56:18 | [diff] [blame] | 243 | // Track ID as specified during track creation. |
| 244 | std::string track_id; |
| 245 | |
aleloi | 440b6d9 | 2017-08-22 12:43:23 | [diff] [blame] | 246 | private: |
| 247 | // Access to the copy constructor is private to force use of the Copy() |
| 248 | // method for those exceptional cases where we do use it. |
| 249 | Config(const Config&); |
| 250 | }; |
| 251 | |
Seth Hampson | cc7125f | 2018-02-02 16:46:16 | [diff] [blame] | 252 | // Updates the sending state for all simulcast layers that the video send |
| 253 | // stream owns. This can mean updating the activity one or for multiple |
| 254 | // layers. The ordering of active layers is the order in which the |
| 255 | // rtp modules are stored in the VideoSendStream. |
| 256 | // Note: This starts stream activity if it is inactive and one of the layers |
| 257 | // is active. This stops stream activity if it is active and all layers are |
| 258 | // inactive. |
| 259 | virtual void UpdateActiveSimulcastLayers( |
| 260 | const std::vector<bool> active_layers) = 0; |
| 261 | |
aleloi | 440b6d9 | 2017-08-22 12:43:23 | [diff] [blame] | 262 | // Starts stream activity. |
| 263 | // When a stream is active, it can receive, process and deliver packets. |
| 264 | virtual void Start() = 0; |
| 265 | // Stops stream activity. |
| 266 | // When a stream is stopped, it can't receive, process or deliver packets. |
| 267 | virtual void Stop() = 0; |
| 268 | |
| 269 | // Based on the spec in |
| 270 | // https://w3c.github.io/webrtc-pc/#idl-def-rtcdegradationpreference. |
| 271 | // These options are enforced on a best-effort basis. For instance, all of |
| 272 | // these options may suffer some frame drops in order to avoid queuing. |
| 273 | // TODO(sprang): Look into possibility of more strictly enforcing the |
| 274 | // maintain-framerate option. |
| 275 | enum class DegradationPreference { |
| 276 | // Don't take any actions based on over-utilization signals. |
| 277 | kDegradationDisabled, |
| 278 | // On over-use, request lower frame rate, possibly causing frame drops. |
| 279 | kMaintainResolution, |
| 280 | // On over-use, request lower resolution, possibly causing down-scaling. |
| 281 | kMaintainFramerate, |
| 282 | // Try to strike a "pleasing" balance between frame rate or resolution. |
| 283 | kBalanced, |
| 284 | }; |
| 285 | |
| 286 | virtual void SetSource( |
| 287 | rtc::VideoSourceInterface<webrtc::VideoFrame>* source, |
| 288 | const DegradationPreference& degradation_preference) = 0; |
| 289 | |
| 290 | // Set which streams to send. Must have at least as many SSRCs as configured |
| 291 | // in the config. Encoder settings are passed on to the encoder instance along |
| 292 | // with the VideoStream settings. |
| 293 | virtual void ReconfigureVideoEncoder(VideoEncoderConfig config) = 0; |
| 294 | |
| 295 | virtual Stats GetStats() = 0; |
| 296 | |
| 297 | // Takes ownership of each file, is responsible for closing them later. |
| 298 | // Calling this method will close and finalize any current logs. |
| 299 | // Some codecs produce multiple streams (VP8 only at present), each of these |
| 300 | // streams will log to a separate file. kMaxSimulcastStreams in common_types.h |
| 301 | // gives the max number of such streams. If there is no file for a stream, or |
| 302 | // the file is rtc::kInvalidPlatformFileValue, frames from that stream will |
| 303 | // not be logged. |
| 304 | // If a frame to be written would make the log too large the write fails and |
| 305 | // the log is closed and finalized. A |byte_limit| of 0 means no limit. |
| 306 | virtual void EnableEncodedFrameRecording( |
| 307 | const std::vector<rtc::PlatformFile>& files, |
| 308 | size_t byte_limit) = 0; |
| 309 | inline void DisableEncodedFrameRecording() { |
| 310 | EnableEncodedFrameRecording(std::vector<rtc::PlatformFile>(), 0); |
| 311 | } |
| 312 | |
| 313 | protected: |
| 314 | virtual ~VideoSendStream() {} |
| 315 | }; |
| 316 | |
| 317 | } // namespace webrtc |
| 318 | |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 319 | #endif // CALL_VIDEO_SEND_STREAM_H_ |