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