video_receive_stream.h 12 KB

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