video_send_stream.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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_SEND_STREAM_H_
  11. #define CALL_VIDEO_SEND_STREAM_H_
  12. #include <stdint.h>
  13. #include <map>
  14. #include <string>
  15. #include <vector>
  16. #include "absl/types/optional.h"
  17. #include "api/adaptation/resource.h"
  18. #include "api/call/transport.h"
  19. #include "api/crypto/crypto_options.h"
  20. #include "api/frame_transformer_interface.h"
  21. #include "api/rtp_parameters.h"
  22. #include "api/scoped_refptr.h"
  23. #include "api/video/video_content_type.h"
  24. #include "api/video/video_frame.h"
  25. #include "api/video/video_sink_interface.h"
  26. #include "api/video/video_source_interface.h"
  27. #include "api/video/video_stream_encoder_settings.h"
  28. #include "api/video_codecs/video_encoder_config.h"
  29. #include "call/rtp_config.h"
  30. #include "common_video/include/quality_limitation_reason.h"
  31. #include "modules/rtp_rtcp/include/report_block_data.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 FrameEncryptorInterface;
  36. class VideoSendStream {
  37. public:
  38. // Multiple StreamStats objects are present if simulcast is used (multiple
  39. // kMedia streams) or if RTX or FlexFEC is negotiated. Multiple SVC layers, on
  40. // the other hand, does not cause additional StreamStats.
  41. struct StreamStats {
  42. enum class StreamType {
  43. // A media stream is an RTP stream for audio or video. Retransmissions and
  44. // FEC is either sent over the same SSRC or negotiated to be sent over
  45. // separate SSRCs, in which case separate StreamStats objects exist with
  46. // references to this media stream's SSRC.
  47. kMedia,
  48. // RTX streams are streams dedicated to retransmissions. They have a
  49. // dependency on a single kMedia stream: |referenced_media_ssrc|.
  50. kRtx,
  51. // FlexFEC streams are streams dedicated to FlexFEC. They have a
  52. // dependency on a single kMedia stream: |referenced_media_ssrc|.
  53. kFlexfec,
  54. };
  55. StreamStats();
  56. ~StreamStats();
  57. std::string ToString() const;
  58. StreamType type = StreamType::kMedia;
  59. // If |type| is kRtx or kFlexfec this value is present. The referenced SSRC
  60. // is the kMedia stream that this stream is performing retransmissions or
  61. // FEC for. If |type| is kMedia, this value is null.
  62. absl::optional<uint32_t> referenced_media_ssrc;
  63. FrameCounts frame_counts;
  64. int width = 0;
  65. int height = 0;
  66. // TODO(holmer): Move bitrate_bps out to the webrtc::Call layer.
  67. int total_bitrate_bps = 0;
  68. int retransmit_bitrate_bps = 0;
  69. int avg_delay_ms = 0;
  70. int max_delay_ms = 0;
  71. uint64_t total_packet_send_delay_ms = 0;
  72. StreamDataCounters rtp_stats;
  73. RtcpPacketTypeCounter rtcp_packet_type_counts;
  74. RtcpStatistics rtcp_stats;
  75. // A snapshot of the most recent Report Block with additional data of
  76. // interest to statistics. Used to implement RTCRemoteInboundRtpStreamStats.
  77. absl::optional<ReportBlockData> report_block_data;
  78. double encode_frame_rate = 0.0;
  79. int frames_encoded = 0;
  80. absl::optional<uint64_t> qp_sum;
  81. uint64_t total_encode_time_ms = 0;
  82. uint64_t total_encoded_bytes_target = 0;
  83. uint32_t huge_frames_sent = 0;
  84. };
  85. struct Stats {
  86. Stats();
  87. ~Stats();
  88. std::string ToString(int64_t time_ms) const;
  89. std::string encoder_implementation_name = "unknown";
  90. int input_frame_rate = 0;
  91. int encode_frame_rate = 0;
  92. int avg_encode_time_ms = 0;
  93. int encode_usage_percent = 0;
  94. uint32_t frames_encoded = 0;
  95. // https://w3c.github.io/webrtc-stats/#dom-rtcoutboundrtpstreamstats-totalencodetime
  96. uint64_t total_encode_time_ms = 0;
  97. // https://w3c.github.io/webrtc-stats/#dom-rtcoutboundrtpstreamstats-totalencodedbytestarget
  98. uint64_t total_encoded_bytes_target = 0;
  99. uint32_t frames_dropped_by_capturer = 0;
  100. uint32_t frames_dropped_by_encoder_queue = 0;
  101. uint32_t frames_dropped_by_rate_limiter = 0;
  102. uint32_t frames_dropped_by_congestion_window = 0;
  103. uint32_t frames_dropped_by_encoder = 0;
  104. // Bitrate the encoder is currently configured to use due to bandwidth
  105. // limitations.
  106. int target_media_bitrate_bps = 0;
  107. // Bitrate the encoder is actually producing.
  108. int media_bitrate_bps = 0;
  109. bool suspended = false;
  110. bool bw_limited_resolution = false;
  111. bool cpu_limited_resolution = false;
  112. bool bw_limited_framerate = false;
  113. bool cpu_limited_framerate = false;
  114. // https://w3c.github.io/webrtc-stats/#dom-rtcoutboundrtpstreamstats-qualitylimitationreason
  115. QualityLimitationReason quality_limitation_reason =
  116. QualityLimitationReason::kNone;
  117. // https://w3c.github.io/webrtc-stats/#dom-rtcoutboundrtpstreamstats-qualitylimitationdurations
  118. std::map<QualityLimitationReason, int64_t> quality_limitation_durations_ms;
  119. // https://w3c.github.io/webrtc-stats/#dom-rtcoutboundrtpstreamstats-qualitylimitationresolutionchanges
  120. uint32_t quality_limitation_resolution_changes = 0;
  121. // Total number of times resolution as been requested to be changed due to
  122. // CPU/quality adaptation.
  123. int number_of_cpu_adapt_changes = 0;
  124. int number_of_quality_adapt_changes = 0;
  125. bool has_entered_low_resolution = false;
  126. std::map<uint32_t, StreamStats> substreams;
  127. webrtc::VideoContentType content_type =
  128. webrtc::VideoContentType::UNSPECIFIED;
  129. uint32_t frames_sent = 0;
  130. uint32_t huge_frames_sent = 0;
  131. };
  132. struct Config {
  133. public:
  134. Config() = delete;
  135. Config(Config&&);
  136. explicit Config(Transport* send_transport);
  137. Config& operator=(Config&&);
  138. Config& operator=(const Config&) = delete;
  139. ~Config();
  140. // Mostly used by tests. Avoid creating copies if you can.
  141. Config Copy() const { return Config(*this); }
  142. std::string ToString() const;
  143. RtpConfig rtp;
  144. VideoStreamEncoderSettings encoder_settings;
  145. // Time interval between RTCP report for video
  146. int rtcp_report_interval_ms = 1000;
  147. // Transport for outgoing packets.
  148. Transport* send_transport = nullptr;
  149. // Expected delay needed by the renderer, i.e. the frame will be delivered
  150. // this many milliseconds, if possible, earlier than expected render time.
  151. // Only valid if |local_renderer| is set.
  152. int render_delay_ms = 0;
  153. // Target delay in milliseconds. A positive value indicates this stream is
  154. // used for streaming instead of a real-time call.
  155. int target_delay_ms = 0;
  156. // True if the stream should be suspended when the available bitrate fall
  157. // below the minimum configured bitrate. If this variable is false, the
  158. // stream may send at a rate higher than the estimated available bitrate.
  159. bool suspend_below_min_bitrate = false;
  160. // Enables periodic bandwidth probing in application-limited region.
  161. bool periodic_alr_bandwidth_probing = false;
  162. // An optional custom frame encryptor that allows the entire frame to be
  163. // encrypted in whatever way the caller chooses. This is not required by
  164. // default.
  165. rtc::scoped_refptr<webrtc::FrameEncryptorInterface> frame_encryptor;
  166. // Per PeerConnection cryptography options.
  167. CryptoOptions crypto_options;
  168. rtc::scoped_refptr<webrtc::FrameTransformerInterface> frame_transformer;
  169. private:
  170. // Access to the copy constructor is private to force use of the Copy()
  171. // method for those exceptional cases where we do use it.
  172. Config(const Config&);
  173. };
  174. // Updates the sending state for all simulcast layers that the video send
  175. // stream owns. This can mean updating the activity one or for multiple
  176. // layers. The ordering of active layers is the order in which the
  177. // rtp modules are stored in the VideoSendStream.
  178. // Note: This starts stream activity if it is inactive and one of the layers
  179. // is active. This stops stream activity if it is active and all layers are
  180. // inactive.
  181. virtual void UpdateActiveSimulcastLayers(
  182. const std::vector<bool> active_layers) = 0;
  183. // Starts stream activity.
  184. // When a stream is active, it can receive, process and deliver packets.
  185. virtual void Start() = 0;
  186. // Stops stream activity.
  187. // When a stream is stopped, it can't receive, process or deliver packets.
  188. virtual void Stop() = 0;
  189. // If the resource is overusing, the VideoSendStream will try to reduce
  190. // resolution or frame rate until no resource is overusing.
  191. // TODO(https://crbug.com/webrtc/11565): When the ResourceAdaptationProcessor
  192. // is moved to Call this method could be deleted altogether in favor of
  193. // Call-level APIs only.
  194. virtual void AddAdaptationResource(rtc::scoped_refptr<Resource> resource) = 0;
  195. virtual std::vector<rtc::scoped_refptr<Resource>>
  196. GetAdaptationResources() = 0;
  197. virtual void SetSource(
  198. rtc::VideoSourceInterface<webrtc::VideoFrame>* source,
  199. const DegradationPreference& degradation_preference) = 0;
  200. // Set which streams to send. Must have at least as many SSRCs as configured
  201. // in the config. Encoder settings are passed on to the encoder instance along
  202. // with the VideoStream settings.
  203. virtual void ReconfigureVideoEncoder(VideoEncoderConfig config) = 0;
  204. virtual Stats GetStats() = 0;
  205. protected:
  206. virtual ~VideoSendStream() {}
  207. };
  208. } // namespace webrtc
  209. #endif // CALL_VIDEO_SEND_STREAM_H_