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