video_send_stream.h 9.7 KB

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