aleloi | 440b6d9 | 2017-08-22 05:43:23 -0700 | [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 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef CALL_VIDEO_SEND_STREAM_H_ |
| 12 | #define CALL_VIDEO_SEND_STREAM_H_ |
aleloi | 440b6d9 | 2017-08-22 05:43:23 -0700 | [diff] [blame] | 13 | |
| 14 | #include <map> |
| 15 | #include <string> |
| 16 | #include <utility> |
| 17 | #include <vector> |
| 18 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 19 | #include "api/call/transport.h" |
| 20 | #include "api/rtpparameters.h" |
| 21 | #include "call/rtp_config.h" |
| 22 | #include "call/video_config.h" |
Mirko Bonadei | 7120742 | 2017-09-15 13:58:09 +0200 | [diff] [blame] | 23 | #include "common_types.h" // NOLINT(build/include) |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 24 | #include "common_video/include/frame_callback.h" |
| 25 | #include "media/base/videosinkinterface.h" |
| 26 | #include "media/base/videosourceinterface.h" |
| 27 | #include "rtc_base/platform_file.h" |
aleloi | 440b6d9 | 2017-08-22 05:43:23 -0700 | [diff] [blame] | 28 | |
| 29 | namespace webrtc { |
| 30 | |
| 31 | class VideoEncoder; |
| 32 | |
| 33 | class VideoSendStream { |
| 34 | public: |
| 35 | struct StreamStats { |
| 36 | StreamStats(); |
| 37 | ~StreamStats(); |
| 38 | |
| 39 | std::string ToString() const; |
| 40 | |
| 41 | FrameCounts frame_counts; |
| 42 | bool is_rtx = false; |
| 43 | bool is_flexfec = false; |
| 44 | int width = 0; |
| 45 | int height = 0; |
| 46 | // TODO(holmer): Move bitrate_bps out to the webrtc::Call layer. |
| 47 | int total_bitrate_bps = 0; |
| 48 | int retransmit_bitrate_bps = 0; |
| 49 | int avg_delay_ms = 0; |
| 50 | int max_delay_ms = 0; |
| 51 | StreamDataCounters rtp_stats; |
| 52 | RtcpPacketTypeCounter rtcp_packet_type_counts; |
| 53 | RtcpStatistics rtcp_stats; |
| 54 | }; |
| 55 | |
| 56 | struct Stats { |
| 57 | Stats(); |
| 58 | ~Stats(); |
| 59 | std::string ToString(int64_t time_ms) const; |
| 60 | std::string encoder_implementation_name = "unknown"; |
| 61 | int input_frame_rate = 0; |
| 62 | int encode_frame_rate = 0; |
| 63 | int avg_encode_time_ms = 0; |
| 64 | int encode_usage_percent = 0; |
| 65 | uint32_t frames_encoded = 0; |
Ilya Nikolaevskiy | d79314f | 2017-10-23 10:45:37 +0200 | [diff] [blame] | 66 | uint32_t frames_dropped_by_capturer = 0; |
| 67 | uint32_t frames_dropped_by_encoder_queue = 0; |
| 68 | uint32_t frames_dropped_by_rate_limiter = 0; |
| 69 | uint32_t frames_dropped_by_encoder = 0; |
aleloi | 440b6d9 | 2017-08-22 05:43:23 -0700 | [diff] [blame] | 70 | rtc::Optional<uint64_t> qp_sum; |
| 71 | // Bitrate the encoder is currently configured to use due to bandwidth |
| 72 | // limitations. |
| 73 | int target_media_bitrate_bps = 0; |
| 74 | // Bitrate the encoder is actually producing. |
| 75 | int media_bitrate_bps = 0; |
| 76 | // Media bitrate this VideoSendStream is configured to prefer if there are |
| 77 | // no bandwidth limitations. |
| 78 | int preferred_media_bitrate_bps = 0; |
| 79 | bool suspended = false; |
| 80 | bool bw_limited_resolution = false; |
| 81 | bool cpu_limited_resolution = false; |
| 82 | bool bw_limited_framerate = false; |
| 83 | bool cpu_limited_framerate = false; |
| 84 | // Total number of times resolution as been requested to be changed due to |
| 85 | // CPU/quality adaptation. |
| 86 | int number_of_cpu_adapt_changes = 0; |
| 87 | int number_of_quality_adapt_changes = 0; |
| 88 | std::map<uint32_t, StreamStats> substreams; |
ilnik | 50864a8 | 2017-09-06 12:32:35 -0700 | [diff] [blame] | 89 | webrtc::VideoContentType content_type = |
| 90 | webrtc::VideoContentType::UNSPECIFIED; |
aleloi | 440b6d9 | 2017-08-22 05:43:23 -0700 | [diff] [blame] | 91 | }; |
| 92 | |
| 93 | struct Config { |
| 94 | public: |
| 95 | Config() = delete; |
| 96 | Config(Config&&); |
| 97 | explicit Config(Transport* send_transport); |
| 98 | |
| 99 | Config& operator=(Config&&); |
| 100 | Config& operator=(const Config&) = delete; |
| 101 | |
| 102 | ~Config(); |
| 103 | |
| 104 | // Mostly used by tests. Avoid creating copies if you can. |
| 105 | Config Copy() const { return Config(*this); } |
| 106 | |
| 107 | std::string ToString() const; |
| 108 | |
| 109 | struct EncoderSettings { |
| 110 | EncoderSettings() = default; |
| 111 | EncoderSettings(std::string payload_name, |
| 112 | int payload_type, |
| 113 | VideoEncoder* encoder) |
| 114 | : payload_name(std::move(payload_name)), |
| 115 | payload_type(payload_type), |
| 116 | encoder(encoder) {} |
| 117 | std::string ToString() const; |
| 118 | |
| 119 | std::string payload_name; |
| 120 | int payload_type = -1; |
| 121 | |
| 122 | // TODO(sophiechang): Delete this field when no one is using internal |
| 123 | // sources anymore. |
| 124 | bool internal_source = false; |
| 125 | |
| 126 | // Allow 100% encoder utilization. Used for HW encoders where CPU isn't |
| 127 | // expected to be the limiting factor, but a chip could be running at |
| 128 | // 30fps (for example) exactly. |
| 129 | bool full_overuse_time = false; |
| 130 | |
| 131 | // Uninitialized VideoEncoder instance to be used for encoding. Will be |
| 132 | // initialized from inside the VideoSendStream. |
| 133 | VideoEncoder* encoder = nullptr; |
| 134 | } encoder_settings; |
| 135 | |
| 136 | static const size_t kDefaultMaxPacketSize = 1500 - 40; // TCP over IPv4. |
| 137 | struct Rtp { |
| 138 | Rtp(); |
| 139 | Rtp(const Rtp&); |
| 140 | ~Rtp(); |
| 141 | std::string ToString() const; |
| 142 | |
| 143 | std::vector<uint32_t> ssrcs; |
| 144 | |
| 145 | // See RtcpMode for description. |
| 146 | RtcpMode rtcp_mode = RtcpMode::kCompound; |
| 147 | |
| 148 | // Max RTP packet size delivered to send transport from VideoEngine. |
| 149 | size_t max_packet_size = kDefaultMaxPacketSize; |
| 150 | |
| 151 | // RTP header extensions to use for this send stream. |
| 152 | std::vector<RtpExtension> extensions; |
| 153 | |
| 154 | // See NackConfig for description. |
| 155 | NackConfig nack; |
| 156 | |
| 157 | // See UlpfecConfig for description. |
| 158 | UlpfecConfig ulpfec; |
| 159 | |
| 160 | struct Flexfec { |
| 161 | Flexfec(); |
| 162 | Flexfec(const Flexfec&); |
| 163 | ~Flexfec(); |
| 164 | // Payload type of FlexFEC. Set to -1 to disable sending FlexFEC. |
| 165 | int payload_type = -1; |
| 166 | |
| 167 | // SSRC of FlexFEC stream. |
| 168 | uint32_t ssrc = 0; |
| 169 | |
| 170 | // Vector containing a single element, corresponding to the SSRC of the |
| 171 | // media stream being protected by this FlexFEC stream. |
| 172 | // The vector MUST have size 1. |
| 173 | // |
| 174 | // TODO(brandtr): Update comment above when we support |
| 175 | // multistream protection. |
| 176 | std::vector<uint32_t> protected_media_ssrcs; |
| 177 | } flexfec; |
| 178 | |
| 179 | // Settings for RTP retransmission payload format, see RFC 4588 for |
| 180 | // details. |
| 181 | struct Rtx { |
| 182 | Rtx(); |
| 183 | Rtx(const Rtx&); |
| 184 | ~Rtx(); |
| 185 | std::string ToString() const; |
| 186 | // SSRCs to use for the RTX streams. |
| 187 | std::vector<uint32_t> ssrcs; |
| 188 | |
| 189 | // Payload type to use for the RTX stream. |
| 190 | int payload_type = -1; |
| 191 | } rtx; |
| 192 | |
| 193 | // RTCP CNAME, see RFC 3550. |
| 194 | std::string c_name; |
| 195 | } rtp; |
| 196 | |
| 197 | // Transport for outgoing packets. |
| 198 | Transport* send_transport = nullptr; |
| 199 | |
| 200 | // Called for each I420 frame before encoding the frame. Can be used for |
| 201 | // effects, snapshots etc. 'nullptr' disables the callback. |
| 202 | rtc::VideoSinkInterface<VideoFrame>* pre_encode_callback = nullptr; |
| 203 | |
| 204 | // Called for each encoded frame, e.g. used for file storage. 'nullptr' |
| 205 | // disables the callback. Also measures timing and passes the time |
| 206 | // spent on encoding. This timing will not fire if encoding takes longer |
| 207 | // than the measuring window, since the sample data will have been dropped. |
| 208 | EncodedFrameObserver* post_encode_callback = nullptr; |
| 209 | |
| 210 | // Expected delay needed by the renderer, i.e. the frame will be delivered |
| 211 | // this many milliseconds, if possible, earlier than expected render time. |
| 212 | // Only valid if |local_renderer| is set. |
| 213 | int render_delay_ms = 0; |
| 214 | |
| 215 | // Target delay in milliseconds. A positive value indicates this stream is |
| 216 | // used for streaming instead of a real-time call. |
| 217 | int target_delay_ms = 0; |
| 218 | |
| 219 | // True if the stream should be suspended when the available bitrate fall |
| 220 | // below the minimum configured bitrate. If this variable is false, the |
| 221 | // stream may send at a rate higher than the estimated available bitrate. |
| 222 | bool suspend_below_min_bitrate = false; |
| 223 | |
| 224 | // Enables periodic bandwidth probing in application-limited region. |
| 225 | bool periodic_alr_bandwidth_probing = false; |
| 226 | |
Alex Narest | b3944f0 | 2017-10-13 14:56:18 +0200 | [diff] [blame] | 227 | // Track ID as specified during track creation. |
| 228 | std::string track_id; |
| 229 | |
aleloi | 440b6d9 | 2017-08-22 05:43:23 -0700 | [diff] [blame] | 230 | private: |
| 231 | // Access to the copy constructor is private to force use of the Copy() |
| 232 | // method for those exceptional cases where we do use it. |
| 233 | Config(const Config&); |
| 234 | }; |
| 235 | |
| 236 | // Starts stream activity. |
| 237 | // When a stream is active, it can receive, process and deliver packets. |
| 238 | virtual void Start() = 0; |
| 239 | // Stops stream activity. |
| 240 | // When a stream is stopped, it can't receive, process or deliver packets. |
| 241 | virtual void Stop() = 0; |
| 242 | |
| 243 | // Based on the spec in |
| 244 | // https://w3c.github.io/webrtc-pc/#idl-def-rtcdegradationpreference. |
| 245 | // These options are enforced on a best-effort basis. For instance, all of |
| 246 | // these options may suffer some frame drops in order to avoid queuing. |
| 247 | // TODO(sprang): Look into possibility of more strictly enforcing the |
| 248 | // maintain-framerate option. |
| 249 | enum class DegradationPreference { |
| 250 | // Don't take any actions based on over-utilization signals. |
| 251 | kDegradationDisabled, |
| 252 | // On over-use, request lower frame rate, possibly causing frame drops. |
| 253 | kMaintainResolution, |
| 254 | // On over-use, request lower resolution, possibly causing down-scaling. |
| 255 | kMaintainFramerate, |
| 256 | // Try to strike a "pleasing" balance between frame rate or resolution. |
| 257 | kBalanced, |
| 258 | }; |
| 259 | |
| 260 | virtual void SetSource( |
| 261 | rtc::VideoSourceInterface<webrtc::VideoFrame>* source, |
| 262 | const DegradationPreference& degradation_preference) = 0; |
| 263 | |
| 264 | // Set which streams to send. Must have at least as many SSRCs as configured |
| 265 | // in the config. Encoder settings are passed on to the encoder instance along |
| 266 | // with the VideoStream settings. |
| 267 | virtual void ReconfigureVideoEncoder(VideoEncoderConfig config) = 0; |
| 268 | |
| 269 | virtual Stats GetStats() = 0; |
| 270 | |
| 271 | // Takes ownership of each file, is responsible for closing them later. |
| 272 | // Calling this method will close and finalize any current logs. |
| 273 | // Some codecs produce multiple streams (VP8 only at present), each of these |
| 274 | // streams will log to a separate file. kMaxSimulcastStreams in common_types.h |
| 275 | // gives the max number of such streams. If there is no file for a stream, or |
| 276 | // the file is rtc::kInvalidPlatformFileValue, frames from that stream will |
| 277 | // not be logged. |
| 278 | // If a frame to be written would make the log too large the write fails and |
| 279 | // the log is closed and finalized. A |byte_limit| of 0 means no limit. |
| 280 | virtual void EnableEncodedFrameRecording( |
| 281 | const std::vector<rtc::PlatformFile>& files, |
| 282 | size_t byte_limit) = 0; |
| 283 | inline void DisableEncodedFrameRecording() { |
| 284 | EnableEncodedFrameRecording(std::vector<rtc::PlatformFile>(), 0); |
| 285 | } |
| 286 | |
| 287 | protected: |
| 288 | virtual ~VideoSendStream() {} |
| 289 | }; |
| 290 | |
| 291 | } // namespace webrtc |
| 292 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 293 | #endif // CALL_VIDEO_SEND_STREAM_H_ |