media_channel.h (42550B)
1 /* 2 * Copyright (c) 2004 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 MEDIA_BASE_MEDIA_CHANNEL_H_ 12 #define MEDIA_BASE_MEDIA_CHANNEL_H_ 13 14 #include <cstddef> 15 #include <cstdint> 16 #include <functional> 17 #include <map> 18 #include <memory> 19 #include <optional> 20 #include <set> 21 #include <string> 22 #include <utility> 23 #include <vector> 24 25 #include "absl/functional/any_invocable.h" 26 #include "absl/strings/str_cat.h" 27 #include "absl/strings/string_view.h" 28 #include "api/audio/audio_processing_statistics.h" 29 #include "api/audio_codecs/audio_encoder.h" 30 #include "api/audio_options.h" 31 #include "api/call/audio_sink.h" 32 #include "api/crypto/frame_decryptor_interface.h" 33 #include "api/crypto/frame_encryptor_interface.h" 34 #include "api/frame_transformer_interface.h" 35 #include "api/media_stream_interface.h" 36 #include "api/media_types.h" 37 #include "api/rtc_error.h" 38 #include "api/rtp_headers.h" 39 #include "api/rtp_parameters.h" 40 #include "api/rtp_sender_interface.h" 41 #include "api/scoped_refptr.h" 42 #include "api/transport/rtp/rtp_source.h" 43 #include "api/units/data_rate.h" 44 #include "api/units/time_delta.h" 45 #include "api/units/timestamp.h" 46 #include "api/video/encoded_image.h" 47 #include "api/video/recordable_encoded_frame.h" 48 #include "api/video/video_content_type.h" 49 #include "api/video/video_sink_interface.h" 50 #include "api/video/video_source_interface.h" 51 #include "api/video/video_timing.h" 52 #include "api/video_codecs/scalability_mode.h" 53 #include "api/video_codecs/video_encoder_factory.h" 54 #include "common_video/include/quality_limitation_reason.h" 55 #include "media/base/audio_source.h" 56 #include "media/base/codec.h" 57 #include "media/base/stream_params.h" 58 #include "modules/rtp_rtcp/include/report_block_data.h" 59 #include "modules/rtp_rtcp/source/rtp_packet_received.h" 60 #include "rtc_base/async_packet_socket.h" 61 #include "rtc_base/copy_on_write_buffer.h" 62 #include "rtc_base/network/sent_packet.h" 63 #include "rtc_base/network_route.h" 64 #include "rtc_base/socket.h" 65 #include "rtc_base/strings/string_builder.h" 66 67 namespace webrtc { 68 class VideoFrame; 69 struct VideoFormat; 70 71 class VideoMediaSendChannelInterface; 72 class VideoMediaReceiveChannelInterface; 73 class VoiceMediaSendChannelInterface; 74 class VoiceMediaReceiveChannelInterface; 75 76 const int kScreencastDefaultFps = 5; 77 78 template <class T> 79 static std::string ToStringIfSet(const char* key, const std::optional<T>& val) { 80 std::string str; 81 if (val) { 82 str = key; 83 str += ": "; 84 str += val ? absl::StrCat(*val) : ""; 85 str += ", "; 86 } 87 return str; 88 } 89 90 template <class T> 91 static std::string VectorToString(const std::vector<T>& vals) { 92 StringBuilder ost; 93 ost << "["; 94 for (size_t i = 0; i < vals.size(); ++i) { 95 if (i > 0) { 96 ost << ", "; 97 } 98 ost << vals[i].ToString(); 99 } 100 ost << "]"; 101 return ost.Release(); 102 } 103 104 // Options that can be applied to a VideoMediaChannel or a VideoMediaEngine. 105 // Used to be flags, but that makes it hard to selectively apply options. 106 // We are moving all of the setting of options to structs like this, 107 // but some things currently still use flags. 108 struct VideoOptions { 109 VideoOptions(); 110 ~VideoOptions(); 111 112 void SetAll(const VideoOptions& change) { 113 SetFrom(&video_noise_reduction, change.video_noise_reduction); 114 SetFrom(&screencast_min_bitrate_kbps, change.screencast_min_bitrate_kbps); 115 SetFrom(&is_screencast, change.is_screencast); 116 } 117 118 bool operator==(const VideoOptions& o) const { 119 return video_noise_reduction == o.video_noise_reduction && 120 screencast_min_bitrate_kbps == o.screencast_min_bitrate_kbps && 121 is_screencast == o.is_screencast; 122 } 123 bool operator!=(const VideoOptions& o) const { return !(*this == o); } 124 125 std::string ToString() const { 126 StringBuilder ost; 127 ost << "VideoOptions {"; 128 ost << ToStringIfSet("noise reduction", video_noise_reduction); 129 ost << ToStringIfSet("screencast min bitrate kbps", 130 screencast_min_bitrate_kbps); 131 ost << ToStringIfSet("is_screencast ", is_screencast); 132 ost << "}"; 133 return ost.Release(); 134 } 135 136 // Enable denoising? This flag comes from the getUserMedia 137 // constraint 'googNoiseReduction', and WebRtcVideoEngine passes it 138 // on to the codec options. Disabled by default. 139 std::optional<bool> video_noise_reduction; 140 // Force screencast to use a minimum bitrate. This flag comes from 141 // the PeerConnection constraint 'googScreencastMinBitrate'. It is 142 // copied to the encoder config by WebRtcVideoChannel. 143 // TODO(https://crbug.com/1315155): Remove the ability to set it in Chromium 144 // and delete this flag (it should default to 100 kbps). 145 std::optional<int> screencast_min_bitrate_kbps; 146 // Set by screencast sources. Implies selection of encoding settings 147 // suitable for screencast. Most likely not the right way to do 148 // things, e.g., screencast of a text document and screencast of a 149 // youtube video have different needs. 150 std::optional<bool> is_screencast; 151 VideoTrackInterface::ContentHint content_hint; 152 153 private: 154 template <typename T> 155 static void SetFrom(std::optional<T>* s, const std::optional<T>& o) { 156 if (o) { 157 *s = o; 158 } 159 } 160 }; 161 162 class MediaChannelNetworkInterface { 163 public: 164 enum SocketType { ST_RTP, ST_RTCP }; 165 virtual bool SendPacket(CopyOnWriteBuffer* packet, 166 const AsyncSocketPacketOptions& options) = 0; 167 virtual bool SendRtcp(CopyOnWriteBuffer* packet, 168 const AsyncSocketPacketOptions& options) = 0; 169 virtual int SetOption(SocketType type, Socket::Option opt, int option) = 0; 170 virtual ~MediaChannelNetworkInterface() {} 171 }; 172 173 class MediaSendChannelInterface { 174 public: 175 virtual ~MediaSendChannelInterface() = default; 176 177 virtual VideoMediaSendChannelInterface* AsVideoSendChannel() = 0; 178 179 virtual VoiceMediaSendChannelInterface* AsVoiceSendChannel() = 0; 180 virtual MediaType media_type() const = 0; 181 182 // Gets the currently set codecs/payload types to be used for outgoing media. 183 virtual std::optional<Codec> GetSendCodec() const = 0; 184 185 // Creates a new outgoing media stream with SSRCs and CNAME as described 186 // by sp. 187 virtual bool AddSendStream(const StreamParams& sp) = 0; 188 // Removes an outgoing media stream. 189 // SSRC must be the first SSRC of the media stream if the stream uses 190 // multiple SSRCs. In the case of an ssrc of 0, the possibly cached 191 // StreamParams is removed. 192 virtual bool RemoveSendStream(uint32_t ssrc) = 0; 193 // Called on the network thread after a transport has finished sending a 194 // packet. 195 virtual void OnPacketSent(const SentPacketInfo& sent_packet) = 0; 196 // Called when the socket's ability to send has changed. 197 virtual void OnReadyToSend(bool ready) = 0; 198 // Called when the network route used for sending packets changed. 199 virtual void OnNetworkRouteChanged(absl::string_view transport_name, 200 const NetworkRoute& network_route) = 0; 201 // Sets the abstract interface class for sending RTP/RTCP data. 202 virtual void SetInterface(MediaChannelNetworkInterface* iface) = 0; 203 204 // Returns `true` if a non-null MediaChannelNetworkInterface pointer is held. 205 // Must be called on the network thread. 206 virtual bool HasNetworkInterface() const = 0; 207 208 // Corresponds to the SDP attribute extmap-allow-mixed, see RFC8285. 209 // Set to true if it's allowed to mix one- and two-byte RTP header extensions 210 // in the same stream. The setter and getter must only be called from 211 // worker_thread. 212 virtual void SetExtmapAllowMixed(bool extmap_allow_mixed) = 0; 213 virtual bool ExtmapAllowMixed() const = 0; 214 215 // Set the frame encryptor to use on all outgoing frames. This is optional. 216 // This pointers lifetime is managed by the set of RtpSender it is attached 217 // to. 218 virtual void SetFrameEncryptor( 219 uint32_t ssrc, 220 scoped_refptr<FrameEncryptorInterface> frame_encryptor) = 0; 221 222 virtual RTCError SetRtpSendParameters( 223 uint32_t ssrc, 224 const RtpParameters& parameters, 225 SetParametersCallback callback = nullptr) = 0; 226 227 virtual void SetEncoderToPacketizerFrameTransformer( 228 uint32_t ssrc, 229 scoped_refptr<FrameTransformerInterface> frame_transformer) = 0; 230 231 // note: The encoder_selector object must remain valid for the lifetime of the 232 // MediaChannel, unless replaced. 233 virtual void SetEncoderSelector( 234 uint32_t /* ssrc */, 235 VideoEncoderFactory::EncoderSelectorInterface* /* encoder_selector */) {} 236 virtual RtpParameters GetRtpSendParameters(uint32_t ssrc) const = 0; 237 virtual bool SendCodecHasNack() const = 0; 238 // Called whenever the list of sending SSRCs changes. 239 virtual void SetSsrcListChangedCallback( 240 absl::AnyInvocable<void(const std::set<uint32_t>&)> callback) = 0; 241 // TODO(bugs.webrtc.org/13931): Remove when configuration is more sensible 242 virtual void SetSendCodecChangedCallback( 243 absl::AnyInvocable<void()> callback) = 0; 244 }; 245 246 class MediaReceiveChannelInterface { 247 public: 248 virtual ~MediaReceiveChannelInterface() = default; 249 250 virtual VideoMediaReceiveChannelInterface* AsVideoReceiveChannel() = 0; 251 virtual VoiceMediaReceiveChannelInterface* AsVoiceReceiveChannel() = 0; 252 253 virtual MediaType media_type() const = 0; 254 // Creates a new incoming media stream with SSRCs, CNAME as described 255 // by sp. In the case of a sp without SSRCs, the unsignaled sp is cached 256 // to be used later for unsignaled streams received. 257 virtual bool AddRecvStream(const StreamParams& sp) = 0; 258 // Removes an incoming media stream. 259 // ssrc must be the first SSRC of the media stream if the stream uses 260 // multiple SSRCs. 261 virtual bool RemoveRecvStream(uint32_t ssrc) = 0; 262 // Resets any cached StreamParams for an unsignaled RecvStream, and removes 263 // any existing unsignaled streams. 264 virtual void ResetUnsignaledRecvStream() = 0; 265 // Sets the abstract interface class for sending RTP/RTCP data. 266 virtual void SetInterface(MediaChannelNetworkInterface* iface) = 0; 267 // Called on the network when an RTP packet is received. 268 virtual void OnPacketReceived(const RtpPacketReceived& packet) = 0; 269 // Gets the current unsignaled receive stream's SSRC, if there is one. 270 virtual std::optional<uint32_t> GetUnsignaledSsrc() const = 0; 271 // Sets the local SSRC for listening to incoming RTCP reports. 272 virtual void ChooseReceiverReportSsrc(const std::set<uint32_t>& choices) = 0; 273 // This is currently a workaround because of the demuxer state being managed 274 // across two separate threads. Once the state is consistently managed on 275 // the same thread (network), this workaround can be removed. 276 // These two notifications inform the media channel when the transport's 277 // demuxer criteria is being updated. 278 // * OnDemuxerCriteriaUpdatePending() happens on the same thread that the 279 // channel's streams are added and removed (worker thread). 280 // * OnDemuxerCriteriaUpdateComplete() happens on the same thread. 281 // Because the demuxer is updated asynchronously, there is a window of time 282 // where packets are arriving to the channel for streams that have already 283 // been removed on the worker thread. It is important NOT to treat these as 284 // new unsignalled ssrcs. 285 virtual void OnDemuxerCriteriaUpdatePending() = 0; 286 virtual void OnDemuxerCriteriaUpdateComplete() = 0; 287 // Set the frame decryptor to use on all incoming frames. This is optional. 288 // This pointers lifetimes is managed by the set of RtpReceivers it is 289 // attached to. 290 virtual void SetFrameDecryptor( 291 uint32_t ssrc, 292 scoped_refptr<FrameDecryptorInterface> frame_decryptor) = 0; 293 294 virtual void SetDepacketizerToDecoderFrameTransformer( 295 uint32_t ssrc, 296 scoped_refptr<FrameTransformerInterface> frame_transformer) = 0; 297 298 // Set base minimum delay of the receive stream with specified ssrc. 299 // Base minimum delay sets lower bound on minimum delay value which 300 // determines minimum delay until audio playout. 301 // Returns false if there is no stream with given ssrc. 302 virtual bool SetBaseMinimumPlayoutDelayMs(uint32_t ssrc, int delay_ms) = 0; 303 304 // Returns current value of base minimum delay in milliseconds. 305 virtual std::optional<int> GetBaseMinimumPlayoutDelayMs( 306 uint32_t ssrc) const = 0; 307 }; 308 309 // The stats information is structured as follows: 310 // Media are represented by either MediaSenderInfo or MediaReceiverInfo. 311 // Media contains a vector of SSRC infos that are exclusively used by this 312 // media. (SSRCs shared between media streams can't be represented.) 313 314 // Information about an SSRC. 315 // This data may be locally recorded, or received in an RTCP SR or RR. 316 struct SsrcSenderInfo { 317 uint32_t ssrc = 0; 318 double timestamp = 0.0; // NTP timestamp, represented as seconds since epoch. 319 }; 320 321 struct SsrcReceiverInfo { 322 uint32_t ssrc = 0; 323 double timestamp = 0.0; 324 }; 325 326 struct MediaSenderInfo { 327 MediaSenderInfo(); 328 ~MediaSenderInfo(); 329 void add_ssrc(const SsrcSenderInfo& stat) { local_stats.push_back(stat); } 330 // Temporary utility function for call sites that only provide SSRC. 331 // As more info is added into SsrcSenderInfo, this function should go away. 332 void add_ssrc(uint32_t ssrc) { 333 SsrcSenderInfo stat; 334 stat.ssrc = ssrc; 335 add_ssrc(stat); 336 } 337 // Utility accessor for clients that are only interested in ssrc numbers. 338 std::vector<uint32_t> ssrcs() const { 339 std::vector<uint32_t> retval; 340 for (std::vector<SsrcSenderInfo>::const_iterator it = local_stats.begin(); 341 it != local_stats.end(); ++it) { 342 retval.push_back(it->ssrc); 343 } 344 return retval; 345 } 346 // Returns true if the media has been connected. 347 bool connected() const { return local_stats.size() > 0; } 348 // Utility accessor for clients that make the assumption only one ssrc 349 // exists per media. 350 // This will eventually go away. 351 // Call sites that compare this to zero should use connected() instead. 352 // https://bugs.webrtc.org/8694 353 uint32_t ssrc() const { 354 if (connected()) { 355 return local_stats[0].ssrc; 356 } else { 357 return 0; 358 } 359 } 360 // https://w3c.github.io/webrtc-stats/#dom-rtcsentrtpstreamstats-bytessent 361 int64_t payload_bytes_sent = 0; 362 // https://w3c.github.io/webrtc-stats/#dom-rtcoutboundrtpstreamstats-headerbytessent 363 int64_t header_and_padding_bytes_sent = 0; 364 // https://w3c.github.io/webrtc-stats/#dom-rtcoutboundrtpstreamstats-retransmittedbytessent 365 uint64_t retransmitted_bytes_sent = 0; 366 int packets_sent = 0; 367 // https://w3c.github.io/webrtc-stats/#dom-rtcoutboundrtpstreamstats-packetssentwithect1 368 int64_t packets_sent_with_ect1 = 0; 369 // https://w3c.github.io/webrtc-stats/#dom-rtcoutboundrtpstreamstats-retransmittedpacketssent 370 uint64_t retransmitted_packets_sent = 0; 371 // https://w3c.github.io/webrtc-stats/#dom-rtcoutboundrtpstreamstats-nackcount 372 uint32_t nacks_received = 0; 373 // https://w3c.github.io/webrtc-stats/#dom-rtcoutboundrtpstreamstats-targetbitrate 374 std::optional<DataRate> target_bitrate; 375 int packets_lost = 0; 376 float fraction_lost = 0.0f; 377 int64_t rtt_ms = 0; 378 std::string codec_name; 379 std::optional<int> codec_payload_type; 380 std::vector<SsrcSenderInfo> local_stats; 381 std::vector<SsrcReceiverInfo> remote_stats; 382 // A snapshot of the most recent Report Block with additional data of interest 383 // to statistics. Used to implement RTCRemoteInboundRtpStreamStats. Within 384 // this list, the `ReportBlockData::source_ssrc()`, which is the SSRC of the 385 // corresponding outbound RTP stream, is unique. 386 std::vector<ReportBlockData> report_block_datas; 387 std::optional<bool> active; 388 // https://w3c.github.io/webrtc-stats/#dom-rtcoutboundrtpstreamstats-totalpacketsenddelay 389 TimeDelta total_packet_send_delay = TimeDelta::Zero(); 390 }; 391 392 struct MediaReceiverInfo { 393 MediaReceiverInfo(); 394 ~MediaReceiverInfo(); 395 396 void add_ssrc(const SsrcReceiverInfo& stat) { local_stats.push_back(stat); } 397 // Temporary utility function for call sites that only provide SSRC. 398 // As more info is added into SsrcSenderInfo, this function should go away. 399 void add_ssrc(uint32_t ssrc) { 400 SsrcReceiverInfo stat; 401 stat.ssrc = ssrc; 402 add_ssrc(stat); 403 } 404 std::vector<uint32_t> ssrcs() const { 405 std::vector<uint32_t> retval; 406 for (std::vector<SsrcReceiverInfo>::const_iterator it = local_stats.begin(); 407 it != local_stats.end(); ++it) { 408 retval.push_back(it->ssrc); 409 } 410 return retval; 411 } 412 // Returns true if the media has been connected. 413 bool connected() const { return local_stats.size() > 0; } 414 // Utility accessor for clients that make the assumption only one ssrc 415 // exists per media. 416 // This will eventually go away. 417 // Call sites that compare this to zero should use connected(); 418 // https://bugs.webrtc.org/8694 419 uint32_t ssrc() const { 420 if (connected()) { 421 return local_stats[0].ssrc; 422 } else { 423 return 0; 424 } 425 } 426 427 // https://w3c.github.io/webrtc-stats/#dom-rtcinboundrtpstreamstats-bytesreceived 428 int64_t payload_bytes_received = 0; 429 // https://w3c.github.io/webrtc-stats/#dom-rtcinboundrtpstreamstats-headerbytesreceived 430 int64_t header_and_padding_bytes_received = 0; 431 int packets_received = 0; 432 // https://w3c.github.io/webrtc-stats/#dom-rtcreceivedrtpstreamstats-packetsreceivedwithect1 433 int64_t packets_received_with_ect1 = 0; 434 // https://w3c.github.io/webrtc-stats/#dom-rtcreceivedrtpstreamstats-packetsreceivedwithce 435 int64_t packets_received_with_ce = 0; 436 int packets_lost = 0; 437 438 std::optional<uint64_t> retransmitted_bytes_received; 439 std::optional<uint64_t> retransmitted_packets_received; 440 std::optional<uint32_t> nacks_sent; 441 // Jitter (network-related) latency (cumulative). 442 // https://w3c.github.io/webrtc-stats/#dom-rtcinboundrtpstreamstats-jitterbufferdelay 443 double jitter_buffer_delay_seconds = 0.0; 444 // Target delay for the jitter buffer (cumulative). 445 // https://w3c.github.io/webrtc-stats/#dom-rtcinboundrtpstreamstats-jitterbuffertargetdelay 446 double jitter_buffer_target_delay_seconds = 0.0; 447 // Minimum obtainable delay for the jitter buffer (cumulative). 448 // https://w3c.github.io/webrtc-stats/#dom-rtcinboundrtpstreamstats-jitterbufferminimumdelay 449 double jitter_buffer_minimum_delay_seconds = 0.0; 450 // Number of observations for cumulative jitter latency. 451 // https://w3c.github.io/webrtc-stats/#dom-rtcinboundrtpstreamstats-jitterbufferemittedcount 452 uint64_t jitter_buffer_emitted_count = 0; 453 // The timestamp at which the last packet was received, i.e. the time of the 454 // local clock when it was received - not the RTP timestamp of that packet. 455 // https://w3c.github.io/webrtc-stats/#dom-rtcinboundrtpstreamstats-lastpacketreceivedtimestamp 456 std::optional<Timestamp> last_packet_received; 457 // https://w3c.github.io/webrtc-stats/#dom-rtcinboundrtpstreamstats-estimatedplayouttimestamp 458 std::optional<int64_t> estimated_playout_ntp_timestamp_ms; 459 std::string codec_name; 460 std::optional<int> codec_payload_type; 461 std::vector<SsrcReceiverInfo> local_stats; 462 std::vector<SsrcSenderInfo> remote_stats; 463 // https://w3c.github.io/webrtc-stats/#dom-rtcinboundrtpstreamstats-fecpacketsreceived 464 std::optional<uint64_t> fec_packets_received; 465 // https://w3c.github.io/webrtc-stats/#dom-rtcinboundrtpstreamstats-fecpacketsdiscarded 466 std::optional<uint64_t> fec_packets_discarded; 467 // https://w3c.github.io/webrtc-stats/#dom-rtcinboundrtpstreamstats-fecbytesreceived 468 std::optional<uint64_t> fec_bytes_received; 469 // https://www.w3.org/TR/webrtc-stats/#dom-rtcinboundrtpstreamstats-totalprocessingdelay 470 double total_processing_delay_seconds = 0.0; 471 472 // Remote outbound stats derived by the received RTCP sender reports. 473 // https://w3c.github.io/webrtc-stats/#remoteoutboundrtpstats-dict* 474 std::optional<Timestamp> last_sender_report_timestamp; 475 // TODO: bugs.webrtc.org/370535296 - Remove the utc timestamp when linked 476 // issue is fixed. 477 std::optional<Timestamp> last_sender_report_utc_timestamp; 478 std::optional<Timestamp> last_sender_report_remote_utc_timestamp; 479 uint64_t sender_reports_packets_sent = 0; 480 uint64_t sender_reports_bytes_sent = 0; 481 uint64_t sender_reports_reports_count = 0; 482 // These require a DLRR block, see 483 // https://w3c.github.io/webrtc-stats/#dom-rtcremoteoutboundrtpstreamstats-roundtriptime 484 std::optional<TimeDelta> round_trip_time; 485 TimeDelta total_round_trip_time = TimeDelta::Zero(); 486 int round_trip_time_measurements = 0; 487 }; 488 489 struct VoiceSenderInfo : public MediaSenderInfo { 490 VoiceSenderInfo(); 491 ~VoiceSenderInfo(); 492 int jitter_ms = 0; 493 // Current audio level, expressed linearly [0,32767]. 494 int audio_level = 0; 495 // See description of "totalAudioEnergy" in the WebRTC stats spec: 496 // https://w3c.github.io/webrtc-stats/#dom-rtcmediastreamtrackstats-totalaudioenergy 497 double total_input_energy = 0.0; 498 double total_input_duration = 0.0; 499 ANAStats ana_statistics; 500 AudioProcessingStats apm_statistics; 501 }; 502 503 struct VoiceReceiverInfo : public MediaReceiverInfo { 504 VoiceReceiverInfo(); 505 ~VoiceReceiverInfo(); 506 int jitter_ms = 0; 507 int jitter_buffer_ms = 0; 508 int jitter_buffer_preferred_ms = 0; 509 int delay_estimate_ms = 0; 510 int audio_level = 0; 511 // Stats below correspond to similarly-named fields in the WebRTC stats spec. 512 // https://w3c.github.io/webrtc-stats/#dom-rtcinboundrtpstreamstats 513 double total_output_energy = 0.0; 514 uint64_t total_samples_received = 0; 515 double total_output_duration = 0.0; 516 uint64_t concealed_samples = 0; 517 uint64_t silent_concealed_samples = 0; 518 uint64_t concealment_events = 0; 519 uint64_t inserted_samples_for_deceleration = 0; 520 uint64_t removed_samples_for_acceleration = 0; 521 // Stats below correspond to similarly-named fields in the WebRTC stats spec. 522 // https://w3c.github.io/webrtc-stats/#dom-rtcreceivedrtpstreamstats 523 uint64_t packets_discarded = 0; 524 // Stats below DO NOT correspond directly to anything in the WebRTC stats 525 // fraction of synthesized audio inserted through expansion. 526 float expand_rate = 0.0f; 527 // fraction of synthesized speech inserted through expansion. 528 float speech_expand_rate = 0.0f; 529 // fraction of data out of secondary decoding, including FEC and RED. 530 float secondary_decoded_rate = 0.0f; 531 // Fraction of secondary data, including FEC and RED, that is discarded. 532 // Discarding of secondary data can be caused by the reception of the primary 533 // data, obsoleting the secondary data. It can also be caused by early 534 // or late arrival of secondary data. This metric is the percentage of 535 // discarded secondary data since last query of receiver info. 536 float secondary_discarded_rate = 0.0f; 537 // Fraction of data removed through time compression. 538 float accelerate_rate = 0.0f; 539 // Fraction of data inserted through time stretching. 540 float preemptive_expand_rate = 0.0f; 541 int decoding_calls_to_silence_generator = 0; 542 int decoding_calls_to_neteq = 0; 543 int decoding_normal = 0; 544 // TODO(alexnarest): Consider decoding_neteq_plc for consistency 545 int decoding_plc = 0; 546 int decoding_codec_plc = 0; 547 int decoding_cng = 0; 548 int decoding_plc_cng = 0; 549 int decoding_muted_output = 0; 550 // Estimated capture start time in NTP time in ms. 551 int64_t capture_start_ntp_time_ms = -1; 552 // Count of the number of buffer flushes. 553 uint64_t jitter_buffer_flushes = 0; 554 // Number of samples expanded due to delayed packets. 555 uint64_t delayed_packet_outage_samples = 0; 556 // Arrival delay of received audio packets. 557 double relative_packet_arrival_delay_seconds = 0.0; 558 // Count and total duration of audio interruptions (loss-concealement periods 559 // longer than 150 ms). 560 int32_t interruption_count = 0; 561 int32_t total_interruption_duration_ms = 0; 562 }; 563 564 struct VideoSenderInfo : public MediaSenderInfo { 565 VideoSenderInfo(); 566 ~VideoSenderInfo(); 567 std::optional<size_t> encoding_index; 568 std::vector<SsrcGroup> ssrc_groups; 569 std::optional<std::string> encoder_implementation_name; 570 int firs_received = 0; 571 int plis_received = 0; 572 int send_frame_width = 0; 573 int send_frame_height = 0; 574 int frames = 0; 575 double framerate_input = 0; 576 int framerate_sent = 0; 577 int aggregated_framerate_sent = 0; 578 int nominal_bitrate = 0; 579 int adapt_reason = 0; 580 int adapt_changes = 0; 581 // https://w3c.github.io/webrtc-stats/#dom-rtcoutboundrtpstreamstats-qualitylimitationreason 582 QualityLimitationReason quality_limitation_reason = 583 QualityLimitationReason::kNone; 584 // https://w3c.github.io/webrtc-stats/#dom-rtcoutboundrtpstreamstats-qualitylimitationdurations 585 std::map<QualityLimitationReason, int64_t> quality_limitation_durations_ms; 586 // https://w3c.github.io/webrtc-stats/#dom-rtcoutboundrtpstreamstats-qualitylimitationresolutionchanges 587 uint32_t quality_limitation_resolution_changes = 0; 588 int avg_encode_ms = 0; 589 int encode_usage_percent = 0; 590 uint32_t frames_encoded = 0; 591 uint32_t key_frames_encoded = 0; 592 // https://w3c.github.io/webrtc-stats/#dom-rtcoutboundrtpstreamstats-totalencodetime 593 uint64_t total_encode_time_ms = 0; 594 // https://w3c.github.io/webrtc-stats/#dom-rtcoutboundrtpstreamstats-totalencodedbytestarget 595 uint64_t total_encoded_bytes_target = 0; 596 bool has_entered_low_resolution = false; 597 // https://w3c.github.io/webrtc-stats/#dom-rtcinboundrtpstreamstats-qpsum 598 std::optional<uint64_t> qp_sum; 599 VideoContentType content_type = VideoContentType::UNSPECIFIED; 600 // https://w3c.github.io/webrtc-stats/#dom-rtcoutboundrtpstreamstats-psnrsum 601 webrtc::EncodedImage::Psnr psnr_sum; 602 uint32_t psnr_measurements = 0; 603 uint32_t frames_sent = 0; 604 // https://w3c.github.io/webrtc-stats/#dom-rtcvideosenderstats-hugeframessent 605 uint32_t huge_frames_sent = 0; 606 uint32_t aggregated_huge_frames_sent = 0; 607 std::optional<std::string> rid; 608 std::optional<bool> power_efficient_encoder; 609 std::optional<ScalabilityMode> scalability_mode; 610 }; 611 612 struct VideoReceiverInfo : public MediaReceiverInfo { 613 VideoReceiverInfo(); 614 ~VideoReceiverInfo(); 615 std::vector<SsrcGroup> ssrc_groups; 616 std::optional<std::string> decoder_implementation_name; 617 std::optional<bool> power_efficient_decoder; 618 int packets_concealed = 0; 619 int firs_sent = 0; 620 int plis_sent = 0; 621 int frame_width = 0; 622 int frame_height = 0; 623 int framerate_received = 0; 624 int framerate_decoded = 0; 625 int framerate_output = 0; 626 // Framerate as sent to the renderer. 627 int framerate_render_input = 0; 628 // Framerate that the renderer reports. 629 int framerate_render_output = 0; 630 uint32_t frames_received = 0; 631 uint32_t frames_dropped = 0; 632 uint32_t frames_decoded = 0; 633 uint32_t key_frames_decoded = 0; 634 uint32_t frames_rendered = 0; 635 std::optional<uint64_t> qp_sum; 636 // Corruption score, indicating the probability of corruption. Its value is 637 // between 0 and 1, where 0 means no corruption and 1 means that the 638 // compressed frame is corrupted. 639 // However, note that the corruption score may not accurately reflect 640 // corruption. E.g. even if the corruption score is 0, the compressed frame 641 // may still be corrupted and vice versa. 642 std::optional<double> corruption_score_sum; 643 std::optional<double> corruption_score_squared_sum; 644 // Number of frames the `corruption_score` was calculated on. This is 645 // usually not the same as `frames_decoded` or `frames_rendered`. 646 uint32_t corruption_score_count = 0; 647 // https://w3c.github.io/webrtc-stats/#dom-rtcinboundrtpstreamstats-totaldecodetime 648 TimeDelta total_decode_time = TimeDelta::Zero(); 649 // https://w3c.github.io/webrtc-stats/#dom-rtcinboundrtpstreamstats-totalprocessingdelay 650 TimeDelta total_processing_delay = TimeDelta::Zero(); 651 TimeDelta total_assembly_time = TimeDelta::Zero(); 652 uint32_t frames_assembled_from_multiple_packets = 0; 653 double total_inter_frame_delay = 0; 654 double total_squared_inter_frame_delay = 0; 655 int64_t interframe_delay_max_ms = -1; 656 uint32_t freeze_count = 0; 657 uint32_t pause_count = 0; 658 uint32_t total_freezes_duration_ms = 0; 659 uint32_t total_pauses_duration_ms = 0; 660 uint32_t jitter_ms = 0; 661 662 VideoContentType content_type = VideoContentType::UNSPECIFIED; 663 664 // All stats below are gathered per-VideoReceiver, but some will be correlated 665 // across MediaStreamTracks. NOTE(hta): when sinking stats into per-SSRC 666 // structures, reflect this in the new layout. 667 668 // Current frame decode latency. 669 int decode_ms = 0; 670 // Maximum observed frame decode latency. 671 int max_decode_ms = 0; 672 // Jitter (network-related) latency. 673 int jitter_buffer_ms = 0; 674 // Requested minimum playout latency. 675 int min_playout_delay_ms = 0; 676 // Requested latency to account for rendering delay. 677 int render_delay_ms = 0; 678 // Target overall delay: network+decode+render, accounting for 679 // min_playout_delay_ms. 680 int target_delay_ms = 0; 681 // Current overall delay, possibly ramping towards target_delay_ms. 682 int current_delay_ms = 0; 683 684 // Estimated capture start time in NTP time in ms. 685 int64_t capture_start_ntp_time_ms = -1; 686 687 // First frame received to first frame decoded latency. 688 int64_t first_frame_received_to_decoded_ms = -1; 689 690 // Timing frame info: all important timestamps for a full lifetime of a 691 // single 'timing frame'. 692 std::optional<TimingFrameInfo> timing_frame_info; 693 }; 694 695 struct BandwidthEstimationInfo { 696 int available_send_bandwidth = 0; 697 int available_recv_bandwidth = 0; 698 int target_enc_bitrate = 0; 699 int actual_enc_bitrate = 0; 700 int retransmit_bitrate = 0; 701 int transmit_bitrate = 0; 702 int64_t bucket_delay = 0; 703 }; 704 705 // Maps from payload type to `RtpCodecParameters`. 706 typedef std::map<int, RtpCodecParameters> RtpCodecParametersMap; 707 708 // Stats returned from VoiceMediaSendChannel.GetStats() 709 struct VoiceMediaSendInfo { 710 VoiceMediaSendInfo(); 711 ~VoiceMediaSendInfo(); 712 void Clear() { 713 senders.clear(); 714 send_codecs.clear(); 715 } 716 std::vector<VoiceSenderInfo> senders; 717 RtpCodecParametersMap send_codecs; 718 }; 719 720 // Stats returned from VoiceMediaReceiveChannel.GetStats() 721 struct VoiceMediaReceiveInfo { 722 VoiceMediaReceiveInfo(); 723 ~VoiceMediaReceiveInfo(); 724 void Clear() { 725 receivers.clear(); 726 receive_codecs.clear(); 727 } 728 std::vector<VoiceReceiverInfo> receivers; 729 RtpCodecParametersMap receive_codecs; 730 int32_t device_underrun_count = 0; 731 }; 732 733 // Combined VoiceMediaSendInfo and VoiceMediaReceiveInfo 734 // Returned from Transceiver.getStats() 735 struct VoiceMediaInfo { 736 VoiceMediaInfo(); 737 VoiceMediaInfo(VoiceMediaSendInfo&& send, VoiceMediaReceiveInfo&& receive) 738 : senders(std::move(send.senders)), 739 receivers(std::move(receive.receivers)), 740 send_codecs(std::move(send.send_codecs)), 741 receive_codecs(std::move(receive.receive_codecs)), 742 device_underrun_count(receive.device_underrun_count) {} 743 ~VoiceMediaInfo(); 744 void Clear() { 745 senders.clear(); 746 receivers.clear(); 747 send_codecs.clear(); 748 receive_codecs.clear(); 749 } 750 std::vector<VoiceSenderInfo> senders; 751 std::vector<VoiceReceiverInfo> receivers; 752 RtpCodecParametersMap send_codecs; 753 RtpCodecParametersMap receive_codecs; 754 int32_t device_underrun_count = 0; 755 }; 756 757 // Stats for a VideoMediaSendChannel 758 struct VideoMediaSendInfo { 759 VideoMediaSendInfo(); 760 ~VideoMediaSendInfo(); 761 void Clear() { 762 senders.clear(); 763 aggregated_senders.clear(); 764 send_codecs.clear(); 765 } 766 // Each sender info represents one "outbound-rtp" stream.In non - simulcast, 767 // this means one info per RtpSender but if simulcast is used this means 768 // one info per simulcast layer. 769 std::vector<VideoSenderInfo> senders; 770 // Used for legacy getStats() API's "ssrc" stats and modern getStats() API's 771 // "track" stats. If simulcast is used, instead of having one sender info per 772 // simulcast layer, the metrics of all layers of an RtpSender are aggregated 773 // into a single sender info per RtpSender. 774 std::vector<VideoSenderInfo> aggregated_senders; 775 RtpCodecParametersMap send_codecs; 776 }; 777 778 // Stats for a VideoMediaReceiveChannel 779 struct VideoMediaReceiveInfo { 780 VideoMediaReceiveInfo(); 781 ~VideoMediaReceiveInfo(); 782 void Clear() { 783 receivers.clear(); 784 receive_codecs.clear(); 785 } 786 std::vector<VideoReceiverInfo> receivers; 787 RtpCodecParametersMap receive_codecs; 788 }; 789 790 // Combined VideoMediaSenderInfo and VideoMediaReceiverInfo. 791 // Returned from channel.GetStats() 792 struct VideoMediaInfo { 793 VideoMediaInfo(); 794 VideoMediaInfo(VideoMediaSendInfo&& send, VideoMediaReceiveInfo&& receive) 795 : senders(std::move(send.senders)), 796 aggregated_senders(std::move(send.aggregated_senders)), 797 receivers(std::move(receive.receivers)), 798 send_codecs(std::move(send.send_codecs)), 799 receive_codecs(std::move(receive.receive_codecs)) {} 800 ~VideoMediaInfo(); 801 void Clear() { 802 senders.clear(); 803 aggregated_senders.clear(); 804 receivers.clear(); 805 send_codecs.clear(); 806 receive_codecs.clear(); 807 } 808 // Each sender info represents one "outbound-rtp" stream. In non-simulcast, 809 // this means one info per RtpSender but if simulcast is used this means 810 // one info per simulcast layer. 811 std::vector<VideoSenderInfo> senders; 812 // Used for legacy getStats() API's "ssrc" stats and modern getStats() API's 813 // "track" stats. If simulcast is used, instead of having one sender info per 814 // simulcast layer, the metrics of all layers of an RtpSender are aggregated 815 // into a single sender info per RtpSender. 816 std::vector<VideoSenderInfo> aggregated_senders; 817 std::vector<VideoReceiverInfo> receivers; 818 RtpCodecParametersMap send_codecs; 819 RtpCodecParametersMap receive_codecs; 820 }; 821 822 struct MediaChannelParameters { 823 virtual ~MediaChannelParameters() = default; 824 // This is the value to be sent in the MID RTP header extension (if the header 825 // extension in included in the list of extensions). 826 // It is also used as a key to map the channel to its transport. 827 std::string mid; 828 829 std::vector<Codec> codecs; 830 std::vector<RtpExtension> extensions; 831 // RTCP feedback type used for congestion control for this media channel. 832 // If transport sequence numbers are used, 'extensions' will include 833 // kTransportSequenceNumberUri. 834 std::optional<RtcpFeedbackType> rtcp_cc_ack_type; 835 // For a send stream this is true if we've negotiated a send direction, 836 // for a receive stream this is true if we've negotiated a receive direction. 837 bool is_stream_active = true; 838 839 // TODO(pthatcher): Add streams. 840 struct RtcpParameters { 841 bool reduced_size = false; 842 bool remote_estimate = false; 843 } rtcp; 844 845 std::string ToString() const { 846 StringBuilder ost; 847 ost << "{"; 848 const char* separator = ""; 849 for (const auto& entry : ToStringMap()) { 850 ost << separator << entry.first << ": " << entry.second; 851 separator = ", "; 852 } 853 ost << "}"; 854 return ost.Release(); 855 } 856 857 protected: 858 virtual std::map<std::string, std::string> ToStringMap() const { 859 return {{"codecs", VectorToString(codecs)}, 860 {"extensions", VectorToString(extensions)}, 861 {"rtcp", "{reduced_size:" + absl::StrCat(rtcp.reduced_size) + 862 ", remote_estimate:" + 863 absl::StrCat(rtcp.remote_estimate) + "}"}}; 864 } 865 }; 866 867 struct SenderParameters : MediaChannelParameters { 868 int max_bandwidth_bps = -1; 869 bool extmap_allow_mixed = false; 870 871 protected: 872 std::map<std::string, std::string> ToStringMap() const override { 873 auto params = MediaChannelParameters::ToStringMap(); 874 params["max_bandwidth_bps"] = absl::StrCat(max_bandwidth_bps); 875 params["mid"] = (mid.empty() ? "<not set>" : mid); 876 params["extmap-allow-mixed"] = extmap_allow_mixed ? "true" : "false"; 877 return params; 878 } 879 }; 880 881 struct AudioSenderParameter : SenderParameters { 882 AudioSenderParameter(); 883 ~AudioSenderParameter() override; 884 AudioOptions options; 885 886 protected: 887 std::map<std::string, std::string> ToStringMap() const override; 888 }; 889 890 struct AudioReceiverParameters : MediaChannelParameters {}; 891 892 class VoiceMediaSendChannelInterface : public MediaSendChannelInterface { 893 public: 894 virtual bool SetSenderParameters(const AudioSenderParameter& params) = 0; 895 // Starts or stops sending (and potentially capture) of local audio. 896 virtual void SetSend(bool send) = 0; 897 // Configure stream for sending. 898 virtual bool SetAudioSend(uint32_t ssrc, 899 bool enable, 900 const AudioOptions* options, 901 AudioSource* source) = 0; 902 // Returns if the telephone-event has been negotiated. 903 virtual bool CanInsertDtmf() = 0; 904 // Send a DTMF `event`. The DTMF out-of-band signal will be used. 905 // The `ssrc` should be either 0 or a valid send stream ssrc. 906 // The valid value for the `event` are 0 to 15 which corresponding to 907 // DTMF event 0-9, *, #, A-D. 908 virtual bool InsertDtmf(uint32_t ssrc, int event, int duration) = 0; 909 virtual bool GetStats(VoiceMediaSendInfo* stats) = 0; 910 virtual bool SenderNackEnabled() const = 0; 911 virtual bool SenderNonSenderRttEnabled() const = 0; 912 }; 913 914 class VoiceMediaReceiveChannelInterface : public MediaReceiveChannelInterface { 915 public: 916 virtual bool SetReceiverParameters(const AudioReceiverParameters& params) = 0; 917 // Get the receive parameters for the incoming stream identified by `ssrc`. 918 virtual RtpParameters GetRtpReceiverParameters(uint32_t ssrc) const = 0; 919 virtual std::vector<RtpSource> GetSources(uint32_t ssrc) const = 0; 920 // Retrieve the receive parameters for the default receive 921 // stream, which is used when SSRCs are not signaled. 922 virtual RtpParameters GetDefaultRtpReceiveParameters() const = 0; 923 // Starts or stops playout of received audio. 924 virtual void SetPlayout(bool playout) = 0; 925 // Set speaker output volume of the specified ssrc. 926 virtual bool SetOutputVolume(uint32_t ssrc, double volume) = 0; 927 // Set speaker output volume for future unsignaled streams. 928 virtual bool SetDefaultOutputVolume(double volume) = 0; 929 virtual void SetRawAudioSink(uint32_t ssrc, 930 std::unique_ptr<AudioSinkInterface> sink) = 0; 931 virtual void SetDefaultRawAudioSink( 932 std::unique_ptr<AudioSinkInterface> sink) = 0; 933 virtual bool GetStats(VoiceMediaReceiveInfo* stats, bool reset_legacy) = 0; 934 virtual enum RtcpMode RtcpMode() const = 0; 935 virtual void SetRtcpMode(enum RtcpMode mode) = 0; 936 virtual void SetReceiveNackEnabled(bool enabled) = 0; 937 virtual void SetReceiveNonSenderRttEnabled(bool enabled) = 0; 938 }; 939 940 struct VideoSenderParameters : SenderParameters { 941 VideoSenderParameters(); 942 ~VideoSenderParameters() override; 943 // Use conference mode? This flag comes from the remote 944 // description's SDP line 'a=x-google-flag:conference', copied over 945 // by VideoChannel::SetRemoteContent_w, and ultimately used by 946 // conference mode screencast logic in 947 // WebRtcVideoChannel::WebRtcVideoSendStream::CreateVideoEncoderConfig. 948 // The special screencast behaviour is disabled by default. 949 bool conference_mode = false; 950 951 protected: 952 std::map<std::string, std::string> ToStringMap() const override; 953 }; 954 955 struct VideoReceiverParameters : MediaChannelParameters {}; 956 957 class VideoMediaSendChannelInterface : public MediaSendChannelInterface { 958 public: 959 virtual bool SetSenderParameters(const VideoSenderParameters& params) = 0; 960 // Starts or stops transmission (and potentially capture) of local video. 961 virtual bool SetSend(bool send) = 0; 962 // Configure stream for sending and register a source. 963 // The `ssrc` must correspond to a registered send stream. 964 virtual bool SetVideoSend(uint32_t ssrc, 965 const VideoOptions* options, 966 VideoSourceInterface<VideoFrame>* source) = 0; 967 // Cause generation of a keyframe for `ssrc` on a sending channel. 968 virtual void GenerateSendKeyFrame(uint32_t ssrc, 969 const std::vector<std::string>& rids) = 0; 970 virtual bool GetStats(VideoMediaSendInfo* stats) = 0; 971 // This fills the "bitrate parts" (rtx, video bitrate) of the 972 // BandwidthEstimationInfo, since that part that isn't possible to get 973 // through Call::GetStats, as they are statistics of the send 974 // streams. 975 // TODO(holmer): We should change this so that either BWE graphs doesn't 976 // need access to bitrates of the streams, or change the (RTC)StatsCollector 977 // so that it's getting the send stream stats separately by calling 978 // GetStats(), and merges with BandwidthEstimationInfo by itself. 979 virtual void FillBitrateInfo(BandwidthEstimationInfo* bwe_info) = 0; 980 // Information queries to support SetReceiverFeedbackParameters 981 virtual RtcpMode SendCodecRtcpMode() const = 0; 982 virtual bool SendCodecHasLntf() const = 0; 983 virtual std::optional<int> SendCodecRtxTime() const = 0; 984 }; 985 986 class VideoMediaReceiveChannelInterface : public MediaReceiveChannelInterface { 987 public: 988 virtual bool SetReceiverParameters(const VideoReceiverParameters& params) = 0; 989 // Get the receive parameters for the incoming stream identified by `ssrc`. 990 virtual RtpParameters GetRtpReceiverParameters(uint32_t ssrc) const = 0; 991 // Starts or stops decoding of remote video. 992 virtual void SetReceive(bool receive) = 0; 993 // Retrieve the receive parameters for the default receive 994 // stream, which is used when SSRCs are not signaled. 995 virtual RtpParameters GetDefaultRtpReceiveParameters() const = 0; 996 // Sets the sink object to be used for the specified stream. 997 virtual bool SetSink(uint32_t ssrc, VideoSinkInterface<VideoFrame>* sink) = 0; 998 // The sink is used for the 'default' stream. 999 virtual void SetDefaultSink(VideoSinkInterface<VideoFrame>* sink) = 0; 1000 // Request generation of a keyframe for `ssrc` on a receiving channel via 1001 // RTCP feedback. 1002 virtual void RequestRecvKeyFrame(uint32_t ssrc) = 0; 1003 1004 virtual std::vector<RtpSource> GetSources(uint32_t ssrc) const = 0; 1005 // Set recordable encoded frame callback for `ssrc` 1006 virtual void SetRecordableEncodedFrameCallback( 1007 uint32_t ssrc, 1008 std::function<void(const RecordableEncodedFrame&)> callback) = 0; 1009 // Clear recordable encoded frame callback for `ssrc` 1010 virtual void ClearRecordableEncodedFrameCallback(uint32_t ssrc) = 0; 1011 virtual bool GetStats(VideoMediaReceiveInfo* stats) = 0; 1012 virtual void SetReceiverFeedbackParameters(bool lntf_enabled, 1013 bool nack_enabled, 1014 RtcpMode rtcp_mode, 1015 std::optional<int> rtx_time) = 0; 1016 virtual bool AddDefaultRecvStreamForTesting(const StreamParams& sp) = 0; 1017 }; 1018 1019 } // namespace webrtc 1020 1021 1022 #endif // MEDIA_BASE_MEDIA_CHANNEL_H_