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