blob: 356d8c8099a7dc2c9f6cbb0e2ee610c7263d1ee5 [file] [log] [blame]
aleloi440b6d92017-08-22 05:43:23 -07001/*
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 Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef CALL_VIDEO_SEND_STREAM_H_
12#define CALL_VIDEO_SEND_STREAM_H_
aleloi440b6d92017-08-22 05:43:23 -070013
Yves Gerey988cc082018-10-23 12:03:01 +020014#include <stdint.h>
Jonas Olssona4d87372019-07-05 19:08:33 +020015
aleloi440b6d92017-08-22 05:43:23 -070016#include <map>
17#include <string>
aleloi440b6d92017-08-22 05:43:23 -070018#include <vector>
19
Yves Gerey988cc082018-10-23 12:03:01 +020020#include "absl/types/optional.h"
Henrik Boströmf4a99912020-06-11 12:07:14 +020021#include "api/adaptation/resource.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "api/call/transport.h"
Steve Anton10542f22019-01-11 09:11:00 -080023#include "api/crypto/crypto_options.h"
Marina Cioceae77912b2020-02-27 16:16:55 +010024#include "api/frame_transformer_interface.h"
Steve Anton10542f22019-01-11 09:11:00 -080025#include "api/rtp_parameters.h"
Henrik Boströmf4a99912020-06-11 12:07:14 +020026#include "api/scoped_refptr.h"
Yves Gerey988cc082018-10-23 12:03:01 +020027#include "api/video/video_content_type.h"
Niels Möller88be9722018-10-10 10:58:52 +020028#include "api/video/video_frame.h"
Niels Möllerc6ce9c52018-05-11 11:15:30 +020029#include "api/video/video_sink_interface.h"
Niels Möller0327c2d2018-05-21 14:09:31 +020030#include "api/video/video_source_interface.h"
Niels Möller213618e2018-07-24 09:29:58 +020031#include "api/video/video_stream_encoder_settings.h"
Niels Möller0a8f4352018-05-18 11:37:23 +020032#include "api/video_codecs/video_encoder_config.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020033#include "call/rtp_config.h"
Niels Möllera8327d42020-08-25 10:28:50 +020034#include "common_video/frame_counts.h"
Henrik Boströmce33b6a2019-05-28 17:42:38 +020035#include "common_video/include/quality_limitation_reason.h"
Henrik Boström87e3f9d2019-05-27 10:44:24 +020036#include "modules/rtp_rtcp/include/report_block_data.h"
Niels Möller53382cb2018-11-27 14:05:08 +010037#include "modules/rtp_rtcp/include/rtcp_statistics.h"
Patrik Höglund3e113432017-12-15 14:40:10 +010038#include "modules/rtp_rtcp/include/rtp_rtcp_defines.h"
aleloi440b6d92017-08-22 05:43:23 -070039
40namespace webrtc {
41
Benjamin Wright192eeec2018-10-17 17:27:25 -070042class FrameEncryptorInterface;
43
aleloi440b6d92017-08-22 05:43:23 -070044class VideoSendStream {
45 public:
Henrik Boströmf45ca372020-03-24 13:30:50 +010046 // Multiple StreamStats objects are present if simulcast is used (multiple
47 // kMedia streams) or if RTX or FlexFEC is negotiated. Multiple SVC layers, on
48 // the other hand, does not cause additional StreamStats.
aleloi440b6d92017-08-22 05:43:23 -070049 struct StreamStats {
Henrik Boströmf45ca372020-03-24 13:30:50 +010050 enum class StreamType {
51 // A media stream is an RTP stream for audio or video. Retransmissions and
52 // FEC is either sent over the same SSRC or negotiated to be sent over
53 // separate SSRCs, in which case separate StreamStats objects exist with
54 // references to this media stream's SSRC.
55 kMedia,
56 // RTX streams are streams dedicated to retransmissions. They have a
Artem Titovea240272021-07-26 12:40:21 +020057 // dependency on a single kMedia stream: `referenced_media_ssrc`.
Henrik Boströmf45ca372020-03-24 13:30:50 +010058 kRtx,
59 // FlexFEC streams are streams dedicated to FlexFEC. They have a
Artem Titovea240272021-07-26 12:40:21 +020060 // dependency on a single kMedia stream: `referenced_media_ssrc`.
Henrik Boströmf45ca372020-03-24 13:30:50 +010061 kFlexfec,
62 };
63
aleloi440b6d92017-08-22 05:43:23 -070064 StreamStats();
65 ~StreamStats();
66
67 std::string ToString() const;
68
Henrik Boströmf45ca372020-03-24 13:30:50 +010069 StreamType type = StreamType::kMedia;
Artem Titovea240272021-07-26 12:40:21 +020070 // If `type` is kRtx or kFlexfec this value is present. The referenced SSRC
Henrik Boströmf45ca372020-03-24 13:30:50 +010071 // is the kMedia stream that this stream is performing retransmissions or
Artem Titovea240272021-07-26 12:40:21 +020072 // FEC for. If `type` is kMedia, this value is null.
Henrik Boströmf45ca372020-03-24 13:30:50 +010073 absl::optional<uint32_t> referenced_media_ssrc;
aleloi440b6d92017-08-22 05:43:23 -070074 FrameCounts frame_counts;
aleloi440b6d92017-08-22 05:43:23 -070075 int width = 0;
76 int height = 0;
77 // TODO(holmer): Move bitrate_bps out to the webrtc::Call layer.
78 int total_bitrate_bps = 0;
79 int retransmit_bitrate_bps = 0;
80 int avg_delay_ms = 0;
81 int max_delay_ms = 0;
Henrik Boström9fe18342019-05-16 18:38:20 +020082 uint64_t total_packet_send_delay_ms = 0;
aleloi440b6d92017-08-22 05:43:23 -070083 StreamDataCounters rtp_stats;
84 RtcpPacketTypeCounter rtcp_packet_type_counts;
Henrik Boström87e3f9d2019-05-27 10:44:24 +020085 // 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;
Henrik Boströma0ff50c2020-05-05 15:54:46 +020088 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;
aleloi440b6d92017-08-22 05:43:23 -070094 };
95
96 struct Stats {
97 Stats();
98 ~Stats();
99 std::string ToString(int64_t time_ms) const;
100 std::string encoder_implementation_name = "unknown";
Byoungchan Leeefe46b62021-11-10 11:23:56 +0900101 double input_frame_rate = 0;
aleloi440b6d92017-08-22 05:43:23 -0700102 int encode_frame_rate = 0;
103 int avg_encode_time_ms = 0;
104 int encode_usage_percent = 0;
105 uint32_t frames_encoded = 0;
Henrik Boström5684af52019-04-02 15:05:21 +0200106 // https://w3c.github.io/webrtc-stats/#dom-rtcoutboundrtpstreamstats-totalencodetime
107 uint64_t total_encode_time_ms = 0;
Henrik Boström23aff9b2019-05-20 15:15:38 +0200108 // https://w3c.github.io/webrtc-stats/#dom-rtcoutboundrtpstreamstats-totalencodedbytestarget
109 uint64_t total_encoded_bytes_target = 0;
Di Wu668dbf62021-02-27 00:29:15 -0800110 uint32_t frames = 0;
Ilya Nikolaevskiyd79314f2017-10-23 10:45:37 +0200111 uint32_t frames_dropped_by_capturer = 0;
112 uint32_t frames_dropped_by_encoder_queue = 0;
113 uint32_t frames_dropped_by_rate_limiter = 0;
Ying Wang9b881ab2020-02-07 14:29:32 +0100114 uint32_t frames_dropped_by_congestion_window = 0;
Ilya Nikolaevskiyd79314f2017-10-23 10:45:37 +0200115 uint32_t frames_dropped_by_encoder = 0;
aleloi440b6d92017-08-22 05:43:23 -0700116 // Bitrate the encoder is currently configured to use due to bandwidth
117 // limitations.
118 int target_media_bitrate_bps = 0;
119 // Bitrate the encoder is actually producing.
120 int media_bitrate_bps = 0;
aleloi440b6d92017-08-22 05:43:23 -0700121 bool suspended = false;
122 bool bw_limited_resolution = false;
123 bool cpu_limited_resolution = false;
124 bool bw_limited_framerate = false;
125 bool cpu_limited_framerate = false;
Henrik Boströmce33b6a2019-05-28 17:42:38 +0200126 // https://w3c.github.io/webrtc-stats/#dom-rtcoutboundrtpstreamstats-qualitylimitationreason
127 QualityLimitationReason quality_limitation_reason =
128 QualityLimitationReason::kNone;
129 // https://w3c.github.io/webrtc-stats/#dom-rtcoutboundrtpstreamstats-qualitylimitationdurations
130 std::map<QualityLimitationReason, int64_t> quality_limitation_durations_ms;
Evan Shrubsolecc62b162019-09-09 11:26:45 +0200131 // https://w3c.github.io/webrtc-stats/#dom-rtcoutboundrtpstreamstats-qualitylimitationresolutionchanges
132 uint32_t quality_limitation_resolution_changes = 0;
aleloi440b6d92017-08-22 05:43:23 -0700133 // Total number of times resolution as been requested to be changed due to
134 // CPU/quality adaptation.
135 int number_of_cpu_adapt_changes = 0;
136 int number_of_quality_adapt_changes = 0;
Åsa Perssonc3ed6302017-11-16 14:04:52 +0100137 bool has_entered_low_resolution = false;
aleloi440b6d92017-08-22 05:43:23 -0700138 std::map<uint32_t, StreamStats> substreams;
ilnik50864a82017-09-06 12:32:35 -0700139 webrtc::VideoContentType content_type =
140 webrtc::VideoContentType::UNSPECIFIED;
Henrik Boströma0ff50c2020-05-05 15:54:46 +0200141 uint32_t frames_sent = 0;
Ilya Nikolaevskiy70473fc2018-02-28 16:35:03 +0100142 uint32_t huge_frames_sent = 0;
aleloi440b6d92017-08-22 05:43:23 -0700143 };
144
145 struct Config {
146 public:
147 Config() = delete;
148 Config(Config&&);
149 explicit Config(Transport* send_transport);
150
151 Config& operator=(Config&&);
152 Config& operator=(const Config&) = delete;
153
154 ~Config();
155
156 // Mostly used by tests. Avoid creating copies if you can.
157 Config Copy() const { return Config(*this); }
158
159 std::string ToString() const;
160
Philip Eliasson49d661a2019-06-11 11:55:47 +0000161 RtpConfig rtp;
162
Elad Alon370f93a2019-06-11 14:57:57 +0200163 VideoStreamEncoderSettings encoder_settings;
164
Jiawei Ou55718122018-11-09 13:17:39 -0800165 // Time interval between RTCP report for video
166 int rtcp_report_interval_ms = 1000;
Jiawei Ou3587b832018-01-31 22:08:26 -0800167
aleloi440b6d92017-08-22 05:43:23 -0700168 // Transport for outgoing packets.
169 Transport* send_transport = nullptr;
170
aleloi440b6d92017-08-22 05:43:23 -0700171 // Expected delay needed by the renderer, i.e. the frame will be delivered
172 // this many milliseconds, if possible, earlier than expected render time.
Artem Titovea240272021-07-26 12:40:21 +0200173 // Only valid if `local_renderer` is set.
aleloi440b6d92017-08-22 05:43:23 -0700174 int render_delay_ms = 0;
175
176 // Target delay in milliseconds. A positive value indicates this stream is
177 // used for streaming instead of a real-time call.
178 int target_delay_ms = 0;
179
180 // True if the stream should be suspended when the available bitrate fall
181 // below the minimum configured bitrate. If this variable is false, the
182 // stream may send at a rate higher than the estimated available bitrate.
183 bool suspend_below_min_bitrate = false;
184
185 // Enables periodic bandwidth probing in application-limited region.
186 bool periodic_alr_bandwidth_probing = false;
187
Benjamin Wright192eeec2018-10-17 17:27:25 -0700188 // An optional custom frame encryptor that allows the entire frame to be
189 // encrypted in whatever way the caller chooses. This is not required by
190 // default.
191 rtc::scoped_refptr<webrtc::FrameEncryptorInterface> frame_encryptor;
192
193 // Per PeerConnection cryptography options.
194 CryptoOptions crypto_options;
195
Marina Cioceae77912b2020-02-27 16:16:55 +0100196 rtc::scoped_refptr<webrtc::FrameTransformerInterface> frame_transformer;
197
aleloi440b6d92017-08-22 05:43:23 -0700198 private:
199 // Access to the copy constructor is private to force use of the Copy()
200 // method for those exceptional cases where we do use it.
201 Config(const Config&);
202 };
203
Seth Hampsoncc7125f2018-02-02 08:46:16 -0800204 // Updates the sending state for all simulcast layers that the video send
205 // stream owns. This can mean updating the activity one or for multiple
206 // layers. The ordering of active layers is the order in which the
207 // rtp modules are stored in the VideoSendStream.
208 // Note: This starts stream activity if it is inactive and one of the layers
209 // is active. This stops stream activity if it is active and all layers are
210 // inactive.
Ali Tofigh1e157a92022-01-31 11:08:24 +0100211 virtual void UpdateActiveSimulcastLayers(std::vector<bool> active_layers) = 0;
Seth Hampsoncc7125f2018-02-02 08:46:16 -0800212
aleloi440b6d92017-08-22 05:43:23 -0700213 // 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
Tommi35b1cb42021-08-05 10:50:26 +0200220 // Accessor for determining if the stream is active. This is an inexpensive
221 // call that must be made on the same thread as `Start()` and `Stop()` methods
222 // are called on and will return `true` iff activity has been started either
223 // via `Start()` or `UpdateActiveSimulcastLayers()`. If activity is either
224 // stopped or is in the process of being stopped as a result of a call to
225 // either `Stop()` or `UpdateActiveSimulcastLayers()` where all layers were
226 // deactivated, the return value will be `false`.
227 virtual bool started() = 0;
228
Henrik Boströmf4a99912020-06-11 12:07:14 +0200229 // If the resource is overusing, the VideoSendStream will try to reduce
230 // resolution or frame rate until no resource is overusing.
231 // TODO(https://crbug.com/webrtc/11565): When the ResourceAdaptationProcessor
232 // is moved to Call this method could be deleted altogether in favor of
233 // Call-level APIs only.
234 virtual void AddAdaptationResource(rtc::scoped_refptr<Resource> resource) = 0;
235 virtual std::vector<rtc::scoped_refptr<Resource>>
236 GetAdaptationResources() = 0;
237
aleloi440b6d92017-08-22 05:43:23 -0700238 virtual void SetSource(
239 rtc::VideoSourceInterface<webrtc::VideoFrame>* source,
240 const DegradationPreference& degradation_preference) = 0;
241
242 // Set which streams to send. Must have at least as many SSRCs as configured
243 // in the config. Encoder settings are passed on to the encoder instance along
244 // with the VideoStream settings.
245 virtual void ReconfigureVideoEncoder(VideoEncoderConfig config) = 0;
246
247 virtual Stats GetStats() = 0;
248
aleloi440b6d92017-08-22 05:43:23 -0700249 protected:
250 virtual ~VideoSendStream() {}
251};
252
253} // namespace webrtc
254
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200255#endif // CALL_VIDEO_SEND_STREAM_H_