• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
11 #ifndef CALL_VIDEO_RECEIVE_STREAM_H_
12 #define CALL_VIDEO_RECEIVE_STREAM_H_
13 
14 #include <limits>
15 #include <map>
16 #include <set>
17 #include <string>
18 #include <utility>
19 #include <vector>
20 
21 #include "api/call/transport.h"
22 #include "api/crypto/crypto_options.h"
23 #include "api/crypto/frame_decryptor_interface.h"
24 #include "api/frame_transformer_interface.h"
25 #include "api/rtp_headers.h"
26 #include "api/rtp_parameters.h"
27 #include "api/transport/rtp/rtp_source.h"
28 #include "api/video/recordable_encoded_frame.h"
29 #include "api/video/video_content_type.h"
30 #include "api/video/video_frame.h"
31 #include "api/video/video_sink_interface.h"
32 #include "api/video/video_timing.h"
33 #include "api/video_codecs/sdp_video_format.h"
34 #include "call/rtp_config.h"
35 #include "modules/rtp_rtcp/include/rtcp_statistics.h"
36 #include "modules/rtp_rtcp/include/rtp_rtcp_defines.h"
37 
38 namespace webrtc {
39 
40 class RtpPacketSinkInterface;
41 class VideoDecoderFactory;
42 
43 class VideoReceiveStream {
44  public:
45   // Class for handling moving in/out recording state.
46   struct RecordingState {
47     RecordingState() = default;
RecordingStateRecordingState48     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 
65   // 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 
73     // Ownership stays with WebrtcVideoEngine (delegated from PeerConnection).
74     // TODO(nisse): Move one level out, to VideoReceiveStream::Config, and later
75     // to the configuration of VideoStreamDecoder.
76     VideoDecoderFactory* decoder_factory = nullptr;
77     SdpVideoFormat video_format;
78 
79     // Received RTP packets with this payload type will be sent to this decoder
80     // instance.
81     int payload_type = 0;
82   };
83 
84   struct Stats {
85     Stats();
86     ~Stats();
87     std::string ToString(int64_t time_ms) const;
88 
89     int network_frame_rate = 0;
90     int decode_frame_rate = 0;
91     int render_frame_rate = 0;
92     uint32_t frames_rendered = 0;
93 
94     // Decoder stats.
95     std::string decoder_implementation_name = "unknown";
96     FrameCounts frame_counts;
97     int decode_ms = 0;
98     int max_decode_ms = 0;
99     int current_delay_ms = 0;
100     int target_delay_ms = 0;
101     int jitter_buffer_ms = 0;
102     // https://w3c.github.io/webrtc-stats/#dom-rtcvideoreceiverstats-jitterbufferdelay
103     double jitter_buffer_delay_seconds = 0;
104     // https://w3c.github.io/webrtc-stats/#dom-rtcvideoreceiverstats-jitterbufferemittedcount
105     uint64_t jitter_buffer_emitted_count = 0;
106     int min_playout_delay_ms = 0;
107     int render_delay_ms = 10;
108     int64_t interframe_delay_max_ms = -1;
109     // Frames dropped due to decoding failures or if the system is too slow.
110     // https://www.w3.org/TR/webrtc-stats/#dom-rtcvideoreceiverstats-framesdropped
111     uint32_t frames_dropped = 0;
112     uint32_t frames_decoded = 0;
113     // https://w3c.github.io/webrtc-stats/#dom-rtcinboundrtpstreamstats-totaldecodetime
114     uint64_t total_decode_time_ms = 0;
115     // Total inter frame delay in seconds.
116     // https://w3c.github.io/webrtc-stats/#dom-rtcinboundrtpstreamstats-totalinterframedelay
117     double total_inter_frame_delay = 0;
118     // Total squared inter frame delay in seconds^2.
119     // https://w3c.github.io/webrtc-stats/#dom-rtcinboundrtpstreamstats-totalsqauredinterframedelay
120     double total_squared_inter_frame_delay = 0;
121     int64_t first_frame_received_to_decoded_ms = -1;
122     absl::optional<uint64_t> qp_sum;
123 
124     int current_payload_type = -1;
125 
126     int total_bitrate_bps = 0;
127 
128     int width = 0;
129     int height = 0;
130 
131     uint32_t freeze_count = 0;
132     uint32_t pause_count = 0;
133     uint32_t total_freezes_duration_ms = 0;
134     uint32_t total_pauses_duration_ms = 0;
135     uint32_t total_frames_duration_ms = 0;
136     double sum_squared_frame_durations = 0.0;
137 
138     VideoContentType content_type = VideoContentType::UNSPECIFIED;
139 
140     // https://w3c.github.io/webrtc-stats/#dom-rtcinboundrtpstreamstats-estimatedplayouttimestamp
141     absl::optional<int64_t> estimated_playout_ntp_timestamp_ms;
142     int sync_offset_ms = std::numeric_limits<int>::max();
143 
144     uint32_t ssrc = 0;
145     std::string c_name;
146     RtpReceiveStats rtp_stats;
147     RtcpPacketTypeCounter rtcp_packet_type_counts;
148 
149     // Timing frame info: all important timestamps for a full lifetime of a
150     // single 'timing frame'.
151     absl::optional<webrtc::TimingFrameInfo> timing_frame_info;
152   };
153 
154   struct Config {
155    private:
156     // Access to the copy constructor is private to force use of the Copy()
157     // method for those exceptional cases where we do use it.
158     Config(const Config&);
159 
160    public:
161     Config() = delete;
162     Config(Config&&);
163     explicit Config(Transport* rtcp_send_transport);
164     Config& operator=(Config&&);
165     Config& operator=(const Config&) = delete;
166     ~Config();
167 
168     // Mostly used by tests.  Avoid creating copies if you can.
CopyConfig169     Config Copy() const { return Config(*this); }
170 
171     std::string ToString() const;
172 
173     // Decoders for every payload that we can receive.
174     std::vector<Decoder> decoders;
175 
176     // Receive-stream specific RTP settings.
177     struct Rtp {
178       Rtp();
179       Rtp(const Rtp&);
180       ~Rtp();
181       std::string ToString() const;
182 
183       // Synchronization source (stream identifier) to be received.
184       uint32_t remote_ssrc = 0;
185 
186       // Sender SSRC used for sending RTCP (such as receiver reports).
187       uint32_t local_ssrc = 0;
188 
189       // See RtcpMode for description.
190       RtcpMode rtcp_mode = RtcpMode::kCompound;
191 
192       // Extended RTCP settings.
193       struct RtcpXr {
194         // True if RTCP Receiver Reference Time Report Block extension
195         // (RFC 3611) should be enabled.
196         bool receiver_reference_time_report = false;
197       } rtcp_xr;
198 
199       // See draft-holmer-rmcat-transport-wide-cc-extensions for details.
200       bool transport_cc = false;
201 
202       // See LntfConfig for description.
203       LntfConfig lntf;
204 
205       // See NackConfig for description.
206       NackConfig nack;
207 
208       // Payload types for ULPFEC and RED, respectively.
209       int ulpfec_payload_type = -1;
210       int red_payload_type = -1;
211 
212       // SSRC for retransmissions.
213       uint32_t rtx_ssrc = 0;
214 
215       // Set if the stream is protected using FlexFEC.
216       bool protected_by_flexfec = false;
217 
218       // Map from rtx payload type -> media payload type.
219       // For RTX to be enabled, both an SSRC and this mapping are needed.
220       std::map<int, int> rtx_associated_payload_types;
221 
222       // Payload types that should be depacketized using raw depacketizer
223       // (payload header will not be parsed and must not be present, additional
224       // meta data is expected to be present in generic frame descriptor
225       // RTP header extension).
226       std::set<int> raw_payload_types;
227 
228       // RTP header extensions used for the received stream.
229       std::vector<RtpExtension> extensions;
230     } rtp;
231 
232     // Transport for outgoing packets (RTCP).
233     Transport* rtcp_send_transport = nullptr;
234 
235     // Must always be set.
236     rtc::VideoSinkInterface<VideoFrame>* renderer = nullptr;
237 
238     // Expected delay needed by the renderer, i.e. the frame will be delivered
239     // this many milliseconds, if possible, earlier than the ideal render time.
240     int render_delay_ms = 10;
241 
242     // If false, pass frames on to the renderer as soon as they are
243     // available.
244     bool enable_prerenderer_smoothing = true;
245 
246     // Identifier for an A/V synchronization group. Empty string to disable.
247     // TODO(pbos): Synchronize streams in a sync group, not just video streams
248     // to one of the audio streams.
249     std::string sync_group;
250 
251     // Target delay in milliseconds. A positive value indicates this stream is
252     // used for streaming instead of a real-time call.
253     int target_delay_ms = 0;
254 
255     // TODO(nisse): Used with VideoDecoderFactory::LegacyCreateVideoDecoder.
256     // Delete when that method is retired.
257     std::string stream_id;
258 
259     // An optional custom frame decryptor that allows the entire frame to be
260     // decrypted in whatever way the caller choses. This is not required by
261     // default.
262     rtc::scoped_refptr<webrtc::FrameDecryptorInterface> frame_decryptor;
263 
264     // Per PeerConnection cryptography options.
265     CryptoOptions crypto_options;
266 
267     rtc::scoped_refptr<webrtc::FrameTransformerInterface> frame_transformer;
268   };
269 
270   // Starts stream activity.
271   // When a stream is active, it can receive, process and deliver packets.
272   virtual void Start() = 0;
273   // Stops stream activity.
274   // When a stream is stopped, it can't receive, process or deliver packets.
275   virtual void Stop() = 0;
276 
277   // TODO(pbos): Add info on currently-received codec to Stats.
278   virtual Stats GetStats() const = 0;
279 
280   // RtpDemuxer only forwards a given RTP packet to one sink. However, some
281   // sinks, such as FlexFEC, might wish to be informed of all of the packets
282   // a given sink receives (or any set of sinks). They may do so by registering
283   // themselves as secondary sinks.
284   virtual void AddSecondarySink(RtpPacketSinkInterface* sink) = 0;
285   virtual void RemoveSecondarySink(const RtpPacketSinkInterface* sink) = 0;
286 
287   virtual std::vector<RtpSource> GetSources() const = 0;
288 
289   // Sets a base minimum for the playout delay. Base minimum delay sets lower
290   // bound on minimum delay value determining lower bound on playout delay.
291   //
292   // Returns true if value was successfully set, false overwise.
293   virtual bool SetBaseMinimumPlayoutDelayMs(int delay_ms) = 0;
294 
295   // Returns current value of base minimum delay in milliseconds.
296   virtual int GetBaseMinimumPlayoutDelayMs() const = 0;
297 
298   // Allows a FrameDecryptor to be attached to a VideoReceiveStream after
299   // creation without resetting the decoder state.
300   virtual void SetFrameDecryptor(
301       rtc::scoped_refptr<FrameDecryptorInterface> frame_decryptor) = 0;
302 
303   // Allows a frame transformer to be attached to a VideoReceiveStream after
304   // creation without resetting the decoder state.
305   virtual void SetDepacketizerToDecoderFrameTransformer(
306       rtc::scoped_refptr<FrameTransformerInterface> frame_transformer) = 0;
307 
308   // Sets and returns recording state. The old state is moved out
309   // of the video receive stream and returned to the caller, and |state|
310   // is moved in. If the state's callback is set, it will be called with
311   // recordable encoded frames as they arrive.
312   // If |generate_key_frame| is true, the method will generate a key frame.
313   // When the function returns, it's guaranteed that all old callouts
314   // to the returned callback has ceased.
315   // Note: the client should not interpret the returned state's attributes, but
316   // instead treat it as opaque data.
317   virtual RecordingState SetAndGetRecordingState(RecordingState state,
318                                                  bool generate_key_frame) = 0;
319 
320   // Cause eventual generation of a key frame from the sender.
321   virtual void GenerateKeyFrame() = 0;
322 
323  protected:
~VideoReceiveStream()324   virtual ~VideoReceiveStream() {}
325 };
326 
327 }  // namespace webrtc
328 
329 #endif  // CALL_VIDEO_RECEIVE_STREAM_H_
330