blob: b4fac51a0422528e05f4c6b75f5ec9f2ee7bf096 [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
14#include <map>
15#include <string>
16#include <utility>
17#include <vector>
18
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "api/call/transport.h"
20#include "api/rtpparameters.h"
Patrik Höglund3e113432017-12-15 14:40:10 +010021#include "api/rtp_headers.h"
Patrik Höglundbe214a22018-01-04 12:14:35 +010022#include "api/videosinkinterface.h"
Patrik Höglund9e194032018-01-04 15:58:20 +010023#include "api/videosourceinterface.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "call/rtp_config.h"
25#include "call/video_config.h"
Mirko Bonadei71207422017-09-15 13:58:09 +020026#include "common_types.h" // NOLINT(build/include)
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020027#include "common_video/include/frame_callback.h"
Patrik Höglund3e113432017-12-15 14:40:10 +010028#include "modules/rtp_rtcp/include/rtp_rtcp_defines.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020029#include "rtc_base/platform_file.h"
aleloi440b6d92017-08-22 05:43:23 -070030
31namespace webrtc {
32
33class VideoEncoder;
34
35class VideoSendStream {
36 public:
37 struct StreamStats {
38 StreamStats();
39 ~StreamStats();
40
41 std::string ToString() const;
42
43 FrameCounts frame_counts;
44 bool is_rtx = false;
45 bool is_flexfec = false;
46 int width = 0;
47 int height = 0;
48 // TODO(holmer): Move bitrate_bps out to the webrtc::Call layer.
49 int total_bitrate_bps = 0;
50 int retransmit_bitrate_bps = 0;
51 int avg_delay_ms = 0;
52 int max_delay_ms = 0;
53 StreamDataCounters rtp_stats;
54 RtcpPacketTypeCounter rtcp_packet_type_counts;
55 RtcpStatistics rtcp_stats;
56 };
57
58 struct Stats {
59 Stats();
60 ~Stats();
61 std::string ToString(int64_t time_ms) const;
62 std::string encoder_implementation_name = "unknown";
63 int input_frame_rate = 0;
64 int encode_frame_rate = 0;
65 int avg_encode_time_ms = 0;
66 int encode_usage_percent = 0;
67 uint32_t frames_encoded = 0;
Ilya Nikolaevskiyd79314f2017-10-23 10:45:37 +020068 uint32_t frames_dropped_by_capturer = 0;
69 uint32_t frames_dropped_by_encoder_queue = 0;
70 uint32_t frames_dropped_by_rate_limiter = 0;
71 uint32_t frames_dropped_by_encoder = 0;
aleloi440b6d92017-08-22 05:43:23 -070072 rtc::Optional<uint64_t> qp_sum;
73 // Bitrate the encoder is currently configured to use due to bandwidth
74 // limitations.
75 int target_media_bitrate_bps = 0;
76 // Bitrate the encoder is actually producing.
77 int media_bitrate_bps = 0;
78 // Media bitrate this VideoSendStream is configured to prefer if there are
79 // no bandwidth limitations.
80 int preferred_media_bitrate_bps = 0;
81 bool suspended = false;
82 bool bw_limited_resolution = false;
83 bool cpu_limited_resolution = false;
84 bool bw_limited_framerate = false;
85 bool cpu_limited_framerate = false;
86 // Total number of times resolution as been requested to be changed due to
87 // CPU/quality adaptation.
88 int number_of_cpu_adapt_changes = 0;
89 int number_of_quality_adapt_changes = 0;
Åsa Perssonc3ed6302017-11-16 14:04:52 +010090 bool has_entered_low_resolution = false;
aleloi440b6d92017-08-22 05:43:23 -070091 std::map<uint32_t, StreamStats> substreams;
ilnik50864a82017-09-06 12:32:35 -070092 webrtc::VideoContentType content_type =
93 webrtc::VideoContentType::UNSPECIFIED;
Ilya Nikolaevskiy70473fc2018-02-28 16:35:03 +010094 uint32_t huge_frames_sent = 0;
aleloi440b6d92017-08-22 05:43:23 -070095 };
96
97 struct Config {
98 public:
99 Config() = delete;
100 Config(Config&&);
101 explicit Config(Transport* send_transport);
102
103 Config& operator=(Config&&);
104 Config& operator=(const Config&) = delete;
105
106 ~Config();
107
108 // Mostly used by tests. Avoid creating copies if you can.
109 Config Copy() const { return Config(*this); }
110
111 std::string ToString() const;
112
113 struct EncoderSettings {
114 EncoderSettings() = default;
Niels Möller04dd1762018-03-23 16:05:22 +0100115 explicit EncoderSettings(VideoEncoder* encoder) : encoder(encoder) {}
aleloi440b6d92017-08-22 05:43:23 -0700116 std::string ToString() const;
117
aleloi440b6d92017-08-22 05:43:23 -0700118 // 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
Niels Möller6539f692018-01-18 08:58:50 +0100127 // Enables the new method to estimate the cpu load from encoding, used for
128 // cpu adaptation.
129 bool experiment_cpu_load_estimator = false;
130
aleloi440b6d92017-08-22 05:43:23 -0700131 // 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
Niels Möller12d6a492018-03-22 12:41:48 +0100154 // TODO(nisse): For now, these are fixed, but we'd like to support
155 // changing codec without recreating the VideoSendStream. Then these
156 // fields must be removed, and association between payload type and codec
157 // must move above the per-stream level. Ownership could be with
158 // RtpTransportControllerSend, with a reference from PayloadRouter, where
159 // the latter would be responsible for mapping the codec type of encoded
160 // images to the right payload type.
161 std::string payload_name;
162 int payload_type = -1;
163
aleloi440b6d92017-08-22 05:43:23 -0700164 // See NackConfig for description.
165 NackConfig nack;
166
167 // See UlpfecConfig for description.
168 UlpfecConfig ulpfec;
169
170 struct Flexfec {
171 Flexfec();
172 Flexfec(const Flexfec&);
173 ~Flexfec();
174 // Payload type of FlexFEC. Set to -1 to disable sending FlexFEC.
175 int payload_type = -1;
176
177 // SSRC of FlexFEC stream.
178 uint32_t ssrc = 0;
179
180 // Vector containing a single element, corresponding to the SSRC of the
181 // media stream being protected by this FlexFEC stream.
182 // The vector MUST have size 1.
183 //
184 // TODO(brandtr): Update comment above when we support
185 // multistream protection.
186 std::vector<uint32_t> protected_media_ssrcs;
187 } flexfec;
188
189 // Settings for RTP retransmission payload format, see RFC 4588 for
190 // details.
191 struct Rtx {
192 Rtx();
193 Rtx(const Rtx&);
194 ~Rtx();
195 std::string ToString() const;
196 // SSRCs to use for the RTX streams.
197 std::vector<uint32_t> ssrcs;
198
199 // Payload type to use for the RTX stream.
200 int payload_type = -1;
201 } rtx;
202
203 // RTCP CNAME, see RFC 3550.
204 std::string c_name;
205 } rtp;
206
Jiawei Ou3587b832018-01-31 22:08:26 -0800207 struct Rtcp {
208 Rtcp();
209 Rtcp(const Rtcp&);
210 ~Rtcp();
211 std::string ToString() const;
212
213 // Time interval between RTCP report for video
214 int64_t video_report_interval_ms = 1000;
215 // Time interval between RTCP report for audio
216 int64_t audio_report_interval_ms = 5000;
217 } rtcp;
218
aleloi440b6d92017-08-22 05:43:23 -0700219 // Transport for outgoing packets.
220 Transport* send_transport = nullptr;
221
222 // Called for each I420 frame before encoding the frame. Can be used for
223 // effects, snapshots etc. 'nullptr' disables the callback.
224 rtc::VideoSinkInterface<VideoFrame>* pre_encode_callback = nullptr;
225
226 // Called for each encoded frame, e.g. used for file storage. 'nullptr'
227 // disables the callback. Also measures timing and passes the time
228 // spent on encoding. This timing will not fire if encoding takes longer
229 // than the measuring window, since the sample data will have been dropped.
230 EncodedFrameObserver* post_encode_callback = nullptr;
231
232 // Expected delay needed by the renderer, i.e. the frame will be delivered
233 // this many milliseconds, if possible, earlier than expected render time.
234 // Only valid if |local_renderer| is set.
235 int render_delay_ms = 0;
236
237 // Target delay in milliseconds. A positive value indicates this stream is
238 // used for streaming instead of a real-time call.
239 int target_delay_ms = 0;
240
241 // True if the stream should be suspended when the available bitrate fall
242 // below the minimum configured bitrate. If this variable is false, the
243 // stream may send at a rate higher than the estimated available bitrate.
244 bool suspend_below_min_bitrate = false;
245
246 // Enables periodic bandwidth probing in application-limited region.
247 bool periodic_alr_bandwidth_probing = false;
248
Alex Narestb3944f02017-10-13 14:56:18 +0200249 // Track ID as specified during track creation.
250 std::string track_id;
251
aleloi440b6d92017-08-22 05:43:23 -0700252 private:
253 // Access to the copy constructor is private to force use of the Copy()
254 // method for those exceptional cases where we do use it.
255 Config(const Config&);
256 };
257
Seth Hampsoncc7125f2018-02-02 08:46:16 -0800258 // Updates the sending state for all simulcast layers that the video send
259 // stream owns. This can mean updating the activity one or for multiple
260 // layers. The ordering of active layers is the order in which the
261 // rtp modules are stored in the VideoSendStream.
262 // Note: This starts stream activity if it is inactive and one of the layers
263 // is active. This stops stream activity if it is active and all layers are
264 // inactive.
265 virtual void UpdateActiveSimulcastLayers(
266 const std::vector<bool> active_layers) = 0;
267
aleloi440b6d92017-08-22 05:43:23 -0700268 // Starts stream activity.
269 // When a stream is active, it can receive, process and deliver packets.
270 virtual void Start() = 0;
271 // Stops stream activity.
272 // When a stream is stopped, it can't receive, process or deliver packets.
273 virtual void Stop() = 0;
274
275 // Based on the spec in
276 // https://w3c.github.io/webrtc-pc/#idl-def-rtcdegradationpreference.
277 // These options are enforced on a best-effort basis. For instance, all of
278 // these options may suffer some frame drops in order to avoid queuing.
279 // TODO(sprang): Look into possibility of more strictly enforcing the
280 // maintain-framerate option.
281 enum class DegradationPreference {
282 // Don't take any actions based on over-utilization signals.
283 kDegradationDisabled,
284 // On over-use, request lower frame rate, possibly causing frame drops.
285 kMaintainResolution,
286 // On over-use, request lower resolution, possibly causing down-scaling.
287 kMaintainFramerate,
288 // Try to strike a "pleasing" balance between frame rate or resolution.
289 kBalanced,
290 };
291
292 virtual void SetSource(
293 rtc::VideoSourceInterface<webrtc::VideoFrame>* source,
294 const DegradationPreference& degradation_preference) = 0;
295
296 // Set which streams to send. Must have at least as many SSRCs as configured
297 // in the config. Encoder settings are passed on to the encoder instance along
298 // with the VideoStream settings.
299 virtual void ReconfigureVideoEncoder(VideoEncoderConfig config) = 0;
300
301 virtual Stats GetStats() = 0;
302
303 // Takes ownership of each file, is responsible for closing them later.
304 // Calling this method will close and finalize any current logs.
305 // Some codecs produce multiple streams (VP8 only at present), each of these
306 // streams will log to a separate file. kMaxSimulcastStreams in common_types.h
307 // gives the max number of such streams. If there is no file for a stream, or
308 // the file is rtc::kInvalidPlatformFileValue, frames from that stream will
309 // not be logged.
310 // If a frame to be written would make the log too large the write fails and
311 // the log is closed and finalized. A |byte_limit| of 0 means no limit.
312 virtual void EnableEncodedFrameRecording(
313 const std::vector<rtc::PlatformFile>& files,
314 size_t byte_limit) = 0;
315 inline void DisableEncodedFrameRecording() {
316 EnableEncodedFrameRecording(std::vector<rtc::PlatformFile>(), 0);
317 }
318
319 protected:
320 virtual ~VideoSendStream() {}
321};
322
323} // namespace webrtc
324
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200325#endif // CALL_VIDEO_SEND_STREAM_H_