blob: b5bd199410b737c12807efa907d10454f4517ca7 [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"
Patrik Höglund3e113432017-12-15 14:40:10 +010020#include "api/rtp_headers.h"
Niels Möller0327c2d2018-05-21 14:09:31 +020021#include "api/rtpparameters.h"
Niels Möllerc6ce9c52018-05-11 11:15:30 +020022#include "api/video/video_sink_interface.h"
Niels Möller0327c2d2018-05-21 14:09:31 +020023#include "api/video/video_source_interface.h"
Niels Möller0a8f4352018-05-18 11:37:23 +020024#include "api/video_codecs/video_encoder_config.h"
Niels Möller88614b02018-03-27 16:39:01 +020025#include "api/video_codecs/video_encoder_factory.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020026#include "call/rtp_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
aleloi440b6d92017-08-22 05:43:23 -070034class VideoSendStream {
35 public:
36 struct StreamStats {
37 StreamStats();
38 ~StreamStats();
39
40 std::string ToString() const;
41
42 FrameCounts frame_counts;
43 bool is_rtx = false;
44 bool is_flexfec = false;
45 int width = 0;
46 int height = 0;
47 // TODO(holmer): Move bitrate_bps out to the webrtc::Call layer.
48 int total_bitrate_bps = 0;
49 int retransmit_bitrate_bps = 0;
50 int avg_delay_ms = 0;
51 int max_delay_ms = 0;
52 StreamDataCounters rtp_stats;
53 RtcpPacketTypeCounter rtcp_packet_type_counts;
54 RtcpStatistics rtcp_stats;
55 };
56
57 struct Stats {
58 Stats();
59 ~Stats();
60 std::string ToString(int64_t time_ms) const;
61 std::string encoder_implementation_name = "unknown";
62 int input_frame_rate = 0;
63 int encode_frame_rate = 0;
64 int avg_encode_time_ms = 0;
65 int encode_usage_percent = 0;
66 uint32_t frames_encoded = 0;
Ilya Nikolaevskiyd79314f2017-10-23 10:45:37 +020067 uint32_t frames_dropped_by_capturer = 0;
68 uint32_t frames_dropped_by_encoder_queue = 0;
69 uint32_t frames_dropped_by_rate_limiter = 0;
70 uint32_t frames_dropped_by_encoder = 0;
Danil Chapovalovb9b146c2018-06-15 12:28:07 +020071 absl::optional<uint64_t> qp_sum;
aleloi440b6d92017-08-22 05:43:23 -070072 // Bitrate the encoder is currently configured to use due to bandwidth
73 // limitations.
74 int target_media_bitrate_bps = 0;
75 // Bitrate the encoder is actually producing.
76 int media_bitrate_bps = 0;
aleloi440b6d92017-08-22 05:43:23 -070077 bool suspended = false;
78 bool bw_limited_resolution = false;
79 bool cpu_limited_resolution = false;
80 bool bw_limited_framerate = false;
81 bool cpu_limited_framerate = false;
82 // Total number of times resolution as been requested to be changed due to
83 // CPU/quality adaptation.
84 int number_of_cpu_adapt_changes = 0;
85 int number_of_quality_adapt_changes = 0;
Åsa Perssonc3ed6302017-11-16 14:04:52 +010086 bool has_entered_low_resolution = false;
aleloi440b6d92017-08-22 05:43:23 -070087 std::map<uint32_t, StreamStats> substreams;
ilnik50864a82017-09-06 12:32:35 -070088 webrtc::VideoContentType content_type =
89 webrtc::VideoContentType::UNSPECIFIED;
Ilya Nikolaevskiy70473fc2018-02-28 16:35:03 +010090 uint32_t huge_frames_sent = 0;
aleloi440b6d92017-08-22 05:43:23 -070091 };
92
93 struct Config {
94 public:
95 Config() = delete;
96 Config(Config&&);
97 explicit Config(Transport* send_transport);
98
99 Config& operator=(Config&&);
100 Config& operator=(const Config&) = delete;
101
102 ~Config();
103
104 // Mostly used by tests. Avoid creating copies if you can.
105 Config Copy() const { return Config(*this); }
106
107 std::string ToString() const;
108
109 struct EncoderSettings {
110 EncoderSettings() = default;
aleloi440b6d92017-08-22 05:43:23 -0700111 std::string ToString() const;
112
Niels Möller6539f692018-01-18 08:58:50 +0100113 // Enables the new method to estimate the cpu load from encoding, used for
114 // cpu adaptation.
115 bool experiment_cpu_load_estimator = false;
116
Niels Möller88614b02018-03-27 16:39:01 +0200117 // Ownership stays with WebrtcVideoEngine (delegated from PeerConnection).
118 VideoEncoderFactory* encoder_factory = nullptr;
aleloi440b6d92017-08-22 05:43:23 -0700119 } encoder_settings;
120
121 static const size_t kDefaultMaxPacketSize = 1500 - 40; // TCP over IPv4.
122 struct Rtp {
123 Rtp();
124 Rtp(const Rtp&);
125 ~Rtp();
126 std::string ToString() const;
127
128 std::vector<uint32_t> ssrcs;
129
Steve Antonbb50ce52018-03-26 10:24:32 -0700130 // The value to send in the MID RTP header extension if the extension is
131 // included in the list of extensions.
132 std::string mid;
133
aleloi440b6d92017-08-22 05:43:23 -0700134 // See RtcpMode for description.
135 RtcpMode rtcp_mode = RtcpMode::kCompound;
136
137 // Max RTP packet size delivered to send transport from VideoEngine.
138 size_t max_packet_size = kDefaultMaxPacketSize;
139
140 // RTP header extensions to use for this send stream.
141 std::vector<RtpExtension> extensions;
142
Niels Möller12d6a492018-03-22 12:41:48 +0100143 // TODO(nisse): For now, these are fixed, but we'd like to support
144 // changing codec without recreating the VideoSendStream. Then these
145 // fields must be removed, and association between payload type and codec
146 // must move above the per-stream level. Ownership could be with
147 // RtpTransportControllerSend, with a reference from PayloadRouter, where
148 // the latter would be responsible for mapping the codec type of encoded
149 // images to the right payload type.
150 std::string payload_name;
151 int payload_type = -1;
152
aleloi440b6d92017-08-22 05:43:23 -0700153 // See NackConfig for description.
154 NackConfig nack;
155
156 // See UlpfecConfig for description.
157 UlpfecConfig ulpfec;
158
159 struct Flexfec {
160 Flexfec();
161 Flexfec(const Flexfec&);
162 ~Flexfec();
163 // Payload type of FlexFEC. Set to -1 to disable sending FlexFEC.
164 int payload_type = -1;
165
166 // SSRC of FlexFEC stream.
167 uint32_t ssrc = 0;
168
169 // Vector containing a single element, corresponding to the SSRC of the
170 // media stream being protected by this FlexFEC stream.
171 // The vector MUST have size 1.
172 //
173 // TODO(brandtr): Update comment above when we support
174 // multistream protection.
175 std::vector<uint32_t> protected_media_ssrcs;
176 } flexfec;
177
178 // Settings for RTP retransmission payload format, see RFC 4588 for
179 // details.
180 struct Rtx {
181 Rtx();
182 Rtx(const Rtx&);
183 ~Rtx();
184 std::string ToString() const;
185 // SSRCs to use for the RTX streams.
186 std::vector<uint32_t> ssrcs;
187
188 // Payload type to use for the RTX stream.
189 int payload_type = -1;
190 } rtx;
191
192 // RTCP CNAME, see RFC 3550.
193 std::string c_name;
194 } rtp;
195
Jiawei Ou3587b832018-01-31 22:08:26 -0800196 struct Rtcp {
197 Rtcp();
198 Rtcp(const Rtcp&);
199 ~Rtcp();
200 std::string ToString() const;
201
202 // Time interval between RTCP report for video
203 int64_t video_report_interval_ms = 1000;
204 // Time interval between RTCP report for audio
205 int64_t audio_report_interval_ms = 5000;
206 } rtcp;
207
aleloi440b6d92017-08-22 05:43:23 -0700208 // Transport for outgoing packets.
209 Transport* send_transport = nullptr;
210
211 // Called for each I420 frame before encoding the frame. Can be used for
212 // effects, snapshots etc. 'nullptr' disables the callback.
213 rtc::VideoSinkInterface<VideoFrame>* pre_encode_callback = nullptr;
214
215 // Called for each encoded frame, e.g. used for file storage. 'nullptr'
216 // disables the callback. Also measures timing and passes the time
217 // spent on encoding. This timing will not fire if encoding takes longer
218 // than the measuring window, since the sample data will have been dropped.
219 EncodedFrameObserver* post_encode_callback = nullptr;
220
221 // Expected delay needed by the renderer, i.e. the frame will be delivered
222 // this many milliseconds, if possible, earlier than expected render time.
223 // Only valid if |local_renderer| is set.
224 int render_delay_ms = 0;
225
226 // Target delay in milliseconds. A positive value indicates this stream is
227 // used for streaming instead of a real-time call.
228 int target_delay_ms = 0;
229
230 // True if the stream should be suspended when the available bitrate fall
231 // below the minimum configured bitrate. If this variable is false, the
232 // stream may send at a rate higher than the estimated available bitrate.
233 bool suspend_below_min_bitrate = false;
234
235 // Enables periodic bandwidth probing in application-limited region.
236 bool periodic_alr_bandwidth_probing = false;
237
Alex Narestb3944f02017-10-13 14:56:18 +0200238 // Track ID as specified during track creation.
239 std::string track_id;
240
aleloi440b6d92017-08-22 05:43:23 -0700241 private:
242 // Access to the copy constructor is private to force use of the Copy()
243 // method for those exceptional cases where we do use it.
244 Config(const Config&);
245 };
246
Seth Hampsoncc7125f2018-02-02 08:46:16 -0800247 // Updates the sending state for all simulcast layers that the video send
248 // stream owns. This can mean updating the activity one or for multiple
249 // layers. The ordering of active layers is the order in which the
250 // rtp modules are stored in the VideoSendStream.
251 // Note: This starts stream activity if it is inactive and one of the layers
252 // is active. This stops stream activity if it is active and all layers are
253 // inactive.
254 virtual void UpdateActiveSimulcastLayers(
255 const std::vector<bool> active_layers) = 0;
256
aleloi440b6d92017-08-22 05:43:23 -0700257 // Starts stream activity.
258 // When a stream is active, it can receive, process and deliver packets.
259 virtual void Start() = 0;
260 // Stops stream activity.
261 // When a stream is stopped, it can't receive, process or deliver packets.
262 virtual void Stop() = 0;
263
aleloi440b6d92017-08-22 05:43:23 -0700264 virtual void SetSource(
265 rtc::VideoSourceInterface<webrtc::VideoFrame>* source,
266 const DegradationPreference& degradation_preference) = 0;
267
268 // Set which streams to send. Must have at least as many SSRCs as configured
269 // in the config. Encoder settings are passed on to the encoder instance along
270 // with the VideoStream settings.
271 virtual void ReconfigureVideoEncoder(VideoEncoderConfig config) = 0;
272
273 virtual Stats GetStats() = 0;
274
275 // Takes ownership of each file, is responsible for closing them later.
276 // Calling this method will close and finalize any current logs.
277 // Some codecs produce multiple streams (VP8 only at present), each of these
278 // streams will log to a separate file. kMaxSimulcastStreams in common_types.h
279 // gives the max number of such streams. If there is no file for a stream, or
280 // the file is rtc::kInvalidPlatformFileValue, frames from that stream will
281 // not be logged.
282 // If a frame to be written would make the log too large the write fails and
283 // the log is closed and finalized. A |byte_limit| of 0 means no limit.
284 virtual void EnableEncodedFrameRecording(
285 const std::vector<rtc::PlatformFile>& files,
286 size_t byte_limit) = 0;
287 inline void DisableEncodedFrameRecording() {
288 EnableEncodedFrameRecording(std::vector<rtc::PlatformFile>(), 0);
289 }
290
291 protected:
292 virtual ~VideoSendStream() {}
293};
294
295} // namespace webrtc
296
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200297#endif // CALL_VIDEO_SEND_STREAM_H_