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 CALL_VIDEO_SEND_STREAM_H_
12 #define CALL_VIDEO_SEND_STREAM_H_
13 
14 #include <stdint.h>
15 
16 #include <map>
17 #include <string>
18 #include <vector>
19 
20 #include "absl/types/optional.h"
21 #include "api/adaptation/resource.h"
22 #include "api/call/transport.h"
23 #include "api/crypto/crypto_options.h"
24 #include "api/frame_transformer_interface.h"
25 #include "api/rtp_parameters.h"
26 #include "api/scoped_refptr.h"
27 #include "api/video/video_content_type.h"
28 #include "api/video/video_frame.h"
29 #include "api/video/video_sink_interface.h"
30 #include "api/video/video_source_interface.h"
31 #include "api/video/video_stream_encoder_settings.h"
32 #include "api/video_codecs/video_encoder_config.h"
33 #include "call/rtp_config.h"
34 #include "common_video/include/quality_limitation_reason.h"
35 #include "modules/rtp_rtcp/include/report_block_data.h"
36 #include "modules/rtp_rtcp/include/rtcp_statistics.h"
37 #include "modules/rtp_rtcp/include/rtp_rtcp_defines.h"
38 
39 namespace webrtc {
40 
41 class FrameEncryptorInterface;
42 
43 class VideoSendStream {
44  public:
45   // Multiple StreamStats objects are present if simulcast is used (multiple
46   // kMedia streams) or if RTX or FlexFEC is negotiated. Multiple SVC layers, on
47   // the other hand, does not cause additional StreamStats.
48   struct StreamStats {
49     enum class StreamType {
50       // A media stream is an RTP stream for audio or video. Retransmissions and
51       // FEC is either sent over the same SSRC or negotiated to be sent over
52       // separate SSRCs, in which case separate StreamStats objects exist with
53       // references to this media stream's SSRC.
54       kMedia,
55       // RTX streams are streams dedicated to retransmissions. They have a
56       // dependency on a single kMedia stream: |referenced_media_ssrc|.
57       kRtx,
58       // FlexFEC streams are streams dedicated to FlexFEC. They have a
59       // dependency on a single kMedia stream: |referenced_media_ssrc|.
60       kFlexfec,
61     };
62 
63     StreamStats();
64     ~StreamStats();
65 
66     std::string ToString() const;
67 
68     StreamType type = StreamType::kMedia;
69     // If |type| is kRtx or kFlexfec this value is present. The referenced SSRC
70     // is the kMedia stream that this stream is performing retransmissions or
71     // FEC for. If |type| is kMedia, this value is null.
72     absl::optional<uint32_t> referenced_media_ssrc;
73     FrameCounts frame_counts;
74     int width = 0;
75     int height = 0;
76     // TODO(holmer): Move bitrate_bps out to the webrtc::Call layer.
77     int total_bitrate_bps = 0;
78     int retransmit_bitrate_bps = 0;
79     int avg_delay_ms = 0;
80     int max_delay_ms = 0;
81     uint64_t total_packet_send_delay_ms = 0;
82     StreamDataCounters rtp_stats;
83     RtcpPacketTypeCounter rtcp_packet_type_counts;
84     RtcpStatistics rtcp_stats;
85     // A snapshot of the most recent Report Block with additional data of
86     // interest to statistics. Used to implement RTCRemoteInboundRtpStreamStats.
87     absl::optional<ReportBlockData> report_block_data;
88     double encode_frame_rate = 0.0;
89     int frames_encoded = 0;
90     absl::optional<uint64_t> qp_sum;
91     uint64_t total_encode_time_ms = 0;
92     uint64_t total_encoded_bytes_target = 0;
93     uint32_t huge_frames_sent = 0;
94   };
95 
96   struct Stats {
97     Stats();
98     ~Stats();
99     std::string ToString(int64_t time_ms) const;
100     std::string encoder_implementation_name = "unknown";
101     int input_frame_rate = 0;
102     int encode_frame_rate = 0;
103     int avg_encode_time_ms = 0;
104     int encode_usage_percent = 0;
105     uint32_t frames_encoded = 0;
106     // https://w3c.github.io/webrtc-stats/#dom-rtcoutboundrtpstreamstats-totalencodetime
107     uint64_t total_encode_time_ms = 0;
108     // https://w3c.github.io/webrtc-stats/#dom-rtcoutboundrtpstreamstats-totalencodedbytestarget
109     uint64_t total_encoded_bytes_target = 0;
110     uint32_t frames_dropped_by_capturer = 0;
111     uint32_t frames_dropped_by_encoder_queue = 0;
112     uint32_t frames_dropped_by_rate_limiter = 0;
113     uint32_t frames_dropped_by_congestion_window = 0;
114     uint32_t frames_dropped_by_encoder = 0;
115     // Bitrate the encoder is currently configured to use due to bandwidth
116     // limitations.
117     int target_media_bitrate_bps = 0;
118     // Bitrate the encoder is actually producing.
119     int media_bitrate_bps = 0;
120     bool suspended = false;
121     bool bw_limited_resolution = false;
122     bool cpu_limited_resolution = false;
123     bool bw_limited_framerate = false;
124     bool cpu_limited_framerate = false;
125     // https://w3c.github.io/webrtc-stats/#dom-rtcoutboundrtpstreamstats-qualitylimitationreason
126     QualityLimitationReason quality_limitation_reason =
127         QualityLimitationReason::kNone;
128     // https://w3c.github.io/webrtc-stats/#dom-rtcoutboundrtpstreamstats-qualitylimitationdurations
129     std::map<QualityLimitationReason, int64_t> quality_limitation_durations_ms;
130     // https://w3c.github.io/webrtc-stats/#dom-rtcoutboundrtpstreamstats-qualitylimitationresolutionchanges
131     uint32_t quality_limitation_resolution_changes = 0;
132     // Total number of times resolution as been requested to be changed due to
133     // CPU/quality adaptation.
134     int number_of_cpu_adapt_changes = 0;
135     int number_of_quality_adapt_changes = 0;
136     bool has_entered_low_resolution = false;
137     std::map<uint32_t, StreamStats> substreams;
138     webrtc::VideoContentType content_type =
139         webrtc::VideoContentType::UNSPECIFIED;
140     uint32_t frames_sent = 0;
141     uint32_t huge_frames_sent = 0;
142   };
143 
144   struct Config {
145    public:
146     Config() = delete;
147     Config(Config&&);
148     explicit Config(Transport* send_transport);
149 
150     Config& operator=(Config&&);
151     Config& operator=(const Config&) = delete;
152 
153     ~Config();
154 
155     // Mostly used by tests.  Avoid creating copies if you can.
CopyConfig156     Config Copy() const { return Config(*this); }
157 
158     std::string ToString() const;
159 
160     RtpConfig rtp;
161 
162     VideoStreamEncoderSettings encoder_settings;
163 
164     // Time interval between RTCP report for video
165     int rtcp_report_interval_ms = 1000;
166 
167     // Transport for outgoing packets.
168     Transport* send_transport = nullptr;
169 
170     // Expected delay needed by the renderer, i.e. the frame will be delivered
171     // this many milliseconds, if possible, earlier than expected render time.
172     // Only valid if |local_renderer| is set.
173     int render_delay_ms = 0;
174 
175     // Target delay in milliseconds. A positive value indicates this stream is
176     // used for streaming instead of a real-time call.
177     int target_delay_ms = 0;
178 
179     // True if the stream should be suspended when the available bitrate fall
180     // below the minimum configured bitrate. If this variable is false, the
181     // stream may send at a rate higher than the estimated available bitrate.
182     bool suspend_below_min_bitrate = false;
183 
184     // Enables periodic bandwidth probing in application-limited region.
185     bool periodic_alr_bandwidth_probing = false;
186 
187     // An optional custom frame encryptor that allows the entire frame to be
188     // encrypted in whatever way the caller chooses. This is not required by
189     // default.
190     rtc::scoped_refptr<webrtc::FrameEncryptorInterface> frame_encryptor;
191 
192     // Per PeerConnection cryptography options.
193     CryptoOptions crypto_options;
194 
195     rtc::scoped_refptr<webrtc::FrameTransformerInterface> frame_transformer;
196 
197    private:
198     // Access to the copy constructor is private to force use of the Copy()
199     // method for those exceptional cases where we do use it.
200     Config(const Config&);
201   };
202 
203   // Updates the sending state for all simulcast layers that the video send
204   // stream owns. This can mean updating the activity one or for multiple
205   // layers. The ordering of active layers is the order in which the
206   // rtp modules are stored in the VideoSendStream.
207   // Note: This starts stream activity if it is inactive and one of the layers
208   // is active. This stops stream activity if it is active and all layers are
209   // inactive.
210   virtual void UpdateActiveSimulcastLayers(
211       const std::vector<bool> active_layers) = 0;
212 
213   // Starts stream activity.
214   // When a stream is active, it can receive, process and deliver packets.
215   virtual void Start() = 0;
216   // Stops stream activity.
217   // When a stream is stopped, it can't receive, process or deliver packets.
218   virtual void Stop() = 0;
219 
220   // If the resource is overusing, the VideoSendStream will try to reduce
221   // resolution or frame rate until no resource is overusing.
222   // TODO(https://crbug.com/webrtc/11565): When the ResourceAdaptationProcessor
223   // is moved to Call this method could be deleted altogether in favor of
224   // Call-level APIs only.
225   virtual void AddAdaptationResource(rtc::scoped_refptr<Resource> resource) = 0;
226   virtual std::vector<rtc::scoped_refptr<Resource>>
227   GetAdaptationResources() = 0;
228 
229   virtual void SetSource(
230       rtc::VideoSourceInterface<webrtc::VideoFrame>* source,
231       const DegradationPreference& degradation_preference) = 0;
232 
233   // Set which streams to send. Must have at least as many SSRCs as configured
234   // in the config. Encoder settings are passed on to the encoder instance along
235   // with the VideoStream settings.
236   virtual void ReconfigureVideoEncoder(VideoEncoderConfig config) = 0;
237 
238   virtual Stats GetStats() = 0;
239 
240  protected:
~VideoSendStream()241   virtual ~VideoSendStream() {}
242 };
243 
244 }  // namespace webrtc
245 
246 #endif  // CALL_VIDEO_SEND_STREAM_H_
247