blob: 91c637160a7e11278340069abb5610be5d9b8db2 [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_RECEIVE_STREAM_H_
12#define CALL_VIDEO_RECEIVE_STREAM_H_
aleloi440b6d92017-08-22 05:43:23 -070013
14#include <limits>
15#include <map>
Mirta Dvornicicfe68daa2019-05-23 13:21:12 +020016#include <set>
aleloi440b6d92017-08-22 05:43:23 -070017#include <string>
Markus Handell269ac812019-12-03 14:31:45 +010018#include <utility>
aleloi440b6d92017-08-22 05:43:23 -070019#include <vector>
20
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "api/call/transport.h"
Steve Anton10542f22019-01-11 09:11:00 -080022#include "api/crypto/crypto_options.h"
Niels Möllera8370302019-09-02 15:16:49 +020023#include "api/crypto/frame_decryptor_interface.h"
Marina Ciocea412a31b2020-02-28 16:02:06 +010024#include "api/frame_transformer_interface.h"
Yves Gerey665174f2018-06-19 15:03:05 +020025#include "api/rtp_headers.h"
Steve Anton10542f22019-01-11 09:11:00 -080026#include "api/rtp_parameters.h"
Niels Möllera8370302019-09-02 15:16:49 +020027#include "api/transport/rtp/rtp_source.h"
Markus Handell269ac812019-12-03 14:31:45 +010028#include "api/video/recordable_encoded_frame.h"
Patrik Höglund3e113432017-12-15 14:40:10 +010029#include "api/video/video_content_type.h"
Niels Möllera8370302019-09-02 15:16:49 +020030#include "api/video/video_frame.h"
Niels Möllerc6ce9c52018-05-11 11:15:30 +020031#include "api/video/video_sink_interface.h"
Yves Gerey665174f2018-06-19 15:03:05 +020032#include "api/video/video_timing.h"
Niels Möllercb7e1d22018-09-11 15:56:04 +020033#include "api/video_codecs/sdp_video_format.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020034#include "call/rtp_config.h"
Niels Möller53382cb2018-11-27 14:05:08 +010035#include "modules/rtp_rtcp/include/rtcp_statistics.h"
Patrik Höglund3e113432017-12-15 14:40:10 +010036#include "modules/rtp_rtcp/include/rtp_rtcp_defines.h"
aleloi440b6d92017-08-22 05:43:23 -070037
38namespace webrtc {
39
40class RtpPacketSinkInterface;
Niels Möllercbcbc222018-09-28 09:07:24 +020041class VideoDecoderFactory;
aleloi440b6d92017-08-22 05:43:23 -070042
43class VideoReceiveStream {
44 public:
Markus Handell269ac812019-12-03 14:31:45 +010045 // Class for handling moving in/out recording state.
46 struct RecordingState {
47 RecordingState() = default;
48 explicit RecordingState(
49 std::function<void(const RecordableEncodedFrame&)> callback)
50 : callback(std::move(callback)) {}
51
52 // Callback stored from the VideoReceiveStream. The VideoReceiveStream
53 // client should not interpret the attribute.
54 std::function<void(const RecordableEncodedFrame&)> callback;
55 // Memento of internal state in VideoReceiveStream, recording wether
56 // we're currently causing generation of a keyframe from the sender. Needed
57 // to avoid sending double keyframe requests. The VideoReceiveStream client
58 // should not interpret the attribute.
59 bool keyframe_needed = false;
60 // Memento of when a keyframe request was last sent. The VideoReceiveStream
61 // client should not interpret the attribute.
62 absl::optional<int64_t> last_keyframe_request_ms;
63 };
64
aleloi440b6d92017-08-22 05:43:23 -070065 // TODO(mflodman) Move all these settings to VideoDecoder and move the
66 // declaration to common_types.h.
67 struct Decoder {
68 Decoder();
69 Decoder(const Decoder&);
70 ~Decoder();
71 std::string ToString() const;
72
Niels Möllercb7e1d22018-09-11 15:56:04 +020073 SdpVideoFormat video_format;
aleloi440b6d92017-08-22 05:43:23 -070074
75 // Received RTP packets with this payload type will be sent to this decoder
76 // instance.
77 int payload_type = 0;
aleloi440b6d92017-08-22 05:43:23 -070078 };
79
80 struct Stats {
81 Stats();
82 ~Stats();
83 std::string ToString(int64_t time_ms) const;
84
85 int network_frame_rate = 0;
86 int decode_frame_rate = 0;
87 int render_frame_rate = 0;
88 uint32_t frames_rendered = 0;
89
90 // Decoder stats.
91 std::string decoder_implementation_name = "unknown";
92 FrameCounts frame_counts;
93 int decode_ms = 0;
94 int max_decode_ms = 0;
95 int current_delay_ms = 0;
96 int target_delay_ms = 0;
97 int jitter_buffer_ms = 0;
Guido Urdaneta67378412019-05-28 17:38:08 +020098 // https://w3c.github.io/webrtc-stats/#dom-rtcvideoreceiverstats-jitterbufferdelay
99 double jitter_buffer_delay_seconds = 0;
100 // https://w3c.github.io/webrtc-stats/#dom-rtcvideoreceiverstats-jitterbufferemittedcount
101 uint64_t jitter_buffer_emitted_count = 0;
aleloi440b6d92017-08-22 05:43:23 -0700102 int min_playout_delay_ms = 0;
103 int render_delay_ms = 10;
ilnika79cc282017-08-23 05:24:10 -0700104 int64_t interframe_delay_max_ms = -1;
Johannes Kron0c141c52019-08-26 15:04:43 +0200105 // Frames dropped due to decoding failures or if the system is too slow.
106 // https://www.w3.org/TR/webrtc-stats/#dom-rtcvideoreceiverstats-framesdropped
107 uint32_t frames_dropped = 0;
aleloi440b6d92017-08-22 05:43:23 -0700108 uint32_t frames_decoded = 0;
Johannes Kronbfd343b2019-07-01 10:07:50 +0200109 // https://w3c.github.io/webrtc-stats/#dom-rtcinboundrtpstreamstats-totaldecodetime
110 uint64_t total_decode_time_ms = 0;
Johannes Kron00376e12019-11-25 10:25:42 +0100111 // Total inter frame delay in seconds.
112 // https://w3c.github.io/webrtc-stats/#dom-rtcinboundrtpstreamstats-totalinterframedelay
113 double total_inter_frame_delay = 0;
114 // Total squared inter frame delay in seconds^2.
115 // https://w3c.github.io/webrtc-stats/#dom-rtcinboundrtpstreamstats-totalsqauredinterframedelay
116 double total_squared_inter_frame_delay = 0;
Benjamin Wright514f0842018-12-10 09:55:17 -0800117 int64_t first_frame_received_to_decoded_ms = -1;
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200118 absl::optional<uint64_t> qp_sum;
aleloi440b6d92017-08-22 05:43:23 -0700119
120 int current_payload_type = -1;
121
122 int total_bitrate_bps = 0;
aleloi440b6d92017-08-22 05:43:23 -0700123
124 int width = 0;
125 int height = 0;
126
Sergey Silkin02371062019-01-31 16:45:42 +0100127 uint32_t freeze_count = 0;
128 uint32_t pause_count = 0;
129 uint32_t total_freezes_duration_ms = 0;
130 uint32_t total_pauses_duration_ms = 0;
131 uint32_t total_frames_duration_ms = 0;
132 double sum_squared_frame_durations = 0.0;
133
ilnik2e1b40b2017-09-04 07:57:17 -0700134 VideoContentType content_type = VideoContentType::UNSPECIFIED;
135
Åsa Perssonfcf79cc2019-10-22 15:23:44 +0200136 // https://w3c.github.io/webrtc-stats/#dom-rtcinboundrtpstreamstats-estimatedplayouttimestamp
137 absl::optional<int64_t> estimated_playout_ntp_timestamp_ms;
aleloi440b6d92017-08-22 05:43:23 -0700138 int sync_offset_ms = std::numeric_limits<int>::max();
139
140 uint32_t ssrc = 0;
141 std::string c_name;
Niels Möllerd77cc242019-08-22 09:40:25 +0200142 RtpReceiveStats rtp_stats;
aleloi440b6d92017-08-22 05:43:23 -0700143 RtcpPacketTypeCounter rtcp_packet_type_counts;
ilnik75204c52017-09-04 03:35:40 -0700144
145 // Timing frame info: all important timestamps for a full lifetime of a
146 // single 'timing frame'.
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200147 absl::optional<webrtc::TimingFrameInfo> timing_frame_info;
aleloi440b6d92017-08-22 05:43:23 -0700148 };
149
150 struct Config {
151 private:
152 // Access to the copy constructor is private to force use of the Copy()
153 // method for those exceptional cases where we do use it.
154 Config(const Config&);
155
156 public:
157 Config() = delete;
158 Config(Config&&);
159 explicit Config(Transport* rtcp_send_transport);
160 Config& operator=(Config&&);
161 Config& operator=(const Config&) = delete;
162 ~Config();
163
164 // Mostly used by tests. Avoid creating copies if you can.
165 Config Copy() const { return Config(*this); }
166
167 std::string ToString() const;
168
169 // Decoders for every payload that we can receive.
170 std::vector<Decoder> decoders;
171
Philip Eliasson2b068ce2020-08-03 15:55:10 +0000172 // Ownership stays with WebrtcVideoEngine (delegated from PeerConnection).
173 VideoDecoderFactory* decoder_factory = nullptr;
174
aleloi440b6d92017-08-22 05:43:23 -0700175 // Receive-stream specific RTP settings.
176 struct Rtp {
177 Rtp();
178 Rtp(const Rtp&);
179 ~Rtp();
180 std::string ToString() const;
181
182 // Synchronization source (stream identifier) to be received.
183 uint32_t remote_ssrc = 0;
184
185 // Sender SSRC used for sending RTCP (such as receiver reports).
186 uint32_t local_ssrc = 0;
187
188 // See RtcpMode for description.
189 RtcpMode rtcp_mode = RtcpMode::kCompound;
190
191 // Extended RTCP settings.
192 struct RtcpXr {
193 // True if RTCP Receiver Reference Time Report Block extension
194 // (RFC 3611) should be enabled.
195 bool receiver_reference_time_report = false;
196 } rtcp_xr;
197
aleloi440b6d92017-08-22 05:43:23 -0700198 // See draft-holmer-rmcat-transport-wide-cc-extensions for details.
199 bool transport_cc = false;
200
Elad Alonfadb1812019-05-24 13:40:02 +0200201 // See LntfConfig for description.
202 LntfConfig lntf;
203
aleloi440b6d92017-08-22 05:43:23 -0700204 // See NackConfig for description.
205 NackConfig nack;
206
nisse3b3622f2017-09-26 02:49:21 -0700207 // Payload types for ULPFEC and RED, respectively.
208 int ulpfec_payload_type = -1;
209 int red_payload_type = -1;
aleloi440b6d92017-08-22 05:43:23 -0700210
211 // SSRC for retransmissions.
212 uint32_t rtx_ssrc = 0;
213
214 // Set if the stream is protected using FlexFEC.
215 bool protected_by_flexfec = false;
216
nisse26e3abb2017-08-25 04:44:25 -0700217 // Map from rtx payload type -> media payload type.
aleloi440b6d92017-08-22 05:43:23 -0700218 // For RTX to be enabled, both an SSRC and this mapping are needed.
nisse26e3abb2017-08-25 04:44:25 -0700219 std::map<int, int> rtx_associated_payload_types;
nisse26e3abb2017-08-25 04:44:25 -0700220
Mirta Dvornicicfe68daa2019-05-23 13:21:12 +0200221 // Payload types that should be depacketized using raw depacketizer
222 // (payload header will not be parsed and must not be present, additional
223 // meta data is expected to be present in generic frame descriptor
224 // RTP header extension).
225 std::set<int> raw_payload_types;
226
aleloi440b6d92017-08-22 05:43:23 -0700227 // RTP header extensions used for the received stream.
228 std::vector<RtpExtension> extensions;
229 } rtp;
230
231 // Transport for outgoing packets (RTCP).
232 Transport* rtcp_send_transport = nullptr;
233
Rasmus Brandt1e27fec2019-01-23 09:47:50 +0100234 // Must always be set.
aleloi440b6d92017-08-22 05:43:23 -0700235 rtc::VideoSinkInterface<VideoFrame>* renderer = nullptr;
236
237 // Expected delay needed by the renderer, i.e. the frame will be delivered
238 // this many milliseconds, if possible, earlier than the ideal render time.
aleloi440b6d92017-08-22 05:43:23 -0700239 int render_delay_ms = 10;
240
Rasmus Brandt1e27fec2019-01-23 09:47:50 +0100241 // If false, pass frames on to the renderer as soon as they are
aleloi440b6d92017-08-22 05:43:23 -0700242 // available.
Rasmus Brandt1e27fec2019-01-23 09:47:50 +0100243 bool enable_prerenderer_smoothing = true;
aleloi440b6d92017-08-22 05:43:23 -0700244
245 // Identifier for an A/V synchronization group. Empty string to disable.
246 // TODO(pbos): Synchronize streams in a sync group, not just video streams
247 // to one of the audio streams.
248 std::string sync_group;
249
aleloi440b6d92017-08-22 05:43:23 -0700250 // Target delay in milliseconds. A positive value indicates this stream is
251 // used for streaming instead of a real-time call.
252 int target_delay_ms = 0;
Niels Möllercbcbc222018-09-28 09:07:24 +0200253
Philip Eliasson49c293f2020-07-27 13:54:46 +0000254 // TODO(nisse): Used with VideoDecoderFactory::LegacyCreateVideoDecoder.
255 // Delete when that method is retired.
256 std::string stream_id;
257
Benjamin Wright192eeec2018-10-17 17:27:25 -0700258 // An optional custom frame decryptor that allows the entire frame to be
259 // decrypted in whatever way the caller choses. This is not required by
260 // default.
261 rtc::scoped_refptr<webrtc::FrameDecryptorInterface> frame_decryptor;
262
263 // Per PeerConnection cryptography options.
264 CryptoOptions crypto_options;
Marina Ciocea412a31b2020-02-28 16:02:06 +0100265
266 rtc::scoped_refptr<webrtc::FrameTransformerInterface> frame_transformer;
aleloi440b6d92017-08-22 05:43:23 -0700267 };
268
269 // Starts stream activity.
270 // When a stream is active, it can receive, process and deliver packets.
271 virtual void Start() = 0;
272 // Stops stream activity.
273 // When a stream is stopped, it can't receive, process or deliver packets.
274 virtual void Stop() = 0;
275
276 // TODO(pbos): Add info on currently-received codec to Stats.
277 virtual Stats GetStats() const = 0;
278
aleloi440b6d92017-08-22 05:43:23 -0700279 // RtpDemuxer only forwards a given RTP packet to one sink. However, some
280 // sinks, such as FlexFEC, might wish to be informed of all of the packets
281 // a given sink receives (or any set of sinks). They may do so by registering
282 // themselves as secondary sinks.
283 virtual void AddSecondarySink(RtpPacketSinkInterface* sink) = 0;
284 virtual void RemoveSecondarySink(const RtpPacketSinkInterface* sink) = 0;
285
Jonas Oreland49ac5952018-09-26 16:04:32 +0200286 virtual std::vector<RtpSource> GetSources() const = 0;
287
Ruslan Burakov493a6502019-02-27 15:32:48 +0100288 // Sets a base minimum for the playout delay. Base minimum delay sets lower
289 // bound on minimum delay value determining lower bound on playout delay.
290 //
291 // Returns true if value was successfully set, false overwise.
292 virtual bool SetBaseMinimumPlayoutDelayMs(int delay_ms) = 0;
293
294 // Returns current value of base minimum delay in milliseconds.
295 virtual int GetBaseMinimumPlayoutDelayMs() const = 0;
296
Benjamin Wrighta5564482019-04-03 10:44:18 -0700297 // Allows a FrameDecryptor to be attached to a VideoReceiveStream after
298 // creation without resetting the decoder state.
299 virtual void SetFrameDecryptor(
300 rtc::scoped_refptr<FrameDecryptorInterface> frame_decryptor) = 0;
301
Guido Urdanetae1aa22f2020-03-30 23:02:14 +0200302 // Allows a frame transformer to be attached to a VideoReceiveStream after
303 // creation without resetting the decoder state.
304 virtual void SetDepacketizerToDecoderFrameTransformer(
305 rtc::scoped_refptr<FrameTransformerInterface> frame_transformer) = 0;
306
Markus Handell269ac812019-12-03 14:31:45 +0100307 // Sets and returns recording state. The old state is moved out
308 // of the video receive stream and returned to the caller, and |state|
309 // is moved in. If the state's callback is set, it will be called with
310 // recordable encoded frames as they arrive.
311 // If |generate_key_frame| is true, the method will generate a key frame.
312 // When the function returns, it's guaranteed that all old callouts
313 // to the returned callback has ceased.
314 // Note: the client should not interpret the returned state's attributes, but
315 // instead treat it as opaque data.
316 virtual RecordingState SetAndGetRecordingState(RecordingState state,
317 bool generate_key_frame) = 0;
318
319 // Cause eventual generation of a key frame from the sender.
320 virtual void GenerateKeyFrame() = 0;
321
aleloi440b6d92017-08-22 05:43:23 -0700322 protected:
323 virtual ~VideoReceiveStream() {}
324};
325
326} // namespace webrtc
327
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200328#endif // CALL_VIDEO_RECEIVE_STREAM_H_